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 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 54 | #include <rfb/ServerCore.h> |
| 55 | #include <rfb/VNCServerST.h> |
| 56 | #include <rfb/VNCSConnectionST.h> |
| 57 | #include <rfb/ComparingUpdateTracker.h> |
Adam Tkac | a6578bf | 2010-04-23 14:07:41 +0000 | [diff] [blame] | 58 | #include <rfb/Security.h> |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 59 | #include <rfb/KeyRemapper.h> |
| 60 | #include <rfb/util.h> |
| 61 | |
| 62 | #include <rdr/types.h> |
| 63 | |
| 64 | using namespace rfb; |
| 65 | |
| 66 | static LogWriter slog("VNCServerST"); |
| 67 | LogWriter VNCServerST::connectionsLog("Connections"); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 68 | |
Pierre Ossman | bbf955e | 2011-11-08 12:44:10 +0000 | [diff] [blame] | 69 | rfb::IntParameter deferUpdateTime("DeferUpdate", |
DRC | 4548f30 | 2011-12-22 15:57:59 +0000 | [diff] [blame] | 70 | "Time in milliseconds to defer updates",1); |
Pierre Ossman | bbf955e | 2011-11-08 12:44:10 +0000 | [diff] [blame] | 71 | |
| 72 | rfb::BoolParameter alwaysSetDeferUpdateTimer("AlwaysSetDeferUpdateTimer", |
| 73 | "Always reset the defer update timer on every change",false); |
| 74 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 75 | // |
| 76 | // -=- VNCServerST Implementation |
| 77 | // |
| 78 | |
| 79 | // -=- Constructors/Destructor |
| 80 | |
Adam Tkac | a6578bf | 2010-04-23 14:07:41 +0000 | [diff] [blame] | 81 | VNCServerST::VNCServerST(const char* name_, SDesktop* desktop_) |
Pierre Ossman | 559a2e8 | 2012-01-23 15:54:11 +0000 | [diff] [blame] | 82 | : blHosts(&blacklist), desktop(desktop_), desktopStarted(false), |
| 83 | blockCounter(0), pb(0), |
Adam Tkac | d36b626 | 2009-09-04 10:57:20 +0000 | [diff] [blame] | 84 | name(strDup(name_)), pointerClient(0), comparer(0), |
Pierre Ossman | 6a1a0d0 | 2017-02-19 15:48:17 +0100 | [diff] [blame] | 85 | cursor(new Cursor(0, 0, Point(), NULL)), |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 86 | renderedCursorInvalid(false), |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 87 | queryConnectionHandler(0), keyRemapper(&KeyRemapper::defInstance), |
Pierre Ossman | bbf955e | 2011-11-08 12:44:10 +0000 | [diff] [blame] | 88 | lastConnectionTime(0), disableclients(false), |
| 89 | deferTimer(this), deferPending(false) |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 90 | { |
| 91 | lastUserInputTime = lastDisconnectTime = time(0); |
| 92 | slog.debug("creating single-threaded server %s", name.buf); |
| 93 | } |
| 94 | |
| 95 | VNCServerST::~VNCServerST() |
| 96 | { |
| 97 | slog.debug("shutting down server %s", name.buf); |
| 98 | |
| 99 | // Close any active clients, with appropriate logging & cleanup |
| 100 | closeClients("Server shutdown"); |
| 101 | |
| 102 | // Delete all the clients, and their sockets, and any closing sockets |
| 103 | // NB: Deleting a client implicitly removes it from the clients list |
| 104 | while (!clients.empty()) { |
| 105 | delete clients.front(); |
| 106 | } |
| 107 | |
| 108 | // Stop the desktop object if active, *only* after deleting all clients! |
| 109 | if (desktopStarted) { |
| 110 | desktopStarted = false; |
| 111 | desktop->stop(); |
| 112 | } |
| 113 | |
Pierre Ossman | 05338bc | 2016-11-08 14:57:11 +0100 | [diff] [blame] | 114 | if (comparer) |
| 115 | comparer->logStats(); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 116 | delete comparer; |
Pierre Ossman | 6a1a0d0 | 2017-02-19 15:48:17 +0100 | [diff] [blame] | 117 | |
| 118 | delete cursor; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | |
| 122 | // SocketServer methods |
| 123 | |
| 124 | void VNCServerST::addSocket(network::Socket* sock, bool outgoing) |
| 125 | { |
| 126 | // - Check the connection isn't black-marked |
| 127 | // *** do this in getSecurity instead? |
| 128 | CharArray address(sock->getPeerAddress()); |
| 129 | if (blHosts->isBlackmarked(address.buf)) { |
| 130 | connectionsLog.error("blacklisted: %s", address.buf); |
| 131 | try { |
| 132 | SConnection::writeConnFailedFromScratch("Too many security failures", |
| 133 | &sock->outStream()); |
| 134 | } catch (rdr::Exception&) { |
| 135 | } |
| 136 | sock->shutdown(); |
| 137 | closingSockets.push_back(sock); |
| 138 | return; |
| 139 | } |
| 140 | |
| 141 | if (clients.empty()) { |
| 142 | lastConnectionTime = time(0); |
| 143 | } |
| 144 | |
| 145 | VNCSConnectionST* client = new VNCSConnectionST(this, sock, outgoing); |
| 146 | client->init(); |
| 147 | } |
| 148 | |
| 149 | void VNCServerST::removeSocket(network::Socket* sock) { |
| 150 | // - If the socket has resources allocated to it, delete them |
| 151 | std::list<VNCSConnectionST*>::iterator ci; |
| 152 | for (ci = clients.begin(); ci != clients.end(); ci++) { |
| 153 | if ((*ci)->getSock() == sock) { |
| 154 | // - Delete the per-Socket resources |
| 155 | delete *ci; |
| 156 | |
| 157 | // - Check that the desktop object is still required |
| 158 | if (authClientCount() == 0 && desktopStarted) { |
| 159 | slog.debug("no authenticated clients - stopping desktop"); |
| 160 | desktopStarted = false; |
| 161 | desktop->stop(); |
| 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++; |
| 289 | } |
| 290 | |
| 291 | void VNCServerST::unblockUpdates() |
| 292 | { |
| 293 | assert(blockCounter > 0); |
| 294 | |
| 295 | blockCounter--; |
| 296 | |
| 297 | // Flush out any updates we might have blocked |
| 298 | if (blockCounter == 0) |
| 299 | tryUpdate(); |
| 300 | } |
| 301 | |
Pierre Ossman | 04e62db | 2009-03-23 16:57:07 +0000 | [diff] [blame] | 302 | void VNCServerST::setPixelBuffer(PixelBuffer* pb_, const ScreenSet& layout) |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 303 | { |
Pierre Ossman | 05338bc | 2016-11-08 14:57:11 +0100 | [diff] [blame] | 304 | if (comparer) |
| 305 | comparer->logStats(); |
| 306 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 307 | pb = pb_; |
| 308 | delete comparer; |
| 309 | comparer = 0; |
| 310 | |
Pierre Ossman | 04e62db | 2009-03-23 16:57:07 +0000 | [diff] [blame] | 311 | screenLayout = layout; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 312 | |
Pierre Ossman | 04e62db | 2009-03-23 16:57:07 +0000 | [diff] [blame] | 313 | if (!pb) { |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 314 | if (desktopStarted) |
| 315 | throw Exception("setPixelBuffer: null PixelBuffer when desktopStarted?"); |
Pierre Ossman | 04e62db | 2009-03-23 16:57:07 +0000 | [diff] [blame] | 316 | return; |
| 317 | } |
| 318 | |
| 319 | comparer = new ComparingUpdateTracker(pb); |
Pierre Ossman | 6ea6e1a | 2014-02-12 16:33:43 +0100 | [diff] [blame] | 320 | renderedCursorInvalid = true; |
Pierre Ossman | 04e62db | 2009-03-23 16:57:07 +0000 | [diff] [blame] | 321 | |
| 322 | // Make sure that we have at least one screen |
| 323 | if (screenLayout.num_screens() == 0) |
| 324 | screenLayout.add_screen(Screen(0, 0, 0, pb->width(), pb->height(), 0)); |
| 325 | |
| 326 | std::list<VNCSConnectionST*>::iterator ci, ci_next; |
| 327 | for (ci=clients.begin();ci!=clients.end();ci=ci_next) { |
| 328 | ci_next = ci; ci_next++; |
| 329 | (*ci)->pixelBufferChange(); |
| 330 | // Since the new pixel buffer means an ExtendedDesktopSize needs to |
| 331 | // be sent anyway, we don't need to call screenLayoutChange. |
| 332 | } |
| 333 | } |
| 334 | |
| 335 | void VNCServerST::setPixelBuffer(PixelBuffer* pb_) |
| 336 | { |
| 337 | ScreenSet layout; |
| 338 | |
| 339 | if (!pb_) { |
| 340 | if (desktopStarted) |
| 341 | throw Exception("setPixelBuffer: null PixelBuffer when desktopStarted?"); |
| 342 | return; |
| 343 | } |
| 344 | |
| 345 | layout = screenLayout; |
| 346 | |
| 347 | // Check that the screen layout is still valid |
| 348 | if (!layout.validate(pb_->width(), pb_->height())) { |
| 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); |
| 420 | startDefer(); |
| 421 | tryUpdate(); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 422 | } |
| 423 | |
| 424 | void VNCServerST::add_copied(const Region& dest, const Point& delta) |
| 425 | { |
Pierre Ossman | bbf955e | 2011-11-08 12:44:10 +0000 | [diff] [blame] | 426 | if (comparer == NULL) |
| 427 | return; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 428 | |
Pierre Ossman | bbf955e | 2011-11-08 12:44:10 +0000 | [diff] [blame] | 429 | comparer->add_copied(dest, delta); |
| 430 | startDefer(); |
| 431 | tryUpdate(); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 432 | } |
| 433 | |
| 434 | void VNCServerST::setCursor(int width, int height, const Point& newHotspot, |
Pierre Ossman | 6a1a0d0 | 2017-02-19 15:48:17 +0100 | [diff] [blame] | 435 | const rdr::U8* data) |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 436 | { |
Pierre Ossman | 6a1a0d0 | 2017-02-19 15:48:17 +0100 | [diff] [blame] | 437 | delete cursor; |
| 438 | cursor = new Cursor(width, height, newHotspot, data); |
| 439 | cursor->crop(); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 440 | |
| 441 | renderedCursorInvalid = true; |
| 442 | |
| 443 | std::list<VNCSConnectionST*>::iterator ci, ci_next; |
| 444 | for (ci = clients.begin(); ci != clients.end(); ci = ci_next) { |
| 445 | ci_next = ci; ci_next++; |
| 446 | (*ci)->renderedCursorChange(); |
| 447 | (*ci)->setCursorOrClose(); |
| 448 | } |
| 449 | } |
| 450 | |
| 451 | void VNCServerST::setCursorPos(const Point& pos) |
| 452 | { |
| 453 | if (!cursorPos.equals(pos)) { |
| 454 | cursorPos = pos; |
| 455 | renderedCursorInvalid = true; |
| 456 | std::list<VNCSConnectionST*>::iterator ci; |
| 457 | for (ci = clients.begin(); ci != clients.end(); ci++) |
| 458 | (*ci)->renderedCursorChange(); |
| 459 | } |
| 460 | } |
| 461 | |
| 462 | // Other public methods |
| 463 | |
| 464 | void VNCServerST::approveConnection(network::Socket* sock, bool accept, |
| 465 | const char* reason) |
| 466 | { |
| 467 | std::list<VNCSConnectionST*>::iterator ci; |
| 468 | for (ci = clients.begin(); ci != clients.end(); ci++) { |
| 469 | if ((*ci)->getSock() == sock) { |
| 470 | (*ci)->approveConnectionOrClose(accept, reason); |
| 471 | return; |
| 472 | } |
| 473 | } |
| 474 | } |
| 475 | |
| 476 | void VNCServerST::closeClients(const char* reason, network::Socket* except) |
| 477 | { |
| 478 | std::list<VNCSConnectionST*>::iterator i, next_i; |
| 479 | for (i=clients.begin(); i!=clients.end(); i=next_i) { |
| 480 | next_i = i; next_i++; |
| 481 | if ((*i)->getSock() != except) |
| 482 | (*i)->close(reason); |
| 483 | } |
| 484 | } |
| 485 | |
| 486 | void VNCServerST::getSockets(std::list<network::Socket*>* sockets) |
| 487 | { |
| 488 | sockets->clear(); |
| 489 | std::list<VNCSConnectionST*>::iterator ci; |
| 490 | for (ci = clients.begin(); ci != clients.end(); ci++) { |
| 491 | sockets->push_back((*ci)->getSock()); |
| 492 | } |
| 493 | std::list<network::Socket*>::iterator si; |
| 494 | for (si = closingSockets.begin(); si != closingSockets.end(); si++) { |
| 495 | sockets->push_back(*si); |
| 496 | } |
| 497 | } |
| 498 | |
| 499 | SConnection* VNCServerST::getSConnection(network::Socket* sock) { |
| 500 | std::list<VNCSConnectionST*>::iterator ci; |
| 501 | for (ci = clients.begin(); ci != clients.end(); ci++) { |
| 502 | if ((*ci)->getSock() == sock) |
| 503 | return *ci; |
| 504 | } |
| 505 | return 0; |
| 506 | } |
| 507 | |
Pierre Ossman | bbf955e | 2011-11-08 12:44:10 +0000 | [diff] [blame] | 508 | bool VNCServerST::handleTimeout(Timer* t) |
| 509 | { |
| 510 | if (t != &deferTimer) |
| 511 | return false; |
| 512 | |
| 513 | tryUpdate(); |
| 514 | |
| 515 | return false; |
| 516 | } |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 517 | |
| 518 | // -=- Internal methods |
| 519 | |
| 520 | void VNCServerST::startDesktop() |
| 521 | { |
| 522 | if (!desktopStarted) { |
| 523 | slog.debug("starting desktop"); |
| 524 | desktop->start(this); |
| 525 | desktopStarted = true; |
| 526 | if (!pb) |
| 527 | throw Exception("SDesktop::start() did not set a valid PixelBuffer"); |
| 528 | } |
| 529 | } |
| 530 | |
| 531 | int VNCServerST::authClientCount() { |
| 532 | int count = 0; |
| 533 | std::list<VNCSConnectionST*>::iterator ci; |
| 534 | for (ci = clients.begin(); ci != clients.end(); ci++) { |
| 535 | if ((*ci)->authenticated()) |
| 536 | count++; |
| 537 | } |
| 538 | return count; |
| 539 | } |
| 540 | |
| 541 | inline bool VNCServerST::needRenderedCursor() |
| 542 | { |
| 543 | std::list<VNCSConnectionST*>::iterator ci; |
| 544 | for (ci = clients.begin(); ci != clients.end(); ci++) |
| 545 | if ((*ci)->needRenderedCursor()) return true; |
| 546 | return false; |
| 547 | } |
| 548 | |
Pierre Ossman | bbf955e | 2011-11-08 12:44:10 +0000 | [diff] [blame] | 549 | inline void VNCServerST::startDefer() |
| 550 | { |
| 551 | if (deferUpdateTime == 0) |
| 552 | return; |
| 553 | |
| 554 | if (deferPending && !alwaysSetDeferUpdateTimer) |
| 555 | return; |
| 556 | |
| 557 | gettimeofday(&deferStart, NULL); |
| 558 | deferTimer.start(deferUpdateTime); |
| 559 | |
| 560 | deferPending = true; |
| 561 | } |
| 562 | |
| 563 | inline bool VNCServerST::checkDefer() |
| 564 | { |
| 565 | if (!deferPending) |
| 566 | return true; |
| 567 | |
Pierre Ossman | 5c23b9e | 2015-03-03 16:26:03 +0100 | [diff] [blame] | 568 | if (msSince(&deferStart) >= (unsigned)deferUpdateTime) |
Pierre Ossman | bbf955e | 2011-11-08 12:44:10 +0000 | [diff] [blame] | 569 | return true; |
| 570 | |
| 571 | return false; |
| 572 | } |
| 573 | |
| 574 | void VNCServerST::tryUpdate() |
| 575 | { |
| 576 | std::list<VNCSConnectionST*>::iterator ci, ci_next; |
| 577 | |
Pierre Ossman | 559a2e8 | 2012-01-23 15:54:11 +0000 | [diff] [blame] | 578 | if (blockCounter > 0) |
| 579 | return; |
| 580 | |
Pierre Ossman | bbf955e | 2011-11-08 12:44:10 +0000 | [diff] [blame] | 581 | if (!checkDefer()) |
| 582 | return; |
| 583 | |
| 584 | for (ci = clients.begin(); ci != clients.end(); ci = ci_next) { |
| 585 | ci_next = ci; ci_next++; |
| 586 | (*ci)->writeFramebufferUpdateOrClose(); |
| 587 | } |
| 588 | } |
| 589 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 590 | // checkUpdate() is called just before sending an update. It checks to see |
| 591 | // what updates are pending and propagates them to the update tracker for each |
| 592 | // client. It uses the ComparingUpdateTracker's compare() method to filter out |
| 593 | // areas of the screen which haven't actually changed. It also checks the |
| 594 | // state of the (server-side) rendered cursor, if necessary rendering it again |
| 595 | // with the correct background. |
| 596 | |
Pierre Ossman | bbf955e | 2011-11-08 12:44:10 +0000 | [diff] [blame] | 597 | bool VNCServerST::checkUpdate() |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 598 | { |
Constantin Kaplinsky | 604d781 | 2007-08-31 15:50:37 +0000 | [diff] [blame] | 599 | UpdateInfo ui; |
| 600 | comparer->getUpdateInfo(&ui, pb->getRect()); |
| 601 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 602 | bool renderCursor = needRenderedCursor(); |
| 603 | |
Constantin Kaplinsky | 604d781 | 2007-08-31 15:50:37 +0000 | [diff] [blame] | 604 | if (ui.is_empty() && !(renderCursor && renderedCursorInvalid)) |
Pierre Ossman | bbf955e | 2011-11-08 12:44:10 +0000 | [diff] [blame] | 605 | return true; |
| 606 | |
Pierre Ossman | 559a2e8 | 2012-01-23 15:54:11 +0000 | [diff] [blame] | 607 | // Block clients as the frame buffer cannot be safely accessed |
| 608 | if (blockCounter > 0) |
| 609 | return false; |
| 610 | |
Pierre Ossman | bbf955e | 2011-11-08 12:44:10 +0000 | [diff] [blame] | 611 | // Block client from updating if we are currently deferring updates |
| 612 | if (!checkDefer()) |
| 613 | return false; |
| 614 | |
| 615 | deferPending = false; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 616 | |
Pierre Ossman | 02e43d7 | 2009-03-05 11:57:11 +0000 | [diff] [blame] | 617 | Region toCheck = ui.changed.union_(ui.copied); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 618 | |
| 619 | if (renderCursor) { |
Pierre Ossman | 6a1a0d0 | 2017-02-19 15:48:17 +0100 | [diff] [blame] | 620 | Rect clippedCursorRect = Rect(0, 0, cursor->width(), cursor->height()) |
| 621 | .translate(cursorPos.subtract(cursor->hotspot())) |
| 622 | .intersect(pb->getRect()); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 623 | |
| 624 | if (!renderedCursorInvalid && (toCheck.intersect(clippedCursorRect) |
| 625 | .is_empty())) { |
| 626 | renderCursor = false; |
| 627 | } else { |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 628 | toCheck.assign_union(clippedCursorRect); |
| 629 | } |
| 630 | } |
| 631 | |
| 632 | pb->grabRegion(toCheck); |
| 633 | |
Pierre Ossman | b114cec | 2011-11-20 15:36:11 +0000 | [diff] [blame] | 634 | if (getComparerState()) |
| 635 | comparer->enable(); |
| 636 | else |
| 637 | comparer->disable(); |
| 638 | |
| 639 | if (comparer->compare()) |
Constantin Kaplinsky | f0b3be7 | 2008-08-21 05:22:04 +0000 | [diff] [blame] | 640 | comparer->getUpdateInfo(&ui, pb->getRect()); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 641 | |
| 642 | if (renderCursor) { |
Pierre Ossman | 6a1a0d0 | 2017-02-19 15:48:17 +0100 | [diff] [blame] | 643 | renderedCursor.update(pb, cursor, cursorPos); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 644 | renderedCursorInvalid = false; |
| 645 | } |
| 646 | |
| 647 | std::list<VNCSConnectionST*>::iterator ci, ci_next; |
| 648 | for (ci = clients.begin(); ci != clients.end(); ci = ci_next) { |
| 649 | ci_next = ci; ci_next++; |
Constantin Kaplinsky | 604d781 | 2007-08-31 15:50:37 +0000 | [diff] [blame] | 650 | (*ci)->add_copied(ui.copied, ui.copy_delta); |
| 651 | (*ci)->add_changed(ui.changed); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 652 | } |
| 653 | |
| 654 | comparer->clear(); |
Pierre Ossman | bbf955e | 2011-11-08 12:44:10 +0000 | [diff] [blame] | 655 | |
| 656 | return true; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 657 | } |
| 658 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 659 | void VNCServerST::getConnInfo(ListConnInfo * listConn) |
| 660 | { |
| 661 | listConn->Clear(); |
| 662 | listConn->setDisable(getDisable()); |
| 663 | if (clients.empty()) |
| 664 | return; |
| 665 | std::list<VNCSConnectionST*>::iterator i; |
| 666 | for (i = clients.begin(); i != clients.end(); i++) |
| 667 | listConn->addInfo((void*)(*i), (*i)->getSock()->getPeerAddress(), |
| 668 | (*i)->getStartTime(), (*i)->getStatus()); |
| 669 | } |
| 670 | |
| 671 | void VNCServerST::setConnStatus(ListConnInfo* listConn) |
| 672 | { |
| 673 | setDisable(listConn->getDisable()); |
| 674 | if (listConn->Empty() || clients.empty()) return; |
| 675 | for (listConn->iBegin(); !listConn->iEnd(); listConn->iNext()) { |
| 676 | VNCSConnectionST* conn = (VNCSConnectionST*)listConn->iGetConn(); |
| 677 | std::list<VNCSConnectionST*>::iterator i; |
| 678 | for (i = clients.begin(); i != clients.end(); i++) { |
| 679 | if ((*i) == conn) { |
| 680 | int status = listConn->iGetStatus(); |
| 681 | if (status == 3) { |
| 682 | (*i)->close(0); |
| 683 | } else { |
| 684 | (*i)->setStatus(status); |
| 685 | } |
| 686 | break; |
| 687 | } |
| 688 | } |
| 689 | } |
| 690 | } |
Constantin Kaplinsky | 9d1fc6c | 2008-06-14 05:23:10 +0000 | [diff] [blame] | 691 | |
Pierre Ossman | 04e62db | 2009-03-23 16:57:07 +0000 | [diff] [blame] | 692 | void VNCServerST::notifyScreenLayoutChange(VNCSConnectionST* requester) |
| 693 | { |
| 694 | std::list<VNCSConnectionST*>::iterator ci, ci_next; |
| 695 | for (ci=clients.begin();ci!=clients.end();ci=ci_next) { |
| 696 | ci_next = ci; ci_next++; |
| 697 | if ((*ci) == requester) |
| 698 | continue; |
Pierre Ossman | a3ac01e | 2011-11-07 21:13:54 +0000 | [diff] [blame] | 699 | (*ci)->screenLayoutChangeOrClose(reasonOtherClient); |
Pierre Ossman | 04e62db | 2009-03-23 16:57:07 +0000 | [diff] [blame] | 700 | } |
| 701 | } |
Pierre Ossman | b114cec | 2011-11-20 15:36:11 +0000 | [diff] [blame] | 702 | |
| 703 | bool VNCServerST::getComparerState() |
| 704 | { |
| 705 | if (rfb::Server::compareFB == 0) |
| 706 | return false; |
| 707 | if (rfb::Server::compareFB != 2) |
| 708 | return true; |
| 709 | |
| 710 | std::list<VNCSConnectionST*>::iterator ci, ci_next; |
| 711 | for (ci=clients.begin();ci!=clients.end();ci=ci_next) { |
| 712 | ci_next = ci; ci_next++; |
| 713 | if ((*ci)->getComparerState()) |
| 714 | return true; |
| 715 | } |
| 716 | return false; |
| 717 | } |