413f8c79ae740f2bd55aa3d30fe4d1b910711307
[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 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 /**
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\n ",
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_HashCode *key;
231
232   cfg = c;
233
234   dht_handle = GNUNET_DHT_connect (cfg, 1);
235
236   if (dht_handle == NULL)
237   {
238     if (verbose)
239       FPRINTF (stderr, "%s",  "Couldn't connect to DHT service!\n");
240     ret = 1;
241     return;
242   }
243   else if (verbose)
244     FPRINTF (stderr, "%s",  "Connected to DHT service!\n");
245
246   if (block_type == GNUNET_BLOCK_TYPE_ANY)      /* Type of data not set */
247     block_type = GNUNET_BLOCK_TYPE_TEST;
248
249   if (query_key != NULL) {
250     key = GNUNET_malloc (sizeof(struct GNUNET_HashCode));
251     GNUNET_CRYPTO_hash (query_key, strlen (query_key), key);
252   }
253   else
254     key = NULL;
255
256   if (verbose)
257   {
258       if (GNUNET_TIME_UNIT_FOREVER_REL.rel_value != timeout_request.rel_value)
259         FPRINTF (stderr, "Monitoring for %lus\n", timeout_request.rel_value / 1000);
260       else
261         FPRINTF (stderr, "%s", "Monitoring indefinitely (close with Ctrl+C)\n");
262   }
263
264   GNUNET_SCHEDULER_add_delayed (timeout_request, &cleanup_task, NULL);
265   if (verbose)
266     FPRINTF (stderr, "Issuing MONITOR request for %s!\n", query_key);
267   monitor_handle = GNUNET_DHT_monitor_start (dht_handle,
268                                              block_type,
269                                              key,
270                                              &get_callback,
271                                              &get_resp_callback,
272                                              &put_callback,
273                                              NULL);
274   if (verbose)
275     FPRINTF (stderr, "%s", "MONITOR started!\n");
276   GNUNET_free_non_null (key);
277
278 }
279
280
281 /**
282  * gnunet-dht-get command line options
283  */
284 static struct GNUNET_GETOPT_CommandLineOption options[] = {
285   {'k', "key", "KEY",
286    gettext_noop ("the query key"),
287    1, &GNUNET_GETOPT_set_string, &query_key},
288   {'t', "type", "TYPE",
289    gettext_noop ("the type of data to look for"),
290    1, &GNUNET_GETOPT_set_uint, &block_type},
291   {'T', "timeout", "TIMEOUT",
292    gettext_noop ("how long to execute? default 60s, use \"forever\" to monitor forever"),
293    1, &GNUNET_GETOPT_set_relative_time, &timeout_request},
294   {'V', "verbose", NULL,
295    gettext_noop ("be verbose (print progress information)"),
296    0, &GNUNET_GETOPT_set_one, &verbose},
297   GNUNET_GETOPT_OPTION_END
298 };
299
300
301 /**
302  * Entry point for gnunet-dht-monitor
303  *
304  * @param argc number of arguments from the command line
305  * @param argv command line arguments
306  * @return 0 ok, 1 on error
307  */
308 int
309 main (int argc, char *const *argv)
310 {
311   if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
312     return 2;
313
314   return (GNUNET_OK ==
315           GNUNET_PROGRAM_run (argc, argv, "gnunet-dht-get",
316                               gettext_noop
317                               ("Prints all packets that go through the DHT."),
318                               options, &run, NULL)) ? ret : 1;
319 }
320
321 /* end of gnunet-dht-monitor.c */