blob: 674c392233d06df1c98faeab7ca5f9fcf0de5f66 [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
Pierre Ossman16e1dcb2017-05-16 14:33:43 +0200103 while (!pressedKeys.empty()) {
104 rdr::U32 keysym, keycode;
105
106 keysym = pressedKeys.begin()->second;
107 keycode = pressedKeys.begin()->first;
108 pressedKeys.erase(pressedKeys.begin());
109
110 vlog.debug("Releasing key 0x%x / 0x%x on client disconnect",
111 keysym, keycode);
112 server->desktop->keyEvent(keysym, keycode, false);
Pierre Ossman9a153b02015-08-31 10:01:14 +0200113 }
Pierre Ossman16e1dcb2017-05-16 14:33:43 +0200114
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000115 if (server->pointerClient == this)
116 server->pointerClient = 0;
117
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000118 // Remove this client from the server
119 server->clients.remove(this);
120
Pierre Ossman2c764942011-11-14 15:54:30 +0000121 delete [] fenceData;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000122}
123
124
125// Methods called from VNCServerST
126
127bool VNCSConnectionST::init()
128{
129 try {
130 initialiseProtocol();
131 } catch (rdr::Exception& e) {
132 close(e.str());
133 return false;
134 }
135 return true;
136}
137
138void VNCSConnectionST::close(const char* reason)
139{
140 // Log the reason for the close
141 if (!closeReason.buf)
Adam Tkacd36b6262009-09-04 10:57:20 +0000142 closeReason.buf = strDup(reason);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000143 else
144 vlog.debug("second close: %s (%s)", peerEndpoint.buf, reason);
145
146 if (authenticated()) {
147 server->lastDisconnectTime = time(0);
148 }
149
150 // Just shutdown the socket and mark our state as closing. Eventually the
151 // calling code will call VNCServerST's removeSocket() method causing us to
152 // be deleted.
153 sock->shutdown();
154 setState(RFBSTATE_CLOSING);
155}
156
157
158void VNCSConnectionST::processMessages()
159{
160 if (state() == RFBSTATE_CLOSING) return;
161 try {
162 // - Now set appropriate socket timeouts and process data
163 setSocketTimeouts();
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000164
165 inProcessMessages = true;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000166
Pierre Ossmana830bec2011-11-08 12:12:02 +0000167 // Get the underlying TCP layer to build large packets if we send
168 // multiple small responses.
169 network::TcpSocket::cork(sock->getFd(), true);
170
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000171 while (getInStream()->checkNoWait(1)) {
Pierre Ossmanb8b1e962012-07-20 10:47:00 +0000172 if (pendingSyncFence) {
173 syncFence = true;
174 pendingSyncFence = false;
175 }
176
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000177 processMsg();
Pierre Ossmanb8b1e962012-07-20 10:47:00 +0000178
Pierre Ossman2c764942011-11-14 15:54:30 +0000179 if (syncFence) {
180 writer()->writeFence(fenceFlags, fenceDataLen, fenceData);
181 syncFence = false;
182 }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000183 }
184
Pierre Ossmana830bec2011-11-08 12:12:02 +0000185 // Flush out everything in case we go idle after this.
186 network::TcpSocket::cork(sock->getFd(), false);
187
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000188 inProcessMessages = false;
Constantin Kaplinsky2ef66952008-08-29 11:33:46 +0000189
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000190 // If there were anything requiring an update, try to send it here.
191 // We wait until now with this to aggregate responses and to give
192 // higher priority to user actions such as keyboard and pointer events.
193 writeFramebufferUpdate();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000194 } catch (rdr::EndOfStream&) {
195 close("Clean disconnection");
196 } catch (rdr::Exception &e) {
197 close(e.str());
198 }
199}
200
Pierre Ossmand408ca52016-04-29 14:26:05 +0200201void VNCSConnectionST::flushSocket()
202{
203 if (state() == RFBSTATE_CLOSING) return;
204 try {
205 setSocketTimeouts();
206 sock->outStream().flush();
Pierre Ossmana40ab202016-04-29 15:35:56 +0200207 // Flushing the socket might release an update that was previously
208 // delayed because of congestion.
209 if (sock->outStream().bufferUsage() == 0)
210 writeFramebufferUpdate();
Pierre Ossmand408ca52016-04-29 14:26:05 +0200211 } catch (rdr::Exception &e) {
212 close(e.str());
213 }
214}
215
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000216void VNCSConnectionST::pixelBufferChange()
217{
218 try {
219 if (!authenticated()) return;
220 if (cp.width && cp.height && (server->pb->width() != cp.width ||
221 server->pb->height() != cp.height))
222 {
223 // We need to clip the next update to the new size, but also add any
224 // extra bits if it's bigger. If we wanted to do this exactly, something
225 // like the code below would do it, but at the moment we just update the
Pierre Ossman671d2ef2015-06-09 16:09:06 +0200226 // entire new size. However, we do need to clip the damagedCursorRegion
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000227 // because that might be added to updates in writeFramebufferUpdate().
228
229 //updates.intersect(server->pb->getRect());
230 //
231 //if (server->pb->width() > cp.width)
232 // updates.add_changed(Rect(cp.width, 0, server->pb->width(),
233 // server->pb->height()));
234 //if (server->pb->height() > cp.height)
235 // updates.add_changed(Rect(0, cp.height, cp.width,
236 // server->pb->height()));
237
Pierre Ossman671d2ef2015-06-09 16:09:06 +0200238 damagedCursorRegion.assign_intersect(server->pb->getRect());
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000239
240 cp.width = server->pb->width();
241 cp.height = server->pb->height();
Pierre Ossman34e62f32009-03-20 21:46:12 +0000242 cp.screenLayout = server->screenLayout;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000243 if (state() == RFBSTATE_NORMAL) {
Pierre Ossman2ee430a2009-05-28 12:54:24 +0000244 // We should only send EDS to client asking for both
245 if (!writer()->writeExtendedDesktopSize()) {
246 if (!writer()->writeSetDesktopSize()) {
247 close("Client does not support desktop resize");
248 return;
249 }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000250 }
251 }
252 }
253 // Just update the whole screen at the moment because we're too lazy to
254 // work out what's actually changed.
255 updates.clear();
256 updates.add_changed(server->pb->getRect());
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000257 writeFramebufferUpdate();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000258 } catch(rdr::Exception &e) {
259 close(e.str());
260 }
261}
262
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000263void VNCSConnectionST::writeFramebufferUpdateOrClose()
Pierre Ossman04e62db2009-03-23 16:57:07 +0000264{
265 try {
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000266 writeFramebufferUpdate();
267 } catch(rdr::Exception &e) {
268 close(e.str());
269 }
270}
Pierre Ossman04e62db2009-03-23 16:57:07 +0000271
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000272void VNCSConnectionST::screenLayoutChangeOrClose(rdr::U16 reason)
273{
274 try {
275 screenLayoutChange(reason);
Pierre Ossman04e62db2009-03-23 16:57:07 +0000276 } catch(rdr::Exception &e) {
277 close(e.str());
278 }
279}
280
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000281void VNCSConnectionST::bellOrClose()
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000282{
283 try {
284 if (state() == RFBSTATE_NORMAL) writer()->writeBell();
285 } catch(rdr::Exception& e) {
286 close(e.str());
287 }
288}
289
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000290void VNCSConnectionST::serverCutTextOrClose(const char *str, int len)
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000291{
292 try {
293 if (!(accessRights & AccessCutText)) return;
294 if (!rfb::Server::sendCutText) return;
295 if (state() == RFBSTATE_NORMAL)
296 writer()->writeServerCutText(str, len);
297 } catch(rdr::Exception& e) {
298 close(e.str());
299 }
300}
301
Peter Ã…strandc39e0782009-01-15 12:21:42 +0000302
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000303void VNCSConnectionST::setDesktopNameOrClose(const char *name)
Peter Ã…strandc39e0782009-01-15 12:21:42 +0000304{
Peter Ã…strandc39e0782009-01-15 12:21:42 +0000305 try {
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000306 setDesktopName(name);
Peter Ã…strandc39e0782009-01-15 12:21:42 +0000307 } catch(rdr::Exception& e) {
308 close(e.str());
309 }
310}
311
312
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000313void VNCSConnectionST::setCursorOrClose()
314{
315 try {
316 setCursor();
317 } catch(rdr::Exception& e) {
318 close(e.str());
319 }
320}
321
322
Pierre Ossmanb45a84f2016-12-12 16:59:15 +0100323void VNCSConnectionST::setLEDStateOrClose(unsigned int state)
324{
325 try {
326 setLEDState(state);
327 } catch(rdr::Exception& e) {
328 close(e.str());
329 }
330}
331
332
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000333int VNCSConnectionST::checkIdleTimeout()
334{
335 int idleTimeout = rfb::Server::idleTimeout;
336 if (idleTimeout == 0) return 0;
337 if (state() != RFBSTATE_NORMAL && idleTimeout < 15)
338 idleTimeout = 15; // minimum of 15 seconds while authenticating
339 time_t now = time(0);
340 if (now < lastEventTime) {
341 // Someone must have set the time backwards. Set lastEventTime so that the
342 // idleTimeout will count from now.
343 vlog.info("Time has gone backwards - resetting idle timeout");
344 lastEventTime = now;
345 }
346 int timeLeft = lastEventTime + idleTimeout - now;
347 if (timeLeft < -60) {
348 // Our callback is over a minute late - someone must have set the time
349 // forwards. Set lastEventTime so that the idleTimeout will count from
350 // now.
351 vlog.info("Time has gone forwards - resetting idle timeout");
352 lastEventTime = now;
353 return secsToMillis(idleTimeout);
354 }
355 if (timeLeft <= 0) {
356 close("Idle timeout");
357 return 0;
358 }
359 return secsToMillis(timeLeft);
360}
361
Pierre Ossmanb114cec2011-11-20 15:36:11 +0000362
363bool VNCSConnectionST::getComparerState()
364{
365 // We interpret a low compression level as an indication that the client
366 // wants to prioritise CPU usage over bandwidth, and hence disable the
367 // comparing update tracker.
368 return (cp.compressLevel == -1) || (cp.compressLevel > 1);
369}
370
371
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000372// renderedCursorChange() is called whenever the server-side rendered cursor
373// changes shape or position. It ensures that the next update will clean up
374// the old rendered cursor and if necessary draw the new rendered cursor.
375
376void VNCSConnectionST::renderedCursorChange()
377{
378 if (state() != RFBSTATE_NORMAL) return;
Pierre Ossman671d2ef2015-06-09 16:09:06 +0200379 if (!damagedCursorRegion.is_empty())
Pierre Ossman5c9e1e52011-11-08 10:32:05 +0000380 removeRenderedCursor = true;
Pierre Ossman6b0bc292011-12-21 13:17:54 +0000381 if (needRenderedCursor()) {
Pierre Ossman671d2ef2015-06-09 16:09:06 +0200382 updateRenderedCursor = true;
Pierre Ossman6b0bc292011-12-21 13:17:54 +0000383 writeFramebufferUpdateOrClose();
384 }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000385}
386
387// needRenderedCursor() returns true if this client needs the server-side
388// rendered cursor. This may be because it does not support local cursor or
389// because the current cursor position has not been set by this client.
390// Unfortunately we can't know for sure when the current cursor position has
391// been set by this client. We guess that this is the case when the current
392// cursor position is the same as the last pointer event from this client, or
393// if it is a very short time since this client's last pointer event (up to a
394// second). [ Ideally we should do finer-grained timing here and make the time
395// configurable, but I don't think it's that important. ]
396
397bool VNCSConnectionST::needRenderedCursor()
398{
Pierre Ossman77ede0a2016-12-05 16:57:30 +0100399 if (state() != RFBSTATE_NORMAL)
400 return false;
401
Pierre Ossman324043e2017-08-16 16:26:11 +0200402 if (!cp.supportsLocalCursorWithAlpha &&
403 !cp.supportsLocalCursor && !cp.supportsLocalXCursor)
Pierre Ossman77ede0a2016-12-05 16:57:30 +0100404 return true;
405 if (!server->cursorPos.equals(pointerEventPos) &&
406 (time(0) - pointerEventTime) > 0)
407 return true;
408
409 return false;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000410}
411
412
413void VNCSConnectionST::approveConnectionOrClose(bool accept,
414 const char* reason)
415{
416 try {
417 approveConnection(accept, reason);
418 } catch (rdr::Exception& e) {
419 close(e.str());
420 }
421}
422
423
424
425// -=- Callbacks from SConnection
426
427void VNCSConnectionST::authSuccess()
428{
429 lastEventTime = time(0);
430
431 server->startDesktop();
432
433 // - Set the connection parameters appropriately
434 cp.width = server->pb->width();
435 cp.height = server->pb->height();
Pierre Ossman34e62f32009-03-20 21:46:12 +0000436 cp.screenLayout = server->screenLayout;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000437 cp.setName(server->getName());
Pierre Ossmanb45a84f2016-12-12 16:59:15 +0100438 cp.setLEDState(server->ledState);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000439
440 // - Set the default pixel format
441 cp.setPF(server->pb->getPF());
442 char buffer[256];
443 cp.pf().print(buffer, 256);
444 vlog.info("Server default pixel format %s", buffer);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000445
446 // - Mark the entire display as "dirty"
447 updates.add_changed(server->pb->getRect());
448 startTime = time(0);
Pierre Ossman1b478e52011-11-15 12:08:30 +0000449
450 // - Bootstrap the congestion control
451 ackedOffset = sock->outStream().length();
452 congWindow = INITIAL_WINDOW;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000453}
454
455void VNCSConnectionST::queryConnection(const char* userName)
456{
457 // - Authentication succeeded - clear from blacklist
458 CharArray name; name.buf = sock->getPeerAddress();
459 server->blHosts->clearBlackmark(name.buf);
460
461 // - Special case to provide a more useful error message
462 if (rfb::Server::neverShared && !rfb::Server::disconnectClients &&
463 server->authClientCount() > 0) {
464 approveConnection(false, "The server is already in use");
465 return;
466 }
467
468 // - Does the client have the right to bypass the query?
469 if (reverseConnection ||
470 !(rfb::Server::queryConnect || sock->requiresQuery()) ||
471 (accessRights & AccessNoQuery))
472 {
473 approveConnection(true);
474 return;
475 }
476
477 // - Get the server to display an Accept/Reject dialog, if required
478 // If a dialog is displayed, the result will be PENDING, and the
479 // server will call approveConnection at a later time
480 CharArray reason;
481 VNCServerST::queryResult qr = server->queryConnection(sock, userName,
482 &reason.buf);
Pierre Ossmanf8e3b342015-01-26 14:37:04 +0100483 if (qr == VNCServerST::PENDING) {
484 queryConnectTimer.start(rfb::Server::queryConnectTimeout * 1000);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000485 return;
Pierre Ossmanf8e3b342015-01-26 14:37:04 +0100486 }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000487
488 // - If server returns ACCEPT/REJECT then pass result to SConnection
489 approveConnection(qr == VNCServerST::ACCEPT, reason.buf);
490}
491
492void VNCSConnectionST::clientInit(bool shared)
493{
494 lastEventTime = time(0);
495 if (rfb::Server::alwaysShared || reverseConnection) shared = true;
Pierre Ossmane7be49b2014-12-02 14:33:17 +0100496 if (!(accessRights & AccessNonShared)) shared = true;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000497 if (rfb::Server::neverShared) shared = false;
498 if (!shared) {
Pierre Ossmane7be49b2014-12-02 14:33:17 +0100499 if (rfb::Server::disconnectClients && (accessRights & AccessNonShared)) {
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000500 // - Close all the other connected clients
501 vlog.debug("non-shared connection - closing clients");
502 server->closeClients("Non-shared connection requested", getSock());
503 } else {
504 // - Refuse this connection if there are existing clients, in addition to
505 // this one
506 if (server->authClientCount() > 1) {
507 close("Server is already in use");
508 return;
509 }
510 }
511 }
512 SConnection::clientInit(shared);
513}
514
515void VNCSConnectionST::setPixelFormat(const PixelFormat& pf)
516{
517 SConnection::setPixelFormat(pf);
518 char buffer[256];
519 pf.print(buffer, 256);
520 vlog.info("Client pixel format %s", buffer);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000521 setCursor();
522}
523
524void VNCSConnectionST::pointerEvent(const Point& pos, int buttonMask)
525{
526 pointerEventTime = lastEventTime = time(0);
527 server->lastUserInputTime = lastEventTime;
528 if (!(accessRights & AccessPtrEvents)) return;
529 if (!rfb::Server::acceptPointerEvents) return;
530 if (!server->pointerClient || server->pointerClient == this) {
531 pointerEventPos = pos;
532 if (buttonMask)
533 server->pointerClient = this;
534 else
535 server->pointerClient = 0;
536 server->desktop->pointerEvent(pointerEventPos, buttonMask);
537 }
538}
539
540
541class VNCSConnectionSTShiftPresser {
542public:
543 VNCSConnectionSTShiftPresser(SDesktop* desktop_)
544 : desktop(desktop_), pressed(false) {}
545 ~VNCSConnectionSTShiftPresser() {
Pierre Ossman9a153b02015-08-31 10:01:14 +0200546 if (pressed) {
547 vlog.debug("Releasing fake Shift_L");
Pierre Ossman5ae28212017-05-16 14:30:38 +0200548 desktop->keyEvent(XK_Shift_L, 0, false);
Pierre Ossman9a153b02015-08-31 10:01:14 +0200549 }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000550 }
551 void press() {
Pierre Ossman9a153b02015-08-31 10:01:14 +0200552 vlog.debug("Pressing fake Shift_L");
Pierre Ossman5ae28212017-05-16 14:30:38 +0200553 desktop->keyEvent(XK_Shift_L, 0, true);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000554 pressed = true;
555 }
556 SDesktop* desktop;
557 bool pressed;
558};
559
560// keyEvent() - record in the pressedKeys which keys were pressed. Allow
561// multiple down events (for autorepeat), but only allow a single up event.
Pierre Ossman5ae28212017-05-16 14:30:38 +0200562void VNCSConnectionST::keyEvent(rdr::U32 keysym, rdr::U32 keycode, bool down) {
Pierre Ossman16e1dcb2017-05-16 14:33:43 +0200563 rdr::U32 lookup;
564
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000565 lastEventTime = time(0);
566 server->lastUserInputTime = lastEventTime;
567 if (!(accessRights & AccessKeyEvents)) return;
568 if (!rfb::Server::acceptKeyEvents) return;
569
Pierre Ossman9a153b02015-08-31 10:01:14 +0200570 if (down)
Pierre Ossman5ae28212017-05-16 14:30:38 +0200571 vlog.debug("Key pressed: 0x%x / 0x%x", keysym, keycode);
Pierre Ossman9a153b02015-08-31 10:01:14 +0200572 else
Pierre Ossman5ae28212017-05-16 14:30:38 +0200573 vlog.debug("Key released: 0x%x / 0x%x", keysym, keycode);
Pierre Ossman9a153b02015-08-31 10:01:14 +0200574
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000575 // Remap the key if required
Pierre Ossman9a153b02015-08-31 10:01:14 +0200576 if (server->keyRemapper) {
577 rdr::U32 newkey;
Pierre Ossman5ae28212017-05-16 14:30:38 +0200578 newkey = server->keyRemapper->remapKey(keysym);
579 if (newkey != keysym) {
Pierre Ossman9a153b02015-08-31 10:01:14 +0200580 vlog.debug("Key remapped to 0x%x", newkey);
Pierre Ossman5ae28212017-05-16 14:30:38 +0200581 keysym = newkey;
Pierre Ossman9a153b02015-08-31 10:01:14 +0200582 }
583 }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000584
Pierre Ossmanbb305ca2016-12-11 12:41:26 +0100585 // Avoid lock keys if we don't know the server state
586 if ((server->ledState == ledUnknown) &&
Pierre Ossman5ae28212017-05-16 14:30:38 +0200587 ((keysym == XK_Caps_Lock) ||
588 (keysym == XK_Num_Lock) ||
589 (keysym == XK_Scroll_Lock))) {
Pierre Ossmanbb305ca2016-12-11 12:41:26 +0100590 vlog.debug("Ignoring lock key (e.g. caps lock)");
591 return;
592 }
Pierre Ossmanbb305ca2016-12-11 12:41:26 +0100593
Pierre Ossmanb45a84f2016-12-12 16:59:15 +0100594 // Lock key heuristics
595 // (only for clients that do not support the LED state extension)
596 if (!cp.supportsLEDState) {
597 // Always ignore ScrollLock as we don't have a heuristic
598 // for that
Pierre Ossman5ae28212017-05-16 14:30:38 +0200599 if (keysym == XK_Scroll_Lock) {
Pierre Ossmanb45a84f2016-12-12 16:59:15 +0100600 vlog.debug("Ignoring lock key (e.g. caps lock)");
601 return;
Pierre Ossmanbb305ca2016-12-11 12:41:26 +0100602 }
603
Pierre Ossmanb45a84f2016-12-12 16:59:15 +0100604 if (down && (server->ledState != ledUnknown)) {
605 // CapsLock synchronisation heuristic
606 // (this assumes standard interaction between CapsLock the Shift
607 // keys and normal characters)
Pierre Ossman5ae28212017-05-16 14:30:38 +0200608 if (((keysym >= XK_A) && (keysym <= XK_Z)) ||
609 ((keysym >= XK_a) && (keysym <= XK_z))) {
Pierre Ossmanb45a84f2016-12-12 16:59:15 +0100610 bool uppercase, shift, lock;
Pierre Ossmanbb305ca2016-12-11 12:41:26 +0100611
Pierre Ossman5ae28212017-05-16 14:30:38 +0200612 uppercase = (keysym >= XK_A) && (keysym <= XK_Z);
Pierre Ossmanb45a84f2016-12-12 16:59:15 +0100613 shift = pressedKeys.find(XK_Shift_L) != pressedKeys.end() ||
614 pressedKeys.find(XK_Shift_R) != pressedKeys.end();
615 lock = server->ledState & ledCapsLock;
Pierre Ossmanbb305ca2016-12-11 12:41:26 +0100616
Pierre Ossmanb45a84f2016-12-12 16:59:15 +0100617 if (lock == (uppercase == shift)) {
618 vlog.debug("Inserting fake CapsLock to get in sync with client");
Pierre Ossman5ae28212017-05-16 14:30:38 +0200619 server->desktop->keyEvent(XK_Caps_Lock, 0, true);
620 server->desktop->keyEvent(XK_Caps_Lock, 0, false);
Pierre Ossmanb45a84f2016-12-12 16:59:15 +0100621 }
622 }
623
624 // NumLock synchronisation heuristic
625 // (this is more cautious because of the differences between Unix,
626 // Windows and macOS)
Pierre Ossman5ae28212017-05-16 14:30:38 +0200627 if (((keysym >= XK_KP_Home) && (keysym <= XK_KP_Delete)) ||
628 ((keysym >= XK_KP_0) && (keysym <= XK_KP_9)) ||
629 (keysym == XK_KP_Separator) || (keysym == XK_KP_Decimal)) {
Pierre Ossmanb45a84f2016-12-12 16:59:15 +0100630 bool number, shift, lock;
631
Pierre Ossman5ae28212017-05-16 14:30:38 +0200632 number = ((keysym >= XK_KP_0) && (keysym <= XK_KP_9)) ||
633 (keysym == XK_KP_Separator) || (keysym == XK_KP_Decimal);
Pierre Ossmanb45a84f2016-12-12 16:59:15 +0100634 shift = pressedKeys.find(XK_Shift_L) != pressedKeys.end() ||
635 pressedKeys.find(XK_Shift_R) != pressedKeys.end();
636 lock = server->ledState & ledNumLock;
637
638 if (shift) {
639 // We don't know the appropriate NumLock state for when Shift
640 // is pressed as it could be one of:
641 //
642 // a) A Unix client where Shift negates NumLock
643 //
644 // b) A Windows client where Shift only cancels NumLock
645 //
646 // c) A macOS client where Shift doesn't have any effect
647 //
648 } else if (lock == (number == shift)) {
649 vlog.debug("Inserting fake NumLock to get in sync with client");
Pierre Ossman5ae28212017-05-16 14:30:38 +0200650 server->desktop->keyEvent(XK_Num_Lock, 0, true);
651 server->desktop->keyEvent(XK_Num_Lock, 0, false);
Pierre Ossmanb45a84f2016-12-12 16:59:15 +0100652 }
Pierre Ossmanbb305ca2016-12-11 12:41:26 +0100653 }
654 }
655 }
656
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000657 // Turn ISO_Left_Tab into shifted Tab.
658 VNCSConnectionSTShiftPresser shiftPresser(server->desktop);
Pierre Ossman5ae28212017-05-16 14:30:38 +0200659 if (keysym == XK_ISO_Left_Tab) {
Pierre Ossman16e1dcb2017-05-16 14:33:43 +0200660 std::map<rdr::U32, rdr::U32>::const_iterator iter;
661 bool shifted;
662
663 shifted = false;
664 for (iter = pressedKeys.begin(); iter != pressedKeys.end(); ++iter) {
665 if ((iter->second == XK_Shift_L) ||
666 (iter->second == XK_Shift_R)) {
667 shifted = true;
668 break;
669 }
670 }
671
672 if (!shifted)
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000673 shiftPresser.press();
Pierre Ossman16e1dcb2017-05-16 14:33:43 +0200674
Pierre Ossman5ae28212017-05-16 14:30:38 +0200675 keysym = XK_Tab;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000676 }
677
Pierre Ossman16e1dcb2017-05-16 14:33:43 +0200678 // We need to be able to track keys, so generate a fake index when we
679 // aren't given a keycode
680 if (keycode == 0)
681 lookup = 0x80000000 | keysym;
682 else
683 lookup = keycode;
684
685 // We force the same keysym for an already down key for the
686 // sake of sanity
687 if (pressedKeys.find(lookup) != pressedKeys.end())
688 keysym = pressedKeys[lookup];
689
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000690 if (down) {
Pierre Ossman16e1dcb2017-05-16 14:33:43 +0200691 pressedKeys[lookup] = keysym;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000692 } else {
Pierre Ossman16e1dcb2017-05-16 14:33:43 +0200693 if (!pressedKeys.erase(lookup))
Pierre Ossman5ae28212017-05-16 14:30:38 +0200694 return;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000695 }
Pierre Ossman16e1dcb2017-05-16 14:33:43 +0200696
Pierre Ossman5ae28212017-05-16 14:30:38 +0200697 server->desktop->keyEvent(keysym, keycode, down);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000698}
699
700void VNCSConnectionST::clientCutText(const char* str, int len)
701{
702 if (!(accessRights & AccessCutText)) return;
703 if (!rfb::Server::acceptCutText) return;
704 server->desktop->clientCutText(str, len);
705}
706
707void VNCSConnectionST::framebufferUpdateRequest(const Rect& r,bool incremental)
708{
Pierre Ossmane9962f72009-04-23 12:31:42 +0000709 Rect safeRect;
710
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000711 if (!(accessRights & AccessView)) return;
712
713 SConnection::framebufferUpdateRequest(r, incremental);
714
Pierre Ossmand9a59ba2009-03-20 15:55:37 +0000715 // Check that the client isn't sending crappy requests
716 if (!r.enclosed_by(Rect(0, 0, cp.width, cp.height))) {
717 vlog.error("FramebufferUpdateRequest %dx%d at %d,%d exceeds framebuffer %dx%d",
718 r.width(), r.height(), r.tl.x, r.tl.y, cp.width, cp.height);
Pierre Ossmane9962f72009-04-23 12:31:42 +0000719 safeRect = r.intersect(Rect(0, 0, cp.width, cp.height));
720 } else {
721 safeRect = r;
Pierre Ossmand9a59ba2009-03-20 15:55:37 +0000722 }
723
Constantin Kaplinsky2ef66952008-08-29 11:33:46 +0000724 // Just update the requested region.
725 // Framebuffer update will be sent a bit later, see processMessages().
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000726 Region reqRgn(r);
Pierre Ossman1b478e52011-11-15 12:08:30 +0000727 if (!incremental || !continuousUpdates)
728 requested.assign_union(reqRgn);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000729
730 if (!incremental) {
731 // Non-incremental update - treat as if area requested has changed
732 updates.add_changed(reqRgn);
Pierre Ossman53125a72009-04-22 15:37:51 +0000733
734 // And send the screen layout to the client (which, unlike the
735 // framebuffer dimensions, the client doesn't get during init)
Pierre Ossmanc5e25602009-03-20 12:59:05 +0000736 writer()->writeExtendedDesktopSize();
Pierre Ossman53125a72009-04-22 15:37:51 +0000737
738 // We do not send a DesktopSize since it only contains the
739 // framebuffer size (which the client already should know) and
740 // because some clients don't handle extra DesktopSize events
741 // very well.
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000742 }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000743}
744
Pierre Ossman34bb0612009-03-21 21:16:14 +0000745void VNCSConnectionST::setDesktopSize(int fb_width, int fb_height,
746 const ScreenSet& layout)
Pierre Ossmanc5e25602009-03-20 12:59:05 +0000747{
Pierre Ossman04e62db2009-03-23 16:57:07 +0000748 unsigned int result;
749
Michal Srbb318b8f2014-11-24 13:18:28 +0200750 if (!(accessRights & AccessSetDesktopSize)) return;
751 if (!rfb::Server::acceptSetDesktopSize) return;
752
Pierre Ossman04e62db2009-03-23 16:57:07 +0000753 // Don't bother the desktop with an invalid configuration
754 if (!layout.validate(fb_width, fb_height)) {
755 writer()->writeExtendedDesktopSize(reasonClient, resultInvalid,
756 fb_width, fb_height, layout);
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000757 writeFramebufferUpdate();
Pierre Ossman04e62db2009-03-23 16:57:07 +0000758 return;
759 }
760
761 // FIXME: the desktop will call back to VNCServerST and an extra set
762 // of ExtendedDesktopSize messages will be sent. This is okay
763 // protocol-wise, but unnecessary.
764 result = server->desktop->setScreenLayout(fb_width, fb_height, layout);
765
Pierre Ossman04e62db2009-03-23 16:57:07 +0000766 writer()->writeExtendedDesktopSize(reasonClient, result,
767 fb_width, fb_height, layout);
Pierre Ossman04e62db2009-03-23 16:57:07 +0000768
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000769 // Only notify other clients on success
Pierre Ossman04e62db2009-03-23 16:57:07 +0000770 if (result == resultSuccess) {
771 if (server->screenLayout != layout)
772 throw Exception("Desktop configured a different screen layout than requested");
773 server->notifyScreenLayoutChange(this);
774 }
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000775
776 // but always send back a reply to the requesting client
777 // (do this last as it might throw an exception on socket errors)
778 writeFramebufferUpdate();
Pierre Ossmanc5e25602009-03-20 12:59:05 +0000779}
780
Pierre Ossman2c764942011-11-14 15:54:30 +0000781void VNCSConnectionST::fence(rdr::U32 flags, unsigned len, const char data[])
782{
783 if (flags & fenceFlagRequest) {
784 if (flags & fenceFlagSyncNext) {
Pierre Ossmanb8b1e962012-07-20 10:47:00 +0000785 pendingSyncFence = true;
Pierre Ossman2c764942011-11-14 15:54:30 +0000786
787 fenceFlags = flags & (fenceFlagBlockBefore | fenceFlagBlockAfter | fenceFlagSyncNext);
788 fenceDataLen = len;
789 delete [] fenceData;
Michal Srbf3afa242017-03-27 19:02:15 +0300790 fenceData = NULL;
Pierre Ossman2c764942011-11-14 15:54:30 +0000791 if (len > 0) {
792 fenceData = new char[len];
793 memcpy(fenceData, data, len);
794 }
795
796 return;
797 }
798
799 // We handle everything synchronously so we trivially honor these modes
800 flags = flags & (fenceFlagBlockBefore | fenceFlagBlockAfter);
801
802 writer()->writeFence(flags, len, data);
803 return;
804 }
805
Pierre Ossman1b478e52011-11-15 12:08:30 +0000806 struct RTTInfo rttInfo;
807
Pierre Ossman2c764942011-11-14 15:54:30 +0000808 switch (len) {
809 case 0:
810 // Initial dummy fence;
811 break;
Pierre Ossman1b478e52011-11-15 12:08:30 +0000812 case sizeof(struct RTTInfo):
813 memcpy(&rttInfo, data, sizeof(struct RTTInfo));
814 handleRTTPong(rttInfo);
815 break;
Pierre Ossman2c764942011-11-14 15:54:30 +0000816 default:
817 vlog.error("Fence response of unexpected size received");
818 }
819}
820
Pierre Ossman1b478e52011-11-15 12:08:30 +0000821void VNCSConnectionST::enableContinuousUpdates(bool enable,
822 int x, int y, int w, int h)
823{
824 Rect rect;
825
826 if (!cp.supportsFence || !cp.supportsContinuousUpdates)
827 throw Exception("Client tried to enable continuous updates when not allowed");
828
829 continuousUpdates = enable;
830
831 rect.setXYWH(x, y, w, h);
832 cuRegion.reset(rect);
833
834 if (enable) {
835 requested.clear();
836 writeFramebufferUpdate();
837 } else {
838 writer()->writeEndOfContinuousUpdates();
839 }
840}
841
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000842// supportsLocalCursor() is called whenever the status of
843// cp.supportsLocalCursor has changed. If the client does now support local
844// cursor, we make sure that the old server-side rendered cursor is cleaned up
845// and the cursor is sent to the client.
846
847void VNCSConnectionST::supportsLocalCursor()
848{
Pierre Ossman324043e2017-08-16 16:26:11 +0200849 if (cp.supportsLocalCursorWithAlpha ||
850 cp.supportsLocalCursor || cp.supportsLocalXCursor) {
Pierre Ossman671d2ef2015-06-09 16:09:06 +0200851 if (!damagedCursorRegion.is_empty())
Pierre Ossman5c9e1e52011-11-08 10:32:05 +0000852 removeRenderedCursor = true;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000853 setCursor();
854 }
855}
856
Pierre Ossman2c764942011-11-14 15:54:30 +0000857void VNCSConnectionST::supportsFence()
858{
859 writer()->writeFence(fenceFlagRequest, 0, NULL);
860}
861
Pierre Ossman1b478e52011-11-15 12:08:30 +0000862void VNCSConnectionST::supportsContinuousUpdates()
863{
864 // We refuse to use continuous updates if we cannot monitor the buffer
865 // usage using fences.
866 if (!cp.supportsFence)
867 return;
868
869 writer()->writeEndOfContinuousUpdates();
870}
871
Pierre Ossmanb45a84f2016-12-12 16:59:15 +0100872void VNCSConnectionST::supportsLEDState()
873{
874 writer()->writeLEDState();
875}
876
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000877
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +0000878bool VNCSConnectionST::handleTimeout(Timer* t)
879{
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000880 try {
Pierre Ossmana40ab202016-04-29 15:35:56 +0200881 if (t == &congestionTimer)
Pierre Ossman1b478e52011-11-15 12:08:30 +0000882 updateCongestion();
Pierre Ossmanf8e3b342015-01-26 14:37:04 +0100883 else if (t == &queryConnectTimer) {
884 if (state() == RFBSTATE_QUERYING)
885 approveConnection(false, "The attempt to prompt the user to accept the connection failed");
886 }
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000887 } catch (rdr::Exception& e) {
888 close(e.str());
889 }
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +0000890
891 return false;
892}
893
894
Pierre Ossman1b478e52011-11-15 12:08:30 +0000895void VNCSConnectionST::writeRTTPing()
896{
897 struct RTTInfo rttInfo;
898
899 if (!cp.supportsFence)
900 return;
901
902 memset(&rttInfo, 0, sizeof(struct RTTInfo));
903
904 gettimeofday(&rttInfo.tv, NULL);
905 rttInfo.offset = sock->outStream().length();
906 rttInfo.inFlight = rttInfo.offset - ackedOffset;
907
908 // We need to make sure any old update are already processed by the
909 // time we get the response back. This allows us to reliably throttle
910 // back on client overload, as well as network overload.
911 writer()->writeFence(fenceFlagRequest | fenceFlagBlockBefore,
912 sizeof(struct RTTInfo), (const char*)&rttInfo);
913
914 pingCounter++;
915
916 sentOffset = rttInfo.offset;
917
918 // Let some data flow before we adjust the settings
919 if (!congestionTimer.isStarted())
920 congestionTimer.start(__rfbmin(baseRTT * 2, 100));
921}
922
923void VNCSConnectionST::handleRTTPong(const struct RTTInfo &rttInfo)
924{
925 unsigned rtt, delay;
Pierre Ossman1b478e52011-11-15 12:08:30 +0000926
927 pingCounter--;
928
929 rtt = msSince(&rttInfo.tv);
930 if (rtt < 1)
931 rtt = 1;
932
933 ackedOffset = rttInfo.offset;
934
935 // Try to estimate wire latency by tracking lowest seen latency
936 if (rtt < baseRTT)
937 baseRTT = rtt;
938
939 if (rttInfo.inFlight > congWindow) {
940 seenCongestion = true;
941
942 // Estimate added delay because of overtaxed buffers
943 delay = (rttInfo.inFlight - congWindow) * baseRTT / congWindow;
944
945 if (delay < rtt)
946 rtt -= delay;
947 else
948 rtt = 1;
949
950 // If we underestimate the congestion window, then we'll get a latency
951 // that's less than the wire latency, which will confuse other portions
952 // of the code.
953 if (rtt < baseRTT)
954 rtt = baseRTT;
955 }
956
957 // We only keep track of the minimum latency seen (for a given interval)
klemens0536d092017-01-28 20:56:56 +0100958 // on the basis that we want to avoid continuous buffer issue, but don't
Pierre Ossman1b478e52011-11-15 12:08:30 +0000959 // mind (or even approve of) bursts.
960 if (rtt < minRTT)
961 minRTT = rtt;
962}
963
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +0000964bool VNCSConnectionST::isCongested()
965{
Pierre Ossman1b478e52011-11-15 12:08:30 +0000966 int offset;
967
968 // Stuff still waiting in the send buffer?
Pierre Ossman352d0622016-04-29 15:50:54 +0200969 sock->outStream().flush();
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +0000970 if (sock->outStream().bufferUsage() > 0)
971 return true;
972
Pierre Ossman1b478e52011-11-15 12:08:30 +0000973 if (!cp.supportsFence)
974 return false;
975
976 // Idle for too long? (and no data on the wire)
977 //
978 // FIXME: This should really just be one baseRTT, but we're getting
979 // problems with triggering the idle timeout on each update.
980 // Maybe we need to use a moving average for the wire latency
981 // instead of baseRTT.
982 if ((sentOffset == ackedOffset) &&
983 (sock->outStream().getIdleTime() > 2 * baseRTT)) {
984
985#ifdef CONGESTION_DEBUG
986 if (congWindow > INITIAL_WINDOW)
987 fprintf(stderr, "Reverting to initial window (%d KiB) after %d ms\n",
988 INITIAL_WINDOW / 1024, sock->outStream().getIdleTime());
989#endif
990
991 // Close congestion window and allow a transfer
992 // FIXME: Reset baseRTT like Linux Vegas?
993 congWindow = __rfbmin(INITIAL_WINDOW, congWindow);
994
995 return false;
996 }
997
998 offset = sock->outStream().length();
999
1000 // FIXME: Should we compensate for non-update data?
1001 // (i.e. use sentOffset instead of offset)
1002 if ((offset - ackedOffset) < congWindow)
1003 return false;
1004
1005 // If we just have one outstanding "ping", that means the client has
1006 // started receiving our update. In order to not regress compared to
1007 // before we had congestion avoidance, we allow another update here.
1008 // This could further clog up the tubes, but congestion control isn't
1009 // really working properly right now anyway as the wire would otherwise
1010 // be idle for at least RTT/2.
1011 if (pingCounter == 1)
1012 return false;
1013
1014 return true;
1015}
1016
1017
1018void VNCSConnectionST::updateCongestion()
1019{
1020 unsigned diff;
1021
1022 if (!seenCongestion)
1023 return;
1024
1025 diff = minRTT - baseRTT;
1026
1027 if (diff > __rfbmin(100, baseRTT)) {
1028 // Way too fast
1029 congWindow = congWindow * baseRTT / minRTT;
1030 } else if (diff > __rfbmin(50, baseRTT/2)) {
1031 // Slightly too fast
1032 congWindow -= 4096;
1033 } else if (diff < 5) {
1034 // Way too slow
1035 congWindow += 8192;
1036 } else if (diff < 25) {
1037 // Too slow
1038 congWindow += 4096;
1039 }
1040
1041 if (congWindow < MINIMUM_WINDOW)
1042 congWindow = MINIMUM_WINDOW;
1043 if (congWindow > MAXIMUM_WINDOW)
1044 congWindow = MAXIMUM_WINDOW;
1045
1046#ifdef CONGESTION_DEBUG
1047 fprintf(stderr, "RTT: %d ms (%d ms), Window: %d KiB, Bandwidth: %g Mbps\n",
1048 minRTT, baseRTT, congWindow / 1024,
1049 congWindow * 8.0 / baseRTT / 1000.0);
1050
1051#ifdef TCP_INFO
1052 struct tcp_info tcp_info;
1053 socklen_t tcp_info_length;
1054
1055 tcp_info_length = sizeof(tcp_info);
1056 if (getsockopt(sock->getFd(), SOL_TCP, TCP_INFO,
1057 (void *)&tcp_info, &tcp_info_length) == 0) {
1058 fprintf(stderr, "Socket: RTT: %d ms (+/- %d ms) Window %d KiB\n",
1059 tcp_info.tcpi_rtt / 1000, tcp_info.tcpi_rttvar / 1000,
1060 tcp_info.tcpi_snd_mss * tcp_info.tcpi_snd_cwnd / 1024);
1061 }
1062#endif
1063
1064#endif
1065
1066 minRTT = -1;
1067 seenCongestion = false;
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +00001068}
1069
1070
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001071void VNCSConnectionST::writeFramebufferUpdate()
1072{
Pierre Ossman2c764942011-11-14 15:54:30 +00001073 // We're in the middle of processing a command that's supposed to be
1074 // synchronised. Allowing an update to slip out right now might violate
1075 // that synchronisation.
1076 if (syncFence)
1077 return;
1078
Pierre Ossmana3ac01e2011-11-07 21:13:54 +00001079 // We try to aggregate responses, so don't send out anything whilst we
1080 // still have incoming messages. processMessages() will give us another
1081 // chance to run once things are idle.
1082 if (inProcessMessages)
1083 return;
1084
Pierre Ossman1b478e52011-11-15 12:08:30 +00001085 if (state() != RFBSTATE_NORMAL)
1086 return;
1087 if (requested.is_empty() && !continuousUpdates)
Pierre Ossmane9962f72009-04-23 12:31:42 +00001088 return;
Pierre Ossmand9a59ba2009-03-20 15:55:37 +00001089
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +00001090 // Check that we actually have some space on the link and retry in a
1091 // bit if things are congested.
Pierre Ossmana40ab202016-04-29 15:35:56 +02001092 if (isCongested())
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +00001093 return;
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +00001094
Pierre Ossmancef3cf72016-11-25 10:06:34 +01001095 // Updates often consists of many small writes, and in continuous
1096 // mode, we will also have small fence messages around the update. We
1097 // need to aggregate these in order to not clog up TCP's congestion
1098 // window.
Pierre Ossman36dadf82011-11-15 12:11:32 +00001099 network::TcpSocket::cork(sock->getFd(), true);
1100
Pierre Ossmane9962f72009-04-23 12:31:42 +00001101 // First take care of any updates that cannot contain framebuffer data
1102 // changes.
Pierre Ossmancef3cf72016-11-25 10:06:34 +01001103 writeNoDataUpdate();
1104
1105 // Then real data (if possible)
1106 writeDataUpdate();
1107
1108 network::TcpSocket::cork(sock->getFd(), false);
1109}
1110
1111void VNCSConnectionST::writeNoDataUpdate()
1112{
1113 if (!writer()->needNoDataUpdate())
1114 return;
1115
1116 writer()->writeNoDataUpdate();
1117
1118 // Make sure no data update is sent until next request
1119 requested.clear();
1120}
1121
1122void VNCSConnectionST::writeDataUpdate()
1123{
1124 Region req;
1125 UpdateInfo ui;
1126 bool needNewUpdateInfo;
Pierre Ossman24684e52016-12-05 16:58:19 +01001127 const RenderedCursor *cursor;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001128
Constantin Kaplinsky604d7812007-08-31 15:50:37 +00001129 updates.enable_copyrect(cp.useCopyRect);
1130
Pierre Ossman6e49e952016-10-07 15:59:38 +02001131 // See if we are allowed to send anything right now (the framebuffer
1132 // might have changed in ways we haven't yet been informed of).
Pierre Ossmanbbf955e2011-11-08 12:44:10 +00001133 if (!server->checkUpdate())
Pierre Ossmancef3cf72016-11-25 10:06:34 +01001134 return;
Constantin Kaplinskya09dc142008-12-18 12:08:15 +00001135
Pierre Ossmancef3cf72016-11-25 10:06:34 +01001136 // See what the client has requested (if anything)
Pierre Ossman1b478e52011-11-15 12:08:30 +00001137 if (continuousUpdates)
1138 req = cuRegion.union_(requested);
1139 else
1140 req = requested;
1141
Pierre Ossmancef3cf72016-11-25 10:06:34 +01001142 if (req.is_empty())
1143 return;
1144
1145 // Get the lists of updates. Prior to exporting the data to the `ui' object,
1146 // getUpdateInfo() will normalize the `updates' object such way that its
1147 // `changed' and `copied' regions would not intersect.
Pierre Ossman1b478e52011-11-15 12:08:30 +00001148 updates.getUpdateInfo(&ui, req);
1149 needNewUpdateInfo = false;
Constantin Kaplinsky604d7812007-08-31 15:50:37 +00001150
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001151 // If the previous position of the rendered cursor overlaps the source of the
1152 // copy, then when the copy happens the corresponding rectangle in the
1153 // destination will be wrong, so add it to the changed region.
1154
Pierre Ossman671d2ef2015-06-09 16:09:06 +02001155 if (!ui.copied.is_empty() && !damagedCursorRegion.is_empty()) {
1156 Region bogusCopiedCursor;
1157
1158 bogusCopiedCursor.copyFrom(damagedCursorRegion);
1159 bogusCopiedCursor.translate(ui.copy_delta);
1160 bogusCopiedCursor.assign_intersect(server->pb->getRect());
Constantin Kaplinsky45517c82007-08-31 15:56:33 +00001161 if (!ui.copied.intersect(bogusCopiedCursor).is_empty()) {
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001162 updates.add_changed(bogusCopiedCursor);
Constantin Kaplinsky604d7812007-08-31 15:50:37 +00001163 needNewUpdateInfo = true;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001164 }
1165 }
1166
Pierre Ossman671d2ef2015-06-09 16:09:06 +02001167 // If we need to remove the old rendered cursor, just add the region to
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001168 // the changed region.
1169
1170 if (removeRenderedCursor) {
Pierre Ossman671d2ef2015-06-09 16:09:06 +02001171 updates.add_changed(damagedCursorRegion);
Constantin Kaplinsky604d7812007-08-31 15:50:37 +00001172 needNewUpdateInfo = true;
Pierre Ossman671d2ef2015-06-09 16:09:06 +02001173 damagedCursorRegion.clear();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001174 removeRenderedCursor = false;
1175 }
1176
1177 // Return if there is nothing to send the client.
1178
Pierre Ossman671d2ef2015-06-09 16:09:06 +02001179 if (updates.is_empty() && !writer()->needFakeUpdate() && !updateRenderedCursor)
Pierre Ossmancef3cf72016-11-25 10:06:34 +01001180 return;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001181
Constantin Kaplinsky604d7812007-08-31 15:50:37 +00001182 // The `updates' object could change, make sure we have valid update info.
1183
1184 if (needNewUpdateInfo)
Pierre Ossman1b478e52011-11-15 12:08:30 +00001185 updates.getUpdateInfo(&ui, req);
Constantin Kaplinsky604d7812007-08-31 15:50:37 +00001186
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001187 // If the client needs a server-side rendered cursor, work out the cursor
1188 // rectangle. If it's empty then don't bother drawing it, but if it overlaps
1189 // with the update region, we need to draw the rendered cursor regardless of
1190 // whether it has changed.
1191
Pierre Ossman24684e52016-12-05 16:58:19 +01001192 cursor = NULL;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001193 if (needRenderedCursor()) {
Pierre Ossman671d2ef2015-06-09 16:09:06 +02001194 Rect renderedCursorRect;
1195
Pierre Ossman24684e52016-12-05 16:58:19 +01001196 cursor = server->getRenderedCursor();
1197
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001198 renderedCursorRect
Pierre Ossman24684e52016-12-05 16:58:19 +01001199 = cursor->getEffectiveRect().intersect(req.get_bounding_rect());
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001200
1201 if (renderedCursorRect.is_empty()) {
Pierre Ossman24684e52016-12-05 16:58:19 +01001202 cursor = NULL;
1203 } else if (!updateRenderedCursor &&
1204 ui.changed.union_(ui.copied)
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001205 .intersect(renderedCursorRect).is_empty()) {
Pierre Ossman24684e52016-12-05 16:58:19 +01001206 cursor = NULL;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001207 }
1208
Pierre Ossman5c037202016-12-05 17:00:35 +01001209 if (cursor) {
1210 updates.subtract(renderedCursorRect);
1211 updates.getUpdateInfo(&ui, req);
1212 }
Pierre Ossman671d2ef2015-06-09 16:09:06 +02001213
1214 damagedCursorRegion.assign_union(renderedCursorRect);
1215 updateRenderedCursor = false;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001216 }
1217
Pierre Ossman24684e52016-12-05 16:58:19 +01001218 if (ui.is_empty() && !writer()->needFakeUpdate() && !cursor)
Pierre Ossmancef3cf72016-11-25 10:06:34 +01001219 return;
Pierre Ossmanfdba3fe2014-01-31 13:12:18 +01001220
Pierre Ossmancef3cf72016-11-25 10:06:34 +01001221 writeRTTPing();
Pierre Ossman1b478e52011-11-15 12:08:30 +00001222
Pierre Ossmancef3cf72016-11-25 10:06:34 +01001223 encodeManager.writeUpdate(ui, server->getPixelBuffer(), cursor);
Pierre Ossman1b478e52011-11-15 12:08:30 +00001224
Pierre Ossmancef3cf72016-11-25 10:06:34 +01001225 writeRTTPing();
Pierre Ossmanc0397262014-03-14 15:59:46 +01001226
Pierre Ossmancef3cf72016-11-25 10:06:34 +01001227 // The request might be for just part of the screen, so we cannot
1228 // just clear the entire update tracker.
1229 updates.subtract(req);
Pierre Ossmanc0397262014-03-14 15:59:46 +01001230
Pierre Ossmancef3cf72016-11-25 10:06:34 +01001231 requested.clear();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001232}
1233
1234
Pierre Ossmana3ac01e2011-11-07 21:13:54 +00001235void VNCSConnectionST::screenLayoutChange(rdr::U16 reason)
1236{
1237 if (!authenticated())
1238 return;
1239
1240 cp.screenLayout = server->screenLayout;
1241
1242 if (state() != RFBSTATE_NORMAL)
1243 return;
1244
1245 writer()->writeExtendedDesktopSize(reason, 0, cp.width, cp.height,
1246 cp.screenLayout);
1247 writeFramebufferUpdate();
1248}
1249
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001250
1251// setCursor() is called whenever the cursor has changed shape or pixel format.
1252// If the client supports local cursor then it will arrange for the cursor to
1253// be sent to the client.
1254
1255void VNCSConnectionST::setCursor()
1256{
Pierre Ossmana3ac01e2011-11-07 21:13:54 +00001257 if (state() != RFBSTATE_NORMAL)
1258 return;
Pierre Ossmana3ac01e2011-11-07 21:13:54 +00001259
Pierre Ossman6a1a0d02017-02-19 15:48:17 +01001260 cp.setCursor(*server->cursor);
Pierre Ossman126e5642014-02-13 14:40:25 +01001261
Pierre Ossman8053c8e2017-02-21 12:59:04 +01001262 if (!writer()->writeSetCursorWithAlpha()) {
1263 if (!writer()->writeSetCursor()) {
1264 if (!writer()->writeSetXCursor()) {
1265 // No client support
1266 return;
1267 }
Pierre Ossman126e5642014-02-13 14:40:25 +01001268 }
1269 }
1270
Pierre Ossmana3ac01e2011-11-07 21:13:54 +00001271 writeFramebufferUpdate();
1272}
1273
1274void VNCSConnectionST::setDesktopName(const char *name)
1275{
1276 cp.setName(name);
1277
1278 if (state() != RFBSTATE_NORMAL)
1279 return;
1280
1281 if (!writer()->writeSetDesktopName()) {
1282 fprintf(stderr, "Client does not support desktop rename\n");
1283 return;
1284 }
1285
1286 writeFramebufferUpdate();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001287}
1288
Pierre Ossmanb45a84f2016-12-12 16:59:15 +01001289void VNCSConnectionST::setLEDState(unsigned int ledstate)
1290{
1291 if (state() != RFBSTATE_NORMAL)
1292 return;
1293
1294 cp.setLEDState(ledstate);
1295
1296 if (!writer()->writeLEDState()) {
1297 // No client support
1298 return;
1299 }
1300
1301 writeFramebufferUpdate();
1302}
1303
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001304void VNCSConnectionST::setSocketTimeouts()
1305{
1306 int timeoutms = rfb::Server::clientWaitTimeMillis;
1307 soonestTimeout(&timeoutms, secsToMillis(rfb::Server::idleTimeout));
1308 if (timeoutms == 0)
1309 timeoutms = -1;
1310 sock->inStream().setTimeout(timeoutms);
1311 sock->outStream().setTimeout(timeoutms);
1312}
1313
1314char* VNCSConnectionST::getStartTime()
1315{
1316 char* result = ctime(&startTime);
1317 result[24] = '\0';
1318 return result;
1319}
1320
1321void VNCSConnectionST::setStatus(int status)
1322{
1323 switch (status) {
1324 case 0:
1325 accessRights = accessRights | AccessPtrEvents | AccessKeyEvents | AccessView;
1326 break;
1327 case 1:
Pierre Ossman7728be22015-03-03 16:39:37 +01001328 accessRights = (accessRights & ~(AccessPtrEvents | AccessKeyEvents)) | AccessView;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001329 break;
1330 case 2:
Adam Tkac8e985062011-02-07 11:33:57 +00001331 accessRights = accessRights & ~(AccessPtrEvents | AccessKeyEvents | AccessView);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001332 break;
1333 }
1334 framebufferUpdateRequest(server->pb->getRect(), false);
1335}
1336int VNCSConnectionST::getStatus()
1337{
1338 if ((accessRights & (AccessPtrEvents | AccessKeyEvents | AccessView)) == 0x0007)
1339 return 0;
1340 if ((accessRights & (AccessPtrEvents | AccessKeyEvents | AccessView)) == 0x0001)
1341 return 1;
1342 if ((accessRights & (AccessPtrEvents | AccessKeyEvents | AccessView)) == 0x0000)
1343 return 2;
1344 return 4;
1345}
1346