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