2 This file is part of GNUnet.
3 Copyright (C) 2012 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
27 #include "gnunet_util_lib.h"
28 #include "gnunet_cadet_service.h"
35 static int monitor_mode;
40 static int request_peers;
50 static int request_tunnels;
55 static char *tunnel_id;
65 static char *channel_id;
68 * Port to listen on (-o).
70 static char *listen_port;
73 * Request echo service
78 * Request a debug dump
83 * Time of last echo request.
85 static struct GNUNET_TIME_Absolute echo_time;
88 * Task for next echo request.
90 static struct GNUNET_SCHEDULER_Task *echo_task;
95 static char *target_id;
100 static char *target_port = "default";
103 * Data pending in netcat mode.
105 static size_t data_size;
110 static struct GNUNET_CADET_Handle *mh;
115 static struct GNUNET_CADET_Channel *ch;
120 static struct GNUNET_CADET_TransmitHandle *th;
123 * HashCode of the given port string
125 static struct GNUNET_HashCode porthash;
128 * Data structure for ongoing reception of incoming virtual circuits.
130 struct GNUNET_CADET_Port *lp;
133 * Shutdown task handle.
135 static struct GNUNET_SCHEDULER_Task *sd;
138 * Task for reading from stdin.
140 static struct GNUNET_SCHEDULER_Task *rd_task;
145 static struct GNUNET_SCHEDULER_Task *job;
153 * Convert encryption status to human readable string.
155 * @param status Encryption status.
157 * @return Human readable string.
160 enc_2s (uint16_t status)
179 * Convert connection status to human readable string.
181 * @param status Connection status.
183 * @return Human readable string.
186 conn_2s (uint16_t status)
208 * Task to shut down this application.
210 * @param cls Closure (unused).
213 shutdown_task (void *cls)
215 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
219 GNUNET_CADET_notify_transmit_ready_cancel (th);
224 GNUNET_CADET_channel_destroy (ch);
227 else if (NULL != target_id) {
228 // FIXME: would be nicer to have proper NACK support from cadet_api
229 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
230 "Connection refused to %s\n",
235 GNUNET_CADET_disconnect (mh);
240 GNUNET_SCHEDULER_cancel (rd_task);
243 if (NULL != echo_task)
245 GNUNET_SCHEDULER_cancel (echo_task);
250 GNUNET_SCHEDULER_cancel (job);
257 * Function called to notify a client about the connection
258 * begin ready to queue more data. "buf" will be
259 * NULL and "size" zero if the connection was closed for
260 * writing in the meantime.
265 * @param size number of bytes available in buf
266 * @param buf where the callee should write the message
267 * @return number of bytes written to buf
270 data_ready (void *cls, size_t size, void *buf)
272 struct GNUNET_MessageHeader *msg;
277 if (NULL == buf || 0 == size)
279 GNUNET_SCHEDULER_shutdown();
283 total_size = data_size + sizeof (struct GNUNET_MessageHeader);
284 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
285 "sending %u bytes\n",
286 (unsigned int) data_size);
287 GNUNET_assert (size >= total_size);
290 msg->size = htons (total_size);
291 msg->type = htons (GNUNET_MESSAGE_TYPE_CADET_CLI);
292 GNUNET_memcpy (&msg[1], cls, data_size);
293 if (GNUNET_NO == echo)
299 echo_time = GNUNET_TIME_absolute_get ();
307 * Task run in stdio mode, after some data is available at stdin.
309 * @param cls Closure (unused).
312 read_stdio (void *cls)
314 static char buf[60000];
316 data_size = read (0, buf, 60000);
317 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
318 "stdio read %u bytes\n",
319 (unsigned int) data_size);
322 GNUNET_SCHEDULER_shutdown();
325 GNUNET_assert (NULL == th);
326 th = GNUNET_CADET_notify_transmit_ready (ch, GNUNET_NO,
327 GNUNET_TIME_UNIT_FOREVER_REL,
328 sizeof (struct GNUNET_MessageHeader)
335 * Start listening to stdin
340 struct GNUNET_NETWORK_FDSet *rs;
342 rs = GNUNET_NETWORK_fdset_create ();
343 GNUNET_NETWORK_fdset_set_native (rs, 0);
344 rd_task = GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
345 GNUNET_TIME_UNIT_FOREVER_REL,
348 GNUNET_NETWORK_fdset_destroy (rs);
353 * Function called whenever a channel is destroyed. Should clean up
354 * any associated state.
356 * It must NOT call #GNUNET_CADET_channel_destroy on the channel.
358 * @param cls closure (set from #GNUNET_CADET_connect)
359 * @param channel connection to the other end (henceforth invalid)
360 * @param channel_ctx place where local state associated
361 * with the channel is stored
364 channel_ended (void *cls,
365 const struct GNUNET_CADET_Channel *channel,
368 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Channel ended!\n");
372 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "ended: %p, expected: %p\n", channel, ch);
380 GNUNET_CADET_notify_transmit_ready_cancel (th);
384 GNUNET_SCHEDULER_shutdown ();
389 * Method called whenever another peer has added us to a channel
390 * the other peer initiated.
391 * Only called (once) upon reception of data with a message type which was
392 * subscribed to in #GNUNET_CADET_connect.
394 * A call to #GNUNET_CADET_channel_destroy causes the channel to be ignored.
395 * In this case the handler MUST return NULL.
398 * @param channel new handle to the channel
399 * @param initiator peer that started the channel
400 * @param port Port this channel is for.
401 * @param options CadetOption flag field, with all active option bits set to 1.
403 * @return initial channel context for the channel
404 * (can be NULL -- that's not an error)
407 channel_incoming (void *cls,
408 struct GNUNET_CADET_Channel *channel,
409 const struct GNUNET_PeerIdentity *initiator,
410 const struct GNUNET_HashCode *port,
411 enum GNUNET_CADET_ChannelOption options)
413 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
414 "Connected from %s\n",
415 GNUNET_i2s_full (initiator));
416 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
417 "Incoming channel %p on port %s\n",
418 channel, GNUNET_h2s (port));
422 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
423 "A channel already exists (%p)\n", ch);
425 * From now on multiple channels will be sending data to us
426 * making the service of this command unpredictable in its
427 * current implementation. So for now let's just bail out.
429 GNUNET_SCHEDULER_shutdown();
432 if (NULL == listen_port)
434 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Not listening to channels\n");
438 // Closing the listen port currently breaks open connections.
439 // Is this an intentional departure from POSIX socket behavior?
442 /* Now that we have our circuit up and running, let's not
443 * get confused by further incoming connect requests.
445 GNUNET_CADET_close_port (lp);
450 if (GNUNET_NO == echo)
460 * @brief Send an echo request to the remote peer.
462 * @param cls Closure (NULL).
465 send_echo (void *cls)
469 GNUNET_assert (NULL == th);
470 th = GNUNET_CADET_notify_transmit_ready (ch, GNUNET_NO,
471 GNUNET_TIME_UNIT_FOREVER_REL,
472 sizeof (struct GNUNET_MessageHeader),
478 * Call CADET's monitor API, request debug dump on the service.
480 * @param cls Closure (unused).
483 request_dump (void *cls)
485 GNUNET_CADET_request_dump (mh);
486 GNUNET_SCHEDULER_cancel (sd);
487 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
488 &shutdown_task, NULL);
493 * Call CADET's monitor API, get info of one connection.
495 * @param cls Closure (unused).
498 create_channel (void *cls)
500 struct GNUNET_PeerIdentity pid;
501 enum GNUNET_CADET_ChannelOption opt;
503 GNUNET_assert (NULL == ch);
506 GNUNET_CRYPTO_eddsa_public_key_from_string (target_id,
511 _("Invalid target `%s'\n"),
513 GNUNET_SCHEDULER_shutdown ();
516 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connecting to `%s'\n", target_id);
517 opt = GNUNET_CADET_OPTION_DEFAULT | GNUNET_CADET_OPTION_RELIABLE;
518 GNUNET_CRYPTO_hash (target_port, strlen(target_port), &porthash);
519 ch = GNUNET_CADET_channel_create (mh, NULL, &pid, &porthash, opt);
520 if (GNUNET_NO == echo)
523 echo_task = GNUNET_SCHEDULER_add_now (&send_echo, NULL);
528 * Function called whenever a message is received.
530 * Each time the function must call #GNUNET_CADET_receive_done on the channel
531 * in order to receive the next message. This doesn't need to be immediate:
532 * can be delayed if some processing is done on the message.
534 * @param cls Closure (set from #GNUNET_CADET_connect).
535 * @param channel Connection to the other end.
536 * @param channel_ctx Place to store local state associated with the channel.
537 * @param message The actual message.
538 * @return #GNUNET_OK to keep the channel open,
539 * #GNUNET_SYSERR to close it (signal serious error).
542 data_callback (void *cls,
543 struct GNUNET_CADET_Channel *channel,
545 const struct GNUNET_MessageHeader *message)
551 GNUNET_break (ch == channel);
552 GNUNET_CADET_receive_done (channel);
554 if (GNUNET_YES == echo)
556 if (NULL != listen_port)
558 /* Just listening to echo incoming messages*/
561 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
562 "Last echo reply not yet sent, dropping current reply.\n");
565 th = GNUNET_CADET_notify_transmit_ready (channel, GNUNET_NO,
566 GNUNET_TIME_UNIT_FOREVER_REL,
567 sizeof (struct GNUNET_MessageHeader),
573 struct GNUNET_TIME_Relative latency;
575 latency = GNUNET_TIME_absolute_get_duration (echo_time);
576 echo_time = GNUNET_TIME_UNIT_FOREVER_ABS;
577 FPRINTF (stdout, "time: %s\n",
578 GNUNET_STRINGS_relative_time_to_string (latency, GNUNET_NO));
579 echo_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
584 len = ntohs (message->size) - sizeof (*message);
585 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got %u bytes\n", len);
586 buf = (const char *) &message[1];
590 done = write (1, &buf[off], len - off);
594 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
596 return GNUNET_SYSERR;
605 * Method called to retrieve information about all peers in CADET, called
608 * After last peer has been reported, an additional call with NULL is done.
610 * @param cls Closure.
611 * @param peer Peer, or NULL on "EOF".
612 * @param tunnel Do we have a tunnel towards this peer?
613 * @param n_paths Number of known paths towards this peer.
614 * @param best_path How long is the best path?
615 * (0 = unknown, 1 = ourselves, 2 = neighbor)
618 peers_callback (void *cls,
619 const struct GNUNET_PeerIdentity *peer,
621 unsigned int n_paths,
622 unsigned int best_path)
626 if (GNUNET_YES != monitor_mode)
628 GNUNET_SCHEDULER_shutdown();
632 FPRINTF (stdout, "%s tunnel: %c, paths: %u\n",
633 GNUNET_i2s_full (peer), tunnel ? 'Y' : 'N', n_paths);
637 * Method called to retrieve information about a specific peer
638 * known to the service.
640 * @param cls Closure.
641 * @param peer Peer ID.
642 * @param tunnel Do we have a tunnel towards this peer? #GNUNET_YES/#GNUNET_NO
643 * @param neighbor Is this a direct neighbor? #GNUNET_YES/#GNUNET_NO
644 * @param n_paths Number of paths known towards peer.
645 * @param paths Array of PEER_IDs representing all paths to reach the peer.
646 * Each path starts with the local peer.
647 * Each path ends with the destination peer (given in @c peer).
650 peer_callback (void *cls,
651 const struct GNUNET_PeerIdentity *peer,
654 unsigned int n_paths,
655 struct GNUNET_PeerIdentity *paths)
658 struct GNUNET_PeerIdentity *p;
660 FPRINTF (stdout, "%s [TUNNEL: %s, NEIGHBOR: %s, PATHS: %u]\n",
661 GNUNET_i2s_full (peer),
662 tunnel ? "Y" : "N", neighbor ? "Y" : "N", n_paths);
664 for (i = 0; i < n_paths && NULL != p;)
666 FPRINTF (stdout, "%s ", GNUNET_i2s (p));
667 if (0 == memcmp (p, peer, sizeof (*p)))
669 FPRINTF (stdout, "\n");
675 GNUNET_SCHEDULER_shutdown();
680 * Method called to retrieve information about all tunnels in CADET.
682 * @param cls Closure.
683 * @param peer Destination peer.
684 * @param channels Number of channels.
685 * @param connections Number of connections.
686 * @param estate Encryption state.
687 * @param cstate Connectivity state.
690 tunnels_callback (void *cls,
691 const struct GNUNET_PeerIdentity *peer,
692 unsigned int channels,
693 unsigned int connections,
699 if (GNUNET_YES != monitor_mode)
701 GNUNET_SCHEDULER_shutdown();
705 FPRINTF (stdout, "%s [ENC: %s, CON: %s] CHs: %u, CONNs: %u\n",
706 GNUNET_i2s_full (peer),
707 enc_2s (estate), conn_2s (cstate),
708 channels, connections);
713 * Method called to retrieve information about a specific tunnel the cadet peer
714 * has established, o`r is trying to establish.
716 * @param cls Closure.
717 * @param peer Peer towards whom the tunnel is directed.
718 * @param n_channels Number of channels.
719 * @param n_connections Number of connections.
720 * @param channels Channels.
721 * @param connections Connections.
722 * @param estate Encryption status.
723 * @param cstate Connectivity status.
726 tunnel_callback (void *cls,
727 const struct GNUNET_PeerIdentity *peer,
728 unsigned int n_channels,
729 unsigned int n_connections,
730 const struct GNUNET_CADET_ChannelTunnelNumber *channels,
731 const struct GNUNET_CADET_ConnectionTunnelIdentifier *connections,
739 FPRINTF (stdout, "Tunnel %s\n", GNUNET_i2s_full (peer));
740 FPRINTF (stdout, "\t%u channels\n", n_channels);
741 for (i = 0; i < n_channels; i++)
742 FPRINTF (stdout, "\t\t%X\n", ntohl (channels[i].cn));
743 FPRINTF (stdout, "\t%u connections\n", n_connections);
744 for (i = 0; i < n_connections; i++)
745 FPRINTF (stdout, "\t\t%s\n", GNUNET_sh2s (&connections[i].connection_of_tunnel));
746 FPRINTF (stdout, "\tencryption state: %s\n", enc_2s (estate));
747 FPRINTF (stdout, "\tconnection state: %s\n", conn_2s (cstate));
749 if (GNUNET_YES != monitor_mode)
751 GNUNET_SCHEDULER_shutdown ();
758 * Call CADET's meta API, get all peers known to a peer.
760 * @param cls Closure (unused).
763 get_peers (void *cls)
766 GNUNET_CADET_get_peers (mh, &peers_callback, NULL);
771 * Call CADET's monitor API, get info of one peer.
773 * @param cls Closure (unused).
776 show_peer (void *cls)
778 struct GNUNET_PeerIdentity pid;
782 GNUNET_CRYPTO_eddsa_public_key_from_string (peer_id,
787 _("Invalid peer ID `%s'\n"),
789 GNUNET_SCHEDULER_shutdown();
792 GNUNET_CADET_get_peer (mh, &pid, peer_callback, NULL);
797 * Call CADET's meta API, get all tunnels known to a peer.
799 * @param cls Closure (unused).
802 get_tunnels (void *cls)
805 GNUNET_CADET_get_tunnels (mh, &tunnels_callback, NULL);
810 * Call CADET's monitor API, get info of one tunnel.
812 * @param cls Closure (unused).
815 show_tunnel (void *cls)
817 struct GNUNET_PeerIdentity pid;
820 GNUNET_CRYPTO_eddsa_public_key_from_string (tunnel_id,
825 _("Invalid tunnel owner `%s'\n"),
827 GNUNET_SCHEDULER_shutdown ();
830 GNUNET_CADET_get_tunnel (mh,
838 * Call CADET's monitor API, get info of one channel.
840 * @param cls Closure (unused).
843 show_channel (void *cls)
851 * Call CADET's monitor API, get info of one connection.
853 * @param cls Closure (unused).
856 show_connection (void *cls)
864 * Main function that will be run by the scheduler.
867 * @param args remaining command-line arguments
868 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
869 * @param cfg configuration
875 const struct GNUNET_CONFIGURATION_Handle *cfg)
877 static const struct GNUNET_CADET_MessageHandler handlers[] = {
878 {&data_callback, GNUNET_MESSAGE_TYPE_CADET_CLI, 0},
879 {NULL, 0, 0} /* FIXME add option to monitor msg types */
882 /* FIXME add option to monitor apps */
885 if (target_id && args[1]) target_port = args[1];
887 if ( (0 != (request_peers | request_tunnels)
891 || NULL != channel_id)
892 && target_id != NULL)
895 _("You must NOT give a TARGET "
896 "when using 'request all' options\n"));
900 if (GNUNET_YES == dump)
902 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
903 "requesting debug dump\n");
904 GNUNET_SCHEDULER_add_now (&request_dump, NULL);
906 else if (NULL != target_id)
908 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
909 "Creating channel to %s\n",
911 GNUNET_SCHEDULER_add_now (&create_channel, NULL);
913 else if (NULL != peer_id)
915 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Show peer\n");
916 job = GNUNET_SCHEDULER_add_now (&show_peer, NULL);
918 else if (NULL != tunnel_id)
920 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Show tunnel\n");
921 job = GNUNET_SCHEDULER_add_now (&show_tunnel, NULL);
923 else if (NULL != channel_id)
925 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Show channel\n");
926 job = GNUNET_SCHEDULER_add_now (&show_channel, NULL);
928 else if (NULL != conn_id)
930 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Show connection\n");
931 job = GNUNET_SCHEDULER_add_now (&show_connection, NULL);
933 else if (GNUNET_YES == request_peers)
935 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Show all peers\n");
936 job = GNUNET_SCHEDULER_add_now (&get_peers, NULL);
938 else if (GNUNET_YES == request_tunnels)
940 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Show all tunnels\n");
941 job = GNUNET_SCHEDULER_add_now (&get_tunnels, NULL);
943 else if (NULL == listen_port)
945 FPRINTF (stderr, "No action requested\n");
949 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connecting to CADET service\n");
950 mh = GNUNET_CADET_connect (cfg,
952 &channel_ended, /* cleaner */
955 GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
957 sd = GNUNET_SCHEDULER_add_shutdown (&shutdown_task, NULL);
959 if (NULL != listen_port)
961 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Opening CADET listen port\n");
962 GNUNET_CRYPTO_hash (listen_port, strlen(listen_port), &porthash);
963 lp = GNUNET_CADET_open_port (mh, &porthash, &channel_incoming, NULL);
969 * The main function to obtain peer information.
971 * @param argc number of arguments from the command line
972 * @param argv command line arguments
973 * @return 0 ok, 1 on error
976 main (int argc, char *const *argv)
979 const char helpstr[] = "Create channels and retreive info about cadets status.";
980 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
981 // {'a', "channel", "TUNNEL_ID:CHANNEL_ID",
982 // gettext_noop ("provide information about a particular channel"),
983 // GNUNET_YES, &GNUNET_GETOPT_set_string, &channel_id},
984 {'C', "connection", "CONNECTION_ID",
985 gettext_noop ("provide information about a particular connection"),
986 GNUNET_YES, &GNUNET_GETOPT_set_string, &conn_id},
988 gettext_noop ("activate echo mode"),
989 GNUNET_NO, &GNUNET_GETOPT_set_one, &echo},
991 gettext_noop ("dump debug information to STDERR"),
992 GNUNET_NO, &GNUNET_GETOPT_set_one, &dump},
993 // {'m', "monitor", NULL,
994 // gettext_noop ("provide information about all events (continuously)"),
995 // GNUNET_NO, &GNUNET_GETOPT_set_one, &monitor_mode},
996 {'o', "open-port", NULL,
997 gettext_noop ("port to listen to"),
998 GNUNET_YES, &GNUNET_GETOPT_set_string, &listen_port},
999 {'p', "peer", "PEER_ID",
1000 gettext_noop ("provide information about a patricular peer"),
1001 GNUNET_YES, &GNUNET_GETOPT_set_string, &peer_id},
1002 {'P', "peers", NULL,
1003 gettext_noop ("provide information about all peers"),
1004 GNUNET_NO, &GNUNET_GETOPT_set_one, &request_peers},
1005 {'t', "tunnel", "TUNNEL_ID",
1006 gettext_noop ("provide information about a particular tunnel"),
1007 GNUNET_YES, &GNUNET_GETOPT_set_string, &tunnel_id},
1008 {'T', "tunnels", NULL,
1009 gettext_noop ("provide information about all tunnels"),
1010 GNUNET_NO, &GNUNET_GETOPT_set_one, &request_tunnels},
1012 GNUNET_GETOPT_OPTION_END
1015 monitor_mode = GNUNET_NO;
1017 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
1020 res = GNUNET_PROGRAM_run (argc, argv, "gnunet-cadet (OPTIONS | TARGET PORT)",
1021 gettext_noop (helpstr),
1022 options, &run, NULL);
1024 GNUNET_free ((void *) argv);
1026 if (GNUNET_OK == res)
1032 /* end of gnunet-cadet.c */