2 This file is part of GNUnet.
3 (C) 2001, 2002, 2004, 2005, 2007, 2009 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 2, 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 peerinfo/peerinfo_api.c
23 * @brief API to access peerinfo service
24 * @author Christian Grothoff
27 #include "gnunet_client_lib.h"
28 #include "gnunet_peerinfo_service.h"
29 #include "gnunet_protocols.h"
30 #include "gnunet_time_lib.h"
36 * Handle to the peerinfo service.
38 struct GNUNET_PEERINFO_Handle
44 * Connect to the peerinfo service.
46 * @param cfg configuration to use
47 * @param sched scheduler to use
48 * @return NULL on error (configuration related, actual connection
49 * etablishment may happen asynchronously).
51 struct GNUNET_PEERINFO_Handle *
52 GNUNET_PEERINFO_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
53 struct GNUNET_SCHEDULER_Handle *sched)
60 * Disconnect from the peerinfo service. Note that all iterators must
61 * have completed or have been cancelled by the time this function is
62 * called (otherwise, calling this function is a serious error).
63 * Furthermore, if 'GNUNET_PEERINFO_add_peer' operations are still
64 * pending, they will be cancelled silently on disconnect.
66 * @param h handle to disconnect
69 GNUNET_PEERINFO_disconnect (struct GNUNET_PEERINFO_Handle *h)
79 * Add a host to the persistent list. This method operates in
80 * semi-reliable mode: if the transmission is not completed by
81 * the time 'GNUNET_PEERINFO_disconnect' is called, it will be
82 * aborted. Furthermore, if a second HELLO is added for the
83 * same peer before the first one was transmitted, PEERINFO may
84 * merge the two HELLOs prior to transmission to the service.
86 * @param h handle to the peerinfo service
87 * @param peer identity of the peer
88 * @param hello the verified (!) HELLO message
91 GNUNET_PEERINFO_add_peer_new (struct GNUNET_PEERINFO_Handle *h,
92 const struct GNUNET_PeerIdentity *peer,
93 const struct GNUNET_HELLO_Message *hello)
98 struct GNUNET_PEERINFO_NewIteratorContext
104 * Call a method for each known matching host and change its trust
105 * value. The callback method will be invoked once for each matching
106 * host and then finally once with a NULL pointer. After that final
107 * invocation, the iterator context must no longer be used.
109 * Note that the last call can be triggered by timeout or by simply
110 * being done; however, the trust argument will be set to zero if we
111 * are done, 1 if we timed out and 2 for fatal error.
113 * Instead of calling this function with 'peer == NULL' and 'trust ==
114 * 0', it is often better to use 'GNUNET_PEERINFO_notify'.
116 * @param h handle to the peerinfo service
117 * @param peer restrict iteration to this peer only (can be NULL)
118 * @param trust_delta how much to change the trust in all matching peers
119 * @param timeout how long to wait until timing out
120 * @param callback the method to call for each peer
121 * @param callback_cls closure for callback
122 * @return NULL on error (in this case, 'callback' is never called!),
123 * otherwise an iterator context
125 struct GNUNET_PEERINFO_NewIteratorContext *
126 GNUNET_PEERINFO_iterate_new (struct GNUNET_PEERINFO_Handle *h,
127 const struct GNUNET_PeerIdentity *peer,
129 struct GNUNET_TIME_Relative timeout,
130 GNUNET_PEERINFO_Processor callback,
139 * Cancel an iteration over peer information.
141 * @param ic context of the iterator to cancel
144 GNUNET_PEERINFO_iterate_cancel_new (struct GNUNET_PEERINFO_NewIteratorContext *ic)
152 /* ***************************** OLD API ****************************** */
157 #define ADD_PEER_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30)
162 struct GNUNET_CLIENT_Connection *client;
163 struct GNUNET_MessageHeader *msg;
168 copy_and_free (void *cls, size_t size, void *buf)
170 struct CAFContext *cc = cls;
171 struct GNUNET_MessageHeader *msg = cc->msg;
177 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
179 ("Failed to transmit message of type %u to `%s' service.\n"),
180 ntohs (msg->type), "peerinfo");
183 GNUNET_CLIENT_disconnect (cc->client, GNUNET_NO);
187 msize = ntohs (msg->size);
188 GNUNET_assert (size >= msize);
189 memcpy (buf, msg, msize);
191 GNUNET_CLIENT_disconnect (cc->client, GNUNET_YES);
199 * Add a host to the persistent list.
201 * @param cfg configuration to use
202 * @param sched scheduler to use
203 * @param peer identity of the peer
204 * @param hello the verified (!) HELLO message
207 GNUNET_PEERINFO_add_peer (const struct GNUNET_CONFIGURATION_Handle *cfg,
208 struct GNUNET_SCHEDULER_Handle *sched,
209 const struct GNUNET_PeerIdentity *peer,
210 const struct GNUNET_HELLO_Message *hello)
212 struct GNUNET_CLIENT_Connection *client;
213 struct PeerAddMessage *pam;
215 struct CAFContext *cc;
218 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
219 "Adding peer `%s' to peerinfo database\n",
222 client = GNUNET_CLIENT_connect (sched, "peerinfo", cfg);
225 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
226 _("Could not connect to `%s' service.\n"), "peerinfo");
229 hs = GNUNET_HELLO_size (hello);
231 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
232 "Size of `%s' is %u bytes\n",
234 (unsigned int) GNUNET_HELLO_size (hello));
236 pam = GNUNET_malloc (sizeof (struct PeerAddMessage) + hs);
237 pam->header.size = htons (hs + sizeof (struct PeerAddMessage));
238 pam->header.type = htons (GNUNET_MESSAGE_TYPE_PEERINFO_ADD);
239 memcpy (&pam->peer, peer, sizeof (struct GNUNET_PeerIdentity));
240 memcpy (&pam[1], hello, hs);
241 cc = GNUNET_malloc (sizeof (struct CAFContext));
243 cc->msg = &pam->header;
244 GNUNET_CLIENT_notify_transmit_ready (client,
245 ntohs (pam->header.size),
253 * Context for the info handler.
255 struct GNUNET_PEERINFO_IteratorContext
259 * Our connection to the PEERINFO service.
261 struct GNUNET_CLIENT_Connection *client;
264 * Function to call with information.
266 GNUNET_PEERINFO_Processor callback;
269 * Closure for callback.
274 * When should we time out?
276 struct GNUNET_TIME_Absolute timeout;
282 * Type of a function to call when we receive a message
286 * @param msg message received, NULL on timeout or fatal error
289 info_handler (void *cls, const struct GNUNET_MessageHeader *msg)
291 struct GNUNET_PEERINFO_IteratorContext *ic = cls;
292 const struct InfoMessage *im;
293 const struct GNUNET_HELLO_Message *hello;
298 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
299 _("Failed to receive response from `%s' service.\n"),
301 ic->callback (ic->callback_cls, NULL, NULL, 1);
302 GNUNET_CLIENT_disconnect (ic->client, GNUNET_NO);
306 if (ntohs (msg->type) == GNUNET_MESSAGE_TYPE_PEERINFO_INFO_END)
309 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
310 "Received end of list of peers from peerinfo database\n");
312 ic->callback (ic->callback_cls, NULL, NULL, 0);
313 GNUNET_CLIENT_disconnect (ic->client, GNUNET_NO);
317 ms = ntohs (msg->size);
318 if ((ms < sizeof (struct InfoMessage)) ||
319 (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_PEERINFO_INFO))
322 ic->callback (ic->callback_cls, NULL, NULL, 2);
323 GNUNET_CLIENT_disconnect (ic->client, GNUNET_NO);
327 im = (const struct InfoMessage *) msg;
329 if (ms > sizeof (struct InfoMessage) + sizeof (struct GNUNET_MessageHeader))
331 hello = (const struct GNUNET_HELLO_Message *) &im[1];
332 if (ms != sizeof (struct InfoMessage) + GNUNET_HELLO_size (hello))
335 ic->callback (ic->callback_cls, NULL, NULL, 2);
336 GNUNET_CLIENT_disconnect (ic->client, GNUNET_NO);
342 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
343 "Received %u bytes of `%s' information about peer `%s' from PEERINFO database\n",
344 (hello == NULL) ? 0 : (unsigned int) GNUNET_HELLO_size (hello),
346 GNUNET_i2s (&im->peer));
348 ic->callback (ic->callback_cls, &im->peer, hello, ntohl (im->trust));
349 GNUNET_CLIENT_receive (ic->client,
352 GNUNET_TIME_absolute_get_remaining (ic->timeout));
357 * Call a method for each known matching host and change
358 * its trust value. The method will be invoked once for
359 * each host and then finally once with a NULL pointer.
360 * Note that the last call can be triggered by timeout or
361 * by simply being done; however, the trust argument will
362 * be set to zero if we are done and to 1 if we timed out.
364 * @param cfg configuration to use
365 * @param sched scheduler to use
366 * @param peer restrict iteration to this peer only (can be NULL)
367 * @param trust_delta how much to change the trust in all matching peers
368 * @param timeout how long to wait until timing out
369 * @param callback the method to call for each peer
370 * @param callback_cls closure for callback
371 * @return NULL on error, otherwise an iterator context
373 struct GNUNET_PEERINFO_IteratorContext *
374 GNUNET_PEERINFO_iterate (const struct GNUNET_CONFIGURATION_Handle *cfg,
375 struct GNUNET_SCHEDULER_Handle *sched,
376 const struct GNUNET_PeerIdentity *peer,
378 struct GNUNET_TIME_Relative timeout,
379 GNUNET_PEERINFO_Processor callback,
382 struct GNUNET_CLIENT_Connection *client;
383 struct ListAllPeersMessage *lapm;
384 struct ListPeerMessage *lpm;
385 struct GNUNET_PEERINFO_IteratorContext *ihc;
387 client = GNUNET_CLIENT_connect (sched, "peerinfo", cfg);
390 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
391 _("Could not connect to `%s' service.\n"), "peerinfo");
395 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
396 "Requesting list of peers from peerinfo database\n");
400 ihc = GNUNET_malloc (sizeof (struct GNUNET_PEERINFO_IteratorContext) +
401 sizeof (struct ListAllPeersMessage));
402 lapm = (struct ListAllPeersMessage *) &ihc[1];
403 lapm->header.size = htons (sizeof (struct ListAllPeersMessage));
404 lapm->header.type = htons (GNUNET_MESSAGE_TYPE_PEERINFO_GET_ALL);
405 lapm->trust_change = htonl (trust_delta);
409 ihc = GNUNET_malloc (sizeof (struct GNUNET_PEERINFO_IteratorContext) +
410 sizeof (struct ListPeerMessage));
411 lpm = (struct ListPeerMessage *) &ihc[1];
412 lpm->header.size = htons (sizeof (struct ListPeerMessage));
413 lpm->header.type = htons (GNUNET_MESSAGE_TYPE_PEERINFO_GET);
414 lpm->trust_change = htonl (trust_delta);
415 memcpy (&lpm->peer, peer, sizeof (struct GNUNET_PeerIdentity));
417 ihc->client = client;
418 ihc->callback = callback;
419 ihc->callback_cls = callback_cls;
420 ihc->timeout = GNUNET_TIME_relative_to_absolute (timeout);
422 GNUNET_CLIENT_transmit_and_get_response (client,
423 (const struct GNUNET_MessageHeader*) &ihc[1],
430 GNUNET_CLIENT_disconnect (ihc->client, GNUNET_NO);
439 * Cancel an iteration over peer information.
441 * @param ic context of the iterator to cancel
444 GNUNET_PEERINFO_iterate_cancel (struct GNUNET_PEERINFO_IteratorContext *ic)
446 GNUNET_CLIENT_disconnect (ic->client, GNUNET_NO);
451 /* end of peerinfo_api.c */