GNUnet  0.20.0
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 
40 
41 
48 static void
49 shutdown_task (void *cls)
50 {
51  (void) cls;
52  if (NULL != mh)
53  {
55  mh = NULL;
56  }
57 }
58 
59 
69 static void
70 monitor_cb (void *cls,
71  const struct GNUNET_PeerIdentity *peer,
74 {
76  const char *now_str;
77  const char *state_str;
78 
79  (void) cls;
80  if (((NULL == peer) || (GNUNET_CORE_KX_ITERATION_FINISHED == state)) &&
82  {
84  return;
85  }
86 
87  switch (state)
88  {
90  /* should never happen, as we immediately send the key */
91  state_str = _ ("fresh connection");
92  break;
93 
95  state_str = _ ("key sent");
96  break;
97 
99  state_str = _ ("key received");
100  break;
101 
103  state_str = _ ("connection established");
104  break;
105 
107  state_str = _ ("rekeying");
108  break;
109 
111  state_str = _ ("disconnected");
112  break;
113 
115  return;
116 
118  fprintf (stderr,
119  "%s\n",
120  _ ("Connection to CORE service lost (reconnecting)"));
121  return;
122 
123  default:
124  state_str = _ ("unknown state");
125  break;
126  }
128  fprintf (stdout,
129  _ ("%24s: %-30s %4s (timeout in %6s)\n"),
130  now_str,
131  state_str,
132  GNUNET_i2s (peer),
135  GNUNET_YES));
136 }
137 
138 
147 static void
148 run (void *cls,
149  char *const *args,
150  const char *cfgfile,
151  const struct GNUNET_CONFIGURATION_Handle *cfg)
152 {
153  (void) cls;
154  (void) cfgfile;
155  if (NULL != args[0])
156  {
157  fprintf (stderr, _ ("Invalid command line argument `%s'\n"), args[0]);
158  return;
159  }
161  if (NULL == mh)
162  {
163  fprintf (stderr, "%s", _ ("Failed to connect to CORE service!\n"));
164  return;
165  }
167 }
168 
169 
177 int
178 main (int argc, char *const *argv)
179 {
180  int res;
183  'm',
184  "monitor",
185  gettext_noop (
186  "provide information about all current connections (continuously)"),
189 
190  if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
191  return 2;
192  res = GNUNET_PROGRAM_run (argc,
193  argv,
194  "gnunet-core",
195  gettext_noop (
196  "Print information about connected peers."),
197  options,
198  &run,
199  NULL);
200 
201  GNUNET_free_nz ((void *) argv);
202  if (GNUNET_OK == res)
203  return 0;
204  return 1;
205 }
206 
207 
208 /* 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:70
static const struct GNUNET_CONFIGURATION_Handle * cfg
Configuration we are using.
Definition: gnunet-abd.c:36
static struct GNUNET_TIME_Relative timeout
Desired timeout for the lookup (default is no timeout).
Definition: gnunet-abd.c:61
static int res
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:70
static void shutdown_task(void *cls)
Task run in monitor mode when the user presses CTRL-C to abort.
Definition: gnunet-core.c:49
static struct GNUNET_CORE_MonitorHandle * mh
Handle to the CORE monitor.
Definition: gnunet-core.c:39
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:148
int main(int argc, char *const *argv)
The main function to obtain peer information from CORE.
Definition: gnunet-core.c:178
enum State state
current state of profiling
Core service; the main API for encrypted P2P communications.
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_KxState
State machine for our P2P encryption handshake.
void GNUNET_CORE_monitor_stop(struct GNUNET_CORE_MonitorHandle *mh)
Stop monitoring CORE activity.
@ 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,...
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.
@ GNUNET_OK
@ GNUNET_YES
@ GNUNET_NO
const char * GNUNET_i2s(const struct GNUNET_PeerIdentity *pid)
Convert a peer identity to a string (for printing debug messages).
#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:562
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:1334
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:1222
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
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:616
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:569
#define _(String)
GNU gettext support macro.
Definition: platform.h:178
Handle to a CORE monitoring operation.
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.
struct GNUNET_TESTBED_Peer * peer
The peer associated with this model.