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 * - document NEW API implementation
28 * - add timeout for iteration
29 * - implement cancellation of iteration
32 #include "gnunet_client_lib.h"
33 #include "gnunet_container_lib.h"
34 #include "gnunet_peerinfo_service.h"
35 #include "gnunet_protocols.h"
36 #include "gnunet_time_lib.h"
42 typedef void (*TransmissionContinuation)(void *cls,
49 struct TransmissionQueueEntry
54 struct TransmissionQueueEntry *next;
59 struct TransmissionQueueEntry *prev;
64 TransmissionContinuation cont;
74 struct GNUNET_TIME_Absolute timeout;
85 * Handle to the peerinfo service.
87 struct GNUNET_PEERINFO_Handle
92 const struct GNUNET_CONFIGURATION_Handle *cfg;
97 struct GNUNET_SCHEDULER_Handle *sched;
100 * Connection to the service.
102 struct GNUNET_CLIENT_Connection *client;
107 struct TransmissionQueueEntry *tq_head;
112 struct TransmissionQueueEntry *tq_tail;
117 struct GNUNET_CLIENT_TransmitHandle *th;
122 * Connect to the peerinfo service.
124 * @param cfg configuration to use
125 * @param sched scheduler to use
126 * @return NULL on error (configuration related, actual connection
127 * etablishment may happen asynchronously).
129 struct GNUNET_PEERINFO_Handle *
130 GNUNET_PEERINFO_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
131 struct GNUNET_SCHEDULER_Handle *sched)
133 struct GNUNET_CLIENT_Connection *client;
134 struct GNUNET_PEERINFO_Handle *ret;
136 client = GNUNET_CLIENT_connect (sched, "peerinfo", cfg);
139 ret = GNUNET_malloc (sizeof (struct GNUNET_PEERINFO_Handle));
140 ret->client = client;
148 * Disconnect from the peerinfo service. Note that all iterators must
149 * have completed or have been cancelled by the time this function is
150 * called (otherwise, calling this function is a serious error).
151 * Furthermore, if 'GNUNET_PEERINFO_add_peer' operations are still
152 * pending, they will be cancelled silently on disconnect.
154 * @param h handle to disconnect
157 GNUNET_PEERINFO_disconnect (struct GNUNET_PEERINFO_Handle *h)
159 struct TransmissionQueueEntry *tqe;
161 while (NULL != (tqe = h->tq_head))
163 GNUNET_CONTAINER_DLL_remove (h->tq_head,
166 if (tqe->cont != NULL)
167 tqe->cont (tqe->cont_cls, GNUNET_SYSERR);
170 GNUNET_CLIENT_disconnect (h->client, GNUNET_NO);
179 trigger_transmit (struct GNUNET_PEERINFO_Handle *h);
186 reconnect (struct GNUNET_PEERINFO_Handle *h)
188 GNUNET_CLIENT_disconnect (h->client, GNUNET_SYSERR);
189 h->client = GNUNET_CLIENT_connect (h->sched, "client", h->cfg);
190 GNUNET_assert (h->client != NULL);
198 do_transmit (void *cls, size_t size, void *buf)
200 struct GNUNET_PEERINFO_Handle *h = cls;
201 struct TransmissionQueueEntry *tqe = h->tq_head;
208 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
210 ("Failed to transmit message of type %u to `%s' service.\n"),
211 ntohs (msg->type), "peerinfo");
213 GNUNET_CONTAINER_DLL_remove (h->tq_head,
217 trigger_transmit (h);
218 if (tqe->cont != NULL)
219 tqe->cont (tqe->cont_cls, GNUNET_SYSERR);
224 GNUNET_assert (size >= ret);
225 memcpy (buf, &tqe[1], ret);
226 GNUNET_CONTAINER_DLL_remove (h->tq_head,
229 if (tqe->cont != NULL)
230 tqe->cont (tqe->cont_cls, GNUNET_OK);
232 trigger_transmit (h);
239 trigger_transmit (struct GNUNET_PEERINFO_Handle *h)
241 struct TransmissionQueueEntry *tqe;
243 if (NULL == (tqe = h->tq_head))
247 h->th = GNUNET_CLIENT_notify_transmit_ready (h->client,
249 GNUNET_TIME_absolute_get_remaining (tqe->timeout),
256 * Add a host to the persistent list. This method operates in
257 * semi-reliable mode: if the transmission is not completed by
258 * the time 'GNUNET_PEERINFO_disconnect' is called, it will be
259 * aborted. Furthermore, if a second HELLO is added for the
260 * same peer before the first one was transmitted, PEERINFO may
261 * merge the two HELLOs prior to transmission to the service.
263 * @param h handle to the peerinfo service
264 * @param peer identity of the peer
265 * @param hello the verified (!) HELLO message
268 GNUNET_PEERINFO_add_peer_new (struct GNUNET_PEERINFO_Handle *h,
269 const struct GNUNET_HELLO_Message *hello)
271 uint16_t hs = GNUNET_HELLO_size (hello);
272 struct TransmissionQueueEntry *tqe;
275 struct GNUNET_PeerIdentity peer;
276 GNUNET_assert (GNUNET_OK == GNUNET_HELLO_get_id (hello, &peer));
277 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
278 "Adding peer `%s' to PEERINFO database (%u bytes of `%s')\n",
283 tqe = GNUNET_malloc (sizeof (struct TransmissionQueueEntry) + hs);
285 tqe->timeout = GNUNET_TIME_UNIT_FOREVER_ABS;
286 memcpy (&tqe[1], hello, hs);
287 GNUNET_CONTAINER_DLL_insert_after (h->tq_head,
291 trigger_transmit (h);
299 struct GNUNET_PEERINFO_NewIteratorContext
304 struct GNUNET_PEERINFO_Handle *h;
309 GNUNET_PEERINFO_Processor callback;
319 struct GNUNET_TIME_Absolute timeout;
325 * Type of a function to call when we receive a message
329 * @param msg message received, NULL on timeout or fatal error
332 peerinfo_handler (void *cls, const struct GNUNET_MessageHeader *msg)
334 struct GNUNET_PEERINFO_NewIteratorContext *ic = cls;
335 const struct InfoMessage *im;
336 const struct GNUNET_HELLO_Message *hello;
341 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
342 _("Failed to receive response from `%s' service.\n"),
345 trigger_transmit (ic->h);
346 ic->callback (ic->callback_cls, NULL, NULL, 1);
350 if (ntohs (msg->type) == GNUNET_MESSAGE_TYPE_PEERINFO_INFO_END)
353 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
354 "Received end of list of peers from peerinfo database\n");
356 trigger_transmit (ic->h);
357 ic->callback (ic->callback_cls, NULL, NULL, 0);
361 ms = ntohs (msg->size);
362 if ((ms < sizeof (struct InfoMessage)) ||
363 (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_PEERINFO_INFO))
367 trigger_transmit (ic->h);
368 ic->callback (ic->callback_cls, NULL, NULL, 2);
372 im = (const struct InfoMessage *) msg;
374 if (ms > sizeof (struct InfoMessage) + sizeof (struct GNUNET_MessageHeader))
376 hello = (const struct GNUNET_HELLO_Message *) &im[1];
377 if (ms != sizeof (struct InfoMessage) + GNUNET_HELLO_size (hello))
381 trigger_transmit (ic->h);
382 ic->callback (ic->callback_cls, NULL, NULL, 2);
388 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
389 "Received %u bytes of `%s' information about peer `%s' from PEERINFO database\n",
390 (hello == NULL) ? 0 : (unsigned int) GNUNET_HELLO_size (hello),
392 GNUNET_i2s (&im->peer));
394 ic->callback (ic->callback_cls, &im->peer, hello, ntohl (im->trust));
395 GNUNET_CLIENT_receive (ic->h->client,
398 GNUNET_TIME_absolute_get_remaining (ic->timeout));
406 iterator_start_receive (void *cls,
407 int transmit_success)
409 struct GNUNET_PEERINFO_NewIteratorContext *ic = cls;
411 if (GNUNET_OK != transmit_success)
413 ic->callback (ic->callback_cls, NULL, NULL, 2);
415 trigger_transmit (ic->h);
419 GNUNET_CLIENT_receive (ic->h->client,
422 GNUNET_TIME_absolute_get_remaining (ic->timeout));
427 * Call a method for each known matching host and change its trust
428 * value. The callback method will be invoked once for each matching
429 * host and then finally once with a NULL pointer. After that final
430 * invocation, the iterator context must no longer be used.
432 * Note that the last call can be triggered by timeout or by simply
433 * being done; however, the trust argument will be set to zero if we
434 * are done, 1 if we timed out and 2 for fatal error.
436 * Instead of calling this function with 'peer == NULL' and 'trust ==
437 * 0', it is often better to use 'GNUNET_PEERINFO_notify'.
439 * @param h handle to the peerinfo service
440 * @param peer restrict iteration to this peer only (can be NULL)
441 * @param trust_delta how much to change the trust in all matching peers
442 * @param timeout how long to wait until timing out
443 * @param callback the method to call for each peer
444 * @param callback_cls closure for callback
445 * @return NULL on error (in this case, 'callback' is never called!),
446 * otherwise an iterator context
448 struct GNUNET_PEERINFO_NewIteratorContext *
449 GNUNET_PEERINFO_iterate_new (struct GNUNET_PEERINFO_Handle *h,
450 const struct GNUNET_PeerIdentity *peer,
452 struct GNUNET_TIME_Relative timeout,
453 GNUNET_PEERINFO_Processor callback,
456 struct ListAllPeersMessage *lapm;
457 struct ListPeerMessage *lpm;
458 struct GNUNET_PEERINFO_NewIteratorContext *ic;
459 struct TransmissionQueueEntry *tqe;
462 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
463 "Requesting list of peers from peerinfo database\n");
467 tqe = GNUNET_malloc (sizeof (struct TransmissionQueueEntry) +
468 sizeof (struct ListAllPeersMessage));
469 tqe->size = sizeof (struct ListAllPeersMessage);
470 lapm = (struct ListAllPeersMessage *) &tqe[1];
471 lapm->header.size = htons (sizeof (struct ListAllPeersMessage));
472 lapm->header.type = htons (GNUNET_MESSAGE_TYPE_PEERINFO_GET_ALL);
473 lapm->trust_change = htonl (trust_delta);
477 tqe = GNUNET_malloc (sizeof (struct TransmissionQueueEntry) +
478 sizeof (struct ListPeerMessage));
479 tqe->size = sizeof (struct ListPeerMessage);
480 lpm = (struct ListPeerMessage *) &tqe[1];
481 lpm->header.size = htons (sizeof (struct ListPeerMessage));
482 lpm->header.type = htons (GNUNET_MESSAGE_TYPE_PEERINFO_GET);
483 lpm->trust_change = htonl (trust_delta);
484 memcpy (&lpm->peer, peer, sizeof (struct GNUNET_PeerIdentity));
486 ic = GNUNET_malloc (sizeof (struct GNUNET_PEERINFO_NewIteratorContext));
487 ic->callback = callback;
488 ic->callback_cls = callback_cls;
489 ic->timeout = GNUNET_TIME_relative_to_absolute (timeout);
490 tqe->timeout = ic->timeout;
491 tqe->cont = &iterator_start_receive;
493 /* FIXME: sort DLL by timeout? */
494 /* FIXME: add timeout task!? */
495 GNUNET_CONTAINER_DLL_insert_after (h->tq_head,
499 trigger_transmit (h);
506 * Cancel an iteration over peer information.
508 * @param ic context of the iterator to cancel
511 GNUNET_PEERINFO_iterate_cancel_new (struct GNUNET_PEERINFO_NewIteratorContext *ic)
514 // FIXME: not implemented
521 /* ***************************** OLD API ****************************** */
526 #define ADD_PEER_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30)
531 struct GNUNET_CLIENT_Connection *client;
532 struct GNUNET_MessageHeader *msg;
537 copy_and_free (void *cls, size_t size, void *buf)
539 struct CAFContext *cc = cls;
540 struct GNUNET_MessageHeader *msg = cc->msg;
546 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
548 ("Failed to transmit message of type %u to `%s' service.\n"),
549 ntohs (msg->type), "peerinfo");
552 GNUNET_CLIENT_disconnect (cc->client, GNUNET_NO);
556 msize = ntohs (msg->size);
557 GNUNET_assert (size >= msize);
558 memcpy (buf, msg, msize);
560 GNUNET_CLIENT_disconnect (cc->client, GNUNET_YES);
568 * Add a host to the persistent list.
570 * @param cfg configuration to use
571 * @param sched scheduler to use
572 * @param peer identity of the peer
573 * @param hello the verified (!) HELLO message
576 GNUNET_PEERINFO_add_peer (const struct GNUNET_CONFIGURATION_Handle *cfg,
577 struct GNUNET_SCHEDULER_Handle *sched,
578 const struct GNUNET_PeerIdentity *peer,
579 const struct GNUNET_HELLO_Message *hello)
581 struct GNUNET_CLIENT_Connection *client;
582 struct PeerAddMessage *pam;
584 struct CAFContext *cc;
587 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
588 "Adding peer `%s' to peerinfo database\n",
591 client = GNUNET_CLIENT_connect (sched, "peerinfo", cfg);
594 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
595 _("Could not connect to `%s' service.\n"), "peerinfo");
598 hs = GNUNET_HELLO_size (hello);
600 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
601 "Size of `%s' is %u bytes\n",
603 (unsigned int) GNUNET_HELLO_size (hello));
605 pam = GNUNET_malloc (sizeof (struct PeerAddMessage) + hs);
606 pam->header.size = htons (hs + sizeof (struct PeerAddMessage));
607 pam->header.type = htons (GNUNET_MESSAGE_TYPE_PEERINFO_ADD);
608 memcpy (&pam->peer, peer, sizeof (struct GNUNET_PeerIdentity));
609 memcpy (&pam[1], hello, hs);
610 cc = GNUNET_malloc (sizeof (struct CAFContext));
612 cc->msg = &pam->header;
613 GNUNET_CLIENT_notify_transmit_ready (client,
614 ntohs (pam->header.size),
622 * Context for the info handler.
624 struct GNUNET_PEERINFO_IteratorContext
628 * Our connection to the PEERINFO service.
630 struct GNUNET_CLIENT_Connection *client;
633 * Function to call with information.
635 GNUNET_PEERINFO_Processor callback;
638 * Closure for callback.
643 * When should we time out?
645 struct GNUNET_TIME_Absolute timeout;
651 * Type of a function to call when we receive a message
655 * @param msg message received, NULL on timeout or fatal error
658 info_handler (void *cls, const struct GNUNET_MessageHeader *msg)
660 struct GNUNET_PEERINFO_IteratorContext *ic = cls;
661 const struct InfoMessage *im;
662 const struct GNUNET_HELLO_Message *hello;
667 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
668 _("Failed to receive response from `%s' service.\n"),
670 ic->callback (ic->callback_cls, NULL, NULL, 1);
671 GNUNET_CLIENT_disconnect (ic->client, GNUNET_NO);
675 if (ntohs (msg->type) == GNUNET_MESSAGE_TYPE_PEERINFO_INFO_END)
678 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
679 "Received end of list of peers from peerinfo database\n");
681 ic->callback (ic->callback_cls, NULL, NULL, 0);
682 GNUNET_CLIENT_disconnect (ic->client, GNUNET_NO);
686 ms = ntohs (msg->size);
687 if ((ms < sizeof (struct InfoMessage)) ||
688 (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_PEERINFO_INFO))
691 ic->callback (ic->callback_cls, NULL, NULL, 2);
692 GNUNET_CLIENT_disconnect (ic->client, GNUNET_NO);
696 im = (const struct InfoMessage *) msg;
698 if (ms > sizeof (struct InfoMessage) + sizeof (struct GNUNET_MessageHeader))
700 hello = (const struct GNUNET_HELLO_Message *) &im[1];
701 if (ms != sizeof (struct InfoMessage) + GNUNET_HELLO_size (hello))
704 ic->callback (ic->callback_cls, NULL, NULL, 2);
705 GNUNET_CLIENT_disconnect (ic->client, GNUNET_NO);
711 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
712 "Received %u bytes of `%s' information about peer `%s' from PEERINFO database\n",
713 (hello == NULL) ? 0 : (unsigned int) GNUNET_HELLO_size (hello),
715 GNUNET_i2s (&im->peer));
717 ic->callback (ic->callback_cls, &im->peer, hello, ntohl (im->trust));
718 GNUNET_CLIENT_receive (ic->client,
721 GNUNET_TIME_absolute_get_remaining (ic->timeout));
726 * Call a method for each known matching host and change
727 * its trust value. The method will be invoked once for
728 * each host and then finally once with a NULL pointer.
729 * Note that the last call can be triggered by timeout or
730 * by simply being done; however, the trust argument will
731 * be set to zero if we are done and to 1 if we timed out.
733 * @param cfg configuration to use
734 * @param sched scheduler to use
735 * @param peer restrict iteration to this peer only (can be NULL)
736 * @param trust_delta how much to change the trust in all matching peers
737 * @param timeout how long to wait until timing out
738 * @param callback the method to call for each peer
739 * @param callback_cls closure for callback
740 * @return NULL on error, otherwise an iterator context
742 struct GNUNET_PEERINFO_IteratorContext *
743 GNUNET_PEERINFO_iterate (const struct GNUNET_CONFIGURATION_Handle *cfg,
744 struct GNUNET_SCHEDULER_Handle *sched,
745 const struct GNUNET_PeerIdentity *peer,
747 struct GNUNET_TIME_Relative timeout,
748 GNUNET_PEERINFO_Processor callback,
751 struct GNUNET_CLIENT_Connection *client;
752 struct ListAllPeersMessage *lapm;
753 struct ListPeerMessage *lpm;
754 struct GNUNET_PEERINFO_IteratorContext *ihc;
756 client = GNUNET_CLIENT_connect (sched, "peerinfo", cfg);
759 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
760 _("Could not connect to `%s' service.\n"), "peerinfo");
764 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
765 "Requesting list of peers from peerinfo database\n");
769 ihc = GNUNET_malloc (sizeof (struct GNUNET_PEERINFO_IteratorContext) +
770 sizeof (struct ListAllPeersMessage));
771 lapm = (struct ListAllPeersMessage *) &ihc[1];
772 lapm->header.size = htons (sizeof (struct ListAllPeersMessage));
773 lapm->header.type = htons (GNUNET_MESSAGE_TYPE_PEERINFO_GET_ALL);
774 lapm->trust_change = htonl (trust_delta);
778 ihc = GNUNET_malloc (sizeof (struct GNUNET_PEERINFO_IteratorContext) +
779 sizeof (struct ListPeerMessage));
780 lpm = (struct ListPeerMessage *) &ihc[1];
781 lpm->header.size = htons (sizeof (struct ListPeerMessage));
782 lpm->header.type = htons (GNUNET_MESSAGE_TYPE_PEERINFO_GET);
783 lpm->trust_change = htonl (trust_delta);
784 memcpy (&lpm->peer, peer, sizeof (struct GNUNET_PeerIdentity));
786 ihc->client = client;
787 ihc->callback = callback;
788 ihc->callback_cls = callback_cls;
789 ihc->timeout = GNUNET_TIME_relative_to_absolute (timeout);
791 GNUNET_CLIENT_transmit_and_get_response (client,
792 (const struct GNUNET_MessageHeader*) &ihc[1],
799 GNUNET_CLIENT_disconnect (ihc->client, GNUNET_NO);
808 * Cancel an iteration over peer information.
810 * @param ic context of the iterator to cancel
813 GNUNET_PEERINFO_iterate_cancel (struct GNUNET_PEERINFO_IteratorContext *ic)
815 GNUNET_CLIENT_disconnect (ic->client, GNUNET_NO);
820 /* end of peerinfo_api.c */