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;
153 * Handle to peerinfo service.
155 static struct GNUNET_PEERINFO_Handle *peerinfo;
158 * Configuration handle.
160 static const struct GNUNET_CONFIGURATION_Handle *cfg;
163 * Main state machine task (if active).
165 static GNUNET_SCHEDULER_TaskIdentifier tt;
168 * Current iterator context (if active, otherwise NULL).
170 static struct GNUNET_PEERINFO_IteratorContext *pic;
175 static struct GNUNET_PeerIdentity my_peer_identity;
180 static struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded my_public_key;
183 * Head of list of print contexts.
185 static struct PrintContext *pc_head;
188 * Tail of list of print contexts.
190 static struct PrintContext *pc_tail;
193 * Handle to current 'GNUNET_PEERINFO_add_peer' operation.
195 static struct GNUNET_PEERINFO_AddContext *ac;
199 * Main state machine that goes over all options and
200 * runs the next requested function.
206 state_machine (void *cls,
207 const struct GNUNET_SCHEDULER_TaskContext *tc);
210 /* ********************* 'get_info' ******************* */
213 * Print the collected address information to the console and free 'pc'.
215 * @param pc printing context
218 dump_pc (struct PrintContext *pc)
220 struct GNUNET_CRYPTO_HashAsciiEncoded enc;
223 GNUNET_CRYPTO_hash_to_enc (&pc->peer.hashPubKey, &enc);
224 printf (_("%sPeer `%s'\n"),
225 (GNUNET_YES == pc->friend_only) ? "F2F: " : "",
226 (const char *) &enc);
227 for (i = 0; i < pc->num_addresses; i++)
229 if (NULL != pc->address_list[i].result)
231 printf ("\t%s\n", pc->address_list[i].result);
232 GNUNET_free (pc->address_list[i].result);
236 GNUNET_free_non_null (pc->address_list);
237 GNUNET_CONTAINER_DLL_remove (pc_head,
241 if ( (NULL == pc_head) &&
243 tt = GNUNET_SCHEDULER_add_now (&state_machine, NULL);
247 /* ************************* list all known addresses **************** */
251 * Function to call with a human-readable format of an address
254 * @param address NULL on error, otherwise 0-terminated printable UTF-8 string
257 process_resolved_address (void *cls, const char *address)
259 struct AddressRecord * ar = cls;
260 struct PrintContext *pc = ar->pc;
264 if (NULL == ar->result)
265 ar->result = GNUNET_strdup (address);
270 if (pc->num_addresses == pc->address_list_size)
276 * Iterator callback to go over all addresses and count them.
278 * @param cls 'struct PrintContext' with 'off' to increment
279 * @param address the address
280 * @param expiration expiration time
281 * @return GNUNET_OK to keep the address and continue
284 count_address (void *cls, const struct GNUNET_HELLO_Address *address,
285 struct GNUNET_TIME_Absolute expiration)
287 struct PrintContext *pc = cls;
295 * Iterator callback to go over all addresses.
298 * @param address the address
299 * @param expiration expiration time
300 * @return GNUNET_OK to keep the address and continue
303 print_address (void *cls, const struct GNUNET_HELLO_Address *address,
304 struct GNUNET_TIME_Absolute expiration)
306 struct PrintContext *pc = cls;
307 struct AddressRecord *ar;
309 GNUNET_assert (0 < pc->off);
310 ar = &pc->address_list[--pc->off];
312 ar->atsc = GNUNET_TRANSPORT_address_to_string (cfg, address, no_resolve,
313 GNUNET_TIME_relative_multiply
314 (GNUNET_TIME_UNIT_SECONDS, 10),
315 &process_resolved_address, ar);
321 * Print information about the peer.
322 * Currently prints the GNUNET_PeerIdentity and the transport address.
324 * @param cls the 'struct PrintContext'
325 * @param peer identity of the peer
326 * @param hello addresses of the peer
327 * @param err_msg error message
330 print_peer_info (void *cls, const struct GNUNET_PeerIdentity *peer,
331 const struct GNUNET_HELLO_Message *hello, const char *err_msg)
333 struct GNUNET_CRYPTO_HashAsciiEncoded enc;
334 struct PrintContext *pc;
339 pic = NULL; /* end of iteration */
343 _("Error in communication with PEERINFO service: %s\n"),
347 tt = GNUNET_SCHEDULER_add_now (&state_machine, NULL);
350 friend_only = GNUNET_HELLO_is_friend_only (hello);
351 if ((GNUNET_YES == be_quiet) || (NULL == hello))
353 GNUNET_CRYPTO_hash_to_enc (&peer->hashPubKey, &enc);
355 (GNUNET_YES == friend_only) ? "F2F: " : "",
356 (const char *) &enc);
359 pc = GNUNET_malloc (sizeof (struct PrintContext));
360 GNUNET_CONTAINER_DLL_insert (pc_head,
364 pc->friend_only = friend_only;
365 GNUNET_HELLO_iterate_addresses (hello,
374 pc->address_list_size = pc->off;
375 pc->address_list = GNUNET_malloc (sizeof (struct AddressRecord) * pc->off);
376 GNUNET_HELLO_iterate_addresses (hello, GNUNET_NO,
381 /* ************************* GET URI ************************** */
385 * Print URI of the peer.
387 * @param cls the 'struct GetUriContext'
388 * @param peer identity of the peer (unused)
389 * @param hello addresses of the peer
390 * @param err_msg error message
393 print_my_uri (void *cls, const struct GNUNET_PeerIdentity *peer,
394 const struct GNUNET_HELLO_Message *hello,
402 _("Error in communication with PEERINFO service: %s\n"),
404 tt = GNUNET_SCHEDULER_add_now (&state_machine, NULL);
411 char *uri = GNUNET_HELLO_compose_uri(hello, &GPI_plugins_find);
413 printf ("%s\n", (const char *) uri);
419 /* ************************* import HELLO by URI ********************* */
423 * Continuation called from 'GNUNET_PEERINFO_add_peer'
425 * @param cls closure, NULL
426 * @param emsg error message, NULL on success
429 add_continuation (void *cls,
435 _("Failure adding HELLO: %s\n"),
437 tt = GNUNET_SCHEDULER_add_now (&state_machine, NULL);
442 * Parse the PUT URI given at the command line and add it to our peerinfo
445 * @param put_uri URI string to parse
446 * @return GNUNET_OK on success, GNUNET_SYSERR if the URI was invalid, GNUNET_NO on other errors
449 parse_hello_uri (const char *put_uri)
451 struct GNUNET_HELLO_Message *hello = NULL;
453 int ret = GNUNET_HELLO_parse_uri(put_uri, &my_public_key, &hello, &GPI_plugins_find);
456 /* WARNING: this adds the address from URI WITHOUT verification! */
457 if (GNUNET_OK == ret)
458 ac = GNUNET_PEERINFO_add_peer (peerinfo, hello, &add_continuation, NULL);
460 tt = GNUNET_SCHEDULER_add_now (&state_machine, NULL);
464 /* wait 1s to give peerinfo operation a chance to succeed */
465 /* FIXME: current peerinfo API sucks to require this; not to mention
466 that we get no feedback to determine if the operation actually succeeded */
471 /* ************************ Main state machine ********************* */
475 * Main state machine that goes over all options and
476 * runs the next requested function.
479 * @param tc scheduler context
482 shutdown_task (void *cls,
483 const struct GNUNET_SCHEDULER_TaskContext *tc)
485 struct PrintContext *pc;
486 struct AddressRecord *ar;
491 GNUNET_PEERINFO_add_peer_cancel (ac);
494 if (GNUNET_SCHEDULER_NO_TASK != tt)
496 GNUNET_SCHEDULER_cancel (tt);
497 tt = GNUNET_SCHEDULER_NO_TASK;
501 GNUNET_PEERINFO_iterate_cancel (pic);
504 while (NULL != (pc = pc_head))
506 GNUNET_CONTAINER_DLL_remove (pc_head,
509 for (i=0;i<pc->address_list_size;i++)
511 ar = &pc->address_list[i];
512 GNUNET_free_non_null (ar->result);
513 if (NULL != ar->atsc)
515 GNUNET_TRANSPORT_address_to_string_cancel (ar->atsc);
519 GNUNET_free_non_null (pc->address_list);
522 GPI_plugins_unload ();
523 if (NULL != peerinfo)
525 GNUNET_PEERINFO_disconnect (peerinfo);
532 * Main function that will be run by the scheduler.
535 * @param args remaining command-line arguments
536 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
537 * @param c configuration
540 run (void *cls, char *const *args, const char *cfgfile,
541 const struct GNUNET_CONFIGURATION_Handle *c)
543 struct GNUNET_CRYPTO_EccPrivateKey *priv;
547 if ( (NULL != args[0]) &&
549 (args[0] == strcasestr (args[0], "gnunet://hello/")) )
551 put_uri = GNUNET_strdup (args[0]);
557 _("Invalid command line argument `%s'\n"),
561 if (NULL == (peerinfo = GNUNET_PEERINFO_connect (cfg)))
563 FPRINTF (stderr, "%s", _("Could not access PEERINFO service. Exiting.\n"));
566 if ( (GNUNET_YES == get_self) || (GNUNET_YES == get_uri) )
568 /* load private key */
570 GNUNET_CONFIGURATION_get_value_filename (cfg, "PEER", "PRIVATE_KEY",
573 FPRINTF (stderr, _("Could not find option `%s:%s' in configuration.\n"),
574 "GNUNETD", "HOSTKEYFILE");
577 if (NULL == (priv = GNUNET_CRYPTO_ecc_key_create_from_file (fn)))
579 FPRINTF (stderr, _("Loading hostkey from `%s' failed.\n"), fn);
584 GNUNET_CRYPTO_ecc_key_get_public (priv, &my_public_key);
585 GNUNET_CRYPTO_ecc_key_free (priv);
586 GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key), &my_peer_identity.hashPubKey);
589 tt = GNUNET_SCHEDULER_add_now (&state_machine, NULL);
590 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
597 * Main state machine that goes over all options and
598 * runs the next requested function.
601 * @param tc scheduler context
604 state_machine (void *cls,
605 const struct GNUNET_SCHEDULER_TaskContext *tc)
607 tt = GNUNET_SCHEDULER_NO_TASK;
611 GPI_plugins_load (cfg);
612 if (GNUNET_SYSERR == parse_hello_uri (put_uri))
615 _("Invalid URI `%s'\n"),
617 GNUNET_SCHEDULER_shutdown ();
619 GNUNET_free (put_uri);
623 if (GNUNET_YES == get_info)
625 get_info = GNUNET_NO;
626 GPI_plugins_load (cfg);
627 pic = GNUNET_PEERINFO_iterate (peerinfo, include_friend_only, NULL,
629 &print_peer_info, NULL);
632 if (GNUNET_YES == get_self)
634 struct GNUNET_CRYPTO_HashAsciiEncoded enc;
636 get_self = GNUNET_NO;
637 GNUNET_CRYPTO_hash_to_enc (&my_peer_identity.hashPubKey, &enc);
639 printf ("%s\n", (char *) &enc);
641 printf (_("I am peer `%s'.\n"), (const char *) &enc);
643 if (GNUNET_YES == get_uri)
645 GPI_plugins_load (cfg);
646 pic = GNUNET_PEERINFO_iterate (peerinfo,include_friend_only, &my_peer_identity,
647 TIMEOUT, &print_my_uri, NULL);
651 GNUNET_SCHEDULER_shutdown ();
656 * The main function to obtain peer information.
658 * @param argc number of arguments from the command line
659 * @param argv command line arguments
660 * @return 0 ok, 1 on error
663 main (int argc, char *const *argv)
665 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
666 {'n', "numeric", NULL,
667 gettext_noop ("don't resolve host names"),
668 0, &GNUNET_GETOPT_set_one, &no_resolve},
670 gettext_noop ("output only the identity strings"),
671 0, &GNUNET_GETOPT_set_one, &be_quiet},
672 {'f', "friends", NULL,
673 gettext_noop ("include friend-only information"),
674 0, &GNUNET_GETOPT_set_one, &include_friend_only},
676 gettext_noop ("output our own identity only"),
677 0, &GNUNET_GETOPT_set_one, &get_self},
679 gettext_noop ("list all known peers"),
680 0, &GNUNET_GETOPT_set_one, &get_info},
681 {'g', "get-hello", NULL,
682 gettext_noop ("also output HELLO uri(s)"),
683 0, &GNUNET_GETOPT_set_one, &get_uri},
684 {'p', "put-hello", "HELLO",
685 gettext_noop ("add given HELLO uri to the database"),
686 1, &GNUNET_GETOPT_set_string, &put_uri},
687 GNUNET_GETOPT_OPTION_END
691 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
695 GNUNET_PROGRAM_run (argc, argv, "gnunet-peerinfo",
696 gettext_noop ("Print information about peers."),
697 options, &run, NULL)) ? 0 : 1;
698 GNUNET_free ((void*) argv);
702 /* end of gnunet-peerinfo.c */