blob: 480fca305fe7c8b330da288c536ff9a66b3efa01 [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// CConnection - class on the client side representing a connection to a
20// server. A derived class should override methods appropriately.
21//
22
23#ifndef __RFB_CCONNECTION_H__
24#define __RFB_CCONNECTION_H__
25
26#include <rdr/InStream.h>
27#include <rdr/OutStream.h>
28#include <rfb/CMsgHandler.h>
29#include <rfb/util.h>
30
31namespace rfb {
32
33 class CMsgReader;
34 class CMsgWriter;
35 class CSecurity;
36 class IdentityVerifier;
37
38 class CConnection : public CMsgHandler {
39 public:
40
41 CConnection();
42 virtual ~CConnection();
43
44 // ***
45 void setServerName(const char* serverName_);
46
47 // Methods to initialise the connection
48
49 // setStreams() sets the streams to be used for the connection. These must
50 // be set before initialiseProtocol() and processMsg() are called. The
51 // CSecurity object may call setStreams() again to provide alternative
52 // streams over which the RFB protocol is sent (i.e. encrypting/decrypting
53 // streams). Ownership of the streams remains with the caller
54 // (i.e. SConnection will not delete them).
55 void setStreams(rdr::InStream* is, rdr::OutStream* os);
56
57 // addSecType() should be called once for each security type which the
58 // client supports. The order in which they're added is such that the
59 // first one is most preferred.
60 void addSecType(rdr::U8 secType);
61
62 // setClientSecTypeOrder() determines whether the client should obey
63 // the server's security type preference, by picking the first server security
64 // type that the client supports, or whether it should pick the first type
65 // that the server supports, from the client-supported list of types.
66 void setClientSecTypeOrder(bool clientOrder);
67
68 // setShared sets the value of the shared flag which will be sent to the
69 // server upon initialisation.
70 void setShared(bool s) { shared = s; }
71
72 // setProtocol3_3 configures whether or not the CConnection should
73 // only ever support protocol version 3.3
74 void setProtocol3_3(bool s) {useProtocol3_3 = s;}
75
76 // initialiseProtocol() should be called once the streams and security
77 // types are set. Subsequently, processMsg() should be called whenever
78 // there is data to read on the InStream.
79 void initialiseProtocol();
80
81 // processMsg() should be called whenever there is either:
82 // - data available on the underlying network stream
83 // In this case, processMsg may return without processing an RFB message,
84 // if the available data does not result in an RFB message being ready
85 // to handle. e.g. if data is encrypted.
86 // NB: This makes it safe to call processMsg() in response to select()
87 // - data available on the CConnection's current InStream
88 // In this case, processMsg should always process the available RFB
89 // message before returning.
90 // NB: In either case, you must have called initialiseProtocol() first.
91 void processMsg();
92
93
94 // Methods to be overridden in a derived class
95
96 // getCSecurity() gets the CSecurity object for the given type. The type
97 // is guaranteed to be one of the secTypes passed in to addSecType(). The
98 // CSecurity object's destroy() method will be called by the CConnection
99 // from its destructor.
100 virtual CSecurity* getCSecurity(int secType)=0;
101
102 // getCurrentCSecurity() gets the CSecurity instance used for this connection.
103 const CSecurity* getCurrentCSecurity() const {return security;}
104
105 // getIdVerifier() returns the identity verifier associated with the connection.
106 // Ownership of the IdentityVerifier is retained by the CConnection instance.
107 virtual IdentityVerifier* getIdentityVerifier() {return 0;}
108
109 // authSuccess() is called when authentication has succeeded.
110 virtual void authSuccess();
111
112 // serverInit() is called when the ServerInit message is received. The
113 // derived class must call on to CConnection::serverInit().
114 virtual void serverInit();
115
116
117 // Other methods
118
119 // deleteReaderAndWriter() deletes the reader and writer associated with
120 // this connection. This may be useful if you want to delete the streams
121 // before deleting the SConnection to make sure that no attempt by the
122 // SConnection is made to read or write.
123 // XXX Do we really need this at all???
124 void deleteReaderAndWriter();
125
126 CMsgReader* reader() { return reader_; }
127 CMsgWriter* writer() { return writer_; }
128
129 rdr::InStream* getInStream() { return is; }
130 rdr::OutStream* getOutStream() { return os; }
131
132 char* getServerName() {return strDup(serverName.buf);}
133
134 enum stateEnum {
135 RFBSTATE_UNINITIALISED,
136 RFBSTATE_PROTOCOL_VERSION,
137 RFBSTATE_SECURITY_TYPES,
138 RFBSTATE_SECURITY,
139 RFBSTATE_SECURITY_RESULT,
140 RFBSTATE_INITIALISATION,
141 RFBSTATE_NORMAL,
142 RFBSTATE_INVALID
143 };
144
145 stateEnum state() { return state_; }
146
147 protected:
148 void setState(stateEnum s) { state_ = s; }
149
150 private:
151 void processVersionMsg();
152 void processSecurityTypesMsg();
153 void processSecurityMsg();
154 void processSecurityResultMsg();
155 void processInitMsg();
156 void throwAuthFailureException();
157 void throwConnFailedException();
158 void securityCompleted();
159
160 rdr::InStream* is;
161 rdr::OutStream* os;
162 CMsgReader* reader_;
163 CMsgWriter* writer_;
164 bool deleteStreamsWhenDone;
165 bool shared;
166 CSecurity* security;
167 enum { maxSecTypes = 8 };
168 int nSecTypes;
169 rdr::U8 secTypes[maxSecTypes];
170 bool clientSecTypeOrder;
171 stateEnum state_;
172
173 CharArray serverName;
174
175 bool useProtocol3_3;
176 };
177}
178#endif