blob: 19453c62a68316f7bfc368a4da42b0b9e8bf784d [file] [log] [blame]
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +00001/* Copyright (C) 2002-2004 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>
29
30namespace rfb {
31
32 class SMsgReader;
33 class SMsgWriter;
34 class SSecurity;
35
36 class SConnection : public SMsgHandler {
37 public:
38
39 SConnection();
40 virtual ~SConnection();
41
42 // Methods to initialise the connection
43
44 // setStreams() sets the streams to be used for the connection. These must
45 // be set before initialiseProtocol() and processMsg() are called. The
46 // SSecurity object may call setStreams() again to provide alternative
47 // streams over which the RFB protocol is sent (i.e. encrypting/decrypting
48 // streams). Ownership of the streams remains with the caller
49 // (i.e. SConnection will not delete them).
50 void setStreams(rdr::InStream* is, rdr::OutStream* os);
51
52 // addSecType() should be called once for each security type which the
53 // server supports to this client.
54 void addSecType(rdr::U8 secType);
55
56 // initialiseProtocol() should be called once the streams and security
57 // types are set. Subsequently, processMsg() should be called whenever
58 // there is data to read on the InStream.
59 void initialiseProtocol();
60
61 // processMsg() should be called whenever there is data to read on the
62 // InStream. You must have called initialiseProtocol() first.
63 void processMsg();
64
65 // approveConnection() is called to either accept or reject the connection.
66 // If accept is false, the reason string gives the reason for the
67 // rejection. It can either be called directly from queryConnection() or
68 // later, after queryConnection() has returned. It can only be called when
69 // in state RFBSTATE_QUERYING. On rejection, an AuthFailureException is
70 // thrown, so this must be handled appropriately by the caller.
71 void approveConnection(bool accept, const char* reason=0);
72
73
74 // Methods to be overridden in a derived class
75
76 // versionReceived() indicates that the version number has just been read
77 // from the client. The version will already have been "cooked"
78 // to deal with unknown/bogus viewer protocol numbers.
79 virtual void versionReceived();
80
81 // getSSecurity() gets the SSecurity object for the given type. The type
82 // is guaranteed to be one of the secTypes passed in to addSecType(). The
83 // SSecurity object's destroy() method will be called by the SConnection
84 // from its destructor.
85 virtual SSecurity* getSSecurity(int secType)=0;
86
87 // authSuccess() is called when authentication has succeeded.
88 virtual void authSuccess();
89
90 // authFailure() is called when authentication has failed. This method is
91 // not normally overridden since an exception is thrown anyway.
92 virtual void authFailure();
93
94 // queryConnection() is called when authentication has succeeded, but
95 // before informing the client. It can be overridden to query a local user
96 // to accept the incoming connection, for example. The userName argument
97 // is the name of the user making the connection, or null (note that the
98 // storage for userName is owned by the caller). The connection must be
99 // accepted or rejected by calling approveConnection(), either directly
100 // from queryConnection() or some time later.
101 virtual void queryConnection(const char* userName);
102
103 // clientInit() is called when the ClientInit message is received. The
104 // derived class must call on to SConnection::clientInit().
105 virtual void clientInit(bool shared);
106
107 // setPixelFormat() is called when a SetPixelFormat message is received.
108 // The derived class must call on to SConnection::setPixelFormat().
109 virtual void setPixelFormat(const PixelFormat& pf);
110
111 // framebufferUpdateRequest() is called when a FramebufferUpdateRequest
112 // message is received. The derived class must call on to
113 // SConnection::framebufferUpdateRequest().
114 virtual void framebufferUpdateRequest(const Rect& r, bool incremental);
115
116 // setInitialColourMap() is called when the client needs an initial
117 // SetColourMapEntries message. In fact this only happens when the client
118 // accepts the server's default pixel format and it uses a colour map.
119 virtual void setInitialColourMap();
120
121 // setAccessRights() allows a security package to limit the access rights
122 // of a VNCSConnectionST to the server. How the access rights are treated
123 // is up to the derived class.
124
125 typedef rdr::U16 AccessRights;
126 static const AccessRights AccessView; // View display contents
127 static const AccessRights AccessKeyEvents; // Send key events
128 static const AccessRights AccessPtrEvents; // Send pointer events
129 static const AccessRights AccessCutText; // Send/receive clipboard events
130 static const AccessRights AccessDefault; // The default rights, INCLUDING FUTURE ONES
131 static const AccessRights AccessNoQuery; // Connect without local user accepting
132 static const AccessRights AccessFull; // All of the available AND FUTURE rights
133 virtual void setAccessRights(AccessRights ar) = 0;
134
135 // Other methods
136
137 // authenticated() returns true if the client has authenticated
138 // successfully.
139 bool authenticated() { return (state_ == RFBSTATE_INITIALISATION ||
140 state_ == RFBSTATE_NORMAL); }
141
142 // deleteReaderAndWriter() deletes the reader and writer associated with
143 // this connection. This may be useful if you want to delete the streams
144 // before deleting the SConnection to make sure that no attempt by the
145 // SConnection is made to read or write.
146 // XXX Do we really need this at all???
147 void deleteReaderAndWriter();
148
149 // throwConnFailedException() prints a message to the log, sends a conn
150 // failed message to the client (if possible) and throws a
151 // ConnFailedException.
152 void throwConnFailedException(const char* msg);
153
154 // writeConnFailedFromScratch() sends a conn failed message to an OutStream
155 // without the need to negotiate the protocol version first. It actually
156 // does this by assuming that the client will understand version 3.3 of the
157 // protocol.
158 static void writeConnFailedFromScratch(const char* msg,
159 rdr::OutStream* os);
160
161 SMsgReader* reader() { return reader_; }
162 SMsgWriter* writer() { return writer_; }
163
164 rdr::InStream* getInStream() { return is; }
165 rdr::OutStream* getOutStream() { return os; }
166
167 enum stateEnum {
168 RFBSTATE_UNINITIALISED,
169 RFBSTATE_PROTOCOL_VERSION,
170 RFBSTATE_SECURITY_TYPE,
171 RFBSTATE_SECURITY,
172 RFBSTATE_QUERYING,
173 RFBSTATE_INITIALISATION,
174 RFBSTATE_NORMAL,
175 RFBSTATE_CLOSING,
176 RFBSTATE_INVALID
177 };
178
179 stateEnum state() { return state_; }
180
181 // ssecurity() returns a pointer to this connection's SSecurity object, if any
182 const SSecurity* ssecurity() const { return security; }
183
184 protected:
185 void setState(stateEnum s) { state_ = s; }
186
187 bool readyForSetColourMapEntries;
188
189 private:
190 void processVersionMsg();
191 void processSecurityTypeMsg();
192 void processSecurityMsg();
193 void processInitMsg();
194
195 int defaultMajorVersion, defaultMinorVersion;
196 rdr::InStream* is;
197 rdr::OutStream* os;
198 SMsgReader* reader_;
199 SMsgWriter* writer_;
200 enum { maxSecTypes = 8 };
201 int nSecTypes;
202 rdr::U8 secTypes[maxSecTypes];
203 SSecurity* security;
204 stateEnum state_;
205 };
206}
207#endif