more information why nat tests fail
[oweals/gnunet.git] / src / core / gnunet-core.c
1 /*
2      This file is part of GNUnet.
3      (C) 2011, 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 core/gnunet-core.c
23  * @brief Print information about other known _connected_ peers.
24  * @author Nathan Evans
25  */
26 #include "platform.h"
27 #include "gnunet_crypto_lib.h"
28 #include "gnunet_configuration_lib.h"
29 #include "gnunet_getopt_lib.h"
30 #include "gnunet_peerinfo_service.h"
31 #include "gnunet_transport_service.h"
32 #include "gnunet_core_service.h"
33 #include "gnunet_program_lib.h"
34
35 /**
36  * Option -m.
37  */
38 static int monitor_connections;
39
40 /**
41  * Current number of connections in monitor mode
42  */
43 static int monitor_connections_counter;
44
45 static struct GNUNET_CORE_Handle *ch;
46
47 static struct GNUNET_PeerIdentity my_id;
48
49 /**
50  * Task run in monitor mode when the user presses CTRL-C to abort.
51  * Stops monitoring activity.
52  *
53  * @param cls the 'struct GNUNET_TRANSPORT_PeerIterateContext *'
54  * @param tc scheduler context
55  */
56 static void
57 shutdown_task (void *cls,
58                const struct GNUNET_SCHEDULER_TaskContext *tc)
59 {
60   if (NULL != ch)
61   {
62     GNUNET_CORE_disconnect (ch);
63     ch = NULL;
64   }
65 }
66
67
68 /**
69  * Callback for retrieving a list of connected peers.
70  *
71  * @param cls closure (unused)
72  * @param peer peer identity this notification is about
73  */
74 static void
75 connected_peer_callback (void *cls,
76                          const struct GNUNET_PeerIdentity *peer)
77 {
78   if (NULL == peer)
79     return;
80   printf (_("Peer `%s'\n"),
81           GNUNET_i2s_full (peer));
82 }
83
84
85 static void
86 monitor_notify_startup (void *cls,
87                         const struct GNUNET_PeerIdentity *my_identity)
88 {
89   my_id = (*my_identity);
90 }
91
92
93 /**
94  * Function called to notify core users that another
95  * peer connected to us.
96  *
97  * @param cls closure
98  * @param peer the peer that connected
99  */
100 static void
101 monitor_notify_connect (void *cls, const struct GNUNET_PeerIdentity *peer)
102 {
103   struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get();
104   const char *now_str;
105
106   if (0 != memcmp (&my_id, peer, sizeof (my_id)))
107   {
108     monitor_connections_counter ++;
109     now_str = GNUNET_STRINGS_absolute_time_to_string (now);
110     FPRINTF (stdout, _("%24s: %-17s %4s   (%u connections in total)\n"),
111              now_str,
112              _("Connected to"),
113              GNUNET_i2s (peer),
114              monitor_connections_counter);
115   }
116 }
117
118
119 /**
120  * Function called to notify core users that another
121  * peer disconnected from us.
122  *
123  * @param cls closure
124  * @param peer the peer that disconnected
125  */
126 static void
127 monitor_notify_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
128 {
129   struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get();
130   const char *now_str;
131
132   if (0 != memcmp (&my_id, peer, sizeof (my_id)))
133   {
134     now_str = GNUNET_STRINGS_absolute_time_to_string (now);
135
136     GNUNET_assert (monitor_connections_counter > 0);
137     monitor_connections_counter--;
138     FPRINTF (stdout, _("%24s: %-17s %4s   (%u connections in total)\n"),
139              now_str,
140              _("Disconnected from"),
141              GNUNET_i2s (peer),
142              monitor_connections_counter);
143   }
144 }
145
146 /**
147  * Function called with the result of the check if the 'transport'
148  * service is running.
149  *
150  * @param cls closure with our configuration
151  * @param result #GNUNET_YES if transport is running
152  */
153 static void
154 testservice_task (void *cls, int result)
155 {
156   const struct GNUNET_CONFIGURATION_Handle *cfg = cls;
157   static const struct GNUNET_CORE_MessageHandler handlers[] = {
158     {NULL, 0, 0}
159   };
160
161   if (result != GNUNET_OK)
162     {
163       FPRINTF (stderr, _("Service `%s' is not running\n"), "core");
164       return;
165     }
166
167   if (GNUNET_NO == monitor_connections)
168   {
169     if (GNUNET_OK != GNUNET_CORE_iterate_peers (cfg, &connected_peer_callback, NULL))
170     {
171       fprintf (stderr, ("Failed to connect to CORE service to iterate peers!\n"));
172       return;
173     }
174   }
175   else
176   {
177     memset(&my_id, '\0', sizeof (my_id));
178     ch = GNUNET_CORE_connect (cfg, NULL,
179                               monitor_notify_startup,
180                               monitor_notify_connect,
181                               monitor_notify_disconnect,
182                               NULL, GNUNET_NO,
183                               NULL, GNUNET_NO,
184                               handlers);
185
186     if (NULL == ch)
187     {
188       GNUNET_SCHEDULER_add_now (shutdown_task, NULL);
189       fprintf (stderr, ("Failed to connect to CORE service!\n"));
190       return;
191     }
192     else
193       GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, shutdown_task, NULL);
194   }
195 }
196
197 /**
198  * Main function that will be run by the scheduler.
199  *
200  * @param cls closure
201  * @param args remaining command-line arguments
202  * @param cfgfile name of the configuration file used (for saving, can be NULL!)
203  * @param cfg configuration
204  */
205 static void
206 run (void *cls, char *const *args, const char *cfgfile,
207      const struct GNUNET_CONFIGURATION_Handle *cfg)
208 {
209   if (args[0] != NULL)
210   {
211     FPRINTF (stderr, _("Invalid command line argument `%s'\n"), args[0]);
212     return;
213   }
214
215   GNUNET_CLIENT_service_test ("core", cfg, GNUNET_TIME_UNIT_SECONDS,
216       &testservice_task, (void *) cfg);
217 }
218
219
220 /**
221  * The main function to obtain peer information.
222  *
223  * @param argc number of arguments from the command line
224  * @param argv command line arguments
225  * @return 0 ok, 1 on error
226  */
227 int
228 main (int argc, char *const *argv)
229 {
230   int res;
231   static const struct GNUNET_GETOPT_CommandLineOption options[] = {
232     {'m', "monitor", NULL,
233      gettext_noop ("provide information about all current connections (continuously)"),
234      0, &GNUNET_GETOPT_set_one, &monitor_connections},
235     GNUNET_GETOPT_OPTION_END
236   };
237
238   if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
239     return 2;
240
241
242   res = GNUNET_PROGRAM_run (argc, argv, "gnunet-core",
243                       gettext_noop
244                       ("Print information about connected peers."),
245                       options, &run, NULL);
246
247   GNUNET_free ((void *) argv);
248
249   if (GNUNET_OK == res)
250     return 0;
251   else
252     return 1;
253 }
254
255 /* end of gnunet-core.c */