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