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 | // -=- VNCServerST.h |
| 20 | |
| 21 | // Single-threaded VNCServer implementation |
| 22 | |
| 23 | #ifndef __RFB_VNCSERVERST_H__ |
| 24 | #define __RFB_VNCSERVERST_H__ |
| 25 | |
| 26 | #include <list> |
| 27 | |
| 28 | #include <rfb/SDesktop.h> |
| 29 | #include <rfb/VNCServer.h> |
| 30 | #include <rfb/Configuration.h> |
| 31 | #include <rfb/LogWriter.h> |
| 32 | #include <rfb/Blacklist.h> |
| 33 | #include <rfb/Cursor.h> |
| 34 | #include <network/Socket.h> |
| 35 | #include <rfb/ListConnInfo.h> |
| 36 | #include <rfb/SFileTransferManager.h> |
| 37 | |
| 38 | namespace rfb { |
| 39 | |
| 40 | class VNCSConnectionST; |
| 41 | class ComparingUpdateTracker; |
| 42 | class PixelBuffer; |
| 43 | class KeyRemapper; |
| 44 | |
| 45 | class VNCServerST : public VNCServer, public network::SocketServer { |
| 46 | public: |
| 47 | // -=- Constructors |
| 48 | |
| 49 | // Create a server exporting the supplied desktop. |
| 50 | VNCServerST(const char* name_, SDesktop* desktop_, |
| 51 | SSecurityFactory* securityFactory_=0); |
| 52 | virtual ~VNCServerST(); |
| 53 | |
| 54 | |
| 55 | // Methods overridden from SocketServer |
| 56 | |
| 57 | // addSocket |
| 58 | // Causes the server to allocate an RFB-protocol management |
| 59 | // structure for the socket & initialise it. |
| 60 | virtual void addSocket(network::Socket* sock, bool outgoing=false); |
| 61 | |
| 62 | // removeSocket |
| 63 | // Clean up any resources associated with the Socket |
| 64 | virtual void removeSocket(network::Socket* sock); |
| 65 | |
| 66 | // processSocketEvent |
| 67 | // Read more RFB data from the Socket. If an error occurs during |
| 68 | // processing then shutdown() is called on the Socket, causing |
| 69 | // removeSocket() to be called by the caller at a later time. |
| 70 | virtual void processSocketEvent(network::Socket* sock); |
| 71 | |
| 72 | // checkTimeouts |
| 73 | // Returns the number of milliseconds left until the next idle timeout |
| 74 | // expires. If any have already expired, the corresponding connections |
| 75 | // are closed. Zero is returned if there is no idle timeout. |
| 76 | virtual int checkTimeouts(); |
| 77 | |
| 78 | |
| 79 | // Methods overridden from VNCServer |
| 80 | |
| 81 | virtual void setPixelBuffer(PixelBuffer* pb); |
Constantin Kaplinsky | d7c0a6d | 2007-09-03 04:23:48 +0000 | [diff] [blame] | 82 | virtual PixelBuffer* getPixelBuffer() const { return pb; } |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 83 | virtual void setColourMapEntries(int firstColour=0, int nColours=0); |
| 84 | virtual void serverCutText(const char* str, int len); |
| 85 | virtual void add_changed(const Region ®ion); |
| 86 | virtual void add_copied(const Region &dest, const Point &delta); |
Constantin Kaplinsky | 1a845d0 | 2007-08-31 21:06:53 +0000 | [diff] [blame] | 87 | virtual void set_video_area(const Rect &rect); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 88 | virtual bool clientsReadyForUpdate(); |
| 89 | virtual void tryUpdate(); |
| 90 | virtual void setCursor(int width, int height, const Point& hotspot, |
| 91 | void* cursorData, void* mask); |
| 92 | virtual void setCursorPos(const Point& p); |
| 93 | virtual void setSSecurityFactory(SSecurityFactory* f) {securityFactory=f;} |
| 94 | |
| 95 | virtual void bell(); |
| 96 | |
| 97 | // - Close all currently-connected clients, by calling |
| 98 | // their close() method with the supplied reason. |
| 99 | virtual void closeClients(const char* reason) {closeClients(reason, 0);} |
| 100 | |
| 101 | // VNCServerST-only methods |
| 102 | |
| 103 | // closeClients() closes all RFB sessions, except the specified one (if |
| 104 | // any), and logs the specified reason for closure. |
| 105 | void closeClients(const char* reason, network::Socket* sock); |
| 106 | |
| 107 | // getSockets() gets a list of sockets. This can be used to generate an |
| 108 | // fd_set for calling select(). |
| 109 | |
| 110 | void getSockets(std::list<network::Socket*>* sockets); |
| 111 | |
| 112 | // getSConnection() gets the SConnection for a particular Socket. If |
| 113 | // the Socket is not recognised then null is returned. |
| 114 | |
| 115 | SConnection* getSConnection(network::Socket* sock); |
| 116 | |
| 117 | // getDesktopSize() returns the size of the SDesktop exported by this |
| 118 | // server. |
| 119 | Point getDesktopSize() const {return desktop->getFbSize();} |
| 120 | |
| 121 | // getName() returns the name of this VNC Server. NB: The value returned |
| 122 | // is the server's internal buffer which may change after any other methods |
| 123 | // are called - take a copy if necessary. |
| 124 | const char* getName() const {return name.buf;} |
| 125 | |
| 126 | // setName() specifies the desktop name that the server should provide to |
| 127 | // clients |
| 128 | void setName(const char* name_) {name.replaceBuf(strDup(name_));} |
| 129 | |
| 130 | // A QueryConnectionHandler, if supplied, is passed details of incoming |
| 131 | // connections to approve, reject, or query the user about. |
| 132 | // |
| 133 | // queryConnection() is called when a connection has been |
| 134 | // successfully authenticated. The sock and userName arguments identify |
| 135 | // the socket and the name of the authenticated user, if any. It should |
| 136 | // return ACCEPT if the connection should be accepted, REJECT if it should |
| 137 | // be rejected, or PENDING if a decision cannot yet be reached. If REJECT |
| 138 | // is returned, *reason can be set to a string describing the reason - this |
| 139 | // will be delete[]ed when it is finished with. If PENDING is returned, |
| 140 | // approveConnection() must be called some time later to accept or reject |
| 141 | // the connection. |
| 142 | enum queryResult { ACCEPT, REJECT, PENDING }; |
| 143 | struct QueryConnectionHandler { |
| 144 | virtual ~QueryConnectionHandler() {} |
| 145 | virtual queryResult queryConnection(network::Socket* sock, |
| 146 | const char* userName, |
| 147 | char** reason) = 0; |
| 148 | }; |
| 149 | void setQueryConnectionHandler(QueryConnectionHandler* qch) { |
| 150 | queryConnectionHandler = qch; |
| 151 | } |
| 152 | |
| 153 | // queryConnection is called as described above, and either passes the |
| 154 | // request on to the registered handler, or accepts the connection if |
| 155 | // no handler has been specified. |
| 156 | virtual queryResult queryConnection(network::Socket* sock, |
| 157 | const char* userName, |
| 158 | char** reason) { |
| 159 | return queryConnectionHandler |
| 160 | ? queryConnectionHandler->queryConnection(sock, userName, reason) |
| 161 | : ACCEPT; |
| 162 | } |
| 163 | |
| 164 | // approveConnection() is called by the active QueryConnectionHandler, |
| 165 | // some time after queryConnection() has returned with PENDING, to accept |
| 166 | // or reject the connection. The accept argument should be true for |
| 167 | // acceptance, or false for rejection, in which case a string reason may |
| 168 | // also be given. |
| 169 | void approveConnection(network::Socket* sock, bool accept, |
| 170 | const char* reason); |
| 171 | |
| 172 | // setBlacklist() is called to replace the VNCServerST's internal |
| 173 | // Blacklist instance with another instance. This allows a single |
| 174 | // Blacklist to be shared by multiple VNCServerST instances. |
| 175 | void setBlacklist(Blacklist* bl) {blHosts = bl ? bl : &blacklist;} |
| 176 | |
| 177 | // setEconomicTranslate() determines (for new connections) whether pixels |
| 178 | // should be translated for <=16bpp clients using a large lookup table |
| 179 | // (fast) or separate, smaller R, G and B tables (slower). If set to true, |
| 180 | // small tables are used, to save memory. |
| 181 | void setEconomicTranslate(bool et) { useEconomicTranslate = et; } |
| 182 | |
| 183 | // setKeyRemapper() replaces the VNCServerST's default key remapper. |
| 184 | // NB: A null pointer is valid here. |
| 185 | void setKeyRemapper(KeyRemapper* kr) { keyRemapper = kr; } |
| 186 | |
| 187 | void getConnInfo(ListConnInfo * listConn); |
| 188 | void setConnStatus(ListConnInfo* listConn); |
| 189 | |
| 190 | bool getDisable() { return disableclients;}; |
| 191 | void setDisable(bool disable) { disableclients = disable;}; |
| 192 | |
| 193 | void setFTManager(rfb::SFileTransferManager *pFTManager) { m_pFTManager = pFTManager; }; |
| 194 | |
Constantin Kaplinsky | 8232831 | 2008-04-24 08:44:24 +0000 | [diff] [blame] | 195 | // Enable/disable support for TightVNC-specific VideoRectangleSelection |
| 196 | // client message. This is a protocol option that lets a client select a |
| 197 | // rectangle to be treated by the server as video data. Once selected, this |
| 198 | // part of the framebuffer will be sent using JpegEncoder, on each update |
| 199 | // request, as we expect that video data is changing continuously. By |
| 200 | // default, this option is disabled, as it's rather a specialized feature |
| 201 | // and video selection GUI can confuse users of the TightVNC client. |
Constantin Kaplinsky | 3ce6a72 | 2008-09-05 02:26:35 +0000 | [diff] [blame] | 202 | void enableVideoSelection(bool enable); |
| 203 | bool isVideoSelectionEnabled() const; |
Constantin Kaplinsky | 8232831 | 2008-04-24 08:44:24 +0000 | [diff] [blame] | 204 | |
Constantin Kaplinsky | 9d1fc6c | 2008-06-14 05:23:10 +0000 | [diff] [blame] | 205 | void setVideoRectangle(const Rect& r); |
Constantin Kaplinsky | 3ce6a72 | 2008-09-05 02:26:35 +0000 | [diff] [blame] | 206 | void setDefaultVideoRectangle(const Rect& r); |
Constantin Kaplinsky | c341ac6 | 2008-08-21 03:35:08 +0000 | [diff] [blame] | 207 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 208 | protected: |
| 209 | |
| 210 | friend class VNCSConnectionST; |
| 211 | |
| 212 | void startDesktop(); |
| 213 | |
| 214 | static LogWriter connectionsLog; |
| 215 | Blacklist blacklist; |
| 216 | Blacklist* blHosts; |
| 217 | |
| 218 | SDesktop* desktop; |
| 219 | bool desktopStarted; |
| 220 | PixelBuffer* pb; |
| 221 | |
| 222 | SFileTransferManager *m_pFTManager; |
| 223 | |
| 224 | CharArray name; |
| 225 | |
| 226 | std::list<VNCSConnectionST*> clients; |
| 227 | VNCSConnectionST* pointerClient; |
| 228 | std::list<network::Socket*> closingSockets; |
| 229 | |
| 230 | ComparingUpdateTracker* comparer; |
| 231 | |
| 232 | Point cursorPos; |
| 233 | Cursor cursor; |
| 234 | Point cursorTL() { return cursorPos.subtract(cursor.hotspot); } |
| 235 | Point renderedCursorTL; |
| 236 | ManagedPixelBuffer renderedCursor; |
| 237 | bool renderedCursorInvalid; |
| 238 | |
| 239 | // - Check how many of the clients are authenticated. |
| 240 | int authClientCount(); |
| 241 | |
| 242 | bool needRenderedCursor(); |
| 243 | void checkUpdate(); |
Constantin Kaplinsky | 6970b2d | 2008-06-13 18:07:53 +0000 | [diff] [blame] | 244 | void checkVideoUpdate(); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 245 | |
| 246 | SSecurityFactory* securityFactory; |
| 247 | QueryConnectionHandler* queryConnectionHandler; |
| 248 | KeyRemapper* keyRemapper; |
| 249 | bool useEconomicTranslate; |
| 250 | |
| 251 | time_t lastUserInputTime; |
| 252 | time_t lastDisconnectTime; |
| 253 | time_t lastConnectionTime; |
| 254 | |
| 255 | bool disableclients; |
Constantin Kaplinsky | 8232831 | 2008-04-24 08:44:24 +0000 | [diff] [blame] | 256 | |
| 257 | bool m_videoSelectionEnabled; |
Constantin Kaplinsky | 9d1fc6c | 2008-06-14 05:23:10 +0000 | [diff] [blame] | 258 | Rect m_videoRect; |
Constantin Kaplinsky | c341ac6 | 2008-08-21 03:35:08 +0000 | [diff] [blame] | 259 | Rect m_defaultVideoRect; |
Constantin Kaplinsky | 3ce6a72 | 2008-09-05 02:26:35 +0000 | [diff] [blame] | 260 | |
| 261 | void applyVideoRectangle(); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 262 | }; |
| 263 | |
| 264 | }; |
| 265 | |
| 266 | #endif |
| 267 | |