Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 1 | /* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved. |
Pierre Ossman | a4c0aac | 2017-02-19 15:50:29 +0100 | [diff] [blame] | 2 | * Copyright 2011-2017 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 | |
| 65 | // setProtocol3_3 configures whether or not the CConnection should |
| 66 | // only ever support protocol version 3.3 |
| 67 | void setProtocol3_3(bool s) {useProtocol3_3 = s;} |
| 68 | |
Pierre Ossman | 9f273e9 | 2015-11-09 16:34:54 +0100 | [diff] [blame] | 69 | // setFramebuffer configures the PixelBuffer that the CConnection |
| 70 | // should render all pixel data in to. Note that the CConnection |
| 71 | // takes ownership of the PixelBuffer and it must not be deleted by |
| 72 | // anyone else. Call setFramebuffer again with NULL or a different |
| 73 | // PixelBuffer to delete the previous one. |
| 74 | void setFramebuffer(ModifiablePixelBuffer* fb); |
| 75 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 76 | // initialiseProtocol() should be called once the streams and security |
| 77 | // types are set. Subsequently, processMsg() should be called whenever |
| 78 | // there is data to read on the InStream. |
| 79 | void initialiseProtocol(); |
| 80 | |
| 81 | // processMsg() should be called whenever there is either: |
| 82 | // - data available on the underlying network stream |
| 83 | // In this case, processMsg may return without processing an RFB message, |
| 84 | // if the available data does not result in an RFB message being ready |
| 85 | // to handle. e.g. if data is encrypted. |
| 86 | // NB: This makes it safe to call processMsg() in response to select() |
| 87 | // - data available on the CConnection's current InStream |
| 88 | // In this case, processMsg should always process the available RFB |
| 89 | // message before returning. |
| 90 | // NB: In either case, you must have called initialiseProtocol() first. |
| 91 | void processMsg(); |
| 92 | |
| 93 | |
Pierre Ossman | 9f273e9 | 2015-11-09 16:34:54 +0100 | [diff] [blame] | 94 | // Methods overridden from CMsgHandler |
| 95 | |
Pierre Ossman | 3da238d | 2015-11-12 12:20:05 +0100 | [diff] [blame] | 96 | // Note: These must be called by any deriving classes |
| 97 | |
| 98 | virtual void setDesktopSize(int w, int h); |
| 99 | virtual void setExtendedDesktopSize(unsigned reason, unsigned result, |
| 100 | int w, int h, |
| 101 | const ScreenSet& layout); |
| 102 | |
Pierre Ossman | 2affd77 | 2018-06-20 07:03:10 +0200 | [diff] [blame^] | 103 | virtual void serverInit(); |
| 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 | |
| 112 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 113 | // Methods to be overridden in a derived class |
| 114 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 115 | // authSuccess() is called when authentication has succeeded. |
| 116 | virtual void authSuccess(); |
| 117 | |
Pierre Ossman | 2affd77 | 2018-06-20 07:03:10 +0200 | [diff] [blame^] | 118 | // initDone() is called when the connection is fully established |
| 119 | // and standard messages can be sent. This is called before the |
| 120 | // initial FramebufferUpdateRequest giving a derived class the |
| 121 | // chance to modify pixel format and settings. |
| 122 | virtual void initDone(); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 123 | |
| 124 | |
| 125 | // Other methods |
| 126 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 127 | CMsgReader* reader() { return reader_; } |
| 128 | CMsgWriter* writer() { return writer_; } |
| 129 | |
| 130 | rdr::InStream* getInStream() { return is; } |
| 131 | rdr::OutStream* getOutStream() { return os; } |
| 132 | |
| 133 | // Access method used by SSecurity implementations that can verify servers' |
| 134 | // Identities, to determine the unique(ish) name of the server. |
| 135 | const char* getServerName() const { return serverName.buf; } |
| 136 | |
Pierre Ossman | daf3d88 | 2017-09-01 11:14:35 +0200 | [diff] [blame] | 137 | bool isSecure() const { return csecurity ? csecurity->isSecure() : false; } |
| 138 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 139 | enum stateEnum { |
| 140 | RFBSTATE_UNINITIALISED, |
| 141 | RFBSTATE_PROTOCOL_VERSION, |
| 142 | RFBSTATE_SECURITY_TYPES, |
| 143 | RFBSTATE_SECURITY, |
| 144 | RFBSTATE_SECURITY_RESULT, |
| 145 | RFBSTATE_INITIALISATION, |
| 146 | RFBSTATE_NORMAL, |
| 147 | RFBSTATE_INVALID |
| 148 | }; |
| 149 | |
| 150 | stateEnum state() { return state_; } |
| 151 | |
Adam Tkac | d3b4dea | 2010-12-08 13:45:40 +0000 | [diff] [blame] | 152 | CSecurity *csecurity; |
Michal Srb | dccb5f7 | 2017-03-27 13:55:46 +0300 | [diff] [blame] | 153 | SecurityClient security; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 154 | protected: |
| 155 | void setState(stateEnum s) { state_ = s; } |
| 156 | |
Pierre Ossman | 0144c53 | 2015-02-04 14:10:43 +0100 | [diff] [blame] | 157 | void setReader(CMsgReader *r) { reader_ = r; } |
| 158 | void setWriter(CMsgWriter *w) { writer_ = w; } |
| 159 | |
Pierre Ossman | 9f273e9 | 2015-11-09 16:34:54 +0100 | [diff] [blame] | 160 | ModifiablePixelBuffer* getFramebuffer() { return framebuffer; } |
| 161 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 162 | private: |
Pierre Ossman | c754cce | 2011-11-14 15:44:11 +0000 | [diff] [blame] | 163 | // This is a default implementation of fences that automatically |
| 164 | // responds to requests, stating no support for synchronisation. |
| 165 | // When overriding, call CMsgHandler::fence() directly in order to |
| 166 | // state correct support for fence flags. |
| 167 | virtual void fence(rdr::U32 flags, unsigned len, const char data[]); |
| 168 | |
| 169 | private: |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 170 | void processVersionMsg(); |
| 171 | void processSecurityTypesMsg(); |
| 172 | void processSecurityMsg(); |
| 173 | void processSecurityResultMsg(); |
| 174 | void processInitMsg(); |
| 175 | void throwAuthFailureException(); |
| 176 | void throwConnFailedException(); |
| 177 | void securityCompleted(); |
| 178 | |
| 179 | rdr::InStream* is; |
| 180 | rdr::OutStream* os; |
| 181 | CMsgReader* reader_; |
| 182 | CMsgWriter* writer_; |
| 183 | bool deleteStreamsWhenDone; |
| 184 | bool shared; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 185 | stateEnum state_; |
| 186 | |
| 187 | CharArray serverName; |
| 188 | |
| 189 | bool useProtocol3_3; |
Pierre Ossman | 9f273e9 | 2015-11-09 16:34:54 +0100 | [diff] [blame] | 190 | |
| 191 | ModifiablePixelBuffer* framebuffer; |
| 192 | DecodeManager decoder; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 193 | }; |
| 194 | } |
| 195 | #endif |