- Add some of the tunnel introscpecion back
authorBart Polot <bart@net.in.tum.de>
Sat, 28 Dec 2013 14:30:51 +0000 (14:30 +0000)
committerBart Polot <bart@net.in.tum.de>
Sat, 28 Dec 2013 14:30:51 +0000 (14:30 +0000)
src/mesh/gnunet-service-mesh_channel.c
src/mesh/gnunet-service-mesh_channel.h
src/mesh/gnunet-service-mesh_local.c
src/mesh/gnunet-service-mesh_tunnel.c
src/mesh/gnunet-service-mesh_tunnel.h
src/mesh/mesh.h
src/mesh/mesh_api.c

index a11210a38881f6370336be0a604410bf33565e24..42d9bc1cf84c9f9c5073abf25fd78898b3164bf3 100644 (file)
@@ -1351,11 +1351,11 @@ GMCH_destroy (struct MeshChannel *ch)
 
 
 /**
- * Get channel ID.
+ * Get the channel's public ID.
  *
  * @param ch Channel.
  *
- * @return ID
+ * @return ID used to identify the channel with the remote peer.
  */
 MESH_ChannelNumber
 GMCH_get_id (const struct MeshChannel *ch)
index 504754148c849fa0c4f1a61a7a65d21d9cc79a65..e1d5d4876ab7b757806941290b2a0c151b9eed4e 100644 (file)
@@ -61,12 +61,13 @@ struct MeshChannel;
 void
 GMCH_destroy (struct MeshChannel *ch);
 
+
 /**
- * Get channel ID.
+ * Get the channel's public ID.
  *
  * @param ch Channel.
  *
- * @return ID
+ * @return ID used to identify the channel with the remote peer.
  */
 MESH_ChannelNumber
 GMCH_get_id (const struct MeshChannel *ch);
index fa54fafa638fb72a8e6acd5a1a45b535d506a557..82743c892d234a8fc57a6db25d1b27ab223ca80a 100644 (file)
@@ -30,6 +30,9 @@
 #include "gnunet-service-mesh_local.h"
 #include "gnunet-service-mesh_channel.h"
 
+/* INFO DEBUG */
+#include "gnunet-service-mesh_tunnel.h"
+
 #define LOG(level, ...) GNUNET_log_from(level,"mesh-loc",__VA_ARGS__)
 
 /******************************************************************************/
@@ -575,41 +578,41 @@ handle_ack (void *cls, struct GNUNET_SERVER_Client *client,
 }
 
 
-/*
+/**
  * Iterator over all tunnels to send a monitoring client info about each tunnel.
  *
  * @param cls Closure (client handle).
- * @param key Key (hashed tunnel ID, unused).
+ * @param peer Peer ID (tunnel remote peer).
  * @param value Tunnel info.
  *
  * @return #GNUNET_YES, to keep iterating.
  */
-// static int
-// monitor_all_tunnels_iterator (void *cls,
-//                               const struct GNUNET_HashCode * key,
-//                               void *value)
-// {
-//   struct GNUNET_SERVER_Client *client = cls;
-//   struct MeshChannel *ch = value;
-//   struct GNUNET_MESH_LocalMonitor *msg;
-//
-//   msg = GNUNET_new (struct GNUNET_MESH_LocalMonitor);
-//   msg->channel_id = htonl (ch->gid);
-//   msg->header.size = htons (sizeof (struct GNUNET_MESH_LocalMonitor));
-//   msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS);
-//
-//   LOG (GNUNET_ERROR_TYPE_INFO,
-//               "*  sending info about tunnel %s\n",
-//               GNUNET_i2s (&msg->owner));
-//
-//   GNUNET_SERVER_notification_context_unicast (nc, client,
-//                                               &msg->header, GNUNET_NO);
-//   return GNUNET_YES;
-// }
+static int
+monitor_all_tunnels_iterator (void *cls,
+                              const struct GNUNET_PeerIdentity * peer,
+                              void *value)
+{
+  struct GNUNET_SERVER_Client *client = cls;
+  struct MeshChannel *ch = value;
+  struct GNUNET_MESH_LocalInfo *msg;
+
+  msg = GNUNET_new (struct GNUNET_MESH_LocalInfo);
+  msg->channel_id = htonl (GMCH_get_id (ch));
+  msg->header.size = htons (sizeof (struct GNUNET_MESH_LocalInfo));
+  msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS);
+
+  LOG (GNUNET_ERROR_TYPE_INFO,
+              "*  sending info about tunnel %s\n",
+              GNUNET_i2s (&msg->owner));
+
+  GNUNET_SERVER_notification_context_unicast (nc, client,
+                                              &msg->header, GNUNET_NO);
+  return GNUNET_YES;
+}
 
 
 /**
- * Handler for client's MONITOR request.
+ * Handler for client's INFO TUNNELS request.
  *
  * @param cls Closure (unused).
  * @param client Identification of the client.
@@ -620,6 +623,8 @@ handle_get_tunnels (void *cls, struct GNUNET_SERVER_Client *client,
                     const struct GNUNET_MessageHeader *message)
 {
   struct MeshClient *c;
+  size_t size;
+  struct GNUNET_MessageHeader *reply;
 
   /* Sanity check for client registration */
   if (NULL == (c = GML_client_get (client)))
@@ -629,12 +634,18 @@ handle_get_tunnels (void *cls, struct GNUNET_SERVER_Client *client,
     return;
   }
 
-  LOG (GNUNET_ERROR_TYPE_INFO,
-              "Received get tunnels request from client %u\n",
-              c->id);
-//   GNUNET_CONTAINER_multihashmap_iterate (tunnels,
-//                                          monitor_all_tunnels_iterator,
-//                                          client);
+  LOG (GNUNET_ERROR_TYPE_INFO, "Received get tunnels request from client %u\n",
+       c->id);
+
+  size = GMT_count_all () + 1; /* Last one is all \0 to mark 'end' */
+  size *= sizeof (struct GNUNET_PeerIdentity);
+  size += sizeof (*reply);
+  reply = GNUNET_malloc (size);
+  GMT_iterate_all (reply, monitor_all_tunnels_iterator);
+  reply->size = htons (size);
+  reply->type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS);
+  GNUNET_SERVER_notification_context_unicast (nc, client, reply, GNUNET_NO);
+
   LOG (GNUNET_ERROR_TYPE_INFO,
               "Get tunnels request from client %u completed\n",
               c->id);
@@ -653,8 +664,8 @@ void
 handle_show_tunnel (void *cls, struct GNUNET_SERVER_Client *client,
                     const struct GNUNET_MessageHeader *message)
 {
-  const struct GNUNET_MESH_LocalMonitor *msg;
-  struct GNUNET_MESH_LocalMonitor *resp;
+  const struct GNUNET_MESH_LocalInfo *msg;
+  struct GNUNET_MESH_LocalInfo *resp;
   struct MeshClient *c;
   struct MeshChannel *ch;
 
@@ -666,7 +677,7 @@ handle_show_tunnel (void *cls, struct GNUNET_SERVER_Client *client,
     return;
   }
 
-  msg = (struct GNUNET_MESH_LocalMonitor *) message;
+  msg = (struct GNUNET_MESH_LocalInfo *) message;
   LOG (GNUNET_ERROR_TYPE_INFO,
               "Received tunnel info request from client %u for tunnel %s[%X]\n",
               c->id,
@@ -677,7 +688,7 @@ handle_show_tunnel (void *cls, struct GNUNET_SERVER_Client *client,
   if (NULL == ch)
   {
     /* We don't know the tunnel */
-    struct GNUNET_MESH_LocalMonitor warn;
+    struct GNUNET_MESH_LocalInfo warn;
 
     warn = *msg;
     GNUNET_SERVER_notification_context_unicast (nc, client,
@@ -688,9 +699,9 @@ handle_show_tunnel (void *cls, struct GNUNET_SERVER_Client *client,
   }
 
   /* Initialize context */
-  resp = GNUNET_new (struct GNUNET_MESH_LocalMonitor);
+  resp = GNUNET_new (struct GNUNET_MESH_LocalInfo);
   *resp = *msg;
-  resp->header.size = htons (sizeof (struct GNUNET_MESH_LocalMonitor));
+  resp->header.size = htons (sizeof (struct GNUNET_MESH_LocalInfo));
   GNUNET_SERVER_notification_context_unicast (nc, c->handle,
                                               &resp->header, GNUNET_NO);
   GNUNET_free (resp);
@@ -717,7 +728,7 @@ static struct GNUNET_SERVER_MessageHandler client_handlers[] = {
   {&handle_get_tunnels, NULL, GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS,
    sizeof (struct GNUNET_MessageHeader)},
   {&handle_show_tunnel, NULL, GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNEL,
-   sizeof (struct GNUNET_MESH_LocalMonitor)},
+   sizeof (struct GNUNET_MESH_LocalInfo)},
   {NULL, NULL, 0, 0}
 };
 
index 6976d576dbf5bef9a84fad1c3288d5817ad3821b..61a66a7b22236304d2b17f1869e840141e52b4d3 100644 (file)
@@ -2542,6 +2542,11 @@ GMT_2s (const struct MeshTunnel3 *t)
 }
 
 
+/******************************************************************************/
+/*****************************    INFO/DEBUG    *******************************/
+/******************************************************************************/
+
+
 /**
  * Log all possible info about the tunnel state.
  *
@@ -2581,3 +2586,16 @@ GMT_debug (const struct MeshTunnel3 *t)
 
   LOG (GNUNET_ERROR_TYPE_DEBUG, "DEBUG TUNNEL END\n");
 }
+
+
+void
+GMT_iterate_all (void *cls, GNUNET_CONTAINER_PeerMapIterator iter)
+{
+  GNUNET_CONTAINER_multipeermap_iterate (tunnels, iter, cls);
+}
+
+unsigned int
+GMT_count_all (void)
+{
+  return GNUNET_CONTAINER_multipeermap_size (tunnels);
+}
index 04d429063a4393240dacb4329060de3cec8e5eca..19431e346865ead4d30bd33b70766fc886e585d1 100644 (file)
@@ -463,6 +463,12 @@ GMT_2s (const struct MeshTunnel3 *t);
 void
 GMT_debug (const struct MeshTunnel3 *t);
 
+void
+GMT_iterate_all (void *cls, GNUNET_CONTAINER_PeerMapIterator iter);
+
+unsigned int
+GMT_count_all (void);
+
 #if 0                           /* keep Emacsens' auto-indent happy */
 {
 #endif
index 1211f7ebc1e20d51f79259b4a1a57ba725461399..0113f7855c50c85f95d1b998edd3edc2385f2f0c 100644 (file)
@@ -166,10 +166,10 @@ struct GNUNET_MESH_LocalAck
 /**
  * Message to inform the client about channels in the service.
  */
-struct GNUNET_MESH_LocalMonitor
+struct GNUNET_MESH_LocalInfo
 {
   /**
-     * Type: GNUNET_MESSAGE_TYPE_MESH_LOCAL_MONITOR[_TUNNEL]
+     * Type: GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO[_TUNNEL]
    */
   struct GNUNET_MessageHeader header;
 
index cd68e253ed2a16e43adb7cb31288b9265241a8f8..926feb8af8cf837983be060b569fab965ab47622 100644 (file)
@@ -203,6 +203,26 @@ struct GNUNET_MESH_Handle
    * Channel callback closure.
    */
   void *channel_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;
 };
 
 
@@ -927,7 +947,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");
 //
@@ -937,16 +957,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,
@@ -967,7 +987,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");
@@ -979,8 +999,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);
@@ -1002,6 +1022,88 @@ 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_PeerIdentity *id;
+  uint16_t size;
+  unsigned int i;
+
+  GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Get Tunnels messasge received\n");
+
+  if (NULL == h->tunnels_cb)
+  {
+    GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "  ignored\n");
+    return;
+  }
+
+  size = ntohs (message->size);
+  size /= sizeof (struct GNUNET_PeerIdentity);
+  id = (struct GNUNET_PeerIdentity *) &message[1];
+
+  for (i = 0; i < size; i++)
+    h->tunnels_cb (h->tunnels_cls, &id[i]);
+  h->tunnels_cb (h->tunnels_cls, NULL);
+
+  h->tunnels_cb = NULL;
+  h->tunnels_cls = NULL;
+}
+
+
+
+/*
+ * Process a local monitor_channel reply, pass info to the user.
+ *
+ * @param h Mesh handle.
+ * @param message Message itself.
+ */
+// static void
+// process_show_channel (struct GNUNET_MESH_Handle *h,
+//                       const struct GNUNET_MessageHeader *message)
+// {
+//   struct GNUNET_MESH_LocalInfo *msg;
+//   size_t esize;
+//
+//   GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Show Channel messasge received\n");
+//
+//   if (NULL == h->channel_cb)
+//   {
+//     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "  ignored\n");
+//     return;
+//   }
+//
+//   /* Verify message sanity */
+//   msg = (struct GNUNET_MESH_LocalInfo *) message;
+//   esize = sizeof (struct GNUNET_MESH_LocalInfo);
+//   if (ntohs (message->size) != esize)
+//   {
+//     GNUNET_break_op (0);
+//     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+//                 "Show channel message: size %hu - expected %u\n",
+//                 ntohs (message->size),
+//                 esize);
+//
+//     h->channel_cb (h->channel_cls, NULL, NULL);
+//     h->channel_cb = NULL;
+//     h->channel_cls = NULL;
+//
+//     return;
+//   }
+//
+//   h->channel_cb (h->channel_cls,
+//                  &msg->destination,
+//                  &msg->owner);
+// }
+
 /**
  * Function to process all messages received from the service
  *
@@ -1042,10 +1144,16 @@ 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_CHANNEL:
 //     process_show_channel (h, msg);
 //     break;
   default:
@@ -1550,6 +1658,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,
@@ -1570,11 +1688,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;
 }
@@ -1583,6 +1697,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.
@@ -1599,6 +1715,84 @@ GNUNET_MESH_get_channels_cancel (struct GNUNET_MESH_Handle *h)
 }
 
 
+/**
+ * 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_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_TunnelsCB 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.destination = *id;
+  send_packet (h, &msg.header, NULL);
+}
+
+
 /**
  * Request information about a specific channel of the running mesh peer.
  *
@@ -1618,7 +1812,7 @@ 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);