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

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