GNUnet 0.28.1-dev.4-24-g0cf3356dd
 
Loading...
Searching...
No Matches
gnunet-dht-get.c
Go to the documentation of this file.
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 */
26#include "platform.h"
27#include "gnunet_dht_service.h"
28
29#define LOG(kind, ...) GNUNET_log_from (kind, "dht-clients", __VA_ARGS__)
33static unsigned int query_type;
34
38static unsigned int replication = 5;
39
43static char *query_key;
44
48static int raw_key;
49
55static struct GNUNET_TIME_Relative timeout_request = { 60 * 1000 * 1000 };
56
60static unsigned int verbose;
61
66
70static int record_route;
71
76
80static const struct GNUNET_CONFIGURATION_Handle *cfg;
81
86
90static unsigned int result_count;
91
95static int ret;
96
101
102
108static void
109cleanup_task (void *cls)
110{
111 if (NULL != get_handle)
112 {
114 get_handle = NULL;
115 }
116 if (NULL != dht_handle)
117 {
119 dht_handle = NULL;
120 }
121 if (NULL != tt)
122 {
124 tt = NULL;
125 }
126}
127
128
134static void
135timeout_task (void *cls)
136{
137 tt = NULL;
139}
140
141
158static void
160 struct GNUNET_TIME_Absolute exp,
161 const struct GNUNET_HashCode *key,
162 const struct GNUNET_PeerIdentity *trunc_peer,
163 const struct GNUNET_DHT_PathElement *get_path,
164 unsigned int get_path_length,
165 const struct GNUNET_DHT_PathElement *put_path,
166 unsigned int put_path_length,
168 size_t size,
169 const void *data)
170{
171 fprintf (stdout,
173 ? _ ("Result %d, type %d:\n%.*s\n")
174 : _ ("Result %d, type %d:\n"),
176 type,
177 (int) size,
178 (char *) data);
179 if (record_route && verbose)
180 {
181 {
183
186 &my_identity));
187 GNUNET_break (0 ==
189 size,
190 exp,
191 trunc_peer,
192 put_path,
193 put_path_length,
194 get_path,
195 get_path_length,
196 &my_identity));
197 }
198 fprintf (stdout,
199 " GET path: ");
200 for (unsigned int i = 0; i < get_path_length; i++)
201 fprintf (stdout,
202 "%s%s",
203 (0 == i) ? "" : "-",
204 GNUNET_i2s (&get_path[i].pred));
205 fprintf (stdout,
206 "\n PUT path: ");
207 for (unsigned int i = 0; i < put_path_length; i++)
208 fprintf (stdout,
209 "%s%s",
210 (0 == i) ? "" : "-",
211 GNUNET_i2s (&put_path[i].pred));
212 if (NULL != trunc_peer)
213 fprintf (stdout,
214 "!%s",
215 GNUNET_i2s (trunc_peer));
216 fprintf (stdout,
217 "\n");
218 }
219 result_count++;
220}
221
222
231static void
232run (void *cls,
233 char *const *args,
234 const char *cfgfile,
235 const struct GNUNET_CONFIGURATION_Handle *c)
236{
237 struct GNUNET_HashCode key;
239
240 cfg = c;
241 if (NULL == query_key)
242 {
243 fprintf (stderr,
244 "%s",
245 _ ("Must provide key for DHT GET!\n"));
246 ret = 1;
247 return;
248 }
249 /* Resolve the key before connecting, so that a bad key does not leak
250 the DHT handle. */
251 if (raw_key)
252 {
253 if (GNUNET_OK !=
255 &key))
256 {
257 fprintf (stderr,
258 _ ("`%s' is not a valid key\n"),
259 query_key);
260 ret = 1;
261 return;
262 }
263 }
264 else
265 {
267 strlen (query_key),
268 &key);
269 }
270 if (NULL == (dht_handle = GNUNET_DHT_connect (cfg, 1)))
271 {
272 fprintf (stderr,
273 "%s",
274 _ ("Failed to connect to DHT service!\n"));
275 ret = 1;
276 return;
277 }
278 if (query_type == GNUNET_BLOCK_TYPE_ANY) /* Type of data not set */
280 if (verbose)
281 fprintf (stderr,
282 "%s `%s' \n",
283 _ ("Issuing DHT GET with key"),
290 if (record_route)
294 &key,
296 ro,
297 NULL,
298 0,
300 NULL);
301}
302
303
311int
312main (int argc, char *const *argv)
313{
316 'k',
317 "key",
318 "KEY",
320 "the string to hash into the query key (see -K to give the key itself)")
321 ,
322 &query_key),
324 'K',
325 "raw-key",
327 "interpret -k as the query key itself, in the base32 form GNUnet prints it, instead of hashing it"),
328 &raw_key),
330 'r',
331 "replication",
332 "LEVEL",
333 gettext_noop ("how many parallel requests (replicas) to create"),
334 &replication),
336 'R',
337 "record",
338 gettext_noop ("use DHT's record route option"),
339 &record_route),
341 't',
342 "type",
343 "TYPE",
344 gettext_noop ("the type of data to look for"),
345 &query_type),
347 'T',
348 "timeout",
349 "TIMEOUT",
350 gettext_noop ("how long to execute this query before giving up?"),
353 'x',
354 "demultiplex",
356 "use DHT's demultiplex everywhere option"),
360 };
361
362
363 ret = (GNUNET_OK ==
366 argc,
367 argv,
368 "gnunet-dht-get",
370 "Issue a GET request to the GNUnet DHT, prints results."),
371 options,
372 &run,
373 NULL))
374 ? ret
375 : 1;
376 return ret;
377}
378
379
380/* end of gnunet-dht-get.c */
struct GNUNET_GETOPT_CommandLineOption options[]
Definition 002.c:5
int main()
Program to simulate results from GCP_get_desirability_of_path() for various plausible inputs.
#define gettext_noop(String)
Definition gettext.h:74
static struct GNUNET_SCHEDULER_Task * timeout_task
Task to be run on timeout.
Definition gnunet-arm.c:123
static int raw_key
Is query_key the key itself rather than its preimage?
static unsigned int query_type
The type of the query.
static unsigned int verbose
Be verbose.
static struct GNUNET_TIME_Relative timeout_request
User supplied timeout value.
static void get_result_iterator(void *cls, struct GNUNET_TIME_Absolute exp, const struct GNUNET_HashCode *key, const struct GNUNET_PeerIdentity *trunc_peer, const struct GNUNET_DHT_PathElement *get_path, unsigned int get_path_length, const struct GNUNET_DHT_PathElement *put_path, unsigned int put_path_length, enum GNUNET_BLOCK_Type type, size_t size, const void *data)
Iterator called on each result obtained for a DHT operation that expects a reply.
static unsigned int result_count
Count of results found.
static const struct GNUNET_CONFIGURATION_Handle * cfg
Global handle of the configuration.
static int ret
Global status value.
static char * query_key
The key for the query.
static struct GNUNET_DHT_Handle * dht_handle
Handle to the DHT.
static struct GNUNET_SCHEDULER_Task * tt
Task scheduled to handle timeout.
static void run(void *cls, char *const *args, const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *c)
Main function that will be run by the scheduler.
static unsigned int replication
Desired replication level.
static struct GNUNET_DHT_GetHandle * get_handle
Handle for the get request.
static int demultixplex_everywhere
Use DHT demultixplex_everywhere.
static int record_route
Use GNUNET_DHT_RO_RECORD_ROUTE.
static char * data
The data to insert into the dht.
struct GNUNET_HashCode key
The key used in the DHT.
static uint32_t type
Type string converted to DNS type value.
static struct GNUNET_SCHEDULER_Task * cleanup_task
Cleanup task.
static struct GNUNET_PeerIdentity my_identity
Identity of this peer.
GNUNET_BLOCK_Type
WARNING: This header is generated! In order to add DHT block types, you must register them in GANA,...
@ GNUNET_BLOCK_TYPE_TEST
Block for testing.
@ GNUNET_BLOCK_TYPE_ANY
Identifier for any block.
API to the DHT service.
enum GNUNET_GenericReturnValue GNUNET_CRYPTO_get_peer_identity(const struct GNUNET_CONFIGURATION_Handle *cfg, struct GNUNET_PeerIdentity *dst)
Retrieve the identity of the host's peer.
GNUNET_DHT_RouteOption
Options for routing.
struct GNUNET_DHT_Handle * GNUNET_DHT_connect(const struct GNUNET_CONFIGURATION_Handle *cfg, unsigned int ht_len)
Initialize the connection with the DHT service.
Definition dht_api.c:1036
void GNUNET_DHT_get_stop(struct GNUNET_DHT_GetHandle *get_handle)
Stop async DHT-get.
Definition dht_api.c:1238
struct GNUNET_DHT_GetHandle * GNUNET_DHT_get_start(struct GNUNET_DHT_Handle *handle, enum GNUNET_BLOCK_Type type, const struct GNUNET_HashCode *key, uint32_t desired_replication_level, enum GNUNET_DHT_RouteOption options, const void *xquery, size_t xquery_size, GNUNET_DHT_GetIterator iter, void *iter_cls)
Perform an asynchronous GET operation on the DHT identified.
Definition dht_api.c:1165
void GNUNET_DHT_disconnect(struct GNUNET_DHT_Handle *handle)
Shutdown connection with the DHT service.
Definition dht_api.c:1058
unsigned int GNUNET_DHT_verify_path(const void *data, size_t data_size, struct GNUNET_TIME_Absolute exp_time, const struct GNUNET_PeerIdentity *trunc_peer, const struct GNUNET_DHT_PathElement *put_path, unsigned int put_path_len, const struct GNUNET_DHT_PathElement *get_path, unsigned int get_path_len, const struct GNUNET_PeerIdentity *me)
Verify signatures on a path consisting of put_path and get_path in reverse order (starting at the las...
Definition dht_api.c:1354
@ GNUNET_DHT_RO_NONE
Default.
@ GNUNET_DHT_RO_RECORD_ROUTE
We should keep track of the route that the message took in the P2P network.
@ GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE
Each peer along the way should process the request (otherwise only peers locally closest to the key w...
struct GNUNET_GETOPT_CommandLineOption GNUNET_GETOPT_option_uint(char shortName, const char *name, const char *argumentHelp, const char *description, unsigned int *val)
Allow user to specify an unsigned int.
struct GNUNET_GETOPT_CommandLineOption GNUNET_GETOPT_option_relative_time(char shortName, const char *name, const char *argumentHelp, const char *description, struct GNUNET_TIME_Relative *val)
Allow user to specify a struct GNUNET_TIME_Relative (using human-readable "fancy" time).
#define GNUNET_GETOPT_OPTION_END
Marker for the end of the list of options.
struct GNUNET_GETOPT_CommandLineOption GNUNET_GETOPT_option_verbose(unsigned int *level)
Define the '-V' verbosity option.
struct GNUNET_GETOPT_CommandLineOption GNUNET_GETOPT_option_flag(char shortName, const char *name, const char *description, int *val)
Allow user to specify a flag (which internally means setting an integer to 1/GNUNET_YES/GNUNET_OK.
struct GNUNET_GETOPT_CommandLineOption GNUNET_GETOPT_option_string(char shortName, const char *name, const char *argumentHelp, const char *description, char **str)
Allow user to specify a string.
void GNUNET_CRYPTO_hash(const void *block, size_t size, struct GNUNET_HashCode *ret)
Compute hash of a given block.
Definition crypto_hash.c:40
#define GNUNET_CRYPTO_hash_from_string(enc, result)
Convert ASCII encoding back to struct GNUNET_HashCode
@ GNUNET_OK
const char * GNUNET_i2s(const struct GNUNET_PeerIdentity *pid)
Convert a peer identity to a string (for printing debug messages).
const char * GNUNET_h2s_full(const struct GNUNET_HashCode *hc)
Convert a hash value to a string (for printing debug messages).
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur.
const struct GNUNET_OS_ProjectData * GNUNET_OS_project_data_gnunet(void)
Return default project data used by 'libgnunetutil' for GNUnet.
enum GNUNET_GenericReturnValue GNUNET_PROGRAM_run(const struct GNUNET_OS_ProjectData *pd, int argc, char *const *argv, const char *binaryName, const char *binaryHelp, const struct GNUNET_GETOPT_CommandLineOption *options, GNUNET_PROGRAM_Main task, void *task_cls)
Run a standard GNUnet command startup sequence (initialize loggers and configuration,...
Definition program.c:406
void GNUNET_SCHEDULER_shutdown(void)
Request the shutdown of a scheduler.
Definition scheduler.c:572
struct GNUNET_SCHEDULER_Task * GNUNET_SCHEDULER_add_shutdown(GNUNET_SCHEDULER_TaskCallback task, void *task_cls)
Schedule a new task to be run on shutdown, that is when a CTRL-C signal is received,...
Definition scheduler.c:1345
void * GNUNET_SCHEDULER_cancel(struct GNUNET_SCHEDULER_Task *task)
Cancel the task with the specified identifier.
Definition scheduler.c:986
struct GNUNET_SCHEDULER_Task * GNUNET_SCHEDULER_add_delayed(struct GNUNET_TIME_Relative delay, GNUNET_SCHEDULER_TaskCallback task, void *task_cls)
Schedule a new task to be run with a specified delay.
Definition scheduler.c:1283
static unsigned int size
Size of the "table".
Definition peer.c:68
#define _(String)
GNU gettext support macro.
Definition platform.h:179
Handle to a GET request.
Definition dht_api.c:79
Connection to the DHT service.
Definition dht_api.c:235
A (signed) path tracking a block's flow through the DHT is represented by an array of path elements,...
Definition of a command line option.
A 512-bit hashcode.
The identity of the host (wraps the signing key of the peer).
Entry in list of pending tasks.
Definition scheduler.c:141
Time for absolute times used by GNUnet, in microseconds.
Time for relative time used by GNUnet, in microseconds.