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/Blacklist.h> |
| 32 | #include <rfb/Cursor.h> |
Pierre Ossman | bbf955e | 2011-11-08 12:44:10 +0000 | [diff] [blame] | 33 | #include <rfb/Timer.h> |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 34 | #include <network/Socket.h> |
Pierre Ossman | 34e62f3 | 2009-03-20 21:46:12 +0000 | [diff] [blame] | 35 | #include <rfb/ScreenSet.h> |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 36 | |
| 37 | namespace rfb { |
| 38 | |
| 39 | class VNCSConnectionST; |
| 40 | class ComparingUpdateTracker; |
Pierre Ossman | 707fa12 | 2015-12-11 20:21:20 +0100 | [diff] [blame] | 41 | class ListConnInfo; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 42 | class PixelBuffer; |
| 43 | class KeyRemapper; |
| 44 | |
Pierre Ossman | bbf955e | 2011-11-08 12:44:10 +0000 | [diff] [blame] | 45 | class VNCServerST : public VNCServer, |
| 46 | public Timer::Callback, |
| 47 | public network::SocketServer { |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 48 | public: |
| 49 | // -=- Constructors |
| 50 | |
| 51 | // Create a server exporting the supplied desktop. |
Adam Tkac | a6578bf | 2010-04-23 14:07:41 +0000 | [diff] [blame] | 52 | VNCServerST(const char* name_, SDesktop* desktop_); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 53 | virtual ~VNCServerST(); |
| 54 | |
| 55 | |
| 56 | // Methods overridden from SocketServer |
| 57 | |
| 58 | // addSocket |
| 59 | // Causes the server to allocate an RFB-protocol management |
| 60 | // structure for the socket & initialise it. |
| 61 | virtual void addSocket(network::Socket* sock, bool outgoing=false); |
| 62 | |
| 63 | // removeSocket |
| 64 | // Clean up any resources associated with the Socket |
| 65 | virtual void removeSocket(network::Socket* sock); |
| 66 | |
Pierre Ossman | 574dc64 | 2016-10-05 13:39:11 +0200 | [diff] [blame] | 67 | // getSockets() gets a list of sockets. This can be used to generate an |
| 68 | // fd_set for calling select(). |
| 69 | virtual void getSockets(std::list<network::Socket*>* sockets); |
| 70 | |
Pierre Ossman | d408ca5 | 2016-04-29 14:26:05 +0200 | [diff] [blame] | 71 | // processSocketReadEvent |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 72 | // Read more RFB data from the Socket. If an error occurs during |
| 73 | // processing then shutdown() is called on the Socket, causing |
| 74 | // removeSocket() to be called by the caller at a later time. |
Pierre Ossman | d408ca5 | 2016-04-29 14:26:05 +0200 | [diff] [blame] | 75 | virtual void processSocketReadEvent(network::Socket* sock); |
| 76 | |
| 77 | // processSocketWriteEvent |
| 78 | // Flush pending data from the Socket on to the network. |
| 79 | virtual void processSocketWriteEvent(network::Socket* sock); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 80 | |
| 81 | // checkTimeouts |
| 82 | // Returns the number of milliseconds left until the next idle timeout |
| 83 | // expires. If any have already expired, the corresponding connections |
| 84 | // are closed. Zero is returned if there is no idle timeout. |
| 85 | virtual int checkTimeouts(); |
| 86 | |
| 87 | |
| 88 | // Methods overridden from VNCServer |
| 89 | |
Pierre Ossman | 559a2e8 | 2012-01-23 15:54:11 +0000 | [diff] [blame] | 90 | virtual void blockUpdates(); |
| 91 | virtual void unblockUpdates(); |
Pierre Ossman | 04e62db | 2009-03-23 16:57:07 +0000 | [diff] [blame] | 92 | virtual void setPixelBuffer(PixelBuffer* pb, const ScreenSet& layout); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 93 | virtual void setPixelBuffer(PixelBuffer* pb); |
Pierre Ossman | 04e62db | 2009-03-23 16:57:07 +0000 | [diff] [blame] | 94 | virtual void setScreenLayout(const ScreenSet& layout); |
Pierre Ossman | 6094ced | 2018-10-05 17:24:51 +0200 | [diff] [blame] | 95 | virtual const PixelBuffer* getPixelBuffer() const { return pb; } |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 96 | virtual void serverCutText(const char* str, int len); |
Pierre Ossman | eef6c9a | 2018-10-05 17:11:25 +0200 | [diff] [blame] | 97 | |
| 98 | virtual void approveConnection(network::Socket* sock, bool accept, |
| 99 | const char* reason); |
| 100 | virtual void closeClients(const char* reason) {closeClients(reason, 0);} |
Pierre Ossman | 7d64b33 | 2018-10-08 15:59:02 +0200 | [diff] [blame] | 101 | virtual SConnection* getConnection(network::Socket* sock); |
Pierre Ossman | eef6c9a | 2018-10-05 17:11:25 +0200 | [diff] [blame] | 102 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 103 | virtual void add_changed(const Region ®ion); |
| 104 | virtual void add_copied(const Region &dest, const Point &delta); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 105 | virtual void setCursor(int width, int height, const Point& hotspot, |
Pierre Ossman | 6a1a0d0 | 2017-02-19 15:48:17 +0100 | [diff] [blame] | 106 | const rdr::U8* data); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 107 | virtual void setCursorPos(const Point& p); |
Pierre Ossman | 6094ced | 2018-10-05 17:24:51 +0200 | [diff] [blame] | 108 | virtual void setName(const char* name_); |
Pierre Ossman | bb305ca | 2016-12-11 12:41:26 +0100 | [diff] [blame] | 109 | virtual void setLEDState(unsigned state); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 110 | |
| 111 | virtual void bell(); |
| 112 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 113 | // VNCServerST-only methods |
| 114 | |
Pierre Ossman | 6094ced | 2018-10-05 17:24:51 +0200 | [diff] [blame] | 115 | // Methods to get the currently set server state |
| 116 | |
| 117 | const ScreenSet& getScreenLayout() const { return screenLayout; } |
| 118 | const Cursor* getCursor() const { return cursor; } |
| 119 | const Point& getCursorPos() const { return cursorPos; } |
| 120 | const char* getName() const { return name.buf; } |
| 121 | unsigned getLEDState() const { return ledState; } |
| 122 | |
Pierre Ossman | b684341 | 2018-10-05 17:30:52 +0200 | [diff] [blame] | 123 | // Event handlers |
| 124 | void keyEvent(rdr::U32 keysym, rdr::U32 keycode, bool down); |
| 125 | void pointerEvent(VNCSConnectionST* client, const Point& pos, int buttonMask); |
| 126 | void clientCutText(const char* str, int len); |
| 127 | |
Pierre Ossman | 07e44cc | 2018-10-05 17:32:57 +0200 | [diff] [blame] | 128 | unsigned int setDesktopSize(VNCSConnectionST* requester, |
| 129 | int fb_width, int fb_height, |
| 130 | const ScreenSet& layout); |
| 131 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 132 | // closeClients() closes all RFB sessions, except the specified one (if |
| 133 | // any), and logs the specified reason for closure. |
| 134 | void closeClients(const char* reason, network::Socket* sock); |
| 135 | |
Pierre Ossman | 6c97fa4 | 2018-10-05 17:35:51 +0200 | [diff] [blame^] | 136 | // queryConnection() does some basic checks and then passes on the |
| 137 | // request to the desktop. |
| 138 | void queryConnection(VNCSConnectionST* client, const char* userName); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 139 | |
| 140 | // setBlacklist() is called to replace the VNCServerST's internal |
| 141 | // Blacklist instance with another instance. This allows a single |
| 142 | // Blacklist to be shared by multiple VNCServerST instances. |
| 143 | void setBlacklist(Blacklist* bl) {blHosts = bl ? bl : &blacklist;} |
| 144 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 145 | // setKeyRemapper() replaces the VNCServerST's default key remapper. |
| 146 | // NB: A null pointer is valid here. |
| 147 | void setKeyRemapper(KeyRemapper* kr) { keyRemapper = kr; } |
| 148 | |
| 149 | void getConnInfo(ListConnInfo * listConn); |
| 150 | void setConnStatus(ListConnInfo* listConn); |
| 151 | |
| 152 | bool getDisable() { return disableclients;}; |
| 153 | void setDisable(bool disable) { disableclients = disable;}; |
| 154 | |
Pierre Ossman | 6c97fa4 | 2018-10-05 17:35:51 +0200 | [diff] [blame^] | 155 | // clientReady() is called by a VNCSConnectionST instance when the |
| 156 | // client has completed the handshake and is ready for normal |
| 157 | // communication. |
| 158 | void clientReady(VNCSConnectionST* client, bool shared); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 159 | |
Pierre Ossman | 6c97fa4 | 2018-10-05 17:35:51 +0200 | [diff] [blame^] | 160 | // Estimated time until the next time new updates will be pushed |
| 161 | // to clients |
| 162 | int msToNextUpdate(); |
| 163 | |
| 164 | // Part of the framebuffer that has been modified but is not yet |
| 165 | // ready to be sent to clients |
| 166 | Region getPendingRegion(); |
| 167 | |
| 168 | const RenderedCursor* getRenderedCursor(); |
| 169 | |
| 170 | protected: |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 171 | |
Pierre Ossman | bbf955e | 2011-11-08 12:44:10 +0000 | [diff] [blame] | 172 | // Timer callbacks |
| 173 | virtual bool handleTimeout(Timer* t); |
| 174 | |
| 175 | // - Internal methods |
| 176 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 177 | void startDesktop(); |
Pierre Ossman | b53c3bf | 2018-03-22 16:01:44 +0100 | [diff] [blame] | 178 | void stopDesktop(); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 179 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 180 | Blacklist blacklist; |
| 181 | Blacklist* blHosts; |
| 182 | |
| 183 | SDesktop* desktop; |
| 184 | bool desktopStarted; |
Pierre Ossman | 559a2e8 | 2012-01-23 15:54:11 +0000 | [diff] [blame] | 185 | int blockCounter; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 186 | PixelBuffer* pb; |
Pierre Ossman | 34e62f3 | 2009-03-20 21:46:12 +0000 | [diff] [blame] | 187 | ScreenSet screenLayout; |
Pierre Ossman | bb305ca | 2016-12-11 12:41:26 +0100 | [diff] [blame] | 188 | unsigned int ledState; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 189 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 190 | CharArray name; |
| 191 | |
| 192 | std::list<VNCSConnectionST*> clients; |
| 193 | VNCSConnectionST* pointerClient; |
| 194 | std::list<network::Socket*> closingSockets; |
| 195 | |
| 196 | ComparingUpdateTracker* comparer; |
| 197 | |
| 198 | Point cursorPos; |
Pierre Ossman | 6a1a0d0 | 2017-02-19 15:48:17 +0100 | [diff] [blame] | 199 | Cursor* cursor; |
Pierre Ossman | 6ea6e1a | 2014-02-12 16:33:43 +0100 | [diff] [blame] | 200 | RenderedCursor renderedCursor; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 201 | bool renderedCursorInvalid; |
| 202 | |
| 203 | // - Check how many of the clients are authenticated. |
| 204 | int authClientCount(); |
| 205 | |
| 206 | bool needRenderedCursor(); |
Pierre Ossman | 6e49e95 | 2016-10-07 15:59:38 +0200 | [diff] [blame] | 207 | void startFrameClock(); |
| 208 | void stopFrameClock(); |
| 209 | void writeUpdate(); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 210 | |
Pierre Ossman | b114cec | 2011-11-20 15:36:11 +0000 | [diff] [blame] | 211 | bool getComparerState(); |
| 212 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 213 | KeyRemapper* keyRemapper; |
Pierre Ossman | 0c9bd4b | 2014-07-09 16:44:11 +0200 | [diff] [blame] | 214 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 215 | time_t lastUserInputTime; |
| 216 | time_t lastDisconnectTime; |
| 217 | time_t lastConnectionTime; |
| 218 | |
| 219 | bool disableclients; |
Pierre Ossman | bbf955e | 2011-11-08 12:44:10 +0000 | [diff] [blame] | 220 | |
Pierre Ossman | 6e49e95 | 2016-10-07 15:59:38 +0200 | [diff] [blame] | 221 | Timer frameTimer; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 222 | }; |
| 223 | |
| 224 | }; |
| 225 | |
| 226 | #endif |
| 227 | |