2 This file is part of GNUnet.
3 Copyright (C) 2011, 2012, 2014 GNUnet e.V.
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your 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 Affero General Public License for more details.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
22 * @file core/gnunet-core.c
23 * @brief Print information about other peers known to CORE.
24 * @author Nathan Evans
27 #include "gnunet_util_lib.h"
28 #include "gnunet_core_service.h"
34 static int monitor_connections;
37 * Handle to the CORE monitor.
39 static struct GNUNET_CORE_MonitorHandle *mh;
43 * Task run in monitor mode when the user presses CTRL-C to abort.
44 * Stops monitoring activity.
49 shutdown_task (void *cls)
54 GNUNET_CORE_monitor_stop (mh);
61 * Function called to notify core users that another
62 * peer changed its state with us.
65 * @param peer the peer that changed state
66 * @param state new state of the peer
67 * @param timeout timeout for the new state
70 monitor_cb (void *cls,
71 const struct GNUNET_PeerIdentity *peer,
72 enum GNUNET_CORE_KxState state,
73 struct GNUNET_TIME_Absolute timeout)
75 struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get ();
77 const char *state_str;
80 if (((NULL == peer) || (GNUNET_CORE_KX_ITERATION_FINISHED == state)) &&
81 (GNUNET_NO == monitor_connections))
83 GNUNET_SCHEDULER_shutdown ();
89 case GNUNET_CORE_KX_STATE_DOWN:
90 /* should never happen, as we immediately send the key */
91 state_str = _ ("fresh connection");
94 case GNUNET_CORE_KX_STATE_KEY_SENT:
95 state_str = _ ("key sent");
98 case GNUNET_CORE_KX_STATE_KEY_RECEIVED:
99 state_str = _ ("key received");
102 case GNUNET_CORE_KX_STATE_UP:
103 state_str = _ ("connection established");
106 case GNUNET_CORE_KX_STATE_REKEY_SENT:
107 state_str = _ ("rekeying");
110 case GNUNET_CORE_KX_PEER_DISCONNECT:
111 state_str = _ ("disconnected");
114 case GNUNET_CORE_KX_ITERATION_FINISHED:
117 case GNUNET_CORE_KX_CORE_DISCONNECT:
120 _ ("Connection to CORE service lost (reconnecting)"));
124 state_str = _ ("unknown state");
127 now_str = GNUNET_STRINGS_absolute_time_to_string (now);
129 _ ("%24s: %-30s %4s (timeout in %6s)\n"),
133 GNUNET_STRINGS_relative_time_to_string (
134 GNUNET_TIME_absolute_get_remaining (timeout),
140 * Main function that will be run by the scheduler.
143 * @param args remaining command-line arguments
144 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
145 * @param cfg configuration
151 const struct GNUNET_CONFIGURATION_Handle *cfg)
157 fprintf (stderr, _ ("Invalid command line argument `%s'\n"), args[0]);
160 mh = GNUNET_CORE_monitor_start (cfg, &monitor_cb, NULL);
163 fprintf (stderr, "%s", _ ("Failed to connect to CORE service!\n"));
166 GNUNET_SCHEDULER_add_shutdown (&shutdown_task, NULL);
171 * The main function to obtain peer information from CORE.
173 * @param argc number of arguments from the command line
174 * @param argv command line arguments
175 * @return 0 ok, 1 on error
178 main (int argc, char *const *argv)
181 struct GNUNET_GETOPT_CommandLineOption options[] =
182 { GNUNET_GETOPT_option_flag (
186 "provide information about all current connections (continuously)"),
187 &monitor_connections),
188 GNUNET_GETOPT_OPTION_END };
190 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
192 res = GNUNET_PROGRAM_run (argc,
196 "Print information about connected peers."),
201 GNUNET_free ((void *) argv);
202 if (GNUNET_OK == res)
208 /* end of gnunet-core.c */