Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 1 | /* 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 | |
| 26 | #include <rdr/InStream.h> |
| 27 | #include <rdr/OutStream.h> |
| 28 | #include <rfb/CMsgHandler.h> |
Adam Tkac | f324dc4 | 2010-04-23 14:10:17 +0000 | [diff] [blame] | 29 | #include <rfb/CSecurity.h> |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 30 | #include <rfb/util.h> |
Adam Tkac | bfd66c1 | 2010-10-01 08:33:29 +0000 | [diff] [blame] | 31 | #include <rfb/SecurityClient.h> |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 32 | |
| 33 | namespace rfb { |
| 34 | |
| 35 | class CMsgReader; |
| 36 | class CMsgWriter; |
| 37 | class CSecurity; |
| 38 | class IdentityVerifier; |
| 39 | |
| 40 | class CConnection : public CMsgHandler { |
| 41 | public: |
| 42 | |
| 43 | CConnection(); |
| 44 | virtual ~CConnection(); |
| 45 | |
| 46 | // Methods to initialise the connection |
| 47 | |
| 48 | // setServerName() is used to provide a unique(ish) name for the server to |
| 49 | // which we are connected. This might be the result of getPeerEndpoint on |
| 50 | // a TcpSocket, for example, or a host specified by DNS name & port. |
| 51 | // 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] | 52 | void setServerName(const char* name_) { serverName.replaceBuf(strDup(name_)); } |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 53 | |
| 54 | // setStreams() sets the streams to be used for the connection. These must |
| 55 | // be set before initialiseProtocol() and processMsg() are called. The |
| 56 | // CSecurity object may call setStreams() again to provide alternative |
| 57 | // streams over which the RFB protocol is sent (i.e. encrypting/decrypting |
| 58 | // streams). Ownership of the streams remains with the caller |
| 59 | // (i.e. SConnection will not delete them). |
| 60 | void setStreams(rdr::InStream* is, rdr::OutStream* os); |
| 61 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 62 | // setShared sets the value of the shared flag which will be sent to the |
| 63 | // server upon initialisation. |
| 64 | void setShared(bool s) { shared = s; } |
| 65 | |
| 66 | // setProtocol3_3 configures whether or not the CConnection should |
| 67 | // only ever support protocol version 3.3 |
| 68 | void setProtocol3_3(bool s) {useProtocol3_3 = s;} |
| 69 | |
| 70 | // initialiseProtocol() should be called once the streams and security |
| 71 | // types are set. Subsequently, processMsg() should be called whenever |
| 72 | // there is data to read on the InStream. |
| 73 | void initialiseProtocol(); |
| 74 | |
| 75 | // processMsg() should be called whenever there is either: |
| 76 | // - data available on the underlying network stream |
| 77 | // In this case, processMsg may return without processing an RFB message, |
| 78 | // if the available data does not result in an RFB message being ready |
| 79 | // to handle. e.g. if data is encrypted. |
| 80 | // NB: This makes it safe to call processMsg() in response to select() |
| 81 | // - data available on the CConnection's current InStream |
| 82 | // In this case, processMsg should always process the available RFB |
| 83 | // message before returning. |
| 84 | // NB: In either case, you must have called initialiseProtocol() first. |
| 85 | void processMsg(); |
| 86 | |
| 87 | |
| 88 | // Methods to be overridden in a derived class |
| 89 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 90 | // getIdVerifier() returns the identity verifier associated with the connection. |
| 91 | // Ownership of the IdentityVerifier is retained by the CConnection instance. |
| 92 | virtual IdentityVerifier* getIdentityVerifier() {return 0;} |
| 93 | |
| 94 | // authSuccess() is called when authentication has succeeded. |
| 95 | virtual void authSuccess(); |
| 96 | |
| 97 | // serverInit() is called when the ServerInit message is received. The |
| 98 | // derived class must call on to CConnection::serverInit(). |
| 99 | virtual void serverInit(); |
| 100 | |
| 101 | |
| 102 | // Other methods |
| 103 | |
| 104 | // deleteReaderAndWriter() deletes the reader and writer associated with |
| 105 | // this connection. This may be useful if you want to delete the streams |
| 106 | // before deleting the SConnection to make sure that no attempt by the |
| 107 | // SConnection is made to read or write. |
| 108 | // XXX Do we really need this at all??? |
| 109 | void deleteReaderAndWriter(); |
| 110 | |
| 111 | CMsgReader* reader() { return reader_; } |
| 112 | CMsgWriter* writer() { return writer_; } |
| 113 | |
| 114 | rdr::InStream* getInStream() { return is; } |
| 115 | rdr::OutStream* getOutStream() { return os; } |
| 116 | |
| 117 | // Access method used by SSecurity implementations that can verify servers' |
| 118 | // Identities, to determine the unique(ish) name of the server. |
| 119 | const char* getServerName() const { return serverName.buf; } |
| 120 | |
| 121 | enum stateEnum { |
| 122 | RFBSTATE_UNINITIALISED, |
| 123 | RFBSTATE_PROTOCOL_VERSION, |
| 124 | RFBSTATE_SECURITY_TYPES, |
| 125 | RFBSTATE_SECURITY, |
| 126 | RFBSTATE_SECURITY_RESULT, |
| 127 | RFBSTATE_INITIALISATION, |
| 128 | RFBSTATE_NORMAL, |
| 129 | RFBSTATE_INVALID |
| 130 | }; |
| 131 | |
| 132 | stateEnum state() { return state_; } |
| 133 | |
Adam Tkac | d3b4dea | 2010-12-08 13:45:40 +0000 | [diff] [blame] | 134 | CSecurity *csecurity; |
| 135 | SecurityClient *security; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 136 | protected: |
| 137 | void setState(stateEnum s) { state_ = s; } |
| 138 | |
| 139 | private: |
Pierre Ossman | c754cce | 2011-11-14 15:44:11 +0000 | [diff] [blame^] | 140 | // This is a default implementation of fences that automatically |
| 141 | // responds to requests, stating no support for synchronisation. |
| 142 | // When overriding, call CMsgHandler::fence() directly in order to |
| 143 | // state correct support for fence flags. |
| 144 | virtual void fence(rdr::U32 flags, unsigned len, const char data[]); |
| 145 | |
| 146 | private: |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 147 | void processVersionMsg(); |
| 148 | void processSecurityTypesMsg(); |
| 149 | void processSecurityMsg(); |
| 150 | void processSecurityResultMsg(); |
| 151 | void processInitMsg(); |
| 152 | void throwAuthFailureException(); |
| 153 | void throwConnFailedException(); |
| 154 | void securityCompleted(); |
| 155 | |
| 156 | rdr::InStream* is; |
| 157 | rdr::OutStream* os; |
| 158 | CMsgReader* reader_; |
| 159 | CMsgWriter* writer_; |
| 160 | bool deleteStreamsWhenDone; |
| 161 | bool shared; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 162 | stateEnum state_; |
| 163 | |
| 164 | CharArray serverName; |
| 165 | |
| 166 | bool useProtocol3_3; |
| 167 | }; |
| 168 | } |
| 169 | #endif |