Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 1 | /* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved. |
Pierre Ossman | c754cce | 2011-11-14 15:44:11 +0000 | [diff] [blame^] | 2 | * Copyright 2011 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 | // SConnection - class on the server side representing a connection to a |
| 21 | // client. A derived class should override methods appropriately. |
| 22 | // |
| 23 | |
| 24 | #ifndef __RFB_SCONNECTION_H__ |
| 25 | #define __RFB_SCONNECTION_H__ |
| 26 | |
| 27 | #include <rdr/InStream.h> |
| 28 | #include <rdr/OutStream.h> |
| 29 | #include <rfb/SMsgHandler.h> |
Adam Tkac | bfd66c1 | 2010-10-01 08:33:29 +0000 | [diff] [blame] | 30 | #include <rfb/SecurityServer.h> |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 31 | #include <rfb/SSecurity.h> |
| 32 | |
| 33 | namespace rfb { |
| 34 | |
| 35 | class SMsgReader; |
| 36 | class SMsgWriter; |
| 37 | class SSecurity; |
| 38 | |
| 39 | class SConnection : public SMsgHandler { |
| 40 | public: |
| 41 | |
Adam Tkac | a6578bf | 2010-04-23 14:07:41 +0000 | [diff] [blame] | 42 | SConnection(bool reverseConnection_); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 43 | virtual ~SConnection(); |
| 44 | |
| 45 | // Methods to initialise the connection |
| 46 | |
| 47 | // setStreams() sets the streams to be used for the connection. These must |
| 48 | // be set before initialiseProtocol() and processMsg() are called. The |
| 49 | // SSecurity object may call setStreams() again to provide alternative |
| 50 | // streams over which the RFB protocol is sent (i.e. encrypting/decrypting |
| 51 | // streams). Ownership of the streams remains with the caller |
| 52 | // (i.e. SConnection will not delete them). |
| 53 | void setStreams(rdr::InStream* is, rdr::OutStream* os); |
| 54 | |
| 55 | // initialiseProtocol() should be called once the streams and security |
| 56 | // types are set. Subsequently, processMsg() should be called whenever |
| 57 | // there is data to read on the InStream. |
| 58 | void initialiseProtocol(); |
| 59 | |
| 60 | // processMsg() should be called whenever there is data to read on the |
| 61 | // InStream. You must have called initialiseProtocol() first. |
| 62 | void processMsg(); |
| 63 | |
| 64 | // approveConnection() is called to either accept or reject the connection. |
| 65 | // If accept is false, the reason string gives the reason for the |
| 66 | // rejection. It can either be called directly from queryConnection() or |
| 67 | // later, after queryConnection() has returned. It can only be called when |
| 68 | // in state RFBSTATE_QUERYING. On rejection, an AuthFailureException is |
| 69 | // thrown, so this must be handled appropriately by the caller. |
| 70 | void approveConnection(bool accept, const char* reason=0); |
| 71 | |
| 72 | |
| 73 | // Methods to be overridden in a derived class |
| 74 | |
| 75 | // versionReceived() indicates that the version number has just been read |
| 76 | // from the client. The version will already have been "cooked" |
| 77 | // to deal with unknown/bogus viewer protocol numbers. |
| 78 | virtual void versionReceived(); |
| 79 | |
| 80 | // authSuccess() is called when authentication has succeeded. |
| 81 | virtual void authSuccess(); |
| 82 | |
| 83 | // queryConnection() is called when authentication has succeeded, but |
| 84 | // before informing the client. It can be overridden to query a local user |
| 85 | // to accept the incoming connection, for example. The userName argument |
| 86 | // is the name of the user making the connection, or null (note that the |
| 87 | // storage for userName is owned by the caller). The connection must be |
| 88 | // accepted or rejected by calling approveConnection(), either directly |
| 89 | // from queryConnection() or some time later. |
| 90 | virtual void queryConnection(const char* userName); |
| 91 | |
| 92 | // clientInit() is called when the ClientInit message is received. The |
| 93 | // derived class must call on to SConnection::clientInit(). |
| 94 | virtual void clientInit(bool shared); |
| 95 | |
| 96 | // setPixelFormat() is called when a SetPixelFormat message is received. |
| 97 | // The derived class must call on to SConnection::setPixelFormat(). |
| 98 | virtual void setPixelFormat(const PixelFormat& pf); |
| 99 | |
| 100 | // framebufferUpdateRequest() is called when a FramebufferUpdateRequest |
| 101 | // message is received. The derived class must call on to |
| 102 | // SConnection::framebufferUpdateRequest(). |
| 103 | virtual void framebufferUpdateRequest(const Rect& r, bool incremental); |
| 104 | |
| 105 | // setInitialColourMap() is called when the client needs an initial |
| 106 | // SetColourMapEntries message. In fact this only happens when the client |
| 107 | // accepts the server's default pixel format and it uses a colour map. |
| 108 | virtual void setInitialColourMap(); |
| 109 | |
Pierre Ossman | c754cce | 2011-11-14 15:44:11 +0000 | [diff] [blame^] | 110 | // fence() is called when we get a fence request or response. By default |
| 111 | // it responds directly to requests (stating it doesn't support any |
| 112 | // synchronisation) and drops responses. Override to implement more proper |
| 113 | // support. |
| 114 | virtual void fence(rdr::U32 flags, unsigned len, const char data[]); |
| 115 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 116 | // setAccessRights() allows a security package to limit the access rights |
| 117 | // of a VNCSConnectionST to the server. How the access rights are treated |
| 118 | // is up to the derived class. |
| 119 | |
| 120 | typedef rdr::U16 AccessRights; |
| 121 | static const AccessRights AccessView; // View display contents |
| 122 | static const AccessRights AccessKeyEvents; // Send key events |
| 123 | static const AccessRights AccessPtrEvents; // Send pointer events |
| 124 | static const AccessRights AccessCutText; // Send/receive clipboard events |
| 125 | static const AccessRights AccessDefault; // The default rights, INCLUDING FUTURE ONES |
| 126 | static const AccessRights AccessNoQuery; // Connect without local user accepting |
| 127 | static const AccessRights AccessFull; // All of the available AND FUTURE rights |
| 128 | virtual void setAccessRights(AccessRights ar) = 0; |
| 129 | |
| 130 | // Other methods |
| 131 | |
| 132 | // authenticated() returns true if the client has authenticated |
| 133 | // successfully. |
| 134 | bool authenticated() { return (state_ == RFBSTATE_INITIALISATION || |
| 135 | state_ == RFBSTATE_NORMAL); } |
| 136 | |
| 137 | // deleteReaderAndWriter() deletes the reader and writer associated with |
| 138 | // this connection. This may be useful if you want to delete the streams |
| 139 | // before deleting the SConnection to make sure that no attempt by the |
| 140 | // SConnection is made to read or write. |
| 141 | // XXX Do we really need this at all??? |
| 142 | void deleteReaderAndWriter(); |
| 143 | |
| 144 | // throwConnFailedException() prints a message to the log, sends a conn |
| 145 | // failed message to the client (if possible) and throws a |
| 146 | // ConnFailedException. |
| 147 | void throwConnFailedException(const char* msg); |
| 148 | |
| 149 | // writeConnFailedFromScratch() sends a conn failed message to an OutStream |
| 150 | // without the need to negotiate the protocol version first. It actually |
| 151 | // does this by assuming that the client will understand version 3.3 of the |
| 152 | // protocol. |
| 153 | static void writeConnFailedFromScratch(const char* msg, |
| 154 | rdr::OutStream* os); |
| 155 | |
| 156 | SMsgReader* reader() { return reader_; } |
| 157 | SMsgWriter* writer() { return writer_; } |
| 158 | |
| 159 | rdr::InStream* getInStream() { return is; } |
| 160 | rdr::OutStream* getOutStream() { return os; } |
| 161 | |
| 162 | enum stateEnum { |
| 163 | RFBSTATE_UNINITIALISED, |
| 164 | RFBSTATE_PROTOCOL_VERSION, |
| 165 | RFBSTATE_SECURITY_TYPE, |
| 166 | RFBSTATE_SECURITY, |
| 167 | RFBSTATE_QUERYING, |
| 168 | RFBSTATE_INITIALISATION, |
| 169 | RFBSTATE_NORMAL, |
| 170 | RFBSTATE_CLOSING, |
| 171 | RFBSTATE_INVALID |
| 172 | }; |
| 173 | |
| 174 | stateEnum state() { return state_; } |
| 175 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 176 | protected: |
| 177 | void setState(stateEnum s) { state_ = s; } |
| 178 | |
| 179 | bool readyForSetColourMapEntries; |
| 180 | |
| 181 | void processVersionMsg(); |
| 182 | void processSecurityTypeMsg(); |
Constantin Kaplinsky | 5fa9d22 | 2006-09-06 10:32:06 +0000 | [diff] [blame] | 183 | void processSecurityType(int secType); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 184 | void processSecurityMsg(); |
| 185 | void processInitMsg(); |
| 186 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 187 | int defaultMajorVersion, defaultMinorVersion; |
| 188 | rdr::InStream* is; |
| 189 | rdr::OutStream* os; |
| 190 | SMsgReader* reader_; |
| 191 | SMsgWriter* writer_; |
Adam Tkac | bfd66c1 | 2010-10-01 08:33:29 +0000 | [diff] [blame] | 192 | SecurityServer *security; |
Adam Tkac | a6578bf | 2010-04-23 14:07:41 +0000 | [diff] [blame] | 193 | SSecurity* ssecurity; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 194 | stateEnum state_; |
| 195 | bool reverseConnection; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 196 | }; |
| 197 | } |
| 198 | #endif |