Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 1 | /* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved. |
Pierre Ossman | b53c3bf | 2018-03-22 16:01:44 +0100 | [diff] [blame] | 2 | * Copyright 2009-2018 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 | |
| 20 | // -=- Single-Threaded VNC Server implementation |
| 21 | |
| 22 | |
| 23 | // Note about how sockets get closed: |
| 24 | // |
| 25 | // Closing sockets to clients is non-trivial because the code which calls |
| 26 | // VNCServerST must explicitly know about all the sockets (so that it can block |
| 27 | // on them appropriately). However, VNCServerST may want to close clients for |
| 28 | // a number of reasons, and from a variety of entry points. The simplest is |
| 29 | // when processSocketEvent() is called for a client, and the remote end has |
| 30 | // closed its socket. A more complex reason is when processSocketEvent() is |
| 31 | // called for a client which has just sent a ClientInit with the shared flag |
| 32 | // set to false - in this case we want to close all other clients. Yet another |
| 33 | // reason for disconnecting clients is when the desktop size has changed as a |
| 34 | // result of a call to setPixelBuffer(). |
| 35 | // |
| 36 | // The responsibility for creating and deleting sockets is entirely with the |
| 37 | // calling code. When VNCServerST wants to close a connection to a client it |
| 38 | // calls the VNCSConnectionST's close() method which calls shutdown() on the |
| 39 | // socket. Eventually the calling code will notice that the socket has been |
| 40 | // shut down and call removeSocket() so that we can delete the |
| 41 | // VNCSConnectionST. Note that the socket must not be deleted by the calling |
| 42 | // code until after removeSocket() has been called. |
| 43 | // |
| 44 | // One minor complication is that we don't allocate a VNCSConnectionST object |
| 45 | // for a blacklisted host (since we want to minimise the resources used for |
| 46 | // dealing with such a connection). In order to properly implement the |
| 47 | // getSockets function, we must maintain a separate closingSockets list, |
| 48 | // otherwise blacklisted connections might be "forgotten". |
| 49 | |
| 50 | |
Pierre Ossman | 559a2e8 | 2012-01-23 15:54:11 +0000 | [diff] [blame] | 51 | #include <assert.h> |
Pierre Ossman | f99c571 | 2009-03-13 14:41:27 +0000 | [diff] [blame] | 52 | #include <stdlib.h> |
| 53 | |
Pierre Ossman | 707fa12 | 2015-12-11 20:21:20 +0100 | [diff] [blame] | 54 | #include <rfb/ComparingUpdateTracker.h> |
| 55 | #include <rfb/KeyRemapper.h> |
| 56 | #include <rfb/ListConnInfo.h> |
Pierre Ossman | 6c97fa4 | 2018-10-05 17:35:51 +0200 | [diff] [blame] | 57 | #include <rfb/LogWriter.h> |
Pierre Ossman | 707fa12 | 2015-12-11 20:21:20 +0100 | [diff] [blame] | 58 | #include <rfb/Security.h> |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 59 | #include <rfb/ServerCore.h> |
| 60 | #include <rfb/VNCServerST.h> |
| 61 | #include <rfb/VNCSConnectionST.h> |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 62 | #include <rfb/util.h> |
Pierre Ossman | bb305ca | 2016-12-11 12:41:26 +0100 | [diff] [blame] | 63 | #include <rfb/ledStates.h> |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 64 | |
| 65 | #include <rdr/types.h> |
| 66 | |
| 67 | using namespace rfb; |
| 68 | |
| 69 | static LogWriter slog("VNCServerST"); |
Pierre Ossman | 6c97fa4 | 2018-10-05 17:35:51 +0200 | [diff] [blame] | 70 | static LogWriter connectionsLog("Connections"); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 71 | |
| 72 | // |
| 73 | // -=- VNCServerST Implementation |
| 74 | // |
| 75 | |
| 76 | // -=- Constructors/Destructor |
| 77 | |
Adam Tkac | a6578bf | 2010-04-23 14:07:41 +0000 | [diff] [blame] | 78 | VNCServerST::VNCServerST(const char* name_, SDesktop* desktop_) |
Pierre Ossman | 559a2e8 | 2012-01-23 15:54:11 +0000 | [diff] [blame] | 79 | : blHosts(&blacklist), desktop(desktop_), desktopStarted(false), |
Pierre Ossman | bb305ca | 2016-12-11 12:41:26 +0100 | [diff] [blame] | 80 | blockCounter(0), pb(0), ledState(ledUnknown), |
Adam Tkac | d36b626 | 2009-09-04 10:57:20 +0000 | [diff] [blame] | 81 | name(strDup(name_)), pointerClient(0), comparer(0), |
Pierre Ossman | 6a1a0d0 | 2017-02-19 15:48:17 +0100 | [diff] [blame] | 82 | cursor(new Cursor(0, 0, Point(), NULL)), |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 83 | renderedCursorInvalid(false), |
Pierre Ossman | eef6c9a | 2018-10-05 17:11:25 +0200 | [diff] [blame] | 84 | keyRemapper(&KeyRemapper::defInstance), |
Pierre Ossman | bbf955e | 2011-11-08 12:44:10 +0000 | [diff] [blame] | 85 | lastConnectionTime(0), disableclients(false), |
Pierre Ossman | 6e49e95 | 2016-10-07 15:59:38 +0200 | [diff] [blame] | 86 | frameTimer(this) |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 87 | { |
| 88 | lastUserInputTime = lastDisconnectTime = time(0); |
| 89 | slog.debug("creating single-threaded server %s", name.buf); |
| 90 | } |
| 91 | |
| 92 | VNCServerST::~VNCServerST() |
| 93 | { |
| 94 | slog.debug("shutting down server %s", name.buf); |
| 95 | |
| 96 | // Close any active clients, with appropriate logging & cleanup |
| 97 | closeClients("Server shutdown"); |
| 98 | |
Pierre Ossman | 6e49e95 | 2016-10-07 15:59:38 +0200 | [diff] [blame] | 99 | // Stop trying to render things |
| 100 | stopFrameClock(); |
| 101 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 102 | // Delete all the clients, and their sockets, and any closing sockets |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 103 | while (!clients.empty()) { |
Pierre Ossman | 6c97fa4 | 2018-10-05 17:35:51 +0200 | [diff] [blame] | 104 | VNCSConnectionST* client; |
| 105 | client = clients.front(); |
| 106 | clients.pop_front(); |
| 107 | delete client; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | // Stop the desktop object if active, *only* after deleting all clients! |
Pierre Ossman | b53c3bf | 2018-03-22 16:01:44 +0100 | [diff] [blame] | 111 | stopDesktop(); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 112 | |
Pierre Ossman | 05338bc | 2016-11-08 14:57:11 +0100 | [diff] [blame] | 113 | if (comparer) |
| 114 | comparer->logStats(); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 115 | delete comparer; |
Pierre Ossman | 6a1a0d0 | 2017-02-19 15:48:17 +0100 | [diff] [blame] | 116 | |
| 117 | delete cursor; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | |
| 121 | // SocketServer methods |
| 122 | |
| 123 | void VNCServerST::addSocket(network::Socket* sock, bool outgoing) |
| 124 | { |
| 125 | // - Check the connection isn't black-marked |
| 126 | // *** do this in getSecurity instead? |
| 127 | CharArray address(sock->getPeerAddress()); |
| 128 | if (blHosts->isBlackmarked(address.buf)) { |
| 129 | connectionsLog.error("blacklisted: %s", address.buf); |
| 130 | try { |
| 131 | SConnection::writeConnFailedFromScratch("Too many security failures", |
| 132 | &sock->outStream()); |
| 133 | } catch (rdr::Exception&) { |
| 134 | } |
| 135 | sock->shutdown(); |
| 136 | closingSockets.push_back(sock); |
| 137 | return; |
| 138 | } |
| 139 | |
Pierre Ossman | 6c97fa4 | 2018-10-05 17:35:51 +0200 | [diff] [blame] | 140 | CharArray name; |
| 141 | name.buf = sock->getPeerEndpoint(); |
| 142 | connectionsLog.status("accepted: %s", name.buf); |
| 143 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 144 | if (clients.empty()) { |
| 145 | lastConnectionTime = time(0); |
| 146 | } |
| 147 | |
| 148 | VNCSConnectionST* client = new VNCSConnectionST(this, sock, outgoing); |
Pierre Ossman | 6c97fa4 | 2018-10-05 17:35:51 +0200 | [diff] [blame] | 149 | clients.push_front(client); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 150 | client->init(); |
| 151 | } |
| 152 | |
| 153 | void VNCServerST::removeSocket(network::Socket* sock) { |
| 154 | // - If the socket has resources allocated to it, delete them |
| 155 | std::list<VNCSConnectionST*>::iterator ci; |
| 156 | for (ci = clients.begin(); ci != clients.end(); ci++) { |
| 157 | if ((*ci)->getSock() == sock) { |
Pierre Ossman | 6c97fa4 | 2018-10-05 17:35:51 +0200 | [diff] [blame] | 158 | clients.remove(*ci); |
| 159 | |
Pierre Ossman | b684341 | 2018-10-05 17:30:52 +0200 | [diff] [blame] | 160 | // - Release the cursor if this client owns it |
| 161 | if (pointerClient == *ci) |
| 162 | pointerClient = NULL; |
| 163 | |
Pierre Ossman | 6c97fa4 | 2018-10-05 17:35:51 +0200 | [diff] [blame] | 164 | if ((*ci)->authenticated()) |
| 165 | lastDisconnectTime = time(0); |
| 166 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 167 | // - Delete the per-Socket resources |
| 168 | delete *ci; |
| 169 | |
Pierre Ossman | 6c97fa4 | 2018-10-05 17:35:51 +0200 | [diff] [blame] | 170 | CharArray name; |
| 171 | name.buf = sock->getPeerEndpoint(); |
| 172 | connectionsLog.status("closed: %s", name.buf); |
| 173 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 174 | // - Check that the desktop object is still required |
Pierre Ossman | b53c3bf | 2018-03-22 16:01:44 +0100 | [diff] [blame] | 175 | if (authClientCount() == 0) |
| 176 | stopDesktop(); |
Pierre Ossman | 05338bc | 2016-11-08 14:57:11 +0100 | [diff] [blame] | 177 | |
| 178 | if (comparer) |
| 179 | comparer->logStats(); |
| 180 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 181 | return; |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | // - If the Socket has no resources, it may have been a closingSocket |
| 186 | closingSockets.remove(sock); |
| 187 | } |
| 188 | |
Pierre Ossman | d408ca5 | 2016-04-29 14:26:05 +0200 | [diff] [blame] | 189 | void VNCServerST::processSocketReadEvent(network::Socket* sock) |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 190 | { |
| 191 | // - Find the appropriate VNCSConnectionST and process the event |
| 192 | std::list<VNCSConnectionST*>::iterator ci; |
| 193 | for (ci = clients.begin(); ci != clients.end(); ci++) { |
| 194 | if ((*ci)->getSock() == sock) { |
| 195 | (*ci)->processMessages(); |
| 196 | return; |
| 197 | } |
| 198 | } |
| 199 | throw rdr::Exception("invalid Socket in VNCServerST"); |
| 200 | } |
| 201 | |
Pierre Ossman | d408ca5 | 2016-04-29 14:26:05 +0200 | [diff] [blame] | 202 | void VNCServerST::processSocketWriteEvent(network::Socket* sock) |
| 203 | { |
| 204 | // - Find the appropriate VNCSConnectionST and process the event |
| 205 | std::list<VNCSConnectionST*>::iterator ci; |
| 206 | for (ci = clients.begin(); ci != clients.end(); ci++) { |
| 207 | if ((*ci)->getSock() == sock) { |
| 208 | (*ci)->flushSocket(); |
| 209 | return; |
| 210 | } |
| 211 | } |
| 212 | throw rdr::Exception("invalid Socket in VNCServerST"); |
| 213 | } |
| 214 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 215 | int VNCServerST::checkTimeouts() |
| 216 | { |
| 217 | int timeout = 0; |
| 218 | std::list<VNCSConnectionST*>::iterator ci, ci_next; |
Pierre Ossman | 2d61deb | 2011-10-25 15:18:53 +0000 | [diff] [blame] | 219 | |
| 220 | soonestTimeout(&timeout, Timer::checkTimeouts()); |
| 221 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 222 | for (ci=clients.begin();ci!=clients.end();ci=ci_next) { |
| 223 | ci_next = ci; ci_next++; |
| 224 | soonestTimeout(&timeout, (*ci)->checkIdleTimeout()); |
| 225 | } |
| 226 | |
| 227 | int timeLeft; |
Constantin Kaplinsky | 8499d0c | 2008-08-21 05:51:29 +0000 | [diff] [blame] | 228 | time_t now = time(0); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 229 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 230 | // Check MaxDisconnectionTime |
| 231 | if (rfb::Server::maxDisconnectionTime && clients.empty()) { |
| 232 | if (now < lastDisconnectTime) { |
| 233 | // Someone must have set the time backwards. |
| 234 | slog.info("Time has gone backwards - resetting lastDisconnectTime"); |
| 235 | lastDisconnectTime = now; |
| 236 | } |
| 237 | timeLeft = lastDisconnectTime + rfb::Server::maxDisconnectionTime - now; |
| 238 | if (timeLeft < -60) { |
| 239 | // Someone must have set the time forwards. |
| 240 | slog.info("Time has gone forwards - resetting lastDisconnectTime"); |
| 241 | lastDisconnectTime = now; |
| 242 | timeLeft = rfb::Server::maxDisconnectionTime; |
| 243 | } |
| 244 | if (timeLeft <= 0) { |
| 245 | slog.info("MaxDisconnectionTime reached, exiting"); |
| 246 | exit(0); |
| 247 | } |
| 248 | soonestTimeout(&timeout, timeLeft * 1000); |
| 249 | } |
| 250 | |
| 251 | // Check MaxConnectionTime |
| 252 | if (rfb::Server::maxConnectionTime && lastConnectionTime && !clients.empty()) { |
| 253 | if (now < lastConnectionTime) { |
| 254 | // Someone must have set the time backwards. |
| 255 | slog.info("Time has gone backwards - resetting lastConnectionTime"); |
| 256 | lastConnectionTime = now; |
| 257 | } |
| 258 | timeLeft = lastConnectionTime + rfb::Server::maxConnectionTime - now; |
| 259 | if (timeLeft < -60) { |
| 260 | // Someone must have set the time forwards. |
| 261 | slog.info("Time has gone forwards - resetting lastConnectionTime"); |
| 262 | lastConnectionTime = now; |
| 263 | timeLeft = rfb::Server::maxConnectionTime; |
| 264 | } |
| 265 | if (timeLeft <= 0) { |
| 266 | slog.info("MaxConnectionTime reached, exiting"); |
| 267 | exit(0); |
| 268 | } |
| 269 | soonestTimeout(&timeout, timeLeft * 1000); |
| 270 | } |
| 271 | |
| 272 | |
| 273 | // Check MaxIdleTime |
| 274 | if (rfb::Server::maxIdleTime) { |
| 275 | if (now < lastUserInputTime) { |
| 276 | // Someone must have set the time backwards. |
| 277 | slog.info("Time has gone backwards - resetting lastUserInputTime"); |
| 278 | lastUserInputTime = now; |
| 279 | } |
| 280 | timeLeft = lastUserInputTime + rfb::Server::maxIdleTime - now; |
| 281 | if (timeLeft < -60) { |
| 282 | // Someone must have set the time forwards. |
| 283 | slog.info("Time has gone forwards - resetting lastUserInputTime"); |
| 284 | lastUserInputTime = now; |
| 285 | timeLeft = rfb::Server::maxIdleTime; |
| 286 | } |
| 287 | if (timeLeft <= 0) { |
| 288 | slog.info("MaxIdleTime reached, exiting"); |
| 289 | exit(0); |
| 290 | } |
| 291 | soonestTimeout(&timeout, timeLeft * 1000); |
| 292 | } |
| 293 | |
| 294 | return timeout; |
| 295 | } |
| 296 | |
| 297 | |
| 298 | // VNCServer methods |
| 299 | |
Pierre Ossman | 559a2e8 | 2012-01-23 15:54:11 +0000 | [diff] [blame] | 300 | void VNCServerST::blockUpdates() |
| 301 | { |
| 302 | blockCounter++; |
Pierre Ossman | 6e49e95 | 2016-10-07 15:59:38 +0200 | [diff] [blame] | 303 | |
| 304 | stopFrameClock(); |
Pierre Ossman | 559a2e8 | 2012-01-23 15:54:11 +0000 | [diff] [blame] | 305 | } |
| 306 | |
| 307 | void VNCServerST::unblockUpdates() |
| 308 | { |
| 309 | assert(blockCounter > 0); |
| 310 | |
| 311 | blockCounter--; |
| 312 | |
Pierre Ossman | 6e49e95 | 2016-10-07 15:59:38 +0200 | [diff] [blame] | 313 | // Restart the frame clock if we have updates |
| 314 | if (blockCounter == 0) { |
| 315 | if (!comparer->is_empty()) |
| 316 | startFrameClock(); |
| 317 | } |
Pierre Ossman | 559a2e8 | 2012-01-23 15:54:11 +0000 | [diff] [blame] | 318 | } |
| 319 | |
Pierre Ossman | 04e62db | 2009-03-23 16:57:07 +0000 | [diff] [blame] | 320 | void VNCServerST::setPixelBuffer(PixelBuffer* pb_, const ScreenSet& layout) |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 321 | { |
Pierre Ossman | 05338bc | 2016-11-08 14:57:11 +0100 | [diff] [blame] | 322 | if (comparer) |
| 323 | comparer->logStats(); |
| 324 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 325 | pb = pb_; |
| 326 | delete comparer; |
| 327 | comparer = 0; |
| 328 | |
Pierre Ossman | 04e62db | 2009-03-23 16:57:07 +0000 | [diff] [blame] | 329 | screenLayout = layout; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 330 | |
Pierre Ossman | 04e62db | 2009-03-23 16:57:07 +0000 | [diff] [blame] | 331 | if (!pb) { |
Michal Srb | 28d570d | 2017-09-29 14:45:33 +0200 | [diff] [blame] | 332 | screenLayout = ScreenSet(); |
| 333 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 334 | if (desktopStarted) |
| 335 | throw Exception("setPixelBuffer: null PixelBuffer when desktopStarted?"); |
Michal Srb | 28d570d | 2017-09-29 14:45:33 +0200 | [diff] [blame] | 336 | |
Pierre Ossman | 04e62db | 2009-03-23 16:57:07 +0000 | [diff] [blame] | 337 | return; |
| 338 | } |
| 339 | |
Pierre Ossman | 6cd6117 | 2018-05-07 14:24:56 +0200 | [diff] [blame] | 340 | // Assume the framebuffer contents wasn't saved and reset everything |
| 341 | // that tracks its contents |
Pierre Ossman | 04e62db | 2009-03-23 16:57:07 +0000 | [diff] [blame] | 342 | comparer = new ComparingUpdateTracker(pb); |
Pierre Ossman | 6ea6e1a | 2014-02-12 16:33:43 +0100 | [diff] [blame] | 343 | renderedCursorInvalid = true; |
Pierre Ossman | 6cd6117 | 2018-05-07 14:24:56 +0200 | [diff] [blame] | 344 | add_changed(pb->getRect()); |
Pierre Ossman | 04e62db | 2009-03-23 16:57:07 +0000 | [diff] [blame] | 345 | |
| 346 | // Make sure that we have at least one screen |
| 347 | if (screenLayout.num_screens() == 0) |
| 348 | screenLayout.add_screen(Screen(0, 0, 0, pb->width(), pb->height(), 0)); |
| 349 | |
| 350 | std::list<VNCSConnectionST*>::iterator ci, ci_next; |
| 351 | for (ci=clients.begin();ci!=clients.end();ci=ci_next) { |
| 352 | ci_next = ci; ci_next++; |
| 353 | (*ci)->pixelBufferChange(); |
| 354 | // Since the new pixel buffer means an ExtendedDesktopSize needs to |
| 355 | // be sent anyway, we don't need to call screenLayoutChange. |
| 356 | } |
| 357 | } |
| 358 | |
| 359 | void VNCServerST::setPixelBuffer(PixelBuffer* pb_) |
| 360 | { |
Michal Srb | 28d570d | 2017-09-29 14:45:33 +0200 | [diff] [blame] | 361 | ScreenSet layout = screenLayout; |
Pierre Ossman | 04e62db | 2009-03-23 16:57:07 +0000 | [diff] [blame] | 362 | |
| 363 | // Check that the screen layout is still valid |
Michal Srb | 28d570d | 2017-09-29 14:45:33 +0200 | [diff] [blame] | 364 | if (pb_ && !layout.validate(pb_->width(), pb_->height())) { |
Pierre Ossman | 04e62db | 2009-03-23 16:57:07 +0000 | [diff] [blame] | 365 | Rect fbRect; |
| 366 | ScreenSet::iterator iter, iter_next; |
| 367 | |
| 368 | fbRect.setXYWH(0, 0, pb_->width(), pb_->height()); |
| 369 | |
| 370 | for (iter = layout.begin();iter != layout.end();iter = iter_next) { |
| 371 | iter_next = iter; ++iter_next; |
| 372 | if (iter->dimensions.enclosed_by(fbRect)) |
| 373 | continue; |
| 374 | iter->dimensions = iter->dimensions.intersect(fbRect); |
| 375 | if (iter->dimensions.is_empty()) { |
| 376 | slog.info("Removing screen %d (%x) as it is completely outside the new framebuffer", |
| 377 | (int)iter->id, (unsigned)iter->id); |
| 378 | layout.remove_screen(iter->id); |
| 379 | } |
| 380 | } |
| 381 | } |
| 382 | |
| 383 | setPixelBuffer(pb_, layout); |
| 384 | } |
| 385 | |
| 386 | void VNCServerST::setScreenLayout(const ScreenSet& layout) |
| 387 | { |
| 388 | if (!pb) |
| 389 | throw Exception("setScreenLayout: new screen layout without a PixelBuffer"); |
| 390 | if (!layout.validate(pb->width(), pb->height())) |
| 391 | throw Exception("setScreenLayout: invalid screen layout"); |
| 392 | |
Pierre Ossman | df45320 | 2009-04-02 14:26:45 +0000 | [diff] [blame] | 393 | screenLayout = layout; |
| 394 | |
Pierre Ossman | 04e62db | 2009-03-23 16:57:07 +0000 | [diff] [blame] | 395 | std::list<VNCSConnectionST*>::iterator ci, ci_next; |
| 396 | for (ci=clients.begin();ci!=clients.end();ci=ci_next) { |
| 397 | ci_next = ci; ci_next++; |
Pierre Ossman | a3ac01e | 2011-11-07 21:13:54 +0000 | [diff] [blame] | 398 | (*ci)->screenLayoutChangeOrClose(reasonServer); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 399 | } |
| 400 | } |
| 401 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 402 | void VNCServerST::bell() |
| 403 | { |
| 404 | std::list<VNCSConnectionST*>::iterator ci, ci_next; |
| 405 | for (ci = clients.begin(); ci != clients.end(); ci = ci_next) { |
| 406 | ci_next = ci; ci_next++; |
Pierre Ossman | a3ac01e | 2011-11-07 21:13:54 +0000 | [diff] [blame] | 407 | (*ci)->bellOrClose(); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 408 | } |
| 409 | } |
| 410 | |
| 411 | void VNCServerST::serverCutText(const char* str, int len) |
| 412 | { |
| 413 | std::list<VNCSConnectionST*>::iterator ci, ci_next; |
| 414 | for (ci = clients.begin(); ci != clients.end(); ci = ci_next) { |
| 415 | ci_next = ci; ci_next++; |
Pierre Ossman | a3ac01e | 2011-11-07 21:13:54 +0000 | [diff] [blame] | 416 | (*ci)->serverCutTextOrClose(str, len); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 417 | } |
| 418 | } |
| 419 | |
Peter Ã…strand | c39e078 | 2009-01-15 12:21:42 +0000 | [diff] [blame] | 420 | void VNCServerST::setName(const char* name_) |
| 421 | { |
Adam Tkac | d36b626 | 2009-09-04 10:57:20 +0000 | [diff] [blame] | 422 | name.replaceBuf(strDup(name_)); |
Peter Ã…strand | c39e078 | 2009-01-15 12:21:42 +0000 | [diff] [blame] | 423 | std::list<VNCSConnectionST*>::iterator ci, ci_next; |
| 424 | for (ci = clients.begin(); ci != clients.end(); ci = ci_next) { |
| 425 | ci_next = ci; ci_next++; |
Pierre Ossman | a3ac01e | 2011-11-07 21:13:54 +0000 | [diff] [blame] | 426 | (*ci)->setDesktopNameOrClose(name_); |
Peter Ã…strand | c39e078 | 2009-01-15 12:21:42 +0000 | [diff] [blame] | 427 | } |
| 428 | } |
| 429 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 430 | void VNCServerST::add_changed(const Region& region) |
| 431 | { |
Pierre Ossman | bbf955e | 2011-11-08 12:44:10 +0000 | [diff] [blame] | 432 | if (comparer == NULL) |
| 433 | return; |
| 434 | |
| 435 | comparer->add_changed(region); |
Pierre Ossman | 6e49e95 | 2016-10-07 15:59:38 +0200 | [diff] [blame] | 436 | startFrameClock(); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 437 | } |
| 438 | |
| 439 | void VNCServerST::add_copied(const Region& dest, const Point& delta) |
| 440 | { |
Pierre Ossman | bbf955e | 2011-11-08 12:44:10 +0000 | [diff] [blame] | 441 | if (comparer == NULL) |
| 442 | return; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 443 | |
Pierre Ossman | bbf955e | 2011-11-08 12:44:10 +0000 | [diff] [blame] | 444 | comparer->add_copied(dest, delta); |
Pierre Ossman | 6e49e95 | 2016-10-07 15:59:38 +0200 | [diff] [blame] | 445 | startFrameClock(); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 446 | } |
| 447 | |
| 448 | void VNCServerST::setCursor(int width, int height, const Point& newHotspot, |
Pierre Ossman | 6a1a0d0 | 2017-02-19 15:48:17 +0100 | [diff] [blame] | 449 | const rdr::U8* data) |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 450 | { |
Pierre Ossman | 6a1a0d0 | 2017-02-19 15:48:17 +0100 | [diff] [blame] | 451 | delete cursor; |
| 452 | cursor = new Cursor(width, height, newHotspot, data); |
| 453 | cursor->crop(); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 454 | |
| 455 | renderedCursorInvalid = true; |
| 456 | |
| 457 | std::list<VNCSConnectionST*>::iterator ci, ci_next; |
| 458 | for (ci = clients.begin(); ci != clients.end(); ci = ci_next) { |
| 459 | ci_next = ci; ci_next++; |
| 460 | (*ci)->renderedCursorChange(); |
| 461 | (*ci)->setCursorOrClose(); |
| 462 | } |
| 463 | } |
| 464 | |
| 465 | void VNCServerST::setCursorPos(const Point& pos) |
| 466 | { |
| 467 | if (!cursorPos.equals(pos)) { |
| 468 | cursorPos = pos; |
| 469 | renderedCursorInvalid = true; |
| 470 | std::list<VNCSConnectionST*>::iterator ci; |
| 471 | for (ci = clients.begin(); ci != clients.end(); ci++) |
| 472 | (*ci)->renderedCursorChange(); |
| 473 | } |
| 474 | } |
| 475 | |
Pierre Ossman | bb305ca | 2016-12-11 12:41:26 +0100 | [diff] [blame] | 476 | void VNCServerST::setLEDState(unsigned int state) |
| 477 | { |
Pierre Ossman | b45a84f | 2016-12-12 16:59:15 +0100 | [diff] [blame] | 478 | std::list<VNCSConnectionST*>::iterator ci, ci_next; |
| 479 | |
| 480 | if (state == ledState) |
| 481 | return; |
| 482 | |
Pierre Ossman | bb305ca | 2016-12-11 12:41:26 +0100 | [diff] [blame] | 483 | ledState = state; |
Pierre Ossman | b45a84f | 2016-12-12 16:59:15 +0100 | [diff] [blame] | 484 | |
| 485 | for (ci = clients.begin(); ci != clients.end(); ci = ci_next) { |
| 486 | ci_next = ci; ci_next++; |
| 487 | (*ci)->setLEDStateOrClose(state); |
| 488 | } |
Pierre Ossman | bb305ca | 2016-12-11 12:41:26 +0100 | [diff] [blame] | 489 | } |
| 490 | |
Pierre Ossman | b684341 | 2018-10-05 17:30:52 +0200 | [diff] [blame] | 491 | // Event handlers |
| 492 | |
| 493 | void VNCServerST::keyEvent(rdr::U32 keysym, rdr::U32 keycode, bool down) |
| 494 | { |
| 495 | lastUserInputTime = time(0); |
| 496 | |
| 497 | // Remap the key if required |
| 498 | if (keyRemapper) { |
| 499 | rdr::U32 newkey; |
| 500 | newkey = keyRemapper->remapKey(keysym); |
| 501 | if (newkey != keysym) { |
| 502 | slog.debug("Key remapped to 0x%x", newkey); |
| 503 | keysym = newkey; |
| 504 | } |
| 505 | } |
| 506 | |
| 507 | desktop->keyEvent(keysym, keycode, down); |
| 508 | } |
| 509 | |
| 510 | void VNCServerST::pointerEvent(VNCSConnectionST* client, |
| 511 | const Point& pos, int buttonMask) |
| 512 | { |
| 513 | lastUserInputTime = time(0); |
| 514 | |
| 515 | // Let one client own the cursor whilst buttons are pressed in order |
| 516 | // to provide a bit more sane user experience |
| 517 | if ((pointerClient != NULL) && (pointerClient != client)) |
| 518 | return; |
| 519 | |
| 520 | if (buttonMask) |
| 521 | pointerClient = client; |
| 522 | else |
| 523 | pointerClient = NULL; |
| 524 | |
| 525 | desktop->pointerEvent(pos, buttonMask); |
| 526 | } |
| 527 | |
| 528 | void VNCServerST::clientCutText(const char* str, int len) |
| 529 | { |
| 530 | desktop->clientCutText(str, len); |
| 531 | } |
| 532 | |
Pierre Ossman | 07e44cc | 2018-10-05 17:32:57 +0200 | [diff] [blame] | 533 | unsigned int VNCServerST::setDesktopSize(VNCSConnectionST* requester, |
| 534 | int fb_width, int fb_height, |
| 535 | const ScreenSet& layout) |
| 536 | { |
| 537 | unsigned int result; |
| 538 | std::list<VNCSConnectionST*>::iterator ci, ci_next; |
| 539 | |
| 540 | // Don't bother the desktop with an invalid configuration |
| 541 | if (!layout.validate(fb_width, fb_height)) |
| 542 | return resultInvalid; |
| 543 | |
| 544 | // FIXME: the desktop will call back to VNCServerST and an extra set |
| 545 | // of ExtendedDesktopSize messages will be sent. This is okay |
| 546 | // protocol-wise, but unnecessary. |
| 547 | result = desktop->setScreenLayout(fb_width, fb_height, layout); |
| 548 | if (result != resultSuccess) |
| 549 | return result; |
| 550 | |
| 551 | // Sanity check |
| 552 | if (screenLayout != layout) |
| 553 | throw Exception("Desktop configured a different screen layout than requested"); |
| 554 | |
| 555 | // Notify other clients |
| 556 | for (ci=clients.begin();ci!=clients.end();ci=ci_next) { |
| 557 | ci_next = ci; ci_next++; |
| 558 | if ((*ci) == requester) |
| 559 | continue; |
| 560 | (*ci)->screenLayoutChangeOrClose(reasonOtherClient); |
| 561 | } |
| 562 | |
| 563 | return resultSuccess; |
| 564 | } |
| 565 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 566 | // Other public methods |
| 567 | |
| 568 | void VNCServerST::approveConnection(network::Socket* sock, bool accept, |
| 569 | const char* reason) |
| 570 | { |
| 571 | std::list<VNCSConnectionST*>::iterator ci; |
| 572 | for (ci = clients.begin(); ci != clients.end(); ci++) { |
| 573 | if ((*ci)->getSock() == sock) { |
| 574 | (*ci)->approveConnectionOrClose(accept, reason); |
| 575 | return; |
| 576 | } |
| 577 | } |
| 578 | } |
| 579 | |
| 580 | void VNCServerST::closeClients(const char* reason, network::Socket* except) |
| 581 | { |
| 582 | std::list<VNCSConnectionST*>::iterator i, next_i; |
| 583 | for (i=clients.begin(); i!=clients.end(); i=next_i) { |
| 584 | next_i = i; next_i++; |
| 585 | if ((*i)->getSock() != except) |
| 586 | (*i)->close(reason); |
| 587 | } |
| 588 | } |
| 589 | |
| 590 | void VNCServerST::getSockets(std::list<network::Socket*>* sockets) |
| 591 | { |
| 592 | sockets->clear(); |
| 593 | std::list<VNCSConnectionST*>::iterator ci; |
| 594 | for (ci = clients.begin(); ci != clients.end(); ci++) { |
| 595 | sockets->push_back((*ci)->getSock()); |
| 596 | } |
| 597 | std::list<network::Socket*>::iterator si; |
| 598 | for (si = closingSockets.begin(); si != closingSockets.end(); si++) { |
| 599 | sockets->push_back(*si); |
| 600 | } |
| 601 | } |
| 602 | |
Pierre Ossman | 7d64b33 | 2018-10-08 15:59:02 +0200 | [diff] [blame] | 603 | SConnection* VNCServerST::getConnection(network::Socket* sock) { |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 604 | std::list<VNCSConnectionST*>::iterator ci; |
| 605 | for (ci = clients.begin(); ci != clients.end(); ci++) { |
| 606 | if ((*ci)->getSock() == sock) |
| 607 | return *ci; |
| 608 | } |
| 609 | return 0; |
| 610 | } |
| 611 | |
Pierre Ossman | bbf955e | 2011-11-08 12:44:10 +0000 | [diff] [blame] | 612 | bool VNCServerST::handleTimeout(Timer* t) |
| 613 | { |
Pierre Ossman | 6e49e95 | 2016-10-07 15:59:38 +0200 | [diff] [blame] | 614 | if (t == &frameTimer) { |
| 615 | // We keep running until we go a full interval without any updates |
| 616 | if (comparer->is_empty()) |
| 617 | return false; |
Pierre Ossman | bbf955e | 2011-11-08 12:44:10 +0000 | [diff] [blame] | 618 | |
Pierre Ossman | 6e49e95 | 2016-10-07 15:59:38 +0200 | [diff] [blame] | 619 | writeUpdate(); |
Pierre Ossman | 7be73d7 | 2017-11-06 13:16:35 +0100 | [diff] [blame] | 620 | |
| 621 | // If this is the first iteration then we need to adjust the timeout |
| 622 | if (frameTimer.getTimeoutMs() != 1000/rfb::Server::frameRate) { |
| 623 | frameTimer.start(1000/rfb::Server::frameRate); |
| 624 | return false; |
| 625 | } |
| 626 | |
Pierre Ossman | 6e49e95 | 2016-10-07 15:59:38 +0200 | [diff] [blame] | 627 | return true; |
| 628 | } |
Pierre Ossman | bbf955e | 2011-11-08 12:44:10 +0000 | [diff] [blame] | 629 | |
| 630 | return false; |
| 631 | } |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 632 | |
Pierre Ossman | 6c97fa4 | 2018-10-05 17:35:51 +0200 | [diff] [blame] | 633 | void VNCServerST::queryConnection(VNCSConnectionST* client, |
Pierre Ossman | eef6c9a | 2018-10-05 17:11:25 +0200 | [diff] [blame] | 634 | const char* userName) |
| 635 | { |
Pierre Ossman | 6c97fa4 | 2018-10-05 17:35:51 +0200 | [diff] [blame] | 636 | // - Authentication succeeded - clear from blacklist |
| 637 | CharArray name; |
| 638 | name.buf = client->getSock()->getPeerAddress(); |
| 639 | blHosts->clearBlackmark(name.buf); |
| 640 | |
| 641 | // - Prepare the desktop for that the client will start requiring |
| 642 | // resources after this |
| 643 | startDesktop(); |
| 644 | |
| 645 | // - Special case to provide a more useful error message |
| 646 | if (rfb::Server::neverShared && |
| 647 | !rfb::Server::disconnectClients && |
| 648 | authClientCount() > 0) { |
| 649 | approveConnection(client->getSock(), false, |
| 650 | "The server is already in use"); |
| 651 | return; |
| 652 | } |
| 653 | |
| 654 | // - Are we configured to do queries? |
| 655 | if (!rfb::Server::queryConnect && |
| 656 | !client->getSock()->requiresQuery()) { |
| 657 | approveConnection(client->getSock(), true, NULL); |
| 658 | return; |
| 659 | } |
| 660 | |
| 661 | // - Does the client have the right to bypass the query? |
| 662 | if (client->accessCheck(SConnection::AccessNoQuery)) |
| 663 | { |
| 664 | approveConnection(client->getSock(), true, NULL); |
| 665 | return; |
| 666 | } |
| 667 | |
| 668 | desktop->queryConnection(client->getSock(), userName); |
| 669 | } |
| 670 | |
| 671 | void VNCServerST::clientReady(VNCSConnectionST* client, bool shared) |
| 672 | { |
| 673 | if (!shared) { |
| 674 | if (rfb::Server::disconnectClients && |
| 675 | client->accessCheck(SConnection::AccessNonShared)) { |
| 676 | // - Close all the other connected clients |
| 677 | slog.debug("non-shared connection - closing clients"); |
| 678 | closeClients("Non-shared connection requested", client->getSock()); |
| 679 | } else { |
| 680 | // - Refuse this connection if there are existing clients, in addition to |
| 681 | // this one |
| 682 | if (authClientCount() > 1) { |
| 683 | client->close("Server is already in use"); |
| 684 | return; |
| 685 | } |
| 686 | } |
| 687 | } |
Pierre Ossman | eef6c9a | 2018-10-05 17:11:25 +0200 | [diff] [blame] | 688 | } |
| 689 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 690 | // -=- Internal methods |
| 691 | |
| 692 | void VNCServerST::startDesktop() |
| 693 | { |
| 694 | if (!desktopStarted) { |
| 695 | slog.debug("starting desktop"); |
| 696 | desktop->start(this); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 697 | if (!pb) |
| 698 | throw Exception("SDesktop::start() did not set a valid PixelBuffer"); |
Pierre Ossman | 6cd6117 | 2018-05-07 14:24:56 +0200 | [diff] [blame] | 699 | desktopStarted = true; |
| 700 | // The tracker might have accumulated changes whilst we were |
| 701 | // stopped, so flush those out |
| 702 | if (!comparer->is_empty()) |
| 703 | writeUpdate(); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 704 | } |
| 705 | } |
| 706 | |
Pierre Ossman | b53c3bf | 2018-03-22 16:01:44 +0100 | [diff] [blame] | 707 | void VNCServerST::stopDesktop() |
| 708 | { |
| 709 | if (desktopStarted) { |
| 710 | slog.debug("stopping desktop"); |
| 711 | desktopStarted = false; |
| 712 | desktop->stop(); |
| 713 | stopFrameClock(); |
| 714 | } |
| 715 | } |
| 716 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 717 | int VNCServerST::authClientCount() { |
| 718 | int count = 0; |
| 719 | std::list<VNCSConnectionST*>::iterator ci; |
| 720 | for (ci = clients.begin(); ci != clients.end(); ci++) { |
| 721 | if ((*ci)->authenticated()) |
| 722 | count++; |
| 723 | } |
| 724 | return count; |
| 725 | } |
| 726 | |
| 727 | inline bool VNCServerST::needRenderedCursor() |
| 728 | { |
| 729 | std::list<VNCSConnectionST*>::iterator ci; |
| 730 | for (ci = clients.begin(); ci != clients.end(); ci++) |
| 731 | if ((*ci)->needRenderedCursor()) return true; |
| 732 | return false; |
| 733 | } |
| 734 | |
Pierre Ossman | 6e49e95 | 2016-10-07 15:59:38 +0200 | [diff] [blame] | 735 | void VNCServerST::startFrameClock() |
Pierre Ossman | bbf955e | 2011-11-08 12:44:10 +0000 | [diff] [blame] | 736 | { |
Pierre Ossman | 6e49e95 | 2016-10-07 15:59:38 +0200 | [diff] [blame] | 737 | if (frameTimer.isStarted()) |
Pierre Ossman | bbf955e | 2011-11-08 12:44:10 +0000 | [diff] [blame] | 738 | return; |
Pierre Ossman | 559a2e8 | 2012-01-23 15:54:11 +0000 | [diff] [blame] | 739 | if (blockCounter > 0) |
| 740 | return; |
Pierre Ossman | b53c3bf | 2018-03-22 16:01:44 +0100 | [diff] [blame] | 741 | if (!desktopStarted) |
| 742 | return; |
Pierre Ossman | 559a2e8 | 2012-01-23 15:54:11 +0000 | [diff] [blame] | 743 | |
Pierre Ossman | 7be73d7 | 2017-11-06 13:16:35 +0100 | [diff] [blame] | 744 | // The first iteration will be just half a frame as we get a very |
| 745 | // unstable update rate if we happen to be perfectly in sync with |
| 746 | // the application's update rate |
| 747 | frameTimer.start(1000/rfb::Server::frameRate/2); |
Pierre Ossman | bbf955e | 2011-11-08 12:44:10 +0000 | [diff] [blame] | 748 | } |
| 749 | |
Pierre Ossman | 6e49e95 | 2016-10-07 15:59:38 +0200 | [diff] [blame] | 750 | void VNCServerST::stopFrameClock() |
| 751 | { |
| 752 | frameTimer.stop(); |
| 753 | } |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 754 | |
Pierre Ossman | a2b80d6 | 2018-03-23 09:30:09 +0100 | [diff] [blame] | 755 | int VNCServerST::msToNextUpdate() |
| 756 | { |
| 757 | // FIXME: If the application is updating slower than frameRate then |
| 758 | // we could allow the clients more time here |
| 759 | |
| 760 | if (!frameTimer.isStarted()) |
| 761 | return 1000/rfb::Server::frameRate/2; |
| 762 | else |
| 763 | return frameTimer.getRemainingMs(); |
| 764 | } |
| 765 | |
Pierre Ossman | 6e49e95 | 2016-10-07 15:59:38 +0200 | [diff] [blame] | 766 | // writeUpdate() is called on a regular interval in order to see what |
| 767 | // updates are pending and propagates them to the update tracker for |
| 768 | // each client. It uses the ComparingUpdateTracker's compare() method |
| 769 | // to filter out areas of the screen which haven't actually changed. It |
| 770 | // also checks the state of the (server-side) rendered cursor, if |
| 771 | // necessary rendering it again with the correct background. |
| 772 | |
| 773 | void VNCServerST::writeUpdate() |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 774 | { |
Constantin Kaplinsky | 604d781 | 2007-08-31 15:50:37 +0000 | [diff] [blame] | 775 | UpdateInfo ui; |
Pierre Ossman | 6e49e95 | 2016-10-07 15:59:38 +0200 | [diff] [blame] | 776 | Region toCheck; |
| 777 | |
| 778 | std::list<VNCSConnectionST*>::iterator ci, ci_next; |
| 779 | |
| 780 | assert(blockCounter == 0); |
Pierre Ossman | b53c3bf | 2018-03-22 16:01:44 +0100 | [diff] [blame] | 781 | assert(desktopStarted); |
Pierre Ossman | 6e49e95 | 2016-10-07 15:59:38 +0200 | [diff] [blame] | 782 | |
Constantin Kaplinsky | 604d781 | 2007-08-31 15:50:37 +0000 | [diff] [blame] | 783 | comparer->getUpdateInfo(&ui, pb->getRect()); |
Pierre Ossman | 6e49e95 | 2016-10-07 15:59:38 +0200 | [diff] [blame] | 784 | toCheck = ui.changed.union_(ui.copied); |
Constantin Kaplinsky | 604d781 | 2007-08-31 15:50:37 +0000 | [diff] [blame] | 785 | |
Pierre Ossman | 6e49e95 | 2016-10-07 15:59:38 +0200 | [diff] [blame] | 786 | if (needRenderedCursor()) { |
Pierre Ossman | 6a1a0d0 | 2017-02-19 15:48:17 +0100 | [diff] [blame] | 787 | Rect clippedCursorRect = Rect(0, 0, cursor->width(), cursor->height()) |
| 788 | .translate(cursorPos.subtract(cursor->hotspot())) |
| 789 | .intersect(pb->getRect()); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 790 | |
Pierre Ossman | 24684e5 | 2016-12-05 16:58:19 +0100 | [diff] [blame] | 791 | if (!toCheck.intersect(clippedCursorRect).is_empty()) |
| 792 | renderedCursorInvalid = true; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 793 | } |
| 794 | |
| 795 | pb->grabRegion(toCheck); |
| 796 | |
Pierre Ossman | b114cec | 2011-11-20 15:36:11 +0000 | [diff] [blame] | 797 | if (getComparerState()) |
| 798 | comparer->enable(); |
| 799 | else |
| 800 | comparer->disable(); |
| 801 | |
| 802 | if (comparer->compare()) |
Constantin Kaplinsky | f0b3be7 | 2008-08-21 05:22:04 +0000 | [diff] [blame] | 803 | comparer->getUpdateInfo(&ui, pb->getRect()); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 804 | |
Pierre Ossman | 6e49e95 | 2016-10-07 15:59:38 +0200 | [diff] [blame] | 805 | comparer->clear(); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 806 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 807 | for (ci = clients.begin(); ci != clients.end(); ci = ci_next) { |
| 808 | ci_next = ci; ci_next++; |
Constantin Kaplinsky | 604d781 | 2007-08-31 15:50:37 +0000 | [diff] [blame] | 809 | (*ci)->add_copied(ui.copied, ui.copy_delta); |
| 810 | (*ci)->add_changed(ui.changed); |
Pierre Ossman | 6e49e95 | 2016-10-07 15:59:38 +0200 | [diff] [blame] | 811 | (*ci)->writeFramebufferUpdateOrClose(); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 812 | } |
Pierre Ossman | 6e49e95 | 2016-10-07 15:59:38 +0200 | [diff] [blame] | 813 | } |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 814 | |
Pierre Ossman | 6e49e95 | 2016-10-07 15:59:38 +0200 | [diff] [blame] | 815 | // checkUpdate() is called by clients to see if it is safe to read from |
| 816 | // the framebuffer at this time. |
| 817 | |
Pierre Ossman | 8efc7b4 | 2018-03-23 11:45:51 +0100 | [diff] [blame] | 818 | Region VNCServerST::getPendingRegion() |
Pierre Ossman | 6e49e95 | 2016-10-07 15:59:38 +0200 | [diff] [blame] | 819 | { |
Pierre Ossman | 8efc7b4 | 2018-03-23 11:45:51 +0100 | [diff] [blame] | 820 | UpdateInfo ui; |
| 821 | |
Pierre Ossman | 6e49e95 | 2016-10-07 15:59:38 +0200 | [diff] [blame] | 822 | // Block clients as the frame buffer cannot be safely accessed |
| 823 | if (blockCounter > 0) |
Pierre Ossman | 8efc7b4 | 2018-03-23 11:45:51 +0100 | [diff] [blame] | 824 | return pb->getRect(); |
Pierre Ossman | 6e49e95 | 2016-10-07 15:59:38 +0200 | [diff] [blame] | 825 | |
| 826 | // Block client from updating if there are pending updates |
Pierre Ossman | 8efc7b4 | 2018-03-23 11:45:51 +0100 | [diff] [blame] | 827 | if (comparer->is_empty()) |
| 828 | return Region(); |
Pierre Ossman | bbf955e | 2011-11-08 12:44:10 +0000 | [diff] [blame] | 829 | |
Pierre Ossman | 8efc7b4 | 2018-03-23 11:45:51 +0100 | [diff] [blame] | 830 | comparer->getUpdateInfo(&ui, pb->getRect()); |
| 831 | |
| 832 | return ui.changed.union_(ui.copied); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 833 | } |
| 834 | |
Pierre Ossman | 24684e5 | 2016-12-05 16:58:19 +0100 | [diff] [blame] | 835 | const RenderedCursor* VNCServerST::getRenderedCursor() |
| 836 | { |
| 837 | if (renderedCursorInvalid) { |
Pierre Ossman | 7cb4f31 | 2017-02-24 13:25:00 +0100 | [diff] [blame] | 838 | renderedCursor.update(pb, cursor, cursorPos); |
Pierre Ossman | 24684e5 | 2016-12-05 16:58:19 +0100 | [diff] [blame] | 839 | renderedCursorInvalid = false; |
| 840 | } |
| 841 | |
| 842 | return &renderedCursor; |
| 843 | } |
| 844 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 845 | void VNCServerST::getConnInfo(ListConnInfo * listConn) |
| 846 | { |
| 847 | listConn->Clear(); |
| 848 | listConn->setDisable(getDisable()); |
| 849 | if (clients.empty()) |
| 850 | return; |
| 851 | std::list<VNCSConnectionST*>::iterator i; |
| 852 | for (i = clients.begin(); i != clients.end(); i++) |
| 853 | listConn->addInfo((void*)(*i), (*i)->getSock()->getPeerAddress(), |
| 854 | (*i)->getStartTime(), (*i)->getStatus()); |
| 855 | } |
| 856 | |
| 857 | void VNCServerST::setConnStatus(ListConnInfo* listConn) |
| 858 | { |
| 859 | setDisable(listConn->getDisable()); |
| 860 | if (listConn->Empty() || clients.empty()) return; |
| 861 | for (listConn->iBegin(); !listConn->iEnd(); listConn->iNext()) { |
| 862 | VNCSConnectionST* conn = (VNCSConnectionST*)listConn->iGetConn(); |
| 863 | std::list<VNCSConnectionST*>::iterator i; |
| 864 | for (i = clients.begin(); i != clients.end(); i++) { |
| 865 | if ((*i) == conn) { |
| 866 | int status = listConn->iGetStatus(); |
| 867 | if (status == 3) { |
| 868 | (*i)->close(0); |
| 869 | } else { |
| 870 | (*i)->setStatus(status); |
| 871 | } |
| 872 | break; |
| 873 | } |
| 874 | } |
| 875 | } |
| 876 | } |
Constantin Kaplinsky | 9d1fc6c | 2008-06-14 05:23:10 +0000 | [diff] [blame] | 877 | |
Pierre Ossman | b114cec | 2011-11-20 15:36:11 +0000 | [diff] [blame] | 878 | bool VNCServerST::getComparerState() |
| 879 | { |
| 880 | if (rfb::Server::compareFB == 0) |
| 881 | return false; |
| 882 | if (rfb::Server::compareFB != 2) |
| 883 | return true; |
| 884 | |
| 885 | std::list<VNCSConnectionST*>::iterator ci, ci_next; |
| 886 | for (ci=clients.begin();ci!=clients.end();ci=ci_next) { |
| 887 | ci_next = ci; ci_next++; |
| 888 | if ((*ci)->getComparerState()) |
| 889 | return true; |
| 890 | } |
| 891 | return false; |
| 892 | } |