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_gnunet_vpn.c
23 * @brief testcase for tunneling HTTP over the GNUnet VPN
24 * @author Christian Grothoff
27 #include <curl/curl.h>
28 #include <microhttpd.h>
29 #include "gnunet_vpn_service.h"
30 #include "gnunet_arm_service.h"
34 #define START_ARM GNUNET_YES
36 #define VERBOSE GNUNET_NO
40 struct GNUNET_CONFIGURATION_Handle *cfg;
41 struct GNUNET_PeerIdentity id;
43 struct GNUNET_OS_Process *arm_proc;
47 static struct PeerContext p1;
50 * Return value for 'main'.
52 static int global_ret;
54 static struct GNUNET_VPN_Handle *vpn;
56 static struct MHD_Daemon *mhd;
58 static GNUNET_SCHEDULER_TaskIdentifier mhd_task_id;
60 static GNUNET_SCHEDULER_TaskIdentifier curl_task_id;
62 static GNUNET_SCHEDULER_TaskIdentifier ctrl_c_task_id;
64 static struct GNUNET_VPN_RedirectionRequest *rr;
78 static struct CBC cbc;
83 copyBuffer (void *ptr, size_t size, size_t nmemb, void *ctx)
85 struct CBC *cbc = ctx;
87 if (cbc->pos + size * nmemb > sizeof(cbc->buf))
88 return 0; /* overflow */
89 memcpy (&cbc->buf[cbc->pos], ptr, size * nmemb);
90 cbc->pos += size * nmemb;
97 struct MHD_Connection *connection,
101 const char *upload_data, size_t *upload_data_size,
105 struct MHD_Response *response;
108 if (0 != strcmp ("GET", method))
109 return MHD_NO; /* unexpected method */
116 fprintf (stderr, "MHD sends respose for request to URL `%s'\n", url);
117 response = MHD_create_response_from_buffer (strlen (url),
119 MHD_RESPMEM_MUST_COPY);
120 ret = MHD_queue_response (connection, MHD_HTTP_OK, response);
121 MHD_destroy_response (response);
131 if (mhd_task_id != GNUNET_SCHEDULER_NO_TASK)
133 GNUNET_SCHEDULER_cancel (mhd_task_id);
134 mhd_task_id = GNUNET_SCHEDULER_NO_TASK;
136 if (curl_task_id != GNUNET_SCHEDULER_NO_TASK)
138 GNUNET_SCHEDULER_cancel (curl_task_id);
139 curl_task_id = GNUNET_SCHEDULER_NO_TASK;
141 if (ctrl_c_task_id != GNUNET_SCHEDULER_NO_TASK)
143 GNUNET_SCHEDULER_cancel (ctrl_c_task_id);
144 ctrl_c_task_id = GNUNET_SCHEDULER_NO_TASK;
148 MHD_stop_daemon (mhd);
153 GNUNET_VPN_cancel_request (rr);
158 GNUNET_VPN_disconnect (vpn);
161 GNUNET_free_non_null (url);
167 * Function to run the HTTP client.
174 curl_task (void *cls,
175 const struct GNUNET_SCHEDULER_TaskContext *tc)
177 curl_task_id = GNUNET_SCHEDULER_NO_TASK;
189 struct GNUNET_NETWORK_FDSet nrs;
190 struct GNUNET_NETWORK_FDSet nws;
191 struct GNUNET_TIME_Relative delay;
200 curl_multi_perform (multi, &running);
203 GNUNET_assert (NULL != (msg = curl_multi_info_read (multi, &running)));
204 if (msg->msg == CURLMSG_DONE)
206 if (msg->data.result != CURLE_OK)
208 printf ("%s failed at %s:%d: `%s'\n",
209 "curl_multi_perform",
211 __LINE__, curl_easy_strerror (msg->data.result));
215 curl_multi_remove_handle (multi, curl);
216 curl_multi_cleanup (multi);
217 curl_easy_cleanup (curl);
220 if (cbc.pos != strlen ("/hello_world"))
222 if (0 != strncmp ("/hello_world", cbc.buf, strlen ("/hello_world")))
224 fprintf (stderr, "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,
241 GNUNET_SCHEDULER_NO_TASK,
251 * Callback invoked from the VPN service once a redirection is
252 * available. Provides the IP address that can now be used to
253 * reach the requested destination (in our case, the MHD server)
256 * @param af address family, AF_INET or AF_INET6; AF_UNSPEC on error;
257 * will match 'result_af' from the request
258 * @param address IP address (struct in_addr or struct in_addr6, depending on 'af')
259 * that the VPN allocated for the redirection;
260 * traffic to this IP will now be redirected to the
261 * specified target peer; NULL on error
264 allocation_cb (void *cls,
268 char ips[INET_ADDRSTRLEN];
274 "VPN failed to allocate appropriate address\n");
275 GNUNET_SCHEDULER_shutdown ();
278 GNUNET_asprintf (&url,
279 "http://%s:%u/hello_world",
280 inet_ntop (af, address, ips, sizeof (ips)),
281 (unsigned int) PORT);
282 curl = curl_easy_init ();
283 curl_easy_setopt (curl, CURLOPT_URL, url);
284 curl_easy_setopt (curl, CURLOPT_WRITEFUNCTION, ©Buffer);
285 curl_easy_setopt (curl, CURLOPT_WRITEDATA, &cbc);
286 curl_easy_setopt (curl, CURLOPT_FAILONERROR, 1);
287 curl_easy_setopt (curl, CURLOPT_TIMEOUT, 150L);
288 curl_easy_setopt (curl, CURLOPT_CONNECTTIMEOUT, 15L);
289 curl_easy_setopt (curl, CURLOPT_NOSIGNAL, 1);
291 multi = curl_multi_init ();
292 GNUNET_assert (multi != NULL);
293 GNUNET_assert (CURLM_OK == curl_multi_add_handle (multi, curl));
295 fprintf (stderr, "Beginning HTTP download from `%s'\n", url);
301 * Function to keep the HTTP server running.
309 const struct GNUNET_SCHEDULER_TaskContext *tc)
311 mhd_task_id = GNUNET_SCHEDULER_NO_TASK;
318 ctrl_c_shutdown (void *cls,
319 const struct GNUNET_SCHEDULER_TaskContext *tc)
321 ctrl_c_task_id = GNUNET_SCHEDULER_NO_TASK;
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 (GNUNET_SCHEDULER_NO_TASK == 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,
358 GNUNET_SCHEDULER_NO_TASK,
368 run (void *cls, char *const *args, const char *cfgfile,
369 const struct GNUNET_CONFIGURATION_Handle *cfg)
373 vpn = GNUNET_VPN_connect (cfg);
374 GNUNET_assert (NULL != vpn);
375 mhd = MHD_start_daemon (MHD_USE_DEBUG,
380 GNUNET_assert (NULL != mhd);
382 GNUNET_assert (1 == inet_pton (AF_INET, "10.10.1.1", &v4));
383 rr = GNUNET_VPN_redirect_to_ip (vpn,
388 GNUNET_TIME_UNIT_FOREVER_ABS,
389 &allocation_cb, NULL);
390 ctrl_c_task_id = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
397 setup_peer (struct PeerContext *p, const char *cfgname)
399 p->cfg = GNUNET_CONFIGURATION_create ();
402 GNUNET_OS_start_process (NULL, NULL, "gnunet-service-arm",
403 "gnunet-service-arm",
407 "-c", cfgname, NULL);
409 GNUNET_assert (NULL != p->arm_proc);
410 GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
415 stop_peer (struct PeerContext *p)
418 if (NULL != p->arm_proc)
420 if (0 != GNUNET_OS_process_kill (p->arm_proc, SIGTERM))
421 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
422 if (GNUNET_OS_process_wait (p->arm_proc) != GNUNET_OK)
423 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "waitpid");
424 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "ARM process %u stopped\n",
425 GNUNET_OS_process_get_pid (p->arm_proc));
426 GNUNET_OS_process_close (p->arm_proc);
430 GNUNET_CONFIGURATION_destroy (p->cfg);
435 main (int argc, char *const *argv)
437 char *const argvx[] = {
440 "test_gnunet_vpn.conf",
446 struct GNUNET_GETOPT_CommandLineOption options[] = {
447 GNUNET_GETOPT_OPTION_END
450 if (0 != curl_global_init (CURL_GLOBAL_WIN32))
452 setup_peer (&p1, "test_gnunet_vpn.conf");
453 GNUNET_log_setup ("test_gnunet_vpn",
460 GNUNET_PROGRAM_run ((sizeof (argvx) / sizeof (char *)) - 1, argvx,
461 "test_gnunet_vpn", "nohelp", options, &run, NULL);
463 GNUNET_DISK_directory_remove ("/tmp/gnunet-test-vpn");