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