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