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)
275 GNUNET_HELLO_iterate_addresses (hello, GNUNET_NO, &count_address, pc);
276 GNUNET_HELLO_iterate_addresses (hello, GNUNET_NO, &compose_uri, pc);
278 printf ("%s\n", pc->uri);
281 struct GNUNET_PEERINFO_HelloAddressParsingContext
289 add_addr_to_hello (void *cls, size_t max, void *buffer)
291 struct tm expiration_time;
294 time_t expiration_seconds;
295 struct GNUNET_TIME_Absolute expire;
297 struct GNUNET_PEERINFO_HelloAddressParsingContext *ctx = cls;
299 struct GNUNET_TRANSPORT_PluginFunctions *papi;
304 if (ctx->pos - ctx->tmp == ctx->tmp_len)
307 /* Parsed past the end of string, OR wrong format */
308 if ((ctx->pos - ctx->tmp > ctx->tmp_len) || ctx->pos[0] != '!')
314 /* Not enough bytes (3 for three '!', 14 for expiration date, and
315 * at least 1 for type and 1 for address (1-byte long address is a joke,
316 * but it is not completely unrealistic. Zero-length address is.
318 if (ctx->tmp_len - (ctx->pos - ctx->tmp) < 1 /*!*/ * 3 + 14 + /* at least */ 2)
323 /* Go past the first '!', now we're on expiration date */
325 /* Its length is known, so check for the next '!' right away */
326 if (ctx->pos[14] != '!')
332 memset (&expiration_time, 0, sizeof (struct tm));
334 /* This is FAR more strict than strptime(ctx->pos, "%Y%m%d%H%M%S", ...); */
335 /* FIXME: make it a separate function, since expiration is specified to every address */
336 #define GETNDIGITS(n,cond) \
337 strncpy (buf, &ctx->pos[0], n); \
340 l = strtol (buf, NULL, 10); \
341 if (errno != 0 || cond) \
348 GETNDIGITS (4, l < 1900)
349 expiration_time.tm_year = l - 1900;
351 GETNDIGITS (2, l < 1 || l > 12)
352 expiration_time.tm_mon = l;
354 GETNDIGITS (2, l < 1 || l > 31)
355 expiration_time.tm_mday = l;
357 GETNDIGITS (2, l < 0 || l > 23)
358 expiration_time.tm_hour = l;
360 GETNDIGITS (2, l < 0 || l > 59)
361 expiration_time.tm_min = l;
363 /* 60 - with a leap second */
364 GETNDIGITS (2, l < 0 || l > 60)
365 expiration_time.tm_sec = l;
367 expiration_time.tm_isdst = -1;
371 expiration_seconds = mktime (&expiration_time);
372 if (expiration_seconds == (time_t) -1)
377 expire.abs_value = expiration_seconds * 1000;
379 /* Now we're at '!', advance to the transport type */
382 /* Find the next '!' that separates transport type from
385 exp1 = strstr (ctx->pos, "!");
391 /* We need it 0-terminated */
393 /* Find the '!' that separates address from the next record.
394 * It might not be there, if this is the last record.
396 exp2 = strstr (&exp1[1], "!");
398 exp2 = &ctx->tmp[ctx->tmp_len];
400 papi = GST_plugins_find (ctx->pos);
403 /* Not an error - we might just not have the right plugin.
404 * Skip this part, advance to the next one and recurse.
405 * But only if this is not the end of string.
408 if (ctx->pos - ctx->tmp >= ctx->tmp_len)
410 return add_addr_to_hello (cls, max, buffer);
413 if (GNUNET_OK == papi->string_to_address (papi->cls, &exp1[1], exp2 - &exp1[1], &addr, &addr_len))
415 struct GNUNET_HELLO_Address address;
418 /* address.peer is unset - not used by add_address() */
419 address.address_length = addr_len;
420 address.address = addr;
421 address.transport_name = ctx->pos;
422 ret = GNUNET_HELLO_add_address (&address, expire, buffer, max);
431 parse_hello (const struct GNUNET_CONFIGURATION_Handle *c,
435 char *scheme_part = NULL;
436 char *path_part = NULL;
438 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pub;
440 struct GNUNET_HELLO_Message *hello;
441 struct GNUNET_PEERINFO_HelloAddressParsingContext ctx;
443 r = GNUNET_STRINGS_parse_uri (put_uri, &scheme_part, (const char **) &path_part);
446 if (scheme_part == NULL || strcmp (scheme_part, "gnunet://") != 0)
448 GNUNET_free_non_null (scheme_part);
451 GNUNET_free (scheme_part);
453 if (strncmp (path_part, "hello/", 6) != 0)
456 path_part = &path_part[6];
457 ctx.tmp = GNUNET_strdup (path_part);
458 ctx.tmp_len = strlen (path_part);
459 exc = strstr (ctx.tmp, "!");
461 exc = ctx.tmp + ctx.tmp_len;
464 std_result = GNUNET_STRINGS_string_to_data (ctx.tmp, exc - ctx.tmp,
465 (unsigned char *) &pub, sizeof (pub));
466 if (std_result != GNUNET_OK)
468 GNUNET_free (ctx.tmp);
472 hello = GNUNET_HELLO_create (&pub, add_addr_to_hello, &ctx);
473 GNUNET_free (ctx.tmp);
475 /* WARNING: this adds the address from URI WITHOUT verification! */
476 GNUNET_PEERINFO_add_peer (peerinfo, hello);
480 static struct GNUNET_TIME_Relative
481 receive_stub (void *cls, const struct GNUNET_PeerIdentity *peer,
482 const struct GNUNET_MessageHeader *message,
483 const struct GNUNET_ATS_Information *ats, uint32_t ats_count,
484 struct Session *session, const char *sender_address,
485 uint16_t sender_address_len)
487 struct GNUNET_TIME_Relative t;
493 address_notification_stub (void *cls, int add_remove,
494 const void *addr, size_t addrlen)
499 session_end_stub (void *cls, const struct GNUNET_PeerIdentity *peer,
500 struct Session * session)
504 static const struct GNUNET_ATS_Information
505 address_to_type_stub (void *cls, const struct sockaddr *addr,
508 struct GNUNET_ATS_Information t;
516 * Main function that will be run by the scheduler.
519 * @param args remaining command-line arguments
520 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
521 * @param c configuration
524 run (void *cls, char *const *args, const char *cfgfile,
525 const struct GNUNET_CONFIGURATION_Handle *c)
527 struct GNUNET_CRYPTO_RsaPrivateKey *priv;
528 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pub;
529 struct GNUNET_PeerIdentity pid;
530 struct GNUNET_CRYPTO_HashAsciiEncoded enc;
536 FPRINTF (stderr, _("Invalid command line argument `%s'\n"), args[0]);
539 if (put_uri != NULL && get_uri == GNUNET_YES)
541 FPRINTF (stderr, "%s", _("--put-uri and --get-uri are mutually exclusive\n"));
544 if (put_uri != NULL || get_uri == GNUNET_YES || get_self != GNUNET_YES)
546 peerinfo = GNUNET_PEERINFO_connect (cfg);
547 if (peerinfo == NULL)
549 FPRINTF (stderr, "%s", _("Could not access PEERINFO service. Exiting.\n"));
553 GST_stats = GNUNET_STATISTICS_create ("transport", c);
554 /* FIXME: shouldn't we free GST_stats somewhere? */
555 GST_plugins_load (receive_stub, address_notification_stub,
556 session_end_stub, address_to_type_stub);
560 parse_hello (c, put_uri);
561 GST_plugins_unload ();
562 GNUNET_STATISTICS_destroy (GST_stats, GNUNET_NO);
565 if (get_self != GNUNET_YES)
567 GNUNET_PEERINFO_iterate (peerinfo, NULL,
568 GNUNET_TIME_relative_multiply
569 (GNUNET_TIME_UNIT_SECONDS, 5), &print_peer_info,
575 GNUNET_CONFIGURATION_get_value_filename (cfg, "GNUNETD", "HOSTKEY",
578 FPRINTF (stderr, _("Could not find option `%s:%s' in configuration.\n"),
579 "GNUNETD", "HOSTKEYFILE");
582 priv = GNUNET_CRYPTO_rsa_key_create_from_file (fn);
585 FPRINTF (stderr, _("Loading hostkey from `%s' failed.\n"), fn);
590 GNUNET_CRYPTO_rsa_key_get_public (priv, &pub);
591 GNUNET_CRYPTO_rsa_key_free (priv);
592 GNUNET_CRYPTO_hash (&pub, sizeof (pub), &pid.hashPubKey);
593 GNUNET_CRYPTO_hash_to_enc (&pid.hashPubKey, &enc);
595 printf ("%s\n", (char *) &enc);
597 printf (_("I am peer `%s'.\n"), (const char *) &enc);
598 if (get_uri == GNUNET_YES)
600 struct PrintContext *pc;
604 pc = GNUNET_malloc (sizeof (struct PrintContext));
605 pkey = GNUNET_CRYPTO_rsa_public_key_to_string (&pub);
606 pl = strlen ("gnunet://hello/");
607 l = strlen (pkey) + pl;
608 pc->uri = GNUNET_malloc (l + 1);
609 strcpy (pc->uri, "gnunet://hello/");
610 strcpy (&pc->uri[pl], pkey);
612 GNUNET_PEERINFO_iterate (peerinfo, &pid,
613 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5),
615 GNUNET_free (pc->uri);
623 * The main function to obtain peer information.
625 * @param argc number of arguments from the command line
626 * @param argv command line arguments
627 * @return 0 ok, 1 on error
630 main (int argc, char *const *argv)
632 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
633 {'n', "numeric", NULL,
634 gettext_noop ("don't resolve host names"),
635 0, &GNUNET_GETOPT_set_one, &no_resolve},
637 gettext_noop ("output only the identity strings"),
638 0, &GNUNET_GETOPT_set_one, &be_quiet},
640 gettext_noop ("output our own identity only"),
641 0, &GNUNET_GETOPT_set_one, &get_self},
642 {'g', "get-hello", NULL,
643 gettext_noop ("also output HELLO uri(s)"),
644 0, &GNUNET_GETOPT_set_one, &get_uri},
645 {'p', "put-hello", "HELLO",
646 gettext_noop ("add given HELLO uri to the database"),
647 1, &GNUNET_GETOPT_set_string, &put_uri},
648 GNUNET_GETOPT_OPTION_END
651 GNUNET_PROGRAM_run (argc, argv, "gnunet-peerinfo",
652 gettext_noop ("Print information about peers."),
653 options, &run, NULL)) ? 0 : 1;
656 /* end of gnunet-peerinfo.c */