cancel existing last_control_qe handle before scheduling a new one, always
[oweals/gnunet.git] / src / cadet / gnunet-service-cadet_channel.c
index 5c2204f9968bba7ea1eb29652e3af137d88911cc..7b7c6e57ce4351c3028f9e0a0deecbe5b5410914 100644 (file)
@@ -1,6 +1,6 @@
 /*
      This file is part of GNUnet.
-     (C) 2013 Christian Grothoff (and other contributing authors)
+     Copyright (C) 2013 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
@@ -14,8 +14,8 @@
 
      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.
 */
 
 
@@ -33,6 +33,7 @@
 #include "gnunet-service-cadet_peer.h"
 
 #define LOG(level, ...) GNUNET_log_from(level,"cadet-chn",__VA_ARGS__)
+#define LOG2(level, ...) GNUNET_log_from_nocheck(level,"cadet-chn",__VA_ARGS__)
 
 #define CADET_RETRANSMIT_TIME    GNUNET_TIME_relative_multiply(\
                                     GNUNET_TIME_UNIT_MILLISECONDS, 250)
@@ -57,7 +58,7 @@ enum CadetChannelState
   /**
    * Connection confirmed, ready to carry traffic.
    */
-  CADET_CHANNEL_READY,
+  CADET_CHANNEL_READY
 };
 
 
@@ -93,38 +94,38 @@ struct CadetChannelQueue
  */
 struct CadetReliableMessage
 {
-    /**
-     * Double linked list, FIFO style
-     */
-  struct CadetReliableMessage    *next;
-  struct CadetReliableMessage    *prev;
+  /**
+   * Double linked list, FIFO style
+   */
+  struct CadetReliableMessage   *next;
+  struct CadetReliableMessage   *prev;
 
-    /**
-     * Type of message (payload, channel management).
-     */
-  int16_t type;
+  /**
+   * Type of message (payload, channel management).
+   */
+  int16_t                       type;
 
-    /**
-     * Tunnel Reliability queue this message is in.
-     */
-  struct CadetChannelReliability  *rel;
+  /**
+   * Tunnel Reliability queue this message is in.
+   */
+  struct CadetChannelReliability *rel;
 
-    /**
-     * ID of the message (ACK needed to free)
-     */
+  /**
+   * ID of the message (ACK needed to free)
+   */
   uint32_t                      mid;
 
   /**
    * Tunnel Queue.
    */
-  struct CadetChannelQueue       *chq;
+  struct CadetChannelQueue      *chq;
 
-    /**
-     * When was this message issued (to calculate ACK delay)
-     */
+  /**
+   * When was this message issued (to calculate ACK delay)
+   */
   struct GNUNET_TIME_Absolute   timestamp;
 
-  /* struct GNUNET_CADET_Data with payload */
+  /* struct GNUNET_CADET_ChannelAppDataMessage with payload */
 };
 
 
@@ -133,46 +134,46 @@ struct CadetReliableMessage
  */
 struct CadetChannelReliability
 {
-    /**
-     * Channel this is about.
-     */
+  /**
+   * Channel this is about.
+   */
   struct CadetChannel *ch;
 
-    /**
-     * DLL of messages sent and not yet ACK'd.
-     */
+  /**
+   * DLL of messages sent and not yet ACK'd.
+   */
   struct CadetReliableMessage        *head_sent;
   struct CadetReliableMessage        *tail_sent;
 
-    /**
-     * DLL of messages received out of order.
-     */
+  /**
+   * DLL of messages received out of order.
+   */
   struct CadetReliableMessage        *head_recv;
   struct CadetReliableMessage        *tail_recv;
 
-    /**
-     * Messages received.
-     */
+  /**
+   * Messages received.
+   */
   unsigned int                      n_recv;
 
-    /**
-     * Next MID to use for outgoing traffic.
-     */
+  /**
+   * Next MID to use for outgoing traffic.
+   */
   uint32_t                          mid_send;
 
-    /**
-     * Next MID expected for incoming traffic.
-     */
+  /**
+   * Next MID expected for incoming traffic.
+   */
   uint32_t                          mid_recv;
 
-    /**
-     * Handle for queued unique data CREATE, DATA_ACK.
-     */
+  /**
+   * Handle for queued unique data CREATE, DATA_ACK.
+   */
   struct CadetChannelQueue           *uniq;
 
-    /**
-     * Can we send data to the client?
-     */
+  /**
+   * Can we send data to the client?
+   */
   int                               client_ready;
 
   /**
@@ -180,19 +181,19 @@ struct CadetChannelReliability
    */
   int                               client_allowed;
 
-    /**
-     * Task to resend/poll in case no ACK is received.
-     */
-  GNUNET_SCHEDULER_TaskIdentifier   retry_task;
+  /**
+   * Task to resend/poll in case no ACK is received.
+   */
+  struct GNUNET_SCHEDULER_Task *   retry_task;
 
-    /**
-     * Counter for exponential backoff.
-     */
+  /**
+   * Counter for exponential backoff.
+   */
   struct GNUNET_TIME_Relative       retry_timer;
 
-    /**
-     * How long does it usually take to get an ACK.
-     */
+  /**
+   * How long does it usually take to get an ACK.
+   */
   struct GNUNET_TIME_Relative       expected_delay;
 };
 
@@ -202,85 +203,85 @@ struct CadetChannelReliability
  */
 struct CadetChannel
 {
-    /**
-     * Tunnel this channel is in.
-     */
+  /**
+   * Tunnel this channel is in.
+   */
   struct CadetTunnel *t;
 
-    /**
-     * Destination port of the channel.
-     */
-  uint32_t port;
+  /**
+   * Destination port of the channel.
+   */
+  struct GNUNET_HashCode port;
 
-    /**
-     * Global channel number ( < GNUNET_CADET_LOCAL_CHANNEL_ID_CLI)
-     */
-  CADET_ChannelNumber gid;
+  /**
+   * Global channel number ( < GNUNET_CADET_LOCAL_CHANNEL_ID_CLI)
+   */
+  struct GNUNET_CADET_ChannelTunnelNumber gid;
 
-    /**
-     * Local tunnel number for root (owner) client.
-     * ( >= GNUNET_CADET_LOCAL_CHANNEL_ID_CLI or 0 )
-     */
-  CADET_ChannelNumber lid_root;
+  /**
+   * Local tunnel number for root (owner) client.
+   * ( >= GNUNET_CADET_LOCAL_CHANNEL_ID_CLI or 0 )
+   */
+  struct GNUNET_CADET_ClientChannelNumber lid_root;
 
-    /**
-     * Local tunnel number for local destination clients (incoming number)
-     * ( >= GNUNET_CADET_LOCAL_CHANNEL_ID_SERV or 0).
-     */
-  CADET_ChannelNumber lid_dest;
+  /**
+   * Local tunnel number for local destination clients (incoming number)
+   * ( >= GNUNET_CADET_LOCAL_CHANNEL_ID_SERV or 0).
+   */
+  struct GNUNET_CADET_ClientChannelNumber lid_dest;
 
-    /**
-     * Channel state.
-     */
+  /**
+   * Channel state.
+   */
   enum CadetChannelState state;
 
-    /**
-     * Is the tunnel bufferless (minimum latency)?
-     */
+  /**
+   * Is the tunnel bufferless (minimum latency)?
+   */
   int nobuffer;
 
-    /**
-     * Is the tunnel reliable?
-     */
+  /**
+   * Is the tunnel reliable?
+   */
   int reliable;
 
-    /**
-     * Last time the channel was used
-     */
+  /**
+   * Last time the channel was used
+   */
   struct GNUNET_TIME_Absolute timestamp;
 
-    /**
-     * Client owner of the tunnel, if any
-     */
+  /**
+   * Client owner of the tunnel, if any
+   */
   struct CadetClient *root;
 
-    /**
-     * Client destination of the tunnel, if any.
-     */
+  /**
+   * Client destination of the tunnel, if any.
+   */
   struct CadetClient *dest;
 
-    /**
-     * Flag to signal the destruction of the channel.
-     * If this is set GNUNET_YES the channel will be destroyed
-     * when the queue is empty.
-     */
+  /**
+   * Flag to signal the destruction of the channel.
+   * If this is set to #GNUNET_YES the channel will be destroyed
+   * when the queue is empty.
+   */
   int destroy;
 
-    /**
-     * Total (reliable) messages pending ACK for this channel.
-     */
+  /**
+   * Total (reliable) messages pending ACK for this channel.
+   */
   unsigned int pending_messages;
 
-    /**
-     * Reliability data.
-     * Only present (non-NULL) at the owner of a tunnel.
-     */
+  /**
+   * Reliability data.
+   * Only present (non-NULL) at the owner of a tunnel.
+   */
   struct CadetChannelReliability *root_rel;
 
-    /**
-     * Reliability data.
-     * Only present (non-NULL) at the destination of a tunnel.
-     */
+  /**
+   * Reliability data.
+   * Only present (non-NULL) at the destination of a tunnel.
+   */
   struct CadetChannelReliability *dest_rel;
 
 };
@@ -305,6 +306,7 @@ extern GNUNET_PEER_Id myid;
 /********************************   STATIC  ***********************************/
 /******************************************************************************/
 
+
 /**
  * Destroy a reliable message after it has been acknowledged, either by
  * direct mid ACK or bitfield. Updates the appropriate data structures and
@@ -315,6 +317,9 @@ extern GNUNET_PEER_Id myid;
  *                    If this message is ACK in a batch the timing information
  *                    is skewed by the retransmission, count only for the
  *                    retransmitted message.
+ *
+ * @return #GNUNET_YES if channel was destroyed as a result of the call,
+ *         #GNUNET_NO otherwise.
  */
 static int
 rel_message_free (struct CadetReliableMessage *copy, int update_time);
@@ -332,10 +337,9 @@ send_create (struct CadetChannel *ch);
  *
  * @param ch The channel to confirm.
  * @param fwd Should we send a FWD ACK? (going dest->root)
- * @param reaction This ACK is a reaction to a duplicate CREATE, don't save.
  */
 static void
-send_ack (struct CadetChannel *ch, int fwd, int reaction);
+send_ack (struct CadetChannel *ch, int fwd);
 
 
 
@@ -364,7 +368,7 @@ is_loopback (const struct CadetChannel *ch)
  * @param rel Reliability data for retransmission.
  */
 static struct CadetReliableMessage *
-copy_message (const struct GNUNET_CADET_Data *msg, uint32_t mid,
+copy_message (const struct GNUNET_CADET_ChannelAppDataMessage *msg, uint32_t mid,
               struct CadetChannelReliability *rel)
 {
   struct CadetReliableMessage *copy;
@@ -374,8 +378,8 @@ copy_message (const struct GNUNET_CADET_Data *msg, uint32_t mid,
   copy = GNUNET_malloc (sizeof (*copy) + size);
   copy->mid = mid;
   copy->rel = rel;
-  copy->type = GNUNET_MESSAGE_TYPE_CADET_DATA;
-  memcpy (&copy[1], msg, size);
+  copy->type = GNUNET_MESSAGE_TYPE_CADET_CHANNEL_APP_DATA;
+  GNUNET_memcpy (&copy[1], msg, size);
 
   return copy;
 }
@@ -389,7 +393,7 @@ copy_message (const struct GNUNET_CADET_Data *msg, uint32_t mid,
  * @param rel Reliability data to the corresponding direction.
  */
 static void
-add_buffered_data (const struct GNUNET_CADET_Data *msg,
+add_buffered_data (const struct GNUNET_CADET_ChannelAppDataMessage *msg,
                    struct CadetChannelReliability *rel)
 {
   struct CadetReliableMessage *copy;
@@ -398,7 +402,8 @@ add_buffered_data (const struct GNUNET_CADET_Data *msg,
 
   mid = ntohl (msg->mid);
 
-  LOG (GNUNET_ERROR_TYPE_DEBUG, "add_buffered_data %u\n", mid);
+  LOG (GNUNET_ERROR_TYPE_DEBUG, "add_buffered_data MID %u (%u)\n",
+       mid, rel->n_recv);
 
   rel->n_recv++;
 
@@ -410,6 +415,7 @@ add_buffered_data (const struct GNUNET_CADET_Data *msg,
     if (prev->mid == mid)
     {
       LOG (GNUNET_ERROR_TYPE_DEBUG, " already there!\n");
+      rel->n_recv--;
       return;
     }
     else if (GC_is_pid_bigger (prev->mid, mid))
@@ -422,7 +428,7 @@ add_buffered_data (const struct GNUNET_CADET_Data *msg,
     }
   }
   copy = copy_message (msg, mid, rel);
-  LOG (GNUNET_ERROR_TYPE_DEBUG, " insert at tail!\n");
+  LOG (GNUNET_ERROR_TYPE_DEBUG, " insert at tail! (now: %u)\n", rel->n_recv);
   GNUNET_CONTAINER_DLL_insert_tail (rel->head_recv, rel->tail_recv, copy);
   LOG (GNUNET_ERROR_TYPE_DEBUG, "add_buffered_data END\n");
 }
@@ -445,7 +451,7 @@ add_destination (struct CadetChannel *ch, struct CadetClient *c)
   }
 
   /* Assign local id as destination */
-  ch->lid_dest = GML_get_next_chid (c);
+  ch->lid_dest = GML_get_next_ccn (c);
 
   /* Store in client's hashmap */
   GML_channel_add (c, ch->lid_dest, ch);
@@ -507,11 +513,11 @@ channel_get_options (struct CadetChannel *ch)
 static void
 send_destroy (struct CadetChannel *ch, int local_only)
 {
-  struct GNUNET_CADET_ChannelManage msg;
+  struct GNUNET_CADET_ChannelManageMessage msg;
 
   msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DESTROY);
   msg.header.size = htons (sizeof (msg));
-  msg.chid = htonl (ch->gid);
+  msg.ctn = ch->gid;
 
   /* If root is not NULL, notify.
    * If it's NULL, check lid_root. When a local destroy comes in, root
@@ -520,12 +526,12 @@ send_destroy (struct CadetChannel *ch, int local_only)
    */
   if (NULL != ch->root)
     GML_send_channel_destroy (ch->root, ch->lid_root);
-  else if (0 == ch->lid_root && GNUNET_NO == local_only)
+  else if (0 == ch->lid_root.channel_of_client && GNUNET_NO == local_only)
     GCCH_send_prebuilt_message (&msg.header, ch, GNUNET_NO, NULL);
 
   if (NULL != ch->dest)
     GML_send_channel_destroy (ch->dest, ch->lid_dest);
-  else if (0 == ch->lid_dest && GNUNET_NO == local_only)
+  else if (0 == ch->lid_dest.channel_of_client && GNUNET_NO == local_only)
     GCCH_send_prebuilt_message (&msg.header, ch, GNUNET_YES, NULL);
 }
 
@@ -546,7 +552,10 @@ send_client_create (struct CadetChannel *ch)
   opt = 0;
   opt |= GNUNET_YES == ch->reliable ? GNUNET_CADET_OPTION_RELIABLE : 0;
   opt |= GNUNET_YES == ch->nobuffer ? GNUNET_CADET_OPTION_NOBUFFER : 0;
-  GML_send_channel_create (ch->dest, ch->lid_dest, ch->port, opt,
+  GML_send_channel_create (ch->dest,
+                           ch->lid_dest,
+                           &ch->port,
+                           opt,
                            GCT_get_destination (ch->t));
 
 }
@@ -564,20 +573,28 @@ send_client_create (struct CadetChannel *ch)
  */
 static void
 send_client_data (struct CadetChannel *ch,
-                  const struct GNUNET_CADET_Data *msg,
+                  const struct GNUNET_CADET_ChannelAppDataMessage *msg,
                   int fwd)
 {
   if (fwd)
   {
     if (ch->dest_rel->client_ready)
+    {
       GML_send_data (ch->dest, msg, ch->lid_dest);
+      ch->dest_rel->client_ready = GNUNET_NO;
+      ch->dest_rel->mid_recv++;
+    }
     else
       add_buffered_data (msg, ch->dest_rel);
   }
   else
   {
     if (ch->root_rel->client_ready)
+    {
       GML_send_data (ch->root, msg, ch->lid_root);
+      ch->root_rel->client_ready = GNUNET_NO;
+      ch->root_rel->mid_recv++;
+    }
     else
       add_buffered_data (msg, ch->root_rel);
   }
@@ -614,24 +631,22 @@ send_client_buffered_data (struct CadetChannel *ch,
   {
     if (copy->mid == rel->mid_recv || GNUNET_NO == ch->reliable)
     {
-      struct GNUNET_CADET_Data *msg = (struct GNUNET_CADET_Data *) &copy[1];
+      struct GNUNET_CADET_ChannelAppDataMessage *msg = (struct GNUNET_CADET_ChannelAppDataMessage *) &copy[1];
 
-      LOG (GNUNET_ERROR_TYPE_DEBUG,
-           " have %u! now expecting %u\n",
+      LOG (GNUNET_ERROR_TYPE_DEBUG, " have %u! now expecting %u\n",
            copy->mid, rel->mid_recv + 1);
       send_client_data (ch, msg, fwd);
       rel->n_recv--;
-      rel->mid_recv++;
       GNUNET_CONTAINER_DLL_remove (rel->head_recv, rel->tail_recv, copy);
-      LOG (GNUNET_ERROR_TYPE_DEBUG, " COPYFREE RECV %p\n", copy);
+      LOG (GNUNET_ERROR_TYPE_DEBUG, " free copy recv MID %u (%p), %u left\n",
+           copy->mid, copy, rel->n_recv);
       GNUNET_free (copy);
+      GCCH_send_data_ack (ch, fwd);
     }
     else
     {
-      LOG (GNUNET_ERROR_TYPE_DEBUG,
-           " reliable && don't have %u, next is %u\n",
-           rel->mid_recv,
-           copy->mid);
+      LOG (GNUNET_ERROR_TYPE_DEBUG, " reliable && don't have %u, next is %u\n",
+           rel->mid_recv, copy->mid);
       if (GNUNET_YES == ch->destroy)
       {
         /* We don't have the next data piece and the remote peer has closed the
@@ -709,35 +724,30 @@ send_client_nack (struct CadetChannel *ch)
  * We haven't received an ACK after a certain time: restransmit the message.
  *
  * @param cls Closure (CadetChannelReliability with the message to restransmit)
- * @param tc TaskContext.
  */
 static void
-channel_retransmit_message (void *cls,
-                            const struct GNUNET_SCHEDULER_TaskContext *tc)
+channel_retransmit_message (void *cls)
 {
   struct CadetChannelReliability *rel = cls;
   struct CadetReliableMessage *copy;
   struct CadetChannel *ch;
-  struct GNUNET_CADET_Data *payload;
+  struct GNUNET_CADET_ChannelAppDataMessage *payload;
   int fwd;
 
-  rel->retry_task = GNUNET_SCHEDULER_NO_TASK;
-  if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
-    return;
-
+  rel->retry_task = NULL;
   ch = rel->ch;
   copy = rel->head_sent;
   if (NULL == copy)
   {
-    GNUNET_break (0);
+    GNUNET_break (0); // FIXME tripped in rps testcase
     return;
   }
 
-  payload = (struct GNUNET_CADET_Data *) &copy[1];
+  payload = (struct GNUNET_CADET_ChannelAppDataMessage *) &copy[1];
   fwd = (rel == ch->root_rel);
 
   /* Message not found in the queue that we are going to use. */
-  LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! RETRANSMIT %u\n", copy->mid);
+  LOG (GNUNET_ERROR_TYPE_DEBUG, "RETRANSMIT MID %u\n", copy->mid);
 
   GCCH_send_prebuilt_message (&payload->header, ch, fwd, copy);
   GNUNET_STATISTICS_update (stats, "# data retransmitted", 1, GNUNET_NO);
@@ -748,19 +758,16 @@ channel_retransmit_message (void *cls,
  * We haven't received an Channel ACK after a certain time: resend the CREATE.
  *
  * @param cls Closure (CadetChannelReliability of the channel to recreate)
- * @param tc TaskContext.
  */
 static void
-channel_recreate (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
+channel_recreate (void *cls)
 {
   struct CadetChannelReliability *rel = cls;
 
-  rel->retry_task = GNUNET_SCHEDULER_NO_TASK;
-  if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
-    return;
-
-  LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! RE-CREATE\n");
-  GNUNET_STATISTICS_update (stats, "# data retransmitted", 1, GNUNET_NO);
+  rel->retry_task = NULL;
+  LOG (GNUNET_ERROR_TYPE_DEBUG, "RE-CREATE\n");
+  GNUNET_STATISTICS_update (stats,
+                           "# data retransmitted", 1, GNUNET_NO);
 
   if (rel == rel->ch->root_rel)
   {
@@ -768,13 +775,12 @@ channel_recreate (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
   }
   else if (rel == rel->ch->dest_rel)
   {
-    send_ack (rel->ch, GNUNET_YES, GNUNET_NO);
+    send_ack (rel->ch, GNUNET_YES);
   }
   else
   {
     GNUNET_break (0);
   }
-
 }
 
 
@@ -797,34 +803,33 @@ ch_message_sent (void *cls,
   struct CadetReliableMessage *copy = chq->copy;
   struct CadetChannelReliability *rel;
 
-  LOG (GNUNET_ERROR_TYPE_DEBUG, "channel message sent callback %s\n",
+  LOG (GNUNET_ERROR_TYPE_DEBUG, "channel_message_sent callback %s\n",
        GC_m2s (chq->type));
 
   switch (chq->type)
   {
-    case GNUNET_MESSAGE_TYPE_CADET_DATA:
-      LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! SENT DATA MID %u\n", copy->mid);
+    case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_APP_DATA:
+      LOG (GNUNET_ERROR_TYPE_DEBUG, "data MID %u sent\n", copy->mid);
       GNUNET_assert (chq == copy->chq);
       copy->timestamp = GNUNET_TIME_absolute_get ();
       rel = copy->rel;
-      if (GNUNET_SCHEDULER_NO_TASK == rel->retry_task)
+      if (NULL == rel->retry_task)
       {
-        LOG (GNUNET_ERROR_TYPE_DEBUG, "!! scheduling retry in 4 * %s\n",
+        LOG (GNUNET_ERROR_TYPE_DEBUG, "  scheduling retry in %d * %s\n",
+             CADET_RETRANSMIT_MARGIN,
              GNUNET_STRINGS_relative_time_to_string (rel->expected_delay,
                                                      GNUNET_YES));
         if (0 != rel->expected_delay.rel_value_us)
         {
-          LOG (GNUNET_ERROR_TYPE_DEBUG, "!! delay != 0\n");
           rel->retry_timer =
-          GNUNET_TIME_relative_multiply (rel->expected_delay,
-                                         CADET_RETRANSMIT_MARGIN);
+              GNUNET_TIME_relative_saturating_multiply (rel->expected_delay,
+                                                        CADET_RETRANSMIT_MARGIN);
         }
         else
         {
-          LOG (GNUNET_ERROR_TYPE_DEBUG, "!! delay reset\n");
           rel->retry_timer = CADET_RETRANSMIT_TIME;
         }
-        LOG (GNUNET_ERROR_TYPE_DEBUG, "!! using delay %s\n",
+        LOG (GNUNET_ERROR_TYPE_DEBUG, "  using delay %s\n",
              GNUNET_STRINGS_relative_time_to_string (rel->retry_timer,
                                                      GNUNET_NO));
         rel->retry_task =
@@ -833,26 +838,26 @@ ch_message_sent (void *cls,
       }
       else
       {
-        LOG (GNUNET_ERROR_TYPE_DEBUG, "!! retry task %u\n", rel->retry_task);
+        LOG (GNUNET_ERROR_TYPE_DEBUG, "retry running %p\n", rel->retry_task);
       }
       copy->chq = NULL;
       break;
 
 
-    case GNUNET_MESSAGE_TYPE_CADET_DATA_ACK:
-    case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_CREATE:
-    case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_ACK:
-      LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! SENT %s\n", GC_m2s (chq->type));
+    case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_APP_DATA_ACK:
+    case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_OPEN:
+    case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_OPEN_ACK:
+      LOG (GNUNET_ERROR_TYPE_DEBUG, "sent %s\n", GC_m2s (chq->type));
       rel = chq->rel;
       GNUNET_assert (rel->uniq == chq);
       rel->uniq = NULL;
 
       if (CADET_CHANNEL_READY != rel->ch->state
-          && GNUNET_MESSAGE_TYPE_CADET_DATA_ACK != type
+          && GNUNET_MESSAGE_TYPE_CADET_CHANNEL_APP_DATA_ACK != type
           && GNUNET_NO == rel->ch->destroy)
       {
-        GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == rel->retry_task);
-        LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! STD BACKOFF %s\n",
+        GNUNET_assert (NULL == rel->retry_task);
+        LOG (GNUNET_ERROR_TYPE_DEBUG, "STD BACKOFF %s\n",
              GNUNET_STRINGS_relative_time_to_string (rel->retry_timer,
                                                      GNUNET_NO));
         rel->retry_timer = GNUNET_TIME_STD_BACKOFF (rel->retry_timer);
@@ -877,12 +882,12 @@ ch_message_sent (void *cls,
 static void
 send_create (struct CadetChannel *ch)
 {
-  struct GNUNET_CADET_ChannelCreate msgcc;
+  struct GNUNET_CADET_ChannelOpenMessage msgcc;
 
   msgcc.header.size = htons (sizeof (msgcc));
-  msgcc.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CHANNEL_CREATE);
-  msgcc.chid = htonl (ch->gid);
-  msgcc.port = htonl (ch->port);
+  msgcc.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CHANNEL_OPEN);
+  msgcc.ctn = ch->gid;
+  msgcc.port = ch->port;
   msgcc.opt = htonl (channel_get_options (ch));
 
   GCCH_send_prebuilt_message (&msgcc.header, ch, GNUNET_YES, NULL);
@@ -894,20 +899,20 @@ send_create (struct CadetChannel *ch)
  *
  * @param ch The channel to confirm.
  * @param fwd Should we send a FWD ACK? (going dest->root)
- * @param reaction This ACK is a reaction to a duplicate CREATE, don't save.
  */
 static void
-send_ack (struct CadetChannel *ch, int fwd, int reaction)
+send_ack (struct CadetChannel *ch, int fwd)
 {
-  struct GNUNET_CADET_ChannelManage msg;
+  struct GNUNET_CADET_ChannelManageMessage msg;
 
   msg.header.size = htons (sizeof (msg));
-  msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CHANNEL_ACK);
-  LOG (GNUNET_ERROR_TYPE_DEBUG, "  sending channel %s ack for channel %s\n",
+  msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CHANNEL_OPEN_ACK);
+  LOG (GNUNET_ERROR_TYPE_DEBUG,
+       "  sending channel %s ack for channel %s\n",
        GC_f2s (fwd), GCCH_2s (ch));
 
-  msg.chid = htonl (ch->gid);
-  GCCH_send_prebuilt_message (&msg.header, ch, !fwd, reaction ? &msg : NULL);
+  msg.ctn =ch->gid;
+  GCCH_send_prebuilt_message (&msg.header, ch, !fwd, NULL);
 }
 
 
@@ -924,8 +929,9 @@ fire_and_forget (const struct GNUNET_MessageHeader *msg,
                  struct CadetChannel *ch,
                  int force)
 {
-  GNUNET_break (NULL == GCT_send_prebuilt_message (msg, ch->t, NULL,
-                                                   force, NULL, NULL));
+  GNUNET_break (NULL ==
+                GCT_send_prebuilt_message (msg, ch->t, NULL,
+                                           force, NULL, NULL));
 }
 
 
@@ -937,15 +943,15 @@ fire_and_forget (const struct GNUNET_MessageHeader *msg,
 static void
 send_nack (struct CadetChannel *ch)
 {
-  struct GNUNET_CADET_ChannelManage msg;
+  struct GNUNET_CADET_ChannelManageMessage msg;
 
   msg.header.size = htons (sizeof (msg));
-  msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CHANNEL_NACK);
+  msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CHANNEL_OPEN_NACK_DEPRECATED);
   LOG (GNUNET_ERROR_TYPE_DEBUG,
        "  sending channel NACK for channel %s\n",
        GCCH_2s (ch));
 
-  msg.chid = htonl (ch->gid);
+  msg.ctn = ch->gid;
   GCCH_send_prebuilt_message (&msg.header, ch, GNUNET_NO, NULL);
 }
 
@@ -970,7 +976,7 @@ channel_rel_free_all (struct CadetChannelReliability *rel)
   {
     next = copy->next;
     GNUNET_CONTAINER_DLL_remove (rel->head_recv, rel->tail_recv, copy);
-    LOG (GNUNET_ERROR_TYPE_DEBUG, " COPYFREE BATCH RECV %p\n", copy);
+    LOG (GNUNET_ERROR_TYPE_DEBUG, " COPYFREE ALL RECV %p\n", copy);
     GNUNET_break (NULL == copy->chq);
     GNUNET_free (copy);
   }
@@ -978,7 +984,7 @@ channel_rel_free_all (struct CadetChannelReliability *rel)
   {
     next = copy->next;
     GNUNET_CONTAINER_DLL_remove (rel->head_sent, rel->tail_sent, copy);
-    LOG (GNUNET_ERROR_TYPE_DEBUG, " COPYFREE BATCH %p\n", copy);
+    LOG (GNUNET_ERROR_TYPE_DEBUG, " COPYFREE ALL SEND %p\n", copy);
     if (NULL != copy->chq)
     {
       if (NULL != copy->chq->tq)
@@ -999,10 +1005,10 @@ channel_rel_free_all (struct CadetChannelReliability *rel)
     GCT_cancel (rel->uniq->tq);
     /* ch_message_sent is called freeing uniq */
   }
-  if (GNUNET_SCHEDULER_NO_TASK != rel->retry_task)
+  if (NULL != rel->retry_task)
   {
     GNUNET_SCHEDULER_cancel (rel->retry_task);
-    rel->retry_task = GNUNET_SCHEDULER_NO_TASK;
+    rel->retry_task = NULL;
   }
   GNUNET_free (rel);
 }
@@ -1013,10 +1019,12 @@ channel_rel_free_all (struct CadetChannelReliability *rel)
  *
  * @param rel Reliability data.
  * @param msg DataACK message with a bitfield of future ACK'd messages.
+ *
+ * @return How many messages have been freed.
  */
-static void
+static unsigned int
 channel_rel_free_sent (struct CadetChannelReliability *rel,
-                       const struct GNUNET_CADET_DataACK *msg)
+                       const struct GNUNET_CADET_ChannelDataAckMessage *msg)
 {
   struct CadetReliableMessage *copy;
   struct CadetReliableMessage *next;
@@ -1025,22 +1033,17 @@ channel_rel_free_sent (struct CadetChannelReliability *rel,
   uint32_t mid;
   uint32_t target;
   unsigned int i;
+  unsigned int r;
 
   bitfield = msg->futures;
   mid = ntohl (msg->mid);
-  LOG (GNUNET_ERROR_TYPE_DEBUG,
-              "!!! free_sent_reliable %u %llX\n",
-              mid, bitfield);
-  LOG (GNUNET_ERROR_TYPE_DEBUG,
-              " rel %p, head %p\n",
-              rel, rel->head_sent);
-  for (i = 0, copy = rel->head_sent;
+  LOG (GNUNET_ERROR_TYPE_DEBUG, "free_sent_reliable %u %lX\n", mid, bitfield);
+  LOG (GNUNET_ERROR_TYPE_DEBUG, " rel %p, head %p\n", rel, rel->head_sent);
+  for (i = 0, r = 0, copy = rel->head_sent;
        i < 64 && NULL != copy && 0 != bitfield;
        i++)
   {
-    LOG (GNUNET_ERROR_TYPE_DEBUG,
-                " trying bit %u (mid %u)\n",
-                i, mid + i + 1);
+    LOG (GNUNET_ERROR_TYPE_DEBUG, " trying bit %u (mid %u)\n", i, mid + i + 1);
     mask = 0x1LL << i;
     if (0 == (bitfield & mask))
      continue;
@@ -1059,23 +1062,28 @@ channel_rel_free_sent (struct CadetChannelReliability *rel,
     /* Did we run out of copies? (previously freed, it's ok) */
     if (NULL == copy)
     {
-     LOG (GNUNET_ERROR_TYPE_DEBUG, "run out of copies...\n");
-     return;
+      LOG (GNUNET_ERROR_TYPE_DEBUG, "run out of copies...\n");
+      return r;
     }
 
     /* Did we overshoot the target? (previously freed, it's ok) */
     if (GC_is_pid_bigger (copy->mid, target))
     {
-     LOG (GNUNET_ERROR_TYPE_DEBUG, " next copy %u\n", copy->mid);
-     continue;
+      LOG (GNUNET_ERROR_TYPE_DEBUG, " next copy %u\n", copy->mid);
+      i += copy->mid - target - 1;   /* MID: 90, t = 85, i += 4 (i++ later) */
+      mask = (0x1LL << (i + 1)) - 1; /* Mask = i-th bit and all before */
+      bitfield &= ~mask;             /* Clear all bits up to MID - 1 */
+      continue;
     }
 
     /* Now copy->mid == target, free it */
     next = copy->next;
     GNUNET_break (GNUNET_YES != rel_message_free (copy, GNUNET_YES));
+    r++;
     copy = next;
   }
   LOG (GNUNET_ERROR_TYPE_DEBUG, "free_sent_reliable END\n");
+  return r;
 }
 
 
@@ -1100,8 +1108,8 @@ rel_message_free (struct CadetReliableMessage *copy, int update_time)
   struct GNUNET_TIME_Relative time;
 
   rel = copy->rel;
-  LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! Freeing %u\n", copy->mid);
-  if (update_time)
+  LOG (GNUNET_ERROR_TYPE_DEBUG, "Freeing %u\n", copy->mid);
+  if (GNUNET_YES == update_time)
   {
     time = GNUNET_TIME_absolute_get_duration (copy->timestamp);
     if (0 == rel->expected_delay.rel_value_us)
@@ -1112,15 +1120,16 @@ rel_message_free (struct CadetReliableMessage *copy, int update_time)
       rel->expected_delay.rel_value_us += time.rel_value_us;
       rel->expected_delay.rel_value_us /= 8;
     }
-    LOG (GNUNET_ERROR_TYPE_INFO, "!!!  took %s, new delay %s\n",
-         GNUNET_STRINGS_relative_time_to_string (time, GNUNET_NO),
+    LOG (GNUNET_ERROR_TYPE_DEBUG, "  message time %12s\n",
+         GNUNET_STRINGS_relative_time_to_string (time, GNUNET_NO));
+    LOG (GNUNET_ERROR_TYPE_DEBUG, "  new delay    %12s\n",
          GNUNET_STRINGS_relative_time_to_string (rel->expected_delay,
                                                  GNUNET_NO));
     rel->retry_timer = rel->expected_delay;
   }
   else
   {
-    LOG (GNUNET_ERROR_TYPE_INFO, "!!! batch free, ignoring timing\n");
+    LOG (GNUNET_ERROR_TYPE_DEBUG, "batch free, ignoring timing\n");
   }
   rel->ch->pending_messages--;
   if (NULL != copy->chq)
@@ -1129,7 +1138,8 @@ rel_message_free (struct CadetReliableMessage *copy, int update_time)
     /* copy->q is set to NULL by ch_message_sent */
   }
   GNUNET_CONTAINER_DLL_remove (rel->head_sent, rel->tail_sent, copy);
-  LOG (GNUNET_ERROR_TYPE_DEBUG, " COPYFREE %p\n", copy);
+  LOG (GNUNET_ERROR_TYPE_DEBUG, " free send copy MID %u at %p\n",
+       copy->mid, copy);
   GNUNET_free (copy);
 
   if (GNUNET_NO != rel->ch->destroy && 0 == rel->ch->pending_messages)
@@ -1168,34 +1178,31 @@ channel_confirm (struct CadetChannel *ch, int fwd)
   {
     rel->client_ready = GNUNET_YES;
     rel->expected_delay = rel->retry_timer;
-    LOG (GNUNET_ERROR_TYPE_DEBUG, "  !! retry timer confirm %s\n",
+    LOG (GNUNET_ERROR_TYPE_DEBUG, "  confirm retry timer %s\n",
          GNUNET_STRINGS_relative_time_to_string (rel->retry_timer, GNUNET_NO));
     if (GCT_get_connections_buffer (ch->t) > 0 || GCT_is_loopback (ch->t))
       send_client_ack (ch, fwd);
 
-    if (GNUNET_SCHEDULER_NO_TASK != rel->retry_task)
+    if (NULL != rel->retry_task)
     {
       GNUNET_SCHEDULER_cancel (rel->retry_task);
-      rel->retry_task = GNUNET_SCHEDULER_NO_TASK;
+      rel->retry_task = NULL;
     }
     else if (NULL != rel->uniq)
     {
       GCT_cancel (rel->uniq->tq);
       /* ch_message_sent will free and NULL uniq */
     }
-    else
+    else if (GNUNET_NO == is_loopback (ch))
     {
-      if (GNUNET_NO == is_loopback (ch))
-      {
-        /* We SHOULD have been trying to retransmit this! */
-        GNUNET_break (0);
-      }
+      /* We SHOULD have been trying to retransmit this! */
+      GNUNET_break (0);
     }
   }
 
   /* In case of a FWD ACK (SYNACK) send a BCK ACK (ACK). */
   if (GNUNET_YES == fwd)
-    send_ack (ch, GNUNET_NO, GNUNET_NO);
+    send_ack (ch, GNUNET_NO);
 }
 
 
@@ -1224,13 +1231,13 @@ channel_save_copy (struct CadetChannel *ch,
   type = ntohs (msg->type);
   size = ntohs (msg->size);
 
-  LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! SAVE %u %s\n", mid, GC_m2s (type));
+  LOG (GNUNET_ERROR_TYPE_DEBUG, "save MID %u %s\n", mid, GC_m2s (type));
   copy = GNUNET_malloc (sizeof (struct CadetReliableMessage) + size);
   LOG (GNUNET_ERROR_TYPE_DEBUG, "  at %p\n", copy);
   copy->mid = mid;
   copy->rel = rel;
   copy->type = type;
-  memcpy (&copy[1], msg, size);
+  GNUNET_memcpy (&copy[1], msg, size);
   GNUNET_CONTAINER_DLL_insert_tail (rel->head_sent, rel->tail_sent, copy);
   ch->pending_messages++;
 
@@ -1250,7 +1257,7 @@ channel_save_copy (struct CadetChannel *ch,
 static struct CadetChannel *
 channel_new (struct CadetTunnel *t,
              struct CadetClient *owner,
-             CADET_ChannelNumber lid_root)
+             struct GNUNET_CADET_ClientChannelNumber lid_root)
 {
   struct CadetChannel *ch;
 
@@ -1263,7 +1270,7 @@ channel_new (struct CadetTunnel *t,
 
   if (NULL != owner)
   {
-    ch->gid = GCT_get_next_chid (t);
+    ch->gid = GCT_get_next_ctn (t);
     GML_channel_add (owner, lid_root, ch);
   }
   GCT_add_channel (t, ch);
@@ -1293,35 +1300,36 @@ handle_loopback (struct CadetChannel *ch,
 
   switch (type)
   {
-    case GNUNET_MESSAGE_TYPE_CADET_DATA:
+    case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_APP_DATA:
       /* Don't send hop ACK, wait for client to ACK */
-      LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! SEND loopback %u (%u)\n",
-           ntohl (((struct GNUNET_CADET_Data *) msgh)->mid), ntohs (msgh->size));
-      GCCH_handle_data (ch, (struct GNUNET_CADET_Data *) msgh, fwd);
+      LOG (GNUNET_ERROR_TYPE_DEBUG, "SEND loopback %u (%u)\n",
+           ntohl (((struct GNUNET_CADET_ChannelAppDataMessage *) msgh)->mid), ntohs (msgh->size));
+      GCCH_handle_data (ch, (struct GNUNET_CADET_ChannelAppDataMessage *) msgh, fwd);
       break;
 
-    case GNUNET_MESSAGE_TYPE_CADET_DATA_ACK:
-      GCCH_handle_data_ack (ch, (struct GNUNET_CADET_DataACK *) msgh, fwd);
+    case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_APP_DATA_ACK:
+      GCCH_handle_data_ack (ch,
+                            (const struct GNUNET_CADET_ChannelDataAckMessage *) msgh, fwd);
       break;
 
-    case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_CREATE:
+    case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_OPEN:
       GCCH_handle_create (ch->t,
-                          (struct GNUNET_CADET_ChannelCreate *) msgh);
+                          (const struct GNUNET_CADET_ChannelOpenMessage *) msgh);
       break;
 
-    case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_ACK:
+    case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_OPEN_ACK:
       GCCH_handle_ack (ch,
-                       (struct GNUNET_CADET_ChannelManage *) msgh,
+                       (const struct GNUNET_CADET_ChannelManageMessage *) msgh,
                        fwd);
       break;
 
-    case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_NACK:
+    case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_OPEN_NACK_DEPRECATED:
       GCCH_handle_nack (ch);
       break;
 
     case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DESTROY:
       GCCH_handle_destroy (ch,
-                           (struct GNUNET_CADET_ChannelManage *) msgh,
+                           (const struct GNUNET_CADET_ChannelManageMessage *) msgh,
                            fwd);
       break;
 
@@ -1358,7 +1366,7 @@ GCCH_destroy (struct CadetChannel *ch)
 
   LOG (GNUNET_ERROR_TYPE_DEBUG, "destroying channel %s:%u\n",
               GCT_2s (ch->t), ch->gid);
-  GCCH_debug (ch);
+  GCCH_debug (ch, GNUNET_ERROR_TYPE_DEBUG);
 
   c = ch->root;
   if (NULL != c)
@@ -1391,7 +1399,7 @@ GCCH_destroy (struct CadetChannel *ch)
  *
  * @return ID used to identify the channel with the remote peer.
  */
-CADET_ChannelNumber
+struct GNUNET_CADET_ChannelTunnelNumber
 GCCH_get_id (const struct CadetChannel *ch)
 {
   return ch->gid;
@@ -1426,14 +1434,19 @@ GCCH_get_buffer (struct CadetChannel *ch, int fwd)
   struct CadetChannelReliability *rel;
 
   rel = fwd ? ch->dest_rel : ch->root_rel;
-
+  LOG (GNUNET_ERROR_TYPE_DEBUG, "   get buffer, channel %s\n", GCCH_2s (ch));
+  GCCH_debug (ch, GNUNET_ERROR_TYPE_DEBUG);
   /* If rel is NULL it means that the end is not yet created,
    * most probably is a loopback channel at the point of sending
    * the ChannelCreate to itself.
    */
   if (NULL == rel)
+  {
+    LOG (GNUNET_ERROR_TYPE_DEBUG, "  rel is NULL: max\n");
     return 64;
+  }
 
+  LOG (GNUNET_ERROR_TYPE_DEBUG, "   n_recv %d\n", rel->n_recv);
   return (64 - rel->n_recv);
 }
 
@@ -1511,7 +1524,7 @@ GCCH_is_terminal (struct CadetChannel *ch, int fwd)
 void
 GCCH_send_data_ack (struct CadetChannel *ch, int fwd)
 {
-  struct GNUNET_CADET_DataACK msg;
+  struct GNUNET_CADET_ChannelDataAckMessage msg;
   struct CadetChannelReliability *rel;
   struct CadetReliableMessage *copy;
   unsigned int delta;
@@ -1519,44 +1532,35 @@ GCCH_send_data_ack (struct CadetChannel *ch, int fwd)
   uint32_t ack;
 
   if (GNUNET_NO == ch->reliable)
-  {
     return;
-  }
+
   rel = fwd ? ch->dest_rel : ch->root_rel;
   ack = rel->mid_recv - 1;
-  LOG (GNUNET_ERROR_TYPE_INFO, "===> DATA_ACK for %u\n", ack);
 
-  msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_DATA_ACK);
+  msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CHANNEL_APP_DATA_ACK);
   msg.header.size = htons (sizeof (msg));
-  msg.chid = htonl (ch->gid);
-  msg.futures = 0;
+  msg.ctn = ch->gid;
+  msg.mid = htonl (ack);
+
+  msg.futures = 0LL;
   for (copy = rel->head_recv; NULL != copy; copy = copy->next)
   {
-    if (copy->type != GNUNET_MESSAGE_TYPE_CADET_DATA)
+    if (copy->type != GNUNET_MESSAGE_TYPE_CADET_CHANNEL_APP_DATA)
     {
-      LOG (GNUNET_ERROR_TYPE_DEBUG,
-           "!!  Type %s, expected DATA\n",
+      LOG (GNUNET_ERROR_TYPE_DEBUG, " Type %s, expected DATA\n",
            GC_m2s (copy->type));
       continue;
     }
-    if (copy->mid == ack + 1)
-    {
-      ack++;
-      continue;
-    }
+    GNUNET_assert (GC_is_pid_bigger(copy->mid, ack));
     delta = copy->mid - (ack + 1);
     if (63 < delta)
       break;
     mask = 0x1LL << delta;
     msg.futures |= mask;
     LOG (GNUNET_ERROR_TYPE_DEBUG,
-         " !! setting bit for %u (delta %u) (%llX) -> %llX\n",
+         " setting bit for %u (delta %u) (%lX) -> %lX\n",
          copy->mid, delta, mask, msg.futures);
   }
-  msg.mid = htonl (ack);
-  LOG (GNUNET_ERROR_TYPE_DEBUG,
-       "!!! ACK for %u, futures %llX\n",
-       ack, msg.futures);
 
   GCCH_send_prebuilt_message (&msg.header, ch, !fwd, NULL);
   LOG (GNUNET_ERROR_TYPE_DEBUG, "send_data_ack END\n");
@@ -1599,8 +1603,15 @@ GCCH_allow_client (struct CadetChannel *ch, int fwd)
         return;
       }
       else
+      {
         LOG (GNUNET_ERROR_TYPE_DEBUG, " gap ok: %u - %u\n",
              rel->head_sent->mid, rel->mid_send);
+        struct CadetReliableMessage *aux;
+        for (aux = rel->head_sent; NULL != aux; aux = aux->next)
+        {
+          LOG (GNUNET_ERROR_TYPE_DEBUG, "   - sent mid %u\n", aux->mid);
+        }
+      }
     }
     else
     {
@@ -1628,34 +1639,46 @@ GCCH_allow_client (struct CadetChannel *ch, int fwd)
  * Log channel info.
  *
  * @param ch Channel.
+ * @param level Debug level to use.
  */
 void
-GCCH_debug (struct CadetChannel *ch)
+GCCH_debug (struct CadetChannel *ch, enum GNUNET_ErrorType level)
 {
+  int do_log;
+
+  do_log = GNUNET_get_log_call_status (level & (~GNUNET_ERROR_TYPE_BULK),
+                                       "cadet-chn",
+                                       __FILE__, __FUNCTION__, __LINE__);
+  if (0 == do_log)
+    return;
+
   if (NULL == ch)
   {
-    LOG (GNUNET_ERROR_TYPE_DEBUG, "*** DEBUG NULL CHANNEL ***\n");
+    LOG2 (level, "CHN *** DEBUG NULL CHANNEL ***\n");
     return;
   }
-  LOG (GNUNET_ERROR_TYPE_DEBUG, "Channel %s:%X (%p)\n",
-              GCT_2s (ch->t), ch->gid, ch);
-  LOG (GNUNET_ERROR_TYPE_DEBUG, "  root %p/%p\n",
-              ch->root, ch->root_rel);
+  LOG2 (level, "CHN Channel %s:%X (%p)\n", GCT_2s (ch->t), ch->gid, ch);
+  LOG2 (level, "CHN   root %p/%p\n", ch->root, ch->root_rel);
   if (NULL != ch->root)
   {
-    LOG (GNUNET_ERROR_TYPE_DEBUG, "  cli %s\n", GML_2s (ch->root));
-    LOG (GNUNET_ERROR_TYPE_DEBUG, "  ready %s\n",
-                ch->root_rel->client_ready ? "YES" : "NO");
-    LOG (GNUNET_ERROR_TYPE_DEBUG, "  id %X\n", ch->lid_root);
+    LOG2 (level, "CHN   cli %s\n", GML_2s (ch->root));
+    LOG2 (level, "CHN   ready %s\n", ch->root_rel->client_ready ? "YES" : "NO");
+    LOG2 (level, "CHN   id %X\n", ch->lid_root.channel_of_client);
+    LOG2 (level, "CHN   recv %d\n", ch->root_rel->n_recv);
+    LOG2 (level, "CHN   MID r: %d, s: %d\n",
+          ch->root_rel->mid_recv, ch->root_rel->mid_send);
   }
-  LOG (GNUNET_ERROR_TYPE_DEBUG, "  dest %p/%p\n",
+  LOG2 (level, "CHN   dest %p/%p\n",
               ch->dest, ch->dest_rel);
   if (NULL != ch->dest)
   {
-    LOG (GNUNET_ERROR_TYPE_DEBUG, "  cli %s\n", GML_2s (ch->dest));
-    LOG (GNUNET_ERROR_TYPE_DEBUG, "  ready %s\n",
-                ch->dest_rel->client_ready ? "YES" : "NO");
-    LOG (GNUNET_ERROR_TYPE_DEBUG, "  id %X\n", ch->lid_dest);
+    LOG2 (level, "CHN   cli %s\n", GML_2s (ch->dest));
+    LOG2 (level, "CHN   ready %s\n", ch->dest_rel->client_ready ? "YES" : "NO");
+    LOG2 (level, "CHN   id %X\n", ch->lid_dest);
+    LOG2 (level, "CHN   recv %d\n", ch->dest_rel->n_recv);
+    LOG2 (level, "CHN   MID r: %d, s: %d\n",
+          ch->dest_rel->mid_recv, ch->dest_rel->mid_send);
+
   }
 }
 
@@ -1684,6 +1707,7 @@ GCCH_handle_local_ack (struct CadetChannel *ch, int fwd)
   {
     send_destroy (ch, GNUNET_YES);
     GCCH_destroy (ch);
+    return;
   }
   /* if loopback is marked for destruction, no need to ACK to the other peer,
    * it requested the destruction and is already gone, therefore, else if.
@@ -1711,22 +1735,24 @@ GCCH_handle_local_ack (struct CadetChannel *ch, int fwd)
  *
  * @param ch Channel.
  * @param c Client which sent the data.
- * @param message Message.
  * @param fwd Is this a FWD data?
+ * @param message Data message.
+ * @param size Size of data.
  *
- * @return GNUNET_OK if everything goes well, GNUNET_SYSERR in case of en error.
+ * @return #GNUNET_OK if everything goes well, #GNUNET_SYSERR in case of en error.
  */
 int
 GCCH_handle_local_data (struct CadetChannel *ch,
                         struct CadetClient *c,
-                        struct GNUNET_MessageHeader *message,
-                        int fwd)
+                        int fwd,
+                        const struct GNUNET_MessageHeader *message,
+                        size_t size)
 {
   struct CadetChannelReliability *rel;
-  struct GNUNET_CADET_Data *payload;
-  size_t size = ntohs (message->size);
-  uint16_t p2p_size = sizeof(struct GNUNET_CADET_Data) + size;
+  struct GNUNET_CADET_ChannelAppDataMessage *payload;
+  uint16_t p2p_size = sizeof(struct GNUNET_CADET_ChannelAppDataMessage) + size;
   unsigned char cbuf[p2p_size];
+  unsigned char buffer;
 
   /* Is the client in the channel? */
   if ( !( (fwd &&
@@ -1750,28 +1776,23 @@ GCCH_handle_local_data (struct CadetChannel *ch,
   rel->client_allowed = GNUNET_NO;
 
   /* Ok, everything is correct, send the message. */
-  payload = (struct GNUNET_CADET_Data *) cbuf;
+  payload = (struct GNUNET_CADET_ChannelAppDataMessage *) cbuf;
   payload->mid = htonl (rel->mid_send);
   rel->mid_send++;
-  memcpy (&payload[1], message, size);
+  GNUNET_memcpy (&payload[1], message, size);
   payload->header.size = htons (p2p_size);
-  payload->header.type = htons (GNUNET_MESSAGE_TYPE_CADET_DATA);
-  payload->chid = htonl (ch->gid);
+  payload->header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CHANNEL_APP_DATA);
+  payload->ctn = ch->gid;
   LOG (GNUNET_ERROR_TYPE_DEBUG, "  sending on channel...\n");
   GCCH_send_prebuilt_message (&payload->header, ch, fwd, NULL);
 
   if (is_loopback (ch))
-  {
-    if (GCCH_get_buffer (ch, fwd) > 0)
-      GCCH_allow_client (ch, fwd);
-
-    return GNUNET_OK;
-  }
+    buffer = GCCH_get_buffer (ch, fwd);
+  else
+    buffer = GCT_get_connections_buffer (ch->t);
 
-  if (GCT_get_connections_buffer (ch->t) > 0)
-  {
+  if (0 < buffer)
     GCCH_allow_client (ch, fwd);
-  }
 
   return GNUNET_OK;
 }
@@ -1780,6 +1801,8 @@ GCCH_handle_local_data (struct CadetChannel *ch,
 /**
  * Handle a channel destroy requested by a client.
  *
+ * TODO: add "reason" field
+ *
  * Destroy the channel and the tunnel in case this was the last channel.
  *
  * @param ch Channel.
@@ -1820,29 +1843,29 @@ GCCH_handle_local_destroy (struct CadetChannel *ch,
  * @param c Client that requested the creation (will be the root).
  * @param msg Create Channel message.
  *
- * @return GNUNET_OK if everything went fine, GNUNET_SYSERR otherwise.
+ * @return #GNUNET_OK if everything went fine, #GNUNET_SYSERR otherwise.
  */
 int
 GCCH_handle_local_create (struct CadetClient *c,
-                          struct GNUNET_CADET_ChannelMessage *msg)
+                          struct GNUNET_CADET_LocalChannelCreateMessage *msg)
 {
   struct CadetChannel *ch;
   struct CadetTunnel *t;
   struct CadetPeer *peer;
-  CADET_ChannelNumber chid;
+  struct GNUNET_CADET_ClientChannelNumber ccn;
 
   LOG (GNUNET_ERROR_TYPE_DEBUG, "  towards %s:%u\n",
-              GNUNET_i2s (&msg->peer), ntohl (msg->port));
-  chid = ntohl (msg->channel_id);
+       GNUNET_i2s (&msg->peer), GNUNET_h2s (&msg->port));
+  ccn = msg->ccn;
 
   /* Sanity check for duplicate channel IDs */
-  if (NULL != GML_channel_get (c, chid))
+  if (NULL != GML_channel_get (c, ccn))
   {
     GNUNET_break (0);
     return GNUNET_SYSERR;
   }
 
-  peer = GCP_get (&msg->peer);
+  peer = GCP_get (&msg->peer, GNUNET_YES);
   GCP_add_tunnel (peer);
   t = GCP_get_tunnel (peer);
 
@@ -1857,13 +1880,13 @@ GCCH_handle_local_create (struct CadetClient *c,
   }
 
   /* Create channel */
-  ch = channel_new (t, c, chid);
+  ch = channel_new (t, c, ccn);
   if (NULL == ch)
   {
     GNUNET_break (0);
     return GNUNET_SYSERR;
   }
-  ch->port = ntohl (msg->port);
+  ch->port = msg->port;
   channel_set_options (ch, ntohl (msg->opt));
 
   /* In unreliable channels, we'll use the DLL to buffer BCK data */
@@ -1892,12 +1915,15 @@ GCCH_handle_local_create (struct CadetClient *c,
  */
 void
 GCCH_handle_data (struct CadetChannel *ch,
-                  const struct GNUNET_CADET_Data *msg,
+                  const struct GNUNET_CADET_ChannelAppDataMessage *msg,
                   int fwd)
 {
   struct CadetChannelReliability *rel;
   struct CadetClient *c;
+  struct GNUNET_MessageHeader *payload_msg;
   uint32_t mid;
+  uint16_t payload_type;
+  uint16_t payload_size;
 
   /* If this is a remote (non-loopback) channel, find 'fwd'. */
   if (GNUNET_SYSERR == fwd)
@@ -1938,30 +1964,35 @@ GCCH_handle_data (struct CadetChannel *ch,
     channel_confirm (ch, GNUNET_NO);
   }
 
-  GNUNET_STATISTICS_update (stats, "# data received", 1, GNUNET_NO);
+  payload_msg = (struct GNUNET_MessageHeader *) &msg[1];
+  payload_type = ntohs (payload_msg->type);
+  payload_size = ntohs (payload_msg->size);
+
+  GNUNET_STATISTICS_update (stats, "# messages received", 1, GNUNET_NO);
+  GNUNET_STATISTICS_update (stats, "# bytes received", payload_size, GNUNET_NO);
 
   mid = ntohl (msg->mid);
-  LOG (GNUNET_ERROR_TYPE_INFO, "<=== DATA %u %s on channel %s\n",
-       mid, GC_f2s (fwd), GCCH_2s (ch));
+  LOG (GNUNET_ERROR_TYPE_INFO, "<== %s (%s %4u) on chan %s (%p) %s [%5u]\n",
+       GC_m2s (GNUNET_MESSAGE_TYPE_CADET_CHANNEL_APP_DATA), GC_m2s (payload_type), mid,
+       GCCH_2s (ch), ch, GC_f2s (fwd), ntohs (msg->header.size));
 
-  if (GNUNET_NO == ch->reliable ||
-      ( !GC_is_pid_bigger (rel->mid_recv, mid) &&
-        GC_is_pid_bigger (rel->mid_recv + 64, mid) ) )
+  if ( (GNUNET_NO == ch->reliable) ||
+       ( (! GC_is_pid_bigger (rel->mid_recv, mid)) &&
+        GC_is_pid_bigger (rel->mid_recv + 64, mid) ) )
   {
-    LOG (GNUNET_ERROR_TYPE_DEBUG, "RECV %u (%u)\n",
-         mid, ntohs (msg->header.size));
     if (GNUNET_YES == ch->reliable)
     {
       /* Is this the exact next expected messasge? */
       if (mid == rel->mid_recv)
       {
-        LOG (GNUNET_ERROR_TYPE_DEBUG, "as expected\n");
-        rel->mid_recv++;
+        LOG (GNUNET_ERROR_TYPE_DEBUG,
+            "as expected, sending to client\n");
         send_client_data (ch, msg, fwd);
       }
       else
       {
-        LOG (GNUNET_ERROR_TYPE_DEBUG, "save for later\n");
+        LOG (GNUNET_ERROR_TYPE_DEBUG,
+            "save for later\n");
         add_buffered_data (msg, rel);
       }
     }
@@ -1975,10 +2006,27 @@ GCCH_handle_data (struct CadetChannel *ch,
   }
   else
   {
-    GNUNET_break_op (GC_is_pid_bigger (rel->mid_recv, mid));
-    LOG (GNUNET_ERROR_TYPE_WARNING,
-         "MID %u on channel %s not expected (window: %u - %u). Dropping!\n",
-         mid, GCCH_2s (ch), rel->mid_recv, rel->mid_recv + 63);
+    GNUNET_STATISTICS_update (stats, "# duplicate MID", 1, GNUNET_NO);
+    if (GC_is_pid_bigger (rel->mid_recv, mid))
+    {
+      GNUNET_break_op (0);
+      LOG (GNUNET_ERROR_TYPE_WARNING,
+           "MID %u on channel %s not expected (window: %u - %u). Dropping!\n",
+           mid, GCCH_2s (ch), rel->mid_recv, rel->mid_recv + 63);
+    }
+    else
+    {
+      LOG (GNUNET_ERROR_TYPE_INFO,
+           "Duplicate MID %u, channel %s (expecting MID %u). Re-sending ACK!\n",
+           mid, GCCH_2s (ch), rel->mid_recv);
+      if (NULL != rel->uniq)
+      {
+        LOG (GNUNET_ERROR_TYPE_WARNING,
+             "We are trying to send an ACK, but don't seem have the "
+             "bandwidth. Have you set enough [ats] QUOTA in your config?\n");
+      }
+
+    }
   }
 
   GCCH_send_data_ack (ch, fwd);
@@ -1997,7 +2045,7 @@ GCCH_handle_data (struct CadetChannel *ch,
  */
 void
 GCCH_handle_data_ack (struct CadetChannel *ch,
-                      const struct GNUNET_CADET_DataACK *msg,
+                      const struct GNUNET_CADET_ChannelDataAckMessage *msg,
                       int fwd)
 {
   struct CadetChannelReliability *rel;
@@ -2020,19 +2068,19 @@ GCCH_handle_data_ack (struct CadetChannel *ch,
   }
 
   ack = ntohl (msg->mid);
-  LOG (GNUNET_ERROR_TYPE_INFO, "<=== %s ACK %u\n", GC_f2s (fwd), ack);
+  LOG (GNUNET_ERROR_TYPE_INFO,
+       "<== %s (0x%010lX %4u) on chan %s (%p) %s [%5u]\n",
+       GC_m2s (GNUNET_MESSAGE_TYPE_CADET_CHANNEL_APP_DATA_ACK), msg->futures, ack,
+       GCCH_2s (ch), ch, GC_f2s (fwd), ntohs (msg->header.size));
 
   if (GNUNET_YES == fwd)
-  {
     rel = ch->root_rel;
-  }
   else
-  {
     rel = ch->dest_rel;
-  }
+
   if (NULL == rel)
   {
-    GNUNET_break_op (GNUNET_NO != ch->destroy);
+    GNUNET_break (GNUNET_NO != ch->destroy);
     return;
   }
 
@@ -2042,14 +2090,18 @@ GCCH_handle_data_ack (struct CadetChannel *ch,
     if (GC_is_pid_bigger (copy->mid, ack))
     {
       LOG (GNUNET_ERROR_TYPE_DEBUG, "  head %u, out!\n", copy->mid);
-      channel_rel_free_sent (rel, msg);
+      if (0 < channel_rel_free_sent (rel, msg))
+        work = GNUNET_YES;
       break;
     }
     work = GNUNET_YES;
     LOG (GNUNET_ERROR_TYPE_DEBUG, "  id %u\n", copy->mid);
     next = copy->next;
     if (GNUNET_YES == rel_message_free (copy, GNUNET_YES))
+    {
+      LOG (GNUNET_ERROR_TYPE_DEBUG, " channel destoyed\n");
       return;
+    }
   }
 
   /* ACK client if needed and possible */
@@ -2058,17 +2110,17 @@ GCCH_handle_data_ack (struct CadetChannel *ch,
   /* If some message was free'd, update the retransmission delay */
   if (GNUNET_YES == work)
   {
-    if (GNUNET_SCHEDULER_NO_TASK != rel->retry_task)
+    if (NULL != rel->retry_task)
     {
       GNUNET_SCHEDULER_cancel (rel->retry_task);
-      rel->retry_task = GNUNET_SCHEDULER_NO_TASK;
+      rel->retry_task = NULL;
       if (NULL != rel->head_sent && NULL == rel->head_sent->chq)
       {
         struct GNUNET_TIME_Absolute new_target;
         struct GNUNET_TIME_Relative delay;
 
-        delay = GNUNET_TIME_relative_multiply (rel->retry_timer,
-                                               CADET_RETRANSMIT_MARGIN);
+        delay = GNUNET_TIME_relative_saturating_multiply (rel->retry_timer,
+                                                          CADET_RETRANSMIT_MARGIN);
         new_target = GNUNET_TIME_absolute_add (rel->head_sent->timestamp,
                                                delay);
         delay = GNUNET_TIME_absolute_get_remaining (new_target);
@@ -2080,8 +2132,11 @@ GCCH_handle_data_ack (struct CadetChannel *ch,
     }
     else
     {
-      /* Work was done but no task was pending? Shouldn't happen! */
-      GNUNET_break (0);
+      /* Work was done but no task was pending.
+       * Task was cancelled by a retransmission that is sitting in the queue.
+       */
+      // FIXME add test to make sure this is the case, probably add return
+      // value to GCCH_send_prebuilt_message
     }
   }
 }
@@ -2097,22 +2152,23 @@ GCCH_handle_data_ack (struct CadetChannel *ch,
  */
 struct CadetChannel *
 GCCH_handle_create (struct CadetTunnel *t,
-                    const struct GNUNET_CADET_ChannelCreate *msg)
+                    const struct GNUNET_CADET_ChannelOpenMessage *msg)
 {
-  CADET_ChannelNumber chid;
+  struct GNUNET_CADET_ClientChannelNumber ccn;
+  struct GNUNET_CADET_ChannelTunnelNumber gid;
   struct CadetChannel *ch;
   struct CadetClient *c;
   int new_channel;
-  int reaction;
+  const struct GNUNET_HashCode *port;
 
-  reaction = GNUNET_NO;
-  chid = ntohl (msg->chid);
-  ch = GCT_get_channel (t, chid);
+  gid = msg->ctn;
+  ch = GCT_get_channel (t, gid);
   if (NULL == ch)
   {
     /* Create channel */
-    ch = channel_new (t, NULL, 0);
-    ch->gid = chid;
+    ccn.channel_of_client = htonl (0);
+    ch = channel_new (t, NULL, ccn);
+    ch->gid = gid;
     channel_set_options (ch, ntohl (msg->opt));
     new_channel = GNUNET_YES;
   }
@@ -2120,13 +2176,19 @@ GCCH_handle_create (struct CadetTunnel *t,
   {
     new_channel = GNUNET_NO;
   }
+  port = &msg->port;
+
+  LOG (GNUNET_ERROR_TYPE_INFO,
+       "<== %s (  0x%08X %4u) on chan %s (%p) %s [%5u]\n",
+       GC_m2s (GNUNET_MESSAGE_TYPE_CADET_CHANNEL_OPEN), ccn, port,
+       GCCH_2s (ch), ch, GC_f2s (GNUNET_YES), ntohs (msg->header.size));
 
   if (GNUNET_YES == new_channel || GCT_is_loopback (t))
   {
     /* Find a destination client */
-    ch->port = ntohl (msg->port);
-    LOG (GNUNET_ERROR_TYPE_DEBUG, "   port %u\n", ch->port);
-    c = GML_client_get_by_port (ch->port);
+    ch->port = *port;
+    LOG (GNUNET_ERROR_TYPE_DEBUG, "   port %s\n", GNUNET_h2s (port));
+    c = GML_client_get_by_port (port);
     if (NULL == c)
     {
       LOG (GNUNET_ERROR_TYPE_DEBUG, "  no client has port registered\n");
@@ -2150,9 +2212,9 @@ GCCH_handle_create (struct CadetTunnel *t,
 
     add_destination (ch, c);
     if (GNUNET_YES == ch->reliable)
-      LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! Reliable\n");
+      LOG (GNUNET_ERROR_TYPE_DEBUG, "Reliable\n");
     else
-      LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! Not Reliable\n");
+      LOG (GNUNET_ERROR_TYPE_DEBUG, "Not Reliable\n");
 
     send_client_create (ch);
     ch->state =  CADET_CHANNEL_SENT;
@@ -2160,16 +2222,20 @@ GCCH_handle_create (struct CadetTunnel *t,
   else
   {
     LOG (GNUNET_ERROR_TYPE_DEBUG, "  duplicate create channel\n");
-    reaction = GNUNET_YES;
-    if (GNUNET_SCHEDULER_NO_TASK != ch->dest_rel->retry_task)
+    if (NULL != ch->dest_rel->retry_task)
     {
       LOG (GNUNET_ERROR_TYPE_DEBUG, "  clearing retry task\n");
       /* we were waiting to re-send our 'SYNACK', wait no more! */
       GNUNET_SCHEDULER_cancel (ch->dest_rel->retry_task);
-      ch->dest_rel->retry_task = GNUNET_SCHEDULER_NO_TASK;
+      ch->dest_rel->retry_task = NULL;
+    }
+    else if (NULL != ch->dest_rel->uniq)
+    {
+      /* we are waiting to for our 'SYNACK' to leave the queue, all done! */
+      return ch;
     }
   }
-  send_ack (ch, GNUNET_YES, reaction);
+  send_ack (ch, GNUNET_YES);
 
   return ch;
 }
@@ -2185,6 +2251,11 @@ GCCH_handle_create (struct CadetTunnel *t,
 void
 GCCH_handle_nack (struct CadetChannel *ch)
 {
+  LOG (GNUNET_ERROR_TYPE_INFO,
+       "<== %s (  0x%08X %4u) on chan %s (%p) %s [%5u]\n",
+       GC_m2s (GNUNET_MESSAGE_TYPE_CADET_CHANNEL_OPEN_NACK_DEPRECATED), ch->gid, 0,
+       GCCH_2s (ch), ch, "---", 0);
+
   send_client_nack (ch);
   GCCH_destroy (ch);
 }
@@ -2202,9 +2273,14 @@ GCCH_handle_nack (struct CadetChannel *ch)
  */
 void
 GCCH_handle_ack (struct CadetChannel *ch,
-                 const struct GNUNET_CADET_ChannelManage *msg,
+                 const struct GNUNET_CADET_ChannelManageMessage *msg,
                  int fwd)
 {
+  LOG (GNUNET_ERROR_TYPE_INFO,
+       "<== %s (  0x%08X %4u) on chan %s (%p) %s [%5u]\n",
+       GC_m2s (GNUNET_MESSAGE_TYPE_CADET_CHANNEL_OPEN_ACK), ch->gid, 0,
+       GCCH_2s (ch), ch, GC_f2s (fwd), ntohs (msg->header.size));
+
   /* If this is a remote (non-loopback) channel, find 'fwd'. */
   if (GNUNET_SYSERR == fwd)
   {
@@ -2233,11 +2309,16 @@ GCCH_handle_ack (struct CadetChannel *ch,
  */
 void
 GCCH_handle_destroy (struct CadetChannel *ch,
-                     const struct GNUNET_CADET_ChannelManage *msg,
+                     const struct GNUNET_CADET_ChannelManageMessage *msg,
                      int fwd)
 {
   struct CadetChannelReliability *rel;
 
+  LOG (GNUNET_ERROR_TYPE_INFO,
+       "<== %s (  0x%08X %4u) on chan %s (%p) %s [%5u]\n",
+       GC_m2s (GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DESTROY), ch->gid, 0,
+       GCCH_2s (ch), ch, GC_f2s (fwd), ntohs (msg->header.size));
+
   /* If this is a remote (non-loopback) channel, find 'fwd'. */
   if (GNUNET_SYSERR == fwd)
   {
@@ -2250,7 +2331,7 @@ GCCH_handle_destroy (struct CadetChannel *ch,
     fwd = (NULL != ch->dest) ? GNUNET_YES : GNUNET_NO;
   }
 
-  GCCH_debug (ch);
+  GCCH_debug (ch, GNUNET_ERROR_TYPE_DEBUG);
   if ( (fwd && NULL == ch->dest) || (!fwd && NULL == ch->root) )
   {
     /* Not for us (don't destroy twice a half-open loopback channel) */
@@ -2292,11 +2373,63 @@ GCCH_send_prebuilt_message (const struct GNUNET_MessageHeader *message,
                             void *existing_copy)
 {
   struct CadetChannelQueue *chq;
+  uint32_t data_id;
   uint16_t type;
+  uint16_t size;
+  char info[32];
 
   type = ntohs (message->type);
-  LOG (GNUNET_ERROR_TYPE_INFO, "===> %s %s on channel %s\n",
-       GC_m2s (type), GC_f2s (fwd), GCCH_2s (ch));
+  size = ntohs (message->size);
+
+  data_id = 0;
+  switch (type)
+  {
+    case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_APP_DATA:
+    {
+      struct GNUNET_CADET_ChannelAppDataMessage *data_msg;
+      struct GNUNET_MessageHeader *payload_msg;
+      uint16_t payload_type;
+
+      data_msg = (struct GNUNET_CADET_ChannelAppDataMessage *) message;
+      data_id = ntohl (data_msg->mid);
+      payload_msg = (struct GNUNET_MessageHeader *) &data_msg[1];
+      payload_type = ntohs (payload_msg->type);
+      strncpy (info, GC_m2s (payload_type), 31);
+      info[31] = '\0';
+      break;
+    }
+    case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_APP_DATA_ACK:
+    {
+      struct GNUNET_CADET_ChannelDataAckMessage *ack_msg;
+      ack_msg = (struct GNUNET_CADET_ChannelDataAckMessage *) message;
+      data_id = ntohl (ack_msg->mid);
+      SPRINTF (info, "0x%010lX",
+              (unsigned long int) ack_msg->futures);
+      break;
+    }
+    case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_OPEN:
+    {
+      struct GNUNET_CADET_ChannelOpenMessage *cc_msg;
+      cc_msg = (struct GNUNET_CADET_ChannelOpenMessage *) message;
+      SPRINTF (info, "  0x%08X", ntohl (cc_msg->ctn.cn));
+      break;
+    }
+    case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_OPEN_ACK:
+    case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_OPEN_NACK_DEPRECATED:
+    case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DESTROY:
+    {
+      struct GNUNET_CADET_ChannelManageMessage *m_msg;
+      m_msg = (struct GNUNET_CADET_ChannelManageMessage *) message;
+      SPRINTF (info, "  0x%08X", ntohl (m_msg->ctn.cn));
+      break;
+    }
+    default:
+      info[0] = '\0';
+  }
+  LOG (GNUNET_ERROR_TYPE_INFO,
+       "==> %s (%12s %4u) on chan %s (%p) %s [%5u]\n",
+       GC_m2s (type), info, data_id,
+       GCCH_2s (ch), ch, GC_f2s (fwd), size);
 
   if (GCT_is_loopback (ch->t))
   {
@@ -2306,12 +2439,7 @@ GCCH_send_prebuilt_message (const struct GNUNET_MessageHeader *message,
 
   switch (type)
   {
-    struct GNUNET_CADET_Data *payload;
-    case GNUNET_MESSAGE_TYPE_CADET_DATA:
-
-      payload = (struct GNUNET_CADET_Data *) message;
-      LOG (GNUNET_ERROR_TYPE_INFO, "===> %s %u\n",
-           GC_m2s (type), ntohl (payload->mid));
+    case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_APP_DATA:
       if (GNUNET_YES == ch->reliable)
       {
         chq = GNUNET_new (struct CadetChannelQueue);
@@ -2343,10 +2471,10 @@ GCCH_send_prebuilt_message (const struct GNUNET_MessageHeader *message,
                chq->copy->rel, chq->copy->chq, chq->copy->type);
         }
         LOG (GNUNET_ERROR_TYPE_DEBUG, "  new chq: %p\n", chq);
-            chq->copy->chq = chq;
-            chq->tq = GCT_send_prebuilt_message (message, ch->t, NULL,
-                                                 NULL != existing_copy,
-                                                 &ch_message_sent, chq);
+        chq->copy->chq = chq;
+        chq->tq = GCT_send_prebuilt_message (message, ch->t, NULL,
+                                             GNUNET_YES,
+                                             &ch_message_sent, chq);
         /* q itself is stored in copy */
         GNUNET_assert (NULL != chq->tq || GNUNET_NO != ch->destroy);
       }
@@ -2357,16 +2485,9 @@ GCCH_send_prebuilt_message (const struct GNUNET_MessageHeader *message,
       break;
 
 
-    case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_ACK:
-      if (GNUNET_YES == fwd || NULL != existing_copy)
-      {
-        /* BCK ACK (going FWD) is just a response for a SYNACK, don't keep*/
-        fire_and_forget (message, ch, GNUNET_YES);
-        return;
-      }
-      /* fall-trough */
-    case GNUNET_MESSAGE_TYPE_CADET_DATA_ACK:
-    case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_CREATE:
+    case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_APP_DATA_ACK:
+    case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_OPEN:
+    case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_OPEN_ACK:
       chq = GNUNET_new (struct CadetChannelQueue);
       chq->type = type;
       chq->rel = fwd ? ch->root_rel : ch->dest_rel;
@@ -2376,6 +2497,7 @@ GCCH_send_prebuilt_message (const struct GNUNET_MessageHeader *message,
         {
           GCT_cancel (chq->rel->uniq->tq);
           /* ch_message_sent is called, freeing and NULLing uniq */
+          GNUNET_break (NULL == chq->rel->uniq);
         }
         else
         {
@@ -2383,28 +2505,31 @@ GCCH_send_prebuilt_message (const struct GNUNET_MessageHeader *message,
           GNUNET_free (chq->rel->uniq);
         }
       }
+
+      chq->rel->uniq = chq;
       chq->tq = GCT_send_prebuilt_message (message, ch->t, NULL, GNUNET_YES,
                                            &ch_message_sent, chq);
       if (NULL == chq->tq)
       {
         GNUNET_break (0);
+       chq->rel->uniq = NULL;
+        GCT_debug (ch->t, GNUNET_ERROR_TYPE_ERROR);
         GNUNET_free (chq);
         chq = NULL;
         return;
       }
-      chq->rel->uniq = chq;
       break;
 
 
     case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DESTROY:
-    case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_NACK:
+    case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_OPEN_NACK_DEPRECATED:
       fire_and_forget (message, ch, GNUNET_YES);
       break;
 
 
     default:
       GNUNET_break (0);
-      LOG (GNUNET_ERROR_TYPE_DEBUG, "type %s unknown!\n", GC_m2s (type));
+      LOG (GNUNET_ERROR_TYPE_WARNING, "type %s unknown!\n", GC_m2s (type));
       fire_and_forget (message, ch, GNUNET_YES);
   }
 }
@@ -2425,8 +2550,13 @@ GCCH_2s (const struct CadetChannel *ch)
   if (NULL == ch)
     return "(NULL Channel)";
 
-  sprintf (buf, "%s:%u gid:%X (%X / %X)",
-           GCT_2s (ch->t), ch->port, ch->gid, ch->lid_root, ch->lid_dest);
+  SPRINTF (buf,
+           "%s:%s gid:%X (%X / %X)",
+           GCT_2s (ch->t),
+           GNUNET_h2s (&ch->port),
+           ntohl (ch->gid.cn),
+           ntohl (ch->lid_root.channel_of_client),
+           ntohl (ch->lid_dest.channel_of_client));
 
   return buf;
 }