2 This file is part of GNUnet.
3 Copyright (C) 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., 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 (-p).
70 static uint32_t listen_port;
73 * Request echo service
78 * Request a debug dump
83 * Time of last echo request.
85 struct GNUNET_TIME_Absolute echo_time;
88 * Task for next echo request.
90 struct GNUNET_SCHEDULER_Task * echo_task;
95 static char *target_id;
100 static uint32_t target_port;
103 * Data pending in netcat mode.
111 static struct GNUNET_CADET_Handle *mh;
116 static struct GNUNET_CADET_Channel *ch;
119 * Shutdown task handle.
121 struct GNUNET_SCHEDULER_Task * sd;
130 * Convert encryption status to human readable string.
132 * @param status Encryption status.
134 * @return Human readable string.
137 enc_2s (uint16_t status)
156 * Convert connection status to human readable string.
158 * @param status Connection status.
160 * @return Human readable string.
163 conn_2s (uint16_t status)
183 * Task run in monitor mode when the user presses CTRL-C to abort.
184 * Stops monitoring activity.
186 * @param cls Closure (unused).
187 * @param tc scheduler context
190 shutdown_task (void *cls,
191 const struct GNUNET_SCHEDULER_TaskContext *tc)
193 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Shutdown\n");
196 GNUNET_CADET_channel_destroy (ch);
201 GNUNET_CADET_disconnect (mh);
208 * Function called to notify a client about the connection
209 * begin ready to queue more data. "buf" will be
210 * NULL and "size" zero if the connection was closed for
211 * writing in the meantime.
216 * @param size number of bytes available in buf
217 * @param buf where the callee should write the message
218 * @return number of bytes written to buf
221 data_ready (void *cls, size_t size, void *buf)
223 struct GNUNET_MessageHeader *msg;
226 if (NULL == buf || 0 == size)
228 GNUNET_SCHEDULER_shutdown();
232 total_size = data_size + sizeof (struct GNUNET_MessageHeader);
233 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "sending %u bytes\n", data_size);
234 GNUNET_assert (size >= total_size);
237 msg->size = htons (total_size);
238 msg->type = htons (GNUNET_MESSAGE_TYPE_CADET_CLI);
239 memcpy (&msg[1], cls, data_size);
240 if (GNUNET_NO == echo)
246 echo_time = GNUNET_TIME_absolute_get ();
254 * Task run in monitor mode when the user presses CTRL-C to abort.
255 * Stops monitoring activity.
257 * @param cls Closure (unused).
258 * @param tc scheduler context
261 read_stdio (void *cls,
262 const struct GNUNET_SCHEDULER_TaskContext *tc)
264 static char buf[60000];
266 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
271 data_size = read (0, buf, 60000);
272 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "stdio read %u bytes\n", data_size);
275 GNUNET_SCHEDULER_shutdown();
278 GNUNET_CADET_notify_transmit_ready (ch, GNUNET_NO,
279 GNUNET_TIME_UNIT_FOREVER_REL,
281 + sizeof (struct GNUNET_MessageHeader),
287 * Start listening to stdin
292 struct GNUNET_NETWORK_FDSet *rs;
294 rs = GNUNET_NETWORK_fdset_create ();
295 GNUNET_NETWORK_fdset_set_native (rs, 0);
296 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
297 GNUNET_TIME_UNIT_FOREVER_REL,
300 GNUNET_NETWORK_fdset_destroy (rs);
305 * Function called whenever a channel is destroyed. Should clean up
306 * any associated state.
308 * It must NOT call #GNUNET_CADET_channel_destroy on the channel.
310 * @param cls closure (set from #GNUNET_CADET_connect)
311 * @param channel connection to the other end (henceforth invalid)
312 * @param channel_ctx place where local state associated
313 * with the channel is stored
316 channel_ended (void *cls,
317 const struct GNUNET_CADET_Channel *channel,
320 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Channel ended!\n");
321 GNUNET_break (channel == ch);
323 GNUNET_SCHEDULER_shutdown ();
328 * Method called whenever another peer has added us to a channel
329 * the other peer initiated.
330 * Only called (once) upon reception of data with a message type which was
331 * subscribed to in #GNUNET_CADET_connect.
333 * A call to #GNUNET_CADET_channel_destroy causes te channel to be ignored. In
334 * this case the handler MUST return NULL.
337 * @param channel new handle to the channel
338 * @param initiator peer that started the channel
339 * @param port Port this channel is for.
340 * @param options CadetOption flag field, with all active option bits set to 1.
342 * @return initial channel context for the channel
343 * (can be NULL -- that's not an error)
346 channel_incoming (void *cls,
347 struct GNUNET_CADET_Channel * channel,
348 const struct GNUNET_PeerIdentity * initiator,
349 uint32_t port, enum GNUNET_CADET_ChannelOption options)
351 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
352 "Incoming channel %p on port %u\n",
356 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "A channel already exists\n");
359 if (0 == listen_port)
361 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not listening to channels\n");
365 if (GNUNET_NO == echo)
375 * @brief Send an echo request to the remote peer.
377 * @param cls Closure (NULL).
378 * @param tc Task context.
381 send_echo (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
383 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) || NULL == ch)
386 GNUNET_CADET_notify_transmit_ready (ch, GNUNET_NO,
387 GNUNET_TIME_UNIT_FOREVER_REL,
388 sizeof (struct GNUNET_MessageHeader),
394 * Call CADET's monitor API, request debug dump on the service.
396 * @param cls Closure (unused).
397 * @param tc TaskContext
400 request_dump (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
402 GNUNET_CADET_request_dump (mh);
403 GNUNET_SCHEDULER_cancel (sd);
404 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &shutdown_task, NULL);
409 * Call CADET's monitor API, get info of one connection.
411 * @param cls Closure (unused).
412 * @param tc TaskContext
415 create_channel (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
417 struct GNUNET_PeerIdentity pid;
418 enum GNUNET_CADET_ChannelOption opt;
420 GNUNET_assert (NULL == ch);
423 GNUNET_CRYPTO_eddsa_public_key_from_string (target_id,
428 _("Invalid target `%s'\n"),
430 GNUNET_SCHEDULER_shutdown ();
433 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connecting to `%s'\n", target_id);
434 opt = GNUNET_CADET_OPTION_DEFAULT | GNUNET_CADET_OPTION_RELIABLE;
435 ch = GNUNET_CADET_channel_create (mh, NULL, &pid, target_port, opt);
436 if (GNUNET_NO == echo)
439 GNUNET_SCHEDULER_add_now (send_echo, NULL);
444 * Function called whenever a message is received.
446 * Each time the function must call #GNUNET_CADET_receive_done on the channel
447 * in order to receive the next message. This doesn't need to be immediate:
448 * can be delayed if some processing is done on the message.
450 * @param cls Closure (set from #GNUNET_CADET_connect).
451 * @param channel Connection to the other end.
452 * @param channel_ctx Place to store local state associated with the channel.
453 * @param message The actual message.
454 * @return #GNUNET_OK to keep the channel open,
455 * #GNUNET_SYSERR to close it (signal serious error).
458 data_callback (void *cls,
459 struct GNUNET_CADET_Channel *channel,
461 const struct GNUNET_MessageHeader *message)
467 GNUNET_break (ch == channel);
468 GNUNET_CADET_receive_done (channel);
470 if (GNUNET_YES == echo)
472 if (0 != listen_port)
474 /* Just listening to echo incoming messages*/
475 GNUNET_CADET_notify_transmit_ready (channel, GNUNET_NO,
476 GNUNET_TIME_UNIT_FOREVER_REL,
477 sizeof (struct GNUNET_MessageHeader),
483 struct GNUNET_TIME_Relative latency;
485 latency = GNUNET_TIME_absolute_get_duration (echo_time);
486 echo_time = GNUNET_TIME_UNIT_FOREVER_ABS;
487 FPRINTF (stdout, "time: %s\n",
488 GNUNET_STRINGS_relative_time_to_string (latency, GNUNET_NO));
489 echo_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
494 len = ntohs (message->size) - sizeof (*message);
495 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got %u bytes\n", len);
496 buf = (const char *) &message[1];
500 done = write (1, &buf[off], len - off);
504 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
506 return GNUNET_SYSERR;
515 * Method called to retrieve information about all peers in CADET, called
518 * After last peer has been reported, an additional call with NULL is done.
520 * @param cls Closure.
521 * @param peer Peer, or NULL on "EOF".
522 * @param tunnel Do we have a tunnel towards this peer?
523 * @param n_paths Number of known paths towards this peer.
524 * @param best_path How long is the best path?
525 * (0 = unknown, 1 = ourselves, 2 = neighbor)
528 peers_callback (void *cls, const struct GNUNET_PeerIdentity *peer,
529 int tunnel, unsigned int n_paths, unsigned int best_path)
533 if (GNUNET_YES != monitor_mode)
535 GNUNET_SCHEDULER_shutdown();
539 FPRINTF (stdout, "%s tunnel: %c, paths: %u\n",
540 GNUNET_i2s_full (peer), tunnel ? 'Y' : 'N', n_paths);
544 * Method called to retrieve information about a specific peer
545 * known to the service.
547 * @param cls Closure.
548 * @param peer Peer ID.
549 * @param tunnel Do we have a tunnel towards this peer? #GNUNET_YES/#GNUNET_NO
550 * @param neighbor Is this a direct neighbor? #GNUNET_YES/#GNUNET_NO
551 * @param n_paths Number of paths known towards peer.
552 * @param paths Array of PEER_IDs representing all paths to reach the peer.
553 * Each path starts with the local peer.
554 * Each path ends with the destination peer (given in @c peer).
557 peer_callback (void *cls,
558 const struct GNUNET_PeerIdentity *peer,
561 unsigned int n_paths,
562 struct GNUNET_PeerIdentity *paths)
565 struct GNUNET_PeerIdentity *p;
567 FPRINTF (stdout, "%s [TUNNEL: %s, NEIGHBOR: %s, PATHS: %u]\n",
568 GNUNET_i2s_full (peer),
569 tunnel ? "Y" : "N", neighbor ? "Y" : "N", n_paths);
571 for (i = 0; i < n_paths && NULL != p;)
573 FPRINTF (stdout, "%s ", GNUNET_i2s (p));
574 if (0 == memcmp (p, peer, sizeof (*p)))
576 FPRINTF (stdout, "\n");
582 GNUNET_SCHEDULER_shutdown();
587 * Method called to retrieve information about all tunnels in CADET.
589 * @param cls Closure.
590 * @param peer Destination peer.
591 * @param channels Number of channels.
592 * @param connections Number of connections.
593 * @param estate Encryption state.
594 * @param cstate Connectivity state.
597 tunnels_callback (void *cls,
598 const struct GNUNET_PeerIdentity *peer,
599 unsigned int channels,
600 unsigned int connections,
606 if (GNUNET_YES != monitor_mode)
608 GNUNET_SCHEDULER_shutdown();
612 FPRINTF (stdout, "%s [ENC: %s, CON: %s] CHs: %u, CONNs: %u\n",
613 GNUNET_i2s_full (peer),
614 enc_2s (estate), conn_2s (cstate),
615 channels, connections);
620 * Method called to retrieve information about a specific tunnel the cadet peer
621 * has established, o`r is trying to establish.
623 * @param cls Closure.
624 * @param peer Peer towards whom the tunnel is directed.
625 * @param n_channels Number of channels.
626 * @param n_connections Number of connections.
627 * @param channels Channels.
628 * @param connections Connections.
629 * @param estate Encryption status.
630 * @param cstate Connectivity status.
633 tunnel_callback (void *cls,
634 const struct GNUNET_PeerIdentity *peer,
635 unsigned int n_channels,
636 unsigned int n_connections,
638 struct GNUNET_CADET_Hash *connections,
646 FPRINTF (stdout, "Tunnel %s\n", GNUNET_i2s_full (peer));
647 FPRINTF (stdout, "\t%u channels\n", n_channels);
648 for (i = 0; i < n_channels; i++)
649 FPRINTF (stdout, "\t\t%X\n", channels[i]);
650 FPRINTF (stdout, "\t%u connections\n", n_connections);
651 for (i = 0; i < n_connections; i++)
652 FPRINTF (stdout, "\t\t%s\n", GC_h2s (&connections[i]));
653 FPRINTF (stdout, "\tencryption state: %s\n", enc_2s (estate));
654 FPRINTF (stdout, "\tconnection state: %s\n", conn_2s (cstate));
656 if (GNUNET_YES != monitor_mode)
658 GNUNET_SCHEDULER_shutdown();
665 * Call CADET's meta API, get all peers known to a peer.
667 * @param cls Closure (unused).
668 * @param tc TaskContext
671 get_peers (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
673 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
675 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Shutdown\n");
678 GNUNET_CADET_get_peers (mh, &peers_callback, NULL);
683 * Call CADET's monitor API, get info of one peer.
685 * @param cls Closure (unused).
686 * @param tc TaskContext
689 show_peer (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
691 struct GNUNET_PeerIdentity pid;
694 GNUNET_CRYPTO_eddsa_public_key_from_string (peer_id,
699 _("Invalid peer ID `%s'\n"),
701 GNUNET_SCHEDULER_shutdown();
704 GNUNET_CADET_get_peer (mh, &pid, peer_callback, NULL);
708 * Call CADET's meta API, get all tunnels known to a peer.
710 * @param cls Closure (unused).
711 * @param tc TaskContext
714 get_tunnels (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
716 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
718 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Shutdown\n");
721 GNUNET_CADET_get_tunnels (mh, &tunnels_callback, NULL);
726 * Call CADET's monitor API, get info of one tunnel.
728 * @param cls Closure (unused).
729 * @param tc TaskContext
732 show_tunnel (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
734 struct GNUNET_PeerIdentity pid;
737 GNUNET_CRYPTO_eddsa_public_key_from_string (tunnel_id,
742 _("Invalid tunnel owner `%s'\n"),
744 GNUNET_SCHEDULER_shutdown();
747 GNUNET_CADET_get_tunnel (mh, &pid, tunnel_callback, NULL);
752 * Call CADET's monitor API, get info of one channel.
754 * @param cls Closure (unused).
755 * @param tc TaskContext
758 show_channel (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
765 * Call CADET's monitor API, get info of one connection.
767 * @param cls Closure (unused).
768 * @param tc TaskContext
771 show_connection (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
778 * Main function that will be run by the scheduler.
781 * @param args remaining command-line arguments
782 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
783 * @param cfg configuration
786 run (void *cls, char *const *args, const char *cfgfile,
787 const struct GNUNET_CONFIGURATION_Handle *cfg)
789 GNUNET_CADET_InboundChannelNotificationHandler *newch = NULL;
790 GNUNET_CADET_ChannelEndHandler *endch = NULL;
791 static const struct GNUNET_CADET_MessageHandler handlers[] = {
792 {&data_callback, GNUNET_MESSAGE_TYPE_CADET_CLI, 0},
793 {NULL, 0, 0} /* FIXME add option to monitor msg types */
795 static uint32_t *ports = NULL;
796 /* FIXME add option to monitor apps */
799 target_port = args[0] && args[1] ? atoi(args[1]) : 0;
800 if ( (0 != (request_peers | request_tunnels)
804 || NULL != channel_id)
805 && target_id != NULL)
808 _("You must NOT give a TARGET "
809 "when using 'request all' options\n"));
813 if (GNUNET_YES == dump)
815 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
816 "requesting debug dump\n");
817 GNUNET_SCHEDULER_add_now (&request_dump, NULL);
819 else if (NULL != target_id)
821 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
822 "Creating channel to %s\n",
824 GNUNET_SCHEDULER_add_now (&create_channel, NULL);
825 endch = &channel_ended;
827 else if (0 != listen_port)
829 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Listen\n");
830 newch = &channel_incoming;
831 endch = &channel_ended;
832 ports = GNUNET_malloc (sizeof (uint32_t) * 2);
833 ports[0] = listen_port;
835 else if (NULL != peer_id)
837 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Show peer\n");
838 GNUNET_SCHEDULER_add_now (&show_peer, NULL);
840 else if (NULL != tunnel_id)
842 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Show tunnel\n");
843 GNUNET_SCHEDULER_add_now (&show_tunnel, NULL);
845 else if (NULL != channel_id)
847 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Show channel\n");
848 GNUNET_SCHEDULER_add_now (&show_channel, NULL);
850 else if (NULL != conn_id)
852 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Show connection\n");
853 GNUNET_SCHEDULER_add_now (&show_connection, NULL);
855 else if (GNUNET_YES == request_peers)
857 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Show all peers\n");
858 GNUNET_SCHEDULER_add_now (&get_peers, NULL);
860 else if (GNUNET_YES == request_tunnels)
862 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Show all tunnels\n");
863 GNUNET_SCHEDULER_add_now (&get_tunnels, NULL);
867 FPRINTF (stderr, "No action requested\n");
871 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connecting to cadet\n");
872 mh = GNUNET_CADET_connect (cfg,
874 newch, /* new channel */
878 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Done\n");
880 GNUNET_SCHEDULER_add_now (shutdown_task, NULL);
882 sd = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
883 shutdown_task, NULL);
889 * The main function to obtain peer information.
891 * @param argc number of arguments from the command line
892 * @param argv command line arguments
893 * @return 0 ok, 1 on error
896 main (int argc, char *const *argv)
899 const char helpstr[] = "Create channels and retreive info about cadets status.";
900 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
901 // {'a', "channel", "TUNNEL_ID:CHANNEL_ID",
902 // gettext_noop ("provide information about a particular channel"),
903 // GNUNET_YES, &GNUNET_GETOPT_set_string, &channel_id},
904 {'C', "connection", "CONNECTION_ID",
905 gettext_noop ("provide information about a particular connection"),
906 GNUNET_YES, &GNUNET_GETOPT_set_string, &conn_id},
908 gettext_noop ("activate echo mode"),
909 GNUNET_NO, &GNUNET_GETOPT_set_one, &echo},
911 gettext_noop ("dump debug information to STDERR"),
912 GNUNET_NO, &GNUNET_GETOPT_set_one, &dump},
913 // {'m', "monitor", NULL,
914 // gettext_noop ("provide information about all events (continuously)"),
915 // GNUNET_NO, &GNUNET_GETOPT_set_one, &monitor_mode},
916 {'o', "open-port", NULL,
917 gettext_noop ("port to listen to (default; 0)"),
918 GNUNET_YES, &GNUNET_GETOPT_set_uint, &listen_port},
919 {'p', "peer", "PEER_ID",
920 gettext_noop ("provide information about a patricular peer"),
921 GNUNET_YES, &GNUNET_GETOPT_set_string, &peer_id},
923 gettext_noop ("provide information about all peers"),
924 GNUNET_NO, &GNUNET_GETOPT_set_one, &request_peers},
925 {'t', "tunnel", "TUNNEL_ID",
926 gettext_noop ("provide information about a particular tunnel"),
927 GNUNET_YES, &GNUNET_GETOPT_set_string, &tunnel_id},
928 {'T', "tunnels", NULL,
929 gettext_noop ("provide information about all tunnels"),
930 GNUNET_NO, &GNUNET_GETOPT_set_one, &request_tunnels},
932 GNUNET_GETOPT_OPTION_END
935 monitor_mode = GNUNET_NO;
937 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
940 res = GNUNET_PROGRAM_run (argc, argv, "gnunet-cadet (OPTIONS | TARGET PORT)",
941 gettext_noop (helpstr),
942 options, &run, NULL);
944 GNUNET_free ((void *) argv);
946 if (GNUNET_OK == res)
952 /* end of gnunet-cadet.c */