Constantin Kaplinsky | 729598c | 2006-05-25 05:12:25 +0000 | [diff] [blame] | 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 | // -=- VNC Server 4.0 for Windows (WinVNC4) |
| 20 | |
| 21 | #include <string.h> |
Constantin Kaplinsky | 729598c | 2006-05-25 05:12:25 +0000 | [diff] [blame] | 22 | |
| 23 | #include <winvnc/VNCServerWin32.h> |
| 24 | #include <winvnc/VNCServerService.h> |
| 25 | #include <winvnc/AddNewClientDialog.h> |
| 26 | |
| 27 | #include <rfb/Logger_stdio.h> |
| 28 | #include <rfb/Logger_file.h> |
| 29 | #include <rfb/LogWriter.h> |
| 30 | #include <rfb_win32/AboutDialog.h> |
| 31 | #include <rfb_win32/MsgBox.h> |
| 32 | #include <network/TcpSocket.h> |
| 33 | |
| 34 | using namespace winvnc; |
| 35 | using namespace rfb; |
| 36 | using namespace win32; |
| 37 | |
| 38 | static LogWriter vlog("main"); |
| 39 | |
| 40 | TStr rfb::win32::AppName("VNC Server"); |
| 41 | |
| 42 | |
Samuel Mannehed | 60c4193 | 2014-02-07 14:53:24 +0000 | [diff] [blame] | 43 | extern bool runAsService; |
Constantin Kaplinsky | 729598c | 2006-05-25 05:12:25 +0000 | [diff] [blame] | 44 | static bool runServer = true; |
| 45 | static bool close_console = false; |
| 46 | |
| 47 | |
| 48 | // |
| 49 | // -=- processParams |
| 50 | // Read in the command-line parameters and interpret them. |
| 51 | // |
| 52 | |
| 53 | static void programInfo() { |
| 54 | win32::FileVersionInfo inf; |
| 55 | _tprintf(_T("%s - %s, Version %s\n"), |
| 56 | inf.getVerString(_T("ProductName")), |
| 57 | inf.getVerString(_T("FileDescription")), |
| 58 | inf.getVerString(_T("FileVersion"))); |
| 59 | printf("%s\n", buildTime); |
| 60 | _tprintf(_T("%s\n\n"), inf.getVerString(_T("LegalCopyright"))); |
| 61 | } |
| 62 | |
| 63 | static void programUsage() { |
| 64 | printf("Command-line options:\n"); |
| 65 | printf(" -connect [<host[::port]>] - Connect an existing WinVNC server to a listening viewer.\n"); |
| 66 | printf(" -disconnect - Disconnect all clients from an existing WinVNC server.\n"); |
| 67 | printf(" -register <options...> - Register WinVNC server as a system service.\n"); |
| 68 | printf(" -unregister - Remove WinVNC server from the list of system services.\n"); |
| 69 | printf(" -start - Start the WinVNC server system service.\n"); |
| 70 | printf(" -stop - Stop the WinVNC server system service.\n"); |
| 71 | printf(" -status - Query the WinVNC service status.\n"); |
| 72 | printf(" -help - Provide usage information.\n"); |
| 73 | printf(" -noconsole - Run without a console (i.e. no stderr/stdout)\n"); |
| 74 | printf(" <setting>=<value> - Set the named configuration parameter.\n"); |
| 75 | printf(" (Parameter values specified on the command-line override those specified by other configuration methods.)\n"); |
| 76 | printf("\nLog names:\n"); |
| 77 | LogWriter::listLogWriters(); |
| 78 | printf("\nLog destinations:\n"); |
| 79 | Logger::listLoggers(); |
| 80 | printf("\nAvailable configuration parameters:\n"); |
Adam Tkac | c58b3d1 | 2010-04-23 13:55:10 +0000 | [diff] [blame] | 81 | Configuration::listParams(ConfServer); |
Constantin Kaplinsky | 729598c | 2006-05-25 05:12:25 +0000 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | static void MsgBoxOrLog(const char* msg, bool isError=false) { |
| 85 | if (close_console) { |
| 86 | MsgBox(0, TStr(msg), (isError ? MB_ICONERROR : MB_ICONINFORMATION) | MB_OK); |
| 87 | } else { |
| 88 | if (isError) { |
| 89 | try { |
| 90 | vlog.error(msg); |
| 91 | return; |
| 92 | } catch (...) { |
| 93 | } |
| 94 | } |
| 95 | fprintf(stderr, "%s\n", msg); |
| 96 | } |
| 97 | } |
| 98 | |
Adam Tkac | 8fb6ac0 | 2010-06-25 11:24:27 +0000 | [diff] [blame] | 99 | static void processParams(int argc, char** argv) { |
Constantin Kaplinsky | 729598c | 2006-05-25 05:12:25 +0000 | [diff] [blame] | 100 | for (int i=1; i<argc; i++) { |
| 101 | try { |
| 102 | |
| 103 | if (strcasecmp(argv[i], "-connect") == 0) { |
| 104 | runServer = false; |
| 105 | CharArray host; |
| 106 | if (i+1 < argc) { |
| 107 | host.buf = strDup(argv[i+1]); |
| 108 | i++; |
| 109 | } else { |
| 110 | AddNewClientDialog ancd; |
| 111 | if (ancd.showDialog()) |
| 112 | host.buf = strDup(ancd.getHostName()); |
| 113 | } |
| 114 | if (host.buf) { |
| 115 | HWND hwnd = FindWindow(0, _T("winvnc::IPC_Interface")); |
| 116 | if (!hwnd) |
| 117 | throw rdr::Exception("Unable to locate existing VNC Server."); |
| 118 | COPYDATASTRUCT copyData; |
| 119 | copyData.dwData = 1; // *** AddNewClient |
| 120 | copyData.cbData = strlen(host.buf); |
| 121 | copyData.lpData = (void*)host.buf; |
| 122 | printf("Sending connect request to VNC Server...\n"); |
| 123 | if (!SendMessage(hwnd, WM_COPYDATA, 0, (LPARAM)©Data)) |
| 124 | MsgBoxOrLog("Connection failed.", true); |
| 125 | } |
| 126 | } else if (strcasecmp(argv[i], "-disconnect") == 0) { |
| 127 | runServer = false; |
| 128 | HWND hwnd = FindWindow(0, _T("winvnc::IPC_Interface")); |
| 129 | if (!hwnd) |
| 130 | throw rdr::Exception("Unable to locate existing VNC Server."); |
| 131 | COPYDATASTRUCT copyData; |
| 132 | copyData.dwData = 2; // *** DisconnectClients |
| 133 | copyData.lpData = 0; |
| 134 | copyData.cbData = 0; |
| 135 | printf("Sending disconnect request to VNC Server...\n"); |
| 136 | if (!SendMessage(hwnd, WM_COPYDATA, 0, (LPARAM)©Data)) |
| 137 | MsgBoxOrLog("Failed to disconnect clients.", true); |
| 138 | } else if (strcasecmp(argv[i], "-start") == 0) { |
| 139 | printf("Attempting to start service...\n"); |
| 140 | runServer = false; |
| 141 | if (rfb::win32::startService(VNCServerService::Name)) |
| 142 | MsgBoxOrLog("Started service successfully"); |
| 143 | } else if (strcasecmp(argv[i], "-stop") == 0) { |
| 144 | printf("Attempting to stop service...\n"); |
| 145 | runServer = false; |
| 146 | if (rfb::win32::stopService(VNCServerService::Name)) |
| 147 | MsgBoxOrLog("Stopped service successfully"); |
| 148 | } else if (strcasecmp(argv[i], "-status") == 0) { |
| 149 | printf("Querying service status...\n"); |
| 150 | runServer = false; |
Pierre Ossman | ba6fbfe | 2015-03-03 16:41:29 +0100 | [diff] [blame^] | 151 | CharArray result; |
Constantin Kaplinsky | 729598c | 2006-05-25 05:12:25 +0000 | [diff] [blame] | 152 | DWORD state = rfb::win32::getServiceState(VNCServerService::Name); |
Pierre Ossman | ba6fbfe | 2015-03-03 16:41:29 +0100 | [diff] [blame^] | 153 | result.format("The %s Service is in the %s state.", |
| 154 | (const char*)CStr(VNCServerService::Name), |
| 155 | rfb::win32::serviceStateName(state)); |
Constantin Kaplinsky | 729598c | 2006-05-25 05:12:25 +0000 | [diff] [blame] | 156 | MsgBoxOrLog(result.buf); |
| 157 | } else if (strcasecmp(argv[i], "-service") == 0) { |
| 158 | printf("Run in service mode\n"); |
Samuel Mannehed | 60c4193 | 2014-02-07 14:53:24 +0000 | [diff] [blame] | 159 | runServer = false; |
| 160 | runAsService = true; |
| 161 | |
| 162 | } else if (strcasecmp(argv[i], "-service_run") == 0) { |
| 163 | printf("Run in service mode\n"); |
Constantin Kaplinsky | 729598c | 2006-05-25 05:12:25 +0000 | [diff] [blame] | 164 | runAsService = true; |
| 165 | |
| 166 | } else if (strcasecmp(argv[i], "-register") == 0) { |
| 167 | printf("Attempting to register service...\n"); |
| 168 | runServer = false; |
| 169 | int j = i; |
| 170 | i = argc; |
| 171 | if (rfb::win32::registerService(VNCServerService::Name, |
| 172 | _T("VNC Server Version 4"), |
| 173 | argc-(j+1), &argv[j+1])) |
| 174 | MsgBoxOrLog("Registered service successfully"); |
| 175 | } else if (strcasecmp(argv[i], "-unregister") == 0) { |
| 176 | printf("Attempting to unregister service...\n"); |
| 177 | runServer = false; |
| 178 | if (rfb::win32::unregisterService(VNCServerService::Name)) |
| 179 | MsgBoxOrLog("Unregistered service successfully"); |
| 180 | |
| 181 | } else if (strcasecmp(argv[i], "-noconsole") == 0) { |
| 182 | close_console = true; |
| 183 | vlog.info("closing console"); |
| 184 | if (!FreeConsole()) |
Pierre Ossman | fb450fb | 2015-03-03 16:34:56 +0100 | [diff] [blame] | 185 | vlog.info("unable to close console:%lu", GetLastError()); |
Constantin Kaplinsky | 729598c | 2006-05-25 05:12:25 +0000 | [diff] [blame] | 186 | |
| 187 | } else if ((strcasecmp(argv[i], "-help") == 0) || |
| 188 | (strcasecmp(argv[i], "--help") == 0) || |
| 189 | (strcasecmp(argv[i], "-h") == 0) || |
| 190 | (strcasecmp(argv[i], "/?") == 0)) { |
| 191 | runServer = false; |
| 192 | programUsage(); |
| 193 | break; |
| 194 | |
| 195 | } else { |
| 196 | // Try to process <option>=<value>, or -<bool> |
| 197 | if (Configuration::setParam(argv[i], true)) |
| 198 | continue; |
| 199 | // Try to process -<option> <value> |
| 200 | if ((argv[i][0] == '-') && (i+1 < argc)) { |
| 201 | if (Configuration::setParam(&argv[i][1], argv[i+1], true)) { |
| 202 | i++; |
| 203 | continue; |
| 204 | } |
| 205 | } |
| 206 | // Nope. Show them usage and don't run the server |
| 207 | runServer = false; |
| 208 | programUsage(); |
| 209 | break; |
| 210 | } |
| 211 | |
| 212 | } catch (rdr::Exception& e) { |
| 213 | MsgBoxOrLog(e.str(), true); |
| 214 | } |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | |
| 219 | // |
| 220 | // -=- main |
| 221 | // |
| 222 | |
Adam Tkac | 01345fc | 2010-06-25 11:29:22 +0000 | [diff] [blame] | 223 | int WINAPI WinMain(HINSTANCE inst, HINSTANCE prevInst, char* cmdLine, int cmdShow) { |
Constantin Kaplinsky | 729598c | 2006-05-25 05:12:25 +0000 | [diff] [blame] | 224 | int result = 0; |
| 225 | |
| 226 | try { |
| 227 | // - Initialise the available loggers |
| 228 | //freopen("\\\\drupe\\tjr\\WinVNC4.log","ab",stderr); |
Adam Tkac | d41d4be | 2011-01-26 19:09:03 +0000 | [diff] [blame] | 229 | #ifdef _DEBUG |
| 230 | AllocConsole(); |
| 231 | freopen("CONIN$", "rb", stdin); |
| 232 | freopen("CONOUT$", "wb", stdout); |
| 233 | freopen("CONOUT$", "wb", stderr); |
| 234 | setbuf(stderr, 0); |
| 235 | initStdIOLoggers(); |
| 236 | initFileLogger("C:\\temp\\WinVNC4.log"); |
| 237 | logParams.setParam("*:stderr:100"); |
| 238 | #else |
Constantin Kaplinsky | 729598c | 2006-05-25 05:12:25 +0000 | [diff] [blame] | 239 | initFileLogger("C:\\temp\\WinVNC4.log"); |
Adam Tkac | d41d4be | 2011-01-26 19:09:03 +0000 | [diff] [blame] | 240 | logParams.setParam("*:stderr:0"); |
| 241 | #endif |
Constantin Kaplinsky | 729598c | 2006-05-25 05:12:25 +0000 | [diff] [blame] | 242 | rfb::win32::initEventLogLogger(VNCServerService::Name); |
| 243 | |
Adam Tkac | 0c4dcd2 | 2010-11-11 11:29:55 +0000 | [diff] [blame] | 244 | Configuration::enableServerParams(); |
| 245 | |
Constantin Kaplinsky | 729598c | 2006-05-25 05:12:25 +0000 | [diff] [blame] | 246 | // - By default, just log errors to stderr |
Adam Tkac | d41d4be | 2011-01-26 19:09:03 +0000 | [diff] [blame] | 247 | |
Constantin Kaplinsky | 729598c | 2006-05-25 05:12:25 +0000 | [diff] [blame] | 248 | |
| 249 | // - Print program details and process the command line |
| 250 | programInfo(); |
Adam Tkac | 01345fc | 2010-06-25 11:29:22 +0000 | [diff] [blame] | 251 | |
| 252 | int argc = __argc; |
| 253 | char **argv = __argv; |
Constantin Kaplinsky | 729598c | 2006-05-25 05:12:25 +0000 | [diff] [blame] | 254 | processParams(argc, argv); |
| 255 | |
| 256 | // - Run the server if required |
| 257 | if (runServer) { |
| 258 | // Start the network subsystem and run the server |
| 259 | VNCServerWin32 server; |
Samuel Mannehed | 60c4193 | 2014-02-07 14:53:24 +0000 | [diff] [blame] | 260 | result = server.run(); |
| 261 | } else if (runAsService) { |
| 262 | VNCServerService service; |
| 263 | service.start(); |
| 264 | result = service.getStatus().dwWin32ExitCode; |
Constantin Kaplinsky | 729598c | 2006-05-25 05:12:25 +0000 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | vlog.debug("WinVNC service destroyed"); |
| 268 | } catch (rdr::Exception& e) { |
| 269 | MsgBoxOrLog(e.str(), true); |
| 270 | } |
| 271 | |
| 272 | vlog.debug("WinVNC process quitting"); |
| 273 | return result; |
| 274 | } |