Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 1 | /* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved. |
Pierre Ossman | 615d16b | 2019-05-03 10:53:06 +0200 | [diff] [blame] | 2 | * Copyright 2011-2019 Pierre Ossman for Cendio AB |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 3 | * |
| 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 Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 27 | #include <rfb/CMsgHandler.h> |
Pierre Ossman | 9f273e9 | 2015-11-09 16:34:54 +0100 | [diff] [blame] | 28 | #include <rfb/DecodeManager.h> |
Michal Srb | dccb5f7 | 2017-03-27 13:55:46 +0300 | [diff] [blame] | 29 | #include <rfb/SecurityClient.h> |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 30 | #include <rfb/util.h> |
| 31 | |
| 32 | namespace 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 Tkac | d36b626 | 2009-09-04 10:57:20 +0000 | [diff] [blame] | 51 | void setServerName(const char* name_) { serverName.replaceBuf(strDup(name_)); } |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 52 | |
| 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 Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 61 | // 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 Ossman | 9f273e9 | 2015-11-09 16:34:54 +0100 | [diff] [blame] | 65 | // 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 Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 72 | // 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 Ossman | 9f273e9 | 2015-11-09 16:34:54 +0100 | [diff] [blame] | 90 | // Methods overridden from CMsgHandler |
| 91 | |
Pierre Ossman | 3da238d | 2015-11-12 12:20:05 +0100 | [diff] [blame] | 92 | // 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 Ossman | ef6881b | 2018-06-20 11:26:18 +0200 | [diff] [blame] | 99 | virtual void endOfContinuousUpdates(); |
| 100 | |
Pierre Ossman | dd45b44 | 2018-10-31 17:08:59 +0100 | [diff] [blame] | 101 | virtual void serverInit(int width, int height, |
| 102 | const PixelFormat& pf, |
| 103 | const char* name); |
Pierre Ossman | 2affd77 | 2018-06-20 07:03:10 +0200 | [diff] [blame] | 104 | |
Pierre Ossman | a4c0aac | 2017-02-19 15:50:29 +0100 | [diff] [blame] | 105 | virtual void readAndDecodeRect(const Rect& r, int encoding, |
| 106 | ModifiablePixelBuffer* pb); |
| 107 | |
Pierre Ossman | 3da238d | 2015-11-12 12:20:05 +0100 | [diff] [blame] | 108 | virtual void framebufferUpdateStart(); |
| 109 | virtual void framebufferUpdateEnd(); |
Pierre Ossman | 9f273e9 | 2015-11-09 16:34:54 +0100 | [diff] [blame] | 110 | virtual void dataRect(const Rect& r, int encoding); |
| 111 | |
Pierre Ossman | 615d16b | 2019-05-03 10:53:06 +0200 | [diff] [blame] | 112 | virtual void serverCutText(const char* str); |
| 113 | |
Pierre Ossman | 0ff2655 | 2016-02-05 10:26:56 +0100 | [diff] [blame^] | 114 | virtual void handleClipboardCaps(rdr::U32 flags, |
| 115 | const rdr::U32* lengths); |
| 116 | virtual void handleClipboardRequest(rdr::U32 flags); |
| 117 | virtual void handleClipboardPeek(rdr::U32 flags); |
| 118 | virtual void handleClipboardNotify(rdr::U32 flags); |
| 119 | virtual void handleClipboardProvide(rdr::U32 flags, |
| 120 | const size_t* lengths, |
| 121 | const rdr::U8* const* data); |
| 122 | |
Pierre Ossman | 9f273e9 | 2015-11-09 16:34:54 +0100 | [diff] [blame] | 123 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 124 | // Methods to be overridden in a derived class |
| 125 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 126 | // authSuccess() is called when authentication has succeeded. |
| 127 | virtual void authSuccess(); |
| 128 | |
Pierre Ossman | 2affd77 | 2018-06-20 07:03:10 +0200 | [diff] [blame] | 129 | // initDone() is called when the connection is fully established |
| 130 | // and standard messages can be sent. This is called before the |
| 131 | // initial FramebufferUpdateRequest giving a derived class the |
Pierre Ossman | dd45b44 | 2018-10-31 17:08:59 +0100 | [diff] [blame] | 132 | // chance to modify pixel format and settings. The derived class |
| 133 | // must also make sure it has provided a valid framebuffer before |
| 134 | // returning. |
| 135 | virtual void initDone() = 0; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 136 | |
Pierre Ossman | 6ea58ba | 2018-06-20 15:47:49 +0200 | [diff] [blame] | 137 | // resizeFramebuffer() is called whenever the framebuffer |
| 138 | // dimensions or the screen layout changes. A subclass must make |
| 139 | // sure the pixel buffer has been updated once this call returns. |
| 140 | virtual void resizeFramebuffer(); |
| 141 | |
Pierre Ossman | 615d16b | 2019-05-03 10:53:06 +0200 | [diff] [blame] | 142 | // handleClipboardRequest() is called whenever the server requests |
| 143 | // the client to send over its clipboard data. It will only be |
| 144 | // called after the client has first announced a clipboard change |
| 145 | // via announceClipboard(). |
| 146 | virtual void handleClipboardRequest(); |
| 147 | |
| 148 | // handleClipboardAnnounce() is called to indicate a change in the |
| 149 | // clipboard on the server. Call requestClipboard() to access the |
| 150 | // actual data. |
| 151 | virtual void handleClipboardAnnounce(bool available); |
| 152 | |
| 153 | // handleClipboardData() is called when the server has sent over |
| 154 | // the clipboard data as a result of a previous call to |
| 155 | // requestClipboard(). Note that this function might never be |
| 156 | // called if the clipboard data was no longer available when the |
| 157 | // server received the request. |
| 158 | virtual void handleClipboardData(const char* data); |
| 159 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 160 | |
| 161 | // Other methods |
| 162 | |
Pierre Ossman | 615d16b | 2019-05-03 10:53:06 +0200 | [diff] [blame] | 163 | // requestClipboard() will result in a request to the server to |
| 164 | // transfer its clipboard data. A call to handleClipboardData() |
| 165 | // will be made once the data is available. |
| 166 | virtual void requestClipboard(); |
| 167 | |
| 168 | // announceClipboard() informs the server of changes to the |
| 169 | // clipboard on the client. The server may later request the |
| 170 | // clipboard data via handleClipboardRequest(). |
| 171 | virtual void announceClipboard(bool available); |
| 172 | |
| 173 | // sendClipboardData() transfers the clipboard data to the server |
| 174 | // and should be called whenever the server has requested the |
| 175 | // clipboard via handleClipboardRequest(). |
| 176 | virtual void sendClipboardData(const char* data); |
| 177 | |
Pierre Ossman | ef6881b | 2018-06-20 11:26:18 +0200 | [diff] [blame] | 178 | // refreshFramebuffer() forces a complete refresh of the entire |
| 179 | // framebuffer |
| 180 | void refreshFramebuffer(); |
| 181 | |
| 182 | // setPreferredEncoding()/getPreferredEncoding() adjusts which |
| 183 | // encoding is listed first as a hint to the server that it is the |
| 184 | // preferred one |
| 185 | void setPreferredEncoding(int encoding); |
| 186 | int getPreferredEncoding(); |
| 187 | // setCompressLevel()/setQualityLevel() controls the encoding hints |
| 188 | // sent to the server |
| 189 | void setCompressLevel(int level); |
| 190 | void setQualityLevel(int level); |
| 191 | // setPF() controls the pixel format requested from the server. |
| 192 | // server.pf() will automatically be adjusted once the new format |
| 193 | // is active. |
| 194 | void setPF(const PixelFormat& pf); |
| 195 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 196 | CMsgReader* reader() { return reader_; } |
| 197 | CMsgWriter* writer() { return writer_; } |
| 198 | |
| 199 | rdr::InStream* getInStream() { return is; } |
| 200 | rdr::OutStream* getOutStream() { return os; } |
| 201 | |
| 202 | // Access method used by SSecurity implementations that can verify servers' |
| 203 | // Identities, to determine the unique(ish) name of the server. |
| 204 | const char* getServerName() const { return serverName.buf; } |
| 205 | |
Pierre Ossman | daf3d88 | 2017-09-01 11:14:35 +0200 | [diff] [blame] | 206 | bool isSecure() const { return csecurity ? csecurity->isSecure() : false; } |
| 207 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 208 | enum stateEnum { |
| 209 | RFBSTATE_UNINITIALISED, |
| 210 | RFBSTATE_PROTOCOL_VERSION, |
| 211 | RFBSTATE_SECURITY_TYPES, |
| 212 | RFBSTATE_SECURITY, |
| 213 | RFBSTATE_SECURITY_RESULT, |
| 214 | RFBSTATE_INITIALISATION, |
| 215 | RFBSTATE_NORMAL, |
| 216 | RFBSTATE_INVALID |
| 217 | }; |
| 218 | |
| 219 | stateEnum state() { return state_; } |
| 220 | |
Adam Tkac | d3b4dea | 2010-12-08 13:45:40 +0000 | [diff] [blame] | 221 | CSecurity *csecurity; |
Michal Srb | dccb5f7 | 2017-03-27 13:55:46 +0300 | [diff] [blame] | 222 | SecurityClient security; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 223 | protected: |
| 224 | void setState(stateEnum s) { state_ = s; } |
| 225 | |
Pierre Ossman | 0144c53 | 2015-02-04 14:10:43 +0100 | [diff] [blame] | 226 | void setReader(CMsgReader *r) { reader_ = r; } |
| 227 | void setWriter(CMsgWriter *w) { writer_ = w; } |
| 228 | |
Pierre Ossman | 9f273e9 | 2015-11-09 16:34:54 +0100 | [diff] [blame] | 229 | ModifiablePixelBuffer* getFramebuffer() { return framebuffer; } |
| 230 | |
Pierre Ossman | b03512c | 2018-06-20 16:03:23 +0200 | [diff] [blame] | 231 | protected: |
| 232 | // Optional capabilities that a subclass is expected to set to true |
| 233 | // if supported |
| 234 | bool supportsLocalCursor; |
| 235 | bool supportsDesktopResize; |
| 236 | bool supportsLEDState; |
| 237 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 238 | private: |
Pierre Ossman | c754cce | 2011-11-14 15:44:11 +0000 | [diff] [blame] | 239 | // This is a default implementation of fences that automatically |
| 240 | // responds to requests, stating no support for synchronisation. |
| 241 | // When overriding, call CMsgHandler::fence() directly in order to |
| 242 | // state correct support for fence flags. |
| 243 | virtual void fence(rdr::U32 flags, unsigned len, const char data[]); |
| 244 | |
| 245 | private: |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 246 | void processVersionMsg(); |
| 247 | void processSecurityTypesMsg(); |
| 248 | void processSecurityMsg(); |
| 249 | void processSecurityResultMsg(); |
| 250 | void processInitMsg(); |
| 251 | void throwAuthFailureException(); |
| 252 | void throwConnFailedException(); |
| 253 | void securityCompleted(); |
| 254 | |
Pierre Ossman | ef6881b | 2018-06-20 11:26:18 +0200 | [diff] [blame] | 255 | void requestNewUpdate(); |
Pierre Ossman | 9672835 | 2018-06-20 11:35:05 +0200 | [diff] [blame] | 256 | void updateEncodings(); |
Pierre Ossman | ef6881b | 2018-06-20 11:26:18 +0200 | [diff] [blame] | 257 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 258 | rdr::InStream* is; |
| 259 | rdr::OutStream* os; |
| 260 | CMsgReader* reader_; |
| 261 | CMsgWriter* writer_; |
| 262 | bool deleteStreamsWhenDone; |
| 263 | bool shared; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 264 | stateEnum state_; |
| 265 | |
| 266 | CharArray serverName; |
| 267 | |
Pierre Ossman | ef6881b | 2018-06-20 11:26:18 +0200 | [diff] [blame] | 268 | bool pendingPFChange; |
| 269 | rfb::PixelFormat pendingPF; |
| 270 | |
| 271 | int preferredEncoding; |
Pierre Ossman | b03512c | 2018-06-20 16:03:23 +0200 | [diff] [blame] | 272 | int compressLevel; |
| 273 | int qualityLevel; |
Pierre Ossman | ef6881b | 2018-06-20 11:26:18 +0200 | [diff] [blame] | 274 | |
| 275 | bool formatChange; |
| 276 | rfb::PixelFormat nextPF; |
| 277 | bool encodingChange; |
| 278 | |
| 279 | bool firstUpdate; |
| 280 | bool pendingUpdate; |
| 281 | bool continuousUpdates; |
| 282 | |
| 283 | bool forceNonincremental; |
| 284 | |
Pierre Ossman | 9f273e9 | 2015-11-09 16:34:54 +0100 | [diff] [blame] | 285 | ModifiablePixelBuffer* framebuffer; |
| 286 | DecodeManager decoder; |
Pierre Ossman | 615d16b | 2019-05-03 10:53:06 +0200 | [diff] [blame] | 287 | |
| 288 | char* serverClipboard; |
Pierre Ossman | 0ff2655 | 2016-02-05 10:26:56 +0100 | [diff] [blame^] | 289 | bool hasLocalClipboard; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 290 | }; |
| 291 | } |
| 292 | #endif |