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.

VNCServerST.h 8.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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. // -=- VNCServerST.h
  19. // Single-threaded VNCServer implementation
  20. #ifndef __RFB_VNCSERVERST_H__
  21. #define __RFB_VNCSERVERST_H__
  22. #include <sys/time.h>
  23. #include <list>
  24. #include <rfb/SDesktop.h>
  25. #include <rfb/VNCServer.h>
  26. #include <rfb/Configuration.h>
  27. #include <rfb/LogWriter.h>
  28. #include <rfb/Blacklist.h>
  29. #include <rfb/Cursor.h>
  30. #include <rfb/Timer.h>
  31. #include <network/Socket.h>
  32. #include <rfb/ListConnInfo.h>
  33. #include <rfb/ScreenSet.h>
  34. namespace rfb {
  35. class VNCSConnectionST;
  36. class ComparingUpdateTracker;
  37. class PixelBuffer;
  38. class KeyRemapper;
  39. class VNCServerST : public VNCServer,
  40. public Timer::Callback,
  41. public network::SocketServer {
  42. public:
  43. // -=- Constructors
  44. // Create a server exporting the supplied desktop.
  45. VNCServerST(const char* name_, SDesktop* desktop_);
  46. virtual ~VNCServerST();
  47. // Methods overridden from SocketServer
  48. // addSocket
  49. // Causes the server to allocate an RFB-protocol management
  50. // structure for the socket & initialise it.
  51. virtual void addSocket(network::Socket* sock, bool outgoing=false);
  52. // removeSocket
  53. // Clean up any resources associated with the Socket
  54. virtual void removeSocket(network::Socket* sock);
  55. // processSocketEvent
  56. // Read more RFB data from the Socket. If an error occurs during
  57. // processing then shutdown() is called on the Socket, causing
  58. // removeSocket() to be called by the caller at a later time.
  59. virtual void processSocketEvent(network::Socket* sock);
  60. // checkTimeouts
  61. // Returns the number of milliseconds left until the next idle timeout
  62. // expires. If any have already expired, the corresponding connections
  63. // are closed. Zero is returned if there is no idle timeout.
  64. virtual int checkTimeouts();
  65. // Methods overridden from VNCServer
  66. virtual void blockUpdates();
  67. virtual void unblockUpdates();
  68. virtual void setPixelBuffer(PixelBuffer* pb, const ScreenSet& layout);
  69. virtual void setPixelBuffer(PixelBuffer* pb);
  70. virtual void setScreenLayout(const ScreenSet& layout);
  71. virtual PixelBuffer* getPixelBuffer() const { return pb; }
  72. virtual void serverCutText(const char* str, int len);
  73. virtual void add_changed(const Region &region);
  74. virtual void add_copied(const Region &dest, const Point &delta);
  75. virtual void setCursor(int width, int height, const Point& hotspot,
  76. const void* cursorData, const void* mask);
  77. virtual void setCursorPos(const Point& p);
  78. virtual void bell();
  79. // - Close all currently-connected clients, by calling
  80. // their close() method with the supplied reason.
  81. virtual void closeClients(const char* reason) {closeClients(reason, 0);}
  82. // VNCServerST-only methods
  83. // closeClients() closes all RFB sessions, except the specified one (if
  84. // any), and logs the specified reason for closure.
  85. void closeClients(const char* reason, network::Socket* sock);
  86. // getSockets() gets a list of sockets. This can be used to generate an
  87. // fd_set for calling select().
  88. void getSockets(std::list<network::Socket*>* sockets);
  89. // getSConnection() gets the SConnection for a particular Socket. If
  90. // the Socket is not recognised then null is returned.
  91. SConnection* getSConnection(network::Socket* sock);
  92. // getDesktopSize() returns the size of the SDesktop exported by this
  93. // server.
  94. Point getDesktopSize() const {return desktop->getFbSize();}
  95. // getName() returns the name of this VNC Server. NB: The value returned
  96. // is the server's internal buffer which may change after any other methods
  97. // are called - take a copy if necessary.
  98. const char* getName() const {return name.buf;}
  99. // setName() specifies the desktop name that the server should provide to
  100. // clients
  101. virtual void setName(const char* name_);
  102. // A QueryConnectionHandler, if supplied, is passed details of incoming
  103. // connections to approve, reject, or query the user about.
  104. //
  105. // queryConnection() is called when a connection has been
  106. // successfully authenticated. The sock and userName arguments identify
  107. // the socket and the name of the authenticated user, if any. It should
  108. // return ACCEPT if the connection should be accepted, REJECT if it should
  109. // be rejected, or PENDING if a decision cannot yet be reached. If REJECT
  110. // is returned, *reason can be set to a string describing the reason - this
  111. // will be delete[]ed when it is finished with. If PENDING is returned,
  112. // approveConnection() must be called some time later to accept or reject
  113. // the connection.
  114. enum queryResult { ACCEPT, REJECT, PENDING };
  115. struct QueryConnectionHandler {
  116. virtual ~QueryConnectionHandler() {}
  117. virtual queryResult queryConnection(network::Socket* sock,
  118. const char* userName,
  119. char** reason) = 0;
  120. };
  121. void setQueryConnectionHandler(QueryConnectionHandler* qch) {
  122. queryConnectionHandler = qch;
  123. }
  124. // queryConnection is called as described above, and either passes the
  125. // request on to the registered handler, or accepts the connection if
  126. // no handler has been specified.
  127. virtual queryResult queryConnection(network::Socket* sock,
  128. const char* userName,
  129. char** reason) {
  130. return queryConnectionHandler
  131. ? queryConnectionHandler->queryConnection(sock, userName, reason)
  132. : ACCEPT;
  133. }
  134. // approveConnection() is called by the active QueryConnectionHandler,
  135. // some time after queryConnection() has returned with PENDING, to accept
  136. // or reject the connection. The accept argument should be true for
  137. // acceptance, or false for rejection, in which case a string reason may
  138. // also be given.
  139. void approveConnection(network::Socket* sock, bool accept,
  140. const char* reason);
  141. // setBlacklist() is called to replace the VNCServerST's internal
  142. // Blacklist instance with another instance. This allows a single
  143. // Blacklist to be shared by multiple VNCServerST instances.
  144. void setBlacklist(Blacklist* bl) {blHosts = bl ? bl : &blacklist;}
  145. // setKeyRemapper() replaces the VNCServerST's default key remapper.
  146. // NB: A null pointer is valid here.
  147. void setKeyRemapper(KeyRemapper* kr) { keyRemapper = kr; }
  148. void getConnInfo(ListConnInfo * listConn);
  149. void setConnStatus(ListConnInfo* listConn);
  150. bool getDisable() { return disableclients;};
  151. void setDisable(bool disable) { disableclients = disable;};
  152. protected:
  153. friend class VNCSConnectionST;
  154. // Timer callbacks
  155. virtual bool handleTimeout(Timer* t);
  156. // - Internal methods
  157. void startDesktop();
  158. static LogWriter connectionsLog;
  159. Blacklist blacklist;
  160. Blacklist* blHosts;
  161. SDesktop* desktop;
  162. bool desktopStarted;
  163. int blockCounter;
  164. PixelBuffer* pb;
  165. ScreenSet screenLayout;
  166. CharArray name;
  167. std::list<VNCSConnectionST*> clients;
  168. VNCSConnectionST* pointerClient;
  169. std::list<network::Socket*> closingSockets;
  170. ComparingUpdateTracker* comparer;
  171. Point cursorPos;
  172. Cursor cursor;
  173. RenderedCursor renderedCursor;
  174. bool renderedCursorInvalid;
  175. // - Check how many of the clients are authenticated.
  176. int authClientCount();
  177. bool needRenderedCursor();
  178. void startDefer();
  179. bool checkDefer();
  180. void tryUpdate();
  181. bool checkUpdate();
  182. void notifyScreenLayoutChange(VNCSConnectionST *requester);
  183. bool getComparerState();
  184. QueryConnectionHandler* queryConnectionHandler;
  185. KeyRemapper* keyRemapper;
  186. time_t lastUserInputTime;
  187. time_t lastDisconnectTime;
  188. time_t lastConnectionTime;
  189. bool disableclients;
  190. Timer deferTimer;
  191. bool deferPending;
  192. struct timeval deferStart;
  193. };
  194. };
  195. #endif