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 unsigned long long timeout_request = 5;
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 GNUNET_HashCode * key)
136 FPRINTF (stdout, "Result %d, operation: %s, type %d\n",
144 * Callback called on each GET reply going through the DHT.
146 * @param cls Closure.
147 * @param type The type of data in the result.
148 * @param get_path Peers on GET path (or NULL if not recorded).
149 * @param get_path_length number of entries in get_path.
150 * @param put_path peers on the PUT path (or NULL if not recorded).
151 * @param put_path_length number of entries in get_path.
152 * @param exp Expiration time of the data.
153 * @param key Key of the data.
154 * @param data Pointer to the result data.
155 * @param size Number of bytes in data.
158 get_resp_callback (void *cls,
159 enum GNUNET_BLOCK_Type type,
160 const struct GNUNET_PeerIdentity *get_path,
161 unsigned int get_path_length,
162 const struct GNUNET_PeerIdentity *put_path,
163 unsigned int put_path_length,
164 struct GNUNET_TIME_Absolute exp,
165 const GNUNET_HashCode * key,
169 FPRINTF (stdout, "Result %d, operation: %s, type %d:\n%.*s\n",
179 * Callback called on each PUT request going through the DHT.
181 * @param cls Closure.
182 * @param options Options, for instance RecordRoute, DemultiplexEverywhere.
183 * @param type The type of data in the request.
184 * @param hop_count Hop count so far.
185 * @param path_length number of entries in path (or 0 if not recorded).
186 * @param path peers on the PUT path (or NULL if not recorded).
187 * @param desired_replication_level Desired replication level.
188 * @param exp Expiration time of the data.
189 * @param key Key under which data is to be stored.
190 * @param data Pointer to the data carried.
191 * @param size Number of bytes in data.
194 put_callback (void *cls,
195 enum GNUNET_DHT_RouteOption options,
196 enum GNUNET_BLOCK_Type type,
198 uint32_t desired_replication_level,
199 unsigned int path_length,
200 const struct GNUNET_PeerIdentity *path,
201 struct GNUNET_TIME_Absolute exp,
202 const GNUNET_HashCode * key,
206 FPRINTF (stdout, "Result %d, operation: %s, type %d:\n%.*s\n",
216 * Main function that will be run by the scheduler.
219 * @param args remaining command-line arguments
220 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
221 * @param c configuration
224 run (void *cls, char *const *args, const char *cfgfile,
225 const struct GNUNET_CONFIGURATION_Handle *c)
227 struct GNUNET_TIME_Relative timeout;
228 GNUNET_HashCode *key;
232 dht_handle = GNUNET_DHT_connect (cfg, 1);
234 if (dht_handle == NULL)
237 FPRINTF (stderr, "%s", "Couldn't connect to DHT service!\n");
242 FPRINTF (stderr, "%s", "Connected to DHT service!\n");
244 if (block_type == GNUNET_BLOCK_TYPE_ANY) /* Type of data not set */
245 block_type = GNUNET_BLOCK_TYPE_TEST;
247 if (query_key != NULL) {
248 key = GNUNET_malloc (sizeof(GNUNET_HashCode));
249 GNUNET_CRYPTO_hash (query_key, strlen (query_key), key);
254 if (0 != timeout_request)
256 timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
259 FPRINTF (stderr, "Monitoring for %llus\n", timeout_request);
263 timeout = GNUNET_TIME_UNIT_FOREVER_REL;
265 FPRINTF (stderr, "%s", "Monitoring indefinitely (close with Ctrl+C)\n");
268 GNUNET_SCHEDULER_add_delayed (timeout, &cleanup_task, NULL);
270 FPRINTF (stderr, "Issuing MONITOR request for %s!\n", query_key);
271 monitor_handle = GNUNET_DHT_monitor_start (dht_handle,
279 FPRINTF (stderr, "%s", "MONITOR started!\n");
280 GNUNET_free_non_null (key);
286 * gnunet-dht-get command line options
288 static struct GNUNET_GETOPT_CommandLineOption options[] = {
290 gettext_noop ("the query key"),
291 1, &GNUNET_GETOPT_set_string, &query_key},
292 {'t', "type", "TYPE",
293 gettext_noop ("the type of data to look for"),
294 1, &GNUNET_GETOPT_set_uint, &block_type},
295 {'T', "timeout", "TIMEOUT",
296 gettext_noop ("how long to execute? 0 = forever"),
297 1, &GNUNET_GETOPT_set_ulong, &timeout_request},
298 {'V', "verbose", NULL,
299 gettext_noop ("be verbose (print progress information)"),
300 0, &GNUNET_GETOPT_set_one, &verbose},
301 GNUNET_GETOPT_OPTION_END
306 * Entry point for gnunet-dht-monitor
308 * @param argc number of arguments from the command line
309 * @param argv command line arguments
310 * @return 0 ok, 1 on error
313 main (int argc, char *const *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 */