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