blob: 4035f93d27bb58e26234782180d8f8e393127000 [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
26#include <list>
27
28#include <rfb/SDesktop.h>
29#include <rfb/VNCServer.h>
30#include <rfb/Configuration.h>
31#include <rfb/LogWriter.h>
32#include <rfb/Blacklist.h>
33#include <rfb/Cursor.h>
34#include <network/Socket.h>
35#include <rfb/ListConnInfo.h>
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000036
37namespace rfb {
38
39 class VNCSConnectionST;
40 class ComparingUpdateTracker;
41 class PixelBuffer;
42 class KeyRemapper;
43
44 class VNCServerST : public VNCServer, public network::SocketServer {
45 public:
46 // -=- Constructors
47
48 // Create a server exporting the supplied desktop.
49 VNCServerST(const char* name_, SDesktop* desktop_,
50 SSecurityFactory* securityFactory_=0);
51 virtual ~VNCServerST();
52
53
54 // Methods overridden from SocketServer
55
56 // addSocket
57 // Causes the server to allocate an RFB-protocol management
58 // structure for the socket & initialise it.
59 virtual void addSocket(network::Socket* sock, bool outgoing=false);
60
61 // removeSocket
62 // Clean up any resources associated with the Socket
63 virtual void removeSocket(network::Socket* sock);
64
65 // processSocketEvent
66 // Read more RFB data from the Socket. If an error occurs during
67 // processing then shutdown() is called on the Socket, causing
68 // removeSocket() to be called by the caller at a later time.
69 virtual void processSocketEvent(network::Socket* sock);
70
71 // checkTimeouts
72 // Returns the number of milliseconds left until the next idle timeout
73 // expires. If any have already expired, the corresponding connections
74 // are closed. Zero is returned if there is no idle timeout.
75 virtual int checkTimeouts();
76
77
78 // Methods overridden from VNCServer
79
80 virtual void setPixelBuffer(PixelBuffer* pb);
Constantin Kaplinskyd7c0a6d2007-09-03 04:23:48 +000081 virtual PixelBuffer* getPixelBuffer() const { return pb; }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000082 virtual void setColourMapEntries(int firstColour=0, int nColours=0);
83 virtual void serverCutText(const char* str, int len);
84 virtual void add_changed(const Region &region);
85 virtual void add_copied(const Region &dest, const Point &delta);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000086 virtual bool clientsReadyForUpdate();
87 virtual void tryUpdate();
88 virtual void setCursor(int width, int height, const Point& hotspot,
89 void* cursorData, void* mask);
90 virtual void setCursorPos(const Point& p);
91 virtual void setSSecurityFactory(SSecurityFactory* f) {securityFactory=f;}
92
93 virtual void bell();
94
95 // - Close all currently-connected clients, by calling
96 // their close() method with the supplied reason.
97 virtual void closeClients(const char* reason) {closeClients(reason, 0);}
98
99 // VNCServerST-only methods
100
101 // closeClients() closes all RFB sessions, except the specified one (if
102 // any), and logs the specified reason for closure.
103 void closeClients(const char* reason, network::Socket* sock);
104
105 // getSockets() gets a list of sockets. This can be used to generate an
106 // fd_set for calling select().
107
108 void getSockets(std::list<network::Socket*>* sockets);
109
110 // getSConnection() gets the SConnection for a particular Socket. If
111 // the Socket is not recognised then null is returned.
112
113 SConnection* getSConnection(network::Socket* sock);
114
115 // getDesktopSize() returns the size of the SDesktop exported by this
116 // server.
117 Point getDesktopSize() const {return desktop->getFbSize();}
118
119 // getName() returns the name of this VNC Server. NB: The value returned
120 // is the server's internal buffer which may change after any other methods
121 // are called - take a copy if necessary.
122 const char* getName() const {return name.buf;}
123
124 // setName() specifies the desktop name that the server should provide to
125 // clients
Peter Åstrandc39e0782009-01-15 12:21:42 +0000126 virtual void setName(const char* name_);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000127
128 // A QueryConnectionHandler, if supplied, is passed details of incoming
129 // connections to approve, reject, or query the user about.
130 //
131 // queryConnection() is called when a connection has been
132 // successfully authenticated. The sock and userName arguments identify
133 // the socket and the name of the authenticated user, if any. It should
134 // return ACCEPT if the connection should be accepted, REJECT if it should
135 // be rejected, or PENDING if a decision cannot yet be reached. If REJECT
136 // is returned, *reason can be set to a string describing the reason - this
137 // will be delete[]ed when it is finished with. If PENDING is returned,
138 // approveConnection() must be called some time later to accept or reject
139 // the connection.
140 enum queryResult { ACCEPT, REJECT, PENDING };
141 struct QueryConnectionHandler {
142 virtual ~QueryConnectionHandler() {}
143 virtual queryResult queryConnection(network::Socket* sock,
144 const char* userName,
145 char** reason) = 0;
146 };
147 void setQueryConnectionHandler(QueryConnectionHandler* qch) {
148 queryConnectionHandler = qch;
149 }
150
151 // queryConnection is called as described above, and either passes the
152 // request on to the registered handler, or accepts the connection if
153 // no handler has been specified.
154 virtual queryResult queryConnection(network::Socket* sock,
155 const char* userName,
156 char** reason) {
157 return queryConnectionHandler
158 ? queryConnectionHandler->queryConnection(sock, userName, reason)
159 : ACCEPT;
160 }
161
162 // approveConnection() is called by the active QueryConnectionHandler,
163 // some time after queryConnection() has returned with PENDING, to accept
164 // or reject the connection. The accept argument should be true for
165 // acceptance, or false for rejection, in which case a string reason may
166 // also be given.
167 void approveConnection(network::Socket* sock, bool accept,
168 const char* reason);
169
170 // setBlacklist() is called to replace the VNCServerST's internal
171 // Blacklist instance with another instance. This allows a single
172 // Blacklist to be shared by multiple VNCServerST instances.
173 void setBlacklist(Blacklist* bl) {blHosts = bl ? bl : &blacklist;}
174
175 // setEconomicTranslate() determines (for new connections) whether pixels
176 // should be translated for <=16bpp clients using a large lookup table
177 // (fast) or separate, smaller R, G and B tables (slower). If set to true,
178 // small tables are used, to save memory.
179 void setEconomicTranslate(bool et) { useEconomicTranslate = et; }
180
181 // setKeyRemapper() replaces the VNCServerST's default key remapper.
182 // NB: A null pointer is valid here.
183 void setKeyRemapper(KeyRemapper* kr) { keyRemapper = kr; }
184
185 void getConnInfo(ListConnInfo * listConn);
186 void setConnStatus(ListConnInfo* listConn);
187
188 bool getDisable() { return disableclients;};
189 void setDisable(bool disable) { disableclients = disable;};
190
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000191 protected:
192
193 friend class VNCSConnectionST;
194
195 void startDesktop();
196
197 static LogWriter connectionsLog;
198 Blacklist blacklist;
199 Blacklist* blHosts;
200
201 SDesktop* desktop;
202 bool desktopStarted;
203 PixelBuffer* pb;
204
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000205 CharArray name;
206
207 std::list<VNCSConnectionST*> clients;
208 VNCSConnectionST* pointerClient;
209 std::list<network::Socket*> closingSockets;
210
211 ComparingUpdateTracker* comparer;
212
213 Point cursorPos;
214 Cursor cursor;
215 Point cursorTL() { return cursorPos.subtract(cursor.hotspot); }
216 Point renderedCursorTL;
217 ManagedPixelBuffer renderedCursor;
218 bool renderedCursorInvalid;
219
220 // - Check how many of the clients are authenticated.
221 int authClientCount();
222
223 bool needRenderedCursor();
224 void checkUpdate();
225
226 SSecurityFactory* securityFactory;
227 QueryConnectionHandler* queryConnectionHandler;
228 KeyRemapper* keyRemapper;
229 bool useEconomicTranslate;
230
231 time_t lastUserInputTime;
232 time_t lastDisconnectTime;
233 time_t lastConnectionTime;
234
235 bool disableclients;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000236 };
237
238};
239
240#endif
241