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