blob: 9d25268910d82b0741c68378be43844bf0cafcd8 [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>
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000031#include <rfb/LogWriter.h>
32#include <rfb/Blacklist.h>
33#include <rfb/Cursor.h>
Pierre Ossmanbbf955e2011-11-08 12:44:10 +000034#include <rfb/Timer.h>
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000035#include <network/Socket.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;
Pierre Ossman707fa122015-12-11 20:21:20 +010042 class ListConnInfo;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000043 class PixelBuffer;
44 class KeyRemapper;
45
Pierre Ossmanbbf955e2011-11-08 12:44:10 +000046 class VNCServerST : public VNCServer,
47 public Timer::Callback,
48 public network::SocketServer {
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000049 public:
50 // -=- Constructors
51
52 // Create a server exporting the supplied desktop.
Adam Tkaca6578bf2010-04-23 14:07:41 +000053 VNCServerST(const char* name_, SDesktop* desktop_);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000054 virtual ~VNCServerST();
55
56
57 // Methods overridden from SocketServer
58
59 // addSocket
60 // Causes the server to allocate an RFB-protocol management
61 // structure for the socket & initialise it.
62 virtual void addSocket(network::Socket* sock, bool outgoing=false);
63
64 // removeSocket
65 // Clean up any resources associated with the Socket
66 virtual void removeSocket(network::Socket* sock);
67
Pierre Ossman574dc642016-10-05 13:39:11 +020068 // getSockets() gets a list of sockets. This can be used to generate an
69 // fd_set for calling select().
70 virtual void getSockets(std::list<network::Socket*>* sockets);
71
Pierre Ossmand408ca52016-04-29 14:26:05 +020072 // processSocketReadEvent
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000073 // Read more RFB data from the Socket. If an error occurs during
74 // processing then shutdown() is called on the Socket, causing
75 // removeSocket() to be called by the caller at a later time.
Pierre Ossmand408ca52016-04-29 14:26:05 +020076 virtual void processSocketReadEvent(network::Socket* sock);
77
78 // processSocketWriteEvent
79 // Flush pending data from the Socket on to the network.
80 virtual void processSocketWriteEvent(network::Socket* sock);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000081
82 // checkTimeouts
83 // Returns the number of milliseconds left until the next idle timeout
84 // expires. If any have already expired, the corresponding connections
85 // are closed. Zero is returned if there is no idle timeout.
86 virtual int checkTimeouts();
87
88
89 // Methods overridden from VNCServer
90
Pierre Ossman559a2e82012-01-23 15:54:11 +000091 virtual void blockUpdates();
92 virtual void unblockUpdates();
Pierre Ossman04e62db2009-03-23 16:57:07 +000093 virtual void setPixelBuffer(PixelBuffer* pb, const ScreenSet& layout);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000094 virtual void setPixelBuffer(PixelBuffer* pb);
Pierre Ossman04e62db2009-03-23 16:57:07 +000095 virtual void setScreenLayout(const ScreenSet& layout);
Constantin Kaplinskyd7c0a6d2007-09-03 04:23:48 +000096 virtual PixelBuffer* getPixelBuffer() const { return pb; }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000097 virtual void serverCutText(const char* str, int len);
98 virtual void add_changed(const Region &region);
99 virtual void add_copied(const Region &dest, const Point &delta);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000100 virtual void setCursor(int width, int height, const Point& hotspot,
Pierre Ossman6a1a0d02017-02-19 15:48:17 +0100101 const rdr::U8* data);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000102 virtual void setCursorPos(const Point& p);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000103
104 virtual void bell();
105
106 // - Close all currently-connected clients, by calling
107 // their close() method with the supplied reason.
108 virtual void closeClients(const char* reason) {closeClients(reason, 0);}
109
110 // VNCServerST-only methods
111
112 // closeClients() closes all RFB sessions, except the specified one (if
113 // any), and logs the specified reason for closure.
114 void closeClients(const char* reason, network::Socket* sock);
115
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000116 // 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
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000181 // setKeyRemapper() replaces the VNCServerST's default key remapper.
182 // NB: A null pointer is valid here.
183 void setKeyRemapper(KeyRemapper* kr) { keyRemapper = kr; }
184
185 void getConnInfo(ListConnInfo * listConn);
186 void setConnStatus(ListConnInfo* listConn);
187
188 bool getDisable() { return disableclients;};
189 void setDisable(bool disable) { disableclients = disable;};
190
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000191 protected:
192
193 friend class VNCSConnectionST;
194
Pierre Ossmanbbf955e2011-11-08 12:44:10 +0000195 // Timer callbacks
196 virtual bool handleTimeout(Timer* t);
197
198 // - Internal methods
199
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000200 void startDesktop();
201
202 static LogWriter connectionsLog;
203 Blacklist blacklist;
204 Blacklist* blHosts;
205
206 SDesktop* desktop;
207 bool desktopStarted;
Pierre Ossman559a2e82012-01-23 15:54:11 +0000208 int blockCounter;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000209 PixelBuffer* pb;
Pierre Ossman34e62f32009-03-20 21:46:12 +0000210 ScreenSet screenLayout;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000211
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000212 CharArray name;
213
214 std::list<VNCSConnectionST*> clients;
215 VNCSConnectionST* pointerClient;
216 std::list<network::Socket*> closingSockets;
217
218 ComparingUpdateTracker* comparer;
219
220 Point cursorPos;
Pierre Ossman6a1a0d02017-02-19 15:48:17 +0100221 Cursor* cursor;
Pierre Ossman6ea6e1a2014-02-12 16:33:43 +0100222 RenderedCursor renderedCursor;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000223 bool renderedCursorInvalid;
224
225 // - Check how many of the clients are authenticated.
226 int authClientCount();
227
228 bool needRenderedCursor();
Pierre Ossman6e49e952016-10-07 15:59:38 +0200229 void startFrameClock();
230 void stopFrameClock();
231 void writeUpdate();
Pierre Ossmanbbf955e2011-11-08 12:44:10 +0000232 bool checkUpdate();
Pierre Ossman24684e52016-12-05 16:58:19 +0100233 const RenderedCursor* getRenderedCursor();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000234
Pierre Ossman04e62db2009-03-23 16:57:07 +0000235 void notifyScreenLayoutChange(VNCSConnectionST *requester);
236
Pierre Ossmanb114cec2011-11-20 15:36:11 +0000237 bool getComparerState();
238
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000239 QueryConnectionHandler* queryConnectionHandler;
240 KeyRemapper* keyRemapper;
Pierre Ossman0c9bd4b2014-07-09 16:44:11 +0200241
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000242 time_t lastUserInputTime;
243 time_t lastDisconnectTime;
244 time_t lastConnectionTime;
245
246 bool disableclients;
Pierre Ossmanbbf955e2011-11-08 12:44:10 +0000247
Pierre Ossman6e49e952016-10-07 15:59:38 +0200248 Timer frameTimer;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000249 };
250
251};
252
253#endif
254