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 it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your 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 Affero General Public License for more details.
17 * @file peerinfo/peerinfo_api.c
18 * @brief API to access peerinfo service
19 * @author Christian Grothoff
22 #include "gnunet_util_lib.h"
23 #include "gnunet_protocols.h"
26 #define LOG(kind,...) GNUNET_log_from (kind, "peerinfo-api",__VA_ARGS__)
30 * Context for an iteration request.
32 struct GNUNET_PEERINFO_IteratorContext
38 struct GNUNET_PEERINFO_IteratorContext *next;
43 struct GNUNET_PEERINFO_IteratorContext *prev;
46 * Handle to the PEERINFO service.
48 struct GNUNET_PEERINFO_Handle *h;
51 * Function to call with the results.
53 GNUNET_PEERINFO_Processor callback;
56 * Closure for @e callback.
61 * Peer we are interested in (only valid if iteration was restricted to one peer).
63 struct GNUNET_PeerIdentity peer;
71 * Only include friends in reply?
73 int include_friend_only;
79 * Handle to the peerinfo service.
81 struct GNUNET_PEERINFO_Handle
86 const struct GNUNET_CONFIGURATION_Handle *cfg;
89 * Connection to the service.
91 struct GNUNET_MQ_Handle *mq;
94 * Head of iterator DLL.
96 struct GNUNET_PEERINFO_IteratorContext *ic_head;
99 * Tail of iterator DLL.
101 struct GNUNET_PEERINFO_IteratorContext *ic_tail;
104 * ID for a reconnect task.
106 struct GNUNET_SCHEDULER_Task *r_task;
112 * Close the existing connection to PEERINFO and reconnect.
114 * @param h handle to the service
117 reconnect (struct GNUNET_PEERINFO_Handle *h);
121 * Connect to the peerinfo service.
123 * @param cfg configuration to use
124 * @return NULL on error (configuration related, actual connection
125 * establishment may happen asynchronously).
127 struct GNUNET_PEERINFO_Handle *
128 GNUNET_PEERINFO_connect (const struct GNUNET_CONFIGURATION_Handle *cfg)
130 struct GNUNET_PEERINFO_Handle *h;
132 h = GNUNET_new (struct GNUNET_PEERINFO_Handle);
145 * Disconnect from the peerinfo service. Note that all iterators must
146 * have completed or have been cancelled by the time this function is
147 * called (otherwise, calling this function is a serious error).
148 * Furthermore, if #GNUNET_PEERINFO_add_peer() operations are still
149 * pending, they will be cancelled silently on disconnect.
151 * @param h handle to disconnect
154 GNUNET_PEERINFO_disconnect (struct GNUNET_PEERINFO_Handle *h)
156 struct GNUNET_PEERINFO_IteratorContext *ic;
158 while (NULL != (ic = h->ic_head))
160 GNUNET_CONTAINER_DLL_remove (h->ic_head,
167 GNUNET_MQ_destroy (h->mq);
170 if (NULL != h->r_task)
172 GNUNET_SCHEDULER_cancel (h->r_task);
180 * Task scheduled to re-try connecting to the peerinfo service.
182 * @param cls the `struct GNUNET_PEERINFO_Handle *`
185 reconnect_task (void *cls)
187 struct GNUNET_PEERINFO_Handle *h = cls;
195 * We encountered an error, reconnect to the PEERINFO service.
197 * @param h handle to reconnect
200 do_reconnect (struct GNUNET_PEERINFO_Handle *h)
202 struct GNUNET_PEERINFO_IteratorContext *ic = h->ic_head;
204 GNUNET_MQ_destroy (h->mq);
208 GNUNET_CONTAINER_DLL_remove (h->ic_head,
211 if (NULL != ic->callback)
212 ic->callback (ic->callback_cls,
215 _("Failed to receive response from `PEERINFO' service."));
218 h->r_task = GNUNET_SCHEDULER_add_now (&reconnect_task,
224 * We got a disconnect after asking regex to do the announcement.
227 * @param cls the `struct GNUNET_PEERINFO_Handle` to retry
228 * @param error error code
231 mq_error_handler (void *cls,
232 enum GNUNET_MQ_Error error)
234 struct GNUNET_PEERINFO_Handle *h = cls;
242 * Function called when we receive an info message. Check it is
246 * @param im message received
247 * @return #GNUNET_OK if the message is OK
250 check_info (void *cls,
251 const struct InfoMessage *im)
253 struct GNUNET_PEERINFO_Handle *h = cls;
254 struct GNUNET_PEERINFO_IteratorContext *ic = h->ic_head;
255 uint16_t ms = ntohs (im->header.size) - sizeof (*im);
257 if (0 != ntohl (im->reserved))
260 return GNUNET_SYSERR;
264 /* didn't expect a response, bad */
266 return GNUNET_SYSERR;
268 if ( (GNUNET_YES == ic->have_peer) &&
269 (0 != memcmp (&ic->peer,
271 sizeof (struct GNUNET_PeerIdentity))) )
273 /* bogus message (from a different iteration call?); out of sequence! */
274 LOG (GNUNET_ERROR_TYPE_ERROR,
275 "Received HELLO for peer `%s', expected peer `%s'\n",
276 GNUNET_i2s (&im->peer),
277 GNUNET_i2s (&ic->peer));
279 return GNUNET_SYSERR;
281 if (ms > sizeof (struct GNUNET_MessageHeader))
283 const struct GNUNET_HELLO_Message *hello;
284 struct GNUNET_PeerIdentity id;
286 hello = (const struct GNUNET_HELLO_Message *) &im[1];
287 if (ms != GNUNET_HELLO_size (hello))
289 /* malformed message */
291 return GNUNET_SYSERR;
294 GNUNET_HELLO_get_id (hello,
297 /* malformed message */
299 return GNUNET_SYSERR;
301 if (0 != memcmp (&im->peer,
303 sizeof (struct GNUNET_PeerIdentity)))
305 /* malformed message */
307 return GNUNET_SYSERR;
312 /* malformed message */
314 return GNUNET_SYSERR;
321 * Handle info message.
324 * @param im message received
327 handle_info (void *cls,
328 const struct InfoMessage *im)
330 struct GNUNET_PEERINFO_Handle *h = cls;
331 struct GNUNET_PEERINFO_IteratorContext *ic = h->ic_head;
332 const struct GNUNET_HELLO_Message *hello = NULL;
335 ms = ntohs (im->header.size);
336 if (ms > sizeof (struct InfoMessage))
337 hello = (const struct GNUNET_HELLO_Message *) &im[1];
338 if (NULL != ic->callback)
339 ic->callback (ic->callback_cls,
347 * Send the next IC request at the head of the queue.
352 send_ic_request (struct GNUNET_PEERINFO_Handle *h)
354 struct GNUNET_PEERINFO_IteratorContext *ic = h->ic_head;
355 struct GNUNET_MQ_Envelope *env;
356 struct ListAllPeersMessage *lapm;
357 struct ListPeerMessage *lpm;
369 if (GNUNET_NO == ic->have_peer)
371 LOG (GNUNET_ERROR_TYPE_DEBUG,
372 "Requesting list of peers from PEERINFO service\n");
373 env = GNUNET_MQ_msg (lapm,
374 GNUNET_MESSAGE_TYPE_PEERINFO_GET_ALL);
375 lapm->include_friend_only = htonl (ic->include_friend_only);
379 LOG (GNUNET_ERROR_TYPE_DEBUG,
380 "Requesting information on peer `%s' from PEERINFO service\n",
381 GNUNET_i2s (&ic->peer));
382 env = GNUNET_MQ_msg (lpm,
383 GNUNET_MESSAGE_TYPE_PEERINFO_GET);
384 lpm->include_friend_only = htonl (ic->include_friend_only);
385 lpm->peer = ic->peer;
387 GNUNET_MQ_send (h->mq,
393 * Type of a function to call when we receive a message from the
394 * service. Call the iterator with the result and (if applicable)
395 * continue to receive more messages or trigger processing the next
396 * event (if applicable).
399 * @param msg message received, NULL on timeout or fatal error
402 handle_end_iteration (void *cls,
403 const struct GNUNET_MessageHeader *msg)
405 struct GNUNET_PEERINFO_Handle *h = cls;
406 struct GNUNET_PEERINFO_IteratorContext *ic = h->ic_head;
410 /* didn't expect a response, reconnect */
415 LOG (GNUNET_ERROR_TYPE_DEBUG,
416 "Received end of list of peers from PEERINFO service\n");
417 GNUNET_CONTAINER_DLL_remove (h->ic_head,
420 if (NULL != h->ic_head)
422 if (NULL != ic->callback)
423 ic->callback (ic->callback_cls,
432 * Close the existing connection to PEERINFO and reconnect.
434 * @param h handle to the service
437 reconnect (struct GNUNET_PEERINFO_Handle *h)
439 struct GNUNET_MQ_MessageHandler handlers[] = {
440 GNUNET_MQ_hd_var_size (info,
441 GNUNET_MESSAGE_TYPE_PEERINFO_INFO,
444 GNUNET_MQ_hd_fixed_size (end_iteration,
445 GNUNET_MESSAGE_TYPE_PEERINFO_INFO_END,
446 struct GNUNET_MessageHeader,
448 GNUNET_MQ_handler_end ()
451 if (NULL != h->r_task)
453 GNUNET_SCHEDULER_cancel (h->r_task);
458 GNUNET_MQ_destroy (h->mq);
461 h->mq = GNUNET_CLIENT_connect (h->cfg,
466 if (NULL != h->ic_head)
472 * Call a method for each known matching host. The callback method
473 * will be invoked once for each matching host and then finally once
474 * with a NULL pointer. After that final invocation, the iterator
475 * context must no longer be used.
477 * Instead of calling this function with `peer == NULL` it is often
478 * better to use #GNUNET_PEERINFO_notify().
480 * @param h handle to the peerinfo service
481 * @param include_friend_only include HELLO messages for friends only
482 * @param peer restrict iteration to this peer only (can be NULL)
483 * @param callback the method to call for each peer
484 * @param callback_cls closure for @a callback
485 * @return iterator context
487 struct GNUNET_PEERINFO_IteratorContext *
488 GNUNET_PEERINFO_iterate (struct GNUNET_PEERINFO_Handle *h,
489 int include_friend_only,
490 const struct GNUNET_PeerIdentity *peer,
491 GNUNET_PEERINFO_Processor callback,
494 struct GNUNET_PEERINFO_IteratorContext *ic;
496 ic = GNUNET_new (struct GNUNET_PEERINFO_IteratorContext);
498 ic->include_friend_only = include_friend_only;
499 ic->callback = callback;
500 ic->callback_cls = callback_cls;
503 ic->have_peer = GNUNET_YES;
506 GNUNET_CONTAINER_DLL_insert_tail (h->ic_head,
509 if (h->ic_head == ic)
516 * Cancel an iteration over peer information.
518 * @param ic context of the iterator to cancel
521 GNUNET_PEERINFO_iterate_cancel (struct GNUNET_PEERINFO_IteratorContext *ic)
523 struct GNUNET_PEERINFO_Handle *h = ic->h;
526 if (ic == h->ic_head)
528 GNUNET_CONTAINER_DLL_remove (h->ic_head,
536 * Add a host to the persistent list. This method operates in
537 * semi-reliable mode: if the transmission is not completed by
538 * the time #GNUNET_PEERINFO_disconnect() is called, it will be
539 * aborted. Furthermore, if a second HELLO is added for the
540 * same peer before the first one was transmitted, PEERINFO may
541 * merge the two HELLOs prior to transmission to the service.
543 * @param h handle to the peerinfo service
544 * @param hello the verified (!) HELLO message
545 * @param cont continuation to call when done, NULL is allowed
546 * @param cont_cls closure for @a cont
547 * @return handle to cancel add operation; all pending
548 * 'add' operations will be cancelled automatically
549 * on disconnect, so it is not necessary to keep this
550 * handle (unless @a cont is NULL and at some point
551 * calling @a cont must be prevented)
553 struct GNUNET_MQ_Envelope *
554 GNUNET_PEERINFO_add_peer (struct GNUNET_PEERINFO_Handle *h,
555 const struct GNUNET_HELLO_Message *hello,
556 GNUNET_SCHEDULER_TaskCallback cont,
559 struct GNUNET_MQ_Envelope *env;
560 struct GNUNET_PeerIdentity peer;
567 GNUNET_assert (GNUNET_OK ==
568 GNUNET_HELLO_get_id (hello,
570 LOG (GNUNET_ERROR_TYPE_DEBUG,
571 "Adding peer `%s' to PEERINFO database\n",
573 env = GNUNET_MQ_msg_copy ((const struct GNUNET_MessageHeader *) hello);
575 GNUNET_MQ_notify_sent (env,
578 GNUNET_MQ_send (h->mq,
584 /* end of peerinfo_api.c */