2 This file is part of GNUnet.
3 Copyright (C) 2001-2014 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_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 struct GNUNET_SCHEDULER_Task * 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.
228 state_machine (void *cls);
231 /* ********************* 'get_info' ******************* */
234 * Print the collected address information to the console and free @a pc.
236 * @param pc printing context
239 dump_pc (struct PrintContext *pc)
243 printf (_("%sPeer `%s'\n"),
244 (GNUNET_YES == pc->friend_only) ? "F2F: " : "",
245 GNUNET_i2s_full (&pc->peer));
246 for (i = 0; i < pc->num_addresses; i++)
248 if (NULL != pc->address_list[i].result)
250 printf (_("\tExpires: %s \t %s\n"),
251 GNUNET_STRINGS_absolute_time_to_string (pc->address_list[i].expiration),
252 pc->address_list[i].result);
253 GNUNET_free (pc->address_list[i].result);
257 GNUNET_free_non_null (pc->address_list);
258 GNUNET_CONTAINER_DLL_remove (pc_head,
262 if ( (NULL == pc_head) &&
264 tt = GNUNET_SCHEDULER_add_now (&state_machine,
269 /* ************************* list all known addresses **************** */
273 * Function to call with a human-readable format of an address
276 * @param address NULL on error, otherwise 0-terminated printable UTF-8 string
277 * @param res result of the address to string conversion:
278 * if #GNUNET_OK: address was valid (conversion to
279 * string might still have failed)
280 * if #GNUNET_SYSERR: address is invalid
283 process_resolved_address (void *cls,
287 struct AddressRecord *ar = cls;
288 struct PrintContext *pc = ar->pc;
292 if (0 != strlen (address))
294 if (NULL != ar->result)
295 GNUNET_free (ar->result);
296 ar->result = GNUNET_strdup (address);
301 if (GNUNET_SYSERR == res)
302 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
303 _("Failure: Cannot convert address to string for peer `%s'\n"),
304 GNUNET_i2s (&ar->pc->peer));
306 if (pc->num_addresses == pc->address_list_size)
312 * Iterator callback to go over all addresses and count them.
314 * @param cls `struct PrintContext *` with `off` to increment
315 * @param address the address
316 * @param expiration expiration time
317 * @return #GNUNET_OK to keep the address and continue
320 count_address (void *cls,
321 const struct GNUNET_HELLO_Address *address,
322 struct GNUNET_TIME_Absolute expiration)
324 struct PrintContext *pc = cls;
332 * Iterator callback to go over all addresses.
335 * @param address the address
336 * @param expiration expiration time
337 * @return #GNUNET_OK to keep the address and continue
340 print_address (void *cls,
341 const struct GNUNET_HELLO_Address *address,
342 struct GNUNET_TIME_Absolute expiration)
344 struct PrintContext *pc = cls;
345 struct AddressRecord *ar;
347 GNUNET_assert (0 < pc->off);
348 ar = &pc->address_list[--pc->off];
350 ar->expiration = expiration;
351 GNUNET_asprintf (&ar->result,
353 address->transport_name,
354 address->address_length,
355 address->local_info);
356 ar->atsc = GNUNET_TRANSPORT_address_to_string (cfg,
360 &process_resolved_address, ar);
366 * Print information about the peer. Currently prints the `struct
367 * GNUNET_PeerIdentity` and the transport address.
369 * @param cls the `struct PrintContext *`
370 * @param peer identity of the peer
371 * @param hello addresses of the peer
372 * @param err_msg error message
375 print_peer_info (void *cls,
376 const struct GNUNET_PeerIdentity *peer,
377 const struct GNUNET_HELLO_Message *hello,
380 struct PrintContext *pc;
385 pic = NULL; /* end of iteration */
389 _("Error in communication with PEERINFO service: %s\n"),
393 tt = GNUNET_SCHEDULER_add_now (&state_machine, NULL);
396 friend_only = GNUNET_NO;
398 friend_only = GNUNET_HELLO_is_friend_only (hello);
399 if ( (GNUNET_YES == be_quiet) ||
403 (GNUNET_YES == friend_only) ? "F2F: " : "",
404 GNUNET_i2s_full (peer));
407 pc = GNUNET_new (struct PrintContext);
408 GNUNET_CONTAINER_DLL_insert (pc_head,
412 pc->friend_only = friend_only;
413 GNUNET_HELLO_iterate_addresses (hello,
422 pc->address_list_size = pc->off;
423 pc->address_list = GNUNET_malloc (sizeof (struct AddressRecord) * pc->off);
424 GNUNET_HELLO_iterate_addresses (hello,
430 /* ************************* DUMP Hello ************************** */
433 * Count the number of addresses in the HELLO.
435 * @param cls pointer to an `int *` used for the counter
436 * @param address an address to count
437 * @param expiration (unused)
441 count_addr (void *cls,
442 const struct GNUNET_HELLO_Address *address,
443 struct GNUNET_TIME_Absolute expiration)
453 * Write HELLO of my peer to a file.
455 * @param cls the `struct GetUriContext *`
456 * @param peer identity of the peer (unused)
457 * @param hello addresses of the peer
458 * @param err_msg error message
466 size = GNUNET_HELLO_size (my_hello);
470 _("Failure: Received invalid %s\n"),
475 GNUNET_DISK_fn_write (dump_hello,
478 GNUNET_DISK_PERM_USER_READ |
479 GNUNET_DISK_PERM_USER_WRITE |
480 GNUNET_DISK_PERM_GROUP_READ |
481 GNUNET_DISK_PERM_OTHER_READ))
484 _("Failed to write HELLO with %u bytes to file `%s'\n"),
487 if (0 != UNLINK (dump_hello))
488 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING |
489 GNUNET_ERROR_TYPE_BULK,
495 GNUNET_HELLO_iterate_addresses (my_hello,
503 _("Wrote %s HELLO containing %u addresses with %u bytes to file `%s'\n"),
504 (GNUNET_YES == GNUNET_HELLO_is_friend_only (my_hello)) ? "friend-only": "public",
509 GNUNET_free (dump_hello);
514 /* ************************* GET URI ************************** */
518 * Print URI of the peer.
520 * @param cls the `struct GetUriContext *`
521 * @param peer identity of the peer (unused)
522 * @param hello addresses of the peer
523 * @param err_msg error message
526 print_my_uri (void *cls,
527 const struct GNUNET_PeerIdentity *peer,
528 const struct GNUNET_HELLO_Message *hello,
538 _("Error in communication with PEERINFO service: %s\n"),
540 tt = GNUNET_SCHEDULER_add_now (&state_machine, NULL);
546 uri = GNUNET_HELLO_compose_uri (hello,
557 /* ************************* import HELLO by URI ********************* */
561 * Continuation called from #GNUNET_PEERINFO_add_peer()
563 * @param cls closure, NULL
564 * @param emsg error message, NULL on success
567 add_continuation (void *cls,
573 _("Failure adding HELLO: %s\n"),
575 tt = GNUNET_SCHEDULER_add_now (&state_machine, NULL);
580 * Parse the PUT URI given at the command line and add it to our peerinfo
583 * @param put_uri URI string to parse
584 * @return #GNUNET_OK on success,
585 * #GNUNET_SYSERR if the URI was invalid,
586 * #GNUNET_NO on other errors
589 parse_hello_uri (const char *put_uri)
591 struct GNUNET_HELLO_Message *hello = NULL;
593 int ret = GNUNET_HELLO_parse_uri (put_uri,
594 &my_peer_identity.public_key,
600 /* WARNING: this adds the address from URI WITHOUT verification! */
601 if (GNUNET_OK == ret)
602 ac = GNUNET_PEERINFO_add_peer (peerinfo, hello,
606 tt = GNUNET_SCHEDULER_add_now (&state_machine, NULL);
613 /* ************************ Main state machine ********************* */
617 * Main state machine that goes over all options and
618 * runs the next requested function.
623 shutdown_task (void *cls)
625 struct PrintContext *pc;
626 struct AddressRecord *ar;
631 GNUNET_PEERINFO_add_peer_cancel (ac);
636 GNUNET_SCHEDULER_cancel (tt);
641 GNUNET_PEERINFO_iterate_cancel (pic);
646 GNUNET_TRANSPORT_get_hello_cancel (gh);
649 if (NULL != transport)
651 GNUNET_TRANSPORT_disconnect (transport);
654 while (NULL != (pc = pc_head))
656 GNUNET_CONTAINER_DLL_remove (pc_head,
659 for (i=0;i<pc->address_list_size;i++)
661 ar = &pc->address_list[i];
662 GNUNET_free_non_null (ar->result);
663 if (NULL != ar->atsc)
665 GNUNET_TRANSPORT_address_to_string_cancel (ar->atsc);
669 GNUNET_free_non_null (pc->address_list);
672 GPI_plugins_unload ();
673 if (NULL != peerinfo)
675 GNUNET_PEERINFO_disconnect (peerinfo);
678 if (NULL != my_hello)
680 GNUNET_free (my_hello);
687 * Function called with our peer's HELLO message.
688 * Used to obtain our peer's public key.
691 * @param hello the HELLO message
694 hello_callback (void *cls,
695 const struct GNUNET_MessageHeader *hello)
700 _("Failed to get my own HELLO from this peer!\n"));
701 GNUNET_SCHEDULER_shutdown ();
704 my_hello = (struct GNUNET_HELLO_Message *) GNUNET_copy_message (hello);
705 GNUNET_assert (GNUNET_OK ==
706 GNUNET_HELLO_get_id (my_hello,
708 GNUNET_TRANSPORT_get_hello_cancel (gh);
710 GNUNET_TRANSPORT_disconnect (transport);
712 if (NULL != dump_hello)
714 tt = GNUNET_SCHEDULER_add_now (&state_machine, NULL);
719 * Function called with the result of the check if the PEERINFO
720 * service is running.
722 * @param cls closure with our configuration
723 * @param result #GNUNET_YES if PEERINFO is running
726 testservice_task (void *cls,
729 if (GNUNET_YES != result)
732 _("Service `%s' is not running, please start GNUnet\n"),
737 if (NULL == (peerinfo = GNUNET_PEERINFO_connect (cfg)))
741 _("Could not access PEERINFO service. Exiting.\n"));
744 if ( (GNUNET_YES == get_self) ||
745 (GNUNET_YES == get_uri) ||
746 (NULL != dump_hello) )
748 transport = GNUNET_TRANSPORT_connect (cfg,
752 gh = GNUNET_TRANSPORT_get_hello (transport,
758 tt = GNUNET_SCHEDULER_add_now (&state_machine, NULL);
760 GNUNET_SCHEDULER_add_shutdown (&shutdown_task,
766 * Main function that will be run by the scheduler.
769 * @param args remaining command-line arguments
770 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
771 * @param c configuration
777 const struct GNUNET_CONFIGURATION_Handle *c)
780 if ( (NULL != args[0]) &&
782 (args[0] == strcasestr (args[0], "gnunet://hello/")) )
784 put_uri = GNUNET_strdup (args[0]);
790 _("Invalid command line argument `%s'\n"),
795 GNUNET_CLIENT_service_test ("peerinfo",
797 GNUNET_TIME_UNIT_SECONDS,
798 &testservice_task, (void *) cfg);
803 * Main state machine that goes over all options and
804 * runs the next requested function.
809 state_machine (void *cls)
815 GPI_plugins_load (cfg);
816 if (GNUNET_SYSERR == parse_hello_uri (put_uri))
819 _("Invalid URI `%s'\n"),
821 GNUNET_SCHEDULER_shutdown ();
823 GNUNET_free (put_uri);
826 else if (GNUNET_YES == get_info)
828 get_info = GNUNET_NO;
829 GPI_plugins_load (cfg);
830 pic = GNUNET_PEERINFO_iterate (peerinfo,
834 &print_peer_info, NULL);
836 else if (GNUNET_YES == get_self)
838 get_self = GNUNET_NO;
841 GNUNET_i2s_full (&my_peer_identity));
843 printf (_("I am peer `%s'.\n"),
844 GNUNET_i2s_full (&my_peer_identity));
845 tt = GNUNET_SCHEDULER_add_now (&state_machine, NULL);
847 else if (GNUNET_YES == get_uri)
849 GPI_plugins_load (cfg);
850 pic = GNUNET_PEERINFO_iterate (peerinfo,
854 &print_my_uri, NULL);
857 else if (GNUNET_YES == default_operation)
859 /* default operation list all */
860 default_operation = GNUNET_NO;
861 get_info = GNUNET_YES;
862 tt = GNUNET_SCHEDULER_add_now (&state_machine, NULL);
866 GNUNET_SCHEDULER_shutdown ();
868 default_operation = GNUNET_NO;
873 * The main function to obtain peer information.
875 * @param argc number of arguments from the command line
876 * @param argv command line arguments
877 * @return 0 ok, 1 on error
880 main (int argc, char *const *argv)
882 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
883 {'n', "numeric", NULL,
884 gettext_noop ("don't resolve host names"),
885 0, &GNUNET_GETOPT_set_one, &no_resolve},
887 gettext_noop ("output only the identity strings"),
888 0, &GNUNET_GETOPT_set_one, &be_quiet},
889 {'f', "friends", NULL,
890 gettext_noop ("include friend-only information"),
891 0, &GNUNET_GETOPT_set_one, &include_friend_only},
893 gettext_noop ("output our own identity only"),
894 0, &GNUNET_GETOPT_set_one, &get_self},
896 gettext_noop ("list all known peers"),
897 0, &GNUNET_GETOPT_set_one, &get_info},
898 {'d', "dump-hello", NULL,
899 gettext_noop ("dump hello to file"),
900 1, &GNUNET_GETOPT_set_string, &dump_hello},
901 {'g', "get-hello", NULL,
902 gettext_noop ("also output HELLO uri(s)"),
903 0, &GNUNET_GETOPT_set_one, &get_uri},
904 {'p', "put-hello", "HELLO",
905 gettext_noop ("add given HELLO uri to the database"),
906 1, &GNUNET_GETOPT_set_string, &put_uri},
907 GNUNET_GETOPT_OPTION_END
911 default_operation = GNUNET_YES;
912 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc,
918 GNUNET_PROGRAM_run (argc, argv, "gnunet-peerinfo",
919 gettext_noop ("Print information about peers."),
920 options, &run, NULL)) ? 0 : 1;
921 GNUNET_free ((void*) argv);
925 /* end of gnunet-peerinfo.c */