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 * How long until we time out during peerinfo iterations?
44 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
47 * Structure we use to collect printable address information.
52 * Record we keep for each printable address.
57 * Current address-to-string context (if active, otherwise NULL).
59 struct GNUNET_TRANSPORT_AddressToStringContext *atsc;
67 * Print context this address record belongs to.
69 struct PrintContext *pc;
74 * Structure we use to collect printable address information.
82 struct PrintContext *next;
87 struct PrintContext *prev;
90 * Identity of the peer.
92 struct GNUNET_PeerIdentity peer;
95 * List of printable addresses.
97 struct AddressRecord *address_list;
100 * Number of completed addresses in 'address_list'.
102 unsigned int num_addresses;
105 * Number of addresses allocated in 'address_list'.
107 unsigned int address_list_size;
110 * Current offset in 'address_list' (counted down).
118 * Context used for building our own URI.
131 * Context for 'add_address_to_hello'.
133 struct GNUNET_PEERINFO_HelloAddressParsingContext
136 * Position in the URI with the next address to parse.
141 * Set to GNUNET_SYSERR to indicate parse errors.
151 static int no_resolve;
176 static char *put_uri;
179 * Handle to peerinfo service.
181 static struct GNUNET_PEERINFO_Handle *peerinfo;
184 * Configuration handle.
186 static const struct GNUNET_CONFIGURATION_Handle *cfg;
189 * Main state machine task (if active).
191 static GNUNET_SCHEDULER_TaskIdentifier tt;
194 * Current iterator context (if active, otherwise NULL).
196 static struct GNUNET_PEERINFO_IteratorContext *pic;
201 static struct GNUNET_PeerIdentity my_peer_identity;
206 static struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded my_public_key;
209 * Head of list of print contexts.
211 static struct PrintContext *pc_head;
214 * Tail of list of print contexts.
216 static struct PrintContext *pc_tail;
219 * Handle to current 'GNUNET_PEERINFO_add_peer' operation.
221 static struct GNUNET_PEERINFO_AddContext *ac;
225 * Main state machine that goes over all options and
226 * runs the next requested function.
232 state_machine (void *cls,
233 const struct GNUNET_SCHEDULER_TaskContext *tc);
238 * Replace all characters in the input 'in' according
239 * to the mapping. The mapping says to map each character
240 * in 'oldchars' to the corresponding character (by offset)
243 * @param in input string to remap
244 * @param oldchars characters to replace
245 * @param newchars replacement characters, must have same length as 'oldchars'
246 * @return copy of string with replacement applied.
249 map_characters (const char *in,
250 const char *oldchars,
251 const char *newchars)
257 GNUNET_assert (strlen (oldchars) == strlen (newchars));
258 ret = GNUNET_strdup (in);
260 while (ret[i] != '\0')
262 off = strchr (oldchars, ret[i]);
264 ret[i] = newchars[off - oldchars];
272 /* ********************* 'get_info' ******************* */
275 * Print the collected address information to the console and free 'pc'.
277 * @param pc printing context
280 dump_pc (struct PrintContext *pc)
282 struct GNUNET_CRYPTO_HashAsciiEncoded enc;
285 GNUNET_CRYPTO_hash_to_enc (&pc->peer.hashPubKey, &enc);
286 printf (_("Peer `%s'\n"),
287 (const char *) &enc);
288 for (i = 0; i < pc->num_addresses; i++)
290 if (NULL != pc->address_list[i].result)
292 printf ("\t%s\n", pc->address_list[i].result);
293 GNUNET_free (pc->address_list[i].result);
297 GNUNET_free_non_null (pc->address_list);
298 GNUNET_CONTAINER_DLL_remove (pc_head,
302 if ( (NULL == pc_head) &&
304 tt = GNUNET_SCHEDULER_add_now (&state_machine, NULL);
308 /* ************************* list all known addresses **************** */
312 * Function to call with a human-readable format of an address
315 * @param address NULL on error, otherwise 0-terminated printable UTF-8 string
318 process_resolved_address (void *cls, const char *address)
320 struct AddressRecord * ar = cls;
321 struct PrintContext *pc = ar->pc;
325 if (NULL == ar->result)
326 ar->result = GNUNET_strdup (address);
331 if (pc->num_addresses == pc->address_list_size)
337 * Iterator callback to go over all addresses and count them.
339 * @param cls 'struct PrintContext' with 'off' to increment
340 * @param address the address
341 * @param expiration expiration time
342 * @return GNUNET_OK to keep the address and continue
345 count_address (void *cls, const struct GNUNET_HELLO_Address *address,
346 struct GNUNET_TIME_Absolute expiration)
348 struct PrintContext *pc = cls;
356 * Iterator callback to go over all addresses.
359 * @param address the address
360 * @param expiration expiration time
361 * @return GNUNET_OK to keep the address and continue
364 print_address (void *cls, const struct GNUNET_HELLO_Address *address,
365 struct GNUNET_TIME_Absolute expiration)
367 struct PrintContext *pc = cls;
368 struct AddressRecord *ar;
370 GNUNET_assert (0 < pc->off);
371 ar = &pc->address_list[--pc->off];
373 ar->atsc = GNUNET_TRANSPORT_address_to_string (cfg, address, no_resolve,
374 GNUNET_TIME_relative_multiply
375 (GNUNET_TIME_UNIT_SECONDS, 10),
376 &process_resolved_address, ar);
382 * Print information about the peer.
383 * Currently prints the GNUNET_PeerIdentity and the transport address.
385 * @param cls the 'struct PrintContext'
386 * @param peer identity of the peer
387 * @param hello addresses of the peer
388 * @param err_msg error message
391 print_peer_info (void *cls, const struct GNUNET_PeerIdentity *peer,
392 const struct GNUNET_HELLO_Message *hello, const char *err_msg)
394 struct GNUNET_CRYPTO_HashAsciiEncoded enc;
395 struct PrintContext *pc;
399 pic = NULL; /* end of iteration */
403 _("Error in communication with PEERINFO service: %s\n"),
407 tt = GNUNET_SCHEDULER_add_now (&state_machine, NULL);
410 if ((GNUNET_YES == be_quiet) || (NULL == hello))
412 GNUNET_CRYPTO_hash_to_enc (&peer->hashPubKey, &enc);
413 printf ("%s\n", (const char *) &enc);
416 pc = GNUNET_malloc (sizeof (struct PrintContext));
417 GNUNET_CONTAINER_DLL_insert (pc_head,
421 GNUNET_HELLO_iterate_addresses (hello,
430 pc->address_list_size = pc->off;
431 pc->address_list = GNUNET_malloc (sizeof (struct AddressRecord) * pc->off);
432 GNUNET_HELLO_iterate_addresses (hello, GNUNET_NO,
437 /* ************************* GET URI ************************** */
441 * Function that is called on each address of this peer.
442 * Expands the corresponding URI string.
444 * @param cls the 'GetUriContext'
445 * @param address address to add
446 * @param expiration expiration time for the address
447 * @return GNUNET_OK (continue iteration).
450 compose_uri (void *cls, const struct GNUNET_HELLO_Address *address,
451 struct GNUNET_TIME_Absolute expiration)
453 struct GetUriContext *guc = cls;
454 struct GNUNET_TRANSPORT_PluginFunctions *papi;
462 papi = GPI_plugins_find (address->transport_name);
465 /* Not an error - we might just not have the right plugin. */
468 if (NULL == papi->address_to_string)
470 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
471 "URI conversion not implemented for plugin `%s'\n",
472 address->transport_name);
475 addr = papi->address_to_string (papi->cls, address->address, address->address_length);
476 if ( (addr == NULL) || (strlen(addr) == 0) )
478 /* For URIs we use '(' and ')' instead of '[' and ']' as brackets are reserved
479 characters in URIs */
480 uri_addr = map_characters (addr, "[]", "()");
481 seconds = expiration.abs_value / 1000;
482 t = gmtime (&seconds);
483 GNUNET_assert (0 != strftime (tbuf, sizeof (tbuf),
486 GNUNET_asprintf (&ret,
490 address->transport_name,
492 GNUNET_free (uri_addr);
493 GNUNET_free (guc->uri);
500 * Print URI of the peer.
502 * @param cls the 'struct GetUriContext'
503 * @param peer identity of the peer (unused)
504 * @param hello addresses of the peer
505 * @param err_msg error message
508 print_my_uri (void *cls, const struct GNUNET_PeerIdentity *peer,
509 const struct GNUNET_HELLO_Message *hello,
512 struct GetUriContext *guc = cls;
519 _("Error in communication with PEERINFO service: %s\n"),
521 GNUNET_free_non_null (guc->uri);
523 tt = GNUNET_SCHEDULER_add_now (&state_machine, NULL);
527 GNUNET_HELLO_iterate_addresses (hello, GNUNET_NO, &compose_uri, guc);
528 printf ("%s\n", (const char *) guc->uri);
532 /* ************************* import HELLO by URI ********************* */
536 * We're building a HELLO. Parse the next address from the
537 * parsing context and append it.
539 * @param cls the 'struct GNUNET_PEERINFO_HelloAddressParsingContext'
540 * @param max number of bytes available for HELLO construction
541 * @param buffer where to copy the next address (in binary format)
542 * @return number of bytes added to buffer
545 add_address_to_hello (void *cls, size_t max, void *buffer)
547 struct GNUNET_PEERINFO_HelloAddressParsingContext *ctx = cls;
551 char *plugin_address;
554 struct tm expiration_time;
555 time_t expiration_seconds;
556 struct GNUNET_TIME_Absolute expire;
557 struct GNUNET_TRANSPORT_PluginFunctions *papi;
560 struct GNUNET_HELLO_Address haddr;
563 if (NULL == ctx->pos)
565 if ('!' != ctx->pos[0])
567 ctx->ret = GNUNET_SYSERR;
572 memset (&expiration_time, 0, sizeof (expiration_time));
573 tname = strptime (ctx->pos,
579 ctx->ret = GNUNET_SYSERR;
580 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
581 _("Failed to parse HELLO message: missing expiration time\n"));
585 expiration_seconds = mktime (&expiration_time);
586 if (expiration_seconds == (time_t) -1)
588 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
589 _("Failed to parse HELLO message: invalid expiration time\n"));
590 ctx->ret = GNUNET_SYSERR;
594 expire.abs_value = expiration_seconds * 1000;
597 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
598 _("Failed to parse HELLO message: malformed\n"));
599 ctx->ret = GNUNET_SYSERR;
604 address = strchr (tname, (int) '!');
607 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
608 _("Failed to parse HELLO message: missing transport plugin\n"));
609 ctx->ret = GNUNET_SYSERR;
614 end = strchr (address, (int) '!');
616 plugin_name = GNUNET_strndup (tname, address - (tname+1));
617 papi = GPI_plugins_find (plugin_name);
620 /* Not an error - we might just not have the right plugin.
621 * Skip this part, advance to the next one and recurse.
622 * But only if this is not the end of string.
624 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
625 _("Plugin `%s' not found\n"),
627 GNUNET_free (plugin_name);
631 if (NULL == papi->string_to_address)
633 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
634 _("Plugin `%s' does not support URIs yet\n"),
636 GNUNET_free (plugin_name);
640 uri_address = GNUNET_strndup (address, end - address);
641 /* For URIs we use '(' and ')' instead of '[' and ']' as brackets are reserved
642 characters in URIs; need to convert back to '[]' for the plugin */
643 plugin_address = map_characters (uri_address, "()", "[]");
644 GNUNET_free (uri_address);
646 papi->string_to_address (papi->cls,
648 strlen (plugin_address) + 1,
652 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
653 _("Failed to parse `%s' as an address for plugin `%s'\n"),
656 GNUNET_free (plugin_name);
657 GNUNET_free (plugin_address);
660 GNUNET_free (plugin_address);
661 /* address.peer is unset - not used by add_address() */
662 haddr.address_length = addr_len;
663 haddr.address = addr;
664 haddr.transport_name = plugin_name;
665 ret = GNUNET_HELLO_add_address (&haddr, expire, buffer, max);
667 GNUNET_free (plugin_name);
673 * Continuation called from 'GNUNET_PEERINFO_add_peer'
675 * @param cls closure, NULL
676 * @param emsg error message, NULL on success
679 add_continuation (void *cls,
685 _("Failure adding HELLO: %s\n"),
687 tt = GNUNET_SCHEDULER_add_now (&state_machine, NULL);
692 * Parse the PUT URI given at the command line and add it to our peerinfo
695 * @param put_uri URI string to parse
696 * @return GNUNET_OK on success, GNUNET_SYSERR if the URI was invalid, GNUNET_NO on other errors
699 parse_hello_uri (const char *put_uri)
703 struct GNUNET_HELLO_Message *hello;
704 struct GNUNET_PEERINFO_HelloAddressParsingContext ctx;
706 if (0 != strncmp (put_uri,
708 strlen (HELLO_URI_PREFIX)))
709 return GNUNET_SYSERR;
710 pks = &put_uri[strlen (HELLO_URI_PREFIX)];
711 exc = strstr (pks, "!");
713 if (GNUNET_OK != GNUNET_STRINGS_string_to_data (pks,
714 (NULL == exc) ? strlen (pks) : (exc - pks),
715 (unsigned char *) &my_public_key,
716 sizeof (my_public_key)))
717 return GNUNET_SYSERR;
720 hello = GNUNET_HELLO_create (&my_public_key, &add_address_to_hello, &ctx);
724 /* WARNING: this adds the address from URI WITHOUT verification! */
725 if (GNUNET_OK == ctx.ret)
726 ac = GNUNET_PEERINFO_add_peer (peerinfo, hello, &add_continuation, NULL);
728 tt = GNUNET_SCHEDULER_add_now (&state_machine, NULL);
732 /* wait 1s to give peerinfo operation a chance to succeed */
733 /* FIXME: current peerinfo API sucks to require this; not to mention
734 that we get no feedback to determine if the operation actually succeeded */
739 /* ************************ Main state machine ********************* */
743 * Main state machine that goes over all options and
744 * runs the next requested function.
747 * @param tc scheduler context
750 shutdown_task (void *cls,
751 const struct GNUNET_SCHEDULER_TaskContext *tc)
753 struct PrintContext *pc;
754 struct AddressRecord *ar;
759 GNUNET_PEERINFO_add_peer_cancel (ac);
762 if (GNUNET_SCHEDULER_NO_TASK != tt)
764 GNUNET_SCHEDULER_cancel (tt);
765 tt = GNUNET_SCHEDULER_NO_TASK;
769 GNUNET_PEERINFO_iterate_cancel (pic);
772 while (NULL != (pc = pc_head))
774 GNUNET_CONTAINER_DLL_remove (pc_head,
777 for (i=0;i<pc->address_list_size;i++)
779 ar = &pc->address_list[i];
780 GNUNET_free_non_null (ar->result);
781 if (NULL != ar->atsc)
783 GNUNET_TRANSPORT_address_to_string_cancel (ar->atsc);
787 GNUNET_free_non_null (pc->address_list);
790 GPI_plugins_unload ();
791 if (NULL != peerinfo)
793 GNUNET_PEERINFO_disconnect (peerinfo);
800 * Main function that will be run by the scheduler.
803 * @param args remaining command-line arguments
804 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
805 * @param c configuration
808 run (void *cls, char *const *args, const char *cfgfile,
809 const struct GNUNET_CONFIGURATION_Handle *c)
811 struct GNUNET_CRYPTO_RsaPrivateKey *priv;
817 FPRINTF (stderr, _("Invalid command line argument `%s'\n"), args[0]);
820 peerinfo = GNUNET_PEERINFO_connect (cfg);
821 if (peerinfo == NULL)
823 FPRINTF (stderr, "%s", _("Could not access PEERINFO service. Exiting.\n"));
826 if ( (GNUNET_YES == get_self) || (GNUNET_YES == get_uri) )
828 /* load private key */
830 GNUNET_CONFIGURATION_get_value_filename (cfg, "GNUNETD", "HOSTKEY",
833 FPRINTF (stderr, _("Could not find option `%s:%s' in configuration.\n"),
834 "GNUNETD", "HOSTKEYFILE");
838 if (NULL == (priv = GNUNET_CRYPTO_rsa_key_create_from_file (fn)))
840 FPRINTF (stderr, _("Loading hostkey from `%s' failed.\n"), fn);
845 GNUNET_CRYPTO_rsa_key_get_public (priv, &my_public_key);
846 GNUNET_CRYPTO_rsa_key_free (priv);
847 GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key), &my_peer_identity.hashPubKey);
850 tt = GNUNET_SCHEDULER_add_now (&state_machine, NULL);
851 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
858 * Main state machine that goes over all options and
859 * runs the next requested function.
862 * @param tc scheduler context
865 state_machine (void *cls,
866 const struct GNUNET_SCHEDULER_TaskContext *tc)
868 tt = GNUNET_SCHEDULER_NO_TASK;
872 GPI_plugins_load (cfg);
873 if (GNUNET_SYSERR == parse_hello_uri (put_uri))
875 _("Invalid URI `%s'\n"),
877 GNUNET_free (put_uri);
881 if (GNUNET_YES == get_info)
883 get_info = GNUNET_NO;
884 GPI_plugins_load (cfg);
885 pic = GNUNET_PEERINFO_iterate (peerinfo, NULL,
887 &print_peer_info, NULL);
890 if (GNUNET_YES == get_self)
892 struct GNUNET_CRYPTO_HashAsciiEncoded enc;
894 get_self = GNUNET_NO;
895 GNUNET_CRYPTO_hash_to_enc (&my_peer_identity.hashPubKey, &enc);
897 printf ("%s\n", (char *) &enc);
899 printf (_("I am peer `%s'.\n"), (const char *) &enc);
901 if (GNUNET_YES == get_uri)
903 struct GetUriContext *guc;
906 guc = GNUNET_malloc (sizeof (struct GetUriContext));
907 pkey = GNUNET_CRYPTO_rsa_public_key_to_string (&my_public_key);
908 GNUNET_asprintf (&guc->uri,
913 GPI_plugins_load (cfg);
914 pic = GNUNET_PEERINFO_iterate (peerinfo, &my_peer_identity,
920 GNUNET_SCHEDULER_shutdown ();
925 * The main function to obtain peer information.
927 * @param argc number of arguments from the command line
928 * @param argv command line arguments
929 * @return 0 ok, 1 on error
932 main (int argc, char *const *argv)
934 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
935 {'n', "numeric", NULL,
936 gettext_noop ("don't resolve host names"),
937 0, &GNUNET_GETOPT_set_one, &no_resolve},
939 gettext_noop ("output only the identity strings"),
940 0, &GNUNET_GETOPT_set_one, &be_quiet},
942 gettext_noop ("output our own identity only"),
943 0, &GNUNET_GETOPT_set_one, &get_self},
945 gettext_noop ("list all known peers"),
946 0, &GNUNET_GETOPT_set_one, &get_info},
947 {'g', "get-hello", NULL,
948 gettext_noop ("also output HELLO uri(s)"),
949 0, &GNUNET_GETOPT_set_one, &get_uri},
950 {'p', "put-hello", "HELLO",
951 gettext_noop ("add given HELLO uri to the database"),
952 1, &GNUNET_GETOPT_set_string, &put_uri},
953 GNUNET_GETOPT_OPTION_END
956 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
960 GNUNET_PROGRAM_run (argc, argv, "gnunet-peerinfo",
961 gettext_noop ("Print information about peers."),
962 options, &run, NULL)) ? 0 : 1;
965 /* end of gnunet-peerinfo.c */