uncrustify as demanded.
[oweals/gnunet.git] / src / pt / test_gnunet_vpn.c
1 /*
2      This file is part of GNUnet
3      Copyright (C) 2007, 2009, 2011, 2012 Christian Grothoff
4
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.
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      Affero General Public License for more details.
14
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/>.
17
18      SPDX-License-Identifier: AGPL3.0-or-later
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 /* Just included for the right curl.h */
28 #include "gnunet_curl_lib.h"
29 #include <microhttpd.h>
30 #include "gnunet_vpn_service.h"
31 #include "gnunet_testing_lib.h"
32
33 #define PORT 48080
34
35 #define TIMEOUT GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 300)
36
37
38 /**
39  * Return value for 'main'.
40  */
41 static int global_ret;
42
43 static struct GNUNET_VPN_Handle *vpn;
44
45 static struct MHD_Daemon *mhd;
46
47 static struct GNUNET_SCHEDULER_Task *mhd_task_id;
48
49 static struct GNUNET_SCHEDULER_Task *curl_task_id;
50
51 static struct GNUNET_SCHEDULER_Task *timeout_task_id;
52
53 static struct GNUNET_VPN_RedirectionRequest *rr;
54
55 static CURL *curl;
56
57 static CURLM *multi;
58
59 static char *url;
60
61 /**
62  * IP address of the ultimate destination.
63  */
64 static const char *dest_ip;
65
66 /**
67  * Address family of the dest_ip.
68  */
69 static int dest_af;
70
71 /**
72  * Address family to use by the curl client.
73  */
74 static int src_af;
75
76
77 struct CBC {
78   char buf[1024];
79   size_t pos;
80 };
81
82 static struct CBC cbc;
83
84
85 static size_t
86 copy_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   GNUNET_memcpy(&cbc->buf[cbc->pos], ptr, size * nmemb);
93   cbc->pos += size * nmemb;
94   return size * nmemb;
95 }
96
97
98 static int
99 mhd_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,
105         size_t * upload_data_size,
106         void **unused)
107 {
108   static int ptr;
109   struct MHD_Response *response;
110   int ret;
111
112   if (0 != strcmp("GET", method))
113     return MHD_NO;              /* unexpected method */
114   if (&ptr != *unused)
115     {
116       *unused = &ptr;
117       return MHD_YES;
118     }
119   *unused = NULL;
120   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
121              "MHD sends respose for request to URL `%s'\n", url);
122   response =
123     MHD_create_response_from_buffer(strlen(url), (void *)url,
124                                     MHD_RESPMEM_MUST_COPY);
125   ret = MHD_queue_response(connection, MHD_HTTP_OK, response);
126   MHD_destroy_response(response);
127   if (ret == MHD_NO)
128     abort();
129   return ret;
130 }
131
132
133 static void
134 do_shutdown(void *cls)
135 {
136   if (NULL != mhd_task_id)
137     {
138       GNUNET_SCHEDULER_cancel(mhd_task_id);
139       mhd_task_id = NULL;
140     }
141   if (NULL != curl_task_id)
142     {
143       GNUNET_SCHEDULER_cancel(curl_task_id);
144       curl_task_id = NULL;
145     }
146   if (NULL != timeout_task_id)
147     {
148       GNUNET_SCHEDULER_cancel(timeout_task_id);
149       timeout_task_id = NULL;
150     }
151   if (NULL != mhd)
152     {
153       MHD_stop_daemon(mhd);
154       mhd = NULL;
155     }
156   if (NULL != rr)
157     {
158       GNUNET_VPN_cancel_request(rr);
159       rr = NULL;
160     }
161   if (NULL != vpn)
162     {
163       GNUNET_VPN_disconnect(vpn);
164       vpn = NULL;
165     }
166   GNUNET_free_non_null(url);
167   url = NULL;
168 }
169
170
171 /**
172  * Function to run the HTTP client.
173  */
174 static void
175 curl_main(void *cls)
176 {
177   fd_set rs;
178   fd_set ws;
179   fd_set es;
180   int max;
181   struct GNUNET_NETWORK_FDSet nrs;
182   struct GNUNET_NETWORK_FDSet nws;
183   struct GNUNET_TIME_Relative delay;
184   long timeout;
185   int running;
186   struct CURLMsg *msg;
187
188   curl_task_id = NULL;
189   max = 0;
190   FD_ZERO(&rs);
191   FD_ZERO(&ws);
192   FD_ZERO(&es);
193   curl_multi_perform(multi, &running);
194   if (running == 0)
195     {
196       GNUNET_assert(NULL != (msg = curl_multi_info_read(multi, &running)));
197       if (msg->msg == CURLMSG_DONE)
198         {
199           if (msg->data.result != CURLE_OK)
200             {
201               fprintf(stderr, "%s failed at %s:%d: `%s'\n", "curl_multi_perform",
202                       __FILE__, __LINE__, curl_easy_strerror(msg->data.result));
203               global_ret = 1;
204             }
205         }
206       curl_multi_remove_handle(multi, curl);
207       curl_multi_cleanup(multi);
208       curl_easy_cleanup(curl);
209       curl = NULL;
210       multi = NULL;
211       if (cbc.pos != strlen("/hello_world"))
212         {
213           GNUNET_break(0);
214           global_ret = 2;
215         }
216       if (0 != strncmp("/hello_world", cbc.buf, strlen("/hello_world")))
217         {
218           GNUNET_break(0);
219           GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
220                      "You might want to check if your host-based firewall is blocking the connections.\n");
221           global_ret = 3;
222         }
223       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Download complete, shutting down!\n");
224       GNUNET_SCHEDULER_shutdown();
225       return;
226     }
227   GNUNET_assert(CURLM_OK == curl_multi_fdset(multi, &rs, &ws, &es, &max));
228   if ((CURLM_OK != curl_multi_timeout(multi, &timeout)) || (-1 == timeout))
229     delay = GNUNET_TIME_UNIT_SECONDS;
230   else
231     delay =
232       GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS,
233                                     (unsigned int)timeout);
234   GNUNET_NETWORK_fdset_copy_native(&nrs, &rs, max + 1);
235   GNUNET_NETWORK_fdset_copy_native(&nws, &ws, max + 1);
236   curl_task_id =
237     GNUNET_SCHEDULER_add_select(GNUNET_SCHEDULER_PRIORITY_DEFAULT, delay,
238                                 &nrs, &nws, &curl_main, NULL);
239 }
240
241
242 /**
243  * Callback invoked from the VPN service once a redirection is
244  * available.  Provides the IP address that can now be used to
245  * reach the requested destination (in our case, the MHD server)
246  *
247  * @param cls closure
248  * @param af address family, AF_INET or AF_INET6; AF_UNSPEC on error;
249  *                will match 'result_af' from the request
250  * @param address IP address (struct in_addr or struct in_addr6, depending on 'af')
251  *                that the VPN allocated for the redirection;
252  *                traffic to this IP will now be redirected to the
253  *                specified target peer; NULL on error
254  */
255 static void
256 allocation_cb(void *cls, int af, const void *address)
257 {
258   char ips[INET6_ADDRSTRLEN];
259
260   rr = NULL;
261   if (src_af != af)
262     {
263       fprintf(stderr,
264               "VPN failed to allocate appropriate address\n");
265       GNUNET_SCHEDULER_shutdown();
266       return;
267     }
268   if (AF_INET6 == af)
269     GNUNET_asprintf(&url,
270                     "http://[%s]:%u/hello_world",
271                     inet_ntop(af,
272                               address,
273                               ips,
274                               sizeof(ips)),
275                     (unsigned int)PORT);
276   else
277     GNUNET_asprintf(&url,
278                     "http://%s:%u/hello_world",
279                     inet_ntop(af,
280                               address,
281                               ips,
282                               sizeof(ips)),
283                     (unsigned int)PORT);
284   curl = curl_easy_init();
285   curl_easy_setopt(curl, CURLOPT_URL, url);
286   curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &copy_buffer);
287   curl_easy_setopt(curl, CURLOPT_WRITEDATA, &cbc);
288   curl_easy_setopt(curl, CURLOPT_FAILONERROR, 1);
289   curl_easy_setopt(curl, CURLOPT_TIMEOUT, 150L);
290   curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 15L);
291   curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
292   curl_easy_setopt(curl, CURLOPT_VERBOSE, 0);
293
294   multi = curl_multi_init();
295   GNUNET_assert(multi != NULL);
296   GNUNET_assert(CURLM_OK == curl_multi_add_handle(multi, curl));
297   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
298              "Beginning HTTP download from `%s'\n",
299              url);
300   GNUNET_SCHEDULER_add_delayed(GNUNET_TIME_UNIT_SECONDS,
301                                &curl_main,
302                                NULL);
303 }
304
305
306 /**
307  * Function to keep the HTTP server running.
308  */
309 static void
310 mhd_main(void);
311
312
313 static void
314 mhd_task(void *cls)
315 {
316   mhd_task_id = NULL;
317   MHD_run(mhd);
318   mhd_main();
319 }
320
321
322 static void
323 do_timeout(void *cls)
324 {
325   timeout_task_id = NULL;
326   GNUNET_SCHEDULER_shutdown();
327   GNUNET_break(0);
328   global_ret = 1;
329 }
330
331
332 static void
333 mhd_main()
334 {
335   struct GNUNET_NETWORK_FDSet nrs;
336   struct GNUNET_NETWORK_FDSet nws;
337   fd_set rs;
338   fd_set ws;
339   fd_set es;
340   int max_fd;
341   unsigned MHD_LONG_LONG timeout;
342   struct GNUNET_TIME_Relative delay;
343
344   GNUNET_assert(NULL == mhd_task_id);
345   FD_ZERO(&rs);
346   FD_ZERO(&ws);
347   FD_ZERO(&es);
348   max_fd = -1;
349   GNUNET_assert(MHD_YES == MHD_get_fdset(mhd, &rs, &ws, &es, &max_fd));
350   if (MHD_YES == MHD_get_timeout(mhd, &timeout))
351     delay =
352       GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS,
353                                     (unsigned int)timeout);
354   else
355     delay = GNUNET_TIME_UNIT_FOREVER_REL;
356   GNUNET_NETWORK_fdset_copy_native(&nrs, &rs, max_fd + 1);
357   GNUNET_NETWORK_fdset_copy_native(&nws, &ws, max_fd + 1);
358   mhd_task_id =
359     GNUNET_SCHEDULER_add_select(GNUNET_SCHEDULER_PRIORITY_DEFAULT, delay,
360                                 &nrs, &nws, &mhd_task, NULL);
361 }
362
363
364 static void
365 run(void *cls,
366     const struct GNUNET_CONFIGURATION_Handle *cfg,
367     struct GNUNET_TESTING_Peer *peer)
368 {
369   struct in_addr v4;
370   struct in6_addr v6;
371   void *addr;
372   enum MHD_FLAG flags;
373
374   vpn = GNUNET_VPN_connect(cfg);
375   GNUNET_assert(NULL != vpn);
376   flags = MHD_USE_DEBUG;
377   if (AF_INET6 == dest_af)
378     flags |= MHD_USE_IPv6;
379   mhd =
380     MHD_start_daemon(flags, PORT, NULL, NULL, &mhd_ahc, NULL,
381                      MHD_OPTION_END);
382
383
384   GNUNET_assert(NULL != mhd);
385   mhd_main();
386   addr = NULL;
387   switch (dest_af)
388     {
389     case AF_INET:
390       GNUNET_assert(1 == inet_pton(dest_af, dest_ip, &v4));
391       addr = &v4;
392       break;
393
394     case AF_INET6:
395       GNUNET_assert(1 == inet_pton(dest_af, dest_ip, &v6));
396       addr = &v6;
397       break;
398
399     default:
400       GNUNET_assert(0);
401     }
402   rr = GNUNET_VPN_redirect_to_ip(vpn, src_af, dest_af, addr,
403                                  GNUNET_TIME_UNIT_FOREVER_ABS, &allocation_cb,
404                                  NULL);
405   timeout_task_id =
406     GNUNET_SCHEDULER_add_delayed(TIMEOUT,
407                                  &do_timeout,
408                                  NULL);
409   GNUNET_SCHEDULER_add_shutdown(&do_shutdown,
410                                 NULL);
411 }
412
413
414 int
415 main(int argc, char *const *argv)
416 {
417   const char *type;
418   const char *bin;
419   char *vpn_binary;
420   char *exit_binary;
421   int ret = 0;
422
423 #ifndef MINGW
424   if (0 != ACCESS("/dev/net/tun", R_OK))
425     {
426       GNUNET_log_strerror_file(GNUNET_ERROR_TYPE_ERROR,
427                                "access",
428                                "/dev/net/tun");
429       fprintf(stderr,
430               "WARNING: System unable to run test, skipping.\n");
431       return 77;
432     }
433 #endif
434   vpn_binary = GNUNET_OS_get_libexec_binary_path("gnunet-helper-vpn");
435   exit_binary = GNUNET_OS_get_libexec_binary_path("gnunet-helper-exit");
436   if ((GNUNET_YES != (ret = GNUNET_OS_check_helper_binary(vpn_binary, GNUNET_YES, "-d gnunet-vpn - - 169.1.3.3.7 255.255.255.0"))) ||  //ipv4 only please!
437       (GNUNET_YES != (ret = GNUNET_OS_check_helper_binary(exit_binary, GNUNET_YES, "-d gnunet-vpn - - - 169.1.3.3.7 255.255.255.0"))))  //no nat, ipv4 only
438     {
439       GNUNET_free(vpn_binary);
440       GNUNET_free(exit_binary);
441       fprintf(stderr,
442               "WARNING: gnunet-helper-{exit,vpn} binaries are not SUID, refusing to run test (as it would have to fail). %d\n", ret);
443       return 77;
444     }
445
446   GNUNET_free(vpn_binary);
447   GNUNET_free(exit_binary);
448   bin = argv[0];
449   if (NULL != strstr(bin, "lt-"))
450     bin = strstr(bin, "lt-") + 4;
451   type = strstr(bin, "-");
452   if (NULL == type)
453     {
454       fprintf(stderr,
455               "invalid binary name\n");
456       return 1;
457     }
458   type++;
459   /* on Windows, .exe is suffixed to these binaries,
460    * thus cease comparison after the 6th char.
461    */
462   if (0 == strncmp(type, "4_to_6", 6))
463     {
464       dest_ip = "FC5A:04E1:C2BA::1";
465       dest_af = AF_INET6;
466       src_af = AF_INET;
467     }
468   else if (0 == strncmp(type, "6_to_4", 6))
469     {
470       dest_ip = "169.254.86.1";
471       dest_af = AF_INET;
472       src_af = AF_INET6;
473     }
474   else if (0 == strncmp(type, "4_over", 6))
475     {
476       dest_ip = "169.254.86.1";
477       dest_af = AF_INET;
478       src_af = AF_INET;
479     }
480   else if (0 == strncmp(type, "6_over", 6))
481     {
482       dest_ip = "FC5A:04E1:C2BA::1";
483       dest_af = AF_INET6;
484       src_af = AF_INET6;
485     }
486   else
487     {
488       fprintf(stderr, "invalid binary suffix `%s'\n", type);
489       return 1;
490     }
491   if ((GNUNET_OK != GNUNET_NETWORK_test_pf(src_af)) ||
492       (GNUNET_OK != GNUNET_NETWORK_test_pf(dest_af)))
493     {
494       fprintf(stderr,
495               "Required address families not supported by this system, skipping test.\n");
496       return 0;
497     }
498   if (0 != curl_global_init(CURL_GLOBAL_WIN32))
499     {
500       fprintf(stderr, "failed to initialize curl\n");
501       return 2;
502     }
503   if (0 !=
504       GNUNET_TESTING_peer_run("test-gnunet-vpn", "test_gnunet_vpn.conf", &run,
505                               NULL))
506     return 1;
507   GNUNET_DISK_directory_remove("/tmp/gnunet-test-vpn");
508   return global_ret;
509 }
510
511 /* end of test_gnunet_vpn.c */