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 6.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved.
  2. * Copyright 2011-2017 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. // setProtocol3_3 configures whether or not the CConnection should
  55. // only ever support protocol version 3.3
  56. void setProtocol3_3(bool s) {useProtocol3_3 = s;}
  57. // setFramebuffer configures the PixelBuffer that the CConnection
  58. // should render all pixel data in to. Note that the CConnection
  59. // takes ownership of the PixelBuffer and it must not be deleted by
  60. // anyone else. Call setFramebuffer again with NULL or a different
  61. // PixelBuffer to delete the previous one.
  62. void setFramebuffer(ModifiablePixelBuffer* fb);
  63. // initialiseProtocol() should be called once the streams and security
  64. // types are set. Subsequently, processMsg() should be called whenever
  65. // there is data to read on the InStream.
  66. void initialiseProtocol();
  67. // processMsg() should be called whenever there is either:
  68. // - data available on the underlying network stream
  69. // In this case, processMsg may return without processing an RFB message,
  70. // if the available data does not result in an RFB message being ready
  71. // to handle. e.g. if data is encrypted.
  72. // NB: This makes it safe to call processMsg() in response to select()
  73. // - data available on the CConnection's current InStream
  74. // In this case, processMsg should always process the available RFB
  75. // message before returning.
  76. // NB: In either case, you must have called initialiseProtocol() first.
  77. void processMsg();
  78. // Methods overridden from CMsgHandler
  79. // Note: These must be called by any deriving classes
  80. virtual void setDesktopSize(int w, int h);
  81. virtual void setExtendedDesktopSize(unsigned reason, unsigned result,
  82. int w, int h,
  83. const ScreenSet& layout);
  84. virtual void readAndDecodeRect(const Rect& r, int encoding,
  85. ModifiablePixelBuffer* pb);
  86. virtual void framebufferUpdateStart();
  87. virtual void framebufferUpdateEnd();
  88. virtual void dataRect(const Rect& r, int encoding);
  89. // Methods to be overridden in a derived class
  90. // getIdVerifier() returns the identity verifier associated with the connection.
  91. // Ownership of the IdentityVerifier is retained by the CConnection instance.
  92. virtual IdentityVerifier* getIdentityVerifier() {return 0;}
  93. // authSuccess() is called when authentication has succeeded.
  94. virtual void authSuccess();
  95. // serverInit() is called when the ServerInit message is received. The
  96. // derived class must call on to CConnection::serverInit().
  97. virtual void serverInit();
  98. // Other methods
  99. CMsgReader* reader() { return reader_; }
  100. CMsgWriter* writer() { return writer_; }
  101. rdr::InStream* getInStream() { return is; }
  102. rdr::OutStream* getOutStream() { return os; }
  103. // Access method used by SSecurity implementations that can verify servers'
  104. // Identities, to determine the unique(ish) name of the server.
  105. const char* getServerName() const { return serverName.buf; }
  106. bool isSecure() const { return csecurity ? csecurity->isSecure() : false; }
  107. enum stateEnum {
  108. RFBSTATE_UNINITIALISED,
  109. RFBSTATE_PROTOCOL_VERSION,
  110. RFBSTATE_SECURITY_TYPES,
  111. RFBSTATE_SECURITY,
  112. RFBSTATE_SECURITY_RESULT,
  113. RFBSTATE_INITIALISATION,
  114. RFBSTATE_NORMAL,
  115. RFBSTATE_INVALID
  116. };
  117. stateEnum state() { return state_; }
  118. CSecurity *csecurity;
  119. SecurityClient security;
  120. protected:
  121. void setState(stateEnum s) { state_ = s; }
  122. void setReader(CMsgReader *r) { reader_ = r; }
  123. void setWriter(CMsgWriter *w) { writer_ = w; }
  124. ModifiablePixelBuffer* getFramebuffer() { return framebuffer; }
  125. private:
  126. // This is a default implementation of fences that automatically
  127. // responds to requests, stating no support for synchronisation.
  128. // When overriding, call CMsgHandler::fence() directly in order to
  129. // state correct support for fence flags.
  130. virtual void fence(rdr::U32 flags, unsigned len, const char data[]);
  131. private:
  132. void processVersionMsg();
  133. void processSecurityTypesMsg();
  134. void processSecurityMsg();
  135. void processSecurityResultMsg();
  136. void processInitMsg();
  137. void throwAuthFailureException();
  138. void throwConnFailedException();
  139. void securityCompleted();
  140. rdr::InStream* is;
  141. rdr::OutStream* os;
  142. CMsgReader* reader_;
  143. CMsgWriter* writer_;
  144. bool deleteStreamsWhenDone;
  145. bool shared;
  146. stateEnum state_;
  147. CharArray serverName;
  148. bool useProtocol3_3;
  149. ModifiablePixelBuffer* framebuffer;
  150. DecodeManager decoder;
  151. };
  152. }
  153. #endif