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 it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your 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 Affero General Public License for more details.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
20 * @file test_gns_proxy.c
21 * @brief testcase for accessing SOCKS5 GNS proxy
22 * @author Martin Schanzenbach
26 #include <curl/curl.h>
27 #elif HAVE_GNURL_CURL_H
28 #include <gnurl/curl.h>
30 #include <microhttpd.h>
31 #include "gnunet_namestore_service.h"
32 #include "gnunet_gns_service.h"
33 #include "gnunet_testing_lib.h"
34 #include "gnunet_os_lib.h"
37 #define TEST_DOMAIN "www.gnu"
39 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 300)
42 * Return value for 'main'.
44 static int global_ret;
46 static struct GNUNET_NAMESTORE_Handle *namestore;
48 static struct MHD_Daemon *mhd;
50 static struct GNUNET_SCHEDULER_Task * mhd_task_id;
52 static struct GNUNET_SCHEDULER_Task * curl_task_id;
60 static struct GNUNET_OS_Process *proxy_proc;
62 static char* tmp_cfgfile;
70 static struct CBC cbc;
74 copy_buffer (void *ptr, size_t size, size_t nmemb, void *ctx)
76 struct CBC *cbc = ctx;
78 if (cbc->pos + size * nmemb > sizeof(cbc->buf))
79 return 0; /* overflow */
80 GNUNET_memcpy (&cbc->buf[cbc->pos], ptr, size * nmemb);
81 cbc->pos += size * nmemb;
88 struct MHD_Connection *connection,
92 const char *upload_data, size_t *upload_data_size,
96 struct MHD_Response *response;
99 if (0 != strcmp ("GET", method))
100 return MHD_NO; /* unexpected method */
107 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MHD sends respose for request to URL `%s'\n", url);
108 response = MHD_create_response_from_buffer (strlen (url),
110 MHD_RESPMEM_MUST_COPY);
111 ret = MHD_queue_response (connection, MHD_HTTP_OK, response);
112 MHD_destroy_response (response);
122 if (mhd_task_id != NULL)
124 GNUNET_SCHEDULER_cancel (mhd_task_id);
127 if (curl_task_id != NULL)
129 GNUNET_SCHEDULER_cancel (curl_task_id);
134 MHD_stop_daemon (mhd);
137 GNUNET_free_non_null (url);
139 if (NULL != tmp_cfgfile)
141 if (0 != remove (tmp_cfgfile))
142 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "remove", tmp_cfgfile);
143 GNUNET_free (tmp_cfgfile);
146 if (NULL != proxy_proc)
148 (void) GNUNET_OS_process_kill (proxy_proc, SIGKILL);
149 GNUNET_assert (GNUNET_OK == GNUNET_OS_process_wait (proxy_proc));
150 GNUNET_OS_process_destroy (proxy_proc);
154 GNUNET_SCHEDULER_shutdown ();
159 * Function to run the HTTP client.
166 curl_task (void *cls)
180 struct GNUNET_NETWORK_FDSet nrs;
181 struct GNUNET_NETWORK_FDSet nws;
182 struct GNUNET_TIME_Relative delay;
191 curl_multi_perform (multi, &running);
194 GNUNET_assert (NULL != (msg = curl_multi_info_read (multi, &running)));
195 if (msg->msg == CURLMSG_DONE)
197 if (msg->data.result != CURLE_OK)
200 "%s failed at %s:%d: `%s'\n",
201 "curl_multi_perform",
203 __LINE__, curl_easy_strerror (msg->data.result));
207 curl_multi_remove_handle (multi, curl);
208 curl_multi_cleanup (multi);
209 curl_easy_cleanup (curl);
212 if (cbc.pos != strlen ("/hello_world"))
217 if (0 != strncmp ("/hello_world", cbc.buf, strlen ("/hello_world")))
222 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Download complete, shutting down!\n");
226 GNUNET_assert (CURLM_OK == curl_multi_fdset (multi, &rs, &ws, &es, &max));
227 if ( (CURLM_OK != curl_multi_timeout (multi, &timeout)) ||
229 delay = GNUNET_TIME_UNIT_SECONDS;
231 delay = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, (unsigned int) timeout);
232 GNUNET_NETWORK_fdset_copy_native (&nrs,
235 GNUNET_NETWORK_fdset_copy_native (&nws,
238 curl_task_id = GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
248 start_curl (void *cls)
250 GNUNET_asprintf (&url,
251 "http://%s:%d/hello_world",
253 curl = curl_easy_init ();
254 curl_easy_setopt (curl, CURLOPT_URL, url);
255 curl_easy_setopt (curl, CURLOPT_WRITEFUNCTION, ©_buffer);
256 curl_easy_setopt (curl, CURLOPT_WRITEDATA, &cbc);
257 curl_easy_setopt (curl, CURLOPT_FAILONERROR, 1);
258 curl_easy_setopt (curl, CURLOPT_TIMEOUT, 150L);
259 curl_easy_setopt (curl, CURLOPT_CONNECTTIMEOUT, 15L);
260 curl_easy_setopt (curl, CURLOPT_NOSIGNAL, 1);
261 curl_easy_setopt (curl, CURLOPT_PROXY, "socks5h://127.0.0.1:7777");
263 multi = curl_multi_init ();
264 GNUNET_assert (multi != NULL);
265 GNUNET_assert (CURLM_OK == curl_multi_add_handle (multi, curl));
266 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Beginning HTTP download from `%s'\n", url);
274 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,
295 GNUNET_SCHEDULER_add_now (&disco_ns, NULL);
297 if ( (emsg != NULL) && (GNUNET_YES != success) )
300 "NS failed to create record %s\n",
302 GNUNET_SCHEDULER_shutdown ();
307 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
313 * Function to keep the HTTP server running.
331 struct GNUNET_NETWORK_FDSet nrs;
332 struct GNUNET_NETWORK_FDSet nws;
337 unsigned MHD_LONG_LONG timeout;
338 struct GNUNET_TIME_Relative delay;
340 GNUNET_assert (NULL == mhd_task_id);
345 GNUNET_assert (MHD_YES ==
346 MHD_get_fdset (mhd, &rs, &ws, &es, &max_fd));
347 if (MHD_YES == MHD_get_timeout (mhd, &timeout))
348 delay = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
349 (unsigned int) timeout);
351 delay = GNUNET_TIME_UNIT_FOREVER_REL;
352 GNUNET_NETWORK_fdset_copy_native (&nrs,
355 GNUNET_NETWORK_fdset_copy_native (&nws,
358 mhd_task_id = GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
368 const struct GNUNET_CONFIGURATION_Handle *cfg,
369 struct GNUNET_TESTING_Peer *peer)
372 struct GNUNET_CRYPTO_EcdsaPrivateKey *host_key;
373 struct GNUNET_GNSRECORD_Data rd;
376 namestore = GNUNET_NAMESTORE_connect (cfg);
377 GNUNET_assert (NULL != namestore);
378 flags = MHD_USE_DEBUG;
379 mhd = MHD_start_daemon (flags,
384 GNUNET_assert (NULL != mhd);
387 tmp_cfgfile = GNUNET_DISK_mktemp ("test_gns_proxy_tmp.conf");
388 if (NULL == tmp_cfgfile)
390 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
391 "Failed to create tmp cfg!\n");
396 if (GNUNET_OK != GNUNET_CONFIGURATION_write ((struct GNUNET_CONFIGURATION_Handle *)cfg,
399 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
400 "Failed to write tmp cfg\n");
405 proxy_proc = GNUNET_OS_start_process (GNUNET_NO,
406 GNUNET_OS_INHERIT_STD_ALL,
412 "-c", tmp_cfgfile, NULL);
414 if (NULL == proxy_proc)
416 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
417 "Unable to start proxy\n");
422 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "gns",
426 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Failed to get key from cfg\n");
430 host_key = GNUNET_CRYPTO_ecdsa_key_create_from_file (zone_keyfile);
431 rd.expiration_time = GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us;
432 GNUNET_assert (GNUNET_OK == GNUNET_GNSRECORD_string_to_value (GNUNET_DNSPARSER_TYPE_A,
436 rd.record_type = GNUNET_DNSPARSER_TYPE_A;
438 GNUNET_NAMESTORE_record_create (namestore,
445 GNUNET_free ((void**)rd.data);
446 GNUNET_free (zone_keyfile);
447 GNUNET_free (host_key);
451 main (int argc, char *const *argv)
455 if (GNUNET_SYSERR == GNUNET_OS_check_helper_binary ("gnunet-gns-proxy", GNUNET_NO, NULL))
457 fprintf (stderr, "Proxy binary not in PATH... skipping!\n");
460 binary = GNUNET_OS_get_libexec_binary_path ("gnunet-helper-dns");
461 if (GNUNET_YES != GNUNET_OS_check_helper_binary (binary, GNUNET_YES, NULL)) // TODO: once we have a windows-testcase, add test parameters here
463 fprintf (stderr, "DNS helper binary has wrong permissions... skipping!\n");
464 GNUNET_free (binary);
467 GNUNET_free (binary);
469 if (0 != curl_global_init (CURL_GLOBAL_WIN32))
471 fprintf (stderr, "failed to initialize curl\n");
474 if (0 != GNUNET_TESTING_peer_run ("test-gnunet-gns-proxy",
475 "test_gns_proxy.conf",
478 GNUNET_DISK_directory_remove ("/tmp/gnunet-test-gns-proxy");
482 /* end of test_gns_vpn.c */