]> git.meshlink.io Git - meshlink/blob - src/tincd.c
45ff3ff697e51f21059534e271f4969e41b1954b
[meshlink] / src / tincd.c
1 /*
2     tincd.c -- the main file for tincd
3     Copyright (C) 2014 Guus Sliepen <guus@meshlink.io>
4
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.
9
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.
14
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.
18 */
19
20 #include "system.h"
21
22 /* Darwin (MacOS/X) needs the following definition... */
23 #ifndef _P1003_1B_VISIBLE
24 #define _P1003_1B_VISIBLE
25 #endif
26
27 #ifdef HAVE_SYS_MMAN_H
28 #include <sys/mman.h>
29 #endif
30
31 #ifndef HAVE_MINGW
32 #include <pwd.h>
33 #include <grp.h>
34 #include <time.h>
35 #endif
36
37 #include <getopt.h>
38
39 #include "conf.h"
40 #include "crypto.h"
41 #include "logger.h"
42 #include "net.h"
43 #include "netutl.h"
44 #include "protocol.h"
45 #include "utils.h"
46 #include "xalloc.h"
47
48 /* If nonzero, display usage information and exit. */
49 static bool show_help = false;
50
51 /* If nonzero, print the version on standard output and exit.  */
52 static bool show_version = false;
53
54 char **g_argv;                  /* a copy of the cmdline arguments */
55
56 static int status = 1;
57
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'},
67         {NULL, 0, NULL, 0}
68 };
69
70 #ifdef HAVE_MINGW
71 static struct WSAData wsa_state;
72 CRITICAL_SECTION mutex;
73 int main2(int argc, char **argv);
74 #endif
75
76 static void usage(bool status) {
77         if(status)
78                 fprintf(stderr, "Try `tincd --help\' for more information.\n");
79         else {
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");
90         }
91 }
92
93 static bool parse_options(int argc, char **argv) {
94         config_t *cfg;
95         int r;
96         int option_index = 0;
97         int lineno = 0;
98
99         while((r = getopt_long(argc, argv, "c:DLd::n:o:RU:", long_options, &option_index)) != EOF) {
100                 switch (r) {
101                         case 0:   /* long option */
102                                 break;
103
104                         case 'c': /* config file */
105                                 confbase = xstrdup(optarg);
106                                 break;
107
108                         case 'd': /* increase debug level */
109                                 if(!optarg && optind < argc && *argv[optind] != '-')
110                                         optarg = argv[optind++];
111                                 if(optarg)
112                                         mesh->debug_level = atoi(optarg);
113                                 else
114                                         mesh->debug_level++;
115                                 break;
116
117                         case 1:   /* show help */
118                                 show_help = true;
119                                 break;
120
121                         case 2:   /* show version */
122                                 show_version = true;
123                                 break;
124
125                         case 3:   /* bypass security */
126                                 bypass_security = true;
127                                 break;
128
129                         case '?': /* wrong options */
130                                 usage(true);
131                                 return false;
132
133                         default:
134                                 break;
135                 }
136         }
137
138         if(optind < argc) {
139                 fprintf(stderr, "%s: unrecognized argument '%s'\n", argv[0], argv[optind]);
140                 usage(true);
141                 return false;
142         }
143
144         return true;
145 }
146
147 int old_main(int argc, char **argv) {
148         if(!parse_options(argc, argv))
149                 return 1;
150
151         if(show_version) {
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");
159
160                 return 0;
161         }
162
163         if(show_help) {
164                 usage(false);
165                 return 0;
166         }
167
168 #ifdef HAVE_MINGW
169         if(WSAStartup(MAKEWORD(2, 2), &wsa_state)) {
170                 logger(DEBUG_ALWAYS, LOG_ERR, "System call `%s' failed: %s", "WSAStartup", winerror(GetLastError()));
171                 return 1;
172         }
173 #endif
174
175         openlogger("tinc", LOGMODE_STDERR);
176
177         g_argv = argv;
178
179         init_configuration(&config_tree);
180
181         /* Slllluuuuuuurrrrp! */
182
183         gettimeofday(&now, NULL);
184         srand(now.tv_sec + now.tv_usec);
185         crypto_init();
186
187         if(!read_server_config())
188                 return 1;
189
190         char *priority = NULL;
191
192         /* Setup sockets. */
193
194         if(!setup_network())
195                 goto end;
196
197         /* Start main loop. It only exits when tinc is killed. */
198
199         logger(DEBUG_ALWAYS, LOG_NOTICE, "Ready");
200
201         try_outgoing_connections();
202
203         status = main_loop();
204
205         /* Shutdown properly. */
206
207 end:
208         close_network_connections();
209
210         logger(DEBUG_ALWAYS, LOG_NOTICE, "Terminating");
211
212         free(priority);
213
214         crypto_exit();
215
216         exit_configuration(&config_tree);
217
218         return status;
219 }