blob: 26302403fcb0e6d951e2023a34fa4b28452c3809 [file] [log] [blame]
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001/* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved.
Pierre Ossmanc754cce2011-11-14 15:44:11 +00002 * Copyright 2011 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 Ossman5ae28212017-05-16 14:30:38 +020083 virtual void supportsQEMUKeyEvent();
Pierre Ossman48700812014-09-17 17:11:56 +020084
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000085 // Methods to be overridden in a derived class
86
87 // versionReceived() indicates that the version number has just been read
88 // from the client. The version will already have been "cooked"
89 // to deal with unknown/bogus viewer protocol numbers.
90 virtual void versionReceived();
91
92 // authSuccess() is called when authentication has succeeded.
93 virtual void authSuccess();
94
95 // queryConnection() is called when authentication has succeeded, but
96 // before informing the client. It can be overridden to query a local user
97 // to accept the incoming connection, for example. The userName argument
98 // is the name of the user making the connection, or null (note that the
99 // storage for userName is owned by the caller). The connection must be
100 // accepted or rejected by calling approveConnection(), either directly
101 // from queryConnection() or some time later.
102 virtual void queryConnection(const char* userName);
103
104 // clientInit() is called when the ClientInit message is received. The
105 // derived class must call on to SConnection::clientInit().
106 virtual void clientInit(bool shared);
107
108 // setPixelFormat() is called when a SetPixelFormat message is received.
109 // The derived class must call on to SConnection::setPixelFormat().
110 virtual void setPixelFormat(const PixelFormat& pf);
111
112 // framebufferUpdateRequest() is called when a FramebufferUpdateRequest
113 // message is received. The derived class must call on to
114 // SConnection::framebufferUpdateRequest().
115 virtual void framebufferUpdateRequest(const Rect& r, bool incremental);
116
Pierre Ossmanc754cce2011-11-14 15:44:11 +0000117 // fence() is called when we get a fence request or response. By default
118 // it responds directly to requests (stating it doesn't support any
119 // synchronisation) and drops responses. Override to implement more proper
120 // support.
121 virtual void fence(rdr::U32 flags, unsigned len, const char data[]);
122
Pierre Ossmanc898d9a2011-11-14 16:22:23 +0000123 // enableContinuousUpdates() is called when the client wants to enable
124 // or disable continuous updates, or change the active area.
125 virtual void enableContinuousUpdates(bool enable,
126 int x, int y, int w, int h);
127
Pierre Ossman7d64b332018-10-08 15:59:02 +0200128 // Other methods
129
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000130 // setAccessRights() allows a security package to limit the access rights
Pierre Ossman7d64b332018-10-08 15:59:02 +0200131 // of a SConnection to the server. How the access rights are treated
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000132 // is up to the derived class.
133
134 typedef rdr::U16 AccessRights;
Michal Srbb318b8f2014-11-24 13:18:28 +0200135 static const AccessRights AccessView; // View display contents
136 static const AccessRights AccessKeyEvents; // Send key events
137 static const AccessRights AccessPtrEvents; // Send pointer events
138 static const AccessRights AccessCutText; // Send/receive clipboard events
139 static const AccessRights AccessSetDesktopSize; // Change desktop size
Pierre Ossmane7be49b2014-12-02 14:33:17 +0100140 static const AccessRights AccessNonShared; // Exclusive access to the server
Michal Srbb318b8f2014-11-24 13:18:28 +0200141 static const AccessRights AccessDefault; // The default rights, INCLUDING FUTURE ONES
142 static const AccessRights AccessNoQuery; // Connect without local user accepting
143 static const AccessRights AccessFull; // All of the available AND FUTURE rights
Pierre Ossman7d64b332018-10-08 15:59:02 +0200144 virtual void setAccessRights(AccessRights ar);
145 virtual bool accessCheck(AccessRights ar) const;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000146
147 // authenticated() returns true if the client has authenticated
148 // successfully.
149 bool authenticated() { return (state_ == RFBSTATE_INITIALISATION ||
150 state_ == RFBSTATE_NORMAL); }
151
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000152 SMsgReader* reader() { return reader_; }
153 SMsgWriter* writer() { return writer_; }
154
155 rdr::InStream* getInStream() { return is; }
156 rdr::OutStream* getOutStream() { return os; }
157
158 enum stateEnum {
159 RFBSTATE_UNINITIALISED,
160 RFBSTATE_PROTOCOL_VERSION,
161 RFBSTATE_SECURITY_TYPE,
162 RFBSTATE_SECURITY,
163 RFBSTATE_QUERYING,
164 RFBSTATE_INITIALISATION,
165 RFBSTATE_NORMAL,
166 RFBSTATE_CLOSING,
167 RFBSTATE_INVALID
168 };
169
170 stateEnum state() { return state_; }
171
Pierre Ossman48700812014-09-17 17:11:56 +0200172 rdr::S32 getPreferredEncoding() { return preferredEncoding; }
173
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000174 protected:
Pierre Ossman2ebed0d2018-10-11 07:54:12 +0200175 // throwConnFailedException() prints a message to the log, sends a conn
176 // failed message to the client (if possible) and throws a
177 // ConnFailedException.
178 void throwConnFailedException(const char* format, ...) __printf_attr(2, 3);
179
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000180 void setState(stateEnum s) { state_ = s; }
Pierre Ossmanb7acf862015-02-06 14:44:32 +0100181
Pierre Ossman0144c532015-02-04 14:10:43 +0100182 void setReader(SMsgReader *r) { reader_ = r; }
183 void setWriter(SMsgWriter *w) { writer_ = w; }
184
Pierre Ossmanb7acf862015-02-06 14:44:32 +0100185 private:
Pierre Ossmanb6b4dc62014-01-20 15:05:21 +0100186 void writeFakeColourMap(void);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000187
188 bool readyForSetColourMapEntries;
189
190 void processVersionMsg();
191 void processSecurityTypeMsg();
Constantin Kaplinsky5fa9d222006-09-06 10:32:06 +0000192 void processSecurityType(int secType);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000193 void processSecurityMsg();
194 void processInitMsg();
195
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000196 int defaultMajorVersion, defaultMinorVersion;
197 rdr::InStream* is;
198 rdr::OutStream* os;
199 SMsgReader* reader_;
200 SMsgWriter* writer_;
Michal Srbdccb5f72017-03-27 13:55:46 +0300201 SecurityServer security;
Adam Tkaca6578bf2010-04-23 14:07:41 +0000202 SSecurity* ssecurity;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000203 stateEnum state_;
Pierre Ossman48700812014-09-17 17:11:56 +0200204 rdr::S32 preferredEncoding;
Pierre Ossman7d64b332018-10-08 15:59:02 +0200205 AccessRights accessRights;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000206 };
207}
208#endif