GNUnet 0.21.2
gnunet-core.c
Go to the documentation of this file.
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
26#include "platform.h"
27#include "gnunet_util_lib.h"
28#include "gnunet_core_service.h"
29
30
35
39static int show_pid;
40
44static int show_conns;
45
50
51
58static void
59shutdown_task (void *cls)
60{
61 (void) cls;
62 if (NULL != mh)
63 {
65 mh = NULL;
66 }
67}
68
69
79static void
80monitor_cb (void *cls,
81 const struct GNUNET_PeerIdentity *peer,
84{
86 const char *now_str;
87 const char *state_str;
88
89 (void) cls;
90 if (((NULL == peer) || (GNUNET_CORE_KX_ITERATION_FINISHED == state)) &&
92 {
94 return;
95 }
96
97 switch (state)
98 {
100 /* should never happen, as we immediately send the key */
101 state_str = _ ("fresh connection");
102 break;
103
105 state_str = _ ("key sent");
106 break;
107
109 state_str = _ ("key received");
110 break;
111
113 state_str = _ ("connection established");
114 break;
115
117 state_str = _ ("rekeying");
118 break;
119
121 state_str = _ ("disconnected");
122 break;
123
125 return;
126
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 }
138 fprintf (stdout,
139 _ ("%24s: %-30s %4s (timeout in %6s)\n"),
140 now_str,
141 state_str,
142 GNUNET_i2s (peer),
145 GNUNET_YES));
146}
147
148
157static void
158run (void *cls,
159 char *const *args,
160 const char *cfgfile,
161 const struct GNUNET_CONFIGURATION_Handle *cfg)
162{
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 !=
175 "PEER",
176 "PRIVATE_KEY",
177 &keyfile))
178 {
179 GNUNET_log (
181 _ ("Core service is lacking HOSTKEY configuration setting. Exiting.\n"));
183 return;
184 }
185 if (GNUNET_SYSERR ==
188 &pk))
189 {
191 "Failed to read peer's private key!\n");
193 GNUNET_free (keyfile);
194 return;
195 }
197 if (show_pid)
198 fprintf (stdout,
199 _ ("Current local peer identity: %s\n"),
202 {
204 if (NULL == mh)
205 {
206 fprintf (stderr, "%s", _ ("Failed to connect to CORE service!\n"));
207 GNUNET_free (keyfile);
208 return;
209 }
210 }
212 {
213 fprintf (stderr, "%s", _ ("No argument given.\n"));
214 GNUNET_free (keyfile);
215 return;
216 }
217 GNUNET_free (keyfile);
219}
220
221
229int
230main (int argc, char *const *argv)
231{
232 int res;
235 'm',
236 "monitor",
238 "provide information about all current connections (continuously)"),
241 'i',
242 "show-identity",
244 "Show our current peer identity"
245 ),
246 &show_pid),
248 's',
249 "connection-status",
251 "Show current connections"
252 ),
253 &show_conns),
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",
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 */
struct GNUNET_GETOPT_CommandLineOption GNUNET_GETOPT_OPTION_END
Definition: 002.c:13
struct GNUNET_GETOPT_CommandLineOption options[]
Definition: 002.c:5
#define gettext_noop(String)
Definition: gettext.h:74
static struct GNUNET_CONFIGURATION_Handle * cfg
Our configuration.
Definition: gnunet-arm.c:109
static struct GNUNET_TIME_Relative timeout
User defined timestamp for completing operations.
Definition: gnunet-arm.c:119
static int monitor_connections
Option -m.
Definition: gnunet-core.c:34
static void monitor_cb(void *cls, const struct GNUNET_PeerIdentity *peer, enum GNUNET_CORE_KxState state, struct GNUNET_TIME_Absolute timeout)
Function called to notify core users that another peer changed its state with us.
Definition: gnunet-core.c:80
static int show_conns
Option -s.
Definition: gnunet-core.c:44
static void shutdown_task(void *cls)
Task run in monitor mode when the user presses CTRL-C to abort.
Definition: gnunet-core.c:59
static struct GNUNET_CORE_MonitorHandle * mh
Handle to the CORE monitor.
Definition: gnunet-core.c:49
static void run(void *cls, char *const *args, const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg)
Main function that will be run by the scheduler.
Definition: gnunet-core.c:158
static int show_pid
Option -i.
Definition: gnunet-core.c:39
int main(int argc, char *const *argv)
The main function to obtain peer information from CORE.
Definition: gnunet-core.c:230
struct GNUNET_CRYPTO_PrivateKey pk
Private key from command line option, or NULL.
static char * res
Currently read line or NULL on EOF.
enum State state
current state of profiling
static struct GNUNET_CRYPTO_EddsaPublicKey pub
Definition: gnunet-scrypt.c:47
Core service; the main API for encrypted P2P communications.
enum GNUNET_GenericReturnValue GNUNET_CONFIGURATION_get_value_filename(const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section, const char *option, char **value)
Get a configuration value that should be the name of a file or directory.
GNUNET_CORE_KxState
State machine for our P2P encryption handshake.
void GNUNET_CORE_monitor_stop(struct GNUNET_CORE_MonitorHandle *mh)
Stop monitoring CORE activity.
struct GNUNET_CORE_MonitorHandle * GNUNET_CORE_monitor_start(const struct GNUNET_CONFIGURATION_Handle *cfg, GNUNET_CORE_MonitorCallback peer_cb, void *peer_cb_cls)
Monitor connectivity and KX status of all peers known to CORE.
@ GNUNET_CORE_KX_PEER_DISCONNECT
Last state of a KX (when it is being terminated).
@ GNUNET_CORE_KX_STATE_UP
The other peer has confirmed our session key + PING with a PONG message encrypted with their session ...
@ GNUNET_CORE_KX_STATE_DOWN
No handshake yet.
@ GNUNET_CORE_KX_CORE_DISCONNECT
This is not a state in a peer's state machine, but a special value used with the GNUNET_CORE_MonitorC...
@ GNUNET_CORE_KX_STATE_KEY_SENT
We've sent our session key.
@ GNUNET_CORE_KX_STATE_KEY_RECEIVED
We've received the other peers session key.
@ GNUNET_CORE_KX_ITERATION_FINISHED
This is not a state in a peer's state machine, but a special value used with the GNUNET_CORE_MonitorC...
@ GNUNET_CORE_KX_STATE_REKEY_SENT
We're rekeying (or had a timeout), so we have sent the other peer our new ephemeral key,...
enum GNUNET_GenericReturnValue GNUNET_CRYPTO_eddsa_key_from_file(const char *filename, int do_create, struct GNUNET_CRYPTO_EddsaPrivateKey *pkey)
Create a new private key by reading it from a file.
void GNUNET_CRYPTO_eddsa_key_get_public(const struct GNUNET_CRYPTO_EddsaPrivateKey *priv, struct GNUNET_CRYPTO_EddsaPublicKey *pub)
Extract the public key for the given private key.
Definition: crypto_ecc.c:201
struct GNUNET_GETOPT_CommandLineOption GNUNET_GETOPT_option_flag(char shortName, const char *name, const char *description, int *val)
Allow user to specify a flag (which internally means setting an integer to 1/GNUNET_YES/GNUNET_OK.
#define GNUNET_log(kind,...)
@ GNUNET_OK
@ GNUNET_YES
@ GNUNET_NO
@ GNUNET_SYSERR
const char * GNUNET_i2s(const struct GNUNET_PeerIdentity *pid)
Convert a peer identity to a string (for printing debug messages).
const char * GNUNET_i2s_full(const struct GNUNET_PeerIdentity *pid)
Convert a peer identity to a string (for printing debug messages).
@ GNUNET_ERROR_TYPE_ERROR
#define GNUNET_free(ptr)
Wrapper around free.
#define GNUNET_free_nz(ptr)
Wrapper around free.
enum GNUNET_GenericReturnValue GNUNET_PROGRAM_run(int argc, char *const *argv, const char *binaryName, const char *binaryHelp, const struct GNUNET_GETOPT_CommandLineOption *options, GNUNET_PROGRAM_Main task, void *task_cls)
Run a standard GNUnet command startup sequence (initialize loggers and configuration,...
Definition: program.c:400
void GNUNET_SCHEDULER_shutdown(void)
Request the shutdown of a scheduler.
Definition: scheduler.c:567
struct GNUNET_SCHEDULER_Task * GNUNET_SCHEDULER_add_shutdown(GNUNET_SCHEDULER_TaskCallback task, void *task_cls)
Schedule a new task to be run on shutdown, that is when a CTRL-C signal is received,...
Definition: scheduler.c:1340
enum GNUNET_GenericReturnValue GNUNET_STRINGS_get_utf8_args(int argc, char *const *argv, int *u8argc, char *const **u8argv)
Returns utf-8 encoded arguments.
Definition: strings.c:1230
struct GNUNET_TIME_Relative GNUNET_TIME_absolute_get_remaining(struct GNUNET_TIME_Absolute future)
Given a timestamp in the future, how much time remains until then?
Definition: time.c:405
const char * GNUNET_STRINGS_relative_time_to_string(struct GNUNET_TIME_Relative delta, int do_round)
Give relative time in human-readable fancy format.
Definition: strings.c:570
struct GNUNET_TIME_Absolute GNUNET_TIME_absolute_get(void)
Get the current time.
Definition: time.c:111
const char * GNUNET_STRINGS_absolute_time_to_string(struct GNUNET_TIME_Absolute t)
Like asctime, except for GNUnet time.
Definition: strings.c:617
#define _(String)
GNU gettext support macro.
Definition: platform.h:178
Handle to a CORE monitoring operation.
Private ECC key encoded for transmission.
Public ECC key (always for curve Ed25519) encoded in a format suitable for network transmission and E...
Definition of a command line option.
The identity of the host (wraps the signing key of the peer).
Time for absolute times used by GNUnet, in microseconds.