2 tincd.c -- the main file for tincd
3 Copyright (C) 2014 Guus Sliepen <guus@meshlink.io>
5 This program 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.
10 This program 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.
15 You should have received a copy of the GNU General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 /* Darwin (MacOS/X) needs the following definition... */
23 #ifndef _P1003_1B_VISIBLE
24 #define _P1003_1B_VISIBLE
27 #ifdef HAVE_SYS_MMAN_H
48 /* If nonzero, display usage information and exit. */
49 static bool show_help = false;
51 /* If nonzero, print the version on standard output and exit. */
52 static bool show_version = false;
54 /* If nonzero, use null ciphers and skip all key exchanges. */
55 bool bypass_security = false;
57 char **g_argv; /* a copy of the cmdline arguments */
59 static int status = 1;
61 static struct option const long_options[] = {
62 {"config", required_argument, NULL, 'c'},
63 {"net", required_argument, NULL, 'n'},
64 {"help", no_argument, NULL, 1},
65 {"version", no_argument, NULL, 2},
66 {"no-detach", no_argument, NULL, 'D'},
67 {"debug", optional_argument, NULL, 'd'},
68 {"bypass-security", no_argument, NULL, 3},
69 {"option", required_argument, NULL, 'o'},
74 static struct WSAData wsa_state;
75 CRITICAL_SECTION mutex;
76 int main2(int argc, char **argv);
79 static void usage(bool status) {
81 fprintf(stderr, "Try `tincd --help\' for more information.\n");
83 printf("Usage: tincd [option]...\n\n");
84 printf( " -c, --config=DIR Read configuration options from DIR.\n"
85 " -D, --no-detach Don't fork and detach.\n"
86 " -d, --debug[=LEVEL] Increase debug level or set it to LEVEL.\n"
87 " -n, --net=NETNAME Connect to net NETNAME.\n"
88 " --bypass-security Disables meta protocol security, for debugging.\n"
89 " -o, --option[HOST.]KEY=VALUE Set global/host configuration value.\n"
90 " --help Display this help and exit.\n"
91 " --version Output version information and exit.\n\n");
92 printf("Report bugs to bugs@meshlink.io.\n");
96 static bool parse_options(int argc, char **argv) {
102 while((r = getopt_long(argc, argv, "c:DLd::n:o:RU:", long_options, &option_index)) != EOF) {
104 case 0: /* long option */
107 case 'c': /* config file */
108 confbase = xstrdup(optarg);
111 case 'd': /* increase debug level */
112 if(!optarg && optind < argc && *argv[optind] != '-')
113 optarg = argv[optind++];
115 debug_level = atoi(optarg);
120 case 1: /* show help */
124 case 2: /* show version */
128 case 3: /* bypass security */
129 bypass_security = true;
132 case '?': /* wrong options */
142 fprintf(stderr, "%s: unrecognized argument '%s'\n", argv[0], argv[optind]);
150 int old_main(int argc, char **argv) {
151 if(!parse_options(argc, argv))
155 printf("%s version %s (built %s %s, protocol %d.%d)\n", PACKAGE,
156 VERSION, __DATE__, __TIME__, PROT_MAJOR, PROT_MINOR);
157 printf("Copyright (C) 1998-2014 Ivo Timmermans, Guus Sliepen and others.\n"
158 "See the AUTHORS file for a complete list.\n\n"
159 "tinc comes with ABSOLUTELY NO WARRANTY. This is free software,\n"
160 "and you are welcome to redistribute it under certain conditions;\n"
161 "see the file COPYING for details.\n");
172 if(WSAStartup(MAKEWORD(2, 2), &wsa_state)) {
173 logger(DEBUG_ALWAYS, LOG_ERR, "System call `%s' failed: %s", "WSAStartup", winerror(GetLastError()));
178 openlogger("tinc", LOGMODE_STDERR);
182 init_configuration(&config_tree);
184 /* Slllluuuuuuurrrrp! */
186 gettimeofday(&now, NULL);
187 srand(now.tv_sec + now.tv_usec);
190 if(!read_server_config())
193 char *priority = NULL;
200 /* Start main loop. It only exits when tinc is killed. */
202 logger(DEBUG_ALWAYS, LOG_NOTICE, "Ready");
204 try_outgoing_connections();
206 status = main_loop();
208 /* Shutdown properly. */
211 close_network_connections();
213 logger(DEBUG_ALWAYS, LOG_NOTICE, "Terminating");
219 exit_configuration(&config_tree);