You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SConnection.h 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. /* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved.
  2. * Copyright 2011-2019 Pierre Ossman for Cendio AB
  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. #ifndef __RFB_SCONNECTION_H__
  24. #define __RFB_SCONNECTION_H__
  25. #include <string>
  26. #include <rdr/InStream.h>
  27. #include <rdr/OutStream.h>
  28. #include <rfb/SMsgHandler.h>
  29. #include <rfb/SecurityServer.h>
  30. #include <rfb/Timer.h>
  31. namespace rfb {
  32. class SMsgReader;
  33. class SMsgWriter;
  34. class SSecurity;
  35. class SConnection : public SMsgHandler {
  36. public:
  37. SConnection();
  38. virtual ~SConnection();
  39. // Methods to initialise the connection
  40. // setStreams() sets the streams to be used for the connection. These must
  41. // be set before initialiseProtocol() and processMsg() are called. The
  42. // SSecurity object may call setStreams() again to provide alternative
  43. // streams over which the RFB protocol is sent (i.e. encrypting/decrypting
  44. // streams). Ownership of the streams remains with the caller
  45. // (i.e. SConnection will not delete them).
  46. void setStreams(rdr::InStream* is, rdr::OutStream* os);
  47. // initialiseProtocol() should be called once the streams and security
  48. // types are set. Subsequently, processMsg() should be called whenever
  49. // there is data to read on the InStream.
  50. void initialiseProtocol();
  51. // processMsg() should be called whenever there is data to read on the
  52. // InStream. You must have called initialiseProtocol() first.
  53. bool processMsg();
  54. // approveConnection() is called to either accept or reject the connection.
  55. // If accept is false, the reason string gives the reason for the
  56. // rejection. It can either be called directly from queryConnection() or
  57. // later, after queryConnection() has returned. It can only be called when
  58. // in state RFBSTATE_QUERYING. On rejection, an AuthFailureException is
  59. // thrown, so this must be handled appropriately by the caller.
  60. void approveConnection(bool accept, const char* reason=0);
  61. // Methods to terminate the connection
  62. // close() shuts down the connection to the client and awaits
  63. // cleanup of the SConnection object by the server
  64. virtual void close(const char* reason);
  65. // Overridden from SMsgHandler
  66. virtual void setEncodings(int nEncodings, const int32_t* encodings);
  67. virtual void clientCutText(const char* str);
  68. virtual void handleClipboardRequest(uint32_t flags);
  69. virtual void handleClipboardPeek();
  70. virtual void handleClipboardNotify(uint32_t flags);
  71. virtual void handleClipboardProvide(uint32_t flags,
  72. const size_t* lengths,
  73. const uint8_t* const* data);
  74. virtual void supportsQEMUKeyEvent();
  75. // Methods to be overridden in a derived class
  76. // versionReceived() indicates that the version number has just been read
  77. // from the client. The version will already have been "cooked"
  78. // to deal with unknown/bogus viewer protocol numbers.
  79. virtual void versionReceived();
  80. // authSuccess() is called when authentication has succeeded.
  81. virtual void authSuccess();
  82. // queryConnection() is called when authentication has succeeded, but
  83. // before informing the client. It can be overridden to query a local user
  84. // to accept the incoming connection, for example. The userName argument
  85. // is the name of the user making the connection, or null (note that the
  86. // storage for userName is owned by the caller). The connection must be
  87. // accepted or rejected by calling approveConnection(), either directly
  88. // from queryConnection() or some time later.
  89. virtual void queryConnection(const char* userName);
  90. // clientInit() is called when the ClientInit message is received. The
  91. // derived class must call on to SConnection::clientInit().
  92. virtual void clientInit(bool shared);
  93. // setPixelFormat() is called when a SetPixelFormat message is received.
  94. // The derived class must call on to SConnection::setPixelFormat().
  95. virtual void setPixelFormat(const PixelFormat& pf);
  96. // framebufferUpdateRequest() is called when a FramebufferUpdateRequest
  97. // message is received. The derived class must call on to
  98. // SConnection::framebufferUpdateRequest().
  99. virtual void framebufferUpdateRequest(const Rect& r, bool incremental);
  100. // fence() is called when we get a fence request or response. By default
  101. // it responds directly to requests (stating it doesn't support any
  102. // synchronisation) and drops responses. Override to implement more proper
  103. // support.
  104. virtual void fence(uint32_t flags, unsigned len, const char data[]);
  105. // enableContinuousUpdates() is called when the client wants to enable
  106. // or disable continuous updates, or change the active area.
  107. virtual void enableContinuousUpdates(bool enable,
  108. int x, int y, int w, int h);
  109. // handleClipboardRequest() is called whenever the client requests
  110. // the server to send over its clipboard data. It will only be
  111. // called after the server has first announced a clipboard change
  112. // via announceClipboard().
  113. virtual void handleClipboardRequest();
  114. // handleClipboardAnnounce() is called to indicate a change in the
  115. // clipboard on the client. Call requestClipboard() to access the
  116. // actual data.
  117. virtual void handleClipboardAnnounce(bool available);
  118. // handleClipboardData() is called when the client has sent over
  119. // the clipboard data as a result of a previous call to
  120. // requestClipboard(). Note that this function might never be
  121. // called if the clipboard data was no longer available when the
  122. // client received the request.
  123. virtual void handleClipboardData(const char* data);
  124. // Other methods
  125. // requestClipboard() will result in a request to the client to
  126. // transfer its clipboard data. A call to handleClipboardData()
  127. // will be made once the data is available.
  128. virtual void requestClipboard();
  129. // announceClipboard() informs the client of changes to the
  130. // clipboard on the server. The client may later request the
  131. // clipboard data via handleClipboardRequest().
  132. virtual void announceClipboard(bool available);
  133. // sendClipboardData() transfers the clipboard data to the client
  134. // and should be called whenever the client has requested the
  135. // clipboard via handleClipboardRequest().
  136. virtual void sendClipboardData(const char* data);
  137. // setAccessRights() allows a security package to limit the access rights
  138. // of a SConnection to the server. How the access rights are treated
  139. // is up to the derived class.
  140. typedef uint16_t AccessRights;
  141. static const AccessRights AccessView; // View display contents
  142. static const AccessRights AccessKeyEvents; // Send key events
  143. static const AccessRights AccessPtrEvents; // Send pointer events
  144. static const AccessRights AccessCutText; // Send/receive clipboard events
  145. static const AccessRights AccessSetDesktopSize; // Change desktop size
  146. static const AccessRights AccessNonShared; // Exclusive access to the server
  147. static const AccessRights AccessDefault; // The default rights, INCLUDING FUTURE ONES
  148. static const AccessRights AccessNoQuery; // Connect without local user accepting
  149. static const AccessRights AccessFull; // All of the available AND FUTURE rights
  150. virtual void setAccessRights(AccessRights ar);
  151. virtual bool accessCheck(AccessRights ar) const;
  152. // authenticated() returns true if the client has authenticated
  153. // successfully.
  154. bool authenticated() { return (state_ == RFBSTATE_INITIALISATION ||
  155. state_ == RFBSTATE_NORMAL); }
  156. SMsgReader* reader() { return reader_; }
  157. SMsgWriter* writer() { return writer_; }
  158. rdr::InStream* getInStream() { return is; }
  159. rdr::OutStream* getOutStream() { return os; }
  160. enum stateEnum {
  161. RFBSTATE_UNINITIALISED,
  162. RFBSTATE_PROTOCOL_VERSION,
  163. RFBSTATE_SECURITY_TYPE,
  164. RFBSTATE_SECURITY,
  165. RFBSTATE_SECURITY_FAILURE,
  166. RFBSTATE_QUERYING,
  167. RFBSTATE_INITIALISATION,
  168. RFBSTATE_NORMAL,
  169. RFBSTATE_CLOSING,
  170. RFBSTATE_INVALID
  171. };
  172. stateEnum state() { return state_; }
  173. int32_t getPreferredEncoding() { return preferredEncoding; }
  174. protected:
  175. // throwConnFailedException() prints a message to the log, sends a conn
  176. // failed message to the client (if possible) and throws a
  177. // ConnFailedException.
  178. void throwConnFailedException(const char* format, ...)
  179. __attribute__((__format__ (__printf__, 2, 3)));
  180. void setState(stateEnum s) { state_ = s; }
  181. void setReader(SMsgReader *r) { reader_ = r; }
  182. void setWriter(SMsgWriter *w) { writer_ = w; }
  183. private:
  184. void cleanup();
  185. void writeFakeColourMap(void);
  186. bool readyForSetColourMapEntries;
  187. bool processVersionMsg();
  188. bool processSecurityTypeMsg();
  189. void processSecurityType(int secType);
  190. bool processSecurityMsg();
  191. bool processSecurityFailure();
  192. bool processInitMsg();
  193. bool handleAuthFailureTimeout(Timer* t);
  194. int defaultMajorVersion, defaultMinorVersion;
  195. rdr::InStream* is;
  196. rdr::OutStream* os;
  197. SMsgReader* reader_;
  198. SMsgWriter* writer_;
  199. SecurityServer security;
  200. SSecurity* ssecurity;
  201. MethodTimer<SConnection> authFailureTimer;
  202. std::string authFailureMsg;
  203. stateEnum state_;
  204. int32_t preferredEncoding;
  205. AccessRights accessRights;
  206. std::string clientClipboard;
  207. bool hasRemoteClipboard;
  208. bool hasLocalClipboard;
  209. bool unsolicitedClipboardAttempt;
  210. };
  211. }
  212. #endif