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;
39 static char *tunnel_id;
44 static struct GNUNET_MESH_Handle *mh;
47 * Shutdown task handle.
49 GNUNET_SCHEDULER_TaskIdentifier sd;
52 * Task run in monitor mode when the user presses CTRL-C to abort.
53 * Stops monitoring activity.
55 * @param cls Closure (unused).
56 * @param tc scheduler context
59 shutdown_task (void *cls,
60 const struct GNUNET_SCHEDULER_TaskContext *tc)
64 GNUNET_MESH_disconnect (mh);
71 * Method called to retrieve information about each tunnel the mesh peer
75 * @param tunnel_number Tunnel number.
76 * @param origin that started the tunnel (owner).
77 * @param target other endpoint of the tunnel
80 tunnels_callback (void *cls,
81 uint32_t tunnel_number,
82 const struct GNUNET_PeerIdentity *origin,
83 const struct GNUNET_PeerIdentity *target)
85 fprintf (stdout, "Tunnel %s [%u]\n",
86 GNUNET_i2s_full (origin), tunnel_number);
87 fprintf (stdout, "\n");
92 * Method called to retrieve information about each tunnel the mesh peer
96 * @param peer Peer in the tunnel's tree.
97 * @param parent Parent of the current peer. All 0 when peer is root.
101 tunnel_callback (void *cls,
102 const struct GNUNET_PeerIdentity *peer,
103 const struct GNUNET_PeerIdentity *parent)
109 * Call MESH's monitor API, get all tunnels known to peer.
111 * @param cls Closure (unused).
112 * @param tc TaskContext
115 get_tunnels (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
117 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
121 GNUNET_MESH_get_tunnels (mh, &tunnels_callback, NULL);
122 if (GNUNET_YES != monitor_connections)
124 GNUNET_SCHEDULER_shutdown();
130 * Call MESH's monitor API, get info of one tunnel.
132 * @param cls Closure (unused).
133 * @param tc TaskContext
136 show_tunnel (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
138 struct GNUNET_PeerIdentity pid;
141 GNUNET_CRYPTO_ecc_public_sign_key_from_string (tunnel_id,
146 _("Invalid tunnel owner `%s'\n"),
148 GNUNET_SCHEDULER_shutdown();
151 GNUNET_MESH_show_tunnel (mh, &pid, 0, tunnel_callback, NULL);
156 * Main function that will be run by the scheduler.
159 * @param args remaining command-line arguments
160 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
161 * @param cfg configuration
164 run (void *cls, char *const *args, const char *cfgfile,
165 const struct GNUNET_CONFIGURATION_Handle *cfg)
167 static const struct GNUNET_MESH_MessageHandler handlers[] = {
168 {NULL, 0, 0} /* FIXME add option to monitor msg types */
170 /* FIXME add option to monitor apps */
174 FPRINTF (stderr, _("Invalid command line argument `%s'\n"), args[0]);
177 mh = GNUNET_MESH_connect (cfg,
179 NULL, /* new tunnel */
184 GNUNET_SCHEDULER_add_now (shutdown_task, NULL);
186 sd = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
187 shutdown_task, NULL);
189 if (NULL != tunnel_id)
190 GNUNET_SCHEDULER_add_now (&show_tunnel, NULL);
192 GNUNET_SCHEDULER_add_now (&get_tunnels, NULL);
197 * The main function to obtain peer information.
199 * @param argc number of arguments from the command line
200 * @param argv command line arguments
201 * @return 0 ok, 1 on error
204 main (int argc, char *const *argv)
207 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
208 {'m', "monitor", NULL,
209 gettext_noop ("provide information about all tunnels (continuously) NOT IMPLEMENTED"), /* FIXME */
210 GNUNET_NO, &GNUNET_GETOPT_set_one, &monitor_connections},
211 {'t', "tunnel", "OWNER_ID:TUNNEL_ID",
212 gettext_noop ("provide information about a particular tunnel"),
213 GNUNET_YES, &GNUNET_GETOPT_set_string, &tunnel_id},
214 GNUNET_GETOPT_OPTION_END
217 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
220 res = GNUNET_PROGRAM_run (argc, argv, "gnunet-mesh",
222 ("Print information about mesh tunnels and peers."),
223 options, &run, NULL);
225 GNUNET_free ((void *) argv);
227 if (GNUNET_OK == res)
233 /* end of gnunet-mesh.c */