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).
115 static int no_resolve;
140 static char *put_uri;
143 * Handle to peerinfo service.
145 static struct GNUNET_PEERINFO_Handle *peerinfo;
148 * Configuration handle.
150 static const struct GNUNET_CONFIGURATION_Handle *cfg;
153 * Main state machine task (if active).
155 static GNUNET_SCHEDULER_TaskIdentifier tt;
158 * Current iterator context (if active, otherwise NULL).
160 static struct GNUNET_PEERINFO_IteratorContext *pic;
165 static struct GNUNET_PeerIdentity my_peer_identity;
170 static struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded my_public_key;
173 * Head of list of print contexts.
175 static struct PrintContext *pc_head;
178 * Tail of list of print contexts.
180 static struct PrintContext *pc_tail;
183 * Handle to current 'GNUNET_PEERINFO_add_peer' operation.
185 static struct GNUNET_PEERINFO_AddContext *ac;
189 * Main state machine that goes over all options and
190 * runs the next requested function.
196 state_machine (void *cls,
197 const struct GNUNET_SCHEDULER_TaskContext *tc);
200 /* ********************* 'get_info' ******************* */
203 * Print the collected address information to the console and free 'pc'.
205 * @param pc printing context
208 dump_pc (struct PrintContext *pc)
210 struct GNUNET_CRYPTO_HashAsciiEncoded enc;
213 GNUNET_CRYPTO_hash_to_enc (&pc->peer.hashPubKey, &enc);
214 printf (_("Peer `%s'\n"),
215 (const char *) &enc);
216 for (i = 0; i < pc->num_addresses; i++)
218 if (NULL != pc->address_list[i].result)
220 printf ("\t%s\n", pc->address_list[i].result);
221 GNUNET_free (pc->address_list[i].result);
225 GNUNET_free_non_null (pc->address_list);
226 GNUNET_CONTAINER_DLL_remove (pc_head,
230 if ( (NULL == pc_head) &&
232 tt = GNUNET_SCHEDULER_add_now (&state_machine, NULL);
236 /* ************************* list all known addresses **************** */
240 * Function to call with a human-readable format of an address
243 * @param address NULL on error, otherwise 0-terminated printable UTF-8 string
246 process_resolved_address (void *cls, const char *address)
248 struct AddressRecord * ar = cls;
249 struct PrintContext *pc = ar->pc;
253 if (NULL == ar->result)
254 ar->result = GNUNET_strdup (address);
259 if (pc->num_addresses == pc->address_list_size)
265 * Iterator callback to go over all addresses and count them.
267 * @param cls 'struct PrintContext' with 'off' to increment
268 * @param address the address
269 * @param expiration expiration time
270 * @return GNUNET_OK to keep the address and continue
273 count_address (void *cls, const struct GNUNET_HELLO_Address *address,
274 struct GNUNET_TIME_Absolute expiration)
276 struct PrintContext *pc = cls;
284 * Iterator callback to go over all addresses.
287 * @param address the address
288 * @param expiration expiration time
289 * @return GNUNET_OK to keep the address and continue
292 print_address (void *cls, const struct GNUNET_HELLO_Address *address,
293 struct GNUNET_TIME_Absolute expiration)
295 struct PrintContext *pc = cls;
296 struct AddressRecord *ar;
298 GNUNET_assert (0 < pc->off);
299 ar = &pc->address_list[--pc->off];
301 ar->atsc = GNUNET_TRANSPORT_address_to_string (cfg, address, no_resolve,
302 GNUNET_TIME_relative_multiply
303 (GNUNET_TIME_UNIT_SECONDS, 10),
304 &process_resolved_address, ar);
310 * Print information about the peer.
311 * Currently prints the GNUNET_PeerIdentity and the transport address.
313 * @param cls the 'struct PrintContext'
314 * @param peer identity of the peer
315 * @param hello addresses of the peer
316 * @param err_msg error message
319 print_peer_info (void *cls, const struct GNUNET_PeerIdentity *peer,
320 const struct GNUNET_HELLO_Message *hello, const char *err_msg)
322 struct GNUNET_CRYPTO_HashAsciiEncoded enc;
323 struct PrintContext *pc;
327 pic = NULL; /* end of iteration */
331 _("Error in communication with PEERINFO service: %s\n"),
335 tt = GNUNET_SCHEDULER_add_now (&state_machine, NULL);
338 if ((GNUNET_YES == be_quiet) || (NULL == hello))
340 GNUNET_CRYPTO_hash_to_enc (&peer->hashPubKey, &enc);
341 printf ("%s\n", (const char *) &enc);
344 pc = GNUNET_malloc (sizeof (struct PrintContext));
345 GNUNET_CONTAINER_DLL_insert (pc_head,
349 GNUNET_HELLO_iterate_addresses (hello,
358 pc->address_list_size = pc->off;
359 pc->address_list = GNUNET_malloc (sizeof (struct AddressRecord) * pc->off);
360 GNUNET_HELLO_iterate_addresses (hello, GNUNET_NO,
365 /* ************************* GET URI ************************** */
369 * Print URI of the peer.
371 * @param cls the 'struct GetUriContext'
372 * @param peer identity of the peer (unused)
373 * @param hello addresses of the peer
374 * @param err_msg error message
377 print_my_uri (void *cls, const struct GNUNET_PeerIdentity *peer,
378 const struct GNUNET_HELLO_Message *hello,
386 _("Error in communication with PEERINFO service: %s\n"),
388 tt = GNUNET_SCHEDULER_add_now (&state_machine, NULL);
395 char *uri = GNUNET_HELLO_compose_uri(hello, &GPI_plugins_find);
397 printf ("%s\n", (const char *) uri);
403 /* ************************* import HELLO by URI ********************* */
407 * Continuation called from 'GNUNET_PEERINFO_add_peer'
409 * @param cls closure, NULL
410 * @param emsg error message, NULL on success
413 add_continuation (void *cls,
419 _("Failure adding HELLO: %s\n"),
421 tt = GNUNET_SCHEDULER_add_now (&state_machine, NULL);
426 * Parse the PUT URI given at the command line and add it to our peerinfo
429 * @param put_uri URI string to parse
430 * @return GNUNET_OK on success, GNUNET_SYSERR if the URI was invalid, GNUNET_NO on other errors
433 parse_hello_uri (const char *put_uri)
435 struct GNUNET_HELLO_Message *hello;
437 int ret = GNUNET_HELLO_parse_uri(put_uri, &my_public_key, &hello, &GPI_plugins_find);
440 /* WARNING: this adds the address from URI WITHOUT verification! */
441 if (GNUNET_OK == ret)
442 ac = GNUNET_PEERINFO_add_peer (peerinfo, hello, &add_continuation, NULL);
444 tt = GNUNET_SCHEDULER_add_now (&state_machine, NULL);
448 /* wait 1s to give peerinfo operation a chance to succeed */
449 /* FIXME: current peerinfo API sucks to require this; not to mention
450 that we get no feedback to determine if the operation actually succeeded */
455 /* ************************ Main state machine ********************* */
459 * Main state machine that goes over all options and
460 * runs the next requested function.
463 * @param tc scheduler context
466 shutdown_task (void *cls,
467 const struct GNUNET_SCHEDULER_TaskContext *tc)
469 struct PrintContext *pc;
470 struct AddressRecord *ar;
475 GNUNET_PEERINFO_add_peer_cancel (ac);
478 if (GNUNET_SCHEDULER_NO_TASK != tt)
480 GNUNET_SCHEDULER_cancel (tt);
481 tt = GNUNET_SCHEDULER_NO_TASK;
485 GNUNET_PEERINFO_iterate_cancel (pic);
488 while (NULL != (pc = pc_head))
490 GNUNET_CONTAINER_DLL_remove (pc_head,
493 for (i=0;i<pc->address_list_size;i++)
495 ar = &pc->address_list[i];
496 GNUNET_free_non_null (ar->result);
497 if (NULL != ar->atsc)
499 GNUNET_TRANSPORT_address_to_string_cancel (ar->atsc);
503 GNUNET_free_non_null (pc->address_list);
506 GPI_plugins_unload ();
507 if (NULL != peerinfo)
509 GNUNET_PEERINFO_disconnect (peerinfo);
516 * Main function that will be run by the scheduler.
519 * @param args remaining command-line arguments
520 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
521 * @param c configuration
524 run (void *cls, char *const *args, const char *cfgfile,
525 const struct GNUNET_CONFIGURATION_Handle *c)
527 struct GNUNET_CRYPTO_RsaPrivateKey *priv;
531 if ( (NULL != args[0]) &&
533 (args[0] == strcasestr (args[0], "gnunet://hello/")) )
535 put_uri = GNUNET_strdup (args[0]);
541 _("Invalid command line argument `%s'\n"),
545 if (NULL == (peerinfo = GNUNET_PEERINFO_connect (cfg)))
547 FPRINTF (stderr, "%s", _("Could not access PEERINFO service. Exiting.\n"));
550 if ( (GNUNET_YES == get_self) || (GNUNET_YES == get_uri) )
552 /* load private key */
554 GNUNET_CONFIGURATION_get_value_filename (cfg, "GNUNETD", "HOSTKEY",
557 FPRINTF (stderr, _("Could not find option `%s:%s' in configuration.\n"),
558 "GNUNETD", "HOSTKEYFILE");
561 if (NULL == (priv = GNUNET_CRYPTO_rsa_key_create_from_file (fn)))
563 FPRINTF (stderr, _("Loading hostkey from `%s' failed.\n"), fn);
568 GNUNET_CRYPTO_rsa_key_get_public (priv, &my_public_key);
569 GNUNET_CRYPTO_rsa_key_free (priv);
570 GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key), &my_peer_identity.hashPubKey);
573 tt = GNUNET_SCHEDULER_add_now (&state_machine, NULL);
574 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
581 * Main state machine that goes over all options and
582 * runs the next requested function.
585 * @param tc scheduler context
588 state_machine (void *cls,
589 const struct GNUNET_SCHEDULER_TaskContext *tc)
591 tt = GNUNET_SCHEDULER_NO_TASK;
595 GPI_plugins_load (cfg);
596 if (GNUNET_SYSERR == parse_hello_uri (put_uri))
598 _("Invalid URI `%s'\n"),
600 GNUNET_free (put_uri);
604 if (GNUNET_YES == get_info)
606 get_info = GNUNET_NO;
607 GPI_plugins_load (cfg);
608 pic = GNUNET_PEERINFO_iterate (peerinfo, NULL,
610 &print_peer_info, NULL);
613 if (GNUNET_YES == get_self)
615 struct GNUNET_CRYPTO_HashAsciiEncoded enc;
617 get_self = GNUNET_NO;
618 GNUNET_CRYPTO_hash_to_enc (&my_peer_identity.hashPubKey, &enc);
620 printf ("%s\n", (char *) &enc);
622 printf (_("I am peer `%s'.\n"), (const char *) &enc);
624 if (GNUNET_YES == get_uri)
626 GPI_plugins_load (cfg);
627 pic = GNUNET_PEERINFO_iterate (peerinfo, &my_peer_identity,
628 TIMEOUT, &print_my_uri, NULL);
632 GNUNET_SCHEDULER_shutdown ();
637 * The main function to obtain peer information.
639 * @param argc number of arguments from the command line
640 * @param argv command line arguments
641 * @return 0 ok, 1 on error
644 main (int argc, char *const *argv)
646 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
647 {'n', "numeric", NULL,
648 gettext_noop ("don't resolve host names"),
649 0, &GNUNET_GETOPT_set_one, &no_resolve},
651 gettext_noop ("output only the identity strings"),
652 0, &GNUNET_GETOPT_set_one, &be_quiet},
654 gettext_noop ("output our own identity only"),
655 0, &GNUNET_GETOPT_set_one, &get_self},
657 gettext_noop ("list all known peers"),
658 0, &GNUNET_GETOPT_set_one, &get_info},
659 {'g', "get-hello", NULL,
660 gettext_noop ("also output HELLO uri(s)"),
661 0, &GNUNET_GETOPT_set_one, &get_uri},
662 {'p', "put-hello", "HELLO",
663 gettext_noop ("add given HELLO uri to the database"),
664 1, &GNUNET_GETOPT_set_string, &put_uri},
665 GNUNET_GETOPT_OPTION_END
669 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
673 GNUNET_PROGRAM_run (argc, argv, "gnunet-peerinfo",
674 gettext_noop ("Print information about peers."),
675 options, &run, NULL)) ? 0 : 1;
676 GNUNET_free ((void*) argv);
680 /* end of gnunet-peerinfo.c */