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,
45 * Entry in the transmission queue to PEERINFO service.
47 struct TransmissionQueueEntry
50 * This is a linked list.
52 struct TransmissionQueueEntry *next;
55 * This is a linked list.
57 struct TransmissionQueueEntry *prev;
60 * Function to call after request has been transmitted, or NULL (in which
61 * case we must consider sending the next entry immediately).
63 TransmissionContinuation cont;
71 * Timeout for the operation.
73 struct GNUNET_TIME_Absolute timeout;
76 * Number of bytes of the request message (follows after this struct).
84 * Handle to the peerinfo service.
86 struct GNUNET_PEERINFO_Handle
91 const struct GNUNET_CONFIGURATION_Handle *cfg;
94 * Connection to the service.
96 struct GNUNET_CLIENT_Connection *client;
99 * Head of transmission queue.
101 struct TransmissionQueueEntry *tq_head;
104 * Tail of transmission queue.
106 struct TransmissionQueueEntry *tq_tail;
109 * Handle for the current transmission request, or NULL if none is pending.
111 struct GNUNET_CLIENT_TransmitHandle *th;
114 * Set to GNUNET_YES if we are currently receiving replies from the
123 * Connect to the peerinfo service.
125 * @param cfg configuration to use
126 * @return NULL on error (configuration related, actual connection
127 * establishment may happen asynchronously).
129 struct GNUNET_PEERINFO_Handle *
130 GNUNET_PEERINFO_connect (const struct GNUNET_CONFIGURATION_Handle *cfg)
132 struct GNUNET_CLIENT_Connection *client;
133 struct GNUNET_PEERINFO_Handle *ret;
135 client = GNUNET_CLIENT_connect ("peerinfo", cfg);
138 ret = GNUNET_malloc (sizeof (struct GNUNET_PEERINFO_Handle));
139 ret->client = client;
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,
164 if (tqe->cont != NULL)
165 tqe->cont (tqe->cont_cls, GNUNET_SYSERR);
170 GNUNET_CLIENT_notify_transmit_ready_cancel (h->th);
173 GNUNET_CLIENT_disconnect (h->client, GNUNET_NO);
179 * Check if we have a request pending in the transmission queue and are
180 * able to transmit it right now. If so, schedule transmission.
182 * @param h handle to the service
185 trigger_transmit (struct GNUNET_PEERINFO_Handle *h);
189 * Close the existing connection to PEERINFO and reconnect.
191 * @param h handle to the service
194 reconnect (struct GNUNET_PEERINFO_Handle *h)
196 GNUNET_CLIENT_disconnect (h->client, GNUNET_SYSERR);
198 h->client = GNUNET_CLIENT_connect ("peerinfo", h->cfg);
199 GNUNET_assert (h->client != NULL);
204 * Transmit the request at the head of the transmission queue
205 * and trigger continuation (if any).
207 * @param cls the 'struct GNUNET_PEERINFO_Handle' (with the queue)
208 * @param size size of the buffer (0 on error)
209 * @param buf where to copy the message
210 * @return number of bytes copied to buf
213 do_transmit (void *cls, size_t size, void *buf)
215 struct GNUNET_PEERINFO_Handle *h = cls;
216 struct TransmissionQueueEntry *tqe = h->tq_head;
222 GNUNET_log (GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK,
223 _("Failed to transmit message to `%s' service.\n"),
226 GNUNET_CONTAINER_DLL_remove (h->tq_head,
230 trigger_transmit (h);
233 if (tqe->cont != NULL)
234 tqe->cont (tqe->cont_cls, GNUNET_SYSERR);
239 /* If it can be NULL above, it can be NULL here to... */
244 GNUNET_assert (size >= ret);
245 memcpy (buf, &tqe[1], ret);
247 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
248 "Transmitting request of size %u to `%s' service.\n",
252 GNUNET_CONTAINER_DLL_remove (h->tq_head,
255 if (tqe->cont != NULL)
256 tqe->cont (tqe->cont_cls, GNUNET_OK);
258 trigger_transmit (h);
265 * Check if we have a request pending in the transmission queue and are
266 * able to transmit it right now. If so, schedule transmission.
268 * @param h handle to the service
271 trigger_transmit (struct GNUNET_PEERINFO_Handle *h)
273 struct TransmissionQueueEntry *tqe;
275 if (NULL == (tqe = h->tq_head))
279 if (h->in_receive == GNUNET_YES)
281 h->th = GNUNET_CLIENT_notify_transmit_ready (h->client,
283 GNUNET_TIME_absolute_get_remaining (tqe->timeout),
290 * Add a host to the persistent list. This method operates in
291 * semi-reliable mode: if the transmission is not completed by
292 * the time 'GNUNET_PEERINFO_disconnect' is called, it will be
293 * aborted. Furthermore, if a second HELLO is added for the
294 * same peer before the first one was transmitted, PEERINFO may
295 * merge the two HELLOs prior to transmission to the service.
297 * @param h handle to the peerinfo service
298 * @param hello the verified (!) HELLO message
301 GNUNET_PEERINFO_add_peer (struct GNUNET_PEERINFO_Handle *h,
302 const struct GNUNET_HELLO_Message *hello)
304 uint16_t hs = GNUNET_HELLO_size (hello);
305 struct TransmissionQueueEntry *tqe;
308 struct GNUNET_PeerIdentity peer;
309 GNUNET_assert (GNUNET_OK == GNUNET_HELLO_get_id (hello, &peer));
310 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
311 "Adding peer `%s' to PEERINFO database (%u bytes of `%s')\n",
316 tqe = GNUNET_malloc (sizeof (struct TransmissionQueueEntry) + hs);
318 tqe->timeout = GNUNET_TIME_UNIT_FOREVER_ABS;
319 memcpy (&tqe[1], hello, hs);
320 GNUNET_CONTAINER_DLL_insert_after (h->tq_head,
324 trigger_transmit (h);
329 * Context for an iteration request.
331 struct GNUNET_PEERINFO_IteratorContext
334 * Handle to the PEERINFO service.
336 struct GNUNET_PEERINFO_Handle *h;
339 * Function to call with the results.
341 GNUNET_PEERINFO_Processor callback;
344 * Closure for 'callback'.
349 * Our entry in the transmission queue.
351 struct TransmissionQueueEntry *tqe;
354 * Task responsible for timeout.
356 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
359 * Timeout for the operation.
361 struct GNUNET_TIME_Absolute timeout;
364 * Are we now receiving?
371 * Type of a function to call when we receive a message
375 * @param msg message received, NULL on timeout or fatal error
378 peerinfo_handler (void *cls, const struct GNUNET_MessageHeader *msg)
380 struct GNUNET_PEERINFO_IteratorContext *ic = cls;
381 const struct InfoMessage *im;
382 const struct GNUNET_HELLO_Message *hello;
385 ic->h->in_receive = GNUNET_NO;
389 GNUNET_asprintf(&err_msg,
390 _("Failed to receive response from `%s' service."),
392 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "%s\n", err_msg);
394 trigger_transmit (ic->h);
395 if (ic->timeout_task != GNUNET_SCHEDULER_NO_TASK)
396 GNUNET_SCHEDULER_cancel (ic->timeout_task);
397 if (ic->callback != NULL)
398 ic->callback (ic->callback_cls, NULL, NULL, err_msg);
399 GNUNET_free (err_msg);
403 if (ntohs (msg->type) == GNUNET_MESSAGE_TYPE_PEERINFO_INFO_END)
406 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
407 "Received end of list of peers from `%s' service\n",
410 trigger_transmit (ic->h);
411 if (ic->timeout_task != GNUNET_SCHEDULER_NO_TASK)
412 GNUNET_SCHEDULER_cancel (ic->timeout_task);
413 if (ic->callback != NULL)
414 ic->callback (ic->callback_cls, NULL, NULL, NULL);
418 ms = ntohs (msg->size);
419 if ((ms < sizeof (struct InfoMessage)) ||
420 (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_PEERINFO_INFO))
423 GNUNET_asprintf(&err_msg,_("Received invalid message from `%s' service.\n"),"PEERINFO");
424 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,err_msg);
427 trigger_transmit (ic->h);
428 if (ic->timeout_task != GNUNET_SCHEDULER_NO_TASK)
429 GNUNET_SCHEDULER_cancel (ic->timeout_task);
430 if (ic->callback != NULL)
431 ic->callback (ic->callback_cls, NULL, NULL, err_msg);
432 GNUNET_free (err_msg);
436 im = (const struct InfoMessage *) msg;
437 GNUNET_break (0 == ntohl (im->reserved));
439 if (ms > sizeof (struct InfoMessage) + sizeof (struct GNUNET_MessageHeader))
441 hello = (const struct GNUNET_HELLO_Message *) &im[1];
442 if (ms != sizeof (struct InfoMessage) + GNUNET_HELLO_size (hello))
445 GNUNET_asprintf(&err_msg,_("Received invalid message from `%s' service.\n"),"PEERINFO");
448 trigger_transmit (ic->h);
449 if (ic->timeout_task != GNUNET_SCHEDULER_NO_TASK)
450 GNUNET_SCHEDULER_cancel (ic->timeout_task);
451 if (ic->callback != NULL)
452 ic->callback (ic->callback_cls, NULL, NULL, err_msg);
454 GNUNET_free (err_msg);
459 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
460 "Received %u bytes of `%s' information about peer `%s' from `%s' service\n",
461 (hello == NULL) ? 0 : (unsigned int) GNUNET_HELLO_size (hello),
463 GNUNET_i2s (&im->peer),
466 ic->h->in_receive = GNUNET_YES;
467 if (ic->callback != NULL)
468 ic->callback (ic->callback_cls, &im->peer, hello, NULL);
469 GNUNET_CLIENT_receive (ic->h->client,
472 GNUNET_TIME_absolute_get_remaining (ic->timeout));
477 * We've transmitted the iteration request. Now get ready to process
478 * the results (or handle transmission error).
480 * @param cls the 'struct GNUNET_PEERINFO_IteratorContext'
481 * @param transmit_success GNUNET_OK if transmission worked
484 iterator_start_receive (void *cls,
485 int transmit_success)
487 struct GNUNET_PEERINFO_IteratorContext *ic = cls;
489 if (GNUNET_OK != transmit_success)
492 GNUNET_asprintf(&err_msg,_("Failed to transmit iteration request to `%s' service (%d).\n"),"PEERINFO",transmit_success);
493 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,err_msg);
494 if (ic->timeout_task != GNUNET_SCHEDULER_NO_TASK)
496 GNUNET_SCHEDULER_cancel (ic->timeout_task);
497 ic->timeout_task = GNUNET_SCHEDULER_NO_TASK;
500 trigger_transmit (ic->h);
501 if (ic->callback != NULL)
502 ic->callback (ic->callback_cls, NULL, NULL, err_msg);
503 GNUNET_free (err_msg);
508 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
509 "Waiting for response from `%s' service.\n",
512 ic->h->in_receive = GNUNET_YES;
513 ic->in_receive = GNUNET_YES;
515 GNUNET_CLIENT_receive (ic->h->client,
518 GNUNET_TIME_absolute_get_remaining (ic->timeout));
523 * Peerinfo iteration request has timed out.
525 * @param cls the 'struct GNUNET_PEERINFO_IteratorContext*'
526 * @param tc scheduler context
529 signal_timeout (void *cls,
530 const struct GNUNET_SCHEDULER_TaskContext *tc)
532 struct GNUNET_PEERINFO_IteratorContext *ic = cls;
535 GNUNET_asprintf(&err_msg,_("Timeout transmitting iteration request to `%s' service.\n"),
538 ic->timeout_task = GNUNET_SCHEDULER_NO_TASK;
539 if (! ic->in_receive)
540 GNUNET_CONTAINER_DLL_remove (ic->h->tq_head,
544 ic->callback (ic->callback_cls, NULL, NULL, err_msg);
546 GNUNET_free_non_null (ic->tqe);
547 GNUNET_free (err_msg);
553 * Call a method for each known matching host and change its trust
554 * value. The callback method will be invoked once for each matching
555 * host and then finally once with a NULL pointer. After that final
556 * invocation, the iterator context must no longer be used.
558 * Instead of calling this function with 'peer == NULL' it is often
559 * better to use 'GNUNET_PEERINFO_notify'.
561 * @param h handle to the peerinfo service
562 * @param peer restrict iteration to this peer only (can be NULL)
563 * @param timeout how long to wait until timing out
564 * @param callback the method to call for each peer
565 * @param callback_cls closure for callback
566 * @return iterator context
568 struct GNUNET_PEERINFO_IteratorContext *
569 GNUNET_PEERINFO_iterate (struct GNUNET_PEERINFO_Handle *h,
570 const struct GNUNET_PeerIdentity *peer,
571 struct GNUNET_TIME_Relative timeout,
572 GNUNET_PEERINFO_Processor callback,
575 struct GNUNET_MessageHeader *lapm;
576 struct ListPeerMessage *lpm;
577 struct GNUNET_PEERINFO_IteratorContext *ic;
578 struct TransmissionQueueEntry *tqe;
583 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
584 "Requesting list of peers from PEERINFO service\n");
586 tqe = GNUNET_malloc (sizeof (struct TransmissionQueueEntry) +
587 sizeof (struct GNUNET_MessageHeader));
588 tqe->size = sizeof (struct GNUNET_MessageHeader);
589 lapm = (struct GNUNET_MessageHeader *) &tqe[1];
590 lapm->size = htons (sizeof (struct GNUNET_MessageHeader));
591 lapm->type = htons (GNUNET_MESSAGE_TYPE_PEERINFO_GET_ALL);
596 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
597 "Requesting information on peer `%4s' from PEERINFO service\n",
600 tqe = GNUNET_malloc (sizeof (struct TransmissionQueueEntry) +
601 sizeof (struct ListPeerMessage));
602 tqe->size = sizeof (struct ListPeerMessage);
603 lpm = (struct ListPeerMessage *) &tqe[1];
604 lpm->header.size = htons (sizeof (struct ListPeerMessage));
605 lpm->header.type = htons (GNUNET_MESSAGE_TYPE_PEERINFO_GET);
606 memcpy (&lpm->peer, peer, sizeof (struct GNUNET_PeerIdentity));
608 ic = GNUNET_malloc (sizeof (struct GNUNET_PEERINFO_IteratorContext));
611 ic->callback = callback;
612 ic->callback_cls = callback_cls;
613 ic->timeout = GNUNET_TIME_relative_to_absolute (timeout);
614 ic->timeout_task = GNUNET_SCHEDULER_add_delayed (timeout,
617 tqe->timeout = ic->timeout;
618 tqe->cont = &iterator_start_receive;
620 tqe->timeout = ic->timeout;
621 GNUNET_CONTAINER_DLL_insert_after (h->tq_head,
625 trigger_transmit (h);
632 * Cancel an iteration over peer information.
634 * @param ic context of the iterator to cancel
637 GNUNET_PEERINFO_iterate_cancel (struct GNUNET_PEERINFO_IteratorContext *ic)
639 if (ic->timeout_task != GNUNET_SCHEDULER_NO_TASK)
641 GNUNET_SCHEDULER_cancel (ic->timeout_task);
642 ic->timeout_task = GNUNET_SCHEDULER_NO_TASK;
646 return; /* need to finish processing */
647 GNUNET_CONTAINER_DLL_remove (ic->h->tq_head,
650 GNUNET_free (ic->tqe);
655 /* end of peerinfo_api.c */