blob: 619b0a24a3aa591e8bd36448a20db0d81d9e3b32 [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>
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000032#include <rfb/VNCSConnectionST.h>
33#include <rfb/LogWriter.h>
Adam Tkac5a0caed2010-04-23 13:58:10 +000034#include <rfb/Security.h>
Pierre Ossmanc5e25602009-03-20 12:59:05 +000035#include <rfb/screenTypes.h>
Pierre Ossman2c764942011-11-14 15:54:30 +000036#include <rfb/fenceTypes.h>
Pierre Ossmanbb305ca2016-12-11 12:41:26 +010037#include <rfb/ledStates.h>
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000038#include <rfb/ServerCore.h>
39#include <rfb/ComparingUpdateTracker.h>
40#include <rfb/KeyRemapper.h>
Pierre Ossmanfdba3fe2014-01-31 13:12:18 +010041#include <rfb/Encoder.h>
Pierre Ossmanbb305ca2016-12-11 12:41:26 +010042#define XK_LATIN1
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000043#define XK_MISCELLANY
44#define XK_XKB_KEYS
45#include <rfb/keysymdef.h>
46
47using namespace rfb;
48
49static LogWriter vlog("VNCSConnST");
50
Pierre Ossman1b478e52011-11-15 12:08:30 +000051// This window should get us going fairly fast on a decent bandwidth network.
52// If it's too high, it will rapidly be reduced and stay low.
53static const unsigned INITIAL_WINDOW = 16384;
54
55// TCP's minimal window is 3*MSS. But since we don't know the MSS, we
klemens0536d092017-01-28 20:56:56 +010056// make a guess at 4 KiB (it's probably a bit higher).
Pierre Ossman1b478e52011-11-15 12:08:30 +000057static const unsigned MINIMUM_WINDOW = 4096;
58
59// The current default maximum window for Linux (4 MiB). Should be a good
60// limit for now...
61static const unsigned MAXIMUM_WINDOW = 4194304;
62
63struct RTTInfo {
64 struct timeval tv;
65 int offset;
66 unsigned inFlight;
67};
68
Pierre Ossman71ca8d52017-09-15 11:03:12 +020069static Cursor emptyCursor(0, 0, Point(0, 0), NULL);
70
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000071VNCSConnectionST::VNCSConnectionST(VNCServerST* server_, network::Socket *s,
72 bool reverse)
Pierre Ossman7069bdd2015-02-06 14:41:58 +010073 : sock(s), reverseConnection(reverse),
Pierre Ossman36304752017-10-04 16:21:57 +020074 inProcessMessages(false),
Pierre Ossmanb8b1e962012-07-20 10:47:00 +000075 pendingSyncFence(false), syncFence(false), fenceFlags(0),
76 fenceDataLen(0), fenceData(NULL),
Pierre Ossmanb1cd6ca2015-03-03 16:37:43 +010077 baseRTT(-1), congWindow(0), ackedOffset(0), sentOffset(0),
78 minRTT(-1), seenCongestion(false),
79 pingCounter(0), congestionTimer(this),
Pierre Ossman0c9bd4b2014-07-09 16:44:11 +020080 server(server_), updates(false),
Pierre Ossman671d2ef2015-06-09 16:09:06 +020081 updateRenderedCursor(false), removeRenderedCursor(false),
Pierre Ossmana40ab202016-04-29 15:35:56 +020082 continuousUpdates(false), encodeManager(this), pointerEventTime(0),
Pierre Ossman25db44a2017-11-16 16:40:44 +010083 clientHasCursor(false),
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +000084 accessRights(AccessDefault), startTime(time(0))
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000085{
86 setStreams(&sock->inStream(), &sock->outStream());
87 peerEndpoint.buf = sock->getPeerEndpoint();
88 VNCServerST::connectionsLog.write(1,"accepted: %s", peerEndpoint.buf);
89
90 // Configure the socket
91 setSocketTimeouts();
92 lastEventTime = time(0);
93
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000094 server->clients.push_front(this);
95}
96
97
98VNCSConnectionST::~VNCSConnectionST()
99{
100 // If we reach here then VNCServerST is deleting us!
101 VNCServerST::connectionsLog.write(1,"closed: %s (%s)",
102 peerEndpoint.buf,
103 (closeReason.buf) ? closeReason.buf : "");
104
105 // Release any keys the client still had pressed
Pierre Ossman16e1dcb2017-05-16 14:33:43 +0200106 while (!pressedKeys.empty()) {
107 rdr::U32 keysym, keycode;
108
109 keysym = pressedKeys.begin()->second;
110 keycode = pressedKeys.begin()->first;
111 pressedKeys.erase(pressedKeys.begin());
112
113 vlog.debug("Releasing key 0x%x / 0x%x on client disconnect",
114 keysym, keycode);
115 server->desktop->keyEvent(keysym, keycode, false);
Pierre Ossman9a153b02015-08-31 10:01:14 +0200116 }
Pierre Ossman16e1dcb2017-05-16 14:33:43 +0200117
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000118 if (server->pointerClient == this)
119 server->pointerClient = 0;
120
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000121 // Remove this client from the server
122 server->clients.remove(this);
123
Pierre Ossman2c764942011-11-14 15:54:30 +0000124 delete [] fenceData;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000125}
126
127
128// Methods called from VNCServerST
129
130bool VNCSConnectionST::init()
131{
132 try {
133 initialiseProtocol();
134 } catch (rdr::Exception& e) {
135 close(e.str());
136 return false;
137 }
138 return true;
139}
140
141void VNCSConnectionST::close(const char* reason)
142{
143 // Log the reason for the close
144 if (!closeReason.buf)
Adam Tkacd36b6262009-09-04 10:57:20 +0000145 closeReason.buf = strDup(reason);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000146 else
147 vlog.debug("second close: %s (%s)", peerEndpoint.buf, reason);
148
149 if (authenticated()) {
150 server->lastDisconnectTime = time(0);
151 }
152
153 // Just shutdown the socket and mark our state as closing. Eventually the
154 // calling code will call VNCServerST's removeSocket() method causing us to
155 // be deleted.
156 sock->shutdown();
157 setState(RFBSTATE_CLOSING);
158}
159
160
161void VNCSConnectionST::processMessages()
162{
163 if (state() == RFBSTATE_CLOSING) return;
164 try {
165 // - Now set appropriate socket timeouts and process data
166 setSocketTimeouts();
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000167
168 inProcessMessages = true;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000169
Pierre Ossmana830bec2011-11-08 12:12:02 +0000170 // Get the underlying TCP layer to build large packets if we send
171 // multiple small responses.
Peter Ã…strand (astrand)01dc1a62017-10-10 12:56:04 +0200172 sock->cork(true);
Pierre Ossmana830bec2011-11-08 12:12:02 +0000173
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000174 while (getInStream()->checkNoWait(1)) {
Pierre Ossmanb8b1e962012-07-20 10:47:00 +0000175 if (pendingSyncFence) {
176 syncFence = true;
177 pendingSyncFence = false;
178 }
179
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000180 processMsg();
Pierre Ossmanb8b1e962012-07-20 10:47:00 +0000181
Pierre Ossman2c764942011-11-14 15:54:30 +0000182 if (syncFence) {
183 writer()->writeFence(fenceFlags, fenceDataLen, fenceData);
184 syncFence = false;
185 }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000186 }
187
Pierre Ossmana830bec2011-11-08 12:12:02 +0000188 // Flush out everything in case we go idle after this.
Peter Ã…strand (astrand)01dc1a62017-10-10 12:56:04 +0200189 sock->cork(false);
Pierre Ossmana830bec2011-11-08 12:12:02 +0000190
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000191 inProcessMessages = false;
Constantin Kaplinsky2ef66952008-08-29 11:33:46 +0000192
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000193 // If there were anything requiring an update, try to send it here.
194 // We wait until now with this to aggregate responses and to give
195 // higher priority to user actions such as keyboard and pointer events.
196 writeFramebufferUpdate();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000197 } catch (rdr::EndOfStream&) {
198 close("Clean disconnection");
199 } catch (rdr::Exception &e) {
200 close(e.str());
201 }
202}
203
Pierre Ossmand408ca52016-04-29 14:26:05 +0200204void VNCSConnectionST::flushSocket()
205{
206 if (state() == RFBSTATE_CLOSING) return;
207 try {
208 setSocketTimeouts();
209 sock->outStream().flush();
Pierre Ossmana40ab202016-04-29 15:35:56 +0200210 // Flushing the socket might release an update that was previously
211 // delayed because of congestion.
212 if (sock->outStream().bufferUsage() == 0)
213 writeFramebufferUpdate();
Pierre Ossmand408ca52016-04-29 14:26:05 +0200214 } catch (rdr::Exception &e) {
215 close(e.str());
216 }
217}
218
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000219void VNCSConnectionST::pixelBufferChange()
220{
221 try {
222 if (!authenticated()) return;
223 if (cp.width && cp.height && (server->pb->width() != cp.width ||
224 server->pb->height() != cp.height))
225 {
226 // We need to clip the next update to the new size, but also add any
227 // extra bits if it's bigger. If we wanted to do this exactly, something
228 // like the code below would do it, but at the moment we just update the
Pierre Ossman671d2ef2015-06-09 16:09:06 +0200229 // entire new size. However, we do need to clip the damagedCursorRegion
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000230 // because that might be added to updates in writeFramebufferUpdate().
231
232 //updates.intersect(server->pb->getRect());
233 //
234 //if (server->pb->width() > cp.width)
235 // updates.add_changed(Rect(cp.width, 0, server->pb->width(),
236 // server->pb->height()));
237 //if (server->pb->height() > cp.height)
238 // updates.add_changed(Rect(0, cp.height, cp.width,
239 // server->pb->height()));
240
Pierre Ossman671d2ef2015-06-09 16:09:06 +0200241 damagedCursorRegion.assign_intersect(server->pb->getRect());
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000242
243 cp.width = server->pb->width();
244 cp.height = server->pb->height();
Pierre Ossman34e62f32009-03-20 21:46:12 +0000245 cp.screenLayout = server->screenLayout;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000246 if (state() == RFBSTATE_NORMAL) {
Pierre Ossman2ee430a2009-05-28 12:54:24 +0000247 // We should only send EDS to client asking for both
248 if (!writer()->writeExtendedDesktopSize()) {
249 if (!writer()->writeSetDesktopSize()) {
250 close("Client does not support desktop resize");
251 return;
252 }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000253 }
254 }
255 }
256 // Just update the whole screen at the moment because we're too lazy to
257 // work out what's actually changed.
258 updates.clear();
259 updates.add_changed(server->pb->getRect());
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000260 writeFramebufferUpdate();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000261 } catch(rdr::Exception &e) {
262 close(e.str());
263 }
264}
265
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000266void VNCSConnectionST::writeFramebufferUpdateOrClose()
Pierre Ossman04e62db2009-03-23 16:57:07 +0000267{
268 try {
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000269 writeFramebufferUpdate();
270 } catch(rdr::Exception &e) {
271 close(e.str());
272 }
273}
Pierre Ossman04e62db2009-03-23 16:57:07 +0000274
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000275void VNCSConnectionST::screenLayoutChangeOrClose(rdr::U16 reason)
276{
277 try {
278 screenLayoutChange(reason);
Pierre Ossmanb218ecd2017-11-16 16:43:13 +0100279 writeFramebufferUpdate();
Pierre Ossman04e62db2009-03-23 16:57:07 +0000280 } catch(rdr::Exception &e) {
281 close(e.str());
282 }
283}
284
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000285void VNCSConnectionST::bellOrClose()
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000286{
287 try {
288 if (state() == RFBSTATE_NORMAL) writer()->writeBell();
289 } catch(rdr::Exception& e) {
290 close(e.str());
291 }
292}
293
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000294void VNCSConnectionST::serverCutTextOrClose(const char *str, int len)
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000295{
296 try {
297 if (!(accessRights & AccessCutText)) return;
298 if (!rfb::Server::sendCutText) return;
299 if (state() == RFBSTATE_NORMAL)
300 writer()->writeServerCutText(str, len);
301 } catch(rdr::Exception& e) {
302 close(e.str());
303 }
304}
305
Peter Ã…strandc39e0782009-01-15 12:21:42 +0000306
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000307void VNCSConnectionST::setDesktopNameOrClose(const char *name)
Peter Ã…strandc39e0782009-01-15 12:21:42 +0000308{
Peter Ã…strandc39e0782009-01-15 12:21:42 +0000309 try {
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000310 setDesktopName(name);
Pierre Ossmanb218ecd2017-11-16 16:43:13 +0100311 writeFramebufferUpdate();
Peter Ã…strandc39e0782009-01-15 12:21:42 +0000312 } catch(rdr::Exception& e) {
313 close(e.str());
314 }
315}
316
317
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000318void VNCSConnectionST::setCursorOrClose()
319{
320 try {
321 setCursor();
Pierre Ossmanb218ecd2017-11-16 16:43:13 +0100322 writeFramebufferUpdate();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000323 } catch(rdr::Exception& e) {
324 close(e.str());
325 }
326}
327
328
Pierre Ossmanb45a84f2016-12-12 16:59:15 +0100329void VNCSConnectionST::setLEDStateOrClose(unsigned int state)
330{
331 try {
332 setLEDState(state);
Pierre Ossmanb218ecd2017-11-16 16:43:13 +0100333 writeFramebufferUpdate();
Pierre Ossmanb45a84f2016-12-12 16:59:15 +0100334 } catch(rdr::Exception& e) {
335 close(e.str());
336 }
337}
338
339
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000340int VNCSConnectionST::checkIdleTimeout()
341{
342 int idleTimeout = rfb::Server::idleTimeout;
343 if (idleTimeout == 0) return 0;
344 if (state() != RFBSTATE_NORMAL && idleTimeout < 15)
345 idleTimeout = 15; // minimum of 15 seconds while authenticating
346 time_t now = time(0);
347 if (now < lastEventTime) {
348 // Someone must have set the time backwards. Set lastEventTime so that the
349 // idleTimeout will count from now.
350 vlog.info("Time has gone backwards - resetting idle timeout");
351 lastEventTime = now;
352 }
353 int timeLeft = lastEventTime + idleTimeout - now;
354 if (timeLeft < -60) {
355 // Our callback is over a minute late - someone must have set the time
356 // forwards. Set lastEventTime so that the idleTimeout will count from
357 // now.
358 vlog.info("Time has gone forwards - resetting idle timeout");
359 lastEventTime = now;
360 return secsToMillis(idleTimeout);
361 }
362 if (timeLeft <= 0) {
363 close("Idle timeout");
364 return 0;
365 }
366 return secsToMillis(timeLeft);
367}
368
Pierre Ossmanb114cec2011-11-20 15:36:11 +0000369
370bool VNCSConnectionST::getComparerState()
371{
372 // We interpret a low compression level as an indication that the client
373 // wants to prioritise CPU usage over bandwidth, and hence disable the
374 // comparing update tracker.
375 return (cp.compressLevel == -1) || (cp.compressLevel > 1);
376}
377
378
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000379// renderedCursorChange() is called whenever the server-side rendered cursor
380// changes shape or position. It ensures that the next update will clean up
381// the old rendered cursor and if necessary draw the new rendered cursor.
382
383void VNCSConnectionST::renderedCursorChange()
384{
385 if (state() != RFBSTATE_NORMAL) return;
Pierre Ossman71ca8d52017-09-15 11:03:12 +0200386 // Are we switching between client-side and server-side cursor?
Pierre Ossman25db44a2017-11-16 16:40:44 +0100387 if (clientHasCursor == needRenderedCursor())
Pierre Ossman71ca8d52017-09-15 11:03:12 +0200388 setCursorOrClose();
Pierre Ossman25db44a2017-11-16 16:40:44 +0100389 bool hasRenderedCursor = !damagedCursorRegion.is_empty();
Pierre Ossman330ca422017-11-06 13:15:55 +0100390 if (hasRenderedCursor)
Pierre Ossman5c9e1e52011-11-08 10:32:05 +0000391 removeRenderedCursor = true;
Pierre Ossman6b0bc292011-12-21 13:17:54 +0000392 if (needRenderedCursor()) {
Pierre Ossman671d2ef2015-06-09 16:09:06 +0200393 updateRenderedCursor = true;
Pierre Ossman6b0bc292011-12-21 13:17:54 +0000394 writeFramebufferUpdateOrClose();
395 }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000396}
397
398// needRenderedCursor() returns true if this client needs the server-side
399// rendered cursor. This may be because it does not support local cursor or
400// because the current cursor position has not been set by this client.
401// Unfortunately we can't know for sure when the current cursor position has
402// been set by this client. We guess that this is the case when the current
403// cursor position is the same as the last pointer event from this client, or
404// if it is a very short time since this client's last pointer event (up to a
405// second). [ Ideally we should do finer-grained timing here and make the time
406// configurable, but I don't think it's that important. ]
407
408bool VNCSConnectionST::needRenderedCursor()
409{
Pierre Ossman77ede0a2016-12-05 16:57:30 +0100410 if (state() != RFBSTATE_NORMAL)
411 return false;
412
Pierre Ossman324043e2017-08-16 16:26:11 +0200413 if (!cp.supportsLocalCursorWithAlpha &&
414 !cp.supportsLocalCursor && !cp.supportsLocalXCursor)
Pierre Ossman77ede0a2016-12-05 16:57:30 +0100415 return true;
416 if (!server->cursorPos.equals(pointerEventPos) &&
417 (time(0) - pointerEventTime) > 0)
418 return true;
419
420 return false;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000421}
422
423
424void VNCSConnectionST::approveConnectionOrClose(bool accept,
425 const char* reason)
426{
427 try {
428 approveConnection(accept, reason);
429 } catch (rdr::Exception& e) {
430 close(e.str());
431 }
432}
433
434
435
436// -=- Callbacks from SConnection
437
438void VNCSConnectionST::authSuccess()
439{
440 lastEventTime = time(0);
441
442 server->startDesktop();
443
444 // - Set the connection parameters appropriately
445 cp.width = server->pb->width();
446 cp.height = server->pb->height();
Pierre Ossman34e62f32009-03-20 21:46:12 +0000447 cp.screenLayout = server->screenLayout;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000448 cp.setName(server->getName());
Pierre Ossmanb45a84f2016-12-12 16:59:15 +0100449 cp.setLEDState(server->ledState);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000450
451 // - Set the default pixel format
452 cp.setPF(server->pb->getPF());
453 char buffer[256];
454 cp.pf().print(buffer, 256);
455 vlog.info("Server default pixel format %s", buffer);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000456
457 // - Mark the entire display as "dirty"
458 updates.add_changed(server->pb->getRect());
459 startTime = time(0);
Pierre Ossman1b478e52011-11-15 12:08:30 +0000460
461 // - Bootstrap the congestion control
462 ackedOffset = sock->outStream().length();
463 congWindow = INITIAL_WINDOW;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000464}
465
466void VNCSConnectionST::queryConnection(const char* userName)
467{
468 // - Authentication succeeded - clear from blacklist
469 CharArray name; name.buf = sock->getPeerAddress();
470 server->blHosts->clearBlackmark(name.buf);
471
472 // - Special case to provide a more useful error message
473 if (rfb::Server::neverShared && !rfb::Server::disconnectClients &&
474 server->authClientCount() > 0) {
475 approveConnection(false, "The server is already in use");
476 return;
477 }
478
479 // - Does the client have the right to bypass the query?
480 if (reverseConnection ||
481 !(rfb::Server::queryConnect || sock->requiresQuery()) ||
482 (accessRights & AccessNoQuery))
483 {
484 approveConnection(true);
485 return;
486 }
487
488 // - Get the server to display an Accept/Reject dialog, if required
489 // If a dialog is displayed, the result will be PENDING, and the
490 // server will call approveConnection at a later time
491 CharArray reason;
492 VNCServerST::queryResult qr = server->queryConnection(sock, userName,
493 &reason.buf);
Pierre Ossman36304752017-10-04 16:21:57 +0200494 if (qr == VNCServerST::PENDING)
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000495 return;
496
497 // - If server returns ACCEPT/REJECT then pass result to SConnection
498 approveConnection(qr == VNCServerST::ACCEPT, reason.buf);
499}
500
501void VNCSConnectionST::clientInit(bool shared)
502{
503 lastEventTime = time(0);
504 if (rfb::Server::alwaysShared || reverseConnection) shared = true;
Pierre Ossmane7be49b2014-12-02 14:33:17 +0100505 if (!(accessRights & AccessNonShared)) shared = true;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000506 if (rfb::Server::neverShared) shared = false;
507 if (!shared) {
Pierre Ossmane7be49b2014-12-02 14:33:17 +0100508 if (rfb::Server::disconnectClients && (accessRights & AccessNonShared)) {
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000509 // - Close all the other connected clients
510 vlog.debug("non-shared connection - closing clients");
511 server->closeClients("Non-shared connection requested", getSock());
512 } else {
513 // - Refuse this connection if there are existing clients, in addition to
514 // this one
515 if (server->authClientCount() > 1) {
516 close("Server is already in use");
517 return;
518 }
519 }
520 }
521 SConnection::clientInit(shared);
522}
523
524void VNCSConnectionST::setPixelFormat(const PixelFormat& pf)
525{
526 SConnection::setPixelFormat(pf);
527 char buffer[256];
528 pf.print(buffer, 256);
529 vlog.info("Client pixel format %s", buffer);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000530 setCursor();
531}
532
533void VNCSConnectionST::pointerEvent(const Point& pos, int buttonMask)
534{
535 pointerEventTime = lastEventTime = time(0);
536 server->lastUserInputTime = lastEventTime;
537 if (!(accessRights & AccessPtrEvents)) return;
538 if (!rfb::Server::acceptPointerEvents) return;
539 if (!server->pointerClient || server->pointerClient == this) {
540 pointerEventPos = pos;
541 if (buttonMask)
542 server->pointerClient = this;
543 else
544 server->pointerClient = 0;
545 server->desktop->pointerEvent(pointerEventPos, buttonMask);
546 }
547}
548
549
550class VNCSConnectionSTShiftPresser {
551public:
552 VNCSConnectionSTShiftPresser(SDesktop* desktop_)
553 : desktop(desktop_), pressed(false) {}
554 ~VNCSConnectionSTShiftPresser() {
Pierre Ossman9a153b02015-08-31 10:01:14 +0200555 if (pressed) {
556 vlog.debug("Releasing fake Shift_L");
Pierre Ossman5ae28212017-05-16 14:30:38 +0200557 desktop->keyEvent(XK_Shift_L, 0, false);
Pierre Ossman9a153b02015-08-31 10:01:14 +0200558 }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000559 }
560 void press() {
Pierre Ossman9a153b02015-08-31 10:01:14 +0200561 vlog.debug("Pressing fake Shift_L");
Pierre Ossman5ae28212017-05-16 14:30:38 +0200562 desktop->keyEvent(XK_Shift_L, 0, true);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000563 pressed = true;
564 }
565 SDesktop* desktop;
566 bool pressed;
567};
568
569// keyEvent() - record in the pressedKeys which keys were pressed. Allow
570// multiple down events (for autorepeat), but only allow a single up event.
Pierre Ossman5ae28212017-05-16 14:30:38 +0200571void VNCSConnectionST::keyEvent(rdr::U32 keysym, rdr::U32 keycode, bool down) {
Pierre Ossman16e1dcb2017-05-16 14:33:43 +0200572 rdr::U32 lookup;
573
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000574 lastEventTime = time(0);
575 server->lastUserInputTime = lastEventTime;
576 if (!(accessRights & AccessKeyEvents)) return;
577 if (!rfb::Server::acceptKeyEvents) return;
578
Pierre Ossman9a153b02015-08-31 10:01:14 +0200579 if (down)
Pierre Ossman5ae28212017-05-16 14:30:38 +0200580 vlog.debug("Key pressed: 0x%x / 0x%x", keysym, keycode);
Pierre Ossman9a153b02015-08-31 10:01:14 +0200581 else
Pierre Ossman5ae28212017-05-16 14:30:38 +0200582 vlog.debug("Key released: 0x%x / 0x%x", keysym, keycode);
Pierre Ossman9a153b02015-08-31 10:01:14 +0200583
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000584 // Remap the key if required
Pierre Ossman9a153b02015-08-31 10:01:14 +0200585 if (server->keyRemapper) {
586 rdr::U32 newkey;
Pierre Ossman5ae28212017-05-16 14:30:38 +0200587 newkey = server->keyRemapper->remapKey(keysym);
588 if (newkey != keysym) {
Pierre Ossman9a153b02015-08-31 10:01:14 +0200589 vlog.debug("Key remapped to 0x%x", newkey);
Pierre Ossman5ae28212017-05-16 14:30:38 +0200590 keysym = newkey;
Pierre Ossman9a153b02015-08-31 10:01:14 +0200591 }
592 }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000593
Pierre Ossmanbb305ca2016-12-11 12:41:26 +0100594 // Avoid lock keys if we don't know the server state
595 if ((server->ledState == ledUnknown) &&
Pierre Ossman5ae28212017-05-16 14:30:38 +0200596 ((keysym == XK_Caps_Lock) ||
597 (keysym == XK_Num_Lock) ||
598 (keysym == XK_Scroll_Lock))) {
Pierre Ossmanbb305ca2016-12-11 12:41:26 +0100599 vlog.debug("Ignoring lock key (e.g. caps lock)");
600 return;
601 }
Pierre Ossmanbb305ca2016-12-11 12:41:26 +0100602
Pierre Ossmanb45a84f2016-12-12 16:59:15 +0100603 // Lock key heuristics
604 // (only for clients that do not support the LED state extension)
605 if (!cp.supportsLEDState) {
606 // Always ignore ScrollLock as we don't have a heuristic
607 // for that
Pierre Ossman5ae28212017-05-16 14:30:38 +0200608 if (keysym == XK_Scroll_Lock) {
Pierre Ossmanb45a84f2016-12-12 16:59:15 +0100609 vlog.debug("Ignoring lock key (e.g. caps lock)");
610 return;
Pierre Ossmanbb305ca2016-12-11 12:41:26 +0100611 }
612
Pierre Ossmanb45a84f2016-12-12 16:59:15 +0100613 if (down && (server->ledState != ledUnknown)) {
614 // CapsLock synchronisation heuristic
615 // (this assumes standard interaction between CapsLock the Shift
616 // keys and normal characters)
Pierre Ossman5ae28212017-05-16 14:30:38 +0200617 if (((keysym >= XK_A) && (keysym <= XK_Z)) ||
618 ((keysym >= XK_a) && (keysym <= XK_z))) {
Pierre Ossmanb45a84f2016-12-12 16:59:15 +0100619 bool uppercase, shift, lock;
Pierre Ossmanbb305ca2016-12-11 12:41:26 +0100620
Pierre Ossman5ae28212017-05-16 14:30:38 +0200621 uppercase = (keysym >= XK_A) && (keysym <= XK_Z);
Pierre Ossman851e6802017-09-12 16:44:44 +0200622 shift = isShiftPressed();
Pierre Ossmanb45a84f2016-12-12 16:59:15 +0100623 lock = server->ledState & ledCapsLock;
Pierre Ossmanbb305ca2016-12-11 12:41:26 +0100624
Pierre Ossmanb45a84f2016-12-12 16:59:15 +0100625 if (lock == (uppercase == shift)) {
626 vlog.debug("Inserting fake CapsLock to get in sync with client");
Pierre Ossman5ae28212017-05-16 14:30:38 +0200627 server->desktop->keyEvent(XK_Caps_Lock, 0, true);
628 server->desktop->keyEvent(XK_Caps_Lock, 0, false);
Pierre Ossmanb45a84f2016-12-12 16:59:15 +0100629 }
630 }
631
632 // NumLock synchronisation heuristic
633 // (this is more cautious because of the differences between Unix,
634 // Windows and macOS)
Pierre Ossman5ae28212017-05-16 14:30:38 +0200635 if (((keysym >= XK_KP_Home) && (keysym <= XK_KP_Delete)) ||
636 ((keysym >= XK_KP_0) && (keysym <= XK_KP_9)) ||
637 (keysym == XK_KP_Separator) || (keysym == XK_KP_Decimal)) {
Pierre Ossmanb45a84f2016-12-12 16:59:15 +0100638 bool number, shift, lock;
639
Pierre Ossman5ae28212017-05-16 14:30:38 +0200640 number = ((keysym >= XK_KP_0) && (keysym <= XK_KP_9)) ||
641 (keysym == XK_KP_Separator) || (keysym == XK_KP_Decimal);
Pierre Ossman851e6802017-09-12 16:44:44 +0200642 shift = isShiftPressed();
Pierre Ossmanb45a84f2016-12-12 16:59:15 +0100643 lock = server->ledState & ledNumLock;
644
645 if (shift) {
646 // We don't know the appropriate NumLock state for when Shift
647 // is pressed as it could be one of:
648 //
649 // a) A Unix client where Shift negates NumLock
650 //
651 // b) A Windows client where Shift only cancels NumLock
652 //
653 // c) A macOS client where Shift doesn't have any effect
654 //
655 } else if (lock == (number == shift)) {
656 vlog.debug("Inserting fake NumLock to get in sync with client");
Pierre Ossman5ae28212017-05-16 14:30:38 +0200657 server->desktop->keyEvent(XK_Num_Lock, 0, true);
658 server->desktop->keyEvent(XK_Num_Lock, 0, false);
Pierre Ossmanb45a84f2016-12-12 16:59:15 +0100659 }
Pierre Ossmanbb305ca2016-12-11 12:41:26 +0100660 }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000661 }
662 }
663
664 // Turn ISO_Left_Tab into shifted Tab.
665 VNCSConnectionSTShiftPresser shiftPresser(server->desktop);
Pierre Ossman5ae28212017-05-16 14:30:38 +0200666 if (keysym == XK_ISO_Left_Tab) {
Pierre Ossman851e6802017-09-12 16:44:44 +0200667 if (!isShiftPressed())
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000668 shiftPresser.press();
Pierre Ossman5ae28212017-05-16 14:30:38 +0200669 keysym = XK_Tab;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000670 }
671
Pierre Ossman16e1dcb2017-05-16 14:33:43 +0200672 // We need to be able to track keys, so generate a fake index when we
673 // aren't given a keycode
674 if (keycode == 0)
675 lookup = 0x80000000 | keysym;
676 else
677 lookup = keycode;
678
679 // We force the same keysym for an already down key for the
680 // sake of sanity
681 if (pressedKeys.find(lookup) != pressedKeys.end())
682 keysym = pressedKeys[lookup];
683
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000684 if (down) {
Pierre Ossman16e1dcb2017-05-16 14:33:43 +0200685 pressedKeys[lookup] = keysym;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000686 } else {
Pierre Ossman16e1dcb2017-05-16 14:33:43 +0200687 if (!pressedKeys.erase(lookup))
Pierre Ossman5ae28212017-05-16 14:30:38 +0200688 return;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000689 }
Pierre Ossman16e1dcb2017-05-16 14:33:43 +0200690
Pierre Ossman5ae28212017-05-16 14:30:38 +0200691 server->desktop->keyEvent(keysym, keycode, down);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000692}
693
694void VNCSConnectionST::clientCutText(const char* str, int len)
695{
696 if (!(accessRights & AccessCutText)) return;
697 if (!rfb::Server::acceptCutText) return;
698 server->desktop->clientCutText(str, len);
699}
700
701void VNCSConnectionST::framebufferUpdateRequest(const Rect& r,bool incremental)
702{
Pierre Ossmane9962f72009-04-23 12:31:42 +0000703 Rect safeRect;
704
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000705 if (!(accessRights & AccessView)) return;
706
707 SConnection::framebufferUpdateRequest(r, incremental);
708
Pierre Ossmand9a59ba2009-03-20 15:55:37 +0000709 // Check that the client isn't sending crappy requests
710 if (!r.enclosed_by(Rect(0, 0, cp.width, cp.height))) {
711 vlog.error("FramebufferUpdateRequest %dx%d at %d,%d exceeds framebuffer %dx%d",
712 r.width(), r.height(), r.tl.x, r.tl.y, cp.width, cp.height);
Pierre Ossmane9962f72009-04-23 12:31:42 +0000713 safeRect = r.intersect(Rect(0, 0, cp.width, cp.height));
714 } else {
715 safeRect = r;
Pierre Ossmand9a59ba2009-03-20 15:55:37 +0000716 }
717
Constantin Kaplinsky2ef66952008-08-29 11:33:46 +0000718 // Just update the requested region.
719 // Framebuffer update will be sent a bit later, see processMessages().
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000720 Region reqRgn(r);
Pierre Ossman1b478e52011-11-15 12:08:30 +0000721 if (!incremental || !continuousUpdates)
722 requested.assign_union(reqRgn);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000723
724 if (!incremental) {
725 // Non-incremental update - treat as if area requested has changed
726 updates.add_changed(reqRgn);
Pierre Ossman53125a72009-04-22 15:37:51 +0000727
728 // And send the screen layout to the client (which, unlike the
729 // framebuffer dimensions, the client doesn't get during init)
Pierre Ossmanc5e25602009-03-20 12:59:05 +0000730 writer()->writeExtendedDesktopSize();
Pierre Ossman53125a72009-04-22 15:37:51 +0000731
732 // We do not send a DesktopSize since it only contains the
733 // framebuffer size (which the client already should know) and
734 // because some clients don't handle extra DesktopSize events
735 // very well.
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000736 }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000737}
738
Pierre Ossman34bb0612009-03-21 21:16:14 +0000739void VNCSConnectionST::setDesktopSize(int fb_width, int fb_height,
740 const ScreenSet& layout)
Pierre Ossmanc5e25602009-03-20 12:59:05 +0000741{
Pierre Ossman04e62db2009-03-23 16:57:07 +0000742 unsigned int result;
743
Michal Srbb318b8f2014-11-24 13:18:28 +0200744 if (!(accessRights & AccessSetDesktopSize)) return;
745 if (!rfb::Server::acceptSetDesktopSize) return;
746
Pierre Ossman04e62db2009-03-23 16:57:07 +0000747 // Don't bother the desktop with an invalid configuration
748 if (!layout.validate(fb_width, fb_height)) {
749 writer()->writeExtendedDesktopSize(reasonClient, resultInvalid,
750 fb_width, fb_height, layout);
Pierre Ossman04e62db2009-03-23 16:57:07 +0000751 return;
752 }
753
754 // FIXME: the desktop will call back to VNCServerST and an extra set
755 // of ExtendedDesktopSize messages will be sent. This is okay
756 // protocol-wise, but unnecessary.
757 result = server->desktop->setScreenLayout(fb_width, fb_height, layout);
758
Pierre Ossman04e62db2009-03-23 16:57:07 +0000759 writer()->writeExtendedDesktopSize(reasonClient, result,
760 fb_width, fb_height, layout);
Pierre Ossman04e62db2009-03-23 16:57:07 +0000761
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000762 // Only notify other clients on success
Pierre Ossman04e62db2009-03-23 16:57:07 +0000763 if (result == resultSuccess) {
764 if (server->screenLayout != layout)
765 throw Exception("Desktop configured a different screen layout than requested");
766 server->notifyScreenLayoutChange(this);
767 }
Pierre Ossmanc5e25602009-03-20 12:59:05 +0000768}
769
Pierre Ossman2c764942011-11-14 15:54:30 +0000770void VNCSConnectionST::fence(rdr::U32 flags, unsigned len, const char data[])
771{
772 if (flags & fenceFlagRequest) {
773 if (flags & fenceFlagSyncNext) {
Pierre Ossmanb8b1e962012-07-20 10:47:00 +0000774 pendingSyncFence = true;
Pierre Ossman2c764942011-11-14 15:54:30 +0000775
776 fenceFlags = flags & (fenceFlagBlockBefore | fenceFlagBlockAfter | fenceFlagSyncNext);
777 fenceDataLen = len;
778 delete [] fenceData;
Michal Srbf3afa242017-03-27 19:02:15 +0300779 fenceData = NULL;
Pierre Ossman2c764942011-11-14 15:54:30 +0000780 if (len > 0) {
781 fenceData = new char[len];
782 memcpy(fenceData, data, len);
783 }
784
785 return;
786 }
787
788 // We handle everything synchronously so we trivially honor these modes
789 flags = flags & (fenceFlagBlockBefore | fenceFlagBlockAfter);
790
791 writer()->writeFence(flags, len, data);
792 return;
793 }
794
Pierre Ossman1b478e52011-11-15 12:08:30 +0000795 struct RTTInfo rttInfo;
796
Pierre Ossman2c764942011-11-14 15:54:30 +0000797 switch (len) {
798 case 0:
799 // Initial dummy fence;
800 break;
Pierre Ossman1b478e52011-11-15 12:08:30 +0000801 case sizeof(struct RTTInfo):
802 memcpy(&rttInfo, data, sizeof(struct RTTInfo));
803 handleRTTPong(rttInfo);
804 break;
Pierre Ossman2c764942011-11-14 15:54:30 +0000805 default:
806 vlog.error("Fence response of unexpected size received");
807 }
808}
809
Pierre Ossman1b478e52011-11-15 12:08:30 +0000810void VNCSConnectionST::enableContinuousUpdates(bool enable,
811 int x, int y, int w, int h)
812{
813 Rect rect;
814
815 if (!cp.supportsFence || !cp.supportsContinuousUpdates)
816 throw Exception("Client tried to enable continuous updates when not allowed");
817
818 continuousUpdates = enable;
819
820 rect.setXYWH(x, y, w, h);
821 cuRegion.reset(rect);
822
823 if (enable) {
824 requested.clear();
Pierre Ossman1b478e52011-11-15 12:08:30 +0000825 } else {
826 writer()->writeEndOfContinuousUpdates();
827 }
828}
829
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000830// supportsLocalCursor() is called whenever the status of
831// cp.supportsLocalCursor has changed. If the client does now support local
832// cursor, we make sure that the old server-side rendered cursor is cleaned up
833// and the cursor is sent to the client.
834
835void VNCSConnectionST::supportsLocalCursor()
836{
Pierre Ossman324043e2017-08-16 16:26:11 +0200837 if (cp.supportsLocalCursorWithAlpha ||
838 cp.supportsLocalCursor || cp.supportsLocalXCursor) {
Pierre Ossman671d2ef2015-06-09 16:09:06 +0200839 if (!damagedCursorRegion.is_empty())
Pierre Ossman5c9e1e52011-11-08 10:32:05 +0000840 removeRenderedCursor = true;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000841 setCursor();
842 }
843}
844
Pierre Ossman2c764942011-11-14 15:54:30 +0000845void VNCSConnectionST::supportsFence()
846{
847 writer()->writeFence(fenceFlagRequest, 0, NULL);
848}
849
Pierre Ossman1b478e52011-11-15 12:08:30 +0000850void VNCSConnectionST::supportsContinuousUpdates()
851{
852 // We refuse to use continuous updates if we cannot monitor the buffer
853 // usage using fences.
854 if (!cp.supportsFence)
855 return;
856
857 writer()->writeEndOfContinuousUpdates();
858}
859
Pierre Ossmanb45a84f2016-12-12 16:59:15 +0100860void VNCSConnectionST::supportsLEDState()
861{
862 writer()->writeLEDState();
863}
864
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000865
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +0000866bool VNCSConnectionST::handleTimeout(Timer* t)
867{
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000868 try {
Pierre Ossmana40ab202016-04-29 15:35:56 +0200869 if (t == &congestionTimer)
Pierre Ossman1b478e52011-11-15 12:08:30 +0000870 updateCongestion();
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000871 } catch (rdr::Exception& e) {
872 close(e.str());
873 }
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +0000874
875 return false;
876}
877
Pierre Ossman851e6802017-09-12 16:44:44 +0200878bool VNCSConnectionST::isShiftPressed()
879{
880 std::map<rdr::U32, rdr::U32>::const_iterator iter;
881
882 for (iter = pressedKeys.begin(); iter != pressedKeys.end(); ++iter) {
883 if (iter->second == XK_Shift_L)
884 return true;
885 if (iter->second == XK_Shift_R)
886 return true;
887 }
888
889 return false;
890}
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +0000891
Pierre Ossman1b478e52011-11-15 12:08:30 +0000892void VNCSConnectionST::writeRTTPing()
893{
894 struct RTTInfo rttInfo;
895
896 if (!cp.supportsFence)
897 return;
898
899 memset(&rttInfo, 0, sizeof(struct RTTInfo));
900
901 gettimeofday(&rttInfo.tv, NULL);
902 rttInfo.offset = sock->outStream().length();
903 rttInfo.inFlight = rttInfo.offset - ackedOffset;
904
905 // We need to make sure any old update are already processed by the
906 // time we get the response back. This allows us to reliably throttle
907 // back on client overload, as well as network overload.
908 writer()->writeFence(fenceFlagRequest | fenceFlagBlockBefore,
909 sizeof(struct RTTInfo), (const char*)&rttInfo);
910
911 pingCounter++;
912
913 sentOffset = rttInfo.offset;
914
915 // Let some data flow before we adjust the settings
916 if (!congestionTimer.isStarted())
917 congestionTimer.start(__rfbmin(baseRTT * 2, 100));
918}
919
920void VNCSConnectionST::handleRTTPong(const struct RTTInfo &rttInfo)
921{
922 unsigned rtt, delay;
Pierre Ossman1b478e52011-11-15 12:08:30 +0000923
924 pingCounter--;
925
926 rtt = msSince(&rttInfo.tv);
927 if (rtt < 1)
928 rtt = 1;
929
930 ackedOffset = rttInfo.offset;
931
932 // Try to estimate wire latency by tracking lowest seen latency
933 if (rtt < baseRTT)
934 baseRTT = rtt;
935
936 if (rttInfo.inFlight > congWindow) {
937 seenCongestion = true;
938
939 // Estimate added delay because of overtaxed buffers
940 delay = (rttInfo.inFlight - congWindow) * baseRTT / congWindow;
941
942 if (delay < rtt)
943 rtt -= delay;
944 else
945 rtt = 1;
946
947 // If we underestimate the congestion window, then we'll get a latency
948 // that's less than the wire latency, which will confuse other portions
949 // of the code.
950 if (rtt < baseRTT)
951 rtt = baseRTT;
952 }
953
954 // We only keep track of the minimum latency seen (for a given interval)
klemens0536d092017-01-28 20:56:56 +0100955 // on the basis that we want to avoid continuous buffer issue, but don't
Pierre Ossman1b478e52011-11-15 12:08:30 +0000956 // mind (or even approve of) bursts.
957 if (rtt < minRTT)
958 minRTT = rtt;
959}
960
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +0000961bool VNCSConnectionST::isCongested()
962{
Pierre Ossman1b478e52011-11-15 12:08:30 +0000963 int offset;
964
965 // Stuff still waiting in the send buffer?
Pierre Ossman352d0622016-04-29 15:50:54 +0200966 sock->outStream().flush();
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +0000967 if (sock->outStream().bufferUsage() > 0)
968 return true;
969
Pierre Ossman1b478e52011-11-15 12:08:30 +0000970 if (!cp.supportsFence)
971 return false;
972
973 // Idle for too long? (and no data on the wire)
974 //
975 // FIXME: This should really just be one baseRTT, but we're getting
976 // problems with triggering the idle timeout on each update.
977 // Maybe we need to use a moving average for the wire latency
978 // instead of baseRTT.
979 if ((sentOffset == ackedOffset) &&
980 (sock->outStream().getIdleTime() > 2 * baseRTT)) {
981
982#ifdef CONGESTION_DEBUG
983 if (congWindow > INITIAL_WINDOW)
984 fprintf(stderr, "Reverting to initial window (%d KiB) after %d ms\n",
985 INITIAL_WINDOW / 1024, sock->outStream().getIdleTime());
986#endif
987
988 // Close congestion window and allow a transfer
989 // FIXME: Reset baseRTT like Linux Vegas?
990 congWindow = __rfbmin(INITIAL_WINDOW, congWindow);
991
992 return false;
993 }
994
995 offset = sock->outStream().length();
996
997 // FIXME: Should we compensate for non-update data?
998 // (i.e. use sentOffset instead of offset)
999 if ((offset - ackedOffset) < congWindow)
1000 return false;
1001
1002 // If we just have one outstanding "ping", that means the client has
1003 // started receiving our update. In order to not regress compared to
1004 // before we had congestion avoidance, we allow another update here.
1005 // This could further clog up the tubes, but congestion control isn't
1006 // really working properly right now anyway as the wire would otherwise
1007 // be idle for at least RTT/2.
1008 if (pingCounter == 1)
1009 return false;
1010
1011 return true;
1012}
1013
1014
1015void VNCSConnectionST::updateCongestion()
1016{
1017 unsigned diff;
1018
1019 if (!seenCongestion)
1020 return;
1021
1022 diff = minRTT - baseRTT;
1023
1024 if (diff > __rfbmin(100, baseRTT)) {
1025 // Way too fast
1026 congWindow = congWindow * baseRTT / minRTT;
1027 } else if (diff > __rfbmin(50, baseRTT/2)) {
1028 // Slightly too fast
1029 congWindow -= 4096;
1030 } else if (diff < 5) {
1031 // Way too slow
1032 congWindow += 8192;
1033 } else if (diff < 25) {
1034 // Too slow
1035 congWindow += 4096;
1036 }
1037
1038 if (congWindow < MINIMUM_WINDOW)
1039 congWindow = MINIMUM_WINDOW;
1040 if (congWindow > MAXIMUM_WINDOW)
1041 congWindow = MAXIMUM_WINDOW;
1042
1043#ifdef CONGESTION_DEBUG
1044 fprintf(stderr, "RTT: %d ms (%d ms), Window: %d KiB, Bandwidth: %g Mbps\n",
1045 minRTT, baseRTT, congWindow / 1024,
1046 congWindow * 8.0 / baseRTT / 1000.0);
1047
1048#ifdef TCP_INFO
1049 struct tcp_info tcp_info;
1050 socklen_t tcp_info_length;
1051
1052 tcp_info_length = sizeof(tcp_info);
1053 if (getsockopt(sock->getFd(), SOL_TCP, TCP_INFO,
1054 (void *)&tcp_info, &tcp_info_length) == 0) {
1055 fprintf(stderr, "Socket: RTT: %d ms (+/- %d ms) Window %d KiB\n",
1056 tcp_info.tcpi_rtt / 1000, tcp_info.tcpi_rttvar / 1000,
1057 tcp_info.tcpi_snd_mss * tcp_info.tcpi_snd_cwnd / 1024);
1058 }
1059#endif
1060
1061#endif
1062
1063 minRTT = -1;
1064 seenCongestion = false;
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +00001065}
1066
1067
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001068void VNCSConnectionST::writeFramebufferUpdate()
1069{
Pierre Ossman2c764942011-11-14 15:54:30 +00001070 // We're in the middle of processing a command that's supposed to be
1071 // synchronised. Allowing an update to slip out right now might violate
1072 // that synchronisation.
1073 if (syncFence)
1074 return;
1075
Pierre Ossmana3ac01e2011-11-07 21:13:54 +00001076 // We try to aggregate responses, so don't send out anything whilst we
1077 // still have incoming messages. processMessages() will give us another
1078 // chance to run once things are idle.
1079 if (inProcessMessages)
1080 return;
1081
Pierre Ossman1b478e52011-11-15 12:08:30 +00001082 if (state() != RFBSTATE_NORMAL)
1083 return;
1084 if (requested.is_empty() && !continuousUpdates)
Pierre Ossmane9962f72009-04-23 12:31:42 +00001085 return;
Pierre Ossmand9a59ba2009-03-20 15:55:37 +00001086
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +00001087 // Check that we actually have some space on the link and retry in a
1088 // bit if things are congested.
Pierre Ossmana40ab202016-04-29 15:35:56 +02001089 if (isCongested())
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +00001090 return;
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +00001091
Pierre Ossmancef3cf72016-11-25 10:06:34 +01001092 // Updates often consists of many small writes, and in continuous
1093 // mode, we will also have small fence messages around the update. We
1094 // need to aggregate these in order to not clog up TCP's congestion
1095 // window.
Peter Ã…strand (astrand)01dc1a62017-10-10 12:56:04 +02001096 sock->cork(true);
Pierre Ossman36dadf82011-11-15 12:11:32 +00001097
Pierre Ossmane9962f72009-04-23 12:31:42 +00001098 // First take care of any updates that cannot contain framebuffer data
1099 // changes.
Pierre Ossmancef3cf72016-11-25 10:06:34 +01001100 writeNoDataUpdate();
1101
1102 // Then real data (if possible)
1103 writeDataUpdate();
1104
Peter Ã…strand (astrand)01dc1a62017-10-10 12:56:04 +02001105 sock->cork(false);
Pierre Ossmancef3cf72016-11-25 10:06:34 +01001106}
1107
1108void VNCSConnectionST::writeNoDataUpdate()
1109{
1110 if (!writer()->needNoDataUpdate())
1111 return;
1112
1113 writer()->writeNoDataUpdate();
1114
1115 // Make sure no data update is sent until next request
1116 requested.clear();
1117}
1118
1119void VNCSConnectionST::writeDataUpdate()
1120{
1121 Region req;
1122 UpdateInfo ui;
1123 bool needNewUpdateInfo;
Pierre Ossman24684e52016-12-05 16:58:19 +01001124 const RenderedCursor *cursor;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001125
Constantin Kaplinsky604d7812007-08-31 15:50:37 +00001126 updates.enable_copyrect(cp.useCopyRect);
1127
Pierre Ossman6e49e952016-10-07 15:59:38 +02001128 // See if we are allowed to send anything right now (the framebuffer
1129 // might have changed in ways we haven't yet been informed of).
Pierre Ossmanbbf955e2011-11-08 12:44:10 +00001130 if (!server->checkUpdate())
Pierre Ossmancef3cf72016-11-25 10:06:34 +01001131 return;
Constantin Kaplinskya09dc142008-12-18 12:08:15 +00001132
Pierre Ossmancef3cf72016-11-25 10:06:34 +01001133 // See what the client has requested (if anything)
Pierre Ossman1b478e52011-11-15 12:08:30 +00001134 if (continuousUpdates)
1135 req = cuRegion.union_(requested);
1136 else
1137 req = requested;
1138
Pierre Ossmancef3cf72016-11-25 10:06:34 +01001139 if (req.is_empty())
1140 return;
1141
1142 // Get the lists of updates. Prior to exporting the data to the `ui' object,
1143 // getUpdateInfo() will normalize the `updates' object such way that its
1144 // `changed' and `copied' regions would not intersect.
Pierre Ossman1b478e52011-11-15 12:08:30 +00001145 updates.getUpdateInfo(&ui, req);
1146 needNewUpdateInfo = false;
Constantin Kaplinsky604d7812007-08-31 15:50:37 +00001147
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001148 // If the previous position of the rendered cursor overlaps the source of the
1149 // copy, then when the copy happens the corresponding rectangle in the
1150 // destination will be wrong, so add it to the changed region.
1151
Pierre Ossman671d2ef2015-06-09 16:09:06 +02001152 if (!ui.copied.is_empty() && !damagedCursorRegion.is_empty()) {
1153 Region bogusCopiedCursor;
1154
1155 bogusCopiedCursor.copyFrom(damagedCursorRegion);
1156 bogusCopiedCursor.translate(ui.copy_delta);
1157 bogusCopiedCursor.assign_intersect(server->pb->getRect());
Constantin Kaplinsky45517c82007-08-31 15:56:33 +00001158 if (!ui.copied.intersect(bogusCopiedCursor).is_empty()) {
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001159 updates.add_changed(bogusCopiedCursor);
Constantin Kaplinsky604d7812007-08-31 15:50:37 +00001160 needNewUpdateInfo = true;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001161 }
1162 }
1163
Pierre Ossman671d2ef2015-06-09 16:09:06 +02001164 // If we need to remove the old rendered cursor, just add the region to
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001165 // the changed region.
1166
1167 if (removeRenderedCursor) {
Pierre Ossman671d2ef2015-06-09 16:09:06 +02001168 updates.add_changed(damagedCursorRegion);
Constantin Kaplinsky604d7812007-08-31 15:50:37 +00001169 needNewUpdateInfo = true;
Pierre Ossman671d2ef2015-06-09 16:09:06 +02001170 damagedCursorRegion.clear();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001171 removeRenderedCursor = false;
1172 }
1173
1174 // Return if there is nothing to send the client.
1175
Pierre Ossman671d2ef2015-06-09 16:09:06 +02001176 if (updates.is_empty() && !writer()->needFakeUpdate() && !updateRenderedCursor)
Pierre Ossmancef3cf72016-11-25 10:06:34 +01001177 return;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001178
Constantin Kaplinsky604d7812007-08-31 15:50:37 +00001179 // The `updates' object could change, make sure we have valid update info.
1180
1181 if (needNewUpdateInfo)
Pierre Ossman1b478e52011-11-15 12:08:30 +00001182 updates.getUpdateInfo(&ui, req);
Constantin Kaplinsky604d7812007-08-31 15:50:37 +00001183
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001184 // If the client needs a server-side rendered cursor, work out the cursor
1185 // rectangle. If it's empty then don't bother drawing it, but if it overlaps
1186 // with the update region, we need to draw the rendered cursor regardless of
1187 // whether it has changed.
1188
Pierre Ossman24684e52016-12-05 16:58:19 +01001189 cursor = NULL;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001190 if (needRenderedCursor()) {
Pierre Ossman671d2ef2015-06-09 16:09:06 +02001191 Rect renderedCursorRect;
1192
Pierre Ossman24684e52016-12-05 16:58:19 +01001193 cursor = server->getRenderedCursor();
1194
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001195 renderedCursorRect
Pierre Ossman24684e52016-12-05 16:58:19 +01001196 = cursor->getEffectiveRect().intersect(req.get_bounding_rect());
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001197
1198 if (renderedCursorRect.is_empty()) {
Pierre Ossman24684e52016-12-05 16:58:19 +01001199 cursor = NULL;
1200 } else if (!updateRenderedCursor &&
1201 ui.changed.union_(ui.copied)
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001202 .intersect(renderedCursorRect).is_empty()) {
Pierre Ossman24684e52016-12-05 16:58:19 +01001203 cursor = NULL;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001204 }
1205
Pierre Ossman5c037202016-12-05 17:00:35 +01001206 if (cursor) {
1207 updates.subtract(renderedCursorRect);
1208 updates.getUpdateInfo(&ui, req);
1209 }
Pierre Ossman671d2ef2015-06-09 16:09:06 +02001210
1211 damagedCursorRegion.assign_union(renderedCursorRect);
1212 updateRenderedCursor = false;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001213 }
1214
Pierre Ossman24684e52016-12-05 16:58:19 +01001215 if (ui.is_empty() && !writer()->needFakeUpdate() && !cursor)
Pierre Ossmancef3cf72016-11-25 10:06:34 +01001216 return;
Pierre Ossmanfdba3fe2014-01-31 13:12:18 +01001217
Pierre Ossmancef3cf72016-11-25 10:06:34 +01001218 writeRTTPing();
Pierre Ossman1b478e52011-11-15 12:08:30 +00001219
Pierre Ossmancef3cf72016-11-25 10:06:34 +01001220 encodeManager.writeUpdate(ui, server->getPixelBuffer(), cursor);
Pierre Ossman1b478e52011-11-15 12:08:30 +00001221
Pierre Ossmancef3cf72016-11-25 10:06:34 +01001222 writeRTTPing();
Pierre Ossmanc0397262014-03-14 15:59:46 +01001223
Pierre Ossmancef3cf72016-11-25 10:06:34 +01001224 // The request might be for just part of the screen, so we cannot
1225 // just clear the entire update tracker.
1226 updates.subtract(req);
Pierre Ossmanc0397262014-03-14 15:59:46 +01001227
Pierre Ossmancef3cf72016-11-25 10:06:34 +01001228 requested.clear();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001229}
1230
1231
Pierre Ossmana3ac01e2011-11-07 21:13:54 +00001232void VNCSConnectionST::screenLayoutChange(rdr::U16 reason)
1233{
1234 if (!authenticated())
1235 return;
1236
1237 cp.screenLayout = server->screenLayout;
1238
1239 if (state() != RFBSTATE_NORMAL)
1240 return;
1241
1242 writer()->writeExtendedDesktopSize(reason, 0, cp.width, cp.height,
1243 cp.screenLayout);
Pierre Ossmana3ac01e2011-11-07 21:13:54 +00001244}
1245
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001246
1247// setCursor() is called whenever the cursor has changed shape or pixel format.
1248// If the client supports local cursor then it will arrange for the cursor to
1249// be sent to the client.
1250
1251void VNCSConnectionST::setCursor()
1252{
Pierre Ossmana3ac01e2011-11-07 21:13:54 +00001253 if (state() != RFBSTATE_NORMAL)
1254 return;
Pierre Ossmana3ac01e2011-11-07 21:13:54 +00001255
Pierre Ossman71ca8d52017-09-15 11:03:12 +02001256 // We need to blank out the client's cursor or there will be two
Pierre Ossman25db44a2017-11-16 16:40:44 +01001257 if (needRenderedCursor()) {
Pierre Ossman71ca8d52017-09-15 11:03:12 +02001258 cp.setCursor(emptyCursor);
Pierre Ossman25db44a2017-11-16 16:40:44 +01001259 clientHasCursor = false;
1260 } else {
Pierre Ossman71ca8d52017-09-15 11:03:12 +02001261 cp.setCursor(*server->cursor);
Pierre Ossman25db44a2017-11-16 16:40:44 +01001262 clientHasCursor = true;
1263 }
Pierre Ossman126e5642014-02-13 14:40:25 +01001264
Pierre Ossman8053c8e2017-02-21 12:59:04 +01001265 if (!writer()->writeSetCursorWithAlpha()) {
1266 if (!writer()->writeSetCursor()) {
1267 if (!writer()->writeSetXCursor()) {
1268 // No client support
1269 return;
1270 }
Pierre Ossman126e5642014-02-13 14:40:25 +01001271 }
1272 }
Pierre Ossmana3ac01e2011-11-07 21:13:54 +00001273}
1274
1275void VNCSConnectionST::setDesktopName(const char *name)
1276{
1277 cp.setName(name);
1278
1279 if (state() != RFBSTATE_NORMAL)
1280 return;
1281
1282 if (!writer()->writeSetDesktopName()) {
1283 fprintf(stderr, "Client does not support desktop rename\n");
1284 return;
1285 }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001286}
1287
Pierre Ossmanb45a84f2016-12-12 16:59:15 +01001288void VNCSConnectionST::setLEDState(unsigned int ledstate)
1289{
1290 if (state() != RFBSTATE_NORMAL)
1291 return;
1292
1293 cp.setLEDState(ledstate);
1294
Pierre Ossmanb218ecd2017-11-16 16:43:13 +01001295 writer()->writeLEDState();
Pierre Ossmanb45a84f2016-12-12 16:59:15 +01001296}
1297
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001298void VNCSConnectionST::setSocketTimeouts()
1299{
1300 int timeoutms = rfb::Server::clientWaitTimeMillis;
1301 soonestTimeout(&timeoutms, secsToMillis(rfb::Server::idleTimeout));
1302 if (timeoutms == 0)
1303 timeoutms = -1;
1304 sock->inStream().setTimeout(timeoutms);
1305 sock->outStream().setTimeout(timeoutms);
1306}
1307
1308char* VNCSConnectionST::getStartTime()
1309{
1310 char* result = ctime(&startTime);
1311 result[24] = '\0';
1312 return result;
1313}
1314
1315void VNCSConnectionST::setStatus(int status)
1316{
1317 switch (status) {
1318 case 0:
1319 accessRights = accessRights | AccessPtrEvents | AccessKeyEvents | AccessView;
1320 break;
1321 case 1:
Pierre Ossman7728be22015-03-03 16:39:37 +01001322 accessRights = (accessRights & ~(AccessPtrEvents | AccessKeyEvents)) | AccessView;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001323 break;
1324 case 2:
Adam Tkac8e985062011-02-07 11:33:57 +00001325 accessRights = accessRights & ~(AccessPtrEvents | AccessKeyEvents | AccessView);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001326 break;
1327 }
1328 framebufferUpdateRequest(server->pb->getRect(), false);
1329}
1330int VNCSConnectionST::getStatus()
1331{
1332 if ((accessRights & (AccessPtrEvents | AccessKeyEvents | AccessView)) == 0x0007)
1333 return 0;
1334 if ((accessRights & (AccessPtrEvents | AccessKeyEvents | AccessView)) == 0x0001)
1335 return 1;
1336 if ((accessRights & (AccessPtrEvents | AccessKeyEvents | AccessView)) == 0x0000)
1337 return 2;
1338 return 4;
1339}
1340