blob: 9d5fe95416da62cda143902bf116f7923bfe4845 [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>
Dennis Syrovatsky265d3c82006-04-17 12:36:11 +000036#include <rfb/SFileTransferManager.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
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000115 // 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
Dennis Syrovatsky265d3c82006-04-17 12:36:11 +0000194 bool getDisable() { return disableclients;};
195 void setDisable(bool disable) { disableclients = disable;};
196
197 void setFTManager(rfb::SFileTransferManager *pFTManager) { m_pFTManager = pFTManager; };
Oleg Sheikin5c642e92005-12-22 20:57:58 +0000198
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000199 protected:
200
201 friend class VNCSConnectionST;
202
203 void startDesktop();
204
205 static LogWriter connectionsLog;
206 Blacklist blacklist;
207 Blacklist* blHosts;
208
209 SDesktop* desktop;
210 bool desktopStarted;
211 PixelBuffer* pb;
212
Dennis Syrovatsky265d3c82006-04-17 12:36:11 +0000213 SFileTransferManager *m_pFTManager;
214
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000215 CharArray name;
216
217 std::list<VNCSConnectionST*> clients;
218 VNCSConnectionST* pointerClient;
219 std::list<network::Socket*> closingSockets;
220
221 ComparingUpdateTracker* comparer;
222
223 Point cursorPos;
224 Cursor cursor;
225 Point cursorTL() { return cursorPos.subtract(cursor.hotspot); }
226 Point renderedCursorTL;
227 ManagedPixelBuffer renderedCursor;
228 bool renderedCursorInvalid;
229
230 // - Check how many of the clients are authenticated.
231 int authClientCount();
232
233 bool needRenderedCursor();
234 void checkUpdate();
235
236 SSecurityFactory* securityFactory;
237 QueryConnectionHandler* queryConnectionHandler;
238 bool useEconomicTranslate;
Peter Åstrand43aa1a12005-02-21 09:58:31 +0000239
240 time_t lastUserInputTime;
241 time_t lastDisconnectTime;
242 time_t lastConnectionTime;
Oleg Sheikin5c642e92005-12-22 20:57:58 +0000243
244 bool disableclients;
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000245 };
246
247};
248
249#endif
250