-fixes
[oweals/gnunet.git] / src / gns / gnunet-gns-proxy.c
1 /*
2      This file is part of GNUnet.
3      (C) 2012 Christian Grothoff (and other contributing authors)
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 #include "platform.h"
22 #include <gnunet_util_lib.h>
23 #include <microhttpd.h>
24 #include <curl/curl.h>
25 #include "gns_proxy_proto.h"
26 #include "gns.h"
27
28 #define GNUNET_GNS_PROXY_PORT 7777
29
30 //TODO maybe make this an api call
31 /**
32  * Checks if name is in tld
33  *
34  * @param name the name to check
35  * @param tld the TLD to check for
36  * @return GNUNET_YES or GNUNET_NO
37  */
38 int
39 is_tld(const char* name, const char* tld)
40 {
41   int offset = 0;
42
43   if (strlen(name) <= strlen(tld))
44   {
45     return GNUNET_NO;
46   }
47
48   offset = strlen(name)-strlen(tld);
49   if (strcmp (name+offset, tld) != 0)
50   {
51     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
52                "%s is not in .%s TLD\n", name, tld);
53     return GNUNET_NO;
54   }
55
56   return GNUNET_YES;
57 }
58
59 struct Socks5Request
60 {
61   struct GNUNET_NETWORK_Handle *sock;
62   struct GNUNET_NETWORK_Handle *remote_sock;
63
64   int state;
65
66   GNUNET_SCHEDULER_TaskIdentifier rtask;
67   GNUNET_SCHEDULER_TaskIdentifier fwdrtask;
68   GNUNET_SCHEDULER_TaskIdentifier wtask;
69   GNUNET_SCHEDULER_TaskIdentifier fwdwtask;
70
71   char rbuf[2048];
72   char wbuf[2048];
73   unsigned int rbuf_len;
74   unsigned int wbuf_len;
75 };
76
77
78 #define BUF_WAIT_FOR_CURL 0
79 #define BUF_WAIT_FOR_MHD 1
80
81 struct ProxyCurlTask
82 {
83   //DLL
84   struct ProxyCurlTask *prev;
85   struct ProxyCurlTask *next;
86
87   CURL *curl;
88   char url[2048];
89   char buffer[CURL_MAX_WRITE_SIZE];
90   int buf_status;
91   unsigned int bytes_downloaded;
92   unsigned int bytes_in_buffer;
93   int download_in_progress;
94   int download_successful;
95   int download_error;
96   struct MHD_Connection *connection;
97   
98 };
99
100 unsigned long port = GNUNET_GNS_PROXY_PORT;
101 static struct GNUNET_NETWORK_Handle *lsock;
102 GNUNET_SCHEDULER_TaskIdentifier ltask;
103 GNUNET_SCHEDULER_TaskIdentifier curl_download_task;
104 static struct MHD_Daemon *httpd;
105 static GNUNET_SCHEDULER_TaskIdentifier httpd_task;
106 CURLM *curl_multi;
107
108 struct ProxyCurlTask *ctasks_head;
109 struct ProxyCurlTask *ctasks_tail;
110
111 static int
112 con_val_iter (void *cls,
113               enum MHD_ValueKind kind,
114               const char *key,
115               const char *value)
116 {
117   char* buf = (char*)cls;
118
119   if (0 == strcmp ("Host", key))
120   {
121     strcpy (buf, value);
122     return MHD_NO;
123   }
124   return MHD_YES;
125 }
126
127
128 /**
129  * schedule mhd
130  */
131 static void
132 run_httpd (void);
133
134
135 /**
136  * Process cURL download bits
137  *
138  * @param ptr buffer with data
139  * @param size size of a record
140  * @param nmemb number of records downloaded
141  * @param ctx context
142  * @return number of processed bytes
143  */
144 static size_t
145 callback_download (void *ptr, size_t size, size_t nmemb, void *ctx)
146 {
147   const char *cbuf = ptr;
148   size_t total;
149   struct ProxyCurlTask *ctask = ctx;
150
151   total = size*nmemb;
152   ctask->bytes_downloaded += total;
153
154   if (total == 0)
155   {
156     return total;
157   }
158
159   if (total > sizeof (ctask->buffer))
160   {
161     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
162                 "cURL gave us too much data to handle (%d)!\n",
163                 total);
164     return 0;
165   }
166
167   if (ctask->buf_status == BUF_WAIT_FOR_MHD)
168   {
169     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
170                 "Waiting for MHD\n");
171     return CURL_WRITEFUNC_PAUSE;
172   }
173
174   memcpy (ctask->buffer, cbuf, total);
175   ctask->bytes_in_buffer = total;
176
177   ctask->buf_status = BUF_WAIT_FOR_MHD;
178
179   //GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
180   //            "cURL chunk:\n%s\n", (char*)ctask->buffer);
181   MHD_run (httpd);
182   return total;
183 }
184
185
186
187 /**
188  * Callback to free content
189  *
190  * @param cls content to free
191  */
192 static void
193 mhd_content_free (void *cls)
194 {
195   struct ProxyCurlTask *ctask = cls;
196
197   if (ctask->curl != NULL)
198     curl_easy_cleanup (ctask->curl);
199
200   GNUNET_free (ctask);
201
202 }
203
204 /**
205  * Callback for MHD response
206  *
207  * @param cls closure
208  * @param pos in buffer
209  * @param buf buffer
210  * @param max space in buffer
211  */
212 static ssize_t
213 mhd_content_cb (void *cls,
214                 uint64_t pos,
215                 char* buf,
216                 size_t max)
217 {
218   struct ProxyCurlTask *ctask = cls;
219
220   if (ctask->download_successful &&
221       (ctask->buf_status == BUF_WAIT_FOR_CURL))
222   {
223     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
224                 "MHD: sending response\n");
225     ctask->download_in_progress = GNUNET_NO;
226     curl_multi_remove_handle (curl_multi, ctask->curl);
227     curl_easy_cleanup (ctask->curl);
228     return MHD_CONTENT_READER_END_OF_STREAM;
229   }
230   
231   if (ctask->download_error &&
232       (ctask->buf_status == BUF_WAIT_FOR_CURL))
233   {
234     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
235                 "MHD: sending error response\n");
236     ctask->download_in_progress = GNUNET_NO;
237     curl_multi_remove_handle (curl_multi, ctask->curl);
238     curl_easy_cleanup (ctask->curl);
239     return MHD_CONTENT_READER_END_WITH_ERROR;
240   }
241
242   if ( ctask->buf_status == BUF_WAIT_FOR_CURL )
243   {
244     return 0;
245   }
246
247   if ( ctask->bytes_in_buffer > max )
248   {
249     GNUNET_log ( GNUNET_ERROR_TYPE_ERROR,
250                  "MHD: buffer in response too small!\n");
251     return MHD_CONTENT_READER_END_WITH_ERROR;
252   }
253
254   if ( 0 != ctask->bytes_in_buffer )
255   {
256     GNUNET_log ( GNUNET_ERROR_TYPE_DEBUG,
257                  "MHD: copying %d bytes to mhd response at offset %d\n",
258                  ctask->bytes_in_buffer, pos);
259     memcpy ( buf, ctask->buffer, ctask->bytes_in_buffer );
260   }
261   
262   ctask->buf_status = BUF_WAIT_FOR_CURL;
263   curl_easy_pause (ctask->curl, CURLPAUSE_CONT);
264
265   return ctask->bytes_in_buffer;
266 }
267
268
269
270 /**
271  * Task that is run when we are ready to receive more data
272  * from curl
273  *
274  * @param cls closure
275  * @param tc task context
276  */
277 static void
278 curl_task_download (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
279
280 /**
281  * Ask cURL for the select sets and schedule download
282  */
283 static void
284 curl_download_prepare ()
285 {
286   CURLMcode mret;
287   fd_set rs;
288   fd_set ws;
289   fd_set es;
290   int max;
291   struct GNUNET_NETWORK_FDSet *grs;
292   struct GNUNET_NETWORK_FDSet *gws;
293   long to;
294   struct GNUNET_TIME_Relative rtime;
295
296   max = -1;
297   FD_ZERO (&rs);
298   FD_ZERO (&ws);
299   FD_ZERO (&es);
300   mret = curl_multi_fdset (curl_multi, &rs, &ws, &es, &max);
301
302   if (mret != CURLM_OK)
303   {
304     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
305                 "%s failed at %s:%d: `%s'\n",
306                 "curl_multi_fdset", __FILE__, __LINE__,
307                 curl_multi_strerror (mret));
308     //TODO cleanup here?
309     return;
310   }
311
312   mret = curl_multi_timeout (curl_multi, &to);
313   rtime = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, to);
314
315   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
316               "cURL multi fds: max=%d\n", max);
317
318   grs = GNUNET_NETWORK_fdset_create ();
319   gws = GNUNET_NETWORK_fdset_create ();
320   GNUNET_NETWORK_fdset_copy_native (grs, &rs, max + 1);
321   GNUNET_NETWORK_fdset_copy_native (gws, &ws, max + 1);
322   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
323               "Scheduling task cURL\n");
324
325   if (curl_download_task != GNUNET_SCHEDULER_NO_TASK)
326     GNUNET_SCHEDULER_cancel (curl_download_task);
327   
328   curl_download_task =
329     GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
330                                  rtime,
331                                  grs, gws,
332                                  &curl_task_download, curl_multi);
333   GNUNET_NETWORK_fdset_destroy (gws);
334   GNUNET_NETWORK_fdset_destroy (grs);
335
336 }
337
338
339 /**
340  * Task that is run when we are ready to receive more data
341  * from curl
342  *
343  * @param cls closure
344  * @param tc task context
345  */
346 static void
347 curl_task_download (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
348 {
349   int running;
350   int msgnum;
351   struct CURLMsg *msg;
352   CURLMcode mret;
353   struct ProxyCurlTask *ctask;
354   int num_ctasks;
355
356   curl_download_task = GNUNET_SCHEDULER_NO_TASK;
357
358   if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
359   {
360     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
361                 "Shutdown requested while trying to download\n");
362     //TODO cleanup
363     return;
364   }
365   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
366               "Ready to dl\n");
367
368   do
369   {
370     running = 0;
371     num_ctasks = 0;
372     
373     mret = curl_multi_perform (curl_multi, &running);
374
375     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
376                 "Running curl tasks: %d\n", running);
377
378     ctask = ctasks_head;
379     for (; ctask != NULL; ctask = ctask->next)
380     {
381       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
382                   "CTask: %s\n", ctask->url);
383       num_ctasks++;
384     }
385
386     if (num_ctasks != running)
387     {
388       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
389                   "%d ctasks, %d curl running\n", num_ctasks, running);
390     }
391     
392     do
393     {
394       ctask = ctasks_head;
395       msg = curl_multi_info_read (curl_multi, &msgnum);
396       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
397                   "Messages left: %d\n", msgnum);
398       
399       if (msg == NULL)
400         break;
401       switch (msg->msg)
402       {
403        case CURLMSG_DONE:
404          if ((msg->data.result != CURLE_OK) &&
405              (msg->data.result != CURLE_GOT_NOTHING))
406          {
407            GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
408                        "Download curl failed");
409             
410            for (; ctask != NULL; ctask = ctask->next)
411            {
412              if (memcmp (msg->easy_handle, ctask->curl, sizeof (CURL)) != 0)
413                continue;
414              
415              GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
416                          "Download curl failed for task %s: %s.\n",
417                          ctask->url,
418                          curl_easy_strerror (msg->data.result));
419              ctask->download_successful = GNUNET_NO;
420              ctask->download_error = GNUNET_YES;
421              //curl_multi_remove_handle (curl_multi, ctask->curl);
422              //curl_easy_cleanup (ctask->curl);
423              GNUNET_CONTAINER_DLL_remove (ctasks_head, ctasks_tail,
424                                           ctask);
425              break;
426            }
427            GNUNET_assert (ctask != NULL);
428          }
429          else
430          {
431            GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
432                        "cURL download completed.\n");
433
434            for (; ctask != NULL; ctask = ctask->next)
435            {
436              if (memcmp (msg->easy_handle, ctask->curl, sizeof (CURL)) != 0)
437                continue;
438              
439              GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
440                          "cURL task %s found.\n", ctask->url);
441              ctask->download_successful = GNUNET_YES;
442              //curl_multi_remove_handle (curl_multi, ctask->curl);
443              //curl_easy_cleanup (ctask->curl);
444              GNUNET_CONTAINER_DLL_remove (ctasks_head, ctasks_tail,
445                                           ctask);
446              break;
447            }
448            GNUNET_assert (ctask != NULL);
449          }
450          GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
451                      "curl end %s\n", curl_easy_strerror(msg->data.result));
452          run_httpd ();
453          break;
454        default:
455          GNUNET_assert (0);
456          break;
457       }
458     } while (msgnum > 0);
459     
460     num_ctasks=0;
461     for (ctask=ctasks_head; ctask != NULL; ctask = ctask->next)
462     {
463       num_ctasks++;
464     }
465     
466     if (num_ctasks != running)
467     {
468       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
469                   "%d ctasks, %d curl running\n", num_ctasks, running);
470     }
471
472     GNUNET_assert ( num_ctasks == running );
473
474     run_httpd ();
475
476   } while (mret == CURLM_CALL_MULTI_PERFORM);
477   
478   
479   if (mret != CURLM_OK)
480   {
481     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "%s failed at %s:%d: `%s'\n",
482                 "curl_multi_perform", __FILE__, __LINE__,
483                 curl_multi_strerror (mret));
484   }
485   curl_download_prepare();
486 }
487
488
489 /**
490  * Main MHD callback for handling requests.
491  *
492  * @param cls unused
493  * @param con MHD connection handle
494  * @param meth the HTTP method used ("GET", "PUT", etc.)
495  * @param ver the HTTP version string (i.e. "HTTP/1.1")
496  * @param upload_data the data being uploaded (excluding HEADERS,
497  *        for a POST that fits into memory and that is encoded
498  *        with a supported encoding, the POST data will NOT be
499  *        given in upload_data and is instead available as
500  *        part of MHD_get_connection_values; very large POST
501  *        data *will* be made available incrementally in
502  *        upload_data)
503  * @param upload_data_size set initially to the size of the
504  *        upload_data provided; the method must update this
505  *        value to the number of bytes NOT processed;
506  * @param con_cls pointer to location where we store the 'struct Request'
507  * @return MHD_YES if the connection was handled successfully,
508  *         MHD_NO if the socket must be closed due to a serious
509  *         error while handling the request
510  */
511 static int
512 create_response (void *cls,
513                  struct MHD_Connection *con,
514                  const char *url,
515                  const char *meth,
516                  const char *ver,
517                  const char *upload_data,
518                  size_t *upload_data_size,
519                  void **con_cls)
520 {
521   static int dummy;
522   const char* page = "<html><head><title>gnoxy</title>"\
523                       "</head><body>cURL fail</body></html>";
524   struct MHD_Response *response;
525   char host[265];
526   char curlurl[512];
527   int ret = MHD_YES;
528
529   CURLMcode mret;
530   struct ProxyCurlTask *ctask;
531   
532   if (0 != strcmp (meth, "GET"))
533     return MHD_NO;
534   if (&dummy != *con_cls)
535   {
536     *con_cls = &dummy;
537     return MHD_YES;
538   }
539
540   if (0 != *upload_data_size)
541     return MHD_NO;
542
543   *con_cls = NULL;
544
545   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
546               "url %s\n", url);
547
548   MHD_get_connection_values (con,
549                              MHD_HEADER_KIND,
550                              &con_val_iter, host);
551
552   
553   /* Do cURL */
554   ctask = GNUNET_malloc (sizeof (struct ProxyCurlTask));
555   ctask->curl = curl_easy_init();
556
557   if (curl_multi == NULL)
558     curl_multi = curl_multi_init ();
559   
560   if ((ctask->curl == NULL) || (curl_multi == NULL))
561   {
562     response = MHD_create_response_from_buffer (strlen (page),
563                                               (void*)page,
564                                               MHD_RESPMEM_PERSISTENT);
565     ret = MHD_queue_response (con,
566                               MHD_HTTP_OK,
567                               response);
568     MHD_destroy_response (response);
569     GNUNET_free (ctask);
570     return ret;
571   }
572
573   ctask->download_in_progress = GNUNET_YES;
574   ctask->download_successful = GNUNET_NO;
575   ctask->bytes_downloaded = 0;
576   ctask->connection = con;
577   ctask->buf_status = BUF_WAIT_FOR_CURL;
578   ctask->bytes_in_buffer = 0;
579
580   curl_easy_setopt (ctask->curl, CURLOPT_WRITEFUNCTION, &callback_download);
581   curl_easy_setopt (ctask->curl, CURLOPT_WRITEDATA, ctask);
582   curl_easy_setopt (ctask->curl, CURLOPT_FOLLOWLOCATION, 1);
583   curl_easy_setopt (ctask->curl, CURLOPT_MAXREDIRS, 4);
584   /* no need to abort if the above failed */
585   sprintf (curlurl, "http://%s%s", host, url);
586   strcpy (ctask->url, curlurl);
587   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
588               "Adding new curl task for %s\n", curlurl);
589   
590   curl_easy_setopt (ctask->curl, CURLOPT_URL, curlurl);
591   curl_easy_setopt (ctask->curl, CURLOPT_FAILONERROR, 1);
592   curl_easy_setopt (ctask->curl, CURLOPT_CONNECTTIMEOUT, 600L);
593   curl_easy_setopt (ctask->curl, CURLOPT_TIMEOUT, 600L);
594
595   mret = curl_multi_add_handle (curl_multi, ctask->curl);
596
597   if (mret != CURLM_OK)
598   {
599     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
600                 "%s failed at %s:%d: `%s'\n",
601                 "curl_multi_add_handle", __FILE__, __LINE__,
602                 curl_multi_strerror (mret));
603     response = MHD_create_response_from_buffer (strlen (page),
604                                                 (void*)page,
605                                                 MHD_RESPMEM_PERSISTENT);
606     ret = MHD_queue_response (con,
607                               MHD_HTTP_OK,
608                               response);
609     MHD_destroy_response (response);
610
611     curl_easy_cleanup (ctask->curl);
612     GNUNET_free (ctask);
613     return ret;
614   }
615   
616   GNUNET_CONTAINER_DLL_insert (ctasks_head, ctasks_tail, ctask);
617
618   curl_download_prepare ();
619
620   response = MHD_create_response_from_callback (-1, -1,
621                                                 &mhd_content_cb,
622                                                 ctask,
623                                                 &mhd_content_free);
624   
625   ret = MHD_queue_response (con, MHD_HTTP_OK, response);
626   
627   //MHD_destroy_response (response);
628
629   return ret;
630 }
631
632 /**
633  * Task run whenever HTTP server operations are pending.
634  *
635  * @param cls unused
636  * @param tc sched context
637  */
638 static void
639 do_httpd (void *cls,
640           const struct GNUNET_SCHEDULER_TaskContext *tc);
641
642
643 /**
644  * schedule mhd
645  */
646 static void
647 run_httpd ()
648 {
649   fd_set rs;
650   fd_set ws;
651   fd_set es;
652   struct GNUNET_NETWORK_FDSet *wrs;
653   struct GNUNET_NETWORK_FDSet *wws;
654   struct GNUNET_NETWORK_FDSet *wes;
655   int max;
656   int haveto;
657   unsigned MHD_LONG_LONG timeout;
658   struct GNUNET_TIME_Relative tv;
659
660   FD_ZERO (&rs);
661   FD_ZERO (&ws);
662   FD_ZERO (&es);
663   wrs = GNUNET_NETWORK_fdset_create ();
664   wes = GNUNET_NETWORK_fdset_create ();
665   wws = GNUNET_NETWORK_fdset_create ();
666   max = -1;
667   GNUNET_assert (MHD_YES == MHD_get_fdset (httpd, &rs, &ws, &es, &max));
668   
669   
670   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
671               "MHD fds: max=%d\n", max);
672   
673   haveto = MHD_get_timeout (httpd, &timeout);
674
675   if (haveto == MHD_YES)
676     tv.rel_value = (uint64_t) timeout;
677   else
678     tv = GNUNET_TIME_UNIT_FOREVER_REL;
679   GNUNET_NETWORK_fdset_copy_native (wrs, &rs, max + 1);
680   GNUNET_NETWORK_fdset_copy_native (wws, &ws, max + 1);
681   GNUNET_NETWORK_fdset_copy_native (wes, &es, max + 1);
682   
683   if (httpd_task != GNUNET_SCHEDULER_NO_TASK)
684     GNUNET_SCHEDULER_cancel (httpd_task);
685   httpd_task =
686     GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_HIGH,
687                                  tv, wrs, wws,
688                                  &do_httpd, NULL);
689   GNUNET_NETWORK_fdset_destroy (wrs);
690   GNUNET_NETWORK_fdset_destroy (wws);
691   GNUNET_NETWORK_fdset_destroy (wes);
692 }
693
694 /**
695  * Task run whenever HTTP server operations are pending.
696  *
697  * @param cls unused
698  * @param tc sched context
699  */
700 static void
701 do_httpd (void *cls,
702           const struct GNUNET_SCHEDULER_TaskContext *tc)
703 {
704   httpd_task = GNUNET_SCHEDULER_NO_TASK;
705   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
706               "MHD run \n");
707   MHD_run (httpd);
708   run_httpd ();
709 }
710
711 /**
712  * Read data from socket
713  *
714  * @param cls the closure
715  * @param tc scheduler context
716  */
717 static void
718 do_read (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
719
720 /**
721  * Read from remote end
722  *
723  * @param cls closure
724  * @param tc scheduler context
725  */
726 static void
727 do_read_remote (void* cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
728
729 /**
730  * Write data to remote socket
731  *
732  * @param cls the closure
733  * @param tc scheduler context
734  */
735 static void
736 do_write_remote (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
737 {
738   struct Socks5Request *s5r = cls;
739   unsigned int len;
740
741   s5r->fwdwtask = GNUNET_SCHEDULER_NO_TASK;
742
743   if ((NULL != tc->read_ready) &&
744       (GNUNET_NETWORK_fdset_isset (tc->write_ready, s5r->remote_sock)) &&
745       ((len = GNUNET_NETWORK_socket_send (s5r->remote_sock, s5r->rbuf,
746                                          s5r->rbuf_len)>0)))
747   {
748     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
749                 "Successfully sent %d bytes to remote socket\n",
750                 len);
751   }
752   else
753   {
754     GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "write remote");
755     //Really!?!?!?
756     if (s5r->rtask != GNUNET_SCHEDULER_NO_TASK)
757       GNUNET_SCHEDULER_cancel (s5r->rtask);
758     if (s5r->wtask != GNUNET_SCHEDULER_NO_TASK)
759       GNUNET_SCHEDULER_cancel (s5r->wtask);
760     if (s5r->fwdrtask != GNUNET_SCHEDULER_NO_TASK)
761       GNUNET_SCHEDULER_cancel (s5r->fwdrtask);
762     GNUNET_NETWORK_socket_close (s5r->remote_sock);
763     GNUNET_NETWORK_socket_close (s5r->sock);
764     GNUNET_free(s5r);
765     return;
766   }
767
768   s5r->rtask =
769     GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
770                                    s5r->sock,
771                                    &do_read, s5r);
772 }
773
774
775 /**
776  * Write data to socket
777  *
778  * @param cls the closure
779  * @param tc scheduler context
780  */
781 static void
782 do_write (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
783 {
784   struct Socks5Request *s5r = cls;
785   unsigned int len;
786
787   s5r->wtask = GNUNET_SCHEDULER_NO_TASK;
788
789   if ((NULL != tc->read_ready) &&
790       (GNUNET_NETWORK_fdset_isset (tc->write_ready, s5r->sock)) &&
791       ((len = GNUNET_NETWORK_socket_send (s5r->sock, s5r->wbuf,
792                                          s5r->wbuf_len)>0)))
793   {
794     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
795                 "Successfully sent %d bytes to socket\n",
796                 len);
797   }
798   else
799   {
800     
801     GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "write");
802     //Really!?!?!?
803     if (s5r->rtask != GNUNET_SCHEDULER_NO_TASK)
804       GNUNET_SCHEDULER_cancel (s5r->rtask);
805     if (s5r->fwdwtask != GNUNET_SCHEDULER_NO_TASK)
806       GNUNET_SCHEDULER_cancel (s5r->fwdwtask);
807     if (s5r->fwdrtask != GNUNET_SCHEDULER_NO_TASK)
808       GNUNET_SCHEDULER_cancel (s5r->fwdrtask);
809     GNUNET_NETWORK_socket_close (s5r->remote_sock);
810     GNUNET_NETWORK_socket_close (s5r->sock);
811     GNUNET_free(s5r);
812     return;
813   }
814
815   if ((s5r->state == SOCKS5_DATA_TRANSFER) &&
816       (s5r->fwdrtask == GNUNET_SCHEDULER_NO_TASK))
817     s5r->fwdrtask =
818       GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
819                                      s5r->remote_sock,
820                                      &do_read_remote, s5r);
821 }
822
823 /**
824  * Read from remote end
825  *
826  * @param cls closure
827  * @param tc scheduler context
828  */
829 static void
830 do_read_remote (void* cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
831 {
832   struct Socks5Request *s5r = cls;
833   
834   s5r->fwdrtask = GNUNET_SCHEDULER_NO_TASK;
835
836
837   if ((NULL != tc->write_ready) &&
838       (GNUNET_NETWORK_fdset_isset (tc->read_ready, s5r->remote_sock)) &&
839       (s5r->wbuf_len = GNUNET_NETWORK_socket_recv (s5r->remote_sock, s5r->wbuf,
840                                          sizeof (s5r->wbuf))))
841   {
842     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
843                 "Successfully read %d bytes from remote socket\n",
844                 s5r->wbuf_len);
845   }
846   else
847   {
848     if (s5r->wbuf_len == 0)
849       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
850                   "0 bytes received from remote... graceful shutdown!\n");
851     if (s5r->fwdwtask != GNUNET_SCHEDULER_NO_TASK)
852       GNUNET_SCHEDULER_cancel (s5r->fwdwtask);
853     if (s5r->rtask != GNUNET_SCHEDULER_NO_TASK)
854       GNUNET_SCHEDULER_cancel (s5r->rtask);
855     
856     GNUNET_NETWORK_socket_close (s5r->remote_sock);
857     s5r->remote_sock = NULL;
858     GNUNET_NETWORK_socket_close (s5r->sock);
859     GNUNET_free(s5r);
860
861     return;
862   }
863   
864   s5r->wtask = GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
865                                                s5r->sock,
866                                                &do_write, s5r);
867   
868 }
869
870
871 static int
872 add_handle_to_mhd (struct GNUNET_NETWORK_Handle *h)
873 {
874   int fd;
875   struct sockaddr *addr;
876   socklen_t len;
877
878   fd = GNUNET_NETWORK_get_fd (h);
879   addr = GNUNET_NETWORK_get_addr (h);
880   len = GNUNET_NETWORK_get_addrlen (h);
881
882   return MHD_add_connection (httpd, fd, addr, len);
883 }
884
885 /**
886  * Read data from incoming connection
887  *
888  * @param cls the closure
889  * @param tc the scheduler context
890  */
891 static void
892 do_read (void* cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
893 {
894   struct Socks5Request *s5r = cls;
895   struct socks5_client_hello *c_hello;
896   struct socks5_server_hello *s_hello;
897   struct socks5_client_request *c_req;
898   struct socks5_server_response *s_resp;
899
900   char domain[256];
901   uint8_t dom_len;
902   uint16_t req_port;
903   struct hostent *phost;
904   uint32_t remote_ip;
905   struct sockaddr_in remote_addr;
906   struct in_addr *r_sin_addr;
907
908   s5r->rtask = GNUNET_SCHEDULER_NO_TASK;
909
910   if ((NULL != tc->write_ready) &&
911       (GNUNET_NETWORK_fdset_isset (tc->read_ready, s5r->sock)) &&
912       (s5r->rbuf_len = GNUNET_NETWORK_socket_recv (s5r->sock, s5r->rbuf,
913                                          sizeof (s5r->rbuf))))
914   {
915     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
916                 "Successfully read %d bytes from socket\n",
917                 s5r->rbuf_len);
918   }
919   else
920   {
921     if (s5r->rbuf_len != 0)
922       GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "read");
923     else
924       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client disco!\n");
925
926     if (s5r->fwdrtask != GNUNET_SCHEDULER_NO_TASK)
927       GNUNET_SCHEDULER_cancel (s5r->fwdrtask);
928     if (s5r->wtask != GNUNET_SCHEDULER_NO_TASK)
929       GNUNET_SCHEDULER_cancel (s5r->wtask);
930     if (s5r->fwdwtask != GNUNET_SCHEDULER_NO_TASK)
931       GNUNET_SCHEDULER_cancel (s5r->fwdwtask);
932     GNUNET_NETWORK_socket_close (s5r->remote_sock);
933     GNUNET_NETWORK_socket_close (s5r->sock);
934     GNUNET_free(s5r);
935     return;
936   }
937
938   if (s5r->state == SOCKS5_INIT)
939   {
940     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
941                 "SOCKS5 init\n");
942     c_hello = (struct socks5_client_hello*)&s5r->rbuf;
943
944     GNUNET_assert (c_hello->version == SOCKS_VERSION_5);
945
946     s_hello = (struct socks5_server_hello*)&s5r->wbuf;
947     s5r->wbuf_len = sizeof( struct socks5_server_hello );
948
949     s_hello->version = c_hello->version;
950     s_hello->auth_method = SOCKS_AUTH_NONE;
951
952     /* Write response to client */
953     s5r->wtask = GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
954                                                 s5r->sock,
955                                                 &do_write, s5r);
956
957     s5r->rtask = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
958                                                 s5r->sock,
959                                                 &do_read, s5r);
960
961     s5r->state = SOCKS5_REQUEST;
962     return;
963   }
964
965   if (s5r->state == SOCKS5_REQUEST)
966   {
967     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
968                 "Processing SOCKS5 request\n");
969     c_req = (struct socks5_client_request*)&s5r->rbuf;
970     s_resp = (struct socks5_server_response*)&s5r->wbuf;
971     //Only 10byte for ipv4 response!
972     s5r->wbuf_len = 10;//sizeof (struct socks5_server_response);
973
974     GNUNET_assert (c_req->addr_type == 3);
975
976     dom_len = *((uint8_t*)(&(c_req->addr_type) + 1));
977     memset(domain, 0, sizeof(domain));
978     strncpy(domain, (char*)(&(c_req->addr_type) + 2), dom_len);
979     req_port = *((uint16_t*)(&(c_req->addr_type) + 2 + dom_len));
980
981     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
982                 "Requested connection is %s:%d\n",
983                 domain,
984                 ntohs(req_port));
985
986     if (is_tld (domain, GNUNET_GNS_TLD) ||
987         is_tld (domain, GNUNET_GNS_TLD_ZKEY))
988     {
989       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
990                   "Requested connection is gnunet tld\n",
991                   domain);
992
993       if (NULL == httpd)
994       {
995         GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
996                     _("Failed to start HTTP server\n"));
997         s_resp->version = 0x05;
998         s_resp->reply = 0x01;
999         s5r->wtask = 
1000           GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
1001                                         s5r->sock,
1002                                         &do_write, s5r);
1003         //ERROR!
1004         //TODO! close socket after the write! schedule task
1005         //GNUNET_NETWORK_socket_close (s5r->sock);
1006         //GNUNET_free(s5r);
1007         return;
1008       }
1009
1010       if (MHD_YES == add_handle_to_mhd ( s5r->sock ))
1011         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1012                     "Sucessfully added client to MHD!\n");
1013       s_resp->version = 0x05;
1014       s_resp->reply = 0x00;
1015       s_resp->reserved = 0x00;
1016       s_resp->addr_type = 0x01;
1017
1018       s5r->wtask =
1019         GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
1020                                         s5r->sock,
1021                                         &do_write, s5r);
1022       run_httpd ();
1023       //GNUNET_free ( s5r );
1024       //FIXME complete socks resp!
1025       return;
1026     }
1027     else
1028     {
1029       phost = (struct hostent*)gethostbyname (domain);
1030       if (phost == NULL)
1031       {
1032         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1033                     "Resolve %s error!\n", domain );
1034         s_resp->version = 0x05;
1035         s_resp->reply = 0x01;
1036         s5r->wtask = 
1037           GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
1038                                           s5r->sock,
1039                                           &do_write, s5r);
1040         //ERROR!
1041         //TODO! close socket after the write! schedule task
1042         //GNUNET_NETWORK_socket_close (s5r->sock);
1043         //GNUNET_free(s5r);
1044         return;
1045       }
1046
1047       s5r->remote_sock = GNUNET_NETWORK_socket_create (AF_INET,
1048                                                        SOCK_STREAM,
1049                                                        0);
1050       r_sin_addr = (struct in_addr*)(phost->h_addr);
1051       remote_ip = r_sin_addr->s_addr;
1052       memset(&remote_addr, 0, sizeof(remote_addr));
1053       remote_addr.sin_family = AF_INET;
1054 #if HAVE_SOCKADDR_IN_SIN_LEN
1055       remote_addr.sin_len = sizeof (remote_addr);
1056 #endif
1057       remote_addr.sin_addr.s_addr = remote_ip;
1058       remote_addr.sin_port = req_port;
1059       
1060       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1061                   "target server: %s:%u\n", inet_ntoa(remote_addr.sin_addr),
1062                   ntohs(req_port));
1063
1064       if ((GNUNET_OK !=
1065           GNUNET_NETWORK_socket_connect ( s5r->remote_sock,
1066                                           (const struct sockaddr*)&remote_addr,
1067                                           sizeof (remote_addr)))
1068           && (errno != EINPROGRESS))
1069       {
1070         GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "connect");
1071         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1072                     "socket request error...\n");
1073         s_resp->version = 0x05;
1074         s_resp->reply = 0x01;
1075         s5r->wtask =
1076           GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
1077                                           s5r->sock,
1078                                           &do_write, s5r);
1079         //TODO see above
1080         return;
1081       }
1082
1083       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1084                   "new remote connection\n");
1085
1086       s_resp->version = 0x05;
1087       s_resp->reply = 0x00;
1088       s_resp->reserved = 0x00;
1089       s_resp->addr_type = 0x01;
1090
1091       s5r->state = SOCKS5_DATA_TRANSFER;
1092
1093       s5r->wtask =
1094         GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
1095                                         s5r->sock,
1096                                         &do_write, s5r);
1097       s5r->rtask =
1098         GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
1099                                        s5r->sock,
1100                                        &do_read, s5r);
1101
1102     }
1103     return;
1104   }
1105
1106   if (s5r->state == SOCKS5_DATA_TRANSFER)
1107   {
1108     if ((s5r->remote_sock == NULL) || (s5r->rbuf_len == 0))
1109     {
1110       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1111                   "Closing connection to client\n");
1112       if (s5r->rtask != GNUNET_SCHEDULER_NO_TASK)
1113         GNUNET_SCHEDULER_cancel (s5r->rtask);
1114       if (s5r->fwdwtask != GNUNET_SCHEDULER_NO_TASK)
1115         GNUNET_SCHEDULER_cancel (s5r->fwdwtask);
1116       if (s5r->fwdrtask != GNUNET_SCHEDULER_NO_TASK)
1117         GNUNET_SCHEDULER_cancel (s5r->fwdrtask);
1118       if (s5r->fwdrtask != GNUNET_SCHEDULER_NO_TASK)
1119         GNUNET_SCHEDULER_cancel (s5r->fwdrtask);
1120       
1121       if (s5r->remote_sock != NULL)
1122         GNUNET_NETWORK_socket_close (s5r->remote_sock);
1123       GNUNET_NETWORK_socket_close (s5r->sock);
1124       GNUNET_free(s5r);
1125       return;
1126     }
1127
1128     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1129                 "forwarding %d bytes from client\n", s5r->rbuf_len);
1130
1131     s5r->fwdwtask =
1132       GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
1133                                       s5r->remote_sock,
1134                                       &do_write_remote, s5r);
1135
1136     if (s5r->fwdrtask == GNUNET_SCHEDULER_NO_TASK)
1137     {
1138       s5r->fwdrtask =
1139         GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
1140                                        s5r->remote_sock,
1141                                        &do_read_remote, s5r);
1142     }
1143
1144
1145   }
1146
1147   //GNUNET_CONTAINER_DLL_remove (s5conns.head, s5conns.tail, s5r);
1148
1149 }
1150
1151 /**
1152  * Accept new incoming connections
1153  *
1154  * @param cls the closure
1155  * @param tc the scheduler context
1156  */
1157 static void
1158 do_accept (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1159 {
1160   struct GNUNET_NETWORK_Handle *s;
1161   struct Socks5Request *s5r;
1162
1163   ltask = GNUNET_SCHEDULER_NO_TASK;
1164   if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1165     return;
1166
1167   ltask = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
1168                                          lsock,
1169                                          &do_accept, NULL);
1170
1171   s = GNUNET_NETWORK_socket_accept (lsock, NULL, NULL);
1172
1173   if (NULL == s)
1174   {
1175     GNUNET_log_strerror (GNUNET_ERROR_TYPE_INFO, "accept");
1176     return;
1177   }
1178
1179   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1180               "Got an inbound connection, waiting for data\n");
1181
1182   s5r = GNUNET_malloc (sizeof (struct Socks5Request));
1183   s5r->sock = s;
1184   s5r->state = SOCKS5_INIT;
1185   s5r->wtask = GNUNET_SCHEDULER_NO_TASK;
1186   s5r->fwdwtask = GNUNET_SCHEDULER_NO_TASK;
1187   s5r->fwdrtask = GNUNET_SCHEDULER_NO_TASK;
1188   s5r->rtask = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
1189                                               s5r->sock,
1190                                               &do_read, s5r);
1191   //GNUNET_CONTAINER_DLL_insert (s5conns.head, s5conns.tail, s5r);
1192 }
1193
1194 /**
1195  * Task run on shutdown
1196  *
1197  * @param cls closure
1198  * @param tc task context
1199  */
1200 static void
1201 do_shutdown (void *cls,
1202              const struct GNUNET_SCHEDULER_TaskContext *tc)
1203 {
1204   if (GNUNET_SCHEDULER_NO_TASK != httpd_task)
1205   {
1206     GNUNET_SCHEDULER_cancel (httpd_task);
1207     httpd_task = GNUNET_SCHEDULER_NO_TASK;
1208   }
1209   
1210   if (GNUNET_SCHEDULER_NO_TASK != curl_download_task)
1211   {
1212     GNUNET_SCHEDULER_cancel (curl_download_task);
1213     curl_download_task = GNUNET_SCHEDULER_NO_TASK;
1214   }
1215
1216   if (NULL != httpd)
1217   {
1218     MHD_stop_daemon (httpd);
1219     httpd = NULL;
1220   }
1221 }
1222
1223 /**
1224  * Main function that will be run
1225  *
1226  * @param cls closure
1227  * @param args remaining command-line arguments
1228  * @param cfgfile name of the configuration file used (for saving, can be NULL!)
1229  * @param cfg configuration
1230  */
1231 static void
1232 run (void *cls, char *const *args, const char *cfgfile,
1233      const struct GNUNET_CONFIGURATION_Handle *cfg)
1234 {
1235   struct sockaddr_in sa;
1236
1237   memset (&sa, 0, sizeof (sa));
1238   sa.sin_family = AF_INET;
1239   sa.sin_port = htons (port);
1240 #if HAVE_SOCKADDR_IN_SIN_LEN
1241   sa.sin_len = sizeof (sa);
1242 #endif
1243
1244   lsock = GNUNET_NETWORK_socket_create (AF_INET,
1245                                         SOCK_STREAM,
1246                                         0);
1247
1248   if ((NULL == lsock) ||
1249       (GNUNET_OK !=
1250        GNUNET_NETWORK_socket_bind (lsock, (const struct sockaddr *) &sa,
1251                                    sizeof (sa))))
1252   {
1253     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1254                 "Failed to create listen socket bound to `%s'",
1255                 GNUNET_a2s ((const struct sockaddr *) &sa, sizeof (sa)));
1256     if (NULL != lsock)
1257       GNUNET_NETWORK_socket_close (lsock);
1258     return;
1259   }
1260
1261   if (GNUNET_OK != GNUNET_NETWORK_socket_listen (lsock, 5))
1262   {
1263     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1264                 "Failed to listen on socket bound to `%s'",
1265                 GNUNET_a2s ((const struct sockaddr *) &sa, sizeof (sa)));
1266     return;
1267   }
1268
1269   ltask = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
1270                                          lsock, &do_accept, NULL);
1271
1272   ctasks_head = NULL;
1273   ctasks_tail = NULL;
1274
1275   if (0 != curl_global_init (CURL_GLOBAL_WIN32))
1276   {
1277     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1278                 "cURL global init failed!\n");
1279     return;
1280   }
1281
1282   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1283               "Proxy listens on port %u\n",
1284               port);
1285   
1286   httpd = MHD_start_daemon (MHD_USE_DEBUG, 4444,
1287                                NULL, NULL,
1288                                &create_response, NULL,
1289                                MHD_OPTION_CONNECTION_LIMIT, (unsigned int) 128,
1290                                MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) 16,
1291                                MHD_OPTION_NOTIFY_COMPLETED,
1292                                NULL, NULL,
1293                                MHD_OPTION_END);
1294   run_httpd ();
1295
1296   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
1297                                 &do_shutdown, NULL);
1298
1299 }
1300
1301 /**
1302  * The main function for gnunet-gns-proxy.
1303  *
1304  * @param argc number of arguments from the command line
1305  * @param argv command line arguments
1306  * @return 0 ok, 1 on error
1307  */
1308 int
1309 main (int argc, char *const *argv)
1310 {
1311   static const struct GNUNET_GETOPT_CommandLineOption options[] = {
1312     {'p', "port", NULL,
1313      gettext_noop ("listen on specified port"), 1,
1314      &GNUNET_GETOPT_set_string, &port},
1315     GNUNET_GETOPT_OPTION_END
1316   };
1317
1318   int ret;
1319
1320   GNUNET_log_setup ("gnunet-gns-proxy", "WARNING", NULL);
1321   ret =
1322       (GNUNET_OK ==
1323        GNUNET_PROGRAM_run (argc, argv, "gnunet-gns-proxy",
1324                            _("GNUnet GNS proxy"),
1325                            options,
1326                            &run, NULL)) ? 0 : 1;
1327   return ret;
1328 }