blob: 501c761f928699f67fd42a6062874a850da8b24b [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
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000026#include <rfb/CMsgHandler.h>
27#include <rfb/util.h>
28
29namespace rfb {
30
31 class CMsgReader;
32 class CMsgWriter;
33 class CSecurity;
34 class IdentityVerifier;
Pierre Ossman0068a4f2015-11-09 15:48:19 +010035 class SecurityClient;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000036
37 class CConnection : public CMsgHandler {
38 public:
39
40 CConnection();
41 virtual ~CConnection();
42
43 // Methods to initialise the connection
44
45 // setServerName() is used to provide a unique(ish) name for the server to
46 // which we are connected. This might be the result of getPeerEndpoint on
47 // a TcpSocket, for example, or a host specified by DNS name & port.
48 // The serverName is used when verifying the Identity of a host (see RA2).
Adam Tkacd36b6262009-09-04 10:57:20 +000049 void setServerName(const char* name_) { serverName.replaceBuf(strDup(name_)); }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000050
51 // setStreams() sets the streams to be used for the connection. These must
52 // be set before initialiseProtocol() and processMsg() are called. The
53 // CSecurity object may call setStreams() again to provide alternative
54 // streams over which the RFB protocol is sent (i.e. encrypting/decrypting
55 // streams). Ownership of the streams remains with the caller
56 // (i.e. SConnection will not delete them).
57 void setStreams(rdr::InStream* is, rdr::OutStream* os);
58
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000059 // setShared sets the value of the shared flag which will be sent to the
60 // server upon initialisation.
61 void setShared(bool s) { shared = s; }
62
63 // setProtocol3_3 configures whether or not the CConnection should
64 // only ever support protocol version 3.3
65 void setProtocol3_3(bool s) {useProtocol3_3 = s;}
66
67 // initialiseProtocol() should be called once the streams and security
68 // types are set. Subsequently, processMsg() should be called whenever
69 // there is data to read on the InStream.
70 void initialiseProtocol();
71
72 // processMsg() should be called whenever there is either:
73 // - data available on the underlying network stream
74 // In this case, processMsg may return without processing an RFB message,
75 // if the available data does not result in an RFB message being ready
76 // to handle. e.g. if data is encrypted.
77 // NB: This makes it safe to call processMsg() in response to select()
78 // - data available on the CConnection's current InStream
79 // In this case, processMsg should always process the available RFB
80 // message before returning.
81 // NB: In either case, you must have called initialiseProtocol() first.
82 void processMsg();
83
84
85 // Methods to be overridden in a derived class
86
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000087 // getIdVerifier() returns the identity verifier associated with the connection.
88 // Ownership of the IdentityVerifier is retained by the CConnection instance.
89 virtual IdentityVerifier* getIdentityVerifier() {return 0;}
90
91 // authSuccess() is called when authentication has succeeded.
92 virtual void authSuccess();
93
94 // serverInit() is called when the ServerInit message is received. The
95 // derived class must call on to CConnection::serverInit().
96 virtual void serverInit();
97
98
99 // Other methods
100
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000101 CMsgReader* reader() { return reader_; }
102 CMsgWriter* writer() { return writer_; }
103
104 rdr::InStream* getInStream() { return is; }
105 rdr::OutStream* getOutStream() { return os; }
106
107 // Access method used by SSecurity implementations that can verify servers'
108 // Identities, to determine the unique(ish) name of the server.
109 const char* getServerName() const { return serverName.buf; }
110
111 enum stateEnum {
112 RFBSTATE_UNINITIALISED,
113 RFBSTATE_PROTOCOL_VERSION,
114 RFBSTATE_SECURITY_TYPES,
115 RFBSTATE_SECURITY,
116 RFBSTATE_SECURITY_RESULT,
117 RFBSTATE_INITIALISATION,
118 RFBSTATE_NORMAL,
119 RFBSTATE_INVALID
120 };
121
122 stateEnum state() { return state_; }
123
Adam Tkacd3b4dea2010-12-08 13:45:40 +0000124 CSecurity *csecurity;
125 SecurityClient *security;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000126 protected:
127 void setState(stateEnum s) { state_ = s; }
128
Pierre Ossman0144c532015-02-04 14:10:43 +0100129 void setReader(CMsgReader *r) { reader_ = r; }
130 void setWriter(CMsgWriter *w) { writer_ = w; }
131
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000132 private:
Pierre Ossmanc754cce2011-11-14 15:44:11 +0000133 // This is a default implementation of fences that automatically
134 // responds to requests, stating no support for synchronisation.
135 // When overriding, call CMsgHandler::fence() directly in order to
136 // state correct support for fence flags.
137 virtual void fence(rdr::U32 flags, unsigned len, const char data[]);
138
139 private:
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000140 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