adding DEBUG logic for #3863
[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 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., 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 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 /**
87  * Task run to clean up on timeout.
88  *
89  * @param cls unused
90  * @param tc unused
91  */
92 static void
93 cleanup_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
94 {
95   if (NULL != get_handle)
96   {
97     GNUNET_DHT_get_stop (get_handle);
98     get_handle = NULL;
99   }
100   if (NULL != dht_handle)
101   {
102     GNUNET_DHT_disconnect (dht_handle);
103     dht_handle = NULL;
104   }
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 struct 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,
133            _("Result %d, type %d:\n%.*s\n"),
134            result_count, type,
135            (unsigned int) size, (char *) data);
136   result_count++;
137 }
138
139
140 /**
141  * Main function that will be run by the scheduler.
142  *
143  * @param cls closure
144  * @param args remaining command-line arguments
145  * @param cfgfile name of the configuration file used (for saving, can be NULL!)
146  * @param c configuration
147  */
148 static void
149 run (void *cls, char *const *args, const char *cfgfile,
150      const struct GNUNET_CONFIGURATION_Handle *c)
151 {
152   struct GNUNET_HashCode key;
153
154
155
156   cfg = c;
157   if (NULL == query_key)
158   {
159     FPRINTF (stderr, "%s",  _("Must provide key for DHT GET!\n"));
160     ret = 1;
161     return;
162   }
163   if (NULL == (dht_handle = GNUNET_DHT_connect (cfg, 1)))
164   {
165     FPRINTF (stderr, "%s",  _("Failed to connect to DHT service!\n"));
166     ret = 1;
167     return;
168   }
169   if (query_type == GNUNET_BLOCK_TYPE_ANY)      /* Type of data not set */
170     query_type = GNUNET_BLOCK_TYPE_TEST;
171   GNUNET_CRYPTO_hash (query_key, strlen (query_key), &key);
172   if (verbose)
173     FPRINTF (stderr, "%s `%s' \n",  _("Issueing DHT GET with key"), GNUNET_h2s_full (&key));
174   GNUNET_SCHEDULER_add_delayed (timeout_request,
175                                 &cleanup_task, NULL);
176   get_handle =
177       GNUNET_DHT_get_start (dht_handle, query_type, &key, replication,
178                             (demultixplex_everywhere) ? GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE : GNUNET_DHT_RO_NONE,
179                             NULL, 0, &get_result_iterator, NULL);
180
181 }
182
183
184 /**
185  * gnunet-dht-get command line options
186  */
187 static struct GNUNET_GETOPT_CommandLineOption options[] = {
188   {'k', "key", "KEY",
189    gettext_noop ("the query key"),
190    1, &GNUNET_GETOPT_set_string, &query_key},
191   {'r', "replication", "LEVEL",
192    gettext_noop ("how many parallel requests (replicas) to create"),
193    1, &GNUNET_GETOPT_set_uint, &replication},
194   {'t', "type", "TYPE",
195    gettext_noop ("the type of data to look for"),
196    1, &GNUNET_GETOPT_set_uint, &query_type},
197   {'T', "timeout", "TIMEOUT",
198    gettext_noop ("how long to execute this query before giving up?"),
199    1, &GNUNET_GETOPT_set_relative_time, &timeout_request},
200   {'x', "demultiplex", NULL,
201     gettext_noop ("use DHT's demultiplex everywhere option"),
202     0, &GNUNET_GETOPT_set_one, &demultixplex_everywhere},
203   {'V', "verbose", NULL,
204    gettext_noop ("be verbose (print progress information)"),
205    0, &GNUNET_GETOPT_set_one, &verbose},
206   GNUNET_GETOPT_OPTION_END
207 };
208
209
210 /**
211  * Entry point for gnunet-dht-get
212  *
213  * @param argc number of arguments from the command line
214  * @param argv command line arguments
215  * @return 0 ok, 1 on error
216  */
217 int
218 main (int argc, char *const *argv)
219 {
220   if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
221     return 2;
222   return (GNUNET_OK ==
223           GNUNET_PROGRAM_run (argc, argv, "gnunet-dht-get",
224                               gettext_noop
225                               ("Issue a GET request to the GNUnet DHT, prints results."),
226                               options, &run, NULL)) ? ret : 1;
227 }
228
229 /* end of gnunet-dht-get.c */