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.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
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
42 struct GNUNET_PEERINFO_IteratorContext *next;
47 struct GNUNET_PEERINFO_IteratorContext *prev;
50 * Handle to the PEERINFO service.
52 struct GNUNET_PEERINFO_Handle *h;
55 * Function to call with the results.
57 GNUNET_PEERINFO_Processor callback;
60 * Closure for @e callback.
65 * Peer we are interested in (only valid if iteration was restricted to one peer).
67 struct GNUNET_PeerIdentity peer;
75 * Only include friends in reply?
77 int include_friend_only;
82 * Handle to the peerinfo service.
84 struct GNUNET_PEERINFO_Handle
89 const struct GNUNET_CONFIGURATION_Handle *cfg;
92 * Connection to the service.
94 struct GNUNET_MQ_Handle *mq;
97 * Head of iterator DLL.
99 struct GNUNET_PEERINFO_IteratorContext *ic_head;
102 * Tail of iterator DLL.
104 struct GNUNET_PEERINFO_IteratorContext *ic_tail;
107 * ID for a reconnect task.
109 struct GNUNET_SCHEDULER_Task *r_task;
114 * Close the existing connection to PEERINFO and reconnect.
116 * @param h handle to the service
119 reconnect (struct GNUNET_PEERINFO_Handle *h);
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_PEERINFO_Handle *h;
134 h = GNUNET_new (struct GNUNET_PEERINFO_Handle);
147 * Disconnect from the peerinfo service. Note that all iterators must
148 * have completed or have been cancelled by the time this function is
149 * called (otherwise, calling this function is a serious error).
150 * Furthermore, if #GNUNET_PEERINFO_add_peer() operations are still
151 * pending, they will be cancelled silently on disconnect.
153 * @param h handle to disconnect
156 GNUNET_PEERINFO_disconnect (struct GNUNET_PEERINFO_Handle *h)
158 struct GNUNET_PEERINFO_IteratorContext *ic;
160 while (NULL != (ic = h->ic_head))
162 GNUNET_CONTAINER_DLL_remove (h->ic_head,
169 GNUNET_MQ_destroy (h->mq);
172 if (NULL != h->r_task)
174 GNUNET_SCHEDULER_cancel (h->r_task);
182 * Task scheduled to re-try connecting to the peerinfo service.
184 * @param cls the `struct GNUNET_PEERINFO_Handle *`
187 reconnect_task (void *cls)
189 struct GNUNET_PEERINFO_Handle *h = cls;
197 * We encountered an error, reconnect to the PEERINFO service.
199 * @param h handle to reconnect
202 do_reconnect (struct GNUNET_PEERINFO_Handle *h)
204 struct GNUNET_PEERINFO_IteratorContext *ic = h->ic_head;
206 GNUNET_MQ_destroy (h->mq);
210 GNUNET_CONTAINER_DLL_remove (h->ic_head,
213 if (NULL != ic->callback)
214 ic->callback (ic->callback_cls,
217 _ ("Failed to receive response from `PEERINFO' service."));
220 h->r_task = GNUNET_SCHEDULER_add_now (&reconnect_task,
226 * We got a disconnect after asking regex to do the announcement.
229 * @param cls the `struct GNUNET_PEERINFO_Handle` to retry
230 * @param error error code
233 mq_error_handler (void *cls,
234 enum GNUNET_MQ_Error error)
236 struct GNUNET_PEERINFO_Handle *h = cls;
243 * Function called when we receive an info message. Check it is
247 * @param im message received
248 * @return #GNUNET_OK if the message is OK
251 check_info (void *cls,
252 const struct InfoMessage *im)
254 struct GNUNET_PEERINFO_Handle *h = cls;
255 struct GNUNET_PEERINFO_IteratorContext *ic = h->ic_head;
256 uint16_t ms = ntohs (im->header.size) - sizeof(*im);
258 if (0 != ntohl (im->reserved))
261 return GNUNET_SYSERR;
265 /* didn't expect a response, bad */
267 return GNUNET_SYSERR;
269 if ((GNUNET_YES == ic->have_peer) &&
270 (0 != GNUNET_memcmp (&ic->peer,
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 != GNUNET_memcmp (&im->peer,
304 /* malformed message */
306 return GNUNET_SYSERR;
311 /* malformed message */
313 return GNUNET_SYSERR;
320 * Handle info message.
323 * @param im message received
326 handle_info (void *cls,
327 const struct InfoMessage *im)
329 struct GNUNET_PEERINFO_Handle *h = cls;
330 struct GNUNET_PEERINFO_IteratorContext *ic = h->ic_head;
331 const struct GNUNET_HELLO_Message *hello = NULL;
334 ms = ntohs (im->header.size);
335 if (ms > sizeof(struct InfoMessage))
336 hello = (const struct GNUNET_HELLO_Message *) &im[1];
337 if (NULL != ic->callback)
338 ic->callback (ic->callback_cls,
346 * Send the next IC request at the head of the queue.
351 send_ic_request (struct GNUNET_PEERINFO_Handle *h)
353 struct GNUNET_PEERINFO_IteratorContext *ic = h->ic_head;
354 struct GNUNET_MQ_Envelope *env;
355 struct ListAllPeersMessage *lapm;
356 struct ListPeerMessage *lpm;
368 if (GNUNET_NO == ic->have_peer)
370 LOG (GNUNET_ERROR_TYPE_DEBUG,
371 "Requesting list of peers from PEERINFO service\n");
372 env = GNUNET_MQ_msg (lapm,
373 GNUNET_MESSAGE_TYPE_PEERINFO_GET_ALL);
374 lapm->include_friend_only = htonl (ic->include_friend_only);
378 LOG (GNUNET_ERROR_TYPE_DEBUG,
379 "Requesting information on peer `%s' from PEERINFO service\n",
380 GNUNET_i2s (&ic->peer));
381 env = GNUNET_MQ_msg (lpm,
382 GNUNET_MESSAGE_TYPE_PEERINFO_GET);
383 lpm->include_friend_only = htonl (ic->include_friend_only);
384 lpm->peer = ic->peer;
386 GNUNET_MQ_send (h->mq,
392 * Type of a function to call when we receive a message from the
393 * service. Call the iterator with the result and (if applicable)
394 * continue to receive more messages or trigger processing the next
395 * event (if applicable).
398 * @param msg message received, NULL on timeout or fatal error
401 handle_end_iteration (void *cls,
402 const struct GNUNET_MessageHeader *msg)
404 struct GNUNET_PEERINFO_Handle *h = cls;
405 struct GNUNET_PEERINFO_IteratorContext *ic = h->ic_head;
409 /* didn't expect a response, reconnect */
414 LOG (GNUNET_ERROR_TYPE_DEBUG,
415 "Received end of list of peers from PEERINFO service\n");
416 GNUNET_CONTAINER_DLL_remove (h->ic_head,
419 if (NULL != h->ic_head)
421 if (NULL != ic->callback)
422 ic->callback (ic->callback_cls,
431 * Close the existing connection to PEERINFO and reconnect.
433 * @param h handle to the service
436 reconnect (struct GNUNET_PEERINFO_Handle *h)
438 struct GNUNET_MQ_MessageHandler handlers[] = {
439 GNUNET_MQ_hd_var_size (info,
440 GNUNET_MESSAGE_TYPE_PEERINFO_INFO,
443 GNUNET_MQ_hd_fixed_size (end_iteration,
444 GNUNET_MESSAGE_TYPE_PEERINFO_INFO_END,
445 struct GNUNET_MessageHeader,
447 GNUNET_MQ_handler_end ()
450 if (NULL != h->r_task)
452 GNUNET_SCHEDULER_cancel (h->r_task);
457 GNUNET_MQ_destroy (h->mq);
460 h->mq = GNUNET_CLIENT_connect (h->cfg,
465 if (NULL != h->ic_head)
471 * Call a method for each known matching host. The callback method
472 * will be invoked once for each matching host and then finally once
473 * with a NULL pointer. After that final invocation, the iterator
474 * context must no longer be used.
476 * Instead of calling this function with `peer == NULL` it is often
477 * better to use #GNUNET_PEERINFO_notify().
479 * @param h handle to the peerinfo service
480 * @param include_friend_only include HELLO messages for friends only
481 * @param peer restrict iteration to this peer only (can be NULL)
482 * @param callback the method to call for each peer
483 * @param callback_cls closure for @a callback
484 * @return iterator context
486 struct GNUNET_PEERINFO_IteratorContext *
487 GNUNET_PEERINFO_iterate (struct GNUNET_PEERINFO_Handle *h,
488 int include_friend_only,
489 const struct GNUNET_PeerIdentity *peer,
490 GNUNET_PEERINFO_Processor callback,
493 struct GNUNET_PEERINFO_IteratorContext *ic;
495 ic = GNUNET_new (struct GNUNET_PEERINFO_IteratorContext);
497 ic->include_friend_only = include_friend_only;
498 ic->callback = callback;
499 ic->callback_cls = callback_cls;
502 ic->have_peer = GNUNET_YES;
505 GNUNET_CONTAINER_DLL_insert_tail (h->ic_head,
508 if (h->ic_head == ic)
515 * Cancel an iteration over peer information.
517 * @param ic context of the iterator to cancel
520 GNUNET_PEERINFO_iterate_cancel (struct GNUNET_PEERINFO_IteratorContext *ic)
522 struct GNUNET_PEERINFO_Handle *h = ic->h;
525 if (ic == h->ic_head)
527 GNUNET_CONTAINER_DLL_remove (h->ic_head,
535 * Add a host to the persistent list. This method operates in
536 * semi-reliable mode: if the transmission is not completed by
537 * the time #GNUNET_PEERINFO_disconnect() is called, it will be
538 * aborted. Furthermore, if a second HELLO is added for the
539 * same peer before the first one was transmitted, PEERINFO may
540 * merge the two HELLOs prior to transmission to the service.
542 * @param h handle to the peerinfo service
543 * @param hello the verified (!) HELLO message
544 * @param cont continuation to call when done, NULL is allowed
545 * @param cont_cls closure for @a cont
546 * @return handle to cancel add operation; all pending
547 * 'add' operations will be cancelled automatically
548 * on disconnect, so it is not necessary to keep this
549 * handle (unless @a cont is NULL and at some point
550 * calling @a cont must be prevented)
552 struct GNUNET_MQ_Envelope *
553 GNUNET_PEERINFO_add_peer (struct GNUNET_PEERINFO_Handle *h,
554 const struct GNUNET_HELLO_Message *hello,
555 GNUNET_SCHEDULER_TaskCallback cont,
558 struct GNUNET_MQ_Envelope *env;
559 struct GNUNET_PeerIdentity peer;
563 GNUNET_assert (GNUNET_OK ==
564 GNUNET_HELLO_get_id (hello,
566 LOG (GNUNET_ERROR_TYPE_DEBUG,
567 "Adding peer `%s' to PEERINFO database\n",
569 env = GNUNET_MQ_msg_copy ((const struct GNUNET_MessageHeader *) hello);
571 GNUNET_MQ_notify_sent (env,
574 GNUNET_MQ_send (h->mq,
580 /* end of peerinfo_api.c */