paragraph for gnunet devs that don't know how to use the web
[oweals/gnunet.git] / src / dht / gnunet-dht-get.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2001, 2002, 2004, 2005, 2006, 2007, 2009 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 /**
19  * @file dht/gnunet-dht-get.c
20  * @brief search for data in DHT
21  * @author Christian Grothoff
22  * @author Nathan Evans
23  */
24 #include "platform.h"
25 #include "gnunet_dht_service.h"
26
27 #define LOG(kind,...) GNUNET_log_from (kind, "dht-clients",__VA_ARGS__)
28 /**
29  * The type of the query
30  */
31 static unsigned int query_type;
32
33 /**
34  * Desired replication level
35  */
36 static unsigned int replication = 5;
37
38 /**
39  * The key for the query
40  */
41 static char *query_key;
42
43 /**
44  * User supplied timeout value
45  */
46 static struct GNUNET_TIME_Relative timeout_request = { 60000 };
47
48 /**
49  * Be verbose
50  */
51 static unsigned int verbose;
52
53 /**
54  * Use DHT demultixplex_everywhere
55  */
56 static int demultixplex_everywhere;
57
58 /**
59  * Handle to the DHT
60  */
61 static struct GNUNET_DHT_Handle *dht_handle;
62
63 /**
64  * Global handle of the configuration
65  */
66 static const struct GNUNET_CONFIGURATION_Handle *cfg;
67
68 /**
69  * Handle for the get request
70  */
71 static struct GNUNET_DHT_GetHandle *get_handle;
72
73 /**
74  * Count of results found
75  */
76 static unsigned int result_count;
77
78 /**
79  * Global status value
80  */
81 static int ret;
82
83 /**
84  * Task scheduled to handle timeout.
85  */
86 static struct GNUNET_SCHEDULER_Task *tt;
87
88
89 /**
90  * Task run to clean up on shutdown.
91  *
92  * @param cls unused
93  */
94 static void
95 cleanup_task (void *cls)
96 {
97   if (NULL != get_handle)
98   {
99     GNUNET_DHT_get_stop (get_handle);
100     get_handle = NULL;
101   }
102   if (NULL != dht_handle)
103   {
104     GNUNET_DHT_disconnect (dht_handle);
105     dht_handle = NULL;
106   }
107   if (NULL != tt)
108   {
109     GNUNET_SCHEDULER_cancel (tt);
110     tt = NULL;
111   }
112 }
113
114
115 /**
116  * Task run on timeout. Triggers shutdown.
117  *
118  * @param cls unused
119  */
120 static void
121 timeout_task (void *cls)
122 {
123   tt = NULL;
124   GNUNET_SCHEDULER_shutdown ();
125 }
126
127
128 /**
129  * Iterator called on each result obtained for a DHT
130  * operation that expects a reply
131  *
132  * @param cls closure
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
142  */
143 static void
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,
151                      size_t size,
152                      const void *data)
153 {
154   FPRINTF (stdout,
155            (GNUNET_BLOCK_TYPE_TEST == type)
156            ? _("Result %d, type %d:\n%.*s\n")
157            : _("Result %d, type %d:\n"),
158            result_count,
159            type,
160            (unsigned int) size,
161            (char *) data);
162   if (verbose)
163   {
164     FPRINTF (stdout,
165              "  GET path: ");
166     for (unsigned int i=0;i<get_path_length;i++)
167       FPRINTF (stdout,
168                "%s%s",
169                (0 == i) ? "" : "-",
170                GNUNET_i2s (&get_path[i]));
171     FPRINTF (stdout,
172              "\n  PUT path: ");
173     for (unsigned int i=0;i<put_path_length;i++)
174       FPRINTF (stdout,
175                "%s%s",
176                (0 == i) ? "" : "-",
177                GNUNET_i2s (&put_path[i]));
178     FPRINTF (stdout,
179              "\n");
180   }
181   result_count++;
182 }
183
184
185 /**
186  * Main function that will be run by the scheduler.
187  *
188  * @param cls closure
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
192  */
193 static void
194 run (void *cls, char *const *args, const char *cfgfile,
195      const struct GNUNET_CONFIGURATION_Handle *c)
196 {
197   struct GNUNET_HashCode key;
198
199   cfg = c;
200   if (NULL == query_key)
201   {
202     FPRINTF (stderr, "%s",  _("Must provide key for DHT GET!\n"));
203     ret = 1;
204     return;
205   }
206   if (NULL == (dht_handle = GNUNET_DHT_connect (cfg, 1)))
207   {
208     FPRINTF (stderr, "%s",  _("Failed to connect to DHT service!\n"));
209     ret = 1;
210     return;
211   }
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);
215   if (verbose)
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,
221                                      &timeout_task,
222                                      NULL);
223   get_handle =
224       GNUNET_DHT_get_start (dht_handle, query_type, &key, replication,
225                             (demultixplex_everywhere) ? GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE : GNUNET_DHT_RO_NONE,
226                             NULL, 0,
227                             &get_result_iterator,
228                             NULL);
229 }
230
231
232 /**
233  * Entry point for gnunet-dht-get
234  *
235  * @param argc number of arguments from the command line
236  * @param argv command line arguments
237  * @return 0 ok, 1 on error
238  */
239 int
240 main (int argc, char *const *argv)
241 {
242   struct GNUNET_GETOPT_CommandLineOption options[] = {
243     GNUNET_GETOPT_option_string ('k',
244                                  "key",
245                                  "KEY",
246                                  gettext_noop ("the query key"),
247                                  &query_key),
248     GNUNET_GETOPT_option_uint ('r',
249                                "replication",
250                                "LEVEL",
251                                gettext_noop ("how many parallel requests (replicas) to create"),
252                                &replication),
253     GNUNET_GETOPT_option_uint ('t',
254                                "type",
255                                "TYPE",
256                                gettext_noop ("the type of data to look for"),
257                                &query_type),
258     GNUNET_GETOPT_option_relative_time ('T',
259                                         "timeout",
260                                         "TIMEOUT",
261                                         gettext_noop ("how long to execute this query before giving up?"),
262                                         &timeout_request),
263     GNUNET_GETOPT_option_flag ('x',
264                                "demultiplex",
265                                gettext_noop ("use DHT's demultiplex everywhere option"),
266                                &demultixplex_everywhere),
267     GNUNET_GETOPT_option_verbose (&verbose),
268     GNUNET_GETOPT_OPTION_END
269   };
270
271
272
273   if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
274     return 2;
275   return (GNUNET_OK ==
276           GNUNET_PROGRAM_run (argc, argv, "gnunet-dht-get",
277                               gettext_noop
278                               ("Issue a GET request to the GNUnet DHT, prints results."),
279                               options, &run, NULL)) ? ret : 1;
280 }
281
282 /* end of gnunet-dht-get.c */