loglevel MESSAGE for the incoming connection message + type
[oweals/gnunet.git] / src / cadet / gnunet-service-cadet_local.c
index b7ac11c5e12b4e9912d733c484e2532ea3ce5ba1..dea6681df8681b6f6f36054ae6c27c278b17c60c 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.
 */
 
 
  */
 struct CadetClient
 {
-    /**
-     * Linked list next
-     */
+  /**
+   * Linked list next
+   */
   struct CadetClient *next;
 
-    /**
-     * Linked list prev
-     */
+  /**
+   * Linked list prev
+   */
   struct CadetClient *prev;
 
-    /**
-     * Tunnels that belong to this client, indexed by local id
-     */
+  /**
+   * Tunnels that belong to this client, indexed by local id
+   */
   struct GNUNET_CONTAINER_MultiHashMap32 *own_channels;
 
-    /**
-     * Tunnels this client has accepted, indexed by incoming local id
-     */
+  /**
+   * Tunnels this client has accepted, indexed by incoming local id
+   */
   struct GNUNET_CONTAINER_MultiHashMap32 *incoming_channels;
 
-    /**
-     * Channel ID for the next incoming channel.
-     */
-  CADET_ChannelNumber next_chid;
+  /**
+   * Channel ID for the next incoming channel.
+   */
+  struct GNUNET_CADET_ClientChannelNumber next_ccn;
 
-    /**
-     * Handle to communicate with the client
-     */
+  /**
+   * Handle to communicate with the client
+   */
   struct GNUNET_SERVER_Client *handle;
 
-    /**
-     * Ports that this client has declared interest in.
-     * Indexed by port, contains *Client.
-     */
-  struct GNUNET_CONTAINER_MultiHashMap32 *ports;
+  /**
+   * Ports that this client has declared interest in.
+   * Indexed by port, contains *Client.
+   */
+  struct GNUNET_CONTAINER_MultiHashMap *ports;
 
-    /**
-     * Whether the client is active or shutting down (don't send confirmations
-     * to a client that is shutting down.
-     */
+  /**
+   * Whether the client is active or shutting down (don't send confirmations
+   * to a client that is shutting down.
+   */
   int shutting_down;
 
-    /**
-     * ID of the client, mainly for debug messages
-     */
+  /**
+   * ID of the client, mainly for debug messages
+   */
   unsigned int id;
 };
 
@@ -127,7 +127,7 @@ unsigned int next_client_id;
 /**
  * All ports clients of this peer have opened.
  */
-static struct GNUNET_CONTAINER_MultiHashMap32 *ports;
+static struct GNUNET_CONTAINER_MultiHashMap *ports;
 
 /**
  * Notification context, to send messages to local clients.
@@ -146,82 +146,144 @@ static struct GNUNET_SERVER_NotificationContext *nc;
  * @param key Port.
  * @param value Client structure.
  *
- * @return GNUNET_OK, keep iterating.
+ * @return #GNUNET_OK, keep iterating.
  */
 static int
 client_release_ports (void *cls,
-                      uint32_t key,
+                      const struct GNUNET_HashCode *key,
                       void *value)
 {
   int res;
 
-  res = GNUNET_CONTAINER_multihashmap32_remove (ports, key, value);
+  res = GNUNET_CONTAINER_multihashmap_remove (ports, key, value);
   if (GNUNET_YES != res)
   {
     GNUNET_break (0);
     LOG (GNUNET_ERROR_TYPE_WARNING,
-                "Port %u by client %p was not registered.\n",
-                key, value);
+         "Port %s by client %p was not registered.\n",
+         GNUNET_h2s (key), value);
   }
   return GNUNET_OK;
 }
 
 
+/**
+ * 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)
+{
+  struct CadetChannel *ch = value;
+  struct CadetClient *c = cls;
 
-/******************************************************************************/
-/********************************  HANDLES  ***********************************/
-/******************************************************************************/
+  LOG (GNUNET_ERROR_TYPE_DEBUG,
+       " Channel %s destroy, due to client %s shutdown.\n",
+       GCCH_2s (ch), GML_2s (c));
+
+  GCCH_handle_local_destroy (ch,
+                             c,
+                             key < GNUNET_CADET_LOCAL_CHANNEL_ID_CLI);
+  return GNUNET_OK;
+}
 
 
 /**
- * Handler for client connection.
+ * Unregister data and free memory for a client.
  *
- * @param cls Closure (unused).
- * @param client Client handler.
+ * @param c Client to destroy. No longer valid after call.
  */
 static void
-handle_client_connect (void *cls, struct GNUNET_SERVER_Client *client)
+client_destroy (struct CadetClient *c)
+{
+  LOG (GNUNET_ERROR_TYPE_DEBUG, "  client destroy: %p/%u\n", c, c->id);
+  GNUNET_SERVER_client_drop (c->handle);
+  c->shutting_down = GNUNET_YES;
+
+  if (NULL != c->own_channels)
+  {
+    GNUNET_CONTAINER_multihashmap32_iterate (c->own_channels,
+                                             &channel_destroy_iterator, c);
+    GNUNET_CONTAINER_multihashmap32_destroy (c->own_channels);
+  }
+  if (NULL != c->incoming_channels)
+  {
+    GNUNET_CONTAINER_multihashmap32_iterate (c->incoming_channels,
+                                             &channel_destroy_iterator, c);
+    GNUNET_CONTAINER_multihashmap32_destroy (c->incoming_channels);
+  }
+  if (NULL != c->ports)
+  {
+    GNUNET_CONTAINER_multihashmap_iterate (c->ports,
+                                           &client_release_ports, c);
+    GNUNET_CONTAINER_multihashmap_destroy (c->ports);
+  }
+
+  GNUNET_CONTAINER_DLL_remove (clients_head, clients_tail, c);
+  GNUNET_STATISTICS_update (stats, "# clients", -1, GNUNET_NO);
+  GNUNET_SERVER_client_set_user_context (c->handle, NULL);
+  GNUNET_free (c);
+}
+
+
+/**
+ * Create a client record, register data and initialize memory.
+ *
+ * @param client Client's handle.
+ */
+static struct CadetClient *
+client_new (struct GNUNET_SERVER_Client *client)
 {
   struct CadetClient *c;
 
-  LOG (GNUNET_ERROR_TYPE_DEBUG, "client connected: %p\n", client);
-  if (NULL == client)
-    return;
+  GNUNET_SERVER_client_keep (client);
+  GNUNET_SERVER_notification_context_add (nc, client);
+
   c = GNUNET_new (struct CadetClient);
   c->handle = client;
   c->id = next_client_id++; /* overflow not important: just for debug */
-  c->next_chid = GNUNET_CADET_LOCAL_CHANNEL_ID_SERV;
-  GNUNET_SERVER_client_keep (client);
+
+  c->own_channels = GNUNET_CONTAINER_multihashmap32_create (32);
+  c->incoming_channels = GNUNET_CONTAINER_multihashmap32_create (32);
+
   GNUNET_SERVER_client_set_user_context (client, c);
   GNUNET_CONTAINER_DLL_insert (clients_head, clients_tail, c);
+  GNUNET_STATISTICS_update (stats, "# clients", +1, GNUNET_NO);
+
+  LOG (GNUNET_ERROR_TYPE_DEBUG, "  client created: %p/%u\n", c, c->id);
+
+  return c;
 }
 
 
+/******************************************************************************/
+/********************************  HANDLES  ***********************************/
+/******************************************************************************/
+
 /**
- * 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).
+ * Handler for client connection.
  *
- * @return GNUNET_OK, keep iterating.
+ * @param cls Closure (unused).
+ * @param client Client handler.
  */
-static int
-channel_destroy_iterator (void *cls,
-                          uint32_t key,
-                          void *value)
+static void
+handle_client_connect (void *cls, struct GNUNET_SERVER_Client *client)
 {
-  struct CadetChannel *ch = value;
-  struct CadetClient *c = cls;
-
-  LOG (GNUNET_ERROR_TYPE_DEBUG,
-              " Channel %s destroy, due to client %s shutdown.\n",
-              GCCH_2s (ch), GML_2s (c));
+  LOG (GNUNET_ERROR_TYPE_DEBUG, "Client connected: %p\n", client);
+  if (NULL == client)
+    return;
 
-  GCCH_handle_local_destroy (ch, c, key < GNUNET_CADET_LOCAL_CHANNEL_ID_SERV);
-  return GNUNET_OK;
+  (void) client_new (client);
 }
 
+
 /**
  * Handler for client disconnection
  *
@@ -234,123 +296,126 @@ handle_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
 {
   struct CadetClient *c;
 
-  LOG (GNUNET_ERROR_TYPE_DEBUG, "client disconnected: %p\n", client);
-  if (client == NULL)
-  {
-    LOG (GNUNET_ERROR_TYPE_DEBUG, "   (SERVER DOWN)\n");
-    return;
-  }
+  LOG (GNUNET_ERROR_TYPE_DEBUG, "Client disconnected: %p\n", client);
 
   c = GML_client_get (client);
   if (NULL != c)
   {
     LOG (GNUNET_ERROR_TYPE_DEBUG, "matching client found (%u, %p)\n",
                 c->id, c);
-    GNUNET_SERVER_client_drop (c->handle);
-    c->shutting_down = GNUNET_YES;
-    if (NULL != c->own_channels)
-    {
-      GNUNET_CONTAINER_multihashmap32_iterate (c->own_channels,
-                                               &channel_destroy_iterator, c);
-      GNUNET_CONTAINER_multihashmap32_destroy (c->own_channels);
-    }
-
-    if (NULL != c->incoming_channels)
-    {
-      GNUNET_CONTAINER_multihashmap32_iterate (c->incoming_channels,
-                                               &channel_destroy_iterator, c);
-      GNUNET_CONTAINER_multihashmap32_destroy (c->incoming_channels);
-    }
-
-    if (NULL != c->ports)
-    {
-      GNUNET_CONTAINER_multihashmap32_iterate (c->ports,
-                                               &client_release_ports, c);
-      GNUNET_CONTAINER_multihashmap32_destroy (c->ports);
-    }
-    GNUNET_CONTAINER_DLL_remove (clients_head, clients_tail, c);
-    GNUNET_STATISTICS_update (stats, "# clients", -1, GNUNET_NO);
-    LOG (GNUNET_ERROR_TYPE_DEBUG, "  client free (%p)\n", c);
-    GNUNET_free (c);
+    client_destroy (c);
   }
   else
   {
-    LOG (GNUNET_ERROR_TYPE_WARNING, " context NULL!\n");
+    LOG (GNUNET_ERROR_TYPE_DEBUG, " disconnecting client's context NULL\n");
   }
-  LOG (GNUNET_ERROR_TYPE_DEBUG, "done!\n");
   return;
 }
 
 
 /**
- * Handler for new clients
+ * Handler for port open requests.
  *
- * @param cls closure
- * @param client identification of the client
- * @param message the actual message, which includes messages the client wants
+ * @param cls Closure.
+ * @param client Identification of the client.
+ * @param message The actual message.
  */
 static void
-handle_new_client (void *cls, struct GNUNET_SERVER_Client *client,
+handle_port_open (void *cls, struct GNUNET_SERVER_Client *client,
+                  const struct GNUNET_MessageHeader *message)
+{
+  struct CadetClient *c;
+  struct GNUNET_CADET_PortMessage *pmsg;
+
+  LOG (GNUNET_ERROR_TYPE_DEBUG, "open port requested\n");
+
+  /* Sanity check for client registration */
+  if (NULL == (c = GML_client_get (client)))
+  {
+    GNUNET_break (0);
+    GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
+    return;
+  }
+  LOG (GNUNET_ERROR_TYPE_DEBUG, "  by client %u\n", c->id);
+
+  /* Message size sanity check */
+  if (sizeof (struct GNUNET_CADET_PortMessage) != ntohs (message->size))
+  {
+    GNUNET_break (0);
+    GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
+    return;
+  }
+
+  pmsg = (struct GNUNET_CADET_PortMessage *) message;
+  if (NULL == c->ports)
+  {
+    c->ports = GNUNET_CONTAINER_multihashmap_create (4, GNUNET_NO);
+  }
+  /* store in client's hashmap */
+  if (GNUNET_OK !=
+      GNUNET_CONTAINER_multihashmap_put (c->ports, &pmsg->port, c,
+                                         GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
+  {
+    GNUNET_break (0);
+    GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
+    return;
+  }
+  /* store in global hashmap */
+  /* FIXME only allow one client to have the port open,
+   *       have a backup hashmap with waiting clients */
+  GNUNET_CONTAINER_multihashmap_put (ports, &pmsg->port, c,
+                                     GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
+
+  GNUNET_SERVER_receive_done (client, GNUNET_OK);
+}
+
+
+/**
+ * Handler for port close requests.
+ *
+ * @param cls Closure.
+ * @param client Identification of the client.
+ * @param message The actual message.
+ */
+static void
+handle_port_close (void *cls, struct GNUNET_SERVER_Client *client,
                    const struct GNUNET_MessageHeader *message)
 {
-  struct GNUNET_CADET_ClientConnect *cc_msg;
   struct CadetClient *c;
-  unsigned int size;
-  uint32_t *p;
-  unsigned int i;
+  struct GNUNET_CADET_PortMessage *pmsg;
+  int removed;
 
-  LOG (GNUNET_ERROR_TYPE_DEBUG, "\n");
-  LOG (GNUNET_ERROR_TYPE_DEBUG, "new client connected %p\n", client);
+  LOG (GNUNET_ERROR_TYPE_DEBUG, "close port requested\n");
 
-  /* Check data sanity */
-  size = ntohs (message->size) - sizeof (struct GNUNET_CADET_ClientConnect);
-  cc_msg = (struct GNUNET_CADET_ClientConnect *) message;
-  if (0 != (size % sizeof (uint32_t)))
+  /* Sanity check for client registration */
+  if (NULL == (c = GML_client_get (client)))
   {
     GNUNET_break (0);
     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
     return;
   }
-  size /= sizeof (uint32_t);
+  LOG (GNUNET_ERROR_TYPE_DEBUG, "  by client %u\n", c->id);
 
-  /* Initialize new client structure */
-  c = GNUNET_SERVER_client_get_user_context (client, struct CadetClient);
-  LOG (GNUNET_ERROR_TYPE_DEBUG, "  client id %u\n", c->id);
-  LOG (GNUNET_ERROR_TYPE_DEBUG, "  client has %u ports\n", size);
-  if (size > 0)
+    /* Message size sanity check */
+  if (sizeof (struct GNUNET_CADET_PortMessage) != ntohs (message->size))
   {
-    uint32_t u32;
-
-    p = (uint32_t *) &cc_msg[1];
-    c->ports = GNUNET_CONTAINER_multihashmap32_create (size);
-    for (i = 0; i < size; i++)
-    {
-      u32 = ntohl (p[i]);
-      LOG (GNUNET_ERROR_TYPE_DEBUG, "    port: %u\n", u32);
-
-      /* store in client's hashmap */
-      GNUNET_CONTAINER_multihashmap32_put (c->ports, u32, c,
-                                           GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
-      /* store in global hashmap */
-      /* FIXME only allow one client to have the port open,
-       *       have a backup hashmap with waiting clients */
-      GNUNET_CONTAINER_multihashmap32_put (ports, u32, c,
-                                           GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
-    }
+    GNUNET_break (0);
+    GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
+    return;
   }
 
-  c->own_channels = GNUNET_CONTAINER_multihashmap32_create (32);
-  c->incoming_channels = GNUNET_CONTAINER_multihashmap32_create (32);
-  GNUNET_SERVER_notification_context_add (nc, client);
-  GNUNET_STATISTICS_update (stats, "# clients", 1, GNUNET_NO);
+  pmsg = (struct GNUNET_CADET_PortMessage *) message;
+  removed = GNUNET_CONTAINER_multihashmap_remove (c->ports, &pmsg->port, c);
+  GNUNET_break_op (GNUNET_YES == removed);
+  removed = GNUNET_CONTAINER_multihashmap_remove (ports, &pmsg->port, c);
+  GNUNET_break_op (GNUNET_YES == removed);
 
   GNUNET_SERVER_receive_done (client, GNUNET_OK);
-  LOG (GNUNET_ERROR_TYPE_DEBUG, "new client processed\n");
 }
 
 
 /**
- * Handler for requests of new tunnels
+ * Handler for requests of new channels.
  *
  * @param cls Closure.
  * @param client Identification of the client.
@@ -375,7 +440,8 @@ handle_channel_create (void *cls, struct GNUNET_SERVER_Client *client,
   LOG (GNUNET_ERROR_TYPE_DEBUG, "  by client %u\n", c->id);
 
   /* Message size sanity check */
-  if (sizeof (struct GNUNET_CADET_ChannelMessage) != ntohs (message->size))
+  if (sizeof (struct GNUNET_CADET_LocalChannelCreateMessage)
+      != ntohs (message->size))
   {
     GNUNET_break (0);
     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
@@ -384,14 +450,14 @@ handle_channel_create (void *cls, struct GNUNET_SERVER_Client *client,
 
   if (GNUNET_OK !=
       GCCH_handle_local_create (c,
-                                (struct GNUNET_CADET_ChannelMessage *) message))
+                                (struct GNUNET_CADET_LocalChannelCreateMessage *)
+                                message))
   {
     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
     return;
   }
 
   GNUNET_SERVER_receive_done (client, GNUNET_OK);
-  return;
 }
 
 
@@ -406,10 +472,10 @@ static void
 handle_channel_destroy (void *cls, struct GNUNET_SERVER_Client *client,
                         const struct GNUNET_MessageHeader *message)
 {
-  struct GNUNET_CADET_ChannelMessage *msg;
+  const struct GNUNET_CADET_LocalChannelDestroyMessage *msg;
   struct CadetClient *c;
   struct CadetChannel *ch;
-  CADET_ChannelNumber chid;
+  struct GNUNET_CADET_ClientChannelNumber ccn;
 
   LOG (GNUNET_ERROR_TYPE_DEBUG, "Got a DESTROY CHANNEL from client!\n");
 
@@ -423,22 +489,26 @@ handle_channel_destroy (void *cls, struct GNUNET_SERVER_Client *client,
   LOG (GNUNET_ERROR_TYPE_DEBUG, "  by client %u\n", c->id);
 
   /* Message sanity check */
-  if (sizeof (struct GNUNET_CADET_ChannelMessage) != ntohs (message->size))
+  if (sizeof (struct GNUNET_CADET_LocalChannelDestroyMessage)
+      != ntohs (message->size))
   {
     GNUNET_break (0);
     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
     return;
   }
 
-  msg = (struct GNUNET_CADET_ChannelMessage *) message;
+  msg = (const struct GNUNET_CADET_LocalChannelDestroyMessage *) message;
 
   /* Retrieve tunnel */
-  chid = ntohl (msg->channel_id);
-  LOG (GNUNET_ERROR_TYPE_DEBUG, "  for channel %X\n", chid);
-  ch = GML_channel_get (c, chid);
+  ccn = msg->ccn;
+  ch = GML_channel_get (c, ccn);
+
+  LOG (GNUNET_ERROR_TYPE_INFO, "Client %u is destroying channel %X\n",
+       c->id, ccn);
+
   if (NULL == ch)
   {
-    LOG (GNUNET_ERROR_TYPE_DEBUG, "  channel %X not found\n", chid);
+    LOG (GNUNET_ERROR_TYPE_WARNING, "  channel %X not found\n", ccn);
     GNUNET_STATISTICS_update (stats,
                               "# client destroy messages on unknown channel",
                               1, GNUNET_NO);
@@ -446,10 +516,11 @@ handle_channel_destroy (void *cls, struct GNUNET_SERVER_Client *client,
     return;
   }
 
-  GCCH_handle_local_destroy (ch, c, chid < GNUNET_CADET_LOCAL_CHANNEL_ID_SERV);
+  GCCH_handle_local_destroy (ch,
+                             c,
+                             ntohl (ccn.channel_of_client) < GNUNET_CADET_LOCAL_CHANNEL_ID_CLI);
 
   GNUNET_SERVER_receive_done (client, GNUNET_OK);
-  return;
 }
 
 
@@ -464,14 +535,19 @@ static void
 handle_data (void *cls, struct GNUNET_SERVER_Client *client,
              const struct GNUNET_MessageHeader *message)
 {
+  const struct GNUNET_MessageHeader *payload;
   struct GNUNET_CADET_LocalData *msg;
   struct CadetClient *c;
   struct CadetChannel *ch;
-  CADET_ChannelNumber chid;
-  size_t size;
+  struct GNUNET_CADET_ClientChannelNumber ccn;
+  size_t message_size;
+  size_t payload_size;
+  size_t payload_claimed_size;
   int fwd;
 
-  LOG (GNUNET_ERROR_TYPE_DEBUG, "Got data from a client!\n");
+  LOG (GNUNET_ERROR_TYPE_DEBUG, "\n");
+  LOG (GNUNET_ERROR_TYPE_DEBUG, "\n");
+  LOG (GNUNET_ERROR_TYPE_DEBUG, "Got data from a client\n");
 
   /* Sanity check for client registration */
   if (NULL == (c = GML_client_get (client)))
@@ -480,24 +556,42 @@ handle_data (void *cls, struct GNUNET_SERVER_Client *client,
     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
     return;
   }
-  LOG (GNUNET_ERROR_TYPE_DEBUG, "  by client %u\n", c->id);
-
-  msg = (struct GNUNET_CADET_LocalData *) message;
 
   /* Sanity check for message size */
-  size = ntohs (message->size) - sizeof (struct GNUNET_CADET_LocalData);
-  if (size < sizeof (struct GNUNET_MessageHeader))
+  message_size = ntohs (message->size);
+  if (sizeof (struct GNUNET_CADET_LocalData)
+      + sizeof (struct GNUNET_MessageHeader) > message_size
+      || GNUNET_CONSTANTS_MAX_CADET_MESSAGE_SIZE < message_size)
   {
     GNUNET_break (0);
     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
     return;
   }
 
+  /* Sanity check for payload size */
+  payload_size = message_size - sizeof (struct GNUNET_CADET_LocalData);
+  msg = (struct GNUNET_CADET_LocalData *) message;
+  payload = (struct GNUNET_MessageHeader *) &msg[1];
+  payload_claimed_size = ntohs (payload->size);
+  if (sizeof (struct GNUNET_MessageHeader) > payload_claimed_size
+      || GNUNET_CONSTANTS_MAX_CADET_MESSAGE_SIZE < payload_claimed_size
+      || payload_claimed_size > payload_size)
+  {
+    LOG (GNUNET_ERROR_TYPE_WARNING,
+         "client claims to send %u bytes in %u payload\n",
+         payload_claimed_size, payload_size);
+    GNUNET_break (0);
+    GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
+    return;
+  }
+
+  ccn = msg->ccn;
+  LOG (GNUNET_ERROR_TYPE_DEBUG, "  %u bytes (%u payload) by client %u\n",
+       payload_size, payload_claimed_size, c->id);
+
   /* Channel exists? */
-  chid = ntohl (msg->id);
-  LOG (GNUNET_ERROR_TYPE_DEBUG, "  on channel %X\n", chid);
-  fwd = chid < GNUNET_CADET_LOCAL_CHANNEL_ID_SERV;
-  ch = GML_channel_get (c, chid);
+  fwd = ntohl (ccn.channel_of_client) >= GNUNET_CADET_LOCAL_CHANNEL_ID_CLI;
+  ch = GML_channel_get (c, ccn);
   if (NULL == ch)
   {
     GNUNET_STATISTICS_update (stats,
@@ -507,9 +601,7 @@ handle_data (void *cls, struct GNUNET_SERVER_Client *client,
     return;
   }
 
-  if (GNUNET_OK !=
-      GCCH_handle_local_data (ch, c,
-                              (struct GNUNET_MessageHeader *)&msg[1], fwd))
+  if (GNUNET_OK != GCCH_handle_local_data (ch, c, fwd, payload, payload_size))
   {
     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
     return;
@@ -536,7 +628,7 @@ handle_ack (void *cls, struct GNUNET_SERVER_Client *client,
   struct GNUNET_CADET_LocalAck *msg;
   struct CadetChannel *ch;
   struct CadetClient *c;
-  CADET_ChannelNumber chid;
+  struct GNUNET_CADET_ClientChannelNumber ccn;
   int fwd;
 
   LOG (GNUNET_ERROR_TYPE_DEBUG, "\n");
@@ -554,13 +646,16 @@ handle_ack (void *cls, struct GNUNET_SERVER_Client *client,
   msg = (struct GNUNET_CADET_LocalAck *) message;
 
   /* Channel exists? */
-  chid = ntohl (msg->channel_id);
-  LOG (GNUNET_ERROR_TYPE_DEBUG, "  on channel %X\n", chid);
-  ch = GML_channel_get (c, chid);
+  ccn = msg->ccn;
+  LOG (GNUNET_ERROR_TYPE_DEBUG, "  on channel %X\n",
+       ntohl (ccn.channel_of_client));
+  ch = GML_channel_get (c, ccn);
   LOG (GNUNET_ERROR_TYPE_DEBUG, "   -- ch %p\n", ch);
   if (NULL == ch)
   {
-    LOG (GNUNET_ERROR_TYPE_DEBUG, "Channel %X unknown.\n", chid);
+    LOG (GNUNET_ERROR_TYPE_DEBUG,
+         "Channel %X unknown.\n",
+         ntohl (ccn.channel_of_client));
     LOG (GNUNET_ERROR_TYPE_DEBUG, "  for client %u.\n", c->id);
     GNUNET_STATISTICS_update (stats,
                               "# client ack messages on unknown channel",
@@ -571,12 +666,10 @@ handle_ack (void *cls, struct GNUNET_SERVER_Client *client,
 
   /* If client is root, the ACK is going FWD, therefore this is "BCK ACK". */
   /* If client is dest, the ACK is going BCK, therefore this is "FWD ACK" */
-  fwd = chid >= GNUNET_CADET_LOCAL_CHANNEL_ID_SERV;
+  fwd = ntohl (ccn.channel_of_client) < GNUNET_CADET_LOCAL_CHANNEL_ID_CLI;
 
   GCCH_handle_local_ack (ch, fwd);
   GNUNET_SERVER_receive_done (client, GNUNET_OK);
-
-  return;
 }
 
 
@@ -630,9 +723,6 @@ show_peer_iterator (void *cls,
   struct CadetPeer *p = value;
   struct CadetTunnel *t;
 
-  LOG (GNUNET_ERROR_TYPE_ERROR, "Peer %s\n", GCP_2s (p));
-  LOG (GNUNET_ERROR_TYPE_ERROR, " %u paths\n", GCP_count_paths (p));
-
   t = GCP_get_tunnel (p);
   if (NULL != t)
     GCT_debug (t, GNUNET_ERROR_TYPE_ERROR);
@@ -643,6 +733,61 @@ show_peer_iterator (void *cls,
 }
 
 
+/**
+ * Iterator over all paths of a peer to build an InfoPeer message.
+ *
+ * Message contains blocks of peers, first not included.
+ *
+ * @param cls Closure (message to build).
+ * @param peer Peer this path is towards.
+ * @param path Path itself
+ * @return #GNUNET_YES if should keep iterating.
+ *         #GNUNET_NO otherwise.
+ */
+static int
+path_info_iterator (void *cls,
+                    struct CadetPeer *peer,
+                    struct CadetPeerPath *path)
+{
+  struct GNUNET_CADET_LocalInfoPeer *resp = cls;
+  struct GNUNET_PeerIdentity *id;
+  uint16_t msg_size;
+  uint16_t path_size;
+  unsigned int i;
+
+  msg_size = ntohs (resp->header.size);
+  path_size = sizeof (struct GNUNET_PeerIdentity) * (path->length - 1);
+
+  LOG (GNUNET_ERROR_TYPE_DEBUG, "Info Path %u\n", path->length);
+  if (msg_size + path_size > UINT16_MAX)
+  {
+    LOG (GNUNET_ERROR_TYPE_WARNING, "path too long for info message\n");
+    return GNUNET_NO;
+  }
+
+  i = msg_size - sizeof (struct GNUNET_CADET_LocalInfoPeer);
+  i = i / sizeof (struct GNUNET_PeerIdentity);
+
+  /* Set id to the address of the first free peer slot. */
+  id = (struct GNUNET_PeerIdentity *) &resp[1];
+  id = &id[i];
+
+  /* Don't copy first peers.
+   * First peer is always the local one.
+   * Last peer is always the destination (leave as 0, EOL).
+   */
+  for (i = 0; i < path->length - 1; i++)
+  {
+    GNUNET_PEER_resolve (path->peers[i + 1], &id[i]);
+    LOG (GNUNET_ERROR_TYPE_DEBUG, " %s\n", GNUNET_i2s (&id[i]));
+  }
+
+  resp->header.size = htons (msg_size + path_size);
+
+  return GNUNET_YES;
+}
+
+
 /**
  * Handler for client's INFO PEERS request.
  *
@@ -680,6 +825,69 @@ handle_get_peers (void *cls, struct GNUNET_SERVER_Client *client,
 }
 
 
+/**
+ * Handler for client's SHOW_PEER request.
+ *
+ * @param cls Closure (unused).
+ * @param client Identification of the client.
+ * @param message The actual message.
+ */
+void
+handle_show_peer (void *cls, struct GNUNET_SERVER_Client *client,
+                  const struct GNUNET_MessageHeader *message)
+{
+  const struct GNUNET_CADET_LocalInfo *msg;
+  struct GNUNET_CADET_LocalInfoPeer *resp;
+  struct CadetPeer *p;
+  struct CadetClient *c;
+  unsigned char cbuf[64 * 1024];
+
+  /* Sanity check for client registration */
+  if (NULL == (c = GML_client_get (client)))
+  {
+    GNUNET_break (0);
+    GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
+    return;
+  }
+
+  msg = (struct GNUNET_CADET_LocalInfo *) message;
+  resp = (struct GNUNET_CADET_LocalInfoPeer *) cbuf;
+  LOG (GNUNET_ERROR_TYPE_INFO,
+       "Received peer info request from client %u for peer %s\n",
+       c->id, GNUNET_i2s_full (&msg->peer));
+
+  resp->header.type = htons (GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEER);
+  resp->header.size = htons (sizeof (struct GNUNET_CADET_LocalInfoPeer));
+  resp->destination = msg->peer;
+  p = GCP_get (&msg->peer, GNUNET_NO);
+  if (NULL == p)
+  {
+    /* We don't know the peer */
+
+    LOG (GNUNET_ERROR_TYPE_INFO, "Peer %s unknown\n",
+         GNUNET_i2s_full (&msg->peer));
+    resp->paths = htons (0);
+    resp->tunnel = htons (NULL != GCP_get_tunnel (p));
+
+    GNUNET_SERVER_notification_context_unicast (nc, client,
+                                                &resp->header,
+                                                GNUNET_NO);
+    GNUNET_SERVER_receive_done (client, GNUNET_OK);
+    return;
+  }
+
+  resp->paths = htons (GCP_count_paths (p));
+  resp->tunnel = htons (NULL != GCP_get_tunnel (p));
+  GCP_iterate_paths (p, &path_info_iterator, resp);
+
+  GNUNET_SERVER_notification_context_unicast (nc, c->handle,
+                                              &resp->header, GNUNET_NO);
+
+  LOG (GNUNET_ERROR_TYPE_INFO, "Show peer from client %u completed.\n", c->id);
+  GNUNET_SERVER_receive_done (client, GNUNET_OK);
+}
+
+
 /**
  * Iterator over all tunnels to send a monitoring client info about each tunnel.
  *
@@ -702,7 +910,7 @@ get_all_tunnels_iterator (void *cls,
   msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNELS);
   msg.destination = *peer;
   msg.channels = htonl (GCT_count_channels (t));
-  msg.connections = htonl (GCT_count_connections (t));
+  msg.connections = htonl (GCT_count_any_connections (t));
   msg.cstate = htons ((uint16_t) GCT_get_cstate (t));
   msg.estate = htons ((uint16_t) GCT_get_estate (t));
 
@@ -756,8 +964,9 @@ static void
 iter_connection (void *cls, struct CadetConnection *c)
 {
   struct GNUNET_CADET_LocalInfoTunnel *msg = cls;
-  struct GNUNET_CADET_Hash *h = (struct GNUNET_CADET_Hash *) &msg[1];
+  struct GNUNET_CADET_ConnectionTunnelIdentifier *h;
 
+  h = (struct GNUNET_CADET_ConnectionTunnelIdentifier *) &msg[1];
   h[msg->connections] = *(GCC_get_id (c));
   msg->connections++;
 }
@@ -766,8 +975,8 @@ static void
 iter_channel (void *cls, struct CadetChannel *ch)
 {
   struct GNUNET_CADET_LocalInfoTunnel *msg = cls;
-  struct GNUNET_HashCode *h = (struct GNUNET_HashCode *) &msg[1];
-  CADET_ChannelNumber *chn = (CADET_ChannelNumber *) &h[msg->connections];
+  struct GNUNET_CADET_ConnectionTunnelIdentifier *h = (struct GNUNET_CADET_ConnectionTunnelIdentifier *) &msg[1];
+  struct GNUNET_CADET_ChannelTunnelNumber *chn = (struct GNUNET_CADET_ChannelTunnelNumber *) &h[msg->connections];
 
   chn[msg->channels] = GCCH_get_id (ch);
   msg->channels++;
@@ -802,11 +1011,11 @@ handle_show_tunnel (void *cls, struct GNUNET_SERVER_Client *client,
   }
 
   msg = (struct GNUNET_CADET_LocalInfo *) message;
-  LOG (GNUNET_ERROR_TYPE_INFO,
+  LOG (GNUNET_ERROR_TYPE_DEBUG,
        "Received tunnel info request from client %u for tunnel %s\n",
        c->id, GNUNET_i2s_full(&msg->peer));
 
-  t = GCP_get_tunnel (GCP_get (&msg->peer));
+  t = GCP_get_tunnel (GCP_get (&msg->peer, GNUNET_NO));
   if (NULL == t)
   {
     /* We don't know the tunnel */
@@ -831,28 +1040,29 @@ handle_show_tunnel (void *cls, struct GNUNET_SERVER_Client *client,
 
   /* Initialize context */
   ch_n = GCT_count_channels (t);
-  c_n = GCT_count_connections (t);
+  c_n = GCT_count_any_connections (t);
 
   size = sizeof (struct GNUNET_CADET_LocalInfoTunnel);
-  size += c_n * sizeof (struct GNUNET_CADET_Hash);
-  size += ch_n * sizeof (CADET_ChannelNumber);
+  size += c_n * sizeof (struct GNUNET_CADET_ConnectionTunnelIdentifier);
+  size += ch_n * sizeof (struct GNUNET_CADET_ChannelTunnelNumber);
 
   resp = GNUNET_malloc (size);
   resp->header.type = htons (GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNEL);
   resp->header.size = htons (size);
+  resp->destination = msg->peer;
+  /* Do not interleave with iterators, iter_channel needs conn in HBO */
   GCT_iterate_connections (t, &iter_connection, resp);
   GCT_iterate_channels (t, &iter_channel, resp);
-  /* Do not interleave with iterators, iter_channel needs conn in HBO */
-  resp->destination = msg->peer;
   resp->connections = htonl (resp->connections);
   resp->channels = htonl (resp->channels);
+  /* Do not interleave end */
   resp->cstate = htons (GCT_get_cstate (t));
   resp->estate = htons (GCT_get_estate (t));
   GNUNET_SERVER_notification_context_unicast (nc, c->handle,
                                               &resp->header, GNUNET_NO);
   GNUNET_free (resp);
 
-  LOG (GNUNET_ERROR_TYPE_INFO,
+  LOG (GNUNET_ERROR_TYPE_DEBUG,
        "Show tunnel request from client %u completed. %u conn, %u ch\n",
        c->id, c_n, ch_n);
   GNUNET_SERVER_receive_done (client, GNUNET_OK);
@@ -882,10 +1092,24 @@ handle_info_dump (void *cls, struct GNUNET_SERVER_Client *client,
 
   LOG (GNUNET_ERROR_TYPE_INFO, "Received dump info request from client %u\n",
        c->id);
-
   LOG (GNUNET_ERROR_TYPE_ERROR,
        "*************************** DUMP START ***************************\n");
 
+  for (c = clients_head; NULL != c; c = c->next)
+  {
+    LOG (GNUNET_ERROR_TYPE_ERROR, "Client %u (%p), handle: %p\n",
+         c->id, c, c->handle);
+    if (NULL != c->ports)
+      LOG (GNUNET_ERROR_TYPE_ERROR, "\t%3u ports registered\n",
+           GNUNET_CONTAINER_multihashmap_size (c->ports));
+    else
+      LOG (GNUNET_ERROR_TYPE_ERROR, "\t no ports registered\n");
+    LOG (GNUNET_ERROR_TYPE_ERROR, "\t%3u own channles\n",
+         GNUNET_CONTAINER_multihashmap32_size (c->own_channels));
+    LOG (GNUNET_ERROR_TYPE_ERROR, "\t%3u incoming channles\n",
+         GNUNET_CONTAINER_multihashmap32_size (c->incoming_channels));
+  }
+  LOG (GNUNET_ERROR_TYPE_ERROR, "***************************\n");
   GCP_iterate_all (&show_peer_iterator, NULL);
 
   LOG (GNUNET_ERROR_TYPE_ERROR,
@@ -899,16 +1123,21 @@ handle_info_dump (void *cls, struct GNUNET_SERVER_Client *client,
  * Functions to handle messages from clients
  */
 static struct GNUNET_SERVER_MessageHandler client_handlers[] = {
-  {&handle_new_client, NULL, GNUNET_MESSAGE_TYPE_CADET_LOCAL_CONNECT, 0},
-  {&handle_channel_create, NULL, GNUNET_MESSAGE_TYPE_CADET_CHANNEL_CREATE,
-   sizeof (struct GNUNET_CADET_ChannelMessage)},
-  {&handle_channel_destroy, NULL, GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DESTROY,
-   sizeof (struct GNUNET_CADET_ChannelMessage)},
+  {&handle_port_open, NULL, GNUNET_MESSAGE_TYPE_CADET_LOCAL_PORT_OPEN,
+    sizeof (struct GNUNET_CADET_PortMessage)},
+  {&handle_port_close, NULL, GNUNET_MESSAGE_TYPE_CADET_LOCAL_PORT_CLOSE,
+    sizeof (struct GNUNET_CADET_PortMessage)},
+  {&handle_channel_create, NULL, GNUNET_MESSAGE_TYPE_CADET_LOCAL_CHANNEL_CREATE,
+   sizeof (struct GNUNET_CADET_LocalChannelCreateMessage)},
+  {&handle_channel_destroy, NULL, GNUNET_MESSAGE_TYPE_CADET_LOCAL_CHANNEL_DESTROY,
+   sizeof (struct GNUNET_CADET_LocalChannelDestroyMessage)},
   {&handle_data, NULL, GNUNET_MESSAGE_TYPE_CADET_LOCAL_DATA, 0},
   {&handle_ack, NULL, GNUNET_MESSAGE_TYPE_CADET_LOCAL_ACK,
    sizeof (struct GNUNET_CADET_LocalAck)},
   {&handle_get_peers, NULL, GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEERS,
    sizeof (struct GNUNET_MessageHeader)},
+  {&handle_show_peer, NULL, GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEER,
+   sizeof (struct GNUNET_CADET_LocalInfo)},
   {&handle_get_tunnels, NULL, GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNELS,
    sizeof (struct GNUNET_MessageHeader)},
   {&handle_show_tunnel, NULL, GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNEL,
@@ -935,7 +1164,7 @@ GML_init (struct GNUNET_SERVER_Handle *handle)
   LOG (GNUNET_ERROR_TYPE_DEBUG, "init\n");
   server_handle = handle;
   GNUNET_SERVER_suspend (server_handle);
-  ports = GNUNET_CONTAINER_multihashmap32_create (32);
+  ports = GNUNET_CONTAINER_multihashmap_create (16, GNUNET_NO);
 }
 
 
@@ -964,11 +1193,19 @@ GML_start (void)
 void
 GML_shutdown (void)
 {
+  struct CadetClient *c;
+
+  LOG (GNUNET_ERROR_TYPE_DEBUG, "Shutting down local\n");
+
+  for (c = clients_head; NULL != clients_head; c = clients_head)
+    client_destroy (c);
+
   if (nc != NULL)
   {
     GNUNET_SERVER_notification_context_destroy (nc);
     nc = NULL;
   }
+
 }
 
 
@@ -976,40 +1213,31 @@ GML_shutdown (void)
  * Get a channel from a client.
  *
  * @param c Client to check.
- * @param chid Channel ID, must be local (> 0x800...).
+ * @param ccn Channel ID, must be local (> 0x800...).
  *
  * @return non-NULL if channel exists in the clients lists
  */
 struct CadetChannel *
-GML_channel_get (struct CadetClient *c, CADET_ChannelNumber chid)
+GML_channel_get (struct CadetClient *c,
+                 struct GNUNET_CADET_ClientChannelNumber ccn)
 {
   struct GNUNET_CONTAINER_MultiHashMap32 *map;
 
-  if (0 == (chid & GNUNET_CADET_LOCAL_CHANNEL_ID_CLI))
-  {
-    GNUNET_break_op (0);
-    LOG (GNUNET_ERROR_TYPE_DEBUG, "CHID %X not a local chid\n", chid);
-    return NULL;
-  }
-
-  if (chid >= GNUNET_CADET_LOCAL_CHANNEL_ID_SERV)
-    map = c->incoming_channels;
-  else if (chid >= GNUNET_CADET_LOCAL_CHANNEL_ID_CLI)
+  if (ntohl (ccn.channel_of_client) >= GNUNET_CADET_LOCAL_CHANNEL_ID_CLI)
     map = c->own_channels;
   else
-  {
-    GNUNET_break (0);
-    map = NULL;
-  }
+    map = c->incoming_channels;
+
   if (NULL == map)
   {
     GNUNET_break (0);
     LOG (GNUNET_ERROR_TYPE_DEBUG,
-         "Client %s does no t have a valid map for CHID %X\n",
-         GML_2s (c), chid);
+         "Client %s does no t have a valid map for CCN %X\n",
+         GML_2s (c), ccn);
     return NULL;
   }
-  return GNUNET_CONTAINER_multihashmap32_get (map, chid);
+  return GNUNET_CONTAINER_multihashmap32_get (map,
+                                              ccn.channel_of_client);
 }
 
 
@@ -1017,22 +1245,24 @@ GML_channel_get (struct CadetClient *c, CADET_ChannelNumber chid)
  * Add a channel to a client
  *
  * @param client Client.
- * @param chid Channel ID.
+ * @param ccn Channel ID.
  * @param ch Channel.
  */
 void
 GML_channel_add (struct CadetClient *client,
-                 uint32_t chid,
+                 struct GNUNET_CADET_ClientChannelNumber ccn,
                  struct CadetChannel *ch)
 {
-  if (chid >= GNUNET_CADET_LOCAL_CHANNEL_ID_SERV)
-    GNUNET_CONTAINER_multihashmap32_put (client->incoming_channels, chid, ch,
-                                         GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
-  else if (chid >= GNUNET_CADET_LOCAL_CHANNEL_ID_CLI)
-    GNUNET_CONTAINER_multihashmap32_put (client->own_channels, chid, ch,
+  if (ntohl (ccn.channel_of_client) >= GNUNET_CADET_LOCAL_CHANNEL_ID_CLI)
+    GNUNET_CONTAINER_multihashmap32_put (client->own_channels,
+                                         ccn.channel_of_client,
+                                         ch,
                                          GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
   else
-    GNUNET_break (0);
+    GNUNET_CONTAINER_multihashmap32_put (client->incoming_channels,
+                                         ccn.channel_of_client,
+                                         ch,
+                                         GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
 }
 
 
@@ -1040,24 +1270,22 @@ GML_channel_add (struct CadetClient *client,
  * Remove a channel from a client.
  *
  * @param client Client.
- * @param chid Channel ID.
+ * @param ccn Channel ID.
  * @param ch Channel.
  */
 void
 GML_channel_remove (struct CadetClient *client,
-                    uint32_t chid,
+                    struct GNUNET_CADET_ClientChannelNumber ccn,
                     struct CadetChannel *ch)
 {
-  if (GNUNET_CADET_LOCAL_CHANNEL_ID_SERV <= chid)
-    GNUNET_break (GNUNET_YES ==
-                  GNUNET_CONTAINER_multihashmap32_remove (client->incoming_channels,
-                                                          chid, ch));
-  else if (GNUNET_CADET_LOCAL_CHANNEL_ID_CLI <= chid)
-    GNUNET_break (GNUNET_YES ==
-                  GNUNET_CONTAINER_multihashmap32_remove (client->own_channels,
-                                                          chid, ch));
+  if (ntohl (ccn.channel_of_client) >= GNUNET_CADET_LOCAL_CHANNEL_ID_CLI)
+    GNUNET_CONTAINER_multihashmap32_remove (client->own_channels,
+                                            ccn.channel_of_client,
+                                            ch);
   else
-    GNUNET_break (0);
+    GNUNET_CONTAINER_multihashmap32_remove (client->incoming_channels,
+                                            ccn.channel_of_client,
+                                            ch);
 }
 
 
@@ -1068,20 +1296,28 @@ GML_channel_remove (struct CadetClient *client,
  *
  * @return LID of a channel free to use.
  */
-CADET_ChannelNumber
-GML_get_next_chid (struct CadetClient *c)
+struct GNUNET_CADET_ClientChannelNumber
+GML_get_next_ccn (struct CadetClient *c)
 {
-  CADET_ChannelNumber chid;
+  struct GNUNET_CADET_ClientChannelNumber ccn;
 
-  while (NULL != GML_channel_get (c, c->next_chid))
+  while (NULL != GML_channel_get (c,
+                                  c->next_ccn))
   {
-    LOG (GNUNET_ERROR_TYPE_DEBUG, "Channel %u exists...\n", c->next_chid);
-    c->next_chid = (c->next_chid + 1) | GNUNET_CADET_LOCAL_CHANNEL_ID_SERV;
+    LOG (GNUNET_ERROR_TYPE_DEBUG,
+         "Channel %u exists...\n",
+         c->next_ccn);
+    c->next_ccn.channel_of_client
+      = htonl (1 + (ntohl (c->next_ccn.channel_of_client)));
+    if (ntohl (c->next_ccn.channel_of_client) >=
+        GNUNET_CADET_LOCAL_CHANNEL_ID_CLI)
+      c->next_ccn.channel_of_client = htonl (0);
   }
-  chid = c->next_chid;
-  c->next_chid = (c->next_chid + 1) | GNUNET_CADET_LOCAL_CHANNEL_ID_SERV;
+  ccn = c->next_ccn;
+  c->next_ccn.channel_of_client
+    = htonl (1 + (ntohl (c->next_ccn.channel_of_client)));
 
-  return chid;
+  return ccn;
 }
 
 
@@ -1095,9 +1331,13 @@ GML_get_next_chid (struct CadetClient *c)
 struct CadetClient *
 GML_client_get (struct GNUNET_SERVER_Client *client)
 {
-  return GNUNET_SERVER_client_get_user_context (client, struct CadetClient);
+  if (NULL == client)
+    return NULL;
+  return GNUNET_SERVER_client_get_user_context (client,
+                                                struct CadetClient);
 }
 
+
 /**
  * Find a client that has opened a port
  *
@@ -1106,9 +1346,9 @@ GML_client_get (struct GNUNET_SERVER_Client *client)
  * @return non-NULL if a client has the port.
  */
 struct CadetClient *
-GML_client_get_by_port (uint32_t port)
+GML_client_get_by_port (const struct GNUNET_HashCode *port)
 {
-  return GNUNET_CONTAINER_multihashmap32_get (ports, port);
+  return GNUNET_CONTAINER_multihashmap_get (ports, port);
 }
 
 
@@ -1122,27 +1362,25 @@ GML_client_get_by_port (uint32_t port)
 void
 GML_client_delete_channel (struct CadetClient *c,
                            struct CadetChannel *ch,
-                           CADET_ChannelNumber id)
+                           struct GNUNET_CADET_ClientChannelNumber id)
 {
   int res;
 
-  if (GNUNET_CADET_LOCAL_CHANNEL_ID_SERV <= id)
-  {
-    res = GNUNET_CONTAINER_multihashmap32_remove (c->incoming_channels,
-                                                  id, ch);
-    if (GNUNET_YES != res)
-      LOG (GNUNET_ERROR_TYPE_DEBUG, "client_delete_channel dest KO\n");
-  }
-  else if (GNUNET_CADET_LOCAL_CHANNEL_ID_CLI <= id)
+  if (ntohl (id.channel_of_client) >= GNUNET_CADET_LOCAL_CHANNEL_ID_CLI)
   {
     res = GNUNET_CONTAINER_multihashmap32_remove (c->own_channels,
-                                                  id, ch);
+                                                  id.channel_of_client,
+                                                  ch);
     if (GNUNET_YES != res)
       LOG (GNUNET_ERROR_TYPE_DEBUG, "client_delete_tunnel root KO\n");
   }
   else
   {
-    GNUNET_break (0);
+    res = GNUNET_CONTAINER_multihashmap32_remove (c->incoming_channels,
+                                                  id.channel_of_client,
+                                                  ch);
+    if (GNUNET_YES != res)
+      LOG (GNUNET_ERROR_TYPE_DEBUG, "client_delete_channel dest KO\n");
   }
 }
 
@@ -1152,20 +1390,24 @@ GML_client_delete_channel (struct CadetClient *c,
  * If the client was already allowed to send data, do nothing.
  *
  * @param c Client to whom send the ACK.
- * @param id Channel ID to use
+ * @param ccn Channel ID to use
  */
 void
-GML_send_ack (struct CadetClient *c, CADET_ChannelNumber id)
+GML_send_ack (struct CadetClient *c,
+              struct GNUNET_CADET_ClientChannelNumber ccn)
 {
   struct GNUNET_CADET_LocalAck msg;
 
   LOG (GNUNET_ERROR_TYPE_DEBUG,
-              "send local %s ack on %X towards %p\n",
-              id < GNUNET_CADET_LOCAL_CHANNEL_ID_SERV ? "FWD" : "BCK", id, c);
+       "send local %s ack on %X towards %p\n",
+       ntohl (ccn.channel_of_client) < GNUNET_CADET_LOCAL_CHANNEL_ID_CLI
+       ? "FWD" : "BCK",
+       ntohl (ccn.channel_of_client),
+       c);
 
   msg.header.size = htons (sizeof (msg));
   msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_LOCAL_ACK);
-  msg.channel_id = htonl (id);
+  msg.ccn = ccn;
   GNUNET_SERVER_notification_context_unicast (nc,
                                               c->handle,
                                               &msg.header,
@@ -1179,22 +1421,24 @@ GML_send_ack (struct CadetClient *c, CADET_ChannelNumber id)
  * Notify the client that a new incoming channel was created.
  *
  * @param c Client to notify.
- * @param id Channel ID.
+ * @param ccn Channel ID.
  * @param port Channel's destination port.
  * @param opt Options (bit array).
  * @param peer Origin peer.
  */
 void
 GML_send_channel_create (struct CadetClient *c,
-                         uint32_t id, uint32_t port, uint32_t opt,
+                         struct GNUNET_CADET_ClientChannelNumber ccn,
+                         const struct GNUNET_HashCode *port,
+                         uint32_t opt,
                          const struct GNUNET_PeerIdentity *peer)
 {
-  struct GNUNET_CADET_ChannelMessage msg;
+  struct GNUNET_CADET_LocalChannelCreateMessage msg;
 
   msg.header.size = htons (sizeof (msg));
-  msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CHANNEL_CREATE);
-  msg.channel_id = htonl (id);
-  msg.port = htonl (port);
+  msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_LOCAL_CHANNEL_CREATE);
+  msg.ccn = ccn;
+  msg.port = *port;
   msg.opt = htonl (opt);
   msg.peer = *peer;
   GNUNET_SERVER_notification_context_unicast (nc, c->handle,
@@ -1206,20 +1450,22 @@ GML_send_channel_create (struct CadetClient *c,
  * Build a local channel NACK message and send it to a local client.
  *
  * @param c Client to whom send the NACK.
- * @param id Channel ID to use
+ * @param ccn Channel ID to use
  */
 void
-GML_send_channel_nack (struct CadetClient *c, CADET_ChannelNumber id)
+GML_send_channel_nack (struct CadetClient *c,
+                       struct GNUNET_CADET_ClientChannelNumber ccn)
 {
   struct GNUNET_CADET_LocalAck msg;
 
   LOG (GNUNET_ERROR_TYPE_DEBUG,
        "send local nack on %X towards %p\n",
-       id, c);
+       ntohl (ccn.channel_of_client),
+       c);
 
   msg.header.size = htons (sizeof (msg));
-  msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CHANNEL_NACK);
-  msg.channel_id = htonl (id);
+  msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CHANNEL_OPEN_NACK_DEPRECATED);
+  msg.ccn = ccn;
   GNUNET_SERVER_notification_context_unicast (nc,
                                               c->handle,
                                               &msg.header,
@@ -1231,12 +1477,13 @@ GML_send_channel_nack (struct CadetClient *c, CADET_ChannelNumber id)
  * Notify a client that a channel is no longer valid.
  *
  * @param c Client.
- * @param id ID of the channel that is destroyed.
+ * @param ccn ID of the channel that is destroyed.
  */
 void
-GML_send_channel_destroy (struct CadetClient *c, uint32_t id)
+GML_send_channel_destroy (struct CadetClient *c,
+                          struct GNUNET_CADET_ClientChannelNumber ccn)
 {
-  struct GNUNET_CADET_ChannelMessage msg;
+  struct GNUNET_CADET_LocalChannelDestroyMessage msg;
 
   if (NULL == c)
   {
@@ -1246,11 +1493,8 @@ GML_send_channel_destroy (struct CadetClient *c, uint32_t id)
   if (GNUNET_YES == c->shutting_down)
     return;
   msg.header.size = htons (sizeof (msg));
-  msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DESTROY);
-  msg.channel_id = htonl (id);
-  msg.port = htonl (0);
-  memset (&msg.peer, 0, sizeof (msg.peer));
-  msg.opt = htonl (0);
+  msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_LOCAL_CHANNEL_DESTROY);
+  msg.ccn = ccn;
   GNUNET_SERVER_notification_context_unicast (nc, c->handle,
                                               &msg.header, GNUNET_NO);
 }
@@ -1261,15 +1505,15 @@ GML_send_channel_destroy (struct CadetClient *c, uint32_t id)
  *
  * @param c Client to send to.
  * @param msg Message to modify and send.
- * @param id Channel ID to use (c can be both owner and client).
+ * @param ccn Channel ID to use (c can be both owner and client).
  */
 void
 GML_send_data (struct CadetClient *c,
-               const struct GNUNET_CADET_Data *msg,
-               CADET_ChannelNumber id)
+               const struct GNUNET_CADET_ChannelAppDataMessage *msg,
+               struct GNUNET_CADET_ClientChannelNumber ccn)
 {
   struct GNUNET_CADET_LocalData *copy;
-  uint16_t size = ntohs (msg->header.size) - sizeof (struct GNUNET_CADET_Data);
+  uint16_t size = ntohs (msg->header.size) - sizeof (struct GNUNET_CADET_ChannelAppDataMessage);
   char cbuf[size + sizeof (struct GNUNET_CADET_LocalData)];
 
   if (size < sizeof (struct GNUNET_MessageHeader))
@@ -1283,10 +1527,10 @@ GML_send_data (struct CadetClient *c,
     return;
   }
   copy = (struct GNUNET_CADET_LocalData *) cbuf;
-  memcpy (&copy[1], &msg[1], size);
+  GNUNET_memcpy (&copy[1], &msg[1], size);
   copy->header.size = htons (sizeof (struct GNUNET_CADET_LocalData) + size);
   copy->header.type = htons (GNUNET_MESSAGE_TYPE_CADET_LOCAL_DATA);
-  copy->id = htonl (id);
+  copy->ccn = ccn;
   GNUNET_SERVER_notification_context_unicast (nc, c->handle,
                                               &copy->header, GNUNET_NO);
 }
@@ -1304,6 +1548,6 @@ GML_2s (const struct CadetClient *c)
 {
   static char buf[32];
 
-  sprintf (buf, "%u", c->id);
+  SPRINTF (buf, "%u", c->id);
   return buf;
 }