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