2 This file is part of GNUnet.
3 Copyright (C) 2001, 2002, 2004, 2005, 2006, 2007, 2009 GNUnet e.V.
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.
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.
16 * @file dht/gnunet-dht-get.c
17 * @brief search for data in DHT
18 * @author Christian Grothoff
19 * @author Nathan Evans
22 #include "gnunet_dht_service.h"
24 #define LOG(kind,...) GNUNET_log_from (kind, "dht-clients",__VA_ARGS__)
26 * The type of the query
28 static unsigned int query_type;
31 * Desired replication level
33 static unsigned int replication = 5;
36 * The key for the query
38 static char *query_key;
41 * User supplied timeout value
43 static struct GNUNET_TIME_Relative timeout_request = { 60000 };
48 static unsigned int verbose;
51 * Use DHT demultixplex_everywhere
53 static int demultixplex_everywhere;
58 static struct GNUNET_DHT_Handle *dht_handle;
61 * Global handle of the configuration
63 static const struct GNUNET_CONFIGURATION_Handle *cfg;
66 * Handle for the get request
68 static struct GNUNET_DHT_GetHandle *get_handle;
71 * Count of results found
73 static unsigned int result_count;
81 * Task scheduled to handle timeout.
83 static struct GNUNET_SCHEDULER_Task *tt;
87 * Task run to clean up on shutdown.
92 cleanup_task (void *cls)
94 if (NULL != get_handle)
96 GNUNET_DHT_get_stop (get_handle);
99 if (NULL != dht_handle)
101 GNUNET_DHT_disconnect (dht_handle);
106 GNUNET_SCHEDULER_cancel (tt);
113 * Task run on timeout. Triggers shutdown.
118 timeout_task (void *cls)
121 GNUNET_SCHEDULER_shutdown ();
126 * Iterator called on each result obtained for a DHT
127 * operation that expects a reply
130 * @param exp when will this value expire
131 * @param key key of the result
132 * @param get_path peers on reply path (or NULL if not recorded)
133 * @param get_path_length number of entries in get_path
134 * @param put_path peers on the PUT path (or NULL if not recorded)
135 * @param put_path_length number of entries in get_path
136 * @param type type of the result
137 * @param size number of bytes in data
138 * @param data pointer to the result data
141 get_result_iterator (void *cls, struct GNUNET_TIME_Absolute exp,
142 const struct GNUNET_HashCode * key,
143 const struct GNUNET_PeerIdentity *get_path,
144 unsigned int get_path_length,
145 const struct GNUNET_PeerIdentity *put_path,
146 unsigned int put_path_length,
147 enum GNUNET_BLOCK_Type type,
152 (GNUNET_BLOCK_TYPE_TEST == type)
153 ? _("Result %d, type %d:\n%.*s\n")
154 : _("Result %d, type %d:\n"),
163 for (unsigned int i=0;i<get_path_length;i++)
167 GNUNET_i2s (&get_path[i]));
170 for (unsigned int i=0;i<put_path_length;i++)
174 GNUNET_i2s (&put_path[i]));
183 * Main function that will be run by the scheduler.
186 * @param args remaining command-line arguments
187 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
188 * @param c configuration
191 run (void *cls, char *const *args, const char *cfgfile,
192 const struct GNUNET_CONFIGURATION_Handle *c)
194 struct GNUNET_HashCode key;
197 if (NULL == query_key)
199 FPRINTF (stderr, "%s", _("Must provide key for DHT GET!\n"));
203 if (NULL == (dht_handle = GNUNET_DHT_connect (cfg, 1)))
205 FPRINTF (stderr, "%s", _("Failed to connect to DHT service!\n"));
209 if (query_type == GNUNET_BLOCK_TYPE_ANY) /* Type of data not set */
210 query_type = GNUNET_BLOCK_TYPE_TEST;
211 GNUNET_CRYPTO_hash (query_key, strlen (query_key), &key);
213 FPRINTF (stderr, "%s `%s' \n",
214 _("Issueing DHT GET with key"),
215 GNUNET_h2s_full (&key));
216 GNUNET_SCHEDULER_add_shutdown (&cleanup_task, NULL);
217 tt = GNUNET_SCHEDULER_add_delayed (timeout_request,
221 GNUNET_DHT_get_start (dht_handle, query_type, &key, replication,
222 (demultixplex_everywhere) ? GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE : GNUNET_DHT_RO_NONE,
224 &get_result_iterator,
230 * Entry point for gnunet-dht-get
232 * @param argc number of arguments from the command line
233 * @param argv command line arguments
234 * @return 0 ok, 1 on error
237 main (int argc, char *const *argv)
239 struct GNUNET_GETOPT_CommandLineOption options[] = {
240 GNUNET_GETOPT_option_string ('k',
243 gettext_noop ("the query key"),
245 GNUNET_GETOPT_option_uint ('r',
248 gettext_noop ("how many parallel requests (replicas) to create"),
250 GNUNET_GETOPT_option_uint ('t',
253 gettext_noop ("the type of data to look for"),
255 GNUNET_GETOPT_option_relative_time ('T',
258 gettext_noop ("how long to execute this query before giving up?"),
260 GNUNET_GETOPT_option_flag ('x',
262 gettext_noop ("use DHT's demultiplex everywhere option"),
263 &demultixplex_everywhere),
264 GNUNET_GETOPT_option_verbose (&verbose),
265 GNUNET_GETOPT_OPTION_END
270 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
273 GNUNET_PROGRAM_run (argc, argv, "gnunet-dht-get",
275 ("Issue a GET request to the GNUnet DHT, prints results."),
276 options, &run, NULL)) ? ret : 1;
279 /* end of gnunet-dht-get.c */