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"
33 #define ADD_PEER_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30)
38 struct GNUNET_CLIENT_Connection *client;
39 struct GNUNET_MessageHeader *msg;
44 copy_and_free (void *cls, size_t size, void *buf)
46 struct CAFContext *cc = cls;
47 struct GNUNET_MessageHeader *msg = cc->msg;
53 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
55 ("Failed to transmit message of type %u to `%s' service.\n"),
56 ntohs (msg->type), "peerinfo");
59 GNUNET_CLIENT_disconnect (cc->client, GNUNET_NO);
63 msize = ntohs (msg->size);
64 GNUNET_assert (size >= msize);
65 memcpy (buf, msg, msize);
67 GNUNET_CLIENT_disconnect (cc->client, GNUNET_YES);
75 * Add a host to the persistent list.
77 * @param cfg configuration to use
78 * @param sched scheduler to use
79 * @param peer identity of the peer
80 * @param hello the verified (!) HELLO message
83 GNUNET_PEERINFO_add_peer (const struct GNUNET_CONFIGURATION_Handle *cfg,
84 struct GNUNET_SCHEDULER_Handle *sched,
85 const struct GNUNET_PeerIdentity *peer,
86 const struct GNUNET_HELLO_Message *hello)
88 struct GNUNET_CLIENT_Connection *client;
89 struct PeerAddMessage *pam;
91 struct CAFContext *cc;
94 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
95 "Adding peer `%s' to peerinfo database\n",
98 client = GNUNET_CLIENT_connect (sched, "peerinfo", cfg);
101 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
102 _("Could not connect to `%s' service.\n"), "peerinfo");
105 hs = GNUNET_HELLO_size (hello);
107 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
108 "Size of `%s' is %u bytes\n",
110 (unsigned int) GNUNET_HELLO_size (hello));
112 pam = GNUNET_malloc (sizeof (struct PeerAddMessage) + hs);
113 pam->header.size = htons (hs + sizeof (struct PeerAddMessage));
114 pam->header.type = htons (GNUNET_MESSAGE_TYPE_PEERINFO_ADD);
115 memcpy (&pam->peer, peer, sizeof (struct GNUNET_PeerIdentity));
116 memcpy (&pam[1], hello, hs);
117 cc = GNUNET_malloc (sizeof (struct CAFContext));
119 cc->msg = &pam->header;
120 GNUNET_CLIENT_notify_transmit_ready (client,
121 ntohs (pam->header.size),
129 * Context for the info handler.
131 struct GNUNET_PEERINFO_IteratorContext
135 * Our connection to the PEERINFO service.
137 struct GNUNET_CLIENT_Connection *client;
140 * Function to call with information.
142 GNUNET_PEERINFO_Processor callback;
145 * Closure for callback.
150 * When should we time out?
152 struct GNUNET_TIME_Absolute timeout;
158 * Type of a function to call when we receive a message
162 * @param msg message received, NULL on timeout or fatal error
165 info_handler (void *cls, const struct GNUNET_MessageHeader *msg)
167 struct GNUNET_PEERINFO_IteratorContext *ic = cls;
168 const struct InfoMessage *im;
169 const struct GNUNET_HELLO_Message *hello;
174 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
175 _("Failed to receive response from `%s' service.\n"),
177 ic->callback (ic->callback_cls, NULL, NULL, 1);
178 GNUNET_CLIENT_disconnect (ic->client, GNUNET_NO);
182 if (ntohs (msg->type) == GNUNET_MESSAGE_TYPE_PEERINFO_INFO_END)
185 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
186 "Received end of list of peers from peerinfo database\n");
188 ic->callback (ic->callback_cls, NULL, NULL, 0);
189 GNUNET_CLIENT_disconnect (ic->client, GNUNET_NO);
193 ms = ntohs (msg->size);
194 if ((ms < sizeof (struct InfoMessage)) ||
195 (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_PEERINFO_INFO))
198 ic->callback (ic->callback_cls, NULL, NULL, 2);
199 GNUNET_CLIENT_disconnect (ic->client, GNUNET_NO);
203 im = (const struct InfoMessage *) msg;
205 if (ms > sizeof (struct InfoMessage) + sizeof (struct GNUNET_MessageHeader))
207 hello = (const struct GNUNET_HELLO_Message *) &im[1];
208 if (ms != sizeof (struct InfoMessage) + GNUNET_HELLO_size (hello))
211 ic->callback (ic->callback_cls, NULL, NULL, 2);
212 GNUNET_CLIENT_disconnect (ic->client, GNUNET_NO);
218 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
219 "Received %u bytes of `%s' information about peer `%s' from PEERINFO database\n",
220 (hello == NULL) ? 0 : (unsigned int) GNUNET_HELLO_size (hello),
222 GNUNET_i2s (&im->peer));
224 ic->callback (ic->callback_cls, &im->peer, hello, ntohl (im->trust));
225 GNUNET_CLIENT_receive (ic->client,
228 GNUNET_TIME_absolute_get_remaining (ic->timeout));
233 * Call a method for each known matching host and change
234 * its trust value. The method will be invoked once for
235 * each host and then finally once with a NULL pointer.
236 * Note that the last call can be triggered by timeout or
237 * by simply being done; however, the trust argument will
238 * be set to zero if we are done and to 1 if we timed out.
240 * @param cfg configuration to use
241 * @param sched scheduler to use
242 * @param peer restrict iteration to this peer only (can be NULL)
243 * @param trust_delta how much to change the trust in all matching peers
244 * @param timeout how long to wait until timing out
245 * @param callback the method to call for each peer
246 * @param callback_cls closure for callback
247 * @return NULL on error, otherwise an iterator context
249 struct GNUNET_PEERINFO_IteratorContext *
250 GNUNET_PEERINFO_iterate (const struct GNUNET_CONFIGURATION_Handle *cfg,
251 struct GNUNET_SCHEDULER_Handle *sched,
252 const struct GNUNET_PeerIdentity *peer,
254 struct GNUNET_TIME_Relative timeout,
255 GNUNET_PEERINFO_Processor callback,
258 struct GNUNET_CLIENT_Connection *client;
259 struct ListAllPeersMessage *lapm;
260 struct ListPeerMessage *lpm;
261 struct GNUNET_PEERINFO_IteratorContext *ihc;
263 client = GNUNET_CLIENT_connect (sched, "peerinfo", cfg);
266 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
267 _("Could not connect to `%s' service.\n"), "peerinfo");
271 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
272 "Requesting list of peers from peerinfo database\n");
276 ihc = GNUNET_malloc (sizeof (struct GNUNET_PEERINFO_IteratorContext) +
277 sizeof (struct ListAllPeersMessage));
278 lapm = (struct ListAllPeersMessage *) &ihc[1];
279 lapm->header.size = htons (sizeof (struct ListAllPeersMessage));
280 lapm->header.type = htons (GNUNET_MESSAGE_TYPE_PEERINFO_GET_ALL);
281 lapm->trust_change = htonl (trust_delta);
285 ihc = GNUNET_malloc (sizeof (struct GNUNET_PEERINFO_IteratorContext) +
286 sizeof (struct ListPeerMessage));
287 lpm = (struct ListPeerMessage *) &ihc[1];
288 lpm->header.size = htons (sizeof (struct ListPeerMessage));
289 lpm->header.type = htons (GNUNET_MESSAGE_TYPE_PEERINFO_GET);
290 lpm->trust_change = htonl (trust_delta);
291 memcpy (&lpm->peer, peer, sizeof (struct GNUNET_PeerIdentity));
293 ihc->client = client;
294 ihc->callback = callback;
295 ihc->callback_cls = callback_cls;
296 ihc->timeout = GNUNET_TIME_relative_to_absolute (timeout);
298 GNUNET_CLIENT_transmit_and_get_response (client,
299 (const struct GNUNET_MessageHeader*) &ihc[1],
306 GNUNET_CLIENT_disconnect (ihc->client, GNUNET_NO);
315 * Cancel an iteration over peer information.
317 * @param ic context of the iterator to cancel
320 GNUNET_PEERINFO_iterate_cancel (struct GNUNET_PEERINFO_IteratorContext *ic)
322 GNUNET_CLIENT_disconnect (ic->client, GNUNET_NO);
328 * Context for the info handler.
330 struct GNUNET_PEERINFO_NotifyContext
334 * Our connection to the PEERINFO service.
336 struct GNUNET_CLIENT_Connection *client;
339 * Function to call with information.
341 GNUNET_PEERINFO_Processor callback;
344 * Closure for callback.
349 * Handle to our initial request for message transmission to
350 * the peerinfo service.
352 struct GNUNET_CLIENT_TransmitHandle *init;
357 const struct GNUNET_CONFIGURATION_Handle *cfg;
362 struct GNUNET_SCHEDULER_Handle *sched;
367 * Send a request to the peerinfo service to start being
368 * notified about all changes to peer information.
370 * @param nc our context
373 request_notifications (struct GNUNET_PEERINFO_NotifyContext *nc);
377 * Read notifications from the client handle and pass them
380 * @param nc our context
383 receive_notifications (struct GNUNET_PEERINFO_NotifyContext *nc);
387 * Receive a peerinfo information message, process it and
391 * @param msg message received, NULL on timeout or fatal error
394 process_notification (void *cls,
396 GNUNET_MessageHeader * msg)
398 struct GNUNET_PEERINFO_NotifyContext *nc = cls;
399 const struct InfoMessage *im;
400 const struct GNUNET_HELLO_Message *hello;
405 GNUNET_CLIENT_disconnect (nc->client, GNUNET_NO);
406 nc->client = GNUNET_CLIENT_connect (nc->sched, "peerinfo", nc->cfg);
407 request_notifications (nc);
410 ms = ntohs (msg->size);
411 if ((ms < sizeof (struct InfoMessage)) ||
412 (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_PEERINFO_INFO))
415 GNUNET_CLIENT_disconnect (nc->client, GNUNET_NO);
416 nc->client = GNUNET_CLIENT_connect (nc->sched, "peerinfo", nc->cfg);
417 request_notifications (nc);
420 im = (const struct InfoMessage *) msg;
422 if (ms > sizeof (struct InfoMessage) + sizeof (struct GNUNET_MessageHeader))
424 hello = (const struct GNUNET_HELLO_Message *) &im[1];
425 if (ms != sizeof (struct InfoMessage) + GNUNET_HELLO_size (hello))
428 GNUNET_CLIENT_disconnect (nc->client, GNUNET_NO);
429 nc->client = GNUNET_CLIENT_connect (nc->sched, "peerinfo", nc->cfg);
430 request_notifications (nc);
435 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
436 "Received information about peer `%s' from peerinfo database\n",
437 GNUNET_i2s (&im->peer));
439 nc->callback (nc->callback_cls, &im->peer, hello, ntohl (im->trust));
440 receive_notifications (nc);
445 * Read notifications from the client handle and pass them
448 * @param nc our context
451 receive_notifications (struct GNUNET_PEERINFO_NotifyContext *nc)
453 GNUNET_CLIENT_receive (nc->client,
454 &process_notification,
456 GNUNET_TIME_UNIT_FOREVER_REL);
461 * Transmit our init-notify request, start receiving.
463 * @param cls closure (our 'struct GNUNET_PEERINFO_NotifyContext')
464 * @param size number of bytes available in buf
465 * @param buf where the callee should write the message
466 * @return number of bytes written to buf
469 transmit_notify_request (void *cls,
473 struct GNUNET_PEERINFO_NotifyContext *nc = cls;
474 struct GNUNET_MessageHeader hdr;
479 GNUNET_CLIENT_disconnect (nc->client, GNUNET_NO);
480 nc->client = GNUNET_CLIENT_connect (nc->sched, "peerinfo", nc->cfg);
481 request_notifications (nc);
484 GNUNET_assert (size >= sizeof (struct GNUNET_MessageHeader));
485 hdr.size = htons (sizeof (struct GNUNET_MessageHeader));
486 hdr.type = htons (GNUNET_MESSAGE_TYPE_PEERINFO_NOTIFY);
487 memcpy (buf, &hdr, sizeof (struct GNUNET_MessageHeader));
488 receive_notifications (nc);
489 return sizeof (struct GNUNET_MessageHeader);
494 * Send a request to the peerinfo service to start being
495 * notified about all changes to peer information.
497 * @param nc our context
500 request_notifications (struct GNUNET_PEERINFO_NotifyContext *nc)
502 GNUNET_assert (NULL == nc->init);
503 nc->init =GNUNET_CLIENT_notify_transmit_ready (nc->client,
504 sizeof (struct GNUNET_MessageHeader),
505 GNUNET_TIME_UNIT_FOREVER_REL,
507 &transmit_notify_request,
513 * Call a method whenever our known information about peers
514 * changes. Initially calls the given function for all known
515 * peers and then only signals changes.
517 * @param cfg configuration to use
518 * @param sched scheduler to use
519 * @param callback the method to call for each peer
520 * @param callback_cls closure for callback
521 * @return NULL on error
523 struct GNUNET_PEERINFO_NotifyContext *
524 GNUNET_PEERINFO_notify (const struct GNUNET_CONFIGURATION_Handle *cfg,
525 struct GNUNET_SCHEDULER_Handle *sched,
526 GNUNET_PEERINFO_Processor callback,
529 struct GNUNET_PEERINFO_NotifyContext *nc;
530 struct GNUNET_CLIENT_Connection *client;
532 client = GNUNET_CLIENT_connect (sched, "peerinfo", cfg);
535 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
536 _("Could not connect to `%s' service.\n"), "peerinfo");
539 nc = GNUNET_malloc (sizeof (struct GNUNET_PEERINFO_NotifyContext));
543 nc->callback = callback;
544 nc->callback_cls = callback_cls;
545 request_notifications (nc);
551 * Stop notifying about changes.
553 * @param nc context to stop notifying
556 GNUNET_PEERINFO_notify_cancel (struct GNUNET_PEERINFO_NotifyContext *nc)
558 if (NULL != nc->init)
560 GNUNET_CLIENT_notify_transmit_ready_cancel (nc->init);
563 GNUNET_CLIENT_disconnect (nc->client, GNUNET_NO);
568 /* end of peerinfo_api.c */