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.

CConnection.h 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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. // CConnection - class on the client side representing a connection to a
  21. // server. A derived class should override methods appropriately.
  22. //
  23. #ifndef __RFB_CCONNECTION_H__
  24. #define __RFB_CCONNECTION_H__
  25. #include <rfb/CMsgHandler.h>
  26. #include <rfb/DecodeManager.h>
  27. #include <rfb/SecurityClient.h>
  28. #include <rfb/util.h>
  29. namespace rfb {
  30. class CMsgReader;
  31. class CMsgWriter;
  32. class CSecurity;
  33. class IdentityVerifier;
  34. class CConnection : public CMsgHandler {
  35. public:
  36. CConnection();
  37. virtual ~CConnection();
  38. // Methods to initialise the connection
  39. // setServerName() is used to provide a unique(ish) name for the server to
  40. // which we are connected. This might be the result of getPeerEndpoint on
  41. // a TcpSocket, for example, or a host specified by DNS name & port.
  42. // The serverName is used when verifying the Identity of a host (see RA2).
  43. void setServerName(const char* name_) { serverName.replaceBuf(strDup(name_)); }
  44. // setStreams() sets the streams to be used for the connection. These must
  45. // be set before initialiseProtocol() and processMsg() are called. The
  46. // CSecurity object may call setStreams() again to provide alternative
  47. // streams over which the RFB protocol is sent (i.e. encrypting/decrypting
  48. // streams). Ownership of the streams remains with the caller
  49. // (i.e. SConnection will not delete them).
  50. void setStreams(rdr::InStream* is, rdr::OutStream* os);
  51. // setShared sets the value of the shared flag which will be sent to the
  52. // server upon initialisation.
  53. void setShared(bool s) { shared = s; }
  54. // setFramebuffer configures the PixelBuffer that the CConnection
  55. // should render all pixel data in to. Note that the CConnection
  56. // takes ownership of the PixelBuffer and it must not be deleted by
  57. // anyone else. Call setFramebuffer again with NULL or a different
  58. // PixelBuffer to delete the previous one.
  59. void setFramebuffer(ModifiablePixelBuffer* fb);
  60. // initialiseProtocol() should be called once the streams and security
  61. // types are set. Subsequently, processMsg() should be called whenever
  62. // there is data to read on the InStream.
  63. void initialiseProtocol();
  64. // processMsg() should be called whenever there is either:
  65. // - data available on the underlying network stream
  66. // In this case, processMsg may return without processing an RFB message,
  67. // if the available data does not result in an RFB message being ready
  68. // to handle. e.g. if data is encrypted.
  69. // NB: This makes it safe to call processMsg() in response to select()
  70. // - data available on the CConnection's current InStream
  71. // In this case, processMsg should always process the available RFB
  72. // message before returning.
  73. // NB: In either case, you must have called initialiseProtocol() first.
  74. bool processMsg();
  75. // close() gracefully shuts down the connection to the server and
  76. // should be called before terminating the underlying network
  77. // connection
  78. void close();
  79. // Methods overridden from CMsgHandler
  80. // Note: These must be called by any deriving classes
  81. virtual void setDesktopSize(int w, int h);
  82. virtual void setExtendedDesktopSize(unsigned reason, unsigned result,
  83. int w, int h,
  84. const ScreenSet& layout);
  85. virtual void endOfContinuousUpdates();
  86. virtual void serverInit(int width, int height,
  87. const PixelFormat& pf,
  88. const char* name);
  89. virtual bool readAndDecodeRect(const Rect& r, int encoding,
  90. ModifiablePixelBuffer* pb);
  91. virtual void framebufferUpdateStart();
  92. virtual void framebufferUpdateEnd();
  93. virtual bool dataRect(const Rect& r, int encoding);
  94. virtual void serverCutText(const char* str);
  95. virtual void handleClipboardCaps(rdr::U32 flags,
  96. const rdr::U32* lengths);
  97. virtual void handleClipboardRequest(rdr::U32 flags);
  98. virtual void handleClipboardPeek(rdr::U32 flags);
  99. virtual void handleClipboardNotify(rdr::U32 flags);
  100. virtual void handleClipboardProvide(rdr::U32 flags,
  101. const size_t* lengths,
  102. const rdr::U8* const* data);
  103. // Methods to be overridden in a derived class
  104. // authSuccess() is called when authentication has succeeded.
  105. virtual void authSuccess();
  106. // initDone() is called when the connection is fully established
  107. // and standard messages can be sent. This is called before the
  108. // initial FramebufferUpdateRequest giving a derived class the
  109. // chance to modify pixel format and settings. The derived class
  110. // must also make sure it has provided a valid framebuffer before
  111. // returning.
  112. virtual void initDone() = 0;
  113. // resizeFramebuffer() is called whenever the framebuffer
  114. // dimensions or the screen layout changes. A subclass must make
  115. // sure the pixel buffer has been updated once this call returns.
  116. virtual void resizeFramebuffer();
  117. // handleClipboardRequest() is called whenever the server requests
  118. // the client to send over its clipboard data. It will only be
  119. // called after the client has first announced a clipboard change
  120. // via announceClipboard().
  121. virtual void handleClipboardRequest();
  122. // handleClipboardAnnounce() is called to indicate a change in the
  123. // clipboard on the server. Call requestClipboard() to access the
  124. // actual data.
  125. virtual void handleClipboardAnnounce(bool available);
  126. // handleClipboardData() is called when the server has sent over
  127. // the clipboard data as a result of a previous call to
  128. // requestClipboard(). Note that this function might never be
  129. // called if the clipboard data was no longer available when the
  130. // server received the request.
  131. virtual void handleClipboardData(const char* data);
  132. // Other methods
  133. // requestClipboard() will result in a request to the server to
  134. // transfer its clipboard data. A call to handleClipboardData()
  135. // will be made once the data is available.
  136. virtual void requestClipboard();
  137. // announceClipboard() informs the server of changes to the
  138. // clipboard on the client. The server may later request the
  139. // clipboard data via handleClipboardRequest().
  140. virtual void announceClipboard(bool available);
  141. // sendClipboardData() transfers the clipboard data to the server
  142. // and should be called whenever the server has requested the
  143. // clipboard via handleClipboardRequest().
  144. virtual void sendClipboardData(const char* data);
  145. // refreshFramebuffer() forces a complete refresh of the entire
  146. // framebuffer
  147. void refreshFramebuffer();
  148. // setPreferredEncoding()/getPreferredEncoding() adjusts which
  149. // encoding is listed first as a hint to the server that it is the
  150. // preferred one
  151. void setPreferredEncoding(int encoding);
  152. int getPreferredEncoding();
  153. // setCompressLevel()/setQualityLevel() controls the encoding hints
  154. // sent to the server
  155. void setCompressLevel(int level);
  156. void setQualityLevel(int level);
  157. // setPF() controls the pixel format requested from the server.
  158. // server.pf() will automatically be adjusted once the new format
  159. // is active.
  160. void setPF(const PixelFormat& pf);
  161. CMsgReader* reader() { return reader_; }
  162. CMsgWriter* writer() { return writer_; }
  163. rdr::InStream* getInStream() { return is; }
  164. rdr::OutStream* getOutStream() { return os; }
  165. // Access method used by SSecurity implementations that can verify servers'
  166. // Identities, to determine the unique(ish) name of the server.
  167. const char* getServerName() const { return serverName.buf; }
  168. bool isSecure() const { return csecurity ? csecurity->isSecure() : false; }
  169. enum stateEnum {
  170. RFBSTATE_UNINITIALISED,
  171. RFBSTATE_PROTOCOL_VERSION,
  172. RFBSTATE_SECURITY_TYPES,
  173. RFBSTATE_SECURITY,
  174. RFBSTATE_SECURITY_RESULT,
  175. RFBSTATE_SECURITY_REASON,
  176. RFBSTATE_INITIALISATION,
  177. RFBSTATE_NORMAL,
  178. RFBSTATE_CLOSING,
  179. RFBSTATE_INVALID
  180. };
  181. stateEnum state() { return state_; }
  182. CSecurity *csecurity;
  183. SecurityClient security;
  184. protected:
  185. void setState(stateEnum s) { state_ = s; }
  186. void setReader(CMsgReader *r) { reader_ = r; }
  187. void setWriter(CMsgWriter *w) { writer_ = w; }
  188. ModifiablePixelBuffer* getFramebuffer() { return framebuffer; }
  189. protected:
  190. // Optional capabilities that a subclass is expected to set to true
  191. // if supported
  192. bool supportsLocalCursor;
  193. bool supportsDesktopResize;
  194. bool supportsLEDState;
  195. private:
  196. // This is a default implementation of fences that automatically
  197. // responds to requests, stating no support for synchronisation.
  198. // When overriding, call CMsgHandler::fence() directly in order to
  199. // state correct support for fence flags.
  200. virtual void fence(rdr::U32 flags, unsigned len, const char data[]);
  201. private:
  202. bool processVersionMsg();
  203. bool processSecurityTypesMsg();
  204. bool processSecurityMsg();
  205. bool processSecurityResultMsg();
  206. bool processSecurityReasonMsg();
  207. bool processInitMsg();
  208. void throwAuthFailureException();
  209. void securityCompleted();
  210. void requestNewUpdate();
  211. void updateEncodings();
  212. rdr::InStream* is;
  213. rdr::OutStream* os;
  214. CMsgReader* reader_;
  215. CMsgWriter* writer_;
  216. bool deleteStreamsWhenDone;
  217. bool shared;
  218. stateEnum state_;
  219. CharArray serverName;
  220. bool pendingPFChange;
  221. rfb::PixelFormat pendingPF;
  222. int preferredEncoding;
  223. int compressLevel;
  224. int qualityLevel;
  225. bool formatChange;
  226. rfb::PixelFormat nextPF;
  227. bool encodingChange;
  228. bool firstUpdate;
  229. bool pendingUpdate;
  230. bool continuousUpdates;
  231. bool forceNonincremental;
  232. ModifiablePixelBuffer* framebuffer;
  233. DecodeManager decoder;
  234. char* serverClipboard;
  235. bool hasLocalClipboard;
  236. };
  237. }
  238. #endif