- use variables to cound ch, conns
[oweals/gnunet.git] / src / mesh / mesh_api.c
index 46dfe546fac5f1cff48770b9ea9f0fbf8af27d16..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;
-
 };
 
 
@@ -279,7 +302,7 @@ struct GNUNET_MESH_Channel
     /**
      * Channel options: reliability, etc.
      */
-  enum MeshOption options;
+  enum GNUNET_MESH_ChannelOption options;
 
     /**
      * Are we allowed to send to the service?
@@ -411,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)
@@ -839,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");
+  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",
+  LOG (GNUNET_ERROR_TYPE_DEBUG, "  %s data on channel %s [%X]\n",
+       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 */
@@ -858,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)
     {
@@ -903,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);
@@ -914,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);
   }
 }
 
@@ -932,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");
 //
@@ -942,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,
@@ -972,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");
@@ -984,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);
@@ -1007,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
  *
@@ -1038,7 +1166,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:
@@ -1047,10 +1175,19 @@ 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:
@@ -1251,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;
@@ -1332,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);
@@ -1382,7 +1521,7 @@ GNUNET_MESH_channel_create (struct GNUNET_MESH_Handle *h,
                             void *channel_ctx,
                             const struct GNUNET_PeerIdentity *peer,
                             uint32_t port,
-                            enum MeshOption options)
+                            enum GNUNET_MESH_ChannelOption options)
 {
   struct GNUNET_MESH_Channel *ch;
   struct GNUNET_MESH_ChannelMessage msg;
@@ -1457,7 +1596,7 @@ 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;
@@ -1504,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);
@@ -1555,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,
@@ -1575,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;
 }
@@ -1588,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.
@@ -1604,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.
  *
@@ -1623,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;