2 This file is part of GNUnet
3 (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 2, 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., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file test_gns_proxy.c
23 * @brief testcase for accessing SOCKS5 GNS proxy
24 * @author Martin Schanzenbach
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"
35 #define TEST_DOMAIN "www.gads"
37 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 300)
40 * Return value for 'main'.
42 static int global_ret;
44 static struct GNUNET_NAMESTORE_Handle *namestore;
46 static struct MHD_Daemon *mhd;
48 static GNUNET_SCHEDULER_TaskIdentifier mhd_task_id;
50 static GNUNET_SCHEDULER_TaskIdentifier curl_task_id;
58 static struct GNUNET_OS_Process *proxy_proc;
60 static char* tmp_cfgfile;
68 static struct CBC cbc;
72 copy_buffer (void *ptr, size_t size, size_t nmemb, void *ctx)
74 struct CBC *cbc = ctx;
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;
86 struct MHD_Connection *connection,
90 const char *upload_data, size_t *upload_data_size,
94 struct MHD_Response *response;
97 if (0 != strcmp ("GET", method))
98 return MHD_NO; /* unexpected method */
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),
108 MHD_RESPMEM_MUST_COPY);
109 ret = MHD_queue_response (connection, MHD_HTTP_OK, response);
110 MHD_destroy_response (response);
120 if (mhd_task_id != GNUNET_SCHEDULER_NO_TASK)
122 GNUNET_SCHEDULER_cancel (mhd_task_id);
123 mhd_task_id = GNUNET_SCHEDULER_NO_TASK;
125 if (curl_task_id != GNUNET_SCHEDULER_NO_TASK)
127 GNUNET_SCHEDULER_cancel (curl_task_id);
128 curl_task_id = GNUNET_SCHEDULER_NO_TASK;
132 MHD_stop_daemon (mhd);
135 GNUNET_free_non_null (url);
137 if (NULL != tmp_cfgfile)
139 if (0 != remove (tmp_cfgfile))
140 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "remove", tmp_cfgfile);
141 GNUNET_free (tmp_cfgfile);
144 if (NULL != proxy_proc)
146 (void) GNUNET_OS_process_kill (proxy_proc, SIGKILL);
147 GNUNET_assert (GNUNET_OK == GNUNET_OS_process_wait (proxy_proc));
148 GNUNET_OS_process_destroy (proxy_proc);
152 GNUNET_SCHEDULER_shutdown ();
157 * Function to run the HTTP client.
164 curl_task (void *cls,
165 const struct GNUNET_SCHEDULER_TaskContext *tc)
167 curl_task_id = GNUNET_SCHEDULER_NO_TASK;
179 struct GNUNET_NETWORK_FDSet nrs;
180 struct GNUNET_NETWORK_FDSet nws;
181 struct GNUNET_TIME_Relative delay;
190 curl_multi_perform (multi, &running);
193 GNUNET_assert (NULL != (msg = curl_multi_info_read (multi, &running)));
194 if (msg->msg == CURLMSG_DONE)
196 if (msg->data.result != CURLE_OK)
199 "%s failed at %s:%d: `%s'\n",
200 "curl_multi_perform",
202 __LINE__, curl_easy_strerror (msg->data.result));
206 curl_multi_remove_handle (multi, curl);
207 curl_multi_cleanup (multi);
208 curl_easy_cleanup (curl);
211 if (cbc.pos != strlen ("/hello_world"))
216 if (0 != strncmp ("/hello_world", cbc.buf, strlen ("/hello_world")))
221 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Download complete, shutting down!\n");
225 GNUNET_assert (CURLM_OK == curl_multi_fdset (multi, &rs, &ws, &es, &max));
226 if ( (CURLM_OK != curl_multi_timeout (multi, &timeout)) ||
228 delay = GNUNET_TIME_UNIT_SECONDS;
230 delay = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, (unsigned int) timeout);
231 GNUNET_NETWORK_fdset_copy_native (&nrs,
234 GNUNET_NETWORK_fdset_copy_native (&nws,
237 curl_task_id = GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
246 start_curl (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
248 GNUNET_asprintf (&url,
249 "http://%s:%d/hello_world",
251 curl = curl_easy_init ();
252 curl_easy_setopt (curl, CURLOPT_URL, url);
253 curl_easy_setopt (curl, CURLOPT_WRITEFUNCTION, ©_buffer);
254 curl_easy_setopt (curl, CURLOPT_WRITEDATA, &cbc);
255 curl_easy_setopt (curl, CURLOPT_FAILONERROR, 1);
256 curl_easy_setopt (curl, CURLOPT_TIMEOUT, 150L);
257 curl_easy_setopt (curl, CURLOPT_CONNECTTIMEOUT, 15L);
258 curl_easy_setopt (curl, CURLOPT_NOSIGNAL, 1);
259 curl_easy_setopt (curl, CURLOPT_PROXY, "socks5h://127.0.0.1:7777");
261 multi = curl_multi_init ();
262 GNUNET_assert (multi != NULL);
263 GNUNET_assert (CURLM_OK == curl_multi_add_handle (multi, curl));
264 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Beginning HTTP download from `%s'\n", url);
269 disco_ns (void* cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
271 GNUNET_NAMESTORE_disconnect (namestore);
275 * Callback invoked from the namestore service once record is
279 * @param af address family, AF_INET or AF_INET6; AF_UNSPEC on error;
280 * will match 'result_af' from the request
281 * @param address IP address (struct in_addr or struct in_addr6, depending on 'af')
282 * that the VPN allocated for the redirection;
283 * traffic to this IP will now be redirected to the
284 * specified target peer; NULL on error
287 commence_testing (void *cls, int32_t success, const char *emsg)
289 GNUNET_SCHEDULER_add_now (&disco_ns, NULL);
291 if ((emsg != NULL) && (GNUNET_YES != success))
294 "NS failed to create record %s\n", emsg);
295 GNUNET_SCHEDULER_shutdown ();
299 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1), start_curl, NULL);
307 * Function to keep the HTTP server running.
315 const struct GNUNET_SCHEDULER_TaskContext *tc)
317 mhd_task_id = GNUNET_SCHEDULER_NO_TASK;
326 struct GNUNET_NETWORK_FDSet nrs;
327 struct GNUNET_NETWORK_FDSet nws;
332 unsigned MHD_LONG_LONG timeout;
333 struct GNUNET_TIME_Relative delay;
335 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == mhd_task_id);
340 GNUNET_assert (MHD_YES ==
341 MHD_get_fdset (mhd, &rs, &ws, &es, &max_fd));
342 if (MHD_YES == MHD_get_timeout (mhd, &timeout))
343 delay = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
344 (unsigned int) timeout);
346 delay = GNUNET_TIME_UNIT_FOREVER_REL;
347 GNUNET_NETWORK_fdset_copy_native (&nrs,
350 GNUNET_NETWORK_fdset_copy_native (&nws,
353 mhd_task_id = GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
363 const struct GNUNET_CONFIGURATION_Handle *cfg,
364 struct GNUNET_TESTING_Peer *peer)
367 struct GNUNET_CRYPTO_RsaPrivateKey *host_key;
368 struct GNUNET_NAMESTORE_RecordData rd;
371 namestore = GNUNET_NAMESTORE_connect (cfg);
372 GNUNET_assert (NULL != namestore);
373 flags = MHD_USE_DEBUG;
374 mhd = MHD_start_daemon (flags,
379 GNUNET_assert (NULL != mhd);
382 tmp_cfgfile = GNUNET_DISK_mktemp ("test_gns_proxy_tmp.conf");
383 if (NULL == tmp_cfgfile)
385 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
386 "Failed to create tmp cfg!\n");
391 if (GNUNET_OK != GNUNET_CONFIGURATION_write ((struct GNUNET_CONFIGURATION_Handle *)cfg,
394 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
395 "Failed to write tmp cfg\n");
400 proxy_proc = GNUNET_OS_start_process (GNUNET_NO,
401 GNUNET_OS_INHERIT_STD_ALL,
406 "-c", tmp_cfgfile, NULL);
408 if (NULL == proxy_proc)
410 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
411 "Unable to start proxy\n");
416 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "gns",
420 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Failed to get key from cfg\n");
424 host_key = GNUNET_CRYPTO_rsa_key_create_from_file (zone_keyfile);
425 rd.expiration_time = GNUNET_TIME_UNIT_FOREVER_ABS.abs_value;
426 GNUNET_assert (GNUNET_OK == GNUNET_NAMESTORE_string_to_value (GNUNET_GNS_RECORD_A,
430 rd.record_type = GNUNET_GNS_RECORD_A;
432 GNUNET_NAMESTORE_record_create (namestore,
439 GNUNET_free ((void**)rd.data);
440 GNUNET_free (zone_keyfile);
441 GNUNET_CRYPTO_rsa_key_free (host_key);
445 main (int argc, char *const *argv)
448 if (GNUNET_SYSERR == GNUNET_OS_check_helper_binary ("gnunet-gns-proxy"))
450 fprintf (stderr, "Proxy binary not in PATH... skipping!\n");
454 if (GNUNET_YES != GNUNET_OS_check_helper_binary ("gnunet-helper-dns"))
456 fprintf (stderr, "DNS helper binary has wrong permissions... skipping!\n");
460 GNUNET_CRYPTO_rsa_setup_hostkey ("test_gns_proxy.conf");
462 if (0 != curl_global_init (CURL_GLOBAL_WIN32))
464 fprintf (stderr, "failed to initialize curl\n");
467 if (0 != GNUNET_TESTING_peer_run ("test-gnunet-gns-proxy",
468 "test_gns_proxy.conf",
471 GNUNET_DISK_directory_remove ("/tmp/gnunet-test-gns-proxy");
475 /* end of test_gns_vpn.c */