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.
17 * @file peerinfo-tool/gnunet-peerinfo.c
18 * @brief Print information about other known peers.
19 * @author Christian Grothoff
20 * @author Matthias Wachs
23 #include "gnunet_util_lib.h"
24 #include "gnunet_hello_lib.h"
25 #include "gnunet_transport_service.h"
26 #include "gnunet_transport_hello_service.h"
27 #include "gnunet_peerinfo_service.h"
28 #include "gnunet-peerinfo_plugins.h"
31 * How long until we time out during address lookup?
33 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
36 * Structure we use to collect printable address information.
41 * Record we keep for each printable address.
46 * Current address-to-string context (if active, otherwise NULL).
48 struct GNUNET_TRANSPORT_AddressToStringContext *atsc;
51 * Address expiration time
53 struct GNUNET_TIME_Absolute expiration;
61 * Print context this address record belongs to.
63 struct PrintContext *pc;
68 * Structure we use to collect printable address information.
76 struct PrintContext *next;
81 struct PrintContext *prev;
84 * Identity of the peer.
86 struct GNUNET_PeerIdentity peer;
89 * List of printable addresses.
91 struct AddressRecord *address_list;
94 * Number of completed addresses in @e address_list.
96 unsigned int num_addresses;
99 * Number of addresses allocated in @e address_list.
101 unsigned int address_list_size;
104 * Current offset in @e address_list (counted down).
109 * Hello was friend only, #GNUNET_YES or #GNUNET_NO
119 static int no_resolve;
129 static int include_friend_only;
144 static int default_operation;
154 static char *put_uri;
159 static char *dump_hello;
162 * Handle to peerinfo service.
164 static struct GNUNET_PEERINFO_Handle *peerinfo;
167 * Configuration handle.
169 static const struct GNUNET_CONFIGURATION_Handle *cfg;
172 * Main state machine task (if active).
174 static struct GNUNET_SCHEDULER_Task *tt;
177 * Pending #GNUNET_TRANSPORT_hello_get() operation.
179 static struct GNUNET_TRANSPORT_HelloGetHandle *gh;
182 * Current iterator context (if active, otherwise NULL).
184 static struct GNUNET_PEERINFO_IteratorContext *pic;
189 static struct GNUNET_PeerIdentity my_peer_identity;
192 * Head of list of print contexts.
194 static struct PrintContext *pc_head;
197 * Tail of list of print contexts.
199 static struct PrintContext *pc_tail;
202 * Handle to current #GNUNET_PEERINFO_add_peer() operation.
204 static struct GNUNET_MQ_Envelope *ac;
207 * Hello of this peer (if initialized).
209 static struct GNUNET_HELLO_Message *my_hello;
213 * Main state machine that goes over all options and
214 * runs the next requested function.
219 state_machine (void *cls);
222 /* ********************* 'get_info' ******************* */
225 * Print the collected address information to the console and free @a pc.
227 * @param pc printing context
230 dump_pc (struct PrintContext *pc)
234 printf (_("%sPeer `%s'\n"),
235 (GNUNET_YES == pc->friend_only) ? "F2F: " : "",
236 GNUNET_i2s_full (&pc->peer));
237 for (i = 0; i < pc->num_addresses; i++)
239 if (NULL != pc->address_list[i].result)
241 printf (_("\tExpires: %s \t %s\n"),
242 GNUNET_STRINGS_absolute_time_to_string (pc->address_list[i].expiration),
243 pc->address_list[i].result);
244 GNUNET_free (pc->address_list[i].result);
248 GNUNET_free_non_null (pc->address_list);
249 GNUNET_CONTAINER_DLL_remove (pc_head,
253 if ( (NULL == pc_head) &&
255 tt = GNUNET_SCHEDULER_add_now (&state_machine,
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,
278 struct AddressRecord *ar = cls;
279 struct PrintContext *pc = ar->pc;
283 if (0 != strlen (address))
285 if (NULL != ar->result)
286 GNUNET_free (ar->result);
287 ar->result = GNUNET_strdup (address);
292 if (GNUNET_SYSERR == res)
293 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
294 _("Failure: Cannot convert address to string for peer `%s'\n"),
295 GNUNET_i2s (&ar->pc->peer));
297 if (pc->num_addresses == pc->address_list_size)
303 * Iterator callback to go over all addresses and count them.
305 * @param cls `struct PrintContext *` with `off` to increment
306 * @param address the address
307 * @param expiration expiration time
308 * @return #GNUNET_OK to keep the address and continue
311 count_address (void *cls,
312 const struct GNUNET_HELLO_Address *address,
313 struct GNUNET_TIME_Absolute expiration)
315 struct PrintContext *pc = cls;
323 * Iterator callback to go over all addresses.
326 * @param address the address
327 * @param expiration expiration time
328 * @return #GNUNET_OK to keep the address and continue
331 print_address (void *cls,
332 const struct GNUNET_HELLO_Address *address,
333 struct GNUNET_TIME_Absolute expiration)
335 struct PrintContext *pc = cls;
336 struct AddressRecord *ar;
338 GNUNET_assert (0 < pc->off);
339 ar = &pc->address_list[--pc->off];
341 ar->expiration = expiration;
342 GNUNET_asprintf (&ar->result,
344 address->transport_name,
345 address->address_length,
346 address->local_info);
347 ar->atsc = GNUNET_TRANSPORT_address_to_string (cfg,
351 &process_resolved_address, ar);
357 * Print information about the peer. Currently prints the `struct
358 * GNUNET_PeerIdentity` and the transport address.
360 * @param cls the `struct PrintContext *`
361 * @param peer identity of the peer
362 * @param hello addresses of the peer
363 * @param err_msg error message
366 print_peer_info (void *cls,
367 const struct GNUNET_PeerIdentity *peer,
368 const struct GNUNET_HELLO_Message *hello,
371 struct PrintContext *pc;
376 pic = NULL; /* end of iteration */
380 _("Error in communication with PEERINFO service: %s\n"),
384 tt = GNUNET_SCHEDULER_add_now (&state_machine, NULL);
387 friend_only = GNUNET_NO;
389 friend_only = GNUNET_HELLO_is_friend_only (hello);
390 if ( (GNUNET_YES == be_quiet) ||
394 (GNUNET_YES == friend_only) ? "F2F: " : "",
395 GNUNET_i2s_full (peer));
398 pc = GNUNET_new (struct PrintContext);
399 GNUNET_CONTAINER_DLL_insert (pc_head,
403 pc->friend_only = friend_only;
404 GNUNET_HELLO_iterate_addresses (hello,
413 pc->address_list_size = pc->off;
414 pc->address_list = GNUNET_malloc (sizeof (struct AddressRecord) * pc->off);
415 GNUNET_HELLO_iterate_addresses (hello,
421 /* ************************* DUMP Hello ************************** */
424 * Count the number of addresses in the HELLO.
426 * @param cls pointer to an `int *` used for the counter
427 * @param address an address to count
428 * @param expiration (unused)
432 count_addr (void *cls,
433 const struct GNUNET_HELLO_Address *address,
434 struct GNUNET_TIME_Absolute expiration)
444 * Write HELLO of my peer to a file.
446 * @param cls the `struct GetUriContext *`
447 * @param peer identity of the peer (unused)
448 * @param hello addresses of the peer
449 * @param err_msg error message
457 size = GNUNET_HELLO_size (my_hello);
461 _("Failure: Received invalid %s\n"),
466 GNUNET_DISK_fn_write (dump_hello,
469 GNUNET_DISK_PERM_USER_READ |
470 GNUNET_DISK_PERM_USER_WRITE |
471 GNUNET_DISK_PERM_GROUP_READ |
472 GNUNET_DISK_PERM_OTHER_READ))
475 _("Failed to write HELLO with %u bytes to file `%s'\n"),
478 if (0 != UNLINK (dump_hello))
479 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING |
480 GNUNET_ERROR_TYPE_BULK,
486 GNUNET_HELLO_iterate_addresses (my_hello,
494 _("Wrote %s HELLO containing %u addresses with %u bytes to file `%s'\n"),
495 (GNUNET_YES == GNUNET_HELLO_is_friend_only (my_hello)) ? "friend-only": "public",
500 GNUNET_free (dump_hello);
505 /* ************************* GET URI ************************** */
509 * Print URI of the peer.
511 * @param cls the `struct GetUriContext *`
512 * @param peer identity of the peer (unused)
513 * @param hello addresses of the peer
514 * @param err_msg error message
517 print_my_uri (void *cls,
518 const struct GNUNET_PeerIdentity *peer,
519 const struct GNUNET_HELLO_Message *hello,
529 _("Error in communication with PEERINFO service: %s\n"),
531 tt = GNUNET_SCHEDULER_add_now (&state_machine, NULL);
537 uri = GNUNET_HELLO_compose_uri (hello,
548 /* ************************* import HELLO by URI ********************* */
552 * Continuation called from #GNUNET_PEERINFO_add_peer()
554 * @param cls closure, NULL
557 add_continuation (void *cls)
560 tt = GNUNET_SCHEDULER_add_now (&state_machine, NULL);
565 * Parse the PUT URI given at the command line and add it to our peerinfo
568 * @param put_uri URI string to parse
569 * @return #GNUNET_OK on success,
570 * #GNUNET_SYSERR if the URI was invalid,
571 * #GNUNET_NO on other errors
574 parse_hello_uri (const char *put_uri)
576 struct GNUNET_HELLO_Message *hello = NULL;
578 int ret = GNUNET_HELLO_parse_uri (put_uri,
579 &my_peer_identity.public_key,
585 /* WARNING: this adds the address from URI WITHOUT verification! */
586 if (GNUNET_OK == ret)
587 ac = GNUNET_PEERINFO_add_peer (peerinfo,
592 tt = GNUNET_SCHEDULER_add_now (&state_machine, NULL);
599 /* ************************ Main state machine ********************* */
603 * Main state machine that goes over all options and
604 * runs the next requested function.
609 shutdown_task (void *cls)
611 struct PrintContext *pc;
612 struct AddressRecord *ar;
617 GNUNET_MQ_send_cancel (ac);
622 GNUNET_SCHEDULER_cancel (tt);
627 GNUNET_PEERINFO_iterate_cancel (pic);
632 GNUNET_TRANSPORT_hello_get_cancel (gh);
635 while (NULL != (pc = pc_head))
637 GNUNET_CONTAINER_DLL_remove (pc_head,
640 for (i=0;i<pc->address_list_size;i++)
642 ar = &pc->address_list[i];
643 GNUNET_free_non_null (ar->result);
644 if (NULL != ar->atsc)
646 GNUNET_TRANSPORT_address_to_string_cancel (ar->atsc);
650 GNUNET_free_non_null (pc->address_list);
653 GPI_plugins_unload ();
654 if (NULL != peerinfo)
656 GNUNET_PEERINFO_disconnect (peerinfo);
659 if (NULL != my_hello)
661 GNUNET_free (my_hello);
668 * Function called with our peer's HELLO message.
669 * Used to obtain our peer's public key.
672 * @param hello the HELLO message
675 hello_callback (void *cls,
676 const struct GNUNET_MessageHeader *hello)
681 "Failed to get my own HELLO from this peer!\n");
682 GNUNET_SCHEDULER_shutdown ();
685 my_hello = (struct GNUNET_HELLO_Message *) GNUNET_copy_message (hello);
686 GNUNET_assert (GNUNET_OK ==
687 GNUNET_HELLO_get_id (my_hello,
689 GNUNET_TRANSPORT_hello_get_cancel (gh);
691 if (NULL != dump_hello)
693 tt = GNUNET_SCHEDULER_add_now (&state_machine, NULL);
698 * Main function that will be run by the scheduler.
701 * @param args remaining command-line arguments
702 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
703 * @param c configuration
709 const struct GNUNET_CONFIGURATION_Handle *c)
712 if ( (NULL != args[0]) &&
714 (args[0] == strcasestr (args[0],
715 "gnunet://hello/")) )
717 put_uri = GNUNET_strdup (args[0]);
723 _("Invalid command line argument `%s'\n"),
727 if (NULL == (peerinfo = GNUNET_PEERINFO_connect (cfg)))
731 "Could not access PEERINFO service. Exiting.\n");
734 if ( (GNUNET_YES == get_self) ||
735 (GNUNET_YES == get_uri) ||
736 (NULL != dump_hello) )
738 gh = GNUNET_TRANSPORT_hello_get (cfg,
739 GNUNET_TRANSPORT_AC_ANY,
745 tt = GNUNET_SCHEDULER_add_now (&state_machine,
748 GNUNET_SCHEDULER_add_shutdown (&shutdown_task,
754 * Main state machine that goes over all options and
755 * runs the next requested function.
760 state_machine (void *cls)
766 GPI_plugins_load (cfg);
767 if (GNUNET_SYSERR == parse_hello_uri (put_uri))
770 _("Invalid URI `%s'\n"),
772 GNUNET_SCHEDULER_shutdown ();
774 GNUNET_free (put_uri);
777 else if (GNUNET_YES == get_info)
779 get_info = GNUNET_NO;
780 GPI_plugins_load (cfg);
781 pic = GNUNET_PEERINFO_iterate (peerinfo,
787 else if (GNUNET_YES == get_self)
789 get_self = GNUNET_NO;
792 GNUNET_i2s_full (&my_peer_identity));
794 printf (_("I am peer `%s'.\n"),
795 GNUNET_i2s_full (&my_peer_identity));
796 tt = GNUNET_SCHEDULER_add_now (&state_machine,
799 else if (GNUNET_YES == get_uri)
801 GPI_plugins_load (cfg);
802 pic = GNUNET_PEERINFO_iterate (peerinfo,
809 else if (GNUNET_YES == default_operation)
811 /* default operation list all */
812 default_operation = GNUNET_NO;
813 get_info = GNUNET_YES;
814 tt = GNUNET_SCHEDULER_add_now (&state_machine,
819 GNUNET_SCHEDULER_shutdown ();
821 default_operation = GNUNET_NO;
826 * The main function to obtain peer information.
828 * @param argc number of arguments from the command line
829 * @param argv command line arguments
830 * @return 0 ok, 1 on error
833 main (int argc, char *const *argv)
835 struct GNUNET_GETOPT_CommandLineOption options[] = {
836 GNUNET_GETOPT_option_flag ('n',
838 gettext_noop ("don't resolve host names"),
841 GNUNET_GETOPT_option_flag ('q',
843 gettext_noop ("output only the identity strings"),
845 GNUNET_GETOPT_option_flag ('f',
847 gettext_noop ("include friend-only information"),
848 &include_friend_only),
850 GNUNET_GETOPT_option_flag ('s',
852 gettext_noop ("output our own identity only"),
855 GNUNET_GETOPT_option_flag ('i',
857 gettext_noop ("list all known peers"),
860 GNUNET_GETOPT_option_string ('d',
863 gettext_noop ("dump hello to file"),
866 GNUNET_GETOPT_option_flag ('g',
868 gettext_noop ("also output HELLO uri(s)"),
871 GNUNET_GETOPT_option_string ('p',
874 gettext_noop ("add given HELLO uri to the database"),
877 GNUNET_GETOPT_OPTION_END
881 default_operation = GNUNET_YES;
882 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc,
889 GNUNET_PROGRAM_run (argc,
892 gettext_noop ("Print information about peers."),
896 GNUNET_free ((void*) argv);
900 /* end of gnunet-peerinfo.c */