blob: 7b261e7428ee1db3391015ab8ff521ffa8e0176c [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 Ossman025326d2018-10-08 16:03:01 +020055 clientHasCursor(false)
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000056{
57 setStreams(&sock->inStream(), &sock->outStream());
58 peerEndpoint.buf = sock->getPeerEndpoint();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000059
60 // Configure the socket
61 setSocketTimeouts();
62 lastEventTime = time(0);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000063}
64
65
66VNCSConnectionST::~VNCSConnectionST()
67{
68 // If we reach here then VNCServerST is deleting us!
Pierre Ossman6c97fa42018-10-05 17:35:51 +020069 if (closeReason.buf)
70 vlog.info("closing %s: %s", peerEndpoint.buf, closeReason.buf);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000071
72 // Release any keys the client still had pressed
Pierre Ossman16e1dcb2017-05-16 14:33:43 +020073 while (!pressedKeys.empty()) {
74 rdr::U32 keysym, keycode;
75
76 keysym = pressedKeys.begin()->second;
77 keycode = pressedKeys.begin()->first;
78 pressedKeys.erase(pressedKeys.begin());
79
80 vlog.debug("Releasing key 0x%x / 0x%x on client disconnect",
81 keysym, keycode);
Pierre Ossmanb6843412018-10-05 17:30:52 +020082 server->keyEvent(keysym, keycode, false);
Pierre Ossman9a153b02015-08-31 10:01:14 +020083 }
Pierre Ossman16e1dcb2017-05-16 14:33:43 +020084
Pierre Ossman2c764942011-11-14 15:54:30 +000085 delete [] fenceData;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000086}
87
88
Pierre Ossman7d64b332018-10-08 15:59:02 +020089// SConnection methods
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000090
Pierre Ossman7d64b332018-10-08 15:59:02 +020091bool VNCSConnectionST::accessCheck(AccessRights ar) const
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000092{
Pierre Ossman7d64b332018-10-08 15:59:02 +020093 // Reverse connections are user initiated, so they are implicitly
94 // allowed to bypass the query
95 if (reverseConnection)
96 ar &= ~AccessNoQuery;
97
98 return SConnection::accessCheck(ar);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000099}
100
101void VNCSConnectionST::close(const char* reason)
102{
103 // Log the reason for the close
104 if (!closeReason.buf)
Adam Tkacd36b6262009-09-04 10:57:20 +0000105 closeReason.buf = strDup(reason);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000106 else
107 vlog.debug("second close: %s (%s)", peerEndpoint.buf, reason);
108
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000109 // Just shutdown the socket and mark our state as closing. Eventually the
110 // calling code will call VNCServerST's removeSocket() method causing us to
111 // be deleted.
112 sock->shutdown();
Pierre Ossman7d64b332018-10-08 15:59:02 +0200113
114 SConnection::close(reason);
115}
116
117
118// Methods called from VNCServerST
119
120bool VNCSConnectionST::init()
121{
122 try {
123 initialiseProtocol();
124 } catch (rdr::Exception& e) {
125 close(e.str());
126 return false;
127 }
128 return true;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000129}
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;
Pierre Ossman6094ced2018-10-05 17:24:51 +0200194 if (cp.width && cp.height &&
195 (server->getPixelBuffer()->width() != cp.width ||
196 server->getPixelBuffer()->height() != cp.height))
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000197 {
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 Ossman6094ced2018-10-05 17:24:51 +0200213 damagedCursorRegion.assign_intersect(server->getPixelBuffer()->getRect());
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000214
Pierre Ossman6094ced2018-10-05 17:24:51 +0200215 cp.width = server->getPixelBuffer()->width();
216 cp.height = server->getPixelBuffer()->height();
217 cp.screenLayout = server->getScreenLayout();
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
Pierre Ossman6094ced2018-10-05 17:24:51 +0200229 encodeManager.pruneLosslessRefresh(Region(server->getPixelBuffer()->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();
Pierre Ossman6094ced2018-10-05 17:24:51 +0200234 updates.add_changed(server->getPixelBuffer()->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 {
Pierre Ossman7d64b332018-10-08 15:59:02 +0200272 if (!accessCheck(AccessCutText)) return;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000273 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;
Pierre Ossman6094ced2018-10-05 17:24:51 +0200391 if (!server->getCursorPos().equals(pointerEventPos) &&
Pierre Ossman77ede0a2016-12-05 16:57:30 +0100392 (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
Pierre Ossman6094ced2018-10-05 17:24:51 +0200418 cp.width = server->getPixelBuffer()->width();
419 cp.height = server->getPixelBuffer()->height();
420 cp.screenLayout = server->getScreenLayout();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000421 cp.setName(server->getName());
Pierre Ossman6094ced2018-10-05 17:24:51 +0200422 cp.setLEDState(server->getLEDState());
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000423
424 // - Set the default pixel format
Pierre Ossman6094ced2018-10-05 17:24:51 +0200425 cp.setPF(server->getPixelBuffer()->getPF());
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000426 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"
Pierre Ossman6094ced2018-10-05 17:24:51 +0200431 updates.add_changed(server->getPixelBuffer()->getRect());
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000432}
433
434void VNCSConnectionST::queryConnection(const char* userName)
435{
Pierre Ossman6c97fa42018-10-05 17:35:51 +0200436 server->queryConnection(this, userName);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000437}
438
439void VNCSConnectionST::clientInit(bool shared)
440{
441 lastEventTime = time(0);
442 if (rfb::Server::alwaysShared || reverseConnection) shared = true;
Pierre Ossman7d64b332018-10-08 15:59:02 +0200443 if (!accessCheck(AccessNonShared)) shared = true;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000444 if (rfb::Server::neverShared) shared = false;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000445 SConnection::clientInit(shared);
Pierre Ossman6c97fa42018-10-05 17:35:51 +0200446 server->clientReady(this, shared);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000447}
448
449void VNCSConnectionST::setPixelFormat(const PixelFormat& pf)
450{
451 SConnection::setPixelFormat(pf);
452 char buffer[256];
453 pf.print(buffer, 256);
454 vlog.info("Client pixel format %s", buffer);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000455 setCursor();
456}
457
458void VNCSConnectionST::pointerEvent(const Point& pos, int buttonMask)
459{
460 pointerEventTime = lastEventTime = time(0);
Pierre Ossman7d64b332018-10-08 15:59:02 +0200461 if (!accessCheck(AccessPtrEvents)) return;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000462 if (!rfb::Server::acceptPointerEvents) return;
Pierre Ossmanb6843412018-10-05 17:30:52 +0200463 pointerEventPos = pos;
464 server->pointerEvent(this, pointerEventPos, buttonMask);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000465}
466
467
468class VNCSConnectionSTShiftPresser {
469public:
Pierre Ossmanb6843412018-10-05 17:30:52 +0200470 VNCSConnectionSTShiftPresser(VNCServerST* server_)
471 : server(server_), pressed(false) {}
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000472 ~VNCSConnectionSTShiftPresser() {
Pierre Ossman9a153b02015-08-31 10:01:14 +0200473 if (pressed) {
474 vlog.debug("Releasing fake Shift_L");
Pierre Ossmanb6843412018-10-05 17:30:52 +0200475 server->keyEvent(XK_Shift_L, 0, false);
Pierre Ossman9a153b02015-08-31 10:01:14 +0200476 }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000477 }
478 void press() {
Pierre Ossman9a153b02015-08-31 10:01:14 +0200479 vlog.debug("Pressing fake Shift_L");
Pierre Ossmanb6843412018-10-05 17:30:52 +0200480 server->keyEvent(XK_Shift_L, 0, true);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000481 pressed = true;
482 }
Pierre Ossmanb6843412018-10-05 17:30:52 +0200483 VNCServerST* server;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000484 bool pressed;
485};
486
487// keyEvent() - record in the pressedKeys which keys were pressed. Allow
488// multiple down events (for autorepeat), but only allow a single up event.
Pierre Ossman5ae28212017-05-16 14:30:38 +0200489void VNCSConnectionST::keyEvent(rdr::U32 keysym, rdr::U32 keycode, bool down) {
Pierre Ossman16e1dcb2017-05-16 14:33:43 +0200490 rdr::U32 lookup;
491
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000492 lastEventTime = time(0);
Pierre Ossman7d64b332018-10-08 15:59:02 +0200493 if (!accessCheck(AccessKeyEvents)) return;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000494 if (!rfb::Server::acceptKeyEvents) return;
495
Pierre Ossman9a153b02015-08-31 10:01:14 +0200496 if (down)
Pierre Ossman5ae28212017-05-16 14:30:38 +0200497 vlog.debug("Key pressed: 0x%x / 0x%x", keysym, keycode);
Pierre Ossman9a153b02015-08-31 10:01:14 +0200498 else
Pierre Ossman5ae28212017-05-16 14:30:38 +0200499 vlog.debug("Key released: 0x%x / 0x%x", keysym, keycode);
Pierre Ossman9a153b02015-08-31 10:01:14 +0200500
Pierre Ossmanbb305ca2016-12-11 12:41:26 +0100501 // Avoid lock keys if we don't know the server state
Pierre Ossman6094ced2018-10-05 17:24:51 +0200502 if ((server->getLEDState() == ledUnknown) &&
Pierre Ossman5ae28212017-05-16 14:30:38 +0200503 ((keysym == XK_Caps_Lock) ||
504 (keysym == XK_Num_Lock) ||
505 (keysym == XK_Scroll_Lock))) {
Pierre Ossmanbb305ca2016-12-11 12:41:26 +0100506 vlog.debug("Ignoring lock key (e.g. caps lock)");
507 return;
508 }
Pierre Ossmanbb305ca2016-12-11 12:41:26 +0100509
Pierre Ossmanb45a84f2016-12-12 16:59:15 +0100510 // Lock key heuristics
511 // (only for clients that do not support the LED state extension)
512 if (!cp.supportsLEDState) {
513 // Always ignore ScrollLock as we don't have a heuristic
514 // for that
Pierre Ossman5ae28212017-05-16 14:30:38 +0200515 if (keysym == XK_Scroll_Lock) {
Pierre Ossmanb45a84f2016-12-12 16:59:15 +0100516 vlog.debug("Ignoring lock key (e.g. caps lock)");
517 return;
Pierre Ossmanbb305ca2016-12-11 12:41:26 +0100518 }
519
Pierre Ossman6094ced2018-10-05 17:24:51 +0200520 if (down && (server->getLEDState() != ledUnknown)) {
Pierre Ossmanb45a84f2016-12-12 16:59:15 +0100521 // CapsLock synchronisation heuristic
522 // (this assumes standard interaction between CapsLock the Shift
523 // keys and normal characters)
Pierre Ossman5ae28212017-05-16 14:30:38 +0200524 if (((keysym >= XK_A) && (keysym <= XK_Z)) ||
525 ((keysym >= XK_a) && (keysym <= XK_z))) {
Pierre Ossmanb45a84f2016-12-12 16:59:15 +0100526 bool uppercase, shift, lock;
Pierre Ossmanbb305ca2016-12-11 12:41:26 +0100527
Pierre Ossman5ae28212017-05-16 14:30:38 +0200528 uppercase = (keysym >= XK_A) && (keysym <= XK_Z);
Pierre Ossman851e6802017-09-12 16:44:44 +0200529 shift = isShiftPressed();
Pierre Ossman6094ced2018-10-05 17:24:51 +0200530 lock = server->getLEDState() & ledCapsLock;
Pierre Ossmanbb305ca2016-12-11 12:41:26 +0100531
Pierre Ossmanb45a84f2016-12-12 16:59:15 +0100532 if (lock == (uppercase == shift)) {
533 vlog.debug("Inserting fake CapsLock to get in sync with client");
Pierre Ossmanb6843412018-10-05 17:30:52 +0200534 server->keyEvent(XK_Caps_Lock, 0, true);
535 server->keyEvent(XK_Caps_Lock, 0, false);
Pierre Ossmanb45a84f2016-12-12 16:59:15 +0100536 }
537 }
538
539 // NumLock synchronisation heuristic
540 // (this is more cautious because of the differences between Unix,
541 // Windows and macOS)
Pierre Ossman5ae28212017-05-16 14:30:38 +0200542 if (((keysym >= XK_KP_Home) && (keysym <= XK_KP_Delete)) ||
543 ((keysym >= XK_KP_0) && (keysym <= XK_KP_9)) ||
544 (keysym == XK_KP_Separator) || (keysym == XK_KP_Decimal)) {
Pierre Ossmanb45a84f2016-12-12 16:59:15 +0100545 bool number, shift, lock;
546
Pierre Ossman5ae28212017-05-16 14:30:38 +0200547 number = ((keysym >= XK_KP_0) && (keysym <= XK_KP_9)) ||
548 (keysym == XK_KP_Separator) || (keysym == XK_KP_Decimal);
Pierre Ossman851e6802017-09-12 16:44:44 +0200549 shift = isShiftPressed();
Pierre Ossman6094ced2018-10-05 17:24:51 +0200550 lock = server->getLEDState() & ledNumLock;
Pierre Ossmanb45a84f2016-12-12 16:59:15 +0100551
552 if (shift) {
553 // We don't know the appropriate NumLock state for when Shift
554 // is pressed as it could be one of:
555 //
556 // a) A Unix client where Shift negates NumLock
557 //
558 // b) A Windows client where Shift only cancels NumLock
559 //
560 // c) A macOS client where Shift doesn't have any effect
561 //
562 } else if (lock == (number == shift)) {
563 vlog.debug("Inserting fake NumLock to get in sync with client");
Pierre Ossmanb6843412018-10-05 17:30:52 +0200564 server->keyEvent(XK_Num_Lock, 0, true);
565 server->keyEvent(XK_Num_Lock, 0, false);
Pierre Ossmanb45a84f2016-12-12 16:59:15 +0100566 }
Pierre Ossmanbb305ca2016-12-11 12:41:26 +0100567 }
Pierre Ossman9a153b02015-08-31 10:01:14 +0200568 }
569 }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000570
571 // Turn ISO_Left_Tab into shifted Tab.
Pierre Ossmanb6843412018-10-05 17:30:52 +0200572 VNCSConnectionSTShiftPresser shiftPresser(server);
Pierre Ossman5ae28212017-05-16 14:30:38 +0200573 if (keysym == XK_ISO_Left_Tab) {
Pierre Ossman851e6802017-09-12 16:44:44 +0200574 if (!isShiftPressed())
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000575 shiftPresser.press();
Pierre Ossman5ae28212017-05-16 14:30:38 +0200576 keysym = XK_Tab;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000577 }
578
Pierre Ossman16e1dcb2017-05-16 14:33:43 +0200579 // We need to be able to track keys, so generate a fake index when we
580 // aren't given a keycode
581 if (keycode == 0)
582 lookup = 0x80000000 | keysym;
583 else
584 lookup = keycode;
585
586 // We force the same keysym for an already down key for the
587 // sake of sanity
588 if (pressedKeys.find(lookup) != pressedKeys.end())
589 keysym = pressedKeys[lookup];
590
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000591 if (down) {
Pierre Ossman16e1dcb2017-05-16 14:33:43 +0200592 pressedKeys[lookup] = keysym;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000593 } else {
Pierre Ossman16e1dcb2017-05-16 14:33:43 +0200594 if (!pressedKeys.erase(lookup))
Pierre Ossman5ae28212017-05-16 14:30:38 +0200595 return;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000596 }
Pierre Ossman16e1dcb2017-05-16 14:33:43 +0200597
Pierre Ossmanb6843412018-10-05 17:30:52 +0200598 server->keyEvent(keysym, keycode, down);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000599}
600
601void VNCSConnectionST::clientCutText(const char* str, int len)
602{
Pierre Ossman7d64b332018-10-08 15:59:02 +0200603 if (!accessCheck(AccessCutText)) return;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000604 if (!rfb::Server::acceptCutText) return;
Pierre Ossmanb6843412018-10-05 17:30:52 +0200605 server->clientCutText(str, len);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000606}
607
608void VNCSConnectionST::framebufferUpdateRequest(const Rect& r,bool incremental)
609{
Pierre Ossmane9962f72009-04-23 12:31:42 +0000610 Rect safeRect;
611
Pierre Ossman7d64b332018-10-08 15:59:02 +0200612 if (!accessCheck(AccessView)) return;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000613
614 SConnection::framebufferUpdateRequest(r, incremental);
615
Pierre Ossmand9a59ba2009-03-20 15:55:37 +0000616 // Check that the client isn't sending crappy requests
617 if (!r.enclosed_by(Rect(0, 0, cp.width, cp.height))) {
618 vlog.error("FramebufferUpdateRequest %dx%d at %d,%d exceeds framebuffer %dx%d",
619 r.width(), r.height(), r.tl.x, r.tl.y, cp.width, cp.height);
Pierre Ossmane9962f72009-04-23 12:31:42 +0000620 safeRect = r.intersect(Rect(0, 0, cp.width, cp.height));
621 } else {
622 safeRect = r;
Pierre Ossmand9a59ba2009-03-20 15:55:37 +0000623 }
624
Constantin Kaplinsky2ef66952008-08-29 11:33:46 +0000625 // Just update the requested region.
626 // Framebuffer update will be sent a bit later, see processMessages().
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000627 Region reqRgn(r);
Pierre Ossman1b478e52011-11-15 12:08:30 +0000628 if (!incremental || !continuousUpdates)
629 requested.assign_union(reqRgn);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000630
631 if (!incremental) {
632 // Non-incremental update - treat as if area requested has changed
633 updates.add_changed(reqRgn);
Pierre Ossman53125a72009-04-22 15:37:51 +0000634
635 // And send the screen layout to the client (which, unlike the
636 // framebuffer dimensions, the client doesn't get during init)
Pierre Ossmanc5e25602009-03-20 12:59:05 +0000637 writer()->writeExtendedDesktopSize();
Pierre Ossman53125a72009-04-22 15:37:51 +0000638
639 // We do not send a DesktopSize since it only contains the
640 // framebuffer size (which the client already should know) and
641 // because some clients don't handle extra DesktopSize events
642 // very well.
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000643 }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000644}
645
Pierre Ossman34bb0612009-03-21 21:16:14 +0000646void VNCSConnectionST::setDesktopSize(int fb_width, int fb_height,
647 const ScreenSet& layout)
Pierre Ossmanc5e25602009-03-20 12:59:05 +0000648{
Pierre Ossman04e62db2009-03-23 16:57:07 +0000649 unsigned int result;
650
Pierre Ossman7d64b332018-10-08 15:59:02 +0200651 if (!accessCheck(AccessSetDesktopSize)) return;
Michal Srbb318b8f2014-11-24 13:18:28 +0200652 if (!rfb::Server::acceptSetDesktopSize) return;
653
Pierre Ossman07e44cc2018-10-05 17:32:57 +0200654 result = server->setDesktopSize(this, fb_width, fb_height, layout);
Pierre Ossman04e62db2009-03-23 16:57:07 +0000655 writer()->writeExtendedDesktopSize(reasonClient, result,
656 fb_width, fb_height, layout);
Pierre Ossmanc5e25602009-03-20 12:59:05 +0000657}
658
Pierre Ossman2c764942011-11-14 15:54:30 +0000659void VNCSConnectionST::fence(rdr::U32 flags, unsigned len, const char data[])
660{
Pierre Ossmanb2a417c2015-12-11 19:32:21 +0100661 rdr::U8 type;
662
Pierre Ossman2c764942011-11-14 15:54:30 +0000663 if (flags & fenceFlagRequest) {
664 if (flags & fenceFlagSyncNext) {
Pierre Ossmanb8b1e962012-07-20 10:47:00 +0000665 pendingSyncFence = true;
Pierre Ossman2c764942011-11-14 15:54:30 +0000666
667 fenceFlags = flags & (fenceFlagBlockBefore | fenceFlagBlockAfter | fenceFlagSyncNext);
668 fenceDataLen = len;
669 delete [] fenceData;
Michal Srbf3afa242017-03-27 19:02:15 +0300670 fenceData = NULL;
Pierre Ossman2c764942011-11-14 15:54:30 +0000671 if (len > 0) {
672 fenceData = new char[len];
673 memcpy(fenceData, data, len);
674 }
675
676 return;
677 }
678
679 // We handle everything synchronously so we trivially honor these modes
680 flags = flags & (fenceFlagBlockBefore | fenceFlagBlockAfter);
681
682 writer()->writeFence(flags, len, data);
683 return;
684 }
685
Pierre Ossmanb2a417c2015-12-11 19:32:21 +0100686 if (len < 1)
687 vlog.error("Fence response of unexpected size received");
Pierre Ossman1b478e52011-11-15 12:08:30 +0000688
Pierre Ossmanb2a417c2015-12-11 19:32:21 +0100689 type = data[0];
690
691 switch (type) {
Pierre Ossman2c764942011-11-14 15:54:30 +0000692 case 0:
693 // Initial dummy fence;
694 break;
Pierre Ossmanb2a417c2015-12-11 19:32:21 +0100695 case 1:
Pierre Ossmanc09e5582015-12-11 20:23:17 +0100696 congestion.gotPong();
Pierre Ossman1b478e52011-11-15 12:08:30 +0000697 break;
Pierre Ossman2c764942011-11-14 15:54:30 +0000698 default:
Pierre Ossmanb2a417c2015-12-11 19:32:21 +0100699 vlog.error("Fence response of unexpected type received");
Pierre Ossman2c764942011-11-14 15:54:30 +0000700 }
701}
702
Pierre Ossman1b478e52011-11-15 12:08:30 +0000703void VNCSConnectionST::enableContinuousUpdates(bool enable,
704 int x, int y, int w, int h)
705{
706 Rect rect;
707
708 if (!cp.supportsFence || !cp.supportsContinuousUpdates)
709 throw Exception("Client tried to enable continuous updates when not allowed");
710
711 continuousUpdates = enable;
712
713 rect.setXYWH(x, y, w, h);
714 cuRegion.reset(rect);
715
716 if (enable) {
717 requested.clear();
Pierre Ossman1b478e52011-11-15 12:08:30 +0000718 } else {
719 writer()->writeEndOfContinuousUpdates();
720 }
721}
722
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000723// supportsLocalCursor() is called whenever the status of
724// cp.supportsLocalCursor has changed. If the client does now support local
725// cursor, we make sure that the old server-side rendered cursor is cleaned up
726// and the cursor is sent to the client.
727
728void VNCSConnectionST::supportsLocalCursor()
729{
Pierre Ossman387a4172017-11-16 16:44:36 +0100730 bool hasRenderedCursor = !damagedCursorRegion.is_empty();
731 if (hasRenderedCursor && !needRenderedCursor())
732 removeRenderedCursor = true;
733 setCursor();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000734}
735
Pierre Ossman2c764942011-11-14 15:54:30 +0000736void VNCSConnectionST::supportsFence()
737{
Pierre Ossmanb2a417c2015-12-11 19:32:21 +0100738 char type = 0;
739 writer()->writeFence(fenceFlagRequest, sizeof(type), &type);
Pierre Ossman2c764942011-11-14 15:54:30 +0000740}
741
Pierre Ossman1b478e52011-11-15 12:08:30 +0000742void VNCSConnectionST::supportsContinuousUpdates()
743{
744 // We refuse to use continuous updates if we cannot monitor the buffer
745 // usage using fences.
746 if (!cp.supportsFence)
747 return;
748
749 writer()->writeEndOfContinuousUpdates();
750}
751
Pierre Ossmanb45a84f2016-12-12 16:59:15 +0100752void VNCSConnectionST::supportsLEDState()
753{
754 writer()->writeLEDState();
755}
756
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000757
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +0000758bool VNCSConnectionST::handleTimeout(Timer* t)
759{
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000760 try {
Peter Åstrand (astrand)7a368c92018-09-19 12:45:17 +0200761 if ((t == &congestionTimer) ||
762 (t == &losslessTimer))
Pierre Ossmana99d14d2015-12-13 15:43:46 +0100763 writeFramebufferUpdate();
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000764 } catch (rdr::Exception& e) {
765 close(e.str());
766 }
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +0000767
768 return false;
769}
770
Pierre Ossman851e6802017-09-12 16:44:44 +0200771bool VNCSConnectionST::isShiftPressed()
772{
773 std::map<rdr::U32, rdr::U32>::const_iterator iter;
774
775 for (iter = pressedKeys.begin(); iter != pressedKeys.end(); ++iter) {
776 if (iter->second == XK_Shift_L)
777 return true;
778 if (iter->second == XK_Shift_R)
779 return true;
780 }
781
782 return false;
783}
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +0000784
Pierre Ossman1b478e52011-11-15 12:08:30 +0000785void VNCSConnectionST::writeRTTPing()
786{
Pierre Ossmanb2a417c2015-12-11 19:32:21 +0100787 char type;
Pierre Ossman1b478e52011-11-15 12:08:30 +0000788
789 if (!cp.supportsFence)
790 return;
791
Pierre Ossmana99d14d2015-12-13 15:43:46 +0100792 congestion.updatePosition(sock->outStream().length());
Pierre Ossman1b478e52011-11-15 12:08:30 +0000793
794 // We need to make sure any old update are already processed by the
795 // time we get the response back. This allows us to reliably throttle
796 // back on client overload, as well as network overload.
Pierre Ossmanb2a417c2015-12-11 19:32:21 +0100797 type = 1;
Pierre Ossman1b478e52011-11-15 12:08:30 +0000798 writer()->writeFence(fenceFlagRequest | fenceFlagBlockBefore,
Pierre Ossmanb2a417c2015-12-11 19:32:21 +0100799 sizeof(type), &type);
Pierre Ossman1b478e52011-11-15 12:08:30 +0000800
Pierre Ossmana99d14d2015-12-13 15:43:46 +0100801 congestion.sentPing();
Pierre Ossman1b478e52011-11-15 12:08:30 +0000802}
803
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +0000804bool VNCSConnectionST::isCongested()
805{
Pierre Ossmance261812018-07-17 15:01:53 +0200806 int eta;
Pierre Ossmana99d14d2015-12-13 15:43:46 +0100807
808 congestionTimer.stop();
Pierre Ossman1b478e52011-11-15 12:08:30 +0000809
810 // Stuff still waiting in the send buffer?
Pierre Ossman352d0622016-04-29 15:50:54 +0200811 sock->outStream().flush();
Pierre Ossman8cf71632016-03-11 09:38:08 +0100812 congestion.debugTrace("congestion-trace.csv", sock->getFd());
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +0000813 if (sock->outStream().bufferUsage() > 0)
814 return true;
815
Pierre Ossman1b478e52011-11-15 12:08:30 +0000816 if (!cp.supportsFence)
817 return false;
818
Pierre Ossmana99d14d2015-12-13 15:43:46 +0100819 congestion.updatePosition(sock->outStream().length());
820 if (!congestion.isCongested())
Pierre Ossman1b478e52011-11-15 12:08:30 +0000821 return false;
822
Pierre Ossmana99d14d2015-12-13 15:43:46 +0100823 eta = congestion.getUncongestedETA();
824 if (eta >= 0)
825 congestionTimer.start(eta);
Pierre Ossman1b478e52011-11-15 12:08:30 +0000826
827 return true;
828}
829
830
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000831void VNCSConnectionST::writeFramebufferUpdate()
832{
Pierre Ossmana99d14d2015-12-13 15:43:46 +0100833 congestion.updatePosition(sock->outStream().length());
834
Pierre Ossman2c764942011-11-14 15:54:30 +0000835 // We're in the middle of processing a command that's supposed to be
836 // synchronised. Allowing an update to slip out right now might violate
837 // that synchronisation.
838 if (syncFence)
839 return;
840
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000841 // We try to aggregate responses, so don't send out anything whilst we
842 // still have incoming messages. processMessages() will give us another
843 // chance to run once things are idle.
844 if (inProcessMessages)
845 return;
846
Pierre Ossman1b478e52011-11-15 12:08:30 +0000847 if (state() != RFBSTATE_NORMAL)
848 return;
849 if (requested.is_empty() && !continuousUpdates)
Pierre Ossmane9962f72009-04-23 12:31:42 +0000850 return;
Pierre Ossmand9a59ba2009-03-20 15:55:37 +0000851
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +0000852 // Check that we actually have some space on the link and retry in a
853 // bit if things are congested.
Pierre Ossmana40ab202016-04-29 15:35:56 +0200854 if (isCongested())
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +0000855 return;
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +0000856
Pierre Ossmancef3cf72016-11-25 10:06:34 +0100857 // Updates often consists of many small writes, and in continuous
858 // mode, we will also have small fence messages around the update. We
859 // need to aggregate these in order to not clog up TCP's congestion
860 // window.
Peter Åstrand (astrand)01dc1a62017-10-10 12:56:04 +0200861 sock->cork(true);
Pierre Ossman36dadf82011-11-15 12:11:32 +0000862
Pierre Ossmane9962f72009-04-23 12:31:42 +0000863 // First take care of any updates that cannot contain framebuffer data
864 // changes.
Pierre Ossmancef3cf72016-11-25 10:06:34 +0100865 writeNoDataUpdate();
866
867 // Then real data (if possible)
868 writeDataUpdate();
869
Peter Åstrand (astrand)01dc1a62017-10-10 12:56:04 +0200870 sock->cork(false);
Pierre Ossmana99d14d2015-12-13 15:43:46 +0100871
872 congestion.updatePosition(sock->outStream().length());
Pierre Ossmancef3cf72016-11-25 10:06:34 +0100873}
874
875void VNCSConnectionST::writeNoDataUpdate()
876{
877 if (!writer()->needNoDataUpdate())
878 return;
879
880 writer()->writeNoDataUpdate();
881
882 // Make sure no data update is sent until next request
883 requested.clear();
884}
885
886void VNCSConnectionST::writeDataUpdate()
887{
Pierre Ossman8efc7b42018-03-23 11:45:51 +0100888 Region req, pending;
Pierre Ossmancef3cf72016-11-25 10:06:34 +0100889 UpdateInfo ui;
890 bool needNewUpdateInfo;
Pierre Ossman24684e52016-12-05 16:58:19 +0100891 const RenderedCursor *cursor;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000892
Constantin Kaplinsky604d7812007-08-31 15:50:37 +0000893 updates.enable_copyrect(cp.useCopyRect);
894
Pierre Ossmancef3cf72016-11-25 10:06:34 +0100895 // See what the client has requested (if anything)
Pierre Ossman1b478e52011-11-15 12:08:30 +0000896 if (continuousUpdates)
897 req = cuRegion.union_(requested);
898 else
899 req = requested;
900
Pierre Ossmancef3cf72016-11-25 10:06:34 +0100901 if (req.is_empty())
902 return;
903
Pierre Ossman8efc7b42018-03-23 11:45:51 +0100904 // Get any framebuffer changes we haven't yet been informed of
905 pending = server->getPendingRegion();
906
Pierre Ossmancef3cf72016-11-25 10:06:34 +0100907 // Get the lists of updates. Prior to exporting the data to the `ui' object,
908 // getUpdateInfo() will normalize the `updates' object such way that its
909 // `changed' and `copied' regions would not intersect.
Pierre Ossman1b478e52011-11-15 12:08:30 +0000910 updates.getUpdateInfo(&ui, req);
911 needNewUpdateInfo = false;
Constantin Kaplinsky604d7812007-08-31 15:50:37 +0000912
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000913 // If the previous position of the rendered cursor overlaps the source of the
914 // copy, then when the copy happens the corresponding rectangle in the
915 // destination will be wrong, so add it to the changed region.
916
Pierre Ossman671d2ef2015-06-09 16:09:06 +0200917 if (!ui.copied.is_empty() && !damagedCursorRegion.is_empty()) {
918 Region bogusCopiedCursor;
919
Pierre Ossman74385d32018-03-22 16:00:18 +0100920 bogusCopiedCursor = damagedCursorRegion;
Pierre Ossman671d2ef2015-06-09 16:09:06 +0200921 bogusCopiedCursor.translate(ui.copy_delta);
Pierre Ossman6094ced2018-10-05 17:24:51 +0200922 bogusCopiedCursor.assign_intersect(server->getPixelBuffer()->getRect());
Constantin Kaplinsky45517c82007-08-31 15:56:33 +0000923 if (!ui.copied.intersect(bogusCopiedCursor).is_empty()) {
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000924 updates.add_changed(bogusCopiedCursor);
Constantin Kaplinsky604d7812007-08-31 15:50:37 +0000925 needNewUpdateInfo = true;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000926 }
927 }
928
Pierre Ossman671d2ef2015-06-09 16:09:06 +0200929 // If we need to remove the old rendered cursor, just add the region to
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000930 // the changed region.
931
932 if (removeRenderedCursor) {
Pierre Ossman671d2ef2015-06-09 16:09:06 +0200933 updates.add_changed(damagedCursorRegion);
Constantin Kaplinsky604d7812007-08-31 15:50:37 +0000934 needNewUpdateInfo = true;
Pierre Ossman671d2ef2015-06-09 16:09:06 +0200935 damagedCursorRegion.clear();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000936 removeRenderedCursor = false;
937 }
938
Pierre Ossman8c3bd692018-03-22 15:58:54 +0100939 // If we need a full cursor update then make sure its entire region
940 // is marked as changed.
941
942 if (updateRenderedCursor) {
943 updates.add_changed(server->getRenderedCursor()->getEffectiveRect());
944 needNewUpdateInfo = true;
945 updateRenderedCursor = false;
946 }
947
Constantin Kaplinsky604d7812007-08-31 15:50:37 +0000948 // The `updates' object could change, make sure we have valid update info.
949
950 if (needNewUpdateInfo)
Pierre Ossman1b478e52011-11-15 12:08:30 +0000951 updates.getUpdateInfo(&ui, req);
Constantin Kaplinsky604d7812007-08-31 15:50:37 +0000952
Pierre Ossman8efc7b42018-03-23 11:45:51 +0100953 // If there are queued updates then we cannot safely send an update
954 // without risking a partially updated screen
955
956 if (!pending.is_empty()) {
957 // However we might still be able to send a lossless refresh
958 req.assign_subtract(pending);
959 req.assign_subtract(ui.changed);
960 req.assign_subtract(ui.copied);
961
962 ui.changed.clear();
963 ui.copied.clear();
964 }
965
Pierre Ossman8c3bd692018-03-22 15:58:54 +0100966 // Does the client need a server-side rendered cursor?
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000967
Pierre Ossman24684e52016-12-05 16:58:19 +0100968 cursor = NULL;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000969 if (needRenderedCursor()) {
Pierre Ossman671d2ef2015-06-09 16:09:06 +0200970 Rect renderedCursorRect;
971
Pierre Ossman24684e52016-12-05 16:58:19 +0100972 cursor = server->getRenderedCursor();
Pierre Ossman8c3bd692018-03-22 15:58:54 +0100973 renderedCursorRect = cursor->getEffectiveRect();
Pierre Ossman24684e52016-12-05 16:58:19 +0100974
Pierre Ossman8c3bd692018-03-22 15:58:54 +0100975 // Check that we don't try to copy over the cursor area, and
976 // if that happens we need to treat it as changed so that we can
977 // re-render it
978 if (!ui.copied.intersect(renderedCursorRect).is_empty()) {
979 ui.changed.assign_union(ui.copied.intersect(renderedCursorRect));
980 ui.copied.assign_subtract(renderedCursorRect);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000981 }
982
Pierre Ossman8c3bd692018-03-22 15:58:54 +0100983 // Track where we've rendered the cursor
984 damagedCursorRegion.assign_union(ui.changed.intersect(renderedCursorRect));
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000985 }
986
Pierre Ossman8efc7b42018-03-23 11:45:51 +0100987 // Return if there is nothing to send the client.
Peter Åstrand (astrand)7a368c92018-09-19 12:45:17 +0200988 if (ui.is_empty() && !writer()->needFakeUpdate()) {
989 int eta;
Pierre Ossman8efc7b42018-03-23 11:45:51 +0100990
Peter Åstrand (astrand)7a368c92018-09-19 12:45:17 +0200991 // Any lossless refresh that needs handling?
992 if (!encodeManager.needsLosslessRefresh(req))
993 return;
994
995 // Now? Or later?
996 eta = encodeManager.getNextLosslessRefresh(req);
997 if (eta > 0) {
998 losslessTimer.start(eta);
999 return;
1000 }
1001 }
Pierre Ossmanfdba3fe2014-01-31 13:12:18 +01001002
Pierre Ossmancef3cf72016-11-25 10:06:34 +01001003 writeRTTPing();
Pierre Ossman1b478e52011-11-15 12:08:30 +00001004
Pierre Ossman6b2f1132016-11-30 08:03:35 +01001005 if (!ui.is_empty())
1006 encodeManager.writeUpdate(ui, server->getPixelBuffer(), cursor);
Pierre Ossmana2b80d62018-03-23 09:30:09 +01001007 else {
Pierre Ossmanc3ce5ce2018-09-19 16:31:18 +02001008 int nextUpdate;
Pierre Ossmana2b80d62018-03-23 09:30:09 +01001009
1010 // FIXME: If continuous updates aren't used then the client might
1011 // be slower than frameRate in its requests and we could
1012 // afford a larger update size
Pierre Ossmanc3ce5ce2018-09-19 16:31:18 +02001013 nextUpdate = server->msToNextUpdate();
1014 if (nextUpdate > 0) {
Pierre Ossmancb5a3992018-09-19 16:35:40 +02001015 size_t bandwidth, maxUpdateSize;
Pierre Ossmana2b80d62018-03-23 09:30:09 +01001016
Pierre Ossmanc3ce5ce2018-09-19 16:31:18 +02001017 // FIXME: Bandwidth estimation without congestion control
Pierre Ossmancb5a3992018-09-19 16:35:40 +02001018 bandwidth = congestion.getBandwidth();
Pierre Ossmana2b80d62018-03-23 09:30:09 +01001019
Pierre Ossmancb5a3992018-09-19 16:35:40 +02001020 // FIXME: Hard coded value for maximum CPU throughput
1021 if (bandwidth > 5000000)
1022 bandwidth = 5000000;
1023
1024 maxUpdateSize = bandwidth * nextUpdate / 1000;
Pierre Ossmanc3ce5ce2018-09-19 16:31:18 +02001025 encodeManager.writeLosslessRefresh(req, server->getPixelBuffer(),
1026 cursor, maxUpdateSize);
1027 }
Pierre Ossmana2b80d62018-03-23 09:30:09 +01001028 }
Pierre Ossman1b478e52011-11-15 12:08:30 +00001029
Pierre Ossmancef3cf72016-11-25 10:06:34 +01001030 writeRTTPing();
Pierre Ossmanc0397262014-03-14 15:59:46 +01001031
Pierre Ossmancef3cf72016-11-25 10:06:34 +01001032 // The request might be for just part of the screen, so we cannot
1033 // just clear the entire update tracker.
1034 updates.subtract(req);
Pierre Ossmanc0397262014-03-14 15:59:46 +01001035
Pierre Ossmancef3cf72016-11-25 10:06:34 +01001036 requested.clear();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001037}
1038
1039
Pierre Ossmana3ac01e2011-11-07 21:13:54 +00001040void VNCSConnectionST::screenLayoutChange(rdr::U16 reason)
1041{
1042 if (!authenticated())
1043 return;
1044
Pierre Ossman6094ced2018-10-05 17:24:51 +02001045 cp.screenLayout = server->getScreenLayout();
Pierre Ossmana3ac01e2011-11-07 21:13:54 +00001046
1047 if (state() != RFBSTATE_NORMAL)
1048 return;
1049
1050 writer()->writeExtendedDesktopSize(reason, 0, cp.width, cp.height,
1051 cp.screenLayout);
Pierre Ossmana3ac01e2011-11-07 21:13:54 +00001052}
1053
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001054
1055// setCursor() is called whenever the cursor has changed shape or pixel format.
1056// If the client supports local cursor then it will arrange for the cursor to
1057// be sent to the client.
1058
1059void VNCSConnectionST::setCursor()
1060{
Pierre Ossmana3ac01e2011-11-07 21:13:54 +00001061 if (state() != RFBSTATE_NORMAL)
1062 return;
Pierre Ossmana3ac01e2011-11-07 21:13:54 +00001063
Pierre Ossman71ca8d52017-09-15 11:03:12 +02001064 // We need to blank out the client's cursor or there will be two
Pierre Ossman25db44a2017-11-16 16:40:44 +01001065 if (needRenderedCursor()) {
Pierre Ossman71ca8d52017-09-15 11:03:12 +02001066 cp.setCursor(emptyCursor);
Pierre Ossman25db44a2017-11-16 16:40:44 +01001067 clientHasCursor = false;
1068 } else {
Pierre Ossman6094ced2018-10-05 17:24:51 +02001069 cp.setCursor(*server->getCursor());
Pierre Ossman25db44a2017-11-16 16:40:44 +01001070 clientHasCursor = true;
1071 }
Pierre Ossman126e5642014-02-13 14:40:25 +01001072
Pierre Ossman8053c8e2017-02-21 12:59:04 +01001073 if (!writer()->writeSetCursorWithAlpha()) {
1074 if (!writer()->writeSetCursor()) {
1075 if (!writer()->writeSetXCursor()) {
1076 // No client support
1077 return;
1078 }
Pierre Ossman126e5642014-02-13 14:40:25 +01001079 }
1080 }
Pierre Ossmana3ac01e2011-11-07 21:13:54 +00001081}
1082
1083void VNCSConnectionST::setDesktopName(const char *name)
1084{
1085 cp.setName(name);
1086
1087 if (state() != RFBSTATE_NORMAL)
1088 return;
1089
1090 if (!writer()->writeSetDesktopName()) {
1091 fprintf(stderr, "Client does not support desktop rename\n");
1092 return;
1093 }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001094}
1095
Pierre Ossmanb45a84f2016-12-12 16:59:15 +01001096void VNCSConnectionST::setLEDState(unsigned int ledstate)
1097{
1098 if (state() != RFBSTATE_NORMAL)
1099 return;
1100
1101 cp.setLEDState(ledstate);
1102
Pierre Ossmanb218ecd2017-11-16 16:43:13 +01001103 writer()->writeLEDState();
Pierre Ossmanb45a84f2016-12-12 16:59:15 +01001104}
1105
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001106void VNCSConnectionST::setSocketTimeouts()
1107{
1108 int timeoutms = rfb::Server::clientWaitTimeMillis;
1109 soonestTimeout(&timeoutms, secsToMillis(rfb::Server::idleTimeout));
1110 if (timeoutms == 0)
1111 timeoutms = -1;
1112 sock->inStream().setTimeout(timeoutms);
1113 sock->outStream().setTimeout(timeoutms);
1114}