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); |
| 82 | virtual void setColourMapEntries(int firstColour=0, int nColours=0); |
| 83 | virtual void serverCutText(const char* str, int len); |
| 84 | virtual void add_changed(const Region ®ion); |
| 85 | virtual void add_copied(const Region &dest, const Point &delta); |
| 86 | virtual bool clientsReadyForUpdate(); |
| 87 | virtual void tryUpdate(); |
| 88 | virtual void setCursor(int width, int height, const Point& hotspot, |
| 89 | void* cursorData, void* mask); |
| 90 | virtual void setCursorPos(const Point& p); |
| 91 | virtual void setSSecurityFactory(SSecurityFactory* f) {securityFactory=f;} |
| 92 | |
| 93 | virtual void bell(); |
| 94 | |
| 95 | // - Close all currently-connected clients, by calling |
| 96 | // their close() method with the supplied reason. |
| 97 | virtual void closeClients(const char* reason) {closeClients(reason, 0);} |
| 98 | |
| 99 | // VNCServerST-only methods |
| 100 | |
| 101 | // closeClients() closes all RFB sessions, except the specified one (if |
| 102 | // any), and logs the specified reason for closure. |
| 103 | void closeClients(const char* reason, network::Socket* sock); |
| 104 | |
| 105 | // getSockets() gets a list of sockets. This can be used to generate an |
| 106 | // fd_set for calling select(). |
| 107 | |
| 108 | void getSockets(std::list<network::Socket*>* sockets); |
| 109 | |
| 110 | // getSConnection() gets the SConnection for a particular Socket. If |
| 111 | // the Socket is not recognised then null is returned. |
| 112 | |
| 113 | SConnection* getSConnection(network::Socket* sock); |
| 114 | |
| 115 | // getDesktopSize() returns the size of the SDesktop exported by this |
| 116 | // server. |
| 117 | Point getDesktopSize() const {return desktop->getFbSize();} |
| 118 | |
| 119 | // getName() returns the name of this VNC Server. NB: The value returned |
| 120 | // is the server's internal buffer which may change after any other methods |
| 121 | // are called - take a copy if necessary. |
| 122 | const char* getName() const {return name.buf;} |
| 123 | |
| 124 | // setName() specifies the desktop name that the server should provide to |
| 125 | // clients |
| 126 | void setName(const char* name_) {name.replaceBuf(strDup(name_));} |
| 127 | |
| 128 | // A QueryConnectionHandler, if supplied, is passed details of incoming |
| 129 | // connections to approve, reject, or query the user about. |
| 130 | // |
| 131 | // queryConnection() is called when a connection has been |
| 132 | // successfully authenticated. The sock and userName arguments identify |
| 133 | // the socket and the name of the authenticated user, if any. It should |
| 134 | // return ACCEPT if the connection should be accepted, REJECT if it should |
| 135 | // be rejected, or PENDING if a decision cannot yet be reached. If REJECT |
| 136 | // is returned, *reason can be set to a string describing the reason - this |
| 137 | // will be delete[]ed when it is finished with. If PENDING is returned, |
| 138 | // approveConnection() must be called some time later to accept or reject |
| 139 | // the connection. |
| 140 | enum queryResult { ACCEPT, REJECT, PENDING }; |
| 141 | struct QueryConnectionHandler { |
| 142 | virtual ~QueryConnectionHandler() {} |
| 143 | virtual queryResult queryConnection(network::Socket* sock, |
| 144 | const char* userName, |
| 145 | char** reason) = 0; |
| 146 | }; |
| 147 | void setQueryConnectionHandler(QueryConnectionHandler* qch) { |
| 148 | queryConnectionHandler = qch; |
| 149 | } |
| 150 | |
| 151 | // queryConnection is called as described above, and either passes the |
| 152 | // request on to the registered handler, or accepts the connection if |
| 153 | // no handler has been specified. |
| 154 | virtual queryResult queryConnection(network::Socket* sock, |
| 155 | const char* userName, |
| 156 | char** reason) { |
| 157 | return queryConnectionHandler |
| 158 | ? queryConnectionHandler->queryConnection(sock, userName, reason) |
| 159 | : ACCEPT; |
| 160 | } |
| 161 | |
| 162 | // approveConnection() is called by the active QueryConnectionHandler, |
| 163 | // some time after queryConnection() has returned with PENDING, to accept |
| 164 | // or reject the connection. The accept argument should be true for |
| 165 | // acceptance, or false for rejection, in which case a string reason may |
| 166 | // also be given. |
| 167 | void approveConnection(network::Socket* sock, bool accept, |
| 168 | const char* reason); |
| 169 | |
| 170 | // setBlacklist() is called to replace the VNCServerST's internal |
| 171 | // Blacklist instance with another instance. This allows a single |
| 172 | // Blacklist to be shared by multiple VNCServerST instances. |
| 173 | void setBlacklist(Blacklist* bl) {blHosts = bl ? bl : &blacklist;} |
| 174 | |
| 175 | // setEconomicTranslate() determines (for new connections) whether pixels |
| 176 | // should be translated for <=16bpp clients using a large lookup table |
| 177 | // (fast) or separate, smaller R, G and B tables (slower). If set to true, |
| 178 | // small tables are used, to save memory. |
| 179 | void setEconomicTranslate(bool et) { useEconomicTranslate = et; } |
| 180 | |
| 181 | // setKeyRemapper() replaces the VNCServerST's default key remapper. |
| 182 | // NB: A null pointer is valid here. |
| 183 | void setKeyRemapper(KeyRemapper* kr) { keyRemapper = kr; } |
| 184 | |
| 185 | void getConnInfo(ListConnInfo * listConn); |
| 186 | void setConnStatus(ListConnInfo* listConn); |
| 187 | |
| 188 | bool getDisable() { return disableclients;}; |
| 189 | void setDisable(bool disable) { disableclients = disable;}; |
| 190 | |
| 191 | void setFTManager(rfb::SFileTransferManager *pFTManager) { m_pFTManager = pFTManager; }; |
| 192 | |
| 193 | protected: |
| 194 | |
| 195 | friend class VNCSConnectionST; |
| 196 | |
| 197 | void startDesktop(); |
| 198 | |
| 199 | static LogWriter connectionsLog; |
| 200 | Blacklist blacklist; |
| 201 | Blacklist* blHosts; |
| 202 | |
| 203 | SDesktop* desktop; |
| 204 | bool desktopStarted; |
| 205 | PixelBuffer* pb; |
| 206 | |
| 207 | SFileTransferManager *m_pFTManager; |
| 208 | |
| 209 | CharArray name; |
| 210 | |
| 211 | std::list<VNCSConnectionST*> clients; |
| 212 | VNCSConnectionST* pointerClient; |
| 213 | std::list<network::Socket*> closingSockets; |
| 214 | |
| 215 | ComparingUpdateTracker* comparer; |
| 216 | |
| 217 | Point cursorPos; |
| 218 | Cursor cursor; |
| 219 | Point cursorTL() { return cursorPos.subtract(cursor.hotspot); } |
| 220 | Point renderedCursorTL; |
| 221 | ManagedPixelBuffer renderedCursor; |
| 222 | bool renderedCursorInvalid; |
| 223 | |
| 224 | // - Check how many of the clients are authenticated. |
| 225 | int authClientCount(); |
| 226 | |
| 227 | bool needRenderedCursor(); |
| 228 | void checkUpdate(); |
| 229 | |
| 230 | SSecurityFactory* securityFactory; |
| 231 | QueryConnectionHandler* queryConnectionHandler; |
| 232 | KeyRemapper* keyRemapper; |
| 233 | bool useEconomicTranslate; |
| 234 | |
| 235 | time_t lastUserInputTime; |
| 236 | time_t lastDisconnectTime; |
| 237 | time_t lastConnectionTime; |
| 238 | |
| 239 | bool disableclients; |
| 240 | }; |
| 241 | |
| 242 | }; |
| 243 | |
| 244 | #endif |
| 245 | |