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> |
| 29 | #include <rfb/util.h> |
| 30 | |
| 31 | namespace rfb { |
| 32 | |
| 33 | class CMsgReader; |
| 34 | class CMsgWriter; |
| 35 | class CSecurity; |
| 36 | class IdentityVerifier; |
| 37 | |
| 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 Tkac | d36b626 | 2009-09-04 10:57:20 +0000 | [diff] [blame^] | 50 | void setServerName(const char* name_) { serverName.replaceBuf(strDup(name_)); } |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 51 | |
| 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 | |
| 60 | // addSecType() should be called once for each security type which the |
| 61 | // client supports. The order in which they're added is such that the |
| 62 | // first one is most preferred. |
| 63 | void addSecType(rdr::U8 secType); |
| 64 | |
| 65 | // setClientSecTypeOrder() determines whether the client should obey |
| 66 | // the server's security type preference, by picking the first server security |
| 67 | // type that the client supports, or whether it should pick the first type |
| 68 | // that the server supports, from the client-supported list of types. |
| 69 | void setClientSecTypeOrder(bool clientOrder); |
| 70 | |
| 71 | // setShared sets the value of the shared flag which will be sent to the |
| 72 | // server upon initialisation. |
| 73 | void setShared(bool s) { shared = s; } |
| 74 | |
| 75 | // setProtocol3_3 configures whether or not the CConnection should |
| 76 | // only ever support protocol version 3.3 |
| 77 | void setProtocol3_3(bool s) {useProtocol3_3 = s;} |
| 78 | |
| 79 | // initialiseProtocol() should be called once the streams and security |
| 80 | // types are set. Subsequently, processMsg() should be called whenever |
| 81 | // there is data to read on the InStream. |
| 82 | void initialiseProtocol(); |
| 83 | |
| 84 | // processMsg() should be called whenever there is either: |
| 85 | // - data available on the underlying network stream |
| 86 | // In this case, processMsg may return without processing an RFB message, |
| 87 | // if the available data does not result in an RFB message being ready |
| 88 | // to handle. e.g. if data is encrypted. |
| 89 | // NB: This makes it safe to call processMsg() in response to select() |
| 90 | // - data available on the CConnection's current InStream |
| 91 | // In this case, processMsg should always process the available RFB |
| 92 | // message before returning. |
| 93 | // NB: In either case, you must have called initialiseProtocol() first. |
| 94 | void processMsg(); |
| 95 | |
| 96 | |
| 97 | // Methods to be overridden in a derived class |
| 98 | |
| 99 | // getCSecurity() gets the CSecurity object for the given type. The type |
| 100 | // is guaranteed to be one of the secTypes passed in to addSecType(). The |
| 101 | // CSecurity object's destroy() method will be called by the CConnection |
| 102 | // from its destructor. |
| 103 | virtual CSecurity* getCSecurity(int secType)=0; |
| 104 | |
| 105 | // getCurrentCSecurity() gets the CSecurity instance used for this connection. |
| 106 | const CSecurity* getCurrentCSecurity() const {return security;} |
| 107 | |
| 108 | // getIdVerifier() returns the identity verifier associated with the connection. |
| 109 | // Ownership of the IdentityVerifier is retained by the CConnection instance. |
| 110 | virtual IdentityVerifier* getIdentityVerifier() {return 0;} |
| 111 | |
| 112 | // authSuccess() is called when authentication has succeeded. |
| 113 | virtual void authSuccess(); |
| 114 | |
| 115 | // serverInit() is called when the ServerInit message is received. The |
| 116 | // derived class must call on to CConnection::serverInit(). |
| 117 | virtual void serverInit(); |
| 118 | |
| 119 | |
| 120 | // Other methods |
| 121 | |
| 122 | // deleteReaderAndWriter() deletes the reader and writer associated with |
| 123 | // this connection. This may be useful if you want to delete the streams |
| 124 | // before deleting the SConnection to make sure that no attempt by the |
| 125 | // SConnection is made to read or write. |
| 126 | // XXX Do we really need this at all??? |
| 127 | void deleteReaderAndWriter(); |
| 128 | |
| 129 | CMsgReader* reader() { return reader_; } |
| 130 | CMsgWriter* writer() { return writer_; } |
| 131 | |
| 132 | rdr::InStream* getInStream() { return is; } |
| 133 | rdr::OutStream* getOutStream() { return os; } |
| 134 | |
| 135 | // Access method used by SSecurity implementations that can verify servers' |
| 136 | // Identities, to determine the unique(ish) name of the server. |
| 137 | const char* getServerName() const { return serverName.buf; } |
| 138 | |
| 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 | |
| 152 | protected: |
| 153 | void setState(stateEnum s) { state_ = s; } |
| 154 | |
| 155 | private: |
| 156 | void processVersionMsg(); |
| 157 | void processSecurityTypesMsg(); |
| 158 | void processSecurityMsg(); |
| 159 | void processSecurityResultMsg(); |
| 160 | void processInitMsg(); |
| 161 | void throwAuthFailureException(); |
| 162 | void throwConnFailedException(); |
| 163 | void securityCompleted(); |
| 164 | |
| 165 | rdr::InStream* is; |
| 166 | rdr::OutStream* os; |
| 167 | CMsgReader* reader_; |
| 168 | CMsgWriter* writer_; |
| 169 | bool deleteStreamsWhenDone; |
| 170 | bool shared; |
| 171 | CSecurity* security; |
| 172 | enum { maxSecTypes = 8 }; |
| 173 | int nSecTypes; |
| 174 | rdr::U8 secTypes[maxSecTypes]; |
| 175 | bool clientSecTypeOrder; |
| 176 | stateEnum state_; |
| 177 | |
| 178 | CharArray serverName; |
| 179 | |
| 180 | bool useProtocol3_3; |
| 181 | }; |
| 182 | } |
| 183 | #endif |