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;
131 * Task run in monitor mode when the user presses CTRL-C to abort.
132 * Stops monitoring activity.
134 * @param cls Closure (unused).
135 * @param tc scheduler context
138 shutdown_task (void *cls,
139 const struct GNUNET_SCHEDULER_TaskContext *tc)
141 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Shutdown\n");
144 GNUNET_CADET_channel_destroy (ch);
149 GNUNET_CADET_disconnect (mh);
156 * Function called to notify a client about the connection
157 * begin ready to queue more data. "buf" will be
158 * NULL and "size" zero if the connection was closed for
159 * writing in the meantime.
164 * @param size number of bytes available in buf
165 * @param buf where the callee should write the message
166 * @return number of bytes written to buf
169 data_ready (void *cls, size_t size, void *buf)
171 struct GNUNET_MessageHeader *msg;
174 if (NULL == buf || 0 == size)
176 GNUNET_SCHEDULER_shutdown();
180 total_size = data_size + sizeof (struct GNUNET_MessageHeader);
181 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "sending %u bytes\n", data_size);
182 GNUNET_assert (size >= total_size);
185 msg->size = htons (total_size);
186 msg->type = htons (GNUNET_MESSAGE_TYPE_CADET_CLI);
187 memcpy (&msg[1], cls, data_size);
188 if (GNUNET_NO == echo)
194 echo_time = GNUNET_TIME_absolute_get ();
202 * Task run in monitor mode when the user presses CTRL-C to abort.
203 * Stops monitoring activity.
205 * @param cls Closure (unused).
206 * @param tc scheduler context
209 read_stdio (void *cls,
210 const struct GNUNET_SCHEDULER_TaskContext *tc)
212 static char buf[60000];
214 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
219 data_size = read (0, buf, 60000);
220 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "stdio read %u bytes\n", data_size);
223 GNUNET_SCHEDULER_shutdown();
226 GNUNET_CADET_notify_transmit_ready (ch, GNUNET_NO,
227 GNUNET_TIME_UNIT_FOREVER_REL,
229 + sizeof (struct GNUNET_MessageHeader),
235 * Start listening to stdin
240 struct GNUNET_NETWORK_FDSet *rs;
242 rs = GNUNET_NETWORK_fdset_create ();
243 GNUNET_NETWORK_fdset_set_native (rs, 0);
244 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
245 GNUNET_TIME_UNIT_FOREVER_REL,
248 GNUNET_NETWORK_fdset_destroy (rs);
253 * Function called whenever a channel is destroyed. Should clean up
254 * any associated state.
256 * It must NOT call #GNUNET_CADET_channel_destroy on the channel.
258 * @param cls closure (set from #GNUNET_CADET_connect)
259 * @param channel connection to the other end (henceforth invalid)
260 * @param channel_ctx place where local state associated
261 * with the channel is stored
264 channel_ended (void *cls,
265 const struct GNUNET_CADET_Channel *channel,
268 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Channel ended!\n");
269 GNUNET_break (channel == ch);
271 GNUNET_SCHEDULER_shutdown ();
276 * Method called whenever another peer has added us to a channel
277 * the other peer initiated.
278 * Only called (once) upon reception of data with a message type which was
279 * subscribed to in #GNUNET_CADET_connect.
281 * A call to #GNUNET_CADET_channel_destroy causes te channel to be ignored. In
282 * this case the handler MUST return NULL.
285 * @param channel new handle to the channel
286 * @param initiator peer that started the channel
287 * @param port Port this channel is for.
288 * @param options CadetOption flag field, with all active option bits set to 1.
290 * @return initial channel context for the channel
291 * (can be NULL -- that's not an error)
294 channel_incoming (void *cls,
295 struct GNUNET_CADET_Channel * channel,
296 const struct GNUNET_PeerIdentity * initiator,
297 uint32_t port, enum GNUNET_CADET_ChannelOption options)
299 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
300 "Incoming channel %p on port %u\n",
304 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "A channel already exists\n");
307 if (0 == listen_port)
309 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not listening to channels\n");
313 if (GNUNET_NO == echo)
323 * @brief Send an echo request to the remote peer.
325 * @param cls Closure (NULL).
326 * @param tc Task context.
329 send_echo (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
331 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) || NULL == ch)
334 GNUNET_CADET_notify_transmit_ready (ch, GNUNET_NO,
335 GNUNET_TIME_UNIT_FOREVER_REL,
336 sizeof (struct GNUNET_MessageHeader),
342 * Call CADET's monitor API, request debug dump on the service.
344 * @param cls Closure (unused).
345 * @param tc TaskContext
348 request_dump (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
350 GNUNET_CADET_request_dump (mh);
351 GNUNET_SCHEDULER_cancel (sd);
352 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &shutdown_task, NULL);
357 * Call CADET's monitor API, get info of one connection.
359 * @param cls Closure (unused).
360 * @param tc TaskContext
363 create_channel (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
365 struct GNUNET_PeerIdentity pid;
366 enum GNUNET_CADET_ChannelOption opt;
368 GNUNET_assert (NULL == ch);
371 GNUNET_CRYPTO_eddsa_public_key_from_string (target_id,
376 _("Invalid target `%s'\n"),
378 GNUNET_SCHEDULER_shutdown ();
381 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connecting to `%s'\n", target_id);
382 opt = GNUNET_CADET_OPTION_DEFAULT | GNUNET_CADET_OPTION_RELIABLE;
383 ch = GNUNET_CADET_channel_create (mh, NULL, &pid, target_port, opt);
384 if (GNUNET_NO == echo)
387 GNUNET_SCHEDULER_add_now (send_echo, NULL);
392 * Function called whenever a message is received.
394 * Each time the function must call #GNUNET_CADET_receive_done on the channel
395 * in order to receive the next message. This doesn't need to be immediate:
396 * can be delayed if some processing is done on the message.
398 * @param cls Closure (set from #GNUNET_CADET_connect).
399 * @param channel Connection to the other end.
400 * @param channel_ctx Place to store local state associated with the channel.
401 * @param message The actual message.
402 * @return #GNUNET_OK to keep the channel open,
403 * #GNUNET_SYSERR to close it (signal serious error).
406 data_callback (void *cls,
407 struct GNUNET_CADET_Channel *channel,
409 const struct GNUNET_MessageHeader *message)
415 GNUNET_break (ch == channel);
416 GNUNET_CADET_receive_done (channel);
418 if (GNUNET_YES == echo)
420 if (0 != listen_port)
422 /* Just listening to echo incoming messages*/
423 GNUNET_CADET_notify_transmit_ready (channel, GNUNET_NO,
424 GNUNET_TIME_UNIT_FOREVER_REL,
425 sizeof (struct GNUNET_MessageHeader),
431 struct GNUNET_TIME_Relative latency;
433 latency = GNUNET_TIME_absolute_get_duration (echo_time);
434 echo_time = GNUNET_TIME_UNIT_FOREVER_ABS;
435 FPRINTF (stdout, "time: %s\n",
436 GNUNET_STRINGS_relative_time_to_string (latency, GNUNET_NO));
437 echo_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
442 len = ntohs (message->size) - sizeof (*message);
443 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got %u bytes\n", len);
444 buf = (const char *) &message[1];
448 done = write (1, &buf[off], len - off);
452 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
454 return GNUNET_SYSERR;
463 * Method called to retrieve information about all peers in CADET, called
466 * After last peer has been reported, an additional call with NULL is done.
468 * @param cls Closure.
469 * @param peer Peer, or NULL on "EOF".
470 * @param tunnel Do we have a tunnel towards this peer?
471 * @param n_paths Number of known paths towards this peer.
472 * @param best_path How long is the best path?
473 * (0 = unknown, 1 = ourselves, 2 = neighbor)
476 peers_callback (void *cls, const struct GNUNET_PeerIdentity *peer,
477 int tunnel, unsigned int n_paths, unsigned int best_path)
481 if (GNUNET_YES != monitor_mode)
483 GNUNET_SCHEDULER_shutdown();
487 FPRINTF (stdout, "%s tunnel: %c, paths: %u\n",
488 GNUNET_i2s_full (peer), tunnel ? 'Y' : 'N', n_paths);
492 * Method called to retrieve information about a specific peer
493 * known to the service.
495 * @param cls Closure.
496 * @param peer Peer ID.
497 * @param tunnel Do we have a tunnel towards this peer? #GNUNET_YES/#GNUNET_NO
498 * @param neighbor Is this a direct neighbor? #GNUNET_YES/#GNUNET_NO
499 * @param n_paths Number of paths known towards peer.
500 * @param paths Array of PEER_IDs representing all paths to reach the peer.
501 * Each path starts with the local peer.
502 * Each path ends with the destination peer (given in @c peer).
505 peer_callback (void *cls,
506 const struct GNUNET_PeerIdentity *peer,
509 unsigned int n_paths,
510 struct GNUNET_PeerIdentity *paths)
513 struct GNUNET_PeerIdentity *p;
515 FPRINTF (stdout, "%s [TUNNEL: %s, NEIGHBOR: %s, PATHS: %u]\n",
516 GNUNET_i2s_full (peer),
517 tunnel ? "Y" : "N", neighbor ? "Y" : "N", n_paths);
519 for (i = 0; i < n_paths && NULL != p;)
521 FPRINTF (stdout, "%s ", GNUNET_i2s (p));
522 if (0 == memcmp (p, peer, sizeof (*p)))
524 FPRINTF (stdout, "\n");
530 GNUNET_SCHEDULER_shutdown();
535 * Method called to retrieve information about all tunnels in CADET.
537 * @param cls Closure.
538 * @param peer Destination peer.
539 * @param channels Number of channels.
540 * @param connections Number of connections.
541 * @param estate Encryption state.
542 * @param cstate Connectivity state.
545 tunnels_callback (void *cls,
546 const struct GNUNET_PeerIdentity *peer,
547 unsigned int channels,
548 unsigned int connections,
554 if (GNUNET_YES != monitor_mode)
556 GNUNET_SCHEDULER_shutdown();
560 FPRINTF (stdout, "%s [ENC: %u, CON: %u] CHs: %u, CONNs: %u\n",
561 GNUNET_i2s_full (peer), estate, cstate, channels, connections);
566 * Method called to retrieve information about a specific tunnel the cadet peer
567 * has established, o`r is trying to establish.
569 * @param cls Closure.
570 * @param peer Peer towards whom the tunnel is directed.
571 * @param n_channels Number of channels.
572 * @param n_connections Number of connections.
573 * @param channels Channels.
574 * @param connections Connections.
575 * @param estate Encryption status.
576 * @param cstate Connectivity status.
579 tunnel_callback (void *cls,
580 const struct GNUNET_PeerIdentity *peer,
581 unsigned int n_channels,
582 unsigned int n_connections,
584 struct GNUNET_CADET_Hash *connections,
592 FPRINTF (stdout, "Tunnel %s\n", GNUNET_i2s_full (peer));
593 FPRINTF (stdout, "- %u channels\n", n_channels);
594 for (i = 0; i < n_channels; i++)
595 FPRINTF (stdout, " %u\n", channels[i]);
596 FPRINTF (stdout, "- %u connections\n", n_connections);
597 for (i = 0; i < n_connections; i++)
598 FPRINTF (stdout, " %s\n", GC_h2s (&connections[i]));
599 FPRINTF (stdout, "- enc state: %u\n", estate);
600 FPRINTF (stdout, "- con state: %u\n", cstate);
602 if (GNUNET_YES != monitor_mode)
604 GNUNET_SCHEDULER_shutdown();
612 * Call CADET's meta API, get all peers known to a peer.
614 * @param cls Closure (unused).
615 * @param tc TaskContext
618 get_peers (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
620 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
622 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Shutdown\n");
625 GNUNET_CADET_get_peers (mh, &peers_callback, NULL);
630 * Call CADET's monitor API, get info of one peer.
632 * @param cls Closure (unused).
633 * @param tc TaskContext
636 show_peer (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
638 struct GNUNET_PeerIdentity pid;
641 GNUNET_CRYPTO_eddsa_public_key_from_string (peer_id,
646 _("Invalid peer ID `%s'\n"),
648 GNUNET_SCHEDULER_shutdown();
651 GNUNET_CADET_get_peer (mh, &pid, peer_callback, NULL);
655 * Call CADET's meta API, get all tunnels known to a peer.
657 * @param cls Closure (unused).
658 * @param tc TaskContext
661 get_tunnels (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
663 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
665 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Shutdown\n");
668 GNUNET_CADET_get_tunnels (mh, &tunnels_callback, NULL);
673 * Call CADET's monitor API, get info of one tunnel.
675 * @param cls Closure (unused).
676 * @param tc TaskContext
679 show_tunnel (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
681 struct GNUNET_PeerIdentity pid;
684 GNUNET_CRYPTO_eddsa_public_key_from_string (tunnel_id,
689 _("Invalid tunnel owner `%s'\n"),
691 GNUNET_SCHEDULER_shutdown();
694 GNUNET_CADET_get_tunnel (mh, &pid, tunnel_callback, NULL);
699 * Call CADET's monitor API, get info of one channel.
701 * @param cls Closure (unused).
702 * @param tc TaskContext
705 show_channel (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
712 * Call CADET's monitor API, get info of one connection.
714 * @param cls Closure (unused).
715 * @param tc TaskContext
718 show_connection (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
725 * Main function that will be run by the scheduler.
728 * @param args remaining command-line arguments
729 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
730 * @param cfg configuration
733 run (void *cls, char *const *args, const char *cfgfile,
734 const struct GNUNET_CONFIGURATION_Handle *cfg)
736 GNUNET_CADET_InboundChannelNotificationHandler *newch = NULL;
737 GNUNET_CADET_ChannelEndHandler *endch = NULL;
738 static const struct GNUNET_CADET_MessageHandler handlers[] = {
739 {&data_callback, GNUNET_MESSAGE_TYPE_CADET_CLI, 0},
740 {NULL, 0, 0} /* FIXME add option to monitor msg types */
742 static uint32_t *ports = NULL;
743 /* FIXME add option to monitor apps */
746 target_port = args[0] && args[1] ? atoi(args[1]) : 0;
747 if ( (0 != (request_peers | request_tunnels)
751 || NULL != channel_id)
752 && target_id != NULL)
755 _("You must NOT give a TARGET"
756 "when using 'request all' options\n"));
760 if (GNUNET_YES == dump)
762 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
763 "requesting debug dump\n");
764 GNUNET_SCHEDULER_add_now (&request_dump, NULL);
766 else if (NULL != target_id)
768 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
769 "Creating channel to %s\n",
771 GNUNET_SCHEDULER_add_now (&create_channel, NULL);
772 endch = &channel_ended;
774 else if (0 != listen_port)
776 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Listen\n");
777 newch = &channel_incoming;
778 endch = &channel_ended;
779 ports = GNUNET_malloc (sizeof (uint32_t) * 2);
780 ports[0] = listen_port;
782 else if (NULL != peer_id)
784 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Show peer\n");
785 GNUNET_SCHEDULER_add_now (&show_peer, NULL);
787 else if (NULL != tunnel_id)
789 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Show tunnel\n");
790 GNUNET_SCHEDULER_add_now (&show_tunnel, NULL);
792 else if (NULL != channel_id)
794 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Show channel\n");
795 GNUNET_SCHEDULER_add_now (&show_channel, NULL);
797 else if (NULL != conn_id)
799 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Show connection\n");
800 GNUNET_SCHEDULER_add_now (&show_connection, NULL);
802 else if (GNUNET_YES == request_peers)
804 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Show all peers\n");
805 GNUNET_SCHEDULER_add_now (&get_peers, NULL);
807 else if (GNUNET_YES == request_tunnels)
809 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Show all tunnels\n");
810 GNUNET_SCHEDULER_add_now (&get_tunnels, NULL);
814 FPRINTF (stderr, "No action requested\n");
818 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connecting to cadet\n");
819 mh = GNUNET_CADET_connect (cfg,
821 newch, /* new channel */
825 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Done\n");
827 GNUNET_SCHEDULER_add_now (shutdown_task, NULL);
829 sd = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
830 shutdown_task, NULL);
836 * The main function to obtain peer information.
838 * @param argc number of arguments from the command line
839 * @param argv command line arguments
840 * @return 0 ok, 1 on error
843 main (int argc, char *const *argv)
846 const char helpstr[] = "Create channels and retreive info about cadets status.";
847 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
848 // {'a', "channel", "TUNNEL_ID:CHANNEL_ID",
849 // gettext_noop ("provide information about a particular channel"),
850 // GNUNET_YES, &GNUNET_GETOPT_set_string, &channel_id},
851 {'C', "connection", "CONNECTION_ID",
852 gettext_noop ("provide information about a particular connection"),
853 GNUNET_YES, &GNUNET_GETOPT_set_string, &conn_id},
855 gettext_noop ("activate echo mode"),
856 GNUNET_NO, &GNUNET_GETOPT_set_one, &echo},
858 gettext_noop ("dump debug information to STDERR"),
859 GNUNET_NO, &GNUNET_GETOPT_set_one, &dump},
860 // {'m', "monitor", NULL,
861 // gettext_noop ("provide information about all events (continuously)"),
862 // GNUNET_NO, &GNUNET_GETOPT_set_one, &monitor_mode},
863 {'o', "open-port", NULL,
864 gettext_noop ("port to listen to (default; 0)"),
865 GNUNET_YES, &GNUNET_GETOPT_set_uint, &listen_port},
866 {'p', "peer", "PEER_ID",
867 gettext_noop ("provide information about a patricular peer"),
868 GNUNET_YES, &GNUNET_GETOPT_set_string, &peer_id},
870 gettext_noop ("provide information about all peers"),
871 GNUNET_NO, &GNUNET_GETOPT_set_one, &request_peers},
872 {'t', "tunnel", "TUNNEL_ID",
873 gettext_noop ("provide information about a particular tunnel"),
874 GNUNET_YES, &GNUNET_GETOPT_set_string, &tunnel_id},
875 {'T', "tunnels", NULL,
876 gettext_noop ("provide information about all tunnels"),
877 GNUNET_NO, &GNUNET_GETOPT_set_one, &request_tunnels},
879 GNUNET_GETOPT_OPTION_END
882 monitor_mode = GNUNET_NO;
884 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
887 res = GNUNET_PROGRAM_run (argc, argv, "gnunet-cadet (OPTIONS | TARGET PORT)",
888 gettext_noop (helpstr),
889 options, &run, NULL);
891 GNUNET_free ((void *) argv);
893 if (GNUNET_OK == res)
899 /* end of gnunet-cadet.c */