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.
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;
};
+/**
+ * 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.
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;
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),
- ntohs (msg->estate),
- ntohs (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.
{
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;
}
/**
// 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;
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)
{
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;
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;
}
*
* @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;
}
/**
- * 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,
{
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;
}
* @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,
{
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;
}