blob: c83254fbe5c0182c626a5c85eb4cc367f19626d6 [file] [log] [blame]
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001/* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved.
Pierre Ossman8c3bd692018-03-22 15:58:54 +01002 * Copyright 2009-2018 Pierre Ossman for Cendio AB
Peter Åstrand (astrand)7a368c92018-09-19 12:45:17 +02003 * Copyright 2018 Peter Astrand for Cendio AB
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00004 *
5 * This is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This software is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this software; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
18 * USA.
19 */
20
Pierre Ossmana830bec2011-11-08 12:12:02 +000021#include <network/TcpSocket.h>
Pierre Ossman707fa122015-12-11 20:21:20 +010022
23#include <rfb/ComparingUpdateTracker.h>
24#include <rfb/Encoder.h>
25#include <rfb/KeyRemapper.h>
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000026#include <rfb/LogWriter.h>
Adam Tkac5a0caed2010-04-23 13:58:10 +000027#include <rfb/Security.h>
Pierre Ossman707fa122015-12-11 20:21:20 +010028#include <rfb/ServerCore.h>
29#include <rfb/SMsgWriter.h>
30#include <rfb/VNCServerST.h>
31#include <rfb/VNCSConnectionST.h>
Pierre Ossmanc5e25602009-03-20 12:59:05 +000032#include <rfb/screenTypes.h>
Pierre Ossman2c764942011-11-14 15:54:30 +000033#include <rfb/fenceTypes.h>
Pierre Ossmanbb305ca2016-12-11 12:41:26 +010034#include <rfb/ledStates.h>
Pierre Ossmanbb305ca2016-12-11 12:41:26 +010035#define XK_LATIN1
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000036#define XK_MISCELLANY
37#define XK_XKB_KEYS
38#include <rfb/keysymdef.h>
39
40using namespace rfb;
41
42static LogWriter vlog("VNCSConnST");
43
Pierre Ossman71ca8d52017-09-15 11:03:12 +020044static Cursor emptyCursor(0, 0, Point(0, 0), NULL);
45
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000046VNCSConnectionST::VNCSConnectionST(VNCServerST* server_, network::Socket *s,
47 bool reverse)
Pierre Ossman7069bdd2015-02-06 14:41:58 +010048 : sock(s), reverseConnection(reverse),
Pierre Ossman36304752017-10-04 16:21:57 +020049 inProcessMessages(false),
Pierre Ossmanb8b1e962012-07-20 10:47:00 +000050 pendingSyncFence(false), syncFence(false), fenceFlags(0),
Pierre Ossmana99d14d2015-12-13 15:43:46 +010051 fenceDataLen(0), fenceData(NULL), congestionTimer(this),
Peter Åstrand (astrand)7a368c92018-09-19 12:45:17 +020052 losslessTimer(this), server(server_), updates(false),
Pierre Ossman671d2ef2015-06-09 16:09:06 +020053 updateRenderedCursor(false), removeRenderedCursor(false),
Pierre Ossmana40ab202016-04-29 15:35:56 +020054 continuousUpdates(false), encodeManager(this), pointerEventTime(0),
Pierre Ossman25db44a2017-11-16 16:40:44 +010055 clientHasCursor(false),
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +000056 accessRights(AccessDefault), startTime(time(0))
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000057{
58 setStreams(&sock->inStream(), &sock->outStream());
59 peerEndpoint.buf = sock->getPeerEndpoint();
60 VNCServerST::connectionsLog.write(1,"accepted: %s", peerEndpoint.buf);
61
62 // Configure the socket
63 setSocketTimeouts();
64 lastEventTime = time(0);
65
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000066 server->clients.push_front(this);
67}
68
69
70VNCSConnectionST::~VNCSConnectionST()
71{
72 // If we reach here then VNCServerST is deleting us!
73 VNCServerST::connectionsLog.write(1,"closed: %s (%s)",
74 peerEndpoint.buf,
75 (closeReason.buf) ? closeReason.buf : "");
76
77 // Release any keys the client still had pressed
Pierre Ossman16e1dcb2017-05-16 14:33:43 +020078 while (!pressedKeys.empty()) {
79 rdr::U32 keysym, keycode;
80
81 keysym = pressedKeys.begin()->second;
82 keycode = pressedKeys.begin()->first;
83 pressedKeys.erase(pressedKeys.begin());
84
85 vlog.debug("Releasing key 0x%x / 0x%x on client disconnect",
86 keysym, keycode);
Pierre Ossmanb6843412018-10-05 17:30:52 +020087 server->keyEvent(keysym, keycode, false);
Pierre Ossman9a153b02015-08-31 10:01:14 +020088 }
Pierre Ossman16e1dcb2017-05-16 14:33:43 +020089
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000090 // Remove this client from the server
91 server->clients.remove(this);
92
Pierre Ossman2c764942011-11-14 15:54:30 +000093 delete [] fenceData;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000094}
95
96
97// Methods called from VNCServerST
98
99bool VNCSConnectionST::init()
100{
101 try {
102 initialiseProtocol();
103 } catch (rdr::Exception& e) {
104 close(e.str());
105 return false;
106 }
107 return true;
108}
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
118 if (authenticated()) {
119 server->lastDisconnectTime = time(0);
120 }
121
122 // Just shutdown the socket and mark our state as closing. Eventually the
123 // calling code will call VNCServerST's removeSocket() method causing us to
124 // be deleted.
125 sock->shutdown();
126 setState(RFBSTATE_CLOSING);
127}
128
129
130void VNCSConnectionST::processMessages()
131{
132 if (state() == RFBSTATE_CLOSING) return;
133 try {
134 // - Now set appropriate socket timeouts and process data
135 setSocketTimeouts();
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000136
137 inProcessMessages = true;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000138
Pierre Ossmana830bec2011-11-08 12:12:02 +0000139 // Get the underlying TCP layer to build large packets if we send
140 // multiple small responses.
Peter Åstrand (astrand)01dc1a62017-10-10 12:56:04 +0200141 sock->cork(true);
Pierre Ossmana830bec2011-11-08 12:12:02 +0000142
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000143 while (getInStream()->checkNoWait(1)) {
Pierre Ossmanb8b1e962012-07-20 10:47:00 +0000144 if (pendingSyncFence) {
145 syncFence = true;
146 pendingSyncFence = false;
147 }
148
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000149 processMsg();
Pierre Ossmanb8b1e962012-07-20 10:47:00 +0000150
Pierre Ossman2c764942011-11-14 15:54:30 +0000151 if (syncFence) {
152 writer()->writeFence(fenceFlags, fenceDataLen, fenceData);
153 syncFence = false;
154 }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000155 }
156
Pierre Ossmana830bec2011-11-08 12:12:02 +0000157 // Flush out everything in case we go idle after this.
Peter Åstrand (astrand)01dc1a62017-10-10 12:56:04 +0200158 sock->cork(false);
Pierre Ossmana830bec2011-11-08 12:12:02 +0000159
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000160 inProcessMessages = false;
Constantin Kaplinsky2ef66952008-08-29 11:33:46 +0000161
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000162 // If there were anything requiring an update, try to send it here.
163 // We wait until now with this to aggregate responses and to give
164 // higher priority to user actions such as keyboard and pointer events.
165 writeFramebufferUpdate();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000166 } catch (rdr::EndOfStream&) {
167 close("Clean disconnection");
168 } catch (rdr::Exception &e) {
169 close(e.str());
170 }
171}
172
Pierre Ossmand408ca52016-04-29 14:26:05 +0200173void VNCSConnectionST::flushSocket()
174{
175 if (state() == RFBSTATE_CLOSING) return;
176 try {
177 setSocketTimeouts();
178 sock->outStream().flush();
Pierre Ossmana40ab202016-04-29 15:35:56 +0200179 // Flushing the socket might release an update that was previously
180 // delayed because of congestion.
181 if (sock->outStream().bufferUsage() == 0)
182 writeFramebufferUpdate();
Pierre Ossmand408ca52016-04-29 14:26:05 +0200183 } catch (rdr::Exception &e) {
184 close(e.str());
185 }
186}
187
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000188void VNCSConnectionST::pixelBufferChange()
189{
190 try {
191 if (!authenticated()) return;
Pierre Ossman6094ced2018-10-05 17:24:51 +0200192 if (cp.width && cp.height &&
193 (server->getPixelBuffer()->width() != cp.width ||
194 server->getPixelBuffer()->height() != cp.height))
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000195 {
196 // We need to clip the next update to the new size, but also add any
197 // extra bits if it's bigger. If we wanted to do this exactly, something
198 // like the code below would do it, but at the moment we just update the
Pierre Ossman671d2ef2015-06-09 16:09:06 +0200199 // entire new size. However, we do need to clip the damagedCursorRegion
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000200 // because that might be added to updates in writeFramebufferUpdate().
201
202 //updates.intersect(server->pb->getRect());
203 //
204 //if (server->pb->width() > cp.width)
205 // updates.add_changed(Rect(cp.width, 0, server->pb->width(),
206 // server->pb->height()));
207 //if (server->pb->height() > cp.height)
208 // updates.add_changed(Rect(0, cp.height, cp.width,
209 // server->pb->height()));
210
Pierre Ossman6094ced2018-10-05 17:24:51 +0200211 damagedCursorRegion.assign_intersect(server->getPixelBuffer()->getRect());
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000212
Pierre Ossman6094ced2018-10-05 17:24:51 +0200213 cp.width = server->getPixelBuffer()->width();
214 cp.height = server->getPixelBuffer()->height();
215 cp.screenLayout = server->getScreenLayout();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000216 if (state() == RFBSTATE_NORMAL) {
Pierre Ossman2ee430a2009-05-28 12:54:24 +0000217 // We should only send EDS to client asking for both
218 if (!writer()->writeExtendedDesktopSize()) {
219 if (!writer()->writeSetDesktopSize()) {
220 close("Client does not support desktop resize");
221 return;
222 }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000223 }
224 }
Pierre Ossman6b2f1132016-11-30 08:03:35 +0100225
226 // Drop any lossy tracking that is now outside the framebuffer
Pierre Ossman6094ced2018-10-05 17:24:51 +0200227 encodeManager.pruneLosslessRefresh(Region(server->getPixelBuffer()->getRect()));
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000228 }
229 // Just update the whole screen at the moment because we're too lazy to
230 // work out what's actually changed.
231 updates.clear();
Pierre Ossman6094ced2018-10-05 17:24:51 +0200232 updates.add_changed(server->getPixelBuffer()->getRect());
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000233 writeFramebufferUpdate();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000234 } catch(rdr::Exception &e) {
235 close(e.str());
236 }
237}
238
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000239void VNCSConnectionST::writeFramebufferUpdateOrClose()
Pierre Ossman04e62db2009-03-23 16:57:07 +0000240{
241 try {
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000242 writeFramebufferUpdate();
243 } catch(rdr::Exception &e) {
244 close(e.str());
245 }
246}
Pierre Ossman04e62db2009-03-23 16:57:07 +0000247
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000248void VNCSConnectionST::screenLayoutChangeOrClose(rdr::U16 reason)
249{
250 try {
251 screenLayoutChange(reason);
Pierre Ossmanb218ecd2017-11-16 16:43:13 +0100252 writeFramebufferUpdate();
Pierre Ossman04e62db2009-03-23 16:57:07 +0000253 } catch(rdr::Exception &e) {
254 close(e.str());
255 }
256}
257
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000258void VNCSConnectionST::bellOrClose()
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000259{
260 try {
261 if (state() == RFBSTATE_NORMAL) writer()->writeBell();
262 } catch(rdr::Exception& e) {
263 close(e.str());
264 }
265}
266
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000267void VNCSConnectionST::serverCutTextOrClose(const char *str, int len)
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000268{
269 try {
270 if (!(accessRights & AccessCutText)) return;
271 if (!rfb::Server::sendCutText) return;
272 if (state() == RFBSTATE_NORMAL)
273 writer()->writeServerCutText(str, len);
274 } catch(rdr::Exception& e) {
275 close(e.str());
276 }
277}
278
Peter Åstrandc39e0782009-01-15 12:21:42 +0000279
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000280void VNCSConnectionST::setDesktopNameOrClose(const char *name)
Peter Åstrandc39e0782009-01-15 12:21:42 +0000281{
Peter Åstrandc39e0782009-01-15 12:21:42 +0000282 try {
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000283 setDesktopName(name);
Pierre Ossmanb218ecd2017-11-16 16:43:13 +0100284 writeFramebufferUpdate();
Peter Åstrandc39e0782009-01-15 12:21:42 +0000285 } catch(rdr::Exception& e) {
286 close(e.str());
287 }
288}
289
290
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000291void VNCSConnectionST::setCursorOrClose()
292{
293 try {
294 setCursor();
Pierre Ossmanb218ecd2017-11-16 16:43:13 +0100295 writeFramebufferUpdate();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000296 } catch(rdr::Exception& e) {
297 close(e.str());
298 }
299}
300
301
Pierre Ossmanb45a84f2016-12-12 16:59:15 +0100302void VNCSConnectionST::setLEDStateOrClose(unsigned int state)
303{
304 try {
305 setLEDState(state);
Pierre Ossmanb218ecd2017-11-16 16:43:13 +0100306 writeFramebufferUpdate();
Pierre Ossmanb45a84f2016-12-12 16:59:15 +0100307 } catch(rdr::Exception& e) {
308 close(e.str());
309 }
310}
311
312
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000313int VNCSConnectionST::checkIdleTimeout()
314{
315 int idleTimeout = rfb::Server::idleTimeout;
316 if (idleTimeout == 0) return 0;
317 if (state() != RFBSTATE_NORMAL && idleTimeout < 15)
318 idleTimeout = 15; // minimum of 15 seconds while authenticating
319 time_t now = time(0);
320 if (now < lastEventTime) {
321 // Someone must have set the time backwards. Set lastEventTime so that the
322 // idleTimeout will count from now.
323 vlog.info("Time has gone backwards - resetting idle timeout");
324 lastEventTime = now;
325 }
326 int timeLeft = lastEventTime + idleTimeout - now;
327 if (timeLeft < -60) {
328 // Our callback is over a minute late - someone must have set the time
329 // forwards. Set lastEventTime so that the idleTimeout will count from
330 // now.
331 vlog.info("Time has gone forwards - resetting idle timeout");
332 lastEventTime = now;
333 return secsToMillis(idleTimeout);
334 }
335 if (timeLeft <= 0) {
336 close("Idle timeout");
337 return 0;
338 }
339 return secsToMillis(timeLeft);
340}
341
Pierre Ossmanb114cec2011-11-20 15:36:11 +0000342
343bool VNCSConnectionST::getComparerState()
344{
345 // We interpret a low compression level as an indication that the client
346 // wants to prioritise CPU usage over bandwidth, and hence disable the
347 // comparing update tracker.
348 return (cp.compressLevel == -1) || (cp.compressLevel > 1);
349}
350
351
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000352// renderedCursorChange() is called whenever the server-side rendered cursor
353// changes shape or position. It ensures that the next update will clean up
354// the old rendered cursor and if necessary draw the new rendered cursor.
355
356void VNCSConnectionST::renderedCursorChange()
357{
358 if (state() != RFBSTATE_NORMAL) return;
Pierre Ossman71ca8d52017-09-15 11:03:12 +0200359 // Are we switching between client-side and server-side cursor?
Pierre Ossman25db44a2017-11-16 16:40:44 +0100360 if (clientHasCursor == needRenderedCursor())
Pierre Ossman71ca8d52017-09-15 11:03:12 +0200361 setCursorOrClose();
Pierre Ossman25db44a2017-11-16 16:40:44 +0100362 bool hasRenderedCursor = !damagedCursorRegion.is_empty();
Pierre Ossman330ca422017-11-06 13:15:55 +0100363 if (hasRenderedCursor)
Pierre Ossman5c9e1e52011-11-08 10:32:05 +0000364 removeRenderedCursor = true;
Pierre Ossman6b0bc292011-12-21 13:17:54 +0000365 if (needRenderedCursor()) {
Pierre Ossman671d2ef2015-06-09 16:09:06 +0200366 updateRenderedCursor = true;
Pierre Ossman6b0bc292011-12-21 13:17:54 +0000367 writeFramebufferUpdateOrClose();
368 }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000369}
370
371// needRenderedCursor() returns true if this client needs the server-side
372// rendered cursor. This may be because it does not support local cursor or
373// because the current cursor position has not been set by this client.
374// Unfortunately we can't know for sure when the current cursor position has
375// been set by this client. We guess that this is the case when the current
376// cursor position is the same as the last pointer event from this client, or
377// if it is a very short time since this client's last pointer event (up to a
378// second). [ Ideally we should do finer-grained timing here and make the time
379// configurable, but I don't think it's that important. ]
380
381bool VNCSConnectionST::needRenderedCursor()
382{
Pierre Ossman77ede0a2016-12-05 16:57:30 +0100383 if (state() != RFBSTATE_NORMAL)
384 return false;
385
Pierre Ossman324043e2017-08-16 16:26:11 +0200386 if (!cp.supportsLocalCursorWithAlpha &&
387 !cp.supportsLocalCursor && !cp.supportsLocalXCursor)
Pierre Ossman77ede0a2016-12-05 16:57:30 +0100388 return true;
Pierre Ossman6094ced2018-10-05 17:24:51 +0200389 if (!server->getCursorPos().equals(pointerEventPos) &&
Pierre Ossman77ede0a2016-12-05 16:57:30 +0100390 (time(0) - pointerEventTime) > 0)
391 return true;
392
393 return false;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000394}
395
396
397void VNCSConnectionST::approveConnectionOrClose(bool accept,
398 const char* reason)
399{
400 try {
401 approveConnection(accept, reason);
402 } catch (rdr::Exception& e) {
403 close(e.str());
404 }
405}
406
407
408
409// -=- Callbacks from SConnection
410
411void VNCSConnectionST::authSuccess()
412{
413 lastEventTime = time(0);
414
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000415 // - Set the connection parameters appropriately
Pierre Ossman6094ced2018-10-05 17:24:51 +0200416 cp.width = server->getPixelBuffer()->width();
417 cp.height = server->getPixelBuffer()->height();
418 cp.screenLayout = server->getScreenLayout();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000419 cp.setName(server->getName());
Pierre Ossman6094ced2018-10-05 17:24:51 +0200420 cp.setLEDState(server->getLEDState());
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000421
422 // - Set the default pixel format
Pierre Ossman6094ced2018-10-05 17:24:51 +0200423 cp.setPF(server->getPixelBuffer()->getPF());
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000424 char buffer[256];
425 cp.pf().print(buffer, 256);
426 vlog.info("Server default pixel format %s", buffer);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000427
428 // - Mark the entire display as "dirty"
Pierre Ossman6094ced2018-10-05 17:24:51 +0200429 updates.add_changed(server->getPixelBuffer()->getRect());
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000430 startTime = time(0);
431}
432
433void VNCSConnectionST::queryConnection(const char* userName)
434{
435 // - Authentication succeeded - clear from blacklist
436 CharArray name; name.buf = sock->getPeerAddress();
437 server->blHosts->clearBlackmark(name.buf);
438
Pierre Ossmaneef6c9a2018-10-05 17:11:25 +0200439 // - Prepare the desktop that we might be making calls
440 server->startDesktop();
441
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000442 // - Special case to provide a more useful error message
443 if (rfb::Server::neverShared && !rfb::Server::disconnectClients &&
444 server->authClientCount() > 0) {
445 approveConnection(false, "The server is already in use");
446 return;
447 }
448
449 // - Does the client have the right to bypass the query?
450 if (reverseConnection ||
451 !(rfb::Server::queryConnect || sock->requiresQuery()) ||
452 (accessRights & AccessNoQuery))
453 {
454 approveConnection(true);
455 return;
456 }
457
458 // - Get the server to display an Accept/Reject dialog, if required
Pierre Ossmane6aab242018-10-05 16:59:22 +0200459 server->queryConnection(sock, userName);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000460}
461
462void VNCSConnectionST::clientInit(bool shared)
463{
464 lastEventTime = time(0);
465 if (rfb::Server::alwaysShared || reverseConnection) shared = true;
Pierre Ossmane7be49b2014-12-02 14:33:17 +0100466 if (!(accessRights & AccessNonShared)) shared = true;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000467 if (rfb::Server::neverShared) shared = false;
468 if (!shared) {
Pierre Ossmane7be49b2014-12-02 14:33:17 +0100469 if (rfb::Server::disconnectClients && (accessRights & AccessNonShared)) {
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000470 // - Close all the other connected clients
471 vlog.debug("non-shared connection - closing clients");
472 server->closeClients("Non-shared connection requested", getSock());
473 } else {
474 // - Refuse this connection if there are existing clients, in addition to
475 // this one
476 if (server->authClientCount() > 1) {
477 close("Server is already in use");
478 return;
479 }
480 }
481 }
482 SConnection::clientInit(shared);
483}
484
485void VNCSConnectionST::setPixelFormat(const PixelFormat& pf)
486{
487 SConnection::setPixelFormat(pf);
488 char buffer[256];
489 pf.print(buffer, 256);
490 vlog.info("Client pixel format %s", buffer);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000491 setCursor();
492}
493
494void VNCSConnectionST::pointerEvent(const Point& pos, int buttonMask)
495{
496 pointerEventTime = lastEventTime = time(0);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000497 if (!(accessRights & AccessPtrEvents)) return;
498 if (!rfb::Server::acceptPointerEvents) return;
Pierre Ossmanb6843412018-10-05 17:30:52 +0200499 pointerEventPos = pos;
500 server->pointerEvent(this, pointerEventPos, buttonMask);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000501}
502
503
504class VNCSConnectionSTShiftPresser {
505public:
Pierre Ossmanb6843412018-10-05 17:30:52 +0200506 VNCSConnectionSTShiftPresser(VNCServerST* server_)
507 : server(server_), pressed(false) {}
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000508 ~VNCSConnectionSTShiftPresser() {
Pierre Ossman9a153b02015-08-31 10:01:14 +0200509 if (pressed) {
510 vlog.debug("Releasing fake Shift_L");
Pierre Ossmanb6843412018-10-05 17:30:52 +0200511 server->keyEvent(XK_Shift_L, 0, false);
Pierre Ossman9a153b02015-08-31 10:01:14 +0200512 }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000513 }
514 void press() {
Pierre Ossman9a153b02015-08-31 10:01:14 +0200515 vlog.debug("Pressing fake Shift_L");
Pierre Ossmanb6843412018-10-05 17:30:52 +0200516 server->keyEvent(XK_Shift_L, 0, true);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000517 pressed = true;
518 }
Pierre Ossmanb6843412018-10-05 17:30:52 +0200519 VNCServerST* server;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000520 bool pressed;
521};
522
523// keyEvent() - record in the pressedKeys which keys were pressed. Allow
524// multiple down events (for autorepeat), but only allow a single up event.
Pierre Ossman5ae28212017-05-16 14:30:38 +0200525void VNCSConnectionST::keyEvent(rdr::U32 keysym, rdr::U32 keycode, bool down) {
Pierre Ossman16e1dcb2017-05-16 14:33:43 +0200526 rdr::U32 lookup;
527
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000528 lastEventTime = time(0);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000529 if (!(accessRights & AccessKeyEvents)) return;
530 if (!rfb::Server::acceptKeyEvents) return;
531
Pierre Ossman9a153b02015-08-31 10:01:14 +0200532 if (down)
Pierre Ossman5ae28212017-05-16 14:30:38 +0200533 vlog.debug("Key pressed: 0x%x / 0x%x", keysym, keycode);
Pierre Ossman9a153b02015-08-31 10:01:14 +0200534 else
Pierre Ossman5ae28212017-05-16 14:30:38 +0200535 vlog.debug("Key released: 0x%x / 0x%x", keysym, keycode);
Pierre Ossman9a153b02015-08-31 10:01:14 +0200536
Pierre Ossmanbb305ca2016-12-11 12:41:26 +0100537 // Avoid lock keys if we don't know the server state
Pierre Ossman6094ced2018-10-05 17:24:51 +0200538 if ((server->getLEDState() == ledUnknown) &&
Pierre Ossman5ae28212017-05-16 14:30:38 +0200539 ((keysym == XK_Caps_Lock) ||
540 (keysym == XK_Num_Lock) ||
541 (keysym == XK_Scroll_Lock))) {
Pierre Ossmanbb305ca2016-12-11 12:41:26 +0100542 vlog.debug("Ignoring lock key (e.g. caps lock)");
543 return;
544 }
Pierre Ossmanbb305ca2016-12-11 12:41:26 +0100545
Pierre Ossmanb45a84f2016-12-12 16:59:15 +0100546 // Lock key heuristics
547 // (only for clients that do not support the LED state extension)
548 if (!cp.supportsLEDState) {
549 // Always ignore ScrollLock as we don't have a heuristic
550 // for that
Pierre Ossman5ae28212017-05-16 14:30:38 +0200551 if (keysym == XK_Scroll_Lock) {
Pierre Ossmanb45a84f2016-12-12 16:59:15 +0100552 vlog.debug("Ignoring lock key (e.g. caps lock)");
553 return;
Pierre Ossmanbb305ca2016-12-11 12:41:26 +0100554 }
555
Pierre Ossman6094ced2018-10-05 17:24:51 +0200556 if (down && (server->getLEDState() != ledUnknown)) {
Pierre Ossmanb45a84f2016-12-12 16:59:15 +0100557 // CapsLock synchronisation heuristic
558 // (this assumes standard interaction between CapsLock the Shift
559 // keys and normal characters)
Pierre Ossman5ae28212017-05-16 14:30:38 +0200560 if (((keysym >= XK_A) && (keysym <= XK_Z)) ||
561 ((keysym >= XK_a) && (keysym <= XK_z))) {
Pierre Ossmanb45a84f2016-12-12 16:59:15 +0100562 bool uppercase, shift, lock;
Pierre Ossmanbb305ca2016-12-11 12:41:26 +0100563
Pierre Ossman5ae28212017-05-16 14:30:38 +0200564 uppercase = (keysym >= XK_A) && (keysym <= XK_Z);
Pierre Ossman851e6802017-09-12 16:44:44 +0200565 shift = isShiftPressed();
Pierre Ossman6094ced2018-10-05 17:24:51 +0200566 lock = server->getLEDState() & ledCapsLock;
Pierre Ossmanbb305ca2016-12-11 12:41:26 +0100567
Pierre Ossmanb45a84f2016-12-12 16:59:15 +0100568 if (lock == (uppercase == shift)) {
569 vlog.debug("Inserting fake CapsLock to get in sync with client");
Pierre Ossmanb6843412018-10-05 17:30:52 +0200570 server->keyEvent(XK_Caps_Lock, 0, true);
571 server->keyEvent(XK_Caps_Lock, 0, false);
Pierre Ossmanb45a84f2016-12-12 16:59:15 +0100572 }
573 }
574
575 // NumLock synchronisation heuristic
576 // (this is more cautious because of the differences between Unix,
577 // Windows and macOS)
Pierre Ossman5ae28212017-05-16 14:30:38 +0200578 if (((keysym >= XK_KP_Home) && (keysym <= XK_KP_Delete)) ||
579 ((keysym >= XK_KP_0) && (keysym <= XK_KP_9)) ||
580 (keysym == XK_KP_Separator) || (keysym == XK_KP_Decimal)) {
Pierre Ossmanb45a84f2016-12-12 16:59:15 +0100581 bool number, shift, lock;
582
Pierre Ossman5ae28212017-05-16 14:30:38 +0200583 number = ((keysym >= XK_KP_0) && (keysym <= XK_KP_9)) ||
584 (keysym == XK_KP_Separator) || (keysym == XK_KP_Decimal);
Pierre Ossman851e6802017-09-12 16:44:44 +0200585 shift = isShiftPressed();
Pierre Ossman6094ced2018-10-05 17:24:51 +0200586 lock = server->getLEDState() & ledNumLock;
Pierre Ossmanb45a84f2016-12-12 16:59:15 +0100587
588 if (shift) {
589 // We don't know the appropriate NumLock state for when Shift
590 // is pressed as it could be one of:
591 //
592 // a) A Unix client where Shift negates NumLock
593 //
594 // b) A Windows client where Shift only cancels NumLock
595 //
596 // c) A macOS client where Shift doesn't have any effect
597 //
598 } else if (lock == (number == shift)) {
599 vlog.debug("Inserting fake NumLock to get in sync with client");
Pierre Ossmanb6843412018-10-05 17:30:52 +0200600 server->keyEvent(XK_Num_Lock, 0, true);
601 server->keyEvent(XK_Num_Lock, 0, false);
Pierre Ossmanb45a84f2016-12-12 16:59:15 +0100602 }
Pierre Ossmanbb305ca2016-12-11 12:41:26 +0100603 }
Pierre Ossman9a153b02015-08-31 10:01:14 +0200604 }
605 }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000606
607 // Turn ISO_Left_Tab into shifted Tab.
Pierre Ossmanb6843412018-10-05 17:30:52 +0200608 VNCSConnectionSTShiftPresser shiftPresser(server);
Pierre Ossman5ae28212017-05-16 14:30:38 +0200609 if (keysym == XK_ISO_Left_Tab) {
Pierre Ossman851e6802017-09-12 16:44:44 +0200610 if (!isShiftPressed())
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000611 shiftPresser.press();
Pierre Ossman5ae28212017-05-16 14:30:38 +0200612 keysym = XK_Tab;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000613 }
614
Pierre Ossman16e1dcb2017-05-16 14:33:43 +0200615 // We need to be able to track keys, so generate a fake index when we
616 // aren't given a keycode
617 if (keycode == 0)
618 lookup = 0x80000000 | keysym;
619 else
620 lookup = keycode;
621
622 // We force the same keysym for an already down key for the
623 // sake of sanity
624 if (pressedKeys.find(lookup) != pressedKeys.end())
625 keysym = pressedKeys[lookup];
626
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000627 if (down) {
Pierre Ossman16e1dcb2017-05-16 14:33:43 +0200628 pressedKeys[lookup] = keysym;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000629 } else {
Pierre Ossman16e1dcb2017-05-16 14:33:43 +0200630 if (!pressedKeys.erase(lookup))
Pierre Ossman5ae28212017-05-16 14:30:38 +0200631 return;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000632 }
Pierre Ossman16e1dcb2017-05-16 14:33:43 +0200633
Pierre Ossmanb6843412018-10-05 17:30:52 +0200634 server->keyEvent(keysym, keycode, down);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000635}
636
637void VNCSConnectionST::clientCutText(const char* str, int len)
638{
639 if (!(accessRights & AccessCutText)) return;
640 if (!rfb::Server::acceptCutText) return;
Pierre Ossmanb6843412018-10-05 17:30:52 +0200641 server->clientCutText(str, len);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000642}
643
644void VNCSConnectionST::framebufferUpdateRequest(const Rect& r,bool incremental)
645{
Pierre Ossmane9962f72009-04-23 12:31:42 +0000646 Rect safeRect;
647
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000648 if (!(accessRights & AccessView)) return;
649
650 SConnection::framebufferUpdateRequest(r, incremental);
651
Pierre Ossmand9a59ba2009-03-20 15:55:37 +0000652 // Check that the client isn't sending crappy requests
653 if (!r.enclosed_by(Rect(0, 0, cp.width, cp.height))) {
654 vlog.error("FramebufferUpdateRequest %dx%d at %d,%d exceeds framebuffer %dx%d",
655 r.width(), r.height(), r.tl.x, r.tl.y, cp.width, cp.height);
Pierre Ossmane9962f72009-04-23 12:31:42 +0000656 safeRect = r.intersect(Rect(0, 0, cp.width, cp.height));
657 } else {
658 safeRect = r;
Pierre Ossmand9a59ba2009-03-20 15:55:37 +0000659 }
660
Constantin Kaplinsky2ef66952008-08-29 11:33:46 +0000661 // Just update the requested region.
662 // Framebuffer update will be sent a bit later, see processMessages().
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000663 Region reqRgn(r);
Pierre Ossman1b478e52011-11-15 12:08:30 +0000664 if (!incremental || !continuousUpdates)
665 requested.assign_union(reqRgn);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000666
667 if (!incremental) {
668 // Non-incremental update - treat as if area requested has changed
669 updates.add_changed(reqRgn);
Pierre Ossman53125a72009-04-22 15:37:51 +0000670
671 // And send the screen layout to the client (which, unlike the
672 // framebuffer dimensions, the client doesn't get during init)
Pierre Ossmanc5e25602009-03-20 12:59:05 +0000673 writer()->writeExtendedDesktopSize();
Pierre Ossman53125a72009-04-22 15:37:51 +0000674
675 // We do not send a DesktopSize since it only contains the
676 // framebuffer size (which the client already should know) and
677 // because some clients don't handle extra DesktopSize events
678 // very well.
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000679 }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000680}
681
Pierre Ossman34bb0612009-03-21 21:16:14 +0000682void VNCSConnectionST::setDesktopSize(int fb_width, int fb_height,
683 const ScreenSet& layout)
Pierre Ossmanc5e25602009-03-20 12:59:05 +0000684{
Pierre Ossman04e62db2009-03-23 16:57:07 +0000685 unsigned int result;
686
Michal Srbb318b8f2014-11-24 13:18:28 +0200687 if (!(accessRights & AccessSetDesktopSize)) return;
688 if (!rfb::Server::acceptSetDesktopSize) return;
689
Pierre Ossman04e62db2009-03-23 16:57:07 +0000690 // Don't bother the desktop with an invalid configuration
691 if (!layout.validate(fb_width, fb_height)) {
692 writer()->writeExtendedDesktopSize(reasonClient, resultInvalid,
693 fb_width, fb_height, layout);
Pierre Ossman04e62db2009-03-23 16:57:07 +0000694 return;
695 }
696
697 // FIXME: the desktop will call back to VNCServerST and an extra set
698 // of ExtendedDesktopSize messages will be sent. This is okay
699 // protocol-wise, but unnecessary.
700 result = server->desktop->setScreenLayout(fb_width, fb_height, layout);
701
Pierre Ossman04e62db2009-03-23 16:57:07 +0000702 writer()->writeExtendedDesktopSize(reasonClient, result,
703 fb_width, fb_height, layout);
Pierre Ossman04e62db2009-03-23 16:57:07 +0000704
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000705 // Only notify other clients on success
Pierre Ossman04e62db2009-03-23 16:57:07 +0000706 if (result == resultSuccess) {
707 if (server->screenLayout != layout)
708 throw Exception("Desktop configured a different screen layout than requested");
709 server->notifyScreenLayoutChange(this);
710 }
Pierre Ossmanc5e25602009-03-20 12:59:05 +0000711}
712
Pierre Ossman2c764942011-11-14 15:54:30 +0000713void VNCSConnectionST::fence(rdr::U32 flags, unsigned len, const char data[])
714{
Pierre Ossmanb2a417c2015-12-11 19:32:21 +0100715 rdr::U8 type;
716
Pierre Ossman2c764942011-11-14 15:54:30 +0000717 if (flags & fenceFlagRequest) {
718 if (flags & fenceFlagSyncNext) {
Pierre Ossmanb8b1e962012-07-20 10:47:00 +0000719 pendingSyncFence = true;
Pierre Ossman2c764942011-11-14 15:54:30 +0000720
721 fenceFlags = flags & (fenceFlagBlockBefore | fenceFlagBlockAfter | fenceFlagSyncNext);
722 fenceDataLen = len;
723 delete [] fenceData;
Michal Srbf3afa242017-03-27 19:02:15 +0300724 fenceData = NULL;
Pierre Ossman2c764942011-11-14 15:54:30 +0000725 if (len > 0) {
726 fenceData = new char[len];
727 memcpy(fenceData, data, len);
728 }
729
730 return;
731 }
732
733 // We handle everything synchronously so we trivially honor these modes
734 flags = flags & (fenceFlagBlockBefore | fenceFlagBlockAfter);
735
736 writer()->writeFence(flags, len, data);
737 return;
738 }
739
Pierre Ossmanb2a417c2015-12-11 19:32:21 +0100740 if (len < 1)
741 vlog.error("Fence response of unexpected size received");
Pierre Ossman1b478e52011-11-15 12:08:30 +0000742
Pierre Ossmanb2a417c2015-12-11 19:32:21 +0100743 type = data[0];
744
745 switch (type) {
Pierre Ossman2c764942011-11-14 15:54:30 +0000746 case 0:
747 // Initial dummy fence;
748 break;
Pierre Ossmanb2a417c2015-12-11 19:32:21 +0100749 case 1:
Pierre Ossmanc09e5582015-12-11 20:23:17 +0100750 congestion.gotPong();
Pierre Ossman1b478e52011-11-15 12:08:30 +0000751 break;
Pierre Ossman2c764942011-11-14 15:54:30 +0000752 default:
Pierre Ossmanb2a417c2015-12-11 19:32:21 +0100753 vlog.error("Fence response of unexpected type received");
Pierre Ossman2c764942011-11-14 15:54:30 +0000754 }
755}
756
Pierre Ossman1b478e52011-11-15 12:08:30 +0000757void VNCSConnectionST::enableContinuousUpdates(bool enable,
758 int x, int y, int w, int h)
759{
760 Rect rect;
761
762 if (!cp.supportsFence || !cp.supportsContinuousUpdates)
763 throw Exception("Client tried to enable continuous updates when not allowed");
764
765 continuousUpdates = enable;
766
767 rect.setXYWH(x, y, w, h);
768 cuRegion.reset(rect);
769
770 if (enable) {
771 requested.clear();
Pierre Ossman1b478e52011-11-15 12:08:30 +0000772 } else {
773 writer()->writeEndOfContinuousUpdates();
774 }
775}
776
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000777// supportsLocalCursor() is called whenever the status of
778// cp.supportsLocalCursor has changed. If the client does now support local
779// cursor, we make sure that the old server-side rendered cursor is cleaned up
780// and the cursor is sent to the client.
781
782void VNCSConnectionST::supportsLocalCursor()
783{
Pierre Ossman387a4172017-11-16 16:44:36 +0100784 bool hasRenderedCursor = !damagedCursorRegion.is_empty();
785 if (hasRenderedCursor && !needRenderedCursor())
786 removeRenderedCursor = true;
787 setCursor();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000788}
789
Pierre Ossman2c764942011-11-14 15:54:30 +0000790void VNCSConnectionST::supportsFence()
791{
Pierre Ossmanb2a417c2015-12-11 19:32:21 +0100792 char type = 0;
793 writer()->writeFence(fenceFlagRequest, sizeof(type), &type);
Pierre Ossman2c764942011-11-14 15:54:30 +0000794}
795
Pierre Ossman1b478e52011-11-15 12:08:30 +0000796void VNCSConnectionST::supportsContinuousUpdates()
797{
798 // We refuse to use continuous updates if we cannot monitor the buffer
799 // usage using fences.
800 if (!cp.supportsFence)
801 return;
802
803 writer()->writeEndOfContinuousUpdates();
804}
805
Pierre Ossmanb45a84f2016-12-12 16:59:15 +0100806void VNCSConnectionST::supportsLEDState()
807{
808 writer()->writeLEDState();
809}
810
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000811
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +0000812bool VNCSConnectionST::handleTimeout(Timer* t)
813{
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000814 try {
Peter Åstrand (astrand)7a368c92018-09-19 12:45:17 +0200815 if ((t == &congestionTimer) ||
816 (t == &losslessTimer))
Pierre Ossmana99d14d2015-12-13 15:43:46 +0100817 writeFramebufferUpdate();
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000818 } catch (rdr::Exception& e) {
819 close(e.str());
820 }
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +0000821
822 return false;
823}
824
Pierre Ossman851e6802017-09-12 16:44:44 +0200825bool VNCSConnectionST::isShiftPressed()
826{
827 std::map<rdr::U32, rdr::U32>::const_iterator iter;
828
829 for (iter = pressedKeys.begin(); iter != pressedKeys.end(); ++iter) {
830 if (iter->second == XK_Shift_L)
831 return true;
832 if (iter->second == XK_Shift_R)
833 return true;
834 }
835
836 return false;
837}
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +0000838
Pierre Ossman1b478e52011-11-15 12:08:30 +0000839void VNCSConnectionST::writeRTTPing()
840{
Pierre Ossmanb2a417c2015-12-11 19:32:21 +0100841 char type;
Pierre Ossman1b478e52011-11-15 12:08:30 +0000842
843 if (!cp.supportsFence)
844 return;
845
Pierre Ossmana99d14d2015-12-13 15:43:46 +0100846 congestion.updatePosition(sock->outStream().length());
Pierre Ossman1b478e52011-11-15 12:08:30 +0000847
848 // We need to make sure any old update are already processed by the
849 // time we get the response back. This allows us to reliably throttle
850 // back on client overload, as well as network overload.
Pierre Ossmanb2a417c2015-12-11 19:32:21 +0100851 type = 1;
Pierre Ossman1b478e52011-11-15 12:08:30 +0000852 writer()->writeFence(fenceFlagRequest | fenceFlagBlockBefore,
Pierre Ossmanb2a417c2015-12-11 19:32:21 +0100853 sizeof(type), &type);
Pierre Ossman1b478e52011-11-15 12:08:30 +0000854
Pierre Ossmana99d14d2015-12-13 15:43:46 +0100855 congestion.sentPing();
Pierre Ossman1b478e52011-11-15 12:08:30 +0000856}
857
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +0000858bool VNCSConnectionST::isCongested()
859{
Pierre Ossmance261812018-07-17 15:01:53 +0200860 int eta;
Pierre Ossmana99d14d2015-12-13 15:43:46 +0100861
862 congestionTimer.stop();
Pierre Ossman1b478e52011-11-15 12:08:30 +0000863
864 // Stuff still waiting in the send buffer?
Pierre Ossman352d0622016-04-29 15:50:54 +0200865 sock->outStream().flush();
Pierre Ossman8cf71632016-03-11 09:38:08 +0100866 congestion.debugTrace("congestion-trace.csv", sock->getFd());
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +0000867 if (sock->outStream().bufferUsage() > 0)
868 return true;
869
Pierre Ossman1b478e52011-11-15 12:08:30 +0000870 if (!cp.supportsFence)
871 return false;
872
Pierre Ossmana99d14d2015-12-13 15:43:46 +0100873 congestion.updatePosition(sock->outStream().length());
874 if (!congestion.isCongested())
Pierre Ossman1b478e52011-11-15 12:08:30 +0000875 return false;
876
Pierre Ossmana99d14d2015-12-13 15:43:46 +0100877 eta = congestion.getUncongestedETA();
878 if (eta >= 0)
879 congestionTimer.start(eta);
Pierre Ossman1b478e52011-11-15 12:08:30 +0000880
881 return true;
882}
883
884
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000885void VNCSConnectionST::writeFramebufferUpdate()
886{
Pierre Ossmana99d14d2015-12-13 15:43:46 +0100887 congestion.updatePosition(sock->outStream().length());
888
Pierre Ossman2c764942011-11-14 15:54:30 +0000889 // We're in the middle of processing a command that's supposed to be
890 // synchronised. Allowing an update to slip out right now might violate
891 // that synchronisation.
892 if (syncFence)
893 return;
894
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000895 // We try to aggregate responses, so don't send out anything whilst we
896 // still have incoming messages. processMessages() will give us another
897 // chance to run once things are idle.
898 if (inProcessMessages)
899 return;
900
Pierre Ossman1b478e52011-11-15 12:08:30 +0000901 if (state() != RFBSTATE_NORMAL)
902 return;
903 if (requested.is_empty() && !continuousUpdates)
Pierre Ossmane9962f72009-04-23 12:31:42 +0000904 return;
Pierre Ossmand9a59ba2009-03-20 15:55:37 +0000905
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +0000906 // Check that we actually have some space on the link and retry in a
907 // bit if things are congested.
Pierre Ossmana40ab202016-04-29 15:35:56 +0200908 if (isCongested())
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +0000909 return;
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +0000910
Pierre Ossmancef3cf72016-11-25 10:06:34 +0100911 // Updates often consists of many small writes, and in continuous
912 // mode, we will also have small fence messages around the update. We
913 // need to aggregate these in order to not clog up TCP's congestion
914 // window.
Peter Åstrand (astrand)01dc1a62017-10-10 12:56:04 +0200915 sock->cork(true);
Pierre Ossman36dadf82011-11-15 12:11:32 +0000916
Pierre Ossmane9962f72009-04-23 12:31:42 +0000917 // First take care of any updates that cannot contain framebuffer data
918 // changes.
Pierre Ossmancef3cf72016-11-25 10:06:34 +0100919 writeNoDataUpdate();
920
921 // Then real data (if possible)
922 writeDataUpdate();
923
Peter Åstrand (astrand)01dc1a62017-10-10 12:56:04 +0200924 sock->cork(false);
Pierre Ossmana99d14d2015-12-13 15:43:46 +0100925
926 congestion.updatePosition(sock->outStream().length());
Pierre Ossmancef3cf72016-11-25 10:06:34 +0100927}
928
929void VNCSConnectionST::writeNoDataUpdate()
930{
931 if (!writer()->needNoDataUpdate())
932 return;
933
934 writer()->writeNoDataUpdate();
935
936 // Make sure no data update is sent until next request
937 requested.clear();
938}
939
940void VNCSConnectionST::writeDataUpdate()
941{
Pierre Ossman8efc7b42018-03-23 11:45:51 +0100942 Region req, pending;
Pierre Ossmancef3cf72016-11-25 10:06:34 +0100943 UpdateInfo ui;
944 bool needNewUpdateInfo;
Pierre Ossman24684e52016-12-05 16:58:19 +0100945 const RenderedCursor *cursor;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000946
Constantin Kaplinsky604d7812007-08-31 15:50:37 +0000947 updates.enable_copyrect(cp.useCopyRect);
948
Pierre Ossmancef3cf72016-11-25 10:06:34 +0100949 // See what the client has requested (if anything)
Pierre Ossman1b478e52011-11-15 12:08:30 +0000950 if (continuousUpdates)
951 req = cuRegion.union_(requested);
952 else
953 req = requested;
954
Pierre Ossmancef3cf72016-11-25 10:06:34 +0100955 if (req.is_empty())
956 return;
957
Pierre Ossman8efc7b42018-03-23 11:45:51 +0100958 // Get any framebuffer changes we haven't yet been informed of
959 pending = server->getPendingRegion();
960
Pierre Ossmancef3cf72016-11-25 10:06:34 +0100961 // Get the lists of updates. Prior to exporting the data to the `ui' object,
962 // getUpdateInfo() will normalize the `updates' object such way that its
963 // `changed' and `copied' regions would not intersect.
Pierre Ossman1b478e52011-11-15 12:08:30 +0000964 updates.getUpdateInfo(&ui, req);
965 needNewUpdateInfo = false;
Constantin Kaplinsky604d7812007-08-31 15:50:37 +0000966
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000967 // If the previous position of the rendered cursor overlaps the source of the
968 // copy, then when the copy happens the corresponding rectangle in the
969 // destination will be wrong, so add it to the changed region.
970
Pierre Ossman671d2ef2015-06-09 16:09:06 +0200971 if (!ui.copied.is_empty() && !damagedCursorRegion.is_empty()) {
972 Region bogusCopiedCursor;
973
Pierre Ossman74385d32018-03-22 16:00:18 +0100974 bogusCopiedCursor = damagedCursorRegion;
Pierre Ossman671d2ef2015-06-09 16:09:06 +0200975 bogusCopiedCursor.translate(ui.copy_delta);
Pierre Ossman6094ced2018-10-05 17:24:51 +0200976 bogusCopiedCursor.assign_intersect(server->getPixelBuffer()->getRect());
Constantin Kaplinsky45517c82007-08-31 15:56:33 +0000977 if (!ui.copied.intersect(bogusCopiedCursor).is_empty()) {
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000978 updates.add_changed(bogusCopiedCursor);
Constantin Kaplinsky604d7812007-08-31 15:50:37 +0000979 needNewUpdateInfo = true;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000980 }
981 }
982
Pierre Ossman671d2ef2015-06-09 16:09:06 +0200983 // If we need to remove the old rendered cursor, just add the region to
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000984 // the changed region.
985
986 if (removeRenderedCursor) {
Pierre Ossman671d2ef2015-06-09 16:09:06 +0200987 updates.add_changed(damagedCursorRegion);
Constantin Kaplinsky604d7812007-08-31 15:50:37 +0000988 needNewUpdateInfo = true;
Pierre Ossman671d2ef2015-06-09 16:09:06 +0200989 damagedCursorRegion.clear();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000990 removeRenderedCursor = false;
991 }
992
Pierre Ossman8c3bd692018-03-22 15:58:54 +0100993 // If we need a full cursor update then make sure its entire region
994 // is marked as changed.
995
996 if (updateRenderedCursor) {
997 updates.add_changed(server->getRenderedCursor()->getEffectiveRect());
998 needNewUpdateInfo = true;
999 updateRenderedCursor = false;
1000 }
1001
Constantin Kaplinsky604d7812007-08-31 15:50:37 +00001002 // The `updates' object could change, make sure we have valid update info.
1003
1004 if (needNewUpdateInfo)
Pierre Ossman1b478e52011-11-15 12:08:30 +00001005 updates.getUpdateInfo(&ui, req);
Constantin Kaplinsky604d7812007-08-31 15:50:37 +00001006
Pierre Ossman8efc7b42018-03-23 11:45:51 +01001007 // If there are queued updates then we cannot safely send an update
1008 // without risking a partially updated screen
1009
1010 if (!pending.is_empty()) {
1011 // However we might still be able to send a lossless refresh
1012 req.assign_subtract(pending);
1013 req.assign_subtract(ui.changed);
1014 req.assign_subtract(ui.copied);
1015
1016 ui.changed.clear();
1017 ui.copied.clear();
1018 }
1019
Pierre Ossman8c3bd692018-03-22 15:58:54 +01001020 // Does the client need a server-side rendered cursor?
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001021
Pierre Ossman24684e52016-12-05 16:58:19 +01001022 cursor = NULL;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001023 if (needRenderedCursor()) {
Pierre Ossman671d2ef2015-06-09 16:09:06 +02001024 Rect renderedCursorRect;
1025
Pierre Ossman24684e52016-12-05 16:58:19 +01001026 cursor = server->getRenderedCursor();
Pierre Ossman8c3bd692018-03-22 15:58:54 +01001027 renderedCursorRect = cursor->getEffectiveRect();
Pierre Ossman24684e52016-12-05 16:58:19 +01001028
Pierre Ossman8c3bd692018-03-22 15:58:54 +01001029 // Check that we don't try to copy over the cursor area, and
1030 // if that happens we need to treat it as changed so that we can
1031 // re-render it
1032 if (!ui.copied.intersect(renderedCursorRect).is_empty()) {
1033 ui.changed.assign_union(ui.copied.intersect(renderedCursorRect));
1034 ui.copied.assign_subtract(renderedCursorRect);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001035 }
1036
Pierre Ossman8c3bd692018-03-22 15:58:54 +01001037 // Track where we've rendered the cursor
1038 damagedCursorRegion.assign_union(ui.changed.intersect(renderedCursorRect));
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001039 }
1040
Pierre Ossman8efc7b42018-03-23 11:45:51 +01001041 // Return if there is nothing to send the client.
Peter Åstrand (astrand)7a368c92018-09-19 12:45:17 +02001042 if (ui.is_empty() && !writer()->needFakeUpdate()) {
1043 int eta;
Pierre Ossman8efc7b42018-03-23 11:45:51 +01001044
Peter Åstrand (astrand)7a368c92018-09-19 12:45:17 +02001045 // Any lossless refresh that needs handling?
1046 if (!encodeManager.needsLosslessRefresh(req))
1047 return;
1048
1049 // Now? Or later?
1050 eta = encodeManager.getNextLosslessRefresh(req);
1051 if (eta > 0) {
1052 losslessTimer.start(eta);
1053 return;
1054 }
1055 }
Pierre Ossmanfdba3fe2014-01-31 13:12:18 +01001056
Pierre Ossmancef3cf72016-11-25 10:06:34 +01001057 writeRTTPing();
Pierre Ossman1b478e52011-11-15 12:08:30 +00001058
Pierre Ossman6b2f1132016-11-30 08:03:35 +01001059 if (!ui.is_empty())
1060 encodeManager.writeUpdate(ui, server->getPixelBuffer(), cursor);
Pierre Ossmana2b80d62018-03-23 09:30:09 +01001061 else {
Pierre Ossmanc3ce5ce2018-09-19 16:31:18 +02001062 int nextUpdate;
Pierre Ossmana2b80d62018-03-23 09:30:09 +01001063
1064 // FIXME: If continuous updates aren't used then the client might
1065 // be slower than frameRate in its requests and we could
1066 // afford a larger update size
Pierre Ossmanc3ce5ce2018-09-19 16:31:18 +02001067 nextUpdate = server->msToNextUpdate();
1068 if (nextUpdate > 0) {
Pierre Ossmancb5a3992018-09-19 16:35:40 +02001069 size_t bandwidth, maxUpdateSize;
Pierre Ossmana2b80d62018-03-23 09:30:09 +01001070
Pierre Ossmanc3ce5ce2018-09-19 16:31:18 +02001071 // FIXME: Bandwidth estimation without congestion control
Pierre Ossmancb5a3992018-09-19 16:35:40 +02001072 bandwidth = congestion.getBandwidth();
Pierre Ossmana2b80d62018-03-23 09:30:09 +01001073
Pierre Ossmancb5a3992018-09-19 16:35:40 +02001074 // FIXME: Hard coded value for maximum CPU throughput
1075 if (bandwidth > 5000000)
1076 bandwidth = 5000000;
1077
1078 maxUpdateSize = bandwidth * nextUpdate / 1000;
Pierre Ossmanc3ce5ce2018-09-19 16:31:18 +02001079 encodeManager.writeLosslessRefresh(req, server->getPixelBuffer(),
1080 cursor, maxUpdateSize);
1081 }
Pierre Ossmana2b80d62018-03-23 09:30:09 +01001082 }
Pierre Ossman1b478e52011-11-15 12:08:30 +00001083
Pierre Ossmancef3cf72016-11-25 10:06:34 +01001084 writeRTTPing();
Pierre Ossmanc0397262014-03-14 15:59:46 +01001085
Pierre Ossmancef3cf72016-11-25 10:06:34 +01001086 // The request might be for just part of the screen, so we cannot
1087 // just clear the entire update tracker.
1088 updates.subtract(req);
Pierre Ossmanc0397262014-03-14 15:59:46 +01001089
Pierre Ossmancef3cf72016-11-25 10:06:34 +01001090 requested.clear();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001091}
1092
1093
Pierre Ossmana3ac01e2011-11-07 21:13:54 +00001094void VNCSConnectionST::screenLayoutChange(rdr::U16 reason)
1095{
1096 if (!authenticated())
1097 return;
1098
Pierre Ossman6094ced2018-10-05 17:24:51 +02001099 cp.screenLayout = server->getScreenLayout();
Pierre Ossmana3ac01e2011-11-07 21:13:54 +00001100
1101 if (state() != RFBSTATE_NORMAL)
1102 return;
1103
1104 writer()->writeExtendedDesktopSize(reason, 0, cp.width, cp.height,
1105 cp.screenLayout);
Pierre Ossmana3ac01e2011-11-07 21:13:54 +00001106}
1107
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001108
1109// setCursor() is called whenever the cursor has changed shape or pixel format.
1110// If the client supports local cursor then it will arrange for the cursor to
1111// be sent to the client.
1112
1113void VNCSConnectionST::setCursor()
1114{
Pierre Ossmana3ac01e2011-11-07 21:13:54 +00001115 if (state() != RFBSTATE_NORMAL)
1116 return;
Pierre Ossmana3ac01e2011-11-07 21:13:54 +00001117
Pierre Ossman71ca8d52017-09-15 11:03:12 +02001118 // We need to blank out the client's cursor or there will be two
Pierre Ossman25db44a2017-11-16 16:40:44 +01001119 if (needRenderedCursor()) {
Pierre Ossman71ca8d52017-09-15 11:03:12 +02001120 cp.setCursor(emptyCursor);
Pierre Ossman25db44a2017-11-16 16:40:44 +01001121 clientHasCursor = false;
1122 } else {
Pierre Ossman6094ced2018-10-05 17:24:51 +02001123 cp.setCursor(*server->getCursor());
Pierre Ossman25db44a2017-11-16 16:40:44 +01001124 clientHasCursor = true;
1125 }
Pierre Ossman126e5642014-02-13 14:40:25 +01001126
Pierre Ossman8053c8e2017-02-21 12:59:04 +01001127 if (!writer()->writeSetCursorWithAlpha()) {
1128 if (!writer()->writeSetCursor()) {
1129 if (!writer()->writeSetXCursor()) {
1130 // No client support
1131 return;
1132 }
Pierre Ossman126e5642014-02-13 14:40:25 +01001133 }
1134 }
Pierre Ossmana3ac01e2011-11-07 21:13:54 +00001135}
1136
1137void VNCSConnectionST::setDesktopName(const char *name)
1138{
1139 cp.setName(name);
1140
1141 if (state() != RFBSTATE_NORMAL)
1142 return;
1143
1144 if (!writer()->writeSetDesktopName()) {
1145 fprintf(stderr, "Client does not support desktop rename\n");
1146 return;
1147 }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001148}
1149
Pierre Ossmanb45a84f2016-12-12 16:59:15 +01001150void VNCSConnectionST::setLEDState(unsigned int ledstate)
1151{
1152 if (state() != RFBSTATE_NORMAL)
1153 return;
1154
1155 cp.setLEDState(ledstate);
1156
Pierre Ossmanb218ecd2017-11-16 16:43:13 +01001157 writer()->writeLEDState();
Pierre Ossmanb45a84f2016-12-12 16:59:15 +01001158}
1159
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001160void VNCSConnectionST::setSocketTimeouts()
1161{
1162 int timeoutms = rfb::Server::clientWaitTimeMillis;
1163 soonestTimeout(&timeoutms, secsToMillis(rfb::Server::idleTimeout));
1164 if (timeoutms == 0)
1165 timeoutms = -1;
1166 sock->inStream().setTimeout(timeoutms);
1167 sock->outStream().setTimeout(timeoutms);
1168}
1169
1170char* VNCSConnectionST::getStartTime()
1171{
1172 char* result = ctime(&startTime);
1173 result[24] = '\0';
1174 return result;
1175}
1176
1177void VNCSConnectionST::setStatus(int status)
1178{
1179 switch (status) {
1180 case 0:
1181 accessRights = accessRights | AccessPtrEvents | AccessKeyEvents | AccessView;
1182 break;
1183 case 1:
Pierre Ossman7728be22015-03-03 16:39:37 +01001184 accessRights = (accessRights & ~(AccessPtrEvents | AccessKeyEvents)) | AccessView;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001185 break;
1186 case 2:
Adam Tkac8e985062011-02-07 11:33:57 +00001187 accessRights = accessRights & ~(AccessPtrEvents | AccessKeyEvents | AccessView);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001188 break;
1189 }
Pierre Ossman6094ced2018-10-05 17:24:51 +02001190 framebufferUpdateRequest(server->getPixelBuffer()->getRect(), false);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001191}
1192int VNCSConnectionST::getStatus()
1193{
1194 if ((accessRights & (AccessPtrEvents | AccessKeyEvents | AccessView)) == 0x0007)
1195 return 0;
1196 if ((accessRights & (AccessPtrEvents | AccessKeyEvents | AccessView)) == 0x0001)
1197 return 1;
1198 if ((accessRights & (AccessPtrEvents | AccessKeyEvents | AccessView)) == 0x0000)
1199 return 2;
1200 return 4;
1201}
1202