blob: d9e276a919c7c8b59cbac09f34e7224c0f0488f4 [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 Ossman7d64b332018-10-08 15:59:02 +020055 clientHasCursor(false), startTime(time(0))
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000056{
57 setStreams(&sock->inStream(), &sock->outStream());
58 peerEndpoint.buf = sock->getPeerEndpoint();
59 VNCServerST::connectionsLog.write(1,"accepted: %s", peerEndpoint.buf);
60
61 // Configure the socket
62 setSocketTimeouts();
63 lastEventTime = time(0);
64
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000065 server->clients.push_front(this);
66}
67
68
69VNCSConnectionST::~VNCSConnectionST()
70{
71 // If we reach here then VNCServerST is deleting us!
72 VNCServerST::connectionsLog.write(1,"closed: %s (%s)",
73 peerEndpoint.buf,
74 (closeReason.buf) ? closeReason.buf : "");
75
76 // Release any keys the client still had pressed
Pierre Ossman16e1dcb2017-05-16 14:33:43 +020077 while (!pressedKeys.empty()) {
78 rdr::U32 keysym, keycode;
79
80 keysym = pressedKeys.begin()->second;
81 keycode = pressedKeys.begin()->first;
82 pressedKeys.erase(pressedKeys.begin());
83
84 vlog.debug("Releasing key 0x%x / 0x%x on client disconnect",
85 keysym, keycode);
Pierre Ossmanb6843412018-10-05 17:30:52 +020086 server->keyEvent(keysym, keycode, false);
Pierre Ossman9a153b02015-08-31 10:01:14 +020087 }
Pierre Ossman16e1dcb2017-05-16 14:33:43 +020088
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000089 // Remove this client from the server
90 server->clients.remove(this);
91
Pierre Ossman2c764942011-11-14 15:54:30 +000092 delete [] fenceData;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000093}
94
95
Pierre Ossman7d64b332018-10-08 15:59:02 +020096// SConnection methods
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000097
Pierre Ossman7d64b332018-10-08 15:59:02 +020098bool VNCSConnectionST::accessCheck(AccessRights ar) const
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000099{
Pierre Ossman7d64b332018-10-08 15:59:02 +0200100 // Reverse connections are user initiated, so they are implicitly
101 // allowed to bypass the query
102 if (reverseConnection)
103 ar &= ~AccessNoQuery;
104
105 return SConnection::accessCheck(ar);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000106}
107
108void VNCSConnectionST::close(const char* reason)
109{
110 // Log the reason for the close
111 if (!closeReason.buf)
Adam Tkacd36b6262009-09-04 10:57:20 +0000112 closeReason.buf = strDup(reason);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000113 else
114 vlog.debug("second close: %s (%s)", peerEndpoint.buf, reason);
115
116 if (authenticated()) {
117 server->lastDisconnectTime = time(0);
118 }
119
120 // Just shutdown the socket and mark our state as closing. Eventually the
121 // calling code will call VNCServerST's removeSocket() method causing us to
122 // be deleted.
123 sock->shutdown();
Pierre Ossman7d64b332018-10-08 15:59:02 +0200124
125 SConnection::close(reason);
126}
127
128
129// Methods called from VNCServerST
130
131bool VNCSConnectionST::init()
132{
133 try {
134 initialiseProtocol();
135 } catch (rdr::Exception& e) {
136 close(e.str());
137 return false;
138 }
139 return true;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000140}
141
142
143void VNCSConnectionST::processMessages()
144{
145 if (state() == RFBSTATE_CLOSING) return;
146 try {
147 // - Now set appropriate socket timeouts and process data
148 setSocketTimeouts();
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000149
150 inProcessMessages = true;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000151
Pierre Ossmana830bec2011-11-08 12:12:02 +0000152 // Get the underlying TCP layer to build large packets if we send
153 // multiple small responses.
Peter Åstrand (astrand)01dc1a62017-10-10 12:56:04 +0200154 sock->cork(true);
Pierre Ossmana830bec2011-11-08 12:12:02 +0000155
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000156 while (getInStream()->checkNoWait(1)) {
Pierre Ossmanb8b1e962012-07-20 10:47:00 +0000157 if (pendingSyncFence) {
158 syncFence = true;
159 pendingSyncFence = false;
160 }
161
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000162 processMsg();
Pierre Ossmanb8b1e962012-07-20 10:47:00 +0000163
Pierre Ossman2c764942011-11-14 15:54:30 +0000164 if (syncFence) {
165 writer()->writeFence(fenceFlags, fenceDataLen, fenceData);
166 syncFence = false;
167 }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000168 }
169
Pierre Ossmana830bec2011-11-08 12:12:02 +0000170 // Flush out everything in case we go idle after this.
Peter Åstrand (astrand)01dc1a62017-10-10 12:56:04 +0200171 sock->cork(false);
Pierre Ossmana830bec2011-11-08 12:12:02 +0000172
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000173 inProcessMessages = false;
Constantin Kaplinsky2ef66952008-08-29 11:33:46 +0000174
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000175 // If there were anything requiring an update, try to send it here.
176 // We wait until now with this to aggregate responses and to give
177 // higher priority to user actions such as keyboard and pointer events.
178 writeFramebufferUpdate();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000179 } catch (rdr::EndOfStream&) {
180 close("Clean disconnection");
181 } catch (rdr::Exception &e) {
182 close(e.str());
183 }
184}
185
Pierre Ossmand408ca52016-04-29 14:26:05 +0200186void VNCSConnectionST::flushSocket()
187{
188 if (state() == RFBSTATE_CLOSING) return;
189 try {
190 setSocketTimeouts();
191 sock->outStream().flush();
Pierre Ossmana40ab202016-04-29 15:35:56 +0200192 // Flushing the socket might release an update that was previously
193 // delayed because of congestion.
194 if (sock->outStream().bufferUsage() == 0)
195 writeFramebufferUpdate();
Pierre Ossmand408ca52016-04-29 14:26:05 +0200196 } catch (rdr::Exception &e) {
197 close(e.str());
198 }
199}
200
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000201void VNCSConnectionST::pixelBufferChange()
202{
203 try {
204 if (!authenticated()) return;
Pierre Ossman6094ced2018-10-05 17:24:51 +0200205 if (cp.width && cp.height &&
206 (server->getPixelBuffer()->width() != cp.width ||
207 server->getPixelBuffer()->height() != cp.height))
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000208 {
209 // We need to clip the next update to the new size, but also add any
210 // extra bits if it's bigger. If we wanted to do this exactly, something
211 // like the code below would do it, but at the moment we just update the
Pierre Ossman671d2ef2015-06-09 16:09:06 +0200212 // entire new size. However, we do need to clip the damagedCursorRegion
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000213 // because that might be added to updates in writeFramebufferUpdate().
214
215 //updates.intersect(server->pb->getRect());
216 //
217 //if (server->pb->width() > cp.width)
218 // updates.add_changed(Rect(cp.width, 0, server->pb->width(),
219 // server->pb->height()));
220 //if (server->pb->height() > cp.height)
221 // updates.add_changed(Rect(0, cp.height, cp.width,
222 // server->pb->height()));
223
Pierre Ossman6094ced2018-10-05 17:24:51 +0200224 damagedCursorRegion.assign_intersect(server->getPixelBuffer()->getRect());
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000225
Pierre Ossman6094ced2018-10-05 17:24:51 +0200226 cp.width = server->getPixelBuffer()->width();
227 cp.height = server->getPixelBuffer()->height();
228 cp.screenLayout = server->getScreenLayout();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000229 if (state() == RFBSTATE_NORMAL) {
Pierre Ossman2ee430a2009-05-28 12:54:24 +0000230 // We should only send EDS to client asking for both
231 if (!writer()->writeExtendedDesktopSize()) {
232 if (!writer()->writeSetDesktopSize()) {
233 close("Client does not support desktop resize");
234 return;
235 }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000236 }
237 }
Pierre Ossman6b2f1132016-11-30 08:03:35 +0100238
239 // Drop any lossy tracking that is now outside the framebuffer
Pierre Ossman6094ced2018-10-05 17:24:51 +0200240 encodeManager.pruneLosslessRefresh(Region(server->getPixelBuffer()->getRect()));
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000241 }
242 // Just update the whole screen at the moment because we're too lazy to
243 // work out what's actually changed.
244 updates.clear();
Pierre Ossman6094ced2018-10-05 17:24:51 +0200245 updates.add_changed(server->getPixelBuffer()->getRect());
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000246 writeFramebufferUpdate();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000247 } catch(rdr::Exception &e) {
248 close(e.str());
249 }
250}
251
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000252void VNCSConnectionST::writeFramebufferUpdateOrClose()
Pierre Ossman04e62db2009-03-23 16:57:07 +0000253{
254 try {
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000255 writeFramebufferUpdate();
256 } catch(rdr::Exception &e) {
257 close(e.str());
258 }
259}
Pierre Ossman04e62db2009-03-23 16:57:07 +0000260
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000261void VNCSConnectionST::screenLayoutChangeOrClose(rdr::U16 reason)
262{
263 try {
264 screenLayoutChange(reason);
Pierre Ossmanb218ecd2017-11-16 16:43:13 +0100265 writeFramebufferUpdate();
Pierre Ossman04e62db2009-03-23 16:57:07 +0000266 } catch(rdr::Exception &e) {
267 close(e.str());
268 }
269}
270
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000271void VNCSConnectionST::bellOrClose()
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000272{
273 try {
274 if (state() == RFBSTATE_NORMAL) writer()->writeBell();
275 } catch(rdr::Exception& e) {
276 close(e.str());
277 }
278}
279
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000280void VNCSConnectionST::serverCutTextOrClose(const char *str, int len)
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000281{
282 try {
Pierre Ossman7d64b332018-10-08 15:59:02 +0200283 if (!accessCheck(AccessCutText)) return;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000284 if (!rfb::Server::sendCutText) return;
285 if (state() == RFBSTATE_NORMAL)
286 writer()->writeServerCutText(str, len);
287 } catch(rdr::Exception& e) {
288 close(e.str());
289 }
290}
291
Peter Åstrandc39e0782009-01-15 12:21:42 +0000292
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000293void VNCSConnectionST::setDesktopNameOrClose(const char *name)
Peter Åstrandc39e0782009-01-15 12:21:42 +0000294{
Peter Åstrandc39e0782009-01-15 12:21:42 +0000295 try {
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000296 setDesktopName(name);
Pierre Ossmanb218ecd2017-11-16 16:43:13 +0100297 writeFramebufferUpdate();
Peter Åstrandc39e0782009-01-15 12:21:42 +0000298 } catch(rdr::Exception& e) {
299 close(e.str());
300 }
301}
302
303
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000304void VNCSConnectionST::setCursorOrClose()
305{
306 try {
307 setCursor();
Pierre Ossmanb218ecd2017-11-16 16:43:13 +0100308 writeFramebufferUpdate();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000309 } catch(rdr::Exception& e) {
310 close(e.str());
311 }
312}
313
314
Pierre Ossmanb45a84f2016-12-12 16:59:15 +0100315void VNCSConnectionST::setLEDStateOrClose(unsigned int state)
316{
317 try {
318 setLEDState(state);
Pierre Ossmanb218ecd2017-11-16 16:43:13 +0100319 writeFramebufferUpdate();
Pierre Ossmanb45a84f2016-12-12 16:59:15 +0100320 } catch(rdr::Exception& e) {
321 close(e.str());
322 }
323}
324
325
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000326int VNCSConnectionST::checkIdleTimeout()
327{
328 int idleTimeout = rfb::Server::idleTimeout;
329 if (idleTimeout == 0) return 0;
330 if (state() != RFBSTATE_NORMAL && idleTimeout < 15)
331 idleTimeout = 15; // minimum of 15 seconds while authenticating
332 time_t now = time(0);
333 if (now < lastEventTime) {
334 // Someone must have set the time backwards. Set lastEventTime so that the
335 // idleTimeout will count from now.
336 vlog.info("Time has gone backwards - resetting idle timeout");
337 lastEventTime = now;
338 }
339 int timeLeft = lastEventTime + idleTimeout - now;
340 if (timeLeft < -60) {
341 // Our callback is over a minute late - someone must have set the time
342 // forwards. Set lastEventTime so that the idleTimeout will count from
343 // now.
344 vlog.info("Time has gone forwards - resetting idle timeout");
345 lastEventTime = now;
346 return secsToMillis(idleTimeout);
347 }
348 if (timeLeft <= 0) {
349 close("Idle timeout");
350 return 0;
351 }
352 return secsToMillis(timeLeft);
353}
354
Pierre Ossmanb114cec2011-11-20 15:36:11 +0000355
356bool VNCSConnectionST::getComparerState()
357{
358 // We interpret a low compression level as an indication that the client
359 // wants to prioritise CPU usage over bandwidth, and hence disable the
360 // comparing update tracker.
361 return (cp.compressLevel == -1) || (cp.compressLevel > 1);
362}
363
364
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000365// renderedCursorChange() is called whenever the server-side rendered cursor
366// changes shape or position. It ensures that the next update will clean up
367// the old rendered cursor and if necessary draw the new rendered cursor.
368
369void VNCSConnectionST::renderedCursorChange()
370{
371 if (state() != RFBSTATE_NORMAL) return;
Pierre Ossman71ca8d52017-09-15 11:03:12 +0200372 // Are we switching between client-side and server-side cursor?
Pierre Ossman25db44a2017-11-16 16:40:44 +0100373 if (clientHasCursor == needRenderedCursor())
Pierre Ossman71ca8d52017-09-15 11:03:12 +0200374 setCursorOrClose();
Pierre Ossman25db44a2017-11-16 16:40:44 +0100375 bool hasRenderedCursor = !damagedCursorRegion.is_empty();
Pierre Ossman330ca422017-11-06 13:15:55 +0100376 if (hasRenderedCursor)
Pierre Ossman5c9e1e52011-11-08 10:32:05 +0000377 removeRenderedCursor = true;
Pierre Ossman6b0bc292011-12-21 13:17:54 +0000378 if (needRenderedCursor()) {
Pierre Ossman671d2ef2015-06-09 16:09:06 +0200379 updateRenderedCursor = true;
Pierre Ossman6b0bc292011-12-21 13:17:54 +0000380 writeFramebufferUpdateOrClose();
381 }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000382}
383
384// needRenderedCursor() returns true if this client needs the server-side
385// rendered cursor. This may be because it does not support local cursor or
386// because the current cursor position has not been set by this client.
387// Unfortunately we can't know for sure when the current cursor position has
388// been set by this client. We guess that this is the case when the current
389// cursor position is the same as the last pointer event from this client, or
390// if it is a very short time since this client's last pointer event (up to a
391// second). [ Ideally we should do finer-grained timing here and make the time
392// configurable, but I don't think it's that important. ]
393
394bool VNCSConnectionST::needRenderedCursor()
395{
Pierre Ossman77ede0a2016-12-05 16:57:30 +0100396 if (state() != RFBSTATE_NORMAL)
397 return false;
398
Pierre Ossman324043e2017-08-16 16:26:11 +0200399 if (!cp.supportsLocalCursorWithAlpha &&
400 !cp.supportsLocalCursor && !cp.supportsLocalXCursor)
Pierre Ossman77ede0a2016-12-05 16:57:30 +0100401 return true;
Pierre Ossman6094ced2018-10-05 17:24:51 +0200402 if (!server->getCursorPos().equals(pointerEventPos) &&
Pierre Ossman77ede0a2016-12-05 16:57:30 +0100403 (time(0) - pointerEventTime) > 0)
404 return true;
405
406 return false;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000407}
408
409
410void VNCSConnectionST::approveConnectionOrClose(bool accept,
411 const char* reason)
412{
413 try {
414 approveConnection(accept, reason);
415 } catch (rdr::Exception& e) {
416 close(e.str());
417 }
418}
419
420
421
422// -=- Callbacks from SConnection
423
424void VNCSConnectionST::authSuccess()
425{
426 lastEventTime = time(0);
427
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000428 // - Set the connection parameters appropriately
Pierre Ossman6094ced2018-10-05 17:24:51 +0200429 cp.width = server->getPixelBuffer()->width();
430 cp.height = server->getPixelBuffer()->height();
431 cp.screenLayout = server->getScreenLayout();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000432 cp.setName(server->getName());
Pierre Ossman6094ced2018-10-05 17:24:51 +0200433 cp.setLEDState(server->getLEDState());
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000434
435 // - Set the default pixel format
Pierre Ossman6094ced2018-10-05 17:24:51 +0200436 cp.setPF(server->getPixelBuffer()->getPF());
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000437 char buffer[256];
438 cp.pf().print(buffer, 256);
439 vlog.info("Server default pixel format %s", buffer);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000440
441 // - Mark the entire display as "dirty"
Pierre Ossman6094ced2018-10-05 17:24:51 +0200442 updates.add_changed(server->getPixelBuffer()->getRect());
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000443 startTime = time(0);
444}
445
446void VNCSConnectionST::queryConnection(const char* userName)
447{
448 // - Authentication succeeded - clear from blacklist
449 CharArray name; name.buf = sock->getPeerAddress();
450 server->blHosts->clearBlackmark(name.buf);
451
Pierre Ossmaneef6c9a2018-10-05 17:11:25 +0200452 // - Prepare the desktop that we might be making calls
453 server->startDesktop();
454
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000455 // - Special case to provide a more useful error message
456 if (rfb::Server::neverShared && !rfb::Server::disconnectClients &&
457 server->authClientCount() > 0) {
458 approveConnection(false, "The server is already in use");
459 return;
460 }
461
462 // - Does the client have the right to bypass the query?
Pierre Ossman7d64b332018-10-08 15:59:02 +0200463 if (!(rfb::Server::queryConnect || sock->requiresQuery()) ||
464 accessCheck(AccessNoQuery))
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000465 {
466 approveConnection(true);
467 return;
468 }
469
470 // - Get the server to display an Accept/Reject dialog, if required
Pierre Ossmane6aab242018-10-05 16:59:22 +0200471 server->queryConnection(sock, userName);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000472}
473
474void VNCSConnectionST::clientInit(bool shared)
475{
476 lastEventTime = time(0);
477 if (rfb::Server::alwaysShared || reverseConnection) shared = true;
Pierre Ossman7d64b332018-10-08 15:59:02 +0200478 if (!accessCheck(AccessNonShared)) shared = true;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000479 if (rfb::Server::neverShared) shared = false;
480 if (!shared) {
Pierre Ossman7d64b332018-10-08 15:59:02 +0200481 if (rfb::Server::disconnectClients && accessCheck(AccessNonShared)) {
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000482 // - Close all the other connected clients
483 vlog.debug("non-shared connection - closing clients");
484 server->closeClients("Non-shared connection requested", getSock());
485 } else {
486 // - Refuse this connection if there are existing clients, in addition to
487 // this one
488 if (server->authClientCount() > 1) {
489 close("Server is already in use");
490 return;
491 }
492 }
493 }
494 SConnection::clientInit(shared);
495}
496
497void VNCSConnectionST::setPixelFormat(const PixelFormat& pf)
498{
499 SConnection::setPixelFormat(pf);
500 char buffer[256];
501 pf.print(buffer, 256);
502 vlog.info("Client pixel format %s", buffer);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000503 setCursor();
504}
505
506void VNCSConnectionST::pointerEvent(const Point& pos, int buttonMask)
507{
508 pointerEventTime = lastEventTime = time(0);
Pierre Ossman7d64b332018-10-08 15:59:02 +0200509 if (!accessCheck(AccessPtrEvents)) return;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000510 if (!rfb::Server::acceptPointerEvents) return;
Pierre Ossmanb6843412018-10-05 17:30:52 +0200511 pointerEventPos = pos;
512 server->pointerEvent(this, pointerEventPos, buttonMask);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000513}
514
515
516class VNCSConnectionSTShiftPresser {
517public:
Pierre Ossmanb6843412018-10-05 17:30:52 +0200518 VNCSConnectionSTShiftPresser(VNCServerST* server_)
519 : server(server_), pressed(false) {}
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000520 ~VNCSConnectionSTShiftPresser() {
Pierre Ossman9a153b02015-08-31 10:01:14 +0200521 if (pressed) {
522 vlog.debug("Releasing fake Shift_L");
Pierre Ossmanb6843412018-10-05 17:30:52 +0200523 server->keyEvent(XK_Shift_L, 0, false);
Pierre Ossman9a153b02015-08-31 10:01:14 +0200524 }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000525 }
526 void press() {
Pierre Ossman9a153b02015-08-31 10:01:14 +0200527 vlog.debug("Pressing fake Shift_L");
Pierre Ossmanb6843412018-10-05 17:30:52 +0200528 server->keyEvent(XK_Shift_L, 0, true);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000529 pressed = true;
530 }
Pierre Ossmanb6843412018-10-05 17:30:52 +0200531 VNCServerST* server;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000532 bool pressed;
533};
534
535// keyEvent() - record in the pressedKeys which keys were pressed. Allow
536// multiple down events (for autorepeat), but only allow a single up event.
Pierre Ossman5ae28212017-05-16 14:30:38 +0200537void VNCSConnectionST::keyEvent(rdr::U32 keysym, rdr::U32 keycode, bool down) {
Pierre Ossman16e1dcb2017-05-16 14:33:43 +0200538 rdr::U32 lookup;
539
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000540 lastEventTime = time(0);
Pierre Ossman7d64b332018-10-08 15:59:02 +0200541 if (!accessCheck(AccessKeyEvents)) return;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000542 if (!rfb::Server::acceptKeyEvents) return;
543
Pierre Ossman9a153b02015-08-31 10:01:14 +0200544 if (down)
Pierre Ossman5ae28212017-05-16 14:30:38 +0200545 vlog.debug("Key pressed: 0x%x / 0x%x", keysym, keycode);
Pierre Ossman9a153b02015-08-31 10:01:14 +0200546 else
Pierre Ossman5ae28212017-05-16 14:30:38 +0200547 vlog.debug("Key released: 0x%x / 0x%x", keysym, keycode);
Pierre Ossman9a153b02015-08-31 10:01:14 +0200548
Pierre Ossmanbb305ca2016-12-11 12:41:26 +0100549 // Avoid lock keys if we don't know the server state
Pierre Ossman6094ced2018-10-05 17:24:51 +0200550 if ((server->getLEDState() == ledUnknown) &&
Pierre Ossman5ae28212017-05-16 14:30:38 +0200551 ((keysym == XK_Caps_Lock) ||
552 (keysym == XK_Num_Lock) ||
553 (keysym == XK_Scroll_Lock))) {
Pierre Ossmanbb305ca2016-12-11 12:41:26 +0100554 vlog.debug("Ignoring lock key (e.g. caps lock)");
555 return;
556 }
Pierre Ossmanbb305ca2016-12-11 12:41:26 +0100557
Pierre Ossmanb45a84f2016-12-12 16:59:15 +0100558 // Lock key heuristics
559 // (only for clients that do not support the LED state extension)
560 if (!cp.supportsLEDState) {
561 // Always ignore ScrollLock as we don't have a heuristic
562 // for that
Pierre Ossman5ae28212017-05-16 14:30:38 +0200563 if (keysym == XK_Scroll_Lock) {
Pierre Ossmanb45a84f2016-12-12 16:59:15 +0100564 vlog.debug("Ignoring lock key (e.g. caps lock)");
565 return;
Pierre Ossmanbb305ca2016-12-11 12:41:26 +0100566 }
567
Pierre Ossman6094ced2018-10-05 17:24:51 +0200568 if (down && (server->getLEDState() != ledUnknown)) {
Pierre Ossmanb45a84f2016-12-12 16:59:15 +0100569 // CapsLock synchronisation heuristic
570 // (this assumes standard interaction between CapsLock the Shift
571 // keys and normal characters)
Pierre Ossman5ae28212017-05-16 14:30:38 +0200572 if (((keysym >= XK_A) && (keysym <= XK_Z)) ||
573 ((keysym >= XK_a) && (keysym <= XK_z))) {
Pierre Ossmanb45a84f2016-12-12 16:59:15 +0100574 bool uppercase, shift, lock;
Pierre Ossmanbb305ca2016-12-11 12:41:26 +0100575
Pierre Ossman5ae28212017-05-16 14:30:38 +0200576 uppercase = (keysym >= XK_A) && (keysym <= XK_Z);
Pierre Ossman851e6802017-09-12 16:44:44 +0200577 shift = isShiftPressed();
Pierre Ossman6094ced2018-10-05 17:24:51 +0200578 lock = server->getLEDState() & ledCapsLock;
Pierre Ossmanbb305ca2016-12-11 12:41:26 +0100579
Pierre Ossmanb45a84f2016-12-12 16:59:15 +0100580 if (lock == (uppercase == shift)) {
581 vlog.debug("Inserting fake CapsLock to get in sync with client");
Pierre Ossmanb6843412018-10-05 17:30:52 +0200582 server->keyEvent(XK_Caps_Lock, 0, true);
583 server->keyEvent(XK_Caps_Lock, 0, false);
Pierre Ossmanb45a84f2016-12-12 16:59:15 +0100584 }
585 }
586
587 // NumLock synchronisation heuristic
588 // (this is more cautious because of the differences between Unix,
589 // Windows and macOS)
Pierre Ossman5ae28212017-05-16 14:30:38 +0200590 if (((keysym >= XK_KP_Home) && (keysym <= XK_KP_Delete)) ||
591 ((keysym >= XK_KP_0) && (keysym <= XK_KP_9)) ||
592 (keysym == XK_KP_Separator) || (keysym == XK_KP_Decimal)) {
Pierre Ossmanb45a84f2016-12-12 16:59:15 +0100593 bool number, shift, lock;
594
Pierre Ossman5ae28212017-05-16 14:30:38 +0200595 number = ((keysym >= XK_KP_0) && (keysym <= XK_KP_9)) ||
596 (keysym == XK_KP_Separator) || (keysym == XK_KP_Decimal);
Pierre Ossman851e6802017-09-12 16:44:44 +0200597 shift = isShiftPressed();
Pierre Ossman6094ced2018-10-05 17:24:51 +0200598 lock = server->getLEDState() & ledNumLock;
Pierre Ossmanb45a84f2016-12-12 16:59:15 +0100599
600 if (shift) {
601 // We don't know the appropriate NumLock state for when Shift
602 // is pressed as it could be one of:
603 //
604 // a) A Unix client where Shift negates NumLock
605 //
606 // b) A Windows client where Shift only cancels NumLock
607 //
608 // c) A macOS client where Shift doesn't have any effect
609 //
610 } else if (lock == (number == shift)) {
611 vlog.debug("Inserting fake NumLock to get in sync with client");
Pierre Ossmanb6843412018-10-05 17:30:52 +0200612 server->keyEvent(XK_Num_Lock, 0, true);
613 server->keyEvent(XK_Num_Lock, 0, false);
Pierre Ossmanb45a84f2016-12-12 16:59:15 +0100614 }
Pierre Ossmanbb305ca2016-12-11 12:41:26 +0100615 }
Pierre Ossman9a153b02015-08-31 10:01:14 +0200616 }
617 }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000618
619 // Turn ISO_Left_Tab into shifted Tab.
Pierre Ossmanb6843412018-10-05 17:30:52 +0200620 VNCSConnectionSTShiftPresser shiftPresser(server);
Pierre Ossman5ae28212017-05-16 14:30:38 +0200621 if (keysym == XK_ISO_Left_Tab) {
Pierre Ossman851e6802017-09-12 16:44:44 +0200622 if (!isShiftPressed())
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000623 shiftPresser.press();
Pierre Ossman5ae28212017-05-16 14:30:38 +0200624 keysym = XK_Tab;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000625 }
626
Pierre Ossman16e1dcb2017-05-16 14:33:43 +0200627 // We need to be able to track keys, so generate a fake index when we
628 // aren't given a keycode
629 if (keycode == 0)
630 lookup = 0x80000000 | keysym;
631 else
632 lookup = keycode;
633
634 // We force the same keysym for an already down key for the
635 // sake of sanity
636 if (pressedKeys.find(lookup) != pressedKeys.end())
637 keysym = pressedKeys[lookup];
638
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000639 if (down) {
Pierre Ossman16e1dcb2017-05-16 14:33:43 +0200640 pressedKeys[lookup] = keysym;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000641 } else {
Pierre Ossman16e1dcb2017-05-16 14:33:43 +0200642 if (!pressedKeys.erase(lookup))
Pierre Ossman5ae28212017-05-16 14:30:38 +0200643 return;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000644 }
Pierre Ossman16e1dcb2017-05-16 14:33:43 +0200645
Pierre Ossmanb6843412018-10-05 17:30:52 +0200646 server->keyEvent(keysym, keycode, down);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000647}
648
649void VNCSConnectionST::clientCutText(const char* str, int len)
650{
Pierre Ossman7d64b332018-10-08 15:59:02 +0200651 if (!accessCheck(AccessCutText)) return;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000652 if (!rfb::Server::acceptCutText) return;
Pierre Ossmanb6843412018-10-05 17:30:52 +0200653 server->clientCutText(str, len);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000654}
655
656void VNCSConnectionST::framebufferUpdateRequest(const Rect& r,bool incremental)
657{
Pierre Ossmane9962f72009-04-23 12:31:42 +0000658 Rect safeRect;
659
Pierre Ossman7d64b332018-10-08 15:59:02 +0200660 if (!accessCheck(AccessView)) return;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000661
662 SConnection::framebufferUpdateRequest(r, incremental);
663
Pierre Ossmand9a59ba2009-03-20 15:55:37 +0000664 // Check that the client isn't sending crappy requests
665 if (!r.enclosed_by(Rect(0, 0, cp.width, cp.height))) {
666 vlog.error("FramebufferUpdateRequest %dx%d at %d,%d exceeds framebuffer %dx%d",
667 r.width(), r.height(), r.tl.x, r.tl.y, cp.width, cp.height);
Pierre Ossmane9962f72009-04-23 12:31:42 +0000668 safeRect = r.intersect(Rect(0, 0, cp.width, cp.height));
669 } else {
670 safeRect = r;
Pierre Ossmand9a59ba2009-03-20 15:55:37 +0000671 }
672
Constantin Kaplinsky2ef66952008-08-29 11:33:46 +0000673 // Just update the requested region.
674 // Framebuffer update will be sent a bit later, see processMessages().
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000675 Region reqRgn(r);
Pierre Ossman1b478e52011-11-15 12:08:30 +0000676 if (!incremental || !continuousUpdates)
677 requested.assign_union(reqRgn);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000678
679 if (!incremental) {
680 // Non-incremental update - treat as if area requested has changed
681 updates.add_changed(reqRgn);
Pierre Ossman53125a72009-04-22 15:37:51 +0000682
683 // And send the screen layout to the client (which, unlike the
684 // framebuffer dimensions, the client doesn't get during init)
Pierre Ossmanc5e25602009-03-20 12:59:05 +0000685 writer()->writeExtendedDesktopSize();
Pierre Ossman53125a72009-04-22 15:37:51 +0000686
687 // We do not send a DesktopSize since it only contains the
688 // framebuffer size (which the client already should know) and
689 // because some clients don't handle extra DesktopSize events
690 // very well.
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000691 }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000692}
693
Pierre Ossman34bb0612009-03-21 21:16:14 +0000694void VNCSConnectionST::setDesktopSize(int fb_width, int fb_height,
695 const ScreenSet& layout)
Pierre Ossmanc5e25602009-03-20 12:59:05 +0000696{
Pierre Ossman04e62db2009-03-23 16:57:07 +0000697 unsigned int result;
698
Pierre Ossman7d64b332018-10-08 15:59:02 +0200699 if (!accessCheck(AccessSetDesktopSize)) return;
Michal Srbb318b8f2014-11-24 13:18:28 +0200700 if (!rfb::Server::acceptSetDesktopSize) return;
701
Pierre Ossman07e44cc2018-10-05 17:32:57 +0200702 result = server->setDesktopSize(this, fb_width, fb_height, layout);
Pierre Ossman04e62db2009-03-23 16:57:07 +0000703 writer()->writeExtendedDesktopSize(reasonClient, result,
704 fb_width, fb_height, layout);
Pierre Ossmanc5e25602009-03-20 12:59:05 +0000705}
706
Pierre Ossman2c764942011-11-14 15:54:30 +0000707void VNCSConnectionST::fence(rdr::U32 flags, unsigned len, const char data[])
708{
Pierre Ossmanb2a417c2015-12-11 19:32:21 +0100709 rdr::U8 type;
710
Pierre Ossman2c764942011-11-14 15:54:30 +0000711 if (flags & fenceFlagRequest) {
712 if (flags & fenceFlagSyncNext) {
Pierre Ossmanb8b1e962012-07-20 10:47:00 +0000713 pendingSyncFence = true;
Pierre Ossman2c764942011-11-14 15:54:30 +0000714
715 fenceFlags = flags & (fenceFlagBlockBefore | fenceFlagBlockAfter | fenceFlagSyncNext);
716 fenceDataLen = len;
717 delete [] fenceData;
Michal Srbf3afa242017-03-27 19:02:15 +0300718 fenceData = NULL;
Pierre Ossman2c764942011-11-14 15:54:30 +0000719 if (len > 0) {
720 fenceData = new char[len];
721 memcpy(fenceData, data, len);
722 }
723
724 return;
725 }
726
727 // We handle everything synchronously so we trivially honor these modes
728 flags = flags & (fenceFlagBlockBefore | fenceFlagBlockAfter);
729
730 writer()->writeFence(flags, len, data);
731 return;
732 }
733
Pierre Ossmanb2a417c2015-12-11 19:32:21 +0100734 if (len < 1)
735 vlog.error("Fence response of unexpected size received");
Pierre Ossman1b478e52011-11-15 12:08:30 +0000736
Pierre Ossmanb2a417c2015-12-11 19:32:21 +0100737 type = data[0];
738
739 switch (type) {
Pierre Ossman2c764942011-11-14 15:54:30 +0000740 case 0:
741 // Initial dummy fence;
742 break;
Pierre Ossmanb2a417c2015-12-11 19:32:21 +0100743 case 1:
Pierre Ossmanc09e5582015-12-11 20:23:17 +0100744 congestion.gotPong();
Pierre Ossman1b478e52011-11-15 12:08:30 +0000745 break;
Pierre Ossman2c764942011-11-14 15:54:30 +0000746 default:
Pierre Ossmanb2a417c2015-12-11 19:32:21 +0100747 vlog.error("Fence response of unexpected type received");
Pierre Ossman2c764942011-11-14 15:54:30 +0000748 }
749}
750
Pierre Ossman1b478e52011-11-15 12:08:30 +0000751void VNCSConnectionST::enableContinuousUpdates(bool enable,
752 int x, int y, int w, int h)
753{
754 Rect rect;
755
756 if (!cp.supportsFence || !cp.supportsContinuousUpdates)
757 throw Exception("Client tried to enable continuous updates when not allowed");
758
759 continuousUpdates = enable;
760
761 rect.setXYWH(x, y, w, h);
762 cuRegion.reset(rect);
763
764 if (enable) {
765 requested.clear();
Pierre Ossman1b478e52011-11-15 12:08:30 +0000766 } else {
767 writer()->writeEndOfContinuousUpdates();
768 }
769}
770
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000771// supportsLocalCursor() is called whenever the status of
772// cp.supportsLocalCursor has changed. If the client does now support local
773// cursor, we make sure that the old server-side rendered cursor is cleaned up
774// and the cursor is sent to the client.
775
776void VNCSConnectionST::supportsLocalCursor()
777{
Pierre Ossman387a4172017-11-16 16:44:36 +0100778 bool hasRenderedCursor = !damagedCursorRegion.is_empty();
779 if (hasRenderedCursor && !needRenderedCursor())
780 removeRenderedCursor = true;
781 setCursor();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000782}
783
Pierre Ossman2c764942011-11-14 15:54:30 +0000784void VNCSConnectionST::supportsFence()
785{
Pierre Ossmanb2a417c2015-12-11 19:32:21 +0100786 char type = 0;
787 writer()->writeFence(fenceFlagRequest, sizeof(type), &type);
Pierre Ossman2c764942011-11-14 15:54:30 +0000788}
789
Pierre Ossman1b478e52011-11-15 12:08:30 +0000790void VNCSConnectionST::supportsContinuousUpdates()
791{
792 // We refuse to use continuous updates if we cannot monitor the buffer
793 // usage using fences.
794 if (!cp.supportsFence)
795 return;
796
797 writer()->writeEndOfContinuousUpdates();
798}
799
Pierre Ossmanb45a84f2016-12-12 16:59:15 +0100800void VNCSConnectionST::supportsLEDState()
801{
802 writer()->writeLEDState();
803}
804
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000805
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +0000806bool VNCSConnectionST::handleTimeout(Timer* t)
807{
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000808 try {
Peter Åstrand (astrand)7a368c92018-09-19 12:45:17 +0200809 if ((t == &congestionTimer) ||
810 (t == &losslessTimer))
Pierre Ossmana99d14d2015-12-13 15:43:46 +0100811 writeFramebufferUpdate();
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000812 } catch (rdr::Exception& e) {
813 close(e.str());
814 }
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +0000815
816 return false;
817}
818
Pierre Ossman851e6802017-09-12 16:44:44 +0200819bool VNCSConnectionST::isShiftPressed()
820{
821 std::map<rdr::U32, rdr::U32>::const_iterator iter;
822
823 for (iter = pressedKeys.begin(); iter != pressedKeys.end(); ++iter) {
824 if (iter->second == XK_Shift_L)
825 return true;
826 if (iter->second == XK_Shift_R)
827 return true;
828 }
829
830 return false;
831}
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +0000832
Pierre Ossman1b478e52011-11-15 12:08:30 +0000833void VNCSConnectionST::writeRTTPing()
834{
Pierre Ossmanb2a417c2015-12-11 19:32:21 +0100835 char type;
Pierre Ossman1b478e52011-11-15 12:08:30 +0000836
837 if (!cp.supportsFence)
838 return;
839
Pierre Ossmana99d14d2015-12-13 15:43:46 +0100840 congestion.updatePosition(sock->outStream().length());
Pierre Ossman1b478e52011-11-15 12:08:30 +0000841
842 // We need to make sure any old update are already processed by the
843 // time we get the response back. This allows us to reliably throttle
844 // back on client overload, as well as network overload.
Pierre Ossmanb2a417c2015-12-11 19:32:21 +0100845 type = 1;
Pierre Ossman1b478e52011-11-15 12:08:30 +0000846 writer()->writeFence(fenceFlagRequest | fenceFlagBlockBefore,
Pierre Ossmanb2a417c2015-12-11 19:32:21 +0100847 sizeof(type), &type);
Pierre Ossman1b478e52011-11-15 12:08:30 +0000848
Pierre Ossmana99d14d2015-12-13 15:43:46 +0100849 congestion.sentPing();
Pierre Ossman1b478e52011-11-15 12:08:30 +0000850}
851
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +0000852bool VNCSConnectionST::isCongested()
853{
Pierre Ossmance261812018-07-17 15:01:53 +0200854 int eta;
Pierre Ossmana99d14d2015-12-13 15:43:46 +0100855
856 congestionTimer.stop();
Pierre Ossman1b478e52011-11-15 12:08:30 +0000857
858 // Stuff still waiting in the send buffer?
Pierre Ossman352d0622016-04-29 15:50:54 +0200859 sock->outStream().flush();
Pierre Ossman8cf71632016-03-11 09:38:08 +0100860 congestion.debugTrace("congestion-trace.csv", sock->getFd());
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +0000861 if (sock->outStream().bufferUsage() > 0)
862 return true;
863
Pierre Ossman1b478e52011-11-15 12:08:30 +0000864 if (!cp.supportsFence)
865 return false;
866
Pierre Ossmana99d14d2015-12-13 15:43:46 +0100867 congestion.updatePosition(sock->outStream().length());
868 if (!congestion.isCongested())
Pierre Ossman1b478e52011-11-15 12:08:30 +0000869 return false;
870
Pierre Ossmana99d14d2015-12-13 15:43:46 +0100871 eta = congestion.getUncongestedETA();
872 if (eta >= 0)
873 congestionTimer.start(eta);
Pierre Ossman1b478e52011-11-15 12:08:30 +0000874
875 return true;
876}
877
878
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000879void VNCSConnectionST::writeFramebufferUpdate()
880{
Pierre Ossmana99d14d2015-12-13 15:43:46 +0100881 congestion.updatePosition(sock->outStream().length());
882
Pierre Ossman2c764942011-11-14 15:54:30 +0000883 // We're in the middle of processing a command that's supposed to be
884 // synchronised. Allowing an update to slip out right now might violate
885 // that synchronisation.
886 if (syncFence)
887 return;
888
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000889 // We try to aggregate responses, so don't send out anything whilst we
890 // still have incoming messages. processMessages() will give us another
891 // chance to run once things are idle.
892 if (inProcessMessages)
893 return;
894
Pierre Ossman1b478e52011-11-15 12:08:30 +0000895 if (state() != RFBSTATE_NORMAL)
896 return;
897 if (requested.is_empty() && !continuousUpdates)
Pierre Ossmane9962f72009-04-23 12:31:42 +0000898 return;
Pierre Ossmand9a59ba2009-03-20 15:55:37 +0000899
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +0000900 // Check that we actually have some space on the link and retry in a
901 // bit if things are congested.
Pierre Ossmana40ab202016-04-29 15:35:56 +0200902 if (isCongested())
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +0000903 return;
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +0000904
Pierre Ossmancef3cf72016-11-25 10:06:34 +0100905 // Updates often consists of many small writes, and in continuous
906 // mode, we will also have small fence messages around the update. We
907 // need to aggregate these in order to not clog up TCP's congestion
908 // window.
Peter Åstrand (astrand)01dc1a62017-10-10 12:56:04 +0200909 sock->cork(true);
Pierre Ossman36dadf82011-11-15 12:11:32 +0000910
Pierre Ossmane9962f72009-04-23 12:31:42 +0000911 // First take care of any updates that cannot contain framebuffer data
912 // changes.
Pierre Ossmancef3cf72016-11-25 10:06:34 +0100913 writeNoDataUpdate();
914
915 // Then real data (if possible)
916 writeDataUpdate();
917
Peter Åstrand (astrand)01dc1a62017-10-10 12:56:04 +0200918 sock->cork(false);
Pierre Ossmana99d14d2015-12-13 15:43:46 +0100919
920 congestion.updatePosition(sock->outStream().length());
Pierre Ossmancef3cf72016-11-25 10:06:34 +0100921}
922
923void VNCSConnectionST::writeNoDataUpdate()
924{
925 if (!writer()->needNoDataUpdate())
926 return;
927
928 writer()->writeNoDataUpdate();
929
930 // Make sure no data update is sent until next request
931 requested.clear();
932}
933
934void VNCSConnectionST::writeDataUpdate()
935{
Pierre Ossman8efc7b42018-03-23 11:45:51 +0100936 Region req, pending;
Pierre Ossmancef3cf72016-11-25 10:06:34 +0100937 UpdateInfo ui;
938 bool needNewUpdateInfo;
Pierre Ossman24684e52016-12-05 16:58:19 +0100939 const RenderedCursor *cursor;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000940
Constantin Kaplinsky604d7812007-08-31 15:50:37 +0000941 updates.enable_copyrect(cp.useCopyRect);
942
Pierre Ossmancef3cf72016-11-25 10:06:34 +0100943 // See what the client has requested (if anything)
Pierre Ossman1b478e52011-11-15 12:08:30 +0000944 if (continuousUpdates)
945 req = cuRegion.union_(requested);
946 else
947 req = requested;
948
Pierre Ossmancef3cf72016-11-25 10:06:34 +0100949 if (req.is_empty())
950 return;
951
Pierre Ossman8efc7b42018-03-23 11:45:51 +0100952 // Get any framebuffer changes we haven't yet been informed of
953 pending = server->getPendingRegion();
954
Pierre Ossmancef3cf72016-11-25 10:06:34 +0100955 // Get the lists of updates. Prior to exporting the data to the `ui' object,
956 // getUpdateInfo() will normalize the `updates' object such way that its
957 // `changed' and `copied' regions would not intersect.
Pierre Ossman1b478e52011-11-15 12:08:30 +0000958 updates.getUpdateInfo(&ui, req);
959 needNewUpdateInfo = false;
Constantin Kaplinsky604d7812007-08-31 15:50:37 +0000960
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000961 // If the previous position of the rendered cursor overlaps the source of the
962 // copy, then when the copy happens the corresponding rectangle in the
963 // destination will be wrong, so add it to the changed region.
964
Pierre Ossman671d2ef2015-06-09 16:09:06 +0200965 if (!ui.copied.is_empty() && !damagedCursorRegion.is_empty()) {
966 Region bogusCopiedCursor;
967
Pierre Ossman74385d32018-03-22 16:00:18 +0100968 bogusCopiedCursor = damagedCursorRegion;
Pierre Ossman671d2ef2015-06-09 16:09:06 +0200969 bogusCopiedCursor.translate(ui.copy_delta);
Pierre Ossman6094ced2018-10-05 17:24:51 +0200970 bogusCopiedCursor.assign_intersect(server->getPixelBuffer()->getRect());
Constantin Kaplinsky45517c82007-08-31 15:56:33 +0000971 if (!ui.copied.intersect(bogusCopiedCursor).is_empty()) {
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000972 updates.add_changed(bogusCopiedCursor);
Constantin Kaplinsky604d7812007-08-31 15:50:37 +0000973 needNewUpdateInfo = true;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000974 }
975 }
976
Pierre Ossman671d2ef2015-06-09 16:09:06 +0200977 // If we need to remove the old rendered cursor, just add the region to
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000978 // the changed region.
979
980 if (removeRenderedCursor) {
Pierre Ossman671d2ef2015-06-09 16:09:06 +0200981 updates.add_changed(damagedCursorRegion);
Constantin Kaplinsky604d7812007-08-31 15:50:37 +0000982 needNewUpdateInfo = true;
Pierre Ossman671d2ef2015-06-09 16:09:06 +0200983 damagedCursorRegion.clear();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000984 removeRenderedCursor = false;
985 }
986
Pierre Ossman8c3bd692018-03-22 15:58:54 +0100987 // If we need a full cursor update then make sure its entire region
988 // is marked as changed.
989
990 if (updateRenderedCursor) {
991 updates.add_changed(server->getRenderedCursor()->getEffectiveRect());
992 needNewUpdateInfo = true;
993 updateRenderedCursor = false;
994 }
995
Constantin Kaplinsky604d7812007-08-31 15:50:37 +0000996 // The `updates' object could change, make sure we have valid update info.
997
998 if (needNewUpdateInfo)
Pierre Ossman1b478e52011-11-15 12:08:30 +0000999 updates.getUpdateInfo(&ui, req);
Constantin Kaplinsky604d7812007-08-31 15:50:37 +00001000
Pierre Ossman8efc7b42018-03-23 11:45:51 +01001001 // If there are queued updates then we cannot safely send an update
1002 // without risking a partially updated screen
1003
1004 if (!pending.is_empty()) {
1005 // However we might still be able to send a lossless refresh
1006 req.assign_subtract(pending);
1007 req.assign_subtract(ui.changed);
1008 req.assign_subtract(ui.copied);
1009
1010 ui.changed.clear();
1011 ui.copied.clear();
1012 }
1013
Pierre Ossman8c3bd692018-03-22 15:58:54 +01001014 // Does the client need a server-side rendered cursor?
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001015
Pierre Ossman24684e52016-12-05 16:58:19 +01001016 cursor = NULL;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001017 if (needRenderedCursor()) {
Pierre Ossman671d2ef2015-06-09 16:09:06 +02001018 Rect renderedCursorRect;
1019
Pierre Ossman24684e52016-12-05 16:58:19 +01001020 cursor = server->getRenderedCursor();
Pierre Ossman8c3bd692018-03-22 15:58:54 +01001021 renderedCursorRect = cursor->getEffectiveRect();
Pierre Ossman24684e52016-12-05 16:58:19 +01001022
Pierre Ossman8c3bd692018-03-22 15:58:54 +01001023 // Check that we don't try to copy over the cursor area, and
1024 // if that happens we need to treat it as changed so that we can
1025 // re-render it
1026 if (!ui.copied.intersect(renderedCursorRect).is_empty()) {
1027 ui.changed.assign_union(ui.copied.intersect(renderedCursorRect));
1028 ui.copied.assign_subtract(renderedCursorRect);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001029 }
1030
Pierre Ossman8c3bd692018-03-22 15:58:54 +01001031 // Track where we've rendered the cursor
1032 damagedCursorRegion.assign_union(ui.changed.intersect(renderedCursorRect));
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001033 }
1034
Pierre Ossman8efc7b42018-03-23 11:45:51 +01001035 // Return if there is nothing to send the client.
Peter Åstrand (astrand)7a368c92018-09-19 12:45:17 +02001036 if (ui.is_empty() && !writer()->needFakeUpdate()) {
1037 int eta;
Pierre Ossman8efc7b42018-03-23 11:45:51 +01001038
Peter Åstrand (astrand)7a368c92018-09-19 12:45:17 +02001039 // Any lossless refresh that needs handling?
1040 if (!encodeManager.needsLosslessRefresh(req))
1041 return;
1042
1043 // Now? Or later?
1044 eta = encodeManager.getNextLosslessRefresh(req);
1045 if (eta > 0) {
1046 losslessTimer.start(eta);
1047 return;
1048 }
1049 }
Pierre Ossmanfdba3fe2014-01-31 13:12:18 +01001050
Pierre Ossmancef3cf72016-11-25 10:06:34 +01001051 writeRTTPing();
Pierre Ossman1b478e52011-11-15 12:08:30 +00001052
Pierre Ossman6b2f1132016-11-30 08:03:35 +01001053 if (!ui.is_empty())
1054 encodeManager.writeUpdate(ui, server->getPixelBuffer(), cursor);
Pierre Ossmana2b80d62018-03-23 09:30:09 +01001055 else {
Pierre Ossmanc3ce5ce2018-09-19 16:31:18 +02001056 int nextUpdate;
Pierre Ossmana2b80d62018-03-23 09:30:09 +01001057
1058 // FIXME: If continuous updates aren't used then the client might
1059 // be slower than frameRate in its requests and we could
1060 // afford a larger update size
Pierre Ossmanc3ce5ce2018-09-19 16:31:18 +02001061 nextUpdate = server->msToNextUpdate();
1062 if (nextUpdate > 0) {
Pierre Ossmancb5a3992018-09-19 16:35:40 +02001063 size_t bandwidth, maxUpdateSize;
Pierre Ossmana2b80d62018-03-23 09:30:09 +01001064
Pierre Ossmanc3ce5ce2018-09-19 16:31:18 +02001065 // FIXME: Bandwidth estimation without congestion control
Pierre Ossmancb5a3992018-09-19 16:35:40 +02001066 bandwidth = congestion.getBandwidth();
Pierre Ossmana2b80d62018-03-23 09:30:09 +01001067
Pierre Ossmancb5a3992018-09-19 16:35:40 +02001068 // FIXME: Hard coded value for maximum CPU throughput
1069 if (bandwidth > 5000000)
1070 bandwidth = 5000000;
1071
1072 maxUpdateSize = bandwidth * nextUpdate / 1000;
Pierre Ossmanc3ce5ce2018-09-19 16:31:18 +02001073 encodeManager.writeLosslessRefresh(req, server->getPixelBuffer(),
1074 cursor, maxUpdateSize);
1075 }
Pierre Ossmana2b80d62018-03-23 09:30:09 +01001076 }
Pierre Ossman1b478e52011-11-15 12:08:30 +00001077
Pierre Ossmancef3cf72016-11-25 10:06:34 +01001078 writeRTTPing();
Pierre Ossmanc0397262014-03-14 15:59:46 +01001079
Pierre Ossmancef3cf72016-11-25 10:06:34 +01001080 // The request might be for just part of the screen, so we cannot
1081 // just clear the entire update tracker.
1082 updates.subtract(req);
Pierre Ossmanc0397262014-03-14 15:59:46 +01001083
Pierre Ossmancef3cf72016-11-25 10:06:34 +01001084 requested.clear();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001085}
1086
1087
Pierre Ossmana3ac01e2011-11-07 21:13:54 +00001088void VNCSConnectionST::screenLayoutChange(rdr::U16 reason)
1089{
1090 if (!authenticated())
1091 return;
1092
Pierre Ossman6094ced2018-10-05 17:24:51 +02001093 cp.screenLayout = server->getScreenLayout();
Pierre Ossmana3ac01e2011-11-07 21:13:54 +00001094
1095 if (state() != RFBSTATE_NORMAL)
1096 return;
1097
1098 writer()->writeExtendedDesktopSize(reason, 0, cp.width, cp.height,
1099 cp.screenLayout);
Pierre Ossmana3ac01e2011-11-07 21:13:54 +00001100}
1101
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001102
1103// setCursor() is called whenever the cursor has changed shape or pixel format.
1104// If the client supports local cursor then it will arrange for the cursor to
1105// be sent to the client.
1106
1107void VNCSConnectionST::setCursor()
1108{
Pierre Ossmana3ac01e2011-11-07 21:13:54 +00001109 if (state() != RFBSTATE_NORMAL)
1110 return;
Pierre Ossmana3ac01e2011-11-07 21:13:54 +00001111
Pierre Ossman71ca8d52017-09-15 11:03:12 +02001112 // We need to blank out the client's cursor or there will be two
Pierre Ossman25db44a2017-11-16 16:40:44 +01001113 if (needRenderedCursor()) {
Pierre Ossman71ca8d52017-09-15 11:03:12 +02001114 cp.setCursor(emptyCursor);
Pierre Ossman25db44a2017-11-16 16:40:44 +01001115 clientHasCursor = false;
1116 } else {
Pierre Ossman6094ced2018-10-05 17:24:51 +02001117 cp.setCursor(*server->getCursor());
Pierre Ossman25db44a2017-11-16 16:40:44 +01001118 clientHasCursor = true;
1119 }
Pierre Ossman126e5642014-02-13 14:40:25 +01001120
Pierre Ossman8053c8e2017-02-21 12:59:04 +01001121 if (!writer()->writeSetCursorWithAlpha()) {
1122 if (!writer()->writeSetCursor()) {
1123 if (!writer()->writeSetXCursor()) {
1124 // No client support
1125 return;
1126 }
Pierre Ossman126e5642014-02-13 14:40:25 +01001127 }
1128 }
Pierre Ossmana3ac01e2011-11-07 21:13:54 +00001129}
1130
1131void VNCSConnectionST::setDesktopName(const char *name)
1132{
1133 cp.setName(name);
1134
1135 if (state() != RFBSTATE_NORMAL)
1136 return;
1137
1138 if (!writer()->writeSetDesktopName()) {
1139 fprintf(stderr, "Client does not support desktop rename\n");
1140 return;
1141 }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001142}
1143
Pierre Ossmanb45a84f2016-12-12 16:59:15 +01001144void VNCSConnectionST::setLEDState(unsigned int ledstate)
1145{
1146 if (state() != RFBSTATE_NORMAL)
1147 return;
1148
1149 cp.setLEDState(ledstate);
1150
Pierre Ossmanb218ecd2017-11-16 16:43:13 +01001151 writer()->writeLEDState();
Pierre Ossmanb45a84f2016-12-12 16:59:15 +01001152}
1153
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001154void VNCSConnectionST::setSocketTimeouts()
1155{
1156 int timeoutms = rfb::Server::clientWaitTimeMillis;
1157 soonestTimeout(&timeoutms, secsToMillis(rfb::Server::idleTimeout));
1158 if (timeoutms == 0)
1159 timeoutms = -1;
1160 sock->inStream().setTimeout(timeoutms);
1161 sock->outStream().setTimeout(timeoutms);
1162}
1163
1164char* VNCSConnectionST::getStartTime()
1165{
1166 char* result = ctime(&startTime);
1167 result[24] = '\0';
1168 return result;
1169}
1170
1171void VNCSConnectionST::setStatus(int status)
1172{
Pierre Ossman7d64b332018-10-08 15:59:02 +02001173 AccessRights ar;
1174
1175 ar = AccessDefault;
1176
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001177 switch (status) {
1178 case 0:
Pierre Ossman7d64b332018-10-08 15:59:02 +02001179 ar |= AccessPtrEvents | AccessKeyEvents | AccessView;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001180 break;
1181 case 1:
Pierre Ossman7d64b332018-10-08 15:59:02 +02001182 ar |= rfb::SConnection::AccessView;
1183 ar &= ~(AccessPtrEvents | AccessKeyEvents);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001184 break;
1185 case 2:
Pierre Ossman7d64b332018-10-08 15:59:02 +02001186 ar &= ~(AccessPtrEvents | AccessKeyEvents | AccessView);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001187 break;
1188 }
Pierre Ossman7d64b332018-10-08 15:59:02 +02001189
1190 setAccessRights(ar);
1191
Pierre Ossman6094ced2018-10-05 17:24:51 +02001192 framebufferUpdateRequest(server->getPixelBuffer()->getRect(), false);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001193}
1194int VNCSConnectionST::getStatus()
1195{
Pierre Ossman7d64b332018-10-08 15:59:02 +02001196 if (accessCheck(AccessPtrEvents | AccessKeyEvents | AccessView))
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001197 return 0;
Pierre Ossman7d64b332018-10-08 15:59:02 +02001198 else if (accessCheck(AccessView))
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001199 return 1;
Pierre Ossman7d64b332018-10-08 15:59:02 +02001200 else
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001201 return 2;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001202}
1203