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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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. namespace rfb {
  30. class SMsgReader;
  31. class SMsgWriter;
  32. class SSecurity;
  33. class SConnection : public SMsgHandler {
  34. public:
  35. SConnection();
  36. virtual ~SConnection();
  37. // Methods to initialise the connection
  38. // setStreams() sets the streams to be used for the connection. These must
  39. // be set before initialiseProtocol() and processMsg() are called. The
  40. // SSecurity object may call setStreams() again to provide alternative
  41. // streams over which the RFB protocol is sent (i.e. encrypting/decrypting
  42. // streams). Ownership of the streams remains with the caller
  43. // (i.e. SConnection will not delete them).
  44. void setStreams(rdr::InStream* is, rdr::OutStream* os);
  45. // initialiseProtocol() should be called once the streams and security
  46. // types are set. Subsequently, processMsg() should be called whenever
  47. // there is data to read on the InStream.
  48. void initialiseProtocol();
  49. // processMsg() should be called whenever there is data to read on the
  50. // InStream. You must have called initialiseProtocol() first.
  51. void processMsg();
  52. // approveConnection() is called to either accept or reject the connection.
  53. // If accept is false, the reason string gives the reason for the
  54. // rejection. It can either be called directly from queryConnection() or
  55. // later, after queryConnection() has returned. It can only be called when
  56. // in state RFBSTATE_QUERYING. On rejection, an AuthFailureException is
  57. // thrown, so this must be handled appropriately by the caller.
  58. void approveConnection(bool accept, const char* reason=0);
  59. // Methods to terminate the connection
  60. // close() shuts down the connection to the client and awaits
  61. // cleanup of the SConnection object by the server
  62. virtual void close(const char* reason);
  63. // Overridden from SMsgHandler
  64. virtual void setEncodings(int nEncodings, const rdr::S32* encodings);
  65. virtual void clientCutText(const char* str);
  66. virtual void handleClipboardRequest(rdr::U32 flags);
  67. virtual void handleClipboardPeek(rdr::U32 flags);
  68. virtual void handleClipboardNotify(rdr::U32 flags);
  69. virtual void handleClipboardProvide(rdr::U32 flags,
  70. const size_t* lengths,
  71. const rdr::U8* const* data);
  72. virtual void supportsQEMUKeyEvent();
  73. // Methods to be overridden in a derived class
  74. // versionReceived() indicates that the version number has just been read
  75. // from the client. The version will already have been "cooked"
  76. // to deal with unknown/bogus viewer protocol numbers.
  77. virtual void versionReceived();
  78. // authSuccess() is called when authentication has succeeded.
  79. virtual void authSuccess();
  80. // authFailure() is called when authentication has failed. The default
  81. // implementation will inform the client and throw a AuthFailureException.
  82. virtual void authFailure(const char* reason);
  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(rdr::U32 flags, unsigned len, const char 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. // setAccessRights() allows a security package to limit the access rights
  139. // of a SConnection to the server. How the access rights are treated
  140. // is up to the derived class.
  141. typedef rdr::U16 AccessRights;
  142. static const AccessRights AccessView; // View display contents
  143. static const AccessRights AccessKeyEvents; // Send key events
  144. static const AccessRights AccessPtrEvents; // Send pointer events
  145. static const AccessRights AccessCutText; // Send/receive clipboard events
  146. static const AccessRights AccessSetDesktopSize; // Change desktop size
  147. static const AccessRights AccessNonShared; // Exclusive access to the server
  148. static const AccessRights AccessDefault; // The default rights, INCLUDING FUTURE ONES
  149. static const AccessRights AccessNoQuery; // Connect without local user accepting
  150. static const AccessRights AccessFull; // All of the available AND FUTURE rights
  151. virtual void setAccessRights(AccessRights ar);
  152. virtual bool accessCheck(AccessRights ar) const;
  153. // authenticated() returns true if the client has authenticated
  154. // successfully.
  155. bool authenticated() { return (state_ == RFBSTATE_INITIALISATION ||
  156. state_ == RFBSTATE_NORMAL); }
  157. SMsgReader* reader() { return reader_; }
  158. SMsgWriter* writer() { return writer_; }
  159. rdr::InStream* getInStream() { return is; }
  160. rdr::OutStream* getOutStream() { return os; }
  161. enum stateEnum {
  162. RFBSTATE_UNINITIALISED,
  163. RFBSTATE_PROTOCOL_VERSION,
  164. RFBSTATE_SECURITY_TYPE,
  165. RFBSTATE_SECURITY,
  166. RFBSTATE_SECURITY_FAILURE,
  167. RFBSTATE_QUERYING,
  168. RFBSTATE_INITIALISATION,
  169. RFBSTATE_NORMAL,
  170. RFBSTATE_CLOSING,
  171. RFBSTATE_INVALID
  172. };
  173. stateEnum state() { return state_; }
  174. rdr::S32 getPreferredEncoding() { return preferredEncoding; }
  175. protected:
  176. // throwConnFailedException() prints a message to the log, sends a conn
  177. // failed message to the client (if possible) and throws a
  178. // ConnFailedException.
  179. void throwConnFailedException(const char* format, ...) __printf_attr(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 writeFakeColourMap(void);
  185. bool readyForSetColourMapEntries;
  186. void processVersionMsg();
  187. void processSecurityTypeMsg();
  188. void processSecurityType(int secType);
  189. void processSecurityMsg();
  190. void processInitMsg();
  191. int defaultMajorVersion, defaultMinorVersion;
  192. rdr::InStream* is;
  193. rdr::OutStream* os;
  194. SMsgReader* reader_;
  195. SMsgWriter* writer_;
  196. SecurityServer security;
  197. SSecurity* ssecurity;
  198. stateEnum state_;
  199. rdr::S32 preferredEncoding;
  200. AccessRights accessRights;
  201. char* clientClipboard;
  202. bool hasLocalClipboard;
  203. };
  204. }
  205. #endif