blob: f718164fd33f14871f8d56134d48df7f29414304 [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>
24#include <sys/time.h>
25#include <sys/types.h>
Constantin Kaplinskyd31fd312005-09-08 19:29:02 +000026#include <sys/stat.h>
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +000027#include <unistd.h>
28#include <errno.h>
29#include <rfb/Logger_stdio.h>
30#include <rfb/LogWriter.h>
31#include <rfb/VNCServerST.h>
32#include <rfb/Configuration.h>
33#include <rfb/SSecurityFactoryStandard.h>
34
35#include <network/TcpSocket.h>
36
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +000037#include <signal.h>
38#include <X11/X.h>
39#include <X11/Xlib.h>
40#include <X11/Xutil.h>
Constantin Kaplinskyd31fd312005-09-08 19:29:02 +000041#ifdef HAVE_XTEST
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +000042#include <X11/extensions/XTest.h>
Constantin Kaplinskyd31fd312005-09-08 19:29:02 +000043#endif
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +000044
Constantin Kaplinskyaf1891c2005-09-29 06:18:28 +000045#include <x0vncserver/Image.h>
46#include <x0vncserver/PollingManager.h>
47#include <x0vncserver/CPUMonitor.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 "
56 "adjusted to satisfy MaxProcessorUsage setting", 50);
57IntParameter 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
327int main(int argc, char** argv)
328{
329 initStdIOLoggers();
330 LogWriter::setLogParams("*:stderr:30");
331
332 programName = argv[0];
333 Display* dpy;
334
335 for (int i = 1; i < argc; i++) {
336 if (Configuration::setParam(argv[i]))
337 continue;
338
339 if (argv[i][0] == '-') {
340 if (i+1 < argc) {
341 if (Configuration::setParam(&argv[i][1], argv[i+1])) {
342 i++;
343 continue;
344 }
345 }
346 usage();
347 }
348
349 usage();
350 }
351
352 CharArray dpyStr(displayname.getData());
353 if (!(dpy = XOpenDisplay(dpyStr.buf[0] ? dpyStr.buf : 0))) {
354 fprintf(stderr,"%s: unable to open display \"%s\"\r\n",
355 programName, XDisplayName(displayname.getData()));
356 exit(1);
357 }
358
359 signal(SIGHUP, CleanupSignalHandler);
360 signal(SIGINT, CleanupSignalHandler);
361 signal(SIGTERM, CleanupSignalHandler);
362
363 try {
364 XDesktop desktop(dpy);
365 VNCServerST server("x0vncserver", &desktop);
366 desktop.setVNCServer(&server);
367
368 TcpSocket::initTcpSockets();
369 TcpListener listener((int)rfbport);
370 vlog.info("Listening on port %d", (int)rfbport);
371
Constantin Kaplinskyd31fd312005-09-08 19:29:02 +0000372 FileTcpFilter fileTcpFilter(hostsFile.getData());
373 if (strlen(hostsFile.getData()) != 0)
374 listener.setFilter(&fileTcpFilter);
375
Constantin Kaplinsky602f34d2005-09-14 16:11:41 +0000376 CPUMonitor cpumon((int)maxProcessorUsage, 1000);
377 int dynPollingCycle = (int)pollingCycle;
378
Constantin Kaplinskyd31fd312005-09-08 19:29:02 +0000379 struct timeval timeSaved, timeNow;
380 struct timezone tz;
381 gettimeofday(&timeSaved, &tz);
382 timeSaved.tv_sec -= 60;
383
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000384 while (true) {
385 fd_set rfds;
386 struct timeval tv;
387
388 tv.tv_sec = 0;
Constantin Kaplinsky602f34d2005-09-14 16:11:41 +0000389 tv.tv_usec = dynPollingCycle * 1000;
390 if (tv.tv_usec > 500000) {
391 tv.tv_usec = 500000;
392 }
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000393
394 FD_ZERO(&rfds);
395 FD_SET(listener.getFd(), &rfds);
396
397 std::list<Socket*> sockets;
398 server.getSockets(&sockets);
399 std::list<Socket*>::iterator i;
400 for (i = sockets.begin(); i != sockets.end(); i++) {
401 FD_SET((*i)->getFd(), &rfds);
402 }
403
404 int n = select(FD_SETSIZE, &rfds, 0, 0, &tv);
Constantin Kaplinsky659e9002005-09-09 08:32:02 +0000405 if (n < 0) {
406 if (errno == EINTR) {
407 vlog.debug("interrupted select() system call");
408 continue;
409 } else {
410 throw rdr::SystemException("select", errno);
411 }
412 }
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000413
414 if (FD_ISSET(listener.getFd(), &rfds)) {
415 Socket* sock = listener.accept();
Constantin Kaplinskyd31fd312005-09-08 19:29:02 +0000416 if (sock) {
417 server.addClient(sock);
Constantin Kaplinsky602f34d2005-09-14 16:11:41 +0000418 cpumon.update(); // count time from now
Constantin Kaplinskyd31fd312005-09-08 19:29:02 +0000419 } else {
420 vlog.status("Client connection rejected");
421 }
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000422 }
423
424 server.getSockets(&sockets);
Constantin Kaplinskyd31fd312005-09-08 19:29:02 +0000425
426 // Nothing more to do if there are no client connections.
427 if (sockets.empty())
428 continue;
429
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000430 for (i = sockets.begin(); i != sockets.end(); i++) {
431 if (FD_ISSET((*i)->getFd(), &rfds)) {
432 server.processSocketEvent(*i);
433 }
434 }
435
436 server.checkTimeouts();
Constantin Kaplinskyd31fd312005-09-08 19:29:02 +0000437
438 if (gettimeofday(&timeNow, &tz) == 0) {
439 int diff = (int)((timeNow.tv_usec - timeSaved.tv_usec + 500) / 1000 +
440 (timeNow.tv_sec - timeSaved.tv_sec) * 1000);
Constantin Kaplinsky602f34d2005-09-14 16:11:41 +0000441 if (diff >= dynPollingCycle) {
Constantin Kaplinskyd31fd312005-09-08 19:29:02 +0000442 timeSaved = timeNow;
Constantin Kaplinsky602f34d2005-09-14 16:11:41 +0000443 int coeff = cpumon.check();
444 if (coeff < 90 || coeff > 110) {
445 // Adjust polling cycle to satisfy MaxProcessorUsage setting
446 dynPollingCycle = (dynPollingCycle * 100 + coeff/2) / coeff;
447 if (dynPollingCycle < (int)pollingCycle) {
448 dynPollingCycle = (int)pollingCycle;
449 } else if (dynPollingCycle > (int)pollingCycle * 32) {
450 dynPollingCycle = (int)pollingCycle * 32;
451 }
452 // DEBUG:
453 // if (dynPollingCycle != old) {
454 // fprintf(stderr, "[%dms]\t", dynPollingCycle);
455 // }
456 }
Constantin Kaplinskyd31fd312005-09-08 19:29:02 +0000457 desktop.poll();
458 }
459 } else {
460 // Something strange has happened -- gettimeofday(2) failed.
461 // Poll after each select(), as in the original VNC4 code.
462 desktop.poll();
463 }
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000464 }
465
466 } catch (rdr::Exception &e) {
467 vlog.error(e.str());
468 };
469
470 return 0;
471}