refactor DHT for new service API
[oweals/gnunet.git] / src / cadet / cadet_api.c
index 5cb42286e794626def2e4bb436f4782e502d159f..d758155886c51312805bdd2dc636db01de2ecfbe 100644 (file)
@@ -1,20 +1,22 @@
 /*
      This file is part of GNUnet.
-     (C) 2011 Christian Grothoff (and other contributing authors)
+     Copyright (C) 2011 GNUnet e.V.
+
      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.
+     Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+     Boston, MA 02110-1301, USA.
 */
-
 /**
  * @file cadet/cadet_api.c
  * @brief cadet api: client implementation of new cadet service
@@ -23,6 +25,7 @@
 
 #include "platform.h"
 #include "gnunet_util_lib.h"
+#include "gnunet_constants.h"
 #include "gnunet_cadet_service.h"
 #include "cadet.h"
 #include "cadet_protocol.h"
@@ -38,7 +41,6 @@
  */
 struct GNUNET_CADET_TransmitHandle
 {
-
     /**
      * Double Linked list
      */
@@ -54,6 +56,11 @@ struct GNUNET_CADET_TransmitHandle
      */
   struct GNUNET_CADET_Channel *channel;
 
+    /**
+     * Request data task.
+     */
+  struct GNUNET_SCHEDULER_Task *request_data_task;
+
     /**
      * Callback to obtain the message to transmit, or NULL if we
      * got the message in 'data'.  Notice that messages built
@@ -68,20 +75,7 @@ struct GNUNET_CADET_TransmitHandle
   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 of the payload.
      */
   size_t size;
 };
@@ -120,11 +114,10 @@ union CadetInfoCB {
  */
 struct GNUNET_CADET_Handle
 {
-
     /**
-     * Handle to the server connection, to send messages later
+     * Message queue (if available).
      */
-  struct GNUNET_CLIENT_Connection *client;
+  struct GNUNET_MQ_Handle *mq;
 
     /**
      * Set of handlers used for processing incoming messages in the channels
@@ -139,12 +132,7 @@ struct GNUNET_CADET_Handle
   /**
    * Ports open.
    */
-  const uint32_t *ports;
-
-  /**
-   * Number of ports.
-   */
-  unsigned int n_ports;
+  struct GNUNET_CONTAINER_MultiHashMap *ports;
 
     /**
      * Double linked list of the channels this client is connected to, head.
@@ -156,21 +144,11 @@ struct GNUNET_CADET_Handle
      */
   struct GNUNET_CADET_Channel *channels_tail;
 
-    /**
-     * Callback for inbound channel creation
-     */
-  GNUNET_CADET_InboundChannelNotificationHandler *new_channel;
-
     /**
      * Callback for inbound channel disconnection
      */
   GNUNET_CADET_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
      */
@@ -191,12 +169,6 @@ struct GNUNET_CADET_Handle
      */
   CADET_ChannelNumber next_chid;
 
-    /**
-     * Have we started the task to receive messages from the service
-     * yet? We do this after we send the 'CADET_LOCAL_CONNECT' message.
-     */
-  int in_receive;
-
   /**
    * Configuration given by the client, in case of reconnection
    */
@@ -210,7 +182,7 @@ struct GNUNET_CADET_Handle
   /**
    * Task for trying to reconnect.
    */
-  GNUNET_SCHEDULER_TaskIdentifier reconnect_task;
+  struct GNUNET_SCHEDULER_Task * reconnect_task;
 
   /**
    * Callback for an info task (only one active at a time).
@@ -246,7 +218,6 @@ struct GNUNET_CADET_Peer
  */
 struct GNUNET_CADET_Channel
 {
-
     /**
      * DLL next
      */
@@ -268,9 +239,9 @@ struct GNUNET_CADET_Channel
   CADET_ChannelNumber chid;
 
     /**
-     * Port number.
+     * Channel's port, if any.
      */
-  uint32_t port;
+  struct GNUNET_CADET_Port *port;
 
     /**
      * Other end of the channel.
@@ -299,6 +270,32 @@ struct GNUNET_CADET_Channel
 
 };
 
+/**
+ * Opaque handle to a port.
+ */
+struct GNUNET_CADET_Port
+{
+    /**
+     * Handle to the CADET session this port belongs to.
+     */
+  struct GNUNET_CADET_Handle *cadet;
+
+    /**
+     * Port ID.
+     */
+  struct GNUNET_HashCode *hash;
+
+    /**
+     * Callback handler for incoming channels on this port.
+     */
+  GNUNET_CADET_InboundChannelNotificationHandler *handler;
+
+    /**
+     * Closure for @a handler.
+     */
+  void *cls;
+};
+
 
 /**
  * Implementation state for cadet's message queue.
@@ -318,24 +315,6 @@ struct CadetMQState
 };
 
 
-/******************************************************************************/
-/***********************         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 cadet 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      *************************/
 /******************************************************************************/
@@ -345,8 +324,8 @@ send_callback (void *cls, size_t size, void *buf);
  *
  * @param th Transmission handle.
  *
- * @return GNUNET_YES if it is a payload packet,
- *         GNUNET_NO if it is a cadet management packet.
+ * @return #GNUNET_YES if it is a payload packet,
+ *         #GNUNET_NO if it is a cadet management packet.
  */
 static int
 th_is_payload (struct GNUNET_CADET_TransmitHandle *th)
@@ -356,34 +335,22 @@ th_is_payload (struct GNUNET_CADET_TransmitHandle *th)
 
 
 /**
- * Check whether there is any message ready in the queue and find the size.
+ * Find the Port struct for a hash.
  *
- * @param h Cadet handle.
+ * @param h CADET handle.
+ * @param hash HashCode for the port number.
  *
- * @return The size of the first ready message in the queue,
- *         0 if there is none.
+ * @return The port handle if known, NULL otherwise.
  */
-static size_t
-message_ready_size (struct GNUNET_CADET_Handle *h)
+static struct GNUNET_CADET_Port *
+find_port (const struct GNUNET_CADET_Handle *h,
+          const struct GNUNET_HashCode *hash)
 {
-  struct GNUNET_CADET_TransmitHandle *th;
-  struct GNUNET_CADET_Channel *ch;
+  struct GNUNET_CADET_Port *p;
 
-  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;
+  p = GNUNET_CONTAINER_multihashmap_get (h->ports, hash);
+
+  return p;
 }
 
 
@@ -456,6 +423,7 @@ create_channel (struct GNUNET_CADET_Handle *h, CADET_ChannelNumber chid)
  *
  * @return Handle to the required channel or NULL if not found.
  */
+// FIXME: simplify: call_cleaner is always #GNUNET_YES!!!
 static void
 destroy_channel (struct GNUNET_CADET_Channel *ch, int call_cleaner)
 {
@@ -491,22 +459,8 @@ destroy_channel (struct GNUNET_CADET_Channel *ch, int call_cleaner)
      * Management traffic should be ok, as clients can't cancel that.
      * If the service crashed and we are reconnecting, it's ok.
      */
-    GNUNET_break (GNUNET_NO == th_is_payload (th)
-                  || GNUNET_NO == h->in_receive);
-    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 cadet 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;
+    GNUNET_break (GNUNET_NO == th_is_payload (th));
+    GNUNET_CADET_notify_transmit_ready_cancel (th);
   }
 
   if (0 != ch->peer)
@@ -516,33 +470,6 @@ destroy_channel (struct GNUNET_CADET_Channel *ch, int call_cleaner)
 }
 
 
-/**
- * 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_CADET_TransmitHandle *th = cls;
-  struct GNUNET_CADET_Handle *cadet;
-
-  cadet = th->channel->cadet;
-  GNUNET_CONTAINER_DLL_remove (cadet->th_head, cadet->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 (cadet)) && (NULL != cadet->th))
-  {
-    /* nothing ready to transmit, no point in asking for transmission */
-    GNUNET_CLIENT_notify_transmit_ready_cancel (cadet->th);
-    cadet->th = NULL;
-  }
-}
-
-
 /**
  * Add a transmit handle to the transmission queue and set the
  * timeout if needed.
@@ -555,27 +482,27 @@ add_to_queue (struct GNUNET_CADET_Handle *h,
               struct GNUNET_CADET_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.
+ * Remove a transmit handle from the transmission queue, if present.
  *
- * @param h cadet handle
- * @param msg message to transmit
- * @param channel channel this send is related to (NULL if N/A)
+ * Safe to call even if not queued.
+ *
+ * @param th handle to the packet to be unqueued.
  */
 static void
-send_packet (struct GNUNET_CADET_Handle *h,
-             const struct GNUNET_MessageHeader *msg,
-             struct GNUNET_CADET_Channel *channel);
+remove_from_queue (struct GNUNET_CADET_TransmitHandle *th)
+{
+  struct GNUNET_CADET_Handle *h = th->channel->cadet;
+
+  /* It might or might not have been queued (rarely not), but check anyway. */
+  if (NULL != th->next || h->th_tail == th)
+  {
+    GNUNET_CONTAINER_DLL_remove (h->th_head, h->th_tail, th);
+  }
+}
 
 
 /**
@@ -586,170 +513,59 @@ send_packet (struct GNUNET_CADET_Handle *h,
 static void
 send_ack (struct GNUNET_CADET_Channel *ch)
 {
-  struct GNUNET_CADET_LocalAck msg;
+  struct GNUNET_CADET_LocalAck *msg;
+  struct GNUNET_MQ_Envelope *env;
+
+  env = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_CADET_LOCAL_ACK);
 
   LOG (GNUNET_ERROR_TYPE_DEBUG, "Sending ACK on channel %X\n", ch->chid);
-  msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_LOCAL_ACK);
-  msg.header.size = htons (sizeof (msg));
-  msg.channel_id = htonl (ch->chid);
+  msg->channel_id = htonl (ch->chid);
+  GNUNET_MQ_send (ch->cadet->mq, env);
 
-  send_packet (ch->cadet, &msg.header, ch);
   return;
 }
 
 
 
-/**
- * Reconnect callback: tries to reconnect again after a failer previous
- * reconnecttion
- * @param cls closure (cadet 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 cadet handle.
- *
- */
-static void
-send_connect (struct GNUNET_CADET_Handle *h)
-{
-  size_t size;
-
-  size = sizeof (struct GNUNET_CADET_ClientConnect);
-  size += h->n_ports * sizeof (uint32_t);
-  {
-    char buf[size] GNUNET_ALIGN;
-    struct GNUNET_CADET_ClientConnect *msg;
-    uint32_t *ports;
-    uint16_t i;
-
-    /* build connection packet */
-    msg = (struct GNUNET_CADET_ClientConnect *) buf;
-    msg->header.type = htons (GNUNET_MESSAGE_TYPE_CADET_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);
-  }
-}
+/******************************************************************************/
+/***********************      RECEIVE HANDLERS     ****************************/
+/******************************************************************************/
 
 
 /**
- * Reconnect to the service, retransmit all infomation to try to restore the
- * original state.
- *
- * @param h handle to the cadet
+ * Call the @a notify callback given to #GNUNET_CADET_notify_transmit_ready to
+ * request the data to send over MQ. Since MQ manages the queue, this function
+ * is scheduled immediatly after a transmit ready notification.
  *
- * @return GNUNET_YES in case of sucess, GNUNET_NO otherwise (service down...)
- */
-static int
-do_reconnect (struct GNUNET_CADET_Handle *h)
-{
-  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 ("cadet", 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);
-  return GNUNET_YES;
-}
-
-/**
- * Reconnect callback: tries to reconnect again after a failer previous
- * reconnecttion
- * @param cls closure (cadet handle)
- * @param tc task context
+ * @param cls Closure (transmit handle).
  */
 static void
-reconnect_cbk (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
+request_data (void *cls)
 {
-  struct GNUNET_CADET_Handle *h = cls;
+  struct GNUNET_CADET_TransmitHandle *th = cls;
+  struct GNUNET_CADET_LocalData *msg;
+  struct GNUNET_MQ_Envelope *env;
+  size_t osize;
 
-  h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
-  if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
-    return;
-  do_reconnect (h);
-}
+  LOG (GNUNET_ERROR_TYPE_DEBUG, "Requesting Data: %u bytes\n", th->size);
 
+  GNUNET_assert (GNUNET_YES == th->channel->allow_send);
+  th->channel->allow_send = GNUNET_NO;
+  th->request_data_task = NULL;
+  th->channel->packet_size = 0;
+  remove_from_queue (th);
 
-/**
- * Reconnect to the service, retransmit all infomation to try to restore the
- * original state.
- *
- * @param h handle to the cadet
- *
- * @return GNUNET_YES in case of sucess, GNUNET_NO otherwise (service down...)
- */
-static void
-reconnect (struct GNUNET_CADET_Handle *h)
-{
-  struct GNUNET_CADET_Channel *ch;
-  struct GNUNET_CADET_Channel *next;
+  env = GNUNET_MQ_msg_extra (msg, th->size,
+                             GNUNET_MESSAGE_TYPE_CADET_LOCAL_DATA);
+  msg->id = htonl (th->channel->chid);
+  osize = th->notify (th->notify_cls, th->size, &msg[1]);
+  GNUNET_assert (osize == th->size);
+  GNUNET_MQ_send (th->channel->cadet->mq, env);
 
-  LOG (GNUNET_ERROR_TYPE_DEBUG,
-       "Requested RECONNECT, destroying all channels\n");
-  h->in_receive = GNUNET_NO;
-  for (ch = h->channels_head; NULL != ch; ch = next)
-  {
-    next = ch->next;
-    destroy_channel (ch, GNUNET_YES);
-  }
-  if (GNUNET_SCHEDULER_NO_TASK == h->reconnect_task)
-    h->reconnect_task = GNUNET_SCHEDULER_add_delayed (h->reconnect_time,
-                                                      &reconnect_cbk, h);
+  GNUNET_free (th);
 }
 
 
-/******************************************************************************/
-/***********************      RECEIVE HANDLERS     ****************************/
-/******************************************************************************/
-
 /**
  * Process the new channel notification and add it to the channels in the handle
  *
@@ -757,22 +573,26 @@ reconnect (struct GNUNET_CADET_Handle *h)
  * @param msg   A message with the details of the new incoming channel
  */
 static void
-process_channel_created (struct GNUNET_CADET_Handle *h,
-                        const struct GNUNET_CADET_ChannelMessage *msg)
+handle_channel_created (void *cls,
+                        const struct GNUNET_CADET_ChannelCreateMessage *msg)
 {
+  struct GNUNET_CADET_Handle *h = cls;
   struct GNUNET_CADET_Channel *ch;
+  struct GNUNET_CADET_Port *port;
+  const struct GNUNET_HashCode *port_number;
   CADET_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);
+  port_number = &msg->port;
+  LOG (GNUNET_ERROR_TYPE_DEBUG, "Creating incoming channel %X [%s]\n",
+       chid, GNUNET_h2s (port_number));
   if (chid < GNUNET_CADET_LOCAL_CHANNEL_ID_SERV)
   {
     GNUNET_break (0);
     return;
   }
-  if (NULL != h->new_channel)
+  port = find_port (h, port_number);
+  if (NULL != port)
   {
     void *ctx;
 
@@ -785,25 +605,20 @@ process_channel_created (struct GNUNET_CADET_Handle *h,
     ch->options = ntohl (msg->opt);
 
     LOG (GNUNET_ERROR_TYPE_DEBUG, "  created channel %p\n", ch);
-    ctx = h->new_channel (h->cls, ch, &msg->peer, ch->port, ch->options);
+    ctx = port->handler (port->cls, ch, &msg->peer, port->hash, ch->options);
     if (NULL != ctx)
       ch->ctx = ctx;
     LOG (GNUNET_ERROR_TYPE_DEBUG, "User notified\n");
   }
   else
   {
-    struct GNUNET_CADET_ChannelMessage d_msg;
+    struct GNUNET_CADET_ChannelDestroyMessage *d_msg;
+    struct GNUNET_MQ_Envelope *env;
 
     LOG (GNUNET_ERROR_TYPE_DEBUG, "No handler for incoming channels\n");
-
-    d_msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DESTROY);
-    d_msg.header.size = htons (sizeof (struct GNUNET_CADET_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);
+    env = GNUNET_MQ_msg (d_msg, GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DESTROY);
+    d_msg->channel_id = msg->channel_id;
+    GNUNET_MQ_send (h->mq, env);
   }
   return;
 }
@@ -816,14 +631,15 @@ process_channel_created (struct GNUNET_CADET_Handle *h,
  * @param msg   A message with the details of the channel being destroyed
  */
 static void
-process_channel_destroy (struct GNUNET_CADET_Handle *h,
-                         const struct GNUNET_CADET_ChannelMessage *msg)
+handle_channel_destroy (void *cls,
+                        const struct GNUNET_CADET_ChannelDestroyMessage *msg)
 {
+  struct GNUNET_CADET_Handle *h = cls;
   struct GNUNET_CADET_Channel *ch;
   CADET_ChannelNumber chid;
 
-  LOG (GNUNET_ERROR_TYPE_DEBUG, "Channel Destroy received from service\n");
   chid = ntohl (msg->channel_id);
+  LOG (GNUNET_ERROR_TYPE_DEBUG, "Channel %X Destroy from service\n", chid);
   ch = retrieve_channel (h, chid);
 
   if (NULL == ch)
@@ -831,54 +647,71 @@ process_channel_destroy (struct GNUNET_CADET_Handle *h,
     LOG (GNUNET_ERROR_TYPE_DEBUG, "channel %X unknown\n", chid);
     return;
   }
-  LOG (GNUNET_ERROR_TYPE_DEBUG, " destroying channel %X\n", ch->chid);
   destroy_channel (ch, GNUNET_YES);
 }
 
 
+/**
+ * Check that message received from CADET service is well-formed.
+ *
+ * @param cls the `struct GNUNET_CADET_Handle`
+ * @param message the message we got
+ * @return #GNUNET_OK if the message is well-formed,
+ *         #GNUNET_SYSERR otherwise
+ */
+static int
+check_local_data (void *cls,
+                  const struct GNUNET_CADET_LocalData *message)
+{
+  struct GNUNET_CADET_Handle *h = cls;
+  struct GNUNET_CADET_Channel *ch;
+  uint16_t size;
+
+  size = ntohs (message->header.size);
+  if (sizeof (*message) + sizeof (struct GNUNET_MessageHeader) > size)
+  {
+    GNUNET_break_op (0);
+    return GNUNET_SYSERR;
+  }
+
+  ch = retrieve_channel (h, ntohl (message->id));
+  if (NULL == ch)
+  {
+    GNUNET_break_op (0);
+    return GNUNET_SYSERR;
+  }
+
+  return GNUNET_OK;
+}
+
+
 /**
  * Process the incoming data packets, call appropriate handlers.
  *
- * @param h         The cadet handle
- * @param message   A message encapsulating the data
+ * @param h       The cadet handle
+ * @param message A message encapsulating the data
  */
 static void
-process_incoming_data (struct GNUNET_CADET_Handle *h,
-                       const struct GNUNET_MessageHeader *message)
+handle_local_data (void *cls,
+                   const struct GNUNET_CADET_LocalData *message)
 {
+  struct GNUNET_CADET_Handle *h = cls;
   const struct GNUNET_MessageHeader *payload;
   const struct GNUNET_CADET_MessageHandler *handler;
-  struct GNUNET_CADET_LocalData *dmsg;
   struct GNUNET_CADET_Channel *ch;
-  size_t size;
   unsigned int i;
   uint16_t type;
 
   LOG (GNUNET_ERROR_TYPE_DEBUG, "Got a data message!\n");
-  dmsg = (struct GNUNET_CADET_LocalData *) message;
-  ch = retrieve_channel (h, ntohl (dmsg->id));
-  if (NULL == ch)
-  {
-    GNUNET_break (0);
-    return;
-  }
+  ch = retrieve_channel (h, ntohl (message->id));
+  GNUNET_assert (NULL != ch);
 
-  payload = (struct GNUNET_MessageHeader *) &dmsg[1];
+  payload = (struct GNUNET_MessageHeader *) &message[1];
   LOG (GNUNET_ERROR_TYPE_DEBUG, "  %s data on channel %s [%X]\n",
        GC_f2s (ch->chid >= GNUNET_CADET_LOCAL_CHANNEL_ID_SERV),
-       GNUNET_i2s (GNUNET_PEER_resolve2 (ch->peer)), ntohl (dmsg->id));
-
-  size = ntohs (message->size);
-  LOG (GNUNET_ERROR_TYPE_DEBUG, "  %u bytes\n", size);
+       GNUNET_i2s (GNUNET_PEER_resolve2 (ch->peer)), ntohl (message->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);
-  size = ntohs (payload->size);
   LOG (GNUNET_ERROR_TYPE_DEBUG, "  payload type %s\n", GC_m2s (type));
   for (i = 0; i < h->n_handlers; i++)
   {
@@ -892,13 +725,13 @@ process_incoming_data (struct GNUNET_CADET_Handle *h,
       {
         LOG (GNUNET_ERROR_TYPE_DEBUG, "callback caused disconnection\n");
         GNUNET_CADET_channel_destroy (ch);
-        return;
+        break;
       }
       else
       {
         LOG (GNUNET_ERROR_TYPE_DEBUG,
              "callback completed successfully\n");
-        return;
+        break;
       }
     }
   }
@@ -913,16 +746,15 @@ process_incoming_data (struct GNUNET_CADET_Handle *h,
  * @param message Message itself.
  */
 static void
-process_ack (struct GNUNET_CADET_Handle *h,
-             const struct GNUNET_MessageHeader *message)
+handle_local_ack (void *cls,
+                  const struct GNUNET_CADET_LocalAck *message)
 {
-  struct GNUNET_CADET_LocalAck *msg;
+  struct GNUNET_CADET_Handle *h = cls;
   struct GNUNET_CADET_Channel *ch;
   CADET_ChannelNumber chid;
 
   LOG (GNUNET_ERROR_TYPE_DEBUG, "Got an ACK!\n");
-  msg = (struct GNUNET_CADET_LocalAck *) message;
-  chid = ntohl (msg->channel_id);
+  chid = ntohl (message->channel_id);
   ch = retrieve_channel (h, chid);
   if (NULL == ch)
   {
@@ -931,15 +763,70 @@ process_ack (struct GNUNET_CADET_Handle *h,
   }
   LOG (GNUNET_ERROR_TYPE_DEBUG, "  on channel %X!\n", ch->chid);
   ch->allow_send = GNUNET_YES;
-  if (NULL == h->th && 0 < ch->packet_size)
+  if (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);
+    struct GNUNET_CADET_TransmitHandle *th;
+    struct GNUNET_CADET_TransmitHandle *next;
+    LOG (GNUNET_ERROR_TYPE_DEBUG,
+         "  pending data, sending %u bytes!\n",
+         ch->packet_size);
+    for (th = h->th_head; NULL != th; th = next)
+    {
+      next = th->next;
+      if (th->channel == ch)
+      {
+        GNUNET_assert (NULL == th->request_data_task);
+        th->request_data_task = GNUNET_SCHEDULER_add_now (&request_data, th);
+        break;
+      }
+    }
+    /* Complain if we got thru all th without sending anything, ch was wrong */
+    GNUNET_break (NULL != th);
   }
 }
 
+/**
+ * Reconnect to the service, retransmit all infomation to try to restore the
+ * original state.
+ *
+ * @param h handle to the cadet
+ *
+ * @return #GNUNET_YES in case of sucess, #GNUNET_NO otherwise (service down...)
+ */
+static void
+reconnect (struct GNUNET_CADET_Handle *h);
+
+
+/**
+ * Reconnect callback: tries to reconnect again after a failer previous
+ * reconnection.
+ *
+ * @param cls closure (cadet handle)
+ */
+static void
+reconnect_cbk (void *cls);
+
+
+/**
+ * Generic error handler, called with the appropriate error code and
+ * the same closure specified at the creation of the message queue.
+ * Not every message queue implementation supports an error handler.
+ *
+ * @param cls closure, a `struct GNUNET_CORE_Handle *`
+ * @param error error code
+ */
+static void
+handle_mq_error (void *cls,
+                 enum GNUNET_MQ_Error error)
+{
+  struct GNUNET_CADET_Handle *h = cls;
+
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MQ ERROR: %u\n", error);
+  GNUNET_MQ_destroy (h->mq);
+  h->mq = NULL;
+  reconnect (h);
+}
+
 
 /*
  * Process a local reply about info on all channels, pass info to the user.
@@ -1028,36 +915,51 @@ process_ack (struct GNUNET_CADET_Handle *h,
 
 
 /**
- * Process a local reply about info on all tunnels, pass info to the user.
+ * Check that message received from CADET service is well-formed.
  *
- * @param h Cadet handle.
- * @param message Message itself.
+ * @param cls the `struct GNUNET_CADET_Handle`
+ * @param message the message we got
+ * @return #GNUNET_OK if the message is well-formed,
+ *         #GNUNET_SYSERR otherwise
  */
-static void
-process_get_peers (struct GNUNET_CADET_Handle *h,
-                     const struct GNUNET_MessageHeader *message)
+static int
+check_get_peers (void *cls,
+                 const struct GNUNET_CADET_LocalInfoPeer *message)
 {
-  struct GNUNET_CADET_LocalInfoPeer *msg;
+  struct GNUNET_CADET_Handle *h = cls;
   uint16_t size;
 
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Get Peer messasge received\n");
-
   if (NULL == h->info_cb.peers_cb)
   {
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "  ignored\n");
-    return;
+    GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
+                "  no handler for peesr monitor message!\n");
+    return GNUNET_SYSERR;
   }
 
-  size = ntohs (message->size);
+  size = ntohs (message->header.size);
   if (sizeof (struct GNUNET_CADET_LocalInfoPeer) > size)
   {
     h->info_cb.peers_cb (h->info_cls, NULL, -1, 0, 0);
     h->info_cb.peers_cb = NULL;
     h->info_cls = NULL;
-    return;
+    return GNUNET_SYSERR;
   }
 
-  msg = (struct GNUNET_CADET_LocalInfoPeer *) message;
+  return GNUNET_OK;
+}
+
+
+/**
+ * Process a local reply about info on all tunnels, pass info to the user.
+ *
+ * @param cls Closure (Cadet handle).
+ * @param msg Message itself.
+ */
+static void
+handle_get_peers (void *cls,
+                  const struct GNUNET_CADET_LocalInfoPeer *msg)
+{
+  struct GNUNET_CADET_Handle *h = cls;
   h->info_cb.peers_cb (h->info_cls, &msg->destination,
                        (int) ntohs (msg->tunnel),
                        (unsigned int ) ntohs (msg->paths),
@@ -1066,86 +968,170 @@ process_get_peers (struct GNUNET_CADET_Handle *h,
 
 
 /**
- * Process a local peer info reply, pass info to the user.
+ * Check that message received from CADET service is well-formed.
  *
- * @param h Cadet handle.
- * @param message Message itself.
+ * @param cls the `struct GNUNET_CADET_Handle`
+ * @param message the message we got
+ * @return #GNUNET_OK if the message is well-formed,
+ *         #GNUNET_SYSERR otherwise
  */
-static void
-process_get_peer (struct GNUNET_CADET_Handle *h,
-                  const struct GNUNET_MessageHeader *message)
+static int
+check_get_peer (void *cls,
+                const struct GNUNET_CADET_LocalInfoPeer *message)
 {
-  struct GNUNET_CADET_LocalInfoTunnel *msg;
+  struct GNUNET_CADET_Handle *h = cls;
+  const size_t msize = sizeof (struct GNUNET_CADET_LocalInfoPeer);
+  struct GNUNET_PeerIdentity *paths_array;
   size_t esize;
-  size_t msize;
+  unsigned int epaths;
+  unsigned int paths;
+  unsigned int peers;
 
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Get Tunnel messasge received\n");
   if (NULL == h->info_cb.peer_cb)
   {
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "  ignored\n");
-    return;
+    GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
+                "  no handler for peer monitor message!\n");
+    goto clean_cls;
   }
 
   /* Verify message sanity */
-  msg = (struct GNUNET_CADET_LocalInfoTunnel *) message;
-  msize = ntohs (message->size);
-  esize = sizeof (struct GNUNET_CADET_LocalInfoPeer);
-  if (esize > msize)
+  esize = ntohs (message->header.size);
+  if (esize < msize)
   {
     GNUNET_break_op (0);
     h->info_cb.peer_cb (h->info_cls, NULL, 0, 0, 0, NULL);
     goto clean_cls;
   }
-//   esize += ch_n * sizeof (CADET_ChannelNumber);
-//   esize += c_n * sizeof (struct GNUNET_CADET_Hash);
-  if (msize != esize)
+  if (0 != ((esize - msize) % sizeof (struct GNUNET_PeerIdentity)))
+  {
+    GNUNET_break_op (0);
+    h->info_cb.peer_cb (h->info_cls, NULL, 0, 0, 0, NULL);
+    goto clean_cls;
+
+  }
+  peers = (esize - msize) / sizeof (struct GNUNET_PeerIdentity);
+  epaths = (unsigned int) ntohs (message->paths);
+  paths_array = (struct GNUNET_PeerIdentity *) &message[1];
+  paths = 0;
+  for (int i = 0; i < peers; i++)
+  {
+    if (0 == memcmp (&paths_array[i], &message->destination,
+                     sizeof (struct GNUNET_PeerIdentity)))
+    {
+      paths++;
+    }
+  }
+  if (paths != epaths)
   {
     GNUNET_break_op (0);
-    GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "m:%u, e: %u\n", msize, esize);
+    GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "p:%u, e: %u\n", paths, epaths);
     h->info_cb.peer_cb (h->info_cls, NULL, 0, 0, 0, NULL);
     goto clean_cls;
   }
 
-  /* Call Callback with tunnel info. */
-  h->info_cb.peer_cb (h->info_cls, &msg->destination, 0, 0, 0, NULL);
+  return GNUNET_OK;
 
-  clean_cls:
+clean_cls:
   h->info_cb.peer_cb = NULL;
   h->info_cls = NULL;
+  return GNUNET_SYSERR;
 }
 
 
 /**
- * Process a local reply about info on all tunnels, pass info to the user.
+ * Process a local peer info reply, pass info to the user.
  *
- * @param h Cadet handle.
+ * @param cls Closure (Cadet handle).
  * @param message Message itself.
  */
 static void
-process_get_tunnels (struct GNUNET_CADET_Handle *h,
-                     const struct GNUNET_MessageHeader *message)
+handle_get_peer (void *cls,
+                 const struct GNUNET_CADET_LocalInfoPeer *message)
 {
-  struct GNUNET_CADET_LocalInfoTunnel *msg;
-  uint16_t size;
+  struct GNUNET_CADET_Handle *h = cls;
+  struct GNUNET_PeerIdentity *paths_array;
+  unsigned int paths;
+  unsigned int path_length;
+  int neighbor;
+  unsigned int peers;
+
+  paths = (unsigned int) ntohs (message->paths);
+  paths_array = (struct GNUNET_PeerIdentity *) &message[1];
+  peers = (ntohs (message->header.size) - sizeof (*message))
+          / sizeof (struct GNUNET_PeerIdentity);
+  path_length = 0;
+  neighbor = GNUNET_NO;
+
+  for (int i = 0; i < peers; i++)
+  {
+    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " %s\n", GNUNET_i2s (&paths_array[i]));
+    path_length++;
+    if (0 == memcmp (&paths_array[i], &message->destination,
+                     sizeof (struct GNUNET_PeerIdentity)))
+    {
+      if (1 == path_length)
+        neighbor = GNUNET_YES;
+      path_length = 0;
+    }
+  }
+
+  /* Call Callback with tunnel info. */
+  paths_array = (struct GNUNET_PeerIdentity *) &message[1];
+  h->info_cb.peer_cb (h->info_cls,
+                      &message->destination,
+                      (int) ntohs (message->tunnel),
+                      neighbor,
+                      paths,
+                      paths_array);
+}
+
 
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Get Tunnels messasge received\n");
+/**
+ * Check that message received from CADET service is well-formed.
+ *
+ * @param cls the `struct GNUNET_CADET_Handle`
+ * @param msg the message we got
+ * @return #GNUNET_OK if the message is well-formed,
+ *         #GNUNET_SYSERR otherwise
+ */
+static int
+check_get_tunnels (void *cls,
+                   const struct GNUNET_CADET_LocalInfoTunnel *msg)
+{
+  struct GNUNET_CADET_Handle *h = cls;
+  uint16_t size;
 
   if (NULL == h->info_cb.tunnels_cb)
   {
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "  ignored\n");
-    return;
+    GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
+                "  no handler for tunnels monitor message!\n");
+    return GNUNET_SYSERR;
   }
 
-  size = ntohs (message->size);
+  size = ntohs (msg->header.size);
   if (sizeof (struct GNUNET_CADET_LocalInfoTunnel) > size)
   {
     h->info_cb.tunnels_cb (h->info_cls, NULL, 0, 0, 0, 0);
     h->info_cb.tunnels_cb = NULL;
     h->info_cls = NULL;
-    return;
+    return GNUNET_SYSERR;
   }
+  return GNUNET_OK;
+}
+
+
+/**
+ * Process a local reply about info on all tunnels, pass info to the user.
+ *
+ * @param cls Closure (Cadet handle).
+ * @param message Message itself.
+ */
+static void
+handle_get_tunnels (void *cls,
+                    const struct GNUNET_CADET_LocalInfoTunnel *msg)
+{
+  struct GNUNET_CADET_Handle *h = cls;
 
-  msg = (struct GNUNET_CADET_LocalInfoTunnel *) message;
   h->info_cb.tunnels_cb (h->info_cls, &msg->destination,
                          ntohl (msg->channels), ntohl (msg->connections),
                          ntohs (msg->estate), ntohs (msg->cstate));
@@ -1154,33 +1140,32 @@ process_get_tunnels (struct GNUNET_CADET_Handle *h,
 
 
 /**
- * Process a local tunnel info reply, pass info to the user.
+ * Check that message received from CADET service is well-formed.
  *
- * @param h Cadet handle.
- * @param message Message itself.
+ * @param cls the `struct GNUNET_CADET_Handle`
+ * @param msg the message we got
+ * @return #GNUNET_OK if the message is well-formed,
+ *         #GNUNET_SYSERR otherwise
  */
-static void
-process_get_tunnel (struct GNUNET_CADET_Handle *h,
-                    const struct GNUNET_MessageHeader *message)
+static int
+check_get_tunnel (void *cls,
+                  const struct GNUNET_CADET_LocalInfoTunnel *msg)
 {
-  struct GNUNET_CADET_LocalInfoTunnel *msg;
-  size_t esize;
-  size_t msize;
+  struct GNUNET_CADET_Handle *h = cls;
   unsigned int ch_n;
   unsigned int c_n;
-  struct GNUNET_CADET_Hash *conns;
-  CADET_ChannelNumber *chns;
+  size_t esize;
+  size_t msize;
 
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Get Tunnel messasge received\n");
   if (NULL == h->info_cb.tunnel_cb)
   {
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "  ignored\n");
-    return;
+    GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
+                "  no handler for tunnel monitor message!\n");
+    goto clean_cls;
   }
 
   /* Verify message sanity */
-  msg = (struct GNUNET_CADET_LocalInfoTunnel *) message;
-  msize = ntohs (message->size);
+  msize = ntohs (msg->header.size);
   esize = sizeof (struct GNUNET_CADET_LocalInfoTunnel);
   if (esize > msize)
   {
@@ -1195,270 +1180,161 @@ process_get_tunnel (struct GNUNET_CADET_Handle *h,
   if (msize != esize)
   {
     GNUNET_break_op (0);
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "m:%u, e: %u (%u ch, %u conn)\n",
-                msize, esize, ch_n, c_n);
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "%u (%u ch, %u conn)\n",
-                sizeof (struct GNUNET_CADET_LocalInfoTunnel),
-                sizeof (CADET_ChannelNumber), sizeof (struct GNUNET_HashCode));
+    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                "m:%u, e: %u (%u ch, %u conn)\n",
+                (unsigned int) msize,
+                (unsigned int) esize,
+                ch_n,
+                c_n);
     h->info_cb.tunnel_cb (h->info_cls, NULL, 0, 0, NULL, NULL, 0, 0);
     goto clean_cls;
   }
 
-  /* Call Callback with tunnel info. */
-  conns = (struct GNUNET_CADET_Hash *) &msg[1];
-  chns = (CADET_ChannelNumber *) &conns[c_n];
-  h->info_cb.tunnel_cb (h->info_cls, &msg->destination,
-                ch_n, c_n, chns, conns,
-                ntohs (msg->estate), ntohs (msg->cstate));
+  return GNUNET_OK;
 
 clean_cls:
   h->info_cb.tunnel_cb = NULL;
   h->info_cls = NULL;
+  return GNUNET_SYSERR;
 }
 
 
 /**
- * Function to process all messages received from the service
+ * Process a local tunnel info reply, pass info to the user.
  *
- * @param cls closure
- * @param msg message received, NULL on timeout or fatal error
+ * @param cls Closure (Cadet handle).
+ * @param msg Message itself.
  */
 static void
-msg_received (void *cls, const struct GNUNET_MessageHeader *msg)
+handle_get_tunnel (void *cls,
+                   const struct GNUNET_CADET_LocalInfoTunnel *msg)
 {
   struct GNUNET_CADET_Handle *h = cls;
-  uint16_t type;
+  unsigned int ch_n;
+  unsigned int c_n;
+  struct GNUNET_CADET_Hash *conns;
+  CADET_ChannelNumber *chns;
 
-  if (msg == NULL)
-  {
-    LOG (GNUNET_ERROR_TYPE_DEBUG,
-        "Cadet 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",
-       GC_m2s (type));
-  switch (type)
-  {
-    /* Notify of a new incoming channel */
-  case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_CREATE:
-    process_channel_created (h, (struct GNUNET_CADET_ChannelMessage *) msg);
-    break;
-    /* Notify of a channel disconnection */
-  case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DESTROY: /* TODO separate(gid problem)*/
-  case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_NACK:
-    process_channel_destroy (h, (struct GNUNET_CADET_ChannelMessage *) msg);
-    break;
-  case GNUNET_MESSAGE_TYPE_CADET_LOCAL_DATA:
-    process_incoming_data (h, msg);
-    break;
-  case GNUNET_MESSAGE_TYPE_CADET_LOCAL_ACK:
-    process_ack (h, msg);
-    break;
-//   case GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_CHANNELS:
-//     process_get_channels (h, msg);
-//     break;
-//   case GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_CHANNEL:
-//     process_show_channel (h, msg);
-//     break;
-  case GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEERS:
-    process_get_peers (h, msg);
-    break;
-  case GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEER:
-    process_get_peer (h, msg);
-    break;
-  case GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNELS:
-    process_get_tunnels (h, msg);
-    break;
-  case GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNEL:
-    process_get_tunnel (h, msg);
-    break;
-//   case GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_CHANNEL:
-//     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",
-         GC_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");
-  }
+  ch_n = ntohl (msg->channels);
+  c_n = ntohl (msg->connections);
+
+  /* Call Callback with tunnel info. */
+  conns = (struct GNUNET_CADET_Hash *) &msg[1];
+  chns = (CADET_ChannelNumber *) &conns[c_n];
+  h->info_cb.tunnel_cb (h->info_cls, &msg->destination,
+                        ch_n, c_n, chns, conns,
+                        ntohs (msg->estate), ntohs (msg->cstate));
 }
 
 
-/******************************************************************************/
-/************************       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.
+ * Reconnect to the service, retransmit all infomation to try to restore the
+ * original state.
  *
- * @param cls closure, the cadet 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
+ * @param h handle to the cadet
+ *
+ * @return GNUNET_YES in case of sucess, GNUNET_NO otherwise (service down...)
  */
-static size_t
-send_callback (void *cls, size_t size, void *buf)
+static int
+do_reconnect (struct GNUNET_CADET_Handle *h)
 {
-  struct GNUNET_CADET_Handle *h = cls;
-  struct GNUNET_CADET_TransmitHandle *th;
-  struct GNUNET_CADET_TransmitHandle *next;
-  struct GNUNET_CADET_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))
+  struct GNUNET_MQ_MessageHandler handlers[] = {
+    GNUNET_MQ_hd_fixed_size (channel_created,
+                             GNUNET_MESSAGE_TYPE_CADET_CHANNEL_CREATE,
+                             struct GNUNET_CADET_ChannelCreateMessage,
+                             h),
+    GNUNET_MQ_hd_fixed_size (channel_destroy,
+                             GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DESTROY,
+                             struct GNUNET_CADET_ChannelDestroyMessage,
+                             h),
+    GNUNET_MQ_hd_var_size (local_data,
+                           GNUNET_MESSAGE_TYPE_CADET_LOCAL_DATA,
+                           struct GNUNET_CADET_LocalData,
+                           h),
+    GNUNET_MQ_hd_fixed_size (local_ack,
+                             GNUNET_MESSAGE_TYPE_CADET_LOCAL_ACK,
+                             struct GNUNET_CADET_LocalAck,
+                             h),
+    GNUNET_MQ_hd_var_size (get_peers,
+                           GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEERS,
+                           struct GNUNET_CADET_LocalInfoPeer,
+                           h),
+    GNUNET_MQ_hd_var_size (get_peer,
+                           GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEER,
+                           struct GNUNET_CADET_LocalInfoPeer,
+                           h),
+    GNUNET_MQ_hd_var_size (get_tunnels,
+                           GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNELS,
+                           struct GNUNET_CADET_LocalInfoTunnel,
+                           h),
+    GNUNET_MQ_hd_var_size (get_tunnel,
+                           GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNEL,
+                           struct GNUNET_CADET_LocalInfoTunnel,
+                           h),
+  // FIXME
+//   GNUNET_MQ_hd_fixed_Y       size (channel_destroyed,
+//                            GNUNET_MESSAGE_TYPE_CADET_CHANNEL_NACK,
+//                            struct GNUNET_CADET_ChannelDestroyMessage);
+    GNUNET_MQ_handler_end ()
+  };
+
+  LOG (GNUNET_ERROR_TYPE_DEBUG, "Connecting to CADET\n");
+
+  GNUNET_assert (NULL == h->mq);
+  h->mq = GNUNET_CLIENT_connecT (h->cfg,
+                                 "cadet",
+                                 handlers,
+                                 &handle_mq_error,
+                                 h);
+  if (NULL == h->mq)
   {
-    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_CADET_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_CADET_LocalData *) cbuf;
-      mh = (struct GNUNET_MessageHeader *) &dmsg[1];
-      psize = th->notify (th->notify_cls,
-                          size - sizeof (struct GNUNET_CADET_LocalData),
-                          mh);
-      if (psize > 0)
-      {
-        psize += sizeof (struct GNUNET_CADET_LocalData);
-        GNUNET_assert (size >= psize);
-        dmsg->header.size = htons (psize);
-        dmsg->id = htonl (ch->chid);
-        dmsg->header.type = htons (GNUNET_MESSAGE_TYPE_CADET_LOCAL_DATA);
-        LOG (GNUNET_ERROR_TYPE_DEBUG, "#  payload type %s\n",
-             GC_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, "#  cadet internal traffic, type %s\n",
-           GC_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);
+    return GNUNET_NO;
   }
   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);
+    h->reconnect_time = GNUNET_TIME_UNIT_MILLISECONDS;
   }
-  LOG (GNUNET_ERROR_TYPE_DEBUG, "# Send packet() END\n");
-  return tsize;
+  return GNUNET_YES;
+}
+
+/**
+ * Reconnect callback: tries to reconnect again after a failer previous
+ * reconnecttion
+ *
+ * @param cls closure (cadet handle)
+ */
+static void
+reconnect_cbk (void *cls)
+{
+  struct GNUNET_CADET_Handle *h = cls;
+
+  h->reconnect_task = NULL;
+  do_reconnect (h);
 }
 
 
 /**
- * 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.
+ * Reconnect to the service, retransmit all infomation to try to restore the
+ * original state.
  *
- * @param h cadet handle
- * @param msg message to transmit
- * @param channel channel this send is related to (NULL if N/A)
+ * @param h handle to the cadet
+ *
+ * @return #GNUNET_YES in case of sucess, #GNUNET_NO otherwise (service down...)
  */
 static void
-send_packet (struct GNUNET_CADET_Handle *h,
-             const struct GNUNET_MessageHeader *msg,
-             struct GNUNET_CADET_Channel *channel)
+reconnect (struct GNUNET_CADET_Handle *h)
 {
-  struct GNUNET_CADET_TransmitHandle *th;
-  size_t msize;
+  struct GNUNET_CADET_Channel *ch;
 
-  LOG (GNUNET_ERROR_TYPE_DEBUG, " Sending message to service: %s\n",
-       GC_m2s(ntohs(msg->type)));
-  msize = ntohs (msg->size);
-  th = GNUNET_malloc (sizeof (struct GNUNET_CADET_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);
+  LOG (GNUNET_ERROR_TYPE_DEBUG,
+       "Requested RECONNECT, destroying all channels\n");
+  for (ch = h->channels_head; NULL != ch; ch = h->channels_head)
+    destroy_channel (ch, GNUNET_YES);
+  if (NULL == h->reconnect_task)
+    h->reconnect_task = GNUNET_SCHEDULER_add_delayed (h->reconnect_time,
+                                                      &reconnect_cbk, h);
 }
 
 
@@ -1468,10 +1344,8 @@ send_packet (struct GNUNET_CADET_Handle *h,
 
 struct GNUNET_CADET_Handle *
 GNUNET_CADET_connect (const struct GNUNET_CONFIGURATION_Handle *cfg, void *cls,
-                     GNUNET_CADET_InboundChannelNotificationHandler new_channel,
                      GNUNET_CADET_ChannelEndHandler cleaner,
-                     const struct GNUNET_CADET_MessageHandler *handlers,
-                     const uint32_t *ports)
+                     const struct GNUNET_CADET_MessageHandler *handlers)
 {
   struct GNUNET_CADET_Handle *h;
 
@@ -1479,42 +1353,25 @@ GNUNET_CADET_connect (const struct GNUNET_CONFIGURATION_Handle *cfg, void *cls,
   h = GNUNET_new (struct GNUNET_CADET_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 ("cadet", cfg);
-  if (h->client == NULL)
+  h->ports = GNUNET_CONTAINER_multihashmap_create (4, GNUNET_YES);
+  do_reconnect (h);
+  if (h->mq == NULL)
   {
     GNUNET_break (0);
-    GNUNET_free (h);
+    GNUNET_CADET_disconnect (h);
     return NULL;
   }
   h->cls = cls;
   h->message_handlers = handlers;
-  h->ports = ports;
   h->next_chid = GNUNET_CADET_LOCAL_CHANNEL_ID_CLI;
   h->reconnect_time = GNUNET_TIME_UNIT_MILLISECONDS;
-  h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
+  h->reconnect_task = NULL;
 
-  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_CADET_connect() END\n");
   return h;
 }
@@ -1552,9 +1409,10 @@ GNUNET_CADET_disconnect (struct GNUNET_CADET_Handle *handle)
     msg = (struct GNUNET_MessageHeader *) &th[1];
     switch (ntohs(msg->type))
     {
-      case GNUNET_MESSAGE_TYPE_CADET_LOCAL_CONNECT:
       case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_CREATE:
       case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DESTROY:
+      case GNUNET_MESSAGE_TYPE_CADET_LOCAL_PORT_OPEN:
+      case GNUNET_MESSAGE_TYPE_CADET_LOCAL_PORT_CLOSE:
       case GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_CHANNELS:
       case GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_CHANNEL:
       case GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEER:
@@ -1564,33 +1422,93 @@ GNUNET_CADET_disconnect (struct GNUNET_CADET_Handle *handle)
         break;
       default:
         GNUNET_break (0);
-        LOG (GNUNET_ERROR_TYPE_ERROR, "unexpected msg %u\n",
-             ntohs(msg->type));
+        LOG (GNUNET_ERROR_TYPE_ERROR, "unexpected unsent msg %s\n",
+             GC_m2s (ntohs(msg->type)));
     }
 
-    GNUNET_CONTAINER_DLL_remove (handle->th_head, handle->th_tail, th);
-    GNUNET_free (th);
+    GNUNET_CADET_notify_transmit_ready_cancel (th);
   }
 
-  if (NULL != handle->th)
+  if (NULL != handle->mq)
   {
-    GNUNET_CLIENT_notify_transmit_ready_cancel (handle->th);
-    handle->th = NULL;
+    GNUNET_MQ_destroy (handle->mq);
+    handle->mq = NULL;
   }
-  if (NULL != handle->client)
-  {
-    GNUNET_CLIENT_disconnect (handle->client);
-    handle->client = NULL;
-  }
-  if (GNUNET_SCHEDULER_NO_TASK != handle->reconnect_task)
+  if (NULL != handle->reconnect_task)
   {
     GNUNET_SCHEDULER_cancel(handle->reconnect_task);
-    handle->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
+    handle->reconnect_task = NULL;
   }
+
+  GNUNET_CONTAINER_multihashmap_destroy (handle->ports);
+  handle->ports = NULL;
   GNUNET_free (handle);
 }
 
 
+/**
+ * Open a port to receive incomming channels.
+ *
+ * @param h CADET handle.
+ * @param port Hash representing the port number.
+ * @param new_channel Function called when an channel is received.
+ * @param new_channel_cls Closure for @a new_channel.
+ *
+ * @return Port handle.
+ */
+struct GNUNET_CADET_Port *
+GNUNET_CADET_open_port (struct GNUNET_CADET_Handle *h,
+                       const struct GNUNET_HashCode *port,
+                       GNUNET_CADET_InboundChannelNotificationHandler
+                           new_channel,
+                       void *new_channel_cls)
+{
+  struct GNUNET_CADET_PortMessage *msg;
+  struct GNUNET_MQ_Envelope *env;
+  struct GNUNET_CADET_Port *p;
+
+  GNUNET_assert (NULL != new_channel);
+  p = GNUNET_new (struct GNUNET_CADET_Port);
+  p->cadet = h;
+  p->hash = GNUNET_new (struct GNUNET_HashCode);
+  *p->hash = *port;
+  p->handler = new_channel;
+  p->cls = new_channel_cls;
+  GNUNET_assert (GNUNET_OK ==
+                GNUNET_CONTAINER_multihashmap_put (h->ports,
+                                                   p->hash,
+                                                   p,
+                                                   GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
+
+  env = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_CADET_LOCAL_PORT_OPEN);
+  msg->port = *p->hash;
+  GNUNET_MQ_send (h->mq, env);
+
+  return p;
+}
+
+/**
+ * Close a port opened with @a GNUNET_CADET_open_port.
+ * The @a new_channel callback will no longer be called.
+ *
+ * @param p Port handle.
+ */
+void
+GNUNET_CADET_close_port (struct GNUNET_CADET_Port *p)
+{
+  struct GNUNET_CADET_PortMessage *msg;
+  struct GNUNET_MQ_Envelope *env;
+
+  env = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_CADET_LOCAL_PORT_CLOSE);
+
+  msg->port = *p->hash;
+  GNUNET_MQ_send (p->cadet->mq, env);
+  GNUNET_CONTAINER_multihashmap_remove (p->cadet->ports, p->hash, p);
+  GNUNET_free (p->hash);
+  GNUNET_free (p);
+}
+
+
 /**
  * Create a new channel towards a remote peer.
  *
@@ -1601,7 +1519,7 @@ GNUNET_CADET_disconnect (struct GNUNET_CADET_Handle *handle)
  * @param h cadet 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 port Port hash (port number).
  * @param options CadetOption flag field, with all desired option bits set to 1.
  *
  * @return handle to the channel
@@ -1610,11 +1528,12 @@ struct GNUNET_CADET_Channel *
 GNUNET_CADET_channel_create (struct GNUNET_CADET_Handle *h,
                             void *channel_ctx,
                             const struct GNUNET_PeerIdentity *peer,
-                            uint32_t port,
+                            const struct GNUNET_HashCode *port,
                             enum GNUNET_CADET_ChannelOption options)
 {
+  struct GNUNET_CADET_ChannelCreateMessage *msg;
+  struct GNUNET_MQ_Envelope *env;
   struct GNUNET_CADET_Channel *ch;
-  struct GNUNET_CADET_ChannelMessage msg;
 
   LOG (GNUNET_ERROR_TYPE_DEBUG,
        "Creating new channel to %s:%u\n",
@@ -1624,14 +1543,15 @@ GNUNET_CADET_channel_create (struct GNUNET_CADET_Handle *h,
   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_CADET_CHANNEL_CREATE);
-  msg.header.size = htons (sizeof (struct GNUNET_CADET_ChannelMessage));
-  msg.channel_id = htonl (ch->chid);
-  msg.port = htonl (port);
-  msg.peer = *peer;
-  msg.opt = htonl (options);
-  ch->allow_send = 0;
-  send_packet (h, &msg.header, ch);
+
+  env = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_CADET_CHANNEL_CREATE);
+  msg->channel_id = htonl (ch->chid);
+  msg->port = *port;
+  msg->peer = *peer;
+  msg->opt = htonl (options);
+  ch->allow_send = GNUNET_NO;
+  GNUNET_MQ_send (h->mq, env);
+
   return ch;
 }
 
@@ -1640,38 +1560,40 @@ void
 GNUNET_CADET_channel_destroy (struct GNUNET_CADET_Channel *channel)
 {
   struct GNUNET_CADET_Handle *h;
-  struct GNUNET_CADET_ChannelMessage msg;
+  struct GNUNET_CADET_ChannelDestroyMessage *msg;
+  struct GNUNET_MQ_Envelope *env;
   struct GNUNET_CADET_TransmitHandle *th;
+  struct GNUNET_CADET_TransmitHandle *next;
 
   LOG (GNUNET_ERROR_TYPE_DEBUG, "Destroying channel\n");
   h = channel->cadet;
-
-  msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DESTROY);
-  msg.header.size = htons (sizeof (struct GNUNET_CADET_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)
+  for  (th = h->th_head; th != NULL; th = next)
   {
-    struct GNUNET_CADET_TransmitHandle *aux;
+    next = th->next;
     if (th->channel == channel)
     {
-      aux = th->next;
-      /* FIXME call the handler? */
+      GNUNET_break (0);
       if (GNUNET_YES == th_is_payload (th))
+      {
+        /* applications should cancel before destroying channel */
+        LOG (GNUNET_ERROR_TYPE_WARNING,
+             "Channel destroyed without cancelling transmission requests\n");
         th->notify (th->notify_cls, 0, NULL);
+      }
+      else
+      {
+        LOG (GNUNET_ERROR_TYPE_WARNING, "no meta-traffic should be queued\n");
+      }
       GNUNET_CONTAINER_DLL_remove (h->th_head, h->th_tail, th);
-      GNUNET_free (th);
-      th = aux;
+      GNUNET_CADET_notify_transmit_ready_cancel (th);
     }
-    else
-      th = th->next;
   }
 
+  env = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DESTROY);
+  msg->channel_id = htonl (channel->chid);
+  GNUNET_MQ_send (h->mq, env);
+
   destroy_channel (channel, GNUNET_YES);
-  send_packet (h, &msg.header, NULL);
 }
 
 
@@ -1713,16 +1635,19 @@ GNUNET_CADET_channel_get_info (struct GNUNET_CADET_Channel *channel,
   return ret;
 }
 
+
 struct GNUNET_CADET_TransmitHandle *
-GNUNET_CADET_notify_transmit_ready (struct GNUNET_CADET_Channel *channel, int cork,
-                                   struct GNUNET_TIME_Relative maxdelay,
-                                   size_t notify_size,
-                                   GNUNET_CONNECTION_TransmitReadyNotify notify,
-                                   void *notify_cls)
+GNUNET_CADET_notify_transmit_ready (struct GNUNET_CADET_Channel *channel,
+                                    int cork,
+                                    struct GNUNET_TIME_Relative maxdelay,
+                                    size_t notify_size,
+                                    GNUNET_CONNECTION_TransmitReadyNotify notify,
+                                    void *notify_cls)
 {
   struct GNUNET_CADET_TransmitHandle *th;
 
   GNUNET_assert (NULL != channel);
+  GNUNET_assert (GNUNET_CONSTANTS_MAX_CADET_MESSAGE_SIZE >= notify_size);
   LOG (GNUNET_ERROR_TYPE_DEBUG, "CADET 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);
@@ -1733,54 +1658,41 @@ GNUNET_CADET_notify_transmit_ready (struct GNUNET_CADET_Channel *channel, int co
   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
+
+  if (GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us != maxdelay.rel_value_us)
+  {
+    LOG (GNUNET_ERROR_TYPE_WARNING,
+         "CADET transmit ready timeout is deprected (has no effect)\n");
+  }
+
   th = GNUNET_new (struct GNUNET_CADET_TransmitHandle);
   th->channel = channel;
-  th->timeout = GNUNET_TIME_relative_to_absolute (maxdelay);
-  th->size = notify_size + sizeof (struct GNUNET_CADET_LocalData);
+  th->size = notify_size;
   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->cadet, th);
-  if (NULL != channel->cadet->th)
-    return th;
-  if (GNUNET_NO == channel->allow_send)
-    return th;
-  LOG (GNUNET_ERROR_TYPE_DEBUG, "    call client notify tmt rdy\n");
-  channel->cadet->th =
-      GNUNET_CLIENT_notify_transmit_ready (channel->cadet->client, th->size,
-                                           GNUNET_TIME_UNIT_FOREVER_REL,
-                                           GNUNET_YES, &send_callback,
-                                           channel->cadet);
+  if (GNUNET_YES == channel->allow_send)
+    th->request_data_task = GNUNET_SCHEDULER_add_now (&request_data, th);
+  else
+    add_to_queue (channel->cadet, th);
+
   LOG (GNUNET_ERROR_TYPE_DEBUG, "CADET NOTIFY TRANSMIT READY END\n");
   return th;
 }
 
 
-void
-GNUNET_CADET_cancel_notify (struct GNUNET_CADET_TransmitHandle *th)
-{
-       th->notify = NULL;
-}
-
-
 void
 GNUNET_CADET_notify_transmit_ready_cancel (struct GNUNET_CADET_TransmitHandle *th)
 {
-  struct GNUNET_CADET_Handle *cadet;
-
-  th->channel->packet_size = 0;
-  cadet = th->channel->cadet;
-  if (th->timeout_task != GNUNET_SCHEDULER_NO_TASK)
-    GNUNET_SCHEDULER_cancel (th->timeout_task);
-  GNUNET_CONTAINER_DLL_remove (cadet->th_head, cadet->th_tail, th);
-  GNUNET_free (th);
-  if ((0 == message_ready_size (cadet)) && (NULL != cadet->th))
+  if (NULL != th->request_data_task)
   {
-    /* queue empty, no point in asking for transmission */
-    GNUNET_CLIENT_notify_transmit_ready_cancel (cadet->th);
-    cadet->th = NULL;
+    GNUNET_SCHEDULER_cancel (th->request_data_task);
   }
+  th->request_data_task = NULL;
+
+  remove_from_queue (th);
+  GNUNET_free (th);
 }
 
 
@@ -1794,11 +1706,15 @@ GNUNET_CADET_receive_done (struct GNUNET_CADET_Channel *channel)
 static void
 send_info_request (struct GNUNET_CADET_Handle *h, uint16_t type)
 {
-  struct GNUNET_MessageHeader msg;
+  struct GNUNET_MessageHeader *msg;
+  struct GNUNET_MQ_Envelope *env;
 
-  msg.size = htons (sizeof (msg));
-  msg.type = htons (type);
-  send_packet (h, &msg, NULL);
+  LOG (GNUNET_ERROR_TYPE_DEBUG,
+       " Sending %s monitor message to service\n",
+       GC_m2s(type));
+
+  env = GNUNET_MQ_msg (msg, type);
+  GNUNET_MQ_send (h->mq, env);
 }
 
 
@@ -1885,11 +1801,12 @@ GNUNET_CADET_get_peers_cancel (struct GNUNET_CADET_Handle *h)
  */
 int
 GNUNET_CADET_get_peer (struct GNUNET_CADET_Handle *h,
-                      const struct GNUNET_PeerIdentity *id,
-                      GNUNET_CADET_PeerCB callback,
-                      void *callback_cls)
+                       const struct GNUNET_PeerIdentity *id,
+                       GNUNET_CADET_PeerCB callback,
+                       void *callback_cls)
 {
-  struct GNUNET_CADET_LocalInfo msg;
+  struct GNUNET_CADET_LocalInfo *msg;
+  struct GNUNET_MQ_Envelope *env;
 
   if (NULL != h->info_cb.peer_cb)
   {
@@ -1897,11 +1814,10 @@ GNUNET_CADET_get_peer (struct GNUNET_CADET_Handle *h,
     return GNUNET_SYSERR;
   }
 
-  memset (&msg, 0, sizeof (msg));
-  msg.header.size = htons (sizeof (msg));
-  msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEER);
-  msg.peer = *id;
-  send_packet (h, &msg.header, NULL);
+  env = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEER);
+  msg->peer = *id;
+  GNUNET_MQ_send (h->mq, env);
+
   h->info_cb.peer_cb = callback;
   h->info_cls = callback_cls;
   return GNUNET_OK;
@@ -1979,7 +1895,8 @@ GNUNET_CADET_get_tunnel (struct GNUNET_CADET_Handle *h,
                         GNUNET_CADET_TunnelCB callback,
                         void *callback_cls)
 {
-  struct GNUNET_CADET_LocalInfo msg;
+  struct GNUNET_CADET_LocalInfo *msg;
+  struct GNUNET_MQ_Envelope *env;
 
   if (NULL != h->info_cb.tunnel_cb)
   {
@@ -1987,11 +1904,10 @@ GNUNET_CADET_get_tunnel (struct GNUNET_CADET_Handle *h,
     return GNUNET_SYSERR;
   }
 
-  memset (&msg, 0, sizeof (msg));
-  msg.header.size = htons (sizeof (msg));
-  msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNEL);
-  msg.peer = *id;
-  send_packet (h, &msg.header, NULL);
+  env = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNEL);
+  msg->peer = *id;
+  GNUNET_MQ_send (h->mq, env);
+
   h->info_cb.tunnel_cb = callback;
   h->info_cls = callback_cls;
   return GNUNET_OK;
@@ -2014,12 +1930,13 @@ GNUNET_CADET_get_tunnel (struct GNUNET_CADET_Handle *h,
  */
 int
 GNUNET_CADET_show_channel (struct GNUNET_CADET_Handle *h,
-                         struct GNUNET_PeerIdentity *initiator,
-                         unsigned int channel_number,
-                         GNUNET_CADET_ChannelCB callback,
-                         void *callback_cls)
+                           struct GNUNET_PeerIdentity *initiator,
+                           unsigned int channel_number,
+                           GNUNET_CADET_ChannelCB callback,
+                           void *callback_cls)
 {
-  struct GNUNET_CADET_LocalInfo msg;
+  struct GNUNET_CADET_LocalInfo *msg;
+  struct GNUNET_MQ_Envelope *env;
 
   if (NULL != h->info_cb.channel_cb)
   {
@@ -2027,12 +1944,11 @@ GNUNET_CADET_show_channel (struct GNUNET_CADET_Handle *h,
     return GNUNET_SYSERR;
   }
 
-  msg.header.size = htons (sizeof (msg));
-  msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_CHANNEL);
-  msg.peer = *initiator;
-  msg.channel_id = htonl (channel_number);
-//   msg.reserved = 0;
-  send_packet (h, &msg.header, NULL);
+  env = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_CHANNEL);
+  msg->peer = *initiator;
+  msg->channel_id = htonl (channel_number);
+  GNUNET_MQ_send (h->mq, env);
+
   h->info_cb.channel_cb = callback;
   h->info_cls = callback_cls;
   return GNUNET_OK;
@@ -2067,7 +1983,7 @@ cadet_mq_ntr (void *cls, size_t size,
   }
   msize = ntohs (msg->size);
   GNUNET_assert (msize <= size);
-  memcpy (buf, msg, msize);
+  GNUNET_memcpy (buf, msg, msize);
   GNUNET_MQ_impl_send_continue (mq);
   return msize;
 }
@@ -2083,7 +1999,8 @@ cadet_mq_ntr (void *cls, size_t size,
  */
 static void
 cadet_mq_send_impl (struct GNUNET_MQ_Handle *mq,
-                   const struct GNUNET_MessageHeader *msg, void *impl_state)
+                    const struct GNUNET_MessageHeader *msg,
+                    void *impl_state)
 {
   struct CadetMQState *state = impl_state;
 
@@ -2094,7 +2011,7 @@ cadet_mq_send_impl (struct GNUNET_MQ_Handle *mq,
                                          GNUNET_NO,
                                          GNUNET_TIME_UNIT_FOREVER_REL,
                                          ntohs (msg->size),
-                                         cadet_mq_ntr, mq);
+                                         &cadet_mq_ntr, mq);
 
 }
 
@@ -2109,7 +2026,8 @@ cadet_mq_send_impl (struct GNUNET_MQ_Handle *mq,
  * @param impl_state state of the implementation
  */
 static void
-cadet_mq_destroy_impl (struct GNUNET_MQ_Handle *mq, void *impl_state)
+cadet_mq_destroy_impl (struct GNUNET_MQ_Handle *mq,
+                       void *impl_state)
 {
   struct CadetMQState *state = impl_state;
 
@@ -2137,8 +2055,8 @@ GNUNET_CADET_mq_create (struct GNUNET_CADET_Channel *channel)
   state = GNUNET_new (struct CadetMQState);
   state->channel = channel;
 
-  mq = GNUNET_MQ_queue_for_callbacks (cadet_mq_send_impl,
-                                      cadet_mq_destroy_impl,
+  mq = GNUNET_MQ_queue_for_callbacks (&cadet_mq_send_impl,
+                                      &cadet_mq_destroy_impl,
                                       NULL, /* FIXME: cancel impl. */
                                       state,
                                       NULL, /* no msg handlers */
@@ -2147,3 +2065,25 @@ GNUNET_CADET_mq_create (struct GNUNET_CADET_Channel *channel)
   return mq;
 }
 
+
+/**
+ * Transitional function to convert an unsigned int port to a hash value.
+ * WARNING: local static value returned, NOT reentrant!
+ * WARNING: do not use this function for new code!
+ *
+ * @param port Numerical port (unsigned int format).
+ *
+ * @return A GNUNET_HashCode usable for the new CADET API.
+ */
+const struct GNUNET_HashCode *
+GC_u2h (uint32_t port)
+{
+  static struct GNUNET_HashCode hash;
+
+  GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
+              "This is a transitional function, "
+              "use proper crypto hashes as CADET ports\n");
+  GNUNET_CRYPTO_hash (&port, sizeof (port), &hash);
+
+  return &hash;
+}