6ad4b30f8e05d0b84eb02425effc663037780b18
[oweals/gnunet.git] / src / dht / gnunet-dht-get.c
1 /*
2      This file is part of GNUnet.
3      (C) 2001, 2002, 2004, 2005, 2006, 2007, 2009 Christian Grothoff (and other contributing authors)
4
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.
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      General Public License for more details.
14
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.
19 */
20 /**
21  * @file dht/gnunet-dht-get.c
22  * @brief search for data in DHT
23  * @author Christian Grothoff
24  * @author Nathan Evans
25  */
26 #include "platform.h"
27 #include "gnunet_dht_service.h"
28
29 /**
30  * The type of the query
31  */
32 static unsigned int query_type;
33
34 /**
35  * Desired replication level
36  */
37 static unsigned int replication = 5;
38
39 /**
40  * The key for the query
41  */
42 static char *query_key;
43
44 /**
45  * User supplied timeout value (in seconds)
46  */
47 static unsigned long long timeout_request = 5;
48
49 /**
50  * When this request should really die
51  */
52 struct GNUNET_TIME_Absolute absolute_timeout;
53
54 /**
55  * Be verbose
56  */
57 static int verbose;
58
59 /**
60  * Handle to the DHT
61  */
62 static struct GNUNET_DHT_Handle *dht_handle;
63
64 /**
65  * Global handle of the configuration
66  */
67 static const struct GNUNET_CONFIGURATION_Handle *cfg;
68
69 /**
70  * Handle for the get request
71  */
72 static struct GNUNET_DHT_GetHandle *get_handle;
73
74 /**
75  * Count of results found
76  */
77 static unsigned int result_count;
78
79 /**
80  * Global status value
81  */
82 static int ret;
83
84
85 static void
86 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
87 {
88   if (dht_handle != NULL)
89   {
90     GNUNET_DHT_disconnect (dht_handle);
91     dht_handle = NULL;
92   }
93 }
94
95
96 static void
97 cleanup_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
98 {
99   if (get_handle != NULL)
100   {
101     GNUNET_DHT_get_stop (get_handle);
102     get_handle = NULL;
103   }
104   GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
105 }
106
107
108 /**
109  * Iterator called on each result obtained for a DHT
110  * operation that expects a reply
111  *
112  * @param cls closure
113  * @param exp when will this value expire
114  * @param key key of the result
115  * @param get_path peers on reply path (or NULL if not recorded)
116  * @param get_path_length number of entries in get_path
117  * @param put_path peers on the PUT path (or NULL if not recorded)
118  * @param put_path_length number of entries in get_path
119  * @param type type of the result
120  * @param size number of bytes in data
121  * @param data pointer to the result data
122  */
123 static void
124 get_result_iterator (void *cls, struct GNUNET_TIME_Absolute exp,
125                      const GNUNET_HashCode * key,
126                      const struct GNUNET_PeerIdentity *get_path,
127                      unsigned int get_path_length,
128                      const struct GNUNET_PeerIdentity *put_path,
129                      unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
130                      size_t size, const void *data)
131 {
132   FPRINTF (stdout, "Result %d, type %d:\n%.*s\n", result_count, type,
133            (unsigned int) size, (char *) data);
134   result_count++;
135 }
136
137
138 /**
139  * Main function that will be run by the scheduler.
140  *
141  * @param cls closure
142  * @param args remaining command-line arguments
143  * @param cfgfile name of the configuration file used (for saving, can be NULL!)
144  * @param c configuration
145  */
146 static void
147 run (void *cls, char *const *args, const char *cfgfile,
148      const struct GNUNET_CONFIGURATION_Handle *c)
149 {
150   struct GNUNET_TIME_Relative timeout;
151   GNUNET_HashCode key;
152
153   cfg = c;
154
155   if (query_key == NULL)
156   {
157     if (verbose)
158       FPRINTF (stderr, "%s",  "Must provide key for DHT GET!\n");
159     ret = 1;
160     return;
161   }
162
163   dht_handle = GNUNET_DHT_connect (cfg, 1);
164
165   if (dht_handle == NULL)
166   {
167     if (verbose)
168       FPRINTF (stderr, "%s",  "Couldn't connect to DHT service!\n");
169     ret = 1;
170     return;
171   }
172   else if (verbose)
173     FPRINTF (stderr, "%s",  "Connected to DHT service!\n");
174
175   if (query_type == GNUNET_BLOCK_TYPE_ANY)      /* Type of data not set */
176     query_type = GNUNET_BLOCK_TYPE_TEST;
177
178   GNUNET_CRYPTO_hash (query_key, strlen (query_key), &key);
179
180   timeout =
181       GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, timeout_request);
182   absolute_timeout = GNUNET_TIME_relative_to_absolute (timeout);
183
184   if (verbose)
185     FPRINTF (stderr, "Issuing GET request for %s!\n", query_key);
186   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_absolute_get_remaining
187                                 (absolute_timeout), &cleanup_task, NULL);
188   get_handle =
189       GNUNET_DHT_get_start (dht_handle, timeout, query_type, &key, replication,
190                             GNUNET_DHT_RO_NONE, NULL, 0, &get_result_iterator,
191                             NULL);
192
193 }
194
195
196 /**
197  * gnunet-dht-get command line options
198  */
199 static struct GNUNET_GETOPT_CommandLineOption options[] = {
200   {'k', "key", "KEY",
201    gettext_noop ("the query key"),
202    1, &GNUNET_GETOPT_set_string, &query_key},
203   {'r', "replication", "LEVEL",
204    gettext_noop ("how many parallel requests (replicas) to create"),
205    1, &GNUNET_GETOPT_set_uint, &replication},
206   {'t', "type", "TYPE",
207    gettext_noop ("the type of data to look for"),
208    1, &GNUNET_GETOPT_set_uint, &query_type},
209   {'T', "timeout", "TIMEOUT",
210    gettext_noop ("how long to execute this query before giving up?"),
211    1, &GNUNET_GETOPT_set_ulong, &timeout_request},
212   {'V', "verbose", NULL,
213    gettext_noop ("be verbose (print progress information)"),
214    0, &GNUNET_GETOPT_set_one, &verbose},
215   GNUNET_GETOPT_OPTION_END
216 };
217
218
219 /**
220  * Entry point for gnunet-dht-get
221  *
222  * @param argc number of arguments from the command line
223  * @param argv command line arguments
224  * @return 0 ok, 1 on error
225  */
226 int
227 main (int argc, char *const *argv)
228 {
229   return (GNUNET_OK ==
230           GNUNET_PROGRAM_run (argc, argv, "gnunet-dht-get",
231                               gettext_noop
232                               ("Issue a GET request to the GNUnet DHT, prints results."),
233                               options, &run, NULL)) ? ret : 1;
234 }
235
236 /* end of gnunet-dht-get.c */