diff options
Diffstat (limited to 'src/dht/gnunet-dht-get.c')
-rw-r--r-- | src/dht/gnunet-dht-get.c | 330 |
1 files changed, 0 insertions, 330 deletions
diff --git a/src/dht/gnunet-dht-get.c b/src/dht/gnunet-dht-get.c deleted file mode 100644 index 806cafd0d..000000000 --- a/src/dht/gnunet-dht-get.c +++ /dev/null | |||
@@ -1,330 +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 | */ | ||
33 | static unsigned int query_type; | ||
34 | |||
35 | /** | ||
36 | * Desired replication level | ||
37 | */ | ||
38 | static unsigned int replication = 5; | ||
39 | |||
40 | /** | ||
41 | * The key for the query | ||
42 | */ | ||
43 | static char *query_key; | ||
44 | |||
45 | /** | ||
46 | * User supplied timeout value | ||
47 | */ | ||
48 | static struct GNUNET_TIME_Relative timeout_request = { 60000 }; | ||
49 | |||
50 | /** | ||
51 | * Be verbose | ||
52 | */ | ||
53 | static unsigned int verbose; | ||
54 | |||
55 | /** | ||
56 | * Use DHT demultixplex_everywhere | ||
57 | */ | ||
58 | static int demultixplex_everywhere; | ||
59 | |||
60 | /** | ||
61 | * Use #GNUNET_DHT_RO_RECORD_ROUTE. | ||
62 | */ | ||
63 | static int record_route; | ||
64 | |||
65 | /** | ||
66 | * Handle to the DHT | ||
67 | */ | ||
68 | static struct GNUNET_DHT_Handle *dht_handle; | ||
69 | |||
70 | /** | ||
71 | * Global handle of the configuration | ||
72 | */ | ||
73 | static const struct GNUNET_CONFIGURATION_Handle *cfg; | ||
74 | |||
75 | /** | ||
76 | * Handle for the get request | ||
77 | */ | ||
78 | static struct GNUNET_DHT_GetHandle *get_handle; | ||
79 | |||
80 | /** | ||
81 | * Count of results found | ||
82 | */ | ||
83 | static unsigned int result_count; | ||
84 | |||
85 | /** | ||
86 | * Global status value | ||
87 | */ | ||
88 | static int ret; | ||
89 | |||
90 | /** | ||
91 | * Task scheduled to handle timeout. | ||
92 | */ | ||
93 | static struct GNUNET_SCHEDULER_Task *tt; | ||
94 | |||
95 | |||
96 | /** | ||
97 | * Task run to clean up on shutdown. | ||
98 | * | ||
99 | * @param cls unused | ||
100 | */ | ||
101 | static void | ||
102 | cleanup_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 | */ | ||
127 | static void | ||
128 | timeout_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 | */ | ||
151 | static void | ||
152 | get_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 | fprintf (stdout, | ||
175 | " GET path: "); | ||
176 | for (unsigned int i = 0; i < get_path_length; i++) | ||
177 | fprintf (stdout, | ||
178 | "%s%s", | ||
179 | (0 == i) ? "" : "-", | ||
180 | GNUNET_i2s (&get_path[i].pred)); | ||
181 | fprintf (stdout, | ||
182 | "\n PUT path: "); | ||
183 | for (unsigned int i = 0; i < put_path_length; i++) | ||
184 | fprintf (stdout, | ||
185 | "%s%s", | ||
186 | (0 == i) ? "" : "-", | ||
187 | GNUNET_i2s (&put_path[i].pred)); | ||
188 | if (NULL != trunc_peer) | ||
189 | fprintf (stdout, | ||
190 | "T%s", | ||
191 | GNUNET_i2s (trunc_peer)); | ||
192 | fprintf (stdout, | ||
193 | "\n"); | ||
194 | } | ||
195 | result_count++; | ||
196 | } | ||
197 | |||
198 | |||
199 | /** | ||
200 | * Main function that will be run by the scheduler. | ||
201 | * | ||
202 | * @param cls closure | ||
203 | * @param args remaining command-line arguments | ||
204 | * @param cfgfile name of the configuration file used (for saving, can be NULL!) | ||
205 | * @param c configuration | ||
206 | */ | ||
207 | static void | ||
208 | run (void *cls, | ||
209 | char *const *args, | ||
210 | const char *cfgfile, | ||
211 | const struct GNUNET_CONFIGURATION_Handle *c) | ||
212 | { | ||
213 | struct GNUNET_HashCode key; | ||
214 | enum GNUNET_DHT_RouteOption ro; | ||
215 | |||
216 | cfg = c; | ||
217 | if (NULL == query_key) | ||
218 | { | ||
219 | fprintf (stderr, | ||
220 | "%s", | ||
221 | _ ("Must provide key for DHT GET!\n")); | ||
222 | ret = 1; | ||
223 | return; | ||
224 | } | ||
225 | if (NULL == (dht_handle = GNUNET_DHT_connect (cfg, 1))) | ||
226 | { | ||
227 | fprintf (stderr, | ||
228 | "%s", | ||
229 | _ ("Failed to connect to DHT service!\n")); | ||
230 | ret = 1; | ||
231 | return; | ||
232 | } | ||
233 | if (query_type == GNUNET_BLOCK_TYPE_ANY) /* Type of data not set */ | ||
234 | query_type = GNUNET_BLOCK_TYPE_TEST; | ||
235 | GNUNET_CRYPTO_hash (query_key, strlen (query_key), &key); | ||
236 | if (verbose) | ||
237 | fprintf (stderr, | ||
238 | "%s `%s' \n", | ||
239 | _ ("Issuing DHT GET with key"), | ||
240 | GNUNET_h2s_full (&key)); | ||
241 | GNUNET_SCHEDULER_add_shutdown (&cleanup_task, NULL); | ||
242 | tt = GNUNET_SCHEDULER_add_delayed (timeout_request, &timeout_task, NULL); | ||
243 | ro = GNUNET_DHT_RO_NONE; | ||
244 | if (demultixplex_everywhere) | ||
245 | ro |= GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE; | ||
246 | if (record_route) | ||
247 | ro |= GNUNET_DHT_RO_RECORD_ROUTE; | ||
248 | get_handle = GNUNET_DHT_get_start (dht_handle, | ||
249 | query_type, | ||
250 | &key, | ||
251 | replication, | ||
252 | ro, | ||
253 | NULL, | ||
254 | 0, | ||
255 | &get_result_iterator, | ||
256 | NULL); | ||
257 | } | ||
258 | |||
259 | |||
260 | /** | ||
261 | * Entry point for gnunet-dht-get | ||
262 | * | ||
263 | * @param argc number of arguments from the command line | ||
264 | * @param argv command line arguments | ||
265 | * @return 0 ok, 1 on error | ||
266 | */ | ||
267 | int | ||
268 | main (int argc, char *const *argv) | ||
269 | { | ||
270 | struct GNUNET_GETOPT_CommandLineOption options[] = { | ||
271 | GNUNET_GETOPT_option_string ( | ||
272 | 'k', | ||
273 | "key", | ||
274 | "KEY", | ||
275 | gettext_noop ("the query key"), | ||
276 | &query_key), | ||
277 | GNUNET_GETOPT_option_uint ( | ||
278 | 'r', | ||
279 | "replication", | ||
280 | "LEVEL", | ||
281 | gettext_noop ("how many parallel requests (replicas) to create"), | ||
282 | &replication), | ||
283 | GNUNET_GETOPT_option_flag ( | ||
284 | 'R', | ||
285 | "record", | ||
286 | gettext_noop ("use DHT's record route option"), | ||
287 | &record_route), | ||
288 | GNUNET_GETOPT_option_uint ( | ||
289 | 't', | ||
290 | "type", | ||
291 | "TYPE", | ||
292 | gettext_noop ("the type of data to look for"), | ||
293 | &query_type), | ||
294 | GNUNET_GETOPT_option_relative_time ( | ||
295 | 'T', | ||
296 | "timeout", | ||
297 | "TIMEOUT", | ||
298 | gettext_noop ("how long to execute this query before giving up?"), | ||
299 | &timeout_request), | ||
300 | GNUNET_GETOPT_option_flag ( | ||
301 | 'x', | ||
302 | "demultiplex", | ||
303 | gettext_noop ( | ||
304 | "use DHT's demultiplex everywhere option"), | ||
305 | &demultixplex_everywhere), | ||
306 | GNUNET_GETOPT_option_verbose (&verbose), | ||
307 | GNUNET_GETOPT_OPTION_END | ||
308 | }; | ||
309 | |||
310 | |||
311 | if (GNUNET_OK != | ||
312 | GNUNET_STRINGS_get_utf8_args (argc, argv, | ||
313 | &argc, &argv)) | ||
314 | return 2; | ||
315 | return (GNUNET_OK == | ||
316 | GNUNET_PROGRAM_run ( | ||
317 | argc, | ||
318 | argv, | ||
319 | "gnunet-dht-get", | ||
320 | gettext_noop ( | ||
321 | "Issue a GET request to the GNUnet DHT, prints results."), | ||
322 | options, | ||
323 | &run, | ||
324 | NULL)) | ||
325 | ? ret | ||
326 | : 1; | ||
327 | } | ||
328 | |||
329 | |||
330 | /* end of gnunet-dht-get.c */ | ||