Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 1 | /* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved. |
| 2 | * |
| 3 | * This is free software; you can redistribute it and/or modify |
| 4 | * it under the terms of the GNU General Public License as published by |
| 5 | * the Free Software Foundation; either version 2 of the License, or |
| 6 | * (at your option) any later version. |
| 7 | * |
| 8 | * This software is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | * GNU General Public License for more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU General Public License |
| 14 | * along with this software; if not, write to the Free Software |
| 15 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
| 16 | * USA. |
| 17 | */ |
| 18 | |
| 19 | // -=- Single-Threaded VNC Server implementation |
| 20 | |
| 21 | |
| 22 | // Note about how sockets get closed: |
| 23 | // |
| 24 | // Closing sockets to clients is non-trivial because the code which calls |
| 25 | // VNCServerST must explicitly know about all the sockets (so that it can block |
| 26 | // on them appropriately). However, VNCServerST may want to close clients for |
| 27 | // a number of reasons, and from a variety of entry points. The simplest is |
| 28 | // when processSocketEvent() is called for a client, and the remote end has |
| 29 | // closed its socket. A more complex reason is when processSocketEvent() is |
| 30 | // called for a client which has just sent a ClientInit with the shared flag |
| 31 | // set to false - in this case we want to close all other clients. Yet another |
| 32 | // reason for disconnecting clients is when the desktop size has changed as a |
| 33 | // result of a call to setPixelBuffer(). |
| 34 | // |
| 35 | // The responsibility for creating and deleting sockets is entirely with the |
| 36 | // calling code. When VNCServerST wants to close a connection to a client it |
| 37 | // calls the VNCSConnectionST's close() method which calls shutdown() on the |
| 38 | // socket. Eventually the calling code will notice that the socket has been |
| 39 | // shut down and call removeSocket() so that we can delete the |
| 40 | // VNCSConnectionST. Note that the socket must not be deleted by the calling |
| 41 | // code until after removeSocket() has been called. |
| 42 | // |
| 43 | // One minor complication is that we don't allocate a VNCSConnectionST object |
| 44 | // for a blacklisted host (since we want to minimise the resources used for |
| 45 | // dealing with such a connection). In order to properly implement the |
| 46 | // getSockets function, we must maintain a separate closingSockets list, |
| 47 | // otherwise blacklisted connections might be "forgotten". |
| 48 | |
| 49 | |
| 50 | #include <rfb/ServerCore.h> |
| 51 | #include <rfb/VNCServerST.h> |
| 52 | #include <rfb/VNCSConnectionST.h> |
| 53 | #include <rfb/ComparingUpdateTracker.h> |
| 54 | #include <rfb/SSecurityFactoryStandard.h> |
| 55 | #include <rfb/KeyRemapper.h> |
| 56 | #include <rfb/util.h> |
| 57 | |
| 58 | #include <rdr/types.h> |
| 59 | |
| 60 | using namespace rfb; |
| 61 | |
| 62 | static LogWriter slog("VNCServerST"); |
| 63 | LogWriter VNCServerST::connectionsLog("Connections"); |
| 64 | static SSecurityFactoryStandard defaultSecurityFactory; |
| 65 | |
| 66 | // |
| 67 | // -=- VNCServerST Implementation |
| 68 | // |
| 69 | |
| 70 | // -=- Constructors/Destructor |
| 71 | |
| 72 | VNCServerST::VNCServerST(const char* name_, SDesktop* desktop_, |
| 73 | SSecurityFactory* sf) |
| 74 | : blHosts(&blacklist), desktop(desktop_), desktopStarted(false), pb(0), |
| 75 | m_pFTManager(0), name(strDup(name_)), pointerClient(0), comparer(0), |
| 76 | renderedCursorInvalid(false), |
| 77 | securityFactory(sf ? sf : &defaultSecurityFactory), |
| 78 | queryConnectionHandler(0), keyRemapper(&KeyRemapper::defInstance), |
| 79 | useEconomicTranslate(false), |
| 80 | lastConnectionTime(0), disableclients(false) |
| 81 | { |
| 82 | lastUserInputTime = lastDisconnectTime = time(0); |
| 83 | slog.debug("creating single-threaded server %s", name.buf); |
| 84 | } |
| 85 | |
| 86 | VNCServerST::~VNCServerST() |
| 87 | { |
| 88 | slog.debug("shutting down server %s", name.buf); |
| 89 | |
| 90 | // Close any active clients, with appropriate logging & cleanup |
| 91 | closeClients("Server shutdown"); |
| 92 | |
| 93 | // Delete all the clients, and their sockets, and any closing sockets |
| 94 | // NB: Deleting a client implicitly removes it from the clients list |
| 95 | while (!clients.empty()) { |
| 96 | delete clients.front(); |
| 97 | } |
| 98 | |
| 99 | // Stop the desktop object if active, *only* after deleting all clients! |
| 100 | if (desktopStarted) { |
| 101 | desktopStarted = false; |
| 102 | desktop->stop(); |
| 103 | } |
| 104 | |
| 105 | delete comparer; |
| 106 | } |
| 107 | |
| 108 | |
| 109 | // SocketServer methods |
| 110 | |
| 111 | void VNCServerST::addSocket(network::Socket* sock, bool outgoing) |
| 112 | { |
| 113 | // - Check the connection isn't black-marked |
| 114 | // *** do this in getSecurity instead? |
| 115 | CharArray address(sock->getPeerAddress()); |
| 116 | if (blHosts->isBlackmarked(address.buf)) { |
| 117 | connectionsLog.error("blacklisted: %s", address.buf); |
| 118 | try { |
| 119 | SConnection::writeConnFailedFromScratch("Too many security failures", |
| 120 | &sock->outStream()); |
| 121 | } catch (rdr::Exception&) { |
| 122 | } |
| 123 | sock->shutdown(); |
| 124 | closingSockets.push_back(sock); |
| 125 | return; |
| 126 | } |
| 127 | |
| 128 | if (clients.empty()) { |
| 129 | lastConnectionTime = time(0); |
| 130 | } |
| 131 | |
| 132 | VNCSConnectionST* client = new VNCSConnectionST(this, sock, outgoing); |
| 133 | client->init(); |
| 134 | } |
| 135 | |
| 136 | void VNCServerST::removeSocket(network::Socket* sock) { |
| 137 | // - If the socket has resources allocated to it, delete them |
| 138 | std::list<VNCSConnectionST*>::iterator ci; |
| 139 | for (ci = clients.begin(); ci != clients.end(); ci++) { |
| 140 | if ((*ci)->getSock() == sock) { |
| 141 | // - Delete the per-Socket resources |
| 142 | delete *ci; |
| 143 | |
| 144 | // - Check that the desktop object is still required |
| 145 | if (authClientCount() == 0 && desktopStarted) { |
| 146 | slog.debug("no authenticated clients - stopping desktop"); |
| 147 | desktopStarted = false; |
| 148 | desktop->stop(); |
| 149 | } |
| 150 | return; |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | // - If the Socket has no resources, it may have been a closingSocket |
| 155 | closingSockets.remove(sock); |
| 156 | } |
| 157 | |
| 158 | void VNCServerST::processSocketEvent(network::Socket* sock) |
| 159 | { |
| 160 | // - Find the appropriate VNCSConnectionST and process the event |
| 161 | std::list<VNCSConnectionST*>::iterator ci; |
| 162 | for (ci = clients.begin(); ci != clients.end(); ci++) { |
| 163 | if ((*ci)->getSock() == sock) { |
| 164 | (*ci)->processMessages(); |
| 165 | return; |
| 166 | } |
| 167 | } |
| 168 | throw rdr::Exception("invalid Socket in VNCServerST"); |
| 169 | } |
| 170 | |
| 171 | int VNCServerST::checkTimeouts() |
| 172 | { |
| 173 | int timeout = 0; |
| 174 | std::list<VNCSConnectionST*>::iterator ci, ci_next; |
| 175 | for (ci=clients.begin();ci!=clients.end();ci=ci_next) { |
| 176 | ci_next = ci; ci_next++; |
| 177 | soonestTimeout(&timeout, (*ci)->checkIdleTimeout()); |
| 178 | } |
| 179 | |
| 180 | int timeLeft; |
| 181 | time_t now; |
| 182 | |
| 183 | // Optimization: Only call time() if using any maxTime. |
| 184 | if (rfb::Server::maxDisconnectionTime || rfb::Server::maxConnectionTime || rfb::Server::maxIdleTime) { |
| 185 | now = time(0); |
| 186 | } |
| 187 | |
| 188 | // Check MaxDisconnectionTime |
| 189 | if (rfb::Server::maxDisconnectionTime && clients.empty()) { |
| 190 | if (now < lastDisconnectTime) { |
| 191 | // Someone must have set the time backwards. |
| 192 | slog.info("Time has gone backwards - resetting lastDisconnectTime"); |
| 193 | lastDisconnectTime = now; |
| 194 | } |
| 195 | timeLeft = lastDisconnectTime + rfb::Server::maxDisconnectionTime - now; |
| 196 | if (timeLeft < -60) { |
| 197 | // Someone must have set the time forwards. |
| 198 | slog.info("Time has gone forwards - resetting lastDisconnectTime"); |
| 199 | lastDisconnectTime = now; |
| 200 | timeLeft = rfb::Server::maxDisconnectionTime; |
| 201 | } |
| 202 | if (timeLeft <= 0) { |
| 203 | slog.info("MaxDisconnectionTime reached, exiting"); |
| 204 | exit(0); |
| 205 | } |
| 206 | soonestTimeout(&timeout, timeLeft * 1000); |
| 207 | } |
| 208 | |
| 209 | // Check MaxConnectionTime |
| 210 | if (rfb::Server::maxConnectionTime && lastConnectionTime && !clients.empty()) { |
| 211 | if (now < lastConnectionTime) { |
| 212 | // Someone must have set the time backwards. |
| 213 | slog.info("Time has gone backwards - resetting lastConnectionTime"); |
| 214 | lastConnectionTime = now; |
| 215 | } |
| 216 | timeLeft = lastConnectionTime + rfb::Server::maxConnectionTime - now; |
| 217 | if (timeLeft < -60) { |
| 218 | // Someone must have set the time forwards. |
| 219 | slog.info("Time has gone forwards - resetting lastConnectionTime"); |
| 220 | lastConnectionTime = now; |
| 221 | timeLeft = rfb::Server::maxConnectionTime; |
| 222 | } |
| 223 | if (timeLeft <= 0) { |
| 224 | slog.info("MaxConnectionTime reached, exiting"); |
| 225 | exit(0); |
| 226 | } |
| 227 | soonestTimeout(&timeout, timeLeft * 1000); |
| 228 | } |
| 229 | |
| 230 | |
| 231 | // Check MaxIdleTime |
| 232 | if (rfb::Server::maxIdleTime) { |
| 233 | if (now < lastUserInputTime) { |
| 234 | // Someone must have set the time backwards. |
| 235 | slog.info("Time has gone backwards - resetting lastUserInputTime"); |
| 236 | lastUserInputTime = now; |
| 237 | } |
| 238 | timeLeft = lastUserInputTime + rfb::Server::maxIdleTime - now; |
| 239 | if (timeLeft < -60) { |
| 240 | // Someone must have set the time forwards. |
| 241 | slog.info("Time has gone forwards - resetting lastUserInputTime"); |
| 242 | lastUserInputTime = now; |
| 243 | timeLeft = rfb::Server::maxIdleTime; |
| 244 | } |
| 245 | if (timeLeft <= 0) { |
| 246 | slog.info("MaxIdleTime reached, exiting"); |
| 247 | exit(0); |
| 248 | } |
| 249 | soonestTimeout(&timeout, timeLeft * 1000); |
| 250 | } |
| 251 | |
| 252 | return timeout; |
| 253 | } |
| 254 | |
| 255 | |
| 256 | // VNCServer methods |
| 257 | |
| 258 | void VNCServerST::setPixelBuffer(PixelBuffer* pb_) |
| 259 | { |
| 260 | pb = pb_; |
| 261 | delete comparer; |
| 262 | comparer = 0; |
| 263 | |
| 264 | if (pb) { |
| 265 | comparer = new ComparingUpdateTracker(pb); |
| 266 | cursor.setPF(pb->getPF()); |
| 267 | renderedCursor.setPF(pb->getPF()); |
| 268 | |
| 269 | std::list<VNCSConnectionST*>::iterator ci, ci_next; |
| 270 | for (ci=clients.begin();ci!=clients.end();ci=ci_next) { |
| 271 | ci_next = ci; ci_next++; |
| 272 | (*ci)->pixelBufferChange(); |
| 273 | } |
| 274 | } else { |
| 275 | if (desktopStarted) |
| 276 | throw Exception("setPixelBuffer: null PixelBuffer when desktopStarted?"); |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | void VNCServerST::setColourMapEntries(int firstColour, int nColours) |
| 281 | { |
| 282 | std::list<VNCSConnectionST*>::iterator ci, ci_next; |
| 283 | for (ci = clients.begin(); ci != clients.end(); ci = ci_next) { |
| 284 | ci_next = ci; ci_next++; |
| 285 | (*ci)->setColourMapEntriesOrClose(firstColour, nColours); |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | void VNCServerST::bell() |
| 290 | { |
| 291 | std::list<VNCSConnectionST*>::iterator ci, ci_next; |
| 292 | for (ci = clients.begin(); ci != clients.end(); ci = ci_next) { |
| 293 | ci_next = ci; ci_next++; |
| 294 | (*ci)->bell(); |
| 295 | } |
| 296 | } |
| 297 | |
| 298 | void VNCServerST::serverCutText(const char* str, int len) |
| 299 | { |
| 300 | std::list<VNCSConnectionST*>::iterator ci, ci_next; |
| 301 | for (ci = clients.begin(); ci != clients.end(); ci = ci_next) { |
| 302 | ci_next = ci; ci_next++; |
| 303 | (*ci)->serverCutText(str, len); |
| 304 | } |
| 305 | } |
| 306 | |
| 307 | void VNCServerST::add_changed(const Region& region) |
| 308 | { |
| 309 | comparer->add_changed(region); |
| 310 | } |
| 311 | |
| 312 | void VNCServerST::add_copied(const Region& dest, const Point& delta) |
| 313 | { |
| 314 | comparer->add_copied(dest, delta); |
| 315 | } |
| 316 | |
| 317 | bool VNCServerST::clientsReadyForUpdate() |
| 318 | { |
| 319 | std::list<VNCSConnectionST*>::iterator ci; |
| 320 | for (ci = clients.begin(); ci != clients.end(); ci++) { |
| 321 | if ((*ci)->readyForUpdate()) |
| 322 | return true; |
| 323 | } |
| 324 | return false; |
| 325 | } |
| 326 | |
| 327 | void VNCServerST::tryUpdate() |
| 328 | { |
| 329 | std::list<VNCSConnectionST*>::iterator ci, ci_next; |
| 330 | for (ci = clients.begin(); ci != clients.end(); ci = ci_next) { |
| 331 | ci_next = ci; ci_next++; |
| 332 | (*ci)->writeFramebufferUpdateOrClose(); |
| 333 | } |
| 334 | } |
| 335 | |
| 336 | void VNCServerST::setCursor(int width, int height, const Point& newHotspot, |
| 337 | void* data, void* mask) |
| 338 | { |
| 339 | cursor.hotspot = newHotspot; |
| 340 | cursor.setSize(width, height); |
| 341 | memcpy(cursor.data, data, cursor.dataLen()); |
| 342 | memcpy(cursor.mask.buf, mask, cursor.maskLen()); |
| 343 | |
| 344 | cursor.crop(); |
| 345 | |
| 346 | renderedCursorInvalid = true; |
| 347 | |
| 348 | std::list<VNCSConnectionST*>::iterator ci, ci_next; |
| 349 | for (ci = clients.begin(); ci != clients.end(); ci = ci_next) { |
| 350 | ci_next = ci; ci_next++; |
| 351 | (*ci)->renderedCursorChange(); |
| 352 | (*ci)->setCursorOrClose(); |
| 353 | } |
| 354 | } |
| 355 | |
| 356 | void VNCServerST::setCursorPos(const Point& pos) |
| 357 | { |
| 358 | if (!cursorPos.equals(pos)) { |
| 359 | cursorPos = pos; |
| 360 | renderedCursorInvalid = true; |
| 361 | std::list<VNCSConnectionST*>::iterator ci; |
| 362 | for (ci = clients.begin(); ci != clients.end(); ci++) |
| 363 | (*ci)->renderedCursorChange(); |
| 364 | } |
| 365 | } |
| 366 | |
| 367 | // Other public methods |
| 368 | |
| 369 | void VNCServerST::approveConnection(network::Socket* sock, bool accept, |
| 370 | const char* reason) |
| 371 | { |
| 372 | std::list<VNCSConnectionST*>::iterator ci; |
| 373 | for (ci = clients.begin(); ci != clients.end(); ci++) { |
| 374 | if ((*ci)->getSock() == sock) { |
| 375 | (*ci)->approveConnectionOrClose(accept, reason); |
| 376 | return; |
| 377 | } |
| 378 | } |
| 379 | } |
| 380 | |
| 381 | void VNCServerST::closeClients(const char* reason, network::Socket* except) |
| 382 | { |
| 383 | std::list<VNCSConnectionST*>::iterator i, next_i; |
| 384 | for (i=clients.begin(); i!=clients.end(); i=next_i) { |
| 385 | next_i = i; next_i++; |
| 386 | if ((*i)->getSock() != except) |
| 387 | (*i)->close(reason); |
| 388 | } |
| 389 | } |
| 390 | |
| 391 | void VNCServerST::getSockets(std::list<network::Socket*>* sockets) |
| 392 | { |
| 393 | sockets->clear(); |
| 394 | std::list<VNCSConnectionST*>::iterator ci; |
| 395 | for (ci = clients.begin(); ci != clients.end(); ci++) { |
| 396 | sockets->push_back((*ci)->getSock()); |
| 397 | } |
| 398 | std::list<network::Socket*>::iterator si; |
| 399 | for (si = closingSockets.begin(); si != closingSockets.end(); si++) { |
| 400 | sockets->push_back(*si); |
| 401 | } |
| 402 | } |
| 403 | |
| 404 | SConnection* VNCServerST::getSConnection(network::Socket* sock) { |
| 405 | std::list<VNCSConnectionST*>::iterator ci; |
| 406 | for (ci = clients.begin(); ci != clients.end(); ci++) { |
| 407 | if ((*ci)->getSock() == sock) |
| 408 | return *ci; |
| 409 | } |
| 410 | return 0; |
| 411 | } |
| 412 | |
| 413 | |
| 414 | // -=- Internal methods |
| 415 | |
| 416 | void VNCServerST::startDesktop() |
| 417 | { |
| 418 | if (!desktopStarted) { |
| 419 | slog.debug("starting desktop"); |
| 420 | desktop->start(this); |
| 421 | desktopStarted = true; |
| 422 | if (!pb) |
| 423 | throw Exception("SDesktop::start() did not set a valid PixelBuffer"); |
| 424 | } |
| 425 | } |
| 426 | |
| 427 | int VNCServerST::authClientCount() { |
| 428 | int count = 0; |
| 429 | std::list<VNCSConnectionST*>::iterator ci; |
| 430 | for (ci = clients.begin(); ci != clients.end(); ci++) { |
| 431 | if ((*ci)->authenticated()) |
| 432 | count++; |
| 433 | } |
| 434 | return count; |
| 435 | } |
| 436 | |
| 437 | inline bool VNCServerST::needRenderedCursor() |
| 438 | { |
| 439 | std::list<VNCSConnectionST*>::iterator ci; |
| 440 | for (ci = clients.begin(); ci != clients.end(); ci++) |
| 441 | if ((*ci)->needRenderedCursor()) return true; |
| 442 | return false; |
| 443 | } |
| 444 | |
| 445 | // checkUpdate() is called just before sending an update. It checks to see |
| 446 | // what updates are pending and propagates them to the update tracker for each |
| 447 | // client. It uses the ComparingUpdateTracker's compare() method to filter out |
| 448 | // areas of the screen which haven't actually changed. It also checks the |
| 449 | // state of the (server-side) rendered cursor, if necessary rendering it again |
| 450 | // with the correct background. |
| 451 | |
| 452 | void VNCServerST::checkUpdate() |
| 453 | { |
| 454 | bool renderCursor = needRenderedCursor(); |
| 455 | |
| 456 | if (comparer->is_empty() && !(renderCursor && renderedCursorInvalid)) |
| 457 | return; |
| 458 | |
| 459 | Region toCheck = comparer->get_changed().union_(comparer->get_copied()); |
| 460 | |
| 461 | if (renderCursor) { |
| 462 | Rect clippedCursorRect |
| 463 | = cursor.getRect(cursorTL()).intersect(pb->getRect()); |
| 464 | |
| 465 | if (!renderedCursorInvalid && (toCheck.intersect(clippedCursorRect) |
| 466 | .is_empty())) { |
| 467 | renderCursor = false; |
| 468 | } else { |
| 469 | renderedCursorTL = clippedCursorRect.tl; |
| 470 | renderedCursor.setSize(clippedCursorRect.width(), |
| 471 | clippedCursorRect.height()); |
| 472 | toCheck.assign_union(clippedCursorRect); |
| 473 | } |
| 474 | } |
| 475 | |
| 476 | pb->grabRegion(toCheck); |
| 477 | |
| 478 | if (rfb::Server::compareFB) |
| 479 | comparer->compare(); |
| 480 | |
| 481 | if (renderCursor) { |
| 482 | pb->getImage(renderedCursor.data, |
| 483 | renderedCursor.getRect(renderedCursorTL)); |
| 484 | renderedCursor.maskRect(cursor.getRect(cursorTL() |
| 485 | .subtract(renderedCursorTL)), |
| 486 | cursor.data, cursor.mask.buf); |
| 487 | renderedCursorInvalid = false; |
| 488 | } |
| 489 | |
| 490 | std::list<VNCSConnectionST*>::iterator ci, ci_next; |
| 491 | for (ci = clients.begin(); ci != clients.end(); ci = ci_next) { |
| 492 | ci_next = ci; ci_next++; |
| 493 | (*ci)->add_copied(comparer->get_copied(), comparer->get_delta()); |
| 494 | (*ci)->add_changed(comparer->get_changed()); |
| 495 | } |
| 496 | |
| 497 | comparer->clear(); |
| 498 | } |
| 499 | |
| 500 | void VNCServerST::getConnInfo(ListConnInfo * listConn) |
| 501 | { |
| 502 | listConn->Clear(); |
| 503 | listConn->setDisable(getDisable()); |
| 504 | if (clients.empty()) |
| 505 | return; |
| 506 | std::list<VNCSConnectionST*>::iterator i; |
| 507 | for (i = clients.begin(); i != clients.end(); i++) |
| 508 | listConn->addInfo((void*)(*i), (*i)->getSock()->getPeerAddress(), |
| 509 | (*i)->getStartTime(), (*i)->getStatus()); |
| 510 | } |
| 511 | |
| 512 | void VNCServerST::setConnStatus(ListConnInfo* listConn) |
| 513 | { |
| 514 | setDisable(listConn->getDisable()); |
| 515 | if (listConn->Empty() || clients.empty()) return; |
| 516 | for (listConn->iBegin(); !listConn->iEnd(); listConn->iNext()) { |
| 517 | VNCSConnectionST* conn = (VNCSConnectionST*)listConn->iGetConn(); |
| 518 | std::list<VNCSConnectionST*>::iterator i; |
| 519 | for (i = clients.begin(); i != clients.end(); i++) { |
| 520 | if ((*i) == conn) { |
| 521 | int status = listConn->iGetStatus(); |
| 522 | if (status == 3) { |
| 523 | (*i)->close(0); |
| 524 | } else { |
| 525 | (*i)->setStatus(status); |
| 526 | } |
| 527 | break; |
| 528 | } |
| 529 | } |
| 530 | } |
| 531 | } |