2 This file is part of GNUnet.
3 (C) 2012 Christian Grothoff (and other contributing authors)
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.
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.
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.
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;
76 * Function called on shutdown, disconnects from DHT if necessary.
78 * @param cls closure (unused)
79 * @param tc Task Context
82 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
85 FPRINTF (stderr, "%s", "Shutting down!\n");
86 if (dht_handle != NULL)
88 GNUNET_DHT_disconnect (dht_handle);
95 * Stop monitoring request and start shutdown
97 * @param cls closure (unused)
98 * @param tc Task Context
101 cleanup_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
104 FPRINTF (stderr, "%s", "Cleaning up!\n");
105 if (monitor_handle != NULL)
107 GNUNET_DHT_monitor_stop (monitor_handle);
108 monitor_handle = NULL;
110 GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
115 * Callback called on each GET request going through the DHT.
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.
127 get_callback (void *cls,
128 enum GNUNET_DHT_RouteOption options,
129 enum GNUNET_BLOCK_Type type,
131 uint32_t desired_replication_level,
132 unsigned int path_length,
133 const struct GNUNET_PeerIdentity *path,
134 const struct GNUNET_HashCode * key)
136 FPRINTF (stdout, "Result %d, operation: %s, type %d\n Key: %s\n ",
140 GNUNET_h2s_full(key));
145 * Callback called on each GET reply going through the DHT.
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.
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,
170 FPRINTF (stdout, "Result %d, operation: %s, type %d:\n Key: %s\n %.*s\n",
174 GNUNET_h2s_full(key),
181 * Callback called on each PUT request going through the DHT.
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.
196 put_callback (void *cls,
197 enum GNUNET_DHT_RouteOption options,
198 enum GNUNET_BLOCK_Type type,
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,
208 FPRINTF (stdout, "Result %d, operation: %s, type %d:\n Key: %s\n %.*s\n",
212 GNUNET_h2s_full(key),
219 * Main function that will be run by the scheduler.
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
227 run (void *cls, char *const *args, const char *cfgfile,
228 const struct GNUNET_CONFIGURATION_Handle *c)
230 struct GNUNET_HashCode *key;
234 dht_handle = GNUNET_DHT_connect (cfg, 1);
236 if (dht_handle == NULL)
239 FPRINTF (stderr, "%s", "Couldn't connect to DHT service!\n");
244 FPRINTF (stderr, "%s", "Connected to DHT service!\n");
246 if (block_type == GNUNET_BLOCK_TYPE_ANY) /* Type of data not set */
247 block_type = GNUNET_BLOCK_TYPE_TEST;
249 if (query_key != NULL) {
250 key = GNUNET_malloc (sizeof(struct GNUNET_HashCode));
251 GNUNET_CRYPTO_hash (query_key, strlen (query_key), key);
258 if (GNUNET_TIME_UNIT_FOREVER_REL.rel_value != timeout_request.rel_value)
259 FPRINTF (stderr, "Monitoring for %s\n",
260 GNUNET_TIME_relative_to_string (timeout_request));
262 FPRINTF (stderr, "%s", "Monitoring indefinitely (close with Ctrl+C)\n");
265 GNUNET_SCHEDULER_add_delayed (timeout_request, &cleanup_task, NULL);
267 FPRINTF (stderr, "Issuing MONITOR request for %s!\n", query_key);
268 monitor_handle = GNUNET_DHT_monitor_start (dht_handle,
276 FPRINTF (stderr, "%s", "MONITOR started!\n");
277 GNUNET_free_non_null (key);
283 * gnunet-dht-get command line options
285 static struct GNUNET_GETOPT_CommandLineOption options[] = {
287 gettext_noop ("the query key"),
288 1, &GNUNET_GETOPT_set_string, &query_key},
289 {'t', "type", "TYPE",
290 gettext_noop ("the type of data to look for"),
291 1, &GNUNET_GETOPT_set_uint, &block_type},
292 {'T', "timeout", "TIMEOUT",
293 gettext_noop ("how long to execute? default 60s, use \"forever\" to monitor forever"),
294 1, &GNUNET_GETOPT_set_relative_time, &timeout_request},
295 {'V', "verbose", NULL,
296 gettext_noop ("be verbose (print progress information)"),
297 0, &GNUNET_GETOPT_set_one, &verbose},
298 GNUNET_GETOPT_OPTION_END
303 * Entry point for gnunet-dht-monitor
305 * @param argc number of arguments from the command line
306 * @param argv command line arguments
307 * @return 0 ok, 1 on error
310 main (int argc, char *const *argv)
312 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
316 GNUNET_PROGRAM_run (argc, argv, "gnunet-dht-get",
318 ("Prints all packets that go through the DHT."),
319 options, &run, NULL)) ? ret : 1;
322 /* end of gnunet-dht-monitor.c */