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;
62 * Print context this address record belongs to.
64 struct PrintContext *pc;
69 * Structure we use to collect printable address information.
77 struct PrintContext *next;
82 struct PrintContext *prev;
85 * Identity of the peer.
87 struct GNUNET_PeerIdentity peer;
90 * List of printable addresses.
92 struct AddressRecord *address_list;
95 * Number of completed addresses in 'address_list'.
97 unsigned int num_addresses;
100 * Number of addresses allocated in 'address_list'.
102 unsigned int address_list_size;
105 * Current offset in 'address_list' (counted down).
110 * Hello was friend only, GNUNET_YES or GNUNET_NO
120 static int no_resolve;
130 static int include_friend_only;
150 static char *put_uri;
155 static char *dump_hello;
158 * Handle to peerinfo service.
160 static struct GNUNET_PEERINFO_Handle *peerinfo;
163 * Configuration handle.
165 static const struct GNUNET_CONFIGURATION_Handle *cfg;
168 * Main state machine task (if active).
170 static GNUNET_SCHEDULER_TaskIdentifier tt;
173 * Current iterator context (if active, otherwise NULL).
175 static struct GNUNET_PEERINFO_IteratorContext *pic;
180 static struct GNUNET_PeerIdentity my_peer_identity;
185 static struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded my_public_key;
188 * Head of list of print contexts.
190 static struct PrintContext *pc_head;
193 * Tail of list of print contexts.
195 static struct PrintContext *pc_tail;
198 * Handle to current 'GNUNET_PEERINFO_add_peer' operation.
200 static struct GNUNET_PEERINFO_AddContext *ac;
204 * Main state machine that goes over all options and
205 * runs the next requested function.
211 state_machine (void *cls,
212 const struct GNUNET_SCHEDULER_TaskContext *tc);
215 /* ********************* 'get_info' ******************* */
218 * Print the collected address information to the console and free 'pc'.
220 * @param pc printing context
223 dump_pc (struct PrintContext *pc)
225 struct GNUNET_CRYPTO_HashAsciiEncoded enc;
228 GNUNET_CRYPTO_hash_to_enc (&pc->peer.hashPubKey, &enc);
229 printf (_("%sPeer `%s'\n"),
230 (GNUNET_YES == pc->friend_only) ? "F2F: " : "",
231 (const char *) &enc);
232 for (i = 0; i < pc->num_addresses; i++)
234 if (NULL != pc->address_list[i].result)
236 printf ("\t%s\n", pc->address_list[i].result);
237 GNUNET_free (pc->address_list[i].result);
241 GNUNET_free_non_null (pc->address_list);
242 GNUNET_CONTAINER_DLL_remove (pc_head,
246 if ( (NULL == pc_head) &&
248 tt = GNUNET_SCHEDULER_add_now (&state_machine, NULL);
252 /* ************************* list all known addresses **************** */
256 * Function to call with a human-readable format of an address
259 * @param address NULL on error, otherwise 0-terminated printable UTF-8 string
262 process_resolved_address (void *cls, const char *address)
264 struct AddressRecord * ar = cls;
265 struct PrintContext *pc = ar->pc;
269 if (NULL == ar->result)
270 ar->result = GNUNET_strdup (address);
275 if (pc->num_addresses == pc->address_list_size)
281 * Iterator callback to go over all addresses and count them.
283 * @param cls 'struct PrintContext' with 'off' to increment
284 * @param address the address
285 * @param expiration expiration time
286 * @return GNUNET_OK to keep the address and continue
289 count_address (void *cls, const struct GNUNET_HELLO_Address *address,
290 struct GNUNET_TIME_Absolute expiration)
292 struct PrintContext *pc = cls;
300 * Iterator callback to go over all addresses.
303 * @param address the address
304 * @param expiration expiration time
305 * @return GNUNET_OK to keep the address and continue
308 print_address (void *cls, const struct GNUNET_HELLO_Address *address,
309 struct GNUNET_TIME_Absolute expiration)
311 struct PrintContext *pc = cls;
312 struct AddressRecord *ar;
314 GNUNET_assert (0 < pc->off);
315 ar = &pc->address_list[--pc->off];
317 ar->atsc = GNUNET_TRANSPORT_address_to_string (cfg, address, no_resolve,
318 GNUNET_TIME_relative_multiply
319 (GNUNET_TIME_UNIT_SECONDS, 10),
320 &process_resolved_address, ar);
326 * Print information about the peer.
327 * Currently prints the GNUNET_PeerIdentity and the transport address.
329 * @param cls the 'struct PrintContext'
330 * @param peer identity of the peer
331 * @param hello addresses of the peer
332 * @param err_msg error message
335 print_peer_info (void *cls, const struct GNUNET_PeerIdentity *peer,
336 const struct GNUNET_HELLO_Message *hello, const char *err_msg)
338 struct GNUNET_CRYPTO_HashAsciiEncoded enc;
339 struct PrintContext *pc;
344 pic = NULL; /* end of iteration */
348 _("Error in communication with PEERINFO service: %s\n"),
352 tt = GNUNET_SCHEDULER_add_now (&state_machine, NULL);
355 friend_only = GNUNET_NO;
357 friend_only = GNUNET_HELLO_is_friend_only (hello);
358 if ((GNUNET_YES == be_quiet) || (NULL == hello))
360 GNUNET_CRYPTO_hash_to_enc (&peer->hashPubKey, &enc);
362 (GNUNET_YES == friend_only) ? "F2F: " : "",
363 (const char *) &enc);
366 pc = GNUNET_malloc (sizeof (struct PrintContext));
367 GNUNET_CONTAINER_DLL_insert (pc_head,
371 pc->friend_only = friend_only;
372 GNUNET_HELLO_iterate_addresses (hello,
381 pc->address_list_size = pc->off;
382 pc->address_list = GNUNET_malloc (sizeof (struct AddressRecord) * pc->off);
383 GNUNET_HELLO_iterate_addresses (hello, GNUNET_NO,
387 /* ************************* DUMP Hello ************************** */
389 static int count_addr(void *cls,
390 const struct GNUNET_HELLO_Address *address,
391 struct GNUNET_TIME_Absolute expiration)
399 * Write Hello of my peer to a file.
401 * @param cls the 'struct GetUriContext'
402 * @param peer identity of the peer (unused)
403 * @param hello addresses of the peer
404 * @param err_msg error message
407 dump_my_hello (void *cls, const struct GNUNET_PeerIdentity *peer,
408 const struct GNUNET_HELLO_Message *hello,
418 _("Error in communication with PEERINFO service: %s\n"),
420 tt = GNUNET_SCHEDULER_add_now (&state_machine, NULL);
427 _("Failure: Did not receive HELLO\n"));
431 size = GNUNET_HELLO_size (hello);
435 _("Failure: Received invalid HELLO\n"));
438 if (GNUNET_SYSERR == GNUNET_DISK_fn_write (dump_hello, hello, size,
439 GNUNET_DISK_PERM_USER_READ |
440 GNUNET_DISK_PERM_USER_WRITE |
441 GNUNET_DISK_PERM_GROUP_READ |
442 GNUNET_DISK_PERM_OTHER_READ))
444 FPRINTF (stderr, _("Failed to write HELLO with %u bytes to file `%s'\n"),
446 if (0 != UNLINK (dump_hello))
447 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING |
448 GNUNET_ERROR_TYPE_BULK, "unlink", dump_hello);
452 GNUNET_HELLO_iterate_addresses (hello, GNUNET_NO, count_addr, &c_addr);
457 _("Wrote %s HELLO containing %u addresses with %u bytes to file `%s'\n"),
458 (GNUNET_YES == GNUNET_HELLO_is_friend_only(hello)) ? "friend-only": "public",
459 c_addr, size, dump_hello);
462 GNUNET_free (dump_hello);
468 /* ************************* GET URI ************************** */
472 * Print URI of the peer.
474 * @param cls the 'struct GetUriContext'
475 * @param peer identity of the peer (unused)
476 * @param hello addresses of the peer
477 * @param err_msg error message
480 print_my_uri (void *cls, const struct GNUNET_PeerIdentity *peer,
481 const struct GNUNET_HELLO_Message *hello,
489 _("Error in communication with PEERINFO service: %s\n"),
491 tt = GNUNET_SCHEDULER_add_now (&state_machine, NULL);
497 char *uri = GNUNET_HELLO_compose_uri(hello, &GPI_plugins_find);
499 printf ("%s\n", (const char *) uri);
505 /* ************************* import HELLO by URI ********************* */
509 * Continuation called from 'GNUNET_PEERINFO_add_peer'
511 * @param cls closure, NULL
512 * @param emsg error message, NULL on success
515 add_continuation (void *cls,
521 _("Failure adding HELLO: %s\n"),
523 tt = GNUNET_SCHEDULER_add_now (&state_machine, NULL);
528 * Parse the PUT URI given at the command line and add it to our peerinfo
531 * @param put_uri URI string to parse
532 * @return GNUNET_OK on success, GNUNET_SYSERR if the URI was invalid, GNUNET_NO on other errors
535 parse_hello_uri (const char *put_uri)
537 struct GNUNET_HELLO_Message *hello = NULL;
539 int ret = GNUNET_HELLO_parse_uri(put_uri, &my_public_key, &hello, &GPI_plugins_find);
542 /* WARNING: this adds the address from URI WITHOUT verification! */
543 if (GNUNET_OK == ret)
544 ac = GNUNET_PEERINFO_add_peer (peerinfo, hello, &add_continuation, NULL);
546 tt = GNUNET_SCHEDULER_add_now (&state_machine, NULL);
550 /* wait 1s to give peerinfo operation a chance to succeed */
551 /* FIXME: current peerinfo API sucks to require this; not to mention
552 that we get no feedback to determine if the operation actually succeeded */
557 /* ************************ Main state machine ********************* */
561 * Main state machine that goes over all options and
562 * runs the next requested function.
565 * @param tc scheduler context
568 shutdown_task (void *cls,
569 const struct GNUNET_SCHEDULER_TaskContext *tc)
571 struct PrintContext *pc;
572 struct AddressRecord *ar;
577 GNUNET_PEERINFO_add_peer_cancel (ac);
580 if (GNUNET_SCHEDULER_NO_TASK != tt)
582 GNUNET_SCHEDULER_cancel (tt);
583 tt = GNUNET_SCHEDULER_NO_TASK;
587 GNUNET_PEERINFO_iterate_cancel (pic);
590 while (NULL != (pc = pc_head))
592 GNUNET_CONTAINER_DLL_remove (pc_head,
595 for (i=0;i<pc->address_list_size;i++)
597 ar = &pc->address_list[i];
598 GNUNET_free_non_null (ar->result);
599 if (NULL != ar->atsc)
601 GNUNET_TRANSPORT_address_to_string_cancel (ar->atsc);
605 GNUNET_free_non_null (pc->address_list);
608 GPI_plugins_unload ();
609 if (NULL != peerinfo)
611 GNUNET_PEERINFO_disconnect (peerinfo);
618 * Main function that will be run by the scheduler.
621 * @param args remaining command-line arguments
622 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
623 * @param c configuration
626 run (void *cls, char *const *args, const char *cfgfile,
627 const struct GNUNET_CONFIGURATION_Handle *c)
629 struct GNUNET_CRYPTO_EccPrivateKey *priv;
633 if ( (NULL != args[0]) &&
635 (args[0] == strcasestr (args[0], "gnunet://hello/")) )
637 put_uri = GNUNET_strdup (args[0]);
643 _("Invalid command line argument `%s'\n"),
647 if (NULL == (peerinfo = GNUNET_PEERINFO_connect (cfg)))
649 FPRINTF (stderr, "%s", _("Could not access PEERINFO service. Exiting.\n"));
652 if ( (GNUNET_YES == get_self) || (GNUNET_YES == get_uri) || (NULL != dump_hello) )
654 /* load private key */
656 GNUNET_CONFIGURATION_get_value_filename (cfg, "PEER", "PRIVATE_KEY",
659 FPRINTF (stderr, _("Could not find option `%s:%s' in configuration.\n"),
660 "GNUNETD", "HOSTKEYFILE");
663 if (NULL == (priv = GNUNET_CRYPTO_ecc_key_create_from_file (fn)))
665 FPRINTF (stderr, _("Loading hostkey from `%s' failed.\n"), fn);
670 GNUNET_CRYPTO_ecc_key_get_public (priv, &my_public_key);
671 GNUNET_CRYPTO_ecc_key_free (priv);
672 GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key), &my_peer_identity.hashPubKey);
675 tt = GNUNET_SCHEDULER_add_now (&state_machine, NULL);
676 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
683 * Main state machine that goes over all options and
684 * runs the next requested function.
687 * @param tc scheduler context
690 state_machine (void *cls,
691 const struct GNUNET_SCHEDULER_TaskContext *tc)
693 tt = GNUNET_SCHEDULER_NO_TASK;
697 GPI_plugins_load (cfg);
698 if (GNUNET_SYSERR == parse_hello_uri (put_uri))
701 _("Invalid URI `%s'\n"),
703 GNUNET_SCHEDULER_shutdown ();
705 GNUNET_free (put_uri);
709 if (GNUNET_YES == get_info)
711 get_info = GNUNET_NO;
712 GPI_plugins_load (cfg);
713 pic = GNUNET_PEERINFO_iterate (peerinfo, include_friend_only, NULL,
715 &print_peer_info, NULL);
718 if (GNUNET_YES == get_self)
720 struct GNUNET_CRYPTO_HashAsciiEncoded enc;
722 get_self = GNUNET_NO;
723 GNUNET_CRYPTO_hash_to_enc (&my_peer_identity.hashPubKey, &enc);
725 printf ("%s\n", (char *) &enc);
727 printf (_("I am peer `%s'.\n"), (const char *) &enc);
729 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);
737 if (NULL != dump_hello)
739 pic = GNUNET_PEERINFO_iterate (peerinfo, include_friend_only, &my_peer_identity,
740 TIMEOUT, &dump_my_hello, NULL);
743 GNUNET_SCHEDULER_shutdown ();
748 * The main function to obtain peer information.
750 * @param argc number of arguments from the command line
751 * @param argv command line arguments
752 * @return 0 ok, 1 on error
755 main (int argc, char *const *argv)
757 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
758 {'n', "numeric", NULL,
759 gettext_noop ("don't resolve host names"),
760 0, &GNUNET_GETOPT_set_one, &no_resolve},
762 gettext_noop ("output only the identity strings"),
763 0, &GNUNET_GETOPT_set_one, &be_quiet},
764 {'f', "friends", NULL,
765 gettext_noop ("include friend-only information"),
766 0, &GNUNET_GETOPT_set_one, &include_friend_only},
768 gettext_noop ("output our own identity only"),
769 0, &GNUNET_GETOPT_set_one, &get_self},
771 gettext_noop ("list all known peers"),
772 0, &GNUNET_GETOPT_set_one, &get_info},
773 {'d', "dump-hello", NULL,
774 gettext_noop ("dump hello to file"),
775 1, &GNUNET_GETOPT_set_string, &dump_hello},
776 {'g', "get-hello", NULL,
777 gettext_noop ("also output HELLO uri(s)"),
778 0, &GNUNET_GETOPT_set_one, &get_uri},
779 {'p', "put-hello", "HELLO",
780 gettext_noop ("add given HELLO uri to the database"),
781 1, &GNUNET_GETOPT_set_string, &put_uri},
782 GNUNET_GETOPT_OPTION_END
786 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
790 GNUNET_PROGRAM_run (argc, argv, "gnunet-peerinfo",
791 gettext_noop ("Print information about peers."),
792 options, &run, NULL)) ? 0 : 1;
793 GNUNET_free ((void*) argv);
797 /* end of gnunet-peerinfo.c */