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;
96 struct GNUNET_SCHEDULER_Handle *sched;
99 * Connection to the service.
101 struct GNUNET_CLIENT_Connection *client;
104 * Head of transmission queue.
106 struct TransmissionQueueEntry *tq_head;
109 * Tail of transmission queue.
111 struct TransmissionQueueEntry *tq_tail;
114 * Handle for the current transmission request, or NULL if none is pending.
116 struct GNUNET_CLIENT_TransmitHandle *th;
119 * Set to GNUNET_YES if we are currently receiving replies from the
128 * Connect to the peerinfo service.
130 * @param sched scheduler to use
131 * @param cfg configuration to use
132 * @return NULL on error (configuration related, actual connection
133 * establishment may happen asynchronously).
135 struct GNUNET_PEERINFO_Handle *
136 GNUNET_PEERINFO_connect (struct GNUNET_SCHEDULER_Handle *sched,
137 const struct GNUNET_CONFIGURATION_Handle *cfg)
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);
178 GNUNET_CLIENT_notify_transmit_ready_cancel (h->th);
181 GNUNET_CLIENT_disconnect (h->client, GNUNET_NO);
187 * Check if we have a request pending in the transmission queue and are
188 * able to transmit it right now. If so, schedule transmission.
190 * @param h handle to the service
193 trigger_transmit (struct GNUNET_PEERINFO_Handle *h);
197 * Close the existing connection to PEERINFO and reconnect.
199 * @param h handle to the service
202 reconnect (struct GNUNET_PEERINFO_Handle *h)
204 GNUNET_CLIENT_disconnect (h->client, GNUNET_SYSERR);
206 h->client = GNUNET_CLIENT_connect (h->sched, "peerinfo", h->cfg);
207 GNUNET_assert (h->client != NULL);
212 * Transmit the request at the head of the transmission queue
213 * and trigger continuation (if any).
215 * @param cls the 'struct GNUNET_PEERINFO_Handle' (with the queue)
216 * @param size size of the buffer (0 on error)
217 * @param buf where to copy the message
218 * @return number of bytes copied to buf
221 do_transmit (void *cls, size_t size, void *buf)
223 struct GNUNET_PEERINFO_Handle *h = cls;
224 struct TransmissionQueueEntry *tqe = h->tq_head;
230 GNUNET_log (GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK,
231 _("Failed to transmit message to `%s' service.\n"),
234 GNUNET_CONTAINER_DLL_remove (h->tq_head,
238 trigger_transmit (h);
241 if (tqe->cont != NULL)
242 tqe->cont (tqe->cont_cls, GNUNET_SYSERR);
248 GNUNET_assert (size >= ret);
249 memcpy (buf, &tqe[1], ret);
251 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
252 "Transmitting request of size %u to `%s' service.\n",
256 GNUNET_CONTAINER_DLL_remove (h->tq_head,
259 if (tqe->cont != NULL)
260 tqe->cont (tqe->cont_cls, GNUNET_OK);
262 trigger_transmit (h);
269 * Check if we have a request pending in the transmission queue and are
270 * able to transmit it right now. If so, schedule transmission.
272 * @param h handle to the service
275 trigger_transmit (struct GNUNET_PEERINFO_Handle *h)
277 struct TransmissionQueueEntry *tqe;
279 if (NULL == (tqe = h->tq_head))
283 if (h->in_receive == GNUNET_YES)
285 h->th = GNUNET_CLIENT_notify_transmit_ready (h->client,
287 GNUNET_TIME_absolute_get_remaining (tqe->timeout),
294 * Add a host to the persistent list. This method operates in
295 * semi-reliable mode: if the transmission is not completed by
296 * the time 'GNUNET_PEERINFO_disconnect' is called, it will be
297 * aborted. Furthermore, if a second HELLO is added for the
298 * same peer before the first one was transmitted, PEERINFO may
299 * merge the two HELLOs prior to transmission to the service.
301 * @param h handle to the peerinfo service
302 * @param hello the verified (!) HELLO message
305 GNUNET_PEERINFO_add_peer (struct GNUNET_PEERINFO_Handle *h,
306 const struct GNUNET_HELLO_Message *hello)
308 uint16_t hs = GNUNET_HELLO_size (hello);
309 struct TransmissionQueueEntry *tqe;
312 struct GNUNET_PeerIdentity peer;
313 GNUNET_assert (GNUNET_OK == GNUNET_HELLO_get_id (hello, &peer));
314 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
315 "Adding peer `%s' to PEERINFO database (%u bytes of `%s')\n",
320 tqe = GNUNET_malloc (sizeof (struct TransmissionQueueEntry) + hs);
322 tqe->timeout = GNUNET_TIME_UNIT_FOREVER_ABS;
323 memcpy (&tqe[1], hello, hs);
324 GNUNET_CONTAINER_DLL_insert_after (h->tq_head,
328 trigger_transmit (h);
333 * Context for an iteration request.
335 struct GNUNET_PEERINFO_IteratorContext
338 * Handle to the PEERINFO service.
340 struct GNUNET_PEERINFO_Handle *h;
343 * Function to call with the results.
345 GNUNET_PEERINFO_Processor callback;
348 * Closure for 'callback'.
353 * Our entry in the transmission queue.
355 struct TransmissionQueueEntry *tqe;
358 * Task responsible for timeout.
360 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
363 * Timeout for the operation.
365 struct GNUNET_TIME_Absolute timeout;
368 * Are we now receiving?
375 * Type of a function to call when we receive a message
379 * @param msg message received, NULL on timeout or fatal error
382 peerinfo_handler (void *cls, const struct GNUNET_MessageHeader *msg)
384 struct GNUNET_PEERINFO_IteratorContext *ic = cls;
385 const struct InfoMessage *im;
386 const struct GNUNET_HELLO_Message *hello;
389 ic->h->in_receive = GNUNET_NO;
392 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
393 _("Failed to receive response from `%s' service.\n"),
396 trigger_transmit (ic->h);
397 if (ic->timeout_task != GNUNET_SCHEDULER_NO_TASK)
398 GNUNET_SCHEDULER_cancel (ic->h->sched,
400 if (ic->callback != NULL)
401 ic->callback (ic->callback_cls, NULL, NULL);
405 if (ntohs (msg->type) == GNUNET_MESSAGE_TYPE_PEERINFO_INFO_END)
408 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
409 "Received end of list of peers from `%s' service\n",
412 trigger_transmit (ic->h);
413 if (ic->timeout_task != GNUNET_SCHEDULER_NO_TASK)
414 GNUNET_SCHEDULER_cancel (ic->h->sched,
416 if (ic->callback != NULL)
417 ic->callback (ic->callback_cls, NULL, NULL);
421 ms = ntohs (msg->size);
422 if ((ms < sizeof (struct InfoMessage)) ||
423 (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_PEERINFO_INFO))
427 trigger_transmit (ic->h);
428 if (ic->timeout_task != GNUNET_SCHEDULER_NO_TASK)
429 GNUNET_SCHEDULER_cancel (ic->h->sched,
431 if (ic->callback != NULL)
432 ic->callback (ic->callback_cls, NULL, NULL);
436 im = (const struct InfoMessage *) msg;
438 if (ms > sizeof (struct InfoMessage) + sizeof (struct GNUNET_MessageHeader))
440 hello = (const struct GNUNET_HELLO_Message *) &im[1];
441 if (ms != sizeof (struct InfoMessage) + GNUNET_HELLO_size (hello))
445 trigger_transmit (ic->h);
446 if (ic->timeout_task != GNUNET_SCHEDULER_NO_TASK)
447 GNUNET_SCHEDULER_cancel (ic->h->sched,
449 if (ic->callback != NULL)
450 ic->callback (ic->callback_cls, NULL, NULL);
456 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
457 "Received %u bytes of `%s' information about peer `%s' from `%s' service\n",
458 (hello == NULL) ? 0 : (unsigned int) GNUNET_HELLO_size (hello),
460 GNUNET_i2s (&im->peer),
463 ic->h->in_receive = GNUNET_YES;
464 if (ic->callback != NULL)
465 ic->callback (ic->callback_cls, &im->peer, hello);
466 GNUNET_CLIENT_receive (ic->h->client,
469 GNUNET_TIME_absolute_get_remaining (ic->timeout));
474 * We've transmitted the iteration request. Now get ready to process
475 * the results (or handle transmission error).
477 * @param cls the 'struct GNUNET_PEERINFO_IteratorContext'
478 * @param transmit_success GNUNET_OK if transmission worked
481 iterator_start_receive (void *cls,
482 int transmit_success)
484 struct GNUNET_PEERINFO_IteratorContext *ic = cls;
486 if (GNUNET_OK != transmit_success)
488 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
489 _("Failed to transmit iteration request to `%s' service (%d).\n"),
492 if (ic->timeout_task != GNUNET_SCHEDULER_NO_TASK)
494 GNUNET_SCHEDULER_cancel (ic->h->sched,
496 ic->timeout_task = GNUNET_SCHEDULER_NO_TASK;
499 trigger_transmit (ic->h);
500 if (ic->callback != NULL)
501 ic->callback (ic->callback_cls, NULL, NULL);
506 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
507 "Waiting for response from `%s' service.\n",
510 ic->h->in_receive = GNUNET_YES;
511 ic->in_receive = GNUNET_YES;
513 GNUNET_CLIENT_receive (ic->h->client,
516 GNUNET_TIME_absolute_get_remaining (ic->timeout));
521 * Peerinfo iteration request has timed out.
523 * @param cls the 'struct GNUNET_PEERINFO_IteratorContext*'
524 * @param tc scheduler context
527 signal_timeout (void *cls,
528 const struct GNUNET_SCHEDULER_TaskContext *tc)
530 struct GNUNET_PEERINFO_IteratorContext *ic = cls;
532 GNUNET_log (GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK,
533 _("Timeout transmitting iteration request to `%s' service.\n"),
535 ic->timeout_task = GNUNET_SCHEDULER_NO_TASK;
536 if (! ic->in_receive)
537 GNUNET_CONTAINER_DLL_remove (ic->h->tq_head,
541 ic->callback (ic->callback_cls, NULL, NULL);
543 GNUNET_free_non_null (ic->tqe);
549 * Call a method for each known matching host and change its trust
550 * value. The callback method will be invoked once for each matching
551 * host and then finally once with a NULL pointer. After that final
552 * invocation, the iterator context must no longer be used.
554 * Instead of calling this function with 'peer == NULL' it is often
555 * better to use 'GNUNET_PEERINFO_notify'.
557 * @param h handle to the peerinfo service
558 * @param peer restrict iteration to this peer only (can be NULL)
559 * @param timeout how long to wait until timing out
560 * @param callback the method to call for each peer
561 * @param callback_cls closure for callback
562 * @return NULL on error (in this case, 'callback' is never called!),
563 * otherwise an iterator context
565 struct GNUNET_PEERINFO_IteratorContext *
566 GNUNET_PEERINFO_iterate (struct GNUNET_PEERINFO_Handle *h,
567 const struct GNUNET_PeerIdentity *peer,
568 struct GNUNET_TIME_Relative timeout,
569 GNUNET_PEERINFO_Processor callback,
572 struct GNUNET_MessageHeader *lapm;
573 struct ListPeerMessage *lpm;
574 struct GNUNET_PEERINFO_IteratorContext *ic;
575 struct TransmissionQueueEntry *tqe;
580 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
581 "Requesting list of peers from PEERINFO service\n");
583 tqe = GNUNET_malloc (sizeof (struct TransmissionQueueEntry) +
584 sizeof (struct GNUNET_MessageHeader));
585 tqe->size = sizeof (struct GNUNET_MessageHeader);
586 lapm = (struct GNUNET_MessageHeader *) &tqe[1];
587 lapm->size = htons (sizeof (struct GNUNET_MessageHeader));
588 lapm->type = htons (GNUNET_MESSAGE_TYPE_PEERINFO_GET_ALL);
593 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
594 "Requesting information on peer `%4s' from PEERINFO service\n",
597 tqe = GNUNET_malloc (sizeof (struct TransmissionQueueEntry) +
598 sizeof (struct ListPeerMessage));
599 tqe->size = sizeof (struct ListPeerMessage);
600 lpm = (struct ListPeerMessage *) &tqe[1];
601 lpm->header.size = htons (sizeof (struct ListPeerMessage));
602 lpm->header.type = htons (GNUNET_MESSAGE_TYPE_PEERINFO_GET);
603 memcpy (&lpm->peer, peer, sizeof (struct GNUNET_PeerIdentity));
605 ic = GNUNET_malloc (sizeof (struct GNUNET_PEERINFO_IteratorContext));
608 ic->callback = callback;
609 ic->callback_cls = callback_cls;
610 ic->timeout = GNUNET_TIME_relative_to_absolute (timeout);
611 ic->timeout_task = GNUNET_SCHEDULER_add_delayed (h->sched,
615 tqe->timeout = ic->timeout;
616 tqe->cont = &iterator_start_receive;
618 tqe->timeout = ic->timeout;
619 GNUNET_CONTAINER_DLL_insert_after (h->tq_head,
623 trigger_transmit (h);
630 * Cancel an iteration over peer information.
632 * @param ic context of the iterator to cancel
635 GNUNET_PEERINFO_iterate_cancel (struct GNUNET_PEERINFO_IteratorContext *ic)
637 if (ic->timeout_task != GNUNET_SCHEDULER_NO_TASK)
639 GNUNET_SCHEDULER_cancel (ic->h->sched,
641 ic->timeout_task = GNUNET_SCHEDULER_NO_TASK;
645 return; /* need to finish processing */
646 GNUNET_CONTAINER_DLL_remove (ic->h->tq_head,
649 GNUNET_free (ic->tqe);
654 /* end of peerinfo_api.c */