-wip
[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 -i.
38  */
39 static int get_info;
40
41 /**
42  * Option --tunnel
43  */
44 static char *tunnel_id;
45
46 /**
47  * Option --connection
48  */
49 static char *conn_id;
50
51 /**
52  * Option --channel
53  */
54 static char *channel_id;
55
56 /**
57  * Port to listen on (-p).
58  */
59 static uint32_t listen_port;
60
61 /**
62  * Peer to connect to.
63  */
64 static char *target_id;
65
66 /**
67  * Port to connect to
68  */
69 static uint32_t target_port;
70
71 /**
72  * Mesh handle.
73  */
74 static struct GNUNET_MESH_Handle *mh;
75
76 /**
77  * Shutdown task handle.
78  */
79 GNUNET_SCHEDULER_TaskIdentifier sd;
80
81 /**
82  * Task run in monitor mode when the user presses CTRL-C to abort.
83  * Stops monitoring activity.
84  *
85  * @param cls Closure (unused).
86  * @param tc scheduler context
87  */
88 static void
89 shutdown_task (void *cls,
90                const struct GNUNET_SCHEDULER_TaskContext *tc)
91 {
92   if (NULL != mh)
93   {
94     GNUNET_MESH_disconnect (mh);
95         mh = NULL;
96   }
97 }
98
99
100 /**
101  * Method called to retrieve information about each tunnel the mesh peer
102  * is aware of.
103  *
104  * @param cls Closure.
105  * @param tunnel_number Tunnel number.
106  * @param origin that started the tunnel (owner).
107  * @param target other endpoint of the tunnel
108  */
109 void /* FIXME static */
110 tunnels_callback (void *cls,
111                   uint32_t tunnel_number,
112                   const struct GNUNET_PeerIdentity *origin,
113                   const struct GNUNET_PeerIdentity *target)
114 {
115   fprintf (stdout, "Tunnel %s [%u]\n",
116            GNUNET_i2s_full (origin), tunnel_number);
117   fprintf (stdout, "\n");
118 }
119
120
121 /**
122  * Method called to retrieve information about each tunnel the mesh peer
123  * is aware of.
124  *
125  * @param cls Closure.
126  * @param peer Peer in the tunnel's tree.
127  * @param parent Parent of the current peer. All 0 when peer is root.
128  *
129  */
130 void /* FIXME static */
131 tunnel_callback (void *cls,
132                  const struct GNUNET_PeerIdentity *peer,
133                  const struct GNUNET_PeerIdentity *parent)
134 {
135 }
136
137
138 /**
139  * Call MESH's monitor API, get all tunnels known to peer.
140  *
141  * @param cls Closure (unused).
142  * @param tc TaskContext
143  */
144 static void
145 get_tunnels (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
146 {
147   if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
148   {
149     return;
150   }
151 //   GNUNET_MESH_get_tunnels (mh, &tunnels_callback, NULL);
152   if (GNUNET_YES != monitor_connections)
153   {
154     GNUNET_SCHEDULER_shutdown();
155   }
156 }
157
158
159 /**
160  * Call MESH's monitor API, get info of one tunnel.
161  *
162  * @param cls Closure (unused).
163  * @param tc TaskContext
164  */
165 static void
166 show_tunnel (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
167 {
168   struct GNUNET_PeerIdentity pid;
169
170   if (GNUNET_OK !=
171       GNUNET_CRYPTO_eddsa_public_key_from_string (tunnel_id,
172                                                      strlen (tunnel_id),
173                                                      &pid.public_key))
174   {
175     fprintf (stderr,
176              _("Invalid tunnel owner `%s'\n"),
177              tunnel_id);
178     GNUNET_SCHEDULER_shutdown();
179     return;
180   }
181 //   GNUNET_MESH_show_tunnel (mh, &pid, 0, tunnel_callback, NULL);
182 }
183
184
185 /**
186  * Main function that will be run by the scheduler.
187  *
188  * @param cls closure
189  * @param args remaining command-line arguments
190  * @param cfgfile name of the configuration file used (for saving, can be NULL!)
191  * @param cfg configuration
192  */
193 static void
194 run (void *cls, char *const *args, const char *cfgfile,
195      const struct GNUNET_CONFIGURATION_Handle *cfg)
196 {
197   static const struct GNUNET_MESH_MessageHandler handlers[] = {
198     {NULL, 0, 0} /* FIXME add option to monitor msg types */
199   };
200   /* FIXME add option to monitor apps */
201   int i;
202   for (i = 0; args[i]; i++)
203   {
204     FPRINTF (stderr, "Parameter %u `%s'\n", i, args[i]);
205   }
206
207   target_id = args[0];
208   target_port = args[0] && args[1] ? atoi(args[1]) : 0;
209   if ( (0 != get_info
210         || 0 != monitor_connections
211         || NULL != tunnel_id
212         || NULL != conn_id
213         || NULL != channel_id)
214        && target_id != NULL)
215   {
216     FPRINTF (stderr, _("You must NOT give a TARGET when using options\n"));
217     return;
218   }
219   mh = GNUNET_MESH_connect (cfg,
220                             NULL, /* cls */
221                             NULL, /* new tunnel */
222                             NULL, /* cleaner */
223                             handlers,
224                             NULL);
225   if (NULL == mh)
226     GNUNET_SCHEDULER_add_now (shutdown_task, NULL);
227   else
228     sd = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
229                                        shutdown_task, NULL);
230
231   if (NULL != tunnel_id)
232     GNUNET_SCHEDULER_add_now (&show_tunnel, NULL);
233   else
234     GNUNET_SCHEDULER_add_now (&get_tunnels, NULL);
235 }
236
237
238 /**
239  * The main function to obtain peer information.
240  *
241  * @param argc number of arguments from the command line
242  * @param argv command line arguments
243  * @return 0 ok, 1 on error
244  */
245 int
246 main (int argc, char *const *argv)
247 {
248   int res;
249   static const struct GNUNET_GETOPT_CommandLineOption options[] = {
250     {'m', "monitor", NULL,
251      gettext_noop ("provide information about all tunnels (continuously) NOT IMPLEMENTED"), /* FIXME */
252      GNUNET_NO, &GNUNET_GETOPT_set_one, &monitor_connections},
253     {'i', "info", NULL,
254      gettext_noop ("provide information about all tunnels"),
255      GNUNET_NO, &GNUNET_GETOPT_set_one, &get_info},
256     {'p', "port", NULL,
257      gettext_noop ("listen on this port"),
258      GNUNET_NO, &GNUNET_GETOPT_set_uint, &listen_port},
259     {'t', "tunnel", "TUNNEL_ID",
260      gettext_noop ("provide information about a particular tunnel"),
261      GNUNET_YES, &GNUNET_GETOPT_set_string, &tunnel_id},
262     {'n', "connection", "TUNNEL_ID:CONNECTION_ID",
263      gettext_noop ("provide information about a particular connection"),
264      GNUNET_YES, &GNUNET_GETOPT_set_string, &conn_id},
265     {'a', "channel", "TUNNEL_ID:CHANNEL_ID",
266      gettext_noop ("provide information about a particular channel"),
267      GNUNET_YES, &GNUNET_GETOPT_set_string, &channel_id},
268     GNUNET_GETOPT_OPTION_END
269   };
270
271   if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
272     return 2;
273
274   res = GNUNET_PROGRAM_run (argc, argv, "gnunet-mesh (OPTIONS | TARGET PORT)",
275                       gettext_noop
276                       ("Create channels and retreive info about meshs status."),
277                       options, &run, NULL);
278
279   GNUNET_free ((void *) argv);
280
281   if (GNUNET_OK == res)
282     return 0;
283   else
284     return 1;
285 }
286
287 /* end of gnunet-mesh.c */