blob: cdd87b135db4ad46047102dc0866861961634b46 [file] [log] [blame]
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001/* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved.
Pierre Ossman615d16b2019-05-03 10:53:06 +02002 * Copyright 2009-2019 Pierre Ossman for Cendio AB
Peter Åstrand (astrand)7a368c92018-09-19 12:45:17 +02003 * Copyright 2018 Peter Astrand for Cendio AB
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00004 *
5 * This is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This software is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this software; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
18 * USA.
19 */
20
Pierre Ossmana830bec2011-11-08 12:12:02 +000021#include <network/TcpSocket.h>
Pierre Ossman707fa122015-12-11 20:21:20 +010022
23#include <rfb/ComparingUpdateTracker.h>
24#include <rfb/Encoder.h>
25#include <rfb/KeyRemapper.h>
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000026#include <rfb/LogWriter.h>
Adam Tkac5a0caed2010-04-23 13:58:10 +000027#include <rfb/Security.h>
Pierre Ossman707fa122015-12-11 20:21:20 +010028#include <rfb/ServerCore.h>
29#include <rfb/SMsgWriter.h>
30#include <rfb/VNCServerST.h>
31#include <rfb/VNCSConnectionST.h>
Pierre Ossmanc5e25602009-03-20 12:59:05 +000032#include <rfb/screenTypes.h>
Pierre Ossman2c764942011-11-14 15:54:30 +000033#include <rfb/fenceTypes.h>
Pierre Ossmanbb305ca2016-12-11 12:41:26 +010034#include <rfb/ledStates.h>
Pierre Ossmanbb305ca2016-12-11 12:41:26 +010035#define XK_LATIN1
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000036#define XK_MISCELLANY
37#define XK_XKB_KEYS
38#include <rfb/keysymdef.h>
39
40using namespace rfb;
41
42static LogWriter vlog("VNCSConnST");
43
Pierre Ossman71ca8d52017-09-15 11:03:12 +020044static Cursor emptyCursor(0, 0, Point(0, 0), NULL);
45
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000046VNCSConnectionST::VNCSConnectionST(VNCServerST* server_, network::Socket *s,
47 bool reverse)
Pierre Ossman7069bdd2015-02-06 14:41:58 +010048 : sock(s), reverseConnection(reverse),
Pierre Ossman36304752017-10-04 16:21:57 +020049 inProcessMessages(false),
Pierre Ossmanb8b1e962012-07-20 10:47:00 +000050 pendingSyncFence(false), syncFence(false), fenceFlags(0),
Pierre Ossmana99d14d2015-12-13 15:43:46 +010051 fenceDataLen(0), fenceData(NULL), congestionTimer(this),
Pierre Ossmana114c342018-06-18 16:29:55 +020052 losslessTimer(this), server(server_),
Pierre Ossman671d2ef2015-06-09 16:09:06 +020053 updateRenderedCursor(false), removeRenderedCursor(false),
Pierre Ossman65e08fd2018-10-26 15:54:00 +020054 continuousUpdates(false), encodeManager(this), idleTimer(this),
Pierre Ossman88a94ed2019-04-01 14:22:01 +020055 pointerEventTime(0), clientHasCursor(false),
56 authFailureTimer(this)
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000057{
58 setStreams(&sock->inStream(), &sock->outStream());
59 peerEndpoint.buf = sock->getPeerEndpoint();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000060
61 // Configure the socket
62 setSocketTimeouts();
Pierre Ossman65e08fd2018-10-26 15:54:00 +020063
64 // Kick off the idle timer
65 if (rfb::Server::idleTimeout) {
66 // minimum of 15 seconds while authenticating
67 if (rfb::Server::idleTimeout < 15)
68 idleTimer.start(secsToMillis(15));
69 else
70 idleTimer.start(secsToMillis(rfb::Server::idleTimeout));
71 }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000072}
73
74
75VNCSConnectionST::~VNCSConnectionST()
76{
77 // If we reach here then VNCServerST is deleting us!
Pierre Ossman6c97fa42018-10-05 17:35:51 +020078 if (closeReason.buf)
79 vlog.info("closing %s: %s", peerEndpoint.buf, closeReason.buf);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000080
81 // Release any keys the client still had pressed
Pierre Ossman16e1dcb2017-05-16 14:33:43 +020082 while (!pressedKeys.empty()) {
83 rdr::U32 keysym, keycode;
84
85 keysym = pressedKeys.begin()->second;
86 keycode = pressedKeys.begin()->first;
87 pressedKeys.erase(pressedKeys.begin());
88
89 vlog.debug("Releasing key 0x%x / 0x%x on client disconnect",
90 keysym, keycode);
Pierre Ossmanb6843412018-10-05 17:30:52 +020091 server->keyEvent(keysym, keycode, false);
Pierre Ossman9a153b02015-08-31 10:01:14 +020092 }
Pierre Ossman16e1dcb2017-05-16 14:33:43 +020093
Pierre Ossman2c764942011-11-14 15:54:30 +000094 delete [] fenceData;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000095}
96
97
Pierre Ossman7d64b332018-10-08 15:59:02 +020098// SConnection methods
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000099
Pierre Ossman7d64b332018-10-08 15:59:02 +0200100bool VNCSConnectionST::accessCheck(AccessRights ar) const
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000101{
Pierre Ossman7d64b332018-10-08 15:59:02 +0200102 // Reverse connections are user initiated, so they are implicitly
103 // allowed to bypass the query
104 if (reverseConnection)
105 ar &= ~AccessNoQuery;
106
107 return SConnection::accessCheck(ar);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000108}
109
110void VNCSConnectionST::close(const char* reason)
111{
112 // Log the reason for the close
113 if (!closeReason.buf)
Adam Tkacd36b6262009-09-04 10:57:20 +0000114 closeReason.buf = strDup(reason);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000115 else
116 vlog.debug("second close: %s (%s)", peerEndpoint.buf, reason);
117
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000118 // Just shutdown the socket and mark our state as closing. Eventually the
119 // calling code will call VNCServerST's removeSocket() method causing us to
120 // be deleted.
121 sock->shutdown();
Pierre Ossman7d64b332018-10-08 15:59:02 +0200122
123 SConnection::close(reason);
124}
125
126
127// Methods called from VNCServerST
128
129bool VNCSConnectionST::init()
130{
131 try {
132 initialiseProtocol();
133 } catch (rdr::Exception& e) {
134 close(e.str());
135 return false;
136 }
137 return true;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000138}
139
140
141void VNCSConnectionST::processMessages()
142{
143 if (state() == RFBSTATE_CLOSING) return;
144 try {
145 // - Now set appropriate socket timeouts and process data
146 setSocketTimeouts();
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000147
148 inProcessMessages = true;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000149
Pierre Ossmana830bec2011-11-08 12:12:02 +0000150 // Get the underlying TCP layer to build large packets if we send
151 // multiple small responses.
Peter Åstrand (astrand)01dc1a62017-10-10 12:56:04 +0200152 sock->cork(true);
Pierre Ossmana830bec2011-11-08 12:12:02 +0000153
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000154 while (getInStream()->checkNoWait(1)) {
Pierre Ossman88a94ed2019-04-01 14:22:01 +0200155 // Silently drop any data if we are currently delaying an
156 // authentication failure response as otherwise we would close
157 // the connection on unexpected data, and an attacker could use
158 // that to detect our delayed state.
159 if (state() == RFBSTATE_SECURITY_FAILURE) {
160 getInStream()->skip(1);
161 continue;
162 }
163
Pierre Ossmanb8b1e962012-07-20 10:47:00 +0000164 if (pendingSyncFence) {
165 syncFence = true;
166 pendingSyncFence = false;
167 }
168
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000169 processMsg();
Pierre Ossmanb8b1e962012-07-20 10:47:00 +0000170
Pierre Ossman2c764942011-11-14 15:54:30 +0000171 if (syncFence) {
172 writer()->writeFence(fenceFlags, fenceDataLen, fenceData);
173 syncFence = false;
174 }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000175 }
176
Pierre Ossmana830bec2011-11-08 12:12:02 +0000177 // Flush out everything in case we go idle after this.
Peter Åstrand (astrand)01dc1a62017-10-10 12:56:04 +0200178 sock->cork(false);
Pierre Ossmana830bec2011-11-08 12:12:02 +0000179
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000180 inProcessMessages = false;
Constantin Kaplinsky2ef66952008-08-29 11:33:46 +0000181
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000182 // If there were anything requiring an update, try to send it here.
183 // We wait until now with this to aggregate responses and to give
184 // higher priority to user actions such as keyboard and pointer events.
185 writeFramebufferUpdate();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000186 } catch (rdr::EndOfStream&) {
187 close("Clean disconnection");
188 } catch (rdr::Exception &e) {
189 close(e.str());
190 }
191}
192
Pierre Ossmand408ca52016-04-29 14:26:05 +0200193void VNCSConnectionST::flushSocket()
194{
195 if (state() == RFBSTATE_CLOSING) return;
196 try {
197 setSocketTimeouts();
198 sock->outStream().flush();
Pierre Ossmana40ab202016-04-29 15:35:56 +0200199 // Flushing the socket might release an update that was previously
200 // delayed because of congestion.
201 if (sock->outStream().bufferUsage() == 0)
202 writeFramebufferUpdate();
Pierre Ossmand408ca52016-04-29 14:26:05 +0200203 } catch (rdr::Exception &e) {
204 close(e.str());
205 }
206}
207
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000208void VNCSConnectionST::pixelBufferChange()
209{
210 try {
211 if (!authenticated()) return;
Pierre Ossman0d3ce872018-06-18 15:59:00 +0200212 if (client.width() && client.height() &&
Pierre Ossmand8bbbeb2018-12-10 21:16:07 +0100213 (server->getPixelBuffer()->width() != client.width() ||
214 server->getPixelBuffer()->height() != client.height()))
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000215 {
216 // We need to clip the next update to the new size, but also add any
217 // extra bits if it's bigger. If we wanted to do this exactly, something
218 // like the code below would do it, but at the moment we just update the
Pierre Ossman671d2ef2015-06-09 16:09:06 +0200219 // entire new size. However, we do need to clip the damagedCursorRegion
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000220 // because that might be added to updates in writeFramebufferUpdate().
221
222 //updates.intersect(server->pb->getRect());
223 //
Pierre Ossman0d3ce872018-06-18 15:59:00 +0200224 //if (server->pb->width() > client.width())
225 // updates.add_changed(Rect(client.width(), 0, server->pb->width(),
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000226 // server->pb->height()));
Pierre Ossman0d3ce872018-06-18 15:59:00 +0200227 //if (server->pb->height() > client.height())
228 // updates.add_changed(Rect(0, client.height(), client.width(),
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000229 // server->pb->height()));
230
Pierre Ossman6094ced2018-10-05 17:24:51 +0200231 damagedCursorRegion.assign_intersect(server->getPixelBuffer()->getRect());
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000232
Pierre Ossmand8bbbeb2018-12-10 21:16:07 +0100233 client.setDimensions(server->getPixelBuffer()->width(),
234 server->getPixelBuffer()->height(),
235 server->getScreenLayout());
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000236 if (state() == RFBSTATE_NORMAL) {
Pierre Ossman2daba9b2018-10-29 10:03:37 +0100237 if (!client.supportsDesktopSize()) {
238 close("Client does not support desktop resize");
239 return;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000240 }
Pierre Ossman2daba9b2018-10-29 10:03:37 +0100241 writer()->writeDesktopSize(reasonServer);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000242 }
Pierre Ossman6b2f1132016-11-30 08:03:35 +0100243
244 // Drop any lossy tracking that is now outside the framebuffer
Pierre Ossman6094ced2018-10-05 17:24:51 +0200245 encodeManager.pruneLosslessRefresh(Region(server->getPixelBuffer()->getRect()));
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000246 }
247 // Just update the whole screen at the moment because we're too lazy to
248 // work out what's actually changed.
249 updates.clear();
Pierre Ossman6094ced2018-10-05 17:24:51 +0200250 updates.add_changed(server->getPixelBuffer()->getRect());
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000251 writeFramebufferUpdate();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000252 } catch(rdr::Exception &e) {
253 close(e.str());
254 }
255}
256
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000257void VNCSConnectionST::writeFramebufferUpdateOrClose()
Pierre Ossman04e62db2009-03-23 16:57:07 +0000258{
259 try {
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000260 writeFramebufferUpdate();
261 } catch(rdr::Exception &e) {
262 close(e.str());
263 }
264}
Pierre Ossman04e62db2009-03-23 16:57:07 +0000265
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000266void VNCSConnectionST::screenLayoutChangeOrClose(rdr::U16 reason)
267{
268 try {
269 screenLayoutChange(reason);
Pierre Ossmanb218ecd2017-11-16 16:43:13 +0100270 writeFramebufferUpdate();
Pierre Ossman04e62db2009-03-23 16:57:07 +0000271 } catch(rdr::Exception &e) {
272 close(e.str());
273 }
274}
275
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000276void VNCSConnectionST::bellOrClose()
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000277{
278 try {
279 if (state() == RFBSTATE_NORMAL) writer()->writeBell();
280 } catch(rdr::Exception& e) {
281 close(e.str());
282 }
283}
284
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000285void VNCSConnectionST::setDesktopNameOrClose(const char *name)
Peter Åstrandc39e0782009-01-15 12:21:42 +0000286{
Peter Åstrandc39e0782009-01-15 12:21:42 +0000287 try {
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000288 setDesktopName(name);
Pierre Ossmanb218ecd2017-11-16 16:43:13 +0100289 writeFramebufferUpdate();
Peter Åstrandc39e0782009-01-15 12:21:42 +0000290 } catch(rdr::Exception& e) {
291 close(e.str());
292 }
293}
294
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000295void VNCSConnectionST::setCursorOrClose()
296{
297 try {
298 setCursor();
Pierre Ossmanb218ecd2017-11-16 16:43:13 +0100299 writeFramebufferUpdate();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000300 } catch(rdr::Exception& e) {
301 close(e.str());
302 }
303}
304
Pierre Ossmanb45a84f2016-12-12 16:59:15 +0100305void VNCSConnectionST::setLEDStateOrClose(unsigned int state)
306{
307 try {
308 setLEDState(state);
Pierre Ossmanb218ecd2017-11-16 16:43:13 +0100309 writeFramebufferUpdate();
Pierre Ossmanb45a84f2016-12-12 16:59:15 +0100310 } catch(rdr::Exception& e) {
311 close(e.str());
312 }
313}
314
Pierre Ossman615d16b2019-05-03 10:53:06 +0200315void VNCSConnectionST::requestClipboardOrClose()
316{
317 try {
318 if (!accessCheck(AccessCutText)) return;
319 if (!rfb::Server::acceptCutText) return;
320 if (state() != RFBSTATE_NORMAL) return;
321 requestClipboard();
322 } catch(rdr::Exception& e) {
323 close(e.str());
324 }
325}
326
327void VNCSConnectionST::announceClipboardOrClose(bool available)
328{
329 try {
330 if (!accessCheck(AccessCutText)) return;
331 if (!rfb::Server::sendCutText) return;
332 if (state() != RFBSTATE_NORMAL) return;
333 announceClipboard(available);
334 } catch(rdr::Exception& e) {
335 close(e.str());
336 }
337}
338
339void VNCSConnectionST::sendClipboardDataOrClose(const char* data)
340{
341 try {
342 if (!accessCheck(AccessCutText)) return;
343 if (!rfb::Server::sendCutText) return;
344 if (state() != RFBSTATE_NORMAL) return;
345 sendClipboardData(data);
346 } catch(rdr::Exception& e) {
347 close(e.str());
348 }
349}
Pierre Ossmanb45a84f2016-12-12 16:59:15 +0100350
Pierre Ossmanb114cec2011-11-20 15:36:11 +0000351bool VNCSConnectionST::getComparerState()
352{
353 // We interpret a low compression level as an indication that the client
354 // wants to prioritise CPU usage over bandwidth, and hence disable the
355 // comparing update tracker.
Pierre Ossman0d3ce872018-06-18 15:59:00 +0200356 return (client.compressLevel == -1) || (client.compressLevel > 1);
Pierre Ossmanb114cec2011-11-20 15:36:11 +0000357}
358
359
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000360// renderedCursorChange() is called whenever the server-side rendered cursor
361// changes shape or position. It ensures that the next update will clean up
362// the old rendered cursor and if necessary draw the new rendered cursor.
363
364void VNCSConnectionST::renderedCursorChange()
365{
366 if (state() != RFBSTATE_NORMAL) return;
Pierre Ossman71ca8d52017-09-15 11:03:12 +0200367 // Are we switching between client-side and server-side cursor?
Pierre Ossman25db44a2017-11-16 16:40:44 +0100368 if (clientHasCursor == needRenderedCursor())
Pierre Ossman71ca8d52017-09-15 11:03:12 +0200369 setCursorOrClose();
Pierre Ossman25db44a2017-11-16 16:40:44 +0100370 bool hasRenderedCursor = !damagedCursorRegion.is_empty();
Pierre Ossman330ca422017-11-06 13:15:55 +0100371 if (hasRenderedCursor)
Pierre Ossman5c9e1e52011-11-08 10:32:05 +0000372 removeRenderedCursor = true;
Pierre Ossman6b0bc292011-12-21 13:17:54 +0000373 if (needRenderedCursor()) {
Pierre Ossman671d2ef2015-06-09 16:09:06 +0200374 updateRenderedCursor = true;
Pierre Ossman6b0bc292011-12-21 13:17:54 +0000375 writeFramebufferUpdateOrClose();
376 }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000377}
378
379// needRenderedCursor() returns true if this client needs the server-side
380// rendered cursor. This may be because it does not support local cursor or
381// because the current cursor position has not been set by this client.
382// Unfortunately we can't know for sure when the current cursor position has
383// been set by this client. We guess that this is the case when the current
384// cursor position is the same as the last pointer event from this client, or
385// if it is a very short time since this client's last pointer event (up to a
386// second). [ Ideally we should do finer-grained timing here and make the time
387// configurable, but I don't think it's that important. ]
388
389bool VNCSConnectionST::needRenderedCursor()
390{
Pierre Ossman77ede0a2016-12-05 16:57:30 +0100391 if (state() != RFBSTATE_NORMAL)
392 return false;
393
Pierre Ossmanb114f5c2018-06-18 16:34:16 +0200394 if (!client.supportsLocalCursor())
Pierre Ossman77ede0a2016-12-05 16:57:30 +0100395 return true;
Pierre Ossman6094ced2018-10-05 17:24:51 +0200396 if (!server->getCursorPos().equals(pointerEventPos) &&
Pierre Ossman77ede0a2016-12-05 16:57:30 +0100397 (time(0) - pointerEventTime) > 0)
398 return true;
399
400 return false;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000401}
402
403
404void VNCSConnectionST::approveConnectionOrClose(bool accept,
405 const char* reason)
406{
407 try {
408 approveConnection(accept, reason);
409 } catch (rdr::Exception& e) {
410 close(e.str());
411 }
412}
413
414
415
416// -=- Callbacks from SConnection
417
418void VNCSConnectionST::authSuccess()
419{
Pierre Ossman65e08fd2018-10-26 15:54:00 +0200420 if (rfb::Server::idleTimeout)
421 idleTimer.start(secsToMillis(rfb::Server::idleTimeout));
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000422
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000423 // - Set the connection parameters appropriately
Pierre Ossmand8bbbeb2018-12-10 21:16:07 +0100424 client.setDimensions(server->getPixelBuffer()->width(),
425 server->getPixelBuffer()->height(),
426 server->getScreenLayout());
Pierre Ossman0d3ce872018-06-18 15:59:00 +0200427 client.setName(server->getName());
Pierre Ossmand8bbbeb2018-12-10 21:16:07 +0100428 client.setLEDState(server->getLEDState());
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000429
430 // - Set the default pixel format
Pierre Ossmand8bbbeb2018-12-10 21:16:07 +0100431 client.setPF(server->getPixelBuffer()->getPF());
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000432 char buffer[256];
Pierre Ossman0d3ce872018-06-18 15:59:00 +0200433 client.pf().print(buffer, 256);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000434 vlog.info("Server default pixel format %s", buffer);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000435
436 // - Mark the entire display as "dirty"
Pierre Ossman6094ced2018-10-05 17:24:51 +0200437 updates.add_changed(server->getPixelBuffer()->getRect());
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000438}
439
Pierre Ossman88a94ed2019-04-01 14:22:01 +0200440void VNCSConnectionST::authFailure(const char* reason)
441{
442 // Introduce a slight delay of the authentication failure response
443 // to make it difficult to brute force a password
444 authFailureMsg.replaceBuf(strDup(reason));
445 authFailureTimer.start(100);
446}
447
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000448void VNCSConnectionST::queryConnection(const char* userName)
449{
Pierre Ossman6c97fa42018-10-05 17:35:51 +0200450 server->queryConnection(this, userName);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000451}
452
453void VNCSConnectionST::clientInit(bool shared)
454{
Pierre Ossman65e08fd2018-10-26 15:54:00 +0200455 if (rfb::Server::idleTimeout)
456 idleTimer.start(secsToMillis(rfb::Server::idleTimeout));
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000457 if (rfb::Server::alwaysShared || reverseConnection) shared = true;
Pierre Ossman7d64b332018-10-08 15:59:02 +0200458 if (!accessCheck(AccessNonShared)) shared = true;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000459 if (rfb::Server::neverShared) shared = false;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000460 SConnection::clientInit(shared);
Pierre Ossman6c97fa42018-10-05 17:35:51 +0200461 server->clientReady(this, shared);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000462}
463
464void VNCSConnectionST::setPixelFormat(const PixelFormat& pf)
465{
466 SConnection::setPixelFormat(pf);
467 char buffer[256];
468 pf.print(buffer, 256);
469 vlog.info("Client pixel format %s", buffer);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000470 setCursor();
471}
472
473void VNCSConnectionST::pointerEvent(const Point& pos, int buttonMask)
474{
Pierre Ossman65e08fd2018-10-26 15:54:00 +0200475 if (rfb::Server::idleTimeout)
476 idleTimer.start(secsToMillis(rfb::Server::idleTimeout));
477 pointerEventTime = time(0);
Pierre Ossman7d64b332018-10-08 15:59:02 +0200478 if (!accessCheck(AccessPtrEvents)) return;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000479 if (!rfb::Server::acceptPointerEvents) return;
Pierre Ossmanb6843412018-10-05 17:30:52 +0200480 pointerEventPos = pos;
481 server->pointerEvent(this, pointerEventPos, buttonMask);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000482}
483
484
485class VNCSConnectionSTShiftPresser {
486public:
Pierre Ossmanb6843412018-10-05 17:30:52 +0200487 VNCSConnectionSTShiftPresser(VNCServerST* server_)
488 : server(server_), pressed(false) {}
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000489 ~VNCSConnectionSTShiftPresser() {
Pierre Ossman9a153b02015-08-31 10:01:14 +0200490 if (pressed) {
491 vlog.debug("Releasing fake Shift_L");
Pierre Ossmanb6843412018-10-05 17:30:52 +0200492 server->keyEvent(XK_Shift_L, 0, false);
Pierre Ossman9a153b02015-08-31 10:01:14 +0200493 }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000494 }
495 void press() {
Pierre Ossman9a153b02015-08-31 10:01:14 +0200496 vlog.debug("Pressing fake Shift_L");
Pierre Ossmanb6843412018-10-05 17:30:52 +0200497 server->keyEvent(XK_Shift_L, 0, true);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000498 pressed = true;
499 }
Pierre Ossmanb6843412018-10-05 17:30:52 +0200500 VNCServerST* server;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000501 bool pressed;
502};
503
504// keyEvent() - record in the pressedKeys which keys were pressed. Allow
505// multiple down events (for autorepeat), but only allow a single up event.
Pierre Ossman5ae28212017-05-16 14:30:38 +0200506void VNCSConnectionST::keyEvent(rdr::U32 keysym, rdr::U32 keycode, bool down) {
Pierre Ossman16e1dcb2017-05-16 14:33:43 +0200507 rdr::U32 lookup;
508
Pierre Ossman65e08fd2018-10-26 15:54:00 +0200509 if (rfb::Server::idleTimeout)
510 idleTimer.start(secsToMillis(rfb::Server::idleTimeout));
Pierre Ossman7d64b332018-10-08 15:59:02 +0200511 if (!accessCheck(AccessKeyEvents)) return;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000512 if (!rfb::Server::acceptKeyEvents) return;
513
Pierre Ossman9a153b02015-08-31 10:01:14 +0200514 if (down)
Pierre Ossman5ae28212017-05-16 14:30:38 +0200515 vlog.debug("Key pressed: 0x%x / 0x%x", keysym, keycode);
Pierre Ossman9a153b02015-08-31 10:01:14 +0200516 else
Pierre Ossman5ae28212017-05-16 14:30:38 +0200517 vlog.debug("Key released: 0x%x / 0x%x", keysym, keycode);
Pierre Ossman9a153b02015-08-31 10:01:14 +0200518
Pierre Ossmanbb305ca2016-12-11 12:41:26 +0100519 // Avoid lock keys if we don't know the server state
Pierre Ossman6094ced2018-10-05 17:24:51 +0200520 if ((server->getLEDState() == ledUnknown) &&
Pierre Ossman5ae28212017-05-16 14:30:38 +0200521 ((keysym == XK_Caps_Lock) ||
522 (keysym == XK_Num_Lock) ||
523 (keysym == XK_Scroll_Lock))) {
Pierre Ossmanbb305ca2016-12-11 12:41:26 +0100524 vlog.debug("Ignoring lock key (e.g. caps lock)");
525 return;
526 }
Pierre Ossmanbb305ca2016-12-11 12:41:26 +0100527
Pierre Ossmanb45a84f2016-12-12 16:59:15 +0100528 // Lock key heuristics
529 // (only for clients that do not support the LED state extension)
Pierre Ossmanb114f5c2018-06-18 16:34:16 +0200530 if (!client.supportsLEDState()) {
Pierre Ossmanb45a84f2016-12-12 16:59:15 +0100531 // Always ignore ScrollLock as we don't have a heuristic
532 // for that
Pierre Ossman5ae28212017-05-16 14:30:38 +0200533 if (keysym == XK_Scroll_Lock) {
Pierre Ossmanb45a84f2016-12-12 16:59:15 +0100534 vlog.debug("Ignoring lock key (e.g. caps lock)");
535 return;
Pierre Ossmanbb305ca2016-12-11 12:41:26 +0100536 }
537
Pierre Ossman6094ced2018-10-05 17:24:51 +0200538 if (down && (server->getLEDState() != ledUnknown)) {
Pierre Ossmanb45a84f2016-12-12 16:59:15 +0100539 // CapsLock synchronisation heuristic
540 // (this assumes standard interaction between CapsLock the Shift
541 // keys and normal characters)
Pierre Ossman5ae28212017-05-16 14:30:38 +0200542 if (((keysym >= XK_A) && (keysym <= XK_Z)) ||
543 ((keysym >= XK_a) && (keysym <= XK_z))) {
Pierre Ossmanb45a84f2016-12-12 16:59:15 +0100544 bool uppercase, shift, lock;
Pierre Ossmanbb305ca2016-12-11 12:41:26 +0100545
Pierre Ossman5ae28212017-05-16 14:30:38 +0200546 uppercase = (keysym >= XK_A) && (keysym <= XK_Z);
Pierre Ossman851e6802017-09-12 16:44:44 +0200547 shift = isShiftPressed();
Pierre Ossman6094ced2018-10-05 17:24:51 +0200548 lock = server->getLEDState() & ledCapsLock;
Pierre Ossmanbb305ca2016-12-11 12:41:26 +0100549
Pierre Ossmanb45a84f2016-12-12 16:59:15 +0100550 if (lock == (uppercase == shift)) {
551 vlog.debug("Inserting fake CapsLock to get in sync with client");
Pierre Ossmanb6843412018-10-05 17:30:52 +0200552 server->keyEvent(XK_Caps_Lock, 0, true);
553 server->keyEvent(XK_Caps_Lock, 0, false);
Pierre Ossmanb45a84f2016-12-12 16:59:15 +0100554 }
555 }
556
557 // NumLock synchronisation heuristic
558 // (this is more cautious because of the differences between Unix,
559 // Windows and macOS)
Pierre Ossman5ae28212017-05-16 14:30:38 +0200560 if (((keysym >= XK_KP_Home) && (keysym <= XK_KP_Delete)) ||
561 ((keysym >= XK_KP_0) && (keysym <= XK_KP_9)) ||
562 (keysym == XK_KP_Separator) || (keysym == XK_KP_Decimal)) {
Pierre Ossmanb45a84f2016-12-12 16:59:15 +0100563 bool number, shift, lock;
564
Pierre Ossman5ae28212017-05-16 14:30:38 +0200565 number = ((keysym >= XK_KP_0) && (keysym <= XK_KP_9)) ||
566 (keysym == XK_KP_Separator) || (keysym == XK_KP_Decimal);
Pierre Ossman851e6802017-09-12 16:44:44 +0200567 shift = isShiftPressed();
Pierre Ossman6094ced2018-10-05 17:24:51 +0200568 lock = server->getLEDState() & ledNumLock;
Pierre Ossmanb45a84f2016-12-12 16:59:15 +0100569
570 if (shift) {
571 // We don't know the appropriate NumLock state for when Shift
572 // is pressed as it could be one of:
573 //
574 // a) A Unix client where Shift negates NumLock
575 //
576 // b) A Windows client where Shift only cancels NumLock
577 //
578 // c) A macOS client where Shift doesn't have any effect
579 //
580 } else if (lock == (number == shift)) {
581 vlog.debug("Inserting fake NumLock to get in sync with client");
Pierre Ossmanb6843412018-10-05 17:30:52 +0200582 server->keyEvent(XK_Num_Lock, 0, true);
583 server->keyEvent(XK_Num_Lock, 0, false);
Pierre Ossmanb45a84f2016-12-12 16:59:15 +0100584 }
Pierre Ossmanbb305ca2016-12-11 12:41:26 +0100585 }
Pierre Ossman9a153b02015-08-31 10:01:14 +0200586 }
587 }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000588
589 // Turn ISO_Left_Tab into shifted Tab.
Pierre Ossmanb6843412018-10-05 17:30:52 +0200590 VNCSConnectionSTShiftPresser shiftPresser(server);
Pierre Ossman5ae28212017-05-16 14:30:38 +0200591 if (keysym == XK_ISO_Left_Tab) {
Pierre Ossman851e6802017-09-12 16:44:44 +0200592 if (!isShiftPressed())
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000593 shiftPresser.press();
Pierre Ossman5ae28212017-05-16 14:30:38 +0200594 keysym = XK_Tab;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000595 }
596
Pierre Ossman16e1dcb2017-05-16 14:33:43 +0200597 // We need to be able to track keys, so generate a fake index when we
598 // aren't given a keycode
599 if (keycode == 0)
600 lookup = 0x80000000 | keysym;
601 else
602 lookup = keycode;
603
604 // We force the same keysym for an already down key for the
605 // sake of sanity
606 if (pressedKeys.find(lookup) != pressedKeys.end())
607 keysym = pressedKeys[lookup];
608
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000609 if (down) {
Pierre Ossman16e1dcb2017-05-16 14:33:43 +0200610 pressedKeys[lookup] = keysym;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000611 } else {
Pierre Ossman16e1dcb2017-05-16 14:33:43 +0200612 if (!pressedKeys.erase(lookup))
Pierre Ossman5ae28212017-05-16 14:30:38 +0200613 return;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000614 }
Pierre Ossman16e1dcb2017-05-16 14:33:43 +0200615
Pierre Ossmanb6843412018-10-05 17:30:52 +0200616 server->keyEvent(keysym, keycode, down);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000617}
618
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000619void VNCSConnectionST::framebufferUpdateRequest(const Rect& r,bool incremental)
620{
Pierre Ossmane9962f72009-04-23 12:31:42 +0000621 Rect safeRect;
622
Pierre Ossman7d64b332018-10-08 15:59:02 +0200623 if (!accessCheck(AccessView)) return;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000624
625 SConnection::framebufferUpdateRequest(r, incremental);
626
Pierre Ossmand9a59ba2009-03-20 15:55:37 +0000627 // Check that the client isn't sending crappy requests
Pierre Ossman0d3ce872018-06-18 15:59:00 +0200628 if (!r.enclosed_by(Rect(0, 0, client.width(), client.height()))) {
Pierre Ossmand9a59ba2009-03-20 15:55:37 +0000629 vlog.error("FramebufferUpdateRequest %dx%d at %d,%d exceeds framebuffer %dx%d",
Pierre Ossman9312b0e2018-06-20 12:25:14 +0200630 r.width(), r.height(), r.tl.x, r.tl.y,
Pierre Ossman0d3ce872018-06-18 15:59:00 +0200631 client.width(), client.height());
632 safeRect = r.intersect(Rect(0, 0, client.width(), client.height()));
Pierre Ossmane9962f72009-04-23 12:31:42 +0000633 } else {
634 safeRect = r;
Pierre Ossmand9a59ba2009-03-20 15:55:37 +0000635 }
636
Constantin Kaplinsky2ef66952008-08-29 11:33:46 +0000637 // Just update the requested region.
638 // Framebuffer update will be sent a bit later, see processMessages().
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000639 Region reqRgn(r);
Pierre Ossman1b478e52011-11-15 12:08:30 +0000640 if (!incremental || !continuousUpdates)
641 requested.assign_union(reqRgn);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000642
643 if (!incremental) {
644 // Non-incremental update - treat as if area requested has changed
645 updates.add_changed(reqRgn);
Pierre Ossman53125a72009-04-22 15:37:51 +0000646
647 // And send the screen layout to the client (which, unlike the
648 // framebuffer dimensions, the client doesn't get during init)
Pierre Ossman2daba9b2018-10-29 10:03:37 +0100649 if (client.supportsEncoding(pseudoEncodingExtendedDesktopSize))
650 writer()->writeDesktopSize(reasonServer);
Pierre Ossman53125a72009-04-22 15:37:51 +0000651
652 // We do not send a DesktopSize since it only contains the
653 // framebuffer size (which the client already should know) and
654 // because some clients don't handle extra DesktopSize events
655 // very well.
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000656 }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000657}
658
Pierre Ossman34bb0612009-03-21 21:16:14 +0000659void VNCSConnectionST::setDesktopSize(int fb_width, int fb_height,
660 const ScreenSet& layout)
Pierre Ossmanc5e25602009-03-20 12:59:05 +0000661{
Pierre Ossman04e62db2009-03-23 16:57:07 +0000662 unsigned int result;
663
Pierre Ossman7d64b332018-10-08 15:59:02 +0200664 if (!accessCheck(AccessSetDesktopSize)) return;
Michal Srbb318b8f2014-11-24 13:18:28 +0200665 if (!rfb::Server::acceptSetDesktopSize) return;
666
Pierre Ossman07e44cc2018-10-05 17:32:57 +0200667 result = server->setDesktopSize(this, fb_width, fb_height, layout);
Pierre Ossman2daba9b2018-10-29 10:03:37 +0100668 writer()->writeDesktopSize(reasonClient, result);
Pierre Ossmanc5e25602009-03-20 12:59:05 +0000669}
670
Pierre Ossman2c764942011-11-14 15:54:30 +0000671void VNCSConnectionST::fence(rdr::U32 flags, unsigned len, const char data[])
672{
Pierre Ossmanb2a417c2015-12-11 19:32:21 +0100673 rdr::U8 type;
674
Pierre Ossman2c764942011-11-14 15:54:30 +0000675 if (flags & fenceFlagRequest) {
676 if (flags & fenceFlagSyncNext) {
Pierre Ossmanb8b1e962012-07-20 10:47:00 +0000677 pendingSyncFence = true;
Pierre Ossman2c764942011-11-14 15:54:30 +0000678
679 fenceFlags = flags & (fenceFlagBlockBefore | fenceFlagBlockAfter | fenceFlagSyncNext);
680 fenceDataLen = len;
681 delete [] fenceData;
Michal Srbf3afa242017-03-27 19:02:15 +0300682 fenceData = NULL;
Pierre Ossman2c764942011-11-14 15:54:30 +0000683 if (len > 0) {
684 fenceData = new char[len];
685 memcpy(fenceData, data, len);
686 }
687
688 return;
689 }
690
691 // We handle everything synchronously so we trivially honor these modes
692 flags = flags & (fenceFlagBlockBefore | fenceFlagBlockAfter);
693
694 writer()->writeFence(flags, len, data);
695 return;
696 }
697
Pierre Ossmanb2a417c2015-12-11 19:32:21 +0100698 if (len < 1)
699 vlog.error("Fence response of unexpected size received");
Pierre Ossman1b478e52011-11-15 12:08:30 +0000700
Pierre Ossmanb2a417c2015-12-11 19:32:21 +0100701 type = data[0];
702
703 switch (type) {
Pierre Ossman2c764942011-11-14 15:54:30 +0000704 case 0:
705 // Initial dummy fence;
706 break;
Pierre Ossmanb2a417c2015-12-11 19:32:21 +0100707 case 1:
Pierre Ossmanc09e5582015-12-11 20:23:17 +0100708 congestion.gotPong();
Pierre Ossman1b478e52011-11-15 12:08:30 +0000709 break;
Pierre Ossman2c764942011-11-14 15:54:30 +0000710 default:
Pierre Ossmanb2a417c2015-12-11 19:32:21 +0100711 vlog.error("Fence response of unexpected type received");
Pierre Ossman2c764942011-11-14 15:54:30 +0000712 }
713}
714
Pierre Ossman1b478e52011-11-15 12:08:30 +0000715void VNCSConnectionST::enableContinuousUpdates(bool enable,
716 int x, int y, int w, int h)
717{
718 Rect rect;
719
Pierre Ossmanb114f5c2018-06-18 16:34:16 +0200720 if (!client.supportsFence() || !client.supportsContinuousUpdates())
Pierre Ossman1b478e52011-11-15 12:08:30 +0000721 throw Exception("Client tried to enable continuous updates when not allowed");
722
723 continuousUpdates = enable;
724
725 rect.setXYWH(x, y, w, h);
726 cuRegion.reset(rect);
727
728 if (enable) {
729 requested.clear();
Pierre Ossman1b478e52011-11-15 12:08:30 +0000730 } else {
731 writer()->writeEndOfContinuousUpdates();
732 }
733}
734
Pierre Ossman615d16b2019-05-03 10:53:06 +0200735void VNCSConnectionST::handleClipboardRequest()
736{
737 if (!accessCheck(AccessCutText)) return;
738 server->handleClipboardRequest(this);
739}
740
741void VNCSConnectionST::handleClipboardAnnounce(bool available)
742{
743 if (!accessCheck(AccessCutText)) return;
744 if (!rfb::Server::acceptCutText) return;
745 server->handleClipboardAnnounce(this, available);
746}
747
748void VNCSConnectionST::handleClipboardData(const char* data)
749{
750 if (!accessCheck(AccessCutText)) return;
751 if (!rfb::Server::acceptCutText) return;
752 server->handleClipboardData(this, data);
753}
754
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000755// supportsLocalCursor() is called whenever the status of
Pierre Ossmanb114f5c2018-06-18 16:34:16 +0200756// client.supportsLocalCursor() has changed. If the client does now support local
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000757// cursor, we make sure that the old server-side rendered cursor is cleaned up
758// and the cursor is sent to the client.
759
760void VNCSConnectionST::supportsLocalCursor()
761{
Pierre Ossman387a4172017-11-16 16:44:36 +0100762 bool hasRenderedCursor = !damagedCursorRegion.is_empty();
763 if (hasRenderedCursor && !needRenderedCursor())
764 removeRenderedCursor = true;
765 setCursor();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000766}
767
Pierre Ossman2c764942011-11-14 15:54:30 +0000768void VNCSConnectionST::supportsFence()
769{
Pierre Ossmanb2a417c2015-12-11 19:32:21 +0100770 char type = 0;
771 writer()->writeFence(fenceFlagRequest, sizeof(type), &type);
Pierre Ossman2c764942011-11-14 15:54:30 +0000772}
773
Pierre Ossman1b478e52011-11-15 12:08:30 +0000774void VNCSConnectionST::supportsContinuousUpdates()
775{
776 // We refuse to use continuous updates if we cannot monitor the buffer
777 // usage using fences.
Pierre Ossmanb114f5c2018-06-18 16:34:16 +0200778 if (!client.supportsFence())
Pierre Ossman1b478e52011-11-15 12:08:30 +0000779 return;
780
781 writer()->writeEndOfContinuousUpdates();
782}
783
Pierre Ossmanb45a84f2016-12-12 16:59:15 +0100784void VNCSConnectionST::supportsLEDState()
785{
Pierre Ossman7b8bc432018-10-29 10:03:37 +0100786 if (client.ledState() == ledUnknown)
787 return;
788
Pierre Ossmanb45a84f2016-12-12 16:59:15 +0100789 writer()->writeLEDState();
790}
791
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +0000792bool VNCSConnectionST::handleTimeout(Timer* t)
793{
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000794 try {
Peter Åstrand (astrand)7a368c92018-09-19 12:45:17 +0200795 if ((t == &congestionTimer) ||
796 (t == &losslessTimer))
Pierre Ossmana99d14d2015-12-13 15:43:46 +0100797 writeFramebufferUpdate();
Pierre Ossman88a94ed2019-04-01 14:22:01 +0200798 else if (t == &authFailureTimer)
799 SConnection::authFailure(authFailureMsg.buf);
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000800 } catch (rdr::Exception& e) {
801 close(e.str());
802 }
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +0000803
Pierre Ossman65e08fd2018-10-26 15:54:00 +0200804 if (t == &idleTimer)
805 close("Idle timeout");
806
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +0000807 return false;
808}
809
Pierre Ossman851e6802017-09-12 16:44:44 +0200810bool VNCSConnectionST::isShiftPressed()
811{
812 std::map<rdr::U32, rdr::U32>::const_iterator iter;
813
814 for (iter = pressedKeys.begin(); iter != pressedKeys.end(); ++iter) {
815 if (iter->second == XK_Shift_L)
816 return true;
817 if (iter->second == XK_Shift_R)
818 return true;
819 }
820
821 return false;
822}
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +0000823
Pierre Ossman1b478e52011-11-15 12:08:30 +0000824void VNCSConnectionST::writeRTTPing()
825{
Pierre Ossmanb2a417c2015-12-11 19:32:21 +0100826 char type;
Pierre Ossman1b478e52011-11-15 12:08:30 +0000827
Pierre Ossmanb114f5c2018-06-18 16:34:16 +0200828 if (!client.supportsFence())
Pierre Ossman1b478e52011-11-15 12:08:30 +0000829 return;
830
Pierre Ossmana99d14d2015-12-13 15:43:46 +0100831 congestion.updatePosition(sock->outStream().length());
Pierre Ossman1b478e52011-11-15 12:08:30 +0000832
833 // We need to make sure any old update are already processed by the
834 // time we get the response back. This allows us to reliably throttle
835 // back on client overload, as well as network overload.
Pierre Ossmanb2a417c2015-12-11 19:32:21 +0100836 type = 1;
Pierre Ossman1b478e52011-11-15 12:08:30 +0000837 writer()->writeFence(fenceFlagRequest | fenceFlagBlockBefore,
Pierre Ossmanb2a417c2015-12-11 19:32:21 +0100838 sizeof(type), &type);
Pierre Ossman1b478e52011-11-15 12:08:30 +0000839
Pierre Ossmana99d14d2015-12-13 15:43:46 +0100840 congestion.sentPing();
Pierre Ossman1b478e52011-11-15 12:08:30 +0000841}
842
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +0000843bool VNCSConnectionST::isCongested()
844{
Pierre Ossmance261812018-07-17 15:01:53 +0200845 int eta;
Pierre Ossmana99d14d2015-12-13 15:43:46 +0100846
847 congestionTimer.stop();
Pierre Ossman1b478e52011-11-15 12:08:30 +0000848
849 // Stuff still waiting in the send buffer?
Pierre Ossman352d0622016-04-29 15:50:54 +0200850 sock->outStream().flush();
Pierre Ossman8cf71632016-03-11 09:38:08 +0100851 congestion.debugTrace("congestion-trace.csv", sock->getFd());
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +0000852 if (sock->outStream().bufferUsage() > 0)
853 return true;
854
Pierre Ossmanb114f5c2018-06-18 16:34:16 +0200855 if (!client.supportsFence())
Pierre Ossman1b478e52011-11-15 12:08:30 +0000856 return false;
857
Pierre Ossmana99d14d2015-12-13 15:43:46 +0100858 congestion.updatePosition(sock->outStream().length());
859 if (!congestion.isCongested())
Pierre Ossman1b478e52011-11-15 12:08:30 +0000860 return false;
861
Pierre Ossmana99d14d2015-12-13 15:43:46 +0100862 eta = congestion.getUncongestedETA();
863 if (eta >= 0)
864 congestionTimer.start(eta);
Pierre Ossman1b478e52011-11-15 12:08:30 +0000865
866 return true;
867}
868
869
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000870void VNCSConnectionST::writeFramebufferUpdate()
871{
Pierre Ossmana99d14d2015-12-13 15:43:46 +0100872 congestion.updatePosition(sock->outStream().length());
873
Pierre Ossman2c764942011-11-14 15:54:30 +0000874 // We're in the middle of processing a command that's supposed to be
875 // synchronised. Allowing an update to slip out right now might violate
876 // that synchronisation.
877 if (syncFence)
878 return;
879
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000880 // We try to aggregate responses, so don't send out anything whilst we
881 // still have incoming messages. processMessages() will give us another
882 // chance to run once things are idle.
883 if (inProcessMessages)
884 return;
885
Pierre Ossman1b478e52011-11-15 12:08:30 +0000886 if (state() != RFBSTATE_NORMAL)
887 return;
888 if (requested.is_empty() && !continuousUpdates)
Pierre Ossmane9962f72009-04-23 12:31:42 +0000889 return;
Pierre Ossmand9a59ba2009-03-20 15:55:37 +0000890
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +0000891 // Check that we actually have some space on the link and retry in a
892 // bit if things are congested.
Pierre Ossmana40ab202016-04-29 15:35:56 +0200893 if (isCongested())
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +0000894 return;
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +0000895
Pierre Ossmancef3cf72016-11-25 10:06:34 +0100896 // Updates often consists of many small writes, and in continuous
897 // mode, we will also have small fence messages around the update. We
898 // need to aggregate these in order to not clog up TCP's congestion
899 // window.
Peter Åstrand (astrand)01dc1a62017-10-10 12:56:04 +0200900 sock->cork(true);
Pierre Ossman36dadf82011-11-15 12:11:32 +0000901
Pierre Ossmane9962f72009-04-23 12:31:42 +0000902 // First take care of any updates that cannot contain framebuffer data
903 // changes.
Pierre Ossmancef3cf72016-11-25 10:06:34 +0100904 writeNoDataUpdate();
905
906 // Then real data (if possible)
907 writeDataUpdate();
908
Peter Åstrand (astrand)01dc1a62017-10-10 12:56:04 +0200909 sock->cork(false);
Pierre Ossmana99d14d2015-12-13 15:43:46 +0100910
911 congestion.updatePosition(sock->outStream().length());
Pierre Ossmancef3cf72016-11-25 10:06:34 +0100912}
913
914void VNCSConnectionST::writeNoDataUpdate()
915{
916 if (!writer()->needNoDataUpdate())
917 return;
918
919 writer()->writeNoDataUpdate();
920
921 // Make sure no data update is sent until next request
922 requested.clear();
923}
924
925void VNCSConnectionST::writeDataUpdate()
926{
Pierre Ossmanb65feda2018-11-23 17:45:04 +0100927 Region req;
Pierre Ossmancef3cf72016-11-25 10:06:34 +0100928 UpdateInfo ui;
929 bool needNewUpdateInfo;
Pierre Ossman24684e52016-12-05 16:58:19 +0100930 const RenderedCursor *cursor;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000931
Pierre Ossmancef3cf72016-11-25 10:06:34 +0100932 // See what the client has requested (if anything)
Pierre Ossman1b478e52011-11-15 12:08:30 +0000933 if (continuousUpdates)
934 req = cuRegion.union_(requested);
935 else
936 req = requested;
937
Pierre Ossmancef3cf72016-11-25 10:06:34 +0100938 if (req.is_empty())
939 return;
940
941 // Get the lists of updates. Prior to exporting the data to the `ui' object,
942 // getUpdateInfo() will normalize the `updates' object such way that its
943 // `changed' and `copied' regions would not intersect.
Pierre Ossman1b478e52011-11-15 12:08:30 +0000944 updates.getUpdateInfo(&ui, req);
945 needNewUpdateInfo = false;
Constantin Kaplinsky604d7812007-08-31 15:50:37 +0000946
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000947 // If the previous position of the rendered cursor overlaps the source of the
948 // copy, then when the copy happens the corresponding rectangle in the
949 // destination will be wrong, so add it to the changed region.
950
Pierre Ossman671d2ef2015-06-09 16:09:06 +0200951 if (!ui.copied.is_empty() && !damagedCursorRegion.is_empty()) {
952 Region bogusCopiedCursor;
953
Pierre Ossman74385d32018-03-22 16:00:18 +0100954 bogusCopiedCursor = damagedCursorRegion;
Pierre Ossman671d2ef2015-06-09 16:09:06 +0200955 bogusCopiedCursor.translate(ui.copy_delta);
Pierre Ossman6094ced2018-10-05 17:24:51 +0200956 bogusCopiedCursor.assign_intersect(server->getPixelBuffer()->getRect());
Constantin Kaplinsky45517c82007-08-31 15:56:33 +0000957 if (!ui.copied.intersect(bogusCopiedCursor).is_empty()) {
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000958 updates.add_changed(bogusCopiedCursor);
Constantin Kaplinsky604d7812007-08-31 15:50:37 +0000959 needNewUpdateInfo = true;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000960 }
961 }
962
Pierre Ossman671d2ef2015-06-09 16:09:06 +0200963 // If we need to remove the old rendered cursor, just add the region to
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000964 // the changed region.
965
966 if (removeRenderedCursor) {
Pierre Ossman671d2ef2015-06-09 16:09:06 +0200967 updates.add_changed(damagedCursorRegion);
Constantin Kaplinsky604d7812007-08-31 15:50:37 +0000968 needNewUpdateInfo = true;
Pierre Ossman671d2ef2015-06-09 16:09:06 +0200969 damagedCursorRegion.clear();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000970 removeRenderedCursor = false;
971 }
972
Pierre Ossman8c3bd692018-03-22 15:58:54 +0100973 // If we need a full cursor update then make sure its entire region
974 // is marked as changed.
975
976 if (updateRenderedCursor) {
977 updates.add_changed(server->getRenderedCursor()->getEffectiveRect());
978 needNewUpdateInfo = true;
979 updateRenderedCursor = false;
980 }
981
Constantin Kaplinsky604d7812007-08-31 15:50:37 +0000982 // The `updates' object could change, make sure we have valid update info.
983
984 if (needNewUpdateInfo)
Pierre Ossman1b478e52011-11-15 12:08:30 +0000985 updates.getUpdateInfo(&ui, req);
Constantin Kaplinsky604d7812007-08-31 15:50:37 +0000986
Pierre Ossman8efc7b42018-03-23 11:45:51 +0100987 // If there are queued updates then we cannot safely send an update
988 // without risking a partially updated screen
Pierre Ossmanb65feda2018-11-23 17:45:04 +0100989 if (!server->getPendingRegion().is_empty()) {
990 req.clear();
Pierre Ossman8efc7b42018-03-23 11:45:51 +0100991 ui.changed.clear();
992 ui.copied.clear();
993 }
994
Pierre Ossman8c3bd692018-03-22 15:58:54 +0100995 // Does the client need a server-side rendered cursor?
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000996
Pierre Ossman24684e52016-12-05 16:58:19 +0100997 cursor = NULL;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000998 if (needRenderedCursor()) {
Pierre Ossman671d2ef2015-06-09 16:09:06 +0200999 Rect renderedCursorRect;
1000
Pierre Ossman24684e52016-12-05 16:58:19 +01001001 cursor = server->getRenderedCursor();
Pierre Ossman8c3bd692018-03-22 15:58:54 +01001002 renderedCursorRect = cursor->getEffectiveRect();
Pierre Ossman24684e52016-12-05 16:58:19 +01001003
Pierre Ossman8c3bd692018-03-22 15:58:54 +01001004 // Check that we don't try to copy over the cursor area, and
1005 // if that happens we need to treat it as changed so that we can
1006 // re-render it
1007 if (!ui.copied.intersect(renderedCursorRect).is_empty()) {
1008 ui.changed.assign_union(ui.copied.intersect(renderedCursorRect));
1009 ui.copied.assign_subtract(renderedCursorRect);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001010 }
1011
Pierre Ossman8c3bd692018-03-22 15:58:54 +01001012 // Track where we've rendered the cursor
1013 damagedCursorRegion.assign_union(ui.changed.intersect(renderedCursorRect));
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001014 }
1015
Pierre Ossmanb65feda2018-11-23 17:45:04 +01001016 // If we don't have a normal update, then try a lossless refresh
Peter Åstrand (astrand)7a368c92018-09-19 12:45:17 +02001017 if (ui.is_empty() && !writer()->needFakeUpdate()) {
Pierre Ossmanb65feda2018-11-23 17:45:04 +01001018 writeLosslessRefresh();
1019 return;
Peter Åstrand (astrand)7a368c92018-09-19 12:45:17 +02001020 }
Pierre Ossmanfdba3fe2014-01-31 13:12:18 +01001021
Pierre Ossmanb65feda2018-11-23 17:45:04 +01001022 // We have something to send, so let's get to it
1023
Pierre Ossmancef3cf72016-11-25 10:06:34 +01001024 writeRTTPing();
Pierre Ossman1b478e52011-11-15 12:08:30 +00001025
Pierre Ossmanb65feda2018-11-23 17:45:04 +01001026 encodeManager.writeUpdate(ui, server->getPixelBuffer(), cursor);
Pierre Ossman1b478e52011-11-15 12:08:30 +00001027
Pierre Ossmancef3cf72016-11-25 10:06:34 +01001028 writeRTTPing();
Pierre Ossmanc0397262014-03-14 15:59:46 +01001029
Pierre Ossmancef3cf72016-11-25 10:06:34 +01001030 // The request might be for just part of the screen, so we cannot
1031 // just clear the entire update tracker.
1032 updates.subtract(req);
Pierre Ossmanc0397262014-03-14 15:59:46 +01001033
Pierre Ossmancef3cf72016-11-25 10:06:34 +01001034 requested.clear();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001035}
1036
Pierre Ossmanb65feda2018-11-23 17:45:04 +01001037void VNCSConnectionST::writeLosslessRefresh()
1038{
1039 Region req, pending;
1040 const RenderedCursor *cursor;
1041
1042 int nextRefresh, nextUpdate;
Pierre Ossman812da462018-11-23 17:48:02 +01001043 size_t bandwidth, maxUpdateSize;
Pierre Ossmanb65feda2018-11-23 17:45:04 +01001044
1045 if (continuousUpdates)
1046 req = cuRegion.union_(requested);
1047 else
1048 req = requested;
1049
1050 // If there are queued updates then we could not safely send an
1051 // update without risking a partially updated screen, however we
1052 // might still be able to send a lossless refresh
1053 pending = server->getPendingRegion();
1054 if (!pending.is_empty()) {
1055 UpdateInfo ui;
1056
1057 // Don't touch the updates pending in the server core
1058 req.assign_subtract(pending);
1059
1060 // Or any updates pending just for this connection
1061 updates.getUpdateInfo(&ui, req);
1062 req.assign_subtract(ui.changed);
1063 req.assign_subtract(ui.copied);
1064 }
1065
1066 // Any lossy area we can refresh?
1067 if (!encodeManager.needsLosslessRefresh(req))
1068 return;
1069
1070 // Right away? Or later?
1071 nextRefresh = encodeManager.getNextLosslessRefresh(req);
1072 if (nextRefresh > 0) {
1073 losslessTimer.start(nextRefresh);
1074 return;
1075 }
1076
1077 // Prepare the cursor in case it overlaps with a region getting
1078 // refreshed
1079 cursor = NULL;
1080 if (needRenderedCursor())
1081 cursor = server->getRenderedCursor();
1082
Pierre Ossmanb65feda2018-11-23 17:45:04 +01001083 // FIXME: If continuous updates aren't used then the client might
1084 // be slower than frameRate in its requests and we could
1085 // afford a larger update size
1086 nextUpdate = server->msToNextUpdate();
Pierre Ossmanb65feda2018-11-23 17:45:04 +01001087
Pierre Ossman812da462018-11-23 17:48:02 +01001088 // Don't bother if we're about to send a real update
1089 if (nextUpdate == 0)
1090 return;
Pierre Ossmanb65feda2018-11-23 17:45:04 +01001091
Pierre Ossman812da462018-11-23 17:48:02 +01001092 // FIXME: Bandwidth estimation without congestion control
1093 bandwidth = congestion.getBandwidth();
Pierre Ossmanb65feda2018-11-23 17:45:04 +01001094
Pierre Ossman812da462018-11-23 17:48:02 +01001095 // FIXME: Hard coded value for maximum CPU throughput
1096 if (bandwidth > 5000000)
1097 bandwidth = 5000000;
Pierre Ossmanb65feda2018-11-23 17:45:04 +01001098
Pierre Ossman812da462018-11-23 17:48:02 +01001099 maxUpdateSize = bandwidth * nextUpdate / 1000;
1100
1101 writeRTTPing();
1102
1103 encodeManager.writeLosslessRefresh(req, server->getPixelBuffer(),
1104 cursor, maxUpdateSize);
Pierre Ossmanb65feda2018-11-23 17:45:04 +01001105
1106 writeRTTPing();
1107
1108 requested.clear();
1109}
1110
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001111
Pierre Ossmana3ac01e2011-11-07 21:13:54 +00001112void VNCSConnectionST::screenLayoutChange(rdr::U16 reason)
1113{
1114 if (!authenticated())
1115 return;
1116
Pierre Ossman0d3ce872018-06-18 15:59:00 +02001117 client.setDimensions(client.width(), client.height(),
Pierre Ossmand8bbbeb2018-12-10 21:16:07 +01001118 server->getScreenLayout());
Pierre Ossmana3ac01e2011-11-07 21:13:54 +00001119
1120 if (state() != RFBSTATE_NORMAL)
1121 return;
1122
Pierre Ossman2daba9b2018-10-29 10:03:37 +01001123 writer()->writeDesktopSize(reason);
Pierre Ossmana3ac01e2011-11-07 21:13:54 +00001124}
1125
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001126
1127// setCursor() is called whenever the cursor has changed shape or pixel format.
1128// If the client supports local cursor then it will arrange for the cursor to
1129// be sent to the client.
1130
1131void VNCSConnectionST::setCursor()
1132{
Pierre Ossmana3ac01e2011-11-07 21:13:54 +00001133 if (state() != RFBSTATE_NORMAL)
1134 return;
Pierre Ossmana3ac01e2011-11-07 21:13:54 +00001135
Pierre Ossman71ca8d52017-09-15 11:03:12 +02001136 // We need to blank out the client's cursor or there will be two
Pierre Ossman25db44a2017-11-16 16:40:44 +01001137 if (needRenderedCursor()) {
Pierre Ossman0d3ce872018-06-18 15:59:00 +02001138 client.setCursor(emptyCursor);
Pierre Ossman25db44a2017-11-16 16:40:44 +01001139 clientHasCursor = false;
1140 } else {
Pierre Ossmand8bbbeb2018-12-10 21:16:07 +01001141 client.setCursor(*server->getCursor());
Pierre Ossman25db44a2017-11-16 16:40:44 +01001142 clientHasCursor = true;
1143 }
Pierre Ossman126e5642014-02-13 14:40:25 +01001144
Pierre Ossman2daba9b2018-10-29 10:03:37 +01001145 if (client.supportsLocalCursor())
1146 writer()->writeCursor();
Pierre Ossmana3ac01e2011-11-07 21:13:54 +00001147}
1148
1149void VNCSConnectionST::setDesktopName(const char *name)
1150{
Pierre Ossman0d3ce872018-06-18 15:59:00 +02001151 client.setName(name);
Pierre Ossmana3ac01e2011-11-07 21:13:54 +00001152
1153 if (state() != RFBSTATE_NORMAL)
1154 return;
1155
Pierre Ossman7b8bc432018-10-29 10:03:37 +01001156 if (client.supportsEncoding(pseudoEncodingDesktopName))
1157 writer()->writeSetDesktopName();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001158}
1159
Pierre Ossmanb45a84f2016-12-12 16:59:15 +01001160void VNCSConnectionST::setLEDState(unsigned int ledstate)
1161{
1162 if (state() != RFBSTATE_NORMAL)
1163 return;
1164
Pierre Ossman0d3ce872018-06-18 15:59:00 +02001165 client.setLEDState(ledstate);
Pierre Ossmanb45a84f2016-12-12 16:59:15 +01001166
Pierre Ossman7b8bc432018-10-29 10:03:37 +01001167 if (client.supportsLEDState())
1168 writer()->writeLEDState();
Pierre Ossmanb45a84f2016-12-12 16:59:15 +01001169}
1170
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001171void VNCSConnectionST::setSocketTimeouts()
1172{
1173 int timeoutms = rfb::Server::clientWaitTimeMillis;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001174 if (timeoutms == 0)
1175 timeoutms = -1;
1176 sock->inStream().setTimeout(timeoutms);
1177 sock->outStream().setTimeout(timeoutms);
1178}