Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 1 | /* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved. |
Pierre Ossman | cef3cf7 | 2016-11-25 10:06:34 +0100 | [diff] [blame] | 2 | * Copyright 2009-2016 Pierre Ossman for Cendio AB |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 3 | * |
| 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 Ossman | 1b478e5 | 2011-11-15 12:08:30 +0000 | [diff] [blame] | 20 | // 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 Ossman | a830bec | 2011-11-08 12:12:02 +0000 | [diff] [blame] | 31 | #include <network/TcpSocket.h> |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 32 | #include <rfb/VNCSConnectionST.h> |
| 33 | #include <rfb/LogWriter.h> |
Adam Tkac | 5a0caed | 2010-04-23 13:58:10 +0000 | [diff] [blame] | 34 | #include <rfb/Security.h> |
Pierre Ossman | c5e2560 | 2009-03-20 12:59:05 +0000 | [diff] [blame] | 35 | #include <rfb/screenTypes.h> |
Pierre Ossman | 2c76494 | 2011-11-14 15:54:30 +0000 | [diff] [blame] | 36 | #include <rfb/fenceTypes.h> |
Pierre Ossman | bb305ca | 2016-12-11 12:41:26 +0100 | [diff] [blame] | 37 | #include <rfb/ledStates.h> |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 38 | #include <rfb/ServerCore.h> |
| 39 | #include <rfb/ComparingUpdateTracker.h> |
| 40 | #include <rfb/KeyRemapper.h> |
Pierre Ossman | fdba3fe | 2014-01-31 13:12:18 +0100 | [diff] [blame] | 41 | #include <rfb/Encoder.h> |
Pierre Ossman | bb305ca | 2016-12-11 12:41:26 +0100 | [diff] [blame] | 42 | #define XK_LATIN1 |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 43 | #define XK_MISCELLANY |
| 44 | #define XK_XKB_KEYS |
| 45 | #include <rfb/keysymdef.h> |
| 46 | |
| 47 | using namespace rfb; |
| 48 | |
| 49 | static LogWriter vlog("VNCSConnST"); |
| 50 | |
Pierre Ossman | 1b478e5 | 2011-11-15 12:08:30 +0000 | [diff] [blame] | 51 | // This window should get us going fairly fast on a decent bandwidth network. |
| 52 | // If it's too high, it will rapidly be reduced and stay low. |
| 53 | static const unsigned INITIAL_WINDOW = 16384; |
| 54 | |
| 55 | // TCP's minimal window is 3*MSS. But since we don't know the MSS, we |
klemens | 0536d09 | 2017-01-28 20:56:56 +0100 | [diff] [blame] | 56 | // make a guess at 4 KiB (it's probably a bit higher). |
Pierre Ossman | 1b478e5 | 2011-11-15 12:08:30 +0000 | [diff] [blame] | 57 | static const unsigned MINIMUM_WINDOW = 4096; |
| 58 | |
| 59 | // The current default maximum window for Linux (4 MiB). Should be a good |
| 60 | // limit for now... |
| 61 | static const unsigned MAXIMUM_WINDOW = 4194304; |
| 62 | |
| 63 | struct RTTInfo { |
| 64 | struct timeval tv; |
| 65 | int offset; |
| 66 | unsigned inFlight; |
| 67 | }; |
| 68 | |
Pierre Ossman | 71ca8d5 | 2017-09-15 11:03:12 +0200 | [diff] [blame] | 69 | static Cursor emptyCursor(0, 0, Point(0, 0), NULL); |
| 70 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 71 | VNCSConnectionST::VNCSConnectionST(VNCServerST* server_, network::Socket *s, |
| 72 | bool reverse) |
Pierre Ossman | 7069bdd | 2015-02-06 14:41:58 +0100 | [diff] [blame] | 73 | : sock(s), reverseConnection(reverse), |
Pierre Ossman | f8e3b34 | 2015-01-26 14:37:04 +0100 | [diff] [blame] | 74 | queryConnectTimer(this), inProcessMessages(false), |
Pierre Ossman | b8b1e96 | 2012-07-20 10:47:00 +0000 | [diff] [blame] | 75 | pendingSyncFence(false), syncFence(false), fenceFlags(0), |
| 76 | fenceDataLen(0), fenceData(NULL), |
Pierre Ossman | b1cd6ca | 2015-03-03 16:37:43 +0100 | [diff] [blame] | 77 | baseRTT(-1), congWindow(0), ackedOffset(0), sentOffset(0), |
| 78 | minRTT(-1), seenCongestion(false), |
| 79 | pingCounter(0), congestionTimer(this), |
Pierre Ossman | 0c9bd4b | 2014-07-09 16:44:11 +0200 | [diff] [blame] | 80 | server(server_), updates(false), |
Pierre Ossman | 671d2ef | 2015-06-09 16:09:06 +0200 | [diff] [blame] | 81 | updateRenderedCursor(false), removeRenderedCursor(false), |
Pierre Ossman | a40ab20 | 2016-04-29 15:35:56 +0200 | [diff] [blame] | 82 | continuousUpdates(false), encodeManager(this), pointerEventTime(0), |
Pierre Ossman | 1bb8b6c | 2011-10-25 15:20:05 +0000 | [diff] [blame] | 83 | accessRights(AccessDefault), startTime(time(0)) |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 84 | { |
| 85 | setStreams(&sock->inStream(), &sock->outStream()); |
| 86 | peerEndpoint.buf = sock->getPeerEndpoint(); |
| 87 | VNCServerST::connectionsLog.write(1,"accepted: %s", peerEndpoint.buf); |
| 88 | |
| 89 | // Configure the socket |
| 90 | setSocketTimeouts(); |
| 91 | lastEventTime = time(0); |
| 92 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 93 | server->clients.push_front(this); |
| 94 | } |
| 95 | |
| 96 | |
| 97 | VNCSConnectionST::~VNCSConnectionST() |
| 98 | { |
| 99 | // If we reach here then VNCServerST is deleting us! |
| 100 | VNCServerST::connectionsLog.write(1,"closed: %s (%s)", |
| 101 | peerEndpoint.buf, |
| 102 | (closeReason.buf) ? closeReason.buf : ""); |
| 103 | |
| 104 | // Release any keys the client still had pressed |
Pierre Ossman | 16e1dcb | 2017-05-16 14:33:43 +0200 | [diff] [blame] | 105 | while (!pressedKeys.empty()) { |
| 106 | rdr::U32 keysym, keycode; |
| 107 | |
| 108 | keysym = pressedKeys.begin()->second; |
| 109 | keycode = pressedKeys.begin()->first; |
| 110 | pressedKeys.erase(pressedKeys.begin()); |
| 111 | |
| 112 | vlog.debug("Releasing key 0x%x / 0x%x on client disconnect", |
| 113 | keysym, keycode); |
| 114 | server->desktop->keyEvent(keysym, keycode, false); |
Pierre Ossman | 9a153b0 | 2015-08-31 10:01:14 +0200 | [diff] [blame] | 115 | } |
Pierre Ossman | 16e1dcb | 2017-05-16 14:33:43 +0200 | [diff] [blame] | 116 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 117 | if (server->pointerClient == this) |
| 118 | server->pointerClient = 0; |
| 119 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 120 | // Remove this client from the server |
| 121 | server->clients.remove(this); |
| 122 | |
Pierre Ossman | 2c76494 | 2011-11-14 15:54:30 +0000 | [diff] [blame] | 123 | delete [] fenceData; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | |
| 127 | // Methods called from VNCServerST |
| 128 | |
| 129 | bool VNCSConnectionST::init() |
| 130 | { |
| 131 | try { |
| 132 | initialiseProtocol(); |
| 133 | } catch (rdr::Exception& e) { |
| 134 | close(e.str()); |
| 135 | return false; |
| 136 | } |
| 137 | return true; |
| 138 | } |
| 139 | |
| 140 | void VNCSConnectionST::close(const char* reason) |
| 141 | { |
| 142 | // Log the reason for the close |
| 143 | if (!closeReason.buf) |
Adam Tkac | d36b626 | 2009-09-04 10:57:20 +0000 | [diff] [blame] | 144 | closeReason.buf = strDup(reason); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 145 | else |
| 146 | vlog.debug("second close: %s (%s)", peerEndpoint.buf, reason); |
| 147 | |
| 148 | if (authenticated()) { |
| 149 | server->lastDisconnectTime = time(0); |
| 150 | } |
| 151 | |
| 152 | // Just shutdown the socket and mark our state as closing. Eventually the |
| 153 | // calling code will call VNCServerST's removeSocket() method causing us to |
| 154 | // be deleted. |
| 155 | sock->shutdown(); |
| 156 | setState(RFBSTATE_CLOSING); |
| 157 | } |
| 158 | |
| 159 | |
| 160 | void VNCSConnectionST::processMessages() |
| 161 | { |
| 162 | if (state() == RFBSTATE_CLOSING) return; |
| 163 | try { |
| 164 | // - Now set appropriate socket timeouts and process data |
| 165 | setSocketTimeouts(); |
Pierre Ossman | a3ac01e | 2011-11-07 21:13:54 +0000 | [diff] [blame] | 166 | |
| 167 | inProcessMessages = true; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 168 | |
Pierre Ossman | a830bec | 2011-11-08 12:12:02 +0000 | [diff] [blame] | 169 | // Get the underlying TCP layer to build large packets if we send |
| 170 | // multiple small responses. |
| 171 | network::TcpSocket::cork(sock->getFd(), true); |
| 172 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 173 | while (getInStream()->checkNoWait(1)) { |
Pierre Ossman | b8b1e96 | 2012-07-20 10:47:00 +0000 | [diff] [blame] | 174 | if (pendingSyncFence) { |
| 175 | syncFence = true; |
| 176 | pendingSyncFence = false; |
| 177 | } |
| 178 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 179 | processMsg(); |
Pierre Ossman | b8b1e96 | 2012-07-20 10:47:00 +0000 | [diff] [blame] | 180 | |
Pierre Ossman | 2c76494 | 2011-11-14 15:54:30 +0000 | [diff] [blame] | 181 | if (syncFence) { |
| 182 | writer()->writeFence(fenceFlags, fenceDataLen, fenceData); |
| 183 | syncFence = false; |
| 184 | } |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 185 | } |
| 186 | |
Pierre Ossman | a830bec | 2011-11-08 12:12:02 +0000 | [diff] [blame] | 187 | // Flush out everything in case we go idle after this. |
| 188 | network::TcpSocket::cork(sock->getFd(), false); |
| 189 | |
Pierre Ossman | a3ac01e | 2011-11-07 21:13:54 +0000 | [diff] [blame] | 190 | inProcessMessages = false; |
Constantin Kaplinsky | 2ef6695 | 2008-08-29 11:33:46 +0000 | [diff] [blame] | 191 | |
Pierre Ossman | a3ac01e | 2011-11-07 21:13:54 +0000 | [diff] [blame] | 192 | // If there were anything requiring an update, try to send it here. |
| 193 | // We wait until now with this to aggregate responses and to give |
| 194 | // higher priority to user actions such as keyboard and pointer events. |
| 195 | writeFramebufferUpdate(); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 196 | } catch (rdr::EndOfStream&) { |
| 197 | close("Clean disconnection"); |
| 198 | } catch (rdr::Exception &e) { |
| 199 | close(e.str()); |
| 200 | } |
| 201 | } |
| 202 | |
Pierre Ossman | d408ca5 | 2016-04-29 14:26:05 +0200 | [diff] [blame] | 203 | void VNCSConnectionST::flushSocket() |
| 204 | { |
| 205 | if (state() == RFBSTATE_CLOSING) return; |
| 206 | try { |
| 207 | setSocketTimeouts(); |
| 208 | sock->outStream().flush(); |
Pierre Ossman | a40ab20 | 2016-04-29 15:35:56 +0200 | [diff] [blame] | 209 | // Flushing the socket might release an update that was previously |
| 210 | // delayed because of congestion. |
| 211 | if (sock->outStream().bufferUsage() == 0) |
| 212 | writeFramebufferUpdate(); |
Pierre Ossman | d408ca5 | 2016-04-29 14:26:05 +0200 | [diff] [blame] | 213 | } catch (rdr::Exception &e) { |
| 214 | close(e.str()); |
| 215 | } |
| 216 | } |
| 217 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 218 | void VNCSConnectionST::pixelBufferChange() |
| 219 | { |
| 220 | try { |
| 221 | if (!authenticated()) return; |
| 222 | if (cp.width && cp.height && (server->pb->width() != cp.width || |
| 223 | server->pb->height() != cp.height)) |
| 224 | { |
| 225 | // We need to clip the next update to the new size, but also add any |
| 226 | // extra bits if it's bigger. If we wanted to do this exactly, something |
| 227 | // like the code below would do it, but at the moment we just update the |
Pierre Ossman | 671d2ef | 2015-06-09 16:09:06 +0200 | [diff] [blame] | 228 | // entire new size. However, we do need to clip the damagedCursorRegion |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 229 | // because that might be added to updates in writeFramebufferUpdate(). |
| 230 | |
| 231 | //updates.intersect(server->pb->getRect()); |
| 232 | // |
| 233 | //if (server->pb->width() > cp.width) |
| 234 | // updates.add_changed(Rect(cp.width, 0, server->pb->width(), |
| 235 | // server->pb->height())); |
| 236 | //if (server->pb->height() > cp.height) |
| 237 | // updates.add_changed(Rect(0, cp.height, cp.width, |
| 238 | // server->pb->height())); |
| 239 | |
Pierre Ossman | 671d2ef | 2015-06-09 16:09:06 +0200 | [diff] [blame] | 240 | damagedCursorRegion.assign_intersect(server->pb->getRect()); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 241 | |
| 242 | cp.width = server->pb->width(); |
| 243 | cp.height = server->pb->height(); |
Pierre Ossman | 34e62f3 | 2009-03-20 21:46:12 +0000 | [diff] [blame] | 244 | cp.screenLayout = server->screenLayout; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 245 | if (state() == RFBSTATE_NORMAL) { |
Pierre Ossman | 2ee430a | 2009-05-28 12:54:24 +0000 | [diff] [blame] | 246 | // We should only send EDS to client asking for both |
| 247 | if (!writer()->writeExtendedDesktopSize()) { |
| 248 | if (!writer()->writeSetDesktopSize()) { |
| 249 | close("Client does not support desktop resize"); |
| 250 | return; |
| 251 | } |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 252 | } |
| 253 | } |
| 254 | } |
| 255 | // Just update the whole screen at the moment because we're too lazy to |
| 256 | // work out what's actually changed. |
| 257 | updates.clear(); |
| 258 | updates.add_changed(server->pb->getRect()); |
Pierre Ossman | a3ac01e | 2011-11-07 21:13:54 +0000 | [diff] [blame] | 259 | writeFramebufferUpdate(); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 260 | } catch(rdr::Exception &e) { |
| 261 | close(e.str()); |
| 262 | } |
| 263 | } |
| 264 | |
Pierre Ossman | a3ac01e | 2011-11-07 21:13:54 +0000 | [diff] [blame] | 265 | void VNCSConnectionST::writeFramebufferUpdateOrClose() |
Pierre Ossman | 04e62db | 2009-03-23 16:57:07 +0000 | [diff] [blame] | 266 | { |
| 267 | try { |
Pierre Ossman | a3ac01e | 2011-11-07 21:13:54 +0000 | [diff] [blame] | 268 | writeFramebufferUpdate(); |
| 269 | } catch(rdr::Exception &e) { |
| 270 | close(e.str()); |
| 271 | } |
| 272 | } |
Pierre Ossman | 04e62db | 2009-03-23 16:57:07 +0000 | [diff] [blame] | 273 | |
Pierre Ossman | a3ac01e | 2011-11-07 21:13:54 +0000 | [diff] [blame] | 274 | void VNCSConnectionST::screenLayoutChangeOrClose(rdr::U16 reason) |
| 275 | { |
| 276 | try { |
| 277 | screenLayoutChange(reason); |
Pierre Ossman | 04e62db | 2009-03-23 16:57:07 +0000 | [diff] [blame] | 278 | } catch(rdr::Exception &e) { |
| 279 | close(e.str()); |
| 280 | } |
| 281 | } |
| 282 | |
Pierre Ossman | a3ac01e | 2011-11-07 21:13:54 +0000 | [diff] [blame] | 283 | void VNCSConnectionST::bellOrClose() |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 284 | { |
| 285 | try { |
| 286 | if (state() == RFBSTATE_NORMAL) writer()->writeBell(); |
| 287 | } catch(rdr::Exception& e) { |
| 288 | close(e.str()); |
| 289 | } |
| 290 | } |
| 291 | |
Pierre Ossman | a3ac01e | 2011-11-07 21:13:54 +0000 | [diff] [blame] | 292 | void VNCSConnectionST::serverCutTextOrClose(const char *str, int len) |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 293 | { |
| 294 | try { |
| 295 | if (!(accessRights & AccessCutText)) return; |
| 296 | if (!rfb::Server::sendCutText) return; |
| 297 | if (state() == RFBSTATE_NORMAL) |
| 298 | writer()->writeServerCutText(str, len); |
| 299 | } catch(rdr::Exception& e) { |
| 300 | close(e.str()); |
| 301 | } |
| 302 | } |
| 303 | |
Peter Ã…strand | c39e078 | 2009-01-15 12:21:42 +0000 | [diff] [blame] | 304 | |
Pierre Ossman | a3ac01e | 2011-11-07 21:13:54 +0000 | [diff] [blame] | 305 | void VNCSConnectionST::setDesktopNameOrClose(const char *name) |
Peter Ã…strand | c39e078 | 2009-01-15 12:21:42 +0000 | [diff] [blame] | 306 | { |
Peter Ã…strand | c39e078 | 2009-01-15 12:21:42 +0000 | [diff] [blame] | 307 | try { |
Pierre Ossman | a3ac01e | 2011-11-07 21:13:54 +0000 | [diff] [blame] | 308 | setDesktopName(name); |
Peter Ã…strand | c39e078 | 2009-01-15 12:21:42 +0000 | [diff] [blame] | 309 | } catch(rdr::Exception& e) { |
| 310 | close(e.str()); |
| 311 | } |
| 312 | } |
| 313 | |
| 314 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 315 | void VNCSConnectionST::setCursorOrClose() |
| 316 | { |
| 317 | try { |
| 318 | setCursor(); |
| 319 | } catch(rdr::Exception& e) { |
| 320 | close(e.str()); |
| 321 | } |
| 322 | } |
| 323 | |
| 324 | |
Pierre Ossman | b45a84f | 2016-12-12 16:59:15 +0100 | [diff] [blame] | 325 | void VNCSConnectionST::setLEDStateOrClose(unsigned int state) |
| 326 | { |
| 327 | try { |
| 328 | setLEDState(state); |
| 329 | } catch(rdr::Exception& e) { |
| 330 | close(e.str()); |
| 331 | } |
| 332 | } |
| 333 | |
| 334 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 335 | int VNCSConnectionST::checkIdleTimeout() |
| 336 | { |
| 337 | int idleTimeout = rfb::Server::idleTimeout; |
| 338 | if (idleTimeout == 0) return 0; |
| 339 | if (state() != RFBSTATE_NORMAL && idleTimeout < 15) |
| 340 | idleTimeout = 15; // minimum of 15 seconds while authenticating |
| 341 | time_t now = time(0); |
| 342 | if (now < lastEventTime) { |
| 343 | // Someone must have set the time backwards. Set lastEventTime so that the |
| 344 | // idleTimeout will count from now. |
| 345 | vlog.info("Time has gone backwards - resetting idle timeout"); |
| 346 | lastEventTime = now; |
| 347 | } |
| 348 | int timeLeft = lastEventTime + idleTimeout - now; |
| 349 | if (timeLeft < -60) { |
| 350 | // Our callback is over a minute late - someone must have set the time |
| 351 | // forwards. Set lastEventTime so that the idleTimeout will count from |
| 352 | // now. |
| 353 | vlog.info("Time has gone forwards - resetting idle timeout"); |
| 354 | lastEventTime = now; |
| 355 | return secsToMillis(idleTimeout); |
| 356 | } |
| 357 | if (timeLeft <= 0) { |
| 358 | close("Idle timeout"); |
| 359 | return 0; |
| 360 | } |
| 361 | return secsToMillis(timeLeft); |
| 362 | } |
| 363 | |
Pierre Ossman | b114cec | 2011-11-20 15:36:11 +0000 | [diff] [blame] | 364 | |
| 365 | bool VNCSConnectionST::getComparerState() |
| 366 | { |
| 367 | // We interpret a low compression level as an indication that the client |
| 368 | // wants to prioritise CPU usage over bandwidth, and hence disable the |
| 369 | // comparing update tracker. |
| 370 | return (cp.compressLevel == -1) || (cp.compressLevel > 1); |
| 371 | } |
| 372 | |
| 373 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 374 | // renderedCursorChange() is called whenever the server-side rendered cursor |
| 375 | // changes shape or position. It ensures that the next update will clean up |
| 376 | // the old rendered cursor and if necessary draw the new rendered cursor. |
| 377 | |
| 378 | void VNCSConnectionST::renderedCursorChange() |
| 379 | { |
| 380 | if (state() != RFBSTATE_NORMAL) return; |
Pierre Ossman | 71ca8d5 | 2017-09-15 11:03:12 +0200 | [diff] [blame] | 381 | // Are we switching between client-side and server-side cursor? |
| 382 | if (damagedCursorRegion.is_empty() != needRenderedCursor()) |
| 383 | setCursorOrClose(); |
Pierre Ossman | 671d2ef | 2015-06-09 16:09:06 +0200 | [diff] [blame] | 384 | if (!damagedCursorRegion.is_empty()) |
Pierre Ossman | 5c9e1e5 | 2011-11-08 10:32:05 +0000 | [diff] [blame] | 385 | removeRenderedCursor = true; |
Pierre Ossman | 6b0bc29 | 2011-12-21 13:17:54 +0000 | [diff] [blame] | 386 | if (needRenderedCursor()) { |
Pierre Ossman | 671d2ef | 2015-06-09 16:09:06 +0200 | [diff] [blame] | 387 | updateRenderedCursor = true; |
Pierre Ossman | 6b0bc29 | 2011-12-21 13:17:54 +0000 | [diff] [blame] | 388 | writeFramebufferUpdateOrClose(); |
| 389 | } |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 390 | } |
| 391 | |
| 392 | // needRenderedCursor() returns true if this client needs the server-side |
| 393 | // rendered cursor. This may be because it does not support local cursor or |
| 394 | // because the current cursor position has not been set by this client. |
| 395 | // Unfortunately we can't know for sure when the current cursor position has |
| 396 | // been set by this client. We guess that this is the case when the current |
| 397 | // cursor position is the same as the last pointer event from this client, or |
| 398 | // if it is a very short time since this client's last pointer event (up to a |
| 399 | // second). [ Ideally we should do finer-grained timing here and make the time |
| 400 | // configurable, but I don't think it's that important. ] |
| 401 | |
| 402 | bool VNCSConnectionST::needRenderedCursor() |
| 403 | { |
Pierre Ossman | 77ede0a | 2016-12-05 16:57:30 +0100 | [diff] [blame] | 404 | if (state() != RFBSTATE_NORMAL) |
| 405 | return false; |
| 406 | |
Pierre Ossman | 324043e | 2017-08-16 16:26:11 +0200 | [diff] [blame] | 407 | if (!cp.supportsLocalCursorWithAlpha && |
| 408 | !cp.supportsLocalCursor && !cp.supportsLocalXCursor) |
Pierre Ossman | 77ede0a | 2016-12-05 16:57:30 +0100 | [diff] [blame] | 409 | return true; |
| 410 | if (!server->cursorPos.equals(pointerEventPos) && |
| 411 | (time(0) - pointerEventTime) > 0) |
| 412 | return true; |
| 413 | |
| 414 | return false; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 415 | } |
| 416 | |
| 417 | |
| 418 | void VNCSConnectionST::approveConnectionOrClose(bool accept, |
| 419 | const char* reason) |
| 420 | { |
| 421 | try { |
| 422 | approveConnection(accept, reason); |
| 423 | } catch (rdr::Exception& e) { |
| 424 | close(e.str()); |
| 425 | } |
| 426 | } |
| 427 | |
| 428 | |
| 429 | |
| 430 | // -=- Callbacks from SConnection |
| 431 | |
| 432 | void VNCSConnectionST::authSuccess() |
| 433 | { |
| 434 | lastEventTime = time(0); |
| 435 | |
| 436 | server->startDesktop(); |
| 437 | |
| 438 | // - Set the connection parameters appropriately |
| 439 | cp.width = server->pb->width(); |
| 440 | cp.height = server->pb->height(); |
Pierre Ossman | 34e62f3 | 2009-03-20 21:46:12 +0000 | [diff] [blame] | 441 | cp.screenLayout = server->screenLayout; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 442 | cp.setName(server->getName()); |
Pierre Ossman | b45a84f | 2016-12-12 16:59:15 +0100 | [diff] [blame] | 443 | cp.setLEDState(server->ledState); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 444 | |
| 445 | // - Set the default pixel format |
| 446 | cp.setPF(server->pb->getPF()); |
| 447 | char buffer[256]; |
| 448 | cp.pf().print(buffer, 256); |
| 449 | vlog.info("Server default pixel format %s", buffer); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 450 | |
| 451 | // - Mark the entire display as "dirty" |
| 452 | updates.add_changed(server->pb->getRect()); |
| 453 | startTime = time(0); |
Pierre Ossman | 1b478e5 | 2011-11-15 12:08:30 +0000 | [diff] [blame] | 454 | |
| 455 | // - Bootstrap the congestion control |
| 456 | ackedOffset = sock->outStream().length(); |
| 457 | congWindow = INITIAL_WINDOW; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 458 | } |
| 459 | |
| 460 | void VNCSConnectionST::queryConnection(const char* userName) |
| 461 | { |
| 462 | // - Authentication succeeded - clear from blacklist |
| 463 | CharArray name; name.buf = sock->getPeerAddress(); |
| 464 | server->blHosts->clearBlackmark(name.buf); |
| 465 | |
| 466 | // - Special case to provide a more useful error message |
| 467 | if (rfb::Server::neverShared && !rfb::Server::disconnectClients && |
| 468 | server->authClientCount() > 0) { |
| 469 | approveConnection(false, "The server is already in use"); |
| 470 | return; |
| 471 | } |
| 472 | |
| 473 | // - Does the client have the right to bypass the query? |
| 474 | if (reverseConnection || |
| 475 | !(rfb::Server::queryConnect || sock->requiresQuery()) || |
| 476 | (accessRights & AccessNoQuery)) |
| 477 | { |
| 478 | approveConnection(true); |
| 479 | return; |
| 480 | } |
| 481 | |
| 482 | // - Get the server to display an Accept/Reject dialog, if required |
| 483 | // If a dialog is displayed, the result will be PENDING, and the |
| 484 | // server will call approveConnection at a later time |
| 485 | CharArray reason; |
| 486 | VNCServerST::queryResult qr = server->queryConnection(sock, userName, |
| 487 | &reason.buf); |
Pierre Ossman | f8e3b34 | 2015-01-26 14:37:04 +0100 | [diff] [blame] | 488 | if (qr == VNCServerST::PENDING) { |
| 489 | queryConnectTimer.start(rfb::Server::queryConnectTimeout * 1000); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 490 | return; |
Pierre Ossman | f8e3b34 | 2015-01-26 14:37:04 +0100 | [diff] [blame] | 491 | } |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 492 | |
| 493 | // - If server returns ACCEPT/REJECT then pass result to SConnection |
| 494 | approveConnection(qr == VNCServerST::ACCEPT, reason.buf); |
| 495 | } |
| 496 | |
| 497 | void VNCSConnectionST::clientInit(bool shared) |
| 498 | { |
| 499 | lastEventTime = time(0); |
| 500 | if (rfb::Server::alwaysShared || reverseConnection) shared = true; |
Pierre Ossman | e7be49b | 2014-12-02 14:33:17 +0100 | [diff] [blame] | 501 | if (!(accessRights & AccessNonShared)) shared = true; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 502 | if (rfb::Server::neverShared) shared = false; |
| 503 | if (!shared) { |
Pierre Ossman | e7be49b | 2014-12-02 14:33:17 +0100 | [diff] [blame] | 504 | if (rfb::Server::disconnectClients && (accessRights & AccessNonShared)) { |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 505 | // - Close all the other connected clients |
| 506 | vlog.debug("non-shared connection - closing clients"); |
| 507 | server->closeClients("Non-shared connection requested", getSock()); |
| 508 | } else { |
| 509 | // - Refuse this connection if there are existing clients, in addition to |
| 510 | // this one |
| 511 | if (server->authClientCount() > 1) { |
| 512 | close("Server is already in use"); |
| 513 | return; |
| 514 | } |
| 515 | } |
| 516 | } |
| 517 | SConnection::clientInit(shared); |
| 518 | } |
| 519 | |
| 520 | void VNCSConnectionST::setPixelFormat(const PixelFormat& pf) |
| 521 | { |
| 522 | SConnection::setPixelFormat(pf); |
| 523 | char buffer[256]; |
| 524 | pf.print(buffer, 256); |
| 525 | vlog.info("Client pixel format %s", buffer); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 526 | setCursor(); |
| 527 | } |
| 528 | |
| 529 | void VNCSConnectionST::pointerEvent(const Point& pos, int buttonMask) |
| 530 | { |
| 531 | pointerEventTime = lastEventTime = time(0); |
| 532 | server->lastUserInputTime = lastEventTime; |
| 533 | if (!(accessRights & AccessPtrEvents)) return; |
| 534 | if (!rfb::Server::acceptPointerEvents) return; |
| 535 | if (!server->pointerClient || server->pointerClient == this) { |
| 536 | pointerEventPos = pos; |
| 537 | if (buttonMask) |
| 538 | server->pointerClient = this; |
| 539 | else |
| 540 | server->pointerClient = 0; |
| 541 | server->desktop->pointerEvent(pointerEventPos, buttonMask); |
| 542 | } |
| 543 | } |
| 544 | |
| 545 | |
| 546 | class VNCSConnectionSTShiftPresser { |
| 547 | public: |
| 548 | VNCSConnectionSTShiftPresser(SDesktop* desktop_) |
| 549 | : desktop(desktop_), pressed(false) {} |
| 550 | ~VNCSConnectionSTShiftPresser() { |
Pierre Ossman | 9a153b0 | 2015-08-31 10:01:14 +0200 | [diff] [blame] | 551 | if (pressed) { |
| 552 | vlog.debug("Releasing fake Shift_L"); |
Pierre Ossman | 5ae2821 | 2017-05-16 14:30:38 +0200 | [diff] [blame] | 553 | desktop->keyEvent(XK_Shift_L, 0, false); |
Pierre Ossman | 9a153b0 | 2015-08-31 10:01:14 +0200 | [diff] [blame] | 554 | } |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 555 | } |
| 556 | void press() { |
Pierre Ossman | 9a153b0 | 2015-08-31 10:01:14 +0200 | [diff] [blame] | 557 | vlog.debug("Pressing fake Shift_L"); |
Pierre Ossman | 5ae2821 | 2017-05-16 14:30:38 +0200 | [diff] [blame] | 558 | desktop->keyEvent(XK_Shift_L, 0, true); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 559 | pressed = true; |
| 560 | } |
| 561 | SDesktop* desktop; |
| 562 | bool pressed; |
| 563 | }; |
| 564 | |
| 565 | // keyEvent() - record in the pressedKeys which keys were pressed. Allow |
| 566 | // multiple down events (for autorepeat), but only allow a single up event. |
Pierre Ossman | 5ae2821 | 2017-05-16 14:30:38 +0200 | [diff] [blame] | 567 | void VNCSConnectionST::keyEvent(rdr::U32 keysym, rdr::U32 keycode, bool down) { |
Pierre Ossman | 16e1dcb | 2017-05-16 14:33:43 +0200 | [diff] [blame] | 568 | rdr::U32 lookup; |
| 569 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 570 | lastEventTime = time(0); |
| 571 | server->lastUserInputTime = lastEventTime; |
| 572 | if (!(accessRights & AccessKeyEvents)) return; |
| 573 | if (!rfb::Server::acceptKeyEvents) return; |
| 574 | |
Pierre Ossman | 9a153b0 | 2015-08-31 10:01:14 +0200 | [diff] [blame] | 575 | if (down) |
Pierre Ossman | 5ae2821 | 2017-05-16 14:30:38 +0200 | [diff] [blame] | 576 | vlog.debug("Key pressed: 0x%x / 0x%x", keysym, keycode); |
Pierre Ossman | 9a153b0 | 2015-08-31 10:01:14 +0200 | [diff] [blame] | 577 | else |
Pierre Ossman | 5ae2821 | 2017-05-16 14:30:38 +0200 | [diff] [blame] | 578 | vlog.debug("Key released: 0x%x / 0x%x", keysym, keycode); |
Pierre Ossman | 9a153b0 | 2015-08-31 10:01:14 +0200 | [diff] [blame] | 579 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 580 | // Remap the key if required |
Pierre Ossman | 9a153b0 | 2015-08-31 10:01:14 +0200 | [diff] [blame] | 581 | if (server->keyRemapper) { |
| 582 | rdr::U32 newkey; |
Pierre Ossman | 5ae2821 | 2017-05-16 14:30:38 +0200 | [diff] [blame] | 583 | newkey = server->keyRemapper->remapKey(keysym); |
| 584 | if (newkey != keysym) { |
Pierre Ossman | 9a153b0 | 2015-08-31 10:01:14 +0200 | [diff] [blame] | 585 | vlog.debug("Key remapped to 0x%x", newkey); |
Pierre Ossman | 5ae2821 | 2017-05-16 14:30:38 +0200 | [diff] [blame] | 586 | keysym = newkey; |
Pierre Ossman | 9a153b0 | 2015-08-31 10:01:14 +0200 | [diff] [blame] | 587 | } |
| 588 | } |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 589 | |
Pierre Ossman | bb305ca | 2016-12-11 12:41:26 +0100 | [diff] [blame] | 590 | // Avoid lock keys if we don't know the server state |
| 591 | if ((server->ledState == ledUnknown) && |
Pierre Ossman | 5ae2821 | 2017-05-16 14:30:38 +0200 | [diff] [blame] | 592 | ((keysym == XK_Caps_Lock) || |
| 593 | (keysym == XK_Num_Lock) || |
| 594 | (keysym == XK_Scroll_Lock))) { |
Pierre Ossman | bb305ca | 2016-12-11 12:41:26 +0100 | [diff] [blame] | 595 | vlog.debug("Ignoring lock key (e.g. caps lock)"); |
| 596 | return; |
| 597 | } |
Pierre Ossman | bb305ca | 2016-12-11 12:41:26 +0100 | [diff] [blame] | 598 | |
Pierre Ossman | b45a84f | 2016-12-12 16:59:15 +0100 | [diff] [blame] | 599 | // Lock key heuristics |
| 600 | // (only for clients that do not support the LED state extension) |
| 601 | if (!cp.supportsLEDState) { |
| 602 | // Always ignore ScrollLock as we don't have a heuristic |
| 603 | // for that |
Pierre Ossman | 5ae2821 | 2017-05-16 14:30:38 +0200 | [diff] [blame] | 604 | if (keysym == XK_Scroll_Lock) { |
Pierre Ossman | b45a84f | 2016-12-12 16:59:15 +0100 | [diff] [blame] | 605 | vlog.debug("Ignoring lock key (e.g. caps lock)"); |
| 606 | return; |
Pierre Ossman | bb305ca | 2016-12-11 12:41:26 +0100 | [diff] [blame] | 607 | } |
| 608 | |
Pierre Ossman | b45a84f | 2016-12-12 16:59:15 +0100 | [diff] [blame] | 609 | if (down && (server->ledState != ledUnknown)) { |
| 610 | // CapsLock synchronisation heuristic |
| 611 | // (this assumes standard interaction between CapsLock the Shift |
| 612 | // keys and normal characters) |
Pierre Ossman | 5ae2821 | 2017-05-16 14:30:38 +0200 | [diff] [blame] | 613 | if (((keysym >= XK_A) && (keysym <= XK_Z)) || |
| 614 | ((keysym >= XK_a) && (keysym <= XK_z))) { |
Pierre Ossman | b45a84f | 2016-12-12 16:59:15 +0100 | [diff] [blame] | 615 | bool uppercase, shift, lock; |
Pierre Ossman | bb305ca | 2016-12-11 12:41:26 +0100 | [diff] [blame] | 616 | |
Pierre Ossman | 5ae2821 | 2017-05-16 14:30:38 +0200 | [diff] [blame] | 617 | uppercase = (keysym >= XK_A) && (keysym <= XK_Z); |
Pierre Ossman | 851e680 | 2017-09-12 16:44:44 +0200 | [diff] [blame^] | 618 | shift = isShiftPressed(); |
Pierre Ossman | b45a84f | 2016-12-12 16:59:15 +0100 | [diff] [blame] | 619 | lock = server->ledState & ledCapsLock; |
Pierre Ossman | bb305ca | 2016-12-11 12:41:26 +0100 | [diff] [blame] | 620 | |
Pierre Ossman | b45a84f | 2016-12-12 16:59:15 +0100 | [diff] [blame] | 621 | if (lock == (uppercase == shift)) { |
| 622 | vlog.debug("Inserting fake CapsLock to get in sync with client"); |
Pierre Ossman | 5ae2821 | 2017-05-16 14:30:38 +0200 | [diff] [blame] | 623 | server->desktop->keyEvent(XK_Caps_Lock, 0, true); |
| 624 | server->desktop->keyEvent(XK_Caps_Lock, 0, false); |
Pierre Ossman | b45a84f | 2016-12-12 16:59:15 +0100 | [diff] [blame] | 625 | } |
| 626 | } |
| 627 | |
| 628 | // NumLock synchronisation heuristic |
| 629 | // (this is more cautious because of the differences between Unix, |
| 630 | // Windows and macOS) |
Pierre Ossman | 5ae2821 | 2017-05-16 14:30:38 +0200 | [diff] [blame] | 631 | if (((keysym >= XK_KP_Home) && (keysym <= XK_KP_Delete)) || |
| 632 | ((keysym >= XK_KP_0) && (keysym <= XK_KP_9)) || |
| 633 | (keysym == XK_KP_Separator) || (keysym == XK_KP_Decimal)) { |
Pierre Ossman | b45a84f | 2016-12-12 16:59:15 +0100 | [diff] [blame] | 634 | bool number, shift, lock; |
| 635 | |
Pierre Ossman | 5ae2821 | 2017-05-16 14:30:38 +0200 | [diff] [blame] | 636 | number = ((keysym >= XK_KP_0) && (keysym <= XK_KP_9)) || |
| 637 | (keysym == XK_KP_Separator) || (keysym == XK_KP_Decimal); |
Pierre Ossman | 851e680 | 2017-09-12 16:44:44 +0200 | [diff] [blame^] | 638 | shift = isShiftPressed(); |
Pierre Ossman | b45a84f | 2016-12-12 16:59:15 +0100 | [diff] [blame] | 639 | lock = server->ledState & ledNumLock; |
| 640 | |
| 641 | if (shift) { |
| 642 | // We don't know the appropriate NumLock state for when Shift |
| 643 | // is pressed as it could be one of: |
| 644 | // |
| 645 | // a) A Unix client where Shift negates NumLock |
| 646 | // |
| 647 | // b) A Windows client where Shift only cancels NumLock |
| 648 | // |
| 649 | // c) A macOS client where Shift doesn't have any effect |
| 650 | // |
| 651 | } else if (lock == (number == shift)) { |
| 652 | vlog.debug("Inserting fake NumLock to get in sync with client"); |
Pierre Ossman | 5ae2821 | 2017-05-16 14:30:38 +0200 | [diff] [blame] | 653 | server->desktop->keyEvent(XK_Num_Lock, 0, true); |
| 654 | server->desktop->keyEvent(XK_Num_Lock, 0, false); |
Pierre Ossman | b45a84f | 2016-12-12 16:59:15 +0100 | [diff] [blame] | 655 | } |
Pierre Ossman | bb305ca | 2016-12-11 12:41:26 +0100 | [diff] [blame] | 656 | } |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 657 | } |
| 658 | } |
| 659 | |
| 660 | // Turn ISO_Left_Tab into shifted Tab. |
| 661 | VNCSConnectionSTShiftPresser shiftPresser(server->desktop); |
Pierre Ossman | 5ae2821 | 2017-05-16 14:30:38 +0200 | [diff] [blame] | 662 | if (keysym == XK_ISO_Left_Tab) { |
Pierre Ossman | 851e680 | 2017-09-12 16:44:44 +0200 | [diff] [blame^] | 663 | if (!isShiftPressed()) |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 664 | shiftPresser.press(); |
Pierre Ossman | 5ae2821 | 2017-05-16 14:30:38 +0200 | [diff] [blame] | 665 | keysym = XK_Tab; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 666 | } |
| 667 | |
Pierre Ossman | 16e1dcb | 2017-05-16 14:33:43 +0200 | [diff] [blame] | 668 | // We need to be able to track keys, so generate a fake index when we |
| 669 | // aren't given a keycode |
| 670 | if (keycode == 0) |
| 671 | lookup = 0x80000000 | keysym; |
| 672 | else |
| 673 | lookup = keycode; |
| 674 | |
| 675 | // We force the same keysym for an already down key for the |
| 676 | // sake of sanity |
| 677 | if (pressedKeys.find(lookup) != pressedKeys.end()) |
| 678 | keysym = pressedKeys[lookup]; |
| 679 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 680 | if (down) { |
Pierre Ossman | 16e1dcb | 2017-05-16 14:33:43 +0200 | [diff] [blame] | 681 | pressedKeys[lookup] = keysym; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 682 | } else { |
Pierre Ossman | 16e1dcb | 2017-05-16 14:33:43 +0200 | [diff] [blame] | 683 | if (!pressedKeys.erase(lookup)) |
Pierre Ossman | 5ae2821 | 2017-05-16 14:30:38 +0200 | [diff] [blame] | 684 | return; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 685 | } |
Pierre Ossman | 16e1dcb | 2017-05-16 14:33:43 +0200 | [diff] [blame] | 686 | |
Pierre Ossman | 5ae2821 | 2017-05-16 14:30:38 +0200 | [diff] [blame] | 687 | server->desktop->keyEvent(keysym, keycode, down); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 688 | } |
| 689 | |
| 690 | void VNCSConnectionST::clientCutText(const char* str, int len) |
| 691 | { |
| 692 | if (!(accessRights & AccessCutText)) return; |
| 693 | if (!rfb::Server::acceptCutText) return; |
| 694 | server->desktop->clientCutText(str, len); |
| 695 | } |
| 696 | |
| 697 | void VNCSConnectionST::framebufferUpdateRequest(const Rect& r,bool incremental) |
| 698 | { |
Pierre Ossman | e9962f7 | 2009-04-23 12:31:42 +0000 | [diff] [blame] | 699 | Rect safeRect; |
| 700 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 701 | if (!(accessRights & AccessView)) return; |
| 702 | |
| 703 | SConnection::framebufferUpdateRequest(r, incremental); |
| 704 | |
Pierre Ossman | d9a59ba | 2009-03-20 15:55:37 +0000 | [diff] [blame] | 705 | // Check that the client isn't sending crappy requests |
| 706 | if (!r.enclosed_by(Rect(0, 0, cp.width, cp.height))) { |
| 707 | vlog.error("FramebufferUpdateRequest %dx%d at %d,%d exceeds framebuffer %dx%d", |
| 708 | r.width(), r.height(), r.tl.x, r.tl.y, cp.width, cp.height); |
Pierre Ossman | e9962f7 | 2009-04-23 12:31:42 +0000 | [diff] [blame] | 709 | safeRect = r.intersect(Rect(0, 0, cp.width, cp.height)); |
| 710 | } else { |
| 711 | safeRect = r; |
Pierre Ossman | d9a59ba | 2009-03-20 15:55:37 +0000 | [diff] [blame] | 712 | } |
| 713 | |
Constantin Kaplinsky | 2ef6695 | 2008-08-29 11:33:46 +0000 | [diff] [blame] | 714 | // Just update the requested region. |
| 715 | // Framebuffer update will be sent a bit later, see processMessages(). |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 716 | Region reqRgn(r); |
Pierre Ossman | 1b478e5 | 2011-11-15 12:08:30 +0000 | [diff] [blame] | 717 | if (!incremental || !continuousUpdates) |
| 718 | requested.assign_union(reqRgn); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 719 | |
| 720 | if (!incremental) { |
| 721 | // Non-incremental update - treat as if area requested has changed |
| 722 | updates.add_changed(reqRgn); |
Pierre Ossman | 53125a7 | 2009-04-22 15:37:51 +0000 | [diff] [blame] | 723 | |
| 724 | // And send the screen layout to the client (which, unlike the |
| 725 | // framebuffer dimensions, the client doesn't get during init) |
Pierre Ossman | c5e2560 | 2009-03-20 12:59:05 +0000 | [diff] [blame] | 726 | writer()->writeExtendedDesktopSize(); |
Pierre Ossman | 53125a7 | 2009-04-22 15:37:51 +0000 | [diff] [blame] | 727 | |
| 728 | // We do not send a DesktopSize since it only contains the |
| 729 | // framebuffer size (which the client already should know) and |
| 730 | // because some clients don't handle extra DesktopSize events |
| 731 | // very well. |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 732 | } |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 733 | } |
| 734 | |
Pierre Ossman | 34bb061 | 2009-03-21 21:16:14 +0000 | [diff] [blame] | 735 | void VNCSConnectionST::setDesktopSize(int fb_width, int fb_height, |
| 736 | const ScreenSet& layout) |
Pierre Ossman | c5e2560 | 2009-03-20 12:59:05 +0000 | [diff] [blame] | 737 | { |
Pierre Ossman | 04e62db | 2009-03-23 16:57:07 +0000 | [diff] [blame] | 738 | unsigned int result; |
| 739 | |
Michal Srb | b318b8f | 2014-11-24 13:18:28 +0200 | [diff] [blame] | 740 | if (!(accessRights & AccessSetDesktopSize)) return; |
| 741 | if (!rfb::Server::acceptSetDesktopSize) return; |
| 742 | |
Pierre Ossman | 04e62db | 2009-03-23 16:57:07 +0000 | [diff] [blame] | 743 | // Don't bother the desktop with an invalid configuration |
| 744 | if (!layout.validate(fb_width, fb_height)) { |
| 745 | writer()->writeExtendedDesktopSize(reasonClient, resultInvalid, |
| 746 | fb_width, fb_height, layout); |
Pierre Ossman | a3ac01e | 2011-11-07 21:13:54 +0000 | [diff] [blame] | 747 | writeFramebufferUpdate(); |
Pierre Ossman | 04e62db | 2009-03-23 16:57:07 +0000 | [diff] [blame] | 748 | return; |
| 749 | } |
| 750 | |
| 751 | // FIXME: the desktop will call back to VNCServerST and an extra set |
| 752 | // of ExtendedDesktopSize messages will be sent. This is okay |
| 753 | // protocol-wise, but unnecessary. |
| 754 | result = server->desktop->setScreenLayout(fb_width, fb_height, layout); |
| 755 | |
Pierre Ossman | 04e62db | 2009-03-23 16:57:07 +0000 | [diff] [blame] | 756 | writer()->writeExtendedDesktopSize(reasonClient, result, |
| 757 | fb_width, fb_height, layout); |
Pierre Ossman | 04e62db | 2009-03-23 16:57:07 +0000 | [diff] [blame] | 758 | |
Pierre Ossman | a3ac01e | 2011-11-07 21:13:54 +0000 | [diff] [blame] | 759 | // Only notify other clients on success |
Pierre Ossman | 04e62db | 2009-03-23 16:57:07 +0000 | [diff] [blame] | 760 | if (result == resultSuccess) { |
| 761 | if (server->screenLayout != layout) |
| 762 | throw Exception("Desktop configured a different screen layout than requested"); |
| 763 | server->notifyScreenLayoutChange(this); |
| 764 | } |
Pierre Ossman | a3ac01e | 2011-11-07 21:13:54 +0000 | [diff] [blame] | 765 | |
| 766 | // but always send back a reply to the requesting client |
| 767 | // (do this last as it might throw an exception on socket errors) |
| 768 | writeFramebufferUpdate(); |
Pierre Ossman | c5e2560 | 2009-03-20 12:59:05 +0000 | [diff] [blame] | 769 | } |
| 770 | |
Pierre Ossman | 2c76494 | 2011-11-14 15:54:30 +0000 | [diff] [blame] | 771 | void VNCSConnectionST::fence(rdr::U32 flags, unsigned len, const char data[]) |
| 772 | { |
| 773 | if (flags & fenceFlagRequest) { |
| 774 | if (flags & fenceFlagSyncNext) { |
Pierre Ossman | b8b1e96 | 2012-07-20 10:47:00 +0000 | [diff] [blame] | 775 | pendingSyncFence = true; |
Pierre Ossman | 2c76494 | 2011-11-14 15:54:30 +0000 | [diff] [blame] | 776 | |
| 777 | fenceFlags = flags & (fenceFlagBlockBefore | fenceFlagBlockAfter | fenceFlagSyncNext); |
| 778 | fenceDataLen = len; |
| 779 | delete [] fenceData; |
Michal Srb | f3afa24 | 2017-03-27 19:02:15 +0300 | [diff] [blame] | 780 | fenceData = NULL; |
Pierre Ossman | 2c76494 | 2011-11-14 15:54:30 +0000 | [diff] [blame] | 781 | if (len > 0) { |
| 782 | fenceData = new char[len]; |
| 783 | memcpy(fenceData, data, len); |
| 784 | } |
| 785 | |
| 786 | return; |
| 787 | } |
| 788 | |
| 789 | // We handle everything synchronously so we trivially honor these modes |
| 790 | flags = flags & (fenceFlagBlockBefore | fenceFlagBlockAfter); |
| 791 | |
| 792 | writer()->writeFence(flags, len, data); |
| 793 | return; |
| 794 | } |
| 795 | |
Pierre Ossman | 1b478e5 | 2011-11-15 12:08:30 +0000 | [diff] [blame] | 796 | struct RTTInfo rttInfo; |
| 797 | |
Pierre Ossman | 2c76494 | 2011-11-14 15:54:30 +0000 | [diff] [blame] | 798 | switch (len) { |
| 799 | case 0: |
| 800 | // Initial dummy fence; |
| 801 | break; |
Pierre Ossman | 1b478e5 | 2011-11-15 12:08:30 +0000 | [diff] [blame] | 802 | case sizeof(struct RTTInfo): |
| 803 | memcpy(&rttInfo, data, sizeof(struct RTTInfo)); |
| 804 | handleRTTPong(rttInfo); |
| 805 | break; |
Pierre Ossman | 2c76494 | 2011-11-14 15:54:30 +0000 | [diff] [blame] | 806 | default: |
| 807 | vlog.error("Fence response of unexpected size received"); |
| 808 | } |
| 809 | } |
| 810 | |
Pierre Ossman | 1b478e5 | 2011-11-15 12:08:30 +0000 | [diff] [blame] | 811 | void VNCSConnectionST::enableContinuousUpdates(bool enable, |
| 812 | int x, int y, int w, int h) |
| 813 | { |
| 814 | Rect rect; |
| 815 | |
| 816 | if (!cp.supportsFence || !cp.supportsContinuousUpdates) |
| 817 | throw Exception("Client tried to enable continuous updates when not allowed"); |
| 818 | |
| 819 | continuousUpdates = enable; |
| 820 | |
| 821 | rect.setXYWH(x, y, w, h); |
| 822 | cuRegion.reset(rect); |
| 823 | |
| 824 | if (enable) { |
| 825 | requested.clear(); |
| 826 | writeFramebufferUpdate(); |
| 827 | } else { |
| 828 | writer()->writeEndOfContinuousUpdates(); |
| 829 | } |
| 830 | } |
| 831 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 832 | // supportsLocalCursor() is called whenever the status of |
| 833 | // cp.supportsLocalCursor has changed. If the client does now support local |
| 834 | // cursor, we make sure that the old server-side rendered cursor is cleaned up |
| 835 | // and the cursor is sent to the client. |
| 836 | |
| 837 | void VNCSConnectionST::supportsLocalCursor() |
| 838 | { |
Pierre Ossman | 324043e | 2017-08-16 16:26:11 +0200 | [diff] [blame] | 839 | if (cp.supportsLocalCursorWithAlpha || |
| 840 | cp.supportsLocalCursor || cp.supportsLocalXCursor) { |
Pierre Ossman | 671d2ef | 2015-06-09 16:09:06 +0200 | [diff] [blame] | 841 | if (!damagedCursorRegion.is_empty()) |
Pierre Ossman | 5c9e1e5 | 2011-11-08 10:32:05 +0000 | [diff] [blame] | 842 | removeRenderedCursor = true; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 843 | setCursor(); |
| 844 | } |
| 845 | } |
| 846 | |
Pierre Ossman | 2c76494 | 2011-11-14 15:54:30 +0000 | [diff] [blame] | 847 | void VNCSConnectionST::supportsFence() |
| 848 | { |
| 849 | writer()->writeFence(fenceFlagRequest, 0, NULL); |
| 850 | } |
| 851 | |
Pierre Ossman | 1b478e5 | 2011-11-15 12:08:30 +0000 | [diff] [blame] | 852 | void VNCSConnectionST::supportsContinuousUpdates() |
| 853 | { |
| 854 | // We refuse to use continuous updates if we cannot monitor the buffer |
| 855 | // usage using fences. |
| 856 | if (!cp.supportsFence) |
| 857 | return; |
| 858 | |
| 859 | writer()->writeEndOfContinuousUpdates(); |
| 860 | } |
| 861 | |
Pierre Ossman | b45a84f | 2016-12-12 16:59:15 +0100 | [diff] [blame] | 862 | void VNCSConnectionST::supportsLEDState() |
| 863 | { |
| 864 | writer()->writeLEDState(); |
| 865 | } |
| 866 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 867 | |
Pierre Ossman | 1bb8b6c | 2011-10-25 15:20:05 +0000 | [diff] [blame] | 868 | bool VNCSConnectionST::handleTimeout(Timer* t) |
| 869 | { |
Pierre Ossman | a3ac01e | 2011-11-07 21:13:54 +0000 | [diff] [blame] | 870 | try { |
Pierre Ossman | a40ab20 | 2016-04-29 15:35:56 +0200 | [diff] [blame] | 871 | if (t == &congestionTimer) |
Pierre Ossman | 1b478e5 | 2011-11-15 12:08:30 +0000 | [diff] [blame] | 872 | updateCongestion(); |
Pierre Ossman | f8e3b34 | 2015-01-26 14:37:04 +0100 | [diff] [blame] | 873 | else if (t == &queryConnectTimer) { |
| 874 | if (state() == RFBSTATE_QUERYING) |
| 875 | approveConnection(false, "The attempt to prompt the user to accept the connection failed"); |
| 876 | } |
Pierre Ossman | a3ac01e | 2011-11-07 21:13:54 +0000 | [diff] [blame] | 877 | } catch (rdr::Exception& e) { |
| 878 | close(e.str()); |
| 879 | } |
Pierre Ossman | 1bb8b6c | 2011-10-25 15:20:05 +0000 | [diff] [blame] | 880 | |
| 881 | return false; |
| 882 | } |
| 883 | |
Pierre Ossman | 851e680 | 2017-09-12 16:44:44 +0200 | [diff] [blame^] | 884 | bool VNCSConnectionST::isShiftPressed() |
| 885 | { |
| 886 | std::map<rdr::U32, rdr::U32>::const_iterator iter; |
| 887 | |
| 888 | for (iter = pressedKeys.begin(); iter != pressedKeys.end(); ++iter) { |
| 889 | if (iter->second == XK_Shift_L) |
| 890 | return true; |
| 891 | if (iter->second == XK_Shift_R) |
| 892 | return true; |
| 893 | } |
| 894 | |
| 895 | return false; |
| 896 | } |
Pierre Ossman | 1bb8b6c | 2011-10-25 15:20:05 +0000 | [diff] [blame] | 897 | |
Pierre Ossman | 1b478e5 | 2011-11-15 12:08:30 +0000 | [diff] [blame] | 898 | void VNCSConnectionST::writeRTTPing() |
| 899 | { |
| 900 | struct RTTInfo rttInfo; |
| 901 | |
| 902 | if (!cp.supportsFence) |
| 903 | return; |
| 904 | |
| 905 | memset(&rttInfo, 0, sizeof(struct RTTInfo)); |
| 906 | |
| 907 | gettimeofday(&rttInfo.tv, NULL); |
| 908 | rttInfo.offset = sock->outStream().length(); |
| 909 | rttInfo.inFlight = rttInfo.offset - ackedOffset; |
| 910 | |
| 911 | // We need to make sure any old update are already processed by the |
| 912 | // time we get the response back. This allows us to reliably throttle |
| 913 | // back on client overload, as well as network overload. |
| 914 | writer()->writeFence(fenceFlagRequest | fenceFlagBlockBefore, |
| 915 | sizeof(struct RTTInfo), (const char*)&rttInfo); |
| 916 | |
| 917 | pingCounter++; |
| 918 | |
| 919 | sentOffset = rttInfo.offset; |
| 920 | |
| 921 | // Let some data flow before we adjust the settings |
| 922 | if (!congestionTimer.isStarted()) |
| 923 | congestionTimer.start(__rfbmin(baseRTT * 2, 100)); |
| 924 | } |
| 925 | |
| 926 | void VNCSConnectionST::handleRTTPong(const struct RTTInfo &rttInfo) |
| 927 | { |
| 928 | unsigned rtt, delay; |
Pierre Ossman | 1b478e5 | 2011-11-15 12:08:30 +0000 | [diff] [blame] | 929 | |
| 930 | pingCounter--; |
| 931 | |
| 932 | rtt = msSince(&rttInfo.tv); |
| 933 | if (rtt < 1) |
| 934 | rtt = 1; |
| 935 | |
| 936 | ackedOffset = rttInfo.offset; |
| 937 | |
| 938 | // Try to estimate wire latency by tracking lowest seen latency |
| 939 | if (rtt < baseRTT) |
| 940 | baseRTT = rtt; |
| 941 | |
| 942 | if (rttInfo.inFlight > congWindow) { |
| 943 | seenCongestion = true; |
| 944 | |
| 945 | // Estimate added delay because of overtaxed buffers |
| 946 | delay = (rttInfo.inFlight - congWindow) * baseRTT / congWindow; |
| 947 | |
| 948 | if (delay < rtt) |
| 949 | rtt -= delay; |
| 950 | else |
| 951 | rtt = 1; |
| 952 | |
| 953 | // If we underestimate the congestion window, then we'll get a latency |
| 954 | // that's less than the wire latency, which will confuse other portions |
| 955 | // of the code. |
| 956 | if (rtt < baseRTT) |
| 957 | rtt = baseRTT; |
| 958 | } |
| 959 | |
| 960 | // We only keep track of the minimum latency seen (for a given interval) |
klemens | 0536d09 | 2017-01-28 20:56:56 +0100 | [diff] [blame] | 961 | // on the basis that we want to avoid continuous buffer issue, but don't |
Pierre Ossman | 1b478e5 | 2011-11-15 12:08:30 +0000 | [diff] [blame] | 962 | // mind (or even approve of) bursts. |
| 963 | if (rtt < minRTT) |
| 964 | minRTT = rtt; |
| 965 | } |
| 966 | |
Pierre Ossman | 1bb8b6c | 2011-10-25 15:20:05 +0000 | [diff] [blame] | 967 | bool VNCSConnectionST::isCongested() |
| 968 | { |
Pierre Ossman | 1b478e5 | 2011-11-15 12:08:30 +0000 | [diff] [blame] | 969 | int offset; |
| 970 | |
| 971 | // Stuff still waiting in the send buffer? |
Pierre Ossman | 352d062 | 2016-04-29 15:50:54 +0200 | [diff] [blame] | 972 | sock->outStream().flush(); |
Pierre Ossman | 1bb8b6c | 2011-10-25 15:20:05 +0000 | [diff] [blame] | 973 | if (sock->outStream().bufferUsage() > 0) |
| 974 | return true; |
| 975 | |
Pierre Ossman | 1b478e5 | 2011-11-15 12:08:30 +0000 | [diff] [blame] | 976 | if (!cp.supportsFence) |
| 977 | return false; |
| 978 | |
| 979 | // Idle for too long? (and no data on the wire) |
| 980 | // |
| 981 | // FIXME: This should really just be one baseRTT, but we're getting |
| 982 | // problems with triggering the idle timeout on each update. |
| 983 | // Maybe we need to use a moving average for the wire latency |
| 984 | // instead of baseRTT. |
| 985 | if ((sentOffset == ackedOffset) && |
| 986 | (sock->outStream().getIdleTime() > 2 * baseRTT)) { |
| 987 | |
| 988 | #ifdef CONGESTION_DEBUG |
| 989 | if (congWindow > INITIAL_WINDOW) |
| 990 | fprintf(stderr, "Reverting to initial window (%d KiB) after %d ms\n", |
| 991 | INITIAL_WINDOW / 1024, sock->outStream().getIdleTime()); |
| 992 | #endif |
| 993 | |
| 994 | // Close congestion window and allow a transfer |
| 995 | // FIXME: Reset baseRTT like Linux Vegas? |
| 996 | congWindow = __rfbmin(INITIAL_WINDOW, congWindow); |
| 997 | |
| 998 | return false; |
| 999 | } |
| 1000 | |
| 1001 | offset = sock->outStream().length(); |
| 1002 | |
| 1003 | // FIXME: Should we compensate for non-update data? |
| 1004 | // (i.e. use sentOffset instead of offset) |
| 1005 | if ((offset - ackedOffset) < congWindow) |
| 1006 | return false; |
| 1007 | |
| 1008 | // If we just have one outstanding "ping", that means the client has |
| 1009 | // started receiving our update. In order to not regress compared to |
| 1010 | // before we had congestion avoidance, we allow another update here. |
| 1011 | // This could further clog up the tubes, but congestion control isn't |
| 1012 | // really working properly right now anyway as the wire would otherwise |
| 1013 | // be idle for at least RTT/2. |
| 1014 | if (pingCounter == 1) |
| 1015 | return false; |
| 1016 | |
| 1017 | return true; |
| 1018 | } |
| 1019 | |
| 1020 | |
| 1021 | void VNCSConnectionST::updateCongestion() |
| 1022 | { |
| 1023 | unsigned diff; |
| 1024 | |
| 1025 | if (!seenCongestion) |
| 1026 | return; |
| 1027 | |
| 1028 | diff = minRTT - baseRTT; |
| 1029 | |
| 1030 | if (diff > __rfbmin(100, baseRTT)) { |
| 1031 | // Way too fast |
| 1032 | congWindow = congWindow * baseRTT / minRTT; |
| 1033 | } else if (diff > __rfbmin(50, baseRTT/2)) { |
| 1034 | // Slightly too fast |
| 1035 | congWindow -= 4096; |
| 1036 | } else if (diff < 5) { |
| 1037 | // Way too slow |
| 1038 | congWindow += 8192; |
| 1039 | } else if (diff < 25) { |
| 1040 | // Too slow |
| 1041 | congWindow += 4096; |
| 1042 | } |
| 1043 | |
| 1044 | if (congWindow < MINIMUM_WINDOW) |
| 1045 | congWindow = MINIMUM_WINDOW; |
| 1046 | if (congWindow > MAXIMUM_WINDOW) |
| 1047 | congWindow = MAXIMUM_WINDOW; |
| 1048 | |
| 1049 | #ifdef CONGESTION_DEBUG |
| 1050 | fprintf(stderr, "RTT: %d ms (%d ms), Window: %d KiB, Bandwidth: %g Mbps\n", |
| 1051 | minRTT, baseRTT, congWindow / 1024, |
| 1052 | congWindow * 8.0 / baseRTT / 1000.0); |
| 1053 | |
| 1054 | #ifdef TCP_INFO |
| 1055 | struct tcp_info tcp_info; |
| 1056 | socklen_t tcp_info_length; |
| 1057 | |
| 1058 | tcp_info_length = sizeof(tcp_info); |
| 1059 | if (getsockopt(sock->getFd(), SOL_TCP, TCP_INFO, |
| 1060 | (void *)&tcp_info, &tcp_info_length) == 0) { |
| 1061 | fprintf(stderr, "Socket: RTT: %d ms (+/- %d ms) Window %d KiB\n", |
| 1062 | tcp_info.tcpi_rtt / 1000, tcp_info.tcpi_rttvar / 1000, |
| 1063 | tcp_info.tcpi_snd_mss * tcp_info.tcpi_snd_cwnd / 1024); |
| 1064 | } |
| 1065 | #endif |
| 1066 | |
| 1067 | #endif |
| 1068 | |
| 1069 | minRTT = -1; |
| 1070 | seenCongestion = false; |
Pierre Ossman | 1bb8b6c | 2011-10-25 15:20:05 +0000 | [diff] [blame] | 1071 | } |
| 1072 | |
| 1073 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 1074 | void VNCSConnectionST::writeFramebufferUpdate() |
| 1075 | { |
Pierre Ossman | 2c76494 | 2011-11-14 15:54:30 +0000 | [diff] [blame] | 1076 | // We're in the middle of processing a command that's supposed to be |
| 1077 | // synchronised. Allowing an update to slip out right now might violate |
| 1078 | // that synchronisation. |
| 1079 | if (syncFence) |
| 1080 | return; |
| 1081 | |
Pierre Ossman | a3ac01e | 2011-11-07 21:13:54 +0000 | [diff] [blame] | 1082 | // We try to aggregate responses, so don't send out anything whilst we |
| 1083 | // still have incoming messages. processMessages() will give us another |
| 1084 | // chance to run once things are idle. |
| 1085 | if (inProcessMessages) |
| 1086 | return; |
| 1087 | |
Pierre Ossman | 1b478e5 | 2011-11-15 12:08:30 +0000 | [diff] [blame] | 1088 | if (state() != RFBSTATE_NORMAL) |
| 1089 | return; |
| 1090 | if (requested.is_empty() && !continuousUpdates) |
Pierre Ossman | e9962f7 | 2009-04-23 12:31:42 +0000 | [diff] [blame] | 1091 | return; |
Pierre Ossman | d9a59ba | 2009-03-20 15:55:37 +0000 | [diff] [blame] | 1092 | |
Pierre Ossman | 1bb8b6c | 2011-10-25 15:20:05 +0000 | [diff] [blame] | 1093 | // Check that we actually have some space on the link and retry in a |
| 1094 | // bit if things are congested. |
Pierre Ossman | a40ab20 | 2016-04-29 15:35:56 +0200 | [diff] [blame] | 1095 | if (isCongested()) |
Pierre Ossman | 1bb8b6c | 2011-10-25 15:20:05 +0000 | [diff] [blame] | 1096 | return; |
Pierre Ossman | 1bb8b6c | 2011-10-25 15:20:05 +0000 | [diff] [blame] | 1097 | |
Pierre Ossman | cef3cf7 | 2016-11-25 10:06:34 +0100 | [diff] [blame] | 1098 | // Updates often consists of many small writes, and in continuous |
| 1099 | // mode, we will also have small fence messages around the update. We |
| 1100 | // need to aggregate these in order to not clog up TCP's congestion |
| 1101 | // window. |
Pierre Ossman | 36dadf8 | 2011-11-15 12:11:32 +0000 | [diff] [blame] | 1102 | network::TcpSocket::cork(sock->getFd(), true); |
| 1103 | |
Pierre Ossman | e9962f7 | 2009-04-23 12:31:42 +0000 | [diff] [blame] | 1104 | // First take care of any updates that cannot contain framebuffer data |
| 1105 | // changes. |
Pierre Ossman | cef3cf7 | 2016-11-25 10:06:34 +0100 | [diff] [blame] | 1106 | writeNoDataUpdate(); |
| 1107 | |
| 1108 | // Then real data (if possible) |
| 1109 | writeDataUpdate(); |
| 1110 | |
| 1111 | network::TcpSocket::cork(sock->getFd(), false); |
| 1112 | } |
| 1113 | |
| 1114 | void VNCSConnectionST::writeNoDataUpdate() |
| 1115 | { |
| 1116 | if (!writer()->needNoDataUpdate()) |
| 1117 | return; |
| 1118 | |
| 1119 | writer()->writeNoDataUpdate(); |
| 1120 | |
| 1121 | // Make sure no data update is sent until next request |
| 1122 | requested.clear(); |
| 1123 | } |
| 1124 | |
| 1125 | void VNCSConnectionST::writeDataUpdate() |
| 1126 | { |
| 1127 | Region req; |
| 1128 | UpdateInfo ui; |
| 1129 | bool needNewUpdateInfo; |
Pierre Ossman | 24684e5 | 2016-12-05 16:58:19 +0100 | [diff] [blame] | 1130 | const RenderedCursor *cursor; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 1131 | |
Constantin Kaplinsky | 604d781 | 2007-08-31 15:50:37 +0000 | [diff] [blame] | 1132 | updates.enable_copyrect(cp.useCopyRect); |
| 1133 | |
Pierre Ossman | 6e49e95 | 2016-10-07 15:59:38 +0200 | [diff] [blame] | 1134 | // See if we are allowed to send anything right now (the framebuffer |
| 1135 | // might have changed in ways we haven't yet been informed of). |
Pierre Ossman | bbf955e | 2011-11-08 12:44:10 +0000 | [diff] [blame] | 1136 | if (!server->checkUpdate()) |
Pierre Ossman | cef3cf7 | 2016-11-25 10:06:34 +0100 | [diff] [blame] | 1137 | return; |
Constantin Kaplinsky | a09dc14 | 2008-12-18 12:08:15 +0000 | [diff] [blame] | 1138 | |
Pierre Ossman | cef3cf7 | 2016-11-25 10:06:34 +0100 | [diff] [blame] | 1139 | // See what the client has requested (if anything) |
Pierre Ossman | 1b478e5 | 2011-11-15 12:08:30 +0000 | [diff] [blame] | 1140 | if (continuousUpdates) |
| 1141 | req = cuRegion.union_(requested); |
| 1142 | else |
| 1143 | req = requested; |
| 1144 | |
Pierre Ossman | cef3cf7 | 2016-11-25 10:06:34 +0100 | [diff] [blame] | 1145 | if (req.is_empty()) |
| 1146 | return; |
| 1147 | |
| 1148 | // Get the lists of updates. Prior to exporting the data to the `ui' object, |
| 1149 | // getUpdateInfo() will normalize the `updates' object such way that its |
| 1150 | // `changed' and `copied' regions would not intersect. |
Pierre Ossman | 1b478e5 | 2011-11-15 12:08:30 +0000 | [diff] [blame] | 1151 | updates.getUpdateInfo(&ui, req); |
| 1152 | needNewUpdateInfo = false; |
Constantin Kaplinsky | 604d781 | 2007-08-31 15:50:37 +0000 | [diff] [blame] | 1153 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 1154 | // If the previous position of the rendered cursor overlaps the source of the |
| 1155 | // copy, then when the copy happens the corresponding rectangle in the |
| 1156 | // destination will be wrong, so add it to the changed region. |
| 1157 | |
Pierre Ossman | 671d2ef | 2015-06-09 16:09:06 +0200 | [diff] [blame] | 1158 | if (!ui.copied.is_empty() && !damagedCursorRegion.is_empty()) { |
| 1159 | Region bogusCopiedCursor; |
| 1160 | |
| 1161 | bogusCopiedCursor.copyFrom(damagedCursorRegion); |
| 1162 | bogusCopiedCursor.translate(ui.copy_delta); |
| 1163 | bogusCopiedCursor.assign_intersect(server->pb->getRect()); |
Constantin Kaplinsky | 45517c8 | 2007-08-31 15:56:33 +0000 | [diff] [blame] | 1164 | if (!ui.copied.intersect(bogusCopiedCursor).is_empty()) { |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 1165 | updates.add_changed(bogusCopiedCursor); |
Constantin Kaplinsky | 604d781 | 2007-08-31 15:50:37 +0000 | [diff] [blame] | 1166 | needNewUpdateInfo = true; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 1167 | } |
| 1168 | } |
| 1169 | |
Pierre Ossman | 671d2ef | 2015-06-09 16:09:06 +0200 | [diff] [blame] | 1170 | // If we need to remove the old rendered cursor, just add the region to |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 1171 | // the changed region. |
| 1172 | |
| 1173 | if (removeRenderedCursor) { |
Pierre Ossman | 671d2ef | 2015-06-09 16:09:06 +0200 | [diff] [blame] | 1174 | updates.add_changed(damagedCursorRegion); |
Constantin Kaplinsky | 604d781 | 2007-08-31 15:50:37 +0000 | [diff] [blame] | 1175 | needNewUpdateInfo = true; |
Pierre Ossman | 671d2ef | 2015-06-09 16:09:06 +0200 | [diff] [blame] | 1176 | damagedCursorRegion.clear(); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 1177 | removeRenderedCursor = false; |
| 1178 | } |
| 1179 | |
| 1180 | // Return if there is nothing to send the client. |
| 1181 | |
Pierre Ossman | 671d2ef | 2015-06-09 16:09:06 +0200 | [diff] [blame] | 1182 | if (updates.is_empty() && !writer()->needFakeUpdate() && !updateRenderedCursor) |
Pierre Ossman | cef3cf7 | 2016-11-25 10:06:34 +0100 | [diff] [blame] | 1183 | return; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 1184 | |
Constantin Kaplinsky | 604d781 | 2007-08-31 15:50:37 +0000 | [diff] [blame] | 1185 | // The `updates' object could change, make sure we have valid update info. |
| 1186 | |
| 1187 | if (needNewUpdateInfo) |
Pierre Ossman | 1b478e5 | 2011-11-15 12:08:30 +0000 | [diff] [blame] | 1188 | updates.getUpdateInfo(&ui, req); |
Constantin Kaplinsky | 604d781 | 2007-08-31 15:50:37 +0000 | [diff] [blame] | 1189 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 1190 | // If the client needs a server-side rendered cursor, work out the cursor |
| 1191 | // rectangle. If it's empty then don't bother drawing it, but if it overlaps |
| 1192 | // with the update region, we need to draw the rendered cursor regardless of |
| 1193 | // whether it has changed. |
| 1194 | |
Pierre Ossman | 24684e5 | 2016-12-05 16:58:19 +0100 | [diff] [blame] | 1195 | cursor = NULL; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 1196 | if (needRenderedCursor()) { |
Pierre Ossman | 671d2ef | 2015-06-09 16:09:06 +0200 | [diff] [blame] | 1197 | Rect renderedCursorRect; |
| 1198 | |
Pierre Ossman | 24684e5 | 2016-12-05 16:58:19 +0100 | [diff] [blame] | 1199 | cursor = server->getRenderedCursor(); |
| 1200 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 1201 | renderedCursorRect |
Pierre Ossman | 24684e5 | 2016-12-05 16:58:19 +0100 | [diff] [blame] | 1202 | = cursor->getEffectiveRect().intersect(req.get_bounding_rect()); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 1203 | |
| 1204 | if (renderedCursorRect.is_empty()) { |
Pierre Ossman | 24684e5 | 2016-12-05 16:58:19 +0100 | [diff] [blame] | 1205 | cursor = NULL; |
| 1206 | } else if (!updateRenderedCursor && |
| 1207 | ui.changed.union_(ui.copied) |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 1208 | .intersect(renderedCursorRect).is_empty()) { |
Pierre Ossman | 24684e5 | 2016-12-05 16:58:19 +0100 | [diff] [blame] | 1209 | cursor = NULL; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 1210 | } |
| 1211 | |
Pierre Ossman | 5c03720 | 2016-12-05 17:00:35 +0100 | [diff] [blame] | 1212 | if (cursor) { |
| 1213 | updates.subtract(renderedCursorRect); |
| 1214 | updates.getUpdateInfo(&ui, req); |
| 1215 | } |
Pierre Ossman | 671d2ef | 2015-06-09 16:09:06 +0200 | [diff] [blame] | 1216 | |
| 1217 | damagedCursorRegion.assign_union(renderedCursorRect); |
| 1218 | updateRenderedCursor = false; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 1219 | } |
| 1220 | |
Pierre Ossman | 24684e5 | 2016-12-05 16:58:19 +0100 | [diff] [blame] | 1221 | if (ui.is_empty() && !writer()->needFakeUpdate() && !cursor) |
Pierre Ossman | cef3cf7 | 2016-11-25 10:06:34 +0100 | [diff] [blame] | 1222 | return; |
Pierre Ossman | fdba3fe | 2014-01-31 13:12:18 +0100 | [diff] [blame] | 1223 | |
Pierre Ossman | cef3cf7 | 2016-11-25 10:06:34 +0100 | [diff] [blame] | 1224 | writeRTTPing(); |
Pierre Ossman | 1b478e5 | 2011-11-15 12:08:30 +0000 | [diff] [blame] | 1225 | |
Pierre Ossman | cef3cf7 | 2016-11-25 10:06:34 +0100 | [diff] [blame] | 1226 | encodeManager.writeUpdate(ui, server->getPixelBuffer(), cursor); |
Pierre Ossman | 1b478e5 | 2011-11-15 12:08:30 +0000 | [diff] [blame] | 1227 | |
Pierre Ossman | cef3cf7 | 2016-11-25 10:06:34 +0100 | [diff] [blame] | 1228 | writeRTTPing(); |
Pierre Ossman | c039726 | 2014-03-14 15:59:46 +0100 | [diff] [blame] | 1229 | |
Pierre Ossman | cef3cf7 | 2016-11-25 10:06:34 +0100 | [diff] [blame] | 1230 | // The request might be for just part of the screen, so we cannot |
| 1231 | // just clear the entire update tracker. |
| 1232 | updates.subtract(req); |
Pierre Ossman | c039726 | 2014-03-14 15:59:46 +0100 | [diff] [blame] | 1233 | |
Pierre Ossman | cef3cf7 | 2016-11-25 10:06:34 +0100 | [diff] [blame] | 1234 | requested.clear(); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 1235 | } |
| 1236 | |
| 1237 | |
Pierre Ossman | a3ac01e | 2011-11-07 21:13:54 +0000 | [diff] [blame] | 1238 | void VNCSConnectionST::screenLayoutChange(rdr::U16 reason) |
| 1239 | { |
| 1240 | if (!authenticated()) |
| 1241 | return; |
| 1242 | |
| 1243 | cp.screenLayout = server->screenLayout; |
| 1244 | |
| 1245 | if (state() != RFBSTATE_NORMAL) |
| 1246 | return; |
| 1247 | |
| 1248 | writer()->writeExtendedDesktopSize(reason, 0, cp.width, cp.height, |
| 1249 | cp.screenLayout); |
| 1250 | writeFramebufferUpdate(); |
| 1251 | } |
| 1252 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 1253 | |
| 1254 | // setCursor() is called whenever the cursor has changed shape or pixel format. |
| 1255 | // If the client supports local cursor then it will arrange for the cursor to |
| 1256 | // be sent to the client. |
| 1257 | |
| 1258 | void VNCSConnectionST::setCursor() |
| 1259 | { |
Pierre Ossman | a3ac01e | 2011-11-07 21:13:54 +0000 | [diff] [blame] | 1260 | if (state() != RFBSTATE_NORMAL) |
| 1261 | return; |
Pierre Ossman | a3ac01e | 2011-11-07 21:13:54 +0000 | [diff] [blame] | 1262 | |
Pierre Ossman | 71ca8d5 | 2017-09-15 11:03:12 +0200 | [diff] [blame] | 1263 | // We need to blank out the client's cursor or there will be two |
| 1264 | if (needRenderedCursor()) |
| 1265 | cp.setCursor(emptyCursor); |
| 1266 | else |
| 1267 | cp.setCursor(*server->cursor); |
Pierre Ossman | 126e564 | 2014-02-13 14:40:25 +0100 | [diff] [blame] | 1268 | |
Pierre Ossman | 8053c8e | 2017-02-21 12:59:04 +0100 | [diff] [blame] | 1269 | if (!writer()->writeSetCursorWithAlpha()) { |
| 1270 | if (!writer()->writeSetCursor()) { |
| 1271 | if (!writer()->writeSetXCursor()) { |
| 1272 | // No client support |
| 1273 | return; |
| 1274 | } |
Pierre Ossman | 126e564 | 2014-02-13 14:40:25 +0100 | [diff] [blame] | 1275 | } |
| 1276 | } |
| 1277 | |
Pierre Ossman | a3ac01e | 2011-11-07 21:13:54 +0000 | [diff] [blame] | 1278 | writeFramebufferUpdate(); |
| 1279 | } |
| 1280 | |
| 1281 | void VNCSConnectionST::setDesktopName(const char *name) |
| 1282 | { |
| 1283 | cp.setName(name); |
| 1284 | |
| 1285 | if (state() != RFBSTATE_NORMAL) |
| 1286 | return; |
| 1287 | |
| 1288 | if (!writer()->writeSetDesktopName()) { |
| 1289 | fprintf(stderr, "Client does not support desktop rename\n"); |
| 1290 | return; |
| 1291 | } |
| 1292 | |
| 1293 | writeFramebufferUpdate(); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 1294 | } |
| 1295 | |
Pierre Ossman | b45a84f | 2016-12-12 16:59:15 +0100 | [diff] [blame] | 1296 | void VNCSConnectionST::setLEDState(unsigned int ledstate) |
| 1297 | { |
| 1298 | if (state() != RFBSTATE_NORMAL) |
| 1299 | return; |
| 1300 | |
| 1301 | cp.setLEDState(ledstate); |
| 1302 | |
| 1303 | if (!writer()->writeLEDState()) { |
| 1304 | // No client support |
| 1305 | return; |
| 1306 | } |
| 1307 | |
| 1308 | writeFramebufferUpdate(); |
| 1309 | } |
| 1310 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 1311 | void VNCSConnectionST::setSocketTimeouts() |
| 1312 | { |
| 1313 | int timeoutms = rfb::Server::clientWaitTimeMillis; |
| 1314 | soonestTimeout(&timeoutms, secsToMillis(rfb::Server::idleTimeout)); |
| 1315 | if (timeoutms == 0) |
| 1316 | timeoutms = -1; |
| 1317 | sock->inStream().setTimeout(timeoutms); |
| 1318 | sock->outStream().setTimeout(timeoutms); |
| 1319 | } |
| 1320 | |
| 1321 | char* VNCSConnectionST::getStartTime() |
| 1322 | { |
| 1323 | char* result = ctime(&startTime); |
| 1324 | result[24] = '\0'; |
| 1325 | return result; |
| 1326 | } |
| 1327 | |
| 1328 | void VNCSConnectionST::setStatus(int status) |
| 1329 | { |
| 1330 | switch (status) { |
| 1331 | case 0: |
| 1332 | accessRights = accessRights | AccessPtrEvents | AccessKeyEvents | AccessView; |
| 1333 | break; |
| 1334 | case 1: |
Pierre Ossman | 7728be2 | 2015-03-03 16:39:37 +0100 | [diff] [blame] | 1335 | accessRights = (accessRights & ~(AccessPtrEvents | AccessKeyEvents)) | AccessView; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 1336 | break; |
| 1337 | case 2: |
Adam Tkac | 8e98506 | 2011-02-07 11:33:57 +0000 | [diff] [blame] | 1338 | accessRights = accessRights & ~(AccessPtrEvents | AccessKeyEvents | AccessView); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 1339 | break; |
| 1340 | } |
| 1341 | framebufferUpdateRequest(server->pb->getRect(), false); |
| 1342 | } |
| 1343 | int VNCSConnectionST::getStatus() |
| 1344 | { |
| 1345 | if ((accessRights & (AccessPtrEvents | AccessKeyEvents | AccessView)) == 0x0007) |
| 1346 | return 0; |
| 1347 | if ((accessRights & (AccessPtrEvents | AccessKeyEvents | AccessView)) == 0x0001) |
| 1348 | return 1; |
| 1349 | if ((accessRights & (AccessPtrEvents | AccessKeyEvents | AccessView)) == 0x0000) |
| 1350 | return 2; |
| 1351 | return 4; |
| 1352 | } |
| 1353 | |