GNUnet  0.11.x
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 
64 
68 static const struct GNUNET_CONFIGURATION_Handle *cfg;
69 
74 
78 static unsigned int result_count;
79 
83 static int ret;
84 
88 static struct GNUNET_SCHEDULER_Task *tt;
89 
90 
96 static void
97 cleanup_task (void *cls)
98 {
99  if (NULL != get_handle)
100  {
102  get_handle = NULL;
103  }
104  if (NULL != dht_handle)
105  {
107  dht_handle = NULL;
108  }
109  if (NULL != tt)
110  {
112  tt = NULL;
113  }
114 }
115 
116 
122 static void
123 timeout_task (void *cls)
124 {
125  tt = NULL;
127 }
128 
129 
145 static void
147  struct GNUNET_TIME_Absolute exp,
148  const struct GNUNET_HashCode *key,
149  const struct GNUNET_DHT_PathElement *get_path,
150  unsigned int get_path_length,
151  const struct GNUNET_DHT_PathElement *put_path,
152  unsigned int put_path_length,
153  enum GNUNET_BLOCK_Type type,
154  size_t size,
155  const void *data)
156 {
157  fprintf (stdout,
159  ? _ ("Result %d, type %d:\n%.*s\n")
160  : _ ("Result %d, type %d:\n"),
161  result_count,
162  type,
163  (unsigned int) size,
164  (char *) data);
165  if (verbose)
166  {
167  fprintf (stdout,
168  " GET path: ");
169  for (unsigned int i = 0; i < get_path_length; i++)
170  fprintf (stdout,
171  "%s%s",
172  (0 == i) ? "" : "-",
173  GNUNET_i2s (&get_path[i].pred));
174  fprintf (stdout,
175  "\n PUT path: ");
176  for (unsigned int i = 0; i < put_path_length; i++)
177  fprintf (stdout,
178  "%s%s",
179  (0 == i) ? "" : "-",
180  GNUNET_i2s (&put_path[i].pred));
181  fprintf (stdout,
182  "\n");
183  }
184  result_count++;
185 }
186 
187 
196 static void
197 run (void *cls,
198  char *const *args,
199  const char *cfgfile,
200  const struct GNUNET_CONFIGURATION_Handle *c)
201 {
202  struct GNUNET_HashCode key;
203 
204  cfg = c;
205  if (NULL == query_key)
206  {
207  fprintf (stderr,
208  "%s",
209  _ ("Must provide key for DHT GET!\n"));
210  ret = 1;
211  return;
212  }
213  if (NULL == (dht_handle = GNUNET_DHT_connect (cfg, 1)))
214  {
215  fprintf (stderr,
216  "%s",
217  _ ("Failed to connect to DHT service!\n"));
218  ret = 1;
219  return;
220  }
221  if (query_type == GNUNET_BLOCK_TYPE_ANY) /* Type of data not set */
224  if (verbose)
225  fprintf (stderr,
226  "%s `%s' \n",
227  _ ("Issuing DHT GET with key"),
228  GNUNET_h2s_full (&key));
232  query_type,
233  &key,
234  replication,
238  NULL,
239  0,
241  NULL);
242 }
243 
244 
252 int
253 main (int argc, char *const *argv)
254 {
257  'k',
258  "key",
259  "KEY",
260  gettext_noop ("the query key"),
261  &query_key),
263  'r',
264  "replication",
265  "LEVEL",
266  gettext_noop ("how many parallel requests (replicas) to create"),
267  &replication),
269  't',
270  "type",
271  "TYPE",
272  gettext_noop ("the type of data to look for"),
273  &query_type),
275  'T',
276  "timeout",
277  "TIMEOUT",
278  gettext_noop ("how long to execute this query before giving up?"),
279  &timeout_request),
281  'x',
282  "demultiplex",
283  gettext_noop (
284  "use DHT's demultiplex everywhere option"),
288  };
289 
290 
291  if (GNUNET_OK !=
292  GNUNET_STRINGS_get_utf8_args (argc, argv,
293  &argc, &argv))
294  return 2;
295  return (GNUNET_OK ==
297  argc,
298  argv,
299  "gnunet-dht-get",
300  gettext_noop (
301  "Issue a GET request to the GNUnet DHT, prints results."),
302  options,
303  &run,
304  NULL))
305  ? ret
306  : 1;
307 }
308 
309 
310 /* 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
#define gettext_noop(String)
Definition: gettext.h:69
static unsigned int query_type
The type of the query.
static void get_result_iterator(void *cls, struct GNUNET_TIME_Absolute exp, const struct GNUNET_HashCode *key, 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 verbose
Be verbose.
static struct GNUNET_TIME_Relative timeout_request
User supplied timeout value.
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.
struct GNUNET_HashCode key
The key used in the DHT.
uint32_t data
The data value.
@ GNUNET_OK
Definition: gnunet_common.h:95
GNUNET_BLOCK_Type
Blocks in the datastore and the datacache must have a unique type.
@ GNUNET_BLOCK_TYPE_TEST
Block for testing.
@ GNUNET_BLOCK_TYPE_ANY
Any type of block, used as a wildcard when searching.
void GNUNET_DHT_get_stop(struct GNUNET_DHT_GetHandle *get_handle)
Stop async DHT-get.
Definition: dht_api.c:1081
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:883
void GNUNET_DHT_disconnect(struct GNUNET_DHT_Handle *handle)
Shutdown connection with the DHT service.
Definition: dht_api.c:906
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:1008
@ GNUNET_DHT_RO_NONE
Default.
@ 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
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).
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:399
void GNUNET_SCHEDULER_shutdown(void)
Request the shutdown of a scheduler.
Definition: scheduler.c:531
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:1331
void * GNUNET_SCHEDULER_cancel(struct GNUNET_SCHEDULER_Task *task)
Cancel the task with the specified identifier.
Definition: scheduler.c:972
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:1269
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:1196
static unsigned int size
Size of the "table".
Definition: peer.c:67
#define _(String)
GNU gettext support macro.
Definition: platform.h:177
Handle to a GET request.
Definition: dht_api.c:81
Connection to the DHT service.
Definition: dht_api.c:203
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.
Entry in list of pending tasks.
Definition: scheduler.c:135
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