blob: f4c6d07a4a52b86a7ca318edd1fe26231656ceab [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),
Peter Åstrand (astrand)7a368c92018-09-19 12:45:17 +020052 losslessTimer(this), server(server_), updates(false),
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;
195 if (cp.width && cp.height && (server->pb->width() != cp.width ||
196 server->pb->height() != cp.height))
197 {
198 // We need to clip the next update to the new size, but also add any
199 // extra bits if it's bigger. If we wanted to do this exactly, something
200 // like the code below would do it, but at the moment we just update the
Pierre Ossman671d2ef2015-06-09 16:09:06 +0200201 // entire new size. However, we do need to clip the damagedCursorRegion
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000202 // because that might be added to updates in writeFramebufferUpdate().
203
204 //updates.intersect(server->pb->getRect());
205 //
206 //if (server->pb->width() > cp.width)
207 // updates.add_changed(Rect(cp.width, 0, server->pb->width(),
208 // server->pb->height()));
209 //if (server->pb->height() > cp.height)
210 // updates.add_changed(Rect(0, cp.height, cp.width,
211 // server->pb->height()));
212
Pierre Ossman671d2ef2015-06-09 16:09:06 +0200213 damagedCursorRegion.assign_intersect(server->pb->getRect());
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000214
215 cp.width = server->pb->width();
216 cp.height = server->pb->height();
Pierre Ossman34e62f32009-03-20 21:46:12 +0000217 cp.screenLayout = 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.
350 return (cp.compressLevel == -1) || (cp.compressLevel > 1);
351}
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 Ossman324043e2017-08-16 16:26:11 +0200388 if (!cp.supportsLocalCursorWithAlpha &&
389 !cp.supportsLocalCursor && !cp.supportsLocalXCursor)
Pierre Ossman77ede0a2016-12-05 16:57:30 +0100390 return true;
391 if (!server->cursorPos.equals(pointerEventPos) &&
392 (time(0) - pointerEventTime) > 0)
393 return true;
394
395 return false;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000396}
397
398
399void VNCSConnectionST::approveConnectionOrClose(bool accept,
400 const char* reason)
401{
402 try {
403 approveConnection(accept, reason);
404 } catch (rdr::Exception& e) {
405 close(e.str());
406 }
407}
408
409
410
411// -=- Callbacks from SConnection
412
413void VNCSConnectionST::authSuccess()
414{
415 lastEventTime = time(0);
416
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000417 // - Set the connection parameters appropriately
418 cp.width = server->pb->width();
419 cp.height = server->pb->height();
Pierre Ossman34e62f32009-03-20 21:46:12 +0000420 cp.screenLayout = server->screenLayout;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000421 cp.setName(server->getName());
Pierre Ossmanb45a84f2016-12-12 16:59:15 +0100422 cp.setLEDState(server->ledState);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000423
424 // - Set the default pixel format
425 cp.setPF(server->pb->getPF());
426 char buffer[256];
427 cp.pf().print(buffer, 256);
428 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
Pierre Ossmaneef6c9a2018-10-05 17:11:25 +0200441 // - Prepare the desktop that we might be making calls
442 server->startDesktop();
443
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000444 // - Special case to provide a more useful error message
445 if (rfb::Server::neverShared && !rfb::Server::disconnectClients &&
446 server->authClientCount() > 0) {
447 approveConnection(false, "The server is already in use");
448 return;
449 }
450
451 // - Does the client have the right to bypass the query?
452 if (reverseConnection ||
453 !(rfb::Server::queryConnect || sock->requiresQuery()) ||
454 (accessRights & AccessNoQuery))
455 {
456 approveConnection(true);
457 return;
458 }
459
460 // - Get the server to display an Accept/Reject dialog, if required
Pierre Ossmane6aab242018-10-05 16:59:22 +0200461 server->queryConnection(sock, userName);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000462}
463
464void VNCSConnectionST::clientInit(bool shared)
465{
466 lastEventTime = time(0);
467 if (rfb::Server::alwaysShared || reverseConnection) shared = true;
Pierre Ossmane7be49b2014-12-02 14:33:17 +0100468 if (!(accessRights & AccessNonShared)) shared = true;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000469 if (rfb::Server::neverShared) shared = false;
470 if (!shared) {
Pierre Ossmane7be49b2014-12-02 14:33:17 +0100471 if (rfb::Server::disconnectClients && (accessRights & AccessNonShared)) {
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000472 // - Close all the other connected clients
473 vlog.debug("non-shared connection - closing clients");
474 server->closeClients("Non-shared connection requested", getSock());
475 } else {
476 // - Refuse this connection if there are existing clients, in addition to
477 // this one
478 if (server->authClientCount() > 1) {
479 close("Server is already in use");
480 return;
481 }
482 }
483 }
484 SConnection::clientInit(shared);
485}
486
487void VNCSConnectionST::setPixelFormat(const PixelFormat& pf)
488{
489 SConnection::setPixelFormat(pf);
490 char buffer[256];
491 pf.print(buffer, 256);
492 vlog.info("Client pixel format %s", buffer);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000493 setCursor();
494}
495
496void VNCSConnectionST::pointerEvent(const Point& pos, int buttonMask)
497{
498 pointerEventTime = lastEventTime = time(0);
499 server->lastUserInputTime = lastEventTime;
500 if (!(accessRights & AccessPtrEvents)) return;
501 if (!rfb::Server::acceptPointerEvents) return;
502 if (!server->pointerClient || server->pointerClient == this) {
503 pointerEventPos = pos;
504 if (buttonMask)
505 server->pointerClient = this;
506 else
507 server->pointerClient = 0;
508 server->desktop->pointerEvent(pointerEventPos, buttonMask);
509 }
510}
511
512
513class VNCSConnectionSTShiftPresser {
514public:
515 VNCSConnectionSTShiftPresser(SDesktop* desktop_)
516 : desktop(desktop_), pressed(false) {}
517 ~VNCSConnectionSTShiftPresser() {
Pierre Ossman9a153b02015-08-31 10:01:14 +0200518 if (pressed) {
519 vlog.debug("Releasing fake Shift_L");
Pierre Ossman5ae28212017-05-16 14:30:38 +0200520 desktop->keyEvent(XK_Shift_L, 0, false);
Pierre Ossman9a153b02015-08-31 10:01:14 +0200521 }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000522 }
523 void press() {
Pierre Ossman9a153b02015-08-31 10:01:14 +0200524 vlog.debug("Pressing fake Shift_L");
Pierre Ossman5ae28212017-05-16 14:30:38 +0200525 desktop->keyEvent(XK_Shift_L, 0, true);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000526 pressed = true;
527 }
528 SDesktop* desktop;
529 bool pressed;
530};
531
532// keyEvent() - record in the pressedKeys which keys were pressed. Allow
533// multiple down events (for autorepeat), but only allow a single up event.
Pierre Ossman5ae28212017-05-16 14:30:38 +0200534void VNCSConnectionST::keyEvent(rdr::U32 keysym, rdr::U32 keycode, bool down) {
Pierre Ossman16e1dcb2017-05-16 14:33:43 +0200535 rdr::U32 lookup;
536
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000537 lastEventTime = time(0);
538 server->lastUserInputTime = lastEventTime;
539 if (!(accessRights & AccessKeyEvents)) return;
540 if (!rfb::Server::acceptKeyEvents) return;
541
Pierre Ossman9a153b02015-08-31 10:01:14 +0200542 if (down)
Pierre Ossman5ae28212017-05-16 14:30:38 +0200543 vlog.debug("Key pressed: 0x%x / 0x%x", keysym, keycode);
Pierre Ossman9a153b02015-08-31 10:01:14 +0200544 else
Pierre Ossman5ae28212017-05-16 14:30:38 +0200545 vlog.debug("Key released: 0x%x / 0x%x", keysym, keycode);
Pierre Ossman9a153b02015-08-31 10:01:14 +0200546
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000547 // Remap the key if required
Pierre Ossman9a153b02015-08-31 10:01:14 +0200548 if (server->keyRemapper) {
549 rdr::U32 newkey;
Pierre Ossman5ae28212017-05-16 14:30:38 +0200550 newkey = server->keyRemapper->remapKey(keysym);
551 if (newkey != keysym) {
Pierre Ossman9a153b02015-08-31 10:01:14 +0200552 vlog.debug("Key remapped to 0x%x", newkey);
Pierre Ossman5ae28212017-05-16 14:30:38 +0200553 keysym = newkey;
Pierre Ossman9a153b02015-08-31 10:01:14 +0200554 }
555 }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000556
Pierre Ossmanbb305ca2016-12-11 12:41:26 +0100557 // Avoid lock keys if we don't know the server state
558 if ((server->ledState == ledUnknown) &&
Pierre Ossman5ae28212017-05-16 14:30:38 +0200559 ((keysym == XK_Caps_Lock) ||
560 (keysym == XK_Num_Lock) ||
561 (keysym == XK_Scroll_Lock))) {
Pierre Ossmanbb305ca2016-12-11 12:41:26 +0100562 vlog.debug("Ignoring lock key (e.g. caps lock)");
563 return;
564 }
Pierre Ossmanbb305ca2016-12-11 12:41:26 +0100565
Pierre Ossmanb45a84f2016-12-12 16:59:15 +0100566 // Lock key heuristics
567 // (only for clients that do not support the LED state extension)
568 if (!cp.supportsLEDState) {
569 // Always ignore ScrollLock as we don't have a heuristic
570 // for that
Pierre Ossman5ae28212017-05-16 14:30:38 +0200571 if (keysym == XK_Scroll_Lock) {
Pierre Ossmanb45a84f2016-12-12 16:59:15 +0100572 vlog.debug("Ignoring lock key (e.g. caps lock)");
573 return;
Pierre Ossmanbb305ca2016-12-11 12:41:26 +0100574 }
575
Pierre Ossmanb45a84f2016-12-12 16:59:15 +0100576 if (down && (server->ledState != ledUnknown)) {
577 // CapsLock synchronisation heuristic
578 // (this assumes standard interaction between CapsLock the Shift
579 // keys and normal characters)
Pierre Ossman5ae28212017-05-16 14:30:38 +0200580 if (((keysym >= XK_A) && (keysym <= XK_Z)) ||
581 ((keysym >= XK_a) && (keysym <= XK_z))) {
Pierre Ossmanb45a84f2016-12-12 16:59:15 +0100582 bool uppercase, shift, lock;
Pierre Ossmanbb305ca2016-12-11 12:41:26 +0100583
Pierre Ossman5ae28212017-05-16 14:30:38 +0200584 uppercase = (keysym >= XK_A) && (keysym <= XK_Z);
Pierre Ossman851e6802017-09-12 16:44:44 +0200585 shift = isShiftPressed();
Pierre Ossmanb45a84f2016-12-12 16:59:15 +0100586 lock = server->ledState & ledCapsLock;
Pierre Ossmanbb305ca2016-12-11 12:41:26 +0100587
Pierre Ossmanb45a84f2016-12-12 16:59:15 +0100588 if (lock == (uppercase == shift)) {
589 vlog.debug("Inserting fake CapsLock to get in sync with client");
Pierre Ossman5ae28212017-05-16 14:30:38 +0200590 server->desktop->keyEvent(XK_Caps_Lock, 0, true);
591 server->desktop->keyEvent(XK_Caps_Lock, 0, false);
Pierre Ossmanb45a84f2016-12-12 16:59:15 +0100592 }
593 }
594
595 // NumLock synchronisation heuristic
596 // (this is more cautious because of the differences between Unix,
597 // Windows and macOS)
Pierre Ossman5ae28212017-05-16 14:30:38 +0200598 if (((keysym >= XK_KP_Home) && (keysym <= XK_KP_Delete)) ||
599 ((keysym >= XK_KP_0) && (keysym <= XK_KP_9)) ||
600 (keysym == XK_KP_Separator) || (keysym == XK_KP_Decimal)) {
Pierre Ossmanb45a84f2016-12-12 16:59:15 +0100601 bool number, shift, lock;
602
Pierre Ossman5ae28212017-05-16 14:30:38 +0200603 number = ((keysym >= XK_KP_0) && (keysym <= XK_KP_9)) ||
604 (keysym == XK_KP_Separator) || (keysym == XK_KP_Decimal);
Pierre Ossman851e6802017-09-12 16:44:44 +0200605 shift = isShiftPressed();
Pierre Ossmanb45a84f2016-12-12 16:59:15 +0100606 lock = server->ledState & ledNumLock;
607
608 if (shift) {
609 // We don't know the appropriate NumLock state for when Shift
610 // is pressed as it could be one of:
611 //
612 // a) A Unix client where Shift negates NumLock
613 //
614 // b) A Windows client where Shift only cancels NumLock
615 //
616 // c) A macOS client where Shift doesn't have any effect
617 //
618 } else if (lock == (number == shift)) {
619 vlog.debug("Inserting fake NumLock to get in sync with client");
Pierre Ossman5ae28212017-05-16 14:30:38 +0200620 server->desktop->keyEvent(XK_Num_Lock, 0, true);
621 server->desktop->keyEvent(XK_Num_Lock, 0, false);
Pierre Ossmanb45a84f2016-12-12 16:59:15 +0100622 }
Pierre Ossmanbb305ca2016-12-11 12:41:26 +0100623 }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000624 }
625 }
626
627 // Turn ISO_Left_Tab into shifted Tab.
628 VNCSConnectionSTShiftPresser shiftPresser(server->desktop);
Pierre Ossman5ae28212017-05-16 14:30:38 +0200629 if (keysym == XK_ISO_Left_Tab) {
Pierre Ossman851e6802017-09-12 16:44:44 +0200630 if (!isShiftPressed())
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000631 shiftPresser.press();
Pierre Ossman5ae28212017-05-16 14:30:38 +0200632 keysym = XK_Tab;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000633 }
634
Pierre Ossman16e1dcb2017-05-16 14:33:43 +0200635 // We need to be able to track keys, so generate a fake index when we
636 // aren't given a keycode
637 if (keycode == 0)
638 lookup = 0x80000000 | keysym;
639 else
640 lookup = keycode;
641
642 // We force the same keysym for an already down key for the
643 // sake of sanity
644 if (pressedKeys.find(lookup) != pressedKeys.end())
645 keysym = pressedKeys[lookup];
646
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000647 if (down) {
Pierre Ossman16e1dcb2017-05-16 14:33:43 +0200648 pressedKeys[lookup] = keysym;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000649 } else {
Pierre Ossman16e1dcb2017-05-16 14:33:43 +0200650 if (!pressedKeys.erase(lookup))
Pierre Ossman5ae28212017-05-16 14:30:38 +0200651 return;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000652 }
Pierre Ossman16e1dcb2017-05-16 14:33:43 +0200653
Pierre Ossman5ae28212017-05-16 14:30:38 +0200654 server->desktop->keyEvent(keysym, keycode, down);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000655}
656
657void VNCSConnectionST::clientCutText(const char* str, int len)
658{
659 if (!(accessRights & AccessCutText)) return;
660 if (!rfb::Server::acceptCutText) return;
661 server->desktop->clientCutText(str, len);
662}
663
664void VNCSConnectionST::framebufferUpdateRequest(const Rect& r,bool incremental)
665{
Pierre Ossmane9962f72009-04-23 12:31:42 +0000666 Rect safeRect;
667
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000668 if (!(accessRights & AccessView)) return;
669
670 SConnection::framebufferUpdateRequest(r, incremental);
671
Pierre Ossmand9a59ba2009-03-20 15:55:37 +0000672 // Check that the client isn't sending crappy requests
673 if (!r.enclosed_by(Rect(0, 0, cp.width, cp.height))) {
674 vlog.error("FramebufferUpdateRequest %dx%d at %d,%d exceeds framebuffer %dx%d",
675 r.width(), r.height(), r.tl.x, r.tl.y, cp.width, cp.height);
Pierre Ossmane9962f72009-04-23 12:31:42 +0000676 safeRect = r.intersect(Rect(0, 0, cp.width, cp.height));
677 } else {
678 safeRect = r;
Pierre Ossmand9a59ba2009-03-20 15:55:37 +0000679 }
680
Constantin Kaplinsky2ef66952008-08-29 11:33:46 +0000681 // Just update the requested region.
682 // Framebuffer update will be sent a bit later, see processMessages().
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000683 Region reqRgn(r);
Pierre Ossman1b478e52011-11-15 12:08:30 +0000684 if (!incremental || !continuousUpdates)
685 requested.assign_union(reqRgn);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000686
687 if (!incremental) {
688 // Non-incremental update - treat as if area requested has changed
689 updates.add_changed(reqRgn);
Pierre Ossman53125a72009-04-22 15:37:51 +0000690
691 // And send the screen layout to the client (which, unlike the
692 // framebuffer dimensions, the client doesn't get during init)
Pierre Ossmanc5e25602009-03-20 12:59:05 +0000693 writer()->writeExtendedDesktopSize();
Pierre Ossman53125a72009-04-22 15:37:51 +0000694
695 // We do not send a DesktopSize since it only contains the
696 // framebuffer size (which the client already should know) and
697 // because some clients don't handle extra DesktopSize events
698 // very well.
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000699 }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000700}
701
Pierre Ossman34bb0612009-03-21 21:16:14 +0000702void VNCSConnectionST::setDesktopSize(int fb_width, int fb_height,
703 const ScreenSet& layout)
Pierre Ossmanc5e25602009-03-20 12:59:05 +0000704{
Pierre Ossman04e62db2009-03-23 16:57:07 +0000705 unsigned int result;
706
Michal Srbb318b8f2014-11-24 13:18:28 +0200707 if (!(accessRights & AccessSetDesktopSize)) return;
708 if (!rfb::Server::acceptSetDesktopSize) return;
709
Pierre Ossman04e62db2009-03-23 16:57:07 +0000710 // Don't bother the desktop with an invalid configuration
711 if (!layout.validate(fb_width, fb_height)) {
712 writer()->writeExtendedDesktopSize(reasonClient, resultInvalid,
713 fb_width, fb_height, layout);
Pierre Ossman04e62db2009-03-23 16:57:07 +0000714 return;
715 }
716
717 // FIXME: the desktop will call back to VNCServerST and an extra set
718 // of ExtendedDesktopSize messages will be sent. This is okay
719 // protocol-wise, but unnecessary.
720 result = server->desktop->setScreenLayout(fb_width, fb_height, layout);
721
Pierre Ossman04e62db2009-03-23 16:57:07 +0000722 writer()->writeExtendedDesktopSize(reasonClient, result,
723 fb_width, fb_height, layout);
Pierre Ossman04e62db2009-03-23 16:57:07 +0000724
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000725 // Only notify other clients on success
Pierre Ossman04e62db2009-03-23 16:57:07 +0000726 if (result == resultSuccess) {
727 if (server->screenLayout != layout)
728 throw Exception("Desktop configured a different screen layout than requested");
729 server->notifyScreenLayoutChange(this);
730 }
Pierre Ossmanc5e25602009-03-20 12:59:05 +0000731}
732
Pierre Ossman2c764942011-11-14 15:54:30 +0000733void VNCSConnectionST::fence(rdr::U32 flags, unsigned len, const char data[])
734{
Pierre Ossmanb2a417c2015-12-11 19:32:21 +0100735 rdr::U8 type;
736
Pierre Ossman2c764942011-11-14 15:54:30 +0000737 if (flags & fenceFlagRequest) {
738 if (flags & fenceFlagSyncNext) {
Pierre Ossmanb8b1e962012-07-20 10:47:00 +0000739 pendingSyncFence = true;
Pierre Ossman2c764942011-11-14 15:54:30 +0000740
741 fenceFlags = flags & (fenceFlagBlockBefore | fenceFlagBlockAfter | fenceFlagSyncNext);
742 fenceDataLen = len;
743 delete [] fenceData;
Michal Srbf3afa242017-03-27 19:02:15 +0300744 fenceData = NULL;
Pierre Ossman2c764942011-11-14 15:54:30 +0000745 if (len > 0) {
746 fenceData = new char[len];
747 memcpy(fenceData, data, len);
748 }
749
750 return;
751 }
752
753 // We handle everything synchronously so we trivially honor these modes
754 flags = flags & (fenceFlagBlockBefore | fenceFlagBlockAfter);
755
756 writer()->writeFence(flags, len, data);
757 return;
758 }
759
Pierre Ossmanb2a417c2015-12-11 19:32:21 +0100760 if (len < 1)
761 vlog.error("Fence response of unexpected size received");
Pierre Ossman1b478e52011-11-15 12:08:30 +0000762
Pierre Ossmanb2a417c2015-12-11 19:32:21 +0100763 type = data[0];
764
765 switch (type) {
Pierre Ossman2c764942011-11-14 15:54:30 +0000766 case 0:
767 // Initial dummy fence;
768 break;
Pierre Ossmanb2a417c2015-12-11 19:32:21 +0100769 case 1:
Pierre Ossmanc09e5582015-12-11 20:23:17 +0100770 congestion.gotPong();
Pierre Ossman1b478e52011-11-15 12:08:30 +0000771 break;
Pierre Ossman2c764942011-11-14 15:54:30 +0000772 default:
Pierre Ossmanb2a417c2015-12-11 19:32:21 +0100773 vlog.error("Fence response of unexpected type received");
Pierre Ossman2c764942011-11-14 15:54:30 +0000774 }
775}
776
Pierre Ossman1b478e52011-11-15 12:08:30 +0000777void VNCSConnectionST::enableContinuousUpdates(bool enable,
778 int x, int y, int w, int h)
779{
780 Rect rect;
781
782 if (!cp.supportsFence || !cp.supportsContinuousUpdates)
783 throw Exception("Client tried to enable continuous updates when not allowed");
784
785 continuousUpdates = enable;
786
787 rect.setXYWH(x, y, w, h);
788 cuRegion.reset(rect);
789
790 if (enable) {
791 requested.clear();
Pierre Ossman1b478e52011-11-15 12:08:30 +0000792 } else {
793 writer()->writeEndOfContinuousUpdates();
794 }
795}
796
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000797// supportsLocalCursor() is called whenever the status of
798// cp.supportsLocalCursor has changed. If the client does now support local
799// cursor, we make sure that the old server-side rendered cursor is cleaned up
800// and the cursor is sent to the client.
801
802void VNCSConnectionST::supportsLocalCursor()
803{
Pierre Ossman387a4172017-11-16 16:44:36 +0100804 bool hasRenderedCursor = !damagedCursorRegion.is_empty();
805 if (hasRenderedCursor && !needRenderedCursor())
806 removeRenderedCursor = true;
807 setCursor();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000808}
809
Pierre Ossman2c764942011-11-14 15:54:30 +0000810void VNCSConnectionST::supportsFence()
811{
Pierre Ossmanb2a417c2015-12-11 19:32:21 +0100812 char type = 0;
813 writer()->writeFence(fenceFlagRequest, sizeof(type), &type);
Pierre Ossman2c764942011-11-14 15:54:30 +0000814}
815
Pierre Ossman1b478e52011-11-15 12:08:30 +0000816void VNCSConnectionST::supportsContinuousUpdates()
817{
818 // We refuse to use continuous updates if we cannot monitor the buffer
819 // usage using fences.
820 if (!cp.supportsFence)
821 return;
822
823 writer()->writeEndOfContinuousUpdates();
824}
825
Pierre Ossmanb45a84f2016-12-12 16:59:15 +0100826void VNCSConnectionST::supportsLEDState()
827{
828 writer()->writeLEDState();
829}
830
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000831
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +0000832bool VNCSConnectionST::handleTimeout(Timer* t)
833{
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000834 try {
Peter Åstrand (astrand)7a368c92018-09-19 12:45:17 +0200835 if ((t == &congestionTimer) ||
836 (t == &losslessTimer))
Pierre Ossmana99d14d2015-12-13 15:43:46 +0100837 writeFramebufferUpdate();
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000838 } catch (rdr::Exception& e) {
839 close(e.str());
840 }
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +0000841
842 return false;
843}
844
Pierre Ossman851e6802017-09-12 16:44:44 +0200845bool VNCSConnectionST::isShiftPressed()
846{
847 std::map<rdr::U32, rdr::U32>::const_iterator iter;
848
849 for (iter = pressedKeys.begin(); iter != pressedKeys.end(); ++iter) {
850 if (iter->second == XK_Shift_L)
851 return true;
852 if (iter->second == XK_Shift_R)
853 return true;
854 }
855
856 return false;
857}
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +0000858
Pierre Ossman1b478e52011-11-15 12:08:30 +0000859void VNCSConnectionST::writeRTTPing()
860{
Pierre Ossmanb2a417c2015-12-11 19:32:21 +0100861 char type;
Pierre Ossman1b478e52011-11-15 12:08:30 +0000862
863 if (!cp.supportsFence)
864 return;
865
Pierre Ossmana99d14d2015-12-13 15:43:46 +0100866 congestion.updatePosition(sock->outStream().length());
Pierre Ossman1b478e52011-11-15 12:08:30 +0000867
868 // We need to make sure any old update are already processed by the
869 // time we get the response back. This allows us to reliably throttle
870 // back on client overload, as well as network overload.
Pierre Ossmanb2a417c2015-12-11 19:32:21 +0100871 type = 1;
Pierre Ossman1b478e52011-11-15 12:08:30 +0000872 writer()->writeFence(fenceFlagRequest | fenceFlagBlockBefore,
Pierre Ossmanb2a417c2015-12-11 19:32:21 +0100873 sizeof(type), &type);
Pierre Ossman1b478e52011-11-15 12:08:30 +0000874
Pierre Ossmana99d14d2015-12-13 15:43:46 +0100875 congestion.sentPing();
Pierre Ossman1b478e52011-11-15 12:08:30 +0000876}
877
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +0000878bool VNCSConnectionST::isCongested()
879{
Pierre Ossmance261812018-07-17 15:01:53 +0200880 int eta;
Pierre Ossmana99d14d2015-12-13 15:43:46 +0100881
882 congestionTimer.stop();
Pierre Ossman1b478e52011-11-15 12:08:30 +0000883
884 // Stuff still waiting in the send buffer?
Pierre Ossman352d0622016-04-29 15:50:54 +0200885 sock->outStream().flush();
Pierre Ossman8cf71632016-03-11 09:38:08 +0100886 congestion.debugTrace("congestion-trace.csv", sock->getFd());
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +0000887 if (sock->outStream().bufferUsage() > 0)
888 return true;
889
Pierre Ossman1b478e52011-11-15 12:08:30 +0000890 if (!cp.supportsFence)
891 return false;
892
Pierre Ossmana99d14d2015-12-13 15:43:46 +0100893 congestion.updatePosition(sock->outStream().length());
894 if (!congestion.isCongested())
Pierre Ossman1b478e52011-11-15 12:08:30 +0000895 return false;
896
Pierre Ossmana99d14d2015-12-13 15:43:46 +0100897 eta = congestion.getUncongestedETA();
898 if (eta >= 0)
899 congestionTimer.start(eta);
Pierre Ossman1b478e52011-11-15 12:08:30 +0000900
901 return true;
902}
903
904
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000905void VNCSConnectionST::writeFramebufferUpdate()
906{
Pierre Ossmana99d14d2015-12-13 15:43:46 +0100907 congestion.updatePosition(sock->outStream().length());
908
Pierre Ossman2c764942011-11-14 15:54:30 +0000909 // We're in the middle of processing a command that's supposed to be
910 // synchronised. Allowing an update to slip out right now might violate
911 // that synchronisation.
912 if (syncFence)
913 return;
914
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000915 // We try to aggregate responses, so don't send out anything whilst we
916 // still have incoming messages. processMessages() will give us another
917 // chance to run once things are idle.
918 if (inProcessMessages)
919 return;
920
Pierre Ossman1b478e52011-11-15 12:08:30 +0000921 if (state() != RFBSTATE_NORMAL)
922 return;
923 if (requested.is_empty() && !continuousUpdates)
Pierre Ossmane9962f72009-04-23 12:31:42 +0000924 return;
Pierre Ossmand9a59ba2009-03-20 15:55:37 +0000925
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +0000926 // Check that we actually have some space on the link and retry in a
927 // bit if things are congested.
Pierre Ossmana40ab202016-04-29 15:35:56 +0200928 if (isCongested())
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +0000929 return;
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +0000930
Pierre Ossmancef3cf72016-11-25 10:06:34 +0100931 // Updates often consists of many small writes, and in continuous
932 // mode, we will also have small fence messages around the update. We
933 // need to aggregate these in order to not clog up TCP's congestion
934 // window.
Peter Åstrand (astrand)01dc1a62017-10-10 12:56:04 +0200935 sock->cork(true);
Pierre Ossman36dadf82011-11-15 12:11:32 +0000936
Pierre Ossmane9962f72009-04-23 12:31:42 +0000937 // First take care of any updates that cannot contain framebuffer data
938 // changes.
Pierre Ossmancef3cf72016-11-25 10:06:34 +0100939 writeNoDataUpdate();
940
941 // Then real data (if possible)
942 writeDataUpdate();
943
Peter Åstrand (astrand)01dc1a62017-10-10 12:56:04 +0200944 sock->cork(false);
Pierre Ossmana99d14d2015-12-13 15:43:46 +0100945
946 congestion.updatePosition(sock->outStream().length());
Pierre Ossmancef3cf72016-11-25 10:06:34 +0100947}
948
949void VNCSConnectionST::writeNoDataUpdate()
950{
951 if (!writer()->needNoDataUpdate())
952 return;
953
954 writer()->writeNoDataUpdate();
955
956 // Make sure no data update is sent until next request
957 requested.clear();
958}
959
960void VNCSConnectionST::writeDataUpdate()
961{
Pierre Ossman8efc7b42018-03-23 11:45:51 +0100962 Region req, pending;
Pierre Ossmancef3cf72016-11-25 10:06:34 +0100963 UpdateInfo ui;
964 bool needNewUpdateInfo;
Pierre Ossman24684e52016-12-05 16:58:19 +0100965 const RenderedCursor *cursor;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000966
Constantin Kaplinsky604d7812007-08-31 15:50:37 +0000967 updates.enable_copyrect(cp.useCopyRect);
968
Pierre Ossmancef3cf72016-11-25 10:06:34 +0100969 // See what the client has requested (if anything)
Pierre Ossman1b478e52011-11-15 12:08:30 +0000970 if (continuousUpdates)
971 req = cuRegion.union_(requested);
972 else
973 req = requested;
974
Pierre Ossmancef3cf72016-11-25 10:06:34 +0100975 if (req.is_empty())
976 return;
977
Pierre Ossman8efc7b42018-03-23 11:45:51 +0100978 // Get any framebuffer changes we haven't yet been informed of
979 pending = server->getPendingRegion();
980
Pierre Ossmancef3cf72016-11-25 10:06:34 +0100981 // Get the lists of updates. Prior to exporting the data to the `ui' object,
982 // getUpdateInfo() will normalize the `updates' object such way that its
983 // `changed' and `copied' regions would not intersect.
Pierre Ossman1b478e52011-11-15 12:08:30 +0000984 updates.getUpdateInfo(&ui, req);
985 needNewUpdateInfo = false;
Constantin Kaplinsky604d7812007-08-31 15:50:37 +0000986
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000987 // If the previous position of the rendered cursor overlaps the source of the
988 // copy, then when the copy happens the corresponding rectangle in the
989 // destination will be wrong, so add it to the changed region.
990
Pierre Ossman671d2ef2015-06-09 16:09:06 +0200991 if (!ui.copied.is_empty() && !damagedCursorRegion.is_empty()) {
992 Region bogusCopiedCursor;
993
Pierre Ossman74385d32018-03-22 16:00:18 +0100994 bogusCopiedCursor = damagedCursorRegion;
Pierre Ossman671d2ef2015-06-09 16:09:06 +0200995 bogusCopiedCursor.translate(ui.copy_delta);
996 bogusCopiedCursor.assign_intersect(server->pb->getRect());
Constantin Kaplinsky45517c82007-08-31 15:56:33 +0000997 if (!ui.copied.intersect(bogusCopiedCursor).is_empty()) {
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000998 updates.add_changed(bogusCopiedCursor);
Constantin Kaplinsky604d7812007-08-31 15:50:37 +0000999 needNewUpdateInfo = true;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001000 }
1001 }
1002
Pierre Ossman671d2ef2015-06-09 16:09:06 +02001003 // If we need to remove the old rendered cursor, just add the region to
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001004 // the changed region.
1005
1006 if (removeRenderedCursor) {
Pierre Ossman671d2ef2015-06-09 16:09:06 +02001007 updates.add_changed(damagedCursorRegion);
Constantin Kaplinsky604d7812007-08-31 15:50:37 +00001008 needNewUpdateInfo = true;
Pierre Ossman671d2ef2015-06-09 16:09:06 +02001009 damagedCursorRegion.clear();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001010 removeRenderedCursor = false;
1011 }
1012
Pierre Ossman8c3bd692018-03-22 15:58:54 +01001013 // If we need a full cursor update then make sure its entire region
1014 // is marked as changed.
1015
1016 if (updateRenderedCursor) {
1017 updates.add_changed(server->getRenderedCursor()->getEffectiveRect());
1018 needNewUpdateInfo = true;
1019 updateRenderedCursor = false;
1020 }
1021
Constantin Kaplinsky604d7812007-08-31 15:50:37 +00001022 // The `updates' object could change, make sure we have valid update info.
1023
1024 if (needNewUpdateInfo)
Pierre Ossman1b478e52011-11-15 12:08:30 +00001025 updates.getUpdateInfo(&ui, req);
Constantin Kaplinsky604d7812007-08-31 15:50:37 +00001026
Pierre Ossman8efc7b42018-03-23 11:45:51 +01001027 // If there are queued updates then we cannot safely send an update
1028 // without risking a partially updated screen
1029
1030 if (!pending.is_empty()) {
1031 // However we might still be able to send a lossless refresh
1032 req.assign_subtract(pending);
1033 req.assign_subtract(ui.changed);
1034 req.assign_subtract(ui.copied);
1035
1036 ui.changed.clear();
1037 ui.copied.clear();
1038 }
1039
Pierre Ossman8c3bd692018-03-22 15:58:54 +01001040 // Does the client need a server-side rendered cursor?
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001041
Pierre Ossman24684e52016-12-05 16:58:19 +01001042 cursor = NULL;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001043 if (needRenderedCursor()) {
Pierre Ossman671d2ef2015-06-09 16:09:06 +02001044 Rect renderedCursorRect;
1045
Pierre Ossman24684e52016-12-05 16:58:19 +01001046 cursor = server->getRenderedCursor();
Pierre Ossman8c3bd692018-03-22 15:58:54 +01001047 renderedCursorRect = cursor->getEffectiveRect();
Pierre Ossman24684e52016-12-05 16:58:19 +01001048
Pierre Ossman8c3bd692018-03-22 15:58:54 +01001049 // Check that we don't try to copy over the cursor area, and
1050 // if that happens we need to treat it as changed so that we can
1051 // re-render it
1052 if (!ui.copied.intersect(renderedCursorRect).is_empty()) {
1053 ui.changed.assign_union(ui.copied.intersect(renderedCursorRect));
1054 ui.copied.assign_subtract(renderedCursorRect);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001055 }
1056
Pierre Ossman8c3bd692018-03-22 15:58:54 +01001057 // Track where we've rendered the cursor
1058 damagedCursorRegion.assign_union(ui.changed.intersect(renderedCursorRect));
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001059 }
1060
Pierre Ossman8efc7b42018-03-23 11:45:51 +01001061 // Return if there is nothing to send the client.
Peter Åstrand (astrand)7a368c92018-09-19 12:45:17 +02001062 if (ui.is_empty() && !writer()->needFakeUpdate()) {
1063 int eta;
Pierre Ossman8efc7b42018-03-23 11:45:51 +01001064
Peter Åstrand (astrand)7a368c92018-09-19 12:45:17 +02001065 // Any lossless refresh that needs handling?
1066 if (!encodeManager.needsLosslessRefresh(req))
1067 return;
1068
1069 // Now? Or later?
1070 eta = encodeManager.getNextLosslessRefresh(req);
1071 if (eta > 0) {
1072 losslessTimer.start(eta);
1073 return;
1074 }
1075 }
Pierre Ossmanfdba3fe2014-01-31 13:12:18 +01001076
Pierre Ossmancef3cf72016-11-25 10:06:34 +01001077 writeRTTPing();
Pierre Ossman1b478e52011-11-15 12:08:30 +00001078
Pierre Ossman6b2f1132016-11-30 08:03:35 +01001079 if (!ui.is_empty())
1080 encodeManager.writeUpdate(ui, server->getPixelBuffer(), cursor);
Pierre Ossmana2b80d62018-03-23 09:30:09 +01001081 else {
Pierre Ossmanc3ce5ce2018-09-19 16:31:18 +02001082 int nextUpdate;
Pierre Ossmana2b80d62018-03-23 09:30:09 +01001083
1084 // FIXME: If continuous updates aren't used then the client might
1085 // be slower than frameRate in its requests and we could
1086 // afford a larger update size
Pierre Ossmanc3ce5ce2018-09-19 16:31:18 +02001087 nextUpdate = server->msToNextUpdate();
1088 if (nextUpdate > 0) {
Pierre Ossmancb5a3992018-09-19 16:35:40 +02001089 size_t bandwidth, maxUpdateSize;
Pierre Ossmana2b80d62018-03-23 09:30:09 +01001090
Pierre Ossmanc3ce5ce2018-09-19 16:31:18 +02001091 // FIXME: Bandwidth estimation without congestion control
Pierre Ossmancb5a3992018-09-19 16:35:40 +02001092 bandwidth = congestion.getBandwidth();
Pierre Ossmana2b80d62018-03-23 09:30:09 +01001093
Pierre Ossmancb5a3992018-09-19 16:35:40 +02001094 // FIXME: Hard coded value for maximum CPU throughput
1095 if (bandwidth > 5000000)
1096 bandwidth = 5000000;
1097
1098 maxUpdateSize = bandwidth * nextUpdate / 1000;
Pierre Ossmanc3ce5ce2018-09-19 16:31:18 +02001099 encodeManager.writeLosslessRefresh(req, server->getPixelBuffer(),
1100 cursor, maxUpdateSize);
1101 }
Pierre Ossmana2b80d62018-03-23 09:30:09 +01001102 }
Pierre Ossman1b478e52011-11-15 12:08:30 +00001103
Pierre Ossmancef3cf72016-11-25 10:06:34 +01001104 writeRTTPing();
Pierre Ossmanc0397262014-03-14 15:59:46 +01001105
Pierre Ossmancef3cf72016-11-25 10:06:34 +01001106 // The request might be for just part of the screen, so we cannot
1107 // just clear the entire update tracker.
1108 updates.subtract(req);
Pierre Ossmanc0397262014-03-14 15:59:46 +01001109
Pierre Ossmancef3cf72016-11-25 10:06:34 +01001110 requested.clear();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001111}
1112
1113
Pierre Ossmana3ac01e2011-11-07 21:13:54 +00001114void VNCSConnectionST::screenLayoutChange(rdr::U16 reason)
1115{
1116 if (!authenticated())
1117 return;
1118
1119 cp.screenLayout = server->screenLayout;
1120
1121 if (state() != RFBSTATE_NORMAL)
1122 return;
1123
1124 writer()->writeExtendedDesktopSize(reason, 0, cp.width, cp.height,
1125 cp.screenLayout);
Pierre Ossmana3ac01e2011-11-07 21:13:54 +00001126}
1127
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001128
1129// setCursor() is called whenever the cursor has changed shape or pixel format.
1130// If the client supports local cursor then it will arrange for the cursor to
1131// be sent to the client.
1132
1133void VNCSConnectionST::setCursor()
1134{
Pierre Ossmana3ac01e2011-11-07 21:13:54 +00001135 if (state() != RFBSTATE_NORMAL)
1136 return;
Pierre Ossmana3ac01e2011-11-07 21:13:54 +00001137
Pierre Ossman71ca8d52017-09-15 11:03:12 +02001138 // We need to blank out the client's cursor or there will be two
Pierre Ossman25db44a2017-11-16 16:40:44 +01001139 if (needRenderedCursor()) {
Pierre Ossman71ca8d52017-09-15 11:03:12 +02001140 cp.setCursor(emptyCursor);
Pierre Ossman25db44a2017-11-16 16:40:44 +01001141 clientHasCursor = false;
1142 } else {
Pierre Ossman71ca8d52017-09-15 11:03:12 +02001143 cp.setCursor(*server->cursor);
Pierre Ossman25db44a2017-11-16 16:40:44 +01001144 clientHasCursor = true;
1145 }
Pierre Ossman126e5642014-02-13 14:40:25 +01001146
Pierre Ossman8053c8e2017-02-21 12:59:04 +01001147 if (!writer()->writeSetCursorWithAlpha()) {
1148 if (!writer()->writeSetCursor()) {
1149 if (!writer()->writeSetXCursor()) {
1150 // No client support
1151 return;
1152 }
Pierre Ossman126e5642014-02-13 14:40:25 +01001153 }
1154 }
Pierre Ossmana3ac01e2011-11-07 21:13:54 +00001155}
1156
1157void VNCSConnectionST::setDesktopName(const char *name)
1158{
1159 cp.setName(name);
1160
1161 if (state() != RFBSTATE_NORMAL)
1162 return;
1163
1164 if (!writer()->writeSetDesktopName()) {
1165 fprintf(stderr, "Client does not support desktop rename\n");
1166 return;
1167 }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001168}
1169
Pierre Ossmanb45a84f2016-12-12 16:59:15 +01001170void VNCSConnectionST::setLEDState(unsigned int ledstate)
1171{
1172 if (state() != RFBSTATE_NORMAL)
1173 return;
1174
1175 cp.setLEDState(ledstate);
1176
Pierre Ossmanb218ecd2017-11-16 16:43:13 +01001177 writer()->writeLEDState();
Pierre Ossmanb45a84f2016-12-12 16:59:15 +01001178}
1179
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001180void VNCSConnectionST::setSocketTimeouts()
1181{
1182 int timeoutms = rfb::Server::clientWaitTimeMillis;
1183 soonestTimeout(&timeoutms, secsToMillis(rfb::Server::idleTimeout));
1184 if (timeoutms == 0)
1185 timeoutms = -1;
1186 sock->inStream().setTimeout(timeoutms);
1187 sock->outStream().setTimeout(timeoutms);
1188}
1189
1190char* VNCSConnectionST::getStartTime()
1191{
1192 char* result = ctime(&startTime);
1193 result[24] = '\0';
1194 return result;
1195}
1196
1197void VNCSConnectionST::setStatus(int status)
1198{
1199 switch (status) {
1200 case 0:
1201 accessRights = accessRights | AccessPtrEvents | AccessKeyEvents | AccessView;
1202 break;
1203 case 1:
Pierre Ossman7728be22015-03-03 16:39:37 +01001204 accessRights = (accessRights & ~(AccessPtrEvents | AccessKeyEvents)) | AccessView;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001205 break;
1206 case 2:
Adam Tkac8e985062011-02-07 11:33:57 +00001207 accessRights = accessRights & ~(AccessPtrEvents | AccessKeyEvents | AccessView);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001208 break;
1209 }
1210 framebufferUpdateRequest(server->pb->getRect(), false);
1211}
1212int VNCSConnectionST::getStatus()
1213{
1214 if ((accessRights & (AccessPtrEvents | AccessKeyEvents | AccessView)) == 0x0007)
1215 return 0;
1216 if ((accessRights & (AccessPtrEvents | AccessKeyEvents | AccessView)) == 0x0001)
1217 return 1;
1218 if ((accessRights & (AccessPtrEvents | AccessKeyEvents | AccessView)) == 0x0000)
1219 return 2;
1220 return 4;
1221}
1222