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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /* Copyright (C) 2002-2005 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/CSecurity.h>
  28. #include <rfb/util.h>
  29. #include <rfb/SecurityClient.h>
  30. namespace rfb {
  31. class CMsgReader;
  32. class CMsgWriter;
  33. class CSecurity;
  34. class IdentityVerifier;
  35. class CConnection : public CMsgHandler {
  36. public:
  37. CConnection();
  38. virtual ~CConnection();
  39. // Methods to initialise the connection
  40. // setServerName() is used to provide a unique(ish) name for the server to
  41. // which we are connected. This might be the result of getPeerEndpoint on
  42. // a TcpSocket, for example, or a host specified by DNS name & port.
  43. // The serverName is used when verifying the Identity of a host (see RA2).
  44. void setServerName(const char* name_) { serverName.replaceBuf(strDup(name_)); }
  45. // setStreams() sets the streams to be used for the connection. These must
  46. // be set before initialiseProtocol() and processMsg() are called. The
  47. // CSecurity object may call setStreams() again to provide alternative
  48. // streams over which the RFB protocol is sent (i.e. encrypting/decrypting
  49. // streams). Ownership of the streams remains with the caller
  50. // (i.e. SConnection will not delete them).
  51. void setStreams(rdr::InStream* is, rdr::OutStream* os);
  52. // setShared sets the value of the shared flag which will be sent to the
  53. // server upon initialisation.
  54. void setShared(bool s) { shared = s; }
  55. // setProtocol3_3 configures whether or not the CConnection should
  56. // only ever support protocol version 3.3
  57. void setProtocol3_3(bool s) {useProtocol3_3 = s;}
  58. // initialiseProtocol() should be called once the streams and security
  59. // types are set. Subsequently, processMsg() should be called whenever
  60. // there is data to read on the InStream.
  61. void initialiseProtocol();
  62. // processMsg() should be called whenever there is either:
  63. // - data available on the underlying network stream
  64. // In this case, processMsg may return without processing an RFB message,
  65. // if the available data does not result in an RFB message being ready
  66. // to handle. e.g. if data is encrypted.
  67. // NB: This makes it safe to call processMsg() in response to select()
  68. // - data available on the CConnection's current InStream
  69. // In this case, processMsg should always process the available RFB
  70. // message before returning.
  71. // NB: In either case, you must have called initialiseProtocol() first.
  72. void processMsg();
  73. // Methods to be overridden in a derived class
  74. // getIdVerifier() returns the identity verifier associated with the connection.
  75. // Ownership of the IdentityVerifier is retained by the CConnection instance.
  76. virtual IdentityVerifier* getIdentityVerifier() {return 0;}
  77. // authSuccess() is called when authentication has succeeded.
  78. virtual void authSuccess();
  79. // serverInit() is called when the ServerInit message is received. The
  80. // derived class must call on to CConnection::serverInit().
  81. virtual void serverInit();
  82. // Other methods
  83. CMsgReader* reader() { return reader_; }
  84. CMsgWriter* writer() { return writer_; }
  85. rdr::InStream* getInStream() { return is; }
  86. rdr::OutStream* getOutStream() { return os; }
  87. // Access method used by SSecurity implementations that can verify servers'
  88. // Identities, to determine the unique(ish) name of the server.
  89. const char* getServerName() const { return serverName.buf; }
  90. enum stateEnum {
  91. RFBSTATE_UNINITIALISED,
  92. RFBSTATE_PROTOCOL_VERSION,
  93. RFBSTATE_SECURITY_TYPES,
  94. RFBSTATE_SECURITY,
  95. RFBSTATE_SECURITY_RESULT,
  96. RFBSTATE_INITIALISATION,
  97. RFBSTATE_NORMAL,
  98. RFBSTATE_INVALID
  99. };
  100. stateEnum state() { return state_; }
  101. CSecurity *csecurity;
  102. SecurityClient *security;
  103. protected:
  104. void setState(stateEnum s) { state_ = s; }
  105. void setReader(CMsgReader *r) { reader_ = r; }
  106. void setWriter(CMsgWriter *w) { writer_ = w; }
  107. private:
  108. // This is a default implementation of fences that automatically
  109. // responds to requests, stating no support for synchronisation.
  110. // When overriding, call CMsgHandler::fence() directly in order to
  111. // state correct support for fence flags.
  112. virtual void fence(rdr::U32 flags, unsigned len, const char data[]);
  113. private:
  114. void processVersionMsg();
  115. void processSecurityTypesMsg();
  116. void processSecurityMsg();
  117. void processSecurityResultMsg();
  118. void processInitMsg();
  119. void throwAuthFailureException();
  120. void throwConnFailedException();
  121. void securityCompleted();
  122. rdr::InStream* is;
  123. rdr::OutStream* os;
  124. CMsgReader* reader_;
  125. CMsgWriter* writer_;
  126. bool deleteStreamsWhenDone;
  127. bool shared;
  128. stateEnum state_;
  129. CharArray serverName;
  130. bool useProtocol3_3;
  131. };
  132. }
  133. #endif