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
27 #include "gnunet_crypto_lib.h"
28 #include "gnunet_configuration_lib.h"
29 #include "gnunet_getopt_lib.h"
30 #include "gnunet_program_lib.h"
31 #include "gnunet_hello_lib.h"
32 #include "gnunet_transport_service.h"
33 #include "gnunet_peerinfo_service.h"
34 #include "gnunet-peerinfo_plugins.h"
37 * How long until we time out during peerinfo iterations?
39 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
42 * Structure we use to collect printable address information.
47 * Record we keep for each printable address.
52 * Current address-to-string context (if active, otherwise NULL).
54 struct GNUNET_TRANSPORT_AddressToStringContext *atsc;
57 * Address expiration time
59 struct GNUNET_TIME_Absolute expiration;
67 * Print context this address record belongs to.
69 struct PrintContext *pc;
74 * Structure we use to collect printable address information.
82 struct PrintContext *next;
87 struct PrintContext *prev;
90 * Identity of the peer.
92 struct GNUNET_PeerIdentity peer;
95 * List of printable addresses.
97 struct AddressRecord *address_list;
100 * Number of completed addresses in 'address_list'.
102 unsigned int num_addresses;
105 * Number of addresses allocated in 'address_list'.
107 unsigned int address_list_size;
110 * Current offset in 'address_list' (counted down).
115 * Hello was friend only, GNUNET_YES or GNUNET_NO
125 static int no_resolve;
135 static int include_friend_only;
150 static int default_operation;
160 static char *put_uri;
165 static char *dump_hello;
168 * Handle to peerinfo service.
170 static struct GNUNET_PEERINFO_Handle *peerinfo;
173 * Configuration handle.
175 static const struct GNUNET_CONFIGURATION_Handle *cfg;
178 * Main state machine task (if active).
180 static GNUNET_SCHEDULER_TaskIdentifier tt;
183 * Current iterator context (if active, otherwise NULL).
185 static struct GNUNET_PEERINFO_IteratorContext *pic;
190 static struct GNUNET_PeerIdentity my_peer_identity;
193 * Head of list of print contexts.
195 static struct PrintContext *pc_head;
198 * Tail of list of print contexts.
200 static struct PrintContext *pc_tail;
203 * Handle to current 'GNUNET_PEERINFO_add_peer' operation.
205 static struct GNUNET_PEERINFO_AddContext *ac;
209 * Main state machine that goes over all options and
210 * runs the next requested function.
216 state_machine (void *cls,
217 const struct GNUNET_SCHEDULER_TaskContext *tc);
220 /* ********************* 'get_info' ******************* */
223 * Print the collected address information to the console and free 'pc'.
225 * @param pc printing context
228 dump_pc (struct PrintContext *pc)
232 printf (_("%sPeer `%s'\n"),
233 (GNUNET_YES == pc->friend_only) ? "F2F: " : "",
234 GNUNET_i2s_full (&pc->peer));
235 for (i = 0; i < pc->num_addresses; i++)
237 if (NULL != pc->address_list[i].result)
239 printf (_("\tExpires: %s \t %s\n"), GNUNET_STRINGS_absolute_time_to_string(pc->address_list[i].expiration), 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, NULL);
255 /* ************************* list all known addresses **************** */
259 * Function to call with a human-readable format of an address
262 * @param address NULL on error, otherwise 0-terminated printable UTF-8 string
265 process_resolved_address (void *cls, const char *address)
267 struct AddressRecord * ar = cls;
268 struct PrintContext *pc = ar->pc;
272 if (NULL == ar->result)
273 ar->result = GNUNET_strdup (address);
278 if (pc->num_addresses == pc->address_list_size)
284 * Iterator callback to go over all addresses and count them.
286 * @param cls 'struct PrintContext' with 'off' to increment
287 * @param address the address
288 * @param expiration expiration time
289 * @return GNUNET_OK to keep the address and continue
292 count_address (void *cls, const struct GNUNET_HELLO_Address *address,
293 struct GNUNET_TIME_Absolute expiration)
295 struct PrintContext *pc = cls;
303 * Iterator callback to go over all addresses.
306 * @param address the address
307 * @param expiration expiration time
308 * @return GNUNET_OK to keep the address and continue
311 print_address (void *cls, const struct GNUNET_HELLO_Address *address,
312 struct GNUNET_TIME_Absolute expiration)
314 struct PrintContext *pc = cls;
315 struct AddressRecord *ar;
316 GNUNET_assert (0 < pc->off);
317 ar = &pc->address_list[--pc->off];
319 ar->expiration = expiration;
320 ar->atsc = GNUNET_TRANSPORT_address_to_string (cfg, address, no_resolve,
321 GNUNET_TIME_relative_multiply
322 (GNUNET_TIME_UNIT_SECONDS, 10),
323 &process_resolved_address, ar);
329 * Print information about the peer.
330 * Currently prints the GNUNET_PeerIdentity and the transport address.
332 * @param cls the 'struct PrintContext'
333 * @param peer identity of the peer
334 * @param hello addresses of the peer
335 * @param err_msg error message
338 print_peer_info (void *cls, const struct GNUNET_PeerIdentity *peer,
339 const struct GNUNET_HELLO_Message *hello, const char *err_msg)
341 struct PrintContext *pc;
346 pic = NULL; /* end of iteration */
350 _("Error in communication with PEERINFO service: %s\n"),
354 tt = GNUNET_SCHEDULER_add_now (&state_machine, NULL);
357 friend_only = GNUNET_NO;
359 friend_only = GNUNET_HELLO_is_friend_only (hello);
360 if ((GNUNET_YES == be_quiet) || (NULL == hello))
363 (GNUNET_YES == friend_only) ? "F2F: " : "",
364 GNUNET_i2s_full (peer));
367 pc = GNUNET_malloc (sizeof (struct PrintContext));
368 GNUNET_CONTAINER_DLL_insert (pc_head,
372 pc->friend_only = friend_only;
373 GNUNET_HELLO_iterate_addresses (hello,
382 pc->address_list_size = pc->off;
383 pc->address_list = GNUNET_malloc (sizeof (struct AddressRecord) * pc->off);
384 GNUNET_HELLO_iterate_addresses (hello, GNUNET_NO,
388 /* ************************* DUMP Hello ************************** */
390 static int count_addr(void *cls,
391 const struct GNUNET_HELLO_Address *address,
392 struct GNUNET_TIME_Absolute expiration)
400 * Write Hello of my peer to a file.
402 * @param cls the 'struct GetUriContext'
403 * @param peer identity of the peer (unused)
404 * @param hello addresses of the peer
405 * @param err_msg error message
408 dump_my_hello (void *cls, const struct GNUNET_PeerIdentity *peer,
409 const struct GNUNET_HELLO_Message *hello,
419 _("Error in communication with PEERINFO service: %s\n"),
421 tt = GNUNET_SCHEDULER_add_now (&state_machine, NULL);
428 _("Failure: Did not receive %s\n"), "HELLO");
432 size = GNUNET_HELLO_size (hello);
436 _("Failure: Received invalid %s\n"), "HELLO");
439 if (GNUNET_SYSERR == GNUNET_DISK_fn_write (dump_hello, hello, size,
440 GNUNET_DISK_PERM_USER_READ |
441 GNUNET_DISK_PERM_USER_WRITE |
442 GNUNET_DISK_PERM_GROUP_READ |
443 GNUNET_DISK_PERM_OTHER_READ))
445 FPRINTF (stderr, _("Failed to write HELLO with %u bytes to file `%s'\n"),
447 if (0 != UNLINK (dump_hello))
448 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING |
449 GNUNET_ERROR_TYPE_BULK, "unlink", dump_hello);
453 GNUNET_HELLO_iterate_addresses (hello, GNUNET_NO, count_addr, &c_addr);
458 _("Wrote %s HELLO containing %u addresses with %u bytes to file `%s'\n"),
459 (GNUNET_YES == GNUNET_HELLO_is_friend_only(hello)) ? "friend-only": "public",
460 c_addr, size, dump_hello);
463 GNUNET_free (dump_hello);
469 /* ************************* GET URI ************************** */
473 * Print URI of the peer.
475 * @param cls the 'struct GetUriContext'
476 * @param peer identity of the peer (unused)
477 * @param hello addresses of the peer
478 * @param err_msg error message
481 print_my_uri (void *cls, const struct GNUNET_PeerIdentity *peer,
482 const struct GNUNET_HELLO_Message *hello,
490 _("Error in communication with PEERINFO service: %s\n"),
492 tt = GNUNET_SCHEDULER_add_now (&state_machine, NULL);
498 char *uri = GNUNET_HELLO_compose_uri(hello, &GPI_plugins_find);
500 printf ("%s\n", (const char *) uri);
506 /* ************************* import HELLO by URI ********************* */
510 * Continuation called from 'GNUNET_PEERINFO_add_peer'
512 * @param cls closure, NULL
513 * @param emsg error message, NULL on success
516 add_continuation (void *cls,
522 _("Failure adding HELLO: %s\n"),
524 tt = GNUNET_SCHEDULER_add_now (&state_machine, NULL);
529 * Parse the PUT URI given at the command line and add it to our peerinfo
532 * @param put_uri URI string to parse
533 * @return GNUNET_OK on success, GNUNET_SYSERR if the URI was invalid, GNUNET_NO on other errors
536 parse_hello_uri (const char *put_uri)
538 struct GNUNET_HELLO_Message *hello = NULL;
540 int ret = GNUNET_HELLO_parse_uri(put_uri, &my_peer_identity.public_key,
541 &hello, &GPI_plugins_find);
544 /* WARNING: this adds the address from URI WITHOUT verification! */
545 if (GNUNET_OK == ret)
546 ac = GNUNET_PEERINFO_add_peer (peerinfo, hello, &add_continuation, NULL);
548 tt = GNUNET_SCHEDULER_add_now (&state_machine, NULL);
552 /* wait 1s to give peerinfo operation a chance to succeed */
553 /* FIXME: current peerinfo API sucks to require this; not to mention
554 that we get no feedback to determine if the operation actually succeeded */
559 /* ************************ Main state machine ********************* */
563 * Main state machine that goes over all options and
564 * runs the next requested function.
567 * @param tc scheduler context
570 shutdown_task (void *cls,
571 const struct GNUNET_SCHEDULER_TaskContext *tc)
573 struct PrintContext *pc;
574 struct AddressRecord *ar;
579 GNUNET_PEERINFO_add_peer_cancel (ac);
582 if (GNUNET_SCHEDULER_NO_TASK != tt)
584 GNUNET_SCHEDULER_cancel (tt);
585 tt = GNUNET_SCHEDULER_NO_TASK;
589 GNUNET_PEERINFO_iterate_cancel (pic);
592 while (NULL != (pc = pc_head))
594 GNUNET_CONTAINER_DLL_remove (pc_head,
597 for (i=0;i<pc->address_list_size;i++)
599 ar = &pc->address_list[i];
600 GNUNET_free_non_null (ar->result);
601 if (NULL != ar->atsc)
603 GNUNET_TRANSPORT_address_to_string_cancel (ar->atsc);
607 GNUNET_free_non_null (pc->address_list);
610 GPI_plugins_unload ();
611 if (NULL != peerinfo)
613 GNUNET_PEERINFO_disconnect (peerinfo);
620 * Main function that will be run by the scheduler.
623 * @param args remaining command-line arguments
624 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
625 * @param c configuration
628 run (void *cls, char *const *args, const char *cfgfile,
629 const struct GNUNET_CONFIGURATION_Handle *c)
631 struct GNUNET_CRYPTO_EccPrivateKey *priv;
635 if ( (NULL != args[0]) &&
637 (args[0] == strcasestr (args[0], "gnunet://hello/")) )
639 put_uri = GNUNET_strdup (args[0]);
645 _("Invalid command line argument `%s'\n"),
649 if (NULL == (peerinfo = GNUNET_PEERINFO_connect (cfg)))
651 FPRINTF (stderr, "%s", _("Could not access PEERINFO service. Exiting.\n"));
654 if ( (GNUNET_YES == get_self) || (GNUNET_YES == get_uri) || (NULL != dump_hello) )
656 /* load private key */
658 GNUNET_CONFIGURATION_get_value_filename (cfg, "PEER", "PRIVATE_KEY",
661 FPRINTF (stderr, _("Could not find option `%s:%s' in configuration.\n"),
662 "GNUNETD", "HOSTKEYFILE");
665 if (NULL == (priv = GNUNET_CRYPTO_ecc_key_create_from_file (fn)))
667 FPRINTF (stderr, _("Loading hostkey from `%s' failed.\n"), fn);
672 GNUNET_CRYPTO_ecc_key_get_public_for_signature (priv,
673 &my_peer_identity.public_key);
677 tt = GNUNET_SCHEDULER_add_now (&state_machine, NULL);
678 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
685 * Main state machine that goes over all options and
686 * runs the next requested function.
689 * @param tc scheduler context
692 state_machine (void *cls,
693 const struct GNUNET_SCHEDULER_TaskContext *tc)
695 tt = GNUNET_SCHEDULER_NO_TASK;
699 GPI_plugins_load (cfg);
700 if (GNUNET_SYSERR == parse_hello_uri (put_uri))
703 _("Invalid URI `%s'\n"),
705 GNUNET_SCHEDULER_shutdown ();
707 GNUNET_free (put_uri);
710 else if (GNUNET_YES == get_info)
712 get_info = GNUNET_NO;
713 GPI_plugins_load (cfg);
714 pic = GNUNET_PEERINFO_iterate (peerinfo, include_friend_only, NULL,
716 &print_peer_info, NULL);
718 else if (GNUNET_YES == get_self)
720 get_self = GNUNET_NO;
723 GNUNET_i2s_full (&my_peer_identity));
725 printf (_("I am peer `%s'.\n"),
726 GNUNET_i2s_full (&my_peer_identity));
727 tt = GNUNET_SCHEDULER_add_now (&state_machine, NULL);
729 else if (GNUNET_YES == get_uri)
731 GPI_plugins_load (cfg);
732 pic = GNUNET_PEERINFO_iterate (peerinfo, include_friend_only, &my_peer_identity,
733 TIMEOUT, &print_my_uri, NULL);
736 else if (NULL != dump_hello)
738 pic = GNUNET_PEERINFO_iterate (peerinfo, include_friend_only, &my_peer_identity,
739 TIMEOUT, &dump_my_hello, NULL);
741 else if (GNUNET_YES == default_operation)
743 /* default operation list all */
744 default_operation = GNUNET_NO;
745 get_info = GNUNET_YES;
746 tt = GNUNET_SCHEDULER_add_now (&state_machine, NULL);
750 GNUNET_SCHEDULER_shutdown ();
752 default_operation = GNUNET_NO;
757 * The main function to obtain peer information.
759 * @param argc number of arguments from the command line
760 * @param argv command line arguments
761 * @return 0 ok, 1 on error
764 main (int argc, char *const *argv)
766 default_operation = GNUNET_YES;
767 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
768 {'n', "numeric", NULL,
769 gettext_noop ("don't resolve host names"),
770 0, &GNUNET_GETOPT_set_one, &no_resolve},
772 gettext_noop ("output only the identity strings"),
773 0, &GNUNET_GETOPT_set_one, &be_quiet},
774 {'f', "friends", NULL,
775 gettext_noop ("include friend-only information"),
776 0, &GNUNET_GETOPT_set_one, &include_friend_only},
778 gettext_noop ("output our own identity only"),
779 0, &GNUNET_GETOPT_set_one, &get_self},
781 gettext_noop ("list all known peers"),
782 0, &GNUNET_GETOPT_set_one, &get_info},
783 {'d', "dump-hello", NULL,
784 gettext_noop ("dump hello to file"),
785 1, &GNUNET_GETOPT_set_string, &dump_hello},
786 {'g', "get-hello", NULL,
787 gettext_noop ("also output HELLO uri(s)"),
788 0, &GNUNET_GETOPT_set_one, &get_uri},
789 {'p', "put-hello", "HELLO",
790 gettext_noop ("add given HELLO uri to the database"),
791 1, &GNUNET_GETOPT_set_string, &put_uri},
792 GNUNET_GETOPT_OPTION_END
796 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
800 GNUNET_PROGRAM_run (argc, argv, "gnunet-peerinfo",
801 gettext_noop ("Print information about peers."),
802 options, &run, NULL)) ? 0 : 1;
803 GNUNET_free ((void*) argv);
807 /* end of gnunet-peerinfo.c */