commented out wrong message type
[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
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., 51 Franklin Street, Fifth Floor,
18      Boston, MA 02110-1301, 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 #define LOG(kind,...) GNUNET_log_from (kind, "dht-clients",__VA_ARGS__)
30 /**
31  * The type of the query
32  */
33 static unsigned int query_type;
34
35 /**
36  * Desired replication level
37  */
38 static unsigned int replication = 5;
39
40 /**
41  * The key for the query
42  */
43 static char *query_key;
44
45 /**
46  * User supplied timeout value
47  */
48 static struct GNUNET_TIME_Relative timeout_request = { 60000 };
49
50 /**
51  * Be verbose
52  */
53 static unsigned int verbose;
54
55 /**
56  * Use DHT demultixplex_everywhere
57  */
58 static int demultixplex_everywhere;
59
60 /**
61  * Handle to the DHT
62  */
63 static struct GNUNET_DHT_Handle *dht_handle;
64
65 /**
66  * Global handle of the configuration
67  */
68 static const struct GNUNET_CONFIGURATION_Handle *cfg;
69
70 /**
71  * Handle for the get request
72  */
73 static struct GNUNET_DHT_GetHandle *get_handle;
74
75 /**
76  * Count of results found
77  */
78 static unsigned int result_count;
79
80 /**
81  * Global status value
82  */
83 static int ret;
84
85 /**
86  * Task scheduled to handle timeout.
87  */
88 static struct GNUNET_SCHEDULER_Task *tt;
89
90
91 /**
92  * Task run to clean up on shutdown.
93  *
94  * @param cls unused
95  */
96 static void
97 cleanup_task (void *cls)
98 {
99   if (NULL != get_handle)
100   {
101     GNUNET_DHT_get_stop (get_handle);
102     get_handle = NULL;
103   }
104   if (NULL != dht_handle)
105   {
106     GNUNET_DHT_disconnect (dht_handle);
107     dht_handle = NULL;
108   }
109   if (NULL != tt)
110   {
111     GNUNET_SCHEDULER_cancel (tt);
112     tt = NULL;
113   }
114 }
115
116
117 /**
118  * Task run on timeout. Triggers shutdown.
119  *
120  * @param cls unused
121  */
122 static void
123 timeout_task (void *cls)
124 {
125   tt = NULL;
126   GNUNET_SCHEDULER_shutdown ();
127 }
128
129
130 /**
131  * Iterator called on each result obtained for a DHT
132  * operation that expects a reply
133  *
134  * @param cls closure
135  * @param exp when will this value expire
136  * @param key key of the result
137  * @param get_path peers on reply path (or NULL if not recorded)
138  * @param get_path_length number of entries in get_path
139  * @param put_path peers on the PUT path (or NULL if not recorded)
140  * @param put_path_length number of entries in get_path
141  * @param type type of the result
142  * @param size number of bytes in data
143  * @param data pointer to the result data
144  */
145 static void
146 get_result_iterator (void *cls, struct GNUNET_TIME_Absolute exp,
147                      const struct GNUNET_HashCode * key,
148                      const struct GNUNET_PeerIdentity *get_path,
149                      unsigned int get_path_length,
150                      const struct GNUNET_PeerIdentity *put_path,
151                      unsigned int put_path_length,
152                      enum GNUNET_BLOCK_Type type,
153                      size_t size,
154                      const void *data)
155 {
156   FPRINTF (stdout,
157            _("Result %d, type %d:\n%.*s\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
200
201   cfg = c;
202   if (NULL == query_key)
203   {
204     FPRINTF (stderr, "%s",  _("Must provide key for DHT GET!\n"));
205     ret = 1;
206     return;
207   }
208   if (NULL == (dht_handle = GNUNET_DHT_connect (cfg, 1)))
209   {
210     FPRINTF (stderr, "%s",  _("Failed to connect to DHT service!\n"));
211     ret = 1;
212     return;
213   }
214   if (query_type == GNUNET_BLOCK_TYPE_ANY)      /* Type of data not set */
215     query_type = GNUNET_BLOCK_TYPE_TEST;
216   GNUNET_CRYPTO_hash (query_key, strlen (query_key), &key);
217   if (verbose)
218     FPRINTF (stderr, "%s `%s' \n",  _("Issueing DHT GET with key"), GNUNET_h2s_full (&key));
219   GNUNET_SCHEDULER_add_shutdown (&cleanup_task, NULL);
220   tt = GNUNET_SCHEDULER_add_delayed (timeout_request,
221                                      &timeout_task, NULL);
222   get_handle =
223       GNUNET_DHT_get_start (dht_handle, query_type, &key, replication,
224                             (demultixplex_everywhere) ? GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE : GNUNET_DHT_RO_NONE,
225                             NULL, 0, &get_result_iterator, NULL);
226
227 }
228
229 /**
230  * Entry point for gnunet-dht-get
231  *
232  * @param argc number of arguments from the command line
233  * @param argv command line arguments
234  * @return 0 ok, 1 on error
235  */
236 int
237 main (int argc, char *const *argv)
238 {
239
240   struct GNUNET_GETOPT_CommandLineOption options[] = {
241   
242     GNUNET_GETOPT_option_string ('k',
243                                  "key",
244                                  "KEY",
245                                  gettext_noop ("the query key"),
246                                  &query_key),
247   
248     GNUNET_GETOPT_option_uint ('r',
249                                    "replication",
250                                    "LEVEL",
251                                    gettext_noop ("how many parallel requests (replicas) to create"),
252                                    &replication),
253   
254   
255     GNUNET_GETOPT_option_uint ('t',
256                                    "type",
257                                    "TYPE",
258                                    gettext_noop ("the type of data to look for"),
259                                    &query_type),
260   
261     GNUNET_GETOPT_option_relative_time ('T',
262                                             "timeout",
263                                             "TIMEOUT",
264                                             gettext_noop ("how long to execute this query before giving up?"),
265                                             &timeout_request),
266   
267     GNUNET_GETOPT_option_flag ('x',
268                                   "demultiplex",
269                                   gettext_noop ("use DHT's demultiplex everywhere option"),
270                                   &demultixplex_everywhere),
271   
272     GNUNET_GETOPT_option_verbose (&verbose),
273     GNUNET_GETOPT_OPTION_END
274   };
275
276
277
278   if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
279     return 2;
280   return (GNUNET_OK ==
281           GNUNET_PROGRAM_run (argc, argv, "gnunet-dht-get",
282                               gettext_noop
283                               ("Issue a GET request to the GNUnet DHT, prints results."),
284                               options, &run, NULL)) ? ret : 1;
285 }
286
287 /* end of gnunet-dht-get.c */