Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 1 | /* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved. |
| 2 | * Copyright 2011 Pierre Ossman <ossman@cendio.se> for Cendio AB |
DRC | b65bb93 | 2011-06-24 03:17:00 +0000 | [diff] [blame] | 3 | * Copyright (C) 2011 D. R. Commander. All Rights Reserved. |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 4 | * |
| 5 | * This is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 2 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This software is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this software; if not, write to the Free Software |
| 17 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
| 18 | * USA. |
| 19 | */ |
| 20 | |
Peter Åstrand | c359f36 | 2011-08-23 12:04:46 +0000 | [diff] [blame] | 21 | #ifdef HAVE_CONFIG_H |
| 22 | #include <config.h> |
| 23 | #endif |
| 24 | |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 25 | #include <string.h> |
| 26 | #include <stdio.h> |
Pierre Ossman | 2a7a8d6 | 2013-02-15 08:33:39 +0000 | [diff] [blame] | 27 | #include <ctype.h> |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 28 | #include <stdlib.h> |
| 29 | #include <errno.h> |
| 30 | #include <signal.h> |
| 31 | #include <locale.h> |
Pierre Ossman | 5bf17ad | 2015-02-17 13:23:00 +0100 | [diff] [blame] | 32 | #include <fcntl.h> |
Pierre Ossman | a71a276 | 2015-03-04 09:57:06 +0100 | [diff] [blame] | 33 | #include <unistd.h> |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 34 | #include <sys/stat.h> |
| 35 | |
| 36 | #ifdef WIN32 |
Peter Åstrand (astrand) | 11167e1 | 2015-02-05 11:10:32 +0100 | [diff] [blame] | 37 | #include <os/winerrno.h> |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 38 | #include <direct.h> |
| 39 | #define mkdir(path, mode) _mkdir(path) |
| 40 | #endif |
| 41 | |
Pierre Ossman | 6b9622d | 2014-07-21 16:42:12 +0200 | [diff] [blame] | 42 | #if !defined(WIN32) && !defined(__APPLE__) |
| 43 | #include <X11/Xlib.h> |
| 44 | #include <X11/XKBlib.h> |
| 45 | #endif |
| 46 | |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 47 | #include <rfb/Logger_stdio.h> |
| 48 | #include <rfb/SecurityClient.h> |
| 49 | #include <rfb/Security.h> |
| 50 | #ifdef HAVE_GNUTLS |
| 51 | #include <rfb/CSecurityTLS.h> |
| 52 | #endif |
| 53 | #include <rfb/LogWriter.h> |
| 54 | #include <rfb/Timer.h> |
Peter Åstrand | 8a2b081 | 2012-08-08 11:49:01 +0000 | [diff] [blame] | 55 | #include <rfb/Exception.h> |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 56 | #include <network/TcpSocket.h> |
DRC | 4426f00 | 2011-10-12 20:02:55 +0000 | [diff] [blame] | 57 | #include <os/os.h> |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 58 | |
| 59 | #include <FL/Fl.H> |
| 60 | #include <FL/Fl_Widget.H> |
Pierre Ossman | 8eb3508 | 2012-03-27 12:50:54 +0000 | [diff] [blame] | 61 | #include <FL/Fl_PNG_Image.H> |
Pierre Ossman | a71a276 | 2015-03-04 09:57:06 +0100 | [diff] [blame] | 62 | #include <FL/Fl_Sys_Menu_Bar.H> |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 63 | #include <FL/fl_ask.H> |
Pierre Ossman | b885822 | 2011-04-29 11:51:38 +0000 | [diff] [blame] | 64 | #include <FL/x.H> |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 65 | |
| 66 | #include "i18n.h" |
| 67 | #include "parameters.h" |
| 68 | #include "CConn.h" |
Pierre Ossman | 561ff0c | 2011-05-13 14:04:59 +0000 | [diff] [blame] | 69 | #include "ServerDialog.h" |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 70 | #include "UserDialog.h" |
Adam Tkac | 8ac4b30 | 2013-01-23 13:55:46 +0000 | [diff] [blame] | 71 | #include "vncviewer.h" |
Pierre Ossman | a71a276 | 2015-03-04 09:57:06 +0100 | [diff] [blame] | 72 | #include "fltk_layout.h" |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 73 | |
DRC | b65bb93 | 2011-06-24 03:17:00 +0000 | [diff] [blame] | 74 | #ifdef WIN32 |
Pierre Ossman | 8eb3508 | 2012-03-27 12:50:54 +0000 | [diff] [blame] | 75 | #include "resource.h" |
DRC | b65bb93 | 2011-06-24 03:17:00 +0000 | [diff] [blame] | 76 | #include "win32.h" |
| 77 | #endif |
| 78 | |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 79 | rfb::LogWriter vlog("main"); |
| 80 | |
| 81 | using namespace network; |
| 82 | using namespace rfb; |
| 83 | using namespace std; |
| 84 | |
Adam Tkac | 8ac4b30 | 2013-01-23 13:55:46 +0000 | [diff] [blame] | 85 | char vncServerName[VNCSERVERNAMELEN] = { '\0' }; |
| 86 | |
Pierre Ossman | a71a276 | 2015-03-04 09:57:06 +0100 | [diff] [blame] | 87 | static const char *argv0 = NULL; |
| 88 | |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 89 | static bool exitMainloop = false; |
Pierre Ossman | e2ef5c1 | 2011-07-12 16:56:34 +0000 | [diff] [blame] | 90 | static const char *exitError = NULL; |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 91 | |
Pierre Ossman | ccd09ba | 2015-04-07 14:58:45 +0200 | [diff] [blame] | 92 | static const char *about_text() |
| 93 | { |
| 94 | static char buffer[1024]; |
| 95 | |
| 96 | // This is used in multiple places with potentially different |
| 97 | // encodings, so we need to make sure we get a fresh string every |
| 98 | // time. |
| 99 | snprintf(buffer, sizeof(buffer), |
| 100 | _("TigerVNC Viewer %d-bit v%s\n" |
| 101 | "Built on: %s\n" |
| 102 | "Copyright (C) 1999-%d TigerVNC Team and many others (see README.txt)\n" |
| 103 | "See http://www.tigervnc.org for information on TigerVNC."), |
| 104 | (int)sizeof(size_t)*8, PACKAGE_VERSION, |
| 105 | BUILD_TIMESTAMP, 2015); |
| 106 | |
| 107 | return buffer; |
| 108 | } |
| 109 | |
Pierre Ossman | e2ef5c1 | 2011-07-12 16:56:34 +0000 | [diff] [blame] | 110 | void exit_vncviewer(const char *error) |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 111 | { |
Pierre Ossman | e2ef5c1 | 2011-07-12 16:56:34 +0000 | [diff] [blame] | 112 | // Prioritise the first error we get as that is probably the most |
| 113 | // relevant one. |
| 114 | if ((error != NULL) && (exitError == NULL)) |
| 115 | exitError = strdup(error); |
| 116 | |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 117 | exitMainloop = true; |
| 118 | } |
| 119 | |
Pierre Ossman | b885822 | 2011-04-29 11:51:38 +0000 | [diff] [blame] | 120 | void about_vncviewer() |
| 121 | { |
| 122 | fl_message_title(_("About TigerVNC Viewer")); |
Pierre Ossman | ccd09ba | 2015-04-07 14:58:45 +0200 | [diff] [blame] | 123 | fl_message("%s", about_text()); |
Pierre Ossman | b885822 | 2011-04-29 11:51:38 +0000 | [diff] [blame] | 124 | } |
| 125 | |
Pierre Ossman | 8d713a9 | 2015-03-04 09:54:27 +0100 | [diff] [blame] | 126 | #ifdef __APPLE__ |
Pierre Ossman | b885822 | 2011-04-29 11:51:38 +0000 | [diff] [blame] | 127 | static void about_callback(Fl_Widget *widget, void *data) |
| 128 | { |
| 129 | about_vncviewer(); |
| 130 | } |
Pierre Ossman | a71a276 | 2015-03-04 09:57:06 +0100 | [diff] [blame] | 131 | |
| 132 | static void new_connection_cb(Fl_Widget *widget, void *data) |
| 133 | { |
| 134 | const char *argv[2]; |
| 135 | pid_t pid; |
| 136 | |
| 137 | pid = fork(); |
| 138 | if (pid == -1) { |
| 139 | vlog.error(_("Error starting new TigerVNC Viewer: %s"), strerror(errno)); |
| 140 | return; |
| 141 | } |
| 142 | |
| 143 | if (pid != 0) |
| 144 | return; |
| 145 | |
| 146 | argv[0] = argv0; |
| 147 | argv[1] = NULL; |
| 148 | |
| 149 | execvp(argv[0], (char * const *)argv); |
| 150 | |
| 151 | vlog.error(_("Error starting new TigerVNC Viewer: %s"), strerror(errno)); |
| 152 | _exit(1); |
| 153 | } |
Pierre Ossman | 8d713a9 | 2015-03-04 09:54:27 +0100 | [diff] [blame] | 154 | #endif |
Pierre Ossman | b885822 | 2011-04-29 11:51:38 +0000 | [diff] [blame] | 155 | |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 156 | static void CleanupSignalHandler(int sig) |
| 157 | { |
| 158 | // CleanupSignalHandler allows C++ object cleanup to happen because it calls |
| 159 | // exit() rather than the default which is to abort. |
Pierre Ossman | c76bd2b | 2014-12-03 14:01:31 +0100 | [diff] [blame] | 160 | vlog.info(_("Termination signal %d has been received. TigerVNC Viewer will now exit."), sig); |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 161 | exit(1); |
| 162 | } |
| 163 | |
| 164 | static void init_fltk() |
| 165 | { |
| 166 | // Basic text size (10pt @ 96 dpi => 13px) |
| 167 | FL_NORMAL_SIZE = 13; |
| 168 | |
| 169 | #ifndef __APPLE__ |
| 170 | // Select a FLTK scheme and background color that looks somewhat |
| 171 | // close to modern Linux and Windows. |
| 172 | Fl::scheme("gtk+"); |
| 173 | Fl::background(220, 220, 220); |
| 174 | #else |
| 175 | // On Mac OS X there is another scheme that fits better though. |
| 176 | Fl::scheme("plastic"); |
| 177 | #endif |
| 178 | |
Henrik Andersson | 3b83703 | 2011-09-19 13:46:55 +0000 | [diff] [blame] | 179 | // Proper Gnome Shell integration requires that we set a sensible |
| 180 | // WM_CLASS for the window. |
| 181 | Fl_Window::default_xclass("vncviewer"); |
| 182 | |
Pierre Ossman | 8eb3508 | 2012-03-27 12:50:54 +0000 | [diff] [blame] | 183 | // Set the default icon for all windows. |
Pierre Ossman | 8eb3508 | 2012-03-27 12:50:54 +0000 | [diff] [blame] | 184 | #ifdef WIN32 |
| 185 | HICON lg, sm; |
| 186 | |
| 187 | lg = (HICON)LoadImage(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_ICON), |
| 188 | IMAGE_ICON, GetSystemMetrics(SM_CXICON), |
| 189 | GetSystemMetrics(SM_CYICON), |
| 190 | LR_DEFAULTCOLOR | LR_SHARED); |
| 191 | sm = (HICON)LoadImage(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_ICON), |
| 192 | IMAGE_ICON, GetSystemMetrics(SM_CXSMICON), |
| 193 | GetSystemMetrics(SM_CYSMICON), |
| 194 | LR_DEFAULTCOLOR | LR_SHARED); |
| 195 | |
| 196 | Fl_Window::default_icons(lg, sm); |
| 197 | #elif ! defined(__APPLE__) |
| 198 | const int icon_sizes[] = {48, 32, 24, 16}; |
| 199 | |
| 200 | Fl_PNG_Image *icons[4]; |
| 201 | int count; |
| 202 | |
| 203 | count = 0; |
| 204 | |
| 205 | // FIXME: Follow icon theme specification |
| 206 | for (size_t i = 0;i < sizeof(icon_sizes)/sizeof(icon_sizes[0]);i++) { |
| 207 | char icon_path[PATH_MAX]; |
| 208 | bool exists; |
| 209 | |
Pierre Ossman | 6a007bd | 2012-09-11 10:56:21 +0000 | [diff] [blame] | 210 | sprintf(icon_path, "%s/icons/hicolor/%dx%d/apps/tigervnc.png", |
Pierre Ossman | 8eb3508 | 2012-03-27 12:50:54 +0000 | [diff] [blame] | 211 | DATA_DIR, icon_sizes[i], icon_sizes[i]); |
| 212 | |
| 213 | #ifndef WIN32 |
| 214 | struct stat st; |
| 215 | if (stat(icon_path, &st) != 0) |
| 216 | #else |
| 217 | struct _stat st; |
| 218 | if (_stat(icon_path, &st) != 0) |
| 219 | return(false); |
| 220 | #endif |
| 221 | exists = false; |
| 222 | else |
| 223 | exists = true; |
| 224 | |
| 225 | if (exists) { |
| 226 | icons[count] = new Fl_PNG_Image(icon_path); |
| 227 | if (icons[count]->w() == 0 || |
| 228 | icons[count]->h() == 0 || |
| 229 | icons[count]->d() != 4) { |
| 230 | delete icons[count]; |
| 231 | continue; |
| 232 | } |
| 233 | |
| 234 | count++; |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | Fl_Window::default_icons((const Fl_RGB_Image**)icons, count); |
| 239 | |
| 240 | for (int i = 0;i < count;i++) |
| 241 | delete icons[i]; |
| 242 | #endif |
Pierre Ossman | 8eb3508 | 2012-03-27 12:50:54 +0000 | [diff] [blame] | 243 | |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 244 | // This makes the "icon" in dialogs rounded, which fits better |
| 245 | // with the above schemes. |
| 246 | fl_message_icon()->box(FL_UP_BOX); |
| 247 | |
| 248 | // Turn off the annoying behaviour where popups track the mouse. |
| 249 | fl_message_hotspot(false); |
| 250 | |
| 251 | // Avoid empty titles for popups |
| 252 | fl_message_title_default(_("TigerVNC Viewer")); |
| 253 | |
| 254 | #ifdef WIN32 |
| 255 | // Most "normal" Windows apps use this font for UI elements. |
| 256 | Fl::set_font(FL_HELVETICA, "Tahoma"); |
| 257 | #endif |
| 258 | |
| 259 | // FLTK exposes these so that we can translate them. |
| 260 | fl_no = _("No"); |
| 261 | fl_yes = _("Yes"); |
| 262 | fl_ok = _("OK"); |
| 263 | fl_cancel = _("Cancel"); |
| 264 | fl_close = _("Close"); |
Pierre Ossman | b885822 | 2011-04-29 11:51:38 +0000 | [diff] [blame] | 265 | |
| 266 | #ifdef __APPLE__ |
Peter Åstrand | b7c5524 | 2011-08-29 13:14:51 +0000 | [diff] [blame] | 267 | /* Needs trailing space */ |
| 268 | static char fltk_about[16]; |
| 269 | snprintf(fltk_about, sizeof(fltk_about), "%s ", _("About")); |
| 270 | Fl_Mac_App_Menu::about = fltk_about; |
| 271 | static char fltk_hide[16]; |
| 272 | snprintf(fltk_hide, sizeof(fltk_hide), "%s ", _("Hide")); |
| 273 | Fl_Mac_App_Menu::hide = fltk_hide; |
| 274 | static char fltk_quit[16]; |
| 275 | snprintf(fltk_quit, sizeof(fltk_quit), "%s ", _("Quit")); |
| 276 | Fl_Mac_App_Menu::quit = fltk_quit; |
| 277 | |
Pierre Ossman | 41ba603 | 2011-06-16 11:09:31 +0000 | [diff] [blame] | 278 | Fl_Mac_App_Menu::print = ""; // Don't want the print item |
| 279 | Fl_Mac_App_Menu::services = _("Services"); |
Pierre Ossman | 41ba603 | 2011-06-16 11:09:31 +0000 | [diff] [blame] | 280 | Fl_Mac_App_Menu::hide_others = _("Hide Others"); |
| 281 | Fl_Mac_App_Menu::show = _("Show All"); |
Pierre Ossman | 41ba603 | 2011-06-16 11:09:31 +0000 | [diff] [blame] | 282 | |
Pierre Ossman | b885822 | 2011-04-29 11:51:38 +0000 | [diff] [blame] | 283 | fl_mac_set_about(about_callback, NULL); |
Pierre Ossman | a71a276 | 2015-03-04 09:57:06 +0100 | [diff] [blame] | 284 | |
| 285 | Fl_Sys_Menu_Bar *menubar; |
| 286 | char buffer[1024]; |
| 287 | menubar = new Fl_Sys_Menu_Bar(0, 0, 500, 25); |
| 288 | // Fl_Sys_Menu_Bar overrides methods without them being virtual, |
| 289 | // which means we cannot use our generic Fl_Menu_ helpers. |
| 290 | if (fltk_menu_escape(_("&File"), buffer, sizeof(buffer)) < sizeof(buffer)) |
| 291 | menubar->add(buffer, 0, 0, 0, FL_SUBMENU); |
| 292 | if (fltk_menu_escape(_("&New Connection"), buffer, sizeof(buffer)) < sizeof(buffer)) |
| 293 | menubar->insert(1, buffer, FL_COMMAND | 'n', new_connection_cb); |
Pierre Ossman | b885822 | 2011-04-29 11:51:38 +0000 | [diff] [blame] | 294 | #endif |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 295 | } |
| 296 | |
| 297 | static void mkvnchomedir() |
| 298 | { |
| 299 | // Create .vnc in the user's home directory if it doesn't already exist |
| 300 | char* homeDir = NULL; |
| 301 | |
| 302 | if (getvnchomedir(&homeDir) == -1) { |
| 303 | vlog.error(_("Could not create VNC home directory: can't obtain home " |
| 304 | "directory path.")); |
| 305 | } else { |
| 306 | int result = mkdir(homeDir, 0755); |
| 307 | if (result == -1 && errno != EEXIST) |
| 308 | vlog.error(_("Could not create VNC home directory: %s."), strerror(errno)); |
| 309 | delete [] homeDir; |
| 310 | } |
| 311 | } |
| 312 | |
| 313 | static void usage(const char *programName) |
| 314 | { |
Pierre Ossman | 5bf17ad | 2015-02-17 13:23:00 +0100 | [diff] [blame] | 315 | #ifdef WIN32 |
| 316 | // If we don't have a console then we need to create one for output |
| 317 | if (GetConsoleWindow() == NULL) { |
| 318 | HANDLE handle; |
| 319 | int fd; |
| 320 | |
| 321 | AllocConsole(); |
| 322 | |
| 323 | handle = GetStdHandle(STD_ERROR_HANDLE); |
| 324 | fd = _open_osfhandle((intptr_t)handle, O_TEXT); |
| 325 | *stderr = *fdopen(fd, "w"); |
| 326 | } |
| 327 | #endif |
| 328 | |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 329 | fprintf(stderr, |
| 330 | "\nusage: %s [parameters] [host:displayNum] [parameters]\n" |
| 331 | " %s [parameters] -listen [port] [parameters]\n", |
| 332 | programName, programName); |
| 333 | fprintf(stderr,"\n" |
| 334 | "Parameters can be turned on with -<param> or off with -<param>=0\n" |
| 335 | "Parameters which take a value can be specified as " |
| 336 | "-<param> <value>\n" |
| 337 | "Other valid forms are <param>=<value> -<param>=<value> " |
| 338 | "--<param>=<value>\n" |
| 339 | "Parameter names are case-insensitive. The parameters are:\n\n"); |
| 340 | Configuration::listParams(79, 14); |
Pierre Ossman | 5bf17ad | 2015-02-17 13:23:00 +0100 | [diff] [blame] | 341 | |
| 342 | #ifdef WIN32 |
| 343 | // Just wait for the user to kill the console window |
| 344 | Sleep(INFINITE); |
| 345 | #endif |
| 346 | |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 347 | exit(1); |
| 348 | } |
| 349 | |
Adam Tkac | 8ac4b30 | 2013-01-23 13:55:46 +0000 | [diff] [blame] | 350 | #ifndef WIN32 |
| 351 | static int |
| 352 | interpretViaParam(char *remoteHost, int *remotePort, int localPort) |
| 353 | { |
| 354 | const int SERVER_PORT_OFFSET = 5900; |
| 355 | char *pos = strchr(vncServerName, ':'); |
| 356 | if (pos == NULL) |
| 357 | *remotePort = SERVER_PORT_OFFSET; |
| 358 | else { |
| 359 | int portOffset = SERVER_PORT_OFFSET; |
| 360 | size_t len; |
| 361 | *pos++ = '\0'; |
| 362 | len = strlen(pos); |
| 363 | if (*pos == ':') { |
| 364 | /* Two colons is an absolute port number, not an offset. */ |
| 365 | pos++; |
| 366 | len--; |
| 367 | portOffset = 0; |
| 368 | } |
| 369 | if (!len || strspn (pos, "-0123456789") != len ) |
| 370 | return 1; |
| 371 | *remotePort = atoi(pos) + portOffset; |
| 372 | } |
| 373 | |
| 374 | if (*vncServerName != '\0') |
| 375 | strncpy(remoteHost, vncServerName, VNCSERVERNAMELEN); |
| 376 | else |
| 377 | strncpy(remoteHost, "localhost", VNCSERVERNAMELEN); |
| 378 | |
| 379 | remoteHost[VNCSERVERNAMELEN - 1] = '\0'; |
| 380 | |
| 381 | snprintf(vncServerName, VNCSERVERNAMELEN, "localhost::%d", localPort); |
| 382 | vncServerName[VNCSERVERNAMELEN - 1] = '\0'; |
Adam Tkac | 8ac4b30 | 2013-01-23 13:55:46 +0000 | [diff] [blame] | 383 | |
| 384 | return 0; |
| 385 | } |
| 386 | |
| 387 | static void |
| 388 | createTunnel(const char *gatewayHost, const char *remoteHost, |
| 389 | int remotePort, int localPort) |
| 390 | { |
Pierre Ossman | f8d525b | 2014-07-09 17:02:27 +0200 | [diff] [blame] | 391 | const char *cmd = getenv("VNC_VIA_CMD"); |
| 392 | char *cmd2, *percent; |
Adam Tkac | 8ac4b30 | 2013-01-23 13:55:46 +0000 | [diff] [blame] | 393 | char lport[10], rport[10]; |
| 394 | sprintf(lport, "%d", localPort); |
| 395 | sprintf(rport, "%d", remotePort); |
| 396 | setenv("G", gatewayHost, 1); |
| 397 | setenv("H", remoteHost, 1); |
| 398 | setenv("R", rport, 1); |
| 399 | setenv("L", lport, 1); |
Adam Tkac | 8ac4b30 | 2013-01-23 13:55:46 +0000 | [diff] [blame] | 400 | if (!cmd) |
| 401 | cmd = "/usr/bin/ssh -f -L \"$L\":\"$H\":\"$R\" \"$G\" sleep 20"; |
| 402 | /* Compatibility with TigerVNC's method. */ |
Pierre Ossman | f8d525b | 2014-07-09 17:02:27 +0200 | [diff] [blame] | 403 | cmd2 = strdup(cmd); |
| 404 | while ((percent = strchr(cmd2, '%')) != NULL) |
Adam Tkac | 8ac4b30 | 2013-01-23 13:55:46 +0000 | [diff] [blame] | 405 | *percent = '$'; |
Pierre Ossman | f8d525b | 2014-07-09 17:02:27 +0200 | [diff] [blame] | 406 | system(cmd2); |
| 407 | free(cmd2); |
Adam Tkac | 8ac4b30 | 2013-01-23 13:55:46 +0000 | [diff] [blame] | 408 | } |
| 409 | |
| 410 | static int mktunnel() |
| 411 | { |
| 412 | const char *gatewayHost; |
| 413 | char remoteHost[VNCSERVERNAMELEN]; |
| 414 | int localPort = findFreeTcpPort(); |
| 415 | int remotePort; |
| 416 | |
| 417 | gatewayHost = strDup(via.getValueStr()); |
| 418 | if (interpretViaParam(remoteHost, &remotePort, localPort) != 0) |
| 419 | return 1; |
| 420 | createTunnel(gatewayHost, remoteHost, remotePort, localPort); |
| 421 | |
| 422 | return 0; |
| 423 | } |
| 424 | #endif /* !WIN32 */ |
| 425 | |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 426 | int main(int argc, char** argv) |
| 427 | { |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 428 | UserDialog dlg; |
| 429 | |
Pierre Ossman | a71a276 | 2015-03-04 09:57:06 +0100 | [diff] [blame] | 430 | argv0 = argv[0]; |
| 431 | |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 432 | setlocale(LC_ALL, ""); |
Pierre Ossman | 0878eca | 2012-03-27 13:03:22 +0000 | [diff] [blame] | 433 | bindtextdomain(PACKAGE_NAME, LOCALE_DIR); |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 434 | textdomain(PACKAGE_NAME); |
| 435 | |
| 436 | rfb::SecurityClient::setDefaults(); |
| 437 | |
| 438 | // Write about text to console, still using normal locale codeset |
Pierre Ossman | ccd09ba | 2015-04-07 14:58:45 +0200 | [diff] [blame] | 439 | fprintf(stderr,"\n%s\n", about_text()); |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 440 | |
| 441 | // Set gettext codeset to what our GUI toolkit uses. Since we are |
| 442 | // passing strings from strerror/gai_strerror to the GUI, these must |
| 443 | // be in GUI codeset as well. |
| 444 | bind_textdomain_codeset(PACKAGE_NAME, "UTF-8"); |
| 445 | bind_textdomain_codeset("libc", "UTF-8"); |
| 446 | |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 447 | rfb::initStdIOLoggers(); |
Pierre Ossman | c8719ad | 2012-04-26 14:27:52 +0000 | [diff] [blame] | 448 | #ifdef WIN32 |
| 449 | rfb::initFileLogger("C:\\temp\\vncviewer.log"); |
| 450 | #else |
| 451 | rfb::initFileLogger("/tmp/vncviewer.log"); |
| 452 | #endif |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 453 | rfb::LogWriter::setLogParams("*:stderr:30"); |
| 454 | |
| 455 | #ifdef SIGHUP |
| 456 | signal(SIGHUP, CleanupSignalHandler); |
| 457 | #endif |
| 458 | signal(SIGINT, CleanupSignalHandler); |
| 459 | signal(SIGTERM, CleanupSignalHandler); |
| 460 | |
| 461 | init_fltk(); |
| 462 | |
Pierre Ossman | 6b9622d | 2014-07-21 16:42:12 +0200 | [diff] [blame] | 463 | #if !defined(WIN32) && !defined(__APPLE__) |
| 464 | fl_open_display(); |
| 465 | XkbSetDetectableAutoRepeat(fl_display, True, NULL); |
| 466 | #endif |
| 467 | |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 468 | Configuration::enableViewerParams(); |
| 469 | |
Peter Åstrand | 8a2b081 | 2012-08-08 11:49:01 +0000 | [diff] [blame] | 470 | /* Load the default parameter settings */ |
| 471 | const char* defaultServerName; |
| 472 | try { |
| 473 | defaultServerName = loadViewerParameters(NULL); |
| 474 | } catch (rfb::Exception& e) { |
Pierre Ossman | 3620e19 | 2015-03-03 16:50:15 +0100 | [diff] [blame] | 475 | defaultServerName = ""; |
Peter Åstrand | 8a2b081 | 2012-08-08 11:49:01 +0000 | [diff] [blame] | 476 | fl_alert("%s", e.str()); |
| 477 | } |
| 478 | |
DRC | 5aa0650 | 2011-06-23 22:04:46 +0000 | [diff] [blame] | 479 | int i = 1; |
| 480 | if (!Fl::args(argc, argv, i) || i < argc) |
| 481 | for (; i < argc; i++) { |
| 482 | if (Configuration::setParam(argv[i])) |
| 483 | continue; |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 484 | |
DRC | 5aa0650 | 2011-06-23 22:04:46 +0000 | [diff] [blame] | 485 | if (argv[i][0] == '-') { |
| 486 | if (i+1 < argc) { |
| 487 | if (Configuration::setParam(&argv[i][1], argv[i+1])) { |
| 488 | i++; |
| 489 | continue; |
| 490 | } |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 491 | } |
DRC | 5aa0650 | 2011-06-23 22:04:46 +0000 | [diff] [blame] | 492 | usage(argv[0]); |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 493 | } |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 494 | |
Adam Tkac | 8ac4b30 | 2013-01-23 13:55:46 +0000 | [diff] [blame] | 495 | strncpy(vncServerName, argv[i], VNCSERVERNAMELEN); |
| 496 | vncServerName[VNCSERVERNAMELEN - 1] = '\0'; |
DRC | 5aa0650 | 2011-06-23 22:04:46 +0000 | [diff] [blame] | 497 | } |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 498 | |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 499 | mkvnchomedir(); |
| 500 | |
| 501 | CSecurity::upg = &dlg; |
| 502 | #ifdef HAVE_GNUTLS |
| 503 | CSecurityTLS::msg = &dlg; |
| 504 | #endif |
| 505 | |
Pierre Ossman | 2a7a8d6 | 2013-02-15 08:33:39 +0000 | [diff] [blame] | 506 | Socket *sock = NULL; |
| 507 | |
DRC | 3e7ed81 | 2013-02-26 10:34:22 +0000 | [diff] [blame] | 508 | #ifndef WIN32 |
Adam Tkac | 571089b | 2013-02-19 14:30:32 +0000 | [diff] [blame] | 509 | /* Specifying -via and -listen together is nonsense */ |
| 510 | if (listenMode && strlen(via.getValueStr()) > 0) { |
Pierre Ossman | 744e55c | 2014-12-03 14:00:54 +0100 | [diff] [blame] | 511 | // TRANSLATORS: "Parameters" are command line arguments, or settings |
| 512 | // from a file or the Windows registry. |
Pierre Ossman | 8ca4c1d | 2014-09-22 12:54:26 +0200 | [diff] [blame] | 513 | vlog.error(_("Parameters -listen and -via are incompatible")); |
| 514 | fl_alert(_("Parameters -listen and -via are incompatible")); |
Adam Tkac | 571089b | 2013-02-19 14:30:32 +0000 | [diff] [blame] | 515 | exit_vncviewer(); |
| 516 | return 1; |
| 517 | } |
DRC | 3e7ed81 | 2013-02-26 10:34:22 +0000 | [diff] [blame] | 518 | #endif |
Adam Tkac | 571089b | 2013-02-19 14:30:32 +0000 | [diff] [blame] | 519 | |
| 520 | if (listenMode) { |
Tim Waugh | 892d10a | 2015-03-11 13:12:07 +0000 | [diff] [blame] | 521 | std::list<TcpListener> listeners; |
Pierre Ossman | 2a7a8d6 | 2013-02-15 08:33:39 +0000 | [diff] [blame] | 522 | try { |
| 523 | int port = 5500; |
| 524 | if (isdigit(vncServerName[0])) |
| 525 | port = atoi(vncServerName); |
| 526 | |
Tim Waugh | 892d10a | 2015-03-11 13:12:07 +0000 | [diff] [blame] | 527 | createTcpListeners(&listeners, 0, port); |
Pierre Ossman | 2a7a8d6 | 2013-02-15 08:33:39 +0000 | [diff] [blame] | 528 | |
Peter Åstrand (astrand) | 4ab2f8a | 2015-03-18 10:31:01 +0100 | [diff] [blame] | 529 | vlog.info(_("Listening on port %d"), port); |
Tim Waugh | 892d10a | 2015-03-11 13:12:07 +0000 | [diff] [blame] | 530 | |
| 531 | /* Wait for a connection */ |
| 532 | while (sock == NULL) { |
| 533 | fd_set rfds; |
| 534 | FD_ZERO(&rfds); |
| 535 | for (std::list<TcpListener>::iterator i = listeners.begin(); |
| 536 | i != listeners.end(); |
| 537 | i++) |
| 538 | FD_SET((*i).getFd(), &rfds); |
| 539 | |
| 540 | int n = select(FD_SETSIZE, &rfds, 0, 0, 0); |
| 541 | if (n < 0) { |
| 542 | if (errno == EINTR) { |
| 543 | vlog.debug("Interrupted select() system call"); |
| 544 | continue; |
| 545 | } else { |
| 546 | throw rdr::SystemException("select", errno); |
| 547 | } |
| 548 | } |
| 549 | |
| 550 | for (std::list<TcpListener>::iterator i = listeners.begin (); |
| 551 | i != listeners.end(); |
| 552 | i++) |
| 553 | if (FD_ISSET((*i).getFd(), &rfds)) { |
| 554 | sock = (*i).accept(); |
| 555 | if (sock) |
| 556 | /* Got a connection */ |
| 557 | break; |
| 558 | } |
| 559 | } |
Pierre Ossman | 2a7a8d6 | 2013-02-15 08:33:39 +0000 | [diff] [blame] | 560 | } catch (rdr::Exception& e) { |
| 561 | vlog.error("%s", e.str()); |
| 562 | fl_alert("%s", e.str()); |
| 563 | exit_vncviewer(); |
| 564 | return 1; |
| 565 | } |
| 566 | |
| 567 | } else { |
| 568 | if (vncServerName[0] == '\0') { |
| 569 | ServerDialog::run(defaultServerName, vncServerName); |
| 570 | if (vncServerName[0] == '\0') |
| 571 | return 1; |
| 572 | } |
Pierre Ossman | 561ff0c | 2011-05-13 14:04:59 +0000 | [diff] [blame] | 573 | |
Adam Tkac | 8ac4b30 | 2013-01-23 13:55:46 +0000 | [diff] [blame] | 574 | #ifndef WIN32 |
Pierre Ossman | 2a7a8d6 | 2013-02-15 08:33:39 +0000 | [diff] [blame] | 575 | if (strlen (via.getValueStr()) > 0 && mktunnel() != 0) |
| 576 | usage(argv[0]); |
Adam Tkac | 8ac4b30 | 2013-01-23 13:55:46 +0000 | [diff] [blame] | 577 | #endif |
Pierre Ossman | 2a7a8d6 | 2013-02-15 08:33:39 +0000 | [diff] [blame] | 578 | } |
Adam Tkac | 8ac4b30 | 2013-01-23 13:55:46 +0000 | [diff] [blame] | 579 | |
Pierre Ossman | 2a7a8d6 | 2013-02-15 08:33:39 +0000 | [diff] [blame] | 580 | CConn *cc = new CConn(vncServerName, sock); |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 581 | |
| 582 | while (!exitMainloop) { |
| 583 | int next_timer; |
| 584 | |
| 585 | next_timer = Timer::checkTimeouts(); |
| 586 | if (next_timer == 0) |
| 587 | next_timer = INT_MAX; |
| 588 | |
| 589 | if (Fl::wait((double)next_timer / 1000.0) < 0.0) { |
| 590 | vlog.error(_("Internal FLTK error. Exiting.")); |
| 591 | break; |
| 592 | } |
| 593 | } |
| 594 | |
Pierre Ossman | e2ef5c1 | 2011-07-12 16:56:34 +0000 | [diff] [blame] | 595 | delete cc; |
| 596 | |
| 597 | if (exitError != NULL) |
Pierre Ossman | f52740e | 2012-04-25 15:43:56 +0000 | [diff] [blame] | 598 | fl_alert("%s", exitError); |
Pierre Ossman | e2ef5c1 | 2011-07-12 16:56:34 +0000 | [diff] [blame] | 599 | |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 600 | return 0; |
| 601 | } |