blob: 162fa9a432f43661625fdbf2f40df4b76e277e07 [file] [log] [blame]
Constantin Kaplinskyde179d42006-04-16 06:53:44 +00001/* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved.
2 *
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +00003 * 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;
Constantin Kaplinskyde179d42006-04-16 06:53:44 +000042 class KeyRemapper;
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +000043
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
Constantin Kaplinskyde179d42006-04-16 06:53:44 +000056 // addSocket
57 // Causes the server to allocate an RFB-protocol management
58 // structure for the socket & initialise it.
59 virtual void addSocket(network::Socket* sock, bool outgoing=false);
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +000060
Constantin Kaplinskyde179d42006-04-16 06:53:44 +000061 // removeSocket
62 // Clean up any resources associated with the Socket
63 virtual void removeSocket(network::Socket* sock);
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +000064
Constantin Kaplinskyde179d42006-04-16 06:53:44 +000065 // processSocketEvent
66 // Read more RFB data from the Socket. If an error occurs during
67 // processing then shutdown() is called on the Socket, causing
68 // removeSocket() to be called by the caller at a later time.
69 virtual void processSocketEvent(network::Socket* sock);
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +000070
Constantin Kaplinskyde179d42006-04-16 06:53:44 +000071 // checkTimeouts
72 // Returns the number of milliseconds left until the next idle timeout
73 // expires. If any have already expired, the corresponding connections
74 // are closed. Zero is returned if there is no idle timeout.
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +000075 virtual int checkTimeouts();
76
77
78 // Methods overridden from VNCServer
79
80 virtual void setPixelBuffer(PixelBuffer* pb);
81 virtual void setColourMapEntries(int firstColour=0, int nColours=0);
82 virtual void serverCutText(const char* str, int len);
83 virtual void add_changed(const Region &region);
84 virtual void add_copied(const Region &dest, const Point &delta);
85 virtual bool clientsReadyForUpdate();
86 virtual void tryUpdate();
Constantin Kaplinskyde179d42006-04-16 06:53:44 +000087 virtual void setCursor(int width, int height, const Point& hotspot,
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +000088 void* cursorData, void* mask);
Constantin Kaplinskyde179d42006-04-16 06:53:44 +000089 virtual void setCursorPos(const Point& p);
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +000090 virtual void setSSecurityFactory(SSecurityFactory* f) {securityFactory=f;}
91
92 virtual void bell();
93
94 // - Close all currently-connected clients, by calling
95 // their close() method with the supplied reason.
96 virtual void closeClients(const char* reason) {closeClients(reason, 0);}
97
98 // VNCServerST-only methods
99
Constantin Kaplinskyde179d42006-04-16 06:53:44 +0000100 // closeClients() closes all RFB sessions, except the specified one (if
101 // any), and logs the specified reason for closure.
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000102 void closeClients(const char* reason, network::Socket* sock);
103
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000104 // getSockets() gets a list of sockets. This can be used to generate an
105 // fd_set for calling select().
106
107 void getSockets(std::list<network::Socket*>* sockets);
108
109 // getSConnection() gets the SConnection for a particular Socket. If
110 // the Socket is not recognised then null is returned.
111
112 SConnection* getSConnection(network::Socket* sock);
113
114 // getDesktopSize() returns the size of the SDesktop exported by this
115 // server.
116 Point getDesktopSize() const {return desktop->getFbSize();}
117
118 // getName() returns the name of this VNC Server. NB: The value returned
119 // is the server's internal buffer which may change after any other methods
120 // are called - take a copy if necessary.
121 const char* getName() const {return name.buf;}
122
123 // setName() specifies the desktop name that the server should provide to
124 // clients
125 void setName(const char* name_) {name.replaceBuf(strDup(name_));}
126
127 // A QueryConnectionHandler, if supplied, is passed details of incoming
128 // connections to approve, reject, or query the user about.
129 //
130 // queryConnection() is called when a connection has been
131 // successfully authenticated. The sock and userName arguments identify
132 // the socket and the name of the authenticated user, if any. It should
133 // return ACCEPT if the connection should be accepted, REJECT if it should
134 // be rejected, or PENDING if a decision cannot yet be reached. If REJECT
135 // is returned, *reason can be set to a string describing the reason - this
136 // will be delete[]ed when it is finished with. If PENDING is returned,
137 // approveConnection() must be called some time later to accept or reject
138 // the connection.
139 enum queryResult { ACCEPT, REJECT, PENDING };
140 struct QueryConnectionHandler {
141 virtual ~QueryConnectionHandler() {}
142 virtual queryResult queryConnection(network::Socket* sock,
143 const char* userName,
144 char** reason) = 0;
145 };
146 void setQueryConnectionHandler(QueryConnectionHandler* qch) {
147 queryConnectionHandler = qch;
148 }
149
150 // queryConnection is called as described above, and either passes the
151 // request on to the registered handler, or accepts the connection if
152 // no handler has been specified.
153 virtual queryResult queryConnection(network::Socket* sock,
154 const char* userName,
155 char** reason) {
156 return queryConnectionHandler
157 ? queryConnectionHandler->queryConnection(sock, userName, reason)
158 : ACCEPT;
159 }
160
161 // approveConnection() is called by the active QueryConnectionHandler,
162 // some time after queryConnection() has returned with PENDING, to accept
163 // or reject the connection. The accept argument should be true for
164 // acceptance, or false for rejection, in which case a string reason may
165 // also be given.
166 void approveConnection(network::Socket* sock, bool accept,
167 const char* reason);
168
169 // setBlacklist() is called to replace the VNCServerST's internal
170 // Blacklist instance with another instance. This allows a single
171 // Blacklist to be shared by multiple VNCServerST instances.
172 void setBlacklist(Blacklist* bl) {blHosts = bl ? bl : &blacklist;}
173
174 // setEconomicTranslate() determines (for new connections) whether pixels
Constantin Kaplinskyde179d42006-04-16 06:53:44 +0000175 // should be translated for <=16bpp clients using a large lookup table
176 // (fast) or separate, smaller R, G and B tables (slower). If set to true,
177 // small tables are used, to save memory.
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000178 void setEconomicTranslate(bool et) { useEconomicTranslate = et; }
179
Constantin Kaplinskyde179d42006-04-16 06:53:44 +0000180 // setKeyRemapper() replaces the VNCServerST's default key remapper.
181 // NB: A null pointer is valid here.
182 void setKeyRemapper(KeyRemapper* kr) { keyRemapper = kr; }
183
Oleg Sheikinff43bfd2005-12-07 08:02:52 +0000184 void getConnInfo(ListConnInfo * listConn);
Oleg Sheikin4b0304f2005-12-09 10:59:12 +0000185 void setConnStatus(ListConnInfo* listConn);
Oleg Sheikin641f7e52005-11-22 18:04:10 +0000186
Constantin Kaplinskycf4e9f92006-01-26 14:27:08 +0000187 bool getDisable() { return disableclients; }
188 void setDisable(bool disable) { disableclients = disable; }
Oleg Sheikin5c642e92005-12-22 20:57:58 +0000189
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000190 protected:
191
192 friend class VNCSConnectionST;
193
194 void startDesktop();
195
196 static LogWriter connectionsLog;
197 Blacklist blacklist;
198 Blacklist* blHosts;
199
200 SDesktop* desktop;
201 bool desktopStarted;
202 PixelBuffer* pb;
203
204 CharArray name;
205
206 std::list<VNCSConnectionST*> clients;
207 VNCSConnectionST* pointerClient;
208 std::list<network::Socket*> closingSockets;
209
210 ComparingUpdateTracker* comparer;
211
212 Point cursorPos;
213 Cursor cursor;
214 Point cursorTL() { return cursorPos.subtract(cursor.hotspot); }
215 Point renderedCursorTL;
216 ManagedPixelBuffer renderedCursor;
217 bool renderedCursorInvalid;
218
219 // - Check how many of the clients are authenticated.
220 int authClientCount();
221
222 bool needRenderedCursor();
223 void checkUpdate();
224
225 SSecurityFactory* securityFactory;
226 QueryConnectionHandler* queryConnectionHandler;
Constantin Kaplinskyde179d42006-04-16 06:53:44 +0000227 KeyRemapper* keyRemapper;
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000228 bool useEconomicTranslate;
Peter Åstrand43aa1a12005-02-21 09:58:31 +0000229
230 time_t lastUserInputTime;
231 time_t lastDisconnectTime;
232 time_t lastConnectionTime;
Oleg Sheikin5c642e92005-12-22 20:57:58 +0000233
234 bool disableclients;
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000235 };
236
237};
238
239#endif
240