2 This file is part of GNUnet.
3 (C) 2011, 2012 Christian Grothoff (and other contributing authors)
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.
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.
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.
22 * @file core/gnunet-core.c
23 * @brief Print information about other known _connected_ peers.
24 * @author Nathan Evans
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"
38 static int monitor_connections;
41 * Current number of connections in monitor mode
43 static int monitor_connections_counter;
45 static struct GNUNET_CORE_Handle *ch;
47 static struct GNUNET_PeerIdentity my_id;
50 * Task run in monitor mode when the user presses CTRL-C to abort.
51 * Stops monitoring activity.
53 * @param cls the 'struct GNUNET_TRANSPORT_PeerIterateContext *'
54 * @param tc scheduler context
57 shutdown_task (void *cls,
58 const struct GNUNET_SCHEDULER_TaskContext *tc)
62 GNUNET_CORE_disconnect (ch);
69 * Callback for retrieving a list of connected peers.
71 * @param cls closure (unused)
72 * @param peer peer identity this notification is about
75 connected_peer_callback (void *cls,
76 const struct GNUNET_PeerIdentity *peer)
80 printf (_("Peer `%s'\n"),
81 GNUNET_i2s_full (peer));
86 monitor_notify_startup (void *cls,
87 const struct GNUNET_PeerIdentity *my_identity)
89 my_id = (*my_identity);
94 * Function called to notify core users that another
95 * peer connected to us.
98 * @param peer the peer that connected
101 monitor_notify_connect (void *cls, const struct GNUNET_PeerIdentity *peer)
103 struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get();
106 if (0 != memcmp (&my_id, peer, sizeof (my_id)))
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"),
114 monitor_connections_counter);
120 * Function called to notify core users that another
121 * peer disconnected from us.
124 * @param peer the peer that disconnected
127 monitor_notify_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
129 struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get();
132 if (0 != memcmp (&my_id, peer, sizeof (my_id)))
134 now_str = GNUNET_STRINGS_absolute_time_to_string (now);
136 GNUNET_assert (monitor_connections_counter > 0);
137 monitor_connections_counter--;
138 FPRINTF (stdout, _("%24s: %-17s %4s (%u connections in total)\n"),
140 _("Disconnected from"),
142 monitor_connections_counter);
148 * Main function that will be run by the scheduler.
151 * @param args remaining command-line arguments
152 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
153 * @param cfg configuration
156 run (void *cls, char *const *args, const char *cfgfile,
157 const struct GNUNET_CONFIGURATION_Handle *cfg)
159 static const struct GNUNET_CORE_MessageHandler handlers[] = {
164 FPRINTF (stderr, _("Invalid command line argument `%s'\n"), args[0]);
167 if (GNUNET_NO == monitor_connections)
168 GNUNET_CORE_iterate_peers (cfg, &connected_peer_callback, NULL);
171 memset(&my_id, '\0', sizeof (my_id));
172 ch = GNUNET_CORE_connect (cfg, NULL,
173 monitor_notify_startup,
174 monitor_notify_connect,
175 monitor_notify_disconnect,
181 GNUNET_SCHEDULER_add_now (shutdown_task, NULL);
183 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, shutdown_task, NULL);
189 * The main function to obtain peer information.
191 * @param argc number of arguments from the command line
192 * @param argv command line arguments
193 * @return 0 ok, 1 on error
196 main (int argc, char *const *argv)
199 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
200 {'m', "monitor", NULL,
201 gettext_noop ("provide information about all current connections (continuously)"),
202 0, &GNUNET_GETOPT_set_one, &monitor_connections},
203 GNUNET_GETOPT_OPTION_END
206 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
210 res = GNUNET_PROGRAM_run (argc, argv, "gnunet-core",
212 ("Print information about connected peers."),
213 options, &run, NULL);
215 GNUNET_free ((void *) argv);
217 if (GNUNET_OK == res)
223 /* end of gnunet-core.c */