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;
64 static char *target_id;
69 static uint32_t target_port;
72 * Data pending in netcat mode.
80 static struct GNUNET_MESH_Handle *mh;
85 static struct GNUNET_MESH_Channel *ch;
88 * Shutdown task handle.
90 GNUNET_SCHEDULER_TaskIdentifier sd;
100 * Task run in monitor mode when the user presses CTRL-C to abort.
101 * Stops monitoring activity.
103 * @param cls Closure (unused).
104 * @param tc scheduler context
107 shutdown_task (void *cls,
108 const struct GNUNET_SCHEDULER_TaskContext *tc)
112 GNUNET_MESH_channel_destroy (ch);
117 GNUNET_MESH_disconnect (mh);
124 * Function called to notify a client about the connection
125 * begin ready to queue more data. "buf" will be
126 * NULL and "size" zero if the connection was closed for
127 * writing in the meantime.
132 * @param size number of bytes available in buf
133 * @param buf where the callee should write the message
134 * @return number of bytes written to buf
137 data_ready (void *cls, size_t size, void *buf)
139 struct GNUNET_MessageHeader *msg;
142 if (NULL == buf || 0 == size)
144 GNUNET_SCHEDULER_shutdown();
148 total_size = data_size + sizeof (struct GNUNET_MessageHeader);
149 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "sending %u bytes\n", data_size);
150 GNUNET_assert (size >= total_size);
153 msg->size = ntohs (total_size);
154 msg->type = ntohs (GNUNET_MESSAGE_TYPE_MESH_CLI);
155 memcpy (&msg[1], cls, data_size);
163 * Task run in monitor mode when the user presses CTRL-C to abort.
164 * Stops monitoring activity.
166 * @param cls Closure (unused).
167 * @param tc scheduler context
170 read_stdio (void *cls,
171 const struct GNUNET_SCHEDULER_TaskContext *tc)
175 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
180 data_size = read (0, buf, 60000);
181 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "stdio read %u bytes\n", data_size);
184 GNUNET_SCHEDULER_shutdown();
187 GNUNET_MESH_notify_transmit_ready (ch, GNUNET_NO,
188 GNUNET_TIME_UNIT_FOREVER_REL,
190 + sizeof (struct GNUNET_MessageHeader),
196 * Start listening to stdin
201 struct GNUNET_NETWORK_FDSet *rs;
203 rs = GNUNET_NETWORK_fdset_create ();
204 GNUNET_NETWORK_fdset_set_native (rs, 0);
205 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
206 GNUNET_TIME_UNIT_FOREVER_REL,
209 GNUNET_NETWORK_fdset_destroy (rs);
214 * Function called whenever a channel is destroyed. Should clean up
215 * any associated state.
217 * It must NOT call #GNUNET_MESH_channel_destroy on the channel.
219 * @param cls closure (set from #GNUNET_MESH_connect)
220 * @param channel connection to the other end (henceforth invalid)
221 * @param channel_ctx place where local state associated
222 * with the channel is stored
225 channel_ended (void *cls,
226 const struct GNUNET_MESH_Channel *channel,
229 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Channel ended!\n");
230 GNUNET_break (channel == ch);
232 GNUNET_SCHEDULER_shutdown ();
237 * Method called whenever another peer has added us to a channel
238 * the other peer initiated.
239 * Only called (once) upon reception of data with a message type which was
240 * subscribed to in #GNUNET_MESH_connect.
242 * A call to #GNUNET_MESH_channel_destroy causes te channel to be ignored. In
243 * this case the handler MUST return NULL.
246 * @param channel new handle to the channel
247 * @param initiator peer that started the channel
248 * @param port Port this channel is for.
249 * @param options MeshOption flag field, with all active option bits set to 1.
251 * @return initial channel context for the channel
252 * (can be NULL -- that's not an error)
255 channel_incoming (void *cls,
256 struct GNUNET_MESH_Channel * channel,
257 const struct GNUNET_PeerIdentity * initiator,
258 uint32_t port, enum GNUNET_MESH_ChannelOption options)
260 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
261 "Incoming channel %p on port %u\n",
265 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "A channel already exists\n");
268 if (0 == listen_port)
270 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not listening to channels\n");
281 * Call MESH's monitor API, get info of one connection.
283 * @param cls Closure (unused).
284 * @param tc TaskContext
287 create_channel (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
289 struct GNUNET_PeerIdentity pid;
291 GNUNET_assert (NULL == ch);
294 GNUNET_CRYPTO_eddsa_public_key_from_string (target_id,
299 _("Invalid target `%s'\n"),
301 GNUNET_SCHEDULER_shutdown ();
304 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connecting to `%s'\n", target_id);
305 ch = GNUNET_MESH_channel_create (mh, NULL, &pid, target_port,
306 GNUNET_MESH_OPTION_DEFAULT);
312 * Function called whenever a message is received.
314 * Each time the function must call #GNUNET_MESH_receive_done on the channel
315 * in order to receive the next message. This doesn't need to be immediate:
316 * can be delayed if some processing is done on the message.
318 * @param cls Closure (set from #GNUNET_MESH_connect).
319 * @param channel Connection to the other end.
320 * @param channel_ctx Place to store local state associated with the channel.
321 * @param message The actual message.
322 * @return #GNUNET_OK to keep the channel open,
323 * #GNUNET_SYSERR to close it (signal serious error).
326 data_callback (void *cls,
327 struct GNUNET_MESH_Channel *channel,
329 const struct GNUNET_MessageHeader *message)
332 GNUNET_break (ch == channel);
334 len = ntohs (message->size) - sizeof (*message);
335 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got %u bytes\n", len);
336 FPRINTF (stdout, "%.*s", len, (char *) &message[1]);
342 * Method called to retrieve information about each tunnel the mesh peer
345 * @param cls Closure.
346 * @param tunnel_number Tunnel number.
347 * @param origin that started the tunnel (owner).
348 * @param target other endpoint of the tunnel
350 void /* FIXME static */
351 tunnels_callback (void *cls,
352 uint32_t tunnel_number,
353 const struct GNUNET_PeerIdentity *origin,
354 const struct GNUNET_PeerIdentity *target)
356 FPRINTF (stdout, "Tunnel %s [%u]\n",
357 GNUNET_i2s_full (origin), tunnel_number);
358 FPRINTF (stdout, "\n");
363 * Method called to retrieve information about each tunnel the mesh peer
366 * @param cls Closure.
367 * @param peer Peer in the tunnel's tree.
368 * @param parent Parent of the current peer. All 0 when peer is root.
371 void /* FIXME static */
372 tunnel_callback (void *cls,
373 const struct GNUNET_PeerIdentity *peer,
374 const struct GNUNET_PeerIdentity *parent)
380 * Call MESH's monitor API, get all tunnels known to peer.
382 * @param cls Closure (unused).
383 * @param tc TaskContext
386 get_tunnels (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
388 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
392 // GNUNET_MESH_get_tunnels (mh, &tunnels_callback, NULL);
393 if (GNUNET_YES != monitor_connections)
395 GNUNET_SCHEDULER_shutdown();
401 * Call MESH's monitor API, get info of one tunnel.
403 * @param cls Closure (unused).
404 * @param tc TaskContext
407 show_tunnel (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
409 struct GNUNET_PeerIdentity pid;
412 GNUNET_CRYPTO_eddsa_public_key_from_string (tunnel_id,
417 _("Invalid tunnel owner `%s'\n"),
419 GNUNET_SCHEDULER_shutdown();
422 // GNUNET_MESH_show_tunnel (mh, &pid, 0, tunnel_callback, NULL);
427 * Call MESH's monitor API, get info of one channel.
429 * @param cls Closure (unused).
430 * @param tc TaskContext
433 show_channel (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
440 * Call MESH's monitor API, get info of one connection.
442 * @param cls Closure (unused).
443 * @param tc TaskContext
446 show_connection (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
453 * Main function that will be run by the scheduler.
456 * @param args remaining command-line arguments
457 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
458 * @param cfg configuration
461 run (void *cls, char *const *args, const char *cfgfile,
462 const struct GNUNET_CONFIGURATION_Handle *cfg)
464 GNUNET_MESH_InboundChannelNotificationHandler *newch = NULL;
465 GNUNET_MESH_ChannelEndHandler *endch = NULL;
466 static const struct GNUNET_MESH_MessageHandler handlers[] = {
467 {&data_callback, GNUNET_MESSAGE_TYPE_MESH_CLI, 0},
468 {NULL, 0, 0} /* FIXME add option to monitor msg types */
470 static uint32_t *ports = NULL;
471 /* FIXME add option to monitor apps */
474 target_port = args[0] && args[1] ? atoi(args[1]) : 0;
476 || 0 != monitor_connections
479 || NULL != channel_id)
480 && target_id != NULL)
482 FPRINTF (stderr, _("You must NOT give a TARGET when using options\n"));
486 if (NULL != target_id)
488 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
489 "Creating channel to %s\n",
491 GNUNET_SCHEDULER_add_now (&create_channel, NULL);
492 endch = &channel_ended;
494 else if (0 != listen_port)
496 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Listen\n");
497 newch = &channel_incoming;
498 endch = &channel_ended;
499 ports = GNUNET_malloc (sizeof (uint32_t) * 2);
500 ports[0] = listen_port;
502 else if (NULL != tunnel_id)
504 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Show tunnel\n");
505 GNUNET_SCHEDULER_add_now (&show_tunnel, NULL);
507 else if (NULL != channel_id)
509 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Show channel\n");
510 GNUNET_SCHEDULER_add_now (&show_channel, NULL);
512 else if (NULL != conn_id)
514 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Show connection\n");
515 GNUNET_SCHEDULER_add_now (&show_connection, NULL);
517 else if (GNUNET_YES == get_info)
519 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Show all tunnels\n");
520 GNUNET_SCHEDULER_add_now (&get_tunnels, NULL);
524 FPRINTF (stderr, "No action requested\n");
528 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connecting to mesh\n");
529 mh = GNUNET_MESH_connect (cfg,
531 newch, /* new channel */
535 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Done\n");
537 GNUNET_SCHEDULER_add_now (shutdown_task, NULL);
539 sd = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
540 shutdown_task, NULL);
546 * The main function to obtain peer information.
548 * @param argc number of arguments from the command line
549 * @param argv command line arguments
550 * @return 0 ok, 1 on error
553 main (int argc, char *const *argv)
556 const char helpstr[] = "Create channels and retreive info about meshs status.";
557 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
558 {'a', "channel", "TUNNEL_ID:CHANNEL_ID",
559 gettext_noop ("provide information about a particular channel"),
560 GNUNET_YES, &GNUNET_GETOPT_set_string, &channel_id},
561 {'b', "connection", "TUNNEL_ID:CONNECTION_ID",
562 gettext_noop ("provide information about a particular connection"),
563 GNUNET_YES, &GNUNET_GETOPT_set_string, &conn_id},
565 gettext_noop ("provide information about all tunnels"),
566 GNUNET_NO, &GNUNET_GETOPT_set_one, &get_info},
567 {'m', "monitor", NULL,
568 gettext_noop ("provide information about all tunnels (continuously) NOT IMPLEMENTED"), /* FIXME */
569 GNUNET_NO, &GNUNET_GETOPT_set_one, &monitor_connections},
571 gettext_noop ("port to listen to (default; 0)"),
572 GNUNET_YES, &GNUNET_GETOPT_set_uint, &listen_port},
573 {'t', "tunnel", "TUNNEL_ID",
574 gettext_noop ("provide information about a particular tunnel"),
575 GNUNET_YES, &GNUNET_GETOPT_set_string, &tunnel_id},
576 GNUNET_GETOPT_OPTION_END
579 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
582 res = GNUNET_PROGRAM_run (argc, argv, "gnunet-mesh (OPTIONS | TARGET PORT)",
583 gettext_noop (helpstr),
584 options, &run, NULL);
586 GNUNET_free ((void *) argv);
588 if (GNUNET_OK == res)
594 /* end of gnunet-mesh.c */