blob: 554a4c9045ab8dca1ddb9ee7f1cf08b9a9b13160 [file] [log] [blame]
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +00001/* Copyright (C) 2002-2004 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>
Oleg Sheikin641f7e52005-11-22 18:04:10 +000035#include <rfb/ListConnInfo.h>
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +000036
37namespace rfb {
38
39 class VNCSConnectionST;
40 class ComparingUpdateTracker;
41 class PixelBuffer;
42
43 class VNCServerST : public VNCServer, public network::SocketServer {
44 public:
45 // -=- Constructors
46
47 // Create a server exporting the supplied desktop.
48 VNCServerST(const char* name_, SDesktop* desktop_,
49 SSecurityFactory* securityFactory_=0);
50 virtual ~VNCServerST();
51
52
53 // Methods overridden from SocketServer
54
55 // - Run a client connection on the supplied socket
56 // This causes the server to allocate the required structures
57 // to handle a client connection, and to initialise the RFB
58 // protocol.
59 // NB: The server assumes ownership of the Socket object.
60
61 virtual void addClient(network::Socket* sock);
62
63 // - Process an input event on a particular Socket
64 // The platform-specific side of the server implementation calls
65 // this method whenever data arrives on one of the active
66 // network sockets.
67 // The method returns true if the Socket is still in use by the
68 // server, or false if it is no longer required and has been
69 // deleted.
70 // NB: If false is returned then the Socket is deleted and must
71 // not be accessed again!
72
73 virtual bool processSocketEvent(network::Socket* sock);
74
75 // - checkTimeouts() returns the number of milliseconds left until the next
76 // idle timeout expires. If any have already expired, the corresponding
77 // connections are closed. Zero is returned if there is no idle timeout.
78
79 virtual int checkTimeouts();
80
81
82 // Methods overridden from VNCServer
83
84 virtual void setPixelBuffer(PixelBuffer* pb);
85 virtual void setColourMapEntries(int firstColour=0, int nColours=0);
86 virtual void serverCutText(const char* str, int len);
87 virtual void add_changed(const Region &region);
88 virtual void add_copied(const Region &dest, const Point &delta);
89 virtual bool clientsReadyForUpdate();
90 virtual void tryUpdate();
91 virtual void setCursor(int width, int height, int hotspotX, int hotspotY,
92 void* cursorData, void* mask);
93 virtual void setCursorPos(int x, int y);
94 virtual void setSSecurityFactory(SSecurityFactory* f) {securityFactory=f;}
95
96 virtual void bell();
97
98 // - Close all currently-connected clients, by calling
99 // their close() method with the supplied reason.
100 virtual void closeClients(const char* reason) {closeClients(reason, 0);}
101
102 // VNCServerST-only methods
103
104 // If a particular VNCSConnectionST* is specified then
105 // that connection will NOT be closed.
106 void closeClients(const char* reason, network::Socket* sock);
107
108 // addClient() with an extra flag to say if this is a reverse connection to
109 // a listening client. Reverse connections are not authenticated and are
110 // always shared (unless the NeverShared parameter is set).
111
112 void addClient(network::Socket* sock, bool reverse);
113
114
115 // getSockets() gets a list of sockets. This can be used to generate an
116 // fd_set for calling select().
117
118 void getSockets(std::list<network::Socket*>* sockets);
119
120 // getSConnection() gets the SConnection for a particular Socket. If
121 // the Socket is not recognised then null is returned.
122
123 SConnection* getSConnection(network::Socket* sock);
124
125 // getDesktopSize() returns the size of the SDesktop exported by this
126 // server.
127 Point getDesktopSize() const {return desktop->getFbSize();}
128
129 // getName() returns the name of this VNC Server. NB: The value returned
130 // is the server's internal buffer which may change after any other methods
131 // are called - take a copy if necessary.
132 const char* getName() const {return name.buf;}
133
134 // setName() specifies the desktop name that the server should provide to
135 // clients
136 void setName(const char* name_) {name.replaceBuf(strDup(name_));}
137
138 // A QueryConnectionHandler, if supplied, is passed details of incoming
139 // connections to approve, reject, or query the user about.
140 //
141 // queryConnection() is called when a connection has been
142 // successfully authenticated. The sock and userName arguments identify
143 // the socket and the name of the authenticated user, if any. It should
144 // return ACCEPT if the connection should be accepted, REJECT if it should
145 // be rejected, or PENDING if a decision cannot yet be reached. If REJECT
146 // is returned, *reason can be set to a string describing the reason - this
147 // will be delete[]ed when it is finished with. If PENDING is returned,
148 // approveConnection() must be called some time later to accept or reject
149 // the connection.
150 enum queryResult { ACCEPT, REJECT, PENDING };
151 struct QueryConnectionHandler {
152 virtual ~QueryConnectionHandler() {}
153 virtual queryResult queryConnection(network::Socket* sock,
154 const char* userName,
155 char** reason) = 0;
156 };
157 void setQueryConnectionHandler(QueryConnectionHandler* qch) {
158 queryConnectionHandler = qch;
159 }
160
161 // queryConnection is called as described above, and either passes the
162 // request on to the registered handler, or accepts the connection if
163 // no handler has been specified.
164 virtual queryResult queryConnection(network::Socket* sock,
165 const char* userName,
166 char** reason) {
167 return queryConnectionHandler
168 ? queryConnectionHandler->queryConnection(sock, userName, reason)
169 : ACCEPT;
170 }
171
172 // approveConnection() is called by the active QueryConnectionHandler,
173 // some time after queryConnection() has returned with PENDING, to accept
174 // or reject the connection. The accept argument should be true for
175 // acceptance, or false for rejection, in which case a string reason may
176 // also be given.
177 void approveConnection(network::Socket* sock, bool accept,
178 const char* reason);
179
180 // setBlacklist() is called to replace the VNCServerST's internal
181 // Blacklist instance with another instance. This allows a single
182 // Blacklist to be shared by multiple VNCServerST instances.
183 void setBlacklist(Blacklist* bl) {blHosts = bl ? bl : &blacklist;}
184
185 // setEconomicTranslate() determines (for new connections) whether pixels
186 // should be translated for <=16bpp clients using a large lookup table (fast)
187 // or separate, smaller R, G and B tables (slower). If set to true, small tables
188 // are used, to save memory.
189 void setEconomicTranslate(bool et) { useEconomicTranslate = et; }
190
Oleg Sheikinff43bfd2005-12-07 08:02:52 +0000191 void getConnInfo(ListConnInfo * listConn);
Oleg Sheikin4b0304f2005-12-09 10:59:12 +0000192 void setConnStatus(ListConnInfo* listConn);
Oleg Sheikin641f7e52005-11-22 18:04:10 +0000193
Oleg Sheikin5c642e92005-12-22 20:57:58 +0000194 bool getDisable() { return disableclients;};
195 void setDisable(bool disable) { disableclients = disable;};
196
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000197 protected:
198
199 friend class VNCSConnectionST;
200
201 void startDesktop();
202
203 static LogWriter connectionsLog;
204 Blacklist blacklist;
205 Blacklist* blHosts;
206
207 SDesktop* desktop;
208 bool desktopStarted;
209 PixelBuffer* pb;
210
211 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;
221 Point cursorTL() { return cursorPos.subtract(cursor.hotspot); }
222 Point renderedCursorTL;
223 ManagedPixelBuffer renderedCursor;
224 bool renderedCursorInvalid;
225
226 // - Check how many of the clients are authenticated.
227 int authClientCount();
228
229 bool needRenderedCursor();
230 void checkUpdate();
231
232 SSecurityFactory* securityFactory;
233 QueryConnectionHandler* queryConnectionHandler;
234 bool useEconomicTranslate;
Peter Åstrand43aa1a12005-02-21 09:58:31 +0000235
236 time_t lastUserInputTime;
237 time_t lastDisconnectTime;
238 time_t lastConnectionTime;
Oleg Sheikin5c642e92005-12-22 20:57:58 +0000239
240 bool disableclients;
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000241 };
242
243};
244
245#endif
246