2 This file is part of GNUnet.
3 Copyright (C) 2001, 2002, 2004, 2005, 2006, 2007, 2009 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-get.c
22 * @brief search for data in DHT
23 * @author Christian Grothoff
24 * @author Nathan Evans
27 #include "gnunet_dht_service.h"
29 #define LOG(kind, ...) GNUNET_log_from (kind, "dht-clients", __VA_ARGS__)
31 * The type of the query
33 static unsigned int query_type;
36 * Desired replication level
38 static unsigned int replication = 5;
41 * The key for the query
43 static char *query_key;
46 * User supplied timeout value
48 static struct GNUNET_TIME_Relative timeout_request = { 60000 };
53 static unsigned int verbose;
56 * Use DHT demultixplex_everywhere
58 static int demultixplex_everywhere;
63 static struct GNUNET_DHT_Handle *dht_handle;
66 * Global handle of the configuration
68 static const struct GNUNET_CONFIGURATION_Handle *cfg;
71 * Handle for the get request
73 static struct GNUNET_DHT_GetHandle *get_handle;
76 * Count of results found
78 static unsigned int result_count;
86 * Task scheduled to handle timeout.
88 static struct GNUNET_SCHEDULER_Task *tt;
92 * Task run to clean up on shutdown.
97 cleanup_task (void *cls)
99 if (NULL != get_handle)
101 GNUNET_DHT_get_stop (get_handle);
104 if (NULL != dht_handle)
106 GNUNET_DHT_disconnect (dht_handle);
111 GNUNET_SCHEDULER_cancel (tt);
118 * Task run on timeout. Triggers shutdown.
123 timeout_task (void *cls)
126 GNUNET_SCHEDULER_shutdown ();
131 * Iterator called on each result obtained for a DHT
132 * operation that expects a reply
135 * @param exp when will this value expire
136 * @param key key of the result
137 * @param get_path peers on reply path (or NULL if not recorded)
138 * @param get_path_length number of entries in get_path
139 * @param put_path peers on the PUT path (or NULL if not recorded)
140 * @param put_path_length number of entries in get_path
141 * @param type type of the result
142 * @param size number of bytes in data
143 * @param data pointer to the result data
146 get_result_iterator (void *cls,
147 struct GNUNET_TIME_Absolute exp,
148 const struct GNUNET_HashCode *key,
149 const struct GNUNET_PeerIdentity *get_path,
150 unsigned int get_path_length,
151 const struct GNUNET_PeerIdentity *put_path,
152 unsigned int put_path_length,
153 enum GNUNET_BLOCK_Type type,
158 (GNUNET_BLOCK_TYPE_TEST == type) ? _ ("Result %d, type %d:\n%.*s\n")
159 : _ ("Result %d, type %d:\n"),
166 fprintf (stdout, " GET path: ");
167 for (unsigned int i = 0; i < get_path_length; i++)
168 fprintf (stdout, "%s%s", (0 == i) ? "" : "-", GNUNET_i2s (&get_path[i]));
169 fprintf (stdout, "\n PUT path: ");
170 for (unsigned int i = 0; i < put_path_length; i++)
171 fprintf (stdout, "%s%s", (0 == i) ? "" : "-", GNUNET_i2s (&put_path[i]));
172 fprintf (stdout, "\n");
179 * Main function that will be run by the scheduler.
182 * @param args remaining command-line arguments
183 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
184 * @param c configuration
190 const struct GNUNET_CONFIGURATION_Handle *c)
192 struct GNUNET_HashCode key;
195 if (NULL == query_key)
197 fprintf (stderr, "%s", _ ("Must provide key for DHT GET!\n"));
201 if (NULL == (dht_handle = GNUNET_DHT_connect (cfg, 1)))
203 fprintf (stderr, "%s", _ ("Failed to connect to DHT service!\n"));
207 if (query_type == GNUNET_BLOCK_TYPE_ANY) /* Type of data not set */
208 query_type = GNUNET_BLOCK_TYPE_TEST;
209 GNUNET_CRYPTO_hash (query_key, strlen (query_key), &key);
213 _ ("Issuing DHT GET with key"),
214 GNUNET_h2s_full (&key));
215 GNUNET_SCHEDULER_add_shutdown (&cleanup_task, NULL);
216 tt = GNUNET_SCHEDULER_add_delayed (timeout_request, &timeout_task, NULL);
217 get_handle = GNUNET_DHT_get_start (dht_handle,
221 (demultixplex_everywhere)
222 ? GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE
223 : GNUNET_DHT_RO_NONE,
226 &get_result_iterator,
232 * Entry point for gnunet-dht-get
234 * @param argc number of arguments from the command line
235 * @param argv command line arguments
236 * @return 0 ok, 1 on error
239 main (int argc, char *const *argv)
241 struct GNUNET_GETOPT_CommandLineOption options[] =
242 { GNUNET_GETOPT_option_string ('k',
245 gettext_noop ("the query key"),
247 GNUNET_GETOPT_option_uint (
251 gettext_noop ("how many parallel requests (replicas) to create"),
253 GNUNET_GETOPT_option_uint ('t',
256 gettext_noop ("the type of data to look for"),
258 GNUNET_GETOPT_option_relative_time (
262 gettext_noop ("how long to execute this query before giving up?"),
264 GNUNET_GETOPT_option_flag ('x',
267 "use DHT's demultiplex everywhere option"),
268 &demultixplex_everywhere),
269 GNUNET_GETOPT_option_verbose (&verbose),
270 GNUNET_GETOPT_OPTION_END };
273 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
281 "Issue a GET request to the GNUnet DHT, prints results."),
290 /* end of gnunet-dht-get.c */