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