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/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
21 * @file dht/gnunet-dht-monitor.c
22 * @brief search for data in DHT
23 * @author Christian Grothoff
24 * @author Bartlomiej Polot
27 #include "gnunet_dht_service.h"
30 * The type of the query
32 static unsigned int block_type;
35 * The key to be monitored
37 static char *query_key;
40 * User supplied timeout value (in seconds)
42 static struct GNUNET_TIME_Relative timeout_request = { 60000 };
52 static struct GNUNET_DHT_Handle *dht_handle;
55 * Global handle of the configuration
57 static const struct GNUNET_CONFIGURATION_Handle *cfg;
60 * Handle for the get request
62 static struct GNUNET_DHT_MonitorHandle *monitor_handle;
65 * Count of messages received
67 static unsigned int result_count;
75 * Task scheduled to handle timeout.
77 static struct GNUNET_SCHEDULER_Task *tt;
81 * Stop monitoring request and start shutdown
83 * @param cls closure (unused)
86 cleanup_task (void *cls)
89 fprintf (stderr, "%s", "Cleaning up!\n");
90 if (NULL != monitor_handle)
92 GNUNET_DHT_monitor_stop (monitor_handle);
93 monitor_handle = NULL;
95 if (NULL != dht_handle)
97 GNUNET_DHT_disconnect (dht_handle);
102 GNUNET_SCHEDULER_cancel (tt);
109 * We hit a timeout. Stop monitoring request and start shutdown
111 * @param cls closure (unused)
114 timeout_task (void *cls)
117 GNUNET_SCHEDULER_shutdown ();
122 * Callback called on each GET request going through the DHT.
124 * @param cls Closure.
125 * @param options Options, for instance RecordRoute, DemultiplexEverywhere.
126 * @param type The type of data in the request.
127 * @param hop_count Hop count so far.
128 * @param path_length number of entries in path (or 0 if not recorded).
129 * @param path peers on the GET path (or NULL if not recorded).
130 * @param desired_replication_level Desired replication level.
131 * @param key Key of the requested data.
134 get_callback (void *cls,
135 enum GNUNET_DHT_RouteOption options,
136 enum GNUNET_BLOCK_Type type,
138 uint32_t desired_replication_level,
139 unsigned int path_length,
140 const struct GNUNET_PeerIdentity *path,
141 const struct GNUNET_HashCode *key)
144 "GET #%u: type %d, key `%s'\n",
147 GNUNET_h2s_full (key));
153 * Callback called on each GET reply going through the DHT.
155 * @param cls Closure.
156 * @param type The type of data in the result.
157 * @param get_path Peers on GET path (or NULL if not recorded).
158 * @param get_path_length number of entries in get_path.
159 * @param put_path peers on the PUT path (or NULL if not recorded).
160 * @param put_path_length number of entries in get_path.
161 * @param exp Expiration time of the data.
162 * @param key Key of the data.
163 * @param data Pointer to the result data.
164 * @param size Number of bytes in data.
167 get_resp_callback (void *cls,
168 enum GNUNET_BLOCK_Type type,
169 const struct GNUNET_PeerIdentity *get_path,
170 unsigned int get_path_length,
171 const struct GNUNET_PeerIdentity *put_path,
172 unsigned int put_path_length,
173 struct GNUNET_TIME_Absolute exp,
174 const struct GNUNET_HashCode *key,
179 (GNUNET_BLOCK_TYPE_TEST == type)
180 ? "RESPONSE #%u (%s): type %d, key `%s', data `%.*s'\n"
181 : "RESPONSE #%u (%s): type %d, key `%s'\n",
183 GNUNET_STRINGS_absolute_time_to_string (exp),
185 GNUNET_h2s_full (key),
193 * Callback called on each PUT request going through the DHT.
195 * @param cls Closure.
196 * @param options Options, for instance RecordRoute, DemultiplexEverywhere.
197 * @param type The type of data in the request.
198 * @param hop_count Hop count so far.
199 * @param path_length number of entries in path (or 0 if not recorded).
200 * @param path peers on the PUT path (or NULL if not recorded).
201 * @param desired_replication_level Desired replication level.
202 * @param exp Expiration time of the data.
203 * @param key Key under which data is to be stored.
204 * @param data Pointer to the data carried.
205 * @param size Number of bytes in data.
208 put_callback (void *cls,
209 enum GNUNET_DHT_RouteOption options,
210 enum GNUNET_BLOCK_Type type,
212 uint32_t desired_replication_level,
213 unsigned int path_length,
214 const struct GNUNET_PeerIdentity *path,
215 struct GNUNET_TIME_Absolute exp,
216 const struct GNUNET_HashCode *key,
221 (GNUNET_BLOCK_TYPE_TEST == type)
222 ? "PUT %u (%s): type %d, key `%s', data `%.*s'\n"
223 : "PUT %u (%s): type %d, key `%s'\n",
225 GNUNET_STRINGS_absolute_time_to_string (exp),
227 GNUNET_h2s_full (key),
235 * Main function that will be run by the scheduler.
238 * @param args remaining command-line arguments
239 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
240 * @param c configuration
246 const struct GNUNET_CONFIGURATION_Handle *c)
248 struct GNUNET_HashCode *key;
249 struct GNUNET_HashCode hc;
253 if (NULL == (dht_handle = GNUNET_DHT_connect (cfg, 1)))
255 fprintf (stderr, "%s", _ ("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)
264 if (GNUNET_OK != GNUNET_CRYPTO_hash_from_string (query_key, key))
265 GNUNET_CRYPTO_hash (query_key, strlen (query_key), key);
273 "Monitoring for %s\n",
274 GNUNET_STRINGS_relative_time_to_string (timeout_request,
276 tt = GNUNET_SCHEDULER_add_delayed (timeout_request, &timeout_task, NULL);
277 GNUNET_SCHEDULER_add_shutdown (&cleanup_task, NULL);
278 monitor_handle = GNUNET_DHT_monitor_start (dht_handle,
289 * Entry point for gnunet-dht-monitor
291 * @param argc number of arguments from the command line
292 * @param argv command line arguments
293 * @return 0 ok, 1 on error
296 main (int argc, char *const *argv)
298 struct GNUNET_GETOPT_CommandLineOption options[] = {
299 GNUNET_GETOPT_option_string ('k',
302 gettext_noop ("the query key"),
305 GNUNET_GETOPT_option_uint ('t',
308 gettext_noop ("the type of data to look for"),
311 GNUNET_GETOPT_option_relative_time (
315 gettext_noop ("how long should the monitor command run"),
318 GNUNET_GETOPT_option_flag ('V',
321 "be verbose (print progress information)"),
324 GNUNET_GETOPT_OPTION_END
328 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
332 GNUNET_PROGRAM_run (argc,
334 "gnunet-dht-monitor",
336 "Prints all packets that go through the DHT."),
345 /* end of gnunet-dht-monitor.c */