even nicer indentation, thanks to LRN's indent patch
[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
365 start_gnunet_nat_server (struct GNUNET_NAT_Handle *h);
366
367
368 /**
369  * Remove all addresses from the list of 'local' addresses
370  * that originated from the given source.
371  * 
372  * @param plugin the plugin
373  * @param src source that identifies addresses to remove
374  */
375 static void
376 remove_from_address_list_by_source (struct GNUNET_NAT_Handle *h,
377                                     enum LocalAddressSource src)
378 {
379   struct LocalAddressList *pos;
380   struct LocalAddressList *next;
381
382   next = h->lal_head;
383   while (NULL != (pos = next))
384   {
385     next = pos->next;
386     if (pos->source != src)
387       continue;
388     GNUNET_CONTAINER_DLL_remove (h->lal_head, h->lal_tail, pos);
389     if (NULL != h->address_callback)
390       h->address_callback (h->callback_cls, GNUNET_NO,
391                            (const struct sockaddr *) &pos[1], pos->addrlen);
392     GNUNET_free (pos);
393   }
394 }
395
396
397 /**
398  * Add the given address to the list of 'local' addresses, thereby
399  * making it a 'legal' address for this peer to have.  
400  * 
401  * @param plugin the plugin
402  * @param src where did the local address originate from?
403  * @param arg the address, some 'struct sockaddr'
404  * @param arg_size number of bytes in arg
405  */
406 static void
407 add_to_address_list_as_is (struct GNUNET_NAT_Handle *h,
408                            enum LocalAddressSource src,
409                            const struct sockaddr *arg, socklen_t arg_size)
410 {
411   struct LocalAddressList *lal;
412
413   lal = GNUNET_malloc (sizeof (struct LocalAddressList) + arg_size);
414   memcpy (&lal[1], arg, arg_size);
415   lal->addrlen = arg_size;
416   lal->source = src;
417   GNUNET_CONTAINER_DLL_insert (h->lal_head, h->lal_tail, lal);
418 #if DEBUG_NAT
419   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "nat",
420                    "Adding address `%s' from source %d\n", GNUNET_a2s (arg,
421                                                                        arg_size),
422                    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 plugin the plugin
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 plugin the plugin
487  * @param src where did the local address originate from?
488  * @param arg the address, some 'struct in_addr' or 'struct in6_addr'
489  * @param arg_size number of bytes in arg
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     GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "nat",
728                      "Finished reading from server stdout with code: %d\n",
729                      bytes);
730 #endif
731     if (0 != GNUNET_OS_process_kill (h->server_proc, SIGTERM))
732       GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
733     GNUNET_OS_process_wait (h->server_proc);
734     GNUNET_OS_process_close (h->server_proc);
735     h->server_proc = NULL;
736     GNUNET_DISK_pipe_close (h->server_stdout);
737     h->server_stdout = NULL;
738     h->server_stdout_handle = NULL;
739     /* now try to restart it */
740     h->server_retry_delay =
741         GNUNET_TIME_relative_multiply (h->server_retry_delay, 2);
742     h->server_retry_delay =
743         GNUNET_TIME_relative_max (GNUNET_TIME_UNIT_HOURS,
744                                   h->server_retry_delay);
745     h->server_read_task =
746         GNUNET_SCHEDULER_add_delayed (h->server_retry_delay,
747                                       &restart_nat_server, h);
748     return;
749   }
750
751   port_start = NULL;
752   for (i = 0; i < sizeof (mybuf); i++)
753   {
754     if (mybuf[i] == '\n')
755     {
756       mybuf[i] = '\0';
757       break;
758     }
759     if ((mybuf[i] == ':') && (i + 1 < sizeof (mybuf)))
760     {
761       mybuf[i] = '\0';
762       port_start = &mybuf[i + 1];
763     }
764   }
765
766   /* construct socket address of sender */
767   memset (&sin_addr, 0, sizeof (sin_addr));
768   sin_addr.sin_family = AF_INET;
769 #if HAVE_SOCKADDR_IN_SIN_LEN
770   sin_addr.sin_len = sizeof (sin_addr);
771 #endif
772   if ((NULL == port_start) || (1 != sscanf (port_start, "%d", &port)) ||
773       (-1 == inet_pton (AF_INET, mybuf, &sin_addr.sin_addr)))
774   {
775     /* should we restart gnunet-helper-nat-server? */
776     GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, "nat",
777                      _
778                      ("gnunet-helper-nat-server generated malformed address `%s'\n"),
779                      mybuf);
780     h->server_read_task =
781         GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
782                                         h->server_stdout_handle,
783                                         &nat_server_read, h);
784     return;
785   }
786   sin_addr.sin_port = htons ((uint16_t) port);
787 #if DEBUG_NAT
788   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "nat",
789                    "gnunet-helper-nat-server read: %s:%d\n", mybuf, port);
790 #endif
791   h->reversal_callback (h->callback_cls, (const struct sockaddr *) &sin_addr,
792                         sizeof (sin_addr));
793   h->server_read_task =
794       GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
795                                       h->server_stdout_handle, &nat_server_read,
796                                       h);
797 }
798
799
800 /**
801  * Try to start the gnunet-helper-nat-server (if it is not
802  * already running).
803  *
804  * @param h handle to NAT
805  */
806 static void
807 start_gnunet_nat_server (struct GNUNET_NAT_Handle *h)
808 {
809   if ((h->behind_nat == GNUNET_YES) && (h->enable_nat_server == GNUNET_YES) &&
810       (h->internal_address != NULL) &&
811       (NULL !=
812        (h->server_stdout =
813         GNUNET_DISK_pipe (GNUNET_YES, GNUNET_NO, GNUNET_YES))))
814   {
815 #if DEBUG_NAT
816     GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "nat" "Starting %s at `%s'\n",
817                      "gnunet-helper-nat-server", h->internal_address);
818 #endif
819     /* Start the server process */
820     h->server_proc =
821         GNUNET_OS_start_process (NULL, h->server_stdout,
822                                  "gnunet-helper-nat-server",
823                                  "gnunet-helper-nat-server",
824                                  h->internal_address, NULL);
825     if (h->server_proc == NULL)
826     {
827       GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, "nat",
828                        _("Failed to start %s\n"), "gnunet-helper-nat-server");
829       GNUNET_DISK_pipe_close (h->server_stdout);
830       h->server_stdout = NULL;
831     }
832     else
833     {
834       /* Close the write end of the read pipe */
835       GNUNET_DISK_pipe_close_end (h->server_stdout, GNUNET_DISK_PIPE_END_WRITE);
836       h->server_stdout_handle =
837           GNUNET_DISK_pipe_handle (h->server_stdout, GNUNET_DISK_PIPE_END_READ);
838       h->server_read_task =
839           GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
840                                           h->server_stdout_handle,
841                                           &nat_server_read, h);
842     }
843   }
844 }
845
846
847 /**
848  * Task to scan the local network interfaces for IP addresses.
849  *
850  * @param cls the NAT handle
851  * @param tc scheduler context
852  */
853 static void
854 list_interfaces (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
855 {
856   struct GNUNET_NAT_Handle *h = cls;
857
858   h->ifc_task = GNUNET_SCHEDULER_NO_TASK;
859   remove_from_address_list_by_source (h, LAL_INTERFACE_ADDRESS);
860   GNUNET_OS_network_interfaces_list (&process_interfaces, h);
861   h->ifc_task =
862       GNUNET_SCHEDULER_add_delayed (h->ifc_scan_frequency, &list_interfaces, h);
863 }
864
865
866 /**
867  * Task to do a lookup on our hostname for IP addresses.
868  *
869  * @param cls the NAT handle
870  * @param tc scheduler context
871  */
872 static void
873 resolve_hostname (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
874 {
875   struct GNUNET_NAT_Handle *h = cls;
876
877   h->hostname_task = GNUNET_SCHEDULER_NO_TASK;
878   remove_from_address_list_by_source (h, LAL_HOSTNAME_DNS);
879   h->hostname_dns =
880       GNUNET_RESOLVER_hostname_resolve (AF_UNSPEC, HOSTNAME_RESOLVE_TIMEOUT,
881                                         &process_hostname_ip, h);
882 }
883
884
885 /**
886  * Task to do DNS lookup on our external hostname to
887  * get DynDNS-IP addresses.
888  *
889  * @param cls the NAT handle
890  * @param tc scheduler context
891  */
892 static void
893 resolve_dns (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
894 {
895   struct GNUNET_NAT_Handle *h = cls;
896
897   h->dns_task = GNUNET_SCHEDULER_NO_TASK;
898   remove_from_address_list_by_source (h, LAL_EXTERNAL_IP);
899   h->ext_dns =
900       GNUNET_RESOLVER_ip_get (h->external_address, AF_INET,
901                               GNUNET_TIME_UNIT_MINUTES, &process_external_ip,
902                               h);
903 }
904
905
906 /**
907  * Add or remove UPnP-mapped addresses.
908  *
909  * @param cls the GNUNET_NAT_Handle
910  * @param add_remove GNUNET_YES to mean the new public IP address, GNUNET_NO to mean
911  *     the previous (now invalid) one
912  * @param addr either the previous or the new public IP address
913  * @param addrlen actual lenght of the address
914  */
915 static void
916 upnp_add (void *cls, int add_remove, const struct sockaddr *addr,
917           socklen_t addrlen)
918 {
919   struct GNUNET_NAT_Handle *h = cls;
920   struct LocalAddressList *pos;
921   struct LocalAddressList *next;
922
923   if (GNUNET_YES == add_remove)
924   {
925     add_to_address_list (h, LAL_UPNP, addr, addrlen);
926     return;
927   }
928   /* remove address */
929   next = h->lal_head;
930   while (NULL != (pos = next))
931   {
932     next = pos->next;
933     if ((pos->source != LAL_UPNP) || (pos->addrlen != addrlen) ||
934         (0 != memcmp (&pos[1], addr, addrlen)))
935       continue;
936     GNUNET_CONTAINER_DLL_remove (h->lal_head, h->lal_tail, pos);
937     if (NULL != h->address_callback)
938       h->address_callback (h->callback_cls, GNUNET_NO,
939                            (const struct sockaddr *) &pos[1], pos->addrlen);
940     GNUNET_free (pos);
941     return;                     /* only remove once */
942   }
943   /* asked to remove address that does not exist */
944   GNUNET_break (0);
945 }
946
947
948 /**
949  * Try to add a port mapping using UPnP.
950  *
951  * @param h overall NAT handle
952  * @param port port to map with UPnP
953  */
954 static void
955 add_minis (struct GNUNET_NAT_Handle *h, uint16_t port)
956 {
957   struct MiniList *ml;
958
959   ml = h->mini_head;
960   while (NULL != ml)
961   {
962     if (port == ml->port)
963       return;                   /* already got this port */
964     ml = ml->next;
965   }
966   ml = GNUNET_malloc (sizeof (struct MiniList));
967   ml->port = port;
968   ml->mini = GNUNET_NAT_mini_map_start (port, h->is_tcp, &upnp_add, h);
969   GNUNET_CONTAINER_DLL_insert (h->mini_head, h->mini_tail, ml);
970 }
971
972
973 /**
974  * Task to add addresses from original bind to set of valid addrs.
975  *
976  * @param cls the NAT handle
977  * @param tc scheduler context
978  */
979 static void
980 add_from_bind (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
981 {
982   static struct in6_addr any = IN6ADDR_ANY_INIT;
983   struct GNUNET_NAT_Handle *h = cls;
984   unsigned int i;
985   struct sockaddr *sa;
986   const struct sockaddr_in *v4;
987
988   h->bind_task = GNUNET_SCHEDULER_NO_TASK;
989   for (i = 0; i < h->num_local_addrs; i++)
990   {
991     sa = h->local_addrs[i];
992     switch (sa->sa_family)
993     {
994     case AF_INET:
995       if (sizeof (struct sockaddr_in) != h->local_addrlens[i])
996       {
997         GNUNET_break (0);
998         break;
999       }
1000       v4 = (const struct sockaddr_in *) sa;
1001       if (0 != v4->sin_addr.s_addr)
1002         add_to_address_list (h, LAL_BINDTO_ADDRESS, sa,
1003                              sizeof (struct sockaddr_in));
1004       if (h->enable_upnp)
1005         add_minis (h, ntohs (v4->sin_port));
1006       break;
1007     case AF_INET6:
1008       if (sizeof (struct sockaddr_in6) != h->local_addrlens[i])
1009       {
1010         GNUNET_break (0);
1011         break;
1012       }
1013       if (0 !=
1014           memcmp (&((const struct sockaddr_in6 *) sa)->sin6_addr, &any,
1015                   sizeof (struct in6_addr)))
1016         add_to_address_list (h, LAL_BINDTO_ADDRESS, sa,
1017                              sizeof (struct sockaddr_in6));
1018       break;
1019     default:
1020       break;
1021     }
1022   }
1023 }
1024
1025
1026
1027 /**
1028  * Attempt to enable port redirection and detect public IP address contacting
1029  * UPnP or NAT-PMP routers on the local network. Use addr to specify to which
1030  * of the local host's addresses should the external port be mapped. The port
1031  * is taken from the corresponding sockaddr_in[6] field.
1032  *
1033  * @param cfg configuration to use
1034  * @param is_tcp GNUNET_YES for TCP, GNUNET_NO for UDP
1035  * @param adv_port advertised port (port we are either bound to or that our OS
1036  *                 locally performs redirection from to our bound port).
1037  * @param num_addrs number of addresses in 'addrs'
1038  * @param addr the local address packets should be redirected to
1039  * @param addrlen actual lenght of the address
1040  * @param address_callback function to call everytime the public IP address changes
1041  * @param reversal_callback function to call if someone wants connection reversal from us
1042  * @param callback_cls closure for callbacks
1043  * @return NULL on error, otherwise handle that can be used to unregister 
1044  */
1045 struct GNUNET_NAT_Handle *
1046 GNUNET_NAT_register (const struct GNUNET_CONFIGURATION_Handle *cfg, int is_tcp,
1047                      uint16_t adv_port, unsigned int num_addrs,
1048                      const struct sockaddr **addrs, 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 #if DEBUG_NAT
1058   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "nat",
1059                    "Registered with NAT service at port %u with %u IP bound local addresses\n",
1060                    (unsigned int) adv_port, num_addrs);
1061 #endif
1062   h = GNUNET_malloc (sizeof (struct GNUNET_NAT_Handle));
1063   h->server_retry_delay = GNUNET_TIME_UNIT_SECONDS;
1064   h->cfg = cfg;
1065   h->is_tcp = is_tcp;
1066   h->address_callback = address_callback;
1067   h->reversal_callback = reversal_callback;
1068   h->callback_cls = callback_cls;
1069   h->num_local_addrs = num_addrs;
1070   h->adv_port = adv_port;
1071   if (num_addrs != 0)
1072   {
1073     h->local_addrs = GNUNET_malloc (num_addrs * sizeof (struct sockaddr *));
1074     h->local_addrlens = GNUNET_malloc (num_addrs * sizeof (socklen_t));
1075     for (i = 0; i < num_addrs; i++)
1076     {
1077       GNUNET_assert (addrlens[i] > 0);
1078       GNUNET_assert (addrs[i] != NULL);
1079       h->local_addrlens[i] = addrlens[i];
1080       h->local_addrs[i] = GNUNET_malloc (addrlens[i]);
1081       memcpy (h->local_addrs[i], addrs[i], addrlens[i]);
1082     }
1083   }
1084   h->bind_task = GNUNET_SCHEDULER_add_now (&add_from_bind, h);
1085   if (GNUNET_OK ==
1086       GNUNET_CONFIGURATION_have_value (cfg, "nat", "INTERNAL_ADDRESS"))
1087   {
1088     (void) GNUNET_CONFIGURATION_get_value_string (cfg, "nat",
1089                                                   "INTERNAL_ADDRESS",
1090                                                   &h->internal_address);
1091   }
1092   if ((h->internal_address != NULL) &&
1093       (inet_pton (AF_INET, h->internal_address, &in_addr) != 1))
1094   {
1095     GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, "nat",
1096                      _("Malformed %s `%s' given in configuration!\n"),
1097                      "INTERNAL_ADDRESS", h->internal_address);
1098     GNUNET_free (h->internal_address);
1099     h->internal_address = NULL;
1100   }
1101
1102   if (GNUNET_OK ==
1103       GNUNET_CONFIGURATION_have_value (cfg, "nat", "EXTERNAL_ADDRESS"))
1104   {
1105     (void) GNUNET_CONFIGURATION_get_value_string (cfg, "nat",
1106                                                   "EXTERNAL_ADDRESS",
1107                                                   &h->external_address);
1108   }
1109   if ((h->external_address != NULL) &&
1110       (inet_pton (AF_INET, h->external_address, &in_addr) != 1))
1111   {
1112     GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, "nat",
1113                      _("Malformed %s `%s' given in configuration!\n"),
1114                      "EXTERNAL_ADDRESS", h->external_address);
1115     GNUNET_free (h->external_address);
1116     h->external_address = NULL;
1117   }
1118   h->behind_nat =
1119       GNUNET_CONFIGURATION_get_value_yesno (cfg, "nat", "BEHIND_NAT");
1120   h->nat_punched =
1121       GNUNET_CONFIGURATION_get_value_yesno (cfg, "nat", "PUNCHED_NAT");
1122   h->enable_nat_client =
1123       GNUNET_CONFIGURATION_get_value_yesno (cfg, "nat", "ENABLE_NAT_CLIENT");
1124   h->enable_nat_server =
1125       GNUNET_CONFIGURATION_get_value_yesno (cfg, "nat", "ENABLE_NAT_SERVER");
1126   h->enable_upnp =
1127       GNUNET_CONFIGURATION_get_value_yesno (cfg, "nat", "ENABLE_UPNP");
1128   h->use_localaddresses =
1129       GNUNET_CONFIGURATION_get_value_yesno (cfg, "nat", "USE_LOCALADDR");
1130   h->use_hostname =
1131       GNUNET_CONFIGURATION_get_value_yesno (cfg, "nat", "USE_HOSTNAME");
1132   h->disable_ipv6 =
1133       GNUNET_CONFIGURATION_get_value_yesno (cfg, "nat", "DISABLEV6");
1134   if (GNUNET_OK !=
1135       GNUNET_CONFIGURATION_get_value_time (cfg, "nat", "DYNDNS_FREQUENCY",
1136                                            &h->dyndns_frequency))
1137     h->dyndns_frequency = DYNDNS_FREQUENCY;
1138   if (GNUNET_OK !=
1139       GNUNET_CONFIGURATION_get_value_time (cfg, "nat", "IFC_SCAN_FREQUENCY",
1140                                            &h->ifc_scan_frequency))
1141     h->ifc_scan_frequency = IFC_SCAN_FREQUENCY;
1142   if (GNUNET_OK !=
1143       GNUNET_CONFIGURATION_get_value_time (cfg, "nat", "HOSTNAME_DNS_FREQUENCY",
1144                                            &h->hostname_dns_frequency))
1145     h->hostname_dns_frequency = HOSTNAME_DNS_FREQUENCY;
1146
1147   if (NULL == reversal_callback)
1148     h->enable_nat_server = GNUNET_NO;
1149
1150   /* Check if NAT was hole-punched */
1151   if ((NULL != h->address_callback) && (h->external_address != NULL) &&
1152       (h->nat_punched == GNUNET_YES))
1153   {
1154     h->dns_task = GNUNET_SCHEDULER_add_now (&resolve_dns, h);
1155     h->enable_nat_server = GNUNET_NO;
1156     h->enable_upnp = GNUNET_NO;
1157   }
1158
1159   /* Test for SUID binaries */
1160   if ((h->behind_nat == GNUNET_YES) && (GNUNET_YES == h->enable_nat_server) &&
1161       (GNUNET_YES !=
1162        GNUNET_OS_check_helper_binary ("gnunet-helper-nat-server")))
1163   {
1164     h->enable_nat_server = GNUNET_NO;
1165     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1166                 _
1167                 ("Configuration requires `%s', but binary is not installed properly (SUID bit not set).  Option disabled.\n"),
1168                 "gnunet-helper-nat-server");
1169   }
1170   if ((GNUNET_YES == h->enable_nat_client) &&
1171       (GNUNET_YES !=
1172        GNUNET_OS_check_helper_binary ("gnunet-helper-nat-client")))
1173   {
1174     h->enable_nat_client = GNUNET_NO;
1175     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1176                 _
1177                 ("Configuration requires `%s', but binary is not installed properly (SUID bit not set).  Option disabled.\n"),
1178                 "gnunet-helper-nat-client");
1179   }
1180
1181   start_gnunet_nat_server (h);
1182
1183   /* FIXME: add support for UPnP, etc */
1184
1185   if (NULL != h->address_callback)
1186   {
1187     h->ifc_task = GNUNET_SCHEDULER_add_now (&list_interfaces, h);
1188     if (GNUNET_YES == h->use_hostname)
1189       h->hostname_task = GNUNET_SCHEDULER_add_now (&resolve_hostname, h);
1190   }
1191
1192   return h;
1193 }
1194
1195
1196 /**
1197  * Stop port redirection and public IP address detection for the given handle.
1198  * This frees the handle, after having sent the needed commands to close open ports.
1199  *
1200  * @param h the handle to stop
1201  */
1202 void
1203 GNUNET_NAT_unregister (struct GNUNET_NAT_Handle *h)
1204 {
1205   unsigned int i;
1206   struct LocalAddressList *lal;
1207   struct MiniList *ml;
1208
1209   while (NULL != (ml = h->mini_head))
1210   {
1211     GNUNET_CONTAINER_DLL_remove (h->mini_head, h->mini_tail, ml);
1212     if (NULL != ml->mini)
1213       GNUNET_NAT_mini_map_stop (ml->mini);
1214     GNUNET_free (ml);
1215   }
1216   if (h->ext_dns != NULL)
1217   {
1218     GNUNET_RESOLVER_request_cancel (h->ext_dns);
1219     h->ext_dns = NULL;
1220   }
1221   if (NULL != h->hostname_dns)
1222   {
1223     GNUNET_RESOLVER_request_cancel (h->hostname_dns);
1224     h->hostname_dns = NULL;
1225   }
1226   if (GNUNET_SCHEDULER_NO_TASK != h->server_read_task)
1227   {
1228     GNUNET_SCHEDULER_cancel (h->server_read_task);
1229     h->server_read_task = GNUNET_SCHEDULER_NO_TASK;
1230   }
1231   if (GNUNET_SCHEDULER_NO_TASK != h->bind_task)
1232   {
1233     GNUNET_SCHEDULER_cancel (h->bind_task);
1234     h->bind_task = GNUNET_SCHEDULER_NO_TASK;
1235   }
1236   if (GNUNET_SCHEDULER_NO_TASK != h->ifc_task)
1237   {
1238     GNUNET_SCHEDULER_cancel (h->ifc_task);
1239     h->ifc_task = GNUNET_SCHEDULER_NO_TASK;
1240   }
1241   if (GNUNET_SCHEDULER_NO_TASK != h->hostname_task)
1242   {
1243     GNUNET_SCHEDULER_cancel (h->hostname_task);
1244     h->hostname_task = GNUNET_SCHEDULER_NO_TASK;
1245   }
1246   if (GNUNET_SCHEDULER_NO_TASK != h->dns_task)
1247   {
1248     GNUNET_SCHEDULER_cancel (h->dns_task);
1249     h->dns_task = GNUNET_SCHEDULER_NO_TASK;
1250   }
1251   if (NULL != h->server_proc)
1252   {
1253     if (0 != GNUNET_OS_process_kill (h->server_proc, SIGTERM))
1254       GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
1255     GNUNET_OS_process_wait (h->server_proc);
1256     GNUNET_OS_process_close (h->server_proc);
1257     h->server_proc = NULL;
1258     GNUNET_DISK_pipe_close (h->server_stdout);
1259     h->server_stdout = NULL;
1260     h->server_stdout_handle = NULL;
1261   }
1262   if (NULL != h->server_stdout)
1263   {
1264     GNUNET_DISK_pipe_close (h->server_stdout);
1265     h->server_stdout = NULL;
1266     h->server_stdout_handle = NULL;
1267   }
1268   while (NULL != (lal = h->lal_head))
1269   {
1270     GNUNET_CONTAINER_DLL_remove (h->lal_head, h->lal_tail, lal);
1271     if (NULL != h->address_callback)
1272       h->address_callback (h->callback_cls, GNUNET_NO,
1273                            (const struct sockaddr *) &lal[1], lal->addrlen);
1274     GNUNET_free (lal);
1275   }
1276   for (i = 0; i < h->num_local_addrs; i++)
1277     GNUNET_free (h->local_addrs[i]);
1278   GNUNET_free_non_null (h->local_addrs);
1279   GNUNET_free_non_null (h->local_addrlens);
1280   GNUNET_free_non_null (h->external_address);
1281   GNUNET_free_non_null (h->internal_address);
1282   GNUNET_free (h);
1283 }
1284
1285
1286 /**
1287  * We learned about a peer (possibly behind NAT) so run the
1288  * gnunet-helper-nat-client to send dummy ICMP responses to cause
1289  * that peer to connect to us (connection reversal).
1290  *
1291  * @param h NAT handle for us (largely used for configuration)
1292  * @param sa the address of the peer (IPv4-only)
1293  */
1294 void
1295 GNUNET_NAT_run_client (struct GNUNET_NAT_Handle *h,
1296                        const struct sockaddr_in *sa)
1297 {
1298   char inet4[INET_ADDRSTRLEN];
1299   char port_as_string[6];
1300   struct GNUNET_OS_Process *proc;
1301
1302   if (GNUNET_YES != h->enable_nat_client)
1303     return;                     /* not permitted / possible */
1304
1305   if (h->internal_address == NULL)
1306   {
1307     GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, "nat",
1308                      _
1309                      ("Internal IP address not known, cannot use ICMP NAT traversal method\n"));
1310     return;
1311   }
1312   GNUNET_assert (sa->sin_family == AF_INET);
1313   if (NULL == inet_ntop (AF_INET, &sa->sin_addr, inet4, INET_ADDRSTRLEN))
1314   {
1315     GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "inet_ntop");
1316     return;
1317   }
1318   GNUNET_snprintf (port_as_string, sizeof (port_as_string), "%d", h->adv_port);
1319 #if DEBUG_NAT
1320   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "nat",
1321                    _("Running gnunet-helper-nat-client %s %s %u\n"),
1322                    h->internal_address, inet4, (unsigned int) h->adv_port);
1323 #endif
1324   proc =
1325       GNUNET_OS_start_process (NULL, NULL, "gnunet-helper-nat-client",
1326                                "gnunet-helper-nat-client", h->internal_address,
1327                                inet4, port_as_string, NULL);
1328   if (NULL == proc)
1329     return;
1330   /* we know that the gnunet-helper-nat-client will terminate virtually
1331    * instantly */
1332   GNUNET_OS_process_wait (proc);
1333   GNUNET_OS_process_close (proc);
1334 }
1335
1336
1337 /**
1338  * Test if the given address is (currently) a plausible IP address for this peer.
1339  *
1340  * @param h the handle returned by register
1341  * @param addr IP address to test (IPv4 or IPv6)
1342  * @param addrlen number of bytes in addr
1343  * @return GNUNET_YES if the address is plausible,
1344  *         GNUNET_NO if the address is not plausible,
1345  *         GNUNET_SYSERR if the address is malformed
1346  */
1347 int
1348 GNUNET_NAT_test_address (struct GNUNET_NAT_Handle *h, const void *addr,
1349                          socklen_t addrlen)
1350 {
1351   struct LocalAddressList *pos;
1352   const struct sockaddr_in *in4;
1353   const struct sockaddr_in6 *in6;
1354
1355   if ((addrlen != sizeof (struct in_addr)) &&
1356       (addrlen != sizeof (struct in6_addr)))
1357   {
1358     GNUNET_break (0);
1359     return GNUNET_SYSERR;
1360   }
1361   pos = h->lal_head;
1362   while (NULL != pos)
1363   {
1364     if (pos->addrlen == sizeof (struct sockaddr_in))
1365     {
1366       in4 = (struct sockaddr_in *) &pos[1];
1367       if ((addrlen == sizeof (struct in_addr)) &&
1368           (0 == memcmp (&in4->sin_addr, addr, sizeof (struct in_addr))))
1369         return GNUNET_YES;
1370     }
1371     else if (pos->addrlen == sizeof (struct sockaddr_in6))
1372     {
1373       in6 = (struct sockaddr_in6 *) &pos[1];
1374       if ((addrlen == sizeof (struct in6_addr)) &&
1375           (0 == memcmp (&in6->sin6_addr, addr, sizeof (struct in6_addr))))
1376         return GNUNET_YES;
1377     }
1378     else
1379     {
1380       GNUNET_assert (0);
1381     }
1382     pos = pos->next;
1383   }
1384   GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1385               "Asked to validate one of my addresses and validation failed!\n");
1386   return GNUNET_NO;
1387 }
1388
1389
1390 /* end of nat.c */