blob: ede522364b6ce4b5292e2d0fda06715575f36ba5 [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
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 Kaplinskya2adc8d2006-05-25 05:01:55 +0000196 protected:
197
198 friend class VNCSConnectionST;
199
Pierre Ossmanbbf955e2011-11-08 12:44:10 +0000200 // Timer callbacks
201 virtual bool handleTimeout(Timer* t);
202
203 // - Internal methods
204
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000205 void startDesktop();
206
207 static LogWriter connectionsLog;
208 Blacklist blacklist;
209 Blacklist* blHosts;
210
211 SDesktop* desktop;
212 bool desktopStarted;
Pierre Ossman559a2e82012-01-23 15:54:11 +0000213 int blockCounter;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000214 PixelBuffer* pb;
Pierre Ossman34e62f32009-03-20 21:46:12 +0000215 ScreenSet screenLayout;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000216
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000217 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 Ossman6ea6e1a2014-02-12 16:33:43 +0100227 RenderedCursor renderedCursor;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000228 bool renderedCursorInvalid;
229
230 // - Check how many of the clients are authenticated.
231 int authClientCount();
232
233 bool needRenderedCursor();
Pierre Ossmanbbf955e2011-11-08 12:44:10 +0000234 void startDefer();
235 bool checkDefer();
236 void tryUpdate();
237 bool checkUpdate();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000238
Pierre Ossman04e62db2009-03-23 16:57:07 +0000239 void notifyScreenLayoutChange(VNCSConnectionST *requester);
240
Pierre Ossmanb114cec2011-11-20 15:36:11 +0000241 bool getComparerState();
242
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000243 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 Ossmanbbf955e2011-11-08 12:44:10 +0000252
253 Timer deferTimer;
254 bool deferPending;
255 struct timeval deferStart;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000256 };
257
258};
259
260#endif
261