blob: 320db2f488b566ba84941577ab30a439ae40f446 [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>
Pierre Ossman34e62f32009-03-20 21:46:12 +000036#include <rfb/ScreenSet.h>
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000037
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
Pierre Ossman04e62db2009-03-23 16:57:07 +000081 virtual void setPixelBuffer(PixelBuffer* pb, const ScreenSet& layout);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000082 virtual void setPixelBuffer(PixelBuffer* pb);
Pierre Ossman04e62db2009-03-23 16:57:07 +000083 virtual void setScreenLayout(const ScreenSet& layout);
Constantin Kaplinskyd7c0a6d2007-09-03 04:23:48 +000084 virtual PixelBuffer* getPixelBuffer() const { return pb; }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000085 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);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000089 virtual bool clientsReadyForUpdate();
90 virtual void tryUpdate();
91 virtual void setCursor(int width, int height, const Point& hotspot,
92 void* cursorData, void* mask);
93 virtual void setCursorPos(const Point& p);
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 // closeClients() closes all RFB sessions, except the specified one (if
105 // any), and logs the specified reason for closure.
106 void closeClients(const char* reason, network::Socket* sock);
107
108 // getSockets() gets a list of sockets. This can be used to generate an
109 // fd_set for calling select().
110
111 void getSockets(std::list<network::Socket*>* sockets);
112
113 // getSConnection() gets the SConnection for a particular Socket. If
114 // the Socket is not recognised then null is returned.
115
116 SConnection* getSConnection(network::Socket* sock);
117
118 // getDesktopSize() returns the size of the SDesktop exported by this
119 // server.
120 Point getDesktopSize() const {return desktop->getFbSize();}
121
122 // getName() returns the name of this VNC Server. NB: The value returned
123 // is the server's internal buffer which may change after any other methods
124 // are called - take a copy if necessary.
125 const char* getName() const {return name.buf;}
126
127 // setName() specifies the desktop name that the server should provide to
128 // clients
Peter Åstrandc39e0782009-01-15 12:21:42 +0000129 virtual void setName(const char* name_);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000130
131 // A QueryConnectionHandler, if supplied, is passed details of incoming
132 // connections to approve, reject, or query the user about.
133 //
134 // queryConnection() is called when a connection has been
135 // successfully authenticated. The sock and userName arguments identify
136 // the socket and the name of the authenticated user, if any. It should
137 // return ACCEPT if the connection should be accepted, REJECT if it should
138 // be rejected, or PENDING if a decision cannot yet be reached. If REJECT
139 // is returned, *reason can be set to a string describing the reason - this
140 // will be delete[]ed when it is finished with. If PENDING is returned,
141 // approveConnection() must be called some time later to accept or reject
142 // the connection.
143 enum queryResult { ACCEPT, REJECT, PENDING };
144 struct QueryConnectionHandler {
145 virtual ~QueryConnectionHandler() {}
146 virtual queryResult queryConnection(network::Socket* sock,
147 const char* userName,
148 char** reason) = 0;
149 };
150 void setQueryConnectionHandler(QueryConnectionHandler* qch) {
151 queryConnectionHandler = qch;
152 }
153
154 // queryConnection is called as described above, and either passes the
155 // request on to the registered handler, or accepts the connection if
156 // no handler has been specified.
157 virtual queryResult queryConnection(network::Socket* sock,
158 const char* userName,
159 char** reason) {
160 return queryConnectionHandler
161 ? queryConnectionHandler->queryConnection(sock, userName, reason)
162 : ACCEPT;
163 }
164
165 // approveConnection() is called by the active QueryConnectionHandler,
166 // some time after queryConnection() has returned with PENDING, to accept
167 // or reject the connection. The accept argument should be true for
168 // acceptance, or false for rejection, in which case a string reason may
169 // also be given.
170 void approveConnection(network::Socket* sock, bool accept,
171 const char* reason);
172
173 // setBlacklist() is called to replace the VNCServerST's internal
174 // Blacklist instance with another instance. This allows a single
175 // Blacklist to be shared by multiple VNCServerST instances.
176 void setBlacklist(Blacklist* bl) {blHosts = bl ? bl : &blacklist;}
177
178 // setEconomicTranslate() determines (for new connections) whether pixels
179 // should be translated for <=16bpp clients using a large lookup table
180 // (fast) or separate, smaller R, G and B tables (slower). If set to true,
181 // small tables are used, to save memory.
182 void setEconomicTranslate(bool et) { useEconomicTranslate = et; }
183
184 // setKeyRemapper() replaces the VNCServerST's default key remapper.
185 // NB: A null pointer is valid here.
186 void setKeyRemapper(KeyRemapper* kr) { keyRemapper = kr; }
187
188 void getConnInfo(ListConnInfo * listConn);
189 void setConnStatus(ListConnInfo* listConn);
190
191 bool getDisable() { return disableclients;};
192 void setDisable(bool disable) { disableclients = disable;};
193
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +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;
Pierre Ossman34e62f32009-03-20 21:46:12 +0000207 ScreenSet screenLayout;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000208
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000209 CharArray name;
210
211 std::list<VNCSConnectionST*> clients;
212 VNCSConnectionST* pointerClient;
213 std::list<network::Socket*> closingSockets;
214
215 ComparingUpdateTracker* comparer;
216
217 Point cursorPos;
218 Cursor cursor;
219 Point cursorTL() { return cursorPos.subtract(cursor.hotspot); }
220 Point renderedCursorTL;
221 ManagedPixelBuffer renderedCursor;
222 bool renderedCursorInvalid;
223
224 // - Check how many of the clients are authenticated.
225 int authClientCount();
226
227 bool needRenderedCursor();
228 void checkUpdate();
229
Pierre Ossman04e62db2009-03-23 16:57:07 +0000230 void notifyScreenLayoutChange(VNCSConnectionST *requester);
231
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000232 SSecurityFactory* securityFactory;
233 QueryConnectionHandler* queryConnectionHandler;
234 KeyRemapper* keyRemapper;
235 bool useEconomicTranslate;
236
237 time_t lastUserInputTime;
238 time_t lastDisconnectTime;
239 time_t lastConnectionTime;
240
241 bool disableclients;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000242 };
243
244};
245
246#endif
247