add function conv param string
[oweals/gnunet.git] / src / vpn / gnunet-service-vpn.c
index 2c14635193290fddc5dc78fc885cd2cfb9709921..8741aa719b3c5351b5865dc844ef79985cdbc6f5 100644 (file)
@@ -1,6 +1,6 @@
 /*
      This file is part of GNUnet.
-     (C) 2010, 2011, 2012 Christian Grothoff
+     Copyright (C) 2010, 2011, 2012 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
 
      You should have received a copy of the GNU General Public License
      along with GNUnet; see the file COPYING.  If not, write to the
-     Free Software Foundation, Inc., 59 Temple Place - Suite 330,
-     Boston, MA 02111-1307, USA.
+     Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+     Boston, MA 02110-1301, USA.
 */
 
 /**
  * @file vpn/gnunet-service-vpn.c
  * @brief service that opens a virtual interface and allows its clients
  *        to allocate IPs on the virtual interface and to then redirect
- *        IP traffic received on those IPs via the GNUnet mesh
+ *        IP traffic received on those IPs via the GNUnet cadet
  * @author Philipp Toelke
  * @author Christian Grothoff
  *
  * TODO:
- * - keep multiple peers/mesh channels ready as alternative exits /
+ * - keep multiple peers/cadet channels ready as alternative exits /
  *   detect & recover from channel-to-exit failure gracefully
  */
 #include "platform.h"
@@ -35,7 +35,7 @@
 #include "gnunet_common.h"
 #include "gnunet_protocols.h"
 #include "gnunet_applications.h"
-#include "gnunet_mesh_service.h"
+#include "gnunet_cadet_service.h"
 #include "gnunet_statistics_service.h"
 #include "gnunet_constants.h"
 #include "gnunet_tun_lib.h"
@@ -45,7 +45,7 @@
 
 
 /**
- * Maximum number of messages we allow in the queue for mesh.
+ * Maximum number of messages we allow in the queue for cadet.
  */
 #define MAX_MESSAGE_QUEUE_SIZE 4
 
@@ -83,11 +83,6 @@ struct DestinationChannel
    */
   struct DestinationEntry *destination;
 
-  /**
-   * Pre-allocated channel for this destination, or NULL for none.
-   */
-  struct ChannelState *ts;
-
   /**
    * Destination port this channel state is used for.
    */
@@ -218,7 +213,7 @@ struct ChannelState
    * Information about the channel to use, NULL if no channel
    * is available right now.
    */
-  struct GNUNET_MESH_Channel *channel;
+  struct GNUNET_CADET_Channel *channel;
 
   /**
    * Active query with REGEX to locate exit.
@@ -228,7 +223,7 @@ struct ChannelState
   /**
    * Active transmission handle, NULL for none.
    */
-  struct GNUNET_MESH_TransmitHandle *th;
+  struct GNUNET_CADET_TransmitHandle *th;
 
   /**
    * Entry for this entry in the channel_heap, NULL as long as this
@@ -246,12 +241,6 @@ struct ChannelState
    */
   struct ChannelMessageQueueEntry *tmq_tail;
 
-  /**
-   * Destination entry that has a pointer to this channel state;
-   * NULL if this channel state is in the channel map.
-   */
-  struct DestinationChannel *destination_container;
-
   /**
    * Destination to which this channel leads.  Note that
    * this struct is NOT in the destination_map (but a
@@ -265,6 +254,12 @@ struct ChannelState
    */
   int af;
 
+  /**
+   * Is this channel new (#GNUNET_NO), or did we exchange messages with the
+   * other side already (#GNUNET_YES)?
+   */
+  int is_established;
+
   /**
    * Length of the doubly linked 'tmq_head/tmq_tail' list.
    */
@@ -324,7 +319,7 @@ struct ChannelState
 
 
 /**
- * Return value from 'main'.
+ * Return value from #main().
  */
 static int global_ret;
 
@@ -334,13 +329,13 @@ static int global_ret;
 static const struct GNUNET_CONFIGURATION_Handle *cfg;
 
 /**
- * Handle to the mesh service.
+ * Handle to the cadet service.
  */
-static struct GNUNET_MESH_Handle *mesh_handle;
+static struct GNUNET_CADET_Handle *cadet_handle;
 
 /**
  * Map from IP address to destination information (possibly with a
- * MESH channel handle for fast setup).
+ * CADET channel handle for fast setup).
  */
 static struct GNUNET_CONTAINER_MultiHashMap *destination_map;
 
@@ -351,7 +346,7 @@ static struct GNUNET_CONTAINER_Heap *destination_heap;
 
 /**
  * Map from source and destination address (IP+port) to connection
- * information (mostly with the respective MESH channel handle).
+ * information (mostly with the respective CADET channel handle).
  */
 static struct GNUNET_CONTAINER_MultiHashMap *channel_map;
 
@@ -445,12 +440,12 @@ get_destination_key_from_ip (int af,
  */
 static void
 get_channel_key_from_ips (int af,
-                        uint8_t protocol,
-                        const void *source_ip,
-                        uint16_t source_port,
-                        const void *destination_ip,
-                        uint16_t destination_port,
-                        struct GNUNET_HashCode *key)
+                          uint8_t protocol,
+                          const void *source_ip,
+                          uint16_t source_port,
+                          const void *destination_ip,
+                          uint16_t destination_port,
+                          struct GNUNET_HashCode *key)
 {
   char *off;
 
@@ -542,10 +537,21 @@ free_channel_state (struct ChannelState *ts)
 {
   struct GNUNET_HashCode key;
   struct ChannelMessageQueueEntry *tnq;
-  struct GNUNET_MESH_Channel *channel;
+  struct GNUNET_CADET_Channel *channel;
 
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
              "Cleaning up channel state\n");
+  if (NULL != ts->th)
+  {
+    GNUNET_CADET_notify_transmit_ready_cancel (ts->th);
+    ts->th = NULL;
+  }
+  if (NULL != (channel = ts->channel))
+  {
+    ts->channel = NULL;
+    GNUNET_CADET_channel_destroy (channel);
+    return;
+  }
   GNUNET_STATISTICS_update (stats,
                            gettext_noop ("# Active channels"),
                            -1, GNUNET_NO);
@@ -558,17 +564,7 @@ free_channel_state (struct ChannelState *ts)
     GNUNET_free (tnq);
   }
   GNUNET_assert (0 == ts->tmq_length);
-  if (NULL != ts->th)
-  {
-    GNUNET_MESH_notify_transmit_ready_cancel (ts->th);
-    ts->th = NULL;
-  }
   GNUNET_assert (NULL == ts->destination.heap_node);
-  if (NULL != (channel = ts->channel))
-  {
-    ts->channel = NULL;
-    GNUNET_MESH_channel_destroy (channel);
-  }
   if (NULL != ts->search)
   {
     GNUNET_REGEX_search_cancel (ts->search);
@@ -590,18 +586,12 @@ free_channel_state (struct ChannelState *ts)
                                                         &key,
                                                         ts));
   }
-  if (NULL != ts->destination_container)
-  {
-    GNUNET_assert (ts == ts->destination_container->ts);
-    ts->destination_container->ts = NULL;
-    ts->destination_container = NULL;
-  }
   GNUNET_free (ts);
 }
 
 
 /**
- * Send a message from the message queue via mesh.
+ * Send a message from the message queue via cadet.
  *
  * @param cls the `struct ChannelState` with the message queue
  * @param size number of bytes available in @a buf
@@ -622,8 +612,8 @@ send_to_peer_notify_callback (void *cls, size_t size, void *buf)
   GNUNET_assert (NULL != tnq);
   GNUNET_assert (size >= tnq->len);
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-             "Sending %u bytes via mesh channel\n",
-             tnq->len);
+             "Sending %u bytes via cadet channel\n",
+             (unsigned int) tnq->len);
   GNUNET_CONTAINER_DLL_remove (ts->tmq_head,
                               ts->tmq_tail,
                               tnq);
@@ -632,14 +622,14 @@ send_to_peer_notify_callback (void *cls, size_t size, void *buf)
   ret = tnq->len;
   GNUNET_free (tnq);
   if (NULL != (tnq = ts->tmq_head))
-    ts->th = GNUNET_MESH_notify_transmit_ready (ts->channel,
+    ts->th = GNUNET_CADET_notify_transmit_ready (ts->channel,
                                                GNUNET_NO /* cork */,
                                                GNUNET_TIME_UNIT_FOREVER_REL,
                                                tnq->len,
                                                &send_to_peer_notify_callback,
                                                ts);
   GNUNET_STATISTICS_update (stats,
-                           gettext_noop ("# Bytes given to mesh for transmission"),
+                           gettext_noop ("# Bytes given to cadet for transmission"),
                            ret, GNUNET_NO);
   return ret;
 }
@@ -657,8 +647,8 @@ send_to_channel (struct ChannelMessageQueueEntry *tnq,
                struct ChannelState *ts)
 {
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-             "Queueing %u bytes for transmission via mesh channel\n",
-             tnq->len);
+             "Queueing %u bytes for transmission via cadet channel\n",
+             (unsigned int) tnq->len);
   GNUNET_assert (NULL != ts->channel);
   GNUNET_CONTAINER_DLL_insert_tail (ts->tmq_head,
                                    ts->tmq_tail,
@@ -674,16 +664,16 @@ send_to_channel (struct ChannelMessageQueueEntry *tnq,
                                 ts->tmq_tail,
                                 dq);
     ts->tmq_length--;
-    GNUNET_MESH_notify_transmit_ready_cancel (ts->th);
+    GNUNET_CADET_notify_transmit_ready_cancel (ts->th);
     ts->th = NULL;
     GNUNET_STATISTICS_update (stats,
-                             gettext_noop ("# Bytes dropped in mesh queue (overflow)"),
+                             gettext_noop ("# Bytes dropped in cadet queue (overflow)"),
                              dq->len,
                              GNUNET_NO);
     GNUNET_free (dq);
   }
   if (NULL == ts->th)
-    ts->th = GNUNET_MESH_notify_transmit_ready (ts->channel,
+    ts->th = GNUNET_CADET_notify_transmit_ready (ts->channel,
                                                GNUNET_NO /* cork */,
                                                GNUNET_TIME_UNIT_FOREVER_REL,
                                                tnq->len,
@@ -692,10 +682,40 @@ send_to_channel (struct ChannelMessageQueueEntry *tnq,
 }
 
 
+/**
+ * Output destination of a channel for diagnostics.
+ *
+ * @param de destination to process
+ * @return diagnostic string describing destination
+ */
+static const char *
+print_channel_destination (const struct DestinationEntry *de)
+{
+  static char dest[256];
+
+  if (de->is_service)
+  {
+    GNUNET_snprintf (dest,
+                     sizeof (dest),
+                     "HS: %s-%s",
+                     GNUNET_i2s (&de->details.service_destination.target),
+                     GNUNET_h2s (&de->details.service_destination.service_descriptor));
+  }
+  else
+  {
+    inet_ntop (de->details.exit_destination.af,
+               &de->details.exit_destination.ip,
+               dest,
+               sizeof (dest));
+  }
+  return dest;
+}
+
+
 /**
  * Regex has found a potential exit peer for us; consider using it.
  *
- * @param cls the 'struct ChannelState'
+ * @param cls the `struct ChannelState`
  * @param id Peer providing a regex that matches the string.
  * @param get_path Path of the get request.
  * @param get_path_length Lenght of @a get_path.
@@ -713,6 +733,10 @@ handle_regex_result (void *cls,
   struct ChannelState *ts = cls;
   unsigned int apptype;
 
+  GNUNET_log (GNUNET_ERROR_TYPE_INFO,
+              "Exit %s found for destination %s!\n",
+              GNUNET_i2s (id),
+              print_channel_destination (&ts->destination));
   GNUNET_REGEX_search_cancel (ts->search);
   ts->search = NULL;
   switch (ts->af)
@@ -727,16 +751,20 @@ handle_regex_result (void *cls,
     GNUNET_break (0);
     return;
   }
-  ts->channel = GNUNET_MESH_channel_create (mesh_handle,
-                                          ts,
-                                          id,
-                                          apptype,
-                                          GNUNET_MESH_OPTION_DEFAULT);
+  GNUNET_log (GNUNET_ERROR_TYPE_INFO,
+              "Creating tunnel to %s for destination %s!\n",
+              GNUNET_i2s (id),
+              print_channel_destination (&ts->destination));
+  ts->channel = GNUNET_CADET_channel_create (cadet_handle,
+                                             ts,
+                                             id,
+                                             apptype,
+                                             GNUNET_CADET_OPTION_DEFAULT);
 }
 
 
 /**
- * Initialize the given destination entry's mesh channel.
+ * Initialize the given destination entry's cadet channel.
  *
  * @param dt destination channel for which we need to setup a channel
  * @param client_af address family of the address returned to the client
@@ -744,15 +772,14 @@ handle_regex_result (void *cls,
  */
 static struct ChannelState *
 create_channel_to_destination (struct DestinationChannel *dt,
-                             int client_af)
+                               int client_af)
 {
   struct ChannelState *ts;
   unsigned int apptype;
 
   GNUNET_STATISTICS_update (stats,
-                           gettext_noop ("# Mesh channels created"),
+                           gettext_noop ("# Cadet channels created"),
                            1, GNUNET_NO);
-  GNUNET_assert (NULL == dt->ts);
   switch (client_af)
   {
   case AF_INET:
@@ -769,24 +796,23 @@ create_channel_to_destination (struct DestinationChannel *dt,
   ts->af = client_af;
   ts->destination = *dt->destination;
   ts->destination.heap_node = NULL; /* copy is NOT in destination heap */
-  dt->ts = ts;
-  ts->destination_container = dt; /* we are referenced from dt */
   if (dt->destination->is_service)
   {
-    ts->channel = GNUNET_MESH_channel_create (mesh_handle,
-                                           ts,
-                                           &dt->destination->details.service_destination.target,
-                                           apptype,
-                                           GNUNET_MESH_OPTION_DEFAULT);
+    ts->channel = GNUNET_CADET_channel_create (cadet_handle,
+                                               ts,
+                                               &dt->destination->details.service_destination.target,
+                                               apptype,
+                                               GNUNET_CADET_OPTION_DEFAULT);
     if (NULL == ts->channel)
     {
       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
-                 _("Failed to setup mesh channel!\n"));
+                 _("Failed to setup cadet channel!\n"));
       GNUNET_free (ts);
       return NULL;
     }
     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-               "Creating channel to peer %s offering service %s\n",
+               "Creating channel %p to peer %s offering service %s\n",
+                ts->channel,
                GNUNET_i2s (&dt->destination->details.service_destination.target),
                GNUNET_h2s (&dt->destination->details.service_destination.service_descriptor));
   }
@@ -803,7 +829,8 @@ create_channel_to_destination (struct DestinationChannel *dt,
       GNUNET_TUN_ipv4toregexsearch (&dt->destination->details.exit_destination.ip.v4,
                                    dt->destination_port,
                                    address);
-      GNUNET_asprintf (&policy, "%s%s",
+      GNUNET_asprintf (&policy,
+                       "%s%s",
                        GNUNET_APPLICATION_TYPE_EXIT_REGEX_PREFIX,
                        address);
       break;
@@ -815,7 +842,8 @@ create_channel_to_destination (struct DestinationChannel *dt,
       GNUNET_TUN_ipv6toregexsearch (&dt->destination->details.exit_destination.ip.v6,
                                    dt->destination_port,
                                     address);
-      GNUNET_asprintf (&policy, "%s%s",
+      GNUNET_asprintf (&policy,
+                       "%s%s",
                        GNUNET_APPLICATION_TYPE_EXIT_REGEX_PREFIX,
                        address);
       break;
@@ -852,12 +880,15 @@ expire_channel (struct ChannelState *except)
   GNUNET_assert (NULL != ts);
   if (except == ts)
     return; /* can't do this */
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+              "Tearing down expired channel to %s\n",
+              print_channel_destination (&except->destination));
   free_channel_state (ts);
 }
 
 
 /**
- * Route a packet via mesh to the given destination.
+ * Route a packet via cadet to the given destination.
  *
  * @param destination description of the destination
  * @param af address family on this end (AF_INET or AF_INET6)
@@ -881,7 +912,6 @@ route_packet (struct DestinationEntry *destination,
   struct ChannelMessageQueueEntry *tnq;
   size_t alen;
   size_t mlen;
-  int is_new;
   const struct GNUNET_TUN_UdpHeader *udp;
   const struct GNUNET_TUN_TcpHeader *tcp;
   const struct GNUNET_TUN_IcmpHeader *icmp;
@@ -910,12 +940,12 @@ route_packet (struct DestinationEntry *destination,
       source_port = ntohs (udp->source_port);
       destination_port = ntohs (udp->destination_port);
       get_channel_key_from_ips (af,
-                              IPPROTO_UDP,
-                              source_ip,
-                              source_port,
-                              destination_ip,
-                              destination_port,
-                              &key);
+                                IPPROTO_UDP,
+                                source_ip,
+                                source_port,
+                                destination_ip,
+                                destination_port,
+                                &key);
     }
     break;
   case IPPROTO_TCP:
@@ -937,12 +967,12 @@ route_packet (struct DestinationEntry *destination,
       source_port = ntohs (tcp->source_port);
       destination_port = ntohs (tcp->destination_port);
       get_channel_key_from_ips (af,
-                              IPPROTO_TCP,
-                              source_ip,
-                              source_port,
-                              destination_ip,
-                              destination_port,
-                              &key);
+                                IPPROTO_TCP,
+                                source_ip,
+                                source_port,
+                                destination_ip,
+                                destination_port,
+                                &key);
     }
     break;
   case IPPROTO_ICMP:
@@ -965,12 +995,12 @@ route_packet (struct DestinationEntry *destination,
       source_port = 0;
       destination_port = 0;
       get_channel_key_from_ips (af,
-                              protocol,
-                              source_ip,
-                              0,
-                              destination_ip,
-                              0,
-                              &key);
+                                protocol,
+                                source_ip,
+                                0,
+                                destination_ip,
+                                0,
+                                &key);
     }
     break;
   default:
@@ -1000,7 +1030,7 @@ route_packet (struct DestinationEntry *destination,
       char xbuf[INET6_ADDRSTRLEN];
 
       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                 "Routing %s packet from %s:%u -> %s:%u to destination %s:%u\n",
+                 "Routing %s packet from [%s]:%u -> [%s]:%u to destination [%s]:%u\n",
                  (protocol == IPPROTO_TCP) ? "TCP" : "UDP",
                  inet_ntop (af, source_ip, sbuf, sizeof (sbuf)),
                  source_port,
@@ -1022,7 +1052,7 @@ route_packet (struct DestinationEntry *destination,
       char dbuf[INET6_ADDRSTRLEN];
 
       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                 "Routing %s packet from %s:%u -> %s:%u to service %s at peer %s\n",
+                 "Routing %s packet from [%s]:%u -> [%s]:%u to service %s at peer %s\n",
                  (protocol == IPPROTO_TCP) ? "TCP" : "UDP",
                  inet_ntop (af, source_ip, sbuf, sizeof (sbuf)),
                  source_port,
@@ -1048,17 +1078,14 @@ route_packet (struct DestinationEntry *destination,
                                          &key);
   if (NULL == ts)
   {
+    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                "Creating new channel for key %s\n",
+                GNUNET_h2s (&key));
     /* need to either use the existing channel from the destination (if still
        available) or create a fresh one */
-    is_new = GNUNET_YES;
-    if (NULL == dt->ts)
-      ts = create_channel_to_destination (dt, af);
-    else
-      ts = dt->ts;
+    ts = create_channel_to_destination (dt, af);
     if (NULL == ts)
       return;
-    dt->ts = NULL;
-    ts->destination_container = NULL; /* no longer 'contained' */
     /* now bind existing "unbound" channel to our IP/port tuple */
     ts->protocol = protocol;
     ts->af = af;
@@ -1090,12 +1117,24 @@ route_packet (struct DestinationEntry *destination,
   }
   else
   {
-    is_new = GNUNET_NO;
     GNUNET_CONTAINER_heap_update_cost (channel_heap,
                                       ts->heap_node,
                                       GNUNET_TIME_absolute_get ().abs_value_us);
   }
-  GNUNET_assert (NULL != ts->channel);
+  if (NULL == ts->channel)
+  {
+    GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
+                "Packet dropped, channel to %s not yet ready (%s)\n",
+                print_channel_destination (&ts->destination),
+                (NULL == ts->search)
+                ? "EXIT search failed"
+                : "EXIT search active");
+    GNUNET_STATISTICS_update (stats,
+                             gettext_noop ("# Packets dropped (channel not yet online)"),
+                             1,
+                              GNUNET_NO);
+    return;
+  }
 
   /* send via channel */
   switch (protocol)
@@ -1171,7 +1210,7 @@ route_packet (struct DestinationEntry *destination,
     }
     break;
   case IPPROTO_TCP:
-    if (is_new)
+    if (GNUNET_NO == ts->is_established)
     {
       if (destination->is_service)
       {
@@ -1503,6 +1542,7 @@ route_packet (struct DestinationEntry *destination,
     GNUNET_assert (0);
     break;
   }
+  ts->is_established = GNUNET_YES;
   send_to_channel (tnq, ts);
 }
 
@@ -1510,7 +1550,7 @@ route_packet (struct DestinationEntry *destination,
 /**
  * Receive packets from the helper-process (someone send to the local
  * virtual channel interface).  Find the destination mapping, and if it
- * exists, identify the correct MESH channel (or possibly create it)
+ * exists, identify the correct CADET channel (or possibly create it)
  * and forward the packet.
  *
  * @param cls closure, NULL
@@ -1682,7 +1722,7 @@ make_up_icmpv6_payload (struct ChannelState *ts,
 
 
 /**
- * We got an ICMP packet back from the MESH channel.  Pass it on to the
+ * We got an ICMP packet back from the CADET channel.  Pass it on to the
  * local virtual interface via the helper.
  *
  * @param cls closure, NULL
@@ -1694,7 +1734,7 @@ make_up_icmpv6_payload (struct ChannelState *ts,
  */
 static int
 receive_icmp_back (void *cls,
-                  struct GNUNET_MESH_Channel *channel,
+                  struct GNUNET_CADET_Channel *channel,
                   void **channel_ctx,
                   const struct GNUNET_MessageHeader *message)
 {
@@ -1703,7 +1743,7 @@ receive_icmp_back (void *cls,
   size_t mlen;
 
   GNUNET_STATISTICS_update (stats,
-                           gettext_noop ("# ICMP packets received from mesh"),
+                           gettext_noop ("# ICMP packets received from cadet"),
                            1, GNUNET_NO);
   mlen = ntohs (message->size);
   if (mlen < sizeof (struct GNUNET_EXIT_IcmpToVPNMessage))
@@ -1728,7 +1768,7 @@ receive_icmp_back (void *cls,
     char dbuf[INET6_ADDRSTRLEN];
 
     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-               "Received ICMP packet from mesh, sending %u bytes from %s -> %s via TUN\n",
+               "Received ICMP packet from cadet, sending %u bytes from %s -> %s via TUN\n",
                (unsigned int) mlen,
                inet_ntop (ts->af, &ts->destination_ip, sbuf, sizeof (sbuf)),
                inet_ntop (ts->af, &ts->source_ip, dbuf, sizeof (dbuf)));
@@ -2016,12 +2056,13 @@ receive_icmp_back (void *cls,
   GNUNET_CONTAINER_heap_update_cost (channel_heap,
                                     ts->heap_node,
                                     GNUNET_TIME_absolute_get ().abs_value_us);
+  GNUNET_CADET_receive_done (channel);
   return GNUNET_OK;
 }
 
 
 /**
- * We got a UDP packet back from the MESH channel.  Pass it on to the
+ * We got a UDP packet back from the CADET channel.  Pass it on to the
  * local virtual interface via the helper.
  *
  * @param cls closure, NULL
@@ -2033,7 +2074,7 @@ receive_icmp_back (void *cls,
  */
 static int
 receive_udp_back (void *cls,
-                 struct GNUNET_MESH_Channel *channel,
+                 struct GNUNET_CADET_Channel *channel,
                   void **channel_ctx,
                   const struct GNUNET_MessageHeader *message)
 {
@@ -2042,7 +2083,7 @@ receive_udp_back (void *cls,
   size_t mlen;
 
   GNUNET_STATISTICS_update (stats,
-                           gettext_noop ("# UDP packets received from mesh"),
+                           gettext_noop ("# UDP packets received from cadet"),
                            1, GNUNET_NO);
   mlen = ntohs (message->size);
   if (mlen < sizeof (struct GNUNET_EXIT_UdpReplyMessage))
@@ -2067,7 +2108,7 @@ receive_udp_back (void *cls,
     char dbuf[INET6_ADDRSTRLEN];
 
     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-               "Received UDP reply from mesh, sending %u bytes from %s:%u -> %s:%u via TUN\n",
+               "Received UDP reply from cadet, sending %u bytes from [%s]:%u -> [%s]:%u via TUN\n",
                (unsigned int) mlen,
                inet_ntop (ts->af, &ts->destination_ip, sbuf, sizeof (sbuf)),
                ts->destination_port,
@@ -2171,12 +2212,13 @@ receive_udp_back (void *cls,
   GNUNET_CONTAINER_heap_update_cost (channel_heap,
                                     ts->heap_node,
                                     GNUNET_TIME_absolute_get ().abs_value_us);
+  GNUNET_CADET_receive_done (channel);
   return GNUNET_OK;
 }
 
 
 /**
- * We got a TCP packet back from the MESH channel.  Pass it on to the
+ * We got a TCP packet back from the CADET channel.  Pass it on to the
  * local virtual interface via the helper.
  *
  * @param cls closure, NULL
@@ -2188,7 +2230,7 @@ receive_udp_back (void *cls,
  */
 static int
 receive_tcp_back (void *cls,
-                 struct GNUNET_MESH_Channel *channel,
+                 struct GNUNET_CADET_Channel *channel,
                   void **channel_ctx,
                   const struct GNUNET_MessageHeader *message)
 {
@@ -2197,7 +2239,7 @@ receive_tcp_back (void *cls,
   size_t mlen;
 
   GNUNET_STATISTICS_update (stats,
-                           gettext_noop ("# TCP packets received from mesh"),
+                           gettext_noop ("# TCP packets received from cadet"),
                            1, GNUNET_NO);
   mlen = ntohs (message->size);
   if (mlen < sizeof (struct GNUNET_EXIT_TcpDataMessage))
@@ -2217,7 +2259,7 @@ receive_tcp_back (void *cls,
     char dbuf[INET6_ADDRSTRLEN];
 
     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-               "Received TCP reply from mesh, sending %u bytes from %s:%u -> %s:%u via TUN\n",
+               "Received TCP reply from cadet, sending %u bytes from [%s]:%u -> [%s]:%u via TUN\n",
                (unsigned int) mlen,
                inet_ntop (ts->af, &ts->destination_ip, sbuf, sizeof (sbuf)),
                ts->destination_port,
@@ -2313,6 +2355,7 @@ receive_tcp_back (void *cls,
   GNUNET_CONTAINER_heap_update_cost (channel_heap,
                                     ts->heap_node,
                                     GNUNET_TIME_absolute_get ().abs_value_us);
+  GNUNET_CADET_receive_done (channel);
   return GNUNET_OK;
 }
 
@@ -2442,7 +2485,8 @@ free_destination_entry (struct DestinationEntry *de)
   struct DestinationChannel *dt;
 
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-             "Cleaning up destination entry\n");
+             "Cleaning up destination entry `%s'\n",
+              print_channel_destination (de));
   GNUNET_STATISTICS_update (stats,
                            gettext_noop ("# Active destinations"),
                            -1, GNUNET_NO);
@@ -2451,11 +2495,6 @@ free_destination_entry (struct DestinationEntry *de)
     GNUNET_CONTAINER_DLL_remove (de->dt_head,
                                 de->dt_tail,
                                 dt);
-    if (NULL != dt->ts)
-    {
-      free_channel_state (dt->ts);
-      GNUNET_assert (NULL == dt->ts);
-    }
     GNUNET_free (dt);
   }
   if (NULL != de->heap_node)
@@ -2689,7 +2728,6 @@ service_redirect_to_service (void *cls,
   void *addr;
   struct DestinationEntry *de;
   struct GNUNET_HashCode key;
-  struct ChannelState *ts;
   struct DestinationChannel *dt;
 
   /*  parse request */
@@ -2752,19 +2790,6 @@ service_redirect_to_service (void *cls,
   GNUNET_CONTAINER_DLL_insert (de->dt_head,
                               de->dt_tail,
                               dt);
-  ts = create_channel_to_destination (dt,
-                                    result_af);
-  switch (result_af)
-  {
-  case AF_INET:
-    ts->destination_ip.v4 = v4;
-    break;
-  case AF_INET6:
-    ts->destination_ip.v6 = v6;
-    break;
-  default:
-    GNUNET_assert (0);
-  }
   /* we're done */
   GNUNET_SERVER_receive_done (client, GNUNET_OK);
 }
@@ -2774,19 +2799,22 @@ service_redirect_to_service (void *cls,
  * Function called whenever a channel is destroyed.  Should clean up
  * any associated state.
  *
- * @param cls closure (set from #GNUNET_MESH_connect)
+ * @param cls closure (set from #GNUNET_CADET_connect)
  * @param channel connection to the other end (henceforth invalid)
  * @param channel_ctx place where local state associated
  *                   with the channel is stored (our `struct ChannelState`)
  */
 static void
 channel_cleaner (void *cls,
-               const struct GNUNET_MESH_Channel *channel,
-               void *channel_ctx)
+                 const struct GNUNET_CADET_Channel *channel,
+                 void *channel_ctx)
 {
   struct ChannelState *ts = channel_ctx;
 
-  ts->channel = NULL; /* we must not call GNUNET_MESH_channel_destroy() anymore */
+  ts->channel = NULL; /* we must not call GNUNET_CADET_channel_destroy() anymore */
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+             "CADET notified us about death of channel to `%s'\n",
+              print_channel_destination (&ts->destination));
   free_channel_state (ts);
 }
 
@@ -2821,11 +2849,14 @@ cleanup_destination (void *cls,
  */
 static int
 cleanup_channel (void *cls,
-               const struct GNUNET_HashCode *key,
-               void *value)
+                 const struct GNUNET_HashCode *key,
+                 void *value)
 {
   struct ChannelState *ts = value;
 
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+             "Tearing down channel to `%s' during cleanup\n",
+              print_channel_destination (&ts->destination));
   free_channel_state (ts);
   return GNUNET_OK;
 }
@@ -2835,11 +2866,9 @@ cleanup_channel (void *cls,
  * Function scheduled as very last function, cleans up after us
  *
  * @param cls unused
- * @param tc unused
  */
 static void
-cleanup (void *cls,
-         const struct GNUNET_SCHEDULER_TaskContext *tc)
+cleanup (void *cls)
 {
   unsigned int i;
 
@@ -2871,14 +2900,15 @@ cleanup (void *cls,
     GNUNET_CONTAINER_heap_destroy (channel_heap);
     channel_heap = NULL;
   }
-  if (NULL != mesh_handle)
+  if (NULL != cadet_handle)
   {
-    GNUNET_MESH_disconnect (mesh_handle);
-    mesh_handle = NULL;
+    GNUNET_CADET_disconnect (cadet_handle);
+    cadet_handle = NULL;
   }
   if (NULL != helper_handle)
   {
-    GNUNET_HELPER_stop (helper_handle, GNUNET_NO);
+    GNUNET_HELPER_kill (helper_handle, GNUNET_NO);
+    GNUNET_HELPER_wait (helper_handle);
     helper_handle = NULL;
   }
   if (NULL != nc)
@@ -2916,7 +2946,7 @@ run (void *cls,
      sizeof (struct RedirectToServiceRequestMessage) },
     {NULL, NULL, 0, 0}
   };
-  static const struct GNUNET_MESH_MessageHandler mesh_handlers[] = {
+  static const struct GNUNET_CADET_MessageHandler cadet_handlers[] = {
     { &receive_udp_back, GNUNET_MESSAGE_TYPE_VPN_UDP_REPLY, 0},
     { &receive_tcp_back, GNUNET_MESSAGE_TYPE_VPN_TCP_DATA_TO_VPN, 0},
     { &receive_icmp_back, GNUNET_MESSAGE_TYPE_VPN_ICMP_TO_VPN, 0},
@@ -2934,13 +2964,18 @@ run (void *cls,
   binary = GNUNET_OS_get_libexec_binary_path ("gnunet-helper-vpn");
 
   if (GNUNET_YES !=
-      GNUNET_OS_check_helper_binary (binary, GNUNET_YES, "-d gnunet-vpn - - 169.1.3.3.7 255.255.255.0")) //ipv4 only please!
+      GNUNET_OS_check_helper_binary (binary,
+                                     GNUNET_YES,
+                                     "-d gnunet-vpn - - 169.1.3.3.7 255.255.255.0")) //ipv4 only please!
   {
-    fprintf (stderr,
-            "`%s' is not SUID, refusing to run.\n",
-            "gnunet-helper-vpn");
+    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+                "`%s' is not SUID, refusing to run.\n",
+                "gnunet-helper-vpn");
     GNUNET_free (binary);
     global_ret = 1;
+    /* we won't "really" exit here, as the 'service' is still running;
+       however, as no handlers are registered, the service won't do
+       anything either */
     return;
   }
   GNUNET_free (binary);
@@ -3053,18 +3088,19 @@ run (void *cls,
   }
   vpn_argv[6] = NULL;
 
-  mesh_handle =
-    GNUNET_MESH_connect (cfg_, NULL,
-                        NULL,
-                        &channel_cleaner,
-                        mesh_handlers,
-                        NULL);
+  cadet_handle =
+    GNUNET_CADET_connect (cfg_, NULL,
+                          NULL,
+                          &channel_cleaner,
+                          cadet_handlers,
+                          NULL);
   helper_handle = GNUNET_HELPER_start (GNUNET_NO,
                                       "gnunet-helper-vpn", vpn_argv,
                                       &message_token, NULL, NULL);
   nc = GNUNET_SERVER_notification_context_create (server, 1);
   GNUNET_SERVER_add_handlers (server, service_handlers);
-  GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &cleanup, cls);
+  GNUNET_SCHEDULER_add_shutdown (&cleanup,
+                                NULL);
 }