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/>.
20 * @file peerinfo-tool/gnunet-peerinfo.c
21 * @brief Print information about other known peers.
22 * @author Christian Grothoff
23 * @author Matthias Wachs
26 #include "gnunet_util_lib.h"
27 #include "gnunet_hello_lib.h"
28 #include "gnunet_transport_service.h"
29 #include "gnunet_transport_hello_service.h"
30 #include "gnunet_peerinfo_service.h"
31 #include "gnunet-peerinfo_plugins.h"
34 * How long until we time out during address lookup?
36 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
39 * Structure we use to collect printable address information.
44 * Record we keep for each printable address.
49 * Current address-to-string context (if active, otherwise NULL).
51 struct GNUNET_TRANSPORT_AddressToStringContext *atsc;
54 * Address expiration time
56 struct GNUNET_TIME_Absolute expiration;
64 * Print context this address record belongs to.
66 struct PrintContext *pc;
71 * Structure we use to collect printable address information.
79 struct PrintContext *next;
84 struct PrintContext *prev;
87 * Identity of the peer.
89 struct GNUNET_PeerIdentity peer;
92 * List of printable addresses.
94 struct AddressRecord *address_list;
97 * Number of completed addresses in @e address_list.
99 unsigned int num_addresses;
102 * Number of addresses allocated in @e address_list.
104 unsigned int address_list_size;
107 * Current offset in @e address_list (counted down).
112 * 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 (pc->address_list[i].expiration),
246 pc->address_list[i].result);
247 GNUNET_free (pc->address_list[i].result);
251 GNUNET_free_non_null (pc->address_list);
252 GNUNET_CONTAINER_DLL_remove (pc_head,
256 if ( (NULL == pc_head) &&
258 tt = GNUNET_SCHEDULER_add_now (&state_machine,
263 /* ************************* list all known addresses **************** */
267 * Function to call with a human-readable format of an address
270 * @param address NULL on error, otherwise 0-terminated printable UTF-8 string
271 * @param res result of the address to string conversion:
272 * if #GNUNET_OK: address was valid (conversion to
273 * string might still have failed)
274 * if #GNUNET_SYSERR: address is invalid
277 process_resolved_address (void *cls,
281 struct AddressRecord *ar = cls;
282 struct PrintContext *pc = ar->pc;
286 if (0 != strlen (address))
288 if (NULL != ar->result)
289 GNUNET_free (ar->result);
290 ar->result = GNUNET_strdup (address);
295 if (GNUNET_SYSERR == res)
296 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
297 _("Failure: Cannot convert address to string for peer `%s'\n"),
298 GNUNET_i2s (&ar->pc->peer));
300 if (pc->num_addresses == pc->address_list_size)
306 * Iterator callback to go over all addresses and count them.
308 * @param cls `struct PrintContext *` with `off` to increment
309 * @param address the address
310 * @param expiration expiration time
311 * @return #GNUNET_OK to keep the address and continue
314 count_address (void *cls,
315 const struct GNUNET_HELLO_Address *address,
316 struct GNUNET_TIME_Absolute expiration)
318 struct PrintContext *pc = cls;
326 * Iterator callback to go over all addresses.
329 * @param address the address
330 * @param expiration expiration time
331 * @return #GNUNET_OK to keep the address and continue
334 print_address (void *cls,
335 const struct GNUNET_HELLO_Address *address,
336 struct GNUNET_TIME_Absolute expiration)
338 struct PrintContext *pc = cls;
339 struct AddressRecord *ar;
341 GNUNET_assert (0 < pc->off);
342 ar = &pc->address_list[--pc->off];
344 ar->expiration = expiration;
345 GNUNET_asprintf (&ar->result,
347 address->transport_name,
348 address->address_length,
349 address->local_info);
350 ar->atsc = GNUNET_TRANSPORT_address_to_string (cfg,
354 &process_resolved_address, ar);
360 * Print information about the peer. Currently prints the `struct
361 * GNUNET_PeerIdentity` and the transport address.
363 * @param cls the `struct PrintContext *`
364 * @param peer identity of the peer
365 * @param hello addresses of the peer
366 * @param err_msg error message
369 print_peer_info (void *cls,
370 const struct GNUNET_PeerIdentity *peer,
371 const struct GNUNET_HELLO_Message *hello,
374 struct PrintContext *pc;
379 pic = NULL; /* end of iteration */
383 _("Error in communication with PEERINFO service: %s\n"),
387 tt = GNUNET_SCHEDULER_add_now (&state_machine, NULL);
390 friend_only = GNUNET_NO;
392 friend_only = GNUNET_HELLO_is_friend_only (hello);
393 if ( (GNUNET_YES == be_quiet) ||
397 (GNUNET_YES == friend_only) ? "F2F: " : "",
398 GNUNET_i2s_full (peer));
401 pc = GNUNET_new (struct PrintContext);
402 GNUNET_CONTAINER_DLL_insert (pc_head,
406 pc->friend_only = friend_only;
407 GNUNET_HELLO_iterate_addresses (hello,
416 pc->address_list_size = pc->off;
417 pc->address_list = GNUNET_malloc (sizeof (struct AddressRecord) * pc->off);
418 GNUNET_HELLO_iterate_addresses (hello,
424 /* ************************* DUMP Hello ************************** */
427 * Count the number of addresses in the HELLO.
429 * @param cls pointer to an `int *` used for the counter
430 * @param address an address to count
431 * @param expiration (unused)
435 count_addr (void *cls,
436 const struct GNUNET_HELLO_Address *address,
437 struct GNUNET_TIME_Absolute expiration)
447 * Write HELLO of my peer to a file.
449 * @param cls the `struct GetUriContext *`
450 * @param peer identity of the peer (unused)
451 * @param hello addresses of the peer
452 * @param err_msg error message
460 size = GNUNET_HELLO_size (my_hello);
464 _("Failure: Received invalid %s\n"),
469 GNUNET_DISK_fn_write (dump_hello,
472 GNUNET_DISK_PERM_USER_READ |
473 GNUNET_DISK_PERM_USER_WRITE |
474 GNUNET_DISK_PERM_GROUP_READ |
475 GNUNET_DISK_PERM_OTHER_READ))
478 _("Failed to write HELLO with %u bytes to file `%s'\n"),
481 if (0 != UNLINK (dump_hello))
482 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING |
483 GNUNET_ERROR_TYPE_BULK,
489 GNUNET_HELLO_iterate_addresses (my_hello,
497 _("Wrote %s HELLO containing %u addresses with %u bytes to file `%s'\n"),
498 (GNUNET_YES == GNUNET_HELLO_is_friend_only (my_hello)) ? "friend-only": "public",
503 GNUNET_free (dump_hello);
508 /* ************************* GET URI ************************** */
512 * Print URI of the peer.
514 * @param cls the `struct GetUriContext *`
515 * @param peer identity of the peer (unused)
516 * @param hello addresses of the peer
517 * @param err_msg error message
520 print_my_uri (void *cls,
521 const struct GNUNET_PeerIdentity *peer,
522 const struct GNUNET_HELLO_Message *hello,
532 _("Error in communication with PEERINFO service: %s\n"),
534 tt = GNUNET_SCHEDULER_add_now (&state_machine, NULL);
540 uri = GNUNET_HELLO_compose_uri (hello,
551 /* ************************* import HELLO by URI ********************* */
555 * Continuation called from #GNUNET_PEERINFO_add_peer()
557 * @param cls closure, NULL
560 add_continuation (void *cls)
563 tt = GNUNET_SCHEDULER_add_now (&state_machine, NULL);
568 * Parse the PUT URI given at the command line and add it to our peerinfo
571 * @param put_uri URI string to parse
572 * @return #GNUNET_OK on success,
573 * #GNUNET_SYSERR if the URI was invalid,
574 * #GNUNET_NO on other errors
577 parse_hello_uri (const char *put_uri)
579 struct GNUNET_HELLO_Message *hello = NULL;
581 int ret = GNUNET_HELLO_parse_uri (put_uri,
582 &my_peer_identity.public_key,
588 /* WARNING: this adds the address from URI WITHOUT verification! */
589 if (GNUNET_OK == ret)
590 ac = GNUNET_PEERINFO_add_peer (peerinfo,
595 tt = GNUNET_SCHEDULER_add_now (&state_machine, NULL);
602 /* ************************ Main state machine ********************* */
606 * Main state machine that goes over all options and
607 * runs the next requested function.
612 shutdown_task (void *cls)
614 struct PrintContext *pc;
615 struct AddressRecord *ar;
620 GNUNET_MQ_send_cancel (ac);
625 GNUNET_SCHEDULER_cancel (tt);
630 GNUNET_PEERINFO_iterate_cancel (pic);
635 GNUNET_TRANSPORT_hello_get_cancel (gh);
638 while (NULL != (pc = pc_head))
640 GNUNET_CONTAINER_DLL_remove (pc_head,
643 for (i=0;i<pc->address_list_size;i++)
645 ar = &pc->address_list[i];
646 GNUNET_free_non_null (ar->result);
647 if (NULL != ar->atsc)
649 GNUNET_TRANSPORT_address_to_string_cancel (ar->atsc);
653 GNUNET_free_non_null (pc->address_list);
656 GPI_plugins_unload ();
657 if (NULL != peerinfo)
659 GNUNET_PEERINFO_disconnect (peerinfo);
662 if (NULL != my_hello)
664 GNUNET_free (my_hello);
671 * Function called with our peer's HELLO message.
672 * Used to obtain our peer's public key.
675 * @param hello the HELLO message
678 hello_callback (void *cls,
679 const struct GNUNET_MessageHeader *hello)
684 "Failed to get my own HELLO from this peer!\n");
685 GNUNET_SCHEDULER_shutdown ();
688 my_hello = (struct GNUNET_HELLO_Message *) GNUNET_copy_message (hello);
689 GNUNET_assert (GNUNET_OK ==
690 GNUNET_HELLO_get_id (my_hello,
692 GNUNET_TRANSPORT_hello_get_cancel (gh);
694 if (NULL != dump_hello)
696 tt = GNUNET_SCHEDULER_add_now (&state_machine, NULL);
701 * Main function that will be run by the scheduler.
704 * @param args remaining command-line arguments
705 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
706 * @param c configuration
712 const struct GNUNET_CONFIGURATION_Handle *c)
715 if ( (NULL != args[0]) &&
717 (args[0] == strcasestr (args[0],
718 "gnunet://hello/")) )
720 put_uri = GNUNET_strdup (args[0]);
726 _("Invalid command line argument `%s'\n"),
730 if (NULL == (peerinfo = GNUNET_PEERINFO_connect (cfg)))
734 "Could not access PEERINFO service. Exiting.\n");
737 if ( (GNUNET_YES == get_self) ||
738 (GNUNET_YES == get_uri) ||
739 (NULL != dump_hello) )
741 gh = GNUNET_TRANSPORT_hello_get (cfg,
742 GNUNET_TRANSPORT_AC_ANY,
748 tt = GNUNET_SCHEDULER_add_now (&state_machine,
751 GNUNET_SCHEDULER_add_shutdown (&shutdown_task,
757 * Main state machine that goes over all options and
758 * runs the next requested function.
763 state_machine (void *cls)
769 GPI_plugins_load (cfg);
770 if (GNUNET_SYSERR == parse_hello_uri (put_uri))
773 _("Invalid URI `%s'\n"),
775 GNUNET_SCHEDULER_shutdown ();
777 GNUNET_free (put_uri);
780 else if (GNUNET_YES == get_info)
782 get_info = GNUNET_NO;
783 GPI_plugins_load (cfg);
784 pic = GNUNET_PEERINFO_iterate (peerinfo,
790 else if (GNUNET_YES == get_self)
792 get_self = GNUNET_NO;
795 GNUNET_i2s_full (&my_peer_identity));
797 printf (_("I am peer `%s'.\n"),
798 GNUNET_i2s_full (&my_peer_identity));
799 tt = GNUNET_SCHEDULER_add_now (&state_machine,
802 else if (GNUNET_YES == get_uri)
804 GPI_plugins_load (cfg);
805 pic = GNUNET_PEERINFO_iterate (peerinfo,
812 else if (GNUNET_YES == default_operation)
814 /* default operation list all */
815 default_operation = GNUNET_NO;
816 get_info = GNUNET_YES;
817 tt = GNUNET_SCHEDULER_add_now (&state_machine,
822 GNUNET_SCHEDULER_shutdown ();
824 default_operation = GNUNET_NO;
829 * The main function to obtain peer information.
831 * @param argc number of arguments from the command line
832 * @param argv command line arguments
833 * @return 0 ok, 1 on error
836 main (int argc, char *const *argv)
838 struct GNUNET_GETOPT_CommandLineOption options[] = {
839 GNUNET_GETOPT_option_flag ('n',
841 gettext_noop ("don't resolve host names"),
844 GNUNET_GETOPT_option_flag ('q',
846 gettext_noop ("output only the identity strings"),
848 GNUNET_GETOPT_option_flag ('f',
850 gettext_noop ("include friend-only information"),
851 &include_friend_only),
853 GNUNET_GETOPT_option_flag ('s',
855 gettext_noop ("output our own identity only"),
858 GNUNET_GETOPT_option_flag ('i',
860 gettext_noop ("list all known peers"),
863 GNUNET_GETOPT_option_string ('d',
866 gettext_noop ("dump hello to file"),
869 GNUNET_GETOPT_option_flag ('g',
871 gettext_noop ("also output HELLO uri(s)"),
874 GNUNET_GETOPT_option_string ('p',
877 gettext_noop ("add given HELLO uri to the database"),
880 GNUNET_GETOPT_OPTION_END
884 default_operation = GNUNET_YES;
885 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc,
892 GNUNET_PROGRAM_run (argc,
895 gettext_noop ("Print information about peers."),
899 GNUNET_free ((void*) argv);
903 /* end of gnunet-peerinfo.c */