Fix broken build
[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
6      it under the terms of the GNU General Public License as published
7      by the Free Software Foundation; either version 3, or (at your
8      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      General Public License for more details.
14
15      You should have received a copy of the GNU General Public License
16      along with GNUnet; see the file COPYING.  If not, write to the
17      Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18      Boston, MA 02110-1301, USA.
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   if (NULL != mh)
52   {
53     GNUNET_CORE_monitor_stop (mh);
54     mh = NULL;
55   }
56 }
57
58
59 /**
60  * Function called to notify core users that another
61  * peer changed its state with us.
62  *
63  * @param cls closure
64  * @param peer the peer that changed state
65  * @param state new state of the peer
66  * @param timeout timeout for the new state
67  */
68 static void
69 monitor_cb (void *cls,
70             const struct GNUNET_PeerIdentity *peer,
71             enum GNUNET_CORE_KxState state,
72             struct GNUNET_TIME_Absolute timeout)
73 {
74   struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get();
75   const char *now_str;
76   const char *state_str;
77
78   if ( ( (NULL == peer) ||
79          (GNUNET_CORE_KX_ITERATION_FINISHED == state) ) &&
80        (GNUNET_NO == monitor_connections) )
81   {
82     GNUNET_SCHEDULER_shutdown ();
83     return;
84   }
85
86   switch (state)
87   {
88   case GNUNET_CORE_KX_STATE_DOWN:
89     /* should never happen, as we immediately send the key */
90     state_str = _("fresh connection");
91     break;
92   case GNUNET_CORE_KX_STATE_KEY_SENT:
93     state_str = _("key sent");
94     break;
95   case GNUNET_CORE_KX_STATE_KEY_RECEIVED:
96     state_str = _("key received");
97     break;
98   case GNUNET_CORE_KX_STATE_UP:
99     state_str = _("connection established");
100     break;
101   case GNUNET_CORE_KX_STATE_REKEY_SENT:
102     state_str = _("rekeying");
103     break;
104   case GNUNET_CORE_KX_PEER_DISCONNECT:
105     state_str = _("disconnected");
106     break;
107   case GNUNET_CORE_KX_ITERATION_FINISHED:
108     return;
109   case GNUNET_CORE_KX_CORE_DISCONNECT:
110     FPRINTF (stderr,
111              "%s\n",
112              _("Connection to CORE service lost (reconnecting)"));
113     return;
114   default:
115     state_str = _("unknown state");
116     break;
117   }
118   now_str = GNUNET_STRINGS_absolute_time_to_string (now);
119   FPRINTF (stdout,
120            _("%24s: %-30s %4s (timeout in %6s)\n"),
121            now_str,
122            state_str,
123            GNUNET_i2s (peer),
124            GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_remaining (timeout),
125                                                    GNUNET_YES));
126 }
127
128
129 /**
130  * Main function that will be run by the scheduler.
131  *
132  * @param cls closure
133  * @param args remaining command-line arguments
134  * @param cfgfile name of the configuration file used (for saving, can be NULL!)
135  * @param cfg configuration
136  */
137 static void
138 run (void *cls, char *const *args, const char *cfgfile,
139      const struct GNUNET_CONFIGURATION_Handle *cfg)
140 {
141   if (NULL != args[0])
142   {
143     FPRINTF (stderr,
144              _("Invalid command line argument `%s'\n"),
145              args[0]);
146     return;
147   }
148   mh = GNUNET_CORE_monitor_start (cfg,
149                                   &monitor_cb,
150                                   NULL);
151   if (NULL == mh)
152   {
153     FPRINTF (stderr,
154              "%s",
155              _("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,
171       char *const *argv)
172 {
173   int res;
174   struct GNUNET_GETOPT_CommandLineOption options[] = {
175     GNUNET_GETOPT_option_flag ('m',
176                                   "monitor",
177                                   gettext_noop ("provide information about all current connections (continuously)"),
178                                   &monitor_connections),
179     GNUNET_GETOPT_OPTION_END
180   };
181
182   if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
183     return 2;
184   res = GNUNET_PROGRAM_run (argc, argv, "gnunet-core",
185                             gettext_noop
186                             ("Print information about connected peers."),
187                             options, &run, NULL);
188
189   GNUNET_free ((void *) argv);
190   if (GNUNET_OK == res)
191     return 0;
192   return 1;
193 }
194
195 /* end of gnunet-core.c */