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.
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/>.
19 * @file dht/gnunet-dht-get.c
20 * @brief search for data in DHT
21 * @author Christian Grothoff
22 * @author Nathan Evans
25 #include "gnunet_dht_service.h"
27 #define LOG(kind,...) GNUNET_log_from (kind, "dht-clients",__VA_ARGS__)
29 * The type of the query
31 static unsigned int query_type;
34 * Desired replication level
36 static unsigned int replication = 5;
39 * The key for the query
41 static char *query_key;
44 * User supplied timeout value
46 static struct GNUNET_TIME_Relative timeout_request = { 60000 };
51 static unsigned int verbose;
54 * Use DHT demultixplex_everywhere
56 static int demultixplex_everywhere;
61 static struct GNUNET_DHT_Handle *dht_handle;
64 * Global handle of the configuration
66 static const struct GNUNET_CONFIGURATION_Handle *cfg;
69 * Handle for the get request
71 static struct GNUNET_DHT_GetHandle *get_handle;
74 * Count of results found
76 static unsigned int result_count;
84 * Task scheduled to handle timeout.
86 static struct GNUNET_SCHEDULER_Task *tt;
90 * Task run to clean up on shutdown.
95 cleanup_task (void *cls)
97 if (NULL != get_handle)
99 GNUNET_DHT_get_stop (get_handle);
102 if (NULL != dht_handle)
104 GNUNET_DHT_disconnect (dht_handle);
109 GNUNET_SCHEDULER_cancel (tt);
116 * Task run on timeout. Triggers shutdown.
121 timeout_task (void *cls)
124 GNUNET_SCHEDULER_shutdown ();
129 * Iterator called on each result obtained for a DHT
130 * operation that expects a reply
133 * @param exp when will this value expire
134 * @param key key of the result
135 * @param get_path peers on reply path (or NULL if not recorded)
136 * @param get_path_length number of entries in get_path
137 * @param put_path peers on the PUT path (or NULL if not recorded)
138 * @param put_path_length number of entries in get_path
139 * @param type type of the result
140 * @param size number of bytes in data
141 * @param data pointer to the result data
144 get_result_iterator (void *cls, struct GNUNET_TIME_Absolute exp,
145 const struct GNUNET_HashCode * key,
146 const struct GNUNET_PeerIdentity *get_path,
147 unsigned int get_path_length,
148 const struct GNUNET_PeerIdentity *put_path,
149 unsigned int put_path_length,
150 enum GNUNET_BLOCK_Type type,
155 (GNUNET_BLOCK_TYPE_TEST == type)
156 ? _("Result %d, type %d:\n%.*s\n")
157 : _("Result %d, type %d:\n"),
166 for (unsigned int i=0;i<get_path_length;i++)
170 GNUNET_i2s (&get_path[i]));
173 for (unsigned int i=0;i<put_path_length;i++)
177 GNUNET_i2s (&put_path[i]));
186 * Main function that will be run by the scheduler.
189 * @param args remaining command-line arguments
190 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
191 * @param c configuration
194 run (void *cls, char *const *args, const char *cfgfile,
195 const struct GNUNET_CONFIGURATION_Handle *c)
197 struct GNUNET_HashCode key;
200 if (NULL == query_key)
202 FPRINTF (stderr, "%s", _("Must provide key for DHT GET!\n"));
206 if (NULL == (dht_handle = GNUNET_DHT_connect (cfg, 1)))
208 FPRINTF (stderr, "%s", _("Failed to connect to DHT service!\n"));
212 if (query_type == GNUNET_BLOCK_TYPE_ANY) /* Type of data not set */
213 query_type = GNUNET_BLOCK_TYPE_TEST;
214 GNUNET_CRYPTO_hash (query_key, strlen (query_key), &key);
216 FPRINTF (stderr, "%s `%s' \n",
217 _("Issueing DHT GET with key"),
218 GNUNET_h2s_full (&key));
219 GNUNET_SCHEDULER_add_shutdown (&cleanup_task, NULL);
220 tt = GNUNET_SCHEDULER_add_delayed (timeout_request,
224 GNUNET_DHT_get_start (dht_handle, query_type, &key, replication,
225 (demultixplex_everywhere) ? GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE : GNUNET_DHT_RO_NONE,
227 &get_result_iterator,
233 * Entry point for gnunet-dht-get
235 * @param argc number of arguments from the command line
236 * @param argv command line arguments
237 * @return 0 ok, 1 on error
240 main (int argc, char *const *argv)
242 struct GNUNET_GETOPT_CommandLineOption options[] = {
243 GNUNET_GETOPT_option_string ('k',
246 gettext_noop ("the query key"),
248 GNUNET_GETOPT_option_uint ('r',
251 gettext_noop ("how many parallel requests (replicas) to create"),
253 GNUNET_GETOPT_option_uint ('t',
256 gettext_noop ("the type of data to look for"),
258 GNUNET_GETOPT_option_relative_time ('T',
261 gettext_noop ("how long to execute this query before giving up?"),
263 GNUNET_GETOPT_option_flag ('x',
265 gettext_noop ("use DHT's demultiplex everywhere option"),
266 &demultixplex_everywhere),
267 GNUNET_GETOPT_option_verbose (&verbose),
268 GNUNET_GETOPT_OPTION_END
273 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
276 GNUNET_PROGRAM_run (argc, argv, "gnunet-dht-get",
278 ("Issue a GET request to the GNUnet DHT, prints results."),
279 options, &run, NULL)) ? ret : 1;
282 /* end of gnunet-dht-get.c */