More W32 resolver workarounds
[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
46  */
47 static struct GNUNET_TIME_Relative timeout_request = { 60000 };
48
49 /**
50  * Be verbose
51  */
52 static int verbose;
53
54 /**
55  * Use DHT demultixplex_everywhere
56  */
57 static int demultixplex_everywhere;
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 /**
86  * Task run to clean up on timeout.
87  *
88  * @param cls unused
89  * @param tc unused
90  */
91 static void
92 cleanup_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
93 {
94   if (NULL != get_handle)
95   {
96     GNUNET_DHT_get_stop (get_handle);
97     get_handle = NULL;
98   }
99   if (NULL != dht_handle)
100   {
101     GNUNET_DHT_disconnect (dht_handle);
102     dht_handle = NULL;
103   }
104 }
105
106
107 /**
108  * Iterator called on each result obtained for a DHT
109  * operation that expects a reply
110  *
111  * @param cls closure
112  * @param exp when will this value expire
113  * @param key key of the result
114  * @param get_path peers on reply path (or NULL if not recorded)
115  * @param get_path_length number of entries in get_path
116  * @param put_path peers on the PUT path (or NULL if not recorded)
117  * @param put_path_length number of entries in get_path
118  * @param type type of the result
119  * @param size number of bytes in data
120  * @param data pointer to the result data
121  */
122 static void
123 get_result_iterator (void *cls, struct GNUNET_TIME_Absolute exp,
124                      const struct GNUNET_HashCode * key,
125                      const struct GNUNET_PeerIdentity *get_path,
126                      unsigned int get_path_length,
127                      const struct GNUNET_PeerIdentity *put_path,
128                      unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
129                      size_t size, const void *data)
130 {
131   FPRINTF (stdout,
132            _("Result %d, type %d:\n%.*s\n"),
133            result_count, type,
134            (unsigned int) size, (char *) data);
135   result_count++;
136 }
137
138
139 /**
140  * Main function that will be run by the scheduler.
141  *
142  * @param cls closure
143  * @param args remaining command-line arguments
144  * @param cfgfile name of the configuration file used (for saving, can be NULL!)
145  * @param c configuration
146  */
147 static void
148 run (void *cls, char *const *args, const char *cfgfile,
149      const struct GNUNET_CONFIGURATION_Handle *c)
150 {
151   struct GNUNET_HashCode key;
152
153   cfg = c;
154   if (NULL == query_key)
155   {
156     FPRINTF (stderr, "%s",  _("Must provide key for DHT GET!\n"));
157     ret = 1;
158     return;
159   }
160   if (NULL == (dht_handle = GNUNET_DHT_connect (cfg, 1)))
161   {
162     FPRINTF (stderr, "%s",  _("Failed to connect to DHT service!\n"));
163     ret = 1;
164     return;
165   }
166   if (query_type == GNUNET_BLOCK_TYPE_ANY)      /* Type of data not set */
167     query_type = GNUNET_BLOCK_TYPE_TEST;
168   GNUNET_CRYPTO_hash (query_key, strlen (query_key), &key);
169   if (verbose)
170     FPRINTF (stderr, "%s `%s' \n",  _("Issueing DHT GET with key"), GNUNET_h2s_full (&key));
171   GNUNET_SCHEDULER_add_delayed (timeout_request,
172                                 &cleanup_task, NULL);
173   get_handle =
174       GNUNET_DHT_get_start (dht_handle, query_type, &key, replication,
175                             (demultixplex_everywhere) ? GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE : GNUNET_DHT_RO_NONE,
176                             NULL, 0, &get_result_iterator, NULL);
177
178 }
179
180
181 /**
182  * gnunet-dht-get command line options
183  */
184 static struct GNUNET_GETOPT_CommandLineOption options[] = {
185   {'k', "key", "KEY",
186    gettext_noop ("the query key"),
187    1, &GNUNET_GETOPT_set_string, &query_key},
188   {'r', "replication", "LEVEL",
189    gettext_noop ("how many parallel requests (replicas) to create"),
190    1, &GNUNET_GETOPT_set_uint, &replication},
191   {'t', "type", "TYPE",
192    gettext_noop ("the type of data to look for"),
193    1, &GNUNET_GETOPT_set_uint, &query_type},
194   {'T', "timeout", "TIMEOUT",
195    gettext_noop ("how long to execute this query before giving up?"),
196    1, &GNUNET_GETOPT_set_relative_time, &timeout_request},
197   {'x', "demultiplex", NULL,
198     gettext_noop ("use DHT's demultiplex everywhere option"),
199     0, &GNUNET_GETOPT_set_one, &demultixplex_everywhere},
200   {'V', "verbose", NULL,
201    gettext_noop ("be verbose (print progress information)"),
202    0, &GNUNET_GETOPT_set_one, &verbose},
203   GNUNET_GETOPT_OPTION_END
204 };
205
206
207 /**
208  * Entry point for gnunet-dht-get
209  *
210  * @param argc number of arguments from the command line
211  * @param argv command line arguments
212  * @return 0 ok, 1 on error
213  */
214 int
215 main (int argc, char *const *argv)
216 {
217   if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
218     return 2;
219   return (GNUNET_OK ==
220           GNUNET_PROGRAM_run (argc, argv, "gnunet-dht-get",
221                               gettext_noop
222                               ("Issue a GET request to the GNUnet DHT, prints results."),
223                               options, &run, NULL)) ? ret : 1;
224 }
225
226 /* end of gnunet-dht-get.c */