2 This file is part of GNUnet.
3 Copyright (C) 2001-2014 GNUnet e.V.
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., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, 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 * Context for an iteration request.
37 struct GNUNET_PEERINFO_IteratorContext
43 struct GNUNET_PEERINFO_IteratorContext *next;
48 struct GNUNET_PEERINFO_IteratorContext *prev;
51 * Handle to the PEERINFO service.
53 struct GNUNET_PEERINFO_Handle *h;
56 * Function to call with the results.
58 GNUNET_PEERINFO_Processor callback;
61 * Closure for @e callback.
66 * Peer we are interested in (only valid if iteration was restricted to one peer).
68 struct GNUNET_PeerIdentity peer;
76 * Only include friends in reply?
78 int include_friend_only;
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_MQ_Handle *mq;
99 * Head of iterator DLL.
101 struct GNUNET_PEERINFO_IteratorContext *ic_head;
104 * Tail of iterator DLL.
106 struct GNUNET_PEERINFO_IteratorContext *ic_tail;
109 * ID for a reconnect task.
111 struct GNUNET_SCHEDULER_Task *r_task;
117 * Close the existing connection to PEERINFO and reconnect.
119 * @param h handle to the service
122 reconnect (struct GNUNET_PEERINFO_Handle *h);
126 * Connect to the peerinfo service.
128 * @param cfg configuration to use
129 * @return NULL on error (configuration related, actual connection
130 * establishment may happen asynchronously).
132 struct GNUNET_PEERINFO_Handle *
133 GNUNET_PEERINFO_connect (const struct GNUNET_CONFIGURATION_Handle *cfg)
135 struct GNUNET_PEERINFO_Handle *h;
137 h = GNUNET_new (struct GNUNET_PEERINFO_Handle);
150 * Disconnect from the peerinfo service. Note that all iterators must
151 * have completed or have been cancelled by the time this function is
152 * called (otherwise, calling this function is a serious error).
153 * Furthermore, if #GNUNET_PEERINFO_add_peer() operations are still
154 * pending, they will be cancelled silently on disconnect.
156 * @param h handle to disconnect
159 GNUNET_PEERINFO_disconnect (struct GNUNET_PEERINFO_Handle *h)
161 struct GNUNET_PEERINFO_IteratorContext *ic;
163 while (NULL != (ic = h->ic_head))
165 GNUNET_CONTAINER_DLL_remove (h->ic_head,
172 GNUNET_MQ_destroy (h->mq);
175 if (NULL != h->r_task)
177 GNUNET_SCHEDULER_cancel (h->r_task);
185 * Task scheduled to re-try connecting to the peerinfo service.
187 * @param cls the `struct GNUNET_PEERINFO_Handle *`
190 reconnect_task (void *cls)
192 struct GNUNET_PEERINFO_Handle *h = cls;
200 * We encountered an error, reconnect to the PEERINFO service.
202 * @param h handle to reconnect
205 do_reconnect (struct GNUNET_PEERINFO_Handle *h)
207 struct GNUNET_PEERINFO_IteratorContext *ic = h->ic_head;
209 GNUNET_MQ_destroy (h->mq);
213 GNUNET_CONTAINER_DLL_remove (h->ic_head,
216 if (NULL != ic->callback)
217 ic->callback (ic->callback_cls,
220 _("Failed to receive response from `PEERINFO' service."));
223 h->r_task = GNUNET_SCHEDULER_add_now (&reconnect_task,
229 * We got a disconnect after asking regex to do the announcement.
232 * @param cls the `struct GNUNET_PEERINFO_Handle` to retry
233 * @param error error code
236 mq_error_handler (void *cls,
237 enum GNUNET_MQ_Error error)
239 struct GNUNET_PEERINFO_Handle *h = cls;
247 * Function called when we receive an info message. Check it is
251 * @param im message received
252 * @return #GNUNET_OK if the message is OK
255 check_info (void *cls,
256 const struct InfoMessage *im)
258 struct GNUNET_PEERINFO_Handle *h = cls;
259 struct GNUNET_PEERINFO_IteratorContext *ic = h->ic_head;
260 uint16_t ms = ntohs (im->header.size) - sizeof (*im);
262 if (0 != ntohl (im->reserved))
265 return GNUNET_SYSERR;
269 /* didn't expect a response, bad */
271 return GNUNET_SYSERR;
273 if ( (GNUNET_YES == ic->have_peer) &&
274 (0 != memcmp (&ic->peer,
276 sizeof (struct GNUNET_PeerIdentity))) )
278 /* bogus message (from a different iteration call?); out of sequence! */
279 LOG (GNUNET_ERROR_TYPE_ERROR,
280 "Received HELLO for peer `%s', expected peer `%s'\n",
281 GNUNET_i2s (&im->peer),
282 GNUNET_i2s (&ic->peer));
284 return GNUNET_SYSERR;
286 if (ms > sizeof (struct GNUNET_MessageHeader))
288 const struct GNUNET_HELLO_Message *hello;
289 struct GNUNET_PeerIdentity id;
291 hello = (const struct GNUNET_HELLO_Message *) &im[1];
292 if (ms != GNUNET_HELLO_size (hello))
294 /* malformed message */
296 return GNUNET_SYSERR;
299 GNUNET_HELLO_get_id (hello,
302 /* malformed message */
304 return GNUNET_SYSERR;
306 if (0 != memcmp (&im->peer,
308 sizeof (struct GNUNET_PeerIdentity)))
310 /* malformed message */
312 return GNUNET_SYSERR;
317 /* malformed message */
319 return GNUNET_SYSERR;
326 * Handle info message.
329 * @param im message received
332 handle_info (void *cls,
333 const struct InfoMessage *im)
335 struct GNUNET_PEERINFO_Handle *h = cls;
336 struct GNUNET_PEERINFO_IteratorContext *ic = h->ic_head;
337 const struct GNUNET_HELLO_Message *hello;
340 ms = ntohs (im->header.size);
341 hello = (0 == ms) ? NULL : (const struct GNUNET_HELLO_Message *) &im[1];
342 if (NULL != ic->callback)
343 ic->callback (ic->callback_cls,
351 * Send the next IC request at the head of the queue.
356 send_ic_request (struct GNUNET_PEERINFO_Handle *h)
358 struct GNUNET_PEERINFO_IteratorContext *ic = h->ic_head;
359 struct GNUNET_MQ_Envelope *env;
360 struct ListAllPeersMessage *lapm;
361 struct ListPeerMessage *lpm;
373 if (GNUNET_NO == ic->have_peer)
375 LOG (GNUNET_ERROR_TYPE_DEBUG,
376 "Requesting list of peers from PEERINFO service\n");
377 env = GNUNET_MQ_msg (lapm,
378 GNUNET_MESSAGE_TYPE_PEERINFO_GET_ALL);
379 lapm->include_friend_only = htonl (ic->include_friend_only);
383 LOG (GNUNET_ERROR_TYPE_DEBUG,
384 "Requesting information on peer `%s' from PEERINFO service\n",
385 GNUNET_i2s (&ic->peer));
386 env = GNUNET_MQ_msg (lpm,
387 GNUNET_MESSAGE_TYPE_PEERINFO_GET);
388 lpm->include_friend_only = htonl (ic->include_friend_only);
389 lpm->peer = ic->peer;
391 GNUNET_MQ_send (h->mq,
397 * Type of a function to call when we receive a message from the
398 * service. Call the iterator with the result and (if applicable)
399 * continue to receive more messages or trigger processing the next
400 * event (if applicable).
403 * @param msg message received, NULL on timeout or fatal error
406 handle_end_iteration (void *cls,
407 const struct GNUNET_MessageHeader *msg)
409 struct GNUNET_PEERINFO_Handle *h = cls;
410 struct GNUNET_PEERINFO_IteratorContext *ic = h->ic_head;
414 /* didn't expect a response, reconnect */
419 LOG (GNUNET_ERROR_TYPE_DEBUG,
420 "Received end of list of peers from PEERINFO service\n");
421 GNUNET_CONTAINER_DLL_remove (h->ic_head,
424 if (NULL != h->ic_head)
426 if (NULL != ic->callback)
427 ic->callback (ic->callback_cls,
436 * Close the existing connection to PEERINFO and reconnect.
438 * @param h handle to the service
441 reconnect (struct GNUNET_PEERINFO_Handle *h)
443 struct GNUNET_MQ_MessageHandler handlers[] = {
444 GNUNET_MQ_hd_var_size (info,
445 GNUNET_MESSAGE_TYPE_PEERINFO_INFO,
448 GNUNET_MQ_hd_fixed_size (end_iteration,
449 GNUNET_MESSAGE_TYPE_PEERINFO_INFO_END,
450 struct GNUNET_MessageHeader,
452 GNUNET_MQ_handler_end ()
455 if (NULL != h->r_task)
457 GNUNET_SCHEDULER_cancel (h->r_task);
462 GNUNET_MQ_destroy (h->mq);
465 h->mq = GNUNET_CLIENT_connect (h->cfg,
470 if (NULL != h->ic_head)
476 * Call a method for each known matching host. The callback method
477 * will be invoked once for each matching host and then finally once
478 * with a NULL pointer. After that final invocation, the iterator
479 * context must no longer be used.
481 * Instead of calling this function with `peer == NULL` it is often
482 * better to use #GNUNET_PEERINFO_notify().
484 * @param h handle to the peerinfo service
485 * @param include_friend_only include HELLO messages for friends only
486 * @param peer restrict iteration to this peer only (can be NULL)
487 * @param callback the method to call for each peer
488 * @param callback_cls closure for @a callback
489 * @return iterator context
491 struct GNUNET_PEERINFO_IteratorContext *
492 GNUNET_PEERINFO_iterate (struct GNUNET_PEERINFO_Handle *h,
493 int include_friend_only,
494 const struct GNUNET_PeerIdentity *peer,
495 GNUNET_PEERINFO_Processor callback,
498 struct GNUNET_PEERINFO_IteratorContext *ic;
500 ic = GNUNET_new (struct GNUNET_PEERINFO_IteratorContext);
502 ic->include_friend_only = include_friend_only;
503 ic->callback = callback;
504 ic->callback_cls = callback_cls;
507 ic->have_peer = GNUNET_YES;
510 GNUNET_CONTAINER_DLL_insert_tail (h->ic_head,
513 if (h->ic_head == ic)
520 * Cancel an iteration over peer information.
522 * @param ic context of the iterator to cancel
525 GNUNET_PEERINFO_iterate_cancel (struct GNUNET_PEERINFO_IteratorContext *ic)
527 struct GNUNET_PEERINFO_Handle *h = ic->h;
530 if (ic == h->ic_head)
532 GNUNET_CONTAINER_DLL_remove (h->ic_head,
540 * Add a host to the persistent list. This method operates in
541 * semi-reliable mode: if the transmission is not completed by
542 * the time #GNUNET_PEERINFO_disconnect() is called, it will be
543 * aborted. Furthermore, if a second HELLO is added for the
544 * same peer before the first one was transmitted, PEERINFO may
545 * merge the two HELLOs prior to transmission to the service.
547 * @param h handle to the peerinfo service
548 * @param hello the verified (!) HELLO message
549 * @param cont continuation to call when done, NULL is allowed
550 * @param cont_cls closure for @a cont
551 * @return handle to cancel add operation; all pending
552 * 'add' operations will be cancelled automatically
553 * on disconnect, so it is not necessary to keep this
554 * handle (unless @a cont is NULL and at some point
555 * calling @a cont must be prevented)
557 struct GNUNET_MQ_Envelope *
558 GNUNET_PEERINFO_add_peer (struct GNUNET_PEERINFO_Handle *h,
559 const struct GNUNET_HELLO_Message *hello,
560 GNUNET_MQ_NotifyCallback cont,
563 struct GNUNET_MQ_Envelope *env;
564 struct GNUNET_PeerIdentity peer;
568 GNUNET_assert (GNUNET_OK ==
569 GNUNET_HELLO_get_id (hello,
571 LOG (GNUNET_ERROR_TYPE_DEBUG,
572 "Adding peer `%s' to PEERINFO database\n",
574 env = GNUNET_MQ_msg_copy ((const struct GNUNET_MessageHeader *) hello);
576 GNUNET_MQ_notify_sent (env,
579 GNUNET_MQ_send (h->mq,
585 /* end of peerinfo_api.c */