GNUnet  0.20.0
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__)
33 static unsigned int query_type;
34 
38 static unsigned int replication = 5;
39 
43 static char *query_key;
44 
48 static struct GNUNET_TIME_Relative timeout_request = { 60000 };
49 
53 static unsigned int verbose;
54 
59 
63 static int record_route;
64 
69 
73 static const struct GNUNET_CONFIGURATION_Handle *cfg;
74 
79 
83 static unsigned int result_count;
84 
88 static int ret;
89 
93 static struct GNUNET_SCHEDULER_Task *tt;
94 
95 
101 static void
102 cleanup_task (void *cls)
103 {
104  if (NULL != get_handle)
105  {
107  get_handle = NULL;
108  }
109  if (NULL != dht_handle)
110  {
112  dht_handle = NULL;
113  }
114  if (NULL != tt)
115  {
117  tt = NULL;
118  }
119 }
120 
121 
127 static void
128 timeout_task (void *cls)
129 {
130  tt = NULL;
132 }
133 
134 
151 static void
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,
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  {
176 
179  &my_identity));
180  GNUNET_break (0 ==
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 
224 static void
225 run (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 */
253  if (verbose)
254  fprintf (stderr,
255  "%s `%s' \n",
256  _ ("Issuing DHT GET with key"),
257  GNUNET_h2s_full (&key));
260  ro = GNUNET_DHT_RO_NONE;
263  if (record_route)
266  query_type,
267  &key,
268  replication,
269  ro,
270  NULL,
271  0,
273  NULL);
274 }
275 
276 
284 int
285 main (int argc, char *const *argv)
286 {
287  char *u8_argv = NULL;
290  'k',
291  "key",
292  "KEY",
293  gettext_noop ("the query key"),
294  &query_key),
296  'r',
297  "replication",
298  "LEVEL",
299  gettext_noop ("how many parallel requests (replicas) to create"),
300  &replication),
302  'R',
303  "record",
304  gettext_noop ("use DHT's record route option"),
305  &record_route),
307  't',
308  "type",
309  "TYPE",
310  gettext_noop ("the type of data to look for"),
311  &query_type),
313  'T',
314  "timeout",
315  "TIMEOUT",
316  gettext_noop ("how long to execute this query before giving up?"),
317  &timeout_request),
319  'x',
320  "demultiplex",
321  gettext_noop (
322  "use DHT's demultiplex everywhere option"),
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 ==
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 */
struct GNUNET_GETOPT_CommandLineOption GNUNET_GETOPT_OPTION_END
Definition: 002.c:13
struct GNUNET_GETOPT_CommandLineOption options[]
Definition: 002.c:5
GNUNET_BLOCK_Type
WARNING: This header is generated! In order to add DHT block types, you must register them in GANA,...
@ GNUNET_BLOCK_TYPE_ANY
Identifier for any block.
@ GNUNET_BLOCK_TYPE_TEST
Block for testing.
#define gettext_noop(String)
Definition: gettext.h:70
struct GNUNET_PeerIdentity my_identity
Our peer identity.
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 void timeout_task(void *cls)
Task run on timeout.
static unsigned int result_count
Count of results found.
static const struct GNUNET_CONFIGURATION_Handle * cfg
Global handle of the configuration.
static void cleanup_task(void *cls)
Task run to clean up on shutdown.
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.
int main(int argc, char *const *argv)
Entry point for gnunet-dht-get.
static int record_route
Use GNUNET_DHT_RO_RECORD_ROUTE.
struct GNUNET_HashCode key
The key used in the DHT.
uint32_t data
The data value.
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.
void GNUNET_DHT_get_stop(struct GNUNET_DHT_GetHandle *get_handle)
Stop async DHT-get.
Definition: dht_api.c:1237
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:1039
void GNUNET_DHT_disconnect(struct GNUNET_DHT_Handle *handle)
Shutdown connection with the DHT service.
Definition: dht_api.c:1062
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:1164
unsigned int GNUNET_DHT_verify_path(const void *data, size_t data_size, struct GNUNET_TIME_Absolute exp_time, const struct GNUNET_PeerIdentity *bpid, 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:1353
@ 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).
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:41
@ 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.
#define GNUNET_free(ptr)
Wrapper around free.
enum GNUNET_GenericReturnValue GNUNET_PROGRAM_run(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:400
void GNUNET_SCHEDULER_shutdown(void)
Request the shutdown of a scheduler.
Definition: scheduler.c:562
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:1334
void * GNUNET_SCHEDULER_cancel(struct GNUNET_SCHEDULER_Task *task)
Cancel the task with the specified identifier.
Definition: scheduler.c:975
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:1272
enum GNUNET_GenericReturnValue GNUNET_STRINGS_get_utf8_args(int argc, char *const *argv, int *u8argc, char *const **u8argv)
Returns utf-8 encoded arguments.
Definition: strings.c:1222
static unsigned int size
Size of the "table".
Definition: peer.c:68
#define _(String)
GNU gettext support macro.
Definition: platform.h:178
Handle to a GET request.
Definition: dht_api.c:81
Connection to the DHT service.
Definition: dht_api.c:237
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:136
Time for absolute times used by GNUnet, in microseconds.
Time for relative time used by GNUnet, in microseconds.
enum GNUNET_TESTBED_UnderlayLinkModelType type
the type of this model