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_NO;
352 friend_only = GNUNET_HELLO_is_friend_only (hello);
353 if ((GNUNET_YES == be_quiet) || (NULL == hello))
355 GNUNET_CRYPTO_hash_to_enc (&peer->hashPubKey, &enc);
357 (GNUNET_YES == friend_only) ? "F2F: " : "",
358 (const char *) &enc);
361 pc = GNUNET_malloc (sizeof (struct PrintContext));
362 GNUNET_CONTAINER_DLL_insert (pc_head,
366 pc->friend_only = friend_only;
367 GNUNET_HELLO_iterate_addresses (hello,
376 pc->address_list_size = pc->off;
377 pc->address_list = GNUNET_malloc (sizeof (struct AddressRecord) * pc->off);
378 GNUNET_HELLO_iterate_addresses (hello, GNUNET_NO,
383 /* ************************* GET URI ************************** */
387 * Print URI of the peer.
389 * @param cls the 'struct GetUriContext'
390 * @param peer identity of the peer (unused)
391 * @param hello addresses of the peer
392 * @param err_msg error message
395 print_my_uri (void *cls, const struct GNUNET_PeerIdentity *peer,
396 const struct GNUNET_HELLO_Message *hello,
404 _("Error in communication with PEERINFO service: %s\n"),
406 tt = GNUNET_SCHEDULER_add_now (&state_machine, NULL);
412 char *uri = GNUNET_HELLO_compose_uri(hello, &GPI_plugins_find);
414 printf ("%s\n", (const char *) uri);
420 /* ************************* import HELLO by URI ********************* */
424 * Continuation called from 'GNUNET_PEERINFO_add_peer'
426 * @param cls closure, NULL
427 * @param emsg error message, NULL on success
430 add_continuation (void *cls,
436 _("Failure adding HELLO: %s\n"),
438 tt = GNUNET_SCHEDULER_add_now (&state_machine, NULL);
443 * Parse the PUT URI given at the command line and add it to our peerinfo
446 * @param put_uri URI string to parse
447 * @return GNUNET_OK on success, GNUNET_SYSERR if the URI was invalid, GNUNET_NO on other errors
450 parse_hello_uri (const char *put_uri)
452 struct GNUNET_HELLO_Message *hello = NULL;
454 int ret = GNUNET_HELLO_parse_uri(put_uri, &my_public_key, &hello, &GPI_plugins_find);
457 /* WARNING: this adds the address from URI WITHOUT verification! */
458 if (GNUNET_OK == ret)
459 ac = GNUNET_PEERINFO_add_peer (peerinfo, hello, &add_continuation, NULL);
461 tt = GNUNET_SCHEDULER_add_now (&state_machine, NULL);
465 /* wait 1s to give peerinfo operation a chance to succeed */
466 /* FIXME: current peerinfo API sucks to require this; not to mention
467 that we get no feedback to determine if the operation actually succeeded */
472 /* ************************ Main state machine ********************* */
476 * Main state machine that goes over all options and
477 * runs the next requested function.
480 * @param tc scheduler context
483 shutdown_task (void *cls,
484 const struct GNUNET_SCHEDULER_TaskContext *tc)
486 struct PrintContext *pc;
487 struct AddressRecord *ar;
492 GNUNET_PEERINFO_add_peer_cancel (ac);
495 if (GNUNET_SCHEDULER_NO_TASK != tt)
497 GNUNET_SCHEDULER_cancel (tt);
498 tt = GNUNET_SCHEDULER_NO_TASK;
502 GNUNET_PEERINFO_iterate_cancel (pic);
505 while (NULL != (pc = pc_head))
507 GNUNET_CONTAINER_DLL_remove (pc_head,
510 for (i=0;i<pc->address_list_size;i++)
512 ar = &pc->address_list[i];
513 GNUNET_free_non_null (ar->result);
514 if (NULL != ar->atsc)
516 GNUNET_TRANSPORT_address_to_string_cancel (ar->atsc);
520 GNUNET_free_non_null (pc->address_list);
523 GPI_plugins_unload ();
524 if (NULL != peerinfo)
526 GNUNET_PEERINFO_disconnect (peerinfo);
533 * Main function that will be run by the scheduler.
536 * @param args remaining command-line arguments
537 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
538 * @param c configuration
541 run (void *cls, char *const *args, const char *cfgfile,
542 const struct GNUNET_CONFIGURATION_Handle *c)
544 struct GNUNET_CRYPTO_EccPrivateKey *priv;
548 if ( (NULL != args[0]) &&
550 (args[0] == strcasestr (args[0], "gnunet://hello/")) )
552 put_uri = GNUNET_strdup (args[0]);
558 _("Invalid command line argument `%s'\n"),
562 if (NULL == (peerinfo = GNUNET_PEERINFO_connect (cfg)))
564 FPRINTF (stderr, "%s", _("Could not access PEERINFO service. Exiting.\n"));
567 if ( (GNUNET_YES == get_self) || (GNUNET_YES == get_uri) )
569 /* load private key */
571 GNUNET_CONFIGURATION_get_value_filename (cfg, "PEER", "PRIVATE_KEY",
574 FPRINTF (stderr, _("Could not find option `%s:%s' in configuration.\n"),
575 "GNUNETD", "HOSTKEYFILE");
578 if (NULL == (priv = GNUNET_CRYPTO_ecc_key_create_from_file (fn)))
580 FPRINTF (stderr, _("Loading hostkey from `%s' failed.\n"), fn);
585 GNUNET_CRYPTO_ecc_key_get_public (priv, &my_public_key);
586 GNUNET_CRYPTO_ecc_key_free (priv);
587 GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key), &my_peer_identity.hashPubKey);
590 tt = GNUNET_SCHEDULER_add_now (&state_machine, NULL);
591 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
598 * Main state machine that goes over all options and
599 * runs the next requested function.
602 * @param tc scheduler context
605 state_machine (void *cls,
606 const struct GNUNET_SCHEDULER_TaskContext *tc)
608 tt = GNUNET_SCHEDULER_NO_TASK;
612 GPI_plugins_load (cfg);
613 if (GNUNET_SYSERR == parse_hello_uri (put_uri))
616 _("Invalid URI `%s'\n"),
618 GNUNET_SCHEDULER_shutdown ();
620 GNUNET_free (put_uri);
624 if (GNUNET_YES == get_info)
626 get_info = GNUNET_NO;
627 GPI_plugins_load (cfg);
628 pic = GNUNET_PEERINFO_iterate (peerinfo, include_friend_only, NULL,
630 &print_peer_info, NULL);
633 if (GNUNET_YES == get_self)
635 struct GNUNET_CRYPTO_HashAsciiEncoded enc;
637 get_self = GNUNET_NO;
638 GNUNET_CRYPTO_hash_to_enc (&my_peer_identity.hashPubKey, &enc);
640 printf ("%s\n", (char *) &enc);
642 printf (_("I am peer `%s'.\n"), (const char *) &enc);
644 if (GNUNET_YES == get_uri)
646 GPI_plugins_load (cfg);
647 pic = GNUNET_PEERINFO_iterate (peerinfo, include_friend_only, &my_peer_identity,
648 TIMEOUT, &print_my_uri, NULL);
652 GNUNET_SCHEDULER_shutdown ();
657 * The main function to obtain peer information.
659 * @param argc number of arguments from the command line
660 * @param argv command line arguments
661 * @return 0 ok, 1 on error
664 main (int argc, char *const *argv)
666 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
667 {'n', "numeric", NULL,
668 gettext_noop ("don't resolve host names"),
669 0, &GNUNET_GETOPT_set_one, &no_resolve},
671 gettext_noop ("output only the identity strings"),
672 0, &GNUNET_GETOPT_set_one, &be_quiet},
673 {'f', "friends", NULL,
674 gettext_noop ("include friend-only information"),
675 0, &GNUNET_GETOPT_set_one, &include_friend_only},
677 gettext_noop ("output our own identity only"),
678 0, &GNUNET_GETOPT_set_one, &get_self},
680 gettext_noop ("list all known peers"),
681 0, &GNUNET_GETOPT_set_one, &get_info},
682 {'g', "get-hello", NULL,
683 gettext_noop ("also output HELLO uri(s)"),
684 0, &GNUNET_GETOPT_set_one, &get_uri},
685 {'p', "put-hello", "HELLO",
686 gettext_noop ("add given HELLO uri to the database"),
687 1, &GNUNET_GETOPT_set_string, &put_uri},
688 GNUNET_GETOPT_OPTION_END
692 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
696 GNUNET_PROGRAM_run (argc, argv, "gnunet-peerinfo",
697 gettext_noop ("Print information about peers."),
698 options, &run, NULL)) ? 0 : 1;
699 GNUNET_free ((void*) argv);
703 /* end of gnunet-peerinfo.c */