blob: 6bc7a389083e4979dafc6c852acb22a410336437 [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
Pierre Ossman3da238d2015-11-12 12:20:05 +010095 // Note: These must be called by any deriving classes
96
97 virtual void setDesktopSize(int w, int h);
98 virtual void setExtendedDesktopSize(unsigned reason, unsigned result,
99 int w, int h,
100 const ScreenSet& layout);
101
102 virtual void framebufferUpdateStart();
103 virtual void framebufferUpdateEnd();
Pierre Ossman9f273e92015-11-09 16:34:54 +0100104 virtual void dataRect(const Rect& r, int encoding);
105
106
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000107 // Methods to be overridden in a derived class
108
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000109 // getIdVerifier() returns the identity verifier associated with the connection.
110 // Ownership of the IdentityVerifier is retained by the CConnection instance.
111 virtual IdentityVerifier* getIdentityVerifier() {return 0;}
112
113 // authSuccess() is called when authentication has succeeded.
114 virtual void authSuccess();
115
116 // serverInit() is called when the ServerInit message is received. The
117 // derived class must call on to CConnection::serverInit().
118 virtual void serverInit();
119
120
121 // Other methods
122
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000123 CMsgReader* reader() { return reader_; }
124 CMsgWriter* writer() { return writer_; }
125
126 rdr::InStream* getInStream() { return is; }
127 rdr::OutStream* getOutStream() { return os; }
128
129 // Access method used by SSecurity implementations that can verify servers'
130 // Identities, to determine the unique(ish) name of the server.
131 const char* getServerName() const { return serverName.buf; }
132
133 enum stateEnum {
134 RFBSTATE_UNINITIALISED,
135 RFBSTATE_PROTOCOL_VERSION,
136 RFBSTATE_SECURITY_TYPES,
137 RFBSTATE_SECURITY,
138 RFBSTATE_SECURITY_RESULT,
139 RFBSTATE_INITIALISATION,
140 RFBSTATE_NORMAL,
141 RFBSTATE_INVALID
142 };
143
144 stateEnum state() { return state_; }
145
Adam Tkacd3b4dea2010-12-08 13:45:40 +0000146 CSecurity *csecurity;
147 SecurityClient *security;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000148 protected:
149 void setState(stateEnum s) { state_ = s; }
150
Pierre Ossman0144c532015-02-04 14:10:43 +0100151 void setReader(CMsgReader *r) { reader_ = r; }
152 void setWriter(CMsgWriter *w) { writer_ = w; }
153
Pierre Ossman9f273e92015-11-09 16:34:54 +0100154 ModifiablePixelBuffer* getFramebuffer() { return framebuffer; }
155
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000156 private:
Pierre Ossmanc754cce2011-11-14 15:44:11 +0000157 // This is a default implementation of fences that automatically
158 // responds to requests, stating no support for synchronisation.
159 // When overriding, call CMsgHandler::fence() directly in order to
160 // state correct support for fence flags.
161 virtual void fence(rdr::U32 flags, unsigned len, const char data[]);
162
163 private:
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000164 void processVersionMsg();
165 void processSecurityTypesMsg();
166 void processSecurityMsg();
167 void processSecurityResultMsg();
168 void processInitMsg();
169 void throwAuthFailureException();
170 void throwConnFailedException();
171 void securityCompleted();
172
173 rdr::InStream* is;
174 rdr::OutStream* os;
175 CMsgReader* reader_;
176 CMsgWriter* writer_;
177 bool deleteStreamsWhenDone;
178 bool shared;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000179 stateEnum state_;
180
181 CharArray serverName;
182
183 bool useProtocol3_3;
Pierre Ossman9f273e92015-11-09 16:34:54 +0100184
185 ModifiablePixelBuffer* framebuffer;
186 DecodeManager decoder;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000187 };
188}
189#endif