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"
34 static int monitor_connections;
44 static char *tunnel_id;
54 static char *channel_id;
57 * Port to listen on (-p).
59 static uint32_t listen_port;
62 * Request echo service
67 * Time of last echo request.
69 struct GNUNET_TIME_Absolute echo_time;
72 * Task for next echo request.
74 GNUNET_SCHEDULER_TaskIdentifier echo_task;
79 static char *target_id;
84 static uint32_t target_port;
87 * Data pending in netcat mode.
95 static struct GNUNET_MESH_Handle *mh;
100 static struct GNUNET_MESH_Channel *ch;
103 * Shutdown task handle.
105 GNUNET_SCHEDULER_TaskIdentifier sd;
115 * Task run in monitor mode when the user presses CTRL-C to abort.
116 * Stops monitoring activity.
118 * @param cls Closure (unused).
119 * @param tc scheduler context
122 shutdown_task (void *cls,
123 const struct GNUNET_SCHEDULER_TaskContext *tc)
127 GNUNET_MESH_channel_destroy (ch);
132 GNUNET_MESH_disconnect (mh);
139 * Function called to notify a client about the connection
140 * begin ready to queue more data. "buf" will be
141 * NULL and "size" zero if the connection was closed for
142 * writing in the meantime.
147 * @param size number of bytes available in buf
148 * @param buf where the callee should write the message
149 * @return number of bytes written to buf
152 data_ready (void *cls, size_t size, void *buf)
154 struct GNUNET_MessageHeader *msg;
157 if (NULL == buf || 0 == size)
159 GNUNET_SCHEDULER_shutdown();
163 total_size = data_size + sizeof (struct GNUNET_MessageHeader);
164 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "sending %u bytes\n", data_size);
165 GNUNET_assert (size >= total_size);
168 msg->size = htons (total_size);
169 msg->type = htons (GNUNET_MESSAGE_TYPE_MESH_CLI);
170 memcpy (&msg[1], cls, data_size);
171 if (GNUNET_NO == echo)
177 echo_time = GNUNET_TIME_absolute_get ();
185 * Task run in monitor mode when the user presses CTRL-C to abort.
186 * Stops monitoring activity.
188 * @param cls Closure (unused).
189 * @param tc scheduler context
192 read_stdio (void *cls,
193 const struct GNUNET_SCHEDULER_TaskContext *tc)
195 static char buf[60000];
197 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
202 data_size = read (0, buf, 60000);
203 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "stdio read %u bytes\n", data_size);
206 GNUNET_SCHEDULER_shutdown();
209 GNUNET_MESH_notify_transmit_ready (ch, GNUNET_NO,
210 GNUNET_TIME_UNIT_FOREVER_REL,
212 + sizeof (struct GNUNET_MessageHeader),
218 * Start listening to stdin
223 struct GNUNET_NETWORK_FDSet *rs;
225 rs = GNUNET_NETWORK_fdset_create ();
226 GNUNET_NETWORK_fdset_set_native (rs, 0);
227 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
228 GNUNET_TIME_UNIT_FOREVER_REL,
231 GNUNET_NETWORK_fdset_destroy (rs);
236 * Function called whenever a channel is destroyed. Should clean up
237 * any associated state.
239 * It must NOT call #GNUNET_MESH_channel_destroy on the channel.
241 * @param cls closure (set from #GNUNET_MESH_connect)
242 * @param channel connection to the other end (henceforth invalid)
243 * @param channel_ctx place where local state associated
244 * with the channel is stored
247 channel_ended (void *cls,
248 const struct GNUNET_MESH_Channel *channel,
251 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Channel ended!\n");
252 GNUNET_break (channel == ch);
254 GNUNET_SCHEDULER_shutdown ();
259 * Method called whenever another peer has added us to a channel
260 * the other peer initiated.
261 * Only called (once) upon reception of data with a message type which was
262 * subscribed to in #GNUNET_MESH_connect.
264 * A call to #GNUNET_MESH_channel_destroy causes te channel to be ignored. In
265 * this case the handler MUST return NULL.
268 * @param channel new handle to the channel
269 * @param initiator peer that started the channel
270 * @param port Port this channel is for.
271 * @param options MeshOption flag field, with all active option bits set to 1.
273 * @return initial channel context for the channel
274 * (can be NULL -- that's not an error)
277 channel_incoming (void *cls,
278 struct GNUNET_MESH_Channel * channel,
279 const struct GNUNET_PeerIdentity * initiator,
280 uint32_t port, enum GNUNET_MESH_ChannelOption options)
282 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
283 "Incoming channel %p on port %u\n",
287 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "A channel already exists\n");
290 if (0 == listen_port)
292 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not listening to channels\n");
296 if (GNUNET_NO == echo)
306 * @brief Send an echo request to the remote peer.
308 * @param cls Closure (NULL).
309 * @param tc Task context.
312 send_echo (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
314 GNUNET_MESH_notify_transmit_ready (ch, GNUNET_NO,
315 GNUNET_TIME_UNIT_FOREVER_REL,
316 sizeof (struct GNUNET_MessageHeader),
323 * Call MESH's monitor API, get info of one connection.
325 * @param cls Closure (unused).
326 * @param tc TaskContext
329 create_channel (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
331 struct GNUNET_PeerIdentity pid;
332 enum GNUNET_MESH_ChannelOption opt;
334 GNUNET_assert (NULL == ch);
337 GNUNET_CRYPTO_eddsa_public_key_from_string (target_id,
342 _("Invalid target `%s'\n"),
344 GNUNET_SCHEDULER_shutdown ();
347 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connecting to `%s'\n", target_id);
348 opt = GNUNET_MESH_OPTION_DEFAULT | GNUNET_MESH_OPTION_RELIABLE;
349 ch = GNUNET_MESH_channel_create (mh, NULL, &pid, target_port, opt);
350 if (GNUNET_NO == echo)
353 GNUNET_SCHEDULER_add_now (send_echo, NULL);
358 * Function called whenever a message is received.
360 * Each time the function must call #GNUNET_MESH_receive_done on the channel
361 * in order to receive the next message. This doesn't need to be immediate:
362 * can be delayed if some processing is done on the message.
364 * @param cls Closure (set from #GNUNET_MESH_connect).
365 * @param channel Connection to the other end.
366 * @param channel_ctx Place to store local state associated with the channel.
367 * @param message The actual message.
368 * @return #GNUNET_OK to keep the channel open,
369 * #GNUNET_SYSERR to close it (signal serious error).
372 data_callback (void *cls,
373 struct GNUNET_MESH_Channel *channel,
375 const struct GNUNET_MessageHeader *message)
381 GNUNET_break (ch == channel);
383 if (GNUNET_YES == echo)
385 if (0 != listen_port)
387 /* Just listening to echo incoming messages*/
388 GNUNET_MESH_notify_transmit_ready (channel, GNUNET_NO,
389 GNUNET_TIME_UNIT_FOREVER_REL,
390 sizeof (struct GNUNET_MessageHeader),
396 struct GNUNET_TIME_Relative latency;
398 latency = GNUNET_TIME_absolute_get_duration (echo_time);
399 echo_time = GNUNET_TIME_UNIT_FOREVER_ABS;
400 FPRINTF (stdout, "time: %s\n",
401 GNUNET_STRINGS_relative_time_to_string (latency, GNUNET_NO));
402 echo_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
407 len = ntohs (message->size) - sizeof (*message);
408 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got %u bytes\n", len);
409 buf = (const char *) &message[1];
413 done = write (1, &buf[off], len - off);
417 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
419 return GNUNET_SYSERR;
428 * Method called to retrieve information about all tunnels in MESH.
430 * @param cls Closure.
431 * @param peer Destination peer.
432 * @param channels Number of channels.
433 * @param connections Number of connections.
434 * @param estate Encryption state.
435 * @param cstate Connectivity state.
438 tunnels_callback (void *cls,
439 const struct GNUNET_PeerIdentity *peer,
440 unsigned int channels,
441 unsigned int connections,
445 FPRINTF (stdout, "%s [%u, %u] CH: %u, C: %u\n",
446 GNUNET_i2s_full (peer), estate, cstate, channels, connections);
451 * Method called to retrieve information about a specific tunnel the mesh peer
452 * has established, o`r is trying to establish.
454 * @param cls Closure.
455 * @param peer Peer towards whom the tunnel is directed.
456 * @param channels Number of channels.
457 * @param connections Number of connections.
458 * @param estate Encryption status.
459 * @param cstate Connectivity status.
462 tunnel_callback (void *cls,
463 const struct GNUNET_PeerIdentity *peer,
464 unsigned int channels,
465 unsigned int connections,
469 FPRINTF (stdout, "Tunnel %s\n", GNUNET_i2s_full (peer));
470 FPRINTF (stdout, "- %u channels\n", channels);
471 FPRINTF (stdout, "- %u connections\n", connections);
472 FPRINTF (stdout, "- enc state: %u\n", estate);
473 FPRINTF (stdout, "- con state: %u\n", cstate);
478 * Call MESH's monitor API, get all tunnels known to peer.
480 * @param cls Closure (unused).
481 * @param tc TaskContext
484 get_tunnels (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
486 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
490 GNUNET_MESH_get_tunnels (mh, &tunnels_callback, NULL);
491 if (GNUNET_YES != monitor_connections)
493 GNUNET_SCHEDULER_shutdown();
499 * Call MESH's monitor API, get info of one tunnel.
501 * @param cls Closure (unused).
502 * @param tc TaskContext
505 show_tunnel (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
507 struct GNUNET_PeerIdentity pid;
510 GNUNET_CRYPTO_eddsa_public_key_from_string (tunnel_id,
515 _("Invalid tunnel owner `%s'\n"),
517 GNUNET_SCHEDULER_shutdown();
520 GNUNET_MESH_get_tunnel (mh, &pid, tunnel_callback, NULL);
525 * Call MESH's monitor API, get info of one channel.
527 * @param cls Closure (unused).
528 * @param tc TaskContext
531 show_channel (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
538 * Call MESH's monitor API, get info of one connection.
540 * @param cls Closure (unused).
541 * @param tc TaskContext
544 show_connection (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
551 * Main function that will be run by the scheduler.
554 * @param args remaining command-line arguments
555 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
556 * @param cfg configuration
559 run (void *cls, char *const *args, const char *cfgfile,
560 const struct GNUNET_CONFIGURATION_Handle *cfg)
562 GNUNET_MESH_InboundChannelNotificationHandler *newch = NULL;
563 GNUNET_MESH_ChannelEndHandler *endch = NULL;
564 static const struct GNUNET_MESH_MessageHandler handlers[] = {
565 {&data_callback, GNUNET_MESSAGE_TYPE_MESH_CLI, 0},
566 {NULL, 0, 0} /* FIXME add option to monitor msg types */
568 static uint32_t *ports = NULL;
569 /* FIXME add option to monitor apps */
572 target_port = args[0] && args[1] ? atoi(args[1]) : 0;
574 || 0 != monitor_connections
577 || NULL != channel_id)
578 && target_id != NULL)
580 FPRINTF (stderr, _("You must NOT give a TARGET when using options\n"));
584 if (NULL != target_id)
586 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
587 "Creating channel to %s\n",
589 GNUNET_SCHEDULER_add_now (&create_channel, NULL);
590 endch = &channel_ended;
592 else if (0 != listen_port)
594 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Listen\n");
595 newch = &channel_incoming;
596 endch = &channel_ended;
597 ports = GNUNET_malloc (sizeof (uint32_t) * 2);
598 ports[0] = listen_port;
600 else if (NULL != tunnel_id)
602 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Show tunnel\n");
603 GNUNET_SCHEDULER_add_now (&show_tunnel, NULL);
605 else if (NULL != channel_id)
607 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Show channel\n");
608 GNUNET_SCHEDULER_add_now (&show_channel, NULL);
610 else if (NULL != conn_id)
612 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Show connection\n");
613 GNUNET_SCHEDULER_add_now (&show_connection, NULL);
615 else if (GNUNET_YES == get_info)
617 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Show all tunnels\n");
618 GNUNET_SCHEDULER_add_now (&get_tunnels, NULL);
622 FPRINTF (stderr, "No action requested\n");
626 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connecting to mesh\n");
627 mh = GNUNET_MESH_connect (cfg,
629 newch, /* new channel */
633 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Done\n");
635 GNUNET_SCHEDULER_add_now (shutdown_task, NULL);
637 sd = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
638 shutdown_task, NULL);
644 * The main function to obtain peer information.
646 * @param argc number of arguments from the command line
647 * @param argv command line arguments
648 * @return 0 ok, 1 on error
651 main (int argc, char *const *argv)
654 const char helpstr[] = "Create channels and retreive info about meshs status.";
655 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
656 {'a', "channel", "TUNNEL_ID:CHANNEL_ID",
657 gettext_noop ("provide information about a particular channel"),
658 GNUNET_YES, &GNUNET_GETOPT_set_string, &channel_id},
659 {'b', "connection", "TUNNEL_ID:CONNECTION_ID",
660 gettext_noop ("provide information about a particular connection"),
661 GNUNET_YES, &GNUNET_GETOPT_set_string, &conn_id},
663 gettext_noop ("activate echo mode"),
664 GNUNET_NO, &GNUNET_GETOPT_set_one, &echo},
666 gettext_noop ("provide information about all tunnels"),
667 GNUNET_NO, &GNUNET_GETOPT_set_one, &get_info},
668 {'m', "monitor", NULL,
669 gettext_noop ("provide information about all tunnels (continuously) NOT IMPLEMENTED"), /* FIXME */
670 GNUNET_NO, &GNUNET_GETOPT_set_one, &monitor_connections},
672 gettext_noop ("port to listen to (default; 0)"),
673 GNUNET_YES, &GNUNET_GETOPT_set_uint, &listen_port},
674 {'t', "tunnel", "TUNNEL_ID",
675 gettext_noop ("provide information about a particular tunnel"),
676 GNUNET_YES, &GNUNET_GETOPT_set_string, &tunnel_id},
678 GNUNET_GETOPT_OPTION_END
681 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
684 res = GNUNET_PROGRAM_run (argc, argv, "gnunet-mesh (OPTIONS | TARGET PORT)",
685 gettext_noop (helpstr),
686 options, &run, NULL);
688 GNUNET_free ((void *) argv);
690 if (GNUNET_OK == res)
696 /* end of gnunet-mesh.c */