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