blob: a9ae62ebec9786f273eb6848391c98a995aed496 [file] [log] [blame]
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001/* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved.
Pierre Ossmanb53c3bf2018-03-22 16:01:44 +01002 * Copyright 2009-2018 Pierre Ossman for Cendio AB
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +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 */
19
20// -=- Single-Threaded VNC Server implementation
21
22
23// Note about how sockets get closed:
24//
25// Closing sockets to clients is non-trivial because the code which calls
26// VNCServerST must explicitly know about all the sockets (so that it can block
27// on them appropriately). However, VNCServerST may want to close clients for
28// a number of reasons, and from a variety of entry points. The simplest is
29// when processSocketEvent() is called for a client, and the remote end has
30// closed its socket. A more complex reason is when processSocketEvent() is
31// called for a client which has just sent a ClientInit with the shared flag
32// set to false - in this case we want to close all other clients. Yet another
33// reason for disconnecting clients is when the desktop size has changed as a
34// result of a call to setPixelBuffer().
35//
36// The responsibility for creating and deleting sockets is entirely with the
37// calling code. When VNCServerST wants to close a connection to a client it
38// calls the VNCSConnectionST's close() method which calls shutdown() on the
39// socket. Eventually the calling code will notice that the socket has been
40// shut down and call removeSocket() so that we can delete the
41// VNCSConnectionST. Note that the socket must not be deleted by the calling
42// code until after removeSocket() has been called.
43//
44// One minor complication is that we don't allocate a VNCSConnectionST object
45// for a blacklisted host (since we want to minimise the resources used for
46// dealing with such a connection). In order to properly implement the
47// getSockets function, we must maintain a separate closingSockets list,
48// otherwise blacklisted connections might be "forgotten".
49
50
Pierre Ossman559a2e82012-01-23 15:54:11 +000051#include <assert.h>
Pierre Ossmanf99c5712009-03-13 14:41:27 +000052#include <stdlib.h>
53
Pierre Ossman707fa122015-12-11 20:21:20 +010054#include <rfb/ComparingUpdateTracker.h>
55#include <rfb/KeyRemapper.h>
56#include <rfb/ListConnInfo.h>
57#include <rfb/Security.h>
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000058#include <rfb/ServerCore.h>
59#include <rfb/VNCServerST.h>
60#include <rfb/VNCSConnectionST.h>
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000061#include <rfb/util.h>
Pierre Ossmanbb305ca2016-12-11 12:41:26 +010062#include <rfb/ledStates.h>
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000063
64#include <rdr/types.h>
65
66using namespace rfb;
67
68static LogWriter slog("VNCServerST");
69LogWriter VNCServerST::connectionsLog("Connections");
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000070
71//
72// -=- VNCServerST Implementation
73//
74
75// -=- Constructors/Destructor
76
Adam Tkaca6578bf2010-04-23 14:07:41 +000077VNCServerST::VNCServerST(const char* name_, SDesktop* desktop_)
Pierre Ossman559a2e82012-01-23 15:54:11 +000078 : blHosts(&blacklist), desktop(desktop_), desktopStarted(false),
Pierre Ossmanbb305ca2016-12-11 12:41:26 +010079 blockCounter(0), pb(0), ledState(ledUnknown),
Adam Tkacd36b6262009-09-04 10:57:20 +000080 name(strDup(name_)), pointerClient(0), comparer(0),
Pierre Ossman6a1a0d02017-02-19 15:48:17 +010081 cursor(new Cursor(0, 0, Point(), NULL)),
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000082 renderedCursorInvalid(false),
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000083 queryConnectionHandler(0), keyRemapper(&KeyRemapper::defInstance),
Pierre Ossmanbbf955e2011-11-08 12:44:10 +000084 lastConnectionTime(0), disableclients(false),
Pierre Ossman6e49e952016-10-07 15:59:38 +020085 frameTimer(this)
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000086{
87 lastUserInputTime = lastDisconnectTime = time(0);
88 slog.debug("creating single-threaded server %s", name.buf);
89}
90
91VNCServerST::~VNCServerST()
92{
93 slog.debug("shutting down server %s", name.buf);
94
95 // Close any active clients, with appropriate logging & cleanup
96 closeClients("Server shutdown");
97
Pierre Ossman6e49e952016-10-07 15:59:38 +020098 // Stop trying to render things
99 stopFrameClock();
100
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000101 // Delete all the clients, and their sockets, and any closing sockets
102 // NB: Deleting a client implicitly removes it from the clients list
103 while (!clients.empty()) {
104 delete clients.front();
105 }
106
107 // Stop the desktop object if active, *only* after deleting all clients!
Pierre Ossmanb53c3bf2018-03-22 16:01:44 +0100108 stopDesktop();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000109
Pierre Ossman05338bc2016-11-08 14:57:11 +0100110 if (comparer)
111 comparer->logStats();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000112 delete comparer;
Pierre Ossman6a1a0d02017-02-19 15:48:17 +0100113
114 delete cursor;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000115}
116
117
118// SocketServer methods
119
120void VNCServerST::addSocket(network::Socket* sock, bool outgoing)
121{
122 // - Check the connection isn't black-marked
123 // *** do this in getSecurity instead?
124 CharArray address(sock->getPeerAddress());
125 if (blHosts->isBlackmarked(address.buf)) {
126 connectionsLog.error("blacklisted: %s", address.buf);
127 try {
128 SConnection::writeConnFailedFromScratch("Too many security failures",
129 &sock->outStream());
130 } catch (rdr::Exception&) {
131 }
132 sock->shutdown();
133 closingSockets.push_back(sock);
134 return;
135 }
136
137 if (clients.empty()) {
138 lastConnectionTime = time(0);
139 }
140
141 VNCSConnectionST* client = new VNCSConnectionST(this, sock, outgoing);
142 client->init();
143}
144
145void VNCServerST::removeSocket(network::Socket* sock) {
146 // - If the socket has resources allocated to it, delete them
147 std::list<VNCSConnectionST*>::iterator ci;
148 for (ci = clients.begin(); ci != clients.end(); ci++) {
149 if ((*ci)->getSock() == sock) {
150 // - Delete the per-Socket resources
151 delete *ci;
152
153 // - Check that the desktop object is still required
Pierre Ossmanb53c3bf2018-03-22 16:01:44 +0100154 if (authClientCount() == 0)
155 stopDesktop();
Pierre Ossman05338bc2016-11-08 14:57:11 +0100156
157 if (comparer)
158 comparer->logStats();
159
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000160 return;
161 }
162 }
163
164 // - If the Socket has no resources, it may have been a closingSocket
165 closingSockets.remove(sock);
166}
167
Pierre Ossmand408ca52016-04-29 14:26:05 +0200168void VNCServerST::processSocketReadEvent(network::Socket* sock)
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000169{
170 // - Find the appropriate VNCSConnectionST and process the event
171 std::list<VNCSConnectionST*>::iterator ci;
172 for (ci = clients.begin(); ci != clients.end(); ci++) {
173 if ((*ci)->getSock() == sock) {
174 (*ci)->processMessages();
175 return;
176 }
177 }
178 throw rdr::Exception("invalid Socket in VNCServerST");
179}
180
Pierre Ossmand408ca52016-04-29 14:26:05 +0200181void VNCServerST::processSocketWriteEvent(network::Socket* sock)
182{
183 // - Find the appropriate VNCSConnectionST and process the event
184 std::list<VNCSConnectionST*>::iterator ci;
185 for (ci = clients.begin(); ci != clients.end(); ci++) {
186 if ((*ci)->getSock() == sock) {
187 (*ci)->flushSocket();
188 return;
189 }
190 }
191 throw rdr::Exception("invalid Socket in VNCServerST");
192}
193
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000194int VNCServerST::checkTimeouts()
195{
196 int timeout = 0;
197 std::list<VNCSConnectionST*>::iterator ci, ci_next;
Pierre Ossman2d61deb2011-10-25 15:18:53 +0000198
199 soonestTimeout(&timeout, Timer::checkTimeouts());
200
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000201 for (ci=clients.begin();ci!=clients.end();ci=ci_next) {
202 ci_next = ci; ci_next++;
203 soonestTimeout(&timeout, (*ci)->checkIdleTimeout());
204 }
205
206 int timeLeft;
Constantin Kaplinsky8499d0c2008-08-21 05:51:29 +0000207 time_t now = time(0);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000208
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000209 // Check MaxDisconnectionTime
210 if (rfb::Server::maxDisconnectionTime && clients.empty()) {
211 if (now < lastDisconnectTime) {
212 // Someone must have set the time backwards.
213 slog.info("Time has gone backwards - resetting lastDisconnectTime");
214 lastDisconnectTime = now;
215 }
216 timeLeft = lastDisconnectTime + rfb::Server::maxDisconnectionTime - now;
217 if (timeLeft < -60) {
218 // Someone must have set the time forwards.
219 slog.info("Time has gone forwards - resetting lastDisconnectTime");
220 lastDisconnectTime = now;
221 timeLeft = rfb::Server::maxDisconnectionTime;
222 }
223 if (timeLeft <= 0) {
224 slog.info("MaxDisconnectionTime reached, exiting");
225 exit(0);
226 }
227 soonestTimeout(&timeout, timeLeft * 1000);
228 }
229
230 // Check MaxConnectionTime
231 if (rfb::Server::maxConnectionTime && lastConnectionTime && !clients.empty()) {
232 if (now < lastConnectionTime) {
233 // Someone must have set the time backwards.
234 slog.info("Time has gone backwards - resetting lastConnectionTime");
235 lastConnectionTime = now;
236 }
237 timeLeft = lastConnectionTime + rfb::Server::maxConnectionTime - now;
238 if (timeLeft < -60) {
239 // Someone must have set the time forwards.
240 slog.info("Time has gone forwards - resetting lastConnectionTime");
241 lastConnectionTime = now;
242 timeLeft = rfb::Server::maxConnectionTime;
243 }
244 if (timeLeft <= 0) {
245 slog.info("MaxConnectionTime reached, exiting");
246 exit(0);
247 }
248 soonestTimeout(&timeout, timeLeft * 1000);
249 }
250
251
252 // Check MaxIdleTime
253 if (rfb::Server::maxIdleTime) {
254 if (now < lastUserInputTime) {
255 // Someone must have set the time backwards.
256 slog.info("Time has gone backwards - resetting lastUserInputTime");
257 lastUserInputTime = now;
258 }
259 timeLeft = lastUserInputTime + rfb::Server::maxIdleTime - now;
260 if (timeLeft < -60) {
261 // Someone must have set the time forwards.
262 slog.info("Time has gone forwards - resetting lastUserInputTime");
263 lastUserInputTime = now;
264 timeLeft = rfb::Server::maxIdleTime;
265 }
266 if (timeLeft <= 0) {
267 slog.info("MaxIdleTime reached, exiting");
268 exit(0);
269 }
270 soonestTimeout(&timeout, timeLeft * 1000);
271 }
272
273 return timeout;
274}
275
276
277// VNCServer methods
278
Pierre Ossman559a2e82012-01-23 15:54:11 +0000279void VNCServerST::blockUpdates()
280{
281 blockCounter++;
Pierre Ossman6e49e952016-10-07 15:59:38 +0200282
283 stopFrameClock();
Pierre Ossman559a2e82012-01-23 15:54:11 +0000284}
285
286void VNCServerST::unblockUpdates()
287{
288 assert(blockCounter > 0);
289
290 blockCounter--;
291
Pierre Ossman6e49e952016-10-07 15:59:38 +0200292 // Restart the frame clock if we have updates
293 if (blockCounter == 0) {
294 if (!comparer->is_empty())
295 startFrameClock();
296 }
Pierre Ossman559a2e82012-01-23 15:54:11 +0000297}
298
Pierre Ossman04e62db2009-03-23 16:57:07 +0000299void VNCServerST::setPixelBuffer(PixelBuffer* pb_, const ScreenSet& layout)
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000300{
Pierre Ossman05338bc2016-11-08 14:57:11 +0100301 if (comparer)
302 comparer->logStats();
303
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000304 pb = pb_;
305 delete comparer;
306 comparer = 0;
307
Pierre Ossman04e62db2009-03-23 16:57:07 +0000308 if (!pb) {
Michal Srb28d570d2017-09-29 14:45:33 +0200309 screenLayout = ScreenSet();
310
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000311 if (desktopStarted)
312 throw Exception("setPixelBuffer: null PixelBuffer when desktopStarted?");
Michal Srb28d570d2017-09-29 14:45:33 +0200313
Pierre Ossman04e62db2009-03-23 16:57:07 +0000314 return;
315 }
316
Pierre Ossmanc3826bb2018-11-01 16:10:19 +0100317 if (!layout.validate(pb->width(), pb->height()))
318 throw Exception("setPixelBuffer: invalid screen layout");
319
320 screenLayout = layout;
321
Pierre Ossman6cd61172018-05-07 14:24:56 +0200322 // Assume the framebuffer contents wasn't saved and reset everything
323 // that tracks its contents
Pierre Ossman04e62db2009-03-23 16:57:07 +0000324 comparer = new ComparingUpdateTracker(pb);
Pierre Ossman6ea6e1a2014-02-12 16:33:43 +0100325 renderedCursorInvalid = true;
Pierre Ossman6cd61172018-05-07 14:24:56 +0200326 add_changed(pb->getRect());
Pierre Ossman04e62db2009-03-23 16:57:07 +0000327
Pierre Ossman04e62db2009-03-23 16:57:07 +0000328 std::list<VNCSConnectionST*>::iterator ci, ci_next;
329 for (ci=clients.begin();ci!=clients.end();ci=ci_next) {
330 ci_next = ci; ci_next++;
331 (*ci)->pixelBufferChange();
332 // Since the new pixel buffer means an ExtendedDesktopSize needs to
333 // be sent anyway, we don't need to call screenLayoutChange.
334 }
335}
336
337void VNCServerST::setPixelBuffer(PixelBuffer* pb_)
338{
Michal Srb28d570d2017-09-29 14:45:33 +0200339 ScreenSet layout = screenLayout;
Pierre Ossman04e62db2009-03-23 16:57:07 +0000340
341 // Check that the screen layout is still valid
Michal Srb28d570d2017-09-29 14:45:33 +0200342 if (pb_ && !layout.validate(pb_->width(), pb_->height())) {
Pierre Ossman04e62db2009-03-23 16:57:07 +0000343 Rect fbRect;
344 ScreenSet::iterator iter, iter_next;
345
346 fbRect.setXYWH(0, 0, pb_->width(), pb_->height());
347
348 for (iter = layout.begin();iter != layout.end();iter = iter_next) {
349 iter_next = iter; ++iter_next;
350 if (iter->dimensions.enclosed_by(fbRect))
351 continue;
352 iter->dimensions = iter->dimensions.intersect(fbRect);
353 if (iter->dimensions.is_empty()) {
354 slog.info("Removing screen %d (%x) as it is completely outside the new framebuffer",
355 (int)iter->id, (unsigned)iter->id);
356 layout.remove_screen(iter->id);
357 }
358 }
359 }
360
Pierre Ossmanc3826bb2018-11-01 16:10:19 +0100361 // Make sure that we have at least one screen
362 if (layout.num_screens() == 0)
363 layout.add_screen(Screen(0, 0, 0, pb->width(), pb->height(), 0));
364
Pierre Ossman04e62db2009-03-23 16:57:07 +0000365 setPixelBuffer(pb_, layout);
366}
367
368void VNCServerST::setScreenLayout(const ScreenSet& layout)
369{
370 if (!pb)
371 throw Exception("setScreenLayout: new screen layout without a PixelBuffer");
372 if (!layout.validate(pb->width(), pb->height()))
373 throw Exception("setScreenLayout: invalid screen layout");
374
Pierre Ossmandf453202009-04-02 14:26:45 +0000375 screenLayout = layout;
376
Pierre Ossman04e62db2009-03-23 16:57:07 +0000377 std::list<VNCSConnectionST*>::iterator ci, ci_next;
378 for (ci=clients.begin();ci!=clients.end();ci=ci_next) {
379 ci_next = ci; ci_next++;
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000380 (*ci)->screenLayoutChangeOrClose(reasonServer);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000381 }
382}
383
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000384void VNCServerST::bell()
385{
386 std::list<VNCSConnectionST*>::iterator ci, ci_next;
387 for (ci = clients.begin(); ci != clients.end(); ci = ci_next) {
388 ci_next = ci; ci_next++;
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000389 (*ci)->bellOrClose();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000390 }
391}
392
393void VNCServerST::serverCutText(const char* str, int len)
394{
395 std::list<VNCSConnectionST*>::iterator ci, ci_next;
396 for (ci = clients.begin(); ci != clients.end(); ci = ci_next) {
397 ci_next = ci; ci_next++;
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000398 (*ci)->serverCutTextOrClose(str, len);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000399 }
400}
401
Peter Ã…strandc39e0782009-01-15 12:21:42 +0000402void VNCServerST::setName(const char* name_)
403{
Adam Tkacd36b6262009-09-04 10:57:20 +0000404 name.replaceBuf(strDup(name_));
Peter Ã…strandc39e0782009-01-15 12:21:42 +0000405 std::list<VNCSConnectionST*>::iterator ci, ci_next;
406 for (ci = clients.begin(); ci != clients.end(); ci = ci_next) {
407 ci_next = ci; ci_next++;
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000408 (*ci)->setDesktopNameOrClose(name_);
Peter Ã…strandc39e0782009-01-15 12:21:42 +0000409 }
410}
411
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000412void VNCServerST::add_changed(const Region& region)
413{
Pierre Ossmanbbf955e2011-11-08 12:44:10 +0000414 if (comparer == NULL)
415 return;
416
417 comparer->add_changed(region);
Pierre Ossman6e49e952016-10-07 15:59:38 +0200418 startFrameClock();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000419}
420
421void VNCServerST::add_copied(const Region& dest, const Point& delta)
422{
Pierre Ossmanbbf955e2011-11-08 12:44:10 +0000423 if (comparer == NULL)
424 return;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000425
Pierre Ossmanbbf955e2011-11-08 12:44:10 +0000426 comparer->add_copied(dest, delta);
Pierre Ossman6e49e952016-10-07 15:59:38 +0200427 startFrameClock();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000428}
429
430void VNCServerST::setCursor(int width, int height, const Point& newHotspot,
Pierre Ossman6a1a0d02017-02-19 15:48:17 +0100431 const rdr::U8* data)
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000432{
Pierre Ossman6a1a0d02017-02-19 15:48:17 +0100433 delete cursor;
434 cursor = new Cursor(width, height, newHotspot, data);
435 cursor->crop();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000436
437 renderedCursorInvalid = true;
438
439 std::list<VNCSConnectionST*>::iterator ci, ci_next;
440 for (ci = clients.begin(); ci != clients.end(); ci = ci_next) {
441 ci_next = ci; ci_next++;
442 (*ci)->renderedCursorChange();
443 (*ci)->setCursorOrClose();
444 }
445}
446
447void VNCServerST::setCursorPos(const Point& pos)
448{
449 if (!cursorPos.equals(pos)) {
450 cursorPos = pos;
451 renderedCursorInvalid = true;
452 std::list<VNCSConnectionST*>::iterator ci;
453 for (ci = clients.begin(); ci != clients.end(); ci++)
454 (*ci)->renderedCursorChange();
455 }
456}
457
Pierre Ossmanbb305ca2016-12-11 12:41:26 +0100458void VNCServerST::setLEDState(unsigned int state)
459{
Pierre Ossmanb45a84f2016-12-12 16:59:15 +0100460 std::list<VNCSConnectionST*>::iterator ci, ci_next;
461
462 if (state == ledState)
463 return;
464
Pierre Ossmanbb305ca2016-12-11 12:41:26 +0100465 ledState = state;
Pierre Ossmanb45a84f2016-12-12 16:59:15 +0100466
467 for (ci = clients.begin(); ci != clients.end(); ci = ci_next) {
468 ci_next = ci; ci_next++;
469 (*ci)->setLEDStateOrClose(state);
470 }
Pierre Ossmanbb305ca2016-12-11 12:41:26 +0100471}
472
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000473// Other public methods
474
475void VNCServerST::approveConnection(network::Socket* sock, bool accept,
476 const char* reason)
477{
478 std::list<VNCSConnectionST*>::iterator ci;
479 for (ci = clients.begin(); ci != clients.end(); ci++) {
480 if ((*ci)->getSock() == sock) {
481 (*ci)->approveConnectionOrClose(accept, reason);
482 return;
483 }
484 }
485}
486
487void VNCServerST::closeClients(const char* reason, network::Socket* except)
488{
489 std::list<VNCSConnectionST*>::iterator i, next_i;
490 for (i=clients.begin(); i!=clients.end(); i=next_i) {
491 next_i = i; next_i++;
492 if ((*i)->getSock() != except)
493 (*i)->close(reason);
494 }
495}
496
497void VNCServerST::getSockets(std::list<network::Socket*>* sockets)
498{
499 sockets->clear();
500 std::list<VNCSConnectionST*>::iterator ci;
501 for (ci = clients.begin(); ci != clients.end(); ci++) {
502 sockets->push_back((*ci)->getSock());
503 }
504 std::list<network::Socket*>::iterator si;
505 for (si = closingSockets.begin(); si != closingSockets.end(); si++) {
506 sockets->push_back(*si);
507 }
508}
509
510SConnection* VNCServerST::getSConnection(network::Socket* sock) {
511 std::list<VNCSConnectionST*>::iterator ci;
512 for (ci = clients.begin(); ci != clients.end(); ci++) {
513 if ((*ci)->getSock() == sock)
514 return *ci;
515 }
516 return 0;
517}
518
Pierre Ossmanbbf955e2011-11-08 12:44:10 +0000519bool VNCServerST::handleTimeout(Timer* t)
520{
Pierre Ossman6e49e952016-10-07 15:59:38 +0200521 if (t == &frameTimer) {
522 // We keep running until we go a full interval without any updates
523 if (comparer->is_empty())
524 return false;
Pierre Ossmanbbf955e2011-11-08 12:44:10 +0000525
Pierre Ossman6e49e952016-10-07 15:59:38 +0200526 writeUpdate();
Pierre Ossman7be73d72017-11-06 13:16:35 +0100527
528 // If this is the first iteration then we need to adjust the timeout
529 if (frameTimer.getTimeoutMs() != 1000/rfb::Server::frameRate) {
530 frameTimer.start(1000/rfb::Server::frameRate);
531 return false;
532 }
533
Pierre Ossman6e49e952016-10-07 15:59:38 +0200534 return true;
535 }
Pierre Ossmanbbf955e2011-11-08 12:44:10 +0000536
537 return false;
538}
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000539
540// -=- Internal methods
541
542void VNCServerST::startDesktop()
543{
544 if (!desktopStarted) {
545 slog.debug("starting desktop");
546 desktop->start(this);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000547 if (!pb)
548 throw Exception("SDesktop::start() did not set a valid PixelBuffer");
Pierre Ossman6cd61172018-05-07 14:24:56 +0200549 desktopStarted = true;
550 // The tracker might have accumulated changes whilst we were
551 // stopped, so flush those out
552 if (!comparer->is_empty())
553 writeUpdate();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000554 }
555}
556
Pierre Ossmanb53c3bf2018-03-22 16:01:44 +0100557void VNCServerST::stopDesktop()
558{
559 if (desktopStarted) {
560 slog.debug("stopping desktop");
561 desktopStarted = false;
562 desktop->stop();
563 stopFrameClock();
564 }
565}
566
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000567int VNCServerST::authClientCount() {
568 int count = 0;
569 std::list<VNCSConnectionST*>::iterator ci;
570 for (ci = clients.begin(); ci != clients.end(); ci++) {
571 if ((*ci)->authenticated())
572 count++;
573 }
574 return count;
575}
576
577inline bool VNCServerST::needRenderedCursor()
578{
579 std::list<VNCSConnectionST*>::iterator ci;
580 for (ci = clients.begin(); ci != clients.end(); ci++)
581 if ((*ci)->needRenderedCursor()) return true;
582 return false;
583}
584
Pierre Ossman6e49e952016-10-07 15:59:38 +0200585void VNCServerST::startFrameClock()
Pierre Ossmanbbf955e2011-11-08 12:44:10 +0000586{
Pierre Ossman6e49e952016-10-07 15:59:38 +0200587 if (frameTimer.isStarted())
Pierre Ossmanbbf955e2011-11-08 12:44:10 +0000588 return;
Pierre Ossman559a2e82012-01-23 15:54:11 +0000589 if (blockCounter > 0)
590 return;
Pierre Ossmanb53c3bf2018-03-22 16:01:44 +0100591 if (!desktopStarted)
592 return;
Pierre Ossman559a2e82012-01-23 15:54:11 +0000593
Pierre Ossman7be73d72017-11-06 13:16:35 +0100594 // The first iteration will be just half a frame as we get a very
595 // unstable update rate if we happen to be perfectly in sync with
596 // the application's update rate
597 frameTimer.start(1000/rfb::Server::frameRate/2);
Pierre Ossmanbbf955e2011-11-08 12:44:10 +0000598}
599
Pierre Ossman6e49e952016-10-07 15:59:38 +0200600void VNCServerST::stopFrameClock()
601{
602 frameTimer.stop();
603}
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000604
Pierre Ossmana2b80d62018-03-23 09:30:09 +0100605int VNCServerST::msToNextUpdate()
606{
607 // FIXME: If the application is updating slower than frameRate then
608 // we could allow the clients more time here
609
610 if (!frameTimer.isStarted())
611 return 1000/rfb::Server::frameRate/2;
612 else
613 return frameTimer.getRemainingMs();
614}
615
Pierre Ossman6e49e952016-10-07 15:59:38 +0200616// writeUpdate() is called on a regular interval in order to see what
617// updates are pending and propagates them to the update tracker for
618// each client. It uses the ComparingUpdateTracker's compare() method
619// to filter out areas of the screen which haven't actually changed. It
620// also checks the state of the (server-side) rendered cursor, if
621// necessary rendering it again with the correct background.
622
623void VNCServerST::writeUpdate()
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000624{
Constantin Kaplinsky604d7812007-08-31 15:50:37 +0000625 UpdateInfo ui;
Pierre Ossman6e49e952016-10-07 15:59:38 +0200626 Region toCheck;
627
628 std::list<VNCSConnectionST*>::iterator ci, ci_next;
629
630 assert(blockCounter == 0);
Pierre Ossmanb53c3bf2018-03-22 16:01:44 +0100631 assert(desktopStarted);
Pierre Ossman6e49e952016-10-07 15:59:38 +0200632
Constantin Kaplinsky604d7812007-08-31 15:50:37 +0000633 comparer->getUpdateInfo(&ui, pb->getRect());
Pierre Ossman6e49e952016-10-07 15:59:38 +0200634 toCheck = ui.changed.union_(ui.copied);
Constantin Kaplinsky604d7812007-08-31 15:50:37 +0000635
Pierre Ossman6e49e952016-10-07 15:59:38 +0200636 if (needRenderedCursor()) {
Pierre Ossman6a1a0d02017-02-19 15:48:17 +0100637 Rect clippedCursorRect = Rect(0, 0, cursor->width(), cursor->height())
638 .translate(cursorPos.subtract(cursor->hotspot()))
639 .intersect(pb->getRect());
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000640
Pierre Ossman24684e52016-12-05 16:58:19 +0100641 if (!toCheck.intersect(clippedCursorRect).is_empty())
642 renderedCursorInvalid = true;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000643 }
644
645 pb->grabRegion(toCheck);
646
Pierre Ossmanb114cec2011-11-20 15:36:11 +0000647 if (getComparerState())
648 comparer->enable();
649 else
650 comparer->disable();
651
652 if (comparer->compare())
Constantin Kaplinskyf0b3be72008-08-21 05:22:04 +0000653 comparer->getUpdateInfo(&ui, pb->getRect());
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000654
Pierre Ossman6e49e952016-10-07 15:59:38 +0200655 comparer->clear();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000656
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000657 for (ci = clients.begin(); ci != clients.end(); ci = ci_next) {
658 ci_next = ci; ci_next++;
Constantin Kaplinsky604d7812007-08-31 15:50:37 +0000659 (*ci)->add_copied(ui.copied, ui.copy_delta);
660 (*ci)->add_changed(ui.changed);
Pierre Ossman6e49e952016-10-07 15:59:38 +0200661 (*ci)->writeFramebufferUpdateOrClose();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000662 }
Pierre Ossman6e49e952016-10-07 15:59:38 +0200663}
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000664
Pierre Ossman6e49e952016-10-07 15:59:38 +0200665// checkUpdate() is called by clients to see if it is safe to read from
666// the framebuffer at this time.
667
Pierre Ossman8efc7b42018-03-23 11:45:51 +0100668Region VNCServerST::getPendingRegion()
Pierre Ossman6e49e952016-10-07 15:59:38 +0200669{
Pierre Ossman8efc7b42018-03-23 11:45:51 +0100670 UpdateInfo ui;
671
Pierre Ossman6e49e952016-10-07 15:59:38 +0200672 // Block clients as the frame buffer cannot be safely accessed
673 if (blockCounter > 0)
Pierre Ossman8efc7b42018-03-23 11:45:51 +0100674 return pb->getRect();
Pierre Ossman6e49e952016-10-07 15:59:38 +0200675
676 // Block client from updating if there are pending updates
Pierre Ossman8efc7b42018-03-23 11:45:51 +0100677 if (comparer->is_empty())
678 return Region();
Pierre Ossmanbbf955e2011-11-08 12:44:10 +0000679
Pierre Ossman8efc7b42018-03-23 11:45:51 +0100680 comparer->getUpdateInfo(&ui, pb->getRect());
681
682 return ui.changed.union_(ui.copied);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000683}
684
Pierre Ossman24684e52016-12-05 16:58:19 +0100685const RenderedCursor* VNCServerST::getRenderedCursor()
686{
687 if (renderedCursorInvalid) {
Pierre Ossman7cb4f312017-02-24 13:25:00 +0100688 renderedCursor.update(pb, cursor, cursorPos);
Pierre Ossman24684e52016-12-05 16:58:19 +0100689 renderedCursorInvalid = false;
690 }
691
692 return &renderedCursor;
693}
694
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000695void VNCServerST::getConnInfo(ListConnInfo * listConn)
696{
697 listConn->Clear();
698 listConn->setDisable(getDisable());
699 if (clients.empty())
700 return;
701 std::list<VNCSConnectionST*>::iterator i;
702 for (i = clients.begin(); i != clients.end(); i++)
703 listConn->addInfo((void*)(*i), (*i)->getSock()->getPeerAddress(),
704 (*i)->getStartTime(), (*i)->getStatus());
705}
706
707void VNCServerST::setConnStatus(ListConnInfo* listConn)
708{
709 setDisable(listConn->getDisable());
710 if (listConn->Empty() || clients.empty()) return;
711 for (listConn->iBegin(); !listConn->iEnd(); listConn->iNext()) {
712 VNCSConnectionST* conn = (VNCSConnectionST*)listConn->iGetConn();
713 std::list<VNCSConnectionST*>::iterator i;
714 for (i = clients.begin(); i != clients.end(); i++) {
715 if ((*i) == conn) {
716 int status = listConn->iGetStatus();
717 if (status == 3) {
718 (*i)->close(0);
719 } else {
720 (*i)->setStatus(status);
721 }
722 break;
723 }
724 }
725 }
726}
Constantin Kaplinsky9d1fc6c2008-06-14 05:23:10 +0000727
Pierre Ossman04e62db2009-03-23 16:57:07 +0000728void VNCServerST::notifyScreenLayoutChange(VNCSConnectionST* requester)
729{
730 std::list<VNCSConnectionST*>::iterator ci, ci_next;
731 for (ci=clients.begin();ci!=clients.end();ci=ci_next) {
732 ci_next = ci; ci_next++;
733 if ((*ci) == requester)
734 continue;
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000735 (*ci)->screenLayoutChangeOrClose(reasonOtherClient);
Pierre Ossman04e62db2009-03-23 16:57:07 +0000736 }
737}
Pierre Ossmanb114cec2011-11-20 15:36:11 +0000738
739bool VNCServerST::getComparerState()
740{
741 if (rfb::Server::compareFB == 0)
742 return false;
743 if (rfb::Server::compareFB != 2)
744 return true;
745
746 std::list<VNCSConnectionST*>::iterator ci, ci_next;
747 for (ci=clients.begin();ci!=clients.end();ci=ci_next) {
748 ci_next = ci; ci_next++;
749 if ((*ci)->getComparerState())
750 return true;
751 }
752 return false;
753}