blob: a58fd09f5bf49607efc14fe98b874672ef80c798 [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 Ossman2daba9b2018-10-29 10:03:37 +0100219 if (!client.supportsDesktopSize()) {
220 close("Client does not support desktop resize");
221 return;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000222 }
Pierre Ossman2daba9b2018-10-29 10:03:37 +0100223 writer()->writeDesktopSize(reasonServer);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000224 }
Pierre Ossman6b2f1132016-11-30 08:03:35 +0100225
226 // Drop any lossy tracking that is now outside the framebuffer
227 encodeManager.pruneLosslessRefresh(Region(server->pb->getRect()));
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000228 }
229 // Just update the whole screen at the moment because we're too lazy to
230 // work out what's actually changed.
231 updates.clear();
232 updates.add_changed(server->pb->getRect());
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000233 writeFramebufferUpdate();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000234 } catch(rdr::Exception &e) {
235 close(e.str());
236 }
237}
238
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000239void VNCSConnectionST::writeFramebufferUpdateOrClose()
Pierre Ossman04e62db2009-03-23 16:57:07 +0000240{
241 try {
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000242 writeFramebufferUpdate();
243 } catch(rdr::Exception &e) {
244 close(e.str());
245 }
246}
Pierre Ossman04e62db2009-03-23 16:57:07 +0000247
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000248void VNCSConnectionST::screenLayoutChangeOrClose(rdr::U16 reason)
249{
250 try {
251 screenLayoutChange(reason);
Pierre Ossmanb218ecd2017-11-16 16:43:13 +0100252 writeFramebufferUpdate();
Pierre Ossman04e62db2009-03-23 16:57:07 +0000253 } catch(rdr::Exception &e) {
254 close(e.str());
255 }
256}
257
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000258void VNCSConnectionST::bellOrClose()
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000259{
260 try {
261 if (state() == RFBSTATE_NORMAL) writer()->writeBell();
262 } catch(rdr::Exception& e) {
263 close(e.str());
264 }
265}
266
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000267void VNCSConnectionST::serverCutTextOrClose(const char *str, int len)
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000268{
269 try {
270 if (!(accessRights & AccessCutText)) return;
271 if (!rfb::Server::sendCutText) return;
272 if (state() == RFBSTATE_NORMAL)
273 writer()->writeServerCutText(str, len);
274 } catch(rdr::Exception& e) {
275 close(e.str());
276 }
277}
278
Peter Åstrandc39e0782009-01-15 12:21:42 +0000279
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000280void VNCSConnectionST::setDesktopNameOrClose(const char *name)
Peter Åstrandc39e0782009-01-15 12:21:42 +0000281{
Peter Åstrandc39e0782009-01-15 12:21:42 +0000282 try {
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000283 setDesktopName(name);
Pierre Ossmanb218ecd2017-11-16 16:43:13 +0100284 writeFramebufferUpdate();
Peter Åstrandc39e0782009-01-15 12:21:42 +0000285 } catch(rdr::Exception& e) {
286 close(e.str());
287 }
288}
289
290
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000291void VNCSConnectionST::setCursorOrClose()
292{
293 try {
294 setCursor();
Pierre Ossmanb218ecd2017-11-16 16:43:13 +0100295 writeFramebufferUpdate();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000296 } catch(rdr::Exception& e) {
297 close(e.str());
298 }
299}
300
301
Pierre Ossmanb45a84f2016-12-12 16:59:15 +0100302void VNCSConnectionST::setLEDStateOrClose(unsigned int state)
303{
304 try {
305 setLEDState(state);
Pierre Ossmanb218ecd2017-11-16 16:43:13 +0100306 writeFramebufferUpdate();
Pierre Ossmanb45a84f2016-12-12 16:59:15 +0100307 } catch(rdr::Exception& e) {
308 close(e.str());
309 }
310}
311
312
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000313int VNCSConnectionST::checkIdleTimeout()
314{
315 int idleTimeout = rfb::Server::idleTimeout;
316 if (idleTimeout == 0) return 0;
317 if (state() != RFBSTATE_NORMAL && idleTimeout < 15)
318 idleTimeout = 15; // minimum of 15 seconds while authenticating
319 time_t now = time(0);
320 if (now < lastEventTime) {
321 // Someone must have set the time backwards. Set lastEventTime so that the
322 // idleTimeout will count from now.
323 vlog.info("Time has gone backwards - resetting idle timeout");
324 lastEventTime = now;
325 }
326 int timeLeft = lastEventTime + idleTimeout - now;
327 if (timeLeft < -60) {
328 // Our callback is over a minute late - someone must have set the time
329 // forwards. Set lastEventTime so that the idleTimeout will count from
330 // now.
331 vlog.info("Time has gone forwards - resetting idle timeout");
332 lastEventTime = now;
333 return secsToMillis(idleTimeout);
334 }
335 if (timeLeft <= 0) {
336 close("Idle timeout");
337 return 0;
338 }
339 return secsToMillis(timeLeft);
340}
341
Pierre Ossmanb114cec2011-11-20 15:36:11 +0000342
343bool VNCSConnectionST::getComparerState()
344{
345 // We interpret a low compression level as an indication that the client
346 // wants to prioritise CPU usage over bandwidth, and hence disable the
347 // comparing update tracker.
Pierre Ossman0d3ce872018-06-18 15:59:00 +0200348 return (client.compressLevel == -1) || (client.compressLevel > 1);
Pierre Ossmanb114cec2011-11-20 15:36:11 +0000349}
350
351
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000352// renderedCursorChange() is called whenever the server-side rendered cursor
353// changes shape or position. It ensures that the next update will clean up
354// the old rendered cursor and if necessary draw the new rendered cursor.
355
356void VNCSConnectionST::renderedCursorChange()
357{
358 if (state() != RFBSTATE_NORMAL) return;
Pierre Ossman71ca8d52017-09-15 11:03:12 +0200359 // Are we switching between client-side and server-side cursor?
Pierre Ossman25db44a2017-11-16 16:40:44 +0100360 if (clientHasCursor == needRenderedCursor())
Pierre Ossman71ca8d52017-09-15 11:03:12 +0200361 setCursorOrClose();
Pierre Ossman25db44a2017-11-16 16:40:44 +0100362 bool hasRenderedCursor = !damagedCursorRegion.is_empty();
Pierre Ossman330ca422017-11-06 13:15:55 +0100363 if (hasRenderedCursor)
Pierre Ossman5c9e1e52011-11-08 10:32:05 +0000364 removeRenderedCursor = true;
Pierre Ossman6b0bc292011-12-21 13:17:54 +0000365 if (needRenderedCursor()) {
Pierre Ossman671d2ef2015-06-09 16:09:06 +0200366 updateRenderedCursor = true;
Pierre Ossman6b0bc292011-12-21 13:17:54 +0000367 writeFramebufferUpdateOrClose();
368 }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000369}
370
371// needRenderedCursor() returns true if this client needs the server-side
372// rendered cursor. This may be because it does not support local cursor or
373// because the current cursor position has not been set by this client.
374// Unfortunately we can't know for sure when the current cursor position has
375// been set by this client. We guess that this is the case when the current
376// cursor position is the same as the last pointer event from this client, or
377// if it is a very short time since this client's last pointer event (up to a
378// second). [ Ideally we should do finer-grained timing here and make the time
379// configurable, but I don't think it's that important. ]
380
381bool VNCSConnectionST::needRenderedCursor()
382{
Pierre Ossman77ede0a2016-12-05 16:57:30 +0100383 if (state() != RFBSTATE_NORMAL)
384 return false;
385
Pierre Ossmanb114f5c2018-06-18 16:34:16 +0200386 if (!client.supportsLocalCursor())
Pierre Ossman77ede0a2016-12-05 16:57:30 +0100387 return true;
388 if (!server->cursorPos.equals(pointerEventPos) &&
389 (time(0) - pointerEventTime) > 0)
390 return true;
391
392 return false;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000393}
394
395
396void VNCSConnectionST::approveConnectionOrClose(bool accept,
397 const char* reason)
398{
399 try {
400 approveConnection(accept, reason);
401 } catch (rdr::Exception& e) {
402 close(e.str());
403 }
404}
405
406
407
408// -=- Callbacks from SConnection
409
410void VNCSConnectionST::authSuccess()
411{
412 lastEventTime = time(0);
413
414 server->startDesktop();
415
416 // - Set the connection parameters appropriately
Pierre Ossman0d3ce872018-06-18 15:59:00 +0200417 client.setDimensions(server->pb->width(), server->pb->height(),
418 server->screenLayout);
419 client.setName(server->getName());
420 client.setLEDState(server->ledState);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000421
422 // - Set the default pixel format
Pierre Ossman0d3ce872018-06-18 15:59:00 +0200423 client.setPF(server->pb->getPF());
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000424 char buffer[256];
Pierre Ossman0d3ce872018-06-18 15:59:00 +0200425 client.pf().print(buffer, 256);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000426 vlog.info("Server default pixel format %s", buffer);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000427
428 // - Mark the entire display as "dirty"
429 updates.add_changed(server->pb->getRect());
430 startTime = time(0);
431}
432
433void VNCSConnectionST::queryConnection(const char* userName)
434{
435 // - Authentication succeeded - clear from blacklist
436 CharArray name; name.buf = sock->getPeerAddress();
437 server->blHosts->clearBlackmark(name.buf);
438
439 // - Special case to provide a more useful error message
440 if (rfb::Server::neverShared && !rfb::Server::disconnectClients &&
441 server->authClientCount() > 0) {
442 approveConnection(false, "The server is already in use");
443 return;
444 }
445
446 // - Does the client have the right to bypass the query?
447 if (reverseConnection ||
448 !(rfb::Server::queryConnect || sock->requiresQuery()) ||
449 (accessRights & AccessNoQuery))
450 {
451 approveConnection(true);
452 return;
453 }
454
455 // - Get the server to display an Accept/Reject dialog, if required
456 // If a dialog is displayed, the result will be PENDING, and the
457 // server will call approveConnection at a later time
458 CharArray reason;
459 VNCServerST::queryResult qr = server->queryConnection(sock, userName,
460 &reason.buf);
Pierre Ossman36304752017-10-04 16:21:57 +0200461 if (qr == VNCServerST::PENDING)
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000462 return;
463
464 // - If server returns ACCEPT/REJECT then pass result to SConnection
465 approveConnection(qr == VNCServerST::ACCEPT, reason.buf);
466}
467
468void VNCSConnectionST::clientInit(bool shared)
469{
470 lastEventTime = time(0);
471 if (rfb::Server::alwaysShared || reverseConnection) shared = true;
Pierre Ossmane7be49b2014-12-02 14:33:17 +0100472 if (!(accessRights & AccessNonShared)) shared = true;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000473 if (rfb::Server::neverShared) shared = false;
474 if (!shared) {
Pierre Ossmane7be49b2014-12-02 14:33:17 +0100475 if (rfb::Server::disconnectClients && (accessRights & AccessNonShared)) {
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000476 // - Close all the other connected clients
477 vlog.debug("non-shared connection - closing clients");
478 server->closeClients("Non-shared connection requested", getSock());
479 } else {
480 // - Refuse this connection if there are existing clients, in addition to
481 // this one
482 if (server->authClientCount() > 1) {
483 close("Server is already in use");
484 return;
485 }
486 }
487 }
488 SConnection::clientInit(shared);
489}
490
491void VNCSConnectionST::setPixelFormat(const PixelFormat& pf)
492{
493 SConnection::setPixelFormat(pf);
494 char buffer[256];
495 pf.print(buffer, 256);
496 vlog.info("Client pixel format %s", buffer);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000497 setCursor();
498}
499
500void VNCSConnectionST::pointerEvent(const Point& pos, int buttonMask)
501{
502 pointerEventTime = lastEventTime = time(0);
503 server->lastUserInputTime = lastEventTime;
504 if (!(accessRights & AccessPtrEvents)) return;
505 if (!rfb::Server::acceptPointerEvents) return;
506 if (!server->pointerClient || server->pointerClient == this) {
507 pointerEventPos = pos;
508 if (buttonMask)
509 server->pointerClient = this;
510 else
511 server->pointerClient = 0;
512 server->desktop->pointerEvent(pointerEventPos, buttonMask);
513 }
514}
515
516
517class VNCSConnectionSTShiftPresser {
518public:
519 VNCSConnectionSTShiftPresser(SDesktop* desktop_)
520 : desktop(desktop_), pressed(false) {}
521 ~VNCSConnectionSTShiftPresser() {
Pierre Ossman9a153b02015-08-31 10:01:14 +0200522 if (pressed) {
523 vlog.debug("Releasing fake Shift_L");
Pierre Ossman5ae28212017-05-16 14:30:38 +0200524 desktop->keyEvent(XK_Shift_L, 0, false);
Pierre Ossman9a153b02015-08-31 10:01:14 +0200525 }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000526 }
527 void press() {
Pierre Ossman9a153b02015-08-31 10:01:14 +0200528 vlog.debug("Pressing fake Shift_L");
Pierre Ossman5ae28212017-05-16 14:30:38 +0200529 desktop->keyEvent(XK_Shift_L, 0, true);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000530 pressed = true;
531 }
532 SDesktop* desktop;
533 bool pressed;
534};
535
536// keyEvent() - record in the pressedKeys which keys were pressed. Allow
537// multiple down events (for autorepeat), but only allow a single up event.
Pierre Ossman5ae28212017-05-16 14:30:38 +0200538void VNCSConnectionST::keyEvent(rdr::U32 keysym, rdr::U32 keycode, bool down) {
Pierre Ossman16e1dcb2017-05-16 14:33:43 +0200539 rdr::U32 lookup;
540
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000541 lastEventTime = time(0);
542 server->lastUserInputTime = lastEventTime;
543 if (!(accessRights & AccessKeyEvents)) return;
544 if (!rfb::Server::acceptKeyEvents) return;
545
Pierre Ossman9a153b02015-08-31 10:01:14 +0200546 if (down)
Pierre Ossman5ae28212017-05-16 14:30:38 +0200547 vlog.debug("Key pressed: 0x%x / 0x%x", keysym, keycode);
Pierre Ossman9a153b02015-08-31 10:01:14 +0200548 else
Pierre Ossman5ae28212017-05-16 14:30:38 +0200549 vlog.debug("Key released: 0x%x / 0x%x", keysym, keycode);
Pierre Ossman9a153b02015-08-31 10:01:14 +0200550
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000551 // Remap the key if required
Pierre Ossman9a153b02015-08-31 10:01:14 +0200552 if (server->keyRemapper) {
553 rdr::U32 newkey;
Pierre Ossman5ae28212017-05-16 14:30:38 +0200554 newkey = server->keyRemapper->remapKey(keysym);
555 if (newkey != keysym) {
Pierre Ossman9a153b02015-08-31 10:01:14 +0200556 vlog.debug("Key remapped to 0x%x", newkey);
Pierre Ossman5ae28212017-05-16 14:30:38 +0200557 keysym = newkey;
Pierre Ossman9a153b02015-08-31 10:01:14 +0200558 }
559 }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000560
Pierre Ossmanbb305ca2016-12-11 12:41:26 +0100561 // Avoid lock keys if we don't know the server state
562 if ((server->ledState == ledUnknown) &&
Pierre Ossman5ae28212017-05-16 14:30:38 +0200563 ((keysym == XK_Caps_Lock) ||
564 (keysym == XK_Num_Lock) ||
565 (keysym == XK_Scroll_Lock))) {
Pierre Ossmanbb305ca2016-12-11 12:41:26 +0100566 vlog.debug("Ignoring lock key (e.g. caps lock)");
567 return;
568 }
Pierre Ossmanbb305ca2016-12-11 12:41:26 +0100569
Pierre Ossmanb45a84f2016-12-12 16:59:15 +0100570 // Lock key heuristics
571 // (only for clients that do not support the LED state extension)
Pierre Ossmanb114f5c2018-06-18 16:34:16 +0200572 if (!client.supportsLEDState()) {
Pierre Ossmanb45a84f2016-12-12 16:59:15 +0100573 // Always ignore ScrollLock as we don't have a heuristic
574 // for that
Pierre Ossman5ae28212017-05-16 14:30:38 +0200575 if (keysym == XK_Scroll_Lock) {
Pierre Ossmanb45a84f2016-12-12 16:59:15 +0100576 vlog.debug("Ignoring lock key (e.g. caps lock)");
577 return;
Pierre Ossmanbb305ca2016-12-11 12:41:26 +0100578 }
579
Pierre Ossmanb45a84f2016-12-12 16:59:15 +0100580 if (down && (server->ledState != ledUnknown)) {
581 // CapsLock synchronisation heuristic
582 // (this assumes standard interaction between CapsLock the Shift
583 // keys and normal characters)
Pierre Ossman5ae28212017-05-16 14:30:38 +0200584 if (((keysym >= XK_A) && (keysym <= XK_Z)) ||
585 ((keysym >= XK_a) && (keysym <= XK_z))) {
Pierre Ossmanb45a84f2016-12-12 16:59:15 +0100586 bool uppercase, shift, lock;
Pierre Ossmanbb305ca2016-12-11 12:41:26 +0100587
Pierre Ossman5ae28212017-05-16 14:30:38 +0200588 uppercase = (keysym >= XK_A) && (keysym <= XK_Z);
Pierre Ossman851e6802017-09-12 16:44:44 +0200589 shift = isShiftPressed();
Pierre Ossmanb45a84f2016-12-12 16:59:15 +0100590 lock = server->ledState & ledCapsLock;
Pierre Ossmanbb305ca2016-12-11 12:41:26 +0100591
Pierre Ossmanb45a84f2016-12-12 16:59:15 +0100592 if (lock == (uppercase == shift)) {
593 vlog.debug("Inserting fake CapsLock to get in sync with client");
Pierre Ossman5ae28212017-05-16 14:30:38 +0200594 server->desktop->keyEvent(XK_Caps_Lock, 0, true);
595 server->desktop->keyEvent(XK_Caps_Lock, 0, false);
Pierre Ossmanb45a84f2016-12-12 16:59:15 +0100596 }
597 }
598
599 // NumLock synchronisation heuristic
600 // (this is more cautious because of the differences between Unix,
601 // Windows and macOS)
Pierre Ossman5ae28212017-05-16 14:30:38 +0200602 if (((keysym >= XK_KP_Home) && (keysym <= XK_KP_Delete)) ||
603 ((keysym >= XK_KP_0) && (keysym <= XK_KP_9)) ||
604 (keysym == XK_KP_Separator) || (keysym == XK_KP_Decimal)) {
Pierre Ossmanb45a84f2016-12-12 16:59:15 +0100605 bool number, shift, lock;
606
Pierre Ossman5ae28212017-05-16 14:30:38 +0200607 number = ((keysym >= XK_KP_0) && (keysym <= XK_KP_9)) ||
608 (keysym == XK_KP_Separator) || (keysym == XK_KP_Decimal);
Pierre Ossman851e6802017-09-12 16:44:44 +0200609 shift = isShiftPressed();
Pierre Ossmanb45a84f2016-12-12 16:59:15 +0100610 lock = server->ledState & ledNumLock;
611
612 if (shift) {
613 // We don't know the appropriate NumLock state for when Shift
614 // is pressed as it could be one of:
615 //
616 // a) A Unix client where Shift negates NumLock
617 //
618 // b) A Windows client where Shift only cancels NumLock
619 //
620 // c) A macOS client where Shift doesn't have any effect
621 //
622 } else if (lock == (number == shift)) {
623 vlog.debug("Inserting fake NumLock to get in sync with client");
Pierre Ossman5ae28212017-05-16 14:30:38 +0200624 server->desktop->keyEvent(XK_Num_Lock, 0, true);
625 server->desktop->keyEvent(XK_Num_Lock, 0, false);
Pierre Ossmanb45a84f2016-12-12 16:59:15 +0100626 }
Pierre Ossmanbb305ca2016-12-11 12:41:26 +0100627 }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000628 }
629 }
630
631 // Turn ISO_Left_Tab into shifted Tab.
632 VNCSConnectionSTShiftPresser shiftPresser(server->desktop);
Pierre Ossman5ae28212017-05-16 14:30:38 +0200633 if (keysym == XK_ISO_Left_Tab) {
Pierre Ossman851e6802017-09-12 16:44:44 +0200634 if (!isShiftPressed())
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000635 shiftPresser.press();
Pierre Ossman5ae28212017-05-16 14:30:38 +0200636 keysym = XK_Tab;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000637 }
638
Pierre Ossman16e1dcb2017-05-16 14:33:43 +0200639 // We need to be able to track keys, so generate a fake index when we
640 // aren't given a keycode
641 if (keycode == 0)
642 lookup = 0x80000000 | keysym;
643 else
644 lookup = keycode;
645
646 // We force the same keysym for an already down key for the
647 // sake of sanity
648 if (pressedKeys.find(lookup) != pressedKeys.end())
649 keysym = pressedKeys[lookup];
650
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000651 if (down) {
Pierre Ossman16e1dcb2017-05-16 14:33:43 +0200652 pressedKeys[lookup] = keysym;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000653 } else {
Pierre Ossman16e1dcb2017-05-16 14:33:43 +0200654 if (!pressedKeys.erase(lookup))
Pierre Ossman5ae28212017-05-16 14:30:38 +0200655 return;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000656 }
Pierre Ossman16e1dcb2017-05-16 14:33:43 +0200657
Pierre Ossman5ae28212017-05-16 14:30:38 +0200658 server->desktop->keyEvent(keysym, keycode, down);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000659}
660
661void VNCSConnectionST::clientCutText(const char* str, int len)
662{
663 if (!(accessRights & AccessCutText)) return;
664 if (!rfb::Server::acceptCutText) return;
665 server->desktop->clientCutText(str, len);
666}
667
668void VNCSConnectionST::framebufferUpdateRequest(const Rect& r,bool incremental)
669{
Pierre Ossmane9962f72009-04-23 12:31:42 +0000670 Rect safeRect;
671
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000672 if (!(accessRights & AccessView)) return;
673
674 SConnection::framebufferUpdateRequest(r, incremental);
675
Pierre Ossmand9a59ba2009-03-20 15:55:37 +0000676 // Check that the client isn't sending crappy requests
Pierre Ossman0d3ce872018-06-18 15:59:00 +0200677 if (!r.enclosed_by(Rect(0, 0, client.width(), client.height()))) {
Pierre Ossmand9a59ba2009-03-20 15:55:37 +0000678 vlog.error("FramebufferUpdateRequest %dx%d at %d,%d exceeds framebuffer %dx%d",
Pierre Ossman9312b0e2018-06-20 12:25:14 +0200679 r.width(), r.height(), r.tl.x, r.tl.y,
Pierre Ossman0d3ce872018-06-18 15:59:00 +0200680 client.width(), client.height());
681 safeRect = r.intersect(Rect(0, 0, client.width(), client.height()));
Pierre Ossmane9962f72009-04-23 12:31:42 +0000682 } else {
683 safeRect = r;
Pierre Ossmand9a59ba2009-03-20 15:55:37 +0000684 }
685
Constantin Kaplinsky2ef66952008-08-29 11:33:46 +0000686 // Just update the requested region.
687 // Framebuffer update will be sent a bit later, see processMessages().
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000688 Region reqRgn(r);
Pierre Ossman1b478e52011-11-15 12:08:30 +0000689 if (!incremental || !continuousUpdates)
690 requested.assign_union(reqRgn);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000691
692 if (!incremental) {
693 // Non-incremental update - treat as if area requested has changed
694 updates.add_changed(reqRgn);
Pierre Ossman53125a72009-04-22 15:37:51 +0000695
696 // And send the screen layout to the client (which, unlike the
697 // framebuffer dimensions, the client doesn't get during init)
Pierre Ossman2daba9b2018-10-29 10:03:37 +0100698 if (client.supportsEncoding(pseudoEncodingExtendedDesktopSize))
699 writer()->writeDesktopSize(reasonServer);
Pierre Ossman53125a72009-04-22 15:37:51 +0000700
701 // We do not send a DesktopSize since it only contains the
702 // framebuffer size (which the client already should know) and
703 // because some clients don't handle extra DesktopSize events
704 // very well.
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000705 }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000706}
707
Pierre Ossman34bb0612009-03-21 21:16:14 +0000708void VNCSConnectionST::setDesktopSize(int fb_width, int fb_height,
709 const ScreenSet& layout)
Pierre Ossmanc5e25602009-03-20 12:59:05 +0000710{
Pierre Ossman04e62db2009-03-23 16:57:07 +0000711 unsigned int result;
712
Michal Srbb318b8f2014-11-24 13:18:28 +0200713 if (!(accessRights & AccessSetDesktopSize)) return;
714 if (!rfb::Server::acceptSetDesktopSize) return;
715
Pierre Ossman04e62db2009-03-23 16:57:07 +0000716 // Don't bother the desktop with an invalid configuration
717 if (!layout.validate(fb_width, fb_height)) {
Pierre Ossman2daba9b2018-10-29 10:03:37 +0100718 writer()->writeDesktopSize(reasonClient, resultInvalid);
Pierre Ossman04e62db2009-03-23 16:57:07 +0000719 return;
720 }
721
722 // FIXME: the desktop will call back to VNCServerST and an extra set
723 // of ExtendedDesktopSize messages will be sent. This is okay
724 // protocol-wise, but unnecessary.
725 result = server->desktop->setScreenLayout(fb_width, fb_height, layout);
726
Pierre Ossman2daba9b2018-10-29 10:03:37 +0100727 writer()->writeDesktopSize(reasonClient, result);
Pierre Ossman04e62db2009-03-23 16:57:07 +0000728
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000729 // Only notify other clients on success
Pierre Ossman04e62db2009-03-23 16:57:07 +0000730 if (result == resultSuccess) {
731 if (server->screenLayout != layout)
732 throw Exception("Desktop configured a different screen layout than requested");
733 server->notifyScreenLayoutChange(this);
734 }
Pierre Ossmanc5e25602009-03-20 12:59:05 +0000735}
736
Pierre Ossman2c764942011-11-14 15:54:30 +0000737void VNCSConnectionST::fence(rdr::U32 flags, unsigned len, const char data[])
738{
Pierre Ossmanb2a417c2015-12-11 19:32:21 +0100739 rdr::U8 type;
740
Pierre Ossman2c764942011-11-14 15:54:30 +0000741 if (flags & fenceFlagRequest) {
742 if (flags & fenceFlagSyncNext) {
Pierre Ossmanb8b1e962012-07-20 10:47:00 +0000743 pendingSyncFence = true;
Pierre Ossman2c764942011-11-14 15:54:30 +0000744
745 fenceFlags = flags & (fenceFlagBlockBefore | fenceFlagBlockAfter | fenceFlagSyncNext);
746 fenceDataLen = len;
747 delete [] fenceData;
Michal Srbf3afa242017-03-27 19:02:15 +0300748 fenceData = NULL;
Pierre Ossman2c764942011-11-14 15:54:30 +0000749 if (len > 0) {
750 fenceData = new char[len];
751 memcpy(fenceData, data, len);
752 }
753
754 return;
755 }
756
757 // We handle everything synchronously so we trivially honor these modes
758 flags = flags & (fenceFlagBlockBefore | fenceFlagBlockAfter);
759
760 writer()->writeFence(flags, len, data);
761 return;
762 }
763
Pierre Ossmanb2a417c2015-12-11 19:32:21 +0100764 if (len < 1)
765 vlog.error("Fence response of unexpected size received");
Pierre Ossman1b478e52011-11-15 12:08:30 +0000766
Pierre Ossmanb2a417c2015-12-11 19:32:21 +0100767 type = data[0];
768
769 switch (type) {
Pierre Ossman2c764942011-11-14 15:54:30 +0000770 case 0:
771 // Initial dummy fence;
772 break;
Pierre Ossmanb2a417c2015-12-11 19:32:21 +0100773 case 1:
Pierre Ossmanc09e5582015-12-11 20:23:17 +0100774 congestion.gotPong();
Pierre Ossman1b478e52011-11-15 12:08:30 +0000775 break;
Pierre Ossman2c764942011-11-14 15:54:30 +0000776 default:
Pierre Ossmanb2a417c2015-12-11 19:32:21 +0100777 vlog.error("Fence response of unexpected type received");
Pierre Ossman2c764942011-11-14 15:54:30 +0000778 }
779}
780
Pierre Ossman1b478e52011-11-15 12:08:30 +0000781void VNCSConnectionST::enableContinuousUpdates(bool enable,
782 int x, int y, int w, int h)
783{
784 Rect rect;
785
Pierre Ossmanb114f5c2018-06-18 16:34:16 +0200786 if (!client.supportsFence() || !client.supportsContinuousUpdates())
Pierre Ossman1b478e52011-11-15 12:08:30 +0000787 throw Exception("Client tried to enable continuous updates when not allowed");
788
789 continuousUpdates = enable;
790
791 rect.setXYWH(x, y, w, h);
792 cuRegion.reset(rect);
793
794 if (enable) {
795 requested.clear();
Pierre Ossman1b478e52011-11-15 12:08:30 +0000796 } else {
797 writer()->writeEndOfContinuousUpdates();
798 }
799}
800
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000801// supportsLocalCursor() is called whenever the status of
Pierre Ossmanb114f5c2018-06-18 16:34:16 +0200802// client.supportsLocalCursor() has changed. If the client does now support local
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000803// cursor, we make sure that the old server-side rendered cursor is cleaned up
804// and the cursor is sent to the client.
805
806void VNCSConnectionST::supportsLocalCursor()
807{
Pierre Ossman387a4172017-11-16 16:44:36 +0100808 bool hasRenderedCursor = !damagedCursorRegion.is_empty();
809 if (hasRenderedCursor && !needRenderedCursor())
810 removeRenderedCursor = true;
811 setCursor();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000812}
813
Pierre Ossman2c764942011-11-14 15:54:30 +0000814void VNCSConnectionST::supportsFence()
815{
Pierre Ossmanb2a417c2015-12-11 19:32:21 +0100816 char type = 0;
817 writer()->writeFence(fenceFlagRequest, sizeof(type), &type);
Pierre Ossman2c764942011-11-14 15:54:30 +0000818}
819
Pierre Ossman1b478e52011-11-15 12:08:30 +0000820void VNCSConnectionST::supportsContinuousUpdates()
821{
822 // We refuse to use continuous updates if we cannot monitor the buffer
823 // usage using fences.
Pierre Ossmanb114f5c2018-06-18 16:34:16 +0200824 if (!client.supportsFence())
Pierre Ossman1b478e52011-11-15 12:08:30 +0000825 return;
826
827 writer()->writeEndOfContinuousUpdates();
828}
829
Pierre Ossmanb45a84f2016-12-12 16:59:15 +0100830void VNCSConnectionST::supportsLEDState()
831{
Pierre Ossman7b8bc432018-10-29 10:03:37 +0100832 if (client.ledState() == ledUnknown)
833 return;
834
Pierre Ossmanb45a84f2016-12-12 16:59:15 +0100835 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 Ossman2daba9b2018-10-29 10:03:37 +01001130 writer()->writeDesktopSize(reason);
Pierre Ossmana3ac01e2011-11-07 21:13:54 +00001131}
1132
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001133
1134// setCursor() is called whenever the cursor has changed shape or pixel format.
1135// If the client supports local cursor then it will arrange for the cursor to
1136// be sent to the client.
1137
1138void VNCSConnectionST::setCursor()
1139{
Pierre Ossmana3ac01e2011-11-07 21:13:54 +00001140 if (state() != RFBSTATE_NORMAL)
1141 return;
Pierre Ossmana3ac01e2011-11-07 21:13:54 +00001142
Pierre Ossman71ca8d52017-09-15 11:03:12 +02001143 // We need to blank out the client's cursor or there will be two
Pierre Ossman25db44a2017-11-16 16:40:44 +01001144 if (needRenderedCursor()) {
Pierre Ossman0d3ce872018-06-18 15:59:00 +02001145 client.setCursor(emptyCursor);
Pierre Ossman25db44a2017-11-16 16:40:44 +01001146 clientHasCursor = false;
1147 } else {
Pierre Ossman0d3ce872018-06-18 15:59:00 +02001148 client.setCursor(*server->cursor);
Pierre Ossman25db44a2017-11-16 16:40:44 +01001149 clientHasCursor = true;
1150 }
Pierre Ossman126e5642014-02-13 14:40:25 +01001151
Pierre Ossman2daba9b2018-10-29 10:03:37 +01001152 if (client.supportsLocalCursor())
1153 writer()->writeCursor();
Pierre Ossmana3ac01e2011-11-07 21:13:54 +00001154}
1155
1156void VNCSConnectionST::setDesktopName(const char *name)
1157{
Pierre Ossman0d3ce872018-06-18 15:59:00 +02001158 client.setName(name);
Pierre Ossmana3ac01e2011-11-07 21:13:54 +00001159
1160 if (state() != RFBSTATE_NORMAL)
1161 return;
1162
Pierre Ossman7b8bc432018-10-29 10:03:37 +01001163 if (client.supportsEncoding(pseudoEncodingDesktopName))
1164 writer()->writeSetDesktopName();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001165}
1166
Pierre Ossmanb45a84f2016-12-12 16:59:15 +01001167void VNCSConnectionST::setLEDState(unsigned int ledstate)
1168{
1169 if (state() != RFBSTATE_NORMAL)
1170 return;
1171
Pierre Ossman0d3ce872018-06-18 15:59:00 +02001172 client.setLEDState(ledstate);
Pierre Ossmanb45a84f2016-12-12 16:59:15 +01001173
Pierre Ossman7b8bc432018-10-29 10:03:37 +01001174 if (client.supportsLEDState())
1175 writer()->writeLEDState();
Pierre Ossmanb45a84f2016-12-12 16:59:15 +01001176}
1177
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001178void VNCSConnectionST::setSocketTimeouts()
1179{
1180 int timeoutms = rfb::Server::clientWaitTimeMillis;
1181 soonestTimeout(&timeoutms, secsToMillis(rfb::Server::idleTimeout));
1182 if (timeoutms == 0)
1183 timeoutms = -1;
1184 sock->inStream().setTimeout(timeoutms);
1185 sock->outStream().setTimeout(timeoutms);
1186}
1187
1188char* VNCSConnectionST::getStartTime()
1189{
1190 char* result = ctime(&startTime);
1191 result[24] = '\0';
1192 return result;
1193}
1194
1195void VNCSConnectionST::setStatus(int status)
1196{
1197 switch (status) {
1198 case 0:
1199 accessRights = accessRights | AccessPtrEvents | AccessKeyEvents | AccessView;
1200 break;
1201 case 1:
Pierre Ossman7728be22015-03-03 16:39:37 +01001202 accessRights = (accessRights & ~(AccessPtrEvents | AccessKeyEvents)) | AccessView;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001203 break;
1204 case 2:
Adam Tkac8e985062011-02-07 11:33:57 +00001205 accessRights = accessRights & ~(AccessPtrEvents | AccessKeyEvents | AccessView);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001206 break;
1207 }
1208 framebufferUpdateRequest(server->pb->getRect(), false);
1209}
1210int VNCSConnectionST::getStatus()
1211{
1212 if ((accessRights & (AccessPtrEvents | AccessKeyEvents | AccessView)) == 0x0007)
1213 return 0;
1214 if ((accessRights & (AccessPtrEvents | AccessKeyEvents | AccessView)) == 0x0001)
1215 return 1;
1216 if ((accessRights & (AccessPtrEvents | AccessKeyEvents | AccessView)) == 0x0000)
1217 return 2;
1218 return 4;
1219}
1220