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"
33 #include "gnunet_transport_plugin.h"
34 #include "../transport/gnunet-service-transport_plugins.h"
36 static int no_resolve;
46 static struct GNUNET_PEERINFO_Handle *peerinfo;
49 * Configuration handle.
51 const struct GNUNET_CONFIGURATION_Handle *GST_cfg;
56 struct GNUNET_STATISTICS_Handle *GST_stats;
59 * Configuration handle.
61 struct GNUNET_PeerIdentity GST_my_identity;
63 struct GNUNET_MessageHeader *our_hello = NULL;
65 static const struct GNUNET_CONFIGURATION_Handle *cfg;
69 struct GNUNET_PeerIdentity peer;
71 unsigned int num_addresses;
78 * Obtain this peers HELLO message.
80 * @return our HELLO message
82 const struct GNUNET_MessageHeader *
85 return (struct GNUNET_MessageHeader *) our_hello;
89 dump_pc (struct PrintContext *pc)
91 struct GNUNET_CRYPTO_HashAsciiEncoded enc;
94 GNUNET_CRYPTO_hash_to_enc (&pc->peer.hashPubKey, &enc);
95 printf (_("Peer `%s'\n"), (const char *) &enc);
96 for (i = 0; i < pc->num_addresses; i++)
98 printf ("\t%s\n", pc->address_list[i]);
99 GNUNET_free (pc->address_list[i]);
102 GNUNET_array_grow (pc->address_list, pc->num_addresses, 0);
108 * Function to call with a human-readable format of an address
111 * @param address NULL on error, otherwise 0-terminated printable UTF-8 string
114 process_resolved_address (void *cls, const char *address)
116 struct PrintContext *pc = cls;
125 GNUNET_array_append (pc->address_list, pc->num_addresses,
126 GNUNET_strdup (address));
131 * Iterator callback to go over all addresses.
134 * @param address the address
135 * @param expiration expiration time
136 * @return GNUNET_OK to keep the address and continue
139 count_address (void *cls, const struct GNUNET_HELLO_Address *address,
140 struct GNUNET_TIME_Absolute expiration)
142 struct PrintContext *pc = cls;
150 * Iterator callback to go over all addresses.
153 * @param address the address
154 * @param expiration expiration time
155 * @return GNUNET_OK to keep the address and continue
158 print_address (void *cls, const struct GNUNET_HELLO_Address *address,
159 struct GNUNET_TIME_Absolute expiration)
161 struct PrintContext *pc = cls;
163 GNUNET_TRANSPORT_address_to_string (cfg, address, no_resolve,
164 GNUNET_TIME_relative_multiply
165 (GNUNET_TIME_UNIT_SECONDS, 10),
166 &process_resolved_address, pc);
172 * Print information about the peer.
173 * Currently prints the GNUNET_PeerIdentity and the IP.
174 * Could of course do more (e.g. resolve via DNS).
177 print_peer_info (void *cls, const struct GNUNET_PeerIdentity *peer,
178 const struct GNUNET_HELLO_Message *hello, const char *err_msg)
180 struct GNUNET_CRYPTO_HashAsciiEncoded enc;
181 struct PrintContext *pc;
186 FPRINTF (stderr, "%s", _("Error in communication with PEERINFO service\n"));
187 GNUNET_PEERINFO_disconnect (peerinfo);
188 GST_plugins_unload ();
189 GNUNET_STATISTICS_destroy (GST_stats, GNUNET_NO);
192 if ((be_quiet) || (NULL == hello))
194 GNUNET_CRYPTO_hash_to_enc (&peer->hashPubKey, &enc);
195 printf ("%s\n", (const char *) &enc);
198 pc = GNUNET_malloc (sizeof (struct PrintContext));
200 GNUNET_HELLO_iterate_addresses (hello, GNUNET_NO, &count_address, pc);
206 GNUNET_HELLO_iterate_addresses (hello, GNUNET_NO, &print_address, pc);
210 compose_uri (void *cls, const struct GNUNET_HELLO_Address *address,
211 struct GNUNET_TIME_Absolute expiration)
213 struct PrintContext *pc = cls;
214 struct GNUNET_TRANSPORT_PluginFunctions *papi;
215 static const char *addr;
217 papi = GST_plugins_find (address->transport_name);
220 /* Not an error - we might just not have the right plugin. */
224 addr = papi->address_to_string (papi->cls, address->address, address->address_length);
227 ssize_t l = strlen (addr);
233 seconds = expiration.abs_value / 1000;
234 t = gmtime(&seconds);
235 pc->uri = GNUNET_realloc (pc->uri, pc->uri_len + 1 + 14 + 1 + strlen (address->transport_name) + 1 + l + 1 /* 0 */);
236 s = sprintf (&pc->uri[pc->uri_len], "!%04u%02u%02u%02u%02u%02u!%s!%s",
237 t->tm_year, t->tm_mon, t->tm_mday, t->tm_hour, t->tm_min, t->tm_sec,
238 address->transport_name, addr);
247 * Print information about the peer.
250 print_my_uri (void *cls, const struct GNUNET_PeerIdentity *peer,
251 const struct GNUNET_HELLO_Message *hello, const char *err_msg)
253 struct GNUNET_CRYPTO_HashAsciiEncoded enc;
254 struct PrintContext *pc = cls;
259 FPRINTF (stderr, "%s", _("Error in communication with PEERINFO service\n"));
260 GNUNET_PEERINFO_disconnect (peerinfo);
261 GST_plugins_unload ();
262 GNUNET_STATISTICS_destroy (GST_stats, GNUNET_NO);
265 if ((be_quiet) || (NULL == hello))
267 GNUNET_CRYPTO_hash_to_enc (&peer->hashPubKey, &enc);
268 printf ("%s\n", (const char *) &enc);
269 if (be_quiet && get_uri != GNUNET_YES)
273 GNUNET_HELLO_iterate_addresses (hello, GNUNET_NO, &count_address, pc);
274 GNUNET_HELLO_iterate_addresses (hello, GNUNET_NO, &compose_uri, pc);
275 printf ("%s\n", pc->uri);
276 GNUNET_free (pc->uri);
280 struct GNUNET_PEERINFO_HelloAddressParsingContext
288 add_addr_to_hello (void *cls, size_t max, void *buffer)
290 struct tm expiration_time;
293 time_t expiration_seconds;
294 struct GNUNET_TIME_Absolute expire;
296 struct GNUNET_PEERINFO_HelloAddressParsingContext *ctx = cls;
298 struct GNUNET_TRANSPORT_PluginFunctions *papi;
303 if (ctx->pos - ctx->tmp == ctx->tmp_len)
306 /* Parsed past the end of string, OR wrong format */
307 if ((ctx->pos - ctx->tmp > ctx->tmp_len) || ctx->pos[0] != '!')
313 /* Not enough bytes (3 for three '!', 14 for expiration date, and
314 * at least 1 for type and 1 for address (1-byte long address is a joke,
315 * but it is not completely unrealistic. Zero-length address is.
317 if (ctx->tmp_len - (ctx->pos - ctx->tmp) < 1 /*!*/ * 3 + 14 + /* at least */ 2)
322 /* Go past the first '!', now we're on expiration date */
324 /* Its length is known, so check for the next '!' right away */
325 if (ctx->pos[14] != '!')
331 memset (&expiration_time, 0, sizeof (struct tm));
333 /* This is FAR more strict than strptime(ctx->pos, "%Y%m%d%H%M%S", ...); */
334 /* FIXME: make it a separate function, since expiration is specified to every address */
335 #define GETNDIGITS(n,cond) \
336 strncpy (buf, &ctx->pos[0], n); \
339 l = strtol (buf, NULL, 10); \
340 if (errno != 0 || cond) \
347 GETNDIGITS (4, l < 1900)
348 expiration_time.tm_year = l - 1900;
350 GETNDIGITS (2, l < 1 || l > 12)
351 expiration_time.tm_mon = l;
353 GETNDIGITS (2, l < 1 || l > 31)
354 expiration_time.tm_mday = l;
356 GETNDIGITS (2, l < 0 || l > 23)
357 expiration_time.tm_hour = l;
359 GETNDIGITS (2, l < 0 || l > 59)
360 expiration_time.tm_min = l;
362 /* 60 - with a leap second */
363 GETNDIGITS (2, l < 0 || l > 60)
364 expiration_time.tm_sec = l;
366 expiration_time.tm_isdst = -1;
370 expiration_seconds = mktime (&expiration_time);
371 if (expiration_seconds == (time_t) -1)
376 expire.abs_value = expiration_seconds * 1000;
378 /* Now we're at '!', advance to the transport type */
381 /* Find the next '!' that separates transport type from
384 exp1 = strstr (ctx->pos, "!");
390 /* We need it 0-terminated */
392 /* Find the '!' that separates address from the next record.
393 * It might not be there, if this is the last record.
395 exp2 = strstr (&exp1[1], "!");
397 exp2 = &ctx->tmp[ctx->tmp_len];
399 papi = GST_plugins_find (ctx->pos);
402 /* Not an error - we might just not have the right plugin.
403 * Skip this part, advance to the next one and recurse.
404 * But only if this is not the end of string.
407 if (ctx->pos - ctx->tmp >= ctx->tmp_len)
409 return add_addr_to_hello (cls, max, buffer);
412 if (GNUNET_OK == papi->string_to_address (papi->cls, &exp1[1], exp2 - &exp1[1], &addr, &addr_len))
414 struct GNUNET_HELLO_Address address;
417 /* address.peer is unset - not used by add_address() */
418 address.address_length = addr_len;
419 address.address = addr;
420 address.transport_name = ctx->pos;
421 ret = GNUNET_HELLO_add_address (&address, expire, buffer, max);
430 parse_hello (const struct GNUNET_CONFIGURATION_Handle *c,
434 char *scheme_part = NULL;
435 char *path_part = NULL;
437 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pub;
439 struct GNUNET_HELLO_Message *hello;
440 struct GNUNET_PEERINFO_HelloAddressParsingContext ctx;
442 r = GNUNET_STRINGS_parse_uri (put_uri, &scheme_part, (const char **) &path_part);
445 if (scheme_part == NULL || strcmp (scheme_part, "gnunet://") != 0)
447 GNUNET_free_non_null (scheme_part);
450 GNUNET_free (scheme_part);
452 if (strncmp (path_part, "hello/", 6) != 0)
455 path_part = &path_part[6];
456 ctx.tmp = GNUNET_strdup (path_part);
457 ctx.tmp_len = strlen (path_part);
458 exc = strstr (ctx.tmp, "!");
460 exc = ctx.pos + ctx.tmp_len;
463 std_result = GNUNET_STRINGS_string_to_data (ctx.tmp, exc - ctx.tmp,
464 (unsigned char *) &pub, sizeof (pub));
465 if (std_result != GNUNET_OK)
467 GNUNET_free (ctx.tmp);
471 hello = GNUNET_HELLO_create (&pub, add_addr_to_hello, &ctx);
472 GNUNET_free (ctx.tmp);
474 /* WARNING: this adds the address from URI WITHOUT verification! */
475 GNUNET_PEERINFO_add_peer (peerinfo, hello);
478 static struct GNUNET_TIME_Relative
479 receive_stub (void *cls, const struct GNUNET_PeerIdentity *peer,
480 const struct GNUNET_MessageHeader *message,
481 const struct GNUNET_ATS_Information *ats, uint32_t ats_count,
482 struct Session *session, const char *sender_address,
483 uint16_t sender_address_len)
485 struct GNUNET_TIME_Relative t;
491 address_notification_stub (void *cls, int add_remove,
492 const void *addr, size_t addrlen)
497 session_end_stub (void *cls, const struct GNUNET_PeerIdentity *peer,
498 struct Session * session)
502 static const struct GNUNET_ATS_Information
503 address_to_type_stub (void *cls, const struct sockaddr *addr,
506 struct GNUNET_ATS_Information t;
514 * Main function that will be run by the scheduler.
517 * @param args remaining command-line arguments
518 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
519 * @param c configuration
522 run (void *cls, char *const *args, const char *cfgfile,
523 const struct GNUNET_CONFIGURATION_Handle *c)
525 struct GNUNET_CRYPTO_RsaPrivateKey *priv;
526 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pub;
527 struct GNUNET_PeerIdentity pid;
528 struct GNUNET_CRYPTO_HashAsciiEncoded enc;
534 FPRINTF (stderr, _("Invalid command line argument `%s'\n"), args[0]);
537 if (put_uri != NULL && get_uri == GNUNET_YES)
539 FPRINTF (stderr, "%s", _("--put-uri and --get-uri are mutually exclusive\n"));
542 if (put_uri != NULL || get_uri == GNUNET_YES)
544 peerinfo = GNUNET_PEERINFO_connect (cfg);
545 if (peerinfo == NULL)
547 FPRINTF (stderr, "%s", _("Could not access PEERINFO service. Exiting.\n"));
551 GST_stats = GNUNET_STATISTICS_create ("transport", c);
552 /* FIXME: shouldn't we free GST_stats somewhere? */
553 GST_plugins_load (receive_stub, address_notification_stub,
554 session_end_stub, address_to_type_stub);
558 parse_hello (c, put_uri);
559 GST_plugins_unload ();
560 GNUNET_STATISTICS_destroy (GST_stats, GNUNET_NO);
563 if (get_self != GNUNET_YES)
565 GNUNET_PEERINFO_iterate (peerinfo, NULL,
566 GNUNET_TIME_relative_multiply
567 (GNUNET_TIME_UNIT_SECONDS, 5), &print_peer_info,
573 GNUNET_CONFIGURATION_get_value_filename (cfg, "GNUNETD", "HOSTKEY",
576 FPRINTF (stderr, _("Could not find option `%s:%s' in configuration.\n"),
577 "GNUNETD", "HOSTKEYFILE");
580 priv = GNUNET_CRYPTO_rsa_key_create_from_file (fn);
583 FPRINTF (stderr, _("Loading hostkey from `%s' failed.\n"), fn);
588 GNUNET_CRYPTO_rsa_key_get_public (priv, &pub);
589 GNUNET_CRYPTO_rsa_key_free (priv);
590 GNUNET_CRYPTO_hash (&pub, sizeof (pub), &pid.hashPubKey);
591 GNUNET_CRYPTO_hash_to_enc (&pid.hashPubKey, &enc);
593 printf ("%s\n", (char *) &enc);
595 printf (_("I am peer `%s'.\n"), (const char *) &enc);
596 if (get_uri == GNUNET_YES)
598 struct PrintContext *pc;
601 pc = GNUNET_malloc (sizeof (struct PrintContext));
602 pkey = GNUNET_CRYPTO_rsa_public_key_to_string (&pub);
603 pl = strlen ("gnunet://hello/");
604 l = strlen (pkey) + pl;
605 pc->uri = GNUNET_malloc (l + 1);
606 strcpy (pc->uri, "gnunet://hello/");
607 strcpy (&pc->uri[pl], pkey);
609 GNUNET_PEERINFO_iterate (peerinfo, &pid,
610 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5),
618 * The main function to obtain peer information.
620 * @param argc number of arguments from the command line
621 * @param argv command line arguments
622 * @return 0 ok, 1 on error
625 main (int argc, char *const *argv)
627 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
628 {'n', "numeric", NULL,
629 gettext_noop ("don't resolve host names"),
630 0, &GNUNET_GETOPT_set_one, &no_resolve},
632 gettext_noop ("output only the identity strings"),
633 0, &GNUNET_GETOPT_set_one, &be_quiet},
635 gettext_noop ("output our own identity only"),
636 0, &GNUNET_GETOPT_set_one, &get_self},
637 {'g', "get-hello", NULL,
638 gettext_noop ("also output HELLO uri(s)"),
639 0, &GNUNET_GETOPT_set_one, &get_uri},
640 {'p', "put-hello", "HELLO",
641 gettext_noop ("add given HELLO uri to the database"),
642 1, &GNUNET_GETOPT_set_string, &put_uri},
643 GNUNET_GETOPT_OPTION_END
646 GNUNET_PROGRAM_run (argc, argv, "gnunet-peerinfo",
647 gettext_noop ("Print information about peers."),
648 options, &run, NULL)) ? 0 : 1;
651 /* end of gnunet-peerinfo.c */