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.
17 * @file core/gnunet-core.c
18 * @brief Print information about other peers known to CORE.
19 * @author Nathan Evans
22 #include "gnunet_util_lib.h"
23 #include "gnunet_core_service.h"
29 static int monitor_connections;
32 * Handle to the CORE monitor.
34 static struct GNUNET_CORE_MonitorHandle *mh;
38 * Task run in monitor mode when the user presses CTRL-C to abort.
39 * Stops monitoring activity.
44 shutdown_task (void *cls)
48 GNUNET_CORE_monitor_stop (mh);
55 * Function called to notify core users that another
56 * peer changed its state with us.
59 * @param peer the peer that changed state
60 * @param state new state of the peer
61 * @param timeout timeout for the new state
64 monitor_cb (void *cls,
65 const struct GNUNET_PeerIdentity *peer,
66 enum GNUNET_CORE_KxState state,
67 struct GNUNET_TIME_Absolute timeout)
69 struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get();
71 const char *state_str;
73 if ( ( (NULL == peer) ||
74 (GNUNET_CORE_KX_ITERATION_FINISHED == state) ) &&
75 (GNUNET_NO == monitor_connections) )
77 GNUNET_SCHEDULER_shutdown ();
83 case GNUNET_CORE_KX_STATE_DOWN:
84 /* should never happen, as we immediately send the key */
85 state_str = _("fresh connection");
87 case GNUNET_CORE_KX_STATE_KEY_SENT:
88 state_str = _("key sent");
90 case GNUNET_CORE_KX_STATE_KEY_RECEIVED:
91 state_str = _("key received");
93 case GNUNET_CORE_KX_STATE_UP:
94 state_str = _("connection established");
96 case GNUNET_CORE_KX_STATE_REKEY_SENT:
97 state_str = _("rekeying");
99 case GNUNET_CORE_KX_PEER_DISCONNECT:
100 state_str = _("disconnected");
102 case GNUNET_CORE_KX_ITERATION_FINISHED:
104 case GNUNET_CORE_KX_CORE_DISCONNECT:
107 _("Connection to CORE service lost (reconnecting)"));
110 state_str = _("unknown state");
113 now_str = GNUNET_STRINGS_absolute_time_to_string (now);
115 _("%24s: %-30s %4s (timeout in %6s)\n"),
119 GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_remaining (timeout),
125 * Main function that will be run by the scheduler.
128 * @param args remaining command-line arguments
129 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
130 * @param cfg configuration
133 run (void *cls, char *const *args, const char *cfgfile,
134 const struct GNUNET_CONFIGURATION_Handle *cfg)
139 _("Invalid command line argument `%s'\n"),
143 mh = GNUNET_CORE_monitor_start (cfg,
150 _("Failed to connect to CORE service!\n"));
153 GNUNET_SCHEDULER_add_shutdown (&shutdown_task, NULL);
158 * The main function to obtain peer information from CORE.
160 * @param argc number of arguments from the command line
161 * @param argv command line arguments
162 * @return 0 ok, 1 on error
169 struct GNUNET_GETOPT_CommandLineOption options[] = {
170 GNUNET_GETOPT_option_flag ('m',
172 gettext_noop ("provide information about all current connections (continuously)"),
173 &monitor_connections),
174 GNUNET_GETOPT_OPTION_END
177 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
179 res = GNUNET_PROGRAM_run (argc, argv, "gnunet-core",
181 ("Print information about connected peers."),
182 options, &run, NULL);
184 GNUNET_free ((void *) argv);
185 if (GNUNET_OK == res)
190 /* end of gnunet-core.c */