add draft man page for gnunet-nat
[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   // 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
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   // FIXME: print!
237 }
238
239
240 /**
241  * Signature of the callback passed to #GNUNET_NAT_register().
242  * for a function to call whenever someone asks us to do connection
243  * reversal.
244  *
245  * @param cls closure
246  * @param local_addr address where we received the request
247  * @param local_addrlen actual length of the @a local_addr
248  * @param remote_addr public IP address of the other peer
249  * @param remote_addrlen actual length of the @a remote_addr
250  */
251 static void
252 reversal_cb (void *cls,
253              const struct sockaddr *local_addr,
254              socklen_t local_addrlen,
255              const struct sockaddr *remote_addr,
256              socklen_t remote_addrlen)
257 {
258   // FIXME: print!
259 }
260
261
262 /**
263  * Task run on shutdown.
264  *
265  * @param cls NULL
266  */
267 static void
268 do_shutdown (void *cls)
269 {
270   if (NULL != ah)
271   {
272     GNUNET_NAT_autoconfig_cancel (ah);
273     ah = NULL;
274   }
275   if (NULL != nt)
276   {
277     GNUNET_NAT_test_stop (nt);
278     nt = NULL;
279   }
280   if (NULL != nh)
281   {
282     GNUNET_NAT_unregister (nh);
283     nh = NULL;
284   }
285 }
286
287
288 /**
289  * Main function that will be run.
290  *
291  * @param cls closure
292  * @param args remaining command-line arguments
293  * @param cfgfile name of the configuration file used (for saving, can be NULL!)
294  * @param c configuration
295  */
296 static void
297 run (void *cls,
298      char *const *args,
299      const char *cfgfile,
300      const struct GNUNET_CONFIGURATION_Handle *c)
301 {
302   uint8_t af;
303   struct sockaddr_in bind_sa;
304   struct sockaddr_in extern_sa;
305   struct sockaddr *local_sa;
306   struct sockaddr *remote_sa;
307   size_t local_len;
308   size_t remote_len;
309   
310   if (use_tcp && use_udp)
311   {
312     GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
313                 "Cannot use TCP and UDP\n");
314     global_ret = 1;
315     return;
316   }
317   proto = 0;
318   if (use_tcp)
319     proto = IPPROTO_TCP;
320   if (use_udp)
321     proto = IPPROTO_UDP;
322   if (0 == proto)
323   {
324     GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
325                 "Must specify either TCP or UDP\n");
326     global_ret = 1;
327     return;
328   }
329   if (NULL != bind_addr)
330   {
331     if (GNUNET_OK !=
332         GNUNET_STRINGS_to_address_ipv4 (bind_addr,
333                                         strlen (bind_addr),
334                                         &bind_sa))
335     {
336       GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
337                   "Invalid socket address `%s'\n",
338                   bind_addr);
339       global_ret = 1;
340       return;
341     }
342   }
343   if (NULL != extern_addr)
344   {
345     if (GNUNET_OK !=
346         GNUNET_STRINGS_to_address_ipv4 (extern_addr,
347                                         strlen (extern_addr),
348                                         &extern_sa))
349     {
350       GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
351                   "Invalid socket address `%s'\n",
352                   extern_addr);
353       global_ret = 1;
354       return;
355     }
356   }
357   if (NULL != local_addr)
358   {
359     local_len = GNUNET_STRINGS_parse_socket_addr (local_addr,
360                                                   &af,
361                                                   &local_sa);
362     if (0 == local_len)
363     {
364       GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
365                   "Invalid socket address `%s'\n",
366                   local_addr);
367       global_ret = 1;
368       return;
369     }
370   }
371   if (NULL != remote_addr)
372   {
373     remote_len = GNUNET_STRINGS_parse_socket_addr (remote_addr,
374                                                    &af,
375                                                    &remote_sa);
376     if (0 == remote_len)
377     {
378       GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
379                   "Invalid socket address `%s'\n",
380                   remote_addr);
381       global_ret = 1;
382       return;
383     }
384   }
385
386   if (NULL != bind_addr)
387   {
388     if (NULL == extern_addr)
389       extern_sa = bind_sa;
390     nt = GNUNET_NAT_test_start (c,
391                                 proto,
392                                 bind_sa.sin_addr,
393                                 ntohs (bind_sa.sin_port),
394                                 extern_sa.sin_addr,
395                                 ntohs (extern_sa.sin_port),
396                                 &test_report_cb,
397                                 NULL);
398   }
399
400   if (NULL != local_addr)
401   {
402     nh = GNUNET_NAT_register (c,
403                               proto,
404                               (uint16_t) adv_port,
405                               1,
406                               (const struct sockaddr **) &local_sa,
407                               &local_len,
408                               &address_cb,
409                               (listen_reversal) ? &reversal_cb : NULL,
410                               NULL);
411   }
412
413   if (NULL != remote_addr)
414   {
415     int ret;
416     
417     if ( (NULL == nh) ||
418          (sizeof (struct sockaddr_in) != local_len) )
419     {
420       GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
421                   "Require IPv4 local address to initiate connection reversal\n");
422       global_ret = 1;
423       GNUNET_SCHEDULER_shutdown ();
424       return;
425     }
426     if (sizeof (struct sockaddr_in) != remote_len)
427     {
428       GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
429                   "Require IPv4 reversal target address\n");
430       global_ret = 1;
431       GNUNET_SCHEDULER_shutdown ();
432       return;
433     }
434     ret = GNUNET_NAT_request_reversal (nh,
435                                        (const struct sockaddr_in *) &local_sa,
436                                        (const struct sockaddr_in *) &remote_sa);
437     switch (ret)
438     {
439     case GNUNET_SYSERR:
440       GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
441                   "Connection reversal internal error\n");
442       break;
443     case GNUNET_NO:
444       GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
445                   "Connection reversal unavailable\n");
446       break;
447     case GNUNET_OK:
448       /* operation in progress */
449       break;
450     }
451   }
452   
453   if (do_auto)
454   {
455     ah = GNUNET_NAT_autoconfig_start (c,
456                                       &auto_config_cb,
457                                       NULL);
458   }
459   GNUNET_SCHEDULER_add_shutdown (&do_shutdown,
460                                  NULL);
461   test_finished ();
462 }
463
464
465 /**
466  * Main function of gnunet-nat
467  *
468  * @param argc number of command-line arguments
469  * @param argv command line
470  * @return 0 on success, -1 on error
471  */
472 int
473 main (int argc,
474       char *const argv[])
475 {
476   static const struct GNUNET_GETOPT_CommandLineOption options[] = {
477     {'a', "auto", NULL,
478      gettext_noop ("run autoconfiguration"),
479      GNUNET_NO, &GNUNET_GETOPT_set_one, &do_auto },
480     {'b', "bind", "ADDRESS",
481      gettext_noop ("which IP and port are we bound to"),
482      GNUNET_YES, &GNUNET_GETOPT_set_string, &bind_addr },
483     {'e', "external", "ADDRESS",
484      gettext_noop ("which external IP and port should be used to test"),
485      GNUNET_YES, &GNUNET_GETOPT_set_string, &extern_addr },
486     {'l', "local", "ADDRESS",
487      gettext_noop ("which IP and port are we locally using to listen to for connection reversals"),
488      GNUNET_YES, &GNUNET_GETOPT_set_string, &local_addr },
489     {'r', "remote", "ADDRESS",
490      gettext_noop ("which remote IP and port should be asked for connection reversal"),
491      GNUNET_YES, &GNUNET_GETOPT_set_string, &remote_addr },
492     {'L', "listen", NULL,
493      gettext_noop ("listen for connection reversal requests"),
494      GNUNET_NO, &GNUNET_GETOPT_set_one, &listen_reversal },
495     {'p', "port", NULL,
496      gettext_noop ("port to use to advertise"),
497      GNUNET_YES, &GNUNET_GETOPT_set_uint, &adv_port },
498     {'s', "stun", NULL,
499      gettext_noop ("enable STUN processing"),
500      GNUNET_NO, &GNUNET_GETOPT_set_one, &do_stun },
501     {'t', "tcp", NULL,
502      gettext_noop ("use TCP"),
503      GNUNET_NO, &GNUNET_GETOPT_set_one, &use_tcp },
504     {'u', "udp", NULL,
505      gettext_noop ("use UDP"),
506      GNUNET_NO, &GNUNET_GETOPT_set_one, &use_udp },
507    GNUNET_GETOPT_OPTION_END
508   };
509
510   if (GNUNET_OK !=
511       GNUNET_STRINGS_get_utf8_args (argc, argv,
512                                     &argc, &argv))
513     return 2;
514   if (GNUNET_OK !=
515       GNUNET_PROGRAM_run (argc, argv,
516                           "gnunet-nat [options]",
517                           _("GNUnet NAT traversal autoconfigure daemon"),
518                           options,
519                           &run,
520                           NULL))
521   {
522     global_ret = 1;
523   }
524   GNUNET_free ((void*) argv);
525   return global_ret;
526 }
527
528
529 /* end of gnunet-nat.c */