diff options
author | Martin Schanzenbach <mschanzenbach@posteo.de> | 2012-07-14 23:57:15 +0000 |
---|---|---|
committer | Martin Schanzenbach <mschanzenbach@posteo.de> | 2012-07-14 23:57:15 +0000 |
commit | 3639c18ee5aca5f44adb104f82253d4a6703e652 (patch) | |
tree | f5ddc5e02ce38613bff02aba6336d0611d201fcf /src/gns/test_gns_proxy.c | |
parent | 1827cb6b6fa660202190da5e5ee75d2ed336ee48 (diff) | |
download | gnunet-3639c18ee5aca5f44adb104f82253d4a6703e652.tar.gz gnunet-3639c18ee5aca5f44adb104f82253d4a6703e652.zip |
-silly
Diffstat (limited to 'src/gns/test_gns_proxy.c')
-rw-r--r-- | src/gns/test_gns_proxy.c | 447 |
1 files changed, 447 insertions, 0 deletions
diff --git a/src/gns/test_gns_proxy.c b/src/gns/test_gns_proxy.c new file mode 100644 index 000000000..916ea7392 --- /dev/null +++ b/src/gns/test_gns_proxy.c | |||
@@ -0,0 +1,447 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet | ||
3 | (C) 2007, 2009, 2011, 2012 Christian Grothoff | ||
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 | /** | ||
22 | * @file test_gns_proxy.c | ||
23 | * @brief testcase for accessing SOCKS5 GNS proxy | ||
24 | * @author Martin Schanzenbach | ||
25 | */ | ||
26 | #include "platform.h" | ||
27 | #include <curl/curl.h> | ||
28 | #include <microhttpd.h> | ||
29 | #include "gnunet_namestore_service.h" | ||
30 | #include "gnunet_gns_service.h" | ||
31 | #include "gnunet_testing_lib-new.h" | ||
32 | #include "gnunet_os_lib.h" | ||
33 | |||
34 | #define PORT 8080 | ||
35 | #define TEST_DOMAIN "www.gnunet" | ||
36 | |||
37 | #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 300) | ||
38 | |||
39 | /** | ||
40 | * Return value for 'main'. | ||
41 | */ | ||
42 | static int global_ret; | ||
43 | |||
44 | static struct GNUNET_NAMESTORE_Handle *namestore; | ||
45 | |||
46 | static struct MHD_Daemon *mhd; | ||
47 | |||
48 | static GNUNET_SCHEDULER_TaskIdentifier mhd_task_id; | ||
49 | |||
50 | static GNUNET_SCHEDULER_TaskIdentifier curl_task_id; | ||
51 | |||
52 | static CURL *curl; | ||
53 | |||
54 | static CURLM *multi; | ||
55 | |||
56 | static char *url; | ||
57 | |||
58 | static struct GNUNET_OS_Process *proxy_proc; | ||
59 | |||
60 | static char* tmp_cfgfile; | ||
61 | |||
62 | struct CBC | ||
63 | { | ||
64 | char buf[1024]; | ||
65 | size_t pos; | ||
66 | }; | ||
67 | |||
68 | static struct CBC cbc; | ||
69 | |||
70 | |||
71 | static size_t | ||
72 | copy_buffer (void *ptr, size_t size, size_t nmemb, void *ctx) | ||
73 | { | ||
74 | struct CBC *cbc = ctx; | ||
75 | |||
76 | if (cbc->pos + size * nmemb > sizeof(cbc->buf)) | ||
77 | return 0; /* overflow */ | ||
78 | memcpy (&cbc->buf[cbc->pos], ptr, size * nmemb); | ||
79 | cbc->pos += size * nmemb; | ||
80 | return size * nmemb; | ||
81 | } | ||
82 | |||
83 | |||
84 | static int | ||
85 | mhd_ahc (void *cls, | ||
86 | struct MHD_Connection *connection, | ||
87 | const char *url, | ||
88 | const char *method, | ||
89 | const char *version, | ||
90 | const char *upload_data, size_t *upload_data_size, | ||
91 | void **unused) | ||
92 | { | ||
93 | static int ptr; | ||
94 | struct MHD_Response *response; | ||
95 | int ret; | ||
96 | |||
97 | if (0 != strcmp ("GET", method)) | ||
98 | return MHD_NO; /* unexpected method */ | ||
99 | if (&ptr != *unused) | ||
100 | { | ||
101 | *unused = &ptr; | ||
102 | return MHD_YES; | ||
103 | } | ||
104 | *unused = NULL; | ||
105 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MHD sends respose for request to URL `%s'\n", url); | ||
106 | response = MHD_create_response_from_buffer (strlen (url), | ||
107 | (void *) url, | ||
108 | MHD_RESPMEM_MUST_COPY); | ||
109 | ret = MHD_queue_response (connection, MHD_HTTP_OK, response); | ||
110 | MHD_destroy_response (response); | ||
111 | if (ret == MHD_NO) | ||
112 | abort (); | ||
113 | return ret; | ||
114 | } | ||
115 | |||
116 | |||
117 | static void | ||
118 | do_shutdown () | ||
119 | { | ||
120 | if (mhd_task_id != GNUNET_SCHEDULER_NO_TASK) | ||
121 | { | ||
122 | GNUNET_SCHEDULER_cancel (mhd_task_id); | ||
123 | mhd_task_id = GNUNET_SCHEDULER_NO_TASK; | ||
124 | } | ||
125 | if (curl_task_id != GNUNET_SCHEDULER_NO_TASK) | ||
126 | { | ||
127 | GNUNET_SCHEDULER_cancel (curl_task_id); | ||
128 | curl_task_id = GNUNET_SCHEDULER_NO_TASK; | ||
129 | } | ||
130 | if (NULL != mhd) | ||
131 | { | ||
132 | MHD_stop_daemon (mhd); | ||
133 | mhd = NULL; | ||
134 | } | ||
135 | GNUNET_free_non_null (url); | ||
136 | |||
137 | if (NULL != tmp_cfgfile) | ||
138 | remove (tmp_cfgfile); | ||
139 | |||
140 | if (NULL != proxy_proc) | ||
141 | GNUNET_OS_process_kill (proxy_proc, 9); | ||
142 | url = NULL; | ||
143 | } | ||
144 | |||
145 | |||
146 | /** | ||
147 | * Function to run the HTTP client. | ||
148 | */ | ||
149 | static void | ||
150 | curl_main (void); | ||
151 | |||
152 | |||
153 | static void | ||
154 | curl_task (void *cls, | ||
155 | const struct GNUNET_SCHEDULER_TaskContext *tc) | ||
156 | { | ||
157 | curl_task_id = GNUNET_SCHEDULER_NO_TASK; | ||
158 | curl_main (); | ||
159 | } | ||
160 | |||
161 | |||
162 | static void | ||
163 | curl_main () | ||
164 | { | ||
165 | fd_set rs; | ||
166 | fd_set ws; | ||
167 | fd_set es; | ||
168 | int max; | ||
169 | struct GNUNET_NETWORK_FDSet nrs; | ||
170 | struct GNUNET_NETWORK_FDSet nws; | ||
171 | struct GNUNET_TIME_Relative delay; | ||
172 | long timeout; | ||
173 | int running; | ||
174 | struct CURLMsg *msg; | ||
175 | |||
176 | max = 0; | ||
177 | FD_ZERO (&rs); | ||
178 | FD_ZERO (&ws); | ||
179 | FD_ZERO (&es); | ||
180 | curl_multi_perform (multi, &running); | ||
181 | if (running == 0) | ||
182 | { | ||
183 | GNUNET_assert (NULL != (msg = curl_multi_info_read (multi, &running))); | ||
184 | if (msg->msg == CURLMSG_DONE) | ||
185 | { | ||
186 | if (msg->data.result != CURLE_OK) | ||
187 | { | ||
188 | fprintf (stderr, | ||
189 | "%s failed at %s:%d: `%s'\n", | ||
190 | "curl_multi_perform", | ||
191 | __FILE__, | ||
192 | __LINE__, curl_easy_strerror (msg->data.result)); | ||
193 | global_ret = 1; | ||
194 | } | ||
195 | } | ||
196 | curl_multi_remove_handle (multi, curl); | ||
197 | curl_multi_cleanup (multi); | ||
198 | curl_easy_cleanup (curl); | ||
199 | curl = NULL; | ||
200 | multi = NULL; | ||
201 | if (cbc.pos != strlen ("/hello_world")) | ||
202 | { | ||
203 | GNUNET_break (0); | ||
204 | global_ret = 2; | ||
205 | } | ||
206 | if (0 != strncmp ("/hello_world", cbc.buf, strlen ("/hello_world"))) | ||
207 | { | ||
208 | GNUNET_break (0); | ||
209 | global_ret = 3; | ||
210 | } | ||
211 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Download complete, shutting down!\n"); | ||
212 | do_shutdown (); | ||
213 | return; | ||
214 | } | ||
215 | GNUNET_assert (CURLM_OK == curl_multi_fdset (multi, &rs, &ws, &es, &max)); | ||
216 | if ( (CURLM_OK != curl_multi_timeout (multi, &timeout)) || | ||
217 | (-1 == timeout) ) | ||
218 | delay = GNUNET_TIME_UNIT_SECONDS; | ||
219 | else | ||
220 | delay = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, (unsigned int) timeout); | ||
221 | GNUNET_NETWORK_fdset_copy_native (&nrs, | ||
222 | &rs, | ||
223 | max + 1); | ||
224 | GNUNET_NETWORK_fdset_copy_native (&nws, | ||
225 | &ws, | ||
226 | max + 1); | ||
227 | curl_task_id = GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT, | ||
228 | delay, | ||
229 | &nrs, | ||
230 | &nws, | ||
231 | &curl_task, | ||
232 | NULL); | ||
233 | } | ||
234 | |||
235 | static void | ||
236 | start_curl (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) | ||
237 | { | ||
238 | GNUNET_asprintf (&url, | ||
239 | "http://%s:%d/hello_world", | ||
240 | TEST_DOMAIN, PORT); | ||
241 | curl = curl_easy_init (); | ||
242 | curl_easy_setopt (curl, CURLOPT_URL, url); | ||
243 | curl_easy_setopt (curl, CURLOPT_WRITEFUNCTION, ©_buffer); | ||
244 | curl_easy_setopt (curl, CURLOPT_WRITEDATA, &cbc); | ||
245 | curl_easy_setopt (curl, CURLOPT_FAILONERROR, 1); | ||
246 | curl_easy_setopt (curl, CURLOPT_TIMEOUT, 150L); | ||
247 | curl_easy_setopt (curl, CURLOPT_CONNECTTIMEOUT, 15L); | ||
248 | curl_easy_setopt (curl, CURLOPT_NOSIGNAL, 1); | ||
249 | curl_easy_setopt (curl, CURLOPT_PROXY, "socks5h://127.0.0.1:7777"); | ||
250 | |||
251 | multi = curl_multi_init (); | ||
252 | GNUNET_assert (multi != NULL); | ||
253 | GNUNET_assert (CURLM_OK == curl_multi_add_handle (multi, curl)); | ||
254 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Beginning HTTP download from `%s'\n", url); | ||
255 | curl_main (); | ||
256 | } | ||
257 | |||
258 | static void | ||
259 | disco_ns (void* cls, const struct GNUNET_SCHEDULER_TaskContext *tc) | ||
260 | { | ||
261 | GNUNET_NAMESTORE_disconnect (namestore); | ||
262 | } | ||
263 | |||
264 | /** | ||
265 | * Callback invoked from the namestore service once record is | ||
266 | * created. | ||
267 | * | ||
268 | * @param cls closure | ||
269 | * @param af address family, AF_INET or AF_INET6; AF_UNSPEC on error; | ||
270 | * will match 'result_af' from the request | ||
271 | * @param address IP address (struct in_addr or struct in_addr6, depending on 'af') | ||
272 | * that the VPN allocated for the redirection; | ||
273 | * traffic to this IP will now be redirected to the | ||
274 | * specified target peer; NULL on error | ||
275 | */ | ||
276 | static void | ||
277 | commence_testing (void *cls, int32_t success, const char *emsg) | ||
278 | { | ||
279 | GNUNET_SCHEDULER_add_now (&disco_ns, NULL); | ||
280 | |||
281 | if ((emsg != NULL) && (GNUNET_YES != success)) | ||
282 | { | ||
283 | fprintf (stderr, | ||
284 | "NS failed to create record %s\n", emsg); | ||
285 | GNUNET_SCHEDULER_shutdown (); | ||
286 | return; | ||
287 | } | ||
288 | |||
289 | GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10), start_curl, NULL); | ||
290 | |||
291 | } | ||
292 | |||
293 | |||
294 | |||
295 | |||
296 | /** | ||
297 | * Function to keep the HTTP server running. | ||
298 | */ | ||
299 | static void | ||
300 | mhd_main (void); | ||
301 | |||
302 | |||
303 | static void | ||
304 | mhd_task (void *cls, | ||
305 | const struct GNUNET_SCHEDULER_TaskContext *tc) | ||
306 | { | ||
307 | mhd_task_id = GNUNET_SCHEDULER_NO_TASK; | ||
308 | MHD_run (mhd); | ||
309 | mhd_main (); | ||
310 | } | ||
311 | |||
312 | |||
313 | static void | ||
314 | mhd_main () | ||
315 | { | ||
316 | struct GNUNET_NETWORK_FDSet nrs; | ||
317 | struct GNUNET_NETWORK_FDSet nws; | ||
318 | fd_set rs; | ||
319 | fd_set ws; | ||
320 | fd_set es; | ||
321 | int max_fd; | ||
322 | unsigned MHD_LONG_LONG timeout; | ||
323 | struct GNUNET_TIME_Relative delay; | ||
324 | |||
325 | GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == mhd_task_id); | ||
326 | FD_ZERO (&rs); | ||
327 | FD_ZERO (&ws); | ||
328 | FD_ZERO (&es); | ||
329 | max_fd = -1; | ||
330 | GNUNET_assert (MHD_YES == | ||
331 | MHD_get_fdset (mhd, &rs, &ws, &es, &max_fd)); | ||
332 | if (MHD_YES == MHD_get_timeout (mhd, &timeout)) | ||
333 | delay = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, | ||
334 | (unsigned int) timeout); | ||
335 | else | ||
336 | delay = GNUNET_TIME_UNIT_FOREVER_REL; | ||
337 | GNUNET_NETWORK_fdset_copy_native (&nrs, | ||
338 | &rs, | ||
339 | max_fd + 1); | ||
340 | GNUNET_NETWORK_fdset_copy_native (&nws, | ||
341 | &ws, | ||
342 | max_fd + 1); | ||
343 | mhd_task_id = GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT, | ||
344 | delay, | ||
345 | &nrs, | ||
346 | &nws, | ||
347 | &mhd_task, | ||
348 | NULL); | ||
349 | } | ||
350 | |||
351 | static void | ||
352 | run (void *cls, | ||
353 | const struct GNUNET_CONFIGURATION_Handle *cfg, | ||
354 | struct GNUNET_TESTING_Peer *peer) | ||
355 | { | ||
356 | enum MHD_FLAG flags; | ||
357 | struct GNUNET_CRYPTO_RsaPrivateKey *host_key; | ||
358 | struct GNUNET_NAMESTORE_RecordData rd; | ||
359 | char *zone_keyfile; | ||
360 | |||
361 | namestore = GNUNET_NAMESTORE_connect (cfg); | ||
362 | GNUNET_assert (NULL != namestore); | ||
363 | flags = MHD_USE_DEBUG; | ||
364 | mhd = MHD_start_daemon (flags, | ||
365 | PORT, | ||
366 | NULL, NULL, | ||
367 | &mhd_ahc, NULL, | ||
368 | MHD_OPTION_END); | ||
369 | GNUNET_assert (NULL != mhd); | ||
370 | mhd_main (); | ||
371 | |||
372 | tmp_cfgfile = GNUNET_DISK_mktemp ("test_gns_proxy_tmp.conf"); | ||
373 | if (NULL == tmp_cfgfile) | ||
374 | { | ||
375 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
376 | "Failed to create tmp cfg!\n"); | ||
377 | do_shutdown (); | ||
378 | return; | ||
379 | } | ||
380 | |||
381 | if (GNUNET_OK != GNUNET_CONFIGURATION_write ((struct GNUNET_CONFIGURATION_Handle *)cfg, | ||
382 | tmp_cfgfile)) | ||
383 | { | ||
384 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
385 | "Failed to write tmp cfg\n"); | ||
386 | return; | ||
387 | } | ||
388 | |||
389 | proxy_proc = GNUNET_OS_start_process (GNUNET_NO, | ||
390 | GNUNET_OS_INHERIT_STD_ALL, | ||
391 | NULL, | ||
392 | NULL, | ||
393 | "gnunet-gns-proxy", | ||
394 | "gnunet-gns-proxy", | ||
395 | "-c", tmp_cfgfile, NULL); | ||
396 | |||
397 | GNUNET_assert (NULL != proxy_proc); | ||
398 | |||
399 | if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "gns", | ||
400 | "ZONEKEY", | ||
401 | &zone_keyfile)) | ||
402 | { | ||
403 | GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Failed to get key from cfg\n"); | ||
404 | return; | ||
405 | } | ||
406 | |||
407 | host_key = GNUNET_CRYPTO_rsa_key_create_from_file (zone_keyfile); | ||
408 | rd.expiration_time = GNUNET_TIME_UNIT_FOREVER_ABS.abs_value; | ||
409 | GNUNET_assert (GNUNET_OK == GNUNET_NAMESTORE_string_to_value (GNUNET_GNS_RECORD_A, | ||
410 | "127.0.0.1", | ||
411 | (void**)&rd.data, | ||
412 | &rd.data_size)); | ||
413 | rd.record_type = GNUNET_GNS_RECORD_A; | ||
414 | |||
415 | GNUNET_NAMESTORE_record_create (namestore, | ||
416 | host_key, | ||
417 | "www", | ||
418 | &rd, | ||
419 | &commence_testing, | ||
420 | NULL); | ||
421 | |||
422 | GNUNET_free ((void**)rd.data); | ||
423 | GNUNET_free (zone_keyfile); | ||
424 | GNUNET_CRYPTO_rsa_key_free (host_key); | ||
425 | } | ||
426 | |||
427 | int | ||
428 | main (int argc, char *const *argv) | ||
429 | { | ||
430 | |||
431 | GNUNET_CRYPTO_setup_hostkey ("test_gns_proxy.conf"); | ||
432 | |||
433 | if (0 != curl_global_init (CURL_GLOBAL_WIN32)) | ||
434 | { | ||
435 | fprintf (stderr, "failed to initialize curl\n"); | ||
436 | return 2; | ||
437 | } | ||
438 | if (0 != GNUNET_TESTING_peer_run ("test-gnunet-gns-proxy", | ||
439 | "test_gns_proxy.conf", | ||
440 | &run, NULL)) | ||
441 | return 1; | ||
442 | GNUNET_DISK_directory_remove ("/tmp/gnunet-test-gns-proxy"); | ||
443 | return global_ret; | ||
444 | } | ||
445 | |||
446 | /* end of test_gns_vpn.c */ | ||
447 | |||