blob: 4106a1e6ea1d7a1af374ad7f3efdd8a88ec10cc9 [file] [log] [blame]
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001/* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved.
Pierre Ossman615d16b2019-05-03 10:53:06 +02002 * Copyright 2011-2019 Pierre Ossman for Cendio AB
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00003 *
4 * This is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This software is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this software; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
17 * USA.
18 */
19//
20// CConnection - class on the client side representing a connection to a
21// server. A derived class should override methods appropriately.
22//
23
24#ifndef __RFB_CCONNECTION_H__
25#define __RFB_CCONNECTION_H__
26
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000027#include <rfb/CMsgHandler.h>
Pierre Ossman9f273e92015-11-09 16:34:54 +010028#include <rfb/DecodeManager.h>
Michal Srbdccb5f72017-03-27 13:55:46 +030029#include <rfb/SecurityClient.h>
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000030#include <rfb/util.h>
31
32namespace rfb {
33
34 class CMsgReader;
35 class CMsgWriter;
36 class CSecurity;
37 class IdentityVerifier;
38
39 class CConnection : public CMsgHandler {
40 public:
41
42 CConnection();
43 virtual ~CConnection();
44
45 // Methods to initialise the connection
46
47 // setServerName() is used to provide a unique(ish) name for the server to
48 // which we are connected. This might be the result of getPeerEndpoint on
49 // a TcpSocket, for example, or a host specified by DNS name & port.
50 // The serverName is used when verifying the Identity of a host (see RA2).
Adam Tkacd36b6262009-09-04 10:57:20 +000051 void setServerName(const char* name_) { serverName.replaceBuf(strDup(name_)); }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000052
53 // setStreams() sets the streams to be used for the connection. These must
54 // be set before initialiseProtocol() and processMsg() are called. The
55 // CSecurity object may call setStreams() again to provide alternative
56 // streams over which the RFB protocol is sent (i.e. encrypting/decrypting
57 // streams). Ownership of the streams remains with the caller
58 // (i.e. SConnection will not delete them).
59 void setStreams(rdr::InStream* is, rdr::OutStream* os);
60
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000061 // setShared sets the value of the shared flag which will be sent to the
62 // server upon initialisation.
63 void setShared(bool s) { shared = s; }
64
Pierre Ossman9f273e92015-11-09 16:34:54 +010065 // setFramebuffer configures the PixelBuffer that the CConnection
66 // should render all pixel data in to. Note that the CConnection
67 // takes ownership of the PixelBuffer and it must not be deleted by
68 // anyone else. Call setFramebuffer again with NULL or a different
69 // PixelBuffer to delete the previous one.
70 void setFramebuffer(ModifiablePixelBuffer* fb);
71
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000072 // initialiseProtocol() should be called once the streams and security
73 // types are set. Subsequently, processMsg() should be called whenever
74 // there is data to read on the InStream.
75 void initialiseProtocol();
76
77 // processMsg() should be called whenever there is either:
78 // - data available on the underlying network stream
79 // In this case, processMsg may return without processing an RFB message,
80 // if the available data does not result in an RFB message being ready
81 // to handle. e.g. if data is encrypted.
82 // NB: This makes it safe to call processMsg() in response to select()
83 // - data available on the CConnection's current InStream
84 // In this case, processMsg should always process the available RFB
85 // message before returning.
86 // NB: In either case, you must have called initialiseProtocol() first.
87 void processMsg();
88
89
Pierre Ossman9f273e92015-11-09 16:34:54 +010090 // Methods overridden from CMsgHandler
91
Pierre Ossman3da238d2015-11-12 12:20:05 +010092 // Note: These must be called by any deriving classes
93
94 virtual void setDesktopSize(int w, int h);
95 virtual void setExtendedDesktopSize(unsigned reason, unsigned result,
96 int w, int h,
97 const ScreenSet& layout);
98
Pierre Ossmanef6881b2018-06-20 11:26:18 +020099 virtual void endOfContinuousUpdates();
100
Pierre Ossmandd45b442018-10-31 17:08:59 +0100101 virtual void serverInit(int width, int height,
102 const PixelFormat& pf,
103 const char* name);
Pierre Ossman2affd772018-06-20 07:03:10 +0200104
Pierre Ossmana4c0aac2017-02-19 15:50:29 +0100105 virtual void readAndDecodeRect(const Rect& r, int encoding,
106 ModifiablePixelBuffer* pb);
107
Pierre Ossman3da238d2015-11-12 12:20:05 +0100108 virtual void framebufferUpdateStart();
109 virtual void framebufferUpdateEnd();
Pierre Ossman9f273e92015-11-09 16:34:54 +0100110 virtual void dataRect(const Rect& r, int encoding);
111
Pierre Ossman615d16b2019-05-03 10:53:06 +0200112 virtual void serverCutText(const char* str);
113
Pierre Ossman9f273e92015-11-09 16:34:54 +0100114
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000115 // Methods to be overridden in a derived class
116
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000117 // authSuccess() is called when authentication has succeeded.
118 virtual void authSuccess();
119
Pierre Ossman2affd772018-06-20 07:03:10 +0200120 // initDone() is called when the connection is fully established
121 // and standard messages can be sent. This is called before the
122 // initial FramebufferUpdateRequest giving a derived class the
Pierre Ossmandd45b442018-10-31 17:08:59 +0100123 // chance to modify pixel format and settings. The derived class
124 // must also make sure it has provided a valid framebuffer before
125 // returning.
126 virtual void initDone() = 0;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000127
Pierre Ossman6ea58ba2018-06-20 15:47:49 +0200128 // resizeFramebuffer() is called whenever the framebuffer
129 // dimensions or the screen layout changes. A subclass must make
130 // sure the pixel buffer has been updated once this call returns.
131 virtual void resizeFramebuffer();
132
Pierre Ossman615d16b2019-05-03 10:53:06 +0200133 // handleClipboardRequest() is called whenever the server requests
134 // the client to send over its clipboard data. It will only be
135 // called after the client has first announced a clipboard change
136 // via announceClipboard().
137 virtual void handleClipboardRequest();
138
139 // handleClipboardAnnounce() is called to indicate a change in the
140 // clipboard on the server. Call requestClipboard() to access the
141 // actual data.
142 virtual void handleClipboardAnnounce(bool available);
143
144 // handleClipboardData() is called when the server has sent over
145 // the clipboard data as a result of a previous call to
146 // requestClipboard(). Note that this function might never be
147 // called if the clipboard data was no longer available when the
148 // server received the request.
149 virtual void handleClipboardData(const char* data);
150
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000151
152 // Other methods
153
Pierre Ossman615d16b2019-05-03 10:53:06 +0200154 // requestClipboard() will result in a request to the server to
155 // transfer its clipboard data. A call to handleClipboardData()
156 // will be made once the data is available.
157 virtual void requestClipboard();
158
159 // announceClipboard() informs the server of changes to the
160 // clipboard on the client. The server may later request the
161 // clipboard data via handleClipboardRequest().
162 virtual void announceClipboard(bool available);
163
164 // sendClipboardData() transfers the clipboard data to the server
165 // and should be called whenever the server has requested the
166 // clipboard via handleClipboardRequest().
167 virtual void sendClipboardData(const char* data);
168
Pierre Ossmanef6881b2018-06-20 11:26:18 +0200169 // refreshFramebuffer() forces a complete refresh of the entire
170 // framebuffer
171 void refreshFramebuffer();
172
173 // setPreferredEncoding()/getPreferredEncoding() adjusts which
174 // encoding is listed first as a hint to the server that it is the
175 // preferred one
176 void setPreferredEncoding(int encoding);
177 int getPreferredEncoding();
178 // setCompressLevel()/setQualityLevel() controls the encoding hints
179 // sent to the server
180 void setCompressLevel(int level);
181 void setQualityLevel(int level);
182 // setPF() controls the pixel format requested from the server.
183 // server.pf() will automatically be adjusted once the new format
184 // is active.
185 void setPF(const PixelFormat& pf);
186
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000187 CMsgReader* reader() { return reader_; }
188 CMsgWriter* writer() { return writer_; }
189
190 rdr::InStream* getInStream() { return is; }
191 rdr::OutStream* getOutStream() { return os; }
192
193 // Access method used by SSecurity implementations that can verify servers'
194 // Identities, to determine the unique(ish) name of the server.
195 const char* getServerName() const { return serverName.buf; }
196
Pierre Ossmandaf3d882017-09-01 11:14:35 +0200197 bool isSecure() const { return csecurity ? csecurity->isSecure() : false; }
198
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000199 enum stateEnum {
200 RFBSTATE_UNINITIALISED,
201 RFBSTATE_PROTOCOL_VERSION,
202 RFBSTATE_SECURITY_TYPES,
203 RFBSTATE_SECURITY,
204 RFBSTATE_SECURITY_RESULT,
205 RFBSTATE_INITIALISATION,
206 RFBSTATE_NORMAL,
207 RFBSTATE_INVALID
208 };
209
210 stateEnum state() { return state_; }
211
Adam Tkacd3b4dea2010-12-08 13:45:40 +0000212 CSecurity *csecurity;
Michal Srbdccb5f72017-03-27 13:55:46 +0300213 SecurityClient security;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000214 protected:
215 void setState(stateEnum s) { state_ = s; }
216
Pierre Ossman0144c532015-02-04 14:10:43 +0100217 void setReader(CMsgReader *r) { reader_ = r; }
218 void setWriter(CMsgWriter *w) { writer_ = w; }
219
Pierre Ossman9f273e92015-11-09 16:34:54 +0100220 ModifiablePixelBuffer* getFramebuffer() { return framebuffer; }
221
Pierre Ossmanb03512c2018-06-20 16:03:23 +0200222 protected:
223 // Optional capabilities that a subclass is expected to set to true
224 // if supported
225 bool supportsLocalCursor;
226 bool supportsDesktopResize;
227 bool supportsLEDState;
228
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000229 private:
Pierre Ossmanc754cce2011-11-14 15:44:11 +0000230 // This is a default implementation of fences that automatically
231 // responds to requests, stating no support for synchronisation.
232 // When overriding, call CMsgHandler::fence() directly in order to
233 // state correct support for fence flags.
234 virtual void fence(rdr::U32 flags, unsigned len, const char data[]);
235
236 private:
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000237 void processVersionMsg();
238 void processSecurityTypesMsg();
239 void processSecurityMsg();
240 void processSecurityResultMsg();
241 void processInitMsg();
242 void throwAuthFailureException();
243 void throwConnFailedException();
244 void securityCompleted();
245
Pierre Ossmanef6881b2018-06-20 11:26:18 +0200246 void requestNewUpdate();
Pierre Ossman96728352018-06-20 11:35:05 +0200247 void updateEncodings();
Pierre Ossmanef6881b2018-06-20 11:26:18 +0200248
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000249 rdr::InStream* is;
250 rdr::OutStream* os;
251 CMsgReader* reader_;
252 CMsgWriter* writer_;
253 bool deleteStreamsWhenDone;
254 bool shared;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000255 stateEnum state_;
256
257 CharArray serverName;
258
Pierre Ossmanef6881b2018-06-20 11:26:18 +0200259 bool pendingPFChange;
260 rfb::PixelFormat pendingPF;
261
262 int preferredEncoding;
Pierre Ossmanb03512c2018-06-20 16:03:23 +0200263 int compressLevel;
264 int qualityLevel;
Pierre Ossmanef6881b2018-06-20 11:26:18 +0200265
266 bool formatChange;
267 rfb::PixelFormat nextPF;
268 bool encodingChange;
269
270 bool firstUpdate;
271 bool pendingUpdate;
272 bool continuousUpdates;
273
274 bool forceNonincremental;
275
Pierre Ossman9f273e92015-11-09 16:34:54 +0100276 ModifiablePixelBuffer* framebuffer;
277 DecodeManager decoder;
Pierre Ossman615d16b2019-05-03 10:53:06 +0200278
279 char* serverClipboard;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000280 };
281}
282#endif