ftbfs
[oweals/gnunet.git] / src / nat-auto / gnunet-nat-auto.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2015, 2016, 2017 GNUnet e.V.
4
5      GNUnet is free software: you can redistribute it and/or modify it
6      under the terms of the GNU Affero General Public License as published
7      by the Free Software Foundation, either version 3 of the License,
8      or (at your option) any later version.
9
10      GNUnet is distributed in the hope that it will be useful, but
11      WITHOUT ANY WARRANTY; without even the implied warranty of
12      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13      Affero General Public License for more details.
14     
15      You should have received a copy of the GNU Affero General Public License
16      along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
18      SPDX-License-Identifier: AGPL3.0-or-later
19 */
20
21 /**
22  * @file src/nat/gnunet-nat-auto.c
23  * @brief Command-line tool for testing and autoconfiguration of NAT traversal
24  * @author Christian Grothoff
25  * @author Bruno Cabral
26  */
27 #include "platform.h"
28 #include "gnunet_util_lib.h"
29 #include "gnunet_nat_service.h"
30 #include "gnunet_nat_auto_service.h"
31
32 /**
33  * Value to return from #main().
34  */
35 static int global_ret;
36
37 /**
38  * Handle to ongoing autoconfiguration.
39  */
40 static struct GNUNET_NAT_AUTO_AutoHandle *ah;
41
42 /**
43  * If we do auto-configuration, should we write the result
44  * to a file?
45  */
46 static int write_cfg;
47
48 /**
49  * Configuration filename.
50  */
51 static const char *cfg_file;
52
53 /**
54  * Original configuration.
55  */
56 static const struct GNUNET_CONFIGURATION_Handle *cfg;
57
58 /**
59  * Adapter we are supposed to test.
60  */
61 static char *section_name;
62
63 /**
64  * Should we run autoconfiguration?
65  */
66 static int do_auto;
67
68 /**
69  * Handle to a NAT test operation.
70  */
71 static struct GNUNET_NAT_AUTO_Test *nt;
72
73 /**
74  * Flag set to 1 if we use IPPROTO_UDP.
75  */
76 static int use_udp;
77
78 /**
79  * Flag set to 1 if we use IPPROTO_TCP.
80  */
81 static int use_tcp;
82
83 /**
84  * Protocol to use.
85  */
86 static uint8_t proto;
87
88 /**
89  * Test if all activities have finished, and if so,
90  * terminate.
91  */
92 static void
93 test_finished ()
94 {
95   if (NULL != ah)
96     return;
97   if (NULL != nt)
98     return;
99   GNUNET_SCHEDULER_shutdown ();
100 }
101
102
103 /**
104  * Function to iterate over sugested changes options
105  *
106  * @param cls closure
107  * @param section name of the section
108  * @param option name of the option
109  * @param value value of the option
110  */
111 static void
112 auto_conf_iter (void *cls,
113                 const char *section,
114                 const char *option,
115                 const char *value)
116 {
117   struct GNUNET_CONFIGURATION_Handle *new_cfg = cls;
118
119   PRINTF ("%s: %s\n",
120           option,
121           value);
122   if (NULL != new_cfg)
123     GNUNET_CONFIGURATION_set_value_string (new_cfg,
124                                            section,
125                                            option,
126                                            value);
127 }
128
129
130 /**
131  * Function called with the result from the autoconfiguration.
132  *
133  * @param cls closure
134  * @param diff minimal suggested changes to the original configuration
135  *             to make it work (as best as we can)
136  * @param result #GNUNET_NAT_ERROR_SUCCESS on success, otherwise the specific error code
137  * @param type what the situation of the NAT
138  */
139 static void
140 auto_config_cb (void *cls,
141                 const struct GNUNET_CONFIGURATION_Handle *diff,
142                 enum GNUNET_NAT_StatusCode result,
143                 enum GNUNET_NAT_Type type)
144 {
145   const char *nat_type;
146   char unknown_type[64];
147   struct GNUNET_CONFIGURATION_Handle *new_cfg;
148
149   ah = NULL;
150   switch (type)
151   {
152   case GNUNET_NAT_TYPE_NO_NAT:
153     nat_type = "NO NAT";
154     break;
155   case GNUNET_NAT_TYPE_UNREACHABLE_NAT:
156     nat_type = "NAT but we can traverse";
157     break;
158   case GNUNET_NAT_TYPE_STUN_PUNCHED_NAT:
159     nat_type = "NAT but STUN is able to identify the correct information";
160     break;
161   case GNUNET_NAT_TYPE_UPNP_NAT:
162     nat_type = "NAT but UPNP opened the ports";
163     break;
164   default:
165     SPRINTF (unknown_type,
166              "NAT unknown, type %u",
167              type);
168     nat_type = unknown_type;
169     break;
170   }
171
172   GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
173               "NAT status: %s/%s\n",
174               GNUNET_NAT_AUTO_status2string (result),
175               nat_type);
176
177   if (NULL == diff)
178     return;
179
180   /* Shortcut: if there are no changes suggested, bail out early. */
181   if (GNUNET_NO ==
182       GNUNET_CONFIGURATION_is_dirty (diff))
183   {
184     test_finished ();
185     return;
186   }
187
188   /* Apply diff to original configuration and show changes
189      to the user */
190   new_cfg = write_cfg ? GNUNET_CONFIGURATION_dup (cfg) : NULL;
191
192   GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
193               _("Suggested configuration changes:\n"));
194   GNUNET_CONFIGURATION_iterate_section_values (diff,
195                                                "nat",
196                                                &auto_conf_iter,
197                                                new_cfg);
198
199   /* If desired, write configuration to file; we write only the
200      changes to the defaults to keep things compact. */
201   if (write_cfg)
202   {
203     struct GNUNET_CONFIGURATION_Handle *def_cfg;
204
205     GNUNET_CONFIGURATION_set_value_string (new_cfg,
206                                            "ARM",
207                                            "CONFIG",
208                                            NULL);
209     def_cfg = GNUNET_CONFIGURATION_create ();
210     GNUNET_break (GNUNET_OK ==
211                   GNUNET_CONFIGURATION_load (def_cfg,
212                                              NULL));
213     if (GNUNET_OK !=
214         GNUNET_CONFIGURATION_write_diffs (def_cfg,
215                                           new_cfg,
216                                           cfg_file))
217     {
218       GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
219                   _("Failed to write configuration to `%s'\n"),
220                   cfg_file);
221       global_ret = 1;
222     }
223     else
224     {
225       GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
226                   _("Wrote updated configuration to `%s'\n"),
227                   cfg_file);
228     }
229     GNUNET_CONFIGURATION_destroy (def_cfg);
230   }
231
232   if (NULL != new_cfg)
233     GNUNET_CONFIGURATION_destroy (new_cfg);
234   test_finished ();
235 }
236
237
238 /**
239  * Function called to report success or failure for
240  * NAT configuration test.
241  *
242  * @param cls closure
243  * @param result #GNUNET_NAT_ERROR_SUCCESS on success, otherwise the specific error code
244  */
245 static void
246 test_report_cb (void *cls,
247                 enum GNUNET_NAT_StatusCode result)
248 {
249   nt = NULL;
250   PRINTF ("NAT test result: %s\n",
251           GNUNET_NAT_AUTO_status2string (result));
252   test_finished ();
253 }
254
255
256 /**
257  * Task run on shutdown.
258  *
259  * @param cls NULL
260  */
261 static void
262 do_shutdown (void *cls)
263 {
264   if (NULL != ah)
265   {
266     GNUNET_NAT_AUTO_autoconfig_cancel (ah);
267     ah = NULL;
268   }
269   if (NULL != nt)
270   {
271     GNUNET_NAT_AUTO_test_stop (nt);
272     nt = NULL;
273   }
274 }
275
276
277 /**
278  * Main function that will be run.
279  *
280  * @param cls closure
281  * @param args remaining command-line arguments
282  * @param cfgfile name of the configuration file used (for saving, can be NULL!)
283  * @param c configuration
284  */
285 static void
286 run (void *cls,
287      char *const *args,
288      const char *cfgfile,
289      const struct GNUNET_CONFIGURATION_Handle *c)
290 {
291   cfg_file = cfgfile;
292   cfg = c;
293
294   GNUNET_SCHEDULER_add_shutdown (&do_shutdown,
295                                  NULL);
296
297   if (do_auto)
298   {
299     ah = GNUNET_NAT_AUTO_autoconfig_start (c,
300                                            &auto_config_cb,
301                                            NULL);
302   }
303
304   if (use_tcp && use_udp)
305   {
306     if (do_auto)
307       return;
308     GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
309                 "Cannot use TCP and UDP\n");
310     global_ret = 1;
311     return;
312   }
313   proto = 0;
314   if (use_tcp)
315     proto = IPPROTO_TCP;
316   if (use_udp)
317     proto = IPPROTO_UDP;
318
319   if (NULL != section_name)
320   {
321     nt = GNUNET_NAT_AUTO_test_start (c,
322                                      proto,
323                                      section_name,
324                                      &test_report_cb,
325                                      NULL);
326   }
327   test_finished ();
328 }
329
330
331 /**
332  * Main function of gnunet-nat-auto
333  *
334  * @param argc number of command-line arguments
335  * @param argv command line
336  * @return 0 on success, -1 on error
337  */
338 int
339 main (int argc,
340       char *const argv[])
341 {
342   struct GNUNET_GETOPT_CommandLineOption options[] = {
343     GNUNET_GETOPT_option_flag ('a',
344                                   "auto",
345                                   gettext_noop ("run autoconfiguration"),
346                                   &do_auto),
347
348     GNUNET_GETOPT_option_string ('S',
349                                  "section",
350                                  "NAME",
351                                  gettext_noop ("section name providing the configuration for the adapter"),
352                                  &section_name),
353
354     GNUNET_GETOPT_option_flag ('t',
355                                    "tcp",
356                                    gettext_noop ("use TCP"),
357                                    &use_tcp),
358
359     GNUNET_GETOPT_option_flag ('u',
360                                    "udp",
361                                    gettext_noop ("use UDP"),
362                                    &use_udp),
363
364     GNUNET_GETOPT_option_flag ('w',
365                                    "write",
366                                    gettext_noop ("write configuration file (for autoconfiguration)"),
367                                    &write_cfg),
368     GNUNET_GETOPT_OPTION_END
369   };
370
371   if (GNUNET_OK !=
372       GNUNET_STRINGS_get_utf8_args (argc, argv,
373                                     &argc, &argv))
374     return 2;
375   if (GNUNET_OK !=
376       GNUNET_PROGRAM_run (argc, argv,
377                           "gnunet-nat-auto [options]",
378                           _("GNUnet NAT traversal autoconfiguration"),
379                           options,
380                           &run,
381                           NULL))
382   {
383     global_ret = 1;
384   }
385   GNUNET_free ((void*) argv);
386   return global_ret;
387 }
388
389
390 /* end of gnunet-nat-auto.c */