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 * Are we now receiving?
131 * Handle to the peerinfo service.
133 struct GNUNET_PEERINFO_Handle
138 const struct GNUNET_CONFIGURATION_Handle *cfg;
141 * Connection to the service.
143 struct GNUNET_CLIENT_Connection *client;
146 * Head of transmission queue.
148 struct GNUNET_PEERINFO_AddContext *ac_head;
151 * Tail of transmission queue.
153 struct GNUNET_PEERINFO_AddContext *ac_tail;
156 * Handle for the current transmission request, or NULL if none is pending.
158 struct GNUNET_CLIENT_TransmitHandle *th;
161 * Head of iterator DLL.
163 struct GNUNET_PEERINFO_IteratorContext *ic_head;
166 * Tail of iterator DLL.
168 struct GNUNET_PEERINFO_IteratorContext *ic_tail;
171 * ID for a reconnect task.
173 GNUNET_SCHEDULER_TaskIdentifier r_task;
176 * Set to GNUNET_YES if we are currently receiving replies from the
185 * Connect to the peerinfo service.
187 * @param cfg configuration to use
188 * @return NULL on error (configuration related, actual connection
189 * establishment may happen asynchronously).
191 struct GNUNET_PEERINFO_Handle *
192 GNUNET_PEERINFO_connect (const struct GNUNET_CONFIGURATION_Handle *cfg)
194 struct GNUNET_PEERINFO_Handle *h;
196 h = GNUNET_malloc (sizeof (struct GNUNET_PEERINFO_Handle));
197 h->client = GNUNET_CLIENT_connect ("peerinfo", cfg);
204 * Disconnect from the peerinfo service. Note that all iterators must
205 * have completed or have been cancelled by the time this function is
206 * called (otherwise, calling this function is a serious error).
207 * Furthermore, if 'GNUNET_PEERINFO_add_peer' operations are still
208 * pending, they will be cancelled silently on disconnect.
210 * @param h handle to disconnect
213 GNUNET_PEERINFO_disconnect (struct GNUNET_PEERINFO_Handle *h)
215 struct GNUNET_PEERINFO_AddContext *ac;
216 struct GNUNET_PEERINFO_IteratorContext *ic;
218 while (NULL != (ic = h->ic_head))
220 GNUNET_break (GNUNET_YES == ic->in_receive);
221 ic->in_receive = GNUNET_NO;
222 GNUNET_PEERINFO_iterate_cancel (ic);
224 while (NULL != (ac = h->ac_head))
226 GNUNET_CONTAINER_DLL_remove (h->ac_head, h->ac_tail, ac);
227 if (NULL != ac->cont)
228 ac->cont (ac->cont_cls, _("aborted due to explicit disconnect request"));
233 GNUNET_CLIENT_notify_transmit_ready_cancel (h->th);
236 if (NULL != h->client)
238 GNUNET_CLIENT_disconnect (h->client);
241 if (GNUNET_SCHEDULER_NO_TASK != h->r_task)
243 GNUNET_SCHEDULER_cancel (h->r_task);
244 h->r_task = GNUNET_SCHEDULER_NO_TASK;
251 * Check if we have a request pending in the transmission queue and are
252 * able to transmit it right now. If so, schedule transmission.
254 * @param h handle to the service
257 trigger_transmit (struct GNUNET_PEERINFO_Handle *h);
261 * Close the existing connection to PEERINFO and reconnect.
263 * @param h handle to the service
266 reconnect (struct GNUNET_PEERINFO_Handle *h);
270 * Task scheduled to re-try connecting to the peerinfo service.
272 * @param cls the 'struct GNUNET_PEERINFO_Handle'
273 * @param tc scheduler context
276 reconnect_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
278 struct GNUNET_PEERINFO_Handle *h = cls;
280 h->r_task = GNUNET_SCHEDULER_NO_TASK;
286 * Close the existing connection to PEERINFO and reconnect.
288 * @param h handle to the service
291 reconnect (struct GNUNET_PEERINFO_Handle *h)
293 if (GNUNET_SCHEDULER_NO_TASK != h->r_task)
295 GNUNET_SCHEDULER_cancel (h->r_task);
296 h->r_task = GNUNET_SCHEDULER_NO_TASK;
300 GNUNET_CLIENT_notify_transmit_ready_cancel (h->th);
303 if (NULL != h->client)
305 GNUNET_CLIENT_disconnect (h->client);
308 h->in_receive = GNUNET_NO;
309 h->client = GNUNET_CLIENT_connect ("peerinfo", h->cfg);
310 if (NULL == h->client)
313 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &reconnect_task,
317 trigger_transmit (h);
322 * Transmit the request at the head of the transmission queue
323 * and trigger continuation (if any).
325 * @param cls the 'struct GNUNET_PEERINFO_Handle' (with the queue)
326 * @param size size of the buffer (0 on error)
327 * @param buf where to copy the message
328 * @return number of bytes copied to buf
331 do_transmit (void *cls, size_t size, void *buf)
333 struct GNUNET_PEERINFO_Handle *h = cls;
334 struct GNUNET_PEERINFO_AddContext *ac = h->ac_head;
339 return 0; /* request was cancelled in the meantime */
342 /* peerinfo service died */
343 LOG (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
344 "Failed to transmit message to `%s' service.\n", "PEERINFO");
345 GNUNET_CONTAINER_DLL_remove (h->ac_head, h->ac_tail, ac);
347 if (NULL != ac->cont)
348 ac->cont (ac->cont_cls, _("failed to transmit request (service down?)"));
355 /* change in head of queue (i.e. cancel + add), try again */
356 trigger_transmit (h);
359 LOG (GNUNET_ERROR_TYPE_DEBUG,
360 "Transmitting request of size %u to `%s' service.\n", ret, "PEERINFO");
361 memcpy (buf, &ac[1], ret);
362 GNUNET_CONTAINER_DLL_remove (h->ac_head, h->ac_tail, ac);
363 trigger_transmit (h);
364 if (NULL != ac->cont)
365 ac->cont (ac->cont_cls, NULL);
372 * Check if we have a request pending in the transmission queue and are
373 * able to transmit it right now. If so, schedule transmission.
375 * @param h handle to the service
378 trigger_transmit (struct GNUNET_PEERINFO_Handle *h)
380 struct GNUNET_PEERINFO_AddContext *ac;
382 if (NULL == (ac = h->ac_head))
383 return; /* no requests queued */
385 return; /* request already pending */
386 if (GNUNET_YES == h->in_receive)
387 return; /* still reading replies from last request */
388 if (NULL == h->client)
390 /* disconnected, try to reconnect */
395 GNUNET_CLIENT_notify_transmit_ready (h->client, ac->size,
396 GNUNET_TIME_UNIT_FOREVER_REL,
403 * Add a host to the persistent list. This method operates in
404 * semi-reliable mode: if the transmission is not completed by
405 * the time 'GNUNET_PEERINFO_disconnect' is called, it will be
406 * aborted. Furthermore, if a second HELLO is added for the
407 * same peer before the first one was transmitted, PEERINFO may
408 * merge the two HELLOs prior to transmission to the service.
410 * @param h handle to the peerinfo service
411 * @param hello the verified (!) HELLO message
412 * @param cont continuation to call when done, NULL is allowed
413 * @param cont_cls closure for 'cont'
414 * @return handle to cancel add operation; all pending
415 * 'add' operations will be cancelled automatically
416 * on disconnect, so it is not necessary to keep this
417 * handle (unless 'cont' is NULL and at some point
418 * calling 'cont' must be prevented)
420 struct GNUNET_PEERINFO_AddContext *
421 GNUNET_PEERINFO_add_peer (struct GNUNET_PEERINFO_Handle *h,
422 const struct GNUNET_HELLO_Message *hello,
423 GNUNET_PEERINFO_Continuation cont,
426 uint16_t hs = GNUNET_HELLO_size (hello);
427 struct GNUNET_PEERINFO_AddContext *ac;
428 struct GNUNET_PeerIdentity peer;
430 GNUNET_assert (GNUNET_OK == GNUNET_HELLO_get_id (hello, &peer));
431 LOG (GNUNET_ERROR_TYPE_DEBUG,
432 "Adding peer `%s' to PEERINFO database (%u bytes of `%s')\n",
433 GNUNET_i2s (&peer), hs, "HELLO");
434 ac = GNUNET_malloc (sizeof (struct GNUNET_PEERINFO_AddContext) + hs);
438 ac->cont_cls = cont_cls;
439 memcpy (&ac[1], hello, hs);
440 GNUNET_CONTAINER_DLL_insert_tail (h->ac_head, h->ac_tail, ac);
441 trigger_transmit (h);
447 * Cancel pending 'add' operation. Must only be called before
448 * either 'cont' or 'GNUNET_PEERINFO_disconnect' are invoked.
450 * @param ac handle for the add operation to cancel
453 GNUNET_PEERINFO_add_peer_cancel (struct GNUNET_PEERINFO_AddContext *ac)
455 struct GNUNET_PEERINFO_Handle *h = ac->h;
457 GNUNET_CONTAINER_DLL_remove (h->ac_head, h->ac_tail, ac);
463 * Type of a function to call when we receive a message from the
464 * service. Call the iterator with the result and (if applicable)
465 * continue to receive more messages or trigger processing the next
466 * event (if applicable).
469 * @param msg message received, NULL on timeout or fatal error
472 peerinfo_handler (void *cls, const struct GNUNET_MessageHeader *msg)
474 struct GNUNET_PEERINFO_IteratorContext *ic = cls;
475 struct GNUNET_PEERINFO_Handle *h = ic->h;
476 const struct InfoMessage *im;
477 const struct GNUNET_HELLO_Message *hello;
478 GNUNET_PEERINFO_Processor cb;
482 h->in_receive = GNUNET_NO;
483 ic->in_receive = GNUNET_NO;
485 cb_cls = ic->callback_cls;
488 /* peerinfo service died, signal error */
489 GNUNET_PEERINFO_iterate_cancel (ic);
492 cb (cb_cls, NULL, NULL,
493 _("Failed to receive response from `PEERINFO' service."));
496 if (GNUNET_MESSAGE_TYPE_PEERINFO_INFO_END == ntohs (msg->type))
498 /* normal end of list of peers, signal end, process next pending request */
499 LOG (GNUNET_ERROR_TYPE_DEBUG,
500 "Received end of list of peers from `%s' service\n", "PEERINFO");
501 GNUNET_PEERINFO_iterate_cancel (ic);
502 trigger_transmit (h);
504 cb (cb_cls, NULL, NULL, NULL);
507 ms = ntohs (msg->size);
508 if ((ms < sizeof (struct InfoMessage)) ||
509 (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_PEERINFO_INFO))
511 /* malformed message */
513 GNUNET_PEERINFO_iterate_cancel (ic);
516 cb (cb_cls, NULL, NULL,
517 _("Received invalid message from `PEERINFO' service."));
520 im = (const struct InfoMessage *) msg;
521 GNUNET_break (0 == ntohl (im->reserved));
523 if (ms > sizeof (struct InfoMessage) + sizeof (struct GNUNET_MessageHeader))
525 hello = (const struct GNUNET_HELLO_Message *) &im[1];
526 if (ms != sizeof (struct InfoMessage) + GNUNET_HELLO_size (hello))
528 /* malformed message */
530 GNUNET_PEERINFO_iterate_cancel (ic);
533 cb (cb_cls, NULL, NULL,
534 _("Received invalid message from `PEERINFO' service."));
538 /* normal data message */
539 LOG (GNUNET_ERROR_TYPE_DEBUG,
540 "Received %u bytes of `%s' information about peer `%s' from `%s' service\n",
541 (hello == NULL) ? 0 : (unsigned int) GNUNET_HELLO_size (hello), "HELLO",
542 GNUNET_i2s (&im->peer), "PEERINFO");
543 h->in_receive = GNUNET_YES;
544 ic->in_receive = GNUNET_YES;
545 GNUNET_CLIENT_receive (h->client, &peerinfo_handler, ic,
546 GNUNET_TIME_absolute_get_remaining (ic->timeout));
548 cb (cb_cls, &im->peer, hello, NULL);
553 * We've transmitted the iteration request. Now get ready to process
554 * the results (or handle transmission error).
556 * @param cls the 'struct GNUNET_PEERINFO_IteratorContext'
557 * @param emsg error message, NULL if transmission worked
560 iterator_start_receive (void *cls, const char *emsg)
562 struct GNUNET_PEERINFO_IteratorContext *ic = cls;
563 struct GNUNET_PEERINFO_Handle *h = ic->h;
564 GNUNET_PEERINFO_Processor cb;
571 cb_cls = ic->callback_cls;
572 GNUNET_PEERINFO_iterate_cancel (ic);
575 cb (cb_cls, NULL, NULL, emsg);
578 LOG (GNUNET_ERROR_TYPE_DEBUG, "Waiting for response from `%s' service.\n",
580 h->in_receive = GNUNET_YES;
581 ic->in_receive = GNUNET_YES;
582 GNUNET_CLIENT_receive (h->client, &peerinfo_handler, ic,
583 GNUNET_TIME_absolute_get_remaining (ic->timeout));
588 * Peerinfo iteration request has timed out.
590 * @param cls the 'struct GNUNET_PEERINFO_IteratorContext*'
591 * @param tc scheduler context
594 signal_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
596 struct GNUNET_PEERINFO_IteratorContext *ic = cls;
597 GNUNET_PEERINFO_Processor cb;
600 ic->timeout_task = GNUNET_SCHEDULER_NO_TASK;
602 cb_cls = ic->callback_cls;
603 GNUNET_PEERINFO_iterate_cancel (ic);
605 cb (cb_cls, NULL, NULL,
606 _("Timeout transmitting iteration request to `PEERINFO' service."));
611 * Call a method for each known matching host and change its trust
612 * value. The callback method will be invoked once for each matching
613 * host and then finally once with a NULL pointer. After that final
614 * invocation, the iterator context must no longer be used.
616 * Instead of calling this function with 'peer == NULL' it is often
617 * better to use 'GNUNET_PEERINFO_notify'.
619 * @param h handle to the peerinfo service
620 * @param peer restrict iteration to this peer only (can be NULL)
621 * @param timeout how long to wait until timing out
622 * @param callback the method to call for each peer
623 * @param callback_cls closure for callback
624 * @return iterator context
626 struct GNUNET_PEERINFO_IteratorContext *
627 GNUNET_PEERINFO_iterate (struct GNUNET_PEERINFO_Handle *h,
628 const struct GNUNET_PeerIdentity *peer,
629 struct GNUNET_TIME_Relative timeout,
630 GNUNET_PEERINFO_Processor callback, void *callback_cls)
632 struct GNUNET_MessageHeader *lapm;
633 struct ListPeerMessage *lpm;
634 struct GNUNET_PEERINFO_IteratorContext *ic;
635 struct GNUNET_PEERINFO_AddContext *ac;
639 LOG (GNUNET_ERROR_TYPE_DEBUG,
640 "Requesting list of peers from PEERINFO service\n");
642 GNUNET_malloc (sizeof (struct GNUNET_PEERINFO_AddContext) +
643 sizeof (struct GNUNET_MessageHeader));
644 ac->size = sizeof (struct GNUNET_MessageHeader);
645 lapm = (struct GNUNET_MessageHeader *) &ac[1];
646 lapm->size = htons (sizeof (struct GNUNET_MessageHeader));
647 lapm->type = htons (GNUNET_MESSAGE_TYPE_PEERINFO_GET_ALL);
651 LOG (GNUNET_ERROR_TYPE_DEBUG,
652 "Requesting information on peer `%4s' from PEERINFO service\n",
655 GNUNET_malloc (sizeof (struct GNUNET_PEERINFO_AddContext) +
656 sizeof (struct ListPeerMessage));
657 ac->size = sizeof (struct ListPeerMessage);
658 lpm = (struct ListPeerMessage *) &ac[1];
659 lpm->header.size = htons (sizeof (struct ListPeerMessage));
660 lpm->header.type = htons (GNUNET_MESSAGE_TYPE_PEERINFO_GET);
661 memcpy (&lpm->peer, peer, sizeof (struct GNUNET_PeerIdentity));
663 ic = GNUNET_malloc (sizeof (struct GNUNET_PEERINFO_IteratorContext));
666 ic->callback = callback;
667 ic->callback_cls = callback_cls;
668 ic->timeout = GNUNET_TIME_relative_to_absolute (timeout);
670 GNUNET_SCHEDULER_add_delayed (timeout, &signal_timeout, ic);
671 ac->cont = &iterator_start_receive;
673 GNUNET_CONTAINER_DLL_insert_after (h->ac_head, h->ac_tail, h->ac_tail, ac);
674 GNUNET_CONTAINER_DLL_insert (h->ic_head,
677 trigger_transmit (h);
683 * Cancel an iteration over peer information.
685 * @param ic context of the iterator to cancel
688 GNUNET_PEERINFO_iterate_cancel (struct GNUNET_PEERINFO_IteratorContext *ic)
690 struct GNUNET_PEERINFO_Handle *h;
693 GNUNET_CONTAINER_DLL_remove (h->ic_head,
696 if (GNUNET_SCHEDULER_NO_TASK != ic->timeout_task)
698 GNUNET_SCHEDULER_cancel (ic->timeout_task);
699 ic->timeout_task = GNUNET_SCHEDULER_NO_TASK;
702 if (GNUNET_YES == ic->in_receive)
703 return; /* need to finish processing */
706 GNUNET_CONTAINER_DLL_remove (h->ac_head, h->ac_tail, ic->ac);
707 GNUNET_free (ic->ac);
713 /* end of peerinfo_api.c */