RECLAIM: refactoring; cleanup
[oweals/gnunet.git] / src / dht / gnunet-dht-monitor.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2012 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      SPDX-License-Identifier: AGPL3.0-or-later
19 */
20 /**
21  * @file dht/gnunet-dht-monitor.c
22  * @brief search for data in DHT
23  * @author Christian Grothoff
24  * @author Bartlomiej Polot
25  */
26 #include "platform.h"
27 #include "gnunet_dht_service.h"
28
29 /**
30  * The type of the query
31  */
32 static unsigned int block_type;
33
34 /**
35  * The key to be monitored
36  */
37 static char *query_key;
38
39 /**
40  * User supplied timeout value (in seconds)
41  */
42 static struct GNUNET_TIME_Relative timeout_request = { 60000 };
43
44 /**
45  * Be verbose
46  */
47 static int verbose;
48
49 /**
50 * Handle to the DHT
51  */
52 static struct GNUNET_DHT_Handle *dht_handle;
53
54 /**
55  * Global handle of the configuration
56  */
57 static const struct GNUNET_CONFIGURATION_Handle *cfg;
58
59 /**
60  * Handle for the get request
61  */
62 static struct GNUNET_DHT_MonitorHandle *monitor_handle;
63
64 /**
65  * Count of messages received
66  */
67 static unsigned int result_count;
68
69 /**
70  * Global status value
71  */
72 static int ret;
73
74 /**
75  * Task scheduled to handle timeout.
76  */
77 static struct GNUNET_SCHEDULER_Task *tt;
78
79
80 /**
81  * Stop monitoring request and start shutdown
82  *
83  * @param cls closure (unused)
84  */
85 static void
86 cleanup_task (void *cls)
87 {
88   if (verbose)
89     FPRINTF (stderr, "%s",  "Cleaning up!\n");
90   if (NULL != monitor_handle)
91   {
92     GNUNET_DHT_monitor_stop (monitor_handle);
93     monitor_handle = NULL;
94   }
95   if (NULL != dht_handle)
96   {
97     GNUNET_DHT_disconnect (dht_handle);
98     dht_handle = NULL;
99   }
100   if (NULL != tt)
101   {
102     GNUNET_SCHEDULER_cancel (tt);
103     tt = NULL;
104   }
105 }
106
107
108 /**
109  * We hit a timeout. Stop monitoring request and start shutdown
110  *
111  * @param cls closure (unused)
112  */
113 static void
114 timeout_task (void *cls)
115 {
116   tt = NULL;
117   GNUNET_SCHEDULER_shutdown ();
118 }
119
120
121
122 /**
123  * Callback called on each GET request going through the DHT.
124  *
125  * @param cls Closure.
126  * @param options Options, for instance RecordRoute, DemultiplexEverywhere.
127  * @param type The type of data in the request.
128  * @param hop_count Hop count so far.
129  * @param path_length number of entries in path (or 0 if not recorded).
130  * @param path peers on the GET path (or NULL if not recorded).
131  * @param desired_replication_level Desired replication level.
132  * @param key Key of the requested data.
133  */
134 static void
135 get_callback (void *cls,
136               enum GNUNET_DHT_RouteOption options,
137               enum GNUNET_BLOCK_Type type,
138               uint32_t hop_count,
139               uint32_t desired_replication_level,
140               unsigned int path_length,
141               const struct GNUNET_PeerIdentity *path,
142               const struct GNUNET_HashCode * key)
143 {
144   FPRINTF (stdout,
145            "GET #%u: type %d, key `%s'\n",
146            result_count,
147            (int) type,
148            GNUNET_h2s_full(key));
149   result_count++;
150 }
151
152
153 /**
154  * Callback called on each GET reply going through the DHT.
155  *
156  * @param cls Closure.
157  * @param type The type of data in the result.
158  * @param get_path Peers on GET path (or NULL if not recorded).
159  * @param get_path_length number of entries in get_path.
160  * @param put_path peers on the PUT path (or NULL if not recorded).
161  * @param put_path_length number of entries in get_path.
162  * @param exp Expiration time of the data.
163  * @param key Key of the data.
164  * @param data Pointer to the result data.
165  * @param size Number of bytes in data.
166  */
167 static void
168 get_resp_callback (void *cls,
169                    enum GNUNET_BLOCK_Type type,
170                    const struct GNUNET_PeerIdentity *get_path,
171                    unsigned int get_path_length,
172                    const struct GNUNET_PeerIdentity *put_path,
173                    unsigned int put_path_length,
174                    struct GNUNET_TIME_Absolute exp,
175                    const struct GNUNET_HashCode * key,
176                    const void *data,
177                    size_t size)
178 {
179   FPRINTF (stdout,
180            (GNUNET_BLOCK_TYPE_TEST == type)
181            ? "RESPONSE #%u (%s): type %d, key `%s', data `%.*s'\n"
182            : "RESPONSE #%u (%s): type %d, key `%s'\n",
183            result_count,
184            GNUNET_STRINGS_absolute_time_to_string (exp),
185            (int) type,
186            GNUNET_h2s_full (key),
187            (unsigned int) size,
188            (char *) data);
189   result_count++;
190 }
191
192
193 /**
194  * Callback called on each PUT request going through the DHT.
195  *
196  * @param cls Closure.
197  * @param options Options, for instance RecordRoute, DemultiplexEverywhere.
198  * @param type The type of data in the request.
199  * @param hop_count Hop count so far.
200  * @param path_length number of entries in path (or 0 if not recorded).
201  * @param path peers on the PUT path (or NULL if not recorded).
202  * @param desired_replication_level Desired replication level.
203  * @param exp Expiration time of the data.
204  * @param key Key under which data is to be stored.
205  * @param data Pointer to the data carried.
206  * @param size Number of bytes in data.
207  */
208 static void
209 put_callback (void *cls,
210               enum GNUNET_DHT_RouteOption options,
211               enum GNUNET_BLOCK_Type type,
212               uint32_t hop_count,
213               uint32_t desired_replication_level,
214               unsigned int path_length,
215               const struct GNUNET_PeerIdentity *path,
216               struct GNUNET_TIME_Absolute exp,
217               const struct GNUNET_HashCode * key,
218               const void *data,
219               size_t size)
220 {
221   FPRINTF (stdout,
222            (GNUNET_BLOCK_TYPE_TEST == type)
223            ? "PUT %u (%s): type %d, key `%s', data `%.*s'\n"
224            : "PUT %u (%s): type %d, key `%s'\n",
225            result_count,
226            GNUNET_STRINGS_absolute_time_to_string (exp),
227            (int) type,
228            GNUNET_h2s_full(key),
229            (unsigned int) size,
230            (char *) data);
231   result_count++;
232 }
233
234
235 /**
236  * Main function that will be run by the scheduler.
237  *
238  * @param cls closure
239  * @param args remaining command-line arguments
240  * @param cfgfile name of the configuration file used (for saving, can be NULL!)
241  * @param c configuration
242  */
243 static void
244 run (void *cls,
245      char *const *args,
246      const char *cfgfile,
247      const struct GNUNET_CONFIGURATION_Handle *c)
248 {
249   struct GNUNET_HashCode *key;
250   struct GNUNET_HashCode hc;
251
252   cfg = c;
253
254   if (NULL == (dht_handle = GNUNET_DHT_connect (cfg, 1)))
255   {
256     FPRINTF (stderr, "%s",
257              _("Failed to connect to DHT service!\n"));
258     ret = 1;
259     return;
260   }
261   if (GNUNET_BLOCK_TYPE_ANY == block_type)      /* Type of data not set */
262     block_type = GNUNET_BLOCK_TYPE_TEST;
263   if (NULL != query_key)
264     {
265       key = &hc;
266       if (GNUNET_OK !=
267           GNUNET_CRYPTO_hash_from_string (query_key, key))
268         GNUNET_CRYPTO_hash (query_key, strlen (query_key), key);
269     }
270   else
271     {
272       key = NULL;
273     }
274   if (verbose)
275     FPRINTF (stderr,
276              "Monitoring for %s\n",
277              GNUNET_STRINGS_relative_time_to_string (timeout_request, GNUNET_NO));
278   tt = GNUNET_SCHEDULER_add_delayed (timeout_request,
279                                      &timeout_task,
280                                      NULL);
281   GNUNET_SCHEDULER_add_shutdown (&cleanup_task,
282                                 NULL);
283   monitor_handle = GNUNET_DHT_monitor_start (dht_handle,
284                                              block_type,
285                                              key,
286                                              &get_callback,
287                                              &get_resp_callback,
288                                              &put_callback,
289                                              NULL);
290 }
291
292 /**
293  * Entry point for gnunet-dht-monitor
294  *
295  * @param argc number of arguments from the command line
296  * @param argv command line arguments
297  * @return 0 ok, 1 on error
298  */
299 int
300 main (int argc, char *const *argv)
301 {
302   struct GNUNET_GETOPT_CommandLineOption options[] = {
303
304     GNUNET_GETOPT_option_string ('k',
305                                  "key",
306                                  "KEY",
307                                  gettext_noop ("the query key"),
308                                  &query_key),
309
310     GNUNET_GETOPT_option_uint ('t',
311                                    "type",
312                                    "TYPE",
313                                    gettext_noop ("the type of data to look for"),
314                                    &block_type),
315
316     GNUNET_GETOPT_option_relative_time ('T',
317                                             "timeout",
318                                             "TIMEOUT",
319                                             gettext_noop ("how long should the monitor command run"),
320                                             &timeout_request),
321
322     GNUNET_GETOPT_option_flag ('V',
323                                   "verbose",
324                                   gettext_noop ("be verbose (print progress information)"),
325                                   &verbose),
326
327     GNUNET_GETOPT_OPTION_END
328   };
329
330
331   if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
332     return 2;
333
334   return (GNUNET_OK ==
335           GNUNET_PROGRAM_run (argc, argv, "gnunet-dht-monitor",
336                               gettext_noop
337                               ("Prints all packets that go through the DHT."),
338                               options, &run, NULL)) ? ret : 1;
339 }
340
341 /* end of gnunet-dht-monitor.c */