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 * Call MESH's monitor API, get info of one connection.
103 * @param cls Closure (unused).
104 * @param tc TaskContext
107 create_channel (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
109 struct GNUNET_PeerIdentity pid;
112 GNUNET_CRYPTO_eddsa_public_key_from_string (target_id,
117 _("Invalid target `%s'\n"),
119 GNUNET_SCHEDULER_shutdown();
122 // GNUNET_MESH_channel_create ()
127 * Method called to retrieve information about each tunnel the mesh peer
130 * @param cls Closure.
131 * @param tunnel_number Tunnel number.
132 * @param origin that started the tunnel (owner).
133 * @param target other endpoint of the tunnel
135 void /* FIXME static */
136 tunnels_callback (void *cls,
137 uint32_t tunnel_number,
138 const struct GNUNET_PeerIdentity *origin,
139 const struct GNUNET_PeerIdentity *target)
141 fprintf (stdout, "Tunnel %s [%u]\n",
142 GNUNET_i2s_full (origin), tunnel_number);
143 fprintf (stdout, "\n");
148 * Method called to retrieve information about each tunnel the mesh peer
151 * @param cls Closure.
152 * @param peer Peer in the tunnel's tree.
153 * @param parent Parent of the current peer. All 0 when peer is root.
156 void /* FIXME static */
157 tunnel_callback (void *cls,
158 const struct GNUNET_PeerIdentity *peer,
159 const struct GNUNET_PeerIdentity *parent)
165 * Call MESH's monitor API, get all tunnels known to peer.
167 * @param cls Closure (unused).
168 * @param tc TaskContext
171 get_tunnels (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
173 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
177 // GNUNET_MESH_get_tunnels (mh, &tunnels_callback, NULL);
178 if (GNUNET_YES != monitor_connections)
180 GNUNET_SCHEDULER_shutdown();
186 * Call MESH's monitor API, get info of one tunnel.
188 * @param cls Closure (unused).
189 * @param tc TaskContext
192 show_tunnel (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
194 struct GNUNET_PeerIdentity pid;
197 GNUNET_CRYPTO_eddsa_public_key_from_string (tunnel_id,
202 _("Invalid tunnel owner `%s'\n"),
204 GNUNET_SCHEDULER_shutdown();
207 // GNUNET_MESH_show_tunnel (mh, &pid, 0, tunnel_callback, NULL);
212 * Call MESH's monitor API, get info of one channel.
214 * @param cls Closure (unused).
215 * @param tc TaskContext
218 show_channel (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
225 * Call MESH's monitor API, get info of one connection.
227 * @param cls Closure (unused).
228 * @param tc TaskContext
231 show_connection (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
239 * Method called whenever another peer has added us to a channel
240 * the other peer initiated.
241 * Only called (once) upon reception of data with a message type which was
242 * subscribed to in #GNUNET_MESH_connect.
244 * A call to #GNUNET_MESH_channel_destroy causes te channel to be ignored. In
245 * this case the handler MUST return NULL.
248 * @param channel new handle to the channel
249 * @param initiator peer that started the channel
250 * @param port Port this channel is for.
251 * @param options MeshOption flag field, with all active option bits set to 1.
253 * @return initial channel context for the channel
254 * (can be NULL -- that's not an error)
257 incoming_channel (void *cls,
258 struct GNUNET_MESH_Channel * channel,
259 const struct GNUNET_PeerIdentity * initiator,
260 uint32_t port, enum MeshOption options)
262 FPRINTF (stdout, "Incoming channel!\n");
269 * Main function that will be run by the scheduler.
272 * @param args remaining command-line arguments
273 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
274 * @param cfg configuration
277 run (void *cls, char *const *args, const char *cfgfile,
278 const struct GNUNET_CONFIGURATION_Handle *cfg)
280 static const struct GNUNET_MESH_MessageHandler handlers[] = {
281 {NULL, 0, 0} /* FIXME add option to monitor msg types */
283 /* FIXME add option to monitor apps */
286 target_port = args[0] && args[1] ? atoi(args[1]) : 0;
288 || 0 != monitor_connections
291 || NULL != channel_id)
292 && target_id != NULL)
294 FPRINTF (stderr, _("You must NOT give a TARGET when using options\n"));
298 if (NULL != target_id)
300 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
301 "Creating channel to %s\n",
303 GNUNET_SCHEDULER_add_now (&create_channel, NULL);
305 else if (NULL != tunnel_id)
307 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Show tunnel\n");
308 GNUNET_SCHEDULER_add_now (&show_tunnel, NULL);
310 else if (NULL != channel_id)
312 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Show channel\n");
313 GNUNET_SCHEDULER_add_now (&show_channel, NULL);
315 else if (NULL != conn_id)
317 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Show connection\n");
318 GNUNET_SCHEDULER_add_now (&show_connection, NULL);
320 else if (GNUNET_YES == get_info)
322 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Show all tunnels\n");
323 GNUNET_SCHEDULER_add_now (&get_tunnels, NULL);
327 FPRINTF (stderr, "No action requested\n");
331 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connecting to mesh\n");
332 mh = GNUNET_MESH_connect (cfg,
334 NULL, /* new tunnel */
338 FPRINTF (stdout, "Done\n");
340 GNUNET_SCHEDULER_add_now (shutdown_task, NULL);
342 sd = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
343 shutdown_task, NULL);
349 * The main function to obtain peer information.
351 * @param argc number of arguments from the command line
352 * @param argv command line arguments
353 * @return 0 ok, 1 on error
356 main (int argc, char *const *argv)
359 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
360 {'a', "channel", "TUNNEL_ID:CHANNEL_ID",
361 gettext_noop ("provide information about a particular channel"),
362 GNUNET_YES, &GNUNET_GETOPT_set_string, &channel_id},
363 {'b', "connection", "TUNNEL_ID:CONNECTION_ID",
364 gettext_noop ("provide information about a particular connection"),
365 GNUNET_YES, &GNUNET_GETOPT_set_string, &conn_id},
367 gettext_noop ("provide information about all tunnels"),
368 GNUNET_NO, &GNUNET_GETOPT_set_one, &get_info},
369 {'m', "monitor", NULL,
370 gettext_noop ("provide information about all tunnels (continuously) NOT IMPLEMENTED"), /* FIXME */
371 GNUNET_NO, &GNUNET_GETOPT_set_one, &monitor_connections},
373 gettext_noop ("port to listen to (default; 0)"),
374 GNUNET_YES, &GNUNET_GETOPT_set_uint, &listen_port},
375 {'t', "tunnel", "TUNNEL_ID",
376 gettext_noop ("provide information about a particular tunnel"),
377 GNUNET_YES, &GNUNET_GETOPT_set_string, &tunnel_id},
378 GNUNET_GETOPT_OPTION_END
381 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
384 res = GNUNET_PROGRAM_run (argc, argv, "gnunet-mesh (OPTIONS | TARGET PORT)",
386 ("Create channels and retreive info about meshs status."),
387 options, &run, NULL);
389 GNUNET_free ((void *) argv);
391 if (GNUNET_OK == res)
397 /* end of gnunet-mesh.c */