2 This file is part of GNUnet.
3 (C) 2001-2014 Christian Grothoff (and other contributing authors)
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., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, 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_peerinfo_service.h"
32 #include "gnunet-peerinfo_plugins.h"
35 * How long until we time out during peerinfo iterations?
37 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
40 * Structure we use to collect printable address information.
45 * Record we keep for each printable address.
50 * Current address-to-string context (if active, otherwise NULL).
52 struct GNUNET_TRANSPORT_AddressToStringContext *atsc;
55 * Address expiration time
57 struct GNUNET_TIME_Absolute expiration;
65 * Print context this address record belongs to.
67 struct PrintContext *pc;
72 * 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
123 static int no_resolve;
133 static int include_friend_only;
148 static int default_operation;
158 static char *put_uri;
163 static char *dump_hello;
166 * Handle to peerinfo service.
168 static struct GNUNET_PEERINFO_Handle *peerinfo;
171 * Configuration handle.
173 static const struct GNUNET_CONFIGURATION_Handle *cfg;
176 * Main state machine task (if active).
178 static GNUNET_SCHEDULER_TaskIdentifier tt;
181 * Pending #GNUNET_TRANSPORT_get_hello() operation.
183 static struct GNUNET_TRANSPORT_GetHelloHandle *gh;
186 * Connection to transport service.
188 static struct GNUNET_TRANSPORT_Handle *transport;
191 * Current iterator context (if active, otherwise NULL).
193 static struct GNUNET_PEERINFO_IteratorContext *pic;
198 static struct GNUNET_PeerIdentity my_peer_identity;
201 * Head of list of print contexts.
203 static struct PrintContext *pc_head;
206 * Tail of list of print contexts.
208 static struct PrintContext *pc_tail;
211 * Handle to current #GNUNET_PEERINFO_add_peer() operation.
213 static struct GNUNET_PEERINFO_AddContext *ac;
216 * Hello of this peer (if initialized).
218 static struct GNUNET_HELLO_Message *my_hello;
222 * Main state machine that goes over all options and
223 * runs the next requested function.
229 state_machine (void *cls,
230 const struct GNUNET_SCHEDULER_TaskContext *tc);
233 /* ********************* 'get_info' ******************* */
236 * Print the collected address information to the console and free @a pc.
238 * @param pc printing context
241 dump_pc (struct PrintContext *pc)
245 printf (_("%sPeer `%s'\n"),
246 (GNUNET_YES == pc->friend_only) ? "F2F: " : "",
247 GNUNET_i2s_full (&pc->peer));
248 for (i = 0; i < pc->num_addresses; i++)
250 if (NULL != pc->address_list[i].result)
252 printf (_("\tExpires: %s \t %s\n"),
253 GNUNET_STRINGS_absolute_time_to_string (pc->address_list[i].expiration),
254 pc->address_list[i].result);
255 GNUNET_free (pc->address_list[i].result);
259 GNUNET_free_non_null (pc->address_list);
260 GNUNET_CONTAINER_DLL_remove (pc_head,
264 if ( (NULL == pc_head) &&
266 tt = GNUNET_SCHEDULER_add_now (&state_machine,
271 /* ************************* list all known addresses **************** */
275 * Function to call with a human-readable format of an address
278 * @param address NULL on error, otherwise 0-terminated printable UTF-8 string
279 * @param res result of the address to string conversion:
280 * if #GNUNET_OK: address was valid (conversion to
281 * string might still have failed)
282 * if #GNUNET_SYSERR: address is invalid
285 process_resolved_address (void *cls,
289 struct AddressRecord *ar = cls;
290 struct PrintContext *pc = ar->pc;
294 if (NULL == ar->result)
295 ar->result = GNUNET_strdup (address);
299 if (GNUNET_SYSERR == res)
300 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
301 _("Failure: Cannot convert address to string for peer `%s'\n"),
302 GNUNET_i2s (&ar->pc->peer));
304 if (pc->num_addresses == pc->address_list_size)
310 * Iterator callback to go over all addresses and count them.
312 * @param cls `struct PrintContext *` with `off` to increment
313 * @param address the address
314 * @param expiration expiration time
315 * @return #GNUNET_OK to keep the address and continue
318 count_address (void *cls,
319 const struct GNUNET_HELLO_Address *address,
320 struct GNUNET_TIME_Absolute expiration)
322 struct PrintContext *pc = cls;
330 * Iterator callback to go over all addresses.
333 * @param address the address
334 * @param expiration expiration time
335 * @return #GNUNET_OK to keep the address and continue
338 print_address (void *cls,
339 const struct GNUNET_HELLO_Address *address,
340 struct GNUNET_TIME_Absolute expiration)
342 struct PrintContext *pc = cls;
343 struct AddressRecord *ar;
345 GNUNET_assert (0 < pc->off);
346 ar = &pc->address_list[--pc->off];
348 ar->expiration = expiration;
349 ar->atsc = GNUNET_TRANSPORT_address_to_string (cfg,
353 &process_resolved_address, ar);
359 * Print information about the peer. Currently prints the `struct
360 * GNUNET_PeerIdentity` and the transport address.
362 * @param cls the `struct PrintContext *`
363 * @param peer identity of the peer
364 * @param hello addresses of the peer
365 * @param err_msg error message
368 print_peer_info (void *cls,
369 const struct GNUNET_PeerIdentity *peer,
370 const struct GNUNET_HELLO_Message *hello,
373 struct PrintContext *pc;
378 pic = NULL; /* end of iteration */
382 _("Error in communication with PEERINFO service: %s\n"),
386 tt = GNUNET_SCHEDULER_add_now (&state_machine, NULL);
389 friend_only = GNUNET_NO;
391 friend_only = GNUNET_HELLO_is_friend_only (hello);
392 if ( (GNUNET_YES == be_quiet) ||
396 (GNUNET_YES == friend_only) ? "F2F: " : "",
397 GNUNET_i2s_full (peer));
400 pc = GNUNET_new (struct PrintContext);
401 GNUNET_CONTAINER_DLL_insert (pc_head,
405 pc->friend_only = friend_only;
406 GNUNET_HELLO_iterate_addresses (hello,
415 pc->address_list_size = pc->off;
416 pc->address_list = GNUNET_malloc (sizeof (struct AddressRecord) * pc->off);
417 GNUNET_HELLO_iterate_addresses (hello,
423 /* ************************* DUMP Hello ************************** */
426 * Count the number of addresses in the HELLO.
428 * @param cls pointer to an `int *` used for the counter
429 * @param address an address to count
430 * @param expiration (unused)
434 count_addr (void *cls,
435 const struct GNUNET_HELLO_Address *address,
436 struct GNUNET_TIME_Absolute expiration)
446 * Write HELLO of my peer to a file.
448 * @param cls the `struct GetUriContext *`
449 * @param peer identity of the peer (unused)
450 * @param hello addresses of the peer
451 * @param err_msg error message
459 size = GNUNET_HELLO_size (my_hello);
463 _("Failure: Received invalid %s\n"),
468 GNUNET_DISK_fn_write (dump_hello,
471 GNUNET_DISK_PERM_USER_READ |
472 GNUNET_DISK_PERM_USER_WRITE |
473 GNUNET_DISK_PERM_GROUP_READ |
474 GNUNET_DISK_PERM_OTHER_READ))
477 _("Failed to write HELLO with %u bytes to file `%s'\n"),
480 if (0 != UNLINK (dump_hello))
481 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING |
482 GNUNET_ERROR_TYPE_BULK,
488 GNUNET_HELLO_iterate_addresses (my_hello,
496 _("Wrote %s HELLO containing %u addresses with %u bytes to file `%s'\n"),
497 (GNUNET_YES == GNUNET_HELLO_is_friend_only (my_hello)) ? "friend-only": "public",
502 GNUNET_free (dump_hello);
507 /* ************************* GET URI ************************** */
511 * Print URI of the peer.
513 * @param cls the `struct GetUriContext *`
514 * @param peer identity of the peer (unused)
515 * @param hello addresses of the peer
516 * @param err_msg error message
519 print_my_uri (void *cls,
520 const struct GNUNET_PeerIdentity *peer,
521 const struct GNUNET_HELLO_Message *hello,
531 _("Error in communication with PEERINFO service: %s\n"),
533 tt = GNUNET_SCHEDULER_add_now (&state_machine, NULL);
539 uri = GNUNET_HELLO_compose_uri (hello,
550 /* ************************* import HELLO by URI ********************* */
554 * Continuation called from #GNUNET_PEERINFO_add_peer()
556 * @param cls closure, NULL
557 * @param emsg error message, NULL on success
560 add_continuation (void *cls,
566 _("Failure adding HELLO: %s\n"),
568 tt = GNUNET_SCHEDULER_add_now (&state_machine, NULL);
573 * Parse the PUT URI given at the command line and add it to our peerinfo
576 * @param put_uri URI string to parse
577 * @return #GNUNET_OK on success,
578 * #GNUNET_SYSERR if the URI was invalid,
579 * #GNUNET_NO on other errors
582 parse_hello_uri (const char *put_uri)
584 struct GNUNET_HELLO_Message *hello = NULL;
586 int ret = GNUNET_HELLO_parse_uri (put_uri,
587 &my_peer_identity.public_key,
593 /* WARNING: this adds the address from URI WITHOUT verification! */
594 if (GNUNET_OK == ret)
595 ac = GNUNET_PEERINFO_add_peer (peerinfo, hello,
599 tt = GNUNET_SCHEDULER_add_now (&state_machine, NULL);
606 /* ************************ Main state machine ********************* */
610 * Main state machine that goes over all options and
611 * runs the next requested function.
614 * @param tc scheduler context
617 shutdown_task (void *cls,
618 const struct GNUNET_SCHEDULER_TaskContext *tc)
620 struct PrintContext *pc;
621 struct AddressRecord *ar;
626 GNUNET_PEERINFO_add_peer_cancel (ac);
629 if (GNUNET_SCHEDULER_NO_TASK != tt)
631 GNUNET_SCHEDULER_cancel (tt);
632 tt = GNUNET_SCHEDULER_NO_TASK;
636 GNUNET_PEERINFO_iterate_cancel (pic);
641 GNUNET_TRANSPORT_get_hello_cancel (gh);
644 if (NULL != transport)
646 GNUNET_TRANSPORT_disconnect (transport);
649 while (NULL != (pc = pc_head))
651 GNUNET_CONTAINER_DLL_remove (pc_head,
654 for (i=0;i<pc->address_list_size;i++)
656 ar = &pc->address_list[i];
657 GNUNET_free_non_null (ar->result);
658 if (NULL != ar->atsc)
660 GNUNET_TRANSPORT_address_to_string_cancel (ar->atsc);
664 GNUNET_free_non_null (pc->address_list);
667 GPI_plugins_unload ();
668 if (NULL != peerinfo)
670 GNUNET_PEERINFO_disconnect (peerinfo);
673 if (NULL != my_hello)
675 GNUNET_free (my_hello);
682 * Function called with our peer's HELLO message.
683 * Used to obtain our peer's public key.
686 * @param hello the HELLO message
689 hello_callback (void *cls,
690 const struct GNUNET_MessageHeader *hello)
695 _("Failed to get my own HELLO from this peer!\n"));
696 GNUNET_SCHEDULER_shutdown ();
699 my_hello = (struct GNUNET_HELLO_Message *) GNUNET_copy_message (hello);
700 GNUNET_assert (GNUNET_OK ==
701 GNUNET_HELLO_get_id (hello,
703 GNUNET_TRANSPORT_get_hello_cancel (gh);
705 GNUNET_TRANSPORT_disconnect (transport);
707 if (NULL != dump_hello)
709 tt = GNUNET_SCHEDULER_add_now (&state_machine, NULL);
714 * Function called with the result of the check if the PEERINFO
715 * service is running.
717 * @param cls closure with our configuration
718 * @param result #GNUNET_YES if PEERINFO is running
721 testservice_task (void *cls,
724 struct GNUNET_CRYPTO_EddsaPrivateKey *priv;
727 if (GNUNET_YES != result)
730 _("Service `%s' is not running, please start GNUnet\n"),
735 if (NULL == (peerinfo = GNUNET_PEERINFO_connect (cfg)))
739 _("Could not access PEERINFO service. Exiting.\n"));
742 if ( (GNUNET_YES == get_self) ||
743 (GNUNET_YES == get_uri) ||
744 (NULL != dump_hello) )
746 transport = GNUNET_TRANSPORT_connect (cfg,
750 gh = GNUNET_TRANSPORT_get_hello (transport,
756 tt = GNUNET_SCHEDULER_add_now (&state_machine, NULL);
758 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
765 * Main function that will be run by the scheduler.
768 * @param args remaining command-line arguments
769 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
770 * @param c configuration
776 const struct GNUNET_CONFIGURATION_Handle *c)
779 if ( (NULL != args[0]) &&
781 (args[0] == strcasestr (args[0], "gnunet://hello/")) )
783 put_uri = GNUNET_strdup (args[0]);
789 _("Invalid command line argument `%s'\n"),
794 GNUNET_CLIENT_service_test ("peerinfo",
796 GNUNET_TIME_UNIT_SECONDS,
797 &testservice_task, (void *) cfg);
802 * Main state machine that goes over all options and
803 * runs the next requested function.
806 * @param tc scheduler context
809 state_machine (void *cls,
810 const struct GNUNET_SCHEDULER_TaskContext *tc)
812 tt = GNUNET_SCHEDULER_NO_TASK;
816 GPI_plugins_load (cfg);
817 if (GNUNET_SYSERR == parse_hello_uri (put_uri))
820 _("Invalid URI `%s'\n"),
822 GNUNET_SCHEDULER_shutdown ();
824 GNUNET_free (put_uri);
827 else if (GNUNET_YES == get_info)
829 get_info = GNUNET_NO;
830 GPI_plugins_load (cfg);
831 pic = GNUNET_PEERINFO_iterate (peerinfo,
835 &print_peer_info, NULL);
837 else if (GNUNET_YES == get_self)
839 get_self = GNUNET_NO;
842 GNUNET_i2s_full (&my_peer_identity));
844 printf (_("I am peer `%s'.\n"),
845 GNUNET_i2s_full (&my_peer_identity));
846 tt = GNUNET_SCHEDULER_add_now (&state_machine, NULL);
848 else if (GNUNET_YES == get_uri)
850 GPI_plugins_load (cfg);
851 pic = GNUNET_PEERINFO_iterate (peerinfo,
855 &print_my_uri, NULL);
858 else if (GNUNET_YES == default_operation)
860 /* default operation list all */
861 default_operation = GNUNET_NO;
862 get_info = GNUNET_YES;
863 tt = GNUNET_SCHEDULER_add_now (&state_machine, NULL);
867 GNUNET_SCHEDULER_shutdown ();
869 default_operation = GNUNET_NO;
874 * The main function to obtain peer information.
876 * @param argc number of arguments from the command line
877 * @param argv command line arguments
878 * @return 0 ok, 1 on error
881 main (int argc, char *const *argv)
883 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
884 {'n', "numeric", NULL,
885 gettext_noop ("don't resolve host names"),
886 0, &GNUNET_GETOPT_set_one, &no_resolve},
888 gettext_noop ("output only the identity strings"),
889 0, &GNUNET_GETOPT_set_one, &be_quiet},
890 {'f', "friends", NULL,
891 gettext_noop ("include friend-only information"),
892 0, &GNUNET_GETOPT_set_one, &include_friend_only},
894 gettext_noop ("output our own identity only"),
895 0, &GNUNET_GETOPT_set_one, &get_self},
897 gettext_noop ("list all known peers"),
898 0, &GNUNET_GETOPT_set_one, &get_info},
899 {'d', "dump-hello", NULL,
900 gettext_noop ("dump hello to file"),
901 1, &GNUNET_GETOPT_set_string, &dump_hello},
902 {'g', "get-hello", NULL,
903 gettext_noop ("also output HELLO uri(s)"),
904 0, &GNUNET_GETOPT_set_one, &get_uri},
905 {'p', "put-hello", "HELLO",
906 gettext_noop ("add given HELLO uri to the database"),
907 1, &GNUNET_GETOPT_set_string, &put_uri},
908 GNUNET_GETOPT_OPTION_END
912 default_operation = GNUNET_YES;
913 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc,
919 GNUNET_PROGRAM_run (argc, argv, "gnunet-peerinfo",
920 gettext_noop ("Print information about peers."),
921 options, &run, NULL)) ? 0 : 1;
922 GNUNET_free ((void*) argv);
926 /* end of gnunet-peerinfo.c */