struct GNUNET_CORE_RequestContext
{
-
/**
* Our connection to the service.
*/
struct GNUNET_CLIENT_Connection *client;
/**
+
* Handle for transmitting a request.
*/
struct GNUNET_CLIENT_TransmitHandle *th;
*/
GNUNET_CORE_ConnectEventHandler peer_cb;
+ /**
+ * Peer to check for.
+ */
+ struct GNUNET_PeerIdentity *peer;
+
/**
* Closure for peer_cb.
*/
* @param msg NULL on error or last entry
*/
static void
-receive_info (void *cls,
- const struct GNUNET_MessageHeader *msg)
+receive_info (void *cls, const struct GNUNET_MessageHeader *msg)
{
struct GNUNET_CORE_RequestContext *request_context = cls;
const struct ConnectNotifyMessage *connect_message;
- uint32_t ats_count;
uint16_t msize;
/* Handle last message or error case, disconnect and clean up */
- if ( (msg == NULL) ||
+ if ((msg == NULL) ||
((ntohs (msg->type) == GNUNET_MESSAGE_TYPE_CORE_ITERATE_PEERS_END) &&
- (ntohs (msg->size) == sizeof (struct GNUNET_MessageHeader))) )
- {
- if (request_context->peer_cb != NULL)
- request_context->peer_cb (request_context->cb_cls,
- NULL, NULL);
- GNUNET_CLIENT_disconnect (request_context->client, GNUNET_NO);
- GNUNET_free (request_context);
- return;
- }
+ (ntohs (msg->size) == sizeof (struct GNUNET_MessageHeader))))
+ {
+ if (request_context->peer_cb != NULL)
+ request_context->peer_cb (request_context->cb_cls, NULL);
+ GNUNET_CLIENT_disconnect (request_context->client);
+ GNUNET_free (request_context);
+ return;
+ }
msize = ntohs (msg->size);
/* Handle incorrect message type or size, disconnect and clean up */
- if ( (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_CORE_NOTIFY_CONNECT) ||
- (msize < sizeof (struct ConnectNotifyMessage)) )
- {
- GNUNET_break (0);
- if (request_context->peer_cb != NULL)
- request_context->peer_cb (request_context->cb_cls,
- NULL, NULL);
- GNUNET_CLIENT_disconnect (request_context->client, GNUNET_NO);
- GNUNET_free (request_context);
- return;
- }
+ if ((ntohs (msg->type) != GNUNET_MESSAGE_TYPE_CORE_NOTIFY_CONNECT) ||
+ (msize < sizeof (struct ConnectNotifyMessage)))
+ {
+ GNUNET_break (0);
+ if (request_context->peer_cb != NULL)
+ request_context->peer_cb (request_context->cb_cls, NULL);
+ GNUNET_CLIENT_disconnect (request_context->client);
+ GNUNET_free (request_context);
+ return;
+ }
connect_message = (const struct ConnectNotifyMessage *) msg;
- ats_count = ntohl (connect_message->ats_count);
- if ( (msize != sizeof (struct ConnectNotifyMessage) + ats_count * sizeof (struct GNUNET_TRANSPORT_ATS_Information)) ||
- (GNUNET_TRANSPORT_ATS_ARRAY_TERMINATOR != ntohl ((&connect_message->ats)[ats_count].type)) )
- {
- GNUNET_break (0);
- if (request_context->peer_cb != NULL)
- request_context->peer_cb (request_context->cb_cls,
- NULL, NULL);
- GNUNET_CLIENT_disconnect (request_context->client, GNUNET_NO);
- GNUNET_free (request_context);
- return;
- }
+ if (msize != sizeof (struct ConnectNotifyMessage))
+ {
+ GNUNET_break (0);
+ if (request_context->peer_cb != NULL)
+ request_context->peer_cb (request_context->cb_cls, NULL);
+ GNUNET_CLIENT_disconnect (request_context->client);
+ GNUNET_free (request_context);
+ return;
+ }
/* Normal case */
if (request_context->peer_cb != NULL)
- request_context->peer_cb (request_context->cb_cls,
- &connect_message->peer,
- &connect_message->ats);
- GNUNET_CLIENT_receive(request_context->client,
- &receive_info,
- request_context,
- GNUNET_TIME_UNIT_FOREVER_REL);
+ request_context->peer_cb (request_context->cb_cls, &connect_message->peer);
+ GNUNET_CLIENT_receive (request_context->client, &receive_info,
+ request_context, GNUNET_TIME_UNIT_FOREVER_REL);
}
+
/**
* Function called to notify a client about the socket
* begin ready to queue more data. "buf" will be
* NULL and "size" zero if the socket was closed for
* writing in the meantime.
*
- * @param cls closure
+ * @param cls closure, always NULL
* @param size number of bytes available in buf
* @param buf where the callee should write the message
* @return number of bytes written to buf
*/
static size_t
-transmit_request(void *cls,
- size_t size, void *buf)
+transmit_request (void *cls, size_t size, void *buf)
{
struct GNUNET_MessageHeader *msg;
- if ((size < sizeof(struct GNUNET_MessageHeader)) || (buf == NULL))
- return 0;
+ int msize;
- msg = (struct GNUNET_MessageHeader *)buf;
- msg->size = htons (sizeof (struct GNUNET_MessageHeader));
+ msize = sizeof (struct GNUNET_MessageHeader);
+ if ((size < msize) || (buf == NULL))
+ return 0;
+ msg = (struct GNUNET_MessageHeader *) buf;
+ msg->size = htons (msize);
msg->type = htons (GNUNET_MESSAGE_TYPE_CORE_ITERATE_PEERS);
- return sizeof(struct GNUNET_MessageHeader);
+
+ return msize;
}
+
/**
- * Obtain statistics and/or change preferences for the given peer.
+ * Iterate over all currently connected peers.
+ * Calls peer_cb with each connected peer, and then
+ * once with NULL to indicate that all peers have
+ * been handled.
*
- * @param sched scheduler to use
* @param cfg configuration to use
* @param peer_cb function to call with the peer information
- * @param cb_cls closure for peer_cb
- * @return GNUNET_OK if iterating, GNUNET_SYSERR on error
+ * @param cb_cls closure for @a peer_cb
+ * @return #GNUNET_OK if iterating, #GNUNET_SYSERR on error
*/
int
GNUNET_CORE_iterate_peers (const struct GNUNET_CONFIGURATION_Handle *cfg,
client = GNUNET_CLIENT_connect ("core", cfg);
if (client == NULL)
return GNUNET_SYSERR;
- request_context = GNUNET_malloc (sizeof (struct GNUNET_CORE_RequestContext));
+ request_context = GNUNET_new (struct GNUNET_CORE_RequestContext);
request_context->client = client;
request_context->peer_cb = peer_cb;
request_context->cb_cls = cb_cls;
- request_context->th = GNUNET_CLIENT_notify_transmit_ready(client,
- sizeof(struct GNUNET_MessageHeader),
- GNUNET_TIME_relative_get_forever(),
- GNUNET_YES,
- &transmit_request,
- NULL);
+ request_context->th =
+ GNUNET_CLIENT_notify_transmit_ready (client,
+ sizeof (struct GNUNET_MessageHeader),
+ GNUNET_TIME_UNIT_FOREVER_REL,
+ GNUNET_YES, &transmit_request, NULL);
- GNUNET_CLIENT_receive(client, &receive_info, request_context, GNUNET_TIME_relative_get_forever());
+ GNUNET_CLIENT_receive (client, &receive_info, request_context,
+ GNUNET_TIME_UNIT_FOREVER_REL);
return GNUNET_OK;
}