blob: 15b2f9ad871f9870d7bd5bd2fc7f87f5ebdcef8c [file] [log] [blame]
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001/* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved.
Pierre Ossman8c3bd692018-03-22 15:58:54 +01002 * Copyright 2009-2018 Pierre Ossman for Cendio AB
Peter Åstrand (astrand)7a368c92018-09-19 12:45:17 +02003 * Copyright 2018 Peter Astrand for Cendio AB
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00004 *
5 * This is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This software is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this software; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
18 * USA.
19 */
20
Pierre Ossmana830bec2011-11-08 12:12:02 +000021#include <network/TcpSocket.h>
Pierre Ossman707fa122015-12-11 20:21:20 +010022
23#include <rfb/ComparingUpdateTracker.h>
24#include <rfb/Encoder.h>
25#include <rfb/KeyRemapper.h>
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000026#include <rfb/LogWriter.h>
Adam Tkac5a0caed2010-04-23 13:58:10 +000027#include <rfb/Security.h>
Pierre Ossman707fa122015-12-11 20:21:20 +010028#include <rfb/ServerCore.h>
29#include <rfb/SMsgWriter.h>
30#include <rfb/VNCServerST.h>
31#include <rfb/VNCSConnectionST.h>
Pierre Ossmanc5e25602009-03-20 12:59:05 +000032#include <rfb/screenTypes.h>
Pierre Ossman2c764942011-11-14 15:54:30 +000033#include <rfb/fenceTypes.h>
Pierre Ossmanbb305ca2016-12-11 12:41:26 +010034#include <rfb/ledStates.h>
Pierre Ossmanbb305ca2016-12-11 12:41:26 +010035#define XK_LATIN1
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000036#define XK_MISCELLANY
37#define XK_XKB_KEYS
38#include <rfb/keysymdef.h>
39
40using namespace rfb;
41
42static LogWriter vlog("VNCSConnST");
43
Pierre Ossman71ca8d52017-09-15 11:03:12 +020044static Cursor emptyCursor(0, 0, Point(0, 0), NULL);
45
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000046VNCSConnectionST::VNCSConnectionST(VNCServerST* server_, network::Socket *s,
47 bool reverse)
Pierre Ossman7069bdd2015-02-06 14:41:58 +010048 : sock(s), reverseConnection(reverse),
Pierre Ossman36304752017-10-04 16:21:57 +020049 inProcessMessages(false),
Pierre Ossmanb8b1e962012-07-20 10:47:00 +000050 pendingSyncFence(false), syncFence(false), fenceFlags(0),
Pierre Ossmana99d14d2015-12-13 15:43:46 +010051 fenceDataLen(0), fenceData(NULL), congestionTimer(this),
Pierre Ossmana114c342018-06-18 16:29:55 +020052 losslessTimer(this), server(server_),
Pierre Ossman671d2ef2015-06-09 16:09:06 +020053 updateRenderedCursor(false), removeRenderedCursor(false),
Pierre Ossmana40ab202016-04-29 15:35:56 +020054 continuousUpdates(false), encodeManager(this), pointerEventTime(0),
Pierre Ossman25db44a2017-11-16 16:40:44 +010055 clientHasCursor(false),
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +000056 accessRights(AccessDefault), startTime(time(0))
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000057{
58 setStreams(&sock->inStream(), &sock->outStream());
59 peerEndpoint.buf = sock->getPeerEndpoint();
60 VNCServerST::connectionsLog.write(1,"accepted: %s", peerEndpoint.buf);
61
62 // Configure the socket
63 setSocketTimeouts();
64 lastEventTime = time(0);
65
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000066 server->clients.push_front(this);
67}
68
69
70VNCSConnectionST::~VNCSConnectionST()
71{
72 // If we reach here then VNCServerST is deleting us!
73 VNCServerST::connectionsLog.write(1,"closed: %s (%s)",
74 peerEndpoint.buf,
75 (closeReason.buf) ? closeReason.buf : "");
76
77 // Release any keys the client still had pressed
Pierre Ossman16e1dcb2017-05-16 14:33:43 +020078 while (!pressedKeys.empty()) {
79 rdr::U32 keysym, keycode;
80
81 keysym = pressedKeys.begin()->second;
82 keycode = pressedKeys.begin()->first;
83 pressedKeys.erase(pressedKeys.begin());
84
85 vlog.debug("Releasing key 0x%x / 0x%x on client disconnect",
86 keysym, keycode);
87 server->desktop->keyEvent(keysym, keycode, false);
Pierre Ossman9a153b02015-08-31 10:01:14 +020088 }
Pierre Ossman16e1dcb2017-05-16 14:33:43 +020089
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000090 if (server->pointerClient == this)
91 server->pointerClient = 0;
92
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000093 // Remove this client from the server
94 server->clients.remove(this);
95
Pierre Ossman2c764942011-11-14 15:54:30 +000096 delete [] fenceData;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000097}
98
99
100// Methods called from VNCServerST
101
102bool VNCSConnectionST::init()
103{
104 try {
105 initialiseProtocol();
106 } catch (rdr::Exception& e) {
107 close(e.str());
108 return false;
109 }
110 return true;
111}
112
113void VNCSConnectionST::close(const char* reason)
114{
115 // Log the reason for the close
116 if (!closeReason.buf)
Adam Tkacd36b6262009-09-04 10:57:20 +0000117 closeReason.buf = strDup(reason);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000118 else
119 vlog.debug("second close: %s (%s)", peerEndpoint.buf, reason);
120
121 if (authenticated()) {
122 server->lastDisconnectTime = time(0);
123 }
124
125 // Just shutdown the socket and mark our state as closing. Eventually the
126 // calling code will call VNCServerST's removeSocket() method causing us to
127 // be deleted.
128 sock->shutdown();
129 setState(RFBSTATE_CLOSING);
130}
131
132
133void VNCSConnectionST::processMessages()
134{
135 if (state() == RFBSTATE_CLOSING) return;
136 try {
137 // - Now set appropriate socket timeouts and process data
138 setSocketTimeouts();
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000139
140 inProcessMessages = true;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000141
Pierre Ossmana830bec2011-11-08 12:12:02 +0000142 // Get the underlying TCP layer to build large packets if we send
143 // multiple small responses.
Peter Åstrand (astrand)01dc1a62017-10-10 12:56:04 +0200144 sock->cork(true);
Pierre Ossmana830bec2011-11-08 12:12:02 +0000145
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000146 while (getInStream()->checkNoWait(1)) {
Pierre Ossmanb8b1e962012-07-20 10:47:00 +0000147 if (pendingSyncFence) {
148 syncFence = true;
149 pendingSyncFence = false;
150 }
151
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000152 processMsg();
Pierre Ossmanb8b1e962012-07-20 10:47:00 +0000153
Pierre Ossman2c764942011-11-14 15:54:30 +0000154 if (syncFence) {
155 writer()->writeFence(fenceFlags, fenceDataLen, fenceData);
156 syncFence = false;
157 }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000158 }
159
Pierre Ossmana830bec2011-11-08 12:12:02 +0000160 // Flush out everything in case we go idle after this.
Peter Åstrand (astrand)01dc1a62017-10-10 12:56:04 +0200161 sock->cork(false);
Pierre Ossmana830bec2011-11-08 12:12:02 +0000162
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000163 inProcessMessages = false;
Constantin Kaplinsky2ef66952008-08-29 11:33:46 +0000164
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000165 // If there were anything requiring an update, try to send it here.
166 // We wait until now with this to aggregate responses and to give
167 // higher priority to user actions such as keyboard and pointer events.
168 writeFramebufferUpdate();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000169 } catch (rdr::EndOfStream&) {
170 close("Clean disconnection");
171 } catch (rdr::Exception &e) {
172 close(e.str());
173 }
174}
175
Pierre Ossmand408ca52016-04-29 14:26:05 +0200176void VNCSConnectionST::flushSocket()
177{
178 if (state() == RFBSTATE_CLOSING) return;
179 try {
180 setSocketTimeouts();
181 sock->outStream().flush();
Pierre Ossmana40ab202016-04-29 15:35:56 +0200182 // Flushing the socket might release an update that was previously
183 // delayed because of congestion.
184 if (sock->outStream().bufferUsage() == 0)
185 writeFramebufferUpdate();
Pierre Ossmand408ca52016-04-29 14:26:05 +0200186 } catch (rdr::Exception &e) {
187 close(e.str());
188 }
189}
190
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000191void VNCSConnectionST::pixelBufferChange()
192{
193 try {
194 if (!authenticated()) return;
Pierre Ossman0d3ce872018-06-18 15:59:00 +0200195 if (client.width() && client.height() &&
196 (server->pb->width() != client.width() ||
197 server->pb->height() != client.height()))
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000198 {
199 // We need to clip the next update to the new size, but also add any
200 // extra bits if it's bigger. If we wanted to do this exactly, something
201 // like the code below would do it, but at the moment we just update the
Pierre Ossman671d2ef2015-06-09 16:09:06 +0200202 // entire new size. However, we do need to clip the damagedCursorRegion
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000203 // because that might be added to updates in writeFramebufferUpdate().
204
205 //updates.intersect(server->pb->getRect());
206 //
Pierre Ossman0d3ce872018-06-18 15:59:00 +0200207 //if (server->pb->width() > client.width())
208 // updates.add_changed(Rect(client.width(), 0, server->pb->width(),
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000209 // server->pb->height()));
Pierre Ossman0d3ce872018-06-18 15:59:00 +0200210 //if (server->pb->height() > client.height())
211 // updates.add_changed(Rect(0, client.height(), client.width(),
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000212 // server->pb->height()));
213
Pierre Ossman671d2ef2015-06-09 16:09:06 +0200214 damagedCursorRegion.assign_intersect(server->pb->getRect());
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000215
Pierre Ossman0d3ce872018-06-18 15:59:00 +0200216 client.setDimensions(server->pb->width(), server->pb->height(),
217 server->screenLayout);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000218 if (state() == RFBSTATE_NORMAL) {
Pierre Ossman2ee430a2009-05-28 12:54:24 +0000219 // We should only send EDS to client asking for both
220 if (!writer()->writeExtendedDesktopSize()) {
221 if (!writer()->writeSetDesktopSize()) {
222 close("Client does not support desktop resize");
223 return;
224 }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000225 }
226 }
Pierre Ossman6b2f1132016-11-30 08:03:35 +0100227
228 // Drop any lossy tracking that is now outside the framebuffer
229 encodeManager.pruneLosslessRefresh(Region(server->pb->getRect()));
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000230 }
231 // Just update the whole screen at the moment because we're too lazy to
232 // work out what's actually changed.
233 updates.clear();
234 updates.add_changed(server->pb->getRect());
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000235 writeFramebufferUpdate();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000236 } catch(rdr::Exception &e) {
237 close(e.str());
238 }
239}
240
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000241void VNCSConnectionST::writeFramebufferUpdateOrClose()
Pierre Ossman04e62db2009-03-23 16:57:07 +0000242{
243 try {
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000244 writeFramebufferUpdate();
245 } catch(rdr::Exception &e) {
246 close(e.str());
247 }
248}
Pierre Ossman04e62db2009-03-23 16:57:07 +0000249
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000250void VNCSConnectionST::screenLayoutChangeOrClose(rdr::U16 reason)
251{
252 try {
253 screenLayoutChange(reason);
Pierre Ossmanb218ecd2017-11-16 16:43:13 +0100254 writeFramebufferUpdate();
Pierre Ossman04e62db2009-03-23 16:57:07 +0000255 } catch(rdr::Exception &e) {
256 close(e.str());
257 }
258}
259
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000260void VNCSConnectionST::bellOrClose()
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000261{
262 try {
263 if (state() == RFBSTATE_NORMAL) writer()->writeBell();
264 } catch(rdr::Exception& e) {
265 close(e.str());
266 }
267}
268
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000269void VNCSConnectionST::serverCutTextOrClose(const char *str, int len)
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000270{
271 try {
272 if (!(accessRights & AccessCutText)) return;
273 if (!rfb::Server::sendCutText) return;
274 if (state() == RFBSTATE_NORMAL)
275 writer()->writeServerCutText(str, len);
276 } catch(rdr::Exception& e) {
277 close(e.str());
278 }
279}
280
Peter Åstrandc39e0782009-01-15 12:21:42 +0000281
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000282void VNCSConnectionST::setDesktopNameOrClose(const char *name)
Peter Åstrandc39e0782009-01-15 12:21:42 +0000283{
Peter Åstrandc39e0782009-01-15 12:21:42 +0000284 try {
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000285 setDesktopName(name);
Pierre Ossmanb218ecd2017-11-16 16:43:13 +0100286 writeFramebufferUpdate();
Peter Åstrandc39e0782009-01-15 12:21:42 +0000287 } catch(rdr::Exception& e) {
288 close(e.str());
289 }
290}
291
292
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000293void VNCSConnectionST::setCursorOrClose()
294{
295 try {
296 setCursor();
Pierre Ossmanb218ecd2017-11-16 16:43:13 +0100297 writeFramebufferUpdate();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000298 } catch(rdr::Exception& e) {
299 close(e.str());
300 }
301}
302
303
Pierre Ossmanb45a84f2016-12-12 16:59:15 +0100304void VNCSConnectionST::setLEDStateOrClose(unsigned int state)
305{
306 try {
307 setLEDState(state);
Pierre Ossmanb218ecd2017-11-16 16:43:13 +0100308 writeFramebufferUpdate();
Pierre Ossmanb45a84f2016-12-12 16:59:15 +0100309 } catch(rdr::Exception& e) {
310 close(e.str());
311 }
312}
313
314
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000315int VNCSConnectionST::checkIdleTimeout()
316{
317 int idleTimeout = rfb::Server::idleTimeout;
318 if (idleTimeout == 0) return 0;
319 if (state() != RFBSTATE_NORMAL && idleTimeout < 15)
320 idleTimeout = 15; // minimum of 15 seconds while authenticating
321 time_t now = time(0);
322 if (now < lastEventTime) {
323 // Someone must have set the time backwards. Set lastEventTime so that the
324 // idleTimeout will count from now.
325 vlog.info("Time has gone backwards - resetting idle timeout");
326 lastEventTime = now;
327 }
328 int timeLeft = lastEventTime + idleTimeout - now;
329 if (timeLeft < -60) {
330 // Our callback is over a minute late - someone must have set the time
331 // forwards. Set lastEventTime so that the idleTimeout will count from
332 // now.
333 vlog.info("Time has gone forwards - resetting idle timeout");
334 lastEventTime = now;
335 return secsToMillis(idleTimeout);
336 }
337 if (timeLeft <= 0) {
338 close("Idle timeout");
339 return 0;
340 }
341 return secsToMillis(timeLeft);
342}
343
Pierre Ossmanb114cec2011-11-20 15:36:11 +0000344
345bool VNCSConnectionST::getComparerState()
346{
347 // We interpret a low compression level as an indication that the client
348 // wants to prioritise CPU usage over bandwidth, and hence disable the
349 // comparing update tracker.
Pierre Ossman0d3ce872018-06-18 15:59:00 +0200350 return (client.compressLevel == -1) || (client.compressLevel > 1);
Pierre Ossmanb114cec2011-11-20 15:36:11 +0000351}
352
353
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000354// renderedCursorChange() is called whenever the server-side rendered cursor
355// changes shape or position. It ensures that the next update will clean up
356// the old rendered cursor and if necessary draw the new rendered cursor.
357
358void VNCSConnectionST::renderedCursorChange()
359{
360 if (state() != RFBSTATE_NORMAL) return;
Pierre Ossman71ca8d52017-09-15 11:03:12 +0200361 // Are we switching between client-side and server-side cursor?
Pierre Ossman25db44a2017-11-16 16:40:44 +0100362 if (clientHasCursor == needRenderedCursor())
Pierre Ossman71ca8d52017-09-15 11:03:12 +0200363 setCursorOrClose();
Pierre Ossman25db44a2017-11-16 16:40:44 +0100364 bool hasRenderedCursor = !damagedCursorRegion.is_empty();
Pierre Ossman330ca422017-11-06 13:15:55 +0100365 if (hasRenderedCursor)
Pierre Ossman5c9e1e52011-11-08 10:32:05 +0000366 removeRenderedCursor = true;
Pierre Ossman6b0bc292011-12-21 13:17:54 +0000367 if (needRenderedCursor()) {
Pierre Ossman671d2ef2015-06-09 16:09:06 +0200368 updateRenderedCursor = true;
Pierre Ossman6b0bc292011-12-21 13:17:54 +0000369 writeFramebufferUpdateOrClose();
370 }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000371}
372
373// needRenderedCursor() returns true if this client needs the server-side
374// rendered cursor. This may be because it does not support local cursor or
375// because the current cursor position has not been set by this client.
376// Unfortunately we can't know for sure when the current cursor position has
377// been set by this client. We guess that this is the case when the current
378// cursor position is the same as the last pointer event from this client, or
379// if it is a very short time since this client's last pointer event (up to a
380// second). [ Ideally we should do finer-grained timing here and make the time
381// configurable, but I don't think it's that important. ]
382
383bool VNCSConnectionST::needRenderedCursor()
384{
Pierre Ossman77ede0a2016-12-05 16:57:30 +0100385 if (state() != RFBSTATE_NORMAL)
386 return false;
387
Pierre Ossmanb114f5c2018-06-18 16:34:16 +0200388 if (!client.supportsLocalCursor())
Pierre Ossman77ede0a2016-12-05 16:57:30 +0100389 return true;
390 if (!server->cursorPos.equals(pointerEventPos) &&
391 (time(0) - pointerEventTime) > 0)
392 return true;
393
394 return false;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000395}
396
397
398void VNCSConnectionST::approveConnectionOrClose(bool accept,
399 const char* reason)
400{
401 try {
402 approveConnection(accept, reason);
403 } catch (rdr::Exception& e) {
404 close(e.str());
405 }
406}
407
408
409
410// -=- Callbacks from SConnection
411
412void VNCSConnectionST::authSuccess()
413{
414 lastEventTime = time(0);
415
416 server->startDesktop();
417
418 // - Set the connection parameters appropriately
Pierre Ossman0d3ce872018-06-18 15:59:00 +0200419 client.setDimensions(server->pb->width(), server->pb->height(),
420 server->screenLayout);
421 client.setName(server->getName());
422 client.setLEDState(server->ledState);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000423
424 // - Set the default pixel format
Pierre Ossman0d3ce872018-06-18 15:59:00 +0200425 client.setPF(server->pb->getPF());
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000426 char buffer[256];
Pierre Ossman0d3ce872018-06-18 15:59:00 +0200427 client.pf().print(buffer, 256);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000428 vlog.info("Server default pixel format %s", buffer);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000429
430 // - Mark the entire display as "dirty"
431 updates.add_changed(server->pb->getRect());
432 startTime = time(0);
433}
434
435void VNCSConnectionST::queryConnection(const char* userName)
436{
437 // - Authentication succeeded - clear from blacklist
438 CharArray name; name.buf = sock->getPeerAddress();
439 server->blHosts->clearBlackmark(name.buf);
440
441 // - Special case to provide a more useful error message
442 if (rfb::Server::neverShared && !rfb::Server::disconnectClients &&
443 server->authClientCount() > 0) {
444 approveConnection(false, "The server is already in use");
445 return;
446 }
447
448 // - Does the client have the right to bypass the query?
449 if (reverseConnection ||
450 !(rfb::Server::queryConnect || sock->requiresQuery()) ||
451 (accessRights & AccessNoQuery))
452 {
453 approveConnection(true);
454 return;
455 }
456
457 // - Get the server to display an Accept/Reject dialog, if required
458 // If a dialog is displayed, the result will be PENDING, and the
459 // server will call approveConnection at a later time
460 CharArray reason;
461 VNCServerST::queryResult qr = server->queryConnection(sock, userName,
462 &reason.buf);
Pierre Ossman36304752017-10-04 16:21:57 +0200463 if (qr == VNCServerST::PENDING)
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000464 return;
465
466 // - If server returns ACCEPT/REJECT then pass result to SConnection
467 approveConnection(qr == VNCServerST::ACCEPT, reason.buf);
468}
469
470void VNCSConnectionST::clientInit(bool shared)
471{
472 lastEventTime = time(0);
473 if (rfb::Server::alwaysShared || reverseConnection) shared = true;
Pierre Ossmane7be49b2014-12-02 14:33:17 +0100474 if (!(accessRights & AccessNonShared)) shared = true;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000475 if (rfb::Server::neverShared) shared = false;
476 if (!shared) {
Pierre Ossmane7be49b2014-12-02 14:33:17 +0100477 if (rfb::Server::disconnectClients && (accessRights & AccessNonShared)) {
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000478 // - Close all the other connected clients
479 vlog.debug("non-shared connection - closing clients");
480 server->closeClients("Non-shared connection requested", getSock());
481 } else {
482 // - Refuse this connection if there are existing clients, in addition to
483 // this one
484 if (server->authClientCount() > 1) {
485 close("Server is already in use");
486 return;
487 }
488 }
489 }
490 SConnection::clientInit(shared);
491}
492
493void VNCSConnectionST::setPixelFormat(const PixelFormat& pf)
494{
495 SConnection::setPixelFormat(pf);
496 char buffer[256];
497 pf.print(buffer, 256);
498 vlog.info("Client pixel format %s", buffer);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000499 setCursor();
500}
501
502void VNCSConnectionST::pointerEvent(const Point& pos, int buttonMask)
503{
504 pointerEventTime = lastEventTime = time(0);
505 server->lastUserInputTime = lastEventTime;
506 if (!(accessRights & AccessPtrEvents)) return;
507 if (!rfb::Server::acceptPointerEvents) return;
508 if (!server->pointerClient || server->pointerClient == this) {
509 pointerEventPos = pos;
510 if (buttonMask)
511 server->pointerClient = this;
512 else
513 server->pointerClient = 0;
514 server->desktop->pointerEvent(pointerEventPos, buttonMask);
515 }
516}
517
518
519class VNCSConnectionSTShiftPresser {
520public:
521 VNCSConnectionSTShiftPresser(SDesktop* desktop_)
522 : desktop(desktop_), pressed(false) {}
523 ~VNCSConnectionSTShiftPresser() {
Pierre Ossman9a153b02015-08-31 10:01:14 +0200524 if (pressed) {
525 vlog.debug("Releasing fake Shift_L");
Pierre Ossman5ae28212017-05-16 14:30:38 +0200526 desktop->keyEvent(XK_Shift_L, 0, false);
Pierre Ossman9a153b02015-08-31 10:01:14 +0200527 }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000528 }
529 void press() {
Pierre Ossman9a153b02015-08-31 10:01:14 +0200530 vlog.debug("Pressing fake Shift_L");
Pierre Ossman5ae28212017-05-16 14:30:38 +0200531 desktop->keyEvent(XK_Shift_L, 0, true);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000532 pressed = true;
533 }
534 SDesktop* desktop;
535 bool pressed;
536};
537
538// keyEvent() - record in the pressedKeys which keys were pressed. Allow
539// multiple down events (for autorepeat), but only allow a single up event.
Pierre Ossman5ae28212017-05-16 14:30:38 +0200540void VNCSConnectionST::keyEvent(rdr::U32 keysym, rdr::U32 keycode, bool down) {
Pierre Ossman16e1dcb2017-05-16 14:33:43 +0200541 rdr::U32 lookup;
542
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000543 lastEventTime = time(0);
544 server->lastUserInputTime = lastEventTime;
545 if (!(accessRights & AccessKeyEvents)) return;
546 if (!rfb::Server::acceptKeyEvents) return;
547
Pierre Ossman9a153b02015-08-31 10:01:14 +0200548 if (down)
Pierre Ossman5ae28212017-05-16 14:30:38 +0200549 vlog.debug("Key pressed: 0x%x / 0x%x", keysym, keycode);
Pierre Ossman9a153b02015-08-31 10:01:14 +0200550 else
Pierre Ossman5ae28212017-05-16 14:30:38 +0200551 vlog.debug("Key released: 0x%x / 0x%x", keysym, keycode);
Pierre Ossman9a153b02015-08-31 10:01:14 +0200552
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000553 // Remap the key if required
Pierre Ossman9a153b02015-08-31 10:01:14 +0200554 if (server->keyRemapper) {
555 rdr::U32 newkey;
Pierre Ossman5ae28212017-05-16 14:30:38 +0200556 newkey = server->keyRemapper->remapKey(keysym);
557 if (newkey != keysym) {
Pierre Ossman9a153b02015-08-31 10:01:14 +0200558 vlog.debug("Key remapped to 0x%x", newkey);
Pierre Ossman5ae28212017-05-16 14:30:38 +0200559 keysym = newkey;
Pierre Ossman9a153b02015-08-31 10:01:14 +0200560 }
561 }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000562
Pierre Ossmanbb305ca2016-12-11 12:41:26 +0100563 // Avoid lock keys if we don't know the server state
564 if ((server->ledState == ledUnknown) &&
Pierre Ossman5ae28212017-05-16 14:30:38 +0200565 ((keysym == XK_Caps_Lock) ||
566 (keysym == XK_Num_Lock) ||
567 (keysym == XK_Scroll_Lock))) {
Pierre Ossmanbb305ca2016-12-11 12:41:26 +0100568 vlog.debug("Ignoring lock key (e.g. caps lock)");
569 return;
570 }
Pierre Ossmanbb305ca2016-12-11 12:41:26 +0100571
Pierre Ossmanb45a84f2016-12-12 16:59:15 +0100572 // Lock key heuristics
573 // (only for clients that do not support the LED state extension)
Pierre Ossmanb114f5c2018-06-18 16:34:16 +0200574 if (!client.supportsLEDState()) {
Pierre Ossmanb45a84f2016-12-12 16:59:15 +0100575 // Always ignore ScrollLock as we don't have a heuristic
576 // for that
Pierre Ossman5ae28212017-05-16 14:30:38 +0200577 if (keysym == XK_Scroll_Lock) {
Pierre Ossmanb45a84f2016-12-12 16:59:15 +0100578 vlog.debug("Ignoring lock key (e.g. caps lock)");
579 return;
Pierre Ossmanbb305ca2016-12-11 12:41:26 +0100580 }
581
Pierre Ossmanb45a84f2016-12-12 16:59:15 +0100582 if (down && (server->ledState != ledUnknown)) {
583 // CapsLock synchronisation heuristic
584 // (this assumes standard interaction between CapsLock the Shift
585 // keys and normal characters)
Pierre Ossman5ae28212017-05-16 14:30:38 +0200586 if (((keysym >= XK_A) && (keysym <= XK_Z)) ||
587 ((keysym >= XK_a) && (keysym <= XK_z))) {
Pierre Ossmanb45a84f2016-12-12 16:59:15 +0100588 bool uppercase, shift, lock;
Pierre Ossmanbb305ca2016-12-11 12:41:26 +0100589
Pierre Ossman5ae28212017-05-16 14:30:38 +0200590 uppercase = (keysym >= XK_A) && (keysym <= XK_Z);
Pierre Ossman851e6802017-09-12 16:44:44 +0200591 shift = isShiftPressed();
Pierre Ossmanb45a84f2016-12-12 16:59:15 +0100592 lock = server->ledState & ledCapsLock;
Pierre Ossmanbb305ca2016-12-11 12:41:26 +0100593
Pierre Ossmanb45a84f2016-12-12 16:59:15 +0100594 if (lock == (uppercase == shift)) {
595 vlog.debug("Inserting fake CapsLock to get in sync with client");
Pierre Ossman5ae28212017-05-16 14:30:38 +0200596 server->desktop->keyEvent(XK_Caps_Lock, 0, true);
597 server->desktop->keyEvent(XK_Caps_Lock, 0, false);
Pierre Ossmanb45a84f2016-12-12 16:59:15 +0100598 }
599 }
600
601 // NumLock synchronisation heuristic
602 // (this is more cautious because of the differences between Unix,
603 // Windows and macOS)
Pierre Ossman5ae28212017-05-16 14:30:38 +0200604 if (((keysym >= XK_KP_Home) && (keysym <= XK_KP_Delete)) ||
605 ((keysym >= XK_KP_0) && (keysym <= XK_KP_9)) ||
606 (keysym == XK_KP_Separator) || (keysym == XK_KP_Decimal)) {
Pierre Ossmanb45a84f2016-12-12 16:59:15 +0100607 bool number, shift, lock;
608
Pierre Ossman5ae28212017-05-16 14:30:38 +0200609 number = ((keysym >= XK_KP_0) && (keysym <= XK_KP_9)) ||
610 (keysym == XK_KP_Separator) || (keysym == XK_KP_Decimal);
Pierre Ossman851e6802017-09-12 16:44:44 +0200611 shift = isShiftPressed();
Pierre Ossmanb45a84f2016-12-12 16:59:15 +0100612 lock = server->ledState & ledNumLock;
613
614 if (shift) {
615 // We don't know the appropriate NumLock state for when Shift
616 // is pressed as it could be one of:
617 //
618 // a) A Unix client where Shift negates NumLock
619 //
620 // b) A Windows client where Shift only cancels NumLock
621 //
622 // c) A macOS client where Shift doesn't have any effect
623 //
624 } else if (lock == (number == shift)) {
625 vlog.debug("Inserting fake NumLock to get in sync with client");
Pierre Ossman5ae28212017-05-16 14:30:38 +0200626 server->desktop->keyEvent(XK_Num_Lock, 0, true);
627 server->desktop->keyEvent(XK_Num_Lock, 0, false);
Pierre Ossmanb45a84f2016-12-12 16:59:15 +0100628 }
Pierre Ossmanbb305ca2016-12-11 12:41:26 +0100629 }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000630 }
631 }
632
633 // Turn ISO_Left_Tab into shifted Tab.
634 VNCSConnectionSTShiftPresser shiftPresser(server->desktop);
Pierre Ossman5ae28212017-05-16 14:30:38 +0200635 if (keysym == XK_ISO_Left_Tab) {
Pierre Ossman851e6802017-09-12 16:44:44 +0200636 if (!isShiftPressed())
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000637 shiftPresser.press();
Pierre Ossman5ae28212017-05-16 14:30:38 +0200638 keysym = XK_Tab;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000639 }
640
Pierre Ossman16e1dcb2017-05-16 14:33:43 +0200641 // We need to be able to track keys, so generate a fake index when we
642 // aren't given a keycode
643 if (keycode == 0)
644 lookup = 0x80000000 | keysym;
645 else
646 lookup = keycode;
647
648 // We force the same keysym for an already down key for the
649 // sake of sanity
650 if (pressedKeys.find(lookup) != pressedKeys.end())
651 keysym = pressedKeys[lookup];
652
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000653 if (down) {
Pierre Ossman16e1dcb2017-05-16 14:33:43 +0200654 pressedKeys[lookup] = keysym;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000655 } else {
Pierre Ossman16e1dcb2017-05-16 14:33:43 +0200656 if (!pressedKeys.erase(lookup))
Pierre Ossman5ae28212017-05-16 14:30:38 +0200657 return;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000658 }
Pierre Ossman16e1dcb2017-05-16 14:33:43 +0200659
Pierre Ossman5ae28212017-05-16 14:30:38 +0200660 server->desktop->keyEvent(keysym, keycode, down);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000661}
662
663void VNCSConnectionST::clientCutText(const char* str, int len)
664{
665 if (!(accessRights & AccessCutText)) return;
666 if (!rfb::Server::acceptCutText) return;
667 server->desktop->clientCutText(str, len);
668}
669
670void VNCSConnectionST::framebufferUpdateRequest(const Rect& r,bool incremental)
671{
Pierre Ossmane9962f72009-04-23 12:31:42 +0000672 Rect safeRect;
673
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000674 if (!(accessRights & AccessView)) return;
675
676 SConnection::framebufferUpdateRequest(r, incremental);
677
Pierre Ossmand9a59ba2009-03-20 15:55:37 +0000678 // Check that the client isn't sending crappy requests
Pierre Ossman0d3ce872018-06-18 15:59:00 +0200679 if (!r.enclosed_by(Rect(0, 0, client.width(), client.height()))) {
Pierre Ossmand9a59ba2009-03-20 15:55:37 +0000680 vlog.error("FramebufferUpdateRequest %dx%d at %d,%d exceeds framebuffer %dx%d",
Pierre Ossman9312b0e2018-06-20 12:25:14 +0200681 r.width(), r.height(), r.tl.x, r.tl.y,
Pierre Ossman0d3ce872018-06-18 15:59:00 +0200682 client.width(), client.height());
683 safeRect = r.intersect(Rect(0, 0, client.width(), client.height()));
Pierre Ossmane9962f72009-04-23 12:31:42 +0000684 } else {
685 safeRect = r;
Pierre Ossmand9a59ba2009-03-20 15:55:37 +0000686 }
687
Constantin Kaplinsky2ef66952008-08-29 11:33:46 +0000688 // Just update the requested region.
689 // Framebuffer update will be sent a bit later, see processMessages().
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000690 Region reqRgn(r);
Pierre Ossman1b478e52011-11-15 12:08:30 +0000691 if (!incremental || !continuousUpdates)
692 requested.assign_union(reqRgn);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000693
694 if (!incremental) {
695 // Non-incremental update - treat as if area requested has changed
696 updates.add_changed(reqRgn);
Pierre Ossman53125a72009-04-22 15:37:51 +0000697
698 // And send the screen layout to the client (which, unlike the
699 // framebuffer dimensions, the client doesn't get during init)
Pierre Ossmanc5e25602009-03-20 12:59:05 +0000700 writer()->writeExtendedDesktopSize();
Pierre Ossman53125a72009-04-22 15:37:51 +0000701
702 // We do not send a DesktopSize since it only contains the
703 // framebuffer size (which the client already should know) and
704 // because some clients don't handle extra DesktopSize events
705 // very well.
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000706 }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000707}
708
Pierre Ossman34bb0612009-03-21 21:16:14 +0000709void VNCSConnectionST::setDesktopSize(int fb_width, int fb_height,
710 const ScreenSet& layout)
Pierre Ossmanc5e25602009-03-20 12:59:05 +0000711{
Pierre Ossman04e62db2009-03-23 16:57:07 +0000712 unsigned int result;
713
Michal Srbb318b8f2014-11-24 13:18:28 +0200714 if (!(accessRights & AccessSetDesktopSize)) return;
715 if (!rfb::Server::acceptSetDesktopSize) return;
716
Pierre Ossman04e62db2009-03-23 16:57:07 +0000717 // Don't bother the desktop with an invalid configuration
718 if (!layout.validate(fb_width, fb_height)) {
719 writer()->writeExtendedDesktopSize(reasonClient, resultInvalid,
720 fb_width, fb_height, layout);
Pierre Ossman04e62db2009-03-23 16:57:07 +0000721 return;
722 }
723
724 // FIXME: the desktop will call back to VNCServerST and an extra set
725 // of ExtendedDesktopSize messages will be sent. This is okay
726 // protocol-wise, but unnecessary.
727 result = server->desktop->setScreenLayout(fb_width, fb_height, layout);
728
Pierre Ossman04e62db2009-03-23 16:57:07 +0000729 writer()->writeExtendedDesktopSize(reasonClient, result,
730 fb_width, fb_height, layout);
Pierre Ossman04e62db2009-03-23 16:57:07 +0000731
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000732 // Only notify other clients on success
Pierre Ossman04e62db2009-03-23 16:57:07 +0000733 if (result == resultSuccess) {
734 if (server->screenLayout != layout)
735 throw Exception("Desktop configured a different screen layout than requested");
736 server->notifyScreenLayoutChange(this);
737 }
Pierre Ossmanc5e25602009-03-20 12:59:05 +0000738}
739
Pierre Ossman2c764942011-11-14 15:54:30 +0000740void VNCSConnectionST::fence(rdr::U32 flags, unsigned len, const char data[])
741{
Pierre Ossmanb2a417c2015-12-11 19:32:21 +0100742 rdr::U8 type;
743
Pierre Ossman2c764942011-11-14 15:54:30 +0000744 if (flags & fenceFlagRequest) {
745 if (flags & fenceFlagSyncNext) {
Pierre Ossmanb8b1e962012-07-20 10:47:00 +0000746 pendingSyncFence = true;
Pierre Ossman2c764942011-11-14 15:54:30 +0000747
748 fenceFlags = flags & (fenceFlagBlockBefore | fenceFlagBlockAfter | fenceFlagSyncNext);
749 fenceDataLen = len;
750 delete [] fenceData;
Michal Srbf3afa242017-03-27 19:02:15 +0300751 fenceData = NULL;
Pierre Ossman2c764942011-11-14 15:54:30 +0000752 if (len > 0) {
753 fenceData = new char[len];
754 memcpy(fenceData, data, len);
755 }
756
757 return;
758 }
759
760 // We handle everything synchronously so we trivially honor these modes
761 flags = flags & (fenceFlagBlockBefore | fenceFlagBlockAfter);
762
763 writer()->writeFence(flags, len, data);
764 return;
765 }
766
Pierre Ossmanb2a417c2015-12-11 19:32:21 +0100767 if (len < 1)
768 vlog.error("Fence response of unexpected size received");
Pierre Ossman1b478e52011-11-15 12:08:30 +0000769
Pierre Ossmanb2a417c2015-12-11 19:32:21 +0100770 type = data[0];
771
772 switch (type) {
Pierre Ossman2c764942011-11-14 15:54:30 +0000773 case 0:
774 // Initial dummy fence;
775 break;
Pierre Ossmanb2a417c2015-12-11 19:32:21 +0100776 case 1:
Pierre Ossmanc09e5582015-12-11 20:23:17 +0100777 congestion.gotPong();
Pierre Ossman1b478e52011-11-15 12:08:30 +0000778 break;
Pierre Ossman2c764942011-11-14 15:54:30 +0000779 default:
Pierre Ossmanb2a417c2015-12-11 19:32:21 +0100780 vlog.error("Fence response of unexpected type received");
Pierre Ossman2c764942011-11-14 15:54:30 +0000781 }
782}
783
Pierre Ossman1b478e52011-11-15 12:08:30 +0000784void VNCSConnectionST::enableContinuousUpdates(bool enable,
785 int x, int y, int w, int h)
786{
787 Rect rect;
788
Pierre Ossmanb114f5c2018-06-18 16:34:16 +0200789 if (!client.supportsFence() || !client.supportsContinuousUpdates())
Pierre Ossman1b478e52011-11-15 12:08:30 +0000790 throw Exception("Client tried to enable continuous updates when not allowed");
791
792 continuousUpdates = enable;
793
794 rect.setXYWH(x, y, w, h);
795 cuRegion.reset(rect);
796
797 if (enable) {
798 requested.clear();
Pierre Ossman1b478e52011-11-15 12:08:30 +0000799 } else {
800 writer()->writeEndOfContinuousUpdates();
801 }
802}
803
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000804// supportsLocalCursor() is called whenever the status of
Pierre Ossmanb114f5c2018-06-18 16:34:16 +0200805// client.supportsLocalCursor() has changed. If the client does now support local
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000806// cursor, we make sure that the old server-side rendered cursor is cleaned up
807// and the cursor is sent to the client.
808
809void VNCSConnectionST::supportsLocalCursor()
810{
Pierre Ossman387a4172017-11-16 16:44:36 +0100811 bool hasRenderedCursor = !damagedCursorRegion.is_empty();
812 if (hasRenderedCursor && !needRenderedCursor())
813 removeRenderedCursor = true;
814 setCursor();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000815}
816
Pierre Ossman2c764942011-11-14 15:54:30 +0000817void VNCSConnectionST::supportsFence()
818{
Pierre Ossmanb2a417c2015-12-11 19:32:21 +0100819 char type = 0;
820 writer()->writeFence(fenceFlagRequest, sizeof(type), &type);
Pierre Ossman2c764942011-11-14 15:54:30 +0000821}
822
Pierre Ossman1b478e52011-11-15 12:08:30 +0000823void VNCSConnectionST::supportsContinuousUpdates()
824{
825 // We refuse to use continuous updates if we cannot monitor the buffer
826 // usage using fences.
Pierre Ossmanb114f5c2018-06-18 16:34:16 +0200827 if (!client.supportsFence())
Pierre Ossman1b478e52011-11-15 12:08:30 +0000828 return;
829
830 writer()->writeEndOfContinuousUpdates();
831}
832
Pierre Ossmanb45a84f2016-12-12 16:59:15 +0100833void VNCSConnectionST::supportsLEDState()
834{
835 writer()->writeLEDState();
836}
837
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000838
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +0000839bool VNCSConnectionST::handleTimeout(Timer* t)
840{
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000841 try {
Peter Åstrand (astrand)7a368c92018-09-19 12:45:17 +0200842 if ((t == &congestionTimer) ||
843 (t == &losslessTimer))
Pierre Ossmana99d14d2015-12-13 15:43:46 +0100844 writeFramebufferUpdate();
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000845 } catch (rdr::Exception& e) {
846 close(e.str());
847 }
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +0000848
849 return false;
850}
851
Pierre Ossman851e6802017-09-12 16:44:44 +0200852bool VNCSConnectionST::isShiftPressed()
853{
854 std::map<rdr::U32, rdr::U32>::const_iterator iter;
855
856 for (iter = pressedKeys.begin(); iter != pressedKeys.end(); ++iter) {
857 if (iter->second == XK_Shift_L)
858 return true;
859 if (iter->second == XK_Shift_R)
860 return true;
861 }
862
863 return false;
864}
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +0000865
Pierre Ossman1b478e52011-11-15 12:08:30 +0000866void VNCSConnectionST::writeRTTPing()
867{
Pierre Ossmanb2a417c2015-12-11 19:32:21 +0100868 char type;
Pierre Ossman1b478e52011-11-15 12:08:30 +0000869
Pierre Ossmanb114f5c2018-06-18 16:34:16 +0200870 if (!client.supportsFence())
Pierre Ossman1b478e52011-11-15 12:08:30 +0000871 return;
872
Pierre Ossmana99d14d2015-12-13 15:43:46 +0100873 congestion.updatePosition(sock->outStream().length());
Pierre Ossman1b478e52011-11-15 12:08:30 +0000874
875 // We need to make sure any old update are already processed by the
876 // time we get the response back. This allows us to reliably throttle
877 // back on client overload, as well as network overload.
Pierre Ossmanb2a417c2015-12-11 19:32:21 +0100878 type = 1;
Pierre Ossman1b478e52011-11-15 12:08:30 +0000879 writer()->writeFence(fenceFlagRequest | fenceFlagBlockBefore,
Pierre Ossmanb2a417c2015-12-11 19:32:21 +0100880 sizeof(type), &type);
Pierre Ossman1b478e52011-11-15 12:08:30 +0000881
Pierre Ossmana99d14d2015-12-13 15:43:46 +0100882 congestion.sentPing();
Pierre Ossman1b478e52011-11-15 12:08:30 +0000883}
884
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +0000885bool VNCSConnectionST::isCongested()
886{
Pierre Ossmance261812018-07-17 15:01:53 +0200887 int eta;
Pierre Ossmana99d14d2015-12-13 15:43:46 +0100888
889 congestionTimer.stop();
Pierre Ossman1b478e52011-11-15 12:08:30 +0000890
891 // Stuff still waiting in the send buffer?
Pierre Ossman352d0622016-04-29 15:50:54 +0200892 sock->outStream().flush();
Pierre Ossman8cf71632016-03-11 09:38:08 +0100893 congestion.debugTrace("congestion-trace.csv", sock->getFd());
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +0000894 if (sock->outStream().bufferUsage() > 0)
895 return true;
896
Pierre Ossmanb114f5c2018-06-18 16:34:16 +0200897 if (!client.supportsFence())
Pierre Ossman1b478e52011-11-15 12:08:30 +0000898 return false;
899
Pierre Ossmana99d14d2015-12-13 15:43:46 +0100900 congestion.updatePosition(sock->outStream().length());
901 if (!congestion.isCongested())
Pierre Ossman1b478e52011-11-15 12:08:30 +0000902 return false;
903
Pierre Ossmana99d14d2015-12-13 15:43:46 +0100904 eta = congestion.getUncongestedETA();
905 if (eta >= 0)
906 congestionTimer.start(eta);
Pierre Ossman1b478e52011-11-15 12:08:30 +0000907
908 return true;
909}
910
911
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000912void VNCSConnectionST::writeFramebufferUpdate()
913{
Pierre Ossmana99d14d2015-12-13 15:43:46 +0100914 congestion.updatePosition(sock->outStream().length());
915
Pierre Ossman2c764942011-11-14 15:54:30 +0000916 // We're in the middle of processing a command that's supposed to be
917 // synchronised. Allowing an update to slip out right now might violate
918 // that synchronisation.
919 if (syncFence)
920 return;
921
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000922 // We try to aggregate responses, so don't send out anything whilst we
923 // still have incoming messages. processMessages() will give us another
924 // chance to run once things are idle.
925 if (inProcessMessages)
926 return;
927
Pierre Ossman1b478e52011-11-15 12:08:30 +0000928 if (state() != RFBSTATE_NORMAL)
929 return;
930 if (requested.is_empty() && !continuousUpdates)
Pierre Ossmane9962f72009-04-23 12:31:42 +0000931 return;
Pierre Ossmand9a59ba2009-03-20 15:55:37 +0000932
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +0000933 // Check that we actually have some space on the link and retry in a
934 // bit if things are congested.
Pierre Ossmana40ab202016-04-29 15:35:56 +0200935 if (isCongested())
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +0000936 return;
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +0000937
Pierre Ossmancef3cf72016-11-25 10:06:34 +0100938 // Updates often consists of many small writes, and in continuous
939 // mode, we will also have small fence messages around the update. We
940 // need to aggregate these in order to not clog up TCP's congestion
941 // window.
Peter Åstrand (astrand)01dc1a62017-10-10 12:56:04 +0200942 sock->cork(true);
Pierre Ossman36dadf82011-11-15 12:11:32 +0000943
Pierre Ossmane9962f72009-04-23 12:31:42 +0000944 // First take care of any updates that cannot contain framebuffer data
945 // changes.
Pierre Ossmancef3cf72016-11-25 10:06:34 +0100946 writeNoDataUpdate();
947
948 // Then real data (if possible)
949 writeDataUpdate();
950
Peter Åstrand (astrand)01dc1a62017-10-10 12:56:04 +0200951 sock->cork(false);
Pierre Ossmana99d14d2015-12-13 15:43:46 +0100952
953 congestion.updatePosition(sock->outStream().length());
Pierre Ossmancef3cf72016-11-25 10:06:34 +0100954}
955
956void VNCSConnectionST::writeNoDataUpdate()
957{
958 if (!writer()->needNoDataUpdate())
959 return;
960
961 writer()->writeNoDataUpdate();
962
963 // Make sure no data update is sent until next request
964 requested.clear();
965}
966
967void VNCSConnectionST::writeDataUpdate()
968{
Pierre Ossman8efc7b42018-03-23 11:45:51 +0100969 Region req, pending;
Pierre Ossmancef3cf72016-11-25 10:06:34 +0100970 UpdateInfo ui;
971 bool needNewUpdateInfo;
Pierre Ossman24684e52016-12-05 16:58:19 +0100972 const RenderedCursor *cursor;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000973
Pierre Ossmancef3cf72016-11-25 10:06:34 +0100974 // See what the client has requested (if anything)
Pierre Ossman1b478e52011-11-15 12:08:30 +0000975 if (continuousUpdates)
976 req = cuRegion.union_(requested);
977 else
978 req = requested;
979
Pierre Ossmancef3cf72016-11-25 10:06:34 +0100980 if (req.is_empty())
981 return;
982
Pierre Ossman8efc7b42018-03-23 11:45:51 +0100983 // Get any framebuffer changes we haven't yet been informed of
984 pending = server->getPendingRegion();
985
Pierre Ossmancef3cf72016-11-25 10:06:34 +0100986 // Get the lists of updates. Prior to exporting the data to the `ui' object,
987 // getUpdateInfo() will normalize the `updates' object such way that its
988 // `changed' and `copied' regions would not intersect.
Pierre Ossman1b478e52011-11-15 12:08:30 +0000989 updates.getUpdateInfo(&ui, req);
990 needNewUpdateInfo = false;
Constantin Kaplinsky604d7812007-08-31 15:50:37 +0000991
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000992 // If the previous position of the rendered cursor overlaps the source of the
993 // copy, then when the copy happens the corresponding rectangle in the
994 // destination will be wrong, so add it to the changed region.
995
Pierre Ossman671d2ef2015-06-09 16:09:06 +0200996 if (!ui.copied.is_empty() && !damagedCursorRegion.is_empty()) {
997 Region bogusCopiedCursor;
998
Pierre Ossman74385d32018-03-22 16:00:18 +0100999 bogusCopiedCursor = damagedCursorRegion;
Pierre Ossman671d2ef2015-06-09 16:09:06 +02001000 bogusCopiedCursor.translate(ui.copy_delta);
1001 bogusCopiedCursor.assign_intersect(server->pb->getRect());
Constantin Kaplinsky45517c82007-08-31 15:56:33 +00001002 if (!ui.copied.intersect(bogusCopiedCursor).is_empty()) {
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001003 updates.add_changed(bogusCopiedCursor);
Constantin Kaplinsky604d7812007-08-31 15:50:37 +00001004 needNewUpdateInfo = true;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001005 }
1006 }
1007
Pierre Ossman671d2ef2015-06-09 16:09:06 +02001008 // If we need to remove the old rendered cursor, just add the region to
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001009 // the changed region.
1010
1011 if (removeRenderedCursor) {
Pierre Ossman671d2ef2015-06-09 16:09:06 +02001012 updates.add_changed(damagedCursorRegion);
Constantin Kaplinsky604d7812007-08-31 15:50:37 +00001013 needNewUpdateInfo = true;
Pierre Ossman671d2ef2015-06-09 16:09:06 +02001014 damagedCursorRegion.clear();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001015 removeRenderedCursor = false;
1016 }
1017
Pierre Ossman8c3bd692018-03-22 15:58:54 +01001018 // If we need a full cursor update then make sure its entire region
1019 // is marked as changed.
1020
1021 if (updateRenderedCursor) {
1022 updates.add_changed(server->getRenderedCursor()->getEffectiveRect());
1023 needNewUpdateInfo = true;
1024 updateRenderedCursor = false;
1025 }
1026
Constantin Kaplinsky604d7812007-08-31 15:50:37 +00001027 // The `updates' object could change, make sure we have valid update info.
1028
1029 if (needNewUpdateInfo)
Pierre Ossman1b478e52011-11-15 12:08:30 +00001030 updates.getUpdateInfo(&ui, req);
Constantin Kaplinsky604d7812007-08-31 15:50:37 +00001031
Pierre Ossman8efc7b42018-03-23 11:45:51 +01001032 // If there are queued updates then we cannot safely send an update
1033 // without risking a partially updated screen
1034
1035 if (!pending.is_empty()) {
1036 // However we might still be able to send a lossless refresh
1037 req.assign_subtract(pending);
1038 req.assign_subtract(ui.changed);
1039 req.assign_subtract(ui.copied);
1040
1041 ui.changed.clear();
1042 ui.copied.clear();
1043 }
1044
Pierre Ossman8c3bd692018-03-22 15:58:54 +01001045 // Does the client need a server-side rendered cursor?
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001046
Pierre Ossman24684e52016-12-05 16:58:19 +01001047 cursor = NULL;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001048 if (needRenderedCursor()) {
Pierre Ossman671d2ef2015-06-09 16:09:06 +02001049 Rect renderedCursorRect;
1050
Pierre Ossman24684e52016-12-05 16:58:19 +01001051 cursor = server->getRenderedCursor();
Pierre Ossman8c3bd692018-03-22 15:58:54 +01001052 renderedCursorRect = cursor->getEffectiveRect();
Pierre Ossman24684e52016-12-05 16:58:19 +01001053
Pierre Ossman8c3bd692018-03-22 15:58:54 +01001054 // Check that we don't try to copy over the cursor area, and
1055 // if that happens we need to treat it as changed so that we can
1056 // re-render it
1057 if (!ui.copied.intersect(renderedCursorRect).is_empty()) {
1058 ui.changed.assign_union(ui.copied.intersect(renderedCursorRect));
1059 ui.copied.assign_subtract(renderedCursorRect);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001060 }
1061
Pierre Ossman8c3bd692018-03-22 15:58:54 +01001062 // Track where we've rendered the cursor
1063 damagedCursorRegion.assign_union(ui.changed.intersect(renderedCursorRect));
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001064 }
1065
Pierre Ossman8efc7b42018-03-23 11:45:51 +01001066 // Return if there is nothing to send the client.
Peter Åstrand (astrand)7a368c92018-09-19 12:45:17 +02001067 if (ui.is_empty() && !writer()->needFakeUpdate()) {
1068 int eta;
Pierre Ossman8efc7b42018-03-23 11:45:51 +01001069
Peter Åstrand (astrand)7a368c92018-09-19 12:45:17 +02001070 // Any lossless refresh that needs handling?
1071 if (!encodeManager.needsLosslessRefresh(req))
1072 return;
1073
1074 // Now? Or later?
1075 eta = encodeManager.getNextLosslessRefresh(req);
1076 if (eta > 0) {
1077 losslessTimer.start(eta);
1078 return;
1079 }
1080 }
Pierre Ossmanfdba3fe2014-01-31 13:12:18 +01001081
Pierre Ossmancef3cf72016-11-25 10:06:34 +01001082 writeRTTPing();
Pierre Ossman1b478e52011-11-15 12:08:30 +00001083
Pierre Ossman6b2f1132016-11-30 08:03:35 +01001084 if (!ui.is_empty())
1085 encodeManager.writeUpdate(ui, server->getPixelBuffer(), cursor);
Pierre Ossmana2b80d62018-03-23 09:30:09 +01001086 else {
Pierre Ossmanc3ce5ce2018-09-19 16:31:18 +02001087 int nextUpdate;
Pierre Ossmana2b80d62018-03-23 09:30:09 +01001088
1089 // FIXME: If continuous updates aren't used then the client might
1090 // be slower than frameRate in its requests and we could
1091 // afford a larger update size
Pierre Ossmanc3ce5ce2018-09-19 16:31:18 +02001092 nextUpdate = server->msToNextUpdate();
1093 if (nextUpdate > 0) {
Pierre Ossmancb5a3992018-09-19 16:35:40 +02001094 size_t bandwidth, maxUpdateSize;
Pierre Ossmana2b80d62018-03-23 09:30:09 +01001095
Pierre Ossmanc3ce5ce2018-09-19 16:31:18 +02001096 // FIXME: Bandwidth estimation without congestion control
Pierre Ossmancb5a3992018-09-19 16:35:40 +02001097 bandwidth = congestion.getBandwidth();
Pierre Ossmana2b80d62018-03-23 09:30:09 +01001098
Pierre Ossmancb5a3992018-09-19 16:35:40 +02001099 // FIXME: Hard coded value for maximum CPU throughput
1100 if (bandwidth > 5000000)
1101 bandwidth = 5000000;
1102
1103 maxUpdateSize = bandwidth * nextUpdate / 1000;
Pierre Ossmanc3ce5ce2018-09-19 16:31:18 +02001104 encodeManager.writeLosslessRefresh(req, server->getPixelBuffer(),
1105 cursor, maxUpdateSize);
1106 }
Pierre Ossmana2b80d62018-03-23 09:30:09 +01001107 }
Pierre Ossman1b478e52011-11-15 12:08:30 +00001108
Pierre Ossmancef3cf72016-11-25 10:06:34 +01001109 writeRTTPing();
Pierre Ossmanc0397262014-03-14 15:59:46 +01001110
Pierre Ossmancef3cf72016-11-25 10:06:34 +01001111 // The request might be for just part of the screen, so we cannot
1112 // just clear the entire update tracker.
1113 updates.subtract(req);
Pierre Ossmanc0397262014-03-14 15:59:46 +01001114
Pierre Ossmancef3cf72016-11-25 10:06:34 +01001115 requested.clear();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001116}
1117
1118
Pierre Ossmana3ac01e2011-11-07 21:13:54 +00001119void VNCSConnectionST::screenLayoutChange(rdr::U16 reason)
1120{
1121 if (!authenticated())
1122 return;
1123
Pierre Ossman0d3ce872018-06-18 15:59:00 +02001124 client.setDimensions(client.width(), client.height(),
1125 server->screenLayout);
Pierre Ossmana3ac01e2011-11-07 21:13:54 +00001126
1127 if (state() != RFBSTATE_NORMAL)
1128 return;
1129
Pierre Ossman9312b0e2018-06-20 12:25:14 +02001130 writer()->writeExtendedDesktopSize(reason, 0,
Pierre Ossman0d3ce872018-06-18 15:59:00 +02001131 client.width(), client.height(),
1132 client.screenLayout());
Pierre Ossmana3ac01e2011-11-07 21:13:54 +00001133}
1134
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001135
1136// setCursor() is called whenever the cursor has changed shape or pixel format.
1137// If the client supports local cursor then it will arrange for the cursor to
1138// be sent to the client.
1139
1140void VNCSConnectionST::setCursor()
1141{
Pierre Ossmana3ac01e2011-11-07 21:13:54 +00001142 if (state() != RFBSTATE_NORMAL)
1143 return;
Pierre Ossmana3ac01e2011-11-07 21:13:54 +00001144
Pierre Ossman71ca8d52017-09-15 11:03:12 +02001145 // We need to blank out the client's cursor or there will be two
Pierre Ossman25db44a2017-11-16 16:40:44 +01001146 if (needRenderedCursor()) {
Pierre Ossman0d3ce872018-06-18 15:59:00 +02001147 client.setCursor(emptyCursor);
Pierre Ossman25db44a2017-11-16 16:40:44 +01001148 clientHasCursor = false;
1149 } else {
Pierre Ossman0d3ce872018-06-18 15:59:00 +02001150 client.setCursor(*server->cursor);
Pierre Ossman25db44a2017-11-16 16:40:44 +01001151 clientHasCursor = true;
1152 }
Pierre Ossman126e5642014-02-13 14:40:25 +01001153
Pierre Ossman8053c8e2017-02-21 12:59:04 +01001154 if (!writer()->writeSetCursorWithAlpha()) {
1155 if (!writer()->writeSetCursor()) {
1156 if (!writer()->writeSetXCursor()) {
1157 // No client support
1158 return;
1159 }
Pierre Ossman126e5642014-02-13 14:40:25 +01001160 }
1161 }
Pierre Ossmana3ac01e2011-11-07 21:13:54 +00001162}
1163
1164void VNCSConnectionST::setDesktopName(const char *name)
1165{
Pierre Ossman0d3ce872018-06-18 15:59:00 +02001166 client.setName(name);
Pierre Ossmana3ac01e2011-11-07 21:13:54 +00001167
1168 if (state() != RFBSTATE_NORMAL)
1169 return;
1170
1171 if (!writer()->writeSetDesktopName()) {
1172 fprintf(stderr, "Client does not support desktop rename\n");
1173 return;
1174 }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001175}
1176
Pierre Ossmanb45a84f2016-12-12 16:59:15 +01001177void VNCSConnectionST::setLEDState(unsigned int ledstate)
1178{
1179 if (state() != RFBSTATE_NORMAL)
1180 return;
1181
Pierre Ossman0d3ce872018-06-18 15:59:00 +02001182 client.setLEDState(ledstate);
Pierre Ossmanb45a84f2016-12-12 16:59:15 +01001183
Pierre Ossmanb218ecd2017-11-16 16:43:13 +01001184 writer()->writeLEDState();
Pierre Ossmanb45a84f2016-12-12 16:59:15 +01001185}
1186
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001187void VNCSConnectionST::setSocketTimeouts()
1188{
1189 int timeoutms = rfb::Server::clientWaitTimeMillis;
1190 soonestTimeout(&timeoutms, secsToMillis(rfb::Server::idleTimeout));
1191 if (timeoutms == 0)
1192 timeoutms = -1;
1193 sock->inStream().setTimeout(timeoutms);
1194 sock->outStream().setTimeout(timeoutms);
1195}
1196
1197char* VNCSConnectionST::getStartTime()
1198{
1199 char* result = ctime(&startTime);
1200 result[24] = '\0';
1201 return result;
1202}
1203
1204void VNCSConnectionST::setStatus(int status)
1205{
1206 switch (status) {
1207 case 0:
1208 accessRights = accessRights | AccessPtrEvents | AccessKeyEvents | AccessView;
1209 break;
1210 case 1:
Pierre Ossman7728be22015-03-03 16:39:37 +01001211 accessRights = (accessRights & ~(AccessPtrEvents | AccessKeyEvents)) | AccessView;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001212 break;
1213 case 2:
Adam Tkac8e985062011-02-07 11:33:57 +00001214 accessRights = accessRights & ~(AccessPtrEvents | AccessKeyEvents | AccessView);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001215 break;
1216 }
1217 framebufferUpdateRequest(server->pb->getRect(), false);
1218}
1219int VNCSConnectionST::getStatus()
1220{
1221 if ((accessRights & (AccessPtrEvents | AccessKeyEvents | AccessView)) == 0x0007)
1222 return 0;
1223 if ((accessRights & (AccessPtrEvents | AccessKeyEvents | AccessView)) == 0x0001)
1224 return 1;
1225 if ((accessRights & (AccessPtrEvents | AccessKeyEvents | AccessView)) == 0x0000)
1226 return 2;
1227 return 4;
1228}
1229