2 This file is part of GNUnet.
3 Copyright (C) 2012 GNUnet e.V.
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.
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.
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/>.
19 * @file dht/gnunet-dht-monitor.c
20 * @brief search for data in DHT
21 * @author Christian Grothoff
22 * @author Bartlomiej Polot
25 #include "gnunet_dht_service.h"
28 * The type of the query
30 static unsigned int block_type;
33 * The key to be monitored
35 static char *query_key;
38 * User supplied timeout value (in seconds)
40 static struct GNUNET_TIME_Relative timeout_request = { 60000 };
50 static struct GNUNET_DHT_Handle *dht_handle;
53 * Global handle of the configuration
55 static const struct GNUNET_CONFIGURATION_Handle *cfg;
58 * Handle for the get request
60 static struct GNUNET_DHT_MonitorHandle *monitor_handle;
63 * Count of messages received
65 static unsigned int result_count;
73 * Task scheduled to handle timeout.
75 static struct GNUNET_SCHEDULER_Task *tt;
79 * Stop monitoring request and start shutdown
81 * @param cls closure (unused)
84 cleanup_task (void *cls)
87 FPRINTF (stderr, "%s", "Cleaning up!\n");
88 if (NULL != monitor_handle)
90 GNUNET_DHT_monitor_stop (monitor_handle);
91 monitor_handle = NULL;
93 if (NULL != dht_handle)
95 GNUNET_DHT_disconnect (dht_handle);
100 GNUNET_SCHEDULER_cancel (tt);
107 * We hit a timeout. Stop monitoring request and start shutdown
109 * @param cls closure (unused)
112 timeout_task (void *cls)
115 GNUNET_SCHEDULER_shutdown ();
121 * Callback called on each GET request going through the DHT.
123 * @param cls Closure.
124 * @param options Options, for instance RecordRoute, DemultiplexEverywhere.
125 * @param type The type of data in the request.
126 * @param hop_count Hop count so far.
127 * @param path_length number of entries in path (or 0 if not recorded).
128 * @param path peers on the GET path (or NULL if not recorded).
129 * @param desired_replication_level Desired replication level.
130 * @param key Key of the requested data.
133 get_callback (void *cls,
134 enum GNUNET_DHT_RouteOption options,
135 enum GNUNET_BLOCK_Type type,
137 uint32_t desired_replication_level,
138 unsigned int path_length,
139 const struct GNUNET_PeerIdentity *path,
140 const struct GNUNET_HashCode * key)
143 "GET #%u: type %d, key `%s'\n",
146 GNUNET_h2s_full(key));
152 * Callback called on each GET reply going through the DHT.
154 * @param cls Closure.
155 * @param type The type of data in the result.
156 * @param get_path Peers on GET path (or NULL if not recorded).
157 * @param get_path_length number of entries in get_path.
158 * @param put_path peers on the PUT path (or NULL if not recorded).
159 * @param put_path_length number of entries in get_path.
160 * @param exp Expiration time of the data.
161 * @param key Key of the data.
162 * @param data Pointer to the result data.
163 * @param size Number of bytes in data.
166 get_resp_callback (void *cls,
167 enum GNUNET_BLOCK_Type type,
168 const struct GNUNET_PeerIdentity *get_path,
169 unsigned int get_path_length,
170 const struct GNUNET_PeerIdentity *put_path,
171 unsigned int put_path_length,
172 struct GNUNET_TIME_Absolute exp,
173 const struct GNUNET_HashCode * key,
178 (GNUNET_BLOCK_TYPE_TEST == type)
179 ? "RESPONSE #%u (%s): type %d, key `%s', data `%.*s'\n"
180 : "RESPONSE #%u (%s): type %d, key `%s'\n",
182 GNUNET_STRINGS_absolute_time_to_string (exp),
184 GNUNET_h2s_full (key),
192 * Callback called on each PUT request going through the DHT.
194 * @param cls Closure.
195 * @param options Options, for instance RecordRoute, DemultiplexEverywhere.
196 * @param type The type of data in the request.
197 * @param hop_count Hop count so far.
198 * @param path_length number of entries in path (or 0 if not recorded).
199 * @param path peers on the PUT path (or NULL if not recorded).
200 * @param desired_replication_level Desired replication level.
201 * @param exp Expiration time of the data.
202 * @param key Key under which data is to be stored.
203 * @param data Pointer to the data carried.
204 * @param size Number of bytes in data.
207 put_callback (void *cls,
208 enum GNUNET_DHT_RouteOption options,
209 enum GNUNET_BLOCK_Type type,
211 uint32_t desired_replication_level,
212 unsigned int path_length,
213 const struct GNUNET_PeerIdentity *path,
214 struct GNUNET_TIME_Absolute exp,
215 const struct GNUNET_HashCode * key,
220 (GNUNET_BLOCK_TYPE_TEST == type)
221 ? "PUT %u (%s): type %d, key `%s', data `%.*s'\n"
222 : "PUT %u (%s): type %d, key `%s'\n",
224 GNUNET_STRINGS_absolute_time_to_string (exp),
226 GNUNET_h2s_full(key),
234 * Main function that will be run by the scheduler.
237 * @param args remaining command-line arguments
238 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
239 * @param c configuration
245 const struct GNUNET_CONFIGURATION_Handle *c)
247 struct GNUNET_HashCode *key;
248 struct GNUNET_HashCode hc;
252 if (NULL == (dht_handle = GNUNET_DHT_connect (cfg, 1)))
254 FPRINTF (stderr, "%s",
255 _("Failed to connect to DHT service!\n"));
259 if (GNUNET_BLOCK_TYPE_ANY == block_type) /* Type of data not set */
260 block_type = GNUNET_BLOCK_TYPE_TEST;
261 if (NULL != query_key)
265 GNUNET_CRYPTO_hash_from_string (query_key, key))
266 GNUNET_CRYPTO_hash (query_key, strlen (query_key), key);
274 "Monitoring for %s\n",
275 GNUNET_STRINGS_relative_time_to_string (timeout_request, GNUNET_NO));
276 tt = GNUNET_SCHEDULER_add_delayed (timeout_request,
279 GNUNET_SCHEDULER_add_shutdown (&cleanup_task,
281 monitor_handle = GNUNET_DHT_monitor_start (dht_handle,
291 * Entry point for gnunet-dht-monitor
293 * @param argc number of arguments from the command line
294 * @param argv command line arguments
295 * @return 0 ok, 1 on error
298 main (int argc, char *const *argv)
300 struct GNUNET_GETOPT_CommandLineOption options[] = {
302 GNUNET_GETOPT_option_string ('k',
305 gettext_noop ("the query key"),
308 GNUNET_GETOPT_option_uint ('t',
311 gettext_noop ("the type of data to look for"),
314 GNUNET_GETOPT_option_relative_time ('T',
317 gettext_noop ("how long should the monitor command run"),
320 GNUNET_GETOPT_option_flag ('V',
322 gettext_noop ("be verbose (print progress information)"),
325 GNUNET_GETOPT_OPTION_END
329 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
333 GNUNET_PROGRAM_run (argc, argv, "gnunet-dht-monitor",
335 ("Prints all packets that go through the DHT."),
336 options, &run, NULL)) ? ret : 1;
339 /* end of gnunet-dht-monitor.c */