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 * Current iterator context (if active, otherwise NULL).
183 static struct GNUNET_PEERINFO_IteratorContext *pic;
188 static struct GNUNET_PeerIdentity my_peer_identity;
191 * Head of list of print contexts.
193 static struct PrintContext *pc_head;
196 * Tail of list of print contexts.
198 static struct PrintContext *pc_tail;
201 * Handle to current #GNUNET_PEERINFO_add_peer() operation.
203 static struct GNUNET_PEERINFO_AddContext *ac;
207 * Main state machine that goes over all options and
208 * runs the next requested function.
214 state_machine (void *cls,
215 const struct GNUNET_SCHEDULER_TaskContext *tc);
218 /* ********************* 'get_info' ******************* */
221 * Print the collected address information to the console and free @a pc.
223 * @param pc printing context
226 dump_pc (struct PrintContext *pc)
230 printf (_("%sPeer `%s'\n"),
231 (GNUNET_YES == pc->friend_only) ? "F2F: " : "",
232 GNUNET_i2s_full (&pc->peer));
233 for (i = 0; i < pc->num_addresses; i++)
235 if (NULL != pc->address_list[i].result)
237 printf (_("\tExpires: %s \t %s\n"),
238 GNUNET_STRINGS_absolute_time_to_string (pc->address_list[i].expiration),
239 pc->address_list[i].result);
240 GNUNET_free (pc->address_list[i].result);
244 GNUNET_free_non_null (pc->address_list);
245 GNUNET_CONTAINER_DLL_remove (pc_head,
249 if ( (NULL == pc_head) &&
251 tt = GNUNET_SCHEDULER_add_now (&state_machine,
256 /* ************************* list all known addresses **************** */
260 * Function to call with a human-readable format of an address
263 * @param address NULL on error, otherwise 0-terminated printable UTF-8 string
264 * @param res result of the address to string conversion:
265 * if #GNUNET_OK: address was valid (conversion to
266 * string might still have failed)
267 * if #GNUNET_SYSERR: address is invalid
270 process_resolved_address (void *cls,
274 struct AddressRecord *ar = cls;
275 struct PrintContext *pc = ar->pc;
279 if (NULL == ar->result)
280 ar->result = GNUNET_strdup (address);
284 if (GNUNET_SYSERR == res)
285 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
286 _("Failure: Cannot convert address to string for peer `%s'\n"),
287 GNUNET_i2s (&ar->pc->peer));
289 if (pc->num_addresses == pc->address_list_size)
295 * Iterator callback to go over all addresses and count them.
297 * @param cls `struct PrintContext *` with `off` to increment
298 * @param address the address
299 * @param expiration expiration time
300 * @return #GNUNET_OK to keep the address and continue
303 count_address (void *cls,
304 const struct GNUNET_HELLO_Address *address,
305 struct GNUNET_TIME_Absolute expiration)
307 struct PrintContext *pc = cls;
315 * Iterator callback to go over all addresses.
318 * @param address the address
319 * @param expiration expiration time
320 * @return #GNUNET_OK to keep the address and continue
323 print_address (void *cls,
324 const struct GNUNET_HELLO_Address *address,
325 struct GNUNET_TIME_Absolute expiration)
327 struct PrintContext *pc = cls;
328 struct AddressRecord *ar;
330 GNUNET_assert (0 < pc->off);
331 ar = &pc->address_list[--pc->off];
333 ar->expiration = expiration;
334 ar->atsc = GNUNET_TRANSPORT_address_to_string (cfg,
338 &process_resolved_address, ar);
344 * Print information about the peer. Currently prints the `struct
345 * GNUNET_PeerIdentity` and the transport address.
347 * @param cls the `struct PrintContext *`
348 * @param peer identity of the peer
349 * @param hello addresses of the peer
350 * @param err_msg error message
353 print_peer_info (void *cls,
354 const struct GNUNET_PeerIdentity *peer,
355 const struct GNUNET_HELLO_Message *hello,
358 struct PrintContext *pc;
363 pic = NULL; /* end of iteration */
367 _("Error in communication with PEERINFO service: %s\n"),
371 tt = GNUNET_SCHEDULER_add_now (&state_machine, NULL);
374 friend_only = GNUNET_NO;
376 friend_only = GNUNET_HELLO_is_friend_only (hello);
377 if ( (GNUNET_YES == be_quiet) ||
381 (GNUNET_YES == friend_only) ? "F2F: " : "",
382 GNUNET_i2s_full (peer));
385 pc = GNUNET_new (struct PrintContext);
386 GNUNET_CONTAINER_DLL_insert (pc_head,
390 pc->friend_only = friend_only;
391 GNUNET_HELLO_iterate_addresses (hello,
400 pc->address_list_size = pc->off;
401 pc->address_list = GNUNET_malloc (sizeof (struct AddressRecord) * pc->off);
402 GNUNET_HELLO_iterate_addresses (hello,
408 /* ************************* DUMP Hello ************************** */
411 * Count the number of addresses in the HELLO.
413 * @param cls pointer to an `int *` used for the counter
414 * @param address an address to count
415 * @param expiration (unused)
419 count_addr (void *cls,
420 const struct GNUNET_HELLO_Address *address,
421 struct GNUNET_TIME_Absolute expiration)
431 * Write HELLO of my peer to a file.
433 * @param cls the `struct GetUriContext *`
434 * @param peer identity of the peer (unused)
435 * @param hello addresses of the peer
436 * @param err_msg error message
439 dump_my_hello (void *cls,
440 const struct GNUNET_PeerIdentity *peer,
441 const struct GNUNET_HELLO_Message *hello,
452 _("Error in communication with PEERINFO service: %s\n"),
454 tt = GNUNET_SCHEDULER_add_now (&state_machine,
462 _("Failure: Did not receive %s\n"),
467 size = GNUNET_HELLO_size (hello);
471 _("Failure: Received invalid %s\n"),
476 GNUNET_DISK_fn_write (dump_hello, hello, size,
477 GNUNET_DISK_PERM_USER_READ |
478 GNUNET_DISK_PERM_USER_WRITE |
479 GNUNET_DISK_PERM_GROUP_READ |
480 GNUNET_DISK_PERM_OTHER_READ))
483 _("Failed to write HELLO with %u bytes to file `%s'\n"),
486 if (0 != UNLINK (dump_hello))
487 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING |
488 GNUNET_ERROR_TYPE_BULK,
494 GNUNET_HELLO_iterate_addresses (hello,
502 _("Wrote %s HELLO containing %u addresses with %u bytes to file `%s'\n"),
503 (GNUNET_YES == GNUNET_HELLO_is_friend_only(hello)) ? "friend-only": "public",
508 GNUNET_free (dump_hello);
513 /* ************************* GET URI ************************** */
517 * Print URI of the peer.
519 * @param cls the `struct GetUriContext *`
520 * @param peer identity of the peer (unused)
521 * @param hello addresses of the peer
522 * @param err_msg error message
525 print_my_uri (void *cls,
526 const struct GNUNET_PeerIdentity *peer,
527 const struct GNUNET_HELLO_Message *hello,
535 _("Error in communication with PEERINFO service: %s\n"),
537 tt = GNUNET_SCHEDULER_add_now (&state_machine, NULL);
543 char *uri = GNUNET_HELLO_compose_uri (hello,
554 /* ************************* import HELLO by URI ********************* */
558 * Continuation called from #GNUNET_PEERINFO_add_peer()
560 * @param cls closure, NULL
561 * @param emsg error message, NULL on success
564 add_continuation (void *cls,
570 _("Failure adding HELLO: %s\n"),
572 tt = GNUNET_SCHEDULER_add_now (&state_machine, NULL);
577 * Parse the PUT URI given at the command line and add it to our peerinfo
580 * @param put_uri URI string to parse
581 * @return #GNUNET_OK on success,
582 * #GNUNET_SYSERR if the URI was invalid,
583 * #GNUNET_NO on other errors
586 parse_hello_uri (const char *put_uri)
588 struct GNUNET_HELLO_Message *hello = NULL;
590 int ret = GNUNET_HELLO_parse_uri (put_uri,
591 &my_peer_identity.public_key,
597 /* WARNING: this adds the address from URI WITHOUT verification! */
598 if (GNUNET_OK == ret)
599 ac = GNUNET_PEERINFO_add_peer (peerinfo, hello,
603 tt = GNUNET_SCHEDULER_add_now (&state_machine, NULL);
610 /* ************************ Main state machine ********************* */
614 * Main state machine that goes over all options and
615 * runs the next requested function.
618 * @param tc scheduler context
621 shutdown_task (void *cls,
622 const struct GNUNET_SCHEDULER_TaskContext *tc)
624 struct PrintContext *pc;
625 struct AddressRecord *ar;
630 GNUNET_PEERINFO_add_peer_cancel (ac);
633 if (GNUNET_SCHEDULER_NO_TASK != tt)
635 GNUNET_SCHEDULER_cancel (tt);
636 tt = GNUNET_SCHEDULER_NO_TASK;
640 GNUNET_PEERINFO_iterate_cancel (pic);
643 while (NULL != (pc = pc_head))
645 GNUNET_CONTAINER_DLL_remove (pc_head,
648 for (i=0;i<pc->address_list_size;i++)
650 ar = &pc->address_list[i];
651 GNUNET_free_non_null (ar->result);
652 if (NULL != ar->atsc)
654 GNUNET_TRANSPORT_address_to_string_cancel (ar->atsc);
658 GNUNET_free_non_null (pc->address_list);
661 GPI_plugins_unload ();
662 if (NULL != peerinfo)
664 GNUNET_PEERINFO_disconnect (peerinfo);
671 * Function called with the result of the check if the PEERINFO
672 * service is running.
674 * @param cls closure with our configuration
675 * @param result #GNUNET_YES if PEERINFO is running
678 testservice_task (void *cls,
681 struct GNUNET_CRYPTO_EddsaPrivateKey *priv;
684 if (GNUNET_YES != result)
687 _("Service `%s' is not running, please start GNUnet\n"),
692 if (NULL == (peerinfo = GNUNET_PEERINFO_connect (cfg)))
696 _("Could not access PEERINFO service. Exiting.\n"));
699 if ( (GNUNET_YES == get_self) ||
700 (GNUNET_YES == get_uri) ||
701 (NULL != dump_hello) )
703 /* load private key */
705 GNUNET_CONFIGURATION_get_value_filename (cfg,
710 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
715 if (NULL == (priv = GNUNET_CRYPTO_eddsa_key_create_from_file (fn)))
718 _("Loading hostkey from `%s' failed.\n"),
724 GNUNET_CRYPTO_eddsa_key_get_public (priv,
725 &my_peer_identity.public_key);
729 tt = GNUNET_SCHEDULER_add_now (&state_machine, NULL);
730 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
737 * Main function that will be run by the scheduler.
740 * @param args remaining command-line arguments
741 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
742 * @param c configuration
748 const struct GNUNET_CONFIGURATION_Handle *c)
751 if ( (NULL != args[0]) &&
753 (args[0] == strcasestr (args[0], "gnunet://hello/")) )
755 put_uri = GNUNET_strdup (args[0]);
761 _("Invalid command line argument `%s'\n"),
766 GNUNET_CLIENT_service_test ("peerinfo",
768 GNUNET_TIME_UNIT_SECONDS,
769 &testservice_task, (void *) cfg);
774 * Main state machine that goes over all options and
775 * runs the next requested function.
778 * @param tc scheduler context
781 state_machine (void *cls,
782 const struct GNUNET_SCHEDULER_TaskContext *tc)
784 tt = GNUNET_SCHEDULER_NO_TASK;
788 GPI_plugins_load (cfg);
789 if (GNUNET_SYSERR == parse_hello_uri (put_uri))
792 _("Invalid URI `%s'\n"),
794 GNUNET_SCHEDULER_shutdown ();
796 GNUNET_free (put_uri);
799 else if (GNUNET_YES == get_info)
801 get_info = GNUNET_NO;
802 GPI_plugins_load (cfg);
803 pic = GNUNET_PEERINFO_iterate (peerinfo,
807 &print_peer_info, NULL);
809 else if (GNUNET_YES == get_self)
811 get_self = GNUNET_NO;
814 GNUNET_i2s_full (&my_peer_identity));
816 printf (_("I am peer `%s'.\n"),
817 GNUNET_i2s_full (&my_peer_identity));
818 tt = GNUNET_SCHEDULER_add_now (&state_machine, NULL);
820 else if (GNUNET_YES == get_uri)
822 GPI_plugins_load (cfg);
823 pic = GNUNET_PEERINFO_iterate (peerinfo,
827 &print_my_uri, NULL);
830 else if (NULL != dump_hello)
832 pic = GNUNET_PEERINFO_iterate (peerinfo,
836 &dump_my_hello, NULL);
838 else if (GNUNET_YES == default_operation)
840 /* default operation list all */
841 default_operation = GNUNET_NO;
842 get_info = GNUNET_YES;
843 tt = GNUNET_SCHEDULER_add_now (&state_machine, NULL);
847 GNUNET_SCHEDULER_shutdown ();
849 default_operation = GNUNET_NO;
854 * The main function to obtain peer information.
856 * @param argc number of arguments from the command line
857 * @param argv command line arguments
858 * @return 0 ok, 1 on error
861 main (int argc, char *const *argv)
863 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
864 {'n', "numeric", NULL,
865 gettext_noop ("don't resolve host names"),
866 0, &GNUNET_GETOPT_set_one, &no_resolve},
868 gettext_noop ("output only the identity strings"),
869 0, &GNUNET_GETOPT_set_one, &be_quiet},
870 {'f', "friends", NULL,
871 gettext_noop ("include friend-only information"),
872 0, &GNUNET_GETOPT_set_one, &include_friend_only},
874 gettext_noop ("output our own identity only"),
875 0, &GNUNET_GETOPT_set_one, &get_self},
877 gettext_noop ("list all known peers"),
878 0, &GNUNET_GETOPT_set_one, &get_info},
879 {'d', "dump-hello", NULL,
880 gettext_noop ("dump hello to file"),
881 1, &GNUNET_GETOPT_set_string, &dump_hello},
882 {'g', "get-hello", NULL,
883 gettext_noop ("also output HELLO uri(s)"),
884 0, &GNUNET_GETOPT_set_one, &get_uri},
885 {'p', "put-hello", "HELLO",
886 gettext_noop ("add given HELLO uri to the database"),
887 1, &GNUNET_GETOPT_set_string, &put_uri},
888 GNUNET_GETOPT_OPTION_END
892 default_operation = GNUNET_YES;
893 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc,
899 GNUNET_PROGRAM_run (argc, argv, "gnunet-peerinfo",
900 gettext_noop ("Print information about peers."),
901 options, &run, NULL)) ? 0 : 1;
902 GNUNET_free ((void*) argv);
906 /* end of gnunet-peerinfo.c */