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_configuration_lib.h"
28 #include "gnunet_getopt_lib.h"
29 #include "gnunet_mesh_service.h"
30 #include "gnunet_program_lib.h"
36 static int monitor_connections;
41 static char *tunnel_id;
46 static struct GNUNET_MESH_Handle *mh;
49 * Shutdown task handle.
51 GNUNET_SCHEDULER_TaskIdentifier sd;
54 * Task run in monitor mode when the user presses CTRL-C to abort.
55 * Stops monitoring activity.
57 * @param cls Closure (unused).
58 * @param tc scheduler context
61 shutdown_task (void *cls,
62 const struct GNUNET_SCHEDULER_TaskContext *tc)
66 GNUNET_MESH_disconnect (mh);
73 * Method called to retrieve information about each tunnel the mesh peer
77 * @param tunnel_number Tunnel number.
78 * @param origin that started the tunnel (owner).
79 * @param target other endpoint of the tunnel
82 tunnels_callback (void *cls,
83 uint32_t tunnel_number,
84 const struct GNUNET_PeerIdentity *origin,
85 const struct GNUNET_PeerIdentity *target)
87 fprintf (stdout, "Tunnel %s [%u]\n",
88 GNUNET_i2s_full (origin), tunnel_number);
89 fprintf (stdout, "\n");
94 * Method called to retrieve information about each tunnel the mesh peer
98 * @param peer Peer in the tunnel's tree.
99 * @param parent Parent of the current peer. All 0 when peer is root.
103 tunnel_callback (void *cls,
104 const struct GNUNET_PeerIdentity *peer,
105 const struct GNUNET_PeerIdentity *parent)
111 * Call MESH's monitor API, get all tunnels known to peer.
113 * @param cls Closure (unused).
114 * @param tc TaskContext
117 get_tunnels (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
119 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
123 GNUNET_MESH_get_tunnels (mh, &tunnels_callback, NULL);
124 if (GNUNET_YES != monitor_connections)
126 GNUNET_SCHEDULER_shutdown();
131 * Call MESH's monitor API, get info of one tunnel.
133 * @param cls Closure (unused).
134 * @param tc TaskContext
137 show_tunnel (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
139 struct GNUNET_PeerIdentity pid;
142 GNUNET_CRYPTO_hash_from_string (tunnel_id, &pid.hashPubKey))
144 GNUNET_SCHEDULER_shutdown();
147 GNUNET_MESH_show_tunnel (mh, &pid, 0, tunnel_callback, NULL);
152 * Main function that will be run by the scheduler.
155 * @param args remaining command-line arguments
156 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
157 * @param cfg configuration
160 run (void *cls, char *const *args, const char *cfgfile,
161 const struct GNUNET_CONFIGURATION_Handle *cfg)
163 static const struct GNUNET_MESH_MessageHandler handlers[] = {
164 {NULL, 0, 0} /* FIXME add option to monitor msg types */
166 /* FIXME add option to monitor apps */
170 FPRINTF (stderr, _("Invalid command line argument `%s'\n"), args[0]);
173 mh = GNUNET_MESH_connect (cfg,
175 NULL, /* new tunnel */
180 GNUNET_SCHEDULER_add_now (shutdown_task, NULL);
182 sd = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
183 shutdown_task, NULL);
185 if (NULL != tunnel_id)
186 GNUNET_SCHEDULER_add_now (&show_tunnel, NULL);
188 GNUNET_SCHEDULER_add_now (&get_tunnels, NULL);
193 * The main function to obtain peer information.
195 * @param argc number of arguments from the command line
196 * @param argv command line arguments
197 * @return 0 ok, 1 on error
200 main (int argc, char *const *argv)
203 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
204 {'m', "monitor", NULL,
205 gettext_noop ("provide information about all tunnels (continuously) NOT IMPLEMENTED"), /* FIXME */
206 GNUNET_NO, &GNUNET_GETOPT_set_one, &monitor_connections},
207 {'t', "tunnel", "OWNER_ID:TUNNEL_ID",
208 gettext_noop ("provide information about a particular tunnel"),
209 GNUNET_YES, &GNUNET_GETOPT_set_string, &tunnel_id},
210 GNUNET_GETOPT_OPTION_END
213 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
216 res = GNUNET_PROGRAM_run (argc, argv, "gnunet-mesh",
218 ("Print information about mesh tunnels and peers."),
219 options, &run, NULL);
221 GNUNET_free ((void *) argv);
223 if (GNUNET_OK == res)
229 /* end of gnunet-mesh.c */