2 This file is part of GNUnet.
3 Copyright (C) 2001-2014, 2016 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-tool/gnunet-peerinfo.c
23 * @brief Print information about other known peers.
24 * @author Christian Grothoff
25 * @author Matthias Wachs
28 #include "gnunet_util_lib.h"
29 #include "gnunet_hello_lib.h"
30 #include "gnunet_transport_service.h"
31 #include "gnunet_transport_hello_service.h"
32 #include "gnunet_peerinfo_service.h"
33 #include "gnunet-peerinfo_plugins.h"
36 * How long until we time out during address lookup?
38 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
41 * Structure we use to collect printable address information.
46 * Record we keep for each printable address.
51 * Current address-to-string context (if active, otherwise NULL).
53 struct GNUNET_TRANSPORT_AddressToStringContext *atsc;
56 * Address expiration time
58 struct GNUNET_TIME_Absolute expiration;
66 * Print context this address record belongs to.
68 struct PrintContext *pc;
73 * Structure we use to collect printable address information.
80 struct PrintContext *next;
85 struct PrintContext *prev;
88 * Identity of the peer.
90 struct GNUNET_PeerIdentity peer;
93 * List of printable addresses.
95 struct AddressRecord *address_list;
98 * Number of completed addresses in @e address_list.
100 unsigned int num_addresses;
103 * Number of addresses allocated in @e address_list.
105 unsigned int address_list_size;
108 * Current offset in @e address_list (counted down).
113 * Hello was friend only, #GNUNET_YES or #GNUNET_NO
122 static int no_resolve;
132 static int include_friend_only;
147 static int default_operation;
157 static char *put_uri;
162 static char *dump_hello;
165 * Handle to peerinfo service.
167 static struct GNUNET_PEERINFO_Handle *peerinfo;
170 * Configuration handle.
172 static const struct GNUNET_CONFIGURATION_Handle *cfg;
175 * Main state machine task (if active).
177 static struct GNUNET_SCHEDULER_Task *tt;
180 * Pending #GNUNET_TRANSPORT_hello_get() operation.
182 static struct GNUNET_TRANSPORT_HelloGetHandle *gh;
185 * Current iterator context (if active, otherwise NULL).
187 static struct GNUNET_PEERINFO_IteratorContext *pic;
192 static struct GNUNET_PeerIdentity my_peer_identity;
195 * Head of list of print contexts.
197 static struct PrintContext *pc_head;
200 * Tail of list of print contexts.
202 static struct PrintContext *pc_tail;
205 * Handle to current #GNUNET_PEERINFO_add_peer() operation.
207 static struct GNUNET_MQ_Envelope *ac;
210 * Hello of this peer (if initialized).
212 static struct GNUNET_HELLO_Message *my_hello;
216 * Main state machine that goes over all options and
217 * runs the next requested function.
222 state_machine (void *cls);
225 /* ********************* 'get_info' ******************* */
228 * Print the collected address information to the console and free @a pc.
230 * @param pc printing context
233 dump_pc (struct PrintContext *pc)
237 printf (_ ("%sPeer `%s'\n"),
238 (GNUNET_YES == pc->friend_only) ? "F2F: " : "",
239 GNUNET_i2s_full (&pc->peer));
240 for (i = 0; i < pc->num_addresses; i++)
242 if (NULL != pc->address_list[i].result)
244 printf (_ ("\tExpires: %s \t %s\n"),
245 GNUNET_STRINGS_absolute_time_to_string (
246 pc->address_list[i].expiration),
247 pc->address_list[i].result);
248 GNUNET_free (pc->address_list[i].result);
252 GNUNET_free_non_null (pc->address_list);
253 GNUNET_CONTAINER_DLL_remove (pc_head, pc_tail, pc);
255 if ((NULL == pc_head) && (NULL == pic))
256 tt = GNUNET_SCHEDULER_add_now (&state_machine, NULL);
260 /* ************************* list all known addresses **************** */
264 * Function to call with a human-readable format of an address
267 * @param address NULL on error, otherwise 0-terminated printable UTF-8 string
268 * @param res result of the address to string conversion:
269 * if #GNUNET_OK: address was valid (conversion to
270 * string might still have failed)
271 * if #GNUNET_SYSERR: address is invalid
274 process_resolved_address (void *cls, const char *address, int res)
276 struct AddressRecord *ar = cls;
277 struct PrintContext *pc = ar->pc;
281 if (0 != strlen (address))
283 if (NULL != ar->result)
284 GNUNET_free (ar->result);
285 ar->result = GNUNET_strdup (address);
290 if (GNUNET_SYSERR == res)
291 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
292 _ ("Failure: Cannot convert address to string for peer `%s'\n"),
293 GNUNET_i2s (&ar->pc->peer));
295 if (pc->num_addresses == pc->address_list_size)
301 * Iterator callback to go over all addresses and count them.
303 * @param cls `struct PrintContext *` with `off` to increment
304 * @param address the address
305 * @param expiration expiration time
306 * @return #GNUNET_OK to keep the address and continue
309 count_address (void *cls,
310 const struct GNUNET_HELLO_Address *address,
311 struct GNUNET_TIME_Absolute expiration)
313 struct PrintContext *pc = cls;
321 * Iterator callback to go over all addresses.
324 * @param address the address
325 * @param expiration expiration time
326 * @return #GNUNET_OK to keep the address and continue
329 print_address (void *cls,
330 const struct GNUNET_HELLO_Address *address,
331 struct GNUNET_TIME_Absolute expiration)
333 struct PrintContext *pc = cls;
334 struct AddressRecord *ar;
336 GNUNET_assert (0 < pc->off);
337 ar = &pc->address_list[--pc->off];
339 ar->expiration = expiration;
340 GNUNET_asprintf (&ar->result,
342 address->transport_name,
343 address->address_length,
344 address->local_info);
345 ar->atsc = GNUNET_TRANSPORT_address_to_string (cfg,
349 &process_resolved_address,
356 * Print information about the peer. Currently prints the `struct
357 * GNUNET_PeerIdentity` and the transport address.
359 * @param cls the `struct PrintContext *`
360 * @param peer identity of the peer
361 * @param hello addresses of the peer
362 * @param err_msg error message
365 print_peer_info (void *cls,
366 const struct GNUNET_PeerIdentity *peer,
367 const struct GNUNET_HELLO_Message *hello,
370 struct PrintContext *pc;
375 pic = NULL; /* end of iteration */
379 _ ("Error in communication with PEERINFO service: %s\n"),
383 tt = GNUNET_SCHEDULER_add_now (&state_machine, NULL);
386 friend_only = GNUNET_NO;
388 friend_only = GNUNET_HELLO_is_friend_only (hello);
389 if ((GNUNET_YES == be_quiet) || (NULL == hello))
392 (GNUNET_YES == friend_only) ? "F2F: " : "",
393 GNUNET_i2s_full (peer));
396 pc = GNUNET_new (struct PrintContext);
397 GNUNET_CONTAINER_DLL_insert (pc_head, pc_tail, pc);
399 pc->friend_only = friend_only;
400 GNUNET_HELLO_iterate_addresses (hello, GNUNET_NO, &count_address, pc);
406 pc->address_list_size = pc->off;
407 pc->address_list = GNUNET_malloc (sizeof(struct AddressRecord) * pc->off);
408 GNUNET_HELLO_iterate_addresses (hello, GNUNET_NO, &print_address, pc);
412 /* ************************* DUMP Hello ************************** */
415 * Count the number of addresses in the HELLO.
417 * @param cls pointer to an `int *` used for the counter
418 * @param address an address to count
419 * @param expiration (unused)
423 count_addr (void *cls,
424 const struct GNUNET_HELLO_Address *address,
425 struct GNUNET_TIME_Absolute expiration)
435 * Write HELLO of my peer to a file.
437 * @param cls the `struct GetUriContext *`
438 * @param peer identity of the peer (unused)
439 * @param hello addresses of the peer
440 * @param err_msg error message
448 size = GNUNET_HELLO_size (my_hello);
451 fprintf (stderr, _ ("Failure: Received invalid %s\n"), "HELLO");
454 if (GNUNET_SYSERR == GNUNET_DISK_fn_write (dump_hello,
457 GNUNET_DISK_PERM_USER_READ
458 | GNUNET_DISK_PERM_USER_WRITE
459 | GNUNET_DISK_PERM_GROUP_READ
460 | GNUNET_DISK_PERM_OTHER_READ))
463 _ ("Failed to write HELLO with %u bytes to file `%s'\n"),
466 if (0 != unlink (dump_hello))
467 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING
468 | GNUNET_ERROR_TYPE_BULK,
473 GNUNET_HELLO_iterate_addresses (my_hello, GNUNET_NO, count_addr, &c_addr);
479 _ ("Wrote %s HELLO containing %u addresses with %u bytes to file `%s'\n"),
480 (GNUNET_YES == GNUNET_HELLO_is_friend_only (my_hello)) ? "friend-only"
486 GNUNET_free (dump_hello);
491 /* ************************* GET URI ************************** */
495 * Print URI of the peer.
497 * @param cls the `struct GetUriContext *`
498 * @param peer identity of the peer (unused)
499 * @param hello addresses of the peer
500 * @param err_msg error message
503 print_my_uri (void *cls,
504 const struct GNUNET_PeerIdentity *peer,
505 const struct GNUNET_HELLO_Message *hello,
515 _ ("Error in communication with PEERINFO service: %s\n"),
517 tt = GNUNET_SCHEDULER_add_now (&state_machine, NULL);
523 uri = GNUNET_HELLO_compose_uri (hello, &GPI_plugins_find);
526 printf ("%s\n", (const char *) uri);
532 /* ************************* import HELLO by URI ********************* */
536 * Continuation called from #GNUNET_PEERINFO_add_peer()
538 * @param cls closure, NULL
541 add_continuation (void *cls)
544 tt = GNUNET_SCHEDULER_add_now (&state_machine, NULL);
549 * Parse the PUT URI given at the command line and add it to our peerinfo
552 * @param put_uri URI string to parse
553 * @return #GNUNET_OK on success,
554 * #GNUNET_SYSERR if the URI was invalid,
555 * #GNUNET_NO on other errors
558 parse_hello_uri (const char *put_uri)
560 struct GNUNET_HELLO_Message *hello = NULL;
562 int ret = GNUNET_HELLO_parse_uri (put_uri,
563 &my_peer_identity.public_key,
569 /* WARNING: this adds the address from URI WITHOUT verification! */
570 if (GNUNET_OK == ret)
571 ac = GNUNET_PEERINFO_add_peer (peerinfo, hello, &add_continuation, NULL);
573 tt = GNUNET_SCHEDULER_add_now (&state_machine, NULL);
580 /* ************************ Main state machine ********************* */
584 * Main state machine that goes over all options and
585 * runs the next requested function.
590 shutdown_task (void *cls)
592 struct PrintContext *pc;
593 struct AddressRecord *ar;
598 GNUNET_MQ_send_cancel (ac);
603 GNUNET_SCHEDULER_cancel (tt);
608 GNUNET_PEERINFO_iterate_cancel (pic);
613 GNUNET_TRANSPORT_hello_get_cancel (gh);
616 while (NULL != (pc = pc_head))
618 GNUNET_CONTAINER_DLL_remove (pc_head, pc_tail, pc);
619 for (i = 0; i < pc->address_list_size; i++)
621 ar = &pc->address_list[i];
622 GNUNET_free_non_null (ar->result);
623 if (NULL != ar->atsc)
625 GNUNET_TRANSPORT_address_to_string_cancel (ar->atsc);
629 GNUNET_free_non_null (pc->address_list);
632 GPI_plugins_unload ();
633 if (NULL != peerinfo)
635 GNUNET_PEERINFO_disconnect (peerinfo);
638 if (NULL != my_hello)
640 GNUNET_free (my_hello);
647 * Function called with our peer's HELLO message.
648 * Used to obtain our peer's public key.
651 * @param hello the HELLO message
654 hello_callback (void *cls, const struct GNUNET_MessageHeader *hello)
658 fprintf (stderr, "Failed to get my own HELLO from this peer!\n");
659 GNUNET_SCHEDULER_shutdown ();
662 my_hello = (struct GNUNET_HELLO_Message *) GNUNET_copy_message (hello);
663 GNUNET_assert (GNUNET_OK ==
664 GNUNET_HELLO_get_id (my_hello, &my_peer_identity));
665 GNUNET_TRANSPORT_hello_get_cancel (gh);
667 if (NULL != dump_hello)
669 tt = GNUNET_SCHEDULER_add_now (&state_machine, NULL);
674 * Main function that will be run by the scheduler.
677 * @param args remaining command-line arguments
678 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
679 * @param c configuration
685 const struct GNUNET_CONFIGURATION_Handle *c)
688 if ((NULL != args[0]) && (NULL == put_uri) &&
689 (args[0] == strcasestr (args[0], "gnunet://hello/")))
691 put_uri = GNUNET_strdup (args[0]);
696 fprintf (stderr, _ ("Invalid command line argument `%s'\n"), args[0]);
699 if (NULL == (peerinfo = GNUNET_PEERINFO_connect (cfg)))
701 fprintf (stderr, "%s", "Could not access PEERINFO service. Exiting.\n");
704 if ((GNUNET_YES == get_self) || (GNUNET_YES == get_uri) ||
705 (NULL != dump_hello))
707 gh = GNUNET_TRANSPORT_hello_get (cfg,
708 GNUNET_TRANSPORT_AC_ANY,
714 tt = GNUNET_SCHEDULER_add_now (&state_machine, NULL);
716 GNUNET_SCHEDULER_add_shutdown (&shutdown_task, NULL);
721 * Main state machine that goes over all options and
722 * runs the next requested function.
727 state_machine (void *cls)
733 GPI_plugins_load (cfg);
734 if (GNUNET_SYSERR == parse_hello_uri (put_uri))
736 fprintf (stderr, _ ("Invalid URI `%s'\n"), put_uri);
737 GNUNET_SCHEDULER_shutdown ();
739 GNUNET_free (put_uri);
742 else if (GNUNET_YES == get_info)
744 get_info = GNUNET_NO;
745 GPI_plugins_load (cfg);
746 pic = GNUNET_PEERINFO_iterate (peerinfo,
752 else if (GNUNET_YES == get_self)
754 get_self = GNUNET_NO;
756 printf ("%s\n", GNUNET_i2s_full (&my_peer_identity));
758 printf (_ ("I am peer `%s'.\n"), GNUNET_i2s_full (&my_peer_identity));
759 tt = GNUNET_SCHEDULER_add_now (&state_machine, NULL);
761 else if (GNUNET_YES == get_uri)
763 GPI_plugins_load (cfg);
764 pic = GNUNET_PEERINFO_iterate (peerinfo,
771 else if (GNUNET_YES == default_operation)
773 /* default operation list all */
774 default_operation = GNUNET_NO;
775 get_info = GNUNET_YES;
776 tt = GNUNET_SCHEDULER_add_now (&state_machine, NULL);
780 GNUNET_SCHEDULER_shutdown ();
782 default_operation = GNUNET_NO;
787 * The main function to obtain peer information.
789 * @param argc number of arguments from the command line
790 * @param argv command line arguments
791 * @return 0 ok, 1 on error
794 main (int argc, char *const *argv)
796 struct GNUNET_GETOPT_CommandLineOption options[] =
797 { GNUNET_GETOPT_option_flag ('n',
799 gettext_noop ("don't resolve host names"),
802 GNUNET_GETOPT_option_flag ('q',
805 "output only the identity strings"),
807 GNUNET_GETOPT_option_flag ('f',
810 "include friend-only information"),
811 &include_friend_only),
813 GNUNET_GETOPT_option_flag ('s',
815 gettext_noop ("output our own identity only"),
818 GNUNET_GETOPT_option_flag ('i',
820 gettext_noop ("list all known peers"),
823 GNUNET_GETOPT_option_string ('d',
826 gettext_noop ("dump hello to file"),
829 GNUNET_GETOPT_option_flag ('g',
831 gettext_noop ("also output HELLO uri(s)"),
834 GNUNET_GETOPT_option_string ('p',
838 "add given HELLO uri to the database"),
841 GNUNET_GETOPT_OPTION_END };
844 default_operation = GNUNET_YES;
845 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
849 GNUNET_PROGRAM_run (argc,
852 gettext_noop ("Print information about peers."),
858 GNUNET_free ((void *) argv);
863 /* end of gnunet-peerinfo.c */