-fixing #2397
[oweals/gnunet.git] / src / dht / gnunet-dht-monitor.c
1 /*
2      This file is part of GNUnet.
3      (C) 2012 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  * @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 unsigned long long timeout_request = 5;
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 /**
76  * Function called on shutdown, disconnects from DHT if necessary.
77  *
78  * @param cls closure (unused)
79  * @param tc Task Context
80  */
81 static void
82 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
83 {
84   if (verbose)
85     FPRINTF (stderr, "%s",  "Shutting down!\n");
86   if (dht_handle != NULL)
87   {
88     GNUNET_DHT_disconnect (dht_handle);
89     dht_handle = NULL;
90   }
91 }
92
93
94 /**
95  * Stop monitoring request and start shutdown
96  *
97  * @param cls closure (unused)
98  * @param tc Task Context
99  */
100 static void
101 cleanup_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
102 {
103   if (verbose)
104     FPRINTF (stderr, "%s",  "Cleaning up!\n");
105   if (monitor_handle != NULL)
106   {
107     GNUNET_DHT_monitor_stop (monitor_handle);
108     monitor_handle = NULL;
109   }
110   GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
111 }
112
113
114 /**
115  * Callback called on each GET request going through the DHT.
116  *
117  * @param cls Closure.
118  * @param options Options, for instance RecordRoute, DemultiplexEverywhere.
119  * @param type The type of data in the request.
120  * @param hop_count Hop count so far.
121  * @param path_length number of entries in path (or 0 if not recorded).
122  * @param path peers on the GET path (or NULL if not recorded).
123  * @param desired_replication_level Desired replication level.
124  * @param key Key of the requested data.
125  */
126 void
127 get_callback (void *cls,
128               enum GNUNET_DHT_RouteOption options,
129               enum GNUNET_BLOCK_Type type,
130               uint32_t hop_count,
131               uint32_t desired_replication_level,
132               unsigned int path_length,
133               const struct GNUNET_PeerIdentity *path,
134               const struct GNUNET_HashCode * key)
135 {
136   FPRINTF (stdout, "Result %d, operation: %s, type %d\n Key: %s",
137            result_count,
138            "GET",
139            type,
140            GNUNET_h2s_full(key));
141   result_count++;
142 }
143
144 /**
145  * Callback called on each GET reply going through the DHT.
146  *
147  * @param cls Closure.
148  * @param type The type of data in the result.
149  * @param get_path Peers on GET path (or NULL if not recorded).
150  * @param get_path_length number of entries in get_path.
151  * @param put_path peers on the PUT path (or NULL if not recorded).
152  * @param put_path_length number of entries in get_path.
153  * @param exp Expiration time of the data.
154  * @param key Key of the data.
155  * @param data Pointer to the result data.
156  * @param size Number of bytes in data.
157  */
158 void
159 get_resp_callback (void *cls,
160                    enum GNUNET_BLOCK_Type type,
161                    const struct GNUNET_PeerIdentity *get_path,
162                    unsigned int get_path_length,
163                    const struct GNUNET_PeerIdentity *put_path,
164                    unsigned int put_path_length,
165                    struct GNUNET_TIME_Absolute exp,
166                    const struct GNUNET_HashCode * key,
167                    const void *data,
168                    size_t size)
169 {
170   FPRINTF (stdout, "Result %d, operation: %s, type %d:\n Key: %s\n %.*s\n",
171            result_count,
172            "GET_RESP",
173            type,
174            GNUNET_h2s_full(key),
175            (unsigned int) size,
176            (char *) data);
177   result_count++;
178 }
179
180 /**
181  * Callback called on each PUT request going through the DHT.
182  *
183  * @param cls Closure.
184  * @param options Options, for instance RecordRoute, DemultiplexEverywhere.
185  * @param type The type of data in the request.
186  * @param hop_count Hop count so far.
187  * @param path_length number of entries in path (or 0 if not recorded).
188  * @param path peers on the PUT path (or NULL if not recorded).
189  * @param desired_replication_level Desired replication level.
190  * @param exp Expiration time of the data.
191  * @param key Key under which data is to be stored.
192  * @param data Pointer to the data carried.
193  * @param size Number of bytes in data.
194  */
195 void
196 put_callback (void *cls,
197               enum GNUNET_DHT_RouteOption options,
198               enum GNUNET_BLOCK_Type type,
199               uint32_t hop_count,
200               uint32_t desired_replication_level,
201               unsigned int path_length,
202               const struct GNUNET_PeerIdentity *path,
203               struct GNUNET_TIME_Absolute exp,
204               const struct GNUNET_HashCode * key,
205               const void *data,
206               size_t size)
207 {
208   FPRINTF (stdout, "Result %d, operation: %s, type %d:\n Key: %s\n %.*s\n",
209            result_count,
210            "PUT",
211            type,
212            GNUNET_h2s_full(key),
213            (unsigned int) size,
214            (char *) data);
215   result_count++;
216 }
217
218 /**
219  * Main function that will be run by the scheduler.
220  *
221  * @param cls closure
222  * @param args remaining command-line arguments
223  * @param cfgfile name of the configuration file used (for saving, can be NULL!)
224  * @param c configuration
225  */
226 static void
227 run (void *cls, char *const *args, const char *cfgfile,
228      const struct GNUNET_CONFIGURATION_Handle *c)
229 {
230   struct GNUNET_TIME_Relative timeout;
231   struct GNUNET_HashCode *key;
232
233   cfg = c;
234
235   dht_handle = GNUNET_DHT_connect (cfg, 1);
236
237   if (dht_handle == NULL)
238   {
239     if (verbose)
240       FPRINTF (stderr, "%s",  "Couldn't connect to DHT service!\n");
241     ret = 1;
242     return;
243   }
244   else if (verbose)
245     FPRINTF (stderr, "%s",  "Connected to DHT service!\n");
246
247   if (block_type == GNUNET_BLOCK_TYPE_ANY)      /* Type of data not set */
248     block_type = GNUNET_BLOCK_TYPE_TEST;
249
250   if (query_key != NULL) {
251     key = GNUNET_malloc (sizeof(struct GNUNET_HashCode));
252     GNUNET_CRYPTO_hash (query_key, strlen (query_key), key);
253   }
254   else
255     key = NULL;
256
257   if (0 != timeout_request)
258   {
259     timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
260                                              timeout_request);
261     if (verbose)
262       FPRINTF (stderr, "Monitoring for %llus\n", timeout_request);
263   }
264   else
265   {
266     timeout = GNUNET_TIME_UNIT_FOREVER_REL;
267     if (verbose)
268       FPRINTF (stderr, "%s", "Monitoring indefinitely (close with Ctrl+C)\n");
269   }
270
271   GNUNET_SCHEDULER_add_delayed (timeout, &cleanup_task, NULL);
272   if (verbose)
273     FPRINTF (stderr, "Issuing MONITOR request for %s!\n", query_key);
274   monitor_handle = GNUNET_DHT_monitor_start (dht_handle,
275                                              block_type,
276                                              key,
277                                              &get_callback,
278                                              &get_resp_callback,
279                                              &put_callback,
280                                              NULL);
281   if (verbose)
282     FPRINTF (stderr, "%s", "MONITOR started!\n");
283   GNUNET_free_non_null (key);
284
285 }
286
287
288 /**
289  * gnunet-dht-get command line options
290  */
291 static struct GNUNET_GETOPT_CommandLineOption options[] = {
292   {'k', "key", "KEY",
293    gettext_noop ("the query key"),
294    1, &GNUNET_GETOPT_set_string, &query_key},
295   {'t', "type", "TYPE",
296    gettext_noop ("the type of data to look for"),
297    1, &GNUNET_GETOPT_set_uint, &block_type},
298   {'T', "timeout", "TIMEOUT",
299    gettext_noop ("how long to execute? 0 = forever"),
300    1, &GNUNET_GETOPT_set_ulong, &timeout_request},
301   {'V', "verbose", NULL,
302    gettext_noop ("be verbose (print progress information)"),
303    0, &GNUNET_GETOPT_set_one, &verbose},
304   GNUNET_GETOPT_OPTION_END
305 };
306
307
308 /**
309  * Entry point for gnunet-dht-monitor
310  *
311  * @param argc number of arguments from the command line
312  * @param argv command line arguments
313  * @return 0 ok, 1 on error
314  */
315 int
316 main (int argc, char *const *argv)
317 {
318   if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
319     return 2;
320
321   return (GNUNET_OK ==
322           GNUNET_PROGRAM_run (argc, argv, "gnunet-dht-get",
323                               gettext_noop
324                               ("Prints all packets that go through the DHT."),
325                               options, &run, NULL)) ? ret : 1;
326 }
327
328 /* end of gnunet-dht-monitor.c */