97895b5a3987cb9adebed8ddb29b1b9020b22eb8
[oweals/gnunet.git] / src / core / gnunet-core.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2011, 2012, 2014 Christian Grothoff (and other contributing authors)
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  * @param tc scheduler context
48  */
49 static void
50 shutdown_task (void *cls,
51                const struct GNUNET_SCHEDULER_TaskContext *tc)
52 {
53   if (NULL != mh)
54   {
55     GNUNET_CORE_monitor_stop (mh);
56     mh = NULL;
57   }
58 }
59
60
61 /**
62  * Function called to notify core users that another
63  * peer changed its state with us.
64  *
65  * @param cls closure
66  * @param peer the peer that changed state
67  * @param state new state of the peer
68  * @param timeout timeout for the new state
69  */
70 static void
71 monitor_cb (void *cls,
72             const struct GNUNET_PeerIdentity *peer,
73             enum GNUNET_CORE_KxState state,
74             struct GNUNET_TIME_Absolute timeout)
75 {
76   struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get();
77   const char *now_str;
78   const char *state_str;
79
80   if ( ( (NULL == peer) ||
81          (GNUNET_CORE_KX_ITERATION_FINISHED == state) ) &&
82        (GNUNET_NO == monitor_connections) )
83   {
84     GNUNET_SCHEDULER_shutdown ();
85     return;
86   }
87
88   switch (state)
89   {
90   case GNUNET_CORE_KX_STATE_DOWN:
91     /* should never happen, as we immediately send the key */
92     state_str = _("fresh connection");
93     break;
94   case GNUNET_CORE_KX_STATE_KEY_SENT:
95     state_str = _("key sent");
96     break;
97   case GNUNET_CORE_KX_STATE_KEY_RECEIVED:
98     state_str = _("key received");
99     break;
100   case GNUNET_CORE_KX_STATE_UP:
101     state_str = _("connection established");
102     break;
103   case GNUNET_CORE_KX_STATE_REKEY_SENT:
104     state_str = _("rekeying");
105     break;
106   case GNUNET_CORE_KX_PEER_DISCONNECT:
107     state_str = _("disconnected");
108     break;
109   case GNUNET_CORE_KX_ITERATION_FINISHED:
110     return;
111   case GNUNET_CORE_KX_CORE_DISCONNECT:
112     FPRINTF (stderr,
113              "%s\n",
114              _("Connection to CORE service lost (reconnecting)"));
115     return;
116   default:
117     state_str = _("unknown state");
118     break;
119   }
120   now_str = GNUNET_STRINGS_absolute_time_to_string (now);
121   FPRINTF (stdout,
122            _("%24s: %-30s %4s (timeout in %6s)\n"),
123            now_str,
124            state_str,
125            GNUNET_i2s (peer),
126            GNUNET_STRINGS_relative_time_to_string (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, char *const *args, const char *cfgfile,
141      const struct GNUNET_CONFIGURATION_Handle *cfg)
142 {
143   if (NULL != args[0])
144   {
145     FPRINTF (stderr,
146              _("Invalid command line argument `%s'\n"),
147              args[0]);
148     return;
149   }
150   mh = GNUNET_CORE_monitor_start (cfg,
151                                   &monitor_cb,
152                                   NULL);
153   if (NULL == mh)
154   {
155     FPRINTF (stderr,
156              "%s",
157              _("Failed to connect to CORE service!\n"));
158     return;
159   }
160   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
161                                 &shutdown_task, NULL);
162 }
163
164
165 /**
166  * The main function to obtain peer information from CORE.
167  *
168  * @param argc number of arguments from the command line
169  * @param argv command line arguments
170  * @return 0 ok, 1 on error
171  */
172 int
173 main (int argc,
174       char *const *argv)
175 {
176   int res;
177   static const struct GNUNET_GETOPT_CommandLineOption options[] = {
178     {'m', "monitor", NULL,
179      gettext_noop ("provide information about all current connections (continuously)"),
180      0, &GNUNET_GETOPT_set_one, &monitor_connections},
181     GNUNET_GETOPT_OPTION_END
182   };
183
184   if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
185     return 2;
186   res = GNUNET_PROGRAM_run (argc, argv, "gnunet-core",
187                             gettext_noop
188                             ("Print information about connected peers."),
189                             options, &run, NULL);
190
191   GNUNET_free ((void *) argv);
192   if (GNUNET_OK == res)
193     return 0;
194   return 1;
195 }
196
197 /* end of gnunet-core.c */