aboutsummaryrefslogtreecommitdiff
path: root/src/pt
diff options
context:
space:
mode:
authorMartin Schanzenbach <mschanzenbach@posteo.de>2012-06-27 10:26:20 +0000
committerMartin Schanzenbach <mschanzenbach@posteo.de>2012-06-27 10:26:20 +0000
commitf68e5cda5acb6f39abaaeec839cb7e7cf2b77053 (patch)
tree82a9eb484330322585b067191e4c823ce8fef955 /src/pt
parentb7daa03785d09510b3f36d4e6bbab37b005c3619 (diff)
downloadgnunet-f68e5cda5acb6f39abaaeec839cb7e7cf2b77053.tar.gz
gnunet-f68e5cda5acb6f39abaaeec839cb7e7cf2b77053.zip
-fix order
Diffstat (limited to 'src/pt')
-rw-r--r--src/pt/Makefile.am66
-rw-r--r--src/pt/test_gns_vpn.c491
-rw-r--r--src/pt/test_gns_vpn.conf52
-rw-r--r--src/pt/test_gnunet_vpn.c535
-rw-r--r--src/pt/test_gnunet_vpn.conf43
5 files changed, 1187 insertions, 0 deletions
diff --git a/src/pt/Makefile.am b/src/pt/Makefile.am
index 385bff672..af2817bf9 100644
--- a/src/pt/Makefile.am
+++ b/src/pt/Makefile.am
@@ -29,3 +29,69 @@ gnunet_daemon_pt_LDADD = \
29 $(top_builddir)/src/util/libgnunetutil.la \ 29 $(top_builddir)/src/util/libgnunetutil.la \
30 $(top_builddir)/src/mesh/libgnunetmesh.la \ 30 $(top_builddir)/src/mesh/libgnunetmesh.la \
31 $(GN_LIBINTL) 31 $(GN_LIBINTL)
32
33check_PROGRAMS = $(VPN_TEST)
34
35if ENABLE_TEST_RUN
36TESTS = $(check_PROGRAMS)
37endif
38
39EXTRA_DIST = \
40 test_gnunet_vpn.conf \
41 test_gns_vpn.conf
42
43if HAVE_MHD
44 VPN_TEST = \
45 test_gnunet_vpn-4_to_6 \
46 test_gnunet_vpn-6_to_4 \
47 test_gnunet_vpn-6_over \
48 test_gnunet_vpn-4_over \
49 test_gns_vpn
50endif
51
52
53test_gns_vpn_SOURCES = \
54 test_gns_vpn.c
55test_gns_vpn_LDADD = -lmicrohttpd @LIBCURL@ \
56 $(top_builddir)/src/namestore/libgnunetnamestore.la \
57 $(top_builddir)/src/testing/libgnunettesting.la \
58 $(top_builddir)/src/util/libgnunetutil.la
59test_gnunet_vpn_4_over_CPPFLAGS = \
60 @LIBCURL_CPPFLAGS@
61
62test_gnunet_vpn_4_over_SOURCES = \
63 test_gnunet_vpn.c
64test_gnunet_vpn_4_over_LDADD = -lmicrohttpd @LIBCURL@ \
65 $(top_builddir)/src/vpn/libgnunetvpn.la \
66 $(top_builddir)/src/testing/libgnunettesting.la \
67 $(top_builddir)/src/util/libgnunetutil.la
68test_gnunet_vpn_4_over_CPPFLAGS = \
69 @LIBCURL_CPPFLAGS@
70
71test_gnunet_vpn_6_over_SOURCES = \
72 test_gnunet_vpn.c
73test_gnunet_vpn_6_over_LDADD = -lmicrohttpd @LIBCURL@ \
74 $(top_builddir)/src/vpn/libgnunetvpn.la \
75 $(top_builddir)/src/testing/libgnunettesting.la \
76 $(top_builddir)/src/util/libgnunetutil.la
77test_gnunet_vpn_6_over_CPPFLAGS = \
78 @LIBCURL_CPPFLAGS@
79
80test_gnunet_vpn_4_to_6_SOURCES = \
81 test_gnunet_vpn.c
82test_gnunet_vpn_4_to_6_LDADD = -lmicrohttpd @LIBCURL@ \
83 $(top_builddir)/src/vpn/libgnunetvpn.la \
84 $(top_builddir)/src/testing/libgnunettesting.la \
85 $(top_builddir)/src/util/libgnunetutil.la
86test_gnunet_vpn_4_to_6_CPPFLAGS = \
87 @LIBCURL_CPPFLAGS@
88
89test_gnunet_vpn_6_to_4_SOURCES = \
90 test_gnunet_vpn.c
91test_gnunet_vpn_6_to_4_LDADD = -lmicrohttpd @LIBCURL@ \
92 $(top_builddir)/src/vpn/libgnunetvpn.la \
93 $(top_builddir)/src/testing/libgnunettesting.la \
94 $(top_builddir)/src/util/libgnunetutil.la
95test_gnunet_vpn_6_to_4_CPPFLAGS = \
96 @LIBCURL_CPPFLAGS@
97
diff --git a/src/pt/test_gns_vpn.c b/src/pt/test_gns_vpn.c
new file mode 100644
index 000000000..0c6e42500
--- /dev/null
+++ b/src/pt/test_gns_vpn.c
@@ -0,0 +1,491 @@
1/*
2 This file is part of GNUnet
3 (C) 2007, 2009, 2011, 2012 Christian Grothoff
4
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.
9
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.
14
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.
19*/
20
21/**
22 * @file test_gns_vpn.c
23 * @brief testcase for accessing VPN services via GNS
24 * @author Martin Schanzenbach
25 */
26#include "platform.h"
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
33#define PORT 8080
34#define TEST_DOMAIN "www.gnunet"
35
36#define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 300)
37
38/**
39 * Return value for 'main'.
40 */
41static int global_ret;
42
43static struct GNUNET_NAMESTORE_Handle *namestore;
44
45static struct MHD_Daemon *mhd;
46
47static GNUNET_SCHEDULER_TaskIdentifier mhd_task_id;
48
49static GNUNET_SCHEDULER_TaskIdentifier curl_task_id;
50
51static CURL *curl;
52
53static CURLM *multi;
54
55static char *url;
56
57/**
58 * IP address of the ultimate destination.
59 */
60static const char *dest_ip;
61
62/**
63 * Address family of the dest_ip.
64 */
65static int dest_af;
66
67/**
68 * Address family to use by the curl client.
69 */
70static int src_af;
71
72static int use_v6;
73
74
75struct CBC
76{
77 char buf[1024];
78 size_t pos;
79};
80
81static struct CBC cbc;
82
83
84static size_t
85copy_buffer (void *ptr, size_t size, size_t nmemb, void *ctx)
86{
87 struct CBC *cbc = ctx;
88
89 if (cbc->pos + size * nmemb > sizeof(cbc->buf))
90 return 0; /* overflow */
91 memcpy (&cbc->buf[cbc->pos], ptr, size * nmemb);
92 cbc->pos += size * nmemb;
93 return size * nmemb;
94}
95
96
97static int
98mhd_ahc (void *cls,
99 struct MHD_Connection *connection,
100 const char *url,
101 const char *method,
102 const char *version,
103 const char *upload_data, size_t *upload_data_size,
104 void **unused)
105{
106 static int ptr;
107 struct MHD_Response *response;
108 int ret;
109
110 if (0 != strcmp ("GET", method))
111 return MHD_NO; /* unexpected method */
112 if (&ptr != *unused)
113 {
114 *unused = &ptr;
115 return MHD_YES;
116 }
117 *unused = NULL;
118 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MHD sends respose for request to URL `%s'\n", url);
119 response = MHD_create_response_from_buffer (strlen (url),
120 (void *) url,
121 MHD_RESPMEM_MUST_COPY);
122 ret = MHD_queue_response (connection, MHD_HTTP_OK, response);
123 MHD_destroy_response (response);
124 if (ret == MHD_NO)
125 abort ();
126 return ret;
127}
128
129
130static void
131do_shutdown ()
132{
133 if (mhd_task_id != GNUNET_SCHEDULER_NO_TASK)
134 {
135 GNUNET_SCHEDULER_cancel (mhd_task_id);
136 mhd_task_id = GNUNET_SCHEDULER_NO_TASK;
137 }
138 if (curl_task_id != GNUNET_SCHEDULER_NO_TASK)
139 {
140 GNUNET_SCHEDULER_cancel (curl_task_id);
141 curl_task_id = GNUNET_SCHEDULER_NO_TASK;
142 }
143 if (NULL != mhd)
144 {
145 MHD_stop_daemon (mhd);
146 mhd = NULL;
147 }
148 GNUNET_free_non_null (url);
149 url = NULL;
150}
151
152
153/**
154 * Function to run the HTTP client.
155 */
156static void
157curl_main (void);
158
159
160static void
161curl_task (void *cls,
162 const struct GNUNET_SCHEDULER_TaskContext *tc)
163{
164 curl_task_id = GNUNET_SCHEDULER_NO_TASK;
165 curl_main ();
166}
167
168
169static void
170curl_main ()
171{
172 fd_set rs;
173 fd_set ws;
174 fd_set es;
175 int max;
176 struct GNUNET_NETWORK_FDSet nrs;
177 struct GNUNET_NETWORK_FDSet nws;
178 struct GNUNET_TIME_Relative delay;
179 long timeout;
180 int running;
181 struct CURLMsg *msg;
182
183 max = 0;
184 FD_ZERO (&rs);
185 FD_ZERO (&ws);
186 FD_ZERO (&es);
187 curl_multi_perform (multi, &running);
188 if (running == 0)
189 {
190 GNUNET_assert (NULL != (msg = curl_multi_info_read (multi, &running)));
191 if (msg->msg == CURLMSG_DONE)
192 {
193 if (msg->data.result != CURLE_OK)
194 {
195 fprintf (stderr,
196 "%s failed at %s:%d: `%s'\n",
197 "curl_multi_perform",
198 __FILE__,
199 __LINE__, curl_easy_strerror (msg->data.result));
200 global_ret = 1;
201 }
202 }
203 curl_multi_remove_handle (multi, curl);
204 curl_multi_cleanup (multi);
205 curl_easy_cleanup (curl);
206 curl = NULL;
207 multi = NULL;
208 if (cbc.pos != strlen ("/hello_world"))
209 {
210 GNUNET_break (0);
211 global_ret = 2;
212 }
213 if (0 != strncmp ("/hello_world", cbc.buf, strlen ("/hello_world")))
214 {
215 GNUNET_break (0);
216 global_ret = 3;
217 }
218 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Download complete, shutting down!\n");
219 do_shutdown ();
220 return;
221 }
222 GNUNET_assert (CURLM_OK == curl_multi_fdset (multi, &rs, &ws, &es, &max));
223 if ( (CURLM_OK != curl_multi_timeout (multi, &timeout)) ||
224 (-1 == timeout) )
225 delay = GNUNET_TIME_UNIT_SECONDS;
226 else
227 delay = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, (unsigned int) timeout);
228 GNUNET_NETWORK_fdset_copy_native (&nrs,
229 &rs,
230 max + 1);
231 GNUNET_NETWORK_fdset_copy_native (&nws,
232 &ws,
233 max + 1);
234 curl_task_id = GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
235 delay,
236 &nrs,
237 &nws,
238 &curl_task,
239 NULL);
240}
241
242static void
243start_curl (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
244{
245 GNUNET_asprintf (&url,
246 "http://%s/hello_world",
247 TEST_DOMAIN);
248 curl = curl_easy_init ();
249 curl_easy_setopt (curl, CURLOPT_URL, url);
250 curl_easy_setopt (curl, CURLOPT_WRITEFUNCTION, &copy_buffer);
251 curl_easy_setopt (curl, CURLOPT_WRITEDATA, &cbc);
252 curl_easy_setopt (curl, CURLOPT_FAILONERROR, 1);
253 curl_easy_setopt (curl, CURLOPT_TIMEOUT, 150L);
254 curl_easy_setopt (curl, CURLOPT_CONNECTTIMEOUT, 15L);
255 curl_easy_setopt (curl, CURLOPT_NOSIGNAL, 1);
256
257 multi = curl_multi_init ();
258 GNUNET_assert (multi != NULL);
259 GNUNET_assert (CURLM_OK == curl_multi_add_handle (multi, curl));
260 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Beginning HTTP download from `%s'\n", url);
261 curl_main ();
262}
263
264/**
265 * Callback invoked from the namestore service once record is
266 * created.
267 *
268 * @param cls closure
269 * @param af address family, AF_INET or AF_INET6; AF_UNSPEC on error;
270 * will match 'result_af' from the request
271 * @param address IP address (struct in_addr or struct in_addr6, depending on 'af')
272 * that the VPN allocated for the redirection;
273 * traffic to this IP will now be redirected to the
274 * specified target peer; NULL on error
275 */
276static void
277commence_testing (void *cls, int32_t success, const char *emsg)
278{
279 GNUNET_NAMESTORE_disconnect (namestore);
280
281 if ((emsg != NULL) && (GNUNET_YES != success))
282 {
283 fprintf (stderr,
284 "NS failed to create record %s\n", emsg);
285 GNUNET_SCHEDULER_shutdown ();
286 return;
287 }
288
289 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10), start_curl, NULL);
290
291}
292
293
294
295
296/**
297 * Function to keep the HTTP server running.
298 */
299static void
300mhd_main (void);
301
302
303static void
304mhd_task (void *cls,
305 const struct GNUNET_SCHEDULER_TaskContext *tc)
306{
307 mhd_task_id = GNUNET_SCHEDULER_NO_TASK;
308 MHD_run (mhd);
309 mhd_main ();
310}
311
312
313static void
314mhd_main ()
315{
316 struct GNUNET_NETWORK_FDSet nrs;
317 struct GNUNET_NETWORK_FDSet nws;
318 fd_set rs;
319 fd_set ws;
320 fd_set es;
321 int max_fd;
322 unsigned MHD_LONG_LONG timeout;
323 struct GNUNET_TIME_Relative delay;
324
325 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == mhd_task_id);
326 FD_ZERO (&rs);
327 FD_ZERO (&ws);
328 FD_ZERO (&es);
329 max_fd = -1;
330 GNUNET_assert (MHD_YES ==
331 MHD_get_fdset (mhd, &rs, &ws, &es, &max_fd));
332 if (MHD_YES == MHD_get_timeout (mhd, &timeout))
333 delay = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
334 (unsigned int) timeout);
335 else
336 delay = GNUNET_TIME_UNIT_FOREVER_REL;
337 GNUNET_NETWORK_fdset_copy_native (&nrs,
338 &rs,
339 max_fd + 1);
340 GNUNET_NETWORK_fdset_copy_native (&nws,
341 &ws,
342 max_fd + 1);
343 mhd_task_id = GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
344 delay,
345 &nrs,
346 &nws,
347 &mhd_task,
348 NULL);
349}
350
351static void
352run (void *cls,
353 const struct GNUNET_CONFIGURATION_Handle *cfg,
354 struct GNUNET_TESTING_Peer *peer)
355{
356 enum MHD_FLAG flags;
357 struct GNUNET_PeerIdentity id;
358 struct GNUNET_CRYPTO_HashAsciiEncoded peername;
359 struct GNUNET_CRYPTO_RsaPrivateKey *host_key;
360 struct GNUNET_NAMESTORE_RecordData rd;
361 char *rd_string;
362 char *zone_keyfile;
363
364 GNUNET_TESTING_peer_get_identity (peer, &id);
365 GNUNET_CRYPTO_hash_to_enc ((struct GNUNET_HashCode*)&id, &peername);
366
367 namestore = GNUNET_NAMESTORE_connect (cfg);
368 GNUNET_assert (NULL != namestore);
369 flags = MHD_USE_DEBUG;
370 //if (GNUNET_YES == use_v6)
371 // flags |= MHD_USE_IPv6;
372 mhd = MHD_start_daemon (flags,
373 PORT,
374 NULL, NULL,
375 &mhd_ahc, NULL,
376 MHD_OPTION_END);
377 GNUNET_assert (NULL != mhd);
378 mhd_main ();
379
380 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "gns",
381 "ZONEKEY",
382 &zone_keyfile))
383 {
384 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Failed to get key from cfg\n");
385 return;
386 }
387
388 host_key = GNUNET_CRYPTO_rsa_key_create_from_file (zone_keyfile);
389 rd.expiration_time = GNUNET_TIME_UNIT_FOREVER_ABS.abs_value;
390 GNUNET_asprintf (&rd_string, "6 %s %s", (char*)&peername, "www.gnunet.");
391 GNUNET_assert (GNUNET_OK == GNUNET_NAMESTORE_string_to_value (GNUNET_GNS_RECORD_VPN,
392 rd_string,
393 (void**)&rd.data,
394 &rd.data_size));
395 rd.record_type = GNUNET_GNS_RECORD_VPN;
396
397 GNUNET_NAMESTORE_record_create (namestore,
398 host_key,
399 "www",
400 &rd,
401 &commence_testing,
402 NULL);
403 GNUNET_free ((void**)rd.data);
404 GNUNET_free (rd_string);
405 GNUNET_free (zone_keyfile);
406 GNUNET_CRYPTO_rsa_key_free (host_key);
407}
408
409
410/**
411 * Test if the given AF is supported by this system.
412 *
413 * @param af to test
414 * @return GNUNET_OK if the AF is supported
415 */
416static int
417test_af (int af)
418{
419 int s;
420
421 s = socket (af, SOCK_STREAM, 0);
422 if (-1 == s)
423 {
424 if (EAFNOSUPPORT == errno)
425 return GNUNET_NO;
426 fprintf (stderr, "Failed to create test socket: %s\n", STRERROR (errno));
427 return GNUNET_SYSERR;
428 }
429 close (s);
430 return GNUNET_OK;
431}
432
433
434int
435main (int argc, char *const *argv)
436{
437 if (0 != ACCESS ("/dev/net/tun", R_OK))
438 {
439 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR,
440 "access",
441 "/dev/net/tun");
442 fprintf (stderr,
443 "WARNING: System unable to run test, skipping.\n");
444 return 0;
445 }
446 if ( (GNUNET_YES !=
447 GNUNET_OS_check_helper_binary ("gnunet-helper-vpn")) ||
448 (GNUNET_YES !=
449 GNUNET_OS_check_helper_binary ("gnunet-helper-exit")) ||
450 (GNUNET_YES !=
451 GNUNET_OS_check_helper_binary ("gnunet-helper-dns")))
452 {
453 fprintf (stderr,
454 "WARNING: gnunet-helper-{exit,vpn,dns} binaries in $PATH are not SUID, refusing to run test (as it would have to fail).\n");
455 fprintf (stderr,
456 "Change $PATH ('.' in $PATH before $GNUNET_PREFIX/bin is problematic) or permissions (run 'make install' as root) to fix this!\n");
457 return 0;
458 }
459 GNUNET_CRYPTO_setup_hostkey ("test_gns_vpn.conf");
460
461 dest_ip = "169.254.86.1";
462 dest_af = AF_INET;
463 src_af = AF_INET;
464
465 if (GNUNET_OK == test_af (AF_INET6))
466 use_v6 = GNUNET_YES;
467 else
468 use_v6 = GNUNET_NO;
469
470 if ( (GNUNET_OK != test_af (src_af)) ||
471 (GNUNET_OK != test_af (dest_af)) )
472 {
473 fprintf (stderr,
474 "Required address families not supported by this system, skipping test.\n");
475 return 0;
476 }
477 if (0 != curl_global_init (CURL_GLOBAL_WIN32))
478 {
479 fprintf (stderr, "failed to initialize curl\n");
480 return 2;
481 }
482 if (0 != GNUNET_TESTING_peer_run ("test-gnunet-vpn",
483 "test_gns_vpn.conf",
484 &run, NULL))
485 return 1;
486 GNUNET_DISK_directory_remove ("/tmp/gnunet-test-vpn");
487 return global_ret;
488}
489
490/* end of test_gns_vpn.c */
491
diff --git a/src/pt/test_gns_vpn.conf b/src/pt/test_gns_vpn.conf
new file mode 100644
index 000000000..4c1425793
--- /dev/null
+++ b/src/pt/test_gns_vpn.conf
@@ -0,0 +1,52 @@
1[PATHS]
2SERVICEHOME = /tmp/gnunet-test-vpn/
3DEFAULTCONFIG = test_gnunet_vpn.conf
4
5[transport]
6PLUGINS = tcp
7
8[arm]
9DEFAULTSERVICES = statistics exit mesh vpn namestore gns
10PORT = 0
11ALLOW_SHUTDOWN = YES
12
13[exit]
14EXIT_IPV4 = YES
15EXIT_IPV6 = YES
16
17# FIXME: can we use 'lo'?
18EXIT_IFNAME = eth1
19
20[testing]
21WEAKRANDOM = YES
22HOSTKEYSFILE = ../../contrib/testing_hostkeys.dat
23
24[nse]
25WORKBITS = 1
26
27# repeating some values from the default configurations
28# here as the respective network addresses are also
29# hard-wired in the tests and the MUST match (!)
30[vpn]
31IPV6ADDR = FC2D:FDAA:6A26::1
32IPV6PREFIX = 64
33IPV4ADDR = 169.254.20.1
34IPV4MASK = 255.255.255.0
35
36[exit]
37IPV6ADDR = FC5A:04E1:C2BA::1
38IPV6PREFIX = 96
39IPV4ADDR = 169.254.86.1
40IPV4MASK = 255.255.255.0
41
42[www.gnunet.]
43TCP_REDIRECTS = 80:localhost4:8080
44TTL = 3600000
45
46[gns]
47AUTOSTART = YES
48ZONEKEY = $SERVICEHOME/hostkey
49HIJACK_DNS = YES
50
51[namestore]
52AUTOSTART = YES
diff --git a/src/pt/test_gnunet_vpn.c b/src/pt/test_gnunet_vpn.c
new file mode 100644
index 000000000..d2cfc757b
--- /dev/null
+++ b/src/pt/test_gnunet_vpn.c
@@ -0,0 +1,535 @@
1/*
2 This file is part of GNUnet
3 (C) 2007, 2009, 2011, 2012 Christian Grothoff
4
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.
9
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.
14
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.
19*/
20
21/**
22 * @file test_gnunet_vpn.c
23 * @brief testcase for tunneling HTTP over the GNUnet VPN
24 * @author Christian Grothoff
25 */
26#include "platform.h"
27#include <curl/curl.h>
28#include <microhttpd.h>
29#include "gnunet_vpn_service.h"
30#include "gnunet_testing_lib-new.h"
31
32#define PORT 48080
33
34#define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 300)
35
36
37/**
38 * Return value for 'main'.
39 */
40static int global_ret;
41
42static struct GNUNET_VPN_Handle *vpn;
43
44static struct MHD_Daemon *mhd;
45
46static GNUNET_SCHEDULER_TaskIdentifier mhd_task_id;
47
48static GNUNET_SCHEDULER_TaskIdentifier curl_task_id;
49
50static GNUNET_SCHEDULER_TaskIdentifier ctrl_c_task_id;
51
52static struct GNUNET_VPN_RedirectionRequest *rr;
53
54static CURL *curl;
55
56static CURLM *multi;
57
58static char *url;
59
60/**
61 * IP address of the ultimate destination.
62 */
63static const char *dest_ip;
64
65/**
66 * Address family of the dest_ip.
67 */
68static int dest_af;
69
70/**
71 * Address family to use by the curl client.
72 */
73static int src_af;
74
75
76struct CBC
77{
78 char buf[1024];
79 size_t pos;
80};
81
82static struct CBC cbc;
83
84
85static size_t
86copy_buffer (void *ptr, size_t size, size_t nmemb, void *ctx)
87{
88 struct CBC *cbc = ctx;
89
90 if (cbc->pos + size * nmemb > sizeof(cbc->buf))
91 return 0; /* overflow */
92 memcpy (&cbc->buf[cbc->pos], ptr, size * nmemb);
93 cbc->pos += size * nmemb;
94 return size * nmemb;
95}
96
97
98static int
99mhd_ahc (void *cls,
100 struct MHD_Connection *connection,
101 const char *url,
102 const char *method,
103 const char *version,
104 const char *upload_data, size_t *upload_data_size,
105 void **unused)
106{
107 static int ptr;
108 struct MHD_Response *response;
109 int ret;
110
111 if (0 != strcmp ("GET", method))
112 return MHD_NO; /* unexpected method */
113 if (&ptr != *unused)
114 {
115 *unused = &ptr;
116 return MHD_YES;
117 }
118 *unused = NULL;
119 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MHD sends respose for request to URL `%s'\n", url);
120 response = MHD_create_response_from_buffer (strlen (url),
121 (void *) url,
122 MHD_RESPMEM_MUST_COPY);
123 ret = MHD_queue_response (connection, MHD_HTTP_OK, response);
124 MHD_destroy_response (response);
125 if (ret == MHD_NO)
126 abort ();
127 return ret;
128}
129
130
131static void
132do_shutdown ()
133{
134 if (mhd_task_id != GNUNET_SCHEDULER_NO_TASK)
135 {
136 GNUNET_SCHEDULER_cancel (mhd_task_id);
137 mhd_task_id = GNUNET_SCHEDULER_NO_TASK;
138 }
139 if (curl_task_id != GNUNET_SCHEDULER_NO_TASK)
140 {
141 GNUNET_SCHEDULER_cancel (curl_task_id);
142 curl_task_id = GNUNET_SCHEDULER_NO_TASK;
143 }
144 if (ctrl_c_task_id != GNUNET_SCHEDULER_NO_TASK)
145 {
146 GNUNET_SCHEDULER_cancel (ctrl_c_task_id);
147 ctrl_c_task_id = GNUNET_SCHEDULER_NO_TASK;
148 }
149 if (NULL != mhd)
150 {
151 MHD_stop_daemon (mhd);
152 mhd = NULL;
153 }
154 if (NULL != rr)
155 {
156 GNUNET_VPN_cancel_request (rr);
157 rr = NULL;
158 }
159 if (NULL != vpn)
160 {
161 GNUNET_VPN_disconnect (vpn);
162 vpn = NULL;
163 }
164 GNUNET_free_non_null (url);
165 url = NULL;
166}
167
168
169/**
170 * Function to run the HTTP client.
171 */
172static void
173curl_main (void);
174
175
176static void
177curl_task (void *cls,
178 const struct GNUNET_SCHEDULER_TaskContext *tc)
179{
180 curl_task_id = GNUNET_SCHEDULER_NO_TASK;
181 curl_main ();
182}
183
184
185static void
186curl_main ()
187{
188 fd_set rs;
189 fd_set ws;
190 fd_set es;
191 int max;
192 struct GNUNET_NETWORK_FDSet nrs;
193 struct GNUNET_NETWORK_FDSet nws;
194 struct GNUNET_TIME_Relative delay;
195 long timeout;
196 int running;
197 struct CURLMsg *msg;
198
199 max = 0;
200 FD_ZERO (&rs);
201 FD_ZERO (&ws);
202 FD_ZERO (&es);
203 curl_multi_perform (multi, &running);
204 if (running == 0)
205 {
206 GNUNET_assert (NULL != (msg = curl_multi_info_read (multi, &running)));
207 if (msg->msg == CURLMSG_DONE)
208 {
209 if (msg->data.result != CURLE_OK)
210 {
211 fprintf (stderr,
212 "%s failed at %s:%d: `%s'\n",
213 "curl_multi_perform",
214 __FILE__,
215 __LINE__, curl_easy_strerror (msg->data.result));
216 global_ret = 1;
217 }
218 }
219 curl_multi_remove_handle (multi, curl);
220 curl_multi_cleanup (multi);
221 curl_easy_cleanup (curl);
222 curl = NULL;
223 multi = NULL;
224 if (cbc.pos != strlen ("/hello_world"))
225 {
226 GNUNET_break (0);
227 global_ret = 2;
228 }
229 if (0 != strncmp ("/hello_world", cbc.buf, strlen ("/hello_world")))
230 {
231 GNUNET_break (0);
232 global_ret = 3;
233 }
234 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Download complete, shutting down!\n");
235 do_shutdown ();
236 return;
237 }
238 GNUNET_assert (CURLM_OK == curl_multi_fdset (multi, &rs, &ws, &es, &max));
239 if ( (CURLM_OK != curl_multi_timeout (multi, &timeout)) ||
240 (-1 == timeout) )
241 delay = GNUNET_TIME_UNIT_SECONDS;
242 else
243 delay = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, (unsigned int) timeout);
244 GNUNET_NETWORK_fdset_copy_native (&nrs,
245 &rs,
246 max + 1);
247 GNUNET_NETWORK_fdset_copy_native (&nws,
248 &ws,
249 max + 1);
250 curl_task_id = GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
251 delay,
252 &nrs,
253 &nws,
254 &curl_task,
255 NULL);
256}
257
258
259/**
260 * Callback invoked from the VPN service once a redirection is
261 * available. Provides the IP address that can now be used to
262 * reach the requested destination (in our case, the MHD server)
263 *
264 * @param cls closure
265 * @param af address family, AF_INET or AF_INET6; AF_UNSPEC on error;
266 * will match 'result_af' from the request
267 * @param address IP address (struct in_addr or struct in_addr6, depending on 'af')
268 * that the VPN allocated for the redirection;
269 * traffic to this IP will now be redirected to the
270 * specified target peer; NULL on error
271 */
272static void
273allocation_cb (void *cls,
274 int af,
275 const void *address)
276{
277 char ips[INET6_ADDRSTRLEN];
278
279 rr = NULL;
280 if (src_af != af)
281 {
282 fprintf (stderr,
283 "VPN failed to allocate appropriate address\n");
284 GNUNET_SCHEDULER_shutdown ();
285 return;
286 }
287 GNUNET_asprintf (&url,
288 "http://%s:%u/hello_world",
289 inet_ntop (af, address, ips, sizeof (ips)),
290 (unsigned int) PORT);
291 curl = curl_easy_init ();
292 curl_easy_setopt (curl, CURLOPT_URL, url);
293 curl_easy_setopt (curl, CURLOPT_WRITEFUNCTION, &copy_buffer);
294 curl_easy_setopt (curl, CURLOPT_WRITEDATA, &cbc);
295 curl_easy_setopt (curl, CURLOPT_FAILONERROR, 1);
296 curl_easy_setopt (curl, CURLOPT_TIMEOUT, 150L);
297 curl_easy_setopt (curl, CURLOPT_CONNECTTIMEOUT, 15L);
298 curl_easy_setopt (curl, CURLOPT_NOSIGNAL, 1);
299
300 multi = curl_multi_init ();
301 GNUNET_assert (multi != NULL);
302 GNUNET_assert (CURLM_OK == curl_multi_add_handle (multi, curl));
303 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Beginning HTTP download from `%s'\n", url);
304 curl_main ();
305}
306
307
308/**
309 * Function to keep the HTTP server running.
310 */
311static void
312mhd_main (void);
313
314
315static void
316mhd_task (void *cls,
317 const struct GNUNET_SCHEDULER_TaskContext *tc)
318{
319 mhd_task_id = GNUNET_SCHEDULER_NO_TASK;
320 MHD_run (mhd);
321 mhd_main ();
322}
323
324
325static void
326ctrl_c_shutdown (void *cls,
327 const struct GNUNET_SCHEDULER_TaskContext *tc)
328{
329 ctrl_c_task_id = GNUNET_SCHEDULER_NO_TASK;
330 do_shutdown ();
331 GNUNET_break (0);
332 global_ret = 1;
333}
334
335
336static void
337mhd_main ()
338{
339 struct GNUNET_NETWORK_FDSet nrs;
340 struct GNUNET_NETWORK_FDSet nws;
341 fd_set rs;
342 fd_set ws;
343 fd_set es;
344 int max_fd;
345 unsigned MHD_LONG_LONG timeout;
346 struct GNUNET_TIME_Relative delay;
347
348 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == mhd_task_id);
349 FD_ZERO (&rs);
350 FD_ZERO (&ws);
351 FD_ZERO (&es);
352 max_fd = -1;
353 GNUNET_assert (MHD_YES ==
354 MHD_get_fdset (mhd, &rs, &ws, &es, &max_fd));
355 if (MHD_YES == MHD_get_timeout (mhd, &timeout))
356 delay = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
357 (unsigned int) timeout);
358 else
359 delay = GNUNET_TIME_UNIT_FOREVER_REL;
360 GNUNET_NETWORK_fdset_copy_native (&nrs,
361 &rs,
362 max_fd + 1);
363 GNUNET_NETWORK_fdset_copy_native (&nws,
364 &ws,
365 max_fd + 1);
366 mhd_task_id = GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
367 delay,
368 &nrs,
369 &nws,
370 &mhd_task,
371 NULL);
372}
373
374
375static void
376run (void *cls,
377 const struct GNUNET_CONFIGURATION_Handle *cfg,
378 struct GNUNET_TESTING_Peer *peer)
379{
380 struct in_addr v4;
381 struct in6_addr v6;
382 void *addr;
383 enum MHD_FLAG flags;
384
385 vpn = GNUNET_VPN_connect (cfg);
386 GNUNET_assert (NULL != vpn);
387 flags = MHD_USE_DEBUG;
388 if (AF_INET6 == dest_af)
389 flags |= MHD_USE_IPv6;
390 mhd = MHD_start_daemon (flags,
391 PORT,
392 NULL, NULL,
393 &mhd_ahc, NULL,
394 MHD_OPTION_END);
395 GNUNET_assert (NULL != mhd);
396 mhd_main ();
397 addr = NULL;
398 switch (dest_af)
399 {
400 case AF_INET:
401 GNUNET_assert (1 == inet_pton (dest_af, dest_ip, &v4));
402 addr = &v4;
403 break;
404 case AF_INET6:
405 GNUNET_assert (1 == inet_pton (dest_af, dest_ip, &v6));
406 addr = &v6;
407 break;
408 default:
409 GNUNET_assert (0);
410 }
411 rr = GNUNET_VPN_redirect_to_ip (vpn,
412 src_af,
413 dest_af,
414 addr,
415 GNUNET_YES,
416 GNUNET_TIME_UNIT_FOREVER_ABS,
417 &allocation_cb, NULL);
418 ctrl_c_task_id = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
419 &ctrl_c_shutdown,
420 NULL);
421}
422
423
424/**
425 * Test if the given AF is supported by this system.
426 *
427 * @param af to test
428 * @return GNUNET_OK if the AF is supported
429 */
430static int
431test_af (int af)
432{
433 int s;
434
435 s = socket (af, SOCK_STREAM, 0);
436 if (-1 == s)
437 {
438 if (EAFNOSUPPORT == errno)
439 return GNUNET_NO;
440 fprintf (stderr, "Failed to create test socket: %s\n", STRERROR (errno));
441 return GNUNET_SYSERR;
442 }
443 close (s);
444 return GNUNET_OK;
445}
446
447
448int
449main (int argc, char *const *argv)
450{
451 const char *type;
452 const char *bin;
453
454 if (0 != ACCESS ("/dev/net/tun", R_OK))
455 {
456 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR,
457 "access",
458 "/dev/net/tun");
459 fprintf (stderr,
460 "WARNING: System unable to run test, skipping.\n");
461 return 0;
462 }
463 if ( (GNUNET_YES !=
464 GNUNET_OS_check_helper_binary ("gnunet-helper-vpn")) ||
465 (GNUNET_YES !=
466 GNUNET_OS_check_helper_binary ("gnunet-helper-exit")) )
467 {
468 fprintf (stderr,
469 "WARNING: gnunet-helper-{exit,vpn} binaries in $PATH are not SUID, refusing to run test (as it would have to fail).\n");
470 fprintf (stderr,
471 "Change $PATH ('.' in $PATH before $GNUNET_PREFIX/bin is problematic) or permissions (run 'make install' as root) to fix this!\n");
472 return 0;
473 }
474 GNUNET_CRYPTO_setup_hostkey ("test_gnunet_vpn.conf");
475 bin = argv[0];
476 if (NULL != strstr (bin, "lt-"))
477 bin = strstr (bin, "lt-") + 4;
478 type = strstr (bin, "-");
479 if (NULL == type)
480 {
481 fprintf (stderr, "invalid binary name\n");
482 return 1;
483 }
484 type++;
485 if (0 == strcmp (type, "4_to_6"))
486 {
487 dest_ip = "FC5A:04E1:C2BA::1";
488 dest_af = AF_INET6;
489 src_af = AF_INET;
490 }
491 else if (0 == strcmp (type, "6_to_4"))
492 {
493 dest_ip = "169.254.86.1";
494 dest_af = AF_INET;
495 src_af = AF_INET6;
496 }
497 else if (0 == strcmp (type, "4_over"))
498 {
499 dest_ip = "169.254.86.1";
500 dest_af = AF_INET;
501 src_af = AF_INET;
502 }
503 else if (0 == strcmp (type, "6_over"))
504 {
505 dest_ip = "FC5A:04E1:C2BA::1";
506 dest_af = AF_INET6;
507 src_af = AF_INET6;
508 }
509 else
510 {
511 fprintf (stderr, "invalid binary suffix `%s'\n", type);
512 return 1;
513 }
514 if ( (GNUNET_OK != test_af (src_af)) ||
515 (GNUNET_OK != test_af (dest_af)) )
516 {
517 fprintf (stderr,
518 "Required address families not supported by this system, skipping test.\n");
519 return 0;
520 }
521 if (0 != curl_global_init (CURL_GLOBAL_WIN32))
522 {
523 fprintf (stderr, "failed to initialize curl\n");
524 return 2;
525 }
526 if (0 != GNUNET_TESTING_peer_run ("test-gnunet-vpn",
527 "test_gnunet_vpn.conf",
528 &run, NULL))
529 return 1;
530 GNUNET_DISK_directory_remove ("/tmp/gnunet-test-vpn");
531 return global_ret;
532}
533
534/* end of test_gnunet_vpn.c */
535
diff --git a/src/pt/test_gnunet_vpn.conf b/src/pt/test_gnunet_vpn.conf
new file mode 100644
index 000000000..bdac1a5b6
--- /dev/null
+++ b/src/pt/test_gnunet_vpn.conf
@@ -0,0 +1,43 @@
1[PATHS]
2SERVICEHOME = /tmp/gnunet-test-vpn/
3DEFAULTCONFIG = test_gnunet_vpn.conf
4
5[transport]
6PLUGINS = tcp
7
8[arm]
9DEFAULTSERVICES = statistics exit vpn
10PORT = 0
11ALLOW_SHUTDOWN = YES
12
13[exit]
14EXIT_IPV4 = YES
15EXIT_IPV6 = YES
16
17# FIXME: can we use 'lo'?
18EXIT_IFNAME = eth1
19
20[testing]
21WEAKRANDOM = YES
22HOSTKEYSFILE = ../../contrib/testing_hostkeys.dat
23
24[nse]
25WORKBITS = 1
26
27# repeating some values from the default configurations
28# here as the respective network addresses are also
29# hard-wired in the tests and the MUST match (!)
30[vpn]
31IPV6ADDR = FC2D:FDAA:6A26::1
32IPV6PREFIX = 64
33IPV4ADDR = 169.254.20.1
34IPV4MASK = 255.255.255.0
35
36[exit]
37IPV6ADDR = FC5A:04E1:C2BA::1
38IPV6PREFIX = 96
39IPV4ADDR = 169.254.86.1
40IPV4MASK = 255.255.255.0
41
42[gns]
43AUTOSTART = NO