blob: ef49a28eee0edd24c1f7e33461aac4c453a19a96 [file] [log] [blame]
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001/* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved.
Pierre Ossmancef3cf72016-11-25 10:06:34 +01002 * Copyright 2009-2016 Pierre Ossman for Cendio AB
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00003 *
4 * This is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This software is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this software; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
17 * USA.
18 */
19
Pierre Ossman1b478e52011-11-15 12:08:30 +000020// Debug output on what the congestion control is up to
21#undef CONGESTION_DEBUG
22
23#include <sys/time.h>
24
25#ifdef CONGESTION_DEBUG
26#include <sys/socket.h>
27#include <netinet/in.h>
28#include <netinet/tcp.h>
29#endif
30
Pierre Ossmana830bec2011-11-08 12:12:02 +000031#include <network/TcpSocket.h>
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000032#include <rfb/VNCSConnectionST.h>
33#include <rfb/LogWriter.h>
Adam Tkac5a0caed2010-04-23 13:58:10 +000034#include <rfb/Security.h>
Pierre Ossmanc5e25602009-03-20 12:59:05 +000035#include <rfb/screenTypes.h>
Pierre Ossman2c764942011-11-14 15:54:30 +000036#include <rfb/fenceTypes.h>
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000037#include <rfb/ServerCore.h>
38#include <rfb/ComparingUpdateTracker.h>
39#include <rfb/KeyRemapper.h>
Pierre Ossmanfdba3fe2014-01-31 13:12:18 +010040#include <rfb/Encoder.h>
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000041#define XK_MISCELLANY
42#define XK_XKB_KEYS
43#include <rfb/keysymdef.h>
44
45using namespace rfb;
46
47static LogWriter vlog("VNCSConnST");
48
Pierre Ossman1b478e52011-11-15 12:08:30 +000049// This window should get us going fairly fast on a decent bandwidth network.
50// If it's too high, it will rapidly be reduced and stay low.
51static const unsigned INITIAL_WINDOW = 16384;
52
53// TCP's minimal window is 3*MSS. But since we don't know the MSS, we
klemens0536d092017-01-28 20:56:56 +010054// make a guess at 4 KiB (it's probably a bit higher).
Pierre Ossman1b478e52011-11-15 12:08:30 +000055static const unsigned MINIMUM_WINDOW = 4096;
56
57// The current default maximum window for Linux (4 MiB). Should be a good
58// limit for now...
59static const unsigned MAXIMUM_WINDOW = 4194304;
60
61struct RTTInfo {
62 struct timeval tv;
63 int offset;
64 unsigned inFlight;
65};
66
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000067VNCSConnectionST::VNCSConnectionST(VNCServerST* server_, network::Socket *s,
68 bool reverse)
Pierre Ossman7069bdd2015-02-06 14:41:58 +010069 : sock(s), reverseConnection(reverse),
Pierre Ossmanf8e3b342015-01-26 14:37:04 +010070 queryConnectTimer(this), inProcessMessages(false),
Pierre Ossmanb8b1e962012-07-20 10:47:00 +000071 pendingSyncFence(false), syncFence(false), fenceFlags(0),
72 fenceDataLen(0), fenceData(NULL),
Pierre Ossmanb1cd6ca2015-03-03 16:37:43 +010073 baseRTT(-1), congWindow(0), ackedOffset(0), sentOffset(0),
74 minRTT(-1), seenCongestion(false),
Pierre Ossmanb2a417c2015-12-11 19:32:21 +010075 congestionTimer(this),
Pierre Ossman0c9bd4b2014-07-09 16:44:11 +020076 server(server_), updates(false),
Pierre Ossman671d2ef2015-06-09 16:09:06 +020077 updateRenderedCursor(false), removeRenderedCursor(false),
Pierre Ossmana40ab202016-04-29 15:35:56 +020078 continuousUpdates(false), encodeManager(this), pointerEventTime(0),
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +000079 accessRights(AccessDefault), startTime(time(0))
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000080{
81 setStreams(&sock->inStream(), &sock->outStream());
82 peerEndpoint.buf = sock->getPeerEndpoint();
83 VNCServerST::connectionsLog.write(1,"accepted: %s", peerEndpoint.buf);
84
85 // Configure the socket
86 setSocketTimeouts();
87 lastEventTime = time(0);
88
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000089 server->clients.push_front(this);
90}
91
92
93VNCSConnectionST::~VNCSConnectionST()
94{
95 // If we reach here then VNCServerST is deleting us!
96 VNCServerST::connectionsLog.write(1,"closed: %s (%s)",
97 peerEndpoint.buf,
98 (closeReason.buf) ? closeReason.buf : "");
99
100 // Release any keys the client still had pressed
101 std::set<rdr::U32>::iterator i;
Pierre Ossman9a153b02015-08-31 10:01:14 +0200102 for (i=pressedKeys.begin(); i!=pressedKeys.end(); i++) {
103 vlog.debug("Releasing key 0x%x on client disconnect", *i);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000104 server->desktop->keyEvent(*i, false);
Pierre Ossman9a153b02015-08-31 10:01:14 +0200105 }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000106 if (server->pointerClient == this)
107 server->pointerClient = 0;
108
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000109 // Remove this client from the server
110 server->clients.remove(this);
111
Pierre Ossman2c764942011-11-14 15:54:30 +0000112 delete [] fenceData;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000113}
114
115
116// Methods called from VNCServerST
117
118bool VNCSConnectionST::init()
119{
120 try {
121 initialiseProtocol();
122 } catch (rdr::Exception& e) {
123 close(e.str());
124 return false;
125 }
126 return true;
127}
128
129void VNCSConnectionST::close(const char* reason)
130{
131 // Log the reason for the close
132 if (!closeReason.buf)
Adam Tkacd36b6262009-09-04 10:57:20 +0000133 closeReason.buf = strDup(reason);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000134 else
135 vlog.debug("second close: %s (%s)", peerEndpoint.buf, reason);
136
137 if (authenticated()) {
138 server->lastDisconnectTime = time(0);
139 }
140
141 // Just shutdown the socket and mark our state as closing. Eventually the
142 // calling code will call VNCServerST's removeSocket() method causing us to
143 // be deleted.
144 sock->shutdown();
145 setState(RFBSTATE_CLOSING);
146}
147
148
149void VNCSConnectionST::processMessages()
150{
151 if (state() == RFBSTATE_CLOSING) return;
152 try {
153 // - Now set appropriate socket timeouts and process data
154 setSocketTimeouts();
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000155
156 inProcessMessages = true;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000157
Pierre Ossmana830bec2011-11-08 12:12:02 +0000158 // Get the underlying TCP layer to build large packets if we send
159 // multiple small responses.
160 network::TcpSocket::cork(sock->getFd(), true);
161
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000162 while (getInStream()->checkNoWait(1)) {
Pierre Ossmanb8b1e962012-07-20 10:47:00 +0000163 if (pendingSyncFence) {
164 syncFence = true;
165 pendingSyncFence = false;
166 }
167
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000168 processMsg();
Pierre Ossmanb8b1e962012-07-20 10:47:00 +0000169
Pierre Ossman2c764942011-11-14 15:54:30 +0000170 if (syncFence) {
171 writer()->writeFence(fenceFlags, fenceDataLen, fenceData);
172 syncFence = false;
173 }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000174 }
175
Pierre Ossmana830bec2011-11-08 12:12:02 +0000176 // Flush out everything in case we go idle after this.
177 network::TcpSocket::cork(sock->getFd(), false);
178
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000179 inProcessMessages = false;
Constantin Kaplinsky2ef66952008-08-29 11:33:46 +0000180
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000181 // If there were anything requiring an update, try to send it here.
182 // We wait until now with this to aggregate responses and to give
183 // higher priority to user actions such as keyboard and pointer events.
184 writeFramebufferUpdate();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000185 } catch (rdr::EndOfStream&) {
186 close("Clean disconnection");
187 } catch (rdr::Exception &e) {
188 close(e.str());
189 }
190}
191
Pierre Ossmand408ca52016-04-29 14:26:05 +0200192void VNCSConnectionST::flushSocket()
193{
194 if (state() == RFBSTATE_CLOSING) return;
195 try {
196 setSocketTimeouts();
197 sock->outStream().flush();
Pierre Ossmana40ab202016-04-29 15:35:56 +0200198 // Flushing the socket might release an update that was previously
199 // delayed because of congestion.
200 if (sock->outStream().bufferUsage() == 0)
201 writeFramebufferUpdate();
Pierre Ossmand408ca52016-04-29 14:26:05 +0200202 } catch (rdr::Exception &e) {
203 close(e.str());
204 }
205}
206
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000207void VNCSConnectionST::pixelBufferChange()
208{
209 try {
210 if (!authenticated()) return;
211 if (cp.width && cp.height && (server->pb->width() != cp.width ||
212 server->pb->height() != cp.height))
213 {
214 // We need to clip the next update to the new size, but also add any
215 // extra bits if it's bigger. If we wanted to do this exactly, something
216 // like the code below would do it, but at the moment we just update the
Pierre Ossman671d2ef2015-06-09 16:09:06 +0200217 // entire new size. However, we do need to clip the damagedCursorRegion
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000218 // because that might be added to updates in writeFramebufferUpdate().
219
220 //updates.intersect(server->pb->getRect());
221 //
222 //if (server->pb->width() > cp.width)
223 // updates.add_changed(Rect(cp.width, 0, server->pb->width(),
224 // server->pb->height()));
225 //if (server->pb->height() > cp.height)
226 // updates.add_changed(Rect(0, cp.height, cp.width,
227 // server->pb->height()));
228
Pierre Ossman671d2ef2015-06-09 16:09:06 +0200229 damagedCursorRegion.assign_intersect(server->pb->getRect());
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000230
231 cp.width = server->pb->width();
232 cp.height = server->pb->height();
Pierre Ossman34e62f32009-03-20 21:46:12 +0000233 cp.screenLayout = server->screenLayout;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000234 if (state() == RFBSTATE_NORMAL) {
Pierre Ossman2ee430a2009-05-28 12:54:24 +0000235 // We should only send EDS to client asking for both
236 if (!writer()->writeExtendedDesktopSize()) {
237 if (!writer()->writeSetDesktopSize()) {
238 close("Client does not support desktop resize");
239 return;
240 }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000241 }
242 }
243 }
244 // Just update the whole screen at the moment because we're too lazy to
245 // work out what's actually changed.
246 updates.clear();
247 updates.add_changed(server->pb->getRect());
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000248 writeFramebufferUpdate();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000249 } catch(rdr::Exception &e) {
250 close(e.str());
251 }
252}
253
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000254void VNCSConnectionST::writeFramebufferUpdateOrClose()
Pierre Ossman04e62db2009-03-23 16:57:07 +0000255{
256 try {
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000257 writeFramebufferUpdate();
258 } catch(rdr::Exception &e) {
259 close(e.str());
260 }
261}
Pierre Ossman04e62db2009-03-23 16:57:07 +0000262
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000263void VNCSConnectionST::screenLayoutChangeOrClose(rdr::U16 reason)
264{
265 try {
266 screenLayoutChange(reason);
Pierre Ossman04e62db2009-03-23 16:57:07 +0000267 } catch(rdr::Exception &e) {
268 close(e.str());
269 }
270}
271
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000272void VNCSConnectionST::bellOrClose()
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000273{
274 try {
275 if (state() == RFBSTATE_NORMAL) writer()->writeBell();
276 } catch(rdr::Exception& e) {
277 close(e.str());
278 }
279}
280
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000281void VNCSConnectionST::serverCutTextOrClose(const char *str, int len)
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000282{
283 try {
284 if (!(accessRights & AccessCutText)) return;
285 if (!rfb::Server::sendCutText) return;
286 if (state() == RFBSTATE_NORMAL)
287 writer()->writeServerCutText(str, len);
288 } catch(rdr::Exception& e) {
289 close(e.str());
290 }
291}
292
Peter Ã…strandc39e0782009-01-15 12:21:42 +0000293
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000294void VNCSConnectionST::setDesktopNameOrClose(const char *name)
Peter Ã…strandc39e0782009-01-15 12:21:42 +0000295{
Peter Ã…strandc39e0782009-01-15 12:21:42 +0000296 try {
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000297 setDesktopName(name);
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();
308 } catch(rdr::Exception& e) {
309 close(e.str());
310 }
311}
312
313
314int VNCSConnectionST::checkIdleTimeout()
315{
316 int idleTimeout = rfb::Server::idleTimeout;
317 if (idleTimeout == 0) return 0;
318 if (state() != RFBSTATE_NORMAL && idleTimeout < 15)
319 idleTimeout = 15; // minimum of 15 seconds while authenticating
320 time_t now = time(0);
321 if (now < lastEventTime) {
322 // Someone must have set the time backwards. Set lastEventTime so that the
323 // idleTimeout will count from now.
324 vlog.info("Time has gone backwards - resetting idle timeout");
325 lastEventTime = now;
326 }
327 int timeLeft = lastEventTime + idleTimeout - now;
328 if (timeLeft < -60) {
329 // Our callback is over a minute late - someone must have set the time
330 // forwards. Set lastEventTime so that the idleTimeout will count from
331 // now.
332 vlog.info("Time has gone forwards - resetting idle timeout");
333 lastEventTime = now;
334 return secsToMillis(idleTimeout);
335 }
336 if (timeLeft <= 0) {
337 close("Idle timeout");
338 return 0;
339 }
340 return secsToMillis(timeLeft);
341}
342
Pierre Ossmanb114cec2011-11-20 15:36:11 +0000343
344bool VNCSConnectionST::getComparerState()
345{
346 // We interpret a low compression level as an indication that the client
347 // wants to prioritise CPU usage over bandwidth, and hence disable the
348 // comparing update tracker.
349 return (cp.compressLevel == -1) || (cp.compressLevel > 1);
350}
351
352
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000353// renderedCursorChange() is called whenever the server-side rendered cursor
354// changes shape or position. It ensures that the next update will clean up
355// the old rendered cursor and if necessary draw the new rendered cursor.
356
357void VNCSConnectionST::renderedCursorChange()
358{
359 if (state() != RFBSTATE_NORMAL) return;
Pierre Ossman671d2ef2015-06-09 16:09:06 +0200360 if (!damagedCursorRegion.is_empty())
Pierre Ossman5c9e1e52011-11-08 10:32:05 +0000361 removeRenderedCursor = true;
Pierre Ossman6b0bc292011-12-21 13:17:54 +0000362 if (needRenderedCursor()) {
Pierre Ossman671d2ef2015-06-09 16:09:06 +0200363 updateRenderedCursor = true;
Pierre Ossman6b0bc292011-12-21 13:17:54 +0000364 writeFramebufferUpdateOrClose();
365 }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000366}
367
368// needRenderedCursor() returns true if this client needs the server-side
369// rendered cursor. This may be because it does not support local cursor or
370// because the current cursor position has not been set by this client.
371// Unfortunately we can't know for sure when the current cursor position has
372// been set by this client. We guess that this is the case when the current
373// cursor position is the same as the last pointer event from this client, or
374// if it is a very short time since this client's last pointer event (up to a
375// second). [ Ideally we should do finer-grained timing here and make the time
376// configurable, but I don't think it's that important. ]
377
378bool VNCSConnectionST::needRenderedCursor()
379{
Pierre Ossman77ede0a2016-12-05 16:57:30 +0100380 if (state() != RFBSTATE_NORMAL)
381 return false;
382
383 if (!cp.supportsLocalCursor && !cp.supportsLocalXCursor)
384 return true;
385 if (!server->cursorPos.equals(pointerEventPos) &&
386 (time(0) - pointerEventTime) > 0)
387 return true;
388
389 return false;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000390}
391
392
393void VNCSConnectionST::approveConnectionOrClose(bool accept,
394 const char* reason)
395{
396 try {
397 approveConnection(accept, reason);
398 } catch (rdr::Exception& e) {
399 close(e.str());
400 }
401}
402
403
404
405// -=- Callbacks from SConnection
406
407void VNCSConnectionST::authSuccess()
408{
409 lastEventTime = time(0);
410
411 server->startDesktop();
412
413 // - Set the connection parameters appropriately
414 cp.width = server->pb->width();
415 cp.height = server->pb->height();
Pierre Ossman34e62f32009-03-20 21:46:12 +0000416 cp.screenLayout = server->screenLayout;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000417 cp.setName(server->getName());
418
419 // - Set the default pixel format
420 cp.setPF(server->pb->getPF());
421 char buffer[256];
422 cp.pf().print(buffer, 256);
423 vlog.info("Server default pixel format %s", buffer);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000424
425 // - Mark the entire display as "dirty"
426 updates.add_changed(server->pb->getRect());
427 startTime = time(0);
Pierre Ossman1b478e52011-11-15 12:08:30 +0000428
429 // - Bootstrap the congestion control
430 ackedOffset = sock->outStream().length();
431 congWindow = INITIAL_WINDOW;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000432}
433
434void VNCSConnectionST::queryConnection(const char* userName)
435{
436 // - Authentication succeeded - clear from blacklist
437 CharArray name; name.buf = sock->getPeerAddress();
438 server->blHosts->clearBlackmark(name.buf);
439
440 // - Special case to provide a more useful error message
441 if (rfb::Server::neverShared && !rfb::Server::disconnectClients &&
442 server->authClientCount() > 0) {
443 approveConnection(false, "The server is already in use");
444 return;
445 }
446
447 // - Does the client have the right to bypass the query?
448 if (reverseConnection ||
449 !(rfb::Server::queryConnect || sock->requiresQuery()) ||
450 (accessRights & AccessNoQuery))
451 {
452 approveConnection(true);
453 return;
454 }
455
456 // - Get the server to display an Accept/Reject dialog, if required
457 // If a dialog is displayed, the result will be PENDING, and the
458 // server will call approveConnection at a later time
459 CharArray reason;
460 VNCServerST::queryResult qr = server->queryConnection(sock, userName,
461 &reason.buf);
Pierre Ossmanf8e3b342015-01-26 14:37:04 +0100462 if (qr == VNCServerST::PENDING) {
463 queryConnectTimer.start(rfb::Server::queryConnectTimeout * 1000);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000464 return;
Pierre Ossmanf8e3b342015-01-26 14:37:04 +0100465 }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000466
467 // - If server returns ACCEPT/REJECT then pass result to SConnection
468 approveConnection(qr == VNCServerST::ACCEPT, reason.buf);
469}
470
471void VNCSConnectionST::clientInit(bool shared)
472{
473 lastEventTime = time(0);
474 if (rfb::Server::alwaysShared || reverseConnection) shared = true;
Pierre Ossmane7be49b2014-12-02 14:33:17 +0100475 if (!(accessRights & AccessNonShared)) shared = true;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000476 if (rfb::Server::neverShared) shared = false;
477 if (!shared) {
Pierre Ossmane7be49b2014-12-02 14:33:17 +0100478 if (rfb::Server::disconnectClients && (accessRights & AccessNonShared)) {
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000479 // - Close all the other connected clients
480 vlog.debug("non-shared connection - closing clients");
481 server->closeClients("Non-shared connection requested", getSock());
482 } else {
483 // - Refuse this connection if there are existing clients, in addition to
484 // this one
485 if (server->authClientCount() > 1) {
486 close("Server is already in use");
487 return;
488 }
489 }
490 }
491 SConnection::clientInit(shared);
492}
493
494void VNCSConnectionST::setPixelFormat(const PixelFormat& pf)
495{
496 SConnection::setPixelFormat(pf);
497 char buffer[256];
498 pf.print(buffer, 256);
499 vlog.info("Client pixel format %s", buffer);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000500 setCursor();
501}
502
503void VNCSConnectionST::pointerEvent(const Point& pos, int buttonMask)
504{
505 pointerEventTime = lastEventTime = time(0);
506 server->lastUserInputTime = lastEventTime;
507 if (!(accessRights & AccessPtrEvents)) return;
508 if (!rfb::Server::acceptPointerEvents) return;
509 if (!server->pointerClient || server->pointerClient == this) {
510 pointerEventPos = pos;
511 if (buttonMask)
512 server->pointerClient = this;
513 else
514 server->pointerClient = 0;
515 server->desktop->pointerEvent(pointerEventPos, buttonMask);
516 }
517}
518
519
520class VNCSConnectionSTShiftPresser {
521public:
522 VNCSConnectionSTShiftPresser(SDesktop* desktop_)
523 : desktop(desktop_), pressed(false) {}
524 ~VNCSConnectionSTShiftPresser() {
Pierre Ossman9a153b02015-08-31 10:01:14 +0200525 if (pressed) {
526 vlog.debug("Releasing fake Shift_L");
527 desktop->keyEvent(XK_Shift_L, false);
528 }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000529 }
530 void press() {
Pierre Ossman9a153b02015-08-31 10:01:14 +0200531 vlog.debug("Pressing fake Shift_L");
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000532 desktop->keyEvent(XK_Shift_L, true);
533 pressed = true;
534 }
535 SDesktop* desktop;
536 bool pressed;
537};
538
539// keyEvent() - record in the pressedKeys which keys were pressed. Allow
540// multiple down events (for autorepeat), but only allow a single up event.
541void VNCSConnectionST::keyEvent(rdr::U32 key, bool down) {
542 lastEventTime = time(0);
543 server->lastUserInputTime = lastEventTime;
544 if (!(accessRights & AccessKeyEvents)) return;
545 if (!rfb::Server::acceptKeyEvents) return;
546
Pierre Ossman9a153b02015-08-31 10:01:14 +0200547 if (down)
548 vlog.debug("Key pressed: 0x%x", key);
549 else
550 vlog.debug("Key released: 0x%x", key);
551
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000552 // Remap the key if required
Pierre Ossman9a153b02015-08-31 10:01:14 +0200553 if (server->keyRemapper) {
554 rdr::U32 newkey;
555 newkey = server->keyRemapper->remapKey(key);
556 if (newkey != key) {
557 vlog.debug("Key remapped to 0x%x", newkey);
558 key = newkey;
559 }
560 }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000561
562 // Turn ISO_Left_Tab into shifted Tab.
563 VNCSConnectionSTShiftPresser shiftPresser(server->desktop);
564 if (key == XK_ISO_Left_Tab) {
565 if (pressedKeys.find(XK_Shift_L) == pressedKeys.end() &&
566 pressedKeys.find(XK_Shift_R) == pressedKeys.end())
567 shiftPresser.press();
568 key = XK_Tab;
569 }
570
571 if (down) {
572 pressedKeys.insert(key);
573 } else {
574 if (!pressedKeys.erase(key)) return;
575 }
576 server->desktop->keyEvent(key, down);
577}
578
579void VNCSConnectionST::clientCutText(const char* str, int len)
580{
581 if (!(accessRights & AccessCutText)) return;
582 if (!rfb::Server::acceptCutText) return;
583 server->desktop->clientCutText(str, len);
584}
585
586void VNCSConnectionST::framebufferUpdateRequest(const Rect& r,bool incremental)
587{
Pierre Ossmane9962f72009-04-23 12:31:42 +0000588 Rect safeRect;
589
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000590 if (!(accessRights & AccessView)) return;
591
592 SConnection::framebufferUpdateRequest(r, incremental);
593
Pierre Ossmand9a59ba2009-03-20 15:55:37 +0000594 // Check that the client isn't sending crappy requests
595 if (!r.enclosed_by(Rect(0, 0, cp.width, cp.height))) {
596 vlog.error("FramebufferUpdateRequest %dx%d at %d,%d exceeds framebuffer %dx%d",
597 r.width(), r.height(), r.tl.x, r.tl.y, cp.width, cp.height);
Pierre Ossmane9962f72009-04-23 12:31:42 +0000598 safeRect = r.intersect(Rect(0, 0, cp.width, cp.height));
599 } else {
600 safeRect = r;
Pierre Ossmand9a59ba2009-03-20 15:55:37 +0000601 }
602
Constantin Kaplinsky2ef66952008-08-29 11:33:46 +0000603 // Just update the requested region.
604 // Framebuffer update will be sent a bit later, see processMessages().
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000605 Region reqRgn(r);
Pierre Ossman1b478e52011-11-15 12:08:30 +0000606 if (!incremental || !continuousUpdates)
607 requested.assign_union(reqRgn);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000608
609 if (!incremental) {
610 // Non-incremental update - treat as if area requested has changed
611 updates.add_changed(reqRgn);
Pierre Ossman53125a72009-04-22 15:37:51 +0000612
613 // And send the screen layout to the client (which, unlike the
614 // framebuffer dimensions, the client doesn't get during init)
Pierre Ossmanc5e25602009-03-20 12:59:05 +0000615 writer()->writeExtendedDesktopSize();
Pierre Ossman53125a72009-04-22 15:37:51 +0000616
617 // We do not send a DesktopSize since it only contains the
618 // framebuffer size (which the client already should know) and
619 // because some clients don't handle extra DesktopSize events
620 // very well.
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000621 }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000622}
623
Pierre Ossman34bb0612009-03-21 21:16:14 +0000624void VNCSConnectionST::setDesktopSize(int fb_width, int fb_height,
625 const ScreenSet& layout)
Pierre Ossmanc5e25602009-03-20 12:59:05 +0000626{
Pierre Ossman04e62db2009-03-23 16:57:07 +0000627 unsigned int result;
628
Michal Srbb318b8f2014-11-24 13:18:28 +0200629 if (!(accessRights & AccessSetDesktopSize)) return;
630 if (!rfb::Server::acceptSetDesktopSize) return;
631
Pierre Ossman04e62db2009-03-23 16:57:07 +0000632 // Don't bother the desktop with an invalid configuration
633 if (!layout.validate(fb_width, fb_height)) {
634 writer()->writeExtendedDesktopSize(reasonClient, resultInvalid,
635 fb_width, fb_height, layout);
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000636 writeFramebufferUpdate();
Pierre Ossman04e62db2009-03-23 16:57:07 +0000637 return;
638 }
639
640 // FIXME: the desktop will call back to VNCServerST and an extra set
641 // of ExtendedDesktopSize messages will be sent. This is okay
642 // protocol-wise, but unnecessary.
643 result = server->desktop->setScreenLayout(fb_width, fb_height, layout);
644
Pierre Ossman04e62db2009-03-23 16:57:07 +0000645 writer()->writeExtendedDesktopSize(reasonClient, result,
646 fb_width, fb_height, layout);
Pierre Ossman04e62db2009-03-23 16:57:07 +0000647
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000648 // Only notify other clients on success
Pierre Ossman04e62db2009-03-23 16:57:07 +0000649 if (result == resultSuccess) {
650 if (server->screenLayout != layout)
651 throw Exception("Desktop configured a different screen layout than requested");
652 server->notifyScreenLayoutChange(this);
653 }
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000654
655 // but always send back a reply to the requesting client
656 // (do this last as it might throw an exception on socket errors)
657 writeFramebufferUpdate();
Pierre Ossmanc5e25602009-03-20 12:59:05 +0000658}
659
Pierre Ossman2c764942011-11-14 15:54:30 +0000660void VNCSConnectionST::fence(rdr::U32 flags, unsigned len, const char data[])
661{
Pierre Ossmanb2a417c2015-12-11 19:32:21 +0100662 rdr::U8 type;
663
Pierre Ossman2c764942011-11-14 15:54:30 +0000664 if (flags & fenceFlagRequest) {
665 if (flags & fenceFlagSyncNext) {
Pierre Ossmanb8b1e962012-07-20 10:47:00 +0000666 pendingSyncFence = true;
Pierre Ossman2c764942011-11-14 15:54:30 +0000667
668 fenceFlags = flags & (fenceFlagBlockBefore | fenceFlagBlockAfter | fenceFlagSyncNext);
669 fenceDataLen = len;
670 delete [] fenceData;
Michal Srbf3afa242017-03-27 19:02:15 +0300671 fenceData = NULL;
Pierre Ossman2c764942011-11-14 15:54:30 +0000672 if (len > 0) {
673 fenceData = new char[len];
674 memcpy(fenceData, data, len);
675 }
676
677 return;
678 }
679
680 // We handle everything synchronously so we trivially honor these modes
681 flags = flags & (fenceFlagBlockBefore | fenceFlagBlockAfter);
682
683 writer()->writeFence(flags, len, data);
684 return;
685 }
686
Pierre Ossmanb2a417c2015-12-11 19:32:21 +0100687 if (len < 1)
688 vlog.error("Fence response of unexpected size received");
Pierre Ossman1b478e52011-11-15 12:08:30 +0000689
Pierre Ossmanb2a417c2015-12-11 19:32:21 +0100690 type = data[0];
691
692 switch (type) {
Pierre Ossman2c764942011-11-14 15:54:30 +0000693 case 0:
694 // Initial dummy fence;
695 break;
Pierre Ossmanb2a417c2015-12-11 19:32:21 +0100696 case 1:
697 handleRTTPong();
Pierre Ossman1b478e52011-11-15 12:08:30 +0000698 break;
Pierre Ossman2c764942011-11-14 15:54:30 +0000699 default:
Pierre Ossmanb2a417c2015-12-11 19:32:21 +0100700 vlog.error("Fence response of unexpected type received");
Pierre Ossman2c764942011-11-14 15:54:30 +0000701 }
702}
703
Pierre Ossman1b478e52011-11-15 12:08:30 +0000704void VNCSConnectionST::enableContinuousUpdates(bool enable,
705 int x, int y, int w, int h)
706{
707 Rect rect;
708
709 if (!cp.supportsFence || !cp.supportsContinuousUpdates)
710 throw Exception("Client tried to enable continuous updates when not allowed");
711
712 continuousUpdates = enable;
713
714 rect.setXYWH(x, y, w, h);
715 cuRegion.reset(rect);
716
717 if (enable) {
718 requested.clear();
719 writeFramebufferUpdate();
720 } else {
721 writer()->writeEndOfContinuousUpdates();
722 }
723}
724
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000725// supportsLocalCursor() is called whenever the status of
726// cp.supportsLocalCursor has changed. If the client does now support local
727// cursor, we make sure that the old server-side rendered cursor is cleaned up
728// and the cursor is sent to the client.
729
730void VNCSConnectionST::supportsLocalCursor()
731{
732 if (cp.supportsLocalCursor || cp.supportsLocalXCursor) {
Pierre Ossman671d2ef2015-06-09 16:09:06 +0200733 if (!damagedCursorRegion.is_empty())
Pierre Ossman5c9e1e52011-11-08 10:32:05 +0000734 removeRenderedCursor = true;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000735 setCursor();
736 }
737}
738
Pierre Ossman2c764942011-11-14 15:54:30 +0000739void VNCSConnectionST::supportsFence()
740{
Pierre Ossmanb2a417c2015-12-11 19:32:21 +0100741 char type = 0;
742 writer()->writeFence(fenceFlagRequest, sizeof(type), &type);
Pierre Ossman2c764942011-11-14 15:54:30 +0000743}
744
Pierre Ossman1b478e52011-11-15 12:08:30 +0000745void VNCSConnectionST::supportsContinuousUpdates()
746{
747 // We refuse to use continuous updates if we cannot monitor the buffer
748 // usage using fences.
749 if (!cp.supportsFence)
750 return;
751
752 writer()->writeEndOfContinuousUpdates();
753}
754
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000755
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +0000756bool VNCSConnectionST::handleTimeout(Timer* t)
757{
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000758 try {
Pierre Ossmana40ab202016-04-29 15:35:56 +0200759 if (t == &congestionTimer)
Pierre Ossman1b478e52011-11-15 12:08:30 +0000760 updateCongestion();
Pierre Ossmanf8e3b342015-01-26 14:37:04 +0100761 else if (t == &queryConnectTimer) {
762 if (state() == RFBSTATE_QUERYING)
763 approveConnection(false, "The attempt to prompt the user to accept the connection failed");
764 }
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000765 } catch (rdr::Exception& e) {
766 close(e.str());
767 }
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +0000768
769 return false;
770}
771
772
Pierre Ossman1b478e52011-11-15 12:08:30 +0000773void VNCSConnectionST::writeRTTPing()
774{
775 struct RTTInfo rttInfo;
Pierre Ossmanb2a417c2015-12-11 19:32:21 +0100776 char type;
Pierre Ossman1b478e52011-11-15 12:08:30 +0000777
778 if (!cp.supportsFence)
779 return;
780
781 memset(&rttInfo, 0, sizeof(struct RTTInfo));
782
783 gettimeofday(&rttInfo.tv, NULL);
784 rttInfo.offset = sock->outStream().length();
785 rttInfo.inFlight = rttInfo.offset - ackedOffset;
786
Pierre Ossmanb2a417c2015-12-11 19:32:21 +0100787 pings.push_back(rttInfo);
788
Pierre Ossman1b478e52011-11-15 12:08:30 +0000789 // We need to make sure any old update are already processed by the
790 // time we get the response back. This allows us to reliably throttle
791 // back on client overload, as well as network overload.
Pierre Ossmanb2a417c2015-12-11 19:32:21 +0100792 type = 1;
Pierre Ossman1b478e52011-11-15 12:08:30 +0000793 writer()->writeFence(fenceFlagRequest | fenceFlagBlockBefore,
Pierre Ossmanb2a417c2015-12-11 19:32:21 +0100794 sizeof(type), &type);
Pierre Ossman1b478e52011-11-15 12:08:30 +0000795
796 sentOffset = rttInfo.offset;
797
798 // Let some data flow before we adjust the settings
799 if (!congestionTimer.isStarted())
800 congestionTimer.start(__rfbmin(baseRTT * 2, 100));
801}
802
Pierre Ossmanb2a417c2015-12-11 19:32:21 +0100803void VNCSConnectionST::handleRTTPong()
Pierre Ossman1b478e52011-11-15 12:08:30 +0000804{
Pierre Ossmanb2a417c2015-12-11 19:32:21 +0100805 struct RTTInfo rttInfo;
Pierre Ossman1b478e52011-11-15 12:08:30 +0000806 unsigned rtt, delay;
Pierre Ossman1b478e52011-11-15 12:08:30 +0000807
Pierre Ossmanb2a417c2015-12-11 19:32:21 +0100808 if (pings.empty())
809 return;
810
811 rttInfo = pings.front();
812 pings.pop_front();
Pierre Ossman1b478e52011-11-15 12:08:30 +0000813
814 rtt = msSince(&rttInfo.tv);
815 if (rtt < 1)
816 rtt = 1;
817
818 ackedOffset = rttInfo.offset;
819
820 // Try to estimate wire latency by tracking lowest seen latency
821 if (rtt < baseRTT)
822 baseRTT = rtt;
823
824 if (rttInfo.inFlight > congWindow) {
825 seenCongestion = true;
826
827 // Estimate added delay because of overtaxed buffers
828 delay = (rttInfo.inFlight - congWindow) * baseRTT / congWindow;
829
830 if (delay < rtt)
831 rtt -= delay;
832 else
833 rtt = 1;
834
835 // If we underestimate the congestion window, then we'll get a latency
836 // that's less than the wire latency, which will confuse other portions
837 // of the code.
838 if (rtt < baseRTT)
839 rtt = baseRTT;
840 }
841
842 // We only keep track of the minimum latency seen (for a given interval)
klemens0536d092017-01-28 20:56:56 +0100843 // on the basis that we want to avoid continuous buffer issue, but don't
Pierre Ossman1b478e52011-11-15 12:08:30 +0000844 // mind (or even approve of) bursts.
845 if (rtt < minRTT)
846 minRTT = rtt;
847}
848
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +0000849bool VNCSConnectionST::isCongested()
850{
Pierre Ossman1b478e52011-11-15 12:08:30 +0000851 int offset;
852
853 // Stuff still waiting in the send buffer?
Pierre Ossman352d0622016-04-29 15:50:54 +0200854 sock->outStream().flush();
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +0000855 if (sock->outStream().bufferUsage() > 0)
856 return true;
857
Pierre Ossman1b478e52011-11-15 12:08:30 +0000858 if (!cp.supportsFence)
859 return false;
860
861 // Idle for too long? (and no data on the wire)
862 //
863 // FIXME: This should really just be one baseRTT, but we're getting
864 // problems with triggering the idle timeout on each update.
865 // Maybe we need to use a moving average for the wire latency
866 // instead of baseRTT.
867 if ((sentOffset == ackedOffset) &&
868 (sock->outStream().getIdleTime() > 2 * baseRTT)) {
869
870#ifdef CONGESTION_DEBUG
871 if (congWindow > INITIAL_WINDOW)
872 fprintf(stderr, "Reverting to initial window (%d KiB) after %d ms\n",
873 INITIAL_WINDOW / 1024, sock->outStream().getIdleTime());
874#endif
875
876 // Close congestion window and allow a transfer
877 // FIXME: Reset baseRTT like Linux Vegas?
878 congWindow = __rfbmin(INITIAL_WINDOW, congWindow);
879
880 return false;
881 }
882
883 offset = sock->outStream().length();
884
885 // FIXME: Should we compensate for non-update data?
886 // (i.e. use sentOffset instead of offset)
887 if ((offset - ackedOffset) < congWindow)
888 return false;
889
890 // If we just have one outstanding "ping", that means the client has
891 // started receiving our update. In order to not regress compared to
892 // before we had congestion avoidance, we allow another update here.
893 // This could further clog up the tubes, but congestion control isn't
894 // really working properly right now anyway as the wire would otherwise
895 // be idle for at least RTT/2.
Pierre Ossmanb2a417c2015-12-11 19:32:21 +0100896 if (pings.size() == 1)
Pierre Ossman1b478e52011-11-15 12:08:30 +0000897 return false;
898
899 return true;
900}
901
902
903void VNCSConnectionST::updateCongestion()
904{
905 unsigned diff;
906
907 if (!seenCongestion)
908 return;
909
910 diff = minRTT - baseRTT;
911
912 if (diff > __rfbmin(100, baseRTT)) {
913 // Way too fast
914 congWindow = congWindow * baseRTT / minRTT;
915 } else if (diff > __rfbmin(50, baseRTT/2)) {
916 // Slightly too fast
917 congWindow -= 4096;
918 } else if (diff < 5) {
919 // Way too slow
920 congWindow += 8192;
921 } else if (diff < 25) {
922 // Too slow
923 congWindow += 4096;
924 }
925
926 if (congWindow < MINIMUM_WINDOW)
927 congWindow = MINIMUM_WINDOW;
928 if (congWindow > MAXIMUM_WINDOW)
929 congWindow = MAXIMUM_WINDOW;
930
931#ifdef CONGESTION_DEBUG
932 fprintf(stderr, "RTT: %d ms (%d ms), Window: %d KiB, Bandwidth: %g Mbps\n",
933 minRTT, baseRTT, congWindow / 1024,
934 congWindow * 8.0 / baseRTT / 1000.0);
935
936#ifdef TCP_INFO
937 struct tcp_info tcp_info;
938 socklen_t tcp_info_length;
939
940 tcp_info_length = sizeof(tcp_info);
941 if (getsockopt(sock->getFd(), SOL_TCP, TCP_INFO,
942 (void *)&tcp_info, &tcp_info_length) == 0) {
943 fprintf(stderr, "Socket: RTT: %d ms (+/- %d ms) Window %d KiB\n",
944 tcp_info.tcpi_rtt / 1000, tcp_info.tcpi_rttvar / 1000,
945 tcp_info.tcpi_snd_mss * tcp_info.tcpi_snd_cwnd / 1024);
946 }
947#endif
948
949#endif
950
951 minRTT = -1;
952 seenCongestion = false;
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +0000953}
954
955
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000956void VNCSConnectionST::writeFramebufferUpdate()
957{
Pierre Ossman2c764942011-11-14 15:54:30 +0000958 // We're in the middle of processing a command that's supposed to be
959 // synchronised. Allowing an update to slip out right now might violate
960 // that synchronisation.
961 if (syncFence)
962 return;
963
Pierre Ossmana3ac01e2011-11-07 21:13:54 +0000964 // We try to aggregate responses, so don't send out anything whilst we
965 // still have incoming messages. processMessages() will give us another
966 // chance to run once things are idle.
967 if (inProcessMessages)
968 return;
969
Pierre Ossman1b478e52011-11-15 12:08:30 +0000970 if (state() != RFBSTATE_NORMAL)
971 return;
972 if (requested.is_empty() && !continuousUpdates)
Pierre Ossmane9962f72009-04-23 12:31:42 +0000973 return;
Pierre Ossmand9a59ba2009-03-20 15:55:37 +0000974
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +0000975 // Check that we actually have some space on the link and retry in a
976 // bit if things are congested.
Pierre Ossmana40ab202016-04-29 15:35:56 +0200977 if (isCongested())
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +0000978 return;
Pierre Ossman1bb8b6c2011-10-25 15:20:05 +0000979
Pierre Ossmancef3cf72016-11-25 10:06:34 +0100980 // Updates often consists of many small writes, and in continuous
981 // mode, we will also have small fence messages around the update. We
982 // need to aggregate these in order to not clog up TCP's congestion
983 // window.
Pierre Ossman36dadf82011-11-15 12:11:32 +0000984 network::TcpSocket::cork(sock->getFd(), true);
985
Pierre Ossmane9962f72009-04-23 12:31:42 +0000986 // First take care of any updates that cannot contain framebuffer data
987 // changes.
Pierre Ossmancef3cf72016-11-25 10:06:34 +0100988 writeNoDataUpdate();
989
990 // Then real data (if possible)
991 writeDataUpdate();
992
993 network::TcpSocket::cork(sock->getFd(), false);
994}
995
996void VNCSConnectionST::writeNoDataUpdate()
997{
998 if (!writer()->needNoDataUpdate())
999 return;
1000
1001 writer()->writeNoDataUpdate();
1002
1003 // Make sure no data update is sent until next request
1004 requested.clear();
1005}
1006
1007void VNCSConnectionST::writeDataUpdate()
1008{
1009 Region req;
1010 UpdateInfo ui;
1011 bool needNewUpdateInfo;
Pierre Ossman24684e52016-12-05 16:58:19 +01001012 const RenderedCursor *cursor;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001013
Constantin Kaplinsky604d7812007-08-31 15:50:37 +00001014 updates.enable_copyrect(cp.useCopyRect);
1015
Pierre Ossman6e49e952016-10-07 15:59:38 +02001016 // See if we are allowed to send anything right now (the framebuffer
1017 // might have changed in ways we haven't yet been informed of).
Pierre Ossmanbbf955e2011-11-08 12:44:10 +00001018 if (!server->checkUpdate())
Pierre Ossmancef3cf72016-11-25 10:06:34 +01001019 return;
Constantin Kaplinskya09dc142008-12-18 12:08:15 +00001020
Pierre Ossmancef3cf72016-11-25 10:06:34 +01001021 // See what the client has requested (if anything)
Pierre Ossman1b478e52011-11-15 12:08:30 +00001022 if (continuousUpdates)
1023 req = cuRegion.union_(requested);
1024 else
1025 req = requested;
1026
Pierre Ossmancef3cf72016-11-25 10:06:34 +01001027 if (req.is_empty())
1028 return;
1029
1030 // Get the lists of updates. Prior to exporting the data to the `ui' object,
1031 // getUpdateInfo() will normalize the `updates' object such way that its
1032 // `changed' and `copied' regions would not intersect.
Pierre Ossman1b478e52011-11-15 12:08:30 +00001033 updates.getUpdateInfo(&ui, req);
1034 needNewUpdateInfo = false;
Constantin Kaplinsky604d7812007-08-31 15:50:37 +00001035
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001036 // If the previous position of the rendered cursor overlaps the source of the
1037 // copy, then when the copy happens the corresponding rectangle in the
1038 // destination will be wrong, so add it to the changed region.
1039
Pierre Ossman671d2ef2015-06-09 16:09:06 +02001040 if (!ui.copied.is_empty() && !damagedCursorRegion.is_empty()) {
1041 Region bogusCopiedCursor;
1042
1043 bogusCopiedCursor.copyFrom(damagedCursorRegion);
1044 bogusCopiedCursor.translate(ui.copy_delta);
1045 bogusCopiedCursor.assign_intersect(server->pb->getRect());
Constantin Kaplinsky45517c82007-08-31 15:56:33 +00001046 if (!ui.copied.intersect(bogusCopiedCursor).is_empty()) {
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001047 updates.add_changed(bogusCopiedCursor);
Constantin Kaplinsky604d7812007-08-31 15:50:37 +00001048 needNewUpdateInfo = true;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001049 }
1050 }
1051
Pierre Ossman671d2ef2015-06-09 16:09:06 +02001052 // If we need to remove the old rendered cursor, just add the region to
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001053 // the changed region.
1054
1055 if (removeRenderedCursor) {
Pierre Ossman671d2ef2015-06-09 16:09:06 +02001056 updates.add_changed(damagedCursorRegion);
Constantin Kaplinsky604d7812007-08-31 15:50:37 +00001057 needNewUpdateInfo = true;
Pierre Ossman671d2ef2015-06-09 16:09:06 +02001058 damagedCursorRegion.clear();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001059 removeRenderedCursor = false;
1060 }
1061
1062 // Return if there is nothing to send the client.
1063
Pierre Ossman671d2ef2015-06-09 16:09:06 +02001064 if (updates.is_empty() && !writer()->needFakeUpdate() && !updateRenderedCursor)
Pierre Ossmancef3cf72016-11-25 10:06:34 +01001065 return;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001066
Constantin Kaplinsky604d7812007-08-31 15:50:37 +00001067 // The `updates' object could change, make sure we have valid update info.
1068
1069 if (needNewUpdateInfo)
Pierre Ossman1b478e52011-11-15 12:08:30 +00001070 updates.getUpdateInfo(&ui, req);
Constantin Kaplinsky604d7812007-08-31 15:50:37 +00001071
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001072 // If the client needs a server-side rendered cursor, work out the cursor
1073 // rectangle. If it's empty then don't bother drawing it, but if it overlaps
1074 // with the update region, we need to draw the rendered cursor regardless of
1075 // whether it has changed.
1076
Pierre Ossman24684e52016-12-05 16:58:19 +01001077 cursor = NULL;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001078 if (needRenderedCursor()) {
Pierre Ossman671d2ef2015-06-09 16:09:06 +02001079 Rect renderedCursorRect;
1080
Pierre Ossman24684e52016-12-05 16:58:19 +01001081 cursor = server->getRenderedCursor();
1082
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001083 renderedCursorRect
Pierre Ossman24684e52016-12-05 16:58:19 +01001084 = cursor->getEffectiveRect().intersect(req.get_bounding_rect());
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001085
1086 if (renderedCursorRect.is_empty()) {
Pierre Ossman24684e52016-12-05 16:58:19 +01001087 cursor = NULL;
1088 } else if (!updateRenderedCursor &&
1089 ui.changed.union_(ui.copied)
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001090 .intersect(renderedCursorRect).is_empty()) {
Pierre Ossman24684e52016-12-05 16:58:19 +01001091 cursor = NULL;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001092 }
1093
Pierre Ossman5c037202016-12-05 17:00:35 +01001094 if (cursor) {
1095 updates.subtract(renderedCursorRect);
1096 updates.getUpdateInfo(&ui, req);
1097 }
Pierre Ossman671d2ef2015-06-09 16:09:06 +02001098
1099 damagedCursorRegion.assign_union(renderedCursorRect);
1100 updateRenderedCursor = false;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001101 }
1102
Pierre Ossman24684e52016-12-05 16:58:19 +01001103 if (ui.is_empty() && !writer()->needFakeUpdate() && !cursor)
Pierre Ossmancef3cf72016-11-25 10:06:34 +01001104 return;
Pierre Ossmanfdba3fe2014-01-31 13:12:18 +01001105
Pierre Ossmancef3cf72016-11-25 10:06:34 +01001106 writeRTTPing();
Pierre Ossman1b478e52011-11-15 12:08:30 +00001107
Pierre Ossmancef3cf72016-11-25 10:06:34 +01001108 encodeManager.writeUpdate(ui, server->getPixelBuffer(), cursor);
Pierre Ossman1b478e52011-11-15 12:08:30 +00001109
Pierre Ossmancef3cf72016-11-25 10:06:34 +01001110 writeRTTPing();
Pierre Ossmanc0397262014-03-14 15:59:46 +01001111
Pierre Ossmancef3cf72016-11-25 10:06:34 +01001112 // The request might be for just part of the screen, so we cannot
1113 // just clear the entire update tracker.
1114 updates.subtract(req);
Pierre Ossmanc0397262014-03-14 15:59:46 +01001115
Pierre Ossmancef3cf72016-11-25 10:06:34 +01001116 requested.clear();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001117}
1118
1119
Pierre Ossmana3ac01e2011-11-07 21:13:54 +00001120void VNCSConnectionST::screenLayoutChange(rdr::U16 reason)
1121{
1122 if (!authenticated())
1123 return;
1124
1125 cp.screenLayout = server->screenLayout;
1126
1127 if (state() != RFBSTATE_NORMAL)
1128 return;
1129
1130 writer()->writeExtendedDesktopSize(reason, 0, cp.width, cp.height,
1131 cp.screenLayout);
1132 writeFramebufferUpdate();
1133}
1134
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001135
1136// setCursor() is called whenever the cursor has changed shape or pixel format.
1137// If the client supports local cursor then it will arrange for the cursor to
1138// be sent to the client.
1139
1140void VNCSConnectionST::setCursor()
1141{
Pierre Ossmana3ac01e2011-11-07 21:13:54 +00001142 if (state() != RFBSTATE_NORMAL)
1143 return;
Pierre Ossmana3ac01e2011-11-07 21:13:54 +00001144
Pierre Ossman6a1a0d02017-02-19 15:48:17 +01001145 cp.setCursor(*server->cursor);
Pierre Ossman126e5642014-02-13 14:40:25 +01001146
Pierre Ossman8053c8e2017-02-21 12:59:04 +01001147 if (!writer()->writeSetCursorWithAlpha()) {
1148 if (!writer()->writeSetCursor()) {
1149 if (!writer()->writeSetXCursor()) {
1150 // No client support
1151 return;
1152 }
Pierre Ossman126e5642014-02-13 14:40:25 +01001153 }
1154 }
1155
Pierre Ossmana3ac01e2011-11-07 21:13:54 +00001156 writeFramebufferUpdate();
1157}
1158
1159void VNCSConnectionST::setDesktopName(const char *name)
1160{
1161 cp.setName(name);
1162
1163 if (state() != RFBSTATE_NORMAL)
1164 return;
1165
1166 if (!writer()->writeSetDesktopName()) {
1167 fprintf(stderr, "Client does not support desktop rename\n");
1168 return;
1169 }
1170
1171 writeFramebufferUpdate();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001172}
1173
1174void VNCSConnectionST::setSocketTimeouts()
1175{
1176 int timeoutms = rfb::Server::clientWaitTimeMillis;
1177 soonestTimeout(&timeoutms, secsToMillis(rfb::Server::idleTimeout));
1178 if (timeoutms == 0)
1179 timeoutms = -1;
1180 sock->inStream().setTimeout(timeoutms);
1181 sock->outStream().setTimeout(timeoutms);
1182}
1183
1184char* VNCSConnectionST::getStartTime()
1185{
1186 char* result = ctime(&startTime);
1187 result[24] = '\0';
1188 return result;
1189}
1190
1191void VNCSConnectionST::setStatus(int status)
1192{
1193 switch (status) {
1194 case 0:
1195 accessRights = accessRights | AccessPtrEvents | AccessKeyEvents | AccessView;
1196 break;
1197 case 1:
Pierre Ossman7728be22015-03-03 16:39:37 +01001198 accessRights = (accessRights & ~(AccessPtrEvents | AccessKeyEvents)) | AccessView;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001199 break;
1200 case 2:
Adam Tkac8e985062011-02-07 11:33:57 +00001201 accessRights = accessRights & ~(AccessPtrEvents | AccessKeyEvents | AccessView);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001202 break;
1203 }
1204 framebufferUpdateRequest(server->pb->getRect(), false);
1205}
1206int VNCSConnectionST::getStatus()
1207{
1208 if ((accessRights & (AccessPtrEvents | AccessKeyEvents | AccessView)) == 0x0007)
1209 return 0;
1210 if ((accessRights & (AccessPtrEvents | AccessKeyEvents | AccessView)) == 0x0001)
1211 return 1;
1212 if ((accessRights & (AccessPtrEvents | AccessKeyEvents | AccessView)) == 0x0000)
1213 return 2;
1214 return 4;
1215}
1216