-building IPv4 TCP reply messages for TUN
[oweals/gnunet.git] / src / vpn / gnunet-daemon-vpn.c
index 1191dffe19a7d350588b765d08d3063d2abaee13..6945fc772a232ea83871e3108b32b2e291f4236b 100644 (file)
 #include "gnunet_container_lib.h"
 #include "gnunet_constants.h"
 #include <block_dns.h>
-#include "gnunet-daemon-vpn-helper.h"
-#include "gnunet-daemon-vpn-dns.h"
-#include "gnunet-daemon-vpn.h"
-#include "gnunet-vpn-checksum.h"
-
-const struct GNUNET_CONFIGURATION_Handle *cfg;
-struct GNUNET_MESH_Handle *mesh_handle;
-struct GNUNET_CONTAINER_MultiHashMap *hashmap;
-static struct GNUNET_CONTAINER_Heap *heap;
+#include "gnunet_dns_service.h"
+
+
+struct answer_packet_list
+{
+  struct answer_packet_list *next;
+  struct answer_packet_list *prev;
+  struct GNUNET_SERVER_Client *client;
+  struct answer_packet pkt;
+};
+
+
+struct map_entry
+{
+    /** The description of the service (used for service) */
+  struct GNUNET_vpn_service_descriptor desc;
+
+    /** The real address of the service (used for remote) */
+  char addrlen;
+  char addr[16];
+
+  struct GNUNET_MESH_Tunnel *tunnel;
+  uint16_t namelen;
+  char additional_ports[8192];
+
+  struct GNUNET_CONTAINER_HeapNode *heap_node;
+  GNUNET_HashCode hash;
+    /**
+     * After this struct the name is located in DNS-Format!
+     */
+};
+
+
+struct remote_addr
+{
+  char addrlen;
+  unsigned char addr[16];
+  char proto;
+};
+
 
 struct tunnel_notify_queue
 {
@@ -53,6 +84,40 @@ struct tunnel_notify_queue
   void *cls;
 };
 
+
+struct tunnel_state
+{
+  struct GNUNET_MESH_TransmitHandle *th;
+  struct tunnel_notify_queue *head, *tail;
+
+  int addrlen;
+};
+
+
+static const struct GNUNET_CONFIGURATION_Handle *cfg;
+
+static struct GNUNET_MESH_Handle *mesh_handle;
+
+static struct GNUNET_CONTAINER_MultiHashMap *hashmap;
+
+static struct GNUNET_CONTAINER_Heap *heap;
+
+/**
+ * The handle to the helper
+ */
+static struct GNUNET_HELPER_Handle *helper_handle;
+
+/**
+ * Arguments to the exit helper.
+ */
+static char *vpn_argv[7];
+
+static struct GNUNET_DNS_Handle *dns_handle;
+
+static struct answer_packet_list *answer_proc_head;
+
+static struct answer_packet_list *answer_proc_tail;
+
 /**
  * If there are at least this many address-mappings, old ones will be removed
  */
@@ -69,36 +134,90 @@ static int ret;
  */
 static struct GNUNET_CONTAINER_MultiHashMap *udp_connections;
 
-GNUNET_SCHEDULER_TaskIdentifier conn_task;
+static GNUNET_SCHEDULER_TaskIdentifier conn_task;
+
+static GNUNET_SCHEDULER_TaskIdentifier shs_task;
+
+/**
+ * The tunnels that will be used to send tcp- and udp-packets
+ */
+static struct GNUNET_MESH_Tunnel *tcp_tunnel;
+static struct GNUNET_MESH_Tunnel *udp_tunnel;
+
+
+static unsigned int
+port_in_ports (uint64_t ports, uint16_t port)
+{
+  uint16_t *ps = (uint16_t *) & ports;
+
+  return ports == 0 || ps[0] == port || ps[1] == port || ps[2] == port ||
+      ps[3] == port;
+}
+
+
+/**
+ * Sets a bit active in a bitArray.
+ *
+ * @param bitArray memory area to set the bit in
+ * @param bitIdx which bit to set
+ */
+static void
+setBit (char *bitArray, unsigned int bitIdx)
+{
+  size_t arraySlot;
+  unsigned int targetBit;
+
+  arraySlot = bitIdx / 8;
+  targetBit = (1L << (bitIdx % 8));
+  bitArray[arraySlot] |= targetBit;
+}
+
+
+/**
+ * Checks if a bit is active in the bitArray
+ *
+ * @param bitArray memory area to set the bit in
+ * @param bitIdx which bit to test
+ * @return GNUNET_YES if the bit is set, GNUNET_NO if not.
+ */
+static int
+testBit (char *bitArray, unsigned int bitIdx)
+{
+  size_t slot;
+  unsigned int targetBit;
+
+  slot = bitIdx / 8;
+  targetBit = (1L << (bitIdx % 8));
+  if (bitArray[slot] & targetBit)
+    return GNUNET_YES;
+  else
+    return GNUNET_NO;
+}
 
-GNUNET_SCHEDULER_TaskIdentifier shs_task;
 
 /**
  * Function scheduled as very last function, cleans up after us
  *{{{
  */
 static void
-cleanup (void *cls
-         __attribute__ ((unused)),
+cleanup (void *cls GNUNET_UNUSED,
          const struct GNUNET_SCHEDULER_TaskContext *tskctx)
 {
-  GNUNET_assert (0 != (tskctx->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN));
-
-  /* stop the helper */
-  cleanup_helper (helper_handle);
+  unsigned int i;
 
+  GNUNET_assert (0 != (tskctx->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN));
   /* close the connection to the service-dns */
-  if (dns_connection != NULL)
-  {
-    GNUNET_CLIENT_disconnect (dns_connection, GNUNET_NO);
-    dns_connection = NULL;
-  }
-
+  GNUNET_DNS_disconnect (dns_handle);
   if (mesh_handle != NULL)
   {
     GNUNET_MESH_disconnect (mesh_handle);
     mesh_handle = NULL;
   }
+  if (helper_handle != NULL)
+  {
+    GNUNET_HELPER_stop (helper_handle);
+    helper_handle = NULL;
+  }
   if (GNUNET_SCHEDULER_NO_TASK != shs_task)
   {
     GNUNET_SCHEDULER_cancel (shs_task);
@@ -109,6 +228,8 @@ cleanup (void *cls
     GNUNET_SCHEDULER_cancel (conn_task);
     conn_task = GNUNET_SCHEDULER_NO_TASK;
   }
+  for (i=0;i<5;i++)
+    GNUNET_free_non_null (vpn_argv[i]);
 }
 
 /*}}}*/
@@ -116,9 +237,10 @@ cleanup (void *cls
 /**
  * @return the hash of the IP-Address if a mapping exists, NULL otherwise
  */
-GNUNET_HashCode *
-address6_mapping_exists (unsigned char addr[])
+static GNUNET_HashCode *
+address6_mapping_exists (struct in6_addr *v6addr)
 {
+  unsigned char *addr = (unsigned char*) v6addr;
   GNUNET_HashCode *key = GNUNET_malloc (sizeof (GNUNET_HashCode));
   unsigned char *k = (unsigned char *) key;
 
@@ -140,7 +262,7 @@ address6_mapping_exists (unsigned char addr[])
 /**
  * @return the hash of the IP-Address if a mapping exists, NULL otherwise
  */
-GNUNET_HashCode *
+static GNUNET_HashCode *
 address4_mapping_exists (uint32_t addr)
 {
   GNUNET_HashCode *key = GNUNET_malloc (sizeof (GNUNET_HashCode));
@@ -167,28 +289,19 @@ address4_mapping_exists (uint32_t addr)
   }
 }
 
-static void
-collect_mappings (void *cls
-                  __attribute__ ((unused)),
-                  const struct GNUNET_SCHEDULER_TaskContext *tc)
-{
-  if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
-    return;
 
-  struct map_entry *me = GNUNET_CONTAINER_heap_remove_root (heap);
-
-  /* This is free()ed memory! */
-  me->heap_node = NULL;
-
-  /* FIXME! GNUNET_MESH_close_tunnel(me->tunnel); */
-
-  GNUNET_assert (GNUNET_YES ==
-                GNUNET_CONTAINER_multihashmap_remove (hashmap, &me->hash, me));
+static void *
+initialize_tunnel_state (int addrlen, struct GNUNET_MESH_TransmitHandle *th)
+{
+  struct tunnel_state *ts = GNUNET_malloc (sizeof *ts);
 
-  GNUNET_free (me);
+  ts->addrlen = addrlen;
+  ts->th = th;
+  return ts;
 }
 
-void
+
+static void
 send_icmp4_response (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
 {
   if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
@@ -215,7 +328,7 @@ send_icmp4_response (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
   response->ip_hdr.tot_lngth = request->ip_hdr.tot_lngth;
 
   response->ip_hdr.chks =
-      calculate_ip_checksum ((uint16_t *) & response->ip_hdr, 20);
+      GNUNET_CRYPTO_crc16_n ((uint16_t *) & response->ip_hdr, 20);
 
   response->icmp_hdr.code = 0;
   response->icmp_hdr.type = 0x0;
@@ -227,12 +340,15 @@ send_icmp4_response (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
   memcpy (response + 1, request + 1,
           ntohs (request->shdr.size) - sizeof (struct ip_icmp));
 
-  write_to_helper (response, ntohs (response->shdr.size));
-
+  (void) GNUNET_HELPER_send (helper_handle,
+                            &response->shdr,
+                            GNUNET_YES,
+                            NULL, NULL);
   GNUNET_free (request);
 }
 
-void
+
+static void
 send_icmp6_response (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
 {
   if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
@@ -268,11 +384,14 @@ send_icmp6_response (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
   memcpy (response + 1, request + 1,
           ntohs (request->shdr.size) - sizeof (struct ip6_icmp));
 
-  write_to_helper (response, ntohs (response->shdr.size));
-
+  (void) GNUNET_HELPER_send (helper_handle,
+                            &response->shdr,
+                            GNUNET_YES,
+                            NULL, NULL);
   GNUNET_free (request);
 }
 
+
 /**
  * cls is the pointer to a GNUNET_MessageHeader that is
  * followed by the service-descriptor and the packet that should be sent;
@@ -282,21 +401,22 @@ send_pkt_to_peer_notify_callback (void *cls, size_t size, void *buf)
 {
   struct GNUNET_MESH_Tunnel **tunnel = cls;
 
-  struct tunnel_state *ts = GNUNET_MESH_tunnel_get_data(*tunnel);
+  struct tunnel_state *ts = GNUNET_MESH_tunnel_get_data (*tunnel);
+
   ts->th = NULL;
 
   if (NULL != buf)
-    {
-      struct GNUNET_MessageHeader *hdr =
+  {
+    struct GNUNET_MessageHeader *hdr =
         (struct GNUNET_MessageHeader *) (tunnel + 1);
-      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                  "send_pkt_to_peer_notify_callback: buf = %x; size = %u;\n", buf,
-                  size);
-      GNUNET_assert (size >= ntohs (hdr->size));
-      memcpy (buf, hdr, ntohs (hdr->size));
-      size = ntohs (hdr->size);
-      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sent!\n");
-    }
+    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                "send_pkt_to_peer_notify_callback: buf = %x; size = %u;\n", buf,
+                size);
+    GNUNET_assert (size >= ntohs (hdr->size));
+    memcpy (buf, hdr, ntohs (hdr->size));
+    size = ntohs (hdr->size);
+    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sent!\n");
+  }
   else
     size = 0;
 
@@ -307,9 +427,7 @@ send_pkt_to_peer_notify_callback (void *cls, size_t size, void *buf)
     GNUNET_CONTAINER_DLL_remove (ts->head, ts->tail, element);
 
     ts->th =
-        GNUNET_MESH_notify_transmit_ready (*tunnel,
-                                           GNUNET_NO,
-                                           42,
+        GNUNET_MESH_notify_transmit_ready (*tunnel, GNUNET_NO, 42,
                                            GNUNET_TIME_relative_divide
                                            (GNUNET_CONSTANTS_MAX_CORK_DELAY, 2),
                                            (const struct GNUNET_PeerIdentity *)
@@ -325,17 +443,10 @@ send_pkt_to_peer_notify_callback (void *cls, size_t size, void *buf)
   return size;
 }
 
-unsigned int
-port_in_ports (uint64_t ports, uint16_t port)
-{
-  uint16_t *ps = (uint16_t *) & ports;
-
-  return ports == 0 || ps[0] == port || ps[1] == port || ps[2] == port || ps[3] == port;
-}
 
-void
+static void
 send_pkt_to_peer (void *cls, const struct GNUNET_PeerIdentity *peer,
-                  const struct GNUNET_ATS_Information *atsi __attribute__ ((unused)))
+                  const struct GNUNET_ATS_Information *atsi GNUNET_UNUSED)
 {
   /* peer == NULL means that all peers in this request are connected */
   if (peer == NULL)
@@ -347,18 +458,16 @@ send_pkt_to_peer (void *cls, const struct GNUNET_PeerIdentity *peer,
   GNUNET_assert (NULL != tunnel);
   GNUNET_assert (NULL != *tunnel);
 
-  struct tunnel_state *ts = GNUNET_MESH_tunnel_get_data(*tunnel);
+  struct tunnel_state *ts = GNUNET_MESH_tunnel_get_data (*tunnel);
+
   if (NULL == ts->th)
   {
     ts->th =
-        GNUNET_MESH_notify_transmit_ready (*tunnel,
-                                           GNUNET_NO,
-                                           42,
+        GNUNET_MESH_notify_transmit_ready (*tunnel, GNUNET_NO, 42,
                                            GNUNET_TIME_relative_divide
                                            (GNUNET_CONSTANTS_MAX_CORK_DELAY, 2),
                                            (const struct GNUNET_PeerIdentity *)
-                                           NULL,
-                                           ntohs (hdr->size),
+                                           NULL, ntohs (hdr->size),
                                            send_pkt_to_peer_notify_callback,
                                            cls);
   }
@@ -373,13 +482,409 @@ send_pkt_to_peer (void *cls, const struct GNUNET_PeerIdentity *peer,
   }
 }
 
+
+
+
+/**
+ * Receive packets from the helper-process
+ */
+static void
+message_token (void *cls GNUNET_UNUSED, void *client GNUNET_UNUSED,
+               const struct GNUNET_MessageHeader *message)
+{
+  GNUNET_assert (ntohs (message->type) == GNUNET_MESSAGE_TYPE_VPN_HELPER);
+
+  struct tun_pkt *pkt_tun = (struct tun_pkt *) message;
+  GNUNET_HashCode *key;
+
+  /* ethertype is ipv6 */
+  if (ntohs (pkt_tun->tun.type) == 0x86dd)
+  {
+    struct ip6_pkt *pkt6 = (struct ip6_pkt *) message;
+
+    GNUNET_assert (pkt6->ip6_hdr.version == 6);
+    struct ip6_tcp *pkt6_tcp;
+    struct ip6_udp *pkt6_udp;
+    struct ip6_icmp *pkt6_icmp;
+
+    pkt6_udp = NULL;            /* make compiler happy */
+    switch (pkt6->ip6_hdr.nxthdr)
+    {
+    case IPPROTO_UDP:
+      pkt6_udp = (struct ip6_udp *) pkt6;
+      /* Send dns-packets to the service-dns */
+      if (ntohs (pkt6_udp->udp_hdr.dpt) == 53)
+      {
+        /* 9 = 8 for the udp-header + 1 for the unsigned char data[1]; */
+       GNUNET_DNS_queue_request_v6 (dns_handle,
+                                    &pkt6->ip6_hdr.dadr,
+                                    &pkt6->ip6_hdr.sadr,
+                                    ntohs (pkt6_udp->udp_hdr.spt),
+                                    ntohs (pkt6_udp->udp_hdr.len) - 8,
+                                    (const void*) pkt6_udp->data);
+
+        break;
+      }
+      /* fall through */
+    case IPPROTO_TCP:
+      pkt6_tcp = (struct ip6_tcp *) pkt6;
+
+      if ((key = address6_mapping_exists (&pkt6->ip6_hdr.dadr)) != NULL)
+      {
+        struct map_entry *me = GNUNET_CONTAINER_multihashmap_get (hashmap, key);
+
+        GNUNET_assert (me != NULL);
+        GNUNET_free (key);
+
+        size_t size =
+            sizeof (struct GNUNET_MESH_Tunnel *) +
+            sizeof (struct GNUNET_MessageHeader) + sizeof (GNUNET_HashCode) +
+            ntohs (pkt6->ip6_hdr.paylgth);
+
+        struct GNUNET_MESH_Tunnel **cls = GNUNET_malloc (size);
+        struct GNUNET_MessageHeader *hdr =
+            (struct GNUNET_MessageHeader *) (cls + 1);
+        GNUNET_HashCode *hc = (GNUNET_HashCode *) (hdr + 1);
+
+        hdr->size =
+            htons (sizeof (struct GNUNET_MessageHeader) +
+                   sizeof (GNUNET_HashCode) + ntohs (pkt6->ip6_hdr.paylgth));
+
+        GNUNET_MESH_ApplicationType app_type = 0;       /* fix compiler uninitialized warning... */
+
+        GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "me->addrlen is %d\n",
+                    me->addrlen);
+        if (me->addrlen == 0)
+        {
+          /* This is a mapping to a gnunet-service */
+          memcpy (hc, &me->desc.service_descriptor, sizeof (GNUNET_HashCode));
+
+          if (IPPROTO_UDP == pkt6->ip6_hdr.nxthdr &&
+              (me->desc.service_type & htonl (GNUNET_DNS_SERVICE_TYPE_UDP)) &&
+              (port_in_ports (me->desc.ports, pkt6_udp->udp_hdr.dpt) ||
+               testBit (me->additional_ports, ntohs (pkt6_udp->udp_hdr.dpt))))
+          {
+            hdr->type = htons (GNUNET_MESSAGE_TYPE_VPN_SERVICE_UDP);
+
+            memcpy (hc + 1, &pkt6_udp->udp_hdr, ntohs (pkt6_udp->udp_hdr.len));
+
+          }
+          else if (IPPROTO_TCP == pkt6->ip6_hdr.nxthdr &&
+                   (me->desc.service_type & htonl (GNUNET_DNS_SERVICE_TYPE_TCP))
+                   && (port_in_ports (me->desc.ports, pkt6_tcp->tcp_hdr.dpt)))
+          {
+            hdr->type = htons (GNUNET_MESSAGE_TYPE_VPN_SERVICE_TCP);
+
+            memcpy (hc + 1, &pkt6_tcp->tcp_hdr, ntohs (pkt6->ip6_hdr.paylgth));
+
+          }
+          else
+          {
+            GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "pip: %d\n",
+                        port_in_ports (me->desc.ports, pkt6_tcp->tcp_hdr.dpt));
+            GNUNET_assert (0);
+          }
+          if (me->tunnel == NULL && NULL != cls)
+          {
+            *cls =
+                GNUNET_MESH_tunnel_create (mesh_handle,
+                                           initialize_tunnel_state (16, NULL),
+                                           &send_pkt_to_peer, NULL, cls);
+
+            GNUNET_MESH_peer_request_connect_add (*cls,
+                                                  (struct GNUNET_PeerIdentity *)
+                                                  &me->desc.peer);
+            me->tunnel = *cls;
+          }
+          else if (NULL != cls)
+          {
+            *cls = me->tunnel;
+            send_pkt_to_peer (cls, (struct GNUNET_PeerIdentity *) 1, NULL);
+            GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                        "Queued to send IPv6 to peer %x, type %d\n",
+                        *((unsigned int *) &me->desc.peer), ntohs (hdr->type));
+          }
+        }
+        else
+        {
+          /* This is a mapping to a "real" address */
+          struct remote_addr *s = (struct remote_addr *) hc;
+
+          s->addrlen = me->addrlen;
+          memcpy (s->addr, me->addr, me->addrlen);
+          s->proto = pkt6->ip6_hdr.nxthdr;
+          if (s->proto == IPPROTO_UDP)
+          {
+            hdr->type = htons (GNUNET_MESSAGE_TYPE_VPN_REMOTE_UDP);
+            memcpy (hc + 1, &pkt6_udp->udp_hdr, ntohs (pkt6_udp->udp_hdr.len));
+            app_type = GNUNET_APPLICATION_TYPE_INTERNET_UDP_GATEWAY;
+            if (NULL != udp_tunnel)
+              me->tunnel = udp_tunnel;
+          }
+          else if (s->proto == IPPROTO_TCP)
+          {
+            hdr->type = htons (GNUNET_MESSAGE_TYPE_VPN_REMOTE_TCP);
+            memcpy (hc + 1, &pkt6_tcp->tcp_hdr, ntohs (pkt6->ip6_hdr.paylgth));
+            app_type = GNUNET_APPLICATION_TYPE_INTERNET_TCP_GATEWAY;
+            if (NULL != tcp_tunnel)
+              me->tunnel = tcp_tunnel;
+          }
+          else
+          {
+            GNUNET_assert (0);
+          }
+          if (me->tunnel == NULL && NULL != cls)
+          {
+            *cls =
+                GNUNET_MESH_tunnel_create (mesh_handle,
+                                           initialize_tunnel_state (16, NULL),
+                                           &send_pkt_to_peer, NULL, cls);
+
+            GNUNET_MESH_peer_request_connect_by_type (*cls, app_type);
+            me->tunnel = *cls;
+            if (GNUNET_APPLICATION_TYPE_INTERNET_UDP_GATEWAY == app_type)
+              udp_tunnel = *cls;
+            else if (GNUNET_APPLICATION_TYPE_INTERNET_TCP_GATEWAY == app_type)
+              tcp_tunnel = *cls;
+          }
+          else if (NULL != cls)
+          {
+            *cls = me->tunnel;
+            send_pkt_to_peer (cls, (struct GNUNET_PeerIdentity *) 1, NULL);
+          }
+        }
+      }
+      else
+      {
+       char pbuf[INET6_ADDRSTRLEN];
+        GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                    "Packet to %s, which has no mapping\n",
+                   inet_ntop (AF_INET6,
+                              &pkt6->ip6_hdr.dadr,
+                              pbuf,
+                              sizeof (pbuf)));
+      }
+      break;
+    case 0x3a:
+      /* ICMPv6 */
+      pkt6_icmp = (struct ip6_icmp *) pkt6;
+      /* If this packet is an icmp-echo-request and a mapping exists, answer */
+      if (pkt6_icmp->icmp_hdr.type == 0x80 &&
+          (key = address6_mapping_exists (&pkt6->ip6_hdr.dadr)) != NULL)
+      {
+        GNUNET_free (key);
+        pkt6_icmp = GNUNET_malloc (ntohs (pkt6->shdr.size));
+        memcpy (pkt6_icmp, pkt6, ntohs (pkt6->shdr.size));
+        GNUNET_SCHEDULER_add_now (&send_icmp6_response, pkt6_icmp);
+      }
+      break;
+    }
+  }
+  /* ethertype is ipv4 */
+  else if (ntohs (pkt_tun->tun.type) == 0x0800)
+  {
+    struct ip_pkt *pkt = (struct ip_pkt *) message;
+    struct ip_udp *udp = (struct ip_udp *) message;
+    struct ip_tcp *pkt_tcp;
+    struct ip_udp *pkt_udp;
+    struct ip_icmp *pkt_icmp;
+
+    GNUNET_assert (pkt->ip_hdr.version == 4);
+
+    /* Send dns-packets to the service-dns */
+    if (pkt->ip_hdr.proto == IPPROTO_UDP && ntohs (udp->udp_hdr.dpt) == 53)
+    {
+      GNUNET_DNS_queue_request_v4 (dns_handle,
+                                  &pkt->ip_hdr.dadr,
+                                  &pkt->ip_hdr.sadr,
+                                  ntohs (udp->udp_hdr.spt),
+                                  ntohs (udp->udp_hdr.len) - 8,
+                                  (const void*) udp->data);
+    }
+    else
+    {
+      uint32_t dadr = pkt->ip_hdr.dadr.s_addr;
+      unsigned char *c = (unsigned char *) &dadr;
+
+      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Packet to %d.%d.%d.%d, proto %x\n",
+                  c[0], c[1], c[2], c[3], pkt->ip_hdr.proto);
+      switch (pkt->ip_hdr.proto)
+      {
+      case IPPROTO_TCP:
+      case IPPROTO_UDP:
+        pkt_tcp = (struct ip_tcp *) pkt;
+        pkt_udp = (struct ip_udp *) pkt;
+
+        if ((key = address4_mapping_exists (dadr)) != NULL)
+        {
+          struct map_entry *me =
+              GNUNET_CONTAINER_multihashmap_get (hashmap, key);
+          GNUNET_assert (me != NULL);
+          GNUNET_free (key);
+
+          size_t size =
+              sizeof (struct GNUNET_MESH_Tunnel *) +
+              sizeof (struct GNUNET_MessageHeader) + sizeof (GNUNET_HashCode) +
+              ntohs (pkt->ip_hdr.tot_lngth) - 4 * pkt->ip_hdr.hdr_lngth;
+
+          struct GNUNET_MESH_Tunnel **cls = GNUNET_malloc (size);
+          struct GNUNET_MessageHeader *hdr =
+              (struct GNUNET_MessageHeader *) (cls + 1);
+          GNUNET_HashCode *hc = (GNUNET_HashCode *) (hdr + 1);
+
+          hdr->size =
+              htons (sizeof (struct GNUNET_MessageHeader) +
+                     sizeof (GNUNET_HashCode) + ntohs (pkt->ip_hdr.tot_lngth) -
+                     4 * pkt->ip_hdr.hdr_lngth);
+
+          GNUNET_MESH_ApplicationType app_type = 0; /* make compiler happy */
+
+          GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "me->addrlen is %d\n",
+                      me->addrlen);
+          if (me->addrlen == 0)
+          {
+            /* This is a mapping to a gnunet-service */
+            memcpy (hc, &me->desc.service_descriptor, sizeof (GNUNET_HashCode));
+
+            if ((IPPROTO_UDP == pkt->ip_hdr.proto) &&
+                (me->desc.service_type & htonl (GNUNET_DNS_SERVICE_TYPE_UDP)) &&
+                (port_in_ports (me->desc.ports, pkt_udp->udp_hdr.dpt) ||
+                 testBit (me->additional_ports, ntohs (pkt_udp->udp_hdr.dpt))))
+            {
+              hdr->type = htons (GNUNET_MESSAGE_TYPE_VPN_SERVICE_UDP);
+
+              memcpy (hc + 1, &pkt_udp->udp_hdr, ntohs (pkt_udp->udp_hdr.len));
+
+            }
+            else if ((IPPROTO_TCP == pkt->ip_hdr.proto) &&
+                     (me->
+                      desc.service_type & htonl (GNUNET_DNS_SERVICE_TYPE_TCP))
+                     && (port_in_ports (me->desc.ports, pkt_tcp->tcp_hdr.dpt)))
+            {
+              hdr->type = htons (GNUNET_MESSAGE_TYPE_VPN_SERVICE_TCP);
+
+              memcpy (hc + 1, &pkt_tcp->tcp_hdr,
+                      ntohs (pkt->ip_hdr.tot_lngth) -
+                      4 * pkt->ip_hdr.hdr_lngth);
+
+            }
+            if (me->tunnel == NULL && NULL != cls)
+            {
+              *cls =
+                  GNUNET_MESH_tunnel_create (mesh_handle,
+                                             initialize_tunnel_state (4, NULL),
+                                             send_pkt_to_peer, NULL, cls);
+              GNUNET_MESH_peer_request_connect_add (*cls,
+                                                    (struct GNUNET_PeerIdentity
+                                                     *) &me->desc.peer);
+              me->tunnel = *cls;
+            }
+            else if (NULL != cls)
+            {
+              *cls = me->tunnel;
+              send_pkt_to_peer (cls, (struct GNUNET_PeerIdentity *) 1, NULL);
+              GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                          "Queued to send IPv4 to peer %x, type %d\n",
+                          *((unsigned int *) &me->desc.peer),
+                          ntohs (hdr->type));
+            }
+          }
+          else
+          {
+            /* This is a mapping to a "real" address */
+            struct remote_addr *s = (struct remote_addr *) hc;
+
+            s->addrlen = me->addrlen;
+            memcpy (s->addr, me->addr, me->addrlen);
+            s->proto = pkt->ip_hdr.proto;
+            if (s->proto == IPPROTO_UDP)
+            {
+              hdr->type = htons (GNUNET_MESSAGE_TYPE_VPN_REMOTE_UDP);
+              memcpy (hc + 1, &pkt_udp->udp_hdr, ntohs (pkt_udp->udp_hdr.len));
+              app_type = GNUNET_APPLICATION_TYPE_INTERNET_UDP_GATEWAY;
+            }
+            else if (s->proto == IPPROTO_TCP)
+            {
+              hdr->type = htons (GNUNET_MESSAGE_TYPE_VPN_REMOTE_TCP);
+              memcpy (hc + 1, &pkt_tcp->tcp_hdr,
+                      ntohs (pkt->ip_hdr.tot_lngth) -
+                      4 * pkt->ip_hdr.hdr_lngth);
+              app_type = GNUNET_APPLICATION_TYPE_INTERNET_TCP_GATEWAY;
+            }
+            else
+              GNUNET_assert (0);
+            if (me->tunnel == NULL && NULL != cls)
+            {
+              *cls =
+                  GNUNET_MESH_tunnel_create (mesh_handle,
+                                             initialize_tunnel_state (4, NULL),
+                                             send_pkt_to_peer, NULL, cls);
+
+              GNUNET_MESH_peer_request_connect_by_type (*cls, app_type);
+              me->tunnel = *cls;
+            }
+            else if (NULL != cls)
+            {
+              *cls = me->tunnel;
+              send_pkt_to_peer (cls, (struct GNUNET_PeerIdentity *) 1, NULL);
+            }
+          }
+        }
+        else
+        {
+          GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                      "Packet to %x which has no mapping\n", dadr);
+        }
+        break;
+      case 0x01:
+        /* ICMP */
+        pkt_icmp = (struct ip_icmp *) pkt;
+        if (pkt_icmp->icmp_hdr.type == 0x8 &&
+            (key = address4_mapping_exists (dadr)) != NULL)
+        {
+          GNUNET_free (key);
+          pkt_icmp = GNUNET_malloc (ntohs (pkt->shdr.size));
+          memcpy (pkt_icmp, pkt, ntohs (pkt->shdr.size));
+          GNUNET_SCHEDULER_add_now (&send_icmp4_response, pkt_icmp);
+        }
+        break;
+      }
+    }
+  }
+}
+
+
+
+static void
+collect_mappings (void *cls GNUNET_UNUSED,
+                  const struct GNUNET_SCHEDULER_TaskContext *tc)
+{
+  if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
+    return;
+
+  struct map_entry *me = GNUNET_CONTAINER_heap_remove_root (heap);
+
+  /* This is free()ed memory! */
+  me->heap_node = NULL;
+
+  /* FIXME! GNUNET_MESH_close_tunnel(me->tunnel); */
+
+  GNUNET_assert (GNUNET_YES ==
+                 GNUNET_CONTAINER_multihashmap_remove (hashmap, &me->hash, me));
+
+  GNUNET_free (me);
+}
+
+
 /**
  * Create a new Address from an answer-packet
  */
-void
-new_ip6addr (unsigned char *buf, const GNUNET_HashCode * peer,
+static void
+new_ip6addr (struct in6_addr *v6addr,
+            const GNUNET_HashCode * peer,
              const GNUNET_HashCode * service_desc)
 {                               /* {{{ */
+  unsigned char *buf = (unsigned char*) v6addr;
   char *ipv6addr;
   unsigned long long ipv6prefix;
 
@@ -416,9 +921,11 @@ new_ip6addr (unsigned char *buf, const GNUNET_HashCode * peer,
 /**
  * Create a new Address from an answer-packet
  */
-void
-new_ip6addr_remote (unsigned char *buf, unsigned char *addr, char addrlen)
+static void
+new_ip6addr_remote (struct in6_addr *v6addr,
+                   unsigned char *addr, char addrlen)
 {                               /* {{{ */
+  unsigned char *buf = (unsigned char*) v6addr;
   char *ipv6addr;
   unsigned long long ipv6prefix;
 
@@ -445,7 +952,7 @@ new_ip6addr_remote (unsigned char *buf, unsigned char *addr, char addrlen)
 /**
  * Create a new Address from an answer-packet
  */
-void
+static void
 new_ip4addr_remote (unsigned char *buf, unsigned char *addr, char addrlen)
 {                               /* {{{ */
   char *ipv4addr;
@@ -506,13 +1013,10 @@ new_ip4addr_remote (unsigned char *buf, unsigned char *addr, char addrlen)
  * At the moment this means "inventing" and IPv6-Address for .gnunet-services and
  * doing nothing for "real" services.
  */
-void
-process_answer (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
+static void
+process_answer (void *cls, 
+               const struct answer_packet *pkt)
 {
-  if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
-    return;
-
-  struct answer_packet *pkt = cls;
   struct answer_packet_list *list;
 
   /* This answer is about a .gnunet-service
@@ -522,16 +1026,22 @@ process_answer (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
    */
   if (pkt->subtype == GNUNET_DNS_ANSWER_TYPE_SERVICE)
   {
-    pkt->subtype = GNUNET_DNS_ANSWER_TYPE_IP;
 
     GNUNET_HashCode key;
 
     memset (&key, 0, sizeof (GNUNET_HashCode));
 
-    unsigned char *c = ((unsigned char *) pkt) + ntohs (pkt->addroffset);
+    list =
+        GNUNET_malloc (htons (pkt->hdr.size) +
+                       sizeof (struct answer_packet_list) -
+                       sizeof (struct answer_packet));
+    memcpy (&list->pkt, pkt, htons (pkt->hdr.size));
+
+    unsigned char *c = ((unsigned char *) &list->pkt) + ntohs (pkt->addroffset);
     unsigned char *k = (unsigned char *) &key;
 
-    new_ip6addr (c, &pkt->service_descr.peer,
+    new_ip6addr ((struct in6_addr*) c, 
+                &pkt->service_descr.peer,
                  &pkt->service_descr.service_descriptor);
     /*
      * Copy the newly generated ip-address to the key backwarts (as only the first part is hashed)
@@ -572,11 +1082,8 @@ process_answer (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
       GNUNET_free (value);
 
 
-    list =
-        GNUNET_malloc (htons (pkt->hdr.size) +
-                       sizeof(struct answer_packet_list) - sizeof(struct answer_packet));
+    list->pkt.subtype = GNUNET_DNS_ANSWER_TYPE_IP;
 
-    memcpy (&list->pkt, pkt, htons (pkt->hdr.size));
 
   }
   else if (pkt->subtype == GNUNET_DNS_ANSWER_TYPE_REV)
@@ -585,7 +1092,7 @@ process_answer (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
 
     memset (&key, 0, sizeof key);
     unsigned char *k = (unsigned char *) &key;
-    unsigned char *s = pkt->data + 12;
+    const unsigned char *s = pkt->data + 12;
     int i = 0;
 
     /* Whoever designed the reverse IPv6-lookup is batshit insane */
@@ -609,10 +1116,7 @@ process_answer (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
     uint16_t offset = ntohs (pkt->addroffset);
 
     if (map_entry == NULL)
-    {
-      GNUNET_free (pkt);
       return;
-    }
 
     GNUNET_CONTAINER_heap_update_cost (heap, map_entry->heap_node,
                                        GNUNET_TIME_absolute_get ().abs_value);
@@ -622,8 +1126,9 @@ process_answer (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
     char *name = (char *) (map_entry + 1);
 
     list =
-      GNUNET_malloc (sizeof(struct answer_packet_list) - sizeof(struct answer_packet) + offset + 2 +
-                     ntohs (namelen));
+        GNUNET_malloc (sizeof (struct answer_packet_list) -
+                       sizeof (struct answer_packet) + offset + 2 +
+                       ntohs (namelen));
 
     struct answer_packet *rpkt = &list->pkt;
 
@@ -641,20 +1146,29 @@ process_answer (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
   {
     list =
         GNUNET_malloc (htons (pkt->hdr.size) +
-                       sizeof(struct answer_packet_list) - sizeof(struct answer_packet));
+                       sizeof (struct answer_packet_list) -
+                       sizeof (struct answer_packet));
     memcpy (&list->pkt, pkt, htons (pkt->hdr.size));
   }
   else if (pkt->subtype == GNUNET_DNS_ANSWER_TYPE_REMOTE_AAAA)
   {
-    pkt->subtype = GNUNET_DNS_ANSWER_TYPE_IP;
 
     GNUNET_HashCode key;
 
     memset (&key, 0, sizeof (GNUNET_HashCode));
 
-    unsigned char *c = ((unsigned char *) pkt) + ntohs (pkt->addroffset);
+    list =
+        GNUNET_malloc (htons (pkt->hdr.size) +
+                       sizeof (struct answer_packet_list) -
+                       sizeof (struct answer_packet));
 
-    new_ip6addr_remote (c, pkt->addr, pkt->addrsize);
+    memcpy (&list->pkt, pkt, htons (pkt->hdr.size));
+    list->pkt.subtype = GNUNET_DNS_ANSWER_TYPE_IP;
+
+    unsigned char *c = ((unsigned char *) &list->pkt) + ntohs (list->pkt.addroffset);
+
+    new_ip6addr_remote ((struct in6_addr*) c,
+                       list->pkt.addr, list->pkt.addrsize);
     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                 "New mapping to %02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x\n",
                 c[0], c[1], c[2], c[3], c[4], c[5], c[6], c[7], c[8], c[9],
@@ -699,23 +1213,25 @@ process_answer (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
     else
       GNUNET_free (value);
 
-    list =
-        GNUNET_malloc (htons (pkt->hdr.size) +
-                       sizeof(struct answer_packet_list) - sizeof(struct answer_packet));
 
-    memcpy (&list->pkt, pkt, htons (pkt->hdr.size));
   }
   else if (pkt->subtype == GNUNET_DNS_ANSWER_TYPE_REMOTE_A)
   {
-    pkt->subtype = GNUNET_DNS_ANSWER_TYPE_IP;
+    list =
+        GNUNET_malloc (htons (pkt->hdr.size) +
+                       sizeof (struct answer_packet_list) -
+                       sizeof (struct answer_packet));
+
+    memcpy (&list->pkt, pkt, htons (pkt->hdr.size));
+    list->pkt.subtype = GNUNET_DNS_ANSWER_TYPE_IP;
 
     GNUNET_HashCode key;
 
     memset (&key, 0, sizeof (GNUNET_HashCode));
 
-    unsigned char *c = ((unsigned char *) pkt) + ntohs (pkt->addroffset);
+    unsigned char *c = ((unsigned char *) &list->pkt) + ntohs (pkt->addroffset);
 
-    new_ip4addr_remote (c, pkt->addr, pkt->addrsize);
+    new_ip4addr_remote (c, list->pkt.addr, pkt->addrsize);
     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "New mapping to %d.%d.%d.%d\n", c[0],
                 c[1], c[2], c[3]);
     unsigned char *k = (unsigned char *) &key;
@@ -761,83 +1277,18 @@ process_answer (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
     else
       GNUNET_free (value);
 
-    list =
-        GNUNET_malloc (htons (pkt->hdr.size) +
-                       sizeof(struct answer_packet_list) - sizeof(struct answer_packet));
-
-    memcpy (&list->pkt, pkt, htons (pkt->hdr.size));
   }
   else
   {
     GNUNET_break (0);
-    GNUNET_free (pkt);
     return;
   }
 
-  GNUNET_free (pkt);
-
   GNUNET_CONTAINER_DLL_insert_after (answer_proc_head, answer_proc_tail,
                                      answer_proc_tail, list);
 
-  schedule_helper_write (GNUNET_TIME_UNIT_FOREVER_REL, NULL);
-
-  return;
-}
-
-/**
- * Sets a bit active in a bitArray.
- *
- * @param bitArray memory area to set the bit in
- * @param bitIdx which bit to set
- */
-void
-setBit (char *bitArray, unsigned int bitIdx)
-{
-  size_t arraySlot;
-  unsigned int targetBit;
-
-  arraySlot = bitIdx / 8;
-  targetBit = (1L << (bitIdx % 8));
-  bitArray[arraySlot] |= targetBit;
 }
 
-/**
- * Clears a bit from bitArray.
- *
- * @param bitArray memory area to set the bit in
- * @param bitIdx which bit to unset
- */
-void
-clearBit (char *bitArray, unsigned int bitIdx)
-{
-  size_t slot;
-  unsigned int targetBit;
-
-  slot = bitIdx / 8;
-  targetBit = (1L << (bitIdx % 8));
-  bitArray[slot] = bitArray[slot] & (~targetBit);
-}
-
-/**
- * Checks if a bit is active in the bitArray
- *
- * @param bitArray memory area to set the bit in
- * @param bitIdx which bit to test
- * @return GNUNET_YES if the bit is set, GNUNET_NO if not.
- */
-int
-testBit (char *bitArray, unsigned int bitIdx)
-{
-  size_t slot;
-  unsigned int targetBit;
-
-  slot = bitIdx / 8;
-  targetBit = (1L << (bitIdx % 8));
-  if (bitArray[slot] & targetBit)
-    return GNUNET_YES;
-  else
-    return GNUNET_NO;
-}
 
 /**
  * @brief Add the port to the list of additional ports in the map_entry
@@ -852,12 +1303,10 @@ add_additional_port (struct map_entry *me, uint16_t port)
 }
 
 static int
-receive_udp_back (void *cls __attribute__ ((unused)),
-                  struct GNUNET_MESH_Tunnel *tunnel,
-                  void **tunnel_ctx,
-                  const struct GNUNET_PeerIdentity *sender,
+receive_udp_back (void *cls GNUNET_UNUSED, struct GNUNET_MESH_Tunnel *tunnel,
+                  void **tunnel_ctx, const struct GNUNET_PeerIdentity *sender,
                   const struct GNUNET_MessageHeader *message,
-                  const struct GNUNET_ATS_Information *atsi __attribute__ ((unused)))
+                  const struct GNUNET_ATS_Information *atsi GNUNET_UNUSED)
 {
   GNUNET_HashCode *desc = (GNUNET_HashCode *) (message + 1);
   struct remote_addr *s = (struct remote_addr *) desc;
@@ -876,9 +1325,9 @@ receive_udp_back (void *cls __attribute__ ((unused)),
     GNUNET_assert (pkt6 != NULL);
 
     if (ntohs (message->type) == GNUNET_MESSAGE_TYPE_VPN_SERVICE_UDP_BACK)
-      new_ip6addr (pkt6->ip6_hdr.sadr, &other->hashPubKey, desc);
+      new_ip6addr (&pkt6->ip6_hdr.sadr, &other->hashPubKey, desc);
     else
-      new_ip6addr_remote (pkt6->ip6_hdr.sadr, s->addr, s->addrlen);
+      new_ip6addr_remote (&pkt6->ip6_hdr.sadr, s->addr, s->addrlen);
 
     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                 "Relaying calc:%d gnu:%d udp:%d bytes!\n", size,
@@ -905,12 +1354,12 @@ receive_udp_back (void *cls __attribute__ ((unused)),
                      GNUNET_CONFIGURATION_get_value_string (cfg, "vpn",
                                                             "IPV6ADDR",
                                                             &ipv6addr));
-      inet_pton (AF_INET6, ipv6addr, pkt6->ip6_hdr.dadr);
+      inet_pton (AF_INET6, ipv6addr, &pkt6->ip6_hdr.dadr);
       GNUNET_free (ipv6addr);
     }
     memcpy (&pkt6->udp_hdr, pkt, ntohs (pkt->len));
 
-    GNUNET_HashCode *key = address6_mapping_exists (pkt6->ip6_hdr.sadr);
+    GNUNET_HashCode *key = address6_mapping_exists (&pkt6->ip6_hdr.sadr);
 
     GNUNET_assert (key != NULL);
 
@@ -937,21 +1386,24 @@ receive_udp_back (void *cls __attribute__ ((unused)),
     uint32_t sum = 0;
 
     sum =
-        calculate_checksum_update (sum, (uint16_t *) & pkt6->ip6_hdr.sadr, 16);
+        GNUNET_CRYPTO_crc16_step (sum, (uint16_t *) & pkt6->ip6_hdr.sadr, 16);
     sum =
-        calculate_checksum_update (sum, (uint16_t *) & pkt6->ip6_hdr.dadr, 16);
+        GNUNET_CRYPTO_crc16_step (sum, (uint16_t *) & pkt6->ip6_hdr.dadr, 16);
     uint32_t tmp = (pkt6->udp_hdr.len & 0xffff);
 
-    sum = calculate_checksum_update (sum, (uint16_t *) & tmp, 4);
+    sum = GNUNET_CRYPTO_crc16_step (sum, (uint16_t *) & tmp, 4);
     tmp = htons (((pkt6->ip6_hdr.nxthdr & 0x00ff)));
-    sum = calculate_checksum_update (sum, (uint16_t *) & tmp, 4);
+    sum = GNUNET_CRYPTO_crc16_step (sum, (uint16_t *) & tmp, 4);
 
     sum =
-        calculate_checksum_update (sum, (uint16_t *) & pkt6->udp_hdr,
+        GNUNET_CRYPTO_crc16_step (sum, (uint16_t *) & pkt6->udp_hdr,
                                    ntohs (pkt->len));
-    pkt6->udp_hdr.crc = calculate_checksum_end (sum);
-
-    write_to_helper (pkt6, size);
+    pkt6->udp_hdr.crc = GNUNET_CRYPTO_crc16_finish (sum);
+    
+    (void) GNUNET_HELPER_send (helper_handle,
+                              &pkt6->shdr,
+                              GNUNET_YES,
+                              NULL, NULL);
   }
   else
   {
@@ -967,7 +1419,7 @@ receive_udp_back (void *cls __attribute__ ((unused)),
     uint32_t sadr;
 
     new_ip4addr_remote ((unsigned char *) &sadr, s->addr, s->addrlen);
-    pkt4->ip_hdr.sadr = sadr;
+    pkt4->ip_hdr.sadr.s_addr = sadr;
 
     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                 "Relaying calc:%d gnu:%d udp:%d bytes!\n", size,
@@ -1000,11 +1452,11 @@ receive_udp_back (void *cls __attribute__ ((unused)),
                                                             &ipv4addr));
       inet_pton (AF_INET, ipv4addr, &dadr);
       GNUNET_free (ipv4addr);
-      pkt4->ip_hdr.dadr = dadr;
+      pkt4->ip_hdr.dadr.s_addr = dadr;
     }
     memcpy (&pkt4->udp_hdr, pkt, ntohs (pkt->len));
 
-    GNUNET_HashCode *key = address4_mapping_exists (pkt4->ip_hdr.sadr);
+    GNUNET_HashCode *key = address4_mapping_exists (pkt4->ip_hdr.sadr.s_addr);
 
     GNUNET_assert (key != NULL);
 
@@ -1020,21 +1472,23 @@ receive_udp_back (void *cls __attribute__ ((unused)),
     pkt4->udp_hdr.crc = 0;      /* Optional for IPv4 */
 
     pkt4->ip_hdr.chks =
-        calculate_ip_checksum ((uint16_t *) & pkt4->ip_hdr, 5 * 4);
+        GNUNET_CRYPTO_crc16_n ((uint16_t *) & pkt4->ip_hdr, 5 * 4);
 
-    write_to_helper (pkt4, size);
+    (void) GNUNET_HELPER_send (helper_handle,
+                              &pkt4->shdr,
+                              GNUNET_YES,
+                              NULL, NULL);
   }
 
   return GNUNET_OK;
 }
 
 static int
-receive_tcp_back (void *cls __attribute__ ((unused)),
-                  struct GNUNET_MESH_Tunnel *tunnel,
+receive_tcp_back (void *cls GNUNET_UNUSED, struct GNUNET_MESH_Tunnel *tunnel,
                   void **tunnel_ctx,
-                  const struct GNUNET_PeerIdentity *sender __attribute__ ((unused)),
+                  const struct GNUNET_PeerIdentity *sender GNUNET_UNUSED,
                   const struct GNUNET_MessageHeader *message,
-                  const struct GNUNET_ATS_Information *atsi __attribute__ ((unused)))
+                  const struct GNUNET_ATS_Information *atsi GNUNET_UNUSED)
 {
   GNUNET_HashCode *desc = (GNUNET_HashCode *) (message + 1);
   struct remote_addr *s = (struct remote_addr *) desc;
@@ -1046,9 +1500,10 @@ receive_tcp_back (void *cls __attribute__ ((unused)),
       ntohs (message->size) - sizeof (struct GNUNET_MessageHeader) -
       sizeof (GNUNET_HashCode);
 
-  GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Received TCP-Packet back, addrlen = %d\n", s->addrlen);
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+              "Received TCP-Packet back, addrlen = %d\n", s->addrlen);
 
-  if (ntohs(message->type) == GNUNET_MESSAGE_TYPE_VPN_SERVICE_TCP_BACK ||
+  if (ntohs (message->type) == GNUNET_MESSAGE_TYPE_VPN_SERVICE_TCP_BACK ||
       ts->addrlen == 16)
   {
     size_t size = pktlen + sizeof (struct ip6_tcp) - 1;
@@ -1060,9 +1515,9 @@ receive_tcp_back (void *cls __attribute__ ((unused)),
     GNUNET_assert (pkt6 != NULL);
 
     if (ntohs (message->type) == GNUNET_MESSAGE_TYPE_VPN_SERVICE_TCP_BACK)
-      new_ip6addr (pkt6->ip6_hdr.sadr, &other->hashPubKey, desc);
+      new_ip6addr (&pkt6->ip6_hdr.sadr, &other->hashPubKey, desc);
     else
-      new_ip6addr_remote (pkt6->ip6_hdr.sadr, s->addr, s->addrlen);
+      new_ip6addr_remote (&pkt6->ip6_hdr.sadr, s->addr, s->addrlen);
 
     pkt6->shdr.type = htons (GNUNET_MESSAGE_TYPE_VPN_HELPER);
     pkt6->shdr.size = htons (size);
@@ -1085,12 +1540,12 @@ receive_tcp_back (void *cls __attribute__ ((unused)),
                      GNUNET_CONFIGURATION_get_value_string (cfg, "vpn",
                                                             "IPV6ADDR",
                                                             &ipv6addr));
-      inet_pton (AF_INET6, ipv6addr, pkt6->ip6_hdr.dadr);
+      inet_pton (AF_INET6, ipv6addr, &pkt6->ip6_hdr.dadr);
       GNUNET_free (ipv6addr);
     }
     memcpy (&pkt6->tcp_hdr, pkt, pktlen);
 
-    GNUNET_HashCode *key = address6_mapping_exists (pkt6->ip6_hdr.sadr);
+    GNUNET_HashCode *key = address6_mapping_exists (&pkt6->ip6_hdr.sadr);
 
     GNUNET_assert (key != NULL);
 
@@ -1111,20 +1566,23 @@ receive_tcp_back (void *cls __attribute__ ((unused)),
     uint32_t tmp;
 
     sum =
-        calculate_checksum_update (sum, (uint16_t *) & pkt6->ip6_hdr.sadr, 16);
+        GNUNET_CRYPTO_crc16_step (sum, (uint16_t *) & pkt6->ip6_hdr.sadr, 16);
     sum =
-        calculate_checksum_update (sum, (uint16_t *) & pkt6->ip6_hdr.dadr, 16);
+        GNUNET_CRYPTO_crc16_step (sum, (uint16_t *) & pkt6->ip6_hdr.dadr, 16);
     tmp = htonl (pktlen);
-    sum = calculate_checksum_update (sum, (uint16_t *) & tmp, 4);
+    sum = GNUNET_CRYPTO_crc16_step (sum, (uint16_t *) & tmp, 4);
     tmp = htonl (((pkt6->ip6_hdr.nxthdr & 0x000000ff)));
-    sum = calculate_checksum_update (sum, (uint16_t *) & tmp, 4);
+    sum = GNUNET_CRYPTO_crc16_step (sum, (uint16_t *) & tmp, 4);
 
     sum =
-        calculate_checksum_update (sum, (uint16_t *) & pkt6->tcp_hdr,
+        GNUNET_CRYPTO_crc16_step (sum, (uint16_t *) & pkt6->tcp_hdr,
                                    ntohs (pkt6->ip6_hdr.paylgth));
-    pkt6->tcp_hdr.crc = calculate_checksum_end (sum);
+    pkt6->tcp_hdr.crc = GNUNET_CRYPTO_crc16_finish (sum);
 
-    write_to_helper (pkt6, size);
+    (void) GNUNET_HELPER_send (helper_handle,
+                              &pkt6->shdr,
+                              GNUNET_YES,
+                              NULL, NULL);
   }
   else
   {
@@ -1140,7 +1598,7 @@ receive_tcp_back (void *cls __attribute__ ((unused)),
     uint32_t sadr;
 
     new_ip4addr_remote ((unsigned char *) &sadr, s->addr, s->addrlen);
-    pkt4->ip_hdr.sadr = sadr;
+    pkt4->ip_hdr.sadr.s_addr = sadr;
 
     pkt4->shdr.type = htons (GNUNET_MESSAGE_TYPE_VPN_HELPER);
     pkt4->shdr.size = htons (size);
@@ -1169,12 +1627,12 @@ receive_tcp_back (void *cls __attribute__ ((unused)),
                                                             &ipv4addr));
       inet_pton (AF_INET, ipv4addr, &dadr);
       GNUNET_free (ipv4addr);
-      pkt4->ip_hdr.dadr = dadr;
+      pkt4->ip_hdr.dadr.s_addr = dadr;
     }
 
     memcpy (&pkt4->tcp_hdr, pkt, pktlen);
 
-    GNUNET_HashCode *key = address4_mapping_exists (pkt4->ip_hdr.sadr);
+    GNUNET_HashCode *key = address4_mapping_exists (pkt4->ip_hdr.sadr.s_addr);
 
     GNUNET_assert (key != NULL);
 
@@ -1190,45 +1648,46 @@ receive_tcp_back (void *cls __attribute__ ((unused)),
     uint32_t sum = 0;
     uint32_t tmp;
 
-    tmp = pkt4->ip_hdr.sadr;
-    sum = calculate_checksum_update (sum, (uint16_t *) & tmp, 4);
-    tmp = pkt4->ip_hdr.dadr;
-    sum = calculate_checksum_update (sum, (uint16_t *) & tmp, 4);
+    sum = GNUNET_CRYPTO_crc16_step (sum, (uint16_t *) &pkt4->ip_hdr.sadr, 4);
+    sum = GNUNET_CRYPTO_crc16_step (sum, (uint16_t *) &pkt4->ip_hdr.dadr, 4);
 
-    tmp = (0x06 << 16) | (0xffff & pktlen); // 0x06 for TCP?
+    tmp = (0x06 << 16) | (0xffff & pktlen);     // 0x06 for TCP?
 
     tmp = htonl (tmp);
 
-    sum = calculate_checksum_update (sum, (uint16_t *) & tmp, 4);
+    sum = GNUNET_CRYPTO_crc16_step (sum, (uint16_t *) & tmp, 4);
 
-    sum = calculate_checksum_update (sum, (uint16_t *) & pkt4->tcp_hdr, pktlen);
-    pkt4->tcp_hdr.crc = calculate_checksum_end (sum);
+    sum = GNUNET_CRYPTO_crc16_step (sum, (uint16_t *) & pkt4->tcp_hdr, pktlen);
+    pkt4->tcp_hdr.crc = GNUNET_CRYPTO_crc16_finish (sum);
 
     pkt4->ip_hdr.chks =
-        calculate_ip_checksum ((uint16_t *) & pkt4->ip_hdr, 5 * 4);
+        GNUNET_CRYPTO_crc16_n ((uint16_t *) & pkt4->ip_hdr, 5 * 4);
+
+    (void) GNUNET_HELPER_send (helper_handle,
+                              &pkt4->shdr,
+                              GNUNET_YES,
+                              NULL, NULL);
 
-    write_to_helper (pkt4, size);
   }
 
   return GNUNET_OK;
 }
 
-static void* new_tunnel(void* cls,
-                        struct GNUNET_MESH_Tunnel *tunnel,
-                        const struct GNUNET_PeerIdentity *initiator,
-                        const struct GNUNET_ATS_Information *atsi)
+static void *
+new_tunnel (void *cls, struct GNUNET_MESH_Tunnel *tunnel,
+            const struct GNUNET_PeerIdentity *initiator,
+            const struct GNUNET_ATS_Information *atsi)
 {
   /* Why should anyone open an inbound tunnel to vpn? */
-  GNUNET_break(0);
+  GNUNET_break (0);
   return NULL;
 }
 
-static void cleaner(void *cls,
-                    const struct GNUNET_MESH_Tunnel *tunnel,
-                    void *tunnel_ctx)
+static void
+cleaner (void *cls, const struct GNUNET_MESH_Tunnel *tunnel, void *tunnel_ctx)
 {
   /* Why should anyone open an inbound tunnel to vpn? */
-  GNUNET_break(0);
+  GNUNET_break (0);
 }
 
 /**
@@ -1240,8 +1699,9 @@ static void cleaner(void *cls,
  * @param cfg_ configuration
  */
 static void
-run (void *cls, char *const *args __attribute__ ((unused)), const char *cfgfilep
-     __attribute__ ((unused)), const struct GNUNET_CONFIGURATION_Handle *cfg_)
+run (void *cls, char *const *args GNUNET_UNUSED,
+     const char *cfgfile GNUNET_UNUSED,
+     const struct GNUNET_CONFIGURATION_Handle *cfg_)
 {
   static const struct GNUNET_MESH_MessageHandler handlers[] = {
     {receive_udp_back, GNUNET_MESSAGE_TYPE_VPN_SERVICE_UDP_BACK, 0},
@@ -1250,22 +1710,79 @@ run (void *cls, char *const *args __attribute__ ((unused)), const char *cfgfilep
     {receive_tcp_back, GNUNET_MESSAGE_TYPE_VPN_REMOTE_TCP_BACK, 0},
     {NULL, 0, 0}
   };
-
   static const GNUNET_MESH_ApplicationType types[] = {
     GNUNET_APPLICATION_TYPE_END
   };
+  char *ifname;
+  char *ipv6addr;
+  char *ipv6prefix;
+  char *ipv4addr;
+  char *ipv4mask;
 
-  mesh_handle = GNUNET_MESH_connect (cfg_, 42, NULL, new_tunnel, cleaner, handlers, types);
+  mesh_handle =
+      GNUNET_MESH_connect (cfg_, 42, NULL, new_tunnel, cleaner, handlers,
+                           types);
   cfg = cfg_;
-  restart_hijack = 0;
   hashmap = GNUNET_CONTAINER_multihashmap_create (65536);
   heap = GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
   GNUNET_CONFIGURATION_get_value_number (cfg, "vpn", "MAX_MAPPINGg",
                                          &max_mappings);
   udp_connections = GNUNET_CONTAINER_multihashmap_create (65536);
-  conn_task = GNUNET_SCHEDULER_add_now (connect_to_service_dns, NULL);
-  shs_task =
-      GNUNET_SCHEDULER_add_after (conn_task, start_helper_and_schedule, NULL);
+  dns_handle = GNUNET_DNS_connect (cfg,
+                                  &process_answer,
+                                  NULL);
+  if (GNUNET_SYSERR ==
+      GNUNET_CONFIGURATION_get_value_string (cfg, "vpn", "IFNAME", &ifname))
+  {
+    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+                "No entry 'IFNAME' in configuration!\n");
+    exit (1);
+  }
+
+  if (GNUNET_SYSERR ==
+      GNUNET_CONFIGURATION_get_value_string (cfg, "vpn", "IPV6ADDR", &ipv6addr))
+  {
+    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+                "No entry 'IPV6ADDR' in configuration!\n");
+    exit (1);
+  }
+
+  if (GNUNET_SYSERR ==
+      GNUNET_CONFIGURATION_get_value_string (cfg, "vpn", "IPV6PREFIX",
+                                             &ipv6prefix))
+  {
+    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+                "No entry 'IPV6PREFIX' in configuration!\n");
+    exit (1);
+  }
+
+  if (GNUNET_SYSERR ==
+      GNUNET_CONFIGURATION_get_value_string (cfg, "vpn", "IPV4ADDR", &ipv4addr))
+  {
+    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+                "No entry 'IPV4ADDR' in configuration!\n");
+    exit (1);
+  }
+
+  if (GNUNET_SYSERR ==
+      GNUNET_CONFIGURATION_get_value_string (cfg, "vpn", "IPV4MASK", &ipv4mask))
+  {
+    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+                "No entry 'IPV4MASK' in configuration!\n");
+    exit (1);
+  }
+
+  vpn_argv[0] = GNUNET_strdup ("vpn-gnunet");
+  vpn_argv[1] = ifname;
+  vpn_argv[2] = ipv6addr;
+  vpn_argv[3] = ipv6prefix;
+  vpn_argv[4] = ipv4addr;
+  vpn_argv[5] = ipv4mask;
+  vpn_argv[6] = NULL;
+  
+  helper_handle = GNUNET_HELPER_start ("gnunet-helper-vpn", vpn_argv,
+                                      &message_token, NULL);
+  GNUNET_DNS_restart_hijack (dns_handle);
   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &cleanup, cls);
 }