- fix #3091
[oweals/gnunet.git] / src / mesh / gnunet-service-mesh_channel.c
index ab0927117cc489c35edd3a58d71a993ba1026ce2..e7fc0323b9a1ed3e5956c154ee3c5b2151ce2902 100644 (file)
@@ -30,6 +30,7 @@
 #include "gnunet-service-mesh_channel.h"
 #include "gnunet-service-mesh_local.h"
 #include "gnunet-service-mesh_tunnel.h"
+#include "gnunet-service-mesh_peer.h"
 
 #define LOG(level, ...) GNUNET_log_from(level,"mesh-chn",__VA_ARGS__)
 
@@ -53,7 +54,7 @@ enum MeshChannelState
   MESH_CHANNEL_SENT,
 
   /**
-   * Connection confirmed, ready to carry traffic..
+   * Connection confirmed, ready to carry traffic.
    */
   MESH_CHANNEL_READY,
 };
@@ -142,6 +143,11 @@ struct MeshChannelReliability
      */
   int                               client_ready;
 
+  /**
+   * Can the client send data to us?
+   */
+  int                               client_allowed;
+
     /**
      * Task to resend/poll in case no ACK is received.
      */
@@ -257,6 +263,11 @@ struct MeshChannel
  */
 extern struct GNUNET_STATISTICS_Handle *stats;
 
+/**
+ * Local peer own ID (memory efficient handle).
+ */
+extern GNUNET_PEER_Id myid;
+
 
 /******************************************************************************/
 /********************************   STATIC  ***********************************/
@@ -319,6 +330,36 @@ add_buffered_data (const struct GNUNET_MESH_Data *msg,
     LOG (GNUNET_ERROR_TYPE_DEBUG, "add_buffered_data END\n");
 }
 
+/**
+ * Add a destination client to a channel, initializing all data structures
+ * in the channel and the client.
+ *
+ * @param ch Channel to which add the destination.
+ * @param c Client which to add to the channel.
+ */
+static void
+add_destination (struct MeshChannel *ch, struct MeshClient *c)
+{
+  if (NULL != ch->dest)
+  {
+    GNUNET_break (0);
+    return;
+  }
+
+  /* Assign local id as destination */
+  ch->lid_dest = GML_get_next_chid (c);
+
+  /* Store in client's hashmap */
+  GML_channel_add (c, ch->lid_dest, ch);
+
+  GNUNET_break (NULL == ch->dest_rel);
+  ch->dest_rel = GNUNET_new (struct MeshChannelReliability);
+  ch->dest_rel->ch = ch;
+  ch->dest_rel->expected_delay = MESH_RETRANSMIT_TIME;
+
+  ch->dest = c;
+}
+
 
 /**
  * Send data to a client.
@@ -338,14 +379,14 @@ send_client_data (struct MeshChannel *ch,
   if (fwd)
   {
     if (ch->dest_rel->client_ready)
-      GML_send_data (ch, msg, ch->dest, ch->lid_dest);
+      GML_send_data (ch->dest, msg, ch->lid_dest);
     else
       add_buffered_data (msg, ch->dest_rel);
   }
   else
   {
     if (ch->root_rel->client_ready)
-      GML_send_data (ch, msg, ch->root, ch->lid_root);
+      GML_send_data (ch->root, msg, ch->lid_root);
     else
       add_buffered_data (msg, ch->root_rel);
   }
@@ -353,34 +394,107 @@ send_client_data (struct MeshChannel *ch,
 
 
 /**
- * Add a client to a channel, initializing all needed data structures.
+ * Send a buffered message to the client, for in order delivery or
+ * as result of client ACK.
  *
- * @param ch Channel to which add the client.
- * @param c Client which to add to the channel.
+ * @param ch Channel on which to empty the message buffer.
+ * @param c Client to send to.
+ * @param fwd Is this to send FWD data?.
  */
-void
-GMCH_add_client (struct MeshChannel *ch, struct MeshClient *c)
+static void
+send_client_buffered_data (struct MeshChannel *ch,
+                           struct MeshClient *c,
+                           int fwd)
 {
-  struct MeshTunnel3 *t = ch->t;
+  struct MeshReliableMessage *copy;
+  struct MeshChannelReliability *rel;
 
-  if (NULL != ch->dest)
+  LOG (GNUNET_ERROR_TYPE_DEBUG, "send_buffered_data\n");
+  rel = fwd ? ch->dest_rel : ch->root_rel;
+  if (GNUNET_NO == rel->client_ready)
+  {
+    LOG (GNUNET_ERROR_TYPE_DEBUG, "client not ready\n");
+    return;
+  }
+
+  copy = rel->head_recv;
+  /* We never buffer channel management messages */
+  if (NULL != copy)
+  {
+    if (copy->mid == rel->mid_recv || GNUNET_NO == ch->reliable)
+    {
+      struct GNUNET_MESH_Data *msg = (struct GNUNET_MESH_Data *) &copy[1];
+
+      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);
+      GNUNET_free (copy);
+    }
+    else
+    {
+      LOG (GNUNET_ERROR_TYPE_DEBUG,
+                  " reliable && don't have %u, next is %u\n",
+                  rel->mid_recv,
+                  copy->mid);
+      return;
+    }
+  }
+  LOG (GNUNET_ERROR_TYPE_DEBUG, "send_buffered_data END\n");
+}
+
+
+/**
+ * Allow a client to send more data.
+ *
+ * In case the client was already allowed to send data, do nothing.
+ *
+ * @param ch Channel.
+ * @param fwd Is this a FWD ACK? (FWD ACKs are sent to root)
+ */
+static void
+send_client_ack (struct MeshChannel *ch, int fwd)
+{
+  struct MeshChannelReliability *rel = fwd ? ch->root_rel : ch->dest_rel;
+
+  LOG (GNUNET_ERROR_TYPE_DEBUG,
+       "  sending %s ack to client on channel %s\n",
+       fwd ? "FWD" : "BCK", GMCH_2s (ch));
+
+  if (NULL == rel)
   {
     GNUNET_break (0);
     return;
   }
 
-  /* Assign local id as destination */
-  ch->lid_dest = GML_get_next_chid (c);
+  if (GNUNET_YES == rel->client_allowed)
+  {
+    LOG (GNUNET_ERROR_TYPE_DEBUG, "  already allowed\n");
+    return;
+  }
+  rel->client_allowed = GNUNET_YES;
 
-  /* Store in client's hashmap */
-  GML_channel_add (c, ch->lid_dest, ch);
+  GML_send_ack (fwd ? ch->root : ch->dest, fwd ? ch->lid_root : ch->lid_dest);
+}
 
-  GNUNET_break (NULL == ch->dest_rel);
-  ch->dest_rel = GNUNET_new (struct MeshChannelReliability);
-  ch->dest_rel->ch = ch;
-  ch->dest_rel->expected_delay = MESH_RETRANSMIT_TIME;
 
-  ch->dest = c;
+/**
+ * Notify the root that the destination rejected the channel.
+ *
+ * @param ch Rejected channel.
+ */
+static void
+send_client_nack (struct MeshChannel *ch)
+{
+  if (NULL == ch->root)
+  {
+    GNUNET_break (0);
+    return;
+  }
+  GML_send_nack (ch->root, ch->lid_root);
 }
 
 
@@ -501,11 +615,8 @@ channel_retransmit_message (void *cls,
 {
   struct MeshChannelReliability *rel = cls;
   struct MeshReliableMessage *copy;
-  struct MeshPeerQueue *q;
   struct MeshChannel *ch;
-  struct MeshConnection *c;
   struct GNUNET_MESH_Data *payload;
-  struct MeshPeer *hop;
   int fwd;
 
   rel->retry_task = GNUNET_SCHEDULER_NO_TASK;
@@ -531,8 +642,8 @@ channel_retransmit_message (void *cls,
    *   is stalled.
    */
 //   FIXME access to queue elements is limited
-//   payload = (struct GNUNET_MESH_Data *) &copy[1];
-//   fwd = (rel == ch->root_rel);
+  payload = (struct GNUNET_MESH_Data *) &copy[1];
+  fwd = (rel == ch->root_rel);
 //   c = GMT_get_connection (ch->t, fwd);
 //   hop = connection_get_hop (c, fwd);
 //   for (q = hop->queue_head; NULL != q; q = q->next)
@@ -598,12 +709,54 @@ rel_message_free (struct MeshReliableMessage *copy)
 }
 
 
+/**
+ * Confirm we got a channel create.
+ *
+ * @param ch The channel to confirm.
+ * @param fwd Should we send a FWD ACK? (going dest->root)
+ */
+static void
+channel_send_ack (struct MeshChannel *ch, int fwd)
+{
+  struct GNUNET_MESH_ChannelManage msg;
+
+  msg.header.size = htons (sizeof (msg));
+  msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CHANNEL_ACK);
+  LOG (GNUNET_ERROR_TYPE_DEBUG,
+              "  sending channel %s ack for channel %s\n",
+              fwd ? "FWD" : "BCK", GMCH_2s (ch));
+
+  msg.chid = htonl (ch->gid);
+  GMCH_send_prebuilt_message (&msg.header, ch, !fwd);
+}
+
+
+/**
+ * Notify that a channel create didn't succeed.
+ *
+ * @param ch The channel to reject.
+ */
+static void
+channel_send_nack (struct MeshChannel *ch)
+{
+  struct GNUNET_MESH_ChannelManage msg;
+
+  msg.header.size = htons (sizeof (msg));
+  msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CHANNEL_NACK);
+  LOG (GNUNET_ERROR_TYPE_DEBUG,
+       "  sending channel NACK for channel %s\n",
+       GMCH_2s (ch));
+
+  msg.chid = htonl (ch->gid);
+  GMCH_send_prebuilt_message (&msg.header, ch, GNUNET_NO);
+}
+
 
 /**
  * Channel was ACK'd by remote peer, mark as ready and cancel retransmission.
  *
  * @param ch Channel to mark as ready.
- * @param fwd Was the CREATE message sent fwd?
+ * @param fwd Was the ACK message a FWD ACK? (dest->root, SYNACK)
  */
 static void
 channel_confirm (struct MeshChannel *ch, int fwd)
@@ -618,6 +771,7 @@ channel_confirm (struct MeshChannel *ch, int fwd)
   ch->state = MESH_CHANNEL_READY;
 
   rel = fwd ? ch->root_rel : ch->dest_rel;
+  rel->client_ready = GNUNET_YES;
   for (copy = rel->head_sent; NULL != copy; copy = next)
   {
     struct GNUNET_MessageHeader *msg;
@@ -630,7 +784,11 @@ channel_confirm (struct MeshChannel *ch, int fwd)
       /* TODO return? */
     }
   }
-  send_ack (NULL, ch, fwd);
+  send_client_ack (ch, fwd);
+
+  /* In case of a FWD ACK (SYNACK) send a BCK ACK (ACK). */
+  if (fwd)
+    channel_send_ack (ch, !fwd);
 }
 
 
@@ -682,64 +840,6 @@ channel_save_copy (struct MeshChannel *ch,
 }
 
 
-
-/**
- * Send a buffered message to the client, for in order delivery or
- * as result of client ACK.
- *
- * @param ch Channel on which to empty the message buffer.
- * @param c Client to send to.
- * @param rel Reliability structure to corresponding peer.
- *            If rel == bck_rel, this is FWD data.
- */
-static void
-send_client_buffered_data (struct MeshChannel *ch,
-                                   struct MeshClient *c,
-                                   int fwd)
-{
-  struct MeshReliableMessage *copy;
-  struct MeshChannelReliability *rel;
-
-  LOG (GNUNET_ERROR_TYPE_DEBUG, "send_buffered_data\n");
-  rel = fwd ? ch->dest_rel : ch->root_rel;
-  if (GNUNET_NO == rel->client_ready)
-  {
-    LOG (GNUNET_ERROR_TYPE_DEBUG, "client not ready\n");
-    return;
-  }
-
-  copy = rel->head_recv;
-  /* We never buffer channel management messages */
-  if (NULL != copy)
-  {
-    if (copy->mid == rel->mid_recv || GNUNET_NO == ch->reliable)
-    {
-      struct GNUNET_MESH_Data *msg = (struct GNUNET_MESH_Data *) &copy[1];
-
-      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);
-      GNUNET_free (copy);
-    }
-    else
-    {
-      LOG (GNUNET_ERROR_TYPE_DEBUG,
-                  " reliable && don't have %u, next is %u\n",
-                  rel->mid_recv,
-                  copy->mid);
-      return;
-    }
-  }
-  LOG (GNUNET_ERROR_TYPE_DEBUG, "send_buffered_data END\n");
-}
-
-
-
-
 /**
  * Destroy a channel and free all resources.
  *
@@ -790,7 +890,8 @@ channel_destroy (struct MeshChannel *ch)
  */
 static struct MeshChannel *
 channel_new (struct MeshTunnel3 *t,
-             struct MeshClient *owner, MESH_ChannelNumber lid_root)
+             struct MeshClient *owner,
+             MESH_ChannelNumber lid_root)
 {
   struct MeshChannel *ch;
 
@@ -827,69 +928,13 @@ channel_set_options (struct MeshChannel *ch, uint32_t options)
                  GNUNET_YES : GNUNET_NO;
 }
 
-
-
-/**
- * Confirm we got a channel create.
- *
- * @param ch The channel to confirm.
- * @param fwd Should we send the ACK fwd?
- */
-static void
-channel_send_ack (struct MeshChannel *ch, int fwd)
-{
-  struct GNUNET_MESH_ChannelManage msg;
-
-  msg.header.size = htons (sizeof (msg));
-  msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CHANNEL_ACK);
-  LOG (GNUNET_ERROR_TYPE_DEBUG,
-              "  sending channel %s ack for channel %s:%X\n",
-              fwd ? "FWD" : "BCK", GMT_2s (ch->t),
-              ch->gid);
-
-  msg.chid = htonl (ch->gid);
-  GMCH_send_prebuilt_message (&msg.header, ch, !fwd);
-}
-
-
-/**
- * Iterator for deleting each channel whose client endpoint disconnected.
- *
- * @param cls Closure (client that has disconnected).
- * @param key The local channel id (used to access the hashmap).
- * @param value The value stored at the key (channel to destroy).
- *
- * @return GNUNET_OK, keep iterating.
- */
 static int
-channel_destroy_iterator (void *cls,
-                          uint32_t key,
-                          void *value)
+is_loopback (const struct MeshChannel *ch)
 {
-  struct MeshChannel *ch = value;
-  struct MeshClient *c = cls;
-  struct MeshTunnel3 *t;
-
-  LOG (GNUNET_ERROR_TYPE_DEBUG,
-              " Channel %X (%X / %X) destroy, due to client %s shutdown.\n",
-              ch->gid, ch->lid_root, ch->lid_dest, GML_2s (c));
-  GMCH_debug (ch);
+  if (NULL != ch->t)
+    return GMT_is_loopback (ch->t);
 
-  if (c == ch->dest)
-  {
-    LOG (GNUNET_ERROR_TYPE_DEBUG, " Client %s is destination.\n", GML_2s (c));
-  }
-  if (c == ch->root)
-  {
-    LOG (GNUNET_ERROR_TYPE_DEBUG, " Client %s is owner.\n", GML_2s (c));
-  }
-
-  t = ch->t;
-  GMCH_send_destroy (ch);
-  channel_destroy (ch);
-  GMT_destroy_if_empty (t);
-
-  return GNUNET_OK;
+  return (NULL != ch->root && NULL != ch->dest);
 }
 
 
@@ -902,15 +947,15 @@ channel_destroy_iterator (void *cls,
  */
 void
 handle_loopback (struct MeshChannel *ch,
-                 struct GNUNET_MessageHeader *msgh,
+                 const struct GNUNET_MessageHeader *msgh,
                  int fwd)
 {
   uint16_t type;
 
   type = ntohs (msgh->type);
   LOG (GNUNET_ERROR_TYPE_DEBUG,
-       "Loopback %s message!\n",
-       GNUNET_MESH_DEBUG_M2S (type));
+       "Loopback %s %s message!\n",
+       fwd ? "FWD" : "BCK", GNUNET_MESH_DEBUG_M2S (type));
 
   switch (type)
   {
@@ -924,9 +969,8 @@ handle_loopback (struct MeshChannel *ch,
       break;
 
     case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE:
-       // FIXME store channel in loopback tunnel?
-      GMCH_handle_create ((struct GNUNET_MESH_ChannelCreate *) msgh,
-                          fwd);
+      GMCH_handle_create (ch->t,
+                          (struct GNUNET_MESH_ChannelCreate *) msgh);
       break;
 
     case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_ACK:
@@ -935,6 +979,10 @@ handle_loopback (struct MeshChannel *ch,
                        fwd);
       break;
 
+    case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_NACK:
+      GMCH_handle_nack (ch);
+      break;
+
     case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY:
       GMCH_handle_destroy (ch,
                            (struct GNUNET_MESH_ChannelManage *) msgh,
@@ -955,6 +1003,7 @@ handle_loopback (struct MeshChannel *ch,
 /********************************    API    ***********************************/
 /******************************************************************************/
 
+
 /**
  * Get channel ID.
  *
@@ -1009,13 +1058,32 @@ GMCH_get_buffer (struct MeshChannel *ch, int fwd)
 }
 
 
+/**
+ * Get flow control status of end point: is client allow to send?
+ *
+ * @param ch Channel.
+ * @param fwd Is query about FWD traffic? (Request root status).
+ *
+ * @return #GNUNET_YES if client is allowed to send us data.
+ */
+int
+GMCH_get_allowed (struct MeshChannel *ch, int fwd)
+{
+  struct MeshChannelReliability *rel;
+
+  rel = fwd ? ch->root_rel : ch->dest_rel;
+
+  return rel->client_allowed;
+}
+
+
 /**
  * Is the root client for this channel on this peer?
  *
  * @param ch Channel.
  * @param fwd Is this for fwd traffic?
  *
- * @return GNUNET_YES in case it is.
+ * @return #GNUNET_YES in case it is.
  */
 int
 GMCH_is_origin (struct MeshChannel *ch, int fwd)
@@ -1033,7 +1101,7 @@ GMCH_is_origin (struct MeshChannel *ch, int fwd)
  * @param ch Channel.
  * @param fwd Is this for fwd traffic?
  *
- * @return GNUNET_YES in case it is.
+ * @return #GNUNET_YES in case it is.
  */
 int
 GMCH_is_terminal (struct MeshChannel *ch, int fwd)
@@ -1053,7 +1121,6 @@ GMCH_is_terminal (struct MeshChannel *ch, int fwd)
 void
 GMCH_send_create (struct MeshChannel *ch)
 {
-  struct GNUNET_MESH_ChannelMessage msg;
   uint32_t opt;
 
   if (NULL == ch->dest)
@@ -1069,18 +1136,32 @@ GMCH_send_create (struct MeshChannel *ch)
 
 /**
  * Notify a client that the channel is no longer valid.
- * FIXME send on tunnel if some client == NULL?
  *
  * @param ch Channel that is destroyed.
  */
 void
 GMCH_send_destroy (struct MeshChannel *ch)
 {
+  struct GNUNET_MESH_ChannelManage msg;
+
+  msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY);
+  msg.header.size = htons (sizeof (msg));
+  msg.chid = htonl (ch->gid);
+
+  /* If root is not NULL, notify.
+   * If it's NULL, check lid_root. When a local destroy comes in, root 
+   * is set to NULL but lid_root is left untouched. In this case, do nothing,
+   * the client is the one who reuqested the channel to be destroyed.
+   */
   if (NULL != ch->root)
     GML_send_channel_destroy (ch->root, ch->lid_root);
+  else if (0 == ch->lid_root)
+    GMCH_send_prebuilt_message (&msg.header, ch, GNUNET_NO);
 
   if (NULL != ch->dest)
     GML_send_channel_destroy (ch->dest, ch->lid_dest);
+  else if (0 == ch->lid_dest)
+    GMCH_send_prebuilt_message (&msg.header, ch, GNUNET_YES);
 }
 
 
@@ -1099,6 +1180,16 @@ GMCH_send_data (struct MeshChannel *ch,
                 const struct GNUNET_MESH_Data *msg,
                 int fwd)
 {
+  if (GMCH_is_terminal (ch, fwd))
+  {
+    GML_send_data (fwd ? ch->dest : ch->root,
+                   msg,
+                   fwd ? ch->lid_dest : ch->lid_root);
+  }
+  else
+  {
+    GMT_send_prebuilt_message (&msg->header, ch->t, ch, fwd);
+  }
 }
 
 
@@ -1111,7 +1202,7 @@ GMCH_send_data (struct MeshChannel *ch,
  * @param fwd Is for FWD traffic? (ACK dest->owner)
  */
 void
-GMCH_send_ack (struct MeshChannel *ch, int fwd)
+GMCH_send_data_ack (struct MeshChannel *ch, int fwd)
 {
   struct GNUNET_MESH_DataACK msg;
   struct MeshChannelReliability *rel;
@@ -1155,6 +1246,22 @@ GMCH_send_ack (struct MeshChannel *ch, int fwd)
 }
 
 
+/**
+ * Allow a client to send us more data, in case it was choked.
+ *
+ * @param ch Channel.
+ * @param fwd Is this about FWD traffic? (Root client).
+ */
+void
+GMCH_allow_client (struct MeshChannel *ch, int fwd)
+{
+  if (MESH_CHANNEL_READY != ch->state)
+    return;
+
+  send_client_ack (ch, fwd);
+}
+
+
 /**
  * Log channel info.
  *
@@ -1191,12 +1298,230 @@ GMCH_debug (struct MeshChannel *ch)
 }
 
 
+/**
+ * Handle an ACK given by a client.
+ *
+ * Mark client as ready and send him any buffered data we could have for him.
+ *
+ * @param ch Channel.
+ * @param fwd Is this a "FWD ACK"? (FWD ACKs are sent by dest and go BCK)
+ */
+void
+GMCH_handle_local_ack (struct MeshChannel *ch, int fwd)
+{
+  struct MeshChannelReliability *rel;
+  struct MeshClient *c;
+
+  rel = fwd ? ch->dest_rel : ch->root_rel;
+  c   = fwd ? ch->dest     : ch->root;
+
+  rel->client_ready = GNUNET_YES;
+  send_client_buffered_data (ch, c, fwd);
+  if (is_loopback (ch))
+  {
+    unsigned int buffer;
+
+    buffer = GMCH_get_buffer (ch, fwd);
+    if (0 < buffer)
+      GMCH_allow_client (ch, fwd);
+
+    return;
+  }
+  GMT_send_connection_acks (ch->t);
+}
+
+
+/**
+ * Handle data given by a client.
+ *
+ * Check whether the client is allowed to send in this tunnel, save if channel
+ * is reliable and send an ACK to the client if there is still buffer space
+ * in the tunnel.
+ *
+ * @param ch Channel.
+ * @param c Client which sent the data.
+ * @param message Message.
+ * @param fwd Is this a FWD data?
+ *
+ * @return GNUNET_OK if everything goes well, GNUNET_SYSERR in case of en error.
+ */
+int
+GMCH_handle_local_data (struct MeshChannel *ch,
+                        struct MeshClient *c,
+                        struct GNUNET_MessageHeader *message,
+                        int fwd)
+{
+  struct MeshChannelReliability *rel;
+  struct GNUNET_MESH_Data *payload;
+  size_t size = ntohs (message->size);
+  uint16_t p2p_size = sizeof(struct GNUNET_MESH_Data) + size;
+  unsigned char cbuf[p2p_size];
+
+  /* Is the client in the channel? */
+  if ( !( (fwd &&
+           ch->root == c)
+         ||
+          (!fwd &&
+           ch->dest == c) ) )
+  {
+    GNUNET_break (0);
+    return GNUNET_SYSERR;
+  }
+
+  rel = fwd ? ch->root_rel : ch->dest_rel;
+
+  rel->client_allowed = GNUNET_NO;
+
+  /* Ok, everything is correct, send the message. */
+  payload = (struct GNUNET_MESH_Data *) cbuf;
+  payload->mid = htonl (rel->mid_send);
+  rel->mid_send++;
+  memcpy (&payload[1], message, size);
+  payload->header.size = htons (p2p_size);
+  payload->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_DATA);
+  payload->chid = htonl (ch->gid);
+  LOG (GNUNET_ERROR_TYPE_DEBUG, "  sending on channel...\n");
+  if (GNUNET_YES == ch->reliable)
+    channel_save_copy (ch, &payload->header, fwd);
+  GMCH_send_prebuilt_message (&payload->header, ch, fwd);
+
+  if (is_loopback (ch))
+  {
+    if (GMCH_get_buffer (ch, fwd) > 0);
+      send_client_ack (ch, fwd);
+
+    return GNUNET_OK;
+  }
+
+  if (GMT_get_connections_buffer (ch->t) > 0)
+  {
+    send_client_ack (ch, fwd);
+  }
+
+  return GNUNET_OK;
+}
+
+
+/**
+ * Handle a channel destroy requested by a client.
+ *
+ * Destroy the channel and the tunnel in case this was the last channel.
+ *
+ * @param ch Channel.
+ * @param c Client that requested the destruction (to avoid notifying him).
+ */
+void
+GMCH_handle_local_destroy (struct MeshChannel *ch,
+                           struct MeshClient *c)
+{
+  struct MeshTunnel3 *t;
+
+  /* Cleanup after the tunnel */
+  if (c == ch->dest)
+  {
+    LOG (GNUNET_ERROR_TYPE_DEBUG, " Client %s is destination.\n", GML_2s (c));
+    GML_client_delete_channel (c, ch, ch->lid_dest);
+    ch->dest = NULL;
+  }
+  if (c == ch->root)
+  {
+    LOG (GNUNET_ERROR_TYPE_DEBUG, " Client %s is owner.\n", GML_2s (c));
+    GML_client_delete_channel (c, ch, ch->lid_root);
+    ch->root = NULL;
+  }
+
+  t = ch->t;
+  GMCH_send_destroy (ch);
+  channel_destroy (ch);
+  GMT_destroy_if_empty (t);
+}
+
+
+/**
+ * Handle a channel create requested by a client.
+ *
+ * Create the channel and the tunnel in case this was the first0 channel.
+ *
+ * @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.
+ */
+int
+GMCH_handle_local_create (struct MeshClient *c,
+                          struct GNUNET_MESH_ChannelMessage *msg)
+{
+  struct MeshChannel *ch;
+  struct MeshTunnel3 *t;
+  struct MeshPeer *peer;
+  MESH_ChannelNumber chid;
+
+  LOG (GNUNET_ERROR_TYPE_DEBUG, "  towards %s:%u\n",
+              GNUNET_i2s (&msg->peer), ntohl (msg->port));
+  chid = ntohl (msg->channel_id);
+
+  /* Sanity check for duplicate channel IDs */
+  if (NULL != GML_channel_get (c, chid))
+  {
+    GNUNET_break (0);
+    return GNUNET_SYSERR;
+  }
+
+  peer = GMP_get (&msg->peer);
+  GMP_add_tunnel (peer);
+  t = GMP_get_tunnel (peer);
+
+  if (GMP_get_short_id (peer) == myid)
+  {
+    GMT_change_state (t, MESH_TUNNEL3_READY);
+  }
+  else
+  {
+    GMP_connect (peer);
+  }
+
+  /* Create channel */
+  ch = channel_new (t, c, chid);
+  if (NULL == ch)
+  {
+    GNUNET_break (0);
+    return GNUNET_SYSERR;
+  }
+  ch->port = ntohl (msg->port);
+  channel_set_options (ch, ntohl (msg->opt));
+
+  /* In unreliable channels, we'll use the DLL to buffer BCK data */
+  ch->root_rel = GNUNET_new (struct MeshChannelReliability);
+  ch->root_rel->ch = ch;
+  ch->root_rel->expected_delay = MESH_RETRANSMIT_TIME;
+
+  LOG (GNUNET_ERROR_TYPE_DEBUG, "CREATED CHANNEL %s\n", GMCH_2s (ch));
+
+  /* Send create channel */
+  {
+    struct GNUNET_MESH_ChannelCreate msgcc;
+
+    msgcc.header.size = htons (sizeof (msgcc));
+    msgcc.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE);
+    msgcc.chid = htonl (ch->gid);
+    msgcc.port = msg->port;
+    msgcc.opt = msg->opt;
+
+    GMT_send_prebuilt_message (&msgcc.header, t, ch, GNUNET_YES);
+  }
+  return GNUNET_OK;
+}
+
+
 /**
  * Handler for mesh network payload traffic.
  *
  * @param ch Channel for the message.
- * @param message Unencryted data message.
- * @param fwd Is this FWD traffic? GNUNET_YES : GNUNET_NO;
+ * @param msg Unencryted data message.
+ * @param fwd Is this message fwd? This only is meaningful in loopback channels.
+ *            #GNUNET_YES if message is FWD on the respective channel (loopback)
+ *            #GNUNET_NO if message is BCK on the respective channel (loopback)
+ *            #GNUNET_SYSERR if message on a one-ended channel (remote)
  */
 void
 GMCH_handle_data (struct MeshChannel *ch,
@@ -1206,8 +1531,18 @@ GMCH_handle_data (struct MeshChannel *ch,
   struct MeshChannelReliability *rel;
   struct MeshClient *c;
   uint32_t mid;
-  uint16_t type;
-  size_t size;
+
+  /* If this is a remote (non-loopback) channel, find 'fwd'. */
+  if (GNUNET_SYSERR == fwd)
+  {
+    if (is_loopback (ch))
+    {
+      /* It is a loopback channel after all... */
+      GNUNET_break (0);
+      return;
+    }
+    fwd = (NULL != ch->dest) ? GNUNET_YES : GNUNET_NO;
+  }
 
   /*  Initialize FWD/BCK data */
   c   = fwd ? ch->dest     : ch->root;
@@ -1260,16 +1595,19 @@ GMCH_handle_data (struct MeshChannel *ch,
                 mid, rel->mid_recv, rel->mid_recv + 64);
   }
 
-  GMCH_send_ack (ch, fwd);
+  GMCH_send_data_ack (ch, fwd);
 }
 
 
 /**
  * Handler for mesh network traffic end-to-end ACKs.
  *
- * @param t Tunnel on which we got this message.
- * @param message Data message.
- * @param fwd Is this a fwd ACK? (dest->orig)
+ * @param ch Channel on which we got this message.
+ * @param msg Data message.
+ * @param fwd Is this message fwd? This only is meaningful in loopback channels.
+ *            #GNUNET_YES if message is FWD on the respective channel (loopback)
+ *            #GNUNET_NO if message is BCK on the respective channel (loopback)
+ *            #GNUNET_SYSERR if message on a one-ended channel (remote)
  */
 void
 GMCH_handle_data_ack (struct MeshChannel *ch,
@@ -1282,9 +1620,21 @@ GMCH_handle_data_ack (struct MeshChannel *ch,
   uint32_t ack;
   int work;
 
+  /* If this is a remote (non-loopback) channel, find 'fwd'. */
+  if (GNUNET_SYSERR == fwd)
+  {
+    if (is_loopback (ch))
+    {
+      /* It is a loopback channel after all... */
+      GNUNET_break (0);
+      return;
+    }
+    fwd = (NULL != ch->dest) ? GNUNET_YES : GNUNET_NO;
+  }
+
   ack = ntohl (msg->mid);
   LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! %s ACK %u\n",
-              (GNUNET_YES == fwd) ? "FWD" : "BCK", ack);
+       (GNUNET_YES == fwd) ? "FWD" : "BCK", ack);
 
   if (GNUNET_YES == fwd)
   {
@@ -1351,12 +1701,14 @@ GMCH_handle_data_ack (struct MeshChannel *ch,
 /**
  * Handler for channel create messages.
  *
- * @param msg Message.
- * @param fwd Is this FWD traffic? GNUNET_YES : GNUNET_NO;
+ * Does not have fwd parameter because it's always 'FWD': channel is incoming.
+ *
+ * @param t Tunnel this channel will be in.
+ * @param msg Channel crate message.
  */
 struct MeshChannel *
-GMCH_handle_create (const struct GNUNET_MESH_ChannelCreate *msg,
-                    int fwd)
+GMCH_handle_create (struct MeshTunnel3 *t,
+                    const struct GNUNET_MESH_ChannelCreate *msg)
 {
   MESH_ChannelNumber chid;
   struct MeshChannel *ch;
@@ -1365,9 +1717,13 @@ GMCH_handle_create (const struct GNUNET_MESH_ChannelCreate *msg,
 
   chid = ntohl (msg->chid);
 
-  /* Create channel */
-  ch = channel_new (NULL, NULL, 0); /* FIXME t */
-  ch->gid = chid;
+  ch = GMT_get_channel (t, chid);
+  if (NULL == ch)
+  {
+    /* Create channel */
+    ch = channel_new (t, NULL, 0);
+    ch->gid = chid;
+  }
   channel_set_options (ch, ntohl (msg->opt));
 
   /* Find a destination client */
@@ -1378,34 +1734,77 @@ GMCH_handle_create (const struct GNUNET_MESH_ChannelCreate *msg,
   {
     /* TODO send reject */
     LOG (GNUNET_ERROR_TYPE_DEBUG, "  no client has port registered\n");
-    /* TODO free ch */
-    return;
+    if (is_loopback (ch))
+    {
+      channel_send_nack (ch);
+    }
+    else
+    {
+      channel_send_nack (ch);
+      channel_destroy (ch);
+    }
+    return NULL;
+  }
+  else
+  {
+    LOG (GNUNET_ERROR_TYPE_DEBUG, "  client %p has port registered\n", c);
   }
 
-  channel_add_client (ch, c);
+  add_destination (ch, c);
   if (GNUNET_YES == ch->reliable)
     LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! Reliable\n");
+  else
+    LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! Not Reliable\n");
 
   GMCH_send_create (ch);
-  GMCH_send_ack (ch, fwd);
-  GML_send_ack (ch, !fwd);
+  channel_send_ack (ch, GNUNET_YES);
 
   return ch;
 }
 
 
+/**
+ * Handler for channel NACK messages.
+ *
+ * NACK messages always go dest -> root, no need for 'fwd' or 'msg' parameter.
+ *
+ * @param ch Channel.
+ */
+void
+GMCH_handle_nack (struct MeshChannel *ch)
+{
+  send_client_nack (ch);
+  channel_destroy (ch);
+}
+
+
 /**
  * Handler for channel ack messages.
  *
  * @param ch Channel.
  * @param msg Message.
- * @param fwd Is this FWD traffic? GNUNET_YES : GNUNET_NO;
+ * @param fwd Is this message fwd? This only is meaningful in loopback channels.
+ *            #GNUNET_YES if message is FWD on the respective channel (loopback)
+ *            #GNUNET_NO if message is BCK on the respective channel (loopback)
+ *            #GNUNET_SYSERR if message on a one-ended channel (remote)
  */
 void
 GMCH_handle_ack (struct MeshChannel *ch,
                  const struct GNUNET_MESH_ChannelManage *msg,
                  int fwd)
 {
+  /* If this is a remote (non-loopback) channel, find 'fwd'. */
+  if (GNUNET_SYSERR == fwd)
+  {
+    if (is_loopback (ch))
+    {
+      /* It is a loopback channel after all... */
+      GNUNET_break (0);
+      return;
+    }
+    fwd = (NULL != ch->dest) ? GNUNET_YES : GNUNET_NO;
+  }
+
   channel_confirm (ch, !fwd);
 }
 
@@ -1415,31 +1814,55 @@ GMCH_handle_ack (struct MeshChannel *ch,
  *
  * @param ch Channel to be destroyed of.
  * @param msg Message.
- * @param fwd Is this FWD traffic? GNUNET_YES : GNUNET_NO;
+ * @param fwd Is this message fwd? This only is meaningful in loopback channels.
+ *            #GNUNET_YES if message is FWD on the respective channel (loopback)
+ *            #GNUNET_NO if message is BCK on the respective channel (loopback)
+ *            #GNUNET_SYSERR if message on a one-ended channel (remote)
  */
 void
 GMCH_handle_destroy (struct MeshChannel *ch,
                      const struct GNUNET_MESH_ChannelManage *msg,
                      int fwd)
 {
-  MESH_ChannelNumber chid;
+  struct MeshTunnel3 *t;
 
-  /* Check if channel exists */
-  chid = ntohl (msg->chid);
+  /* If this is a remote (non-loopback) channel, find 'fwd'. */
+  if (GNUNET_SYSERR == fwd)
+  {
+    if (is_loopback (ch))
+    {
+      /* It is a loopback channel after all... */
+      GNUNET_break (0);
+      return;
+    }
+    fwd = (NULL != ch->dest) ? GNUNET_YES : GNUNET_NO;
+  }
+
+  GMCH_debug (ch);
   if ( (fwd && NULL == ch->dest) || (!fwd && NULL == ch->root) )
   {
     /* Not for us (don't destroy twice a half-open loopback channel) */
     return;
   }
 
+  t = ch->t;
   GMCH_send_destroy (ch);
   channel_destroy (ch);
+  GMT_destroy_if_empty (t);
 }
 
 
 /**
  * Sends an already built message on a channel.
  *
+ * If the channel is on a loopback tunnel, notifies the appropriate destination
+ * client locally.
+ *
+ * On a normal channel passes the message to the tunnel for encryption and
+ * sending on a connection.
+ *
+ * This function DOES NOT save the message for retransmission.
+ *
  * @param message Message to send. Function makes a copy of it.
  * @param ch Channel on which this message is transmitted.
  * @param fwd Is this a fwd message?
@@ -1448,18 +1871,37 @@ void
 GMCH_send_prebuilt_message (const struct GNUNET_MessageHeader *message,
                             struct MeshChannel *ch, int fwd)
 {
-  size_t size = ntohs (message->size);
-
-  LOG (GNUNET_ERROR_TYPE_DEBUG, "Send on Channel %s:%X %s\n",
-       GMT_2s (ch->t), ch->gid, fwd ? "FWD" : "BCK");
-  LOG (GNUNET_ERROR_TYPE_DEBUG, "  %s\n",
-       GNUNET_MESH_DEBUG_M2S (ntohs (message->type)));
+  LOG (GNUNET_ERROR_TYPE_DEBUG, "GMCH Send %s %s on channel %s\n",
+       fwd ? "FWD" : "BCK", GNUNET_MESH_DEBUG_M2S (ntohs (message->type)), 
+       GMCH_2s (ch));
 
   if (GMT_is_loopback (ch->t))
   {
-    handle_loopback (ch->t, message, fwd);
+    handle_loopback (ch, message, fwd);
     return;
   }
 
   GMT_send_prebuilt_message (message, ch->t, ch, fwd);
 }
+
+
+/**
+ * Get the static string for identification of the channel.
+ *
+ * @param ch Channel.
+ *
+ * @return Static string with the channel IDs.
+ */
+const char *
+GMCH_2s (const struct MeshChannel *ch)
+{
+  static char buf[64];
+
+  if (NULL == ch)
+    return "(NULL Channel)";
+
+  sprintf (buf, "%s:%X (%X / %X)",
+           GMT_2s (ch->t), ch->gid, ch->lid_root, ch->lid_dest);
+
+  return buf;
+}