fix
[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 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_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-new.h"
32 #include "gnunet_os_lib.h"
33
34 #define PORT 8080
35 #define TEST_DOMAIN "www.gnunet"
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 }
153
154
155 /**
156  * Function to run the HTTP client.
157  */
158 static void
159 curl_main (void);
160
161
162 static void
163 curl_task (void *cls,
164           const struct GNUNET_SCHEDULER_TaskContext *tc)
165 {
166   curl_task_id = GNUNET_SCHEDULER_NO_TASK;
167   curl_main ();
168 }
169
170
171 static void
172 curl_main ()
173 {
174   fd_set rs;
175   fd_set ws;
176   fd_set es;
177   int max;
178   struct GNUNET_NETWORK_FDSet nrs;
179   struct GNUNET_NETWORK_FDSet nws;
180   struct GNUNET_TIME_Relative delay;
181   long timeout;
182   int running;
183   struct CURLMsg *msg;
184
185   max = 0;
186   FD_ZERO (&rs);
187   FD_ZERO (&ws);
188   FD_ZERO (&es);
189   curl_multi_perform (multi, &running);
190   if (running == 0)
191   {
192     GNUNET_assert (NULL != (msg = curl_multi_info_read (multi, &running)));
193     if (msg->msg == CURLMSG_DONE)
194     {
195       if (msg->data.result != CURLE_OK)
196       {
197         fprintf (stderr,
198                  "%s failed at %s:%d: `%s'\n",
199                  "curl_multi_perform",
200                 __FILE__,
201                 __LINE__, curl_easy_strerror (msg->data.result));
202         global_ret = 1;
203       }
204     }
205     curl_multi_remove_handle (multi, curl);
206     curl_multi_cleanup (multi);
207     curl_easy_cleanup (curl);
208     curl = NULL;
209     multi = NULL;
210     if (cbc.pos != strlen ("/hello_world"))
211     {
212       GNUNET_break (0);
213       global_ret = 2;
214     }
215     if (0 != strncmp ("/hello_world", cbc.buf, strlen ("/hello_world")))
216     {
217       GNUNET_break (0);
218       global_ret = 3;
219     }
220     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Download complete, shutting down!\n");
221     do_shutdown ();
222     return;    
223   }
224   GNUNET_assert (CURLM_OK == curl_multi_fdset (multi, &rs, &ws, &es, &max)); 
225   if ( (CURLM_OK != curl_multi_timeout (multi, &timeout)) ||
226        (-1 == timeout) )
227     delay = GNUNET_TIME_UNIT_SECONDS;
228   else
229     delay = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, (unsigned int) timeout);
230   GNUNET_NETWORK_fdset_copy_native (&nrs,
231                                     &rs,
232                                     max + 1);
233   GNUNET_NETWORK_fdset_copy_native (&nws,
234                                     &ws,
235                                     max + 1);
236   curl_task_id = GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
237                                               delay,
238                                               &nrs,
239                                               &nws,
240                                               &curl_task,
241                                               NULL);  
242 }
243
244 static void
245 start_curl (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
246 {
247   GNUNET_asprintf (&url, 
248                    "http://%s:%d/hello_world",  
249                    TEST_DOMAIN, PORT);
250   curl = curl_easy_init ();
251   curl_easy_setopt (curl, CURLOPT_URL, url);
252   curl_easy_setopt (curl, CURLOPT_WRITEFUNCTION, &copy_buffer);
253   curl_easy_setopt (curl, CURLOPT_WRITEDATA, &cbc);
254   curl_easy_setopt (curl, CURLOPT_FAILONERROR, 1);
255   curl_easy_setopt (curl, CURLOPT_TIMEOUT, 150L);
256   curl_easy_setopt (curl, CURLOPT_CONNECTTIMEOUT, 15L);
257   curl_easy_setopt (curl, CURLOPT_NOSIGNAL, 1);
258   curl_easy_setopt (curl, CURLOPT_PROXY, "socks5h://127.0.0.1:7777");
259
260   multi = curl_multi_init ();
261   GNUNET_assert (multi != NULL);
262   GNUNET_assert (CURLM_OK == curl_multi_add_handle (multi, curl));
263   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Beginning HTTP download from `%s'\n", url);
264   curl_main ();
265 }
266
267 static void
268 disco_ns (void* cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
269 {
270   GNUNET_NAMESTORE_disconnect (namestore);
271 }
272
273 /**
274  * Callback invoked from the namestore service once record is
275  * created.
276  *
277  * @param cls closure
278  * @param af address family, AF_INET or AF_INET6; AF_UNSPEC on error;
279  *                will match 'result_af' from the request
280  * @param address IP address (struct in_addr or struct in_addr6, depending on 'af')
281  *                that the VPN allocated for the redirection;
282  *                traffic to this IP will now be redirected to the 
283  *                specified target peer; NULL on error
284  */
285 static void
286 commence_testing (void *cls, int32_t success, const char *emsg)
287 {
288   GNUNET_SCHEDULER_add_now (&disco_ns, NULL);
289
290   if ((emsg != NULL) && (GNUNET_YES != success))
291   {
292     fprintf (stderr, 
293              "NS failed to create record %s\n", emsg);
294     GNUNET_SCHEDULER_shutdown ();
295     return;
296   }
297   
298   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1), start_curl, NULL);
299
300 }
301
302
303
304
305 /**
306  * Function to keep the HTTP server running.
307  */
308 static void 
309 mhd_main (void);
310
311
312 static void
313 mhd_task (void *cls,
314           const struct GNUNET_SCHEDULER_TaskContext *tc)
315 {
316   mhd_task_id = GNUNET_SCHEDULER_NO_TASK;
317   MHD_run (mhd);
318   mhd_main ();
319 }
320
321
322 static void 
323 mhd_main ()
324 {
325   struct GNUNET_NETWORK_FDSet nrs;
326   struct GNUNET_NETWORK_FDSet nws;
327   fd_set rs;
328   fd_set ws;
329   fd_set es;
330   int max_fd;
331   unsigned MHD_LONG_LONG timeout;
332   struct GNUNET_TIME_Relative delay;
333
334   GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == mhd_task_id);
335   FD_ZERO (&rs);
336   FD_ZERO (&ws);
337   FD_ZERO (&es);
338   max_fd = -1;
339   GNUNET_assert (MHD_YES ==
340                  MHD_get_fdset (mhd, &rs, &ws, &es, &max_fd));
341   if (MHD_YES == MHD_get_timeout (mhd, &timeout))
342     delay = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
343                                            (unsigned int) timeout);
344   else
345     delay = GNUNET_TIME_UNIT_FOREVER_REL;
346   GNUNET_NETWORK_fdset_copy_native (&nrs,
347                                     &rs,
348                                     max_fd + 1);
349   GNUNET_NETWORK_fdset_copy_native (&nws,
350                                     &ws,
351                                     max_fd + 1);
352   mhd_task_id = GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
353                                              delay,
354                                              &nrs,
355                                              &nws,
356                                              &mhd_task,
357                                              NULL);  
358 }
359
360 static void
361 run (void *cls,
362      const struct GNUNET_CONFIGURATION_Handle *cfg,
363      struct GNUNET_TESTING_Peer *peer)
364 {
365   enum MHD_FLAG flags;
366   struct GNUNET_CRYPTO_RsaPrivateKey *host_key;
367   struct GNUNET_NAMESTORE_RecordData rd;
368   char *zone_keyfile;
369   
370   namestore = GNUNET_NAMESTORE_connect (cfg);
371   GNUNET_assert (NULL != namestore);
372   flags = MHD_USE_DEBUG;
373   mhd = MHD_start_daemon (flags,
374                           PORT,
375                           NULL, NULL,
376                           &mhd_ahc, NULL,
377                           MHD_OPTION_END);
378   GNUNET_assert (NULL != mhd);
379   mhd_main ();
380
381   tmp_cfgfile = GNUNET_DISK_mktemp ("test_gns_proxy_tmp.conf");
382   if (NULL == tmp_cfgfile)
383   {
384     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
385                 "Failed to create tmp cfg!\n");
386     do_shutdown ();
387     return;
388   }
389
390   if (GNUNET_OK != GNUNET_CONFIGURATION_write ((struct GNUNET_CONFIGURATION_Handle *)cfg,
391                               tmp_cfgfile))
392   {
393     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
394                 "Failed to write tmp cfg\n");
395     do_shutdown ();
396     return;
397   }
398   
399   proxy_proc = GNUNET_OS_start_process (GNUNET_NO,
400                                         GNUNET_OS_INHERIT_STD_ALL,
401                                         NULL,
402                                         NULL,
403                                         "gnunet-gns-proxy",
404                                         "gnunet-gns-proxy",
405                                         "-c", tmp_cfgfile, NULL);
406
407   if (NULL == proxy_proc)
408   {
409     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
410                 "Unable to start proxy\n");
411     do_shutdown ();
412     return;
413   }
414   
415   if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "gns",
416                                                             "ZONEKEY",
417                                                             &zone_keyfile))
418   {
419     GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Failed to get key from cfg\n");
420     return;
421   }
422
423   host_key = GNUNET_CRYPTO_rsa_key_create_from_file (zone_keyfile);
424   rd.expiration_time = GNUNET_TIME_UNIT_FOREVER_ABS.abs_value;
425   GNUNET_assert (GNUNET_OK == GNUNET_NAMESTORE_string_to_value (GNUNET_GNS_RECORD_A,
426                                                                "127.0.0.1",
427                                                                (void**)&rd.data,
428                                                                &rd.data_size));
429   rd.record_type = GNUNET_GNS_RECORD_A;
430
431   GNUNET_NAMESTORE_record_create (namestore,
432                                   host_key,
433                                   "www",
434                                   &rd,
435                                   &commence_testing,
436                                   NULL);
437
438   GNUNET_free ((void**)rd.data);
439   GNUNET_free (zone_keyfile);
440   GNUNET_CRYPTO_rsa_key_free (host_key);
441 }
442
443 int
444 main (int argc, char *const *argv)
445 {
446
447   if (GNUNET_SYSERR == GNUNET_OS_check_helper_binary ("gnunet-gns-proxy"))
448   {
449     fprintf (stderr, "Proxy binary not in PATH... skipping!\n");
450     return 0;
451   }
452
453   if (GNUNET_YES != GNUNET_OS_check_helper_binary ("gnunet-helper-dns"))
454   {
455     fprintf (stderr, "DNS helper binary has wrong permissions... skipping!\n");
456     return 0;
457   }
458
459   GNUNET_CRYPTO_setup_hostkey ("test_gns_proxy.conf");
460   
461   if (0 != curl_global_init (CURL_GLOBAL_WIN32))
462   {
463     fprintf (stderr, "failed to initialize curl\n");
464     return 2;
465   }
466   if (0 != GNUNET_TESTING_peer_run ("test-gnunet-gns-proxy",
467                                     "test_gns_proxy.conf",
468                                     &run, NULL))
469     return 1;
470   GNUNET_DISK_directory_remove ("/tmp/gnunet-test-gns-proxy");
471   return global_ret;
472 }
473
474 /* end of test_gns_vpn.c */
475