2 This file is part of GNUnet.
3 (C) 2001, 2002, 2003, 2004, 2006, 2009, 2010 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 2, 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_peerinfo_service.h"
31 #include "gnunet_transport_service.h"
32 #include "gnunet_program_lib.h"
34 static int no_resolve;
40 static struct GNUNET_SCHEDULER_Handle *sched;
42 static const struct GNUNET_CONFIGURATION_Handle *cfg;
48 * Function to call with a human-readable format of an address
51 * @param address NULL on error, otherwise 0-terminated printable UTF-8 string
54 print_resolved_address (void *cls,
57 /* FIXME: need to buffer output from all requests and print it at
58 once, otherwise we mix results... */
63 fprintf (stderr, " %s\n", address);
68 * Iterator callback to go over all addresses.
71 * @param tname name of the transport
72 * @param expiration expiration time
73 * @param addr the address
74 * @param addrlen length of the address
75 * @return GNUNET_OK to keep the address and continue
78 print_address (void *cls,
80 struct GNUNET_TIME_Absolute expiration,
81 const void *addr, size_t addrlen)
84 GNUNET_TRANSPORT_address_lookup (sched,
90 GNUNET_TIME_UNIT_SECONDS,
91 &print_resolved_address,
99 * Print information about the peer.
100 * Currently prints the GNUNET_PeerIdentity, trust and the IP.
101 * Could of course do more (e.g. resolve via DNS).
104 print_peer_info (void *cls,
105 const struct GNUNET_PeerIdentity *peer,
106 const struct GNUNET_HELLO_Message *hello, uint32_t trust)
108 struct GNUNET_CRYPTO_HashAsciiEncoded enc;
112 GNUNET_CRYPTO_hash_to_enc (&peer->hashPubKey, &enc);
115 printf ("%s\n", (const char *) &enc);
118 printf (_("Peer `%s' with trust %8u\n"), (const char *) &enc, trust);
119 GNUNET_HELLO_iterate_addresses (hello, GNUNET_NO, &print_address, NULL);
123 * Main function that will be run by the scheduler.
126 * @param s the scheduler to use
127 * @param args remaining command-line arguments
128 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
129 * @param c configuration
133 struct GNUNET_SCHEDULER_Handle *s,
136 const struct GNUNET_CONFIGURATION_Handle *c)
138 struct GNUNET_CRYPTO_RsaPrivateKey *priv;
139 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pub;
140 struct GNUNET_PeerIdentity pid;
141 struct GNUNET_CRYPTO_HashAsciiEncoded enc;
146 if (get_self != GNUNET_YES)
148 (void) GNUNET_PEERINFO_iterate (cfg,
152 GNUNET_TIME_relative_multiply
153 (GNUNET_TIME_UNIT_SECONDS, 2),
154 &print_peer_info, NULL);
159 GNUNET_CONFIGURATION_get_value_filename (cfg,
164 _("Could not find option `%s:%s' in configuration.\n"),
169 priv = GNUNET_CRYPTO_rsa_key_create_from_file (fn);
172 fprintf (stderr, _("Loading hostkey from `%s' failed.\n"), fn);
177 GNUNET_CRYPTO_rsa_key_get_public (priv, &pub);
178 GNUNET_CRYPTO_rsa_key_free (priv);
179 GNUNET_CRYPTO_hash (&pub, sizeof (pub), &pid.hashPubKey);
180 GNUNET_CRYPTO_hash_to_enc (&pid.hashPubKey, &enc);
182 printf ("%s\n", (char *) &enc);
184 printf (_("I am peer `%s'.\n"), (const char *) &enc);
190 * gnunet-peerinfo command line options
192 static struct GNUNET_GETOPT_CommandLineOption options[] = {
193 {'n', "numeric", NULL,
194 gettext_noop ("don't resolve host names"),
195 0, &GNUNET_GETOPT_set_one, &no_resolve},
197 gettext_noop ("output only the identity strings"),
198 0, &GNUNET_GETOPT_set_one, &be_quiet},
200 gettext_noop ("output our own identity only"),
201 0, &GNUNET_GETOPT_set_one, &get_self},
202 GNUNET_GETOPT_OPTION_END
206 * The main function to obtain peer information.
208 * @param argc number of arguments from the command line
209 * @param argv command line arguments
210 * @return 0 ok, 1 on error
213 main (int argc, char *const *argv)
216 GNUNET_PROGRAM_run (argc,
219 gettext_noop ("Print information about peers."),
220 options, &run, NULL)) ? 0 : 1;
223 /* end of gnunet-peerinfo.c */