handling replies continuously from server
[oweals/gnunet.git] / src / exit / gnunet-daemon-exit.c
index 04bec763e04880f0f0215efe9facfa3161e1714e..e58611cc62b37fd68fc1981b2b9afa3f90281722 100644 (file)
@@ -284,7 +284,7 @@ static struct GNUNET_CONTAINER_Heap *connections_heap;
 /**
  * If there are at least this many connections, old ones will be removed
  */
-static long long unsigned int max_connections;
+static unsigned long long max_connections;
 
 /**
  * This hashmaps saves interesting things about the configured UDP services
@@ -395,7 +395,8 @@ get_redirect_state (int af,
   GNUNET_HashCode key;
   struct TunnelState *state;
 
-  if (protocol == IPPROTO_ICMP)
+  if ( ( (af == AF_INET) && (protocol == IPPROTO_ICMP) ) ||
+       ( (af == AF_INET6) && (protocol == IPPROTO_ICMPV6) ) )
   {
     /* ignore ports */
     destination_port = 0;
@@ -436,17 +437,17 @@ get_redirect_state (int af,
  *
  * @param service_map map of services (TCP or UDP)
  * @param desc service descriptor
- * @param dpt destination port
+ * @param destination_port destination port
  * @return NULL if we are not aware of such a service
  */
 static struct LocalService *
 find_service (struct GNUNET_CONTAINER_MultiHashMap *service_map,
              const GNUNET_HashCode *desc,
-             uint16_t dpt)
+             uint16_t destination_port)
 {
   char key[sizeof (GNUNET_HashCode) + sizeof (uint16_t)];
 
-  memcpy (&key[0], &dpt, sizeof (uint16_t));
+  memcpy (&key[0], &destination_port, sizeof (uint16_t));
   memcpy (&key[sizeof(uint16_t)], desc, sizeof (GNUNET_HashCode));
   return GNUNET_CONTAINER_multihashmap_get (service_map,
                                            (GNUNET_HashCode *) key);
@@ -480,13 +481,13 @@ free_service_record (void *cls,
  *
  * @param service_map map of services (TCP or UDP)
  * @param name name of the service 
- * @param dpt destination port
+ * @param destination_port destination port
  * @param service service information record to store (service->name will be set).
  */
 static void
 store_service (struct GNUNET_CONTAINER_MultiHashMap *service_map,
               const char *name,
-              uint16_t dpt,
+              uint16_t destination_port,
               struct LocalService *service)
 {
   char key[sizeof (GNUNET_HashCode) + sizeof (uint16_t)];
@@ -494,7 +495,7 @@ store_service (struct GNUNET_CONTAINER_MultiHashMap *service_map,
 
   GNUNET_CRYPTO_hash (name, strlen (name) + 1, &desc);
   service->name = GNUNET_strdup (name);
-  memcpy (&key[0], &dpt, sizeof (uint16_t));
+  memcpy (&key[0], &destination_port, sizeof (uint16_t));
   memcpy (&key[sizeof(uint16_t)], &desc, sizeof (GNUNET_HashCode));
   if (GNUNET_OK !=
       GNUNET_CONTAINER_multihashmap_put (service_map,
@@ -506,7 +507,7 @@ store_service (struct GNUNET_CONTAINER_MultiHashMap *service_map,
     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
                _("Got duplicate service records for `%s:%u'\n"),
                name,
-               (unsigned int) dpt);
+               (unsigned int) destination_port);
   }
 }
 
@@ -614,8 +615,8 @@ icmp_from_helper (const struct GNUNET_TUN_IcmpHeader *icmp,
   const struct GNUNET_TUN_IPv6Header *ipv6;
   const struct GNUNET_TUN_UdpHeader *udp;
   size_t mlen;
-  uint16_t spt;
-  uint16_t dpt;
+  uint16_t source_port;
+  uint16_t destination_port;
   uint8_t protocol;
 
   {
@@ -640,12 +641,12 @@ icmp_from_helper (const struct GNUNET_TUN_IcmpHeader *icmp,
   /* Find out if this is an ICMP packet in response to an existing
      TCP/UDP packet and if so, figure out ports / protocol of the
      existing session from the IP data in the ICMP payload */
-  spt = 0;
-  dpt = 0;
-  protocol = IPPROTO_ICMP;
+  source_port = 0;
+  destination_port = 0;
   switch (af)
   {
   case AF_INET:
+    protocol = IPPROTO_ICMP;
     switch (icmp->type)
       {
       case GNUNET_TUN_ICMPTYPE_ECHO_REPLY:
@@ -667,8 +668,9 @@ icmp_from_helper (const struct GNUNET_TUN_IcmpHeader *icmp,
        /* could be TCP or UDP, but both have the ports in the right
           place, so that doesn't matter here */
        udp = (const struct GNUNET_TUN_UdpHeader *) &ipv4[1];
-       spt = ntohs (udp->spt);
-       dpt = ntohs (udp->dpt);
+       /* swap ports, as they are from the original message */
+       destination_port = ntohs (udp->source_port);
+       source_port = ntohs (udp->destination_port);
        /* throw away ICMP payload, won't be useful for the other side anyway */
        pktlen = sizeof (struct GNUNET_TUN_IcmpHeader);
        break;
@@ -680,6 +682,7 @@ icmp_from_helper (const struct GNUNET_TUN_IcmpHeader *icmp,
       }
     break;
   case AF_INET6:
+    protocol = IPPROTO_ICMPV6;
     switch (icmp->type)
       {
       case GNUNET_TUN_ICMPTYPE6_DESTINATION_UNREACHABLE:
@@ -699,8 +702,9 @@ icmp_from_helper (const struct GNUNET_TUN_IcmpHeader *icmp,
        /* could be TCP or UDP, but both have the ports in the right
           place, so that doesn't matter here */
        udp = (const struct GNUNET_TUN_UdpHeader *) &ipv6[1];
-       spt = ntohs (udp->spt);
-       dpt = ntohs (udp->dpt);
+       /* swap ports, as they are from the original message */
+       destination_port = ntohs (udp->source_port);
+       source_port = ntohs (udp->destination_port);
        /* throw away ICMP payload, won't be useful for the other side anyway */
        pktlen = sizeof (struct GNUNET_TUN_IcmpHeader);
        break;
@@ -725,20 +729,26 @@ icmp_from_helper (const struct GNUNET_TUN_IcmpHeader *icmp,
                                destination_ip, 0,
                                NULL);
     break;
+  case IPPROTO_ICMPV6:
+    state = get_redirect_state (af, IPPROTO_ICMPV6,
+                               source_ip, 0,
+                               destination_ip, 0,
+                               NULL);
+    break;
   case IPPROTO_UDP:
     state = get_redirect_state (af, IPPROTO_UDP,
                                source_ip,
-                               spt,
+                               source_port,
                                destination_ip,
-                               dpt,
+                               destination_port,
                                NULL);
     break;
   case IPPROTO_TCP:
     state = get_redirect_state (af, IPPROTO_TCP,
                                source_ip,
-                               spt,
+                               source_port,
                                destination_ip,
-                               dpt,
+                               destination_port,
                                NULL);
     break;
   default:
@@ -800,11 +810,11 @@ udp_from_helper (const struct GNUNET_TUN_UdpHeader *udp,
                inet_ntop (af,
                           source_ip,
                           sbuf, sizeof (sbuf)),
-               (unsigned int) ntohs (udp->spt),
+               (unsigned int) ntohs (udp->source_port),
                inet_ntop (af,
                           destination_ip,
                           dbuf, sizeof (dbuf)),
-               (unsigned int) ntohs (udp->dpt));
+               (unsigned int) ntohs (udp->destination_port));
   }
   if (pktlen < sizeof (struct GNUNET_TUN_UdpHeader))
   {
@@ -820,9 +830,9 @@ udp_from_helper (const struct GNUNET_TUN_UdpHeader *udp,
   }
   state = get_redirect_state (af, IPPROTO_UDP,
                              source_ip,
-                             ntohs (udp->spt),
+                             ntohs (udp->source_port),
                              destination_ip,
-                             ntohs (udp->dpt),
+                             ntohs (udp->destination_port),
                              NULL);
   if (NULL == state)
   {
@@ -866,7 +876,7 @@ tcp_from_helper (const struct GNUNET_TUN_TcpHeader *tcp,
                 const void *source_ip)
 {
   struct TunnelState *state;
-  char buf[pktlen];
+  char buf[pktlen] GNUNET_ALIGN;
   struct GNUNET_TUN_TcpHeader *mtcp;
   struct GNUNET_EXIT_TcpDataMessage *tdm;
   struct TunnelMessageQueue *tnq;
@@ -881,11 +891,11 @@ tcp_from_helper (const struct GNUNET_TUN_TcpHeader *tcp,
                inet_ntop (af,
                           source_ip,
                           sbuf, sizeof (sbuf)),
-               (unsigned int) ntohs (tcp->spt),
+               (unsigned int) ntohs (tcp->source_port),
                inet_ntop (af,
                           destination_ip,
                           dbuf, sizeof (dbuf)),
-               (unsigned int) ntohs (tcp->dpt));
+               (unsigned int) ntohs (tcp->destination_port));
   }
   if (pktlen < sizeof (struct GNUNET_TUN_TcpHeader))
   {
@@ -895,9 +905,9 @@ tcp_from_helper (const struct GNUNET_TUN_TcpHeader *tcp,
   }
   state = get_redirect_state (af, IPPROTO_TCP,
                              source_ip, 
-                             ntohs (tcp->spt),
+                             ntohs (tcp->source_port),
                              destination_ip,
-                             ntohs (tcp->dpt),
+                             ntohs (tcp->destination_port),
                              NULL);
   if (NULL == state)
   {
@@ -910,8 +920,8 @@ tcp_from_helper (const struct GNUNET_TUN_TcpHeader *tcp,
      sender will need to lookup the correct values anyway */
   memcpy (buf, tcp, pktlen);  
   mtcp = (struct GNUNET_TUN_TcpHeader *) buf;
-  mtcp->spt = 0;
-  mtcp->dpt = 0;
+  mtcp->source_port = 0;
+  mtcp->destination_port = 0;
   mtcp->crc = 0;
 
   mlen = sizeof (struct GNUNET_EXIT_TcpDataMessage) + (pktlen - sizeof (struct GNUNET_TUN_TcpHeader));
@@ -1022,7 +1032,8 @@ message_token (void *cls GNUNET_UNUSED, void *client GNUNET_UNUSED,
        break;
       default:
        GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
-                   _("IPv4 packet with unsupported next header received.  Ignored.\n"));
+                   _("IPv4 packet with unsupported next header %u received.  Ignored.\n"),
+                   (int) pkt4->protocol);
        return;
       }
     }
@@ -1038,7 +1049,7 @@ message_token (void *cls GNUNET_UNUSED, void *client GNUNET_UNUSED,
        return;
       }
       pkt6 = (struct GNUNET_TUN_IPv6Header *) &pkt_tun[1];
-      if (size != ntohs (pkt6->payload_length))
+      if (size != ntohs (pkt6->payload_length) + sizeof (struct GNUNET_TUN_IPv6Header))
       {
        /* Kernel to blame? */
        GNUNET_break (0);
@@ -1059,7 +1070,7 @@ message_token (void *cls GNUNET_UNUSED, void *client GNUNET_UNUSED,
                         &pkt6->destination_address, 
                         &pkt6->source_address);
        break;
-      case IPPROTO_ICMP:
+      case IPPROTO_ICMPV6:
        icmp_from_helper ((const struct GNUNET_TUN_IcmpHeader *) &pkt6[1], size,
                          AF_INET6,
                          &pkt6->destination_address, 
@@ -1067,7 +1078,8 @@ message_token (void *cls GNUNET_UNUSED, void *client GNUNET_UNUSED,
        break;
       default:
        GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
-                   _("IPv6 packet with unsupported next header received.  Ignored.\n"));
+                   _("IPv6 packet with unsupported next header %d received.  Ignored.\n"),
+                    pkt6->next_header);
        return;
       }
     }
@@ -1098,7 +1110,8 @@ setup_fresh_address (int af,
   local_address->proto = (uint8_t) proto;
   /* default "local" port range is often 32768--61000,
      so we pick a random value in that range */         
-  if (proto == IPPROTO_ICMP)
+  if ( ( (af == AF_INET) && (proto == IPPROTO_ICMP) ) ||
+       ( (af == AF_INET6) && (proto == IPPROTO_ICMPV6) ) )
     local_address->port = 0;
   else
     local_address->port 
@@ -1152,7 +1165,7 @@ setup_fresh_address (int af,
       /* Given ABCD::/96, we want a mask of 'ABCD::FFFF:FFFF,
         thus: */
       mask = addr;
-      for (i=127;i>=128-ipv6prefix;i--)
+      for (i=127;i>=ipv6prefix;i--)
        mask.s6_addr[i / 8] |= (1 << (i % 8));
       
       /* Pick random IPv6 address within the subnet, except 'addr' or 'mask' itself */
@@ -1271,7 +1284,7 @@ setup_state_record (struct TunnelState *state)
  * @param tcp_header skeleton of the TCP header, NULL for UDP
  * @param src_address source address to use (IP and port)
  * @param dst_address destination address to use (IP and port)
- * @param pkt6 where to write the assembled packet; must
+ * @param pkt4 where to write the assembled packet; must
  *        contain enough space for the IP header, UDP/TCP header
  *        AND the payload
  */
@@ -1316,8 +1329,8 @@ prepare_ipv4_packet (const void *payload, size_t payload_length,
     {
       struct GNUNET_TUN_UdpHeader *pkt4_udp = (struct GNUNET_TUN_UdpHeader *) &pkt4[1];
 
-      pkt4_udp->spt = htons (src_address->port);
-      pkt4_udp->dpt = htons (dst_address->port);
+      pkt4_udp->source_port = htons (src_address->port);
+      pkt4_udp->destination_port = htons (dst_address->port);
       pkt4_udp->len = htons ((uint16_t) payload_length);
       GNUNET_TUN_calculate_udp4_checksum (pkt4,
                                          pkt4_udp,
@@ -1329,9 +1342,9 @@ prepare_ipv4_packet (const void *payload, size_t payload_length,
     {
       struct GNUNET_TUN_TcpHeader *pkt4_tcp = (struct GNUNET_TUN_TcpHeader *) &pkt4[1];
       
-      memcpy (pkt4_tcp, tcp_header, sizeof (struct GNUNET_TUN_TcpHeader));
-      pkt4_tcp->spt = htons (src_address->port);
-      pkt4_tcp->dpt = htons (dst_address->port);
+      *pkt4_tcp = *tcp_header;
+      pkt4_tcp->source_port = htons (src_address->port);
+      pkt4_tcp->destination_port = htons (dst_address->port);
       GNUNET_TUN_calculate_tcp4_checksum (pkt4,
                                          pkt4_tcp,
                                          payload,
@@ -1356,6 +1369,7 @@ prepare_ipv4_packet (const void *payload, size_t payload_length,
  *                TCP header, depending on protocol)
  * @param payload_length number of bytes in 'payload'
  * @param protocol IPPROTO_UDP or IPPROTO_TCP
+ * @param tcp_header skeleton TCP header data to send, NULL for UDP
  * @param src_address source address to use (IP and port)
  * @param dst_address destination address to use (IP and port)
  * @param pkt6 where to write the assembled packet; must
@@ -1393,9 +1407,9 @@ prepare_ipv6_packet (const void *payload, size_t payload_length,
 
   GNUNET_TUN_initialize_ipv6_header (pkt6,
                                     protocol,
-                                    len,
-                                    &dst_address->address.ipv6,
-                                    &src_address->address.ipv6);
+                                    len,                                  
+                                    &src_address->address.ipv6,
+                                    &dst_address->address.ipv6);
 
   switch (protocol)
   {
@@ -1403,29 +1417,29 @@ prepare_ipv6_packet (const void *payload, size_t payload_length,
     {
       struct GNUNET_TUN_UdpHeader *pkt6_udp = (struct GNUNET_TUN_UdpHeader *) &pkt6[1];
 
-      pkt6_udp->spt = htons (src_address->port);
-      pkt6_udp->dpt = htons (dst_address->port);
+      pkt6_udp->source_port = htons (src_address->port);
+      pkt6_udp->destination_port = htons (dst_address->port);
       pkt6_udp->len = htons ((uint16_t) payload_length);
-      pkt6_udp->crc = 0;
       GNUNET_TUN_calculate_udp6_checksum (pkt6,
                                          pkt6_udp,
                                          payload,
                                          payload_length);
-      memcpy (&pkt6[1], payload, payload_length);
+      memcpy (&pkt6_udp[1], payload, payload_length);
     }
     break;
   case IPPROTO_TCP:
     {
-      struct GNUNET_TUN_TcpHeader *pkt6_tcp = (struct GNUNET_TUN_TcpHeader *) pkt6;
+      struct GNUNET_TUN_TcpHeader *pkt6_tcp = (struct GNUNET_TUN_TcpHeader *) &pkt6[1];
 
       /* memcpy first here as some TCP header fields are initialized this way! */
-      memcpy (pkt6_tcp, payload, payload_length);
-      pkt6_tcp->spt = htons (src_address->port);
-      pkt6_tcp->dpt = htons (dst_address->port);
+      *pkt6_tcp = *tcp_header;
+      pkt6_tcp->source_port = htons (src_address->port);
+      pkt6_tcp->destination_port = htons (dst_address->port);
       GNUNET_TUN_calculate_tcp6_checksum (pkt6,
                                          pkt6_tcp,
                                          payload,
                                          payload_length);
+      memcpy (&pkt6_tcp[1], payload, payload_length);
     }
     break;
   default:
@@ -1479,7 +1493,7 @@ send_tcp_packet_via_tun (const struct SocketAddress *destination_address,
     return;
   }
   {
-    char buf[len];
+    char buf[len] GNUNET_ALIGN;
     struct GNUNET_MessageHeader *hdr;
     struct GNUNET_TUN_Layer2PacketHeader *tun;
     
@@ -1573,20 +1587,25 @@ receive_tcp_service (void *cls GNUNET_UNUSED, struct GNUNET_MESH_Tunnel *tunnel,
     GNUNET_break_op (0);
     return GNUNET_SYSERR;
   }
+  if (start->tcp_header.off * 4 < sizeof (struct GNUNET_TUN_TcpHeader))
+  {
+    GNUNET_break_op (0);
+    return GNUNET_SYSERR;
+  }
   GNUNET_break_op (ntohl (start->reserved) == 0);
   /* setup fresh connection */
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
              "Received data from %s for forwarding to TCP service %s on port %u\n",
              GNUNET_i2s (sender),
              GNUNET_h2s (&start->service_descriptor),
-             (unsigned int) ntohs (start->tcp_header.dpt));  
+             (unsigned int) ntohs (start->tcp_header.destination_port));  
   if (NULL == (state->serv = find_service (tcp_services, &start->service_descriptor, 
-                                          ntohs (start->tcp_header.dpt))))
+                                          ntohs (start->tcp_header.destination_port))))
   {
     GNUNET_log (GNUNET_ERROR_TYPE_INFO, 
                _("No service found for %s on port %d!\n"),
                "TCP",
-               ntohs (start->tcp_header.dpt));
+               ntohs (start->tcp_header.destination_port));
     GNUNET_STATISTICS_update (stats,
                              gettext_noop ("# TCP requests dropped (no such service)"),
                              1, GNUNET_NO);
@@ -1649,6 +1668,11 @@ receive_tcp_remote (void *cls GNUNET_UNUSED, struct GNUNET_MESH_Tunnel *tunnel,
     GNUNET_break_op (0);
     return GNUNET_SYSERR;
   }
+  if (start->tcp_header.off * 4 < sizeof (struct GNUNET_TUN_TcpHeader))
+  {
+    GNUNET_break_op (0);
+    return GNUNET_SYSERR;
+  }
   af = (int) ntohl (start->af);
   state->ri.remote_address.af = af;
   switch (af)
@@ -1697,11 +1721,10 @@ receive_tcp_remote (void *cls GNUNET_UNUSED, struct GNUNET_MESH_Tunnel *tunnel,
                inet_ntop (af, 
                           &state->ri.remote_address.address,
                           buf, sizeof (buf)),
-               (unsigned int) ntohs (start->tcp_header.dpt));  
+               (unsigned int) ntohs (start->tcp_header.destination_port));  
   }
-
   state->ri.remote_address.proto = IPPROTO_TCP;
-  state->ri.remote_address.port = ntohs (start->tcp_header.dpt);
+  state->ri.remote_address.port = ntohs (start->tcp_header.destination_port);
   setup_state_record (state);
   send_tcp_packet_via_tun (&state->ri.remote_address,
                           &state->ri.local_address,
@@ -1757,6 +1780,12 @@ receive_tcp_data (void *cls GNUNET_UNUSED, struct GNUNET_MESH_Tunnel *tunnel,
                              1, GNUNET_NO);
     return GNUNET_SYSERR;
   }
+  if (data->tcp_header.off * 4 < sizeof (struct GNUNET_TUN_TcpHeader))
+  {
+    GNUNET_break_op (0);
+    return GNUNET_SYSERR;
+  }
+
   GNUNET_break_op (ntohl (data->reserved) == 0);
   {
     char buf[INET6_ADDRSTRLEN];
@@ -1823,7 +1852,7 @@ send_icmp_packet_via_tun (const struct SocketAddress *destination_address,
     return;
   }
   {
-    char buf[len];
+    char buf[len] GNUNET_ALIGN;
     struct GNUNET_MessageHeader *hdr;
     struct GNUNET_TUN_Layer2PacketHeader *tun;
     
@@ -1853,7 +1882,7 @@ send_icmp_packet_via_tun (const struct SocketAddress *destination_address,
        
        tun->proto = htons (ETH_P_IPV6);
        GNUNET_TUN_initialize_ipv6_header (ipv6,
-                                          IPPROTO_ICMP,
+                                          IPPROTO_ICMPV6,
                                           sizeof (struct GNUNET_TUN_IcmpHeader) + payload_length,
                                           &source_address->address.ipv6,
                                           &destination_address->address.ipv6);
@@ -1898,8 +1927,8 @@ make_up_icmpv4_payload (struct TunnelState *state,
                                     sizeof (struct GNUNET_TUN_TcpHeader),
                                     &state->ri.remote_address.address.ipv4,
                                     &state->ri.local_address.address.ipv4);
-  udp->spt = htons (state->ri.remote_address.port);
-  udp->dpt = htons (state->ri.local_address.port);
+  udp->source_port = htons (state->ri.remote_address.port);
+  udp->destination_port = htons (state->ri.local_address.port);
   udp->len = htons (0);
   udp->crc = htons (0);
 }
@@ -1924,8 +1953,8 @@ make_up_icmpv6_payload (struct TunnelState *state,
                                     sizeof (struct GNUNET_TUN_TcpHeader),
                                     &state->ri.remote_address.address.ipv6,
                                     &state->ri.local_address.address.ipv6);
-  udp->spt = htons (state->ri.remote_address.port);
-  udp->dpt = htons (state->ri.local_address.port);
+  udp->source_port = htons (state->ri.remote_address.port);
+  udp->destination_port = htons (state->ri.local_address.port);
   udp->len = htons (0);
   udp->crc = htons (0);
 }
@@ -1956,7 +1985,7 @@ receive_icmp_remote (void *cls GNUNET_UNUSED, struct GNUNET_MESH_Tunnel *tunnel,
   const struct in_addr *v4;
   const struct in6_addr *v6;  
   const void *payload;
-  char buf[sizeof (struct GNUNET_TUN_IPv6Header) + 8];
+  char buf[sizeof (struct GNUNET_TUN_IPv6Header) + 8] GNUNET_ALIGN;
   int af;
 
   GNUNET_STATISTICS_update (stats,
@@ -2060,7 +2089,7 @@ receive_icmp_remote (void *cls GNUNET_UNUSED, struct GNUNET_MESH_Tunnel *tunnel,
     if (NULL == state->heap_node)
     {
       state->ri.remote_address.af = af;
-      state->ri.remote_address.proto = IPPROTO_ICMP;
+      state->ri.remote_address.proto = IPPROTO_ICMPV6;
       setup_state_record (state);
     }
     /* check that ICMP type is something we want to support 
@@ -2198,7 +2227,7 @@ receive_icmp_service (void *cls GNUNET_UNUSED, struct GNUNET_MESH_Tunnel *tunnel
   const struct GNUNET_EXIT_IcmpServiceMessage *msg;
   uint16_t pkt_len = ntohs (message->size);
   struct GNUNET_TUN_IcmpHeader icmp;
-  char buf[sizeof (struct GNUNET_TUN_IPv6Header) + 8];
+  char buf[sizeof (struct GNUNET_TUN_IPv6Header) + 8] GNUNET_ALIGN;
   const void *payload;
 
   GNUNET_STATISTICS_update (stats,
@@ -2406,7 +2435,7 @@ send_udp_packet_via_tun (const struct SocketAddress *destination_address,
     return;
   }
   {
-    char buf[len];
+    char buf[len] GNUNET_ALIGN;
     struct GNUNET_MessageHeader *hdr;
     struct GNUNET_TUN_Layer2PacketHeader *tun;
     
@@ -2636,7 +2665,7 @@ receive_udp_service (void *cls GNUNET_UNUSED, struct GNUNET_MESH_Tunnel *tunnel,
 static void *
 new_tunnel (void *cls GNUNET_UNUSED, struct GNUNET_MESH_Tunnel *tunnel,
             const struct GNUNET_PeerIdentity *initiator GNUNET_UNUSED,
-            const struct GNUNET_ATS_Information *ats GNUNET_UNUSED)
+            const struct GNUNET_ATS_Information *atsi GNUNET_UNUSED)
 {
   struct TunnelState *s = GNUNET_malloc (sizeof (struct TunnelState));
 
@@ -2933,6 +2962,31 @@ read_service_conf (void *cls GNUNET_UNUSED, const char *section)
 }
 
 
+/**
+ * Test if the given AF is supported by this system.
+ * 
+ * @param af to test
+ * @return GNUNET_OK if the AF is supported
+ */
+static int
+test_af (int af)
+{
+  int s;
+
+  s = socket (af, SOCK_STREAM, 0);
+  if (-1 == s)
+  {
+    if (EAFNOSUPPORT == errno)
+      return GNUNET_NO;
+    GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR,
+                        "socket");
+    return GNUNET_SYSERR;
+  }
+  close (s);
+  return GNUNET_OK;
+}
+
+
 /**
  * @brief Main function that will be run by the scheduler.
  *
@@ -2973,9 +3027,9 @@ run (void *cls, char *const *args GNUNET_UNUSED,
   if (GNUNET_YES !=
       GNUNET_OS_check_helper_binary ("gnunet-helper-exit"))
   {
-    fprintf (stderr,
-            "`%s' is not SUID, refusing to run.\n",
-            "gnunet-helper-exit");
+    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+               _("`%s' must be installed SUID, refusing to run\n"),
+               "gnunet-helper-exit");
     global_ret = 1;
     return;
   }
@@ -2985,6 +3039,23 @@ run (void *cls, char *const *args GNUNET_UNUSED,
   ipv6_exit = GNUNET_CONFIGURATION_get_value_yesno (cfg, "exit", "EXIT_IPV6"); 
   ipv4_enabled = GNUNET_CONFIGURATION_get_value_yesno (cfg, "exit", "ENABLE_IPV4");
   ipv6_enabled = GNUNET_CONFIGURATION_get_value_yesno (cfg, "exit", "ENABLE_IPV6"); 
+
+  if ( (ipv4_exit || ipv4_enabled) &&
+       GNUNET_OK != test_af (AF_INET))
+  {
+    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+               _("This system does not support IPv4, will disable IPv4 functions despite them being enabled in the configuration\n"));
+    ipv4_exit = GNUNET_NO;
+    ipv4_enabled = GNUNET_NO;
+  }
+  if ( (ipv6_exit || ipv6_enabled) &&
+       GNUNET_OK != test_af (AF_INET6))
+  {
+    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+               _("This system does not support IPv6, will disable IPv6 functions despite them being enabled in the configuration\n"));
+    ipv6_exit = GNUNET_NO;
+    ipv6_enabled = GNUNET_NO;
+  }
   if (ipv4_exit && (! ipv4_enabled))
   {
     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
@@ -3048,6 +3119,8 @@ run (void *cls, char *const *args GNUNET_UNUSED,
   {
     exit_argv[2] = GNUNET_strdup ("%");
   }
+  
+
   if (GNUNET_YES == ipv6_enabled)
   {
     if ( (GNUNET_SYSERR ==