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 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_peerinfo_service.h"
31 #include "gnunet_transport_service.h"
32 #include "gnunet_program_lib.h"
34 static int no_resolve;
40 static struct GNUNET_PEERINFO_Handle *peerinfo;
42 static const struct GNUNET_CONFIGURATION_Handle *cfg;
46 struct GNUNET_PeerIdentity peer;
48 unsigned int num_addresses;
54 dump_pc (struct PrintContext *pc)
56 struct GNUNET_CRYPTO_HashAsciiEncoded enc;
59 GNUNET_CRYPTO_hash_to_enc (&pc->peer.hashPubKey, &enc);
60 printf (_("Peer `%s'\n"), (const char *) &enc);
61 for (i = 0; i < pc->num_addresses; i++)
63 printf ("\t%s\n", pc->address_list[i]);
64 GNUNET_free (pc->address_list[i]);
67 GNUNET_array_grow (pc->address_list, pc->num_addresses, 0);
73 * Function to call with a human-readable format of an address
76 * @param address NULL on error, otherwise 0-terminated printable UTF-8 string
79 process_resolved_address (void *cls, const char *address)
81 struct PrintContext *pc = cls;
90 GNUNET_array_append (pc->address_list, pc->num_addresses,
91 GNUNET_strdup (address));
96 * Iterator callback to go over all addresses.
99 * @param tname name of the transport
100 * @param expiration expiration time
101 * @param addr the address
102 * @param addrlen length of the address
103 * @return GNUNET_OK to keep the address and continue
106 count_address (void *cls, const char *tname,
107 struct GNUNET_TIME_Absolute expiration, const void *addr,
110 struct PrintContext *pc = cls;
118 * Iterator callback to go over all addresses.
121 * @param tname name of the transport
122 * @param expiration expiration time
123 * @param addr the address
124 * @param addrlen length of the address
125 * @return GNUNET_OK to keep the address and continue
128 print_address (void *cls, const char *tname,
129 struct GNUNET_TIME_Absolute expiration, const void *addr,
132 struct PrintContext *pc = cls;
134 GNUNET_TRANSPORT_address_lookup (cfg, addr, addrlen, no_resolve, tname,
135 GNUNET_TIME_relative_multiply
136 (GNUNET_TIME_UNIT_SECONDS, 10),
137 &process_resolved_address, pc);
143 * Print information about the peer.
144 * Currently prints the GNUNET_PeerIdentity and the IP.
145 * Could of course do more (e.g. resolve via DNS).
148 print_peer_info (void *cls, const struct GNUNET_PeerIdentity *peer,
149 const struct GNUNET_HELLO_Message *hello, const char *err_msg)
151 struct GNUNET_CRYPTO_HashAsciiEncoded enc;
152 struct PrintContext *pc;
157 fprintf (stderr, _("Error in communication with PEERINFO service\n"));
158 GNUNET_PEERINFO_disconnect (peerinfo);
161 if ((be_quiet) || (NULL == hello))
163 GNUNET_CRYPTO_hash_to_enc (&peer->hashPubKey, &enc);
164 printf ("%s\n", (const char *) &enc);
167 pc = GNUNET_malloc (sizeof (struct PrintContext));
169 GNUNET_HELLO_iterate_addresses (hello, GNUNET_NO, &count_address, pc);
175 GNUNET_HELLO_iterate_addresses (hello, GNUNET_NO, &print_address, pc);
180 * Main function that will be run by the scheduler.
183 * @param args remaining command-line arguments
184 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
185 * @param c configuration
188 run (void *cls, char *const *args, const char *cfgfile,
189 const struct GNUNET_CONFIGURATION_Handle *c)
191 struct GNUNET_CRYPTO_RsaPrivateKey *priv;
192 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pub;
193 struct GNUNET_PeerIdentity pid;
194 struct GNUNET_CRYPTO_HashAsciiEncoded enc;
200 fprintf (stderr, _("Invalid command line argument `%s'\n"), args[0]);
203 if (get_self != GNUNET_YES)
205 peerinfo = GNUNET_PEERINFO_connect (cfg);
206 if (peerinfo == NULL)
208 fprintf (stderr, _("Could not access PEERINFO service. Exiting.\n"));
211 GNUNET_PEERINFO_iterate (peerinfo, NULL,
212 GNUNET_TIME_relative_multiply
213 (GNUNET_TIME_UNIT_SECONDS, 5), &print_peer_info,
219 GNUNET_CONFIGURATION_get_value_filename (cfg, "GNUNETD", "HOSTKEY",
222 fprintf (stderr, _("Could not find option `%s:%s' in configuration.\n"),
223 "GNUNETD", "HOSTKEYFILE");
226 priv = GNUNET_CRYPTO_rsa_key_create_from_file (fn);
229 fprintf (stderr, _("Loading hostkey from `%s' failed.\n"), fn);
234 GNUNET_CRYPTO_rsa_key_get_public (priv, &pub);
235 GNUNET_CRYPTO_rsa_key_free (priv);
236 GNUNET_CRYPTO_hash (&pub, sizeof (pub), &pid.hashPubKey);
237 GNUNET_CRYPTO_hash_to_enc (&pid.hashPubKey, &enc);
239 printf ("%s\n", (char *) &enc);
241 printf (_("I am peer `%s'.\n"), (const char *) &enc);
247 * The main function to obtain peer information.
249 * @param argc number of arguments from the command line
250 * @param argv command line arguments
251 * @return 0 ok, 1 on error
254 main (int argc, char *const *argv)
256 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
257 {'n', "numeric", NULL,
258 gettext_noop ("don't resolve host names"),
259 0, &GNUNET_GETOPT_set_one, &no_resolve},
261 gettext_noop ("output only the identity strings"),
262 0, &GNUNET_GETOPT_set_one, &be_quiet},
264 gettext_noop ("output our own identity only"),
265 0, &GNUNET_GETOPT_set_one, &get_self},
266 GNUNET_GETOPT_OPTION_END
269 GNUNET_PROGRAM_run (argc, argv, "gnunet-peerinfo",
270 gettext_noop ("Print information about peers."),
271 options, &run, NULL)) ? 0 : 1;
274 /* end of gnunet-peerinfo.c */