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