blob: 2dfdbbdbcff984e5f79940289103a18c19de2736 [file] [log] [blame]
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001/* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved.
Pierre Ossman24684e52016-12-05 16:58:19 +01002 * Copyright 2009-2016 Pierre Ossman for Cendio AB
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00003 *
4 * This is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This software is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this software; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
17 * USA.
18 */
19
20// -=- VNCServerST.h
21
22// Single-threaded VNCServer implementation
23
24#ifndef __RFB_VNCSERVERST_H__
25#define __RFB_VNCSERVERST_H__
26
Pierre Ossmanbbf955e2011-11-08 12:44:10 +000027#include <sys/time.h>
28
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000029#include <rfb/SDesktop.h>
30#include <rfb/VNCServer.h>
31#include <rfb/Configuration.h>
32#include <rfb/LogWriter.h>
33#include <rfb/Blacklist.h>
34#include <rfb/Cursor.h>
Pierre Ossmanbbf955e2011-11-08 12:44:10 +000035#include <rfb/Timer.h>
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000036#include <network/Socket.h>
37#include <rfb/ListConnInfo.h>
Pierre Ossman34e62f32009-03-20 21:46:12 +000038#include <rfb/ScreenSet.h>
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000039
40namespace rfb {
41
42 class VNCSConnectionST;
43 class ComparingUpdateTracker;
44 class PixelBuffer;
45 class KeyRemapper;
46
Pierre Ossmanbbf955e2011-11-08 12:44:10 +000047 class VNCServerST : public VNCServer,
48 public Timer::Callback,
49 public network::SocketServer {
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000050 public:
51 // -=- Constructors
52
53 // Create a server exporting the supplied desktop.
Adam Tkaca6578bf2010-04-23 14:07:41 +000054 VNCServerST(const char* name_, SDesktop* desktop_);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000055 virtual ~VNCServerST();
56
57
58 // Methods overridden from SocketServer
59
60 // addSocket
61 // Causes the server to allocate an RFB-protocol management
62 // structure for the socket & initialise it.
63 virtual void addSocket(network::Socket* sock, bool outgoing=false);
64
65 // removeSocket
66 // Clean up any resources associated with the Socket
67 virtual void removeSocket(network::Socket* sock);
68
Pierre Ossman574dc642016-10-05 13:39:11 +020069 // getSockets() gets a list of sockets. This can be used to generate an
70 // fd_set for calling select().
71 virtual void getSockets(std::list<network::Socket*>* sockets);
72
Pierre Ossmand408ca52016-04-29 14:26:05 +020073 // processSocketReadEvent
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000074 // Read more RFB data from the Socket. If an error occurs during
75 // processing then shutdown() is called on the Socket, causing
76 // removeSocket() to be called by the caller at a later time.
Pierre Ossmand408ca52016-04-29 14:26:05 +020077 virtual void processSocketReadEvent(network::Socket* sock);
78
79 // processSocketWriteEvent
80 // Flush pending data from the Socket on to the network.
81 virtual void processSocketWriteEvent(network::Socket* sock);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000082
83 // checkTimeouts
84 // Returns the number of milliseconds left until the next idle timeout
85 // expires. If any have already expired, the corresponding connections
86 // are closed. Zero is returned if there is no idle timeout.
87 virtual int checkTimeouts();
88
89
90 // Methods overridden from VNCServer
91
Pierre Ossman559a2e82012-01-23 15:54:11 +000092 virtual void blockUpdates();
93 virtual void unblockUpdates();
Pierre Ossman04e62db2009-03-23 16:57:07 +000094 virtual void setPixelBuffer(PixelBuffer* pb, const ScreenSet& layout);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000095 virtual void setPixelBuffer(PixelBuffer* pb);
Pierre Ossman04e62db2009-03-23 16:57:07 +000096 virtual void setScreenLayout(const ScreenSet& layout);
Constantin Kaplinskyd7c0a6d2007-09-03 04:23:48 +000097 virtual PixelBuffer* getPixelBuffer() const { return pb; }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000098 virtual void serverCutText(const char* str, int len);
99 virtual void add_changed(const Region &region);
100 virtual void add_copied(const Region &dest, const Point &delta);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000101 virtual void setCursor(int width, int height, const Point& hotspot,
Pierre Ossman6a1a0d02017-02-19 15:48:17 +0100102 const rdr::U8* data);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000103 virtual void setCursorPos(const Point& p);
Pierre Ossmanbb305ca2016-12-11 12:41:26 +0100104 virtual void setLEDState(unsigned state);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000105
106 virtual void bell();
107
108 // - Close all currently-connected clients, by calling
109 // their close() method with the supplied reason.
110 virtual void closeClients(const char* reason) {closeClients(reason, 0);}
111
112 // VNCServerST-only methods
113
114 // closeClients() closes all RFB sessions, except the specified one (if
115 // any), and logs the specified reason for closure.
116 void closeClients(const char* reason, network::Socket* sock);
117
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000118 // getSConnection() gets the SConnection for a particular Socket. If
119 // the Socket is not recognised then null is returned.
120
121 SConnection* getSConnection(network::Socket* sock);
122
123 // getDesktopSize() returns the size of the SDesktop exported by this
124 // server.
125 Point getDesktopSize() const {return desktop->getFbSize();}
126
127 // getName() returns the name of this VNC Server. NB: The value returned
128 // is the server's internal buffer which may change after any other methods
129 // are called - take a copy if necessary.
130 const char* getName() const {return name.buf;}
131
132 // setName() specifies the desktop name that the server should provide to
133 // clients
Peter Åstrandc39e0782009-01-15 12:21:42 +0000134 virtual void setName(const char* name_);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000135
136 // A QueryConnectionHandler, if supplied, is passed details of incoming
137 // connections to approve, reject, or query the user about.
138 //
139 // queryConnection() is called when a connection has been
140 // successfully authenticated. The sock and userName arguments identify
141 // the socket and the name of the authenticated user, if any. It should
142 // return ACCEPT if the connection should be accepted, REJECT if it should
143 // be rejected, or PENDING if a decision cannot yet be reached. If REJECT
144 // is returned, *reason can be set to a string describing the reason - this
145 // will be delete[]ed when it is finished with. If PENDING is returned,
146 // approveConnection() must be called some time later to accept or reject
147 // the connection.
148 enum queryResult { ACCEPT, REJECT, PENDING };
149 struct QueryConnectionHandler {
150 virtual ~QueryConnectionHandler() {}
151 virtual queryResult queryConnection(network::Socket* sock,
152 const char* userName,
153 char** reason) = 0;
154 };
155 void setQueryConnectionHandler(QueryConnectionHandler* qch) {
156 queryConnectionHandler = qch;
157 }
158
159 // queryConnection is called as described above, and either passes the
160 // request on to the registered handler, or accepts the connection if
161 // no handler has been specified.
162 virtual queryResult queryConnection(network::Socket* sock,
163 const char* userName,
164 char** reason) {
165 return queryConnectionHandler
166 ? queryConnectionHandler->queryConnection(sock, userName, reason)
167 : ACCEPT;
168 }
169
170 // approveConnection() is called by the active QueryConnectionHandler,
171 // some time after queryConnection() has returned with PENDING, to accept
172 // or reject the connection. The accept argument should be true for
173 // acceptance, or false for rejection, in which case a string reason may
174 // also be given.
175 void approveConnection(network::Socket* sock, bool accept,
176 const char* reason);
177
178 // setBlacklist() is called to replace the VNCServerST's internal
179 // Blacklist instance with another instance. This allows a single
180 // Blacklist to be shared by multiple VNCServerST instances.
181 void setBlacklist(Blacklist* bl) {blHosts = bl ? bl : &blacklist;}
182
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000183 // setKeyRemapper() replaces the VNCServerST's default key remapper.
184 // NB: A null pointer is valid here.
185 void setKeyRemapper(KeyRemapper* kr) { keyRemapper = kr; }
186
187 void getConnInfo(ListConnInfo * listConn);
188 void setConnStatus(ListConnInfo* listConn);
189
190 bool getDisable() { return disableclients;};
191 void setDisable(bool disable) { disableclients = disable;};
192
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000193 protected:
194
195 friend class VNCSConnectionST;
196
Pierre Ossmanbbf955e2011-11-08 12:44:10 +0000197 // Timer callbacks
198 virtual bool handleTimeout(Timer* t);
199
200 // - Internal methods
201
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000202 void startDesktop();
203
204 static LogWriter connectionsLog;
205 Blacklist blacklist;
206 Blacklist* blHosts;
207
208 SDesktop* desktop;
209 bool desktopStarted;
Pierre Ossman559a2e82012-01-23 15:54:11 +0000210 int blockCounter;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000211 PixelBuffer* pb;
Pierre Ossman34e62f32009-03-20 21:46:12 +0000212 ScreenSet screenLayout;
Pierre Ossmanbb305ca2016-12-11 12:41:26 +0100213 unsigned int ledState;
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;
Pierre Ossman6a1a0d02017-02-19 15:48:17 +0100224 Cursor* cursor;
Pierre Ossman6ea6e1a2014-02-12 16:33:43 +0100225 RenderedCursor renderedCursor;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000226 bool renderedCursorInvalid;
227
228 // - Check how many of the clients are authenticated.
229 int authClientCount();
230
231 bool needRenderedCursor();
Pierre Ossman6e49e952016-10-07 15:59:38 +0200232 void startFrameClock();
233 void stopFrameClock();
234 void writeUpdate();
Pierre Ossmanbbf955e2011-11-08 12:44:10 +0000235 bool checkUpdate();
Pierre Ossman24684e52016-12-05 16:58:19 +0100236 const RenderedCursor* getRenderedCursor();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000237
Pierre Ossman04e62db2009-03-23 16:57:07 +0000238 void notifyScreenLayoutChange(VNCSConnectionST *requester);
239
Pierre Ossmanb114cec2011-11-20 15:36:11 +0000240 bool getComparerState();
241
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000242 QueryConnectionHandler* queryConnectionHandler;
243 KeyRemapper* keyRemapper;
Pierre Ossman0c9bd4b2014-07-09 16:44:11 +0200244
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000245 time_t lastUserInputTime;
246 time_t lastDisconnectTime;
247 time_t lastConnectionTime;
248
249 bool disableclients;
Pierre Ossmanbbf955e2011-11-08 12:44:10 +0000250
Pierre Ossman6e49e952016-10-07 15:59:38 +0200251 Timer frameTimer;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000252 };
253
254};
255
256#endif
257