aboutsummaryrefslogtreecommitdiff
path: root/src/cli/core
diff options
context:
space:
mode:
Diffstat (limited to 'src/cli/core')
-rw-r--r--src/cli/core/.gitignore1
-rw-r--r--src/cli/core/Makefile.am24
-rw-r--r--src/cli/core/gnunet-core.c274
-rw-r--r--src/cli/core/meson.build6
4 files changed, 305 insertions, 0 deletions
diff --git a/src/cli/core/.gitignore b/src/cli/core/.gitignore
new file mode 100644
index 000000000..95169ed2b
--- /dev/null
+++ b/src/cli/core/.gitignore
@@ -0,0 +1 @@
gnunet-core
diff --git a/src/cli/core/Makefile.am b/src/cli/core/Makefile.am
new file mode 100644
index 000000000..97abf0db2
--- /dev/null
+++ b/src/cli/core/Makefile.am
@@ -0,0 +1,24 @@
1# This Makefile.am is in the public domain
2AM_CPPFLAGS = -I$(top_srcdir)/src/include
3
4pkgcfgdir= $(pkgdatadir)/config.d/
5
6plugindir = $(libdir)/gnunet
7
8libexecdir= $(pkglibdir)/libexec/
9
10if USE_COVERAGE
11 AM_CFLAGS = --coverage -O0
12 XLIB = -lgcov
13endif
14
15bin_PROGRAMS = \
16 gnunet-core
17
18gnunet_core_SOURCES = \
19 gnunet-core.c
20gnunet_core_LDADD = \
21 $(top_builddir)/src/service/core/libgnunetcore.la \
22 $(top_builddir)/src/lib/util/libgnunetutil.la
23gnunet_core_LDFLAGS = \
24 $(GN_LIBINTL)
diff --git a/src/cli/core/gnunet-core.c b/src/cli/core/gnunet-core.c
new file mode 100644
index 000000000..00b08eefc
--- /dev/null
+++ b/src/cli/core/gnunet-core.c
@@ -0,0 +1,274 @@
1/*
2 This file is part of GNUnet.
3 Copyright (C) 2011, 2012, 2014 GNUnet e.V.
4
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.
9
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.
14
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/>.
17
18 SPDX-License-Identifier: AGPL3.0-or-later
19 */
20
21/**
22 * @file core/gnunet-core.c
23 * @brief Print information about other peers known to CORE.
24 * @author Nathan Evans
25 */
26#include "platform.h"
27#include "gnunet_util_lib.h"
28#include "gnunet_core_service.h"
29
30
31/**
32 * Option -m.
33 */
34static int monitor_connections;
35
36/**
37 * Option -i.
38 */
39static int show_pid;
40
41/**
42 * Option -s.
43 */
44static int show_conns;
45
46/**
47 * Handle to the CORE monitor.
48 */
49static struct GNUNET_CORE_MonitorHandle *mh;
50
51
52/**
53 * Task run in monitor mode when the user presses CTRL-C to abort.
54 * Stops monitoring activity.
55 *
56 * @param cls NULL
57 */
58static void
59shutdown_task (void *cls)
60{
61 (void) cls;
62 if (NULL != mh)
63 {
64 GNUNET_CORE_monitor_stop (mh);
65 mh = NULL;
66 }
67}
68
69
70/**
71 * Function called to notify core users that another
72 * peer changed its state with us.
73 *
74 * @param cls closure
75 * @param peer the peer that changed state
76 * @param state new state of the peer
77 * @param timeout timeout for the new state
78 */
79static void
80monitor_cb (void *cls,
81 const struct GNUNET_PeerIdentity *peer,
82 enum GNUNET_CORE_KxState state,
83 struct GNUNET_TIME_Absolute timeout)
84{
85 struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get ();
86 const char *now_str;
87 const char *state_str;
88
89 (void) cls;
90 if (((NULL == peer) || (GNUNET_CORE_KX_ITERATION_FINISHED == state)) &&
91 (GNUNET_NO == monitor_connections))
92 {
93 GNUNET_SCHEDULER_shutdown ();
94 return;
95 }
96
97 switch (state)
98 {
99 case GNUNET_CORE_KX_STATE_DOWN:
100 /* should never happen, as we immediately send the key */
101 state_str = _ ("fresh connection");
102 break;
103
104 case GNUNET_CORE_KX_STATE_KEY_SENT:
105 state_str = _ ("key sent");
106 break;
107
108 case GNUNET_CORE_KX_STATE_KEY_RECEIVED:
109 state_str = _ ("key received");
110 break;
111
112 case GNUNET_CORE_KX_STATE_UP:
113 state_str = _ ("connection established");
114 break;
115
116 case GNUNET_CORE_KX_STATE_REKEY_SENT:
117 state_str = _ ("rekeying");
118 break;
119
120 case GNUNET_CORE_KX_PEER_DISCONNECT:
121 state_str = _ ("disconnected");
122 break;
123
124 case GNUNET_CORE_KX_ITERATION_FINISHED:
125 return;
126
127 case GNUNET_CORE_KX_CORE_DISCONNECT:
128 fprintf (stderr,
129 "%s\n",
130 _ ("Connection to CORE service lost (reconnecting)"));
131 return;
132
133 default:
134 state_str = _ ("unknown state");
135 break;
136 }
137 now_str = GNUNET_STRINGS_absolute_time_to_string (now);
138 fprintf (stdout,
139 _ ("%24s: %-30s %4s (timeout in %6s)\n"),
140 now_str,
141 state_str,
142 GNUNET_i2s (peer),
143 GNUNET_STRINGS_relative_time_to_string (
144 GNUNET_TIME_absolute_get_remaining (timeout),
145 GNUNET_YES));
146}
147
148
149/**
150 * Main function that will be run by the scheduler.
151 *
152 * @param cls closure
153 * @param args remaining command-line arguments
154 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
155 * @param cfg configuration
156 */
157static void
158run (void *cls,
159 char *const *args,
160 const char *cfgfile,
161 const struct GNUNET_CONFIGURATION_Handle *cfg)
162{
163 struct GNUNET_CRYPTO_EddsaPrivateKey pk;
164 struct GNUNET_CRYPTO_EddsaPublicKey pub;
165 char *keyfile;
166 (void) cls;
167 (void) cfgfile;
168 if (NULL != args[0])
169 {
170 fprintf (stderr, _ ("Invalid command line argument `%s'\n"), args[0]);
171 return;
172 }
173 if (GNUNET_OK !=
174 GNUNET_CONFIGURATION_get_value_filename (cfg,
175 "PEER",
176 "PRIVATE_KEY",
177 &keyfile))
178 {
179 GNUNET_log (
180 GNUNET_ERROR_TYPE_ERROR,
181 _ ("Core service is lacking HOSTKEY configuration setting. Exiting.\n"));
182 GNUNET_SCHEDULER_shutdown ();
183 return;
184 }
185 if (GNUNET_SYSERR ==
186 GNUNET_CRYPTO_eddsa_key_from_file (keyfile,
187 GNUNET_YES,
188 &pk))
189 {
190 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
191 "Failed to read peer's private key!\n");
192 GNUNET_SCHEDULER_shutdown ();
193 GNUNET_free (keyfile);
194 return;
195 }
196 GNUNET_CRYPTO_eddsa_key_get_public (&pk, &pub);
197 if (show_pid)
198 fprintf (stdout,
199 _ ("Current local peer identity: %s\n"),
200 GNUNET_i2s_full ((struct GNUNET_PeerIdentity*) &pub));
201 if (show_conns || monitor_connections)
202 {
203 mh = GNUNET_CORE_monitor_start (cfg, &monitor_cb, NULL);
204 if (NULL == mh)
205 {
206 fprintf (stderr, "%s", _ ("Failed to connect to CORE service!\n"));
207 GNUNET_free (keyfile);
208 return;
209 }
210 }
211 if (! show_pid && ! show_conns && ! monitor_connections)
212 {
213 fprintf (stderr, "%s", _ ("No argument given.\n"));
214 GNUNET_free (keyfile);
215 return;
216 }
217 GNUNET_free (keyfile);
218 GNUNET_SCHEDULER_add_shutdown (&shutdown_task, NULL);
219}
220
221
222/**
223 * The main function to obtain peer information from CORE.
224 *
225 * @param argc number of arguments from the command line
226 * @param argv command line arguments
227 * @return 0 ok, 1 on error
228 */
229int
230main (int argc, char *const *argv)
231{
232 int res;
233 struct GNUNET_GETOPT_CommandLineOption options[] =
234 { GNUNET_GETOPT_option_flag (
235 'm',
236 "monitor",
237 gettext_noop (
238 "provide information about all current connections (continuously)"),
239 &monitor_connections),
240 GNUNET_GETOPT_option_flag (
241 'i',
242 "show-identity",
243 gettext_noop (
244 "Show our current peer identity"
245 ),
246 &show_pid),
247 GNUNET_GETOPT_option_flag (
248 's',
249 "connection-status",
250 gettext_noop (
251 "Show current connections"
252 ),
253 &show_conns),
254 GNUNET_GETOPT_OPTION_END };
255
256 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
257 return 2;
258 res = GNUNET_PROGRAM_run (argc,
259 argv,
260 "gnunet-core",
261 gettext_noop (
262 "Print information about connected peers."),
263 options,
264 &run,
265 NULL);
266
267 GNUNET_free_nz ((void *) argv);
268 if (GNUNET_OK == res)
269 return 0;
270 return 1;
271}
272
273
274/* end of gnunet-core.c */
diff --git a/src/cli/core/meson.build b/src/cli/core/meson.build
new file mode 100644
index 000000000..db246a3c4
--- /dev/null
+++ b/src/cli/core/meson.build
@@ -0,0 +1,6 @@
1executable ('gnunet-core',
2 ['gnunet-core.c'],
3 dependencies: [libgnunetcore_dep, libgnunetutil_dep],
4 include_directories: [incdir, configuration_inc],
5 install: true,
6 install_dir: get_option('bindir'))