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