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.

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