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 "gnunet-peerinfo_plugins.h"
38 * Structure we use to collect printable address information.
43 * Identity of the peer.
45 struct GNUNET_PeerIdentity peer;
48 * List of printable addresses.
53 * Number of addresses in 'address_list'.
55 unsigned int num_addresses;
58 * Current offset in 'address_list'
63 * URI (FIXME: overloaded struct!)
68 * Length of 'uri' (FIXME: not nice)
77 struct GNUNET_PEERINFO_HelloAddressParsingContext
99 static int no_resolve;
124 static char *put_uri;
127 * Handle to peerinfo service.
129 static struct GNUNET_PEERINFO_Handle *peerinfo;
132 * Configuration handle.
134 static const struct GNUNET_CONFIGURATION_Handle *cfg;
137 * Main state machine task (if active).
139 static GNUNET_SCHEDULER_TaskIdentifier tt;
142 * Current iterator context (if active, otherwise NULL).
144 static struct GNUNET_PEERINFO_IteratorContext *pic;
147 * Current address-to-string context (if active, otherwise NULL).
149 static struct GNUNET_TRANSPORT_AddressToStringContext *atsc;
154 static struct GNUNET_PeerIdentity my_peer_identity;
159 static struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded my_public_key;
163 * Main state machine that goes over all options and
164 * runs the next requested function.
170 state_machine (void *cls,
171 const struct GNUNET_SCHEDULER_TaskContext *tc);
174 /* ********************* 'get_info' ******************* */
177 * Print the collected address information to the console and free 'pc'.
179 * @param pc printing context
182 dump_pc (struct PrintContext *pc)
184 struct GNUNET_CRYPTO_HashAsciiEncoded enc;
187 GNUNET_CRYPTO_hash_to_enc (&pc->peer.hashPubKey, &enc);
188 printf (_("Peer `%s'\n"), (const char *) &enc);
189 for (i = 0; i < pc->num_addresses; i++)
191 printf ("\t%s\n", pc->address_list[i]);
192 GNUNET_free (pc->address_list[i]);
195 GNUNET_array_grow (pc->address_list, pc->num_addresses, 0);
201 * Function to call with a human-readable format of an address
204 * @param address NULL on error, otherwise 0-terminated printable UTF-8 string
207 process_resolved_address (void *cls, const char *address)
209 struct PrintContext *pc = cls;
219 GNUNET_array_append (pc->address_list, pc->num_addresses,
220 GNUNET_strdup (address));
225 * Iterator callback to go over all addresses.
228 * @param address the address
229 * @param expiration expiration time
230 * @return GNUNET_OK to keep the address and continue
233 count_address (void *cls, const struct GNUNET_HELLO_Address *address,
234 struct GNUNET_TIME_Absolute expiration)
236 struct PrintContext *pc = cls;
244 * Iterator callback to go over all addresses.
247 * @param address the address
248 * @param expiration expiration time
249 * @return GNUNET_OK to keep the address and continue
252 print_address (void *cls, const struct GNUNET_HELLO_Address *address,
253 struct GNUNET_TIME_Absolute expiration)
255 struct PrintContext *pc = cls;
257 // FIXME: this is called many times in parallel!
258 atsc = GNUNET_TRANSPORT_address_to_string (cfg, address, no_resolve,
259 GNUNET_TIME_relative_multiply
260 (GNUNET_TIME_UNIT_SECONDS, 10),
261 &process_resolved_address, pc);
267 * Print information about the peer.
268 * Currently prints the GNUNET_PeerIdentity and the IP.
269 * Could of course do more (e.g. resolve via DNS).
272 print_peer_info (void *cls, const struct GNUNET_PeerIdentity *peer,
273 const struct GNUNET_HELLO_Message *hello, const char *err_msg)
275 struct GNUNET_CRYPTO_HashAsciiEncoded enc;
276 struct PrintContext *pc;
281 FPRINTF (stderr, "%s", _("Error in communication with PEERINFO service\n"));
282 // FIXME: this doesn't mean we're fully done with the printing!
283 // (as the a2s calls happen asynchronously!)
284 tt = GNUNET_SCHEDULER_add_now (&state_machine, NULL);
287 if ((be_quiet) || (NULL == hello))
289 GNUNET_CRYPTO_hash_to_enc (&peer->hashPubKey, &enc);
290 printf ("%s\n", (const char *) &enc);
293 pc = GNUNET_malloc (sizeof (struct PrintContext));
295 GNUNET_HELLO_iterate_addresses (hello, GNUNET_NO, &count_address, pc);
301 GNUNET_HELLO_iterate_addresses (hello, GNUNET_NO, &print_address, pc);
316 compose_uri (void *cls, const struct GNUNET_HELLO_Address *address,
317 struct GNUNET_TIME_Absolute expiration)
319 struct PrintContext *pc = cls;
320 struct GNUNET_TRANSPORT_PluginFunctions *papi;
321 static const char *addr;
323 papi = GPI_plugins_find (address->transport_name);
326 /* Not an error - we might just not have the right plugin. */
330 addr = papi->address_to_string (papi->cls, address->address, address->address_length);
333 ssize_t l = strlen (addr);
339 seconds = expiration.abs_value / 1000;
340 t = gmtime(&seconds);
341 pc->uri = GNUNET_realloc (pc->uri, pc->uri_len + 1 + 14 + 1 + strlen (address->transport_name) + 1 + l + 1 /* 0 */);
342 s = sprintf (&pc->uri[pc->uri_len], "!%04u%02u%02u%02u%02u%02u!%s!%s",
343 t->tm_year, t->tm_mon, t->tm_mday, t->tm_hour, t->tm_min, t->tm_sec,
344 address->transport_name, addr);
354 * Print information about the peer.
357 print_my_uri (void *cls, const struct GNUNET_PeerIdentity *peer,
358 const struct GNUNET_HELLO_Message *hello, const char *err_msg)
360 struct GNUNET_CRYPTO_HashAsciiEncoded enc;
361 struct PrintContext *pc = cls;
366 FPRINTF (stderr, "%s", _("Error in communication with PEERINFO service\n"));
367 GNUNET_free (pc->uri);
370 if ((be_quiet) || (NULL == hello))
372 GNUNET_CRYPTO_hash_to_enc (&peer->hashPubKey, &enc);
373 printf ("%s\n", (const char *) &enc);
374 if (be_quiet && get_uri != GNUNET_YES)
380 GNUNET_HELLO_iterate_addresses (hello, GNUNET_NO, &count_address, pc);
381 GNUNET_HELLO_iterate_addresses (hello, GNUNET_NO, &compose_uri, pc);
383 printf ("%s\n", pc->uri);
389 add_addr_to_hello (void *cls, size_t max, void *buffer)
391 struct tm expiration_time;
394 time_t expiration_seconds;
395 struct GNUNET_TIME_Absolute expire;
397 struct GNUNET_PEERINFO_HelloAddressParsingContext *ctx = cls;
399 struct GNUNET_TRANSPORT_PluginFunctions *papi;
404 if (ctx->pos - ctx->tmp == ctx->tmp_len)
407 /* Parsed past the end of string, OR wrong format */
408 if ((ctx->pos - ctx->tmp > ctx->tmp_len) || ctx->pos[0] != '!')
414 /* Not enough bytes (3 for three '!', 14 for expiration date, and
415 * at least 1 for type and 1 for address (1-byte long address is a joke,
416 * but it is not completely unrealistic. Zero-length address is.
418 if (ctx->tmp_len - (ctx->pos - ctx->tmp) < 1 /*!*/ * 3 + 14 + /* at least */ 2)
423 /* Go past the first '!', now we're on expiration date */
425 /* Its length is known, so check for the next '!' right away */
426 if (ctx->pos[14] != '!')
432 memset (&expiration_time, 0, sizeof (struct tm));
434 /* This is FAR more strict than strptime(ctx->pos, "%Y%m%d%H%M%S", ...); */
435 /* FIXME: make it a separate function, since expiration is specified to every address */
436 #define GETNDIGITS(n,cond) \
437 strncpy (buf, &ctx->pos[0], n); \
440 l = strtol (buf, NULL, 10); \
441 if (errno != 0 || cond) \
448 GETNDIGITS (4, l < 1900)
449 expiration_time.tm_year = l - 1900;
451 GETNDIGITS (2, l < 1 || l > 12)
452 expiration_time.tm_mon = l;
454 GETNDIGITS (2, l < 1 || l > 31)
455 expiration_time.tm_mday = l;
457 GETNDIGITS (2, l < 0 || l > 23)
458 expiration_time.tm_hour = l;
460 GETNDIGITS (2, l < 0 || l > 59)
461 expiration_time.tm_min = l;
463 /* 60 - with a leap second */
464 GETNDIGITS (2, l < 0 || l > 60)
465 expiration_time.tm_sec = l;
467 expiration_time.tm_isdst = -1;
471 expiration_seconds = mktime (&expiration_time);
472 if (expiration_seconds == (time_t) -1)
477 expire.abs_value = expiration_seconds * 1000;
479 /* Now we're at '!', advance to the transport type */
482 /* Find the next '!' that separates transport type from
485 exp1 = strstr (ctx->pos, "!");
491 /* We need it 0-terminated */
493 /* Find the '!' that separates address from the next record.
494 * It might not be there, if this is the last record.
496 exp2 = strstr (&exp1[1], "!");
498 exp2 = &ctx->tmp[ctx->tmp_len];
500 papi = GPI_plugins_find (ctx->pos);
503 /* Not an error - we might just not have the right plugin.
504 * Skip this part, advance to the next one and recurse.
505 * But only if this is not the end of string.
508 if (ctx->pos - ctx->tmp >= ctx->tmp_len)
510 return add_addr_to_hello (cls, max, buffer);
512 if (NULL == papi->string_to_address)
514 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
515 _("Plugin `%s' does not support URIs yet\n"),
518 if (ctx->pos - ctx->tmp >= ctx->tmp_len)
520 return add_addr_to_hello (cls, max, buffer);
522 if (GNUNET_OK == papi->string_to_address (papi->cls, &exp1[1], exp2 - &exp1[1], &addr, &addr_len))
524 struct GNUNET_HELLO_Address address;
527 /* address.peer is unset - not used by add_address() */
528 address.address_length = addr_len;
529 address.address = addr;
530 address.transport_name = ctx->pos;
531 ret = GNUNET_HELLO_add_address (&address, expire, buffer, max);
541 parse_hello (const struct GNUNET_CONFIGURATION_Handle *c,
545 char *scheme_part = NULL;
546 char *path_part = NULL;
549 struct GNUNET_HELLO_Message *hello;
550 struct GNUNET_PEERINFO_HelloAddressParsingContext ctx;
552 r = GNUNET_STRINGS_parse_uri (put_uri, &scheme_part, (const char **) &path_part);
555 if (scheme_part == NULL || strcmp (scheme_part, "gnunet://") != 0)
557 GNUNET_free_non_null (scheme_part);
560 GNUNET_free (scheme_part);
562 if (strncmp (path_part, "hello/", 6) != 0)
565 path_part = &path_part[6];
566 ctx.tmp = GNUNET_strdup (path_part);
567 ctx.tmp_len = strlen (path_part);
568 exc = strstr (ctx.tmp, "!");
570 exc = ctx.tmp + ctx.tmp_len;
573 std_result = GNUNET_STRINGS_string_to_data (ctx.tmp, exc - ctx.tmp,
574 (unsigned char *) &my_public_key, sizeof (my_public_key));
575 if (std_result != GNUNET_OK)
577 GNUNET_free (ctx.tmp);
581 hello = GNUNET_HELLO_create (&my_public_key, add_addr_to_hello, &ctx);
582 GNUNET_free (ctx.tmp);
584 /* WARNING: this adds the address from URI WITHOUT verification! */
585 GNUNET_PEERINFO_add_peer (peerinfo, hello);
587 /* wait 1s to give peerinfo operation a chance to succeed */
588 tt = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
589 &state_machine, NULL);
594 * Main state machine that goes over all options and
595 * runs the next requested function.
598 * @param tc scheduler context
601 shutdown_task (void *cls,
602 const struct GNUNET_SCHEDULER_TaskContext *tc)
604 if (GNUNET_SCHEDULER_NO_TASK != tt)
606 GNUNET_SCHEDULER_cancel (tt);
607 tt = GNUNET_SCHEDULER_NO_TASK;
611 GNUNET_PEERINFO_iterate_cancel (pic);
616 GNUNET_TRANSPORT_address_to_string_cancel (atsc);
619 GPI_plugins_unload ();
620 GNUNET_PEERINFO_disconnect (peerinfo);
626 * Main function that will be run by the scheduler.
629 * @param args remaining command-line arguments
630 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
631 * @param c configuration
634 run (void *cls, char *const *args, const char *cfgfile,
635 const struct GNUNET_CONFIGURATION_Handle *c)
637 struct GNUNET_CRYPTO_RsaPrivateKey *priv;
643 FPRINTF (stderr, _("Invalid command line argument `%s'\n"), args[0]);
646 peerinfo = GNUNET_PEERINFO_connect (cfg);
647 if (peerinfo == NULL)
649 FPRINTF (stderr, "%s", _("Could not access PEERINFO service. Exiting.\n"));
652 if ( (GNUNET_YES == get_self) || (GNUNET_YES == get_uri) )
654 /* load private key */
656 GNUNET_CONFIGURATION_get_value_filename (cfg, "GNUNETD", "HOSTKEY",
659 FPRINTF (stderr, _("Could not find option `%s:%s' in configuration.\n"),
660 "GNUNETD", "HOSTKEYFILE");
664 if (NULL == (priv = GNUNET_CRYPTO_rsa_key_create_from_file (fn)))
666 FPRINTF (stderr, _("Loading hostkey from `%s' failed.\n"), fn);
671 GNUNET_CRYPTO_rsa_key_get_public (priv, &my_public_key);
672 GNUNET_CRYPTO_rsa_key_free (priv);
673 GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key), &my_peer_identity.hashPubKey);
676 tt = GNUNET_SCHEDULER_add_now (&state_machine, NULL);
677 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
684 * Main state machine that goes over all options and
685 * runs the next requested function.
688 * @param tc scheduler context
691 state_machine (void *cls,
692 const struct GNUNET_SCHEDULER_TaskContext *tc)
694 tt = GNUNET_SCHEDULER_NO_TASK;
698 GPI_plugins_load (cfg);
699 parse_hello (cfg, put_uri);
703 if (GNUNET_YES == get_info)
705 get_info = GNUNET_NO;
706 GPI_plugins_load (cfg);
707 GNUNET_PEERINFO_iterate (peerinfo, NULL,
708 GNUNET_TIME_relative_multiply
709 (GNUNET_TIME_UNIT_SECONDS, 5), &print_peer_info,
713 if (GNUNET_YES == get_self)
715 struct GNUNET_CRYPTO_HashAsciiEncoded enc;
717 get_self = GNUNET_NO;
718 GNUNET_CRYPTO_hash_to_enc (&my_peer_identity.hashPubKey, &enc);
720 printf ("%s\n", (char *) &enc);
722 printf (_("I am peer `%s'.\n"), (const char *) &enc);
724 if (GNUNET_YES == get_uri)
726 struct PrintContext *pc;
732 pc = GNUNET_malloc (sizeof (struct PrintContext));
733 pkey = GNUNET_CRYPTO_rsa_public_key_to_string (&my_public_key);
734 pl = strlen ("gnunet://hello/");
735 l = strlen (pkey) + pl;
736 pc->uri = GNUNET_malloc (l + 1);
737 strcpy (pc->uri, "gnunet://hello/");
738 strcpy (&pc->uri[pl], pkey);
741 GPI_plugins_load (cfg);
742 pic = GNUNET_PEERINFO_iterate (peerinfo, &my_peer_identity,
743 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5),
747 GNUNET_SCHEDULER_shutdown ();
752 * The main function to obtain peer information.
754 * @param argc number of arguments from the command line
755 * @param argv command line arguments
756 * @return 0 ok, 1 on error
759 main (int argc, char *const *argv)
761 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
762 {'n', "numeric", NULL,
763 gettext_noop ("don't resolve host names"),
764 0, &GNUNET_GETOPT_set_one, &no_resolve},
766 gettext_noop ("output only the identity strings"),
767 0, &GNUNET_GETOPT_set_one, &be_quiet},
769 gettext_noop ("output our own identity only"),
770 0, &GNUNET_GETOPT_set_one, &get_self},
772 gettext_noop ("list all known peers"),
773 0, &GNUNET_GETOPT_set_one, &get_info},
774 {'g', "get-hello", NULL,
775 gettext_noop ("also output HELLO uri(s)"),
776 0, &GNUNET_GETOPT_set_one, &get_uri},
777 {'p', "put-hello", "HELLO",
778 gettext_noop ("add given HELLO uri to the database"),
779 1, &GNUNET_GETOPT_set_string, &put_uri},
780 GNUNET_GETOPT_OPTION_END
783 GNUNET_PROGRAM_run (argc, argv, "gnunet-peerinfo",
784 gettext_noop ("Print information about peers."),
785 options, &run, NULL)) ? 0 : 1;
788 /* end of gnunet-peerinfo.c */