aboutsummaryrefslogtreecommitdiff
path: root/src/cli/dht/gnunet-dht-get.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/cli/dht/gnunet-dht-get.c')
-rw-r--r--src/cli/dht/gnunet-dht-get.c352
1 files changed, 352 insertions, 0 deletions
diff --git a/src/cli/dht/gnunet-dht-get.c b/src/cli/dht/gnunet-dht-get.c
new file mode 100644
index 000000000..393184bb6
--- /dev/null
+++ b/src/cli/dht/gnunet-dht-get.c
@@ -0,0 +1,352 @@
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 trunc_peer peer at which the path was truncated, or NULL if not
143 * @param get_path peers on reply path (or NULL if not recorded)
144 * @param get_path_length number of entries in get_path
145 * @param put_path peers on the PUT path (or NULL if not recorded)
146 * @param put_path_length number of entries in get_path
147 * @param type type of the result
148 * @param size number of bytes in data
149 * @param data pointer to the result data
150 */
151static void
152get_result_iterator (void *cls,
153 struct GNUNET_TIME_Absolute exp,
154 const struct GNUNET_HashCode *key,
155 const struct GNUNET_PeerIdentity *trunc_peer,
156 const struct GNUNET_DHT_PathElement *get_path,
157 unsigned int get_path_length,
158 const struct GNUNET_DHT_PathElement *put_path,
159 unsigned int put_path_length,
160 enum GNUNET_BLOCK_Type type,
161 size_t size,
162 const void *data)
163{
164 fprintf (stdout,
165 (GNUNET_BLOCK_TYPE_TEST == type)
166 ? _ ("Result %d, type %d:\n%.*s\n")
167 : _ ("Result %d, type %d:\n"),
168 result_count,
169 type,
170 (int) size,
171 (char *) data);
172 if (record_route && verbose)
173 {
174 {
175 struct GNUNET_PeerIdentity my_identity;
176
177 GNUNET_break (GNUNET_OK ==
178 GNUNET_CRYPTO_get_peer_identity (cfg,
179 &my_identity));
180 GNUNET_break (0 ==
181 GNUNET_DHT_verify_path (data,
182 size,
183 exp,
184 trunc_peer,
185 put_path,
186 put_path_length,
187 get_path,
188 get_path_length,
189 &my_identity));
190 }
191 fprintf (stdout,
192 " GET path: ");
193 for (unsigned int i = 0; i < get_path_length; i++)
194 fprintf (stdout,
195 "%s%s",
196 (0 == i) ? "" : "-",
197 GNUNET_i2s (&get_path[i].pred));
198 fprintf (stdout,
199 "\n PUT path: ");
200 for (unsigned int i = 0; i < put_path_length; i++)
201 fprintf (stdout,
202 "%s%s",
203 (0 == i) ? "" : "-",
204 GNUNET_i2s (&put_path[i].pred));
205 if (NULL != trunc_peer)
206 fprintf (stdout,
207 "!%s",
208 GNUNET_i2s (trunc_peer));
209 fprintf (stdout,
210 "\n");
211 }
212 result_count++;
213}
214
215
216/**
217 * Main function that will be run by the scheduler.
218 *
219 * @param cls closure
220 * @param args remaining command-line arguments
221 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
222 * @param c configuration
223 */
224static void
225run (void *cls,
226 char *const *args,
227 const char *cfgfile,
228 const struct GNUNET_CONFIGURATION_Handle *c)
229{
230 struct GNUNET_HashCode key;
231 enum GNUNET_DHT_RouteOption ro;
232
233 cfg = c;
234 if (NULL == query_key)
235 {
236 fprintf (stderr,
237 "%s",
238 _ ("Must provide key for DHT GET!\n"));
239 ret = 1;
240 return;
241 }
242 if (NULL == (dht_handle = GNUNET_DHT_connect (cfg, 1)))
243 {
244 fprintf (stderr,
245 "%s",
246 _ ("Failed to connect to DHT service!\n"));
247 ret = 1;
248 return;
249 }
250 if (query_type == GNUNET_BLOCK_TYPE_ANY) /* Type of data not set */
251 query_type = GNUNET_BLOCK_TYPE_TEST;
252 GNUNET_CRYPTO_hash (query_key, strlen (query_key), &key);
253 if (verbose)
254 fprintf (stderr,
255 "%s `%s' \n",
256 _ ("Issuing DHT GET with key"),
257 GNUNET_h2s_full (&key));
258 GNUNET_SCHEDULER_add_shutdown (&cleanup_task, NULL);
259 tt = GNUNET_SCHEDULER_add_delayed (timeout_request, &timeout_task, NULL);
260 ro = GNUNET_DHT_RO_NONE;
261 if (demultixplex_everywhere)
262 ro |= GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE;
263 if (record_route)
264 ro |= GNUNET_DHT_RO_RECORD_ROUTE;
265 get_handle = GNUNET_DHT_get_start (dht_handle,
266 query_type,
267 &key,
268 replication,
269 ro,
270 NULL,
271 0,
272 &get_result_iterator,
273 NULL);
274}
275
276
277/**
278 * Entry point for gnunet-dht-get
279 *
280 * @param argc number of arguments from the command line
281 * @param argv command line arguments
282 * @return 0 ok, 1 on error
283 */
284int
285main (int argc, char *const *argv)
286{
287 char *u8_argv = NULL;
288 struct GNUNET_GETOPT_CommandLineOption options[] = {
289 GNUNET_GETOPT_option_string (
290 'k',
291 "key",
292 "KEY",
293 gettext_noop ("the query key"),
294 &query_key),
295 GNUNET_GETOPT_option_uint (
296 'r',
297 "replication",
298 "LEVEL",
299 gettext_noop ("how many parallel requests (replicas) to create"),
300 &replication),
301 GNUNET_GETOPT_option_flag (
302 'R',
303 "record",
304 gettext_noop ("use DHT's record route option"),
305 &record_route),
306 GNUNET_GETOPT_option_uint (
307 't',
308 "type",
309 "TYPE",
310 gettext_noop ("the type of data to look for"),
311 &query_type),
312 GNUNET_GETOPT_option_relative_time (
313 'T',
314 "timeout",
315 "TIMEOUT",
316 gettext_noop ("how long to execute this query before giving up?"),
317 &timeout_request),
318 GNUNET_GETOPT_option_flag (
319 'x',
320 "demultiplex",
321 gettext_noop (
322 "use DHT's demultiplex everywhere option"),
323 &demultixplex_everywhere),
324 GNUNET_GETOPT_option_verbose (&verbose),
325 GNUNET_GETOPT_OPTION_END
326 };
327
328
329 if (GNUNET_OK !=
330 GNUNET_STRINGS_get_utf8_args (argc, argv,
331 &argc, &argv))
332 return 2;
333 ret = (GNUNET_OK ==
334 GNUNET_PROGRAM_run (
335 argc,
336 argv,
337 "gnunet-dht-get",
338 gettext_noop (
339 "Issue a GET request to the GNUnet DHT, prints results."),
340 options,
341 &run,
342 NULL))
343 ? ret
344 : 1;
345 // This is ugly, but meh. The GNUNET_STRINGS_get_utf8_args allows us to do this.
346 u8_argv = (char*) argv;
347 GNUNET_free (u8_argv);
348 return ret;
349}
350
351
352/* end of gnunet-dht-get.c */