674da705a47c1dc4a8ecfdf9682eed582a2a3088
[oweals/gnunet.git] / src / core / gnunet-core.c
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  */
34 static int monitor_connections;
35
36 /**
37  * Handle to the CORE monitor.
38  */
39 static struct GNUNET_CORE_MonitorHandle *mh;
40
41
42 /**
43  * Task run in monitor mode when the user presses CTRL-C to abort.
44  * Stops monitoring activity.
45  *
46  * @param cls NULL
47  */
48 static void
49 shutdown_task (void *cls)
50 {
51   (void) cls;
52   if (NULL != mh)
53   {
54     GNUNET_CORE_monitor_stop (mh);
55     mh = NULL;
56   }
57 }
58
59
60 /**
61  * Function called to notify core users that another
62  * peer changed its state with us.
63  *
64  * @param cls closure
65  * @param peer the peer that changed state
66  * @param state new state of the peer
67  * @param timeout timeout for the new state
68  */
69 static void
70 monitor_cb (void *cls,
71             const struct GNUNET_PeerIdentity *peer,
72             enum GNUNET_CORE_KxState state,
73             struct GNUNET_TIME_Absolute timeout)
74 {
75   struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get ();
76   const char *now_str;
77   const char *state_str;
78
79   (void) cls;
80   if (((NULL == peer) || (GNUNET_CORE_KX_ITERATION_FINISHED == state)) &&
81       (GNUNET_NO == monitor_connections))
82   {
83     GNUNET_SCHEDULER_shutdown ();
84     return;
85   }
86
87   switch (state)
88   {
89   case GNUNET_CORE_KX_STATE_DOWN:
90     /* should never happen, as we immediately send the key */
91     state_str = _ ("fresh connection");
92     break;
93   case GNUNET_CORE_KX_STATE_KEY_SENT:
94     state_str = _ ("key sent");
95     break;
96   case GNUNET_CORE_KX_STATE_KEY_RECEIVED:
97     state_str = _ ("key received");
98     break;
99   case GNUNET_CORE_KX_STATE_UP:
100     state_str = _ ("connection established");
101     break;
102   case GNUNET_CORE_KX_STATE_REKEY_SENT:
103     state_str = _ ("rekeying");
104     break;
105   case GNUNET_CORE_KX_PEER_DISCONNECT:
106     state_str = _ ("disconnected");
107     break;
108   case GNUNET_CORE_KX_ITERATION_FINISHED:
109     return;
110   case GNUNET_CORE_KX_CORE_DISCONNECT:
111     FPRINTF (stderr,
112              "%s\n",
113              _ ("Connection to CORE service lost (reconnecting)"));
114     return;
115   default:
116     state_str = _ ("unknown state");
117     break;
118   }
119   now_str = GNUNET_STRINGS_absolute_time_to_string (now);
120   FPRINTF (stdout,
121            _ ("%24s: %-30s %4s (timeout in %6s)\n"),
122            now_str,
123            state_str,
124            GNUNET_i2s (peer),
125            GNUNET_STRINGS_relative_time_to_string (
126              GNUNET_TIME_absolute_get_remaining (timeout),
127              GNUNET_YES));
128 }
129
130
131 /**
132  * Main function that will be run by the scheduler.
133  *
134  * @param cls closure
135  * @param args remaining command-line arguments
136  * @param cfgfile name of the configuration file used (for saving, can be NULL!)
137  * @param cfg configuration
138  */
139 static void
140 run (void *cls,
141      char *const *args,
142      const char *cfgfile,
143      const struct GNUNET_CONFIGURATION_Handle *cfg)
144 {
145   (void) cls;
146   (void) cfgfile;
147   if (NULL != args[0])
148   {
149     FPRINTF (stderr, _ ("Invalid command line argument `%s'\n"), args[0]);
150     return;
151   }
152   mh = GNUNET_CORE_monitor_start (cfg, &monitor_cb, NULL);
153   if (NULL == mh)
154   {
155     FPRINTF (stderr, "%s", _ ("Failed to connect to CORE service!\n"));
156     return;
157   }
158   GNUNET_SCHEDULER_add_shutdown (&shutdown_task, NULL);
159 }
160
161
162 /**
163  * The main function to obtain peer information from CORE.
164  *
165  * @param argc number of arguments from the command line
166  * @param argv command line arguments
167  * @return 0 ok, 1 on error
168  */
169 int
170 main (int argc, char *const *argv)
171 {
172   int res;
173   struct GNUNET_GETOPT_CommandLineOption options[] =
174     {GNUNET_GETOPT_option_flag (
175        'm',
176        "monitor",
177        gettext_noop (
178          "provide information about all current connections (continuously)"),
179        &monitor_connections),
180      GNUNET_GETOPT_OPTION_END};
181
182   if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
183     return 2;
184   res = GNUNET_PROGRAM_run (argc,
185                             argv,
186                             "gnunet-core",
187                             gettext_noop (
188                               "Print information about connected peers."),
189                             options,
190                             &run,
191                             NULL);
192
193   GNUNET_free ((void *) argv);
194   if (GNUNET_OK == res)
195     return 0;
196   return 1;
197 }
198
199 /* end of gnunet-core.c */