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