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 char **g_argv; /* a copy of the cmdline arguments */
56 static int status = 1;
58 static struct option const long_options[] = {
59 {"config", required_argument, NULL, 'c'},
60 {"net", required_argument, NULL, 'n'},
61 {"help", no_argument, NULL, 1},
62 {"version", no_argument, NULL, 2},
63 {"no-detach", no_argument, NULL, 'D'},
64 {"debug", optional_argument, NULL, 'd'},
65 {"bypass-security", no_argument, NULL, 3},
66 {"option", required_argument, NULL, 'o'},
71 static struct WSAData wsa_state;
72 CRITICAL_SECTION mutex;
73 int main2(int argc, char **argv);
76 static void usage(bool status) {
78 fprintf(stderr, "Try `tincd --help\' for more information.\n");
80 printf("Usage: tincd [option]...\n\n");
81 printf( " -c, --config=DIR Read configuration options from DIR.\n"
82 " -D, --no-detach Don't fork and detach.\n"
83 " -d, --debug[=LEVEL] Increase debug level or set it to LEVEL.\n"
84 " -n, --net=NETNAME Connect to net NETNAME.\n"
85 " --bypass-security Disables meta protocol security, for debugging.\n"
86 " -o, --option[HOST.]KEY=VALUE Set global/host configuration value.\n"
87 " --help Display this help and exit.\n"
88 " --version Output version information and exit.\n\n");
89 printf("Report bugs to bugs@meshlink.io.\n");
93 static bool parse_options(int argc, char **argv) {
99 while((r = getopt_long(argc, argv, "c:DLd::n:o:RU:", long_options, &option_index)) != EOF) {
101 case 0: /* long option */
104 case 'c': /* config file */
105 confbase = xstrdup(optarg);
108 case 'd': /* increase debug level */
109 if(!optarg && optind < argc && *argv[optind] != '-')
110 optarg = argv[optind++];
112 mesh->debug_level = atoi(optarg);
117 case 1: /* show help */
121 case 2: /* show version */
125 case 3: /* bypass security */
126 bypass_security = true;
129 case '?': /* wrong options */
139 fprintf(stderr, "%s: unrecognized argument '%s'\n", argv[0], argv[optind]);
147 int old_main(int argc, char **argv) {
148 if(!parse_options(argc, argv))
152 printf("%s version %s (built %s %s, protocol %d.%d)\n", PACKAGE,
153 VERSION, __DATE__, __TIME__, PROT_MAJOR, PROT_MINOR);
154 printf("Copyright (C) 1998-2014 Ivo Timmermans, Guus Sliepen and others.\n"
155 "See the AUTHORS file for a complete list.\n\n"
156 "tinc comes with ABSOLUTELY NO WARRANTY. This is free software,\n"
157 "and you are welcome to redistribute it under certain conditions;\n"
158 "see the file COPYING for details.\n");
169 if(WSAStartup(MAKEWORD(2, 2), &wsa_state)) {
170 logger(DEBUG_ALWAYS, LOG_ERR, "System call `%s' failed: %s", "WSAStartup", winerror(GetLastError()));
175 openlogger("tinc", LOGMODE_STDERR);
179 init_configuration(&config_tree);
181 /* Slllluuuuuuurrrrp! */
183 gettimeofday(&now, NULL);
184 srand(now.tv_sec + now.tv_usec);
187 if(!read_server_config())
190 char *priority = NULL;
197 /* Start main loop. It only exits when tinc is killed. */
199 logger(DEBUG_ALWAYS, LOG_NOTICE, "Ready");
201 try_outgoing_connections();
203 status = main_loop();
205 /* Shutdown properly. */
208 close_network_connections();
210 logger(DEBUG_ALWAYS, LOG_NOTICE, "Terminating");
216 exit_configuration(&config_tree);