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