您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

VNCServerWin32.cxx 9.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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. // -=- WinVNC Version 4.0 Main Routine
  19. #include <winvnc/VNCServerWin32.h>
  20. #include <winvnc/resource.h>
  21. #include <winvnc/STrayIcon.h>
  22. #include <rfb_win32/ComputerName.h>
  23. #include <rfb_win32/CurrentUser.h>
  24. #include <rfb_win32/Service.h>
  25. #include <rfb/Hostname.h>
  26. #include <rfb/LogWriter.h>
  27. using namespace rfb;
  28. using namespace win32;
  29. using namespace winvnc;
  30. using namespace network;
  31. static LogWriter vlog("VNCServerWin32");
  32. const TCHAR* winvnc::VNCServerWin32::RegConfigPath = _T("Software\\TigerVNC\\WinVNC4");
  33. static IntParameter http_port("HTTPPortNumber",
  34. "TCP/IP port on which the server will serve the Java applet VNC Viewer ", 5800);
  35. static IntParameter port_number("PortNumber",
  36. "TCP/IP port on which the server will accept connections", 5900);
  37. static StringParameter hosts("Hosts",
  38. "Filter describing which hosts are allowed access to this server", "+0.0.0.0/0.0.0.0");
  39. static BoolParameter localHost("LocalHost",
  40. "Only accept connections from via the local loop-back network interface", false);
  41. static BoolParameter queryOnlyIfLoggedOn("QueryOnlyIfLoggedOn",
  42. "Only prompt for a local user to accept incoming connections if there is a user logged on", false);
  43. static BoolParameter showTrayIcon("ShowTrayIcon",
  44. "Show the configuration applet in the system tray icon", true);
  45. VNCServerWin32::VNCServerWin32()
  46. : command(NoCommand), commandSig(commandLock),
  47. commandEvent(CreateEvent(0, TRUE, FALSE, 0)),
  48. sessionEvent(isServiceProcess() ?
  49. CreateEvent(0, FALSE, FALSE, "Global\\SessionEventTigerVNC") : 0),
  50. vncServer(CStr(ComputerName().buf), &desktop),
  51. hostThread(0), runServer(false), isDesktopStarted(false),
  52. httpServer(&vncServer), config(&sockMgr),
  53. rfbSock(&sockMgr), httpSock(&sockMgr), trayIcon(0),
  54. queryConnectDialog(0)
  55. {
  56. // Initialise the desktop
  57. desktop.setStatusLocation(&isDesktopStarted);
  58. // Initialise the VNC server
  59. vncServer.setQueryConnectionHandler(this);
  60. // Register the desktop's event to be handled
  61. sockMgr.addEvent(desktop.getUpdateEvent(), &desktop);
  62. // Register the queued command event to be handled
  63. sockMgr.addEvent(commandEvent, this);
  64. if (sessionEvent)
  65. sockMgr.addEvent(sessionEvent, this);
  66. }
  67. VNCServerWin32::~VNCServerWin32() {
  68. delete trayIcon;
  69. // Stop the SDisplay from updating our state
  70. desktop.setStatusLocation(0);
  71. // Join the Accept/Reject dialog thread
  72. if (queryConnectDialog)
  73. delete queryConnectDialog->join();
  74. }
  75. void VNCServerWin32::processAddressChange(network::SocketListener* sock_) {
  76. if (!trayIcon || (sock_ != rfbSock.sock))
  77. return;
  78. // Tool-tip prefix depends on server mode
  79. const TCHAR* prefix = _T("VNC Server (User):");
  80. if (isServiceProcess())
  81. prefix = _T("VNC Server (Service):");
  82. // Fetch the list of addresses
  83. std::list<char*> addrs;
  84. if (rfbSock.sock)
  85. rfbSock.sock->getMyAddresses(&addrs);
  86. else
  87. addrs.push_front(strDup("Not accepting connections"));
  88. // Allocate space for the new tip
  89. std::list<char*>::iterator i, next_i;
  90. int length = _tcslen(prefix)+1;
  91. for (i=addrs.begin(); i!= addrs.end(); i++)
  92. length += strlen(*i) + 1;
  93. // Build the new tip
  94. TCharArray toolTip(length);
  95. _tcscpy(toolTip.buf, prefix);
  96. for (i=addrs.begin(); i!= addrs.end(); i=next_i) {
  97. next_i = i; next_i ++;
  98. TCharArray addr(*i); // Assumes ownership of string
  99. _tcscat(toolTip.buf, addr.buf);
  100. if (next_i != addrs.end())
  101. _tcscat(toolTip.buf, _T(","));
  102. }
  103. // Pass the new tip to the tray icon
  104. vlog.info("Refreshing tray icon");
  105. trayIcon->setToolTip(toolTip.buf);
  106. }
  107. void VNCServerWin32::regConfigChanged() {
  108. // -=- Make sure we're listening on the right ports.
  109. rfbSock.setServer(&vncServer);
  110. rfbSock.setPort(port_number, localHost);
  111. httpSock.setServer(&httpServer);
  112. httpSock.setPort(http_port, localHost);
  113. // -=- Update the Java viewer's web page port number.
  114. httpServer.setRFBport(rfbSock.sock ? port_number : 0);
  115. // -=- Update the TCP address filter for both ports, if open.
  116. CharArray pattern(hosts.getData());
  117. rfbSock.setFilter(pattern.buf);
  118. httpSock.setFilter(pattern.buf);
  119. // -=- Update the tray icon tooltip text with IP addresses
  120. processAddressChange(rfbSock.sock);
  121. }
  122. int VNCServerWin32::run() {
  123. { Lock l(runLock);
  124. hostThread = Thread::self();
  125. runServer = true;
  126. }
  127. // - Create the tray icon (if possible)
  128. if (showTrayIcon)
  129. trayIcon = new STrayIconThread(*this, IDI_ICON, IDI_CONNECTED,
  130. IDI_ICON_DISABLE, IDI_CONNECTED_DISABLE,
  131. IDR_TRAY);
  132. // - Register for notification of configuration changes
  133. config.setCallback(this);
  134. if (isServiceProcess())
  135. config.setKey(HKEY_LOCAL_MACHINE, RegConfigPath);
  136. else
  137. config.setKey(HKEY_CURRENT_USER, RegConfigPath);
  138. // - Set the address-changed handler for the RFB socket
  139. rfbSock.setAddressChangeNotifier(this);
  140. DWORD result = 0;
  141. try {
  142. vlog.debug("Entering message loop");
  143. // - Run the server until we're told to quit
  144. MSG msg;
  145. int result = 0;
  146. while (runServer) {
  147. result = sockMgr.getMessage(&msg, NULL, 0, 0);
  148. if (result < 0)
  149. throw rdr::SystemException("getMessage", GetLastError());
  150. if (!isServiceProcess() && (result == 0))
  151. break;
  152. TranslateMessage(&msg);
  153. DispatchMessage(&msg);
  154. }
  155. vlog.debug("Server exited cleanly");
  156. } catch (rdr::SystemException &s) {
  157. vlog.error("%s", s.str());
  158. result = s.err;
  159. } catch (rdr::Exception &e) {
  160. vlog.error("%s", e.str());
  161. }
  162. { Lock l(runLock);
  163. runServer = false;
  164. hostThread = 0;
  165. }
  166. return result;
  167. }
  168. void VNCServerWin32::stop() {
  169. Lock l(runLock);
  170. runServer = false;
  171. if (hostThread)
  172. PostThreadMessage(hostThread->getThreadId(), WM_QUIT, 0, 0);
  173. }
  174. bool VNCServerWin32::disconnectClients(const char* reason) {
  175. return queueCommand(DisconnectClients, reason, 0);
  176. }
  177. bool VNCServerWin32::addNewClient(const char* client) {
  178. TcpSocket* sock = 0;
  179. try {
  180. CharArray hostname;
  181. int port;
  182. getHostAndPort(client, &hostname.buf, &port, 5500);
  183. vlog.error("port=%d", port);
  184. sock = new TcpSocket(hostname.buf, port);
  185. if (queueCommand(AddClient, sock, 0))
  186. return true;
  187. delete sock;
  188. } catch (...) {
  189. delete sock;
  190. }
  191. return false;
  192. }
  193. bool VNCServerWin32::getClientsInfo(rfb::ListConnInfo* LCInfo) {
  194. return queueCommand(GetClientsInfo, LCInfo, 0);
  195. }
  196. bool VNCServerWin32::setClientsStatus(rfb::ListConnInfo* LCInfo) {
  197. return queueCommand(SetClientsStatus, LCInfo, 0);
  198. }
  199. VNCServerST::queryResult VNCServerWin32::queryConnection(network::Socket* sock,
  200. const char* userName,
  201. char** reason)
  202. {
  203. if (queryOnlyIfLoggedOn && CurrentUserToken().noUserLoggedOn())
  204. return VNCServerST::ACCEPT;
  205. if (queryConnectDialog) {
  206. *reason = rfb::strDup("Another connection is currently being queried.");
  207. return VNCServerST::REJECT;
  208. }
  209. queryConnectDialog = new QueryConnectDialog(sock, userName, this);
  210. queryConnectDialog->startDialog();
  211. return VNCServerST::PENDING;
  212. }
  213. void VNCServerWin32::queryConnectionComplete() {
  214. queueCommand(QueryConnectionComplete, 0, 0, false);
  215. }
  216. bool VNCServerWin32::queueCommand(Command cmd, const void* data, int len, bool wait) {
  217. Lock l(commandLock);
  218. while (command != NoCommand)
  219. commandSig.wait();
  220. command = cmd;
  221. commandData = data;
  222. commandDataLen = len;
  223. SetEvent(commandEvent);
  224. if (wait) {
  225. while (command != NoCommand)
  226. commandSig.wait();
  227. commandSig.signal();
  228. }
  229. return true;
  230. }
  231. void VNCServerWin32::processEvent(HANDLE event_) {
  232. ResetEvent(event_);
  233. if (event_ == commandEvent.h) {
  234. // If there is no command queued then return immediately
  235. {
  236. Lock l(commandLock);
  237. if (command == NoCommand)
  238. return;
  239. }
  240. // Perform the required command
  241. switch (command) {
  242. case DisconnectClients:
  243. // Disconnect all currently active VNC Viewers
  244. vncServer.closeClients((const char*)commandData);
  245. break;
  246. case AddClient:
  247. // Make a reverse connection to a VNC Viewer
  248. sockMgr.addSocket((network::Socket*)commandData, &vncServer);
  249. break;
  250. case GetClientsInfo:
  251. vncServer.getConnInfo((ListConnInfo*)commandData);
  252. break;
  253. case SetClientsStatus:
  254. vncServer.setConnStatus((ListConnInfo*)commandData);
  255. break;
  256. case QueryConnectionComplete:
  257. // The Accept/Reject dialog has completed
  258. // Get the result, then clean it up
  259. vncServer.approveConnection(queryConnectDialog->getSock(),
  260. queryConnectDialog->isAccepted(),
  261. "Connection rejected by user");
  262. delete queryConnectDialog->join();
  263. queryConnectDialog = 0;
  264. break;
  265. default:
  266. vlog.error("unknown command %d queued", command);
  267. };
  268. // Clear the command and signal completion
  269. {
  270. Lock l(commandLock);
  271. command = NoCommand;
  272. commandSig.signal();
  273. }
  274. } else if (event_ == sessionEvent.h) {
  275. stop();
  276. }
  277. }