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_program_lib.h"
31 #include "gnunet_hello_lib.h"
32 #include "gnunet_transport_service.h"
33 #include "gnunet_peerinfo_service.h"
34 #include "gnunet-peerinfo_plugins.h"
37 * How long until we time out during peerinfo iterations?
39 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
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;
57 * Address expiration time
59 struct GNUNET_TIME_Absolute expiration;
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).
115 * Hello was friend only, GNUNET_YES or GNUNET_NO
125 static int no_resolve;
135 static int include_friend_only;
150 static int default_operation;
160 static char *put_uri;
165 static char *dump_hello;
168 * Handle to peerinfo service.
170 static struct GNUNET_PEERINFO_Handle *peerinfo;
173 * Configuration handle.
175 static const struct GNUNET_CONFIGURATION_Handle *cfg;
178 * Main state machine task (if active).
180 static GNUNET_SCHEDULER_TaskIdentifier tt;
183 * Current iterator context (if active, otherwise NULL).
185 static struct GNUNET_PEERINFO_IteratorContext *pic;
190 static struct GNUNET_PeerIdentity my_peer_identity;
193 * Head of list of print contexts.
195 static struct PrintContext *pc_head;
198 * Tail of list of print contexts.
200 static struct PrintContext *pc_tail;
203 * Handle to current 'GNUNET_PEERINFO_add_peer' operation.
205 static struct GNUNET_PEERINFO_AddContext *ac;
209 * Main state machine that goes over all options and
210 * runs the next requested function.
216 state_machine (void *cls,
217 const struct GNUNET_SCHEDULER_TaskContext *tc);
220 /* ********************* 'get_info' ******************* */
223 * Print the collected address information to the console and free 'pc'.
225 * @param pc printing context
228 dump_pc (struct PrintContext *pc)
232 printf (_("%sPeer `%s'\n"),
233 (GNUNET_YES == pc->friend_only) ? "F2F: " : "",
234 GNUNET_i2s_full (&pc->peer));
235 for (i = 0; i < pc->num_addresses; i++)
237 if (NULL != pc->address_list[i].result)
239 printf (_("\tExpires: %s \t %s\n"), GNUNET_STRINGS_absolute_time_to_string(pc->address_list[i].expiration), pc->address_list[i].result);
240 GNUNET_free (pc->address_list[i].result);
244 GNUNET_free_non_null (pc->address_list);
245 GNUNET_CONTAINER_DLL_remove (pc_head,
249 if ( (NULL == pc_head) &&
251 tt = GNUNET_SCHEDULER_add_now (&state_machine, NULL);
255 /* ************************* list all known addresses **************** */
259 * Function to call with a human-readable format of an address
262 * @param address NULL on error, otherwise 0-terminated printable UTF-8 string
265 process_resolved_address (void *cls, const char *address, int res)
267 struct AddressRecord * ar = cls;
268 struct PrintContext *pc = ar->pc;
272 if (NULL == ar->result)
273 ar->result = GNUNET_strdup (address);
277 if (GNUNET_SYSERR == res)
280 _("Failure: Cannot convert address to string for peer `%s'\n"),
281 GNUNET_i2s (&ar->pc->peer));
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;
323 GNUNET_assert (0 < pc->off);
324 ar = &pc->address_list[--pc->off];
326 ar->expiration = expiration;
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 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 friend_only = GNUNET_NO;
366 friend_only = GNUNET_HELLO_is_friend_only (hello);
367 if ((GNUNET_YES == be_quiet) || (NULL == hello))
370 (GNUNET_YES == friend_only) ? "F2F: " : "",
371 GNUNET_i2s_full (peer));
374 pc = GNUNET_new (struct PrintContext);
375 GNUNET_CONTAINER_DLL_insert (pc_head,
379 pc->friend_only = friend_only;
380 GNUNET_HELLO_iterate_addresses (hello,
389 pc->address_list_size = pc->off;
390 pc->address_list = GNUNET_malloc (sizeof (struct AddressRecord) * pc->off);
391 GNUNET_HELLO_iterate_addresses (hello, GNUNET_NO,
395 /* ************************* DUMP Hello ************************** */
397 static int count_addr(void *cls,
398 const struct GNUNET_HELLO_Address *address,
399 struct GNUNET_TIME_Absolute expiration)
407 * Write Hello of my peer to a file.
409 * @param cls the 'struct GetUriContext'
410 * @param peer identity of the peer (unused)
411 * @param hello addresses of the peer
412 * @param err_msg error message
415 dump_my_hello (void *cls, const struct GNUNET_PeerIdentity *peer,
416 const struct GNUNET_HELLO_Message *hello,
426 _("Error in communication with PEERINFO service: %s\n"),
428 tt = GNUNET_SCHEDULER_add_now (&state_machine, NULL);
435 _("Failure: Did not receive %s\n"), "HELLO");
439 size = GNUNET_HELLO_size (hello);
443 _("Failure: Received invalid %s\n"), "HELLO");
446 if (GNUNET_SYSERR == GNUNET_DISK_fn_write (dump_hello, hello, size,
447 GNUNET_DISK_PERM_USER_READ |
448 GNUNET_DISK_PERM_USER_WRITE |
449 GNUNET_DISK_PERM_GROUP_READ |
450 GNUNET_DISK_PERM_OTHER_READ))
452 FPRINTF (stderr, _("Failed to write HELLO with %u bytes to file `%s'\n"),
454 if (0 != UNLINK (dump_hello))
455 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING |
456 GNUNET_ERROR_TYPE_BULK, "unlink", dump_hello);
460 GNUNET_HELLO_iterate_addresses (hello, GNUNET_NO, count_addr, &c_addr);
465 _("Wrote %s HELLO containing %u addresses with %u bytes to file `%s'\n"),
466 (GNUNET_YES == GNUNET_HELLO_is_friend_only(hello)) ? "friend-only": "public",
467 c_addr, size, dump_hello);
470 GNUNET_free (dump_hello);
476 /* ************************* GET URI ************************** */
480 * Print URI of the peer.
482 * @param cls the 'struct GetUriContext'
483 * @param peer identity of the peer (unused)
484 * @param hello addresses of the peer
485 * @param err_msg error message
488 print_my_uri (void *cls, const struct GNUNET_PeerIdentity *peer,
489 const struct GNUNET_HELLO_Message *hello,
497 _("Error in communication with PEERINFO service: %s\n"),
499 tt = GNUNET_SCHEDULER_add_now (&state_machine, NULL);
505 char *uri = GNUNET_HELLO_compose_uri(hello, &GPI_plugins_find);
507 printf ("%s\n", (const char *) uri);
513 /* ************************* import HELLO by URI ********************* */
517 * Continuation called from 'GNUNET_PEERINFO_add_peer'
519 * @param cls closure, NULL
520 * @param emsg error message, NULL on success
523 add_continuation (void *cls,
529 _("Failure adding HELLO: %s\n"),
531 tt = GNUNET_SCHEDULER_add_now (&state_machine, NULL);
536 * Parse the PUT URI given at the command line and add it to our peerinfo
539 * @param put_uri URI string to parse
540 * @return GNUNET_OK on success, GNUNET_SYSERR if the URI was invalid, GNUNET_NO on other errors
543 parse_hello_uri (const char *put_uri)
545 struct GNUNET_HELLO_Message *hello = NULL;
547 int ret = GNUNET_HELLO_parse_uri(put_uri, &my_peer_identity.public_key,
548 &hello, &GPI_plugins_find);
551 /* WARNING: this adds the address from URI WITHOUT verification! */
552 if (GNUNET_OK == ret)
553 ac = GNUNET_PEERINFO_add_peer (peerinfo, hello, &add_continuation, NULL);
555 tt = GNUNET_SCHEDULER_add_now (&state_machine, NULL);
559 /* wait 1s to give peerinfo operation a chance to succeed */
560 /* FIXME: current peerinfo API sucks to require this; not to mention
561 that we get no feedback to determine if the operation actually succeeded */
566 /* ************************ Main state machine ********************* */
570 * Main state machine that goes over all options and
571 * runs the next requested function.
574 * @param tc scheduler context
577 shutdown_task (void *cls,
578 const struct GNUNET_SCHEDULER_TaskContext *tc)
580 struct PrintContext *pc;
581 struct AddressRecord *ar;
586 GNUNET_PEERINFO_add_peer_cancel (ac);
589 if (GNUNET_SCHEDULER_NO_TASK != tt)
591 GNUNET_SCHEDULER_cancel (tt);
592 tt = GNUNET_SCHEDULER_NO_TASK;
596 GNUNET_PEERINFO_iterate_cancel (pic);
599 while (NULL != (pc = pc_head))
601 GNUNET_CONTAINER_DLL_remove (pc_head,
604 for (i=0;i<pc->address_list_size;i++)
606 ar = &pc->address_list[i];
607 GNUNET_free_non_null (ar->result);
608 if (NULL != ar->atsc)
610 GNUNET_TRANSPORT_address_to_string_cancel (ar->atsc);
614 GNUNET_free_non_null (pc->address_list);
617 GPI_plugins_unload ();
618 if (NULL != peerinfo)
620 GNUNET_PEERINFO_disconnect (peerinfo);
626 * Function called with the result of the check if the 'peerinfo'
627 * service is running.
629 * @param cls closure with our configuration
630 * @param result #GNUNET_YES if transport is running
633 testservice_task (void *cls, int result)
635 struct GNUNET_CRYPTO_EddsaPrivateKey *priv;
638 if (GNUNET_YES != result)
640 FPRINTF (stderr, _("Service `%s' is not running, please start GNUnet\n"), "peerinfo");
644 if (NULL == (peerinfo = GNUNET_PEERINFO_connect (cfg)))
646 FPRINTF (stderr, "%s", _("Could not access PEERINFO service. Exiting.\n"));
649 if ( (GNUNET_YES == get_self) || (GNUNET_YES == get_uri) || (NULL != dump_hello) )
651 /* load private key */
652 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "PEER",
656 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, "PEER", "PRIVATE_KEY");
659 if (NULL == (priv = GNUNET_CRYPTO_eddsa_key_create_from_file (fn)))
661 FPRINTF (stderr, _("Loading hostkey from `%s' failed.\n"), fn);
666 GNUNET_CRYPTO_eddsa_key_get_public (priv,
667 &my_peer_identity.public_key);
671 tt = GNUNET_SCHEDULER_add_now (&state_machine, NULL);
672 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
679 * Main function that will be run by the scheduler.
682 * @param args remaining command-line arguments
683 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
684 * @param c configuration
687 run (void *cls, char *const *args, const char *cfgfile,
688 const struct GNUNET_CONFIGURATION_Handle *c)
691 if ( (NULL != args[0]) &&
693 (args[0] == strcasestr (args[0], "gnunet://hello/")) )
695 put_uri = GNUNET_strdup (args[0]);
701 _("Invalid command line argument `%s'\n"),
706 GNUNET_CLIENT_service_test ("peerinfo", cfg, GNUNET_TIME_UNIT_SECONDS,
707 &testservice_task, (void *) cfg);
712 * Main state machine that goes over all options and
713 * runs the next requested function.
716 * @param tc scheduler context
719 state_machine (void *cls,
720 const struct GNUNET_SCHEDULER_TaskContext *tc)
722 tt = GNUNET_SCHEDULER_NO_TASK;
726 GPI_plugins_load (cfg);
727 if (GNUNET_SYSERR == parse_hello_uri (put_uri))
730 _("Invalid URI `%s'\n"),
732 GNUNET_SCHEDULER_shutdown ();
734 GNUNET_free (put_uri);
737 else if (GNUNET_YES == get_info)
739 get_info = GNUNET_NO;
740 GPI_plugins_load (cfg);
741 pic = GNUNET_PEERINFO_iterate (peerinfo, include_friend_only, NULL,
743 &print_peer_info, NULL);
745 else if (GNUNET_YES == get_self)
747 get_self = GNUNET_NO;
750 GNUNET_i2s_full (&my_peer_identity));
752 printf (_("I am peer `%s'.\n"),
753 GNUNET_i2s_full (&my_peer_identity));
754 tt = GNUNET_SCHEDULER_add_now (&state_machine, NULL);
756 else if (GNUNET_YES == get_uri)
758 GPI_plugins_load (cfg);
759 pic = GNUNET_PEERINFO_iterate (peerinfo, include_friend_only, &my_peer_identity,
760 TIMEOUT, &print_my_uri, NULL);
763 else if (NULL != dump_hello)
765 pic = GNUNET_PEERINFO_iterate (peerinfo, include_friend_only, &my_peer_identity,
766 TIMEOUT, &dump_my_hello, NULL);
768 else if (GNUNET_YES == default_operation)
770 /* default operation list all */
771 default_operation = GNUNET_NO;
772 get_info = GNUNET_YES;
773 tt = GNUNET_SCHEDULER_add_now (&state_machine, NULL);
777 GNUNET_SCHEDULER_shutdown ();
779 default_operation = GNUNET_NO;
784 * The main function to obtain peer information.
786 * @param argc number of arguments from the command line
787 * @param argv command line arguments
788 * @return 0 ok, 1 on error
791 main (int argc, char *const *argv)
793 default_operation = GNUNET_YES;
794 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
795 {'n', "numeric", NULL,
796 gettext_noop ("don't resolve host names"),
797 0, &GNUNET_GETOPT_set_one, &no_resolve},
799 gettext_noop ("output only the identity strings"),
800 0, &GNUNET_GETOPT_set_one, &be_quiet},
801 {'f', "friends", NULL,
802 gettext_noop ("include friend-only information"),
803 0, &GNUNET_GETOPT_set_one, &include_friend_only},
805 gettext_noop ("output our own identity only"),
806 0, &GNUNET_GETOPT_set_one, &get_self},
808 gettext_noop ("list all known peers"),
809 0, &GNUNET_GETOPT_set_one, &get_info},
810 {'d', "dump-hello", NULL,
811 gettext_noop ("dump hello to file"),
812 1, &GNUNET_GETOPT_set_string, &dump_hello},
813 {'g', "get-hello", NULL,
814 gettext_noop ("also output HELLO uri(s)"),
815 0, &GNUNET_GETOPT_set_one, &get_uri},
816 {'p', "put-hello", "HELLO",
817 gettext_noop ("add given HELLO uri to the database"),
818 1, &GNUNET_GETOPT_set_string, &put_uri},
819 GNUNET_GETOPT_OPTION_END
823 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
827 GNUNET_PROGRAM_run (argc, argv, "gnunet-peerinfo",
828 gettext_noop ("Print information about peers."),
829 options, &run, NULL)) ? 0 : 1;
830 GNUNET_free ((void*) argv);
834 /* end of gnunet-peerinfo.c */