blob: e4e5ab3e604686a22fb344d7f74d3966b921c5e5 [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)) {
Pierre Ossmandfc28f12018-12-10 20:16:12 +0100719 writer()->writeExtendedDesktopSize(reasonClient, resultInvalid);
Pierre Ossman04e62db2009-03-23 16:57:07 +0000720 return;
721 }
722
723 // FIXME: the desktop will call back to VNCServerST and an extra set
724 // of ExtendedDesktopSize messages will be sent. This is okay
725 // protocol-wise, but unnecessary.
726 result = server->desktop->setScreenLayout(fb_width, fb_height, layout);
727
Pierre Ossmandfc28f12018-12-10 20:16:12 +0100728 writer()->writeExtendedDesktopSize(reasonClient, result);
Pierre Ossman04e62db2009-03-23 16:57:07 +0000729
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000730 // Only notify other clients on success
Pierre Ossman04e62db2009-03-23 16:57:07 +0000731 if (result == resultSuccess) {
732 if (server->screenLayout != layout)
733 throw Exception("Desktop configured a different screen layout than requested");
734 server->notifyScreenLayoutChange(this);
735 }
Pierre Ossmanc5e25602009-03-20 12:59:05 +0000736}
737
Pierre Ossman2c764942011-11-14 15:54:30 +0000738void VNCSConnectionST::fence(rdr::U32 flags, unsigned len, const char data[])
739{
Pierre Ossmanb2a417c2015-12-11 19:32:21 +0100740 rdr::U8 type;
741
Pierre Ossman2c764942011-11-14 15:54:30 +0000742 if (flags & fenceFlagRequest) {
743 if (flags & fenceFlagSyncNext) {
Pierre Ossmanb8b1e962012-07-20 10:47:00 +0000744 pendingSyncFence = true;
Pierre Ossman2c764942011-11-14 15:54:30 +0000745
746 fenceFlags = flags & (fenceFlagBlockBefore | fenceFlagBlockAfter | fenceFlagSyncNext);
747 fenceDataLen = len;
748 delete [] fenceData;
Michal Srbf3afa242017-03-27 19:02:15 +0300749 fenceData = NULL;
Pierre Ossman2c764942011-11-14 15:54:30 +0000750 if (len > 0) {
751 fenceData = new char[len];
752 memcpy(fenceData, data, len);
753 }
754
755 return;
756 }
757
758 // We handle everything synchronously so we trivially honor these modes
759 flags = flags & (fenceFlagBlockBefore | fenceFlagBlockAfter);
760
761 writer()->writeFence(flags, len, data);
762 return;
763 }
764
Pierre Ossmanb2a417c2015-12-11 19:32:21 +0100765 if (len < 1)
766 vlog.error("Fence response of unexpected size received");
Pierre Ossman1b478e52011-11-15 12:08:30 +0000767
Pierre Ossmanb2a417c2015-12-11 19:32:21 +0100768 type = data[0];
769
770 switch (type) {
Pierre Ossman2c764942011-11-14 15:54:30 +0000771 case 0:
772 // Initial dummy fence;
773 break;
Pierre Ossmanb2a417c2015-12-11 19:32:21 +0100774 case 1:
Pierre Ossmanc09e5582015-12-11 20:23:17 +0100775 congestion.gotPong();
Pierre Ossman1b478e52011-11-15 12:08:30 +0000776 break;
Pierre Ossman2c764942011-11-14 15:54:30 +0000777 default:
Pierre Ossmanb2a417c2015-12-11 19:32:21 +0100778 vlog.error("Fence response of unexpected type received");
Pierre Ossman2c764942011-11-14 15:54:30 +0000779 }
780}
781
Pierre Ossman1b478e52011-11-15 12:08:30 +0000782void VNCSConnectionST::enableContinuousUpdates(bool enable,
783 int x, int y, int w, int h)
784{
785 Rect rect;
786
Pierre Ossmanb114f5c2018-06-18 16:34:16 +0200787 if (!client.supportsFence() || !client.supportsContinuousUpdates())
Pierre Ossman1b478e52011-11-15 12:08:30 +0000788 throw Exception("Client tried to enable continuous updates when not allowed");
789
790 continuousUpdates = enable;
791
792 rect.setXYWH(x, y, w, h);
793 cuRegion.reset(rect);
794
795 if (enable) {
796 requested.clear();
Pierre Ossman1b478e52011-11-15 12:08:30 +0000797 } else {
798 writer()->writeEndOfContinuousUpdates();
799 }
800}
801
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000802// supportsLocalCursor() is called whenever the status of
Pierre Ossmanb114f5c2018-06-18 16:34:16 +0200803// client.supportsLocalCursor() has changed. If the client does now support local
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000804// cursor, we make sure that the old server-side rendered cursor is cleaned up
805// and the cursor is sent to the client.
806
807void VNCSConnectionST::supportsLocalCursor()
808{
Pierre Ossman387a4172017-11-16 16:44:36 +0100809 bool hasRenderedCursor = !damagedCursorRegion.is_empty();
810 if (hasRenderedCursor && !needRenderedCursor())
811 removeRenderedCursor = true;
812 setCursor();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000813}
814
Pierre Ossman2c764942011-11-14 15:54:30 +0000815void VNCSConnectionST::supportsFence()
816{
Pierre Ossmanb2a417c2015-12-11 19:32:21 +0100817 char type = 0;
818 writer()->writeFence(fenceFlagRequest, sizeof(type), &type);
Pierre Ossman2c764942011-11-14 15:54:30 +0000819}
820
Pierre Ossman1b478e52011-11-15 12:08:30 +0000821void VNCSConnectionST::supportsContinuousUpdates()
822{
823 // We refuse to use continuous updates if we cannot monitor the buffer
824 // usage using fences.
Pierre Ossmanb114f5c2018-06-18 16:34:16 +0200825 if (!client.supportsFence())
Pierre Ossman1b478e52011-11-15 12:08:30 +0000826 return;
827
828 writer()->writeEndOfContinuousUpdates();
829}
830
Pierre Ossmanb45a84f2016-12-12 16:59:15 +0100831void VNCSConnectionST::supportsLEDState()
832{
833 writer()->writeLEDState();
834}
835
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000836
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +0000837bool VNCSConnectionST::handleTimeout(Timer* t)
838{
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000839 try {
Peter Åstrand (astrand)7a368c92018-09-19 12:45:17 +0200840 if ((t == &congestionTimer) ||
841 (t == &losslessTimer))
Pierre Ossmana99d14d2015-12-13 15:43:46 +0100842 writeFramebufferUpdate();
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000843 } catch (rdr::Exception& e) {
844 close(e.str());
845 }
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +0000846
847 return false;
848}
849
Pierre Ossman851e6802017-09-12 16:44:44 +0200850bool VNCSConnectionST::isShiftPressed()
851{
852 std::map<rdr::U32, rdr::U32>::const_iterator iter;
853
854 for (iter = pressedKeys.begin(); iter != pressedKeys.end(); ++iter) {
855 if (iter->second == XK_Shift_L)
856 return true;
857 if (iter->second == XK_Shift_R)
858 return true;
859 }
860
861 return false;
862}
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +0000863
Pierre Ossman1b478e52011-11-15 12:08:30 +0000864void VNCSConnectionST::writeRTTPing()
865{
Pierre Ossmanb2a417c2015-12-11 19:32:21 +0100866 char type;
Pierre Ossman1b478e52011-11-15 12:08:30 +0000867
Pierre Ossmanb114f5c2018-06-18 16:34:16 +0200868 if (!client.supportsFence())
Pierre Ossman1b478e52011-11-15 12:08:30 +0000869 return;
870
Pierre Ossmana99d14d2015-12-13 15:43:46 +0100871 congestion.updatePosition(sock->outStream().length());
Pierre Ossman1b478e52011-11-15 12:08:30 +0000872
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.
Pierre Ossmanb2a417c2015-12-11 19:32:21 +0100876 type = 1;
Pierre Ossman1b478e52011-11-15 12:08:30 +0000877 writer()->writeFence(fenceFlagRequest | fenceFlagBlockBefore,
Pierre Ossmanb2a417c2015-12-11 19:32:21 +0100878 sizeof(type), &type);
Pierre Ossman1b478e52011-11-15 12:08:30 +0000879
Pierre Ossmana99d14d2015-12-13 15:43:46 +0100880 congestion.sentPing();
Pierre Ossman1b478e52011-11-15 12:08:30 +0000881}
882
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +0000883bool VNCSConnectionST::isCongested()
884{
Pierre Ossmance261812018-07-17 15:01:53 +0200885 int eta;
Pierre Ossmana99d14d2015-12-13 15:43:46 +0100886
887 congestionTimer.stop();
Pierre Ossman1b478e52011-11-15 12:08:30 +0000888
889 // Stuff still waiting in the send buffer?
Pierre Ossman352d0622016-04-29 15:50:54 +0200890 sock->outStream().flush();
Pierre Ossman8cf71632016-03-11 09:38:08 +0100891 congestion.debugTrace("congestion-trace.csv", sock->getFd());
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +0000892 if (sock->outStream().bufferUsage() > 0)
893 return true;
894
Pierre Ossmanb114f5c2018-06-18 16:34:16 +0200895 if (!client.supportsFence())
Pierre Ossman1b478e52011-11-15 12:08:30 +0000896 return false;
897
Pierre Ossmana99d14d2015-12-13 15:43:46 +0100898 congestion.updatePosition(sock->outStream().length());
899 if (!congestion.isCongested())
Pierre Ossman1b478e52011-11-15 12:08:30 +0000900 return false;
901
Pierre Ossmana99d14d2015-12-13 15:43:46 +0100902 eta = congestion.getUncongestedETA();
903 if (eta >= 0)
904 congestionTimer.start(eta);
Pierre Ossman1b478e52011-11-15 12:08:30 +0000905
906 return true;
907}
908
909
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000910void VNCSConnectionST::writeFramebufferUpdate()
911{
Pierre Ossmana99d14d2015-12-13 15:43:46 +0100912 congestion.updatePosition(sock->outStream().length());
913
Pierre Ossman2c764942011-11-14 15:54:30 +0000914 // We're in the middle of processing a command that's supposed to be
915 // synchronised. Allowing an update to slip out right now might violate
916 // that synchronisation.
917 if (syncFence)
918 return;
919
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000920 // We try to aggregate responses, so don't send out anything whilst we
921 // still have incoming messages. processMessages() will give us another
922 // chance to run once things are idle.
923 if (inProcessMessages)
924 return;
925
Pierre Ossman1b478e52011-11-15 12:08:30 +0000926 if (state() != RFBSTATE_NORMAL)
927 return;
928 if (requested.is_empty() && !continuousUpdates)
Pierre Ossmane9962f72009-04-23 12:31:42 +0000929 return;
Pierre Ossmand9a59ba2009-03-20 15:55:37 +0000930
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +0000931 // Check that we actually have some space on the link and retry in a
932 // bit if things are congested.
Pierre Ossmana40ab202016-04-29 15:35:56 +0200933 if (isCongested())
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +0000934 return;
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +0000935
Pierre Ossmancef3cf72016-11-25 10:06:34 +0100936 // Updates often consists of many small writes, and in continuous
937 // mode, we will also have small fence messages around the update. We
938 // need to aggregate these in order to not clog up TCP's congestion
939 // window.
Peter Åstrand (astrand)01dc1a62017-10-10 12:56:04 +0200940 sock->cork(true);
Pierre Ossman36dadf82011-11-15 12:11:32 +0000941
Pierre Ossmane9962f72009-04-23 12:31:42 +0000942 // First take care of any updates that cannot contain framebuffer data
943 // changes.
Pierre Ossmancef3cf72016-11-25 10:06:34 +0100944 writeNoDataUpdate();
945
946 // Then real data (if possible)
947 writeDataUpdate();
948
Peter Åstrand (astrand)01dc1a62017-10-10 12:56:04 +0200949 sock->cork(false);
Pierre Ossmana99d14d2015-12-13 15:43:46 +0100950
951 congestion.updatePosition(sock->outStream().length());
Pierre Ossmancef3cf72016-11-25 10:06:34 +0100952}
953
954void VNCSConnectionST::writeNoDataUpdate()
955{
956 if (!writer()->needNoDataUpdate())
957 return;
958
959 writer()->writeNoDataUpdate();
960
961 // Make sure no data update is sent until next request
962 requested.clear();
963}
964
965void VNCSConnectionST::writeDataUpdate()
966{
Pierre Ossman8efc7b42018-03-23 11:45:51 +0100967 Region req, pending;
Pierre Ossmancef3cf72016-11-25 10:06:34 +0100968 UpdateInfo ui;
969 bool needNewUpdateInfo;
Pierre Ossman24684e52016-12-05 16:58:19 +0100970 const RenderedCursor *cursor;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000971
Pierre Ossmancef3cf72016-11-25 10:06:34 +0100972 // See what the client has requested (if anything)
Pierre Ossman1b478e52011-11-15 12:08:30 +0000973 if (continuousUpdates)
974 req = cuRegion.union_(requested);
975 else
976 req = requested;
977
Pierre Ossmancef3cf72016-11-25 10:06:34 +0100978 if (req.is_empty())
979 return;
980
Pierre Ossman8efc7b42018-03-23 11:45:51 +0100981 // Get any framebuffer changes we haven't yet been informed of
982 pending = server->getPendingRegion();
983
Pierre Ossmancef3cf72016-11-25 10:06:34 +0100984 // Get the lists of updates. Prior to exporting the data to the `ui' object,
985 // getUpdateInfo() will normalize the `updates' object such way that its
986 // `changed' and `copied' regions would not intersect.
Pierre Ossman1b478e52011-11-15 12:08:30 +0000987 updates.getUpdateInfo(&ui, req);
988 needNewUpdateInfo = false;
Constantin Kaplinsky604d7812007-08-31 15:50:37 +0000989
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000990 // If the previous position of the rendered cursor overlaps the source of the
991 // copy, then when the copy happens the corresponding rectangle in the
992 // destination will be wrong, so add it to the changed region.
993
Pierre Ossman671d2ef2015-06-09 16:09:06 +0200994 if (!ui.copied.is_empty() && !damagedCursorRegion.is_empty()) {
995 Region bogusCopiedCursor;
996
Pierre Ossman74385d32018-03-22 16:00:18 +0100997 bogusCopiedCursor = damagedCursorRegion;
Pierre Ossman671d2ef2015-06-09 16:09:06 +0200998 bogusCopiedCursor.translate(ui.copy_delta);
999 bogusCopiedCursor.assign_intersect(server->pb->getRect());
Constantin Kaplinsky45517c82007-08-31 15:56:33 +00001000 if (!ui.copied.intersect(bogusCopiedCursor).is_empty()) {
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001001 updates.add_changed(bogusCopiedCursor);
Constantin Kaplinsky604d7812007-08-31 15:50:37 +00001002 needNewUpdateInfo = true;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001003 }
1004 }
1005
Pierre Ossman671d2ef2015-06-09 16:09:06 +02001006 // If we need to remove the old rendered cursor, just add the region to
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001007 // the changed region.
1008
1009 if (removeRenderedCursor) {
Pierre Ossman671d2ef2015-06-09 16:09:06 +02001010 updates.add_changed(damagedCursorRegion);
Constantin Kaplinsky604d7812007-08-31 15:50:37 +00001011 needNewUpdateInfo = true;
Pierre Ossman671d2ef2015-06-09 16:09:06 +02001012 damagedCursorRegion.clear();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001013 removeRenderedCursor = false;
1014 }
1015
Pierre Ossman8c3bd692018-03-22 15:58:54 +01001016 // If we need a full cursor update then make sure its entire region
1017 // is marked as changed.
1018
1019 if (updateRenderedCursor) {
1020 updates.add_changed(server->getRenderedCursor()->getEffectiveRect());
1021 needNewUpdateInfo = true;
1022 updateRenderedCursor = false;
1023 }
1024
Constantin Kaplinsky604d7812007-08-31 15:50:37 +00001025 // The `updates' object could change, make sure we have valid update info.
1026
1027 if (needNewUpdateInfo)
Pierre Ossman1b478e52011-11-15 12:08:30 +00001028 updates.getUpdateInfo(&ui, req);
Constantin Kaplinsky604d7812007-08-31 15:50:37 +00001029
Pierre Ossman8efc7b42018-03-23 11:45:51 +01001030 // If there are queued updates then we cannot safely send an update
1031 // without risking a partially updated screen
1032
1033 if (!pending.is_empty()) {
1034 // However we might still be able to send a lossless refresh
1035 req.assign_subtract(pending);
1036 req.assign_subtract(ui.changed);
1037 req.assign_subtract(ui.copied);
1038
1039 ui.changed.clear();
1040 ui.copied.clear();
1041 }
1042
Pierre Ossman8c3bd692018-03-22 15:58:54 +01001043 // Does the client need a server-side rendered cursor?
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001044
Pierre Ossman24684e52016-12-05 16:58:19 +01001045 cursor = NULL;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001046 if (needRenderedCursor()) {
Pierre Ossman671d2ef2015-06-09 16:09:06 +02001047 Rect renderedCursorRect;
1048
Pierre Ossman24684e52016-12-05 16:58:19 +01001049 cursor = server->getRenderedCursor();
Pierre Ossman8c3bd692018-03-22 15:58:54 +01001050 renderedCursorRect = cursor->getEffectiveRect();
Pierre Ossman24684e52016-12-05 16:58:19 +01001051
Pierre Ossman8c3bd692018-03-22 15:58:54 +01001052 // Check that we don't try to copy over the cursor area, and
1053 // if that happens we need to treat it as changed so that we can
1054 // re-render it
1055 if (!ui.copied.intersect(renderedCursorRect).is_empty()) {
1056 ui.changed.assign_union(ui.copied.intersect(renderedCursorRect));
1057 ui.copied.assign_subtract(renderedCursorRect);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001058 }
1059
Pierre Ossman8c3bd692018-03-22 15:58:54 +01001060 // Track where we've rendered the cursor
1061 damagedCursorRegion.assign_union(ui.changed.intersect(renderedCursorRect));
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001062 }
1063
Pierre Ossman8efc7b42018-03-23 11:45:51 +01001064 // Return if there is nothing to send the client.
Peter Åstrand (astrand)7a368c92018-09-19 12:45:17 +02001065 if (ui.is_empty() && !writer()->needFakeUpdate()) {
1066 int eta;
Pierre Ossman8efc7b42018-03-23 11:45:51 +01001067
Peter Åstrand (astrand)7a368c92018-09-19 12:45:17 +02001068 // Any lossless refresh that needs handling?
1069 if (!encodeManager.needsLosslessRefresh(req))
1070 return;
1071
1072 // Now? Or later?
1073 eta = encodeManager.getNextLosslessRefresh(req);
1074 if (eta > 0) {
1075 losslessTimer.start(eta);
1076 return;
1077 }
1078 }
Pierre Ossmanfdba3fe2014-01-31 13:12:18 +01001079
Pierre Ossmancef3cf72016-11-25 10:06:34 +01001080 writeRTTPing();
Pierre Ossman1b478e52011-11-15 12:08:30 +00001081
Pierre Ossman6b2f1132016-11-30 08:03:35 +01001082 if (!ui.is_empty())
1083 encodeManager.writeUpdate(ui, server->getPixelBuffer(), cursor);
Pierre Ossmana2b80d62018-03-23 09:30:09 +01001084 else {
Pierre Ossmanc3ce5ce2018-09-19 16:31:18 +02001085 int nextUpdate;
Pierre Ossmana2b80d62018-03-23 09:30:09 +01001086
1087 // FIXME: If continuous updates aren't used then the client might
1088 // be slower than frameRate in its requests and we could
1089 // afford a larger update size
Pierre Ossmanc3ce5ce2018-09-19 16:31:18 +02001090 nextUpdate = server->msToNextUpdate();
1091 if (nextUpdate > 0) {
Pierre Ossmancb5a3992018-09-19 16:35:40 +02001092 size_t bandwidth, maxUpdateSize;
Pierre Ossmana2b80d62018-03-23 09:30:09 +01001093
Pierre Ossmanc3ce5ce2018-09-19 16:31:18 +02001094 // FIXME: Bandwidth estimation without congestion control
Pierre Ossmancb5a3992018-09-19 16:35:40 +02001095 bandwidth = congestion.getBandwidth();
Pierre Ossmana2b80d62018-03-23 09:30:09 +01001096
Pierre Ossmancb5a3992018-09-19 16:35:40 +02001097 // FIXME: Hard coded value for maximum CPU throughput
1098 if (bandwidth > 5000000)
1099 bandwidth = 5000000;
1100
1101 maxUpdateSize = bandwidth * nextUpdate / 1000;
Pierre Ossmanc3ce5ce2018-09-19 16:31:18 +02001102 encodeManager.writeLosslessRefresh(req, server->getPixelBuffer(),
1103 cursor, maxUpdateSize);
1104 }
Pierre Ossmana2b80d62018-03-23 09:30:09 +01001105 }
Pierre Ossman1b478e52011-11-15 12:08:30 +00001106
Pierre Ossmancef3cf72016-11-25 10:06:34 +01001107 writeRTTPing();
Pierre Ossmanc0397262014-03-14 15:59:46 +01001108
Pierre Ossmancef3cf72016-11-25 10:06:34 +01001109 // The request might be for just part of the screen, so we cannot
1110 // just clear the entire update tracker.
1111 updates.subtract(req);
Pierre Ossmanc0397262014-03-14 15:59:46 +01001112
Pierre Ossmancef3cf72016-11-25 10:06:34 +01001113 requested.clear();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001114}
1115
1116
Pierre Ossmana3ac01e2011-11-07 21:13:54 +00001117void VNCSConnectionST::screenLayoutChange(rdr::U16 reason)
1118{
1119 if (!authenticated())
1120 return;
1121
Pierre Ossman0d3ce872018-06-18 15:59:00 +02001122 client.setDimensions(client.width(), client.height(),
1123 server->screenLayout);
Pierre Ossmana3ac01e2011-11-07 21:13:54 +00001124
1125 if (state() != RFBSTATE_NORMAL)
1126 return;
1127
Pierre Ossmandfc28f12018-12-10 20:16:12 +01001128 writer()->writeExtendedDesktopSize(reason, 0);
Pierre Ossmana3ac01e2011-11-07 21:13:54 +00001129}
1130
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001131
1132// setCursor() is called whenever the cursor has changed shape or pixel format.
1133// If the client supports local cursor then it will arrange for the cursor to
1134// be sent to the client.
1135
1136void VNCSConnectionST::setCursor()
1137{
Pierre Ossmana3ac01e2011-11-07 21:13:54 +00001138 if (state() != RFBSTATE_NORMAL)
1139 return;
Pierre Ossmana3ac01e2011-11-07 21:13:54 +00001140
Pierre Ossman71ca8d52017-09-15 11:03:12 +02001141 // We need to blank out the client's cursor or there will be two
Pierre Ossman25db44a2017-11-16 16:40:44 +01001142 if (needRenderedCursor()) {
Pierre Ossman0d3ce872018-06-18 15:59:00 +02001143 client.setCursor(emptyCursor);
Pierre Ossman25db44a2017-11-16 16:40:44 +01001144 clientHasCursor = false;
1145 } else {
Pierre Ossman0d3ce872018-06-18 15:59:00 +02001146 client.setCursor(*server->cursor);
Pierre Ossman25db44a2017-11-16 16:40:44 +01001147 clientHasCursor = true;
1148 }
Pierre Ossman126e5642014-02-13 14:40:25 +01001149
Pierre Ossman8053c8e2017-02-21 12:59:04 +01001150 if (!writer()->writeSetCursorWithAlpha()) {
1151 if (!writer()->writeSetCursor()) {
1152 if (!writer()->writeSetXCursor()) {
1153 // No client support
1154 return;
1155 }
Pierre Ossman126e5642014-02-13 14:40:25 +01001156 }
1157 }
Pierre Ossmana3ac01e2011-11-07 21:13:54 +00001158}
1159
1160void VNCSConnectionST::setDesktopName(const char *name)
1161{
Pierre Ossman0d3ce872018-06-18 15:59:00 +02001162 client.setName(name);
Pierre Ossmana3ac01e2011-11-07 21:13:54 +00001163
1164 if (state() != RFBSTATE_NORMAL)
1165 return;
1166
1167 if (!writer()->writeSetDesktopName()) {
1168 fprintf(stderr, "Client does not support desktop rename\n");
1169 return;
1170 }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001171}
1172
Pierre Ossmanb45a84f2016-12-12 16:59:15 +01001173void VNCSConnectionST::setLEDState(unsigned int ledstate)
1174{
1175 if (state() != RFBSTATE_NORMAL)
1176 return;
1177
Pierre Ossman0d3ce872018-06-18 15:59:00 +02001178 client.setLEDState(ledstate);
Pierre Ossmanb45a84f2016-12-12 16:59:15 +01001179
Pierre Ossmanb218ecd2017-11-16 16:43:13 +01001180 writer()->writeLEDState();
Pierre Ossmanb45a84f2016-12-12 16:59:15 +01001181}
1182
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001183void VNCSConnectionST::setSocketTimeouts()
1184{
1185 int timeoutms = rfb::Server::clientWaitTimeMillis;
1186 soonestTimeout(&timeoutms, secsToMillis(rfb::Server::idleTimeout));
1187 if (timeoutms == 0)
1188 timeoutms = -1;
1189 sock->inStream().setTimeout(timeoutms);
1190 sock->outStream().setTimeout(timeoutms);
1191}
1192
1193char* VNCSConnectionST::getStartTime()
1194{
1195 char* result = ctime(&startTime);
1196 result[24] = '\0';
1197 return result;
1198}
1199
1200void VNCSConnectionST::setStatus(int status)
1201{
1202 switch (status) {
1203 case 0:
1204 accessRights = accessRights | AccessPtrEvents | AccessKeyEvents | AccessView;
1205 break;
1206 case 1:
Pierre Ossman7728be22015-03-03 16:39:37 +01001207 accessRights = (accessRights & ~(AccessPtrEvents | AccessKeyEvents)) | AccessView;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001208 break;
1209 case 2:
Adam Tkac8e985062011-02-07 11:33:57 +00001210 accessRights = accessRights & ~(AccessPtrEvents | AccessKeyEvents | AccessView);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001211 break;
1212 }
1213 framebufferUpdateRequest(server->pb->getRect(), false);
1214}
1215int VNCSConnectionST::getStatus()
1216{
1217 if ((accessRights & (AccessPtrEvents | AccessKeyEvents | AccessView)) == 0x0007)
1218 return 0;
1219 if ((accessRights & (AccessPtrEvents | AccessKeyEvents | AccessView)) == 0x0001)
1220 return 1;
1221 if ((accessRights & (AccessPtrEvents | AccessKeyEvents | AccessView)) == 0x0000)
1222 return 2;
1223 return 4;
1224}
1225