2 This file is part of GNUnet.
3 (C) 2001, 2002, 2004, 2005, 2007, 2009, 2010, 2012 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"
34 #define LOG(kind,...) GNUNET_log_from (kind, "peerinfo-api",__VA_ARGS__)
38 * Entry in the transmission queue to PEERINFO service. We use
39 * the same structure for queueing 'iteration' requests and
40 * actual 'add' messages.
42 struct GNUNET_PEERINFO_AddContext
45 * This is a linked list.
47 struct GNUNET_PEERINFO_AddContext *next;
50 * This is a linked list.
52 struct GNUNET_PEERINFO_AddContext *prev;
55 * Handle to the PEERINFO service.
57 struct GNUNET_PEERINFO_Handle *h;
60 * Function to call after request has been transmitted, or NULL.
62 GNUNET_PEERINFO_Continuation cont;
70 * Number of bytes of the request message (follows after this struct).
78 * Context for an iteration request.
80 struct GNUNET_PEERINFO_IteratorContext
86 struct GNUNET_PEERINFO_IteratorContext *next;
91 struct GNUNET_PEERINFO_IteratorContext *prev;
94 * Handle to the PEERINFO service.
96 struct GNUNET_PEERINFO_Handle *h;
99 * Function to call with the results.
101 GNUNET_PEERINFO_Processor callback;
104 * Closure for 'callback'.
109 * Our entry in the transmission queue.
111 struct GNUNET_PEERINFO_AddContext *ac;
114 * Task responsible for timeout.
116 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
119 * Timeout for the operation.
121 struct GNUNET_TIME_Absolute timeout;
124 * Peer we are interested in (only valid if iteration was restricted to one peer).
126 struct GNUNET_PeerIdentity peer;
134 * Are we now receiving?
141 * Handle to the peerinfo service.
143 struct GNUNET_PEERINFO_Handle
148 const struct GNUNET_CONFIGURATION_Handle *cfg;
151 * Connection to the service.
153 struct GNUNET_CLIENT_Connection *client;
156 * Head of transmission queue.
158 struct GNUNET_PEERINFO_AddContext *ac_head;
161 * Tail of transmission queue.
163 struct GNUNET_PEERINFO_AddContext *ac_tail;
166 * Handle for the current transmission request, or NULL if none is pending.
168 struct GNUNET_CLIENT_TransmitHandle *th;
171 * Head of iterator DLL.
173 struct GNUNET_PEERINFO_IteratorContext *ic_head;
176 * Tail of iterator DLL.
178 struct GNUNET_PEERINFO_IteratorContext *ic_tail;
181 * ID for a reconnect task.
183 GNUNET_SCHEDULER_TaskIdentifier r_task;
186 * Set to GNUNET_YES if we are currently receiving replies from the
195 * Connect to the peerinfo service.
197 * @param cfg configuration to use
198 * @return NULL on error (configuration related, actual connection
199 * establishment may happen asynchronously).
201 struct GNUNET_PEERINFO_Handle *
202 GNUNET_PEERINFO_connect (const struct GNUNET_CONFIGURATION_Handle *cfg)
204 struct GNUNET_PEERINFO_Handle *h;
206 h = GNUNET_malloc (sizeof (struct GNUNET_PEERINFO_Handle));
207 h->client = GNUNET_CLIENT_connect ("peerinfo", cfg);
214 * Disconnect from the peerinfo service. Note that all iterators must
215 * have completed or have been cancelled by the time this function is
216 * called (otherwise, calling this function is a serious error).
217 * Furthermore, if 'GNUNET_PEERINFO_add_peer' operations are still
218 * pending, they will be cancelled silently on disconnect.
220 * @param h handle to disconnect
223 GNUNET_PEERINFO_disconnect (struct GNUNET_PEERINFO_Handle *h)
225 struct GNUNET_PEERINFO_AddContext *ac;
226 struct GNUNET_PEERINFO_IteratorContext *ic;
228 while (NULL != (ic = h->ic_head))
230 GNUNET_break (GNUNET_YES == ic->in_receive);
231 ic->in_receive = GNUNET_NO;
232 GNUNET_PEERINFO_iterate_cancel (ic);
234 while (NULL != (ac = h->ac_head))
236 GNUNET_CONTAINER_DLL_remove (h->ac_head, h->ac_tail, ac);
237 if (NULL != ac->cont)
238 ac->cont (ac->cont_cls, _("aborted due to explicit disconnect request"));
243 GNUNET_CLIENT_notify_transmit_ready_cancel (h->th);
246 if (NULL != h->client)
248 GNUNET_CLIENT_disconnect (h->client);
251 if (GNUNET_SCHEDULER_NO_TASK != h->r_task)
253 GNUNET_SCHEDULER_cancel (h->r_task);
254 h->r_task = GNUNET_SCHEDULER_NO_TASK;
261 * Check if we have a request pending in the transmission queue and are
262 * able to transmit it right now. If so, schedule transmission.
264 * @param h handle to the service
267 trigger_transmit (struct GNUNET_PEERINFO_Handle *h);
271 * Close the existing connection to PEERINFO and reconnect.
273 * @param h handle to the service
276 reconnect (struct GNUNET_PEERINFO_Handle *h);
280 * Task scheduled to re-try connecting to the peerinfo service.
282 * @param cls the 'struct GNUNET_PEERINFO_Handle'
283 * @param tc scheduler context
286 reconnect_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
288 struct GNUNET_PEERINFO_Handle *h = cls;
290 h->r_task = GNUNET_SCHEDULER_NO_TASK;
296 * Close the existing connection to PEERINFO and reconnect.
298 * @param h handle to the service
301 reconnect (struct GNUNET_PEERINFO_Handle *h)
303 if (GNUNET_SCHEDULER_NO_TASK != h->r_task)
305 GNUNET_SCHEDULER_cancel (h->r_task);
306 h->r_task = GNUNET_SCHEDULER_NO_TASK;
310 GNUNET_CLIENT_notify_transmit_ready_cancel (h->th);
313 if (NULL != h->client)
315 GNUNET_CLIENT_disconnect (h->client);
318 h->in_receive = GNUNET_NO;
319 h->client = GNUNET_CLIENT_connect ("peerinfo", h->cfg);
320 if (NULL == h->client)
323 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &reconnect_task,
327 trigger_transmit (h);
332 * Transmit the request at the head of the transmission queue
333 * and trigger continuation (if any).
335 * @param cls the 'struct GNUNET_PEERINFO_Handle' (with the queue)
336 * @param size size of the buffer (0 on error)
337 * @param buf where to copy the message
338 * @return number of bytes copied to buf
341 do_transmit (void *cls, size_t size, void *buf)
343 struct GNUNET_PEERINFO_Handle *h = cls;
344 struct GNUNET_PEERINFO_AddContext *ac = h->ac_head;
349 return 0; /* request was cancelled in the meantime */
352 /* peerinfo service died */
353 LOG (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
354 "Failed to transmit message to `%s' service.\n", "PEERINFO");
355 GNUNET_CONTAINER_DLL_remove (h->ac_head, h->ac_tail, ac);
357 if (NULL != ac->cont)
358 ac->cont (ac->cont_cls, _("failed to transmit request (service down?)"));
365 /* change in head of queue (i.e. cancel + add), try again */
366 trigger_transmit (h);
369 LOG (GNUNET_ERROR_TYPE_DEBUG,
370 "Transmitting request of size %u to `%s' service.\n", ret, "PEERINFO");
371 memcpy (buf, &ac[1], ret);
372 GNUNET_CONTAINER_DLL_remove (h->ac_head, h->ac_tail, ac);
373 trigger_transmit (h);
374 if (NULL != ac->cont)
375 ac->cont (ac->cont_cls, NULL);
382 * Check if we have a request pending in the transmission queue and are
383 * able to transmit it right now. If so, schedule transmission.
385 * @param h handle to the service
388 trigger_transmit (struct GNUNET_PEERINFO_Handle *h)
390 struct GNUNET_PEERINFO_AddContext *ac;
392 if (NULL == (ac = h->ac_head))
393 return; /* no requests queued */
395 return; /* request already pending */
396 if (GNUNET_YES == h->in_receive)
397 return; /* still reading replies from last request */
398 if (NULL == h->client)
400 /* disconnected, try to reconnect */
405 GNUNET_CLIENT_notify_transmit_ready (h->client, ac->size,
406 GNUNET_TIME_UNIT_FOREVER_REL,
413 * Add a host to the persistent list. This method operates in
414 * semi-reliable mode: if the transmission is not completed by
415 * the time 'GNUNET_PEERINFO_disconnect' is called, it will be
416 * aborted. Furthermore, if a second HELLO is added for the
417 * same peer before the first one was transmitted, PEERINFO may
418 * merge the two HELLOs prior to transmission to the service.
420 * @param h handle to the peerinfo service
421 * @param hello the verified (!) HELLO message
422 * @param cont continuation to call when done, NULL is allowed
423 * @param cont_cls closure for 'cont'
424 * @return handle to cancel add operation; all pending
425 * 'add' operations will be cancelled automatically
426 * on disconnect, so it is not necessary to keep this
427 * handle (unless 'cont' is NULL and at some point
428 * calling 'cont' must be prevented)
430 struct GNUNET_PEERINFO_AddContext *
431 GNUNET_PEERINFO_add_peer (struct GNUNET_PEERINFO_Handle *h,
432 const struct GNUNET_HELLO_Message *hello,
433 GNUNET_PEERINFO_Continuation cont,
436 uint16_t hs = GNUNET_HELLO_size (hello);
437 struct GNUNET_PEERINFO_AddContext *ac;
438 struct GNUNET_PeerIdentity peer;
440 GNUNET_assert (GNUNET_OK == GNUNET_HELLO_get_id (hello, &peer));
441 LOG (GNUNET_ERROR_TYPE_DEBUG,
442 "Adding peer `%s' to PEERINFO database (%u bytes of `%s')\n",
443 GNUNET_i2s (&peer), hs, "HELLO");
444 ac = GNUNET_malloc (sizeof (struct GNUNET_PEERINFO_AddContext) + hs);
448 ac->cont_cls = cont_cls;
449 memcpy (&ac[1], hello, hs);
450 GNUNET_CONTAINER_DLL_insert_tail (h->ac_head, h->ac_tail, ac);
451 trigger_transmit (h);
457 * Cancel pending 'add' operation. Must only be called before
458 * either 'cont' or 'GNUNET_PEERINFO_disconnect' are invoked.
460 * @param ac handle for the add operation to cancel
463 GNUNET_PEERINFO_add_peer_cancel (struct GNUNET_PEERINFO_AddContext *ac)
465 struct GNUNET_PEERINFO_Handle *h = ac->h;
467 GNUNET_CONTAINER_DLL_remove (h->ac_head, h->ac_tail, ac);
473 * Type of a function to call when we receive a message from the
474 * service. Call the iterator with the result and (if applicable)
475 * continue to receive more messages or trigger processing the next
476 * event (if applicable).
479 * @param msg message received, NULL on timeout or fatal error
482 peerinfo_handler (void *cls, const struct GNUNET_MessageHeader *msg)
484 struct GNUNET_PEERINFO_Handle *h = cls;
485 struct GNUNET_PEERINFO_IteratorContext *ic = h->ic_head;
486 const struct InfoMessage *im;
487 const struct GNUNET_HELLO_Message *hello;
488 GNUNET_PEERINFO_Processor cb;
489 struct GNUNET_PeerIdentity id;
493 GNUNET_assert (NULL != ic);
494 h->in_receive = GNUNET_NO;
495 ic->in_receive = GNUNET_NO;
497 cb_cls = ic->callback_cls;
500 /* peerinfo service died, signal error */
501 GNUNET_PEERINFO_iterate_cancel (ic);
504 cb (cb_cls, NULL, NULL,
505 _("Failed to receive response from `PEERINFO' service."));
509 if (GNUNET_MESSAGE_TYPE_PEERINFO_INFO_END == ntohs (msg->type))
511 /* normal end of list of peers, signal end, process next pending request */
512 LOG (GNUNET_ERROR_TYPE_DEBUG,
513 "Received end of list of peers from `%s' service\n", "PEERINFO");
514 GNUNET_PEERINFO_iterate_cancel (ic);
515 trigger_transmit (h);
517 cb (cb_cls, NULL, NULL, NULL);
521 ms = ntohs (msg->size);
522 if ((ms < sizeof (struct InfoMessage)) ||
523 (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_PEERINFO_INFO))
525 /* malformed message */
527 GNUNET_PEERINFO_iterate_cancel (ic);
530 cb (cb_cls, NULL, NULL,
531 _("Received invalid message from `PEERINFO' service."));
534 im = (const struct InfoMessage *) msg;
535 GNUNET_break (0 == ntohl (im->reserved));
536 if ( (GNUNET_YES == ic->have_peer) &&
537 (0 != memcmp (&ic->peer, &im->peer, sizeof (struct GNUNET_PeerIdentity))) )
539 /* bogus message (from a different iteration call?); out of sequence! */
540 LOG (GNUNET_ERROR_TYPE_ERROR,
541 "Received HELLO for peer `%s', expected peer `%s'\n",
542 GNUNET_h2s (&im->peer.hashPubKey),
543 GNUNET_i2s (&ic->peer));
546 GNUNET_PEERINFO_iterate_cancel (ic);
549 cb (cb_cls, NULL, NULL,
550 _("Received invalid message from `PEERINFO' service."));
554 if (ms > sizeof (struct InfoMessage) + sizeof (struct GNUNET_MessageHeader))
556 hello = (const struct GNUNET_HELLO_Message *) &im[1];
557 if (ms != sizeof (struct InfoMessage) + GNUNET_HELLO_size (hello))
559 /* malformed message */
561 GNUNET_PEERINFO_iterate_cancel (ic);
564 cb (cb_cls, NULL, NULL,
565 _("Received invalid message from `PEERINFO' service."));
568 if (GNUNET_OK != GNUNET_HELLO_get_id (hello, &id))
570 /* malformed message */
572 GNUNET_PEERINFO_iterate_cancel (ic);
575 cb (cb_cls, NULL, NULL,
576 _("Received invalid message from `PEERINFO' service."));
579 if (0 != memcmp (&im->peer, &id, sizeof (struct GNUNET_PeerIdentity)))
581 /* malformed message */
583 GNUNET_PEERINFO_iterate_cancel (ic);
586 cb (cb_cls, NULL, NULL,
587 _("Received invalid message from `PEERINFO' service."));
592 /* normal data message */
593 LOG (GNUNET_ERROR_TYPE_DEBUG,
594 "Received %u bytes of `%s' information about peer `%s' from `%s' service\n",
595 (hello == NULL) ? 0 : (unsigned int) GNUNET_HELLO_size (hello), "HELLO",
596 GNUNET_i2s (&im->peer), "PEERINFO");
597 h->in_receive = GNUNET_YES;
598 ic->in_receive = GNUNET_YES;
599 GNUNET_CLIENT_receive (h->client, &peerinfo_handler, h,
600 GNUNET_TIME_absolute_get_remaining (ic->timeout));
602 cb (cb_cls, &im->peer, hello, NULL);
607 * We've transmitted the iteration request. Now get ready to process
608 * the results (or handle transmission error).
610 * @param cls the 'struct GNUNET_PEERINFO_IteratorContext'
611 * @param emsg error message, NULL if transmission worked
614 iterator_start_receive (void *cls, const char *emsg)
616 struct GNUNET_PEERINFO_IteratorContext *ic = cls;
617 struct GNUNET_PEERINFO_Handle *h = ic->h;
618 GNUNET_PEERINFO_Processor cb;
625 cb_cls = ic->callback_cls;
626 GNUNET_PEERINFO_iterate_cancel (ic);
629 cb (cb_cls, NULL, NULL, emsg);
632 LOG (GNUNET_ERROR_TYPE_DEBUG, "Waiting for response from `%s' service.\n",
634 ic->in_receive = GNUNET_YES;
635 if (GNUNET_NO == h->in_receive)
637 h->in_receive = GNUNET_YES;
638 GNUNET_CLIENT_receive (h->client, &peerinfo_handler, h,
639 GNUNET_TIME_absolute_get_remaining (ic->timeout));
645 * Peerinfo iteration request has timed out.
647 * @param cls the 'struct GNUNET_PEERINFO_IteratorContext*'
648 * @param tc scheduler context
651 signal_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
653 struct GNUNET_PEERINFO_IteratorContext *ic = cls;
654 GNUNET_PEERINFO_Processor cb;
657 ic->timeout_task = GNUNET_SCHEDULER_NO_TASK;
659 cb_cls = ic->callback_cls;
660 GNUNET_PEERINFO_iterate_cancel (ic);
662 cb (cb_cls, NULL, NULL,
663 _("Timeout transmitting iteration request to `PEERINFO' service."));
668 * Call a method for each known matching host and change its trust
669 * value. The callback method will be invoked once for each matching
670 * host and then finally once with a NULL pointer. After that final
671 * invocation, the iterator context must no longer be used.
673 * Instead of calling this function with 'peer == NULL' it is often
674 * better to use 'GNUNET_PEERINFO_notify'.
676 * @param h handle to the peerinfo service
677 * @param peer restrict iteration to this peer only (can be NULL)
678 * @param timeout how long to wait until timing out
679 * @param callback the method to call for each peer
680 * @param callback_cls closure for callback
681 * @return iterator context
683 struct GNUNET_PEERINFO_IteratorContext *
684 GNUNET_PEERINFO_iterate (struct GNUNET_PEERINFO_Handle *h,
685 const struct GNUNET_PeerIdentity *peer,
686 struct GNUNET_TIME_Relative timeout,
687 GNUNET_PEERINFO_Processor callback, void *callback_cls)
689 struct GNUNET_MessageHeader *lapm;
690 struct ListPeerMessage *lpm;
691 struct GNUNET_PEERINFO_IteratorContext *ic;
692 struct GNUNET_PEERINFO_AddContext *ac;
694 ic = GNUNET_malloc (sizeof (struct GNUNET_PEERINFO_IteratorContext));
697 LOG (GNUNET_ERROR_TYPE_DEBUG,
698 "Requesting list of peers from PEERINFO service\n");
700 GNUNET_malloc (sizeof (struct GNUNET_PEERINFO_AddContext) +
701 sizeof (struct GNUNET_MessageHeader));
702 ac->size = sizeof (struct GNUNET_MessageHeader);
703 lapm = (struct GNUNET_MessageHeader *) &ac[1];
704 lapm->size = htons (sizeof (struct GNUNET_MessageHeader));
705 lapm->type = htons (GNUNET_MESSAGE_TYPE_PEERINFO_GET_ALL);
709 LOG (GNUNET_ERROR_TYPE_DEBUG,
710 "Requesting information on peer `%4s' from PEERINFO service\n",
713 GNUNET_malloc (sizeof (struct GNUNET_PEERINFO_AddContext) +
714 sizeof (struct ListPeerMessage));
715 ac->size = sizeof (struct ListPeerMessage);
716 lpm = (struct ListPeerMessage *) &ac[1];
717 lpm->header.size = htons (sizeof (struct ListPeerMessage));
718 lpm->header.type = htons (GNUNET_MESSAGE_TYPE_PEERINFO_GET);
719 memcpy (&lpm->peer, peer, sizeof (struct GNUNET_PeerIdentity));
720 ic->have_peer = GNUNET_YES;
725 ic->callback = callback;
726 ic->callback_cls = callback_cls;
727 ic->timeout = GNUNET_TIME_relative_to_absolute (timeout);
729 GNUNET_SCHEDULER_add_delayed (timeout, &signal_timeout, ic);
730 ac->cont = &iterator_start_receive;
732 GNUNET_CONTAINER_DLL_insert_tail (h->ac_head, h->ac_tail, ac);
733 GNUNET_CONTAINER_DLL_insert_tail (h->ic_head,
736 trigger_transmit (h);
742 * Cancel an iteration over peer information.
744 * @param ic context of the iterator to cancel
747 GNUNET_PEERINFO_iterate_cancel (struct GNUNET_PEERINFO_IteratorContext *ic)
749 struct GNUNET_PEERINFO_Handle *h;
752 if (GNUNET_SCHEDULER_NO_TASK != ic->timeout_task)
754 GNUNET_SCHEDULER_cancel (ic->timeout_task);
755 ic->timeout_task = GNUNET_SCHEDULER_NO_TASK;
758 if (GNUNET_YES == ic->in_receive)
759 return; /* need to finish processing */
760 GNUNET_CONTAINER_DLL_remove (h->ic_head,
765 GNUNET_CONTAINER_DLL_remove (h->ac_head, h->ac_tail, ic->ac);
766 GNUNET_free (ic->ac);
772 /* end of peerinfo_api.c */