blob: be496e73cf9e0d51a090612a08d66c6f79646b30 [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
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000069VNCSConnectionST::VNCSConnectionST(VNCServerST* server_, network::Socket *s,
70 bool reverse)
Pierre Ossman7069bdd2015-02-06 14:41:58 +010071 : sock(s), reverseConnection(reverse),
Pierre Ossmanf8e3b342015-01-26 14:37:04 +010072 queryConnectTimer(this), inProcessMessages(false),
Pierre Ossmanb8b1e962012-07-20 10:47:00 +000073 pendingSyncFence(false), syncFence(false), fenceFlags(0),
74 fenceDataLen(0), fenceData(NULL),
Pierre Ossmanb1cd6ca2015-03-03 16:37:43 +010075 baseRTT(-1), congWindow(0), ackedOffset(0), sentOffset(0),
76 minRTT(-1), seenCongestion(false),
77 pingCounter(0), congestionTimer(this),
Pierre Ossman0c9bd4b2014-07-09 16:44:11 +020078 server(server_), updates(false),
Pierre Ossman671d2ef2015-06-09 16:09:06 +020079 updateRenderedCursor(false), removeRenderedCursor(false),
Pierre Ossmana40ab202016-04-29 15:35:56 +020080 continuousUpdates(false), encodeManager(this), pointerEventTime(0),
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +000081 accessRights(AccessDefault), startTime(time(0))
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000082{
83 setStreams(&sock->inStream(), &sock->outStream());
84 peerEndpoint.buf = sock->getPeerEndpoint();
85 VNCServerST::connectionsLog.write(1,"accepted: %s", peerEndpoint.buf);
86
87 // Configure the socket
88 setSocketTimeouts();
89 lastEventTime = time(0);
90
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000091 server->clients.push_front(this);
92}
93
94
95VNCSConnectionST::~VNCSConnectionST()
96{
97 // If we reach here then VNCServerST is deleting us!
98 VNCServerST::connectionsLog.write(1,"closed: %s (%s)",
99 peerEndpoint.buf,
100 (closeReason.buf) ? closeReason.buf : "");
101
102 // Release any keys the client still had pressed
103 std::set<rdr::U32>::iterator i;
Pierre Ossman9a153b02015-08-31 10:01:14 +0200104 for (i=pressedKeys.begin(); i!=pressedKeys.end(); i++) {
105 vlog.debug("Releasing key 0x%x on client disconnect", *i);
Pierre Ossman5ae28212017-05-16 14:30:38 +0200106 server->desktop->keyEvent(*i, 0, false);
Pierre Ossman9a153b02015-08-31 10:01:14 +0200107 }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000108 if (server->pointerClient == this)
109 server->pointerClient = 0;
110
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000111 // Remove this client from the server
112 server->clients.remove(this);
113
Pierre Ossman2c764942011-11-14 15:54:30 +0000114 delete [] fenceData;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000115}
116
117
118// Methods called from VNCServerST
119
120bool VNCSConnectionST::init()
121{
122 try {
123 initialiseProtocol();
124 } catch (rdr::Exception& e) {
125 close(e.str());
126 return false;
127 }
128 return true;
129}
130
131void VNCSConnectionST::close(const char* reason)
132{
133 // Log the reason for the close
134 if (!closeReason.buf)
Adam Tkacd36b6262009-09-04 10:57:20 +0000135 closeReason.buf = strDup(reason);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000136 else
137 vlog.debug("second close: %s (%s)", peerEndpoint.buf, reason);
138
139 if (authenticated()) {
140 server->lastDisconnectTime = time(0);
141 }
142
143 // Just shutdown the socket and mark our state as closing. Eventually the
144 // calling code will call VNCServerST's removeSocket() method causing us to
145 // be deleted.
146 sock->shutdown();
147 setState(RFBSTATE_CLOSING);
148}
149
150
151void VNCSConnectionST::processMessages()
152{
153 if (state() == RFBSTATE_CLOSING) return;
154 try {
155 // - Now set appropriate socket timeouts and process data
156 setSocketTimeouts();
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000157
158 inProcessMessages = true;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000159
Pierre Ossmana830bec2011-11-08 12:12:02 +0000160 // Get the underlying TCP layer to build large packets if we send
161 // multiple small responses.
162 network::TcpSocket::cork(sock->getFd(), true);
163
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000164 while (getInStream()->checkNoWait(1)) {
Pierre Ossmanb8b1e962012-07-20 10:47:00 +0000165 if (pendingSyncFence) {
166 syncFence = true;
167 pendingSyncFence = false;
168 }
169
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000170 processMsg();
Pierre Ossmanb8b1e962012-07-20 10:47:00 +0000171
Pierre Ossman2c764942011-11-14 15:54:30 +0000172 if (syncFence) {
173 writer()->writeFence(fenceFlags, fenceDataLen, fenceData);
174 syncFence = false;
175 }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000176 }
177
Pierre Ossmana830bec2011-11-08 12:12:02 +0000178 // Flush out everything in case we go idle after this.
179 network::TcpSocket::cork(sock->getFd(), false);
180
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000181 inProcessMessages = false;
Constantin Kaplinsky2ef66952008-08-29 11:33:46 +0000182
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000183 // If there were anything requiring an update, try to send it here.
184 // We wait until now with this to aggregate responses and to give
185 // higher priority to user actions such as keyboard and pointer events.
186 writeFramebufferUpdate();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000187 } catch (rdr::EndOfStream&) {
188 close("Clean disconnection");
189 } catch (rdr::Exception &e) {
190 close(e.str());
191 }
192}
193
Pierre Ossmand408ca52016-04-29 14:26:05 +0200194void VNCSConnectionST::flushSocket()
195{
196 if (state() == RFBSTATE_CLOSING) return;
197 try {
198 setSocketTimeouts();
199 sock->outStream().flush();
Pierre Ossmana40ab202016-04-29 15:35:56 +0200200 // Flushing the socket might release an update that was previously
201 // delayed because of congestion.
202 if (sock->outStream().bufferUsage() == 0)
203 writeFramebufferUpdate();
Pierre Ossmand408ca52016-04-29 14:26:05 +0200204 } catch (rdr::Exception &e) {
205 close(e.str());
206 }
207}
208
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000209void VNCSConnectionST::pixelBufferChange()
210{
211 try {
212 if (!authenticated()) return;
213 if (cp.width && cp.height && (server->pb->width() != cp.width ||
214 server->pb->height() != cp.height))
215 {
216 // We need to clip the next update to the new size, but also add any
217 // extra bits if it's bigger. If we wanted to do this exactly, something
218 // like the code below would do it, but at the moment we just update the
Pierre Ossman671d2ef2015-06-09 16:09:06 +0200219 // entire new size. However, we do need to clip the damagedCursorRegion
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000220 // because that might be added to updates in writeFramebufferUpdate().
221
222 //updates.intersect(server->pb->getRect());
223 //
224 //if (server->pb->width() > cp.width)
225 // updates.add_changed(Rect(cp.width, 0, server->pb->width(),
226 // server->pb->height()));
227 //if (server->pb->height() > cp.height)
228 // updates.add_changed(Rect(0, cp.height, cp.width,
229 // server->pb->height()));
230
Pierre Ossman671d2ef2015-06-09 16:09:06 +0200231 damagedCursorRegion.assign_intersect(server->pb->getRect());
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000232
233 cp.width = server->pb->width();
234 cp.height = server->pb->height();
Pierre Ossman34e62f32009-03-20 21:46:12 +0000235 cp.screenLayout = server->screenLayout;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000236 if (state() == RFBSTATE_NORMAL) {
Pierre Ossman2ee430a2009-05-28 12:54:24 +0000237 // We should only send EDS to client asking for both
238 if (!writer()->writeExtendedDesktopSize()) {
239 if (!writer()->writeSetDesktopSize()) {
240 close("Client does not support desktop resize");
241 return;
242 }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000243 }
244 }
245 }
246 // Just update the whole screen at the moment because we're too lazy to
247 // work out what's actually changed.
248 updates.clear();
249 updates.add_changed(server->pb->getRect());
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000250 writeFramebufferUpdate();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000251 } catch(rdr::Exception &e) {
252 close(e.str());
253 }
254}
255
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000256void VNCSConnectionST::writeFramebufferUpdateOrClose()
Pierre Ossman04e62db2009-03-23 16:57:07 +0000257{
258 try {
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000259 writeFramebufferUpdate();
260 } catch(rdr::Exception &e) {
261 close(e.str());
262 }
263}
Pierre Ossman04e62db2009-03-23 16:57:07 +0000264
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000265void VNCSConnectionST::screenLayoutChangeOrClose(rdr::U16 reason)
266{
267 try {
268 screenLayoutChange(reason);
Pierre Ossman04e62db2009-03-23 16:57:07 +0000269 } catch(rdr::Exception &e) {
270 close(e.str());
271 }
272}
273
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000274void VNCSConnectionST::bellOrClose()
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000275{
276 try {
277 if (state() == RFBSTATE_NORMAL) writer()->writeBell();
278 } catch(rdr::Exception& e) {
279 close(e.str());
280 }
281}
282
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000283void VNCSConnectionST::serverCutTextOrClose(const char *str, int len)
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000284{
285 try {
286 if (!(accessRights & AccessCutText)) return;
287 if (!rfb::Server::sendCutText) return;
288 if (state() == RFBSTATE_NORMAL)
289 writer()->writeServerCutText(str, len);
290 } catch(rdr::Exception& e) {
291 close(e.str());
292 }
293}
294
Peter Ã…strandc39e0782009-01-15 12:21:42 +0000295
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000296void VNCSConnectionST::setDesktopNameOrClose(const char *name)
Peter Ã…strandc39e0782009-01-15 12:21:42 +0000297{
Peter Ã…strandc39e0782009-01-15 12:21:42 +0000298 try {
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000299 setDesktopName(name);
Peter Ã…strandc39e0782009-01-15 12:21:42 +0000300 } catch(rdr::Exception& e) {
301 close(e.str());
302 }
303}
304
305
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000306void VNCSConnectionST::setCursorOrClose()
307{
308 try {
309 setCursor();
310 } catch(rdr::Exception& e) {
311 close(e.str());
312 }
313}
314
315
Pierre Ossmanb45a84f2016-12-12 16:59:15 +0100316void VNCSConnectionST::setLEDStateOrClose(unsigned int state)
317{
318 try {
319 setLEDState(state);
320 } catch(rdr::Exception& e) {
321 close(e.str());
322 }
323}
324
325
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000326int VNCSConnectionST::checkIdleTimeout()
327{
328 int idleTimeout = rfb::Server::idleTimeout;
329 if (idleTimeout == 0) return 0;
330 if (state() != RFBSTATE_NORMAL && idleTimeout < 15)
331 idleTimeout = 15; // minimum of 15 seconds while authenticating
332 time_t now = time(0);
333 if (now < lastEventTime) {
334 // Someone must have set the time backwards. Set lastEventTime so that the
335 // idleTimeout will count from now.
336 vlog.info("Time has gone backwards - resetting idle timeout");
337 lastEventTime = now;
338 }
339 int timeLeft = lastEventTime + idleTimeout - now;
340 if (timeLeft < -60) {
341 // Our callback is over a minute late - someone must have set the time
342 // forwards. Set lastEventTime so that the idleTimeout will count from
343 // now.
344 vlog.info("Time has gone forwards - resetting idle timeout");
345 lastEventTime = now;
346 return secsToMillis(idleTimeout);
347 }
348 if (timeLeft <= 0) {
349 close("Idle timeout");
350 return 0;
351 }
352 return secsToMillis(timeLeft);
353}
354
Pierre Ossmanb114cec2011-11-20 15:36:11 +0000355
356bool VNCSConnectionST::getComparerState()
357{
358 // We interpret a low compression level as an indication that the client
359 // wants to prioritise CPU usage over bandwidth, and hence disable the
360 // comparing update tracker.
361 return (cp.compressLevel == -1) || (cp.compressLevel > 1);
362}
363
364
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000365// renderedCursorChange() is called whenever the server-side rendered cursor
366// changes shape or position. It ensures that the next update will clean up
367// the old rendered cursor and if necessary draw the new rendered cursor.
368
369void VNCSConnectionST::renderedCursorChange()
370{
371 if (state() != RFBSTATE_NORMAL) return;
Pierre Ossman671d2ef2015-06-09 16:09:06 +0200372 if (!damagedCursorRegion.is_empty())
Pierre Ossman5c9e1e52011-11-08 10:32:05 +0000373 removeRenderedCursor = true;
Pierre Ossman6b0bc292011-12-21 13:17:54 +0000374 if (needRenderedCursor()) {
Pierre Ossman671d2ef2015-06-09 16:09:06 +0200375 updateRenderedCursor = true;
Pierre Ossman6b0bc292011-12-21 13:17:54 +0000376 writeFramebufferUpdateOrClose();
377 }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000378}
379
380// needRenderedCursor() returns true if this client needs the server-side
381// rendered cursor. This may be because it does not support local cursor or
382// because the current cursor position has not been set by this client.
383// Unfortunately we can't know for sure when the current cursor position has
384// been set by this client. We guess that this is the case when the current
385// cursor position is the same as the last pointer event from this client, or
386// if it is a very short time since this client's last pointer event (up to a
387// second). [ Ideally we should do finer-grained timing here and make the time
388// configurable, but I don't think it's that important. ]
389
390bool VNCSConnectionST::needRenderedCursor()
391{
Pierre Ossman77ede0a2016-12-05 16:57:30 +0100392 if (state() != RFBSTATE_NORMAL)
393 return false;
394
Pierre Ossman324043e2017-08-16 16:26:11 +0200395 if (!cp.supportsLocalCursorWithAlpha &&
396 !cp.supportsLocalCursor && !cp.supportsLocalXCursor)
Pierre Ossman77ede0a2016-12-05 16:57:30 +0100397 return true;
398 if (!server->cursorPos.equals(pointerEventPos) &&
399 (time(0) - pointerEventTime) > 0)
400 return true;
401
402 return false;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000403}
404
405
406void VNCSConnectionST::approveConnectionOrClose(bool accept,
407 const char* reason)
408{
409 try {
410 approveConnection(accept, reason);
411 } catch (rdr::Exception& e) {
412 close(e.str());
413 }
414}
415
416
417
418// -=- Callbacks from SConnection
419
420void VNCSConnectionST::authSuccess()
421{
422 lastEventTime = time(0);
423
424 server->startDesktop();
425
426 // - Set the connection parameters appropriately
427 cp.width = server->pb->width();
428 cp.height = server->pb->height();
Pierre Ossman34e62f32009-03-20 21:46:12 +0000429 cp.screenLayout = server->screenLayout;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000430 cp.setName(server->getName());
Pierre Ossmanb45a84f2016-12-12 16:59:15 +0100431 cp.setLEDState(server->ledState);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000432
433 // - Set the default pixel format
434 cp.setPF(server->pb->getPF());
435 char buffer[256];
436 cp.pf().print(buffer, 256);
437 vlog.info("Server default pixel format %s", buffer);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000438
439 // - Mark the entire display as "dirty"
440 updates.add_changed(server->pb->getRect());
441 startTime = time(0);
Pierre Ossman1b478e52011-11-15 12:08:30 +0000442
443 // - Bootstrap the congestion control
444 ackedOffset = sock->outStream().length();
445 congWindow = INITIAL_WINDOW;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000446}
447
448void VNCSConnectionST::queryConnection(const char* userName)
449{
450 // - Authentication succeeded - clear from blacklist
451 CharArray name; name.buf = sock->getPeerAddress();
452 server->blHosts->clearBlackmark(name.buf);
453
454 // - Special case to provide a more useful error message
455 if (rfb::Server::neverShared && !rfb::Server::disconnectClients &&
456 server->authClientCount() > 0) {
457 approveConnection(false, "The server is already in use");
458 return;
459 }
460
461 // - Does the client have the right to bypass the query?
462 if (reverseConnection ||
463 !(rfb::Server::queryConnect || sock->requiresQuery()) ||
464 (accessRights & AccessNoQuery))
465 {
466 approveConnection(true);
467 return;
468 }
469
470 // - Get the server to display an Accept/Reject dialog, if required
471 // If a dialog is displayed, the result will be PENDING, and the
472 // server will call approveConnection at a later time
473 CharArray reason;
474 VNCServerST::queryResult qr = server->queryConnection(sock, userName,
475 &reason.buf);
Pierre Ossmanf8e3b342015-01-26 14:37:04 +0100476 if (qr == VNCServerST::PENDING) {
477 queryConnectTimer.start(rfb::Server::queryConnectTimeout * 1000);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000478 return;
Pierre Ossmanf8e3b342015-01-26 14:37:04 +0100479 }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000480
481 // - If server returns ACCEPT/REJECT then pass result to SConnection
482 approveConnection(qr == VNCServerST::ACCEPT, reason.buf);
483}
484
485void VNCSConnectionST::clientInit(bool shared)
486{
487 lastEventTime = time(0);
488 if (rfb::Server::alwaysShared || reverseConnection) shared = true;
Pierre Ossmane7be49b2014-12-02 14:33:17 +0100489 if (!(accessRights & AccessNonShared)) shared = true;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000490 if (rfb::Server::neverShared) shared = false;
491 if (!shared) {
Pierre Ossmane7be49b2014-12-02 14:33:17 +0100492 if (rfb::Server::disconnectClients && (accessRights & AccessNonShared)) {
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000493 // - Close all the other connected clients
494 vlog.debug("non-shared connection - closing clients");
495 server->closeClients("Non-shared connection requested", getSock());
496 } else {
497 // - Refuse this connection if there are existing clients, in addition to
498 // this one
499 if (server->authClientCount() > 1) {
500 close("Server is already in use");
501 return;
502 }
503 }
504 }
505 SConnection::clientInit(shared);
506}
507
508void VNCSConnectionST::setPixelFormat(const PixelFormat& pf)
509{
510 SConnection::setPixelFormat(pf);
511 char buffer[256];
512 pf.print(buffer, 256);
513 vlog.info("Client pixel format %s", buffer);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000514 setCursor();
515}
516
517void VNCSConnectionST::pointerEvent(const Point& pos, int buttonMask)
518{
519 pointerEventTime = lastEventTime = time(0);
520 server->lastUserInputTime = lastEventTime;
521 if (!(accessRights & AccessPtrEvents)) return;
522 if (!rfb::Server::acceptPointerEvents) return;
523 if (!server->pointerClient || server->pointerClient == this) {
524 pointerEventPos = pos;
525 if (buttonMask)
526 server->pointerClient = this;
527 else
528 server->pointerClient = 0;
529 server->desktop->pointerEvent(pointerEventPos, buttonMask);
530 }
531}
532
533
534class VNCSConnectionSTShiftPresser {
535public:
536 VNCSConnectionSTShiftPresser(SDesktop* desktop_)
537 : desktop(desktop_), pressed(false) {}
538 ~VNCSConnectionSTShiftPresser() {
Pierre Ossman9a153b02015-08-31 10:01:14 +0200539 if (pressed) {
540 vlog.debug("Releasing fake Shift_L");
Pierre Ossman5ae28212017-05-16 14:30:38 +0200541 desktop->keyEvent(XK_Shift_L, 0, false);
Pierre Ossman9a153b02015-08-31 10:01:14 +0200542 }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000543 }
544 void press() {
Pierre Ossman9a153b02015-08-31 10:01:14 +0200545 vlog.debug("Pressing fake Shift_L");
Pierre Ossman5ae28212017-05-16 14:30:38 +0200546 desktop->keyEvent(XK_Shift_L, 0, true);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000547 pressed = true;
548 }
549 SDesktop* desktop;
550 bool pressed;
551};
552
553// keyEvent() - record in the pressedKeys which keys were pressed. Allow
554// multiple down events (for autorepeat), but only allow a single up event.
Pierre Ossman5ae28212017-05-16 14:30:38 +0200555void VNCSConnectionST::keyEvent(rdr::U32 keysym, rdr::U32 keycode, bool down) {
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000556 lastEventTime = time(0);
557 server->lastUserInputTime = lastEventTime;
558 if (!(accessRights & AccessKeyEvents)) return;
559 if (!rfb::Server::acceptKeyEvents) return;
560
Pierre Ossman9a153b02015-08-31 10:01:14 +0200561 if (down)
Pierre Ossman5ae28212017-05-16 14:30:38 +0200562 vlog.debug("Key pressed: 0x%x / 0x%x", keysym, keycode);
Pierre Ossman9a153b02015-08-31 10:01:14 +0200563 else
Pierre Ossman5ae28212017-05-16 14:30:38 +0200564 vlog.debug("Key released: 0x%x / 0x%x", keysym, keycode);
Pierre Ossman9a153b02015-08-31 10:01:14 +0200565
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000566 // Remap the key if required
Pierre Ossman9a153b02015-08-31 10:01:14 +0200567 if (server->keyRemapper) {
568 rdr::U32 newkey;
Pierre Ossman5ae28212017-05-16 14:30:38 +0200569 newkey = server->keyRemapper->remapKey(keysym);
570 if (newkey != keysym) {
Pierre Ossman9a153b02015-08-31 10:01:14 +0200571 vlog.debug("Key remapped to 0x%x", newkey);
Pierre Ossman5ae28212017-05-16 14:30:38 +0200572 keysym = newkey;
Pierre Ossman9a153b02015-08-31 10:01:14 +0200573 }
574 }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000575
Pierre Ossmanbb305ca2016-12-11 12:41:26 +0100576 // Avoid lock keys if we don't know the server state
577 if ((server->ledState == ledUnknown) &&
Pierre Ossman5ae28212017-05-16 14:30:38 +0200578 ((keysym == XK_Caps_Lock) ||
579 (keysym == XK_Num_Lock) ||
580 (keysym == XK_Scroll_Lock))) {
Pierre Ossmanbb305ca2016-12-11 12:41:26 +0100581 vlog.debug("Ignoring lock key (e.g. caps lock)");
582 return;
583 }
Pierre Ossmanbb305ca2016-12-11 12:41:26 +0100584
Pierre Ossmanb45a84f2016-12-12 16:59:15 +0100585 // Lock key heuristics
586 // (only for clients that do not support the LED state extension)
587 if (!cp.supportsLEDState) {
588 // Always ignore ScrollLock as we don't have a heuristic
589 // for that
Pierre Ossman5ae28212017-05-16 14:30:38 +0200590 if (keysym == XK_Scroll_Lock) {
Pierre Ossmanb45a84f2016-12-12 16:59:15 +0100591 vlog.debug("Ignoring lock key (e.g. caps lock)");
592 return;
Pierre Ossmanbb305ca2016-12-11 12:41:26 +0100593 }
594
Pierre Ossmanb45a84f2016-12-12 16:59:15 +0100595 if (down && (server->ledState != ledUnknown)) {
596 // CapsLock synchronisation heuristic
597 // (this assumes standard interaction between CapsLock the Shift
598 // keys and normal characters)
Pierre Ossman5ae28212017-05-16 14:30:38 +0200599 if (((keysym >= XK_A) && (keysym <= XK_Z)) ||
600 ((keysym >= XK_a) && (keysym <= XK_z))) {
Pierre Ossmanb45a84f2016-12-12 16:59:15 +0100601 bool uppercase, shift, lock;
Pierre Ossmanbb305ca2016-12-11 12:41:26 +0100602
Pierre Ossman5ae28212017-05-16 14:30:38 +0200603 uppercase = (keysym >= XK_A) && (keysym <= XK_Z);
Pierre Ossmanb45a84f2016-12-12 16:59:15 +0100604 shift = pressedKeys.find(XK_Shift_L) != pressedKeys.end() ||
605 pressedKeys.find(XK_Shift_R) != pressedKeys.end();
606 lock = server->ledState & ledCapsLock;
Pierre Ossmanbb305ca2016-12-11 12:41:26 +0100607
Pierre Ossmanb45a84f2016-12-12 16:59:15 +0100608 if (lock == (uppercase == shift)) {
609 vlog.debug("Inserting fake CapsLock to get in sync with client");
Pierre Ossman5ae28212017-05-16 14:30:38 +0200610 server->desktop->keyEvent(XK_Caps_Lock, 0, true);
611 server->desktop->keyEvent(XK_Caps_Lock, 0, false);
Pierre Ossmanb45a84f2016-12-12 16:59:15 +0100612 }
613 }
614
615 // NumLock synchronisation heuristic
616 // (this is more cautious because of the differences between Unix,
617 // Windows and macOS)
Pierre Ossman5ae28212017-05-16 14:30:38 +0200618 if (((keysym >= XK_KP_Home) && (keysym <= XK_KP_Delete)) ||
619 ((keysym >= XK_KP_0) && (keysym <= XK_KP_9)) ||
620 (keysym == XK_KP_Separator) || (keysym == XK_KP_Decimal)) {
Pierre Ossmanb45a84f2016-12-12 16:59:15 +0100621 bool number, shift, lock;
622
Pierre Ossman5ae28212017-05-16 14:30:38 +0200623 number = ((keysym >= XK_KP_0) && (keysym <= XK_KP_9)) ||
624 (keysym == XK_KP_Separator) || (keysym == XK_KP_Decimal);
Pierre Ossmanb45a84f2016-12-12 16:59:15 +0100625 shift = pressedKeys.find(XK_Shift_L) != pressedKeys.end() ||
626 pressedKeys.find(XK_Shift_R) != pressedKeys.end();
627 lock = server->ledState & ledNumLock;
628
629 if (shift) {
630 // We don't know the appropriate NumLock state for when Shift
631 // is pressed as it could be one of:
632 //
633 // a) A Unix client where Shift negates NumLock
634 //
635 // b) A Windows client where Shift only cancels NumLock
636 //
637 // c) A macOS client where Shift doesn't have any effect
638 //
639 } else if (lock == (number == shift)) {
640 vlog.debug("Inserting fake NumLock to get in sync with client");
Pierre Ossman5ae28212017-05-16 14:30:38 +0200641 server->desktop->keyEvent(XK_Num_Lock, 0, true);
642 server->desktop->keyEvent(XK_Num_Lock, 0, false);
Pierre Ossmanb45a84f2016-12-12 16:59:15 +0100643 }
Pierre Ossmanbb305ca2016-12-11 12:41:26 +0100644 }
645 }
646 }
647
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000648 // Turn ISO_Left_Tab into shifted Tab.
649 VNCSConnectionSTShiftPresser shiftPresser(server->desktop);
Pierre Ossman5ae28212017-05-16 14:30:38 +0200650 if (keysym == XK_ISO_Left_Tab) {
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000651 if (pressedKeys.find(XK_Shift_L) == pressedKeys.end() &&
652 pressedKeys.find(XK_Shift_R) == pressedKeys.end())
653 shiftPresser.press();
Pierre Ossman5ae28212017-05-16 14:30:38 +0200654 keysym = XK_Tab;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000655 }
656
657 if (down) {
Pierre Ossman5ae28212017-05-16 14:30:38 +0200658 pressedKeys.insert(keysym);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000659 } else {
Pierre Ossman5ae28212017-05-16 14:30:38 +0200660 if (!pressedKeys.erase(keysym))
661 return;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000662 }
Pierre Ossman5ae28212017-05-16 14:30:38 +0200663 server->desktop->keyEvent(keysym, keycode, down);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000664}
665
666void VNCSConnectionST::clientCutText(const char* str, int len)
667{
668 if (!(accessRights & AccessCutText)) return;
669 if (!rfb::Server::acceptCutText) return;
670 server->desktop->clientCutText(str, len);
671}
672
673void VNCSConnectionST::framebufferUpdateRequest(const Rect& r,bool incremental)
674{
Pierre Ossmane9962f72009-04-23 12:31:42 +0000675 Rect safeRect;
676
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000677 if (!(accessRights & AccessView)) return;
678
679 SConnection::framebufferUpdateRequest(r, incremental);
680
Pierre Ossmand9a59ba2009-03-20 15:55:37 +0000681 // Check that the client isn't sending crappy requests
682 if (!r.enclosed_by(Rect(0, 0, cp.width, cp.height))) {
683 vlog.error("FramebufferUpdateRequest %dx%d at %d,%d exceeds framebuffer %dx%d",
684 r.width(), r.height(), r.tl.x, r.tl.y, cp.width, cp.height);
Pierre Ossmane9962f72009-04-23 12:31:42 +0000685 safeRect = r.intersect(Rect(0, 0, cp.width, cp.height));
686 } else {
687 safeRect = r;
Pierre Ossmand9a59ba2009-03-20 15:55:37 +0000688 }
689
Constantin Kaplinsky2ef66952008-08-29 11:33:46 +0000690 // Just update the requested region.
691 // Framebuffer update will be sent a bit later, see processMessages().
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000692 Region reqRgn(r);
Pierre Ossman1b478e52011-11-15 12:08:30 +0000693 if (!incremental || !continuousUpdates)
694 requested.assign_union(reqRgn);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000695
696 if (!incremental) {
697 // Non-incremental update - treat as if area requested has changed
698 updates.add_changed(reqRgn);
Pierre Ossman53125a72009-04-22 15:37:51 +0000699
700 // And send the screen layout to the client (which, unlike the
701 // framebuffer dimensions, the client doesn't get during init)
Pierre Ossmanc5e25602009-03-20 12:59:05 +0000702 writer()->writeExtendedDesktopSize();
Pierre Ossman53125a72009-04-22 15:37:51 +0000703
704 // We do not send a DesktopSize since it only contains the
705 // framebuffer size (which the client already should know) and
706 // because some clients don't handle extra DesktopSize events
707 // very well.
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000708 }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000709}
710
Pierre Ossman34bb0612009-03-21 21:16:14 +0000711void VNCSConnectionST::setDesktopSize(int fb_width, int fb_height,
712 const ScreenSet& layout)
Pierre Ossmanc5e25602009-03-20 12:59:05 +0000713{
Pierre Ossman04e62db2009-03-23 16:57:07 +0000714 unsigned int result;
715
Michal Srbb318b8f2014-11-24 13:18:28 +0200716 if (!(accessRights & AccessSetDesktopSize)) return;
717 if (!rfb::Server::acceptSetDesktopSize) return;
718
Pierre Ossman04e62db2009-03-23 16:57:07 +0000719 // Don't bother the desktop with an invalid configuration
720 if (!layout.validate(fb_width, fb_height)) {
721 writer()->writeExtendedDesktopSize(reasonClient, resultInvalid,
722 fb_width, fb_height, layout);
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000723 writeFramebufferUpdate();
Pierre Ossman04e62db2009-03-23 16:57:07 +0000724 return;
725 }
726
727 // FIXME: the desktop will call back to VNCServerST and an extra set
728 // of ExtendedDesktopSize messages will be sent. This is okay
729 // protocol-wise, but unnecessary.
730 result = server->desktop->setScreenLayout(fb_width, fb_height, layout);
731
Pierre Ossman04e62db2009-03-23 16:57:07 +0000732 writer()->writeExtendedDesktopSize(reasonClient, result,
733 fb_width, fb_height, layout);
Pierre Ossman04e62db2009-03-23 16:57:07 +0000734
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000735 // Only notify other clients on success
Pierre Ossman04e62db2009-03-23 16:57:07 +0000736 if (result == resultSuccess) {
737 if (server->screenLayout != layout)
738 throw Exception("Desktop configured a different screen layout than requested");
739 server->notifyScreenLayoutChange(this);
740 }
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000741
742 // but always send back a reply to the requesting client
743 // (do this last as it might throw an exception on socket errors)
744 writeFramebufferUpdate();
Pierre Ossmanc5e25602009-03-20 12:59:05 +0000745}
746
Pierre Ossman2c764942011-11-14 15:54:30 +0000747void VNCSConnectionST::fence(rdr::U32 flags, unsigned len, const char data[])
748{
749 if (flags & fenceFlagRequest) {
750 if (flags & fenceFlagSyncNext) {
Pierre Ossmanb8b1e962012-07-20 10:47:00 +0000751 pendingSyncFence = true;
Pierre Ossman2c764942011-11-14 15:54:30 +0000752
753 fenceFlags = flags & (fenceFlagBlockBefore | fenceFlagBlockAfter | fenceFlagSyncNext);
754 fenceDataLen = len;
755 delete [] fenceData;
Michal Srbf3afa242017-03-27 19:02:15 +0300756 fenceData = NULL;
Pierre Ossman2c764942011-11-14 15:54:30 +0000757 if (len > 0) {
758 fenceData = new char[len];
759 memcpy(fenceData, data, len);
760 }
761
762 return;
763 }
764
765 // We handle everything synchronously so we trivially honor these modes
766 flags = flags & (fenceFlagBlockBefore | fenceFlagBlockAfter);
767
768 writer()->writeFence(flags, len, data);
769 return;
770 }
771
Pierre Ossman1b478e52011-11-15 12:08:30 +0000772 struct RTTInfo rttInfo;
773
Pierre Ossman2c764942011-11-14 15:54:30 +0000774 switch (len) {
775 case 0:
776 // Initial dummy fence;
777 break;
Pierre Ossman1b478e52011-11-15 12:08:30 +0000778 case sizeof(struct RTTInfo):
779 memcpy(&rttInfo, data, sizeof(struct RTTInfo));
780 handleRTTPong(rttInfo);
781 break;
Pierre Ossman2c764942011-11-14 15:54:30 +0000782 default:
783 vlog.error("Fence response of unexpected size received");
784 }
785}
786
Pierre Ossman1b478e52011-11-15 12:08:30 +0000787void VNCSConnectionST::enableContinuousUpdates(bool enable,
788 int x, int y, int w, int h)
789{
790 Rect rect;
791
792 if (!cp.supportsFence || !cp.supportsContinuousUpdates)
793 throw Exception("Client tried to enable continuous updates when not allowed");
794
795 continuousUpdates = enable;
796
797 rect.setXYWH(x, y, w, h);
798 cuRegion.reset(rect);
799
800 if (enable) {
801 requested.clear();
802 writeFramebufferUpdate();
803 } else {
804 writer()->writeEndOfContinuousUpdates();
805 }
806}
807
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000808// supportsLocalCursor() is called whenever the status of
809// cp.supportsLocalCursor has changed. If the client does now support local
810// cursor, we make sure that the old server-side rendered cursor is cleaned up
811// and the cursor is sent to the client.
812
813void VNCSConnectionST::supportsLocalCursor()
814{
Pierre Ossman324043e2017-08-16 16:26:11 +0200815 if (cp.supportsLocalCursorWithAlpha ||
816 cp.supportsLocalCursor || cp.supportsLocalXCursor) {
Pierre Ossman671d2ef2015-06-09 16:09:06 +0200817 if (!damagedCursorRegion.is_empty())
Pierre Ossman5c9e1e52011-11-08 10:32:05 +0000818 removeRenderedCursor = true;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000819 setCursor();
820 }
821}
822
Pierre Ossman2c764942011-11-14 15:54:30 +0000823void VNCSConnectionST::supportsFence()
824{
825 writer()->writeFence(fenceFlagRequest, 0, NULL);
826}
827
Pierre Ossman1b478e52011-11-15 12:08:30 +0000828void VNCSConnectionST::supportsContinuousUpdates()
829{
830 // We refuse to use continuous updates if we cannot monitor the buffer
831 // usage using fences.
832 if (!cp.supportsFence)
833 return;
834
835 writer()->writeEndOfContinuousUpdates();
836}
837
Pierre Ossmanb45a84f2016-12-12 16:59:15 +0100838void VNCSConnectionST::supportsLEDState()
839{
840 writer()->writeLEDState();
841}
842
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000843
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +0000844bool VNCSConnectionST::handleTimeout(Timer* t)
845{
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000846 try {
Pierre Ossmana40ab202016-04-29 15:35:56 +0200847 if (t == &congestionTimer)
Pierre Ossman1b478e52011-11-15 12:08:30 +0000848 updateCongestion();
Pierre Ossmanf8e3b342015-01-26 14:37:04 +0100849 else if (t == &queryConnectTimer) {
850 if (state() == RFBSTATE_QUERYING)
851 approveConnection(false, "The attempt to prompt the user to accept the connection failed");
852 }
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000853 } catch (rdr::Exception& e) {
854 close(e.str());
855 }
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +0000856
857 return false;
858}
859
860
Pierre Ossman1b478e52011-11-15 12:08:30 +0000861void VNCSConnectionST::writeRTTPing()
862{
863 struct RTTInfo rttInfo;
864
865 if (!cp.supportsFence)
866 return;
867
868 memset(&rttInfo, 0, sizeof(struct RTTInfo));
869
870 gettimeofday(&rttInfo.tv, NULL);
871 rttInfo.offset = sock->outStream().length();
872 rttInfo.inFlight = rttInfo.offset - ackedOffset;
873
874 // We need to make sure any old update are already processed by the
875 // time we get the response back. This allows us to reliably throttle
876 // back on client overload, as well as network overload.
877 writer()->writeFence(fenceFlagRequest | fenceFlagBlockBefore,
878 sizeof(struct RTTInfo), (const char*)&rttInfo);
879
880 pingCounter++;
881
882 sentOffset = rttInfo.offset;
883
884 // Let some data flow before we adjust the settings
885 if (!congestionTimer.isStarted())
886 congestionTimer.start(__rfbmin(baseRTT * 2, 100));
887}
888
889void VNCSConnectionST::handleRTTPong(const struct RTTInfo &rttInfo)
890{
891 unsigned rtt, delay;
Pierre Ossman1b478e52011-11-15 12:08:30 +0000892
893 pingCounter--;
894
895 rtt = msSince(&rttInfo.tv);
896 if (rtt < 1)
897 rtt = 1;
898
899 ackedOffset = rttInfo.offset;
900
901 // Try to estimate wire latency by tracking lowest seen latency
902 if (rtt < baseRTT)
903 baseRTT = rtt;
904
905 if (rttInfo.inFlight > congWindow) {
906 seenCongestion = true;
907
908 // Estimate added delay because of overtaxed buffers
909 delay = (rttInfo.inFlight - congWindow) * baseRTT / congWindow;
910
911 if (delay < rtt)
912 rtt -= delay;
913 else
914 rtt = 1;
915
916 // If we underestimate the congestion window, then we'll get a latency
917 // that's less than the wire latency, which will confuse other portions
918 // of the code.
919 if (rtt < baseRTT)
920 rtt = baseRTT;
921 }
922
923 // We only keep track of the minimum latency seen (for a given interval)
klemens0536d092017-01-28 20:56:56 +0100924 // on the basis that we want to avoid continuous buffer issue, but don't
Pierre Ossman1b478e52011-11-15 12:08:30 +0000925 // mind (or even approve of) bursts.
926 if (rtt < minRTT)
927 minRTT = rtt;
928}
929
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +0000930bool VNCSConnectionST::isCongested()
931{
Pierre Ossman1b478e52011-11-15 12:08:30 +0000932 int offset;
933
934 // Stuff still waiting in the send buffer?
Pierre Ossman352d0622016-04-29 15:50:54 +0200935 sock->outStream().flush();
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +0000936 if (sock->outStream().bufferUsage() > 0)
937 return true;
938
Pierre Ossman1b478e52011-11-15 12:08:30 +0000939 if (!cp.supportsFence)
940 return false;
941
942 // Idle for too long? (and no data on the wire)
943 //
944 // FIXME: This should really just be one baseRTT, but we're getting
945 // problems with triggering the idle timeout on each update.
946 // Maybe we need to use a moving average for the wire latency
947 // instead of baseRTT.
948 if ((sentOffset == ackedOffset) &&
949 (sock->outStream().getIdleTime() > 2 * baseRTT)) {
950
951#ifdef CONGESTION_DEBUG
952 if (congWindow > INITIAL_WINDOW)
953 fprintf(stderr, "Reverting to initial window (%d KiB) after %d ms\n",
954 INITIAL_WINDOW / 1024, sock->outStream().getIdleTime());
955#endif
956
957 // Close congestion window and allow a transfer
958 // FIXME: Reset baseRTT like Linux Vegas?
959 congWindow = __rfbmin(INITIAL_WINDOW, congWindow);
960
961 return false;
962 }
963
964 offset = sock->outStream().length();
965
966 // FIXME: Should we compensate for non-update data?
967 // (i.e. use sentOffset instead of offset)
968 if ((offset - ackedOffset) < congWindow)
969 return false;
970
971 // If we just have one outstanding "ping", that means the client has
972 // started receiving our update. In order to not regress compared to
973 // before we had congestion avoidance, we allow another update here.
974 // This could further clog up the tubes, but congestion control isn't
975 // really working properly right now anyway as the wire would otherwise
976 // be idle for at least RTT/2.
977 if (pingCounter == 1)
978 return false;
979
980 return true;
981}
982
983
984void VNCSConnectionST::updateCongestion()
985{
986 unsigned diff;
987
988 if (!seenCongestion)
989 return;
990
991 diff = minRTT - baseRTT;
992
993 if (diff > __rfbmin(100, baseRTT)) {
994 // Way too fast
995 congWindow = congWindow * baseRTT / minRTT;
996 } else if (diff > __rfbmin(50, baseRTT/2)) {
997 // Slightly too fast
998 congWindow -= 4096;
999 } else if (diff < 5) {
1000 // Way too slow
1001 congWindow += 8192;
1002 } else if (diff < 25) {
1003 // Too slow
1004 congWindow += 4096;
1005 }
1006
1007 if (congWindow < MINIMUM_WINDOW)
1008 congWindow = MINIMUM_WINDOW;
1009 if (congWindow > MAXIMUM_WINDOW)
1010 congWindow = MAXIMUM_WINDOW;
1011
1012#ifdef CONGESTION_DEBUG
1013 fprintf(stderr, "RTT: %d ms (%d ms), Window: %d KiB, Bandwidth: %g Mbps\n",
1014 minRTT, baseRTT, congWindow / 1024,
1015 congWindow * 8.0 / baseRTT / 1000.0);
1016
1017#ifdef TCP_INFO
1018 struct tcp_info tcp_info;
1019 socklen_t tcp_info_length;
1020
1021 tcp_info_length = sizeof(tcp_info);
1022 if (getsockopt(sock->getFd(), SOL_TCP, TCP_INFO,
1023 (void *)&tcp_info, &tcp_info_length) == 0) {
1024 fprintf(stderr, "Socket: RTT: %d ms (+/- %d ms) Window %d KiB\n",
1025 tcp_info.tcpi_rtt / 1000, tcp_info.tcpi_rttvar / 1000,
1026 tcp_info.tcpi_snd_mss * tcp_info.tcpi_snd_cwnd / 1024);
1027 }
1028#endif
1029
1030#endif
1031
1032 minRTT = -1;
1033 seenCongestion = false;
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +00001034}
1035
1036
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001037void VNCSConnectionST::writeFramebufferUpdate()
1038{
Pierre Ossman2c764942011-11-14 15:54:30 +00001039 // We're in the middle of processing a command that's supposed to be
1040 // synchronised. Allowing an update to slip out right now might violate
1041 // that synchronisation.
1042 if (syncFence)
1043 return;
1044
Pierre Ossmana3ac01e2011-11-07 21:13:54 +00001045 // We try to aggregate responses, so don't send out anything whilst we
1046 // still have incoming messages. processMessages() will give us another
1047 // chance to run once things are idle.
1048 if (inProcessMessages)
1049 return;
1050
Pierre Ossman1b478e52011-11-15 12:08:30 +00001051 if (state() != RFBSTATE_NORMAL)
1052 return;
1053 if (requested.is_empty() && !continuousUpdates)
Pierre Ossmane9962f72009-04-23 12:31:42 +00001054 return;
Pierre Ossmand9a59ba2009-03-20 15:55:37 +00001055
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +00001056 // Check that we actually have some space on the link and retry in a
1057 // bit if things are congested.
Pierre Ossmana40ab202016-04-29 15:35:56 +02001058 if (isCongested())
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +00001059 return;
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +00001060
Pierre Ossmancef3cf72016-11-25 10:06:34 +01001061 // Updates often consists of many small writes, and in continuous
1062 // mode, we will also have small fence messages around the update. We
1063 // need to aggregate these in order to not clog up TCP's congestion
1064 // window.
Pierre Ossman36dadf82011-11-15 12:11:32 +00001065 network::TcpSocket::cork(sock->getFd(), true);
1066
Pierre Ossmane9962f72009-04-23 12:31:42 +00001067 // First take care of any updates that cannot contain framebuffer data
1068 // changes.
Pierre Ossmancef3cf72016-11-25 10:06:34 +01001069 writeNoDataUpdate();
1070
1071 // Then real data (if possible)
1072 writeDataUpdate();
1073
1074 network::TcpSocket::cork(sock->getFd(), false);
1075}
1076
1077void VNCSConnectionST::writeNoDataUpdate()
1078{
1079 if (!writer()->needNoDataUpdate())
1080 return;
1081
1082 writer()->writeNoDataUpdate();
1083
1084 // Make sure no data update is sent until next request
1085 requested.clear();
1086}
1087
1088void VNCSConnectionST::writeDataUpdate()
1089{
1090 Region req;
1091 UpdateInfo ui;
1092 bool needNewUpdateInfo;
Pierre Ossman24684e52016-12-05 16:58:19 +01001093 const RenderedCursor *cursor;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001094
Constantin Kaplinsky604d7812007-08-31 15:50:37 +00001095 updates.enable_copyrect(cp.useCopyRect);
1096
Pierre Ossman6e49e952016-10-07 15:59:38 +02001097 // See if we are allowed to send anything right now (the framebuffer
1098 // might have changed in ways we haven't yet been informed of).
Pierre Ossmanbbf955e2011-11-08 12:44:10 +00001099 if (!server->checkUpdate())
Pierre Ossmancef3cf72016-11-25 10:06:34 +01001100 return;
Constantin Kaplinskya09dc142008-12-18 12:08:15 +00001101
Pierre Ossmancef3cf72016-11-25 10:06:34 +01001102 // See what the client has requested (if anything)
Pierre Ossman1b478e52011-11-15 12:08:30 +00001103 if (continuousUpdates)
1104 req = cuRegion.union_(requested);
1105 else
1106 req = requested;
1107
Pierre Ossmancef3cf72016-11-25 10:06:34 +01001108 if (req.is_empty())
1109 return;
1110
1111 // Get the lists of updates. Prior to exporting the data to the `ui' object,
1112 // getUpdateInfo() will normalize the `updates' object such way that its
1113 // `changed' and `copied' regions would not intersect.
Pierre Ossman1b478e52011-11-15 12:08:30 +00001114 updates.getUpdateInfo(&ui, req);
1115 needNewUpdateInfo = false;
Constantin Kaplinsky604d7812007-08-31 15:50:37 +00001116
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001117 // If the previous position of the rendered cursor overlaps the source of the
1118 // copy, then when the copy happens the corresponding rectangle in the
1119 // destination will be wrong, so add it to the changed region.
1120
Pierre Ossman671d2ef2015-06-09 16:09:06 +02001121 if (!ui.copied.is_empty() && !damagedCursorRegion.is_empty()) {
1122 Region bogusCopiedCursor;
1123
1124 bogusCopiedCursor.copyFrom(damagedCursorRegion);
1125 bogusCopiedCursor.translate(ui.copy_delta);
1126 bogusCopiedCursor.assign_intersect(server->pb->getRect());
Constantin Kaplinsky45517c82007-08-31 15:56:33 +00001127 if (!ui.copied.intersect(bogusCopiedCursor).is_empty()) {
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001128 updates.add_changed(bogusCopiedCursor);
Constantin Kaplinsky604d7812007-08-31 15:50:37 +00001129 needNewUpdateInfo = true;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001130 }
1131 }
1132
Pierre Ossman671d2ef2015-06-09 16:09:06 +02001133 // If we need to remove the old rendered cursor, just add the region to
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001134 // the changed region.
1135
1136 if (removeRenderedCursor) {
Pierre Ossman671d2ef2015-06-09 16:09:06 +02001137 updates.add_changed(damagedCursorRegion);
Constantin Kaplinsky604d7812007-08-31 15:50:37 +00001138 needNewUpdateInfo = true;
Pierre Ossman671d2ef2015-06-09 16:09:06 +02001139 damagedCursorRegion.clear();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001140 removeRenderedCursor = false;
1141 }
1142
1143 // Return if there is nothing to send the client.
1144
Pierre Ossman671d2ef2015-06-09 16:09:06 +02001145 if (updates.is_empty() && !writer()->needFakeUpdate() && !updateRenderedCursor)
Pierre Ossmancef3cf72016-11-25 10:06:34 +01001146 return;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001147
Constantin Kaplinsky604d7812007-08-31 15:50:37 +00001148 // The `updates' object could change, make sure we have valid update info.
1149
1150 if (needNewUpdateInfo)
Pierre Ossman1b478e52011-11-15 12:08:30 +00001151 updates.getUpdateInfo(&ui, req);
Constantin Kaplinsky604d7812007-08-31 15:50:37 +00001152
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001153 // If the client needs a server-side rendered cursor, work out the cursor
1154 // rectangle. If it's empty then don't bother drawing it, but if it overlaps
1155 // with the update region, we need to draw the rendered cursor regardless of
1156 // whether it has changed.
1157
Pierre Ossman24684e52016-12-05 16:58:19 +01001158 cursor = NULL;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001159 if (needRenderedCursor()) {
Pierre Ossman671d2ef2015-06-09 16:09:06 +02001160 Rect renderedCursorRect;
1161
Pierre Ossman24684e52016-12-05 16:58:19 +01001162 cursor = server->getRenderedCursor();
1163
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001164 renderedCursorRect
Pierre Ossman24684e52016-12-05 16:58:19 +01001165 = cursor->getEffectiveRect().intersect(req.get_bounding_rect());
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001166
1167 if (renderedCursorRect.is_empty()) {
Pierre Ossman24684e52016-12-05 16:58:19 +01001168 cursor = NULL;
1169 } else if (!updateRenderedCursor &&
1170 ui.changed.union_(ui.copied)
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001171 .intersect(renderedCursorRect).is_empty()) {
Pierre Ossman24684e52016-12-05 16:58:19 +01001172 cursor = NULL;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001173 }
1174
Pierre Ossman5c037202016-12-05 17:00:35 +01001175 if (cursor) {
1176 updates.subtract(renderedCursorRect);
1177 updates.getUpdateInfo(&ui, req);
1178 }
Pierre Ossman671d2ef2015-06-09 16:09:06 +02001179
1180 damagedCursorRegion.assign_union(renderedCursorRect);
1181 updateRenderedCursor = false;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001182 }
1183
Pierre Ossman24684e52016-12-05 16:58:19 +01001184 if (ui.is_empty() && !writer()->needFakeUpdate() && !cursor)
Pierre Ossmancef3cf72016-11-25 10:06:34 +01001185 return;
Pierre Ossmanfdba3fe2014-01-31 13:12:18 +01001186
Pierre Ossmancef3cf72016-11-25 10:06:34 +01001187 writeRTTPing();
Pierre Ossman1b478e52011-11-15 12:08:30 +00001188
Pierre Ossmancef3cf72016-11-25 10:06:34 +01001189 encodeManager.writeUpdate(ui, server->getPixelBuffer(), cursor);
Pierre Ossman1b478e52011-11-15 12:08:30 +00001190
Pierre Ossmancef3cf72016-11-25 10:06:34 +01001191 writeRTTPing();
Pierre Ossmanc0397262014-03-14 15:59:46 +01001192
Pierre Ossmancef3cf72016-11-25 10:06:34 +01001193 // The request might be for just part of the screen, so we cannot
1194 // just clear the entire update tracker.
1195 updates.subtract(req);
Pierre Ossmanc0397262014-03-14 15:59:46 +01001196
Pierre Ossmancef3cf72016-11-25 10:06:34 +01001197 requested.clear();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001198}
1199
1200
Pierre Ossmana3ac01e2011-11-07 21:13:54 +00001201void VNCSConnectionST::screenLayoutChange(rdr::U16 reason)
1202{
1203 if (!authenticated())
1204 return;
1205
1206 cp.screenLayout = server->screenLayout;
1207
1208 if (state() != RFBSTATE_NORMAL)
1209 return;
1210
1211 writer()->writeExtendedDesktopSize(reason, 0, cp.width, cp.height,
1212 cp.screenLayout);
1213 writeFramebufferUpdate();
1214}
1215
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001216
1217// setCursor() is called whenever the cursor has changed shape or pixel format.
1218// If the client supports local cursor then it will arrange for the cursor to
1219// be sent to the client.
1220
1221void VNCSConnectionST::setCursor()
1222{
Pierre Ossmana3ac01e2011-11-07 21:13:54 +00001223 if (state() != RFBSTATE_NORMAL)
1224 return;
Pierre Ossmana3ac01e2011-11-07 21:13:54 +00001225
Pierre Ossman6a1a0d02017-02-19 15:48:17 +01001226 cp.setCursor(*server->cursor);
Pierre Ossman126e5642014-02-13 14:40:25 +01001227
Pierre Ossman8053c8e2017-02-21 12:59:04 +01001228 if (!writer()->writeSetCursorWithAlpha()) {
1229 if (!writer()->writeSetCursor()) {
1230 if (!writer()->writeSetXCursor()) {
1231 // No client support
1232 return;
1233 }
Pierre Ossman126e5642014-02-13 14:40:25 +01001234 }
1235 }
1236
Pierre Ossmana3ac01e2011-11-07 21:13:54 +00001237 writeFramebufferUpdate();
1238}
1239
1240void VNCSConnectionST::setDesktopName(const char *name)
1241{
1242 cp.setName(name);
1243
1244 if (state() != RFBSTATE_NORMAL)
1245 return;
1246
1247 if (!writer()->writeSetDesktopName()) {
1248 fprintf(stderr, "Client does not support desktop rename\n");
1249 return;
1250 }
1251
1252 writeFramebufferUpdate();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001253}
1254
Pierre Ossmanb45a84f2016-12-12 16:59:15 +01001255void VNCSConnectionST::setLEDState(unsigned int ledstate)
1256{
1257 if (state() != RFBSTATE_NORMAL)
1258 return;
1259
1260 cp.setLEDState(ledstate);
1261
1262 if (!writer()->writeLEDState()) {
1263 // No client support
1264 return;
1265 }
1266
1267 writeFramebufferUpdate();
1268}
1269
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001270void VNCSConnectionST::setSocketTimeouts()
1271{
1272 int timeoutms = rfb::Server::clientWaitTimeMillis;
1273 soonestTimeout(&timeoutms, secsToMillis(rfb::Server::idleTimeout));
1274 if (timeoutms == 0)
1275 timeoutms = -1;
1276 sock->inStream().setTimeout(timeoutms);
1277 sock->outStream().setTimeout(timeoutms);
1278}
1279
1280char* VNCSConnectionST::getStartTime()
1281{
1282 char* result = ctime(&startTime);
1283 result[24] = '\0';
1284 return result;
1285}
1286
1287void VNCSConnectionST::setStatus(int status)
1288{
1289 switch (status) {
1290 case 0:
1291 accessRights = accessRights | AccessPtrEvents | AccessKeyEvents | AccessView;
1292 break;
1293 case 1:
Pierre Ossman7728be22015-03-03 16:39:37 +01001294 accessRights = (accessRights & ~(AccessPtrEvents | AccessKeyEvents)) | AccessView;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001295 break;
1296 case 2:
Adam Tkac8e985062011-02-07 11:33:57 +00001297 accessRights = accessRights & ~(AccessPtrEvents | AccessKeyEvents | AccessView);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001298 break;
1299 }
1300 framebufferUpdateRequest(server->pb->getRect(), false);
1301}
1302int VNCSConnectionST::getStatus()
1303{
1304 if ((accessRights & (AccessPtrEvents | AccessKeyEvents | AccessView)) == 0x0007)
1305 return 0;
1306 if ((accessRights & (AccessPtrEvents | AccessKeyEvents | AccessView)) == 0x0001)
1307 return 1;
1308 if ((accessRights & (AccessPtrEvents | AccessKeyEvents | AccessView)) == 0x0000)
1309 return 2;
1310 return 4;
1311}
1312