Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 1 | /* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved. |
Pierre Ossman | 6a1a0d0 | 2017-02-19 15:48:17 +0100 | [diff] [blame] | 2 | * Copyright 2009-2017 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> |
| 57 | #include <rfb/Security.h> |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 58 | #include <rfb/ServerCore.h> |
| 59 | #include <rfb/VNCServerST.h> |
| 60 | #include <rfb/VNCSConnectionST.h> |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 61 | #include <rfb/util.h> |
Pierre Ossman | bb305ca | 2016-12-11 12:41:26 +0100 | [diff] [blame] | 62 | #include <rfb/ledStates.h> |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 63 | |
| 64 | #include <rdr/types.h> |
| 65 | |
| 66 | using namespace rfb; |
| 67 | |
| 68 | static LogWriter slog("VNCServerST"); |
| 69 | LogWriter VNCServerST::connectionsLog("Connections"); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 70 | |
| 71 | // |
| 72 | // -=- VNCServerST Implementation |
| 73 | // |
| 74 | |
| 75 | // -=- Constructors/Destructor |
| 76 | |
Adam Tkac | a6578bf | 2010-04-23 14:07:41 +0000 | [diff] [blame] | 77 | VNCServerST::VNCServerST(const char* name_, SDesktop* desktop_) |
Pierre Ossman | 559a2e8 | 2012-01-23 15:54:11 +0000 | [diff] [blame] | 78 | : blHosts(&blacklist), desktop(desktop_), desktopStarted(false), |
Pierre Ossman | bb305ca | 2016-12-11 12:41:26 +0100 | [diff] [blame] | 79 | blockCounter(0), pb(0), ledState(ledUnknown), |
Adam Tkac | d36b626 | 2009-09-04 10:57:20 +0000 | [diff] [blame] | 80 | name(strDup(name_)), pointerClient(0), comparer(0), |
Pierre Ossman | 6a1a0d0 | 2017-02-19 15:48:17 +0100 | [diff] [blame] | 81 | cursor(new Cursor(0, 0, Point(), NULL)), |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 82 | renderedCursorInvalid(false), |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 83 | queryConnectionHandler(0), keyRemapper(&KeyRemapper::defInstance), |
Pierre Ossman | bbf955e | 2011-11-08 12:44:10 +0000 | [diff] [blame] | 84 | lastConnectionTime(0), disableclients(false), |
Pierre Ossman | 6e49e95 | 2016-10-07 15:59:38 +0200 | [diff] [blame] | 85 | frameTimer(this) |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 86 | { |
| 87 | lastUserInputTime = lastDisconnectTime = time(0); |
| 88 | slog.debug("creating single-threaded server %s", name.buf); |
| 89 | } |
| 90 | |
| 91 | VNCServerST::~VNCServerST() |
| 92 | { |
| 93 | slog.debug("shutting down server %s", name.buf); |
| 94 | |
| 95 | // Close any active clients, with appropriate logging & cleanup |
| 96 | closeClients("Server shutdown"); |
| 97 | |
Pierre Ossman | 6e49e95 | 2016-10-07 15:59:38 +0200 | [diff] [blame] | 98 | // Stop trying to render things |
| 99 | stopFrameClock(); |
| 100 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 101 | // Delete all the clients, and their sockets, and any closing sockets |
| 102 | // NB: Deleting a client implicitly removes it from the clients list |
| 103 | while (!clients.empty()) { |
| 104 | delete clients.front(); |
| 105 | } |
| 106 | |
| 107 | // Stop the desktop object if active, *only* after deleting all clients! |
| 108 | if (desktopStarted) { |
| 109 | desktopStarted = false; |
| 110 | desktop->stop(); |
| 111 | } |
| 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 | |
| 140 | if (clients.empty()) { |
| 141 | lastConnectionTime = time(0); |
| 142 | } |
| 143 | |
| 144 | VNCSConnectionST* client = new VNCSConnectionST(this, sock, outgoing); |
| 145 | client->init(); |
| 146 | } |
| 147 | |
| 148 | void VNCServerST::removeSocket(network::Socket* sock) { |
| 149 | // - If the socket has resources allocated to it, delete them |
| 150 | std::list<VNCSConnectionST*>::iterator ci; |
| 151 | for (ci = clients.begin(); ci != clients.end(); ci++) { |
| 152 | if ((*ci)->getSock() == sock) { |
| 153 | // - Delete the per-Socket resources |
| 154 | delete *ci; |
| 155 | |
| 156 | // - Check that the desktop object is still required |
| 157 | if (authClientCount() == 0 && desktopStarted) { |
| 158 | slog.debug("no authenticated clients - stopping desktop"); |
| 159 | desktopStarted = false; |
| 160 | desktop->stop(); |
Michal Srb | 28d570d | 2017-09-29 14:45:33 +0200 | [diff] [blame] | 161 | stopFrameClock(); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 162 | } |
Pierre Ossman | 05338bc | 2016-11-08 14:57:11 +0100 | [diff] [blame] | 163 | |
| 164 | if (comparer) |
| 165 | comparer->logStats(); |
| 166 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 167 | return; |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | // - If the Socket has no resources, it may have been a closingSocket |
| 172 | closingSockets.remove(sock); |
| 173 | } |
| 174 | |
Pierre Ossman | d408ca5 | 2016-04-29 14:26:05 +0200 | [diff] [blame] | 175 | void VNCServerST::processSocketReadEvent(network::Socket* sock) |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 176 | { |
| 177 | // - Find the appropriate VNCSConnectionST and process the event |
| 178 | std::list<VNCSConnectionST*>::iterator ci; |
| 179 | for (ci = clients.begin(); ci != clients.end(); ci++) { |
| 180 | if ((*ci)->getSock() == sock) { |
| 181 | (*ci)->processMessages(); |
| 182 | return; |
| 183 | } |
| 184 | } |
| 185 | throw rdr::Exception("invalid Socket in VNCServerST"); |
| 186 | } |
| 187 | |
Pierre Ossman | d408ca5 | 2016-04-29 14:26:05 +0200 | [diff] [blame] | 188 | void VNCServerST::processSocketWriteEvent(network::Socket* sock) |
| 189 | { |
| 190 | // - Find the appropriate VNCSConnectionST and process the event |
| 191 | std::list<VNCSConnectionST*>::iterator ci; |
| 192 | for (ci = clients.begin(); ci != clients.end(); ci++) { |
| 193 | if ((*ci)->getSock() == sock) { |
| 194 | (*ci)->flushSocket(); |
| 195 | return; |
| 196 | } |
| 197 | } |
| 198 | throw rdr::Exception("invalid Socket in VNCServerST"); |
| 199 | } |
| 200 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 201 | int VNCServerST::checkTimeouts() |
| 202 | { |
| 203 | int timeout = 0; |
| 204 | std::list<VNCSConnectionST*>::iterator ci, ci_next; |
Pierre Ossman | 2d61deb | 2011-10-25 15:18:53 +0000 | [diff] [blame] | 205 | |
| 206 | soonestTimeout(&timeout, Timer::checkTimeouts()); |
| 207 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 208 | for (ci=clients.begin();ci!=clients.end();ci=ci_next) { |
| 209 | ci_next = ci; ci_next++; |
| 210 | soonestTimeout(&timeout, (*ci)->checkIdleTimeout()); |
| 211 | } |
| 212 | |
| 213 | int timeLeft; |
Constantin Kaplinsky | 8499d0c | 2008-08-21 05:51:29 +0000 | [diff] [blame] | 214 | time_t now = time(0); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 215 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 216 | // Check MaxDisconnectionTime |
| 217 | if (rfb::Server::maxDisconnectionTime && clients.empty()) { |
| 218 | if (now < lastDisconnectTime) { |
| 219 | // Someone must have set the time backwards. |
| 220 | slog.info("Time has gone backwards - resetting lastDisconnectTime"); |
| 221 | lastDisconnectTime = now; |
| 222 | } |
| 223 | timeLeft = lastDisconnectTime + rfb::Server::maxDisconnectionTime - now; |
| 224 | if (timeLeft < -60) { |
| 225 | // Someone must have set the time forwards. |
| 226 | slog.info("Time has gone forwards - resetting lastDisconnectTime"); |
| 227 | lastDisconnectTime = now; |
| 228 | timeLeft = rfb::Server::maxDisconnectionTime; |
| 229 | } |
| 230 | if (timeLeft <= 0) { |
| 231 | slog.info("MaxDisconnectionTime reached, exiting"); |
| 232 | exit(0); |
| 233 | } |
| 234 | soonestTimeout(&timeout, timeLeft * 1000); |
| 235 | } |
| 236 | |
| 237 | // Check MaxConnectionTime |
| 238 | if (rfb::Server::maxConnectionTime && lastConnectionTime && !clients.empty()) { |
| 239 | if (now < lastConnectionTime) { |
| 240 | // Someone must have set the time backwards. |
| 241 | slog.info("Time has gone backwards - resetting lastConnectionTime"); |
| 242 | lastConnectionTime = now; |
| 243 | } |
| 244 | timeLeft = lastConnectionTime + rfb::Server::maxConnectionTime - now; |
| 245 | if (timeLeft < -60) { |
| 246 | // Someone must have set the time forwards. |
| 247 | slog.info("Time has gone forwards - resetting lastConnectionTime"); |
| 248 | lastConnectionTime = now; |
| 249 | timeLeft = rfb::Server::maxConnectionTime; |
| 250 | } |
| 251 | if (timeLeft <= 0) { |
| 252 | slog.info("MaxConnectionTime reached, exiting"); |
| 253 | exit(0); |
| 254 | } |
| 255 | soonestTimeout(&timeout, timeLeft * 1000); |
| 256 | } |
| 257 | |
| 258 | |
| 259 | // Check MaxIdleTime |
| 260 | if (rfb::Server::maxIdleTime) { |
| 261 | if (now < lastUserInputTime) { |
| 262 | // Someone must have set the time backwards. |
| 263 | slog.info("Time has gone backwards - resetting lastUserInputTime"); |
| 264 | lastUserInputTime = now; |
| 265 | } |
| 266 | timeLeft = lastUserInputTime + rfb::Server::maxIdleTime - now; |
| 267 | if (timeLeft < -60) { |
| 268 | // Someone must have set the time forwards. |
| 269 | slog.info("Time has gone forwards - resetting lastUserInputTime"); |
| 270 | lastUserInputTime = now; |
| 271 | timeLeft = rfb::Server::maxIdleTime; |
| 272 | } |
| 273 | if (timeLeft <= 0) { |
| 274 | slog.info("MaxIdleTime reached, exiting"); |
| 275 | exit(0); |
| 276 | } |
| 277 | soonestTimeout(&timeout, timeLeft * 1000); |
| 278 | } |
| 279 | |
| 280 | return timeout; |
| 281 | } |
| 282 | |
| 283 | |
| 284 | // VNCServer methods |
| 285 | |
Pierre Ossman | 559a2e8 | 2012-01-23 15:54:11 +0000 | [diff] [blame] | 286 | void VNCServerST::blockUpdates() |
| 287 | { |
| 288 | blockCounter++; |
Pierre Ossman | 6e49e95 | 2016-10-07 15:59:38 +0200 | [diff] [blame] | 289 | |
| 290 | stopFrameClock(); |
Pierre Ossman | 559a2e8 | 2012-01-23 15:54:11 +0000 | [diff] [blame] | 291 | } |
| 292 | |
| 293 | void VNCServerST::unblockUpdates() |
| 294 | { |
| 295 | assert(blockCounter > 0); |
| 296 | |
| 297 | blockCounter--; |
| 298 | |
Pierre Ossman | 6e49e95 | 2016-10-07 15:59:38 +0200 | [diff] [blame] | 299 | // Restart the frame clock if we have updates |
| 300 | if (blockCounter == 0) { |
| 301 | if (!comparer->is_empty()) |
| 302 | startFrameClock(); |
| 303 | } |
Pierre Ossman | 559a2e8 | 2012-01-23 15:54:11 +0000 | [diff] [blame] | 304 | } |
| 305 | |
Pierre Ossman | 04e62db | 2009-03-23 16:57:07 +0000 | [diff] [blame] | 306 | void VNCServerST::setPixelBuffer(PixelBuffer* pb_, const ScreenSet& layout) |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 307 | { |
Pierre Ossman | 05338bc | 2016-11-08 14:57:11 +0100 | [diff] [blame] | 308 | if (comparer) |
| 309 | comparer->logStats(); |
| 310 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 311 | pb = pb_; |
| 312 | delete comparer; |
| 313 | comparer = 0; |
| 314 | |
Pierre Ossman | 04e62db | 2009-03-23 16:57:07 +0000 | [diff] [blame] | 315 | screenLayout = layout; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 316 | |
Pierre Ossman | 04e62db | 2009-03-23 16:57:07 +0000 | [diff] [blame] | 317 | if (!pb) { |
Michal Srb | 28d570d | 2017-09-29 14:45:33 +0200 | [diff] [blame] | 318 | screenLayout = ScreenSet(); |
| 319 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 320 | if (desktopStarted) |
| 321 | throw Exception("setPixelBuffer: null PixelBuffer when desktopStarted?"); |
Michal Srb | 28d570d | 2017-09-29 14:45:33 +0200 | [diff] [blame] | 322 | |
Pierre Ossman | 04e62db | 2009-03-23 16:57:07 +0000 | [diff] [blame] | 323 | return; |
| 324 | } |
| 325 | |
| 326 | comparer = new ComparingUpdateTracker(pb); |
Pierre Ossman | 6ea6e1a | 2014-02-12 16:33:43 +0100 | [diff] [blame] | 327 | renderedCursorInvalid = true; |
Pierre Ossman | b1e80f7 | 2017-09-22 16:48:14 +0200 | [diff] [blame] | 328 | startFrameClock(); |
Pierre Ossman | 04e62db | 2009-03-23 16:57:07 +0000 | [diff] [blame] | 329 | |
| 330 | // Make sure that we have at least one screen |
| 331 | if (screenLayout.num_screens() == 0) |
| 332 | screenLayout.add_screen(Screen(0, 0, 0, pb->width(), pb->height(), 0)); |
| 333 | |
| 334 | std::list<VNCSConnectionST*>::iterator ci, ci_next; |
| 335 | for (ci=clients.begin();ci!=clients.end();ci=ci_next) { |
| 336 | ci_next = ci; ci_next++; |
| 337 | (*ci)->pixelBufferChange(); |
| 338 | // Since the new pixel buffer means an ExtendedDesktopSize needs to |
| 339 | // be sent anyway, we don't need to call screenLayoutChange. |
| 340 | } |
| 341 | } |
| 342 | |
| 343 | void VNCServerST::setPixelBuffer(PixelBuffer* pb_) |
| 344 | { |
Michal Srb | 28d570d | 2017-09-29 14:45:33 +0200 | [diff] [blame] | 345 | ScreenSet layout = screenLayout; |
Pierre Ossman | 04e62db | 2009-03-23 16:57:07 +0000 | [diff] [blame] | 346 | |
| 347 | // Check that the screen layout is still valid |
Michal Srb | 28d570d | 2017-09-29 14:45:33 +0200 | [diff] [blame] | 348 | if (pb_ && !layout.validate(pb_->width(), pb_->height())) { |
Pierre Ossman | 04e62db | 2009-03-23 16:57:07 +0000 | [diff] [blame] | 349 | Rect fbRect; |
| 350 | ScreenSet::iterator iter, iter_next; |
| 351 | |
| 352 | fbRect.setXYWH(0, 0, pb_->width(), pb_->height()); |
| 353 | |
| 354 | for (iter = layout.begin();iter != layout.end();iter = iter_next) { |
| 355 | iter_next = iter; ++iter_next; |
| 356 | if (iter->dimensions.enclosed_by(fbRect)) |
| 357 | continue; |
| 358 | iter->dimensions = iter->dimensions.intersect(fbRect); |
| 359 | if (iter->dimensions.is_empty()) { |
| 360 | slog.info("Removing screen %d (%x) as it is completely outside the new framebuffer", |
| 361 | (int)iter->id, (unsigned)iter->id); |
| 362 | layout.remove_screen(iter->id); |
| 363 | } |
| 364 | } |
| 365 | } |
| 366 | |
| 367 | setPixelBuffer(pb_, layout); |
| 368 | } |
| 369 | |
| 370 | void VNCServerST::setScreenLayout(const ScreenSet& layout) |
| 371 | { |
| 372 | if (!pb) |
| 373 | throw Exception("setScreenLayout: new screen layout without a PixelBuffer"); |
| 374 | if (!layout.validate(pb->width(), pb->height())) |
| 375 | throw Exception("setScreenLayout: invalid screen layout"); |
| 376 | |
Pierre Ossman | df45320 | 2009-04-02 14:26:45 +0000 | [diff] [blame] | 377 | screenLayout = layout; |
| 378 | |
Pierre Ossman | 04e62db | 2009-03-23 16:57:07 +0000 | [diff] [blame] | 379 | std::list<VNCSConnectionST*>::iterator ci, ci_next; |
| 380 | for (ci=clients.begin();ci!=clients.end();ci=ci_next) { |
| 381 | ci_next = ci; ci_next++; |
Pierre Ossman | a3ac01e | 2011-11-07 21:13:54 +0000 | [diff] [blame] | 382 | (*ci)->screenLayoutChangeOrClose(reasonServer); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 383 | } |
| 384 | } |
| 385 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 386 | void VNCServerST::bell() |
| 387 | { |
| 388 | std::list<VNCSConnectionST*>::iterator ci, ci_next; |
| 389 | for (ci = clients.begin(); ci != clients.end(); ci = ci_next) { |
| 390 | ci_next = ci; ci_next++; |
Pierre Ossman | a3ac01e | 2011-11-07 21:13:54 +0000 | [diff] [blame] | 391 | (*ci)->bellOrClose(); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 392 | } |
| 393 | } |
| 394 | |
| 395 | void VNCServerST::serverCutText(const char* str, int len) |
| 396 | { |
| 397 | std::list<VNCSConnectionST*>::iterator ci, ci_next; |
| 398 | for (ci = clients.begin(); ci != clients.end(); ci = ci_next) { |
| 399 | ci_next = ci; ci_next++; |
Pierre Ossman | a3ac01e | 2011-11-07 21:13:54 +0000 | [diff] [blame] | 400 | (*ci)->serverCutTextOrClose(str, len); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 401 | } |
| 402 | } |
| 403 | |
Peter Ã…strand | c39e078 | 2009-01-15 12:21:42 +0000 | [diff] [blame] | 404 | void VNCServerST::setName(const char* name_) |
| 405 | { |
Adam Tkac | d36b626 | 2009-09-04 10:57:20 +0000 | [diff] [blame] | 406 | name.replaceBuf(strDup(name_)); |
Peter Ã…strand | c39e078 | 2009-01-15 12:21:42 +0000 | [diff] [blame] | 407 | std::list<VNCSConnectionST*>::iterator ci, ci_next; |
| 408 | for (ci = clients.begin(); ci != clients.end(); ci = ci_next) { |
| 409 | ci_next = ci; ci_next++; |
Pierre Ossman | a3ac01e | 2011-11-07 21:13:54 +0000 | [diff] [blame] | 410 | (*ci)->setDesktopNameOrClose(name_); |
Peter Ã…strand | c39e078 | 2009-01-15 12:21:42 +0000 | [diff] [blame] | 411 | } |
| 412 | } |
| 413 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 414 | void VNCServerST::add_changed(const Region& region) |
| 415 | { |
Pierre Ossman | bbf955e | 2011-11-08 12:44:10 +0000 | [diff] [blame] | 416 | if (comparer == NULL) |
| 417 | return; |
| 418 | |
| 419 | comparer->add_changed(region); |
Pierre Ossman | 6e49e95 | 2016-10-07 15:59:38 +0200 | [diff] [blame] | 420 | startFrameClock(); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 421 | } |
| 422 | |
| 423 | void VNCServerST::add_copied(const Region& dest, const Point& delta) |
| 424 | { |
Pierre Ossman | bbf955e | 2011-11-08 12:44:10 +0000 | [diff] [blame] | 425 | if (comparer == NULL) |
| 426 | return; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 427 | |
Pierre Ossman | bbf955e | 2011-11-08 12:44:10 +0000 | [diff] [blame] | 428 | comparer->add_copied(dest, delta); |
Pierre Ossman | 6e49e95 | 2016-10-07 15:59:38 +0200 | [diff] [blame] | 429 | startFrameClock(); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 430 | } |
| 431 | |
| 432 | void VNCServerST::setCursor(int width, int height, const Point& newHotspot, |
Pierre Ossman | 6a1a0d0 | 2017-02-19 15:48:17 +0100 | [diff] [blame] | 433 | const rdr::U8* data) |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 434 | { |
Pierre Ossman | 6a1a0d0 | 2017-02-19 15:48:17 +0100 | [diff] [blame] | 435 | delete cursor; |
| 436 | cursor = new Cursor(width, height, newHotspot, data); |
| 437 | cursor->crop(); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 438 | |
| 439 | renderedCursorInvalid = true; |
| 440 | |
| 441 | std::list<VNCSConnectionST*>::iterator ci, ci_next; |
| 442 | for (ci = clients.begin(); ci != clients.end(); ci = ci_next) { |
| 443 | ci_next = ci; ci_next++; |
| 444 | (*ci)->renderedCursorChange(); |
| 445 | (*ci)->setCursorOrClose(); |
| 446 | } |
| 447 | } |
| 448 | |
| 449 | void VNCServerST::setCursorPos(const Point& pos) |
| 450 | { |
| 451 | if (!cursorPos.equals(pos)) { |
| 452 | cursorPos = pos; |
| 453 | renderedCursorInvalid = true; |
| 454 | std::list<VNCSConnectionST*>::iterator ci; |
| 455 | for (ci = clients.begin(); ci != clients.end(); ci++) |
| 456 | (*ci)->renderedCursorChange(); |
| 457 | } |
| 458 | } |
| 459 | |
Pierre Ossman | bb305ca | 2016-12-11 12:41:26 +0100 | [diff] [blame] | 460 | void VNCServerST::setLEDState(unsigned int state) |
| 461 | { |
Pierre Ossman | b45a84f | 2016-12-12 16:59:15 +0100 | [diff] [blame] | 462 | std::list<VNCSConnectionST*>::iterator ci, ci_next; |
| 463 | |
| 464 | if (state == ledState) |
| 465 | return; |
| 466 | |
Pierre Ossman | bb305ca | 2016-12-11 12:41:26 +0100 | [diff] [blame] | 467 | ledState = state; |
Pierre Ossman | b45a84f | 2016-12-12 16:59:15 +0100 | [diff] [blame] | 468 | |
| 469 | for (ci = clients.begin(); ci != clients.end(); ci = ci_next) { |
| 470 | ci_next = ci; ci_next++; |
| 471 | (*ci)->setLEDStateOrClose(state); |
| 472 | } |
Pierre Ossman | bb305ca | 2016-12-11 12:41:26 +0100 | [diff] [blame] | 473 | } |
| 474 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 475 | // Other public methods |
| 476 | |
| 477 | void VNCServerST::approveConnection(network::Socket* sock, bool accept, |
| 478 | const char* reason) |
| 479 | { |
| 480 | std::list<VNCSConnectionST*>::iterator ci; |
| 481 | for (ci = clients.begin(); ci != clients.end(); ci++) { |
| 482 | if ((*ci)->getSock() == sock) { |
| 483 | (*ci)->approveConnectionOrClose(accept, reason); |
| 484 | return; |
| 485 | } |
| 486 | } |
| 487 | } |
| 488 | |
| 489 | void VNCServerST::closeClients(const char* reason, network::Socket* except) |
| 490 | { |
| 491 | std::list<VNCSConnectionST*>::iterator i, next_i; |
| 492 | for (i=clients.begin(); i!=clients.end(); i=next_i) { |
| 493 | next_i = i; next_i++; |
| 494 | if ((*i)->getSock() != except) |
| 495 | (*i)->close(reason); |
| 496 | } |
| 497 | } |
| 498 | |
| 499 | void VNCServerST::getSockets(std::list<network::Socket*>* sockets) |
| 500 | { |
| 501 | sockets->clear(); |
| 502 | std::list<VNCSConnectionST*>::iterator ci; |
| 503 | for (ci = clients.begin(); ci != clients.end(); ci++) { |
| 504 | sockets->push_back((*ci)->getSock()); |
| 505 | } |
| 506 | std::list<network::Socket*>::iterator si; |
| 507 | for (si = closingSockets.begin(); si != closingSockets.end(); si++) { |
| 508 | sockets->push_back(*si); |
| 509 | } |
| 510 | } |
| 511 | |
| 512 | SConnection* VNCServerST::getSConnection(network::Socket* sock) { |
| 513 | std::list<VNCSConnectionST*>::iterator ci; |
| 514 | for (ci = clients.begin(); ci != clients.end(); ci++) { |
| 515 | if ((*ci)->getSock() == sock) |
| 516 | return *ci; |
| 517 | } |
| 518 | return 0; |
| 519 | } |
| 520 | |
Pierre Ossman | bbf955e | 2011-11-08 12:44:10 +0000 | [diff] [blame] | 521 | bool VNCServerST::handleTimeout(Timer* t) |
| 522 | { |
Pierre Ossman | 6e49e95 | 2016-10-07 15:59:38 +0200 | [diff] [blame] | 523 | if (t == &frameTimer) { |
| 524 | // We keep running until we go a full interval without any updates |
| 525 | if (comparer->is_empty()) |
| 526 | return false; |
Pierre Ossman | bbf955e | 2011-11-08 12:44:10 +0000 | [diff] [blame] | 527 | |
Pierre Ossman | 6e49e95 | 2016-10-07 15:59:38 +0200 | [diff] [blame] | 528 | writeUpdate(); |
Pierre Ossman | 7be73d7 | 2017-11-06 13:16:35 +0100 | [diff] [blame] | 529 | |
| 530 | // If this is the first iteration then we need to adjust the timeout |
| 531 | if (frameTimer.getTimeoutMs() != 1000/rfb::Server::frameRate) { |
| 532 | frameTimer.start(1000/rfb::Server::frameRate); |
| 533 | return false; |
| 534 | } |
| 535 | |
Pierre Ossman | 6e49e95 | 2016-10-07 15:59:38 +0200 | [diff] [blame] | 536 | return true; |
| 537 | } |
Pierre Ossman | bbf955e | 2011-11-08 12:44:10 +0000 | [diff] [blame] | 538 | |
| 539 | return false; |
| 540 | } |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 541 | |
| 542 | // -=- Internal methods |
| 543 | |
| 544 | void VNCServerST::startDesktop() |
| 545 | { |
| 546 | if (!desktopStarted) { |
| 547 | slog.debug("starting desktop"); |
| 548 | desktop->start(this); |
| 549 | desktopStarted = true; |
| 550 | if (!pb) |
| 551 | throw Exception("SDesktop::start() did not set a valid PixelBuffer"); |
| 552 | } |
| 553 | } |
| 554 | |
| 555 | int VNCServerST::authClientCount() { |
| 556 | int count = 0; |
| 557 | std::list<VNCSConnectionST*>::iterator ci; |
| 558 | for (ci = clients.begin(); ci != clients.end(); ci++) { |
| 559 | if ((*ci)->authenticated()) |
| 560 | count++; |
| 561 | } |
| 562 | return count; |
| 563 | } |
| 564 | |
| 565 | inline bool VNCServerST::needRenderedCursor() |
| 566 | { |
| 567 | std::list<VNCSConnectionST*>::iterator ci; |
| 568 | for (ci = clients.begin(); ci != clients.end(); ci++) |
| 569 | if ((*ci)->needRenderedCursor()) return true; |
| 570 | return false; |
| 571 | } |
| 572 | |
Pierre Ossman | 6e49e95 | 2016-10-07 15:59:38 +0200 | [diff] [blame] | 573 | void VNCServerST::startFrameClock() |
Pierre Ossman | bbf955e | 2011-11-08 12:44:10 +0000 | [diff] [blame] | 574 | { |
Pierre Ossman | 6e49e95 | 2016-10-07 15:59:38 +0200 | [diff] [blame] | 575 | if (frameTimer.isStarted()) |
Pierre Ossman | bbf955e | 2011-11-08 12:44:10 +0000 | [diff] [blame] | 576 | return; |
Pierre Ossman | 559a2e8 | 2012-01-23 15:54:11 +0000 | [diff] [blame] | 577 | if (blockCounter > 0) |
| 578 | return; |
| 579 | |
Pierre Ossman | 7be73d7 | 2017-11-06 13:16:35 +0100 | [diff] [blame] | 580 | // The first iteration will be just half a frame as we get a very |
| 581 | // unstable update rate if we happen to be perfectly in sync with |
| 582 | // the application's update rate |
| 583 | frameTimer.start(1000/rfb::Server::frameRate/2); |
Pierre Ossman | bbf955e | 2011-11-08 12:44:10 +0000 | [diff] [blame] | 584 | } |
| 585 | |
Pierre Ossman | 6e49e95 | 2016-10-07 15:59:38 +0200 | [diff] [blame] | 586 | void VNCServerST::stopFrameClock() |
| 587 | { |
| 588 | frameTimer.stop(); |
| 589 | } |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 590 | |
Pierre Ossman | 6e49e95 | 2016-10-07 15:59:38 +0200 | [diff] [blame] | 591 | // writeUpdate() is called on a regular interval in order to see what |
| 592 | // updates are pending and propagates them to the update tracker for |
| 593 | // each client. It uses the ComparingUpdateTracker's compare() method |
| 594 | // to filter out areas of the screen which haven't actually changed. It |
| 595 | // also checks the state of the (server-side) rendered cursor, if |
| 596 | // necessary rendering it again with the correct background. |
| 597 | |
| 598 | void VNCServerST::writeUpdate() |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 599 | { |
Constantin Kaplinsky | 604d781 | 2007-08-31 15:50:37 +0000 | [diff] [blame] | 600 | UpdateInfo ui; |
Pierre Ossman | 6e49e95 | 2016-10-07 15:59:38 +0200 | [diff] [blame] | 601 | Region toCheck; |
| 602 | |
| 603 | std::list<VNCSConnectionST*>::iterator ci, ci_next; |
| 604 | |
| 605 | assert(blockCounter == 0); |
| 606 | |
Constantin Kaplinsky | 604d781 | 2007-08-31 15:50:37 +0000 | [diff] [blame] | 607 | comparer->getUpdateInfo(&ui, pb->getRect()); |
Pierre Ossman | 6e49e95 | 2016-10-07 15:59:38 +0200 | [diff] [blame] | 608 | toCheck = ui.changed.union_(ui.copied); |
Constantin Kaplinsky | 604d781 | 2007-08-31 15:50:37 +0000 | [diff] [blame] | 609 | |
Pierre Ossman | 6e49e95 | 2016-10-07 15:59:38 +0200 | [diff] [blame] | 610 | if (needRenderedCursor()) { |
Pierre Ossman | 6a1a0d0 | 2017-02-19 15:48:17 +0100 | [diff] [blame] | 611 | Rect clippedCursorRect = Rect(0, 0, cursor->width(), cursor->height()) |
| 612 | .translate(cursorPos.subtract(cursor->hotspot())) |
| 613 | .intersect(pb->getRect()); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 614 | |
Pierre Ossman | 24684e5 | 2016-12-05 16:58:19 +0100 | [diff] [blame] | 615 | if (!toCheck.intersect(clippedCursorRect).is_empty()) |
| 616 | renderedCursorInvalid = true; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 617 | } |
| 618 | |
| 619 | pb->grabRegion(toCheck); |
| 620 | |
Pierre Ossman | b114cec | 2011-11-20 15:36:11 +0000 | [diff] [blame] | 621 | if (getComparerState()) |
| 622 | comparer->enable(); |
| 623 | else |
| 624 | comparer->disable(); |
| 625 | |
| 626 | if (comparer->compare()) |
Constantin Kaplinsky | f0b3be7 | 2008-08-21 05:22:04 +0000 | [diff] [blame] | 627 | comparer->getUpdateInfo(&ui, pb->getRect()); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 628 | |
Pierre Ossman | 6e49e95 | 2016-10-07 15:59:38 +0200 | [diff] [blame] | 629 | comparer->clear(); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 630 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 631 | for (ci = clients.begin(); ci != clients.end(); ci = ci_next) { |
| 632 | ci_next = ci; ci_next++; |
Constantin Kaplinsky | 604d781 | 2007-08-31 15:50:37 +0000 | [diff] [blame] | 633 | (*ci)->add_copied(ui.copied, ui.copy_delta); |
| 634 | (*ci)->add_changed(ui.changed); |
Pierre Ossman | 6e49e95 | 2016-10-07 15:59:38 +0200 | [diff] [blame] | 635 | (*ci)->writeFramebufferUpdateOrClose(); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 636 | } |
Pierre Ossman | 6e49e95 | 2016-10-07 15:59:38 +0200 | [diff] [blame] | 637 | } |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 638 | |
Pierre Ossman | 6e49e95 | 2016-10-07 15:59:38 +0200 | [diff] [blame] | 639 | // checkUpdate() is called by clients to see if it is safe to read from |
| 640 | // the framebuffer at this time. |
| 641 | |
| 642 | bool VNCServerST::checkUpdate() |
| 643 | { |
| 644 | // Block clients as the frame buffer cannot be safely accessed |
| 645 | if (blockCounter > 0) |
| 646 | return false; |
| 647 | |
| 648 | // Block client from updating if there are pending updates |
| 649 | if (!comparer->is_empty()) |
| 650 | return false; |
Pierre Ossman | bbf955e | 2011-11-08 12:44:10 +0000 | [diff] [blame] | 651 | |
| 652 | return true; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 653 | } |
| 654 | |
Pierre Ossman | 24684e5 | 2016-12-05 16:58:19 +0100 | [diff] [blame] | 655 | const RenderedCursor* VNCServerST::getRenderedCursor() |
| 656 | { |
| 657 | if (renderedCursorInvalid) { |
Pierre Ossman | 7cb4f31 | 2017-02-24 13:25:00 +0100 | [diff] [blame] | 658 | renderedCursor.update(pb, cursor, cursorPos); |
Pierre Ossman | 24684e5 | 2016-12-05 16:58:19 +0100 | [diff] [blame] | 659 | renderedCursorInvalid = false; |
| 660 | } |
| 661 | |
| 662 | return &renderedCursor; |
| 663 | } |
| 664 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 665 | void VNCServerST::getConnInfo(ListConnInfo * listConn) |
| 666 | { |
| 667 | listConn->Clear(); |
| 668 | listConn->setDisable(getDisable()); |
| 669 | if (clients.empty()) |
| 670 | return; |
| 671 | std::list<VNCSConnectionST*>::iterator i; |
| 672 | for (i = clients.begin(); i != clients.end(); i++) |
| 673 | listConn->addInfo((void*)(*i), (*i)->getSock()->getPeerAddress(), |
| 674 | (*i)->getStartTime(), (*i)->getStatus()); |
| 675 | } |
| 676 | |
| 677 | void VNCServerST::setConnStatus(ListConnInfo* listConn) |
| 678 | { |
| 679 | setDisable(listConn->getDisable()); |
| 680 | if (listConn->Empty() || clients.empty()) return; |
| 681 | for (listConn->iBegin(); !listConn->iEnd(); listConn->iNext()) { |
| 682 | VNCSConnectionST* conn = (VNCSConnectionST*)listConn->iGetConn(); |
| 683 | std::list<VNCSConnectionST*>::iterator i; |
| 684 | for (i = clients.begin(); i != clients.end(); i++) { |
| 685 | if ((*i) == conn) { |
| 686 | int status = listConn->iGetStatus(); |
| 687 | if (status == 3) { |
| 688 | (*i)->close(0); |
| 689 | } else { |
| 690 | (*i)->setStatus(status); |
| 691 | } |
| 692 | break; |
| 693 | } |
| 694 | } |
| 695 | } |
| 696 | } |
Constantin Kaplinsky | 9d1fc6c | 2008-06-14 05:23:10 +0000 | [diff] [blame] | 697 | |
Pierre Ossman | 04e62db | 2009-03-23 16:57:07 +0000 | [diff] [blame] | 698 | void VNCServerST::notifyScreenLayoutChange(VNCSConnectionST* requester) |
| 699 | { |
| 700 | std::list<VNCSConnectionST*>::iterator ci, ci_next; |
| 701 | for (ci=clients.begin();ci!=clients.end();ci=ci_next) { |
| 702 | ci_next = ci; ci_next++; |
| 703 | if ((*ci) == requester) |
| 704 | continue; |
Pierre Ossman | a3ac01e | 2011-11-07 21:13:54 +0000 | [diff] [blame] | 705 | (*ci)->screenLayoutChangeOrClose(reasonOtherClient); |
Pierre Ossman | 04e62db | 2009-03-23 16:57:07 +0000 | [diff] [blame] | 706 | } |
| 707 | } |
Pierre Ossman | b114cec | 2011-11-20 15:36:11 +0000 | [diff] [blame] | 708 | |
| 709 | bool VNCServerST::getComparerState() |
| 710 | { |
| 711 | if (rfb::Server::compareFB == 0) |
| 712 | return false; |
| 713 | if (rfb::Server::compareFB != 2) |
| 714 | return true; |
| 715 | |
| 716 | std::list<VNCSConnectionST*>::iterator ci, ci_next; |
| 717 | for (ci=clients.begin();ci!=clients.end();ci=ci_next) { |
| 718 | ci_next = ci; ci_next++; |
| 719 | if ((*ci)->getComparerState()) |
| 720 | return true; |
| 721 | } |
| 722 | return false; |
| 723 | } |