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 peer identity of the peer
290 * @param hello the verified (!) HELLO message
293 GNUNET_PEERINFO_add_peer_new (struct GNUNET_PEERINFO_Handle *h,
294 const struct GNUNET_HELLO_Message *hello)
296 uint16_t hs = GNUNET_HELLO_size (hello);
297 struct TransmissionQueueEntry *tqe;
300 struct GNUNET_PeerIdentity peer;
301 GNUNET_assert (GNUNET_OK == GNUNET_HELLO_get_id (hello, &peer));
302 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
303 "Adding peer `%s' to PEERINFO database (%u bytes of `%s')\n",
308 tqe = GNUNET_malloc (sizeof (struct TransmissionQueueEntry) + hs);
310 tqe->timeout = GNUNET_TIME_UNIT_FOREVER_ABS;
311 memcpy (&tqe[1], hello, hs);
312 GNUNET_CONTAINER_DLL_insert_after (h->tq_head,
316 trigger_transmit (h);
321 * Context for an iteration request.
323 struct GNUNET_PEERINFO_NewIteratorContext
326 * Handle to the PEERINFO service.
328 struct GNUNET_PEERINFO_Handle *h;
331 * Function to call with the results.
333 GNUNET_PEERINFO_Processor callback;
336 * Closure for 'callback'.
341 * Timeout for the operation.
343 struct GNUNET_TIME_Absolute timeout;
348 * Type of a function to call when we receive a message
352 * @param msg message received, NULL on timeout or fatal error
355 peerinfo_handler (void *cls, const struct GNUNET_MessageHeader *msg)
357 struct GNUNET_PEERINFO_NewIteratorContext *ic = cls;
358 const struct InfoMessage *im;
359 const struct GNUNET_HELLO_Message *hello;
364 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
365 _("Failed to receive response from `%s' service.\n"),
368 trigger_transmit (ic->h);
369 ic->callback (ic->callback_cls, NULL, NULL, 1);
373 if (ntohs (msg->type) == GNUNET_MESSAGE_TYPE_PEERINFO_INFO_END)
376 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
377 "Received end of list of peers from peerinfo database\n");
379 trigger_transmit (ic->h);
380 ic->callback (ic->callback_cls, NULL, NULL, 0);
384 ms = ntohs (msg->size);
385 if ((ms < sizeof (struct InfoMessage)) ||
386 (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_PEERINFO_INFO))
390 trigger_transmit (ic->h);
391 ic->callback (ic->callback_cls, NULL, NULL, 2);
395 im = (const struct InfoMessage *) msg;
397 if (ms > sizeof (struct InfoMessage) + sizeof (struct GNUNET_MessageHeader))
399 hello = (const struct GNUNET_HELLO_Message *) &im[1];
400 if (ms != sizeof (struct InfoMessage) + GNUNET_HELLO_size (hello))
404 trigger_transmit (ic->h);
405 ic->callback (ic->callback_cls, NULL, NULL, 2);
411 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
412 "Received %u bytes of `%s' information about peer `%s' from PEERINFO database\n",
413 (hello == NULL) ? 0 : (unsigned int) GNUNET_HELLO_size (hello),
415 GNUNET_i2s (&im->peer));
417 ic->callback (ic->callback_cls, &im->peer, hello, ntohl (im->trust));
418 GNUNET_CLIENT_receive (ic->h->client,
421 GNUNET_TIME_absolute_get_remaining (ic->timeout));
426 * We've transmitted the iteration request. Now get ready to process
427 * the results (or handle transmission error).
429 * @param cls the 'struct GNUNET_PEERINFO_NewIteratorContext'
430 * @param transmit_success GNUNET_OK if transmission worked
433 iterator_start_receive (void *cls,
434 int transmit_success)
436 struct GNUNET_PEERINFO_NewIteratorContext *ic = cls;
438 if (GNUNET_OK != transmit_success)
440 ic->callback (ic->callback_cls, NULL, NULL, 2);
442 trigger_transmit (ic->h);
446 GNUNET_CLIENT_receive (ic->h->client,
449 GNUNET_TIME_absolute_get_remaining (ic->timeout));
454 * Call a method for each known matching host and change its trust
455 * value. The callback method will be invoked once for each matching
456 * host and then finally once with a NULL pointer. After that final
457 * invocation, the iterator context must no longer be used.
459 * Note that the last call can be triggered by timeout or by simply
460 * being done; however, the trust argument will be set to zero if we
461 * are done, 1 if we timed out and 2 for fatal error.
463 * Instead of calling this function with 'peer == NULL' and 'trust ==
464 * 0', it is often better to use 'GNUNET_PEERINFO_notify'.
466 * @param h handle to the peerinfo service
467 * @param peer restrict iteration to this peer only (can be NULL)
468 * @param trust_delta how much to change the trust in all matching peers
469 * @param timeout how long to wait until timing out
470 * @param callback the method to call for each peer
471 * @param callback_cls closure for callback
472 * @return NULL on error (in this case, 'callback' is never called!),
473 * otherwise an iterator context
475 struct GNUNET_PEERINFO_NewIteratorContext *
476 GNUNET_PEERINFO_iterate_new (struct GNUNET_PEERINFO_Handle *h,
477 const struct GNUNET_PeerIdentity *peer,
479 struct GNUNET_TIME_Relative timeout,
480 GNUNET_PEERINFO_Processor callback,
483 struct ListAllPeersMessage *lapm;
484 struct ListPeerMessage *lpm;
485 struct GNUNET_PEERINFO_NewIteratorContext *ic;
486 struct TransmissionQueueEntry *tqe;
489 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
490 "Requesting list of peers from peerinfo database\n");
494 tqe = GNUNET_malloc (sizeof (struct TransmissionQueueEntry) +
495 sizeof (struct ListAllPeersMessage));
496 tqe->size = sizeof (struct ListAllPeersMessage);
497 lapm = (struct ListAllPeersMessage *) &tqe[1];
498 lapm->header.size = htons (sizeof (struct ListAllPeersMessage));
499 lapm->header.type = htons (GNUNET_MESSAGE_TYPE_PEERINFO_GET_ALL);
500 lapm->trust_change = htonl (trust_delta);
504 tqe = GNUNET_malloc (sizeof (struct TransmissionQueueEntry) +
505 sizeof (struct ListPeerMessage));
506 tqe->size = sizeof (struct ListPeerMessage);
507 lpm = (struct ListPeerMessage *) &tqe[1];
508 lpm->header.size = htons (sizeof (struct ListPeerMessage));
509 lpm->header.type = htons (GNUNET_MESSAGE_TYPE_PEERINFO_GET);
510 lpm->trust_change = htonl (trust_delta);
511 memcpy (&lpm->peer, peer, sizeof (struct GNUNET_PeerIdentity));
513 ic = GNUNET_malloc (sizeof (struct GNUNET_PEERINFO_NewIteratorContext));
514 ic->callback = callback;
515 ic->callback_cls = callback_cls;
516 ic->timeout = GNUNET_TIME_relative_to_absolute (timeout);
517 tqe->timeout = ic->timeout;
518 tqe->cont = &iterator_start_receive;
520 /* FIXME: sort DLL by timeout? */
521 /* FIXME: add timeout task!? */
522 GNUNET_CONTAINER_DLL_insert_after (h->tq_head,
526 trigger_transmit (h);
533 * Cancel an iteration over peer information.
535 * @param ic context of the iterator to cancel
538 GNUNET_PEERINFO_iterate_cancel_new (struct GNUNET_PEERINFO_NewIteratorContext *ic)
541 // FIXME: not implemented
548 /* ***************************** OLD API ****************************** */
553 #define ADD_PEER_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30)
558 struct GNUNET_CLIENT_Connection *client;
559 struct GNUNET_MessageHeader *msg;
564 copy_and_free (void *cls, size_t size, void *buf)
566 struct CAFContext *cc = cls;
567 struct GNUNET_MessageHeader *msg = cc->msg;
573 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
575 ("Failed to transmit message of type %u to `%s' service.\n"),
576 ntohs (msg->type), "peerinfo");
579 GNUNET_CLIENT_disconnect (cc->client, GNUNET_NO);
583 msize = ntohs (msg->size);
584 GNUNET_assert (size >= msize);
585 memcpy (buf, msg, msize);
587 GNUNET_CLIENT_disconnect (cc->client, GNUNET_YES);
595 * Add a host to the persistent list.
597 * @param cfg configuration to use
598 * @param sched scheduler to use
599 * @param peer identity of the peer
600 * @param hello the verified (!) HELLO message
603 GNUNET_PEERINFO_add_peer (const struct GNUNET_CONFIGURATION_Handle *cfg,
604 struct GNUNET_SCHEDULER_Handle *sched,
605 const struct GNUNET_PeerIdentity *peer,
606 const struct GNUNET_HELLO_Message *hello)
608 struct GNUNET_CLIENT_Connection *client;
609 struct PeerAddMessage *pam;
611 struct CAFContext *cc;
614 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
615 "Adding peer `%s' to peerinfo database\n",
618 client = GNUNET_CLIENT_connect (sched, "peerinfo", cfg);
621 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
622 _("Could not connect to `%s' service.\n"), "peerinfo");
625 hs = GNUNET_HELLO_size (hello);
627 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
628 "Size of `%s' is %u bytes\n",
630 (unsigned int) GNUNET_HELLO_size (hello));
632 pam = GNUNET_malloc (sizeof (struct PeerAddMessage) + hs);
633 pam->header.size = htons (hs + sizeof (struct PeerAddMessage));
634 pam->header.type = htons (GNUNET_MESSAGE_TYPE_PEERINFO_ADD);
635 memcpy (&pam->peer, peer, sizeof (struct GNUNET_PeerIdentity));
636 memcpy (&pam[1], hello, hs);
637 cc = GNUNET_malloc (sizeof (struct CAFContext));
639 cc->msg = &pam->header;
640 GNUNET_CLIENT_notify_transmit_ready (client,
641 ntohs (pam->header.size),
649 * Context for the info handler.
651 struct GNUNET_PEERINFO_IteratorContext
655 * Our connection to the PEERINFO service.
657 struct GNUNET_CLIENT_Connection *client;
660 * Function to call with information.
662 GNUNET_PEERINFO_Processor callback;
665 * Closure for callback.
670 * When should we time out?
672 struct GNUNET_TIME_Absolute timeout;
678 * Type of a function to call when we receive a message
682 * @param msg message received, NULL on timeout or fatal error
685 info_handler (void *cls, const struct GNUNET_MessageHeader *msg)
687 struct GNUNET_PEERINFO_IteratorContext *ic = cls;
688 const struct InfoMessage *im;
689 const struct GNUNET_HELLO_Message *hello;
694 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
695 _("Failed to receive response from `%s' service.\n"),
697 ic->callback (ic->callback_cls, NULL, NULL, 1);
698 GNUNET_CLIENT_disconnect (ic->client, GNUNET_NO);
702 if (ntohs (msg->type) == GNUNET_MESSAGE_TYPE_PEERINFO_INFO_END)
705 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
706 "Received end of list of peers from peerinfo database\n");
708 ic->callback (ic->callback_cls, NULL, NULL, 0);
709 GNUNET_CLIENT_disconnect (ic->client, GNUNET_NO);
713 ms = ntohs (msg->size);
714 if ((ms < sizeof (struct InfoMessage)) ||
715 (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_PEERINFO_INFO))
718 ic->callback (ic->callback_cls, NULL, NULL, 2);
719 GNUNET_CLIENT_disconnect (ic->client, GNUNET_NO);
723 im = (const struct InfoMessage *) msg;
725 if (ms > sizeof (struct InfoMessage) + sizeof (struct GNUNET_MessageHeader))
727 hello = (const struct GNUNET_HELLO_Message *) &im[1];
728 if (ms != sizeof (struct InfoMessage) + GNUNET_HELLO_size (hello))
731 ic->callback (ic->callback_cls, NULL, NULL, 2);
732 GNUNET_CLIENT_disconnect (ic->client, GNUNET_NO);
738 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
739 "Received %u bytes of `%s' information about peer `%s' from PEERINFO database\n",
740 (hello == NULL) ? 0 : (unsigned int) GNUNET_HELLO_size (hello),
742 GNUNET_i2s (&im->peer));
744 ic->callback (ic->callback_cls, &im->peer, hello, ntohl (im->trust));
745 GNUNET_CLIENT_receive (ic->client,
748 GNUNET_TIME_absolute_get_remaining (ic->timeout));
753 * Call a method for each known matching host and change
754 * its trust value. The method will be invoked once for
755 * each host and then finally once with a NULL pointer.
756 * Note that the last call can be triggered by timeout or
757 * by simply being done; however, the trust argument will
758 * be set to zero if we are done and to 1 if we timed out.
760 * @param cfg configuration to use
761 * @param sched scheduler to use
762 * @param peer restrict iteration to this peer only (can be NULL)
763 * @param trust_delta how much to change the trust in all matching peers
764 * @param timeout how long to wait until timing out
765 * @param callback the method to call for each peer
766 * @param callback_cls closure for callback
767 * @return NULL on error, otherwise an iterator context
769 struct GNUNET_PEERINFO_IteratorContext *
770 GNUNET_PEERINFO_iterate (const struct GNUNET_CONFIGURATION_Handle *cfg,
771 struct GNUNET_SCHEDULER_Handle *sched,
772 const struct GNUNET_PeerIdentity *peer,
774 struct GNUNET_TIME_Relative timeout,
775 GNUNET_PEERINFO_Processor callback,
778 struct GNUNET_CLIENT_Connection *client;
779 struct ListAllPeersMessage *lapm;
780 struct ListPeerMessage *lpm;
781 struct GNUNET_PEERINFO_IteratorContext *ihc;
783 client = GNUNET_CLIENT_connect (sched, "peerinfo", cfg);
786 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
787 _("Could not connect to `%s' service.\n"), "peerinfo");
791 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
792 "Requesting list of peers from peerinfo database\n");
796 ihc = GNUNET_malloc (sizeof (struct GNUNET_PEERINFO_IteratorContext) +
797 sizeof (struct ListAllPeersMessage));
798 lapm = (struct ListAllPeersMessage *) &ihc[1];
799 lapm->header.size = htons (sizeof (struct ListAllPeersMessage));
800 lapm->header.type = htons (GNUNET_MESSAGE_TYPE_PEERINFO_GET_ALL);
801 lapm->trust_change = htonl (trust_delta);
805 ihc = GNUNET_malloc (sizeof (struct GNUNET_PEERINFO_IteratorContext) +
806 sizeof (struct ListPeerMessage));
807 lpm = (struct ListPeerMessage *) &ihc[1];
808 lpm->header.size = htons (sizeof (struct ListPeerMessage));
809 lpm->header.type = htons (GNUNET_MESSAGE_TYPE_PEERINFO_GET);
810 lpm->trust_change = htonl (trust_delta);
811 memcpy (&lpm->peer, peer, sizeof (struct GNUNET_PeerIdentity));
813 ihc->client = client;
814 ihc->callback = callback;
815 ihc->callback_cls = callback_cls;
816 ihc->timeout = GNUNET_TIME_relative_to_absolute (timeout);
818 GNUNET_CLIENT_transmit_and_get_response (client,
819 (const struct GNUNET_MessageHeader*) &ihc[1],
826 GNUNET_CLIENT_disconnect (ihc->client, GNUNET_NO);
835 * Cancel an iteration over peer information.
837 * @param ic context of the iterator to cancel
840 GNUNET_PEERINFO_iterate_cancel (struct GNUNET_PEERINFO_IteratorContext *ic)
842 GNUNET_CLIENT_disconnect (ic->client, GNUNET_NO);
847 /* end of peerinfo_api.c */