2 This file is part of GNUnet.
3 Copyright (C) 2001-2014 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., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
22 * @file peerinfo-tool/gnunet-peerinfo.c
23 * @brief Print information about other known peers.
24 * @author Christian Grothoff
25 * @author Matthias Wachs
28 #include "gnunet_util_lib.h"
29 #include "gnunet_hello_lib.h"
30 #include "gnunet_transport_service.h"
31 #include "gnunet_peerinfo_service.h"
32 #include "gnunet-peerinfo_plugins.h"
35 * How long until we time out during peerinfo iterations?
37 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
40 * Structure we use to collect printable address information.
45 * Record we keep for each printable address.
50 * Current address-to-string context (if active, otherwise NULL).
52 struct GNUNET_TRANSPORT_AddressToStringContext *atsc;
55 * Address expiration time
57 struct GNUNET_TIME_Absolute expiration;
65 * Print context this address record belongs to.
67 struct PrintContext *pc;
72 * Structure we use to collect printable address information.
80 struct PrintContext *next;
85 struct PrintContext *prev;
88 * Identity of the peer.
90 struct GNUNET_PeerIdentity peer;
93 * List of printable addresses.
95 struct AddressRecord *address_list;
98 * Number of completed addresses in @e address_list.
100 unsigned int num_addresses;
103 * Number of addresses allocated in @e address_list.
105 unsigned int address_list_size;
108 * Current offset in @e address_list (counted down).
113 * Hello was friend only, #GNUNET_YES or #GNUNET_NO
123 static int no_resolve;
133 static int include_friend_only;
148 static int default_operation;
158 static char *put_uri;
163 static char *dump_hello;
166 * Handle to peerinfo service.
168 static struct GNUNET_PEERINFO_Handle *peerinfo;
171 * Configuration handle.
173 static const struct GNUNET_CONFIGURATION_Handle *cfg;
176 * Main state machine task (if active).
178 static struct GNUNET_SCHEDULER_Task * tt;
181 * Pending #GNUNET_TRANSPORT_get_hello() operation.
183 static struct GNUNET_TRANSPORT_GetHelloHandle *gh;
186 * Connection to transport service.
188 static struct GNUNET_TRANSPORT_Handle *transport;
191 * Current iterator context (if active, otherwise NULL).
193 static struct GNUNET_PEERINFO_IteratorContext *pic;
198 static struct GNUNET_PeerIdentity my_peer_identity;
201 * Head of list of print contexts.
203 static struct PrintContext *pc_head;
206 * Tail of list of print contexts.
208 static struct PrintContext *pc_tail;
211 * Handle to current #GNUNET_PEERINFO_add_peer() operation.
213 static struct GNUNET_PEERINFO_AddContext *ac;
216 * Hello of this peer (if initialized).
218 static struct GNUNET_HELLO_Message *my_hello;
222 * Main state machine that goes over all options and
223 * runs the next requested function.
229 state_machine (void *cls,
230 const struct GNUNET_SCHEDULER_TaskContext *tc);
233 /* ********************* 'get_info' ******************* */
236 * Print the collected address information to the console and free @a pc.
238 * @param pc printing context
241 dump_pc (struct PrintContext *pc)
245 printf (_("%sPeer `%s'\n"),
246 (GNUNET_YES == pc->friend_only) ? "F2F: " : "",
247 GNUNET_i2s_full (&pc->peer));
248 for (i = 0; i < pc->num_addresses; i++)
250 if (NULL != pc->address_list[i].result)
252 printf (_("\tExpires: %s \t %s\n"),
253 GNUNET_STRINGS_absolute_time_to_string (pc->address_list[i].expiration),
254 pc->address_list[i].result);
255 GNUNET_free (pc->address_list[i].result);
259 GNUNET_free_non_null (pc->address_list);
260 GNUNET_CONTAINER_DLL_remove (pc_head,
264 if ( (NULL == pc_head) &&
266 tt = GNUNET_SCHEDULER_add_now (&state_machine,
271 /* ************************* list all known addresses **************** */
275 * Function to call with a human-readable format of an address
278 * @param address NULL on error, otherwise 0-terminated printable UTF-8 string
279 * @param res result of the address to string conversion:
280 * if #GNUNET_OK: address was valid (conversion to
281 * string might still have failed)
282 * if #GNUNET_SYSERR: address is invalid
285 process_resolved_address (void *cls,
289 struct AddressRecord *ar = cls;
290 struct PrintContext *pc = ar->pc;
294 if (0 != strlen (address))
296 if (NULL != ar->result)
297 GNUNET_free (ar->result);
298 ar->result = GNUNET_strdup (address);
303 if (GNUNET_SYSERR == res)
304 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
305 _("Failure: Cannot convert address to string for peer `%s'\n"),
306 GNUNET_i2s (&ar->pc->peer));
308 if (pc->num_addresses == pc->address_list_size)
314 * Iterator callback to go over all addresses and count them.
316 * @param cls `struct PrintContext *` with `off` to increment
317 * @param address the address
318 * @param expiration expiration time
319 * @return #GNUNET_OK to keep the address and continue
322 count_address (void *cls,
323 const struct GNUNET_HELLO_Address *address,
324 struct GNUNET_TIME_Absolute expiration)
326 struct PrintContext *pc = cls;
334 * Iterator callback to go over all addresses.
337 * @param address the address
338 * @param expiration expiration time
339 * @return #GNUNET_OK to keep the address and continue
342 print_address (void *cls,
343 const struct GNUNET_HELLO_Address *address,
344 struct GNUNET_TIME_Absolute expiration)
346 struct PrintContext *pc = cls;
347 struct AddressRecord *ar;
349 GNUNET_assert (0 < pc->off);
350 ar = &pc->address_list[--pc->off];
352 ar->expiration = expiration;
353 GNUNET_asprintf (&ar->result,
355 address->transport_name,
356 address->address_length,
357 address->local_info);
358 ar->atsc = GNUNET_TRANSPORT_address_to_string (cfg,
362 &process_resolved_address, ar);
368 * Print information about the peer. Currently prints the `struct
369 * GNUNET_PeerIdentity` and the transport address.
371 * @param cls the `struct PrintContext *`
372 * @param peer identity of the peer
373 * @param hello addresses of the peer
374 * @param err_msg error message
377 print_peer_info (void *cls,
378 const struct GNUNET_PeerIdentity *peer,
379 const struct GNUNET_HELLO_Message *hello,
382 struct PrintContext *pc;
387 pic = NULL; /* end of iteration */
391 _("Error in communication with PEERINFO service: %s\n"),
395 tt = GNUNET_SCHEDULER_add_now (&state_machine, NULL);
398 friend_only = GNUNET_NO;
400 friend_only = GNUNET_HELLO_is_friend_only (hello);
401 if ( (GNUNET_YES == be_quiet) ||
405 (GNUNET_YES == friend_only) ? "F2F: " : "",
406 GNUNET_i2s_full (peer));
409 pc = GNUNET_new (struct PrintContext);
410 GNUNET_CONTAINER_DLL_insert (pc_head,
414 pc->friend_only = friend_only;
415 GNUNET_HELLO_iterate_addresses (hello,
424 pc->address_list_size = pc->off;
425 pc->address_list = GNUNET_malloc (sizeof (struct AddressRecord) * pc->off);
426 GNUNET_HELLO_iterate_addresses (hello,
432 /* ************************* DUMP Hello ************************** */
435 * Count the number of addresses in the HELLO.
437 * @param cls pointer to an `int *` used for the counter
438 * @param address an address to count
439 * @param expiration (unused)
443 count_addr (void *cls,
444 const struct GNUNET_HELLO_Address *address,
445 struct GNUNET_TIME_Absolute expiration)
455 * Write HELLO of my peer to a file.
457 * @param cls the `struct GetUriContext *`
458 * @param peer identity of the peer (unused)
459 * @param hello addresses of the peer
460 * @param err_msg error message
468 size = GNUNET_HELLO_size (my_hello);
472 _("Failure: Received invalid %s\n"),
477 GNUNET_DISK_fn_write (dump_hello,
480 GNUNET_DISK_PERM_USER_READ |
481 GNUNET_DISK_PERM_USER_WRITE |
482 GNUNET_DISK_PERM_GROUP_READ |
483 GNUNET_DISK_PERM_OTHER_READ))
486 _("Failed to write HELLO with %u bytes to file `%s'\n"),
489 if (0 != UNLINK (dump_hello))
490 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING |
491 GNUNET_ERROR_TYPE_BULK,
497 GNUNET_HELLO_iterate_addresses (my_hello,
505 _("Wrote %s HELLO containing %u addresses with %u bytes to file `%s'\n"),
506 (GNUNET_YES == GNUNET_HELLO_is_friend_only (my_hello)) ? "friend-only": "public",
511 GNUNET_free (dump_hello);
516 /* ************************* GET URI ************************** */
520 * Print URI of the peer.
522 * @param cls the `struct GetUriContext *`
523 * @param peer identity of the peer (unused)
524 * @param hello addresses of the peer
525 * @param err_msg error message
528 print_my_uri (void *cls,
529 const struct GNUNET_PeerIdentity *peer,
530 const struct GNUNET_HELLO_Message *hello,
540 _("Error in communication with PEERINFO service: %s\n"),
542 tt = GNUNET_SCHEDULER_add_now (&state_machine, NULL);
548 uri = GNUNET_HELLO_compose_uri (hello,
559 /* ************************* import HELLO by URI ********************* */
563 * Continuation called from #GNUNET_PEERINFO_add_peer()
565 * @param cls closure, NULL
566 * @param emsg error message, NULL on success
569 add_continuation (void *cls,
575 _("Failure adding HELLO: %s\n"),
577 tt = GNUNET_SCHEDULER_add_now (&state_machine, NULL);
582 * Parse the PUT URI given at the command line and add it to our peerinfo
585 * @param put_uri URI string to parse
586 * @return #GNUNET_OK on success,
587 * #GNUNET_SYSERR if the URI was invalid,
588 * #GNUNET_NO on other errors
591 parse_hello_uri (const char *put_uri)
593 struct GNUNET_HELLO_Message *hello = NULL;
595 int ret = GNUNET_HELLO_parse_uri (put_uri,
596 &my_peer_identity.public_key,
602 /* WARNING: this adds the address from URI WITHOUT verification! */
603 if (GNUNET_OK == ret)
604 ac = GNUNET_PEERINFO_add_peer (peerinfo, hello,
608 tt = GNUNET_SCHEDULER_add_now (&state_machine, NULL);
615 /* ************************ Main state machine ********************* */
619 * Main state machine that goes over all options and
620 * runs the next requested function.
623 * @param tc scheduler context
626 shutdown_task (void *cls,
627 const struct GNUNET_SCHEDULER_TaskContext *tc)
629 struct PrintContext *pc;
630 struct AddressRecord *ar;
635 GNUNET_PEERINFO_add_peer_cancel (ac);
640 GNUNET_SCHEDULER_cancel (tt);
645 GNUNET_PEERINFO_iterate_cancel (pic);
650 GNUNET_TRANSPORT_get_hello_cancel (gh);
653 if (NULL != transport)
655 GNUNET_TRANSPORT_disconnect (transport);
658 while (NULL != (pc = pc_head))
660 GNUNET_CONTAINER_DLL_remove (pc_head,
663 for (i=0;i<pc->address_list_size;i++)
665 ar = &pc->address_list[i];
666 GNUNET_free_non_null (ar->result);
667 if (NULL != ar->atsc)
669 GNUNET_TRANSPORT_address_to_string_cancel (ar->atsc);
673 GNUNET_free_non_null (pc->address_list);
676 GPI_plugins_unload ();
677 if (NULL != peerinfo)
679 GNUNET_PEERINFO_disconnect (peerinfo);
682 if (NULL != my_hello)
684 GNUNET_free (my_hello);
691 * Function called with our peer's HELLO message.
692 * Used to obtain our peer's public key.
695 * @param hello the HELLO message
698 hello_callback (void *cls,
699 const struct GNUNET_MessageHeader *hello)
704 _("Failed to get my own HELLO from this peer!\n"));
705 GNUNET_SCHEDULER_shutdown ();
708 my_hello = (struct GNUNET_HELLO_Message *) GNUNET_copy_message (hello);
709 GNUNET_assert (GNUNET_OK ==
710 GNUNET_HELLO_get_id (my_hello,
712 GNUNET_TRANSPORT_get_hello_cancel (gh);
714 GNUNET_TRANSPORT_disconnect (transport);
716 if (NULL != dump_hello)
718 tt = GNUNET_SCHEDULER_add_now (&state_machine, NULL);
723 * Function called with the result of the check if the PEERINFO
724 * service is running.
726 * @param cls closure with our configuration
727 * @param result #GNUNET_YES if PEERINFO is running
730 testservice_task (void *cls,
733 if (GNUNET_YES != result)
736 _("Service `%s' is not running, please start GNUnet\n"),
741 if (NULL == (peerinfo = GNUNET_PEERINFO_connect (cfg)))
745 _("Could not access PEERINFO service. Exiting.\n"));
748 if ( (GNUNET_YES == get_self) ||
749 (GNUNET_YES == get_uri) ||
750 (NULL != dump_hello) )
752 transport = GNUNET_TRANSPORT_connect (cfg,
756 gh = GNUNET_TRANSPORT_get_hello (transport,
762 tt = GNUNET_SCHEDULER_add_now (&state_machine, NULL);
764 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
771 * Main function that will be run by the scheduler.
774 * @param args remaining command-line arguments
775 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
776 * @param c configuration
782 const struct GNUNET_CONFIGURATION_Handle *c)
785 if ( (NULL != args[0]) &&
787 (args[0] == strcasestr (args[0], "gnunet://hello/")) )
789 put_uri = GNUNET_strdup (args[0]);
795 _("Invalid command line argument `%s'\n"),
800 GNUNET_CLIENT_service_test ("peerinfo",
802 GNUNET_TIME_UNIT_SECONDS,
803 &testservice_task, (void *) cfg);
808 * Main state machine that goes over all options and
809 * runs the next requested function.
812 * @param tc scheduler context
815 state_machine (void *cls,
816 const struct GNUNET_SCHEDULER_TaskContext *tc)
822 GPI_plugins_load (cfg);
823 if (GNUNET_SYSERR == parse_hello_uri (put_uri))
826 _("Invalid URI `%s'\n"),
828 GNUNET_SCHEDULER_shutdown ();
830 GNUNET_free (put_uri);
833 else if (GNUNET_YES == get_info)
835 get_info = GNUNET_NO;
836 GPI_plugins_load (cfg);
837 pic = GNUNET_PEERINFO_iterate (peerinfo,
841 &print_peer_info, NULL);
843 else if (GNUNET_YES == get_self)
845 get_self = GNUNET_NO;
848 GNUNET_i2s_full (&my_peer_identity));
850 printf (_("I am peer `%s'.\n"),
851 GNUNET_i2s_full (&my_peer_identity));
852 tt = GNUNET_SCHEDULER_add_now (&state_machine, NULL);
854 else if (GNUNET_YES == get_uri)
856 GPI_plugins_load (cfg);
857 pic = GNUNET_PEERINFO_iterate (peerinfo,
861 &print_my_uri, NULL);
864 else if (GNUNET_YES == default_operation)
866 /* default operation list all */
867 default_operation = GNUNET_NO;
868 get_info = GNUNET_YES;
869 tt = GNUNET_SCHEDULER_add_now (&state_machine, NULL);
873 GNUNET_SCHEDULER_shutdown ();
875 default_operation = GNUNET_NO;
880 * The main function to obtain peer information.
882 * @param argc number of arguments from the command line
883 * @param argv command line arguments
884 * @return 0 ok, 1 on error
887 main (int argc, char *const *argv)
889 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
890 {'n', "numeric", NULL,
891 gettext_noop ("don't resolve host names"),
892 0, &GNUNET_GETOPT_set_one, &no_resolve},
894 gettext_noop ("output only the identity strings"),
895 0, &GNUNET_GETOPT_set_one, &be_quiet},
896 {'f', "friends", NULL,
897 gettext_noop ("include friend-only information"),
898 0, &GNUNET_GETOPT_set_one, &include_friend_only},
900 gettext_noop ("output our own identity only"),
901 0, &GNUNET_GETOPT_set_one, &get_self},
903 gettext_noop ("list all known peers"),
904 0, &GNUNET_GETOPT_set_one, &get_info},
905 {'d', "dump-hello", NULL,
906 gettext_noop ("dump hello to file"),
907 1, &GNUNET_GETOPT_set_string, &dump_hello},
908 {'g', "get-hello", NULL,
909 gettext_noop ("also output HELLO uri(s)"),
910 0, &GNUNET_GETOPT_set_one, &get_uri},
911 {'p', "put-hello", "HELLO",
912 gettext_noop ("add given HELLO uri to the database"),
913 1, &GNUNET_GETOPT_set_string, &put_uri},
914 GNUNET_GETOPT_OPTION_END
918 default_operation = GNUNET_YES;
919 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc,
925 GNUNET_PROGRAM_run (argc, argv, "gnunet-peerinfo",
926 gettext_noop ("Print information about peers."),
927 options, &run, NULL)) ? 0 : 1;
928 GNUNET_free ((void*) argv);
932 /* end of gnunet-peerinfo.c */