REST: nothing triggers rest
[oweals/gnunet.git] / src / nat / gnunet-service-nat.c
index 9a87c0586a6def4807797b148df7f1715e7f1b30..8d771f474d8706748363ea522b3ab092c5857cd9 100644 (file)
@@ -2,20 +2,20 @@
   This file is part of GNUnet.
   Copyright (C) 2016, 2017 GNUnet e.V.
 
-  GNUnet is free software; you can redistribute it and/or modify
-  it under the terms of the GNU General Public License as published
-  by the Free Software Foundation; either version 3, or (at your
-  option) any later version.
+  GNUnet is free software: you can redistribute it and/or modify it
+  under the terms of the GNU Affero General Public License as published
+  by the Free Software Foundation, either version 3 of the License,
+  or (at your option) any later version.
 
   GNUnet is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
-  General Public License for more details.
+  Affero General Public License for more details.
+  You should have received a copy of the GNU Affero General Public License
+  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
-  You should have received a copy of the GNU General Public License
-  along with GNUnet; see the file COPYING.  If not, write to the
-  Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
-  Boston, MA 02110-1301, USA.
+     SPDX-License-Identifier: AGPL3.0-or-later
  */
 
 /**
  * @brief network address translation traversal service
  * @author Christian Grothoff
  *
- * The purpose of this service is to enable transports to 
+ * The purpose of this service is to enable transports to
  * traverse NAT routers, by providing traversal options and
  * knowledge about the local network topology.
  *
  * TODO:
- * - test and document (!) ICMP based NAT traversal
- * - test manual hole punching support
- * - implement "more" autoconfig:
- *   + consider moving autoconfig-logic into separate service! 
- *   + re-work gnunet-nat-server & integrate!
- *   + test manually punched NAT (how?)
+ * - migrate test cases to new NAT service
+ * - add new traceroute-based logic for external IP detection
+ *
  * - implement & test STUN processing to classify NAT;
  *   basically, open port & try different methods.
  */
@@ -72,7 +69,7 @@
 
 
 /**
- * Information we track per client address. 
+ * Information we track per client address.
  */
 struct ClientAddress
 {
@@ -87,7 +84,7 @@ struct ClientAddress
    * pending.
    */
   struct GNUNET_NAT_MiniHandle *mh;
-  
+
 };
 
 
@@ -111,7 +108,7 @@ struct LocalAddressList
    * for ICMP messages to this client for connection reversal.
    */
   struct HelperContext *hc;
-  
+
   /**
    * The address itself (i.e. `struct sockaddr_in` or `struct
    * sockaddr_in6`, in the respective byte order).
@@ -122,7 +119,7 @@ struct LocalAddressList
    * Address family. (FIXME: redundant, addr.ss_family! Remove!?)
    */
   int af;
-  
+
   /**
    * #GNUNET_YES if we saw this one in the previous iteration,
    * but not in the current iteration and thus might need to
@@ -134,7 +131,7 @@ struct LocalAddressList
    * What type of address is this?
    */
   enum GNUNET_NAT_AddressClass ac;
-  
+
 };
 
 
@@ -148,7 +145,7 @@ struct ClientHandle
    * Kept in a DLL.
    */
   struct ClientHandle *next;
-  
+
   /**
    * Kept in a DLL.
    */
@@ -156,7 +153,7 @@ struct ClientHandle
 
   /**
    * Underlying handle for this client with the service.
-   */ 
+   */
   struct GNUNET_SERVICE_Client *client;
 
   /**
@@ -172,18 +169,23 @@ struct ClientHandle
   /**
    * External DNS name and port given by user due to manual
    * hole punching.  Special DNS name 'AUTO' is used to indicate
-   * desire for automatic determination of the external IP 
-   * (instead of DNS or manual configuration, i.e. to be used 
+   * desire for automatic determination of the external IP
+   * (instead of DNS or manual configuration, i.e. to be used
    * if the IP keeps changing and we have no DynDNS, but we do
    * have a hole punched).
    */
   char *hole_external;
 
+  /**
+   * Name of the configuration section this client cares about.
+   */
+  char *section_name;
+
   /**
    * Task for periodically re-running the @e ext_dns DNS lookup.
-   */ 
+   */
   struct GNUNET_SCHEDULER_Task *ext_dns_task;
-  
+
   /**
    * Handle for (DYN)DNS lookup of our external IP as given in
    * @e hole_external.
@@ -194,7 +196,7 @@ struct ClientHandle
    * Handle for monitoring external IP changes.
    */
   struct GN_ExternalIPMonitor *external_monitor;
-  
+
   /**
    * DLL of external IP addresses as given in @e hole_external.
    */
@@ -207,9 +209,9 @@ struct ClientHandle
 
   /**
    * Port number we found in @e hole_external.
-   */ 
+   */
   uint16_t ext_dns_port;
-  
+
   /**
    * What does this client care about?
    */
@@ -219,13 +221,13 @@ struct ClientHandle
    * Is any of the @e caddrs in a reserved subnet for NAT?
    */
   int natted_address;
-  
+
   /**
    * Number of addresses that this service is bound to.
    * Length of the @e caddrs array.
    */
   uint16_t num_caddrs;
-  
+
   /**
    * Client's IPPROTO, e.g. IPPROTO_UDP or IPPROTO_TCP.
    */
@@ -241,12 +243,12 @@ struct StunExternalIP
 {
   /**
    * Kept in a DLL.
-   */ 
+   */
   struct StunExternalIP *next;
 
   /**
    * Kept in a DLL.
-   */ 
+   */
   struct StunExternalIP *prev;
 
   /**
@@ -255,13 +257,13 @@ struct StunExternalIP
   struct GNUNET_SCHEDULER_Task *timeout_task;
 
   /**
-   * Our external IP address as reported by the 
+   * Our external IP address as reported by the
    * STUN server.
    */
   struct sockaddr_in external_addr;
 
   /**
-   * Address of the reporting STUN server.  Used to 
+   * Address of the reporting STUN server.  Used to
    * detect when a STUN server changes its opinion
    * to more quickly remove stale results.
    */
@@ -274,80 +276,6 @@ struct StunExternalIP
 };
 
 
-/**
- * Context for autoconfiguration operations.
- */
-struct AutoconfigContext
-{
-  /**
-   * Kept in a DLL.
-   */
-  struct AutoconfigContext *prev;
-
-  /**
-   * Kept in a DLL.
-   */
-  struct AutoconfigContext *next;
-
-  /**
-   * Which client asked the question.
-   */
-  struct ClientHandle *ch;
-
-  /**
-   * Configuration we are creating.
-   */ 
-  struct GNUNET_CONFIGURATION_Handle *c;
-
-  /**
-   * Original configuration (for diffing).
-   */ 
-  struct GNUNET_CONFIGURATION_Handle *orig;
-
-  /**
-   * Timeout task to force termination.
-   */
-  struct GNUNET_SCHEDULER_Task *timeout_task;
-
-  /**
-   * What type of system are we on?
-   */
-  char *system_type;
-
-  /**
-   * Handle to activity to probe for our external IP.
-   */
-  struct GNUNET_NAT_ExternalHandle *probe_external;
-
-  /**
-   * #GNUNET_YES if upnpc should be used,
-   * #GNUNET_NO if upnpc should not be used,
-   * #GNUNET_SYSERR if we should simply not change the option.
-   */
-  int enable_upnpc;
-
-  /**
-   * Status code to return to the client.
-   */
-  enum GNUNET_NAT_StatusCode status_code;
-
-  /**
-   * NAT type to return to the client.
-   */
-  enum GNUNET_NAT_Type type;
-};
-
-
-/**
- * DLL of our autoconfiguration operations.
- */
-static struct AutoconfigContext *ac_head;
-
-/**
- * DLL of our autoconfiguration operations.
- */
-static struct AutoconfigContext *ac_tail;
-
 /**
  * Timeout to use when STUN data is considered stale.
  */
@@ -377,7 +305,7 @@ static struct GNUNET_SCHEDULER_Task *scan_task;
  * Head of client DLL.
  */
 static struct ClientHandle *ch_head;
-  
+
 /**
  * Tail of client DLL.
  */
@@ -395,12 +323,12 @@ static struct LocalAddressList *lal_tail;
 
 /**
  * Kept in a DLL.
- */ 
+ */
 static struct StunExternalIP *se_head;
 
 /**
  * Kept in a DLL.
- */ 
+ */
 static struct StunExternalIP *se_tail;
 
 /**
@@ -437,7 +365,7 @@ free_lal (struct LocalAddressList *lal)
 
 /**
  * Free the DLL starting at #lal_head.
- */ 
+ */
 static void
 destroy_lal ()
 {
@@ -489,22 +417,22 @@ check_register (void *cls,
 #endif
     default:
       GNUNET_break (0);
-      return GNUNET_SYSERR;      
+      return GNUNET_SYSERR;
     }
     if (alen > left)
     {
       GNUNET_break (0);
-      return GNUNET_SYSERR;      
+      return GNUNET_SYSERR;
     }
     off += alen;
     left -= alen;
   }
-  if (left != ntohs (message->hole_external_len))
+  if (left != ntohs (message->str_len))
   {
     GNUNET_break (0);
-    return GNUNET_SYSERR;      
+    return GNUNET_SYSERR;
   }
-  return GNUNET_OK; 
+  return GNUNET_OK;
 }
 
 
@@ -550,16 +478,15 @@ match_ipv6 (const char *network,
   struct in6_addr net;
   struct in6_addr mask;
   unsigned int off;
-  
+
   if (0 == bits)
     return GNUNET_YES;
   GNUNET_assert (1 == inet_pton (AF_INET6,
                                 network,
                                 &net));
   memset (&mask, 0, sizeof (mask));
-  if (0 == memcmp (&mask,
-                  ip,
-                  sizeof (mask)))
+  if (0 == GNUNET_memcmp (&mask,
+                  ip))
     return GNUNET_YES;
   off = 0;
   while (bits > 8)
@@ -622,7 +549,7 @@ is_nat_v6 (const struct in6_addr *ip)
 struct IfcProcContext
 {
 
-  /** 
+  /**
    * Head of DLL of local addresses.
    */
   struct LocalAddressList *lal_head;
@@ -733,7 +660,12 @@ notify_client (enum GNUNET_NAT_AddressClass ac,
 {
   struct GNUNET_MQ_Envelope *env;
   struct GNUNET_NAT_AddressChangeNotificationMessage *msg;
-  
+
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+              "Notifying client about %s of IP %s\n",
+              add ? "addition" : "removal",
+              GNUNET_a2s (addr,
+                          addr_len));
   env = GNUNET_MQ_msg_extra (msg,
                             addr_len,
                             GNUNET_MESSAGE_TYPE_NAT_ADDRESS_CHANGE);
@@ -744,7 +676,7 @@ notify_client (enum GNUNET_NAT_AddressClass ac,
                 addr_len);
   GNUNET_MQ_send (ch->mq,
                  env);
-}                     
+}
 
 
 /**
@@ -763,9 +695,13 @@ check_notify_client (struct LocalAddressList *delta,
   size_t alen;
   struct sockaddr_in v4;
   struct sockaddr_in6 v6;
-  
+
   if (0 == (ch->flags & GNUNET_NAT_RF_ADDRESSES))
+  {
+    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                "Not notifying client as it does not care about addresses\n");
     return;
+  }
   switch (delta->af)
   {
   case AF_INET:
@@ -773,7 +709,7 @@ check_notify_client (struct LocalAddressList *delta,
     GNUNET_memcpy (&v4,
                   &delta->addr,
                   alen);
-    
+
     /* Check for client notifications */
     for (unsigned int i=0;i<ch->num_caddrs;i++)
     {
@@ -787,25 +723,23 @@ check_notify_client (struct LocalAddressList *delta,
           (! match_ipv4 ("127.0.0.1", &v4.sin_addr, 8)) )
        continue; /* bound to loopback, but this is not loopback */
       if ( (! match_ipv4 ("127.0.0.1", &c4->sin_addr, 8) ) &&
-          (0 != c4->sin_addr.s_addr) &&
           match_ipv4 ("127.0.0.1", &v4.sin_addr, 8) )
        continue; /* bound to non-loopback, but this is loopback */
-      if ( (0 != (ch->flags & GNUNET_NAT_AC_EXTERN)) &&
-          (0 != c4->sin_addr.s_addr) &&
-          (! is_nat_v4 (&v4.sin_addr)) )
-       continue; /* based on external-IP, but this IP is not
-                    from private address range. */
-      if ( (0 != memcmp (&v4.sin_addr,
-                        &c4->sin_addr,
-                        sizeof (struct in_addr))) &&
-          (0 != c4->sin_addr.s_addr) &&
-          ( (! is_nat_v4 (&c4->sin_addr)) ||
-            (0 == (ch->flags & GNUNET_NAT_AC_EXTERN))) )
+      if ( (0 != (delta->ac & GNUNET_NAT_AC_EXTERN)) &&
+           (0 != c4->sin_addr.s_addr) &&
+           (! is_nat_v4 (&v4.sin_addr)) )
+       continue; /* based on external-IP, but this IP is not
+                    from private address range. */
+      if ( (0 != GNUNET_memcmp (&v4.sin_addr,
+                         &c4->sin_addr)) &&
+           (0 != c4->sin_addr.s_addr) &&
+           (! is_nat_v4 (&c4->sin_addr)) )
        continue; /* this IP is not from private address range,
                     and IP does not match. */
 
       /* OK, IP seems relevant, notify client */
-      v4.sin_port = c4->sin_port;
+      if (0 == htons (v4.sin_port))
+        v4.sin_port = c4->sin_port;
       notify_client (delta->ac,
                     ch,
                     add,
@@ -821,51 +755,43 @@ check_notify_client (struct LocalAddressList *delta,
     for (unsigned int i=0;i<ch->num_caddrs;i++)
     {
       const struct sockaddr_in6 *c6;
-      
+
       if (AF_INET6 != ch->caddrs[i].ss.ss_family)
        continue; /* IPv4 not relevant */
       c6 = (const struct sockaddr_in6 *) &ch->caddrs[i].ss;
       if ( match_ipv6 ("::1", &c6->sin6_addr, 128) &&
-          (0 != memcmp (&c6->sin6_addr,
-                        &in6addr_any,
-                        sizeof (struct in6_addr))) &&
+          (0 != GNUNET_memcmp (&c6->sin6_addr,
+                        &in6addr_any)) &&
           (! match_ipv6 ("::1", &v6.sin6_addr, 128)) )
        continue; /* bound to loopback, but this is not loopback */
       if ( (! match_ipv6 ("::1", &c6->sin6_addr, 128) ) &&
-          (0 != memcmp (&c6->sin6_addr,
-                        &in6addr_any,
-                        sizeof (struct in6_addr))) &&
           match_ipv6 ("::1", &v6.sin6_addr, 128) )
        continue; /* bound to non-loopback, but this is loopback */
-      if ( (0 != (ch->flags & GNUNET_NAT_AC_EXTERN)) &&
-          (0 != memcmp (&c6->sin6_addr,
-                        &in6addr_any,
-                        sizeof (struct in6_addr))) &&
+      if ( (0 != (delta->ac & GNUNET_NAT_AC_EXTERN)) &&
+           (0 != GNUNET_memcmp (&c6->sin6_addr,
+                        &in6addr_any)) &&
           (! is_nat_v6 (&v6.sin6_addr)) )
        continue; /* based on external-IP, but this IP is not
                     from private address range. */
-      if ( (0 != memcmp (&v6.sin6_addr,
-                        &c6->sin6_addr,
-                        sizeof (struct in6_addr))) &&
-          (0 != memcmp (&c6->sin6_addr,
-                        &in6addr_any,
-                        sizeof (struct in6_addr))) &&
+      if ( (0 != GNUNET_memcmp (&v6.sin6_addr,
+                        &c6->sin6_addr)) &&
+          (0 != GNUNET_memcmp (&c6->sin6_addr,
+                        &in6addr_any)) &&
           (! is_nat_v6 (&c6->sin6_addr)) )
        continue; /* this IP is not from private address range,
                     and IP does not match. */
       if ( (match_ipv6 ("fe80::", &c6->sin6_addr, 10)) &&
-          (0 != memcmp (&c6->sin6_addr,
-                        &in6addr_any,
-                        sizeof (struct in6_addr))) &&
-          (0 != memcmp (&v6.sin6_addr,
-                        &c6->sin6_addr,
-                        sizeof (struct in6_addr))) &&
+          (0 != GNUNET_memcmp (&c6->sin6_addr,
+                        &in6addr_any)) &&
+          (0 != GNUNET_memcmp (&v6.sin6_addr,
+                        &c6->sin6_addr)) &&
           (0 == (delta->ac & GNUNET_NAT_AC_EXTERN)) )
        continue; /* client bound to link-local, and the other address
                     does not match and is not an external IP */
 
       /* OK, IP seems relevant, notify client */
-      v6.sin6_port = c6->sin6_port;
+      if (0 == htons (v6.sin6_port))
+        v6.sin6_port = c6->sin6_port;
       notify_client (delta->ac,
                     ch,
                     add,
@@ -903,7 +829,7 @@ notify_clients (struct LocalAddressList *delta,
 /**
  * Tell relevant client about a change in our external
  * IPv4 address.
- * 
+ *
  * @param cls client to check if it cares and possibly notify
  * @param v4 the external address that changed
  * @param add #GNUNET_YES to add, #GNUNET_NO to remove
@@ -918,28 +844,32 @@ notify_client_external_ipv4_change (void *cls,
   int have_v4;
 
   /* (0) check if this impacts 'hole_external' */
-  if (0 == strcasecmp (ch->hole_external,
-                      "AUTO"))
+  if ( (NULL != ch->hole_external) &&
+       (0 == strcasecmp (ch->hole_external,
+                        "AUTO")) )
   {
     struct LocalAddressList lal;
     struct sockaddr_in *s4;
-  
+
+    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                "Detected eternal IP, can now back-fill AUTO:%u in hole punching specification of `%s'\n",
+                (unsigned int) ch->ext_dns_port,
+                ch->section_name);
     memset (&lal, 0, sizeof (lal));
     s4 = (struct sockaddr_in *) &lal.addr;
     s4->sin_family = AF_INET;
     s4->sin_port = htons (ch->ext_dns_port);
+    s4->sin_addr = *v4;
     lal.af = AF_INET;
     lal.ac = GNUNET_NAT_AC_GLOBAL | GNUNET_NAT_AC_MANUAL;
     check_notify_client (&lal,
                         ch,
                         add);
   }
-  
+
   /* (1) check if client cares. */
   if (! ch->natted_address)
     return;
-  if (0 == (GNUNET_NAT_RF_ADDRESSES & ch->flags))
-    return;
   have_v4 = GNUNET_NO;
   for (unsigned int i=0;i<ch->num_caddrs;i++)
   {
@@ -960,10 +890,14 @@ notify_client_external_ipv4_change (void *cls,
   sa.sin_family = AF_INET;
   sa.sin_addr = *v4;
   sa.sin_port = htons (0);
-  
+
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+              "Detected eternal IP %s, notifying client of external IP (without port)\n",
+              GNUNET_a2s ((const struct sockaddr *) &sa,
+                          sizeof (sa)));
   /* (3) notify client of change */
   notify_client (is_nat_v4 (v4)
-                ? GNUNET_NAT_AC_EXTERN | GNUNET_NAT_AC_LAN 
+                ? GNUNET_NAT_AC_EXTERN | GNUNET_NAT_AC_LAN
                 : GNUNET_NAT_AC_EXTERN | GNUNET_NAT_AC_GLOBAL,
                 ch,
                 add,
@@ -976,8 +910,8 @@ notify_client_external_ipv4_change (void *cls,
  * We got a connection reversal request from another peer.
  * Notify applicable clients.
  *
- * @param cls closure with the `struct LocalAddressList` 
- * @param ra IP address of the peer who wants us to connect to it 
+ * @param cls closure with the `struct LocalAddressList`
+ * @param ra IP address of the peer who wants us to connect to it
  */
 static void
 reversal_callback (void *cls,
@@ -991,7 +925,7 @@ reversal_callback (void *cls,
   for (struct ClientHandle *ch = ch_head;
        NULL != ch;
        ch = ch->next)
-  {    
+  {
     struct GNUNET_NAT_ConnectionReversalRequestedMessage *crrm;
     struct GNUNET_MQ_Envelope *env;
     int match;
@@ -1005,7 +939,7 @@ reversal_callback (void *cls,
     {
       struct ClientAddress *ca = &ch->caddrs[i];
       const struct sockaddr_in *c4;
-      
+
       if (AF_INET != ca->ss.ss_family)
        continue;
       c4 = (const struct sockaddr_in *) &ca->ss;
@@ -1035,7 +969,7 @@ reversal_callback (void *cls,
  * Task we run periodically to scan for network interfaces.
  *
  * @param cls NULL
- */ 
+ */
 static void
 run_scan (void *cls)
 {
@@ -1043,7 +977,7 @@ run_scan (void *cls)
   int found;
   int have_nat;
   struct LocalAddressList *lnext;
-  
+
   scan_task = GNUNET_SCHEDULER_add_delayed (SCAN_FREQ,
                                            &run_scan,
                                            NULL);
@@ -1122,10 +1056,11 @@ run_scan (void *cls)
       {
        const struct sockaddr_in *s4
          = (const struct sockaddr_in *) &pos->addr;
-       
-       GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
+
+       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                    "Found NATed local address %s, starting NAT server\n",
-                   GNUNET_a2s ((void *) &pos->addr, sizeof (*s4)));
+                   GNUNET_a2s ((const struct sockaddr *) &pos->addr,
+                                sizeof (*s4)));
        pos->hc = GN_start_gnunet_nat_server_ (&s4->sin_addr,
                                               &reversal_callback,
                                               pos);
@@ -1173,6 +1108,10 @@ upnp_addr_change_cb (void *cls,
     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
                "external-ip binary not found\n");
     return;
+  case GNUNET_NAT_ERROR_UPNPC_NOT_FOUND:
+    GNUNET_log (GNUNET_ERROR_TYPE_INFO,
+               "upnpc binary not found\n");
+    return;
   case GNUNET_NAT_ERROR_EXTERNAL_IP_UTILITY_FAILED:
     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
                "external-ip binary could not be run\n");
@@ -1223,9 +1162,9 @@ upnp_addr_change_cb (void *cls,
 
 
 /**
- * Resolve the `hole_external` name to figure out our 
+ * Resolve the `hole_external` name to figure out our
  * external address from a manually punched hole.  The
- * port number has already been parsed, this task is 
+ * port number has already been parsed, this task is
  * responsible for periodically doing a DNS lookup.
  *
  * @param ch client handle to act upon
@@ -1257,9 +1196,9 @@ process_external_ip (void *cls,
   if (NULL == addr)
   {
     struct LocalAddressList *laln;
-    
+
     ch->ext_dns = NULL;
-    ch->ext_dns_task 
+    ch->ext_dns_task
       = GNUNET_SCHEDULER_add_delayed (dyndns_frequency,
                                      &dyndns_lookup,
                                      ch);
@@ -1285,10 +1224,14 @@ process_external_ip (void *cls,
              GNUNET_a2s (addr,
                          addrlen),
              ch->hole_external);
-  
+
   /* build sockaddr storage with port number */
-  memset (&ss, 0, sizeof (ss));
-  memcpy (&ss, addr, addrlen);
+  memset (&ss,
+          0,
+          sizeof (ss));
+  GNUNET_memcpy (&ss,
+                 addr,
+                 addrlen);
   switch (addr->sa_family)
   {
   case AF_INET:
@@ -1333,9 +1276,9 @@ process_external_ip (void *cls,
 
 
 /**
- * Resolve the `hole_external` name to figure out our 
+ * Resolve the `hole_external` name to figure out our
  * external address from a manually punched hole.  The
- * port number has already been parsed, this task is 
+ * port number has already been parsed, this task is
  * responsible for periodically doing a DNS lookup.
  *
  * @param ch client handle to act upon
@@ -1346,6 +1289,11 @@ dyndns_lookup (void *cls)
   struct ClientHandle *ch = cls;
   struct LocalAddressList *lal;
 
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+              "Performing DNS lookup for punched hole given for `%s' as `%s:%u'\n",
+              ch->section_name,
+              ch->hole_external,
+              (unsigned int) ch->ext_dns_port);
   for (lal = ch->ext_addr_head; NULL != lal; lal = lal->next)
     lal->old = GNUNET_YES;
   ch->ext_dns_task = NULL;
@@ -1353,16 +1301,16 @@ dyndns_lookup (void *cls)
                                        AF_UNSPEC,
                                        GNUNET_TIME_UNIT_MINUTES,
                                        &process_external_ip,
-                                       ch);  
+                                       ch);
 }
 
 
 /**
- * Resolve the `hole_external` name to figure out our 
+ * Resolve the `hole_external` name to figure out our
  * external address from a manually punched hole.  The
  * given name may be "AUTO" in which case we should use
  * the IP address(es) we have from upnpc or other methods.
- * The name can also be an IP address, in which case we 
+ * The name can also be an IP address, in which case we
  * do not need to do DNS resolution.  Finally, we also
  * need to parse the port number.
  *
@@ -1375,7 +1323,7 @@ lookup_hole_external (struct ClientHandle *ch)
   unsigned int pnum;
   struct sockaddr_in *s4;
   struct LocalAddressList *lal;
-    
+
   port = strrchr (ch->hole_external, ':');
   if (NULL == port)
   {
@@ -1392,7 +1340,7 @@ lookup_hole_external (struct ClientHandle *ch)
     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
                _("Invalid port number in punched hole specification `%s' (lacks port)\n"),
                port + 1);
-    return;    
+    return;
   }
   ch->ext_dns_port = (uint16_t) pnum;
   *port = '\0';
@@ -1440,6 +1388,11 @@ lookup_hole_external (struct ClientHandle *ch)
                      ch->hole_external,
                      &s4->sin_addr))
   {
+    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                "IPv4 punched hole given for `%s' via `%s:%u'\n",
+                ch->section_name,
+                ch->hole_external,
+                (unsigned int) ch->ext_dns_port);
     s4->sin_port = htons (ch->ext_dns_port);
     lal->af = AF_INET;
     lal->ac = GNUNET_NAT_AC_GLOBAL | GNUNET_NAT_AC_MANUAL;
@@ -1462,7 +1415,7 @@ lookup_hole_external (struct ClientHandle *ch)
   GNUNET_free (lal);
   ch->ext_dns_task
     = GNUNET_SCHEDULER_add_now (&dyndns_lookup,
-                               ch);  
+                               ch);
 }
 
 
@@ -1489,8 +1442,6 @@ handle_register (void *cls,
     GNUNET_SERVICE_client_drop (ch->client);
     return;
   }
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-             "Received REGISTER message from client\n");
   ch->flags = message->flags;
   ch->proto = message->proto;
   ch->num_caddrs = ntohs (message->num_addrs);
@@ -1500,8 +1451,8 @@ handle_register (void *cls,
   off = (const char *) &message[1];
   for (unsigned int i=0;i<ch->num_caddrs;i++)
   {
-    size_t alen;
     const struct sockaddr *sa = (const struct sockaddr *) off;
+    size_t alen;
     uint16_t port;
     int is_nat;
 
@@ -1516,22 +1467,28 @@ handle_register (void *cls,
     {
     case AF_INET:
       {
-       const struct sockaddr_in *s4 = (const struct sockaddr_in *) sa;
-       
+        struct sockaddr_in s4;
+
+        GNUNET_memcpy (&s4,
+                       off,
+                       sizeof (struct sockaddr_in));
        alen = sizeof (struct sockaddr_in);
-       if (is_nat_v4 (&s4->sin_addr))
+       if (is_nat_v4 (&s4.sin_addr))
          is_nat = GNUNET_YES;
-       port = ntohs (s4->sin_port);
+       port = ntohs (s4.sin_port);
       }
       break;
     case AF_INET6:
       {
-       const struct sockaddr_in6 *s6 = (const struct sockaddr_in6 *) sa;
-       
+        struct sockaddr_in6 s6;
+
+        GNUNET_memcpy (&s6,
+                       off,
+                       sizeof (struct sockaddr_in6));
        alen = sizeof (struct sockaddr_in6);
-       if (is_nat_v6 (&s6->sin6_addr))
+       if (is_nat_v6 (&s6.sin6_addr))
          is_nat = GNUNET_YES;
-       port = ntohs (s6->sin6_port);
+       port = ntohs (s6.sin6_port);
       }
       break;
 #if AF_UNIX
@@ -1543,14 +1500,14 @@ handle_register (void *cls,
     default:
       GNUNET_break (0);
       GNUNET_SERVICE_client_drop (ch->client);
-      return;      
+      return;
     }
     /* store address */
     GNUNET_assert (alen <= left);
     GNUNET_assert (alen <= sizeof (struct sockaddr_storage));
     GNUNET_memcpy (&ch->caddrs[i].ss,
-                  sa,
-                  alen);    
+                  off,
+                  alen);
 
     /* If applicable, try UPNPC NAT punching */
     if ( (is_nat) &&
@@ -1569,12 +1526,19 @@ handle_register (void *cls,
     off += alen;
   }
 
-  ch->hole_external
+  ch->section_name
     = GNUNET_strndup (off,
-                     ntohs (message->hole_external_len));
-  if (0 != ntohs (message->hole_external_len))
+                     ntohs (message->str_len));
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+             "Received REGISTER message from client for subsystem `%s'\n",
+              ch->section_name);
+  if (GNUNET_OK ==
+      GNUNET_CONFIGURATION_get_value_string (cfg,
+                                            ch->section_name,
+                                            "HOLE_EXTERNAL",
+                                            &ch->hole_external))
     lookup_hole_external (ch);
-  
+
   /* Actually send IP address list to client */
   for (struct LocalAddressList *lal = lal_head;
        NULL != lal;
@@ -1606,7 +1570,7 @@ check_stun (void *cls,
 {
   size_t sa_len = ntohs (message->sender_addr_size);
   size_t expect = sa_len + ntohs (message->payload_size);
-  
+
   if (ntohs (message->header.size) - sizeof (*message) != expect)
   {
     GNUNET_break (0);
@@ -1639,7 +1603,7 @@ notify_clients_stun_change (const struct sockaddr_in *ip,
     struct sockaddr_in v4;
     struct GNUNET_NAT_AddressChangeNotificationMessage *msg;
     struct GNUNET_MQ_Envelope *env;
-    
+
     if (! ch->natted_address)
       continue;
     v4 = *ip;
@@ -1729,9 +1693,9 @@ handle_stun (void *cls,
       GNUNET_NAT_stun_handle_packet_ (payload,
                                      payload_size,
                                      &external_addr))
-  {     
+  {
     /* We now know that a server at "sa" claims that
-       we are visible at IP "external_addr". 
+       we are visible at IP "external_addr".
 
        We should (for some fixed period of time) tell
        all of our clients that listen to a NAT'ed address
@@ -1754,9 +1718,8 @@ handle_stun (void *cls,
                         &se->stun_server_addr,
                         sa_len)) )
        continue; /* different STUN server */
-      if (0 != memcmp (&external_addr,
-                      &se->external_addr,
-                      sizeof (struct sockaddr_in)))
+      if (0 != GNUNET_memcmp (&external_addr,
+                      &se->external_addr))
       {
        /* external IP changed, update! */
        notify_clients_stun_change (&se->external_addr,
@@ -1834,346 +1797,43 @@ handle_request_connection_reversal (void *cls,
   const char *buf = (const char *) &message[1];
   size_t local_sa_len = ntohs (message->local_addr_size);
   size_t remote_sa_len = ntohs (message->remote_addr_size);
-  const struct sockaddr *local_sa = (const struct sockaddr *) &buf[0];
-  const struct sockaddr *remote_sa = (const struct sockaddr *) &buf[local_sa_len];
-  const struct sockaddr_in *l4 = NULL;
-  const struct sockaddr_in *r4;
+  struct sockaddr_in l4;
+  struct sockaddr_in r4;
   int ret;
-  
+
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
              "Received REQUEST CONNECTION REVERSAL message from client\n");
-  switch (local_sa->sa_family)
+  if (local_sa_len != sizeof (struct sockaddr_in))
   {
-  case AF_INET:
-    if (local_sa_len != sizeof (struct sockaddr_in))
-    {
-      GNUNET_break (0);
-      GNUNET_SERVICE_client_drop (ch->client);
-      return;
-    }
-    l4 = (const struct sockaddr_in *) local_sa;    
-    break;
-  case AF_INET6:
-    if (local_sa_len != sizeof (struct sockaddr_in6))
-    {
-      GNUNET_break (0);
-      GNUNET_SERVICE_client_drop (ch->client);
-      return;
-    }
-    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
-               _("Connection reversal for IPv6 not supported yet\n"));
-    ret = GNUNET_SYSERR;
-    break;
-  default:
-    GNUNET_break (0);
+    GNUNET_break_op (0);
     GNUNET_SERVICE_client_drop (ch->client);
     return;
   }
-  switch (remote_sa->sa_family)
+  if (remote_sa_len != sizeof (struct sockaddr_in))
   {
-  case AF_INET:
-    if (remote_sa_len != sizeof (struct sockaddr_in))
-    {
-      GNUNET_break (0);
-      GNUNET_SERVICE_client_drop (ch->client);
-      return;
-    }
-    r4 = (const struct sockaddr_in *) remote_sa;
-    ret = GN_request_connection_reversal (&l4->sin_addr,
-                                         ntohs (l4->sin_port),
-                                         &r4->sin_addr);
-    break;
-  case AF_INET6:
-    if (remote_sa_len != sizeof (struct sockaddr_in6))
-    {
-      GNUNET_break (0);
-      GNUNET_SERVICE_client_drop (ch->client);
-      return;
-    }
-    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
-               _("Connection reversal for IPv6 not supported yet\n"));
-    ret = GNUNET_SYSERR;
-    break;
-  default:
-    GNUNET_break (0);
+    GNUNET_break_op (0);
     GNUNET_SERVICE_client_drop (ch->client);
     return;
   }
+  GNUNET_memcpy (&l4,
+                buf,
+                sizeof (struct sockaddr_in));
+  GNUNET_break_op (AF_INET == l4.sin_family);
+  buf += sizeof (struct sockaddr_in);
+  GNUNET_memcpy (&r4,
+                buf,
+                sizeof (struct sockaddr_in));
+  GNUNET_break_op (AF_INET == r4.sin_family);
+  ret = GN_request_connection_reversal (&l4.sin_addr,
+                                       ntohs (l4.sin_port),
+                                       &r4.sin_addr);
   if (GNUNET_OK != ret)
     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
-               _("Connection reversal request failed\n"));  
-  GNUNET_SERVICE_client_continue (ch->client);
-}
-
-
-/**
- * Check validity of #GNUNET_MESSAGE_TYPE_NAT_REQUEST_AUTO_CFG message
- * from client.
- *
- * @param cls client who sent the message
- * @param message the message received
- * @return #GNUNET_OK if message is well-formed
- */
-static int
-check_autoconfig_request (void *cls,
-                         const struct GNUNET_NAT_AutoconfigRequestMessage *message)
-{
-  return GNUNET_OK;  /* checked later */
-}
-
-
-/**
- * Stop all pending activities with respect to the @a ac
- *
- * @param ac autoconfiguration to terminate activities for
- */
-static void
-terminate_ac_activities (struct AutoconfigContext *ac)
-{
-  if (NULL != ac->probe_external)
-  {
-    GNUNET_NAT_mini_get_external_ipv4_cancel_ (ac->probe_external);
-    ac->probe_external = NULL;
-  }
-  if (NULL != ac->timeout_task)
-  {
-    GNUNET_SCHEDULER_cancel (ac->timeout_task);
-    ac->timeout_task = NULL;
-  }
-}
-
-
-/**
- * Finish handling the autoconfiguration request and send
- * the response to the client.
- *
- * @param cls the `struct AutoconfigContext` to conclude
- */
-static void
-conclude_autoconfig_request (void *cls)
-{
-  struct AutoconfigContext *ac = cls;
-  struct ClientHandle *ch = ac->ch;
-  struct GNUNET_NAT_AutoconfigResultMessage *arm;
-  struct GNUNET_MQ_Envelope *env;
-  size_t c_size;
-  char *buf;
-  struct GNUNET_CONFIGURATION_Handle *diff;
-  
-  ac->timeout_task = NULL;
-  terminate_ac_activities (ac);
-
-  /* Send back response */
-  diff = GNUNET_CONFIGURATION_get_diff (ac->orig,
-                                       ac->c);
-  buf = GNUNET_CONFIGURATION_serialize (diff,
-                                       &c_size);
-  GNUNET_CONFIGURATION_destroy (diff);
-  env = GNUNET_MQ_msg_extra (arm,
-                            c_size,
-                            GNUNET_MESSAGE_TYPE_NAT_AUTO_CFG_RESULT);
-  arm->status_code = htonl ((uint32_t) ac->status_code);
-  arm->type = htonl ((uint32_t) ac->type);
-  GNUNET_memcpy (&arm[1],
-                buf,
-                c_size);
-  GNUNET_free (buf);
-  GNUNET_MQ_send (ch->mq,
-                 env);
-
-  /* clean up */
-  GNUNET_free (ac->system_type);
-  GNUNET_CONFIGURATION_destroy (ac->orig);
-  GNUNET_CONFIGURATION_destroy (ac->c);
-  GNUNET_CONTAINER_DLL_remove (ac_head,
-                              ac_tail,
-                              ac);
-  GNUNET_free (ac);
+               _("Connection reversal request failed\n"));
   GNUNET_SERVICE_client_continue (ch->client);
 }
 
 
-/**
- * Check if all autoconfiguration operations have concluded,
- * and if they have, send the result back to the client.
- *
- * @param ac autoconfiguation context to check
- */
-static void
-check_autoconfig_finished (struct AutoconfigContext *ac)
-{
-  if (NULL != ac->probe_external)
-    return;
-  GNUNET_SCHEDULER_cancel (ac->timeout_task);
-  ac->timeout_task
-    = GNUNET_SCHEDULER_add_now (&conclude_autoconfig_request,
-                               ac);
-}
-
-
-/**
- * Update ENABLE_UPNPC configuration option.
- *
- * @param ac autoconfiguration to update
- */
-static void
-update_enable_upnpc_option (struct AutoconfigContext *ac)
-{
-  switch (ac->enable_upnpc)
-  {
-  case GNUNET_YES:
-    GNUNET_CONFIGURATION_set_value_string (ac->c,
-                                          "NAT",
-                                          "ENABLE_UPNP",
-                                          "YES");
-    break;
-  case GNUNET_NO:
-    GNUNET_CONFIGURATION_set_value_string (ac->c,
-                                          "NAT",
-                                          "ENABLE_UPNP",
-                                          "NO");
-    break;
-  case GNUNET_SYSERR:
-    /* We are unsure, do not change option */
-    break;
-  }
-}
-
-
-/**
- * Handle result from external IP address probe during
- * autoconfiguration.
- *
- * @param cls our `struct AutoconfigContext`
- * @param addr the address, NULL on errors
- * @param result #GNUNET_NAT_ERROR_SUCCESS on success, otherwise the specific error code
- */
-static void
-auto_external_result_cb (void *cls,
-                        const struct in_addr *addr,
-                        enum GNUNET_NAT_StatusCode result)
-{
-  struct AutoconfigContext *ac = cls;
-
-  ac->probe_external = NULL;
-  switch (result)
-  {
-  case GNUNET_NAT_ERROR_SUCCESS:
-    ac->enable_upnpc = GNUNET_YES;
-    break;
-  case GNUNET_NAT_ERROR_EXTERNAL_IP_UTILITY_OUTPUT_INVALID:
-  case GNUNET_NAT_ERROR_EXTERNAL_IP_ADDRESS_INVALID:
-  case GNUNET_NAT_ERROR_IPC_FAILURE:
-    GNUNET_log (GNUNET_ERROR_TYPE_INFO,
-               "Disabling UPNPC: %d\n",
-               (int) result);
-    ac->enable_upnpc = GNUNET_NO; /* did not work */
-    break;
-  default:
-    GNUNET_break (0); /* unexpected */
-    ac->enable_upnpc = GNUNET_SYSERR;
-    break;    
-  }
-  update_enable_upnpc_option (ac);
-  check_autoconfig_finished (ac);
-}
-
-
-/**
- * Handler for #GNUNET_MESSAGE_TYPE_NAT_REQUEST_AUTO_CFG message from
- * client.
- *
- * @param cls client who sent the message
- * @param message the message received
- */
-static void
-handle_autoconfig_request (void *cls,
-                          const struct GNUNET_NAT_AutoconfigRequestMessage *message)
-{
-  struct ClientHandle *ch = cls;
-  size_t left = ntohs (message->header.size) - sizeof (*message);
-  struct LocalAddressList *lal;
-  struct AutoconfigContext *ac;
-
-  ac = GNUNET_new (struct AutoconfigContext);
-  ac->status_code = GNUNET_NAT_ERROR_SUCCESS;
-  ac->ch = ch;
-  ac->c = GNUNET_CONFIGURATION_create ();
-  if (GNUNET_OK !=
-      GNUNET_CONFIGURATION_deserialize (ac->c,
-                                       (const char *) &message[1],
-                                       left,
-                                       GNUNET_NO))
-  {
-    GNUNET_break (0);
-    GNUNET_SERVICE_client_drop (ch->client);
-    GNUNET_CONFIGURATION_destroy (ac->c);
-    GNUNET_free (ac);
-    return;
-  }
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-             "Received REQUEST_AUTO_CONFIG message from client\n");
-
-  if (GNUNET_OK !=
-      GNUNET_CONFIGURATION_get_value_string (ac->c,
-                                            "PEER",
-                                            "SYSTEM_TYPE",
-                                            &ac->system_type))
-    ac->system_type = GNUNET_strdup ("UNKNOWN");
-
-  GNUNET_CONTAINER_DLL_insert (ac_head,
-                              ac_tail,
-                              ac);
-  ac->orig
-    = GNUNET_CONFIGURATION_dup (ac->c);
-  ac->timeout_task
-    = GNUNET_SCHEDULER_add_delayed (AUTOCONFIG_TIMEOUT,
-                                   &conclude_autoconfig_request,
-                                   ac);
-  ac->enable_upnpc = GNUNET_SYSERR; /* undecided */
-  
-  /* Probe for upnpc */
-  if (GNUNET_SYSERR ==
-      GNUNET_OS_check_helper_binary ("upnpc",
-                                    GNUNET_NO,
-                                    NULL))
-  {
-    GNUNET_log (GNUNET_ERROR_TYPE_INFO,
-               _("UPnP client `upnpc` command not found, disabling UPnP\n"));
-    ac->enable_upnpc = GNUNET_NO;
-  }
-  else
-  {
-    for (lal = lal_head; NULL != lal; lal = lal->next)
-      if (GNUNET_NAT_AC_LAN == (lal->ac & GNUNET_NAT_AC_LAN))
-       /* we are behind NAT, useful to try upnpc */
-       ac->enable_upnpc = GNUNET_YES;
-  }
-  if (GNUNET_YES == ac->enable_upnpc)
-  {
-    /* If we are a mobile device, always leave it on as the network
-       may change to one that supports UPnP anytime.  If we are
-       stationary, check if our network actually supports UPnP, and if
-       not, disable it. */
-    if ( (0 == strcasecmp (ac->system_type,
-                          "INFRASTRUCTURE")) ||
-        (0 == strcasecmp (ac->system_type,
-                          "DESKTOP")) )
-    {
-      /* Check if upnpc gives us an external IP */
-      ac->probe_external
-       = GNUNET_NAT_mini_get_external_ipv4_ (&auto_external_result_cb,
-                                             ac);
-    }
-  }
-  if (NULL == ac->probe_external)
-    update_enable_upnpc_option (ac);
-
-  /* Finally, check if we are already done */  
-  check_autoconfig_finished (ac);
-}
-
-
 /**
  * Task run during shutdown.
  *
@@ -2183,16 +1843,7 @@ static void
 shutdown_task (void *cls)
 {
   struct StunExternalIP *se;
-  struct AutoconfigContext *ac;
 
-  while (NULL != (ac = ac_head))
-  {
-    GNUNET_CONTAINER_DLL_remove (ac_head,
-                                ac_tail,
-                                ac);
-    terminate_ac_activities (ac);
-    GNUNET_free (ac);
-  }
   while (NULL != (se = se_head))
   {
     GNUNET_CONTAINER_DLL_remove (se_head,
@@ -2238,7 +1889,7 @@ run (void *cls,
     stun_stale_timeout = GNUNET_TIME_UNIT_HOURS;
 
   /* Check for UPnP */
-  enable_upnp 
+  enable_upnp
     = GNUNET_CONFIGURATION_get_value_yesno (cfg,
                                            "NAT",
                                            "ENABLE_UPNP");
@@ -2261,7 +1912,7 @@ run (void *cls,
                                           "DYNDNS_FREQUENCY",
                                            &dyndns_frequency))
     dyndns_frequency = DYNDNS_FREQUENCY;
-  
+
   GNUNET_SCHEDULER_add_shutdown (&shutdown_task,
                                 NULL);
   stats = GNUNET_STATISTICS_create ("nat",
@@ -2345,7 +1996,8 @@ client_disconnect_cb (void *cls,
     GNUNET_RESOLVER_request_cancel (ch->ext_dns);
     ch->ext_dns = NULL;
   }
-  GNUNET_free (ch->hole_external);
+  GNUNET_free_non_null (ch->hole_external);
+  GNUNET_free_non_null (ch->section_name);
   GNUNET_free (ch);
 }
 
@@ -2372,10 +2024,6 @@ GNUNET_SERVICE_MAIN
                        GNUNET_MESSAGE_TYPE_NAT_REQUEST_CONNECTION_REVERSAL,
                        struct GNUNET_NAT_RequestConnectionReversalMessage,
                        NULL),
- GNUNET_MQ_hd_var_size (autoconfig_request,
-                       GNUNET_MESSAGE_TYPE_NAT_REQUEST_AUTO_CFG,
-                       struct GNUNET_NAT_AutoconfigRequestMessage,
-                       NULL),
  GNUNET_MQ_handler_end ());