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 * Set to GNUNET_YES if we are currently receiving replies from the
137 int request_transmitted;
143 * Handle to the peerinfo service.
145 struct GNUNET_PEERINFO_Handle
150 const struct GNUNET_CONFIGURATION_Handle *cfg;
153 * Connection to the service.
155 struct GNUNET_CLIENT_Connection *client;
158 * Head of transmission queue.
160 struct GNUNET_PEERINFO_AddContext *ac_head;
163 * Tail of transmission queue.
165 struct GNUNET_PEERINFO_AddContext *ac_tail;
168 * Handle for the current transmission request, or NULL if none is pending.
170 struct GNUNET_CLIENT_TransmitHandle *th;
173 * Head of iterator DLL.
175 struct GNUNET_PEERINFO_IteratorContext *ic_head;
178 * Tail of iterator DLL.
180 struct GNUNET_PEERINFO_IteratorContext *ic_tail;
183 * ID for a reconnect task.
185 GNUNET_SCHEDULER_TaskIdentifier r_task;
188 * Are we now receiving?
196 * Connect to the peerinfo service.
198 * @param cfg configuration to use
199 * @return NULL on error (configuration related, actual connection
200 * establishment may happen asynchronously).
202 struct GNUNET_PEERINFO_Handle *
203 GNUNET_PEERINFO_connect (const struct GNUNET_CONFIGURATION_Handle *cfg)
205 struct GNUNET_PEERINFO_Handle *h;
207 h = GNUNET_malloc (sizeof (struct GNUNET_PEERINFO_Handle));
208 h->client = GNUNET_CLIENT_connect ("peerinfo", cfg);
215 * Disconnect from the peerinfo service. Note that all iterators must
216 * have completed or have been cancelled by the time this function is
217 * called (otherwise, calling this function is a serious error).
218 * Furthermore, if 'GNUNET_PEERINFO_add_peer' operations are still
219 * pending, they will be cancelled silently on disconnect.
221 * @param h handle to disconnect
224 GNUNET_PEERINFO_disconnect (struct GNUNET_PEERINFO_Handle *h)
226 struct GNUNET_PEERINFO_AddContext *ac;
227 struct GNUNET_PEERINFO_IteratorContext *ic;
229 while (NULL != (ic = h->ic_head))
231 GNUNET_break (GNUNET_YES == ic->request_transmitted);
232 ic->request_transmitted = GNUNET_NO;
233 GNUNET_PEERINFO_iterate_cancel (ic);
235 while (NULL != (ac = h->ac_head))
237 GNUNET_CONTAINER_DLL_remove (h->ac_head, h->ac_tail, ac);
238 if (NULL != ac->cont)
239 ac->cont (ac->cont_cls, _("aborted due to explicit disconnect request"));
244 GNUNET_CLIENT_notify_transmit_ready_cancel (h->th);
247 if (NULL != h->client)
249 GNUNET_CLIENT_disconnect (h->client);
252 if (GNUNET_SCHEDULER_NO_TASK != h->r_task)
254 GNUNET_SCHEDULER_cancel (h->r_task);
255 h->r_task = GNUNET_SCHEDULER_NO_TASK;
262 * Check if we have a request pending in the transmission queue and are
263 * able to transmit it right now. If so, schedule transmission.
265 * @param h handle to the service
268 trigger_transmit (struct GNUNET_PEERINFO_Handle *h);
272 * Close the existing connection to PEERINFO and reconnect.
274 * @param h handle to the service
277 reconnect (struct GNUNET_PEERINFO_Handle *h);
281 * Task scheduled to re-try connecting to the peerinfo service.
283 * @param cls the 'struct GNUNET_PEERINFO_Handle'
284 * @param tc scheduler context
287 reconnect_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
289 struct GNUNET_PEERINFO_Handle *h = cls;
291 h->r_task = GNUNET_SCHEDULER_NO_TASK;
297 * Close the existing connection to PEERINFO and reconnect.
299 * @param h handle to the service
302 reconnect (struct GNUNET_PEERINFO_Handle *h)
304 if (GNUNET_SCHEDULER_NO_TASK != h->r_task)
306 GNUNET_SCHEDULER_cancel (h->r_task);
307 h->r_task = GNUNET_SCHEDULER_NO_TASK;
311 GNUNET_CLIENT_notify_transmit_ready_cancel (h->th);
314 if (NULL != h->client)
316 GNUNET_CLIENT_disconnect (h->client);
319 h->in_receive = GNUNET_NO;
320 h->client = GNUNET_CLIENT_connect ("peerinfo", h->cfg);
321 if (NULL == h->client)
324 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &reconnect_task,
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 the buffer (0 on error)
338 * @param buf where to copy the message
339 * @return number of bytes copied to buf
342 do_transmit (void *cls, size_t size, void *buf)
344 struct GNUNET_PEERINFO_Handle *h = cls;
345 struct GNUNET_PEERINFO_AddContext *ac = h->ac_head;
350 return 0; /* request was cancelled in the meantime */
353 /* peerinfo service died */
354 LOG (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
355 "Failed to transmit message to `%s' service.\n", "PEERINFO");
356 GNUNET_CONTAINER_DLL_remove (h->ac_head, h->ac_tail, ac);
358 if (NULL != ac->cont)
359 ac->cont (ac->cont_cls, _("failed to transmit request (service down?)"));
366 /* change in head of queue (i.e. cancel + add), try again */
367 trigger_transmit (h);
370 LOG (GNUNET_ERROR_TYPE_DEBUG,
371 "Transmitting request of size %u to `%s' service.\n", ret, "PEERINFO");
372 memcpy (buf, &ac[1], ret);
373 GNUNET_CONTAINER_DLL_remove (h->ac_head, h->ac_tail, ac);
374 trigger_transmit (h);
375 if (NULL != ac->cont)
376 ac->cont (ac->cont_cls, NULL);
383 * Check if we have a request pending in the transmission queue and are
384 * able to transmit it right now. If so, schedule transmission.
386 * @param h handle to the service
389 trigger_transmit (struct GNUNET_PEERINFO_Handle *h)
391 struct GNUNET_PEERINFO_AddContext *ac;
393 if (NULL == (ac = h->ac_head))
394 return; /* no requests queued */
396 return; /* request already pending */
397 if (NULL == h->client)
399 /* disconnected, try to reconnect */
404 GNUNET_CLIENT_notify_transmit_ready (h->client, ac->size,
405 GNUNET_TIME_UNIT_FOREVER_REL,
412 * Add a host to the persistent list. This method operates in
413 * semi-reliable mode: if the transmission is not completed by
414 * the time 'GNUNET_PEERINFO_disconnect' is called, it will be
415 * aborted. Furthermore, if a second HELLO is added for the
416 * same peer before the first one was transmitted, PEERINFO may
417 * merge the two HELLOs prior to transmission to the service.
419 * @param h handle to the peerinfo service
420 * @param hello the verified (!) HELLO message
421 * @param cont continuation to call when done, NULL is allowed
422 * @param cont_cls closure for 'cont'
423 * @return handle to cancel add operation; all pending
424 * 'add' operations will be cancelled automatically
425 * on disconnect, so it is not necessary to keep this
426 * handle (unless 'cont' is NULL and at some point
427 * calling 'cont' must be prevented)
429 struct GNUNET_PEERINFO_AddContext *
430 GNUNET_PEERINFO_add_peer (struct GNUNET_PEERINFO_Handle *h,
431 const struct GNUNET_HELLO_Message *hello,
432 GNUNET_PEERINFO_Continuation cont,
435 uint16_t hs = GNUNET_HELLO_size (hello);
436 struct GNUNET_PEERINFO_AddContext *ac;
437 struct GNUNET_PeerIdentity peer;
439 GNUNET_assert (GNUNET_OK == GNUNET_HELLO_get_id (hello, &peer));
440 LOG (GNUNET_ERROR_TYPE_DEBUG,
441 "Adding peer `%s' to PEERINFO database (%u bytes of `%s')\n",
442 GNUNET_i2s (&peer), hs, "HELLO");
443 ac = GNUNET_malloc (sizeof (struct GNUNET_PEERINFO_AddContext) + hs);
447 ac->cont_cls = cont_cls;
448 memcpy (&ac[1], hello, hs);
449 GNUNET_CONTAINER_DLL_insert_tail (h->ac_head, h->ac_tail, ac);
450 trigger_transmit (h);
456 * Cancel pending 'add' operation. Must only be called before
457 * either 'cont' or 'GNUNET_PEERINFO_disconnect' are invoked.
459 * @param ac handle for the add operation to cancel
462 GNUNET_PEERINFO_add_peer_cancel (struct GNUNET_PEERINFO_AddContext *ac)
464 struct GNUNET_PEERINFO_Handle *h = ac->h;
466 GNUNET_CONTAINER_DLL_remove (h->ac_head, h->ac_tail, ac);
472 * Type of a function to call when we receive a message from the
473 * service. Call the iterator with the result and (if applicable)
474 * continue to receive more messages or trigger processing the next
475 * event (if applicable).
478 * @param msg message received, NULL on timeout or fatal error
481 peerinfo_handler (void *cls, const struct GNUNET_MessageHeader *msg)
483 struct GNUNET_PEERINFO_Handle *h = cls;
484 struct GNUNET_PEERINFO_IteratorContext *ic = h->ic_head;
485 const struct InfoMessage *im;
486 const struct GNUNET_HELLO_Message *hello;
487 GNUNET_PEERINFO_Processor cb;
488 struct GNUNET_PeerIdentity id;
492 h->in_receive = GNUNET_NO;
495 /* peerinfo service died, signal error */
499 cb_cls = ic->callback_cls;
500 GNUNET_PEERINFO_iterate_cancel (ic);
508 cb (cb_cls, NULL, NULL,
509 _("Failed to receive response from `PEERINFO' service."));
514 /* didn't expect a response, reconnect */
518 ic->request_transmitted = GNUNET_NO;
520 cb_cls = ic->callback_cls;
521 if (GNUNET_MESSAGE_TYPE_PEERINFO_INFO_END == ntohs (msg->type))
523 /* normal end of list of peers, signal end, process next pending request */
524 LOG (GNUNET_ERROR_TYPE_DEBUG,
525 "Received end of list of peers from `%s' service\n", "PEERINFO");
526 GNUNET_PEERINFO_iterate_cancel (ic);
527 trigger_transmit (h);
528 if ( (GNUNET_NO == h->in_receive) &&
529 (NULL != h->ic_head) )
531 h->in_receive = GNUNET_YES;
532 GNUNET_CLIENT_receive (h->client, &peerinfo_handler, h,
533 GNUNET_TIME_absolute_get_remaining (h->ic_head->timeout));
536 cb (cb_cls, NULL, NULL, NULL);
540 ms = ntohs (msg->size);
541 if ((ms < sizeof (struct InfoMessage)) ||
542 (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_PEERINFO_INFO))
544 /* malformed message */
546 GNUNET_PEERINFO_iterate_cancel (ic);
549 cb (cb_cls, NULL, NULL,
550 _("Received invalid message from `PEERINFO' service."));
553 im = (const struct InfoMessage *) msg;
554 GNUNET_break (0 == ntohl (im->reserved));
555 if ( (GNUNET_YES == ic->have_peer) &&
556 (0 != memcmp (&ic->peer, &im->peer, sizeof (struct GNUNET_PeerIdentity))) )
558 /* bogus message (from a different iteration call?); out of sequence! */
559 LOG (GNUNET_ERROR_TYPE_ERROR,
560 "Received HELLO for peer `%s', expected peer `%s'\n",
561 GNUNET_h2s (&im->peer.hashPubKey),
562 GNUNET_i2s (&ic->peer));
565 GNUNET_PEERINFO_iterate_cancel (ic);
568 cb (cb_cls, NULL, NULL,
569 _("Received invalid message from `PEERINFO' service."));
573 if (ms > sizeof (struct InfoMessage) + sizeof (struct GNUNET_MessageHeader))
575 hello = (const struct GNUNET_HELLO_Message *) &im[1];
576 if (ms != sizeof (struct InfoMessage) + GNUNET_HELLO_size (hello))
578 /* malformed message */
580 GNUNET_PEERINFO_iterate_cancel (ic);
583 cb (cb_cls, NULL, NULL,
584 _("Received invalid message from `PEERINFO' service."));
587 if (GNUNET_OK != GNUNET_HELLO_get_id (hello, &id))
589 /* malformed message */
591 GNUNET_PEERINFO_iterate_cancel (ic);
594 cb (cb_cls, NULL, NULL,
595 _("Received invalid message from `PEERINFO' service."));
598 if (0 != memcmp (&im->peer, &id, sizeof (struct GNUNET_PeerIdentity)))
600 /* malformed message */
602 GNUNET_PEERINFO_iterate_cancel (ic);
605 cb (cb_cls, NULL, NULL,
606 _("Received invalid message from `PEERINFO' service."));
611 /* normal data message */
612 LOG (GNUNET_ERROR_TYPE_DEBUG,
613 "Received %u bytes of `%s' information about peer `%s' from `%s' service\n",
614 (hello == NULL) ? 0 : (unsigned int) GNUNET_HELLO_size (hello), "HELLO",
615 GNUNET_i2s (&im->peer), "PEERINFO");
616 h->in_receive = GNUNET_YES;
617 GNUNET_CLIENT_receive (h->client, &peerinfo_handler, h,
618 GNUNET_TIME_absolute_get_remaining (ic->timeout));
620 cb (cb_cls, &im->peer, hello, NULL);
625 * We've transmitted the iteration request. Now get ready to process
626 * the results (or handle transmission error).
628 * @param cls the 'struct GNUNET_PEERINFO_IteratorContext'
629 * @param emsg error message, NULL if transmission worked
632 iterator_start_receive (void *cls, const char *emsg)
634 struct GNUNET_PEERINFO_IteratorContext *ic = cls;
635 struct GNUNET_PEERINFO_Handle *h = ic->h;
636 GNUNET_PEERINFO_Processor cb;
643 cb_cls = ic->callback_cls;
644 GNUNET_PEERINFO_iterate_cancel (ic);
647 cb (cb_cls, NULL, NULL, emsg);
650 LOG (GNUNET_ERROR_TYPE_DEBUG, "Waiting for response from `%s' service.\n",
652 ic->request_transmitted = GNUNET_YES;
653 if (GNUNET_NO == h->in_receive)
655 h->in_receive = GNUNET_YES;
656 GNUNET_CLIENT_receive (h->client, &peerinfo_handler, h,
657 GNUNET_TIME_absolute_get_remaining (ic->timeout));
663 * Peerinfo iteration request has timed out.
665 * @param cls the 'struct GNUNET_PEERINFO_IteratorContext*'
666 * @param tc scheduler context
669 signal_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
671 struct GNUNET_PEERINFO_IteratorContext *ic = cls;
672 GNUNET_PEERINFO_Processor cb;
675 ic->timeout_task = GNUNET_SCHEDULER_NO_TASK;
677 cb_cls = ic->callback_cls;
678 GNUNET_PEERINFO_iterate_cancel (ic);
680 cb (cb_cls, NULL, NULL,
681 _("Timeout transmitting iteration request to `PEERINFO' service."));
686 * Call a method for each known matching host. The callback method
687 * will be invoked once for each matching host and then finally once
688 * with a NULL pointer. After that final invocation, the iterator
689 * context must no longer be used.
691 * Instead of calling this function with 'peer == NULL' it is often
692 * better to use 'GNUNET_PEERINFO_notify'.
694 * @param h handle to the peerinfo service
695 * @param include_friend_only include HELLO messages for friends only
696 * @param peer restrict iteration to this peer only (can be NULL)
697 * @param timeout how long to wait until timing out
698 * @param callback the method to call for each peer
699 * @param callback_cls closure for callback
700 * @return iterator context
702 struct GNUNET_PEERINFO_IteratorContext *
703 GNUNET_PEERINFO_iterate (struct GNUNET_PEERINFO_Handle *h,
704 int include_friend_only,
705 const struct GNUNET_PeerIdentity *peer,
706 struct GNUNET_TIME_Relative timeout,
707 GNUNET_PEERINFO_Processor callback, void *callback_cls)
709 struct ListAllPeersMessage *lapm;
710 struct ListPeerMessage *lpm;
711 struct GNUNET_PEERINFO_IteratorContext *ic;
712 struct GNUNET_PEERINFO_AddContext *ac;
714 ic = GNUNET_malloc (sizeof (struct GNUNET_PEERINFO_IteratorContext));
717 LOG (GNUNET_ERROR_TYPE_DEBUG,
718 "Requesting list of peers from PEERINFO service\n");
720 GNUNET_malloc (sizeof (struct GNUNET_PEERINFO_AddContext) +
721 sizeof (struct ListAllPeersMessage));
722 ac->size = sizeof (struct ListAllPeersMessage);
723 lapm = (struct ListAllPeersMessage *) &ac[1];
724 lapm->header.size = htons (sizeof (struct ListAllPeersMessage));
725 lapm->header.type = htons (GNUNET_MESSAGE_TYPE_PEERINFO_GET_ALL);
726 lapm->include_friend_only = htonl (include_friend_only);
730 LOG (GNUNET_ERROR_TYPE_DEBUG,
731 "Requesting information on peer `%4s' from PEERINFO service\n",
734 GNUNET_malloc (sizeof (struct GNUNET_PEERINFO_AddContext) +
735 sizeof (struct ListPeerMessage));
736 ac->size = sizeof (struct ListPeerMessage);
737 lpm = (struct ListPeerMessage *) &ac[1];
738 lpm->header.size = htons (sizeof (struct ListPeerMessage));
739 lpm->header.type = htons (GNUNET_MESSAGE_TYPE_PEERINFO_GET);
740 lpm->include_friend_only = htonl (include_friend_only);
741 memcpy (&lpm->peer, peer, sizeof (struct GNUNET_PeerIdentity));
742 ic->have_peer = GNUNET_YES;
747 ic->callback = callback;
748 ic->callback_cls = callback_cls;
749 ic->timeout = GNUNET_TIME_relative_to_absolute (timeout);
751 GNUNET_SCHEDULER_add_delayed (timeout, &signal_timeout, ic);
752 ac->cont = &iterator_start_receive;
754 GNUNET_CONTAINER_DLL_insert_tail (h->ac_head, h->ac_tail, ac);
755 GNUNET_CONTAINER_DLL_insert_tail (h->ic_head,
758 trigger_transmit (h);
764 * Cancel an iteration over peer information.
766 * @param ic context of the iterator to cancel
769 GNUNET_PEERINFO_iterate_cancel (struct GNUNET_PEERINFO_IteratorContext *ic)
771 struct GNUNET_PEERINFO_Handle *h;
774 if (GNUNET_SCHEDULER_NO_TASK != ic->timeout_task)
776 GNUNET_SCHEDULER_cancel (ic->timeout_task);
777 ic->timeout_task = GNUNET_SCHEDULER_NO_TASK;
780 if (GNUNET_YES == ic->request_transmitted)
781 return; /* need to finish processing */
782 GNUNET_CONTAINER_DLL_remove (h->ic_head,
787 GNUNET_CONTAINER_DLL_remove (h->ac_head, h->ac_tail, ic->ac);
788 GNUNET_free (ic->ac);
794 /* end of peerinfo_api.c */