blob: 7f8dbea0b8c3ef6c5511cf5a93f5db502803a8d1 [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>
Pierre Ossman9f273e92015-11-09 16:34:54 +010027#include <rfb/DecodeManager.h>
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000028#include <rfb/util.h>
29
30namespace rfb {
31
32 class CMsgReader;
33 class CMsgWriter;
34 class CSecurity;
35 class IdentityVerifier;
Pierre Ossman0068a4f2015-11-09 15:48:19 +010036 class SecurityClient;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000037
38 class CConnection : public CMsgHandler {
39 public:
40
41 CConnection();
42 virtual ~CConnection();
43
44 // Methods to initialise the connection
45
46 // setServerName() is used to provide a unique(ish) name for the server to
47 // which we are connected. This might be the result of getPeerEndpoint on
48 // a TcpSocket, for example, or a host specified by DNS name & port.
49 // The serverName is used when verifying the Identity of a host (see RA2).
Adam Tkacd36b6262009-09-04 10:57:20 +000050 void setServerName(const char* name_) { serverName.replaceBuf(strDup(name_)); }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000051
52 // setStreams() sets the streams to be used for the connection. These must
53 // be set before initialiseProtocol() and processMsg() are called. The
54 // CSecurity object may call setStreams() again to provide alternative
55 // streams over which the RFB protocol is sent (i.e. encrypting/decrypting
56 // streams). Ownership of the streams remains with the caller
57 // (i.e. SConnection will not delete them).
58 void setStreams(rdr::InStream* is, rdr::OutStream* os);
59
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000060 // setShared sets the value of the shared flag which will be sent to the
61 // server upon initialisation.
62 void setShared(bool s) { shared = s; }
63
64 // setProtocol3_3 configures whether or not the CConnection should
65 // only ever support protocol version 3.3
66 void setProtocol3_3(bool s) {useProtocol3_3 = s;}
67
Pierre Ossman9f273e92015-11-09 16:34:54 +010068 // setFramebuffer configures the PixelBuffer that the CConnection
69 // should render all pixel data in to. Note that the CConnection
70 // takes ownership of the PixelBuffer and it must not be deleted by
71 // anyone else. Call setFramebuffer again with NULL or a different
72 // PixelBuffer to delete the previous one.
73 void setFramebuffer(ModifiablePixelBuffer* fb);
74
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000075 // initialiseProtocol() should be called once the streams and security
76 // types are set. Subsequently, processMsg() should be called whenever
77 // there is data to read on the InStream.
78 void initialiseProtocol();
79
80 // processMsg() should be called whenever there is either:
81 // - data available on the underlying network stream
82 // In this case, processMsg may return without processing an RFB message,
83 // if the available data does not result in an RFB message being ready
84 // to handle. e.g. if data is encrypted.
85 // NB: This makes it safe to call processMsg() in response to select()
86 // - data available on the CConnection's current InStream
87 // In this case, processMsg should always process the available RFB
88 // message before returning.
89 // NB: In either case, you must have called initialiseProtocol() first.
90 void processMsg();
91
92
Pierre Ossman9f273e92015-11-09 16:34:54 +010093 // Methods overridden from CMsgHandler
94
95 virtual void dataRect(const Rect& r, int encoding);
96
97
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000098 // Methods to be overridden in a derived class
99
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000100 // getIdVerifier() returns the identity verifier associated with the connection.
101 // Ownership of the IdentityVerifier is retained by the CConnection instance.
102 virtual IdentityVerifier* getIdentityVerifier() {return 0;}
103
104 // authSuccess() is called when authentication has succeeded.
105 virtual void authSuccess();
106
107 // serverInit() is called when the ServerInit message is received. The
108 // derived class must call on to CConnection::serverInit().
109 virtual void serverInit();
110
111
112 // Other methods
113
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000114 CMsgReader* reader() { return reader_; }
115 CMsgWriter* writer() { return writer_; }
116
117 rdr::InStream* getInStream() { return is; }
118 rdr::OutStream* getOutStream() { return os; }
119
120 // Access method used by SSecurity implementations that can verify servers'
121 // Identities, to determine the unique(ish) name of the server.
122 const char* getServerName() const { return serverName.buf; }
123
124 enum stateEnum {
125 RFBSTATE_UNINITIALISED,
126 RFBSTATE_PROTOCOL_VERSION,
127 RFBSTATE_SECURITY_TYPES,
128 RFBSTATE_SECURITY,
129 RFBSTATE_SECURITY_RESULT,
130 RFBSTATE_INITIALISATION,
131 RFBSTATE_NORMAL,
132 RFBSTATE_INVALID
133 };
134
135 stateEnum state() { return state_; }
136
Adam Tkacd3b4dea2010-12-08 13:45:40 +0000137 CSecurity *csecurity;
138 SecurityClient *security;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000139 protected:
140 void setState(stateEnum s) { state_ = s; }
141
Pierre Ossman0144c532015-02-04 14:10:43 +0100142 void setReader(CMsgReader *r) { reader_ = r; }
143 void setWriter(CMsgWriter *w) { writer_ = w; }
144
Pierre Ossman9f273e92015-11-09 16:34:54 +0100145 ModifiablePixelBuffer* getFramebuffer() { return framebuffer; }
146
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000147 private:
Pierre Ossmanc754cce2011-11-14 15:44:11 +0000148 // This is a default implementation of fences that automatically
149 // responds to requests, stating no support for synchronisation.
150 // When overriding, call CMsgHandler::fence() directly in order to
151 // state correct support for fence flags.
152 virtual void fence(rdr::U32 flags, unsigned len, const char data[]);
153
154 private:
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000155 void processVersionMsg();
156 void processSecurityTypesMsg();
157 void processSecurityMsg();
158 void processSecurityResultMsg();
159 void processInitMsg();
160 void throwAuthFailureException();
161 void throwConnFailedException();
162 void securityCompleted();
163
164 rdr::InStream* is;
165 rdr::OutStream* os;
166 CMsgReader* reader_;
167 CMsgWriter* writer_;
168 bool deleteStreamsWhenDone;
169 bool shared;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000170 stateEnum state_;
171
172 CharArray serverName;
173
174 bool useProtocol3_3;
Pierre Ossman9f273e92015-11-09 16:34:54 +0100175
176 ModifiablePixelBuffer* framebuffer;
177 DecodeManager decoder;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000178 };
179}
180#endif