+#ifdef HAVE_MINGW
+ HKEY key;
+ char installdir[1024] = "";
+ long len = sizeof(installdir);
+#endif
+
+ if(netname)
+ asprintf(&identname, "tinc.%s", netname);
+ else
+ identname = xstrdup("tinc");
+
+#ifdef HAVE_MINGW
+ if(!RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SOFTWARE\\tinc", 0, KEY_READ, &key)) {
+ if(!RegQueryValueEx(key, NULL, 0, 0, installdir, &len)) {
+ if(!logfilename)
+ asprintf(&logfilename, "%s/log/%s.log", identname);
+ if(!confbase) {
+ if(netname)
+ asprintf(&confbase, "%s/%s", installdir, netname);
+ else
+ asprintf(&confbase, "%s", installdir);
+ }
+ }
+ RegCloseKey(key);
+ if(*installdir)
+ return;
+ }
+#endif
+
+ if(!pidfilename)
+ asprintf(&pidfilename, LOCALSTATEDIR "/run/%s.pid", identname);
+
+ if(!logfilename)
+ asprintf(&logfilename, LOCALSTATEDIR "/log/%s.log", identname);
+
+ if(netname) {
+ if(!confbase)
+ asprintf(&confbase, CONFDIR "/tinc/%s", netname);
+ else
+ logger(LOG_INFO, _("Both netname and configuration directory given, using the latter..."));
+ } else {
+ if(!confbase)
+ asprintf(&confbase, CONFDIR "/tinc");
+ }
+}
+
+int main(int argc, char **argv)
+{
+ program_name = argv[0];
+
+ setlocale(LC_ALL, "");
+ bindtextdomain(PACKAGE, LOCALEDIR);
+ textdomain(PACKAGE);
+
+ if(!parse_options(argc, argv))
+ return 1;
+
+ make_names();
+
+ if(show_version) {
+ printf(_("%s version %s (built %s %s, protocol %d)\n"), PACKAGE,
+ VERSION, __DATE__, __TIME__, PROT_CURRENT);
+ printf(_("Copyright (C) 1998-2003 Ivo Timmermans, Guus Sliepen and others.\n"
+ "See the AUTHORS file for a complete list.\n\n"
+ "tinc comes with ABSOLUTELY NO WARRANTY. This is free software,\n"
+ "and you are welcome to redistribute it under certain conditions;\n"
+ "see the file COPYING for details.\n"));
+
+ return 0;
+ }
+
+ if(show_help) {
+ usage(false);
+ return 0;
+ }
+
+ if(kill_tincd)
+ return !kill_other(kill_tincd);
+
+ openlogger("tinc", use_logfile?LOGMODE_FILE:LOGMODE_STDERR);
+
+ /* Lock all pages into memory if requested */
+
+ if(do_mlock)
+#ifdef HAVE_MLOCKALL
+ if(mlockall(MCL_CURRENT | MCL_FUTURE)) {
+ logger(LOG_ERR, _("System call `%s' failed: %s"), "mlockall",
+ strerror(errno));
+#else
+ {
+ logger(LOG_ERR, _("mlockall() not supported on this platform!"));
+#endif
+ return -1;
+ }
+
+ g_argv = argv;
+
+ init_configuration(&config_tree);
+
+ /* Slllluuuuuuurrrrp! */
+
+ RAND_load_file("/dev/urandom", 1024);
+
+ OpenSSL_add_all_algorithms();
+
+ if(generate_keys) {
+ read_server_config();
+ return !keygen(generate_keys);
+ }
+
+ if(!read_server_config())
+ return 1;
+
+ if(lzo_init() != LZO_E_OK) {
+ logger(LOG_ERR, _("Error initializing LZO compressor!"));
+ return 1;
+ }
+
+#ifdef HAVE_MINGW
+ if(WSAStartup(MAKEWORD(2, 2), &wsa_state)) {
+ logger(LOG_ERR, _("System call `%s' failed: %s"), "WSAStartup", winerror(GetLastError()));
+ return 1;
+ }
+
+ if(!do_detach || !init_service())
+ return main2(argc, argv);
+ else
+ return 1;