blob: eb8c1c3f34dadf2cdc3fa05d547f2a6623229438 [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// 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>
Adam Tkacf324dc42010-04-23 14:10:17 +000029#include <rfb/CSecurity.h>
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000030#include <rfb/util.h>
Adam Tkacbfd66c12010-10-01 08:33:29 +000031#include <rfb/SecurityClient.h>
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000032
33namespace rfb {
34
35 class CMsgReader;
36 class CMsgWriter;
37 class CSecurity;
38 class IdentityVerifier;
39
40 class CConnection : public CMsgHandler {
41 public:
42
43 CConnection();
44 virtual ~CConnection();
45
46 // Methods to initialise the connection
47
48 // setServerName() is used to provide a unique(ish) name for the server to
49 // which we are connected. This might be the result of getPeerEndpoint on
50 // a TcpSocket, for example, or a host specified by DNS name & port.
51 // The serverName is used when verifying the Identity of a host (see RA2).
Adam Tkacd36b6262009-09-04 10:57:20 +000052 void setServerName(const char* name_) { serverName.replaceBuf(strDup(name_)); }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000053
54 // setStreams() sets the streams to be used for the connection. These must
55 // be set before initialiseProtocol() and processMsg() are called. The
56 // CSecurity object may call setStreams() again to provide alternative
57 // streams over which the RFB protocol is sent (i.e. encrypting/decrypting
58 // streams). Ownership of the streams remains with the caller
59 // (i.e. SConnection will not delete them).
60 void setStreams(rdr::InStream* is, rdr::OutStream* os);
61
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000062 // setShared sets the value of the shared flag which will be sent to the
63 // server upon initialisation.
64 void setShared(bool s) { shared = s; }
65
66 // setProtocol3_3 configures whether or not the CConnection should
67 // only ever support protocol version 3.3
68 void setProtocol3_3(bool s) {useProtocol3_3 = s;}
69
70 // initialiseProtocol() should be called once the streams and security
71 // types are set. Subsequently, processMsg() should be called whenever
72 // there is data to read on the InStream.
73 void initialiseProtocol();
74
75 // processMsg() should be called whenever there is either:
76 // - data available on the underlying network stream
77 // In this case, processMsg may return without processing an RFB message,
78 // if the available data does not result in an RFB message being ready
79 // to handle. e.g. if data is encrypted.
80 // NB: This makes it safe to call processMsg() in response to select()
81 // - data available on the CConnection's current InStream
82 // In this case, processMsg should always process the available RFB
83 // message before returning.
84 // NB: In either case, you must have called initialiseProtocol() first.
85 void processMsg();
86
87
88 // Methods to be overridden in a derived class
89
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000090 // getIdVerifier() returns the identity verifier associated with the connection.
91 // Ownership of the IdentityVerifier is retained by the CConnection instance.
92 virtual IdentityVerifier* getIdentityVerifier() {return 0;}
93
94 // authSuccess() is called when authentication has succeeded.
95 virtual void authSuccess();
96
97 // serverInit() is called when the ServerInit message is received. The
98 // derived class must call on to CConnection::serverInit().
99 virtual void serverInit();
100
101
102 // Other methods
103
104 // deleteReaderAndWriter() deletes the reader and writer associated with
105 // this connection. This may be useful if you want to delete the streams
106 // before deleting the SConnection to make sure that no attempt by the
107 // SConnection is made to read or write.
108 // XXX Do we really need this at all???
109 void deleteReaderAndWriter();
110
111 CMsgReader* reader() { return reader_; }
112 CMsgWriter* writer() { return writer_; }
113
114 rdr::InStream* getInStream() { return is; }
115 rdr::OutStream* getOutStream() { return os; }
116
117 // Access method used by SSecurity implementations that can verify servers'
118 // Identities, to determine the unique(ish) name of the server.
119 const char* getServerName() const { return serverName.buf; }
120
121 enum stateEnum {
122 RFBSTATE_UNINITIALISED,
123 RFBSTATE_PROTOCOL_VERSION,
124 RFBSTATE_SECURITY_TYPES,
125 RFBSTATE_SECURITY,
126 RFBSTATE_SECURITY_RESULT,
127 RFBSTATE_INITIALISATION,
128 RFBSTATE_NORMAL,
129 RFBSTATE_INVALID
130 };
131
132 stateEnum state() { return state_; }
133
Adam Tkacf324dc42010-04-23 14:10:17 +0000134 CSecurity *csecurity; /* Windows viewer needs it exported. */
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000135 protected:
136 void setState(stateEnum s) { state_ = s; }
Adam Tkacbfd66c12010-10-01 08:33:29 +0000137 SecurityClient *security;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000138
139 private:
140 void processVersionMsg();
141 void processSecurityTypesMsg();
142 void processSecurityMsg();
143 void processSecurityResultMsg();
144 void processInitMsg();
145 void throwAuthFailureException();
146 void throwConnFailedException();
147 void securityCompleted();
148
149 rdr::InStream* is;
150 rdr::OutStream* os;
151 CMsgReader* reader_;
152 CMsgWriter* writer_;
153 bool deleteStreamsWhenDone;
154 bool shared;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000155 stateEnum state_;
156
157 CharArray serverName;
158
159 bool useProtocol3_3;
160 };
161}
162#endif