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
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-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.
81 struct PrintContext *next;
86 struct PrintContext *prev;
89 * Identity of the peer.
91 struct GNUNET_PeerIdentity peer;
94 * List of printable addresses.
96 struct AddressRecord *address_list;
99 * Number of completed addresses in @e address_list.
101 unsigned int num_addresses;
104 * Number of addresses allocated in @e address_list.
106 unsigned int address_list_size;
109 * Current offset in @e address_list (counted down).
114 * Hello was friend only, #GNUNET_YES or #GNUNET_NO
124 static int no_resolve;
134 static int include_friend_only;
149 static int default_operation;
159 static char *put_uri;
164 static char *dump_hello;
167 * Handle to peerinfo service.
169 static struct GNUNET_PEERINFO_Handle *peerinfo;
172 * Configuration handle.
174 static const struct GNUNET_CONFIGURATION_Handle *cfg;
177 * Main state machine task (if active).
179 static struct GNUNET_SCHEDULER_Task *tt;
182 * Pending #GNUNET_TRANSPORT_hello_get() operation.
184 static struct GNUNET_TRANSPORT_HelloGetHandle *gh;
187 * Current iterator context (if active, otherwise NULL).
189 static struct GNUNET_PEERINFO_IteratorContext *pic;
194 static struct GNUNET_PeerIdentity my_peer_identity;
197 * Head of list of print contexts.
199 static struct PrintContext *pc_head;
202 * Tail of list of print contexts.
204 static struct PrintContext *pc_tail;
207 * Handle to current #GNUNET_PEERINFO_add_peer() operation.
209 static struct GNUNET_MQ_Envelope *ac;
212 * Hello of this peer (if initialized).
214 static struct GNUNET_HELLO_Message *my_hello;
218 * Main state machine that goes over all options and
219 * runs the next requested function.
224 state_machine (void *cls);
227 /* ********************* 'get_info' ******************* */
230 * Print the collected address information to the console and free @a pc.
232 * @param pc printing context
235 dump_pc (struct PrintContext *pc)
239 printf (_("%sPeer `%s'\n"),
240 (GNUNET_YES == pc->friend_only) ? "F2F: " : "",
241 GNUNET_i2s_full (&pc->peer));
242 for (i = 0; i < pc->num_addresses; i++)
244 if (NULL != pc->address_list[i].result)
246 printf (_("\tExpires: %s \t %s\n"),
247 GNUNET_STRINGS_absolute_time_to_string (pc->address_list[i].expiration),
248 pc->address_list[i].result);
249 GNUNET_free (pc->address_list[i].result);
253 GNUNET_free_non_null (pc->address_list);
254 GNUNET_CONTAINER_DLL_remove (pc_head,
258 if ( (NULL == pc_head) &&
260 tt = GNUNET_SCHEDULER_add_now (&state_machine,
265 /* ************************* list all known addresses **************** */
269 * Function to call with a human-readable format of an address
272 * @param address NULL on error, otherwise 0-terminated printable UTF-8 string
273 * @param res result of the address to string conversion:
274 * if #GNUNET_OK: address was valid (conversion to
275 * string might still have failed)
276 * if #GNUNET_SYSERR: address is invalid
279 process_resolved_address (void *cls,
283 struct AddressRecord *ar = cls;
284 struct PrintContext *pc = ar->pc;
288 if (0 != strlen (address))
290 if (NULL != ar->result)
291 GNUNET_free (ar->result);
292 ar->result = GNUNET_strdup (address);
297 if (GNUNET_SYSERR == res)
298 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
299 _("Failure: Cannot convert address to string for peer `%s'\n"),
300 GNUNET_i2s (&ar->pc->peer));
302 if (pc->num_addresses == pc->address_list_size)
308 * Iterator callback to go over all addresses and count them.
310 * @param cls `struct PrintContext *` with `off` to increment
311 * @param address the address
312 * @param expiration expiration time
313 * @return #GNUNET_OK to keep the address and continue
316 count_address (void *cls,
317 const struct GNUNET_HELLO_Address *address,
318 struct GNUNET_TIME_Absolute expiration)
320 struct PrintContext *pc = cls;
328 * Iterator callback to go over all addresses.
331 * @param address the address
332 * @param expiration expiration time
333 * @return #GNUNET_OK to keep the address and continue
336 print_address (void *cls,
337 const struct GNUNET_HELLO_Address *address,
338 struct GNUNET_TIME_Absolute expiration)
340 struct PrintContext *pc = cls;
341 struct AddressRecord *ar;
343 GNUNET_assert (0 < pc->off);
344 ar = &pc->address_list[--pc->off];
346 ar->expiration = expiration;
347 GNUNET_asprintf (&ar->result,
349 address->transport_name,
350 address->address_length,
351 address->local_info);
352 ar->atsc = GNUNET_TRANSPORT_address_to_string (cfg,
356 &process_resolved_address, ar);
362 * Print information about the peer. Currently prints the `struct
363 * GNUNET_PeerIdentity` and the transport address.
365 * @param cls the `struct PrintContext *`
366 * @param peer identity of the peer
367 * @param hello addresses of the peer
368 * @param err_msg error message
371 print_peer_info (void *cls,
372 const struct GNUNET_PeerIdentity *peer,
373 const struct GNUNET_HELLO_Message *hello,
376 struct PrintContext *pc;
381 pic = NULL; /* end of iteration */
385 _("Error in communication with PEERINFO service: %s\n"),
389 tt = GNUNET_SCHEDULER_add_now (&state_machine, NULL);
392 friend_only = GNUNET_NO;
394 friend_only = GNUNET_HELLO_is_friend_only (hello);
395 if ( (GNUNET_YES == be_quiet) ||
399 (GNUNET_YES == friend_only) ? "F2F: " : "",
400 GNUNET_i2s_full (peer));
403 pc = GNUNET_new (struct PrintContext);
404 GNUNET_CONTAINER_DLL_insert (pc_head,
408 pc->friend_only = friend_only;
409 GNUNET_HELLO_iterate_addresses (hello,
418 pc->address_list_size = pc->off;
419 pc->address_list = GNUNET_malloc (sizeof (struct AddressRecord) * pc->off);
420 GNUNET_HELLO_iterate_addresses (hello,
426 /* ************************* DUMP Hello ************************** */
429 * Count the number of addresses in the HELLO.
431 * @param cls pointer to an `int *` used for the counter
432 * @param address an address to count
433 * @param expiration (unused)
437 count_addr (void *cls,
438 const struct GNUNET_HELLO_Address *address,
439 struct GNUNET_TIME_Absolute expiration)
449 * Write HELLO of my peer to a file.
451 * @param cls the `struct GetUriContext *`
452 * @param peer identity of the peer (unused)
453 * @param hello addresses of the peer
454 * @param err_msg error message
462 size = GNUNET_HELLO_size (my_hello);
466 _("Failure: Received invalid %s\n"),
471 GNUNET_DISK_fn_write (dump_hello,
474 GNUNET_DISK_PERM_USER_READ |
475 GNUNET_DISK_PERM_USER_WRITE |
476 GNUNET_DISK_PERM_GROUP_READ |
477 GNUNET_DISK_PERM_OTHER_READ))
480 _("Failed to write HELLO with %u bytes to file `%s'\n"),
483 if (0 != UNLINK (dump_hello))
484 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING |
485 GNUNET_ERROR_TYPE_BULK,
491 GNUNET_HELLO_iterate_addresses (my_hello,
499 _("Wrote %s HELLO containing %u addresses with %u bytes to file `%s'\n"),
500 (GNUNET_YES == GNUNET_HELLO_is_friend_only (my_hello)) ? "friend-only": "public",
505 GNUNET_free (dump_hello);
510 /* ************************* GET URI ************************** */
514 * Print URI of the peer.
516 * @param cls the `struct GetUriContext *`
517 * @param peer identity of the peer (unused)
518 * @param hello addresses of the peer
519 * @param err_msg error message
522 print_my_uri (void *cls,
523 const struct GNUNET_PeerIdentity *peer,
524 const struct GNUNET_HELLO_Message *hello,
534 _("Error in communication with PEERINFO service: %s\n"),
536 tt = GNUNET_SCHEDULER_add_now (&state_machine, NULL);
542 uri = GNUNET_HELLO_compose_uri (hello,
553 /* ************************* import HELLO by URI ********************* */
557 * Continuation called from #GNUNET_PEERINFO_add_peer()
559 * @param cls closure, NULL
562 add_continuation (void *cls)
565 tt = GNUNET_SCHEDULER_add_now (&state_machine, NULL);
570 * Parse the PUT URI given at the command line and add it to our peerinfo
573 * @param put_uri URI string to parse
574 * @return #GNUNET_OK on success,
575 * #GNUNET_SYSERR if the URI was invalid,
576 * #GNUNET_NO on other errors
579 parse_hello_uri (const char *put_uri)
581 struct GNUNET_HELLO_Message *hello = NULL;
583 int ret = GNUNET_HELLO_parse_uri (put_uri,
584 &my_peer_identity.public_key,
590 /* WARNING: this adds the address from URI WITHOUT verification! */
591 if (GNUNET_OK == ret)
592 ac = GNUNET_PEERINFO_add_peer (peerinfo,
597 tt = GNUNET_SCHEDULER_add_now (&state_machine, NULL);
604 /* ************************ Main state machine ********************* */
608 * Main state machine that goes over all options and
609 * runs the next requested function.
614 shutdown_task (void *cls)
616 struct PrintContext *pc;
617 struct AddressRecord *ar;
622 GNUNET_MQ_send_cancel (ac);
627 GNUNET_SCHEDULER_cancel (tt);
632 GNUNET_PEERINFO_iterate_cancel (pic);
637 GNUNET_TRANSPORT_hello_get_cancel (gh);
640 while (NULL != (pc = pc_head))
642 GNUNET_CONTAINER_DLL_remove (pc_head,
645 for (i=0;i<pc->address_list_size;i++)
647 ar = &pc->address_list[i];
648 GNUNET_free_non_null (ar->result);
649 if (NULL != ar->atsc)
651 GNUNET_TRANSPORT_address_to_string_cancel (ar->atsc);
655 GNUNET_free_non_null (pc->address_list);
658 GPI_plugins_unload ();
659 if (NULL != peerinfo)
661 GNUNET_PEERINFO_disconnect (peerinfo);
664 if (NULL != my_hello)
666 GNUNET_free (my_hello);
673 * Function called with our peer's HELLO message.
674 * Used to obtain our peer's public key.
677 * @param hello the HELLO message
680 hello_callback (void *cls,
681 const struct GNUNET_MessageHeader *hello)
686 "Failed to get my own HELLO from this peer!\n");
687 GNUNET_SCHEDULER_shutdown ();
690 my_hello = (struct GNUNET_HELLO_Message *) GNUNET_copy_message (hello);
691 GNUNET_assert (GNUNET_OK ==
692 GNUNET_HELLO_get_id (my_hello,
694 GNUNET_TRANSPORT_hello_get_cancel (gh);
696 if (NULL != dump_hello)
698 tt = GNUNET_SCHEDULER_add_now (&state_machine, NULL);
703 * Main function that will be run by the scheduler.
706 * @param args remaining command-line arguments
707 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
708 * @param c configuration
714 const struct GNUNET_CONFIGURATION_Handle *c)
717 if ( (NULL != args[0]) &&
719 (args[0] == strcasestr (args[0],
720 "gnunet://hello/")) )
722 put_uri = GNUNET_strdup (args[0]);
728 _("Invalid command line argument `%s'\n"),
732 if (NULL == (peerinfo = GNUNET_PEERINFO_connect (cfg)))
736 "Could not access PEERINFO service. Exiting.\n");
739 if ( (GNUNET_YES == get_self) ||
740 (GNUNET_YES == get_uri) ||
741 (NULL != dump_hello) )
743 gh = GNUNET_TRANSPORT_hello_get (cfg,
744 GNUNET_TRANSPORT_AC_ANY,
750 tt = GNUNET_SCHEDULER_add_now (&state_machine,
753 GNUNET_SCHEDULER_add_shutdown (&shutdown_task,
759 * Main state machine that goes over all options and
760 * runs the next requested function.
765 state_machine (void *cls)
771 GPI_plugins_load (cfg);
772 if (GNUNET_SYSERR == parse_hello_uri (put_uri))
775 _("Invalid URI `%s'\n"),
777 GNUNET_SCHEDULER_shutdown ();
779 GNUNET_free (put_uri);
782 else if (GNUNET_YES == get_info)
784 get_info = GNUNET_NO;
785 GPI_plugins_load (cfg);
786 pic = GNUNET_PEERINFO_iterate (peerinfo,
792 else if (GNUNET_YES == get_self)
794 get_self = GNUNET_NO;
797 GNUNET_i2s_full (&my_peer_identity));
799 printf (_("I am peer `%s'.\n"),
800 GNUNET_i2s_full (&my_peer_identity));
801 tt = GNUNET_SCHEDULER_add_now (&state_machine,
804 else if (GNUNET_YES == get_uri)
806 GPI_plugins_load (cfg);
807 pic = GNUNET_PEERINFO_iterate (peerinfo,
814 else if (GNUNET_YES == default_operation)
816 /* default operation list all */
817 default_operation = GNUNET_NO;
818 get_info = GNUNET_YES;
819 tt = GNUNET_SCHEDULER_add_now (&state_machine,
824 GNUNET_SCHEDULER_shutdown ();
826 default_operation = GNUNET_NO;
831 * The main function to obtain peer information.
833 * @param argc number of arguments from the command line
834 * @param argv command line arguments
835 * @return 0 ok, 1 on error
838 main (int argc, char *const *argv)
840 struct GNUNET_GETOPT_CommandLineOption options[] = {
841 GNUNET_GETOPT_option_flag ('n',
843 gettext_noop ("don't resolve host names"),
846 GNUNET_GETOPT_option_flag ('q',
848 gettext_noop ("output only the identity strings"),
850 GNUNET_GETOPT_option_flag ('f',
852 gettext_noop ("include friend-only information"),
853 &include_friend_only),
855 GNUNET_GETOPT_option_flag ('s',
857 gettext_noop ("output our own identity only"),
860 GNUNET_GETOPT_option_flag ('i',
862 gettext_noop ("list all known peers"),
865 GNUNET_GETOPT_option_string ('d',
868 gettext_noop ("dump hello to file"),
871 GNUNET_GETOPT_option_flag ('g',
873 gettext_noop ("also output HELLO uri(s)"),
876 GNUNET_GETOPT_option_string ('p',
879 gettext_noop ("add given HELLO uri to the database"),
882 GNUNET_GETOPT_OPTION_END
886 default_operation = GNUNET_YES;
887 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc,
894 GNUNET_PROGRAM_run (argc,
897 gettext_noop ("Print information about peers."),
901 GNUNET_free ((void*) argv);
905 /* end of gnunet-peerinfo.c */