2 This file is part of GNUnet.
3 (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., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file mesh/gnunet-mesh.c
23 * @brief Print information about mesh tunnels and peers.
24 * @author Bartlomiej Polot
27 #include "gnunet_util_lib.h"
28 #include "gnunet_mesh_service.h"
35 static int monitor_connections;
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 * Time of last echo request.
80 struct GNUNET_TIME_Absolute echo_time;
83 * Task for next echo request.
85 GNUNET_SCHEDULER_TaskIdentifier echo_task;
90 static char *target_id;
95 static uint32_t target_port;
98 * Data pending in netcat mode.
106 static struct GNUNET_MESH_Handle *mh;
111 static struct GNUNET_MESH_Channel *ch;
114 * Shutdown task handle.
116 GNUNET_SCHEDULER_TaskIdentifier sd;
126 * Task run in monitor mode when the user presses CTRL-C to abort.
127 * Stops monitoring activity.
129 * @param cls Closure (unused).
130 * @param tc scheduler context
133 shutdown_task (void *cls,
134 const struct GNUNET_SCHEDULER_TaskContext *tc)
136 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Shutdown\n");
139 GNUNET_MESH_channel_destroy (ch);
144 GNUNET_MESH_disconnect (mh);
151 * Function called to notify a client about the connection
152 * begin ready to queue more data. "buf" will be
153 * NULL and "size" zero if the connection was closed for
154 * writing in the meantime.
159 * @param size number of bytes available in buf
160 * @param buf where the callee should write the message
161 * @return number of bytes written to buf
164 data_ready (void *cls, size_t size, void *buf)
166 struct GNUNET_MessageHeader *msg;
169 if (NULL == buf || 0 == size)
171 GNUNET_SCHEDULER_shutdown();
175 total_size = data_size + sizeof (struct GNUNET_MessageHeader);
176 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "sending %u bytes\n", data_size);
177 GNUNET_assert (size >= total_size);
180 msg->size = htons (total_size);
181 msg->type = htons (GNUNET_MESSAGE_TYPE_MESH_CLI);
182 memcpy (&msg[1], cls, data_size);
183 if (GNUNET_NO == echo)
189 echo_time = GNUNET_TIME_absolute_get ();
197 * Task run in monitor mode when the user presses CTRL-C to abort.
198 * Stops monitoring activity.
200 * @param cls Closure (unused).
201 * @param tc scheduler context
204 read_stdio (void *cls,
205 const struct GNUNET_SCHEDULER_TaskContext *tc)
207 static char buf[60000];
209 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
214 data_size = read (0, buf, 60000);
215 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "stdio read %u bytes\n", data_size);
218 GNUNET_SCHEDULER_shutdown();
221 GNUNET_MESH_notify_transmit_ready (ch, GNUNET_NO,
222 GNUNET_TIME_UNIT_FOREVER_REL,
224 + sizeof (struct GNUNET_MessageHeader),
230 * Start listening to stdin
235 struct GNUNET_NETWORK_FDSet *rs;
237 rs = GNUNET_NETWORK_fdset_create ();
238 GNUNET_NETWORK_fdset_set_native (rs, 0);
239 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
240 GNUNET_TIME_UNIT_FOREVER_REL,
243 GNUNET_NETWORK_fdset_destroy (rs);
248 * Function called whenever a channel is destroyed. Should clean up
249 * any associated state.
251 * It must NOT call #GNUNET_MESH_channel_destroy on the channel.
253 * @param cls closure (set from #GNUNET_MESH_connect)
254 * @param channel connection to the other end (henceforth invalid)
255 * @param channel_ctx place where local state associated
256 * with the channel is stored
259 channel_ended (void *cls,
260 const struct GNUNET_MESH_Channel *channel,
263 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Channel ended!\n");
264 GNUNET_break (channel == ch);
266 GNUNET_SCHEDULER_shutdown ();
271 * Method called whenever another peer has added us to a channel
272 * the other peer initiated.
273 * Only called (once) upon reception of data with a message type which was
274 * subscribed to in #GNUNET_MESH_connect.
276 * A call to #GNUNET_MESH_channel_destroy causes te channel to be ignored. In
277 * this case the handler MUST return NULL.
280 * @param channel new handle to the channel
281 * @param initiator peer that started the channel
282 * @param port Port this channel is for.
283 * @param options MeshOption flag field, with all active option bits set to 1.
285 * @return initial channel context for the channel
286 * (can be NULL -- that's not an error)
289 channel_incoming (void *cls,
290 struct GNUNET_MESH_Channel * channel,
291 const struct GNUNET_PeerIdentity * initiator,
292 uint32_t port, enum GNUNET_MESH_ChannelOption options)
294 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
295 "Incoming channel %p on port %u\n",
299 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "A channel already exists\n");
302 if (0 == listen_port)
304 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not listening to channels\n");
308 if (GNUNET_NO == echo)
318 * @brief Send an echo request to the remote peer.
320 * @param cls Closure (NULL).
321 * @param tc Task context.
324 send_echo (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
326 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) || NULL == ch)
329 GNUNET_MESH_notify_transmit_ready (ch, GNUNET_NO,
330 GNUNET_TIME_UNIT_FOREVER_REL,
331 sizeof (struct GNUNET_MessageHeader),
338 * Call MESH's monitor API, get info of one connection.
340 * @param cls Closure (unused).
341 * @param tc TaskContext
344 create_channel (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
346 struct GNUNET_PeerIdentity pid;
347 enum GNUNET_MESH_ChannelOption opt;
349 GNUNET_assert (NULL == ch);
352 GNUNET_CRYPTO_eddsa_public_key_from_string (target_id,
357 _("Invalid target `%s'\n"),
359 GNUNET_SCHEDULER_shutdown ();
362 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connecting to `%s'\n", target_id);
363 opt = GNUNET_MESH_OPTION_DEFAULT | GNUNET_MESH_OPTION_RELIABLE;
364 ch = GNUNET_MESH_channel_create (mh, NULL, &pid, target_port, opt);
365 if (GNUNET_NO == echo)
368 GNUNET_SCHEDULER_add_now (send_echo, NULL);
373 * Function called whenever a message is received.
375 * Each time the function must call #GNUNET_MESH_receive_done on the channel
376 * in order to receive the next message. This doesn't need to be immediate:
377 * can be delayed if some processing is done on the message.
379 * @param cls Closure (set from #GNUNET_MESH_connect).
380 * @param channel Connection to the other end.
381 * @param channel_ctx Place to store local state associated with the channel.
382 * @param message The actual message.
383 * @return #GNUNET_OK to keep the channel open,
384 * #GNUNET_SYSERR to close it (signal serious error).
387 data_callback (void *cls,
388 struct GNUNET_MESH_Channel *channel,
390 const struct GNUNET_MessageHeader *message)
396 GNUNET_break (ch == channel);
398 if (GNUNET_YES == echo)
400 if (0 != listen_port)
402 /* Just listening to echo incoming messages*/
403 GNUNET_MESH_notify_transmit_ready (channel, GNUNET_NO,
404 GNUNET_TIME_UNIT_FOREVER_REL,
405 sizeof (struct GNUNET_MessageHeader),
411 struct GNUNET_TIME_Relative latency;
413 latency = GNUNET_TIME_absolute_get_duration (echo_time);
414 echo_time = GNUNET_TIME_UNIT_FOREVER_ABS;
415 FPRINTF (stdout, "time: %s\n",
416 GNUNET_STRINGS_relative_time_to_string (latency, GNUNET_NO));
417 echo_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
422 len = ntohs (message->size) - sizeof (*message);
423 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got %u bytes\n", len);
424 buf = (const char *) &message[1];
428 done = write (1, &buf[off], len - off);
432 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
434 return GNUNET_SYSERR;
443 * Method called to retrieve information about all peers in MESH, called
446 * After last peer has been reported, an additional call with NULL is done.
448 * @param cls Closure.
449 * @param peer Peer, or NULL on "EOF".
450 * @param tunnel Do we have a tunnel towards this peer?
451 * @param n_paths Number of known paths towards this peer.
452 * @param best_path How long is the best path?
453 * (0 = unknown, 1 = ourselves, 2 = neighbor)
456 peers_callback (void *cls, const struct GNUNET_PeerIdentity *peer,
457 int tunnel, unsigned int n_paths, unsigned int best_path)
461 if (GNUNET_YES != monitor_connections)
463 GNUNET_SCHEDULER_shutdown();
467 FPRINTF (stdout, "%s tunnel: %c, paths: %u\n",
468 GNUNET_i2s_full (peer), tunnel ? 'Y' : 'N', n_paths);
472 * Method called to retrieve information about a specific peer
473 * known to the service.
475 * @param cls Closure.
476 * @param peer Peer ID.
477 * @param tunnel Do we have a tunnel towards this peer? #GNUNET_YES/#GNUNET_NO
478 * @param neighbor Is this a direct neighbor? #GNUNET_YES/#GNUNET_NO
479 * @param n_paths Number of paths known towards peer.
480 * @param paths Array of PEER_IDs representing all paths to reach the peer.
481 * Each path starts with the local peer.
482 * Each path ends with the destination peer (given in @c peer).
485 peer_callback (void *cls,
486 const struct GNUNET_PeerIdentity *peer,
489 unsigned int n_paths,
490 struct GNUNET_PeerIdentity *paths)
496 * Method called to retrieve information about all tunnels in MESH.
498 * @param cls Closure.
499 * @param peer Destination peer.
500 * @param channels Number of channels.
501 * @param connections Number of connections.
502 * @param estate Encryption state.
503 * @param cstate Connectivity state.
506 tunnels_callback (void *cls,
507 const struct GNUNET_PeerIdentity *peer,
508 unsigned int channels,
509 unsigned int connections,
515 if (GNUNET_YES != monitor_connections)
517 GNUNET_SCHEDULER_shutdown();
521 FPRINTF (stdout, "%s [ENC: %u, CON: %u] CHs: %u, CONNs: %u\n",
522 GNUNET_i2s_full (peer), estate, cstate, channels, connections);
527 * Method called to retrieve information about a specific tunnel the mesh peer
528 * has established, o`r is trying to establish.
530 * @param cls Closure.
531 * @param peer Peer towards whom the tunnel is directed.
532 * @param n_channels Number of channels.
533 * @param n_connections Number of connections.
534 * @param channels Channels.
535 * @param connections Connections.
536 * @param estate Encryption status.
537 * @param cstate Connectivity status.
540 tunnel_callback (void *cls,
541 const struct GNUNET_PeerIdentity *peer,
542 unsigned int n_channels,
543 unsigned int n_connections,
545 struct GNUNET_MESH_Hash *connections,
553 FPRINTF (stdout, "Tunnel %s\n", GNUNET_i2s_full (peer));
554 FPRINTF (stdout, "- %u channels\n", n_channels);
555 for (i = 0; i < n_channels; i++)
556 FPRINTF (stdout, " %u\n", channels[i]);
557 FPRINTF (stdout, "- %u connections\n", n_connections);
558 for (i = 0; i < n_connections; i++)
559 FPRINTF (stdout, " %s\n", GM_h2s (&connections[i]));
560 FPRINTF (stdout, "- enc state: %u\n", estate);
561 FPRINTF (stdout, "- con state: %u\n", cstate);
563 if (GNUNET_YES != monitor_connections)
565 GNUNET_SCHEDULER_shutdown();
573 * Call MESH's meta API, get all peers known to a peer.
575 * @param cls Closure (unused).
576 * @param tc TaskContext
579 get_peers (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
581 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
583 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Shutdown\n");
586 GNUNET_MESH_get_peers (mh, &peers_callback, NULL);
591 * Call MESH's monitor API, get info of one peer.
593 * @param cls Closure (unused).
594 * @param tc TaskContext
597 show_peer (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
599 struct GNUNET_PeerIdentity pid;
602 GNUNET_CRYPTO_eddsa_public_key_from_string (peer_id,
607 _("Invalid peer ID `%s'\n"),
609 GNUNET_SCHEDULER_shutdown();
612 GNUNET_MESH_get_peer (mh, &pid, peer_callback, NULL);
616 * Call MESH's meta API, get all tunnels known to a peer.
618 * @param cls Closure (unused).
619 * @param tc TaskContext
622 get_tunnels (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
624 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
626 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Shutdown\n");
629 GNUNET_MESH_get_tunnels (mh, &tunnels_callback, NULL);
634 * Call MESH's monitor API, get info of one tunnel.
636 * @param cls Closure (unused).
637 * @param tc TaskContext
640 show_tunnel (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
642 struct GNUNET_PeerIdentity pid;
645 GNUNET_CRYPTO_eddsa_public_key_from_string (tunnel_id,
650 _("Invalid tunnel owner `%s'\n"),
652 GNUNET_SCHEDULER_shutdown();
655 GNUNET_MESH_get_tunnel (mh, &pid, tunnel_callback, NULL);
660 * Call MESH's monitor API, get info of one channel.
662 * @param cls Closure (unused).
663 * @param tc TaskContext
666 show_channel (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
673 * Call MESH's monitor API, get info of one connection.
675 * @param cls Closure (unused).
676 * @param tc TaskContext
679 show_connection (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
686 * Main function that will be run by the scheduler.
689 * @param args remaining command-line arguments
690 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
691 * @param cfg configuration
694 run (void *cls, char *const *args, const char *cfgfile,
695 const struct GNUNET_CONFIGURATION_Handle *cfg)
697 GNUNET_MESH_InboundChannelNotificationHandler *newch = NULL;
698 GNUNET_MESH_ChannelEndHandler *endch = NULL;
699 static const struct GNUNET_MESH_MessageHandler handlers[] = {
700 {&data_callback, GNUNET_MESSAGE_TYPE_MESH_CLI, 0},
701 {NULL, 0, 0} /* FIXME add option to monitor msg types */
703 static uint32_t *ports = NULL;
704 /* FIXME add option to monitor apps */
707 target_port = args[0] && args[1] ? atoi(args[1]) : 0;
708 if ( (0 != (request_peers | request_tunnels)
709 || 0 != monitor_connections
712 || NULL != channel_id)
713 && target_id != NULL)
716 _("You must NOT give a TARGET"
717 "when using 'request all' options\n"));
721 if (NULL != target_id)
723 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
724 "Creating channel to %s\n",
726 GNUNET_SCHEDULER_add_now (&create_channel, NULL);
727 endch = &channel_ended;
729 else if (0 != listen_port)
731 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Listen\n");
732 newch = &channel_incoming;
733 endch = &channel_ended;
734 ports = GNUNET_malloc (sizeof (uint32_t) * 2);
735 ports[0] = listen_port;
737 else if (NULL != peer_id)
739 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Show peer\n");
740 GNUNET_SCHEDULER_add_now (&show_peer, NULL);
742 else if (NULL != tunnel_id)
744 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Show tunnel\n");
745 GNUNET_SCHEDULER_add_now (&show_tunnel, NULL);
747 else if (NULL != channel_id)
749 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Show channel\n");
750 GNUNET_SCHEDULER_add_now (&show_channel, NULL);
752 else if (NULL != conn_id)
754 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Show connection\n");
755 GNUNET_SCHEDULER_add_now (&show_connection, NULL);
757 else if (GNUNET_YES == request_peers)
759 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Show all peers\n");
760 GNUNET_SCHEDULER_add_now (&get_peers, NULL);
762 else if (GNUNET_YES == request_tunnels)
764 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Show all tunnels\n");
765 GNUNET_SCHEDULER_add_now (&get_tunnels, NULL);
769 FPRINTF (stderr, "No action requested\n");
773 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connecting to mesh\n");
774 mh = GNUNET_MESH_connect (cfg,
776 newch, /* new channel */
780 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Done\n");
782 GNUNET_SCHEDULER_add_now (shutdown_task, NULL);
784 sd = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
785 shutdown_task, NULL);
791 * The main function to obtain peer information.
793 * @param argc number of arguments from the command line
794 * @param argv command line arguments
795 * @return 0 ok, 1 on error
798 main (int argc, char *const *argv)
801 const char helpstr[] = "Create channels and retreive info about meshs status.";
802 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
803 // {'a', "channel", "TUNNEL_ID:CHANNEL_ID",
804 // gettext_noop ("provide information about a particular channel"),
805 // GNUNET_YES, &GNUNET_GETOPT_set_string, &channel_id},
806 {'C', "connection", "CONNECTION_ID",
807 gettext_noop ("provide information about a particular connection"),
808 GNUNET_YES, &GNUNET_GETOPT_set_string, &conn_id},
810 gettext_noop ("activate echo mode"),
811 GNUNET_NO, &GNUNET_GETOPT_set_one, &echo},
812 // {'m', "monitor", NULL,
813 // gettext_noop ("provide information about all tunnels (continuously) NOT IMPLEMENTED"), /* FIXME */
814 // GNUNET_NO, &GNUNET_GETOPT_set_one, &monitor_connections},
815 {'o', "open-port", NULL,
816 gettext_noop ("port to listen to (default; 0)"),
817 GNUNET_YES, &GNUNET_GETOPT_set_uint, &listen_port},
818 {'p', "peer", "PEER_ID",
819 gettext_noop ("provide information about all peers"),
820 GNUNET_NO, &GNUNET_GETOPT_set_string, &peer_id},
822 gettext_noop ("provide information about all peers"),
823 GNUNET_NO, &GNUNET_GETOPT_set_one, &request_peers},
824 {'t', "tunnel", "TUNNEL_ID",
825 gettext_noop ("provide information about a particular tunnel"),
826 GNUNET_YES, &GNUNET_GETOPT_set_string, &tunnel_id},
827 {'T', "tunnels", NULL,
828 gettext_noop ("provide information about all tunnels"),
829 GNUNET_NO, &GNUNET_GETOPT_set_one, &request_tunnels},
831 GNUNET_GETOPT_OPTION_END
834 monitor_connections = GNUNET_NO;
836 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
839 res = GNUNET_PROGRAM_run (argc, argv, "gnunet-mesh (OPTIONS | TARGET PORT)",
840 gettext_noop (helpstr),
841 options, &run, NULL);
843 GNUNET_free ((void *) argv);
845 if (GNUNET_OK == res)
851 /* end of gnunet-mesh.c */