blob: bd84c452e78456f1fe31a93498db5b0eaa89fc09 [file] [log] [blame]
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001/* 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 Ossmanbbf955e2011-11-08 12:44:10 +000026#include <sys/time.h>
27
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000028#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 Ossmanbbf955e2011-11-08 12:44:10 +000036#include <rfb/Timer.h>
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000037#include <network/Socket.h>
38#include <rfb/ListConnInfo.h>
Pierre Ossman34e62f32009-03-20 21:46:12 +000039#include <rfb/ScreenSet.h>
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000040
41namespace rfb {
42
43 class VNCSConnectionST;
44 class ComparingUpdateTracker;
45 class PixelBuffer;
46 class KeyRemapper;
47
Pierre Ossmanbbf955e2011-11-08 12:44:10 +000048 class VNCServerST : public VNCServer,
49 public Timer::Callback,
50 public network::SocketServer {
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000051 public:
52 // -=- Constructors
53
54 // Create a server exporting the supplied desktop.
Adam Tkaca6578bf2010-04-23 14:07:41 +000055 VNCServerST(const char* name_, SDesktop* desktop_);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000056 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
Pierre Ossmand408ca52016-04-29 14:26:05 +020070 // processSocketReadEvent
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000071 // 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.
Pierre Ossmand408ca52016-04-29 14:26:05 +020074 virtual void processSocketReadEvent(network::Socket* sock);
75
76 // processSocketWriteEvent
77 // Flush pending data from the Socket on to the network.
78 virtual void processSocketWriteEvent(network::Socket* sock);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000079
80 // checkTimeouts
81 // Returns the number of milliseconds left until the next idle timeout
82 // expires. If any have already expired, the corresponding connections
83 // are closed. Zero is returned if there is no idle timeout.
84 virtual int checkTimeouts();
85
86
87 // Methods overridden from VNCServer
88
Pierre Ossman559a2e82012-01-23 15:54:11 +000089 virtual void blockUpdates();
90 virtual void unblockUpdates();
Pierre Ossman04e62db2009-03-23 16:57:07 +000091 virtual void setPixelBuffer(PixelBuffer* pb, const ScreenSet& layout);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000092 virtual void setPixelBuffer(PixelBuffer* pb);
Pierre Ossman04e62db2009-03-23 16:57:07 +000093 virtual void setScreenLayout(const ScreenSet& layout);
Constantin Kaplinskyd7c0a6d2007-09-03 04:23:48 +000094 virtual PixelBuffer* getPixelBuffer() const { return pb; }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000095 virtual void serverCutText(const char* str, int len);
96 virtual void add_changed(const Region &region);
97 virtual void add_copied(const Region &dest, const Point &delta);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000098 virtual void setCursor(int width, int height, const Point& hotspot,
Pierre Ossmanff9eb5a2014-01-30 17:47:31 +010099 const void* cursorData, const void* mask);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000100 virtual void setCursorPos(const Point& p);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000101
102 virtual void bell();
103
104 // - Close all currently-connected clients, by calling
105 // their close() method with the supplied reason.
106 virtual void closeClients(const char* reason) {closeClients(reason, 0);}
107
108 // VNCServerST-only methods
109
110 // closeClients() closes all RFB sessions, except the specified one (if
111 // any), and logs the specified reason for closure.
112 void closeClients(const char* reason, network::Socket* sock);
113
114 // getSockets() gets a list of sockets. This can be used to generate an
115 // fd_set for calling select().
116
117 void getSockets(std::list<network::Socket*>* sockets);
118
119 // getSConnection() gets the SConnection for a particular Socket. If
120 // the Socket is not recognised then null is returned.
121
122 SConnection* getSConnection(network::Socket* sock);
123
124 // getDesktopSize() returns the size of the SDesktop exported by this
125 // server.
126 Point getDesktopSize() const {return desktop->getFbSize();}
127
128 // getName() returns the name of this VNC Server. NB: The value returned
129 // is the server's internal buffer which may change after any other methods
130 // are called - take a copy if necessary.
131 const char* getName() const {return name.buf;}
132
133 // setName() specifies the desktop name that the server should provide to
134 // clients
Peter Åstrandc39e0782009-01-15 12:21:42 +0000135 virtual void setName(const char* name_);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000136
137 // A QueryConnectionHandler, if supplied, is passed details of incoming
138 // connections to approve, reject, or query the user about.
139 //
140 // queryConnection() is called when a connection has been
141 // successfully authenticated. The sock and userName arguments identify
142 // the socket and the name of the authenticated user, if any. It should
143 // return ACCEPT if the connection should be accepted, REJECT if it should
144 // be rejected, or PENDING if a decision cannot yet be reached. If REJECT
145 // is returned, *reason can be set to a string describing the reason - this
146 // will be delete[]ed when it is finished with. If PENDING is returned,
147 // approveConnection() must be called some time later to accept or reject
148 // the connection.
149 enum queryResult { ACCEPT, REJECT, PENDING };
150 struct QueryConnectionHandler {
151 virtual ~QueryConnectionHandler() {}
152 virtual queryResult queryConnection(network::Socket* sock,
153 const char* userName,
154 char** reason) = 0;
155 };
156 void setQueryConnectionHandler(QueryConnectionHandler* qch) {
157 queryConnectionHandler = qch;
158 }
159
160 // queryConnection is called as described above, and either passes the
161 // request on to the registered handler, or accepts the connection if
162 // no handler has been specified.
163 virtual queryResult queryConnection(network::Socket* sock,
164 const char* userName,
165 char** reason) {
166 return queryConnectionHandler
167 ? queryConnectionHandler->queryConnection(sock, userName, reason)
168 : ACCEPT;
169 }
170
171 // approveConnection() is called by the active QueryConnectionHandler,
172 // some time after queryConnection() has returned with PENDING, to accept
173 // or reject the connection. The accept argument should be true for
174 // acceptance, or false for rejection, in which case a string reason may
175 // also be given.
176 void approveConnection(network::Socket* sock, bool accept,
177 const char* reason);
178
179 // setBlacklist() is called to replace the VNCServerST's internal
180 // Blacklist instance with another instance. This allows a single
181 // Blacklist to be shared by multiple VNCServerST instances.
182 void setBlacklist(Blacklist* bl) {blHosts = bl ? bl : &blacklist;}
183
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000184 // setKeyRemapper() replaces the VNCServerST's default key remapper.
185 // NB: A null pointer is valid here.
186 void setKeyRemapper(KeyRemapper* kr) { keyRemapper = kr; }
187
188 void getConnInfo(ListConnInfo * listConn);
189 void setConnStatus(ListConnInfo* listConn);
190
191 bool getDisable() { return disableclients;};
192 void setDisable(bool disable) { disableclients = disable;};
193
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000194 protected:
195
196 friend class VNCSConnectionST;
197
Pierre Ossmanbbf955e2011-11-08 12:44:10 +0000198 // Timer callbacks
199 virtual bool handleTimeout(Timer* t);
200
201 // - Internal methods
202
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000203 void startDesktop();
204
205 static LogWriter connectionsLog;
206 Blacklist blacklist;
207 Blacklist* blHosts;
208
209 SDesktop* desktop;
210 bool desktopStarted;
Pierre Ossman559a2e82012-01-23 15:54:11 +0000211 int blockCounter;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000212 PixelBuffer* pb;
Pierre Ossman34e62f32009-03-20 21:46:12 +0000213 ScreenSet screenLayout;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000214
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000215 CharArray name;
216
217 std::list<VNCSConnectionST*> clients;
218 VNCSConnectionST* pointerClient;
219 std::list<network::Socket*> closingSockets;
220
221 ComparingUpdateTracker* comparer;
222
223 Point cursorPos;
224 Cursor cursor;
Pierre Ossman6ea6e1a2014-02-12 16:33:43 +0100225 RenderedCursor renderedCursor;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000226 bool renderedCursorInvalid;
227
228 // - Check how many of the clients are authenticated.
229 int authClientCount();
230
231 bool needRenderedCursor();
Pierre Ossmanbbf955e2011-11-08 12:44:10 +0000232 void startDefer();
233 bool checkDefer();
234 void tryUpdate();
235 bool checkUpdate();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000236
Pierre Ossman04e62db2009-03-23 16:57:07 +0000237 void notifyScreenLayoutChange(VNCSConnectionST *requester);
238
Pierre Ossmanb114cec2011-11-20 15:36:11 +0000239 bool getComparerState();
240
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000241 QueryConnectionHandler* queryConnectionHandler;
242 KeyRemapper* keyRemapper;
Pierre Ossman0c9bd4b2014-07-09 16:44:11 +0200243
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000244 time_t lastUserInputTime;
245 time_t lastDisconnectTime;
246 time_t lastConnectionTime;
247
248 bool disableclients;
Pierre Ossmanbbf955e2011-11-08 12:44:10 +0000249
250 Timer deferTimer;
251 bool deferPending;
252 struct timeval deferStart;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000253 };
254
255};
256
257#endif
258