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