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: %-17s %4s\n"),
130 * Main function that will be run by the scheduler.
133 * @param args remaining command-line arguments
134 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
135 * @param cfg configuration
138 run (void *cls, char *const *args, const char *cfgfile,
139 const struct GNUNET_CONFIGURATION_Handle *cfg)
144 _("Invalid command line argument `%s'\n"),
148 mh = GNUNET_CORE_monitor_start (cfg,
155 _("Failed to connect to CORE service!\n"));
158 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
159 &shutdown_task, NULL);
164 * The main function to obtain peer information from CORE.
166 * @param argc number of arguments from the command line
167 * @param argv command line arguments
168 * @return 0 ok, 1 on error
175 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
176 {'m', "monitor", NULL,
177 gettext_noop ("provide information about all current connections (continuously)"),
178 0, &GNUNET_GETOPT_set_one, &monitor_connections},
179 GNUNET_GETOPT_OPTION_END
182 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
184 res = GNUNET_PROGRAM_run (argc, argv, "gnunet-core",
186 ("Print information about connected peers."),
187 options, &run, NULL);
189 GNUNET_free ((void *) argv);
190 if (GNUNET_OK == res)
195 /* end of gnunet-core.c */