blob: f712417398851708d8c5633290b42f4e505fd3e0 [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
Adam Tkaca6578bf2010-04-23 14:07:41 +000041 SConnection(bool reverseConnection_);
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 Ossman48700812014-09-17 17:11:56 +020072 // Overridden from SMsgHandler
73
74 virtual void setEncodings(int nEncodings, rdr::S32* encodings);
75
76
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000077 // Methods to be overridden in a derived class
78
79 // versionReceived() indicates that the version number has just been read
80 // from the client. The version will already have been "cooked"
81 // to deal with unknown/bogus viewer protocol numbers.
82 virtual void versionReceived();
83
84 // authSuccess() is called when authentication has succeeded.
85 virtual void authSuccess();
86
87 // queryConnection() is called when authentication has succeeded, but
88 // before informing the client. It can be overridden to query a local user
89 // to accept the incoming connection, for example. The userName argument
90 // is the name of the user making the connection, or null (note that the
91 // storage for userName is owned by the caller). The connection must be
92 // accepted or rejected by calling approveConnection(), either directly
93 // from queryConnection() or some time later.
94 virtual void queryConnection(const char* userName);
95
96 // clientInit() is called when the ClientInit message is received. The
97 // derived class must call on to SConnection::clientInit().
98 virtual void clientInit(bool shared);
99
100 // setPixelFormat() is called when a SetPixelFormat message is received.
101 // The derived class must call on to SConnection::setPixelFormat().
102 virtual void setPixelFormat(const PixelFormat& pf);
103
104 // framebufferUpdateRequest() is called when a FramebufferUpdateRequest
105 // message is received. The derived class must call on to
106 // SConnection::framebufferUpdateRequest().
107 virtual void framebufferUpdateRequest(const Rect& r, bool incremental);
108
Pierre Ossmanc754cce2011-11-14 15:44:11 +0000109 // fence() is called when we get a fence request or response. By default
110 // it responds directly to requests (stating it doesn't support any
111 // synchronisation) and drops responses. Override to implement more proper
112 // support.
113 virtual void fence(rdr::U32 flags, unsigned len, const char data[]);
114
Pierre Ossmanc898d9a2011-11-14 16:22:23 +0000115 // enableContinuousUpdates() is called when the client wants to enable
116 // or disable continuous updates, or change the active area.
117 virtual void enableContinuousUpdates(bool enable,
118 int x, int y, int w, int h);
119
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000120 // setAccessRights() allows a security package to limit the access rights
121 // of a VNCSConnectionST to the server. How the access rights are treated
122 // is up to the derived class.
123
124 typedef rdr::U16 AccessRights;
125 static const AccessRights AccessView; // View display contents
126 static const AccessRights AccessKeyEvents; // Send key events
127 static const AccessRights AccessPtrEvents; // Send pointer events
128 static const AccessRights AccessCutText; // Send/receive clipboard events
129 static const AccessRights AccessDefault; // The default rights, INCLUDING FUTURE ONES
130 static const AccessRights AccessNoQuery; // Connect without local user accepting
131 static const AccessRights AccessFull; // All of the available AND FUTURE rights
132 virtual void setAccessRights(AccessRights ar) = 0;
133
134 // Other methods
135
136 // authenticated() returns true if the client has authenticated
137 // successfully.
138 bool authenticated() { return (state_ == RFBSTATE_INITIALISATION ||
139 state_ == RFBSTATE_NORMAL); }
140
141 // deleteReaderAndWriter() deletes the reader and writer associated with
142 // this connection. This may be useful if you want to delete the streams
143 // before deleting the SConnection to make sure that no attempt by the
144 // SConnection is made to read or write.
145 // XXX Do we really need this at all???
146 void deleteReaderAndWriter();
147
148 // throwConnFailedException() prints a message to the log, sends a conn
149 // failed message to the client (if possible) and throws a
150 // ConnFailedException.
151 void throwConnFailedException(const char* msg);
152
153 // writeConnFailedFromScratch() sends a conn failed message to an OutStream
154 // without the need to negotiate the protocol version first. It actually
155 // does this by assuming that the client will understand version 3.3 of the
156 // protocol.
157 static void writeConnFailedFromScratch(const char* msg,
158 rdr::OutStream* os);
159
160 SMsgReader* reader() { return reader_; }
161 SMsgWriter* writer() { return writer_; }
162
163 rdr::InStream* getInStream() { return is; }
164 rdr::OutStream* getOutStream() { return os; }
165
166 enum stateEnum {
167 RFBSTATE_UNINITIALISED,
168 RFBSTATE_PROTOCOL_VERSION,
169 RFBSTATE_SECURITY_TYPE,
170 RFBSTATE_SECURITY,
171 RFBSTATE_QUERYING,
172 RFBSTATE_INITIALISATION,
173 RFBSTATE_NORMAL,
174 RFBSTATE_CLOSING,
175 RFBSTATE_INVALID
176 };
177
178 stateEnum state() { return state_; }
179
Pierre Ossman48700812014-09-17 17:11:56 +0200180 rdr::S32 getPreferredEncoding() { return preferredEncoding; }
181
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000182 protected:
183 void setState(stateEnum s) { state_ = s; }
Pierre Ossmanb6b4dc62014-01-20 15:05:21 +0100184 void writeFakeColourMap(void);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000185
186 bool readyForSetColourMapEntries;
187
188 void processVersionMsg();
189 void processSecurityTypeMsg();
Constantin Kaplinsky5fa9d222006-09-06 10:32:06 +0000190 void processSecurityType(int secType);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000191 void processSecurityMsg();
192 void processInitMsg();
193
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000194 int defaultMajorVersion, defaultMinorVersion;
195 rdr::InStream* is;
196 rdr::OutStream* os;
197 SMsgReader* reader_;
198 SMsgWriter* writer_;
Adam Tkacbfd66c12010-10-01 08:33:29 +0000199 SecurityServer *security;
Adam Tkaca6578bf2010-04-23 14:07:41 +0000200 SSecurity* ssecurity;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000201 stateEnum state_;
202 bool reverseConnection;
Pierre Ossman48700812014-09-17 17:11:56 +0200203 rdr::S32 preferredEncoding;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000204 };
205}
206#endif