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.

SConnection.h 8.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved.
  2. * Copyright 2011 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. // SConnection - class on the server side representing a connection to a
  21. // client. A derived class should override methods appropriately.
  22. //
  23. #ifndef __RFB_SCONNECTION_H__
  24. #define __RFB_SCONNECTION_H__
  25. #include <rdr/InStream.h>
  26. #include <rdr/OutStream.h>
  27. #include <rfb/SMsgHandler.h>
  28. #include <rfb/SecurityServer.h>
  29. namespace rfb {
  30. class SMsgReader;
  31. class SMsgWriter;
  32. class SSecurity;
  33. class SConnection : public SMsgHandler {
  34. public:
  35. SConnection();
  36. virtual ~SConnection();
  37. // Methods to initialise the connection
  38. // setStreams() sets the streams to be used for the connection. These must
  39. // be set before initialiseProtocol() and processMsg() are called. The
  40. // SSecurity object may call setStreams() again to provide alternative
  41. // streams over which the RFB protocol is sent (i.e. encrypting/decrypting
  42. // streams). Ownership of the streams remains with the caller
  43. // (i.e. SConnection will not delete them).
  44. void setStreams(rdr::InStream* is, rdr::OutStream* os);
  45. // initialiseProtocol() should be called once the streams and security
  46. // types are set. Subsequently, processMsg() should be called whenever
  47. // there is data to read on the InStream.
  48. void initialiseProtocol();
  49. // processMsg() should be called whenever there is data to read on the
  50. // InStream. You must have called initialiseProtocol() first.
  51. void processMsg();
  52. // approveConnection() is called to either accept or reject the connection.
  53. // If accept is false, the reason string gives the reason for the
  54. // rejection. It can either be called directly from queryConnection() or
  55. // later, after queryConnection() has returned. It can only be called when
  56. // in state RFBSTATE_QUERYING. On rejection, an AuthFailureException is
  57. // thrown, so this must be handled appropriately by the caller.
  58. void approveConnection(bool accept, const char* reason=0);
  59. // Overridden from SMsgHandler
  60. virtual void setEncodings(int nEncodings, const rdr::S32* encodings);
  61. // Methods to be overridden in a derived class
  62. // versionReceived() indicates that the version number has just been read
  63. // from the client. The version will already have been "cooked"
  64. // to deal with unknown/bogus viewer protocol numbers.
  65. virtual void versionReceived();
  66. // authSuccess() is called when authentication has succeeded.
  67. virtual void authSuccess();
  68. // queryConnection() is called when authentication has succeeded, but
  69. // before informing the client. It can be overridden to query a local user
  70. // to accept the incoming connection, for example. The userName argument
  71. // is the name of the user making the connection, or null (note that the
  72. // storage for userName is owned by the caller). The connection must be
  73. // accepted or rejected by calling approveConnection(), either directly
  74. // from queryConnection() or some time later.
  75. virtual void queryConnection(const char* userName);
  76. // clientInit() is called when the ClientInit message is received. The
  77. // derived class must call on to SConnection::clientInit().
  78. virtual void clientInit(bool shared);
  79. // setPixelFormat() is called when a SetPixelFormat message is received.
  80. // The derived class must call on to SConnection::setPixelFormat().
  81. virtual void setPixelFormat(const PixelFormat& pf);
  82. // framebufferUpdateRequest() is called when a FramebufferUpdateRequest
  83. // message is received. The derived class must call on to
  84. // SConnection::framebufferUpdateRequest().
  85. virtual void framebufferUpdateRequest(const Rect& r, bool incremental);
  86. // fence() is called when we get a fence request or response. By default
  87. // it responds directly to requests (stating it doesn't support any
  88. // synchronisation) and drops responses. Override to implement more proper
  89. // support.
  90. virtual void fence(rdr::U32 flags, unsigned len, const char data[]);
  91. // enableContinuousUpdates() is called when the client wants to enable
  92. // or disable continuous updates, or change the active area.
  93. virtual void enableContinuousUpdates(bool enable,
  94. int x, int y, int w, int h);
  95. // setAccessRights() allows a security package to limit the access rights
  96. // of a VNCSConnectionST to the server. How the access rights are treated
  97. // is up to the derived class.
  98. typedef rdr::U16 AccessRights;
  99. static const AccessRights AccessView; // View display contents
  100. static const AccessRights AccessKeyEvents; // Send key events
  101. static const AccessRights AccessPtrEvents; // Send pointer events
  102. static const AccessRights AccessCutText; // Send/receive clipboard events
  103. static const AccessRights AccessSetDesktopSize; // Change desktop size
  104. static const AccessRights AccessNonShared; // Exclusive access to the server
  105. static const AccessRights AccessDefault; // The default rights, INCLUDING FUTURE ONES
  106. static const AccessRights AccessNoQuery; // Connect without local user accepting
  107. static const AccessRights AccessFull; // All of the available AND FUTURE rights
  108. virtual void setAccessRights(AccessRights ar) = 0;
  109. // Other methods
  110. // authenticated() returns true if the client has authenticated
  111. // successfully.
  112. bool authenticated() { return (state_ == RFBSTATE_INITIALISATION ||
  113. state_ == RFBSTATE_NORMAL); }
  114. // deleteReaderAndWriter() deletes the reader and writer associated with
  115. // this connection. This may be useful if you want to delete the streams
  116. // before deleting the SConnection to make sure that no attempt by the
  117. // SConnection is made to read or write.
  118. // XXX Do we really need this at all???
  119. void deleteReaderAndWriter();
  120. // throwConnFailedException() prints a message to the log, sends a conn
  121. // failed message to the client (if possible) and throws a
  122. // ConnFailedException.
  123. void throwConnFailedException(const char* msg);
  124. // writeConnFailedFromScratch() sends a conn failed message to an OutStream
  125. // without the need to negotiate the protocol version first. It actually
  126. // does this by assuming that the client will understand version 3.3 of the
  127. // protocol.
  128. static void writeConnFailedFromScratch(const char* msg,
  129. rdr::OutStream* os);
  130. SMsgReader* reader() { return reader_; }
  131. SMsgWriter* writer() { return writer_; }
  132. rdr::InStream* getInStream() { return is; }
  133. rdr::OutStream* getOutStream() { return os; }
  134. enum stateEnum {
  135. RFBSTATE_UNINITIALISED,
  136. RFBSTATE_PROTOCOL_VERSION,
  137. RFBSTATE_SECURITY_TYPE,
  138. RFBSTATE_SECURITY,
  139. RFBSTATE_QUERYING,
  140. RFBSTATE_INITIALISATION,
  141. RFBSTATE_NORMAL,
  142. RFBSTATE_CLOSING,
  143. RFBSTATE_INVALID
  144. };
  145. stateEnum state() { return state_; }
  146. rdr::S32 getPreferredEncoding() { return preferredEncoding; }
  147. protected:
  148. void setState(stateEnum s) { state_ = s; }
  149. void setReader(SMsgReader *r) { reader_ = r; }
  150. void setWriter(SMsgWriter *w) { writer_ = w; }
  151. private:
  152. void writeFakeColourMap(void);
  153. bool readyForSetColourMapEntries;
  154. void processVersionMsg();
  155. void processSecurityTypeMsg();
  156. void processSecurityType(int secType);
  157. void processSecurityMsg();
  158. void processInitMsg();
  159. int defaultMajorVersion, defaultMinorVersion;
  160. rdr::InStream* is;
  161. rdr::OutStream* os;
  162. SMsgReader* reader_;
  163. SMsgWriter* writer_;
  164. SecurityServer *security;
  165. SSecurity* ssecurity;
  166. stateEnum state_;
  167. rdr::S32 preferredEncoding;
  168. };
  169. }
  170. #endif