2 This file is part of GNUnet.
3 Copyright (C) 2001-2014 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_util_lib.h"
28 #include "gnunet_protocols.h"
31 #define LOG(kind,...) GNUNET_log_from (kind, "peerinfo-api",__VA_ARGS__)
35 * Entry in the transmission queue to PEERINFO service. We use
36 * the same structure for queueing 'iteration' requests and
37 * actual 'add' messages.
39 struct GNUNET_PEERINFO_AddContext
42 * This is a linked list.
44 struct GNUNET_PEERINFO_AddContext *next;
47 * This is a linked list.
49 struct GNUNET_PEERINFO_AddContext *prev;
52 * Handle to the PEERINFO service.
54 struct GNUNET_PEERINFO_Handle *h;
57 * Function to call after request has been transmitted, or NULL.
59 GNUNET_PEERINFO_Continuation cont;
62 * Closure for @e cont.
67 * Number of bytes of the request message (follows after this struct).
75 * Context for an iteration request.
77 struct GNUNET_PEERINFO_IteratorContext
83 struct GNUNET_PEERINFO_IteratorContext *next;
88 struct GNUNET_PEERINFO_IteratorContext *prev;
91 * Handle to the PEERINFO service.
93 struct GNUNET_PEERINFO_Handle *h;
96 * Function to call with the results.
98 GNUNET_PEERINFO_Processor callback;
101 * Closure for @e callback.
106 * Our entry in the transmission queue.
108 struct GNUNET_PEERINFO_AddContext *ac;
111 * Task responsible for timeout.
113 struct GNUNET_SCHEDULER_Task *timeout_task;
116 * Timeout for the operation.
118 struct GNUNET_TIME_Absolute timeout;
121 * Peer we are interested in (only valid if iteration was restricted to one peer).
123 struct GNUNET_PeerIdentity peer;
131 * Set to #GNUNET_YES if we are currently receiving replies from the
134 int request_transmitted;
140 * Handle to the peerinfo service.
142 struct GNUNET_PEERINFO_Handle
147 const struct GNUNET_CONFIGURATION_Handle *cfg;
150 * Connection to the service.
152 struct GNUNET_CLIENT_Connection *client;
155 * Head of transmission queue.
157 struct GNUNET_PEERINFO_AddContext *ac_head;
160 * Tail of transmission queue.
162 struct GNUNET_PEERINFO_AddContext *ac_tail;
165 * Handle for the current transmission request, or NULL if none is pending.
167 struct GNUNET_CLIENT_TransmitHandle *th;
170 * Head of iterator DLL.
172 struct GNUNET_PEERINFO_IteratorContext *ic_head;
175 * Tail of iterator DLL.
177 struct GNUNET_PEERINFO_IteratorContext *ic_tail;
180 * ID for a reconnect task.
182 struct GNUNET_SCHEDULER_Task *r_task;
185 * Are we now receiving?
193 * Connect to the peerinfo service.
195 * @param cfg configuration to use
196 * @return NULL on error (configuration related, actual connection
197 * establishment may happen asynchronously).
199 struct GNUNET_PEERINFO_Handle *
200 GNUNET_PEERINFO_connect (const struct GNUNET_CONFIGURATION_Handle *cfg)
202 struct GNUNET_PEERINFO_Handle *h;
204 h = GNUNET_new (struct GNUNET_PEERINFO_Handle);
205 h->client = GNUNET_CLIENT_connect ("peerinfo", cfg);
212 * Disconnect from the peerinfo service. Note that all iterators must
213 * have completed or have been cancelled by the time this function is
214 * called (otherwise, calling this function is a serious error).
215 * Furthermore, if #GNUNET_PEERINFO_add_peer() operations are still
216 * pending, they will be cancelled silently on disconnect.
218 * @param h handle to disconnect
221 GNUNET_PEERINFO_disconnect (struct GNUNET_PEERINFO_Handle *h)
223 struct GNUNET_PEERINFO_AddContext *ac;
224 struct GNUNET_PEERINFO_IteratorContext *ic;
226 while (NULL != (ic = h->ic_head))
228 GNUNET_break (GNUNET_YES == ic->request_transmitted);
229 ic->request_transmitted = GNUNET_NO;
230 GNUNET_PEERINFO_iterate_cancel (ic);
232 while (NULL != (ac = h->ac_head))
234 GNUNET_CONTAINER_DLL_remove (h->ac_head, h->ac_tail, ac);
235 if (NULL != ac->cont)
236 ac->cont (ac->cont_cls,
237 _("aborted due to explicit disconnect request"));
242 GNUNET_CLIENT_notify_transmit_ready_cancel (h->th);
245 if (NULL != h->client)
247 GNUNET_CLIENT_disconnect (h->client);
250 if (NULL != h->r_task)
252 GNUNET_SCHEDULER_cancel (h->r_task);
260 * Check if we have a request pending in the transmission queue and are
261 * able to transmit it right now. If so, schedule transmission.
263 * @param h handle to the service
266 trigger_transmit (struct GNUNET_PEERINFO_Handle *h);
270 * Close the existing connection to PEERINFO and reconnect.
272 * @param h handle to the service
275 reconnect (struct GNUNET_PEERINFO_Handle *h);
279 * Task scheduled to re-try connecting to the peerinfo service.
281 * @param cls the `struct GNUNET_PEERINFO_Handle *`
282 * @param tc scheduler context
285 reconnect_task (void *cls,
286 const struct GNUNET_SCHEDULER_TaskContext *tc)
288 struct GNUNET_PEERINFO_Handle *h = cls;
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 (NULL != h->r_task)
305 GNUNET_SCHEDULER_cancel (h->r_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,
328 trigger_transmit (h);
333 * Transmit the request at the head of the transmission queue
334 * and trigger continuation (if any).
336 * @param cls the `struct GNUNET_PEERINFO_Handle *` (with the queue)
337 * @param size size of @a buf (0 on error)
338 * @param buf where to copy the message
339 * @return number of bytes copied to @a buf
342 do_transmit (void *cls,
346 struct GNUNET_PEERINFO_Handle *h = cls;
347 struct GNUNET_PEERINFO_AddContext *ac = h->ac_head;
352 return 0; /* request was cancelled in the meantime */
355 /* peerinfo service died */
356 LOG (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
357 "Failed to transmit message to `%s' service.\n",
359 GNUNET_CONTAINER_DLL_remove (h->ac_head, h->ac_tail, ac);
361 if (NULL != ac->cont)
362 ac->cont (ac->cont_cls,
363 _("failed to transmit request (service down?)"));
370 /* change in head of queue (i.e. cancel + add), try again */
371 trigger_transmit (h);
374 LOG (GNUNET_ERROR_TYPE_DEBUG,
375 "Transmitting request of size %u to `%s' service.\n",
378 memcpy (buf, &ac[1], ret);
379 GNUNET_CONTAINER_DLL_remove (h->ac_head, h->ac_tail, ac);
380 trigger_transmit (h);
381 if (NULL != ac->cont)
382 ac->cont (ac->cont_cls, NULL);
389 * Check if we have a request pending in the transmission queue and are
390 * able to transmit it right now. If so, schedule transmission.
392 * @param h handle to the service
395 trigger_transmit (struct GNUNET_PEERINFO_Handle *h)
397 struct GNUNET_PEERINFO_AddContext *ac;
399 if (NULL == (ac = h->ac_head))
400 return; /* no requests queued */
402 return; /* request already pending */
403 if (NULL == h->client)
405 /* disconnected, try to reconnect */
410 GNUNET_CLIENT_notify_transmit_ready (h->client,
412 GNUNET_TIME_UNIT_FOREVER_REL,
419 * Add a host to the persistent list. This method operates in
420 * semi-reliable mode: if the transmission is not completed by
421 * the time #GNUNET_PEERINFO_disconnect() is called, it will be
422 * aborted. Furthermore, if a second HELLO is added for the
423 * same peer before the first one was transmitted, PEERINFO may
424 * merge the two HELLOs prior to transmission to the service.
426 * @param h handle to the peerinfo service
427 * @param hello the verified (!) HELLO message
428 * @param cont continuation to call when done, NULL is allowed
429 * @param cont_cls closure for @a cont
430 * @return handle to cancel add operation; all pending
431 * 'add' operations will be cancelled automatically
432 * on disconnect, so it is not necessary to keep this
433 * handle (unless @a cont is NULL and at some point
434 * calling @a cont must be prevented)
436 struct GNUNET_PEERINFO_AddContext *
437 GNUNET_PEERINFO_add_peer (struct GNUNET_PEERINFO_Handle *h,
438 const struct GNUNET_HELLO_Message *hello,
439 GNUNET_PEERINFO_Continuation cont,
442 uint16_t hs = GNUNET_HELLO_size (hello);
443 struct GNUNET_PEERINFO_AddContext *ac;
444 struct GNUNET_PeerIdentity peer;
446 GNUNET_assert (GNUNET_OK == GNUNET_HELLO_get_id (hello, &peer));
447 LOG (GNUNET_ERROR_TYPE_DEBUG,
448 "Adding peer `%s' to PEERINFO database (%u bytes of HELLO)\n",
451 ac = GNUNET_malloc (sizeof (struct GNUNET_PEERINFO_AddContext) + hs);
455 ac->cont_cls = cont_cls;
456 memcpy (&ac[1], hello, hs);
457 GNUNET_CONTAINER_DLL_insert_tail (h->ac_head, h->ac_tail, ac);
458 trigger_transmit (h);
464 * Cancel pending 'add' operation. Must only be called before
465 * either 'cont' or #GNUNET_PEERINFO_disconnect() are invoked.
467 * @param ac handle for the add operation to cancel
470 GNUNET_PEERINFO_add_peer_cancel (struct GNUNET_PEERINFO_AddContext *ac)
472 struct GNUNET_PEERINFO_Handle *h = ac->h;
474 GNUNET_CONTAINER_DLL_remove (h->ac_head, h->ac_tail, ac);
480 * Type of a function to call when we receive a message from the
481 * service. Call the iterator with the result and (if applicable)
482 * continue to receive more messages or trigger processing the next
483 * event (if applicable).
486 * @param msg message received, NULL on timeout or fatal error
489 peerinfo_handler (void *cls,
490 const struct GNUNET_MessageHeader *msg)
492 struct GNUNET_PEERINFO_Handle *h = cls;
493 struct GNUNET_PEERINFO_IteratorContext *ic = h->ic_head;
494 const struct InfoMessage *im;
495 const struct GNUNET_HELLO_Message *hello;
496 GNUNET_PEERINFO_Processor cb;
497 struct GNUNET_PeerIdentity id;
501 h->in_receive = GNUNET_NO;
504 /* peerinfo service died, signal error */
508 cb_cls = ic->callback_cls;
509 GNUNET_PEERINFO_iterate_cancel (ic);
517 cb (cb_cls, NULL, NULL,
518 _("Failed to receive response from `PEERINFO' service."));
523 /* didn't expect a response, reconnect */
527 ic->request_transmitted = GNUNET_NO;
529 cb_cls = ic->callback_cls;
530 if (GNUNET_MESSAGE_TYPE_PEERINFO_INFO_END == ntohs (msg->type))
532 /* normal end of list of peers, signal end, process next pending request */
533 LOG (GNUNET_ERROR_TYPE_DEBUG,
534 "Received end of list of peers from `%s' service\n",
536 GNUNET_PEERINFO_iterate_cancel (ic);
537 trigger_transmit (h);
538 if ( (GNUNET_NO == h->in_receive) &&
539 (NULL != h->ic_head) )
541 h->in_receive = GNUNET_YES;
542 GNUNET_CLIENT_receive (h->client,
545 GNUNET_TIME_absolute_get_remaining (h->ic_head->timeout));
548 cb (cb_cls, NULL, NULL, NULL);
552 ms = ntohs (msg->size);
553 if ((ms < sizeof (struct InfoMessage)) ||
554 (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_PEERINFO_INFO))
556 /* malformed message */
558 GNUNET_PEERINFO_iterate_cancel (ic);
561 cb (cb_cls, NULL, NULL,
562 _("Received invalid message from `PEERINFO' service."));
565 im = (const struct InfoMessage *) msg;
566 GNUNET_break (0 == ntohl (im->reserved));
567 if ( (GNUNET_YES == ic->have_peer) &&
568 (0 != memcmp (&ic->peer,
570 sizeof (struct GNUNET_PeerIdentity))) )
572 /* bogus message (from a different iteration call?); out of sequence! */
573 LOG (GNUNET_ERROR_TYPE_ERROR,
574 "Received HELLO for peer `%s', expected peer `%s'\n",
575 GNUNET_i2s (&im->peer),
576 GNUNET_i2s (&ic->peer));
579 GNUNET_PEERINFO_iterate_cancel (ic);
585 _("Received invalid message from `PEERINFO' service."));
589 if (ms > sizeof (struct InfoMessage) + sizeof (struct GNUNET_MessageHeader))
591 hello = (const struct GNUNET_HELLO_Message *) &im[1];
592 if (ms != sizeof (struct InfoMessage) + GNUNET_HELLO_size (hello))
594 /* malformed message */
596 GNUNET_PEERINFO_iterate_cancel (ic);
602 _("Received invalid message from `PEERINFO' service."));
605 if (GNUNET_OK != GNUNET_HELLO_get_id (hello, &id))
607 /* malformed message */
609 GNUNET_PEERINFO_iterate_cancel (ic);
615 _("Received invalid message from `PEERINFO' service."));
618 if (0 != memcmp (&im->peer,
620 sizeof (struct GNUNET_PeerIdentity)))
622 /* malformed message */
624 GNUNET_PEERINFO_iterate_cancel (ic);
630 _("Received invalid message from `PEERINFO' service."));
635 /* normal data message */
636 LOG (GNUNET_ERROR_TYPE_DEBUG,
637 "Received %u bytes of `%s' information about peer `%s' from `%s' service\n",
638 (hello == NULL) ? 0 : (unsigned int) GNUNET_HELLO_size (hello),
640 GNUNET_i2s (&im->peer),
642 h->in_receive = GNUNET_YES;
643 GNUNET_CLIENT_receive (h->client,
646 GNUNET_TIME_absolute_get_remaining (ic->timeout));
656 * We've transmitted the iteration request. Now get ready to process
657 * the results (or handle transmission error).
659 * @param cls the `struct GNUNET_PEERINFO_IteratorContext *`
660 * @param emsg error message, NULL if transmission worked
663 iterator_start_receive (void *cls,
666 struct GNUNET_PEERINFO_IteratorContext *ic = cls;
667 struct GNUNET_PEERINFO_Handle *h = ic->h;
668 GNUNET_PEERINFO_Processor cb;
675 cb_cls = ic->callback_cls;
676 GNUNET_PEERINFO_iterate_cancel (ic);
679 cb (cb_cls, NULL, NULL, emsg);
682 LOG (GNUNET_ERROR_TYPE_DEBUG,
683 "Waiting for response from `%s' service.\n",
685 ic->request_transmitted = GNUNET_YES;
686 if (GNUNET_NO == h->in_receive)
688 h->in_receive = GNUNET_YES;
689 GNUNET_CLIENT_receive (h->client,
692 GNUNET_TIME_absolute_get_remaining (ic->timeout));
698 * Peerinfo iteration request has timed out.
700 * @param cls the `struct GNUNET_PEERINFO_IteratorContext *`
701 * @param tc scheduler context
704 signal_timeout (void *cls,
705 const struct GNUNET_SCHEDULER_TaskContext *tc)
707 struct GNUNET_PEERINFO_IteratorContext *ic = cls;
708 GNUNET_PEERINFO_Processor cb;
711 ic->timeout_task = NULL;
713 cb_cls = ic->callback_cls;
714 GNUNET_PEERINFO_iterate_cancel (ic);
719 _("Timeout transmitting iteration request to `PEERINFO' service."));
724 * Call a method for each known matching host. The callback method
725 * will be invoked once for each matching host and then finally once
726 * with a NULL pointer. After that final invocation, the iterator
727 * context must no longer be used.
729 * Instead of calling this function with `peer == NULL` it is often
730 * better to use #GNUNET_PEERINFO_notify().
732 * @param h handle to the peerinfo service
733 * @param include_friend_only include HELLO messages for friends only
734 * @param peer restrict iteration to this peer only (can be NULL)
735 * @param timeout how long to wait until timing out
736 * @param callback the method to call for each peer
737 * @param callback_cls closure for @a callback
738 * @return iterator context
740 struct GNUNET_PEERINFO_IteratorContext *
741 GNUNET_PEERINFO_iterate (struct GNUNET_PEERINFO_Handle *h,
742 int include_friend_only,
743 const struct GNUNET_PeerIdentity *peer,
744 struct GNUNET_TIME_Relative timeout,
745 GNUNET_PEERINFO_Processor callback,
748 struct ListAllPeersMessage *lapm;
749 struct ListPeerMessage *lpm;
750 struct GNUNET_PEERINFO_IteratorContext *ic;
751 struct GNUNET_PEERINFO_AddContext *ac;
753 ic = GNUNET_new (struct GNUNET_PEERINFO_IteratorContext);
756 LOG (GNUNET_ERROR_TYPE_DEBUG,
757 "Requesting list of peers from PEERINFO service\n");
759 GNUNET_malloc (sizeof (struct GNUNET_PEERINFO_AddContext) +
760 sizeof (struct ListAllPeersMessage));
761 ac->size = sizeof (struct ListAllPeersMessage);
762 lapm = (struct ListAllPeersMessage *) &ac[1];
763 lapm->header.size = htons (sizeof (struct ListAllPeersMessage));
764 lapm->header.type = htons (GNUNET_MESSAGE_TYPE_PEERINFO_GET_ALL);
765 lapm->include_friend_only = htonl (include_friend_only);
769 LOG (GNUNET_ERROR_TYPE_DEBUG,
770 "Requesting information on peer `%4s' from PEERINFO service\n",
773 GNUNET_malloc (sizeof (struct GNUNET_PEERINFO_AddContext) +
774 sizeof (struct ListPeerMessage));
775 ac->size = sizeof (struct ListPeerMessage);
776 lpm = (struct ListPeerMessage *) &ac[1];
777 lpm->header.size = htons (sizeof (struct ListPeerMessage));
778 lpm->header.type = htons (GNUNET_MESSAGE_TYPE_PEERINFO_GET);
779 lpm->include_friend_only = htonl (include_friend_only);
780 memcpy (&lpm->peer, peer, sizeof (struct GNUNET_PeerIdentity));
781 ic->have_peer = GNUNET_YES;
786 ic->callback = callback;
787 ic->callback_cls = callback_cls;
788 ic->timeout = GNUNET_TIME_relative_to_absolute (timeout);
790 GNUNET_SCHEDULER_add_delayed (timeout, &signal_timeout, ic);
791 ac->cont = &iterator_start_receive;
793 GNUNET_CONTAINER_DLL_insert_tail (h->ac_head, h->ac_tail, ac);
794 GNUNET_CONTAINER_DLL_insert_tail (h->ic_head,
797 trigger_transmit (h);
803 * Cancel an iteration over peer information.
805 * @param ic context of the iterator to cancel
808 GNUNET_PEERINFO_iterate_cancel (struct GNUNET_PEERINFO_IteratorContext *ic)
810 struct GNUNET_PEERINFO_Handle *h;
813 if (NULL != ic->timeout_task)
815 GNUNET_SCHEDULER_cancel (ic->timeout_task);
816 ic->timeout_task = NULL;
819 if (GNUNET_YES == ic->request_transmitted)
820 return; /* need to finish processing */
821 GNUNET_CONTAINER_DLL_remove (h->ic_head,
826 GNUNET_CONTAINER_DLL_remove (h->ac_head,
829 GNUNET_free (ic->ac);
835 /* end of peerinfo_api.c */