blob: f22b993a84e2cfaa0bb8bc5fed00fc889e8401c2 [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
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00003 *
4 * This is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This software is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this software; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
17 * USA.
18 */
19
Pierre Ossmana830bec2011-11-08 12:12:02 +000020#include <network/TcpSocket.h>
Pierre Ossman707fa122015-12-11 20:21:20 +010021
22#include <rfb/ComparingUpdateTracker.h>
23#include <rfb/Encoder.h>
24#include <rfb/KeyRemapper.h>
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000025#include <rfb/LogWriter.h>
Adam Tkac5a0caed2010-04-23 13:58:10 +000026#include <rfb/Security.h>
Pierre Ossman707fa122015-12-11 20:21:20 +010027#include <rfb/ServerCore.h>
28#include <rfb/SMsgWriter.h>
29#include <rfb/VNCServerST.h>
30#include <rfb/VNCSConnectionST.h>
Pierre Ossmanc5e25602009-03-20 12:59:05 +000031#include <rfb/screenTypes.h>
Pierre Ossman2c764942011-11-14 15:54:30 +000032#include <rfb/fenceTypes.h>
Pierre Ossmanbb305ca2016-12-11 12:41:26 +010033#include <rfb/ledStates.h>
Pierre Ossmanbb305ca2016-12-11 12:41:26 +010034#define XK_LATIN1
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000035#define XK_MISCELLANY
36#define XK_XKB_KEYS
37#include <rfb/keysymdef.h>
38
39using namespace rfb;
40
41static LogWriter vlog("VNCSConnST");
42
Pierre Ossman71ca8d52017-09-15 11:03:12 +020043static Cursor emptyCursor(0, 0, Point(0, 0), NULL);
44
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000045VNCSConnectionST::VNCSConnectionST(VNCServerST* server_, network::Socket *s,
46 bool reverse)
Pierre Ossman7069bdd2015-02-06 14:41:58 +010047 : sock(s), reverseConnection(reverse),
Pierre Ossman36304752017-10-04 16:21:57 +020048 inProcessMessages(false),
Pierre Ossmanb8b1e962012-07-20 10:47:00 +000049 pendingSyncFence(false), syncFence(false), fenceFlags(0),
Pierre Ossmana99d14d2015-12-13 15:43:46 +010050 fenceDataLen(0), fenceData(NULL), congestionTimer(this),
Pierre Ossman0c9bd4b2014-07-09 16:44:11 +020051 server(server_), updates(false),
Pierre Ossman671d2ef2015-06-09 16:09:06 +020052 updateRenderedCursor(false), removeRenderedCursor(false),
Pierre Ossmana40ab202016-04-29 15:35:56 +020053 continuousUpdates(false), encodeManager(this), pointerEventTime(0),
Pierre Ossman25db44a2017-11-16 16:40:44 +010054 clientHasCursor(false),
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +000055 accessRights(AccessDefault), startTime(time(0))
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000056{
57 setStreams(&sock->inStream(), &sock->outStream());
58 peerEndpoint.buf = sock->getPeerEndpoint();
59 VNCServerST::connectionsLog.write(1,"accepted: %s", peerEndpoint.buf);
60
61 // Configure the socket
62 setSocketTimeouts();
63 lastEventTime = time(0);
64
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000065 server->clients.push_front(this);
66}
67
68
69VNCSConnectionST::~VNCSConnectionST()
70{
71 // If we reach here then VNCServerST is deleting us!
72 VNCServerST::connectionsLog.write(1,"closed: %s (%s)",
73 peerEndpoint.buf,
74 (closeReason.buf) ? closeReason.buf : "");
75
76 // Release any keys the client still had pressed
Pierre Ossman16e1dcb2017-05-16 14:33:43 +020077 while (!pressedKeys.empty()) {
78 rdr::U32 keysym, keycode;
79
80 keysym = pressedKeys.begin()->second;
81 keycode = pressedKeys.begin()->first;
82 pressedKeys.erase(pressedKeys.begin());
83
84 vlog.debug("Releasing key 0x%x / 0x%x on client disconnect",
85 keysym, keycode);
86 server->desktop->keyEvent(keysym, keycode, false);
Pierre Ossman9a153b02015-08-31 10:01:14 +020087 }
Pierre Ossman16e1dcb2017-05-16 14:33:43 +020088
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000089 if (server->pointerClient == this)
90 server->pointerClient = 0;
91
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000092 // Remove this client from the server
93 server->clients.remove(this);
94
Pierre Ossman2c764942011-11-14 15:54:30 +000095 delete [] fenceData;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000096}
97
98
99// Methods called from VNCServerST
100
101bool VNCSConnectionST::init()
102{
103 try {
104 initialiseProtocol();
105 } catch (rdr::Exception& e) {
106 close(e.str());
107 return false;
108 }
109 return true;
110}
111
112void VNCSConnectionST::close(const char* reason)
113{
114 // Log the reason for the close
115 if (!closeReason.buf)
Adam Tkacd36b6262009-09-04 10:57:20 +0000116 closeReason.buf = strDup(reason);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000117 else
118 vlog.debug("second close: %s (%s)", peerEndpoint.buf, reason);
119
120 if (authenticated()) {
121 server->lastDisconnectTime = time(0);
122 }
123
124 // Just shutdown the socket and mark our state as closing. Eventually the
125 // calling code will call VNCServerST's removeSocket() method causing us to
126 // be deleted.
127 sock->shutdown();
128 setState(RFBSTATE_CLOSING);
129}
130
131
132void VNCSConnectionST::processMessages()
133{
134 if (state() == RFBSTATE_CLOSING) return;
135 try {
136 // - Now set appropriate socket timeouts and process data
137 setSocketTimeouts();
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000138
139 inProcessMessages = true;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000140
Pierre Ossmana830bec2011-11-08 12:12:02 +0000141 // Get the underlying TCP layer to build large packets if we send
142 // multiple small responses.
Peter Ã…strand (astrand)01dc1a62017-10-10 12:56:04 +0200143 sock->cork(true);
Pierre Ossmana830bec2011-11-08 12:12:02 +0000144
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000145 while (getInStream()->checkNoWait(1)) {
Pierre Ossmanb8b1e962012-07-20 10:47:00 +0000146 if (pendingSyncFence) {
147 syncFence = true;
148 pendingSyncFence = false;
149 }
150
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000151 processMsg();
Pierre Ossmanb8b1e962012-07-20 10:47:00 +0000152
Pierre Ossman2c764942011-11-14 15:54:30 +0000153 if (syncFence) {
154 writer()->writeFence(fenceFlags, fenceDataLen, fenceData);
155 syncFence = false;
156 }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000157 }
158
Pierre Ossmana830bec2011-11-08 12:12:02 +0000159 // Flush out everything in case we go idle after this.
Peter Ã…strand (astrand)01dc1a62017-10-10 12:56:04 +0200160 sock->cork(false);
Pierre Ossmana830bec2011-11-08 12:12:02 +0000161
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000162 inProcessMessages = false;
Constantin Kaplinsky2ef66952008-08-29 11:33:46 +0000163
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000164 // If there were anything requiring an update, try to send it here.
165 // We wait until now with this to aggregate responses and to give
166 // higher priority to user actions such as keyboard and pointer events.
167 writeFramebufferUpdate();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000168 } catch (rdr::EndOfStream&) {
169 close("Clean disconnection");
170 } catch (rdr::Exception &e) {
171 close(e.str());
172 }
173}
174
Pierre Ossmand408ca52016-04-29 14:26:05 +0200175void VNCSConnectionST::flushSocket()
176{
177 if (state() == RFBSTATE_CLOSING) return;
178 try {
179 setSocketTimeouts();
180 sock->outStream().flush();
Pierre Ossmana40ab202016-04-29 15:35:56 +0200181 // Flushing the socket might release an update that was previously
182 // delayed because of congestion.
183 if (sock->outStream().bufferUsage() == 0)
184 writeFramebufferUpdate();
Pierre Ossmand408ca52016-04-29 14:26:05 +0200185 } catch (rdr::Exception &e) {
186 close(e.str());
187 }
188}
189
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000190void VNCSConnectionST::pixelBufferChange()
191{
192 try {
193 if (!authenticated()) return;
194 if (cp.width && cp.height && (server->pb->width() != cp.width ||
195 server->pb->height() != cp.height))
196 {
197 // We need to clip the next update to the new size, but also add any
198 // extra bits if it's bigger. If we wanted to do this exactly, something
199 // like the code below would do it, but at the moment we just update the
Pierre Ossman671d2ef2015-06-09 16:09:06 +0200200 // entire new size. However, we do need to clip the damagedCursorRegion
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000201 // because that might be added to updates in writeFramebufferUpdate().
202
203 //updates.intersect(server->pb->getRect());
204 //
205 //if (server->pb->width() > cp.width)
206 // updates.add_changed(Rect(cp.width, 0, server->pb->width(),
207 // server->pb->height()));
208 //if (server->pb->height() > cp.height)
209 // updates.add_changed(Rect(0, cp.height, cp.width,
210 // server->pb->height()));
211
Pierre Ossman671d2ef2015-06-09 16:09:06 +0200212 damagedCursorRegion.assign_intersect(server->pb->getRect());
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000213
214 cp.width = server->pb->width();
215 cp.height = server->pb->height();
Pierre Ossman34e62f32009-03-20 21:46:12 +0000216 cp.screenLayout = server->screenLayout;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000217 if (state() == RFBSTATE_NORMAL) {
Pierre Ossman2ee430a2009-05-28 12:54:24 +0000218 // We should only send EDS to client asking for both
219 if (!writer()->writeExtendedDesktopSize()) {
220 if (!writer()->writeSetDesktopSize()) {
221 close("Client does not support desktop resize");
222 return;
223 }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000224 }
225 }
Pierre Ossman6b2f1132016-11-30 08:03:35 +0100226
227 // Drop any lossy tracking that is now outside the framebuffer
228 encodeManager.pruneLosslessRefresh(Region(server->pb->getRect()));
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000229 }
230 // Just update the whole screen at the moment because we're too lazy to
231 // work out what's actually changed.
232 updates.clear();
233 updates.add_changed(server->pb->getRect());
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000234 writeFramebufferUpdate();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000235 } catch(rdr::Exception &e) {
236 close(e.str());
237 }
238}
239
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000240void VNCSConnectionST::writeFramebufferUpdateOrClose()
Pierre Ossman04e62db2009-03-23 16:57:07 +0000241{
242 try {
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000243 writeFramebufferUpdate();
244 } catch(rdr::Exception &e) {
245 close(e.str());
246 }
247}
Pierre Ossman04e62db2009-03-23 16:57:07 +0000248
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000249void VNCSConnectionST::screenLayoutChangeOrClose(rdr::U16 reason)
250{
251 try {
252 screenLayoutChange(reason);
Pierre Ossmanb218ecd2017-11-16 16:43:13 +0100253 writeFramebufferUpdate();
Pierre Ossman04e62db2009-03-23 16:57:07 +0000254 } catch(rdr::Exception &e) {
255 close(e.str());
256 }
257}
258
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000259void VNCSConnectionST::bellOrClose()
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000260{
261 try {
262 if (state() == RFBSTATE_NORMAL) writer()->writeBell();
263 } catch(rdr::Exception& e) {
264 close(e.str());
265 }
266}
267
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000268void VNCSConnectionST::serverCutTextOrClose(const char *str, int len)
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000269{
270 try {
271 if (!(accessRights & AccessCutText)) return;
272 if (!rfb::Server::sendCutText) return;
273 if (state() == RFBSTATE_NORMAL)
274 writer()->writeServerCutText(str, len);
275 } catch(rdr::Exception& e) {
276 close(e.str());
277 }
278}
279
Peter Ã…strandc39e0782009-01-15 12:21:42 +0000280
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000281void VNCSConnectionST::setDesktopNameOrClose(const char *name)
Peter Ã…strandc39e0782009-01-15 12:21:42 +0000282{
Peter Ã…strandc39e0782009-01-15 12:21:42 +0000283 try {
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000284 setDesktopName(name);
Pierre Ossmanb218ecd2017-11-16 16:43:13 +0100285 writeFramebufferUpdate();
Peter Ã…strandc39e0782009-01-15 12:21:42 +0000286 } catch(rdr::Exception& e) {
287 close(e.str());
288 }
289}
290
291
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000292void VNCSConnectionST::setCursorOrClose()
293{
294 try {
295 setCursor();
Pierre Ossmanb218ecd2017-11-16 16:43:13 +0100296 writeFramebufferUpdate();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000297 } catch(rdr::Exception& e) {
298 close(e.str());
299 }
300}
301
302
Pierre Ossmanb45a84f2016-12-12 16:59:15 +0100303void VNCSConnectionST::setLEDStateOrClose(unsigned int state)
304{
305 try {
306 setLEDState(state);
Pierre Ossmanb218ecd2017-11-16 16:43:13 +0100307 writeFramebufferUpdate();
Pierre Ossmanb45a84f2016-12-12 16:59:15 +0100308 } catch(rdr::Exception& e) {
309 close(e.str());
310 }
311}
312
313
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000314int VNCSConnectionST::checkIdleTimeout()
315{
316 int idleTimeout = rfb::Server::idleTimeout;
317 if (idleTimeout == 0) return 0;
318 if (state() != RFBSTATE_NORMAL && idleTimeout < 15)
319 idleTimeout = 15; // minimum of 15 seconds while authenticating
320 time_t now = time(0);
321 if (now < lastEventTime) {
322 // Someone must have set the time backwards. Set lastEventTime so that the
323 // idleTimeout will count from now.
324 vlog.info("Time has gone backwards - resetting idle timeout");
325 lastEventTime = now;
326 }
327 int timeLeft = lastEventTime + idleTimeout - now;
328 if (timeLeft < -60) {
329 // Our callback is over a minute late - someone must have set the time
330 // forwards. Set lastEventTime so that the idleTimeout will count from
331 // now.
332 vlog.info("Time has gone forwards - resetting idle timeout");
333 lastEventTime = now;
334 return secsToMillis(idleTimeout);
335 }
336 if (timeLeft <= 0) {
337 close("Idle timeout");
338 return 0;
339 }
340 return secsToMillis(timeLeft);
341}
342
Pierre Ossmanb114cec2011-11-20 15:36:11 +0000343
344bool VNCSConnectionST::getComparerState()
345{
346 // We interpret a low compression level as an indication that the client
347 // wants to prioritise CPU usage over bandwidth, and hence disable the
348 // comparing update tracker.
349 return (cp.compressLevel == -1) || (cp.compressLevel > 1);
350}
351
352
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000353// renderedCursorChange() is called whenever the server-side rendered cursor
354// changes shape or position. It ensures that the next update will clean up
355// the old rendered cursor and if necessary draw the new rendered cursor.
356
357void VNCSConnectionST::renderedCursorChange()
358{
359 if (state() != RFBSTATE_NORMAL) return;
Pierre Ossman71ca8d52017-09-15 11:03:12 +0200360 // Are we switching between client-side and server-side cursor?
Pierre Ossman25db44a2017-11-16 16:40:44 +0100361 if (clientHasCursor == needRenderedCursor())
Pierre Ossman71ca8d52017-09-15 11:03:12 +0200362 setCursorOrClose();
Pierre Ossman25db44a2017-11-16 16:40:44 +0100363 bool hasRenderedCursor = !damagedCursorRegion.is_empty();
Pierre Ossman330ca422017-11-06 13:15:55 +0100364 if (hasRenderedCursor)
Pierre Ossman5c9e1e52011-11-08 10:32:05 +0000365 removeRenderedCursor = true;
Pierre Ossman6b0bc292011-12-21 13:17:54 +0000366 if (needRenderedCursor()) {
Pierre Ossman671d2ef2015-06-09 16:09:06 +0200367 updateRenderedCursor = true;
Pierre Ossman6b0bc292011-12-21 13:17:54 +0000368 writeFramebufferUpdateOrClose();
369 }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000370}
371
372// needRenderedCursor() returns true if this client needs the server-side
373// rendered cursor. This may be because it does not support local cursor or
374// because the current cursor position has not been set by this client.
375// Unfortunately we can't know for sure when the current cursor position has
376// been set by this client. We guess that this is the case when the current
377// cursor position is the same as the last pointer event from this client, or
378// if it is a very short time since this client's last pointer event (up to a
379// second). [ Ideally we should do finer-grained timing here and make the time
380// configurable, but I don't think it's that important. ]
381
382bool VNCSConnectionST::needRenderedCursor()
383{
Pierre Ossman77ede0a2016-12-05 16:57:30 +0100384 if (state() != RFBSTATE_NORMAL)
385 return false;
386
Pierre Ossman324043e2017-08-16 16:26:11 +0200387 if (!cp.supportsLocalCursorWithAlpha &&
388 !cp.supportsLocalCursor && !cp.supportsLocalXCursor)
Pierre Ossman77ede0a2016-12-05 16:57:30 +0100389 return true;
390 if (!server->cursorPos.equals(pointerEventPos) &&
391 (time(0) - pointerEventTime) > 0)
392 return true;
393
394 return false;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000395}
396
397
398void VNCSConnectionST::approveConnectionOrClose(bool accept,
399 const char* reason)
400{
401 try {
402 approveConnection(accept, reason);
403 } catch (rdr::Exception& e) {
404 close(e.str());
405 }
406}
407
408
409
410// -=- Callbacks from SConnection
411
412void VNCSConnectionST::authSuccess()
413{
414 lastEventTime = time(0);
415
416 server->startDesktop();
417
418 // - Set the connection parameters appropriately
419 cp.width = server->pb->width();
420 cp.height = server->pb->height();
Pierre Ossman34e62f32009-03-20 21:46:12 +0000421 cp.screenLayout = server->screenLayout;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000422 cp.setName(server->getName());
Pierre Ossmanb45a84f2016-12-12 16:59:15 +0100423 cp.setLEDState(server->ledState);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000424
425 // - Set the default pixel format
426 cp.setPF(server->pb->getPF());
427 char buffer[256];
428 cp.pf().print(buffer, 256);
429 vlog.info("Server default pixel format %s", buffer);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000430
431 // - Mark the entire display as "dirty"
432 updates.add_changed(server->pb->getRect());
433 startTime = time(0);
434}
435
436void VNCSConnectionST::queryConnection(const char* userName)
437{
438 // - Authentication succeeded - clear from blacklist
439 CharArray name; name.buf = sock->getPeerAddress();
440 server->blHosts->clearBlackmark(name.buf);
441
442 // - Special case to provide a more useful error message
443 if (rfb::Server::neverShared && !rfb::Server::disconnectClients &&
444 server->authClientCount() > 0) {
445 approveConnection(false, "The server is already in use");
446 return;
447 }
448
449 // - Does the client have the right to bypass the query?
450 if (reverseConnection ||
451 !(rfb::Server::queryConnect || sock->requiresQuery()) ||
452 (accessRights & AccessNoQuery))
453 {
454 approveConnection(true);
455 return;
456 }
457
458 // - Get the server to display an Accept/Reject dialog, if required
459 // If a dialog is displayed, the result will be PENDING, and the
460 // server will call approveConnection at a later time
461 CharArray reason;
462 VNCServerST::queryResult qr = server->queryConnection(sock, userName,
463 &reason.buf);
Pierre Ossman36304752017-10-04 16:21:57 +0200464 if (qr == VNCServerST::PENDING)
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000465 return;
466
467 // - If server returns ACCEPT/REJECT then pass result to SConnection
468 approveConnection(qr == VNCServerST::ACCEPT, reason.buf);
469}
470
471void VNCSConnectionST::clientInit(bool shared)
472{
473 lastEventTime = time(0);
474 if (rfb::Server::alwaysShared || reverseConnection) shared = true;
Pierre Ossmane7be49b2014-12-02 14:33:17 +0100475 if (!(accessRights & AccessNonShared)) shared = true;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000476 if (rfb::Server::neverShared) shared = false;
477 if (!shared) {
Pierre Ossmane7be49b2014-12-02 14:33:17 +0100478 if (rfb::Server::disconnectClients && (accessRights & AccessNonShared)) {
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000479 // - Close all the other connected clients
480 vlog.debug("non-shared connection - closing clients");
481 server->closeClients("Non-shared connection requested", getSock());
482 } else {
483 // - Refuse this connection if there are existing clients, in addition to
484 // this one
485 if (server->authClientCount() > 1) {
486 close("Server is already in use");
487 return;
488 }
489 }
490 }
491 SConnection::clientInit(shared);
492}
493
494void VNCSConnectionST::setPixelFormat(const PixelFormat& pf)
495{
496 SConnection::setPixelFormat(pf);
497 char buffer[256];
498 pf.print(buffer, 256);
499 vlog.info("Client pixel format %s", buffer);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000500 setCursor();
501}
502
503void VNCSConnectionST::pointerEvent(const Point& pos, int buttonMask)
504{
505 pointerEventTime = lastEventTime = time(0);
506 server->lastUserInputTime = lastEventTime;
507 if (!(accessRights & AccessPtrEvents)) return;
508 if (!rfb::Server::acceptPointerEvents) return;
509 if (!server->pointerClient || server->pointerClient == this) {
510 pointerEventPos = pos;
511 if (buttonMask)
512 server->pointerClient = this;
513 else
514 server->pointerClient = 0;
515 server->desktop->pointerEvent(pointerEventPos, buttonMask);
516 }
517}
518
519
520class VNCSConnectionSTShiftPresser {
521public:
522 VNCSConnectionSTShiftPresser(SDesktop* desktop_)
523 : desktop(desktop_), pressed(false) {}
524 ~VNCSConnectionSTShiftPresser() {
Pierre Ossman9a153b02015-08-31 10:01:14 +0200525 if (pressed) {
526 vlog.debug("Releasing fake Shift_L");
Pierre Ossman5ae28212017-05-16 14:30:38 +0200527 desktop->keyEvent(XK_Shift_L, 0, false);
Pierre Ossman9a153b02015-08-31 10:01:14 +0200528 }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000529 }
530 void press() {
Pierre Ossman9a153b02015-08-31 10:01:14 +0200531 vlog.debug("Pressing fake Shift_L");
Pierre Ossman5ae28212017-05-16 14:30:38 +0200532 desktop->keyEvent(XK_Shift_L, 0, true);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000533 pressed = true;
534 }
535 SDesktop* desktop;
536 bool pressed;
537};
538
539// keyEvent() - record in the pressedKeys which keys were pressed. Allow
540// multiple down events (for autorepeat), but only allow a single up event.
Pierre Ossman5ae28212017-05-16 14:30:38 +0200541void VNCSConnectionST::keyEvent(rdr::U32 keysym, rdr::U32 keycode, bool down) {
Pierre Ossman16e1dcb2017-05-16 14:33:43 +0200542 rdr::U32 lookup;
543
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000544 lastEventTime = time(0);
545 server->lastUserInputTime = lastEventTime;
546 if (!(accessRights & AccessKeyEvents)) return;
547 if (!rfb::Server::acceptKeyEvents) return;
548
Pierre Ossman9a153b02015-08-31 10:01:14 +0200549 if (down)
Pierre Ossman5ae28212017-05-16 14:30:38 +0200550 vlog.debug("Key pressed: 0x%x / 0x%x", keysym, keycode);
Pierre Ossman9a153b02015-08-31 10:01:14 +0200551 else
Pierre Ossman5ae28212017-05-16 14:30:38 +0200552 vlog.debug("Key released: 0x%x / 0x%x", keysym, keycode);
Pierre Ossman9a153b02015-08-31 10:01:14 +0200553
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000554 // Remap the key if required
Pierre Ossman9a153b02015-08-31 10:01:14 +0200555 if (server->keyRemapper) {
556 rdr::U32 newkey;
Pierre Ossman5ae28212017-05-16 14:30:38 +0200557 newkey = server->keyRemapper->remapKey(keysym);
558 if (newkey != keysym) {
Pierre Ossman9a153b02015-08-31 10:01:14 +0200559 vlog.debug("Key remapped to 0x%x", newkey);
Pierre Ossman5ae28212017-05-16 14:30:38 +0200560 keysym = newkey;
Pierre Ossman9a153b02015-08-31 10:01:14 +0200561 }
562 }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000563
Pierre Ossmanbb305ca2016-12-11 12:41:26 +0100564 // Avoid lock keys if we don't know the server state
565 if ((server->ledState == ledUnknown) &&
Pierre Ossman5ae28212017-05-16 14:30:38 +0200566 ((keysym == XK_Caps_Lock) ||
567 (keysym == XK_Num_Lock) ||
568 (keysym == XK_Scroll_Lock))) {
Pierre Ossmanbb305ca2016-12-11 12:41:26 +0100569 vlog.debug("Ignoring lock key (e.g. caps lock)");
570 return;
571 }
Pierre Ossmanbb305ca2016-12-11 12:41:26 +0100572
Pierre Ossmanb45a84f2016-12-12 16:59:15 +0100573 // Lock key heuristics
574 // (only for clients that do not support the LED state extension)
575 if (!cp.supportsLEDState) {
576 // Always ignore ScrollLock as we don't have a heuristic
577 // for that
Pierre Ossman5ae28212017-05-16 14:30:38 +0200578 if (keysym == XK_Scroll_Lock) {
Pierre Ossmanb45a84f2016-12-12 16:59:15 +0100579 vlog.debug("Ignoring lock key (e.g. caps lock)");
580 return;
Pierre Ossmanbb305ca2016-12-11 12:41:26 +0100581 }
582
Pierre Ossmanb45a84f2016-12-12 16:59:15 +0100583 if (down && (server->ledState != ledUnknown)) {
584 // CapsLock synchronisation heuristic
585 // (this assumes standard interaction between CapsLock the Shift
586 // keys and normal characters)
Pierre Ossman5ae28212017-05-16 14:30:38 +0200587 if (((keysym >= XK_A) && (keysym <= XK_Z)) ||
588 ((keysym >= XK_a) && (keysym <= XK_z))) {
Pierre Ossmanb45a84f2016-12-12 16:59:15 +0100589 bool uppercase, shift, lock;
Pierre Ossmanbb305ca2016-12-11 12:41:26 +0100590
Pierre Ossman5ae28212017-05-16 14:30:38 +0200591 uppercase = (keysym >= XK_A) && (keysym <= XK_Z);
Pierre Ossman851e6802017-09-12 16:44:44 +0200592 shift = isShiftPressed();
Pierre Ossmanb45a84f2016-12-12 16:59:15 +0100593 lock = server->ledState & ledCapsLock;
Pierre Ossmanbb305ca2016-12-11 12:41:26 +0100594
Pierre Ossmanb45a84f2016-12-12 16:59:15 +0100595 if (lock == (uppercase == shift)) {
596 vlog.debug("Inserting fake CapsLock to get in sync with client");
Pierre Ossman5ae28212017-05-16 14:30:38 +0200597 server->desktop->keyEvent(XK_Caps_Lock, 0, true);
598 server->desktop->keyEvent(XK_Caps_Lock, 0, false);
Pierre Ossmanb45a84f2016-12-12 16:59:15 +0100599 }
600 }
601
602 // NumLock synchronisation heuristic
603 // (this is more cautious because of the differences between Unix,
604 // Windows and macOS)
Pierre Ossman5ae28212017-05-16 14:30:38 +0200605 if (((keysym >= XK_KP_Home) && (keysym <= XK_KP_Delete)) ||
606 ((keysym >= XK_KP_0) && (keysym <= XK_KP_9)) ||
607 (keysym == XK_KP_Separator) || (keysym == XK_KP_Decimal)) {
Pierre Ossmanb45a84f2016-12-12 16:59:15 +0100608 bool number, shift, lock;
609
Pierre Ossman5ae28212017-05-16 14:30:38 +0200610 number = ((keysym >= XK_KP_0) && (keysym <= XK_KP_9)) ||
611 (keysym == XK_KP_Separator) || (keysym == XK_KP_Decimal);
Pierre Ossman851e6802017-09-12 16:44:44 +0200612 shift = isShiftPressed();
Pierre Ossmanb45a84f2016-12-12 16:59:15 +0100613 lock = server->ledState & ledNumLock;
614
615 if (shift) {
616 // We don't know the appropriate NumLock state for when Shift
617 // is pressed as it could be one of:
618 //
619 // a) A Unix client where Shift negates NumLock
620 //
621 // b) A Windows client where Shift only cancels NumLock
622 //
623 // c) A macOS client where Shift doesn't have any effect
624 //
625 } else if (lock == (number == shift)) {
626 vlog.debug("Inserting fake NumLock to get in sync with client");
Pierre Ossman5ae28212017-05-16 14:30:38 +0200627 server->desktop->keyEvent(XK_Num_Lock, 0, true);
628 server->desktop->keyEvent(XK_Num_Lock, 0, false);
Pierre Ossmanb45a84f2016-12-12 16:59:15 +0100629 }
Pierre Ossmanbb305ca2016-12-11 12:41:26 +0100630 }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000631 }
632 }
633
634 // Turn ISO_Left_Tab into shifted Tab.
635 VNCSConnectionSTShiftPresser shiftPresser(server->desktop);
Pierre Ossman5ae28212017-05-16 14:30:38 +0200636 if (keysym == XK_ISO_Left_Tab) {
Pierre Ossman851e6802017-09-12 16:44:44 +0200637 if (!isShiftPressed())
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000638 shiftPresser.press();
Pierre Ossman5ae28212017-05-16 14:30:38 +0200639 keysym = XK_Tab;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000640 }
641
Pierre Ossman16e1dcb2017-05-16 14:33:43 +0200642 // We need to be able to track keys, so generate a fake index when we
643 // aren't given a keycode
644 if (keycode == 0)
645 lookup = 0x80000000 | keysym;
646 else
647 lookup = keycode;
648
649 // We force the same keysym for an already down key for the
650 // sake of sanity
651 if (pressedKeys.find(lookup) != pressedKeys.end())
652 keysym = pressedKeys[lookup];
653
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000654 if (down) {
Pierre Ossman16e1dcb2017-05-16 14:33:43 +0200655 pressedKeys[lookup] = keysym;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000656 } else {
Pierre Ossman16e1dcb2017-05-16 14:33:43 +0200657 if (!pressedKeys.erase(lookup))
Pierre Ossman5ae28212017-05-16 14:30:38 +0200658 return;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000659 }
Pierre Ossman16e1dcb2017-05-16 14:33:43 +0200660
Pierre Ossman5ae28212017-05-16 14:30:38 +0200661 server->desktop->keyEvent(keysym, keycode, down);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000662}
663
664void VNCSConnectionST::clientCutText(const char* str, int len)
665{
666 if (!(accessRights & AccessCutText)) return;
667 if (!rfb::Server::acceptCutText) return;
668 server->desktop->clientCutText(str, len);
669}
670
671void VNCSConnectionST::framebufferUpdateRequest(const Rect& r,bool incremental)
672{
Pierre Ossmane9962f72009-04-23 12:31:42 +0000673 Rect safeRect;
674
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000675 if (!(accessRights & AccessView)) return;
676
677 SConnection::framebufferUpdateRequest(r, incremental);
678
Pierre Ossmand9a59ba2009-03-20 15:55:37 +0000679 // Check that the client isn't sending crappy requests
680 if (!r.enclosed_by(Rect(0, 0, cp.width, cp.height))) {
681 vlog.error("FramebufferUpdateRequest %dx%d at %d,%d exceeds framebuffer %dx%d",
682 r.width(), r.height(), r.tl.x, r.tl.y, cp.width, cp.height);
Pierre Ossmane9962f72009-04-23 12:31:42 +0000683 safeRect = r.intersect(Rect(0, 0, cp.width, cp.height));
684 } else {
685 safeRect = r;
Pierre Ossmand9a59ba2009-03-20 15:55:37 +0000686 }
687
Constantin Kaplinsky2ef66952008-08-29 11:33:46 +0000688 // Just update the requested region.
689 // Framebuffer update will be sent a bit later, see processMessages().
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000690 Region reqRgn(r);
Pierre Ossman1b478e52011-11-15 12:08:30 +0000691 if (!incremental || !continuousUpdates)
692 requested.assign_union(reqRgn);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000693
694 if (!incremental) {
695 // Non-incremental update - treat as if area requested has changed
696 updates.add_changed(reqRgn);
Pierre Ossman53125a72009-04-22 15:37:51 +0000697
698 // And send the screen layout to the client (which, unlike the
699 // framebuffer dimensions, the client doesn't get during init)
Pierre Ossmanc5e25602009-03-20 12:59:05 +0000700 writer()->writeExtendedDesktopSize();
Pierre Ossman53125a72009-04-22 15:37:51 +0000701
702 // We do not send a DesktopSize since it only contains the
703 // framebuffer size (which the client already should know) and
704 // because some clients don't handle extra DesktopSize events
705 // very well.
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000706 }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000707}
708
Pierre Ossman34bb0612009-03-21 21:16:14 +0000709void VNCSConnectionST::setDesktopSize(int fb_width, int fb_height,
710 const ScreenSet& layout)
Pierre Ossmanc5e25602009-03-20 12:59:05 +0000711{
Pierre Ossman04e62db2009-03-23 16:57:07 +0000712 unsigned int result;
713
Michal Srbb318b8f2014-11-24 13:18:28 +0200714 if (!(accessRights & AccessSetDesktopSize)) return;
715 if (!rfb::Server::acceptSetDesktopSize) return;
716
Pierre Ossman04e62db2009-03-23 16:57:07 +0000717 // Don't bother the desktop with an invalid configuration
718 if (!layout.validate(fb_width, fb_height)) {
719 writer()->writeExtendedDesktopSize(reasonClient, resultInvalid,
720 fb_width, fb_height, layout);
Pierre Ossman04e62db2009-03-23 16:57:07 +0000721 return;
722 }
723
724 // FIXME: the desktop will call back to VNCServerST and an extra set
725 // of ExtendedDesktopSize messages will be sent. This is okay
726 // protocol-wise, but unnecessary.
727 result = server->desktop->setScreenLayout(fb_width, fb_height, layout);
728
Pierre Ossman04e62db2009-03-23 16:57:07 +0000729 writer()->writeExtendedDesktopSize(reasonClient, result,
730 fb_width, fb_height, layout);
Pierre Ossman04e62db2009-03-23 16:57:07 +0000731
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000732 // Only notify other clients on success
Pierre Ossman04e62db2009-03-23 16:57:07 +0000733 if (result == resultSuccess) {
734 if (server->screenLayout != layout)
735 throw Exception("Desktop configured a different screen layout than requested");
736 server->notifyScreenLayoutChange(this);
737 }
Pierre Ossmanc5e25602009-03-20 12:59:05 +0000738}
739
Pierre Ossman2c764942011-11-14 15:54:30 +0000740void VNCSConnectionST::fence(rdr::U32 flags, unsigned len, const char data[])
741{
Pierre Ossmanb2a417c2015-12-11 19:32:21 +0100742 rdr::U8 type;
743
Pierre Ossman2c764942011-11-14 15:54:30 +0000744 if (flags & fenceFlagRequest) {
745 if (flags & fenceFlagSyncNext) {
Pierre Ossmanb8b1e962012-07-20 10:47:00 +0000746 pendingSyncFence = true;
Pierre Ossman2c764942011-11-14 15:54:30 +0000747
748 fenceFlags = flags & (fenceFlagBlockBefore | fenceFlagBlockAfter | fenceFlagSyncNext);
749 fenceDataLen = len;
750 delete [] fenceData;
Michal Srbf3afa242017-03-27 19:02:15 +0300751 fenceData = NULL;
Pierre Ossman2c764942011-11-14 15:54:30 +0000752 if (len > 0) {
753 fenceData = new char[len];
754 memcpy(fenceData, data, len);
755 }
756
757 return;
758 }
759
760 // We handle everything synchronously so we trivially honor these modes
761 flags = flags & (fenceFlagBlockBefore | fenceFlagBlockAfter);
762
763 writer()->writeFence(flags, len, data);
764 return;
765 }
766
Pierre Ossmanb2a417c2015-12-11 19:32:21 +0100767 if (len < 1)
768 vlog.error("Fence response of unexpected size received");
Pierre Ossman1b478e52011-11-15 12:08:30 +0000769
Pierre Ossmanb2a417c2015-12-11 19:32:21 +0100770 type = data[0];
771
772 switch (type) {
Pierre Ossman2c764942011-11-14 15:54:30 +0000773 case 0:
774 // Initial dummy fence;
775 break;
Pierre Ossmanb2a417c2015-12-11 19:32:21 +0100776 case 1:
Pierre Ossmanc09e5582015-12-11 20:23:17 +0100777 congestion.gotPong();
Pierre Ossman1b478e52011-11-15 12:08:30 +0000778 break;
Pierre Ossman2c764942011-11-14 15:54:30 +0000779 default:
Pierre Ossmanb2a417c2015-12-11 19:32:21 +0100780 vlog.error("Fence response of unexpected type received");
Pierre Ossman2c764942011-11-14 15:54:30 +0000781 }
782}
783
Pierre Ossman1b478e52011-11-15 12:08:30 +0000784void VNCSConnectionST::enableContinuousUpdates(bool enable,
785 int x, int y, int w, int h)
786{
787 Rect rect;
788
789 if (!cp.supportsFence || !cp.supportsContinuousUpdates)
790 throw Exception("Client tried to enable continuous updates when not allowed");
791
792 continuousUpdates = enable;
793
794 rect.setXYWH(x, y, w, h);
795 cuRegion.reset(rect);
796
797 if (enable) {
798 requested.clear();
Pierre Ossman1b478e52011-11-15 12:08:30 +0000799 } else {
800 writer()->writeEndOfContinuousUpdates();
801 }
802}
803
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000804// supportsLocalCursor() is called whenever the status of
805// cp.supportsLocalCursor has changed. If the client does now support local
806// cursor, we make sure that the old server-side rendered cursor is cleaned up
807// and the cursor is sent to the client.
808
809void VNCSConnectionST::supportsLocalCursor()
810{
Pierre Ossman387a4172017-11-16 16:44:36 +0100811 bool hasRenderedCursor = !damagedCursorRegion.is_empty();
812 if (hasRenderedCursor && !needRenderedCursor())
813 removeRenderedCursor = true;
814 setCursor();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000815}
816
Pierre Ossman2c764942011-11-14 15:54:30 +0000817void VNCSConnectionST::supportsFence()
818{
Pierre Ossmanb2a417c2015-12-11 19:32:21 +0100819 char type = 0;
820 writer()->writeFence(fenceFlagRequest, sizeof(type), &type);
Pierre Ossman2c764942011-11-14 15:54:30 +0000821}
822
Pierre Ossman1b478e52011-11-15 12:08:30 +0000823void VNCSConnectionST::supportsContinuousUpdates()
824{
825 // We refuse to use continuous updates if we cannot monitor the buffer
826 // usage using fences.
827 if (!cp.supportsFence)
828 return;
829
830 writer()->writeEndOfContinuousUpdates();
831}
832
Pierre Ossmanb45a84f2016-12-12 16:59:15 +0100833void VNCSConnectionST::supportsLEDState()
834{
835 writer()->writeLEDState();
836}
837
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000838
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +0000839bool VNCSConnectionST::handleTimeout(Timer* t)
840{
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000841 try {
Pierre Ossmana40ab202016-04-29 15:35:56 +0200842 if (t == &congestionTimer)
Pierre Ossmana99d14d2015-12-13 15:43:46 +0100843 writeFramebufferUpdate();
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000844 } catch (rdr::Exception& e) {
845 close(e.str());
846 }
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +0000847
848 return false;
849}
850
Pierre Ossman851e6802017-09-12 16:44:44 +0200851bool VNCSConnectionST::isShiftPressed()
852{
853 std::map<rdr::U32, rdr::U32>::const_iterator iter;
854
855 for (iter = pressedKeys.begin(); iter != pressedKeys.end(); ++iter) {
856 if (iter->second == XK_Shift_L)
857 return true;
858 if (iter->second == XK_Shift_R)
859 return true;
860 }
861
862 return false;
863}
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +0000864
Pierre Ossman1b478e52011-11-15 12:08:30 +0000865void VNCSConnectionST::writeRTTPing()
866{
Pierre Ossmanb2a417c2015-12-11 19:32:21 +0100867 char type;
Pierre Ossman1b478e52011-11-15 12:08:30 +0000868
869 if (!cp.supportsFence)
870 return;
871
Pierre Ossmana99d14d2015-12-13 15:43:46 +0100872 congestion.updatePosition(sock->outStream().length());
Pierre Ossman1b478e52011-11-15 12:08:30 +0000873
874 // We need to make sure any old update are already processed by the
875 // time we get the response back. This allows us to reliably throttle
876 // back on client overload, as well as network overload.
Pierre Ossmanb2a417c2015-12-11 19:32:21 +0100877 type = 1;
Pierre Ossman1b478e52011-11-15 12:08:30 +0000878 writer()->writeFence(fenceFlagRequest | fenceFlagBlockBefore,
Pierre Ossmanb2a417c2015-12-11 19:32:21 +0100879 sizeof(type), &type);
Pierre Ossman1b478e52011-11-15 12:08:30 +0000880
Pierre Ossmana99d14d2015-12-13 15:43:46 +0100881 congestion.sentPing();
Pierre Ossman1b478e52011-11-15 12:08:30 +0000882}
883
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +0000884bool VNCSConnectionST::isCongested()
885{
Pierre Ossmance261812018-07-17 15:01:53 +0200886 int eta;
Pierre Ossmana99d14d2015-12-13 15:43:46 +0100887
888 congestionTimer.stop();
Pierre Ossman1b478e52011-11-15 12:08:30 +0000889
890 // Stuff still waiting in the send buffer?
Pierre Ossman352d0622016-04-29 15:50:54 +0200891 sock->outStream().flush();
Pierre Ossman8cf71632016-03-11 09:38:08 +0100892 congestion.debugTrace("congestion-trace.csv", sock->getFd());
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +0000893 if (sock->outStream().bufferUsage() > 0)
894 return true;
895
Pierre Ossman1b478e52011-11-15 12:08:30 +0000896 if (!cp.supportsFence)
897 return false;
898
Pierre Ossmana99d14d2015-12-13 15:43:46 +0100899 congestion.updatePosition(sock->outStream().length());
900 if (!congestion.isCongested())
Pierre Ossman1b478e52011-11-15 12:08:30 +0000901 return false;
902
Pierre Ossmana99d14d2015-12-13 15:43:46 +0100903 eta = congestion.getUncongestedETA();
904 if (eta >= 0)
905 congestionTimer.start(eta);
Pierre Ossman1b478e52011-11-15 12:08:30 +0000906
907 return true;
908}
909
910
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000911void VNCSConnectionST::writeFramebufferUpdate()
912{
Pierre Ossmana99d14d2015-12-13 15:43:46 +0100913 congestion.updatePosition(sock->outStream().length());
914
Pierre Ossman2c764942011-11-14 15:54:30 +0000915 // We're in the middle of processing a command that's supposed to be
916 // synchronised. Allowing an update to slip out right now might violate
917 // that synchronisation.
918 if (syncFence)
919 return;
920
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000921 // We try to aggregate responses, so don't send out anything whilst we
922 // still have incoming messages. processMessages() will give us another
923 // chance to run once things are idle.
924 if (inProcessMessages)
925 return;
926
Pierre Ossman1b478e52011-11-15 12:08:30 +0000927 if (state() != RFBSTATE_NORMAL)
928 return;
929 if (requested.is_empty() && !continuousUpdates)
Pierre Ossmane9962f72009-04-23 12:31:42 +0000930 return;
Pierre Ossmand9a59ba2009-03-20 15:55:37 +0000931
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +0000932 // Check that we actually have some space on the link and retry in a
933 // bit if things are congested.
Pierre Ossmana40ab202016-04-29 15:35:56 +0200934 if (isCongested())
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +0000935 return;
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +0000936
Pierre Ossmancef3cf72016-11-25 10:06:34 +0100937 // Updates often consists of many small writes, and in continuous
938 // mode, we will also have small fence messages around the update. We
939 // need to aggregate these in order to not clog up TCP's congestion
940 // window.
Peter Ã…strand (astrand)01dc1a62017-10-10 12:56:04 +0200941 sock->cork(true);
Pierre Ossman36dadf82011-11-15 12:11:32 +0000942
Pierre Ossmane9962f72009-04-23 12:31:42 +0000943 // First take care of any updates that cannot contain framebuffer data
944 // changes.
Pierre Ossmancef3cf72016-11-25 10:06:34 +0100945 writeNoDataUpdate();
946
947 // Then real data (if possible)
948 writeDataUpdate();
949
Peter Ã…strand (astrand)01dc1a62017-10-10 12:56:04 +0200950 sock->cork(false);
Pierre Ossmana99d14d2015-12-13 15:43:46 +0100951
952 congestion.updatePosition(sock->outStream().length());
Pierre Ossmancef3cf72016-11-25 10:06:34 +0100953}
954
955void VNCSConnectionST::writeNoDataUpdate()
956{
957 if (!writer()->needNoDataUpdate())
958 return;
959
960 writer()->writeNoDataUpdate();
961
962 // Make sure no data update is sent until next request
963 requested.clear();
964}
965
966void VNCSConnectionST::writeDataUpdate()
967{
Pierre Ossman8efc7b42018-03-23 11:45:51 +0100968 Region req, pending;
Pierre Ossmancef3cf72016-11-25 10:06:34 +0100969 UpdateInfo ui;
970 bool needNewUpdateInfo;
Pierre Ossman24684e52016-12-05 16:58:19 +0100971 const RenderedCursor *cursor;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000972
Constantin Kaplinsky604d7812007-08-31 15:50:37 +0000973 updates.enable_copyrect(cp.useCopyRect);
974
Pierre Ossmancef3cf72016-11-25 10:06:34 +0100975 // See what the client has requested (if anything)
Pierre Ossman1b478e52011-11-15 12:08:30 +0000976 if (continuousUpdates)
977 req = cuRegion.union_(requested);
978 else
979 req = requested;
980
Pierre Ossmancef3cf72016-11-25 10:06:34 +0100981 if (req.is_empty())
982 return;
983
Pierre Ossman8efc7b42018-03-23 11:45:51 +0100984 // Get any framebuffer changes we haven't yet been informed of
985 pending = server->getPendingRegion();
986
Pierre Ossmancef3cf72016-11-25 10:06:34 +0100987 // Get the lists of updates. Prior to exporting the data to the `ui' object,
988 // getUpdateInfo() will normalize the `updates' object such way that its
989 // `changed' and `copied' regions would not intersect.
Pierre Ossman1b478e52011-11-15 12:08:30 +0000990 updates.getUpdateInfo(&ui, req);
991 needNewUpdateInfo = false;
Constantin Kaplinsky604d7812007-08-31 15:50:37 +0000992
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000993 // If the previous position of the rendered cursor overlaps the source of the
994 // copy, then when the copy happens the corresponding rectangle in the
995 // destination will be wrong, so add it to the changed region.
996
Pierre Ossman671d2ef2015-06-09 16:09:06 +0200997 if (!ui.copied.is_empty() && !damagedCursorRegion.is_empty()) {
998 Region bogusCopiedCursor;
999
Pierre Ossman74385d32018-03-22 16:00:18 +01001000 bogusCopiedCursor = damagedCursorRegion;
Pierre Ossman671d2ef2015-06-09 16:09:06 +02001001 bogusCopiedCursor.translate(ui.copy_delta);
1002 bogusCopiedCursor.assign_intersect(server->pb->getRect());
Constantin Kaplinsky45517c82007-08-31 15:56:33 +00001003 if (!ui.copied.intersect(bogusCopiedCursor).is_empty()) {
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001004 updates.add_changed(bogusCopiedCursor);
Constantin Kaplinsky604d7812007-08-31 15:50:37 +00001005 needNewUpdateInfo = true;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001006 }
1007 }
1008
Pierre Ossman671d2ef2015-06-09 16:09:06 +02001009 // If we need to remove the old rendered cursor, just add the region to
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001010 // the changed region.
1011
1012 if (removeRenderedCursor) {
Pierre Ossman671d2ef2015-06-09 16:09:06 +02001013 updates.add_changed(damagedCursorRegion);
Constantin Kaplinsky604d7812007-08-31 15:50:37 +00001014 needNewUpdateInfo = true;
Pierre Ossman671d2ef2015-06-09 16:09:06 +02001015 damagedCursorRegion.clear();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001016 removeRenderedCursor = false;
1017 }
1018
Pierre Ossman8c3bd692018-03-22 15:58:54 +01001019 // If we need a full cursor update then make sure its entire region
1020 // is marked as changed.
1021
1022 if (updateRenderedCursor) {
1023 updates.add_changed(server->getRenderedCursor()->getEffectiveRect());
1024 needNewUpdateInfo = true;
1025 updateRenderedCursor = false;
1026 }
1027
Constantin Kaplinsky604d7812007-08-31 15:50:37 +00001028 // The `updates' object could change, make sure we have valid update info.
1029
1030 if (needNewUpdateInfo)
Pierre Ossman1b478e52011-11-15 12:08:30 +00001031 updates.getUpdateInfo(&ui, req);
Constantin Kaplinsky604d7812007-08-31 15:50:37 +00001032
Pierre Ossman8efc7b42018-03-23 11:45:51 +01001033 // If there are queued updates then we cannot safely send an update
1034 // without risking a partially updated screen
1035
1036 if (!pending.is_empty()) {
1037 // However we might still be able to send a lossless refresh
1038 req.assign_subtract(pending);
1039 req.assign_subtract(ui.changed);
1040 req.assign_subtract(ui.copied);
1041
1042 ui.changed.clear();
1043 ui.copied.clear();
1044 }
1045
Pierre Ossman8c3bd692018-03-22 15:58:54 +01001046 // Does the client need a server-side rendered cursor?
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001047
Pierre Ossman24684e52016-12-05 16:58:19 +01001048 cursor = NULL;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001049 if (needRenderedCursor()) {
Pierre Ossman671d2ef2015-06-09 16:09:06 +02001050 Rect renderedCursorRect;
1051
Pierre Ossman24684e52016-12-05 16:58:19 +01001052 cursor = server->getRenderedCursor();
Pierre Ossman8c3bd692018-03-22 15:58:54 +01001053 renderedCursorRect = cursor->getEffectiveRect();
Pierre Ossman24684e52016-12-05 16:58:19 +01001054
Pierre Ossman8c3bd692018-03-22 15:58:54 +01001055 // Check that we don't try to copy over the cursor area, and
1056 // if that happens we need to treat it as changed so that we can
1057 // re-render it
1058 if (!ui.copied.intersect(renderedCursorRect).is_empty()) {
1059 ui.changed.assign_union(ui.copied.intersect(renderedCursorRect));
1060 ui.copied.assign_subtract(renderedCursorRect);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001061 }
1062
Pierre Ossman8c3bd692018-03-22 15:58:54 +01001063 // Track where we've rendered the cursor
1064 damagedCursorRegion.assign_union(ui.changed.intersect(renderedCursorRect));
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001065 }
1066
Pierre Ossman8efc7b42018-03-23 11:45:51 +01001067 // Return if there is nothing to send the client.
1068
Pierre Ossman6b2f1132016-11-30 08:03:35 +01001069 if (ui.is_empty() && !writer()->needFakeUpdate() &&
1070 !encodeManager.needsLosslessRefresh(req))
Pierre Ossmancef3cf72016-11-25 10:06:34 +01001071 return;
Pierre Ossmanfdba3fe2014-01-31 13:12:18 +01001072
Pierre Ossmancef3cf72016-11-25 10:06:34 +01001073 writeRTTPing();
Pierre Ossman1b478e52011-11-15 12:08:30 +00001074
Pierre Ossman6b2f1132016-11-30 08:03:35 +01001075 if (!ui.is_empty())
1076 encodeManager.writeUpdate(ui, server->getPixelBuffer(), cursor);
Pierre Ossmana2b80d62018-03-23 09:30:09 +01001077 else {
1078 size_t maxUpdateSize;
1079
1080 // FIXME: If continuous updates aren't used then the client might
1081 // be slower than frameRate in its requests and we could
1082 // afford a larger update size
1083
1084 // FIXME: Bandwidth estimation without congestion control
1085 maxUpdateSize = congestion.getBandwidth() *
1086 server->msToNextUpdate() / 1000;
1087
1088 encodeManager.writeLosslessRefresh(req, server->getPixelBuffer(),
1089 cursor, maxUpdateSize);
1090 }
Pierre Ossman1b478e52011-11-15 12:08:30 +00001091
Pierre Ossmancef3cf72016-11-25 10:06:34 +01001092 writeRTTPing();
Pierre Ossmanc0397262014-03-14 15:59:46 +01001093
Pierre Ossmancef3cf72016-11-25 10:06:34 +01001094 // The request might be for just part of the screen, so we cannot
1095 // just clear the entire update tracker.
1096 updates.subtract(req);
Pierre Ossmanc0397262014-03-14 15:59:46 +01001097
Pierre Ossmancef3cf72016-11-25 10:06:34 +01001098 requested.clear();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001099}
1100
1101
Pierre Ossmana3ac01e2011-11-07 21:13:54 +00001102void VNCSConnectionST::screenLayoutChange(rdr::U16 reason)
1103{
1104 if (!authenticated())
1105 return;
1106
1107 cp.screenLayout = server->screenLayout;
1108
1109 if (state() != RFBSTATE_NORMAL)
1110 return;
1111
1112 writer()->writeExtendedDesktopSize(reason, 0, cp.width, cp.height,
1113 cp.screenLayout);
Pierre Ossmana3ac01e2011-11-07 21:13:54 +00001114}
1115
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001116
1117// setCursor() is called whenever the cursor has changed shape or pixel format.
1118// If the client supports local cursor then it will arrange for the cursor to
1119// be sent to the client.
1120
1121void VNCSConnectionST::setCursor()
1122{
Pierre Ossmana3ac01e2011-11-07 21:13:54 +00001123 if (state() != RFBSTATE_NORMAL)
1124 return;
Pierre Ossmana3ac01e2011-11-07 21:13:54 +00001125
Pierre Ossman71ca8d52017-09-15 11:03:12 +02001126 // We need to blank out the client's cursor or there will be two
Pierre Ossman25db44a2017-11-16 16:40:44 +01001127 if (needRenderedCursor()) {
Pierre Ossman71ca8d52017-09-15 11:03:12 +02001128 cp.setCursor(emptyCursor);
Pierre Ossman25db44a2017-11-16 16:40:44 +01001129 clientHasCursor = false;
1130 } else {
Pierre Ossman71ca8d52017-09-15 11:03:12 +02001131 cp.setCursor(*server->cursor);
Pierre Ossman25db44a2017-11-16 16:40:44 +01001132 clientHasCursor = true;
1133 }
Pierre Ossman126e5642014-02-13 14:40:25 +01001134
Pierre Ossman8053c8e2017-02-21 12:59:04 +01001135 if (!writer()->writeSetCursorWithAlpha()) {
1136 if (!writer()->writeSetCursor()) {
1137 if (!writer()->writeSetXCursor()) {
1138 // No client support
1139 return;
1140 }
Pierre Ossman126e5642014-02-13 14:40:25 +01001141 }
1142 }
Pierre Ossmana3ac01e2011-11-07 21:13:54 +00001143}
1144
1145void VNCSConnectionST::setDesktopName(const char *name)
1146{
1147 cp.setName(name);
1148
1149 if (state() != RFBSTATE_NORMAL)
1150 return;
1151
1152 if (!writer()->writeSetDesktopName()) {
1153 fprintf(stderr, "Client does not support desktop rename\n");
1154 return;
1155 }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001156}
1157
Pierre Ossmanb45a84f2016-12-12 16:59:15 +01001158void VNCSConnectionST::setLEDState(unsigned int ledstate)
1159{
1160 if (state() != RFBSTATE_NORMAL)
1161 return;
1162
1163 cp.setLEDState(ledstate);
1164
Pierre Ossmanb218ecd2017-11-16 16:43:13 +01001165 writer()->writeLEDState();
Pierre Ossmanb45a84f2016-12-12 16:59:15 +01001166}
1167
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001168void VNCSConnectionST::setSocketTimeouts()
1169{
1170 int timeoutms = rfb::Server::clientWaitTimeMillis;
1171 soonestTimeout(&timeoutms, secsToMillis(rfb::Server::idleTimeout));
1172 if (timeoutms == 0)
1173 timeoutms = -1;
1174 sock->inStream().setTimeout(timeoutms);
1175 sock->outStream().setTimeout(timeoutms);
1176}
1177
1178char* VNCSConnectionST::getStartTime()
1179{
1180 char* result = ctime(&startTime);
1181 result[24] = '\0';
1182 return result;
1183}
1184
1185void VNCSConnectionST::setStatus(int status)
1186{
1187 switch (status) {
1188 case 0:
1189 accessRights = accessRights | AccessPtrEvents | AccessKeyEvents | AccessView;
1190 break;
1191 case 1:
Pierre Ossman7728be22015-03-03 16:39:37 +01001192 accessRights = (accessRights & ~(AccessPtrEvents | AccessKeyEvents)) | AccessView;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001193 break;
1194 case 2:
Adam Tkac8e985062011-02-07 11:33:57 +00001195 accessRights = accessRights & ~(AccessPtrEvents | AccessKeyEvents | AccessView);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001196 break;
1197 }
1198 framebufferUpdateRequest(server->pb->getRect(), false);
1199}
1200int VNCSConnectionST::getStatus()
1201{
1202 if ((accessRights & (AccessPtrEvents | AccessKeyEvents | AccessView)) == 0x0007)
1203 return 0;
1204 if ((accessRights & (AccessPtrEvents | AccessKeyEvents | AccessView)) == 0x0001)
1205 return 1;
1206 if ((accessRights & (AccessPtrEvents | AccessKeyEvents | AccessView)) == 0x0000)
1207 return 2;
1208 return 4;
1209}
1210