2 This file is part of GNUnet.
3 (C) 2011, 2012, 2014 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 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.
47 * @param tc scheduler context
50 shutdown_task (void *cls,
51 const struct GNUNET_SCHEDULER_TaskContext *tc)
55 GNUNET_CORE_monitor_stop (mh);
62 * Function called to notify core users that another
63 * peer changed its state with us.
66 * @param peer the peer that changed state
67 * @param state new state of the peer
68 * @param timeout timeout for the new state
71 monitor_cb (void *cls,
72 const struct GNUNET_PeerIdentity *peer,
73 enum GNUNET_CORE_KxState state,
74 struct GNUNET_TIME_Absolute timeout)
76 struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get();
78 const char *state_str;
80 if ( ( (NULL == peer) ||
81 (GNUNET_CORE_KX_ITERATION_FINISHED == state) ) &&
82 (GNUNET_NO == monitor_connections) )
84 GNUNET_SCHEDULER_shutdown ();
90 case GNUNET_CORE_KX_STATE_DOWN:
91 /* should never happen, as we immediately send the key */
92 state_str = _("fresh connection");
94 case GNUNET_CORE_KX_STATE_KEY_SENT:
95 state_str = _("key sent");
97 case GNUNET_CORE_KX_STATE_KEY_RECEIVED:
98 state_str = _("key received");
100 case GNUNET_CORE_KX_STATE_UP:
101 state_str = _("connection established");
103 case GNUNET_CORE_KX_STATE_REKEY_SENT:
104 state_str = _("rekeying");
106 case GNUNET_CORE_KX_PEER_DISCONNECT:
107 state_str = _("disconnected");
109 case GNUNET_CORE_KX_ITERATION_FINISHED:
111 case GNUNET_CORE_KX_CORE_DISCONNECT:
114 _("Connection to CORE service lost (reconnecting)"));
117 state_str = _("unknown state");
120 now_str = GNUNET_STRINGS_absolute_time_to_string (now);
122 _("%24s: %-30s %4s (timeout in %6s)\n"),
126 GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_remaining (timeout),
132 * Main function that will be run by the scheduler.
135 * @param args remaining command-line arguments
136 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
137 * @param cfg configuration
140 run (void *cls, char *const *args, const char *cfgfile,
141 const struct GNUNET_CONFIGURATION_Handle *cfg)
146 _("Invalid command line argument `%s'\n"),
150 mh = GNUNET_CORE_monitor_start (cfg,
157 _("Failed to connect to CORE service!\n"));
160 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
161 &shutdown_task, NULL);
166 * The main function to obtain peer information from CORE.
168 * @param argc number of arguments from the command line
169 * @param argv command line arguments
170 * @return 0 ok, 1 on error
177 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
178 {'m', "monitor", NULL,
179 gettext_noop ("provide information about all current connections (continuously)"),
180 0, &GNUNET_GETOPT_set_one, &monitor_connections},
181 GNUNET_GETOPT_OPTION_END
184 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
186 res = GNUNET_PROGRAM_run (argc, argv, "gnunet-core",
188 ("Print information about connected peers."),
189 options, &run, NULL);
191 GNUNET_free ((void *) argv);
192 if (GNUNET_OK == res)
197 /* end of gnunet-core.c */