-remove trailing whitespace
[oweals/gnunet.git] / src / exit / gnunet-daemon-exit.c
index e58611cc62b37fd68fc1981b2b9afa3f90281722..f4d656f3ce0da23032e41d8d8a76403faf40496d 100644 (file)
@@ -1,6 +1,6 @@
 /*
      This file is part of GNUnet.
-     (C) 2010, 2012 Christian Grothoff
+     (C) 2010-2013 Christian Grothoff
 
      GNUnet is free software; you can redistribute it and/or modify
      it under the terms of the GNU General Public License as published
  * - which code should advertise services? the service model is right
  *   now a bit odd, especially as this code DOES the exit and knows
  *   the DNS "name", but OTOH this is clearly NOT the place to advertise
- *   the service's existence; maybe the daemon should turn into a 
+ *   the service's existence; maybe the daemon should turn into a
  *   service with an API to add local-exit services dynamically?
  */
 #include "platform.h"
 #include "gnunet_util_lib.h"
 #include "gnunet_protocols.h"
 #include "gnunet_applications.h"
+#include "gnunet_dht_service.h"
 #include "gnunet_mesh_service.h"
+#include "gnunet_dnsparser_lib.h"
+#include "gnunet_dnsstub_lib.h"
 #include "gnunet_statistics_service.h"
 #include "gnunet_constants.h"
+#include "gnunet_signatures.h"
 #include "gnunet_tun_lib.h"
+#include "gnunet_regex_service.h"
 #include "exit.h"
+#include "block_dns.h"
+
+
+/**
+ * Maximum path compression length for mesh regex announcing for IPv4 address
+ * based regex.
+ */
+#define REGEX_MAX_PATH_LEN_IPV4 4
+
+/**
+ * Maximum path compression length for mesh regex announcing for IPv6 address
+ * based regex.
+ */
+#define REGEX_MAX_PATH_LEN_IPV6 8
+
+/**
+ * How frequently do we re-announce the regex for the exit?
+ */
+#define REGEX_REFRESH_FREQUENCY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 30)
+
+/**
+ * How frequently do we re-announce the DNS exit in the DHT?
+ */
+#define DHT_PUT_FREQUENCY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 15)
+
+/**
+ * How long do we typically sign the DNS exit advertisement for?
+ */
+#define DNS_ADVERTISEMENT_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_HOURS, 3)
+
+
+/**
+ * Generic logging shorthand
+ */
+#define LOG(kind, ...)                          \
+  GNUNET_log_from (kind, "exit", __VA_ARGS__);
+
 
 /**
  * Information about an address.
@@ -69,7 +111,7 @@ struct SocketAddress
      */
     struct in6_addr ipv6;
   } address;
-  
+
   /**
    * IPPROTO_TCP or IPPROTO_UDP;
    */
@@ -113,7 +155,7 @@ struct LocalService
  * IP-version, protocol, source-IP, destination-IP, source-port and
  * destinatin-port.
  */
-struct RedirectInformation 
+struct RedirectInformation
 {
 
   /**
@@ -129,10 +171,10 @@ struct RedirectInformation
    */
   struct SocketAddress local_address;
 
-  /* 
+  /*
      Note 1: additional information might be added here in the
      future to support protocols that require special handling,
-     such as ftp/tftp 
+     such as ftp/tftp
 
      Note 2: we might also sometimes not match on all components
      of the tuple, to support protocols where things do not always
@@ -182,39 +224,91 @@ struct TunnelState
   struct GNUNET_MESH_Tunnel *tunnel;
 
   /**
-   * Heap node for this state in the connections_heap.
+   * Who is the other end of this tunnel.
+   * FIXME is this needed? Only used for debugging messages
    */
-  struct GNUNET_CONTAINER_HeapNode *heap_node;
+  struct GNUNET_PeerIdentity peer;
 
   /**
-   * Key this state has in the connections_map.
+   * Active tunnel transmission request (or NULL).
    */
-  GNUNET_HashCode state_key;
+  struct GNUNET_MESH_TransmitHandle *th;
 
   /**
-   * Associated service record, or NULL for no service.
+   * #GNUNET_NO if this is a tunnel for TCP/UDP,
+   * #GNUNET_YES if this is a tunnel for DNS,
+   * #GNUNET_SYSERR if the tunnel is not yet initialized.
    */
-  struct LocalService *serv;
+  int is_dns;
 
-  /**
-   * Head of DLL of messages for this tunnel.
-   */
-  struct TunnelMessageQueue *head;
+  union
+  {
+    struct
+    {
 
-  /**
-   * Tail of DLL of messages for this tunnel.
-   */
-  struct TunnelMessageQueue *tail;
+      /**
+       * Heap node for this state in the connections_heap.
+       */
+      struct GNUNET_CONTAINER_HeapNode *heap_node;
+
+      /**
+       * Key this state has in the connections_map.
+       */
+      struct GNUNET_HashCode state_key;
+
+      /**
+       * Associated service record, or NULL for no service.
+       */
+      struct LocalService *serv;
+
+      /**
+       * Head of DLL of messages for this tunnel.
+       */
+      struct TunnelMessageQueue *head;
+
+      /**
+       * Tail of DLL of messages for this tunnel.
+       */
+      struct TunnelMessageQueue *tail;
+
+      /**
+       * Primary redirection information for this connection.
+       */
+      struct RedirectInformation ri;
+    } tcp_udp;
+
+    struct
+    {
 
-  /**
-   * Active tunnel transmission request (or NULL).
-   */
-  struct GNUNET_MESH_TransmitHandle *th;
+      /**
+       * DNS reply ready for transmission.
+       */
+      char *reply;
 
-  /**
-   * Primary redirection information for this connection.
-   */
-  struct RedirectInformation ri;
+      /**
+       * Socket we are using to transmit this request (must match if we receive
+       * a response).
+       */
+      struct GNUNET_DNSSTUB_RequestSocket *rs;
+
+      /**
+       * Number of bytes in 'reply'.
+       */
+      size_t reply_length;
+
+      /**
+       * Original DNS request ID as used by the client.
+       */
+      uint16_t original_id;
+
+      /**
+       * DNS request ID that we used for forwarding.
+       */
+      uint16_t my_id;
+
+    } dns;
+
+  } specifics;
 
 };
 
@@ -224,6 +318,16 @@ struct TunnelState
  */
 static int global_ret;
 
+/**
+ * Handle to our regex announcement for IPv4.
+ */
+static struct GNUNET_REGEX_Announcement *regex4;
+
+/**
+ * Handle to our regex announcement for IPv4.
+ */
+static struct GNUNET_REGEX_Announcement *regex6;
+
 /**
  * The handle to the configuration used throughout the process
  */
@@ -259,7 +363,6 @@ static struct in_addr exit_ipv4addr;
  */
 static struct in_addr exit_ipv4mask;
 
-
 /**
  * Statistics.
  */
@@ -296,6 +399,47 @@ static struct GNUNET_CONTAINER_MultiHashMap *udp_services;
  */
 static struct GNUNET_CONTAINER_MultiHashMap *tcp_services;
 
+/**
+ * Array of all open DNS requests from tunnels.
+ */
+static struct TunnelState *tunnels[UINT16_MAX + 1];
+
+/**
+ * Handle to the DNS Stub resolver.
+ */
+static struct GNUNET_DNSSTUB_Context *dnsstub;
+
+/**
+ * Handle for ongoing DHT PUT operations to advertise exit service.
+ */
+static struct GNUNET_DHT_PutHandle *dht_put;
+
+/**
+ * Handle to the DHT.
+ */
+static struct GNUNET_DHT_Handle *dht;
+
+/**
+ * Task for doing DHT PUTs to advertise exit service.
+ */
+static GNUNET_SCHEDULER_TaskIdentifier dht_task;
+
+/**
+ * Advertisement message we put into the DHT to advertise us
+ * as a DNS exit.
+ */
+static struct GNUNET_DNS_Advertisement dns_advertisement;
+
+/**
+ * Key we store the DNS advertismenet under.
+ */
+static struct GNUNET_HashCode dht_put_key;
+
+/**
+ * Private key for this peer.
+ */
+static struct GNUNET_CRYPTO_EccPrivateKey *peer_key;
+
 /**
  * Are we an IPv4-exit?
  */
@@ -317,6 +461,156 @@ static int ipv4_enabled;
 static int ipv6_enabled;
 
 
+/**
+ * We got a reply from DNS for a request of a MESH tunnel.  Send it
+ * via the tunnel (after changing the request ID back).
+ *
+ * @param cls the 'struct TunnelState'
+ * @param size number of bytes available in buf
+ * @param buf where to copy the reply
+ * @return number of bytes written to buf
+ */
+static size_t
+transmit_reply_to_mesh (void *cls,
+                       size_t size,
+                       void *buf)
+{
+  struct TunnelState *ts = cls;
+  size_t off;
+  size_t ret;
+  char *cbuf = buf;
+  struct GNUNET_MessageHeader hdr;
+  struct GNUNET_TUN_DnsHeader dns;
+
+  GNUNET_assert (GNUNET_YES == ts->is_dns);
+  ts->th = NULL;
+  GNUNET_assert (ts->specifics.dns.reply != NULL);
+  if (size == 0)
+    return 0;
+  ret = sizeof (struct GNUNET_MessageHeader) + ts->specifics.dns.reply_length;
+  GNUNET_assert (ret <= size);
+  hdr.size = htons (ret);
+  hdr.type = htons (GNUNET_MESSAGE_TYPE_VPN_DNS_FROM_INTERNET);
+  memcpy (&dns, ts->specifics.dns.reply, sizeof (dns));
+  dns.id = ts->specifics.dns.original_id;
+  off = 0;
+  memcpy (&cbuf[off], &hdr, sizeof (hdr));
+  off += sizeof (hdr);
+  memcpy (&cbuf[off], &dns, sizeof (dns));
+  off += sizeof (dns);
+  memcpy (&cbuf[off], &ts->specifics.dns.reply[sizeof (dns)], ts->specifics.dns.reply_length - sizeof (dns));
+  off += ts->specifics.dns.reply_length - sizeof (dns);
+  GNUNET_free (ts->specifics.dns.reply);
+  ts->specifics.dns.reply = NULL;
+  ts->specifics.dns.reply_length = 0;
+  GNUNET_assert (ret == off);
+  return ret;
+}
+
+
+/**
+ * Callback called from DNSSTUB resolver when a resolution
+ * succeeded.
+ *
+ * @param cls NULL
+ * @param rs the socket that received the response
+ * @param dns the response itself
+ * @param r number of bytes in dns
+ */
+static void
+process_dns_result (void *cls,
+                   struct GNUNET_DNSSTUB_RequestSocket *rs,
+                   const struct GNUNET_TUN_DnsHeader *dns,
+                   size_t r)
+{
+  struct TunnelState *ts;
+
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+             "Processing DNS result from stub resolver\n");
+  GNUNET_assert (NULL == cls);
+  /* Handle case that this is a reply to a request from a MESH DNS tunnel */
+  ts = tunnels[dns->id];
+  if ( (NULL == ts) ||
+       (ts->specifics.dns.rs != rs) )
+    return;
+  LOG (GNUNET_ERROR_TYPE_DEBUG,
+       "Got a response from the stub resolver for DNS request received via MESH!\n");
+  tunnels[dns->id] = NULL;
+  GNUNET_free_non_null (ts->specifics.dns.reply);
+  ts->specifics.dns.reply = GNUNET_malloc (r);
+  ts->specifics.dns.reply_length = r;
+  memcpy (ts->specifics.dns.reply, dns, r);
+  if (NULL != ts->th)
+    GNUNET_MESH_notify_transmit_ready_cancel (ts->th);
+  ts->th = GNUNET_MESH_notify_transmit_ready (ts->tunnel,
+                                             GNUNET_NO,
+                                             GNUNET_TIME_UNIT_FOREVER_REL,
+                                             sizeof (struct GNUNET_MessageHeader) + r,
+                                             &transmit_reply_to_mesh,
+                                             ts);
+}
+
+
+/**
+ * Process a request via mesh to perform a DNS query.
+ *
+ * @param cls closure, NULL
+ * @param tunnel connection to the other end
+ * @param tunnel_ctx pointer to our `struct TunnelState *`
+ * @param message the actual message
+ *
+ * @return #GNUNET_OK to keep the connection open,
+ *         #GNUNET_SYSERR to close it (signal serious error)
+ */
+static int
+receive_dns_request (void *cls GNUNET_UNUSED, struct GNUNET_MESH_Tunnel *tunnel,
+                     void **tunnel_ctx,
+                     const struct GNUNET_MessageHeader *message)
+{
+  struct TunnelState *ts = *tunnel_ctx;
+  const struct GNUNET_TUN_DnsHeader *dns;
+  size_t mlen = ntohs (message->size);
+  size_t dlen = mlen - sizeof (struct GNUNET_MessageHeader);
+  char buf[dlen] GNUNET_ALIGN;
+  struct GNUNET_TUN_DnsHeader *dout;
+
+  if (NULL == dnsstub)
+    return GNUNET_SYSERR;
+  if (GNUNET_NO == ts->is_dns)
+  {
+    GNUNET_break_op (0);
+    return GNUNET_SYSERR;
+  }
+  if (GNUNET_SYSERR == ts->is_dns)
+  {
+    /* tunnel is DNS from now on */
+    ts->is_dns = GNUNET_YES;
+  }
+  if (dlen < sizeof (struct GNUNET_TUN_DnsHeader))
+  {
+    GNUNET_break_op (0);
+    return GNUNET_SYSERR;
+  }
+  dns = (const struct GNUNET_TUN_DnsHeader *) &message[1];
+  ts->specifics.dns.original_id = dns->id;
+  if (tunnels[ts->specifics.dns.my_id] == ts)
+    tunnels[ts->specifics.dns.my_id] = NULL;
+  ts->specifics.dns.my_id = (uint16_t) GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
+                                                  UINT16_MAX + 1);
+  tunnels[ts->specifics.dns.my_id] = ts;
+  memcpy (buf, dns, dlen);
+  dout = (struct GNUNET_TUN_DnsHeader *) buf;
+  dout->id = ts->specifics.dns.my_id;
+  ts->specifics.dns.rs = GNUNET_DNSSTUB_resolve2 (dnsstub,
+                                                 buf, dlen,
+                                                 &process_dns_result,
+                                                 NULL);
+  if (NULL == ts->specifics.dns.rs)
+    return GNUNET_SYSERR;
+  return GNUNET_OK;
+}
+
+
 /**
  * Given IP information about a connection, calculate the respective
  * hash we would use for the 'connections_map'.
@@ -325,12 +619,12 @@ static int ipv6_enabled;
  * @param ri information about the connection
  */
 static void
-hash_redirect_info (GNUNET_HashCode *hash, 
+hash_redirect_info (struct GNUNET_HashCode *hash,
                    const struct RedirectInformation *ri)
 {
   char *off;
 
-  memset (hash, 0, sizeof (GNUNET_HashCode));
+  memset (hash, 0, sizeof (struct GNUNET_HashCode));
   /* the GNUnet hashmap only uses the first sizeof(unsigned int) of the hash,
      so we put the IP address in there (and hope for few collisions) */
   off = (char*) hash;
@@ -365,7 +659,7 @@ hash_redirect_info (GNUNET_HashCode *hash,
   memcpy (off, &ri->local_address.port, sizeof (uint16_t));
   off += sizeof (uint16_t);
   memcpy (off, &ri->remote_address.proto, sizeof (uint8_t));
-  off += sizeof (uint8_t);
+  /* off += sizeof (uint8_t); */
 }
 
 
@@ -384,15 +678,15 @@ hash_redirect_info (GNUNET_HashCode *hash,
  */
 static struct TunnelState *
 get_redirect_state (int af,
-                   int protocol,                   
+                   int protocol,               
                    const void *destination_ip,
                    uint16_t destination_port,
                    const void *local_ip,
                    uint16_t local_port,
-                   GNUNET_HashCode *state_key)
+                   struct GNUNET_HashCode *state_key)
 {
   struct RedirectInformation ri;
-  GNUNET_HashCode key;
+  struct GNUNET_HashCode key;
   struct TunnelState *state;
 
   if ( ( (af == AF_INET) && (protocol == IPPROTO_ICMP) ) ||
@@ -424,9 +718,9 @@ get_redirect_state (int af,
     return NULL;
   /* Mark this connection as freshly used */
   if (NULL == state_key)
-    GNUNET_CONTAINER_heap_update_cost (connections_heap, 
-                                      state->heap_node,
-                                      GNUNET_TIME_absolute_get ().abs_value);
+    GNUNET_CONTAINER_heap_update_cost (connections_heap,
+                                      state->specifics.tcp_udp.heap_node,
+                                      GNUNET_TIME_absolute_get ().abs_value_us);
   return state;
 }
 
@@ -442,15 +736,15 @@ get_redirect_state (int af,
  */
 static struct LocalService *
 find_service (struct GNUNET_CONTAINER_MultiHashMap *service_map,
-             const GNUNET_HashCode *desc,
+             const struct GNUNET_HashCode *desc,
              uint16_t destination_port)
 {
-  char key[sizeof (GNUNET_HashCode) + sizeof (uint16_t)];
+  char key[sizeof (struct GNUNET_HashCode) + sizeof (uint16_t)];
 
   memcpy (&key[0], &destination_port, sizeof (uint16_t));
-  memcpy (&key[sizeof(uint16_t)], desc, sizeof (GNUNET_HashCode));
+  memcpy (&key[sizeof(uint16_t)], desc, sizeof (struct GNUNET_HashCode));
   return GNUNET_CONTAINER_multihashmap_get (service_map,
-                                           (GNUNET_HashCode *) key);
+                                           (struct GNUNET_HashCode *) key);
 }
 
 
@@ -464,7 +758,7 @@ find_service (struct GNUNET_CONTAINER_MultiHashMap *service_map,
  */
 static int
 free_service_record (void *cls,
-                    const GNUNET_HashCode *key,
+                    const struct GNUNET_HashCode *key,
                     void *value)
 {
   struct LocalService *service = value;
@@ -480,7 +774,7 @@ free_service_record (void *cls,
  * respective service entry.
  *
  * @param service_map map of services (TCP or UDP)
- * @param name name of the service 
+ * @param name name of the service
  * @param destination_port destination port
  * @param service service information record to store (service->name will be set).
  */
@@ -490,20 +784,20 @@ store_service (struct GNUNET_CONTAINER_MultiHashMap *service_map,
               uint16_t destination_port,
               struct LocalService *service)
 {
-  char key[sizeof (GNUNET_HashCode) + sizeof (uint16_t)];
-  GNUNET_HashCode desc;
+  char key[sizeof (struct GNUNET_HashCode) + sizeof (uint16_t)];
+  struct GNUNET_HashCode desc;
 
   GNUNET_CRYPTO_hash (name, strlen (name) + 1, &desc);
   service->name = GNUNET_strdup (name);
   memcpy (&key[0], &destination_port, sizeof (uint16_t));
-  memcpy (&key[sizeof(uint16_t)], &desc, sizeof (GNUNET_HashCode));
+  memcpy (&key[sizeof(uint16_t)], &desc, sizeof (struct GNUNET_HashCode));
   if (GNUNET_OK !=
       GNUNET_CONTAINER_multihashmap_put (service_map,
-                                        (GNUNET_HashCode *) key,
+                                        (struct GNUNET_HashCode *) key,
                                         service,
                                         GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
   {
-    free_service_record (NULL, (GNUNET_HashCode *) key, service);
+    free_service_record (NULL, (struct GNUNET_HashCode *) key, service);
     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
                _("Got duplicate service records for `%s:%u'\n"),
                name,
@@ -528,16 +822,14 @@ send_to_peer_notify_callback (void *cls, size_t size, void *buf)
   struct TunnelMessageQueue *tnq;
 
   s->th = NULL;
-  tnq = s->head;
+  tnq = s->specifics.tcp_udp.head;
   if (NULL == tnq)
     return 0;
   if (0 == size)
   {
-    s->th = GNUNET_MESH_notify_transmit_ready (tunnel, 
-                                              GNUNET_NO /* corking */, 
-                                              0 /* priority */,
+    s->th = GNUNET_MESH_notify_transmit_ready (tunnel,
+                                              GNUNET_NO /* corking */,
                                               GNUNET_TIME_UNIT_FOREVER_REL,
-                                              NULL,
                                               tnq->len,
                                               &send_to_peer_notify_callback,
                                               s);
@@ -546,16 +838,14 @@ send_to_peer_notify_callback (void *cls, size_t size, void *buf)
   GNUNET_assert (size >= tnq->len);
   memcpy (buf, tnq->payload, tnq->len);
   size = tnq->len;
-  GNUNET_CONTAINER_DLL_remove (s->head, 
-                              s->tail,
-                              tnq);  
+  GNUNET_CONTAINER_DLL_remove (s->specifics.tcp_udp.head,
+                              s->specifics.tcp_udp.tail,
+                              tnq);
   GNUNET_free (tnq);
-  if (NULL != (tnq = s->head))
-    s->th = GNUNET_MESH_notify_transmit_ready (tunnel, 
-                                              GNUNET_NO /* corking */, 
-                                              0 /* priority */,
+  if (NULL != (tnq = s->specifics.tcp_udp.head))
+    s->th = GNUNET_MESH_notify_transmit_ready (tunnel,
+                                              GNUNET_NO /* corking */,
                                               GNUNET_TIME_UNIT_FOREVER_REL,
-                                              NULL,
                                               tnq->len,
                                               &send_to_peer_notify_callback,
                                               s);
@@ -569,22 +859,23 @@ send_to_peer_notify_callback (void *cls, size_t size, void *buf)
 /**
  * Send the given packet via the mesh tunnel.
  *
- * @param mesh_tunnel destination
+ * @param tunnel destination
  * @param tnq message to queue
  */
 static void
-send_packet_to_mesh_tunnel (struct GNUNET_MESH_Tunnel *mesh_tunnel,
+send_packet_to_mesh_tunnel (struct TunnelState *s,
                            struct TunnelMessageQueue *tnq)
 {
-  struct TunnelState *s;
+  struct GNUNET_MESH_Tunnel *mesh_tunnel;
 
-  s = GNUNET_MESH_tunnel_get_data (mesh_tunnel);
+  mesh_tunnel = s->tunnel;
   GNUNET_assert (NULL != s);
-  GNUNET_CONTAINER_DLL_insert_tail (s->head, s->tail, tnq);
+  GNUNET_CONTAINER_DLL_insert_tail (s->specifics.tcp_udp.head, s->specifics.tcp_udp.tail, tnq);
   if (NULL == s->th)
-    s->th = GNUNET_MESH_notify_transmit_ready (mesh_tunnel, GNUNET_NO /* cork */, 0 /* priority */,
+    s->th = GNUNET_MESH_notify_transmit_ready (mesh_tunnel,
+                                               GNUNET_NO /* cork */,
                                               GNUNET_TIME_UNIT_FOREVER_REL,
-                                              NULL, tnq->len,
+                                              tnq->len,
                                               &send_to_peer_notify_callback,
                                               s);
 }
@@ -596,16 +887,16 @@ send_packet_to_mesh_tunnel (struct GNUNET_MESH_Tunnel *mesh_tunnel,
  * @param icmp A pointer to the Packet
  * @param pktlen number of bytes in 'icmp'
  * @param af address family (AFINET or AF_INET6)
- * @param destination_ip destination IP-address of the IP packet (should 
+ * @param destination_ip destination IP-address of the IP packet (should
  *                       be our local address)
  * @param source_ip original source IP-address of the IP packet (should
  *                       be the original destination address)
  */
 static void
-icmp_from_helper (const struct GNUNET_TUN_IcmpHeader *icmp, 
+icmp_from_helper (const struct GNUNET_TUN_IcmpHeader *icmp,
                  size_t pktlen,
                  int af,
-                 const void *destination_ip, 
+                 const void *destination_ip,
                  const void *source_ip)
 {
   struct TunnelState *state;
@@ -629,7 +920,7 @@ icmp_from_helper (const struct GNUNET_TUN_IcmpHeader *icmp,
                           sbuf, sizeof (sbuf)),
                inet_ntop (af,
                           destination_ip,
-                          dbuf, sizeof (dbuf)));    
+                          dbuf, sizeof (dbuf)));
   }
   if (pktlen < sizeof (struct GNUNET_TUN_IcmpHeader))
   {
@@ -655,7 +946,7 @@ icmp_from_helper (const struct GNUNET_TUN_IcmpHeader *icmp,
       case GNUNET_TUN_ICMPTYPE_DESTINATION_UNREACHABLE:
       case GNUNET_TUN_ICMPTYPE_SOURCE_QUENCH:
       case GNUNET_TUN_ICMPTYPE_TIME_EXCEEDED:
-       if (pktlen < 
+       if (pktlen <
            sizeof (struct GNUNET_TUN_IcmpHeader) +
            sizeof (struct GNUNET_TUN_IPv4Header) + 8)
        {
@@ -689,7 +980,7 @@ icmp_from_helper (const struct GNUNET_TUN_IcmpHeader *icmp,
       case GNUNET_TUN_ICMPTYPE6_PACKET_TOO_BIG:
       case GNUNET_TUN_ICMPTYPE6_TIME_EXCEEDED:
       case GNUNET_TUN_ICMPTYPE6_PARAMETER_PROBLEM:
-       if (pktlen < 
+       if (pktlen <
            sizeof (struct GNUNET_TUN_IcmpHeader) +
            sizeof (struct GNUNET_TUN_IPv6Header) + 8)
        {
@@ -764,7 +1055,7 @@ icmp_from_helper (const struct GNUNET_TUN_IcmpHeader *icmp,
     return;
   }
   mlen = sizeof (struct GNUNET_EXIT_IcmpToVPNMessage) + pktlen - sizeof (struct GNUNET_TUN_IcmpHeader);
-  tnq = GNUNET_malloc (sizeof (struct TunnelMessageQueue) + mlen);  
+  tnq = GNUNET_malloc (sizeof (struct TunnelMessageQueue) + mlen);
   tnq->payload = &tnq[1];
   tnq->len = mlen;
   i2v = (struct GNUNET_EXIT_IcmpToVPNMessage *) &tnq[1];
@@ -774,8 +1065,7 @@ icmp_from_helper (const struct GNUNET_TUN_IcmpHeader *icmp,
   memcpy (&i2v->icmp_header,
          icmp,
          pktlen);
-  send_packet_to_mesh_tunnel (state->tunnel,
-                             tnq);
+  send_packet_to_mesh_tunnel (state, tnq);
 }
 
 
@@ -785,16 +1075,16 @@ icmp_from_helper (const struct GNUNET_TUN_IcmpHeader *icmp,
  * @param udp A pointer to the Packet
  * @param pktlen number of bytes in 'udp'
  * @param af address family (AFINET or AF_INET6)
- * @param destination_ip destination IP-address of the IP packet (should 
+ * @param destination_ip destination IP-address of the IP packet (should
  *                       be our local address)
  * @param source_ip original source IP-address of the IP packet (should
  *                       be the original destination address)
  */
 static void
-udp_from_helper (const struct GNUNET_TUN_UdpHeader *udp, 
+udp_from_helper (const struct GNUNET_TUN_UdpHeader *udp,
                 size_t pktlen,
                 int af,
-                const void *destination_ip, 
+                const void *destination_ip,
                 const void *source_ip)
 {
   struct TunnelState *state;
@@ -841,7 +1131,7 @@ udp_from_helper (const struct GNUNET_TUN_UdpHeader *udp,
     return;
   }
   mlen = sizeof (struct GNUNET_EXIT_UdpReplyMessage) + pktlen - sizeof (struct GNUNET_TUN_UdpHeader);
-  tnq = GNUNET_malloc (sizeof (struct TunnelMessageQueue) + mlen);  
+  tnq = GNUNET_malloc (sizeof (struct TunnelMessageQueue) + mlen);
   tnq->payload = &tnq[1];
   tnq->len = mlen;
   urm = (struct GNUNET_EXIT_UdpReplyMessage *) &tnq[1];
@@ -852,8 +1142,7 @@ udp_from_helper (const struct GNUNET_TUN_UdpHeader *udp,
   memcpy (&urm[1],
          &udp[1],
          pktlen - sizeof (struct GNUNET_TUN_UdpHeader));
-  send_packet_to_mesh_tunnel (state->tunnel,
-                             tnq);
+  send_packet_to_mesh_tunnel (state, tnq);
 }
 
 
@@ -863,13 +1152,13 @@ udp_from_helper (const struct GNUNET_TUN_UdpHeader *udp,
  * @param tcp A pointer to the Packet
  * @param pktlen the length of the packet, including its TCP header
  * @param af address family (AFINET or AF_INET6)
- * @param destination_ip destination IP-address of the IP packet (should 
+ * @param destination_ip destination IP-address of the IP packet (should
  *                       be our local address)
  * @param source_ip original source IP-address of the IP packet (should
  *                       be the original destination address)
  */
 static void
-tcp_from_helper (const struct GNUNET_TUN_TcpHeader *tcp, 
+tcp_from_helper (const struct GNUNET_TUN_TcpHeader *tcp,
                 size_t pktlen,
                 int af,
                 const void *destination_ip,
@@ -904,7 +1193,7 @@ tcp_from_helper (const struct GNUNET_TUN_TcpHeader *tcp,
     return;
   }
   state = get_redirect_state (af, IPPROTO_TCP,
-                             source_ip, 
+                             source_ip,
                              ntohs (tcp->source_port),
                              destination_ip,
                              ntohs (tcp->destination_port),
@@ -913,12 +1202,12 @@ tcp_from_helper (const struct GNUNET_TUN_TcpHeader *tcp,
   {
     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
                _("TCP Packet dropped, have no matching connection information\n"));
-    
+
     return;
   }
   /* mug port numbers and crc to avoid information leakage;
      sender will need to lookup the correct values anyway */
-  memcpy (buf, tcp, pktlen);  
+  memcpy (buf, tcp, pktlen);
   mtcp = (struct GNUNET_TUN_TcpHeader *) buf;
   mtcp->source_port = 0;
   mtcp->destination_port = 0;
@@ -939,10 +1228,9 @@ tcp_from_helper (const struct GNUNET_TUN_TcpHeader *tcp,
   tdm->header.type = htons (GNUNET_MESSAGE_TYPE_VPN_TCP_DATA_TO_VPN);
   tdm->reserved = htonl (0);
   memcpy (&tdm->tcp_header,
-         buf, 
+         buf,
          pktlen);
-  send_packet_to_mesh_tunnel (state->tunnel,
-                             tnq);
+  send_packet_to_mesh_tunnel (state, tnq);
 }
 
 
@@ -953,7 +1241,7 @@ tcp_from_helper (const struct GNUNET_TUN_TcpHeader *tcp,
  * @param client unsued
  * @param message message received from helper
  */
-static void
+static int
 message_token (void *cls GNUNET_UNUSED, void *client GNUNET_UNUSED,
                const struct GNUNET_MessageHeader *message)
 {
@@ -970,13 +1258,13 @@ message_token (void *cls GNUNET_UNUSED, void *client GNUNET_UNUSED,
   if (ntohs (message->type) != GNUNET_MESSAGE_TYPE_VPN_HELPER)
   {
     GNUNET_break (0);
-    return;
+    return GNUNET_OK;
   }
   size = ntohs (message->size);
   if (size < sizeof (struct GNUNET_TUN_Layer2PacketHeader) + sizeof (struct GNUNET_MessageHeader))
   {
     GNUNET_break (0);
-    return;
+    return GNUNET_OK;
   }
   GNUNET_STATISTICS_update (stats,
                            gettext_noop ("# Bytes received from TUN"),
@@ -993,48 +1281,48 @@ message_token (void *cls GNUNET_UNUSED, void *client GNUNET_UNUSED,
       {
        /* Kernel to blame? */
        GNUNET_break (0);
-       return;
+        return GNUNET_OK;
       }
       pkt4 = (const struct GNUNET_TUN_IPv4Header *) &pkt_tun[1];
       if (size != ntohs (pkt4->total_length))
       {
        /* Kernel to blame? */
        GNUNET_break (0);
-       return;
+        return GNUNET_OK;
       }
       if (pkt4->header_length * 4 != sizeof (struct GNUNET_TUN_IPv4Header))
       {
        GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
                    _("IPv4 packet options received.  Ignored.\n"));
-       return;
+        return GNUNET_OK;
       }
-      
+
       size -= sizeof (struct GNUNET_TUN_IPv4Header);
       switch (pkt4->protocol)
       {
       case IPPROTO_UDP:
        udp_from_helper ((const struct GNUNET_TUN_UdpHeader *) &pkt4[1], size,
                         AF_INET,
-                        &pkt4->destination_address, 
+                        &pkt4->destination_address,
                         &pkt4->source_address);
        break;
       case IPPROTO_TCP:
        tcp_from_helper ((const struct GNUNET_TUN_TcpHeader *) &pkt4[1], size,
                         AF_INET,
-                        &pkt4->destination_address, 
+                        &pkt4->destination_address,
                         &pkt4->source_address);
        break;
       case IPPROTO_ICMP:
        icmp_from_helper ((const struct GNUNET_TUN_IcmpHeader *) &pkt4[1], size,
                          AF_INET,
-                         &pkt4->destination_address, 
+                         &pkt4->destination_address,
                          &pkt4->source_address);
        break;
       default:
        GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
                    _("IPv4 packet with unsupported next header %u received.  Ignored.\n"),
                    (int) pkt4->protocol);
-       return;
+        return GNUNET_OK;
       }
     }
     break;
@@ -1046,14 +1334,14 @@ message_token (void *cls GNUNET_UNUSED, void *client GNUNET_UNUSED,
       {
        /* Kernel to blame? */
        GNUNET_break (0);
-       return;
+        return GNUNET_OK;
       }
       pkt6 = (struct GNUNET_TUN_IPv6Header *) &pkt_tun[1];
       if (size != ntohs (pkt6->payload_length) + sizeof (struct GNUNET_TUN_IPv6Header))
       {
        /* Kernel to blame? */
        GNUNET_break (0);
-       return;
+        return GNUNET_OK;
       }
       size -= sizeof (struct GNUNET_TUN_IPv6Header);
       switch (pkt6->next_header)
@@ -1061,26 +1349,26 @@ message_token (void *cls GNUNET_UNUSED, void *client GNUNET_UNUSED,
       case IPPROTO_UDP:
        udp_from_helper ((const struct GNUNET_TUN_UdpHeader *) &pkt6[1], size,
                         AF_INET6,
-                        &pkt6->destination_address, 
+                        &pkt6->destination_address,
                         &pkt6->source_address);
        break;
       case IPPROTO_TCP:
        tcp_from_helper ((const struct GNUNET_TUN_TcpHeader *) &pkt6[1], size,
                         AF_INET6,
-                        &pkt6->destination_address, 
+                        &pkt6->destination_address,
                         &pkt6->source_address);
        break;
       case IPPROTO_ICMPV6:
        icmp_from_helper ((const struct GNUNET_TUN_IcmpHeader *) &pkt6[1], size,
                          AF_INET6,
-                         &pkt6->destination_address, 
+                         &pkt6->destination_address,
                          &pkt6->source_address);
        break;
       default:
        GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
                    _("IPv6 packet with unsupported next header %d received.  Ignored.\n"),
                     pkt6->next_header);
-       return;
+        return GNUNET_OK;
       }
     }
     break;
@@ -1090,6 +1378,7 @@ message_token (void *cls GNUNET_UNUSED, void *client GNUNET_UNUSED,
                ntohs (pkt_tun->proto));
     break;
   }
+  return GNUNET_OK;
 }
 
 
@@ -1109,14 +1398,14 @@ setup_fresh_address (int af,
   local_address->af = af;
   local_address->proto = (uint8_t) proto;
   /* default "local" port range is often 32768--61000,
-     so we pick a random value in that range */         
+     so we pick a random value in that range */        
   if ( ( (af == AF_INET) && (proto == IPPROTO_ICMP) ) ||
        ( (af == AF_INET6) && (proto == IPPROTO_ICMPV6) ) )
     local_address->port = 0;
   else
-    local_address->port 
-      = (uint16_t) 32768 + GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 
-                                                    28232);      
+    local_address->port
+      = (uint16_t) 32768 + GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
+                                                    28232);
   switch (af)
   {
   case AF_INET:
@@ -1133,13 +1422,13 @@ setup_fresh_address (int af,
        local_address->address.ipv4 = addr;
        return;
       }
-      /* Given 192.168.0.1/255.255.0.0, we want a mask 
+      /* Given 192.168.0.1/255.255.0.0, we want a mask
         of '192.168.255.255', thus:  */
       mask.s_addr = addr.s_addr | ~mask.s_addr;
       /* Pick random IPv4 address within the subnet, except 'addr' or 'mask' itself */
       do
        {
-         rnd.s_addr = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 
+         rnd.s_addr = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
                                                 UINT32_MAX);   
          local_address->address.ipv4.s_addr = (addr.s_addr | rnd.s_addr) & mask.s_addr;
        }
@@ -1153,7 +1442,7 @@ setup_fresh_address (int af,
       struct in6_addr mask;
       struct in6_addr rnd;
       int i;
-      
+
       addr = exit_ipv6addr;
       GNUNET_assert (ipv6prefix < 128);
       if (ipv6prefix == 127)
@@ -1167,13 +1456,13 @@ setup_fresh_address (int af,
       mask = addr;
       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 */
       do
        {
          for (i=0;i<16;i++)
          {
-           rnd.s6_addr[i] = (unsigned char) GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 
+           rnd.s6_addr[i] = (unsigned char) GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
                                                                       256);
            local_address->address.ipv6.s6_addr[i]
              = (addr.s6_addr[i] | rnd.s6_addr[i]) & mask.s6_addr[i];
@@ -1189,7 +1478,7 @@ setup_fresh_address (int af,
     break;
   default:
     GNUNET_assert (0);
-  }  
+  }
 }
 
 
@@ -1206,7 +1495,7 @@ setup_fresh_address (int af,
  * cleaning up 'old' states.
  *
  * @param state skeleton state to setup a record for; should
- *              'state->ri.remote_address' filled in so that
+ *              'state->specifics.tcp_udp.ri.remote_address' filled in so that
  *              this code can determine which AF/protocol is
  *              going to be used (the 'tunnel' should also
  *              already be set); after calling this function,
@@ -1217,53 +1506,53 @@ setup_fresh_address (int af,
 static void
 setup_state_record (struct TunnelState *state)
 {
-  GNUNET_HashCode key;
+  struct GNUNET_HashCode key;
   struct TunnelState *s;
 
   /* generate fresh, unique address */
   do
   {
-    if (NULL == state->serv)
-      setup_fresh_address (state->ri.remote_address.af,
-                          state->ri.remote_address.proto,
-                          &state->ri.local_address);
+    if (NULL == state->specifics.tcp_udp.serv)
+      setup_fresh_address (state->specifics.tcp_udp.ri.remote_address.af,
+                          state->specifics.tcp_udp.ri.remote_address.proto,
+                          &state->specifics.tcp_udp.ri.local_address);
     else
-      setup_fresh_address (state->serv->address.af,
-                          state->serv->address.proto,
-                          &state->ri.local_address);
-  } while (NULL != get_redirect_state (state->ri.remote_address.af,
-                                      state->ri.remote_address.proto,
-                                      &state->ri.remote_address.address,
-                                      state->ri.remote_address.port,
-                                      &state->ri.local_address.address,
-                                      state->ri.local_address.port,
+      setup_fresh_address (state->specifics.tcp_udp.serv->address.af,
+                          state->specifics.tcp_udp.serv->address.proto,
+                          &state->specifics.tcp_udp.ri.local_address);
+  } while (NULL != get_redirect_state (state->specifics.tcp_udp.ri.remote_address.af,
+                                      state->specifics.tcp_udp.ri.remote_address.proto,
+                                      &state->specifics.tcp_udp.ri.remote_address.address,
+                                      state->specifics.tcp_udp.ri.remote_address.port,
+                                      &state->specifics.tcp_udp.ri.local_address.address,
+                                      state->specifics.tcp_udp.ri.local_address.port,
                                       &key));
   {
     char buf[INET6_ADDRSTRLEN];
     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                "Picked local address %s:%u for new connection\n",
-               inet_ntop (state->ri.local_address.af, 
-                          &state->ri.local_address.address,
+               inet_ntop (state->specifics.tcp_udp.ri.local_address.af,
+                          &state->specifics.tcp_udp.ri.local_address.address,
                           buf, sizeof (buf)),
-               (unsigned int) state->ri.local_address.port);
+               (unsigned int) state->specifics.tcp_udp.ri.local_address.port);
   }
-  state->state_key = key;
+  state->specifics.tcp_udp.state_key = key;
   GNUNET_assert (GNUNET_OK ==
-                GNUNET_CONTAINER_multihashmap_put (connections_map, 
+                GNUNET_CONTAINER_multihashmap_put (connections_map,
                                                    &key, state,
                                                    GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
-  state->heap_node = GNUNET_CONTAINER_heap_insert (connections_heap,
+  state->specifics.tcp_udp.heap_node = GNUNET_CONTAINER_heap_insert (connections_heap,
                                                   state,
-                                                  GNUNET_TIME_absolute_get ().abs_value);   
+                                                  GNUNET_TIME_absolute_get ().abs_value_us);
   while (GNUNET_CONTAINER_heap_get_size (connections_heap) > max_connections)
   {
     s = GNUNET_CONTAINER_heap_remove_root (connections_heap);
     GNUNET_assert (state != s);
-    s->heap_node = NULL;
+    s->specifics.tcp_udp.heap_node = NULL;
     GNUNET_MESH_tunnel_destroy (s->tunnel);
     GNUNET_assert (GNUNET_OK ==
                   GNUNET_CONTAINER_multihashmap_remove (connections_map,
-                                                        &s->state_key, 
+                                                        &s->specifics.tcp_udp.state_key,
                                                         s));
     GNUNET_free (s);
   }
@@ -1341,7 +1630,7 @@ prepare_ipv4_packet (const void *payload, size_t payload_length,
   case IPPROTO_TCP:
     {
       struct GNUNET_TUN_TcpHeader *pkt4_tcp = (struct GNUNET_TUN_TcpHeader *) &pkt4[1];
-      
+
       *pkt4_tcp = *tcp_header;
       pkt4_tcp->source_port = htons (src_address->port);
       pkt4_tcp->destination_port = htons (dst_address->port);
@@ -1407,7 +1696,7 @@ prepare_ipv6_packet (const void *payload, size_t payload_length,
 
   GNUNET_TUN_initialize_ipv6_header (pkt6,
                                     protocol,
-                                    len,                                  
+                                    len,                               
                                     &src_address->address.ipv6,
                                     &dst_address->address.ipv6);
 
@@ -1496,7 +1785,7 @@ send_tcp_packet_via_tun (const struct SocketAddress *destination_address,
     char buf[len] GNUNET_ALIGN;
     struct GNUNET_MessageHeader *hdr;
     struct GNUNET_TUN_Layer2PacketHeader *tun;
-    
+
     hdr = (struct GNUNET_MessageHeader *) buf;
     hdr->type = htons (GNUNET_MESSAGE_TYPE_VPN_HELPER);
     hdr->size = htons (len);
@@ -1511,7 +1800,7 @@ send_tcp_packet_via_tun (const struct SocketAddress *destination_address,
        tun->proto = htons (ETH_P_IPV4);
        prepare_ipv4_packet (payload, payload_length,
                             IPPROTO_TCP,
-                            tcp_header, 
+                            tcp_header,
                             source_address,
                             destination_address,
                             ipv4);
@@ -1522,9 +1811,9 @@ send_tcp_packet_via_tun (const struct SocketAddress *destination_address,
        struct GNUNET_TUN_IPv6Header * ipv6 = (struct GNUNET_TUN_IPv6Header*) &tun[1];
        
        tun->proto = htons (ETH_P_IPV6);
-       prepare_ipv6_packet (payload, payload_length, 
+       prepare_ipv6_packet (payload, payload_length,
                             IPPROTO_TCP,
-                            tcp_header, 
+                            tcp_header,
                             source_address,
                             destination_address,
                             ipv6);
@@ -1534,10 +1823,11 @@ send_tcp_packet_via_tun (const struct SocketAddress *destination_address,
       GNUNET_assert (0);
       break;
     }
-    (void) GNUNET_HELPER_send (helper_handle,
-                              (const struct GNUNET_MessageHeader*) buf,
-                              GNUNET_YES,
-                              NULL, NULL);
+    if (NULL != helper_handle)
+      (void) GNUNET_HELPER_send (helper_handle,
+                                (const struct GNUNET_MessageHeader*) buf,
+                                GNUNET_YES,
+                                NULL, NULL);
   }
 }
 
@@ -1549,23 +1839,35 @@ send_tcp_packet_via_tun (const struct SocketAddress *destination_address,
  * @param cls closure, NULL
  * @param tunnel connection to the other end
  * @param tunnel_ctx pointer to our 'struct TunnelState *'
- * @param sender who sent the message
  * @param message the actual message
- * @param atsi performance data for the connection
+ *
  * @return GNUNET_OK to keep the connection open,
  *         GNUNET_SYSERR to close it (signal serious error)
  */
 static int
 receive_tcp_service (void *cls GNUNET_UNUSED, struct GNUNET_MESH_Tunnel *tunnel,
                      void **tunnel_ctx GNUNET_UNUSED,
-                     const struct GNUNET_PeerIdentity *sender GNUNET_UNUSED,
-                     const struct GNUNET_MessageHeader *message,
-                     const struct GNUNET_ATS_Information *atsi GNUNET_UNUSED)
+                     const struct GNUNET_MessageHeader *message)
 {
   struct TunnelState *state = *tunnel_ctx;
   const struct GNUNET_EXIT_TcpServiceStartMessage *start;
   uint16_t pkt_len = ntohs (message->size);
 
+  if (NULL == state)
+  {
+    GNUNET_break_op (0);
+    return GNUNET_SYSERR;
+  }
+  if (GNUNET_YES == state->is_dns)
+  {
+    GNUNET_break_op (0);
+    return GNUNET_SYSERR;
+  }
+  if (GNUNET_SYSERR == state->is_dns)
+  {
+    /* tunnel is UDP/TCP from now on */
+    state->is_dns = GNUNET_NO;
+  }
   GNUNET_STATISTICS_update (stats,
                            gettext_noop ("# TCP service creation requests received via mesh"),
                            1, GNUNET_NO);
@@ -1580,9 +1882,8 @@ receive_tcp_service (void *cls GNUNET_UNUSED, struct GNUNET_MESH_Tunnel *tunnel,
   }
   start = (const struct GNUNET_EXIT_TcpServiceStartMessage*) message;
   pkt_len -= sizeof (struct GNUNET_EXIT_TcpServiceStartMessage);
-  if ( (NULL == state) ||
-       (NULL != state->serv) ||
-       (NULL != state->heap_node) )
+  if ( (NULL != state->specifics.tcp_udp.serv) ||
+       (NULL != state->specifics.tcp_udp.heap_node) )
   {
     GNUNET_break_op (0);
     return GNUNET_SYSERR;
@@ -1596,13 +1897,13 @@ receive_tcp_service (void *cls GNUNET_UNUSED, struct GNUNET_MESH_Tunnel *tunnel,
   /* 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_i2s (&state->peer),
              GNUNET_h2s (&start->service_descriptor),
-             (unsigned int) ntohs (start->tcp_header.destination_port));  
-  if (NULL == (state->serv = find_service (tcp_services, &start->service_descriptor, 
-                                          ntohs (start->tcp_header.destination_port))))
+             (unsigned int) ntohs (start->tcp_header.destination_port));
+  if (NULL == (state->specifics.tcp_udp.serv = find_service (tcp_services, &start->service_descriptor,
+                                                            ntohs (start->tcp_header.destination_port))))
   {
-    GNUNET_log (GNUNET_ERROR_TYPE_INFO, 
+    GNUNET_log (GNUNET_ERROR_TYPE_INFO,
                _("No service found for %s on port %d!\n"),
                "TCP",
                ntohs (start->tcp_header.destination_port));
@@ -1611,10 +1912,10 @@ receive_tcp_service (void *cls GNUNET_UNUSED, struct GNUNET_MESH_Tunnel *tunnel,
                              1, GNUNET_NO);
     return GNUNET_SYSERR;
   }
-  state->ri.remote_address = state->serv->address;    
+  state->specifics.tcp_udp.ri.remote_address = state->specifics.tcp_udp.serv->address;
   setup_state_record (state);
-  send_tcp_packet_via_tun (&state->ri.remote_address,
-                          &state->ri.local_address,
+  send_tcp_packet_via_tun (&state->specifics.tcp_udp.ri.remote_address,
+                          &state->specifics.tcp_udp.ri.local_address,
                           &start->tcp_header,
                           &start[1], pkt_len);
   return GNUNET_YES;
@@ -1627,18 +1928,15 @@ receive_tcp_service (void *cls GNUNET_UNUSED, struct GNUNET_MESH_Tunnel *tunnel,
  * @param cls closure, NULL
  * @param tunnel connection to the other end
  * @param tunnel_ctx pointer to our 'struct TunnelState *'
- * @param sender who sent the message
  * @param message the actual message
- * @param atsi performance data for the connection
+ *
  * @return GNUNET_OK to keep the connection open,
  *         GNUNET_SYSERR to close it (signal serious error)
  */
 static int
 receive_tcp_remote (void *cls GNUNET_UNUSED, struct GNUNET_MESH_Tunnel *tunnel,
                     void **tunnel_ctx GNUNET_UNUSED,
-                    const struct GNUNET_PeerIdentity *sender GNUNET_UNUSED,
-                    const struct GNUNET_MessageHeader *message,
-                    const struct GNUNET_ATS_Information *atsi GNUNET_UNUSED)
+                    const struct GNUNET_MessageHeader *message)
 {
   struct TunnelState *state = *tunnel_ctx;
   const struct GNUNET_EXIT_TcpInternetStartMessage *start;
@@ -1648,6 +1946,21 @@ receive_tcp_remote (void *cls GNUNET_UNUSED, struct GNUNET_MESH_Tunnel *tunnel,
   const void *payload;
   int af;
 
+  if (NULL == state)
+  {
+    GNUNET_break_op (0);
+    return GNUNET_SYSERR;
+  }
+  if (GNUNET_YES == state->is_dns)
+  {
+    GNUNET_break_op (0);
+    return GNUNET_SYSERR;
+  }
+  if (GNUNET_SYSERR == state->is_dns)
+  {
+    /* tunnel is UDP/TCP from now on */
+    state->is_dns = GNUNET_NO;
+  }
   GNUNET_STATISTICS_update (stats,
                            gettext_noop ("# Bytes received from MESH"),
                            pkt_len, GNUNET_NO);
@@ -1660,10 +1973,9 @@ receive_tcp_remote (void *cls GNUNET_UNUSED, struct GNUNET_MESH_Tunnel *tunnel,
     return GNUNET_SYSERR;
   }
   start = (const struct GNUNET_EXIT_TcpInternetStartMessage*) message;
-  pkt_len -= sizeof (struct GNUNET_EXIT_TcpInternetStartMessage);  
-  if ( (NULL == state) ||
-       (NULL != state->serv) ||
-       (NULL != state->heap_node) )
+  pkt_len -= sizeof (struct GNUNET_EXIT_TcpInternetStartMessage);
+  if ( (NULL != state->specifics.tcp_udp.serv) ||
+       (NULL != state->specifics.tcp_udp.heap_node) )
   {
     GNUNET_break_op (0);
     return GNUNET_SYSERR;
@@ -1674,7 +1986,7 @@ receive_tcp_remote (void *cls GNUNET_UNUSED, struct GNUNET_MESH_Tunnel *tunnel,
     return GNUNET_SYSERR;
   }
   af = (int) ntohl (start->af);
-  state->ri.remote_address.af = af;
+  state->specifics.tcp_udp.ri.remote_address.af = af;
   switch (af)
   {
   case AF_INET:
@@ -1691,7 +2003,7 @@ receive_tcp_remote (void *cls GNUNET_UNUSED, struct GNUNET_MESH_Tunnel *tunnel,
     v4 = (const struct in_addr*) &start[1];
     payload = &v4[1];
     pkt_len -= sizeof (struct in_addr);
-    state->ri.remote_address.address.ipv4 = *v4;
+    state->specifics.tcp_udp.ri.remote_address.address.ipv4 = *v4;
     break;
   case AF_INET6:
     if (pkt_len < sizeof (struct in6_addr))
@@ -1707,7 +2019,7 @@ receive_tcp_remote (void *cls GNUNET_UNUSED, struct GNUNET_MESH_Tunnel *tunnel,
     v6 = (const struct in6_addr*) &start[1];
     payload = &v6[1];
     pkt_len -= sizeof (struct in6_addr);
-    state->ri.remote_address.address.ipv6 = *v6;
+    state->specifics.tcp_udp.ri.remote_address.address.ipv6 = *v6;
     break;
   default:
     GNUNET_break_op (0);
@@ -1717,17 +2029,17 @@ receive_tcp_remote (void *cls GNUNET_UNUSED, struct GNUNET_MESH_Tunnel *tunnel,
     char buf[INET6_ADDRSTRLEN];
     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                "Received data from %s for starting TCP stream to %s:%u\n",
-               GNUNET_i2s (sender),
-               inet_ntop (af, 
-                          &state->ri.remote_address.address,
+               GNUNET_i2s (&state->peer),
+               inet_ntop (af,
+                          &state->specifics.tcp_udp.ri.remote_address.address,
                           buf, sizeof (buf)),
-               (unsigned int) ntohs (start->tcp_header.destination_port));  
+               (unsigned int) ntohs (start->tcp_header.destination_port));
   }
-  state->ri.remote_address.proto = IPPROTO_TCP;
-  state->ri.remote_address.port = ntohs (start->tcp_header.destination_port);
+  state->specifics.tcp_udp.ri.remote_address.proto = IPPROTO_TCP;
+  state->specifics.tcp_udp.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,
+  send_tcp_packet_via_tun (&state->specifics.tcp_udp.ri.remote_address,
+                          &state->specifics.tcp_udp.ri.local_address,
                           &start->tcp_header,
                           payload, pkt_len);
   return GNUNET_YES;
@@ -1735,29 +2047,36 @@ receive_tcp_remote (void *cls GNUNET_UNUSED, struct GNUNET_MESH_Tunnel *tunnel,
 
 
 /**
- * Process a request to forward TCP data on an established 
+ * Process a request to forward TCP data on an established
  * connection via this peer.
  *
  * @param cls closure, NULL
  * @param tunnel connection to the other end
  * @param tunnel_ctx pointer to our 'struct TunnelState *'
- * @param sender who sent the message
  * @param message the actual message
- * @param atsi performance data for the connection
+ *
  * @return GNUNET_OK to keep the connection open,
  *         GNUNET_SYSERR to close it (signal serious error)
  */
 static int
 receive_tcp_data (void *cls GNUNET_UNUSED, struct GNUNET_MESH_Tunnel *tunnel,
                  void **tunnel_ctx GNUNET_UNUSED,
-                 const struct GNUNET_PeerIdentity *sender GNUNET_UNUSED,
-                 const struct GNUNET_MessageHeader *message,
-                 const struct GNUNET_ATS_Information *atsi GNUNET_UNUSED)
+                 const struct GNUNET_MessageHeader *message)
 {
   struct TunnelState *state = *tunnel_ctx;
   const struct GNUNET_EXIT_TcpDataMessage *data;
   uint16_t pkt_len = ntohs (message->size);
 
+  if (GNUNET_YES == state->is_dns)
+  {
+    GNUNET_break_op (0);
+    return GNUNET_SYSERR;
+  }
+  if (GNUNET_SYSERR == state->is_dns)
+  {
+    /* tunnel is UDP/TCP from now on */
+    state->is_dns = GNUNET_NO;
+  }
   GNUNET_STATISTICS_update (stats,
                            gettext_noop ("# Bytes received from MESH"),
                            pkt_len, GNUNET_NO);
@@ -1770,9 +2089,9 @@ receive_tcp_data (void *cls GNUNET_UNUSED, struct GNUNET_MESH_Tunnel *tunnel,
     return GNUNET_SYSERR;
   }
   data = (const struct GNUNET_EXIT_TcpDataMessage*) message;
-  pkt_len -= sizeof (struct GNUNET_EXIT_TcpDataMessage);  
+  pkt_len -= sizeof (struct GNUNET_EXIT_TcpDataMessage);
   if ( (NULL == state) ||
-       (NULL == state->heap_node) )
+       (NULL == state->specifics.tcp_udp.heap_node) )
   {
     /* connection should have been up! */
     GNUNET_STATISTICS_update (stats,
@@ -1792,15 +2111,15 @@ receive_tcp_data (void *cls GNUNET_UNUSED, struct GNUNET_MESH_Tunnel *tunnel,
     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                "Received additional %u bytes of data from %s for TCP stream to %s:%u\n",
                pkt_len,
-               GNUNET_i2s (sender),
-               inet_ntop (state->ri.remote_address.af, 
-                          &state->ri.remote_address.address,
+               GNUNET_i2s (&state->peer),
+               inet_ntop (state->specifics.tcp_udp.ri.remote_address.af,
+                          &state->specifics.tcp_udp.ri.remote_address.address,
                           buf, sizeof (buf)),
-               (unsigned int) state->ri.remote_address.port);
+               (unsigned int) state->specifics.tcp_udp.ri.remote_address.port);
   }
 
-  send_tcp_packet_via_tun (&state->ri.remote_address,
-                          &state->ri.local_address,
+  send_tcp_packet_via_tun (&state->specifics.tcp_udp.ri.remote_address,
+                          &state->specifics.tcp_udp.ri.local_address,
                           &data->tcp_header,
                           &data[1], pkt_len);
   return GNUNET_YES;
@@ -1855,7 +2174,7 @@ send_icmp_packet_via_tun (const struct SocketAddress *destination_address,
     char buf[len] GNUNET_ALIGN;
     struct GNUNET_MessageHeader *hdr;
     struct GNUNET_TUN_Layer2PacketHeader *tun;
-    
+
     hdr= (struct GNUNET_MessageHeader *) buf;
     hdr->type = htons (GNUNET_MESSAGE_TYPE_VPN_HELPER);
     hdr->size = htons (len);
@@ -1900,10 +2219,11 @@ send_icmp_packet_via_tun (const struct SocketAddress *destination_address,
     GNUNET_TUN_calculate_icmp_checksum (icmp,
                                        payload,
                                        payload_length);
-    (void) GNUNET_HELPER_send (helper_handle,
-                              (const struct GNUNET_MessageHeader*) buf,
-                              GNUNET_YES,
-                              NULL, NULL);
+    if (NULL != helper_handle)
+      (void) GNUNET_HELPER_send (helper_handle,
+                                (const struct GNUNET_MessageHeader*) buf,
+                                GNUNET_YES,
+                                NULL, NULL);
   }
 }
 
@@ -1923,12 +2243,12 @@ make_up_icmpv4_payload (struct TunnelState *state,
                        struct GNUNET_TUN_UdpHeader *udp)
 {
   GNUNET_TUN_initialize_ipv4_header (ipp,
-                                    state->ri.remote_address.proto,
+                                    state->specifics.tcp_udp.ri.remote_address.proto,
                                     sizeof (struct GNUNET_TUN_TcpHeader),
-                                    &state->ri.remote_address.address.ipv4,
-                                    &state->ri.local_address.address.ipv4);
-  udp->source_port = htons (state->ri.remote_address.port);
-  udp->destination_port = htons (state->ri.local_address.port);
+                                    &state->specifics.tcp_udp.ri.remote_address.address.ipv4,
+                                    &state->specifics.tcp_udp.ri.local_address.address.ipv4);
+  udp->source_port = htons (state->specifics.tcp_udp.ri.remote_address.port);
+  udp->destination_port = htons (state->specifics.tcp_udp.ri.local_address.port);
   udp->len = htons (0);
   udp->crc = htons (0);
 }
@@ -1949,12 +2269,12 @@ make_up_icmpv6_payload (struct TunnelState *state,
                        struct GNUNET_TUN_UdpHeader *udp)
 {
   GNUNET_TUN_initialize_ipv6_header (ipp,
-                                    state->ri.remote_address.proto,
+                                    state->specifics.tcp_udp.ri.remote_address.proto,
                                     sizeof (struct GNUNET_TUN_TcpHeader),
-                                    &state->ri.remote_address.address.ipv6,
-                                    &state->ri.local_address.address.ipv6);
-  udp->source_port = htons (state->ri.remote_address.port);
-  udp->destination_port = htons (state->ri.local_address.port);
+                                    &state->specifics.tcp_udp.ri.remote_address.address.ipv6,
+                                    &state->specifics.tcp_udp.ri.local_address.address.ipv6);
+  udp->source_port = htons (state->specifics.tcp_udp.ri.remote_address.port);
+  udp->destination_port = htons (state->specifics.tcp_udp.ri.local_address.port);
   udp->len = htons (0);
   udp->crc = htons (0);
 }
@@ -1966,28 +2286,35 @@ make_up_icmpv6_payload (struct TunnelState *state,
  * @param cls closure, NULL
  * @param tunnel connection to the other end
  * @param tunnel_ctx pointer to our 'struct TunnelState *'
- * @param sender who sent the message
  * @param message the actual message
- * @param atsi performance data for the connection
+ *
  * @return GNUNET_OK to keep the connection open,
  *         GNUNET_SYSERR to close it (signal serious error)
  */
 static int
 receive_icmp_remote (void *cls GNUNET_UNUSED, struct GNUNET_MESH_Tunnel *tunnel,
                     void **tunnel_ctx GNUNET_UNUSED,
-                    const struct GNUNET_PeerIdentity *sender GNUNET_UNUSED,
-                    const struct GNUNET_MessageHeader *message,
-                    const struct GNUNET_ATS_Information *atsi GNUNET_UNUSED)
+                    const struct GNUNET_MessageHeader *message)
 {
   struct TunnelState *state = *tunnel_ctx;
   const struct GNUNET_EXIT_IcmpInternetMessage *msg;
   uint16_t pkt_len = ntohs (message->size);
   const struct in_addr *v4;
-  const struct in6_addr *v6;  
+  const struct in6_addr *v6;
   const void *payload;
   char buf[sizeof (struct GNUNET_TUN_IPv6Header) + 8] GNUNET_ALIGN;
   int af;
 
+  if (GNUNET_YES == state->is_dns)
+  {
+    GNUNET_break_op (0);
+    return GNUNET_SYSERR;
+  }
+  if (GNUNET_SYSERR == state->is_dns)
+  {
+    /* tunnel is UDP/TCP from now on */
+    state->is_dns = GNUNET_NO;
+  }
   GNUNET_STATISTICS_update (stats,
                            gettext_noop ("# Bytes received from MESH"),
                            pkt_len, GNUNET_NO);
@@ -2000,11 +2327,11 @@ receive_icmp_remote (void *cls GNUNET_UNUSED, struct GNUNET_MESH_Tunnel *tunnel,
     return GNUNET_SYSERR;
   }
   msg = (const struct GNUNET_EXIT_IcmpInternetMessage*) message;
-  pkt_len -= sizeof (struct GNUNET_EXIT_IcmpInternetMessage);  
+  pkt_len -= sizeof (struct GNUNET_EXIT_IcmpInternetMessage);
 
   af = (int) ntohl (msg->af);
-  if ( (NULL != state->heap_node) &&
-       (af != state->ri.remote_address.af) )
+  if ( (NULL != state->specifics.tcp_udp.heap_node) &&
+       (af != state->specifics.tcp_udp.ri.remote_address.af) )
   {
     /* other peer switched AF on this tunnel; not allowed */
     GNUNET_break_op (0);
@@ -2027,14 +2354,14 @@ receive_icmp_remote (void *cls GNUNET_UNUSED, struct GNUNET_MESH_Tunnel *tunnel,
     v4 = (const struct in_addr*) &msg[1];
     payload = &v4[1];
     pkt_len -= sizeof (struct in_addr);
-    state->ri.remote_address.address.ipv4 = *v4;
-    if (NULL == state->heap_node)
+    state->specifics.tcp_udp.ri.remote_address.address.ipv4 = *v4;
+    if (NULL == state->specifics.tcp_udp.heap_node)
     {
-      state->ri.remote_address.af = af;
-      state->ri.remote_address.proto = IPPROTO_ICMP;
+      state->specifics.tcp_udp.ri.remote_address.af = af;
+      state->specifics.tcp_udp.ri.remote_address.proto = IPPROTO_ICMP;
       setup_state_record (state);
     }
-    /* check that ICMP type is something we want to support 
+    /* check that ICMP type is something we want to support
        and possibly make up payload! */
     switch (msg->icmp_header.type)
     {
@@ -2056,7 +2383,7 @@ receive_icmp_remote (void *cls GNUNET_UNUSED, struct GNUNET_MESH_Tunnel *tunnel,
 
        GNUNET_assert (8 == sizeof (struct GNUNET_TUN_UdpHeader));
        pkt_len = sizeof (struct GNUNET_TUN_IPv4Header) + 8;
-       make_up_icmpv4_payload (state, 
+       make_up_icmpv4_payload (state,
                                ipp,
                                udp);
        payload = ipp;
@@ -2067,7 +2394,7 @@ receive_icmp_remote (void *cls GNUNET_UNUSED, struct GNUNET_MESH_Tunnel *tunnel,
       GNUNET_STATISTICS_update (stats,
                                gettext_noop ("# ICMPv4 packets dropped (type not allowed)"),
                                1, GNUNET_NO);
-      return GNUNET_SYSERR;      
+      return GNUNET_SYSERR;
     }
     /* end AF_INET */
     break;
@@ -2085,14 +2412,14 @@ receive_icmp_remote (void *cls GNUNET_UNUSED, struct GNUNET_MESH_Tunnel *tunnel,
     v6 = (const struct in6_addr*) &msg[1];
     payload = &v6[1];
     pkt_len -= sizeof (struct in6_addr);
-    state->ri.remote_address.address.ipv6 = *v6;
-    if (NULL == state->heap_node)
+    state->specifics.tcp_udp.ri.remote_address.address.ipv6 = *v6;
+    if (NULL == state->specifics.tcp_udp.heap_node)
     {
-      state->ri.remote_address.af = af;
-      state->ri.remote_address.proto = IPPROTO_ICMPV6;
+      state->specifics.tcp_udp.ri.remote_address.af = af;
+      state->specifics.tcp_udp.ri.remote_address.proto = IPPROTO_ICMPV6;
       setup_state_record (state);
     }
-    /* check that ICMP type is something we want to support 
+    /* check that ICMP type is something we want to support
        and possibly make up payload! */
     switch (msg->icmp_header.type)
     {
@@ -2115,7 +2442,7 @@ receive_icmp_remote (void *cls GNUNET_UNUSED, struct GNUNET_MESH_Tunnel *tunnel,
 
        GNUNET_assert (8 == sizeof (struct GNUNET_TUN_UdpHeader));
        pkt_len = sizeof (struct GNUNET_TUN_IPv6Header) + 8;
-       make_up_icmpv6_payload (state, 
+       make_up_icmpv6_payload (state,
                                ipp,
                                udp);
        payload = ipp;
@@ -2126,27 +2453,27 @@ receive_icmp_remote (void *cls GNUNET_UNUSED, struct GNUNET_MESH_Tunnel *tunnel,
       GNUNET_STATISTICS_update (stats,
                                gettext_noop ("# ICMPv6 packets dropped (type not allowed)"),
                                1, GNUNET_NO);
-      return GNUNET_SYSERR;      
+      return GNUNET_SYSERR;
     }
     /* end AF_INET6 */
-    break;    
+    break;
   default:
     /* bad AF */
     GNUNET_break_op (0);
     return GNUNET_SYSERR;
   }
+
   {
     char buf[INET6_ADDRSTRLEN];
     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                "Received ICMP data from %s for forwarding to %s\n",
-               GNUNET_i2s (sender),
-               inet_ntop (af, 
-                          &state->ri.remote_address.address,
+               GNUNET_i2s (&state->peer),
+               inet_ntop (af,
+                          &state->specifics.tcp_udp.ri.remote_address.address,
                           buf, sizeof (buf)));
   }
-  send_icmp_packet_via_tun (&state->ri.remote_address,
-                           &state->ri.local_address,
+  send_icmp_packet_via_tun (&state->specifics.tcp_udp.ri.remote_address,
+                           &state->specifics.tcp_udp.ri.local_address,
                            &msg->icmp_header,
                            payload, pkt_len);
   return GNUNET_YES;
@@ -2166,13 +2493,13 @@ static uint16_t
 make_up_icmp_service_payload (struct TunnelState *state,
                              char *buf)
 {
-  switch (state->serv->address.af)
+  switch (state->specifics.tcp_udp.serv->address.af)
   {
   case AF_INET:
     {
       struct GNUNET_TUN_IPv4Header *ipv4;
       struct GNUNET_TUN_UdpHeader *udp;
-      
+
       ipv4 = (struct GNUNET_TUN_IPv4Header *)buf;
       udp = (struct GNUNET_TUN_UdpHeader *) &ipv4[1];
       make_up_icmpv4_payload (state,
@@ -2193,7 +2520,7 @@ make_up_icmp_service_payload (struct TunnelState *state,
                              ipv6,
                              udp);
       GNUNET_assert (8 == sizeof (struct GNUNET_TUN_UdpHeader));
-      return sizeof (struct GNUNET_TUN_IPv6Header) + 8;      
+      return sizeof (struct GNUNET_TUN_IPv6Header) + 8;
     }
     break;
   default:
@@ -2210,18 +2537,15 @@ make_up_icmp_service_payload (struct TunnelState *state,
  * @param cls closure, NULL
  * @param tunnel connection to the other end
  * @param tunnel_ctx pointer to our 'struct TunnelState *'
- * @param sender who sent the message
  * @param message the actual message
- * @param atsi performance data for the connection
+ *
  * @return GNUNET_OK to keep the connection open,
  *         GNUNET_SYSERR to close it (signal serious error)
  */
 static int
 receive_icmp_service (void *cls GNUNET_UNUSED, struct GNUNET_MESH_Tunnel *tunnel,
                      void **tunnel_ctx,
-                     const struct GNUNET_PeerIdentity *sender GNUNET_UNUSED,
-                     const struct GNUNET_MessageHeader *message,
-                     const struct GNUNET_ATS_Information *atsi GNUNET_UNUSED)
+                     const struct GNUNET_MessageHeader *message)
 {
   struct TunnelState *state = *tunnel_ctx;
   const struct GNUNET_EXIT_IcmpServiceMessage *msg;
@@ -2230,6 +2554,16 @@ receive_icmp_service (void *cls GNUNET_UNUSED, struct GNUNET_MESH_Tunnel *tunnel
   char buf[sizeof (struct GNUNET_TUN_IPv6Header) + 8] GNUNET_ALIGN;
   const void *payload;
 
+  if (GNUNET_YES == state->is_dns)
+  {
+    GNUNET_break_op (0);
+    return GNUNET_SYSERR;
+  }
+  if (GNUNET_SYSERR == state->is_dns)
+  {
+    /* tunnel is UDP/TCP from now on */
+    state->is_dns = GNUNET_NO;
+  }
   GNUNET_STATISTICS_update (stats,
                            gettext_noop ("# Bytes received from MESH"),
                            pkt_len, GNUNET_NO);
@@ -2246,9 +2580,9 @@ receive_icmp_service (void *cls GNUNET_UNUSED, struct GNUNET_MESH_Tunnel *tunnel
   pkt_len -= sizeof (struct GNUNET_EXIT_IcmpServiceMessage);
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
              "Received data from %s for forwarding to ICMP service %s\n",
-             GNUNET_i2s (sender),
+             GNUNET_i2s (&state->peer),
              GNUNET_h2s (&msg->service_descriptor));
-  if (NULL == state->serv)
+  if (NULL == state->specifics.tcp_udp.serv)
   {
     /* first packet to service must not be ICMP (cannot determine service!) */
     GNUNET_break_op (0);
@@ -2256,7 +2590,7 @@ receive_icmp_service (void *cls GNUNET_UNUSED, struct GNUNET_MESH_Tunnel *tunnel
   }
   icmp = msg->icmp_header;
   payload = &msg[1];
-  state->ri.remote_address = state->serv->address;    
+  state->specifics.tcp_udp.ri.remote_address = state->specifics.tcp_udp.serv->address;
   setup_state_record (state);
 
   /* check that ICMP type is something we want to support,
@@ -2267,15 +2601,15 @@ receive_icmp_service (void *cls GNUNET_UNUSED, struct GNUNET_MESH_Tunnel *tunnel
     switch (msg->icmp_header.type)
     {
     case GNUNET_TUN_ICMPTYPE_ECHO_REPLY:
-      if (state->serv->address.af == AF_INET6)
+      if (state->specifics.tcp_udp.serv->address.af == AF_INET6)
        icmp.type = GNUNET_TUN_ICMPTYPE6_ECHO_REPLY;
       break;
     case GNUNET_TUN_ICMPTYPE_ECHO_REQUEST:
-      if (state->serv->address.af == AF_INET6)
+      if (state->specifics.tcp_udp.serv->address.af == AF_INET6)
        icmp.type = GNUNET_TUN_ICMPTYPE6_ECHO_REQUEST;
       break;
     case GNUNET_TUN_ICMPTYPE_DESTINATION_UNREACHABLE:
-      if (state->serv->address.af == AF_INET6)
+      if (state->specifics.tcp_udp.serv->address.af == AF_INET6)
        icmp.type = GNUNET_TUN_ICMPTYPE6_DESTINATION_UNREACHABLE;
       if (0 != pkt_len)
       {
@@ -2286,7 +2620,7 @@ receive_icmp_service (void *cls GNUNET_UNUSED, struct GNUNET_MESH_Tunnel *tunnel
       pkt_len = make_up_icmp_service_payload (state, buf);
       break;
     case GNUNET_TUN_ICMPTYPE_TIME_EXCEEDED:
-      if (state->serv->address.af == AF_INET6)
+      if (state->specifics.tcp_udp.serv->address.af == AF_INET6)
        icmp.type = GNUNET_TUN_ICMPTYPE6_TIME_EXCEEDED;
       if (0 != pkt_len)
       {
@@ -2297,7 +2631,7 @@ receive_icmp_service (void *cls GNUNET_UNUSED, struct GNUNET_MESH_Tunnel *tunnel
       pkt_len = make_up_icmp_service_payload (state, buf);
       break;
     case GNUNET_TUN_ICMPTYPE_SOURCE_QUENCH:
-      if (state->serv->address.af == AF_INET6)
+      if (state->specifics.tcp_udp.serv->address.af == AF_INET6)
       {
        GNUNET_STATISTICS_update (stats,
                                  gettext_noop ("# ICMPv4 packets dropped (impossible PT to v6)"),
@@ -2325,15 +2659,15 @@ receive_icmp_service (void *cls GNUNET_UNUSED, struct GNUNET_MESH_Tunnel *tunnel
     switch (msg->icmp_header.type)
     {
     case GNUNET_TUN_ICMPTYPE6_ECHO_REPLY:
-      if (state->serv->address.af == AF_INET)
+      if (state->specifics.tcp_udp.serv->address.af == AF_INET)
        icmp.type = GNUNET_TUN_ICMPTYPE_ECHO_REPLY;
       break;
     case GNUNET_TUN_ICMPTYPE6_ECHO_REQUEST:
-      if (state->serv->address.af == AF_INET)
+      if (state->specifics.tcp_udp.serv->address.af == AF_INET)
        icmp.type = GNUNET_TUN_ICMPTYPE_ECHO_REQUEST;
       break;
     case GNUNET_TUN_ICMPTYPE6_DESTINATION_UNREACHABLE:
-      if (state->serv->address.af == AF_INET)
+      if (state->specifics.tcp_udp.serv->address.af == AF_INET)
        icmp.type = GNUNET_TUN_ICMPTYPE_DESTINATION_UNREACHABLE;
       if (0 != pkt_len)
       {
@@ -2344,7 +2678,7 @@ receive_icmp_service (void *cls GNUNET_UNUSED, struct GNUNET_MESH_Tunnel *tunnel
       pkt_len = make_up_icmp_service_payload (state, buf);
       break;
     case GNUNET_TUN_ICMPTYPE6_TIME_EXCEEDED:
-      if (state->serv->address.af == AF_INET)
+      if (state->specifics.tcp_udp.serv->address.af == AF_INET)
        icmp.type = GNUNET_TUN_ICMPTYPE_TIME_EXCEEDED;
       if (0 != pkt_len)
       {
@@ -2356,7 +2690,7 @@ receive_icmp_service (void *cls GNUNET_UNUSED, struct GNUNET_MESH_Tunnel *tunnel
       break;
     case GNUNET_TUN_ICMPTYPE6_PACKET_TOO_BIG:
     case GNUNET_TUN_ICMPTYPE6_PARAMETER_PROBLEM:
-      if (state->serv->address.af == AF_INET)
+      if (state->specifics.tcp_udp.serv->address.af == AF_INET)
       {
        GNUNET_STATISTICS_update (stats,
                                  gettext_noop ("# ICMPv6 packets dropped (impossible PT to v4)"),
@@ -2385,8 +2719,8 @@ receive_icmp_service (void *cls GNUNET_UNUSED, struct GNUNET_MESH_Tunnel *tunnel
     return GNUNET_SYSERR;
   }
 
-  send_icmp_packet_via_tun (&state->ri.remote_address,
-                           &state->ri.local_address,
+  send_icmp_packet_via_tun (&state->specifics.tcp_udp.ri.remote_address,
+                           &state->specifics.tcp_udp.ri.local_address,
                            &icmp,
                            payload, pkt_len);
   return GNUNET_YES;
@@ -2438,7 +2772,7 @@ send_udp_packet_via_tun (const struct SocketAddress *destination_address,
     char buf[len] GNUNET_ALIGN;
     struct GNUNET_MessageHeader *hdr;
     struct GNUNET_TUN_Layer2PacketHeader *tun;
-    
+
     hdr= (struct GNUNET_MessageHeader *) buf;
     hdr->type = htons (GNUNET_MESSAGE_TYPE_VPN_HELPER);
     hdr->size = htons (len);
@@ -2464,7 +2798,7 @@ send_udp_packet_via_tun (const struct SocketAddress *destination_address,
        struct GNUNET_TUN_IPv6Header * ipv6 = (struct GNUNET_TUN_IPv6Header*) &tun[1];
        
        tun->proto = htons (ETH_P_IPV6);
-       prepare_ipv6_packet (payload, payload_length, 
+       prepare_ipv6_packet (payload, payload_length,
                             IPPROTO_UDP,
                             NULL,
                             source_address,
@@ -2476,10 +2810,11 @@ send_udp_packet_via_tun (const struct SocketAddress *destination_address,
       GNUNET_assert (0);
       break;
     }
-    (void) GNUNET_HELPER_send (helper_handle,
-                              (const struct GNUNET_MessageHeader*) buf,
-                              GNUNET_YES,
-                              NULL, NULL);
+    if (NULL != helper_handle)
+      (void) GNUNET_HELPER_send (helper_handle,
+                                (const struct GNUNET_MessageHeader*) buf,
+                                GNUNET_YES,
+                                NULL, NULL);
   }
 }
 
@@ -2490,18 +2825,15 @@ send_udp_packet_via_tun (const struct SocketAddress *destination_address,
  * @param cls closure, NULL
  * @param tunnel connection to the other end
  * @param tunnel_ctx pointer to our 'struct TunnelState *'
- * @param sender who sent the message
  * @param message the actual message
- * @param atsi performance data for the connection
+ *
  * @return GNUNET_OK to keep the connection open,
  *         GNUNET_SYSERR to close it (signal serious error)
  */
 static int
 receive_udp_remote (void *cls GNUNET_UNUSED, struct GNUNET_MESH_Tunnel *tunnel,
                     void **tunnel_ctx GNUNET_UNUSED,
-                    const struct GNUNET_PeerIdentity *sender GNUNET_UNUSED,
-                    const struct GNUNET_MessageHeader *message,
-                    const struct GNUNET_ATS_Information *atsi GNUNET_UNUSED)
+                    const struct GNUNET_MessageHeader *message)
 {
   struct TunnelState *state = *tunnel_ctx;
   const struct GNUNET_EXIT_UdpInternetMessage *msg;
@@ -2511,6 +2843,16 @@ receive_udp_remote (void *cls GNUNET_UNUSED, struct GNUNET_MESH_Tunnel *tunnel,
   const void *payload;
   int af;
 
+  if (GNUNET_YES == state->is_dns)
+  {
+    GNUNET_break_op (0);
+    return GNUNET_SYSERR;
+  }
+  if (GNUNET_SYSERR == state->is_dns)
+  {
+    /* tunnel is UDP/TCP from now on */
+    state->is_dns = GNUNET_NO;
+  }
   GNUNET_STATISTICS_update (stats,
                            gettext_noop ("# Bytes received from MESH"),
                            pkt_len, GNUNET_NO);
@@ -2523,9 +2865,9 @@ receive_udp_remote (void *cls GNUNET_UNUSED, struct GNUNET_MESH_Tunnel *tunnel,
     return GNUNET_SYSERR;
   }
   msg = (const struct GNUNET_EXIT_UdpInternetMessage*) message;
-  pkt_len -= sizeof (struct GNUNET_EXIT_UdpInternetMessage);  
+  pkt_len -= sizeof (struct GNUNET_EXIT_UdpInternetMessage);
   af = (int) ntohl (msg->af);
-  state->ri.remote_address.af = af;
+  state->specifics.tcp_udp.ri.remote_address.af = af;
   switch (af)
   {
   case AF_INET:
@@ -2542,7 +2884,7 @@ receive_udp_remote (void *cls GNUNET_UNUSED, struct GNUNET_MESH_Tunnel *tunnel,
     v4 = (const struct in_addr*) &msg[1];
     payload = &v4[1];
     pkt_len -= sizeof (struct in_addr);
-    state->ri.remote_address.address.ipv4 = *v4;
+    state->specifics.tcp_udp.ri.remote_address.address.ipv4 = *v4;
     break;
   case AF_INET6:
     if (pkt_len < sizeof (struct in6_addr))
@@ -2558,7 +2900,7 @@ receive_udp_remote (void *cls GNUNET_UNUSED, struct GNUNET_MESH_Tunnel *tunnel,
     v6 = (const struct in6_addr*) &msg[1];
     payload = &v6[1];
     pkt_len -= sizeof (struct in6_addr);
-    state->ri.remote_address.address.ipv6 = *v6;
+    state->specifics.tcp_udp.ri.remote_address.address.ipv6 = *v6;
     break;
   default:
     GNUNET_break_op (0);
@@ -2568,20 +2910,20 @@ receive_udp_remote (void *cls GNUNET_UNUSED, struct GNUNET_MESH_Tunnel *tunnel,
     char buf[INET6_ADDRSTRLEN];
     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                "Received data from %s for forwarding to UDP %s:%u\n",
-               GNUNET_i2s (sender),
-               inet_ntop (af, 
-                          &state->ri.remote_address.address,
+               GNUNET_i2s (&state->peer),
+               inet_ntop (af,
+                          &state->specifics.tcp_udp.ri.remote_address.address,
                           buf, sizeof (buf)),
-               (unsigned int) ntohs (msg->destination_port));  
+               (unsigned int) ntohs (msg->destination_port));
   }
-  state->ri.remote_address.proto = IPPROTO_UDP;
-  state->ri.remote_address.port = msg->destination_port;
-  if (NULL == state->heap_node)
+  state->specifics.tcp_udp.ri.remote_address.proto = IPPROTO_UDP;
+  state->specifics.tcp_udp.ri.remote_address.port = msg->destination_port;
+  if (NULL == state->specifics.tcp_udp.heap_node)
     setup_state_record (state);
   if (0 != ntohs (msg->source_port))
-    state->ri.local_address.port = msg->source_port;
-  send_udp_packet_via_tun (&state->ri.remote_address,
-                          &state->ri.local_address,
+    state->specifics.tcp_udp.ri.local_address.port = msg->source_port;
+  send_udp_packet_via_tun (&state->specifics.tcp_udp.ri.remote_address,
+                          &state->specifics.tcp_udp.ri.local_address,
                           payload, pkt_len);
   return GNUNET_YES;
 }
@@ -2594,23 +2936,30 @@ receive_udp_remote (void *cls GNUNET_UNUSED, struct GNUNET_MESH_Tunnel *tunnel,
  * @param cls closure, NULL
  * @param tunnel connection to the other end
  * @param tunnel_ctx pointer to our 'struct TunnelState *'
- * @param sender who sent the message
  * @param message the actual message
- * @param atsi performance data for the connection
+ *
  * @return GNUNET_OK to keep the connection open,
  *         GNUNET_SYSERR to close it (signal serious error)
  */
 static int
 receive_udp_service (void *cls GNUNET_UNUSED, struct GNUNET_MESH_Tunnel *tunnel,
                      void **tunnel_ctx,
-                     const struct GNUNET_PeerIdentity *sender GNUNET_UNUSED,
-                     const struct GNUNET_MessageHeader *message,
-                     const struct GNUNET_ATS_Information *atsi GNUNET_UNUSED)
+                     const struct GNUNET_MessageHeader *message)
 {
   struct TunnelState *state = *tunnel_ctx;
   const struct GNUNET_EXIT_UdpServiceMessage *msg;
   uint16_t pkt_len = ntohs (message->size);
 
+  if (GNUNET_YES == state->is_dns)
+  {
+    GNUNET_break_op (0);
+    return GNUNET_SYSERR;
+  }
+  if (GNUNET_SYSERR == state->is_dns)
+  {
+    /* tunnel is UDP/TCP from now on */
+    state->is_dns = GNUNET_NO;
+  }
   GNUNET_STATISTICS_update (stats,
                            gettext_noop ("# Bytes received from MESH"),
                            pkt_len, GNUNET_NO);
@@ -2625,15 +2974,15 @@ receive_udp_service (void *cls GNUNET_UNUSED, struct GNUNET_MESH_Tunnel *tunnel,
   }
   msg = (const struct GNUNET_EXIT_UdpServiceMessage*) message;
   pkt_len -= sizeof (struct GNUNET_EXIT_UdpServiceMessage);
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-             "Received data from %s for forwarding to UDP service %s on port %u\n",
-             GNUNET_i2s (sender),
-             GNUNET_h2s (&msg->service_descriptor),
-             (unsigned int) ntohs (msg->destination_port));  
-  if (NULL == (state->serv = find_service (udp_services, &msg->service_descriptor, 
-                                          ntohs (msg->destination_port))))
-  {
-    GNUNET_log (GNUNET_ERROR_TYPE_INFO, 
+  LOG (GNUNET_ERROR_TYPE_DEBUG,
+       "Received data from %s for forwarding to UDP service %s on port %u\n",
+       GNUNET_i2s (&state->peer),
+       GNUNET_h2s (&msg->service_descriptor),
+       (unsigned int) ntohs (msg->destination_port));
+  if (NULL == (state->specifics.tcp_udp.serv = find_service (udp_services, &msg->service_descriptor,
+                                                            ntohs (msg->destination_port))))
+  {
+    GNUNET_log (GNUNET_ERROR_TYPE_INFO,
                _("No service found for %s on port %d!\n"),
                "UDP",
                ntohs (msg->destination_port));
@@ -2642,12 +2991,12 @@ receive_udp_service (void *cls GNUNET_UNUSED, struct GNUNET_MESH_Tunnel *tunnel,
                              1, GNUNET_NO);
     return GNUNET_SYSERR;
   }
-  state->ri.remote_address = state->serv->address;    
+  state->specifics.tcp_udp.ri.remote_address = state->specifics.tcp_udp.serv->address;
   setup_state_record (state);
   if (0 != ntohs (msg->source_port))
-    state->ri.local_address.port = msg->source_port;
-  send_udp_packet_via_tun (&state->ri.remote_address,
-                          &state->ri.local_address,
+    state->specifics.tcp_udp.ri.local_address.port = msg->source_port;
+  send_udp_packet_via_tun (&state->specifics.tcp_udp.ri.remote_address,
+                          &state->specifics.tcp_udp.ri.local_address,
                           &msg[1], pkt_len);
   return GNUNET_YES;
 }
@@ -2659,16 +3008,17 @@ receive_udp_service (void *cls GNUNET_UNUSED, struct GNUNET_MESH_Tunnel *tunnel,
  * @param cls closure
  * @param tunnel new handle to the tunnel
  * @param initiator peer that started the tunnel
- * @param atsi performance information for the tunnel
+ * @param port destination port
  * @return initial tunnel context for the 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 *atsi GNUNET_UNUSED)
+            const struct GNUNET_PeerIdentity *initiator, uint32_t port)
 {
-  struct TunnelState *s = GNUNET_malloc (sizeof (struct TunnelState));
+  struct TunnelState *s = GNUNET_new (struct TunnelState);
 
+  s->is_dns = GNUNET_SYSERR;
+  s->peer = *initiator;
   GNUNET_STATISTICS_update (stats,
                            gettext_noop ("# Inbound MESH tunnels created"),
                            1, GNUNET_NO);
@@ -2696,23 +3046,37 @@ clean_tunnel (void *cls GNUNET_UNUSED, const struct GNUNET_MESH_Tunnel *tunnel,
   struct TunnelState *s = tunnel_ctx;
   struct TunnelMessageQueue *tnq;
 
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-             "Tunnel destroyed\n");
-  while (NULL != (tnq = s->head))
+  LOG (GNUNET_ERROR_TYPE_DEBUG,
+       "Tunnel destroyed\n");
+  if (GNUNET_SYSERR == s->is_dns)
   {
-    GNUNET_CONTAINER_DLL_remove (s->head,
-                                s->tail,
-                                tnq);
-    GNUNET_free (tnq);
+    GNUNET_free (s);
+    return;
   }
-  if (s->heap_node != NULL)
+  if (GNUNET_YES == s->is_dns)
   {
-    GNUNET_assert (GNUNET_YES ==
-                  GNUNET_CONTAINER_multihashmap_remove (connections_map,
-                                                        &s->state_key,
-                                                        s));
-    GNUNET_CONTAINER_heap_remove_node (s->heap_node);
-    s->heap_node = NULL;
+    if (tunnels[s->specifics.dns.my_id] == s)
+      tunnels[s->specifics.dns.my_id] = NULL;
+    GNUNET_free_non_null (s->specifics.dns.reply);
+  }
+  else
+  {
+    while (NULL != (tnq = s->specifics.tcp_udp.head))
+    {
+      GNUNET_CONTAINER_DLL_remove (s->specifics.tcp_udp.head,
+                                  s->specifics.tcp_udp.tail,
+                                  tnq);
+      GNUNET_free (tnq);
+    }
+    if (NULL != s->specifics.tcp_udp.heap_node)
+    {
+      GNUNET_assert (GNUNET_YES ==
+                    GNUNET_CONTAINER_multihashmap_remove (connections_map,
+                                                          &s->specifics.tcp_udp.state_key,
+                                                          s));
+      GNUNET_CONTAINER_heap_remove_node (s->specifics.tcp_udp.heap_node);
+      s->specifics.tcp_udp.heap_node = NULL;
+    }
   }
   if (NULL != s->th)
   {
@@ -2732,7 +3096,7 @@ clean_tunnel (void *cls GNUNET_UNUSED, const struct GNUNET_MESH_Tunnel *tunnel,
  */
 static int
 free_iterate (void *cls GNUNET_UNUSED,
-              const GNUNET_HashCode * hash GNUNET_UNUSED, void *value)
+              const struct GNUNET_HashCode * hash GNUNET_UNUSED, void *value)
 {
   GNUNET_free (value);
   return GNUNET_YES;
@@ -2750,12 +3114,23 @@ cleanup (void *cls GNUNET_UNUSED,
 
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
              "Exit service is shutting down now\n");
-  if (helper_handle != NULL)
+
+  if (NULL != helper_handle)
   {
-    GNUNET_HELPER_stop (helper_handle);
+    GNUNET_HELPER_stop (helper_handle, GNUNET_NO);
     helper_handle = NULL;
   }
-  if (mesh_handle != NULL)
+  if (NULL != regex4)
+  {
+    GNUNET_REGEX_announce_cancel (regex4);
+    regex4 = NULL;
+  }
+  if (NULL != regex6)
+  {
+    GNUNET_REGEX_announce_cancel (regex6);
+    regex6 = NULL;
+  }
+  if (NULL != mesh_handle)
   {
     GNUNET_MESH_disconnect (mesh_handle);
     mesh_handle = NULL;
@@ -2783,7 +3158,32 @@ cleanup (void *cls GNUNET_UNUSED,
     GNUNET_CONTAINER_multihashmap_destroy (udp_services);
     udp_services = NULL;
   }
-  if (stats != NULL)
+  if (NULL != dnsstub)
+  {
+    GNUNET_DNSSTUB_stop (dnsstub);
+    dnsstub = NULL;
+  }
+  if (NULL != peer_key)
+  {
+    GNUNET_free (peer_key);
+    peer_key = NULL;
+  }
+  if (GNUNET_SCHEDULER_NO_TASK != dht_task)
+  {
+    GNUNET_SCHEDULER_cancel (dht_task);
+    dht_task = GNUNET_SCHEDULER_NO_TASK;
+  }
+  if (NULL != dht_put)
+  {
+    GNUNET_DHT_put_cancel (dht_put);
+    dht_put = NULL;
+  }
+  if (NULL != dht)
+  {
+    GNUNET_DHT_disconnect (dht);
+    dht = NULL;
+  }
+  if (NULL != stats)
   {
     GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
     stats = NULL;
@@ -2833,10 +3233,10 @@ add_services (int proto,
     }
     hostport[0] = '\0';
     hostport++;
-    
+
     int local_port = atoi (redirect);
     int remote_port = atoi (hostport);
-    
+
     if (!((local_port > 0) && (local_port < 65536)))
     {
       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
@@ -2852,29 +3252,30 @@ add_services (int proto,
       continue;
     }
 
-    serv = GNUNET_malloc (sizeof (struct LocalService));
+    serv = GNUNET_new (struct LocalService);
+    serv->address.proto = proto;
     serv->my_port = (uint16_t) local_port;
     serv->address.port = remote_port;
     if (0 == strcmp ("localhost4", hostname))
     {
-      const char *ip4addr = exit_argv[4];
+      const char *ip4addr = exit_argv[5];
 
-      serv->address.af = AF_INET;      
-      GNUNET_assert (1 != inet_pton (AF_INET, ip4addr, &serv->address.address.ipv4));
+      serv->address.af = AF_INET;
+      GNUNET_assert (1 == inet_pton (AF_INET, ip4addr, &serv->address.address.ipv4));
     }
     else if (0 == strcmp ("localhost6", hostname))
     {
-      const char *ip6addr = exit_argv[2];
+      const char *ip6addr = exit_argv[3];
 
       serv->address.af = AF_INET6;
       GNUNET_assert (1 == inet_pton (AF_INET6, ip6addr, &serv->address.address.ipv6));
     }
     else
     {
-      struct addrinfo *res;      
+      struct addrinfo *res;
       int ret;
 
-      ret = getaddrinfo (hostname, NULL, NULL, &res);      
+      ret = getaddrinfo (hostname, NULL, NULL, &res);
       if ( (ret != 0) || (res == NULL) )
       {
        GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
@@ -2884,7 +3285,7 @@ add_services (int proto,
        GNUNET_free (serv);
        continue;
       }
-      
+
       serv->address.af = res->ai_family;
       switch (res->ai_family)
       {
@@ -2963,27 +3364,71 @@ 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
+ * We are running a DNS exit service, advertise it in the
+ * DHT.  This task is run periodically to do the DHT PUT.
+ *
+ * @param cls closure
+ * @param tc scheduler context
  */
-static int
-test_af (int af)
+static void
+do_dht_put (void *cls,
+           const struct GNUNET_SCHEDULER_TaskContext *tc);
+
+
+/**
+ * Function called when the DHT PUT operation is complete.
+ * Schedules the next PUT.
+ *
+ * @param cls closure, NULL
+ * @param success #GNUNET_OK if the operation worked (unused)
+ */
+static void
+dht_put_cont (void *cls,
+             int success)
+{
+  dht_put = NULL;
+  dht_task = GNUNET_SCHEDULER_add_delayed (DHT_PUT_FREQUENCY,
+                                          &do_dht_put,
+                                          NULL);
+}
+
+
+/**
+ * We are running a DNS exit service, advertise it in the
+ * DHT.  This task is run periodically to do the DHT PUT.
+ *
+ * @param cls closure
+ * @param tc scheduler context
+ */
+static void
+do_dht_put (void *cls,
+           const struct GNUNET_SCHEDULER_TaskContext *tc)
 {
-  int s;
+  struct GNUNET_TIME_Absolute expiration;
 
-  s = socket (af, SOCK_STREAM, 0);
-  if (-1 == s)
+  dht_task = GNUNET_SCHEDULER_NO_TASK;
+  expiration = GNUNET_TIME_absolute_ntoh (dns_advertisement.expiration_time);
+  if (GNUNET_TIME_absolute_get_remaining (expiration).rel_value_us <
+      GNUNET_TIME_UNIT_HOURS.rel_value_us)
   {
-    if (EAFNOSUPPORT == errno)
-      return GNUNET_NO;
-    GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR,
-                        "socket");
-    return GNUNET_SYSERR;
-  }
-  close (s);
-  return GNUNET_OK;
+    /* refresh advertisement */
+    expiration = GNUNET_TIME_relative_to_absolute (DNS_ADVERTISEMENT_TIMEOUT);
+    dns_advertisement.expiration_time = GNUNET_TIME_absolute_hton (expiration);
+    GNUNET_assert (GNUNET_OK ==
+                  GNUNET_CRYPTO_ecc_sign (peer_key,
+                                          &dns_advertisement.purpose,
+                                          &dns_advertisement.signature));
+  }
+  dht_put = GNUNET_DHT_put (dht,
+                           &dht_put_key,
+                           1 /* replication */,
+                           GNUNET_DHT_RO_NONE,
+                           GNUNET_BLOCK_TYPE_DNS,
+                           sizeof (struct GNUNET_DNS_Advertisement),
+                           &dns_advertisement,
+                           expiration,
+                           GNUNET_TIME_UNIT_FOREVER_REL,
+                           &dht_put_cont, NULL);
 }
 
 
@@ -3008,10 +3453,12 @@ run (void *cls, char *const *args GNUNET_UNUSED,
     {&receive_tcp_service, GNUNET_MESSAGE_TYPE_VPN_TCP_TO_SERVICE_START, 0},
     {&receive_tcp_remote, GNUNET_MESSAGE_TYPE_VPN_TCP_TO_INTERNET_START, 0},
     {&receive_tcp_data, GNUNET_MESSAGE_TYPE_VPN_TCP_DATA_TO_EXIT, 0},
+    {&receive_dns_request, GNUNET_MESSAGE_TYPE_VPN_DNS_TO_INTERNET, 0},
     {NULL, 0, 0}
   };
 
-  static GNUNET_MESH_ApplicationType apptypes[] = {
+  static uint32_t apptypes[] = {
+    GNUNET_APPLICATION_TYPE_END,
     GNUNET_APPLICATION_TYPE_END,
     GNUNET_APPLICATION_TYPE_END,
     GNUNET_APPLICATION_TYPE_END
@@ -3019,29 +3466,42 @@ run (void *cls, char *const *args GNUNET_UNUSED,
   unsigned int app_idx;
   char *exit_ifname;
   char *tun_ifname;
+  char *policy;
   char *ipv6addr;
   char *ipv6prefix_s;
   char *ipv4addr;
   char *ipv4mask;
+  char *binary;
+  char *regex;
+  char *prefixed_regex;
+  struct in_addr dns_exit4;
+  struct in6_addr dns_exit6;
+  char *dns_exit;
 
-  if (GNUNET_YES !=
-      GNUNET_OS_check_helper_binary ("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;
-  }
   cfg = cfg_;
-  stats = GNUNET_STATISTICS_create ("exit", cfg);
   ipv4_exit = GNUNET_CONFIGURATION_get_value_yesno (cfg, "exit", "EXIT_IPV4");
-  ipv6_exit = GNUNET_CONFIGURATION_get_value_yesno (cfg, "exit", "EXIT_IPV6"); 
+  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"); 
+  ipv6_enabled = GNUNET_CONFIGURATION_get_value_yesno (cfg, "exit", "ENABLE_IPV6");
+  if ( (ipv4_exit) || (ipv6_exit) )
+  {
+    binary = GNUNET_OS_get_libexec_binary_path ("gnunet-helper-exit");
+    if (GNUNET_YES !=
+       GNUNET_OS_check_helper_binary (binary, GNUNET_YES, "-d gnunet-vpn - - - 169.1.3.3.7 255.255.255.0")) //no nat, ipv4 only
+    {
+      GNUNET_free (binary);
+      GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+                 _("`%s' must be installed SUID, refusing to run\n"),
+                 "gnunet-helper-exit");
+      global_ret = 1;
+      return;
+    }
+    GNUNET_free (binary);
+  }
+  stats = GNUNET_STATISTICS_create ("exit", cfg);
 
   if ( (ipv4_exit || ipv4_enabled) &&
-       GNUNET_OK != test_af (AF_INET))
+       GNUNET_OK != GNUNET_NETWORK_test_pf (PF_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"));
@@ -3049,7 +3509,7 @@ run (void *cls, char *const *args GNUNET_UNUSED,
     ipv4_enabled = GNUNET_NO;
   }
   if ( (ipv6_exit || ipv6_enabled) &&
-       GNUNET_OK != test_af (AF_INET6))
+       GNUNET_OK != GNUNET_NETWORK_test_pf (PF_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"));
@@ -3073,19 +3533,59 @@ run (void *cls, char *const *args GNUNET_UNUSED,
     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                _("No useful service enabled.  Exiting.\n"));
     GNUNET_SCHEDULER_shutdown ();
-    return;    
+    return;
+  }
+
+  dns_exit = NULL;
+  if ( (GNUNET_YES ==
+       GNUNET_CONFIGURATION_get_value_yesno (cfg_, "exit", "ENABLE_DNS")) &&
+       ( (GNUNET_OK !=
+         GNUNET_CONFIGURATION_get_value_string (cfg, "exit",
+                                                "DNS_RESOLVER",
+                                                &dns_exit)) ||
+        ( (1 != inet_pton (AF_INET, dns_exit, &dns_exit4)) &&
+          (1 != inet_pton (AF_INET6, dns_exit, &dns_exit6)) ) ) )
+  {
+    GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
+                              "dns", "DNS_RESOLVER",
+                              _("need a valid IPv4 or IPv6 address\n"));
+    GNUNET_free_non_null (dns_exit);
+    dns_exit = NULL;
   }
+  if (NULL != dns_exit)
+    dnsstub = GNUNET_DNSSTUB_start (dns_exit);
+
+
   app_idx = 0;
-  if (GNUNET_YES == ipv4_exit)    
+  if (GNUNET_YES == ipv4_exit)
   {
+    // FIXME use regex to put info
     apptypes[app_idx] = GNUNET_APPLICATION_TYPE_IPV4_GATEWAY;
     app_idx++;
   }
-  if (GNUNET_YES == ipv6_exit)    
+  if (GNUNET_YES == ipv6_exit)
   {
+    // FIXME use regex to put info
     apptypes[app_idx] = GNUNET_APPLICATION_TYPE_IPV6_GATEWAY;
     app_idx++;
   }
+  if (NULL != dns_exit)
+  {
+    dht = GNUNET_DHT_connect (cfg, 1);
+    peer_key = GNUNET_CRYPTO_ecc_key_create_from_configuration (cfg);
+    GNUNET_CRYPTO_ecc_key_get_public_for_signature (peer_key,
+                                                   &dns_advertisement.peer.public_key);
+    dns_advertisement.purpose.size = htonl (sizeof (struct GNUNET_DNS_Advertisement) -
+                                           sizeof (struct GNUNET_CRYPTO_EccSignature));
+    dns_advertisement.purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_DNS_RECORD);
+    GNUNET_CRYPTO_hash ("dns",
+                       strlen ("dns"),
+                       &dht_put_key);
+    dht_task = GNUNET_SCHEDULER_add_now (&do_dht_put,
+                                        NULL);
+    apptypes[app_idx] = GNUNET_APPLICATION_TYPE_INTERNET_RESOLVER;
+    app_idx++;
+  }
 
   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &cleanup, cls);
 
@@ -3097,8 +3597,7 @@ run (void *cls, char *const *args GNUNET_UNUSED,
   if (GNUNET_SYSERR ==
       GNUNET_CONFIGURATION_get_value_string (cfg, "exit", "TUN_IFNAME", &tun_ifname))
   {
-    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
-                "No entry 'TUN_IFNAME' in configuration!\n");
+    GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, "EXIT", "TUN_IFNAME");
     GNUNET_SCHEDULER_shutdown ();
     return;
   }
@@ -3108,8 +3607,7 @@ run (void *cls, char *const *args GNUNET_UNUSED,
     if (GNUNET_SYSERR ==
        GNUNET_CONFIGURATION_get_value_string (cfg, "exit", "EXIT_IFNAME", &exit_ifname))
     {
-      GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
-                 "No entry 'EXIT_IFNAME' in configuration!\n");
+      GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, "EXIT", "EXIT_IFNAME");
       GNUNET_SCHEDULER_shutdown ();
       return;
     }
@@ -3117,9 +3615,9 @@ run (void *cls, char *const *args GNUNET_UNUSED,
   }
   else
   {
-    exit_argv[2] = GNUNET_strdup ("%");
+    exit_argv[2] = GNUNET_strdup ("-");
   }
-  
+
 
   if (GNUNET_YES == ipv6_enabled)
   {
@@ -3128,8 +3626,7 @@ run (void *cls, char *const *args GNUNET_UNUSED,
                                                 &ipv6addr) ||
          (1 != inet_pton (AF_INET6, ipv6addr, &exit_ipv6addr))) )
     {
-      GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
-                 "No valid entry 'IPV6ADDR' in configuration!\n");
+      GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, "EXIT", "IPV6ADDR");
       GNUNET_SCHEDULER_shutdown ();
       return;
     }
@@ -3138,8 +3635,7 @@ run (void *cls, char *const *args GNUNET_UNUSED,
        GNUNET_CONFIGURATION_get_value_string (cfg, "exit", "IPV6PREFIX",
                                               &ipv6prefix_s))
     {
-      GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
-                 "No entry 'IPV6PREFIX' in configuration!\n");
+      GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, "EXIT", "IPV6PREFIX");
       GNUNET_SCHEDULER_shutdown ();
       return;
     }
@@ -3150,10 +3646,12 @@ run (void *cls, char *const *args GNUNET_UNUSED,
                                                 &ipv6prefix)) ||
         (ipv6prefix >= 127) )
     {
+      GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR, "EXIT", "IPV6PREFIX",
+                                _("Must be a number"));
       GNUNET_SCHEDULER_shutdown ();
       return;
     }
-  } 
+  }
   else
   {
     /* IPv6 explicitly disabled */
@@ -3167,8 +3665,7 @@ run (void *cls, char *const *args GNUNET_UNUSED,
                                                 &ipv4addr) ||
          (1 != inet_pton (AF_INET, ipv4addr, &exit_ipv4addr))) )
       {
-       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
-                   "No valid entry for 'IPV4ADDR' in configuration!\n");
+       GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, "EXIT", "IPV4ADDR");
        GNUNET_SCHEDULER_shutdown ();
        return;
       }
@@ -3178,8 +3675,7 @@ run (void *cls, char *const *args GNUNET_UNUSED,
                                                 &ipv4mask) ||
          (1 != inet_pton (AF_INET, ipv4mask, &exit_ipv4mask))) )
     {
-      GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
-                 "No valid entry 'IPV4MASK' in configuration!\n");
+      GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, "EXIT", "IPV4MASK");
       GNUNET_SCHEDULER_shutdown ();
       return;
     }
@@ -3193,25 +3689,81 @@ run (void *cls, char *const *args GNUNET_UNUSED,
   }
   exit_argv[7] = NULL;
 
-  udp_services = GNUNET_CONTAINER_multihashmap_create (65536);
-  tcp_services = GNUNET_CONTAINER_multihashmap_create (65536);
+  udp_services = GNUNET_CONTAINER_multihashmap_create (65536, GNUNET_NO);
+  tcp_services = GNUNET_CONTAINER_multihashmap_create (65536, GNUNET_NO);
   GNUNET_CONFIGURATION_iterate_sections (cfg, &read_service_conf, NULL);
 
-  connections_map = GNUNET_CONTAINER_multihashmap_create (65536);
+  connections_map = GNUNET_CONTAINER_multihashmap_create (65536, GNUNET_NO);
   connections_heap = GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
-  mesh_handle 
-    = GNUNET_MESH_connect (cfg, 42 /* queue size */, NULL, 
-                          &new_tunnel, 
+  mesh_handle
+    = GNUNET_MESH_connect (cfg, NULL,
+                          &new_tunnel,
                           &clean_tunnel, handlers,
-                           apptypes);
+                           apptypes); // FIXME use ports
   if (NULL == mesh_handle)
   {
     GNUNET_SCHEDULER_shutdown ();
     return;
   }
-  helper_handle = GNUNET_HELPER_start ("gnunet-helper-exit", 
-                                      exit_argv,
-                                      &message_token, NULL);
+
+  /* Mesh handle acquired, now announce regular expressions matching our exit */
+  if ( (GNUNET_YES == ipv4_enabled) && (GNUNET_YES == ipv4_exit) )
+  {
+    policy = NULL;
+    if (GNUNET_OK !=
+       GNUNET_CONFIGURATION_get_value_string (cfg,
+                                               "exit",
+                                               "EXIT_RANGE_IPV4_REGEX",
+                                               &policy))
+      regex = NULL;
+    else
+      regex = GNUNET_TUN_ipv4policy2regex (policy);
+    GNUNET_free_non_null (policy);
+    if (NULL != regex)
+    {
+      (void) GNUNET_asprintf (&prefixed_regex, "%s%s%s",
+                             GNUNET_APPLICATION_TYPE_EXIT_REGEX_PREFIX,
+                             "4", regex);
+      regex4 = GNUNET_REGEX_announce (cfg,                     
+                                     prefixed_regex,
+                                     REGEX_REFRESH_FREQUENCY,
+                                     REGEX_MAX_PATH_LEN_IPV4);
+      GNUNET_free (regex);
+      GNUNET_free (prefixed_regex);
+    }
+  }
+
+  if (GNUNET_YES == ipv6_enabled && GNUNET_YES == ipv6_exit)
+  {
+    policy = NULL;
+    if (GNUNET_OK !=
+       GNUNET_CONFIGURATION_get_value_string (cfg,
+                                               "exit",
+                                               "EXIT_RANGE_IPV6_REGEX",
+                                               &policy))
+      regex = NULL;
+    else
+      regex = GNUNET_TUN_ipv6policy2regex (policy);
+    GNUNET_free_non_null (policy);
+    if (NULL != regex)
+    {
+      (void) GNUNET_asprintf (&prefixed_regex, "%s%s%s",
+                             GNUNET_APPLICATION_TYPE_EXIT_REGEX_PREFIX,
+                             "6", regex);
+      regex6 = GNUNET_REGEX_announce (cfg,
+                                     prefixed_regex,
+                                     REGEX_REFRESH_FREQUENCY,
+                                     REGEX_MAX_PATH_LEN_IPV6);
+      GNUNET_free (regex);
+      GNUNET_free (prefixed_regex);
+    }
+  }
+  if ((ipv4_exit) || (ipv6_exit))
+    helper_handle = GNUNET_HELPER_start (GNUNET_NO,
+                                        "gnunet-helper-exit",
+                                        exit_argv,
+                                        &message_token,
+                                        NULL, NULL);
 }
 
 
@@ -3229,6 +3781,9 @@ main (int argc, char *const *argv)
     GNUNET_GETOPT_OPTION_END
   };
 
+  if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
+    return 2;
+
   return (GNUNET_OK ==
           GNUNET_PROGRAM_run (argc, argv, "gnunet-daemon-exit",
                               gettext_noop