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 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,
169 const struct GNUNET_SCHEDULER_TaskContext *tc)
183 struct GNUNET_NETWORK_FDSet nrs;
184 struct GNUNET_NETWORK_FDSet nws;
185 struct GNUNET_TIME_Relative delay;
194 curl_multi_perform (multi, &running);
197 GNUNET_assert (NULL != (msg = curl_multi_info_read (multi, &running)));
198 if (msg->msg == CURLMSG_DONE)
200 if (msg->data.result != CURLE_OK)
203 "%s failed at %s:%d: `%s'\n",
204 "curl_multi_perform",
206 __LINE__, curl_easy_strerror (msg->data.result));
210 curl_multi_remove_handle (multi, curl);
211 curl_multi_cleanup (multi);
212 curl_easy_cleanup (curl);
215 if (cbc.pos != strlen ("/hello_world"))
220 if (0 != strncmp ("/hello_world", cbc.buf, strlen ("/hello_world")))
225 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Download complete, shutting down!\n");
229 GNUNET_assert (CURLM_OK == curl_multi_fdset (multi, &rs, &ws, &es, &max));
230 if ( (CURLM_OK != curl_multi_timeout (multi, &timeout)) ||
232 delay = GNUNET_TIME_UNIT_SECONDS;
234 delay = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, (unsigned int) timeout);
235 GNUNET_NETWORK_fdset_copy_native (&nrs,
238 GNUNET_NETWORK_fdset_copy_native (&nws,
241 curl_task_id = GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
250 start_curl (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
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);
273 disco_ns (void* cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
275 GNUNET_NAMESTORE_disconnect (namestore);
279 * Callback invoked from the namestore service once record is
283 * @param af address family, AF_INET or AF_INET6; AF_UNSPEC on error;
284 * will match 'result_af' from the request
285 * @param address IP address (struct in_addr or struct in_addr6, depending on 'af')
286 * that the VPN allocated for the redirection;
287 * traffic to this IP will now be redirected to the
288 * specified target peer; NULL on error
291 commence_testing (void *cls, int32_t success, const char *emsg)
293 GNUNET_SCHEDULER_add_now (&disco_ns, NULL);
295 if ((emsg != NULL) && (GNUNET_YES != success))
298 "NS failed to create record %s\n", emsg);
299 GNUNET_SCHEDULER_shutdown ();
303 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1), start_curl, NULL);
311 * Function to keep the HTTP server running.
319 const struct GNUNET_SCHEDULER_TaskContext *tc)
330 struct GNUNET_NETWORK_FDSet nrs;
331 struct GNUNET_NETWORK_FDSet nws;
336 unsigned MHD_LONG_LONG timeout;
337 struct GNUNET_TIME_Relative delay;
339 GNUNET_assert (NULL == mhd_task_id);
344 GNUNET_assert (MHD_YES ==
345 MHD_get_fdset (mhd, &rs, &ws, &es, &max_fd));
346 if (MHD_YES == MHD_get_timeout (mhd, &timeout))
347 delay = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
348 (unsigned int) timeout);
350 delay = GNUNET_TIME_UNIT_FOREVER_REL;
351 GNUNET_NETWORK_fdset_copy_native (&nrs,
354 GNUNET_NETWORK_fdset_copy_native (&nws,
357 mhd_task_id = GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
367 const struct GNUNET_CONFIGURATION_Handle *cfg,
368 struct GNUNET_TESTING_Peer *peer)
371 struct GNUNET_CRYPTO_EcdsaPrivateKey *host_key;
372 struct GNUNET_GNSRECORD_Data rd;
375 namestore = GNUNET_NAMESTORE_connect (cfg);
376 GNUNET_assert (NULL != namestore);
377 flags = MHD_USE_DEBUG;
378 mhd = MHD_start_daemon (flags,
383 GNUNET_assert (NULL != mhd);
386 tmp_cfgfile = GNUNET_DISK_mktemp ("test_gns_proxy_tmp.conf");
387 if (NULL == tmp_cfgfile)
389 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
390 "Failed to create tmp cfg!\n");
395 if (GNUNET_OK != GNUNET_CONFIGURATION_write ((struct GNUNET_CONFIGURATION_Handle *)cfg,
398 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
399 "Failed to write tmp cfg\n");
404 proxy_proc = GNUNET_OS_start_process (GNUNET_NO,
405 GNUNET_OS_INHERIT_STD_ALL,
411 "-c", tmp_cfgfile, NULL);
413 if (NULL == proxy_proc)
415 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
416 "Unable to start proxy\n");
421 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "gns",
425 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Failed to get key from cfg\n");
429 host_key = GNUNET_CRYPTO_ecdsa_key_create_from_file (zone_keyfile);
430 rd.expiration_time = GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us;
431 GNUNET_assert (GNUNET_OK == GNUNET_GNSRECORD_string_to_value (GNUNET_DNSPARSER_TYPE_A,
435 rd.record_type = GNUNET_DNSPARSER_TYPE_A;
437 GNUNET_NAMESTORE_record_create (namestore,
444 GNUNET_free ((void**)rd.data);
445 GNUNET_free (zone_keyfile);
446 GNUNET_free (host_key);
450 main (int argc, char *const *argv)
454 if (GNUNET_SYSERR == GNUNET_OS_check_helper_binary ("gnunet-gns-proxy", GNUNET_NO, NULL))
456 fprintf (stderr, "Proxy binary not in PATH... skipping!\n");
459 binary = GNUNET_OS_get_libexec_binary_path ("gnunet-helper-dns");
460 if (GNUNET_YES != GNUNET_OS_check_helper_binary (binary, GNUNET_YES, NULL)) // TODO: once we have a windows-testcase, add test parameters here
462 fprintf (stderr, "DNS helper binary has wrong permissions... skipping!\n");
463 GNUNET_free (binary);
466 GNUNET_free (binary);
468 if (0 != curl_global_init (CURL_GLOBAL_WIN32))
470 fprintf (stderr, "failed to initialize curl\n");
473 if (0 != GNUNET_TESTING_peer_run ("test-gnunet-gns-proxy",
474 "test_gns_proxy.conf",
477 GNUNET_DISK_directory_remove ("/tmp/gnunet-test-gns-proxy");
481 /* end of test_gns_vpn.c */