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