blob: 1e055dd9d548e91855348c3f33534ccd15f03056 [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
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 Ossman559a2e82012-01-23 15:54:11 +000085 virtual void blockUpdates();
86 virtual void unblockUpdates();
Pierre Ossman04e62db2009-03-23 16:57:07 +000087 virtual void setPixelBuffer(PixelBuffer* pb, const ScreenSet& layout);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000088 virtual void setPixelBuffer(PixelBuffer* pb);
Pierre Ossman04e62db2009-03-23 16:57:07 +000089 virtual void setScreenLayout(const ScreenSet& layout);
Constantin Kaplinskyd7c0a6d2007-09-03 04:23:48 +000090 virtual PixelBuffer* getPixelBuffer() const { return pb; }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000091 virtual void serverCutText(const char* str, int len);
92 virtual void add_changed(const Region &region);
93 virtual void add_copied(const Region &dest, const Point &delta);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000094 virtual void setCursor(int width, int height, const Point& hotspot,
Pierre Ossmanff9eb5a2014-01-30 17:47:31 +010095 const void* cursorData, const void* mask);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000096 virtual void setCursorPos(const Point& p);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000097
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 Åstrandc39e0782009-01-15 12:21:42 +0000131 virtual void setName(const char* name_);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000132
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
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000180 // setKeyRemapper() replaces the VNCServerST's default key remapper.
181 // NB: A null pointer is valid here.
182 void setKeyRemapper(KeyRemapper* kr) { keyRemapper = kr; }
183
184 void getConnInfo(ListConnInfo * listConn);
185 void setConnStatus(ListConnInfo* listConn);
186
187 bool getDisable() { return disableclients;};
188 void setDisable(bool disable) { disableclients = disable;};
189
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000190 protected:
191
192 friend class VNCSConnectionST;
193
Pierre Ossmanbbf955e2011-11-08 12:44:10 +0000194 // Timer callbacks
195 virtual bool handleTimeout(Timer* t);
196
197 // - Internal methods
198
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000199 void startDesktop();
200
201 static LogWriter connectionsLog;
202 Blacklist blacklist;
203 Blacklist* blHosts;
204
205 SDesktop* desktop;
206 bool desktopStarted;
Pierre Ossman559a2e82012-01-23 15:54:11 +0000207 int blockCounter;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000208 PixelBuffer* pb;
Pierre Ossman34e62f32009-03-20 21:46:12 +0000209 ScreenSet screenLayout;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000210
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000211 CharArray name;
212
213 std::list<VNCSConnectionST*> clients;
214 VNCSConnectionST* pointerClient;
215 std::list<network::Socket*> closingSockets;
216
217 ComparingUpdateTracker* comparer;
218
219 Point cursorPos;
220 Cursor cursor;
Pierre Ossman6ea6e1a2014-02-12 16:33:43 +0100221 RenderedCursor renderedCursor;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000222 bool renderedCursorInvalid;
223
224 // - Check how many of the clients are authenticated.
225 int authClientCount();
226
227 bool needRenderedCursor();
Pierre Ossmanbbf955e2011-11-08 12:44:10 +0000228 void startDefer();
229 bool checkDefer();
230 void tryUpdate();
231 bool checkUpdate();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000232
Pierre Ossman04e62db2009-03-23 16:57:07 +0000233 void notifyScreenLayoutChange(VNCSConnectionST *requester);
234
Pierre Ossmanb114cec2011-11-20 15:36:11 +0000235 bool getComparerState();
236
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000237 QueryConnectionHandler* queryConnectionHandler;
238 KeyRemapper* keyRemapper;
Pierre Ossman0c9bd4b2014-07-09 16:44:11 +0200239
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000240 time_t lastUserInputTime;
241 time_t lastDisconnectTime;
242 time_t lastConnectionTime;
243
244 bool disableclients;
Pierre Ossmanbbf955e2011-11-08 12:44:10 +0000245
246 Timer deferTimer;
247 bool deferPending;
248 struct timeval deferStart;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000249 };
250
251};
252
253#endif
254