82aa946445ee8f83deb4f68c1337470145cf57a5
[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_configuration_lib.h"
28 #include "gnunet_getopt_lib.h"
29 #include "gnunet_mesh_service.h"
30 #include "gnunet_program_lib.h"
31
32
33 /**
34  * Option -m.
35  */
36 static int monitor_connections;
37
38
39 /**
40  * Mesh handle.
41  */
42 static struct GNUNET_MESH_Handle *mh;
43
44
45 /**
46  * Task run in monitor mode when the user presses CTRL-C to abort.
47  * Stops monitoring activity.
48  *
49  * @param cls Closure (unused).
50  * @param tc scheduler context
51  */
52 static void
53 shutdown_task (void *cls,
54                const struct GNUNET_SCHEDULER_TaskContext *tc)
55 {
56   if (NULL != mh)
57   {
58     GNUNET_MESH_disconnect (mh);
59         mh = NULL;
60   }
61 }
62
63
64 /**
65  * Method called to retrieve information about each tunnel the mesh peer
66  * is aware of.
67  *
68  * @param cls Closure (unused).
69  * @param initiator Peer that started the tunnel (owner).
70  * @param tunnel_number Tunnel number.
71  * @param peer Array of peer identities that participate in the tunnel.
72  * @param npeers Number of peers in peers.
73  */
74 static void
75 monitor_callback (void *cls,
76                  const struct GNUNET_PeerIdentity *initiator,
77                  unsigned int tunnel_number,
78                  const struct GNUNET_PeerIdentity *peers,
79                  unsigned int npeers)
80 {
81   unsigned int i;
82
83   fprintf (stdout, "Tunnel %s [%u]: %u peers\n",
84            GNUNET_i2s_full (initiator), tunnel_number, npeers);
85   for (i = 0; i < npeers; i++)
86     fprintf (stdout, " * %s\n", GNUNET_i2s_full (&peers[i]));
87   fprintf (stdout, "\n");
88 }
89
90
91 /**
92  * Call MESH's monitor API, start monitoring process
93  *
94  * @param cls Closure (unused).
95  * @param tc TaskContext
96  */
97 static void
98 monitor (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
99 {
100   if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
101   {
102     return;
103   }
104   GNUNET_MESH_monitor (mh, &monitor_callback, NULL);
105   if (GNUNET_YES == monitor_connections)
106   {
107     /* keep open */
108   }
109 }
110
111
112 /**
113  * Main function that will be run by the scheduler.
114  *
115  * @param cls closure
116  * @param args remaining command-line arguments
117  * @param cfgfile name of the configuration file used (for saving, can be NULL!)
118  * @param cfg configuration
119  */
120 static void
121 run (void *cls, char *const *args, const char *cfgfile,
122      const struct GNUNET_CONFIGURATION_Handle *cfg)
123 {
124   static const struct GNUNET_MESH_MessageHandler handlers[] = {
125     {NULL, 0, 0} /* FIXME add option to monitor msg types */
126   };
127   GNUNET_MESH_ApplicationType apps = 0; /* FIXME add option to monitor apps */
128
129   if (args[0] != NULL)
130   {
131     FPRINTF (stderr, _("Invalid command line argument `%s'\n"), args[0]);
132     return;
133   }
134   mh = GNUNET_MESH_connect (cfg,
135                             NULL, /* cls */
136                             NULL, /* nt */
137                             NULL, /* cleaner */
138                             handlers,
139                             &apps);
140   if (NULL == mh)
141     GNUNET_SCHEDULER_add_now (shutdown_task, NULL);
142   else
143     GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
144                                   shutdown_task, NULL);
145   GNUNET_SCHEDULER_add_now (&monitor, NULL);
146 }
147
148
149 /**
150  * The main function to obtain peer information.
151  *
152  * @param argc number of arguments from the command line
153  * @param argv command line arguments
154  * @return 0 ok, 1 on error
155  */
156 int
157 main (int argc, char *const *argv)
158 {
159   int res;
160   static const struct GNUNET_GETOPT_CommandLineOption options[] = {
161     {'m', "monitor", NULL,
162     gettext_noop ("provide inthe 'struct GNUNET_TRANSPORT_PeerIterateContextformation about all tunnels (continuously)"),
163      0, &GNUNET_GETOPT_set_one, &monitor_connections},
164     GNUNET_GETOPT_OPTION_END
165   };
166
167   if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
168     return 2;
169
170
171   res = GNUNET_PROGRAM_run (argc, argv, "gnunet-mesh",
172                       gettext_noop
173                       ("Print information about mesh tunnels and peers."),
174                       options, &run, NULL);
175
176   GNUNET_free ((void *) argv);
177
178   if (GNUNET_OK == res)
179     return 0;
180   else
181     return 1;
182 }
183
184 /* end of gnunet-mesh.c */