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