2 This file is part of GNUnet.
3 Copyright (C) 2012, 2017 GNUnet e.V.
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 cadet/gnunet-cadet.c
23 * @brief Print information about cadet tunnels and peers.
24 * @author Bartlomiej Polot
25 * @author Christian Grothoff
28 #include "gnunet_util_lib.h"
29 #include "gnunet_cadet_service.h"
36 static int request_peers;
46 static int request_tunnels;
51 static char *tunnel_id;
61 static char *channel_id;
64 * Port to listen on (-o).
66 static char *listen_port;
69 * Request echo service
74 * Request a debug dump
79 * Time of last echo request.
81 static struct GNUNET_TIME_Absolute echo_time;
84 * Task for next echo request.
86 static struct GNUNET_SCHEDULER_Task *echo_task;
91 static char *target_id;
96 static char *target_port = "default";
101 static struct GNUNET_CADET_Handle *mh;
106 static struct GNUNET_CADET_Channel *ch;
109 * HashCode of the given port string
111 static struct GNUNET_HashCode porthash;
114 * Data structure for ongoing reception of incoming virtual circuits.
116 struct GNUNET_CADET_Port *lp;
119 * Task for reading from stdin.
121 static struct GNUNET_SCHEDULER_Task *rd_task;
126 static struct GNUNET_SCHEDULER_Task *job;
130 * Wait for input on STDIO and send it out over the #ch.
137 * Convert encryption status to human readable string.
139 * @param status Encryption status.
141 * @return Human readable string.
144 enc_2s (uint16_t status)
163 * Convert connection status to human readable string.
165 * @param status Connection status.
167 * @return Human readable string.
170 conn_2s (uint16_t status)
192 * Task to shut down this application.
194 * @param cls Closure (unused).
197 shutdown_task (void *cls)
199 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
203 GNUNET_CADET_channel_destroy (ch);
208 GNUNET_CADET_disconnect (mh);
213 GNUNET_SCHEDULER_cancel (rd_task);
216 if (NULL != echo_task)
218 GNUNET_SCHEDULER_cancel (echo_task);
223 GNUNET_SCHEDULER_cancel (job);
230 * Task run in stdio mode, after some data is available at stdin.
232 * @param cls Closure (unused).
235 read_stdio (void *cls)
237 struct GNUNET_MQ_Envelope *env;
238 struct GNUNET_MessageHeader *msg;
248 GNUNET_SCHEDULER_shutdown();
251 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
252 "Read %u bytes from stdio\n",
253 (unsigned int) data_size);
254 env = GNUNET_MQ_msg_extra (msg,
256 GNUNET_MESSAGE_TYPE_CADET_CLI);
257 GNUNET_memcpy (&msg[1],
260 GNUNET_MQ_send (GNUNET_CADET_get_mq (ch),
262 if (GNUNET_NO == echo)
268 echo_time = GNUNET_TIME_absolute_get ();
274 * Wait for input on STDIO and send it out over the #ch.
279 struct GNUNET_NETWORK_FDSet *rs;
281 /* FIXME: why use 'rs' here, seems overly complicated... */
282 rs = GNUNET_NETWORK_fdset_create ();
283 GNUNET_NETWORK_fdset_set_native (rs,
285 rd_task = GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
286 GNUNET_TIME_UNIT_FOREVER_REL,
291 GNUNET_NETWORK_fdset_destroy (rs);
296 * Function called whenever a channel is destroyed. Should clean up
297 * any associated state.
299 * It must NOT call #GNUNET_CADET_channel_destroy on the channel.
302 * @param channel connection to the other end (henceforth invalid)
305 channel_ended (void *cls,
306 const struct GNUNET_CADET_Channel *channel)
308 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
310 GNUNET_assert (channel == ch);
312 GNUNET_SCHEDULER_shutdown ();
317 * Method called whenever another peer has added us to a channel
318 * the other peer initiated.
319 * Only called (once) upon reception of data with a message type which was
320 * subscribed to in #GNUNET_CADET_connect.
322 * A call to #GNUNET_CADET_channel_destroy causes the channel to be ignored.
323 * In this case the handler MUST return NULL.
326 * @param channel new handle to the channel
327 * @param initiator peer that started the channel
328 * @return initial channel context for the channel, we use @a channel
331 channel_incoming (void *cls,
332 struct GNUNET_CADET_Channel *channel,
333 const struct GNUNET_PeerIdentity *initiator)
335 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
336 "Incoming connection from %s\n",
337 GNUNET_i2s_full (initiator));
338 GNUNET_assert (NULL == ch);
339 GNUNET_assert (NULL != lp);
340 GNUNET_CADET_close_port (lp);
343 if (GNUNET_NO == echo)
350 * @brief Send an echo request to the remote peer.
352 * @param cls Closure (NULL).
355 send_echo (void *cls)
357 struct GNUNET_MQ_Envelope *env;
358 struct GNUNET_MessageHeader *msg;
363 env = GNUNET_MQ_msg (msg,
364 GNUNET_MESSAGE_TYPE_CADET_CLI);
365 GNUNET_MQ_send (GNUNET_CADET_get_mq (ch),
371 * Call CADET's monitor API, request debug dump on the service.
373 * @param cls Closure (unused).
376 request_dump (void *cls)
378 GNUNET_CADET_request_dump (mh);
379 GNUNET_SCHEDULER_shutdown ();
384 * Check data message sanity. Does nothing so far (all messages are OK).
386 * @param cls Closure (unused).
387 * @param message The message to check.
388 * @return #GNUNET_OK to keep the channel open,
389 * #GNUNET_SYSERR to close it (signal serious error).
392 check_data (void *cls,
393 const struct GNUNET_MessageHeader *message)
395 return GNUNET_OK; /* all is well-formed */
400 * Function called whenever a message is received.
402 * Each time the function must call #GNUNET_CADET_receive_done on the channel
403 * in order to receive the next message. This doesn't need to be immediate:
404 * can be delayed if some processing is done on the message.
407 * @param message The actual message.
410 handle_data (void *cls,
411 const struct GNUNET_MessageHeader *message)
413 size_t payload_size = ntohs (message->size) - sizeof (*message);
419 GNUNET_CADET_receive_done (ch);
420 if (GNUNET_YES == echo)
422 if (NULL != listen_port)
424 struct GNUNET_MQ_Envelope *env;
425 struct GNUNET_MessageHeader *msg;
427 env = GNUNET_MQ_msg_extra (msg,
429 GNUNET_MESSAGE_TYPE_CADET_CLI);
430 GNUNET_memcpy (&msg[1],
433 GNUNET_MQ_send (GNUNET_CADET_get_mq (ch),
439 struct GNUNET_TIME_Relative latency;
441 latency = GNUNET_TIME_absolute_get_duration (echo_time);
442 echo_time = GNUNET_TIME_UNIT_FOREVER_ABS;
443 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
445 GNUNET_STRINGS_relative_time_to_string (latency,
447 echo_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
453 len = ntohs (message->size) - sizeof (*message);
454 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
457 buf = (const char *) &message[1];
467 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
469 GNUNET_SCHEDULER_shutdown ();
478 * Method called to retrieve information about all peers in CADET, called
481 * After last peer has been reported, an additional call with NULL is done.
483 * @param cls Closure.
484 * @param peer Peer, or NULL on "EOF".
485 * @param tunnel Do we have a tunnel towards this peer?
486 * @param n_paths Number of known paths towards this peer.
487 * @param best_path How long is the best path?
488 * (0 = unknown, 1 = ourselves, 2 = neighbor)
491 peers_callback (void *cls,
492 const struct GNUNET_PeerIdentity *peer,
494 unsigned int n_paths,
495 unsigned int best_path)
499 GNUNET_SCHEDULER_shutdown();
503 "%s tunnel: %c, paths: %u\n",
504 GNUNET_i2s_full (peer),
511 * Method called to retrieve information about a specific peer
512 * known to the service.
514 * @param cls Closure.
515 * @param peer Peer ID.
516 * @param tunnel Do we have a tunnel towards this peer? #GNUNET_YES/#GNUNET_NO
517 * @param neighbor Is this a direct neighbor? #GNUNET_YES/#GNUNET_NO
518 * @param n_paths Number of paths known towards peer.
519 * @param paths Array of PEER_IDs representing all paths to reach the peer.
520 * Each path starts with the local peer.
521 * Each path ends with the destination peer (given in @c peer).
524 peer_callback (void *cls,
525 const struct GNUNET_PeerIdentity *peer,
528 unsigned int n_paths,
529 const struct GNUNET_PeerIdentity *paths)
532 const struct GNUNET_PeerIdentity *p;
535 "%s [TUNNEL: %s, NEIGHBOR: %s, PATHS: %u]\n",
536 GNUNET_i2s_full (peer),
538 neighbor ? "Y" : "N",
541 for (i = 0; i < n_paths && NULL != p;)
550 FPRINTF (stdout, "\n");
556 GNUNET_SCHEDULER_shutdown();
561 * Method called to retrieve information about all tunnels in CADET.
563 * @param cls Closure.
564 * @param peer Destination peer.
565 * @param channels Number of channels.
566 * @param connections Number of connections.
567 * @param estate Encryption state.
568 * @param cstate Connectivity state.
571 tunnels_callback (void *cls,
572 const struct GNUNET_PeerIdentity *peer,
573 unsigned int channels,
574 unsigned int connections,
580 GNUNET_SCHEDULER_shutdown();
584 "%s [ENC: %s, CON: %s] CHs: %u, CONNs: %u\n",
585 GNUNET_i2s_full (peer),
594 * Method called to retrieve information about a specific tunnel the cadet peer
595 * has established, o`r is trying to establish.
597 * @param cls Closure.
598 * @param peer Peer towards whom the tunnel is directed.
599 * @param n_channels Number of channels.
600 * @param n_connections Number of connections.
601 * @param channels Channels.
602 * @param connections Connections.
603 * @param estate Encryption status.
604 * @param cstate Connectivity status.
607 tunnel_callback (void *cls,
608 const struct GNUNET_PeerIdentity *peer,
609 unsigned int n_channels,
610 unsigned int n_connections,
611 const struct GNUNET_CADET_ChannelTunnelNumber *channels,
612 const struct GNUNET_CADET_ConnectionTunnelIdentifier *connections,
620 FPRINTF (stdout, "Tunnel %s\n", GNUNET_i2s_full (peer));
621 FPRINTF (stdout, "\t%u channels\n", n_channels);
622 for (i = 0; i < n_channels; i++)
623 FPRINTF (stdout, "\t\t%X\n", ntohl (channels[i].cn));
624 FPRINTF (stdout, "\t%u connections\n", n_connections);
625 for (i = 0; i < n_connections; i++)
626 FPRINTF (stdout, "\t\t%s\n", GNUNET_sh2s (&connections[i].connection_of_tunnel));
627 FPRINTF (stdout, "\tencryption state: %s\n", enc_2s (estate));
628 FPRINTF (stdout, "\tconnection state: %s\n", conn_2s (cstate));
630 GNUNET_SCHEDULER_shutdown ();
635 * Call CADET's meta API, get all peers known to a peer.
637 * @param cls Closure (unused).
640 get_peers (void *cls)
643 GNUNET_CADET_get_peers (mh, &peers_callback, NULL);
648 * Call CADET's monitor API, get info of one peer.
650 * @param cls Closure (unused).
653 show_peer (void *cls)
655 struct GNUNET_PeerIdentity pid;
659 GNUNET_CRYPTO_eddsa_public_key_from_string (peer_id,
664 _("Invalid peer ID `%s'\n"),
666 GNUNET_SCHEDULER_shutdown();
669 GNUNET_CADET_get_peer (mh, &pid, peer_callback, NULL);
674 * Call CADET's meta API, get all tunnels known to a peer.
676 * @param cls Closure (unused).
679 get_tunnels (void *cls)
682 GNUNET_CADET_get_tunnels (mh, &tunnels_callback, NULL);
687 * Call CADET's monitor API, get info of one tunnel.
689 * @param cls Closure (unused).
692 show_tunnel (void *cls)
694 struct GNUNET_PeerIdentity pid;
698 GNUNET_CRYPTO_eddsa_public_key_from_string (tunnel_id,
703 _("Invalid tunnel owner `%s'\n"),
705 GNUNET_SCHEDULER_shutdown ();
708 GNUNET_CADET_get_tunnel (mh,
716 * Call CADET's monitor API, get info of one channel.
718 * @param cls Closure (unused).
721 show_channel (void *cls)
729 * Call CADET's monitor API, get info of one connection.
731 * @param cls Closure (unused).
734 show_connection (void *cls)
742 * Main function that will be run by the scheduler.
745 * @param args remaining command-line arguments
746 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
747 * @param cfg configuration
753 const struct GNUNET_CONFIGURATION_Handle *cfg)
755 struct GNUNET_MQ_MessageHandler handlers[] = {
756 GNUNET_MQ_hd_var_size (data,
757 GNUNET_MESSAGE_TYPE_CADET_CLI,
758 struct GNUNET_MessageHeader,
760 GNUNET_MQ_handler_end ()
763 /* FIXME add option to monitor apps */
766 if (target_id && args[1])
767 target_port = args[1];
769 if ( (0 != (request_peers | request_tunnels)
772 || NULL != channel_id)
773 && target_id != NULL)
776 _("Extra arguments are not applicable "
777 "in combination with this option.\n"));
781 if (GNUNET_YES == dump)
783 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
784 "Requesting debug dump\n");
785 job = GNUNET_SCHEDULER_add_now (&request_dump,
788 else if (NULL != peer_id)
790 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
792 job = GNUNET_SCHEDULER_add_now (&show_peer,
795 else if (NULL != tunnel_id)
797 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
799 job = GNUNET_SCHEDULER_add_now (&show_tunnel,
802 else if (NULL != channel_id)
804 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
806 job = GNUNET_SCHEDULER_add_now (&show_channel,
809 else if (NULL != conn_id)
811 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
812 "Show connection\n");
813 job = GNUNET_SCHEDULER_add_now (&show_connection,
816 else if (GNUNET_YES == request_peers)
818 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
820 job = GNUNET_SCHEDULER_add_now (&get_peers,
823 else if (GNUNET_YES == request_tunnels)
825 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
826 "Show all tunnels\n");
827 job = GNUNET_SCHEDULER_add_now (&get_tunnels,
831 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
832 "Connecting to CADET service\n");
833 mh = GNUNET_CADET_connect (cfg);
834 GNUNET_SCHEDULER_add_shutdown (&shutdown_task,
838 GNUNET_SCHEDULER_shutdown ();
841 if (NULL != listen_port)
843 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
844 "Opening CADET listen port\n");
845 GNUNET_CRYPTO_hash (listen_port,
846 strlen (listen_port),
848 lp = GNUNET_CADET_open_port (mh,
852 NULL /* window changes */,
856 if (NULL != target_id)
858 struct GNUNET_PeerIdentity pid;
859 enum GNUNET_CADET_ChannelOption opt;
862 GNUNET_CRYPTO_eddsa_public_key_from_string (target_id,
866 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
867 _("Invalid target `%s'\n"),
869 GNUNET_SCHEDULER_shutdown ();
872 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
873 "Connecting to `%s:%s'\n",
876 opt = GNUNET_CADET_OPTION_DEFAULT | GNUNET_CADET_OPTION_RELIABLE;
877 GNUNET_CRYPTO_hash (target_port,
880 ch = GNUNET_CADET_channel_create (mh,
885 NULL /* window changes */,
888 if (GNUNET_YES == echo)
890 echo_task = GNUNET_SCHEDULER_add_now (&send_echo,
903 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
904 _("No action requested\n"));
905 GNUNET_SCHEDULER_shutdown ();
912 * The main function to obtain peer information.
914 * @param argc number of arguments from the command line
915 * @param argv command line arguments
916 * @return 0 ok, 1 on error
923 const char helpstr[] = "Create tunnels and retrieve info about CADET's status.";
924 struct GNUNET_GETOPT_CommandLineOption options[] = {
925 /* I would use the terminology 'circuit' here... --lynX */
926 GNUNET_GETOPT_option_string ('C',
929 gettext_noop ("Provide information about a particular connection"),
932 GNUNET_GETOPT_option_flag ('e',
934 gettext_noop ("Activate echo mode"),
937 GNUNET_GETOPT_option_flag ('d',
939 gettext_noop ("Dump debug information to STDERR"),
942 GNUNET_GETOPT_option_string ('o',
945 gettext_noop ("Listen for connections using a shared secret among sender and recipient"),
949 GNUNET_GETOPT_option_string ('p',
952 gettext_noop ("Provide information about a patricular peer"),
956 GNUNET_GETOPT_option_flag ('P',
958 gettext_noop ("Provide information about all peers"),
961 GNUNET_GETOPT_option_string ('t',
964 gettext_noop ("Provide information about a particular tunnel"),
968 GNUNET_GETOPT_option_flag ('T',
970 gettext_noop ("Provide information about all tunnels"),
973 GNUNET_GETOPT_OPTION_END
977 GNUNET_STRINGS_get_utf8_args (argc, argv,
981 res = GNUNET_PROGRAM_run (argc, argv,
982 "gnunet-cadet (OPTIONS | PEER_ID SHARED_SECRET)",
983 gettext_noop (helpstr),
984 options, &run, NULL);
986 GNUNET_free ((void *) argv);
988 if (GNUNET_OK == res)
993 /* end of gnunet-cadet.c */