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.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /* Copyright (C) 2002-2004 RealVNC Ltd. All Rights Reserved.
  2. *
  3. * This is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation; either version 2 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This software is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this software; if not, write to the Free Software
  15. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
  16. * USA.
  17. */
  18. //
  19. // CConnection - class on the client side representing a connection to a
  20. // server. A derived class should override methods appropriately.
  21. //
  22. #ifndef __RFB_CCONNECTION_H__
  23. #define __RFB_CCONNECTION_H__
  24. #include <rdr/InStream.h>
  25. #include <rdr/OutStream.h>
  26. #include <rfb/CMsgHandler.h>
  27. #include <rfb/util.h>
  28. namespace rfb {
  29. class CMsgReader;
  30. class CMsgWriter;
  31. class CSecurity;
  32. class IdentityVerifier;
  33. class CConnection : public CMsgHandler {
  34. public:
  35. CConnection();
  36. virtual ~CConnection();
  37. // ***
  38. void setServerName(const char* serverName_);
  39. // Methods to initialise the connection
  40. // setStreams() sets the streams to be used for the connection. These must
  41. // be set before initialiseProtocol() and processMsg() are called. The
  42. // CSecurity object may call setStreams() again to provide alternative
  43. // streams over which the RFB protocol is sent (i.e. encrypting/decrypting
  44. // streams). Ownership of the streams remains with the caller
  45. // (i.e. SConnection will not delete them).
  46. void setStreams(rdr::InStream* is, rdr::OutStream* os);
  47. // addSecType() should be called once for each security type which the
  48. // client supports. The order in which they're added is such that the
  49. // first one is most preferred.
  50. void addSecType(rdr::U8 secType);
  51. // setClientSecTypeOrder() determines whether the client should obey
  52. // the server's security type preference, by picking the first server security
  53. // type that the client supports, or whether it should pick the first type
  54. // that the server supports, from the client-supported list of types.
  55. void setClientSecTypeOrder(bool clientOrder);
  56. // setShared sets the value of the shared flag which will be sent to the
  57. // server upon initialisation.
  58. void setShared(bool s) { shared = s; }
  59. // setProtocol3_3 configures whether or not the CConnection should
  60. // only ever support protocol version 3.3
  61. void setProtocol3_3(bool s) {useProtocol3_3 = s;}
  62. // initialiseProtocol() should be called once the streams and security
  63. // types are set. Subsequently, processMsg() should be called whenever
  64. // there is data to read on the InStream.
  65. void initialiseProtocol();
  66. // processMsg() should be called whenever there is either:
  67. // - data available on the underlying network stream
  68. // In this case, processMsg may return without processing an RFB message,
  69. // if the available data does not result in an RFB message being ready
  70. // to handle. e.g. if data is encrypted.
  71. // NB: This makes it safe to call processMsg() in response to select()
  72. // - data available on the CConnection's current InStream
  73. // In this case, processMsg should always process the available RFB
  74. // message before returning.
  75. // NB: In either case, you must have called initialiseProtocol() first.
  76. void processMsg();
  77. // Methods to be overridden in a derived class
  78. // getCSecurity() gets the CSecurity object for the given type. The type
  79. // is guaranteed to be one of the secTypes passed in to addSecType(). The
  80. // CSecurity object's destroy() method will be called by the CConnection
  81. // from its destructor.
  82. virtual CSecurity* getCSecurity(int secType)=0;
  83. // getCurrentCSecurity() gets the CSecurity instance used for this connection.
  84. const CSecurity* getCurrentCSecurity() const {return security;}
  85. // getIdVerifier() returns the identity verifier associated with the connection.
  86. // Ownership of the IdentityVerifier is retained by the CConnection instance.
  87. virtual IdentityVerifier* getIdentityVerifier() {return 0;}
  88. // authSuccess() is called when authentication has succeeded.
  89. virtual void authSuccess();
  90. // serverInit() is called when the ServerInit message is received. The
  91. // derived class must call on to CConnection::serverInit().
  92. virtual void serverInit();
  93. // Other methods
  94. // deleteReaderAndWriter() deletes the reader and writer associated with
  95. // this connection. This may be useful if you want to delete the streams
  96. // before deleting the SConnection to make sure that no attempt by the
  97. // SConnection is made to read or write.
  98. // XXX Do we really need this at all???
  99. void deleteReaderAndWriter();
  100. CMsgReader* reader() { return reader_; }
  101. CMsgWriter* writer() { return writer_; }
  102. rdr::InStream* getInStream() { return is; }
  103. rdr::OutStream* getOutStream() { return os; }
  104. char* getServerName() {return strDup(serverName.buf);}
  105. enum stateEnum {
  106. RFBSTATE_UNINITIALISED,
  107. RFBSTATE_PROTOCOL_VERSION,
  108. RFBSTATE_SECURITY_TYPES,
  109. RFBSTATE_SECURITY,
  110. RFBSTATE_SECURITY_RESULT,
  111. RFBSTATE_INITIALISATION,
  112. RFBSTATE_NORMAL,
  113. RFBSTATE_INVALID
  114. };
  115. stateEnum state() { return state_; }
  116. protected:
  117. void setState(stateEnum s) { state_ = s; }
  118. private:
  119. void processVersionMsg();
  120. void processSecurityTypesMsg();
  121. void processSecurityMsg();
  122. void processSecurityResultMsg();
  123. void processInitMsg();
  124. void throwAuthFailureException();
  125. void throwConnFailedException();
  126. void securityCompleted();
  127. rdr::InStream* is;
  128. rdr::OutStream* os;
  129. CMsgReader* reader_;
  130. CMsgWriter* writer_;
  131. bool deleteStreamsWhenDone;
  132. bool shared;
  133. CSecurity* security;
  134. enum { maxSecTypes = 8 };
  135. int nSecTypes;
  136. rdr::U8 secTypes[maxSecTypes];
  137. bool clientSecTypeOrder;
  138. stateEnum state_;
  139. CharArray serverName;
  140. bool useProtocol3_3;
  141. };
  142. }
  143. #endif