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, int success);
44 * Entry in the transmission queue to PEERINFO service.
46 struct TransmissionQueueEntry
49 * This is a linked list.
51 struct TransmissionQueueEntry *next;
54 * This is a linked list.
56 struct TransmissionQueueEntry *prev;
59 * Function to call after request has been transmitted, or NULL (in which
60 * case we must consider sending the next entry immediately).
62 TransmissionContinuation cont;
70 * Timeout for the operation.
72 struct GNUNET_TIME_Absolute timeout;
75 * Number of bytes of the request message (follows after this struct).
83 * Handle to the peerinfo service.
85 struct GNUNET_PEERINFO_Handle
90 const struct GNUNET_CONFIGURATION_Handle *cfg;
93 * Connection to the service.
95 struct GNUNET_CLIENT_Connection *client;
98 * Head of transmission queue.
100 struct TransmissionQueueEntry *tq_head;
103 * Tail of transmission queue.
105 struct TransmissionQueueEntry *tq_tail;
108 * Handle for the current transmission request, or NULL if none is pending.
110 struct GNUNET_CLIENT_TransmitHandle *th;
113 * ID for a reconnect task.
115 GNUNET_SCHEDULER_TaskIdentifier r_task;
118 * Set to GNUNET_YES if we are currently receiving replies from the
127 * Connect to the peerinfo service.
129 * @param cfg configuration to use
130 * @return NULL on error (configuration related, actual connection
131 * establishment may happen asynchronously).
133 struct GNUNET_PEERINFO_Handle *
134 GNUNET_PEERINFO_connect (const struct GNUNET_CONFIGURATION_Handle *cfg)
136 struct GNUNET_PEERINFO_Handle *ret;
138 ret = GNUNET_malloc (sizeof (struct GNUNET_PEERINFO_Handle));
139 ret->client = GNUNET_CLIENT_connect ("peerinfo", cfg);
146 * Disconnect from the peerinfo service. Note that all iterators must
147 * have completed or have been cancelled by the time this function is
148 * called (otherwise, calling this function is a serious error).
149 * Furthermore, if 'GNUNET_PEERINFO_add_peer' operations are still
150 * pending, they will be cancelled silently on disconnect.
152 * @param h handle to disconnect
155 GNUNET_PEERINFO_disconnect (struct GNUNET_PEERINFO_Handle *h)
157 struct TransmissionQueueEntry *tqe;
159 while (NULL != (tqe = h->tq_head))
161 GNUNET_CONTAINER_DLL_remove (h->tq_head, h->tq_tail, tqe);
162 if (tqe->cont != NULL)
163 tqe->cont (tqe->cont_cls, GNUNET_SYSERR);
168 GNUNET_CLIENT_notify_transmit_ready_cancel (h->th);
171 if (NULL != h->client)
173 GNUNET_CLIENT_disconnect (h->client, GNUNET_NO);
176 if (GNUNET_SCHEDULER_NO_TASK != h->r_task)
178 GNUNET_SCHEDULER_cancel (h->r_task);
179 h->r_task = GNUNET_SCHEDULER_NO_TASK;
186 * Check if we have a request pending in the transmission queue and are
187 * able to transmit it right now. If so, schedule transmission.
189 * @param h handle to the service
191 static void trigger_transmit (struct GNUNET_PEERINFO_Handle *h);
195 * Close the existing connection to PEERINFO and reconnect.
197 * @param h handle to the service
199 static void reconnect (struct GNUNET_PEERINFO_Handle *h);
202 * Task scheduled to re-try connecting to the peerinfo service.
204 * @param cls the 'struct GNUNET_PEERINFO_Handle'
205 * @param ts scheduler context
208 reconnect_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
210 struct GNUNET_PEERINFO_Handle *h = cls;
212 h->r_task = GNUNET_SCHEDULER_NO_TASK;
218 * Close the existing connection to PEERINFO and reconnect.
220 * @param h handle to the service
223 reconnect (struct GNUNET_PEERINFO_Handle *h)
225 if (h->r_task != GNUNET_SCHEDULER_NO_TASK)
227 GNUNET_SCHEDULER_cancel (h->r_task);
228 h->r_task = GNUNET_SCHEDULER_NO_TASK;
232 GNUNET_CLIENT_notify_transmit_ready_cancel (h->th);
235 if (NULL != h->client)
237 GNUNET_CLIENT_disconnect (h->client, GNUNET_SYSERR);
240 h->client = GNUNET_CLIENT_connect ("peerinfo", h->cfg);
241 if (NULL == h->client)
244 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &reconnect_task,
248 trigger_transmit (h);
253 * Transmit the request at the head of the transmission queue
254 * and trigger continuation (if any).
256 * @param cls the 'struct GNUNET_PEERINFO_Handle' (with the queue)
257 * @param size size of the buffer (0 on error)
258 * @param buf where to copy the message
259 * @return number of bytes copied to buf
262 do_transmit (void *cls, size_t size, void *buf)
264 struct GNUNET_PEERINFO_Handle *h = cls;
265 struct TransmissionQueueEntry *tqe = h->tq_head;
273 GNUNET_log (GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK,
274 _("Failed to transmit message to `%s' service.\n"), "PEERINFO");
275 GNUNET_CONTAINER_DLL_remove (h->tq_head, h->tq_tail, tqe);
277 if (tqe->cont != NULL)
278 tqe->cont (tqe->cont_cls, GNUNET_SYSERR);
283 GNUNET_assert (size >= ret);
284 memcpy (buf, &tqe[1], ret);
286 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
287 "Transmitting request of size %u to `%s' service.\n", ret,
290 GNUNET_CONTAINER_DLL_remove (h->tq_head, h->tq_tail, tqe);
291 if (tqe->cont != NULL)
292 tqe->cont (tqe->cont_cls, GNUNET_OK);
294 trigger_transmit (h);
301 * Check if we have a request pending in the transmission queue and are
302 * able to transmit it right now. If so, schedule transmission.
304 * @param h handle to the service
307 trigger_transmit (struct GNUNET_PEERINFO_Handle *h)
309 struct TransmissionQueueEntry *tqe;
311 if (NULL == (tqe = h->tq_head))
315 if (h->in_receive == GNUNET_YES)
317 if (NULL == h->client)
323 GNUNET_CLIENT_notify_transmit_ready (h->client, tqe->size,
324 GNUNET_TIME_absolute_get_remaining
325 (tqe->timeout), GNUNET_YES,
331 * Add a host to the persistent list. This method operates in
332 * semi-reliable mode: if the transmission is not completed by
333 * the time 'GNUNET_PEERINFO_disconnect' is called, it will be
334 * aborted. Furthermore, if a second HELLO is added for the
335 * same peer before the first one was transmitted, PEERINFO may
336 * merge the two HELLOs prior to transmission to the service.
338 * @param h handle to the peerinfo service
339 * @param hello the verified (!) HELLO message
342 GNUNET_PEERINFO_add_peer (struct GNUNET_PEERINFO_Handle *h,
343 const struct GNUNET_HELLO_Message *hello)
345 uint16_t hs = GNUNET_HELLO_size (hello);
346 struct TransmissionQueueEntry *tqe;
349 struct GNUNET_PeerIdentity peer;
351 GNUNET_assert (GNUNET_OK == GNUNET_HELLO_get_id (hello, &peer));
352 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
353 "Adding peer `%s' to PEERINFO database (%u bytes of `%s')\n",
354 GNUNET_i2s (&peer), hs, "HELLO");
356 tqe = GNUNET_malloc (sizeof (struct TransmissionQueueEntry) + hs);
358 tqe->timeout = GNUNET_TIME_UNIT_FOREVER_ABS;
359 memcpy (&tqe[1], hello, hs);
360 GNUNET_CONTAINER_DLL_insert_after (h->tq_head, h->tq_tail, h->tq_tail, tqe);
361 trigger_transmit (h);
366 * Context for an iteration request.
368 struct GNUNET_PEERINFO_IteratorContext
371 * Handle to the PEERINFO service.
373 struct GNUNET_PEERINFO_Handle *h;
376 * Function to call with the results.
378 GNUNET_PEERINFO_Processor callback;
381 * Closure for 'callback'.
386 * Our entry in the transmission queue.
388 struct TransmissionQueueEntry *tqe;
391 * Task responsible for timeout.
393 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
396 * Timeout for the operation.
398 struct GNUNET_TIME_Absolute timeout;
401 * Are we now receiving?
408 * Type of a function to call when we receive a message
412 * @param msg message received, NULL on timeout or fatal error
415 peerinfo_handler (void *cls, const struct GNUNET_MessageHeader *msg)
417 struct GNUNET_PEERINFO_IteratorContext *ic = cls;
418 const struct InfoMessage *im;
419 const struct GNUNET_HELLO_Message *hello;
422 ic->h->in_receive = GNUNET_NO;
426 if (ic->timeout_task != GNUNET_SCHEDULER_NO_TASK)
427 GNUNET_SCHEDULER_cancel (ic->timeout_task);
428 if (ic->callback != NULL)
429 ic->callback (ic->callback_cls, NULL, NULL,
430 _("Failed to receive response from `PEERINFO' service."));
434 if (ntohs (msg->type) == GNUNET_MESSAGE_TYPE_PEERINFO_INFO_END)
437 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
438 "Received end of list of peers from `%s' service\n",
441 trigger_transmit (ic->h);
442 if (ic->timeout_task != GNUNET_SCHEDULER_NO_TASK)
443 GNUNET_SCHEDULER_cancel (ic->timeout_task);
444 if (ic->callback != NULL)
445 ic->callback (ic->callback_cls, NULL, NULL, NULL);
449 ms = ntohs (msg->size);
450 if ((ms < sizeof (struct InfoMessage)) ||
451 (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_PEERINFO_INFO))
455 if (ic->timeout_task != GNUNET_SCHEDULER_NO_TASK)
456 GNUNET_SCHEDULER_cancel (ic->timeout_task);
457 if (ic->callback != NULL)
458 ic->callback (ic->callback_cls, NULL, NULL,
459 _("Received invalid message from `PEERINFO' service.\n"));
463 im = (const struct InfoMessage *) msg;
464 GNUNET_break (0 == ntohl (im->reserved));
466 if (ms > sizeof (struct InfoMessage) + sizeof (struct GNUNET_MessageHeader))
468 hello = (const struct GNUNET_HELLO_Message *) &im[1];
469 if (ms != sizeof (struct InfoMessage) + GNUNET_HELLO_size (hello))
473 if (ic->timeout_task != GNUNET_SCHEDULER_NO_TASK)
474 GNUNET_SCHEDULER_cancel (ic->timeout_task);
475 if (ic->callback != NULL)
476 ic->callback (ic->callback_cls, NULL, NULL,
477 _("Received invalid message from `PEERINFO' service.\n"));
483 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
484 "Received %u bytes of `%s' information about peer `%s' from `%s' service\n",
485 (hello == NULL) ? 0 : (unsigned int) GNUNET_HELLO_size (hello),
486 "HELLO", GNUNET_i2s (&im->peer), "PEERINFO");
488 ic->h->in_receive = GNUNET_YES;
489 if (ic->callback != NULL)
490 ic->callback (ic->callback_cls, &im->peer, hello, NULL);
491 GNUNET_CLIENT_receive (ic->h->client, &peerinfo_handler, ic,
492 GNUNET_TIME_absolute_get_remaining (ic->timeout));
497 * We've transmitted the iteration request. Now get ready to process
498 * the results (or handle transmission error).
500 * @param cls the 'struct GNUNET_PEERINFO_IteratorContext'
501 * @param transmit_success GNUNET_OK if transmission worked
504 iterator_start_receive (void *cls, int transmit_success)
506 struct GNUNET_PEERINFO_IteratorContext *ic = cls;
508 if (GNUNET_OK != transmit_success)
510 if (ic->timeout_task != GNUNET_SCHEDULER_NO_TASK)
512 GNUNET_SCHEDULER_cancel (ic->timeout_task);
513 ic->timeout_task = GNUNET_SCHEDULER_NO_TASK;
516 if (ic->callback != NULL)
517 ic->callback (ic->callback_cls, NULL, NULL,
519 ("Failed to transmit iteration request to `PEERINFO' service\n"));
524 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
525 "Waiting for response from `%s' service.\n", "PEERINFO");
527 ic->h->in_receive = GNUNET_YES;
528 ic->in_receive = GNUNET_YES;
530 GNUNET_CLIENT_receive (ic->h->client, &peerinfo_handler, ic,
531 GNUNET_TIME_absolute_get_remaining (ic->timeout));
536 * Peerinfo iteration request has timed out.
538 * @param cls the 'struct GNUNET_PEERINFO_IteratorContext*'
539 * @param tc scheduler context
542 signal_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
544 struct GNUNET_PEERINFO_IteratorContext *ic = cls;
546 ic->timeout_task = GNUNET_SCHEDULER_NO_TASK;
548 GNUNET_CONTAINER_DLL_remove (ic->h->tq_head, ic->h->tq_tail, ic->tqe);
551 ic->callback (ic->callback_cls, NULL, NULL,
553 ("Timeout transmitting iteration request to `PEERINFO' service.\n"));
555 GNUNET_free_non_null (ic->tqe);
561 * Call a method for each known matching host and change its trust
562 * value. The callback method will be invoked once for each matching
563 * host and then finally once with a NULL pointer. After that final
564 * invocation, the iterator context must no longer be used.
566 * Instead of calling this function with 'peer == NULL' it is often
567 * better to use 'GNUNET_PEERINFO_notify'.
569 * @param h handle to the peerinfo service
570 * @param peer restrict iteration to this peer only (can be NULL)
571 * @param timeout how long to wait until timing out
572 * @param callback the method to call for each peer
573 * @param callback_cls closure for callback
574 * @return iterator context
576 struct GNUNET_PEERINFO_IteratorContext *
577 GNUNET_PEERINFO_iterate (struct GNUNET_PEERINFO_Handle *h,
578 const struct GNUNET_PeerIdentity *peer,
579 struct GNUNET_TIME_Relative timeout,
580 GNUNET_PEERINFO_Processor callback, void *callback_cls)
582 struct GNUNET_MessageHeader *lapm;
583 struct ListPeerMessage *lpm;
584 struct GNUNET_PEERINFO_IteratorContext *ic;
585 struct TransmissionQueueEntry *tqe;
590 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
591 "Requesting list of peers from PEERINFO service\n");
594 GNUNET_malloc (sizeof (struct TransmissionQueueEntry) +
595 sizeof (struct GNUNET_MessageHeader));
596 tqe->size = sizeof (struct GNUNET_MessageHeader);
597 lapm = (struct GNUNET_MessageHeader *) &tqe[1];
598 lapm->size = htons (sizeof (struct GNUNET_MessageHeader));
599 lapm->type = htons (GNUNET_MESSAGE_TYPE_PEERINFO_GET_ALL);
604 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
605 "Requesting information on peer `%4s' from PEERINFO service\n",
609 GNUNET_malloc (sizeof (struct TransmissionQueueEntry) +
610 sizeof (struct ListPeerMessage));
611 tqe->size = sizeof (struct ListPeerMessage);
612 lpm = (struct ListPeerMessage *) &tqe[1];
613 lpm->header.size = htons (sizeof (struct ListPeerMessage));
614 lpm->header.type = htons (GNUNET_MESSAGE_TYPE_PEERINFO_GET);
615 memcpy (&lpm->peer, peer, sizeof (struct GNUNET_PeerIdentity));
617 ic = GNUNET_malloc (sizeof (struct GNUNET_PEERINFO_IteratorContext));
620 ic->callback = callback;
621 ic->callback_cls = callback_cls;
622 ic->timeout = GNUNET_TIME_relative_to_absolute (timeout);
624 GNUNET_SCHEDULER_add_delayed (timeout, &signal_timeout, ic);
625 tqe->timeout = ic->timeout;
626 tqe->cont = &iterator_start_receive;
628 tqe->timeout = ic->timeout;
629 GNUNET_CONTAINER_DLL_insert_after (h->tq_head, h->tq_tail, h->tq_tail, tqe);
630 trigger_transmit (h);
636 * Cancel an iteration over peer information.
638 * @param ic context of the iterator to cancel
641 GNUNET_PEERINFO_iterate_cancel (struct GNUNET_PEERINFO_IteratorContext *ic)
643 if (ic->timeout_task != GNUNET_SCHEDULER_NO_TASK)
645 GNUNET_SCHEDULER_cancel (ic->timeout_task);
646 ic->timeout_task = GNUNET_SCHEDULER_NO_TASK;
649 if (GNUNET_YES == ic->in_receive)
650 return; /* need to finish processing */
651 GNUNET_CONTAINER_DLL_remove (ic->h->tq_head, ic->h->tq_tail, ic->tqe);
652 GNUNET_free (ic->tqe);
657 /* end of peerinfo_api.c */