gnunet-svn
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[GNUnet-SVN] r30697 - in gnunet/src: conversation exit include mesh pt s


From: gnunet
Subject: [GNUnet-SVN] r30697 - in gnunet/src: conversation exit include mesh pt scalarproduct set vpn
Date: Thu, 14 Nov 2013 14:55:13 +0100

Author: bartpolot
Date: 2013-11-14 14:55:12 +0100 (Thu, 14 Nov 2013)
New Revision: 30697

Added:
   gnunet/src/include/gnunet_mesh_service.h
   gnunet/src/mesh/gnunet-service-mesh.c
   gnunet/src/mesh/mesh.h
   gnunet/src/mesh/mesh_api.c
   gnunet/src/mesh/mesh_protocol.h
Removed:
   gnunet/src/include/gnunet_mesh_service.h
   gnunet/src/include/gnunet_mesh_service_enc.h
   gnunet/src/mesh/gnunet-service-mesh-enc.c
   gnunet/src/mesh/gnunet-service-mesh.c
   gnunet/src/mesh/mesh.h
   gnunet/src/mesh/mesh_api.c
   gnunet/src/mesh/mesh_api_enc.c
   gnunet/src/mesh/mesh_enc.h
   gnunet/src/mesh/mesh_protocol.h
   gnunet/src/mesh/mesh_protocol_enc.h
Modified:
   gnunet/src/conversation/gnunet-service-conversation.c
   gnunet/src/exit/gnunet-daemon-exit.c
   gnunet/src/mesh/Makefile.am
   gnunet/src/mesh/gnunet-mesh.c
   gnunet/src/mesh/gnunet-service-mesh_channel.c
   gnunet/src/mesh/gnunet-service-mesh_channel.h
   gnunet/src/mesh/gnunet-service-mesh_connection.c
   gnunet/src/mesh/gnunet-service-mesh_local.c
   gnunet/src/mesh/gnunet-service-mesh_peer.c
   gnunet/src/mesh/gnunet-service-mesh_tunnel.c
   gnunet/src/mesh/mesh_test_lib.c
   gnunet/src/mesh/mesh_test_lib.h
   gnunet/src/mesh/test_mesh_local.c
   gnunet/src/mesh/test_mesh_single.c
   gnunet/src/mesh/test_mesh_small.c
   gnunet/src/pt/gnunet-daemon-pt.c
   gnunet/src/scalarproduct/gnunet-service-scalarproduct.c
   gnunet/src/set/gnunet-service-set.c
   gnunet/src/set/gnunet-service-set.h
   gnunet/src/vpn/gnunet-service-vpn.c
Log:
Use encrypted MESH by default


Modified: gnunet/src/conversation/gnunet-service-conversation.c
===================================================================
--- gnunet/src/conversation/gnunet-service-conversation.c       2013-11-14 
13:30:29 UTC (rev 30696)
+++ gnunet/src/conversation/gnunet-service-conversation.c       2013-11-14 
13:55:12 UTC (rev 30697)
@@ -86,8 +86,8 @@
 
 
 /**
- * A line connects a local client with a mesh tunnel (or, if it is an
- * open line, is waiting for a mesh tunnel).
+ * A line connects a local client with a mesh channel (or, if it is an
+ * open line, is waiting for a mesh channel).
  */
 struct Line
 {
@@ -102,14 +102,14 @@
   struct Line *prev;
 
   /**
-   * Handle for the reliable tunnel (contol data)
+   * Handle for the reliable channel (contol data)
    */
-  struct GNUNET_MESH_Tunnel *tunnel_reliable;
+  struct GNUNET_MESH_Channel *channel_reliable;
 
   /**
-   * Handle for unreliable tunnel (audio data)
+   * Handle for unreliable channel (audio data)
    */
-  struct GNUNET_MESH_Tunnel *tunnel_unreliable;
+  struct GNUNET_MESH_Channel *channel_unreliable;
 
   /**
    * Transmit handle for pending audio messages
@@ -307,14 +307,14 @@
 
 
 /**
- * Destroy the mesh tunnels of a line.
+ * Destroy the mesh channels of a line.
  *
- * @param line line to shutdown tunnels of
+ * @param line line to shutdown channels of
  */
 static void
-destroy_line_mesh_tunnels (struct Line *line)
+destroy_line_mesh_channels (struct Line *line)
 {
-  struct GNUNET_MESH_Tunnel *t;
+  struct GNUNET_MESH_Channel *t;
 
   if (NULL != line->reliable_mq)
   {
@@ -326,15 +326,15 @@
     GNUNET_MESH_notify_transmit_ready_cancel (line->unreliable_mth);
     line->unreliable_mth = NULL;
   }
-  if (NULL != (t = line->tunnel_unreliable))
+  if (NULL != (t = line->channel_unreliable))
   {
-    line->tunnel_unreliable = NULL;
-    GNUNET_MESH_tunnel_destroy (t);
+    line->channel_unreliable = NULL;
+    GNUNET_MESH_channel_destroy (t);
   }
-  if (NULL != (t = line->tunnel_reliable))
+  if (NULL != (t = line->channel_reliable))
   {
-    line->tunnel_reliable = NULL;
-    GNUNET_MESH_tunnel_destroy (t);
+    line->channel_reliable = NULL;
+    GNUNET_MESH_channel_destroy (t);
   }
 }
 
@@ -363,7 +363,7 @@
     break;
   case LS_CALLEE_SHUTDOWN:
     line->status = LS_CALLEE_LISTEN;
-    destroy_line_mesh_tunnels (line);
+    destroy_line_mesh_channels (line);
     return;
   case LS_CALLER_CALLING:
     line->status = LS_CALLER_SHUTDOWN;
@@ -372,7 +372,7 @@
     line->status = LS_CALLER_SHUTDOWN;
     break;
   case LS_CALLER_SHUTDOWN:
-    destroy_line_mesh_tunnels (line);
+    destroy_line_mesh_channels (line);
     break;
   }
 }
@@ -490,13 +490,13 @@
                                line);
   line->remote_line = ntohl (msg->line);
   line->status = LS_CALLER_CALLING;
-  line->tunnel_reliable = GNUNET_MESH_tunnel_create (mesh,
+  line->channel_reliable = GNUNET_MESH_channel_create (mesh,
                                                      line,
                                                      &msg->target,
                                                      
GNUNET_APPLICATION_TYPE_CONVERSATION_CONTROL,
                                                      GNUNET_NO,
                                                      GNUNET_YES);
-  line->reliable_mq = GNUNET_MESH_mq_create (line->tunnel_reliable);
+  line->reliable_mq = GNUNET_MESH_mq_create (line->channel_reliable);
   line->local_line = local_line_cnt++;
   e = GNUNET_MQ_msg (ring, GNUNET_MESSAGE_TYPE_CONVERSATION_MESH_PHONE_RING);
   ring->purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_CONVERSATION_RING);
@@ -606,7 +606,7 @@
     GNUNET_SERVER_receive_done (client, GNUNET_OK);
     return;
   }
-  if (NULL == line->tunnel_unreliable)
+  if (NULL == line->channel_unreliable)
   {
     GNUNET_log (GNUNET_ERROR_TYPE_INFO | GNUNET_ERROR_TYPE_BULK,
                 _("Mesh audio channel not ready; audio data dropped\n"));
@@ -629,7 +629,7 @@
   memcpy (line->audio_data,
           &msg[1],
           size);
-  line->unreliable_mth = GNUNET_MESH_notify_transmit_ready 
(line->tunnel_unreliable,
+  line->unreliable_mth = GNUNET_MESH_notify_transmit_ready 
(line->channel_unreliable,
                                                             GNUNET_NO,
                                                             
GNUNET_TIME_UNIT_FOREVER_REL,
                                                             sizeof (struct 
MeshAudioMessage)
@@ -642,16 +642,16 @@
 
 /**
  * We are done signalling shutdown to the other peer.
- * Destroy the tunnel.
+ * Destroy the channel.
  *
- * @param cls the `struct GNUNET_MESH_tunnel` to destroy
+ * @param cls the `struct GNUNET_MESH_channel` to destroy
  */
 static void
-mq_done_destroy_tunnel (void *cls)
+mq_done_destroy_channel (void *cls)
 {
-  struct GNUNET_MESH_Tunnel *tunnel = cls;
+  struct GNUNET_MESH_Channel *channel = cls;
 
-  GNUNET_MESH_tunnel_destroy (tunnel);
+  GNUNET_MESH_channel_destroy (channel);
 }
 
 
@@ -659,15 +659,15 @@
  * Function to handle a ring message incoming over mesh
  *
  * @param cls closure, NULL
- * @param tunnel the tunnel over which the message arrived
- * @param tunnel_ctx the tunnel context, can be NULL
+ * @param channel the channel over which the message arrived
+ * @param channel_ctx the channel context, can be NULL
  * @param message the incoming message
  * @return #GNUNET_OK
  */
 static int
 handle_mesh_ring_message (void *cls,
-                          struct GNUNET_MESH_Tunnel *tunnel,
-                          void **tunnel_ctx,
+                          struct GNUNET_MESH_Channel *channel,
+                          void **channel_ctx,
                           const struct GNUNET_MessageHeader *message)
 {
   const struct MeshPhoneRingMessage *msg;
@@ -701,17 +701,17 @@
                 ntohl (msg->remote_line));
     e = GNUNET_MQ_msg (busy, GNUNET_MESSAGE_TYPE_CONVERSATION_MESH_PHONE_BUSY);
     GNUNET_MQ_notify_sent (e,
-                           &mq_done_destroy_tunnel,
-                           tunnel);
+                           &mq_done_destroy_channel,
+                           channel);
     GNUNET_MQ_send (line->reliable_mq, e);
-    GNUNET_MESH_receive_done (tunnel); /* needed? */
+    GNUNET_MESH_receive_done (channel); /* needed? */
     return GNUNET_OK;
   }
   line->status = LS_CALLEE_RINGING;
   line->remote_line = ntohl (msg->source_line);
-  line->tunnel_reliable = tunnel;
-  line->reliable_mq = GNUNET_MESH_mq_create (line->tunnel_reliable);
-  *tunnel_ctx = line;
+  line->channel_reliable = channel;
+  line->reliable_mq = GNUNET_MESH_mq_create (line->channel_reliable);
+  *channel_ctx = line;
   cring.header.type = htons (GNUNET_MESSAGE_TYPE_CONVERSATION_CS_PHONE_RING);
   cring.header.size = htons (sizeof (cring));
   cring.reserved = htonl (0);
@@ -722,7 +722,7 @@
                                               line->client,
                                               &cring.header,
                                               GNUNET_NO);
-  GNUNET_MESH_receive_done (tunnel);
+  GNUNET_MESH_receive_done (channel);
   return GNUNET_OK;
 }
 
@@ -731,18 +731,18 @@
  * Function to handle a hangup message incoming over mesh
  *
  * @param cls closure, NULL
- * @param tunnel the tunnel over which the message arrived
- * @param tunnel_ctx the tunnel context, can be NULL
+ * @param channel the channel over which the message arrived
+ * @param channel_ctx the channel context, can be NULL
  * @param message the incoming message
  * @return #GNUNET_OK
  */
 static int
 handle_mesh_hangup_message (void *cls,
-                            struct GNUNET_MESH_Tunnel *tunnel,
-                            void **tunnel_ctx,
+                            struct GNUNET_MESH_Channel *channel,
+                            void **channel_ctx,
                             const struct GNUNET_MessageHeader *message)
 {
-  struct Line *line = *tunnel_ctx;
+  struct Line *line = *channel_ctx;
   const struct MeshPhoneHangupMessage *msg;
   const char *reason;
   size_t len = ntohs (message->size) - sizeof (struct MeshPhoneHangupMessage);
@@ -761,10 +761,10 @@
   if (NULL == line)
   {
     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                "HANGUP message received for non-existing line, dropping 
tunnel.\n");
+                "HANGUP message received for non-existing line, dropping 
channel.\n");
     return GNUNET_SYSERR;
   }
-  *tunnel_ctx = NULL;
+  *channel_ctx = NULL;
   switch (line->status)
   {
   case LS_CALLEE_LISTEN:
@@ -772,15 +772,15 @@
     return GNUNET_SYSERR;
   case LS_CALLEE_RINGING:
     line->status = LS_CALLEE_LISTEN;
-    destroy_line_mesh_tunnels (line);
+    destroy_line_mesh_channels (line);
     break;
   case LS_CALLEE_CONNECTED:
     line->status = LS_CALLEE_LISTEN;
-    destroy_line_mesh_tunnels (line);
+    destroy_line_mesh_channels (line);
     break;
   case LS_CALLEE_SHUTDOWN:
     line->status = LS_CALLEE_LISTEN;
-    destroy_line_mesh_tunnels (line);
+    destroy_line_mesh_channels (line);
     return GNUNET_OK;
   case LS_CALLER_CALLING:
     line->status = LS_CALLER_SHUTDOWN;
@@ -805,7 +805,7 @@
                                               line->client,
                                               &hup->header,
                                               GNUNET_NO);
-  GNUNET_MESH_receive_done (tunnel);
+  GNUNET_MESH_receive_done (channel);
   return GNUNET_OK;
 }
 
@@ -814,19 +814,19 @@
  * Function to handle a pickup message incoming over mesh
  *
  * @param cls closure, NULL
- * @param tunnel the tunnel over which the message arrived
- * @param tunnel_ctx the tunnel context, can be NULL
+ * @param channel the channel over which the message arrived
+ * @param channel_ctx the channel context, can be NULL
  * @param message the incoming message
  * @return #GNUNET_OK
  */
 static int
 handle_mesh_pickup_message (void *cls,
-                            struct GNUNET_MESH_Tunnel *tunnel,
-                            void **tunnel_ctx,
+                            struct GNUNET_MESH_Channel *channel,
+                            void **channel_ctx,
                             const struct GNUNET_MessageHeader *message)
 {
   const struct MeshPhonePickupMessage *msg;
-  struct Line *line = *tunnel_ctx;
+  struct Line *line = *channel_ctx;
   const char *metadata;
   size_t len = ntohs (message->size) - sizeof (struct MeshPhonePickupMessage);
   char buf[len + sizeof (struct ClientPhonePickupMessage)];
@@ -844,10 +844,10 @@
   if (NULL == line)
   {
     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                "PICKUP message received for non-existing line, dropping 
tunnel.\n");
+                "PICKUP message received for non-existing line, dropping 
channel.\n");
     return GNUNET_SYSERR;
   }
-  GNUNET_MESH_receive_done (tunnel);
+  GNUNET_MESH_receive_done (channel);
   switch (line->status)
   {
   case LS_CALLEE_LISTEN:
@@ -856,13 +856,13 @@
   case LS_CALLEE_RINGING:
   case LS_CALLEE_CONNECTED:
     GNUNET_break_op (0);
-    destroy_line_mesh_tunnels (line);
+    destroy_line_mesh_channels (line);
     line->status = LS_CALLEE_LISTEN;
     return GNUNET_SYSERR;
   case LS_CALLEE_SHUTDOWN:
     GNUNET_break_op (0);
     line->status = LS_CALLEE_LISTEN;
-    destroy_line_mesh_tunnels (line);
+    destroy_line_mesh_channels (line);
     break;
   case LS_CALLER_CALLING:
     line->status = LS_CALLER_CONNECTED;
@@ -886,13 +886,13 @@
                                               line->client,
                                               &pick->header,
                                               GNUNET_NO);
-  line->tunnel_unreliable = GNUNET_MESH_tunnel_create (mesh,
+  line->channel_unreliable = GNUNET_MESH_channel_create (mesh,
                                                        line,
                                                        &line->target,
                                                        
GNUNET_APPLICATION_TYPE_CONVERSATION_AUDIO,
                                                        GNUNET_YES,
                                                        GNUNET_NO);
-  if (NULL == line->tunnel_unreliable)
+  if (NULL == line->channel_unreliable)
   {
     GNUNET_break (0);
   }
@@ -904,30 +904,30 @@
  * Function to handle a busy message incoming over mesh
  *
  * @param cls closure, NULL
- * @param tunnel the tunnel over which the message arrived
- * @param tunnel_ctx the tunnel context, can be NULL
+ * @param channel the channel over which the message arrived
+ * @param channel_ctx the channel context, can be NULL
  * @param message the incoming message
  * @return #GNUNET_OK
  */
 static int
 handle_mesh_busy_message (void *cls,
-                          struct GNUNET_MESH_Tunnel *tunnel,
-                          void **tunnel_ctx,
+                          struct GNUNET_MESH_Channel *channel,
+                          void **channel_ctx,
                           const struct GNUNET_MessageHeader *message)
 {
-  struct Line *line = *tunnel_ctx;
+  struct Line *line = *channel_ctx;
   struct ClientPhoneBusyMessage busy;
 
   if (NULL == line)
   {
     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                "HANGUP message received for non-existing line, dropping 
tunnel.\n");
+                "HANGUP message received for non-existing line, dropping 
channel.\n");
     return GNUNET_SYSERR;
   }
   busy.header.size = sizeof (busy);
   busy.header.type = htons (GNUNET_MESSAGE_TYPE_CONVERSATION_CS_PHONE_BUSY);
-  GNUNET_MESH_receive_done (tunnel);
-  *tunnel_ctx = NULL;
+  GNUNET_MESH_receive_done (channel);
+  *channel_ctx = NULL;
   switch (line->status)
   {
   case LS_CALLEE_LISTEN:
@@ -970,43 +970,43 @@
  * Function to handle an audio message incoming over mesh
  *
  * @param cls closure, NULL
- * @param tunnel the tunnel over which the message arrived
- * @param tunnel_ctx the tunnel context, can be NULL
+ * @param channel the channel over which the message arrived
+ * @param channel_ctx the channel context, can be NULL
  * @param message the incoming message
  * @return #GNUNET_OK
  */
 static int
 handle_mesh_audio_message (void *cls,
-                           struct GNUNET_MESH_Tunnel *tunnel,
-                           void **tunnel_ctx,
+                           struct GNUNET_MESH_Channel *channel,
+                           void **channel_ctx,
                            const struct GNUNET_MessageHeader *message)
 {
   const struct MeshAudioMessage *msg;
-  struct Line *line = *tunnel_ctx;
+  struct Line *line = *channel_ctx;
   struct GNUNET_PeerIdentity sender;
   size_t msize = ntohs (message->size) - sizeof (struct MeshAudioMessage);
   char buf[msize + sizeof (struct ClientAudioMessage)];
   struct ClientAudioMessage *cam;
-  const union GNUNET_MESH_TunnelInfo *info;
+  const union GNUNET_MESH_ChannelInfo *info;
 
   msg = (const struct MeshAudioMessage *) message;
   if (NULL == line)
   {
-    info = GNUNET_MESH_tunnel_get_info (tunnel,
+    info = GNUNET_MESH_channel_get_info (channel,
                                         GNUNET_MESH_OPTION_PEER);
     if (NULL == info)
     {
       GNUNET_break (0);
       return GNUNET_OK;
     }
-    sender = info->peer;
+    sender = *(info->peer);
     for (line = lines_head; NULL != line; line = line->next)
       if ( (line->local_line == ntohl (msg->remote_line)) &&
            (LS_CALLEE_CONNECTED == line->status) &&
            (0 == memcmp (&line->target,
                          &sender,
                          sizeof (struct GNUNET_PeerIdentity))) &&
-           (NULL == line->tunnel_unreliable) )
+           (NULL == line->channel_unreliable) )
         break;
     if (NULL == line)
     {
@@ -1015,8 +1015,8 @@
                   ntohl (msg->remote_line));
       return GNUNET_SYSERR;
     }
-    line->tunnel_unreliable = tunnel;
-    *tunnel_ctx = line;
+    line->channel_unreliable = channel;
+    *channel_ctx = line;
   }
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
               "Forwarding %u bytes of AUDIO data to client\n",
@@ -1029,69 +1029,69 @@
                                               line->client,
                                               &cam->header,
                                               GNUNET_YES);
-  GNUNET_MESH_receive_done (tunnel);
+  GNUNET_MESH_receive_done (channel);
   return GNUNET_OK;
 }
 
 
 /**
- * Method called whenever another peer has added us to a tunnel
+ * Method called whenever another peer has added us to a channel
  * the other peer initiated.
  *
  * @param cls closure
- * @param tunnel new handle to the tunnel
- * @param initiator peer that started the tunnel
+ * @param channel new handle to the channel
+ * @param initiator peer that started the channel
  * @param port port
- * @return initial tunnel context for the tunnel (can be NULL -- that's not an 
error)
+ * @return initial channel context for the channel (can be NULL -- that's not 
an error)
  */
 static void *
-inbound_tunnel (void *cls,
-                struct GNUNET_MESH_Tunnel *tunnel,
+inbound_channel (void *cls,
+                struct GNUNET_MESH_Channel *channel,
                const struct GNUNET_PeerIdentity *initiator,
                 uint32_t port)
 {
   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
-             _("Received incoming tunnel on port %u\n"),
+             _("Received incoming channel on port %u\n"),
               (unsigned int) port);
   return NULL;
 }
 
 
 /**
- * Function called whenever an inbound tunnel is destroyed.  Should clean up
+ * Function called whenever an inbound channel is destroyed.  Should clean up
  * any associated state.
  *
  * @param cls closure (set from #GNUNET_MESH_connect)
- * @param tunnel connection to the other end (henceforth invalid)
- * @param tunnel_ctx place where local state associated
- *                   with the tunnel is stored
+ * @param channel connection to the other end (henceforth invalid)
+ * @param channel_ctx place where local state associated
+ *                   with the channel is stored
  */
 static void
 inbound_end (void *cls,
-             const struct GNUNET_MESH_Tunnel *tunnel,
-            void *tunnel_ctx)
+             const struct GNUNET_MESH_Channel *channel,
+            void *channel_ctx)
 {
-  struct Line *line = tunnel_ctx;
+  struct Line *line = channel_ctx;
   struct ClientPhoneHangupMessage hup;
 
   if (NULL == line)
     return;
-  if (line->tunnel_unreliable == tunnel)
+  if (line->channel_unreliable == channel)
   {
     if (NULL != line->unreliable_mth)
     {
       GNUNET_MESH_notify_transmit_ready_cancel (line->unreliable_mth);
       line->unreliable_mth = NULL;
     }
-    line->tunnel_unreliable = NULL;
+    line->channel_unreliable = NULL;
     return;
   }
-  if (line->tunnel_reliable != tunnel)
+  if (line->channel_reliable != channel)
     return;
-  line->tunnel_reliable = NULL;
+  line->channel_reliable = NULL;
 
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-             "Mesh tunnel destroyed by mesh\n");
+             "Mesh channel destroyed by mesh\n");
   hup.header.size = sizeof (hup);
   hup.header.type = htons (GNUNET_MESSAGE_TYPE_CONVERSATION_CS_PHONE_HANG_UP);
   switch (line->status)
@@ -1120,7 +1120,7 @@
   case LS_CALLER_SHUTDOWN:
     break;
   }
-  destroy_line_mesh_tunnels (line);
+  destroy_line_mesh_channels (line);
 }
 
 
@@ -1147,7 +1147,7 @@
   GNUNET_CONTAINER_DLL_remove (lines_head,
                                lines_tail,
                                line);
-  destroy_line_mesh_tunnels (line);
+  destroy_line_mesh_channels (line);
   GNUNET_free_non_null (line->audio_data);
   GNUNET_free (line);
 }
@@ -1235,7 +1235,7 @@
                                                   &my_identity));
   mesh = GNUNET_MESH_connect (cfg,
                              NULL,
-                             &inbound_tunnel,
+                             &inbound_channel,
                              &inbound_end,
                               mesh_handlers,
                               ports);

Modified: gnunet/src/exit/gnunet-daemon-exit.c
===================================================================
--- gnunet/src/exit/gnunet-daemon-exit.c        2013-11-14 13:30:29 UTC (rev 
30696)
+++ gnunet/src/exit/gnunet-daemon-exit.c        2013-11-14 13:55:12 UTC (rev 
30697)
@@ -184,22 +184,22 @@
 
 
 /**
- * Queue of messages to a tunnel.
+ * Queue of messages to a channel.
  */
-struct TunnelMessageQueue
+struct ChannelMessageQueue
 {
   /**
    * This is a doubly-linked list.
    */
-  struct TunnelMessageQueue *next;
+  struct ChannelMessageQueue *next;
 
   /**
    * This is a doubly-linked list.
    */
-  struct TunnelMessageQueue *prev;
+  struct ChannelMessageQueue *prev;
 
   /**
-   * Payload to send via the tunnel.
+   * Payload to send via the channel.
    */
   const void *payload;
 
@@ -212,32 +212,32 @@
 
 /**
  * This struct is saved into connections_map to allow finding the
- * right tunnel given an IP packet from TUN.  It is also associated
- * with the tunnel's closure so we can find it again for the next
- * message from the tunnel.
+ * right channel given an IP packet from TUN.  It is also associated
+ * with the channel's closure so we can find it again for the next
+ * message from the channel.
  */
-struct TunnelState
+struct ChannelState
 {
   /**
-   * Mesh tunnel that is used for this connection.
+   * Mesh channel that is used for this connection.
    */
-  struct GNUNET_MESH_Tunnel *tunnel;
+  struct GNUNET_MESH_Channel *channel;
 
   /**
-   * Who is the other end of this tunnel.
+   * Who is the other end of this channel.
    * FIXME is this needed? Only used for debugging messages
    */
   struct GNUNET_PeerIdentity peer;
 
   /**
-   * Active tunnel transmission request (or NULL).
+   * Active channel transmission request (or NULL).
    */
   struct GNUNET_MESH_TransmitHandle *th;
 
   /**
-   * #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.
+   * #GNUNET_NO if this is a channel for TCP/UDP,
+   * #GNUNET_YES if this is a channel for DNS,
+   * #GNUNET_SYSERR if the channel is not yet initialized.
    */
   int is_dns;
 
@@ -262,14 +262,14 @@
       struct LocalService *serv;
 
       /**
-       * Head of DLL of messages for this tunnel.
+       * Head of DLL of messages for this channel.
        */
-      struct TunnelMessageQueue *head;
+      struct ChannelMessageQueue *head;
 
       /**
-       * Tail of DLL of messages for this tunnel.
+       * Tail of DLL of messages for this channel.
        */
-      struct TunnelMessageQueue *tail;
+      struct ChannelMessageQueue *tail;
 
       /**
        * Primary redirection information for this connection.
@@ -375,7 +375,7 @@
 
 /**
  * This hashmaps contains the mapping from peer, service-descriptor,
- * source-port and destination-port to a struct TunnelState
+ * source-port and destination-port to a struct ChannelState
  */
 static struct GNUNET_CONTAINER_MultiHashMap *connections_map;
 
@@ -400,9 +400,9 @@
 static struct GNUNET_CONTAINER_MultiHashMap *tcp_services;
 
 /**
- * Array of all open DNS requests from tunnels.
+ * Array of all open DNS requests from channels.
  */
-static struct TunnelState *tunnels[UINT16_MAX + 1];
+static struct ChannelState *channels[UINT16_MAX + 1];
 
 /**
  * Handle to the DNS Stub resolver.
@@ -462,10 +462,10 @@
 
 
 /**
- * We got a reply from DNS for a request of a MESH tunnel.  Send it
- * via the tunnel (after changing the request ID back).
+ * We got a reply from DNS for a request of a MESH channel.  Send it
+ * via the channel (after changing the request ID back).
  *
- * @param cls the 'struct TunnelState'
+ * @param cls the 'struct ChannelState'
  * @param size number of bytes available in buf
  * @param buf where to copy the reply
  * @return number of bytes written to buf
@@ -475,7 +475,7 @@
                        size_t size,
                        void *buf)
 {
-  struct TunnelState *ts = cls;
+  struct ChannelState *ts = cls;
   size_t off;
   size_t ret;
   char *cbuf = buf;
@@ -523,26 +523,26 @@
                    const struct GNUNET_TUN_DnsHeader *dns,
                    size_t r)
 {
-  struct TunnelState *ts;
+  struct ChannelState *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];
+  /* Handle case that this is a reply to a request from a MESH DNS channel */
+  ts = channels[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;
+  channels[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,
+  ts->th = GNUNET_MESH_notify_transmit_ready (ts->channel,
                                              GNUNET_NO,
                                              GNUNET_TIME_UNIT_FOREVER_REL,
                                              sizeof (struct 
GNUNET_MessageHeader) + r,
@@ -555,19 +555,19 @@
  * 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 channel connection to the other end
+ * @param channel_ctx pointer to our `struct ChannelState *`
  * @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,
+receive_dns_request (void *cls GNUNET_UNUSED, struct GNUNET_MESH_Channel 
*channel,
+                     void **channel_ctx,
                      const struct GNUNET_MessageHeader *message)
 {
-  struct TunnelState *ts = *tunnel_ctx;
+  struct ChannelState *ts = *channel_ctx;
   const struct GNUNET_TUN_DnsHeader *dns;
   size_t mlen = ntohs (message->size);
   size_t dlen = mlen - sizeof (struct GNUNET_MessageHeader);
@@ -583,7 +583,7 @@
   }
   if (GNUNET_SYSERR == ts->is_dns)
   {
-    /* tunnel is DNS from now on */
+    /* channel is DNS from now on */
     ts->is_dns = GNUNET_YES;
   }
   if (dlen < sizeof (struct GNUNET_TUN_DnsHeader))
@@ -593,11 +593,11 @@
   }
   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;
+  if (channels[ts->specifics.dns.my_id] == ts)
+    channels[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;
+  channels[ts->specifics.dns.my_id] = ts;
   memcpy (buf, dns, dlen);
   dout = (struct GNUNET_TUN_DnsHeader *) buf;
   dout->id = ts->specifics.dns.my_id;
@@ -676,7 +676,7 @@
  * @param state_key set to hash's state if non-NULL
  * @return NULL if we have no tracking information for this tuple
  */
-static struct TunnelState *
+static struct ChannelState *
 get_redirect_state (int af,
                    int protocol,
                    const void *destination_ip,
@@ -687,7 +687,7 @@
 {
   struct RedirectInformation ri;
   struct GNUNET_HashCode key;
-  struct TunnelState *state;
+  struct ChannelState *state;
 
   if ( ( (af == AF_INET) && (protocol == IPPROTO_ICMP) ) ||
        ( (af == AF_INET6) && (protocol == IPPROTO_ICMPV6) ) )
@@ -807,9 +807,9 @@
 
 
 /**
- * MESH is ready to receive a message for the tunnel.  Transmit it.
+ * MESH is ready to receive a message for the channel.  Transmit it.
  *
- * @param cls the 'struct TunnelState'.
+ * @param cls the 'struct ChannelState'.
  * @param size number of bytes available in buf
  * @param buf where to copy the message
  * @return number of bytes copied to buf
@@ -817,9 +817,9 @@
 static size_t
 send_to_peer_notify_callback (void *cls, size_t size, void *buf)
 {
-  struct TunnelState *s = cls;
-  struct GNUNET_MESH_Tunnel *tunnel = s->tunnel;
-  struct TunnelMessageQueue *tnq;
+  struct ChannelState *s = cls;
+  struct GNUNET_MESH_Channel *channel = s->channel;
+  struct ChannelMessageQueue *tnq;
 
   s->th = NULL;
   tnq = s->specifics.tcp_udp.head;
@@ -827,7 +827,7 @@
     return 0;
   if (0 == size)
   {
-    s->th = GNUNET_MESH_notify_transmit_ready (tunnel,
+    s->th = GNUNET_MESH_notify_transmit_ready (channel,
                                               GNUNET_NO /* corking */,
                                               GNUNET_TIME_UNIT_FOREVER_REL,
                                               tnq->len,
@@ -843,36 +843,36 @@
                               tnq);
   GNUNET_free (tnq);
   if (NULL != (tnq = s->specifics.tcp_udp.head))
-    s->th = GNUNET_MESH_notify_transmit_ready (tunnel,
+    s->th = GNUNET_MESH_notify_transmit_ready (channel,
                                               GNUNET_NO /* corking */,
                                               GNUNET_TIME_UNIT_FOREVER_REL,
                                               tnq->len,
                                               &send_to_peer_notify_callback,
                                               s);
   GNUNET_STATISTICS_update (stats,
-                           gettext_noop ("# Bytes transmitted via mesh 
tunnels"),
+                           gettext_noop ("# Bytes transmitted via mesh 
channels"),
                            size, GNUNET_NO);
   return size;
 }
 
 
 /**
- * Send the given packet via the mesh tunnel.
+ * Send the given packet via the mesh channel.
  *
- * @param s tunnel destination
+ * @param s channel destination
  * @param tnq message to queue
  */
 static void
-send_packet_to_mesh_tunnel (struct TunnelState *s,
-                           struct TunnelMessageQueue *tnq)
+send_packet_to_mesh_channel (struct ChannelState *s,
+                           struct ChannelMessageQueue *tnq)
 {
-  struct GNUNET_MESH_Tunnel *mesh_tunnel;
+  struct GNUNET_MESH_Channel *mesh_channel;
 
-  mesh_tunnel = s->tunnel;
+  mesh_channel = s->channel;
   GNUNET_assert (NULL != s);
   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,
+    s->th = GNUNET_MESH_notify_transmit_ready (mesh_channel,
                                                GNUNET_NO /* cork */,
                                               GNUNET_TIME_UNIT_FOREVER_REL,
                                               tnq->len,
@@ -899,8 +899,8 @@
                  const void *destination_ip,
                  const void *source_ip)
 {
-  struct TunnelState *state;
-  struct TunnelMessageQueue *tnq;
+  struct ChannelState *state;
+  struct ChannelMessageQueue *tnq;
   struct GNUNET_EXIT_IcmpToVPNMessage *i2v;
   const struct GNUNET_TUN_IPv4Header *ipv4;
   const struct GNUNET_TUN_IPv6Header *ipv6;
@@ -1055,7 +1055,7 @@
     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 ChannelMessageQueue) + mlen);
   tnq->payload = &tnq[1];
   tnq->len = mlen;
   i2v = (struct GNUNET_EXIT_IcmpToVPNMessage *) &tnq[1];
@@ -1065,7 +1065,7 @@
   memcpy (&i2v->icmp_header,
          icmp,
          pktlen);
-  send_packet_to_mesh_tunnel (state, tnq);
+  send_packet_to_mesh_channel (state, tnq);
 }
 
 
@@ -1087,8 +1087,8 @@
                 const void *destination_ip,
                 const void *source_ip)
 {
-  struct TunnelState *state;
-  struct TunnelMessageQueue *tnq;
+  struct ChannelState *state;
+  struct ChannelMessageQueue *tnq;
   struct GNUNET_EXIT_UdpReplyMessage *urm;
   size_t mlen;
 
@@ -1131,7 +1131,7 @@
     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 ChannelMessageQueue) + mlen);
   tnq->payload = &tnq[1];
   tnq->len = mlen;
   urm = (struct GNUNET_EXIT_UdpReplyMessage *) &tnq[1];
@@ -1142,7 +1142,7 @@
   memcpy (&urm[1],
          &udp[1],
          pktlen - sizeof (struct GNUNET_TUN_UdpHeader));
-  send_packet_to_mesh_tunnel (state, tnq);
+  send_packet_to_mesh_channel (state, tnq);
 }
 
 
@@ -1164,11 +1164,11 @@
                 const void *destination_ip,
                 const void *source_ip)
 {
-  struct TunnelState *state;
+  struct ChannelState *state;
   char buf[pktlen] GNUNET_ALIGN;
   struct GNUNET_TUN_TcpHeader *mtcp;
   struct GNUNET_EXIT_TcpDataMessage *tdm;
-  struct TunnelMessageQueue *tnq;
+  struct ChannelMessageQueue *tnq;
   size_t mlen;
 
   {
@@ -1220,7 +1220,7 @@
     return;
   }
 
-  tnq = GNUNET_malloc (sizeof (struct TunnelMessageQueue) + mlen);
+  tnq = GNUNET_malloc (sizeof (struct ChannelMessageQueue) + mlen);
   tnq->payload = &tnq[1];
   tnq->len = mlen;
   tdm = (struct GNUNET_EXIT_TcpDataMessage *) &tnq[1];
@@ -1230,7 +1230,7 @@
   memcpy (&tdm->tcp_header,
          buf,
          pktlen);
-  send_packet_to_mesh_tunnel (state, tnq);
+  send_packet_to_mesh_channel (state, tnq);
 }
 
 
@@ -1486,7 +1486,7 @@
  * We are starting a fresh connection (TCP or UDP) and need
  * to pick a source port and IP address (within the correct
  * range and address family) to associate replies with the
- * connection / correct mesh tunnel.  This function generates
+ * connection / correct mesh channel.  This function generates
  * a "fresh" source IP and source port number for a connection
  * After picking a good source address, this function sets up
  * the state in the 'connections_map' and 'connections_heap'
@@ -1497,17 +1497,17 @@
  * @param state skeleton state to setup a record for; should
  *              '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
+ *              going to be used (the 'channel' should also
  *              already be set); after calling this function,
  *              heap_node and the local_address will be
  *              also initialized (heap_node != NULL can be
  *              used to test if a state has been fully setup).
  */
 static void
-setup_state_record (struct TunnelState *state)
+setup_state_record (struct ChannelState *state)
 {
   struct GNUNET_HashCode key;
-  struct TunnelState *s;
+  struct ChannelState *s;
 
   /* generate fresh, unique address */
   do
@@ -1549,7 +1549,7 @@
     s = GNUNET_CONTAINER_heap_remove_root (connections_heap);
     GNUNET_assert (state != s);
     s->specifics.tcp_udp.heap_node = NULL;
-    GNUNET_MESH_tunnel_destroy (s->tunnel);
+    GNUNET_MESH_channel_destroy (s->channel);
     GNUNET_assert (GNUNET_OK ==
                   GNUNET_CONTAINER_multihashmap_remove (connections_map,
                                                         
&s->specifics.tcp_udp.state_key,
@@ -1837,19 +1837,19 @@
  * offered by this system.
  *
  * @param cls closure, NULL
- * @param tunnel connection to the other end
- * @param tunnel_ctx pointer to our `struct TunnelState *`
+ * @param channel connection to the other end
+ * @param channel_ctx pointer to our `struct ChannelState *`
  * @param message the actual message
  * @return #GNUNET_OK to keep the connection open,
  *         #GNUNET_SYSERR to close it (signal serious error)
  */
 static int
 receive_tcp_service (void *cls,
-                     struct GNUNET_MESH_Tunnel *tunnel,
-                     void **tunnel_ctx,
+                     struct GNUNET_MESH_Channel *channel,
+                     void **channel_ctx,
                      const struct GNUNET_MessageHeader *message)
 {
-  struct TunnelState *state = *tunnel_ctx;
+  struct ChannelState *state = *channel_ctx;
   const struct GNUNET_EXIT_TcpServiceStartMessage *start;
   uint16_t pkt_len = ntohs (message->size);
 
@@ -1865,7 +1865,7 @@
   }
   if (GNUNET_SYSERR == state->is_dns)
   {
-    /* tunnel is UDP/TCP from now on */
+    /* channel is UDP/TCP from now on */
     state->is_dns = GNUNET_NO;
   }
   GNUNET_STATISTICS_update (stats,
@@ -1926,19 +1926,19 @@
  * Process a request to forward TCP data to the Internet via this peer.
  *
  * @param cls closure, NULL
- * @param tunnel connection to the other end
- * @param tunnel_ctx pointer to our 'struct TunnelState *'
+ * @param channel connection to the other end
+ * @param channel_ctx pointer to our 'struct ChannelState *'
  * @param message the actual message
  *
  * @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,
+receive_tcp_remote (void *cls GNUNET_UNUSED, struct GNUNET_MESH_Channel 
*channel,
+                    void **channel_ctx GNUNET_UNUSED,
                     const struct GNUNET_MessageHeader *message)
 {
-  struct TunnelState *state = *tunnel_ctx;
+  struct ChannelState *state = *channel_ctx;
   const struct GNUNET_EXIT_TcpInternetStartMessage *start;
   uint16_t pkt_len = ntohs (message->size);
   const struct in_addr *v4;
@@ -1958,7 +1958,7 @@
   }
   if (GNUNET_SYSERR == state->is_dns)
   {
-    /* tunnel is UDP/TCP from now on */
+    /* channel is UDP/TCP from now on */
     state->is_dns = GNUNET_NO;
   }
   GNUNET_STATISTICS_update (stats,
@@ -2051,18 +2051,18 @@
  * connection via this peer.
  *
  * @param cls closure, NULL
- * @param tunnel connection to the other end
- * @param tunnel_ctx pointer to our 'struct TunnelState *'
+ * @param channel connection to the other end
+ * @param channel_ctx pointer to our 'struct ChannelState *'
  * @param message the actual message
  * @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,
+receive_tcp_data (void *cls GNUNET_UNUSED, struct GNUNET_MESH_Channel *channel,
+                 void **channel_ctx GNUNET_UNUSED,
                  const struct GNUNET_MessageHeader *message)
 {
-  struct TunnelState *state = *tunnel_ctx;
+  struct ChannelState *state = *channel_ctx;
   const struct GNUNET_EXIT_TcpDataMessage *data;
   uint16_t pkt_len = ntohs (message->size);
 
@@ -2100,7 +2100,7 @@
   }
   if (GNUNET_SYSERR == state->is_dns)
   {
-    /* tunnel is UDP/TCP from now on */
+    /* channel is UDP/TCP from now on */
     state->is_dns = GNUNET_NO;
   }
 
@@ -2229,15 +2229,15 @@
 
 /**
  * Synthesize a plausible ICMP payload for an ICMPv4 error
- * response on the given tunnel.
+ * response on the given channel.
  *
- * @param state tunnel information
+ * @param state channel information
  * @param ipp IPv6 header to fill in (ICMP payload)
  * @param udp "UDP" header to fill in (ICMP payload); might actually
  *            also be the first 8 bytes of the TCP header
  */
 static void
-make_up_icmpv4_payload (struct TunnelState *state,
+make_up_icmpv4_payload (struct ChannelState *state,
                        struct GNUNET_TUN_IPv4Header *ipp,
                        struct GNUNET_TUN_UdpHeader *udp)
 {
@@ -2255,15 +2255,15 @@
 
 /**
  * Synthesize a plausible ICMP payload for an ICMPv6 error
- * response on the given tunnel.
+ * response on the given channel.
  *
- * @param state tunnel information
+ * @param state channel information
  * @param ipp IPv6 header to fill in (ICMP payload)
  * @param udp "UDP" header to fill in (ICMP payload); might actually
  *            also be the first 8 bytes of the TCP header
  */
 static void
-make_up_icmpv6_payload (struct TunnelState *state,
+make_up_icmpv6_payload (struct ChannelState *state,
                        struct GNUNET_TUN_IPv6Header *ipp,
                        struct GNUNET_TUN_UdpHeader *udp)
 {
@@ -2283,19 +2283,19 @@
  * Process a request to forward ICMP data to the Internet via this peer.
  *
  * @param cls closure, NULL
- * @param tunnel connection to the other end
- * @param tunnel_ctx pointer to our 'struct TunnelState *'
+ * @param channel connection to the other end
+ * @param channel_ctx pointer to our 'struct ChannelState *'
  * @param message the actual message
  * @return #GNUNET_OK to keep the connection open,
  *         #GNUNET_SYSERR to close it (signal serious error)
  */
 static int
 receive_icmp_remote (void *cls,
-                     struct GNUNET_MESH_Tunnel *tunnel,
-                    void **tunnel_ctx,
+                     struct GNUNET_MESH_Channel *channel,
+                    void **channel_ctx,
                     const struct GNUNET_MessageHeader *message)
 {
-  struct TunnelState *state = *tunnel_ctx;
+  struct ChannelState *state = *channel_ctx;
   const struct GNUNET_EXIT_IcmpInternetMessage *msg;
   uint16_t pkt_len = ntohs (message->size);
   const struct in_addr *v4;
@@ -2311,7 +2311,7 @@
   }
   if (GNUNET_SYSERR == state->is_dns)
   {
-    /* tunnel is UDP/TCP from now on */
+    /* channel is UDP/TCP from now on */
     state->is_dns = GNUNET_NO;
   }
   GNUNET_STATISTICS_update (stats,
@@ -2332,7 +2332,7 @@
   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 */
+    /* other peer switched AF on this channel; not allowed */
     GNUNET_break_op (0);
     return GNUNET_SYSERR;
   }
@@ -2489,7 +2489,7 @@
  * @return number of bytes of payload we created in buf
  */
 static uint16_t
-make_up_icmp_service_payload (struct TunnelState *state,
+make_up_icmp_service_payload (struct ChannelState *state,
                              char *buf)
 {
   switch (state->specifics.tcp_udp.serv->address.af)
@@ -2534,19 +2534,19 @@
  * offered by this system.
  *
  * @param cls closure, NULL
- * @param tunnel connection to the other end
- * @param tunnel_ctx pointer to our 'struct TunnelState *'
+ * @param channel connection to the other end
+ * @param channel_ctx pointer to our 'struct ChannelState *'
  * @param message the actual message
  * @return #GNUNET_OK to keep the connection open,
  *         #GNUNET_SYSERR to close it (signal serious error)
  */
 static int
 receive_icmp_service (void *cls,
-                      struct GNUNET_MESH_Tunnel *tunnel,
-                     void **tunnel_ctx,
+                      struct GNUNET_MESH_Channel *channel,
+                     void **channel_ctx,
                      const struct GNUNET_MessageHeader *message)
 {
-  struct TunnelState *state = *tunnel_ctx;
+  struct ChannelState *state = *channel_ctx;
   const struct GNUNET_EXIT_IcmpServiceMessage *msg;
   uint16_t pkt_len = ntohs (message->size);
   struct GNUNET_TUN_IcmpHeader icmp;
@@ -2560,7 +2560,7 @@
   }
   if (GNUNET_SYSERR == state->is_dns)
   {
-    /* tunnel is UDP/TCP from now on */
+    /* channel is UDP/TCP from now on */
     state->is_dns = GNUNET_NO;
   }
   GNUNET_STATISTICS_update (stats,
@@ -2822,19 +2822,19 @@
  * Process a request to forward UDP data to the Internet via this peer.
  *
  * @param cls closure, NULL
- * @param tunnel connection to the other end
- * @param tunnel_ctx pointer to our 'struct TunnelState *'
+ * @param channel connection to the other end
+ * @param channel_ctx pointer to our 'struct ChannelState *'
  * @param message the actual message
  * @return #GNUNET_OK to keep the connection open,
  *         #GNUNET_SYSERR to close it (signal serious error)
  */
 static int
 receive_udp_remote (void *cls,
-                    struct GNUNET_MESH_Tunnel *tunnel,
-                    void **tunnel_ctx,
+                    struct GNUNET_MESH_Channel *channel,
+                    void **channel_ctx,
                     const struct GNUNET_MessageHeader *message)
 {
-  struct TunnelState *state = *tunnel_ctx;
+  struct ChannelState *state = *channel_ctx;
   const struct GNUNET_EXIT_UdpInternetMessage *msg;
   uint16_t pkt_len = ntohs (message->size);
   const struct in_addr *v4;
@@ -2849,7 +2849,7 @@
   }
   if (GNUNET_SYSERR == state->is_dns)
   {
-    /* tunnel is UDP/TCP from now on */
+    /* channel is UDP/TCP from now on */
     state->is_dns = GNUNET_NO;
   }
   GNUNET_STATISTICS_update (stats,
@@ -2933,19 +2933,19 @@
  * offered by this system.
  *
  * @param cls closure, NULL
- * @param tunnel connection to the other end
- * @param tunnel_ctx pointer to our 'struct TunnelState *'
+ * @param channel connection to the other end
+ * @param channel_ctx pointer to our 'struct ChannelState *'
  * @param message the actual message
  * @return #GNUNET_OK to keep the connection open,
  *         #GNUNET_SYSERR to close it (signal serious error)
  */
 static int
 receive_udp_service (void *cls,
-                     struct GNUNET_MESH_Tunnel *tunnel,
-                     void **tunnel_ctx,
+                     struct GNUNET_MESH_Channel *channel,
+                     void **channel_ctx,
                      const struct GNUNET_MessageHeader *message)
 {
-  struct TunnelState *state = *tunnel_ctx;
+  struct ChannelState *state = *channel_ctx;
   const struct GNUNET_EXIT_UdpServiceMessage *msg;
   uint16_t pkt_len = ntohs (message->size);
 
@@ -2956,7 +2956,7 @@
   }
   if (GNUNET_SYSERR == state->is_dns)
   {
-    /* tunnel is UDP/TCP from now on */
+    /* channel is UDP/TCP from now on */
     state->is_dns = GNUNET_NO;
   }
   GNUNET_STATISTICS_update (stats,
@@ -3002,54 +3002,54 @@
 
 
 /**
- * Callback from GNUNET_MESH for new tunnels.
+ * Callback from GNUNET_MESH for new channels.
  *
  * @param cls closure
- * @param tunnel new handle to the tunnel
- * @param initiator peer that started the tunnel
+ * @param channel new handle to the channel
+ * @param initiator peer that started the channel
  * @param port destination port
- * @return initial tunnel context for the tunnel
+ * @return initial channel context for the channel
  */
 static void *
-new_tunnel (void *cls,
-            struct GNUNET_MESH_Tunnel *tunnel,
+new_channel (void *cls,
+            struct GNUNET_MESH_Channel *channel,
             const struct GNUNET_PeerIdentity *initiator,
             uint32_t port)
 {
-  struct TunnelState *s = GNUNET_new (struct TunnelState);
+  struct ChannelState *s = GNUNET_new (struct ChannelState);
 
   s->is_dns = GNUNET_SYSERR;
   s->peer = *initiator;
   GNUNET_STATISTICS_update (stats,
-                           gettext_noop ("# Inbound MESH tunnels created"),
+                           gettext_noop ("# Inbound MESH channels created"),
                            1, GNUNET_NO);
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-             "Received inbound tunnel from `%s'\n",
+             "Received inbound channel from `%s'\n",
              GNUNET_i2s (initiator));
-  s->tunnel = tunnel;
+  s->channel = channel;
   return s;
 }
 
 
 /**
- * Function called by mesh whenever an inbound tunnel is destroyed.
+ * Function called by mesh whenever an inbound channel is destroyed.
  * Should clean up any associated state.
  *
  * @param cls closure (set from #GNUNET_MESH_connect)
- * @param tunnel connection to the other end (henceforth invalid)
- * @param tunnel_ctx place where local state associated
- *                   with the tunnel is stored
+ * @param channel connection to the other end (henceforth invalid)
+ * @param channel_ctx place where local state associated
+ *                   with the channel is stored
  */
 static void
-clean_tunnel (void *cls,
-              const struct GNUNET_MESH_Tunnel *tunnel,
-              void *tunnel_ctx)
+clean_channel (void *cls,
+              const struct GNUNET_MESH_Channel *channel,
+              void *channel_ctx)
 {
-  struct TunnelState *s = tunnel_ctx;
-  struct TunnelMessageQueue *tnq;
+  struct ChannelState *s = channel_ctx;
+  struct ChannelMessageQueue *tnq;
 
   LOG (GNUNET_ERROR_TYPE_DEBUG,
-       "Tunnel destroyed\n");
+       "Channel destroyed\n");
   if (GNUNET_SYSERR == s->is_dns)
   {
     GNUNET_free (s);
@@ -3057,8 +3057,8 @@
   }
   if (GNUNET_YES == s->is_dns)
   {
-    if (tunnels[s->specifics.dns.my_id] == s)
-      tunnels[s->specifics.dns.my_id] = NULL;
+    if (channels[s->specifics.dns.my_id] == s)
+      channels[s->specifics.dns.my_id] = NULL;
     GNUNET_free_non_null (s->specifics.dns.reply);
   }
   else
@@ -3703,8 +3703,8 @@
   connections_heap = GNUNET_CONTAINER_heap_create 
(GNUNET_CONTAINER_HEAP_ORDER_MIN);
   mesh_handle
     = GNUNET_MESH_connect (cfg, NULL,
-                          &new_tunnel,
-                          &clean_tunnel, handlers,
+                          &new_channel,
+                          &clean_channel, handlers,
                            apptypes); // FIXME use ports
   if (NULL == mesh_handle)
   {

Deleted: gnunet/src/include/gnunet_mesh_service.h
===================================================================
--- gnunet/src/include/gnunet_mesh_service.h    2013-11-14 13:30:29 UTC (rev 
30696)
+++ gnunet/src/include/gnunet_mesh_service.h    2013-11-14 13:55:12 UTC (rev 
30697)
@@ -1,457 +0,0 @@
-/*
-     This file is part of GNUnet.
-     (C) 2009, 2010, 2011, 2012, 2013 Christian Grothoff (and other 
contributing authors)
-
-     GNUnet is free software; you can redistribute it and/or modify
-     it under the terms of the GNU General Public License as published
-     by the Free Software Foundation; either version 3, or (at your
-     option) any later version.
-
-     GNUnet is distributed in the hope that it will be useful, but
-     WITHOUT ANY WARRANTY; without even the implied warranty of
-     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
-     General Public License for more details.
-
-     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.
-*/
-
-/**
- * @file include/gnunet_mesh_service.h
- * @brief mesh service; establish tunnels to distant peers
- * @author Christian Grothoff
- *
- * TODO:
- * - need to do sanity check that this is consistent
- *   with current ideas for the multicast layer's needs
- */
-
-#ifndef GNUNET_MESH_SERVICE_H
-#define GNUNET_MESH_SERVICE_H
-
-#ifdef __cplusplus
-extern "C"
-{
-#if 0                           /* keep Emacsens' auto-indent happy */
-}
-#endif
-#endif
-
-#include "gnunet_util_lib.h"
-#include "gnunet_transport_service.h"
-
-/**
- * Version number of GNUnet-mesh API.
- */
-#define GNUNET_MESH_VERSION 0x00000002
-
-
-/**
- * Opaque handle to the service.
- */
-struct GNUNET_MESH_Handle;
-
-/**
- * Opaque handle to a tunnel.
- */
-struct GNUNET_MESH_Tunnel;
-
-
-/**
- * Options for querying a tunnel.
- * Second line indicates filed in the MeshTunnelInfo union carrying the answer.
- */
-enum MeshTunnelOption
-{
-  /**
-   * Disable buffering on intermediate nodes (for minimum latency).
-   * Yes/No.
-   */
-  GNUNET_MESH_OPTION_NOBUFFER   = 0x1,
-
-  /**
-   * Enable tunnel reliability, lost messages will be retransmitted.
-   * Yes/No.
-   */
-  GNUNET_MESH_OPTION_RELIABLE   = 0x2,
-
-  /**
-   * Enable out of order delivery of messages.
-   * Yes/No.
-   */
-  GNUNET_MESH_OPTION_OOORDER    = 0x4,
-
-  /**
-   * Who is the peer at the other end of the tunnel.
-   * struct GNUNET_PeerIdentity *peer
-   */
-  GNUNET_MESH_OPTION_PEER       = 0x8
-
-};
-
-
-/**
- * Functions with this signature are called whenever a message is
- * received.
- *
- * Each time the function must call #GNUNET_MESH_receive_done on the tunnel
- * in order to receive the next message. This doesn't need to be immediate:
- * can be delayed if some processing is done on the message.
- *
- * @param cls Closure (set from #GNUNET_MESH_connect).
- * @param tunnel Connection to the other end.
- * @param tunnel_ctx Place to store local state associated with the tunnel.
- * @param message The actual message.
- *
- * @return #GNUNET_OK to keep the tunnel open,
- *         #GNUNET_SYSERR to close it (signal serious error).
- */
-typedef int (*GNUNET_MESH_MessageCallback) (void *cls,
-                                            struct GNUNET_MESH_Tunnel *tunnel,
-                                            void **tunnel_ctx,
-                                            const struct GNUNET_MessageHeader 
*message);
-
-
-/**
- * Message handler.  Each struct specifies how to handle on particular
- * type of message received.
- */
-struct GNUNET_MESH_MessageHandler
-{
-  /**
-   * Function to call for messages of "type".
-   */
-  GNUNET_MESH_MessageCallback callback;
-
-  /**
-   * Type of the message this handler covers.
-   */
-  uint16_t type;
-
-  /**
-   * Expected size of messages of this type.  Use 0 for variable-size.
-   * If non-zero, messages of the given type will be discarded if they
-   * do not have the right size.
-   */
-  uint16_t expected_size;
-};
-
-
-/**
- * Method called whenever another peer has added us to a tunnel
- * the other peer initiated.
- * Only called (once) upon reception of data with a message type which was
- * subscribed to in #GNUNET_MESH_connect. A call to #GNUNET_MESH_tunnel_destroy
- * causes te tunnel to be ignored and no further notifications are sent about
- * the same tunnel.
- *
- * @param cls closure
- * @param tunnel new handle to the tunnel
- * @param initiator peer that started the tunnel
- * @param port Port this tunnel is for.
- * @return initial tunnel context for the tunnel
- *         (can be NULL -- that's not an error)
- */
-typedef void *(GNUNET_MESH_InboundTunnelNotificationHandler) (void *cls,
-                                                              struct 
GNUNET_MESH_Tunnel *tunnel,
-                                                              const struct
-                                                              
GNUNET_PeerIdentity
-                                                              * initiator,
-                                                              uint32_t port);
-
-
-/**
- * Function called whenever a tunnel is destroyed.  Should clean up
- * any associated state.
- *
- * It must NOT call #GNUNET_MESH_tunnel_destroy on the tunnel.
- *
- * @param cls closure (set from #GNUNET_MESH_connect)
- * @param tunnel connection to the other end (henceforth invalid)
- * @param tunnel_ctx place where local state associated
- *                   with the tunnel is stored
- */
-typedef void (GNUNET_MESH_TunnelEndHandler) (void *cls,
-                                             const struct GNUNET_MESH_Tunnel *
-                                             tunnel, void *tunnel_ctx);
-
-
-/**
- * Connect to the mesh service.
- *
- * @param cfg Configuration to use.
- * @param cls Closure for the various callbacks that follow (including
- *            handlers in the handlers array).
- * @param new_tunnel Function called when an *incoming* tunnel is created.
- *                   Can be NULL if no inbound tunnels are desired.
- *                   See @c ports.
- * @param cleaner Function called when a tunnel is destroyed by the remote 
peer.
- *                It is NOT called if #GNUNET_MESH_tunnel_destroy is called on
- *                the tunnel.
- * @param handlers Callbacks for messages we care about, NULL-terminated. Each
- *                 one must call #GNUNET_MESH_receive_done on the tunnel to
- *                 receive the next message.  Messages of a type that is not
- *                 in the handlers array are ignored if received.
- * @param ports NULL or 0-terminated array of port numbers for incoming 
tunnels.
- *              See @c new_tunnel.
- *
- * @return handle to the mesh service NULL on error
- *         (in this case, init is never called)
- */
-struct GNUNET_MESH_Handle *
-GNUNET_MESH_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
-                    void *cls,
-                     GNUNET_MESH_InboundTunnelNotificationHandler new_tunnel,
-                     GNUNET_MESH_TunnelEndHandler cleaner,
-                     const struct GNUNET_MESH_MessageHandler *handlers,
-                     const uint32_t *ports);
-
-
-/**
- * Disconnect from the mesh service. All tunnels will be destroyed. All tunnel
- * disconnect callbacks will be called on any still connected peers, notifying
- * about their disconnection. The registered inbound tunnel cleaner will be
- * called should any inbound tunnels still exist.
- *
- * @param handle connection to mesh to disconnect
- */
-void
-GNUNET_MESH_disconnect (struct GNUNET_MESH_Handle *handle);
-
-
-/**
- * Create a new tunnel (we're initiator and will be allowed to add/remove peers
- * and to broadcast).
- *
- * @param h mesh handle
- * @param tunnel_ctx client's tunnel context to associate with the tunnel
- * @param peer peer identity the tunnel should go to
- * @param port Port number.
- * @param nobuffer Flag for disabling buffering on relay nodes.
- * @param reliable Flag for end-to-end reliability.
- * @return handle to the tunnel
- */
-struct GNUNET_MESH_Tunnel *
-GNUNET_MESH_tunnel_create (struct GNUNET_MESH_Handle *h,
-                           void *tunnel_ctx,
-                           const struct GNUNET_PeerIdentity *peer,
-                           uint32_t port,
-                           int nobuffer,
-                           int reliable);
-
-
-/**
- * Destroy an existing tunnel.
- *
- * The existing end callback for the tunnel will be called immediately.
- * Any pending outgoing messages will be sent but no incoming messages will be
- * accepted and no data callbacks will be called.
- *
- * @param tunnel Tunnel handle, becomes invalid after this call.
- */
-void
-GNUNET_MESH_tunnel_destroy (struct GNUNET_MESH_Tunnel *tunnel);
-
-
-/**
- * Struct to retrieve info about a tunnel.
- */
-union GNUNET_MESH_TunnelInfo
-{
-
-  /**
-   * #GNUNET_YES / #GNUNET_NO, for binary flags.
-   */
-  int yes_no;
-
-  /**
-   * Peer on the other side of the tunnel
-   */
-  const struct GNUNET_PeerIdentity peer;
-};
-
-
-/**
- * Get information about a tunnel.
- *
- * @param tunnel Tunnel handle.
- * @param option Query, as listed in src/mesh/mesh.h (GNUNET_MESH_OPTION_*)
- * @param ... dependant on option, currently not used
- *
- * @return Union with an answer to the query.
- */
-const union GNUNET_MESH_TunnelInfo *
-GNUNET_MESH_tunnel_get_info (struct GNUNET_MESH_Tunnel *tunnel,
-                             enum MeshTunnelOption option, ...);
-
-
-/**
- * Handle for a transmission request.
- */
-struct GNUNET_MESH_TransmitHandle;
-
-
-/**
- * Ask the mesh to call @a notify once it is ready to transmit the
- * given number of bytes to the specified tunnel.
- * Only one call can be active at any time, to issue another request,
- * wait for the callback or cancel the current request.
- *
- * @param tunnel tunnel to use for transmission
- * @param cork is corking allowed for this transmission?
- * @param maxdelay how long can the message wait?
- * @param notify_size how many bytes of buffer space does @a notify want?
- * @param notify function to call when buffer space is available;
- *        will be called with NULL on timeout or if the overall queue
- *        for this peer is larger than queue_size and this is currently
- *        the message with the lowest priority
- * @param notify_cls closure for @a notify
- * @return non-NULL if the notify callback was queued,
- *         NULL if we can not even queue the request (insufficient
- *         memory); if NULL is returned, "notify" will NOT be called.
- */
-struct GNUNET_MESH_TransmitHandle *
-GNUNET_MESH_notify_transmit_ready (struct GNUNET_MESH_Tunnel *tunnel, int cork,
-                                   struct GNUNET_TIME_Relative maxdelay,
-                                   size_t notify_size,
-                                   GNUNET_CONNECTION_TransmitReadyNotify 
notify,
-                                   void *notify_cls);
-
-
-/**
- * Cancel the specified transmission-ready notification.
- *
- * @param th handle that was returned by "notify_transmit_ready".
- */
-void
-GNUNET_MESH_notify_transmit_ready_cancel (struct GNUNET_MESH_TransmitHandle
-                                          *th);
-
-
-/**
- * Indicate readiness to receive the next message on a tunnel.
- *
- * Should only be called once per handler called.
- *
- * @param tunnel Tunnel that will be allowed to call another handler.
- */
-void
-GNUNET_MESH_receive_done (struct GNUNET_MESH_Tunnel *tunnel);
-
-
-
-/******************************************************************************/
-/********************       MONITORING /DEBUG API     
*************************/
-/******************************************************************************/
-/* The following calls are not useful for normal MESH operation, but for      
*/
-/* debug and monitoring of the mesh state. They can be safely ignored.        
*/
-/* The API can change at any point without notice.                            
*/
-/* Please contact the developer if you consider any of this calls useful for  
*/
-/* normal mesh applications.                                                  
*/
-/******************************************************************************/
-
-/**
- * Method called to retrieve information about each tunnel the mesh peer
- * is aware of.
- *
- * @param cls Closure.
- * @param tunnel_number Tunnel number.
- * @param origin that started the tunnel (owner).
- * @param target other endpoint of the tunnel
- */
-typedef void (*GNUNET_MESH_TunnelsCB) (void *cls,
-                                       uint32_t tunnel_number,
-                                       const struct GNUNET_PeerIdentity 
*origin,
-                                       const struct GNUNET_PeerIdentity 
*target);
-
-
-/**
- * Method called to retrieve information about a specific tunnel the mesh peer
- * is aware of, including all transit nodes.
- *
- * @param cls Closure.
- * @param peer Peer in the tunnel's tree.
- * @param parent Parent of the current peer. All 0 when peer is root.
- */
-typedef void (*GNUNET_MESH_TunnelCB) (void *cls,
-                                      const struct GNUNET_PeerIdentity *peer,
-                                      const struct GNUNET_PeerIdentity 
*parent);
-
-
-/**
- * Request information about the running mesh peer.
- * The callback will be called for every tunnel known to the service,
- * listing all active peers that belong to the tunnel.
- *
- * If called again on the same handle, it will overwrite the previous
- * callback and cls. To retrieve the cls, monitor_cancel must be
- * called first.
- *
- * WARNING: unstable API, likely to change in the future!
- *
- * @param h Handle to the mesh peer.
- * @param callback Function to call with the requested data.
- * @param callback_cls Closure for @c callback.
- */
-void
-GNUNET_MESH_get_tunnels (struct GNUNET_MESH_Handle *h,
-                         GNUNET_MESH_TunnelsCB callback,
-                         void *callback_cls);
-
-
-/**
- * Request information about a specific tunnel of the running mesh peer.
- *
- * WARNING: unstable API, likely to change in the future!
- *
- * @param h Handle to the mesh peer.
- * @param initiator ID of the owner of the tunnel.
- * @param tunnel_number Tunnel number.
- * @param callback Function to call with the requested data.
- * @param callback_cls Closure for @a callback.
- */
-void
-GNUNET_MESH_show_tunnel (struct GNUNET_MESH_Handle *h,
-                         struct GNUNET_PeerIdentity *initiator,
-                         uint32_t tunnel_number,
-                         GNUNET_MESH_TunnelCB callback,
-                         void *callback_cls);
-
-
-/**
- * Cancel a monitor request. The monitor callback will not be called.
- *
- * WARNING: unstable API, likely to change in the future!
- *
- * @param h Mesh handle.
- *
- * @return Closure given to GNUNET_MESH_monitor, if any.
- */
-void *
-GNUNET_MESH_get_tunnels_cancel (struct GNUNET_MESH_Handle *h);
-
-
-/**
- * Create a message queue for a mesh tunnel.
- * The message queue can only be used to transmit messages,
- * not to receive them.
- *
- * @param tunnel the tunnel to create the message queue for
- * @return a message queue for sending messages over the tunnel
- */
-struct GNUNET_MQ_Handle *
-GNUNET_MESH_mq_create (struct GNUNET_MESH_Tunnel *tunnel);
-
-
-#if 0                           /* keep Emacsens' auto-indent happy */
-{
-#endif
-#ifdef __cplusplus
-}
-#endif
-
-/* ifndef GNUNET_MESH_SERVICE_H */
-#endif
-/* end of gnunet_mesh_service.h */

Copied: gnunet/src/include/gnunet_mesh_service.h (from rev 30696, 
gnunet/src/include/gnunet_mesh_service_enc.h)
===================================================================
--- gnunet/src/include/gnunet_mesh_service.h                            (rev 0)
+++ gnunet/src/include/gnunet_mesh_service.h    2013-11-14 13:55:12 UTC (rev 
30697)
@@ -0,0 +1,457 @@
+/*
+     This file is part of GNUnet.
+     (C) 2009 - 2013 Christian Grothoff (and other contributing authors)
+
+     GNUnet is free software; you can redistribute it and/or modify
+     it under the terms of the GNU General Public License as published
+     by the Free Software Foundation; either version 3, or (at your
+     option) any later version.
+
+     GNUnet is distributed in the hope that it will be useful, but
+     WITHOUT ANY WARRANTY; without even the implied warranty of
+     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+     General Public License for more details.
+
+     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.
+*/
+
+/**
+ * @file include/gnunet_mesh_service_enc.h
+ * @brief mesh service; establish channels to distant peers
+ * @author Christian Grothoff
+ */
+
+#ifndef GNUNET_MESH_SERVICE_H
+#define GNUNET_MESH_SERVICE_H
+
+#ifdef __cplusplus
+extern "C"
+{
+#if 0                           /* keep Emacsens' auto-indent happy */
+}
+#endif
+#endif
+
+#include "gnunet_util_lib.h"
+#include "gnunet_transport_service.h"
+
+/**
+ * Version number of GNUnet-mesh API.
+ */
+#define GNUNET_MESH_VERSION 0x00000003
+
+
+/**
+ * Opaque handle to the service.
+ */
+struct GNUNET_MESH_Handle;
+
+/**
+ * Opaque handle to a channel.
+ */
+struct GNUNET_MESH_Channel;
+
+
+/**
+ * Channel options.
+ * Second line indicates filed in the MeshChannelInfo union carrying the 
answer.
+ */
+enum MeshOption
+{
+  /**
+   * Disable buffering on intermediate nodes (for minimum latency).
+   * Yes/No.
+   */
+  GNUNET_MESH_OPTION_NOBUFFER   = 0x1,
+
+  /**
+   * Enable channel reliability, lost messages will be retransmitted.
+   * Yes/No.
+   */
+  GNUNET_MESH_OPTION_RELIABLE   = 0x2,
+
+  /**
+   * Enable out of order delivery of messages.
+   * Yes/No.
+   */
+  GNUNET_MESH_OPTION_OOORDER    = 0x4,
+
+  /**
+   * Who is the peer at the other end of the channel.
+   * struct GNUNET_PeerIdentity *peer
+   */
+  GNUNET_MESH_OPTION_PEER       = 0x8
+
+};
+
+
+/**
+ * Functions with this signature are called whenever a message is
+ * received.
+ *
+ * Each time the function must call #GNUNET_MESH_receive_done on the channel
+ * in order to receive the next message. This doesn't need to be immediate:
+ * can be delayed if some processing is done on the message.
+ *
+ * @param cls Closure (set from #GNUNET_MESH_connect).
+ * @param channel Connection to the other end.
+ * @param channel_ctx Place to store local state associated with the channel.
+ * @param message The actual message.
+ * @return #GNUNET_OK to keep the channel open,
+ *         #GNUNET_SYSERR to close it (signal serious error).
+ */
+typedef int (*GNUNET_MESH_MessageCallback) (void *cls,
+                                            struct GNUNET_MESH_Channel 
*channel,
+                                            void **channel_ctx,
+                                            const struct GNUNET_MessageHeader 
*message);
+
+
+/**
+ * Message handler.  Each struct specifies how to handle on particular
+ * type of message received.
+ */
+struct GNUNET_MESH_MessageHandler
+{
+  /**
+   * Function to call for messages of "type".
+   */
+  GNUNET_MESH_MessageCallback callback;
+
+  /**
+   * Type of the message this handler covers.
+   */
+  uint16_t type;
+
+  /**
+   * Expected size of messages of this type.  Use 0 for variable-size.
+   * If non-zero, messages of the given type will be discarded if they
+   * do not have the right size.
+   */
+  uint16_t expected_size;
+};
+
+
+/**
+ * Method called whenever another peer has added us to a channel
+ * the other peer initiated.
+ * Only called (once) upon reception of data with a message type which was
+ * subscribed to in #GNUNET_MESH_connect. A call to 
#GNUNET_MESH_channel_destroy
+ * causes te channel to be ignored and no further notifications are sent about
+ * the same channel.
+ *
+ * @param cls closure
+ * @param channel new handle to the channel
+ * @param initiator peer that started the channel
+ * @param port Port this channel is for.
+ * @return initial channel context for the channel
+ *         (can be NULL -- that's not an error)
+ */
+typedef void *(GNUNET_MESH_InboundChannelNotificationHandler) (void *cls,
+                                                               struct
+                                                               
GNUNET_MESH_Channel
+                                                               * channel,
+                                                               const struct
+                                                               
GNUNET_PeerIdentity
+                                                               * initiator,
+                                                               uint32_t port);
+
+
+/**
+ * Function called whenever a channel is destroyed.  Should clean up
+ * any associated state.
+ *
+ * It must NOT call #GNUNET_MESH_channel_destroy on the channel.
+ *
+ * @param cls closure (set from #GNUNET_MESH_connect)
+ * @param channel connection to the other end (henceforth invalid)
+ * @param channel_ctx place where local state associated
+ *                   with the channel is stored
+ */
+typedef void (GNUNET_MESH_ChannelEndHandler) (void *cls,
+                                              const struct GNUNET_MESH_Channel 
*
+                                              channel,
+                                              void *channel_ctx);
+
+
+/**
+ * Connect to the mesh service.
+ *
+ * @param cfg Configuration to use.
+ * @param cls Closure for the various callbacks that follow (including
+ *            handlers in the handlers array).
+ * @param new_channel Function called when an *incoming* channel is created.
+ *                    Can be NULL if no inbound channels are desired.
+ *                    See @a ports.
+ * @param cleaner Function called when a channel is destroyed by the remote 
peer.
+ *                It is NOT called if #GNUNET_MESH_channel_destroy is called on
+ *                the channel.
+ * @param handlers Callbacks for messages we care about, NULL-terminated. Each
+ *                 one must call #GNUNET_MESH_receive_done on the channel to
+ *                 receive the next message.  Messages of a type that is not
+ *                 in the handlers array are ignored if received.
+ * @param ports NULL or 0-terminated array of port numbers for incoming 
channels.
+ *              See @a new_channel.
+ *
+ * @return handle to the mesh service NULL on error
+ *         (in this case, init is never called)
+ */
+struct GNUNET_MESH_Handle *
+GNUNET_MESH_connect (const struct GNUNET_CONFIGURATION_Handle *cfg, void *cls,
+                     GNUNET_MESH_InboundChannelNotificationHandler new_channel,
+                     GNUNET_MESH_ChannelEndHandler cleaner,
+                     const struct GNUNET_MESH_MessageHandler *handlers,
+                     const uint32_t *ports);
+
+
+/**
+ * Disconnect from the mesh service. All channels will be destroyed. All 
channel
+ * disconnect callbacks will be called on any still connected peers, notifying
+ * about their disconnection. The registered inbound channel cleaner will be
+ * called should any inbound channels still exist.
+ *
+ * @param handle connection to mesh to disconnect
+ */
+void
+GNUNET_MESH_disconnect (struct GNUNET_MESH_Handle *handle);
+
+
+/**
+ * Create a new channel towards a remote peer.
+ *
+ * If the destination port is not open by any peer or the destination peer
+ * does not accept the channel, #GNUNET_MESH_ChannelEndHandler will be called
+ * for this channel.
+ *
+ * @param h mesh handle
+ * @param channel_ctx client's channel context to associate with the channel
+ * @param peer peer identity the channel should go to
+ * @param port Port number.
+ * @param nobuffer Flag for disabling buffering on relay nodes.
+ * @param reliable Flag for end-to-end reliability.
+ * @return handle to the channel
+ */
+struct GNUNET_MESH_Channel *
+GNUNET_MESH_channel_create (struct GNUNET_MESH_Handle *h,
+                            void *channel_ctx,
+                            const struct GNUNET_PeerIdentity *peer,
+                            uint32_t port,
+                            int nobuffer,
+                            int reliable);
+
+
+/**
+ * Destroy an existing channel.
+ *
+ * The existing end callback for the channel will be called immediately.
+ * Any pending outgoing messages will be sent but no incoming messages will be
+ * accepted and no data callbacks will be called.
+ *
+ * @param channel Channel handle, becomes invalid after this call.
+ */
+void
+GNUNET_MESH_channel_destroy (struct GNUNET_MESH_Channel *channel);
+
+
+/**
+ * Struct to retrieve info about a channel.
+ */
+union GNUNET_MESH_ChannelInfo
+{
+
+  /**
+   * #GNUNET_YES / #GNUNET_NO, for binary flags.
+   */
+  int yes_no;
+
+  /**
+   * Peer on the other side of the channel
+   */
+  const struct GNUNET_PeerIdentity *peer;
+};
+
+
+/**
+ * Get information about a channel.
+ *
+ * @param channel Channel handle.
+ * @param option Query type GNUNET_MESH_OPTION_*
+ * @param ... dependant on option, currently not used
+ * @return Union with an answer to the query.
+ */
+const union GNUNET_MESH_ChannelInfo *
+GNUNET_MESH_channel_get_info (struct GNUNET_MESH_Channel *channel,
+                              enum MeshOption option, ...);
+
+
+/**
+ * Handle for a transmission request.
+ */
+struct GNUNET_MESH_TransmitHandle;
+
+
+/**
+ * Ask the mesh to call @a notify once it is ready to transmit the
+ * given number of bytes to the specified channel.
+ * Only one call can be active at any time, to issue another request,
+ * wait for the callback or cancel the current request.
+ *
+ * @param channel channel to use for transmission
+ * @param cork is corking allowed for this transmission?
+ * @param maxdelay how long can the message wait?
+ * @param notify_size how many bytes of buffer space does notify want?
+ * @param notify function to call when buffer space is available;
+ *        will be called with NULL on timeout or if the overall queue
+ *        for this peer is larger than queue_size and this is currently
+ *        the message with the lowest priority
+ * @param notify_cls closure for @a notify
+ * @return non-NULL if the notify callback was queued,
+ *         NULL if we can not even queue the request (insufficient
+ *         memory); if NULL is returned, @a notify will NOT be called.
+ */
+struct GNUNET_MESH_TransmitHandle *
+GNUNET_MESH_notify_transmit_ready (struct GNUNET_MESH_Channel *channel,
+                                   int cork,
+                                   struct GNUNET_TIME_Relative maxdelay,
+                                   size_t notify_size,
+                                   GNUNET_CONNECTION_TransmitReadyNotify 
notify,
+                                   void *notify_cls);
+
+
+/**
+ * Cancel the specified transmission-ready notification.
+ *
+ * @param th handle that was returned by "notify_transmit_ready".
+ */
+void
+GNUNET_MESH_notify_transmit_ready_cancel (struct GNUNET_MESH_TransmitHandle
+                                          *th);
+
+
+/**
+ * Indicate readiness to receive the next message on a channel.
+ *
+ * Should only be called once per handler called.
+ *
+ * @param channel Channel that will be allowed to call another handler.
+ */
+void
+GNUNET_MESH_receive_done (struct GNUNET_MESH_Channel *channel);
+
+
+
+/******************************************************************************/
+/********************       MONITORING /DEBUG API     
*************************/
+/******************************************************************************/
+/* The following calls are not useful for normal MESH operation, but for      
*/
+/* debug and monitoring of the mesh state. They can be safely ignored.        
*/
+/* The API can change at any point without notice.                            
*/
+/* Please contact the developer if you consider any of this calls useful for  
*/
+/* normal mesh applications.                                                  
*/
+/******************************************************************************/
+
+/**
+ * Method called to retrieve information about each channel the mesh peer
+ * is aware of.
+ *
+ * @param cls Closure.
+ * @param channel_number Channel number.
+ * @param origin that started the channel (owner).
+ * @param target other endpoint of the channel
+ */
+typedef void (*GNUNET_MESH_ChannelsCB) (void *cls,
+                                        uint32_t channel_number,
+                                        const struct GNUNET_PeerIdentity 
*origin,
+                                        const struct GNUNET_PeerIdentity 
*target);
+
+
+/**
+ * Method called to retrieve information about a specific channel the mesh peer
+ * is aware of, including all transit nodes.
+ *
+ * @param cls Closure.
+ * @param peer Peer in the channel's tree.
+ * @param parent Parent of the current peer. All 0 when peer is root.
+ */
+typedef void (*GNUNET_MESH_ChannelCB) (void *cls,
+                                      const struct GNUNET_PeerIdentity *peer,
+                                      const struct GNUNET_PeerIdentity 
*parent);
+
+
+/**
+ * Request information about the running mesh peer.
+ * The callback will be called for every channel known to the service,
+ * listing all active peers that belong to the channel.
+ *
+ * If called again on the same handle, it will overwrite the previous
+ * callback and cls. To retrieve the cls, monitor_cancel must be
+ * called first.
+ *
+ * WARNING: unstable API, likely to change in the future!
+ *
+ * @param h Handle to the mesh peer.
+ * @param callback Function to call with the requested data.
+ * @param callback_cls Closure for @c callback.
+ */
+void
+GNUNET_MESH_get_channels (struct GNUNET_MESH_Handle *h,
+                         GNUNET_MESH_ChannelsCB callback,
+                         void *callback_cls);
+
+
+/**
+ * Request information about a specific channel of the running mesh peer.
+ *
+ * WARNING: unstable API, likely to change in the future!
+ *
+ * @param h Handle to the mesh peer.
+ * @param initiator ID of the owner of the channel.
+ * @param channel_number Channel number.
+ * @param callback Function to call with the requested data.
+ * @param callback_cls Closure for @c callback.
+ */
+void
+GNUNET_MESH_show_channel (struct GNUNET_MESH_Handle *h,
+                         struct GNUNET_PeerIdentity *initiator,
+                         uint32_t channel_number,
+                         GNUNET_MESH_ChannelCB callback,
+                         void *callback_cls);
+
+
+/**
+ * Cancel a monitor request. The monitor callback will not be called.
+ *
+ * WARNING: unstable API, likely to change in the future!
+ *
+ * @param h Mesh handle.
+ *
+ * @return Closure given to GNUNET_MESH_monitor, if any.
+ */
+void *
+GNUNET_MESH_get_channels_cancel (struct GNUNET_MESH_Handle *h);
+
+
+/**
+ * Create a message queue for a mesh channel.
+ * The message queue can only be used to transmit messages,
+ * not to receive them.
+ *
+ * @param channel the channel to create the message qeue for
+ * @return a message queue to messages over the channel
+ */
+struct GNUNET_MQ_Handle *
+GNUNET_MESH_mq_create (struct GNUNET_MESH_Channel *channel);
+
+
+#if 0                           /* keep Emacsens' auto-indent happy */
+{
+#endif
+#ifdef __cplusplus
+}
+#endif
+
+/* ifndef GNUNET_MESH_SERVICE_H */
+#endif
+/* end of gnunet_mesh_service.h */

Deleted: gnunet/src/include/gnunet_mesh_service_enc.h
===================================================================
--- gnunet/src/include/gnunet_mesh_service_enc.h        2013-11-14 13:30:29 UTC 
(rev 30696)
+++ gnunet/src/include/gnunet_mesh_service_enc.h        2013-11-14 13:55:12 UTC 
(rev 30697)
@@ -1,457 +0,0 @@
-/*
-     This file is part of GNUnet.
-     (C) 2009 - 2013 Christian Grothoff (and other contributing authors)
-
-     GNUnet is free software; you can redistribute it and/or modify
-     it under the terms of the GNU General Public License as published
-     by the Free Software Foundation; either version 3, or (at your
-     option) any later version.
-
-     GNUnet is distributed in the hope that it will be useful, but
-     WITHOUT ANY WARRANTY; without even the implied warranty of
-     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
-     General Public License for more details.
-
-     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.
-*/
-
-/**
- * @file include/gnunet_mesh_service_enc.h
- * @brief mesh service; establish channels to distant peers
- * @author Christian Grothoff
- */
-
-#ifndef GNUNET_MESH_SERVICE_H
-#define GNUNET_MESH_SERVICE_H
-
-#ifdef __cplusplus
-extern "C"
-{
-#if 0                           /* keep Emacsens' auto-indent happy */
-}
-#endif
-#endif
-
-#include "gnunet_util_lib.h"
-#include "gnunet_transport_service.h"
-
-/**
- * Version number of GNUnet-mesh API.
- */
-#define GNUNET_MESH_VERSION 0x00000003
-
-
-/**
- * Opaque handle to the service.
- */
-struct GNUNET_MESH_Handle;
-
-/**
- * Opaque handle to a channel.
- */
-struct GNUNET_MESH_Channel;
-
-
-/**
- * Channel options.
- * Second line indicates filed in the MeshChannelInfo union carrying the 
answer.
- */
-enum MeshOption
-{
-  /**
-   * Disable buffering on intermediate nodes (for minimum latency).
-   * Yes/No.
-   */
-  GNUNET_MESH_OPTION_NOBUFFER   = 0x1,
-
-  /**
-   * Enable channel reliability, lost messages will be retransmitted.
-   * Yes/No.
-   */
-  GNUNET_MESH_OPTION_RELIABLE   = 0x2,
-
-  /**
-   * Enable out of order delivery of messages.
-   * Yes/No.
-   */
-  GNUNET_MESH_OPTION_OOORDER    = 0x4,
-
-  /**
-   * Who is the peer at the other end of the channel.
-   * struct GNUNET_PeerIdentity *peer
-   */
-  GNUNET_MESH_OPTION_PEER       = 0x8
-
-};
-
-
-/**
- * Functions with this signature are called whenever a message is
- * received.
- *
- * Each time the function must call #GNUNET_MESH_receive_done on the channel
- * in order to receive the next message. This doesn't need to be immediate:
- * can be delayed if some processing is done on the message.
- *
- * @param cls Closure (set from #GNUNET_MESH_connect).
- * @param channel Connection to the other end.
- * @param channel_ctx Place to store local state associated with the channel.
- * @param message The actual message.
- * @return #GNUNET_OK to keep the channel open,
- *         #GNUNET_SYSERR to close it (signal serious error).
- */
-typedef int (*GNUNET_MESH_MessageCallback) (void *cls,
-                                            struct GNUNET_MESH_Channel 
*channel,
-                                            void **channel_ctx,
-                                            const struct GNUNET_MessageHeader 
*message);
-
-
-/**
- * Message handler.  Each struct specifies how to handle on particular
- * type of message received.
- */
-struct GNUNET_MESH_MessageHandler
-{
-  /**
-   * Function to call for messages of "type".
-   */
-  GNUNET_MESH_MessageCallback callback;
-
-  /**
-   * Type of the message this handler covers.
-   */
-  uint16_t type;
-
-  /**
-   * Expected size of messages of this type.  Use 0 for variable-size.
-   * If non-zero, messages of the given type will be discarded if they
-   * do not have the right size.
-   */
-  uint16_t expected_size;
-};
-
-
-/**
- * Method called whenever another peer has added us to a channel
- * the other peer initiated.
- * Only called (once) upon reception of data with a message type which was
- * subscribed to in #GNUNET_MESH_connect. A call to 
#GNUNET_MESH_channel_destroy
- * causes te channel to be ignored and no further notifications are sent about
- * the same channel.
- *
- * @param cls closure
- * @param channel new handle to the channel
- * @param initiator peer that started the channel
- * @param port Port this channel is for.
- * @return initial channel context for the channel
- *         (can be NULL -- that's not an error)
- */
-typedef void *(GNUNET_MESH_InboundChannelNotificationHandler) (void *cls,
-                                                               struct
-                                                               
GNUNET_MESH_Channel
-                                                               * channel,
-                                                               const struct
-                                                               
GNUNET_PeerIdentity
-                                                               * initiator,
-                                                               uint32_t port);
-
-
-/**
- * Function called whenever a channel is destroyed.  Should clean up
- * any associated state.
- *
- * It must NOT call #GNUNET_MESH_channel_destroy on the channel.
- *
- * @param cls closure (set from #GNUNET_MESH_connect)
- * @param channel connection to the other end (henceforth invalid)
- * @param channel_ctx place where local state associated
- *                   with the channel is stored
- */
-typedef void (GNUNET_MESH_ChannelEndHandler) (void *cls,
-                                              const struct GNUNET_MESH_Channel 
*
-                                              channel,
-                                              void *channel_ctx);
-
-
-/**
- * Connect to the mesh service.
- *
- * @param cfg Configuration to use.
- * @param cls Closure for the various callbacks that follow (including
- *            handlers in the handlers array).
- * @param new_channel Function called when an *incoming* channel is created.
- *                    Can be NULL if no inbound channels are desired.
- *                    See @a ports.
- * @param cleaner Function called when a channel is destroyed by the remote 
peer.
- *                It is NOT called if #GNUNET_MESH_channel_destroy is called on
- *                the channel.
- * @param handlers Callbacks for messages we care about, NULL-terminated. Each
- *                 one must call #GNUNET_MESH_receive_done on the channel to
- *                 receive the next message.  Messages of a type that is not
- *                 in the handlers array are ignored if received.
- * @param ports NULL or 0-terminated array of port numbers for incoming 
channels.
- *              See @a new_channel.
- *
- * @return handle to the mesh service NULL on error
- *         (in this case, init is never called)
- */
-struct GNUNET_MESH_Handle *
-GNUNET_MESH_connect (const struct GNUNET_CONFIGURATION_Handle *cfg, void *cls,
-                     GNUNET_MESH_InboundChannelNotificationHandler new_channel,
-                     GNUNET_MESH_ChannelEndHandler cleaner,
-                     const struct GNUNET_MESH_MessageHandler *handlers,
-                     const uint32_t *ports);
-
-
-/**
- * Disconnect from the mesh service. All channels will be destroyed. All 
channel
- * disconnect callbacks will be called on any still connected peers, notifying
- * about their disconnection. The registered inbound channel cleaner will be
- * called should any inbound channels still exist.
- *
- * @param handle connection to mesh to disconnect
- */
-void
-GNUNET_MESH_disconnect (struct GNUNET_MESH_Handle *handle);
-
-
-/**
- * Create a new channel towards a remote peer.
- *
- * If the destination port is not open by any peer or the destination peer
- * does not accept the channel, #GNUNET_MESH_ChannelEndHandler will be called
- * for this channel.
- *
- * @param h mesh handle
- * @param channel_ctx client's channel context to associate with the channel
- * @param peer peer identity the channel should go to
- * @param port Port number.
- * @param nobuffer Flag for disabling buffering on relay nodes.
- * @param reliable Flag for end-to-end reliability.
- * @return handle to the channel
- */
-struct GNUNET_MESH_Channel *
-GNUNET_MESH_channel_create (struct GNUNET_MESH_Handle *h,
-                            void *channel_ctx,
-                            const struct GNUNET_PeerIdentity *peer,
-                            uint32_t port,
-                            int nobuffer,
-                            int reliable);
-
-
-/**
- * Destroy an existing channel.
- *
- * The existing end callback for the channel will be called immediately.
- * Any pending outgoing messages will be sent but no incoming messages will be
- * accepted and no data callbacks will be called.
- *
- * @param channel Channel handle, becomes invalid after this call.
- */
-void
-GNUNET_MESH_channel_destroy (struct GNUNET_MESH_Channel *channel);
-
-
-/**
- * Struct to retrieve info about a channel.
- */
-union GNUNET_MESH_ChannelInfo
-{
-
-  /**
-   * #GNUNET_YES / #GNUNET_NO, for binary flags.
-   */
-  int yes_no;
-
-  /**
-   * Peer on the other side of the channel
-   */
-  const struct GNUNET_PeerIdentity *peer;
-};
-
-
-/**
- * Get information about a channel.
- *
- * @param channel Channel handle.
- * @param option Query type GNUNET_MESH_OPTION_*
- * @param ... dependant on option, currently not used
- * @return Union with an answer to the query.
- */
-const union GNUNET_MESH_ChannelInfo *
-GNUNET_MESH_channel_get_info (struct GNUNET_MESH_Channel *channel,
-                              enum MeshOption option, ...);
-
-
-/**
- * Handle for a transmission request.
- */
-struct GNUNET_MESH_TransmitHandle;
-
-
-/**
- * Ask the mesh to call @a notify once it is ready to transmit the
- * given number of bytes to the specified channel.
- * Only one call can be active at any time, to issue another request,
- * wait for the callback or cancel the current request.
- *
- * @param channel channel to use for transmission
- * @param cork is corking allowed for this transmission?
- * @param maxdelay how long can the message wait?
- * @param notify_size how many bytes of buffer space does notify want?
- * @param notify function to call when buffer space is available;
- *        will be called with NULL on timeout or if the overall queue
- *        for this peer is larger than queue_size and this is currently
- *        the message with the lowest priority
- * @param notify_cls closure for @a notify
- * @return non-NULL if the notify callback was queued,
- *         NULL if we can not even queue the request (insufficient
- *         memory); if NULL is returned, @a notify will NOT be called.
- */
-struct GNUNET_MESH_TransmitHandle *
-GNUNET_MESH_notify_transmit_ready (struct GNUNET_MESH_Channel *channel,
-                                   int cork,
-                                   struct GNUNET_TIME_Relative maxdelay,
-                                   size_t notify_size,
-                                   GNUNET_CONNECTION_TransmitReadyNotify 
notify,
-                                   void *notify_cls);
-
-
-/**
- * Cancel the specified transmission-ready notification.
- *
- * @param th handle that was returned by "notify_transmit_ready".
- */
-void
-GNUNET_MESH_notify_transmit_ready_cancel (struct GNUNET_MESH_TransmitHandle
-                                          *th);
-
-
-/**
- * Indicate readiness to receive the next message on a channel.
- *
- * Should only be called once per handler called.
- *
- * @param channel Channel that will be allowed to call another handler.
- */
-void
-GNUNET_MESH_receive_done (struct GNUNET_MESH_Channel *channel);
-
-
-
-/******************************************************************************/
-/********************       MONITORING /DEBUG API     
*************************/
-/******************************************************************************/
-/* The following calls are not useful for normal MESH operation, but for      
*/
-/* debug and monitoring of the mesh state. They can be safely ignored.        
*/
-/* The API can change at any point without notice.                            
*/
-/* Please contact the developer if you consider any of this calls useful for  
*/
-/* normal mesh applications.                                                  
*/
-/******************************************************************************/
-
-/**
- * Method called to retrieve information about each channel the mesh peer
- * is aware of.
- *
- * @param cls Closure.
- * @param channel_number Channel number.
- * @param origin that started the channel (owner).
- * @param target other endpoint of the channel
- */
-typedef void (*GNUNET_MESH_ChannelsCB) (void *cls,
-                                        uint32_t channel_number,
-                                        const struct GNUNET_PeerIdentity 
*origin,
-                                        const struct GNUNET_PeerIdentity 
*target);
-
-
-/**
- * Method called to retrieve information about a specific channel the mesh peer
- * is aware of, including all transit nodes.
- *
- * @param cls Closure.
- * @param peer Peer in the channel's tree.
- * @param parent Parent of the current peer. All 0 when peer is root.
- */
-typedef void (*GNUNET_MESH_ChannelCB) (void *cls,
-                                      const struct GNUNET_PeerIdentity *peer,
-                                      const struct GNUNET_PeerIdentity 
*parent);
-
-
-/**
- * Request information about the running mesh peer.
- * The callback will be called for every channel known to the service,
- * listing all active peers that belong to the channel.
- *
- * If called again on the same handle, it will overwrite the previous
- * callback and cls. To retrieve the cls, monitor_cancel must be
- * called first.
- *
- * WARNING: unstable API, likely to change in the future!
- *
- * @param h Handle to the mesh peer.
- * @param callback Function to call with the requested data.
- * @param callback_cls Closure for @c callback.
- */
-void
-GNUNET_MESH_get_channels (struct GNUNET_MESH_Handle *h,
-                         GNUNET_MESH_ChannelsCB callback,
-                         void *callback_cls);
-
-
-/**
- * Request information about a specific channel of the running mesh peer.
- *
- * WARNING: unstable API, likely to change in the future!
- *
- * @param h Handle to the mesh peer.
- * @param initiator ID of the owner of the channel.
- * @param channel_number Channel number.
- * @param callback Function to call with the requested data.
- * @param callback_cls Closure for @c callback.
- */
-void
-GNUNET_MESH_show_channel (struct GNUNET_MESH_Handle *h,
-                         struct GNUNET_PeerIdentity *initiator,
-                         uint32_t channel_number,
-                         GNUNET_MESH_ChannelCB callback,
-                         void *callback_cls);
-
-
-/**
- * Cancel a monitor request. The monitor callback will not be called.
- *
- * WARNING: unstable API, likely to change in the future!
- *
- * @param h Mesh handle.
- *
- * @return Closure given to GNUNET_MESH_monitor, if any.
- */
-void *
-GNUNET_MESH_get_channels_cancel (struct GNUNET_MESH_Handle *h);
-
-
-/**
- * Create a message queue for a mesh channel.
- * The message queue can only be used to transmit messages,
- * not to receive them.
- *
- * @param channel the channel to create the message qeue for
- * @return a message queue to messages over the channel
- */
-struct GNUNET_MQ_Handle *
-GNUNET_MESH_mq_create (struct GNUNET_MESH_Channel *channel);
-
-
-#if 0                           /* keep Emacsens' auto-indent happy */
-{
-#endif
-#ifdef __cplusplus
-}
-#endif
-
-/* ifndef GNUNET_MESH_SERVICE_H */
-#endif
-/* end of gnunet_mesh_service.h */

Modified: gnunet/src/mesh/Makefile.am
===================================================================
--- gnunet/src/mesh/Makefile.am 2013-11-14 13:30:29 UTC (rev 30696)
+++ gnunet/src/mesh/Makefile.am 2013-11-14 13:55:12 UTC (rev 30697)
@@ -20,12 +20,6 @@
 
 AM_CLFAGS = -g
 
-EXP_LIB = \
- libgnunetmeshenc.la
-
-EXP_LIBEXEC = \
- gnunet-service-mesh-enc
-
 libexec_PROGRAMS = \
  gnunet-service-mesh $(EXP_LIBEXEC)
 
@@ -58,20 +52,8 @@
   $(LTLIBINTL)
 libgnunetmesh_la_LDFLAGS = \
   $(GN_LIB_LDFLAGS) $(WINFLAGS) \
-  -version-info 3:0:0
+  -version-info 4:0:0
 
-
-libgnunetmeshenc_la_SOURCES = \
-  mesh_api_enc.c mesh_common.c
-libgnunetmeshenc_la_LIBADD = \
-  $(top_builddir)/src/util/libgnunetutil.la \
-  $(XLIB) \
-  $(LTLIBINTL)
-libgnunetmeshenc_la_LDFLAGS = \
-  $(GN_LIB_LDFLAGS) $(WINFLAGS) \
-  -version-info 1:0:0
-
-
 gnunet_mesh_SOURCES = \
   gnunet-mesh.c
 gnunet_mesh_LDADD = \
@@ -81,27 +63,6 @@
   libgnunetmesh.la
 
 gnunet_service_mesh_SOURCES = \
- gnunet-service-mesh.c \
- mesh_path.c \
- mesh_common.c
-gnunet_service_mesh_CFLAGS = $(AM_CFLAGS)
-gnunet_service_mesh_LDADD = \
-  $(top_builddir)/src/util/libgnunetutil.la \
-  $(top_builddir)/src/core/libgnunetcore.la \
-  $(top_builddir)/src/dht/libgnunetdht.la \
-  $(top_builddir)/src/statistics/libgnunetstatistics.la \
-  $(top_builddir)/src/block/libgnunetblock.la
-gnunet_service_mesh_DEPENDENCIES = \
-  $(top_builddir)/src/util/libgnunetutil.la \
-  $(top_builddir)/src/core/libgnunetcore.la \
-  $(top_builddir)/src/dht/libgnunetdht.la \
-  $(top_builddir)/src/statistics/libgnunetstatistics.la \
-  $(top_builddir)/src/block/libgnunetblock.la
-if LINUX
-  gnunet_service_mesh_LDFLAGS = -lrt
-endif
-
-gnunet_service_mesh_enc_SOURCES = \
  gnunet-service-mesh_tunnel.c \
  gnunet-service-mesh_connection.c \
  gnunet-service-mesh_channel.c \
@@ -110,22 +71,22 @@
  gnunet-service-mesh_dht.c \
  mesh_path.c \
  mesh_common.c \
- gnunet-service-mesh-enc.c
-gnunet_service_mesh_enc_CFLAGS = $(AM_CFLAGS)
-gnunet_service_mesh_enc_LDADD = \
+ gnunet-service-mesh.c
+gnunet_service_mesh_CFLAGS = $(AM_CFLAGS)
+gnunet_service_mesh_LDADD = \
   $(top_builddir)/src/util/libgnunetutil.la \
   $(top_builddir)/src/core/libgnunetcore.la \
   $(top_builddir)/src/dht/libgnunetdht.la \
   $(top_builddir)/src/statistics/libgnunetstatistics.la \
   $(top_builddir)/src/block/libgnunetblock.la
-gnunet_service_mesh_enc_DEPENDENCIES = \
+gnunet_service_mesh_DEPENDENCIES = \
   $(top_builddir)/src/util/libgnunetutil.la \
   $(top_builddir)/src/core/libgnunetcore.la \
   $(top_builddir)/src/dht/libgnunetdht.la \
   $(top_builddir)/src/statistics/libgnunetstatistics.la \
   $(top_builddir)/src/block/libgnunetblock.la
 if LINUX
-  gnunet_service_mesh_enc_LDFLAGS = -lrt
+  gnunet_service_mesh_LDFLAGS = -lrt
 endif
 
 
@@ -138,9 +99,9 @@
 libgnunetmeshtest_a_LIBADD = \
  $(top_builddir)/src/util/libgnunetutil.la \
  $(top_builddir)/src/testbed/libgnunettestbed.la \
- $(top_builddir)/src/mesh/libgnunetmeshenc.la
+ $(top_builddir)/src/mesh/libgnunetmesh.la
 libgnunetmeshtest_a_DEPENDENCIES = \
-  libgnunetmeshenc.la
+  libgnunetmesh.la
 
 if HAVE_TESTING
 check_PROGRAMS = \
@@ -161,11 +122,11 @@
   $(top_builddir)/src/util/libgnunetutil.la \
   $(top_builddir)/src/testing/libgnunettesting.la \
   $(top_builddir)/src/mesh/libgnunetmeshtest.a \
-  $(top_builddir)/src/mesh/libgnunetmeshenc.la \
+  $(top_builddir)/src/mesh/libgnunetmesh.la \
   $(top_builddir)/src/testbed/libgnunettestbed.la
 
 dep_mesh_test_lib = \
-  libgnunetmeshenc.la \
+  libgnunetmesh.la \
   libgnunetmeshtest.a
 
 test_mesh_single_SOURCES = \

Modified: gnunet/src/mesh/gnunet-mesh.c
===================================================================
--- gnunet/src/mesh/gnunet-mesh.c       2013-11-14 13:30:29 UTC (rev 30696)
+++ gnunet/src/mesh/gnunet-mesh.c       2013-11-14 13:55:12 UTC (rev 30697)
@@ -118,7 +118,7 @@
   {
     return;
   }
-  GNUNET_MESH_get_tunnels (mh, &tunnels_callback, NULL);
+//   GNUNET_MESH_get_tunnels (mh, &tunnels_callback, NULL);
   if (GNUNET_YES != monitor_connections)
   {
     GNUNET_SCHEDULER_shutdown();
@@ -148,7 +148,7 @@
     GNUNET_SCHEDULER_shutdown();
     return;
   }
-  GNUNET_MESH_show_tunnel (mh, &pid, 0, tunnel_callback, NULL);
+//   GNUNET_MESH_show_tunnel (mh, &pid, 0, tunnel_callback, NULL);
 }
 
 

Deleted: gnunet/src/mesh/gnunet-service-mesh-enc.c
===================================================================
--- gnunet/src/mesh/gnunet-service-mesh-enc.c   2013-11-14 13:30:29 UTC (rev 
30696)
+++ gnunet/src/mesh/gnunet-service-mesh-enc.c   2013-11-14 13:55:12 UTC (rev 
30697)
@@ -1,171 +0,0 @@
-/*
-     This file is part of GNUnet.
-     (C) 2001-2013 Christian Grothoff (and other contributing authors)
-
-     GNUnet is free software; you can redistribute it and/or modify
-     it under the terms of the GNU General Public License as published
-     by the Free Software Foundation; either version 3, or (at your
-     option) any later version.
-
-     GNUnet is distributed in the hope that it will be useful, but
-     WITHOUT ANY WARRANTY; without even the implied warranty of
-     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
-     General Public License for more details.
-
-     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.
-*/
-
-/**
- * @file mesh/gnunet-service-mesh-enc.c
- * @brief GNUnet MESH service with encryption
- * @author Bartlomiej Polot
- *
- *  FIXME in progress:
- * - when sending in-order buffered data, wait for client ACKs
- * - add signatures
- * - add encryption
- * - set connection IDs independently from tunnel, tunnel has no ID
- *
- * TODO:
- * - relay corking down to core
- * - set ttl relative to path length
- * TODO END
- *
- * Dictionary:
- * - peer: other mesh instance. If there is direct connection it's a neighbor.
- * - tunnel: encrypted connection to a peer, neighbor or not.
- * - channel: connection between two clients, on the same or different peers.
- *            have properties like reliability.
- * - path: series of directly connected peer from one peer to another.
- * - connection: path which is being used in a tunnel.
- */
-
-#include "platform.h"
-#include "gnunet_util_lib.h"
-#include "mesh_enc.h"
-#include "gnunet_statistics_service.h"
-
-#include "gnunet-service-mesh_local.h"
-#include "gnunet-service-mesh_channel.h"
-#include "gnunet-service-mesh_connection.h"
-#include "gnunet-service-mesh_tunnel.h"
-#include "gnunet-service-mesh_dht.h"
-#include "gnunet-service-mesh_peer.h"
-
-
-/******************************************************************************/
-/***********************      GLOBAL VARIABLES     
****************************/
-/******************************************************************************/
-
-/****************************** Global variables 
******************************/
-
-/**
- * Handle to the statistics service.
- */
-struct GNUNET_STATISTICS_Handle *stats;
-
-/**
- * Local peer own ID (memory efficient handle).
- */
-GNUNET_PEER_Id myid;
-
-/**
- * Local peer own ID (full value).
- */
-struct GNUNET_PeerIdentity my_full_id;
-
-/*************************** Static global variables 
**************************/
-
-/**
- * Own private key.
- */
-static struct GNUNET_CRYPTO_EddsaPrivateKey *my_private_key;
-
-
-/******************************************************************************/
-/************************      MAIN FUNCTIONS      
****************************/
-/******************************************************************************/
-
-/**
- * Task run during shutdown.
- *
- * @param cls unused
- * @param tc unused
- */
-static void
-shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
-{
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shutting down\n");
-
-  GML_shutdown ();
-  GMD_shutdown ();
-  GMP_shutdown ();
-  GMC_shutdown ();
-  GMT_shutdown ();
-
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shut down\n");
-}
-
-
-/**
- * Process mesh requests.
- *
- * @param cls closure
- * @param server the initialized server
- * @param c configuration to use
- */
-static void
-run (void *cls, struct GNUNET_SERVER_Handle *server,
-     const struct GNUNET_CONFIGURATION_Handle *c)
-{
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "starting to run\n");
-
-  stats = GNUNET_STATISTICS_create ("mesh", c);
-
-  /* Scheduled the task to clean up when shutdown is called */
-  GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
-                                NULL);
-  GNUNET_log (GNUNET_ERROR_TYPE_INFO, "reading key\n");
-  my_private_key = GNUNET_CRYPTO_eddsa_key_create_from_configuration (c);
-  GNUNET_assert (NULL != my_private_key);
-  GNUNET_CRYPTO_eddsa_key_get_public (my_private_key, &my_full_id.public_key);
-  myid = GNUNET_PEER_intern (&my_full_id);
-  GNUNET_log (GNUNET_ERROR_TYPE_INFO,
-              "Mesh for peer [%s] starting\n",
-              GNUNET_i2s (&my_full_id));
-
-  GML_init (server);    /* Local clients */
-  GMC_init (c);         /* Connections */
-  GMP_init (c);         /* Peers */
-  GMD_init (c);         /* DHT */
-  GMT_init (c, my_private_key); /* Tunnels */
-
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Mesh service running\n");
-}
-
-
-/**
- * The main function for the mesh service.
- *
- * @param argc number of arguments from the command line
- * @param argv command line arguments
- * @return 0 ok, 1 on error
- */
-int
-main (int argc, char *const *argv)
-{
-  int ret;
-  int r;
-
-  fprintf (stderr, "main()\n");
-  r = GNUNET_SERVICE_run (argc, argv, "mesh", GNUNET_SERVICE_OPTION_NONE, &run,
-                          NULL);
-  GNUNET_free (my_private_key);
-  ret = (GNUNET_OK == r) ? 0 : 1;
-  fprintf (stderr, "main() END\n");
-
-  return ret;
-}

Deleted: gnunet/src/mesh/gnunet-service-mesh.c
===================================================================
--- gnunet/src/mesh/gnunet-service-mesh.c       2013-11-14 13:30:29 UTC (rev 
30696)
+++ gnunet/src/mesh/gnunet-service-mesh.c       2013-11-14 13:55:12 UTC (rev 
30697)
@@ -1,5956 +0,0 @@
-/*
-     This file is part of GNUnet.
-     (C) 2001-2013 Christian Grothoff (and other contributing authors)
-
-     GNUnet is free software; you can redistribute it and/or modify
-     it under the terms of the GNU General Public License as published
-     by the Free Software Foundation; either version 3, or (at your
-     option) any later version.
-
-     GNUnet is distributed in the hope that it will be useful, but
-     WITHOUT ANY WARRANTY; without even the implied warranty of
-     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
-     General Public License for more details.
-
-     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.
-*/
-
-/**
- * @file mesh/gnunet-service-mesh.c
- * @brief GNUnet MESH service
- * @author Bartlomiej Polot
- *
- *  FIXME in progress:
- * - when sending in-order buffered data, wait for client ACKs
- *
- * TODO:
- * - relay corking down to core
- * - set ttl relative to path length
- * - add signatures
- * - add encryption
- * TODO END
- */
-
-#include "platform.h"
-#include "mesh.h"
-#include "mesh_protocol.h"
-#include "mesh_path.h"
-#include "block_mesh.h"
-#include "gnunet_dht_service.h"
-#include "gnunet_statistics_service.h"
-
-#define MESH_BLOOM_SIZE         128
-
-#define MESH_DEBUG_DHT          GNUNET_NO
-#define MESH_DEBUG_CONNECTION   GNUNET_NO
-#define MESH_DEBUG_TIMING       __LINUX__ && GNUNET_NO
-
-#define MESH_MAX_POLL_TIME      GNUNET_TIME_relative_multiply (\
-                                  GNUNET_TIME_UNIT_MINUTES,\
-                                  10)
-#define MESH_RETRANSMIT_TIME    GNUNET_TIME_UNIT_SECONDS
-#define MESH_RETRANSMIT_MARGIN  4
-
-#if MESH_DEBUG_CONNECTION
-#define DEBUG_CONN(...) GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
-#else
-#define DEBUG_CONN(...)
-#endif
-
-#if MESH_DEBUG_DHT
-#define DEBUG_DHT(...) GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
-#else
-#define DEBUG_DHT(...)
-#endif
-
-#if MESH_DEBUG_TIMING
-#include <time.h>
-double __sum;
-uint64_t __count;
-struct timespec __mesh_start;
-struct timespec __mesh_end;
-#define INTERVAL_START clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &(__mesh_start))
-#define INTERVAL_END \
-do {\
-  clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &(__mesh_end));\
-  double __diff = __mesh_end.tv_nsec - __mesh_start.tv_nsec;\
-  if (__diff < 0) __diff += 1000000000;\
-  __sum += __diff;\
-  __count++;\
-} while (0)
-#define INTERVAL_SHOW \
-if (0 < __count)\
-  GNUNET_log (GNUNET_ERROR_TYPE_INFO, "AVG process time: %f ns\n", 
__sum/__count)
-#else
-#define INTERVAL_START
-#define INTERVAL_END
-#define INTERVAL_SHOW
-#endif
-
-/******************************************************************************/
-/************************      DATA STRUCTURES     
****************************/
-/******************************************************************************/
-
-/** FWD declaration */
-struct MeshPeer;
-struct MeshClient;
-
-
-/**
- * Struct containing info about a queued transmission to this peer
- */
-struct MeshPeerQueue
-{
-    /**
-      * DLL next
-      */
-  struct MeshPeerQueue *next;
-
-    /**
-      * DLL previous
-      */
-  struct MeshPeerQueue *prev;
-
-    /**
-     * Peer this transmission is directed to.
-     */
-  struct MeshPeer *peer;
-
-    /**
-     * Tunnel this message belongs to.
-     */
-  struct MeshTunnel *tunnel;
-
-    /**
-     * Pointer to info stucture used as cls.
-     */
-  void *cls;
-
-    /**
-     * Type of message
-     */
-  uint16_t type;
-
-    /**
-     * Size of the message
-     */
-  size_t size;
-};
-
-
-/**
- * Struct containing all information regarding a given peer
- */
-struct MeshPeer
-{
-    /**
-     * ID of the peer
-     */
-  GNUNET_PEER_Id id;
-
-    /**
-     * Last time we heard from this peer
-     */
-  struct GNUNET_TIME_Absolute last_contact;
-
-    /**
-     * Number of attempts to reconnect so far
-     */
-  int n_reconnect_attempts;
-
-    /**
-     * Paths to reach the peer, ordered by ascending hop count
-     */
-  struct MeshPeerPath *path_head;
-
-    /**
-     * Paths to reach the peer, ordered by ascending hop count
-     */
-  struct MeshPeerPath *path_tail;
-
-    /**
-     * Handle to stop the DHT search for a path to this peer
-     */
-  struct GNUNET_DHT_GetHandle *dhtget;
-
-    /**
-     * Array of tunnels this peer is the target of.
-     * Most probably a small amount, therefore not a hashmap.
-     * When the path to the peer changes, notify these tunnels to let them
-     * re-adjust their path trees.
-     */
-  struct MeshTunnel **tunnels;
-
-    /**
-     * Number of tunnels this peers participates in
-     */
-  unsigned int ntunnels;
-
-   /**
-    * Transmission queue to core DLL head
-    */
-  struct MeshPeerQueue *queue_head;
-
-   /**
-    * Transmission queue to core DLL tail
-    */
-   struct MeshPeerQueue *queue_tail;
-
-   /**
-    * How many messages are in the queue to this peer.
-    */
-   unsigned int queue_n;
-
-   /**
-    * Handle for queued transmissions
-    */
-  struct GNUNET_CORE_TransmitHandle *core_transmit;
-};
-
-
-/**
- * Struct to encapsulate all the Flow Control information to a peer in the
- * context of a tunnel: Same peer in different tunnels will have independent
- * flow control structures, allowing to choke/free tunnels according to its
- * own criteria.
- */
-struct MeshFlowControl
-{
-  /**
-   * ID of the last packet sent towards the peer.
-   */
-  uint32_t last_pid_sent;
-
-  /**
-   * ID of the last packet received from the peer.
-   */
-  uint32_t last_pid_recv;
-
-  /**
-   * Last ACK sent to the peer (peer can't send more than this PID).
-   */
-  uint32_t last_ack_sent;
-
-  /**
-   * Last ACK sent towards the origin (for traffic towards leaf node).
-   */
-  uint32_t last_ack_recv;
-
-  /**
-   * How many payload messages are in the queue towards this peer.
-   */
-  uint32_t queue_n;
-
-  /**
-   * Task to poll the peer in case of a lost ACK causes stall.
-   */
-  GNUNET_SCHEDULER_TaskIdentifier poll_task;
-
-  /**
-   * How frequently to poll for ACKs.
-   */
-  struct GNUNET_TIME_Relative poll_time;
-
-  /**
-   * On which tunnel to poll.
-   * Using an explicit poll_ctx would not help memory wise,
-   * since the allocated context would have to be stored in the
-   * fc struct in order to free it upon cancelling poll_task.
-   */
-  struct MeshTunnel *t;
-};
-
-
-/**
- * Globally unique tunnel identification (owner + number)
- * DO NOT USE OVER THE NETWORK
- */
-struct MESH_TunnelID
-{
-    /**
-     * Node that owns the tunnel
-     */
-  GNUNET_PEER_Id oid;
-
-    /**
-     * Tunnel number to differentiate all the tunnels owned by the node oid
-     * ( tid < GNUNET_MESH_LOCAL_TUNNEL_ID_CLI )
-     */
-  MESH_TunnelNumber tid;
-};
-
-
-/**
- * Data needed for reliable tunnel endpoint retransmission management.
- */
-struct MeshTunnelReliability;
-
-
-/**
- * Info needed to retry a message in case it gets lost.
- */
-struct MeshReliableMessage
-{
-    /**
-     * Double linked list, FIFO style
-     */
-  struct MeshReliableMessage    *next;
-  struct MeshReliableMessage    *prev;
-
-    /**
-     * Tunnel Reliability queue this message is in.
-     */
-  struct MeshTunnelReliability  *rel;
-
-    /**
-     * ID of the message (ACK needed to free)
-     */
-  uint32_t                      mid;
-
-    /**
-     * When was this message issued (to calculate ACK delay)
-     */
-  struct GNUNET_TIME_Absolute   timestamp;
-
-  /* struct GNUNET_MESH_Data with payload */
-};
-
-
-struct MeshTunnelReliability
-{
-    /**
-     * Tunnel this is about.
-     */
-  struct MeshTunnel *t;
-
-    /**
-     * DLL of messages sent and not yet ACK'd.
-     */
-  struct MeshReliableMessage        *head_sent;
-  struct MeshReliableMessage        *tail_sent;
-
-    /**
-     * Messages pending
-     */
-  unsigned int                      n_sent;
-
-    /**
-     * Next MID to use.
-     */
-  uint32_t                          mid_sent;
-
-    /**
-     * DLL of messages received out of order.
-     */
-  struct MeshReliableMessage        *head_recv;
-  struct MeshReliableMessage        *tail_recv;
-
-    /**
-     * Next MID expected.
-     */
-  uint32_t                          mid_recv;
-
-    /**
-     * Task to resend/poll in case no ACK is received.
-     */
-  GNUNET_SCHEDULER_TaskIdentifier   retry_task;
-
-    /**
-     * Counter for exponential backoff.
-     */
-  struct GNUNET_TIME_Relative       retry_timer;
-
-    /**
-     * How long does it usually take to get an ACK.
-     */
-  struct GNUNET_TIME_Relative       expected_delay;
-};
-
-
-/**
- * Struct containing all information regarding a tunnel
- * For an intermediate node the improtant info used will be:
- * - id        Tunnel unique identification
- * - paths[0]  To know where to send it next
- * - metainfo: ready, speeds, accounting
- */
-struct MeshTunnel
-{
-    /**
-     * Tunnel ID
-     */
-  struct MESH_TunnelID id;
-
-    /**
-     * Port of the tunnel.
-     */
-  uint32_t port;
-
-    /**
-     * State of the tunnel.
-     */
-  enum MeshTunnelState state;
-
-    /**
-     * Local tunnel number ( >= GNUNET_MESH_LOCAL_TUNNEL_ID_CLI or 0 )
-     */
-  MESH_TunnelNumber local_tid;
-
-    /**
-     * Local tunnel number for local destination clients (incoming number)
-     * ( >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV or 0). All clients share the same
-     * number.
-     */
-  MESH_TunnelNumber local_tid_dest;
-
-    /**
-     * Is the tunnel bufferless (minimum latency)?
-     */
-  int nobuffer;
-
-    /**
-     * Is the tunnel reliable?
-     */
-  int reliable;
-
-    /**
-     * Force sending ACK? Flag to allow duplicate ACK on POLL.
-     */
-  int force_ack;
-
-    /**
-     * How many messages do we accept in the forward queue.
-     */
-  unsigned int queue_max;
-
-    /**
-     * Last time the tunnel was used
-     */
-  struct GNUNET_TIME_Absolute timestamp;
-
-    /**
-     * Destination of the tunnel.
-     */
-  GNUNET_PEER_Id dest;
-
-    /**
-     * Next hop in the tunnel. If 0, @c client must be set.
-     */
-  GNUNET_PEER_Id next_hop;
-
-    /**
-     * Previous hop in the tunnel. If 0, @c owner must be set.
-     */
-  GNUNET_PEER_Id prev_hop;
-
-    /**
-     * Flow control information about @c next_hop or @c client.
-     */
-  struct MeshFlowControl next_fc;
-
-  /**
-   * Flow control information about @c prev_hop or @c owner.
-   */
-  struct MeshFlowControl prev_fc;
-
-    /**
-     * Client owner of the tunnel, if any
-     */
-  struct MeshClient *owner;
-
-    /**
-     * Client destination of the tunnel, if any.
-     */
-  struct MeshClient *client;
-
-    /**
-     * Task to keep the used paths alive at the owner,
-     * time tunnel out on all the other peers.
-     */
-  GNUNET_SCHEDULER_TaskIdentifier fwd_maintenance_task;
-
-  /**
-     * Task to keep the used paths alive at the destination,
-     * time tunnel out on all the other peers.
-     */
-  GNUNET_SCHEDULER_TaskIdentifier bck_maintenance_task;
-
-    /**
-     * Path being used for the tunnel.
-     */
-  struct MeshPeerPath *path;
-
-    /**
-     * Flag to signal the destruction of the tunnel.
-     * If this is set GNUNET_YES the tunnel will be destroyed
-     * when the queue is empty.
-     */
-  int destroy;
-
-    /**
-     * Total messages pending for this tunnels, payload or not.
-     */
-  unsigned int pending_messages;
-
-    /**
-     * Reliability data.
-     * Only present (non-NULL) at the owner of a tunnel.
-     */
-  struct MeshTunnelReliability *fwd_rel;
-
-    /**
-     * Reliability data.
-     * Only present (non-NULL) at the destination of a tunnel.
-     */
-  struct MeshTunnelReliability *bck_rel;
-};
-
-
-/**
- * Struct containing information about a client of the service
- *
- * TODO: add a list of 'waiting' ports
- */
-struct MeshClient
-{
-    /**
-     * Linked list next
-     */
-  struct MeshClient *next;
-
-    /**
-     * Linked list prev
-     */
-  struct MeshClient *prev;
-
-    /**
-     * Tunnels that belong to this client, indexed by local id
-     */
-  struct GNUNET_CONTAINER_MultiHashMap32 *own_tunnels;
-
-   /**
-     * Tunnels this client has accepted, indexed by incoming local id
-     */
-  struct GNUNET_CONTAINER_MultiHashMap32 *incoming_tunnels;
-
-    /**
-     * Handle to communicate with the client
-     */
-  struct GNUNET_SERVER_Client *handle;
-
-    /**
-     * Ports that this client has declared interest in.
-     * Indexed by port, contains *Client.
-     */
-  struct GNUNET_CONTAINER_MultiHashMap32 *ports;
-
-    /**
-     * Whether the client is active or shutting down (don't send confirmations
-     * to a client that is shutting down.
-     */
-  int shutting_down;
-
-    /**
-     * ID of the client, mainly for debug messages
-     */
-  unsigned int id;
-
-};
-
-
-/******************************************************************************/
-/************************      DEBUG FUNCTIONS     
****************************/
-/******************************************************************************/
-
-#if MESH_DEBUG
-/**
- * GNUNET_SCHEDULER_Task for printing a message after some operation is done
- * @param cls string to print
- * @param success  GNUNET_OK if the PUT was transmitted,
- *                GNUNET_NO on timeout,
- *                GNUNET_SYSERR on disconnect from service
- *                after the PUT message was transmitted
- *                (so we don't know if it was received or not)
- */
-
-#if 0
-static void
-mesh_debug (void *cls, int success)
-{
-  char *s = cls;
-
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "%s (%d)\n", s, success);
-}
-#endif
-
-#endif
-
-/******************************************************************************/
-/***********************      GLOBAL VARIABLES     
****************************/
-/******************************************************************************/
-
-/************************** Configuration parameters 
**************************/
-
-/**
- * How often to send tunnel keepalives. Tunnels timeout after 4 missed.
- */
-static struct GNUNET_TIME_Relative refresh_path_time;
-
-/**
- * How often to PUT own ID in the DHT.
- */
-static struct GNUNET_TIME_Relative id_announce_time;
-
-/**
- * Maximum time allowed to connect to a peer found by string.
- */
-static struct GNUNET_TIME_Relative connect_timeout;
-
-/**
- * Default TTL for payload packets.
- */
-static unsigned long long default_ttl;
-
-/**
- * DHT replication level, see DHT API: GNUNET_DHT_get_start, GNUNET_DHT_put.
- */
-static unsigned long long dht_replication_level;
-
-/**
- * How many tunnels are we willing to maintain.
- * Local tunnels are always allowed, even if there are more tunnels than max.
- */
-static unsigned long long max_tunnels;
-
-/**
- * How many messages *in total* are we willing to queue, divided by number of
- * tunnels to get tunnel queue size.
- */
-static unsigned long long max_msgs_queue;
-
-/**
- * How many peers do we want to remember?
- */
-static unsigned long long max_peers;
-
-/**
- * Percentage of messages that will be dropped (for test purposes only).
- */
-static unsigned long long drop_percent;
-
-/*************************** Static global variables 
**************************/
-
-/**
- * DLL with all the clients, head.
- */
-static struct MeshClient *clients_head;
-
-/**
- * DLL with all the clients, tail.
- */
-static struct MeshClient *clients_tail;
-
-/**
- * Tunnels known, indexed by MESH_TunnelID (MeshTunnel).
- */
-static struct GNUNET_CONTAINER_MultiHashMap *tunnels;
-
-/**
- * Number of tunnels known.
- */
-static unsigned long long n_tunnels;
-
-/**
- * Tunnels incoming, indexed by MESH_TunnelNumber
- * (which is greater than GNUNET_MESH_LOCAL_TUNNEL_ID_SERV).
- */
-static struct GNUNET_CONTAINER_MultiHashMap32 *incoming_tunnels;
-
-/**
- * Peers known, indexed by PeerIdentity (MeshPeer).
- */
-static struct GNUNET_CONTAINER_MultiPeerMap *peers;
-
-/*
- * Handle to communicate with transport
- */
-// static struct GNUNET_TRANSPORT_Handle *transport_handle;
-
-/**
- * Handle to communicate with core.
- */
-static struct GNUNET_CORE_Handle *core_handle;
-
-/**
- * Handle to use DHT.
- */
-static struct GNUNET_DHT_Handle *dht_handle;
-
-/**
- * Handle to server.
- */
-static struct GNUNET_SERVER_Handle *server_handle;
-
-/**
- * Handle to the statistics service.
- */
-static struct GNUNET_STATISTICS_Handle *stats;
-
-/**
- * Notification context, to send messages to local clients.
- */
-static struct GNUNET_SERVER_NotificationContext *nc;
-
-/**
- * Local peer own ID (memory efficient handle).
- */
-static GNUNET_PEER_Id myid;
-
-/**
- * Local peer own ID (full value).
- */
-static struct GNUNET_PeerIdentity my_full_id;
-
-/**
- * Own private key.
- */
-static struct GNUNET_CRYPTO_EddsaPrivateKey *my_private_key;
-
-/**
- * Tunnel ID for the next created tunnel (global tunnel number).
- */
-static MESH_TunnelNumber next_tid;
-
-/**
- * Tunnel ID for the next incoming tunnel (local tunnel number).
- */
-static MESH_TunnelNumber next_local_tid;
-
-/**
- * All ports clients of this peer have opened.
- */
-static struct GNUNET_CONTAINER_MultiHashMap32 *ports;
-
-/**
- * Task to periodically announce itself in the network.
- */
-GNUNET_SCHEDULER_TaskIdentifier announce_id_task;
-
-/**
- * Next ID to assign to a client.
- */
-unsigned int next_client_id;
-
-
-/******************************************************************************/
-/***********************         DECLARATIONS        
**************************/
-/******************************************************************************/
-
-/**
- * Function to process paths received for a new peer addition. The recorded
- * paths form the initial tunnel, which can be optimized later.
- * Called on each result obtained for the DHT search.
- *
- * @param cls closure
- * @param exp when will this value expire
- * @param key key of the result
- * @param type type of the result
- * @param size number of bytes in data
- * @param data pointer to the result data
- */
-static void
-dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
-                    const struct GNUNET_HashCode * key,
-                    const struct GNUNET_PeerIdentity *get_path,
-                    unsigned int get_path_length,
-                    const struct GNUNET_PeerIdentity *put_path,
-                    unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
-                    size_t size, const void *data);
-
-
-/**
- * Retrieve the MeshPeer stucture associated with the peer, create one
- * and insert it in the appropriate structures if the peer is not known yet.
- *
- * @param peer_id Full identity of the peer.
- *
- * @return Existing or newly created peer info.
- */
-static struct MeshPeer *
-peer_get (const struct GNUNET_PeerIdentity *peer_id);
-
-
-/**
- * Retrieve the MeshPeer stucture associated with the peer, create one
- * and insert it in the appropriate structures if the peer is not known yet.
- *
- * @param peer Short identity of the peer.
- *
- * @return Existing or newly created peer info.
- */
-static struct MeshPeer *
-peer_get_short (const GNUNET_PEER_Id peer);
-
-
-/**
- * Build a PeerPath from the paths returned from the DHT, reversing the paths
- * to obtain a local peer -> destination path and interning the peer ids.
- *
- * @return Newly allocated and created path
- */
-static struct MeshPeerPath *
-path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
-                     unsigned int get_path_length,
-                     const struct GNUNET_PeerIdentity *put_path,
-                     unsigned int put_path_length);
-
-
-/**
- * Adds a path to the peer_infos of all the peers in the path
- *
- * @param p Path to process.
- * @param confirmed Whether we know if the path works or not.
- */
-static void
-path_add_to_peers (struct MeshPeerPath *p, int confirmed);
-
-
-
-/**
- * Search for a tunnel by global ID using full PeerIdentities.
- *
- * @param oid owner of the tunnel.
- * @param tid global tunnel number.
- *
- * @return tunnel handler, NULL if doesn't exist.
- */
-static struct MeshTunnel *
-tunnel_get (const struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid);
-
-
-/**
- * Change the tunnel state.
- *
- * @param t Tunnel whose ttate to change.
- * @param state New state.
- */
-static void
-tunnel_change_state (struct MeshTunnel *t, enum MeshTunnelState state);
-
-
-/**
- * Notify a tunnel that a connection has broken that affects at least
- * some of its peers.
- *
- * @param t Tunnel affected.
- * @param p1 Peer that got disconnected from p2.
- * @param p2 Peer that got disconnected from p1.
- *
- * @return Short ID of the peer disconnected (either p1 or p2).
- *         0 if the tunnel remained unaffected.
- */
-static GNUNET_PEER_Id
-tunnel_notify_connection_broken (struct MeshTunnel *t, GNUNET_PEER_Id p1,
-                                 GNUNET_PEER_Id p2);
-
-
-/**
- * Send FWD keepalive packets for a tunnel.
- *
- * @param cls Closure (tunnel for which to send the keepalive).
- * @param tc Notification context.
- */
-static void
-tunnel_fwd_keepalive (void *cls, const struct GNUNET_SCHEDULER_TaskContext 
*tc);
-
-/**
- * Send BCK keepalive packets for a tunnel.
- *
- * @param cls Closure (tunnel for which to send the keepalive).
- * @param tc Notification context.
- */
-static void
-tunnel_bck_keepalive (void *cls, const struct GNUNET_SCHEDULER_TaskContext 
*tc);
-
-/**
- * @brief Use the given path for the tunnel.
- * Update the next and prev hops (and RCs).
- * (Re)start the path refresh in case the tunnel is locally owned.
- *
- * @param t Tunnel to update.
- * @param p Path to use.
- */
-static void
-tunnel_use_path (struct MeshTunnel *t, struct MeshPeerPath *p);
-
-/**
- * Tunnel is empty: destroy it.
- *
- * Notifies all participants (peers, cleints) about the destruction.
- *
- * @param t Tunnel to destroy.
- */
-static void
-tunnel_destroy_empty (struct MeshTunnel *t);
-
-/**
- * Destroy the tunnel.
- *
- * This function does not generate any warning traffic to clients or peers.
- *
- * Tasks:
- * Remove the tunnel from peer_info's and clients' hashmaps.
- * Cancel messages belonging to this tunnel queued to neighbors.
- * Free any allocated resources linked to the tunnel.
- *
- * @param t the tunnel to destroy
- *
- * @return GNUNET_OK on success
- */
-static int
-tunnel_destroy (struct MeshTunnel *t);
-
-/**
- * @brief Queue and pass message to core when possible.
- *
- * If type is payload (UNICAST, TO_ORIGIN, MULTICAST) checks for queue status
- * and accounts for it. In case the queue is full, the message is dropped and
- * a break issued.
- *
- * Otherwise, message is treated as internal and allowed to go regardless of
- * queue status.
- *
- * @param cls Closure (@c type dependant). It will be used by queue_send to
- *            build the message to be sent if not already prebuilt.
- * @param type Type of the message, 0 for a raw message.
- * @param size Size of the message.
- * @param dst Neighbor to send message to.
- * @param t Tunnel this message belongs to.
- */
-static void
-queue_add (void *cls, uint16_t type, size_t size,
-           struct MeshPeer *dst, struct MeshTunnel *t);
-
-
-/**
- * Free a transmission that was already queued with all resources
- * associated to the request.
- *
- * @param queue Queue handler to cancel.
- * @param clear_cls Is it necessary to free associated cls?
- */
-static void
-queue_destroy (struct MeshPeerQueue *queue, int clear_cls);
-
-
-/**
- * @brief Get the next transmittable message from the queue.
- *
- * This will be the head, except in the case of being a data packet
- * not allowed by the destination peer.
- *
- * @param peer Destination peer.
- *
- * @return The next viable MeshPeerQueue element to send to that peer.
- *         NULL when there are no transmittable messages.
- */
-struct MeshPeerQueue *
-queue_get_next (const struct MeshPeer *peer);
-
-
-/**
- * Core callback to write a queued packet to core buffer
- *
- * @param cls Closure (peer info).
- * @param size Number of bytes available in buf.
- * @param buf Where the to write the message.
- *
- * @return number of bytes written to buf
- */
-static size_t
-queue_send (void *cls, size_t size, void *buf);
-
-
-/**
- * Dummy function to separate declarations from definitions in function list.
- */
-void
-__mesh_divider______________________________________________________________();
-
-
-static const char *
-GNUNET_MESH_DEBUG_S2S (enum MeshTunnelState s)
-{
-  static char buf[128];
-
-  switch (s)
-  {
-    /**
-     * Uninitialized status, should never appear in operation.
-     */
-    case MESH_TUNNEL_NEW: return "MESH_TUNNEL_NEW";
-
-    /**
-     * Path to the peer not known yet
-     */
-    case MESH_TUNNEL_SEARCHING: return "MESH_TUNNEL_SEARCHING";
-
-    /**
-     * Request sent, not yet answered.
-     */
-    case MESH_TUNNEL_WAITING: return "MESH_TUNNEL_WAITING";
-
-    /**
-     * Peer connected and ready to accept data
-     */
-    case MESH_TUNNEL_READY: return "MESH_TUNNEL_READY";
-
-    /**
-     * Peer connected previosly but not responding
-     */
-    case MESH_TUNNEL_RECONNECTING: return "MESH_TUNNEL_RECONNECTING";
-
-    default:
-      sprintf (buf, "%u (UNKNOWN STATE)", s);
-      return buf;
-  }
-}
-
-
-/******************************************************************************/
-/************************    PERIODIC FUNCTIONS    
****************************/
-/******************************************************************************/
-
-/**
- * Periodically announce self id in the DHT
- *
- * @param cls closure
- * @param tc task context
- */
-static void
-announce_id (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
-{
-  struct PBlock block;
-  struct GNUNET_HashCode phash;
-
-  if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
-  {
-    announce_id_task = GNUNET_SCHEDULER_NO_TASK;
-    return;
-  }
-  /* TODO
-   * - Set data expiration in function of X
-   * - Adapt X to churn
-   */
-  DEBUG_DHT ("DHT_put for ID %s started.\n", GNUNET_i2s (&my_full_id));
-  GNUNET_CRYPTO_hash (&my_full_id, sizeof (my_full_id), &phash);
-  block.id = my_full_id;
-  GNUNET_DHT_put (dht_handle,   /* DHT handle */
-                  &phash,
-                  dht_replication_level,     /* Replication level */
-                  GNUNET_DHT_RO_RECORD_ROUTE | 
GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,    /* DHT options */
-                  GNUNET_BLOCK_TYPE_MESH_PEER,       /* Block type */
-                  sizeof (block),  /* Size of the data */
-                  (const char *) &block, /* Data itself */
-                  GNUNET_TIME_UNIT_FOREVER_ABS,  /* Data expiration */
-                  GNUNET_TIME_UNIT_FOREVER_REL, /* Retry time */
-                  NULL,         /* Continuation */
-                  NULL);        /* Continuation closure */
-  announce_id_task =
-      GNUNET_SCHEDULER_add_delayed (id_announce_time, &announce_id, cls);
-}
-
-
-/******************************************************************************/
-/******************      GENERAL HELPER FUNCTIONS      
************************/
-/******************************************************************************/
-
-
-/**
- * Check if client has registered with the service and has not disconnected
- *
- * @param client the client to check
- *
- * @return non-NULL if client exists in the global DLL
- */
-static struct MeshClient *
-client_get (struct GNUNET_SERVER_Client *client)
-{
-  return GNUNET_SERVER_client_get_user_context (client, struct MeshClient);
-}
-
-
-/**
- * Deletes a tunnel from a client (either owner or destination). To be used on
- * tunnel destroy.
- *
- * @param c Client whose tunnel to delete.
- * @param t Tunnel which should be deleted.
- */
-static void
-client_delete_tunnel (struct MeshClient *c, struct MeshTunnel *t)
-{
-  int res;
-
-  if (c == t->owner)
-  {
-    res = GNUNET_CONTAINER_multihashmap32_remove (c->own_tunnels,
-                                                  t->local_tid, t);
-    if (GNUNET_YES != res)
-      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client_delete_tunnel owner KO\n");
-  }
-  if (c == t->client)
-  {
-    res = GNUNET_CONTAINER_multihashmap32_remove (c->incoming_tunnels,
-                                                  t->local_tid_dest, t);
-    if (GNUNET_YES != res)
-      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client_delete_tunnel client KO\n");
-  }
-}
-
-
-/**
- * Notify the appropriate client that a new incoming tunnel was created.
- *
- * @param t Tunnel that was created.
- */
-static void
-send_local_tunnel_create (struct MeshTunnel *t)
-{
-  struct GNUNET_MESH_TunnelMessage msg;
-
-  if (NULL == t->client)
-    return;
-  msg.header.size = htons (sizeof (msg));
-  msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE);
-  msg.tunnel_id = htonl (t->local_tid_dest);
-  msg.port = htonl (t->port);
-  msg.opt = 0;
-  msg.opt |= GNUNET_YES == t->reliable ? GNUNET_MESH_OPTION_RELIABLE : 0;
-  msg.opt |= GNUNET_YES == t->nobuffer ? GNUNET_MESH_OPTION_NOBUFFER : 0;
-  msg.opt = htonl (msg.opt);
-  GNUNET_PEER_resolve (t->id.oid, &msg.peer);
-  GNUNET_SERVER_notification_context_unicast (nc, t->client->handle,
-                                              &msg.header, GNUNET_NO);
-}
-
-
-/**
- * Notify a client that the incoming tunnel is no longer valid.
- *
- * @param t Tunnel that is destroyed.
- * @param fwd Forward notification (owner->dest)?
- */
-static void
-send_local_tunnel_destroy (struct MeshTunnel *t, int fwd)
-{
-  struct GNUNET_MESH_TunnelMessage msg;
-  struct MeshClient *c;
-
-  c = fwd ? t->client : t->owner;
-  if (NULL == c)
-  {
-    GNUNET_break (0);
-    return;
-  }
-  msg.header.size = htons (sizeof (msg));
-  msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
-  msg.tunnel_id = htonl (fwd ? t->local_tid_dest : t->local_tid);
-  msg.port = htonl (0);
-  memset (&msg.peer, 0, sizeof (msg.peer));
-  msg.opt = htonl (0);
-  GNUNET_SERVER_notification_context_unicast (nc, c->handle,
-                                              &msg.header, GNUNET_NO);
-}
-
-
-/**
- * Build a local ACK message and send it to a local client.
- *
- * @param t Tunnel on which to send the ACK.
- * @param c Client to whom send the ACK.
- * @param is_fwd Set to GNUNET_YES for FWD ACK (dest->owner)
- */
-static void
-send_local_ack (struct MeshTunnel *t,
-                struct MeshClient *c,
-                int is_fwd)
-{
-  struct GNUNET_MESH_LocalAck msg;
-
-  msg.header.size = htons (sizeof (msg));
-  msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
-  msg.tunnel_id = htonl (is_fwd ? t->local_tid : t->local_tid_dest);
-  GNUNET_SERVER_notification_context_unicast (nc,
-                                              c->handle,
-                                              &msg.header,
-                                              GNUNET_NO);
-}
-
-
-
-/**
- * Sends an already built message to a peer, properly registrating
- * all used resources.
- *
- * @param message Message to send. Function makes a copy of it.
- * @param peer Short ID of the neighbor whom to send the message.
- * @param t Tunnel on which this message is transmitted.
- */
-static void
-send_prebuilt_message (const struct GNUNET_MessageHeader *message,
-                       GNUNET_PEER_Id peer,
-                       struct MeshTunnel *t)
-{
-  struct GNUNET_PeerIdentity id;
-  struct MeshPeer *neighbor;
-  struct MeshPeerPath *p;
-  void *data;
-  size_t size;
-  uint16_t type;
-
-//   GNUNET_TRANSPORT_try_connect(); FIXME use?
-
-  if (0 == peer)
-    return;
-
-  size = ntohs (message->size);
-  data = GNUNET_malloc (size);
-  memcpy (data, message, size);
-  type = ntohs(message->type);
-  if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == type ||
-      GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == type)
-  {
-    struct GNUNET_MESH_Data *u;
-
-    u = (struct GNUNET_MESH_Data *) data;
-    u->ttl = htonl (ntohl (u->ttl) - 1);
-  }
-  GNUNET_PEER_resolve (peer, &id);
-  neighbor = peer_get (&id);
-  for (p = neighbor->path_head; NULL != p; p = p->next)
-  {
-    if (2 >= p->length)
-    {
-      break;
-    }
-  }
-  if (NULL == p)
-  {
-#if MESH_DEBUG
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                "  %s IS NOT DIRECTLY CONNECTED\n",
-                GNUNET_i2s(&id));
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                "  PATHS TO %s:\n",
-                GNUNET_i2s(&id));
-    for (p = neighbor->path_head; NULL != p; p = p->next)
-    {
-      struct GNUNET_PeerIdentity debug_id;
-      unsigned int i;
-
-      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                  "    path with %u hops through:\n",
-                  p->length);
-      for (i = 0; i < p->length; i++)
-      {
-        GNUNET_PEER_resolve(p->peers[i], &debug_id);
-        GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                    "      hop %u: %s\n",
-                    i, GNUNET_i2s(&debug_id));
-      }
-    }
-#endif
-    GNUNET_break (0);
-    GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
-                    " no direct connection to %s\n",
-                    GNUNET_i2s (&id));
-    GNUNET_free (data);
-    return;
-  }
-  if (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK == type)
-    type = 0;
-  queue_add (data,
-             type,
-             size,
-             neighbor,
-             t);
-}
-
-
-/**
- * Sends a CREATE PATH message for a path to a peer, properly registrating
- * all used resources.
- *
- * @param t Tunnel for which the path is created.
- */
-static void
-send_path_create (struct MeshTunnel *t)
-{
-  struct MeshPeer *neighbor;
-
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Send create path\n");
-  neighbor = peer_get_short (t->next_hop);
-  queue_add (t,
-             GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE,
-             sizeof (struct GNUNET_MESH_CreateTunnel) +
-                (t->path->length * sizeof (struct GNUNET_PeerIdentity)),
-             neighbor,
-             t);
-  tunnel_change_state (t, MESH_TUNNEL_WAITING);
-}
-
-
-/**
- * Sends a PATH ACK message in reponse to a received PATH_CREATE directed to 
us.
- *
- * @param t Tunnel which to confirm.
- */
-static void
-send_path_ack (struct MeshTunnel *t)
-{
-  struct MeshPeer *neighbor;
-
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Send path ack\n");
-  neighbor = peer_get_short (t->prev_hop);
-  queue_add (t,
-             GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
-             sizeof (struct GNUNET_MESH_PathACK),
-             neighbor,
-             t);
-  tunnel_change_state (t, MESH_TUNNEL_WAITING);
-}
-
-
-/**
- * Build an ACK message and queue it to send to the given peer.
- *
- * @param t Tunnel on which to send the ACK.
- * @param peer Peer to whom send the ACK.
- * @param ack Value of the ACK.
- */
-static void
-send_ack (struct MeshTunnel *t, GNUNET_PEER_Id peer,  uint32_t ack)
-{
-  struct GNUNET_MESH_ACK msg;
-
-  GNUNET_PEER_resolve (t->id.oid, &msg.oid);
-  msg.header.size = htons (sizeof (msg));
-  msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_ACK);
-  msg.pid = htonl (ack);
-  msg.tid = htonl (t->id.tid);
-
-  send_prebuilt_message (&msg.header, peer, t);
-}
-
-
-/**
-  * Core callback to write a pre-constructed data packet to core buffer
-  *
-  * @param cls Closure (MeshTransmissionDescriptor with data in "data" member).
-  * @param size Number of bytes available in buf.
-  * @param buf Where the to write the message.
-  *
-  * @return number of bytes written to buf
-  */
-static size_t
-send_core_data_raw (void *cls, size_t size, void *buf)
-{
-  struct GNUNET_MessageHeader *msg = cls;
-  size_t total_size;
-
-  GNUNET_assert (NULL != msg);
-  total_size = ntohs (msg->size);
-
-  if (total_size > size)
-  {
-    GNUNET_break (0);
-    return 0;
-  }
-  memcpy (buf, msg, total_size);
-  GNUNET_free (cls);
-  return total_size;
-}
-
-
-/**
- * Function to send a create path packet to a peer.
- *
- * @param cls closure
- * @param size number of bytes available in buf
- * @param buf where the callee should write the message
- * @return number of bytes written to buf
- */
-static size_t
-send_core_path_create (void *cls, size_t size, void *buf)
-{
-  struct MeshTunnel *t = cls;
-  struct GNUNET_MESH_CreateTunnel *msg;
-  struct GNUNET_PeerIdentity *peer_ptr;
-  struct MeshPeerPath *p = t->path;
-  size_t size_needed;
-  uint32_t opt;
-  int i;
-
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATE PATH sending...\n");
-  size_needed =
-      sizeof (struct GNUNET_MESH_CreateTunnel) +
-      p->length * sizeof (struct GNUNET_PeerIdentity);
-
-  if (size < size_needed || NULL == buf)
-  {
-    GNUNET_break (0);
-    return 0;
-  }
-  msg = (struct GNUNET_MESH_CreateTunnel *) buf;
-  msg->header.size = htons (size_needed);
-  msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE);
-  msg->tid = ntohl (t->id.tid);
-
-  opt = 0;
-  if (GNUNET_YES == t->nobuffer)
-    opt |= GNUNET_MESH_OPTION_NOBUFFER;
-  if (GNUNET_YES == t->reliable)
-    opt |= GNUNET_MESH_OPTION_RELIABLE;
-  msg->opt = htonl (opt);
-  msg->port = htonl (t->port);
-
-  peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
-  for (i = 0; i < p->length; i++)
-  {
-    GNUNET_PEER_resolve (p->peers[i], peer_ptr++);
-  }
-
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-              "CREATE PATH (%u bytes long) sent!\n", size_needed);
-  return size_needed;
-}
-
-
-/**
- * Creates a path ack message in buf and frees all unused resources.
- *
- * @param cls closure (MeshTransmissionDescriptor)
- * @param size number of bytes available in buf
- * @param buf where the callee should write the message
- * @return number of bytes written to buf
- */
-static size_t
-send_core_path_ack (void *cls, size_t size, void *buf)
-{
-  struct MeshTunnel *t = cls;
-  struct GNUNET_MESH_PathACK *msg = buf;
-
-  GNUNET_assert (NULL != t);
-  if (sizeof (struct GNUNET_MESH_PathACK) > size)
-  {
-    GNUNET_break (0);
-    return 0;
-  }
-  t->prev_fc.last_ack_sent = t->nobuffer ? 0 : t->queue_max - 1;
-  msg->header.size = htons (sizeof (struct GNUNET_MESH_PathACK));
-  msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK);
-  GNUNET_PEER_resolve (t->id.oid, &msg->oid);
-  msg->tid = htonl (t->id.tid);
-  msg->peer_id = my_full_id;
-  msg->ack = htonl (t->prev_fc.last_ack_sent);
-
-  /* TODO add signature */
-
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "PATH ACK sent!\n");
-  return sizeof (struct GNUNET_MESH_PathACK);
-}
-
-
-/**
- * Iterator over all the peers to remove the oldest not-used entry.
- *
- * @param cls Closure (unsued).
- * @param key ID of the peer.
- * @param value Peer_Info of the peer.
- *
- * FIXME implement
- */
-static int
-peer_timeout (void *cls,
-              const struct GNUNET_PeerIdentity *key,
-              void *value)
-{
-  return GNUNET_YES;
-}
-
-
-/**
- * Retrieve the MeshPeer stucture associated with the peer, create one
- * and insert it in the appropriate structures if the peer is not known yet.
- *
- * @param peer_id Full identity of the peer.
- *
- * @return Existing or newly created peer info.
- */
-static struct MeshPeer *
-peer_get (const struct GNUNET_PeerIdentity *peer_id)
-{
-  struct MeshPeer *peer;
-
-  peer = GNUNET_CONTAINER_multipeermap_get (peers, peer_id);
-  if (NULL == peer)
-  {
-    peer = GNUNET_new (struct MeshPeer);
-    if (GNUNET_CONTAINER_multipeermap_size (peers) > max_peers)
-    {
-      GNUNET_CONTAINER_multipeermap_iterate (peers,
-                                             &peer_timeout,
-                                             NULL);
-    }
-    GNUNET_CONTAINER_multipeermap_put (peers, peer_id, peer,
-                                       
GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
-    peer->id = GNUNET_PEER_intern (peer_id);
-  }
-  peer->last_contact = GNUNET_TIME_absolute_get();
-
-  return peer;
-}
-
-
-/**
- * Retrieve the MeshPeer stucture associated with the peer, create one
- * and insert it in the appropriate structures if the peer is not known yet.
- *
- * @param peer Short identity of the peer.
- *
- * @return Existing or newly created peer info.
- */
-static struct MeshPeer *
-peer_get_short (const GNUNET_PEER_Id peer)
-{
-  return peer_get (GNUNET_PEER_resolve2 (peer));
-}
-
-
-/**
- * Select which PID to POLL for, to compensate for lost messages.
- *
- * @param p Peer we want to poll.
- * @param t Tunnel about which we want to poll.
- *
- * @return PID to use, either last sent or first_in_queue - 1
- */
-static uint32_t
-peer_get_first_payload_pid (struct MeshPeer *p, struct MeshTunnel *t)
-{
-  struct MeshPeerQueue *q;
-  uint16_t type;
-
-  type = p->id == t->next_hop ? GNUNET_MESSAGE_TYPE_MESH_UNICAST :
-                                 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN;
-
-  for (q = p->queue_head; NULL != q; q = q->next)
-  {
-    if (q->type == type && q->tunnel == t)
-    {
-      struct GNUNET_MESH_Data *msg = q->cls;
-
-      /* Pretend that the last one sent was the previous to this */
-      return ntohl (msg->pid) - 1;
-    }
-  }
-
-  /* No data in queue, use last sent */
-  {
-    struct MeshFlowControl *fc;
-
-    fc = p->id == t->next_hop ? &t->next_fc : &t->prev_fc;
-    return fc->last_pid_sent;
-  }
-}
-
-
-/**
- * Choose the best path towards a peer considering the tunnel properties.
- *
- * @param peer The destination peer.
- * @param t The tunnel the path is for.
- *
- * @return Best current known path towards the peer, if any.
- */
-static struct MeshPeerPath *
-peer_get_best_path (const struct MeshPeer *peer, const struct MeshTunnel *t)
-{
-  struct MeshPeerPath *best_p;
-  struct MeshPeerPath *p;
-  unsigned int best_cost;
-  unsigned int cost;
-
-  best_p = p = peer->path_head;
-  best_cost = cost = p->length;
-  while (NULL != p)
-  {
-    if ((cost = p->length) < best_cost)
-    {
-      best_cost = cost;
-      best_p = p;
-    }
-    p = p->next;
-  }
-  return best_p;
-}
-
-
-/**
- * Try to establish a new connection to this peer in the given tunnel.
- * If the peer doesn't have any path to it yet, try to get one.
- * If the peer already has some path, send a CREATE PATH towards it.
- *
- * @param peer PeerInfo of the peer.
- * @param t Tunnel for which to create the path, if possible.
- */
-static void
-peer_connect (struct MeshPeer *peer, struct MeshTunnel *t)
-{
-  struct MeshPeerPath *p;
-  struct GNUNET_HashCode phash;
-
-  if (NULL != peer->path_head)
-  {
-    p = peer_get_best_path (peer, t);
-    tunnel_use_path (t, p);
-    send_path_create (t);
-  }
-  else if (NULL == peer->dhtget)
-  {
-    struct GNUNET_PeerIdentity id;
-
-    GNUNET_PEER_resolve (peer->id, &id);
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                "Starting DHT GET for peer %s\n",
-               GNUNET_i2s (&id));
-    GNUNET_CRYPTO_hash (&id, sizeof (my_full_id), &phash);
-    peer->dhtget = GNUNET_DHT_get_start (dht_handle,    /* handle */
-                                         GNUNET_BLOCK_TYPE_MESH_PEER, /* type 
*/
-                                         &phash,     /* key to search */
-                                         dht_replication_level, /* replication 
level */
-                                         GNUNET_DHT_RO_RECORD_ROUTE |
-                                         GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
-                                         NULL,       /* xquery */
-                                         0,     /* xquery bits */
-                                         &dht_get_id_handler, peer);
-    tunnel_change_state (t, MESH_TUNNEL_SEARCHING);
-  }
-  else
-  {
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                "There is no path but the DHT GET is already started.\n");
-  }
-}
-
-
-/**
- * @brief Re-initiate traffic to this peer if necessary.
- *
- * Check if there is traffic queued towards this peer
- * and the core transmit handle is NULL (traffic was stalled).
- * If so, call core tmt rdy.
- *
- * @param peer_id Short ID of peer to which initiate traffic.
- */
-static void
-peer_unlock_queue (GNUNET_PEER_Id peer_id)
-{
-  struct MeshPeer *peer;
-  struct MeshPeerQueue *q;
-  size_t size;
-
-  peer = peer_get_short (peer_id);
-  if (NULL != peer->core_transmit)
-    return;
-
-  q = queue_get_next (peer);
-  if (NULL == q)
-    return;
-
-  size = q->size;
-  peer->core_transmit =
-      GNUNET_CORE_notify_transmit_ready (core_handle,
-                                         GNUNET_NO,
-                                         0,
-                                         GNUNET_TIME_UNIT_FOREVER_REL,
-                                         GNUNET_PEER_resolve2 (peer->id),
-                                         size,
-                                         &queue_send,
-                                         peer);
-}
-
-
-/**
- * Cancel all transmissions towards a neighbor that belong to a certain tunnel.
- *
- * @param neighbor Short ID of the neighbor to whom cancel the transmissions.
- * @param t Tunnel which to cancel.
- */
-static void
-peer_cancel_queues (GNUNET_PEER_Id neighbor, struct MeshTunnel *t)
-{
-  struct MeshPeer *peer;
-  struct MeshPeerQueue *q;
-  struct MeshPeerQueue *next;
-  struct MeshFlowControl *fc;
-
-  if (0 == neighbor)
-    return; /* Was local peer, 0'ed in tunnel_destroy_iterator */
-  peer = peer_get_short (neighbor);
-  for (q = peer->queue_head; NULL != q; q = next)
-  {
-    next = q->next;
-    if (q->tunnel == t)
-    {
-      if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == q->type ||
-          GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == q->type)
-      {
-        GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                    "peer_cancel_queues %s\n",
-                    GNUNET_MESH_DEBUG_M2S (q->type));
-      }
-      queue_destroy (q, GNUNET_YES);
-    }
-  }
-  if (NULL == peer->queue_head && NULL != peer->core_transmit)
-  {
-    GNUNET_CORE_notify_transmit_ready_cancel (peer->core_transmit);
-    peer->core_transmit = NULL;
-  }
-  fc = neighbor == t->next_hop ? &t->next_fc : &t->prev_fc;
-  if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task)
-  {
-    GNUNET_SCHEDULER_cancel (fc->poll_task);
-    fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
-  }
-}
-
-
-/**
- * Destroy the peer_info and free any allocated resources linked to it
- *
- * @param peer The peer_info to destroy.
- *
- * @return GNUNET_OK on success
- */
-static int
-peer_destroy (struct MeshPeer *peer)
-{
-  struct GNUNET_PeerIdentity id;
-  struct MeshPeerPath *p;
-  struct MeshPeerPath *nextp;
-  unsigned int i;
-
-  GNUNET_PEER_resolve (peer->id, &id);
-  GNUNET_PEER_change_rc (peer->id, -1);
-
-  if (GNUNET_YES !=
-      GNUNET_CONTAINER_multipeermap_remove (peers, &id, peer))
-  {
-    GNUNET_break (0);
-    GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
-                "removing peer %s, not in hashmap\n", GNUNET_i2s (&id));
-  }
-  if (NULL != peer->dhtget)
-  {
-    GNUNET_DHT_get_stop (peer->dhtget);
-  }
-  p = peer->path_head;
-  while (NULL != p)
-  {
-    nextp = p->next;
-    GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, p);
-    path_destroy (p);
-    p = nextp;
-  }
-  for (i = 0; i < peer->ntunnels; i++)
-    tunnel_destroy_empty (peer->tunnels[i]);
-  GNUNET_array_grow (peer->tunnels, peer->ntunnels, 0);
-  GNUNET_free (peer);
-  return GNUNET_OK;
-}
-
-
-/**
- * Remove all paths that rely on a direct connection between p1 and p2
- * from the peer itself and notify all tunnels about it.
- *
- * @param peer PeerInfo of affected peer.
- * @param p1 GNUNET_PEER_Id of one peer.
- * @param p2 GNUNET_PEER_Id of another peer that was connected to the first and
- *           no longer is.
- *
- * TODO: optimize (see below)
- */
-static void
-peer_remove_path (struct MeshPeer *peer, GNUNET_PEER_Id p1,
-                       GNUNET_PEER_Id p2)
-{
-  struct MeshPeerPath *p;
-  struct MeshPeerPath *next;
-  struct MeshPeer *peer_d;
-  GNUNET_PEER_Id d;
-  unsigned int destroyed;
-  unsigned int i;
-
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path\n");
-  destroyed = 0;
-  for (p = peer->path_head; NULL != p; p = next)
-  {
-    next = p->next;
-    for (i = 0; i < (p->length - 1); i++)
-    {
-      if ((p->peers[i] == p1 && p->peers[i + 1] == p2) ||
-          (p->peers[i] == p2 && p->peers[i + 1] == p1))
-      {
-        GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, p);
-        path_destroy (p);
-        destroyed++;
-        break;
-      }
-    }
-  }
-  if (0 == destroyed)
-    return;
-
-  for (i = 0; i < peer->ntunnels; i++)
-  {
-    d = tunnel_notify_connection_broken (peer->tunnels[i], p1, p2);
-    if (0 == d)
-      continue;
-
-    peer_d = peer_get_short (d);
-    next = peer_get_best_path (peer_d, peer->tunnels[i]);
-    tunnel_use_path (peer->tunnels[i], next);
-    peer_connect (peer_d, peer->tunnels[i]);
-  }
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path END\n");
-}
-
-
-/**
- * Add the path to the peer and update the path used to reach it in case this
- * is the shortest.
- *
- * @param peer_info Destination peer to add the path to.
- * @param path New path to add. Last peer must be the peer in arg 1.
- *             Path will be either used of freed if already known.
- * @param trusted Do we trust that this path is real?
- */
-void
-peer_add_path (struct MeshPeer *peer_info, struct MeshPeerPath *path,
-                    int trusted)
-{
-  struct MeshPeerPath *aux;
-  unsigned int l;
-  unsigned int l2;
-
-  if ((NULL == peer_info) || (NULL == path))
-  {
-    GNUNET_break (0);
-    path_destroy (path);
-    return;
-  }
-  if (path->peers[path->length - 1] != peer_info->id)
-  {
-    GNUNET_break (0);
-    path_destroy (path);
-    return;
-  }
-  if (2 >= path->length && GNUNET_NO == trusted)
-  {
-    /* Only allow CORE to tell us about direct paths */
-    path_destroy (path);
-    return;
-  }
-  for (l = 1; l < path->length; l++)
-  {
-    if (path->peers[l] == myid)
-    {
-      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shortening path by %u\n", l);
-      for (l2 = 0; l2 < path->length - l; l2++)
-      {
-        path->peers[l2] = path->peers[l + l2];
-      }
-      path->length -= l;
-      l = 1;
-      path->peers =
-          GNUNET_realloc (path->peers, path->length * sizeof (GNUNET_PEER_Id));
-    }
-  }
-#if MESH_DEBUG
-  {
-    struct GNUNET_PeerIdentity id;
-
-    GNUNET_PEER_resolve (peer_info->id, &id);
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "adding path [%u] to peer %s\n",
-                path->length, GNUNET_i2s (&id));
-  }
-#endif
-  l = path_get_length (path);
-  if (0 == l)
-  {
-    path_destroy (path);
-    return;
-  }
-
-  GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
-  for (aux = peer_info->path_head; aux != NULL; aux = aux->next)
-  {
-    l2 = path_get_length (aux);
-    if (l2 > l)
-    {
-      GNUNET_CONTAINER_DLL_insert_before (peer_info->path_head,
-                                          peer_info->path_tail, aux, path);
-      return;
-    }
-    else
-    {
-      if (l2 == l && memcmp (path->peers, aux->peers, l) == 0)
-      {
-        path_destroy (path);
-        return;
-      }
-    }
-  }
-  GNUNET_CONTAINER_DLL_insert_tail (peer_info->path_head, peer_info->path_tail,
-                                    path);
-  return;
-}
-
-
-/**
- * Add the path to the origin peer and update the path used to reach it in case
- * this is the shortest.
- * The path is given in peer_info -> destination, therefore we turn the path
- * upside down first.
- *
- * @param peer_info Peer to add the path to, being the origin of the path.
- * @param path New path to add after being inversed.
- *             Path will be either used or freed.
- * @param trusted Do we trust that this path is real?
- */
-static void
-peer_add_path_to_origin (struct MeshPeer *peer_info,
-                         struct MeshPeerPath *path, int trusted)
-{
-  path_invert (path);
-  peer_add_path (peer_info, path, trusted);
-}
-
-
-/**
- * Add a tunnel to the list of tunnels a peer participates in.
- * Update the tunnel's destination.
- *
- * @param p Peer to add to.
- * @param t Tunnel to add.
- */
-static void
-peer_add_tunnel (struct MeshPeer *p, struct MeshTunnel *t)
-{
-  if (0 != t->dest)
-  {
-    GNUNET_break (t->dest == p->id);
-    return;
-  }
-  t->dest = p->id;
-  GNUNET_PEER_change_rc (t->dest, 1);
-  GNUNET_array_append (p->tunnels, p->ntunnels, t);
-}
-
-
-/**
- * Remove a tunnel from the list of tunnels a peer participates in.
- * Free the tunnel's destination.
- *
- * @param p Peer to clean.
- * @param t Tunnel to remove.
- */
-static void
-peer_remove_tunnel (struct MeshPeer *p, struct MeshTunnel *t)
-{
-  unsigned int i;
-
-  if (t->dest == p->id)
-  {
-      GNUNET_PEER_change_rc (t->dest, -1);
-      t->dest = 0;
-  }
-  for (i = 0; i < p->ntunnels; i++)
-  {
-    if (p->tunnels[i] == t)
-    {
-      p->tunnels[i] = p->tunnels[p->ntunnels - 1];
-      GNUNET_array_grow (p->tunnels, p->ntunnels, p->ntunnels - 1);
-      return;
-    }
-  }
-}
-
-
-/**
- * Function called if the connection to the peer has been stalled for a while,
- * possibly due to a missed ACK. Poll the peer about its ACK status.
- *
- * @param cls Closure (poll ctx).
- * @param tc TaskContext.
- */
-static void
-tunnel_poll (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
-{
-  struct MeshFlowControl *fc = cls;
-  struct GNUNET_MESH_Poll msg;
-  struct MeshTunnel *t = fc->t;
-  GNUNET_PEER_Id peer;
-
-  fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
-  if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
-  {
-    return;
-  }
-
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " *** Polling!\n");
-
-  GNUNET_PEER_resolve (t->id.oid, &msg.oid);
-
-  if (fc == &t->prev_fc)
-  {
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " *** prev peer!\n");
-    peer = t->prev_hop;
-  }
-  else if (fc == &t->next_fc)
-  {
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " *** next peer!\n");
-    peer = t->next_hop;
-  }
-  else
-  {
-    GNUNET_break (0);
-    return;
-  }
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " *** peer: %s!\n",
-                GNUNET_i2s(GNUNET_PEER_resolve2 (peer)));
-  if (0 == peer)
-  {
-    if (GNUNET_YES == t->destroy)
-      tunnel_destroy (t);
-    else
-      GNUNET_break (0);
-
-    return;
-  }
-  msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_POLL);
-  msg.header.size = htons (sizeof (msg));
-  msg.tid = htonl (t->id.tid);
-  msg.pid = htonl (peer_get_first_payload_pid (peer_get_short (peer), t));
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " *** pid (%u)!\n", ntohl (msg.pid));
-  send_prebuilt_message (&msg.header, peer, t);
-  fc->poll_time = GNUNET_TIME_STD_BACKOFF (fc->poll_time);
-  fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time,
-                                                &tunnel_poll, fc);
-}
-
-
-/**
- * Build a PeerPath from the paths returned from the DHT, reversing the paths
- * to obtain a local peer -> destination path and interning the peer ids.
- *
- * @return Newly allocated and created path
- */
-static struct MeshPeerPath *
-path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
-                     unsigned int get_path_length,
-                     const struct GNUNET_PeerIdentity *put_path,
-                     unsigned int put_path_length)
-{
-  struct MeshPeerPath *p;
-  GNUNET_PEER_Id id;
-  int i;
-
-  p = path_new (1);
-  p->peers[0] = myid;
-  GNUNET_PEER_change_rc (myid, 1);
-  i = get_path_length;
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "   GET has %d hops.\n", i);
-  for (i--; i >= 0; i--)
-  {
-    id = GNUNET_PEER_intern (&get_path[i]);
-    if (p->length > 0 && id == p->peers[p->length - 1])
-    {
-      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "   Optimizing 1 hop out.\n");
-      GNUNET_PEER_change_rc (id, -1);
-    }
-    else
-    {
-      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "   Adding from GET: %s.\n",
-                  GNUNET_i2s (&get_path[i]));
-      p->length++;
-      p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * 
p->length);
-      p->peers[p->length - 1] = id;
-    }
-  }
-  i = put_path_length;
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "   PUT has %d hops.\n", i);
-  for (i--; i >= 0; i--)
-  {
-    id = GNUNET_PEER_intern (&put_path[i]);
-    if (id == myid)
-    {
-      /* PUT path went through us, so discard the path up until now and start
-       * from here to get a much shorter (and loop-free) path.
-       */
-      path_destroy (p);
-      p = path_new (0);
-    }
-    if (p->length > 0 && id == p->peers[p->length - 1])
-    {
-      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "   Optimizing 1 hop out.\n");
-      GNUNET_PEER_change_rc (id, -1);
-    }
-    else
-    {
-      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "   Adding from PUT: %s.\n",
-                  GNUNET_i2s (&put_path[i]));
-      p->length++;
-      p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * 
p->length);
-      p->peers[p->length - 1] = id;
-    }
-  }
-#if MESH_DEBUG
-  if (get_path_length > 0)
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "   (first of GET: %s)\n",
-                GNUNET_i2s (&get_path[0]));
-  if (put_path_length > 0)
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "   (first of PUT: %s)\n",
-                GNUNET_i2s (&put_path[0]));
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "   In total: %d hops\n",
-              p->length);
-  for (i = 0; i < p->length; i++)
-  {
-    struct GNUNET_PeerIdentity peer_id;
-
-    GNUNET_PEER_resolve (p->peers[i], &peer_id);
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "       %u: %s\n", p->peers[i],
-                GNUNET_i2s (&peer_id));
-  }
-#endif
-  return p;
-}
-
-
-/**
- * Adds a path to the peer_infos of all the peers in the path
- *
- * @param p Path to process.
- * @param confirmed Whether we know if the path works or not.
- */
-static void
-path_add_to_peers (struct MeshPeerPath *p, int confirmed)
-{
-  unsigned int i;
-
-  /* TODO: invert and add */
-  for (i = 0; i < p->length && p->peers[i] != myid; i++) /* skip'em */ ;
-  for (i++; i < p->length; i++)
-  {
-    struct MeshPeer *aux;
-    struct MeshPeerPath *copy;
-
-    aux = peer_get_short (p->peers[i]);
-    copy = path_duplicate (p);
-    copy->length = i + 1;
-    peer_add_path (aux, copy, p->length < 3 ? GNUNET_NO : confirmed);
-  }
-}
-
-
-/**
- * Search for a tunnel among the incoming tunnels
- *
- * @param tid the local id of the tunnel
- *
- * @return tunnel handler, NULL if doesn't exist
- */
-static struct MeshTunnel *
-tunnel_get_incoming (MESH_TunnelNumber tid)
-{
-  GNUNET_assert (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV);
-  return GNUNET_CONTAINER_multihashmap32_get (incoming_tunnels, tid);
-}
-
-
-/**
- * Search for a tunnel among the tunnels for a client
- *
- * @param c the client whose tunnels to search in
- * @param tid the local id of the tunnel
- *
- * @return tunnel handler, NULL if doesn't exist
- */
-static struct MeshTunnel *
-tunnel_get_by_local_id (struct MeshClient *c, MESH_TunnelNumber tid)
-{
-  if (0 == (tid & GNUNET_MESH_LOCAL_TUNNEL_ID_CLI))
-  {
-    GNUNET_break_op (0);
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "TID %X not a local tid\n", tid);
-    return NULL;
-  }
-  if (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
-  {
-    return tunnel_get_incoming (tid);
-  }
-  else
-  {
-    return GNUNET_CONTAINER_multihashmap32_get (c->own_tunnels, tid);
-  }
-}
-
-
-/**
- * Search for a tunnel by global ID using PEER_ID
- *
- * @param pi owner of the tunnel
- * @param tid global tunnel number
- *
- * @return tunnel handler, NULL if doesn't exist
- */
-static struct MeshTunnel *
-tunnel_get_by_pi (GNUNET_PEER_Id pi, MESH_TunnelNumber tid)
-{
-  struct MESH_TunnelID id;
-  struct GNUNET_HashCode hash;
-
-  id.oid = pi;
-  id.tid = tid;
-
-  GNUNET_CRYPTO_hash (&id, sizeof (struct MESH_TunnelID), &hash);
-  return GNUNET_CONTAINER_multihashmap_get (tunnels, &hash);
-}
-
-
-/**
- * Search for a tunnel by global ID using full PeerIdentities
- *
- * @param oid owner of the tunnel
- * @param tid global tunnel number
- *
- * @return tunnel handler, NULL if doesn't exist
- */
-static struct MeshTunnel *
-tunnel_get (const struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid)
-{
-  return tunnel_get_by_pi (GNUNET_PEER_search (oid), tid);
-}
-
-
-/**
- * Change the tunnel state.
- *
- * @param t Tunnel whose ttate to change.
- * @param state New state.
- */
-static void
-tunnel_change_state (struct MeshTunnel *t, enum MeshTunnelState state)
-{
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-              "Tunnel %s[%X] state was %s\n",
-              GNUNET_i2s (GNUNET_PEER_resolve2 (t->id.oid)), t->id.tid,
-              GNUNET_MESH_DEBUG_S2S (t->state));
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-              "Tunnel %s[%X] state is now %s\n",
-              GNUNET_i2s (GNUNET_PEER_resolve2 (t->id.oid)), t->id.tid,
-              GNUNET_MESH_DEBUG_S2S (state));
-  t->state = state;
-}
-
-
-
-/**
- * Add a client to a tunnel, initializing all needed data structures.
- *
- * @param t Tunnel to which add the client.
- * @param c Client which to add to the tunnel.
- */
-static void
-tunnel_add_client (struct MeshTunnel *t, struct MeshClient *c)
-{
-  if (NULL != t->client)
-  {
-    GNUNET_break(0);
-    return;
-  }
-  if (GNUNET_OK !=
-      GNUNET_CONTAINER_multihashmap32_put (c->incoming_tunnels,
-                                           t->local_tid_dest, t,
-                                           
GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
-  {
-    GNUNET_break (0);
-    return;
-  }
-  if (GNUNET_OK !=
-      GNUNET_CONTAINER_multihashmap32_put (incoming_tunnels,
-                                           t->local_tid_dest, t,
-                                           
GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
-  {
-    GNUNET_break (0);
-    return;
-  }
-  t->client = c;
-}
-
-
-static void
-tunnel_use_path (struct MeshTunnel *t, struct MeshPeerPath *p)
-{
-  unsigned int own_pos;
-
-  for (own_pos = 0; own_pos < p->length; own_pos++)
-  {
-    if (p->peers[own_pos] == myid)
-      break;
-  }
-  if (own_pos > p->length - 1)
-  {
-    GNUNET_break (0);
-    return;
-  }
-
-  if (own_pos < p->length - 1)
-    t->next_hop = p->peers[own_pos + 1];
-  else
-    t->next_hop = p->peers[own_pos];
-  GNUNET_PEER_change_rc (t->next_hop, 1);
-  if (0 < own_pos)
-    t->prev_hop = p->peers[own_pos - 1];
-  else
-    t->prev_hop = p->peers[0];
-  GNUNET_PEER_change_rc (t->prev_hop, 1);
-
-  if (NULL != t->path)
-    path_destroy (t->path);
-  t->path = path_duplicate (p);
-  if (0 == own_pos)
-  {
-    if (GNUNET_SCHEDULER_NO_TASK != t->fwd_maintenance_task)
-      GNUNET_SCHEDULER_cancel (t->fwd_maintenance_task);
-    t->fwd_maintenance_task =
-        GNUNET_SCHEDULER_add_delayed (refresh_path_time,
-                                      &tunnel_fwd_keepalive, t);
-  }
-}
-
-
-/**
- * Notifies a tunnel that a connection has broken that affects at least
- * some of its peers. Sends a notification towards the root of the tree.
- * In case the peer is the owner of the tree, notifies the client that owns
- * the tunnel and tries to reconnect.
- *
- * @param t Tunnel affected.
- * @param p1 Peer that got disconnected from p2.
- * @param p2 Peer that got disconnected from p1.
- *
- * @return Short ID of the peer disconnected (either p1 or p2).
- *         0 if the tunnel remained unaffected.
- */
-static GNUNET_PEER_Id
-tunnel_notify_connection_broken (struct MeshTunnel *t, GNUNET_PEER_Id p1,
-                                 GNUNET_PEER_Id p2)
-{
-//   if (myid != p1 && myid != p2) FIXME
-//   {
-//     return;
-//   }
-//
-//   if (tree_get_predecessor (t->tree) != 0)
-//   {
-//     /* We are the peer still connected, notify owner of the disconnection. 
*/
-//     struct GNUNET_MESH_PathBroken msg;
-//     struct GNUNET_PeerIdentity neighbor;
-//
-//     msg.header.size = htons (sizeof (msg));
-//     msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN);
-//     GNUNET_PEER_resolve (t->id.oid, &msg.oid);
-//     msg.tid = htonl (t->id.tid);
-//     msg.peer1 = my_full_id;
-//     GNUNET_PEER_resolve (pid, &msg.peer2);
-//     GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &neighbor);
-//     send_prebuilt_message (&msg.header, &neighbor, t);
-//   }
-  return 0;
-}
-
-
-/**
- * Send an end-to-end FWD ACK message for the most recent in-sequence payload.
- *
- * @param t Tunnel this is about.
- * @param fwd Is for FWD traffic? (ACK dest->owner)
- */
-static void
-tunnel_send_data_ack (struct MeshTunnel *t, int fwd)
-{
-  struct GNUNET_MESH_DataACK msg;
-  struct MeshTunnelReliability *rel;
-  struct MeshReliableMessage *copy;
-  GNUNET_PEER_Id hop;
-  uint64_t mask;
-  unsigned int delta;
-
-  rel = fwd ? t->bck_rel  : t->fwd_rel;
-  hop = fwd ? t->prev_hop : t->next_hop;
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-              "send_data_ack for %u\n",
-              rel->mid_recv - 1);
-
-  if (GNUNET_NO == t->reliable)
-  {
-    GNUNET_break_op (0);
-    return;
-  }
-  msg.header.type = htons (fwd ? GNUNET_MESSAGE_TYPE_MESH_UNICAST_ACK :
-                                 GNUNET_MESSAGE_TYPE_MESH_TO_ORIG_ACK);
-  msg.header.size = htons (sizeof (msg));
-  msg.tid = htonl (t->id.tid);
-  GNUNET_PEER_resolve (t->id.oid, &msg.oid);
-  msg.mid = htonl (rel->mid_recv - 1);
-  msg.futures = 0;
-  for (copy = rel->head_recv; NULL != copy; copy = copy->next)
-  {
-    delta = copy->mid - rel->mid_recv;
-    if (63 < delta)
-      break;
-    mask = 0x1LL << delta;
-    msg.futures |= mask;
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                " setting bit for %u (delta %u) (%llX) -> %llX\n",
-                copy->mid, delta, mask, msg.futures);
-  }
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " final futures %llX\n", msg.futures);
-
-  send_prebuilt_message (&msg.header, hop, t);
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "send_data_ack END\n");
-}
-
-
-/**
- * Send an ACK informing the predecessor about the available buffer space.
- * In case there is no predecessor, inform the owning client.
- * If buffering is off, send only on behalf of children or self if endpoint.
- * If buffering is on, send when sent to children and buffer space is free.
- * Note that although the name is fwd_ack, the FWD mean forward *traffic*,
- * the ACK itself goes "back" (towards root).
- *
- * @param t Tunnel on which to send the ACK.
- * @param type Type of message that triggered the ACK transmission.
- * @param fwd Is this FWD ACK? (Going dest->owner)
- */
-static void
-tunnel_send_ack (struct MeshTunnel *t, uint16_t type, int fwd)
-{
-  struct MeshTunnelReliability *rel;
-  struct MeshFlowControl *next_fc;
-  struct MeshFlowControl *prev_fc;
-  struct MeshClient *c;
-  struct MeshClient *o;
-  GNUNET_PEER_Id hop;
-  uint32_t delta_mid;
-  uint32_t ack;
-  int delta;
-
-  rel     = fwd ? t->fwd_rel  : t->bck_rel;
-  c       = fwd ? t->client   : t->owner;
-  o       = fwd ? t->owner    : t->client;
-  next_fc = fwd ? &t->next_fc : &t->prev_fc;
-  prev_fc = fwd ? &t->prev_fc : &t->next_fc;
-  hop     = fwd ? t->prev_hop : t->next_hop;
-
-  switch (type)
-  {
-    case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
-    case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
-      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                  "ACK due to %s\n",
-                  GNUNET_MESH_DEBUG_M2S (type));
-      if (GNUNET_YES == t->nobuffer && (GNUNET_NO == t->reliable || NULL == c))
-      {
-        GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, nobuffer\n");
-        return;
-      }
-      if (GNUNET_YES == t->reliable && NULL != c)
-        tunnel_send_data_ack (t, fwd);
-      break;
-    case GNUNET_MESSAGE_TYPE_MESH_UNICAST_ACK:
-    case GNUNET_MESSAGE_TYPE_MESH_TO_ORIG_ACK:
-    case GNUNET_MESSAGE_TYPE_MESH_ACK:
-    case GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK:
-      break;
-    case GNUNET_MESSAGE_TYPE_MESH_POLL:
-    case GNUNET_MESSAGE_TYPE_MESH_PATH_ACK:
-      t->force_ack = GNUNET_YES;
-      break;
-    default:
-      GNUNET_break (0);
-  }
-
-  /* Check if we need to transmit the ACK */
-  if (NULL == o &&
-      prev_fc->last_ack_sent - prev_fc->last_pid_recv > 3 &&
-      GNUNET_NO == t->force_ack)
-  {
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, buffer free\n");
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                "  last pid recv: %u, last ack sent: %u\n",
-                prev_fc->last_pid_recv, prev_fc->last_ack_sent);
-    return;
-  }
-
-  /* Ok, ACK might be necessary, what PID to ACK? */
-  delta = t->queue_max - next_fc->queue_n;
-  if (NULL != o && GNUNET_YES == t->reliable && NULL != rel->head_sent)
-    delta_mid = rel->mid_sent - rel->head_sent->mid;
-  else
-    delta_mid = 0;
-  if (0 > delta || (GNUNET_YES == t->reliable &&
-                    NULL != o &&
-                    (10 < rel->n_sent || 64 <= delta_mid)))
-    delta = 0;
-  if (NULL != o && delta > 1)
-    delta = 1;
-  ack = prev_fc->last_pid_recv + delta;
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ACK %u\n", ack);
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-              " last pid %u, last ack %u, qmax %u, q %u\n",
-              prev_fc->last_pid_recv, prev_fc->last_ack_sent,
-              t->queue_max, next_fc->queue_n);
-  if (ack == prev_fc->last_ack_sent && GNUNET_NO == t->force_ack)
-  {
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending FWD ACK, not needed\n");
-    return;
-  }
-
-  prev_fc->last_ack_sent = ack;
-  if (NULL != o)
-    send_local_ack (t, o, fwd);
-  else if (0 != hop)
-    send_ack (t, hop, ack);
-  else
-    GNUNET_break (GNUNET_YES == t->destroy);
-  t->force_ack = GNUNET_NO;
-}
-
-
-/**
- * Modify the mesh message TID from global to local and send to client.
- *
- * @param t Tunnel on which to send the message.
- * @param msg Message to modify and send.
- * @param c Client to send to.
- * @param tid Tunnel ID to use (c can be both owner and client).
- */
-static void
-tunnel_send_client_to_tid (struct MeshTunnel *t,
-                           const struct GNUNET_MESH_Data *msg,
-                           struct MeshClient *c, MESH_TunnelNumber tid)
-{
-  struct GNUNET_MESH_LocalData *copy;
-  uint16_t size = ntohs (msg->header.size) - sizeof (struct GNUNET_MESH_Data);
-  char cbuf[size + sizeof (struct GNUNET_MESH_LocalData)];
-
-  if (size < sizeof (struct GNUNET_MessageHeader))
-  {
-    GNUNET_break_op (0);
-    return;
-  }
-  if (NULL == c)
-  {
-    GNUNET_break (0);
-    return;
-  }
-  copy = (struct GNUNET_MESH_LocalData *) cbuf;
-  memcpy (&copy[1], &msg[1], size);
-  copy->header.size = htons (sizeof (struct GNUNET_MESH_LocalData) + size);
-  copy->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_DATA);
-  copy->tid = htonl (tid);
-  GNUNET_SERVER_notification_context_unicast (nc, c->handle,
-                                              &copy->header, GNUNET_NO);
-}
-
-/**
- * Modify the unicast message TID from global to local and send to client.
- *
- * @param t Tunnel on which to send the message.
- * @param msg Message to modify and send.
- * @param fwd Forward?
- */
-static void
-tunnel_send_client_data (struct MeshTunnel *t,
-                         const struct GNUNET_MESH_Data *msg,
-                         int fwd)
-{
-  if (fwd)
-    tunnel_send_client_to_tid (t, msg, t->client, t->local_tid_dest);
-  else
-    tunnel_send_client_to_tid (t, msg, t->owner, t->local_tid);
-}
-
-
-/**
- * Send up to 64 buffered messages to the client for in order delivery.
- *
- * @param t Tunnel on which to empty the message buffer.
- * @param c Client to send to.
- * @param rel Reliability structure to corresponding peer.
- *            If rel == t->bck_rel, this is FWD data.
- */
-static void
-tunnel_send_client_buffered_data (struct MeshTunnel *t, struct MeshClient *c,
-                                  struct MeshTunnelReliability *rel)
-{
-  ;
-  struct MeshReliableMessage *copy;
-  struct MeshReliableMessage *next;
-
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "send_buffered_data\n");
-  for (copy = rel->head_recv; NULL != copy; copy = next)
-  {
-    next = copy->next;
-    if (copy->mid == rel->mid_recv)
-    {
-      struct GNUNET_MESH_Data *msg = (struct GNUNET_MESH_Data *) &copy[1];
-
-      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                  " have %u! now expecting %u\n",
-                  copy->mid, rel->mid_recv + 1);
-      tunnel_send_client_data (t, msg, (rel == t->bck_rel));
-      rel->mid_recv++;
-      GNUNET_CONTAINER_DLL_remove (rel->head_recv, rel->tail_recv, copy);
-      GNUNET_free (copy);
-    }
-    else
-    {
-      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                  " don't have %u, next is %u\n",
-                  rel->mid_recv,
-                  copy->mid);
-      return;
-    }
-  }
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "send_buffered_data END\n");
-}
-
-
-/**
- * We have received a message out of order, buffer it until we receive
- * the missing one and we can feed the rest to the client.
- *
- * @param t Tunnel to add to.
- * @param msg Message to buffer.
- * @param rel Reliability data to the corresponding direction.
- */
-static void
-tunnel_add_buffered_data (struct MeshTunnel *t,
-                           const struct GNUNET_MESH_Data *msg,
-                          struct MeshTunnelReliability *rel)
-{
-  struct MeshReliableMessage *copy;
-  struct MeshReliableMessage *prev;
-  uint32_t mid;
-  uint16_t size;
-
-  size = ntohs (msg->header.size);
-  mid = ntohl (msg->mid);
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "add_buffered_data %u\n", mid);
-
-  copy = GNUNET_malloc (sizeof (*copy) + size);
-  copy->mid = mid;
-  copy->rel = rel;
-  memcpy (&copy[1], msg, size);
-
-  // FIXME do something better than O(n), although n < 64...
-  // FIXME start from the end (most messages are the latest ones)
-  for (prev = rel->head_recv; NULL != prev; prev = prev->next)
-  {
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " prev %u\n", prev->mid);
-    if (GMC_is_pid_bigger (prev->mid, mid))
-    {
-      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " bingo!\n");
-      GNUNET_CONTAINER_DLL_insert_before (rel->head_recv, rel->tail_recv,
-                                          prev, copy);
-      return;
-    }
-  }
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " insert at tail!\n");
-  GNUNET_CONTAINER_DLL_insert_tail (rel->head_recv, rel->tail_recv, copy);
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "add_buffered_data END\n");
-}
-
-
-/**
- * Destroy a reliable message after it has been acknowledged, either by
- * direct mid ACK or bitfield. Updates the appropriate data structures and
- * timers and frees all memory.
- *
- * @param copy Message that is no longer needed: remote peer got it.
- */
-static void
-tunnel_free_reliable_message (struct MeshReliableMessage *copy)
-{
-  struct MeshTunnelReliability *rel;
-  struct GNUNET_TIME_Relative time;
-
-  rel = copy->rel;
-  time = GNUNET_TIME_absolute_get_duration (copy->timestamp);
-  rel->expected_delay.rel_value_us *= 7;
-  rel->expected_delay.rel_value_us += time.rel_value_us;
-  rel->expected_delay.rel_value_us /= 8;
-  rel->n_sent--;
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! Freeing %u\n", copy->mid);
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "    n_sent %u\n", rel->n_sent);
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!!  took %s\n",
-              GNUNET_STRINGS_relative_time_to_string (time, GNUNET_NO));
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!!  new expected delay %s\n",
-              GNUNET_STRINGS_relative_time_to_string (rel->expected_delay,
-                                                      GNUNET_NO));
-  rel->retry_timer = rel->expected_delay;
-  GNUNET_CONTAINER_DLL_remove (rel->head_sent, rel->tail_sent, copy);
-  GNUNET_free (copy);
-}
-
-
-/**
- * Destroy all reliable messages queued for a tunnel,
- * during a tunnel destruction.
- * Frees the reliability structure itself.
- *
- * @param rel Reliability data for a tunnel.
- */
-static void
-tunnel_free_reliable_all (struct MeshTunnelReliability *rel)
-{
-  struct MeshReliableMessage *copy;
-  struct MeshReliableMessage *next;
-
-  if (NULL == rel)
-    return;
-
-  for (copy = rel->head_recv; NULL != copy; copy = next)
-  {
-    next = copy->next;
-    GNUNET_CONTAINER_DLL_remove (rel->head_recv, rel->tail_recv, copy);
-    GNUNET_free (copy);
-  }
-  for (copy = rel->head_sent; NULL != copy; copy = next)
-  {
-    next = copy->next;
-    GNUNET_CONTAINER_DLL_remove (rel->head_sent, rel->tail_sent, copy);
-    GNUNET_free (copy);
-  }
-  if (GNUNET_SCHEDULER_NO_TASK != rel->retry_task)
-    GNUNET_SCHEDULER_cancel (rel->retry_task);
-  GNUNET_free (rel);
-}
-
-
-/**
- * Mark future messages as ACK'd.
- *
- * @param t Tunnel whose sent buffer to clean.
- * @param msg DataACK message with a bitfield of future ACK'd messages.
- * @param rel Reliability data.
- */
-static void
-tunnel_free_sent_reliable (struct MeshTunnel *t,
-                           const struct GNUNET_MESH_DataACK *msg,
-                           struct MeshTunnelReliability *rel)
-{
-  struct MeshReliableMessage *copy;
-  struct MeshReliableMessage *next;
-  uint64_t bitfield;
-  uint64_t mask;
-  uint32_t mid;
-  uint32_t target;
-  unsigned int i;
-
-  bitfield = msg->futures;
-  mid = ntohl (msg->mid);
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-              "free_sent_reliable %u %llX\n",
-              mid, bitfield);
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-              " rel %p, head %p\n",
-              rel, rel->head_sent);
-  for (i = 0, copy = rel->head_sent;
-       i < 64 && NULL != copy && 0 != bitfield;
-       i++)
-  {
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                " trying bit %u (mid %u)\n",
-                i, mid + i + 1);
-    mask = 0x1LL << i;
-    if (0 == (bitfield & mask))
-     continue;
-
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " set!\n");
-    /* Bit was set, clear the bit from the bitfield */
-    bitfield &= ~mask;
-
-    /* The i-th bit was set. Do we have that copy? */
-    /* Skip copies with mid < target */
-    target = mid + i + 1;
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " target %u\n", target);
-    while (NULL != copy && GMC_is_pid_bigger (target, copy->mid))
-     copy = copy->next;
-
-    /* Did we run out of copies? (previously freed, it's ok) */
-    if (NULL == copy)
-    {
-     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "run out of copies...\n");
-     return;
-    }
-
-    /* Did we overshoot the target? (previously freed, it's ok) */
-    if (GMC_is_pid_bigger (copy->mid, target))
-    {
-     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " next copy %u\n", copy->mid);
-     continue;
-    }
-
-    /* Now copy->mid == target, free it */
-    next = copy->next;
-    tunnel_free_reliable_message (copy);
-    copy = next;
-  }
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "free_sent_reliable END\n");
-}
-
-
-/**
- * We haven't received an ACK after a certain time: restransmit the message.
- *
- * @param cls Closure (MeshReliableMessage with the message to restransmit)
- * @param tc TaskContext.
- */
-static void
-tunnel_retransmit_message (void *cls,
-                           const struct GNUNET_SCHEDULER_TaskContext *tc)
-{
-  struct MeshTunnelReliability *rel = cls;
-  struct MeshReliableMessage *copy;
-  struct MeshFlowControl *fc;
-  struct MeshPeerQueue *q;
-  struct MeshPeer *pi;
-  struct MeshTunnel *t;
-  struct GNUNET_MESH_Data *payload;
-  GNUNET_PEER_Id hop;
-
-  rel->retry_task = GNUNET_SCHEDULER_NO_TASK;
-  if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
-    return;
-
-  t = rel->t;
-  copy = rel->head_sent;
-  if (NULL == copy)
-  {
-    GNUNET_break (0);
-    return;
-  }
-
-  /* Search the message to be retransmitted in the outgoing queue */
-  payload = (struct GNUNET_MESH_Data *) &copy[1];
-  hop = rel == t->fwd_rel ? t->next_hop : t->prev_hop;
-  fc  = rel == t->fwd_rel ? &t->prev_fc : &t->next_fc;
-  pi  = peer_get_short (hop);
-  for (q = pi->queue_head; NULL != q; q = q->next)
-  {
-    if (ntohs (payload->header.type) == q->type)
-    {
-      struct GNUNET_MESH_Data *queued_data = q->cls;
-
-      if (queued_data->mid == payload->mid)
-        break;
-    }
-  }
-
-  /* Message not found in the queue */
-  if (NULL == q)
-  {
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! RETRANSMIT %u\n", copy->mid);
-
-    fc->last_ack_sent++;
-    fc->last_pid_recv++;
-    payload->pid = htonl (fc->last_pid_recv);
-    send_prebuilt_message (&payload->header, hop, t);
-    GNUNET_STATISTICS_update (stats, "# data retransmitted", 1, GNUNET_NO);
-  }
-  else
-  {
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! STILL IN QUEUE %u\n", copy->mid);
-  }
-
-  rel->retry_timer = GNUNET_TIME_STD_BACKOFF (rel->retry_timer);
-  rel->retry_task = GNUNET_SCHEDULER_add_delayed (rel->retry_timer,
-                                                  &tunnel_retransmit_message,
-                                                  cls);
-}
-
-
-/**
- * Send keepalive packets for a tunnel.
- *
- * @param t Tunnel to keep alive..
- * @param fwd Is this a FWD keepalive? (owner -> dest).
- */
-static void
-tunnel_keepalive (struct MeshTunnel *t, int fwd)
-{
-  struct GNUNET_MESH_TunnelKeepAlive *msg;
-  size_t size = sizeof (struct GNUNET_MESH_TunnelKeepAlive);
-  char cbuf[size];
-  GNUNET_PEER_Id hop;
-  uint16_t type;
-
-  type = fwd ? GNUNET_MESSAGE_TYPE_MESH_FWD_KEEPALIVE :
-               GNUNET_MESSAGE_TYPE_MESH_BCK_KEEPALIVE;
-  hop  = fwd ? t->next_hop : t->prev_hop;
-
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-              "sending %s keepalive for tunnel %d\n",
-              fwd ? "FWD" : "BCK", t->id.tid);
-
-  msg = (struct GNUNET_MESH_TunnelKeepAlive *) cbuf;
-  msg->header.size = htons (size);
-  msg->header.type = htons (type);
-  msg->oid = *(GNUNET_PEER_resolve2 (t->id.oid));
-  msg->tid = htonl (t->id.tid);
-  send_prebuilt_message (&msg->header, hop, t);
-}
-
-
-/**
- * Send create (PATH_CREATE/PATH_ACK) packets for a tunnel.
- *
- * @param t Tunnel for which to send the message.
- * @param fwd If GNUNET_YES, send CREATE, otherwise send ACK.
- */
-static void
-tunnel_recreate (struct MeshTunnel *t, int fwd)
-{
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-              "sending path recreate for tunnel %s[%X]\n",
-              GNUNET_i2s (GNUNET_PEER_resolve2 (t->id.oid)), t->id.tid);
-  if (fwd)
-    send_path_create (t);
-  else
-    send_path_ack (t);
-}
-
-
-/**
- * Generic tunnel timer management.
- * Depending on the role of the peer in the tunnel will send the
- * appropriate message (build or keepalive)
- *
- * @param t Tunnel to maintain.
- * @param fwd Is FWD?
- */
-static void
-tunnel_maintain (struct MeshTunnel *t, int fwd)
-{
-  switch (t->state)
-  {
-    case MESH_TUNNEL_NEW:
-      GNUNET_break (0);
-    case MESH_TUNNEL_SEARCHING:
-      /* TODO DHT GET with RO_BART */
-      break;
-    case MESH_TUNNEL_WAITING:
-      tunnel_recreate (t, fwd);
-      break;
-    case MESH_TUNNEL_READY:
-      tunnel_keepalive (t, fwd);
-      break;
-    default:
-      break;
-  }
-}
-
-
-static void
-tunnel_fwd_keepalive (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
-{
-  struct MeshTunnel *t = cls;
-
-  t->fwd_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
-  if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) ||
-      NULL == t->owner)
-    return;
-
-  tunnel_maintain (t, GNUNET_YES);
-  t->fwd_maintenance_task = GNUNET_SCHEDULER_add_delayed (refresh_path_time,
-                                                          
&tunnel_fwd_keepalive,
-                                                          t);
-}
-
-
-static void
-tunnel_bck_keepalive (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
-{
-  struct MeshTunnel *t = cls;
-
-  t->bck_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
-  if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) ||
-      NULL == t->client)
-    return;
-
-  tunnel_keepalive (t, GNUNET_NO);
-  t->bck_maintenance_task = GNUNET_SCHEDULER_add_delayed (refresh_path_time,
-                                                          
&tunnel_bck_keepalive,
-                                                          t);
-}
-
-
-/**
- * Send a message to all peers and clients in this tunnel that the tunnel
- * is no longer valid. If some peer or client should not receive the message,
- * should be zero'ed out before calling this function.
- *
- * @param t The tunnel whose peers and clients to notify.
- */
-static void
-tunnel_send_destroy (struct MeshTunnel *t)
-{
-  struct GNUNET_MESH_TunnelDestroy msg;
-  struct GNUNET_PeerIdentity id;
-
-  msg.header.size = htons (sizeof (msg));
-  msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY);
-  GNUNET_PEER_resolve (t->id.oid, &msg.oid);
-  msg.tid = htonl (t->id.tid);
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-              "  sending tunnel destroy for tunnel: %s [%X]\n",
-              GNUNET_i2s (&msg.oid), t->id.tid);
-
-  if (NULL == t->client && 0 != t->next_hop)
-  {
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "  child: %u\n", t->next_hop);
-    GNUNET_PEER_resolve (t->next_hop, &id);
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                "  sending forward to %s\n",
-                GNUNET_i2s (&id));
-    send_prebuilt_message (&msg.header, t->next_hop, t);
-  }
-  if (NULL == t->owner && 0 != t->prev_hop)
-  {
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "  parent: %u\n", t->prev_hop);
-    GNUNET_PEER_resolve (t->prev_hop, &id);
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                "  sending back to %s\n",
-                GNUNET_i2s (&id));
-    send_prebuilt_message (&msg.header, t->prev_hop, t);
-  }
-  if (NULL != t->owner)
-  {
-    send_local_tunnel_destroy (t, GNUNET_NO);
-  }
-  if (NULL != t->client)
-  {
-    send_local_tunnel_destroy (t, GNUNET_YES);
-  }
-}
-
-static int
-tunnel_destroy (struct MeshTunnel *t)
-{
-  struct MeshClient *c;
-  struct GNUNET_HashCode hash;
-  int r;
-
-  if (NULL == t)
-    return GNUNET_OK;
-
-  r = GNUNET_OK;
-  c = t->owner;
-
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "destroying tunnel %s [%x]\n",
-              GNUNET_i2s (GNUNET_PEER_resolve2 (t->id.oid)), t->id.tid);
-  if (NULL != c)
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "  by client %u\n", c->id);
-
-  GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
-  if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (tunnels, &hash, t))
-  {
-    GNUNET_break (0);
-    r = GNUNET_SYSERR;
-  }
-
-  if (NULL != c)
-  {
-    if (GNUNET_YES != GNUNET_CONTAINER_multihashmap32_remove (c->own_tunnels,
-                                                              t->local_tid, t))
-    {
-      GNUNET_break (0);
-      r = GNUNET_SYSERR;
-    }
-  }
-
-  c = t->client;
-  if (NULL != c)
-  {
-    if (GNUNET_YES !=
-        GNUNET_CONTAINER_multihashmap32_remove (c->incoming_tunnels,
-                                                t->local_tid_dest, t))
-    {
-      GNUNET_break (0);
-      r = GNUNET_SYSERR;
-    }
-    if (GNUNET_YES !=
-        GNUNET_CONTAINER_multihashmap32_remove (incoming_tunnels,
-                                                t->local_tid_dest, t))
-    {
-      GNUNET_break (0);
-      r = GNUNET_SYSERR;
-    }
-  }
-
-  if(GNUNET_YES == t->reliable)
-  {
-    tunnel_free_reliable_all (t->fwd_rel);
-    tunnel_free_reliable_all (t->bck_rel);
-  }
-  if (0 != t->prev_hop)
-  {
-    peer_cancel_queues (t->prev_hop, t);
-    GNUNET_PEER_change_rc (t->prev_hop, -1);
-  }
-  if (0 != t->next_hop)
-  {
-    peer_cancel_queues (t->next_hop, t);
-    GNUNET_PEER_change_rc (t->next_hop, -1);
-  }
-  if (GNUNET_SCHEDULER_NO_TASK != t->next_fc.poll_task)
-  {
-    GNUNET_SCHEDULER_cancel (t->next_fc.poll_task);
-    t->next_fc.poll_task = GNUNET_SCHEDULER_NO_TASK;
-  }
-  if (GNUNET_SCHEDULER_NO_TASK != t->prev_fc.poll_task)
-  {
-    GNUNET_SCHEDULER_cancel (t->prev_fc.poll_task);
-    t->prev_fc.poll_task = GNUNET_SCHEDULER_NO_TASK;
-  }
-  if (0 != t->dest) {
-    peer_remove_tunnel (peer_get_short (t->dest), t);
-  }
-
-  if (GNUNET_SCHEDULER_NO_TASK != t->fwd_maintenance_task)
-    GNUNET_SCHEDULER_cancel (t->fwd_maintenance_task);
-  if (GNUNET_SCHEDULER_NO_TASK != t->bck_maintenance_task)
-    GNUNET_SCHEDULER_cancel (t->bck_maintenance_task);
-
-  n_tunnels--;
-  GNUNET_STATISTICS_update (stats, "# tunnels", -1, GNUNET_NO);
-  path_destroy (t->path);
-  GNUNET_free (t);
-  return r;
-}
-
-/**
- * Tunnel is empty: destroy it.
- *
- * Notifies all participants (peers, cleints) about the destruction.
- *
- * @param t Tunnel to destroy.
- */
-static void
-tunnel_destroy_empty (struct MeshTunnel *t)
-{
-  #if MESH_DEBUG
-  {
-    struct GNUNET_PeerIdentity id;
-
-    GNUNET_PEER_resolve (t->id.oid, &id);
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                "executing destruction of empty tunnel %s [%X]\n",
-                GNUNET_i2s (&id), t->id.tid);
-  }
-  #endif
-
-  if (GNUNET_NO == t->destroy)
-    tunnel_send_destroy (t);
-  if (0 == t->pending_messages)
-    tunnel_destroy (t);
-  else
-    t->destroy = GNUNET_YES;
-}
-
-/**
- * Initialize a Flow Control structure to the initial state.
- *
- * @param fc Flow Control structure to initialize.
- */
-static void
-fc_init (struct MeshFlowControl *fc)
-{
-  fc->last_pid_sent = (uint32_t) -1; /* Next (expected) = 0 */
-  fc->last_pid_recv = (uint32_t) -1;
-  fc->last_ack_sent = (uint32_t) -1; /* No traffic allowed yet */
-  fc->last_ack_recv = (uint32_t) -1;
-  fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
-  fc->poll_time = GNUNET_TIME_UNIT_SECONDS;
-  fc->queue_n = 0;
-}
-
-/**
- * Create a new tunnel
- *
- * @param owner Who is the owner of the tunnel (short ID).
- * @param tid Tunnel Number of the tunnel.
- * @param client Clients that owns the tunnel, NULL for foreign tunnels.
- * @param local Tunnel Number for the tunnel, for the client point of view.
- *
- * @return A new initialized tunnel. NULL on error.
- */
-static struct MeshTunnel *
-tunnel_new (GNUNET_PEER_Id owner,
-            MESH_TunnelNumber tid,
-            struct MeshClient *client,
-            MESH_TunnelNumber local)
-{
-  struct MeshTunnel *t;
-  struct GNUNET_HashCode hash;
-
-  if (n_tunnels >= max_tunnels && NULL == client)
-    return NULL;
-
-  t = GNUNET_malloc (sizeof (struct MeshTunnel));
-  t->id.oid = owner;
-  t->id.tid = tid;
-  t->queue_max = (max_msgs_queue / max_tunnels) + 1;
-  t->owner = client;
-  fc_init (&t->next_fc);
-  fc_init (&t->prev_fc);
-  t->next_fc.t = t;
-  t->prev_fc.t = t;
-  t->local_tid = local;
-  n_tunnels++;
-  GNUNET_STATISTICS_update (stats, "# tunnels", 1, GNUNET_NO);
-
-  GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
-  if (GNUNET_OK !=
-      GNUNET_CONTAINER_multihashmap_put (tunnels, &hash, t,
-                                         
GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
-  {
-    GNUNET_break (0);
-    tunnel_destroy (t);
-    if (NULL != client)
-    {
-      GNUNET_break (0);
-      GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
-    }
-    return NULL;
-  }
-
-  if (NULL != client)
-  {
-    if (GNUNET_OK !=
-        GNUNET_CONTAINER_multihashmap32_put (client->own_tunnels,
-                                             t->local_tid, t,
-                                             
GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
-    {
-      tunnel_destroy (t);
-      GNUNET_break (0);
-      GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
-      return NULL;
-    }
-  }
-
-  return t;
-}
-
-
-/**
- * Set options in a tunnel, extracted from a bit flag field
- *
- * @param t Tunnel to set options to.
- * @param options Bit array in host byte order.
- */
-static void
-tunnel_set_options (struct MeshTunnel *t, uint32_t options)
-{
-  t->nobuffer = (options & GNUNET_MESH_OPTION_NOBUFFER) != 0 ?
-                 GNUNET_YES : GNUNET_NO;
-  t->reliable = (options & GNUNET_MESH_OPTION_RELIABLE) != 0 ?
-                 GNUNET_YES : GNUNET_NO;
-}
-
-
-/**
- * Iterator for deleting each tunnel whose client endpoint disconnected.
- *
- * @param cls Closure (client that has disconnected).
- * @param key The local tunnel id (used to access the hashmap).
- * @param value The value stored at the key (tunnel to destroy).
- *
- * @return GNUNET_OK, keep iterating.
- */
-static int
-tunnel_destroy_iterator (void *cls,
-                         uint32_t key,
-                         void *value)
-{
-  struct MeshTunnel *t = value;
-  struct MeshClient *c = cls;
-
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-              " Tunnel %X / %X destroy, due to client %u shutdown.\n",
-              t->local_tid, t->local_tid_dest, c->id);
-  client_delete_tunnel (c, t);
-  if (c == t->client)
-  {
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Client %u is destination.\n", 
c->id);
-    t->client = NULL;
-    if (0 != t->next_hop) { /* destroy could come before a path is used */
-        GNUNET_PEER_change_rc (t->next_hop, -1);
-        t->next_hop = 0;
-    }
-  }
-  if (c == t->owner)
-  {
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Client %u is owner.\n", c->id);
-    t->owner = NULL;
-    if (0 != t->prev_hop) { /* destroy could come before a path is used */
-        GNUNET_PEER_change_rc (t->prev_hop, -1);
-        t->prev_hop = 0;
-    }
-  }
-
-  tunnel_destroy_empty (t);
-
-  return GNUNET_OK;
-}
-
-
-/**
- * remove client's ports from the global hashmap on diconnect.
- *
- * @param cls Closure (unused).
- * @param key Port.
- * @param value ThClient structure.
- *
- * @return GNUNET_OK, keep iterating.
- */
-static int
-client_release_ports (void *cls,
-                      uint32_t key,
-                      void *value)
-{
-  int res;
-
-  res = GNUNET_CONTAINER_multihashmap32_remove (ports, key, value);
-  if (GNUNET_YES != res)
-  {
-    GNUNET_break (0);
-    GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
-                "Port %u by client %p was not registered.\n",
-                key, value);
-  }
-  return GNUNET_OK;
-}
-
-/**
- * Timeout function due to lack of keepalive/traffic from the owner.
- * Destroys tunnel if called.
- *
- * @param cls Closure (tunnel to destroy).
- * @param tc TaskContext
- */
-static void
-tunnel_fwd_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
-{
-  struct MeshTunnel *t = cls;
-
-  t->fwd_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
-  if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
-    return;
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-              "Tunnel %s [%X] FWD timed out. Destroying.\n",
-              GNUNET_i2s(GNUNET_PEER_resolve2 (t->id.oid)), t->id.tid);
-  if (NULL != t->client)
-    send_local_tunnel_destroy (t, GNUNET_YES);
-  tunnel_destroy (t); /* Do not notify other */
-}
-
-
-/**
- * Timeout function due to lack of keepalive/traffic from the destination.
- * Destroys tunnel if called.
- *
- * @param cls Closure (tunnel to destroy).
- * @param tc TaskContext
- */
-static void
-tunnel_bck_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
-{
-  struct MeshTunnel *t = cls;
-
-  t->bck_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
-  if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
-    return;
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-              "Tunnel %s [%X] BCK timed out. Destroying.\n",
-              GNUNET_i2s(GNUNET_PEER_resolve2 (t->id.oid)), t->id.tid);
-  if (NULL != t->owner)
-    send_local_tunnel_destroy (t, GNUNET_NO);
-  tunnel_destroy (t); /* Do not notify other */
-}
-
-
-/**
- * Resets the tunnel timeout task, some other message has done the task's job.
- * - For the first peer on the direction this means to send
- *   a keepalive or a path confirmation message (either create or ACK).
- * - For all other peers, this means to destroy the tunnel,
- *   due to lack of activity.
- * Starts the tiemout if no timeout was running (tunnel just created).
- *
- * @param t Tunnel whose timeout to reset.
- * @param fwd Is this forward?
- *
- * TODO use heap to improve efficiency of scheduler.
- */
-static void
-tunnel_reset_timeout (struct MeshTunnel *t, int fwd)
-{
-  GNUNET_SCHEDULER_TaskIdentifier *ti;
-  GNUNET_SCHEDULER_Task f;
-  struct MeshClient *c;
-
-  ti = fwd ? &t->fwd_maintenance_task : &t->bck_maintenance_task;
-  c  = fwd ? t->owner                 : t->client;
-
-  if (GNUNET_SCHEDULER_NO_TASK != *ti)
-    GNUNET_SCHEDULER_cancel (*ti);
-
-  if (NULL != c)
-  {
-    f  = fwd ? &tunnel_fwd_keepalive : &tunnel_bck_keepalive;
-    *ti = GNUNET_SCHEDULER_add_delayed (refresh_path_time, f, t);
-  }
-  else
-  {
-    f  = fwd ? &tunnel_fwd_timeout : &tunnel_bck_timeout;
-    *ti = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
-                                            (refresh_path_time, 4),
-                                        f, t);
-  }
-}
-
-
-/******************************************************************************/
-/****************      MESH NETWORK HANDLER HELPERS     
***********************/
-/******************************************************************************/
-
-/**
- * Free a transmission that was already queued with all resources
- * associated to the request.
- *
- * @param queue Queue handler to cancel.
- * @param clear_cls Is it necessary to free associated cls?
- */
-static void
-queue_destroy (struct MeshPeerQueue *queue, int clear_cls)
-{
-  struct MeshFlowControl *fc;
-
-  if (GNUNET_YES == clear_cls)
-  {
-    switch (queue->type)
-    {
-      case GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY:
-        GNUNET_log (GNUNET_ERROR_TYPE_INFO, "   cancelling TUNNEL_DESTROY\n");
-        GNUNET_break (GNUNET_YES == queue->tunnel->destroy);
-        /* fall through */
-      case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
-      case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
-      case GNUNET_MESSAGE_TYPE_MESH_ACK:
-      case GNUNET_MESSAGE_TYPE_MESH_UNICAST_ACK:
-      case GNUNET_MESSAGE_TYPE_MESH_TO_ORIG_ACK:
-      case GNUNET_MESSAGE_TYPE_MESH_POLL:
-      case GNUNET_MESSAGE_TYPE_MESH_FWD_KEEPALIVE:
-      case GNUNET_MESSAGE_TYPE_MESH_BCK_KEEPALIVE:
-        GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                    "   prebuilt message\n");
-        GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                    "   type %s\n",
-                    GNUNET_MESH_DEBUG_M2S (queue->type));
-        break;
-      case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
-        GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "   type create path\n");
-        break;
-      default:
-        GNUNET_break (0);
-        GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
-                    "   type %s unknown!\n",
-                    GNUNET_MESH_DEBUG_M2S (queue->type));
-    }
-    GNUNET_free_non_null (queue->cls);
-  }
-  GNUNET_CONTAINER_DLL_remove (queue->peer->queue_head,
-                               queue->peer->queue_tail,
-                               queue);
-
-  /* Delete from appropriate fc in the tunnel */
-  if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == queue->type ||
-      GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == queue->type )
-  {
-    if (queue->peer->id == queue->tunnel->prev_hop)
-      fc = &queue->tunnel->prev_fc;
-    else if (queue->peer->id == queue->tunnel->next_hop)
-      fc = &queue->tunnel->next_fc;
-    else
-    {
-      GNUNET_break (0);
-      GNUNET_free (queue);
-      return;
-    }
-    fc->queue_n--;
-  }
-  GNUNET_free (queue);
-}
-
-
-/**
- * @brief Get the next transmittable message from the queue.
- *
- * This will be the head, except in the case of being a data packet
- * not allowed by the destination peer.
- *
- * @param peer Destination peer.
- *
- * @return The next viable MeshPeerQueue element to send to that peer.
- *         NULL when there are no transmittable messages.
- */
-struct MeshPeerQueue *
-queue_get_next (const struct MeshPeer *peer)
-{
-  struct MeshPeerQueue *q;
-
-  struct GNUNET_MESH_Data *dmsg;
-  struct MeshTunnel* t;
-  uint32_t pid;
-  uint32_t ack;
-
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "*   selecting message\n");
-  for (q = peer->queue_head; NULL != q; q = q->next)
-  {
-    t = q->tunnel;
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                "*     %s\n",
-                GNUNET_MESH_DEBUG_M2S (q->type));
-    dmsg = (struct GNUNET_MESH_Data *) q->cls;
-    pid = ntohl (dmsg->pid);
-    switch (q->type)
-    {
-      case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
-        ack = t->next_fc.last_ack_recv;
-        break;
-      case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
-        ack = t->prev_fc.last_ack_recv;
-        break;
-      default:
-        GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                    "*   OK!\n");
-        return q;
-    }
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                "*     ACK: %u, PID: %u, MID: %u\n",
-                ack, pid, ntohl (dmsg->mid));
-    if (GNUNET_NO == GMC_is_pid_bigger (pid, ack))
-    {
-      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                  "*   OK!\n");
-      return q;
-    }
-    else
-    {
-      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                  "*     NEXT!\n");
-    }
-  }
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                "*   nothing found\n");
-  return NULL;
-}
-
-
-static size_t
-queue_send (void *cls, size_t size, void *buf)
-{
-  struct MeshPeer *peer = cls;
-  struct GNUNET_MessageHeader *msg;
-  struct MeshPeerQueue *queue;
-  struct MeshTunnel *t;
-  struct GNUNET_PeerIdentity dst_id;
-  struct MeshFlowControl *fc;
-  size_t data_size;
-  uint32_t pid;
-  uint16_t type;
-
-  peer->core_transmit = NULL;
-
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Queue send\n");
-
-  if (NULL == buf || 0 == size)
-  {
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Buffer size 0.\n");
-    return 0;
-  }
-  queue = queue_get_next (peer);
-
-  /* Queue has no internal mesh traffic nor sendable payload */
-  if (NULL == queue)
-  {
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "*   not ready, return\n");
-    if (NULL == peer->queue_head)
-      GNUNET_break (0); /* Core tmt_rdy should've been canceled */
-    return 0;
-  }
-
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "*   not empty\n");
-
-  GNUNET_PEER_resolve (peer->id, &dst_id);
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-              "*   towards %s\n",
-              GNUNET_i2s (&dst_id));
-  /* Check if buffer size is enough for the message */
-  if (queue->size > size)
-  {
-      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                  "*   not enough room, reissue\n");
-      peer->core_transmit =
-          GNUNET_CORE_notify_transmit_ready (core_handle,
-                                             GNUNET_NO,
-                                             0,
-                                             GNUNET_TIME_UNIT_FOREVER_REL,
-                                             &dst_id,
-                                             queue->size,
-                                             &queue_send,
-                                             peer);
-      return 0;
-  }
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "*   size ok\n");
-
-  t = queue->tunnel;
-  GNUNET_assert (0 < t->pending_messages);
-  t->pending_messages--;
-  type = 0;
-
-  /* Fill buf */
-  switch (queue->type)
-  {
-    case 0:
-    case GNUNET_MESSAGE_TYPE_MESH_ACK:
-    case GNUNET_MESSAGE_TYPE_MESH_UNICAST_ACK:
-    case GNUNET_MESSAGE_TYPE_MESH_TO_ORIG_ACK:
-    case GNUNET_MESSAGE_TYPE_MESH_POLL:
-    case GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN:
-    case GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY:
-    case GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY:
-    case GNUNET_MESSAGE_TYPE_MESH_FWD_KEEPALIVE:
-    case GNUNET_MESSAGE_TYPE_MESH_BCK_KEEPALIVE:
-      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                  "*   raw: %s\n",
-                  GNUNET_MESH_DEBUG_M2S (queue->type));
-      /* Fall through */
-    case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
-    case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
-      data_size = send_core_data_raw (queue->cls, size, buf);
-      msg = (struct GNUNET_MessageHeader *) buf;
-      type = ntohs (msg->type);
-      break;
-    case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
-      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "*   path create\n");
-      data_size = send_core_path_create (queue->cls, size, buf);
-      break;
-    case GNUNET_MESSAGE_TYPE_MESH_PATH_ACK:
-      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "*   path ack\n");
-      data_size = send_core_path_ack (queue->cls, size, buf);
-      break;
-    default:
-      GNUNET_break (0);
-      GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
-                  "*   type unknown: %u\n",
-                  queue->type);
-      data_size = 0;
-  }
-
-  if (0 < drop_percent &&
-      GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, 101) < drop_percent)
-  {
-    GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
-                "Dropping message of type %s\n",
-                GNUNET_MESH_DEBUG_M2S(queue->type));
-    data_size = 0;
-  }
-  /* Free queue, but cls was freed by send_core_* */
-  queue_destroy (queue, GNUNET_NO);
-
-  /* Send ACK if needed, after accounting for sent ID in fc->queue_n */
-  pid = ((struct GNUNET_MESH_Data *) buf)->pid;
-  pid = ntohl (pid);
-  switch (type)
-  {
-    case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
-      t->next_fc.last_pid_sent = pid;
-      tunnel_send_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST, GNUNET_YES);
-      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                  "!!! FWD  %u\n",
-                  ntohl ( ((struct GNUNET_MESH_Data *) buf)->mid ) );
-      break;
-    case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
-      t->prev_fc.last_pid_sent = pid;
-      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                  "!!! BCK  %u\n",
-                  ntohl ( ((struct GNUNET_MESH_Data *) buf)->mid ) );
-      tunnel_send_ack (t, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, GNUNET_NO);
-      break;
-    default:
-      break;
-  }
-
-  /* If more data in queue, send next */
-  queue = queue_get_next (peer);
-  if (NULL != queue)
-  {
-      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "*   more data!\n");
-      if (NULL == peer->core_transmit) {
-        peer->core_transmit =
-            GNUNET_CORE_notify_transmit_ready(core_handle,
-                                              0,
-                                              0,
-                                              GNUNET_TIME_UNIT_FOREVER_REL,
-                                              &dst_id,
-                                              queue->size,
-                                              &queue_send,
-                                              peer);
-      }
-      else
-      {
-        GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                    "*   tmt rdy called somewhere else\n");
-      }
-  }
-  if (peer->id == t->next_hop)
-    fc = &t->next_fc;
-  else if (peer->id == t->prev_hop)
-    fc = &t->prev_fc;
-  else
-  {
-    GNUNET_break (0);
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "id: %u, next: %u, prev: %u\n",
-                peer->id, t->next_hop, t->prev_hop);
-    return data_size;
-  }
-  if (NULL != peer->queue_head)
-  {
-    if (GNUNET_SCHEDULER_NO_TASK == fc->poll_task && fc->queue_n > 0)
-    {
-      GNUNET_log (GNUNET_ERROR_TYPE_INFO,
-                  "*   %s starting poll timeout\n",
-                  GNUNET_i2s (&my_full_id));
-      fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time,
-                                                    &tunnel_poll, fc);
-    }
-  }
-  else
-  {
-    if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task)
-    {
-      GNUNET_SCHEDULER_cancel (fc->poll_task);
-      fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
-    }
-  }
-  if (GNUNET_YES == t->destroy && 0 == t->pending_messages)
-  {
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "*  destroying tunnel!\n");
-    tunnel_destroy (t);
-  }
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "*  Return %d\n", data_size);
-  return data_size;
-}
-
-
-/**
- * @brief Queue and pass message to core when possible.
- *
- * If type is payload (UNICAST, TO_ORIGIN) checks for queue status and
- * accounts for it. In case the queue is full, the message is dropped and
- * a break issued.
- *
- * Otherwise, message is treated as internal and allowed to go regardless of
- * queue status.
- *
- * @param cls Closure (@c type dependant). It will be used by queue_send to
- *            build the message to be sent if not already prebuilt.
- * @param type Type of the message, 0 for a raw message.
- * @param size Size of the message.
- * @param dst Neighbor to send message to.
- * @param t Tunnel this message belongs to.
- */
-static void
-queue_add (void *cls, uint16_t type, size_t size,
-           struct MeshPeer *dst, struct MeshTunnel *t)
-{
-  struct MeshPeerQueue *queue;
-  struct MeshFlowControl *fc;
-  int priority;
-
-  fc = NULL;
-  priority = GNUNET_NO;
-  if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == type)
-  {
-    fc = &t->next_fc;
-  }
-  else if (GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == type)
-  {
-    fc = &t->prev_fc;
-  }
-  if (NULL != fc)
-  {
-    if (fc->queue_n >= t->queue_max)
-    {
-      /* If this isn't a retransmission, drop the message */
-      if (GNUNET_NO == t->reliable ||
-          (NULL == t->owner && GNUNET_MESSAGE_TYPE_MESH_UNICAST == type) ||
-          (NULL == t->client && GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == type))
-      {
-        GNUNET_STATISTICS_update (stats, "# messages dropped (buffer full)",
-                                  1, GNUNET_NO);
-        GNUNET_break (0);
-        GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                    "queue full: %u/%u\n",
-                    fc->queue_n, t->queue_max);
-        return; /* Drop this message */
-      }
-      priority = GNUNET_YES;
-    }
-    fc->queue_n++;
-    if (GMC_is_pid_bigger(fc->last_pid_sent + 1, fc->last_ack_recv) &&
-        GNUNET_SCHEDULER_NO_TASK == fc->poll_task)
-      fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time,
-                                                    &tunnel_poll,
-                                                    fc);
-  }
-  queue = GNUNET_malloc (sizeof (struct MeshPeerQueue));
-  queue->cls = cls;
-  queue->type = type;
-  queue->size = size;
-  queue->peer = dst;
-  queue->tunnel = t;
-  if (GNUNET_YES == priority)
-  {
-    struct GNUNET_MESH_Data *d;
-    uint32_t prev;
-    uint32_t next;
-
-    GNUNET_CONTAINER_DLL_insert (dst->queue_head, dst->queue_tail, queue);
-    d = (struct GNUNET_MESH_Data *) queue->cls;
-    prev = d->pid;
-    for (queue = dst->queue_tail; NULL != queue; queue = queue->prev)
-    {
-      if (queue->type != type)
-        continue;
-      d = (struct GNUNET_MESH_Data *) queue->cls;
-      next = d->pid;
-      d->pid = prev;
-      prev = next;
-    }
-  }
-  else
-    GNUNET_CONTAINER_DLL_insert_tail (dst->queue_head, dst->queue_tail, queue);
-
-  if (NULL == dst->core_transmit)
-  {
-    dst->core_transmit =
-        GNUNET_CORE_notify_transmit_ready (core_handle,
-                                           0,
-                                           0,
-                                           GNUNET_TIME_UNIT_FOREVER_REL,
-                                           GNUNET_PEER_resolve2 (dst->id),
-                                           size,
-                                           &queue_send,
-                                           dst);
-  }
-  t->pending_messages++;
-}
-
-
-/******************************************************************************/
-/********************      MESH NETWORK HANDLERS     
**************************/
-/******************************************************************************/
-
-
-/**
- * Core handler for path creation
- *
- * @param cls closure
- * @param message message
- * @param peer peer identity this notification is about
- *
- * @return GNUNET_OK to keep the connection open,
- *         GNUNET_SYSERR to close it (signal serious error)
- */
-static int
-handle_mesh_path_create (void *cls, const struct GNUNET_PeerIdentity *peer,
-                         const struct GNUNET_MessageHeader *message)
-{
-  unsigned int own_pos;
-  uint16_t size;
-  uint16_t i;
-  MESH_TunnelNumber tid;
-  struct GNUNET_MESH_CreateTunnel *msg;
-  struct GNUNET_PeerIdentity *pi;
-  struct MeshPeerPath *path;
-  struct MeshPeer *dest_peer_info;
-  struct MeshPeer *orig_peer_info;
-  struct MeshTunnel *t;
-
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-              "Received a path create msg [%s]\n",
-              GNUNET_i2s (&my_full_id));
-  size = ntohs (message->size);
-  if (size < sizeof (struct GNUNET_MESH_CreateTunnel))
-  {
-    GNUNET_break_op (0);
-    return GNUNET_OK;
-  }
-
-  size -= sizeof (struct GNUNET_MESH_CreateTunnel);
-  if (size % sizeof (struct GNUNET_PeerIdentity))
-  {
-    GNUNET_break_op (0);
-    return GNUNET_OK;
-  }
-  size /= sizeof (struct GNUNET_PeerIdentity);
-  if (size < 1)
-  {
-    GNUNET_break_op (0);
-    return GNUNET_OK;
-  }
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "    path has %u hops.\n", size);
-  msg = (struct GNUNET_MESH_CreateTunnel *) message;
-
-  tid = ntohl (msg->tid);
-  pi = (struct GNUNET_PeerIdentity *) &msg[1];
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-              "    path is for tunnel %s[%X]:%u.\n",
-              GNUNET_i2s (pi), tid, ntohl (msg->port));
-  t = tunnel_get (pi, tid);
-  if (NULL == t) /* might be a local tunnel */
-  {
-    uint32_t opt;
-
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "  Creating tunnel\n");
-    t = tunnel_new (GNUNET_PEER_intern (pi), tid, NULL, 0);
-    if (NULL == t)
-    {
-      GNUNET_break (0);
-      return GNUNET_OK;
-    }
-    t->port = ntohl (msg->port);
-    opt = ntohl (msg->opt);
-    if (0 != (opt & GNUNET_MESH_OPTION_NOBUFFER))
-    {
-      t->nobuffer = GNUNET_YES;
-      t->queue_max = 1;
-    }
-    if (0 != (opt & GNUNET_MESH_OPTION_RELIABLE))
-    {
-      t->reliable = GNUNET_YES;
-    }
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "  nobuffer:%d\n", t->nobuffer);
-  }
-  tunnel_reset_timeout (t, GNUNET_YES);
-  tunnel_change_state (t,  MESH_TUNNEL_WAITING);
-  dest_peer_info =
-      GNUNET_CONTAINER_multipeermap_get (peers, &pi[size - 1]);
-  if (NULL == dest_peer_info)
-  {
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                "  Creating PeerInfo for destination.\n");
-    dest_peer_info = GNUNET_malloc (sizeof (struct MeshPeer));
-    dest_peer_info->id = GNUNET_PEER_intern (&pi[size - 1]);
-    GNUNET_CONTAINER_multipeermap_put (peers, &pi[size - 1],
-                                       dest_peer_info,
-                                       
GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
-  }
-  orig_peer_info = GNUNET_CONTAINER_multipeermap_get (peers, pi);
-  if (NULL == orig_peer_info)
-  {
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                "  Creating PeerInfo for origin.\n");
-    orig_peer_info = GNUNET_new (struct MeshPeer);
-    orig_peer_info->id = GNUNET_PEER_intern (pi);
-    GNUNET_CONTAINER_multipeermap_put (peers, pi, orig_peer_info,
-                                       
GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
-  }
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "  Creating path...\n");
-  path = path_new (size);
-  own_pos = 0;
-  for (i = 0; i < size; i++)
-  {
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "  ... adding %s\n",
-                GNUNET_i2s (&pi[i]));
-    path->peers[i] = GNUNET_PEER_intern (&pi[i]);
-    if (path->peers[i] == myid)
-      own_pos = i;
-  }
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "  Own position: %u\n", own_pos);
-  if (own_pos == 0 && path->peers[own_pos] != myid)
-  {
-    /* create path: self not found in path through self */
-    GNUNET_break_op (0);
-    path_destroy (path);
-    tunnel_destroy (t);
-    return GNUNET_OK;
-  }
-  path_add_to_peers (path, GNUNET_NO);
-  tunnel_use_path (t, path);
-
-  peer_add_tunnel (dest_peer_info, t);
-
-  if (own_pos == size - 1)
-  {
-    struct MeshClient *c;
-
-    /* Find target client */
-    c = GNUNET_CONTAINER_multihashmap32_get (ports, t->port);
-    if (NULL == c)
-    {
-      /* TODO send reject */
-      return GNUNET_OK;
-    }
-
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "  It's for us!\n");
-    peer_add_path_to_origin (orig_peer_info, path, GNUNET_YES);
-    /* This can be a retransmission due to a lost PATH ACK.
-     * Check if we already have a destination client for the tunnel. */
-    if (t->client != c)
-    {
-      /* Assign local tid */
-      while (NULL != tunnel_get_incoming (next_local_tid))
-        next_local_tid = (next_local_tid + 1) | 
GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
-      t->local_tid_dest = next_local_tid++;
-      next_local_tid = next_local_tid | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
-
-      if (GNUNET_YES == t->reliable)
-      {
-        GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! Reliable\n");
-        t->bck_rel = GNUNET_malloc (sizeof (struct MeshTunnelReliability));
-        t->bck_rel->t = t;
-        t->bck_rel->expected_delay = MESH_RETRANSMIT_TIME;
-      }
-
-      tunnel_add_client (t, c);
-      send_local_tunnel_create (t);
-    }
-    send_path_ack (t);
-     /* Eliminate tunnel when origin dies */
-    tunnel_reset_timeout (t, GNUNET_YES);
-    /* Keep tunnel alive in direction dest->owner*/
-    tunnel_reset_timeout (t, GNUNET_NO);
-  }
-  else
-  {
-    struct MeshPeerPath *path2;
-
-    t->next_hop = path->peers[own_pos + 1];
-    GNUNET_PEER_change_rc(t->next_hop, 1);
-
-    /* It's for somebody else! Retransmit. */
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "  Retransmitting.\n");
-    path2 = path_duplicate (path);
-    peer_add_path (dest_peer_info, path2, GNUNET_NO);
-    peer_add_path_to_origin (orig_peer_info, path, GNUNET_NO);
-    send_path_create (t);
-  }
-  return GNUNET_OK;
-}
-
-
-
-/**
- * Core handler for path ACKs
- *
- * @param cls closure
- * @param message message
- * @param peer peer identity this notification is about
- *
- * @return GNUNET_OK to keep the connection open,
- *         GNUNET_SYSERR to close it (signal serious error)
- */
-static int
-handle_mesh_path_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
-                      const struct GNUNET_MessageHeader *message)
-{
-  struct GNUNET_MESH_PathACK *msg;
-  struct MeshPeer *peer_info;
-  struct MeshPeerPath *p;
-  struct MeshTunnel *t;
-
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received a path ACK msg [%s]\n",
-              GNUNET_i2s (&my_full_id));
-  msg = (struct GNUNET_MESH_PathACK *) message;
-  t = tunnel_get (&msg->oid, ntohl(msg->tid));
-  if (NULL == t)
-  {
-    /* TODO notify that we don't know the tunnel */
-    GNUNET_STATISTICS_update (stats, "# control on unknown tunnel", 1, 
GNUNET_NO);
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "  don't know the tunnel %s [%X]!\n",
-                GNUNET_i2s (&msg->oid), ntohl(msg->tid));
-    return GNUNET_OK;
-  }
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "  on tunnel %s [%X]\n",
-              GNUNET_i2s (&msg->oid), ntohl(msg->tid));
-
-  peer_info = peer_get (&msg->peer_id);
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "  by peer %s\n",
-              GNUNET_i2s (&msg->peer_id));
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "  via peer %s\n",
-              GNUNET_i2s (peer));
-
-  /* Add path to peers? */
-  p = t->path;
-  if (NULL != p)
-  {
-    path_add_to_peers (p, GNUNET_YES);
-  }
-  else
-  {
-    GNUNET_break (0);
-  }
-  tunnel_change_state (t, MESH_TUNNEL_READY);
-  tunnel_reset_timeout (t, GNUNET_NO);
-  t->next_fc.last_ack_recv = (NULL == t->client) ? ntohl (msg->ack) : 0;
-  t->prev_fc.last_ack_sent = ntohl (msg->ack);
-
-  /* Message for us? */
-  if (0 == memcmp (&msg->oid, &my_full_id, sizeof (struct 
GNUNET_PeerIdentity)))
-  {
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "  It's for us!\n");
-    if (NULL == t->owner)
-    {
-      GNUNET_break_op (0);
-      return GNUNET_OK;
-    }
-    if (NULL != peer_info->dhtget)
-    {
-      GNUNET_DHT_get_stop (peer_info->dhtget);
-      peer_info->dhtget = NULL;
-    }
-    tunnel_send_ack (t, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK, GNUNET_YES);
-    tunnel_send_ack (t, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK, GNUNET_NO);
-    return GNUNET_OK;
-  }
-
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-              "  not for us, retransmitting...\n");
-  send_prebuilt_message (message, t->prev_hop, t);
-  return GNUNET_OK;
-}
-
-
-/**
- * Core handler for notifications of broken paths
- *
- * @param cls closure
- * @param message message
- * @param peer peer identity this notification is about
- *
- * @return GNUNET_OK to keep the connection open,
- *         GNUNET_SYSERR to close it (signal serious error)
- */
-static int
-handle_mesh_path_broken (void *cls, const struct GNUNET_PeerIdentity *peer,
-                         const struct GNUNET_MessageHeader *message)
-{
-  struct GNUNET_MESH_PathBroken *msg;
-  struct MeshTunnel *t;
-
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-              "Received a PATH BROKEN msg from %s\n", GNUNET_i2s (peer));
-  msg = (struct GNUNET_MESH_PathBroken *) message;
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "  regarding %s\n",
-              GNUNET_i2s (&msg->peer1));
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "  regarding %s\n",
-              GNUNET_i2s (&msg->peer2));
-  t = tunnel_get (&msg->oid, ntohl (msg->tid));
-  if (NULL == t)
-  {
-    GNUNET_break_op (0);
-    return GNUNET_OK;
-  }
-  tunnel_notify_connection_broken (t, GNUNET_PEER_search (&msg->peer1),
-                                   GNUNET_PEER_search (&msg->peer2));
-  return GNUNET_OK;
-
-}
-
-
-/**
- * Core handler for tunnel destruction
- *
- * @param cls closure
- * @param message message
- * @param peer peer identity this notification is about
- *
- * @return GNUNET_OK to keep the connection open,
- *         GNUNET_SYSERR to close it (signal serious error)
- */
-static int
-handle_mesh_tunnel_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
-                            const struct GNUNET_MessageHeader *message)
-{
-  struct GNUNET_MESH_TunnelDestroy *msg;
-  struct MeshTunnel *t;
-
-  msg = (struct GNUNET_MESH_TunnelDestroy *) message;
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-              "Got a TUNNEL DESTROY packet from %s\n",
-              GNUNET_i2s (peer));
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-              "  for tunnel %s [%u]\n",
-              GNUNET_i2s (&msg->oid), ntohl (msg->tid));
-  t = tunnel_get (&msg->oid, ntohl (msg->tid));
-  if (NULL == t)
-  {
-    /* Probably already got the message from another path,
-     * destroyed the tunnel and retransmitted to children.
-     * Safe to ignore.
-     */
-    GNUNET_STATISTICS_update (stats, "# control on unknown tunnel",
-                              1, GNUNET_NO);
-    return GNUNET_OK;
-  }
-  if (t->local_tid_dest >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
-  {
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "INCOMING TUNNEL %X %X\n",
-                t->local_tid, t->local_tid_dest);
-  }
-  if (GNUNET_PEER_search (peer) == t->prev_hop)
-  {
-    // TODO check owner's signature
-    // TODO add owner's signatue to tunnel for retransmission
-    peer_cancel_queues (t->prev_hop, t);
-    GNUNET_PEER_change_rc (t->prev_hop, -1);
-    t->prev_hop = 0;
-  }
-  else if (GNUNET_PEER_search (peer) == t->next_hop)
-  {
-    // TODO check dest's signature
-    // TODO add dest's signatue to tunnel for retransmission
-    peer_cancel_queues (t->next_hop, t);
-    GNUNET_PEER_change_rc (t->next_hop, -1);
-    t->next_hop = 0;
-  }
-  else
-  {
-    GNUNET_break_op (0);
-    // TODO check both owner AND destination's signature to see which matches
-    // TODO restransmit in appropriate direction
-    return GNUNET_OK;
-  }
-  tunnel_destroy_empty (t);
-
-  // TODO: add timeout to destroy the tunnel anyway
-  return GNUNET_OK;
-}
-
-
-/**
- * Generic handler for mesh network payload traffic.
- *
- * @param peer Peer identity this notification is about.
- * @param message Data message.
- * @param fwd Is this FWD traffic? GNUNET_YES : GNUNET_NO;
- *
- * @return GNUNET_OK to keep the connection open,
- *         GNUNET_SYSERR to close it (signal serious error)
- */
-static int
-handle_mesh_data (const struct GNUNET_PeerIdentity *peer,
-                  const struct GNUNET_MessageHeader *message,
-                  int fwd)
-{
-  struct GNUNET_MESH_Data *msg;
-  struct MeshFlowControl *fc;
-  struct MeshTunnelReliability *rel;
-  struct MeshTunnel *t;
-  struct MeshClient *c;
-  GNUNET_PEER_Id hop;
-  uint32_t pid;
-  uint32_t ttl;
-  uint16_t type;
-  size_t size;
-
-  /* Check size */
-  size = ntohs (message->size);
-  if (size <
-      sizeof (struct GNUNET_MESH_Data) +
-      sizeof (struct GNUNET_MessageHeader))
-  {
-    GNUNET_break (0);
-    return GNUNET_OK;
-  }
-  type =ntohs (message->type);
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a %s message from %s\n",
-              GNUNET_MESH_DEBUG_M2S (type), GNUNET_i2s (peer));
-  msg = (struct GNUNET_MESH_Data *) message;
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " payload of type %s\n",
-              GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
-  /* Check tunnel */
-  t = tunnel_get (&msg->oid, ntohl (msg->tid));
-  if (NULL == t)
-  {
-    /* TODO notify back: we don't know this tunnel */
-    GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "WARNING tunnel unknown\n");
-    return GNUNET_OK;
-  }
-
-  /*  Initialize FWD/BCK data */
-  pid = ntohl (msg->pid);
-  fc =  fwd ? &t->prev_fc : &t->next_fc;
-  c =   fwd ? t->client   : t->owner;
-  rel = fwd ? t->bck_rel  : t->fwd_rel;
-  hop = fwd ? t->next_hop : t->prev_hop;
-  if (GMC_is_pid_bigger (pid, fc->last_ack_sent))
-  {
-    GNUNET_STATISTICS_update (stats, "# unsolicited data", 1, GNUNET_NO);
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                "WARNING Received PID %u, (prev %u), ACK %u\n",
-                pid, fc->last_pid_recv, fc->last_ack_sent);
-    return GNUNET_OK;
-  }
-  if (NULL != c)
-    tunnel_change_state (t, MESH_TUNNEL_READY);
-  tunnel_reset_timeout (t, fwd);
-  if (NULL != c)
-  {
-    /* TODO signature verification */
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "  it's for us! sending to client\n");
-    GNUNET_STATISTICS_update (stats, "# data received", 1, GNUNET_NO);
-    if (GMC_is_pid_bigger (pid, fc->last_pid_recv))
-    {
-      uint32_t mid;
-
-      mid = ntohl (msg->mid);
-      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                  " pid %u (mid %u) not seen yet\n", pid, mid);
-      fc->last_pid_recv = pid;
-
-      if (GNUNET_NO == t->reliable ||
-          ( !GMC_is_pid_bigger (rel->mid_recv, mid) &&
-            GMC_is_pid_bigger (rel->mid_recv + 64, mid) ) )
-      {
-        GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                    "!!! RECV %u\n", ntohl (msg->mid));
-        if (GNUNET_YES == t->reliable)
-        {
-          /* Is this the exact next expected messasge? */
-          if (mid == rel->mid_recv)
-          {
-            GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "as expected\n");
-            rel->mid_recv++;
-            tunnel_send_client_data (t, msg, fwd);
-            tunnel_send_client_buffered_data (t, c, rel);
-          }
-          else
-          {
-            GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "save for later\n");
-            tunnel_add_buffered_data (t, msg, rel);
-          }
-        }
-        else /* Tunnel unreliable, send to clients directly */
-        {
-          tunnel_send_client_data (t, msg, fwd);
-        }
-      }
-      else
-      {
-        GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                    " MID %u not expected (%u - %u), dropping!\n",
-                    ntohl (msg->mid), rel->mid_recv, rel->mid_recv + 64);
-      }
-    }
-    else
-    {
-//       GNUNET_STATISTICS_update (stats, "# duplicate PID", 1, GNUNET_NO);
-      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                  " Pid %u not expected (%u+), dropping!\n",
-                  pid, fc->last_pid_recv + 1);
-    }
-    tunnel_send_ack (t, type, fwd);
-    return GNUNET_OK;
-  }
-  fc->last_pid_recv = pid;
-  if (0 == hop)
-  {
-    GNUNET_STATISTICS_update (stats, "# data on dying tunnel", 1, GNUNET_NO);
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "data on dying tunnel %s[%X]\n",
-                GNUNET_PEER_resolve2 (t->id.oid), ntohl (msg->tid));
-    return GNUNET_OK; /* Next hop has destoyed the tunnel, drop */
-  }
-  ttl = ntohl (msg->ttl);
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "   ttl: %u\n", ttl);
-  if (ttl == 0)
-  {
-    GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
-    GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
-    tunnel_send_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK, fwd);
-    return GNUNET_OK;
-  }
-
-  if (myid != hop)
-  {
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "  not for us, retransmitting...\n");
-    send_prebuilt_message (message, hop, t);
-    GNUNET_STATISTICS_update (stats, "# unicast forwarded", 1, GNUNET_NO);
-  }
-  return GNUNET_OK;
-}
-
-
-/**
- * Core handler for mesh network traffic going from the origin to a peer
- *
- * @param cls Closure (unused).
- * @param message Message received.
- * @param peer Peer who sent the message.
- *
- * @return GNUNET_OK to keep the connection open,
- *         GNUNET_SYSERR to close it (signal serious error)
- */
-static int
-handle_mesh_unicast (void *cls, const struct GNUNET_PeerIdentity *peer,
-                     const struct GNUNET_MessageHeader *message)
-{
-  return handle_mesh_data (peer, message, GNUNET_YES);
-}
-
-/**
- * Core handler for mesh network traffic towards the owner of a tunnel.
- *
- * @param cls Closure (unused).
- * @param message Message received.
- * @param peer Peer who sent the message.
- *
- * @return GNUNET_OK to keep the connection open,
- *         GNUNET_SYSERR to close it (signal serious error)
- */
-static int
-handle_mesh_to_orig (void *cls, const struct GNUNET_PeerIdentity *peer,
-                     const struct GNUNET_MessageHeader *message)
-{
-  return handle_mesh_data (peer, message, GNUNET_NO);
-}
-
-
-/**
- * Core handler for mesh network traffic end-to-end ACKs.
- *
- * @param cls Closure.
- * @param message Message.
- * @param peer Peer identity this notification is about.
- *
- * @return GNUNET_OK to keep the connection open,
- *         GNUNET_SYSERR to close it (signal serious error)
- */
-static int
-handle_mesh_data_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
-                      const struct GNUNET_MessageHeader *message)
-{
-  struct GNUNET_MESH_DataACK *msg;
-  struct MeshTunnelReliability *rel;
-  struct MeshReliableMessage *copy;
-  struct MeshReliableMessage *next;
-  struct MeshTunnel *t;
-  GNUNET_PEER_Id id;
-  uint32_t ack;
-  uint16_t type;
-  int work;
-
-  type = ntohs (message->type);
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a %s message from %s!\n",
-              GNUNET_MESH_DEBUG_M2S (type), GNUNET_i2s (peer));
-  msg = (struct GNUNET_MESH_DataACK *) message;
-
-  t = tunnel_get (&msg->oid, ntohl (msg->tid));
-  if (NULL == t)
-  {
-    /* TODO notify that we dont know this tunnel (whom)? */
-    GNUNET_STATISTICS_update (stats, "# ack on unknown tunnel", 1, GNUNET_NO);
-    return GNUNET_OK;
-  }
-  ack = ntohl (msg->mid);
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "  ACK %u\n", ack);
-
-  /* Is this a forward or backward ACK? */
-  id = GNUNET_PEER_search (peer);
-  if (t->next_hop == id && GNUNET_MESSAGE_TYPE_MESH_UNICAST_ACK == type)
-  {
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "  FWD ACK\n");
-    if (NULL == t->owner)
-    {
-      send_prebuilt_message (message, t->prev_hop, t);
-      return GNUNET_OK;
-    }
-    rel = t->fwd_rel;
-  }
-  else if (t->prev_hop == id && GNUNET_MESSAGE_TYPE_MESH_TO_ORIG_ACK == type)
-  {
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "  BCK ACK\n");
-    if (NULL == t->client)
-    {
-      send_prebuilt_message (message, t->next_hop, t);
-      return GNUNET_OK;
-    }
-    rel = t->bck_rel;
-  }
-  else
-  {
-    GNUNET_break_op (0);
-    return GNUNET_OK;
-  }
-
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! ACK %u\n", ack);
-  for (work = GNUNET_NO, copy = rel->head_sent; copy != NULL; copy = next)
-  {
-   if (GMC_is_pid_bigger (copy->mid, ack))
-    {
-      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!!  head %u, out!\n", copy->mid);
-      tunnel_free_sent_reliable (t, msg, rel);
-      break;
-    }
-    work = GNUNET_YES;
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!!  id %u\n", copy->mid);
-    next = copy->next;
-    tunnel_free_reliable_message (copy);
-  }
-  /* Once buffers have been free'd, send ACK */
-  tunnel_send_ack (t, type, GNUNET_MESSAGE_TYPE_MESH_UNICAST_ACK == type);
-
-  /* If some message was free'd, update the retransmission delay*/
-  if (GNUNET_YES == work)
-  {
-    if (GNUNET_SCHEDULER_NO_TASK != rel->retry_task)
-    {
-      GNUNET_SCHEDULER_cancel (rel->retry_task);
-      if (NULL == rel->head_sent)
-      {
-        rel->retry_task = GNUNET_SCHEDULER_NO_TASK;
-      }
-      else
-      {
-        struct GNUNET_TIME_Absolute new_target;
-        struct GNUNET_TIME_Relative delay;
-
-        delay = GNUNET_TIME_relative_multiply (rel->retry_timer,
-                                               MESH_RETRANSMIT_MARGIN);
-        new_target = GNUNET_TIME_absolute_add (rel->head_sent->timestamp,
-                                               delay);
-        delay = GNUNET_TIME_absolute_get_remaining (new_target);
-        rel->retry_task =
-            GNUNET_SCHEDULER_add_delayed (delay,
-                                          &tunnel_retransmit_message,
-                                          rel);
-      }
-    }
-    else
-      GNUNET_break (0);
-  }
-  return GNUNET_OK;
-}
-
-/**
- * Core handler for mesh network traffic point-to-point acks.
- *
- * @param cls closure
- * @param message message
- * @param peer peer identity this notification is about
- *
- * @return GNUNET_OK to keep the connection open,
- *         GNUNET_SYSERR to close it (signal serious error)
- */
-static int
-handle_mesh_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
-                 const struct GNUNET_MessageHeader *message)
-{
-  struct GNUNET_MESH_ACK *msg;
-  struct MeshTunnel *t;
-  struct MeshFlowControl *fc;
-  GNUNET_PEER_Id id;
-  uint32_t ack;
-
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got an ACK packet from %s!\n",
-              GNUNET_i2s (peer));
-  msg = (struct GNUNET_MESH_ACK *) message;
-
-  t = tunnel_get (&msg->oid, ntohl (msg->tid));
-
-  if (NULL == t)
-  {
-    /* TODO notify that we dont know this tunnel (whom)? */
-    GNUNET_STATISTICS_update (stats, "# ack on unknown tunnel", 1, GNUNET_NO);
-    return GNUNET_OK;
-  }
-  ack = ntohl (msg->pid);
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "  ACK %u\n", ack);
-
-  /* Is this a forward or backward ACK? */
-  id = GNUNET_PEER_search (peer);
-  if (t->next_hop == id)
-  {
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "  FWD ACK\n");
-    fc = &t->next_fc;
-  }
-  else if (t->prev_hop == id)
-  {
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "  BCK ACK\n");
-    fc = &t->prev_fc;
-  }
-  else
-  {
-    GNUNET_break_op (0);
-    return GNUNET_OK;
-  }
-
-  if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task &&
-      GMC_is_pid_bigger (ack, fc->last_ack_recv))
-  {
-    GNUNET_SCHEDULER_cancel (fc->poll_task);
-    fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
-    fc->poll_time = GNUNET_TIME_UNIT_SECONDS;
-  }
-
-  fc->last_ack_recv = ack;
-  peer_unlock_queue (id);
-  tunnel_change_state (t, MESH_TUNNEL_READY);
-
-  tunnel_send_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK, t->next_hop == id);
-
-  return GNUNET_OK;
-}
-
-
-/**
- * Core handler for mesh network traffic point-to-point ack polls.
- *
- * @param cls closure
- * @param message message
- * @param peer peer identity this notification is about
- *
- * @return GNUNET_OK to keep the connection open,
- *         GNUNET_SYSERR to close it (signal serious error)
- */
-static int
-handle_mesh_poll (void *cls, const struct GNUNET_PeerIdentity *peer,
-                  const struct GNUNET_MessageHeader *message)
-{
-  struct GNUNET_MESH_Poll *msg;
-  struct MeshTunnel *t;
-  struct MeshFlowControl *fc;
-  GNUNET_PEER_Id id;
-  uint32_t pid;
-  uint32_t old;
-
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a POLL packet from %s!\n",
-              GNUNET_i2s (peer));
-
-  msg = (struct GNUNET_MESH_Poll *) message;
-
-  t = tunnel_get (&msg->oid, ntohl (msg->tid));
-
-  if (NULL == t)
-  {
-    /* TODO notify that we dont know this tunnel (whom)? */
-    GNUNET_STATISTICS_update (stats, "# poll on unknown tunnel", 1, GNUNET_NO);
-    GNUNET_break_op (0);
-    return GNUNET_OK;
-  }
-
-  /* Is this a forward or backward ACK? */
-  id = GNUNET_PEER_search (peer);
-  pid = ntohl (msg->pid);
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "  PID %u\n", pid);
-  if (t->next_hop == id)
-  {
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "  from FWD\n");
-    fc = &t->next_fc;
-    old = fc->last_pid_recv;
-  }
-  else if (t->prev_hop == id)
-  {
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "  from BCK\n");
-    fc = &t->prev_fc;
-    old = fc->last_pid_recv;
-  }
-  else
-  {
-    GNUNET_break (0);
-    return GNUNET_OK;
-  }
-
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "  was %u\n", fc->last_pid_recv);
-  fc->last_pid_recv = pid;
-  tunnel_send_ack (t, GNUNET_MESSAGE_TYPE_MESH_POLL, t->prev_hop == id);
-
-  if (GNUNET_YES == t->reliable)
-    fc->last_pid_recv = old;
-
-  return GNUNET_OK;
-}
-
-
-/**
- * Core handler for mesh keepalives.
- *
- * @param cls closure
- * @param message message
- * @param peer peer identity this notification is about
- * @return GNUNET_OK to keep the connection open,
- *         GNUNET_SYSERR to close it (signal serious error)
- *
- * TODO: Check who we got this from, to validate route.
- */
-static int
-handle_mesh_keepalive (void *cls, const struct GNUNET_PeerIdentity *peer,
-                       const struct GNUNET_MessageHeader *message)
-{
-  struct GNUNET_MESH_TunnelKeepAlive *msg;
-  struct MeshTunnel *t;
-  struct MeshClient *c;
-  GNUNET_PEER_Id hop;
-  int fwd;
-
-  msg = (struct GNUNET_MESH_TunnelKeepAlive *) message;
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a keepalive packet from %s\n",
-              GNUNET_i2s (peer));
-
-  t = tunnel_get (&msg->oid, ntohl (msg->tid));
-  if (NULL == t)
-  {
-    /* TODO notify that we dont know that tunnel */
-    GNUNET_STATISTICS_update (stats, "# keepalive on unknown tunnel", 1,
-                              GNUNET_NO);
-    return GNUNET_OK;
-  }
-
-  fwd = GNUNET_MESSAGE_TYPE_MESH_FWD_KEEPALIVE == ntohs (message->type) ?
-        GNUNET_YES : GNUNET_NO;
-  c   = fwd ? t->client   : t->owner;
-  hop = fwd ? t->next_hop : t->prev_hop;
-
-  if (NULL != c)
-    tunnel_change_state (t, MESH_TUNNEL_READY);
-  tunnel_reset_timeout (t, fwd);
-  if (NULL != c || 0 == hop || myid == hop)
-    return GNUNET_OK;
-
-  GNUNET_STATISTICS_update (stats, "# keepalives forwarded", 1, GNUNET_NO);
-  send_prebuilt_message (message, hop, t);
-  return GNUNET_OK;
-  }
-
-
-
-/**
- * Functions to handle messages from core
- */
-static struct GNUNET_CORE_MessageHandler core_handlers[] = {
-  {&handle_mesh_path_create, GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE, 0},
-  {&handle_mesh_path_broken, GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN,
-   sizeof (struct GNUNET_MESH_PathBroken)},
-  {&handle_mesh_tunnel_destroy, GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY,
-   sizeof (struct GNUNET_MESH_TunnelDestroy)},
-  {&handle_mesh_unicast, GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
-  {&handle_mesh_to_orig, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
-  {&handle_mesh_data_ack, GNUNET_MESSAGE_TYPE_MESH_UNICAST_ACK,
-    sizeof (struct GNUNET_MESH_DataACK)},
-  {&handle_mesh_data_ack, GNUNET_MESSAGE_TYPE_MESH_TO_ORIG_ACK,
-    sizeof (struct GNUNET_MESH_DataACK)},
-  {&handle_mesh_keepalive, GNUNET_MESSAGE_TYPE_MESH_FWD_KEEPALIVE,
-    sizeof (struct GNUNET_MESH_TunnelKeepAlive)},
-  {&handle_mesh_keepalive, GNUNET_MESSAGE_TYPE_MESH_BCK_KEEPALIVE,
-    sizeof (struct GNUNET_MESH_TunnelKeepAlive)},
-  {&handle_mesh_ack, GNUNET_MESSAGE_TYPE_MESH_ACK,
-    sizeof (struct GNUNET_MESH_ACK)},
-  {&handle_mesh_poll, GNUNET_MESSAGE_TYPE_MESH_POLL,
-    sizeof (struct GNUNET_MESH_Poll)},
-  {&handle_mesh_path_ack, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
-   sizeof (struct GNUNET_MESH_PathACK)},
-  {NULL, 0, 0}
-};
-
-
-/**
- * Function to process paths received for a new peer addition. The recorded
- * paths form the initial tunnel, which can be optimized later.
- * Called on each result obtained for the DHT search.
- *
- * @param cls closure
- * @param exp when will this value expire
- * @param key key of the result
- * @param get_path path of the get request
- * @param get_path_length lenght of get_path
- * @param put_path path of the put request
- * @param put_path_length length of the put_path
- * @param type type of the result
- * @param size number of bytes in data
- * @param data pointer to the result data
- */
-static void
-dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
-                    const struct GNUNET_HashCode * key,
-                    const struct GNUNET_PeerIdentity *get_path,
-                    unsigned int get_path_length,
-                    const struct GNUNET_PeerIdentity *put_path,
-                    unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
-                    size_t size, const void *data)
-{
-  struct MeshPeer *peer = cls;
-  struct MeshPeerPath *p;
-  struct GNUNET_PeerIdentity pi;
-  int i;
-
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got results from DHT!\n");
-  GNUNET_PEER_resolve (peer->id, &pi);
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "  for %s\n", GNUNET_i2s (&pi));
-
-  p = path_build_from_dht (get_path, get_path_length,
-                           put_path, put_path_length);
-  path_add_to_peers (p, GNUNET_NO);
-  path_destroy (p);
-  for (i = 0; i < peer->ntunnels; i++)
-  {
-    struct GNUNET_PeerIdentity id;
-
-    GNUNET_PEER_resolve (peer->tunnels[i]->id.oid, &id);
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... tunnel %s:%X (%X / %X)\n",
-                GNUNET_i2s (&id), peer->tunnels[i]->id.tid,
-                peer->tunnels[i]->local_tid,
-                peer->tunnels[i]->local_tid_dest);
-    if (peer->tunnels[i]->state == MESH_TUNNEL_SEARCHING)
-    {
-      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... connect!\n");
-      peer_connect (peer, peer->tunnels[i]);
-    }
-  }
-
-  return;
-}
-
-
-/******************************************************************************/
-/*********************       MESH LOCAL HANDLES      
**************************/
-/******************************************************************************/
-
-
-/**
- * Handler for client connection.
- *
- * @param cls Closure (unused).
- * @param client Client handler.
- */
-static void
-handle_local_client_connect (void *cls, struct GNUNET_SERVER_Client *client)
-{
-  struct MeshClient *c;
-
-  if (NULL == client)
-    return;
-  c = GNUNET_malloc (sizeof (struct MeshClient));
-  c->handle = client;
-  GNUNET_SERVER_client_keep (client);
-  GNUNET_SERVER_client_set_user_context (client, c);
-  GNUNET_CONTAINER_DLL_insert (clients_head, clients_tail, c);
-}
-
-
-/**
- * Handler for client disconnection
- *
- * @param cls closure
- * @param client identification of the client; NULL
- *        for the last call when the server is destroyed
- */
-static void
-handle_local_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
-{
-  struct MeshClient *c;
-
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client disconnected: %p\n", client);
-  if (client == NULL)
-  {
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "   (SERVER DOWN)\n");
-    return;
-  }
-
-  c = client_get (client);
-  if (NULL != c)
-  {
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "matching client found (%u, %p)\n",
-                c->id, c);
-    GNUNET_SERVER_client_drop (c->handle);
-    c->shutting_down = GNUNET_YES;
-    if (NULL != c->own_tunnels)
-    {
-      GNUNET_CONTAINER_multihashmap32_iterate (c->own_tunnels,
-                                               &tunnel_destroy_iterator, c);
-      GNUNET_CONTAINER_multihashmap32_destroy (c->own_tunnels);
-    }
-
-    if (NULL != c->incoming_tunnels)
-    {
-      GNUNET_CONTAINER_multihashmap32_iterate (c->incoming_tunnels,
-                                               &tunnel_destroy_iterator, c);
-      GNUNET_CONTAINER_multihashmap32_destroy (c->incoming_tunnels);
-    }
-
-    if (NULL != c->ports)
-    {
-      GNUNET_CONTAINER_multihashmap32_iterate (c->ports,
-                                               &client_release_ports, c);
-      GNUNET_CONTAINER_multihashmap32_destroy (c->ports);
-    }
-    GNUNET_CONTAINER_DLL_remove (clients_head, clients_tail, c);
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "  client free (%p)\n", c);
-    GNUNET_free (c);
-    GNUNET_STATISTICS_update (stats, "# clients", -1, GNUNET_NO);
-  }
-  else
-  {
-    GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " context NULL!\n");
-  }
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "done!\n");
-  return;
-}
-
-
-/**
- * Handler for new clients
- *
- * @param cls closure
- * @param client identification of the client
- * @param message the actual message, which includes messages the client wants
- */
-static void
-handle_local_new_client (void *cls, struct GNUNET_SERVER_Client *client,
-                         const struct GNUNET_MessageHeader *message)
-{
-  struct GNUNET_MESH_ClientConnect *cc_msg;
-  struct MeshClient *c;
-  unsigned int size;
-  uint32_t *p;
-  unsigned int i;
-
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client connected %p\n", client);
-
-  /* Check data sanity */
-  size = ntohs (message->size) - sizeof (struct GNUNET_MESH_ClientConnect);
-  cc_msg = (struct GNUNET_MESH_ClientConnect *) message;
-  if (0 != (size % sizeof (uint32_t)))
-  {
-    GNUNET_break (0);
-    GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
-    return;
-  }
-  size /= sizeof (uint32_t);
-
-  /* Initialize new client structure */
-  c = GNUNET_SERVER_client_get_user_context (client, struct MeshClient);
-  c->id = next_client_id++; /* overflow not important: just for debug */
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "  client id %u\n", c->id);
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "  client has %u ports\n", size);
-  if (size > 0)
-  {
-    uint32_t u32;
-
-    p = (uint32_t *) &cc_msg[1];
-    c->ports = GNUNET_CONTAINER_multihashmap32_create (size);
-    for (i = 0; i < size; i++)
-    {
-      u32 = ntohl (p[i]);
-      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "    port: %u\n", u32);
-
-      /* store in client's hashmap */
-      GNUNET_CONTAINER_multihashmap32_put (c->ports, u32, c,
-                                           
GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
-      /* store in global hashmap */
-      /* FIXME only allow one client to have the port open,
-       *       have a backup hashmap with waiting clients */
-      GNUNET_CONTAINER_multihashmap32_put (ports, u32, c,
-                                           
GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
-    }
-  }
-
-  c->own_tunnels = GNUNET_CONTAINER_multihashmap32_create (32);
-  c->incoming_tunnels = GNUNET_CONTAINER_multihashmap32_create (32);
-  GNUNET_SERVER_notification_context_add (nc, client);
-  GNUNET_STATISTICS_update (stats, "# clients", 1, GNUNET_NO);
-
-  GNUNET_SERVER_receive_done (client, GNUNET_OK);
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client processed\n");
-}
-
-
-/**
- * Handler for requests of new tunnels
- *
- * @param cls Closure.
- * @param client Identification of the client.
- * @param message The actual message.
- */
-static void
-handle_local_tunnel_create (void *cls, struct GNUNET_SERVER_Client *client,
-                            const struct GNUNET_MessageHeader *message)
-{
-  struct GNUNET_MESH_TunnelMessage *t_msg;
-  struct MeshPeer *peer_info;
-  struct MeshTunnel *t;
-  struct MeshClient *c;
-  MESH_TunnelNumber tid;
-
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new tunnel requested\n");
-
-  /* Sanity check for client registration */
-  if (NULL == (c = client_get (client)))
-  {
-    GNUNET_break (0);
-    GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
-    return;
-  }
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "  by client %u\n", c->id);
-
-  /* Message size sanity check */
-  if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
-  {
-    GNUNET_break (0);
-    GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
-    return;
-  }
-
-  t_msg = (struct GNUNET_MESH_TunnelMessage *) message;
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "  towards %s:%u\n",
-              GNUNET_i2s (&t_msg->peer), ntohl (t_msg->port));
-  tid = ntohl (t_msg->tunnel_id);
-  /* Sanity check for duplicate tunnel IDs */
-  if (NULL != tunnel_get_by_local_id (c, tid))
-  {
-    GNUNET_break (0);
-    GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
-    return;
-  }
-
-  /* Create tunnel */
-  while (NULL != tunnel_get_by_pi (myid, next_tid))
-    next_tid = (next_tid + 1) & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
-  t = tunnel_new (myid, next_tid, c, tid);
-  next_tid = (next_tid + 1) & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
-  if (NULL == t)
-  {
-    GNUNET_break (0);
-    GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
-    return;
-  }
-  t->port = ntohl (t_msg->port);
-  tunnel_set_options (t, ntohl (t_msg->opt));
-  if (GNUNET_YES == t->reliable)
-  {
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! Reliable\n");
-    t->fwd_rel = GNUNET_malloc (sizeof (struct MeshTunnelReliability));
-    t->fwd_rel->t = t;
-    t->fwd_rel->expected_delay = MESH_RETRANSMIT_TIME;
-  }
-
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATED TUNNEL %s[%x]:%u (%x)\n",
-              GNUNET_i2s (&my_full_id), t->id.tid, t->port, t->local_tid);
-
-  peer_info = peer_get (&t_msg->peer);
-  peer_add_tunnel (peer_info, t);
-  peer_connect (peer_info, t);
-  tunnel_reset_timeout (t, GNUNET_YES);
-  GNUNET_SERVER_receive_done (client, GNUNET_OK);
-  return;
-}
-
-
-/**
- * Handler for requests of deleting tunnels
- *
- * @param cls closure
- * @param client identification of the client
- * @param message the actual message
- */
-static void
-handle_local_tunnel_destroy (void *cls, struct GNUNET_SERVER_Client *client,
-                             const struct GNUNET_MessageHeader *message)
-{
-  struct GNUNET_MESH_TunnelMessage *tunnel_msg;
-  struct MeshClient *c;
-  struct MeshTunnel *t;
-  MESH_TunnelNumber tid;
-
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-              "Got a DESTROY TUNNEL from client!\n");
-
-  /* Sanity check for client registration */
-  if (NULL == (c = client_get (client)))
-  {
-    GNUNET_break (0);
-    GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
-    return;
-  }
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "  by client %u\n", c->id);
-
-  /* Message sanity check */
-  if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
-  {
-    GNUNET_break (0);
-    GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
-    return;
-  }
-
-  tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
-
-  /* Retrieve tunnel */
-  tid = ntohl (tunnel_msg->tunnel_id);
-  t = tunnel_get_by_local_id (c, tid);
-  if (NULL == t)
-  {
-    GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "  tunnel %X not found\n", tid);
-    GNUNET_break (0);
-    GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
-    return;
-  }
-
-  /* Cleanup after the tunnel */
-  client_delete_tunnel (c, t);
-  if (c == t->client && GNUNET_MESH_LOCAL_TUNNEL_ID_SERV <= tid)
-  {
-    t->client = NULL;
-  }
-  else if (c == t->owner && GNUNET_MESH_LOCAL_TUNNEL_ID_SERV > tid)
-  {
-    peer_remove_tunnel (peer_get_short (t->dest), t);
-    t->owner = NULL;
-  }
-  else
-  {
-    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
-                "  tunnel %X client %p (%p, %p)\n",
-                tid, c, t->owner, t->client);
-    GNUNET_break (0);
-  }
-
-  /* The tunnel will be destroyed when the last message is transmitted. */
-  tunnel_destroy_empty (t);
-
-  GNUNET_SERVER_receive_done (client, GNUNET_OK);
-  return;
-}
-
-
-/**
- * Handler for client traffic
- *
- * @param cls closure
- * @param client identification of the client
- * @param message the actual message
- */
-static void
-handle_local_data (void *cls, struct GNUNET_SERVER_Client *client,
-                   const struct GNUNET_MessageHeader *message)
-{
-  struct GNUNET_MESH_LocalData *data_msg;
-  struct MeshClient *c;
-  struct MeshTunnel *t;
-  struct MeshFlowControl *fc;
-  MESH_TunnelNumber tid;
-  size_t size;
-
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-              "Got data from a client!\n");
-
-  /* Sanity check for client registration */
-  if (NULL == (c = client_get (client)))
-  {
-    GNUNET_break (0);
-    GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
-    return;
-  }
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "  by client %u\n", c->id);
-
-  data_msg = (struct GNUNET_MESH_LocalData *) message;
-
-  /* Sanity check for message size */
-  size = ntohs (message->size) - sizeof (struct GNUNET_MESH_LocalData);
-  if (size < sizeof (struct GNUNET_MessageHeader))
-  {
-    GNUNET_break (0);
-    GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
-    return;
-  }
-
-  /* Tunnel exists? */
-  tid = ntohl (data_msg->tid);
-  t = tunnel_get_by_local_id (c, tid);
-  if (NULL == t)
-  {
-    GNUNET_break (0);
-    GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
-    return;
-  }
-
-  /* Is the client in the tunnel? */
-  if ( !( (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV &&
-           t->owner &&
-           t->owner->handle == client)
-         ||
-          (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV &&
-           t->client &&
-           t->client->handle == client) ) )
-  {
-    GNUNET_break (0);
-    GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
-    return;
-  }
-
-  /* Ok, everything is correct, send the message
-   * (pretend we got it from a mesh peer)
-   */
-  {
-    struct GNUNET_MESH_Data *payload;
-    char cbuf[sizeof(struct GNUNET_MESH_Data) + size];
-
-    fc = tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV ? &t->prev_fc : &t->next_fc;
-    if (GNUNET_YES == t->reliable)
-    {
-      struct MeshTunnelReliability *rel;
-      struct MeshReliableMessage *copy;
-
-      rel = (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV) ? t->fwd_rel : t->bck_rel;
-      copy = GNUNET_malloc (sizeof (struct MeshReliableMessage)
-                            + sizeof(struct GNUNET_MESH_Data)
-                            + size);
-      copy->mid = rel->mid_sent++;
-      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! DATA %u\n", copy->mid);
-      copy->timestamp = GNUNET_TIME_absolute_get ();
-      copy->rel = rel;
-      rel->n_sent++;
-      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " n_sent %u\n", rel->n_sent);
-      GNUNET_CONTAINER_DLL_insert_tail (rel->head_sent, rel->tail_sent, copy);
-      if (GNUNET_SCHEDULER_NO_TASK == rel->retry_task)
-      {
-        rel->retry_timer =
-            GNUNET_TIME_relative_multiply (rel->expected_delay,
-                                           MESH_RETRANSMIT_MARGIN);
-        rel->retry_task =
-            GNUNET_SCHEDULER_add_delayed (rel->retry_timer,
-                                          &tunnel_retransmit_message,
-                                          rel);
-      }
-      payload = (struct GNUNET_MESH_Data *) &copy[1];
-      payload->mid = htonl (copy->mid);
-    }
-    else
-    {
-      payload = (struct GNUNET_MESH_Data *) cbuf;
-      payload->mid = htonl (fc->last_pid_recv + 1);
-    }
-    memcpy (&payload[1], &data_msg[1], size);
-    payload->header.size = htons (sizeof (struct GNUNET_MESH_Data) + size);
-    payload->header.type = htons (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV ?
-                                  GNUNET_MESSAGE_TYPE_MESH_UNICAST :
-                                  GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
-    GNUNET_PEER_resolve(t->id.oid, &payload->oid);;
-    payload->tid = htonl (t->id.tid);
-    payload->ttl = htonl (default_ttl);
-    payload->pid = htonl (fc->last_pid_recv + 1);
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                "  calling generic handler...\n");
-    if (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
-      handle_mesh_unicast (NULL, &my_full_id, &payload->header);
-    else
-      handle_mesh_to_orig (NULL, &my_full_id, &payload->header);
-  }
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "receive done OK\n");
-  GNUNET_SERVER_receive_done (client, GNUNET_OK);
-
-  return;
-}
-
-
-/**
- * Handler for client's ACKs for payload traffic.
- *
- * @param cls Closure (unused).
- * @param client Identification of the client.
- * @param message The actual message.
- */
-static void
-handle_local_ack (void *cls, struct GNUNET_SERVER_Client *client,
-                  const struct GNUNET_MessageHeader *message)
-{
-  struct GNUNET_MESH_LocalAck *msg;
-  struct MeshTunnel *t;
-  struct MeshClient *c;
-  MESH_TunnelNumber tid;
-
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a local ACK\n");
-  /* Sanity check for client registration */
-  if (NULL == (c = client_get (client)))
-  {
-    GNUNET_break (0);
-    GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
-    return;
-  }
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "  by client %u\n", c->id);
-
-  msg = (struct GNUNET_MESH_LocalAck *) message;
-
-  /* Tunnel exists? */
-  tid = ntohl (msg->tunnel_id);
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "  on tunnel %X\n", tid);
-  t = tunnel_get_by_local_id (c, tid);
-  if (NULL == t)
-  {
-    GNUNET_break (0);
-    GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tunnel %X unknown.\n", tid);
-    GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "  for client %u.\n", c->id);
-    GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
-    return;
-  }
-
-  /* Does client own tunnel? I.E: Is this an ACK for BCK traffic? */
-  if (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
-  {
-    /* The client owns the tunnel, ACK is for data to_origin, send BCK ACK. */
-    t->prev_fc.last_ack_recv++;
-    tunnel_send_ack (t, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK, GNUNET_NO);
-  }
-  else
-  {
-    /* The client doesn't own the tunnel, this ACK is for FWD traffic. */
-    t->next_fc.last_ack_recv++;
-    tunnel_send_ack (t, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK, GNUNET_YES);
-  }
-
-  GNUNET_SERVER_receive_done (client, GNUNET_OK);
-
-  return;
-}
-
-
-
-/**
- * Iterator over all tunnels to send a monitoring client info about each 
tunnel.
- *
- * @param cls Closure (client handle).
- * @param key Key (hashed tunnel ID, unused).
- * @param value Tunnel info.
- *
- * @return GNUNET_YES, to keep iterating.
- */
-static int
-monitor_all_tunnels_iterator (void *cls,
-                              const struct GNUNET_HashCode * key,
-                              void *value)
-{
-  struct GNUNET_SERVER_Client *client = cls;
-  struct MeshTunnel *t = value;
-  struct GNUNET_MESH_LocalMonitor *msg;
-
-  msg = GNUNET_malloc (sizeof(struct GNUNET_MESH_LocalMonitor));
-  GNUNET_PEER_resolve(t->id.oid, &msg->owner);
-  msg->tunnel_id = htonl (t->id.tid);
-  msg->header.size = htons (sizeof (struct GNUNET_MESH_LocalMonitor));
-  msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS);
-  GNUNET_PEER_resolve (t->dest, &msg->destination);
-
-  GNUNET_log (GNUNET_ERROR_TYPE_INFO,
-              "*  sending info about tunnel %s [%u]\n",
-              GNUNET_i2s (&msg->owner), t->id.tid);
-
-  GNUNET_SERVER_notification_context_unicast (nc, client,
-                                              &msg->header, GNUNET_NO);
-  return GNUNET_YES;
-}
-
-
-/**
- * Handler for client's MONITOR request.
- *
- * @param cls Closure (unused).
- * @param client Identification of the client.
- * @param message The actual message.
- */
-static void
-handle_local_get_tunnels (void *cls, struct GNUNET_SERVER_Client *client,
-                          const struct GNUNET_MessageHeader *message)
-{
-  struct MeshClient *c;
-
-  /* Sanity check for client registration */
-  if (NULL == (c = client_get (client)))
-  {
-    GNUNET_break (0);
-    GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
-    return;
-  }
-
-  GNUNET_log (GNUNET_ERROR_TYPE_INFO,
-              "Received get tunnels request from client %u\n",
-              c->id);
-  GNUNET_CONTAINER_multihashmap_iterate (tunnels,
-                                         monitor_all_tunnels_iterator,
-                                         client);
-  GNUNET_log (GNUNET_ERROR_TYPE_INFO,
-              "Get tunnels request from client %u completed\n",
-              c->id);
-  GNUNET_SERVER_receive_done (client, GNUNET_OK);
-}
-
-
-/**
- * Handler for client's MONITOR_TUNNEL request.
- *
- * @param cls Closure (unused).
- * @param client Identification of the client.
- * @param message The actual message.
- */
-static void
-handle_local_show_tunnel (void *cls, struct GNUNET_SERVER_Client *client,
-                          const struct GNUNET_MessageHeader *message)
-{
-  const struct GNUNET_MESH_LocalMonitor *msg;
-  struct GNUNET_MESH_LocalMonitor *resp;
-  struct MeshClient *c;
-  struct MeshTunnel *t;
-
-  /* Sanity check for client registration */
-  if (NULL == (c = client_get (client)))
-  {
-    GNUNET_break (0);
-    GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
-    return;
-  }
-
-  msg = (struct GNUNET_MESH_LocalMonitor *) message;
-  GNUNET_log (GNUNET_ERROR_TYPE_INFO,
-              "Received tunnel info request from client %u for tunnel 
%s[%X]\n",
-              c->id,
-              &msg->owner,
-              ntohl (msg->tunnel_id));
-  t = tunnel_get (&msg->owner, ntohl (msg->tunnel_id));
-  if (NULL == t)
-  {
-    /* We don't know the tunnel FIXME */
-    struct GNUNET_MESH_LocalMonitor warn;
-
-    warn = *msg;
-    GNUNET_SERVER_notification_context_unicast (nc, client,
-                                                &warn.header,
-                                                GNUNET_NO);
-    GNUNET_SERVER_receive_done (client, GNUNET_OK);
-    return;
-  }
-
-  /* Initialize context */
-  resp = GNUNET_malloc (sizeof (struct GNUNET_MESH_LocalMonitor));
-  *resp = *msg;
-  GNUNET_PEER_resolve (t->dest, &resp->destination);
-  resp->header.size = htons (sizeof (struct GNUNET_MESH_LocalMonitor));
-  GNUNET_SERVER_notification_context_unicast (nc, c->handle,
-                                              &resp->header, GNUNET_NO);
-  GNUNET_free (resp);
-
-  GNUNET_log (GNUNET_ERROR_TYPE_INFO,
-              "Monitor tunnel request from client %u completed\n",
-              c->id);
-  GNUNET_SERVER_receive_done (client, GNUNET_OK);
-}
-
-
-/**
- * Functions to handle messages from clients
- */
-static struct GNUNET_SERVER_MessageHandler client_handlers[] = {
-  {&handle_local_new_client, NULL,
-   GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0},
-  {&handle_local_tunnel_create, NULL,
-   GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE,
-   sizeof (struct GNUNET_MESH_TunnelMessage)},
-  {&handle_local_tunnel_destroy, NULL,
-   GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY,
-   sizeof (struct GNUNET_MESH_TunnelMessage)},
-  {&handle_local_data, NULL,
-   GNUNET_MESSAGE_TYPE_MESH_LOCAL_DATA, 0},
-  {&handle_local_ack, NULL,
-   GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK,
-   sizeof (struct GNUNET_MESH_LocalAck)},
-  {&handle_local_get_tunnels, NULL,
-   GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS,
-   sizeof (struct GNUNET_MessageHeader)},
-  {&handle_local_show_tunnel, NULL,
-   GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNEL,
-     sizeof (struct GNUNET_MESH_LocalMonitor)},
-  {NULL, NULL, 0, 0}
-};
-
-
-/**
- * Method called whenever a given peer connects.
- *
- * @param cls closure
- * @param peer peer identity this notification is about
- */
-static void
-core_connect (void *cls, const struct GNUNET_PeerIdentity *peer)
-{
-  struct MeshPeer *peer_info;
-  struct MeshPeerPath *path;
-
-  DEBUG_CONN ("Peer connected\n");
-  DEBUG_CONN ("     %s\n", GNUNET_i2s (&my_full_id));
-  peer_info = peer_get (peer);
-  if (myid == peer_info->id)
-  {
-    DEBUG_CONN ("     (self)\n");
-    path = path_new (1);
-  }
-  else
-  {
-    DEBUG_CONN ("     %s\n", GNUNET_i2s (peer));
-    path = path_new (2);
-    path->peers[1] = peer_info->id;
-    GNUNET_PEER_change_rc (peer_info->id, 1);
-    GNUNET_STATISTICS_update (stats, "# peers", 1, GNUNET_NO);
-  }
-  path->peers[0] = myid;
-  GNUNET_PEER_change_rc (myid, 1);
-  peer_add_path (peer_info, path, GNUNET_YES);
-  return;
-}
-
-
-/**
- * Method called whenever a peer disconnects.
- *
- * @param cls closure
- * @param peer peer identity this notification is about
- */
-static void
-core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
-{
-  struct MeshPeer *pi;
-  struct MeshPeerQueue *q;
-  struct MeshPeerQueue *n;
-
-  DEBUG_CONN ("Peer disconnected\n");
-  pi = GNUNET_CONTAINER_multipeermap_get (peers, peer);
-  if (NULL == pi)
-  {
-    GNUNET_break (0);
-    return;
-  }
-  q = pi->queue_head;
-  while (NULL != q)
-  {
-      n = q->next;
-      /* TODO try to reroute this traffic instead */
-      queue_destroy(q, GNUNET_YES);
-      q = n;
-  }
-  if (NULL != pi->core_transmit)
-  {
-    GNUNET_CORE_notify_transmit_ready_cancel(pi->core_transmit);
-    pi->core_transmit = NULL;
-  }
-  peer_remove_path (pi, pi->id, myid);
-  if (myid == pi->id)
-  {
-    DEBUG_CONN ("     (self)\n");
-  }
-  GNUNET_STATISTICS_update (stats, "# peers", -1, GNUNET_NO);
-  return;
-}
-
-
-/**
- * Install server (service) handlers and start listening to clients.
- */
-static void
-server_init (void)
-{
-  GNUNET_SERVER_add_handlers (server_handle, client_handlers);
-  GNUNET_SERVER_connect_notify (server_handle,
-                                &handle_local_client_connect, NULL);
-  GNUNET_SERVER_disconnect_notify (server_handle,
-                                   &handle_local_client_disconnect, NULL);
-  nc = GNUNET_SERVER_notification_context_create (server_handle, 1);
-
-  clients_head = NULL;
-  clients_tail = NULL;
-  next_client_id = 0;
-  GNUNET_SERVER_resume (server_handle);
-}
-
-
-/**
- * To be called on core init/fail.
- *
- * @param cls Closure (config)
- * @param identity the public identity of this peer
- */
-static void
-core_init (void *cls,
-           const struct GNUNET_PeerIdentity *identity)
-{
-  const struct GNUNET_CONFIGURATION_Handle *c = cls;
-  static int i = 0;
-
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Core init\n");
-  if (0 != memcmp (identity, &my_full_id, sizeof (my_full_id)))
-  {
-    GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Wrong CORE service\n"));
-    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
-                " core id %s\n",
-                GNUNET_i2s (identity));
-    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
-                " my id %s\n",
-                GNUNET_i2s (&my_full_id));
-    GNUNET_CORE_disconnect (core_handle);
-    core_handle = GNUNET_CORE_connect (c, /* Main configuration */
-                                       NULL,      /* Closure passed to MESH 
functions */
-                                       &core_init,        /* Call core_init 
once connected */
-                                       &core_connect,     /* Handle connects */
-                                       &core_disconnect,  /* remove peers on 
disconnects */
-                                       NULL,      /* Don't notify about all 
incoming messages */
-                                       GNUNET_NO, /* For header only in 
notification */
-                                       NULL,      /* Don't notify about all 
outbound messages */
-                                       GNUNET_NO, /* For header-only out 
notification */
-                                       core_handlers);    /* Register these 
handlers */
-    if (10 < i++)
-      GNUNET_abort();
-  }
-  server_init ();
-  return;
-}
-
-
-/******************************************************************************/
-/************************      MAIN FUNCTIONS      
****************************/
-/******************************************************************************/
-
-/**
- * Iterator over tunnel hash map entries to destroy the tunnel during shutdown.
- *
- * @param cls closure
- * @param key current key code
- * @param value value in the hash map
- * @return GNUNET_YES if we should continue to iterate,
- *         GNUNET_NO if not.
- */
-static int
-shutdown_tunnel (void *cls, const struct GNUNET_HashCode * key, void *value)
-{
-  struct MeshTunnel *t = value;
-
-  tunnel_destroy (t);
-  return GNUNET_YES;
-}
-
-/**
- * Iterator over peer hash map entries to destroy the tunnel during shutdown.
- *
- * @param cls closure
- * @param key current key code
- * @param value value in the hash map
- * @return GNUNET_YES if we should continue to iterate,
- *         GNUNET_NO if not.
- */
-static int
-shutdown_peer (void *cls, const struct GNUNET_PeerIdentity *key, void *value)
-{
-  struct MeshPeer *p = value;
-  struct MeshPeerQueue *q;
-  struct MeshPeerQueue *n;
-
-  q = p->queue_head;
-  while (NULL != q)
-  {
-      n = q->next;
-      if (q->peer == p)
-      {
-        queue_destroy(q, GNUNET_YES);
-      }
-      q = n;
-  }
-  peer_destroy (p);
-  return GNUNET_YES;
-}
-
-
-/**
- * Task run during shutdown.
- *
- * @param cls unused
- * @param tc unused
- */
-static void
-shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
-{
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shutting down\n");
-
-  if (core_handle != NULL)
-  {
-    GNUNET_CORE_disconnect (core_handle);
-    core_handle = NULL;
-  }
-  GNUNET_CONTAINER_multihashmap_iterate (tunnels, &shutdown_tunnel, NULL);
-  GNUNET_CONTAINER_multipeermap_iterate (peers, &shutdown_peer, NULL);
-  if (dht_handle != NULL)
-  {
-    GNUNET_DHT_disconnect (dht_handle);
-    dht_handle = NULL;
-  }
-  if (nc != NULL)
-  {
-    GNUNET_SERVER_notification_context_destroy (nc);
-    nc = NULL;
-  }
-  if (GNUNET_SCHEDULER_NO_TASK != announce_id_task)
-  {
-    GNUNET_SCHEDULER_cancel (announce_id_task);
-    announce_id_task = GNUNET_SCHEDULER_NO_TASK;
-  }
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shut down\n");
-}
-
-
-/**
- * Process mesh requests.
- *
- * @param cls closure
- * @param server the initialized server
- * @param c configuration to use
- */
-static void
-run (void *cls, struct GNUNET_SERVER_Handle *server,
-     const struct GNUNET_CONFIGURATION_Handle *c)
-{
-  char *keyfile;
-  struct GNUNET_CRYPTO_EddsaPrivateKey *pk;
-
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "starting to run\n");
-  server_handle = server;
-  GNUNET_SERVER_suspend (server_handle);
-
-  if (GNUNET_OK !=
-      GNUNET_CONFIGURATION_get_value_filename (c, "PEER", "PRIVATE_KEY",
-                                               &keyfile))
-  {
-    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
-                _
-                ("%s service is lacking key configuration settings (%s).  
Exiting.\n"),
-                "mesh", "peer/privatekey");
-    GNUNET_SCHEDULER_shutdown ();
-    return;
-  }
-
-  if (GNUNET_OK !=
-      GNUNET_CONFIGURATION_get_value_time (c, "MESH", "REFRESH_PATH_TIME",
-                                           &refresh_path_time))
-  {
-    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
-                _
-                ("%s service is lacking key configuration settings (%s).  
Exiting.\n"),
-                "mesh", "refresh path time");
-    GNUNET_SCHEDULER_shutdown ();
-    return;
-  }
-
-  if (GNUNET_OK !=
-      GNUNET_CONFIGURATION_get_value_time (c, "MESH", "ID_ANNOUNCE_TIME",
-                                           &id_announce_time))
-  {
-    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
-                _
-                ("%s service is lacking key configuration settings (%s).  
Exiting.\n"),
-                "mesh", "id announce time");
-    GNUNET_SCHEDULER_shutdown ();
-    return;
-  }
-
-  if (GNUNET_OK !=
-      GNUNET_CONFIGURATION_get_value_time (c, "MESH", "CONNECT_TIMEOUT",
-                                           &connect_timeout))
-  {
-    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
-                _
-                ("%s service is lacking key configuration settings (%s).  
Exiting.\n"),
-                "mesh", "connect timeout");
-    GNUNET_SCHEDULER_shutdown ();
-    return;
-  }
-
-  if (GNUNET_OK !=
-      GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_MSGS_QUEUE",
-                                             &max_msgs_queue))
-  {
-    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
-                _
-                ("%s service is lacking key configuration settings (%s).  
Exiting.\n"),
-                "mesh", "max msgs queue");
-    GNUNET_SCHEDULER_shutdown ();
-    return;
-  }
-
-  if (GNUNET_OK !=
-      GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_TUNNELS",
-                                             &max_tunnels))
-  {
-    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
-                _
-                ("%s service is lacking key configuration settings (%s).  
Exiting.\n"),
-                "mesh", "max tunnels");
-    GNUNET_SCHEDULER_shutdown ();
-    return;
-  }
-
-  if (GNUNET_OK !=
-      GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DEFAULT_TTL",
-                                             &default_ttl))
-  {
-    GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
-                _
-                ("%s service is lacking key configuration settings (%s). Using 
default (%u).\n"),
-                "mesh", "default ttl", 64);
-    default_ttl = 64;
-  }
-
-  if (GNUNET_OK !=
-      GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_PEERS",
-                                             &max_peers))
-  {
-    GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
-                _("%s service is lacking key configuration settings (%s). 
Using default (%u).\n"),
-                "mesh", "max peers", 1000);
-    max_peers = 1000;
-  }
-
-  if (GNUNET_OK !=
-      GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DROP_PERCENT",
-                                             &drop_percent))
-  {
-    drop_percent = 0;
-  }
-  else
-  {
-    GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
-                "Mesh is running with drop mode enabled. "
-                "This is NOT a good idea! "
-                "Remove the DROP_PERCENT option from your configuration.\n");
-  }
-
-  if (GNUNET_OK !=
-      GNUNET_CONFIGURATION_get_value_number (c, "MESH", 
"DHT_REPLICATION_LEVEL",
-                                             &dht_replication_level))
-  {
-    GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
-                _
-                ("%s service is lacking key configuration settings (%s). Using 
default (%u).\n"),
-                "mesh", "dht replication level", 3);
-    dht_replication_level = 3;
-  }
-
-  tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
-  incoming_tunnels = GNUNET_CONTAINER_multihashmap32_create (32);
-  peers = GNUNET_CONTAINER_multipeermap_create (32, GNUNET_NO);
-  ports = GNUNET_CONTAINER_multihashmap32_create (32);
-
-  dht_handle = GNUNET_DHT_connect (c, 64);
-  if (NULL == dht_handle)
-  {
-    GNUNET_break (0);
-  }
-  stats = GNUNET_STATISTICS_create ("mesh", c);
-
-  /* Scheduled the task to clean up when shutdown is called */
-  GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
-                                NULL);
-  pk = GNUNET_CRYPTO_eddsa_key_create_from_file (keyfile);
-  GNUNET_free (keyfile);
-  GNUNET_assert (NULL != pk);
-  my_private_key = pk;
-  GNUNET_CRYPTO_eddsa_key_get_public (my_private_key,
-                                                 &my_full_id.public_key);
-  myid = GNUNET_PEER_intern (&my_full_id);
-  GNUNET_log (GNUNET_ERROR_TYPE_INFO,
-              "Mesh for peer [%s] starting\n",
-              GNUNET_i2s(&my_full_id));
-
-  core_handle = GNUNET_CORE_connect (c, /* Main configuration */
-                                     NULL,      /* Closure passed to MESH 
functions */
-                                     &core_init,        /* Call core_init once 
connected */
-                                     &core_connect,     /* Handle connects */
-                                     &core_disconnect,  /* remove peers on 
disconnects */
-                                     NULL,      /* Don't notify about all 
incoming messages */
-                                     GNUNET_NO, /* For header only in 
notification */
-                                     NULL,      /* Don't notify about all 
outbound messages */
-                                     GNUNET_NO, /* For header-only out 
notification */
-                                     core_handlers);    /* Register these 
handlers */
-  if (NULL == core_handle)
-  {
-    GNUNET_break (0);
-    GNUNET_SCHEDULER_shutdown ();
-    return;
-  }
-  next_tid = 0;
-  next_local_tid = GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
-  announce_id_task = GNUNET_SCHEDULER_add_now (&announce_id, cls);
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Mesh service running\n");
-}
-
-
-/**
- * The main function for the mesh service.
- *
- * @param argc number of arguments from the command line
- * @param argv command line arguments
- * @return 0 ok, 1 on error
- */
-int
-main (int argc, char *const *argv)
-{
-  int ret;
-  int r;
-
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main()\n");
-  r = GNUNET_SERVICE_run (argc, argv, "mesh", GNUNET_SERVICE_OPTION_NONE, &run,
-                          NULL);
-  ret = (GNUNET_OK == r) ? 0 : 1;
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main() END\n");
-
-  INTERVAL_SHOW;
-
-  return ret;
-}

Copied: gnunet/src/mesh/gnunet-service-mesh.c (from rev 30671, 
gnunet/src/mesh/gnunet-service-mesh-enc.c)
===================================================================
--- gnunet/src/mesh/gnunet-service-mesh.c                               (rev 0)
+++ gnunet/src/mesh/gnunet-service-mesh.c       2013-11-14 13:55:12 UTC (rev 
30697)
@@ -0,0 +1,169 @@
+/*
+     This file is part of GNUnet.
+     (C) 2001-2013 Christian Grothoff (and other contributing authors)
+
+     GNUnet is free software; you can redistribute it and/or modify
+     it under the terms of the GNU General Public License as published
+     by the Free Software Foundation; either version 3, or (at your
+     option) any later version.
+
+     GNUnet is distributed in the hope that it will be useful, but
+     WITHOUT ANY WARRANTY; without even the implied warranty of
+     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+     General Public License for more details.
+
+     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.
+*/
+
+/**
+ * @file mesh/gnunet-service-mesh.c
+ * @brief GNUnet MESH service with encryption
+ * @author Bartlomiej Polot
+ *
+ *  FIXME in progress:
+ * - rekey - reliability interaction
+ * - channel retransmit timing
+ *
+ * TODO:
+ * - relay corking down to core
+ * - set ttl relative to path length
+ * TODO END
+ *
+ * Dictionary:
+ * - peer: other mesh instance. If there is direct connection it's a neighbor.
+ * - tunnel: encrypted connection to a peer, neighbor or not.
+ * - channel: connection between two clients, on the same or different peers.
+ *            have properties like reliability.
+ * - path: series of directly connected peer from one peer to another.
+ * - connection: path which is being used in a tunnel.
+ */
+
+#include "platform.h"
+#include "gnunet_util_lib.h"
+#include "mesh.h"
+#include "gnunet_statistics_service.h"
+
+#include "gnunet-service-mesh_local.h"
+#include "gnunet-service-mesh_channel.h"
+#include "gnunet-service-mesh_connection.h"
+#include "gnunet-service-mesh_tunnel.h"
+#include "gnunet-service-mesh_dht.h"
+#include "gnunet-service-mesh_peer.h"
+
+
+/******************************************************************************/
+/***********************      GLOBAL VARIABLES     
****************************/
+/******************************************************************************/
+
+/****************************** Global variables 
******************************/
+
+/**
+ * Handle to the statistics service.
+ */
+struct GNUNET_STATISTICS_Handle *stats;
+
+/**
+ * Local peer own ID (memory efficient handle).
+ */
+GNUNET_PEER_Id myid;
+
+/**
+ * Local peer own ID (full value).
+ */
+struct GNUNET_PeerIdentity my_full_id;
+
+/*************************** Static global variables 
**************************/
+
+/**
+ * Own private key.
+ */
+static struct GNUNET_CRYPTO_EddsaPrivateKey *my_private_key;
+
+
+/******************************************************************************/
+/************************      MAIN FUNCTIONS      
****************************/
+/******************************************************************************/
+
+/**
+ * Task run during shutdown.
+ *
+ * @param cls unused
+ * @param tc unused
+ */
+static void
+shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
+{
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shutting down\n");
+
+  GML_shutdown ();
+  GMD_shutdown ();
+  GMP_shutdown ();
+  GMC_shutdown ();
+  GMT_shutdown ();
+
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shut down\n");
+}
+
+
+/**
+ * Process mesh requests.
+ *
+ * @param cls closure
+ * @param server the initialized server
+ * @param c configuration to use
+ */
+static void
+run (void *cls, struct GNUNET_SERVER_Handle *server,
+     const struct GNUNET_CONFIGURATION_Handle *c)
+{
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "starting to run\n");
+
+  stats = GNUNET_STATISTICS_create ("mesh", c);
+
+  /* Scheduled the task to clean up when shutdown is called */
+  GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
+                                NULL);
+  GNUNET_log (GNUNET_ERROR_TYPE_INFO, "reading key\n");
+  my_private_key = GNUNET_CRYPTO_eddsa_key_create_from_configuration (c);
+  GNUNET_assert (NULL != my_private_key);
+  GNUNET_CRYPTO_eddsa_key_get_public (my_private_key, &my_full_id.public_key);
+  myid = GNUNET_PEER_intern (&my_full_id);
+  GNUNET_log (GNUNET_ERROR_TYPE_INFO,
+              "Mesh for peer [%s] starting\n",
+              GNUNET_i2s (&my_full_id));
+
+  GML_init (server);    /* Local clients */
+  GMC_init (c);         /* Connections */
+  GMP_init (c);         /* Peers */
+  GMD_init (c);         /* DHT */
+  GMT_init (c, my_private_key); /* Tunnels */
+
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Mesh service running\n");
+}
+
+
+/**
+ * The main function for the mesh service.
+ *
+ * @param argc number of arguments from the command line
+ * @param argv command line arguments
+ * @return 0 ok, 1 on error
+ */
+int
+main (int argc, char *const *argv)
+{
+  int ret;
+  int r;
+
+  fprintf (stderr, "main()\n");
+  r = GNUNET_SERVICE_run (argc, argv, "mesh", GNUNET_SERVICE_OPTION_NONE, &run,
+                          NULL);
+  GNUNET_free (my_private_key);
+  ret = (GNUNET_OK == r) ? 0 : 1;
+  fprintf (stderr, "main() END\n");
+
+  return ret;
+}

Modified: gnunet/src/mesh/gnunet-service-mesh_channel.c
===================================================================
--- gnunet/src/mesh/gnunet-service-mesh_channel.c       2013-11-14 13:30:29 UTC 
(rev 30696)
+++ gnunet/src/mesh/gnunet-service-mesh_channel.c       2013-11-14 13:55:12 UTC 
(rev 30697)
@@ -24,8 +24,8 @@
 
 #include "gnunet_statistics_service.h"
 
-#include "mesh_enc.h"
-#include "mesh_protocol_enc.h"
+#include "mesh.h"
+#include "mesh_protocol.h"
 
 #include "gnunet-service-mesh_channel.h"
 #include "gnunet-service-mesh_local.h"

Modified: gnunet/src/mesh/gnunet-service-mesh_channel.h
===================================================================
--- gnunet/src/mesh/gnunet-service-mesh_channel.h       2013-11-14 13:30:29 UTC 
(rev 30696)
+++ gnunet/src/mesh/gnunet-service-mesh_channel.h       2013-11-14 13:55:12 UTC 
(rev 30697)
@@ -40,8 +40,8 @@
 #include "platform.h"
 #include "gnunet_util_lib.h"
 
-#include "mesh_protocol_enc.h"
-#include "mesh_enc.h"
+#include "mesh_protocol.h"
+#include "mesh.h"
 
 /**
  * Struct containing all information regarding a channel to a remote client.

Modified: gnunet/src/mesh/gnunet-service-mesh_connection.c
===================================================================
--- gnunet/src/mesh/gnunet-service-mesh_connection.c    2013-11-14 13:30:29 UTC 
(rev 30696)
+++ gnunet/src/mesh/gnunet-service-mesh_connection.c    2013-11-14 13:55:12 UTC 
(rev 30697)
@@ -30,8 +30,8 @@
 #include "gnunet_statistics_service.h"
 
 #include "mesh_path.h"
-#include "mesh_protocol_enc.h"
-#include "mesh_enc.h"
+#include "mesh_protocol.h"
+#include "mesh.h"
 #include "gnunet-service-mesh_connection.h"
 #include "gnunet-service-mesh_peer.h"
 #include "gnunet-service-mesh_tunnel.h"

Modified: gnunet/src/mesh/gnunet-service-mesh_local.c
===================================================================
--- gnunet/src/mesh/gnunet-service-mesh_local.c 2013-11-14 13:30:29 UTC (rev 
30696)
+++ gnunet/src/mesh/gnunet-service-mesh_local.c 2013-11-14 13:55:12 UTC (rev 
30697)
@@ -24,8 +24,8 @@
 
 #include "gnunet_statistics_service.h"
 
-#include "mesh_enc.h"
-#include "mesh_protocol_enc.h" // GNUNET_MESH_Data is shared
+#include "mesh.h"
+#include "mesh_protocol.h" /* GNUNET_MESH_Data is shared */
 
 #include "gnunet-service-mesh_local.h"
 

Modified: gnunet/src/mesh/gnunet-service-mesh_peer.c
===================================================================
--- gnunet/src/mesh/gnunet-service-mesh_peer.c  2013-11-14 13:30:29 UTC (rev 
30696)
+++ gnunet/src/mesh/gnunet-service-mesh_peer.c  2013-11-14 13:55:12 UTC (rev 
30697)
@@ -25,7 +25,7 @@
 #include "gnunet_core_service.h"
 #include "gnunet_statistics_service.h"
 
-#include "mesh_protocol_enc.h"
+#include "mesh_protocol.h"
 
 #include "gnunet-service-mesh_peer.h"
 #include "gnunet-service-mesh_dht.h"

Modified: gnunet/src/mesh/gnunet-service-mesh_tunnel.c
===================================================================
--- gnunet/src/mesh/gnunet-service-mesh_tunnel.c        2013-11-14 13:30:29 UTC 
(rev 30696)
+++ gnunet/src/mesh/gnunet-service-mesh_tunnel.c        2013-11-14 13:55:12 UTC 
(rev 30697)
@@ -24,7 +24,7 @@
 #include "gnunet_signatures.h"
 #include "gnunet_statistics_service.h"
 
-#include "mesh_protocol_enc.h"
+#include "mesh_protocol.h"
 #include "mesh_path.h"
 
 #include "gnunet-service-mesh_tunnel.h"

Deleted: gnunet/src/mesh/mesh.h
===================================================================
--- gnunet/src/mesh/mesh.h      2013-11-14 13:30:29 UTC (rev 30696)
+++ gnunet/src/mesh/mesh.h      2013-11-14 13:55:12 UTC (rev 30697)
@@ -1,323 +0,0 @@
-/*
-     This file is part of GNUnet.
-     (C) 2001 - 2011 Christian Grothoff (and other contributing authors)
-
-     GNUnet is free software; you can redistribute it and/or modify
-     it under the terms of the GNU General Public License as published
-     by the Free Software Foundation; either version 3, or (at your
-     option) any later version.
-
-     GNUnet is distributed in the hope that it will be useful, but
-     WITHOUT ANY WARRANTY; without even the implied warranty of
-     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
-     General Public License for more details.
-
-     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.
-*/
-
-/**
- * @author Bartlomiej Polot
- * @file mesh/mesh.h
- */
-
-#ifndef MESH_H_
-#define MESH_H_
-
-#ifdef __cplusplus
-extern "C"
-{
-#if 0                           /* keep Emacsens' auto-indent happy */
-}
-#endif
-#endif
-
-#include <stdint.h>
-
-#define MESH_DEBUG              GNUNET_YES
-
-#include "platform.h"
-#include "gnunet_util_lib.h"
-#include "gnunet_core_service.h"
-#include "gnunet_protocols.h"
-#include <gnunet_mesh_service.h>
-
-/******************************************************************************/
-/********************        MESH LOCAL MESSAGES      
*************************/
-/******************************************************************************/
-/*  Any API call should be documented in the folowing table under API CALL.
- *  Also, any message type should be documented in the following table, with 
the
- * associated event.
- *
- * API CALL (GNUNET_MESH_*)             MESSAGE USED
- * ------------------------             ------------
- * connect                              GNUNET_MESH_ClientConnect
- * disconnect                           None (network level disconnect)
- *
- * tunnel_create                        GNUNET_MESH_TunnelMessage
- * tunnel_destroy                       GNUNET_MESH_TunnelMessage
- * tunnel_buffer                        GNUNET_MESH_TunnelMessage
- *
- * notify_transmit_ready                None (queue / 
GNUNET_CLIENT_ntf_tmt_rdy)
- * notify_transmit_ready_cancel         None (clear of internal data 
structures)
- *
- *
- * EVENT                                MESSAGE USED
- * -----                                ------------
- * data                                 GNUNET_MESH_Unicast OR
- *                                      GNUNET_MESH_Multicast OR
- *                                      GNUNET_MESH_ToOrigin
- * data ack                             GNUNET_MESH_LocalAck
- *
- * new incoming tunnel                  GNUNET_MESH_PeerControl
- * peer connects to a tunnel            FIXME
- * peer disconnects from a tunnel       FIXME
- */
-
-/******************************************************************************/
-/**************************       CONSTANTS      
******************************/
-/******************************************************************************/
-
-#define GNUNET_MESH_LOCAL_TUNNEL_ID_CLI         0x80000000
-#define GNUNET_MESH_LOCAL_TUNNEL_ID_SERV        0xB0000000
-
-#define HIGH_PID                                0xFFFF0000
-#define LOW_PID                                 0x0000FFFF
-
-#define PID_OVERFLOW(pid, max) (pid > HIGH_PID && max < LOW_PID)
-
-/******************************************************************************/
-/**************************        MESSAGES      
******************************/
-/******************************************************************************/
-
-GNUNET_NETWORK_STRUCT_BEGIN
-
-/**
- * Message for a client to register to the service
- */
-struct GNUNET_MESH_ClientConnect
-{
-    /**
-     * Type: GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT
-     *
-     * Size: sizeof(struct GNUNET_MESH_ClientConnect) +
-     *       sizeof(MESH_ApplicationType) * applications +
-     *       sizeof(uint16_t) * types
-     */
-  struct GNUNET_MessageHeader header;
-  /* uint32_t                 list_ports[]           */
-};
-
-
-/**
- * Type for tunnel numbering.
- * - Local tunnel numbers given by the service (incoming) are >= 0xB0000000
- * - Local tunnel numbers given by the client (created) are >= 0x80000000
- * - Global tunnel numbers are < 0x80000000
- */
-typedef uint32_t MESH_TunnelNumber;
-
-
-/**
- * Message for a client to create and destroy tunnels.
- */
-struct GNUNET_MESH_TunnelMessage
-{
-    /**
-     * Type: GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_[CREATE|DESTROY]
-     *
-     * Size: sizeof(struct GNUNET_MESH_TunnelMessage)
-     */
-  struct GNUNET_MessageHeader header;
-
-    /**
-     * ID of a tunnel controlled by this client.
-     */
-  MESH_TunnelNumber tunnel_id GNUNET_PACKED;
-
-    /**
-     * Tunnel's peer
-     */
-  struct GNUNET_PeerIdentity peer;
-
-    /**
-     * Port of the tunnel.
-     */
-  uint32_t port GNUNET_PACKED;
-
-    /**
-     * Options.
-     */
-  uint32_t opt GNUNET_PACKED;
-};
-
-
-/**
- * Message for mesh data traffic.
- */
-struct GNUNET_MESH_LocalData
-{
-    /**
-     * Type: GNUNET_MESSAGE_TYPE_MESH_LOCAL_DATA
-     */
-  struct GNUNET_MessageHeader header;
-
-    /**
-     * TID of the tunnel
-     */
-  uint32_t tid GNUNET_PACKED;
-
-    /**
-     * Payload follows
-     */
-};
-
-
-/**
- * Message to allow the client send more data to the service
- * (always service -> client).
- */
-struct GNUNET_MESH_LocalAck
-{
-    /**
-     * Type: GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK
-     */
-  struct GNUNET_MessageHeader header;
-
-    /**
-     * ID of the tunnel allowed to send more data.
-     */
-  MESH_TunnelNumber tunnel_id GNUNET_PACKED;
-
-};
-
-
-/**
- * Message to inform the client about tunnels in the service.
- */
-struct GNUNET_MESH_LocalMonitor
-{
-  /**
-     * Type: GNUNET_MESSAGE_TYPE_MESH_LOCAL_MONITOR[_TUNNEL]
-   */
-  struct GNUNET_MessageHeader header;
-
-  /**
-   * ID of the tunnel allowed to send more data.
-   */
-  MESH_TunnelNumber tunnel_id GNUNET_PACKED;
-
-  /**
-   * Alignment.
-   */
-  uint32_t reserved GNUNET_PACKED;
-
-  /**
-   * ID of the owner of the tunnel (can be local peer).
-   */
-  struct GNUNET_PeerIdentity owner;
-
-  /**
-   * ID of the destination of the tunnel (can be local peer).
-   */
-  struct GNUNET_PeerIdentity destination;
-};
-
-
-GNUNET_NETWORK_STRUCT_END
-
-/******************************************************************************/
-/************************        ENUMERATIONS      
****************************/
-/******************************************************************************/
-
-/**
- * All the states a tunnel can be in.
- */
-enum MeshTunnelState
-{
-    /**
-     * Uninitialized status, should never appear in operation.
-     */
-  MESH_TUNNEL_NEW,
-
-    /**
-     * Path to the peer not known yet
-     */
-  MESH_TUNNEL_SEARCHING,
-
-    /**
-     * Request sent, not yet answered.
-     */
-  MESH_TUNNEL_WAITING,
-
-    /**
-     * Peer connected and ready to accept data
-     */
-  MESH_TUNNEL_READY,
-
-    /**
-     * Peer connected previosly but not responding
-     */
-  MESH_TUNNEL_RECONNECTING
-};
-
-
-/**
- * Check if one pid is bigger than other, accounting for overflow.
- *
- * @param bigger Argument that should be bigger.
- * @param smaller Argument that should be smaller.
- *
- * @return True if bigger (arg1) has a higher value than smaller (arg 2).
- */
-int
-GMC_is_pid_bigger (uint32_t bigger, uint32_t smaller);
-
-
-/**
- * Get the higher ACK value out of two values, taking in account overflow.
- *
- * @param a First ACK value.
- * @param b Second ACK value.
- *
- * @return Highest ACK value from the two.
- */
-uint32_t
-GMC_max_pid (uint32_t a, uint32_t b);
-
-
-/**
- * Get the lower ACK value out of two values, taking in account overflow.
- *
- * @param a First ACK value.
- * @param b Second ACK value.
- *
- * @return Lowest ACK value from the two.
- */
-uint32_t
-GMC_min_pid (uint32_t a, uint32_t b);
-
-
-/**
- * Convert a message type into a string to help debug
- * Generated with:
- * FIND:        "#define ([^ ]+)[ ]*([0-9]+)"
- * REPLACE:     "    case \2: return "\1"; break;"
- *
- * @param m Message type.
- *
- * @return Human readable string description.
- */
-const char *
-GNUNET_MESH_DEBUG_M2S (uint16_t m);
-
-#if 0                           /* keep Emacsens' auto-indent happy */
-{
-#endif
-#ifdef __cplusplus
-}
-#endif
-
-#endif

Copied: gnunet/src/mesh/mesh.h (from rev 30671, gnunet/src/mesh/mesh_enc.h)
===================================================================
--- gnunet/src/mesh/mesh.h                              (rev 0)
+++ gnunet/src/mesh/mesh.h      2013-11-14 13:55:12 UTC (rev 30697)
@@ -0,0 +1,259 @@
+/*
+     This file is part of GNUnet.
+     (C) 2001 - 2011 Christian Grothoff (and other contributing authors)
+
+     GNUnet is free software; you can redistribute it and/or modify
+     it under the terms of the GNU General Public License as published
+     by the Free Software Foundation; either version 3, or (at your
+     option) any later version.
+
+     GNUnet is distributed in the hope that it will be useful, but
+     WITHOUT ANY WARRANTY; without even the implied warranty of
+     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+     General Public License for more details.
+
+     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.
+*/
+
+/**
+ * @author Bartlomiej Polot
+ * @file mesh/mesh.h
+ */
+
+#ifndef MESH_H_
+#define MESH_H_
+
+#ifdef __cplusplus
+extern "C"
+{
+#if 0                           /* keep Emacsens' auto-indent happy */
+}
+#endif
+#endif
+
+#include <stdint.h>
+
+#define MESH_DEBUG              GNUNET_YES
+
+#include "platform.h"
+#include "gnunet_common.h"
+#include "gnunet_util_lib.h"
+#include "gnunet_peer_lib.h"
+#include "gnunet_core_service.h"
+#include "gnunet_protocols.h"
+#include <gnunet_mesh_service.h>
+
+/******************************************************************************/
+/**************************       CONSTANTS      
******************************/
+/******************************************************************************/
+
+#define GNUNET_MESH_LOCAL_CHANNEL_ID_CLI        0x80000000
+#define GNUNET_MESH_LOCAL_CHANNEL_ID_SERV       0xB0000000
+
+#define HIGH_PID                                0xFFFF0000
+#define LOW_PID                                 0x0000FFFF
+
+#define PID_OVERFLOW(pid, max) (pid > HIGH_PID && max < LOW_PID)
+
+/******************************************************************************/
+/**************************        MESSAGES      
******************************/
+/******************************************************************************/
+
+GNUNET_NETWORK_STRUCT_BEGIN
+
+/**
+ * Message for a client to register to the service
+ */
+struct GNUNET_MESH_ClientConnect
+{
+    /**
+     * Type: GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT
+     *
+     * Size: sizeof(struct GNUNET_MESH_ClientConnect) +
+     *       sizeof(MESH_ApplicationType) * applications +
+     *       sizeof(uint16_t) * types
+     */
+  struct GNUNET_MessageHeader header;
+  /* uint32_t                 list_ports[]           */
+};
+
+
+/**
+ * Type for channel numbering.
+ * - Local channel numbers given by the service (incoming) are >= 0xB0000000
+ * - Local channel numbers given by the client (created) are >= 0x80000000
+ * - Global channel numbers are < 0x80000000
+ */
+typedef uint32_t MESH_ChannelNumber;
+
+
+/**
+ * Message for a client to create and destroy channels.
+ */
+struct GNUNET_MESH_ChannelMessage
+{
+    /**
+     * Type: GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_[CREATE|DESTROY]
+     *
+     * Size: sizeof(struct GNUNET_MESH_ChannelMessage)
+     */
+  struct GNUNET_MessageHeader header;
+
+    /**
+     * ID of a channel controlled by this client.
+     */
+  MESH_ChannelNumber channel_id GNUNET_PACKED;
+
+    /**
+     * Channel's peer
+     */
+  struct GNUNET_PeerIdentity peer;
+
+    /**
+     * Port of the channel.
+     */
+  uint32_t port GNUNET_PACKED;
+
+    /**
+     * Options.
+     */
+  uint32_t opt GNUNET_PACKED;
+};
+
+
+/**
+ * Message for mesh data traffic.
+ */
+struct GNUNET_MESH_LocalData
+{
+    /**
+     * Type: GNUNET_MESSAGE_TYPE_MESH_LOCAL_DATA
+     */
+  struct GNUNET_MessageHeader header;
+
+    /**
+     * ID of the channel
+     */
+  uint32_t id GNUNET_PACKED;
+
+    /**
+     * Payload follows
+     */
+};
+
+
+/**
+ * Message to allow the client send more data to the service
+ * (always service -> client).
+ */
+struct GNUNET_MESH_LocalAck
+{
+    /**
+     * Type: GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK
+     */
+  struct GNUNET_MessageHeader header;
+
+    /**
+     * ID of the channel allowed to send more data.
+     */
+  MESH_ChannelNumber channel_id GNUNET_PACKED;
+
+};
+
+
+/**
+ * Message to inform the client about channels in the service.
+ */
+struct GNUNET_MESH_LocalMonitor
+{
+  /**
+     * Type: GNUNET_MESSAGE_TYPE_MESH_LOCAL_MONITOR[_TUNNEL]
+   */
+  struct GNUNET_MessageHeader header;
+
+  /**
+   * ID of the channel allowed to send more data.
+   */
+  MESH_ChannelNumber channel_id GNUNET_PACKED;
+
+  /**
+   * Alignment.
+   */
+  uint32_t reserved GNUNET_PACKED;
+
+  /**
+   * ID of the owner of the channel (can be local peer).
+   */
+  struct GNUNET_PeerIdentity owner;
+
+  /**
+   * ID of the destination of the channel (can be local peer).
+   */
+  struct GNUNET_PeerIdentity destination;
+};
+
+
+GNUNET_NETWORK_STRUCT_END
+
+
+
+/**
+ * Check if one pid is bigger than other, accounting for overflow.
+ *
+ * @param bigger Argument that should be bigger.
+ * @param smaller Argument that should be smaller.
+ *
+ * @return True if bigger (arg1) has a higher value than smaller (arg 2).
+ */
+int
+GMC_is_pid_bigger (uint32_t bigger, uint32_t smaller);
+
+
+/**
+ * Get the higher ACK value out of two values, taking in account overflow.
+ *
+ * @param a First ACK value.
+ * @param b Second ACK value.
+ *
+ * @return Highest ACK value from the two.
+ */
+uint32_t
+GMC_max_pid (uint32_t a, uint32_t b);
+
+
+/**
+ * Get the lower ACK value out of two values, taking in account overflow.
+ *
+ * @param a First ACK value.
+ * @param b Second ACK value.
+ *
+ * @return Lowest ACK value from the two.
+ */
+uint32_t
+GMC_min_pid (uint32_t a, uint32_t b);
+
+
+/**
+ * Convert a message type into a string to help debug
+ * Generated with:
+ * FIND:        "#define ([^ ]+)[ ]*([0-9]+)"
+ * REPLACE:     "    case \2: return "\1"; break;"
+ *
+ * @param m Message type.
+ *
+ * @return Human readable string description.
+ */
+const char *
+GNUNET_MESH_DEBUG_M2S (uint16_t m);
+
+#if 0                           /* keep Emacsens' auto-indent happy */
+{
+#endif
+#ifdef __cplusplus
+}
+#endif
+
+#endif

Deleted: gnunet/src/mesh/mesh_api.c
===================================================================
--- gnunet/src/mesh/mesh_api.c  2013-11-14 13:30:29 UTC (rev 30696)
+++ gnunet/src/mesh/mesh_api.c  2013-11-14 13:55:12 UTC (rev 30697)
@@ -1,1826 +0,0 @@
-/*
-     This file is part of GNUnet.
-     (C) 2011 Christian Grothoff (and other contributing authors)
-     GNUnet is free software; you can redistribute it and/or modify
-     it under the terms of the GNU General Public License as published
-     by the Free Software Foundation; either version 3, or (at your
-     option) any later version.
-     GNUnet is distributed in the hope that it will be useful, but
-     WITHOUT ANY WARRANTY; without even the implied warranty of
-     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
-     General Public License for more details.
-     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.
-*/
-
-/**
- * @file mesh/mesh_api.c
- * @brief mesh api: client implementation of new mesh service
- * @author Bartlomiej Polot
- *
- * STRUCTURE:
- * - DATA STRUCTURES
- * - DECLARATIONS
- * - AUXILIARY FUNCTIONS
- * - RECEIVE HANDLERS
- * - SEND FUNCTIONS
- * - API CALL DEFINITIONS
- *
- * TODO: add regex to reconnect
- */
-#include "platform.h"
-#include "gnunet_util_lib.h"
-#include "gnunet_mesh_service.h"
-#include "mesh.h"
-#include "mesh_protocol.h"
-
-#define LOG(kind,...) GNUNET_log_from (kind, "mesh-api",__VA_ARGS__)
-#define DEBUG_ACK GNUNET_YES
-
-/******************************************************************************/
-/************************      DATA STRUCTURES     
****************************/
-/******************************************************************************/
-
-/**
- * Transmission queue to the service
- */
-struct GNUNET_MESH_TransmitHandle
-{
-
-    /**
-     * Double Linked list
-     */
-  struct GNUNET_MESH_TransmitHandle *next;
-
-    /**
-     * Double Linked list
-     */
-  struct GNUNET_MESH_TransmitHandle *prev;
-
-    /**
-     * Tunnel this message is sent on / for (may be NULL for control messages).
-     */
-  struct GNUNET_MESH_Tunnel *tunnel;
-
-    /**
-     * Callback to obtain the message to transmit, or NULL if we
-     * got the message in 'data'.  Notice that messages built
-     * by 'notify' need to be encapsulated with information about
-     * the 'target'.
-     */
-  GNUNET_CONNECTION_TransmitReadyNotify notify;
-
-    /**
-     * Closure for 'notify'
-     */
-  void *notify_cls;
-
-    /**
-     * How long is this message valid.  Once the timeout has been
-     * reached, the message must no longer be sent.  If this
-     * is a message with a 'notify' callback set, the 'notify'
-     * function should be called with 'buf' NULL and size 0.
-     */
-  struct GNUNET_TIME_Absolute timeout;
-
-    /**
-     * Task triggering a timeout, can be NO_TASK if the timeout is FOREVER.
-     */
-  GNUNET_SCHEDULER_TaskIdentifier timeout_task;
-
-    /**
-     * Size of 'data' -- or the desired size of 'notify' if 'data' is NULL.
-     */
-  size_t size;
-};
-
-
-/**
- * Opaque handle to the service.
- */
-struct GNUNET_MESH_Handle
-{
-
-    /**
-     * Handle to the server connection, to send messages later
-     */
-  struct GNUNET_CLIENT_Connection *client;
-
-    /**
-     * Set of handlers used for processing incoming messages in the tunnels
-     */
-  const struct GNUNET_MESH_MessageHandler *message_handlers;
-
-  /**
-   * Number of handlers in the handlers array.
-   */
-  unsigned int n_handlers;
-
-  /**
-   * Ports open.
-   */
-  const uint32_t *ports;
-
-  /**
-   * Number of ports.
-   */
-  unsigned int n_ports;
-
-    /**
-     * Double linked list of the tunnels this client is connected to, head.
-     */
-  struct GNUNET_MESH_Tunnel *tunnels_head;
-
-    /**
-     * Double linked list of the tunnels this client is connected to, tail.
-     */
-  struct GNUNET_MESH_Tunnel *tunnels_tail;
-
-    /**
-     * Callback for inbound tunnel creation
-     */
-  GNUNET_MESH_InboundTunnelNotificationHandler *new_tunnel;
-
-    /**
-     * Callback for inbound tunnel disconnection
-     */
-  GNUNET_MESH_TunnelEndHandler *cleaner;
-
-    /**
-     * Handle to cancel pending transmissions in case of disconnection
-     */
-  struct GNUNET_CLIENT_TransmitHandle *th;
-
-    /**
-     * Closure for all the handlers given by the client
-     */
-  void *cls;
-
-    /**
-     * Messages to send to the service, head.
-     */
-  struct GNUNET_MESH_TransmitHandle *th_head;
-
-    /**
-     * Messages to send to the service, tail.
-     */
-  struct GNUNET_MESH_TransmitHandle *th_tail;
-
-    /**
-     * tid of the next tunnel to create (to avoid reusing IDs often)
-     */
-  MESH_TunnelNumber next_tid;
-
-    /**
-     * Have we started the task to receive messages from the service
-     * yet? We do this after we send the 'MESH_LOCAL_CONNECT' message.
-     */
-  int in_receive;
-
-  /**
-   * Configuration given by the client, in case of reconnection
-   */
-  const struct GNUNET_CONFIGURATION_Handle *cfg;
-
-  /**
-   * Time to the next reconnect in case one reconnect fails
-   */
-  struct GNUNET_TIME_Relative reconnect_time;
-
-  /**
-   * Task for trying to reconnect.
-   */
-  GNUNET_SCHEDULER_TaskIdentifier reconnect_task;
-
-  /**
-   * Monitor callback
-   */
-  GNUNET_MESH_TunnelsCB tunnels_cb;
-
-  /**
-   * Monitor callback closure.
-   */
-  void *tunnels_cls;
-
-  /**
-   * Tunnel callback.
-   */
-  GNUNET_MESH_TunnelCB tunnel_cb;
-
-  /**
-   * Tunnel callback closure.
-   */
-  void *tunnel_cls;
-
-#if DEBUG_ACK
-  unsigned int acks_sent;
-  unsigned int acks_recv;
-#endif
-};
-
-
-/**
- * Description of a peer
- */
-struct GNUNET_MESH_Peer
-{
-  /**
-   * ID of the peer in short form
-   */
-  GNUNET_PEER_Id id;
-
-  /**
-   * Tunnel this peer belongs to
-   */
-  struct GNUNET_MESH_Tunnel *t;
-
-  /**
-   * Flag indicating whether service has informed about its connection
-   * FIXME-BART: is this flag used? Seems dead right now...
-   */
-  int connected;
-
-};
-
-
-/**
- * Opaque handle to a tunnel.
- */
-struct GNUNET_MESH_Tunnel
-{
-
-    /**
-     * DLL next
-     */
-  struct GNUNET_MESH_Tunnel *next;
-
-    /**
-     * DLL prev
-     */
-  struct GNUNET_MESH_Tunnel *prev;
-
-    /**
-     * Handle to the mesh this tunnel belongs to
-     */
-  struct GNUNET_MESH_Handle *mesh;
-
-    /**
-     * Local ID of the tunnel
-     */
-  MESH_TunnelNumber tid;
-
-    /**
-     * Port number.
-     */
-  uint32_t port;
-
-    /**
-     * Other end of the tunnel.
-     */
-  GNUNET_PEER_Id peer;
-
-  struct GNUNET_PeerIdentity pid;
-
-  /**
-   * Any data the caller wants to put in here
-   */
-  void *ctx;
-
-    /**
-     * Size of packet queued in this tunnel
-     */
-  unsigned int packet_size;
-
-    /**
-     * Is the tunnel allowed to buffer?
-     */
-  int nobuffer;
-
-    /**
-     * Is the tunnel realiable?
-     */
-  int reliable;
-
-    /**
-     * If reliable, is the tunnel out of order?
-     */
-  int ooorder;
-
-    /**
-     * Are we allowed to send to the service?
-     */
-  int allow_send;
-
-};
-
-
-/**
- * Implementation state for mesh's message queue.
- */
-struct MeshMQState
-{
-  /**
-   * The current transmit handle, or NULL
-   * if no transmit is active.
-   */
-  struct GNUNET_MESH_TransmitHandle *th;
-
-  /**
-   * Tunnel to send the data over.
-   */
-  struct GNUNET_MESH_Tunnel *tunnel;
-};
-
-
-/******************************************************************************/
-/***********************         DECLARATIONS         
*************************/
-/******************************************************************************/
-
-/**
- * Function called to send a message to the service.
- * "buf" will be NULL and "size" zero if the socket was closed for writing in
- * the meantime.
- *
- * @param cls closure, the mesh handle
- * @param size number of bytes available in buf
- * @param buf where the callee should write the connect message
- * @return number of bytes written to buf
- */
-static size_t
-send_callback (void *cls, size_t size, void *buf);
-
-
-/******************************************************************************/
-/***********************     AUXILIARY FUNCTIONS      
*************************/
-/******************************************************************************/
-
-/**
- * Check if transmission is a payload packet.
- *
- * @param th Transmission handle.
- *
- * @return GNUNET_YES if it is a payload packet,
- *         GNUNET_NO if it is a mesh management packet.
- */
-static int
-th_is_payload (struct GNUNET_MESH_TransmitHandle *th)
-{
-  return (th->notify != NULL) ? GNUNET_YES : GNUNET_NO;
-}
-
-
-/**
- * Check whether there is any message ready in the queue and find the size.
- *
- * @param h Mesh handle.
- *
- * @return The size of the first ready message in the queue,
- *         0 if there is none.
- */
-static size_t
-message_ready_size (struct GNUNET_MESH_Handle *h)
-{
-  struct GNUNET_MESH_TransmitHandle *th;
-  struct GNUNET_MESH_Tunnel *t;
-
-  for (th = h->th_head; NULL != th; th = th->next)
-  {
-    t = th->tunnel;
-    if (GNUNET_NO == th_is_payload (th))
-    {
-      LOG (GNUNET_ERROR_TYPE_DEBUG, "#  message internal\n");
-      return th->size;
-    }
-    if (GNUNET_YES == t->allow_send)
-    {
-      LOG (GNUNET_ERROR_TYPE_DEBUG, "#  message payload ok\n");
-      return th->size;
-    }
-  }
-  return 0;
-}
-
-
-/**
- * Get the tunnel handler for the tunnel specified by id from the given handle
- * @param h Mesh handle
- * @param tid ID of the wanted tunnel
- * @return handle to the required tunnel or NULL if not found
- */
-static struct GNUNET_MESH_Tunnel *
-retrieve_tunnel (struct GNUNET_MESH_Handle *h, MESH_TunnelNumber tid)
-{
-  struct GNUNET_MESH_Tunnel *t;
-
-  t = h->tunnels_head;
-  while (t != NULL)
-  {
-    if (t->tid == tid)
-      return t;
-    t = t->next;
-  }
-  return NULL;
-}
-
-
-/**
- * Create a new tunnel and insert it in the tunnel list of the mesh handle
- * @param h Mesh handle
- * @param tid desired tid of the tunnel, 0 to assign one automatically
- * @return handle to the created tunnel
- */
-static struct GNUNET_MESH_Tunnel *
-create_tunnel (struct GNUNET_MESH_Handle *h, MESH_TunnelNumber tid)
-{
-  struct GNUNET_MESH_Tunnel *t;
-
-  t = GNUNET_malloc (sizeof (struct GNUNET_MESH_Tunnel));
-  GNUNET_CONTAINER_DLL_insert (h->tunnels_head, h->tunnels_tail, t);
-  t->mesh = h;
-  if (0 == tid)
-  {
-    t->tid = h->next_tid;
-    while (NULL != retrieve_tunnel (h, h->next_tid))
-    {
-      h->next_tid++;
-      h->next_tid &= ~GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
-      h->next_tid |= GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
-    }
-  }
-  else
-  {
-    t->tid = tid;
-  }
-  t->allow_send = GNUNET_NO;
-  t->nobuffer = GNUNET_NO;
-  return t;
-}
-
-
-/**
- * Destroy the specified tunnel.
- * - Destroys all peers, calling the disconnect callback on each if needed
- * - Cancels all outgoing traffic for that tunnel, calling respective notifys
- * - Calls cleaner if tunnel was inbound
- * - Frees all memory used
- *
- * @param t Pointer to the tunnel.
- * @param call_cleaner Whether to call the cleaner handler.
- *
- * @return Handle to the required tunnel or NULL if not found.
- */
-static void
-destroy_tunnel (struct GNUNET_MESH_Tunnel *t, int call_cleaner)
-{
-  struct GNUNET_MESH_Handle *h;
-  struct GNUNET_MESH_TransmitHandle *th;
-  struct GNUNET_MESH_TransmitHandle *next;
-
-  if (NULL == t)
-  {
-    GNUNET_break (0);
-    return;
-  }
-  LOG (GNUNET_ERROR_TYPE_DEBUG,
-       "destroy_tunnel %X\n",
-       t->tid);
-  h = t->mesh;
-
-  GNUNET_CONTAINER_DLL_remove (h->tunnels_head, h->tunnels_tail, t);
-
-  /* signal tunnel destruction */
-  if ( (NULL != h->cleaner) && (0 != t->peer) && (GNUNET_YES == call_cleaner) )
-    h->cleaner (h->cls, t, t->ctx);
-
-  /* check that clients did not leave messages behind in the queue */
-  for (th = h->th_head; NULL != th; th = next)
-  {
-    next = th->next;
-    if (th->tunnel != t)
-      continue;
-    /* Clients should have aborted their requests already.
-     * Management traffic should be ok, as clients can't cancel that */
-    GNUNET_break (GNUNET_NO == th_is_payload(th));
-    GNUNET_CONTAINER_DLL_remove (h->th_head, h->th_tail, th);
-
-    /* clean up request */
-    if (GNUNET_SCHEDULER_NO_TASK != th->timeout_task)
-      GNUNET_SCHEDULER_cancel (th->timeout_task);
-    GNUNET_free (th);
-  }
-
-  /* if there are no more pending requests with mesh service, cancel active 
request */
-  /* Note: this should be unnecessary... */
-  if ((0 == message_ready_size (h)) && (NULL != h->th))
-  {
-    GNUNET_CLIENT_notify_transmit_ready_cancel (h->th);
-    h->th = NULL;
-  }
-
-  if (0 != t->peer)
-    GNUNET_PEER_change_rc (t->peer, -1);
-  GNUNET_free (t);
-  return;
-}
-
-
-/**
- * Notify client that the transmission has timed out
- *
- * @param cls closure
- * @param tc task context
- */
-static void
-timeout_transmission (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
-{
-  struct GNUNET_MESH_TransmitHandle *th = cls;
-  struct GNUNET_MESH_Handle *mesh;
-
-  mesh = th->tunnel->mesh;
-  GNUNET_CONTAINER_DLL_remove (mesh->th_head, mesh->th_tail, th);
-  th->tunnel->packet_size = 0;
-  if (GNUNET_YES == th_is_payload (th))
-    th->notify (th->notify_cls, 0, NULL);
-  GNUNET_free (th);
-  if ((0 == message_ready_size (mesh)) && (NULL != mesh->th))
-  {
-    /* nothing ready to transmit, no point in asking for transmission */
-    GNUNET_CLIENT_notify_transmit_ready_cancel (mesh->th);
-    mesh->th = NULL;
-  }
-}
-
-
-/**
- * Add a transmit handle to the transmission queue and set the
- * timeout if needed.
- *
- * @param h mesh handle with the queue head and tail
- * @param th handle to the packet to be transmitted
- */
-static void
-add_to_queue (struct GNUNET_MESH_Handle *h,
-              struct GNUNET_MESH_TransmitHandle *th)
-{
-  GNUNET_CONTAINER_DLL_insert_tail (h->th_head, h->th_tail, th);
-  if (GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us == th->timeout.abs_value_us)
-    return;
-  th->timeout_task =
-      GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_absolute_get_remaining
-                                    (th->timeout), &timeout_transmission, th);
-}
-
-
-/**
- * Auxiliary function to send an already constructed packet to the service.
- * Takes care of creating a new queue element, copying the message and
- * calling the tmt_rdy function if necessary.
- *
- * @param h mesh handle
- * @param msg message to transmit
- * @param tunnel tunnel this send is related to (NULL if N/A)
- */
-static void
-send_packet (struct GNUNET_MESH_Handle *h,
-             const struct GNUNET_MessageHeader *msg,
-             struct GNUNET_MESH_Tunnel *tunnel);
-
-
-/**
- * Send an ack on the tunnel to confirm the processing of a message.
- *
- * @param t Tunnel on which to send the ACK.
- */
-static void
-send_ack (struct GNUNET_MESH_Tunnel *t)
-{
-  struct GNUNET_MESH_LocalAck msg;
-
-  LOG (GNUNET_ERROR_TYPE_DEBUG, "Sending ACK on tunnel %X\n", t->tid);
-  msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
-  msg.header.size = htons (sizeof (msg));
-  msg.tunnel_id = htonl (t->tid);
-
-#if DEBUG_ACK
-  t->mesh->acks_sent++;
-#endif
-
-  send_packet (t->mesh, &msg.header, t);
-  return;
-}
-
-
-
-/**
- * Reconnect callback: tries to reconnect again after a failer previous
- * reconnecttion
- * @param cls closure (mesh handle)
- * @param tc task context
- */
-static void
-reconnect_cbk (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
-
-
-/**
- * Send a connect packet to the service with the applications and types
- * requested by the user.
- *
- * @param h The mesh handle.
- *
- */
-static void
-send_connect (struct GNUNET_MESH_Handle *h)
-{
-  size_t size;
-
-  size = sizeof (struct GNUNET_MESH_ClientConnect);
-  size += h->n_ports * sizeof (uint32_t);
-  {
-    char buf[size] GNUNET_ALIGN;
-    struct GNUNET_MESH_ClientConnect *msg;
-    uint32_t *ports;
-    uint16_t i;
-
-    /* build connection packet */
-    msg = (struct GNUNET_MESH_ClientConnect *) buf;
-    msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT);
-    msg->header.size = htons (size);
-    ports = (uint32_t *) &msg[1];
-    for (i = 0; i < h->n_ports; i++)
-    {
-      ports[i] = htonl (h->ports[i]);
-      LOG (GNUNET_ERROR_TYPE_DEBUG, " port %u\n",
-           h->ports[i]);
-    }
-    LOG (GNUNET_ERROR_TYPE_DEBUG,
-         "Sending %lu bytes long message with %u ports\n",
-         ntohs (msg->header.size), h->n_ports);
-    send_packet (h, &msg->header, NULL);
-  }
-}
-
-
-/**
- * Reconnect to the service, retransmit all infomation to try to restore the
- * original state.
- *
- * @param h handle to the mesh
- *
- * @return GNUNET_YES in case of sucess, GNUNET_NO otherwise (service down...)
- */
-static int
-do_reconnect (struct GNUNET_MESH_Handle *h)
-{
-  struct GNUNET_MESH_Tunnel *t;
-
-  LOG (GNUNET_ERROR_TYPE_DEBUG, "*****************************\n");
-  LOG (GNUNET_ERROR_TYPE_DEBUG, "*******   RECONNECT   *******\n");
-  LOG (GNUNET_ERROR_TYPE_DEBUG, "*****************************\n");
-  LOG (GNUNET_ERROR_TYPE_DEBUG, "******** on %p *******\n", h);
-  LOG (GNUNET_ERROR_TYPE_DEBUG, "*****************************\n");
-
-  /* disconnect */
-  if (NULL != h->th)
-  {
-    GNUNET_CLIENT_notify_transmit_ready_cancel (h->th);
-    h->th = NULL;
-  }
-  if (NULL != h->client)
-  {
-    GNUNET_CLIENT_disconnect (h->client);
-  }
-
-  /* connect again */
-  h->client = GNUNET_CLIENT_connect ("mesh", h->cfg);
-  if (h->client == NULL)
-  {
-    h->reconnect_task = GNUNET_SCHEDULER_add_delayed (h->reconnect_time,
-                                                      &reconnect_cbk, h);
-    h->reconnect_time =
-        GNUNET_TIME_relative_min (GNUNET_TIME_UNIT_SECONDS,
-                                  GNUNET_TIME_relative_multiply
-                                  (h->reconnect_time, 2));
-    LOG (GNUNET_ERROR_TYPE_DEBUG,
-        "Next retry in %s\n",
-         GNUNET_STRINGS_relative_time_to_string (h->reconnect_time,
-                                                GNUNET_NO));
-    GNUNET_break (0);
-    return GNUNET_NO;
-  }
-  else
-  {
-    h->reconnect_time = GNUNET_TIME_UNIT_MILLISECONDS;
-  }
-  send_connect (h);
-  /* Rebuild all tunnels */
-  for (t = h->tunnels_head; NULL != t; t = t->next)
-  {
-    struct GNUNET_MESH_TunnelMessage tmsg;
-    uint32_t options;
-
-    if (t->tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
-    {
-      /* Tunnel was created by service (incoming tunnel) */
-      /* TODO: Notify service of missing tunnel, to request
-       * creator to recreate path (find a path to him via DHT?)
-       */
-      continue;
-    }
-    t->allow_send = GNUNET_NO;
-    tmsg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE);
-    tmsg.header.size = htons (sizeof (struct GNUNET_MESH_TunnelMessage));
-    tmsg.tunnel_id = htonl (t->tid);
-    tmsg.port = htonl (t->port);
-    t->pid = tmsg.peer;
-    GNUNET_PEER_resolve (t->peer, &tmsg.peer);
-
-    options = 0;
-    if (GNUNET_YES == t->nobuffer)
-      options |= GNUNET_MESH_OPTION_NOBUFFER;
-
-    if (GNUNET_YES == t->reliable)
-      options |= GNUNET_MESH_OPTION_RELIABLE;
-
-    tmsg.opt = htonl (options);
-    send_packet (h, &tmsg.header, t);
-  }
-  return GNUNET_YES;
-}
-
-/**
- * Reconnect callback: tries to reconnect again after a failer previous
- * reconnecttion
- * @param cls closure (mesh handle)
- * @param tc task context
- */
-static void
-reconnect_cbk (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
-{
-  struct GNUNET_MESH_Handle *h = cls;
-
-  h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
-  if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
-    return;
-  do_reconnect (h);
-}
-
-
-/**
- * Reconnect to the service, retransmit all infomation to try to restore the
- * original state.
- *
- * @param h handle to the mesh
- *
- * @return GNUNET_YES in case of sucess, GNUNET_NO otherwise (service down...)
- */
-static void
-reconnect (struct GNUNET_MESH_Handle *h)
-{
-  LOG (GNUNET_ERROR_TYPE_DEBUG, "Requested RECONNECT\n");
-  h->in_receive = GNUNET_NO;
-  if (GNUNET_SCHEDULER_NO_TASK == h->reconnect_task)
-    h->reconnect_task = GNUNET_SCHEDULER_add_delayed (h->reconnect_time,
-                                                      &reconnect_cbk, h);
-}
-
-
-/******************************************************************************/
-/***********************      RECEIVE HANDLERS     
****************************/
-/******************************************************************************/
-
-/**
- * Process the new tunnel notification and add it to the tunnels in the handle
- *
- * @param h     The mesh handle
- * @param msg   A message with the details of the new incoming tunnel
- */
-static void
-process_tunnel_created (struct GNUNET_MESH_Handle *h,
-                        const struct GNUNET_MESH_TunnelMessage *msg)
-{
-  struct GNUNET_MESH_Tunnel *t;
-  MESH_TunnelNumber tid;
-  uint32_t port;
-
-  tid = ntohl (msg->tunnel_id);
-  port = ntohl (msg->port);
-  LOG (GNUNET_ERROR_TYPE_DEBUG, "Creating incoming tunnel %X:%u\n", tid, port);
-  if (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
-  {
-    GNUNET_break (0);
-    return;
-  }
-  if (NULL != h->new_tunnel)
-  {
-    t = create_tunnel (h, tid);
-    t->allow_send = GNUNET_NO;
-    t->peer = GNUNET_PEER_intern (&msg->peer);
-    t->pid = msg->peer;
-    t->mesh = h;
-    t->tid = tid;
-    t->port = port;
-    if (0 != (msg->opt & GNUNET_MESH_OPTION_NOBUFFER))
-      t->nobuffer = GNUNET_YES;
-    else
-      t->nobuffer = GNUNET_NO;
-    if (0 != (msg->opt & GNUNET_MESH_OPTION_RELIABLE))
-      t->reliable = GNUNET_YES;
-    else
-      t->reliable = GNUNET_NO;
-    if (GNUNET_YES == t->reliable &&
-        0 != (msg->opt & GNUNET_MESH_OPTION_OOORDER))
-      t->ooorder = GNUNET_YES;
-    else
-      t->ooorder = GNUNET_NO;
-    LOG (GNUNET_ERROR_TYPE_DEBUG, "  created tunnel %p\n", t);
-    t->ctx = h->new_tunnel (h->cls, t, &msg->peer, t->port);
-    LOG (GNUNET_ERROR_TYPE_DEBUG, "User notified\n");
-  }
-  else
-  {
-    struct GNUNET_MESH_TunnelMessage d_msg;
-
-    LOG (GNUNET_ERROR_TYPE_DEBUG, "No handler for incoming tunnels\n");
-
-    d_msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
-    d_msg.header.size = htons (sizeof (struct GNUNET_MESH_TunnelMessage));
-    d_msg.tunnel_id = msg->tunnel_id;
-    memset (&d_msg.peer, 0, sizeof (struct GNUNET_PeerIdentity));
-    d_msg.port = 0;
-    d_msg.opt = 0;
-
-    send_packet (h, &d_msg.header, NULL);
-  }
-  return;
-}
-
-
-/**
- * Process the tunnel destroy notification and free associated resources
- *
- * @param h     The mesh handle
- * @param msg   A message with the details of the tunnel being destroyed
- */
-static void
-process_tunnel_destroy (struct GNUNET_MESH_Handle *h,
-                        const struct GNUNET_MESH_TunnelMessage *msg)
-{
-  struct GNUNET_MESH_Tunnel *t;
-  MESH_TunnelNumber tid;
-
-  LOG (GNUNET_ERROR_TYPE_DEBUG, "Destroying tunnel from service\n");
-  tid = ntohl (msg->tunnel_id);
-  t = retrieve_tunnel (h, tid);
-
-  if (NULL == t)
-  {
-    LOG (GNUNET_ERROR_TYPE_DEBUG, "tunnel %X unknown\n", tid);
-    return;
-  }
-  LOG (GNUNET_ERROR_TYPE_DEBUG, "tunnel %X destroyed\n", t->tid);
-  destroy_tunnel (t, GNUNET_YES);
-}
-
-
-/**
- * Process the incoming data packets, call appropriate handlers.
- *
- * @param h         The mesh handle
- * @param message   A message encapsulating the data
- */
-static void
-process_incoming_data (struct GNUNET_MESH_Handle *h,
-                       const struct GNUNET_MessageHeader *message)
-{
-  const struct GNUNET_MessageHeader *payload;
-  const struct GNUNET_MESH_MessageHandler *handler;
-  struct GNUNET_MESH_LocalData *dmsg;
-  struct GNUNET_MESH_Tunnel *t;
-  unsigned int i;
-  uint16_t type;
-
-  LOG (GNUNET_ERROR_TYPE_DEBUG, "Got a data message!\n");
-
-  dmsg = (struct GNUNET_MESH_LocalData *) message;
-
-  t = retrieve_tunnel (h, ntohl (dmsg->tid));
-  payload = (struct GNUNET_MessageHeader *) &dmsg[1];
-  if (NULL == t)
-  {
-    /* Tunnel was ignored/destroyed, probably service didn't get it yet */
-    LOG (GNUNET_ERROR_TYPE_DEBUG, "  ignored!\n");
-    return;
-  }
-  LOG (GNUNET_ERROR_TYPE_DEBUG,
-       "  %s data on tunnel %s [%X]\n",
-       t->tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV ? "fwd" : "bck",
-       GNUNET_i2s (GNUNET_PEER_resolve2(t->peer)),
-       ntohl (dmsg->tid));
-  type = ntohs (payload->type);
-  LOG (GNUNET_ERROR_TYPE_DEBUG, "  payload type %u\n", type);
-  for (i = 0; i < h->n_handlers; i++)
-  {
-    handler = &h->message_handlers[i];
-    LOG (GNUNET_ERROR_TYPE_DEBUG,
-         "    checking handler for type %u\n",
-         handler->type);
-    if (handler->type == type)
-    {
-      if (GNUNET_OK !=
-          handler->callback (h->cls, t, &t->ctx, payload))
-      {
-        LOG (GNUNET_ERROR_TYPE_DEBUG, "callback caused disconnection\n");
-        GNUNET_MESH_tunnel_destroy (t);
-        return;
-      }
-      else
-      {
-        LOG (GNUNET_ERROR_TYPE_DEBUG,
-             "callback completed successfully\n");
-      }
-    }
-  }
-}
-
-
-/**
- * Process a local ACK message, enabling the client to send
- * more data to the service.
- *
- * @param h Mesh handle.
- * @param message Message itself.
- */
-static void
-process_ack (struct GNUNET_MESH_Handle *h,
-             const struct GNUNET_MessageHeader *message)
-{
-  struct GNUNET_MESH_LocalAck *msg;
-  struct GNUNET_MESH_Tunnel *t;
-  MESH_TunnelNumber tid;
-
-  LOG (GNUNET_ERROR_TYPE_DEBUG, "Got an ACK!\n");
-  h->acks_recv++;
-  msg = (struct GNUNET_MESH_LocalAck *) message;
-  tid = ntohl (msg->tunnel_id);
-  t = retrieve_tunnel (h, tid);
-  if (NULL == t)
-  {
-    LOG (GNUNET_ERROR_TYPE_WARNING, "ACK on unknown tunnel %X\n", tid);
-    return;
-  }
-  LOG (GNUNET_ERROR_TYPE_DEBUG, "  on tunnel %X!\n", t->tid);
-  t->allow_send = GNUNET_YES;
-  if (NULL == h->th && 0 < t->packet_size)
-  {
-    LOG (GNUNET_ERROR_TYPE_DEBUG, "  tmt rdy was NULL, requesting!\n");
-    h->th =
-        GNUNET_CLIENT_notify_transmit_ready (h->client, t->packet_size,
-                                             GNUNET_TIME_UNIT_FOREVER_REL,
-                                             GNUNET_YES, &send_callback, h);
-  }
-}
-
-
-/**
- * Process a local reply about info on all tunnels, pass info to the user.
- *
- * @param h Mesh handle.
- * @param message Message itself.
- */
-static void
-process_get_tunnels (struct GNUNET_MESH_Handle *h,
-                     const struct GNUNET_MessageHeader *message)
-{
-  struct GNUNET_MESH_LocalMonitor *msg;
-
-  GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Get Tunnels messasge received\n");
-
-  if (NULL == h->tunnels_cb)
-  {
-    GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "  ignored\n");
-    return;
-  }
-
-  msg = (struct GNUNET_MESH_LocalMonitor *) message;
-  if (ntohs (message->size) !=
-      (sizeof (struct GNUNET_MESH_LocalMonitor) +
-       sizeof (struct GNUNET_PeerIdentity)))
-  {
-    GNUNET_break_op (0);
-    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
-                "Get tunnels message: size %hu - expected %u\n",
-                ntohs (message->size),
-                sizeof (struct GNUNET_MESH_LocalMonitor));
-    return;
-  }
-  h->tunnels_cb (h->tunnels_cls,
-                 ntohl (msg->tunnel_id),
-                 &msg->owner,
-                 &msg->destination);
-}
-
-
-
-/**
- * Process a local monitor_tunnel reply, pass info to the user.
- *
- * @param h Mesh handle.
- * @param message Message itself.
- */
-static void
-process_show_tunnel (struct GNUNET_MESH_Handle *h,
-                     const struct GNUNET_MessageHeader *message)
-{
-  struct GNUNET_MESH_LocalMonitor *msg;
-  size_t esize;
-
-  GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Show Tunnel messasge received\n");
-
-  if (NULL == h->tunnel_cb)
-  {
-    GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "  ignored\n");
-    return;
-  }
-
-  /* Verify message sanity */
-  msg = (struct GNUNET_MESH_LocalMonitor *) message;
-  esize = sizeof (struct GNUNET_MESH_LocalMonitor);
-  if (ntohs (message->size) != esize)
-  {
-    GNUNET_break_op (0);
-    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
-                "Show tunnel message: size %hu - expected %u\n",
-                ntohs (message->size),
-                esize);
-
-    h->tunnel_cb (h->tunnel_cls, NULL, NULL);
-    h->tunnel_cb = NULL;
-    h->tunnel_cls = NULL;
-
-    return;
-  }
-
-  h->tunnel_cb (h->tunnel_cls,
-                &msg->destination,
-                &msg->owner);
-}
-
-
-/**
- * Function to process all messages received from the service
- *
- * @param cls closure
- * @param msg message received, NULL on timeout or fatal error
- */
-static void
-msg_received (void *cls, const struct GNUNET_MessageHeader *msg)
-{
-  struct GNUNET_MESH_Handle *h = cls;
-  uint16_t type;
-
-  if (msg == NULL)
-  {
-    LOG (GNUNET_ERROR_TYPE_DEBUG,
-        "Mesh service disconnected, reconnecting\n", h);
-    reconnect (h);
-    return;
-  }
-  type = ntohs (msg->type);
-  LOG (GNUNET_ERROR_TYPE_DEBUG, "\n");
-  LOG (GNUNET_ERROR_TYPE_DEBUG, "Received a message: %s\n",
-       GNUNET_MESH_DEBUG_M2S (type));
-  switch (type)
-  {
-    /* Notify of a new incoming tunnel */
-  case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE:
-    process_tunnel_created (h, (struct GNUNET_MESH_TunnelMessage *) msg);
-    break;
-    /* Notify of a tunnel disconnection */
-  case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY:
-    process_tunnel_destroy (h, (struct GNUNET_MESH_TunnelMessage *) msg);
-    break;
-    /* Notify of a new data packet in the tunnel */
-  case GNUNET_MESSAGE_TYPE_MESH_LOCAL_DATA:
-    process_incoming_data (h, msg);
-    break;
-  case GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK:
-    process_ack (h, msg);
-    break;
-  case GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS:
-    process_get_tunnels (h, msg);
-    break;
-  case GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNEL:
-    process_show_tunnel (h, msg);
-    break;
-  default:
-    /* We shouldn't get any other packages, log and ignore */
-    LOG (GNUNET_ERROR_TYPE_WARNING,
-         "unsolicited message form service (type %s)\n",
-         GNUNET_MESH_DEBUG_M2S (ntohs (msg->type)));
-  }
-  LOG (GNUNET_ERROR_TYPE_DEBUG, "message processed\n");
-  if (GNUNET_YES == h->in_receive)
-  {
-    GNUNET_CLIENT_receive (h->client, &msg_received, h,
-                           GNUNET_TIME_UNIT_FOREVER_REL);
-  }
-  else
-  {
-    LOG (GNUNET_ERROR_TYPE_DEBUG,
-         "in receive off, not calling CLIENT_receive\n");
-  }
-}
-
-
-/******************************************************************************/
-/************************       SEND FUNCTIONS     
****************************/
-/******************************************************************************/
-
-/**
- * Function called to send a message to the service.
- * "buf" will be NULL and "size" zero if the socket was closed for writing in
- * the meantime.
- *
- * @param cls closure, the mesh handle
- * @param size number of bytes available in buf
- * @param buf where the callee should write the connect message
- * @return number of bytes written to buf
- */
-static size_t
-send_callback (void *cls, size_t size, void *buf)
-{
-  struct GNUNET_MESH_Handle *h = cls;
-  struct GNUNET_MESH_TransmitHandle *th;
-  struct GNUNET_MESH_TransmitHandle *next;
-  struct GNUNET_MESH_Tunnel *t;
-  char *cbuf = buf;
-  size_t tsize;
-  size_t psize;
-  size_t nsize;
-
-  LOG (GNUNET_ERROR_TYPE_DEBUG, "\n");
-  LOG (GNUNET_ERROR_TYPE_DEBUG, "# Send packet() Buffer %u\n", size);
-  if ((0 == size) || (NULL == buf))
-  {
-    LOG (GNUNET_ERROR_TYPE_DEBUG, "# Received NULL send callback on %p\n", h);
-    reconnect (h);
-    h->th = NULL;
-    return 0;
-  }
-  tsize = 0;
-  next = h->th_head;
-  nsize = message_ready_size (h);
-  while ((NULL != (th = next)) && (0 < nsize) && (size >= nsize))
-  {
-    t = th->tunnel;
-    if (GNUNET_YES == th_is_payload (th))
-    {
-      struct GNUNET_MESH_LocalData *dmsg;
-      struct GNUNET_MessageHeader *mh;
-
-      LOG (GNUNET_ERROR_TYPE_DEBUG, "#  payload\n");
-      if (GNUNET_NO == t->allow_send)
-      {
-        /* This tunnel is not ready to transmit yet, try next message */
-        next = th->next;
-        continue;
-      }
-      t->packet_size = 0;
-      GNUNET_assert (size >= th->size);
-      dmsg = (struct GNUNET_MESH_LocalData *) cbuf;
-      mh = (struct GNUNET_MessageHeader *) &dmsg[1];
-      psize = th->notify (th->notify_cls,
-                          size - sizeof (struct GNUNET_MESH_LocalData),
-                          mh);
-      if (psize > 0)
-      {
-        psize += sizeof (struct GNUNET_MESH_LocalData);
-        GNUNET_assert (size >= psize);
-        dmsg->header.size = htons (psize);
-        dmsg->tid = htonl (t->tid);
-        dmsg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_DATA);
-        LOG (GNUNET_ERROR_TYPE_DEBUG, "#  payload type %s\n",
-             GNUNET_MESH_DEBUG_M2S (ntohs (mh->type)));
-        t->allow_send = GNUNET_NO;
-      }
-      else
-      {
-        LOG (GNUNET_ERROR_TYPE_DEBUG,
-             "#  callback returned size 0, "
-             "application canceled transmission\n");
-      }
-    }
-    else
-    {
-      struct GNUNET_MessageHeader *mh = (struct GNUNET_MessageHeader *) &th[1];
-
-      LOG (GNUNET_ERROR_TYPE_DEBUG, "#  mesh internal traffic, type %s\n",
-           GNUNET_MESH_DEBUG_M2S (ntohs (mh->type)));
-      memcpy (cbuf, &th[1], th->size);
-      psize = th->size;
-    }
-    if (th->timeout_task != GNUNET_SCHEDULER_NO_TASK)
-      GNUNET_SCHEDULER_cancel (th->timeout_task);
-    GNUNET_CONTAINER_DLL_remove (h->th_head, h->th_tail, th);
-    GNUNET_free (th);
-    next = h->th_head;
-    nsize = message_ready_size (h);
-    cbuf += psize;
-    size -= psize;
-    tsize += psize;
-  }
-  LOG (GNUNET_ERROR_TYPE_DEBUG, "#  total size: %u\n", tsize);
-  h->th = NULL;
-  size = message_ready_size (h);
-  if (0 != size)
-  {
-    LOG (GNUNET_ERROR_TYPE_DEBUG, "#  next size: %u\n", size);
-    h->th =
-        GNUNET_CLIENT_notify_transmit_ready (h->client, size,
-                                             GNUNET_TIME_UNIT_FOREVER_REL,
-                                             GNUNET_YES, &send_callback, h);
-  }
-  else
-  {
-    if (NULL != h->th_head)
-      LOG (GNUNET_ERROR_TYPE_DEBUG, "#  can't transmit any more\n");
-    else
-      LOG (GNUNET_ERROR_TYPE_DEBUG, "#  nothing left to transmit\n");
-  }
-  if (GNUNET_NO == h->in_receive)
-  {
-    LOG (GNUNET_ERROR_TYPE_DEBUG, "# start receiving from service\n");
-    h->in_receive = GNUNET_YES;
-    GNUNET_CLIENT_receive (h->client, &msg_received, h,
-                           GNUNET_TIME_UNIT_FOREVER_REL);
-  }
-  LOG (GNUNET_ERROR_TYPE_DEBUG, "# Send packet() END\n");
-  return tsize;
-}
-
-
-/**
- * Auxiliary function to send an already constructed packet to the service.
- * Takes care of creating a new queue element, copying the message and
- * calling the tmt_rdy function if necessary.
- *
- * @param h mesh handle
- * @param msg message to transmit
- * @param tunnel tunnel this send is related to (NULL if N/A)
- */
-static void
-send_packet (struct GNUNET_MESH_Handle *h,
-             const struct GNUNET_MessageHeader *msg,
-             struct GNUNET_MESH_Tunnel *tunnel)
-{
-  struct GNUNET_MESH_TransmitHandle *th;
-  size_t msize;
-
-  LOG (GNUNET_ERROR_TYPE_DEBUG, " Sending message to service: %s\n",
-       GNUNET_MESH_DEBUG_M2S(ntohs(msg->type)));
-  msize = ntohs (msg->size);
-  th = GNUNET_malloc (sizeof (struct GNUNET_MESH_TransmitHandle) + msize);
-  th->timeout = GNUNET_TIME_UNIT_FOREVER_ABS;
-  th->size = msize;
-  th->tunnel = tunnel;
-  memcpy (&th[1], msg, msize);
-  add_to_queue (h, th);
-  LOG (GNUNET_ERROR_TYPE_DEBUG, "  queued\n");
-  if (NULL != h->th)
-    return;
-  LOG (GNUNET_ERROR_TYPE_DEBUG, "  calling ntfy tmt rdy for %u bytes\n", 
msize);
-  h->th =
-      GNUNET_CLIENT_notify_transmit_ready (h->client, msize,
-                                           GNUNET_TIME_UNIT_FOREVER_REL,
-                                           GNUNET_YES, &send_callback, h);
-}
-
-
-/******************************************************************************/
-/**********************      API CALL DEFINITIONS     
*************************/
-/******************************************************************************/
-
-struct GNUNET_MESH_Handle *
-GNUNET_MESH_connect (const struct GNUNET_CONFIGURATION_Handle *cfg, void *cls,
-                     GNUNET_MESH_InboundTunnelNotificationHandler new_tunnel,
-                     GNUNET_MESH_TunnelEndHandler cleaner,
-                     const struct GNUNET_MESH_MessageHandler *handlers,
-                     const uint32_t *ports)
-{
-  struct GNUNET_MESH_Handle *h;
-
-  LOG (GNUNET_ERROR_TYPE_DEBUG, "GNUNET_MESH_connect()\n");
-  h = GNUNET_malloc (sizeof (struct GNUNET_MESH_Handle));
-  LOG (GNUNET_ERROR_TYPE_DEBUG, " addr %p\n", h);
-  h->cfg = cfg;
-  h->new_tunnel = new_tunnel;
-  h->cleaner = cleaner;
-  h->client = GNUNET_CLIENT_connect ("mesh", cfg);
-  if (h->client == NULL)
-  {
-    GNUNET_break (0);
-    GNUNET_free (h);
-    return NULL;
-  }
-  h->cls = cls;
-  h->message_handlers = handlers;
-  h->ports = ports;
-  h->next_tid = GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
-  h->reconnect_time = GNUNET_TIME_UNIT_MILLISECONDS;
-  h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
-
-  if (NULL != ports && ports[0] != 0 && NULL == new_tunnel)
-  {
-    GNUNET_break (0);
-    LOG (GNUNET_ERROR_TYPE_DEBUG,
-         "no new tunnel handler given, ports parameter is useless!!\n");
-  }
-  if ((NULL == ports || ports[0] == 0) && NULL != new_tunnel)
-  {
-    GNUNET_break (0);
-    LOG (GNUNET_ERROR_TYPE_DEBUG,
-         "no ports given, new tunnel handler will never be called!!\n");
-  }
-  /* count handlers */
-  for (h->n_handlers = 0;
-       handlers && handlers[h->n_handlers].type;
-       h->n_handlers++) ;
-  for (h->n_ports = 0;
-       ports && ports[h->n_ports];
-       h->n_ports++) ;
-  send_connect (h);
-  LOG (GNUNET_ERROR_TYPE_DEBUG, "GNUNET_MESH_connect() END\n");
-  return h;
-}
-
-
-void
-GNUNET_MESH_disconnect (struct GNUNET_MESH_Handle *handle)
-{
-  struct GNUNET_MESH_Tunnel *t;
-  struct GNUNET_MESH_Tunnel *aux;
-  struct GNUNET_MESH_TransmitHandle *th;
-
-  LOG (GNUNET_ERROR_TYPE_DEBUG, "MESH DISCONNECT\n");
-
-#if DEBUG_ACK
-  LOG (GNUNET_ERROR_TYPE_INFO, "Sent %d ACKs\n", handle->acks_sent);
-  LOG (GNUNET_ERROR_TYPE_INFO, "Recv %d ACKs\n\n", handle->acks_recv);
-#endif
-
-  t = handle->tunnels_head;
-  while (NULL != t)
-  {
-    aux = t->next;
-    if (t->tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
-    {
-      GNUNET_break (0);
-      LOG (GNUNET_ERROR_TYPE_DEBUG, "tunnel %X not destroyed\n", t->tid);
-    }
-    destroy_tunnel (t, GNUNET_YES);
-    t = aux;
-  }
-  while ( (th = handle->th_head) != NULL)
-  {
-    struct GNUNET_MessageHeader *msg;
-
-    /* Make sure it is an allowed packet (everything else should have been
-     * already canceled).
-     */
-    GNUNET_break (GNUNET_NO == th_is_payload (th));
-    msg = (struct GNUNET_MessageHeader *) &th[1];
-    switch (ntohs(msg->type))
-    {
-      case GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT:
-      case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY:
-      case GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS:
-      case GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNEL:
-        break;
-      default:
-        GNUNET_break (0);
-        LOG (GNUNET_ERROR_TYPE_ERROR, "unexpected msg %u\n",
-             ntohs(msg->type));
-    }
-
-    GNUNET_CONTAINER_DLL_remove (handle->th_head, handle->th_tail, th);
-    GNUNET_free (th);
-  }
-
-  if (NULL != handle->th)
-  {
-    GNUNET_CLIENT_notify_transmit_ready_cancel (handle->th);
-    handle->th = NULL;
-  }
-  if (NULL != handle->client)
-  {
-    GNUNET_CLIENT_disconnect (handle->client);
-    handle->client = NULL;
-  }
-  if (GNUNET_SCHEDULER_NO_TASK != handle->reconnect_task)
-  {
-    GNUNET_SCHEDULER_cancel(handle->reconnect_task);
-    handle->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
-  }
-  GNUNET_free (handle);
-}
-
-
-/**
- * Create a new tunnel (we're initiator and will be allowed to add/remove peers
- * and to broadcast).
- *
- * @param h mesh handle
- * @param tunnel_ctx client's tunnel context to associate with the tunnel
- * @param peer peer identity the tunnel should go to
- * @param port Port number.
- * @param nobuffer Flag for disabling buffering on relay nodes.
- * @param reliable Flag for end-to-end reliability.
- *
- * @return handle to the tunnel
- */
-struct GNUNET_MESH_Tunnel *
-GNUNET_MESH_tunnel_create (struct GNUNET_MESH_Handle *h,
-                           void *tunnel_ctx,
-                           const struct GNUNET_PeerIdentity *peer,
-                           uint32_t port,
-                           int nobuffer,
-                           int reliable)
-{
-  struct GNUNET_MESH_Tunnel *t;
-  struct GNUNET_MESH_TunnelMessage msg;
-
-  LOG (GNUNET_ERROR_TYPE_DEBUG,
-       "Creating new tunnel to %s:%u\n",
-       GNUNET_i2s (peer), port);
-  t = create_tunnel (h, 0);
-  LOG (GNUNET_ERROR_TYPE_DEBUG, "  at %p\n", t);
-  LOG (GNUNET_ERROR_TYPE_DEBUG, "  number %X\n", t->tid);
-  t->ctx = tunnel_ctx;
-  t->peer = GNUNET_PEER_intern (peer);
-  t->pid = *peer;
-  msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE);
-  msg.header.size = htons (sizeof (struct GNUNET_MESH_TunnelMessage));
-  msg.tunnel_id = htonl (t->tid);
-  msg.port = htonl (port);
-  msg.peer = *peer;
-  msg.opt = 0;
-  if (GNUNET_YES == reliable)
-    msg.opt |= GNUNET_MESH_OPTION_RELIABLE;
-  if (GNUNET_YES == nobuffer)
-    msg.opt |= GNUNET_MESH_OPTION_NOBUFFER;
-  msg.opt = htonl (msg.opt);
-  t->allow_send = 0;
-  send_packet (h, &msg.header, t);
-  return t;
-}
-
-
-void
-GNUNET_MESH_tunnel_destroy (struct GNUNET_MESH_Tunnel *tunnel)
-{
-  struct GNUNET_MESH_Handle *h;
-  struct GNUNET_MESH_TunnelMessage msg;
-  struct GNUNET_MESH_TransmitHandle *th;
-
-  LOG (GNUNET_ERROR_TYPE_DEBUG, "Destroying tunnel\n");
-  h = tunnel->mesh;
-
-  msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
-  msg.header.size = htons (sizeof (struct GNUNET_MESH_TunnelMessage));
-  msg.tunnel_id = htonl (tunnel->tid);
-  memset (&msg.peer, 0, sizeof (struct GNUNET_PeerIdentity));
-  msg.port = 0;
-  msg.opt = 0;
-  th = h->th_head;
-  while (th != NULL)
-  {
-    struct GNUNET_MESH_TransmitHandle *aux;
-    if (th->tunnel == tunnel)
-    {
-      aux = th->next;
-      /* FIXME call the handler? */
-      if (GNUNET_YES == th_is_payload (th))
-        th->notify (th->notify_cls, 0, NULL);
-      GNUNET_CONTAINER_DLL_remove (h->th_head, h->th_tail, th);
-      GNUNET_free (th);
-      th = aux;
-    }
-    else
-      th = th->next;
-  }
-
-  destroy_tunnel (tunnel, GNUNET_YES);
-  send_packet (h, &msg.header, NULL);
-}
-
-
-/**
- * Get information about a tunnel.
- *
- * @param tunnel Tunnel handle.
- * @param option Query (GNUNET_MESH_OPTION_*).
- * @param ... dependant on option, currently not used
- *
- * @return Union with an answer to the query.
- */
-const union GNUNET_MESH_TunnelInfo *
-GNUNET_MESH_tunnel_get_info (struct GNUNET_MESH_Tunnel *tunnel,
-                             enum MeshTunnelOption option, ...)
-{
-  const union GNUNET_MESH_TunnelInfo *ret;
-
-  switch (option)
-  {
-    case GNUNET_MESH_OPTION_NOBUFFER:
-      ret = (const union GNUNET_MESH_TunnelInfo *) &tunnel->nobuffer;
-      break;
-    case GNUNET_MESH_OPTION_RELIABLE:
-      ret = (const union GNUNET_MESH_TunnelInfo *) &tunnel->reliable;
-      break;
-    case GNUNET_MESH_OPTION_OOORDER:
-      ret = (const union GNUNET_MESH_TunnelInfo *) &tunnel->ooorder;
-      break;
-    case GNUNET_MESH_OPTION_PEER:
-      ret = (const union GNUNET_MESH_TunnelInfo *) &tunnel->pid;
-      break;
-    default:
-      GNUNET_break (0);
-      return NULL;
-  }
-
-  return ret;
-}
-
-struct GNUNET_MESH_TransmitHandle *
-GNUNET_MESH_notify_transmit_ready (struct GNUNET_MESH_Tunnel *tunnel, int cork,
-                                   struct GNUNET_TIME_Relative maxdelay,
-                                   size_t notify_size,
-                                   GNUNET_CONNECTION_TransmitReadyNotify 
notify,
-                                   void *notify_cls)
-{
-  struct GNUNET_MESH_TransmitHandle *th;
-
-  GNUNET_assert (NULL != tunnel);
-  LOG (GNUNET_ERROR_TYPE_DEBUG, "MESH NOTIFY TRANSMIT READY\n");
-  LOG (GNUNET_ERROR_TYPE_DEBUG, "    on tunnel %X\n", tunnel->tid);
-  LOG (GNUNET_ERROR_TYPE_DEBUG, "    allow_send %d\n", tunnel->allow_send);
-  if (tunnel->tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
-    LOG (GNUNET_ERROR_TYPE_DEBUG, "    to origin\n");
-  else
-    LOG (GNUNET_ERROR_TYPE_DEBUG, "    to destination\n");
-  LOG (GNUNET_ERROR_TYPE_DEBUG, "    payload size %u\n", notify_size);
-  GNUNET_assert (NULL != notify);
-  GNUNET_assert (0 == tunnel->packet_size); // Only one data packet allowed
-  th = GNUNET_malloc (sizeof (struct GNUNET_MESH_TransmitHandle));
-  th->tunnel = tunnel;
-  th->timeout = GNUNET_TIME_relative_to_absolute (maxdelay);
-  th->size = notify_size + sizeof (struct GNUNET_MESH_LocalData);
-  tunnel->packet_size = th->size;
-  LOG (GNUNET_ERROR_TYPE_DEBUG, "    total size %u\n", th->size);
-  th->notify = notify;
-  th->notify_cls = notify_cls;
-  add_to_queue (tunnel->mesh, th);
-  if (NULL != tunnel->mesh->th)
-    return th;
-  if (GNUNET_NO == tunnel->allow_send)
-    return th;
-  LOG (GNUNET_ERROR_TYPE_DEBUG, "    call client notify tmt rdy\n");
-  tunnel->mesh->th =
-      GNUNET_CLIENT_notify_transmit_ready (tunnel->mesh->client, th->size,
-                                           GNUNET_TIME_UNIT_FOREVER_REL,
-                                           GNUNET_YES, &send_callback,
-                                           tunnel->mesh);
-  LOG (GNUNET_ERROR_TYPE_DEBUG, "MESH NOTIFY TRANSMIT READY END\n");
-  return th;
-}
-
-
-void
-GNUNET_MESH_notify_transmit_ready_cancel (struct GNUNET_MESH_TransmitHandle 
*th)
-{
-  struct GNUNET_MESH_Handle *mesh;
-
-  th->tunnel->packet_size = 0;
-  mesh = th->tunnel->mesh;
-  if (th->timeout_task != GNUNET_SCHEDULER_NO_TASK)
-    GNUNET_SCHEDULER_cancel (th->timeout_task);
-  GNUNET_CONTAINER_DLL_remove (mesh->th_head, mesh->th_tail, th);
-  GNUNET_free (th);
-  if ((0 == message_ready_size (mesh)) && (NULL != mesh->th))
-  {
-    /* queue empty, no point in asking for transmission */
-    GNUNET_CLIENT_notify_transmit_ready_cancel (mesh->th);
-    mesh->th = NULL;
-  }
-}
-
-void
-GNUNET_MESH_receive_done (struct GNUNET_MESH_Tunnel *tunnel)
-{
-  send_ack (tunnel);
-}
-
-
-/**
- * Request information about the running mesh peer.
- * The callback will be called for every tunnel known to the service,
- * listing all active peers that blong to the tunnel.
- *
- * If called again on the same handle, it will overwrite the previous
- * callback and cls. To retrieve the cls, monitor_cancel must be
- * called first.
- *
- * WARNING: unstable API, likely to change in the future!
- *
- * @param h Handle to the mesh peer.
- * @param callback Function to call with the requested data.
- * @param callback_cls Closure for @c callback.
- */
-void
-GNUNET_MESH_get_tunnels (struct GNUNET_MESH_Handle *h,
-                         GNUNET_MESH_TunnelsCB callback,
-                         void *callback_cls)
-{
-  struct GNUNET_MessageHeader msg;
-
-  msg.size = htons (sizeof (msg));
-  msg.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS);
-  send_packet (h, &msg, NULL);
-  h->tunnels_cb = callback;
-  h->tunnels_cls = callback_cls;
-
-  return;
-}
-
-
-/**
- * Cancel a monitor request. The monitor callback will not be called.
- *
- * @param h Mesh handle.
- *
- * @return Closure given to GNUNET_MESH_monitor, if any.
- */
-void *
-GNUNET_MESH_get_tunnels_cancel (struct GNUNET_MESH_Handle *h)
-{
-  void *cls;
-
-  cls = h->tunnels_cls;
-  h->tunnels_cb = NULL;
-  h->tunnels_cls = NULL;
-  return cls;
-}
-
-
-/**
- * Request information about a specific tunnel of the running mesh peer.
- *
- * WARNING: unstable API, likely to change in the future!
- * FIXME Add destination option.
- *
- * @param h Handle to the mesh peer.
- * @param initiator ID of the owner of the tunnel.
- * @param tunnel_number Tunnel number.
- * @param callback Function to call with the requested data.
- * @param callback_cls Closure for @c callback.
- */
-void
-GNUNET_MESH_show_tunnel (struct GNUNET_MESH_Handle *h,
-                         struct GNUNET_PeerIdentity *initiator,
-                         unsigned int tunnel_number,
-                         GNUNET_MESH_TunnelCB callback,
-                         void *callback_cls)
-{
-  struct GNUNET_MESH_LocalMonitor msg;
-
-  msg.header.size = htons (sizeof (msg));
-  msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNEL);
-  msg.owner = *initiator;
-  msg.tunnel_id = htonl (tunnel_number);
-  msg.reserved = 0;
-  send_packet (h, &msg.header, NULL);
-  h->tunnel_cb = callback;
-  h->tunnel_cls = callback_cls;
-
-  return;
-}
-
-
-/**
- * Function called to notify a client about the connection
- * begin ready to queue more data.  "buf" will be
- * NULL and "size" zero if the connection was closed for
- * writing in the meantime.
- *
- * @param cls closure
- * @param size number of bytes available in buf
- * @param buf where the callee should write the message
- * @return number of bytes written to buf
- */
-static size_t
-mesh_mq_ntr (void *cls, size_t size,
-             void *buf)
-{
-  struct GNUNET_MQ_Handle *mq = cls;
-  struct MeshMQState *state = GNUNET_MQ_impl_state (mq);
-  const struct GNUNET_MessageHeader *msg = GNUNET_MQ_impl_current (mq);
-  uint16_t msize;
-
-  GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "mesh-mq",
-                   "writing message (t: %u, s: %u) to buffer\n",
-                   ntohs (msg->type), ntohs (msg->size));
-
-  state->th = NULL;
-  if (NULL == buf)
-  {
-    GNUNET_MQ_inject_error (mq, GNUNET_MQ_ERROR_WRITE);
-    return 0;
-  }
-  msize = ntohs (msg->size);
-  GNUNET_assert (msize <= size);
-  memcpy (buf, msg, msize);
-  GNUNET_MQ_impl_send_continue (mq);
-  return msize;
-}
-
-
-/**
- * Signature of functions implementing the
- * sending functionality of a message queue.
- *
- * @param mq the message queue
- * @param msg the message to send
- * @param impl_state state of the implementation
- */
-static void
-mesh_mq_send_impl (struct GNUNET_MQ_Handle *mq,
-                   const struct GNUNET_MessageHeader *msg, void *impl_state)
-{
-  struct MeshMQState *state = impl_state;
-
-  GNUNET_assert (NULL == state->th);
-  GNUNET_MQ_impl_send_commit (mq);
-  GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "mesh-mq",
-                   "calling ntr for message (t: %u, s: %u)\n",
-                   ntohs (msg->type), ntohs (msg->size));
-  state->th =
-      GNUNET_MESH_notify_transmit_ready (state->tunnel,
-                                         /* FIXME: add option for corking */
-                                         GNUNET_NO,
-                                         GNUNET_TIME_UNIT_FOREVER_REL,
-                                         ntohs (msg->size),
-                                         mesh_mq_ntr, mq);
-
-}
-
-
-/**
- * Signature of functions implementing the
- * destruction of a message queue.
- * Implementations must not free 'mq', but should
- * take care of 'impl_state'.
- *
- * @param mq the message queue to destroy
- * @param impl_state state of the implementation
- */
-static void
-mesh_mq_destroy_impl (struct GNUNET_MQ_Handle *mq, void *impl_state)
-{
-  struct MeshMQState *state = impl_state;
-
-  if (NULL != state->th)
-    GNUNET_MESH_notify_transmit_ready_cancel (state->th);
-
-  GNUNET_free (state);
-}
-
-
-/**
- * Create a message queue for a mesh tunnel.
- * The message queue can only be used to transmit messages,
- * not to receive them.
- *
- * @param tunnel the tunnel to create the message qeue for
- * @return a message queue to messages over the tunnel
- */
-struct GNUNET_MQ_Handle *
-GNUNET_MESH_mq_create (struct GNUNET_MESH_Tunnel *tunnel)
-{
-  struct GNUNET_MQ_Handle *mq;
-  struct MeshMQState *state;
-
-  state = GNUNET_new (struct MeshMQState);
-  state->tunnel = tunnel;
-
-  mq = GNUNET_MQ_queue_for_callbacks (mesh_mq_send_impl,
-                                      mesh_mq_destroy_impl,
-                                      NULL, /* FIXME: cancel impl. */
-                                      state,
-                                      NULL, /* no msg handlers */
-                                      NULL, /* no err handlers */
-                                      NULL); /* no handler cls */
-  return mq;
-}
-

Copied: gnunet/src/mesh/mesh_api.c (from rev 30671, 
gnunet/src/mesh/mesh_api_enc.c)
===================================================================
--- gnunet/src/mesh/mesh_api.c                          (rev 0)
+++ gnunet/src/mesh/mesh_api.c  2013-11-14 13:55:12 UTC (rev 30697)
@@ -0,0 +1,1794 @@
+/*
+     This file is part of GNUnet.
+     (C) 2011 Christian Grothoff (and other contributing authors)
+     GNUnet is free software; you can redistribute it and/or modify
+     it under the terms of the GNU General Public License as published
+     by the Free Software Foundation; either version 3, or (at your
+     option) any later version.
+     GNUnet is distributed in the hope that it will be useful, but
+     WITHOUT ANY WARRANTY; without even the implied warranty of
+     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+     General Public License for more details.
+     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.
+*/
+
+/**
+ * @file mesh/mesh_api_enc.c
+ * @brief mesh api: client implementation of new mesh service
+ * @author Bartlomiej Polot
+ */
+
+#include "platform.h"
+#include "gnunet_util_lib.h"
+#include "gnunet_mesh_service.h"
+#include "mesh.h"
+#include "mesh_protocol.h"
+
+#define LOG(kind,...) GNUNET_log_from (kind, "mesh-api",__VA_ARGS__)
+
+/******************************************************************************/
+/************************      DATA STRUCTURES     
****************************/
+/******************************************************************************/
+
+/**
+ * Transmission queue to the service
+ */
+struct GNUNET_MESH_TransmitHandle
+{
+
+    /**
+     * Double Linked list
+     */
+  struct GNUNET_MESH_TransmitHandle *next;
+
+    /**
+     * Double Linked list
+     */
+  struct GNUNET_MESH_TransmitHandle *prev;
+
+    /**
+     * Channel this message is sent on / for (may be NULL for control 
messages).
+     */
+  struct GNUNET_MESH_Channel *channel;
+
+    /**
+     * Callback to obtain the message to transmit, or NULL if we
+     * got the message in 'data'.  Notice that messages built
+     * by 'notify' need to be encapsulated with information about
+     * the 'target'.
+     */
+  GNUNET_CONNECTION_TransmitReadyNotify notify;
+
+    /**
+     * Closure for 'notify'
+     */
+  void *notify_cls;
+
+    /**
+     * How long is this message valid.  Once the timeout has been
+     * reached, the message must no longer be sent.  If this
+     * is a message with a 'notify' callback set, the 'notify'
+     * function should be called with 'buf' NULL and size 0.
+     */
+  struct GNUNET_TIME_Absolute timeout;
+
+    /**
+     * Task triggering a timeout, can be NO_TASK if the timeout is FOREVER.
+     */
+  GNUNET_SCHEDULER_TaskIdentifier timeout_task;
+
+    /**
+     * Size of 'data' -- or the desired size of 'notify' if 'data' is NULL.
+     */
+  size_t size;
+};
+
+
+/**
+ * Opaque handle to the service.
+ */
+struct GNUNET_MESH_Handle
+{
+
+    /**
+     * Handle to the server connection, to send messages later
+     */
+  struct GNUNET_CLIENT_Connection *client;
+
+    /**
+     * Set of handlers used for processing incoming messages in the channels
+     */
+  const struct GNUNET_MESH_MessageHandler *message_handlers;
+
+  /**
+   * Number of handlers in the handlers array.
+   */
+  unsigned int n_handlers;
+
+  /**
+   * Ports open.
+   */
+  const uint32_t *ports;
+
+  /**
+   * Number of ports.
+   */
+  unsigned int n_ports;
+
+    /**
+     * Double linked list of the channels this client is connected to, head.
+     */
+  struct GNUNET_MESH_Channel *channels_head;
+
+    /**
+     * Double linked list of the channels this client is connected to, tail.
+     */
+  struct GNUNET_MESH_Channel *channels_tail;
+
+    /**
+     * Callback for inbound channel creation
+     */
+  GNUNET_MESH_InboundChannelNotificationHandler *new_channel;
+
+    /**
+     * Callback for inbound channel disconnection
+     */
+  GNUNET_MESH_ChannelEndHandler *cleaner;
+
+    /**
+     * Handle to cancel pending transmissions in case of disconnection
+     */
+  struct GNUNET_CLIENT_TransmitHandle *th;
+
+    /**
+     * Closure for all the handlers given by the client
+     */
+  void *cls;
+
+    /**
+     * Messages to send to the service, head.
+     */
+  struct GNUNET_MESH_TransmitHandle *th_head;
+
+    /**
+     * Messages to send to the service, tail.
+     */
+  struct GNUNET_MESH_TransmitHandle *th_tail;
+
+    /**
+     * chid of the next channel to create (to avoid reusing IDs often)
+     */
+  MESH_ChannelNumber next_chid;
+
+    /**
+     * Have we started the task to receive messages from the service
+     * yet? We do this after we send the 'MESH_LOCAL_CONNECT' message.
+     */
+  int in_receive;
+
+  /**
+   * Configuration given by the client, in case of reconnection
+   */
+  const struct GNUNET_CONFIGURATION_Handle *cfg;
+
+  /**
+   * Time to the next reconnect in case one reconnect fails
+   */
+  struct GNUNET_TIME_Relative reconnect_time;
+
+  /**
+   * Task for trying to reconnect.
+   */
+  GNUNET_SCHEDULER_TaskIdentifier reconnect_task;
+
+  /**
+   * Monitor callback
+   */
+  GNUNET_MESH_ChannelsCB channels_cb;
+
+  /**
+   * Monitor callback closure.
+   */
+  void *channels_cls;
+
+  /**
+   * Channel callback.
+   */
+  GNUNET_MESH_ChannelCB channel_cb;
+
+  /**
+   * Channel callback closure.
+   */
+  void *channel_cls;
+};
+
+
+/**
+ * Description of a peer
+ */
+struct GNUNET_MESH_Peer
+{
+    /**
+     * ID of the peer in short form
+     */
+  GNUNET_PEER_Id id;
+
+  /**
+   * Channel this peer belongs to
+   */
+  struct GNUNET_MESH_Channel *t;
+
+  /**
+   * Flag indicating whether service has informed about its connection
+   * FIXME-BART: is this flag used? Seems dead right now...
+   */
+  int connected;
+
+};
+
+
+/**
+ * Opaque handle to a channel.
+ */
+struct GNUNET_MESH_Channel
+{
+
+    /**
+     * DLL next
+     */
+  struct GNUNET_MESH_Channel *next;
+
+    /**
+     * DLL prev
+     */
+  struct GNUNET_MESH_Channel *prev;
+
+    /**
+     * Handle to the mesh this channel belongs to
+     */
+  struct GNUNET_MESH_Handle *mesh;
+
+    /**
+     * Local ID of the channel
+     */
+  MESH_ChannelNumber chid;
+
+    /**
+     * Port number.
+     */
+  uint32_t port;
+
+    /**
+     * Other end of the channel.
+     */
+  GNUNET_PEER_Id peer;
+
+  /**
+   * Any data the caller wants to put in here
+   */
+  void *ctx;
+
+    /**
+     * Size of packet queued in this channel
+     */
+  unsigned int packet_size;
+
+    /**
+     * Is the channel allowed to buffer?
+     */
+  int nobuffer;
+
+    /**
+     * Is the channel realiable?
+     */
+  int reliable;
+
+    /**
+     * If reliable, is the channel out of order?
+     */
+  int ooorder;
+
+    /**
+     * Are we allowed to send to the service?
+     */
+  int allow_send;
+
+};
+
+
+/**
+ * Implementation state for mesh's message queue.
+ */
+struct MeshMQState
+{
+  /**
+   * The current transmit handle, or NULL
+   * if no transmit is active.
+   */
+  struct GNUNET_MESH_TransmitHandle *th;
+
+  /**
+   * Channel to send the data over.
+   */
+  struct GNUNET_MESH_Channel *channel;
+};
+
+
+/******************************************************************************/
+/***********************         DECLARATIONS         
*************************/
+/******************************************************************************/
+
+/**
+ * Function called to send a message to the service.
+ * "buf" will be NULL and "size" zero if the socket was closed for writing in
+ * the meantime.
+ *
+ * @param cls closure, the mesh handle
+ * @param size number of bytes available in buf
+ * @param buf where the callee should write the connect message
+ * @return number of bytes written to buf
+ */
+static size_t
+send_callback (void *cls, size_t size, void *buf);
+
+
+/******************************************************************************/
+/***********************     AUXILIARY FUNCTIONS      
*************************/
+/******************************************************************************/
+
+/**
+ * Check if transmission is a payload packet.
+ *
+ * @param th Transmission handle.
+ *
+ * @return GNUNET_YES if it is a payload packet,
+ *         GNUNET_NO if it is a mesh management packet.
+ */
+static int
+th_is_payload (struct GNUNET_MESH_TransmitHandle *th)
+{
+  return (th->notify != NULL) ? GNUNET_YES : GNUNET_NO;
+}
+
+
+/**
+ * Check whether there is any message ready in the queue and find the size.
+ *
+ * @param h Mesh handle.
+ *
+ * @return The size of the first ready message in the queue,
+ *         0 if there is none.
+ */
+static size_t
+message_ready_size (struct GNUNET_MESH_Handle *h)
+{
+  struct GNUNET_MESH_TransmitHandle *th;
+  struct GNUNET_MESH_Channel *ch;
+
+  for (th = h->th_head; NULL != th; th = th->next)
+  {
+    ch = th->channel;
+    if (GNUNET_NO == th_is_payload (th))
+    {
+      LOG (GNUNET_ERROR_TYPE_DEBUG, "#  message internal\n");
+      return th->size;
+    }
+    if (GNUNET_YES == ch->allow_send)
+    {
+      LOG (GNUNET_ERROR_TYPE_DEBUG, "#  message payload ok\n");
+      return th->size;
+    }
+  }
+  return 0;
+}
+
+
+/**
+ * Get the channel handler for the channel specified by id from the given 
handle
+ * @param h Mesh handle
+ * @param chid ID of the wanted channel
+ * @return handle to the required channel or NULL if not found
+ */
+static struct GNUNET_MESH_Channel *
+retrieve_channel (struct GNUNET_MESH_Handle *h, MESH_ChannelNumber chid)
+{
+  struct GNUNET_MESH_Channel *ch;
+
+  ch = h->channels_head;
+  while (ch != NULL)
+  {
+    if (ch->chid == chid)
+      return ch;
+    ch = ch->next;
+  }
+  return NULL;
+}
+
+
+/**
+ * Create a new channel and insert it in the channel list of the mesh handle
+ *
+ * @param h Mesh handle
+ * @param chid Desired chid of the channel, 0 to assign one automatically.
+ *
+ * @return Handle to the created channel.
+ */
+static struct GNUNET_MESH_Channel *
+create_channel (struct GNUNET_MESH_Handle *h, MESH_ChannelNumber chid)
+{
+  struct GNUNET_MESH_Channel *ch;
+
+  ch = GNUNET_malloc (sizeof (struct GNUNET_MESH_Channel));
+  GNUNET_CONTAINER_DLL_insert (h->channels_head, h->channels_tail, ch);
+  ch->mesh = h;
+  if (0 == chid)
+  {
+    ch->chid = h->next_chid;
+    while (NULL != retrieve_channel (h, h->next_chid))
+    {
+      h->next_chid++;
+      h->next_chid &= ~GNUNET_MESH_LOCAL_CHANNEL_ID_SERV;
+      h->next_chid |= GNUNET_MESH_LOCAL_CHANNEL_ID_CLI;
+    }
+  }
+  else
+  {
+    ch->chid = chid;
+  }
+  ch->allow_send = GNUNET_NO;
+  ch->nobuffer = GNUNET_NO;
+  return ch;
+}
+
+
+/**
+ * Destroy the specified channel.
+ * - Destroys all peers, calling the disconnect callback on each if needed
+ * - Cancels all outgoing traffic for that channel, calling respective notifys
+ * - Calls cleaner if channel was inbound
+ * - Frees all memory used
+ *
+ * @param ch Pointer to the channel.
+ * @param call_cleaner Whether to call the cleaner handler.
+ *
+ * @return Handle to the required channel or NULL if not found.
+ */
+static void
+destroy_channel (struct GNUNET_MESH_Channel *ch, int call_cleaner)
+{
+  struct GNUNET_MESH_Handle *h;
+  struct GNUNET_MESH_TransmitHandle *th;
+  struct GNUNET_MESH_TransmitHandle *next;
+
+  LOG (GNUNET_ERROR_TYPE_DEBUG, "destroy_channel %X\n", ch->chid);
+
+  if (NULL == ch)
+  {
+    GNUNET_break (0);
+    return;
+  }
+  h = ch->mesh;
+
+  GNUNET_CONTAINER_DLL_remove (h->channels_head, h->channels_tail, ch);
+
+  /* signal channel destruction */
+  if ( (NULL != h->cleaner) && (0 != ch->peer) && (GNUNET_YES == call_cleaner) 
)
+    h->cleaner (h->cls, ch, ch->ctx);
+
+  /* check that clients did not leave messages behind in the queue */
+  for (th = h->th_head; NULL != th; th = next)
+  {
+    next = th->next;
+    if (th->channel != ch)
+      continue;
+    /* Clients should have aborted their requests already.
+     * Management traffic should be ok, as clients can't cancel that */
+    GNUNET_break (GNUNET_NO == th_is_payload (th));
+    GNUNET_CONTAINER_DLL_remove (h->th_head, h->th_tail, th);
+
+    /* clean up request */
+    if (GNUNET_SCHEDULER_NO_TASK != th->timeout_task)
+      GNUNET_SCHEDULER_cancel (th->timeout_task);
+    GNUNET_free (th);
+  }
+
+  /* if there are no more pending requests with mesh service, cancel active 
request */
+  /* Note: this should be unnecessary... */
+  if ((0 == message_ready_size (h)) && (NULL != h->th))
+  {
+    GNUNET_CLIENT_notify_transmit_ready_cancel (h->th);
+    h->th = NULL;
+  }
+
+  if (0 != ch->peer)
+    GNUNET_PEER_change_rc (ch->peer, -1);
+  GNUNET_free (ch);
+  return;
+}
+
+
+/**
+ * Notify client that the transmission has timed out
+ *
+ * @param cls closure
+ * @param tc task context
+ */
+static void
+timeout_transmission (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
+{
+  struct GNUNET_MESH_TransmitHandle *th = cls;
+  struct GNUNET_MESH_Handle *mesh;
+
+  mesh = th->channel->mesh;
+  GNUNET_CONTAINER_DLL_remove (mesh->th_head, mesh->th_tail, th);
+  th->channel->packet_size = 0;
+  if (GNUNET_YES == th_is_payload (th))
+    th->notify (th->notify_cls, 0, NULL);
+  GNUNET_free (th);
+  if ((0 == message_ready_size (mesh)) && (NULL != mesh->th))
+  {
+    /* nothing ready to transmit, no point in asking for transmission */
+    GNUNET_CLIENT_notify_transmit_ready_cancel (mesh->th);
+    mesh->th = NULL;
+  }
+}
+
+
+/**
+ * Add a transmit handle to the transmission queue and set the
+ * timeout if needed.
+ *
+ * @param h mesh handle with the queue head and tail
+ * @param th handle to the packet to be transmitted
+ */
+static void
+add_to_queue (struct GNUNET_MESH_Handle *h,
+              struct GNUNET_MESH_TransmitHandle *th)
+{
+  GNUNET_CONTAINER_DLL_insert_tail (h->th_head, h->th_tail, th);
+  if (GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us == th->timeout.abs_value_us)
+    return;
+  th->timeout_task =
+      GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_absolute_get_remaining
+                                    (th->timeout), &timeout_transmission, th);
+}
+
+
+/**
+ * Auxiliary function to send an already constructed packet to the service.
+ * Takes care of creating a new queue element, copying the message and
+ * calling the tmt_rdy function if necessary.
+ *
+ * @param h mesh handle
+ * @param msg message to transmit
+ * @param channel channel this send is related to (NULL if N/A)
+ */
+static void
+send_packet (struct GNUNET_MESH_Handle *h,
+             const struct GNUNET_MessageHeader *msg,
+             struct GNUNET_MESH_Channel *channel);
+
+
+/**
+ * Send an ack on the channel to confirm the processing of a message.
+ *
+ * @param ch Channel on which to send the ACK.
+ */
+static void
+send_ack (struct GNUNET_MESH_Channel *ch)
+{
+  struct GNUNET_MESH_LocalAck msg;
+
+  LOG (GNUNET_ERROR_TYPE_DEBUG, "Sending ACK on channel %X\n", ch->chid);
+  msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
+  msg.header.size = htons (sizeof (msg));
+  msg.channel_id = htonl (ch->chid);
+
+  send_packet (ch->mesh, &msg.header, ch);
+  return;
+}
+
+
+
+/**
+ * Reconnect callback: tries to reconnect again after a failer previous
+ * reconnecttion
+ * @param cls closure (mesh handle)
+ * @param tc task context
+ */
+static void
+reconnect_cbk (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
+
+
+/**
+ * Send a connect packet to the service with the applications and types
+ * requested by the user.
+ *
+ * @param h The mesh handle.
+ *
+ */
+static void
+send_connect (struct GNUNET_MESH_Handle *h)
+{
+  size_t size;
+
+  size = sizeof (struct GNUNET_MESH_ClientConnect);
+  size += h->n_ports * sizeof (uint32_t);
+  {
+    char buf[size] GNUNET_ALIGN;
+    struct GNUNET_MESH_ClientConnect *msg;
+    uint32_t *ports;
+    uint16_t i;
+
+    /* build connection packet */
+    msg = (struct GNUNET_MESH_ClientConnect *) buf;
+    msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT);
+    msg->header.size = htons (size);
+    ports = (uint32_t *) &msg[1];
+    for (i = 0; i < h->n_ports; i++)
+    {
+      ports[i] = htonl (h->ports[i]);
+      LOG (GNUNET_ERROR_TYPE_DEBUG, " port %u\n",
+           h->ports[i]);
+    }
+    LOG (GNUNET_ERROR_TYPE_DEBUG,
+         "Sending %lu bytes long message with %u ports\n",
+         ntohs (msg->header.size), h->n_ports);
+    send_packet (h, &msg->header, NULL);
+  }
+}
+
+
+/**
+ * Reconnect to the service, retransmit all infomation to try to restore the
+ * original state.
+ *
+ * @param h handle to the mesh
+ *
+ * @return GNUNET_YES in case of sucess, GNUNET_NO otherwise (service down...)
+ */
+static int
+do_reconnect (struct GNUNET_MESH_Handle *h)
+{
+  struct GNUNET_MESH_Channel *ch;
+
+  LOG (GNUNET_ERROR_TYPE_DEBUG, "*****************************\n");
+  LOG (GNUNET_ERROR_TYPE_DEBUG, "*******   RECONNECT   *******\n");
+  LOG (GNUNET_ERROR_TYPE_DEBUG, "*****************************\n");
+  LOG (GNUNET_ERROR_TYPE_DEBUG, "******** on %p *******\n", h);
+  LOG (GNUNET_ERROR_TYPE_DEBUG, "*****************************\n");
+
+  /* disconnect */
+  if (NULL != h->th)
+  {
+    GNUNET_CLIENT_notify_transmit_ready_cancel (h->th);
+    h->th = NULL;
+  }
+  if (NULL != h->client)
+  {
+    GNUNET_CLIENT_disconnect (h->client);
+  }
+
+  /* connect again */
+  h->client = GNUNET_CLIENT_connect ("mesh", h->cfg);
+  if (h->client == NULL)
+  {
+    h->reconnect_task = GNUNET_SCHEDULER_add_delayed (h->reconnect_time,
+                                                      &reconnect_cbk, h);
+    h->reconnect_time =
+        GNUNET_TIME_relative_min (GNUNET_TIME_UNIT_SECONDS,
+                                  GNUNET_TIME_relative_multiply
+                                  (h->reconnect_time, 2));
+    LOG (GNUNET_ERROR_TYPE_DEBUG, "Next retry in %s\n",
+         GNUNET_STRINGS_relative_time_to_string (h->reconnect_time,
+                                                 GNUNET_NO));
+    GNUNET_break (0);
+    return GNUNET_NO;
+  }
+  else
+  {
+    h->reconnect_time = GNUNET_TIME_UNIT_MILLISECONDS;
+  }
+  send_connect (h);
+  /* Rebuild all channels */
+  for (ch = h->channels_head; NULL != ch; ch = ch->next)
+  {
+    struct GNUNET_MESH_ChannelMessage tmsg;
+    uint32_t options;
+
+    if (ch->chid >= GNUNET_MESH_LOCAL_CHANNEL_ID_SERV)
+    {
+      /* Channel was created by service (incoming channel) */
+      /* TODO: Notify service of missing channel, to request
+       * creator to recreate path (find a path to him via DHT?)
+       */
+      continue;
+    }
+    ch->allow_send = GNUNET_NO;
+    tmsg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE);
+    tmsg.header.size = htons (sizeof (struct GNUNET_MESH_ChannelMessage));
+    tmsg.channel_id = htonl (ch->chid);
+    tmsg.port = htonl (ch->port);
+    GNUNET_PEER_resolve (ch->peer, &tmsg.peer);
+
+    options = 0;
+    if (GNUNET_YES == ch->nobuffer)
+      options |= GNUNET_MESH_OPTION_NOBUFFER;
+
+    if (GNUNET_YES == ch->reliable)
+      options |= GNUNET_MESH_OPTION_RELIABLE;
+
+    tmsg.opt = htonl (options);
+    send_packet (h, &tmsg.header, ch);
+  }
+  return GNUNET_YES;
+}
+
+/**
+ * Reconnect callback: tries to reconnect again after a failer previous
+ * reconnecttion
+ * @param cls closure (mesh handle)
+ * @param tc task context
+ */
+static void
+reconnect_cbk (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
+{
+  struct GNUNET_MESH_Handle *h = cls;
+
+  h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
+  if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
+    return;
+  do_reconnect (h);
+}
+
+
+/**
+ * Reconnect to the service, retransmit all infomation to try to restore the
+ * original state.
+ *
+ * @param h handle to the mesh
+ *
+ * @return GNUNET_YES in case of sucess, GNUNET_NO otherwise (service down...)
+ */
+static void
+reconnect (struct GNUNET_MESH_Handle *h)
+{
+  LOG (GNUNET_ERROR_TYPE_DEBUG, "Requested RECONNECT\n");
+  h->in_receive = GNUNET_NO;
+  if (GNUNET_SCHEDULER_NO_TASK == h->reconnect_task)
+    h->reconnect_task = GNUNET_SCHEDULER_add_delayed (h->reconnect_time,
+                                                      &reconnect_cbk, h);
+}
+
+
+/******************************************************************************/
+/***********************      RECEIVE HANDLERS     
****************************/
+/******************************************************************************/
+
+/**
+ * Process the new channel notification and add it to the channels in the 
handle
+ *
+ * @param h     The mesh handle
+ * @param msg   A message with the details of the new incoming channel
+ */
+static void
+process_channel_created (struct GNUNET_MESH_Handle *h,
+                        const struct GNUNET_MESH_ChannelMessage *msg)
+{
+  struct GNUNET_MESH_Channel *ch;
+  MESH_ChannelNumber chid;
+  uint32_t port;
+
+  chid = ntohl (msg->channel_id);
+  port = ntohl (msg->port);
+  LOG (GNUNET_ERROR_TYPE_DEBUG, "Creating incoming channel %X:%u\n", chid, 
port);
+  if (chid < GNUNET_MESH_LOCAL_CHANNEL_ID_SERV)
+  {
+    GNUNET_break (0);
+    return;
+  }
+  if (NULL != h->new_channel)
+  {
+    ch = create_channel (h, chid);
+    ch->allow_send = GNUNET_NO;
+    ch->peer = GNUNET_PEER_intern (&msg->peer);
+    ch->mesh = h;
+    ch->chid = chid;
+    ch->port = port;
+    if (0 != (msg->opt & GNUNET_MESH_OPTION_NOBUFFER))
+      ch->nobuffer = GNUNET_YES;
+    else
+      ch->nobuffer = GNUNET_NO;
+
+    if (0 != (msg->opt & GNUNET_MESH_OPTION_RELIABLE))
+      ch->reliable = GNUNET_YES;
+    else
+      ch->reliable = GNUNET_NO;
+
+    if (GNUNET_YES == ch->reliable &&
+        0 != (msg->opt & GNUNET_MESH_OPTION_OOORDER))
+      ch->ooorder = GNUNET_YES;
+    else
+      ch->ooorder = GNUNET_NO;
+
+    LOG (GNUNET_ERROR_TYPE_DEBUG, "  created channel %p\n", ch);
+    ch->ctx = h->new_channel (h->cls, ch, &msg->peer, ch->port);
+    LOG (GNUNET_ERROR_TYPE_DEBUG, "User notified\n");
+  }
+  else
+  {
+    struct GNUNET_MESH_ChannelMessage d_msg;
+
+    LOG (GNUNET_ERROR_TYPE_DEBUG, "No handler for incoming channels\n");
+
+    d_msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY);
+    d_msg.header.size = htons (sizeof (struct GNUNET_MESH_ChannelMessage));
+    d_msg.channel_id = msg->channel_id;
+    memset (&d_msg.peer, 0, sizeof (struct GNUNET_PeerIdentity));
+    d_msg.port = 0;
+    d_msg.opt = 0;
+
+    send_packet (h, &d_msg.header, NULL);
+  }
+  return;
+}
+
+
+/**
+ * Process the channel destroy notification and free associated resources
+ *
+ * @param h     The mesh handle
+ * @param msg   A message with the details of the channel being destroyed
+ */
+static void
+process_channel_destroy (struct GNUNET_MESH_Handle *h,
+                         const struct GNUNET_MESH_ChannelMessage *msg)
+{
+  struct GNUNET_MESH_Channel *ch;
+  MESH_ChannelNumber chid;
+
+  LOG (GNUNET_ERROR_TYPE_DEBUG, "Destroying channel from service\n");
+  chid = ntohl (msg->channel_id);
+  ch = retrieve_channel (h, chid);
+
+  if (NULL == ch)
+  {
+    LOG (GNUNET_ERROR_TYPE_DEBUG, "channel %X unknown\n", chid);
+    return;
+  }
+  LOG (GNUNET_ERROR_TYPE_DEBUG, "channel %X destroyed\n", ch->chid);
+  destroy_channel (ch, GNUNET_YES);
+}
+
+
+/**
+ * Process the incoming data packets, call appropriate handlers.
+ *
+ * @param h         The mesh handle
+ * @param message   A message encapsulating the data
+ */
+static void
+process_incoming_data (struct GNUNET_MESH_Handle *h,
+                       const struct GNUNET_MessageHeader *message)
+{
+  const struct GNUNET_MessageHeader *payload;
+  const struct GNUNET_MESH_MessageHandler *handler;
+  struct GNUNET_MESH_LocalData *dmsg;
+  struct GNUNET_MESH_Channel *ch;
+  unsigned int i;
+  uint16_t type;
+
+  LOG (GNUNET_ERROR_TYPE_DEBUG, "Got a data message!\n");
+
+  dmsg = (struct GNUNET_MESH_LocalData *) message;
+
+  ch = retrieve_channel (h, ntohl (dmsg->id));
+  payload = (struct GNUNET_MessageHeader *) &dmsg[1];
+  LOG (GNUNET_ERROR_TYPE_DEBUG, "  %s data on channel %s [%X]\n",
+       ch->chid >= GNUNET_MESH_LOCAL_CHANNEL_ID_SERV ? "fwd" : "bck",
+       GNUNET_i2s (GNUNET_PEER_resolve2 (ch->peer)), ntohl (dmsg->id));
+  if (NULL == ch)
+  {
+    /* Channel was ignored/destroyed, probably service didn't get it yet */
+    LOG (GNUNET_ERROR_TYPE_DEBUG, "  ignored!\n");
+    return;
+  }
+  type = ntohs (payload->type);
+  LOG (GNUNET_ERROR_TYPE_DEBUG, "  payload type %u\n", type);
+  for (i = 0; i < h->n_handlers; i++)
+  {
+    handler = &h->message_handlers[i];
+    LOG (GNUNET_ERROR_TYPE_DEBUG,
+         "    checking handler for type %u\n",
+         handler->type);
+    if (handler->type == type)
+    {
+      if (GNUNET_OK !=
+          handler->callback (h->cls, ch, &ch->ctx, payload))
+      {
+        LOG (GNUNET_ERROR_TYPE_DEBUG, "callback caused disconnection\n");
+        GNUNET_MESH_channel_destroy (ch);
+        return;
+      }
+      else
+      {
+        LOG (GNUNET_ERROR_TYPE_DEBUG,
+             "callback completed successfully\n");
+      }
+    }
+  }
+}
+
+
+/**
+ * Process a local ACK message, enabling the client to send
+ * more data to the service.
+ *
+ * @param h Mesh handle.
+ * @param message Message itself.
+ */
+static void
+process_ack (struct GNUNET_MESH_Handle *h,
+             const struct GNUNET_MessageHeader *message)
+{
+  struct GNUNET_MESH_LocalAck *msg;
+  struct GNUNET_MESH_Channel *ch;
+  MESH_ChannelNumber chid;
+
+  LOG (GNUNET_ERROR_TYPE_DEBUG, "Got an ACK!\n");
+  msg = (struct GNUNET_MESH_LocalAck *) message;
+  chid = ntohl (msg->channel_id);
+    ch = retrieve_channel (h, chid);
+  if (NULL == ch)
+  {
+    LOG (GNUNET_ERROR_TYPE_WARNING, "ACK on unknown channel %X\n", chid);
+    return;
+  }
+  LOG (GNUNET_ERROR_TYPE_DEBUG, "  on channel %X!\n", ch->chid);
+  ch->allow_send = GNUNET_YES;
+  if (NULL == h->th && 0 < ch->packet_size)
+  {
+    LOG (GNUNET_ERROR_TYPE_DEBUG, "  tmt rdy was NULL, requesting!\n");
+    h->th =
+        GNUNET_CLIENT_notify_transmit_ready (h->client, ch->packet_size,
+                                             GNUNET_TIME_UNIT_FOREVER_REL,
+                                             GNUNET_YES, &send_callback, h);
+  }
+}
+
+
+/*
+ * Process a local reply about info on all channels, pass info to the user.
+ *
+ * @param h Mesh handle.
+ * @param message Message itself.
+ */
+// static void
+// process_get_channels (struct GNUNET_MESH_Handle *h,
+//                      const struct GNUNET_MessageHeader *message)
+// {
+//   struct GNUNET_MESH_LocalMonitor *msg;
+// 
+//   GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Get Channels messasge received\n");
+// 
+//   if (NULL == h->channels_cb)
+//   {
+//     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "  ignored\n");
+//     return;
+//   }
+// 
+//   msg = (struct GNUNET_MESH_LocalMonitor *) message;
+//   if (ntohs (message->size) !=
+//       (sizeof (struct GNUNET_MESH_LocalMonitor) +
+//        sizeof (struct GNUNET_PeerIdentity)))
+//   {
+//     GNUNET_break_op (0);
+//     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+//                 "Get channels message: size %hu - expected %u\n",
+//                 ntohs (message->size),
+//                 sizeof (struct GNUNET_MESH_LocalMonitor));
+//     return;
+//   }
+//   h->channels_cb (h->channels_cls,
+//                   ntohl (msg->channel_id),
+//                   &msg->owner,
+//                   &msg->destination);
+// }
+
+
+
+/*
+ * Process a local monitor_channel reply, pass info to the user.
+ *
+ * @param h Mesh handle.
+ * @param message Message itself.
+ */
+// static void
+// process_show_channel (struct GNUNET_MESH_Handle *h,
+//                      const struct GNUNET_MessageHeader *message)
+// {
+//   struct GNUNET_MESH_LocalMonitor *msg;
+//   size_t esize;
+// 
+//   GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Show Channel messasge received\n");
+// 
+//   if (NULL == h->channel_cb)
+//   {
+//     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "  ignored\n");
+//     return;
+//   }
+// 
+//   /* Verify message sanity */
+//   msg = (struct GNUNET_MESH_LocalMonitor *) message;
+//   esize = sizeof (struct GNUNET_MESH_LocalMonitor);
+//   if (ntohs (message->size) != esize)
+//   {
+//     GNUNET_break_op (0);
+//     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+//                 "Show channel message: size %hu - expected %u\n",
+//                 ntohs (message->size),
+//                 esize);
+// 
+//     h->channel_cb (h->channel_cls, NULL, NULL);
+//     h->channel_cb = NULL;
+//     h->channel_cls = NULL;
+// 
+//     return;
+//   }
+// 
+//   h->channel_cb (h->channel_cls,
+//                  &msg->destination,
+//                  &msg->owner);
+// }
+
+
+/**
+ * Function to process all messages received from the service
+ *
+ * @param cls closure
+ * @param msg message received, NULL on timeout or fatal error
+ */
+static void
+msg_received (void *cls, const struct GNUNET_MessageHeader *msg)
+{
+  struct GNUNET_MESH_Handle *h = cls;
+  uint16_t type;
+
+  if (msg == NULL)
+  {
+    LOG (GNUNET_ERROR_TYPE_DEBUG,
+        "Mesh service disconnected, reconnecting\n", h);
+    reconnect (h);
+    return;
+  }
+  type = ntohs (msg->type);
+  LOG (GNUNET_ERROR_TYPE_DEBUG, "\n");
+  LOG (GNUNET_ERROR_TYPE_DEBUG, "Received a message: %s\n",
+       GNUNET_MESH_DEBUG_M2S (type));
+  switch (type)
+  {
+    /* Notify of a new incoming channel */
+  case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE:
+    process_channel_created (h, (struct GNUNET_MESH_ChannelMessage *) msg);
+    break;
+    /* Notify of a channel disconnection */
+  case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY:
+  case GNUNET_MESSAGE_TYPE_MESH_LOCAL_NACK:
+    process_channel_destroy (h, (struct GNUNET_MESH_ChannelMessage *) msg);
+    break;
+  case GNUNET_MESSAGE_TYPE_MESH_LOCAL_DATA:
+    process_incoming_data (h, msg);
+    break;
+  case GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK:
+    process_ack (h, msg);
+    break;
+//   case GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_CHANNELS: DEPRECATED
+//     process_get_channels (h, msg);
+//     break;
+//   case GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_CHANNEL: DEPRECATED
+//     process_show_channel (h, msg);
+//     break;
+  default:
+    /* We shouldn't get any other packages, log and ignore */
+    LOG (GNUNET_ERROR_TYPE_WARNING,
+         "unsolicited message form service (type %s)\n",
+         GNUNET_MESH_DEBUG_M2S (ntohs (msg->type)));
+  }
+  LOG (GNUNET_ERROR_TYPE_DEBUG, "message processed\n");
+  if (GNUNET_YES == h->in_receive)
+  {
+    GNUNET_CLIENT_receive (h->client, &msg_received, h,
+                           GNUNET_TIME_UNIT_FOREVER_REL);
+  }
+  else
+  {
+    LOG (GNUNET_ERROR_TYPE_DEBUG,
+         "in receive off, not calling CLIENT_receive\n");
+  }
+}
+
+
+/******************************************************************************/
+/************************       SEND FUNCTIONS     
****************************/
+/******************************************************************************/
+
+/**
+ * Function called to send a message to the service.
+ * "buf" will be NULL and "size" zero if the socket was closed for writing in
+ * the meantime.
+ *
+ * @param cls closure, the mesh handle
+ * @param size number of bytes available in buf
+ * @param buf where the callee should write the connect message
+ * @return number of bytes written to buf
+ */
+static size_t
+send_callback (void *cls, size_t size, void *buf)
+{
+  struct GNUNET_MESH_Handle *h = cls;
+  struct GNUNET_MESH_TransmitHandle *th;
+  struct GNUNET_MESH_TransmitHandle *next;
+  struct GNUNET_MESH_Channel *ch;
+  char *cbuf = buf;
+  size_t tsize;
+  size_t psize;
+  size_t nsize;
+
+  LOG (GNUNET_ERROR_TYPE_DEBUG, "\n");
+  LOG (GNUNET_ERROR_TYPE_DEBUG, "# Send packet() Buffer %u\n", size);
+  if ((0 == size) || (NULL == buf))
+  {
+    LOG (GNUNET_ERROR_TYPE_DEBUG, "# Received NULL send callback on %p\n", h);
+    reconnect (h);
+    h->th = NULL;
+    return 0;
+  }
+  tsize = 0;
+  next = h->th_head;
+  nsize = message_ready_size (h);
+  while ((NULL != (th = next)) && (0 < nsize) && (size >= nsize))
+  {
+    ch = th->channel;
+    if (GNUNET_YES == th_is_payload (th))
+    {
+      struct GNUNET_MESH_LocalData *dmsg;
+      struct GNUNET_MessageHeader *mh;
+
+      LOG (GNUNET_ERROR_TYPE_DEBUG, "#  payload\n");
+      if (GNUNET_NO == ch->allow_send)
+      {
+        /* This channel is not ready to transmit yet, try next message */
+        next = th->next;
+        continue;
+      }
+      ch->packet_size = 0;
+      GNUNET_assert (size >= th->size);
+      dmsg = (struct GNUNET_MESH_LocalData *) cbuf;
+      mh = (struct GNUNET_MessageHeader *) &dmsg[1];
+      psize = th->notify (th->notify_cls,
+                          size - sizeof (struct GNUNET_MESH_LocalData),
+                          mh);
+      if (psize > 0)
+      {
+        psize += sizeof (struct GNUNET_MESH_LocalData);
+        GNUNET_assert (size >= psize);
+        dmsg->header.size = htons (psize);
+        dmsg->id = htonl (ch->chid);
+        dmsg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_DATA);
+        LOG (GNUNET_ERROR_TYPE_DEBUG, "#  payload type %s\n",
+             GNUNET_MESH_DEBUG_M2S (ntohs (mh->type)));
+                ch->allow_send = GNUNET_NO;
+      }
+      else
+      {
+        LOG (GNUNET_ERROR_TYPE_DEBUG,
+             "#  callback returned size 0, "
+             "application canceled transmission\n");
+      }
+    }
+    else
+    {
+      struct GNUNET_MessageHeader *mh = (struct GNUNET_MessageHeader *) &th[1];
+
+      LOG (GNUNET_ERROR_TYPE_DEBUG, "#  mesh internal traffic, type %s\n",
+           GNUNET_MESH_DEBUG_M2S (ntohs (mh->type)));
+      memcpy (cbuf, &th[1], th->size);
+      psize = th->size;
+    }
+    if (th->timeout_task != GNUNET_SCHEDULER_NO_TASK)
+      GNUNET_SCHEDULER_cancel (th->timeout_task);
+    GNUNET_CONTAINER_DLL_remove (h->th_head, h->th_tail, th);
+    GNUNET_free (th);
+    next = h->th_head;
+    nsize = message_ready_size (h);
+    cbuf += psize;
+    size -= psize;
+    tsize += psize;
+  }
+  LOG (GNUNET_ERROR_TYPE_DEBUG, "#  total size: %u\n", tsize);
+  h->th = NULL;
+  size = message_ready_size (h);
+  if (0 != size)
+  {
+    LOG (GNUNET_ERROR_TYPE_DEBUG, "#  next size: %u\n", size);
+    h->th =
+        GNUNET_CLIENT_notify_transmit_ready (h->client, size,
+                                             GNUNET_TIME_UNIT_FOREVER_REL,
+                                             GNUNET_YES, &send_callback, h);
+  }
+  else
+  {
+    if (NULL != h->th_head)
+      LOG (GNUNET_ERROR_TYPE_DEBUG, "#  can't transmit any more\n");
+    else
+      LOG (GNUNET_ERROR_TYPE_DEBUG, "#  nothing left to transmit\n");
+  }
+  if (GNUNET_NO == h->in_receive)
+  {
+    LOG (GNUNET_ERROR_TYPE_DEBUG, "# start receiving from service\n");
+    h->in_receive = GNUNET_YES;
+    GNUNET_CLIENT_receive (h->client, &msg_received, h,
+                           GNUNET_TIME_UNIT_FOREVER_REL);
+  }
+  LOG (GNUNET_ERROR_TYPE_DEBUG, "# Send packet() END\n");
+  return tsize;
+}
+
+
+/**
+ * Auxiliary function to send an already constructed packet to the service.
+ * Takes care of creating a new queue element, copying the message and
+ * calling the tmt_rdy function if necessary.
+ *
+ * @param h mesh handle
+ * @param msg message to transmit
+ * @param channel channel this send is related to (NULL if N/A)
+ */
+static void
+send_packet (struct GNUNET_MESH_Handle *h,
+             const struct GNUNET_MessageHeader *msg,
+             struct GNUNET_MESH_Channel *channel)
+{
+  struct GNUNET_MESH_TransmitHandle *th;
+  size_t msize;
+
+  LOG (GNUNET_ERROR_TYPE_DEBUG, " Sending message to service: %s\n",
+       GNUNET_MESH_DEBUG_M2S(ntohs(msg->type)));
+  msize = ntohs (msg->size);
+  th = GNUNET_malloc (sizeof (struct GNUNET_MESH_TransmitHandle) + msize);
+  th->timeout = GNUNET_TIME_UNIT_FOREVER_ABS;
+  th->size = msize;
+  th->channel = channel;
+  memcpy (&th[1], msg, msize);
+  add_to_queue (h, th);
+  LOG (GNUNET_ERROR_TYPE_DEBUG, "  queued\n");
+  if (NULL != h->th)
+    return;
+  LOG (GNUNET_ERROR_TYPE_DEBUG, "  calling ntfy tmt rdy for %u bytes\n", 
msize);
+  h->th =
+      GNUNET_CLIENT_notify_transmit_ready (h->client, msize,
+                                           GNUNET_TIME_UNIT_FOREVER_REL,
+                                           GNUNET_YES, &send_callback, h);
+}
+
+
+/******************************************************************************/
+/**********************      API CALL DEFINITIONS     
*************************/
+/******************************************************************************/
+
+struct GNUNET_MESH_Handle *
+GNUNET_MESH_connect (const struct GNUNET_CONFIGURATION_Handle *cfg, void *cls,
+                     GNUNET_MESH_InboundChannelNotificationHandler new_channel,
+                     GNUNET_MESH_ChannelEndHandler cleaner,
+                     const struct GNUNET_MESH_MessageHandler *handlers,
+                     const uint32_t *ports)
+{
+  struct GNUNET_MESH_Handle *h;
+
+  LOG (GNUNET_ERROR_TYPE_DEBUG, "GNUNET_MESH_connect()\n");
+  h = GNUNET_malloc (sizeof (struct GNUNET_MESH_Handle));
+  LOG (GNUNET_ERROR_TYPE_DEBUG, " addr %p\n", h);
+  h->cfg = cfg;
+  h->new_channel = new_channel;
+  h->cleaner = cleaner;
+  h->client = GNUNET_CLIENT_connect ("mesh", cfg);
+  if (h->client == NULL)
+  {
+    GNUNET_break (0);
+    GNUNET_free (h);
+    return NULL;
+  }
+  h->cls = cls;
+  h->message_handlers = handlers;
+  h->ports = ports;
+  h->next_chid = GNUNET_MESH_LOCAL_CHANNEL_ID_CLI;
+  h->reconnect_time = GNUNET_TIME_UNIT_MILLISECONDS;
+  h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
+
+  if (NULL != ports && ports[0] != 0 && NULL == new_channel)
+  {
+    GNUNET_break (0);
+    LOG (GNUNET_ERROR_TYPE_DEBUG,
+         "no new channel handler given, ports parameter is useless!!\n");
+  }
+  if ((NULL == ports || ports[0] == 0) && NULL != new_channel)
+  {
+    GNUNET_break (0);
+    LOG (GNUNET_ERROR_TYPE_DEBUG,
+         "no ports given, new channel handler will never be called!!\n");
+  }
+  /* count handlers */
+  for (h->n_handlers = 0;
+       handlers && handlers[h->n_handlers].type;
+       h->n_handlers++) ;
+  for (h->n_ports = 0;
+       ports && ports[h->n_ports];
+       h->n_ports++) ;
+  send_connect (h);
+  LOG (GNUNET_ERROR_TYPE_DEBUG, "GNUNET_MESH_connect() END\n");
+  return h;
+}
+
+
+void
+GNUNET_MESH_disconnect (struct GNUNET_MESH_Handle *handle)
+{
+  struct GNUNET_MESH_Channel *ch;
+  struct GNUNET_MESH_Channel *aux;
+  struct GNUNET_MESH_TransmitHandle *th;
+
+  LOG (GNUNET_ERROR_TYPE_DEBUG, "MESH DISCONNECT\n");
+
+  ch = handle->channels_head;
+  while (NULL != ch)
+  {
+    aux = ch->next;
+    if (ch->chid < GNUNET_MESH_LOCAL_CHANNEL_ID_SERV)
+    {
+      GNUNET_break (0);
+      LOG (GNUNET_ERROR_TYPE_DEBUG, "channel %X not destroyed\n", ch->chid);
+    }
+    destroy_channel (ch, GNUNET_YES);
+    ch = aux;
+  }
+  while ( (th = handle->th_head) != NULL)
+  {
+    struct GNUNET_MessageHeader *msg;
+
+    /* Make sure it is an allowed packet (everything else should have been
+     * already canceled).
+     */
+    GNUNET_break (GNUNET_NO == th_is_payload (th));
+    msg = (struct GNUNET_MessageHeader *) &th[1];
+    switch (ntohs(msg->type))
+    {
+      case GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT:
+      case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE:
+      case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY:
+      case GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_CHANNELS:
+      case GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_CHANNEL:
+        break;
+      default:
+        GNUNET_break (0);
+        LOG (GNUNET_ERROR_TYPE_ERROR, "unexpected msg %u\n",
+             ntohs(msg->type));
+    }
+
+    GNUNET_CONTAINER_DLL_remove (handle->th_head, handle->th_tail, th);
+    GNUNET_free (th);
+  }
+
+  if (NULL != handle->th)
+  {
+    GNUNET_CLIENT_notify_transmit_ready_cancel (handle->th);
+    handle->th = NULL;
+  }
+  if (NULL != handle->client)
+  {
+    GNUNET_CLIENT_disconnect (handle->client);
+    handle->client = NULL;
+  }
+  if (GNUNET_SCHEDULER_NO_TASK != handle->reconnect_task)
+  {
+    GNUNET_SCHEDULER_cancel(handle->reconnect_task);
+    handle->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
+  }
+  GNUNET_free (handle);
+}
+
+
+/**
+ * Create a new channel towards a remote peer.
+ *
+ * If the destination port is not open by any peer or the destination peer
+ * does not accept the channel, #GNUNET_MESH_ChannelEndHandler will be called
+ * for this channel.
+ *
+ * @param h mesh handle
+ * @param channel_ctx client's channel context to associate with the channel
+ * @param peer peer identity the channel should go to
+ * @param port Port number.
+ * @param nobuffer Flag for disabling buffering on relay nodes.
+ * @param reliable Flag for end-to-end reliability.
+ *
+ * @return handle to the channel
+ */
+struct GNUNET_MESH_Channel *
+GNUNET_MESH_channel_create (struct GNUNET_MESH_Handle *h,
+                           void *channel_ctx,
+                           const struct GNUNET_PeerIdentity *peer,
+                           uint32_t port,
+                           int nobuffer,
+                           int reliable)
+{
+  struct GNUNET_MESH_Channel *ch;
+  struct GNUNET_MESH_ChannelMessage msg;
+
+  LOG (GNUNET_ERROR_TYPE_DEBUG,
+       "Creating new channel to %s:%u\n",
+       GNUNET_i2s (peer), port);
+  ch = create_channel (h, 0);
+  LOG (GNUNET_ERROR_TYPE_DEBUG, "  at %p\n", ch);
+  LOG (GNUNET_ERROR_TYPE_DEBUG, "  number %X\n", ch->chid);
+  ch->ctx = channel_ctx;
+  ch->peer = GNUNET_PEER_intern (peer);
+  msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE);
+  msg.header.size = htons (sizeof (struct GNUNET_MESH_ChannelMessage));
+  msg.channel_id = htonl (ch->chid);
+  msg.port = htonl (port);
+  msg.peer = *peer;
+  msg.opt = 0;
+  if (GNUNET_YES == reliable)
+    msg.opt |= GNUNET_MESH_OPTION_RELIABLE;
+  if (GNUNET_YES == nobuffer)
+    msg.opt |= GNUNET_MESH_OPTION_NOBUFFER;
+  msg.opt = htonl (msg.opt);
+  ch->allow_send = 0;
+  send_packet (h, &msg.header, ch);
+  return ch;
+}
+
+
+void
+GNUNET_MESH_channel_destroy (struct GNUNET_MESH_Channel *channel)
+{
+  struct GNUNET_MESH_Handle *h;
+  struct GNUNET_MESH_ChannelMessage msg;
+  struct GNUNET_MESH_TransmitHandle *th;
+
+  LOG (GNUNET_ERROR_TYPE_DEBUG, "Destroying channel\n");
+  h = channel->mesh;
+
+  msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY);
+  msg.header.size = htons (sizeof (struct GNUNET_MESH_ChannelMessage));
+  msg.channel_id = htonl (channel->chid);
+  memset (&msg.peer, 0, sizeof (struct GNUNET_PeerIdentity));
+  msg.port = 0;
+  msg.opt = 0;
+  th = h->th_head;
+  while (th != NULL)
+  {
+    struct GNUNET_MESH_TransmitHandle *aux;
+    if (th->channel == channel)
+    {
+      aux = th->next;
+      /* FIXME call the handler? */
+      if (GNUNET_YES == th_is_payload (th))
+        th->notify (th->notify_cls, 0, NULL);
+      GNUNET_CONTAINER_DLL_remove (h->th_head, h->th_tail, th);
+      GNUNET_free (th);
+      th = aux;
+    }
+    else
+      th = th->next;
+  }
+
+  destroy_channel (channel, GNUNET_YES);
+  send_packet (h, &msg.header, NULL);
+}
+
+
+/**
+ * Get information about a channel.
+ *
+ * @param channel Channel handle.
+ * @param option Query (GNUNET_MESH_OPTION_*).
+ * @param ... dependant on option, currently not used
+ *
+ * @return Union with an answer to the query.
+ */
+const union GNUNET_MESH_ChannelInfo *
+GNUNET_MESH_channel_get_info (struct GNUNET_MESH_Channel *channel,
+                             enum MeshOption option, ...)
+{
+  const union GNUNET_MESH_ChannelInfo *ret;
+
+  switch (option)
+  {
+    case GNUNET_MESH_OPTION_NOBUFFER:
+      ret = (const union GNUNET_MESH_ChannelInfo *) &channel->nobuffer;
+      break;
+    case GNUNET_MESH_OPTION_RELIABLE:
+      ret = (const union GNUNET_MESH_ChannelInfo *) &channel->reliable;
+      break;
+    case GNUNET_MESH_OPTION_OOORDER:
+      ret = (const union GNUNET_MESH_ChannelInfo *) &channel->ooorder;
+      break;
+    case GNUNET_MESH_OPTION_PEER:
+      ret = (const union GNUNET_MESH_ChannelInfo *) &channel->peer;
+      break;
+    default:
+      GNUNET_break (0);
+      return NULL;
+  }
+
+  return ret;
+}
+
+struct GNUNET_MESH_TransmitHandle *
+GNUNET_MESH_notify_transmit_ready (struct GNUNET_MESH_Channel *channel, int 
cork,
+                                   struct GNUNET_TIME_Relative maxdelay,
+                                   size_t notify_size,
+                                   GNUNET_CONNECTION_TransmitReadyNotify 
notify,
+                                   void *notify_cls)
+{
+  struct GNUNET_MESH_TransmitHandle *th;
+
+  GNUNET_assert (NULL != channel);
+  LOG (GNUNET_ERROR_TYPE_DEBUG, "MESH NOTIFY TRANSMIT READY\n");
+  LOG (GNUNET_ERROR_TYPE_DEBUG, "    on channel %X\n", channel->chid);
+  LOG (GNUNET_ERROR_TYPE_DEBUG, "    allow_send %d\n", channel->allow_send);
+  if (channel->chid >= GNUNET_MESH_LOCAL_CHANNEL_ID_SERV)
+    LOG (GNUNET_ERROR_TYPE_DEBUG, "    to origin\n");
+  else
+    LOG (GNUNET_ERROR_TYPE_DEBUG, "    to destination\n");
+  LOG (GNUNET_ERROR_TYPE_DEBUG, "    payload size %u\n", notify_size);
+  GNUNET_assert (NULL != notify);
+  GNUNET_assert (0 == channel->packet_size); // Only one data packet allowed
+  th = GNUNET_malloc (sizeof (struct GNUNET_MESH_TransmitHandle));
+  th->channel = channel;
+  th->timeout = GNUNET_TIME_relative_to_absolute (maxdelay);
+  th->size = notify_size + sizeof (struct GNUNET_MESH_LocalData);
+  channel->packet_size = th->size;
+  LOG (GNUNET_ERROR_TYPE_DEBUG, "    total size %u\n", th->size);
+  th->notify = notify;
+  th->notify_cls = notify_cls;
+  add_to_queue (channel->mesh, th);
+  if (NULL != channel->mesh->th)
+    return th;
+  if (GNUNET_NO == channel->allow_send)
+    return th;
+  LOG (GNUNET_ERROR_TYPE_DEBUG, "    call client notify tmt rdy\n");
+  channel->mesh->th =
+      GNUNET_CLIENT_notify_transmit_ready (channel->mesh->client, th->size,
+                                           GNUNET_TIME_UNIT_FOREVER_REL,
+                                           GNUNET_YES, &send_callback,
+                                           channel->mesh);
+  LOG (GNUNET_ERROR_TYPE_DEBUG, "MESH NOTIFY TRANSMIT READY END\n");
+  return th;
+}
+
+
+void
+GNUNET_MESH_notify_transmit_ready_cancel (struct GNUNET_MESH_TransmitHandle 
*th)
+{
+  struct GNUNET_MESH_Handle *mesh;
+
+  th->channel->packet_size = 0;
+  mesh = th->channel->mesh;
+  if (th->timeout_task != GNUNET_SCHEDULER_NO_TASK)
+    GNUNET_SCHEDULER_cancel (th->timeout_task);
+  GNUNET_CONTAINER_DLL_remove (mesh->th_head, mesh->th_tail, th);
+  GNUNET_free (th);
+  if ((0 == message_ready_size (mesh)) && (NULL != mesh->th))
+  {
+    /* queue empty, no point in asking for transmission */
+    GNUNET_CLIENT_notify_transmit_ready_cancel (mesh->th);
+    mesh->th = NULL;
+  }
+}
+
+void
+GNUNET_MESH_receive_done (struct GNUNET_MESH_Channel *channel)
+{
+  send_ack (channel);
+}
+
+
+/**
+ * Request information about the running mesh peer.
+ * The callback will be called for every channel known to the service,
+ * listing all active peers that blong to the channel.
+ *
+ * If called again on the same handle, it will overwrite the previous
+ * callback and cls. To retrieve the cls, monitor_cancel must be
+ * called first.
+ *
+ * WARNING: unstable API, likely to change in the future!
+ *
+ * @param h Handle to the mesh peer.
+ * @param callback Function to call with the requested data.
+ * @param callback_cls Closure for @c callback.
+ */
+void
+GNUNET_MESH_get_channels (struct GNUNET_MESH_Handle *h,
+                         GNUNET_MESH_ChannelsCB callback,
+                         void *callback_cls)
+{
+  struct GNUNET_MessageHeader msg;
+
+  msg.size = htons (sizeof (msg));
+  msg.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_CHANNELS);
+  send_packet (h, &msg, NULL);
+  h->channels_cb = callback;
+  h->channels_cls = callback_cls;
+
+  return;
+}
+
+
+/**
+ * Cancel a monitor request. The monitor callback will not be called.
+ *
+ * @param h Mesh handle.
+ *
+ * @return Closure given to GNUNET_MESH_monitor, if any.
+ */
+void *
+GNUNET_MESH_get_channels_cancel (struct GNUNET_MESH_Handle *h)
+{
+  void *cls;
+
+  cls = h->channels_cls;
+  h->channels_cb = NULL;
+  h->channels_cls = NULL;
+  return cls;
+}
+
+
+/**
+ * Request information about a specific channel of the running mesh peer.
+ *
+ * WARNING: unstable API, likely to change in the future!
+ * FIXME Add destination option.
+ *
+ * @param h Handle to the mesh peer.
+ * @param initiator ID of the owner of the channel.
+ * @param channel_number Channel number.
+ * @param callback Function to call with the requested data.
+ * @param callback_cls Closure for @c callback.
+ */
+void
+GNUNET_MESH_show_channel (struct GNUNET_MESH_Handle *h,
+                         struct GNUNET_PeerIdentity *initiator,
+                         unsigned int channel_number,
+                         GNUNET_MESH_ChannelCB callback,
+                         void *callback_cls)
+{
+  struct GNUNET_MESH_LocalMonitor msg;
+
+  msg.header.size = htons (sizeof (msg));
+  msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_CHANNEL);
+  msg.owner = *initiator;
+  msg.channel_id = htonl (channel_number);
+  msg.reserved = 0;
+  send_packet (h, &msg.header, NULL);
+  h->channel_cb = callback;
+  h->channel_cls = callback_cls;
+
+  return;
+}
+
+
+/**
+ * Function called to notify a client about the connection
+ * begin ready to queue more data.  "buf" will be
+ * NULL and "size" zero if the connection was closed for
+ * writing in the meantime.
+ *
+ * @param cls closure
+ * @param size number of bytes available in buf
+ * @param buf where the callee should write the message
+ * @return number of bytes written to buf
+ */
+static size_t
+mesh_mq_ntr (void *cls, size_t size,
+             void *buf)
+{
+  struct GNUNET_MQ_Handle *mq = cls;
+  struct MeshMQState *state = GNUNET_MQ_impl_state (mq);
+  const struct GNUNET_MessageHeader *msg = GNUNET_MQ_impl_current (mq);
+  uint16_t msize;
+
+  state->th = NULL;
+  if (NULL == buf)
+  {
+    GNUNET_MQ_inject_error (mq, GNUNET_MQ_ERROR_WRITE);
+    return 0;
+  }
+  msize = ntohs (msg->size);
+  GNUNET_assert (msize <= size);
+  memcpy (buf, msg, msize);
+  GNUNET_MQ_impl_send_continue (mq);
+  return msize;
+}
+
+
+/**
+ * Signature of functions implementing the
+ * sending functionality of a message queue.
+ *
+ * @param mq the message queue
+ * @param msg the message to send
+ * @param impl_state state of the implementation
+ */
+static void
+mesh_mq_send_impl (struct GNUNET_MQ_Handle *mq,
+                   const struct GNUNET_MessageHeader *msg, void *impl_state)
+{
+  struct MeshMQState *state = impl_state;
+
+  GNUNET_assert (NULL == state->th);
+  GNUNET_MQ_impl_send_commit (mq);
+  state->th =
+      GNUNET_MESH_notify_transmit_ready (state->channel,
+                                         /* FIXME: add option for corking */
+                                         GNUNET_NO,
+                                         GNUNET_TIME_UNIT_FOREVER_REL,
+                                         ntohs (msg->size),
+                                         mesh_mq_ntr, mq);
+
+}
+
+
+/**
+ * Signature of functions implementing the
+ * destruction of a message queue.
+ * Implementations must not free 'mq', but should
+ * take care of 'impl_state'.
+ *
+ * @param mq the message queue to destroy
+ * @param impl_state state of the implementation
+ */
+static void
+mesh_mq_destroy_impl (struct GNUNET_MQ_Handle *mq, void *impl_state)
+{
+  struct MeshMQState *state = impl_state;
+
+  if (NULL != state->th)
+    GNUNET_MESH_notify_transmit_ready_cancel (state->th);
+
+  GNUNET_free (state);
+}
+
+
+/**
+ * Create a message queue for a mesh channel.
+ * The message queue can only be used to transmit messages,
+ * not to receive them.
+ *
+ * @param channel the channel to create the message qeue for
+ * @return a message queue to messages over the channel
+ */
+struct GNUNET_MQ_Handle *
+GNUNET_MESH_mq_create (struct GNUNET_MESH_Channel *channel)
+{
+  struct GNUNET_MQ_Handle *mq;
+  struct MeshMQState *state;
+
+  state = GNUNET_new (struct MeshMQState);
+  state->channel = channel;
+
+  mq = GNUNET_MQ_queue_for_callbacks (mesh_mq_send_impl,
+                                      mesh_mq_destroy_impl,
+                                      NULL, /* FIXME: cancel impl. */
+                                      state,
+                                      NULL, /* no msg handlers */
+                                      NULL, /* no err handlers */
+                                      NULL); /* no handler cls */
+  return mq;
+}
+

Deleted: gnunet/src/mesh/mesh_api_enc.c
===================================================================
--- gnunet/src/mesh/mesh_api_enc.c      2013-11-14 13:30:29 UTC (rev 30696)
+++ gnunet/src/mesh/mesh_api_enc.c      2013-11-14 13:55:12 UTC (rev 30697)
@@ -1,1794 +0,0 @@
-/*
-     This file is part of GNUnet.
-     (C) 2011 Christian Grothoff (and other contributing authors)
-     GNUnet is free software; you can redistribute it and/or modify
-     it under the terms of the GNU General Public License as published
-     by the Free Software Foundation; either version 3, or (at your
-     option) any later version.
-     GNUnet is distributed in the hope that it will be useful, but
-     WITHOUT ANY WARRANTY; without even the implied warranty of
-     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
-     General Public License for more details.
-     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.
-*/
-
-/**
- * @file mesh/mesh_api_enc.c
- * @brief mesh api: client implementation of new mesh service
- * @author Bartlomiej Polot
- */
-
-#include "platform.h"
-#include "gnunet_util_lib.h"
-#include "gnunet_mesh_service_enc.h"
-#include "mesh_enc.h"
-#include "mesh_protocol_enc.h"
-
-#define LOG(kind,...) GNUNET_log_from (kind, "mesh-api",__VA_ARGS__)
-
-/******************************************************************************/
-/************************      DATA STRUCTURES     
****************************/
-/******************************************************************************/
-
-/**
- * Transmission queue to the service
- */
-struct GNUNET_MESH_TransmitHandle
-{
-
-    /**
-     * Double Linked list
-     */
-  struct GNUNET_MESH_TransmitHandle *next;
-
-    /**
-     * Double Linked list
-     */
-  struct GNUNET_MESH_TransmitHandle *prev;
-
-    /**
-     * Channel this message is sent on / for (may be NULL for control 
messages).
-     */
-  struct GNUNET_MESH_Channel *channel;
-
-    /**
-     * Callback to obtain the message to transmit, or NULL if we
-     * got the message in 'data'.  Notice that messages built
-     * by 'notify' need to be encapsulated with information about
-     * the 'target'.
-     */
-  GNUNET_CONNECTION_TransmitReadyNotify notify;
-
-    /**
-     * Closure for 'notify'
-     */
-  void *notify_cls;
-
-    /**
-     * How long is this message valid.  Once the timeout has been
-     * reached, the message must no longer be sent.  If this
-     * is a message with a 'notify' callback set, the 'notify'
-     * function should be called with 'buf' NULL and size 0.
-     */
-  struct GNUNET_TIME_Absolute timeout;
-
-    /**
-     * Task triggering a timeout, can be NO_TASK if the timeout is FOREVER.
-     */
-  GNUNET_SCHEDULER_TaskIdentifier timeout_task;
-
-    /**
-     * Size of 'data' -- or the desired size of 'notify' if 'data' is NULL.
-     */
-  size_t size;
-};
-
-
-/**
- * Opaque handle to the service.
- */
-struct GNUNET_MESH_Handle
-{
-
-    /**
-     * Handle to the server connection, to send messages later
-     */
-  struct GNUNET_CLIENT_Connection *client;
-
-    /**
-     * Set of handlers used for processing incoming messages in the channels
-     */
-  const struct GNUNET_MESH_MessageHandler *message_handlers;
-
-  /**
-   * Number of handlers in the handlers array.
-   */
-  unsigned int n_handlers;
-
-  /**
-   * Ports open.
-   */
-  const uint32_t *ports;
-
-  /**
-   * Number of ports.
-   */
-  unsigned int n_ports;
-
-    /**
-     * Double linked list of the channels this client is connected to, head.
-     */
-  struct GNUNET_MESH_Channel *channels_head;
-
-    /**
-     * Double linked list of the channels this client is connected to, tail.
-     */
-  struct GNUNET_MESH_Channel *channels_tail;
-
-    /**
-     * Callback for inbound channel creation
-     */
-  GNUNET_MESH_InboundChannelNotificationHandler *new_channel;
-
-    /**
-     * Callback for inbound channel disconnection
-     */
-  GNUNET_MESH_ChannelEndHandler *cleaner;
-
-    /**
-     * Handle to cancel pending transmissions in case of disconnection
-     */
-  struct GNUNET_CLIENT_TransmitHandle *th;
-
-    /**
-     * Closure for all the handlers given by the client
-     */
-  void *cls;
-
-    /**
-     * Messages to send to the service, head.
-     */
-  struct GNUNET_MESH_TransmitHandle *th_head;
-
-    /**
-     * Messages to send to the service, tail.
-     */
-  struct GNUNET_MESH_TransmitHandle *th_tail;
-
-    /**
-     * chid of the next channel to create (to avoid reusing IDs often)
-     */
-  MESH_ChannelNumber next_chid;
-
-    /**
-     * Have we started the task to receive messages from the service
-     * yet? We do this after we send the 'MESH_LOCAL_CONNECT' message.
-     */
-  int in_receive;
-
-  /**
-   * Configuration given by the client, in case of reconnection
-   */
-  const struct GNUNET_CONFIGURATION_Handle *cfg;
-
-  /**
-   * Time to the next reconnect in case one reconnect fails
-   */
-  struct GNUNET_TIME_Relative reconnect_time;
-
-  /**
-   * Task for trying to reconnect.
-   */
-  GNUNET_SCHEDULER_TaskIdentifier reconnect_task;
-
-  /**
-   * Monitor callback
-   */
-  GNUNET_MESH_ChannelsCB channels_cb;
-
-  /**
-   * Monitor callback closure.
-   */
-  void *channels_cls;
-
-  /**
-   * Channel callback.
-   */
-  GNUNET_MESH_ChannelCB channel_cb;
-
-  /**
-   * Channel callback closure.
-   */
-  void *channel_cls;
-};
-
-
-/**
- * Description of a peer
- */
-struct GNUNET_MESH_Peer
-{
-    /**
-     * ID of the peer in short form
-     */
-  GNUNET_PEER_Id id;
-
-  /**
-   * Channel this peer belongs to
-   */
-  struct GNUNET_MESH_Channel *t;
-
-  /**
-   * Flag indicating whether service has informed about its connection
-   * FIXME-BART: is this flag used? Seems dead right now...
-   */
-  int connected;
-
-};
-
-
-/**
- * Opaque handle to a channel.
- */
-struct GNUNET_MESH_Channel
-{
-
-    /**
-     * DLL next
-     */
-  struct GNUNET_MESH_Channel *next;
-
-    /**
-     * DLL prev
-     */
-  struct GNUNET_MESH_Channel *prev;
-
-    /**
-     * Handle to the mesh this channel belongs to
-     */
-  struct GNUNET_MESH_Handle *mesh;
-
-    /**
-     * Local ID of the channel
-     */
-  MESH_ChannelNumber chid;
-
-    /**
-     * Port number.
-     */
-  uint32_t port;
-
-    /**
-     * Other end of the channel.
-     */
-  GNUNET_PEER_Id peer;
-
-  /**
-   * Any data the caller wants to put in here
-   */
-  void *ctx;
-
-    /**
-     * Size of packet queued in this channel
-     */
-  unsigned int packet_size;
-
-    /**
-     * Is the channel allowed to buffer?
-     */
-  int nobuffer;
-
-    /**
-     * Is the channel realiable?
-     */
-  int reliable;
-
-    /**
-     * If reliable, is the channel out of order?
-     */
-  int ooorder;
-
-    /**
-     * Are we allowed to send to the service?
-     */
-  int allow_send;
-
-};
-
-
-/**
- * Implementation state for mesh's message queue.
- */
-struct MeshMQState
-{
-  /**
-   * The current transmit handle, or NULL
-   * if no transmit is active.
-   */
-  struct GNUNET_MESH_TransmitHandle *th;
-
-  /**
-   * Channel to send the data over.
-   */
-  struct GNUNET_MESH_Channel *channel;
-};
-
-
-/******************************************************************************/
-/***********************         DECLARATIONS         
*************************/
-/******************************************************************************/
-
-/**
- * Function called to send a message to the service.
- * "buf" will be NULL and "size" zero if the socket was closed for writing in
- * the meantime.
- *
- * @param cls closure, the mesh handle
- * @param size number of bytes available in buf
- * @param buf where the callee should write the connect message
- * @return number of bytes written to buf
- */
-static size_t
-send_callback (void *cls, size_t size, void *buf);
-
-
-/******************************************************************************/
-/***********************     AUXILIARY FUNCTIONS      
*************************/
-/******************************************************************************/
-
-/**
- * Check if transmission is a payload packet.
- *
- * @param th Transmission handle.
- *
- * @return GNUNET_YES if it is a payload packet,
- *         GNUNET_NO if it is a mesh management packet.
- */
-static int
-th_is_payload (struct GNUNET_MESH_TransmitHandle *th)
-{
-  return (th->notify != NULL) ? GNUNET_YES : GNUNET_NO;
-}
-
-
-/**
- * Check whether there is any message ready in the queue and find the size.
- *
- * @param h Mesh handle.
- *
- * @return The size of the first ready message in the queue,
- *         0 if there is none.
- */
-static size_t
-message_ready_size (struct GNUNET_MESH_Handle *h)
-{
-  struct GNUNET_MESH_TransmitHandle *th;
-  struct GNUNET_MESH_Channel *ch;
-
-  for (th = h->th_head; NULL != th; th = th->next)
-  {
-    ch = th->channel;
-    if (GNUNET_NO == th_is_payload (th))
-    {
-      LOG (GNUNET_ERROR_TYPE_DEBUG, "#  message internal\n");
-      return th->size;
-    }
-    if (GNUNET_YES == ch->allow_send)
-    {
-      LOG (GNUNET_ERROR_TYPE_DEBUG, "#  message payload ok\n");
-      return th->size;
-    }
-  }
-  return 0;
-}
-
-
-/**
- * Get the channel handler for the channel specified by id from the given 
handle
- * @param h Mesh handle
- * @param chid ID of the wanted channel
- * @return handle to the required channel or NULL if not found
- */
-static struct GNUNET_MESH_Channel *
-retrieve_channel (struct GNUNET_MESH_Handle *h, MESH_ChannelNumber chid)
-{
-  struct GNUNET_MESH_Channel *ch;
-
-  ch = h->channels_head;
-  while (ch != NULL)
-  {
-    if (ch->chid == chid)
-      return ch;
-    ch = ch->next;
-  }
-  return NULL;
-}
-
-
-/**
- * Create a new channel and insert it in the channel list of the mesh handle
- *
- * @param h Mesh handle
- * @param chid Desired chid of the channel, 0 to assign one automatically.
- *
- * @return Handle to the created channel.
- */
-static struct GNUNET_MESH_Channel *
-create_channel (struct GNUNET_MESH_Handle *h, MESH_ChannelNumber chid)
-{
-  struct GNUNET_MESH_Channel *ch;
-
-  ch = GNUNET_malloc (sizeof (struct GNUNET_MESH_Channel));
-  GNUNET_CONTAINER_DLL_insert (h->channels_head, h->channels_tail, ch);
-  ch->mesh = h;
-  if (0 == chid)
-  {
-    ch->chid = h->next_chid;
-    while (NULL != retrieve_channel (h, h->next_chid))
-    {
-      h->next_chid++;
-      h->next_chid &= ~GNUNET_MESH_LOCAL_CHANNEL_ID_SERV;
-      h->next_chid |= GNUNET_MESH_LOCAL_CHANNEL_ID_CLI;
-    }
-  }
-  else
-  {
-    ch->chid = chid;
-  }
-  ch->allow_send = GNUNET_NO;
-  ch->nobuffer = GNUNET_NO;
-  return ch;
-}
-
-
-/**
- * Destroy the specified channel.
- * - Destroys all peers, calling the disconnect callback on each if needed
- * - Cancels all outgoing traffic for that channel, calling respective notifys
- * - Calls cleaner if channel was inbound
- * - Frees all memory used
- *
- * @param ch Pointer to the channel.
- * @param call_cleaner Whether to call the cleaner handler.
- *
- * @return Handle to the required channel or NULL if not found.
- */
-static void
-destroy_channel (struct GNUNET_MESH_Channel *ch, int call_cleaner)
-{
-  struct GNUNET_MESH_Handle *h;
-  struct GNUNET_MESH_TransmitHandle *th;
-  struct GNUNET_MESH_TransmitHandle *next;
-
-  LOG (GNUNET_ERROR_TYPE_DEBUG, "destroy_channel %X\n", ch->chid);
-
-  if (NULL == ch)
-  {
-    GNUNET_break (0);
-    return;
-  }
-  h = ch->mesh;
-
-  GNUNET_CONTAINER_DLL_remove (h->channels_head, h->channels_tail, ch);
-
-  /* signal channel destruction */
-  if ( (NULL != h->cleaner) && (0 != ch->peer) && (GNUNET_YES == call_cleaner) 
)
-    h->cleaner (h->cls, ch, ch->ctx);
-
-  /* check that clients did not leave messages behind in the queue */
-  for (th = h->th_head; NULL != th; th = next)
-  {
-    next = th->next;
-    if (th->channel != ch)
-      continue;
-    /* Clients should have aborted their requests already.
-     * Management traffic should be ok, as clients can't cancel that */
-    GNUNET_break (GNUNET_NO == th_is_payload (th));
-    GNUNET_CONTAINER_DLL_remove (h->th_head, h->th_tail, th);
-
-    /* clean up request */
-    if (GNUNET_SCHEDULER_NO_TASK != th->timeout_task)
-      GNUNET_SCHEDULER_cancel (th->timeout_task);
-    GNUNET_free (th);
-  }
-
-  /* if there are no more pending requests with mesh service, cancel active 
request */
-  /* Note: this should be unnecessary... */
-  if ((0 == message_ready_size (h)) && (NULL != h->th))
-  {
-    GNUNET_CLIENT_notify_transmit_ready_cancel (h->th);
-    h->th = NULL;
-  }
-
-  if (0 != ch->peer)
-    GNUNET_PEER_change_rc (ch->peer, -1);
-  GNUNET_free (ch);
-  return;
-}
-
-
-/**
- * Notify client that the transmission has timed out
- *
- * @param cls closure
- * @param tc task context
- */
-static void
-timeout_transmission (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
-{
-  struct GNUNET_MESH_TransmitHandle *th = cls;
-  struct GNUNET_MESH_Handle *mesh;
-
-  mesh = th->channel->mesh;
-  GNUNET_CONTAINER_DLL_remove (mesh->th_head, mesh->th_tail, th);
-  th->channel->packet_size = 0;
-  if (GNUNET_YES == th_is_payload (th))
-    th->notify (th->notify_cls, 0, NULL);
-  GNUNET_free (th);
-  if ((0 == message_ready_size (mesh)) && (NULL != mesh->th))
-  {
-    /* nothing ready to transmit, no point in asking for transmission */
-    GNUNET_CLIENT_notify_transmit_ready_cancel (mesh->th);
-    mesh->th = NULL;
-  }
-}
-
-
-/**
- * Add a transmit handle to the transmission queue and set the
- * timeout if needed.
- *
- * @param h mesh handle with the queue head and tail
- * @param th handle to the packet to be transmitted
- */
-static void
-add_to_queue (struct GNUNET_MESH_Handle *h,
-              struct GNUNET_MESH_TransmitHandle *th)
-{
-  GNUNET_CONTAINER_DLL_insert_tail (h->th_head, h->th_tail, th);
-  if (GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us == th->timeout.abs_value_us)
-    return;
-  th->timeout_task =
-      GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_absolute_get_remaining
-                                    (th->timeout), &timeout_transmission, th);
-}
-
-
-/**
- * Auxiliary function to send an already constructed packet to the service.
- * Takes care of creating a new queue element, copying the message and
- * calling the tmt_rdy function if necessary.
- *
- * @param h mesh handle
- * @param msg message to transmit
- * @param channel channel this send is related to (NULL if N/A)
- */
-static void
-send_packet (struct GNUNET_MESH_Handle *h,
-             const struct GNUNET_MessageHeader *msg,
-             struct GNUNET_MESH_Channel *channel);
-
-
-/**
- * Send an ack on the channel to confirm the processing of a message.
- *
- * @param ch Channel on which to send the ACK.
- */
-static void
-send_ack (struct GNUNET_MESH_Channel *ch)
-{
-  struct GNUNET_MESH_LocalAck msg;
-
-  LOG (GNUNET_ERROR_TYPE_DEBUG, "Sending ACK on channel %X\n", ch->chid);
-  msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
-  msg.header.size = htons (sizeof (msg));
-  msg.channel_id = htonl (ch->chid);
-
-  send_packet (ch->mesh, &msg.header, ch);
-  return;
-}
-
-
-
-/**
- * Reconnect callback: tries to reconnect again after a failer previous
- * reconnecttion
- * @param cls closure (mesh handle)
- * @param tc task context
- */
-static void
-reconnect_cbk (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
-
-
-/**
- * Send a connect packet to the service with the applications and types
- * requested by the user.
- *
- * @param h The mesh handle.
- *
- */
-static void
-send_connect (struct GNUNET_MESH_Handle *h)
-{
-  size_t size;
-
-  size = sizeof (struct GNUNET_MESH_ClientConnect);
-  size += h->n_ports * sizeof (uint32_t);
-  {
-    char buf[size] GNUNET_ALIGN;
-    struct GNUNET_MESH_ClientConnect *msg;
-    uint32_t *ports;
-    uint16_t i;
-
-    /* build connection packet */
-    msg = (struct GNUNET_MESH_ClientConnect *) buf;
-    msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT);
-    msg->header.size = htons (size);
-    ports = (uint32_t *) &msg[1];
-    for (i = 0; i < h->n_ports; i++)
-    {
-      ports[i] = htonl (h->ports[i]);
-      LOG (GNUNET_ERROR_TYPE_DEBUG, " port %u\n",
-           h->ports[i]);
-    }
-    LOG (GNUNET_ERROR_TYPE_DEBUG,
-         "Sending %lu bytes long message with %u ports\n",
-         ntohs (msg->header.size), h->n_ports);
-    send_packet (h, &msg->header, NULL);
-  }
-}
-
-
-/**
- * Reconnect to the service, retransmit all infomation to try to restore the
- * original state.
- *
- * @param h handle to the mesh
- *
- * @return GNUNET_YES in case of sucess, GNUNET_NO otherwise (service down...)
- */
-static int
-do_reconnect (struct GNUNET_MESH_Handle *h)
-{
-  struct GNUNET_MESH_Channel *ch;
-
-  LOG (GNUNET_ERROR_TYPE_DEBUG, "*****************************\n");
-  LOG (GNUNET_ERROR_TYPE_DEBUG, "*******   RECONNECT   *******\n");
-  LOG (GNUNET_ERROR_TYPE_DEBUG, "*****************************\n");
-  LOG (GNUNET_ERROR_TYPE_DEBUG, "******** on %p *******\n", h);
-  LOG (GNUNET_ERROR_TYPE_DEBUG, "*****************************\n");
-
-  /* disconnect */
-  if (NULL != h->th)
-  {
-    GNUNET_CLIENT_notify_transmit_ready_cancel (h->th);
-    h->th = NULL;
-  }
-  if (NULL != h->client)
-  {
-    GNUNET_CLIENT_disconnect (h->client);
-  }
-
-  /* connect again */
-  h->client = GNUNET_CLIENT_connect ("mesh", h->cfg);
-  if (h->client == NULL)
-  {
-    h->reconnect_task = GNUNET_SCHEDULER_add_delayed (h->reconnect_time,
-                                                      &reconnect_cbk, h);
-    h->reconnect_time =
-        GNUNET_TIME_relative_min (GNUNET_TIME_UNIT_SECONDS,
-                                  GNUNET_TIME_relative_multiply
-                                  (h->reconnect_time, 2));
-    LOG (GNUNET_ERROR_TYPE_DEBUG, "Next retry in %s\n",
-         GNUNET_STRINGS_relative_time_to_string (h->reconnect_time,
-                                                 GNUNET_NO));
-    GNUNET_break (0);
-    return GNUNET_NO;
-  }
-  else
-  {
-    h->reconnect_time = GNUNET_TIME_UNIT_MILLISECONDS;
-  }
-  send_connect (h);
-  /* Rebuild all channels */
-  for (ch = h->channels_head; NULL != ch; ch = ch->next)
-  {
-    struct GNUNET_MESH_ChannelMessage tmsg;
-    uint32_t options;
-
-    if (ch->chid >= GNUNET_MESH_LOCAL_CHANNEL_ID_SERV)
-    {
-      /* Channel was created by service (incoming channel) */
-      /* TODO: Notify service of missing channel, to request
-       * creator to recreate path (find a path to him via DHT?)
-       */
-      continue;
-    }
-    ch->allow_send = GNUNET_NO;
-    tmsg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE);
-    tmsg.header.size = htons (sizeof (struct GNUNET_MESH_ChannelMessage));
-    tmsg.channel_id = htonl (ch->chid);
-    tmsg.port = htonl (ch->port);
-    GNUNET_PEER_resolve (ch->peer, &tmsg.peer);
-
-    options = 0;
-    if (GNUNET_YES == ch->nobuffer)
-      options |= GNUNET_MESH_OPTION_NOBUFFER;
-
-    if (GNUNET_YES == ch->reliable)
-      options |= GNUNET_MESH_OPTION_RELIABLE;
-
-    tmsg.opt = htonl (options);
-    send_packet (h, &tmsg.header, ch);
-  }
-  return GNUNET_YES;
-}
-
-/**
- * Reconnect callback: tries to reconnect again after a failer previous
- * reconnecttion
- * @param cls closure (mesh handle)
- * @param tc task context
- */
-static void
-reconnect_cbk (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
-{
-  struct GNUNET_MESH_Handle *h = cls;
-
-  h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
-  if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
-    return;
-  do_reconnect (h);
-}
-
-
-/**
- * Reconnect to the service, retransmit all infomation to try to restore the
- * original state.
- *
- * @param h handle to the mesh
- *
- * @return GNUNET_YES in case of sucess, GNUNET_NO otherwise (service down...)
- */
-static void
-reconnect (struct GNUNET_MESH_Handle *h)
-{
-  LOG (GNUNET_ERROR_TYPE_DEBUG, "Requested RECONNECT\n");
-  h->in_receive = GNUNET_NO;
-  if (GNUNET_SCHEDULER_NO_TASK == h->reconnect_task)
-    h->reconnect_task = GNUNET_SCHEDULER_add_delayed (h->reconnect_time,
-                                                      &reconnect_cbk, h);
-}
-
-
-/******************************************************************************/
-/***********************      RECEIVE HANDLERS     
****************************/
-/******************************************************************************/
-
-/**
- * Process the new channel notification and add it to the channels in the 
handle
- *
- * @param h     The mesh handle
- * @param msg   A message with the details of the new incoming channel
- */
-static void
-process_channel_created (struct GNUNET_MESH_Handle *h,
-                        const struct GNUNET_MESH_ChannelMessage *msg)
-{
-  struct GNUNET_MESH_Channel *ch;
-  MESH_ChannelNumber chid;
-  uint32_t port;
-
-  chid = ntohl (msg->channel_id);
-  port = ntohl (msg->port);
-  LOG (GNUNET_ERROR_TYPE_DEBUG, "Creating incoming channel %X:%u\n", chid, 
port);
-  if (chid < GNUNET_MESH_LOCAL_CHANNEL_ID_SERV)
-  {
-    GNUNET_break (0);
-    return;
-  }
-  if (NULL != h->new_channel)
-  {
-    ch = create_channel (h, chid);
-    ch->allow_send = GNUNET_NO;
-    ch->peer = GNUNET_PEER_intern (&msg->peer);
-    ch->mesh = h;
-    ch->chid = chid;
-    ch->port = port;
-    if (0 != (msg->opt & GNUNET_MESH_OPTION_NOBUFFER))
-      ch->nobuffer = GNUNET_YES;
-    else
-      ch->nobuffer = GNUNET_NO;
-
-    if (0 != (msg->opt & GNUNET_MESH_OPTION_RELIABLE))
-      ch->reliable = GNUNET_YES;
-    else
-      ch->reliable = GNUNET_NO;
-
-    if (GNUNET_YES == ch->reliable &&
-        0 != (msg->opt & GNUNET_MESH_OPTION_OOORDER))
-      ch->ooorder = GNUNET_YES;
-    else
-      ch->ooorder = GNUNET_NO;
-
-    LOG (GNUNET_ERROR_TYPE_DEBUG, "  created channel %p\n", ch);
-    ch->ctx = h->new_channel (h->cls, ch, &msg->peer, ch->port);
-    LOG (GNUNET_ERROR_TYPE_DEBUG, "User notified\n");
-  }
-  else
-  {
-    struct GNUNET_MESH_ChannelMessage d_msg;
-
-    LOG (GNUNET_ERROR_TYPE_DEBUG, "No handler for incoming channels\n");
-
-    d_msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY);
-    d_msg.header.size = htons (sizeof (struct GNUNET_MESH_ChannelMessage));
-    d_msg.channel_id = msg->channel_id;
-    memset (&d_msg.peer, 0, sizeof (struct GNUNET_PeerIdentity));
-    d_msg.port = 0;
-    d_msg.opt = 0;
-
-    send_packet (h, &d_msg.header, NULL);
-  }
-  return;
-}
-
-
-/**
- * Process the channel destroy notification and free associated resources
- *
- * @param h     The mesh handle
- * @param msg   A message with the details of the channel being destroyed
- */
-static void
-process_channel_destroy (struct GNUNET_MESH_Handle *h,
-                         const struct GNUNET_MESH_ChannelMessage *msg)
-{
-  struct GNUNET_MESH_Channel *ch;
-  MESH_ChannelNumber chid;
-
-  LOG (GNUNET_ERROR_TYPE_DEBUG, "Destroying channel from service\n");
-  chid = ntohl (msg->channel_id);
-  ch = retrieve_channel (h, chid);
-
-  if (NULL == ch)
-  {
-    LOG (GNUNET_ERROR_TYPE_DEBUG, "channel %X unknown\n", chid);
-    return;
-  }
-  LOG (GNUNET_ERROR_TYPE_DEBUG, "channel %X destroyed\n", ch->chid);
-  destroy_channel (ch, GNUNET_YES);
-}
-
-
-/**
- * Process the incoming data packets, call appropriate handlers.
- *
- * @param h         The mesh handle
- * @param message   A message encapsulating the data
- */
-static void
-process_incoming_data (struct GNUNET_MESH_Handle *h,
-                       const struct GNUNET_MessageHeader *message)
-{
-  const struct GNUNET_MessageHeader *payload;
-  const struct GNUNET_MESH_MessageHandler *handler;
-  struct GNUNET_MESH_LocalData *dmsg;
-  struct GNUNET_MESH_Channel *ch;
-  unsigned int i;
-  uint16_t type;
-
-  LOG (GNUNET_ERROR_TYPE_DEBUG, "Got a data message!\n");
-
-  dmsg = (struct GNUNET_MESH_LocalData *) message;
-
-  ch = retrieve_channel (h, ntohl (dmsg->id));
-  payload = (struct GNUNET_MessageHeader *) &dmsg[1];
-  LOG (GNUNET_ERROR_TYPE_DEBUG, "  %s data on channel %s [%X]\n",
-       ch->chid >= GNUNET_MESH_LOCAL_CHANNEL_ID_SERV ? "fwd" : "bck",
-       GNUNET_i2s (GNUNET_PEER_resolve2 (ch->peer)), ntohl (dmsg->id));
-  if (NULL == ch)
-  {
-    /* Channel was ignored/destroyed, probably service didn't get it yet */
-    LOG (GNUNET_ERROR_TYPE_DEBUG, "  ignored!\n");
-    return;
-  }
-  type = ntohs (payload->type);
-  LOG (GNUNET_ERROR_TYPE_DEBUG, "  payload type %u\n", type);
-  for (i = 0; i < h->n_handlers; i++)
-  {
-    handler = &h->message_handlers[i];
-    LOG (GNUNET_ERROR_TYPE_DEBUG,
-         "    checking handler for type %u\n",
-         handler->type);
-    if (handler->type == type)
-    {
-      if (GNUNET_OK !=
-          handler->callback (h->cls, ch, &ch->ctx, payload))
-      {
-        LOG (GNUNET_ERROR_TYPE_DEBUG, "callback caused disconnection\n");
-        GNUNET_MESH_channel_destroy (ch);
-        return;
-      }
-      else
-      {
-        LOG (GNUNET_ERROR_TYPE_DEBUG,
-             "callback completed successfully\n");
-      }
-    }
-  }
-}
-
-
-/**
- * Process a local ACK message, enabling the client to send
- * more data to the service.
- *
- * @param h Mesh handle.
- * @param message Message itself.
- */
-static void
-process_ack (struct GNUNET_MESH_Handle *h,
-             const struct GNUNET_MessageHeader *message)
-{
-  struct GNUNET_MESH_LocalAck *msg;
-  struct GNUNET_MESH_Channel *ch;
-  MESH_ChannelNumber chid;
-
-  LOG (GNUNET_ERROR_TYPE_DEBUG, "Got an ACK!\n");
-  msg = (struct GNUNET_MESH_LocalAck *) message;
-  chid = ntohl (msg->channel_id);
-    ch = retrieve_channel (h, chid);
-  if (NULL == ch)
-  {
-    LOG (GNUNET_ERROR_TYPE_WARNING, "ACK on unknown channel %X\n", chid);
-    return;
-  }
-  LOG (GNUNET_ERROR_TYPE_DEBUG, "  on channel %X!\n", ch->chid);
-  ch->allow_send = GNUNET_YES;
-  if (NULL == h->th && 0 < ch->packet_size)
-  {
-    LOG (GNUNET_ERROR_TYPE_DEBUG, "  tmt rdy was NULL, requesting!\n");
-    h->th =
-        GNUNET_CLIENT_notify_transmit_ready (h->client, ch->packet_size,
-                                             GNUNET_TIME_UNIT_FOREVER_REL,
-                                             GNUNET_YES, &send_callback, h);
-  }
-}
-
-
-/*
- * Process a local reply about info on all channels, pass info to the user.
- *
- * @param h Mesh handle.
- * @param message Message itself.
- */
-// static void
-// process_get_channels (struct GNUNET_MESH_Handle *h,
-//                      const struct GNUNET_MessageHeader *message)
-// {
-//   struct GNUNET_MESH_LocalMonitor *msg;
-// 
-//   GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Get Channels messasge received\n");
-// 
-//   if (NULL == h->channels_cb)
-//   {
-//     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "  ignored\n");
-//     return;
-//   }
-// 
-//   msg = (struct GNUNET_MESH_LocalMonitor *) message;
-//   if (ntohs (message->size) !=
-//       (sizeof (struct GNUNET_MESH_LocalMonitor) +
-//        sizeof (struct GNUNET_PeerIdentity)))
-//   {
-//     GNUNET_break_op (0);
-//     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
-//                 "Get channels message: size %hu - expected %u\n",
-//                 ntohs (message->size),
-//                 sizeof (struct GNUNET_MESH_LocalMonitor));
-//     return;
-//   }
-//   h->channels_cb (h->channels_cls,
-//                   ntohl (msg->channel_id),
-//                   &msg->owner,
-//                   &msg->destination);
-// }
-
-
-
-/*
- * Process a local monitor_channel reply, pass info to the user.
- *
- * @param h Mesh handle.
- * @param message Message itself.
- */
-// static void
-// process_show_channel (struct GNUNET_MESH_Handle *h,
-//                      const struct GNUNET_MessageHeader *message)
-// {
-//   struct GNUNET_MESH_LocalMonitor *msg;
-//   size_t esize;
-// 
-//   GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Show Channel messasge received\n");
-// 
-//   if (NULL == h->channel_cb)
-//   {
-//     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "  ignored\n");
-//     return;
-//   }
-// 
-//   /* Verify message sanity */
-//   msg = (struct GNUNET_MESH_LocalMonitor *) message;
-//   esize = sizeof (struct GNUNET_MESH_LocalMonitor);
-//   if (ntohs (message->size) != esize)
-//   {
-//     GNUNET_break_op (0);
-//     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
-//                 "Show channel message: size %hu - expected %u\n",
-//                 ntohs (message->size),
-//                 esize);
-// 
-//     h->channel_cb (h->channel_cls, NULL, NULL);
-//     h->channel_cb = NULL;
-//     h->channel_cls = NULL;
-// 
-//     return;
-//   }
-// 
-//   h->channel_cb (h->channel_cls,
-//                  &msg->destination,
-//                  &msg->owner);
-// }
-
-
-/**
- * Function to process all messages received from the service
- *
- * @param cls closure
- * @param msg message received, NULL on timeout or fatal error
- */
-static void
-msg_received (void *cls, const struct GNUNET_MessageHeader *msg)
-{
-  struct GNUNET_MESH_Handle *h = cls;
-  uint16_t type;
-
-  if (msg == NULL)
-  {
-    LOG (GNUNET_ERROR_TYPE_DEBUG,
-        "Mesh service disconnected, reconnecting\n", h);
-    reconnect (h);
-    return;
-  }
-  type = ntohs (msg->type);
-  LOG (GNUNET_ERROR_TYPE_DEBUG, "\n");
-  LOG (GNUNET_ERROR_TYPE_DEBUG, "Received a message: %s\n",
-       GNUNET_MESH_DEBUG_M2S (type));
-  switch (type)
-  {
-    /* Notify of a new incoming channel */
-  case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE:
-    process_channel_created (h, (struct GNUNET_MESH_ChannelMessage *) msg);
-    break;
-    /* Notify of a channel disconnection */
-  case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY:
-  case GNUNET_MESSAGE_TYPE_MESH_LOCAL_NACK:
-    process_channel_destroy (h, (struct GNUNET_MESH_ChannelMessage *) msg);
-    break;
-  case GNUNET_MESSAGE_TYPE_MESH_LOCAL_DATA:
-    process_incoming_data (h, msg);
-    break;
-  case GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK:
-    process_ack (h, msg);
-    break;
-//   case GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_CHANNELS: DEPRECATED
-//     process_get_channels (h, msg);
-//     break;
-//   case GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_CHANNEL: DEPRECATED
-//     process_show_channel (h, msg);
-//     break;
-  default:
-    /* We shouldn't get any other packages, log and ignore */
-    LOG (GNUNET_ERROR_TYPE_WARNING,
-         "unsolicited message form service (type %s)\n",
-         GNUNET_MESH_DEBUG_M2S (ntohs (msg->type)));
-  }
-  LOG (GNUNET_ERROR_TYPE_DEBUG, "message processed\n");
-  if (GNUNET_YES == h->in_receive)
-  {
-    GNUNET_CLIENT_receive (h->client, &msg_received, h,
-                           GNUNET_TIME_UNIT_FOREVER_REL);
-  }
-  else
-  {
-    LOG (GNUNET_ERROR_TYPE_DEBUG,
-         "in receive off, not calling CLIENT_receive\n");
-  }
-}
-
-
-/******************************************************************************/
-/************************       SEND FUNCTIONS     
****************************/
-/******************************************************************************/
-
-/**
- * Function called to send a message to the service.
- * "buf" will be NULL and "size" zero if the socket was closed for writing in
- * the meantime.
- *
- * @param cls closure, the mesh handle
- * @param size number of bytes available in buf
- * @param buf where the callee should write the connect message
- * @return number of bytes written to buf
- */
-static size_t
-send_callback (void *cls, size_t size, void *buf)
-{
-  struct GNUNET_MESH_Handle *h = cls;
-  struct GNUNET_MESH_TransmitHandle *th;
-  struct GNUNET_MESH_TransmitHandle *next;
-  struct GNUNET_MESH_Channel *ch;
-  char *cbuf = buf;
-  size_t tsize;
-  size_t psize;
-  size_t nsize;
-
-  LOG (GNUNET_ERROR_TYPE_DEBUG, "\n");
-  LOG (GNUNET_ERROR_TYPE_DEBUG, "# Send packet() Buffer %u\n", size);
-  if ((0 == size) || (NULL == buf))
-  {
-    LOG (GNUNET_ERROR_TYPE_DEBUG, "# Received NULL send callback on %p\n", h);
-    reconnect (h);
-    h->th = NULL;
-    return 0;
-  }
-  tsize = 0;
-  next = h->th_head;
-  nsize = message_ready_size (h);
-  while ((NULL != (th = next)) && (0 < nsize) && (size >= nsize))
-  {
-    ch = th->channel;
-    if (GNUNET_YES == th_is_payload (th))
-    {
-      struct GNUNET_MESH_LocalData *dmsg;
-      struct GNUNET_MessageHeader *mh;
-
-      LOG (GNUNET_ERROR_TYPE_DEBUG, "#  payload\n");
-      if (GNUNET_NO == ch->allow_send)
-      {
-        /* This channel is not ready to transmit yet, try next message */
-        next = th->next;
-        continue;
-      }
-      ch->packet_size = 0;
-      GNUNET_assert (size >= th->size);
-      dmsg = (struct GNUNET_MESH_LocalData *) cbuf;
-      mh = (struct GNUNET_MessageHeader *) &dmsg[1];
-      psize = th->notify (th->notify_cls,
-                          size - sizeof (struct GNUNET_MESH_LocalData),
-                          mh);
-      if (psize > 0)
-      {
-        psize += sizeof (struct GNUNET_MESH_LocalData);
-        GNUNET_assert (size >= psize);
-        dmsg->header.size = htons (psize);
-        dmsg->id = htonl (ch->chid);
-        dmsg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_DATA);
-        LOG (GNUNET_ERROR_TYPE_DEBUG, "#  payload type %s\n",
-             GNUNET_MESH_DEBUG_M2S (ntohs (mh->type)));
-                ch->allow_send = GNUNET_NO;
-      }
-      else
-      {
-        LOG (GNUNET_ERROR_TYPE_DEBUG,
-             "#  callback returned size 0, "
-             "application canceled transmission\n");
-      }
-    }
-    else
-    {
-      struct GNUNET_MessageHeader *mh = (struct GNUNET_MessageHeader *) &th[1];
-
-      LOG (GNUNET_ERROR_TYPE_DEBUG, "#  mesh internal traffic, type %s\n",
-           GNUNET_MESH_DEBUG_M2S (ntohs (mh->type)));
-      memcpy (cbuf, &th[1], th->size);
-      psize = th->size;
-    }
-    if (th->timeout_task != GNUNET_SCHEDULER_NO_TASK)
-      GNUNET_SCHEDULER_cancel (th->timeout_task);
-    GNUNET_CONTAINER_DLL_remove (h->th_head, h->th_tail, th);
-    GNUNET_free (th);
-    next = h->th_head;
-    nsize = message_ready_size (h);
-    cbuf += psize;
-    size -= psize;
-    tsize += psize;
-  }
-  LOG (GNUNET_ERROR_TYPE_DEBUG, "#  total size: %u\n", tsize);
-  h->th = NULL;
-  size = message_ready_size (h);
-  if (0 != size)
-  {
-    LOG (GNUNET_ERROR_TYPE_DEBUG, "#  next size: %u\n", size);
-    h->th =
-        GNUNET_CLIENT_notify_transmit_ready (h->client, size,
-                                             GNUNET_TIME_UNIT_FOREVER_REL,
-                                             GNUNET_YES, &send_callback, h);
-  }
-  else
-  {
-    if (NULL != h->th_head)
-      LOG (GNUNET_ERROR_TYPE_DEBUG, "#  can't transmit any more\n");
-    else
-      LOG (GNUNET_ERROR_TYPE_DEBUG, "#  nothing left to transmit\n");
-  }
-  if (GNUNET_NO == h->in_receive)
-  {
-    LOG (GNUNET_ERROR_TYPE_DEBUG, "# start receiving from service\n");
-    h->in_receive = GNUNET_YES;
-    GNUNET_CLIENT_receive (h->client, &msg_received, h,
-                           GNUNET_TIME_UNIT_FOREVER_REL);
-  }
-  LOG (GNUNET_ERROR_TYPE_DEBUG, "# Send packet() END\n");
-  return tsize;
-}
-
-
-/**
- * Auxiliary function to send an already constructed packet to the service.
- * Takes care of creating a new queue element, copying the message and
- * calling the tmt_rdy function if necessary.
- *
- * @param h mesh handle
- * @param msg message to transmit
- * @param channel channel this send is related to (NULL if N/A)
- */
-static void
-send_packet (struct GNUNET_MESH_Handle *h,
-             const struct GNUNET_MessageHeader *msg,
-             struct GNUNET_MESH_Channel *channel)
-{
-  struct GNUNET_MESH_TransmitHandle *th;
-  size_t msize;
-
-  LOG (GNUNET_ERROR_TYPE_DEBUG, " Sending message to service: %s\n",
-       GNUNET_MESH_DEBUG_M2S(ntohs(msg->type)));
-  msize = ntohs (msg->size);
-  th = GNUNET_malloc (sizeof (struct GNUNET_MESH_TransmitHandle) + msize);
-  th->timeout = GNUNET_TIME_UNIT_FOREVER_ABS;
-  th->size = msize;
-  th->channel = channel;
-  memcpy (&th[1], msg, msize);
-  add_to_queue (h, th);
-  LOG (GNUNET_ERROR_TYPE_DEBUG, "  queued\n");
-  if (NULL != h->th)
-    return;
-  LOG (GNUNET_ERROR_TYPE_DEBUG, "  calling ntfy tmt rdy for %u bytes\n", 
msize);
-  h->th =
-      GNUNET_CLIENT_notify_transmit_ready (h->client, msize,
-                                           GNUNET_TIME_UNIT_FOREVER_REL,
-                                           GNUNET_YES, &send_callback, h);
-}
-
-
-/******************************************************************************/
-/**********************      API CALL DEFINITIONS     
*************************/
-/******************************************************************************/
-
-struct GNUNET_MESH_Handle *
-GNUNET_MESH_connect (const struct GNUNET_CONFIGURATION_Handle *cfg, void *cls,
-                     GNUNET_MESH_InboundChannelNotificationHandler new_channel,
-                     GNUNET_MESH_ChannelEndHandler cleaner,
-                     const struct GNUNET_MESH_MessageHandler *handlers,
-                     const uint32_t *ports)
-{
-  struct GNUNET_MESH_Handle *h;
-
-  LOG (GNUNET_ERROR_TYPE_DEBUG, "GNUNET_MESH_connect()\n");
-  h = GNUNET_malloc (sizeof (struct GNUNET_MESH_Handle));
-  LOG (GNUNET_ERROR_TYPE_DEBUG, " addr %p\n", h);
-  h->cfg = cfg;
-  h->new_channel = new_channel;
-  h->cleaner = cleaner;
-  h->client = GNUNET_CLIENT_connect ("mesh", cfg);
-  if (h->client == NULL)
-  {
-    GNUNET_break (0);
-    GNUNET_free (h);
-    return NULL;
-  }
-  h->cls = cls;
-  h->message_handlers = handlers;
-  h->ports = ports;
-  h->next_chid = GNUNET_MESH_LOCAL_CHANNEL_ID_CLI;
-  h->reconnect_time = GNUNET_TIME_UNIT_MILLISECONDS;
-  h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
-
-  if (NULL != ports && ports[0] != 0 && NULL == new_channel)
-  {
-    GNUNET_break (0);
-    LOG (GNUNET_ERROR_TYPE_DEBUG,
-         "no new channel handler given, ports parameter is useless!!\n");
-  }
-  if ((NULL == ports || ports[0] == 0) && NULL != new_channel)
-  {
-    GNUNET_break (0);
-    LOG (GNUNET_ERROR_TYPE_DEBUG,
-         "no ports given, new channel handler will never be called!!\n");
-  }
-  /* count handlers */
-  for (h->n_handlers = 0;
-       handlers && handlers[h->n_handlers].type;
-       h->n_handlers++) ;
-  for (h->n_ports = 0;
-       ports && ports[h->n_ports];
-       h->n_ports++) ;
-  send_connect (h);
-  LOG (GNUNET_ERROR_TYPE_DEBUG, "GNUNET_MESH_connect() END\n");
-  return h;
-}
-
-
-void
-GNUNET_MESH_disconnect (struct GNUNET_MESH_Handle *handle)
-{
-  struct GNUNET_MESH_Channel *ch;
-  struct GNUNET_MESH_Channel *aux;
-  struct GNUNET_MESH_TransmitHandle *th;
-
-  LOG (GNUNET_ERROR_TYPE_DEBUG, "MESH DISCONNECT\n");
-
-  ch = handle->channels_head;
-  while (NULL != ch)
-  {
-    aux = ch->next;
-    if (ch->chid < GNUNET_MESH_LOCAL_CHANNEL_ID_SERV)
-    {
-      GNUNET_break (0);
-      LOG (GNUNET_ERROR_TYPE_DEBUG, "channel %X not destroyed\n", ch->chid);
-    }
-    destroy_channel (ch, GNUNET_YES);
-    ch = aux;
-  }
-  while ( (th = handle->th_head) != NULL)
-  {
-    struct GNUNET_MessageHeader *msg;
-
-    /* Make sure it is an allowed packet (everything else should have been
-     * already canceled).
-     */
-    GNUNET_break (GNUNET_NO == th_is_payload (th));
-    msg = (struct GNUNET_MessageHeader *) &th[1];
-    switch (ntohs(msg->type))
-    {
-      case GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT:
-      case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE:
-      case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY:
-      case GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_CHANNELS:
-      case GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_CHANNEL:
-        break;
-      default:
-        GNUNET_break (0);
-        LOG (GNUNET_ERROR_TYPE_ERROR, "unexpected msg %u\n",
-             ntohs(msg->type));
-    }
-
-    GNUNET_CONTAINER_DLL_remove (handle->th_head, handle->th_tail, th);
-    GNUNET_free (th);
-  }
-
-  if (NULL != handle->th)
-  {
-    GNUNET_CLIENT_notify_transmit_ready_cancel (handle->th);
-    handle->th = NULL;
-  }
-  if (NULL != handle->client)
-  {
-    GNUNET_CLIENT_disconnect (handle->client);
-    handle->client = NULL;
-  }
-  if (GNUNET_SCHEDULER_NO_TASK != handle->reconnect_task)
-  {
-    GNUNET_SCHEDULER_cancel(handle->reconnect_task);
-    handle->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
-  }
-  GNUNET_free (handle);
-}
-
-
-/**
- * Create a new channel towards a remote peer.
- *
- * If the destination port is not open by any peer or the destination peer
- * does not accept the channel, #GNUNET_MESH_ChannelEndHandler will be called
- * for this channel.
- *
- * @param h mesh handle
- * @param channel_ctx client's channel context to associate with the channel
- * @param peer peer identity the channel should go to
- * @param port Port number.
- * @param nobuffer Flag for disabling buffering on relay nodes.
- * @param reliable Flag for end-to-end reliability.
- *
- * @return handle to the channel
- */
-struct GNUNET_MESH_Channel *
-GNUNET_MESH_channel_create (struct GNUNET_MESH_Handle *h,
-                           void *channel_ctx,
-                           const struct GNUNET_PeerIdentity *peer,
-                           uint32_t port,
-                           int nobuffer,
-                           int reliable)
-{
-  struct GNUNET_MESH_Channel *ch;
-  struct GNUNET_MESH_ChannelMessage msg;
-
-  LOG (GNUNET_ERROR_TYPE_DEBUG,
-       "Creating new channel to %s:%u\n",
-       GNUNET_i2s (peer), port);
-  ch = create_channel (h, 0);
-  LOG (GNUNET_ERROR_TYPE_DEBUG, "  at %p\n", ch);
-  LOG (GNUNET_ERROR_TYPE_DEBUG, "  number %X\n", ch->chid);
-  ch->ctx = channel_ctx;
-  ch->peer = GNUNET_PEER_intern (peer);
-  msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE);
-  msg.header.size = htons (sizeof (struct GNUNET_MESH_ChannelMessage));
-  msg.channel_id = htonl (ch->chid);
-  msg.port = htonl (port);
-  msg.peer = *peer;
-  msg.opt = 0;
-  if (GNUNET_YES == reliable)
-    msg.opt |= GNUNET_MESH_OPTION_RELIABLE;
-  if (GNUNET_YES == nobuffer)
-    msg.opt |= GNUNET_MESH_OPTION_NOBUFFER;
-  msg.opt = htonl (msg.opt);
-  ch->allow_send = 0;
-  send_packet (h, &msg.header, ch);
-  return ch;
-}
-
-
-void
-GNUNET_MESH_channel_destroy (struct GNUNET_MESH_Channel *channel)
-{
-  struct GNUNET_MESH_Handle *h;
-  struct GNUNET_MESH_ChannelMessage msg;
-  struct GNUNET_MESH_TransmitHandle *th;
-
-  LOG (GNUNET_ERROR_TYPE_DEBUG, "Destroying channel\n");
-  h = channel->mesh;
-
-  msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY);
-  msg.header.size = htons (sizeof (struct GNUNET_MESH_ChannelMessage));
-  msg.channel_id = htonl (channel->chid);
-  memset (&msg.peer, 0, sizeof (struct GNUNET_PeerIdentity));
-  msg.port = 0;
-  msg.opt = 0;
-  th = h->th_head;
-  while (th != NULL)
-  {
-    struct GNUNET_MESH_TransmitHandle *aux;
-    if (th->channel == channel)
-    {
-      aux = th->next;
-      /* FIXME call the handler? */
-      if (GNUNET_YES == th_is_payload (th))
-        th->notify (th->notify_cls, 0, NULL);
-      GNUNET_CONTAINER_DLL_remove (h->th_head, h->th_tail, th);
-      GNUNET_free (th);
-      th = aux;
-    }
-    else
-      th = th->next;
-  }
-
-  destroy_channel (channel, GNUNET_YES);
-  send_packet (h, &msg.header, NULL);
-}
-
-
-/**
- * Get information about a channel.
- *
- * @param channel Channel handle.
- * @param option Query (GNUNET_MESH_OPTION_*).
- * @param ... dependant on option, currently not used
- *
- * @return Union with an answer to the query.
- */
-const union GNUNET_MESH_ChannelInfo *
-GNUNET_MESH_channel_get_info (struct GNUNET_MESH_Channel *channel,
-                             enum MeshOption option, ...)
-{
-  const union GNUNET_MESH_ChannelInfo *ret;
-
-  switch (option)
-  {
-    case GNUNET_MESH_OPTION_NOBUFFER:
-      ret = (const union GNUNET_MESH_ChannelInfo *) &channel->nobuffer;
-      break;
-    case GNUNET_MESH_OPTION_RELIABLE:
-      ret = (const union GNUNET_MESH_ChannelInfo *) &channel->reliable;
-      break;
-    case GNUNET_MESH_OPTION_OOORDER:
-      ret = (const union GNUNET_MESH_ChannelInfo *) &channel->ooorder;
-      break;
-    case GNUNET_MESH_OPTION_PEER:
-      ret = (const union GNUNET_MESH_ChannelInfo *) &channel->peer;
-      break;
-    default:
-      GNUNET_break (0);
-      return NULL;
-  }
-
-  return ret;
-}
-
-struct GNUNET_MESH_TransmitHandle *
-GNUNET_MESH_notify_transmit_ready (struct GNUNET_MESH_Channel *channel, int 
cork,
-                                   struct GNUNET_TIME_Relative maxdelay,
-                                   size_t notify_size,
-                                   GNUNET_CONNECTION_TransmitReadyNotify 
notify,
-                                   void *notify_cls)
-{
-  struct GNUNET_MESH_TransmitHandle *th;
-
-  GNUNET_assert (NULL != channel);
-  LOG (GNUNET_ERROR_TYPE_DEBUG, "MESH NOTIFY TRANSMIT READY\n");
-  LOG (GNUNET_ERROR_TYPE_DEBUG, "    on channel %X\n", channel->chid);
-  LOG (GNUNET_ERROR_TYPE_DEBUG, "    allow_send %d\n", channel->allow_send);
-  if (channel->chid >= GNUNET_MESH_LOCAL_CHANNEL_ID_SERV)
-    LOG (GNUNET_ERROR_TYPE_DEBUG, "    to origin\n");
-  else
-    LOG (GNUNET_ERROR_TYPE_DEBUG, "    to destination\n");
-  LOG (GNUNET_ERROR_TYPE_DEBUG, "    payload size %u\n", notify_size);
-  GNUNET_assert (NULL != notify);
-  GNUNET_assert (0 == channel->packet_size); // Only one data packet allowed
-  th = GNUNET_malloc (sizeof (struct GNUNET_MESH_TransmitHandle));
-  th->channel = channel;
-  th->timeout = GNUNET_TIME_relative_to_absolute (maxdelay);
-  th->size = notify_size + sizeof (struct GNUNET_MESH_LocalData);
-  channel->packet_size = th->size;
-  LOG (GNUNET_ERROR_TYPE_DEBUG, "    total size %u\n", th->size);
-  th->notify = notify;
-  th->notify_cls = notify_cls;
-  add_to_queue (channel->mesh, th);
-  if (NULL != channel->mesh->th)
-    return th;
-  if (GNUNET_NO == channel->allow_send)
-    return th;
-  LOG (GNUNET_ERROR_TYPE_DEBUG, "    call client notify tmt rdy\n");
-  channel->mesh->th =
-      GNUNET_CLIENT_notify_transmit_ready (channel->mesh->client, th->size,
-                                           GNUNET_TIME_UNIT_FOREVER_REL,
-                                           GNUNET_YES, &send_callback,
-                                           channel->mesh);
-  LOG (GNUNET_ERROR_TYPE_DEBUG, "MESH NOTIFY TRANSMIT READY END\n");
-  return th;
-}
-
-
-void
-GNUNET_MESH_notify_transmit_ready_cancel (struct GNUNET_MESH_TransmitHandle 
*th)
-{
-  struct GNUNET_MESH_Handle *mesh;
-
-  th->channel->packet_size = 0;
-  mesh = th->channel->mesh;
-  if (th->timeout_task != GNUNET_SCHEDULER_NO_TASK)
-    GNUNET_SCHEDULER_cancel (th->timeout_task);
-  GNUNET_CONTAINER_DLL_remove (mesh->th_head, mesh->th_tail, th);
-  GNUNET_free (th);
-  if ((0 == message_ready_size (mesh)) && (NULL != mesh->th))
-  {
-    /* queue empty, no point in asking for transmission */
-    GNUNET_CLIENT_notify_transmit_ready_cancel (mesh->th);
-    mesh->th = NULL;
-  }
-}
-
-void
-GNUNET_MESH_receive_done (struct GNUNET_MESH_Channel *channel)
-{
-  send_ack (channel);
-}
-
-
-/**
- * Request information about the running mesh peer.
- * The callback will be called for every channel known to the service,
- * listing all active peers that blong to the channel.
- *
- * If called again on the same handle, it will overwrite the previous
- * callback and cls. To retrieve the cls, monitor_cancel must be
- * called first.
- *
- * WARNING: unstable API, likely to change in the future!
- *
- * @param h Handle to the mesh peer.
- * @param callback Function to call with the requested data.
- * @param callback_cls Closure for @c callback.
- */
-void
-GNUNET_MESH_get_channels (struct GNUNET_MESH_Handle *h,
-                         GNUNET_MESH_ChannelsCB callback,
-                         void *callback_cls)
-{
-  struct GNUNET_MessageHeader msg;
-
-  msg.size = htons (sizeof (msg));
-  msg.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_CHANNELS);
-  send_packet (h, &msg, NULL);
-  h->channels_cb = callback;
-  h->channels_cls = callback_cls;
-
-  return;
-}
-
-
-/**
- * Cancel a monitor request. The monitor callback will not be called.
- *
- * @param h Mesh handle.
- *
- * @return Closure given to GNUNET_MESH_monitor, if any.
- */
-void *
-GNUNET_MESH_get_channels_cancel (struct GNUNET_MESH_Handle *h)
-{
-  void *cls;
-
-  cls = h->channels_cls;
-  h->channels_cb = NULL;
-  h->channels_cls = NULL;
-  return cls;
-}
-
-
-/**
- * Request information about a specific channel of the running mesh peer.
- *
- * WARNING: unstable API, likely to change in the future!
- * FIXME Add destination option.
- *
- * @param h Handle to the mesh peer.
- * @param initiator ID of the owner of the channel.
- * @param channel_number Channel number.
- * @param callback Function to call with the requested data.
- * @param callback_cls Closure for @c callback.
- */
-void
-GNUNET_MESH_show_channel (struct GNUNET_MESH_Handle *h,
-                         struct GNUNET_PeerIdentity *initiator,
-                         unsigned int channel_number,
-                         GNUNET_MESH_ChannelCB callback,
-                         void *callback_cls)
-{
-  struct GNUNET_MESH_LocalMonitor msg;
-
-  msg.header.size = htons (sizeof (msg));
-  msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_CHANNEL);
-  msg.owner = *initiator;
-  msg.channel_id = htonl (channel_number);
-  msg.reserved = 0;
-  send_packet (h, &msg.header, NULL);
-  h->channel_cb = callback;
-  h->channel_cls = callback_cls;
-
-  return;
-}
-
-
-/**
- * Function called to notify a client about the connection
- * begin ready to queue more data.  "buf" will be
- * NULL and "size" zero if the connection was closed for
- * writing in the meantime.
- *
- * @param cls closure
- * @param size number of bytes available in buf
- * @param buf where the callee should write the message
- * @return number of bytes written to buf
- */
-static size_t
-mesh_mq_ntr (void *cls, size_t size,
-             void *buf)
-{
-  struct GNUNET_MQ_Handle *mq = cls;
-  struct MeshMQState *state = GNUNET_MQ_impl_state (mq);
-  const struct GNUNET_MessageHeader *msg = GNUNET_MQ_impl_current (mq);
-  uint16_t msize;
-
-  state->th = NULL;
-  if (NULL == buf)
-  {
-    GNUNET_MQ_inject_error (mq, GNUNET_MQ_ERROR_WRITE);
-    return 0;
-  }
-  msize = ntohs (msg->size);
-  GNUNET_assert (msize <= size);
-  memcpy (buf, msg, msize);
-  GNUNET_MQ_impl_send_continue (mq);
-  return msize;
-}
-
-
-/**
- * Signature of functions implementing the
- * sending functionality of a message queue.
- *
- * @param mq the message queue
- * @param msg the message to send
- * @param impl_state state of the implementation
- */
-static void
-mesh_mq_send_impl (struct GNUNET_MQ_Handle *mq,
-                   const struct GNUNET_MessageHeader *msg, void *impl_state)
-{
-  struct MeshMQState *state = impl_state;
-
-  GNUNET_assert (NULL == state->th);
-  GNUNET_MQ_impl_send_commit (mq);
-  state->th =
-      GNUNET_MESH_notify_transmit_ready (state->channel,
-                                         /* FIXME: add option for corking */
-                                         GNUNET_NO,
-                                         GNUNET_TIME_UNIT_FOREVER_REL,
-                                         ntohs (msg->size),
-                                         mesh_mq_ntr, mq);
-
-}
-
-
-/**
- * Signature of functions implementing the
- * destruction of a message queue.
- * Implementations must not free 'mq', but should
- * take care of 'impl_state'.
- *
- * @param mq the message queue to destroy
- * @param impl_state state of the implementation
- */
-static void
-mesh_mq_destroy_impl (struct GNUNET_MQ_Handle *mq, void *impl_state)
-{
-  struct MeshMQState *state = impl_state;
-
-  if (NULL != state->th)
-    GNUNET_MESH_notify_transmit_ready_cancel (state->th);
-
-  GNUNET_free (state);
-}
-
-
-/**
- * Create a message queue for a mesh channel.
- * The message queue can only be used to transmit messages,
- * not to receive them.
- *
- * @param channel the channel to create the message qeue for
- * @return a message queue to messages over the channel
- */
-struct GNUNET_MQ_Handle *
-GNUNET_MESH_mq_create (struct GNUNET_MESH_Channel *channel)
-{
-  struct GNUNET_MQ_Handle *mq;
-  struct MeshMQState *state;
-
-  state = GNUNET_new (struct MeshMQState);
-  state->channel = channel;
-
-  mq = GNUNET_MQ_queue_for_callbacks (mesh_mq_send_impl,
-                                      mesh_mq_destroy_impl,
-                                      NULL, /* FIXME: cancel impl. */
-                                      state,
-                                      NULL, /* no msg handlers */
-                                      NULL, /* no err handlers */
-                                      NULL); /* no handler cls */
-  return mq;
-}
-

Deleted: gnunet/src/mesh/mesh_enc.h
===================================================================
--- gnunet/src/mesh/mesh_enc.h  2013-11-14 13:30:29 UTC (rev 30696)
+++ gnunet/src/mesh/mesh_enc.h  2013-11-14 13:55:12 UTC (rev 30697)
@@ -1,259 +0,0 @@
-/*
-     This file is part of GNUnet.
-     (C) 2001 - 2011 Christian Grothoff (and other contributing authors)
-
-     GNUnet is free software; you can redistribute it and/or modify
-     it under the terms of the GNU General Public License as published
-     by the Free Software Foundation; either version 3, or (at your
-     option) any later version.
-
-     GNUnet is distributed in the hope that it will be useful, but
-     WITHOUT ANY WARRANTY; without even the implied warranty of
-     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
-     General Public License for more details.
-
-     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.
-*/
-
-/**
- * @author Bartlomiej Polot
- * @file mesh/mesh_enc.h
- */
-
-#ifndef MESH_H_
-#define MESH_H_
-
-#ifdef __cplusplus
-extern "C"
-{
-#if 0                           /* keep Emacsens' auto-indent happy */
-}
-#endif
-#endif
-
-#include <stdint.h>
-
-#define MESH_DEBUG              GNUNET_YES
-
-#include "platform.h"
-#include "gnunet_common.h"
-#include "gnunet_util_lib.h"
-#include "gnunet_peer_lib.h"
-#include "gnunet_core_service.h"
-#include "gnunet_protocols.h"
-#include <gnunet_mesh_service.h>
-
-/******************************************************************************/
-/**************************       CONSTANTS      
******************************/
-/******************************************************************************/
-
-#define GNUNET_MESH_LOCAL_CHANNEL_ID_CLI        0x80000000
-#define GNUNET_MESH_LOCAL_CHANNEL_ID_SERV       0xB0000000
-
-#define HIGH_PID                                0xFFFF0000
-#define LOW_PID                                 0x0000FFFF
-
-#define PID_OVERFLOW(pid, max) (pid > HIGH_PID && max < LOW_PID)
-
-/******************************************************************************/
-/**************************        MESSAGES      
******************************/
-/******************************************************************************/
-
-GNUNET_NETWORK_STRUCT_BEGIN
-
-/**
- * Message for a client to register to the service
- */
-struct GNUNET_MESH_ClientConnect
-{
-    /**
-     * Type: GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT
-     *
-     * Size: sizeof(struct GNUNET_MESH_ClientConnect) +
-     *       sizeof(MESH_ApplicationType) * applications +
-     *       sizeof(uint16_t) * types
-     */
-  struct GNUNET_MessageHeader header;
-  /* uint32_t                 list_ports[]           */
-};
-
-
-/**
- * Type for channel numbering.
- * - Local channel numbers given by the service (incoming) are >= 0xB0000000
- * - Local channel numbers given by the client (created) are >= 0x80000000
- * - Global channel numbers are < 0x80000000
- */
-typedef uint32_t MESH_ChannelNumber;
-
-
-/**
- * Message for a client to create and destroy channels.
- */
-struct GNUNET_MESH_ChannelMessage
-{
-    /**
-     * Type: GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_[CREATE|DESTROY]
-     *
-     * Size: sizeof(struct GNUNET_MESH_ChannelMessage)
-     */
-  struct GNUNET_MessageHeader header;
-
-    /**
-     * ID of a channel controlled by this client.
-     */
-  MESH_ChannelNumber channel_id GNUNET_PACKED;
-
-    /**
-     * Channel's peer
-     */
-  struct GNUNET_PeerIdentity peer;
-
-    /**
-     * Port of the channel.
-     */
-  uint32_t port GNUNET_PACKED;
-
-    /**
-     * Options.
-     */
-  uint32_t opt GNUNET_PACKED;
-};
-
-
-/**
- * Message for mesh data traffic.
- */
-struct GNUNET_MESH_LocalData
-{
-    /**
-     * Type: GNUNET_MESSAGE_TYPE_MESH_LOCAL_DATA
-     */
-  struct GNUNET_MessageHeader header;
-
-    /**
-     * ID of the channel
-     */
-  uint32_t id GNUNET_PACKED;
-
-    /**
-     * Payload follows
-     */
-};
-
-
-/**
- * Message to allow the client send more data to the service
- * (always service -> client).
- */
-struct GNUNET_MESH_LocalAck
-{
-    /**
-     * Type: GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK
-     */
-  struct GNUNET_MessageHeader header;
-
-    /**
-     * ID of the channel allowed to send more data.
-     */
-  MESH_ChannelNumber channel_id GNUNET_PACKED;
-
-};
-
-
-/**
- * Message to inform the client about channels in the service.
- */
-struct GNUNET_MESH_LocalMonitor
-{
-  /**
-     * Type: GNUNET_MESSAGE_TYPE_MESH_LOCAL_MONITOR[_TUNNEL]
-   */
-  struct GNUNET_MessageHeader header;
-
-  /**
-   * ID of the channel allowed to send more data.
-   */
-  MESH_ChannelNumber channel_id GNUNET_PACKED;
-
-  /**
-   * Alignment.
-   */
-  uint32_t reserved GNUNET_PACKED;
-
-  /**
-   * ID of the owner of the channel (can be local peer).
-   */
-  struct GNUNET_PeerIdentity owner;
-
-  /**
-   * ID of the destination of the channel (can be local peer).
-   */
-  struct GNUNET_PeerIdentity destination;
-};
-
-
-GNUNET_NETWORK_STRUCT_END
-
-
-
-/**
- * Check if one pid is bigger than other, accounting for overflow.
- *
- * @param bigger Argument that should be bigger.
- * @param smaller Argument that should be smaller.
- *
- * @return True if bigger (arg1) has a higher value than smaller (arg 2).
- */
-int
-GMC_is_pid_bigger (uint32_t bigger, uint32_t smaller);
-
-
-/**
- * Get the higher ACK value out of two values, taking in account overflow.
- *
- * @param a First ACK value.
- * @param b Second ACK value.
- *
- * @return Highest ACK value from the two.
- */
-uint32_t
-GMC_max_pid (uint32_t a, uint32_t b);
-
-
-/**
- * Get the lower ACK value out of two values, taking in account overflow.
- *
- * @param a First ACK value.
- * @param b Second ACK value.
- *
- * @return Lowest ACK value from the two.
- */
-uint32_t
-GMC_min_pid (uint32_t a, uint32_t b);
-
-
-/**
- * Convert a message type into a string to help debug
- * Generated with:
- * FIND:        "#define ([^ ]+)[ ]*([0-9]+)"
- * REPLACE:     "    case \2: return "\1"; break;"
- *
- * @param m Message type.
- *
- * @return Human readable string description.
- */
-const char *
-GNUNET_MESH_DEBUG_M2S (uint16_t m);
-
-#if 0                           /* keep Emacsens' auto-indent happy */
-{
-#endif
-#ifdef __cplusplus
-}
-#endif
-
-#endif

Deleted: gnunet/src/mesh/mesh_protocol.h
===================================================================
--- gnunet/src/mesh/mesh_protocol.h     2013-11-14 13:30:29 UTC (rev 30696)
+++ gnunet/src/mesh/mesh_protocol.h     2013-11-14 13:55:12 UTC (rev 30697)
@@ -1,337 +0,0 @@
-/*
-     This file is part of GNUnet.
-     (C) 2001 - 2011 Christian Grothoff (and other contributing authors)
-
-     GNUnet is free software; you can redistribute it and/or modify
-     it under the terms of the GNU General Public License as published
-     by the Free Software Foundation; either version 3, or (at your
-     option) any later version.
-
-     GNUnet is distributed in the hope that it will be useful, but
-     WITHOUT ANY WARRANTY; without even the implied warranty of
-     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
-     General Public License for more details.
-
-     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.
-*/
-
-/**
- * @author Bartlomiej Polot
- * @file mesh/mesh_protocol.h
- */
-
-#ifndef MESH_PROTOCOL_H_
-#define MESH_PROTOCOL_H_
-
-#ifdef __cplusplus
-extern "C"
-{
-#if 0
-  /* keep Emacsens' auto-indent happy */
-}
-#endif
-#endif
-
-/******************************************************************************/
-/********************      MESH NETWORK MESSAGES     
**************************/
-/******************************************************************************/
-
-GNUNET_NETWORK_STRUCT_BEGIN
-
-/**
- * Message for mesh path creation.
- */
-struct GNUNET_MESH_CreateTunnel
-{
-    /**
-     * Type: GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE
-     *
-     * Size: sizeof(struct GNUNET_MESH_ManipulatePath) +
-     *       path_length * sizeof (struct GNUNET_PeerIdentity)
-     */
-  struct GNUNET_MessageHeader header;
-
-    /**
-     * Global id of the tunnel this path belongs to,
-     * unique in conjunction with the origin.
-     */
-  uint32_t tid GNUNET_PACKED;
-
-    /**
-     * Tunnel options (GNUNET_MESH_OPTION_*).
-     */
-  uint32_t opt GNUNET_PACKED;
-
-    /**
-     * Destination port.
-     */
-  uint32_t port GNUNET_PACKED;
-
-    /**
-     * FIXME do not add the first hop
-     * path_length structs defining the *whole* path from the origin [0] to the
-     * final destination [path_length-1].
-     */
-  /* struct GNUNET_PeerIdentity peers[path_length]; */
-};
-
-
-/**
- * Message for mesh data traffic.
- */
-struct GNUNET_MESH_Data
-{
-    /**
-     * Type: GNUNET_MESSAGE_TYPE_MESH_UNICAST,
-     *       GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN
-     */
-  struct GNUNET_MessageHeader header;
-
-    /**
-     * TID of the tunnel
-     */
-  uint32_t tid GNUNET_PACKED;
-
-    /**
-     * Number of hops to live
-     */
-  uint32_t ttl GNUNET_PACKED;
-
-    /**
-     * ID of the packet
-     */
-  uint32_t pid GNUNET_PACKED;
-
-    /**
-     * OID of the tunnel
-     */
-  struct GNUNET_PeerIdentity oid;
-
-  /**
-   * Unique ID of the payload message
-   */
-  uint32_t mid GNUNET_PACKED;
-
-    /**
-     * Payload follows
-     */
-};
-
-
-/**
- * Message to acknowledge end-to-end data.
- */
-struct GNUNET_MESH_DataACK
-{
-  /**
-   * Type: GNUNET_MESSAGE_TYPE_MESH_DATA_ACK
-   */
-  struct GNUNET_MessageHeader header;
-
-  /**
-   * TID of the tunnel
-   */
-  uint32_t tid GNUNET_PACKED;
-
-  /**
-   * OID of the tunnel
-   */
-  struct GNUNET_PeerIdentity oid;
-
-  /**
-   * Bitfield of already-received newer messages
-   * pid +  1 @ LSB
-   * pid + 64 @ MSB
-   */
-  uint64_t futures GNUNET_PACKED;
-
-  /**
-   * Last message ID received.
-   */
-  uint32_t mid GNUNET_PACKED;
-};
-
-
-/**
- * Message to acknowledge mesh data traffic.
- */
-struct GNUNET_MESH_ACK
-{
-    /**
-     * Type: GNUNET_MESSAGE_TYPE_MESH_ACK
-     */
-  struct GNUNET_MessageHeader header;
-
-    /**
-     * TID of the tunnel
-     */
-  uint32_t tid GNUNET_PACKED;
-
-    /**
-     * OID of the tunnel
-     */
-  struct GNUNET_PeerIdentity oid;
-
-    /**
-     * Maximum packet ID authorized.
-     */
-  uint32_t pid GNUNET_PACKED;
-};
-
-
-/**
- * Message to query a peer about its Flow Control status regarding a tunnel.
- */
-struct GNUNET_MESH_Poll
-{
-  /**
-   * Type: GNUNET_MESSAGE_TYPE_MESH_POLL
-   */
-  struct GNUNET_MessageHeader header;
-
-  /**
-   * TID of the tunnel
-   */
-  uint32_t tid GNUNET_PACKED;
-
-  /**
-   * OID of the tunnel
-   */
-  struct GNUNET_PeerIdentity oid;
-
-  /**
-   * Last packet sent.
-   */
-  uint32_t pid GNUNET_PACKED;
-};
-
-/**
- * Message for ack'ing a path
- */
-struct GNUNET_MESH_PathACK
-{
-    /**
-     * Type: GNUNET_MESSAGE_TYPE_MESH_PATH_ACK
-     */
-  struct GNUNET_MessageHeader header;
-
-    /**
-     * TID of the tunnel
-     */
-  uint32_t tid GNUNET_PACKED;
-
-    /**
-     * OID of the tunnel
-     */
-  struct GNUNET_PeerIdentity oid;
-
-    /**
-     * ID of the endpoint
-     */
-  struct GNUNET_PeerIdentity peer_id;
-
-    /**
-     * Initial ACK value for payload.
-     */
-  uint32_t ack GNUNET_PACKED;
-
-  /* TODO: signature */
-};
-
-
-/**
- * Message for notifying a disconnection in a path
- */
-struct GNUNET_MESH_PathBroken
-{
-    /**
-     * Type: GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN
-     */
-  struct GNUNET_MessageHeader header;
-
-    /**
-     * TID of the tunnel
-     */
-  uint32_t tid GNUNET_PACKED;
-
-    /**
-     * OID of the tunnel
-     */
-  struct GNUNET_PeerIdentity oid;
-
-    /**
-     * ID of the endpoint
-     */
-  struct GNUNET_PeerIdentity peer1;
-
-    /**
-     * ID of the endpoint
-     */
-  struct GNUNET_PeerIdentity peer2;
-
-  /* TODO: signature */
-};
-
-
-/**
- * Message to destroy a tunnel
- */
-struct GNUNET_MESH_TunnelDestroy
-{
-    /**
-     * Type: GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY
-     */
-  struct GNUNET_MessageHeader header;
-
-    /**
-     * TID of the tunnel
-     */
-  uint32_t tid GNUNET_PACKED;
-
-    /**
-     * OID of the tunnel
-     */
-  struct GNUNET_PeerIdentity oid;
-
-  /* TODO: signature */
-};
-
-
-/**
- * Message to destroy a tunnel
- */
-struct GNUNET_MESH_TunnelKeepAlive
-{
-  /**
-   * Type: GNUNET_MESSAGE_TYPE_MESH_PATH_(FWD|BCK)_KEEPALIVE
-   */
-  struct GNUNET_MessageHeader header;
-
-  /**
-   * TID of the tunnel
-   */
-  uint32_t tid GNUNET_PACKED;
-
-  /**
-   * OID of the tunnel
-   */
-  struct GNUNET_PeerIdentity oid;
-};
-
-
-
-GNUNET_NETWORK_STRUCT_END
-
-#if 0                           /* keep Emacsens' auto-indent happy */
-{
-#endif
-#ifdef __cplusplus
-}
-#endif
-
-/* ifndef MESH_PROTOCOL_H */
-#endif
-/* end of mesh_protocol.h */

Copied: gnunet/src/mesh/mesh_protocol.h (from rev 30671, 
gnunet/src/mesh/mesh_protocol_enc.h)
===================================================================
--- gnunet/src/mesh/mesh_protocol.h                             (rev 0)
+++ gnunet/src/mesh/mesh_protocol.h     2013-11-14 13:55:12 UTC (rev 30697)
@@ -0,0 +1,493 @@
+/*
+     This file is part of GNUnet.
+     (C) 2001 - 2011 Christian Grothoff (and other contributing authors)
+
+     GNUnet is free software; you can redistribute it and/or modify
+     it under the terms of the GNU General Public License as published
+     by the Free Software Foundation; either version 3, or (at your
+     option) any later version.
+
+     GNUnet is distributed in the hope that it will be useful, but
+     WITHOUT ANY WARRANTY; without even the implied warranty of
+     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+     General Public License for more details.
+
+     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.
+*/
+
+/**
+ * @author Bartlomiej Polot
+ * @file mesh/mesh_protocol.h
+ */
+
+#ifndef MESH_PROTOCOL_H_
+#define MESH_PROTOCOL_H_
+
+#include "platform.h"
+#include "gnunet_util_lib.h"
+#include "mesh.h"
+
+#ifdef __cplusplus
+
+struct GNUNET_MESH_TunnelMessage;
+extern "C"
+{
+#if 0
+  /* keep Emacsens' auto-indent happy */
+}
+#endif
+#endif
+
+/******************************************************************************/
+/********************      MESH NETWORK MESSAGES     
**************************/
+/******************************************************************************/
+
+GNUNET_NETWORK_STRUCT_BEGIN
+
+/**
+ * Message for mesh connection creation.
+ * TODO onionify path, add random tunnel ID
+ */
+struct GNUNET_MESH_ConnectionCreate
+{
+    /**
+     * Type: GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE
+     *
+     * Size: sizeof(struct GNUNET_MESH_ManipulatePath) +
+     *              path_length * sizeof (struct GNUNET_PeerIdentity)
+     */
+  struct GNUNET_MessageHeader header;
+
+    /**
+     * ID of the connection
+     */
+  struct GNUNET_HashCode cid;
+
+    /**
+     * path_length structs defining the *whole* path from the origin [0] to the
+     * final destination [path_length-1].
+     */
+  /* struct GNUNET_PeerIdentity peers[path_length]; */
+};
+
+/**
+ * Message for ack'ing a connection
+ */
+struct GNUNET_MESH_ConnectionACK
+{
+    /**
+     * Type: GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK
+     */
+  struct GNUNET_MessageHeader header;
+
+    /**
+     * Always 0.
+     */
+  uint32_t reserved GNUNET_PACKED;
+
+    /**
+     * ID of the connection.
+     */
+  struct GNUNET_HashCode cid;
+
+  /* TODO: signature */
+};
+
+
+/**
+ * Message for encapsulation of a Key eXchange message in a connection.
+ */
+struct GNUNET_MESH_KX
+{
+    /**
+     * Type: GNUNET_MESSAGE_TYPE_MESH_KX.
+     */
+  struct GNUNET_MessageHeader header;
+
+    /**
+     * Always 0.
+     */
+  uint32_t reserved GNUNET_PACKED;
+
+    /**
+     * ID of the connection.
+     */
+  struct GNUNET_HashCode cid;
+
+  /* Specific KX message follows. */
+};
+
+
+/**
+ * Message transmitted with the signed ephemeral key of a peer.  The
+ * session key is then derived from the two ephemeral keys (ECDHE).
+ *
+ * As far as possible, same as CORE's EphemeralKeyMessage.
+ */
+struct GNUNET_MESH_KX_Ephemeral
+{
+
+  /**
+   * Message type is GNUNET_MESSAGE_TYPE_MESH_KX_EPHEMERAL.
+   */
+  struct GNUNET_MessageHeader header;
+
+  /**
+   * Status of the sender (should be in "enum PeerStateMachine"), nbo.
+   */
+  int32_t sender_status GNUNET_PACKED;
+
+  /**
+   * An ECC signature of the 'origin' asserting the validity of
+   * the given ephemeral key.
+   */
+  struct GNUNET_CRYPTO_EddsaSignature signature;
+
+  /**
+   * Information about what is being signed.
+   */
+  struct GNUNET_CRYPTO_EccSignaturePurpose purpose;
+
+  /**
+   * At what time was this key created (beginning of validity).
+   */
+  struct GNUNET_TIME_AbsoluteNBO creation_time;
+
+  /**
+   * When does the given ephemeral key expire (end of validity).
+   */
+  struct GNUNET_TIME_AbsoluteNBO expiration_time;
+
+  /**
+   * Ephemeral public ECC key (always for NIST P-521) encoded in a format 
suitable
+   * for network transmission as created using 'gcry_sexp_sprint'.
+   */
+  struct GNUNET_CRYPTO_EcdhePublicKey ephemeral_key;
+
+  /**
+   * Public key of the signing peer (persistent version, not the ephemeral 
public key).
+   */
+  struct GNUNET_PeerIdentity origin_identity;
+};
+
+
+/**
+ * We're sending an (encrypted) PING to the other peer to check if he
+ * can decrypt.  The other peer should respond with a PONG with the
+ * same content, except this time encrypted with the receiver's key.
+ */
+struct GNUNET_MESH_KX_Ping
+{
+  /**
+   * Message type is GNUNET_MESSAGE_TYPE_MESH_KX_PING.
+   */
+  struct GNUNET_MessageHeader header;
+
+  /**
+   * Seed for the IV
+   */
+  uint32_t iv GNUNET_PACKED;
+
+  /**
+   * Intended target of the PING, used primarily to check
+   * that decryption actually worked.
+   */
+  struct GNUNET_PeerIdentity target;
+
+  /**
+   * Random number chosen to make reply harder.
+   */
+  uint32_t nonce GNUNET_PACKED;
+};
+
+
+/**
+ * Response to a PING.  Includes data from the original PING.
+ */
+struct GNUNET_MESH_KX_Pong
+{
+  /**
+   * Message type is GNUNET_MESSAGE_TYPE_MESH_KX_PONG.
+   */
+  struct GNUNET_MessageHeader header;
+
+  /**
+   * Seed for the IV
+   */
+  uint32_t iv GNUNET_PACKED;
+
+  /**
+   * Same nonce as in the reve.
+   */
+  uint32_t nonce GNUNET_PACKED;
+};
+
+
+/**
+ * Tunnel(ed) message.
+ */
+struct GNUNET_MESH_Encrypted
+{
+  /**
+   * Type: GNUNET_MESSAGE_TYPE_MESH_ENCRYPTED
+   */
+  struct GNUNET_MessageHeader header;
+
+  /**
+   * Initialization Vector for payload encryption.
+   */
+  uint32_t iv GNUNET_PACKED;
+
+  /**
+   * ID of the connection.
+   */
+  struct GNUNET_HashCode cid;
+
+  /**
+   * ID of the packet (hop by hop).
+   */
+  uint32_t pid GNUNET_PACKED;
+
+  /**
+   * Number of hops to live.
+   */
+  uint32_t ttl GNUNET_PACKED;
+
+  /**
+   * Encrypted content follows.
+   */
+};
+
+struct GNUNET_MESH_ChannelCreate
+{
+  /**
+   * Type: GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE
+   */
+  struct GNUNET_MessageHeader header;
+
+  /**
+   * ID of the channel
+   */
+  MESH_ChannelNumber chid GNUNET_PACKED;
+
+  /**
+   * Destination port.
+   */
+  uint32_t port GNUNET_PACKED;
+
+  /**
+   * Channel options.
+   */
+  uint32_t opt GNUNET_PACKED;
+};
+
+struct GNUNET_MESH_ChannelManage
+{
+  /**
+   * Type: GNUNET_MESSAGE_TYPE_MESH_CHANNEL_{ACK|NACK|DESTROY}
+   */
+  struct GNUNET_MessageHeader header;
+
+  /**
+   * ID of the channel
+   */
+  MESH_ChannelNumber chid GNUNET_PACKED;
+};
+
+/**
+ * Message for mesh data traffic.
+ */
+struct GNUNET_MESH_Data
+{
+    /**
+     * Type: GNUNET_MESSAGE_TYPE_MESH_UNICAST,
+     *       GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN
+     */
+  struct GNUNET_MessageHeader header;
+
+    /**
+     * Unique ID of the payload message
+     */
+  uint32_t mid GNUNET_PACKED;
+
+    /**
+     * ID of the channel
+     */
+  MESH_ChannelNumber chid GNUNET_PACKED;
+
+    /**
+     * Payload follows
+     */
+};
+
+
+/**
+ * Message to acknowledge end-to-end data.
+ */
+struct GNUNET_MESH_DataACK
+{
+  /**
+   * Type: GNUNET_MESSAGE_TYPE_MESH_DATA_ACK
+   */
+  struct GNUNET_MessageHeader header;
+
+  /**
+   * ID of the channel
+   */
+  MESH_ChannelNumber chid GNUNET_PACKED;
+
+  /**
+   * Bitfield of already-received newer messages
+   * pid +  1 @ LSB
+   * pid + 64 @ MSB
+   */
+  uint64_t futures GNUNET_PACKED;
+
+  /**
+   * Last message ID received.
+   */
+  uint32_t mid GNUNET_PACKED;
+};
+
+
+/**
+ * Message to acknowledge mesh encrypted traffic.
+ */
+struct GNUNET_MESH_ACK
+{
+    /**
+     * Type: GNUNET_MESSAGE_TYPE_MESH_ACK
+     */
+  struct GNUNET_MessageHeader header;
+
+    /**
+     * Maximum packet ID authorized.
+     */
+  uint32_t ack GNUNET_PACKED;
+
+    /**
+     * ID of the connection.
+     */
+  struct GNUNET_HashCode cid;
+};
+
+
+/**
+ * Message to query a peer about its Flow Control status regarding a tunnel.
+ */
+struct GNUNET_MESH_Poll
+{
+  /**
+   * Type: GNUNET_MESSAGE_TYPE_MESH_POLL
+   */
+  struct GNUNET_MessageHeader header;
+
+  /**
+   * Last packet sent.
+   */
+  uint32_t pid GNUNET_PACKED;
+
+    /**
+     * ID of the connection.
+     */
+  struct GNUNET_HashCode cid;
+
+};
+
+
+/**
+ * Message for notifying a disconnection in a path
+ */
+struct GNUNET_MESH_ConnectionBroken
+{
+    /**
+     * Type: GNUNET_MESSAGE_TYPE_MESH_CONNECTION_BROKEN
+     */
+  struct GNUNET_MessageHeader header;
+
+    /**
+     * Always 0.
+     */
+  uint32_t reserved GNUNET_PACKED;
+
+    /**
+     * ID of the connection.
+     */
+  struct GNUNET_HashCode cid;
+
+    /**
+     * ID of the endpoint
+     */
+  struct GNUNET_PeerIdentity peer1;
+
+    /**
+     * ID of the endpoint
+     */
+  struct GNUNET_PeerIdentity peer2;
+
+  /* TODO: signature */
+};
+
+
+/**
+ * Message to destroy a connection.
+ */
+struct GNUNET_MESH_ConnectionDestroy
+{
+    /**
+     * Type: GNUNET_MESSAGE_TYPE_MESH_CONNECTION_DESTROY
+     */
+  struct GNUNET_MessageHeader header;
+
+    /**
+     * Always 0.
+     */
+  uint32_t reserved GNUNET_PACKED;
+
+    /**
+     * ID of the connection.
+     */
+  struct GNUNET_HashCode cid;
+
+  /* TODO: signature */
+};
+
+
+/**
+ * Message to keep a connection alive.
+ */
+struct GNUNET_MESH_ConnectionKeepAlive
+{
+  /**
+   * Type: GNUNET_MESSAGE_TYPE_MESH_(FWD|BCK)_KEEPALIVE
+   */
+  struct GNUNET_MessageHeader header;
+
+  /**
+   * Always 0.
+   */
+  uint32_t reserved GNUNET_PACKED;
+
+  /**
+   * ID of the connection.
+   */
+  struct GNUNET_HashCode cid;
+};
+
+
+
+GNUNET_NETWORK_STRUCT_END
+
+#if 0                           /* keep Emacsens' auto-indent happy */
+{
+#endif
+#ifdef __cplusplus
+}
+#endif
+
+/* ifndef MESH_PROTOCOL_H */
+#endif
+/* end of mesh_protocol.h */

Deleted: gnunet/src/mesh/mesh_protocol_enc.h
===================================================================
--- gnunet/src/mesh/mesh_protocol_enc.h 2013-11-14 13:30:29 UTC (rev 30696)
+++ gnunet/src/mesh/mesh_protocol_enc.h 2013-11-14 13:55:12 UTC (rev 30697)
@@ -1,493 +0,0 @@
-/*
-     This file is part of GNUnet.
-     (C) 2001 - 2011 Christian Grothoff (and other contributing authors)
-
-     GNUnet is free software; you can redistribute it and/or modify
-     it under the terms of the GNU General Public License as published
-     by the Free Software Foundation; either version 3, or (at your
-     option) any later version.
-
-     GNUnet is distributed in the hope that it will be useful, but
-     WITHOUT ANY WARRANTY; without even the implied warranty of
-     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
-     General Public License for more details.
-
-     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.
-*/
-
-/**
- * @author Bartlomiej Polot
- * @file mesh/mesh_protocol_enc.h
- */
-
-#ifndef MESH_PROTOCOL_ENC_H_
-#define MESH_PROTOCOL_ENC_H_
-
-#include "platform.h"
-#include "gnunet_util_lib.h"
-#include "mesh_enc.h"
-
-#ifdef __cplusplus
-
-struct GNUNET_MESH_TunnelMessage;
-extern "C"
-{
-#if 0
-  /* keep Emacsens' auto-indent happy */
-}
-#endif
-#endif
-
-/******************************************************************************/
-/********************      MESH NETWORK MESSAGES     
**************************/
-/******************************************************************************/
-
-GNUNET_NETWORK_STRUCT_BEGIN
-
-/**
- * Message for mesh connection creation.
- * TODO onionify path, add random tunnel ID
- */
-struct GNUNET_MESH_ConnectionCreate
-{
-    /**
-     * Type: GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE
-     *
-     * Size: sizeof(struct GNUNET_MESH_ManipulatePath) +
-     *              path_length * sizeof (struct GNUNET_PeerIdentity)
-     */
-  struct GNUNET_MessageHeader header;
-
-    /**
-     * ID of the connection
-     */
-  struct GNUNET_HashCode cid;
-
-    /**
-     * path_length structs defining the *whole* path from the origin [0] to the
-     * final destination [path_length-1].
-     */
-  /* struct GNUNET_PeerIdentity peers[path_length]; */
-};
-
-/**
- * Message for ack'ing a connection
- */
-struct GNUNET_MESH_ConnectionACK
-{
-    /**
-     * Type: GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK
-     */
-  struct GNUNET_MessageHeader header;
-
-    /**
-     * Always 0.
-     */
-  uint32_t reserved GNUNET_PACKED;
-
-    /**
-     * ID of the connection.
-     */
-  struct GNUNET_HashCode cid;
-
-  /* TODO: signature */
-};
-
-
-/**
- * Message for encapsulation of a Key eXchange message in a connection.
- */
-struct GNUNET_MESH_KX
-{
-    /**
-     * Type: GNUNET_MESSAGE_TYPE_MESH_KX.
-     */
-  struct GNUNET_MessageHeader header;
-
-    /**
-     * Always 0.
-     */
-  uint32_t reserved GNUNET_PACKED;
-
-    /**
-     * ID of the connection.
-     */
-  struct GNUNET_HashCode cid;
-
-  /* Specific KX message follows. */
-};
-
-
-/**
- * Message transmitted with the signed ephemeral key of a peer.  The
- * session key is then derived from the two ephemeral keys (ECDHE).
- *
- * As far as possible, same as CORE's EphemeralKeyMessage.
- */
-struct GNUNET_MESH_KX_Ephemeral
-{
-
-  /**
-   * Message type is GNUNET_MESSAGE_TYPE_MESH_KX_EPHEMERAL.
-   */
-  struct GNUNET_MessageHeader header;
-
-  /**
-   * Status of the sender (should be in "enum PeerStateMachine"), nbo.
-   */
-  int32_t sender_status GNUNET_PACKED;
-
-  /**
-   * An ECC signature of the 'origin' asserting the validity of
-   * the given ephemeral key.
-   */
-  struct GNUNET_CRYPTO_EddsaSignature signature;
-
-  /**
-   * Information about what is being signed.
-   */
-  struct GNUNET_CRYPTO_EccSignaturePurpose purpose;
-
-  /**
-   * At what time was this key created (beginning of validity).
-   */
-  struct GNUNET_TIME_AbsoluteNBO creation_time;
-
-  /**
-   * When does the given ephemeral key expire (end of validity).
-   */
-  struct GNUNET_TIME_AbsoluteNBO expiration_time;
-
-  /**
-   * Ephemeral public ECC key (always for NIST P-521) encoded in a format 
suitable
-   * for network transmission as created using 'gcry_sexp_sprint'.
-   */
-  struct GNUNET_CRYPTO_EcdhePublicKey ephemeral_key;
-
-  /**
-   * Public key of the signing peer (persistent version, not the ephemeral 
public key).
-   */
-  struct GNUNET_PeerIdentity origin_identity;
-};
-
-
-/**
- * We're sending an (encrypted) PING to the other peer to check if he
- * can decrypt.  The other peer should respond with a PONG with the
- * same content, except this time encrypted with the receiver's key.
- */
-struct GNUNET_MESH_KX_Ping
-{
-  /**
-   * Message type is GNUNET_MESSAGE_TYPE_MESH_KX_PING.
-   */
-  struct GNUNET_MessageHeader header;
-
-  /**
-   * Seed for the IV
-   */
-  uint32_t iv GNUNET_PACKED;
-
-  /**
-   * Intended target of the PING, used primarily to check
-   * that decryption actually worked.
-   */
-  struct GNUNET_PeerIdentity target;
-
-  /**
-   * Random number chosen to make reply harder.
-   */
-  uint32_t nonce GNUNET_PACKED;
-};
-
-
-/**
- * Response to a PING.  Includes data from the original PING.
- */
-struct GNUNET_MESH_KX_Pong
-{
-  /**
-   * Message type is GNUNET_MESSAGE_TYPE_MESH_KX_PONG.
-   */
-  struct GNUNET_MessageHeader header;
-
-  /**
-   * Seed for the IV
-   */
-  uint32_t iv GNUNET_PACKED;
-
-  /**
-   * Same nonce as in the reve.
-   */
-  uint32_t nonce GNUNET_PACKED;
-};
-
-
-/**
- * Tunnel(ed) message.
- */
-struct GNUNET_MESH_Encrypted
-{
-  /**
-   * Type: GNUNET_MESSAGE_TYPE_MESH_ENCRYPTED
-   */
-  struct GNUNET_MessageHeader header;
-
-  /**
-   * Initialization Vector for payload encryption.
-   */
-  uint32_t iv GNUNET_PACKED;
-
-  /**
-   * ID of the connection.
-   */
-  struct GNUNET_HashCode cid;
-
-  /**
-   * ID of the packet (hop by hop).
-   */
-  uint32_t pid GNUNET_PACKED;
-
-  /**
-   * Number of hops to live.
-   */
-  uint32_t ttl GNUNET_PACKED;
-
-  /**
-   * Encrypted content follows.
-   */
-};
-
-struct GNUNET_MESH_ChannelCreate
-{
-  /**
-   * Type: GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE
-   */
-  struct GNUNET_MessageHeader header;
-
-  /**
-   * ID of the channel
-   */
-  MESH_ChannelNumber chid GNUNET_PACKED;
-
-  /**
-   * Destination port.
-   */
-  uint32_t port GNUNET_PACKED;
-
-  /**
-   * Channel options.
-   */
-  uint32_t opt GNUNET_PACKED;
-};
-
-struct GNUNET_MESH_ChannelManage
-{
-  /**
-   * Type: GNUNET_MESSAGE_TYPE_MESH_CHANNEL_{ACK|NACK|DESTROY}
-   */
-  struct GNUNET_MessageHeader header;
-
-  /**
-   * ID of the channel
-   */
-  MESH_ChannelNumber chid GNUNET_PACKED;
-};
-
-/**
- * Message for mesh data traffic.
- */
-struct GNUNET_MESH_Data
-{
-    /**
-     * Type: GNUNET_MESSAGE_TYPE_MESH_UNICAST,
-     *       GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN
-     */
-  struct GNUNET_MessageHeader header;
-
-    /**
-     * Unique ID of the payload message
-     */
-  uint32_t mid GNUNET_PACKED;
-
-    /**
-     * ID of the channel
-     */
-  MESH_ChannelNumber chid GNUNET_PACKED;
-
-    /**
-     * Payload follows
-     */
-};
-
-
-/**
- * Message to acknowledge end-to-end data.
- */
-struct GNUNET_MESH_DataACK
-{
-  /**
-   * Type: GNUNET_MESSAGE_TYPE_MESH_DATA_ACK
-   */
-  struct GNUNET_MessageHeader header;
-
-  /**
-   * ID of the channel
-   */
-  MESH_ChannelNumber chid GNUNET_PACKED;
-
-  /**
-   * Bitfield of already-received newer messages
-   * pid +  1 @ LSB
-   * pid + 64 @ MSB
-   */
-  uint64_t futures GNUNET_PACKED;
-
-  /**
-   * Last message ID received.
-   */
-  uint32_t mid GNUNET_PACKED;
-};
-
-
-/**
- * Message to acknowledge mesh encrypted traffic.
- */
-struct GNUNET_MESH_ACK
-{
-    /**
-     * Type: GNUNET_MESSAGE_TYPE_MESH_ACK
-     */
-  struct GNUNET_MessageHeader header;
-
-    /**
-     * Maximum packet ID authorized.
-     */
-  uint32_t ack GNUNET_PACKED;
-
-    /**
-     * ID of the connection.
-     */
-  struct GNUNET_HashCode cid;
-};
-
-
-/**
- * Message to query a peer about its Flow Control status regarding a tunnel.
- */
-struct GNUNET_MESH_Poll
-{
-  /**
-   * Type: GNUNET_MESSAGE_TYPE_MESH_POLL
-   */
-  struct GNUNET_MessageHeader header;
-
-  /**
-   * Last packet sent.
-   */
-  uint32_t pid GNUNET_PACKED;
-
-    /**
-     * ID of the connection.
-     */
-  struct GNUNET_HashCode cid;
-
-};
-
-
-/**
- * Message for notifying a disconnection in a path
- */
-struct GNUNET_MESH_ConnectionBroken
-{
-    /**
-     * Type: GNUNET_MESSAGE_TYPE_MESH_CONNECTION_BROKEN
-     */
-  struct GNUNET_MessageHeader header;
-
-    /**
-     * Always 0.
-     */
-  uint32_t reserved GNUNET_PACKED;
-
-    /**
-     * ID of the connection.
-     */
-  struct GNUNET_HashCode cid;
-
-    /**
-     * ID of the endpoint
-     */
-  struct GNUNET_PeerIdentity peer1;
-
-    /**
-     * ID of the endpoint
-     */
-  struct GNUNET_PeerIdentity peer2;
-
-  /* TODO: signature */
-};
-
-
-/**
- * Message to destroy a connection.
- */
-struct GNUNET_MESH_ConnectionDestroy
-{
-    /**
-     * Type: GNUNET_MESSAGE_TYPE_MESH_CONNECTION_DESTROY
-     */
-  struct GNUNET_MessageHeader header;
-
-    /**
-     * Always 0.
-     */
-  uint32_t reserved GNUNET_PACKED;
-
-    /**
-     * ID of the connection.
-     */
-  struct GNUNET_HashCode cid;
-
-  /* TODO: signature */
-};
-
-
-/**
- * Message to keep a connection alive.
- */
-struct GNUNET_MESH_ConnectionKeepAlive
-{
-  /**
-   * Type: GNUNET_MESSAGE_TYPE_MESH_(FWD|BCK)_KEEPALIVE
-   */
-  struct GNUNET_MessageHeader header;
-
-  /**
-   * Always 0.
-   */
-  uint32_t reserved GNUNET_PACKED;
-
-  /**
-   * ID of the connection.
-   */
-  struct GNUNET_HashCode cid;
-};
-
-
-
-GNUNET_NETWORK_STRUCT_END
-
-#if 0                           /* keep Emacsens' auto-indent happy */
-{
-#endif
-#ifdef __cplusplus
-}
-#endif
-
-/* ifndef MESH_PROTOCOL_H */
-#endif
-/* end of mesh_protocol.h */

Modified: gnunet/src/mesh/mesh_test_lib.c
===================================================================
--- gnunet/src/mesh/mesh_test_lib.c     2013-11-14 13:30:29 UTC (rev 30696)
+++ gnunet/src/mesh/mesh_test_lib.c     2013-11-14 13:55:12 UTC (rev 30697)
@@ -25,7 +25,7 @@
 #include "platform.h"
 #include "gnunet_util_lib.h"
 #include "mesh_test_lib.h"
-#include "gnunet_mesh_service_enc.h"
+#include "gnunet_mesh_service.h"
 
 /**
  * Test context for a MESH Test.

Modified: gnunet/src/mesh/mesh_test_lib.h
===================================================================
--- gnunet/src/mesh/mesh_test_lib.h     2013-11-14 13:30:29 UTC (rev 30696)
+++ gnunet/src/mesh/mesh_test_lib.h     2013-11-14 13:55:12 UTC (rev 30697)
@@ -34,7 +34,7 @@
 #endif
 
 #include "gnunet_testbed_service.h"
-#include "gnunet_mesh_service_enc.h"
+#include "gnunet_mesh_service.h"
 
 /**
  * Test context for a MESH Test.

Modified: gnunet/src/mesh/test_mesh_local.c
===================================================================
--- gnunet/src/mesh/test_mesh_local.c   2013-11-14 13:30:29 UTC (rev 30696)
+++ gnunet/src/mesh/test_mesh_local.c   2013-11-14 13:55:12 UTC (rev 30697)
@@ -28,7 +28,7 @@
 #include "gnunet_util_lib.h"
 #include "gnunet_dht_service.h"
 #include "gnunet_testing_lib.h"
-#include "gnunet_mesh_service_enc.h"
+#include "gnunet_mesh_service.h"
 
 struct GNUNET_TESTING_Peer *me;
 

Modified: gnunet/src/mesh/test_mesh_single.c
===================================================================
--- gnunet/src/mesh/test_mesh_single.c  2013-11-14 13:30:29 UTC (rev 30696)
+++ gnunet/src/mesh/test_mesh_single.c  2013-11-14 13:55:12 UTC (rev 30697)
@@ -28,7 +28,7 @@
 #include "gnunet_util_lib.h"
 #include "gnunet_dht_service.h"
 #include "gnunet_testing_lib.h"
-#include "gnunet_mesh_service_enc.h"
+#include "gnunet_mesh_service.h"
 
 #define REPETITIONS 5
 #define DATA_SIZE 35000

Modified: gnunet/src/mesh/test_mesh_small.c
===================================================================
--- gnunet/src/mesh/test_mesh_small.c   2013-11-14 13:30:29 UTC (rev 30696)
+++ gnunet/src/mesh/test_mesh_small.c   2013-11-14 13:55:12 UTC (rev 30697)
@@ -25,7 +25,7 @@
 #include <stdio.h>
 #include "platform.h"
 #include "mesh_test_lib.h"
-#include "gnunet_mesh_service_enc.h"
+#include "gnunet_mesh_service.h"
 #include <gauger.h>
 
 

Modified: gnunet/src/pt/gnunet-daemon-pt.c
===================================================================
--- gnunet/src/pt/gnunet-daemon-pt.c    2013-11-14 13:30:29 UTC (rev 30696)
+++ gnunet/src/pt/gnunet-daemon-pt.c    2013-11-14 13:55:12 UTC (rev 30697)
@@ -51,7 +51,7 @@
 #define MAX_DNS_SIZE (8 * 1024)
 
 /**
- * How many tunnels do we open at most at the same time?
+ * How many channels do we open at most at the same time?
  */
 #define MAX_OPEN_TUNNELS 4
 
@@ -123,7 +123,7 @@
 
 /**
  * Handle to a peer that advertised that it is willing to serve
- * as a DNS exit.  We try to keep a few tunnels open and a few
+ * as a DNS exit.  We try to keep a few channels open and a few
  * peers in reserve.
  */
 struct MeshExit
@@ -140,10 +140,10 @@
   struct MeshExit *prev;
 
   /**
-   * Tunnel we use for DNS requests over MESH, NULL if we did
-   * not initialze a tunnel to this peer yet.
+   * Channel we use for DNS requests over MESH, NULL if we did
+   * not initialze a channel to this peer yet.
    */
-  struct GNUNET_MESH_Tunnel *mesh_tunnel;
+  struct GNUNET_MESH_Channel *mesh_channel;
 
   /**
    * At what time did the peer's advertisement expire?
@@ -161,17 +161,17 @@
   struct RequestContext *receive_queue_tail;
 
   /**
-   * Head of DLL of requests to be transmitted to a mesh_tunnel.
+   * Head of DLL of requests to be transmitted to a mesh_channel.
    */
   struct RequestContext *transmit_queue_head;
 
   /**
-   * Tail of DLL of requests to be transmitted to a mesh_tunnel.
+   * Tail of DLL of requests to be transmitted to a mesh_channel.
    */
   struct RequestContext *transmit_queue_tail;
 
   /**
-   * Active transmission request for this tunnel (or NULL).
+   * Active transmission request for this channel (or NULL).
    */
   struct GNUNET_MESH_TransmitHandle *mesh_th;
 
@@ -181,12 +181,12 @@
   struct GNUNET_PeerIdentity peer;
 
   /**
-   * How many DNS requests did we transmit via this tunnel?
+   * How many DNS requests did we transmit via this channel?
    */
   unsigned int num_transmitted;
 
   /**
-   * How many DNS requests were answered via this tunnel?
+   * How many DNS requests were answered via this channel?
    */
   unsigned int num_answered;
 
@@ -250,7 +250,7 @@
 
 
 /**
- * Head of DLL of mesh exits.  Mesh exits with an open tunnel are
+ * Head of DLL of mesh exits.  Mesh exits with an open channel are
  * always at the beginning (so we do not have to traverse the entire
  * list to find them).
  */
@@ -312,14 +312,14 @@
 static int ipv6_pt;
 
 /**
- * Are we tunneling DNS queries?
+ * Are we channeling DNS queries?
  */
-static int dns_tunnel;
+static int dns_channel;
 
 /**
- * Number of DNS exit peers we currently have in the mesh tunnel.
- * Used to see if using the mesh tunnel makes any sense right now,
- * as well as to decide if we should open new tunnels.
+ * Number of DNS exit peers we currently have in the mesh channel.
+ * Used to see if using the mesh channel makes any sense right now,
+ * as well as to decide if we should open new channels.
  */
 static unsigned int dns_exit_available;
 
@@ -336,25 +336,25 @@
 
   candidate_count = 0;
   for (pos = exit_head; NULL != pos; pos = pos->next)
-    if (NULL == pos->mesh_tunnel)
+    if (NULL == pos->mesh_channel)
       candidate_count++;
   candidate_selected = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
                                                 candidate_count);
   candidate_count = 0;
   for (pos = exit_head; NULL != pos; pos = pos->next)
-    if (NULL == pos->mesh_tunnel)
+    if (NULL == pos->mesh_channel)
     {
       candidate_count++;
       if (candidate_selected < candidate_count)
       {
        /* move to the head of the DLL */
-       pos->mesh_tunnel = GNUNET_MESH_tunnel_create (mesh_handle,
+       pos->mesh_channel = GNUNET_MESH_channel_create (mesh_handle,
                                                      pos,
                                                      &pos->peer,
                                                      
GNUNET_APPLICATION_TYPE_INTERNET_RESOLVER,
                                                      GNUNET_YES /* no buffer 
*/,
                                                      GNUNET_NO /* reliable */);
-       if (NULL == pos->mesh_tunnel)
+       if (NULL == pos->mesh_channel)
        {
          GNUNET_break (0);
          continue;
@@ -375,15 +375,15 @@
 
 /**
  * Compute the weight of the given exit.  The higher the weight,
- * the more likely it will be that the tunnel will be chosen.
- * A weigt of zero means that we should close the tunnel as it
+ * the more likely it will be that the channel will be chosen.
+ * A weigt of zero means that we should close the channel as it
  * is so bad, that we should not use it.
  *
  * @param exit exit to calculate the weight for
- * @return weight of the tunnel
+ * @return weight of the channel
  */
 static uint32_t
-get_tunnel_weight (struct MeshExit *exit)
+get_channel_weight (struct MeshExit *exit)
 {
   uint32_t dropped;
   uint32_t drop_percent;
@@ -407,12 +407,12 @@
 
 
 /**
- * Choose a mesh exit for a DNS request.  We try to use a tunnel
+ * Choose a mesh exit for a DNS request.  We try to use a channel
  * that is reliable and currently available.  All existing
- * tunnels are given a base weight of 1, plus a score relating
+ * channels are given a base weight of 1, plus a score relating
  * to the total number of queries answered in relation to the
- * total number of queries we sent to that tunnel.  That
- * score is doubled if the tunnel is currently idle.
+ * total number of queries we sent to that channel.  That
+ * score is doubled if the channel is currently idle.
  *
  * @return NULL if no exit is known, otherwise the
  *         exit that we should use to queue a message with
@@ -423,22 +423,22 @@
   struct MeshExit *pos;
   uint64_t total_transmitted;
   uint64_t selected_offset;
-  uint32_t tunnel_weight;
+  uint32_t channel_weight;
 
   total_transmitted = 0;
   for (pos = exit_head; NULL != pos; pos = pos->next)
   {
-    if (NULL == pos->mesh_tunnel)
+    if (NULL == pos->mesh_channel)
       break;
-    tunnel_weight = get_tunnel_weight (pos);
-    total_transmitted += tunnel_weight;
-    /* double weight for idle tunnels */
+    channel_weight = get_channel_weight (pos);
+    total_transmitted += channel_weight;
+    /* double weight for idle channels */
     if (NULL == pos->mesh_th)
-      total_transmitted += tunnel_weight;
+      total_transmitted += channel_weight;
   }
   if (0 == total_transmitted)
   {
-    /* no tunnels available, or only a very bad one... */
+    /* no channels available, or only a very bad one... */
     return exit_head;
   }
   selected_offset = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
@@ -446,13 +446,13 @@
   total_transmitted = 0;
   for (pos = exit_head; NULL != pos; pos = pos->next)
   {
-    if (NULL == pos->mesh_tunnel)
+    if (NULL == pos->mesh_channel)
       break;
-    tunnel_weight = get_tunnel_weight (pos);
-    total_transmitted += tunnel_weight;
-    /* double weight for idle tunnels */
+    channel_weight = get_channel_weight (pos);
+    total_transmitted += channel_weight;
+    /* double weight for idle channels */
     if (NULL == pos->mesh_th)
-      total_transmitted += tunnel_weight;
+      total_transmitted += channel_weight;
     if (total_transmitted > selected_offset)
       return pos;
   }
@@ -561,7 +561,7 @@
 
 
 /**
- * Modify the given DNS record by asking VPN to create a tunnel
+ * Modify the given DNS record by asking VPN to create a channel
  * to the given address.  When done, continue with submitting
  * other records from the request context ('submit_request' is
  * our continuation).
@@ -696,7 +696,7 @@
 /**
  * This function is called AFTER we got an IP address for a
  * DNS request.  Now, the PT daemon has the chance to substitute
- * the IP address with one from the VPN range to tunnel requests
+ * the IP address with one from the VPN range to channel requests
  * destined for this IP address via VPN and MESH.
  *
  * @param cls closure
@@ -768,7 +768,7 @@
   mlen = rc->mlen;
   if (mlen > size)
   {
-    exit->mesh_th = GNUNET_MESH_notify_transmit_ready (exit->mesh_tunnel,
+    exit->mesh_th = GNUNET_MESH_notify_transmit_ready (exit->mesh_channel,
                                                       GNUNET_NO,
                                                       TIMEOUT,
                                                       mlen,
@@ -787,7 +787,7 @@
                               rc);
   rc = exit->transmit_queue_head;
   if (NULL != rc)
-    exit->mesh_th = GNUNET_MESH_notify_transmit_ready (exit->mesh_tunnel,
+    exit->mesh_th = GNUNET_MESH_notify_transmit_ready (exit->mesh_channel,
                                                       GNUNET_NO,
                                                       TIMEOUT,
                                                       rc->mlen,
@@ -828,15 +828,15 @@
                            1, GNUNET_NO);
   GNUNET_DNS_request_drop (rc->rh);
   GNUNET_free (rc);
-  if ( (0 == get_tunnel_weight (exit)) &&
+  if ( (0 == get_channel_weight (exit)) &&
        (NULL == exit->receive_queue_head) &&
        (NULL == exit->transmit_queue_head) )
   {
-    /* this straw broke the camel's back: this tunnel now has
+    /* this straw broke the camel's back: this channel now has
        such a low score that it will not be used; close it! */
     GNUNET_assert (NULL == exit->mesh_th);
-    GNUNET_MESH_tunnel_destroy (exit->mesh_tunnel);
-    exit->mesh_tunnel = NULL;
+    GNUNET_MESH_channel_destroy (exit->mesh_channel);
+    exit->mesh_channel = NULL;
     GNUNET_CONTAINER_DLL_remove (exit_head,
                                 exit_tail,
                                 exit);
@@ -845,7 +845,7 @@
                                      exit);
     /* go back to semi-innocent: mark as not great, but
        avoid a prohibitively negative score (see
-       #get_tunnel_weight, which checks for a certain
+       #get_channel_weight, which checks for a certain
        minimum number of transmissions before making
        up an opinion) */
     exit->num_transmitted = 5;
@@ -859,9 +859,9 @@
 
 /**
  * This function is called *before* the DNS request has been
- * given to a "local" DNS resolver.  Tunneling for DNS requests
+ * given to a "local" DNS resolver.  Channeling for DNS requests
  * was enabled, so we now need to send the request via some MESH
- * tunnel to a DNS EXIT for resolution.
+ * channel to a DNS EXIT for resolution.
  *
  * @param cls closure
  * @param rh request handle to user for reply
@@ -886,7 +886,7 @@
   if (0 == dns_exit_available)
   {
     GNUNET_STATISTICS_update (stats,
-                             gettext_noop ("# DNS requests dropped (DNS mesh 
tunnel down)"),
+                             gettext_noop ("# DNS requests dropped (DNS mesh 
channel down)"),
                              1, GNUNET_NO);
     GNUNET_DNS_request_drop (rh);
     return;
@@ -903,7 +903,7 @@
   mlen = sizeof (struct GNUNET_MessageHeader) + request_length;
   exit = choose_exit ();
   GNUNET_assert (NULL != exit);
-  GNUNET_assert (NULL != exit->mesh_tunnel);
+  GNUNET_assert (NULL != exit->mesh_channel);
   rc = GNUNET_malloc (sizeof (struct RequestContext) + mlen);
   rc->exit = exit;
   rc->rh = rh;
@@ -923,7 +923,7 @@
                                    exit->transmit_queue_tail,
                                    rc);
   if (NULL == exit->mesh_th)
-    exit->mesh_th = GNUNET_MESH_notify_transmit_ready (exit->mesh_tunnel,
+    exit->mesh_th = GNUNET_MESH_notify_transmit_ready (exit->mesh_channel,
                                                       GNUNET_NO,
                                                       TIMEOUT,
                                                       mlen,
@@ -936,19 +936,19 @@
  * Process a request via mesh to perform a DNS query.
  *
  * @param cls NULL
- * @param tunnel connection to the other end
- * @param tunnel_ctx pointer to our `struct MeshExit`
+ * @param channel connection to the other end
+ * @param channel_ctx pointer to our `struct MeshExit`
  * @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_response (void *cls,
-                     struct GNUNET_MESH_Tunnel *tunnel,
-                     void **tunnel_ctx,
+                     struct GNUNET_MESH_Channel *channel,
+                     void **channel_ctx,
                      const struct GNUNET_MessageHeader *message)
 {
-  struct MeshExit *exit = *tunnel_ctx;
+  struct MeshExit *exit = *channel_ctx;
   struct GNUNET_TUN_DnsHeader dns;
   size_t mlen;
   struct RequestContext *rc;
@@ -1049,10 +1049,10 @@
       GNUNET_MESH_notify_transmit_ready_cancel (exit->mesh_th);
       exit->mesh_th = NULL;
     }
-    if (NULL != exit->mesh_tunnel)
+    if (NULL != exit->mesh_channel)
     {
-      GNUNET_MESH_tunnel_destroy (exit->mesh_tunnel);
-      exit->mesh_tunnel = NULL;
+      GNUNET_MESH_channel_destroy (exit->mesh_channel);
+      exit->mesh_channel = NULL;
     }
     abort_all_requests (exit);
     GNUNET_free (exit);
@@ -1091,22 +1091,22 @@
 
 
 /**
- * Function called whenever a tunnel is destroyed.  Should clean up
+ * Function called whenever a channel is destroyed.  Should clean up
  * the associated state and attempt to build a new one.
  *
- * It must NOT call #GNUNET_MESH_tunnel_destroy on the tunnel.
+ * It must NOT call #GNUNET_MESH_channel_destroy on the channel.
  *
  * @param cls closure (the `struct MeshExit` set from #GNUNET_MESH_connect)
- * @param tunnel connection to the other end (henceforth invalid)
- * @param tunnel_ctx place where local state associated
- *                   with the tunnel is stored
+ * @param channel connection to the other end (henceforth invalid)
+ * @param channel_ctx place where local state associated
+ *                   with the channel is stored
  */
 static void
-mesh_tunnel_end_cb (void *cls,
-                   const struct GNUNET_MESH_Tunnel *tunnel,
-                   void *tunnel_ctx)
+mesh_channel_end_cb (void *cls,
+                   const struct GNUNET_MESH_Channel *channel,
+                   void *channel_ctx)
 {
-  struct MeshExit *exit = tunnel_ctx;
+  struct MeshExit *exit = channel_ctx;
   struct MeshExit *alt;
   struct RequestContext *rc;
 
@@ -1115,14 +1115,14 @@
     GNUNET_MESH_notify_transmit_ready_cancel (exit->mesh_th);
     exit->mesh_th = NULL;
   }
-  exit->mesh_tunnel = NULL;
+  exit->mesh_channel = NULL;
   dns_exit_available--;
-  /* open alternative tunnels */
+  /* open alternative channels */
   try_open_exit ();
-  if (NULL == exit->mesh_tunnel)
+  if (NULL == exit->mesh_channel)
   {
-    /* our tunnel is now closed, move our requests to an alternative
-       tunnel */
+    /* our channel is now closed, move our requests to an alternative
+       channel */
     alt = choose_exit ();
     while (NULL != (rc = exit->transmit_queue_head))
     {
@@ -1153,7 +1153,7 @@
   }
   if ( (NULL == alt->mesh_th) &&
        (NULL != (rc = alt->transmit_queue_head)) )
-    alt->mesh_th = GNUNET_MESH_notify_transmit_ready (alt->mesh_tunnel,
+    alt->mesh_th = GNUNET_MESH_notify_transmit_ready (alt->mesh_channel,
                                                      GNUNET_NO,
                                                      TIMEOUT,
                                                      rc->mlen,
@@ -1164,7 +1164,7 @@
 
 /**
  * Function called whenever we find an advertisement for a
- * DNS exit in the DHT.  If we don't have a mesh tunnel,
+ * DNS exit in the DHT.  If we don't have a mesh channel,
  * we should build one; otherwise, we should save the
  * advertisement for later use.
  *
@@ -1210,7 +1210,7 @@
   {
     exit = GNUNET_new (struct MeshExit);
     exit->peer = ad->peer;
-    /* tunnel is closed, so insert at the end */
+    /* channel is closed, so insert at the end */
     GNUNET_CONTAINER_DLL_insert_tail (exit_head,
                                      exit_tail,
                                      exit);
@@ -1241,8 +1241,8 @@
   stats = GNUNET_STATISTICS_create ("pt", cfg);
   ipv4_pt = GNUNET_CONFIGURATION_get_value_yesno (cfg, "pt", "TUNNEL_IPV4");
   ipv6_pt = GNUNET_CONFIGURATION_get_value_yesno (cfg, "pt", "TUNNEL_IPV6");
-  dns_tunnel = GNUNET_CONFIGURATION_get_value_yesno (cfg, "pt", "TUNNEL_DNS");
-  if (! (ipv4_pt || ipv6_pt || dns_tunnel))
+  dns_channel = GNUNET_CONFIGURATION_get_value_yesno (cfg, "pt", "TUNNEL_DNS");
+  if (! (ipv4_pt || ipv6_pt || dns_channel))
   {
     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                _("No useful service enabled.  Exiting.\n"));
@@ -1274,7 +1274,7 @@
       return;
     }
   }
-  if (dns_tunnel)
+  if (dns_channel)
   {
     static struct GNUNET_MESH_MessageHandler mesh_handlers[] = {
       {&receive_dns_response, GNUNET_MESSAGE_TYPE_VPN_DNS_FROM_INTERNET, 0},
@@ -1294,7 +1294,7 @@
       return;
     }
     mesh_handle = GNUNET_MESH_connect (cfg, NULL, NULL,
-                                      &mesh_tunnel_end_cb,
+                                      &mesh_channel_end_cb,
                                       mesh_handlers, NULL);
     if (NULL == mesh_handle)
     {

Modified: gnunet/src/scalarproduct/gnunet-service-scalarproduct.c
===================================================================
--- gnunet/src/scalarproduct/gnunet-service-scalarproduct.c     2013-11-14 
13:30:29 UTC (rev 30696)
+++ gnunet/src/scalarproduct/gnunet-service-scalarproduct.c     2013-11-14 
13:55:12 UTC (rev 30697)
@@ -203,9 +203,9 @@
   struct GNUNET_SERVER_TransmitHandle * client_transmit_handle;
 
   /**
-   * tunnel-handle associated with our mesh handle
+   * channel-handle associated with our mesh handle
    */
-  struct GNUNET_MESH_Tunnel * tunnel;
+  struct GNUNET_MESH_Channel * channel;
 
   /**
    * Handle to a task that sends a msg to the our client
@@ -783,10 +783,10 @@
 
   if (!(session->role == BOB && session->state == FINALIZED)) {
     //we MUST terminate any client message underway
-    if (session->service_transmit_handle && session->tunnel)
+    if (session->service_transmit_handle && session->channel)
       GNUNET_MESH_notify_transmit_ready_cancel 
(session->service_transmit_handle);
-    if (session->tunnel && session->state == WAITING_FOR_SERVICE_RESPONSE)
-      GNUNET_MESH_tunnel_destroy (session->tunnel);
+    if (session->channel && session->state == WAITING_FOR_SERVICE_RESPONSE)
+      GNUNET_MESH_channel_destroy (session->channel);
   }
   if (GNUNET_SCHEDULER_NO_TASK != session->client_notification_task) {
     GNUNET_SCHEDULER_cancel (session->client_notification_task);
@@ -1014,7 +1014,7 @@
   session->transferred += todo_count;
   session->msg = (struct GNUNET_MessageHeader *) msg;
   session->service_transmit_handle =
-          GNUNET_MESH_notify_transmit_ready (session->tunnel,
+          GNUNET_MESH_notify_transmit_ready (session->channel,
                                              GNUNET_YES,
                                              GNUNET_TIME_UNIT_FOREVER_REL,
                                              msg_length,
@@ -1152,7 +1152,7 @@
 
   session->msg = (struct GNUNET_MessageHeader *) msg;
   session->service_transmit_handle =
-          GNUNET_MESH_notify_transmit_ready (session->tunnel,
+          GNUNET_MESH_notify_transmit_ready (session->channel,
                                              GNUNET_YES,
                                              GNUNET_TIME_UNIT_FOREVER_REL,
                                              msg_length,
@@ -1476,13 +1476,13 @@
   GNUNET_log (GNUNET_ERROR_TYPE_INFO, _ ("Transmitting service request.\n"));
 
   //transmit via mesh messaging
-  session->service_transmit_handle = GNUNET_MESH_notify_transmit_ready 
(session->tunnel, GNUNET_YES,
+  session->service_transmit_handle = GNUNET_MESH_notify_transmit_ready 
(session->channel, GNUNET_YES,
                                                                         
GNUNET_TIME_UNIT_FOREVER_REL,
                                                                         
msg_length,
                                                                         
&do_send_message,
                                                                         
session);
   if (!session->service_transmit_handle) {
-    GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _ ("Could not send service-request 
multipart message to tunnel!\n"));
+    GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _ ("Could not send service-request 
multipart message to channel!\n"));
     GNUNET_free (msg);
     session->msg = NULL;
     session->client_notification_task =
@@ -1522,7 +1522,7 @@
 
   session->service_request_task = GNUNET_SCHEDULER_NO_TASK;
 
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _ ("Successfully created new tunnel to 
peer (%s)!\n"), GNUNET_i2s (&session->peer));
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _ ("Successfully created new channel to 
peer (%s)!\n"), GNUNET_i2s (&session->peer));
 
   msg_length = sizeof (struct GNUNET_SCALARPRODUCT_service_request)
           +session->mask_length
@@ -1603,13 +1603,13 @@
   GNUNET_log (GNUNET_ERROR_TYPE_INFO, _ ("Transmitting service request.\n"));
 
   //transmit via mesh messaging
-  session->service_transmit_handle = GNUNET_MESH_notify_transmit_ready 
(session->tunnel, GNUNET_YES,
+  session->service_transmit_handle = GNUNET_MESH_notify_transmit_ready 
(session->channel, GNUNET_YES,
                                                                         
GNUNET_TIME_UNIT_FOREVER_REL,
                                                                         
msg_length,
                                                                         
&do_send_message,
                                                                         
session);
   if (!session->service_transmit_handle) {
-    GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _ ("Could not send message to 
tunnel!\n"));
+    GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _ ("Could not send message to 
channel!\n"));
     GNUNET_free (msg);
     session->msg = NULL;
     session->client_notification_task =
@@ -1710,7 +1710,7 @@
 
   if (GNUNET_MESSAGE_TYPE_SCALARPRODUCT_CLIENT_TO_ALICE == msg_type) {
     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                _ ("Got client-request-session with key %s, preparing tunnel 
to remote service.\n"),
+                _ ("Got client-request-session with key %s, preparing channel 
to remote service.\n"),
                 GNUNET_h2s (&session->key));
 
     session->role = ALICE;
@@ -1746,15 +1746,15 @@
     // get our peer ID
     memcpy (&session->peer, &msg->peer, sizeof (struct GNUNET_PeerIdentity));
     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
-                _ ("Creating new tunnel for session with key %s.\n"),
+                _ ("Creating new channel for session with key %s.\n"),
                 GNUNET_h2s (&session->key));
-    session->tunnel = GNUNET_MESH_tunnel_create (my_mesh, session,
+    session->channel = GNUNET_MESH_channel_create (my_mesh, session,
                                                  &session->peer,
                                                  
GNUNET_APPLICATION_TYPE_SCALARPRODUCT,
                                                  GNUNET_NO,
                                                  GNUNET_YES);
-    //prepare_service_request, tunnel_peer_disconnect_handler,
-    if (!session->tunnel) {
+    //prepare_service_request, channel_peer_disconnect_handler,
+    if (!session->channel) {
       GNUNET_break (0);
       GNUNET_free (session->vector);
       GNUNET_free (session);
@@ -1809,26 +1809,26 @@
 
 
 /**
- * Function called for inbound tunnels.
+ * Function called for inbound channels.
  *
  * @param cls closure
- * @param tunnel new handle to the tunnel
- * @param initiator peer that started the tunnel
+ * @param channel new handle to the channel
+ * @param initiator peer that started the channel
  * @param port unused
- * @return session associated with the tunnel
+ * @return session associated with the channel
  */
 static void *
-tunnel_incoming_handler (void *cls,
-                         struct GNUNET_MESH_Tunnel *tunnel,
+channel_incoming_handler (void *cls,
+                         struct GNUNET_MESH_Channel *channel,
                          const struct GNUNET_PeerIdentity *initiator,
                          uint32_t port)
 {
   struct ServiceSession * c = GNUNET_new (struct ServiceSession);
 
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _ ("New incoming tunnel from peer 
%s.\n"), GNUNET_i2s (initiator));
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _ ("New incoming channel from peer 
%s.\n"), GNUNET_i2s (initiator));
   
   c->peer = *initiator;
-  c->tunnel = tunnel;
+  c->channel = channel;
   c->role = BOB;
   c->state = WAITING_FOR_SERVICE_REQUEST;
   return c;
@@ -1836,22 +1836,22 @@
 
 
 /**
- * Function called whenever a tunnel is destroyed.  Should clean up
+ * Function called whenever a channel is destroyed.  Should clean up
  * any associated state.
  *
- * It must NOT call GNUNET_MESH_tunnel_destroy on the tunnel.
+ * It must NOT call GNUNET_MESH_channel_destroy on the channel.
  *
  * @param cls closure (set from GNUNET_MESH_connect)
- * @param tunnel connection to the other end (henceforth invalid)
- * @param tunnel_ctx place where local state associated
- *                   with the tunnel is stored
+ * @param channel connection to the other end (henceforth invalid)
+ * @param channel_ctx place where local state associated
+ *                   with the channel is stored
  */
 static void
-tunnel_destruction_handler (void *cls,
-                            const struct GNUNET_MESH_Tunnel *tunnel,
-                            void *tunnel_ctx)
+channel_destruction_handler (void *cls,
+                            const struct GNUNET_MESH_Channel *channel,
+                            void *channel_ctx)
 {
-  struct ServiceSession * session = tunnel_ctx;
+  struct ServiceSession * session = channel_ctx;
   struct ServiceSession * client_session;
   struct ServiceSession * curr;
 
@@ -1863,7 +1863,7 @@
     // as we have only one peer connected in each session, just remove the 
session
     
     if ((SERVICE_RESPONSE_RECEIVED > session->state) && (!do_shutdown)) {
-      session->tunnel = NULL;
+      session->channel = NULL;
       // if this happened before we received the answer, we must terminate the 
session
       session->client_notification_task =
               GNUNET_SCHEDULER_add_now (&prepare_client_end_notification,
@@ -1985,16 +1985,16 @@
  * Handle a multipart-chunk of a request from another service to calculate a 
scalarproduct with us.
  *
  * @param cls closure (set from #GNUNET_MESH_connect)
- * @param tunnel connection to the other end
- * @param tunnel_ctx place to store local state associated with the tunnel
+ * @param channel connection to the other end
+ * @param channel_ctx place to store local state associated with the channel
  * @param message the actual message
  * @return #GNUNET_OK to keep the connection open,
  *         #GNUNET_SYSERR to close it (signal serious error)
  */
 static int
 handle_service_request_multipart (void *cls,
-                                  struct GNUNET_MESH_Tunnel * tunnel,
-                                  void **tunnel_ctx,
+                                  struct GNUNET_MESH_Channel * channel,
+                                  void **channel_ctx,
                                   const struct GNUNET_MessageHeader * message)
 {
   struct ServiceSession * session;
@@ -2007,7 +2007,7 @@
   int32_t i = -1;
 
   // are we in the correct state?
-  session = (struct ServiceSession *) * tunnel_ctx;
+  session = (struct ServiceSession *) * channel_ctx;
   if ((BOB != session->role) || (WAITING_FOR_MULTIPART_TRANSMISSION != 
session->state)) {
     goto except;
   }
@@ -2077,16 +2077,16 @@
  * Handle a request from another service to calculate a scalarproduct with us.
  *
  * @param cls closure (set from #GNUNET_MESH_connect)
- * @param tunnel connection to the other end
- * @param tunnel_ctx place to store local state associated with the tunnel
+ * @param channel connection to the other end
+ * @param channel_ctx place to store local state associated with the channel
  * @param message the actual message
  * @return #GNUNET_OK to keep the connection open,
  *         #GNUNET_SYSERR to close it (signal serious error)
  */
 static int
 handle_service_request (void *cls,
-                        struct GNUNET_MESH_Tunnel * tunnel,
-                        void **tunnel_ctx,
+                        struct GNUNET_MESH_Channel * channel,
+                        void **channel_ctx,
                         const struct GNUNET_MessageHeader * message)
 {
   struct ServiceSession * session;
@@ -2102,7 +2102,7 @@
   int32_t i = -1;
   enum SessionState needed_state;
 
-  session = (struct ServiceSession *) * tunnel_ctx;
+  session = (struct ServiceSession *) * channel_ctx;
   if (WAITING_FOR_SERVICE_REQUEST != session->state) {
     goto invalid_msg;
   }
@@ -2147,7 +2147,7 @@
   session->total = element_count;
   session->used = used_elements;
   session->transferred = contained_elements;
-  session->tunnel = tunnel;
+  session->channel = channel;
 
   // session key
   memcpy (&session->key, &msg->key, sizeof (struct GNUNET_HashCode));
@@ -2226,16 +2226,16 @@
  * Handle a multipart chunk of a response we got from another service we 
wanted to calculate a scalarproduct with.
  *
  * @param cls closure (set from #GNUNET_MESH_connect)
- * @param tunnel connection to the other end
- * @param tunnel_ctx place to store local state associated with the tunnel
+ * @param channel connection to the other end
+ * @param channel_ctx place to store local state associated with the channel
  * @param message the actual message
  * @return #GNUNET_OK to keep the connection open,
  *         #GNUNET_SYSERR to close it (signal serious error)
  */
 static int
 handle_service_response_multipart (void *cls,
-                                   struct GNUNET_MESH_Tunnel * tunnel,
-                                   void **tunnel_ctx,
+                                   struct GNUNET_MESH_Channel * channel,
+                                   void **channel_ctx,
                                    const struct GNUNET_MessageHeader * message)
 {
   struct ServiceSession * session;
@@ -2250,7 +2250,7 @@
 
   GNUNET_assert (NULL != message);
   // are we in the correct state?
-  session = (struct ServiceSession *) * tunnel_ctx;
+  session = (struct ServiceSession *) * channel_ctx;
   if ((ALICE != session->role) || (WAITING_FOR_MULTIPART_TRANSMISSION != 
session->state)) {
     goto invalid_msg;
   }
@@ -2295,13 +2295,13 @@
   // send message with product to client
   if (ALICE == session->role){
     session->state = FINALIZED;
-    session->tunnel = NULL;
+    session->channel = NULL;
     session->client_notification_task =
           GNUNET_SCHEDULER_add_now (&prepare_client_response,
                                     session);
   }
-  // the tunnel has done its job, terminate our connection and the tunnel
-  // the peer will be notified that the tunnel was destroyed via 
tunnel_destruction_handler
+  // the channel has done its job, terminate our connection and the channel
+  // the peer will be notified that the channel was destroyed via 
channel_destruction_handler
   // just close the connection, as recommended by Christian
   return GNUNET_SYSERR;
 }
@@ -2311,16 +2311,16 @@
  * Handle a response we got from another service we wanted to calculate a 
scalarproduct with.
  *
  * @param cls closure (set from #GNUNET_MESH_connect)
- * @param tunnel connection to the other end
- * @param tunnel_ctx place to store local state associated with the tunnel
+ * @param channel connection to the other end
+ * @param channel_ctx place to store local state associated with the channel
  * @param message the actual message
  * @return #GNUNET_OK to keep the connection open,
  *         #GNUNET_SYSERR to close it (we are done)
  */
 static int
 handle_service_response (void *cls,
-                         struct GNUNET_MESH_Tunnel * tunnel,
-                         void **tunnel_ctx,
+                         struct GNUNET_MESH_Channel * channel,
+                         void **channel_ctx,
                          const struct GNUNET_MessageHeader * message)
 {
   struct ServiceSession * session;
@@ -2334,7 +2334,7 @@
   int rc;
 
   GNUNET_assert (NULL != message);
-  session = (struct ServiceSession *) * tunnel_ctx;
+  session = (struct ServiceSession *) * channel_ctx;
   // are we in the correct state?
   if (WAITING_FOR_SERVICE_RESPONSE != session->state) {
     goto invalid_msg;
@@ -2399,13 +2399,13 @@
   // send message with product to client
   if (ALICE == session->role){
     session->state = FINALIZED;
-    session->tunnel = NULL;
+    session->channel = NULL;
     session->client_notification_task =
           GNUNET_SCHEDULER_add_now (&prepare_client_response,
                                     session);
   }
-  // the tunnel has done its job, terminate our connection and the tunnel
-  // the peer will be notified that the tunnel was destroyed via 
tunnel_destruction_handler
+  // the channel has done its job, terminate our connection and the channel
+  // the peer will be notified that the channel was destroyed via 
channel_destruction_handler
   // just close the connection, as recommended by Christian
   return GNUNET_SYSERR;
 }
@@ -2426,11 +2426,11 @@
 
   do_shutdown = GNUNET_YES;
 
-  // terminate all owned open tunnels.
+  // terminate all owned open channels.
   for (session = from_client_head; NULL != session; session = session->next) {
-    if ((FINALIZED != session->state) && (NULL != session->tunnel)) {
-      GNUNET_MESH_tunnel_destroy (session->tunnel);
-      session->tunnel = NULL;
+    if ((FINALIZED != session->state) && (NULL != session->channel)) {
+      GNUNET_MESH_channel_destroy (session->channel);
+      session->channel = NULL;
     }
     if (GNUNET_SCHEDULER_NO_TASK != session->client_notification_task) {
       GNUNET_SCHEDULER_cancel (session->client_notification_task);
@@ -2446,9 +2446,9 @@
     }
   }
   for (session = from_service_head; NULL != session; session = session->next)
-    if (NULL != session->tunnel) {
-      GNUNET_MESH_tunnel_destroy (session->tunnel);
-      session->tunnel = NULL;
+    if (NULL != session->channel) {
+      GNUNET_MESH_channel_destroy (session->channel);
+      session->channel = NULL;
     }
 
   if (my_mesh) {
@@ -2498,8 +2498,8 @@
                 GNUNET_CRYPTO_get_peer_identity (c,
                                                  &me));
   my_mesh = GNUNET_MESH_connect (c, NULL,
-                                 &tunnel_incoming_handler,
-                                 &tunnel_destruction_handler,
+                                 &channel_incoming_handler,
+                                 &channel_destruction_handler,
                                  mesh_handlers, ports);
   if (!my_mesh) {
     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _ ("Connect to MESH failed\n"));

Modified: gnunet/src/set/gnunet-service-set.c
===================================================================
--- gnunet/src/set/gnunet-service-set.c 2013-11-14 13:30:29 UTC (rev 30696)
+++ gnunet/src/set/gnunet-service-set.c 2013-11-14 13:55:12 UTC (rev 30697)
@@ -316,16 +316,16 @@
     op->mq = NULL;
   }
 
-  if (NULL != op->tunnel)
+  if (NULL != op->channel)
   {
-    GNUNET_MESH_tunnel_destroy (op->tunnel);
-    op->tunnel = NULL;
+    GNUNET_MESH_channel_destroy (op->channel);
+    op->channel = NULL;
   }
 
   collect_generation_garbage (set);
 
-  /* We rely on the tunnel end handler to free 'op'. When 'op->tunnel' was 
NULL,
-   * there was a tunnel end handler that will free 'op' on the call stack. */
+  /* We rely on the channel end handler to free 'op'. When 'op->channel' was 
NULL,
+   * there was a channel end handler that will free 'op' on the call stack. */
 }
 
 
@@ -362,7 +362,7 @@
 {
   /* If the client is not dead yet, destroy it.
    * The client's destroy callback will destroy the set again.
-   * We do this so that the tunnel end handler still has a valid set handle
+   * We do this so that the channel end handler still has a valid set handle
    * to destroy. */
   if (NULL != set->client)
   {
@@ -532,8 +532,8 @@
  *
  * @param op the operation state
  * @param mh the received message
- * @return GNUNET_OK if the tunnel should be kept alive,
- *         GNUNET_SYSERR to destroy the tunnel
+ * @return GNUNET_OK if the channel should be kept alive,
+ *         GNUNET_SYSERR to destroy the channel
  */
 static int
 handle_incoming_msg (struct Operation *op,
@@ -805,8 +805,8 @@
   }
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer request rejected by client\n");
   
-  GNUNET_MESH_tunnel_destroy (incoming->tunnel);
-  //tunnel destruction handler called immediately upon destruction
+  GNUNET_MESH_channel_destroy (incoming->channel);
+  //channel destruction handler called immediately upon destruction
   GNUNET_SERVER_receive_done (client, GNUNET_OK);
 }
 
@@ -935,12 +935,12 @@
   op->vt = set->vt;
   GNUNET_CONTAINER_DLL_insert (set->ops_head, set->ops_tail, op);
 
-  op->tunnel = GNUNET_MESH_tunnel_create (mesh, op, &msg->target_peer,
+  op->channel = GNUNET_MESH_channel_create (mesh, op, &msg->target_peer,
                                           GNUNET_APPLICATION_TYPE_SET,
                                           GNUNET_YES,
                                           GNUNET_YES);
 
-  op->mq = GNUNET_MESH_mq_create (op->tunnel);
+  op->mq = GNUNET_MESH_mq_create (op->channel);
 
   set->vt->evaluate (op);
   GNUNET_SERVER_receive_done (client, GNUNET_OK);
@@ -1111,7 +1111,7 @@
   while (NULL != sets_head)
     set_destroy (sets_head);
 
-  /* it's important to destroy mesh at the end, as all tunnels
+  /* it's important to destroy mesh at the end, as all channels
    * must be destroyed before the mesh handle! */
   if (NULL != mesh)
   {
@@ -1127,9 +1127,9 @@
  * Timeout happens iff:
  *  - we suggested an operation to our listener, 
  *    but did not receive a response in time
- *  - we got the tunnel from a peer but no 
GNUNET_MESSAGE_TYPE_SET_P2P_OPERATION_REQUEST
+ *  - we got the channel from a peer but no 
GNUNET_MESSAGE_TYPE_SET_P2P_OPERATION_REQUEST
  *  - shutdown (obviously)
- * @param cls tunnel context
+ * @param cls channel context
  * @param tc context information (why was this task triggered now)
  */
 static void
@@ -1150,16 +1150,16 @@
 
 /**
  * Terminates an incoming operation in case we have not yet received an
- * operation request. Called by the tunnel destruction handler.
+ * operation request. Called by the channel destruction handler.
  * 
- * @param op the tunnel context
+ * @param op the channel context
  */
 static void
 handle_incoming_disconnect (struct Operation *op)
 {
   GNUNET_assert (GNUNET_YES == op->is_incoming);
   
-  if (NULL == op->tunnel)
+  if (NULL == op->channel)
     return;
 
   incoming_destroy (op);
@@ -1167,25 +1167,25 @@
 
 
 /**
- * Method called whenever another peer has added us to a tunnel
+ * Method called whenever another peer has added us to a channel
  * the other peer initiated.
  * Only called (once) upon reception of data with a message type which was
  * subscribed to in GNUNET_MESH_connect. 
  * 
- * The tunnel context represents the operation itself and gets added to a DLL,
+ * The channel context represents the operation itself and gets added to a DLL,
  * from where it gets looked up when our local listener client responds 
  * to a proposed/suggested operation or connects and associates with this 
operation.
  *
  * @param cls closure
- * @param tunnel new handle to the tunnel
- * @param initiator peer that started the tunnel
- * @param port Port this tunnel is for.
- * @return initial tunnel context for the tunnel
+ * @param channel new handle to the channel
+ * @param initiator peer that started the channel
+ * @param port Port this channel is for.
+ * @return initial channel context for the channel
  *         (can be NULL -- that's not an error)
  */
 static void *
-tunnel_new_cb (void *cls,
-               struct GNUNET_MESH_Tunnel *tunnel,
+channel_new_cb (void *cls,
+               struct GNUNET_MESH_Channel *channel,
                const struct GNUNET_PeerIdentity *initiator,
                uint32_t port)
 {
@@ -1195,15 +1195,15 @@
     .peer_disconnect = handle_incoming_disconnect
   };
 
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new incoming tunnel\n");
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new incoming channel\n");
 
   GNUNET_assert (port == GNUNET_APPLICATION_TYPE_SET);
   incoming = GNUNET_new (struct Operation);
   incoming->is_incoming = GNUNET_YES;
   incoming->state = GNUNET_new (struct OperationState);
   incoming->state->peer = *initiator;
-  incoming->tunnel = tunnel;
-  incoming->mq = GNUNET_MESH_mq_create (incoming->tunnel);
+  incoming->channel = channel;
+  incoming->mq = GNUNET_MESH_mq_create (incoming->channel);
   incoming->vt = &incoming_vt;
   incoming->state->timeout_task =
       GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES, 
incoming_timeout_cb, incoming);
@@ -1214,72 +1214,72 @@
 
 
 /**
- * Function called whenever a tunnel is destroyed.  Should clean up
+ * Function called whenever a channel is destroyed.  Should clean up
  * any associated state.
- * GNUNET_MESH_tunnel_destroy. It must NOT call GNUNET_MESH_tunnel_destroy on
- * the tunnel.
+ * GNUNET_MESH_channel_destroy. It must NOT call GNUNET_MESH_channel_destroy on
+ * the channel.
  * 
  * The peer_disconnect function is part of a a virtual table set initially 
either 
- * when a peer creates a new tunnel with us (tunnel_new_cb), or once we create
- * a new tunnel ourselves (evaluate). 
+ * when a peer creates a new channel with us (channel_new_cb), or once we 
create
+ * a new channel ourselves (evaluate). 
  * 
  * Once we know the exact type of operation (union/intersection), the vt is 
  * replaced with an operation specific instance (_GSS_[op]_vt).
  *
  * @param cls closure (set from GNUNET_MESH_connect)
- * @param tunnel connection to the other end (henceforth invalid)
- * @param tunnel_ctx place where local state associated
- *                   with the tunnel is stored
+ * @param channel connection to the other end (henceforth invalid)
+ * @param channel_ctx place where local state associated
+ *                   with the channel is stored
  */
 static void
-tunnel_end_cb (void *cls,
-               const struct GNUNET_MESH_Tunnel *tunnel, void *tunnel_ctx)
+channel_end_cb (void *cls,
+               const struct GNUNET_MESH_Channel *channel, void *channel_ctx)
 {
-  struct Operation *op = tunnel_ctx;
+  struct Operation *op = channel_ctx;
 
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel end cb called\n");
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "channel end cb called\n");
 
-  op->tunnel = NULL;
+  op->channel = NULL;
 
   if (NULL != op->vt)
     op->vt->peer_disconnect (op);
   /* mesh will never call us with the context again! */
-  GNUNET_free (tunnel_ctx);
+  GNUNET_free (channel_ctx);
 }
 
 
 /**
  * Functions with this signature are called whenever any message is
- * received via the mesh tunnel.
+ * received via the mesh channel.
  *
  * The msg_handler is a virtual table set in initially either when a peer 
- * creates a new tunnel with us (tunnel_new_cb), or once we create a new 
tunnel 
+ * creates a new channel with us (channel_new_cb), or once we create a new 
channel 
  * ourselves (evaluate). 
  * 
  * Once we know the exact type of operation (union/intersection), the vt is 
  * replaced with an operation specific instance (_GSS_[op]_vt).
  *
  * @param cls Closure (set from GNUNET_MESH_connect).
- * @param tunnel Connection to the other end.
- * @param tunnel_ctx Place to store local state associated with the tunnel.
+ * @param channel Connection to the other end.
+ * @param channel_ctx Place to store local state associated with the channel.
  * @param message The actual message.
  *
- * @return GNUNET_OK to keep the tunnel open,
+ * @return GNUNET_OK to keep the channel open,
  *         GNUNET_SYSERR to close it (signal serious error).
  */
 static int
 dispatch_p2p_message (void *cls,
-                      struct GNUNET_MESH_Tunnel *tunnel,
-                      void **tunnel_ctx,
+                      struct GNUNET_MESH_Channel *channel,
+                      void **channel_ctx,
                       const struct GNUNET_MessageHeader *message)
 {
-  struct Operation *op = *tunnel_ctx;
+  struct Operation *op = *channel_ctx;
   int ret;
 
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "dispatching mesh message (type: %u)\n",
               ntohs (message->type));
-  /* do this before the handler, as the handler might kill the tunnel */
-  GNUNET_MESH_receive_done (tunnel);
+  /* do this before the handler, as the handler might kill the channel */
+  GNUNET_MESH_receive_done (channel);
   ret = op->vt->msg_handler (op, message);
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "handled mesh message (type: %u)\n",
               ntohs (message->type));
@@ -1336,7 +1336,7 @@
   GNUNET_SERVER_disconnect_notify (server, &handle_client_disconnect, NULL);
   GNUNET_SERVER_add_handlers (server, server_handlers);
 
-  mesh = GNUNET_MESH_connect (cfg, NULL, tunnel_new_cb, tunnel_end_cb,
+  mesh = GNUNET_MESH_connect (cfg, NULL, channel_new_cb, channel_end_cb,
                               mesh_handlers, mesh_ports);
   if (NULL == mesh)
   {

Modified: gnunet/src/set/gnunet-service-set.h
===================================================================
--- gnunet/src/set/gnunet-service-set.h 2013-11-14 13:30:29 UTC (rev 30696)
+++ gnunet/src/set/gnunet-service-set.h 2013-11-14 13:55:12 UTC (rev 30697)
@@ -296,7 +296,7 @@
   /**
    * Tunnel to the peer.
    */
-  struct GNUNET_MESH_Tunnel *tunnel;
+  struct GNUNET_MESH_Channel *channel;
 
   /**
    * Message queue for the tunnel.

Modified: gnunet/src/vpn/gnunet-service-vpn.c
===================================================================
--- gnunet/src/vpn/gnunet-service-vpn.c 2013-11-14 13:30:29 UTC (rev 30696)
+++ gnunet/src/vpn/gnunet-service-vpn.c 2013-11-14 13:55:12 UTC (rev 30697)
@@ -27,8 +27,8 @@
  * @author Christian Grothoff
  *
  * TODO:
- * - keep multiple peers/mesh tunnels ready as alternative exits /
- *   detect & recover from tunnel-to-exit failure gracefully
+ * - keep multiple peers/mesh channels ready as alternative exits /
+ *   detect & recover from channel-to-exit failure gracefully
  */
 #include "platform.h"
 #include "gnunet_util_lib.h"
@@ -51,45 +51,45 @@
 
 
 /**
- * State we keep for each of our tunnels.
+ * State we keep for each of our channels.
  */
-struct TunnelState;
+struct ChannelState;
 
 /**
- * Information we track for each IP address to determine which tunnel
+ * Information we track for each IP address to determine which channel
  * to send the traffic over to the destination.
  */
 struct DestinationEntry;
 
 /**
- * List of tunnels we keep for each destination port for a given
+ * List of channels we keep for each destination port for a given
  * destination entry.
  */
-struct DestinationTunnel
+struct DestinationChannel
 {
 
   /**
    * Kept in a DLL.
    */
-  struct DestinationTunnel *next;
+  struct DestinationChannel *next;
 
   /**
    * Kept in a DLL.
    */
-  struct DestinationTunnel *prev;
+  struct DestinationChannel *prev;
 
   /**
-   * Destination entry list this `struct DestinationTunnel` belongs with.
+   * Destination entry list this `struct DestinationChannel` belongs with.
    */
   struct DestinationEntry *destination;
 
   /**
-   * Pre-allocated tunnel for this destination, or NULL for none.
+   * Pre-allocated channel for this destination, or NULL for none.
    */
-  struct TunnelState *ts;
+  struct ChannelState *ts;
 
   /**
-   * Destination port this tunnel state is used for.
+   * Destination port this channel state is used for.
    */
   uint16_t destination_port;
 
@@ -97,7 +97,7 @@
 
 
 /**
- * Information we track for each IP address to determine which tunnel
+ * Information we track for each IP address to determine which channel
  * to send the traffic over to the destination.
  */
 struct DestinationEntry
@@ -110,14 +110,14 @@
   struct GNUNET_HashCode key;
 
   /**
-   * Head of DLL of tunnels associated with this destination.
+   * Head of DLL of channels associated with this destination.
    */
-  struct DestinationTunnel *dt_head;
+  struct DestinationChannel *dt_head;
 
   /**
-   * Tail of DLL of tunnels associated with this destination.
+   * Tail of DLL of channels associated with this destination.
    */
-  struct DestinationTunnel *dt_tail;
+  struct DestinationChannel *dt_tail;
 
   /**
    * Entry for this entry in the destination_heap.
@@ -125,8 +125,8 @@
   struct GNUNET_CONTAINER_HeapNode *heap_node;
 
   /**
-   * #GNUNET_NO if this is a tunnel to an Internet-exit,
-   * #GNUNET_YES if this tunnel is to a service.
+   * #GNUNET_NO if this is a channel to an Internet-exit,
+   * #GNUNET_YES if this channel is to a service.
    */
   int is_service;
 
@@ -139,7 +139,7 @@
     struct
     {
       /**
-       * The description of the service (only used for service tunnels).
+       * The description of the service (only used for service channels).
        */
       struct GNUNET_HashCode service_descriptor;
 
@@ -159,7 +159,7 @@
       int af;
 
       /**
-       * IP address of the ultimate destination (only used for exit tunnels).
+       * IP address of the ultimate destination (only used for exit channels).
        */
       union
       {
@@ -182,19 +182,19 @@
 
 
 /**
- * A messages we have in queue for a particular tunnel.
+ * A messages we have in queue for a particular channel.
  */
-struct TunnelMessageQueueEntry
+struct ChannelMessageQueueEntry
 {
   /**
    * This is a doubly-linked list.
    */
-  struct TunnelMessageQueueEntry *next;
+  struct ChannelMessageQueueEntry *next;
 
   /**
    * This is a doubly-linked list.
    */
-  struct TunnelMessageQueueEntry *prev;
+  struct ChannelMessageQueueEntry *prev;
 
   /**
    * Number of bytes in 'msg'.
@@ -209,16 +209,16 @@
 
 
 /**
- * State we keep for each of our tunnels.
+ * State we keep for each of our channels.
  */
-struct TunnelState
+struct ChannelState
 {
 
   /**
-   * Information about the tunnel to use, NULL if no tunnel
+   * Information about the channel to use, NULL if no channel
    * is available right now.
    */
-  struct GNUNET_MESH_Tunnel *tunnel;
+  struct GNUNET_MESH_Channel *channel;
 
   /**
    * Active query with REGEX to locate exit.
@@ -231,29 +231,29 @@
   struct GNUNET_MESH_TransmitHandle *th;
 
   /**
-   * Entry for this entry in the tunnel_heap, NULL as long as this
-   * tunnel state is not fully bound.
+   * Entry for this entry in the channel_heap, NULL as long as this
+   * channel state is not fully bound.
    */
   struct GNUNET_CONTAINER_HeapNode *heap_node;
 
   /**
    * Head of list of messages scheduled for transmission.
    */
-  struct TunnelMessageQueueEntry *tmq_head;
+  struct ChannelMessageQueueEntry *tmq_head;
 
   /**
    * Tail of list of messages scheduled for transmission.
    */
-  struct TunnelMessageQueueEntry *tmq_tail;
+  struct ChannelMessageQueueEntry *tmq_tail;
 
   /**
-   * Destination entry that has a pointer to this tunnel state;
-   * NULL if this tunnel state is in the tunnel map.
+   * Destination entry that has a pointer to this channel state;
+   * NULL if this channel state is in the channel map.
    */
-  struct DestinationTunnel *destination_container;
+  struct DestinationChannel *destination_container;
 
   /**
-   * Destination to which this tunnel leads.  Note that
+   * Destination to which this channel leads.  Note that
    * this struct is NOT in the destination_map (but a
    * local copy) and that the 'heap_node' should always
    * be NULL.
@@ -261,7 +261,7 @@
   struct DestinationEntry destination;
 
   /**
-   * Addess family used for this tunnel on the local TUN interface.
+   * Addess family used for this channel on the local TUN interface.
    */
   int af;
 
@@ -294,7 +294,7 @@
 
   /**
    * Destination IP address used by the source on our end (this is the IP
-   * that we pick freely within the VPN's tunnel IP range).
+   * that we pick freely within the VPN's channel IP range).
    */
   union
   {
@@ -340,7 +340,7 @@
 
 /**
  * Map from IP address to destination information (possibly with a
- * MESH tunnel handle for fast setup).
+ * MESH channel handle for fast setup).
  */
 static struct GNUNET_CONTAINER_MultiHashMap *destination_map;
 
@@ -351,15 +351,15 @@
 
 /**
  * Map from source and destination address (IP+port) to connection
- * information (mostly with the respective MESH tunnel handle).
+ * information (mostly with the respective MESH channel handle).
  */
-static struct GNUNET_CONTAINER_MultiHashMap *tunnel_map;
+static struct GNUNET_CONTAINER_MultiHashMap *channel_map;
 
 /**
  * Min-Heap sorted by activity time to expire old mappings; values are
- * of type 'struct TunnelState'.
+ * of type 'struct ChannelState'.
  */
-static struct GNUNET_CONTAINER_Heap *tunnel_heap;
+static struct GNUNET_CONTAINER_Heap *channel_heap;
 
 /**
  * Statistics.
@@ -393,10 +393,10 @@
 static unsigned long long max_destination_mappings;
 
 /**
- * If there are more than this number of open tunnels, old ones
+ * If there are more than this number of open channels, old ones
  * will be removed
  */
-static unsigned long long max_tunnel_mappings;
+static unsigned long long max_channel_mappings;
 
 
 /**
@@ -433,7 +433,7 @@
 
 /**
  * Compute the key under which we would store an entry in the
- * tunnel_map for the given socket address pair.
+ * channel_map for the given socket address pair.
  *
  * @param af address family (AF_INET or AF_INET6)
  * @param protocol IPPROTO_TCP or IPPROTO_UDP
@@ -444,7 +444,7 @@
  * @param key where to store the key
  */
 static void
-get_tunnel_key_from_ips (int af,
+get_channel_key_from_ips (int af,
                         uint8_t protocol,
                         const void *source_ip,
                         uint16_t source_port,
@@ -533,21 +533,21 @@
 
 
 /**
- * Free resources associated with a tunnel state.
+ * Free resources associated with a channel state.
  *
  * @param ts state to free
  */
 static void
-free_tunnel_state (struct TunnelState *ts)
+free_channel_state (struct ChannelState *ts)
 {
   struct GNUNET_HashCode key;
-  struct TunnelMessageQueueEntry *tnq;
-  struct GNUNET_MESH_Tunnel *tunnel;
+  struct ChannelMessageQueueEntry *tnq;
+  struct GNUNET_MESH_Channel *channel;
 
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-             "Cleaning up tunnel state\n");
+             "Cleaning up channel state\n");
   GNUNET_STATISTICS_update (stats,
-                           gettext_noop ("# Active tunnels"),
+                           gettext_noop ("# Active channels"),
                            -1, GNUNET_NO);
   while (NULL != (tnq = ts->tmq_head))
   {
@@ -564,10 +564,10 @@
     ts->th = NULL;
   }
   GNUNET_assert (NULL == ts->destination.heap_node);
-  if (NULL != (tunnel = ts->tunnel))
+  if (NULL != (channel = ts->channel))
   {
-    ts->tunnel = NULL;
-    GNUNET_MESH_tunnel_destroy (tunnel);
+    ts->channel = NULL;
+    GNUNET_MESH_channel_destroy (channel);
   }
   if (NULL != ts->search)
   {
@@ -578,7 +578,7 @@
   {
     GNUNET_CONTAINER_heap_remove_node (ts->heap_node);
     ts->heap_node = NULL;
-    get_tunnel_key_from_ips (ts->af,
+    get_channel_key_from_ips (ts->af,
                             ts->protocol,
                             &ts->source_ip,
                             ts->source_port,
@@ -586,7 +586,7 @@
                             ts->destination_port,
                             &key);
     GNUNET_assert (GNUNET_YES ==
-                  GNUNET_CONTAINER_multihashmap_remove (tunnel_map,
+                  GNUNET_CONTAINER_multihashmap_remove (channel_map,
                                                         &key,
                                                         ts));
   }
@@ -603,7 +603,7 @@
 /**
  * Send a message from the message queue via mesh.
  *
- * @param cls the `struct TunnelState` with the message queue
+ * @param cls the `struct ChannelState` with the message queue
  * @param size number of bytes available in @a buf
  * @param buf where to copy the message
  * @return number of bytes copied to @a buf
@@ -611,8 +611,8 @@
 static size_t
 send_to_peer_notify_callback (void *cls, size_t size, void *buf)
 {
-  struct TunnelState *ts = cls;
-  struct TunnelMessageQueueEntry *tnq;
+  struct ChannelState *ts = cls;
+  struct ChannelMessageQueueEntry *tnq;
   size_t ret;
 
   ts->th = NULL;
@@ -622,7 +622,7 @@
   GNUNET_assert (NULL != tnq);
   GNUNET_assert (size >= tnq->len);
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-             "Sending %u bytes via mesh tunnel\n",
+             "Sending %u bytes via mesh channel\n",
              tnq->len);
   GNUNET_CONTAINER_DLL_remove (ts->tmq_head,
                               ts->tmq_tail,
@@ -632,7 +632,7 @@
   ret = tnq->len;
   GNUNET_free (tnq);
   if (NULL != (tnq = ts->tmq_head))
-    ts->th = GNUNET_MESH_notify_transmit_ready (ts->tunnel,
+    ts->th = GNUNET_MESH_notify_transmit_ready (ts->channel,
                                                GNUNET_NO /* cork */,
                                                GNUNET_TIME_UNIT_FOREVER_REL,
                                                tnq->len,
@@ -646,27 +646,27 @@
 
 
 /**
- * Add the given message to the given tunnel and trigger the
+ * Add the given message to the given channel and trigger the
  * transmission process.
  *
  * @param tnq message to queue
- * @param ts tunnel to queue the message for
+ * @param ts channel to queue the message for
  */
 static void
-send_to_tunnel (struct TunnelMessageQueueEntry *tnq,
-               struct TunnelState *ts)
+send_to_channel (struct ChannelMessageQueueEntry *tnq,
+               struct ChannelState *ts)
 {
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-             "Queueing %u bytes for transmission via mesh tunnel\n",
+             "Queueing %u bytes for transmission via mesh channel\n",
              tnq->len);
-  GNUNET_assert (NULL != ts->tunnel);
+  GNUNET_assert (NULL != ts->channel);
   GNUNET_CONTAINER_DLL_insert_tail (ts->tmq_head,
                                    ts->tmq_tail,
                                    tnq);
   ts->tmq_length++;
   if (ts->tmq_length > MAX_MESSAGE_QUEUE_SIZE)
   {
-    struct TunnelMessageQueueEntry *dq;
+    struct ChannelMessageQueueEntry *dq;
 
     dq = ts->tmq_head;
     GNUNET_assert (dq != tnq);
@@ -683,7 +683,7 @@
     GNUNET_free (dq);
   }
   if (NULL == ts->th)
-    ts->th = GNUNET_MESH_notify_transmit_ready (ts->tunnel,
+    ts->th = GNUNET_MESH_notify_transmit_ready (ts->channel,
                                                GNUNET_NO /* cork */,
                                                GNUNET_TIME_UNIT_FOREVER_REL,
                                                tnq->len,
@@ -695,7 +695,7 @@
 /**
  * Regex has found a potential exit peer for us; consider using it.
  *
- * @param cls the 'struct TunnelState'
+ * @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.
@@ -710,7 +710,7 @@
                     const struct GNUNET_PeerIdentity *put_path,
                     unsigned int put_path_length)
 {
-  struct TunnelState *ts = cls;
+  struct ChannelState *ts = cls;
   unsigned int apptype;
 
   GNUNET_REGEX_search_cancel (ts->search);
@@ -727,7 +727,7 @@
     GNUNET_break (0);
     return;
   }
-  ts->tunnel = GNUNET_MESH_tunnel_create (mesh_handle,
+  ts->channel = GNUNET_MESH_channel_create (mesh_handle,
                                           ts,
                                           id,
                                           apptype,
@@ -737,21 +737,21 @@
 
 
 /**
- * Initialize the given destination entry's mesh tunnel.
+ * Initialize the given destination entry's mesh channel.
  *
- * @param dt destination tunnel for which we need to setup a tunnel
+ * @param dt destination channel for which we need to setup a channel
  * @param client_af address family of the address returned to the client
- * @return tunnel state of the tunnel that was created
+ * @return channel state of the channel that was created
  */
-static struct TunnelState *
-create_tunnel_to_destination (struct DestinationTunnel *dt,
+static struct ChannelState *
+create_channel_to_destination (struct DestinationChannel *dt,
                              int client_af)
 {
-  struct TunnelState *ts;
+  struct ChannelState *ts;
   unsigned int apptype;
 
   GNUNET_STATISTICS_update (stats,
-                           gettext_noop ("# Mesh tunnels created"),
+                           gettext_noop ("# Mesh channels created"),
                            1, GNUNET_NO);
   GNUNET_assert (NULL == dt->ts);
   switch (client_af)
@@ -766,7 +766,7 @@
     GNUNET_break (0);
     return NULL;
   }
-  ts = GNUNET_new (struct TunnelState);
+  ts = GNUNET_new (struct ChannelState);
   ts->af = client_af;
   ts->destination = *dt->destination;
   ts->destination.heap_node = NULL; /* copy is NOT in destination heap */
@@ -774,21 +774,21 @@
   ts->destination_container = dt; /* we are referenced from dt */
   if (dt->destination->is_service)
   {
-    ts->tunnel = GNUNET_MESH_tunnel_create (mesh_handle,
+    ts->channel = GNUNET_MESH_channel_create (mesh_handle,
                                            ts,
                                            
&dt->destination->details.service_destination.target,
                                            apptype,
                                            GNUNET_YES,
                                            GNUNET_NO);
-    if (NULL == ts->tunnel)
+    if (NULL == ts->channel)
     {
       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
-                 _("Failed to setup mesh tunnel!\n"));
+                 _("Failed to setup mesh channel!\n"));
       GNUNET_free (ts);
       return NULL;
     }
     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-               "Creating tunnel to peer %s offering service %s\n",
+               "Creating channel to peer %s offering service %s\n",
                GNUNET_i2s 
(&dt->destination->details.service_destination.target),
                GNUNET_h2s 
(&dt->destination->details.service_destination.service_descriptor));
   }
@@ -841,20 +841,20 @@
 
 
 /**
- * We have too many active tunnels.  Clean up the oldest tunnel.
+ * We have too many active channels.  Clean up the oldest channel.
  *
- * @param except tunnel that must NOT be cleaned up, even if it is the oldest
+ * @param except channel that must NOT be cleaned up, even if it is the oldest
  */
 static void
-expire_tunnel (struct TunnelState *except)
+expire_channel (struct ChannelState *except)
 {
-  struct TunnelState *ts;
+  struct ChannelState *ts;
 
-  ts = GNUNET_CONTAINER_heap_peek (tunnel_heap);
+  ts = GNUNET_CONTAINER_heap_peek (channel_heap);
   GNUNET_assert (NULL != ts);
   if (except == ts)
     return; /* can't do this */
-  free_tunnel_state (ts);
+  free_channel_state (ts);
 }
 
 
@@ -879,15 +879,15 @@
              size_t payload_length)
 {
   struct GNUNET_HashCode key;
-  struct TunnelState *ts;
-  struct TunnelMessageQueueEntry *tnq;
+  struct ChannelState *ts;
+  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;
-  struct DestinationTunnel *dt;
+  struct DestinationChannel *dt;
   uint16_t source_port;
   uint16_t destination_port;
 
@@ -911,7 +911,7 @@
       }
       source_port = ntohs (udp->source_port);
       destination_port = ntohs (udp->destination_port);
-      get_tunnel_key_from_ips (af,
+      get_channel_key_from_ips (af,
                               IPPROTO_UDP,
                               source_ip,
                               source_port,
@@ -938,7 +938,7 @@
       }
       source_port = ntohs (tcp->source_port);
       destination_port = ntohs (tcp->destination_port);
-      get_tunnel_key_from_ips (af,
+      get_channel_key_from_ips (af,
                               IPPROTO_TCP,
                               source_ip,
                               source_port,
@@ -966,7 +966,7 @@
       icmp = payload;
       source_port = 0;
       destination_port = 0;
-      get_tunnel_key_from_ips (af,
+      get_channel_key_from_ips (af,
                               protocol,
                               source_ip,
                               0,
@@ -1037,7 +1037,7 @@
   }
   if (NULL == dt)
   {
-    dt = GNUNET_new (struct DestinationTunnel);
+    dt = GNUNET_new (struct DestinationChannel);
     dt->destination = destination;
     GNUNET_CONTAINER_DLL_insert (destination->dt_head,
                                 destination->dt_tail,
@@ -1045,23 +1045,23 @@
     dt->destination_port = destination_port;
   }
 
-  /* see if we have an existing tunnel for this destination */
-  ts = GNUNET_CONTAINER_multihashmap_get (tunnel_map,
+  /* see if we have an existing channel for this destination */
+  ts = GNUNET_CONTAINER_multihashmap_get (channel_map,
                                          &key);
   if (NULL == ts)
   {
-    /* need to either use the existing tunnel from the destination (if still
+    /* 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_tunnel_to_destination (dt, af);
+      ts = create_channel_to_destination (dt, af);
     else
       ts = dt->ts;
     if (NULL == ts)
       return;
     dt->ts = NULL;
     ts->destination_container = NULL; /* no longer 'contained' */
-    /* now bind existing "unbound" tunnel to our IP/port tuple */
+    /* now bind existing "unbound" channel to our IP/port tuple */
     ts->protocol = protocol;
     ts->af = af;
     if (AF_INET == af)
@@ -1076,30 +1076,30 @@
     }
     ts->source_port = source_port;
     ts->destination_port = destination_port;
-    ts->heap_node = GNUNET_CONTAINER_heap_insert (tunnel_heap,
+    ts->heap_node = GNUNET_CONTAINER_heap_insert (channel_heap,
                                                  ts,
                                                  GNUNET_TIME_absolute_get 
().abs_value_us);
     GNUNET_assert (GNUNET_YES ==
-                  GNUNET_CONTAINER_multihashmap_put (tunnel_map,
+                  GNUNET_CONTAINER_multihashmap_put (channel_map,
                                                      &key,
                                                      ts,
                                                      
GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
     GNUNET_STATISTICS_update (stats,
-                             gettext_noop ("# Active tunnels"),
+                             gettext_noop ("# Active channels"),
                              1, GNUNET_NO);
-    while (GNUNET_CONTAINER_multihashmap_size (tunnel_map) > 
max_tunnel_mappings)
-      expire_tunnel (ts);
+    while (GNUNET_CONTAINER_multihashmap_size (channel_map) > 
max_channel_mappings)
+      expire_channel (ts);
   }
   else
   {
     is_new = GNUNET_NO;
-    GNUNET_CONTAINER_heap_update_cost (tunnel_heap,
+    GNUNET_CONTAINER_heap_update_cost (channel_heap,
                                       ts->heap_node,
                                       GNUNET_TIME_absolute_get 
().abs_value_us);
   }
-  GNUNET_assert (NULL != ts->tunnel);
+  GNUNET_assert (NULL != ts->channel);
 
-  /* send via tunnel */
+  /* send via channel */
   switch (protocol)
   {
   case IPPROTO_UDP:
@@ -1114,7 +1114,7 @@
        GNUNET_break (0);
        return;
       }
-      tnq = GNUNET_malloc (sizeof (struct TunnelMessageQueueEntry) + mlen);
+      tnq = GNUNET_malloc (sizeof (struct ChannelMessageQueueEntry) + mlen);
       tnq->len = mlen;
       tnq->msg = &tnq[1];
       usm = (struct GNUNET_EXIT_UdpServiceMessage *) &tnq[1];
@@ -1143,7 +1143,7 @@
        GNUNET_break (0);
        return;
       }
-      tnq = GNUNET_malloc (sizeof (struct TunnelMessageQueueEntry) + mlen);
+      tnq = GNUNET_malloc (sizeof (struct ChannelMessageQueueEntry) + mlen);
       tnq->len = mlen;
       tnq->msg = &tnq[1];
       uim = (struct GNUNET_EXIT_UdpInternetMessage *) &tnq[1];
@@ -1186,7 +1186,7 @@
          GNUNET_break (0);
          return;
        }
-       tnq = GNUNET_malloc (sizeof (struct TunnelMessageQueueEntry) + mlen);
+       tnq = GNUNET_malloc (sizeof (struct ChannelMessageQueueEntry) + mlen);
        tnq->len = mlen;
        tnq->msg = &tnq[1];
        tsm = (struct  GNUNET_EXIT_TcpServiceStartMessage *) &tnq[1];
@@ -1213,7 +1213,7 @@
          GNUNET_break (0);
          return;
        }
-       tnq = GNUNET_malloc (sizeof (struct TunnelMessageQueueEntry) + mlen);
+       tnq = GNUNET_malloc (sizeof (struct ChannelMessageQueueEntry) + mlen);
        tnq->len = mlen;
        tnq->msg = &tnq[1];
        tim = (struct  GNUNET_EXIT_TcpInternetStartMessage *) &tnq[1];
@@ -1252,7 +1252,7 @@
        GNUNET_break (0);
        return;
       }
-      tnq = GNUNET_malloc (sizeof (struct TunnelMessageQueueEntry) + mlen);
+      tnq = GNUNET_malloc (sizeof (struct ChannelMessageQueueEntry) + mlen);
       tnq->len = mlen;
       tnq->msg = &tnq[1];
       tdm = (struct  GNUNET_EXIT_TcpDataMessage *) &tnq[1];
@@ -1278,7 +1278,7 @@
        GNUNET_break (0);
        return;
       }
-      tnq = GNUNET_malloc (sizeof (struct TunnelMessageQueueEntry) + mlen);
+      tnq = GNUNET_malloc (sizeof (struct ChannelMessageQueueEntry) + mlen);
       tnq->msg = &tnq[1];
       ism = (struct GNUNET_EXIT_IcmpServiceMessage *) &tnq[1];
       ism->header.type = htons (GNUNET_MESSAGE_TYPE_VPN_ICMP_TO_SERVICE);
@@ -1360,7 +1360,7 @@
        GNUNET_break (0);
        return;
       }
-      tnq = GNUNET_malloc (sizeof (struct TunnelMessageQueueEntry) + mlen);
+      tnq = GNUNET_malloc (sizeof (struct ChannelMessageQueueEntry) + mlen);
       tnq->msg = &tnq[1];
       iim = (struct GNUNET_EXIT_IcmpInternetMessage *) &tnq[1];
       iim->header.type = htons (GNUNET_MESSAGE_TYPE_VPN_ICMP_TO_INTERNET);
@@ -1505,19 +1505,19 @@
     GNUNET_assert (0);
     break;
   }
-  send_to_tunnel (tnq, ts);
+  send_to_channel (tnq, ts);
 }
 
 
 /**
  * Receive packets from the helper-process (someone send to the local
- * virtual tunnel interface).  Find the destination mapping, and if it
- * exists, identify the correct MESH tunnel (or possibly create it)
+ * virtual channel interface).  Find the destination mapping, and if it
+ * exists, identify the correct MESH channel (or possibly create it)
  * and forward the packet.
  *
  * @param cls closure, NULL
  * @param client NULL
- * @param message message we got from the client (VPN tunnel interface)
+ * @param message message we got from the client (VPN channel interface)
  */
 static int
 message_token (void *cls,
@@ -1633,15 +1633,15 @@
 
 /**
  * Synthesize a plausible ICMP payload for an ICMP error
- * response on the given tunnel.
+ * response on the given channel.
  *
- * @param ts tunnel information
+ * @param ts channel information
  * @param ipp IPv4 header to fill in (ICMP payload)
  * @param udp "UDP" header to fill in (ICMP payload); might actually
  *            also be the first 8 bytes of the TCP header
  */
 static void
-make_up_icmpv4_payload (struct TunnelState *ts,
+make_up_icmpv4_payload (struct ChannelState *ts,
                        struct GNUNET_TUN_IPv4Header *ipp,
                        struct GNUNET_TUN_UdpHeader *udp)
 {
@@ -1659,15 +1659,15 @@
 
 /**
  * Synthesize a plausible ICMP payload for an ICMP error
- * response on the given tunnel.
+ * response on the given channel.
  *
- * @param ts tunnel information
+ * @param ts channel information
  * @param ipp IPv6 header to fill in (ICMP payload)
  * @param udp "UDP" header to fill in (ICMP payload); might actually
  *            also be the first 8 bytes of the TCP header
  */
 static void
-make_up_icmpv6_payload (struct TunnelState *ts,
+make_up_icmpv6_payload (struct ChannelState *ts,
                        struct GNUNET_TUN_IPv6Header *ipp,
                        struct GNUNET_TUN_UdpHeader *udp)
 {
@@ -1684,23 +1684,23 @@
 
 
 /**
- * We got an ICMP packet back from the MESH tunnel.  Pass it on to the
+ * We got an ICMP packet back from the MESH channel.  Pass it on to the
  * local virtual interface via the helper.
  *
  * @param cls closure, NULL
- * @param tunnel connection to the other end
- * @param tunnel_ctx pointer to our 'struct TunnelState *'
+ * @param channel connection to the other end
+ * @param channel_ctx pointer to our 'struct ChannelState *'
  * @param message the actual message
  * @return #GNUNET_OK to keep the connection open,
  *         #GNUNET_SYSERR to close it (signal serious error)
  */
 static int
 receive_icmp_back (void *cls,
-                  struct GNUNET_MESH_Tunnel *tunnel,
-                  void **tunnel_ctx,
+                  struct GNUNET_MESH_Channel *channel,
+                  void **channel_ctx,
                   const struct GNUNET_MessageHeader *message)
 {
-  struct TunnelState *ts = *tunnel_ctx;
+  struct ChannelState *ts = *channel_ctx;
   const struct GNUNET_EXIT_IcmpToVPNMessage *i2v;
   size_t mlen;
 
@@ -2015,7 +2015,7 @@
   default:
     GNUNET_assert (0);
   }
-  GNUNET_CONTAINER_heap_update_cost (tunnel_heap,
+  GNUNET_CONTAINER_heap_update_cost (channel_heap,
                                     ts->heap_node,
                                     GNUNET_TIME_absolute_get ().abs_value_us);
   return GNUNET_OK;
@@ -2023,23 +2023,23 @@
 
 
 /**
- * We got a UDP packet back from the MESH tunnel.  Pass it on to the
+ * We got a UDP packet back from the MESH channel.  Pass it on to the
  * local virtual interface via the helper.
  *
  * @param cls closure, NULL
- * @param tunnel connection to the other end
- * @param tunnel_ctx pointer to our 'struct TunnelState *'
+ * @param channel connection to the other end
+ * @param channel_ctx pointer to our 'struct ChannelState *'
  * @param message the actual message
  * @return #GNUNET_OK to keep the connection open,
  *         #GNUNET_SYSERR to close it (signal serious error)
  */
 static int
 receive_udp_back (void *cls,
-                 struct GNUNET_MESH_Tunnel *tunnel,
-                  void **tunnel_ctx,
+                 struct GNUNET_MESH_Channel *channel,
+                  void **channel_ctx,
                   const struct GNUNET_MessageHeader *message)
 {
-  struct TunnelState *ts = *tunnel_ctx;
+  struct ChannelState *ts = *channel_ctx;
   const struct GNUNET_EXIT_UdpReplyMessage *reply;
   size_t mlen;
 
@@ -2170,7 +2170,7 @@
   default:
     GNUNET_assert (0);
   }
-  GNUNET_CONTAINER_heap_update_cost (tunnel_heap,
+  GNUNET_CONTAINER_heap_update_cost (channel_heap,
                                     ts->heap_node,
                                     GNUNET_TIME_absolute_get ().abs_value_us);
   return GNUNET_OK;
@@ -2178,23 +2178,23 @@
 
 
 /**
- * We got a TCP packet back from the MESH tunnel.  Pass it on to the
+ * We got a TCP packet back from the MESH channel.  Pass it on to the
  * local virtual interface via the helper.
  *
  * @param cls closure, NULL
- * @param tunnel connection to the other end
- * @param tunnel_ctx pointer to our `struct TunnelState *`
+ * @param channel connection to the other end
+ * @param channel_ctx pointer to our `struct ChannelState *`
  * @param message the actual message
  * @return #GNUNET_OK to keep the connection open,
  *         #GNUNET_SYSERR to close it (signal serious error)
  */
 static int
 receive_tcp_back (void *cls,
-                 struct GNUNET_MESH_Tunnel *tunnel,
-                  void **tunnel_ctx,
+                 struct GNUNET_MESH_Channel *channel,
+                  void **channel_ctx,
                   const struct GNUNET_MessageHeader *message)
 {
-  struct TunnelState *ts = *tunnel_ctx;
+  struct ChannelState *ts = *channel_ctx;
   const struct GNUNET_EXIT_TcpDataMessage *data;
   size_t mlen;
 
@@ -2312,7 +2312,7 @@
     }
     break;
   }
-  GNUNET_CONTAINER_heap_update_cost (tunnel_heap,
+  GNUNET_CONTAINER_heap_update_cost (channel_heap,
                                     ts->heap_node,
                                     GNUNET_TIME_absolute_get ().abs_value_us);
   return GNUNET_OK;
@@ -2320,7 +2320,7 @@
 
 
 /**
- * Allocate an IPv4 address from the range of the tunnel
+ * Allocate an IPv4 address from the range of the channel
  * for a new redirection.
  *
  * @param v4 where to store the address
@@ -2371,7 +2371,7 @@
 
 
 /**
- * Allocate an IPv6 address from the range of the tunnel
+ * Allocate an IPv6 address from the range of the channel
  * for a new redirection.
  *
  * @param v6 where to store the address
@@ -2441,7 +2441,7 @@
 static void
 free_destination_entry (struct DestinationEntry *de)
 {
-  struct DestinationTunnel *dt;
+  struct DestinationChannel *dt;
 
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
              "Cleaning up destination entry\n");
@@ -2452,7 +2452,7 @@
   {
     if (NULL != dt->ts)
     {
-      free_tunnel_state (dt->ts);
+      free_channel_state (dt->ts);
       GNUNET_assert (NULL == dt->ts);
     }
     GNUNET_CONTAINER_DLL_remove (de->dt_head,
@@ -2691,8 +2691,8 @@
   void *addr;
   struct DestinationEntry *de;
   struct GNUNET_HashCode key;
-  struct TunnelState *ts;
-  struct DestinationTunnel *dt;
+  struct ChannelState *ts;
+  struct DestinationChannel *dt;
 
   /*  parse request */
   msg = (const struct RedirectToServiceRequestMessage *) message;
@@ -2749,12 +2749,12 @@
   while (GNUNET_CONTAINER_multihashmap_size (destination_map) > 
max_destination_mappings)
     expire_destination (de);
 
-  dt = GNUNET_new (struct DestinationTunnel);
+  dt = GNUNET_new (struct DestinationChannel);
   dt->destination = de;
   GNUNET_CONTAINER_DLL_insert (de->dt_head,
                               de->dt_tail,
                               dt);
-  ts = create_tunnel_to_destination (dt,
+  ts = create_channel_to_destination (dt,
                                     result_af);
   switch (result_af)
   {
@@ -2773,23 +2773,23 @@
 
 
 /**
- * Function called whenever a tunnel is destroyed.  Should clean up
+ * Function called whenever a channel is destroyed.  Should clean up
  * any associated state.
  *
  * @param cls closure (set from #GNUNET_MESH_connect)
- * @param tunnel connection to the other end (henceforth invalid)
- * @param tunnel_ctx place where local state associated
- *                   with the tunnel is stored (our `struct TunnelState`)
+ * @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
-tunnel_cleaner (void *cls,
-               const struct GNUNET_MESH_Tunnel *tunnel,
-               void *tunnel_ctx)
+channel_cleaner (void *cls,
+               const struct GNUNET_MESH_Channel *channel,
+               void *channel_ctx)
 {
-  struct TunnelState *ts = tunnel_ctx;
+  struct ChannelState *ts = channel_ctx;
 
-  ts->tunnel = NULL; /* we must not call GNUNET_MESH_tunnel_destroy() anymore 
*/
-  free_tunnel_state (ts);
+  ts->channel = NULL; /* we must not call GNUNET_MESH_channel_destroy() 
anymore */
+  free_channel_state (ts);
 }
 
 
@@ -2814,21 +2814,21 @@
 
 
 /**
- * Free memory occupied by an entry in the tunnel map.
+ * Free memory occupied by an entry in the channel map.
  *
  * @param cls unused
  * @param key unused
- * @param value a `struct TunnelState *`
+ * @param value a `struct ChannelState *`
  * @return #GNUNET_OK (continue to iterate)
  */
 static int
-cleanup_tunnel (void *cls,
+cleanup_channel (void *cls,
                const struct GNUNET_HashCode *key,
                void *value)
 {
-  struct TunnelState *ts = value;
+  struct ChannelState *ts = value;
 
-  free_tunnel_state (ts);
+  free_channel_state (ts);
   return GNUNET_OK;
 }
 
@@ -2860,18 +2860,18 @@
     GNUNET_CONTAINER_heap_destroy (destination_heap);
     destination_heap = NULL;
   }
-  if (NULL != tunnel_map)
+  if (NULL != channel_map)
   {
-    GNUNET_CONTAINER_multihashmap_iterate (tunnel_map,
-                                          &cleanup_tunnel,
+    GNUNET_CONTAINER_multihashmap_iterate (channel_map,
+                                          &cleanup_channel,
                                           NULL);
-    GNUNET_CONTAINER_multihashmap_destroy (tunnel_map);
-    tunnel_map = NULL;
+    GNUNET_CONTAINER_multihashmap_destroy (channel_map);
+    channel_map = NULL;
   }
-  if (NULL != tunnel_heap)
+  if (NULL != channel_heap)
   {
-    GNUNET_CONTAINER_heap_destroy (tunnel_heap);
-    tunnel_heap = NULL;
+    GNUNET_CONTAINER_heap_destroy (channel_heap);
+    channel_heap = NULL;
   }
   if (NULL != mesh_handle)
   {
@@ -2954,13 +2954,13 @@
     max_destination_mappings = 200;
   if (GNUNET_OK !=
       GNUNET_CONFIGURATION_get_value_number (cfg, "VPN", "MAX_TUNNELS",
-                                            &max_tunnel_mappings))
-    max_tunnel_mappings = 200;
+                                            &max_channel_mappings))
+    max_channel_mappings = 200;
 
   destination_map = GNUNET_CONTAINER_multihashmap_create 
(max_destination_mappings * 2, GNUNET_NO);
   destination_heap = GNUNET_CONTAINER_heap_create 
(GNUNET_CONTAINER_HEAP_ORDER_MIN);
-  tunnel_map = GNUNET_CONTAINER_multihashmap_create (max_tunnel_mappings * 2, 
GNUNET_NO);
-  tunnel_heap = GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
+  channel_map = GNUNET_CONTAINER_multihashmap_create (max_channel_mappings * 
2, GNUNET_NO);
+  channel_heap = GNUNET_CONTAINER_heap_create 
(GNUNET_CONTAINER_HEAP_ORDER_MIN);
 
 
   vpn_argv[0] = GNUNET_strdup ("vpn-gnunet");
@@ -3058,7 +3058,7 @@
   mesh_handle =
     GNUNET_MESH_connect (cfg_, NULL,
                         NULL,
-                        &tunnel_cleaner,
+                        &channel_cleaner,
                         mesh_handlers,
                         NULL);
   helper_handle = GNUNET_HELPER_start (GNUNET_NO,




reply via email to

[Prev in Thread] Current Thread [Next in Thread]