- use variables to cound ch, conns
[oweals/gnunet.git] / src / mesh / mesh_api.c
index 9b18df8dda99670c55f17a236d8539a8227e05ea..0ff46f01c6be553b397a879d01596fda7b5a11b7 100644 (file)
@@ -203,6 +203,36 @@ struct GNUNET_MESH_Handle
    * Channel callback closure.
    */
   void *channel_cls;
+
+  /**
+   * Monitor callback
+   */
+  GNUNET_MESH_PeersCB peers_cb;
+
+  /**
+   * Monitor callback closure.
+   */
+  void *peers_cls;
+
+  /**
+   * Monitor callback
+   */
+  GNUNET_MESH_TunnelsCB tunnels_cb;
+
+  /**
+   * Monitor callback closure.
+   */
+  void *tunnels_cls;
+
+  /**
+   * Tunnel callback.
+   */
+  GNUNET_MESH_TunnelCB tunnel_cb;
+
+  /**
+   * Tunnel callback closure.
+   */
+  void *tunnel_cls;
 };
 
 
@@ -220,13 +250,6 @@ struct GNUNET_MESH_Peer
    * Channel this peer belongs to
    */
   struct GNUNET_MESH_Channel *t;
-
-  /**
-   * Flag indicating whether service has informed about its connection
-   * FIXME-BART: is this flag used? Seems dead right now...
-   */
-  int connected;
-
 };
 
 
@@ -277,19 +300,9 @@ struct GNUNET_MESH_Channel
   unsigned int packet_size;
 
     /**
-     * Is the channel allowed to buffer?
+     * Channel options: reliability, etc.
      */
-  int nobuffer;
-
-    /**
-     * Is the channel realiable?
-     */
-  int reliable;
-
-    /**
-     * If reliable, is the channel out of order?
-     */
-  int ooorder;
+  enum GNUNET_MESH_ChannelOption options;
 
     /**
      * Are we allowed to send to the service?
@@ -421,7 +434,7 @@ create_channel (struct GNUNET_MESH_Handle *h, MESH_ChannelNumber chid)
 {
   struct GNUNET_MESH_Channel *ch;
 
-  ch = GNUNET_malloc (sizeof (struct GNUNET_MESH_Channel));
+  ch = GNUNET_new (struct GNUNET_MESH_Channel);
   GNUNET_CONTAINER_DLL_insert (h->channels_head, h->channels_tail, ch);
   ch->mesh = h;
   if (0 == chid)
@@ -439,7 +452,6 @@ create_channel (struct GNUNET_MESH_Handle *h, MESH_ChannelNumber chid)
     ch->chid = chid;
   }
   ch->allow_send = GNUNET_NO;
-  ch->nobuffer = GNUNET_NO;
   return ch;
 }
 
@@ -463,7 +475,7 @@ destroy_channel (struct GNUNET_MESH_Channel *ch, int call_cleaner)
   struct GNUNET_MESH_TransmitHandle *th;
   struct GNUNET_MESH_TransmitHandle *next;
 
-  LOG (GNUNET_ERROR_TYPE_DEBUG, "destroy_channel %X\n", ch->chid);
+  LOG (GNUNET_ERROR_TYPE_DEBUG, " destroy_channel %X\n", ch->chid);
 
   if (NULL == ch)
   {
@@ -476,7 +488,10 @@ destroy_channel (struct GNUNET_MESH_Channel *ch, int call_cleaner)
 
   /* signal channel destruction */
   if ( (NULL != h->cleaner) && (0 != ch->peer) && (GNUNET_YES == call_cleaner) )
+  {
+    LOG (GNUNET_ERROR_TYPE_DEBUG, " calling cleaner\n");
     h->cleaner (h->cls, ch, ch->ctx);
+  }
 
   /* check that clients did not leave messages behind in the queue */
   for (th = h->th_head; NULL != th; th = next)
@@ -485,8 +500,11 @@ destroy_channel (struct GNUNET_MESH_Channel *ch, int call_cleaner)
     if (th->channel != ch)
       continue;
     /* Clients should have aborted their requests already.
-     * Management traffic should be ok, as clients can't cancel that */
-    GNUNET_break (GNUNET_NO == th_is_payload (th));
+     * Management traffic should be ok, as clients can't cancel that.
+     * If the service crashed and we are reconnecting, it's ok.
+     */
+    GNUNET_break (GNUNET_NO == th_is_payload (th)
+                  || GNUNET_NO == h->in_receive);
     GNUNET_CONTAINER_DLL_remove (h->th_head, h->th_tail, th);
 
     /* clean up request */
@@ -653,8 +671,6 @@ send_connect (struct GNUNET_MESH_Handle *h)
 static int
 do_reconnect (struct GNUNET_MESH_Handle *h)
 {
-  struct GNUNET_MESH_Channel *ch;
-
   LOG (GNUNET_ERROR_TYPE_DEBUG, "*****************************\n");
   LOG (GNUNET_ERROR_TYPE_DEBUG, "*******   RECONNECT   *******\n");
   LOG (GNUNET_ERROR_TYPE_DEBUG, "*****************************\n");
@@ -693,37 +709,6 @@ do_reconnect (struct GNUNET_MESH_Handle *h)
     h->reconnect_time = GNUNET_TIME_UNIT_MILLISECONDS;
   }
   send_connect (h);
-  /* Rebuild all channels */
-  for (ch = h->channels_head; NULL != ch; ch = ch->next)
-  {
-    struct GNUNET_MESH_ChannelMessage tmsg;
-    uint32_t options;
-
-    if (ch->chid >= GNUNET_MESH_LOCAL_CHANNEL_ID_SERV)
-    {
-      /* Channel was created by service (incoming channel) */
-      /* TODO: Notify service of missing channel, to request
-       * creator to recreate path (find a path to him via DHT?)
-       */
-      continue;
-    }
-    ch->allow_send = GNUNET_NO;
-    tmsg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE);
-    tmsg.header.size = htons (sizeof (struct GNUNET_MESH_ChannelMessage));
-    tmsg.channel_id = htonl (ch->chid);
-    tmsg.port = htonl (ch->port);
-    GNUNET_PEER_resolve (ch->peer, &tmsg.peer);
-
-    options = 0;
-    if (GNUNET_YES == ch->nobuffer)
-      options |= GNUNET_MESH_OPTION_NOBUFFER;
-
-    if (GNUNET_YES == ch->reliable)
-      options |= GNUNET_MESH_OPTION_RELIABLE;
-
-    tmsg.opt = htonl (options);
-    send_packet (h, &tmsg.header, ch);
-  }
   return GNUNET_YES;
 }
 
@@ -756,8 +741,17 @@ reconnect_cbk (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
 static void
 reconnect (struct GNUNET_MESH_Handle *h)
 {
-  LOG (GNUNET_ERROR_TYPE_DEBUG, "Requested RECONNECT\n");
+  struct GNUNET_MESH_Channel *ch;
+  struct GNUNET_MESH_Channel *next;
+
+  LOG (GNUNET_ERROR_TYPE_DEBUG,
+       "Requested RECONNECT, destroying all channels\n");
   h->in_receive = GNUNET_NO;
+  for (ch = h->channels_head; NULL != ch; ch = next)
+  {
+    next = ch->next;
+    destroy_channel (ch, GNUNET_YES);
+  }
   if (GNUNET_SCHEDULER_NO_TASK == h->reconnect_task)
     h->reconnect_task = GNUNET_SCHEDULER_add_delayed (h->reconnect_time,
                                                       &reconnect_cbk, h);
@@ -792,30 +786,20 @@ process_channel_created (struct GNUNET_MESH_Handle *h,
   }
   if (NULL != h->new_channel)
   {
+    void *ctx;
+
     ch = create_channel (h, chid);
     ch->allow_send = GNUNET_NO;
     ch->peer = GNUNET_PEER_intern (&msg->peer);
     ch->mesh = h;
     ch->chid = chid;
     ch->port = port;
-    if (0 != (msg->opt & GNUNET_MESH_OPTION_NOBUFFER))
-      ch->nobuffer = GNUNET_YES;
-    else
-      ch->nobuffer = GNUNET_NO;
-
-    if (0 != (msg->opt & GNUNET_MESH_OPTION_RELIABLE))
-      ch->reliable = GNUNET_YES;
-    else
-      ch->reliable = GNUNET_NO;
-
-    if (GNUNET_YES == ch->reliable &&
-        0 != (msg->opt & GNUNET_MESH_OPTION_OOORDER))
-      ch->ooorder = GNUNET_YES;
-    else
-      ch->ooorder = GNUNET_NO;
+    ch->options = ntohl (msg->opt);
 
     LOG (GNUNET_ERROR_TYPE_DEBUG, "  created channel %p\n", ch);
-    ch->ctx = h->new_channel (h->cls, ch, &msg->peer, ch->port);
+    ctx = h->new_channel (h->cls, ch, &msg->peer, ch->port, ch->options);
+    if (NULL != ctx)
+      ch->ctx = ctx;
     LOG (GNUNET_ERROR_TYPE_DEBUG, "User notified\n");
   }
   else
@@ -850,7 +834,7 @@ process_channel_destroy (struct GNUNET_MESH_Handle *h,
   struct GNUNET_MESH_Channel *ch;
   MESH_ChannelNumber chid;
 
-  LOG (GNUNET_ERROR_TYPE_DEBUG, "Destroying channel from service\n");
+  LOG (GNUNET_ERROR_TYPE_DEBUG, "Channel Destroy received from service\n");
   chid = ntohl (msg->channel_id);
   ch = retrieve_channel (h, chid);
 
@@ -859,7 +843,7 @@ process_channel_destroy (struct GNUNET_MESH_Handle *h,
     LOG (GNUNET_ERROR_TYPE_DEBUG, "channel %X unknown\n", chid);
     return;
   }
-  LOG (GNUNET_ERROR_TYPE_DEBUG, "channel %X destroyed\n", ch->chid);
+  LOG (GNUNET_ERROR_TYPE_DEBUG, " destroying channel %X\n", ch->chid);
   destroy_channel (ch, GNUNET_YES);
 }
 
@@ -878,18 +862,21 @@ process_incoming_data (struct GNUNET_MESH_Handle *h,
   const struct GNUNET_MESH_MessageHandler *handler;
   struct GNUNET_MESH_LocalData *dmsg;
   struct GNUNET_MESH_Channel *ch;
+  size_t size;
   unsigned int i;
   uint16_t type;
 
   LOG (GNUNET_ERROR_TYPE_DEBUG, "Got a data message!\n");
-
   dmsg = (struct GNUNET_MESH_LocalData *) message;
-
   ch = retrieve_channel (h, ntohl (dmsg->id));
   payload = (struct GNUNET_MessageHeader *) &dmsg[1];
   LOG (GNUNET_ERROR_TYPE_DEBUG, "  %s data on channel %s [%X]\n",
-       ch->chid >= GNUNET_MESH_LOCAL_CHANNEL_ID_SERV ? "fwd" : "bck",
+       GM_f2s (ch->chid >= GNUNET_MESH_LOCAL_CHANNEL_ID_SERV),
        GNUNET_i2s (GNUNET_PEER_resolve2 (ch->peer)), ntohl (dmsg->id));
+
+  size = ntohs (message->size);
+  LOG (GNUNET_ERROR_TYPE_DEBUG, "  %u bytes\n", size);
+
   if (NULL == ch)
   {
     /* Channel was ignored/destroyed, probably service didn't get it yet */
@@ -897,12 +884,12 @@ process_incoming_data (struct GNUNET_MESH_Handle *h,
     return;
   }
   type = ntohs (payload->type);
-  LOG (GNUNET_ERROR_TYPE_DEBUG, "  payload type %u\n", type);
+  size = ntohs (payload->size);
+  LOG (GNUNET_ERROR_TYPE_DEBUG, "  payload type %s\n", GM_m2s (type));
   for (i = 0; i < h->n_handlers; i++)
   {
     handler = &h->message_handlers[i];
-    LOG (GNUNET_ERROR_TYPE_DEBUG,
-         "    checking handler for type %u\n",
+    LOG (GNUNET_ERROR_TYPE_DEBUG, "    checking handler for type %u\n",
          handler->type);
     if (handler->type == type)
     {
@@ -917,6 +904,7 @@ process_incoming_data (struct GNUNET_MESH_Handle *h,
       {
         LOG (GNUNET_ERROR_TYPE_DEBUG,
              "callback completed successfully\n");
+        return;
       }
     }
   }
@@ -941,10 +929,10 @@ process_ack (struct GNUNET_MESH_Handle *h,
   LOG (GNUNET_ERROR_TYPE_DEBUG, "Got an ACK!\n");
   msg = (struct GNUNET_MESH_LocalAck *) message;
   chid = ntohl (msg->channel_id);
-    ch = retrieve_channel (h, chid);
+  ch = retrieve_channel (h, chid);
   if (NULL == ch)
   {
-    LOG (GNUNET_ERROR_TYPE_WARNING, "ACK on unknown channel %X\n", chid);
+    LOG (GNUNET_ERROR_TYPE_DEBUG, "ACK on unknown channel %X\n", chid);
     return;
   }
   LOG (GNUNET_ERROR_TYPE_DEBUG, "  on channel %X!\n", ch->chid);
@@ -952,10 +940,9 @@ process_ack (struct GNUNET_MESH_Handle *h,
   if (NULL == h->th && 0 < ch->packet_size)
   {
     LOG (GNUNET_ERROR_TYPE_DEBUG, "  tmt rdy was NULL, requesting!\n");
-    h->th =
-        GNUNET_CLIENT_notify_transmit_ready (h->client, ch->packet_size,
-                                             GNUNET_TIME_UNIT_FOREVER_REL,
-                                             GNUNET_YES, &send_callback, h);
+    h->th = GNUNET_CLIENT_notify_transmit_ready (h->client, ch->packet_size,
+                                                 GNUNET_TIME_UNIT_FOREVER_REL,
+                                                 GNUNET_YES, &send_callback, h);
   }
 }
 
@@ -970,7 +957,7 @@ process_ack (struct GNUNET_MESH_Handle *h,
 // process_get_channels (struct GNUNET_MESH_Handle *h,
 //                      const struct GNUNET_MessageHeader *message)
 // {
-//   struct GNUNET_MESH_LocalMonitor *msg;
+//   struct GNUNET_MESH_LocalInfo *msg;
 //
 //   GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Get Channels messasge received\n");
 //
@@ -980,16 +967,16 @@ process_ack (struct GNUNET_MESH_Handle *h,
 //     return;
 //   }
 //
-//   msg = (struct GNUNET_MESH_LocalMonitor *) message;
+//   msg = (struct GNUNET_MESH_LocalInfo *) message;
 //   if (ntohs (message->size) !=
-//       (sizeof (struct GNUNET_MESH_LocalMonitor) +
+//       (sizeof (struct GNUNET_MESH_LocalInfo) +
 //        sizeof (struct GNUNET_PeerIdentity)))
 //   {
 //     GNUNET_break_op (0);
 //     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
 //                 "Get channels message: size %hu - expected %u\n",
 //                 ntohs (message->size),
-//                 sizeof (struct GNUNET_MESH_LocalMonitor));
+//                 sizeof (struct GNUNET_MESH_LocalInfo));
 //     return;
 //   }
 //   h->channels_cb (h->channels_cls,
@@ -1010,7 +997,7 @@ process_ack (struct GNUNET_MESH_Handle *h,
 // process_show_channel (struct GNUNET_MESH_Handle *h,
 //                      const struct GNUNET_MessageHeader *message)
 // {
-//   struct GNUNET_MESH_LocalMonitor *msg;
+//   struct GNUNET_MESH_LocalInfo *msg;
 //   size_t esize;
 //
 //   GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Show Channel messasge received\n");
@@ -1022,8 +1009,8 @@ process_ack (struct GNUNET_MESH_Handle *h,
 //   }
 //
 //   /* Verify message sanity */
-//   msg = (struct GNUNET_MESH_LocalMonitor *) message;
-//   esize = sizeof (struct GNUNET_MESH_LocalMonitor);
+//   msg = (struct GNUNET_MESH_LocalInfo *) message;
+//   esize = sizeof (struct GNUNET_MESH_LocalInfo);
 //   if (ntohs (message->size) != esize)
 //   {
 //     GNUNET_break_op (0);
@@ -1045,6 +1032,109 @@ process_ack (struct GNUNET_MESH_Handle *h,
 // }
 
 
+
+
+/*
+ * Process a local reply about info on all tunnels, pass info to the user.
+ *
+ * @param h Mesh handle.
+ * @param message Message itself.
+ */
+static void
+process_get_tunnels (struct GNUNET_MESH_Handle *h,
+                     const struct GNUNET_MessageHeader *message)
+{
+  struct GNUNET_MESH_LocalInfoTunnel *msg;
+  uint16_t size;
+
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Get Tunnels messasge received\n");
+
+  if (NULL == h->tunnels_cb)
+  {
+    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "  ignored\n");
+    return;
+  }
+
+  size = ntohs (message->size);
+  if (sizeof (struct GNUNET_MESH_LocalInfoTunnel) > size)
+  {
+    h->tunnels_cb (h->tunnel_cls, NULL, 0, 0, 0, 0);
+    h->tunnels_cb = NULL;
+    h->tunnels_cls = NULL;
+    return;
+  }
+
+  msg = (struct GNUNET_MESH_LocalInfoTunnel *) message;
+  h->tunnels_cb (h->tunnel_cls,
+                 &msg->destination,
+                 ntohl (msg->channels),
+                 ntohl (msg->connections),
+                 ntohs (msg->estate),
+                 ntohs (msg->cstate));
+
+}
+
+
+
+/*
+ * Process a local monitor_channel reply, pass info to the user.
+ *
+ * @param h Mesh handle.
+ * @param message Message itself.
+ */
+static void
+process_get_tunnel (struct GNUNET_MESH_Handle *h,
+                    const struct GNUNET_MessageHeader *message)
+{
+  struct GNUNET_MESH_LocalInfoTunnel *msg;
+  size_t esize;
+  size_t msize;
+  unsigned int ch_n;
+  unsigned int c_n;
+
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Get Tunnel messasge received\n");
+  if (NULL == h->tunnel_cb)
+  {
+    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "  ignored\n");
+    return;
+  }
+
+  /* Verify message sanity */
+  msg = (struct GNUNET_MESH_LocalInfoTunnel *) message;
+  msize = ntohs (message->size);
+  esize = sizeof (struct GNUNET_MESH_LocalInfoTunnel);
+  if (esize > msize)
+  {
+    GNUNET_break_op (0);
+    h->tunnel_cb (h->tunnel_cls, NULL, 0, 0, 0, 0);
+    goto clean_cls;
+  }
+  ch_n = ntohl (msg->channels);
+  c_n = ntohl (msg->connections);
+  esize += ch_n * sizeof (MESH_ChannelNumber);
+  esize += c_n * sizeof (struct GNUNET_HashCode);
+  if (msize != esize)
+  {
+    GNUNET_break_op (0);
+    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "m:%u, e: %u (%u ch, %u conn)\n",
+                msize, esize, ch_n, c_n);
+    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "%u (%u ch, %u conn)\n",
+                sizeof (struct GNUNET_MESH_LocalInfoTunnel),
+                sizeof (MESH_ChannelNumber), sizeof (struct GNUNET_HashCode));
+    h->tunnel_cb (h->tunnel_cls, NULL, 0, 0, 0, 0);
+    goto clean_cls;
+  }
+
+  /* Call Callback with tunnel info. */
+  h->tunnel_cb (h->tunnel_cls, &msg->destination,
+                ch_n, c_n,
+                ntohs (msg->estate), ntohs (msg->cstate));
+
+clean_cls:
+  h->tunnel_cb = NULL;
+  h->tunnel_cls = NULL;
+}
+
 /**
  * Function to process all messages received from the service
  *
@@ -1067,7 +1157,7 @@ msg_received (void *cls, const struct GNUNET_MessageHeader *msg)
   type = ntohs (msg->type);
   LOG (GNUNET_ERROR_TYPE_DEBUG, "\n");
   LOG (GNUNET_ERROR_TYPE_DEBUG, "Received a message: %s\n",
-       GNUNET_MESH_DEBUG_M2S (type));
+       GM_m2s (type));
   switch (type)
   {
     /* Notify of a new incoming channel */
@@ -1075,8 +1165,8 @@ msg_received (void *cls, const struct GNUNET_MessageHeader *msg)
     process_channel_created (h, (struct GNUNET_MESH_ChannelMessage *) msg);
     break;
     /* Notify of a channel disconnection */
-  case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY:
-  case GNUNET_MESSAGE_TYPE_MESH_LOCAL_NACK:
+  case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY: /* TODO separate(gid problem)*/
+  case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_NACK:
     process_channel_destroy (h, (struct GNUNET_MESH_ChannelMessage *) msg);
     break;
   case GNUNET_MESSAGE_TYPE_MESH_LOCAL_DATA:
@@ -1085,17 +1175,26 @@ msg_received (void *cls, const struct GNUNET_MessageHeader *msg)
   case GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK:
     process_ack (h, msg);
     break;
-//   case GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_CHANNELS: DEPRECATED
+//   case GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_CHANNELS:
 //     process_get_channels (h, msg);
 //     break;
-//   case GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_CHANNEL: DEPRECATED
+//   case GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_CHANNEL:
+//     process_show_channel (h, msg);
+//     break;
+  case GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS:
+    process_get_tunnels (h, msg);
+    break;
+  case GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNEL:
+    process_get_tunnel (h, msg);
+    break;
+//   case GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_CHANNEL:
 //     process_show_channel (h, msg);
 //     break;
   default:
     /* We shouldn't get any other packages, log and ignore */
     LOG (GNUNET_ERROR_TYPE_WARNING,
          "unsolicited message form service (type %s)\n",
-         GNUNET_MESH_DEBUG_M2S (ntohs (msg->type)));
+         GM_m2s (ntohs (msg->type)));
   }
   LOG (GNUNET_ERROR_TYPE_DEBUG, "message processed\n");
   if (GNUNET_YES == h->in_receive)
@@ -1179,7 +1278,7 @@ send_callback (void *cls, size_t size, void *buf)
         dmsg->id = htonl (ch->chid);
         dmsg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_DATA);
         LOG (GNUNET_ERROR_TYPE_DEBUG, "#  payload type %s\n",
-             GNUNET_MESH_DEBUG_M2S (ntohs (mh->type)));
+             GM_m2s (ntohs (mh->type)));
                 ch->allow_send = GNUNET_NO;
       }
       else
@@ -1194,7 +1293,7 @@ send_callback (void *cls, size_t size, void *buf)
       struct GNUNET_MessageHeader *mh = (struct GNUNET_MessageHeader *) &th[1];
 
       LOG (GNUNET_ERROR_TYPE_DEBUG, "#  mesh internal traffic, type %s\n",
-           GNUNET_MESH_DEBUG_M2S (ntohs (mh->type)));
+           GM_m2s (ntohs (mh->type)));
       memcpy (cbuf, &th[1], th->size);
       psize = th->size;
     }
@@ -1256,7 +1355,7 @@ send_packet (struct GNUNET_MESH_Handle *h,
   size_t msize;
 
   LOG (GNUNET_ERROR_TYPE_DEBUG, " Sending message to service: %s\n",
-       GNUNET_MESH_DEBUG_M2S(ntohs(msg->type)));
+       GM_m2s(ntohs(msg->type)));
   msize = ntohs (msg->size);
   th = GNUNET_malloc (sizeof (struct GNUNET_MESH_TransmitHandle) + msize);
   th->timeout = GNUNET_TIME_UNIT_FOREVER_ABS;
@@ -1289,7 +1388,7 @@ GNUNET_MESH_connect (const struct GNUNET_CONFIGURATION_Handle *cfg, void *cls,
   struct GNUNET_MESH_Handle *h;
 
   LOG (GNUNET_ERROR_TYPE_DEBUG, "GNUNET_MESH_connect()\n");
-  h = GNUNET_malloc (sizeof (struct GNUNET_MESH_Handle));
+  h = GNUNET_new (struct GNUNET_MESH_Handle);
   LOG (GNUNET_ERROR_TYPE_DEBUG, " addr %p\n", h);
   h->cfg = cfg;
   h->new_channel = new_channel;
@@ -1370,6 +1469,8 @@ GNUNET_MESH_disconnect (struct GNUNET_MESH_Handle *handle)
       case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY:
       case GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_CHANNELS:
       case GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_CHANNEL:
+      case GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNEL:
+      case GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS:
         break;
       default:
         GNUNET_break (0);
@@ -1411,18 +1512,16 @@ GNUNET_MESH_disconnect (struct GNUNET_MESH_Handle *handle)
  * @param channel_ctx client's channel context to associate with the channel
  * @param peer peer identity the channel should go to
  * @param port Port number.
- * @param nobuffer Flag for disabling buffering on relay nodes.
- * @param reliable Flag for end-to-end reliability.
+ * @param options MeshOption flag field, with all desired option bits set to 1.
  *
  * @return handle to the channel
  */
 struct GNUNET_MESH_Channel *
 GNUNET_MESH_channel_create (struct GNUNET_MESH_Handle *h,
-                           void *channel_ctx,
-                           const struct GNUNET_PeerIdentity *peer,
-                           uint32_t port,
-                           int nobuffer,
-                           int reliable)
+                            void *channel_ctx,
+                            const struct GNUNET_PeerIdentity *peer,
+                            uint32_t port,
+                            enum GNUNET_MESH_ChannelOption options)
 {
   struct GNUNET_MESH_Channel *ch;
   struct GNUNET_MESH_ChannelMessage msg;
@@ -1440,12 +1539,7 @@ GNUNET_MESH_channel_create (struct GNUNET_MESH_Handle *h,
   msg.channel_id = htonl (ch->chid);
   msg.port = htonl (port);
   msg.peer = *peer;
-  msg.opt = 0;
-  if (GNUNET_YES == reliable)
-    msg.opt |= GNUNET_MESH_OPTION_RELIABLE;
-  if (GNUNET_YES == nobuffer)
-    msg.opt |= GNUNET_MESH_OPTION_NOBUFFER;
-  msg.opt = htonl (msg.opt);
+  msg.opt = htonl (options);
   ch->allow_send = 0;
   send_packet (h, &msg.header, ch);
   return ch;
@@ -1502,23 +1596,24 @@ GNUNET_MESH_channel_destroy (struct GNUNET_MESH_Channel *channel)
  */
 const union GNUNET_MESH_ChannelInfo *
 GNUNET_MESH_channel_get_info (struct GNUNET_MESH_Channel *channel,
-                             enum MeshOption option, ...)
+                              enum GNUNET_MESH_ChannelOption option, ...)
 {
+  static int bool_flag;
   const union GNUNET_MESH_ChannelInfo *ret;
 
   switch (option)
   {
     case GNUNET_MESH_OPTION_NOBUFFER:
-      ret = (const union GNUNET_MESH_ChannelInfo *) &channel->nobuffer;
-      break;
     case GNUNET_MESH_OPTION_RELIABLE:
-      ret = (const union GNUNET_MESH_ChannelInfo *) &channel->reliable;
-      break;
     case GNUNET_MESH_OPTION_OOORDER:
-      ret = (const union GNUNET_MESH_ChannelInfo *) &channel->ooorder;
+      if (0 != (option & channel->options))
+        bool_flag = GNUNET_YES;
+      else
+        bool_flag = GNUNET_NO;
+      ret = (const union GNUNET_MESH_ChannelInfo *) &bool_flag;
       break;
     case GNUNET_MESH_OPTION_PEER:
-      ret = (const union GNUNET_MESH_ChannelInfo *) &channel->peer;
+      ret = (const union GNUNET_MESH_ChannelInfo *) GNUNET_PEER_resolve2 (channel->peer);
       break;
     default:
       GNUNET_break (0);
@@ -1548,7 +1643,7 @@ GNUNET_MESH_notify_transmit_ready (struct GNUNET_MESH_Channel *channel, int cork
   LOG (GNUNET_ERROR_TYPE_DEBUG, "    payload size %u\n", notify_size);
   GNUNET_assert (NULL != notify);
   GNUNET_assert (0 == channel->packet_size); // Only one data packet allowed
-  th = GNUNET_malloc (sizeof (struct GNUNET_MESH_TransmitHandle));
+  th = GNUNET_new (struct GNUNET_MESH_TransmitHandle);
   th->channel = channel;
   th->timeout = GNUNET_TIME_relative_to_absolute (maxdelay);
   th->size = notify_size + sizeof (struct GNUNET_MESH_LocalData);
@@ -1599,6 +1694,16 @@ GNUNET_MESH_receive_done (struct GNUNET_MESH_Channel *channel)
 }
 
 
+static void
+send_info_request (struct GNUNET_MESH_Handle *h, uint16_t type)
+{
+  struct GNUNET_MessageHeader msg;
+
+  msg.size = htons (sizeof (msg));
+  msg.type = htons (type);
+  send_packet (h, &msg, NULL);
+}
+
 /**
  * Request information about the running mesh peer.
  * The callback will be called for every channel known to the service,
@@ -1619,11 +1724,7 @@ GNUNET_MESH_get_channels (struct GNUNET_MESH_Handle *h,
                          GNUNET_MESH_ChannelsCB callback,
                          void *callback_cls)
 {
-  struct GNUNET_MessageHeader msg;
-
-  msg.size = htons (sizeof (msg));
-  msg.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_CHANNELS);
-  send_packet (h, &msg, NULL);
+  send_info_request (h, GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_CHANNELS);
   h->channels_cb = callback;
   h->channels_cls = callback_cls;
 }
@@ -1632,6 +1733,8 @@ GNUNET_MESH_get_channels (struct GNUNET_MESH_Handle *h,
 /**
  * Cancel a monitor request. The monitor callback will not be called.
  *
+ * WARNING: unstable API, likely to change in the future!
+ *
  * @param h Mesh handle.
  *
  * @return Closure given to GNUNET_MESH_monitor, if any.
@@ -1648,6 +1751,111 @@ GNUNET_MESH_get_channels_cancel (struct GNUNET_MESH_Handle *h)
 }
 
 
+/**
+ * Request information about the running mesh peer.
+ * The callback will be called for every peer known to the service.
+ *
+ * If called again on the same handle, it will overwrite the previous
+ * callback and cls. To retrieve the cls, monitor_cancel must be
+ * called first.
+ *
+ * WARNING: unstable API, likely to change in the future!
+ *
+ * @param h Handle to the mesh peer.
+ * @param callback Function to call with the requested data.
+ * @param callback_cls Closure for @c callback.
+ */
+void
+GNUNET_MESH_get_peers (struct GNUNET_MESH_Handle *h,
+                       GNUNET_MESH_PeersCB callback,
+                       void *callback_cls)
+{
+  send_info_request (h, GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_PEERS);
+  h->peers_cb = callback;
+  h->peers_cls = callback_cls;
+}
+
+
+
+/**
+ * Request information about the running mesh peer.
+ * The callback will be called for every tunnel known to the service.
+ *
+ * If called again on the same handle, it will overwrite the previous
+ * callback and cls. To retrieve the cls, monitor_cancel must be
+ * called first.
+ *
+ * WARNING: unstable API, likely to change in the future!
+ *
+ * @param h Handle to the mesh peer.
+ * @param callback Function to call with the requested data.
+ * @param callback_cls Closure for @c callback.
+ */
+void
+GNUNET_MESH_get_tunnels (struct GNUNET_MESH_Handle *h,
+                         GNUNET_MESH_TunnelsCB callback,
+                         void *callback_cls)
+{
+  send_info_request (h, GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS);
+  h->tunnels_cb = callback;
+  h->tunnels_cls = callback_cls;
+}
+
+
+/**
+ * Cancel a monitor request. The monitor callback will not be called.
+ *
+ * @param h Mesh handle.
+ *
+ * @return Closure given to GNUNET_MESH_monitor, if any.
+ */
+void *
+GNUNET_MESH_get_tunnels_cancel (struct GNUNET_MESH_Handle *h)
+{
+  void *cls;
+
+  h->tunnels_cb = NULL;
+  cls = h->tunnels_cls;
+  h->tunnels_cls = NULL;
+
+  return cls;
+}
+
+
+
+/**
+ * Request information about the running mesh peer.
+ * The callback will be called for every channel known to the service,
+ * listing all active peers that blong to the channel.
+ *
+ * If called again on the same handle, it will overwrite the previous
+ * callback and cls. To retrieve the cls, monitor_cancel must be
+ * called first.
+ *
+ * WARNING: unstable API, likely to change in the future!
+ *
+ * @param h Handle to the mesh peer.
+ * @param callback Function to call with the requested data.
+ * @param callback_cls Closure for @c callback.
+ */
+void
+GNUNET_MESH_get_tunnel (struct GNUNET_MESH_Handle *h,
+                        const struct GNUNET_PeerIdentity *id,
+                        GNUNET_MESH_TunnelCB callback,
+                        void *callback_cls)
+{
+  struct GNUNET_MESH_LocalInfo msg;
+
+  memset (&msg, 0, sizeof (msg));
+  msg.header.size = htons (sizeof (msg));
+  msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNEL);
+  msg.peer = *id;
+  send_packet (h, &msg.header, NULL);
+  h->tunnel_cb = callback;
+  h->tunnel_cls = callback_cls;
+}
+
+
 /**
  * Request information about a specific channel of the running mesh peer.
  *
@@ -1667,13 +1875,13 @@ GNUNET_MESH_show_channel (struct GNUNET_MESH_Handle *h,
                          GNUNET_MESH_ChannelCB callback,
                          void *callback_cls)
 {
-  struct GNUNET_MESH_LocalMonitor msg;
+  struct GNUNET_MESH_LocalInfo msg;
 
   msg.header.size = htons (sizeof (msg));
   msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_CHANNEL);
-  msg.owner = *initiator;
+  msg.peer = *initiator;
   msg.channel_id = htonl (channel_number);
-  msg.reserved = 0;
+//   msg.reserved = 0;
   send_packet (h, &msg.header, NULL);
   h->channel_cb = callback;
   h->channel_cls = callback_cls;