-fixing #2434, plus some code cleanup
[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  * @param atsi performance data for the connection
74  * @param atsi_count number of records in 'atsi'
75  */
76 static void
77 connected_peer_callback (void *cls, const struct GNUNET_PeerIdentity *peer,
78                          const struct GNUNET_ATS_Information *atsi,
79                          unsigned int atsi_count)
80 {
81   struct GNUNET_CRYPTO_HashAsciiEncoded enc;
82
83   if (NULL == peer)
84     return;
85   GNUNET_CRYPTO_hash_to_enc (&peer->hashPubKey, &enc);
86   printf (_("Peer `%s'\n"), (const char *) &enc);
87 }
88
89 void
90 monitor_notify_startup (void *cls,
91                        struct GNUNET_CORE_Handle * server,
92                        const struct GNUNET_PeerIdentity *
93                        my_identity)
94 {
95   my_id = (*my_identity);
96 }
97
98
99 /**
100  * Function called to notify core users that another
101  * peer connected to us.
102  *
103  * @param cls closure
104  * @param peer the peer that connected
105  * @param ats performance data
106  * @param ats_count number of entries in ats (excluding 0-termination)
107  */
108 static void
109 monitor_notify_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
110                 const struct GNUNET_ATS_Information *ats, uint32_t ats_count)
111 {
112   struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get();
113   char *now_str;
114   if (0 != memcmp (&my_id, peer, sizeof (my_id)))
115   {
116     monitor_connections_counter ++;
117     now_str = GNUNET_STRINGS_absolute_time_to_string (now);
118     FPRINTF (stdout, _("%24s: %-17s %4s   (%u connections in total)\n"),
119              now_str,
120              _("Connected to"),
121              GNUNET_i2s (peer),
122              monitor_connections_counter);
123
124     GNUNET_free (now_str);
125   }
126 }
127
128
129 /**
130  * Function called to notify core users that another
131  * peer disconnected from us.
132  *
133  * @param cls closure
134  * @param peer the peer that disconnected
135  */
136 static void
137 monitor_notify_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
138 {
139   struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get();
140   char *now_str;
141
142   if (0 != memcmp (&my_id, peer, sizeof (my_id)))
143   {
144     now_str = GNUNET_STRINGS_absolute_time_to_string (now);
145
146     GNUNET_assert (monitor_connections_counter > 0);
147     monitor_connections_counter --;
148
149     FPRINTF (stdout, _("%24s: %-17s %4s   (%u connections in total)\n"),
150              now_str,
151              _("Disconnected from"),
152              GNUNET_i2s (peer),
153              monitor_connections_counter);
154     GNUNET_free (now_str);
155   }
156 }
157
158
159
160 /**
161  * Main function that will be run by the scheduler.
162  *
163  * @param cls closure
164  * @param args remaining command-line arguments
165  * @param cfgfile name of the configuration file used (for saving, can be NULL!)
166  * @param cfg configuration
167  */
168 static void
169 run (void *cls, char *const *args, const char *cfgfile,
170      const struct GNUNET_CONFIGURATION_Handle *cfg)
171 {
172   if (args[0] != NULL)
173   {
174     FPRINTF (stderr, _("Invalid command line argument `%s'\n"), args[0]);
175     return;
176   }
177   if (GNUNET_NO == monitor_connections)
178     GNUNET_CORE_iterate_peers (cfg, &connected_peer_callback, NULL);
179   else
180   {
181     const static struct GNUNET_CORE_MessageHandler handlers[] = {
182       {NULL, 0, 0}
183     };
184
185     memset(&my_id, '\0', sizeof (my_id));
186
187     ch = GNUNET_CORE_connect (cfg, NULL,
188                               monitor_notify_startup,
189                               monitor_notify_connect,
190                               monitor_notify_disconnect,
191                               NULL, GNUNET_NO,
192                               NULL, GNUNET_NO,
193                               handlers);
194
195     if (NULL == ch)
196       GNUNET_SCHEDULER_add_now (shutdown_task, NULL);
197     else
198       GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, shutdown_task, NULL);
199   }
200 }
201
202
203 /**
204  * The main function to obtain peer information.
205  *
206  * @param argc number of arguments from the command line
207  * @param argv command line arguments
208  * @return 0 ok, 1 on error
209  */
210 int
211 main (int argc, char *const *argv)
212 {
213   int res;
214   static const struct GNUNET_GETOPT_CommandLineOption options[] = {
215     {'m', "monitor", NULL,
216      gettext_noop ("provide information about all current connections (continuously)"),
217      0, &GNUNET_GETOPT_set_one, &monitor_connections},
218     GNUNET_GETOPT_OPTION_END
219   };
220
221   if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
222     return 2;
223
224
225   res = GNUNET_PROGRAM_run (argc, argv, "gnunet-core",
226                       gettext_noop
227                       ("Print information about connected peers."),
228                       options, &run, NULL);
229
230   GNUNET_free ((void *) argv);
231
232   if (GNUNET_OK == res)
233     return 0;
234   else
235     return 1;
236 }
237
238 /* end of gnunet-core.c */