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