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/>.
20 * @file core/gnunet-core.c
21 * @brief Print information about other peers known to CORE.
22 * @author Nathan Evans
25 #include "gnunet_util_lib.h"
26 #include "gnunet_core_service.h"
32 static int monitor_connections;
35 * Handle to the CORE monitor.
37 static struct GNUNET_CORE_MonitorHandle *mh;
41 * Task run in monitor mode when the user presses CTRL-C to abort.
42 * Stops monitoring activity.
47 shutdown_task (void *cls)
51 GNUNET_CORE_monitor_stop (mh);
58 * Function called to notify core users that another
59 * peer changed its state with us.
62 * @param peer the peer that changed state
63 * @param state new state of the peer
64 * @param timeout timeout for the new state
67 monitor_cb (void *cls,
68 const struct GNUNET_PeerIdentity *peer,
69 enum GNUNET_CORE_KxState state,
70 struct GNUNET_TIME_Absolute timeout)
72 struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get();
74 const char *state_str;
76 if ( ( (NULL == peer) ||
77 (GNUNET_CORE_KX_ITERATION_FINISHED == state) ) &&
78 (GNUNET_NO == monitor_connections) )
80 GNUNET_SCHEDULER_shutdown ();
86 case GNUNET_CORE_KX_STATE_DOWN:
87 /* should never happen, as we immediately send the key */
88 state_str = _("fresh connection");
90 case GNUNET_CORE_KX_STATE_KEY_SENT:
91 state_str = _("key sent");
93 case GNUNET_CORE_KX_STATE_KEY_RECEIVED:
94 state_str = _("key received");
96 case GNUNET_CORE_KX_STATE_UP:
97 state_str = _("connection established");
99 case GNUNET_CORE_KX_STATE_REKEY_SENT:
100 state_str = _("rekeying");
102 case GNUNET_CORE_KX_PEER_DISCONNECT:
103 state_str = _("disconnected");
105 case GNUNET_CORE_KX_ITERATION_FINISHED:
107 case GNUNET_CORE_KX_CORE_DISCONNECT:
110 _("Connection to CORE service lost (reconnecting)"));
113 state_str = _("unknown state");
116 now_str = GNUNET_STRINGS_absolute_time_to_string (now);
118 _("%24s: %-30s %4s (timeout in %6s)\n"),
122 GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_remaining (timeout),
128 * Main function that will be run by the scheduler.
131 * @param args remaining command-line arguments
132 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
133 * @param cfg configuration
136 run (void *cls, char *const *args, const char *cfgfile,
137 const struct GNUNET_CONFIGURATION_Handle *cfg)
142 _("Invalid command line argument `%s'\n"),
146 mh = GNUNET_CORE_monitor_start (cfg,
153 _("Failed to connect to CORE service!\n"));
156 GNUNET_SCHEDULER_add_shutdown (&shutdown_task, NULL);
161 * The main function to obtain peer information from CORE.
163 * @param argc number of arguments from the command line
164 * @param argv command line arguments
165 * @return 0 ok, 1 on error
172 struct GNUNET_GETOPT_CommandLineOption options[] = {
173 GNUNET_GETOPT_option_flag ('m',
175 gettext_noop ("provide information about all current connections (continuously)"),
176 &monitor_connections),
177 GNUNET_GETOPT_OPTION_END
180 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
182 res = GNUNET_PROGRAM_run (argc, argv, "gnunet-core",
184 ("Print information about connected peers."),
185 options, &run, NULL);
187 GNUNET_free ((void *) argv);
188 if (GNUNET_OK == res)
193 /* end of gnunet-core.c */