2 This file is part of GNUnet.
3 (C) 2001, 2002, 2004, 2005, 2007, 2009, 2010 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 3, 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_container_lib.h"
29 #include "gnunet_peerinfo_service.h"
30 #include "gnunet_protocols.h"
31 #include "gnunet_time_lib.h"
35 * Function to call after transmission has succeeded.
38 * @param success GNUNET_OK if transmission worked, GNUNET_SYSERR on error
40 typedef void (*TransmissionContinuation)(void *cls,
45 * Entry in the transmission queue to PEERINFO service.
47 struct TransmissionQueueEntry
50 * This is a linked list.
52 struct TransmissionQueueEntry *next;
55 * This is a linked list.
57 struct TransmissionQueueEntry *prev;
60 * Function to call after request has been transmitted, or NULL (in which
61 * case we must consider sending the next entry immediately).
63 TransmissionContinuation cont;
71 * Timeout for the operation.
73 struct GNUNET_TIME_Absolute timeout;
76 * Number of bytes of the request message (follows after this struct).
84 * Handle to the peerinfo service.
86 struct GNUNET_PEERINFO_Handle
91 const struct GNUNET_CONFIGURATION_Handle *cfg;
94 * Connection to the service.
96 struct GNUNET_CLIENT_Connection *client;
99 * Head of transmission queue.
101 struct TransmissionQueueEntry *tq_head;
104 * Tail of transmission queue.
106 struct TransmissionQueueEntry *tq_tail;
109 * Handle for the current transmission request, or NULL if none is pending.
111 struct GNUNET_CLIENT_TransmitHandle *th;
114 * ID for a reconnect task.
116 GNUNET_SCHEDULER_TaskIdentifier r_task;
119 * Set to GNUNET_YES if we are currently receiving replies from the
128 * Connect to the peerinfo service.
130 * @param cfg configuration to use
131 * @return NULL on error (configuration related, actual connection
132 * establishment may happen asynchronously).
134 struct GNUNET_PEERINFO_Handle *
135 GNUNET_PEERINFO_connect (const struct GNUNET_CONFIGURATION_Handle *cfg)
137 struct GNUNET_PEERINFO_Handle *ret;
139 ret = GNUNET_malloc (sizeof (struct GNUNET_PEERINFO_Handle));
140 ret->client = GNUNET_CLIENT_connect ("peerinfo", cfg);
147 * Disconnect from the peerinfo service. Note that all iterators must
148 * have completed or have been cancelled by the time this function is
149 * called (otherwise, calling this function is a serious error).
150 * Furthermore, if 'GNUNET_PEERINFO_add_peer' operations are still
151 * pending, they will be cancelled silently on disconnect.
153 * @param h handle to disconnect
156 GNUNET_PEERINFO_disconnect (struct GNUNET_PEERINFO_Handle *h)
158 struct TransmissionQueueEntry *tqe;
160 while (NULL != (tqe = h->tq_head))
162 GNUNET_CONTAINER_DLL_remove (h->tq_head,
165 if (tqe->cont != NULL)
166 tqe->cont (tqe->cont_cls, GNUNET_SYSERR);
171 GNUNET_CLIENT_notify_transmit_ready_cancel (h->th);
174 if (NULL != h->client)
176 GNUNET_CLIENT_disconnect (h->client, GNUNET_NO);
179 if (GNUNET_SCHEDULER_NO_TASK != h->r_task)
181 GNUNET_SCHEDULER_cancel (h->r_task);
182 h->r_task = GNUNET_SCHEDULER_NO_TASK;
189 * Check if we have a request pending in the transmission queue and are
190 * able to transmit it right now. If so, schedule transmission.
192 * @param h handle to the service
195 trigger_transmit (struct GNUNET_PEERINFO_Handle *h);
199 * Close the existing connection to PEERINFO and reconnect.
201 * @param h handle to the service
204 reconnect (struct GNUNET_PEERINFO_Handle *h);
207 * Task scheduled to re-try connecting to the peerinfo service.
209 * @param cls the 'struct GNUNET_PEERINFO_Handle'
210 * @param ts scheduler context
213 reconnect_task (void *cls,
214 const struct GNUNET_SCHEDULER_TaskContext *tc)
216 struct GNUNET_PEERINFO_Handle *h = cls;
218 h->r_task = GNUNET_SCHEDULER_NO_TASK;
224 * Close the existing connection to PEERINFO and reconnect.
226 * @param h handle to the service
229 reconnect (struct GNUNET_PEERINFO_Handle *h)
231 if (h->r_task != GNUNET_SCHEDULER_NO_TASK)
233 GNUNET_SCHEDULER_cancel (h->r_task);
234 h->r_task = GNUNET_SCHEDULER_NO_TASK;
238 GNUNET_CLIENT_notify_transmit_ready_cancel (h->th);
241 if (NULL != h->client)
243 GNUNET_CLIENT_disconnect (h->client, GNUNET_SYSERR);
246 h->client = GNUNET_CLIENT_connect ("peerinfo", h->cfg);
247 if (NULL == h->client)
249 h->r_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
254 trigger_transmit (h);
259 * Transmit the request at the head of the transmission queue
260 * and trigger continuation (if any).
262 * @param cls the 'struct GNUNET_PEERINFO_Handle' (with the queue)
263 * @param size size of the buffer (0 on error)
264 * @param buf where to copy the message
265 * @return number of bytes copied to buf
268 do_transmit (void *cls, size_t size, void *buf)
270 struct GNUNET_PEERINFO_Handle *h = cls;
271 struct TransmissionQueueEntry *tqe = h->tq_head;
279 GNUNET_log (GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK,
280 _("Failed to transmit message to `%s' service.\n"),
282 GNUNET_CONTAINER_DLL_remove (h->tq_head,
286 if (tqe->cont != NULL)
287 tqe->cont (tqe->cont_cls, GNUNET_SYSERR);
292 GNUNET_assert (size >= ret);
293 memcpy (buf, &tqe[1], ret);
295 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
296 "Transmitting request of size %u to `%s' service.\n",
300 GNUNET_CONTAINER_DLL_remove (h->tq_head,
303 if (tqe->cont != NULL)
304 tqe->cont (tqe->cont_cls, GNUNET_OK);
306 trigger_transmit (h);
313 * Check if we have a request pending in the transmission queue and are
314 * able to transmit it right now. If so, schedule transmission.
316 * @param h handle to the service
319 trigger_transmit (struct GNUNET_PEERINFO_Handle *h)
321 struct TransmissionQueueEntry *tqe;
323 if (NULL == (tqe = h->tq_head))
327 if (h->in_receive == GNUNET_YES)
329 if (NULL == h->client)
334 h->th = GNUNET_CLIENT_notify_transmit_ready (h->client,
336 GNUNET_TIME_absolute_get_remaining (tqe->timeout),
343 * Add a host to the persistent list. This method operates in
344 * semi-reliable mode: if the transmission is not completed by
345 * the time 'GNUNET_PEERINFO_disconnect' is called, it will be
346 * aborted. Furthermore, if a second HELLO is added for the
347 * same peer before the first one was transmitted, PEERINFO may
348 * merge the two HELLOs prior to transmission to the service.
350 * @param h handle to the peerinfo service
351 * @param hello the verified (!) HELLO message
354 GNUNET_PEERINFO_add_peer (struct GNUNET_PEERINFO_Handle *h,
355 const struct GNUNET_HELLO_Message *hello)
357 uint16_t hs = GNUNET_HELLO_size (hello);
358 struct TransmissionQueueEntry *tqe;
361 struct GNUNET_PeerIdentity peer;
362 GNUNET_assert (GNUNET_OK == GNUNET_HELLO_get_id (hello, &peer));
363 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
364 "Adding peer `%s' to PEERINFO database (%u bytes of `%s')\n",
369 tqe = GNUNET_malloc (sizeof (struct TransmissionQueueEntry) + hs);
371 tqe->timeout = GNUNET_TIME_UNIT_FOREVER_ABS;
372 memcpy (&tqe[1], hello, hs);
373 GNUNET_CONTAINER_DLL_insert_after (h->tq_head,
377 trigger_transmit (h);
382 * Context for an iteration request.
384 struct GNUNET_PEERINFO_IteratorContext
387 * Handle to the PEERINFO service.
389 struct GNUNET_PEERINFO_Handle *h;
392 * Function to call with the results.
394 GNUNET_PEERINFO_Processor callback;
397 * Closure for 'callback'.
402 * Our entry in the transmission queue.
404 struct TransmissionQueueEntry *tqe;
407 * Task responsible for timeout.
409 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
412 * Timeout for the operation.
414 struct GNUNET_TIME_Absolute timeout;
417 * Are we now receiving?
424 * Type of a function to call when we receive a message
428 * @param msg message received, NULL on timeout or fatal error
431 peerinfo_handler (void *cls,
432 const struct GNUNET_MessageHeader *msg)
434 struct GNUNET_PEERINFO_IteratorContext *ic = cls;
435 const struct InfoMessage *im;
436 const struct GNUNET_HELLO_Message *hello;
439 ic->h->in_receive = GNUNET_NO;
443 if (ic->timeout_task != GNUNET_SCHEDULER_NO_TASK)
444 GNUNET_SCHEDULER_cancel (ic->timeout_task);
445 if (ic->callback != NULL)
446 ic->callback (ic->callback_cls, NULL, NULL,
447 _("Failed to receive response from `PEERINFO' service."));
451 if (ntohs (msg->type) == GNUNET_MESSAGE_TYPE_PEERINFO_INFO_END)
454 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
455 "Received end of list of peers from `%s' service\n",
458 trigger_transmit (ic->h);
459 if (ic->timeout_task != GNUNET_SCHEDULER_NO_TASK)
460 GNUNET_SCHEDULER_cancel (ic->timeout_task);
461 if (ic->callback != NULL)
462 ic->callback (ic->callback_cls, NULL, NULL, NULL);
466 ms = ntohs (msg->size);
467 if ((ms < sizeof (struct InfoMessage)) ||
468 (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_PEERINFO_INFO))
472 if (ic->timeout_task != GNUNET_SCHEDULER_NO_TASK)
473 GNUNET_SCHEDULER_cancel (ic->timeout_task);
474 if (ic->callback != NULL)
475 ic->callback (ic->callback_cls, NULL, NULL,
476 _("Received invalid message from `PEERINFO' service.\n"));
480 im = (const struct InfoMessage *) msg;
481 GNUNET_break (0 == ntohl (im->reserved));
483 if (ms > sizeof (struct InfoMessage) + sizeof (struct GNUNET_MessageHeader))
485 hello = (const struct GNUNET_HELLO_Message *) &im[1];
486 if (ms != sizeof (struct InfoMessage) + GNUNET_HELLO_size (hello))
490 if (ic->timeout_task != GNUNET_SCHEDULER_NO_TASK)
491 GNUNET_SCHEDULER_cancel (ic->timeout_task);
492 if (ic->callback != NULL)
493 ic->callback (ic->callback_cls, NULL, NULL,
494 _("Received invalid message from `PEERINFO' service.\n"));
500 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
501 "Received %u bytes of `%s' information about peer `%s' from `%s' service\n",
502 (hello == NULL) ? 0 : (unsigned int) GNUNET_HELLO_size (hello),
504 GNUNET_i2s (&im->peer),
507 ic->h->in_receive = GNUNET_YES;
508 if (ic->callback != NULL)
509 ic->callback (ic->callback_cls, &im->peer, hello, NULL);
510 GNUNET_CLIENT_receive (ic->h->client,
513 GNUNET_TIME_absolute_get_remaining (ic->timeout));
518 * We've transmitted the iteration request. Now get ready to process
519 * the results (or handle transmission error).
521 * @param cls the 'struct GNUNET_PEERINFO_IteratorContext'
522 * @param transmit_success GNUNET_OK if transmission worked
525 iterator_start_receive (void *cls,
526 int transmit_success)
528 struct GNUNET_PEERINFO_IteratorContext *ic = cls;
530 if (GNUNET_OK != transmit_success)
532 if (ic->timeout_task != GNUNET_SCHEDULER_NO_TASK)
534 GNUNET_SCHEDULER_cancel (ic->timeout_task);
535 ic->timeout_task = GNUNET_SCHEDULER_NO_TASK;
538 if (ic->callback != NULL)
539 ic->callback (ic->callback_cls, NULL, NULL,
540 _("Failed to transmit iteration request to `PEERINFO' service\n"));
545 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
546 "Waiting for response from `%s' service.\n",
549 ic->h->in_receive = GNUNET_YES;
550 ic->in_receive = GNUNET_YES;
552 GNUNET_CLIENT_receive (ic->h->client,
555 GNUNET_TIME_absolute_get_remaining (ic->timeout));
560 * Peerinfo iteration request has timed out.
562 * @param cls the 'struct GNUNET_PEERINFO_IteratorContext*'
563 * @param tc scheduler context
566 signal_timeout (void *cls,
567 const struct GNUNET_SCHEDULER_TaskContext *tc)
569 struct GNUNET_PEERINFO_IteratorContext *ic = cls;
571 ic->timeout_task = GNUNET_SCHEDULER_NO_TASK;
572 if (! ic->in_receive)
573 GNUNET_CONTAINER_DLL_remove (ic->h->tq_head,
578 ic->callback (ic->callback_cls,
580 _("Timeout transmitting iteration request to `PEERINFO' service.\n"));
582 GNUNET_free_non_null (ic->tqe);
588 * Call a method for each known matching host and change its trust
589 * value. The callback method will be invoked once for each matching
590 * host and then finally once with a NULL pointer. After that final
591 * invocation, the iterator context must no longer be used.
593 * Instead of calling this function with 'peer == NULL' it is often
594 * better to use 'GNUNET_PEERINFO_notify'.
596 * @param h handle to the peerinfo service
597 * @param peer restrict iteration to this peer only (can be NULL)
598 * @param timeout how long to wait until timing out
599 * @param callback the method to call for each peer
600 * @param callback_cls closure for callback
601 * @return iterator context
603 struct GNUNET_PEERINFO_IteratorContext *
604 GNUNET_PEERINFO_iterate (struct GNUNET_PEERINFO_Handle *h,
605 const struct GNUNET_PeerIdentity *peer,
606 struct GNUNET_TIME_Relative timeout,
607 GNUNET_PEERINFO_Processor callback,
610 struct GNUNET_MessageHeader *lapm;
611 struct ListPeerMessage *lpm;
612 struct GNUNET_PEERINFO_IteratorContext *ic;
613 struct TransmissionQueueEntry *tqe;
618 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
619 "Requesting list of peers from PEERINFO service\n");
621 tqe = GNUNET_malloc (sizeof (struct TransmissionQueueEntry) +
622 sizeof (struct GNUNET_MessageHeader));
623 tqe->size = sizeof (struct GNUNET_MessageHeader);
624 lapm = (struct GNUNET_MessageHeader *) &tqe[1];
625 lapm->size = htons (sizeof (struct GNUNET_MessageHeader));
626 lapm->type = htons (GNUNET_MESSAGE_TYPE_PEERINFO_GET_ALL);
631 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
632 "Requesting information on peer `%4s' from PEERINFO service\n",
635 tqe = GNUNET_malloc (sizeof (struct TransmissionQueueEntry) +
636 sizeof (struct ListPeerMessage));
637 tqe->size = sizeof (struct ListPeerMessage);
638 lpm = (struct ListPeerMessage *) &tqe[1];
639 lpm->header.size = htons (sizeof (struct ListPeerMessage));
640 lpm->header.type = htons (GNUNET_MESSAGE_TYPE_PEERINFO_GET);
641 memcpy (&lpm->peer, peer, sizeof (struct GNUNET_PeerIdentity));
643 ic = GNUNET_malloc (sizeof (struct GNUNET_PEERINFO_IteratorContext));
646 ic->callback = callback;
647 ic->callback_cls = callback_cls;
648 ic->timeout = GNUNET_TIME_relative_to_absolute (timeout);
649 ic->timeout_task = GNUNET_SCHEDULER_add_delayed (timeout,
652 tqe->timeout = ic->timeout;
653 tqe->cont = &iterator_start_receive;
655 tqe->timeout = ic->timeout;
656 GNUNET_CONTAINER_DLL_insert_after (h->tq_head,
660 trigger_transmit (h);
666 * Cancel an iteration over peer information.
668 * @param ic context of the iterator to cancel
671 GNUNET_PEERINFO_iterate_cancel (struct GNUNET_PEERINFO_IteratorContext *ic)
673 if (ic->timeout_task != GNUNET_SCHEDULER_NO_TASK)
675 GNUNET_SCHEDULER_cancel (ic->timeout_task);
676 ic->timeout_task = GNUNET_SCHEDULER_NO_TASK;
679 if (GNUNET_YES == ic->in_receive)
680 return; /* need to finish processing */
681 GNUNET_CONTAINER_DLL_remove (ic->h->tq_head,
684 GNUNET_free (ic->tqe);
689 /* end of peerinfo_api.c */