blob: e75954fccb401dd2099214dab07ba0badb7edcd2 [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
Pierre Ossmanbbf955e2011-11-08 12:44:10 +000026#include <sys/time.h>
27
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000028#include <list>
29
30#include <rfb/SDesktop.h>
31#include <rfb/VNCServer.h>
32#include <rfb/Configuration.h>
33#include <rfb/LogWriter.h>
34#include <rfb/Blacklist.h>
35#include <rfb/Cursor.h>
Pierre Ossmanbbf955e2011-11-08 12:44:10 +000036#include <rfb/Timer.h>
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000037#include <network/Socket.h>
38#include <rfb/ListConnInfo.h>
Pierre Ossman34e62f32009-03-20 21:46:12 +000039#include <rfb/ScreenSet.h>
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000040
41namespace rfb {
42
43 class VNCSConnectionST;
44 class ComparingUpdateTracker;
45 class PixelBuffer;
46 class KeyRemapper;
47
Pierre Ossmanbbf955e2011-11-08 12:44:10 +000048 class VNCServerST : public VNCServer,
49 public Timer::Callback,
50 public network::SocketServer {
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000051 public:
52 // -=- Constructors
53
54 // Create a server exporting the supplied desktop.
Adam Tkaca6578bf2010-04-23 14:07:41 +000055 VNCServerST(const char* name_, SDesktop* desktop_);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000056 virtual ~VNCServerST();
57
58
59 // Methods overridden from SocketServer
60
61 // addSocket
62 // Causes the server to allocate an RFB-protocol management
63 // structure for the socket & initialise it.
64 virtual void addSocket(network::Socket* sock, bool outgoing=false);
65
66 // removeSocket
67 // Clean up any resources associated with the Socket
68 virtual void removeSocket(network::Socket* sock);
69
70 // processSocketEvent
71 // Read more RFB data from the Socket. If an error occurs during
72 // processing then shutdown() is called on the Socket, causing
73 // removeSocket() to be called by the caller at a later time.
74 virtual void processSocketEvent(network::Socket* sock);
75
76 // checkTimeouts
77 // Returns the number of milliseconds left until the next idle timeout
78 // expires. If any have already expired, the corresponding connections
79 // are closed. Zero is returned if there is no idle timeout.
80 virtual int checkTimeouts();
81
82
83 // Methods overridden from VNCServer
84
Pierre Ossman559a2e82012-01-23 15:54:11 +000085 virtual void blockUpdates();
86 virtual void unblockUpdates();
Pierre Ossman04e62db2009-03-23 16:57:07 +000087 virtual void setPixelBuffer(PixelBuffer* pb, const ScreenSet& layout);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000088 virtual void setPixelBuffer(PixelBuffer* pb);
Pierre Ossman04e62db2009-03-23 16:57:07 +000089 virtual void setScreenLayout(const ScreenSet& layout);
Constantin Kaplinskyd7c0a6d2007-09-03 04:23:48 +000090 virtual PixelBuffer* getPixelBuffer() const { return pb; }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000091 virtual void setColourMapEntries(int firstColour=0, int nColours=0);
92 virtual void serverCutText(const char* str, int len);
93 virtual void add_changed(const Region &region);
94 virtual void add_copied(const Region &dest, const Point &delta);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000095 virtual void setCursor(int width, int height, const Point& hotspot,
96 void* cursorData, void* mask);
97 virtual void setCursorPos(const Point& p);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000098
99 virtual void bell();
100
101 // - Close all currently-connected clients, by calling
102 // their close() method with the supplied reason.
103 virtual void closeClients(const char* reason) {closeClients(reason, 0);}
104
105 // VNCServerST-only methods
106
107 // closeClients() closes all RFB sessions, except the specified one (if
108 // any), and logs the specified reason for closure.
109 void closeClients(const char* reason, network::Socket* sock);
110
111 // getSockets() gets a list of sockets. This can be used to generate an
112 // fd_set for calling select().
113
114 void getSockets(std::list<network::Socket*>* sockets);
115
116 // getSConnection() gets the SConnection for a particular Socket. If
117 // the Socket is not recognised then null is returned.
118
119 SConnection* getSConnection(network::Socket* sock);
120
121 // getDesktopSize() returns the size of the SDesktop exported by this
122 // server.
123 Point getDesktopSize() const {return desktop->getFbSize();}
124
125 // getName() returns the name of this VNC Server. NB: The value returned
126 // is the server's internal buffer which may change after any other methods
127 // are called - take a copy if necessary.
128 const char* getName() const {return name.buf;}
129
130 // setName() specifies the desktop name that the server should provide to
131 // clients
Peter Åstrandc39e0782009-01-15 12:21:42 +0000132 virtual void setName(const char* name_);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000133
134 // A QueryConnectionHandler, if supplied, is passed details of incoming
135 // connections to approve, reject, or query the user about.
136 //
137 // queryConnection() is called when a connection has been
138 // successfully authenticated. The sock and userName arguments identify
139 // the socket and the name of the authenticated user, if any. It should
140 // return ACCEPT if the connection should be accepted, REJECT if it should
141 // be rejected, or PENDING if a decision cannot yet be reached. If REJECT
142 // is returned, *reason can be set to a string describing the reason - this
143 // will be delete[]ed when it is finished with. If PENDING is returned,
144 // approveConnection() must be called some time later to accept or reject
145 // the connection.
146 enum queryResult { ACCEPT, REJECT, PENDING };
147 struct QueryConnectionHandler {
148 virtual ~QueryConnectionHandler() {}
149 virtual queryResult queryConnection(network::Socket* sock,
150 const char* userName,
151 char** reason) = 0;
152 };
153 void setQueryConnectionHandler(QueryConnectionHandler* qch) {
154 queryConnectionHandler = qch;
155 }
156
157 // queryConnection is called as described above, and either passes the
158 // request on to the registered handler, or accepts the connection if
159 // no handler has been specified.
160 virtual queryResult queryConnection(network::Socket* sock,
161 const char* userName,
162 char** reason) {
163 return queryConnectionHandler
164 ? queryConnectionHandler->queryConnection(sock, userName, reason)
165 : ACCEPT;
166 }
167
168 // approveConnection() is called by the active QueryConnectionHandler,
169 // some time after queryConnection() has returned with PENDING, to accept
170 // or reject the connection. The accept argument should be true for
171 // acceptance, or false for rejection, in which case a string reason may
172 // also be given.
173 void approveConnection(network::Socket* sock, bool accept,
174 const char* reason);
175
176 // setBlacklist() is called to replace the VNCServerST's internal
177 // Blacklist instance with another instance. This allows a single
178 // Blacklist to be shared by multiple VNCServerST instances.
179 void setBlacklist(Blacklist* bl) {blHosts = bl ? bl : &blacklist;}
180
181 // setEconomicTranslate() determines (for new connections) whether pixels
182 // should be translated for <=16bpp clients using a large lookup table
183 // (fast) or separate, smaller R, G and B tables (slower). If set to true,
184 // small tables are used, to save memory.
185 void setEconomicTranslate(bool et) { useEconomicTranslate = et; }
186
187 // setKeyRemapper() replaces the VNCServerST's default key remapper.
188 // NB: A null pointer is valid here.
189 void setKeyRemapper(KeyRemapper* kr) { keyRemapper = kr; }
190
191 void getConnInfo(ListConnInfo * listConn);
192 void setConnStatus(ListConnInfo* listConn);
193
194 bool getDisable() { return disableclients;};
195 void setDisable(bool disable) { disableclients = disable;};
196
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000197 protected:
198
199 friend class VNCSConnectionST;
200
Pierre Ossmanbbf955e2011-11-08 12:44:10 +0000201 // Timer callbacks
202 virtual bool handleTimeout(Timer* t);
203
204 // - Internal methods
205
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000206 void startDesktop();
207
208 static LogWriter connectionsLog;
209 Blacklist blacklist;
210 Blacklist* blHosts;
211
212 SDesktop* desktop;
213 bool desktopStarted;
Pierre Ossman559a2e82012-01-23 15:54:11 +0000214 int blockCounter;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000215 PixelBuffer* pb;
Pierre Ossman34e62f32009-03-20 21:46:12 +0000216 ScreenSet screenLayout;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000217
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000218 CharArray name;
219
220 std::list<VNCSConnectionST*> clients;
221 VNCSConnectionST* pointerClient;
222 std::list<network::Socket*> closingSockets;
223
224 ComparingUpdateTracker* comparer;
225
226 Point cursorPos;
227 Cursor cursor;
228 Point cursorTL() { return cursorPos.subtract(cursor.hotspot); }
229 Point renderedCursorTL;
230 ManagedPixelBuffer renderedCursor;
231 bool renderedCursorInvalid;
232
233 // - Check how many of the clients are authenticated.
234 int authClientCount();
235
236 bool needRenderedCursor();
Pierre Ossmanbbf955e2011-11-08 12:44:10 +0000237 void startDefer();
238 bool checkDefer();
239 void tryUpdate();
240 bool checkUpdate();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000241
Pierre Ossman04e62db2009-03-23 16:57:07 +0000242 void notifyScreenLayoutChange(VNCSConnectionST *requester);
243
Pierre Ossmanb114cec2011-11-20 15:36:11 +0000244 bool getComparerState();
245
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000246 QueryConnectionHandler* queryConnectionHandler;
247 KeyRemapper* keyRemapper;
248 bool useEconomicTranslate;
249
250 time_t lastUserInputTime;
251 time_t lastDisconnectTime;
252 time_t lastConnectionTime;
253
254 bool disableclients;
Pierre Ossmanbbf955e2011-11-08 12:44:10 +0000255
256 Timer deferTimer;
257 bool deferPending;
258 struct timeval deferStart;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000259 };
260
261};
262
263#endif
264