- use proper channel_ack/data_ack
[oweals/gnunet.git] / src / mesh / gnunet-mesh.c
1 /*
2      This file is part of GNUnet.
3      (C) 2012 Christian Grothoff (and other contributing authors)
4
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.
9
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.
14
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.
19 */
20
21 /**
22  * @file mesh/gnunet-mesh.c
23  * @brief Print information about mesh tunnels and peers.
24  * @author Bartlomiej Polot
25  */
26 #include "platform.h"
27 #include "gnunet_util_lib.h"
28 #include "gnunet_mesh_service.h"
29
30
31 /**
32  * Option -m.
33  */
34 static int monitor_connections;
35
36 /**
37  * Option -t
38  */
39 static char *tunnel_id;
40
41 /**
42  * Mesh handle.
43  */
44 static struct GNUNET_MESH_Handle *mh;
45
46 /**
47  * Shutdown task handle.
48  */
49 GNUNET_SCHEDULER_TaskIdentifier sd;
50
51 /**
52  * Task run in monitor mode when the user presses CTRL-C to abort.
53  * Stops monitoring activity.
54  *
55  * @param cls Closure (unused).
56  * @param tc scheduler context
57  */
58 static void
59 shutdown_task (void *cls,
60                const struct GNUNET_SCHEDULER_TaskContext *tc)
61 {
62   if (NULL != mh)
63   {
64     GNUNET_MESH_disconnect (mh);
65         mh = NULL;
66   }
67 }
68
69
70 /**
71  * Method called to retrieve information about each tunnel the mesh peer
72  * is aware of.
73  *
74  * @param cls Closure.
75  * @param tunnel_number Tunnel number.
76  * @param origin that started the tunnel (owner).
77  * @param target other endpoint of the tunnel
78  */
79 static void
80 tunnels_callback (void *cls,
81                   uint32_t tunnel_number,
82                   const struct GNUNET_PeerIdentity *origin,
83                   const struct GNUNET_PeerIdentity *target)
84 {
85   fprintf (stdout, "Tunnel %s [%u]\n",
86            GNUNET_i2s_full (origin), tunnel_number);
87   fprintf (stdout, "\n");
88 }
89
90
91 /**
92  * Method called to retrieve information about each tunnel the mesh peer
93  * is aware of.
94  *
95  * @param cls Closure.
96  * @param peer Peer in the tunnel's tree.
97  * @param parent Parent of the current peer. All 0 when peer is root.
98  *
99  */
100 static void
101 tunnel_callback (void *cls,
102                  const struct GNUNET_PeerIdentity *peer,
103                  const struct GNUNET_PeerIdentity *parent)
104 {
105 }
106
107
108 /**
109  * Call MESH's monitor API, get all tunnels known to peer.
110  *
111  * @param cls Closure (unused).
112  * @param tc TaskContext
113  */
114 static void
115 get_tunnels (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
116 {
117   if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
118   {
119     return;
120   }
121   GNUNET_MESH_get_tunnels (mh, &tunnels_callback, NULL);
122   if (GNUNET_YES != monitor_connections)
123   {
124     GNUNET_SCHEDULER_shutdown();
125   }
126 }
127
128
129 /**
130  * Call MESH's monitor API, get info of one tunnel.
131  *
132  * @param cls Closure (unused).
133  * @param tc TaskContext
134  */
135 static void
136 show_tunnel (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
137 {
138   struct GNUNET_PeerIdentity pid;
139
140   if (GNUNET_OK !=
141       GNUNET_CRYPTO_eddsa_public_key_from_string (tunnel_id,
142                                                      strlen (tunnel_id),
143                                                      &pid.public_key))
144   {
145     fprintf (stderr,
146              _("Invalid tunnel owner `%s'\n"),
147              tunnel_id);
148     GNUNET_SCHEDULER_shutdown();
149     return;
150   }
151   GNUNET_MESH_show_tunnel (mh, &pid, 0, tunnel_callback, NULL);
152 }
153
154
155 /**
156  * Main function that will be run by the scheduler.
157  *
158  * @param cls closure
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
162  */
163 static void
164 run (void *cls, char *const *args, const char *cfgfile,
165      const struct GNUNET_CONFIGURATION_Handle *cfg)
166 {
167   static const struct GNUNET_MESH_MessageHandler handlers[] = {
168     {NULL, 0, 0} /* FIXME add option to monitor msg types */
169   };
170   /* FIXME add option to monitor apps */
171
172   if (args[0] != NULL)
173   {
174     FPRINTF (stderr, _("Invalid command line argument `%s'\n"), args[0]);
175     return;
176   }
177   mh = GNUNET_MESH_connect (cfg,
178                             NULL, /* cls */
179                             NULL, /* new tunnel */
180                             NULL, /* cleaner */
181                             handlers,
182                             NULL);
183   if (NULL == mh)
184     GNUNET_SCHEDULER_add_now (shutdown_task, NULL);
185   else
186     sd = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
187                                        shutdown_task, NULL);
188
189   if (NULL != tunnel_id)
190     GNUNET_SCHEDULER_add_now (&show_tunnel, NULL);
191   else
192     GNUNET_SCHEDULER_add_now (&get_tunnels, NULL);
193 }
194
195
196 /**
197  * The main function to obtain peer information.
198  *
199  * @param argc number of arguments from the command line
200  * @param argv command line arguments
201  * @return 0 ok, 1 on error
202  */
203 int
204 main (int argc, char *const *argv)
205 {
206   int res;
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
215   };
216
217   if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
218     return 2;
219
220   res = GNUNET_PROGRAM_run (argc, argv, "gnunet-mesh",
221                       gettext_noop
222                       ("Print information about mesh tunnels and peers."),
223                       options, &run, NULL);
224
225   GNUNET_free ((void *) argv);
226
227   if (GNUNET_OK == res)
228     return 0;
229   else
230     return 1;
231 }
232
233 /* end of gnunet-mesh.c */