25127da6642a13427261ec7ec02251639ee3dd27
[oweals/gnunet.git] / src / core / gnunet-core.c
1 /*
2      This file is part of GNUnet.
3      (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., 59 Temple Place - Suite 330,
18      Boston, MA 02111-1307, 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: %-17s %4s\n"),
123            now_str,
124            state_str,
125            GNUNET_i2s (peer));
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_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
159                                 &shutdown_task, NULL);
160 }
161
162
163 /**
164  * The main function to obtain peer information from CORE.
165  *
166  * @param argc number of arguments from the command line
167  * @param argv command line arguments
168  * @return 0 ok, 1 on error
169  */
170 int
171 main (int argc,
172       char *const *argv)
173 {
174   int res;
175   static const struct GNUNET_GETOPT_CommandLineOption options[] = {
176     {'m', "monitor", NULL,
177      gettext_noop ("provide information about all current connections (continuously)"),
178      0, &GNUNET_GETOPT_set_one, &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 */