blob: e139f15f05b1acfbc7aa0ae0cb34127c56ca5765 [file] [log] [blame]
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001/* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved.
2 *
3 * This is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This software is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this software; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
16 * USA.
17 */
18//
19// SConnection - class on the server side representing a connection to a
20// client. A derived class should override methods appropriately.
21//
22
23#ifndef __RFB_SCONNECTION_H__
24#define __RFB_SCONNECTION_H__
25
26#include <rdr/InStream.h>
27#include <rdr/OutStream.h>
28#include <rfb/SMsgHandler.h>
Adam Tkaca6578bf2010-04-23 14:07:41 +000029#include <rfb/Security.h>
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000030#include <rfb/SSecurity.h>
31
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
72 // Methods to be overridden in a derived class
73
74 // versionReceived() indicates that the version number has just been read
75 // from the client. The version will already have been "cooked"
76 // to deal with unknown/bogus viewer protocol numbers.
77 virtual void versionReceived();
78
79 // authSuccess() is called when authentication has succeeded.
80 virtual void authSuccess();
81
82 // queryConnection() is called when authentication has succeeded, but
83 // before informing the client. It can be overridden to query a local user
84 // to accept the incoming connection, for example. The userName argument
85 // is the name of the user making the connection, or null (note that the
86 // storage for userName is owned by the caller). The connection must be
87 // accepted or rejected by calling approveConnection(), either directly
88 // from queryConnection() or some time later.
89 virtual void queryConnection(const char* userName);
90
91 // clientInit() is called when the ClientInit message is received. The
92 // derived class must call on to SConnection::clientInit().
93 virtual void clientInit(bool shared);
94
95 // setPixelFormat() is called when a SetPixelFormat message is received.
96 // The derived class must call on to SConnection::setPixelFormat().
97 virtual void setPixelFormat(const PixelFormat& pf);
98
99 // framebufferUpdateRequest() is called when a FramebufferUpdateRequest
100 // message is received. The derived class must call on to
101 // SConnection::framebufferUpdateRequest().
102 virtual void framebufferUpdateRequest(const Rect& r, bool incremental);
103
104 // setInitialColourMap() is called when the client needs an initial
105 // SetColourMapEntries message. In fact this only happens when the client
106 // accepts the server's default pixel format and it uses a colour map.
107 virtual void setInitialColourMap();
108
109 // setAccessRights() allows a security package to limit the access rights
110 // of a VNCSConnectionST to the server. How the access rights are treated
111 // is up to the derived class.
112
113 typedef rdr::U16 AccessRights;
114 static const AccessRights AccessView; // View display contents
115 static const AccessRights AccessKeyEvents; // Send key events
116 static const AccessRights AccessPtrEvents; // Send pointer events
117 static const AccessRights AccessCutText; // Send/receive clipboard events
118 static const AccessRights AccessDefault; // The default rights, INCLUDING FUTURE ONES
119 static const AccessRights AccessNoQuery; // Connect without local user accepting
120 static const AccessRights AccessFull; // All of the available AND FUTURE rights
121 virtual void setAccessRights(AccessRights ar) = 0;
122
123 // Other methods
124
125 // authenticated() returns true if the client has authenticated
126 // successfully.
127 bool authenticated() { return (state_ == RFBSTATE_INITIALISATION ||
128 state_ == RFBSTATE_NORMAL); }
129
130 // deleteReaderAndWriter() deletes the reader and writer associated with
131 // this connection. This may be useful if you want to delete the streams
132 // before deleting the SConnection to make sure that no attempt by the
133 // SConnection is made to read or write.
134 // XXX Do we really need this at all???
135 void deleteReaderAndWriter();
136
137 // throwConnFailedException() prints a message to the log, sends a conn
138 // failed message to the client (if possible) and throws a
139 // ConnFailedException.
140 void throwConnFailedException(const char* msg);
141
142 // writeConnFailedFromScratch() sends a conn failed message to an OutStream
143 // without the need to negotiate the protocol version first. It actually
144 // does this by assuming that the client will understand version 3.3 of the
145 // protocol.
146 static void writeConnFailedFromScratch(const char* msg,
147 rdr::OutStream* os);
148
149 SMsgReader* reader() { return reader_; }
150 SMsgWriter* writer() { return writer_; }
151
152 rdr::InStream* getInStream() { return is; }
153 rdr::OutStream* getOutStream() { return os; }
154
155 enum stateEnum {
156 RFBSTATE_UNINITIALISED,
157 RFBSTATE_PROTOCOL_VERSION,
158 RFBSTATE_SECURITY_TYPE,
Constantin Kaplinskyce0907d2006-09-08 12:55:37 +0000159 RFBSTATE_TIGHT_TUNN_TYPE,
160 RFBSTATE_TIGHT_AUTH_TYPE,
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000161 RFBSTATE_SECURITY,
162 RFBSTATE_QUERYING,
163 RFBSTATE_INITIALISATION,
164 RFBSTATE_NORMAL,
165 RFBSTATE_CLOSING,
166 RFBSTATE_INVALID
167 };
168
169 stateEnum state() { return state_; }
170
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000171 protected:
172 void setState(stateEnum s) { state_ = s; }
173
174 bool readyForSetColourMapEntries;
175
176 void processVersionMsg();
177 void processSecurityTypeMsg();
Constantin Kaplinsky5fa9d222006-09-06 10:32:06 +0000178 void processSecurityType(int secType);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000179 void processSecurityMsg();
180 void processInitMsg();
181
Constantin Kaplinskyce0907d2006-09-08 12:55:37 +0000182 // These functions add support for TightVNC protocol extensions.
183 void offerTunneling();
184 void processTunnelTypeMsg();
185 void offerAuthentication();
186 void processAuthTypeMsg();
187 void sendInteractionCaps();
188
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000189 int defaultMajorVersion, defaultMinorVersion;
190 rdr::InStream* is;
191 rdr::OutStream* os;
192 SMsgReader* reader_;
193 SMsgWriter* writer_;
Adam Tkaca6578bf2010-04-23 14:07:41 +0000194 Security *security;
195 SSecurity* ssecurity;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000196 stateEnum state_;
197 bool reverseConnection;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000198 };
199}
200#endif