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 | // 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 | |
| 32 | namespace rfb { |
| 33 | |
| 34 | class SMsgReader; |
| 35 | class SMsgWriter; |
| 36 | class SSecurity; |
| 37 | |
| 38 | class SConnection : public SMsgHandler { |
| 39 | public: |
| 40 | |
Pierre Ossman | 7069bdd | 2015-02-06 14:41:58 +0100 | [diff] [blame] | 41 | SConnection(); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 42 | virtual ~SConnection(); |
| 43 | |
| 44 | // Methods to initialise the connection |
| 45 | |
| 46 | // setStreams() sets the streams to be used for the connection. These must |
| 47 | // be set before initialiseProtocol() and processMsg() are called. The |
| 48 | // SSecurity object may call setStreams() again to provide alternative |
| 49 | // streams over which the RFB protocol is sent (i.e. encrypting/decrypting |
| 50 | // streams). Ownership of the streams remains with the caller |
| 51 | // (i.e. SConnection will not delete them). |
| 52 | void setStreams(rdr::InStream* is, rdr::OutStream* os); |
| 53 | |
| 54 | // initialiseProtocol() should be called once the streams and security |
| 55 | // types are set. Subsequently, processMsg() should be called whenever |
| 56 | // there is data to read on the InStream. |
| 57 | void initialiseProtocol(); |
| 58 | |
| 59 | // processMsg() should be called whenever there is data to read on the |
| 60 | // InStream. You must have called initialiseProtocol() first. |
| 61 | void processMsg(); |
| 62 | |
| 63 | // approveConnection() is called to either accept or reject the connection. |
| 64 | // If accept is false, the reason string gives the reason for the |
| 65 | // rejection. It can either be called directly from queryConnection() or |
| 66 | // later, after queryConnection() has returned. It can only be called when |
| 67 | // in state RFBSTATE_QUERYING. On rejection, an AuthFailureException is |
| 68 | // thrown, so this must be handled appropriately by the caller. |
| 69 | void approveConnection(bool accept, const char* reason=0); |
| 70 | |
| 71 | |
Pierre Ossman | 7d64b33 | 2018-10-08 15:59:02 +0200 | [diff] [blame] | 72 | // Methods to terminate the connection |
| 73 | |
| 74 | // close() shuts down the connection to the client and awaits |
| 75 | // cleanup of the SConnection object by the server |
| 76 | virtual void close(const char* reason); |
| 77 | |
| 78 | |
Pierre Ossman | 4870081 | 2014-09-17 17:11:56 +0200 | [diff] [blame] | 79 | // Overridden from SMsgHandler |
| 80 | |
Pierre Ossman | f38e243 | 2015-02-11 13:47:58 +0100 | [diff] [blame] | 81 | virtual void setEncodings(int nEncodings, const rdr::S32* encodings); |
Pierre Ossman | 4870081 | 2014-09-17 17:11:56 +0200 | [diff] [blame] | 82 | |
Pierre Ossman | 615d16b | 2019-05-03 10:53:06 +0200 | [diff] [blame] | 83 | virtual void clientCutText(const char* str); |
| 84 | |
Pierre Ossman | 0ff2655 | 2016-02-05 10:26:56 +0100 | [diff] [blame] | 85 | virtual void handleClipboardRequest(rdr::U32 flags); |
| 86 | virtual void handleClipboardPeek(rdr::U32 flags); |
| 87 | virtual void handleClipboardNotify(rdr::U32 flags); |
| 88 | virtual void handleClipboardProvide(rdr::U32 flags, |
| 89 | const size_t* lengths, |
| 90 | const rdr::U8* const* data); |
| 91 | |
Pierre Ossman | 5ae2821 | 2017-05-16 14:30:38 +0200 | [diff] [blame] | 92 | virtual void supportsQEMUKeyEvent(); |
Pierre Ossman | 4870081 | 2014-09-17 17:11:56 +0200 | [diff] [blame] | 93 | |
Pierre Ossman | 615d16b | 2019-05-03 10:53:06 +0200 | [diff] [blame] | 94 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 95 | // Methods to be overridden in a derived class |
| 96 | |
| 97 | // versionReceived() indicates that the version number has just been read |
| 98 | // from the client. The version will already have been "cooked" |
| 99 | // to deal with unknown/bogus viewer protocol numbers. |
| 100 | virtual void versionReceived(); |
| 101 | |
| 102 | // authSuccess() is called when authentication has succeeded. |
| 103 | virtual void authSuccess(); |
| 104 | |
Pierre Ossman | 88a94ed | 2019-04-01 14:22:01 +0200 | [diff] [blame] | 105 | // authFailure() is called when authentication has failed. The default |
| 106 | // implementation will inform the client and throw a AuthFailureException. |
| 107 | virtual void authFailure(const char* reason); |
| 108 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 109 | // queryConnection() is called when authentication has succeeded, but |
| 110 | // before informing the client. It can be overridden to query a local user |
| 111 | // to accept the incoming connection, for example. The userName argument |
| 112 | // is the name of the user making the connection, or null (note that the |
| 113 | // storage for userName is owned by the caller). The connection must be |
| 114 | // accepted or rejected by calling approveConnection(), either directly |
| 115 | // from queryConnection() or some time later. |
| 116 | virtual void queryConnection(const char* userName); |
| 117 | |
| 118 | // clientInit() is called when the ClientInit message is received. The |
| 119 | // derived class must call on to SConnection::clientInit(). |
| 120 | virtual void clientInit(bool shared); |
| 121 | |
| 122 | // setPixelFormat() is called when a SetPixelFormat message is received. |
| 123 | // The derived class must call on to SConnection::setPixelFormat(). |
| 124 | virtual void setPixelFormat(const PixelFormat& pf); |
| 125 | |
| 126 | // framebufferUpdateRequest() is called when a FramebufferUpdateRequest |
| 127 | // message is received. The derived class must call on to |
| 128 | // SConnection::framebufferUpdateRequest(). |
| 129 | virtual void framebufferUpdateRequest(const Rect& r, bool incremental); |
| 130 | |
Pierre Ossman | c754cce | 2011-11-14 15:44:11 +0000 | [diff] [blame] | 131 | // fence() is called when we get a fence request or response. By default |
| 132 | // it responds directly to requests (stating it doesn't support any |
| 133 | // synchronisation) and drops responses. Override to implement more proper |
| 134 | // support. |
| 135 | virtual void fence(rdr::U32 flags, unsigned len, const char data[]); |
| 136 | |
Pierre Ossman | c898d9a | 2011-11-14 16:22:23 +0000 | [diff] [blame] | 137 | // enableContinuousUpdates() is called when the client wants to enable |
| 138 | // or disable continuous updates, or change the active area. |
| 139 | virtual void enableContinuousUpdates(bool enable, |
| 140 | int x, int y, int w, int h); |
| 141 | |
Pierre Ossman | 615d16b | 2019-05-03 10:53:06 +0200 | [diff] [blame] | 142 | // handleClipboardRequest() is called whenever the client requests |
| 143 | // the server to send over its clipboard data. It will only be |
| 144 | // called after the server 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 client. Call requestClipboard() to access the |
| 150 | // actual data. |
| 151 | virtual void handleClipboardAnnounce(bool available); |
| 152 | |
| 153 | // handleClipboardData() is called when the client 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 | // client received the request. |
| 158 | virtual void handleClipboardData(const char* data); |
| 159 | |
| 160 | |
Pierre Ossman | 7d64b33 | 2018-10-08 15:59:02 +0200 | [diff] [blame] | 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 client 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 client of changes to the |
| 169 | // clipboard on the server. The client may later request the |
| 170 | // clipboard data via handleClipboardRequest(). |
| 171 | virtual void announceClipboard(bool available); |
| 172 | |
| 173 | // sendClipboardData() transfers the clipboard data to the client |
| 174 | // and should be called whenever the client has requested the |
| 175 | // clipboard via handleClipboardRequest(). |
| 176 | virtual void sendClipboardData(const char* data); |
| 177 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 178 | // setAccessRights() allows a security package to limit the access rights |
Pierre Ossman | 7d64b33 | 2018-10-08 15:59:02 +0200 | [diff] [blame] | 179 | // of a SConnection to the server. How the access rights are treated |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 180 | // is up to the derived class. |
| 181 | |
| 182 | typedef rdr::U16 AccessRights; |
Michal Srb | b318b8f | 2014-11-24 13:18:28 +0200 | [diff] [blame] | 183 | static const AccessRights AccessView; // View display contents |
| 184 | static const AccessRights AccessKeyEvents; // Send key events |
| 185 | static const AccessRights AccessPtrEvents; // Send pointer events |
| 186 | static const AccessRights AccessCutText; // Send/receive clipboard events |
| 187 | static const AccessRights AccessSetDesktopSize; // Change desktop size |
Pierre Ossman | e7be49b | 2014-12-02 14:33:17 +0100 | [diff] [blame] | 188 | static const AccessRights AccessNonShared; // Exclusive access to the server |
Michal Srb | b318b8f | 2014-11-24 13:18:28 +0200 | [diff] [blame] | 189 | static const AccessRights AccessDefault; // The default rights, INCLUDING FUTURE ONES |
| 190 | static const AccessRights AccessNoQuery; // Connect without local user accepting |
| 191 | static const AccessRights AccessFull; // All of the available AND FUTURE rights |
Pierre Ossman | 7d64b33 | 2018-10-08 15:59:02 +0200 | [diff] [blame] | 192 | virtual void setAccessRights(AccessRights ar); |
| 193 | virtual bool accessCheck(AccessRights ar) const; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 194 | |
| 195 | // authenticated() returns true if the client has authenticated |
| 196 | // successfully. |
| 197 | bool authenticated() { return (state_ == RFBSTATE_INITIALISATION || |
| 198 | state_ == RFBSTATE_NORMAL); } |
| 199 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 200 | SMsgReader* reader() { return reader_; } |
| 201 | SMsgWriter* writer() { return writer_; } |
| 202 | |
| 203 | rdr::InStream* getInStream() { return is; } |
| 204 | rdr::OutStream* getOutStream() { return os; } |
| 205 | |
| 206 | enum stateEnum { |
| 207 | RFBSTATE_UNINITIALISED, |
| 208 | RFBSTATE_PROTOCOL_VERSION, |
| 209 | RFBSTATE_SECURITY_TYPE, |
| 210 | RFBSTATE_SECURITY, |
Pierre Ossman | 88a94ed | 2019-04-01 14:22:01 +0200 | [diff] [blame] | 211 | RFBSTATE_SECURITY_FAILURE, |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 212 | RFBSTATE_QUERYING, |
| 213 | RFBSTATE_INITIALISATION, |
| 214 | RFBSTATE_NORMAL, |
| 215 | RFBSTATE_CLOSING, |
| 216 | RFBSTATE_INVALID |
| 217 | }; |
| 218 | |
| 219 | stateEnum state() { return state_; } |
| 220 | |
Pierre Ossman | 4870081 | 2014-09-17 17:11:56 +0200 | [diff] [blame] | 221 | rdr::S32 getPreferredEncoding() { return preferredEncoding; } |
| 222 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 223 | protected: |
Pierre Ossman | 2ebed0d | 2018-10-11 07:54:12 +0200 | [diff] [blame] | 224 | // throwConnFailedException() prints a message to the log, sends a conn |
| 225 | // failed message to the client (if possible) and throws a |
| 226 | // ConnFailedException. |
| 227 | void throwConnFailedException(const char* format, ...) __printf_attr(2, 3); |
| 228 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 229 | void setState(stateEnum s) { state_ = s; } |
Pierre Ossman | b7acf86 | 2015-02-06 14:44:32 +0100 | [diff] [blame] | 230 | |
Pierre Ossman | 0144c53 | 2015-02-04 14:10:43 +0100 | [diff] [blame] | 231 | void setReader(SMsgReader *r) { reader_ = r; } |
| 232 | void setWriter(SMsgWriter *w) { writer_ = w; } |
| 233 | |
Pierre Ossman | b7acf86 | 2015-02-06 14:44:32 +0100 | [diff] [blame] | 234 | private: |
Pierre Ossman | b6b4dc6 | 2014-01-20 15:05:21 +0100 | [diff] [blame] | 235 | void writeFakeColourMap(void); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 236 | |
| 237 | bool readyForSetColourMapEntries; |
| 238 | |
| 239 | void processVersionMsg(); |
| 240 | void processSecurityTypeMsg(); |
Constantin Kaplinsky | 5fa9d22 | 2006-09-06 10:32:06 +0000 | [diff] [blame] | 241 | void processSecurityType(int secType); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 242 | void processSecurityMsg(); |
| 243 | void processInitMsg(); |
| 244 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 245 | int defaultMajorVersion, defaultMinorVersion; |
| 246 | rdr::InStream* is; |
| 247 | rdr::OutStream* os; |
| 248 | SMsgReader* reader_; |
| 249 | SMsgWriter* writer_; |
Michal Srb | dccb5f7 | 2017-03-27 13:55:46 +0300 | [diff] [blame] | 250 | SecurityServer security; |
Adam Tkac | a6578bf | 2010-04-23 14:07:41 +0000 | [diff] [blame] | 251 | SSecurity* ssecurity; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 252 | stateEnum state_; |
Pierre Ossman | 4870081 | 2014-09-17 17:11:56 +0200 | [diff] [blame] | 253 | rdr::S32 preferredEncoding; |
Pierre Ossman | 7d64b33 | 2018-10-08 15:59:02 +0200 | [diff] [blame] | 254 | AccessRights accessRights; |
Pierre Ossman | 615d16b | 2019-05-03 10:53:06 +0200 | [diff] [blame] | 255 | |
| 256 | char* clientClipboard; |
Pierre Ossman | 0ff2655 | 2016-02-05 10:26:56 +0100 | [diff] [blame] | 257 | bool hasLocalClipboard; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 258 | }; |
| 259 | } |
| 260 | #endif |