blob: 7b8c66098c3d5100a1ab513950618bcc20ff5aed [file] [log] [blame]
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +00001/* Copyright (C) 2002-2004 RealVNC Ltd. All Rights Reserved.
Constantin Kaplinskyd31fd312005-09-08 19:29:02 +00002 * Copyright (C) 2004-2005 Constantin Kaplinsky. All Rights Reserved.
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +00003 *
4 * This is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This software is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this software; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
17 * USA.
18 */
Constantin Kaplinskyd31fd312005-09-08 19:29:02 +000019
20// FIXME: Check cases when screen width/height is not a multiply of 32.
21// e.g. 800x600.
22
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +000023#include <strings.h>
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +000024#include <sys/types.h>
Constantin Kaplinskyd31fd312005-09-08 19:29:02 +000025#include <sys/stat.h>
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +000026#include <unistd.h>
27#include <errno.h>
28#include <rfb/Logger_stdio.h>
29#include <rfb/LogWriter.h>
30#include <rfb/VNCServerST.h>
31#include <rfb/Configuration.h>
32#include <rfb/SSecurityFactoryStandard.h>
33
34#include <network/TcpSocket.h>
35
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +000036#include <signal.h>
37#include <X11/X.h>
38#include <X11/Xlib.h>
39#include <X11/Xutil.h>
Constantin Kaplinskyd31fd312005-09-08 19:29:02 +000040#ifdef HAVE_XTEST
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +000041#include <X11/extensions/XTest.h>
Constantin Kaplinskyd31fd312005-09-08 19:29:02 +000042#endif
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +000043
Constantin Kaplinskyaf1891c2005-09-29 06:18:28 +000044#include <x0vncserver/Image.h>
45#include <x0vncserver/PollingManager.h>
46#include <x0vncserver/CPUMonitor.h>
Constantin Kaplinskyfbaab7f2006-02-16 04:51:38 +000047#include <x0vncserver/TimeMillis.h>
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +000048
49using namespace rfb;
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +000050using namespace network;
51
52LogWriter vlog("main");
53
Constantin Kaplinsky602f34d2005-09-14 16:11:41 +000054IntParameter pollingCycle("PollingCycle", "Milliseconds per one polling "
55 "cycle; actual interval may be dynamically "
Constantin Kaplinskyef7ac9b2006-02-10 12:26:54 +000056 "adjusted to satisfy MaxProcessorUsage setting", 30);
Constantin Kaplinsky602f34d2005-09-14 16:11:41 +000057IntParameter maxProcessorUsage("MaxProcessorUsage", "Maximum percentage of "
58 "CPU time to be consumed", 35);
Constantin Kaplinskyd31fd312005-09-08 19:29:02 +000059BoolParameter useShm("UseSHM", "Use MIT-SHM extension if available", true);
60BoolParameter useOverlay("OverlayMode", "Use overlay mode under "
61 "IRIX or Solaris", true);
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +000062StringParameter displayname("display", "The X display", "");
63IntParameter rfbport("rfbport", "TCP port to listen for RFB protocol",5900);
Constantin Kaplinskyd31fd312005-09-08 19:29:02 +000064StringParameter hostsFile("HostsFile", "File with IP access control rules", "");
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +000065VncAuthPasswdFileParameter vncAuthPasswdFile;
66
67static void CleanupSignalHandler(int sig)
68{
69 // CleanupSignalHandler allows C++ object cleanup to happen because it calls
70 // exit() rather than the default which is to abort.
71 fprintf(stderr,"CleanupSignalHandler called\n");
72 exit(1);
73}
74
75
76class XDesktop : public SDesktop, public rfb::ColourMap
77{
78public:
79 XDesktop(Display* dpy_)
Constantin Kaplinskyd31fd312005-09-08 19:29:02 +000080 : dpy(dpy_), pb(0), server(0), oldButtonMask(0), haveXtest(false),
Constantin Kaplinskyaf1891c2005-09-29 06:18:28 +000081 maxButtons(0)
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +000082 {
Constantin Kaplinskyd31fd312005-09-08 19:29:02 +000083#ifdef HAVE_XTEST
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +000084 int xtestEventBase;
85 int xtestErrorBase;
86 int major, minor;
87
88 if (XTestQueryExtension(dpy, &xtestEventBase,
89 &xtestErrorBase, &major, &minor)) {
90 XTestGrabControl(dpy, True);
91 vlog.info("XTest extension present - version %d.%d",major,minor);
92 haveXtest = true;
93 } else {
Constantin Kaplinskyd31fd312005-09-08 19:29:02 +000094#endif
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +000095 vlog.info("XTest extension not present");
96 vlog.info("unable to inject events or display while server is grabbed");
Constantin Kaplinskyd31fd312005-09-08 19:29:02 +000097#ifdef HAVE_XTEST
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +000098 }
Constantin Kaplinskyd31fd312005-09-08 19:29:02 +000099#endif
100
101 // Determine actual number of buttons of the X pointer device.
102 unsigned char btnMap[8];
103 int numButtons = XGetPointerMapping(dpy, btnMap, 8);
104 maxButtons = (numButtons > 8) ? 8 : numButtons;
105 vlog.info("Enabling %d button%s of X pointer device",
106 maxButtons, (maxButtons != 1) ? "s" : "");
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000107
108 int dpyWidth = DisplayWidth(dpy, DefaultScreen(dpy));
109 int dpyHeight = DisplayHeight(dpy, DefaultScreen(dpy));
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000110
Constantin Kaplinskyd31fd312005-09-08 19:29:02 +0000111 ImageFactory factory((bool)useShm, (bool)useOverlay);
112 image = factory.newImage(dpy, dpyWidth, dpyHeight);
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000113 image->get(DefaultRootWindow(dpy));
114
Constantin Kaplinskyaf1891c2005-09-29 06:18:28 +0000115 pollmgr = new PollingManager(dpy, image, &factory);
116
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000117 pf.bpp = image->xim->bits_per_pixel;
118 pf.depth = image->xim->depth;
119 pf.bigEndian = (image->xim->byte_order == MSBFirst);
Constantin Kaplinskyd31fd312005-09-08 19:29:02 +0000120 pf.trueColour = image->isTrueColor();
121 pf.redShift = ffs(image->xim->red_mask) - 1;
122 pf.greenShift = ffs(image->xim->green_mask) - 1;
123 pf.blueShift = ffs(image->xim->blue_mask) - 1;
124 pf.redMax = image->xim->red_mask >> pf.redShift;
125 pf.greenMax = image->xim->green_mask >> pf.greenShift;
126 pf.blueMax = image->xim->blue_mask >> pf.blueShift;
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000127
128 pb = new FullFramePixelBuffer(pf, dpyWidth, dpyHeight,
129 (rdr::U8*)image->xim->data, this);
130 }
131 virtual ~XDesktop() {
132 delete pb;
Constantin Kaplinskyaf1891c2005-09-29 06:18:28 +0000133 delete pollmgr;
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000134 }
135
136 void setVNCServer(VNCServer* s) {
137 server = s;
Constantin Kaplinskyaf1891c2005-09-29 06:18:28 +0000138 pollmgr->setVNCServer(s);
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000139 server->setPixelBuffer(pb);
140 }
141
Constantin Kaplinskyaf1891c2005-09-29 06:18:28 +0000142 inline void poll() {
143 pollmgr->poll();
144 }
145
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000146 // -=- SDesktop interface
147
148 virtual void pointerEvent(const Point& pos, rdr::U8 buttonMask) {
Constantin Kaplinskyce676c62006-02-08 13:36:58 +0000149 pollmgr->setPointerPos(pos);
Constantin Kaplinskyd31fd312005-09-08 19:29:02 +0000150#ifdef HAVE_XTEST
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000151 if (!haveXtest) return;
152 XTestFakeMotionEvent(dpy, DefaultScreen(dpy), pos.x, pos.y, CurrentTime);
153 if (buttonMask != oldButtonMask) {
Constantin Kaplinskyd31fd312005-09-08 19:29:02 +0000154 for (int i = 0; i < maxButtons; i++) {
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000155 if ((buttonMask ^ oldButtonMask) & (1<<i)) {
156 if (buttonMask & (1<<i)) {
157 XTestFakeButtonEvent(dpy, i+1, True, CurrentTime);
158 } else {
159 XTestFakeButtonEvent(dpy, i+1, False, CurrentTime);
160 }
161 }
162 }
163 }
164 oldButtonMask = buttonMask;
Constantin Kaplinskyd31fd312005-09-08 19:29:02 +0000165#endif
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000166 }
167
168 virtual void keyEvent(rdr::U32 key, bool down) {
Constantin Kaplinskyd31fd312005-09-08 19:29:02 +0000169#ifdef HAVE_XTEST
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000170 if (!haveXtest) return;
171 int keycode = XKeysymToKeycode(dpy, key);
172 if (keycode)
173 XTestFakeKeyEvent(dpy, keycode, down, CurrentTime);
Constantin Kaplinskyd31fd312005-09-08 19:29:02 +0000174#endif
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000175 }
176
177 virtual void clientCutText(const char* str, int len) {
178 }
179
180 virtual Point getFbSize() {
181 return Point(pb->width(), pb->height());
182 }
183
184 // rfb::ColourMap callbacks
185 virtual void lookup(int index, int* r, int* g, int* b) {
186 XColor xc;
187 xc.pixel = index;
188 if (index < DisplayCells(dpy,DefaultScreen(dpy))) {
189 XQueryColor(dpy, DefaultColormap(dpy,DefaultScreen(dpy)), &xc);
190 } else {
191 xc.red = xc.green = xc.blue = 0;
192 }
193 *r = xc.red;
194 *g = xc.green;
195 *b = xc.blue;
196 }
197
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000198protected:
199 Display* dpy;
200 PixelFormat pf;
201 PixelBuffer* pb;
202 VNCServer* server;
203 Image* image;
Constantin Kaplinskyaf1891c2005-09-29 06:18:28 +0000204 PollingManager* pollmgr;
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000205 int oldButtonMask;
206 bool haveXtest;
Constantin Kaplinskyd31fd312005-09-08 19:29:02 +0000207 int maxButtons;
Constantin Kaplinskyd31fd312005-09-08 19:29:02 +0000208};
209
210
211class FileTcpFilter : public TcpFilter
212{
213
214public:
215
216 FileTcpFilter(const char *fname)
217 : TcpFilter("-"), fileName(NULL), lastModTime(0)
218 {
219 if (fname != NULL)
220 fileName = strdup((char *)fname);
221 }
222
223 virtual ~FileTcpFilter()
224 {
225 if (fileName != NULL)
226 free(fileName);
227 }
228
229 virtual bool verifyConnection(Socket* s)
230 {
231 if (!reloadRules()) {
232 vlog.error("Could not read IP filtering rules: rejecting all clients");
233 filter.clear();
234 filter.push_back(parsePattern("-"));
235 return false;
236 }
237
238 return TcpFilter::verifyConnection(s);
239 }
240
241protected:
242
243 bool reloadRules()
244 {
245 if (fileName == NULL)
246 return true;
247
248 struct stat st;
249 if (stat(fileName, &st) != 0)
250 return false;
251
252 if (st.st_mtime != lastModTime) {
253 // Actually reload only if the file was modified
254 FILE *fp = fopen(fileName, "r");
255 if (fp == NULL)
256 return false;
257
258 // Remove all the rules from the parent class
259 filter.clear();
260
261 // Parse the file contents adding rules to the parent class
262 char buf[32];
263 while (readLine(buf, 32, fp)) {
264 if (buf[0] && strchr("+-?", buf[0])) {
265 filter.push_back(parsePattern(buf));
266 }
267 }
268
269 fclose(fp);
270 lastModTime = st.st_mtime;
271 }
272 return true;
273 }
274
275protected:
276
277 char *fileName;
278 time_t lastModTime;
279
280private:
281
282 //
283 // NOTE: we silently truncate long lines in this function.
284 //
285
286 bool readLine(char *buf, int bufSize, FILE *fp)
287 {
288 if (fp == NULL || buf == NULL || bufSize == 0)
289 return false;
290
291 if (fgets(buf, bufSize, fp) == NULL)
292 return false;
293
294 char *ptr = strchr(buf, '\n');
295 if (ptr != NULL) {
296 *ptr = '\0'; // remove newline at the end
297 } else {
298 if (!feof(fp)) {
299 int c;
300 do { // skip the rest of a long line
301 c = getc(fp);
302 } while (c != '\n' && c != EOF);
303 }
304 }
305 return true;
306 }
307
308};
309
310
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000311char* programName;
312
313static void usage()
314{
315 fprintf(stderr, "\nusage: %s [<parameters>]\n", programName);
316 fprintf(stderr,"\n"
317 "Parameters can be turned on with -<param> or off with -<param>=0\n"
318 "Parameters which take a value can be specified as "
319 "-<param> <value>\n"
320 "Other valid forms are <param>=<value> -<param>=<value> "
321 "--<param>=<value>\n"
322 "Parameter names are case-insensitive. The parameters are:\n\n");
323 Configuration::listParams(79, 14);
324 exit(1);
325}
326
Constantin Kaplinskyfbaab7f2006-02-16 04:51:38 +0000327//
328// Adjust polling cycle to satisfy MaxProcessorUsage setting.
329//
330
331static void adjustPollingCycle(int *cycle, CPUMonitor *mon)
332{
333 int coeff = mon->check();
334 if (coeff < 90 || coeff > 110) {
335#ifdef DEBUG
336 int oldPollingCycle = *cycle;
337#endif
338 *cycle = (*cycle * 100 + coeff/2) / coeff;
339 if (*cycle < (int)pollingCycle) {
340 *cycle = (int)pollingCycle;
341 } else if (*cycle > (int)pollingCycle * 32) {
342 *cycle = (int)pollingCycle * 32;
343 }
344#ifdef DEBUG
345 if (*cycle != oldPollingCycle)
346 fprintf(stderr, "\t[new cycle %dms]\n", *cycle);
347#endif
348 }
349}
350
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000351int main(int argc, char** argv)
352{
353 initStdIOLoggers();
354 LogWriter::setLogParams("*:stderr:30");
355
356 programName = argv[0];
357 Display* dpy;
358
359 for (int i = 1; i < argc; i++) {
360 if (Configuration::setParam(argv[i]))
361 continue;
362
363 if (argv[i][0] == '-') {
364 if (i+1 < argc) {
365 if (Configuration::setParam(&argv[i][1], argv[i+1])) {
366 i++;
367 continue;
368 }
369 }
370 usage();
371 }
372
373 usage();
374 }
375
376 CharArray dpyStr(displayname.getData());
377 if (!(dpy = XOpenDisplay(dpyStr.buf[0] ? dpyStr.buf : 0))) {
378 fprintf(stderr,"%s: unable to open display \"%s\"\r\n",
379 programName, XDisplayName(displayname.getData()));
380 exit(1);
381 }
382
383 signal(SIGHUP, CleanupSignalHandler);
384 signal(SIGINT, CleanupSignalHandler);
385 signal(SIGTERM, CleanupSignalHandler);
386
387 try {
388 XDesktop desktop(dpy);
389 VNCServerST server("x0vncserver", &desktop);
390 desktop.setVNCServer(&server);
391
392 TcpSocket::initTcpSockets();
393 TcpListener listener((int)rfbport);
394 vlog.info("Listening on port %d", (int)rfbport);
395
Constantin Kaplinskyd31fd312005-09-08 19:29:02 +0000396 FileTcpFilter fileTcpFilter(hostsFile.getData());
397 if (strlen(hostsFile.getData()) != 0)
398 listener.setFilter(&fileTcpFilter);
399
Constantin Kaplinsky602f34d2005-09-14 16:11:41 +0000400 CPUMonitor cpumon((int)maxProcessorUsage, 1000);
401 int dynPollingCycle = (int)pollingCycle;
402
Constantin Kaplinskyfbaab7f2006-02-16 04:51:38 +0000403 TimeMillis timeSaved, timeNow;
Constantin Kaplinsky3f56fa72006-02-16 11:50:25 +0000404 fd_set rfds;
405 struct timeval tv;
Constantin Kaplinskyd31fd312005-09-08 19:29:02 +0000406
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000407 while (true) {
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000408
409 FD_ZERO(&rfds);
410 FD_SET(listener.getFd(), &rfds);
411
412 std::list<Socket*> sockets;
413 server.getSockets(&sockets);
414 std::list<Socket*>::iterator i;
Constantin Kaplinsky3f56fa72006-02-16 11:50:25 +0000415 int clients_connected = 0;
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000416 for (i = sockets.begin(); i != sockets.end(); i++) {
417 FD_SET((*i)->getFd(), &rfds);
Constantin Kaplinsky3f56fa72006-02-16 11:50:25 +0000418 clients_connected++;
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000419 }
420
Constantin Kaplinsky3f56fa72006-02-16 11:50:25 +0000421 if (clients_connected) {
422 int poll_ms = 20;
423 if (timeNow.update()) {
424 poll_ms = timeNow.diffFrom(timeSaved);
425 }
426 int wait_ms = dynPollingCycle - poll_ms;
427 if (wait_ms < 0) {
428 wait_ms = 0;
429 } else if (wait_ms > 500) {
430 wait_ms = 500;
431 }
432 tv.tv_usec = wait_ms * 1000;
433#ifdef DEBUG
434 fprintf(stderr, "[%d]\t", wait_ms);
435#endif
436 } else {
437 tv.tv_usec = 50000;
438 }
439 tv.tv_sec = 0;
440
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000441 int n = select(FD_SETSIZE, &rfds, 0, 0, &tv);
Constantin Kaplinsky659e9002005-09-09 08:32:02 +0000442 if (n < 0) {
443 if (errno == EINTR) {
444 vlog.debug("interrupted select() system call");
445 continue;
446 } else {
447 throw rdr::SystemException("select", errno);
448 }
449 }
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000450
451 if (FD_ISSET(listener.getFd(), &rfds)) {
452 Socket* sock = listener.accept();
Constantin Kaplinskyd31fd312005-09-08 19:29:02 +0000453 if (sock) {
454 server.addClient(sock);
Constantin Kaplinsky602f34d2005-09-14 16:11:41 +0000455 cpumon.update(); // count time from now
Constantin Kaplinskyd31fd312005-09-08 19:29:02 +0000456 } else {
457 vlog.status("Client connection rejected");
458 }
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000459 }
460
461 server.getSockets(&sockets);
Constantin Kaplinskyd31fd312005-09-08 19:29:02 +0000462
463 // Nothing more to do if there are no client connections.
464 if (sockets.empty())
465 continue;
466
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000467 for (i = sockets.begin(); i != sockets.end(); i++) {
468 if (FD_ISSET((*i)->getFd(), &rfds)) {
469 server.processSocketEvent(*i);
470 }
471 }
472
473 server.checkTimeouts();
Constantin Kaplinskyd31fd312005-09-08 19:29:02 +0000474
Constantin Kaplinskyfbaab7f2006-02-16 04:51:38 +0000475 if (timeNow.update()) {
476 int diff = timeNow.diffFrom(timeSaved);
Constantin Kaplinsky602f34d2005-09-14 16:11:41 +0000477 if (diff >= dynPollingCycle) {
Constantin Kaplinskyfbaab7f2006-02-16 04:51:38 +0000478 adjustPollingCycle(&dynPollingCycle, &cpumon);
Constantin Kaplinskyd31fd312005-09-08 19:29:02 +0000479 timeSaved = timeNow;
480 desktop.poll();
481 }
482 } else {
Constantin Kaplinskyfbaab7f2006-02-16 04:51:38 +0000483 // Something strange has happened -- TimeMillis::update() failed.
Constantin Kaplinskyd31fd312005-09-08 19:29:02 +0000484 // Poll after each select(), as in the original VNC4 code.
485 desktop.poll();
486 }
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000487 }
488
489 } catch (rdr::Exception &e) {
490 vlog.error(e.str());
491 };
492
493 return 0;
494}