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
30 * - prevent transmit during receive!
33 #include "gnunet_client_lib.h"
34 #include "gnunet_container_lib.h"
35 #include "gnunet_peerinfo_service.h"
36 #include "gnunet_protocols.h"
37 #include "gnunet_time_lib.h"
41 * Function to call after transmission has succeeded.
44 * @param success GNUNET_OK if transmission worked, GNUNET_SYSERR on error
46 typedef void (*TransmissionContinuation)(void *cls,
51 * Entry in the transmission queue to PEERINFO service.
53 struct TransmissionQueueEntry
56 * This is a linked list.
58 struct TransmissionQueueEntry *next;
61 * This is a linked list.
63 struct TransmissionQueueEntry *prev;
66 * Function to call after request has been transmitted, or NULL (in which
67 * case we must consider sending the next entry immediately).
69 TransmissionContinuation cont;
77 * When this request times out.
79 struct GNUNET_TIME_Absolute timeout;
82 * Number of bytes of the request message (follows after this struct).
90 * Handle to the peerinfo service.
92 struct GNUNET_PEERINFO_Handle
97 const struct GNUNET_CONFIGURATION_Handle *cfg;
102 struct GNUNET_SCHEDULER_Handle *sched;
105 * Connection to the service.
107 struct GNUNET_CLIENT_Connection *client;
110 * Head of transmission queue.
112 struct TransmissionQueueEntry *tq_head;
115 * Tail of transmission queue.
117 struct TransmissionQueueEntry *tq_tail;
120 * Handle for the current transmission request, or NULL if none is pending.
122 struct GNUNET_CLIENT_TransmitHandle *th;
128 * Connect to the peerinfo service.
130 * @param cfg configuration to use
131 * @param sched scheduler to use
132 * @return NULL on error (configuration related, actual connection
133 * etablishment may happen asynchronously).
135 struct GNUNET_PEERINFO_Handle *
136 GNUNET_PEERINFO_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
137 struct GNUNET_SCHEDULER_Handle *sched)
139 struct GNUNET_CLIENT_Connection *client;
140 struct GNUNET_PEERINFO_Handle *ret;
142 client = GNUNET_CLIENT_connect (sched, "peerinfo", cfg);
145 ret = GNUNET_malloc (sizeof (struct GNUNET_PEERINFO_Handle));
146 ret->client = client;
154 * Disconnect from the peerinfo service. Note that all iterators must
155 * have completed or have been cancelled by the time this function is
156 * called (otherwise, calling this function is a serious error).
157 * Furthermore, if 'GNUNET_PEERINFO_add_peer' operations are still
158 * pending, they will be cancelled silently on disconnect.
160 * @param h handle to disconnect
163 GNUNET_PEERINFO_disconnect (struct GNUNET_PEERINFO_Handle *h)
165 struct TransmissionQueueEntry *tqe;
167 while (NULL != (tqe = h->tq_head))
169 GNUNET_CONTAINER_DLL_remove (h->tq_head,
172 if (tqe->cont != NULL)
173 tqe->cont (tqe->cont_cls, GNUNET_SYSERR);
176 GNUNET_CLIENT_disconnect (h->client, GNUNET_NO);
182 * Check if we have a request pending in the transmission queue and are
183 * able to transmit it right now. If so, schedule transmission.
185 * @param h handle to the service
188 trigger_transmit (struct GNUNET_PEERINFO_Handle *h);
192 * Close the existing connection to PEERINFO and reconnect.
194 * @param h handle to the service
197 reconnect (struct GNUNET_PEERINFO_Handle *h)
199 GNUNET_CLIENT_disconnect (h->client, GNUNET_SYSERR);
200 h->client = GNUNET_CLIENT_connect (h->sched, "client", h->cfg);
201 GNUNET_assert (h->client != NULL);
206 * Transmit the request at the head of the transmission queue
207 * and trigger continuation (if any).
209 * @param cls the 'struct GNUNET_PEERINFO_Handle' (with the queue)
210 * @param size size of the buffer (0 on error)
211 * @param buf where to copy the message
212 * @return number of bytes copied to buf
215 do_transmit (void *cls, size_t size, void *buf)
217 struct GNUNET_PEERINFO_Handle *h = cls;
218 struct TransmissionQueueEntry *tqe = h->tq_head;
225 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
227 ("Failed to transmit message of type %u to `%s' service.\n"),
228 ntohs (msg->type), "peerinfo");
230 GNUNET_CONTAINER_DLL_remove (h->tq_head,
234 trigger_transmit (h);
235 if (tqe->cont != NULL)
236 tqe->cont (tqe->cont_cls, GNUNET_SYSERR);
241 GNUNET_assert (size >= ret);
242 memcpy (buf, &tqe[1], ret);
243 GNUNET_CONTAINER_DLL_remove (h->tq_head,
246 if (tqe->cont != NULL)
247 tqe->cont (tqe->cont_cls, GNUNET_OK);
249 trigger_transmit (h);
256 * Check if we have a request pending in the transmission queue and are
257 * able to transmit it right now. If so, schedule transmission.
259 * @param h handle to the service
262 trigger_transmit (struct GNUNET_PEERINFO_Handle *h)
264 struct TransmissionQueueEntry *tqe;
266 /* FIXME: need to handle case where we are still *receiving* (and then
267 do nothing here as well!) */
268 if (NULL == (tqe = h->tq_head))
272 h->th = GNUNET_CLIENT_notify_transmit_ready (h->client,
274 GNUNET_TIME_absolute_get_remaining (tqe->timeout),
281 * Add a host to the persistent list. This method operates in
282 * semi-reliable mode: if the transmission is not completed by
283 * the time 'GNUNET_PEERINFO_disconnect' is called, it will be
284 * aborted. Furthermore, if a second HELLO is added for the
285 * same peer before the first one was transmitted, PEERINFO may
286 * merge the two HELLOs prior to transmission to the service.
288 * @param h handle to the peerinfo service
289 * @param hello the verified (!) HELLO message
292 GNUNET_PEERINFO_add_peer_new (struct GNUNET_PEERINFO_Handle *h,
293 const struct GNUNET_HELLO_Message *hello)
295 uint16_t hs = GNUNET_HELLO_size (hello);
296 struct TransmissionQueueEntry *tqe;
299 struct GNUNET_PeerIdentity peer;
300 GNUNET_assert (GNUNET_OK == GNUNET_HELLO_get_id (hello, &peer));
301 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
302 "Adding peer `%s' to PEERINFO database (%u bytes of `%s')\n",
307 tqe = GNUNET_malloc (sizeof (struct TransmissionQueueEntry) + hs);
309 tqe->timeout = GNUNET_TIME_UNIT_FOREVER_ABS;
310 memcpy (&tqe[1], hello, hs);
311 GNUNET_CONTAINER_DLL_insert_after (h->tq_head,
315 trigger_transmit (h);
320 * Context for an iteration request.
322 struct GNUNET_PEERINFO_NewIteratorContext
325 * Handle to the PEERINFO service.
327 struct GNUNET_PEERINFO_Handle *h;
330 * Function to call with the results.
332 GNUNET_PEERINFO_Processor callback;
335 * Closure for 'callback'.
340 * Timeout for the operation.
342 struct GNUNET_TIME_Absolute timeout;
347 * Type of a function to call when we receive a message
351 * @param msg message received, NULL on timeout or fatal error
354 peerinfo_handler (void *cls, const struct GNUNET_MessageHeader *msg)
356 struct GNUNET_PEERINFO_NewIteratorContext *ic = cls;
357 const struct InfoMessage *im;
358 const struct GNUNET_HELLO_Message *hello;
363 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
364 _("Failed to receive response from `%s' service.\n"),
367 trigger_transmit (ic->h);
368 ic->callback (ic->callback_cls, NULL, NULL, 1);
372 if (ntohs (msg->type) == GNUNET_MESSAGE_TYPE_PEERINFO_INFO_END)
375 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
376 "Received end of list of peers from peerinfo database\n");
378 trigger_transmit (ic->h);
379 ic->callback (ic->callback_cls, NULL, NULL, 0);
383 ms = ntohs (msg->size);
384 if ((ms < sizeof (struct InfoMessage)) ||
385 (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_PEERINFO_INFO))
389 trigger_transmit (ic->h);
390 ic->callback (ic->callback_cls, NULL, NULL, 2);
394 im = (const struct InfoMessage *) msg;
396 if (ms > sizeof (struct InfoMessage) + sizeof (struct GNUNET_MessageHeader))
398 hello = (const struct GNUNET_HELLO_Message *) &im[1];
399 if (ms != sizeof (struct InfoMessage) + GNUNET_HELLO_size (hello))
403 trigger_transmit (ic->h);
404 ic->callback (ic->callback_cls, NULL, NULL, 2);
410 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
411 "Received %u bytes of `%s' information about peer `%s' from PEERINFO database\n",
412 (hello == NULL) ? 0 : (unsigned int) GNUNET_HELLO_size (hello),
414 GNUNET_i2s (&im->peer));
416 ic->callback (ic->callback_cls, &im->peer, hello, ntohl (im->trust));
417 GNUNET_CLIENT_receive (ic->h->client,
420 GNUNET_TIME_absolute_get_remaining (ic->timeout));
425 * We've transmitted the iteration request. Now get ready to process
426 * the results (or handle transmission error).
428 * @param cls the 'struct GNUNET_PEERINFO_NewIteratorContext'
429 * @param transmit_success GNUNET_OK if transmission worked
432 iterator_start_receive (void *cls,
433 int transmit_success)
435 struct GNUNET_PEERINFO_NewIteratorContext *ic = cls;
437 if (GNUNET_OK != transmit_success)
439 ic->callback (ic->callback_cls, NULL, NULL, 2);
441 trigger_transmit (ic->h);
445 GNUNET_CLIENT_receive (ic->h->client,
448 GNUNET_TIME_absolute_get_remaining (ic->timeout));
453 * Call a method for each known matching host and change its trust
454 * value. The callback method will be invoked once for each matching
455 * host and then finally once with a NULL pointer. After that final
456 * invocation, the iterator context must no longer be used.
458 * Note that the last call can be triggered by timeout or by simply
459 * being done; however, the trust argument will be set to zero if we
460 * are done, 1 if we timed out and 2 for fatal error.
462 * Instead of calling this function with 'peer == NULL' and 'trust ==
463 * 0', it is often better to use 'GNUNET_PEERINFO_notify'.
465 * @param h handle to the peerinfo service
466 * @param peer restrict iteration to this peer only (can be NULL)
467 * @param trust_delta how much to change the trust in all matching peers
468 * @param timeout how long to wait until timing out
469 * @param callback the method to call for each peer
470 * @param callback_cls closure for callback
471 * @return NULL on error (in this case, 'callback' is never called!),
472 * otherwise an iterator context
474 struct GNUNET_PEERINFO_NewIteratorContext *
475 GNUNET_PEERINFO_iterate_new (struct GNUNET_PEERINFO_Handle *h,
476 const struct GNUNET_PeerIdentity *peer,
478 struct GNUNET_TIME_Relative timeout,
479 GNUNET_PEERINFO_Processor callback,
482 struct ListAllPeersMessage *lapm;
483 struct ListPeerMessage *lpm;
484 struct GNUNET_PEERINFO_NewIteratorContext *ic;
485 struct TransmissionQueueEntry *tqe;
488 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
489 "Requesting list of peers from peerinfo database\n");
493 tqe = GNUNET_malloc (sizeof (struct TransmissionQueueEntry) +
494 sizeof (struct ListAllPeersMessage));
495 tqe->size = sizeof (struct ListAllPeersMessage);
496 lapm = (struct ListAllPeersMessage *) &tqe[1];
497 lapm->header.size = htons (sizeof (struct ListAllPeersMessage));
498 lapm->header.type = htons (GNUNET_MESSAGE_TYPE_PEERINFO_GET_ALL);
499 lapm->trust_change = htonl (trust_delta);
503 tqe = GNUNET_malloc (sizeof (struct TransmissionQueueEntry) +
504 sizeof (struct ListPeerMessage));
505 tqe->size = sizeof (struct ListPeerMessage);
506 lpm = (struct ListPeerMessage *) &tqe[1];
507 lpm->header.size = htons (sizeof (struct ListPeerMessage));
508 lpm->header.type = htons (GNUNET_MESSAGE_TYPE_PEERINFO_GET);
509 lpm->trust_change = htonl (trust_delta);
510 memcpy (&lpm->peer, peer, sizeof (struct GNUNET_PeerIdentity));
512 ic = GNUNET_malloc (sizeof (struct GNUNET_PEERINFO_NewIteratorContext));
513 ic->callback = callback;
514 ic->callback_cls = callback_cls;
515 ic->timeout = GNUNET_TIME_relative_to_absolute (timeout);
516 tqe->timeout = ic->timeout;
517 tqe->cont = &iterator_start_receive;
519 /* FIXME: sort DLL by timeout? */
520 /* FIXME: add timeout task!? */
521 GNUNET_CONTAINER_DLL_insert_after (h->tq_head,
525 trigger_transmit (h);
532 * Cancel an iteration over peer information.
534 * @param ic context of the iterator to cancel
537 GNUNET_PEERINFO_iterate_cancel_new (struct GNUNET_PEERINFO_NewIteratorContext *ic)
540 // FIXME: not implemented
547 /* ***************************** OLD API ****************************** */
552 #define ADD_PEER_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30)
557 struct GNUNET_CLIENT_Connection *client;
558 struct GNUNET_MessageHeader *msg;
563 copy_and_free (void *cls, size_t size, void *buf)
565 struct CAFContext *cc = cls;
566 struct GNUNET_MessageHeader *msg = cc->msg;
572 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
574 ("Failed to transmit message of type %u to `%s' service.\n"),
575 ntohs (msg->type), "peerinfo");
578 GNUNET_CLIENT_disconnect (cc->client, GNUNET_NO);
582 msize = ntohs (msg->size);
583 GNUNET_assert (size >= msize);
584 memcpy (buf, msg, msize);
586 GNUNET_CLIENT_disconnect (cc->client, GNUNET_YES);
594 * Add a host to the persistent list.
596 * @param cfg configuration to use
597 * @param sched scheduler to use
598 * @param peer identity of the peer
599 * @param hello the verified (!) HELLO message
602 GNUNET_PEERINFO_add_peer (const struct GNUNET_CONFIGURATION_Handle *cfg,
603 struct GNUNET_SCHEDULER_Handle *sched,
604 const struct GNUNET_PeerIdentity *peer,
605 const struct GNUNET_HELLO_Message *hello)
607 struct GNUNET_CLIENT_Connection *client;
608 struct PeerAddMessage *pam;
610 struct CAFContext *cc;
613 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
614 "Adding peer `%s' to peerinfo database\n",
617 client = GNUNET_CLIENT_connect (sched, "peerinfo", cfg);
620 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
621 _("Could not connect to `%s' service.\n"), "peerinfo");
624 hs = GNUNET_HELLO_size (hello);
626 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
627 "Size of `%s' is %u bytes\n",
629 (unsigned int) GNUNET_HELLO_size (hello));
631 pam = GNUNET_malloc (sizeof (struct PeerAddMessage) + hs);
632 pam->header.size = htons (hs + sizeof (struct PeerAddMessage));
633 pam->header.type = htons (GNUNET_MESSAGE_TYPE_PEERINFO_ADD);
634 memcpy (&pam->peer, peer, sizeof (struct GNUNET_PeerIdentity));
635 memcpy (&pam[1], hello, hs);
636 cc = GNUNET_malloc (sizeof (struct CAFContext));
638 cc->msg = &pam->header;
639 GNUNET_CLIENT_notify_transmit_ready (client,
640 ntohs (pam->header.size),
648 * Context for the info handler.
650 struct GNUNET_PEERINFO_IteratorContext
654 * Our connection to the PEERINFO service.
656 struct GNUNET_CLIENT_Connection *client;
659 * Function to call with information.
661 GNUNET_PEERINFO_Processor callback;
664 * Closure for callback.
669 * When should we time out?
671 struct GNUNET_TIME_Absolute timeout;
677 * Type of a function to call when we receive a message
681 * @param msg message received, NULL on timeout or fatal error
684 info_handler (void *cls, const struct GNUNET_MessageHeader *msg)
686 struct GNUNET_PEERINFO_IteratorContext *ic = cls;
687 const struct InfoMessage *im;
688 const struct GNUNET_HELLO_Message *hello;
693 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
694 _("Failed to receive response from `%s' service.\n"),
696 ic->callback (ic->callback_cls, NULL, NULL, 1);
697 GNUNET_CLIENT_disconnect (ic->client, GNUNET_NO);
701 if (ntohs (msg->type) == GNUNET_MESSAGE_TYPE_PEERINFO_INFO_END)
704 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
705 "Received end of list of peers from peerinfo database\n");
707 ic->callback (ic->callback_cls, NULL, NULL, 0);
708 GNUNET_CLIENT_disconnect (ic->client, GNUNET_NO);
712 ms = ntohs (msg->size);
713 if ((ms < sizeof (struct InfoMessage)) ||
714 (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_PEERINFO_INFO))
717 ic->callback (ic->callback_cls, NULL, NULL, 2);
718 GNUNET_CLIENT_disconnect (ic->client, GNUNET_NO);
722 im = (const struct InfoMessage *) msg;
724 if (ms > sizeof (struct InfoMessage) + sizeof (struct GNUNET_MessageHeader))
726 hello = (const struct GNUNET_HELLO_Message *) &im[1];
727 if (ms != sizeof (struct InfoMessage) + GNUNET_HELLO_size (hello))
730 ic->callback (ic->callback_cls, NULL, NULL, 2);
731 GNUNET_CLIENT_disconnect (ic->client, GNUNET_NO);
737 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
738 "Received %u bytes of `%s' information about peer `%s' from PEERINFO database\n",
739 (hello == NULL) ? 0 : (unsigned int) GNUNET_HELLO_size (hello),
741 GNUNET_i2s (&im->peer));
743 ic->callback (ic->callback_cls, &im->peer, hello, ntohl (im->trust));
744 GNUNET_CLIENT_receive (ic->client,
747 GNUNET_TIME_absolute_get_remaining (ic->timeout));
752 * Call a method for each known matching host and change
753 * its trust value. The method will be invoked once for
754 * each host and then finally once with a NULL pointer.
755 * Note that the last call can be triggered by timeout or
756 * by simply being done; however, the trust argument will
757 * be set to zero if we are done and to 1 if we timed out.
759 * @param cfg configuration to use
760 * @param sched scheduler to use
761 * @param peer restrict iteration to this peer only (can be NULL)
762 * @param trust_delta how much to change the trust in all matching peers
763 * @param timeout how long to wait until timing out
764 * @param callback the method to call for each peer
765 * @param callback_cls closure for callback
766 * @return NULL on error, otherwise an iterator context
768 struct GNUNET_PEERINFO_IteratorContext *
769 GNUNET_PEERINFO_iterate (const struct GNUNET_CONFIGURATION_Handle *cfg,
770 struct GNUNET_SCHEDULER_Handle *sched,
771 const struct GNUNET_PeerIdentity *peer,
773 struct GNUNET_TIME_Relative timeout,
774 GNUNET_PEERINFO_Processor callback,
777 struct GNUNET_CLIENT_Connection *client;
778 struct ListAllPeersMessage *lapm;
779 struct ListPeerMessage *lpm;
780 struct GNUNET_PEERINFO_IteratorContext *ihc;
782 client = GNUNET_CLIENT_connect (sched, "peerinfo", cfg);
785 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
786 _("Could not connect to `%s' service.\n"), "peerinfo");
790 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
791 "Requesting list of peers from peerinfo database\n");
795 ihc = GNUNET_malloc (sizeof (struct GNUNET_PEERINFO_IteratorContext) +
796 sizeof (struct ListAllPeersMessage));
797 lapm = (struct ListAllPeersMessage *) &ihc[1];
798 lapm->header.size = htons (sizeof (struct ListAllPeersMessage));
799 lapm->header.type = htons (GNUNET_MESSAGE_TYPE_PEERINFO_GET_ALL);
800 lapm->trust_change = htonl (trust_delta);
804 ihc = GNUNET_malloc (sizeof (struct GNUNET_PEERINFO_IteratorContext) +
805 sizeof (struct ListPeerMessage));
806 lpm = (struct ListPeerMessage *) &ihc[1];
807 lpm->header.size = htons (sizeof (struct ListPeerMessage));
808 lpm->header.type = htons (GNUNET_MESSAGE_TYPE_PEERINFO_GET);
809 lpm->trust_change = htonl (trust_delta);
810 memcpy (&lpm->peer, peer, sizeof (struct GNUNET_PeerIdentity));
812 ihc->client = client;
813 ihc->callback = callback;
814 ihc->callback_cls = callback_cls;
815 ihc->timeout = GNUNET_TIME_relative_to_absolute (timeout);
817 GNUNET_CLIENT_transmit_and_get_response (client,
818 (const struct GNUNET_MessageHeader*) &ihc[1],
825 GNUNET_CLIENT_disconnect (ihc->client, GNUNET_NO);
834 * Cancel an iteration over peer information.
836 * @param ic context of the iterator to cancel
839 GNUNET_PEERINFO_iterate_cancel (struct GNUNET_PEERINFO_IteratorContext *ic)
841 GNUNET_CLIENT_disconnect (ic->client, GNUNET_NO);
846 /* end of peerinfo_api.c */