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