blob: 6c80569db09122f4df1f5668e82103b559c34552 [file] [log] [blame]
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001/* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved.
Pierre Ossman615d16b2019-05-03 10:53:06 +02002 * Copyright 2011-2019 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// SConnection - class on the server side representing a connection to a
21// client. A derived class should override methods appropriately.
22//
23
24#ifndef __RFB_SCONNECTION_H__
25#define __RFB_SCONNECTION_H__
26
27#include <rdr/InStream.h>
28#include <rdr/OutStream.h>
29#include <rfb/SMsgHandler.h>
Adam Tkacbfd66c12010-10-01 08:33:29 +000030#include <rfb/SecurityServer.h>
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000031
32namespace rfb {
33
34 class SMsgReader;
35 class SMsgWriter;
36 class SSecurity;
37
38 class SConnection : public SMsgHandler {
39 public:
40
Pierre Ossman7069bdd2015-02-06 14:41:58 +010041 SConnection();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000042 virtual ~SConnection();
43
44 // Methods to initialise the connection
45
46 // setStreams() sets the streams to be used for the connection. These must
47 // be set before initialiseProtocol() and processMsg() are called. The
48 // SSecurity object may call setStreams() again to provide alternative
49 // streams over which the RFB protocol is sent (i.e. encrypting/decrypting
50 // streams). Ownership of the streams remains with the caller
51 // (i.e. SConnection will not delete them).
52 void setStreams(rdr::InStream* is, rdr::OutStream* os);
53
54 // initialiseProtocol() should be called once the streams and security
55 // types are set. Subsequently, processMsg() should be called whenever
56 // there is data to read on the InStream.
57 void initialiseProtocol();
58
59 // processMsg() should be called whenever there is data to read on the
60 // InStream. You must have called initialiseProtocol() first.
61 void processMsg();
62
63 // approveConnection() is called to either accept or reject the connection.
64 // If accept is false, the reason string gives the reason for the
65 // rejection. It can either be called directly from queryConnection() or
66 // later, after queryConnection() has returned. It can only be called when
67 // in state RFBSTATE_QUERYING. On rejection, an AuthFailureException is
68 // thrown, so this must be handled appropriately by the caller.
69 void approveConnection(bool accept, const char* reason=0);
70
71
Pierre Ossman7d64b332018-10-08 15:59:02 +020072 // Methods to terminate the connection
73
74 // close() shuts down the connection to the client and awaits
75 // cleanup of the SConnection object by the server
76 virtual void close(const char* reason);
77
78
Pierre Ossman48700812014-09-17 17:11:56 +020079 // Overridden from SMsgHandler
80
Pierre Ossmanf38e2432015-02-11 13:47:58 +010081 virtual void setEncodings(int nEncodings, const rdr::S32* encodings);
Pierre Ossman48700812014-09-17 17:11:56 +020082
Pierre Ossman615d16b2019-05-03 10:53:06 +020083 virtual void clientCutText(const char* str);
84
Pierre Ossman5ae28212017-05-16 14:30:38 +020085 virtual void supportsQEMUKeyEvent();
Pierre Ossman48700812014-09-17 17:11:56 +020086
Pierre Ossman615d16b2019-05-03 10:53:06 +020087
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000088 // Methods to be overridden in a derived class
89
90 // versionReceived() indicates that the version number has just been read
91 // from the client. The version will already have been "cooked"
92 // to deal with unknown/bogus viewer protocol numbers.
93 virtual void versionReceived();
94
95 // authSuccess() is called when authentication has succeeded.
96 virtual void authSuccess();
97
Pierre Ossman88a94ed2019-04-01 14:22:01 +020098 // authFailure() is called when authentication has failed. The default
99 // implementation will inform the client and throw a AuthFailureException.
100 virtual void authFailure(const char* reason);
101
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000102 // queryConnection() is called when authentication has succeeded, but
103 // before informing the client. It can be overridden to query a local user
104 // to accept the incoming connection, for example. The userName argument
105 // is the name of the user making the connection, or null (note that the
106 // storage for userName is owned by the caller). The connection must be
107 // accepted or rejected by calling approveConnection(), either directly
108 // from queryConnection() or some time later.
109 virtual void queryConnection(const char* userName);
110
111 // clientInit() is called when the ClientInit message is received. The
112 // derived class must call on to SConnection::clientInit().
113 virtual void clientInit(bool shared);
114
115 // setPixelFormat() is called when a SetPixelFormat message is received.
116 // The derived class must call on to SConnection::setPixelFormat().
117 virtual void setPixelFormat(const PixelFormat& pf);
118
119 // framebufferUpdateRequest() is called when a FramebufferUpdateRequest
120 // message is received. The derived class must call on to
121 // SConnection::framebufferUpdateRequest().
122 virtual void framebufferUpdateRequest(const Rect& r, bool incremental);
123
Pierre Ossmanc754cce2011-11-14 15:44:11 +0000124 // fence() is called when we get a fence request or response. By default
125 // it responds directly to requests (stating it doesn't support any
126 // synchronisation) and drops responses. Override to implement more proper
127 // support.
128 virtual void fence(rdr::U32 flags, unsigned len, const char data[]);
129
Pierre Ossmanc898d9a2011-11-14 16:22:23 +0000130 // enableContinuousUpdates() is called when the client wants to enable
131 // or disable continuous updates, or change the active area.
132 virtual void enableContinuousUpdates(bool enable,
133 int x, int y, int w, int h);
134
Pierre Ossman615d16b2019-05-03 10:53:06 +0200135 // handleClipboardRequest() is called whenever the client requests
136 // the server to send over its clipboard data. It will only be
137 // called after the server has first announced a clipboard change
138 // via announceClipboard().
139 virtual void handleClipboardRequest();
140
141 // handleClipboardAnnounce() is called to indicate a change in the
142 // clipboard on the client. Call requestClipboard() to access the
143 // actual data.
144 virtual void handleClipboardAnnounce(bool available);
145
146 // handleClipboardData() is called when the client has sent over
147 // the clipboard data as a result of a previous call to
148 // requestClipboard(). Note that this function might never be
149 // called if the clipboard data was no longer available when the
150 // client received the request.
151 virtual void handleClipboardData(const char* data);
152
153
Pierre Ossman7d64b332018-10-08 15:59:02 +0200154 // Other methods
155
Pierre Ossman615d16b2019-05-03 10:53:06 +0200156 // requestClipboard() will result in a request to the client to
157 // transfer its clipboard data. A call to handleClipboardData()
158 // will be made once the data is available.
159 virtual void requestClipboard();
160
161 // announceClipboard() informs the client of changes to the
162 // clipboard on the server. The client may later request the
163 // clipboard data via handleClipboardRequest().
164 virtual void announceClipboard(bool available);
165
166 // sendClipboardData() transfers the clipboard data to the client
167 // and should be called whenever the client has requested the
168 // clipboard via handleClipboardRequest().
169 virtual void sendClipboardData(const char* data);
170
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000171 // setAccessRights() allows a security package to limit the access rights
Pierre Ossman7d64b332018-10-08 15:59:02 +0200172 // of a SConnection to the server. How the access rights are treated
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000173 // is up to the derived class.
174
175 typedef rdr::U16 AccessRights;
Michal Srbb318b8f2014-11-24 13:18:28 +0200176 static const AccessRights AccessView; // View display contents
177 static const AccessRights AccessKeyEvents; // Send key events
178 static const AccessRights AccessPtrEvents; // Send pointer events
179 static const AccessRights AccessCutText; // Send/receive clipboard events
180 static const AccessRights AccessSetDesktopSize; // Change desktop size
Pierre Ossmane7be49b2014-12-02 14:33:17 +0100181 static const AccessRights AccessNonShared; // Exclusive access to the server
Michal Srbb318b8f2014-11-24 13:18:28 +0200182 static const AccessRights AccessDefault; // The default rights, INCLUDING FUTURE ONES
183 static const AccessRights AccessNoQuery; // Connect without local user accepting
184 static const AccessRights AccessFull; // All of the available AND FUTURE rights
Pierre Ossman7d64b332018-10-08 15:59:02 +0200185 virtual void setAccessRights(AccessRights ar);
186 virtual bool accessCheck(AccessRights ar) const;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000187
188 // authenticated() returns true if the client has authenticated
189 // successfully.
190 bool authenticated() { return (state_ == RFBSTATE_INITIALISATION ||
191 state_ == RFBSTATE_NORMAL); }
192
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000193 SMsgReader* reader() { return reader_; }
194 SMsgWriter* writer() { return writer_; }
195
196 rdr::InStream* getInStream() { return is; }
197 rdr::OutStream* getOutStream() { return os; }
198
199 enum stateEnum {
200 RFBSTATE_UNINITIALISED,
201 RFBSTATE_PROTOCOL_VERSION,
202 RFBSTATE_SECURITY_TYPE,
203 RFBSTATE_SECURITY,
Pierre Ossman88a94ed2019-04-01 14:22:01 +0200204 RFBSTATE_SECURITY_FAILURE,
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000205 RFBSTATE_QUERYING,
206 RFBSTATE_INITIALISATION,
207 RFBSTATE_NORMAL,
208 RFBSTATE_CLOSING,
209 RFBSTATE_INVALID
210 };
211
212 stateEnum state() { return state_; }
213
Pierre Ossman48700812014-09-17 17:11:56 +0200214 rdr::S32 getPreferredEncoding() { return preferredEncoding; }
215
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000216 protected:
Pierre Ossman2ebed0d2018-10-11 07:54:12 +0200217 // throwConnFailedException() prints a message to the log, sends a conn
218 // failed message to the client (if possible) and throws a
219 // ConnFailedException.
220 void throwConnFailedException(const char* format, ...) __printf_attr(2, 3);
221
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000222 void setState(stateEnum s) { state_ = s; }
Pierre Ossmanb7acf862015-02-06 14:44:32 +0100223
Pierre Ossman0144c532015-02-04 14:10:43 +0100224 void setReader(SMsgReader *r) { reader_ = r; }
225 void setWriter(SMsgWriter *w) { writer_ = w; }
226
Pierre Ossmanb7acf862015-02-06 14:44:32 +0100227 private:
Pierre Ossmanb6b4dc62014-01-20 15:05:21 +0100228 void writeFakeColourMap(void);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000229
230 bool readyForSetColourMapEntries;
231
232 void processVersionMsg();
233 void processSecurityTypeMsg();
Constantin Kaplinsky5fa9d222006-09-06 10:32:06 +0000234 void processSecurityType(int secType);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000235 void processSecurityMsg();
236 void processInitMsg();
237
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000238 int defaultMajorVersion, defaultMinorVersion;
239 rdr::InStream* is;
240 rdr::OutStream* os;
241 SMsgReader* reader_;
242 SMsgWriter* writer_;
Michal Srbdccb5f72017-03-27 13:55:46 +0300243 SecurityServer security;
Adam Tkaca6578bf2010-04-23 14:07:41 +0000244 SSecurity* ssecurity;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000245 stateEnum state_;
Pierre Ossman48700812014-09-17 17:11:56 +0200246 rdr::S32 preferredEncoding;
Pierre Ossman7d64b332018-10-08 15:59:02 +0200247 AccessRights accessRights;
Pierre Ossman615d16b2019-05-03 10:53:06 +0200248
249 char* clientClipboard;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000250 };
251}
252#endif