- fix disconnect task scheduling
[oweals/gnunet.git] / src / mesh / mesh_api.c
index f27e86b71935f8fe5b28bb52ad0017f894c8b953..ec5ddcf71d17fd5a8c899383acff924abd09d0ea 100644 (file)
@@ -86,6 +86,29 @@ struct GNUNET_MESH_TransmitHandle
   size_t size;
 };
 
+union MeshInfoCB {
+
+  /**
+   * Channel callback.
+   */
+  GNUNET_MESH_ChannelCB channel_cb;
+
+  /**
+   * Monitor callback
+   */
+  GNUNET_MESH_PeersCB peers_cb;
+
+  /**
+   * Monitor callback
+   */
+  GNUNET_MESH_TunnelsCB tunnels_cb;
+
+  /**
+   * Tunnel callback.
+   */
+  GNUNET_MESH_TunnelCB tunnel_cb;
+};
+
 
 /**
  * Opaque handle to the service.
@@ -185,44 +208,14 @@ struct GNUNET_MESH_Handle
   GNUNET_SCHEDULER_TaskIdentifier reconnect_task;
 
   /**
-   * Monitor callback
-   */
-  GNUNET_MESH_ChannelsCB channels_cb;
-
-  /**
-   * Monitor callback closure.
+   * Callback for an info task (only one active at a time).
    */
-  void *channels_cls;
+  union MeshInfoCB info_cb;
 
   /**
-   * Channel callback.
+   * Info callback closure for @c info_cb.
    */
-  GNUNET_MESH_ChannelCB channel_cb;
-
-  /**
-   * 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;
+  void *info_cls;
 };
 
 
@@ -1023,8 +1016,45 @@ 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_peers (struct GNUNET_MESH_Handle *h,
+                     const struct GNUNET_MessageHeader *message)
+{
+  struct GNUNET_MESH_LocalInfoPeer *msg;
+  uint16_t size;
 
-/*
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Get Peer messasge received\n");
+
+  if (NULL == h->info_cb.peers_cb)
+  {
+    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "  ignored\n");
+    return;
+  }
+
+  size = ntohs (message->size);
+  if (sizeof (struct GNUNET_MESH_LocalInfoPeer) > size)
+  {
+    h->info_cb.peers_cb (h->info_cls, NULL, -1, 0, 0);
+    h->info_cb.peers_cb = NULL;
+    h->info_cls = NULL;
+    return;
+  }
+
+  msg = (struct GNUNET_MESH_LocalInfoPeer *) message;
+  h->info_cb.peers_cb (h->info_cls, &msg->destination,
+                       (int) ntohs (msg->tunnel),
+                       (unsigned int ) ntohs (msg->paths),
+                       0);
+}
+
+
+/**
  * Process a local reply about info on all tunnels, pass info to the user.
  *
  * @param h Mesh handle.
@@ -1039,7 +1069,7 @@ process_get_tunnels (struct GNUNET_MESH_Handle *h,
 
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Get Tunnels messasge received\n");
 
-  if (NULL == h->tunnels_cb)
+  if (NULL == h->info_cb.tunnels_cb)
   {
     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "  ignored\n");
     return;
@@ -1048,26 +1078,23 @@ process_get_tunnels (struct GNUNET_MESH_Handle *h,
   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;
+    h->info_cb.tunnels_cb (h->info_cls, NULL, 0, 0, 0, 0);
+    h->info_cb.tunnels_cb = NULL;
+    h->info_cls = NULL;
     return;
   }
 
   msg = (struct GNUNET_MESH_LocalInfoTunnel *) message;
-  h->tunnels_cb (h->tunnel_cls,
-                 &msg->destination,
-                 ntohl (msg->channels),
-                 ntohl (msg->connections),
-                 ntohl (msg->estate),
-                 ntohl (msg->cstate));
+  h->info_cb.tunnels_cb (h->info_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.
+/**
+ * Process a local tunnel info reply, pass info to the user.
  *
  * @param h Mesh handle.
  * @param message Message itself.
@@ -1078,41 +1105,55 @@ process_get_tunnel (struct GNUNET_MESH_Handle *h,
 {
   struct GNUNET_MESH_LocalInfoTunnel *msg;
   size_t esize;
+  size_t msize;
+  unsigned int ch_n;
+  unsigned int c_n;
+  struct GNUNET_HashCode *conns;
+  MESH_ChannelNumber *chns;
 
-  GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Get Tunnel messasge received\n");
-
-  if (NULL == h->tunnel_cb)
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Get Tunnel messasge received\n");
+  if (NULL == h->info_cb.tunnel_cb)
   {
-    GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "  ignored\n");
+    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "  ignored\n");
     return;
   }
 
   /* Verify message sanity */
   msg = (struct GNUNET_MESH_LocalInfoTunnel *) message;
-  esize = sizeof (struct GNUNET_MESH_LocalInfo);
-  if (ntohs (message->size) != esize)
+  msize = ntohs (message->size);
+  esize = sizeof (struct GNUNET_MESH_LocalInfoTunnel);
+  if (esize > msize)
   {
     GNUNET_break_op (0);
-    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
-                "Get Tunnel message: size %hu - expected %u\n",
-                ntohs (message->size),
-                esize);
-
-    h->tunnel_cb (h->tunnel_cls, NULL, 0, 0, 0, 0);
-    h->tunnel_cb = NULL;
-    h->tunnel_cls = NULL;
-
-    return;
+    h->info_cb.tunnel_cb (h->info_cls, NULL, 0, 0, NULL, NULL, 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->info_cb.tunnel_cb (h->info_cls, NULL, 0, 0, NULL, NULL, 0, 0);
+    goto clean_cls;
+  }
+
+  /* Call Callback with tunnel info. */
+  conns = (struct GNUNET_HashCode *) &msg[1];
+  chns = (MESH_ChannelNumber *) &conns[c_n];
+  h->info_cb.tunnel_cb (h->info_cls, &msg->destination,
+                ch_n, c_n, chns, conns,
+                ntohs (msg->estate), ntohs (msg->cstate));
 
-  h->tunnel_cb (h->tunnel_cls,
-                &msg->destination,
-                ntohl (msg->channels),
-                ntohl (msg->connections),
-                ntohl (msg->estate),
-                ntohl (msg->cstate));
-  h->tunnel_cb = NULL;
-  h->tunnel_cls = NULL;
+clean_cls:
+  h->info_cb.tunnel_cb = NULL;
+  h->info_cls = NULL;
 }
 
 /**
@@ -1146,7 +1187,7 @@ msg_received (void *cls, const struct GNUNET_MessageHeader *msg)
     break;
     /* Notify of a channel disconnection */
   case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY: /* TODO separate(gid problem)*/
-  case GNUNET_MESSAGE_TYPE_MESH_LOCAL_NACK:
+  case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_NACK:
     process_channel_destroy (h, (struct GNUNET_MESH_ChannelMessage *) msg);
     break;
   case GNUNET_MESSAGE_TYPE_MESH_LOCAL_DATA:
@@ -1161,6 +1202,9 @@ msg_received (void *cls, const struct GNUNET_MessageHeader *msg)
 //   case GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_CHANNEL:
 //     process_show_channel (h, msg);
 //     break;
+  case GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_PEERS:
+    process_get_peers (h, msg);
+    break;
   case GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS:
     process_get_tunnels (h, msg);
     break;
@@ -1175,7 +1219,7 @@ msg_received (void *cls, const struct GNUNET_MessageHeader *msg)
     LOG (GNUNET_ERROR_TYPE_WARNING,
          "unsolicited message form service (type %s)\n",
          GM_m2s (ntohs (msg->type)));
-  }  FPRINTF (stdout, "\n");
+  }
   LOG (GNUNET_ERROR_TYPE_DEBUG, "message processed\n");
   if (GNUNET_YES == h->in_receive)
   {
@@ -1449,6 +1493,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_PEER:
+      case GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_PEERS:
       case GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNEL:
       case GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS:
         break;
@@ -1684,76 +1730,86 @@ send_info_request (struct GNUNET_MESH_Handle *h, uint16_t 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,
- * listing all active peers that blong to the channel.
+ * Request information about peers known to the running mesh service.
+ * The callback will be called for every peer known to the service.
+ * Only one info request (of any kind) can be active at once.
  *
- * 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.
+ *
+ * @return #GNUNET_OK / #GNUNET_SYSERR
  */
-void
-GNUNET_MESH_get_channels (struct GNUNET_MESH_Handle *h,
-                         GNUNET_MESH_ChannelsCB callback,
-                         void *callback_cls)
+int
+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_CHANNELS);
-  h->channels_cb = callback;
-  h->channels_cls = callback_cls;
+  if (NULL != h->info_cb.peers_cb)
+  {
+    GNUNET_break (0);
+    return GNUNET_SYSERR;
+  }
+  send_info_request (h, GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_PEERS);
+  h->info_cb.peers_cb = callback;
+  h->info_cls = callback_cls;
+  return GNUNET_OK;
 }
 
 
 /**
- * Cancel a monitor request. The monitor callback will not be called.
+ * Cancel a peer info request. The callback will not be called (anymore).
  *
  * WARNING: unstable API, likely to change in the future!
  *
  * @param h Mesh handle.
  *
- * @return Closure given to GNUNET_MESH_monitor, if any.
+ * @return Closure given to GNUNET_MESH_get_peers.
  */
 void *
-GNUNET_MESH_get_channels_cancel (struct GNUNET_MESH_Handle *h)
+GNUNET_MESH_get_peers_cancel (struct GNUNET_MESH_Handle *h)
 {
   void *cls;
 
-  cls = h->channels_cls;
-  h->channels_cb = NULL;
-  h->channels_cls = NULL;
+  cls = h->info_cls;
+  h->info_cb.peers_cb = NULL;
+  h->info_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.
+ * Request information about tunnels of the running mesh peer.
+ * The callback will be called for every tunnel of the service.
+ * Only one info request (of any kind) can be active at once.
  *
  * 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.
+ *
+ * @return #GNUNET_OK / #GNUNET_SYSERR
  */
-void
+int
 GNUNET_MESH_get_tunnels (struct GNUNET_MESH_Handle *h,
                          GNUNET_MESH_TunnelsCB callback,
                          void *callback_cls)
 {
+  if (NULL != h->info_cb.tunnels_cb)
+  {
+    GNUNET_break (0);
+    return GNUNET_SYSERR;
+  }
   send_info_request (h, GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS);
-  h->tunnels_cb = callback;
-  h->tunnels_cls = callback_cls;
+  h->info_cb.tunnels_cb = callback;
+  h->info_cls = callback_cls;
+  return GNUNET_OK;
 }
 
 
@@ -1762,16 +1818,16 @@ GNUNET_MESH_get_tunnels (struct GNUNET_MESH_Handle *h,
  *
  * @param h Mesh handle.
  *
- * @return Closure given to GNUNET_MESH_monitor, if any.
+ * @return Closure given to GNUNET_MESH_get_tunnels.
  */
 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;
+  h->info_cb.tunnels_cb = NULL;
+  cls = h->info_cls;
+  h->info_cls = NULL;
 
   return cls;
 }
@@ -1779,21 +1835,20 @@ GNUNET_MESH_get_tunnels_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.
+ * Request information about a tunnel of the running mesh peer.
+ * The callback will be called for the tunnel once.
+ * Only one info request (of any kind) can be active at once.
  *
  * WARNING: unstable API, likely to change in the future!
  *
  * @param h Handle to the mesh peer.
+ * @param id Peer whose tunnel to examine.
  * @param callback Function to call with the requested data.
  * @param callback_cls Closure for @c callback.
+ *
+ * @return #GNUNET_OK / #GNUNET_SYSERR
  */
-void
+int
 GNUNET_MESH_get_tunnel (struct GNUNET_MESH_Handle *h,
                         const struct GNUNET_PeerIdentity *id,
                         GNUNET_MESH_TunnelCB callback,
@@ -1801,13 +1856,20 @@ GNUNET_MESH_get_tunnel (struct GNUNET_MESH_Handle *h,
 {
   struct GNUNET_MESH_LocalInfo msg;
 
+  if (NULL != h->info_cb.tunnel_cb)
+  {
+    GNUNET_break (0);
+    return GNUNET_SYSERR;
+  }
+
   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;
+  msg.peer = *id;
   send_packet (h, &msg.header, NULL);
-  h->tunnel_cb = callback;
-  h->tunnel_cls = callback_cls;
+  h->info_cb.tunnel_cb = callback;
+  h->info_cls = callback_cls;
+  return GNUNET_OK;
 }
 
 
@@ -1822,8 +1884,10 @@ GNUNET_MESH_get_tunnel (struct GNUNET_MESH_Handle *h,
  * @param channel_number Channel number.
  * @param callback Function to call with the requested data.
  * @param callback_cls Closure for @c callback.
+ *
+ * @return #GNUNET_OK / #GNUNET_SYSERR
  */
-void
+int
 GNUNET_MESH_show_channel (struct GNUNET_MESH_Handle *h,
                          struct GNUNET_PeerIdentity *initiator,
                          unsigned int channel_number,
@@ -1832,14 +1896,21 @@ GNUNET_MESH_show_channel (struct GNUNET_MESH_Handle *h,
 {
   struct GNUNET_MESH_LocalInfo msg;
 
+  if (NULL != h->info_cb.channel_cb)
+  {
+    GNUNET_break (0);
+    return GNUNET_SYSERR;
+  }
+
   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;
   send_packet (h, &msg.header, NULL);
-  h->channel_cb = callback;
-  h->channel_cls = callback_cls;
+  h->info_cb.channel_cb = callback;
+  h->info_cls = callback_cls;
+  return GNUNET_OK;
 }