2 This file is part of GNUnet.
3 (C) 2001-2012 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)
75 * Context used for building our own URI.
84 struct PrintContext *pc;
91 struct GNUNET_PEERINFO_HelloAddressParsingContext
113 static int no_resolve;
138 static char *put_uri;
141 * Handle to peerinfo service.
143 static struct GNUNET_PEERINFO_Handle *peerinfo;
146 * Configuration handle.
148 static const struct GNUNET_CONFIGURATION_Handle *cfg;
151 * Main state machine task (if active).
153 static GNUNET_SCHEDULER_TaskIdentifier tt;
156 * Current iterator context (if active, otherwise NULL).
158 static struct GNUNET_PEERINFO_IteratorContext *pic;
161 * Current address-to-string context (if active, otherwise NULL).
163 static struct GNUNET_TRANSPORT_AddressToStringContext *atsc;
168 static struct GNUNET_PeerIdentity my_peer_identity;
173 static struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded my_public_key;
177 * Main state machine that goes over all options and
178 * runs the next requested function.
184 state_machine (void *cls,
185 const struct GNUNET_SCHEDULER_TaskContext *tc);
188 /* ********************* 'get_info' ******************* */
191 * Print the collected address information to the console and free 'pc'.
193 * @param pc printing context
196 dump_pc (struct PrintContext *pc)
198 struct GNUNET_CRYPTO_HashAsciiEncoded enc;
201 GNUNET_CRYPTO_hash_to_enc (&pc->peer.hashPubKey, &enc);
202 printf (_("Peer `%s'\n"), (const char *) &enc);
203 for (i = 0; i < pc->num_addresses; i++)
205 printf ("\t%s\n", pc->address_list[i]);
206 GNUNET_free (pc->address_list[i]);
209 GNUNET_array_grow (pc->address_list, pc->num_addresses, 0);
214 /* ************************* list all known addresses **************** */
218 * Function to call with a human-readable format of an address
221 * @param address NULL on error, otherwise 0-terminated printable UTF-8 string
224 process_resolved_address (void *cls, const char *address)
226 struct PrintContext *pc = cls;
236 GNUNET_array_append (pc->address_list, pc->num_addresses,
237 GNUNET_strdup (address));
242 * Iterator callback to go over all addresses and count them.
244 * @param cls 'struct PrintContext' with 'off' to increment
245 * @param address the address
246 * @param expiration expiration time
247 * @return GNUNET_OK to keep the address and continue
250 count_address (void *cls, const struct GNUNET_HELLO_Address *address,
251 struct GNUNET_TIME_Absolute expiration)
253 struct PrintContext *pc = cls;
261 * Iterator callback to go over all addresses.
264 * @param address the address
265 * @param expiration expiration time
266 * @return GNUNET_OK to keep the address and continue
269 print_address (void *cls, const struct GNUNET_HELLO_Address *address,
270 struct GNUNET_TIME_Absolute expiration)
272 struct PrintContext *pc = cls;
274 // FIXME: this is called many times in parallel!
275 atsc = GNUNET_TRANSPORT_address_to_string (cfg, address, no_resolve,
276 GNUNET_TIME_relative_multiply
277 (GNUNET_TIME_UNIT_SECONDS, 10),
278 &process_resolved_address, pc);
284 * Print information about the peer.
285 * Currently prints the GNUNET_PeerIdentity and the IP.
286 * Could of course do more (e.g. resolve via DNS).
289 print_peer_info (void *cls, const struct GNUNET_PeerIdentity *peer,
290 const struct GNUNET_HELLO_Message *hello, const char *err_msg)
292 struct GNUNET_CRYPTO_HashAsciiEncoded enc;
293 struct PrintContext *pc;
298 FPRINTF (stderr, "%s", _("Error in communication with PEERINFO service\n"));
299 // FIXME: this doesn't mean we're fully done with the printing!
300 // (as the a2s calls happen asynchronously!)
301 tt = GNUNET_SCHEDULER_add_now (&state_machine, NULL);
304 if ((be_quiet) || (NULL == hello))
306 GNUNET_CRYPTO_hash_to_enc (&peer->hashPubKey, &enc);
307 printf ("%s\n", (const char *) &enc);
310 pc = GNUNET_malloc (sizeof (struct PrintContext));
312 GNUNET_HELLO_iterate_addresses (hello, GNUNET_NO, &count_address, pc);
318 GNUNET_HELLO_iterate_addresses (hello, GNUNET_NO, &print_address, pc);
322 /* ************************* GET URI ************************** */
326 * Function that is called on each address of this peer.
327 * Expands the corresponding URI string.
329 * @param cls the 'GetUriContext'
330 * @param address address to add
331 * @param expiration expiration time for the address
332 * @return GNUNET_OK (continue iteration).
335 compose_uri (void *cls, const struct GNUNET_HELLO_Address *address,
336 struct GNUNET_TIME_Absolute expiration)
338 struct GetUriContext *guc = cls;
339 struct GNUNET_TRANSPORT_PluginFunctions *papi;
345 papi = GPI_plugins_find (address->transport_name);
348 /* Not an error - we might just not have the right plugin. */
351 if (NULL == papi->address_to_string)
353 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
354 "URI conversion not implemented for plugin `%s'\n",
355 address->transport_name);
358 addr = papi->address_to_string (papi->cls, address->address, address->address_length);
359 if ( (addr == NULL) || (strlen(addr) == 0) )
361 seconds = expiration.abs_value / 1000;
362 t = gmtime (&seconds);
363 GNUNET_asprintf (&ret,
364 "%s!%04u%02u%02u%02u%02u%02u!%s!%s",
372 address->transport_name,
374 GNUNET_free (guc->uri);
381 * Print URI of the peer.
383 * @param cls the 'struct GetUriContext'
384 * @param peer identity of the peer (unused)
385 * @param hello addresses of the peer
386 * @param err_msg error message
389 print_my_uri (void *cls, const struct GNUNET_PeerIdentity *peer,
390 const struct GNUNET_HELLO_Message *hello,
393 struct GetUriContext *guc = cls;
399 _("Error in communication with PEERINFO service: %s\n"),
405 GNUNET_HELLO_iterate_addresses (hello, GNUNET_NO, &compose_uri, guc);
406 printf ("%s\n", (const char *) guc->uri);
408 GNUNET_free (guc->uri);
410 tt = GNUNET_SCHEDULER_add_now (&state_machine, NULL);
414 /* ************************* import HELLO by URI ********************* */
418 add_addr_to_hello (void *cls, size_t max, void *buffer)
420 struct tm expiration_time;
423 time_t expiration_seconds;
424 struct GNUNET_TIME_Absolute expire;
426 struct GNUNET_PEERINFO_HelloAddressParsingContext *ctx = cls;
428 struct GNUNET_TRANSPORT_PluginFunctions *papi;
433 if (ctx->pos - ctx->tmp == ctx->tmp_len)
436 /* Parsed past the end of string, OR wrong format */
437 if ((ctx->pos - ctx->tmp > ctx->tmp_len) || ctx->pos[0] != '!')
443 /* Not enough bytes (3 for three '!', 14 for expiration date, and
444 * at least 1 for type and 1 for address (1-byte long address is a joke,
445 * but it is not completely unrealistic. Zero-length address is.
447 if (ctx->tmp_len - (ctx->pos - ctx->tmp) < 1 /*!*/ * 3 + 14 + /* at least */ 2)
452 /* Go past the first '!', now we're on expiration date */
454 /* Its length is known, so check for the next '!' right away */
455 if (ctx->pos[14] != '!')
461 memset (&expiration_time, 0, sizeof (struct tm));
463 /* This is FAR more strict than strptime(ctx->pos, "%Y%m%d%H%M%S", ...); */
464 /* FIXME: make it a separate function, since expiration is specified to every address */
465 #define GETNDIGITS(n,cond) \
466 strncpy (buf, &ctx->pos[0], n); \
469 l = strtol (buf, NULL, 10); \
470 if (errno != 0 || cond) \
477 GETNDIGITS (4, l < 1900)
478 expiration_time.tm_year = l - 1900;
480 GETNDIGITS (2, l < 1 || l > 12)
481 expiration_time.tm_mon = l;
483 GETNDIGITS (2, l < 1 || l > 31)
484 expiration_time.tm_mday = l;
486 GETNDIGITS (2, l < 0 || l > 23)
487 expiration_time.tm_hour = l;
489 GETNDIGITS (2, l < 0 || l > 59)
490 expiration_time.tm_min = l;
492 /* 60 - with a leap second */
493 GETNDIGITS (2, l < 0 || l > 60)
494 expiration_time.tm_sec = l;
496 expiration_time.tm_isdst = -1;
500 expiration_seconds = mktime (&expiration_time);
501 if (expiration_seconds == (time_t) -1)
506 expire.abs_value = expiration_seconds * 1000;
508 /* Now we're at '!', advance to the transport type */
511 /* Find the next '!' that separates transport type from
514 exp1 = strstr (ctx->pos, "!");
520 /* We need it 0-terminated */
522 /* Find the '!' that separates address from the next record.
523 * It might not be there, if this is the last record.
525 exp2 = strstr (&exp1[1], "!");
527 exp2 = &ctx->tmp[ctx->tmp_len];
529 papi = GPI_plugins_find (ctx->pos);
532 /* Not an error - we might just not have the right plugin.
533 * Skip this part, advance to the next one and recurse.
534 * But only if this is not the end of string.
537 if (ctx->pos - ctx->tmp >= ctx->tmp_len)
539 return add_addr_to_hello (cls, max, buffer);
541 if (NULL == papi->string_to_address)
543 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
544 _("Plugin `%s' does not support URIs yet\n"),
547 if (ctx->pos - ctx->tmp >= ctx->tmp_len)
549 return add_addr_to_hello (cls, max, buffer);
551 if ((papi->string_to_address != NULL) && (GNUNET_OK ==
552 papi->string_to_address (papi->cls, &exp1[1], exp2 - &exp1[1], &addr,
555 struct GNUNET_HELLO_Address address;
558 /* address.peer is unset - not used by add_address() */
559 address.address_length = addr_len;
560 address.address = addr;
561 address.transport_name = ctx->pos;
562 ret = GNUNET_HELLO_add_address (&address, expire, buffer, max);
572 parse_hello (const struct GNUNET_CONFIGURATION_Handle *c,
576 char *scheme_part = NULL;
577 char *path_part = NULL;
580 struct GNUNET_HELLO_Message *hello;
581 struct GNUNET_PEERINFO_HelloAddressParsingContext ctx;
583 r = GNUNET_STRINGS_parse_uri (put_uri, &scheme_part, (const char **) &path_part);
586 if (scheme_part == NULL || strcmp (scheme_part, "gnunet://") != 0)
588 GNUNET_free_non_null (scheme_part);
591 GNUNET_free (scheme_part);
593 if (strncmp (path_part, "hello/", 6) != 0)
596 path_part = &path_part[6];
597 ctx.tmp = GNUNET_strdup (path_part);
598 ctx.tmp_len = strlen (path_part);
599 exc = strstr (ctx.tmp, "!");
601 exc = ctx.tmp + ctx.tmp_len;
604 std_result = GNUNET_STRINGS_string_to_data (ctx.tmp, exc - ctx.tmp,
605 (unsigned char *) &my_public_key, sizeof (my_public_key));
606 if (std_result != GNUNET_OK)
608 GNUNET_free (ctx.tmp);
612 hello = GNUNET_HELLO_create (&my_public_key, add_addr_to_hello, &ctx);
613 GNUNET_free (ctx.tmp);
615 /* WARNING: this adds the address from URI WITHOUT verification! */
616 GNUNET_PEERINFO_add_peer (peerinfo, hello);
618 /* wait 1s to give peerinfo operation a chance to succeed */
619 tt = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
620 &state_machine, NULL);
624 /* ************************ Main state machine ********************* */
628 * Main state machine that goes over all options and
629 * runs the next requested function.
632 * @param tc scheduler context
635 shutdown_task (void *cls,
636 const struct GNUNET_SCHEDULER_TaskContext *tc)
638 if (GNUNET_SCHEDULER_NO_TASK != tt)
640 GNUNET_SCHEDULER_cancel (tt);
641 tt = GNUNET_SCHEDULER_NO_TASK;
645 GNUNET_PEERINFO_iterate_cancel (pic);
650 GNUNET_TRANSPORT_address_to_string_cancel (atsc);
653 GPI_plugins_unload ();
654 GNUNET_PEERINFO_disconnect (peerinfo);
660 * Main function that will be run by the scheduler.
663 * @param args remaining command-line arguments
664 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
665 * @param c configuration
668 run (void *cls, char *const *args, const char *cfgfile,
669 const struct GNUNET_CONFIGURATION_Handle *c)
671 struct GNUNET_CRYPTO_RsaPrivateKey *priv;
677 FPRINTF (stderr, _("Invalid command line argument `%s'\n"), args[0]);
680 peerinfo = GNUNET_PEERINFO_connect (cfg);
681 if (peerinfo == NULL)
683 FPRINTF (stderr, "%s", _("Could not access PEERINFO service. Exiting.\n"));
686 if ( (GNUNET_YES == get_self) || (GNUNET_YES == get_uri) )
688 /* load private key */
690 GNUNET_CONFIGURATION_get_value_filename (cfg, "GNUNETD", "HOSTKEY",
693 FPRINTF (stderr, _("Could not find option `%s:%s' in configuration.\n"),
694 "GNUNETD", "HOSTKEYFILE");
698 if (NULL == (priv = GNUNET_CRYPTO_rsa_key_create_from_file (fn)))
700 FPRINTF (stderr, _("Loading hostkey from `%s' failed.\n"), fn);
705 GNUNET_CRYPTO_rsa_key_get_public (priv, &my_public_key);
706 fprintf (stderr, "PK: `%s\n",
707 GNUNET_CRYPTO_rsa_public_key_to_string (&my_public_key));
708 GNUNET_CRYPTO_rsa_key_free (priv);
709 GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key), &my_peer_identity.hashPubKey);
712 tt = GNUNET_SCHEDULER_add_now (&state_machine, NULL);
713 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
720 * Main state machine that goes over all options and
721 * runs the next requested function.
724 * @param tc scheduler context
727 state_machine (void *cls,
728 const struct GNUNET_SCHEDULER_TaskContext *tc)
730 tt = GNUNET_SCHEDULER_NO_TASK;
734 GPI_plugins_load (cfg);
735 parse_hello (cfg, put_uri);
739 if (GNUNET_YES == get_info)
741 get_info = GNUNET_NO;
742 GPI_plugins_load (cfg);
743 GNUNET_PEERINFO_iterate (peerinfo, NULL,
744 GNUNET_TIME_relative_multiply
745 (GNUNET_TIME_UNIT_SECONDS, 5), &print_peer_info,
749 if (GNUNET_YES == get_self)
751 struct GNUNET_CRYPTO_HashAsciiEncoded enc;
753 get_self = GNUNET_NO;
754 GNUNET_CRYPTO_hash_to_enc (&my_peer_identity.hashPubKey, &enc);
756 printf ("%s\n", (char *) &enc);
758 printf (_("I am peer `%s'.\n"), (const char *) &enc);
760 if (GNUNET_YES == get_uri)
762 struct GetUriContext *guc;
765 guc = GNUNET_malloc (sizeof (struct GetUriContext));
766 pkey = GNUNET_CRYPTO_rsa_public_key_to_string (&my_public_key);
767 GNUNET_asprintf (&guc->uri,
771 GPI_plugins_load (cfg);
772 pic = GNUNET_PEERINFO_iterate (peerinfo, &my_peer_identity,
773 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5),
778 GNUNET_SCHEDULER_shutdown ();
783 * The main function to obtain peer information.
785 * @param argc number of arguments from the command line
786 * @param argv command line arguments
787 * @return 0 ok, 1 on error
790 main (int argc, char *const *argv)
792 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
793 {'n', "numeric", NULL,
794 gettext_noop ("don't resolve host names"),
795 0, &GNUNET_GETOPT_set_one, &no_resolve},
797 gettext_noop ("output only the identity strings"),
798 0, &GNUNET_GETOPT_set_one, &be_quiet},
800 gettext_noop ("output our own identity only"),
801 0, &GNUNET_GETOPT_set_one, &get_self},
803 gettext_noop ("list all known peers"),
804 0, &GNUNET_GETOPT_set_one, &get_info},
805 {'g', "get-hello", NULL,
806 gettext_noop ("also output HELLO uri(s)"),
807 0, &GNUNET_GETOPT_set_one, &get_uri},
808 {'p', "put-hello", "HELLO",
809 gettext_noop ("add given HELLO uri to the database"),
810 1, &GNUNET_GETOPT_set_string, &put_uri},
811 GNUNET_GETOPT_OPTION_END
814 GNUNET_PROGRAM_run (argc, argv, "gnunet-peerinfo",
815 gettext_noop ("Print information about peers."),
816 options, &run, NULL)) ? 0 : 1;
819 /* end of gnunet-peerinfo.c */