blob: cd5d0896fc6d71dfafc3d5049d2d85d2116d1a92 [file] [log] [blame]
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001/* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved.
Pierre Ossmancef3cf72016-11-25 10:06:34 +01002 * Copyright 2009-2016 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
Pierre Ossman1b478e52011-11-15 12:08:30 +000020// Debug output on what the congestion control is up to
21#undef CONGESTION_DEBUG
22
23#include <sys/time.h>
24
25#ifdef CONGESTION_DEBUG
26#include <sys/socket.h>
27#include <netinet/in.h>
28#include <netinet/tcp.h>
29#endif
30
Pierre Ossmana830bec2011-11-08 12:12:02 +000031#include <network/TcpSocket.h>
Pierre Ossman707fa122015-12-11 20:21:20 +010032
33#include <rfb/ComparingUpdateTracker.h>
34#include <rfb/Encoder.h>
35#include <rfb/KeyRemapper.h>
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000036#include <rfb/LogWriter.h>
Adam Tkac5a0caed2010-04-23 13:58:10 +000037#include <rfb/Security.h>
Pierre Ossman707fa122015-12-11 20:21:20 +010038#include <rfb/ServerCore.h>
39#include <rfb/SMsgWriter.h>
40#include <rfb/VNCServerST.h>
41#include <rfb/VNCSConnectionST.h>
Pierre Ossmanc5e25602009-03-20 12:59:05 +000042#include <rfb/screenTypes.h>
Pierre Ossman2c764942011-11-14 15:54:30 +000043#include <rfb/fenceTypes.h>
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000044#define XK_MISCELLANY
45#define XK_XKB_KEYS
46#include <rfb/keysymdef.h>
47
48using namespace rfb;
49
50static LogWriter vlog("VNCSConnST");
51
Pierre Ossman1b478e52011-11-15 12:08:30 +000052// This window should get us going fairly fast on a decent bandwidth network.
53// If it's too high, it will rapidly be reduced and stay low.
54static const unsigned INITIAL_WINDOW = 16384;
55
56// TCP's minimal window is 3*MSS. But since we don't know the MSS, we
klemens0536d092017-01-28 20:56:56 +010057// make a guess at 4 KiB (it's probably a bit higher).
Pierre Ossman1b478e52011-11-15 12:08:30 +000058static const unsigned MINIMUM_WINDOW = 4096;
59
60// The current default maximum window for Linux (4 MiB). Should be a good
61// limit for now...
62static const unsigned MAXIMUM_WINDOW = 4194304;
63
64struct RTTInfo {
65 struct timeval tv;
66 int offset;
67 unsigned inFlight;
68};
69
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000070VNCSConnectionST::VNCSConnectionST(VNCServerST* server_, network::Socket *s,
71 bool reverse)
Pierre Ossman7069bdd2015-02-06 14:41:58 +010072 : sock(s), reverseConnection(reverse),
Pierre Ossmanf8e3b342015-01-26 14:37:04 +010073 queryConnectTimer(this), inProcessMessages(false),
Pierre Ossmanb8b1e962012-07-20 10:47:00 +000074 pendingSyncFence(false), syncFence(false), fenceFlags(0),
75 fenceDataLen(0), fenceData(NULL),
Pierre Ossmanb1cd6ca2015-03-03 16:37:43 +010076 baseRTT(-1), congWindow(0), ackedOffset(0), sentOffset(0),
77 minRTT(-1), seenCongestion(false),
Pierre Ossmanb2a417c2015-12-11 19:32:21 +010078 congestionTimer(this),
Pierre Ossman0c9bd4b2014-07-09 16:44:11 +020079 server(server_), updates(false),
Pierre Ossman671d2ef2015-06-09 16:09:06 +020080 updateRenderedCursor(false), removeRenderedCursor(false),
Pierre Ossmana40ab202016-04-29 15:35:56 +020081 continuousUpdates(false), encodeManager(this), pointerEventTime(0),
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +000082 accessRights(AccessDefault), startTime(time(0))
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000083{
84 setStreams(&sock->inStream(), &sock->outStream());
85 peerEndpoint.buf = sock->getPeerEndpoint();
86 VNCServerST::connectionsLog.write(1,"accepted: %s", peerEndpoint.buf);
87
88 // Configure the socket
89 setSocketTimeouts();
90 lastEventTime = time(0);
91
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000092 server->clients.push_front(this);
93}
94
95
96VNCSConnectionST::~VNCSConnectionST()
97{
98 // If we reach here then VNCServerST is deleting us!
99 VNCServerST::connectionsLog.write(1,"closed: %s (%s)",
100 peerEndpoint.buf,
101 (closeReason.buf) ? closeReason.buf : "");
102
103 // Release any keys the client still had pressed
104 std::set<rdr::U32>::iterator i;
Pierre Ossman9a153b02015-08-31 10:01:14 +0200105 for (i=pressedKeys.begin(); i!=pressedKeys.end(); i++) {
106 vlog.debug("Releasing key 0x%x on client disconnect", *i);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000107 server->desktop->keyEvent(*i, false);
Pierre Ossman9a153b02015-08-31 10:01:14 +0200108 }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000109 if (server->pointerClient == this)
110 server->pointerClient = 0;
111
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000112 // Remove this client from the server
113 server->clients.remove(this);
114
Pierre Ossman2c764942011-11-14 15:54:30 +0000115 delete [] fenceData;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000116}
117
118
119// Methods called from VNCServerST
120
121bool VNCSConnectionST::init()
122{
123 try {
124 initialiseProtocol();
125 } catch (rdr::Exception& e) {
126 close(e.str());
127 return false;
128 }
129 return true;
130}
131
132void VNCSConnectionST::close(const char* reason)
133{
134 // Log the reason for the close
135 if (!closeReason.buf)
Adam Tkacd36b6262009-09-04 10:57:20 +0000136 closeReason.buf = strDup(reason);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000137 else
138 vlog.debug("second close: %s (%s)", peerEndpoint.buf, reason);
139
140 if (authenticated()) {
141 server->lastDisconnectTime = time(0);
142 }
143
144 // Just shutdown the socket and mark our state as closing. Eventually the
145 // calling code will call VNCServerST's removeSocket() method causing us to
146 // be deleted.
147 sock->shutdown();
148 setState(RFBSTATE_CLOSING);
149}
150
151
152void VNCSConnectionST::processMessages()
153{
154 if (state() == RFBSTATE_CLOSING) return;
155 try {
156 // - Now set appropriate socket timeouts and process data
157 setSocketTimeouts();
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000158
159 inProcessMessages = true;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000160
Pierre Ossmana830bec2011-11-08 12:12:02 +0000161 // Get the underlying TCP layer to build large packets if we send
162 // multiple small responses.
163 network::TcpSocket::cork(sock->getFd(), true);
164
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000165 while (getInStream()->checkNoWait(1)) {
Pierre Ossmanb8b1e962012-07-20 10:47:00 +0000166 if (pendingSyncFence) {
167 syncFence = true;
168 pendingSyncFence = false;
169 }
170
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000171 processMsg();
Pierre Ossmanb8b1e962012-07-20 10:47:00 +0000172
Pierre Ossman2c764942011-11-14 15:54:30 +0000173 if (syncFence) {
174 writer()->writeFence(fenceFlags, fenceDataLen, fenceData);
175 syncFence = false;
176 }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000177 }
178
Pierre Ossmana830bec2011-11-08 12:12:02 +0000179 // Flush out everything in case we go idle after this.
180 network::TcpSocket::cork(sock->getFd(), false);
181
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000182 inProcessMessages = false;
Constantin Kaplinsky2ef66952008-08-29 11:33:46 +0000183
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000184 // If there were anything requiring an update, try to send it here.
185 // We wait until now with this to aggregate responses and to give
186 // higher priority to user actions such as keyboard and pointer events.
187 writeFramebufferUpdate();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000188 } catch (rdr::EndOfStream&) {
189 close("Clean disconnection");
190 } catch (rdr::Exception &e) {
191 close(e.str());
192 }
193}
194
Pierre Ossmand408ca52016-04-29 14:26:05 +0200195void VNCSConnectionST::flushSocket()
196{
197 if (state() == RFBSTATE_CLOSING) return;
198 try {
199 setSocketTimeouts();
200 sock->outStream().flush();
Pierre Ossmana40ab202016-04-29 15:35:56 +0200201 // Flushing the socket might release an update that was previously
202 // delayed because of congestion.
203 if (sock->outStream().bufferUsage() == 0)
204 writeFramebufferUpdate();
Pierre Ossmand408ca52016-04-29 14:26:05 +0200205 } catch (rdr::Exception &e) {
206 close(e.str());
207 }
208}
209
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000210void VNCSConnectionST::pixelBufferChange()
211{
212 try {
213 if (!authenticated()) return;
214 if (cp.width && cp.height && (server->pb->width() != cp.width ||
215 server->pb->height() != cp.height))
216 {
217 // We need to clip the next update to the new size, but also add any
218 // extra bits if it's bigger. If we wanted to do this exactly, something
219 // like the code below would do it, but at the moment we just update the
Pierre Ossman671d2ef2015-06-09 16:09:06 +0200220 // entire new size. However, we do need to clip the damagedCursorRegion
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000221 // because that might be added to updates in writeFramebufferUpdate().
222
223 //updates.intersect(server->pb->getRect());
224 //
225 //if (server->pb->width() > cp.width)
226 // updates.add_changed(Rect(cp.width, 0, server->pb->width(),
227 // server->pb->height()));
228 //if (server->pb->height() > cp.height)
229 // updates.add_changed(Rect(0, cp.height, cp.width,
230 // server->pb->height()));
231
Pierre Ossman671d2ef2015-06-09 16:09:06 +0200232 damagedCursorRegion.assign_intersect(server->pb->getRect());
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000233
234 cp.width = server->pb->width();
235 cp.height = server->pb->height();
Pierre Ossman34e62f32009-03-20 21:46:12 +0000236 cp.screenLayout = server->screenLayout;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000237 if (state() == RFBSTATE_NORMAL) {
Pierre Ossman2ee430a2009-05-28 12:54:24 +0000238 // We should only send EDS to client asking for both
239 if (!writer()->writeExtendedDesktopSize()) {
240 if (!writer()->writeSetDesktopSize()) {
241 close("Client does not support desktop resize");
242 return;
243 }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000244 }
245 }
246 }
247 // Just update the whole screen at the moment because we're too lazy to
248 // work out what's actually changed.
249 updates.clear();
250 updates.add_changed(server->pb->getRect());
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000251 writeFramebufferUpdate();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000252 } catch(rdr::Exception &e) {
253 close(e.str());
254 }
255}
256
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000257void VNCSConnectionST::writeFramebufferUpdateOrClose()
Pierre Ossman04e62db2009-03-23 16:57:07 +0000258{
259 try {
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000260 writeFramebufferUpdate();
261 } catch(rdr::Exception &e) {
262 close(e.str());
263 }
264}
Pierre Ossman04e62db2009-03-23 16:57:07 +0000265
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000266void VNCSConnectionST::screenLayoutChangeOrClose(rdr::U16 reason)
267{
268 try {
269 screenLayoutChange(reason);
Pierre Ossman04e62db2009-03-23 16:57:07 +0000270 } catch(rdr::Exception &e) {
271 close(e.str());
272 }
273}
274
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000275void VNCSConnectionST::bellOrClose()
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000276{
277 try {
278 if (state() == RFBSTATE_NORMAL) writer()->writeBell();
279 } catch(rdr::Exception& e) {
280 close(e.str());
281 }
282}
283
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000284void VNCSConnectionST::serverCutTextOrClose(const char *str, int len)
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000285{
286 try {
287 if (!(accessRights & AccessCutText)) return;
288 if (!rfb::Server::sendCutText) return;
289 if (state() == RFBSTATE_NORMAL)
290 writer()->writeServerCutText(str, len);
291 } catch(rdr::Exception& e) {
292 close(e.str());
293 }
294}
295
Peter Ã…strandc39e0782009-01-15 12:21:42 +0000296
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000297void VNCSConnectionST::setDesktopNameOrClose(const char *name)
Peter Ã…strandc39e0782009-01-15 12:21:42 +0000298{
Peter Ã…strandc39e0782009-01-15 12:21:42 +0000299 try {
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000300 setDesktopName(name);
Peter Ã…strandc39e0782009-01-15 12:21:42 +0000301 } catch(rdr::Exception& e) {
302 close(e.str());
303 }
304}
305
306
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000307void VNCSConnectionST::setCursorOrClose()
308{
309 try {
310 setCursor();
311 } catch(rdr::Exception& e) {
312 close(e.str());
313 }
314}
315
316
317int VNCSConnectionST::checkIdleTimeout()
318{
319 int idleTimeout = rfb::Server::idleTimeout;
320 if (idleTimeout == 0) return 0;
321 if (state() != RFBSTATE_NORMAL && idleTimeout < 15)
322 idleTimeout = 15; // minimum of 15 seconds while authenticating
323 time_t now = time(0);
324 if (now < lastEventTime) {
325 // Someone must have set the time backwards. Set lastEventTime so that the
326 // idleTimeout will count from now.
327 vlog.info("Time has gone backwards - resetting idle timeout");
328 lastEventTime = now;
329 }
330 int timeLeft = lastEventTime + idleTimeout - now;
331 if (timeLeft < -60) {
332 // Our callback is over a minute late - someone must have set the time
333 // forwards. Set lastEventTime so that the idleTimeout will count from
334 // now.
335 vlog.info("Time has gone forwards - resetting idle timeout");
336 lastEventTime = now;
337 return secsToMillis(idleTimeout);
338 }
339 if (timeLeft <= 0) {
340 close("Idle timeout");
341 return 0;
342 }
343 return secsToMillis(timeLeft);
344}
345
Pierre Ossmanb114cec2011-11-20 15:36:11 +0000346
347bool VNCSConnectionST::getComparerState()
348{
349 // We interpret a low compression level as an indication that the client
350 // wants to prioritise CPU usage over bandwidth, and hence disable the
351 // comparing update tracker.
352 return (cp.compressLevel == -1) || (cp.compressLevel > 1);
353}
354
355
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000356// renderedCursorChange() is called whenever the server-side rendered cursor
357// changes shape or position. It ensures that the next update will clean up
358// the old rendered cursor and if necessary draw the new rendered cursor.
359
360void VNCSConnectionST::renderedCursorChange()
361{
362 if (state() != RFBSTATE_NORMAL) return;
Pierre Ossman671d2ef2015-06-09 16:09:06 +0200363 if (!damagedCursorRegion.is_empty())
Pierre Ossman5c9e1e52011-11-08 10:32:05 +0000364 removeRenderedCursor = true;
Pierre Ossman6b0bc292011-12-21 13:17:54 +0000365 if (needRenderedCursor()) {
Pierre Ossman671d2ef2015-06-09 16:09:06 +0200366 updateRenderedCursor = true;
Pierre Ossman6b0bc292011-12-21 13:17:54 +0000367 writeFramebufferUpdateOrClose();
368 }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000369}
370
371// needRenderedCursor() returns true if this client needs the server-side
372// rendered cursor. This may be because it does not support local cursor or
373// because the current cursor position has not been set by this client.
374// Unfortunately we can't know for sure when the current cursor position has
375// been set by this client. We guess that this is the case when the current
376// cursor position is the same as the last pointer event from this client, or
377// if it is a very short time since this client's last pointer event (up to a
378// second). [ Ideally we should do finer-grained timing here and make the time
379// configurable, but I don't think it's that important. ]
380
381bool VNCSConnectionST::needRenderedCursor()
382{
Pierre Ossman77ede0a2016-12-05 16:57:30 +0100383 if (state() != RFBSTATE_NORMAL)
384 return false;
385
386 if (!cp.supportsLocalCursor && !cp.supportsLocalXCursor)
387 return true;
388 if (!server->cursorPos.equals(pointerEventPos) &&
389 (time(0) - pointerEventTime) > 0)
390 return true;
391
392 return false;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000393}
394
395
396void VNCSConnectionST::approveConnectionOrClose(bool accept,
397 const char* reason)
398{
399 try {
400 approveConnection(accept, reason);
401 } catch (rdr::Exception& e) {
402 close(e.str());
403 }
404}
405
406
407
408// -=- Callbacks from SConnection
409
410void VNCSConnectionST::authSuccess()
411{
412 lastEventTime = time(0);
413
414 server->startDesktop();
415
416 // - Set the connection parameters appropriately
417 cp.width = server->pb->width();
418 cp.height = server->pb->height();
Pierre Ossman34e62f32009-03-20 21:46:12 +0000419 cp.screenLayout = server->screenLayout;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000420 cp.setName(server->getName());
421
422 // - Set the default pixel format
423 cp.setPF(server->pb->getPF());
424 char buffer[256];
425 cp.pf().print(buffer, 256);
426 vlog.info("Server default pixel format %s", buffer);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000427
428 // - Mark the entire display as "dirty"
429 updates.add_changed(server->pb->getRect());
430 startTime = time(0);
Pierre Ossman1b478e52011-11-15 12:08:30 +0000431
432 // - Bootstrap the congestion control
433 ackedOffset = sock->outStream().length();
434 congWindow = INITIAL_WINDOW;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000435}
436
437void VNCSConnectionST::queryConnection(const char* userName)
438{
439 // - Authentication succeeded - clear from blacklist
440 CharArray name; name.buf = sock->getPeerAddress();
441 server->blHosts->clearBlackmark(name.buf);
442
443 // - Special case to provide a more useful error message
444 if (rfb::Server::neverShared && !rfb::Server::disconnectClients &&
445 server->authClientCount() > 0) {
446 approveConnection(false, "The server is already in use");
447 return;
448 }
449
450 // - Does the client have the right to bypass the query?
451 if (reverseConnection ||
452 !(rfb::Server::queryConnect || sock->requiresQuery()) ||
453 (accessRights & AccessNoQuery))
454 {
455 approveConnection(true);
456 return;
457 }
458
459 // - Get the server to display an Accept/Reject dialog, if required
460 // If a dialog is displayed, the result will be PENDING, and the
461 // server will call approveConnection at a later time
462 CharArray reason;
463 VNCServerST::queryResult qr = server->queryConnection(sock, userName,
464 &reason.buf);
Pierre Ossmanf8e3b342015-01-26 14:37:04 +0100465 if (qr == VNCServerST::PENDING) {
466 queryConnectTimer.start(rfb::Server::queryConnectTimeout * 1000);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000467 return;
Pierre Ossmanf8e3b342015-01-26 14:37:04 +0100468 }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000469
470 // - If server returns ACCEPT/REJECT then pass result to SConnection
471 approveConnection(qr == VNCServerST::ACCEPT, reason.buf);
472}
473
474void VNCSConnectionST::clientInit(bool shared)
475{
476 lastEventTime = time(0);
477 if (rfb::Server::alwaysShared || reverseConnection) shared = true;
Pierre Ossmane7be49b2014-12-02 14:33:17 +0100478 if (!(accessRights & AccessNonShared)) shared = true;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000479 if (rfb::Server::neverShared) shared = false;
480 if (!shared) {
Pierre Ossmane7be49b2014-12-02 14:33:17 +0100481 if (rfb::Server::disconnectClients && (accessRights & AccessNonShared)) {
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000482 // - Close all the other connected clients
483 vlog.debug("non-shared connection - closing clients");
484 server->closeClients("Non-shared connection requested", getSock());
485 } else {
486 // - Refuse this connection if there are existing clients, in addition to
487 // this one
488 if (server->authClientCount() > 1) {
489 close("Server is already in use");
490 return;
491 }
492 }
493 }
494 SConnection::clientInit(shared);
495}
496
497void VNCSConnectionST::setPixelFormat(const PixelFormat& pf)
498{
499 SConnection::setPixelFormat(pf);
500 char buffer[256];
501 pf.print(buffer, 256);
502 vlog.info("Client pixel format %s", buffer);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000503 setCursor();
504}
505
506void VNCSConnectionST::pointerEvent(const Point& pos, int buttonMask)
507{
508 pointerEventTime = lastEventTime = time(0);
509 server->lastUserInputTime = lastEventTime;
510 if (!(accessRights & AccessPtrEvents)) return;
511 if (!rfb::Server::acceptPointerEvents) return;
512 if (!server->pointerClient || server->pointerClient == this) {
513 pointerEventPos = pos;
514 if (buttonMask)
515 server->pointerClient = this;
516 else
517 server->pointerClient = 0;
518 server->desktop->pointerEvent(pointerEventPos, buttonMask);
519 }
520}
521
522
523class VNCSConnectionSTShiftPresser {
524public:
525 VNCSConnectionSTShiftPresser(SDesktop* desktop_)
526 : desktop(desktop_), pressed(false) {}
527 ~VNCSConnectionSTShiftPresser() {
Pierre Ossman9a153b02015-08-31 10:01:14 +0200528 if (pressed) {
529 vlog.debug("Releasing fake Shift_L");
530 desktop->keyEvent(XK_Shift_L, false);
531 }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000532 }
533 void press() {
Pierre Ossman9a153b02015-08-31 10:01:14 +0200534 vlog.debug("Pressing fake Shift_L");
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000535 desktop->keyEvent(XK_Shift_L, true);
536 pressed = true;
537 }
538 SDesktop* desktop;
539 bool pressed;
540};
541
542// keyEvent() - record in the pressedKeys which keys were pressed. Allow
543// multiple down events (for autorepeat), but only allow a single up event.
544void VNCSConnectionST::keyEvent(rdr::U32 key, bool down) {
545 lastEventTime = time(0);
546 server->lastUserInputTime = lastEventTime;
547 if (!(accessRights & AccessKeyEvents)) return;
548 if (!rfb::Server::acceptKeyEvents) return;
549
Pierre Ossman9a153b02015-08-31 10:01:14 +0200550 if (down)
551 vlog.debug("Key pressed: 0x%x", key);
552 else
553 vlog.debug("Key released: 0x%x", key);
554
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000555 // Remap the key if required
Pierre Ossman9a153b02015-08-31 10:01:14 +0200556 if (server->keyRemapper) {
557 rdr::U32 newkey;
558 newkey = server->keyRemapper->remapKey(key);
559 if (newkey != key) {
560 vlog.debug("Key remapped to 0x%x", newkey);
561 key = newkey;
562 }
563 }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000564
565 // Turn ISO_Left_Tab into shifted Tab.
566 VNCSConnectionSTShiftPresser shiftPresser(server->desktop);
567 if (key == XK_ISO_Left_Tab) {
568 if (pressedKeys.find(XK_Shift_L) == pressedKeys.end() &&
569 pressedKeys.find(XK_Shift_R) == pressedKeys.end())
570 shiftPresser.press();
571 key = XK_Tab;
572 }
573
574 if (down) {
575 pressedKeys.insert(key);
576 } else {
577 if (!pressedKeys.erase(key)) return;
578 }
579 server->desktop->keyEvent(key, down);
580}
581
582void VNCSConnectionST::clientCutText(const char* str, int len)
583{
584 if (!(accessRights & AccessCutText)) return;
585 if (!rfb::Server::acceptCutText) return;
586 server->desktop->clientCutText(str, len);
587}
588
589void VNCSConnectionST::framebufferUpdateRequest(const Rect& r,bool incremental)
590{
Pierre Ossmane9962f72009-04-23 12:31:42 +0000591 Rect safeRect;
592
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000593 if (!(accessRights & AccessView)) return;
594
595 SConnection::framebufferUpdateRequest(r, incremental);
596
Pierre Ossmand9a59ba2009-03-20 15:55:37 +0000597 // Check that the client isn't sending crappy requests
598 if (!r.enclosed_by(Rect(0, 0, cp.width, cp.height))) {
599 vlog.error("FramebufferUpdateRequest %dx%d at %d,%d exceeds framebuffer %dx%d",
600 r.width(), r.height(), r.tl.x, r.tl.y, cp.width, cp.height);
Pierre Ossmane9962f72009-04-23 12:31:42 +0000601 safeRect = r.intersect(Rect(0, 0, cp.width, cp.height));
602 } else {
603 safeRect = r;
Pierre Ossmand9a59ba2009-03-20 15:55:37 +0000604 }
605
Constantin Kaplinsky2ef66952008-08-29 11:33:46 +0000606 // Just update the requested region.
607 // Framebuffer update will be sent a bit later, see processMessages().
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000608 Region reqRgn(r);
Pierre Ossman1b478e52011-11-15 12:08:30 +0000609 if (!incremental || !continuousUpdates)
610 requested.assign_union(reqRgn);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000611
612 if (!incremental) {
613 // Non-incremental update - treat as if area requested has changed
614 updates.add_changed(reqRgn);
Pierre Ossman53125a72009-04-22 15:37:51 +0000615
616 // And send the screen layout to the client (which, unlike the
617 // framebuffer dimensions, the client doesn't get during init)
Pierre Ossmanc5e25602009-03-20 12:59:05 +0000618 writer()->writeExtendedDesktopSize();
Pierre Ossman53125a72009-04-22 15:37:51 +0000619
620 // We do not send a DesktopSize since it only contains the
621 // framebuffer size (which the client already should know) and
622 // because some clients don't handle extra DesktopSize events
623 // very well.
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000624 }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000625}
626
Pierre Ossman34bb0612009-03-21 21:16:14 +0000627void VNCSConnectionST::setDesktopSize(int fb_width, int fb_height,
628 const ScreenSet& layout)
Pierre Ossmanc5e25602009-03-20 12:59:05 +0000629{
Pierre Ossman04e62db2009-03-23 16:57:07 +0000630 unsigned int result;
631
Michal Srbb318b8f2014-11-24 13:18:28 +0200632 if (!(accessRights & AccessSetDesktopSize)) return;
633 if (!rfb::Server::acceptSetDesktopSize) return;
634
Pierre Ossman04e62db2009-03-23 16:57:07 +0000635 // Don't bother the desktop with an invalid configuration
636 if (!layout.validate(fb_width, fb_height)) {
637 writer()->writeExtendedDesktopSize(reasonClient, resultInvalid,
638 fb_width, fb_height, layout);
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000639 writeFramebufferUpdate();
Pierre Ossman04e62db2009-03-23 16:57:07 +0000640 return;
641 }
642
643 // FIXME: the desktop will call back to VNCServerST and an extra set
644 // of ExtendedDesktopSize messages will be sent. This is okay
645 // protocol-wise, but unnecessary.
646 result = server->desktop->setScreenLayout(fb_width, fb_height, layout);
647
Pierre Ossman04e62db2009-03-23 16:57:07 +0000648 writer()->writeExtendedDesktopSize(reasonClient, result,
649 fb_width, fb_height, layout);
Pierre Ossman04e62db2009-03-23 16:57:07 +0000650
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000651 // Only notify other clients on success
Pierre Ossman04e62db2009-03-23 16:57:07 +0000652 if (result == resultSuccess) {
653 if (server->screenLayout != layout)
654 throw Exception("Desktop configured a different screen layout than requested");
655 server->notifyScreenLayoutChange(this);
656 }
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000657
658 // but always send back a reply to the requesting client
659 // (do this last as it might throw an exception on socket errors)
660 writeFramebufferUpdate();
Pierre Ossmanc5e25602009-03-20 12:59:05 +0000661}
662
Pierre Ossman2c764942011-11-14 15:54:30 +0000663void VNCSConnectionST::fence(rdr::U32 flags, unsigned len, const char data[])
664{
Pierre Ossmanb2a417c2015-12-11 19:32:21 +0100665 rdr::U8 type;
666
Pierre Ossman2c764942011-11-14 15:54:30 +0000667 if (flags & fenceFlagRequest) {
668 if (flags & fenceFlagSyncNext) {
Pierre Ossmanb8b1e962012-07-20 10:47:00 +0000669 pendingSyncFence = true;
Pierre Ossman2c764942011-11-14 15:54:30 +0000670
671 fenceFlags = flags & (fenceFlagBlockBefore | fenceFlagBlockAfter | fenceFlagSyncNext);
672 fenceDataLen = len;
673 delete [] fenceData;
Michal Srbf3afa242017-03-27 19:02:15 +0300674 fenceData = NULL;
Pierre Ossman2c764942011-11-14 15:54:30 +0000675 if (len > 0) {
676 fenceData = new char[len];
677 memcpy(fenceData, data, len);
678 }
679
680 return;
681 }
682
683 // We handle everything synchronously so we trivially honor these modes
684 flags = flags & (fenceFlagBlockBefore | fenceFlagBlockAfter);
685
686 writer()->writeFence(flags, len, data);
687 return;
688 }
689
Pierre Ossmanb2a417c2015-12-11 19:32:21 +0100690 if (len < 1)
691 vlog.error("Fence response of unexpected size received");
Pierre Ossman1b478e52011-11-15 12:08:30 +0000692
Pierre Ossmanb2a417c2015-12-11 19:32:21 +0100693 type = data[0];
694
695 switch (type) {
Pierre Ossman2c764942011-11-14 15:54:30 +0000696 case 0:
697 // Initial dummy fence;
698 break;
Pierre Ossmanb2a417c2015-12-11 19:32:21 +0100699 case 1:
700 handleRTTPong();
Pierre Ossman1b478e52011-11-15 12:08:30 +0000701 break;
Pierre Ossman2c764942011-11-14 15:54:30 +0000702 default:
Pierre Ossmanb2a417c2015-12-11 19:32:21 +0100703 vlog.error("Fence response of unexpected type received");
Pierre Ossman2c764942011-11-14 15:54:30 +0000704 }
705}
706
Pierre Ossman1b478e52011-11-15 12:08:30 +0000707void VNCSConnectionST::enableContinuousUpdates(bool enable,
708 int x, int y, int w, int h)
709{
710 Rect rect;
711
712 if (!cp.supportsFence || !cp.supportsContinuousUpdates)
713 throw Exception("Client tried to enable continuous updates when not allowed");
714
715 continuousUpdates = enable;
716
717 rect.setXYWH(x, y, w, h);
718 cuRegion.reset(rect);
719
720 if (enable) {
721 requested.clear();
722 writeFramebufferUpdate();
723 } else {
724 writer()->writeEndOfContinuousUpdates();
725 }
726}
727
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000728// supportsLocalCursor() is called whenever the status of
729// cp.supportsLocalCursor has changed. If the client does now support local
730// cursor, we make sure that the old server-side rendered cursor is cleaned up
731// and the cursor is sent to the client.
732
733void VNCSConnectionST::supportsLocalCursor()
734{
735 if (cp.supportsLocalCursor || cp.supportsLocalXCursor) {
Pierre Ossman671d2ef2015-06-09 16:09:06 +0200736 if (!damagedCursorRegion.is_empty())
Pierre Ossman5c9e1e52011-11-08 10:32:05 +0000737 removeRenderedCursor = true;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000738 setCursor();
739 }
740}
741
Pierre Ossman2c764942011-11-14 15:54:30 +0000742void VNCSConnectionST::supportsFence()
743{
Pierre Ossmanb2a417c2015-12-11 19:32:21 +0100744 char type = 0;
745 writer()->writeFence(fenceFlagRequest, sizeof(type), &type);
Pierre Ossman2c764942011-11-14 15:54:30 +0000746}
747
Pierre Ossman1b478e52011-11-15 12:08:30 +0000748void VNCSConnectionST::supportsContinuousUpdates()
749{
750 // We refuse to use continuous updates if we cannot monitor the buffer
751 // usage using fences.
752 if (!cp.supportsFence)
753 return;
754
755 writer()->writeEndOfContinuousUpdates();
756}
757
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000758
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +0000759bool VNCSConnectionST::handleTimeout(Timer* t)
760{
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000761 try {
Pierre Ossmana40ab202016-04-29 15:35:56 +0200762 if (t == &congestionTimer)
Pierre Ossman1b478e52011-11-15 12:08:30 +0000763 updateCongestion();
Pierre Ossmanf8e3b342015-01-26 14:37:04 +0100764 else if (t == &queryConnectTimer) {
765 if (state() == RFBSTATE_QUERYING)
766 approveConnection(false, "The attempt to prompt the user to accept the connection failed");
767 }
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000768 } catch (rdr::Exception& e) {
769 close(e.str());
770 }
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +0000771
772 return false;
773}
774
775
Pierre Ossman1b478e52011-11-15 12:08:30 +0000776void VNCSConnectionST::writeRTTPing()
777{
778 struct RTTInfo rttInfo;
Pierre Ossmanb2a417c2015-12-11 19:32:21 +0100779 char type;
Pierre Ossman1b478e52011-11-15 12:08:30 +0000780
781 if (!cp.supportsFence)
782 return;
783
784 memset(&rttInfo, 0, sizeof(struct RTTInfo));
785
786 gettimeofday(&rttInfo.tv, NULL);
787 rttInfo.offset = sock->outStream().length();
788 rttInfo.inFlight = rttInfo.offset - ackedOffset;
789
Pierre Ossmanb2a417c2015-12-11 19:32:21 +0100790 pings.push_back(rttInfo);
791
Pierre Ossman1b478e52011-11-15 12:08:30 +0000792 // We need to make sure any old update are already processed by the
793 // time we get the response back. This allows us to reliably throttle
794 // back on client overload, as well as network overload.
Pierre Ossmanb2a417c2015-12-11 19:32:21 +0100795 type = 1;
Pierre Ossman1b478e52011-11-15 12:08:30 +0000796 writer()->writeFence(fenceFlagRequest | fenceFlagBlockBefore,
Pierre Ossmanb2a417c2015-12-11 19:32:21 +0100797 sizeof(type), &type);
Pierre Ossman1b478e52011-11-15 12:08:30 +0000798
799 sentOffset = rttInfo.offset;
800
801 // Let some data flow before we adjust the settings
802 if (!congestionTimer.isStarted())
803 congestionTimer.start(__rfbmin(baseRTT * 2, 100));
804}
805
Pierre Ossmanb2a417c2015-12-11 19:32:21 +0100806void VNCSConnectionST::handleRTTPong()
Pierre Ossman1b478e52011-11-15 12:08:30 +0000807{
Pierre Ossmanb2a417c2015-12-11 19:32:21 +0100808 struct RTTInfo rttInfo;
Pierre Ossman1b478e52011-11-15 12:08:30 +0000809 unsigned rtt, delay;
Pierre Ossman1b478e52011-11-15 12:08:30 +0000810
Pierre Ossmanb2a417c2015-12-11 19:32:21 +0100811 if (pings.empty())
812 return;
813
814 rttInfo = pings.front();
815 pings.pop_front();
Pierre Ossman1b478e52011-11-15 12:08:30 +0000816
817 rtt = msSince(&rttInfo.tv);
818 if (rtt < 1)
819 rtt = 1;
820
821 ackedOffset = rttInfo.offset;
822
823 // Try to estimate wire latency by tracking lowest seen latency
824 if (rtt < baseRTT)
825 baseRTT = rtt;
826
827 if (rttInfo.inFlight > congWindow) {
828 seenCongestion = true;
829
830 // Estimate added delay because of overtaxed buffers
831 delay = (rttInfo.inFlight - congWindow) * baseRTT / congWindow;
832
833 if (delay < rtt)
834 rtt -= delay;
835 else
836 rtt = 1;
837
838 // If we underestimate the congestion window, then we'll get a latency
839 // that's less than the wire latency, which will confuse other portions
840 // of the code.
841 if (rtt < baseRTT)
842 rtt = baseRTT;
843 }
844
845 // We only keep track of the minimum latency seen (for a given interval)
klemens0536d092017-01-28 20:56:56 +0100846 // on the basis that we want to avoid continuous buffer issue, but don't
Pierre Ossman1b478e52011-11-15 12:08:30 +0000847 // mind (or even approve of) bursts.
848 if (rtt < minRTT)
849 minRTT = rtt;
850}
851
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +0000852bool VNCSConnectionST::isCongested()
853{
Pierre Ossman1b478e52011-11-15 12:08:30 +0000854 int offset;
855
856 // Stuff still waiting in the send buffer?
Pierre Ossman352d0622016-04-29 15:50:54 +0200857 sock->outStream().flush();
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +0000858 if (sock->outStream().bufferUsage() > 0)
859 return true;
860
Pierre Ossman1b478e52011-11-15 12:08:30 +0000861 if (!cp.supportsFence)
862 return false;
863
864 // Idle for too long? (and no data on the wire)
865 //
866 // FIXME: This should really just be one baseRTT, but we're getting
867 // problems with triggering the idle timeout on each update.
868 // Maybe we need to use a moving average for the wire latency
869 // instead of baseRTT.
870 if ((sentOffset == ackedOffset) &&
871 (sock->outStream().getIdleTime() > 2 * baseRTT)) {
872
873#ifdef CONGESTION_DEBUG
874 if (congWindow > INITIAL_WINDOW)
875 fprintf(stderr, "Reverting to initial window (%d KiB) after %d ms\n",
876 INITIAL_WINDOW / 1024, sock->outStream().getIdleTime());
877#endif
878
879 // Close congestion window and allow a transfer
880 // FIXME: Reset baseRTT like Linux Vegas?
881 congWindow = __rfbmin(INITIAL_WINDOW, congWindow);
882
883 return false;
884 }
885
886 offset = sock->outStream().length();
887
888 // FIXME: Should we compensate for non-update data?
889 // (i.e. use sentOffset instead of offset)
890 if ((offset - ackedOffset) < congWindow)
891 return false;
892
893 // If we just have one outstanding "ping", that means the client has
894 // started receiving our update. In order to not regress compared to
895 // before we had congestion avoidance, we allow another update here.
896 // This could further clog up the tubes, but congestion control isn't
897 // really working properly right now anyway as the wire would otherwise
898 // be idle for at least RTT/2.
Pierre Ossmanb2a417c2015-12-11 19:32:21 +0100899 if (pings.size() == 1)
Pierre Ossman1b478e52011-11-15 12:08:30 +0000900 return false;
901
902 return true;
903}
904
905
906void VNCSConnectionST::updateCongestion()
907{
908 unsigned diff;
909
910 if (!seenCongestion)
911 return;
912
913 diff = minRTT - baseRTT;
914
915 if (diff > __rfbmin(100, baseRTT)) {
916 // Way too fast
917 congWindow = congWindow * baseRTT / minRTT;
918 } else if (diff > __rfbmin(50, baseRTT/2)) {
919 // Slightly too fast
920 congWindow -= 4096;
921 } else if (diff < 5) {
922 // Way too slow
923 congWindow += 8192;
924 } else if (diff < 25) {
925 // Too slow
926 congWindow += 4096;
927 }
928
929 if (congWindow < MINIMUM_WINDOW)
930 congWindow = MINIMUM_WINDOW;
931 if (congWindow > MAXIMUM_WINDOW)
932 congWindow = MAXIMUM_WINDOW;
933
934#ifdef CONGESTION_DEBUG
935 fprintf(stderr, "RTT: %d ms (%d ms), Window: %d KiB, Bandwidth: %g Mbps\n",
936 minRTT, baseRTT, congWindow / 1024,
937 congWindow * 8.0 / baseRTT / 1000.0);
938
939#ifdef TCP_INFO
940 struct tcp_info tcp_info;
941 socklen_t tcp_info_length;
942
943 tcp_info_length = sizeof(tcp_info);
944 if (getsockopt(sock->getFd(), SOL_TCP, TCP_INFO,
945 (void *)&tcp_info, &tcp_info_length) == 0) {
946 fprintf(stderr, "Socket: RTT: %d ms (+/- %d ms) Window %d KiB\n",
947 tcp_info.tcpi_rtt / 1000, tcp_info.tcpi_rttvar / 1000,
948 tcp_info.tcpi_snd_mss * tcp_info.tcpi_snd_cwnd / 1024);
949 }
950#endif
951
952#endif
953
954 minRTT = -1;
955 seenCongestion = false;
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +0000956}
957
958
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000959void VNCSConnectionST::writeFramebufferUpdate()
960{
Pierre Ossman2c764942011-11-14 15:54:30 +0000961 // We're in the middle of processing a command that's supposed to be
962 // synchronised. Allowing an update to slip out right now might violate
963 // that synchronisation.
964 if (syncFence)
965 return;
966
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000967 // We try to aggregate responses, so don't send out anything whilst we
968 // still have incoming messages. processMessages() will give us another
969 // chance to run once things are idle.
970 if (inProcessMessages)
971 return;
972
Pierre Ossman1b478e52011-11-15 12:08:30 +0000973 if (state() != RFBSTATE_NORMAL)
974 return;
975 if (requested.is_empty() && !continuousUpdates)
Pierre Ossmane9962f72009-04-23 12:31:42 +0000976 return;
Pierre Ossmand9a59ba2009-03-20 15:55:37 +0000977
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +0000978 // Check that we actually have some space on the link and retry in a
979 // bit if things are congested.
Pierre Ossmana40ab202016-04-29 15:35:56 +0200980 if (isCongested())
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +0000981 return;
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +0000982
Pierre Ossmancef3cf72016-11-25 10:06:34 +0100983 // Updates often consists of many small writes, and in continuous
984 // mode, we will also have small fence messages around the update. We
985 // need to aggregate these in order to not clog up TCP's congestion
986 // window.
Pierre Ossman36dadf82011-11-15 12:11:32 +0000987 network::TcpSocket::cork(sock->getFd(), true);
988
Pierre Ossmane9962f72009-04-23 12:31:42 +0000989 // First take care of any updates that cannot contain framebuffer data
990 // changes.
Pierre Ossmancef3cf72016-11-25 10:06:34 +0100991 writeNoDataUpdate();
992
993 // Then real data (if possible)
994 writeDataUpdate();
995
996 network::TcpSocket::cork(sock->getFd(), false);
997}
998
999void VNCSConnectionST::writeNoDataUpdate()
1000{
1001 if (!writer()->needNoDataUpdate())
1002 return;
1003
1004 writer()->writeNoDataUpdate();
1005
1006 // Make sure no data update is sent until next request
1007 requested.clear();
1008}
1009
1010void VNCSConnectionST::writeDataUpdate()
1011{
1012 Region req;
1013 UpdateInfo ui;
1014 bool needNewUpdateInfo;
Pierre Ossman24684e52016-12-05 16:58:19 +01001015 const RenderedCursor *cursor;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001016
Constantin Kaplinsky604d7812007-08-31 15:50:37 +00001017 updates.enable_copyrect(cp.useCopyRect);
1018
Pierre Ossman6e49e952016-10-07 15:59:38 +02001019 // See if we are allowed to send anything right now (the framebuffer
1020 // might have changed in ways we haven't yet been informed of).
Pierre Ossmanbbf955e2011-11-08 12:44:10 +00001021 if (!server->checkUpdate())
Pierre Ossmancef3cf72016-11-25 10:06:34 +01001022 return;
Constantin Kaplinskya09dc142008-12-18 12:08:15 +00001023
Pierre Ossmancef3cf72016-11-25 10:06:34 +01001024 // See what the client has requested (if anything)
Pierre Ossman1b478e52011-11-15 12:08:30 +00001025 if (continuousUpdates)
1026 req = cuRegion.union_(requested);
1027 else
1028 req = requested;
1029
Pierre Ossmancef3cf72016-11-25 10:06:34 +01001030 if (req.is_empty())
1031 return;
1032
1033 // Get the lists of updates. Prior to exporting the data to the `ui' object,
1034 // getUpdateInfo() will normalize the `updates' object such way that its
1035 // `changed' and `copied' regions would not intersect.
Pierre Ossman1b478e52011-11-15 12:08:30 +00001036 updates.getUpdateInfo(&ui, req);
1037 needNewUpdateInfo = false;
Constantin Kaplinsky604d7812007-08-31 15:50:37 +00001038
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001039 // If the previous position of the rendered cursor overlaps the source of the
1040 // copy, then when the copy happens the corresponding rectangle in the
1041 // destination will be wrong, so add it to the changed region.
1042
Pierre Ossman671d2ef2015-06-09 16:09:06 +02001043 if (!ui.copied.is_empty() && !damagedCursorRegion.is_empty()) {
1044 Region bogusCopiedCursor;
1045
1046 bogusCopiedCursor.copyFrom(damagedCursorRegion);
1047 bogusCopiedCursor.translate(ui.copy_delta);
1048 bogusCopiedCursor.assign_intersect(server->pb->getRect());
Constantin Kaplinsky45517c82007-08-31 15:56:33 +00001049 if (!ui.copied.intersect(bogusCopiedCursor).is_empty()) {
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001050 updates.add_changed(bogusCopiedCursor);
Constantin Kaplinsky604d7812007-08-31 15:50:37 +00001051 needNewUpdateInfo = true;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001052 }
1053 }
1054
Pierre Ossman671d2ef2015-06-09 16:09:06 +02001055 // If we need to remove the old rendered cursor, just add the region to
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001056 // the changed region.
1057
1058 if (removeRenderedCursor) {
Pierre Ossman671d2ef2015-06-09 16:09:06 +02001059 updates.add_changed(damagedCursorRegion);
Constantin Kaplinsky604d7812007-08-31 15:50:37 +00001060 needNewUpdateInfo = true;
Pierre Ossman671d2ef2015-06-09 16:09:06 +02001061 damagedCursorRegion.clear();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001062 removeRenderedCursor = false;
1063 }
1064
1065 // Return if there is nothing to send the client.
1066
Pierre Ossman671d2ef2015-06-09 16:09:06 +02001067 if (updates.is_empty() && !writer()->needFakeUpdate() && !updateRenderedCursor)
Pierre Ossmancef3cf72016-11-25 10:06:34 +01001068 return;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001069
Constantin Kaplinsky604d7812007-08-31 15:50:37 +00001070 // The `updates' object could change, make sure we have valid update info.
1071
1072 if (needNewUpdateInfo)
Pierre Ossman1b478e52011-11-15 12:08:30 +00001073 updates.getUpdateInfo(&ui, req);
Constantin Kaplinsky604d7812007-08-31 15:50:37 +00001074
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001075 // If the client needs a server-side rendered cursor, work out the cursor
1076 // rectangle. If it's empty then don't bother drawing it, but if it overlaps
1077 // with the update region, we need to draw the rendered cursor regardless of
1078 // whether it has changed.
1079
Pierre Ossman24684e52016-12-05 16:58:19 +01001080 cursor = NULL;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001081 if (needRenderedCursor()) {
Pierre Ossman671d2ef2015-06-09 16:09:06 +02001082 Rect renderedCursorRect;
1083
Pierre Ossman24684e52016-12-05 16:58:19 +01001084 cursor = server->getRenderedCursor();
1085
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001086 renderedCursorRect
Pierre Ossman24684e52016-12-05 16:58:19 +01001087 = cursor->getEffectiveRect().intersect(req.get_bounding_rect());
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001088
1089 if (renderedCursorRect.is_empty()) {
Pierre Ossman24684e52016-12-05 16:58:19 +01001090 cursor = NULL;
1091 } else if (!updateRenderedCursor &&
1092 ui.changed.union_(ui.copied)
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001093 .intersect(renderedCursorRect).is_empty()) {
Pierre Ossman24684e52016-12-05 16:58:19 +01001094 cursor = NULL;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001095 }
1096
Pierre Ossman5c037202016-12-05 17:00:35 +01001097 if (cursor) {
1098 updates.subtract(renderedCursorRect);
1099 updates.getUpdateInfo(&ui, req);
1100 }
Pierre Ossman671d2ef2015-06-09 16:09:06 +02001101
1102 damagedCursorRegion.assign_union(renderedCursorRect);
1103 updateRenderedCursor = false;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001104 }
1105
Pierre Ossman24684e52016-12-05 16:58:19 +01001106 if (ui.is_empty() && !writer()->needFakeUpdate() && !cursor)
Pierre Ossmancef3cf72016-11-25 10:06:34 +01001107 return;
Pierre Ossmanfdba3fe2014-01-31 13:12:18 +01001108
Pierre Ossmancef3cf72016-11-25 10:06:34 +01001109 writeRTTPing();
Pierre Ossman1b478e52011-11-15 12:08:30 +00001110
Pierre Ossmancef3cf72016-11-25 10:06:34 +01001111 encodeManager.writeUpdate(ui, server->getPixelBuffer(), cursor);
Pierre Ossman1b478e52011-11-15 12:08:30 +00001112
Pierre Ossmancef3cf72016-11-25 10:06:34 +01001113 writeRTTPing();
Pierre Ossmanc0397262014-03-14 15:59:46 +01001114
Pierre Ossmancef3cf72016-11-25 10:06:34 +01001115 // The request might be for just part of the screen, so we cannot
1116 // just clear the entire update tracker.
1117 updates.subtract(req);
Pierre Ossmanc0397262014-03-14 15:59:46 +01001118
Pierre Ossmancef3cf72016-11-25 10:06:34 +01001119 requested.clear();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001120}
1121
1122
Pierre Ossmana3ac01e2011-11-07 21:13:54 +00001123void VNCSConnectionST::screenLayoutChange(rdr::U16 reason)
1124{
1125 if (!authenticated())
1126 return;
1127
1128 cp.screenLayout = server->screenLayout;
1129
1130 if (state() != RFBSTATE_NORMAL)
1131 return;
1132
1133 writer()->writeExtendedDesktopSize(reason, 0, cp.width, cp.height,
1134 cp.screenLayout);
1135 writeFramebufferUpdate();
1136}
1137
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001138
1139// setCursor() is called whenever the cursor has changed shape or pixel format.
1140// If the client supports local cursor then it will arrange for the cursor to
1141// be sent to the client.
1142
1143void VNCSConnectionST::setCursor()
1144{
Pierre Ossmana3ac01e2011-11-07 21:13:54 +00001145 if (state() != RFBSTATE_NORMAL)
1146 return;
Pierre Ossmana3ac01e2011-11-07 21:13:54 +00001147
Pierre Ossman6a1a0d02017-02-19 15:48:17 +01001148 cp.setCursor(*server->cursor);
Pierre Ossman126e5642014-02-13 14:40:25 +01001149
Pierre Ossman8053c8e2017-02-21 12:59:04 +01001150 if (!writer()->writeSetCursorWithAlpha()) {
1151 if (!writer()->writeSetCursor()) {
1152 if (!writer()->writeSetXCursor()) {
1153 // No client support
1154 return;
1155 }
Pierre Ossman126e5642014-02-13 14:40:25 +01001156 }
1157 }
1158
Pierre Ossmana3ac01e2011-11-07 21:13:54 +00001159 writeFramebufferUpdate();
1160}
1161
1162void VNCSConnectionST::setDesktopName(const char *name)
1163{
1164 cp.setName(name);
1165
1166 if (state() != RFBSTATE_NORMAL)
1167 return;
1168
1169 if (!writer()->writeSetDesktopName()) {
1170 fprintf(stderr, "Client does not support desktop rename\n");
1171 return;
1172 }
1173
1174 writeFramebufferUpdate();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001175}
1176
1177void VNCSConnectionST::setSocketTimeouts()
1178{
1179 int timeoutms = rfb::Server::clientWaitTimeMillis;
1180 soonestTimeout(&timeoutms, secsToMillis(rfb::Server::idleTimeout));
1181 if (timeoutms == 0)
1182 timeoutms = -1;
1183 sock->inStream().setTimeout(timeoutms);
1184 sock->outStream().setTimeout(timeoutms);
1185}
1186
1187char* VNCSConnectionST::getStartTime()
1188{
1189 char* result = ctime(&startTime);
1190 result[24] = '\0';
1191 return result;
1192}
1193
1194void VNCSConnectionST::setStatus(int status)
1195{
1196 switch (status) {
1197 case 0:
1198 accessRights = accessRights | AccessPtrEvents | AccessKeyEvents | AccessView;
1199 break;
1200 case 1:
Pierre Ossman7728be22015-03-03 16:39:37 +01001201 accessRights = (accessRights & ~(AccessPtrEvents | AccessKeyEvents)) | AccessView;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001202 break;
1203 case 2:
Adam Tkac8e985062011-02-07 11:33:57 +00001204 accessRights = accessRights & ~(AccessPtrEvents | AccessKeyEvents | AccessView);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001205 break;
1206 }
1207 framebufferUpdateRequest(server->pb->getRect(), false);
1208}
1209int VNCSConnectionST::getStatus()
1210{
1211 if ((accessRights & (AccessPtrEvents | AccessKeyEvents | AccessView)) == 0x0007)
1212 return 0;
1213 if ((accessRights & (AccessPtrEvents | AccessKeyEvents | AccessView)) == 0x0001)
1214 return 1;
1215 if ((accessRights & (AccessPtrEvents | AccessKeyEvents | AccessView)) == 0x0000)
1216 return 2;
1217 return 4;
1218}
1219