2 This file is part of GNUnet.
3 (C) 2001-2012 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_crypto_lib.h"
29 #include "gnunet_configuration_lib.h"
30 #include "gnunet_getopt_lib.h"
31 #include "gnunet_program_lib.h"
32 #include "gnunet_hello_lib.h"
33 #include "gnunet_transport_service.h"
34 #include "gnunet_peerinfo_service.h"
35 #include "gnunet-peerinfo_plugins.h"
38 * How long until we time out during peerinfo iterations?
40 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
43 * Structure we use to collect printable address information.
48 * Record we keep for each printable address.
53 * Current address-to-string context (if active, otherwise NULL).
55 struct GNUNET_TRANSPORT_AddressToStringContext *atsc;
58 * Address expiration time
60 struct GNUNET_TIME_Absolute expiration;
68 * Print context this address record belongs to.
70 struct PrintContext *pc;
75 * Structure we use to collect printable address information.
83 struct PrintContext *next;
88 struct PrintContext *prev;
91 * Identity of the peer.
93 struct GNUNET_PeerIdentity peer;
96 * List of printable addresses.
98 struct AddressRecord *address_list;
101 * Number of completed addresses in 'address_list'.
103 unsigned int num_addresses;
106 * Number of addresses allocated in 'address_list'.
108 unsigned int address_list_size;
111 * Current offset in 'address_list' (counted down).
116 * Hello was friend only, GNUNET_YES or GNUNET_NO
126 static int no_resolve;
136 static int include_friend_only;
151 static int default_operation;
161 static char *put_uri;
166 static char *dump_hello;
169 * Handle to peerinfo service.
171 static struct GNUNET_PEERINFO_Handle *peerinfo;
174 * Configuration handle.
176 static const struct GNUNET_CONFIGURATION_Handle *cfg;
179 * Main state machine task (if active).
181 static GNUNET_SCHEDULER_TaskIdentifier tt;
184 * Current iterator context (if active, otherwise NULL).
186 static struct GNUNET_PEERINFO_IteratorContext *pic;
191 static struct GNUNET_PeerIdentity my_peer_identity;
194 * Head of list of print contexts.
196 static struct PrintContext *pc_head;
199 * Tail of list of print contexts.
201 static struct PrintContext *pc_tail;
204 * Handle to current 'GNUNET_PEERINFO_add_peer' operation.
206 static struct GNUNET_PEERINFO_AddContext *ac;
210 * Main state machine that goes over all options and
211 * runs the next requested function.
217 state_machine (void *cls,
218 const struct GNUNET_SCHEDULER_TaskContext *tc);
221 /* ********************* 'get_info' ******************* */
224 * Print the collected address information to the console and free 'pc'.
226 * @param pc printing context
229 dump_pc (struct PrintContext *pc)
233 printf (_("%sPeer `%s'\n"),
234 (GNUNET_YES == pc->friend_only) ? "F2F: " : "",
235 GNUNET_i2s_full (&pc->peer));
236 for (i = 0; i < pc->num_addresses; i++)
238 if (NULL != pc->address_list[i].result)
240 printf (_("\tExpires: %s \t %s\n"), GNUNET_STRINGS_absolute_time_to_string(pc->address_list[i].expiration), pc->address_list[i].result);
241 GNUNET_free (pc->address_list[i].result);
245 GNUNET_free_non_null (pc->address_list);
246 GNUNET_CONTAINER_DLL_remove (pc_head,
250 if ( (NULL == pc_head) &&
252 tt = GNUNET_SCHEDULER_add_now (&state_machine, NULL);
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
266 process_resolved_address (void *cls, const char *address, int res)
268 struct AddressRecord * ar = cls;
269 struct PrintContext *pc = ar->pc;
273 if (NULL == ar->result)
274 ar->result = GNUNET_strdup (address);
279 if (GNUNET_SYSERR == res)
282 _("Failure: Cannot convert address to string for peer `%s'\n"),
283 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, const struct GNUNET_HELLO_Address *address,
304 struct GNUNET_TIME_Absolute expiration)
306 struct PrintContext *pc = cls;
314 * Iterator callback to go over all addresses.
317 * @param address the address
318 * @param expiration expiration time
319 * @return GNUNET_OK to keep the address and continue
322 print_address (void *cls, const struct GNUNET_HELLO_Address *address,
323 struct GNUNET_TIME_Absolute expiration)
325 struct PrintContext *pc = cls;
326 struct AddressRecord *ar;
327 GNUNET_assert (0 < pc->off);
328 ar = &pc->address_list[--pc->off];
330 ar->expiration = expiration;
331 ar->atsc = GNUNET_TRANSPORT_address_to_string (cfg, address, no_resolve,
332 GNUNET_TIME_relative_multiply
333 (GNUNET_TIME_UNIT_SECONDS, 10),
334 &process_resolved_address, ar);
340 * Print information about the peer.
341 * Currently prints the GNUNET_PeerIdentity and the transport address.
343 * @param cls the 'struct PrintContext'
344 * @param peer identity of the peer
345 * @param hello addresses of the peer
346 * @param err_msg error message
349 print_peer_info (void *cls, const struct GNUNET_PeerIdentity *peer,
350 const struct GNUNET_HELLO_Message *hello, const char *err_msg)
352 struct PrintContext *pc;
357 pic = NULL; /* end of iteration */
361 _("Error in communication with PEERINFO service: %s\n"),
365 tt = GNUNET_SCHEDULER_add_now (&state_machine, NULL);
368 friend_only = GNUNET_NO;
370 friend_only = GNUNET_HELLO_is_friend_only (hello);
371 if ((GNUNET_YES == be_quiet) || (NULL == hello))
374 (GNUNET_YES == friend_only) ? "F2F: " : "",
375 GNUNET_i2s_full (peer));
378 pc = GNUNET_new (struct PrintContext);
379 GNUNET_CONTAINER_DLL_insert (pc_head,
383 pc->friend_only = friend_only;
384 GNUNET_HELLO_iterate_addresses (hello,
393 pc->address_list_size = pc->off;
394 pc->address_list = GNUNET_malloc (sizeof (struct AddressRecord) * pc->off);
395 GNUNET_HELLO_iterate_addresses (hello, GNUNET_NO,
399 /* ************************* DUMP Hello ************************** */
401 static int count_addr(void *cls,
402 const struct GNUNET_HELLO_Address *address,
403 struct GNUNET_TIME_Absolute expiration)
411 * Write Hello of my peer to a file.
413 * @param cls the 'struct GetUriContext'
414 * @param peer identity of the peer (unused)
415 * @param hello addresses of the peer
416 * @param err_msg error message
419 dump_my_hello (void *cls, const struct GNUNET_PeerIdentity *peer,
420 const struct GNUNET_HELLO_Message *hello,
430 _("Error in communication with PEERINFO service: %s\n"),
432 tt = GNUNET_SCHEDULER_add_now (&state_machine, NULL);
439 _("Failure: Did not receive %s\n"), "HELLO");
443 size = GNUNET_HELLO_size (hello);
447 _("Failure: Received invalid %s\n"), "HELLO");
450 if (GNUNET_SYSERR == GNUNET_DISK_fn_write (dump_hello, hello, size,
451 GNUNET_DISK_PERM_USER_READ |
452 GNUNET_DISK_PERM_USER_WRITE |
453 GNUNET_DISK_PERM_GROUP_READ |
454 GNUNET_DISK_PERM_OTHER_READ))
456 FPRINTF (stderr, _("Failed to write HELLO with %u bytes to file `%s'\n"),
458 if (0 != UNLINK (dump_hello))
459 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING |
460 GNUNET_ERROR_TYPE_BULK, "unlink", dump_hello);
464 GNUNET_HELLO_iterate_addresses (hello, GNUNET_NO, count_addr, &c_addr);
469 _("Wrote %s HELLO containing %u addresses with %u bytes to file `%s'\n"),
470 (GNUNET_YES == GNUNET_HELLO_is_friend_only(hello)) ? "friend-only": "public",
471 c_addr, size, dump_hello);
474 GNUNET_free (dump_hello);
480 /* ************************* GET URI ************************** */
484 * Print URI of the peer.
486 * @param cls the 'struct GetUriContext'
487 * @param peer identity of the peer (unused)
488 * @param hello addresses of the peer
489 * @param err_msg error message
492 print_my_uri (void *cls, const struct GNUNET_PeerIdentity *peer,
493 const struct GNUNET_HELLO_Message *hello,
501 _("Error in communication with PEERINFO service: %s\n"),
503 tt = GNUNET_SCHEDULER_add_now (&state_machine, NULL);
509 char *uri = GNUNET_HELLO_compose_uri(hello, &GPI_plugins_find);
511 printf ("%s\n", (const char *) uri);
517 /* ************************* import HELLO by URI ********************* */
521 * Continuation called from 'GNUNET_PEERINFO_add_peer'
523 * @param cls closure, NULL
524 * @param emsg error message, NULL on success
527 add_continuation (void *cls,
533 _("Failure adding HELLO: %s\n"),
535 tt = GNUNET_SCHEDULER_add_now (&state_machine, NULL);
540 * Parse the PUT URI given at the command line and add it to our peerinfo
543 * @param put_uri URI string to parse
544 * @return GNUNET_OK on success, GNUNET_SYSERR if the URI was invalid, GNUNET_NO on other errors
547 parse_hello_uri (const char *put_uri)
549 struct GNUNET_HELLO_Message *hello = NULL;
551 int ret = GNUNET_HELLO_parse_uri(put_uri, &my_peer_identity.public_key,
552 &hello, &GPI_plugins_find);
555 /* WARNING: this adds the address from URI WITHOUT verification! */
556 if (GNUNET_OK == ret)
557 ac = GNUNET_PEERINFO_add_peer (peerinfo, hello, &add_continuation, NULL);
559 tt = GNUNET_SCHEDULER_add_now (&state_machine, NULL);
563 /* wait 1s to give peerinfo operation a chance to succeed */
564 /* FIXME: current peerinfo API sucks to require this; not to mention
565 that we get no feedback to determine if the operation actually succeeded */
570 /* ************************ Main state machine ********************* */
574 * Main state machine that goes over all options and
575 * runs the next requested function.
578 * @param tc scheduler context
581 shutdown_task (void *cls,
582 const struct GNUNET_SCHEDULER_TaskContext *tc)
584 struct PrintContext *pc;
585 struct AddressRecord *ar;
590 GNUNET_PEERINFO_add_peer_cancel (ac);
593 if (GNUNET_SCHEDULER_NO_TASK != tt)
595 GNUNET_SCHEDULER_cancel (tt);
596 tt = GNUNET_SCHEDULER_NO_TASK;
600 GNUNET_PEERINFO_iterate_cancel (pic);
603 while (NULL != (pc = pc_head))
605 GNUNET_CONTAINER_DLL_remove (pc_head,
608 for (i=0;i<pc->address_list_size;i++)
610 ar = &pc->address_list[i];
611 GNUNET_free_non_null (ar->result);
612 if (NULL != ar->atsc)
614 GNUNET_TRANSPORT_address_to_string_cancel (ar->atsc);
618 GNUNET_free_non_null (pc->address_list);
621 GPI_plugins_unload ();
622 if (NULL != peerinfo)
624 GNUNET_PEERINFO_disconnect (peerinfo);
630 * Function called with the result of the check if the 'peerinfo'
631 * service is running.
633 * @param cls closure with our configuration
634 * @param result #GNUNET_YES if transport is running
637 testservice_task (void *cls, int result)
639 struct GNUNET_CRYPTO_EddsaPrivateKey *priv;
642 if (GNUNET_YES != result)
644 FPRINTF (stderr, _("Service `%s' is not running, please start GNUnet\n"), "peerinfo");
648 if (NULL == (peerinfo = GNUNET_PEERINFO_connect (cfg)))
650 FPRINTF (stderr, "%s", _("Could not access PEERINFO service. Exiting.\n"));
653 if ( (GNUNET_YES == get_self) || (GNUNET_YES == get_uri) || (NULL != dump_hello) )
655 /* load private key */
656 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "PEER",
660 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, "PEER", "PRIVATE_KEY");
663 if (NULL == (priv = GNUNET_CRYPTO_eddsa_key_create_from_file (fn)))
665 FPRINTF (stderr, _("Loading hostkey from `%s' failed.\n"), fn);
670 GNUNET_CRYPTO_eddsa_key_get_public (priv,
671 &my_peer_identity.public_key);
675 tt = GNUNET_SCHEDULER_add_now (&state_machine, NULL);
676 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
683 * Main function that will be run by the scheduler.
686 * @param args remaining command-line arguments
687 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
688 * @param c configuration
691 run (void *cls, char *const *args, const char *cfgfile,
692 const struct GNUNET_CONFIGURATION_Handle *c)
695 if ( (NULL != args[0]) &&
697 (args[0] == strcasestr (args[0], "gnunet://hello/")) )
699 put_uri = GNUNET_strdup (args[0]);
705 _("Invalid command line argument `%s'\n"),
710 GNUNET_CLIENT_service_test ("peerinfo", cfg, GNUNET_TIME_UNIT_SECONDS,
711 &testservice_task, (void *) cfg);
716 * Main state machine that goes over all options and
717 * runs the next requested function.
720 * @param tc scheduler context
723 state_machine (void *cls,
724 const struct GNUNET_SCHEDULER_TaskContext *tc)
726 tt = GNUNET_SCHEDULER_NO_TASK;
730 GPI_plugins_load (cfg);
731 if (GNUNET_SYSERR == parse_hello_uri (put_uri))
734 _("Invalid URI `%s'\n"),
736 GNUNET_SCHEDULER_shutdown ();
738 GNUNET_free (put_uri);
741 else if (GNUNET_YES == get_info)
743 get_info = GNUNET_NO;
744 GPI_plugins_load (cfg);
745 pic = GNUNET_PEERINFO_iterate (peerinfo, include_friend_only, NULL,
747 &print_peer_info, NULL);
749 else if (GNUNET_YES == get_self)
751 get_self = GNUNET_NO;
754 GNUNET_i2s_full (&my_peer_identity));
756 printf (_("I am peer `%s'.\n"),
757 GNUNET_i2s_full (&my_peer_identity));
758 tt = GNUNET_SCHEDULER_add_now (&state_machine, NULL);
760 else if (GNUNET_YES == get_uri)
762 GPI_plugins_load (cfg);
763 pic = GNUNET_PEERINFO_iterate (peerinfo, include_friend_only, &my_peer_identity,
764 TIMEOUT, &print_my_uri, NULL);
767 else if (NULL != dump_hello)
769 pic = GNUNET_PEERINFO_iterate (peerinfo, include_friend_only, &my_peer_identity,
770 TIMEOUT, &dump_my_hello, NULL);
772 else if (GNUNET_YES == default_operation)
774 /* default operation list all */
775 default_operation = GNUNET_NO;
776 get_info = GNUNET_YES;
777 tt = GNUNET_SCHEDULER_add_now (&state_machine, NULL);
781 GNUNET_SCHEDULER_shutdown ();
783 default_operation = GNUNET_NO;
788 * The main function to obtain peer information.
790 * @param argc number of arguments from the command line
791 * @param argv command line arguments
792 * @return 0 ok, 1 on error
795 main (int argc, char *const *argv)
797 default_operation = GNUNET_YES;
798 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
799 {'n', "numeric", NULL,
800 gettext_noop ("don't resolve host names"),
801 0, &GNUNET_GETOPT_set_one, &no_resolve},
803 gettext_noop ("output only the identity strings"),
804 0, &GNUNET_GETOPT_set_one, &be_quiet},
805 {'f', "friends", NULL,
806 gettext_noop ("include friend-only information"),
807 0, &GNUNET_GETOPT_set_one, &include_friend_only},
809 gettext_noop ("output our own identity only"),
810 0, &GNUNET_GETOPT_set_one, &get_self},
812 gettext_noop ("list all known peers"),
813 0, &GNUNET_GETOPT_set_one, &get_info},
814 {'d', "dump-hello", NULL,
815 gettext_noop ("dump hello to file"),
816 1, &GNUNET_GETOPT_set_string, &dump_hello},
817 {'g', "get-hello", NULL,
818 gettext_noop ("also output HELLO uri(s)"),
819 0, &GNUNET_GETOPT_set_one, &get_uri},
820 {'p', "put-hello", "HELLO",
821 gettext_noop ("add given HELLO uri to the database"),
822 1, &GNUNET_GETOPT_set_string, &put_uri},
823 GNUNET_GETOPT_OPTION_END
827 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
831 GNUNET_PROGRAM_run (argc, argv, "gnunet-peerinfo",
832 gettext_noop ("Print information about peers."),
833 options, &run, NULL)) ? 0 : 1;
834 GNUNET_free ((void*) argv);
838 /* end of gnunet-peerinfo.c */