indentation
[oweals/gnunet.git] / src / nat / nat.c
1 /*
2      This file is part of GNUnet.
3      (C) 2009, 2010, 2011 Christian Grothoff (and other contributing authors)
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., 59 Temple Place - Suite 330,
18      Boston, MA 02111-1307, USA.
19 */
20
21 /**
22  * @file nat/nat.c
23  * @brief Library handling UPnP and NAT-PMP port forwarding and
24  *     external IP address retrieval
25  * @author Milan Bouchet-Valat
26  * @author Christian Grothoff
27  */
28 #include "platform.h"
29 #include "gnunet_util_lib.h"
30 #include "gnunet_resolver_service.h"
31 #include "gnunet_nat_lib.h"
32
33 #define DEBUG_NAT GNUNET_NO
34
35 /**
36  * How often do we scan for changes in our IP address from our local
37  * interfaces?
38  */
39 #define IFC_SCAN_FREQUENCY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 15)
40
41 /**
42  * How often do we scan for changes in how our hostname resolves?
43  */
44 #define HOSTNAME_DNS_FREQUENCY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 20)
45
46
47 /**
48  * How often do we scan for changes in how our external (dyndns) hostname resolves?
49  */
50 #define DYNDNS_FREQUENCY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 7)
51
52 /**
53  * How long until we give up trying to resolve our own hostname?
54  */
55 #define HOSTNAME_RESOLVE_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1)
56
57
58 /**
59  * Where did the given local address originate from?
60  * To be used for debugging as well as in the future 
61  * to remove all addresses from a certain source when
62  * we reevaluate the source.
63  */
64 enum LocalAddressSource
65 {
66     /**
67      * Address was obtained by DNS resolution of the external hostname
68      * given in the configuration (i.e. hole-punched DynDNS setup).
69      */
70   LAL_EXTERNAL_IP,
71
72     /**
73      * Address was obtained by looking up our own hostname in DNS.
74      */
75   LAL_HOSTNAME_DNS,
76
77     /**
78      * Address was obtained by scanning our hosts's network interfaces
79      * and taking their address (no DNS involved).
80      */
81   LAL_INTERFACE_ADDRESS,
82
83     /**
84      * Addresses we were explicitly bound to.
85      */
86   LAL_BINDTO_ADDRESS,
87
88     /**
89      * Addresses from UPnP or PMP
90      */
91   LAL_UPNP,
92
93     /**
94      * End of the list.
95      */
96   LAL_END
97 };
98
99
100 /**
101  * List of local addresses that we currently deem valid.  Actual
102  * struct is followed by the 'struct sockaddr'.  Note that the code
103  * intentionally makes no attempt to ensure that a particular address
104  * is only listed once (especially since it may come from different
105  * sources, and the source is an "internal" construct).
106  */
107 struct LocalAddressList
108 {
109   /**
110    * This is a linked list.
111    */
112   struct LocalAddressList *next;
113
114   /**
115    * Previous entry.
116    */
117   struct LocalAddressList *prev;
118
119   /**
120    * Number of bytes of address that follow.
121    */
122   socklen_t addrlen;
123
124   /**
125    * Origin of the local address.
126    */
127   enum LocalAddressSource source;
128 };
129
130
131 /**
132  * Handle for miniupnp-based NAT traversal actions.
133  */
134 struct MiniList
135 {
136
137   /**
138    * Doubly-linked list.
139    */
140   struct MiniList *next;
141
142   /**
143    * Doubly-linked list.
144    */
145   struct MiniList *prev;
146
147   /**
148    * Handle to mini-action.
149    */
150   struct GNUNET_NAT_MiniHandle *mini;
151
152   /**
153    * Local port number that was mapped.
154    */
155   uint16_t port;
156
157 };
158
159
160 /**
161  * Handle for active NAT registrations.
162  */
163 struct GNUNET_NAT_Handle
164 {
165
166   /**
167    * Configuration to use.
168    */
169   const struct GNUNET_CONFIGURATION_Handle *cfg;
170
171   /**
172    * Function to call when we learn about a new address.
173    */
174   GNUNET_NAT_AddressCallback address_callback;
175
176   /**
177    * Function to call when we notice another peer asking for
178    * connection reversal.
179    */
180   GNUNET_NAT_ReversalCallback reversal_callback;
181
182   /**
183    * Closure for 'callback'.
184    */
185   void *callback_cls;
186
187   /**
188    * Handle for (DYN)DNS lookup of our external IP.
189    */
190   struct GNUNET_RESOLVER_RequestHandle *ext_dns;
191
192   /**
193    * Handle for request of hostname resolution, non-NULL if pending.
194    */
195   struct GNUNET_RESOLVER_RequestHandle *hostname_dns;
196
197   /**
198    * stdout pipe handle for the gnunet-helper-nat-server process
199    */
200   struct GNUNET_DISK_PipeHandle *server_stdout;
201
202   /**
203    * stdout file handle (for reading) for the gnunet-helper-nat-server process
204    */
205   const struct GNUNET_DISK_FileHandle *server_stdout_handle;
206
207   /**
208    * Linked list of currently valid addresses (head).
209    */
210   struct LocalAddressList *lal_head;
211
212   /**
213    * Linked list of currently valid addresses (tail).
214    */
215   struct LocalAddressList *lal_tail;
216
217   /**
218    * How long do we wait for restarting a crashed gnunet-helper-nat-server?
219    */
220   struct GNUNET_TIME_Relative server_retry_delay;
221
222   /**
223    * ID of select gnunet-helper-nat-server stdout read task
224    */
225   GNUNET_SCHEDULER_TaskIdentifier server_read_task;
226
227   /**
228    * ID of interface IP-scan task
229    */
230   GNUNET_SCHEDULER_TaskIdentifier ifc_task;
231
232   /**
233    * ID of hostname DNS lookup task
234    */
235   GNUNET_SCHEDULER_TaskIdentifier hostname_task;
236
237   /**
238    * ID of DynDNS lookup task
239    */
240   GNUNET_SCHEDULER_TaskIdentifier dns_task;
241
242   /**
243    * ID of task to add addresses from bind.
244    */
245   GNUNET_SCHEDULER_TaskIdentifier bind_task;
246
247   /**
248    * How often do we scan for changes in our IP address from our local
249    * interfaces?
250    */
251   struct GNUNET_TIME_Relative ifc_scan_frequency;
252
253   /**
254    * How often do we scan for changes in how our hostname resolves?
255    */
256   struct GNUNET_TIME_Relative hostname_dns_frequency;
257
258   /**
259    * How often do we scan for changes in how our external (dyndns) hostname resolves?
260    */
261   struct GNUNET_TIME_Relative dyndns_frequency;
262
263   /**
264    * The process id of the server process (if behind NAT)
265    */
266   struct GNUNET_OS_Process *server_proc;
267
268   /**
269    * LAN address as passed by the caller (array).
270    */
271   struct sockaddr **local_addrs;
272
273   /**
274    * Length of the 'local_addrs'.
275    */
276   socklen_t *local_addrlens;
277
278   /**
279    * List of handles for UPnP-traversal, one per local port (if
280    * not IPv6-only).
281    */
282   struct MiniList *mini_head;
283
284   /**
285    * List of handles for UPnP-traversal, one per local port (if
286    * not IPv6-only).
287    */
288   struct MiniList *mini_tail;
289
290   /**
291    * Number of entries in 'local_addrs' array.
292    */
293   unsigned int num_local_addrs;
294
295   /**
296    * The our external address (according to config, UPnP may disagree...)
297    */
298   char *external_address;
299
300   /**
301    * Presumably our internal address (according to config)
302    */
303   char *internal_address;
304
305   /**
306    * Is this transport configured to be behind a NAT?
307    */
308   int behind_nat;
309
310   /**
311    * Has the NAT been punched? (according to config)
312    */
313   int nat_punched;
314
315   /**
316    * Is this transport configured to allow connections to NAT'd peers?
317    */
318   int enable_nat_client;
319
320   /**
321    * Should we run the gnunet-helper-nat-server?
322    */
323   int enable_nat_server;
324
325   /**
326    * Are we allowed to try UPnP/PMP for NAT traversal?
327    */
328   int enable_upnp;
329
330   /**
331    * Should we use local addresses (loopback)? (according to config)
332    */
333   int use_localaddresses;
334
335   /**
336    * Should we do a DNS lookup of our hostname to find out our own IP?
337    */
338   int use_hostname;
339
340   /**
341    * Is using IPv6 disabled?
342    */
343   int disable_ipv6;
344
345   /**
346    * Is this TCP or UDP?
347    */
348   int is_tcp;
349
350   /**
351    * Port we advertise to the outside.
352    */
353   uint16_t adv_port;
354
355 };
356
357
358 /**
359  * Try to start the gnunet-helper-nat-server (if it is not
360  * already running).
361  *
362  * @param h handle to NAT
363  */
364 static void start_gnunet_nat_server (struct GNUNET_NAT_Handle *h);
365
366
367 /**
368  * Remove all addresses from the list of 'local' addresses
369  * that originated from the given source.
370  * 
371  * @param plugin the plugin
372  * @param src source that identifies addresses to remove
373  */
374 static void
375 remove_from_address_list_by_source (struct GNUNET_NAT_Handle *h,
376                                     enum LocalAddressSource src)
377 {
378   struct LocalAddressList *pos;
379   struct LocalAddressList *next;
380
381   next = h->lal_head;
382   while (NULL != (pos = next))
383   {
384     next = pos->next;
385     if (pos->source != src)
386       continue;
387     GNUNET_CONTAINER_DLL_remove (h->lal_head, h->lal_tail, pos);
388     if (NULL != h->address_callback)
389       h->address_callback (h->callback_cls,
390                            GNUNET_NO,
391                            (const struct sockaddr *) &pos[1], pos->addrlen);
392     GNUNET_free (pos);
393   }
394 }
395
396
397 /**
398  * Add the given address to the list of 'local' addresses, thereby
399  * making it a 'legal' address for this peer to have.  
400  * 
401  * @param plugin the plugin
402  * @param src where did the local address originate from?
403  * @param arg the address, some 'struct sockaddr'
404  * @param arg_size number of bytes in arg
405  */
406 static void
407 add_to_address_list_as_is (struct GNUNET_NAT_Handle *h,
408                            enum LocalAddressSource src,
409                            const struct sockaddr *arg, socklen_t arg_size)
410 {
411   struct LocalAddressList *lal;
412
413   lal = GNUNET_malloc (sizeof (struct LocalAddressList) + arg_size);
414   memcpy (&lal[1], arg, arg_size);
415   lal->addrlen = arg_size;
416   lal->source = src;
417   GNUNET_CONTAINER_DLL_insert (h->lal_head, h->lal_tail, lal);
418 #if DEBUG_NAT
419   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
420                    "nat",
421                    "Adding address `%s' from source %d\n",
422                    GNUNET_a2s (arg, arg_size), src);
423 #endif
424   if (NULL != h->address_callback)
425     h->address_callback (h->callback_cls, GNUNET_YES, arg, arg_size);
426 }
427
428
429 /**
430  * Add the given address to the list of 'local' addresses, thereby
431  * making it a 'legal' address for this peer to have.   Set the
432  * port number in the process to the advertised port and possibly
433  * also to zero (if we have the gnunet-helper-nat-server).
434  * 
435  * @param plugin the plugin
436  * @param src where did the local address originate from?
437  * @param arg the address, some 'struct sockaddr'
438  * @param arg_size number of bytes in arg
439  */
440 static void
441 add_to_address_list (struct GNUNET_NAT_Handle *h,
442                      enum LocalAddressSource src,
443                      const struct sockaddr *arg, socklen_t arg_size)
444 {
445   struct sockaddr_in s4;
446   const struct sockaddr_in *in4;
447   struct sockaddr_in6 s6;
448   const struct sockaddr_in6 *in6;
449
450   if (arg_size == sizeof (struct sockaddr_in))
451   {
452     in4 = (const struct sockaddr_in *) arg;
453     s4 = *in4;
454     s4.sin_port = htons (h->adv_port);
455     add_to_address_list_as_is (h,
456                                src,
457                                (const struct sockaddr *) &s4,
458                                sizeof (struct sockaddr_in));
459     if (GNUNET_YES == h->enable_nat_server)
460     {
461       /* also add with PORT = 0 to indicate NAT server is enabled */
462       s4.sin_port = htons (0);
463       add_to_address_list_as_is (h,
464                                  src,
465                                  (const struct sockaddr *) &s4,
466                                  sizeof (struct sockaddr_in));
467     }
468   }
469   else if (arg_size == sizeof (struct sockaddr_in6))
470   {
471     if (GNUNET_YES != h->disable_ipv6)
472     {
473       in6 = (const struct sockaddr_in6 *) arg;
474       s6 = *in6;
475       s6.sin6_port = htons (h->adv_port);
476       add_to_address_list_as_is (h,
477                                  src,
478                                  (const struct sockaddr *) &s6,
479                                  sizeof (struct sockaddr_in6));
480     }
481   }
482   else
483   {
484     GNUNET_assert (0);
485   }
486 }
487
488
489 /**
490  * Add the given IP address to the list of 'local' addresses, thereby
491  * making it a 'legal' address for this peer to have.  
492  * 
493  * @param plugin the plugin
494  * @param src where did the local address originate from?
495  * @param arg the address, some 'struct in_addr' or 'struct in6_addr'
496  * @param arg_size number of bytes in arg
497  */
498 static void
499 add_ip_to_address_list (struct GNUNET_NAT_Handle *h,
500                         enum LocalAddressSource src,
501                         const void *addr, socklen_t addrlen)
502 {
503   struct sockaddr_in s4;
504   const struct in_addr *in4;
505   struct sockaddr_in6 s6;
506   const struct in6_addr *in6;
507
508   if (addrlen == sizeof (struct in_addr))
509   {
510     in4 = (const struct in_addr *) addr;
511     memset (&s4, 0, sizeof (s4));
512     s4.sin_family = AF_INET;
513     s4.sin_port = 0;
514 #if HAVE_SOCKADDR_IN_SIN_LEN
515     s4.sin_len = (u_char) sizeof (struct sockaddr_in);
516 #endif
517     s4.sin_addr = *in4;
518     add_to_address_list (h,
519                          src,
520                          (const struct sockaddr *) &s4,
521                          sizeof (struct sockaddr_in));
522     if (GNUNET_YES == h->enable_nat_server)
523     {
524       /* also add with PORT = 0 to indicate NAT server is enabled */
525       s4.sin_port = htons (0);
526       add_to_address_list (h,
527                            src,
528                            (const struct sockaddr *) &s4,
529                            sizeof (struct sockaddr_in));
530
531     }
532   }
533   else if (addrlen == sizeof (struct in6_addr))
534   {
535     if (GNUNET_YES != h->disable_ipv6)
536     {
537       in6 = (const struct in6_addr *) addr;
538       memset (&s6, 0, sizeof (s6));
539       s6.sin6_family = AF_INET6;
540       s6.sin6_port = htons (h->adv_port);
541 #if HAVE_SOCKADDR_IN_SIN_LEN
542       s6.sin6_len = (u_char) sizeof (struct sockaddr_in6);
543 #endif
544       s6.sin6_addr = *in6;
545       add_to_address_list (h,
546                            src,
547                            (const struct sockaddr *) &s6,
548                            sizeof (struct sockaddr_in6));
549     }
550   }
551   else
552   {
553     GNUNET_assert (0);
554   }
555 }
556
557
558 /**
559  * Task to do DNS lookup on our external hostname to
560  * get DynDNS-IP addresses.
561  *
562  * @param cls the NAT handle
563  * @param tc scheduler context
564  */
565 static void
566 resolve_dns (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
567
568
569 /**
570  * Our (external) hostname was resolved and the configuration says that
571  * the NAT was hole-punched.
572  *
573  * @param cls the 'struct Plugin'
574  * @param addr NULL on error, otherwise result of DNS lookup
575  * @param addrlen number of bytes in addr
576  */
577 static void
578 process_external_ip (void *cls, const struct sockaddr *addr, socklen_t addrlen)
579 {
580   struct GNUNET_NAT_Handle *h = cls;
581   struct in_addr dummy;
582
583   if (addr == NULL)
584   {
585     h->ext_dns = NULL;
586     if (1 == inet_pton (AF_INET, h->external_address, &dummy))
587       return;                   /* repated lookup pointless: was numeric! */
588     h->dns_task = GNUNET_SCHEDULER_add_delayed (h->dyndns_frequency,
589                                                 &resolve_dns, h);
590     return;
591   }
592   add_to_address_list (h, LAL_EXTERNAL_IP, addr, addrlen);
593 }
594
595
596 /**
597  * Task to do a lookup on our hostname for IP addresses.
598  *
599  * @param cls the NAT handle
600  * @param tc scheduler context
601  */
602 static void
603 resolve_hostname (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
604
605
606 /**
607  * Function called by the resolver for each address obtained from DNS
608  * for our own hostname.  Add the addresses to the list of our IP
609  * addresses.
610  *
611  * @param cls closure
612  * @param addr one of the addresses of the host, NULL for the last address
613  * @param addrlen length of the address
614  */
615 static void
616 process_hostname_ip (void *cls, const struct sockaddr *addr, socklen_t addrlen)
617 {
618   struct GNUNET_NAT_Handle *h = cls;
619
620   if (addr == NULL)
621   {
622     h->hostname_dns = NULL;
623     h->hostname_task = GNUNET_SCHEDULER_add_delayed (h->hostname_dns_frequency,
624                                                      &resolve_hostname, h);
625     return;
626   }
627   add_to_address_list (h, LAL_HOSTNAME_DNS, addr, addrlen);
628 }
629
630
631 /**
632  * Add the IP of our network interface to the list of
633  * our IP addresses.
634  *
635  * @param cls the 'struct GNUNET_NAT_Handle'
636  * @param name name of the interface
637  * @param isDefault do we think this may be our default interface
638  * @param addr address of the interface
639  * @param addrlen number of bytes in addr
640  * @return GNUNET_OK to continue iterating
641  */
642 static int
643 process_interfaces (void *cls,
644                     const char *name,
645                     int isDefault,
646                     const struct sockaddr *addr, socklen_t addrlen)
647 {
648   struct GNUNET_NAT_Handle *h = cls;
649   const struct sockaddr_in *s4;
650   const struct sockaddr_in6 *s6;
651   const void *ip;
652   char buf[INET6_ADDRSTRLEN];
653
654   switch (addr->sa_family)
655   {
656   case AF_INET:
657     s4 = (struct sockaddr_in *) addr;
658     ip = &s4->sin_addr;
659     if (GNUNET_YES == h->use_localaddresses)
660       add_ip_to_address_list (h,
661                               LAL_INTERFACE_ADDRESS,
662                               &s4->sin_addr, sizeof (struct in_addr));
663     break;
664   case AF_INET6:
665     s6 = (struct sockaddr_in6 *) addr;
666     if (IN6_IS_ADDR_LINKLOCAL (&((struct sockaddr_in6 *) addr)->sin6_addr))
667     {
668       /* skip link local addresses */
669       return GNUNET_OK;
670     }
671     ip = &s6->sin6_addr;
672     if (GNUNET_YES == h->use_localaddresses)
673       add_ip_to_address_list (h,
674                               LAL_INTERFACE_ADDRESS,
675                               &s6->sin6_addr, sizeof (struct in6_addr));
676     break;
677   default:
678     GNUNET_break (0);
679     return GNUNET_OK;
680   }
681   if ((h->internal_address == NULL) &&
682       (h->server_proc == NULL) &&
683       (h->server_read_task == GNUNET_SCHEDULER_NO_TASK) &&
684       (GNUNET_YES == isDefault) &&
685       ((addr->sa_family == AF_INET) || (addr->sa_family == AF_INET6)))
686   {
687     /* no internal address configured, but we found a "default"
688      * interface, try using that as our 'internal' address */
689     h->internal_address = GNUNET_strdup (inet_ntop (addr->sa_family,
690                                                     ip, buf, sizeof (buf)));
691     start_gnunet_nat_server (h);
692   }
693   return GNUNET_OK;
694 }
695
696
697 /**
698  * Task that restarts the gnunet-helper-nat-server process after a crash
699  * after a certain delay.
700  *
701  * @param cls the 'struct GNUNET_NAT_Handle'
702  * @param tc scheduler context
703  */
704 static void
705 restart_nat_server (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
706 {
707   struct GNUNET_NAT_Handle *h = cls;
708
709   h->server_read_task = GNUNET_SCHEDULER_NO_TASK;
710   if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
711     return;
712   start_gnunet_nat_server (h);
713 }
714
715
716 /**
717  * We have been notified that gnunet-helper-nat-server has written something to stdout.
718  * Handle the output, then reschedule this function to be called again once
719  * more is available.
720  *
721  * @param cls the NAT handle
722  * @param tc the scheduling context
723  */
724 static void
725 nat_server_read (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
726 {
727   struct GNUNET_NAT_Handle *h = cls;
728   char mybuf[40];
729   ssize_t bytes;
730   size_t i;
731   int port;
732   const char *port_start;
733   struct sockaddr_in sin_addr;
734
735   h->server_read_task = GNUNET_SCHEDULER_NO_TASK;
736   if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
737     return;
738   memset (mybuf, 0, sizeof (mybuf));
739   bytes = GNUNET_DISK_file_read (h->server_stdout_handle,
740                                  mybuf, sizeof (mybuf));
741   if (bytes < 1)
742   {
743 #if DEBUG_NAT
744     GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
745                      "nat",
746                      "Finished reading from server stdout with code: %d\n",
747                      bytes);
748 #endif
749     if (0 != GNUNET_OS_process_kill (h->server_proc, SIGTERM))
750       GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
751     GNUNET_OS_process_wait (h->server_proc);
752     GNUNET_OS_process_close (h->server_proc);
753     h->server_proc = NULL;
754     GNUNET_DISK_pipe_close (h->server_stdout);
755     h->server_stdout = NULL;
756     h->server_stdout_handle = NULL;
757     /* now try to restart it */
758     h->server_retry_delay =
759         GNUNET_TIME_relative_multiply (h->server_retry_delay, 2);
760     h->server_retry_delay =
761         GNUNET_TIME_relative_max (GNUNET_TIME_UNIT_HOURS,
762                                   h->server_retry_delay);
763     h->server_read_task =
764         GNUNET_SCHEDULER_add_delayed (h->server_retry_delay,
765                                       &restart_nat_server, h);
766     return;
767   }
768
769   port_start = NULL;
770   for (i = 0; i < sizeof (mybuf); i++)
771   {
772     if (mybuf[i] == '\n')
773     {
774       mybuf[i] = '\0';
775       break;
776     }
777     if ((mybuf[i] == ':') && (i + 1 < sizeof (mybuf)))
778     {
779       mybuf[i] = '\0';
780       port_start = &mybuf[i + 1];
781     }
782   }
783
784   /* construct socket address of sender */
785   memset (&sin_addr, 0, sizeof (sin_addr));
786   sin_addr.sin_family = AF_INET;
787 #if HAVE_SOCKADDR_IN_SIN_LEN
788   sin_addr.sin_len = sizeof (sin_addr);
789 #endif
790   if ((NULL == port_start) ||
791       (1 != sscanf (port_start, "%d", &port)) ||
792       (-1 == inet_pton (AF_INET, mybuf, &sin_addr.sin_addr)))
793   {
794     /* should we restart gnunet-helper-nat-server? */
795     GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING,
796                      "nat",
797                      _
798                      ("gnunet-helper-nat-server generated malformed address `%s'\n"),
799                      mybuf);
800     h->server_read_task =
801         GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
802                                         h->server_stdout_handle,
803                                         &nat_server_read, h);
804     return;
805   }
806   sin_addr.sin_port = htons ((uint16_t) port);
807 #if DEBUG_NAT
808   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
809                    "nat",
810                    "gnunet-helper-nat-server read: %s:%d\n", mybuf, port);
811 #endif
812   h->reversal_callback (h->callback_cls,
813                         (const struct sockaddr *) &sin_addr, sizeof (sin_addr));
814   h->server_read_task =
815       GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
816                                       h->server_stdout_handle,
817                                       &nat_server_read, h);
818 }
819
820
821 /**
822  * Try to start the gnunet-helper-nat-server (if it is not
823  * already running).
824  *
825  * @param h handle to NAT
826  */
827 static void
828 start_gnunet_nat_server (struct GNUNET_NAT_Handle *h)
829 {
830   if ((h->behind_nat == GNUNET_YES) &&
831       (h->enable_nat_server == GNUNET_YES) &&
832       (h->internal_address != NULL) &&
833       (NULL != (h->server_stdout = GNUNET_DISK_pipe (GNUNET_YES,
834                                                      GNUNET_NO, GNUNET_YES))))
835   {
836 #if DEBUG_NAT
837     GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
838                      "nat"
839                      "Starting %s at `%s'\n",
840                      "gnunet-helper-nat-server", h->internal_address);
841 #endif
842     /* Start the server process */
843     h->server_proc = GNUNET_OS_start_process (NULL,
844                                               h->server_stdout,
845                                               "gnunet-helper-nat-server",
846                                               "gnunet-helper-nat-server",
847                                               h->internal_address, NULL);
848     if (h->server_proc == NULL)
849     {
850       GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING,
851                        "nat",
852                        _("Failed to start %s\n"), "gnunet-helper-nat-server");
853       GNUNET_DISK_pipe_close (h->server_stdout);
854       h->server_stdout = NULL;
855     }
856     else
857     {
858       /* Close the write end of the read pipe */
859       GNUNET_DISK_pipe_close_end (h->server_stdout, GNUNET_DISK_PIPE_END_WRITE);
860       h->server_stdout_handle
861           = GNUNET_DISK_pipe_handle (h->server_stdout,
862                                      GNUNET_DISK_PIPE_END_READ);
863       h->server_read_task
864           = GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
865                                             h->server_stdout_handle,
866                                             &nat_server_read, h);
867     }
868   }
869 }
870
871
872 /**
873  * Task to scan the local network interfaces for IP addresses.
874  *
875  * @param cls the NAT handle
876  * @param tc scheduler context
877  */
878 static void
879 list_interfaces (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
880 {
881   struct GNUNET_NAT_Handle *h = cls;
882
883   h->ifc_task = GNUNET_SCHEDULER_NO_TASK;
884   remove_from_address_list_by_source (h, LAL_INTERFACE_ADDRESS);
885   GNUNET_OS_network_interfaces_list (&process_interfaces, h);
886   h->ifc_task = GNUNET_SCHEDULER_add_delayed (h->ifc_scan_frequency,
887                                               &list_interfaces, h);
888 }
889
890
891 /**
892  * Task to do a lookup on our hostname for IP addresses.
893  *
894  * @param cls the NAT handle
895  * @param tc scheduler context
896  */
897 static void
898 resolve_hostname (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
899 {
900   struct GNUNET_NAT_Handle *h = cls;
901
902   h->hostname_task = GNUNET_SCHEDULER_NO_TASK;
903   remove_from_address_list_by_source (h, LAL_HOSTNAME_DNS);
904   h->hostname_dns = GNUNET_RESOLVER_hostname_resolve (AF_UNSPEC,
905                                                       HOSTNAME_RESOLVE_TIMEOUT,
906                                                       &process_hostname_ip, h);
907 }
908
909
910 /**
911  * Task to do DNS lookup on our external hostname to
912  * get DynDNS-IP addresses.
913  *
914  * @param cls the NAT handle
915  * @param tc scheduler context
916  */
917 static void
918 resolve_dns (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
919 {
920   struct GNUNET_NAT_Handle *h = cls;
921
922   h->dns_task = GNUNET_SCHEDULER_NO_TASK;
923   remove_from_address_list_by_source (h, LAL_EXTERNAL_IP);
924   h->ext_dns = GNUNET_RESOLVER_ip_get (h->external_address,
925                                        AF_INET,
926                                        GNUNET_TIME_UNIT_MINUTES,
927                                        &process_external_ip, h);
928 }
929
930
931 /**
932  * Add or remove UPnP-mapped addresses.
933  *
934  * @param cls the GNUNET_NAT_Handle
935  * @param add_remove GNUNET_YES to mean the new public IP address, GNUNET_NO to mean
936  *     the previous (now invalid) one
937  * @param addr either the previous or the new public IP address
938  * @param addrlen actual lenght of the address
939  */
940 static void
941 upnp_add (void *cls,
942           int add_remove, const struct sockaddr *addr, socklen_t addrlen)
943 {
944   struct GNUNET_NAT_Handle *h = cls;
945   struct LocalAddressList *pos;
946   struct LocalAddressList *next;
947
948   if (GNUNET_YES == add_remove)
949   {
950     add_to_address_list (h, LAL_UPNP, addr, addrlen);
951     return;
952   }
953   /* remove address */
954   next = h->lal_head;
955   while (NULL != (pos = next))
956   {
957     next = pos->next;
958     if ((pos->source != LAL_UPNP) ||
959         (pos->addrlen != addrlen) || (0 != memcmp (&pos[1], addr, addrlen)))
960       continue;
961     GNUNET_CONTAINER_DLL_remove (h->lal_head, h->lal_tail, pos);
962     if (NULL != h->address_callback)
963       h->address_callback (h->callback_cls,
964                            GNUNET_NO,
965                            (const struct sockaddr *) &pos[1], pos->addrlen);
966     GNUNET_free (pos);
967     return;                     /* only remove once */
968   }
969   /* asked to remove address that does not exist */
970   GNUNET_break (0);
971 }
972
973
974 /**
975  * Try to add a port mapping using UPnP.
976  *
977  * @param h overall NAT handle
978  * @param port port to map with UPnP
979  */
980 static void
981 add_minis (struct GNUNET_NAT_Handle *h, uint16_t port)
982 {
983   struct MiniList *ml;
984
985   ml = h->mini_head;
986   while (NULL != ml)
987   {
988     if (port == ml->port)
989       return;                   /* already got this port */
990     ml = ml->next;
991   }
992   ml = GNUNET_malloc (sizeof (struct MiniList));
993   ml->port = port;
994   ml->mini = GNUNET_NAT_mini_map_start (port, h->is_tcp, &upnp_add, h);
995   GNUNET_CONTAINER_DLL_insert (h->mini_head, h->mini_tail, ml);
996 }
997
998
999 /**
1000  * Task to add addresses from original bind to set of valid addrs.
1001  *
1002  * @param cls the NAT handle
1003  * @param tc scheduler context
1004  */
1005 static void
1006 add_from_bind (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1007 {
1008   static struct in6_addr any = IN6ADDR_ANY_INIT;
1009   struct GNUNET_NAT_Handle *h = cls;
1010   unsigned int i;
1011   struct sockaddr *sa;
1012   const struct sockaddr_in *v4;
1013
1014   h->bind_task = GNUNET_SCHEDULER_NO_TASK;
1015   for (i = 0; i < h->num_local_addrs; i++)
1016   {
1017     sa = h->local_addrs[i];
1018     switch (sa->sa_family)
1019     {
1020     case AF_INET:
1021       if (sizeof (struct sockaddr_in) != h->local_addrlens[i])
1022       {
1023         GNUNET_break (0);
1024         break;
1025       }
1026       v4 = (const struct sockaddr_in *) sa;
1027       if (0 != v4->sin_addr.s_addr)
1028         add_to_address_list (h, LAL_BINDTO_ADDRESS, sa,
1029                              sizeof (struct sockaddr_in));
1030       if (h->enable_upnp)
1031         add_minis (h, ntohs (v4->sin_port));
1032       break;
1033     case AF_INET6:
1034       if (sizeof (struct sockaddr_in6) != h->local_addrlens[i])
1035       {
1036         GNUNET_break (0);
1037         break;
1038       }
1039       if (0 != memcmp (&((const struct sockaddr_in6 *) sa)->sin6_addr,
1040                        &any, sizeof (struct in6_addr)))
1041         add_to_address_list (h, LAL_BINDTO_ADDRESS, sa,
1042                              sizeof (struct sockaddr_in6));
1043       break;
1044     default:
1045       break;
1046     }
1047   }
1048 }
1049
1050
1051
1052 /**
1053  * Attempt to enable port redirection and detect public IP address contacting
1054  * UPnP or NAT-PMP routers on the local network. Use addr to specify to which
1055  * of the local host's addresses should the external port be mapped. The port
1056  * is taken from the corresponding sockaddr_in[6] field.
1057  *
1058  * @param cfg configuration to use
1059  * @param is_tcp GNUNET_YES for TCP, GNUNET_NO for UDP
1060  * @param adv_port advertised port (port we are either bound to or that our OS
1061  *                 locally performs redirection from to our bound port).
1062  * @param num_addrs number of addresses in 'addrs'
1063  * @param addr the local address packets should be redirected to
1064  * @param addrlen actual lenght of the address
1065  * @param address_callback function to call everytime the public IP address changes
1066  * @param reversal_callback function to call if someone wants connection reversal from us
1067  * @param callback_cls closure for callbacks
1068  * @return NULL on error, otherwise handle that can be used to unregister 
1069  */
1070 struct GNUNET_NAT_Handle *
1071 GNUNET_NAT_register (const struct GNUNET_CONFIGURATION_Handle *cfg,
1072                      int is_tcp,
1073                      uint16_t adv_port,
1074                      unsigned int num_addrs,
1075                      const struct sockaddr **addrs,
1076                      const socklen_t * addrlens,
1077                      GNUNET_NAT_AddressCallback address_callback,
1078                      GNUNET_NAT_ReversalCallback reversal_callback,
1079                      void *callback_cls)
1080 {
1081   struct GNUNET_NAT_Handle *h;
1082   struct in_addr in_addr;
1083   unsigned int i;
1084
1085 #if DEBUG_NAT
1086   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1087                    "nat",
1088                    "Registered with NAT service at port %u with %u IP bound local addresses\n",
1089                    (unsigned int) adv_port, num_addrs);
1090 #endif
1091   h = GNUNET_malloc (sizeof (struct GNUNET_NAT_Handle));
1092   h->server_retry_delay = GNUNET_TIME_UNIT_SECONDS;
1093   h->cfg = cfg;
1094   h->is_tcp = is_tcp;
1095   h->address_callback = address_callback;
1096   h->reversal_callback = reversal_callback;
1097   h->callback_cls = callback_cls;
1098   h->num_local_addrs = num_addrs;
1099   h->adv_port = adv_port;
1100   if (num_addrs != 0)
1101   {
1102     h->local_addrs = GNUNET_malloc (num_addrs * sizeof (struct sockaddr *));
1103     h->local_addrlens = GNUNET_malloc (num_addrs * sizeof (socklen_t));
1104     for (i = 0; i < num_addrs; i++)
1105     {
1106       GNUNET_assert (addrlens[i] > 0);
1107       GNUNET_assert (addrs[i] != NULL);
1108       h->local_addrlens[i] = addrlens[i];
1109       h->local_addrs[i] = GNUNET_malloc (addrlens[i]);
1110       memcpy (h->local_addrs[i], addrs[i], addrlens[i]);
1111     }
1112   }
1113   h->bind_task = GNUNET_SCHEDULER_add_now (&add_from_bind, h);
1114   if (GNUNET_OK ==
1115       GNUNET_CONFIGURATION_have_value (cfg, "nat", "INTERNAL_ADDRESS"))
1116   {
1117     (void) GNUNET_CONFIGURATION_get_value_string (cfg,
1118                                                   "nat",
1119                                                   "INTERNAL_ADDRESS",
1120                                                   &h->internal_address);
1121   }
1122   if ((h->internal_address != NULL) &&
1123       (inet_pton (AF_INET, h->internal_address, &in_addr) != 1))
1124   {
1125     GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING,
1126                      "nat",
1127                      _("Malformed %s `%s' given in configuration!\n"),
1128                      "INTERNAL_ADDRESS", h->internal_address);
1129     GNUNET_free (h->internal_address);
1130     h->internal_address = NULL;
1131   }
1132
1133   if (GNUNET_OK ==
1134       GNUNET_CONFIGURATION_have_value (cfg, "nat", "EXTERNAL_ADDRESS"))
1135   {
1136     (void) GNUNET_CONFIGURATION_get_value_string (cfg,
1137                                                   "nat",
1138                                                   "EXTERNAL_ADDRESS",
1139                                                   &h->external_address);
1140   }
1141   if ((h->external_address != NULL) &&
1142       (inet_pton (AF_INET, h->external_address, &in_addr) != 1))
1143   {
1144     GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING,
1145                      "nat",
1146                      _("Malformed %s `%s' given in configuration!\n"),
1147                      "EXTERNAL_ADDRESS", h->external_address);
1148     GNUNET_free (h->external_address);
1149     h->external_address = NULL;
1150   }
1151   h->behind_nat = GNUNET_CONFIGURATION_get_value_yesno (cfg,
1152                                                         "nat", "BEHIND_NAT");
1153   h->nat_punched = GNUNET_CONFIGURATION_get_value_yesno (cfg,
1154                                                          "nat", "PUNCHED_NAT");
1155   h->enable_nat_client = GNUNET_CONFIGURATION_get_value_yesno (cfg,
1156                                                                "nat",
1157                                                                "ENABLE_NAT_CLIENT");
1158   h->enable_nat_server = GNUNET_CONFIGURATION_get_value_yesno (cfg,
1159                                                                "nat",
1160                                                                "ENABLE_NAT_SERVER");
1161   h->enable_upnp = GNUNET_CONFIGURATION_get_value_yesno (cfg,
1162                                                          "nat", "ENABLE_UPNP");
1163   h->use_localaddresses = GNUNET_CONFIGURATION_get_value_yesno (cfg,
1164                                                                 "nat",
1165                                                                 "USE_LOCALADDR");
1166   h->use_hostname = GNUNET_CONFIGURATION_get_value_yesno (cfg,
1167                                                           "nat",
1168                                                           "USE_HOSTNAME");
1169   h->disable_ipv6 = GNUNET_CONFIGURATION_get_value_yesno (cfg,
1170                                                           "nat", "DISABLEV6");
1171   if (GNUNET_OK !=
1172       GNUNET_CONFIGURATION_get_value_time (cfg,
1173                                            "nat",
1174                                            "DYNDNS_FREQUENCY",
1175                                            &h->dyndns_frequency))
1176     h->dyndns_frequency = DYNDNS_FREQUENCY;
1177   if (GNUNET_OK !=
1178       GNUNET_CONFIGURATION_get_value_time (cfg,
1179                                            "nat",
1180                                            "IFC_SCAN_FREQUENCY",
1181                                            &h->ifc_scan_frequency))
1182     h->ifc_scan_frequency = IFC_SCAN_FREQUENCY;
1183   if (GNUNET_OK !=
1184       GNUNET_CONFIGURATION_get_value_time (cfg,
1185                                            "nat",
1186                                            "HOSTNAME_DNS_FREQUENCY",
1187                                            &h->hostname_dns_frequency))
1188     h->hostname_dns_frequency = HOSTNAME_DNS_FREQUENCY;
1189
1190   if (NULL == reversal_callback)
1191     h->enable_nat_server = GNUNET_NO;
1192
1193   /* Check if NAT was hole-punched */
1194   if ((NULL != h->address_callback) &&
1195       (h->external_address != NULL) && (h->nat_punched == GNUNET_YES))
1196   {
1197     h->dns_task = GNUNET_SCHEDULER_add_now (&resolve_dns, h);
1198     h->enable_nat_server = GNUNET_NO;
1199     h->enable_upnp = GNUNET_NO;
1200   }
1201
1202   /* Test for SUID binaries */
1203   if ((h->behind_nat == GNUNET_YES) &&
1204       (GNUNET_YES == h->enable_nat_server) &&
1205       (GNUNET_YES !=
1206        GNUNET_OS_check_helper_binary ("gnunet-helper-nat-server")))
1207   {
1208     h->enable_nat_server = GNUNET_NO;
1209     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1210                 _
1211                 ("Configuration requires `%s', but binary is not installed properly (SUID bit not set).  Option disabled.\n"),
1212                 "gnunet-helper-nat-server");
1213   }
1214   if ((GNUNET_YES == h->enable_nat_client) &&
1215       (GNUNET_YES !=
1216        GNUNET_OS_check_helper_binary ("gnunet-helper-nat-client")))
1217   {
1218     h->enable_nat_client = GNUNET_NO;
1219     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1220                 _
1221                 ("Configuration requires `%s', but binary is not installed properly (SUID bit not set).  Option disabled.\n"),
1222                 "gnunet-helper-nat-client");
1223   }
1224
1225   start_gnunet_nat_server (h);
1226
1227   /* FIXME: add support for UPnP, etc */
1228
1229   if (NULL != h->address_callback)
1230   {
1231     h->ifc_task = GNUNET_SCHEDULER_add_now (&list_interfaces, h);
1232     if (GNUNET_YES == h->use_hostname)
1233       h->hostname_task = GNUNET_SCHEDULER_add_now (&resolve_hostname, h);
1234   }
1235
1236   return h;
1237 }
1238
1239
1240 /**
1241  * Stop port redirection and public IP address detection for the given handle.
1242  * This frees the handle, after having sent the needed commands to close open ports.
1243  *
1244  * @param h the handle to stop
1245  */
1246 void
1247 GNUNET_NAT_unregister (struct GNUNET_NAT_Handle *h)
1248 {
1249   unsigned int i;
1250   struct LocalAddressList *lal;
1251   struct MiniList *ml;
1252
1253   while (NULL != (ml = h->mini_head))
1254   {
1255     GNUNET_CONTAINER_DLL_remove (h->mini_head, h->mini_tail, ml);
1256     if (NULL != ml->mini)
1257       GNUNET_NAT_mini_map_stop (ml->mini);
1258     GNUNET_free (ml);
1259   }
1260   if (h->ext_dns != NULL)
1261   {
1262     GNUNET_RESOLVER_request_cancel (h->ext_dns);
1263     h->ext_dns = NULL;
1264   }
1265   if (NULL != h->hostname_dns)
1266   {
1267     GNUNET_RESOLVER_request_cancel (h->hostname_dns);
1268     h->hostname_dns = NULL;
1269   }
1270   if (GNUNET_SCHEDULER_NO_TASK != h->server_read_task)
1271   {
1272     GNUNET_SCHEDULER_cancel (h->server_read_task);
1273     h->server_read_task = GNUNET_SCHEDULER_NO_TASK;
1274   }
1275   if (GNUNET_SCHEDULER_NO_TASK != h->bind_task)
1276   {
1277     GNUNET_SCHEDULER_cancel (h->bind_task);
1278     h->bind_task = GNUNET_SCHEDULER_NO_TASK;
1279   }
1280   if (GNUNET_SCHEDULER_NO_TASK != h->ifc_task)
1281   {
1282     GNUNET_SCHEDULER_cancel (h->ifc_task);
1283     h->ifc_task = GNUNET_SCHEDULER_NO_TASK;
1284   }
1285   if (GNUNET_SCHEDULER_NO_TASK != h->hostname_task)
1286   {
1287     GNUNET_SCHEDULER_cancel (h->hostname_task);
1288     h->hostname_task = GNUNET_SCHEDULER_NO_TASK;
1289   }
1290   if (GNUNET_SCHEDULER_NO_TASK != h->dns_task)
1291   {
1292     GNUNET_SCHEDULER_cancel (h->dns_task);
1293     h->dns_task = GNUNET_SCHEDULER_NO_TASK;
1294   }
1295   if (NULL != h->server_proc)
1296   {
1297     if (0 != GNUNET_OS_process_kill (h->server_proc, SIGTERM))
1298       GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
1299     GNUNET_OS_process_wait (h->server_proc);
1300     GNUNET_OS_process_close (h->server_proc);
1301     h->server_proc = NULL;
1302     GNUNET_DISK_pipe_close (h->server_stdout);
1303     h->server_stdout = NULL;
1304     h->server_stdout_handle = NULL;
1305   }
1306   if (NULL != h->server_stdout)
1307   {
1308     GNUNET_DISK_pipe_close (h->server_stdout);
1309     h->server_stdout = NULL;
1310     h->server_stdout_handle = NULL;
1311   }
1312   while (NULL != (lal = h->lal_head))
1313   {
1314     GNUNET_CONTAINER_DLL_remove (h->lal_head, h->lal_tail, lal);
1315     if (NULL != h->address_callback)
1316       h->address_callback (h->callback_cls,
1317                            GNUNET_NO,
1318                            (const struct sockaddr *) &lal[1], lal->addrlen);
1319     GNUNET_free (lal);
1320   }
1321   for (i = 0; i < h->num_local_addrs; i++)
1322     GNUNET_free (h->local_addrs[i]);
1323   GNUNET_free_non_null (h->local_addrs);
1324   GNUNET_free_non_null (h->local_addrlens);
1325   GNUNET_free_non_null (h->external_address);
1326   GNUNET_free_non_null (h->internal_address);
1327   GNUNET_free (h);
1328 }
1329
1330
1331 /**
1332  * We learned about a peer (possibly behind NAT) so run the
1333  * gnunet-helper-nat-client to send dummy ICMP responses to cause
1334  * that peer to connect to us (connection reversal).
1335  *
1336  * @param h NAT handle for us (largely used for configuration)
1337  * @param sa the address of the peer (IPv4-only)
1338  */
1339 void
1340 GNUNET_NAT_run_client (struct GNUNET_NAT_Handle *h,
1341                        const struct sockaddr_in *sa)
1342 {
1343   char inet4[INET_ADDRSTRLEN];
1344   char port_as_string[6];
1345   struct GNUNET_OS_Process *proc;
1346
1347   if (GNUNET_YES != h->enable_nat_client)
1348     return;                     /* not permitted / possible */
1349
1350   if (h->internal_address == NULL)
1351   {
1352     GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING,
1353                      "nat",
1354                      _
1355                      ("Internal IP address not known, cannot use ICMP NAT traversal method\n"));
1356     return;
1357   }
1358   GNUNET_assert (sa->sin_family == AF_INET);
1359   if (NULL == inet_ntop (AF_INET, &sa->sin_addr, inet4, INET_ADDRSTRLEN))
1360   {
1361     GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "inet_ntop");
1362     return;
1363   }
1364   GNUNET_snprintf (port_as_string, sizeof (port_as_string), "%d", h->adv_port);
1365 #if DEBUG_NAT
1366   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1367                    "nat",
1368                    _("Running gnunet-helper-nat-client %s %s %u\n"),
1369                    h->internal_address, inet4, (unsigned int) h->adv_port);
1370 #endif
1371   proc = GNUNET_OS_start_process (NULL,
1372                                   NULL,
1373                                   "gnunet-helper-nat-client",
1374                                   "gnunet-helper-nat-client",
1375                                   h->internal_address,
1376                                   inet4, port_as_string, NULL);
1377   if (NULL == proc)
1378     return;
1379   /* we know that the gnunet-helper-nat-client will terminate virtually
1380    * instantly */
1381   GNUNET_OS_process_wait (proc);
1382   GNUNET_OS_process_close (proc);
1383 }
1384
1385
1386 /**
1387  * Test if the given address is (currently) a plausible IP address for this peer.
1388  *
1389  * @param h the handle returned by register
1390  * @param addr IP address to test (IPv4 or IPv6)
1391  * @param addrlen number of bytes in addr
1392  * @return GNUNET_YES if the address is plausible,
1393  *         GNUNET_NO if the address is not plausible,
1394  *         GNUNET_SYSERR if the address is malformed
1395  */
1396 int
1397 GNUNET_NAT_test_address (struct GNUNET_NAT_Handle *h,
1398                          const void *addr, socklen_t addrlen)
1399 {
1400   struct LocalAddressList *pos;
1401   const struct sockaddr_in *in4;
1402   const struct sockaddr_in6 *in6;
1403
1404   if ((addrlen != sizeof (struct in_addr)) &&
1405       (addrlen != sizeof (struct in6_addr)))
1406   {
1407     GNUNET_break (0);
1408     return GNUNET_SYSERR;
1409   }
1410   pos = h->lal_head;
1411   while (NULL != pos)
1412   {
1413     if (pos->addrlen == sizeof (struct sockaddr_in))
1414     {
1415       in4 = (struct sockaddr_in *) &pos[1];
1416       if ((addrlen == sizeof (struct in_addr)) &&
1417           (0 == memcmp (&in4->sin_addr, addr, sizeof (struct in_addr))))
1418         return GNUNET_YES;
1419     }
1420     else if (pos->addrlen == sizeof (struct sockaddr_in6))
1421     {
1422       in6 = (struct sockaddr_in6 *) &pos[1];
1423       if ((addrlen == sizeof (struct in6_addr)) &&
1424           (0 == memcmp (&in6->sin6_addr, addr, sizeof (struct in6_addr))))
1425         return GNUNET_YES;
1426     }
1427     else
1428     {
1429       GNUNET_assert (0);
1430     }
1431     pos = pos->next;
1432   }
1433   GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1434               "Asked to validate one of my addresses and validation failed!\n");
1435   return GNUNET_NO;
1436 }
1437
1438
1439 /* end of nat.c */