diff options
author | Nathan S. Evans <evans@in.tum.de> | 2010-04-05 13:52:41 +0000 |
---|---|---|
committer | Nathan S. Evans <evans@in.tum.de> | 2010-04-05 13:52:41 +0000 |
commit | e4de5bb96aa6560bbd85ebca896e7cb36d426ad5 (patch) | |
tree | 658263e857a93978b669cc90438ed1c5521e188d /src/dht/gnunet-dht-get.c | |
parent | 2b5542569ff904ad595b40aa150823bfbd7bf39b (diff) | |
download | gnunet-e4de5bb96aa6560bbd85ebca896e7cb36d426ad5.tar.gz gnunet-e4de5bb96aa6560bbd85ebca896e7cb36d426ad5.zip |
add gnunet-dht-get and gnunet-dht-put binaries, changes to service
Diffstat (limited to 'src/dht/gnunet-dht-get.c')
-rw-r--r-- | src/dht/gnunet-dht-get.c | 244 |
1 files changed, 244 insertions, 0 deletions
diff --git a/src/dht/gnunet-dht-get.c b/src/dht/gnunet-dht-get.c new file mode 100644 index 000000000..de36610bd --- /dev/null +++ b/src/dht/gnunet-dht-get.c | |||
@@ -0,0 +1,244 @@ | |||
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 2, 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 | * Global handle of the scheduler | ||
61 | */ | ||
62 | static struct GNUNET_SCHEDULER_Handle *sched; | ||
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 | static void | ||
85 | shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) | ||
86 | { | ||
87 | |||
88 | if (dht_handle != NULL) | ||
89 | GNUNET_DHT_disconnect (dht_handle); | ||
90 | |||
91 | dht_handle = NULL; | ||
92 | } | ||
93 | |||
94 | static void | ||
95 | cleanup_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) | ||
96 | { | ||
97 | if (get_handle != NULL) | ||
98 | GNUNET_DHT_get_stop (get_handle, &shutdown_task, NULL); | ||
99 | else | ||
100 | GNUNET_SCHEDULER_add_now (sched, &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 type type of the result | ||
112 | * @param size number of bytes in data | ||
113 | * @param data pointer to the result data | ||
114 | */ | ||
115 | void | ||
116 | get_result_iterator (void *cls, | ||
117 | struct GNUNET_TIME_Absolute exp, | ||
118 | const GNUNET_HashCode * key, | ||
119 | uint32_t type, uint32_t size, const void *data) | ||
120 | { | ||
121 | fprintf (stdout, "Result %d, type %d:\n%.*s\n", result_count, type, size, | ||
122 | (char *) data); | ||
123 | result_count++; | ||
124 | } | ||
125 | |||
126 | /** | ||
127 | * Signature of the main function of a task. | ||
128 | * | ||
129 | * @param cls closure | ||
130 | * @param tc context information (why was this task triggered now) | ||
131 | */ | ||
132 | void | ||
133 | message_sent_cont (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) | ||
134 | { | ||
135 | if (tc->reason == GNUNET_SCHEDULER_REASON_TIMEOUT) | ||
136 | { | ||
137 | if (verbose) | ||
138 | fprintf (stderr, | ||
139 | "Failed to send GET request to service, quitting.\n"); | ||
140 | ret = 1; | ||
141 | GNUNET_SCHEDULER_add_now (sched, &shutdown_task, NULL); | ||
142 | } | ||
143 | else | ||
144 | { | ||
145 | if (verbose) | ||
146 | fprintf (stderr, "GET request sent, awaiting results!\n"); | ||
147 | GNUNET_SCHEDULER_add_delayed (sched, | ||
148 | GNUNET_TIME_absolute_get_remaining | ||
149 | (absolute_timeout), &cleanup_task, NULL); | ||
150 | } | ||
151 | } | ||
152 | |||
153 | /** | ||
154 | * Main function that will be run by the scheduler. | ||
155 | * | ||
156 | * @param cls closure | ||
157 | * @param s the scheduler to use | ||
158 | * @param args remaining command-line arguments | ||
159 | * @param cfgfile name of the configuration file used (for saving, can be NULL!) | ||
160 | * @param c configuration | ||
161 | */ | ||
162 | static void | ||
163 | run (void *cls, | ||
164 | struct GNUNET_SCHEDULER_Handle *s, | ||
165 | char *const *args, | ||
166 | const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *c) | ||
167 | { | ||
168 | struct GNUNET_TIME_Relative timeout; | ||
169 | GNUNET_HashCode key; | ||
170 | sched = s; | ||
171 | cfg = c; | ||
172 | |||
173 | if (query_key == NULL) | ||
174 | { | ||
175 | if (verbose) | ||
176 | fprintf (stderr, "Must provide key for DHT GET!\n"); | ||
177 | ret = 1; | ||
178 | return; | ||
179 | } | ||
180 | |||
181 | dht_handle = GNUNET_DHT_connect (sched, cfg, 1); | ||
182 | |||
183 | if (dht_handle == NULL) | ||
184 | { | ||
185 | if (verbose) | ||
186 | fprintf (stderr, "Couldn't connect to DHT service!\n"); | ||
187 | ret = 1; | ||
188 | return; | ||
189 | } | ||
190 | else if (verbose) | ||
191 | fprintf (stderr, "Connected to DHT service!\n"); | ||
192 | |||
193 | GNUNET_CRYPTO_hash (query_key, strlen (query_key), &key); | ||
194 | |||
195 | timeout = | ||
196 | GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, timeout_request); | ||
197 | absolute_timeout = GNUNET_TIME_relative_to_absolute (timeout); | ||
198 | |||
199 | if (verbose) | ||
200 | fprintf (stderr, "Issuing GET request for %s!\n", query_key); | ||
201 | GNUNET_DHT_get_start (dht_handle, timeout, query_type, &key, | ||
202 | &get_result_iterator, NULL, &message_sent_cont, NULL); | ||
203 | |||
204 | } | ||
205 | |||
206 | |||
207 | /** | ||
208 | * gnunet-dht-get command line options | ||
209 | */ | ||
210 | static struct GNUNET_GETOPT_CommandLineOption options[] = { | ||
211 | {'k', "key", "KEY", | ||
212 | gettext_noop ("the query key"), | ||
213 | 1, &GNUNET_GETOPT_set_string, &query_key}, | ||
214 | {'t', "type", "TYPE", | ||
215 | gettext_noop ("the type of data to look for"), | ||
216 | 0, &GNUNET_GETOPT_set_uint, &query_type}, | ||
217 | {'T', "timeout", "TIMEOUT", | ||
218 | gettext_noop ("how long to execute this query before giving up?"), | ||
219 | 0, &GNUNET_GETOPT_set_ulong, &timeout_request}, | ||
220 | {'V', "verbose", NULL, | ||
221 | gettext_noop ("be verbose (print progress information)"), | ||
222 | 0, &GNUNET_GETOPT_set_one, &verbose}, | ||
223 | GNUNET_GETOPT_OPTION_END | ||
224 | }; | ||
225 | |||
226 | |||
227 | /** | ||
228 | * Entry point for gnunet-dht-get | ||
229 | * | ||
230 | * @param argc number of arguments from the command line | ||
231 | * @param argv command line arguments | ||
232 | * @return 0 ok, 1 on error | ||
233 | */ | ||
234 | int | ||
235 | main (int argc, char *const *argv) | ||
236 | { | ||
237 | return (GNUNET_OK == | ||
238 | GNUNET_PROGRAM_run (argc, | ||
239 | argv, | ||
240 | "gnunet-dht-get", | ||
241 | gettext_noop | ||
242 | ("Issue a GET request to the GNUnet DHT, prints results."), | ||
243 | options, &run, NULL)) ? ret : 1; | ||
244 | } | ||