-fixes
[oweals/gnunet.git] / src / vpn / test_gnunet_vpn.c
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_arm_service.h"
31
32 #define PORT 48080
33
34 #define START_ARM GNUNET_YES
35
36 #define VERBOSE GNUNET_NO
37
38 struct PeerContext
39 {
40   struct GNUNET_CONFIGURATION_Handle *cfg;
41   struct GNUNET_PeerIdentity id;
42 #if START_ARM
43   struct GNUNET_OS_Process *arm_proc;
44 #endif
45 };
46
47 static struct PeerContext p1;
48
49 /**
50  * Return value for 'main'.
51  */
52 static int global_ret;
53
54 static struct GNUNET_VPN_Handle *vpn;
55
56 static struct MHD_Daemon *mhd;
57
58 static GNUNET_SCHEDULER_TaskIdentifier mhd_task_id;
59
60 static GNUNET_SCHEDULER_TaskIdentifier curl_task_id;
61
62 static GNUNET_SCHEDULER_TaskIdentifier ctrl_c_task_id;
63
64 static struct GNUNET_VPN_RedirectionRequest *rr;
65
66 static CURL *curl;
67
68 static CURLM *multi;
69
70 static char *url;
71
72 struct CBC
73 {
74   char *buf;
75   size_t pos;
76   size_t size;
77 };
78
79 static struct CBC cbc;
80
81
82
83 static size_t
84 copyBuffer (void *ptr, size_t size, size_t nmemb, void *ctx)
85 {
86   struct CBC *cbc = ctx;
87
88   if (cbc->pos + size * nmemb > cbc->size)
89     return 0;                   /* overflow */
90   memcpy (&cbc->buf[cbc->pos], ptr, size * nmemb);
91   cbc->pos += size * nmemb;
92   return size * nmemb;
93 }
94
95
96 static int
97 mhd_ahc (void *cls,
98           struct MHD_Connection *connection,
99           const char *url,
100           const char *method,
101           const char *version,
102           const char *upload_data, size_t *upload_data_size,
103           void **unused)
104 {
105   static int ptr;
106   struct MHD_Response *response;
107   int ret;
108
109   if (0 != strcmp ("GET", method))
110     return MHD_NO;              /* unexpected method */
111   if (&ptr != *unused)
112     {
113       *unused = &ptr;
114       return MHD_YES;
115     }
116   *unused = NULL;
117   response = MHD_create_response_from_buffer (strlen (url),
118                                               (void *) url,
119                                               MHD_RESPMEM_MUST_COPY);
120   ret = MHD_queue_response (connection, MHD_HTTP_OK, response);
121   MHD_destroy_response (response);
122   if (ret == MHD_NO)
123     abort ();
124   return ret;
125 }
126
127
128 static void
129 do_shutdown ()
130 {
131   if (mhd_task_id != GNUNET_SCHEDULER_NO_TASK)
132   {
133     GNUNET_SCHEDULER_cancel (mhd_task_id);
134     mhd_task_id = GNUNET_SCHEDULER_NO_TASK;
135   }
136   if (NULL != mhd)
137   {
138     MHD_stop_daemon (mhd);
139     mhd = NULL;
140   }
141   if (NULL != rr)
142   {
143     GNUNET_VPN_cancel_request (rr);
144     rr = NULL;
145   }
146   if (NULL != vpn)
147   {
148     GNUNET_VPN_disconnect (vpn);
149     vpn = NULL;
150   }
151   GNUNET_free_non_null (url);
152   url = NULL;
153 }
154
155
156 /**
157  * Function to run the HTTP client.
158  */
159 static void
160 curl_main (void);
161
162
163 static void
164 curl_task (void *cls,
165           const struct GNUNET_SCHEDULER_TaskContext *tc)
166 {
167   curl_task_id = GNUNET_SCHEDULER_NO_TASK;
168
169 }
170
171
172 static void
173 curl_main ()
174 {
175   fd_set rs;
176   fd_set ws;
177   fd_set es;
178   int max;
179   struct GNUNET_NETWORK_FDSet nrs;
180   struct GNUNET_NETWORK_FDSet nws;
181   struct GNUNET_TIME_Relative delay;
182   long timeout;
183   int running;
184   struct CURLMsg *msg;
185
186   max = 0;
187   FD_ZERO (&rs);
188   FD_ZERO (&ws);
189   FD_ZERO (&es);
190   curl_multi_perform (multi, &running);
191   if (running == 0)
192   {
193     GNUNET_assert (NULL != (msg = curl_multi_info_read (multi, &running)));
194     if (msg->msg == CURLMSG_DONE)
195     {
196       if (msg->data.result != CURLE_OK)
197         printf ("%s failed at %s:%d: `%s'\n",
198                 "curl_multi_perform",
199                 __FILE__,
200                 __LINE__, curl_easy_strerror (msg->data.result));
201       global_ret = 1;
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       global_ret = 2;
210     if (0 != strncmp ("/hello_world", cbc.buf, strlen ("/hello_world")))
211       global_ret = 3;
212     do_shutdown ();
213     return;    
214   }
215   GNUNET_assert (CURLM_OK == curl_multi_fdset (multi, &rs, &ws, &es, &max));
216  
217   if ( (CURLM_OK != curl_multi_timeout (multi, &timeout)) ||
218        (-1 == timeout) )
219     delay = GNUNET_TIME_UNIT_FOREVER_REL;
220   else
221     delay = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, (unsigned int) timeout);
222   GNUNET_NETWORK_fdset_copy_native (&nrs,
223                                     &rs,
224                                     max);
225   GNUNET_NETWORK_fdset_copy_native (&nws,
226                                     &ws,
227                                     max);
228   curl_task_id = GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
229                                               GNUNET_SCHEDULER_NO_TASK,
230                                               delay,
231                                               &nrs,
232                                               &nws,
233                                               &curl_task,
234                                               NULL);  
235 }
236
237
238 /**
239  * Callback invoked from the VPN service once a redirection is
240  * available.  Provides the IP address that can now be used to
241  * reach the requested destination (in our case, the MHD server)
242  *
243  * @param cls closure
244  * @param af address family, AF_INET or AF_INET6; AF_UNSPEC on error;
245  *                will match 'result_af' from the request
246  * @param address IP address (struct in_addr or struct in_addr6, depending on 'af')
247  *                that the VPN allocated for the redirection;
248  *                traffic to this IP will now be redirected to the 
249  *                specified target peer; NULL on error
250  */
251 static void
252 allocation_cb (void *cls,
253                int af,
254                const void *address)
255 {
256   char ips[INET_ADDRSTRLEN];
257
258   rr = NULL;
259   if (AF_INET != af)
260   {
261     fprintf (stderr, 
262              "VPN failed to allocate appropriate address\n");
263     GNUNET_SCHEDULER_shutdown ();
264     return;
265   }
266   GNUNET_asprintf (&url, 
267                    "http://%s:%u/hello_world",  
268                    inet_ntop (af, address, ips, sizeof (ips)),
269                    (unsigned int) PORT);
270   curl = curl_easy_init ();
271   curl_easy_setopt (curl, CURLOPT_URL, url);
272   curl_easy_setopt (curl, CURLOPT_WRITEFUNCTION, &copyBuffer);
273   curl_easy_setopt (curl, CURLOPT_WRITEDATA, &cbc);
274   curl_easy_setopt (curl, CURLOPT_FAILONERROR, 1);
275   curl_easy_setopt (curl, CURLOPT_TIMEOUT, 150L);
276   curl_easy_setopt (curl, CURLOPT_CONNECTTIMEOUT, 15L);
277   curl_easy_setopt (curl, CURLOPT_NOSIGNAL, 1);
278
279   multi = curl_multi_init ();
280   GNUNET_assert (multi != NULL);
281   GNUNET_assert (CURLM_OK == curl_multi_add_handle (multi, curl));
282
283   fprintf (stderr, "Beginning HTTP download from `%s'\n", url);
284   curl_main ();
285 }
286
287
288 /**
289  * Function to keep the HTTP server running.
290  */
291 static void 
292 mhd_main (void);
293
294
295 static void
296 mhd_task (void *cls,
297           const struct GNUNET_SCHEDULER_TaskContext *tc)
298 {
299   mhd_task_id = GNUNET_SCHEDULER_NO_TASK;
300   MHD_run (mhd);
301   mhd_main ();
302 }
303
304
305 static void
306 ctrl_c_shutdown (void *cls,
307                  const struct GNUNET_SCHEDULER_TaskContext *tc)
308 {
309   ctrl_c_task_id = GNUNET_SCHEDULER_NO_TASK;
310   do_shutdown ();
311   global_ret = 1;
312 }
313
314
315 static void 
316 mhd_main ()
317 {
318   struct GNUNET_NETWORK_FDSet nrs;
319   struct GNUNET_NETWORK_FDSet nws;
320   fd_set rs;
321   fd_set ws;
322   fd_set es;
323   int max_fd;
324   unsigned MHD_LONG_LONG timeout;
325   struct GNUNET_TIME_Relative delay;
326
327   GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == mhd_task_id);
328   FD_ZERO (&rs);
329   FD_ZERO (&ws);
330   FD_ZERO (&es);
331   max_fd = -1;
332   GNUNET_assert (MHD_YES ==
333                  MHD_get_fdset (mhd, &rs, &ws, &es, &max_fd));
334   if (MHD_YES == MHD_get_timeout (mhd, &timeout))
335     delay = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
336                                            (unsigned int) timeout);
337   else
338     delay = GNUNET_TIME_UNIT_FOREVER_REL;
339   GNUNET_NETWORK_fdset_copy_native (&nrs,
340                                     &rs,
341                                     max_fd);
342   GNUNET_NETWORK_fdset_copy_native (&nws,
343                                     &ws,
344                                     max_fd);
345   mhd_task_id = GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
346                                              GNUNET_SCHEDULER_NO_TASK,
347                                              delay,
348                                              &nrs,
349                                              &nws,
350                                              &mhd_task,
351                                              NULL);  
352 }
353
354
355 static void
356 run (void *cls, char *const *args, const char *cfgfile,
357      const struct GNUNET_CONFIGURATION_Handle *cfg)
358 {
359   struct sockaddr_in v4;
360
361   vpn = GNUNET_VPN_connect (cfg);
362   GNUNET_assert (NULL != vpn);
363   v4.sin_family = AF_INET;
364   v4.sin_port = htons (PORT);
365   GNUNET_assert (1 == inet_pton (AF_INET, "127.0.0.1", &v4.sin_addr));
366   mhd = MHD_start_daemon (MHD_USE_DEBUG,
367                           PORT,
368                           NULL, NULL,
369                           &mhd_ahc, NULL,
370                           MHD_OPTION_SOCK_ADDR, &v4,
371                           MHD_OPTION_END);
372   GNUNET_assert (NULL != mhd);
373   mhd_main ();
374   rr = GNUNET_VPN_redirect_to_ip (vpn,
375                                   AF_INET,
376                                   AF_INET,
377                                   &v4,
378                                   GNUNET_YES,
379                                   GNUNET_TIME_UNIT_FOREVER_ABS,
380                                   &allocation_cb, NULL);
381   ctrl_c_task_id = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
382                                                  &ctrl_c_shutdown,
383                                                  NULL);
384 }
385
386
387 static void
388 setup_peer (struct PeerContext *p, const char *cfgname)
389 {
390   p->cfg = GNUNET_CONFIGURATION_create ();
391 #if START_ARM
392   p->arm_proc =
393       GNUNET_OS_start_process (NULL, NULL, "gnunet-service-arm",
394                                "gnunet-service-arm",
395 #if VERBOSE
396                                "-L", "DEBUG",
397 #endif
398                                "-c", cfgname, NULL);
399 #endif
400   GNUNET_assert (NULL != p->arm_proc);
401   GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
402 }
403
404
405 static void
406 stop_peer (struct PeerContext *p)
407 {
408 #if START_ARM
409   if (NULL != p->arm_proc)
410   {
411     if (0 != GNUNET_OS_process_kill (p->arm_proc, SIGTERM))
412       GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
413     if (GNUNET_OS_process_wait (p->arm_proc) != GNUNET_OK)
414       GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "waitpid");
415     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "ARM process %u stopped\n",
416                 GNUNET_OS_process_get_pid (p->arm_proc));
417     GNUNET_OS_process_close (p->arm_proc);
418     p->arm_proc = NULL;
419   }
420 #endif
421   GNUNET_CONFIGURATION_destroy (p->cfg);
422 }
423
424
425 int
426 main (int argc, char *const *argv)
427 {
428   char *const argvx[] = {
429     "test_gnunet_vpn",
430     "-c",
431     "test_gnunet_vpn.conf",
432 #if VERBOSE
433     "-L", "DEBUG",
434 #endif
435     NULL
436   };
437   struct GNUNET_GETOPT_CommandLineOption options[] = {
438     GNUNET_GETOPT_OPTION_END
439   };
440
441   if (0 != curl_global_init (CURL_GLOBAL_WIN32))
442     return 2;
443   setup_peer (&p1, "test_gnunet_vpn.conf");
444   GNUNET_log_setup ("test_gnunet_vpn",
445 #if VERBOSE
446                     "DEBUG",
447 #else
448                     "WARNING",
449 #endif
450                     NULL);
451   GNUNET_PROGRAM_run ((sizeof (argvx) / sizeof (char *)) - 1, argvx,
452                       "test_gnunet_vpn", "nohelp", options, &run, NULL);
453   stop_peer (&p1);
454   GNUNET_DISK_directory_remove ("/tmp/gnunet-test-vpn");
455   return global_ret;
456 }