]> git.meshlink.io Git - meshlink/blob - src/tincd.c
Remove everything GPL that is not copyright Guus Sliepen, update copyright statements.
[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 /* If nonzero, use null ciphers and skip all key exchanges. */
55 bool bypass_security = false;
56
57 char **g_argv;                  /* a copy of the cmdline arguments */
58
59 static int status = 1;
60
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'},
70         {NULL, 0, NULL, 0}
71 };
72
73 #ifdef HAVE_MINGW
74 static struct WSAData wsa_state;
75 CRITICAL_SECTION mutex;
76 int main2(int argc, char **argv);
77 #endif
78
79 static void usage(bool status) {
80         if(status)
81                 fprintf(stderr, "Try `tincd --help\' for more information.\n");
82         else {
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");
93         }
94 }
95
96 static bool parse_options(int argc, char **argv) {
97         config_t *cfg;
98         int r;
99         int option_index = 0;
100         int lineno = 0;
101
102         while((r = getopt_long(argc, argv, "c:DLd::n:o:RU:", long_options, &option_index)) != EOF) {
103                 switch (r) {
104                         case 0:   /* long option */
105                                 break;
106
107                         case 'c': /* config file */
108                                 confbase = xstrdup(optarg);
109                                 break;
110
111                         case 'd': /* increase debug level */
112                                 if(!optarg && optind < argc && *argv[optind] != '-')
113                                         optarg = argv[optind++];
114                                 if(optarg)
115                                         debug_level = atoi(optarg);
116                                 else
117                                         debug_level++;
118                                 break;
119
120                         case 1:   /* show help */
121                                 show_help = true;
122                                 break;
123
124                         case 2:   /* show version */
125                                 show_version = true;
126                                 break;
127
128                         case 3:   /* bypass security */
129                                 bypass_security = true;
130                                 break;
131
132                         case '?': /* wrong options */
133                                 usage(true);
134                                 return false;
135
136                         default:
137                                 break;
138                 }
139         }
140
141         if(optind < argc) {
142                 fprintf(stderr, "%s: unrecognized argument '%s'\n", argv[0], argv[optind]);
143                 usage(true);
144                 return false;
145         }
146
147         return true;
148 }
149
150 int main(int argc, char **argv) {
151         if(!parse_options(argc, argv))
152                 return 1;
153
154         if(show_version) {
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");
162
163                 return 0;
164         }
165
166         if(show_help) {
167                 usage(false);
168                 return 0;
169         }
170
171 #ifdef HAVE_MINGW
172         if(WSAStartup(MAKEWORD(2, 2), &wsa_state)) {
173                 logger(DEBUG_ALWAYS, LOG_ERR, "System call `%s' failed: %s", "WSAStartup", winerror(GetLastError()));
174                 return 1;
175         }
176 #endif
177
178         openlogger("tinc", LOGMODE_STDERR);
179
180         g_argv = argv;
181
182         init_configuration(&config_tree);
183
184         /* Slllluuuuuuurrrrp! */
185
186         gettimeofday(&now, NULL);
187         srand(now.tv_sec + now.tv_usec);
188         crypto_init();
189
190         if(!read_server_config())
191                 return 1;
192
193         char *priority = NULL;
194
195         /* Setup sockets. */
196
197         if(!setup_network())
198                 goto end;
199
200         /* Start main loop. It only exits when tinc is killed. */
201
202         logger(DEBUG_ALWAYS, LOG_NOTICE, "Ready");
203
204         try_outgoing_connections();
205
206         status = main_loop();
207
208         /* Shutdown properly. */
209
210 end:
211         close_network_connections();
212
213         logger(DEBUG_ALWAYS, LOG_NOTICE, "Terminating");
214
215         free(priority);
216
217         crypto_exit();
218
219         exit_configuration(&config_tree);
220
221         return status;
222 }