2 This file is part of GNUnet.
3 (C) 2009, 2010 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file core/core_api_iterate_peers.c
23 * @brief implementation of the peer_iterate function
24 * @author Christian Grothoff
25 * @author Nathan Evans
28 #include "gnunet_core_service.h"
32 struct GNUNET_CORE_RequestContext
35 * Our connection to the service.
37 struct GNUNET_CLIENT_Connection *client;
40 * Handle for transmitting a request.
42 struct GNUNET_CLIENT_TransmitHandle *th;
45 * Function called with the peer.
47 GNUNET_CORE_ConnectEventHandler peer_cb;
52 struct GNUNET_PeerIdentity *peer;
55 * Closure for peer_cb.
63 * Receive reply from core service with information about a peer.
65 * @param cls our 'struct GNUNET_CORE_RequestContext *'
66 * @param msg NULL on error or last entry
69 receive_info (void *cls, const struct GNUNET_MessageHeader *msg)
71 struct GNUNET_CORE_RequestContext *request_context = cls;
72 const struct ConnectNotifyMessage *connect_message;
76 /* Handle last message or error case, disconnect and clean up */
78 ((ntohs (msg->type) == GNUNET_MESSAGE_TYPE_CORE_ITERATE_PEERS_END) &&
79 (ntohs (msg->size) == sizeof (struct GNUNET_MessageHeader))))
81 if (request_context->peer_cb != NULL)
82 request_context->peer_cb (request_context->cb_cls, NULL, NULL, 0);
83 GNUNET_CLIENT_disconnect (request_context->client, GNUNET_NO);
84 GNUNET_free (request_context);
88 msize = ntohs (msg->size);
89 /* Handle incorrect message type or size, disconnect and clean up */
90 if ((ntohs (msg->type) != GNUNET_MESSAGE_TYPE_CORE_NOTIFY_CONNECT) ||
91 (msize < sizeof (struct ConnectNotifyMessage)))
94 if (request_context->peer_cb != NULL)
95 request_context->peer_cb (request_context->cb_cls, NULL, NULL, 0);
96 GNUNET_CLIENT_disconnect (request_context->client, GNUNET_NO);
97 GNUNET_free (request_context);
100 connect_message = (const struct ConnectNotifyMessage *) msg;
101 ats_count = ntohl (connect_message->ats_count);
103 sizeof (struct ConnectNotifyMessage) +
104 ats_count * sizeof (struct GNUNET_ATS_Information))
107 if (request_context->peer_cb != NULL)
108 request_context->peer_cb (request_context->cb_cls, NULL, NULL, 0);
109 GNUNET_CLIENT_disconnect (request_context->client, GNUNET_NO);
110 GNUNET_free (request_context);
114 if (request_context->peer_cb != NULL)
115 request_context->peer_cb (request_context->cb_cls, &connect_message->peer,
116 (const struct GNUNET_ATS_Information *) &connect_message[1],
118 GNUNET_CLIENT_receive (request_context->client, &receive_info,
119 request_context, GNUNET_TIME_UNIT_FOREVER_REL);
123 * Function called to notify a client about the socket
124 * begin ready to queue more data. "buf" will be
125 * NULL and "size" zero if the socket was closed for
126 * writing in the meantime.
129 * @param size number of bytes available in buf
130 * @param buf where the callee should write the message
131 * @return number of bytes written to buf
134 transmit_request (void *cls, size_t size, void *buf)
136 struct GNUNET_MessageHeader *msg;
137 struct GNUNET_PeerIdentity *peer = cls;
141 msize = sizeof (struct GNUNET_MessageHeader);
144 sizeof (struct GNUNET_MessageHeader) +
145 sizeof (struct GNUNET_PeerIdentity);
147 if ((size < msize) || (buf == NULL))
150 msg = (struct GNUNET_MessageHeader *) buf;
151 msg->size = htons (msize);
154 msg->type = htons (GNUNET_MESSAGE_TYPE_CORE_PEER_CONNECTED);
155 memcpy (&msg[1], peer, sizeof (struct GNUNET_PeerIdentity));
158 msg->type = htons (GNUNET_MESSAGE_TYPE_CORE_ITERATE_PEERS);
164 * Iterate over all currently connected peers.
165 * Calls peer_cb with each connected peer, and then
166 * once with NULL to indicate that all peers have
169 * @param cfg configuration to use
170 * @param peer the specific peer to check for
171 * @param peer_cb function to call with the peer information
172 * @param cb_cls closure for peer_cb
174 * @return GNUNET_OK if iterating, GNUNET_SYSERR on error
177 GNUNET_CORE_is_peer_connected (const struct GNUNET_CONFIGURATION_Handle *cfg,
178 struct GNUNET_PeerIdentity *peer,
179 GNUNET_CORE_ConnectEventHandler peer_cb,
182 struct GNUNET_CORE_RequestContext *request_context;
183 struct GNUNET_CLIENT_Connection *client;
185 client = GNUNET_CLIENT_connect ("core", cfg);
187 return GNUNET_SYSERR;
188 GNUNET_assert (peer != NULL);
189 request_context = GNUNET_malloc (sizeof (struct GNUNET_CORE_RequestContext));
190 request_context->client = client;
191 request_context->peer_cb = peer_cb;
192 request_context->cb_cls = cb_cls;
193 request_context->peer = peer;
195 request_context->th =
196 GNUNET_CLIENT_notify_transmit_ready (client,
197 sizeof (struct GNUNET_MessageHeader)
199 sizeof (struct GNUNET_PeerIdentity),
200 GNUNET_TIME_relative_get_forever (),
201 GNUNET_YES, &transmit_request, peer);
202 GNUNET_assert (request_context->th != NULL);
203 GNUNET_CLIENT_receive (client, &receive_info, request_context,
204 GNUNET_TIME_relative_get_forever ());
209 * Iterate over all currently connected peers.
210 * Calls peer_cb with each connected peer, and then
211 * once with NULL to indicate that all peers have
214 * @param cfg configuration to use
215 * @param peer_cb function to call with the peer information
216 * @param cb_cls closure for peer_cb
218 * @return GNUNET_OK if iterating, GNUNET_SYSERR on error
221 GNUNET_CORE_iterate_peers (const struct GNUNET_CONFIGURATION_Handle *cfg,
222 GNUNET_CORE_ConnectEventHandler peer_cb,
225 struct GNUNET_CORE_RequestContext *request_context;
226 struct GNUNET_CLIENT_Connection *client;
228 client = GNUNET_CLIENT_connect ("core", cfg);
230 return GNUNET_SYSERR;
231 request_context = GNUNET_malloc (sizeof (struct GNUNET_CORE_RequestContext));
232 request_context->client = client;
233 request_context->peer_cb = peer_cb;
234 request_context->cb_cls = cb_cls;
236 request_context->th =
237 GNUNET_CLIENT_notify_transmit_ready (client,
238 sizeof (struct GNUNET_MessageHeader),
239 GNUNET_TIME_relative_get_forever (),
240 GNUNET_YES, &transmit_request, NULL);
242 GNUNET_CLIENT_receive (client, &receive_info, request_context,
243 GNUNET_TIME_relative_get_forever ());
247 /* end of core_api_iterate_peers.c */