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

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