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;
74 static struct GNUNET_MESH_Handle *mh;
77 * Shutdown task handle.
79 GNUNET_SCHEDULER_TaskIdentifier sd;
82 * Task run in monitor mode when the user presses CTRL-C to abort.
83 * Stops monitoring activity.
85 * @param cls Closure (unused).
86 * @param tc scheduler context
89 shutdown_task (void *cls,
90 const struct GNUNET_SCHEDULER_TaskContext *tc)
94 GNUNET_MESH_disconnect (mh);
101 * Function called whenever a channel is destroyed. Should clean up
102 * any associated state.
104 * It must NOT call #GNUNET_MESH_channel_destroy on the channel.
106 * @param cls closure (set from #GNUNET_MESH_connect)
107 * @param channel connection to the other end (henceforth invalid)
108 * @param channel_ctx place where local state associated
109 * with the channel is stored
112 channel_ended (void *cls,
113 const struct GNUNET_MESH_Channel *channel,
116 FPRINTF (stdout, "Channel ended!\n");
121 * Method called whenever another peer has added us to a channel
122 * the other peer initiated.
123 * Only called (once) upon reception of data with a message type which was
124 * subscribed to in #GNUNET_MESH_connect.
126 * A call to #GNUNET_MESH_channel_destroy causes te channel to be ignored. In
127 * this case the handler MUST return NULL.
130 * @param channel new handle to the channel
131 * @param initiator peer that started the channel
132 * @param port Port this channel is for.
133 * @param options MeshOption flag field, with all active option bits set to 1.
135 * @return initial channel context for the channel
136 * (can be NULL -- that's not an error)
139 channel_incoming (void *cls,
140 struct GNUNET_MESH_Channel * channel,
141 const struct GNUNET_PeerIdentity * initiator,
142 uint32_t port, enum MeshOption options)
144 FPRINTF (stdout, "Incoming channel!\n");
151 * Call MESH's monitor API, get info of one connection.
153 * @param cls Closure (unused).
154 * @param tc TaskContext
157 create_channel (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
159 struct GNUNET_PeerIdentity pid;
162 GNUNET_CRYPTO_eddsa_public_key_from_string (target_id,
167 _("Invalid target `%s'\n"),
169 GNUNET_SCHEDULER_shutdown();
172 // GNUNET_MESH_channel_create ()
177 * Method called to retrieve information about each tunnel the mesh peer
180 * @param cls Closure.
181 * @param tunnel_number Tunnel number.
182 * @param origin that started the tunnel (owner).
183 * @param target other endpoint of the tunnel
185 void /* FIXME static */
186 tunnels_callback (void *cls,
187 uint32_t tunnel_number,
188 const struct GNUNET_PeerIdentity *origin,
189 const struct GNUNET_PeerIdentity *target)
191 fprintf (stdout, "Tunnel %s [%u]\n",
192 GNUNET_i2s_full (origin), tunnel_number);
193 fprintf (stdout, "\n");
198 * Method called to retrieve information about each tunnel the mesh peer
201 * @param cls Closure.
202 * @param peer Peer in the tunnel's tree.
203 * @param parent Parent of the current peer. All 0 when peer is root.
206 void /* FIXME static */
207 tunnel_callback (void *cls,
208 const struct GNUNET_PeerIdentity *peer,
209 const struct GNUNET_PeerIdentity *parent)
215 * Call MESH's monitor API, get all tunnels known to peer.
217 * @param cls Closure (unused).
218 * @param tc TaskContext
221 get_tunnels (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
223 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
227 // GNUNET_MESH_get_tunnels (mh, &tunnels_callback, NULL);
228 if (GNUNET_YES != monitor_connections)
230 GNUNET_SCHEDULER_shutdown();
236 * Call MESH's monitor API, get info of one tunnel.
238 * @param cls Closure (unused).
239 * @param tc TaskContext
242 show_tunnel (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
244 struct GNUNET_PeerIdentity pid;
247 GNUNET_CRYPTO_eddsa_public_key_from_string (tunnel_id,
252 _("Invalid tunnel owner `%s'\n"),
254 GNUNET_SCHEDULER_shutdown();
257 // GNUNET_MESH_show_tunnel (mh, &pid, 0, tunnel_callback, NULL);
262 * Call MESH's monitor API, get info of one channel.
264 * @param cls Closure (unused).
265 * @param tc TaskContext
268 show_channel (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
275 * Call MESH's monitor API, get info of one connection.
277 * @param cls Closure (unused).
278 * @param tc TaskContext
281 show_connection (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
288 * Main function that will be run by the scheduler.
291 * @param args remaining command-line arguments
292 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
293 * @param cfg configuration
296 run (void *cls, char *const *args, const char *cfgfile,
297 const struct GNUNET_CONFIGURATION_Handle *cfg)
299 static const struct GNUNET_MESH_MessageHandler handlers[] = {
300 {NULL, 0, 0} /* FIXME add option to monitor msg types */
302 /* FIXME add option to monitor apps */
305 target_port = args[0] && args[1] ? atoi(args[1]) : 0;
307 || 0 != monitor_connections
310 || NULL != channel_id)
311 && target_id != NULL)
313 FPRINTF (stderr, _("You must NOT give a TARGET when using options\n"));
317 if (NULL != target_id)
319 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
320 "Creating channel to %s\n",
322 GNUNET_SCHEDULER_add_now (&create_channel, NULL);
324 else if (NULL != tunnel_id)
326 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Show tunnel\n");
327 GNUNET_SCHEDULER_add_now (&show_tunnel, NULL);
329 else if (NULL != channel_id)
331 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Show channel\n");
332 GNUNET_SCHEDULER_add_now (&show_channel, NULL);
334 else if (NULL != conn_id)
336 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Show connection\n");
337 GNUNET_SCHEDULER_add_now (&show_connection, NULL);
339 else if (GNUNET_YES == get_info)
341 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Show all tunnels\n");
342 GNUNET_SCHEDULER_add_now (&get_tunnels, NULL);
346 FPRINTF (stderr, "No action requested\n");
350 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connecting to mesh\n");
351 mh = GNUNET_MESH_connect (cfg,
353 NULL, /* new tunnel */
357 FPRINTF (stdout, "Done\n");
359 GNUNET_SCHEDULER_add_now (shutdown_task, NULL);
361 sd = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
362 shutdown_task, NULL);
368 * The main function to obtain peer information.
370 * @param argc number of arguments from the command line
371 * @param argv command line arguments
372 * @return 0 ok, 1 on error
375 main (int argc, char *const *argv)
378 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
379 {'a', "channel", "TUNNEL_ID:CHANNEL_ID",
380 gettext_noop ("provide information about a particular channel"),
381 GNUNET_YES, &GNUNET_GETOPT_set_string, &channel_id},
382 {'b', "connection", "TUNNEL_ID:CONNECTION_ID",
383 gettext_noop ("provide information about a particular connection"),
384 GNUNET_YES, &GNUNET_GETOPT_set_string, &conn_id},
386 gettext_noop ("provide information about all tunnels"),
387 GNUNET_NO, &GNUNET_GETOPT_set_one, &get_info},
388 {'m', "monitor", NULL,
389 gettext_noop ("provide information about all tunnels (continuously) NOT IMPLEMENTED"), /* FIXME */
390 GNUNET_NO, &GNUNET_GETOPT_set_one, &monitor_connections},
392 gettext_noop ("port to listen to (default; 0)"),
393 GNUNET_YES, &GNUNET_GETOPT_set_uint, &listen_port},
394 {'t', "tunnel", "TUNNEL_ID",
395 gettext_noop ("provide information about a particular tunnel"),
396 GNUNET_YES, &GNUNET_GETOPT_set_string, &tunnel_id},
397 GNUNET_GETOPT_OPTION_END
400 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
403 res = GNUNET_PROGRAM_run (argc, argv, "gnunet-mesh (OPTIONS | TARGET PORT)",
405 ("Create channels and retreive info about meshs status."),
406 options, &run, NULL);
408 GNUNET_free ((void *) argv);
410 if (GNUNET_OK == res)
416 /* end of gnunet-mesh.c */