blob: 47a4801972a559793469ee70ba49a17f247f4421 [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 Ossman04e62db2009-03-23 16:57:07 +000085 virtual void setPixelBuffer(PixelBuffer* pb, const ScreenSet& layout);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000086 virtual void setPixelBuffer(PixelBuffer* pb);
Pierre Ossman04e62db2009-03-23 16:57:07 +000087 virtual void setScreenLayout(const ScreenSet& layout);
Constantin Kaplinskyd7c0a6d2007-09-03 04:23:48 +000088 virtual PixelBuffer* getPixelBuffer() const { return pb; }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000089 virtual void setColourMapEntries(int firstColour=0, int nColours=0);
90 virtual void serverCutText(const char* str, int len);
91 virtual void add_changed(const Region &region);
92 virtual void add_copied(const Region &dest, const Point &delta);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000093 virtual void setCursor(int width, int height, const Point& hotspot,
94 void* cursorData, void* mask);
95 virtual void setCursorPos(const Point& p);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000096
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 // closeClients() closes all RFB sessions, except the specified one (if
106 // any), and logs the specified reason for closure.
107 void closeClients(const char* reason, network::Socket* sock);
108
109 // getSockets() gets a list of sockets. This can be used to generate an
110 // fd_set for calling select().
111
112 void getSockets(std::list<network::Socket*>* sockets);
113
114 // getSConnection() gets the SConnection for a particular Socket. If
115 // the Socket is not recognised then null is returned.
116
117 SConnection* getSConnection(network::Socket* sock);
118
119 // getDesktopSize() returns the size of the SDesktop exported by this
120 // server.
121 Point getDesktopSize() const {return desktop->getFbSize();}
122
123 // getName() returns the name of this VNC Server. NB: The value returned
124 // is the server's internal buffer which may change after any other methods
125 // are called - take a copy if necessary.
126 const char* getName() const {return name.buf;}
127
128 // setName() specifies the desktop name that the server should provide to
129 // clients
Peter Åstrandc39e0782009-01-15 12:21:42 +0000130 virtual void setName(const char* name_);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000131
132 // A QueryConnectionHandler, if supplied, is passed details of incoming
133 // connections to approve, reject, or query the user about.
134 //
135 // queryConnection() is called when a connection has been
136 // successfully authenticated. The sock and userName arguments identify
137 // the socket and the name of the authenticated user, if any. It should
138 // return ACCEPT if the connection should be accepted, REJECT if it should
139 // be rejected, or PENDING if a decision cannot yet be reached. If REJECT
140 // is returned, *reason can be set to a string describing the reason - this
141 // will be delete[]ed when it is finished with. If PENDING is returned,
142 // approveConnection() must be called some time later to accept or reject
143 // the connection.
144 enum queryResult { ACCEPT, REJECT, PENDING };
145 struct QueryConnectionHandler {
146 virtual ~QueryConnectionHandler() {}
147 virtual queryResult queryConnection(network::Socket* sock,
148 const char* userName,
149 char** reason) = 0;
150 };
151 void setQueryConnectionHandler(QueryConnectionHandler* qch) {
152 queryConnectionHandler = qch;
153 }
154
155 // queryConnection is called as described above, and either passes the
156 // request on to the registered handler, or accepts the connection if
157 // no handler has been specified.
158 virtual queryResult queryConnection(network::Socket* sock,
159 const char* userName,
160 char** reason) {
161 return queryConnectionHandler
162 ? queryConnectionHandler->queryConnection(sock, userName, reason)
163 : ACCEPT;
164 }
165
166 // approveConnection() is called by the active QueryConnectionHandler,
167 // some time after queryConnection() has returned with PENDING, to accept
168 // or reject the connection. The accept argument should be true for
169 // acceptance, or false for rejection, in which case a string reason may
170 // also be given.
171 void approveConnection(network::Socket* sock, bool accept,
172 const char* reason);
173
174 // setBlacklist() is called to replace the VNCServerST's internal
175 // Blacklist instance with another instance. This allows a single
176 // Blacklist to be shared by multiple VNCServerST instances.
177 void setBlacklist(Blacklist* bl) {blHosts = bl ? bl : &blacklist;}
178
179 // setEconomicTranslate() determines (for new connections) whether pixels
180 // should be translated for <=16bpp clients using a large lookup table
181 // (fast) or separate, smaller R, G and B tables (slower). If set to true,
182 // small tables are used, to save memory.
183 void setEconomicTranslate(bool et) { useEconomicTranslate = et; }
184
185 // setKeyRemapper() replaces the VNCServerST's default key remapper.
186 // NB: A null pointer is valid here.
187 void setKeyRemapper(KeyRemapper* kr) { keyRemapper = kr; }
188
189 void getConnInfo(ListConnInfo * listConn);
190 void setConnStatus(ListConnInfo* listConn);
191
192 bool getDisable() { return disableclients;};
193 void setDisable(bool disable) { disableclients = disable;};
194
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000195 protected:
196
197 friend class VNCSConnectionST;
198
Pierre Ossmanbbf955e2011-11-08 12:44:10 +0000199 // Timer callbacks
200 virtual bool handleTimeout(Timer* t);
201
202 // - Internal methods
203
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000204 void startDesktop();
205
206 static LogWriter connectionsLog;
207 Blacklist blacklist;
208 Blacklist* blHosts;
209
210 SDesktop* desktop;
211 bool desktopStarted;
212 PixelBuffer* pb;
Pierre Ossman34e62f32009-03-20 21:46:12 +0000213 ScreenSet screenLayout;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000214
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +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();
Pierre Ossmanbbf955e2011-11-08 12:44:10 +0000234 void startDefer();
235 bool checkDefer();
236 void tryUpdate();
237 bool checkUpdate();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000238
Pierre Ossman04e62db2009-03-23 16:57:07 +0000239 void notifyScreenLayoutChange(VNCSConnectionST *requester);
240
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000241 QueryConnectionHandler* queryConnectionHandler;
242 KeyRemapper* keyRemapper;
243 bool useEconomicTranslate;
244
245 time_t lastUserInputTime;
246 time_t lastDisconnectTime;
247 time_t lastConnectionTime;
248
249 bool disableclients;
Pierre Ossmanbbf955e2011-11-08 12:44:10 +0000250
251 Timer deferTimer;
252 bool deferPending;
253 struct timeval deferStart;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000254 };
255
256};
257
258#endif
259