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"
37 * Prefix that every HELLO URI must start with.
39 #define HELLO_URI_PREFIX "gnunet://hello/"
42 * Structure we use to collect printable address information.
47 * Record we keep for each printable address.
52 * Current address-to-string context (if active, otherwise NULL).
54 struct GNUNET_TRANSPORT_AddressToStringContext *atsc;
62 * Print context this address record belongs to.
64 struct PrintContext *pc;
69 * Structure we use to collect printable address information.
77 struct PrintContext *next;
82 struct PrintContext *prev;
85 * Identity of the peer.
87 struct GNUNET_PeerIdentity peer;
90 * List of printable addresses.
92 struct AddressRecord *address_list;
95 * Number of completed addresses in 'address_list'.
97 unsigned int num_addresses;
100 * Number of addresses allocated in 'address_list'.
102 unsigned int address_list_size;
105 * Current offset in 'address_list' (counted down).
113 * Context used for building our own URI.
126 * Context for 'add_address_to_hello'.
128 struct GNUNET_PEERINFO_HelloAddressParsingContext
131 * Position in the URI with the next address to parse.
136 * Set to GNUNET_SYSERR to indicate parse errors.
146 static int no_resolve;
171 static char *put_uri;
174 * Handle to peerinfo service.
176 static struct GNUNET_PEERINFO_Handle *peerinfo;
179 * Configuration handle.
181 static const struct GNUNET_CONFIGURATION_Handle *cfg;
184 * Main state machine task (if active).
186 static GNUNET_SCHEDULER_TaskIdentifier tt;
189 * Current iterator context (if active, otherwise NULL).
191 static struct GNUNET_PEERINFO_IteratorContext *pic;
196 static struct GNUNET_PeerIdentity my_peer_identity;
201 static struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded my_public_key;
204 * Head of list of print contexts.
206 static struct PrintContext *pc_head;
209 * Tail of list of print contexts.
211 static struct PrintContext *pc_tail;
215 * Main state machine that goes over all options and
216 * runs the next requested function.
222 state_machine (void *cls,
223 const struct GNUNET_SCHEDULER_TaskContext *tc);
226 /* ********************* 'get_info' ******************* */
229 * Print the collected address information to the console and free 'pc'.
231 * @param pc printing context
234 dump_pc (struct PrintContext *pc)
236 struct GNUNET_CRYPTO_HashAsciiEncoded enc;
239 GNUNET_CRYPTO_hash_to_enc (&pc->peer.hashPubKey, &enc);
240 printf (_("Peer `%s'\n"),
241 (const char *) &enc);
242 for (i = 0; i < pc->num_addresses; i++)
244 if (NULL != pc->address_list[i].result)
246 printf ("\t%s\n", pc->address_list[i].result);
247 GNUNET_free (pc->address_list[i].result);
251 GNUNET_free_non_null (pc->address_list);
252 GNUNET_CONTAINER_DLL_remove (pc_head,
256 if ( (NULL == pc_head) &&
258 tt = GNUNET_SCHEDULER_add_now (&state_machine, NULL);
262 /* ************************* list all known addresses **************** */
266 * Function to call with a human-readable format of an address
269 * @param address NULL on error, otherwise 0-terminated printable UTF-8 string
272 process_resolved_address (void *cls, const char *address)
274 struct AddressRecord * ar = cls;
275 struct PrintContext *pc = ar->pc;
279 if (NULL == ar->result)
280 ar->result = GNUNET_strdup (address);
285 if (pc->num_addresses == pc->address_list_size)
291 * Iterator callback to go over all addresses and count them.
293 * @param cls 'struct PrintContext' with 'off' to increment
294 * @param address the address
295 * @param expiration expiration time
296 * @return GNUNET_OK to keep the address and continue
299 count_address (void *cls, const struct GNUNET_HELLO_Address *address,
300 struct GNUNET_TIME_Absolute expiration)
302 struct PrintContext *pc = cls;
310 * Iterator callback to go over all addresses.
313 * @param address the address
314 * @param expiration expiration time
315 * @return GNUNET_OK to keep the address and continue
318 print_address (void *cls, const struct GNUNET_HELLO_Address *address,
319 struct GNUNET_TIME_Absolute expiration)
321 struct PrintContext *pc = cls;
322 struct AddressRecord *ar;
324 GNUNET_assert (0 < pc->off);
325 ar = &pc->address_list[--pc->off];
327 ar->atsc = GNUNET_TRANSPORT_address_to_string (cfg, address, no_resolve,
328 GNUNET_TIME_relative_multiply
329 (GNUNET_TIME_UNIT_SECONDS, 10),
330 &process_resolved_address, ar);
336 * Print information about the peer.
337 * Currently prints the GNUNET_PeerIdentity and the transport address.
339 * @param cls the 'struct PrintContext'
340 * @param peer identity of the peer
341 * @param hello addresses of the peer
342 * @param err_msg error message
345 print_peer_info (void *cls, const struct GNUNET_PeerIdentity *peer,
346 const struct GNUNET_HELLO_Message *hello, const char *err_msg)
348 struct GNUNET_CRYPTO_HashAsciiEncoded enc;
349 struct PrintContext *pc;
353 pic = NULL; /* end of iteration */
357 _("Error in communication with PEERINFO service: %s\n"),
361 tt = GNUNET_SCHEDULER_add_now (&state_machine, NULL);
364 if ((GNUNET_YES == be_quiet) || (NULL == hello))
366 GNUNET_CRYPTO_hash_to_enc (&peer->hashPubKey, &enc);
367 printf ("%s\n", (const char *) &enc);
370 pc = GNUNET_malloc (sizeof (struct PrintContext));
371 GNUNET_CONTAINER_DLL_insert (pc_head,
375 GNUNET_HELLO_iterate_addresses (hello,
384 pc->address_list_size = pc->off;
385 pc->address_list = GNUNET_malloc (sizeof (struct AddressRecord) * pc->off);
386 GNUNET_HELLO_iterate_addresses (hello, GNUNET_NO,
391 /* ************************* GET URI ************************** */
395 * Function that is called on each address of this peer.
396 * Expands the corresponding URI string.
398 * @param cls the 'GetUriContext'
399 * @param address address to add
400 * @param expiration expiration time for the address
401 * @return GNUNET_OK (continue iteration).
404 compose_uri (void *cls, const struct GNUNET_HELLO_Address *address,
405 struct GNUNET_TIME_Absolute expiration)
407 struct GetUriContext *guc = cls;
408 struct GNUNET_TRANSPORT_PluginFunctions *papi;
415 papi = GPI_plugins_find (address->transport_name);
418 /* Not an error - we might just not have the right plugin. */
421 if (NULL == papi->address_to_string)
423 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
424 "URI conversion not implemented for plugin `%s'\n",
425 address->transport_name);
428 addr = papi->address_to_string (papi->cls, address->address, address->address_length);
429 if ( (addr == NULL) || (strlen(addr) == 0) )
431 seconds = expiration.abs_value / 1000;
432 t = gmtime (&seconds);
433 GNUNET_assert (0 != strftime (tbuf, sizeof (tbuf),
436 GNUNET_asprintf (&ret,
440 address->transport_name,
442 GNUNET_free (guc->uri);
449 * Print URI of the peer.
451 * @param cls the 'struct GetUriContext'
452 * @param peer identity of the peer (unused)
453 * @param hello addresses of the peer
454 * @param err_msg error message
457 print_my_uri (void *cls, const struct GNUNET_PeerIdentity *peer,
458 const struct GNUNET_HELLO_Message *hello,
461 struct GetUriContext *guc = cls;
468 _("Error in communication with PEERINFO service: %s\n"),
470 GNUNET_free_non_null (guc->uri);
472 tt = GNUNET_SCHEDULER_add_now (&state_machine, NULL);
476 GNUNET_HELLO_iterate_addresses (hello, GNUNET_NO, &compose_uri, guc);
477 printf ("%s\n", (const char *) guc->uri);
481 /* ************************* import HELLO by URI ********************* */
485 * We're building a HELLO. Parse the next address from the
486 * parsing context and append it.
488 * @param cls the 'struct GNUNET_PEERINFO_HelloAddressParsingContext'
489 * @param max number of bytes available for HELLO construction
490 * @param buffer where to copy the next address (in binary format)
491 * @return number of bytes added to buffer
494 add_address_to_hello (void *cls, size_t max, void *buffer)
496 struct GNUNET_PEERINFO_HelloAddressParsingContext *ctx = cls;
501 struct tm expiration_time;
502 time_t expiration_seconds;
503 struct GNUNET_TIME_Absolute expire;
504 struct GNUNET_TRANSPORT_PluginFunctions *papi;
507 struct GNUNET_HELLO_Address haddr;
510 if (NULL == ctx->pos)
512 if ('!' != ctx->pos[0])
514 ctx->ret = GNUNET_SYSERR;
518 memset (&expiration_time, 0, sizeof (expiration_time));
519 tname = strptime (ctx->pos,
524 ctx->ret = GNUNET_SYSERR;
527 expiration_seconds = mktime (&expiration_time);
528 if (expiration_seconds == (time_t) -1)
530 ctx->ret = GNUNET_SYSERR;
533 expire.abs_value = expiration_seconds * 1000;
536 ctx->ret = GNUNET_SYSERR;
540 address = strchr (tname, (int) '!');
543 ctx->ret = GNUNET_SYSERR;
547 end = strchr (address, (int) '!');
551 end = address + strlen (address);
557 plugin_name = GNUNET_strndup (tname, address - tname);
558 papi = GPI_plugins_find (plugin_name);
561 /* Not an error - we might just not have the right plugin.
562 * Skip this part, advance to the next one and recurse.
563 * But only if this is not the end of string.
565 GNUNET_free (plugin_name);
568 if (NULL == papi->string_to_address)
570 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
571 _("Plugin `%s' does not support URIs yet\n"),
573 GNUNET_free (plugin_name);
577 papi->string_to_address (papi->cls,
583 GNUNET_free (plugin_name);
584 return GNUNET_SYSERR;
586 /* address.peer is unset - not used by add_address() */
587 haddr.address_length = addr_len;
588 haddr.address = addr;
589 haddr.transport_name = plugin_name;
590 ret = GNUNET_HELLO_add_address (&haddr, expire, buffer, max);
592 GNUNET_free (plugin_name);
598 * Parse the PUT URI given at the command line and add it to our peerinfo
601 * @param put_uri URI string to parse
602 * @return GNUNET_OK on success, GNUNET_SYSERR if the URI was invalid, GNUNET_NO on other errors
605 parse_hello_uri (const char *put_uri)
609 struct GNUNET_HELLO_Message *hello;
610 struct GNUNET_PEERINFO_HelloAddressParsingContext ctx;
612 if (0 != strncmp (put_uri,
614 strlen (HELLO_URI_PREFIX)))
615 return GNUNET_SYSERR;
616 pks = &put_uri[strlen (HELLO_URI_PREFIX)];
617 exc = strstr (pks, "!");
618 if (GNUNET_OK != GNUNET_STRINGS_string_to_data (pks,
619 (NULL == exc) ? strlen (pks) : (exc - pks),
620 (unsigned char *) &my_public_key,
621 sizeof (my_public_key)))
622 return GNUNET_SYSERR;
625 hello = GNUNET_HELLO_create (&my_public_key, &add_address_to_hello, &ctx);
629 /* WARNING: this adds the address from URI WITHOUT verification! */
630 if (GNUNET_OK == ctx.ret)
631 GNUNET_PEERINFO_add_peer (peerinfo, hello);
635 /* wait 1s to give peerinfo operation a chance to succeed */
636 /* FIXME: current peerinfo API sucks to require this; not to mention
637 that we get no feedback to determine if the operation actually succeeded */
638 tt = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
639 &state_machine, NULL);
644 /* ************************ Main state machine ********************* */
648 * Main state machine that goes over all options and
649 * runs the next requested function.
652 * @param tc scheduler context
655 shutdown_task (void *cls,
656 const struct GNUNET_SCHEDULER_TaskContext *tc)
658 struct PrintContext *pc;
659 struct AddressRecord *ar;
662 if (GNUNET_SCHEDULER_NO_TASK != tt)
664 GNUNET_SCHEDULER_cancel (tt);
665 tt = GNUNET_SCHEDULER_NO_TASK;
669 GNUNET_PEERINFO_iterate_cancel (pic);
672 while (NULL != (pc = pc_head))
674 GNUNET_CONTAINER_DLL_remove (pc_head,
677 for (i=0;i<pc->address_list_size;i++)
679 ar = &pc->address_list[i];
680 GNUNET_free_non_null (ar->result);
681 if (NULL != ar->atsc)
683 GNUNET_TRANSPORT_address_to_string_cancel (ar->atsc);
687 GNUNET_free_non_null (pc->address_list);
690 GPI_plugins_unload ();
691 if (NULL != peerinfo)
693 GNUNET_PEERINFO_disconnect (peerinfo);
700 * Main function that will be run by the scheduler.
703 * @param args remaining command-line arguments
704 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
705 * @param c configuration
708 run (void *cls, char *const *args, const char *cfgfile,
709 const struct GNUNET_CONFIGURATION_Handle *c)
711 struct GNUNET_CRYPTO_RsaPrivateKey *priv;
717 FPRINTF (stderr, _("Invalid command line argument `%s'\n"), args[0]);
720 peerinfo = GNUNET_PEERINFO_connect (cfg);
721 if (peerinfo == NULL)
723 FPRINTF (stderr, "%s", _("Could not access PEERINFO service. Exiting.\n"));
726 if ( (GNUNET_YES == get_self) || (GNUNET_YES == get_uri) )
728 /* load private key */
730 GNUNET_CONFIGURATION_get_value_filename (cfg, "GNUNETD", "HOSTKEY",
733 FPRINTF (stderr, _("Could not find option `%s:%s' in configuration.\n"),
734 "GNUNETD", "HOSTKEYFILE");
738 if (NULL == (priv = GNUNET_CRYPTO_rsa_key_create_from_file (fn)))
740 FPRINTF (stderr, _("Loading hostkey from `%s' failed.\n"), fn);
745 GNUNET_CRYPTO_rsa_key_get_public (priv, &my_public_key);
746 GNUNET_CRYPTO_rsa_key_free (priv);
747 GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key), &my_peer_identity.hashPubKey);
750 tt = GNUNET_SCHEDULER_add_now (&state_machine, NULL);
751 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
758 * Main state machine that goes over all options and
759 * runs the next requested function.
762 * @param tc scheduler context
765 state_machine (void *cls,
766 const struct GNUNET_SCHEDULER_TaskContext *tc)
768 tt = GNUNET_SCHEDULER_NO_TASK;
772 GPI_plugins_load (cfg);
773 if (GNUNET_SYSERR == parse_hello_uri (put_uri))
775 _("Invalid URI `%s'\n"),
777 GNUNET_free (put_uri);
781 if (GNUNET_YES == get_info)
783 get_info = GNUNET_NO;
784 GPI_plugins_load (cfg);
785 pic = GNUNET_PEERINFO_iterate (peerinfo, NULL,
786 GNUNET_TIME_UNIT_FOREVER_REL,
787 &print_peer_info, NULL);
790 if (GNUNET_YES == get_self)
792 struct GNUNET_CRYPTO_HashAsciiEncoded enc;
794 get_self = GNUNET_NO;
795 GNUNET_CRYPTO_hash_to_enc (&my_peer_identity.hashPubKey, &enc);
797 printf ("%s\n", (char *) &enc);
799 printf (_("I am peer `%s'.\n"), (const char *) &enc);
801 if (GNUNET_YES == get_uri)
803 struct GetUriContext *guc;
806 guc = GNUNET_malloc (sizeof (struct GetUriContext));
807 pkey = GNUNET_CRYPTO_rsa_public_key_to_string (&my_public_key);
808 GNUNET_asprintf (&guc->uri,
813 GPI_plugins_load (cfg);
814 pic = GNUNET_PEERINFO_iterate (peerinfo, &my_peer_identity,
815 GNUNET_TIME_UNIT_FOREVER_REL,
820 GNUNET_SCHEDULER_shutdown ();
825 * The main function to obtain peer information.
827 * @param argc number of arguments from the command line
828 * @param argv command line arguments
829 * @return 0 ok, 1 on error
832 main (int argc, char *const *argv)
834 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
835 {'n', "numeric", NULL,
836 gettext_noop ("don't resolve host names"),
837 0, &GNUNET_GETOPT_set_one, &no_resolve},
839 gettext_noop ("output only the identity strings"),
840 0, &GNUNET_GETOPT_set_one, &be_quiet},
842 gettext_noop ("output our own identity only"),
843 0, &GNUNET_GETOPT_set_one, &get_self},
845 gettext_noop ("list all known peers"),
846 0, &GNUNET_GETOPT_set_one, &get_info},
847 {'g', "get-hello", NULL,
848 gettext_noop ("also output HELLO uri(s)"),
849 0, &GNUNET_GETOPT_set_one, &get_uri},
850 {'p', "put-hello", "HELLO",
851 gettext_noop ("add given HELLO uri to the database"),
852 1, &GNUNET_GETOPT_set_string, &put_uri},
853 GNUNET_GETOPT_OPTION_END
856 GNUNET_PROGRAM_run (argc, argv, "gnunet-peerinfo",
857 gettext_noop ("Print information about peers."),
858 options, &run, NULL)) ? 0 : 1;
861 /* end of gnunet-peerinfo.c */