4c915cabede46979861289dda3e9b179e8cb4c54
[oweals/gnunet.git] / src / gns / test_gns_proxy.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
19 /**
20  * @file test_gns_proxy.c
21  * @brief testcase for accessing SOCKS5 GNS proxy
22  * @author Martin Schanzenbach
23  */
24 #include "platform.h"
25 #if HAVE_CURL_CURL_H
26 #include <curl/curl.h>
27 #elif HAVE_GNURL_CURL_H
28 #include <gnurl/curl.h>
29 #endif
30 #include <microhttpd.h>
31 #include "gnunet_namestore_service.h"
32 #include "gnunet_gns_service.h"
33 #include "gnunet_testing_lib.h"
34 #include "gnunet_os_lib.h"
35
36 #define PORT 8080
37 #define TEST_DOMAIN "www.gnu"
38
39 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 300)
40
41 /**
42  * Return value for 'main'.
43  */
44 static int global_ret;
45
46 static struct GNUNET_NAMESTORE_Handle *namestore;
47
48 static struct MHD_Daemon *mhd;
49
50 static struct GNUNET_SCHEDULER_Task * mhd_task_id;
51
52 static struct GNUNET_SCHEDULER_Task * curl_task_id;
53
54 static CURL *curl;
55
56 static CURLM *multi;
57
58 static char *url;
59
60 static struct GNUNET_OS_Process *proxy_proc;
61
62 static char* tmp_cfgfile;
63
64 struct CBC
65 {
66   char buf[1024];
67   size_t pos;
68 };
69
70 static struct CBC cbc;
71
72
73 static size_t
74 copy_buffer (void *ptr, size_t size, size_t nmemb, void *ctx)
75 {
76   struct CBC *cbc = ctx;
77
78   if (cbc->pos + size * nmemb > sizeof(cbc->buf))
79     return 0;                   /* overflow */
80   GNUNET_memcpy (&cbc->buf[cbc->pos], ptr, size * nmemb);
81   cbc->pos += size * nmemb;
82   return size * nmemb;
83 }
84
85
86 static int
87 mhd_ahc (void *cls,
88           struct MHD_Connection *connection,
89           const char *url,
90           const char *method,
91           const char *version,
92           const char *upload_data, size_t *upload_data_size,
93           void **unused)
94 {
95   static int ptr;
96   struct MHD_Response *response;
97   int ret;
98
99   if (0 != strcmp ("GET", method))
100     return MHD_NO;              /* unexpected method */
101   if (&ptr != *unused)
102   {
103     *unused = &ptr;
104     return MHD_YES;
105   }
106   *unused = NULL;
107   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MHD sends respose for request to URL `%s'\n", url);
108   response = MHD_create_response_from_buffer (strlen (url),
109                                               (void *) url,
110                                               MHD_RESPMEM_MUST_COPY);
111   ret = MHD_queue_response (connection, MHD_HTTP_OK, response);
112   MHD_destroy_response (response);
113   if (ret == MHD_NO)
114     abort ();
115   return ret;
116 }
117
118
119 static void
120 do_shutdown ()
121 {
122   if (mhd_task_id != NULL)
123   {
124     GNUNET_SCHEDULER_cancel (mhd_task_id);
125     mhd_task_id = NULL;
126   }
127   if (curl_task_id != NULL)
128   {
129     GNUNET_SCHEDULER_cancel (curl_task_id);
130     curl_task_id = NULL;
131   }
132   if (NULL != mhd)
133   {
134     MHD_stop_daemon (mhd);
135     mhd = NULL;
136   }
137   GNUNET_free_non_null (url);
138
139   if (NULL != tmp_cfgfile)
140     {
141       if (0 != remove (tmp_cfgfile))
142         GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "remove", tmp_cfgfile);
143       GNUNET_free (tmp_cfgfile);
144       tmp_cfgfile = NULL;
145     }
146   if (NULL != proxy_proc)
147     {
148       (void) GNUNET_OS_process_kill (proxy_proc, SIGKILL);
149       GNUNET_assert (GNUNET_OK == GNUNET_OS_process_wait (proxy_proc));
150       GNUNET_OS_process_destroy (proxy_proc);
151       proxy_proc = NULL;
152     }
153   url = NULL;
154   GNUNET_SCHEDULER_shutdown ();
155 }
156
157
158 /**
159  * Function to run the HTTP client.
160  */
161 static void
162 curl_main (void);
163
164
165 static void
166 curl_task (void *cls)
167 {
168   curl_task_id = NULL;
169   curl_main ();
170 }
171
172
173 static void
174 curl_main ()
175 {
176   fd_set rs;
177   fd_set ws;
178   fd_set es;
179   int max;
180   struct GNUNET_NETWORK_FDSet nrs;
181   struct GNUNET_NETWORK_FDSet nws;
182   struct GNUNET_TIME_Relative delay;
183   long timeout;
184   int running;
185   struct CURLMsg *msg;
186
187   max = 0;
188   FD_ZERO (&rs);
189   FD_ZERO (&ws);
190   FD_ZERO (&es);
191   curl_multi_perform (multi, &running);
192   if (running == 0)
193   {
194     GNUNET_assert (NULL != (msg = curl_multi_info_read (multi, &running)));
195     if (msg->msg == CURLMSG_DONE)
196     {
197       if (msg->data.result != CURLE_OK)
198       {
199         fprintf (stderr,
200                  "%s failed at %s:%d: `%s'\n",
201                  "curl_multi_perform",
202                 __FILE__,
203                 __LINE__, curl_easy_strerror (msg->data.result));
204         global_ret = 1;
205       }
206     }
207     curl_multi_remove_handle (multi, curl);
208     curl_multi_cleanup (multi);
209     curl_easy_cleanup (curl);
210     curl = NULL;
211     multi = NULL;
212     if (cbc.pos != strlen ("/hello_world"))
213     {
214       GNUNET_break (0);
215       global_ret = 2;
216     }
217     if (0 != strncmp ("/hello_world", cbc.buf, strlen ("/hello_world")))
218     {
219       GNUNET_break (0);
220       global_ret = 3;
221     }
222     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Download complete, shutting down!\n");
223     do_shutdown ();
224     return;
225   }
226   GNUNET_assert (CURLM_OK == curl_multi_fdset (multi, &rs, &ws, &es, &max));
227   if ( (CURLM_OK != curl_multi_timeout (multi, &timeout)) ||
228        (-1 == timeout) )
229     delay = GNUNET_TIME_UNIT_SECONDS;
230   else
231     delay = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, (unsigned int) timeout);
232   GNUNET_NETWORK_fdset_copy_native (&nrs,
233                                     &rs,
234                                     max + 1);
235   GNUNET_NETWORK_fdset_copy_native (&nws,
236                                     &ws,
237                                     max + 1);
238   curl_task_id = GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
239                                               delay,
240                                               &nrs,
241                                               &nws,
242                                               &curl_task,
243                                               NULL);
244 }
245
246
247 static void
248 start_curl (void *cls)
249 {
250   GNUNET_asprintf (&url,
251                    "http://%s:%d/hello_world",
252                    TEST_DOMAIN, PORT);
253   curl = curl_easy_init ();
254   curl_easy_setopt (curl, CURLOPT_URL, url);
255   curl_easy_setopt (curl, CURLOPT_WRITEFUNCTION, &copy_buffer);
256   curl_easy_setopt (curl, CURLOPT_WRITEDATA, &cbc);
257   curl_easy_setopt (curl, CURLOPT_FAILONERROR, 1);
258   curl_easy_setopt (curl, CURLOPT_TIMEOUT, 150L);
259   curl_easy_setopt (curl, CURLOPT_CONNECTTIMEOUT, 15L);
260   curl_easy_setopt (curl, CURLOPT_NOSIGNAL, 1);
261   curl_easy_setopt (curl, CURLOPT_PROXY, "socks5h://127.0.0.1:7777");
262
263   multi = curl_multi_init ();
264   GNUNET_assert (multi != NULL);
265   GNUNET_assert (CURLM_OK == curl_multi_add_handle (multi, curl));
266   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Beginning HTTP download from `%s'\n", url);
267   curl_main ();
268 }
269
270
271 static void
272 disco_ns (void* cls)
273 {
274   GNUNET_NAMESTORE_disconnect (namestore);
275 }
276
277
278 /**
279  * Callback invoked from the namestore service once record is
280  * created.
281  *
282  * @param cls closure
283  * @param af address family, AF_INET or AF_INET6; AF_UNSPEC on error;
284  *                will match 'result_af' from the request
285  * @param address IP address (struct in_addr or struct in_addr6, depending on 'af')
286  *                that the VPN allocated for the redirection;
287  *                traffic to this IP will now be redirected to the
288  *                specified target peer; NULL on error
289  */
290 static void
291 commence_testing (void *cls,
292                   int32_t success,
293                   const char *emsg)
294 {
295   GNUNET_SCHEDULER_add_now (&disco_ns, NULL);
296
297   if ( (emsg != NULL) && (GNUNET_YES != success) )
298   {
299     fprintf (stderr,
300              "NS failed to create record %s\n",
301              emsg);
302     GNUNET_SCHEDULER_shutdown ();
303     return;
304   }
305
306   curl_task_id =
307     GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, 
308                                   &start_curl, NULL);
309 }
310
311
312 /**
313  * Function to keep the HTTP server running.
314  */
315 static void
316 mhd_main (void);
317
318
319 static void
320 mhd_task (void *cls)
321 {
322   mhd_task_id = NULL;
323   MHD_run (mhd);
324   mhd_main ();
325 }
326
327
328 static void
329 mhd_main ()
330 {
331   struct GNUNET_NETWORK_FDSet nrs;
332   struct GNUNET_NETWORK_FDSet nws;
333   fd_set rs;
334   fd_set ws;
335   fd_set es;
336   int max_fd;
337   unsigned MHD_LONG_LONG timeout;
338   struct GNUNET_TIME_Relative delay;
339
340   GNUNET_assert (NULL == mhd_task_id);
341   FD_ZERO (&rs);
342   FD_ZERO (&ws);
343   FD_ZERO (&es);
344   max_fd = -1;
345   GNUNET_assert (MHD_YES ==
346                  MHD_get_fdset (mhd, &rs, &ws, &es, &max_fd));
347   if (MHD_YES == MHD_get_timeout (mhd, &timeout))
348     delay = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
349                                            (unsigned int) timeout);
350   else
351     delay = GNUNET_TIME_UNIT_FOREVER_REL;
352   GNUNET_NETWORK_fdset_copy_native (&nrs,
353                                     &rs,
354                                     max_fd + 1);
355   GNUNET_NETWORK_fdset_copy_native (&nws,
356                                     &ws,
357                                     max_fd + 1);
358   mhd_task_id = GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
359                                              delay,
360                                              &nrs,
361                                              &nws,
362                                              &mhd_task,
363                                              NULL);
364 }
365
366 static void
367 run (void *cls,
368      const struct GNUNET_CONFIGURATION_Handle *cfg,
369      struct GNUNET_TESTING_Peer *peer)
370 {
371   enum MHD_FLAG flags;
372   struct GNUNET_CRYPTO_EcdsaPrivateKey *host_key;
373   struct GNUNET_GNSRECORD_Data rd;
374   char *zone_keyfile;
375
376   namestore = GNUNET_NAMESTORE_connect (cfg);
377   GNUNET_assert (NULL != namestore);
378   flags = MHD_USE_DEBUG;
379   mhd = MHD_start_daemon (flags,
380                           PORT,
381                           NULL, NULL,
382                           &mhd_ahc, NULL,
383                           MHD_OPTION_END);
384   GNUNET_assert (NULL != mhd);
385   mhd_main ();
386
387   tmp_cfgfile = GNUNET_DISK_mktemp ("test_gns_proxy_tmp.conf");
388   if (NULL == tmp_cfgfile)
389   {
390     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
391                 "Failed to create tmp cfg!\n");
392     do_shutdown ();
393     return;
394   }
395
396   if (GNUNET_OK != GNUNET_CONFIGURATION_write ((struct GNUNET_CONFIGURATION_Handle *)cfg,
397                               tmp_cfgfile))
398   {
399     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
400                 "Failed to write tmp cfg\n");
401     do_shutdown ();
402     return;
403   }
404
405   proxy_proc = GNUNET_OS_start_process (GNUNET_NO,
406                                         GNUNET_OS_INHERIT_STD_ALL,
407                                         NULL,
408                                         NULL,
409                                         NULL,
410                                         "gnunet-gns-proxy",
411                                         "gnunet-gns-proxy",
412                                         "-c", tmp_cfgfile, NULL);
413
414   if (NULL == proxy_proc)
415   {
416     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
417                 "Unable to start proxy\n");
418     do_shutdown ();
419     return;
420   }
421
422   if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "gns",
423                                                             "ZONEKEY",
424                                                             &zone_keyfile))
425   {
426     GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Failed to get key from cfg\n");
427     return;
428   }
429
430   host_key = GNUNET_CRYPTO_ecdsa_key_create_from_file (zone_keyfile);
431   rd.expiration_time = GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us;
432   GNUNET_assert (GNUNET_OK == GNUNET_GNSRECORD_string_to_value (GNUNET_DNSPARSER_TYPE_A,
433                                                                "127.0.0.1",
434                                                                (void**)&rd.data,
435                                                                &rd.data_size));
436   rd.record_type = GNUNET_DNSPARSER_TYPE_A;
437
438   GNUNET_NAMESTORE_record_create (namestore,
439                                   host_key,
440                                   "www",
441                                   &rd,
442                                   &commence_testing,
443                                   NULL);
444
445   GNUNET_free ((void**)rd.data);
446   GNUNET_free (zone_keyfile);
447   GNUNET_free (host_key);
448 }
449
450 int
451 main (int argc, char *const *argv)
452 {
453   char *binary;
454
455   if (GNUNET_SYSERR == GNUNET_OS_check_helper_binary ("gnunet-gns-proxy", GNUNET_NO, NULL))
456   {
457     fprintf (stderr, "Proxy binary not in PATH... skipping!\n");
458     return 0;
459   }
460   binary = GNUNET_OS_get_libexec_binary_path ("gnunet-helper-dns");
461   if (GNUNET_YES != GNUNET_OS_check_helper_binary (binary, GNUNET_YES, NULL)) // TODO: once we have a windows-testcase, add test parameters here
462   {
463     fprintf (stderr, "DNS helper binary has wrong permissions... skipping!\n");
464     GNUNET_free (binary);
465     return 0;
466   }
467     GNUNET_free (binary);
468
469   if (0 != curl_global_init (CURL_GLOBAL_WIN32))
470   {
471     fprintf (stderr, "failed to initialize curl\n");
472     return 2;
473   }
474   if (0 != GNUNET_TESTING_peer_run ("test-gnunet-gns-proxy",
475                                     "test_gns_proxy.conf",
476                                     &run, NULL))
477     return 1;
478   GNUNET_DISK_directory_remove ("/tmp/gnunet-test-gns-proxy");
479   return global_ret;
480 }
481
482 /* end of test_gns_vpn.c */