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