blob: 232776fc3bc946e2f45d87926265b7418ac9ad73 [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);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000106 server->desktop->keyEvent(*i, 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");
541 desktop->keyEvent(XK_Shift_L, false);
542 }
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");
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000546 desktop->keyEvent(XK_Shift_L, true);
547 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.
555void VNCSConnectionST::keyEvent(rdr::U32 key, bool down) {
556 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)
562 vlog.debug("Key pressed: 0x%x", key);
563 else
564 vlog.debug("Key released: 0x%x", key);
565
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;
569 newkey = server->keyRemapper->remapKey(key);
570 if (newkey != key) {
571 vlog.debug("Key remapped to 0x%x", newkey);
572 key = newkey;
573 }
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) &&
578 ((key == XK_Caps_Lock) ||
579 (key == XK_Num_Lock) ||
580 (key == XK_Scroll_Lock))) {
581 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
590 if (key == XK_Scroll_Lock) {
591 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)
599 if (((key >= XK_A) && (key <= XK_Z)) ||
600 ((key >= XK_a) && (key <= XK_z))) {
601 bool uppercase, shift, lock;
Pierre Ossmanbb305ca2016-12-11 12:41:26 +0100602
Pierre Ossmanb45a84f2016-12-12 16:59:15 +0100603 uppercase = (key >= XK_A) && (key <= XK_Z);
604 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");
610 server->desktop->keyEvent(XK_Caps_Lock, true);
611 server->desktop->keyEvent(XK_Caps_Lock, false);
612 }
613 }
614
615 // NumLock synchronisation heuristic
616 // (this is more cautious because of the differences between Unix,
617 // Windows and macOS)
618 if (((key >= XK_KP_Home) && (key <= XK_KP_Delete)) ||
619 ((key >= XK_KP_0) && (key <= XK_KP_9)) ||
620 (key == XK_KP_Separator) || (key == XK_KP_Decimal)) {
621 bool number, shift, lock;
622
623 number = ((key >= XK_KP_0) && (key <= XK_KP_9)) ||
624 (key == XK_KP_Separator) || (key == XK_KP_Decimal);
625 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");
641 server->desktop->keyEvent(XK_Num_Lock, true);
642 server->desktop->keyEvent(XK_Num_Lock, false);
643 }
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);
650 if (key == XK_ISO_Left_Tab) {
651 if (pressedKeys.find(XK_Shift_L) == pressedKeys.end() &&
652 pressedKeys.find(XK_Shift_R) == pressedKeys.end())
653 shiftPresser.press();
654 key = XK_Tab;
655 }
656
657 if (down) {
658 pressedKeys.insert(key);
659 } else {
660 if (!pressedKeys.erase(key)) return;
661 }
662 server->desktop->keyEvent(key, down);
663}
664
665void VNCSConnectionST::clientCutText(const char* str, int len)
666{
667 if (!(accessRights & AccessCutText)) return;
668 if (!rfb::Server::acceptCutText) return;
669 server->desktop->clientCutText(str, len);
670}
671
672void VNCSConnectionST::framebufferUpdateRequest(const Rect& r,bool incremental)
673{
Pierre Ossmane9962f72009-04-23 12:31:42 +0000674 Rect safeRect;
675
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000676 if (!(accessRights & AccessView)) return;
677
678 SConnection::framebufferUpdateRequest(r, incremental);
679
Pierre Ossmand9a59ba2009-03-20 15:55:37 +0000680 // Check that the client isn't sending crappy requests
681 if (!r.enclosed_by(Rect(0, 0, cp.width, cp.height))) {
682 vlog.error("FramebufferUpdateRequest %dx%d at %d,%d exceeds framebuffer %dx%d",
683 r.width(), r.height(), r.tl.x, r.tl.y, cp.width, cp.height);
Pierre Ossmane9962f72009-04-23 12:31:42 +0000684 safeRect = r.intersect(Rect(0, 0, cp.width, cp.height));
685 } else {
686 safeRect = r;
Pierre Ossmand9a59ba2009-03-20 15:55:37 +0000687 }
688
Constantin Kaplinsky2ef66952008-08-29 11:33:46 +0000689 // Just update the requested region.
690 // Framebuffer update will be sent a bit later, see processMessages().
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000691 Region reqRgn(r);
Pierre Ossman1b478e52011-11-15 12:08:30 +0000692 if (!incremental || !continuousUpdates)
693 requested.assign_union(reqRgn);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000694
695 if (!incremental) {
696 // Non-incremental update - treat as if area requested has changed
697 updates.add_changed(reqRgn);
Pierre Ossman53125a72009-04-22 15:37:51 +0000698
699 // And send the screen layout to the client (which, unlike the
700 // framebuffer dimensions, the client doesn't get during init)
Pierre Ossmanc5e25602009-03-20 12:59:05 +0000701 writer()->writeExtendedDesktopSize();
Pierre Ossman53125a72009-04-22 15:37:51 +0000702
703 // We do not send a DesktopSize since it only contains the
704 // framebuffer size (which the client already should know) and
705 // because some clients don't handle extra DesktopSize events
706 // very well.
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000707 }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000708}
709
Pierre Ossman34bb0612009-03-21 21:16:14 +0000710void VNCSConnectionST::setDesktopSize(int fb_width, int fb_height,
711 const ScreenSet& layout)
Pierre Ossmanc5e25602009-03-20 12:59:05 +0000712{
Pierre Ossman04e62db2009-03-23 16:57:07 +0000713 unsigned int result;
714
Michal Srbb318b8f2014-11-24 13:18:28 +0200715 if (!(accessRights & AccessSetDesktopSize)) return;
716 if (!rfb::Server::acceptSetDesktopSize) return;
717
Pierre Ossman04e62db2009-03-23 16:57:07 +0000718 // Don't bother the desktop with an invalid configuration
719 if (!layout.validate(fb_width, fb_height)) {
720 writer()->writeExtendedDesktopSize(reasonClient, resultInvalid,
721 fb_width, fb_height, layout);
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000722 writeFramebufferUpdate();
Pierre Ossman04e62db2009-03-23 16:57:07 +0000723 return;
724 }
725
726 // FIXME: the desktop will call back to VNCServerST and an extra set
727 // of ExtendedDesktopSize messages will be sent. This is okay
728 // protocol-wise, but unnecessary.
729 result = server->desktop->setScreenLayout(fb_width, fb_height, layout);
730
Pierre Ossman04e62db2009-03-23 16:57:07 +0000731 writer()->writeExtendedDesktopSize(reasonClient, result,
732 fb_width, fb_height, layout);
Pierre Ossman04e62db2009-03-23 16:57:07 +0000733
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000734 // Only notify other clients on success
Pierre Ossman04e62db2009-03-23 16:57:07 +0000735 if (result == resultSuccess) {
736 if (server->screenLayout != layout)
737 throw Exception("Desktop configured a different screen layout than requested");
738 server->notifyScreenLayoutChange(this);
739 }
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000740
741 // but always send back a reply to the requesting client
742 // (do this last as it might throw an exception on socket errors)
743 writeFramebufferUpdate();
Pierre Ossmanc5e25602009-03-20 12:59:05 +0000744}
745
Pierre Ossman2c764942011-11-14 15:54:30 +0000746void VNCSConnectionST::fence(rdr::U32 flags, unsigned len, const char data[])
747{
748 if (flags & fenceFlagRequest) {
749 if (flags & fenceFlagSyncNext) {
Pierre Ossmanb8b1e962012-07-20 10:47:00 +0000750 pendingSyncFence = true;
Pierre Ossman2c764942011-11-14 15:54:30 +0000751
752 fenceFlags = flags & (fenceFlagBlockBefore | fenceFlagBlockAfter | fenceFlagSyncNext);
753 fenceDataLen = len;
754 delete [] fenceData;
Michal Srbf3afa242017-03-27 19:02:15 +0300755 fenceData = NULL;
Pierre Ossman2c764942011-11-14 15:54:30 +0000756 if (len > 0) {
757 fenceData = new char[len];
758 memcpy(fenceData, data, len);
759 }
760
761 return;
762 }
763
764 // We handle everything synchronously so we trivially honor these modes
765 flags = flags & (fenceFlagBlockBefore | fenceFlagBlockAfter);
766
767 writer()->writeFence(flags, len, data);
768 return;
769 }
770
Pierre Ossman1b478e52011-11-15 12:08:30 +0000771 struct RTTInfo rttInfo;
772
Pierre Ossman2c764942011-11-14 15:54:30 +0000773 switch (len) {
774 case 0:
775 // Initial dummy fence;
776 break;
Pierre Ossman1b478e52011-11-15 12:08:30 +0000777 case sizeof(struct RTTInfo):
778 memcpy(&rttInfo, data, sizeof(struct RTTInfo));
779 handleRTTPong(rttInfo);
780 break;
Pierre Ossman2c764942011-11-14 15:54:30 +0000781 default:
782 vlog.error("Fence response of unexpected size received");
783 }
784}
785
Pierre Ossman1b478e52011-11-15 12:08:30 +0000786void VNCSConnectionST::enableContinuousUpdates(bool enable,
787 int x, int y, int w, int h)
788{
789 Rect rect;
790
791 if (!cp.supportsFence || !cp.supportsContinuousUpdates)
792 throw Exception("Client tried to enable continuous updates when not allowed");
793
794 continuousUpdates = enable;
795
796 rect.setXYWH(x, y, w, h);
797 cuRegion.reset(rect);
798
799 if (enable) {
800 requested.clear();
801 writeFramebufferUpdate();
802 } else {
803 writer()->writeEndOfContinuousUpdates();
804 }
805}
806
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000807// supportsLocalCursor() is called whenever the status of
808// cp.supportsLocalCursor has changed. If the client does now support local
809// cursor, we make sure that the old server-side rendered cursor is cleaned up
810// and the cursor is sent to the client.
811
812void VNCSConnectionST::supportsLocalCursor()
813{
Pierre Ossman324043e2017-08-16 16:26:11 +0200814 if (cp.supportsLocalCursorWithAlpha ||
815 cp.supportsLocalCursor || cp.supportsLocalXCursor) {
Pierre Ossman671d2ef2015-06-09 16:09:06 +0200816 if (!damagedCursorRegion.is_empty())
Pierre Ossman5c9e1e52011-11-08 10:32:05 +0000817 removeRenderedCursor = true;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000818 setCursor();
819 }
820}
821
Pierre Ossman2c764942011-11-14 15:54:30 +0000822void VNCSConnectionST::supportsFence()
823{
824 writer()->writeFence(fenceFlagRequest, 0, NULL);
825}
826
Pierre Ossman1b478e52011-11-15 12:08:30 +0000827void VNCSConnectionST::supportsContinuousUpdates()
828{
829 // We refuse to use continuous updates if we cannot monitor the buffer
830 // usage using fences.
831 if (!cp.supportsFence)
832 return;
833
834 writer()->writeEndOfContinuousUpdates();
835}
836
Pierre Ossmanb45a84f2016-12-12 16:59:15 +0100837void VNCSConnectionST::supportsLEDState()
838{
839 writer()->writeLEDState();
840}
841
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000842
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +0000843bool VNCSConnectionST::handleTimeout(Timer* t)
844{
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000845 try {
Pierre Ossmana40ab202016-04-29 15:35:56 +0200846 if (t == &congestionTimer)
Pierre Ossman1b478e52011-11-15 12:08:30 +0000847 updateCongestion();
Pierre Ossmanf8e3b342015-01-26 14:37:04 +0100848 else if (t == &queryConnectTimer) {
849 if (state() == RFBSTATE_QUERYING)
850 approveConnection(false, "The attempt to prompt the user to accept the connection failed");
851 }
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000852 } catch (rdr::Exception& e) {
853 close(e.str());
854 }
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +0000855
856 return false;
857}
858
859
Pierre Ossman1b478e52011-11-15 12:08:30 +0000860void VNCSConnectionST::writeRTTPing()
861{
862 struct RTTInfo rttInfo;
863
864 if (!cp.supportsFence)
865 return;
866
867 memset(&rttInfo, 0, sizeof(struct RTTInfo));
868
869 gettimeofday(&rttInfo.tv, NULL);
870 rttInfo.offset = sock->outStream().length();
871 rttInfo.inFlight = rttInfo.offset - ackedOffset;
872
873 // We need to make sure any old update are already processed by the
874 // time we get the response back. This allows us to reliably throttle
875 // back on client overload, as well as network overload.
876 writer()->writeFence(fenceFlagRequest | fenceFlagBlockBefore,
877 sizeof(struct RTTInfo), (const char*)&rttInfo);
878
879 pingCounter++;
880
881 sentOffset = rttInfo.offset;
882
883 // Let some data flow before we adjust the settings
884 if (!congestionTimer.isStarted())
885 congestionTimer.start(__rfbmin(baseRTT * 2, 100));
886}
887
888void VNCSConnectionST::handleRTTPong(const struct RTTInfo &rttInfo)
889{
890 unsigned rtt, delay;
Pierre Ossman1b478e52011-11-15 12:08:30 +0000891
892 pingCounter--;
893
894 rtt = msSince(&rttInfo.tv);
895 if (rtt < 1)
896 rtt = 1;
897
898 ackedOffset = rttInfo.offset;
899
900 // Try to estimate wire latency by tracking lowest seen latency
901 if (rtt < baseRTT)
902 baseRTT = rtt;
903
904 if (rttInfo.inFlight > congWindow) {
905 seenCongestion = true;
906
907 // Estimate added delay because of overtaxed buffers
908 delay = (rttInfo.inFlight - congWindow) * baseRTT / congWindow;
909
910 if (delay < rtt)
911 rtt -= delay;
912 else
913 rtt = 1;
914
915 // If we underestimate the congestion window, then we'll get a latency
916 // that's less than the wire latency, which will confuse other portions
917 // of the code.
918 if (rtt < baseRTT)
919 rtt = baseRTT;
920 }
921
922 // We only keep track of the minimum latency seen (for a given interval)
klemens0536d092017-01-28 20:56:56 +0100923 // on the basis that we want to avoid continuous buffer issue, but don't
Pierre Ossman1b478e52011-11-15 12:08:30 +0000924 // mind (or even approve of) bursts.
925 if (rtt < minRTT)
926 minRTT = rtt;
927}
928
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +0000929bool VNCSConnectionST::isCongested()
930{
Pierre Ossman1b478e52011-11-15 12:08:30 +0000931 int offset;
932
933 // Stuff still waiting in the send buffer?
Pierre Ossman352d0622016-04-29 15:50:54 +0200934 sock->outStream().flush();
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +0000935 if (sock->outStream().bufferUsage() > 0)
936 return true;
937
Pierre Ossman1b478e52011-11-15 12:08:30 +0000938 if (!cp.supportsFence)
939 return false;
940
941 // Idle for too long? (and no data on the wire)
942 //
943 // FIXME: This should really just be one baseRTT, but we're getting
944 // problems with triggering the idle timeout on each update.
945 // Maybe we need to use a moving average for the wire latency
946 // instead of baseRTT.
947 if ((sentOffset == ackedOffset) &&
948 (sock->outStream().getIdleTime() > 2 * baseRTT)) {
949
950#ifdef CONGESTION_DEBUG
951 if (congWindow > INITIAL_WINDOW)
952 fprintf(stderr, "Reverting to initial window (%d KiB) after %d ms\n",
953 INITIAL_WINDOW / 1024, sock->outStream().getIdleTime());
954#endif
955
956 // Close congestion window and allow a transfer
957 // FIXME: Reset baseRTT like Linux Vegas?
958 congWindow = __rfbmin(INITIAL_WINDOW, congWindow);
959
960 return false;
961 }
962
963 offset = sock->outStream().length();
964
965 // FIXME: Should we compensate for non-update data?
966 // (i.e. use sentOffset instead of offset)
967 if ((offset - ackedOffset) < congWindow)
968 return false;
969
970 // If we just have one outstanding "ping", that means the client has
971 // started receiving our update. In order to not regress compared to
972 // before we had congestion avoidance, we allow another update here.
973 // This could further clog up the tubes, but congestion control isn't
974 // really working properly right now anyway as the wire would otherwise
975 // be idle for at least RTT/2.
976 if (pingCounter == 1)
977 return false;
978
979 return true;
980}
981
982
983void VNCSConnectionST::updateCongestion()
984{
985 unsigned diff;
986
987 if (!seenCongestion)
988 return;
989
990 diff = minRTT - baseRTT;
991
992 if (diff > __rfbmin(100, baseRTT)) {
993 // Way too fast
994 congWindow = congWindow * baseRTT / minRTT;
995 } else if (diff > __rfbmin(50, baseRTT/2)) {
996 // Slightly too fast
997 congWindow -= 4096;
998 } else if (diff < 5) {
999 // Way too slow
1000 congWindow += 8192;
1001 } else if (diff < 25) {
1002 // Too slow
1003 congWindow += 4096;
1004 }
1005
1006 if (congWindow < MINIMUM_WINDOW)
1007 congWindow = MINIMUM_WINDOW;
1008 if (congWindow > MAXIMUM_WINDOW)
1009 congWindow = MAXIMUM_WINDOW;
1010
1011#ifdef CONGESTION_DEBUG
1012 fprintf(stderr, "RTT: %d ms (%d ms), Window: %d KiB, Bandwidth: %g Mbps\n",
1013 minRTT, baseRTT, congWindow / 1024,
1014 congWindow * 8.0 / baseRTT / 1000.0);
1015
1016#ifdef TCP_INFO
1017 struct tcp_info tcp_info;
1018 socklen_t tcp_info_length;
1019
1020 tcp_info_length = sizeof(tcp_info);
1021 if (getsockopt(sock->getFd(), SOL_TCP, TCP_INFO,
1022 (void *)&tcp_info, &tcp_info_length) == 0) {
1023 fprintf(stderr, "Socket: RTT: %d ms (+/- %d ms) Window %d KiB\n",
1024 tcp_info.tcpi_rtt / 1000, tcp_info.tcpi_rttvar / 1000,
1025 tcp_info.tcpi_snd_mss * tcp_info.tcpi_snd_cwnd / 1024);
1026 }
1027#endif
1028
1029#endif
1030
1031 minRTT = -1;
1032 seenCongestion = false;
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +00001033}
1034
1035
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001036void VNCSConnectionST::writeFramebufferUpdate()
1037{
Pierre Ossman2c764942011-11-14 15:54:30 +00001038 // We're in the middle of processing a command that's supposed to be
1039 // synchronised. Allowing an update to slip out right now might violate
1040 // that synchronisation.
1041 if (syncFence)
1042 return;
1043
Pierre Ossmana3ac01e2011-11-07 21:13:54 +00001044 // We try to aggregate responses, so don't send out anything whilst we
1045 // still have incoming messages. processMessages() will give us another
1046 // chance to run once things are idle.
1047 if (inProcessMessages)
1048 return;
1049
Pierre Ossman1b478e52011-11-15 12:08:30 +00001050 if (state() != RFBSTATE_NORMAL)
1051 return;
1052 if (requested.is_empty() && !continuousUpdates)
Pierre Ossmane9962f72009-04-23 12:31:42 +00001053 return;
Pierre Ossmand9a59ba2009-03-20 15:55:37 +00001054
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +00001055 // Check that we actually have some space on the link and retry in a
1056 // bit if things are congested.
Pierre Ossmana40ab202016-04-29 15:35:56 +02001057 if (isCongested())
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +00001058 return;
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +00001059
Pierre Ossmancef3cf72016-11-25 10:06:34 +01001060 // Updates often consists of many small writes, and in continuous
1061 // mode, we will also have small fence messages around the update. We
1062 // need to aggregate these in order to not clog up TCP's congestion
1063 // window.
Pierre Ossman36dadf82011-11-15 12:11:32 +00001064 network::TcpSocket::cork(sock->getFd(), true);
1065
Pierre Ossmane9962f72009-04-23 12:31:42 +00001066 // First take care of any updates that cannot contain framebuffer data
1067 // changes.
Pierre Ossmancef3cf72016-11-25 10:06:34 +01001068 writeNoDataUpdate();
1069
1070 // Then real data (if possible)
1071 writeDataUpdate();
1072
1073 network::TcpSocket::cork(sock->getFd(), false);
1074}
1075
1076void VNCSConnectionST::writeNoDataUpdate()
1077{
1078 if (!writer()->needNoDataUpdate())
1079 return;
1080
1081 writer()->writeNoDataUpdate();
1082
1083 // Make sure no data update is sent until next request
1084 requested.clear();
1085}
1086
1087void VNCSConnectionST::writeDataUpdate()
1088{
1089 Region req;
1090 UpdateInfo ui;
1091 bool needNewUpdateInfo;
Pierre Ossman24684e52016-12-05 16:58:19 +01001092 const RenderedCursor *cursor;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001093
Constantin Kaplinsky604d7812007-08-31 15:50:37 +00001094 updates.enable_copyrect(cp.useCopyRect);
1095
Pierre Ossman6e49e952016-10-07 15:59:38 +02001096 // See if we are allowed to send anything right now (the framebuffer
1097 // might have changed in ways we haven't yet been informed of).
Pierre Ossmanbbf955e2011-11-08 12:44:10 +00001098 if (!server->checkUpdate())
Pierre Ossmancef3cf72016-11-25 10:06:34 +01001099 return;
Constantin Kaplinskya09dc142008-12-18 12:08:15 +00001100
Pierre Ossmancef3cf72016-11-25 10:06:34 +01001101 // See what the client has requested (if anything)
Pierre Ossman1b478e52011-11-15 12:08:30 +00001102 if (continuousUpdates)
1103 req = cuRegion.union_(requested);
1104 else
1105 req = requested;
1106
Pierre Ossmancef3cf72016-11-25 10:06:34 +01001107 if (req.is_empty())
1108 return;
1109
1110 // Get the lists of updates. Prior to exporting the data to the `ui' object,
1111 // getUpdateInfo() will normalize the `updates' object such way that its
1112 // `changed' and `copied' regions would not intersect.
Pierre Ossman1b478e52011-11-15 12:08:30 +00001113 updates.getUpdateInfo(&ui, req);
1114 needNewUpdateInfo = false;
Constantin Kaplinsky604d7812007-08-31 15:50:37 +00001115
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001116 // If the previous position of the rendered cursor overlaps the source of the
1117 // copy, then when the copy happens the corresponding rectangle in the
1118 // destination will be wrong, so add it to the changed region.
1119
Pierre Ossman671d2ef2015-06-09 16:09:06 +02001120 if (!ui.copied.is_empty() && !damagedCursorRegion.is_empty()) {
1121 Region bogusCopiedCursor;
1122
1123 bogusCopiedCursor.copyFrom(damagedCursorRegion);
1124 bogusCopiedCursor.translate(ui.copy_delta);
1125 bogusCopiedCursor.assign_intersect(server->pb->getRect());
Constantin Kaplinsky45517c82007-08-31 15:56:33 +00001126 if (!ui.copied.intersect(bogusCopiedCursor).is_empty()) {
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001127 updates.add_changed(bogusCopiedCursor);
Constantin Kaplinsky604d7812007-08-31 15:50:37 +00001128 needNewUpdateInfo = true;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001129 }
1130 }
1131
Pierre Ossman671d2ef2015-06-09 16:09:06 +02001132 // If we need to remove the old rendered cursor, just add the region to
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001133 // the changed region.
1134
1135 if (removeRenderedCursor) {
Pierre Ossman671d2ef2015-06-09 16:09:06 +02001136 updates.add_changed(damagedCursorRegion);
Constantin Kaplinsky604d7812007-08-31 15:50:37 +00001137 needNewUpdateInfo = true;
Pierre Ossman671d2ef2015-06-09 16:09:06 +02001138 damagedCursorRegion.clear();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001139 removeRenderedCursor = false;
1140 }
1141
1142 // Return if there is nothing to send the client.
1143
Pierre Ossman671d2ef2015-06-09 16:09:06 +02001144 if (updates.is_empty() && !writer()->needFakeUpdate() && !updateRenderedCursor)
Pierre Ossmancef3cf72016-11-25 10:06:34 +01001145 return;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001146
Constantin Kaplinsky604d7812007-08-31 15:50:37 +00001147 // The `updates' object could change, make sure we have valid update info.
1148
1149 if (needNewUpdateInfo)
Pierre Ossman1b478e52011-11-15 12:08:30 +00001150 updates.getUpdateInfo(&ui, req);
Constantin Kaplinsky604d7812007-08-31 15:50:37 +00001151
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001152 // If the client needs a server-side rendered cursor, work out the cursor
1153 // rectangle. If it's empty then don't bother drawing it, but if it overlaps
1154 // with the update region, we need to draw the rendered cursor regardless of
1155 // whether it has changed.
1156
Pierre Ossman24684e52016-12-05 16:58:19 +01001157 cursor = NULL;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001158 if (needRenderedCursor()) {
Pierre Ossman671d2ef2015-06-09 16:09:06 +02001159 Rect renderedCursorRect;
1160
Pierre Ossman24684e52016-12-05 16:58:19 +01001161 cursor = server->getRenderedCursor();
1162
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001163 renderedCursorRect
Pierre Ossman24684e52016-12-05 16:58:19 +01001164 = cursor->getEffectiveRect().intersect(req.get_bounding_rect());
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001165
1166 if (renderedCursorRect.is_empty()) {
Pierre Ossman24684e52016-12-05 16:58:19 +01001167 cursor = NULL;
1168 } else if (!updateRenderedCursor &&
1169 ui.changed.union_(ui.copied)
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001170 .intersect(renderedCursorRect).is_empty()) {
Pierre Ossman24684e52016-12-05 16:58:19 +01001171 cursor = NULL;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001172 }
1173
Pierre Ossman5c037202016-12-05 17:00:35 +01001174 if (cursor) {
1175 updates.subtract(renderedCursorRect);
1176 updates.getUpdateInfo(&ui, req);
1177 }
Pierre Ossman671d2ef2015-06-09 16:09:06 +02001178
1179 damagedCursorRegion.assign_union(renderedCursorRect);
1180 updateRenderedCursor = false;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001181 }
1182
Pierre Ossman24684e52016-12-05 16:58:19 +01001183 if (ui.is_empty() && !writer()->needFakeUpdate() && !cursor)
Pierre Ossmancef3cf72016-11-25 10:06:34 +01001184 return;
Pierre Ossmanfdba3fe2014-01-31 13:12:18 +01001185
Pierre Ossmancef3cf72016-11-25 10:06:34 +01001186 writeRTTPing();
Pierre Ossman1b478e52011-11-15 12:08:30 +00001187
Pierre Ossmancef3cf72016-11-25 10:06:34 +01001188 encodeManager.writeUpdate(ui, server->getPixelBuffer(), cursor);
Pierre Ossman1b478e52011-11-15 12:08:30 +00001189
Pierre Ossmancef3cf72016-11-25 10:06:34 +01001190 writeRTTPing();
Pierre Ossmanc0397262014-03-14 15:59:46 +01001191
Pierre Ossmancef3cf72016-11-25 10:06:34 +01001192 // The request might be for just part of the screen, so we cannot
1193 // just clear the entire update tracker.
1194 updates.subtract(req);
Pierre Ossmanc0397262014-03-14 15:59:46 +01001195
Pierre Ossmancef3cf72016-11-25 10:06:34 +01001196 requested.clear();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001197}
1198
1199
Pierre Ossmana3ac01e2011-11-07 21:13:54 +00001200void VNCSConnectionST::screenLayoutChange(rdr::U16 reason)
1201{
1202 if (!authenticated())
1203 return;
1204
1205 cp.screenLayout = server->screenLayout;
1206
1207 if (state() != RFBSTATE_NORMAL)
1208 return;
1209
1210 writer()->writeExtendedDesktopSize(reason, 0, cp.width, cp.height,
1211 cp.screenLayout);
1212 writeFramebufferUpdate();
1213}
1214
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001215
1216// setCursor() is called whenever the cursor has changed shape or pixel format.
1217// If the client supports local cursor then it will arrange for the cursor to
1218// be sent to the client.
1219
1220void VNCSConnectionST::setCursor()
1221{
Pierre Ossmana3ac01e2011-11-07 21:13:54 +00001222 if (state() != RFBSTATE_NORMAL)
1223 return;
Pierre Ossmana3ac01e2011-11-07 21:13:54 +00001224
Pierre Ossman6a1a0d02017-02-19 15:48:17 +01001225 cp.setCursor(*server->cursor);
Pierre Ossman126e5642014-02-13 14:40:25 +01001226
Pierre Ossman8053c8e2017-02-21 12:59:04 +01001227 if (!writer()->writeSetCursorWithAlpha()) {
1228 if (!writer()->writeSetCursor()) {
1229 if (!writer()->writeSetXCursor()) {
1230 // No client support
1231 return;
1232 }
Pierre Ossman126e5642014-02-13 14:40:25 +01001233 }
1234 }
1235
Pierre Ossmana3ac01e2011-11-07 21:13:54 +00001236 writeFramebufferUpdate();
1237}
1238
1239void VNCSConnectionST::setDesktopName(const char *name)
1240{
1241 cp.setName(name);
1242
1243 if (state() != RFBSTATE_NORMAL)
1244 return;
1245
1246 if (!writer()->writeSetDesktopName()) {
1247 fprintf(stderr, "Client does not support desktop rename\n");
1248 return;
1249 }
1250
1251 writeFramebufferUpdate();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001252}
1253
Pierre Ossmanb45a84f2016-12-12 16:59:15 +01001254void VNCSConnectionST::setLEDState(unsigned int ledstate)
1255{
1256 if (state() != RFBSTATE_NORMAL)
1257 return;
1258
1259 cp.setLEDState(ledstate);
1260
1261 if (!writer()->writeLEDState()) {
1262 // No client support
1263 return;
1264 }
1265
1266 writeFramebufferUpdate();
1267}
1268
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001269void VNCSConnectionST::setSocketTimeouts()
1270{
1271 int timeoutms = rfb::Server::clientWaitTimeMillis;
1272 soonestTimeout(&timeoutms, secsToMillis(rfb::Server::idleTimeout));
1273 if (timeoutms == 0)
1274 timeoutms = -1;
1275 sock->inStream().setTimeout(timeoutms);
1276 sock->outStream().setTimeout(timeoutms);
1277}
1278
1279char* VNCSConnectionST::getStartTime()
1280{
1281 char* result = ctime(&startTime);
1282 result[24] = '\0';
1283 return result;
1284}
1285
1286void VNCSConnectionST::setStatus(int status)
1287{
1288 switch (status) {
1289 case 0:
1290 accessRights = accessRights | AccessPtrEvents | AccessKeyEvents | AccessView;
1291 break;
1292 case 1:
Pierre Ossman7728be22015-03-03 16:39:37 +01001293 accessRights = (accessRights & ~(AccessPtrEvents | AccessKeyEvents)) | AccessView;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001294 break;
1295 case 2:
Adam Tkac8e985062011-02-07 11:33:57 +00001296 accessRights = accessRights & ~(AccessPtrEvents | AccessKeyEvents | AccessView);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001297 break;
1298 }
1299 framebufferUpdateRequest(server->pb->getRect(), false);
1300}
1301int VNCSConnectionST::getStatus()
1302{
1303 if ((accessRights & (AccessPtrEvents | AccessKeyEvents | AccessView)) == 0x0007)
1304 return 0;
1305 if ((accessRights & (AccessPtrEvents | AccessKeyEvents | AccessView)) == 0x0001)
1306 return 1;
1307 if ((accessRights & (AccessPtrEvents | AccessKeyEvents | AccessView)) == 0x0000)
1308 return 2;
1309 return 4;
1310}
1311