2 This file is part of GNUnet
3 Copyright (C) 2007, 2009, 2011, 2012 Christian Grothoff
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.
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.
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.
22 * @file test_gns_proxy.c
23 * @brief testcase for accessing SOCKS5 GNS proxy
24 * @author Martin Schanzenbach
28 #include <curl/curl.h>
29 #elif HAVE_GNURL_CURL_H
30 #include <gnurl/curl.h>
32 #include <microhttpd.h>
33 #include "gnunet_namestore_service.h"
34 #include "gnunet_gns_service.h"
35 #include "gnunet_testing_lib.h"
36 #include "gnunet_os_lib.h"
39 #define TEST_DOMAIN "www.gnu"
41 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 300)
44 * Return value for 'main'.
46 static int global_ret;
48 static struct GNUNET_NAMESTORE_Handle *namestore;
50 static struct MHD_Daemon *mhd;
52 static struct GNUNET_SCHEDULER_Task * mhd_task_id;
54 static struct GNUNET_SCHEDULER_Task * curl_task_id;
62 static struct GNUNET_OS_Process *proxy_proc;
64 static char* tmp_cfgfile;
72 static struct CBC cbc;
76 copy_buffer (void *ptr, size_t size, size_t nmemb, void *ctx)
78 struct CBC *cbc = ctx;
80 if (cbc->pos + size * nmemb > sizeof(cbc->buf))
81 return 0; /* overflow */
82 GNUNET_memcpy (&cbc->buf[cbc->pos], ptr, size * nmemb);
83 cbc->pos += size * nmemb;
90 struct MHD_Connection *connection,
94 const char *upload_data, size_t *upload_data_size,
98 struct MHD_Response *response;
101 if (0 != strcmp ("GET", method))
102 return MHD_NO; /* unexpected method */
109 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MHD sends respose for request to URL `%s'\n", url);
110 response = MHD_create_response_from_buffer (strlen (url),
112 MHD_RESPMEM_MUST_COPY);
113 ret = MHD_queue_response (connection, MHD_HTTP_OK, response);
114 MHD_destroy_response (response);
124 if (mhd_task_id != NULL)
126 GNUNET_SCHEDULER_cancel (mhd_task_id);
129 if (curl_task_id != NULL)
131 GNUNET_SCHEDULER_cancel (curl_task_id);
136 MHD_stop_daemon (mhd);
139 GNUNET_free_non_null (url);
141 if (NULL != tmp_cfgfile)
143 if (0 != remove (tmp_cfgfile))
144 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "remove", tmp_cfgfile);
145 GNUNET_free (tmp_cfgfile);
148 if (NULL != proxy_proc)
150 (void) GNUNET_OS_process_kill (proxy_proc, SIGKILL);
151 GNUNET_assert (GNUNET_OK == GNUNET_OS_process_wait (proxy_proc));
152 GNUNET_OS_process_destroy (proxy_proc);
156 GNUNET_SCHEDULER_shutdown ();
161 * Function to run the HTTP client.
168 curl_task (void *cls)
182 struct GNUNET_NETWORK_FDSet nrs;
183 struct GNUNET_NETWORK_FDSet nws;
184 struct GNUNET_TIME_Relative delay;
193 curl_multi_perform (multi, &running);
196 GNUNET_assert (NULL != (msg = curl_multi_info_read (multi, &running)));
197 if (msg->msg == CURLMSG_DONE)
199 if (msg->data.result != CURLE_OK)
202 "%s failed at %s:%d: `%s'\n",
203 "curl_multi_perform",
205 __LINE__, curl_easy_strerror (msg->data.result));
209 curl_multi_remove_handle (multi, curl);
210 curl_multi_cleanup (multi);
211 curl_easy_cleanup (curl);
214 if (cbc.pos != strlen ("/hello_world"))
219 if (0 != strncmp ("/hello_world", cbc.buf, strlen ("/hello_world")))
224 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Download complete, shutting down!\n");
228 GNUNET_assert (CURLM_OK == curl_multi_fdset (multi, &rs, &ws, &es, &max));
229 if ( (CURLM_OK != curl_multi_timeout (multi, &timeout)) ||
231 delay = GNUNET_TIME_UNIT_SECONDS;
233 delay = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, (unsigned int) timeout);
234 GNUNET_NETWORK_fdset_copy_native (&nrs,
237 GNUNET_NETWORK_fdset_copy_native (&nws,
240 curl_task_id = GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
250 start_curl (void *cls)
252 GNUNET_asprintf (&url,
253 "http://%s:%d/hello_world",
255 curl = curl_easy_init ();
256 curl_easy_setopt (curl, CURLOPT_URL, url);
257 curl_easy_setopt (curl, CURLOPT_WRITEFUNCTION, ©_buffer);
258 curl_easy_setopt (curl, CURLOPT_WRITEDATA, &cbc);
259 curl_easy_setopt (curl, CURLOPT_FAILONERROR, 1);
260 curl_easy_setopt (curl, CURLOPT_TIMEOUT, 150L);
261 curl_easy_setopt (curl, CURLOPT_CONNECTTIMEOUT, 15L);
262 curl_easy_setopt (curl, CURLOPT_NOSIGNAL, 1);
263 curl_easy_setopt (curl, CURLOPT_PROXY, "socks5h://127.0.0.1:7777");
265 multi = curl_multi_init ();
266 GNUNET_assert (multi != NULL);
267 GNUNET_assert (CURLM_OK == curl_multi_add_handle (multi, curl));
268 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Beginning HTTP download from `%s'\n", url);
276 GNUNET_NAMESTORE_disconnect (namestore);
281 * Callback invoked from the namestore service once record is
285 * @param af address family, AF_INET or AF_INET6; AF_UNSPEC on error;
286 * will match 'result_af' from the request
287 * @param address IP address (struct in_addr or struct in_addr6, depending on 'af')
288 * that the VPN allocated for the redirection;
289 * traffic to this IP will now be redirected to the
290 * specified target peer; NULL on error
293 commence_testing (void *cls,
297 GNUNET_SCHEDULER_add_now (&disco_ns, NULL);
299 if ( (emsg != NULL) && (GNUNET_YES != success) )
302 "NS failed to create record %s\n",
304 GNUNET_SCHEDULER_shutdown ();
309 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
315 * Function to keep the HTTP server running.
333 struct GNUNET_NETWORK_FDSet nrs;
334 struct GNUNET_NETWORK_FDSet nws;
339 unsigned MHD_LONG_LONG timeout;
340 struct GNUNET_TIME_Relative delay;
342 GNUNET_assert (NULL == mhd_task_id);
347 GNUNET_assert (MHD_YES ==
348 MHD_get_fdset (mhd, &rs, &ws, &es, &max_fd));
349 if (MHD_YES == MHD_get_timeout (mhd, &timeout))
350 delay = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
351 (unsigned int) timeout);
353 delay = GNUNET_TIME_UNIT_FOREVER_REL;
354 GNUNET_NETWORK_fdset_copy_native (&nrs,
357 GNUNET_NETWORK_fdset_copy_native (&nws,
360 mhd_task_id = GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
370 const struct GNUNET_CONFIGURATION_Handle *cfg,
371 struct GNUNET_TESTING_Peer *peer)
374 struct GNUNET_CRYPTO_EcdsaPrivateKey *host_key;
375 struct GNUNET_GNSRECORD_Data rd;
378 namestore = GNUNET_NAMESTORE_connect (cfg);
379 GNUNET_assert (NULL != namestore);
380 flags = MHD_USE_DEBUG;
381 mhd = MHD_start_daemon (flags,
386 GNUNET_assert (NULL != mhd);
389 tmp_cfgfile = GNUNET_DISK_mktemp ("test_gns_proxy_tmp.conf");
390 if (NULL == tmp_cfgfile)
392 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
393 "Failed to create tmp cfg!\n");
398 if (GNUNET_OK != GNUNET_CONFIGURATION_write ((struct GNUNET_CONFIGURATION_Handle *)cfg,
401 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
402 "Failed to write tmp cfg\n");
407 proxy_proc = GNUNET_OS_start_process (GNUNET_NO,
408 GNUNET_OS_INHERIT_STD_ALL,
414 "-c", tmp_cfgfile, NULL);
416 if (NULL == proxy_proc)
418 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
419 "Unable to start proxy\n");
424 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "gns",
428 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Failed to get key from cfg\n");
432 host_key = GNUNET_CRYPTO_ecdsa_key_create_from_file (zone_keyfile);
433 rd.expiration_time = GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us;
434 GNUNET_assert (GNUNET_OK == GNUNET_GNSRECORD_string_to_value (GNUNET_DNSPARSER_TYPE_A,
438 rd.record_type = GNUNET_DNSPARSER_TYPE_A;
440 GNUNET_NAMESTORE_record_create (namestore,
447 GNUNET_free ((void**)rd.data);
448 GNUNET_free (zone_keyfile);
449 GNUNET_free (host_key);
453 main (int argc, char *const *argv)
457 if (GNUNET_SYSERR == GNUNET_OS_check_helper_binary ("gnunet-gns-proxy", GNUNET_NO, NULL))
459 fprintf (stderr, "Proxy binary not in PATH... skipping!\n");
462 binary = GNUNET_OS_get_libexec_binary_path ("gnunet-helper-dns");
463 if (GNUNET_YES != GNUNET_OS_check_helper_binary (binary, GNUNET_YES, NULL)) // TODO: once we have a windows-testcase, add test parameters here
465 fprintf (stderr, "DNS helper binary has wrong permissions... skipping!\n");
466 GNUNET_free (binary);
469 GNUNET_free (binary);
471 if (0 != curl_global_init (CURL_GLOBAL_WIN32))
473 fprintf (stderr, "failed to initialize curl\n");
476 if (0 != GNUNET_TESTING_peer_run ("test-gnunet-gns-proxy",
477 "test_gns_proxy.conf",
480 GNUNET_DISK_directory_remove ("/tmp/gnunet-test-gns-proxy");
484 /* end of test_gns_vpn.c */