aboutsummaryrefslogtreecommitdiff
path: root/src/dht/gnunet-dht-get.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/dht/gnunet-dht-get.c')
-rw-r--r--src/dht/gnunet-dht-get.c324
1 files changed, 0 insertions, 324 deletions
diff --git a/src/dht/gnunet-dht-get.c b/src/dht/gnunet-dht-get.c
deleted file mode 100644
index 42ffe75ba..000000000
--- a/src/dht/gnunet-dht-get.c
+++ /dev/null
@@ -1,324 +0,0 @@
1/*
2 This file is part of GNUnet.
3 Copyright (C) 2001, 2002, 2004, 2005, 2006, 2007, 2009, 2022 GNUnet e.V.
4
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.
9
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.
14
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/>.
17
18 SPDX-License-Identifier: AGPL3.0-or-later
19 */
20/**
21 * @file dht/gnunet-dht-get.c
22 * @brief search for data in DHT
23 * @author Christian Grothoff
24 * @author Nathan Evans
25 */
26#include "platform.h"
27#include "gnunet_dht_service.h"
28
29#define LOG(kind, ...) GNUNET_log_from (kind, "dht-clients", __VA_ARGS__)
30/**
31 * The type of the query
32 */
33static unsigned int query_type;
34
35/**
36 * Desired replication level
37 */
38static unsigned int replication = 5;
39
40/**
41 * The key for the query
42 */
43static char *query_key;
44
45/**
46 * User supplied timeout value
47 */
48static struct GNUNET_TIME_Relative timeout_request = { 60000 };
49
50/**
51 * Be verbose
52 */
53static unsigned int verbose;
54
55/**
56 * Use DHT demultixplex_everywhere
57 */
58static int demultixplex_everywhere;
59
60/**
61 * Use #GNUNET_DHT_RO_RECORD_ROUTE.
62 */
63static int record_route;
64
65/**
66 * Handle to the DHT
67 */
68static struct GNUNET_DHT_Handle *dht_handle;
69
70/**
71 * Global handle of the configuration
72 */
73static const struct GNUNET_CONFIGURATION_Handle *cfg;
74
75/**
76 * Handle for the get request
77 */
78static struct GNUNET_DHT_GetHandle *get_handle;
79
80/**
81 * Count of results found
82 */
83static unsigned int result_count;
84
85/**
86 * Global status value
87 */
88static int ret;
89
90/**
91 * Task scheduled to handle timeout.
92 */
93static struct GNUNET_SCHEDULER_Task *tt;
94
95
96/**
97 * Task run to clean up on shutdown.
98 *
99 * @param cls unused
100 */
101static void
102cleanup_task (void *cls)
103{
104 if (NULL != get_handle)
105 {
106 GNUNET_DHT_get_stop (get_handle);
107 get_handle = NULL;
108 }
109 if (NULL != dht_handle)
110 {
111 GNUNET_DHT_disconnect (dht_handle);
112 dht_handle = NULL;
113 }
114 if (NULL != tt)
115 {
116 GNUNET_SCHEDULER_cancel (tt);
117 tt = NULL;
118 }
119}
120
121
122/**
123 * Task run on timeout. Triggers shutdown.
124 *
125 * @param cls unused
126 */
127static void
128timeout_task (void *cls)
129{
130 tt = NULL;
131 GNUNET_SCHEDULER_shutdown ();
132}
133
134
135/**
136 * Iterator called on each result obtained for a DHT
137 * operation that expects a reply
138 *
139 * @param cls closure
140 * @param exp when will this value expire
141 * @param key key of the result
142 * @param get_path peers on reply path (or NULL if not recorded)
143 * @param get_path_length number of entries in get_path
144 * @param put_path peers on the PUT path (or NULL if not recorded)
145 * @param put_path_length number of entries in get_path
146 * @param type type of the result
147 * @param size number of bytes in data
148 * @param data pointer to the result data
149 */
150static void
151get_result_iterator (void *cls,
152 struct GNUNET_TIME_Absolute exp,
153 const struct GNUNET_HashCode *key,
154 const struct GNUNET_DHT_PathElement *get_path,
155 unsigned int get_path_length,
156 const struct GNUNET_DHT_PathElement *put_path,
157 unsigned int put_path_length,
158 enum GNUNET_BLOCK_Type type,
159 size_t size,
160 const void *data)
161{
162 fprintf (stdout,
163 (GNUNET_BLOCK_TYPE_TEST == type)
164 ? _ ("Result %d, type %d:\n%.*s\n")
165 : _ ("Result %d, type %d:\n"),
166 result_count,
167 type,
168 (int) size,
169 (char *) data);
170 if (record_route && verbose)
171 {
172 fprintf (stdout,
173 " GET path: ");
174 for (unsigned int i = 0; i < get_path_length; i++)
175 fprintf (stdout,
176 "%s%s",
177 (0 == i) ? "" : "-",
178 GNUNET_i2s (&get_path[i].pred));
179 fprintf (stdout,
180 "\n PUT path: ");
181 for (unsigned int i = 0; i < put_path_length; i++)
182 fprintf (stdout,
183 "%s%s",
184 (0 == i) ? "" : "-",
185 GNUNET_i2s (&put_path[i].pred));
186 fprintf (stdout,
187 "\n");
188 }
189 result_count++;
190}
191
192
193/**
194 * Main function that will be run by the scheduler.
195 *
196 * @param cls closure
197 * @param args remaining command-line arguments
198 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
199 * @param c configuration
200 */
201static void
202run (void *cls,
203 char *const *args,
204 const char *cfgfile,
205 const struct GNUNET_CONFIGURATION_Handle *c)
206{
207 struct GNUNET_HashCode key;
208 enum GNUNET_DHT_RouteOption ro;
209
210 cfg = c;
211 if (NULL == query_key)
212 {
213 fprintf (stderr,
214 "%s",
215 _ ("Must provide key for DHT GET!\n"));
216 ret = 1;
217 return;
218 }
219 if (NULL == (dht_handle = GNUNET_DHT_connect (cfg, 1)))
220 {
221 fprintf (stderr,
222 "%s",
223 _ ("Failed to connect to DHT service!\n"));
224 ret = 1;
225 return;
226 }
227 if (query_type == GNUNET_BLOCK_TYPE_ANY) /* Type of data not set */
228 query_type = GNUNET_BLOCK_TYPE_TEST;
229 GNUNET_CRYPTO_hash (query_key, strlen (query_key), &key);
230 if (verbose)
231 fprintf (stderr,
232 "%s `%s' \n",
233 _ ("Issuing DHT GET with key"),
234 GNUNET_h2s_full (&key));
235 GNUNET_SCHEDULER_add_shutdown (&cleanup_task, NULL);
236 tt = GNUNET_SCHEDULER_add_delayed (timeout_request, &timeout_task, NULL);
237 ro = GNUNET_DHT_RO_NONE;
238 if (demultixplex_everywhere)
239 ro |= GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE;
240 if (record_route)
241 ro |= GNUNET_DHT_RO_RECORD_ROUTE;
242 get_handle = GNUNET_DHT_get_start (dht_handle,
243 query_type,
244 &key,
245 replication,
246 ro,
247 NULL,
248 0,
249 &get_result_iterator,
250 NULL);
251}
252
253
254/**
255 * Entry point for gnunet-dht-get
256 *
257 * @param argc number of arguments from the command line
258 * @param argv command line arguments
259 * @return 0 ok, 1 on error
260 */
261int
262main (int argc, char *const *argv)
263{
264 struct GNUNET_GETOPT_CommandLineOption options[] = {
265 GNUNET_GETOPT_option_string (
266 'k',
267 "key",
268 "KEY",
269 gettext_noop ("the query key"),
270 &query_key),
271 GNUNET_GETOPT_option_uint (
272 'r',
273 "replication",
274 "LEVEL",
275 gettext_noop ("how many parallel requests (replicas) to create"),
276 &replication),
277 GNUNET_GETOPT_option_flag (
278 'R',
279 "record",
280 gettext_noop ("use DHT's record route option"),
281 &record_route),
282 GNUNET_GETOPT_option_uint (
283 't',
284 "type",
285 "TYPE",
286 gettext_noop ("the type of data to look for"),
287 &query_type),
288 GNUNET_GETOPT_option_relative_time (
289 'T',
290 "timeout",
291 "TIMEOUT",
292 gettext_noop ("how long to execute this query before giving up?"),
293 &timeout_request),
294 GNUNET_GETOPT_option_flag (
295 'x',
296 "demultiplex",
297 gettext_noop (
298 "use DHT's demultiplex everywhere option"),
299 &demultixplex_everywhere),
300 GNUNET_GETOPT_option_verbose (&verbose),
301 GNUNET_GETOPT_OPTION_END
302 };
303
304
305 if (GNUNET_OK !=
306 GNUNET_STRINGS_get_utf8_args (argc, argv,
307 &argc, &argv))
308 return 2;
309 return (GNUNET_OK ==
310 GNUNET_PROGRAM_run (
311 argc,
312 argv,
313 "gnunet-dht-get",
314 gettext_noop (
315 "Issue a GET request to the GNUnet DHT, prints results."),
316 options,
317 &run,
318 NULL))
319 ? ret
320 : 1;
321}
322
323
324/* end of gnunet-dht-get.c */