Merge remote-tracking branch 'origin/master' into credentials
[oweals/gnunet.git] / src / nat / gnunet-nat.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2015, 2016 GNUnet e.V.
4
5      GNUnet is free software; you can redistribute it and/or modify
6      it under the terms of the GNU General Public License as published
7      by the Free Software Foundation; either version 3, or (at your
8      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      General Public License for more details.
14
15      You should have received a copy of the GNU General Public License
16      along with GNUnet; see the file COPYING.  If not, write to the
17      Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18      Boston, MA 02110-1301, USA.
19 */
20
21 /**
22  * @file src/nat/gnunet-nat.c
23  * @brief Command-line tool to interact with the NAT service
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
31 /**
32  * Value to return from #main().
33  */
34 static int global_ret;
35
36 /**
37  * Handle to ongoing autoconfiguration.
38  */
39 static struct GNUNET_NAT_AutoHandle *ah;
40
41 /**
42  * Port we advertise.
43  */ 
44 static unsigned int adv_port;
45
46 /**
47  * Flag set to 1 if we use IPPROTO_UDP.
48  */
49 static int use_udp;
50
51 /**
52  * Flag set to 1 if we are to listen for connection reversal requests.
53  */
54 static int listen_reversal;
55
56 /**
57  * Flag set to 1 if we use IPPROTO_TCP.
58  */
59 static int use_tcp;
60
61 /**
62  * Protocol to use.
63  */
64 static uint8_t proto;
65
66 /**
67  * Address we are bound to (in test), or should bind to
68  * (if #do_stun is set).
69  */
70 static char *bind_addr;
71
72 /**
73  * External IP address and port to use for the test.
74  * If not set, use #bind_addr.
75  */
76 static char *extern_addr;
77
78 /**
79  * Local address to use for connection reversal request.
80  */
81 static char *local_addr;
82
83 /**
84  * Remote address to use for connection reversal request.
85  */
86 static char *remote_addr;
87
88 /**
89  * Should we actually bind to #bind_addr and receive and process STUN requests?
90  */
91 static unsigned int do_stun;
92
93 /**
94  * Should we run autoconfiguration?
95  */
96 static unsigned int do_auto;
97
98 /**
99  * Handle to a NAT test operation.
100  */
101 static struct GNUNET_NAT_Test *nt;
102
103 /**
104  * Handle to NAT operation.
105  */
106 static struct GNUNET_NAT_Handle *nh;
107
108
109 /**
110  * Test if all activities have finished, and if so,
111  * terminate.
112  */
113 static void
114 test_finished ()
115 {
116   if (NULL != ah)
117     return;
118   if (NULL != nt)
119     return;
120   if (NULL != nh)
121     return;
122   GNUNET_SCHEDULER_shutdown ();
123 }
124
125
126 /**
127  * Function to iterate over sugested changes options
128  *
129  * @param cls closure
130  * @param section name of the section
131  * @param option name of the option
132  * @param value value of the option
133  */
134 static void
135 auto_conf_iter (void *cls,
136                 const char *section,
137                 const char *option,
138                 const char *value)
139 {
140   PRINTF ("%s: %s\n",
141           option,
142           value);
143 }
144
145
146 /**
147  * Function called with the result from the autoconfiguration.
148  *
149  * @param cls closure
150  * @param diff minimal suggested changes to the original configuration
151  *             to make it work (as best as we can)
152  * @param result #GNUNET_NAT_ERROR_SUCCESS on success, otherwise the specific error code
153  * @param type what the situation of the NAT
154  */
155 static void
156 auto_config_cb (void *cls,
157                 const struct GNUNET_CONFIGURATION_Handle *diff,
158                 enum GNUNET_NAT_StatusCode result,
159                 enum GNUNET_NAT_Type type)
160 {
161   const char *nat_type;
162   char unknown_type[64];
163
164   ah = NULL;
165   switch (type)
166   {
167     case GNUNET_NAT_TYPE_NO_NAT:
168       nat_type = "NO NAT";
169       break;
170     case GNUNET_NAT_TYPE_UNREACHABLE_NAT:
171       nat_type = "NAT but we can traverse";
172       break;
173     case GNUNET_NAT_TYPE_STUN_PUNCHED_NAT:
174       nat_type = "NAT but STUN is able to identify the correct information";
175       break;
176     case GNUNET_NAT_TYPE_UPNP_NAT:
177       nat_type = "NAT but UPNP opened the ports";
178       break;
179     default:
180       SPRINTF (unknown_type,
181                "NAT unknown, type %u",
182                type);
183       nat_type = unknown_type;
184   }
185
186   PRINTF ("NAT status: %s/%s\n",
187           GNUNET_NAT_status2string (result),
188           nat_type);
189   
190   PRINTF ("SUGGESTED CHANGES:\n");
191   GNUNET_CONFIGURATION_iterate_section_values (diff,
192                                                "nat",
193                                                &auto_conf_iter,
194                                                NULL);
195   // FIXME: have option to save config
196   test_finished ();
197 }
198
199
200 /**
201  * Function called to report success or failure for
202  * NAT configuration test.
203  *
204  * @param cls closure
205  * @param result #GNUNET_NAT_ERROR_SUCCESS on success, otherwise the specific error code
206  */
207 static void
208 test_report_cb (void *cls,
209                 enum GNUNET_NAT_StatusCode result)
210 {
211   nt = NULL;
212   PRINTF ("NAT test result: %s\n",
213           GNUNET_NAT_status2string (result));
214   test_finished ();
215 }
216
217
218 /**
219  * Signature of the callback passed to #GNUNET_NAT_register() for
220  * a function to call whenever our set of 'valid' addresses changes.
221  *
222  * @param cls closure, NULL
223  * @param add_remove #GNUNET_YES to add a new public IP address, 
224  *                   #GNUNET_NO to remove a previous (now invalid) one
225  * @param ac address class the address belongs to
226  * @param addr either the previous or the new public IP address
227  * @param addrlen actual length of the @a addr
228  */
229 static void
230 address_cb (void *cls,
231             int add_remove,
232             enum GNUNET_NAT_AddressClass ac,
233             const struct sockaddr *addr,
234             socklen_t addrlen)
235 {
236   GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
237               "%s %s (%d)\n",
238               add_remove ? "+" : "-",
239               GNUNET_a2s (addr,
240                           addrlen),
241               (int) ac);
242 }
243
244
245 /**
246  * Signature of the callback passed to #GNUNET_NAT_register().
247  * for a function to call whenever someone asks us to do connection
248  * reversal.
249  *
250  * @param cls closure, NULL
251  * @param local_addr address where we received the request
252  * @param local_addrlen actual length of the @a local_addr
253  * @param remote_addr public IP address of the other peer
254  * @param remote_addrlen actual length of the @a remote_addr
255  */
256 static void
257 reversal_cb (void *cls,
258              const struct sockaddr *local_addr,
259              socklen_t local_addrlen,
260              const struct sockaddr *remote_addr,
261              socklen_t remote_addrlen)
262 {
263   GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
264               "Connection reversal requested by %s\n",
265               GNUNET_a2s (remote_addr,
266                           remote_addrlen));
267 }
268
269
270 /**
271  * Task run on shutdown.
272  *
273  * @param cls NULL
274  */
275 static void
276 do_shutdown (void *cls)
277 {
278   if (NULL != ah)
279   {
280     GNUNET_NAT_autoconfig_cancel (ah);
281     ah = NULL;
282   }
283   if (NULL != nt)
284   {
285     GNUNET_NAT_test_stop (nt);
286     nt = NULL;
287   }
288   if (NULL != nh)
289   {
290     GNUNET_NAT_unregister (nh);
291     nh = NULL;
292   }
293 }
294
295
296 /**
297  * Main function that will be run.
298  *
299  * @param cls closure
300  * @param args remaining command-line arguments
301  * @param cfgfile name of the configuration file used (for saving, can be NULL!)
302  * @param c configuration
303  */
304 static void
305 run (void *cls,
306      char *const *args,
307      const char *cfgfile,
308      const struct GNUNET_CONFIGURATION_Handle *c)
309 {
310   uint8_t af;
311   struct sockaddr_in bind_sa;
312   struct sockaddr_in extern_sa;
313   struct sockaddr *local_sa;
314   struct sockaddr *remote_sa;
315   size_t local_len;
316   size_t remote_len;
317   
318   if (use_tcp && use_udp)
319   {
320     GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
321                 "Cannot use TCP and UDP\n");
322     global_ret = 1;
323     return;
324   }
325   proto = 0;
326   if (use_tcp)
327     proto = IPPROTO_TCP;
328   if (use_udp)
329     proto = IPPROTO_UDP;
330   if (0 == proto)
331   {
332     GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
333                 "Must specify either TCP or UDP\n");
334     global_ret = 1;
335     return;
336   }
337   if (NULL != bind_addr)
338   {
339     if (GNUNET_OK !=
340         GNUNET_STRINGS_to_address_ipv4 (bind_addr,
341                                         strlen (bind_addr),
342                                         &bind_sa))
343     {
344       GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
345                   "Invalid socket address `%s'\n",
346                   bind_addr);
347       global_ret = 1;
348       return;
349     }
350   }
351   if (NULL != extern_addr)
352   {
353     if (GNUNET_OK !=
354         GNUNET_STRINGS_to_address_ipv4 (extern_addr,
355                                         strlen (extern_addr),
356                                         &extern_sa))
357     {
358       GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
359                   "Invalid socket address `%s'\n",
360                   extern_addr);
361       global_ret = 1;
362       return;
363     }
364   }
365   if (NULL != local_addr)
366   {
367     local_len = GNUNET_STRINGS_parse_socket_addr (local_addr,
368                                                   &af,
369                                                   &local_sa);
370     if (0 == local_len)
371     {
372       GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
373                   "Invalid socket address `%s'\n",
374                   local_addr);
375       global_ret = 1;
376       return;
377     }
378   }
379   if (NULL != remote_addr)
380   {
381     remote_len = GNUNET_STRINGS_parse_socket_addr (remote_addr,
382                                                    &af,
383                                                    &remote_sa);
384     if (0 == remote_len)
385     {
386       GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
387                   "Invalid socket address `%s'\n",
388                   remote_addr);
389       global_ret = 1;
390       return;
391     }
392   }
393
394   if (NULL != bind_addr)
395   {
396     if (NULL == extern_addr)
397       extern_sa = bind_sa;
398     nt = GNUNET_NAT_test_start (c,
399                                 proto,
400                                 bind_sa.sin_addr,
401                                 ntohs (bind_sa.sin_port),
402                                 extern_sa.sin_addr,
403                                 ntohs (extern_sa.sin_port),
404                                 &test_report_cb,
405                                 NULL);
406   }
407
408   if (NULL != local_addr)
409   {
410     nh = GNUNET_NAT_register (c,
411                               proto,
412                               (uint16_t) adv_port,
413                               1,
414                               (const struct sockaddr **) &local_sa,
415                               &local_len,
416                               &address_cb,
417                               (listen_reversal) ? &reversal_cb : NULL,
418                               NULL);
419   }
420
421   if (NULL != remote_addr)
422   {
423     int ret;
424     
425     if ( (NULL == nh) ||
426          (sizeof (struct sockaddr_in) != local_len) )
427     {
428       GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
429                   "Require IPv4 local address to initiate connection reversal\n");
430       global_ret = 1;
431       GNUNET_SCHEDULER_shutdown ();
432       return;
433     }
434     if (sizeof (struct sockaddr_in) != remote_len)
435     {
436       GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
437                   "Require IPv4 reversal target address\n");
438       global_ret = 1;
439       GNUNET_SCHEDULER_shutdown ();
440       return;
441     }
442     ret = GNUNET_NAT_request_reversal (nh,
443                                        (const struct sockaddr_in *) &local_sa,
444                                        (const struct sockaddr_in *) &remote_sa);
445     switch (ret)
446     {
447     case GNUNET_SYSERR:
448       GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
449                   "Connection reversal internal error\n");
450       break;
451     case GNUNET_NO:
452       GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
453                   "Connection reversal unavailable\n");
454       break;
455     case GNUNET_OK:
456       /* operation in progress */
457       break;
458     }
459   }
460   
461   if (do_auto)
462   {
463     ah = GNUNET_NAT_autoconfig_start (c,
464                                       &auto_config_cb,
465                                       NULL);
466   }
467   GNUNET_SCHEDULER_add_shutdown (&do_shutdown,
468                                  NULL);
469   test_finished ();
470 }
471
472
473 /**
474  * Main function of gnunet-nat
475  *
476  * @param argc number of command-line arguments
477  * @param argv command line
478  * @return 0 on success, -1 on error
479  */
480 int
481 main (int argc,
482       char *const argv[])
483 {
484   static const struct GNUNET_GETOPT_CommandLineOption options[] = {
485     {'a', "auto", NULL,
486      gettext_noop ("run autoconfiguration"),
487      GNUNET_NO, &GNUNET_GETOPT_set_one, &do_auto },
488     {'b', "bind", "ADDRESS",
489      gettext_noop ("which IP and port are we bound to"),
490      GNUNET_YES, &GNUNET_GETOPT_set_string, &bind_addr },
491     {'e', "external", "ADDRESS",
492      gettext_noop ("which external IP and port should be used to test"),
493      GNUNET_YES, &GNUNET_GETOPT_set_string, &extern_addr },
494     {'l', "local", "ADDRESS",
495      gettext_noop ("which IP and port are we locally using to listen to for connection reversals"),
496      GNUNET_YES, &GNUNET_GETOPT_set_string, &local_addr },
497     {'r', "remote", "ADDRESS",
498      gettext_noop ("which remote IP and port should be asked for connection reversal"),
499      GNUNET_YES, &GNUNET_GETOPT_set_string, &remote_addr },
500     {'L', "listen", NULL,
501      gettext_noop ("listen for connection reversal requests"),
502      GNUNET_NO, &GNUNET_GETOPT_set_one, &listen_reversal },
503     {'p', "port", NULL,
504      gettext_noop ("port to use to advertise"),
505      GNUNET_YES, &GNUNET_GETOPT_set_uint, &adv_port },
506     {'s', "stun", NULL,
507      gettext_noop ("enable STUN processing"),
508      GNUNET_NO, &GNUNET_GETOPT_set_one, &do_stun },
509     // FIMXE: -s not implemented!
510     {'t', "tcp", NULL,
511      gettext_noop ("use TCP"),
512      GNUNET_NO, &GNUNET_GETOPT_set_one, &use_tcp },
513     {'u', "udp", NULL,
514      gettext_noop ("use UDP"),
515      GNUNET_NO, &GNUNET_GETOPT_set_one, &use_udp },
516    GNUNET_GETOPT_OPTION_END
517   };
518
519   if (GNUNET_OK !=
520       GNUNET_STRINGS_get_utf8_args (argc, argv,
521                                     &argc, &argv))
522     return 2;
523   if (GNUNET_OK !=
524       GNUNET_PROGRAM_run (argc, argv,
525                           "gnunet-nat [options]",
526                           _("GNUnet NAT traversal autoconfigure daemon"),
527                           options,
528                           &run,
529                           NULL))
530   {
531     global_ret = 1;
532   }
533   GNUNET_free ((void*) argv);
534   return global_ret;
535 }
536
537
538 /* end of gnunet-nat.c */