2 This file is part of GNUnet.
3 (C) 2001, 2002, 2004, 2005, 2006, 2007, 2009 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 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 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
21 * @file dht/gnunet-dht-get.c
22 * @brief search for data in DHT
23 * @author Christian Grothoff
24 * @author Nathan Evans
27 #include "gnunet_dht_service.h"
30 * The type of the query
32 static unsigned int query_type;
35 * The key for the query
37 static char *query_key;
40 * User supplied timeout value (in seconds)
42 static unsigned long long timeout_request = 5;
45 * When this request should really die
47 struct GNUNET_TIME_Absolute absolute_timeout;
57 static struct GNUNET_DHT_Handle *dht_handle;
60 * Global handle of the scheduler
62 static struct GNUNET_SCHEDULER_Handle *sched;
65 * Global handle of the configuration
67 static const struct GNUNET_CONFIGURATION_Handle *cfg;
70 * Handle for the get request
72 static struct GNUNET_DHT_GetHandle *get_handle;
75 * Count of results found
77 static unsigned int result_count;
86 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
88 if (dht_handle != NULL)
90 GNUNET_DHT_disconnect (dht_handle);
97 cleanup_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
99 if (get_handle != NULL)
101 GNUNET_DHT_get_stop (get_handle);
104 GNUNET_SCHEDULER_add_now (sched, &shutdown_task, NULL);
109 * Iterator called on each result obtained for a DHT
110 * operation that expects a reply
113 * @param exp when will this value expire
114 * @param key key of the result
115 * @param get_path NULL-terminated array of pointers
116 * to the peers on reverse GET path (or NULL if not recorded)
117 * @param put_path NULL-terminated array of pointers
118 * to the peers on the PUT path (or NULL if not recorded)
119 * @param type type of the result
120 * @param size number of bytes in data
121 * @param data pointer to the result data
124 get_result_iterator (void *cls,
125 struct GNUNET_TIME_Absolute exp,
126 const GNUNET_HashCode * key,
127 const struct GNUNET_PeerIdentity * const *get_path,
128 const struct GNUNET_PeerIdentity * const *put_path,
129 enum GNUNET_BLOCK_Type type,
133 fprintf (stdout, "Result %d, type %d:\n%.*s\n", result_count, type, size,
140 * Main function that will be run by the scheduler.
143 * @param s the scheduler to use
144 * @param args remaining command-line arguments
145 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
146 * @param c configuration
150 struct GNUNET_SCHEDULER_Handle *s,
152 const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *c)
154 struct GNUNET_TIME_Relative timeout;
159 if (query_key == NULL)
162 fprintf (stderr, "Must provide key for DHT GET!\n");
167 dht_handle = GNUNET_DHT_connect (sched, cfg, 1);
169 if (dht_handle == NULL)
172 fprintf (stderr, "Couldn't connect to DHT service!\n");
177 fprintf (stderr, "Connected to DHT service!\n");
179 GNUNET_CRYPTO_hash (query_key, strlen (query_key), &key);
182 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, timeout_request);
183 absolute_timeout = GNUNET_TIME_relative_to_absolute (timeout);
186 fprintf (stderr, "Issuing GET request for %s!\n", query_key);
187 GNUNET_SCHEDULER_add_delayed (sched,
188 GNUNET_TIME_absolute_get_remaining
189 (absolute_timeout), &cleanup_task, NULL);
190 get_handle = GNUNET_DHT_get_start (dht_handle,
197 &get_result_iterator, NULL);
203 * gnunet-dht-get command line options
205 static struct GNUNET_GETOPT_CommandLineOption options[] = {
207 gettext_noop ("the query key"),
208 1, &GNUNET_GETOPT_set_string, &query_key},
209 {'t', "type", "TYPE",
210 gettext_noop ("the type of data to look for"),
211 1, &GNUNET_GETOPT_set_uint, &query_type},
212 {'T', "timeout", "TIMEOUT",
213 gettext_noop ("how long to execute this query before giving up?"),
214 1, &GNUNET_GETOPT_set_ulong, &timeout_request},
215 {'V', "verbose", NULL,
216 gettext_noop ("be verbose (print progress information)"),
217 0, &GNUNET_GETOPT_set_one, &verbose},
218 GNUNET_GETOPT_OPTION_END
223 * Entry point for gnunet-dht-get
225 * @param argc number of arguments from the command line
226 * @param argv command line arguments
227 * @return 0 ok, 1 on error
230 main (int argc, char *const *argv)
233 GNUNET_PROGRAM_run (argc,
237 ("Issue a GET request to the GNUnet DHT, prints results."),
238 options, &run, NULL)) ? ret : 1;
241 /* end of gnunet-dht-get.c */