Script for CA generation. Making it easy with config option
[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 <gnunet_gns_service.h>
24 #include <microhttpd.h>
25 #include <curl/curl.h>
26 #include <regex.h>
27 #include "gns_proxy_proto.h"
28 #include "gns.h"
29
30 /** SSL **/
31 #include <gnutls/gnutls.h>
32 #include <gnutls/x509.h>
33 #include <gnutls/abstract.h>
34 #include <gnutls/crypto.h>
35 #include <time.h>
36
37 #define GNUNET_GNS_PROXY_PORT 7777
38 #define MAX_MHD_CONNECTIONS 300
39
40 /* MHD/cURL defines */
41 #define BUF_WAIT_FOR_CURL 0
42 #define BUF_WAIT_FOR_MHD 1
43 #define HTML_HDR_CONTENT "Content-Type: text/html\r\n"
44
45 /* regexp */
46 //#define RE_DOTPLUS "<a href=\"http://(([A-Za-z]+[.])+)([+])"
47 #define RE_A_HREF  "<a href=\"https?://(([A-Za-z0-9]+[.])+)([+]|zkey)"
48 #define RE_N_MATCHES 4
49
50 /* The usual suspects */
51 #define HTTP_PORT 80
52 #define HTTPS_PORT 443
53
54
55 /**
56  * A structure for CA cert/key
57  */
58 struct ProxyCA
59 {
60   /* The certificate */
61   gnutls_x509_crt_t cert;
62
63   /* The private key */
64   gnutls_x509_privkey_t key;
65 };
66
67
68 /**
69  * Structure for GNS certificates
70  */
71 struct ProxyGNSCertificate
72 {
73   /* The certificate as PEM */
74   char cert[10 * 1024];
75
76   /* The private key as PEM */
77   char key[10 * 1024];
78 };
79
80
81 /**
82  * A structure for socks requests
83  */
84 struct Socks5Request
85 {
86   /* The client socket */
87   struct GNUNET_NETWORK_Handle *sock;
88
89   /* The server socket */
90   struct GNUNET_NETWORK_Handle *remote_sock;
91   
92   /* The socks state */
93   int state;
94   
95   /* Client socket read task */
96   GNUNET_SCHEDULER_TaskIdentifier rtask;
97
98   /* Server socket read task */
99   GNUNET_SCHEDULER_TaskIdentifier fwdrtask;
100
101   /* Client socket write task */
102   GNUNET_SCHEDULER_TaskIdentifier wtask;
103
104   /* Server socket write task */
105   GNUNET_SCHEDULER_TaskIdentifier fwdwtask;
106
107   /* Read buffer */
108   char rbuf[2048];
109
110   /* Write buffer */
111   char wbuf[2048];
112
113   /* Length of data in read buffer */
114   unsigned int rbuf_len;
115
116   /* Length of data in write buffer */
117   unsigned int wbuf_len;
118
119   /* This handle is scheduled for cleanup? */
120   int cleanup;
121
122   /* Shall we close the client socket on cleanup? */
123   int cleanup_sock;
124 };
125
126
127 /**
128  * A structure for all running Httpds
129  */
130 struct MhdHttpList
131 {
132   /* DLL for httpds */
133   struct MhdHttpList *prev;
134
135   /* DLL for httpds */
136   struct MhdHttpList *next;
137
138   /* is this an ssl daemon? */
139   int is_ssl;
140
141   /* the domain name to server (only important for SSL) */
142   char domain[256];
143
144   /* The daemon handle */
145   struct MHD_Daemon *daemon;
146
147   /* Optional proxy certificate used */
148   struct ProxyGNSCertificate *proxy_cert;
149
150   /* The task ID */
151   GNUNET_SCHEDULER_TaskIdentifier httpd_task;
152 };
153
154 /**
155  * A structure for MHD<->cURL streams
156  */
157 struct ProxyCurlTask
158 {
159   /* DLL for tasks */
160   struct ProxyCurlTask *prev;
161
162   /* DLL for tasks */
163   struct ProxyCurlTask *next;
164
165   /* Handle to cURL */
166   CURL *curl;
167
168   /* Optional header replacements for curl (LEHO) */
169   struct curl_slist *headers;
170
171   /* Optional resolver replacements for curl (LEHO) */
172   struct curl_slist *resolver;
173
174   /* The URL to fetch */
175   char url[2048];
176
177   /* The cURL write buffer / MHD read buffer */
178   char buffer[CURL_MAX_WRITE_SIZE];
179
180   /* The pointer to the data in the buffer */
181   char *buffer_ptr;
182
183   /* The buffer status (BUF_WAIT_FOR_CURL or BUF_WAIT_FOR_MHD) */
184   int buf_status;
185
186   /* Number of bytes in buffer */
187   unsigned int bytes_in_buffer;
188
189   /* Indicates wheather the download is in progress */
190   int download_in_progress;
191
192   /* Indicates wheather the download was successful */
193   int download_successful;
194
195   /* Indicates wheather the download failed */
196   int download_error;
197
198   /* Indicates wheather we need to parse HTML */
199   int parse_content;
200
201   /* Indicates wheather we are postprocessing the HTML right now */
202   int is_postprocessing;
203
204   /* Indicates wheather postprocessing has finished */
205   int pp_finished;
206
207   /* Task ID of the postprocessing task */
208   GNUNET_SCHEDULER_TaskIdentifier pp_task;
209
210   /* The postprocessing buffer TODO length? */
211   char pp_buf[256];
212
213   /* The authority of the corresponding host (site of origin) */
214   char authority[256];
215
216   /* The hostname (Host header field) */
217   char host[256];
218
219   /* The LEgacy HOstname (can be empty) */
220   char leho[256];
221
222   /* The associated daemon list entry */
223   struct MhdHttpList *mhd;
224   
225 };
226
227 /* The port the proxy is running on (default 7777) */
228 static unsigned long port = GNUNET_GNS_PROXY_PORT;
229
230 /* The CA file (pem) to use for the proxy CA */
231 static char* cafile_opt;
232
233 /* The listen socket of the proxy */
234 static struct GNUNET_NETWORK_Handle *lsock;
235
236 /* The listen task ID */
237 GNUNET_SCHEDULER_TaskIdentifier ltask;
238
239 /* The cURL download task */
240 GNUNET_SCHEDULER_TaskIdentifier curl_download_task;
241
242 /* The non SSL httpd daemon handle */
243 static struct MHD_Daemon *httpd;
244
245 /* Number of current mhd connections */
246 static unsigned int total_mhd_connections;
247
248 /* The cURL multi handle */
249 static CURLM *curl_multi;
250
251 /* Handle to the GNS service */
252 static struct GNUNET_GNS_Handle *gns_handle;
253
254 /* DLL for ProxyCurlTasks */
255 static struct ProxyCurlTask *ctasks_head;
256
257 /* DLL for ProxyCurlTasks */
258 static struct ProxyCurlTask *ctasks_tail;
259
260 /* DLL for http daemons */
261 static struct MhdHttpList *mhd_httpd_head;
262
263 /* DLL for http daemons */
264 static struct MhdHttpList *mhd_httpd_tail;
265
266 /* Handle to the regex for dotplus (.+) replacement in HTML */
267 static regex_t re_dotplus;
268
269 /* The users local GNS zone hash */
270 static struct GNUNET_CRYPTO_ShortHashCode local_gns_zone;
271
272 /* The users local shorten zone hash */
273 static struct GNUNET_CRYPTO_ShortHashCode local_shorten_zone;
274
275 /* The CA for SSL certificate generation */
276 static struct ProxyCA proxy_ca;
277
278 /* UNIX domain socket for mhd */
279 struct GNUNET_NETWORK_Handle *mhd_unix_socket;
280
281 /* Shorten names? */
282 int use_shorten;
283
284 /**
285  * Checks if name is in tld
286  *
287  * @param name the name to check
288  * @param tld the TLD to check for
289  * @return GNUNET_YES or GNUNET_NO
290  */
291 int
292 is_tld(const char* name, const char* tld)
293 {
294   int offset = 0;
295
296   if (strlen(name) <= strlen(tld))
297   {
298     return GNUNET_NO;
299   }
300
301   offset = strlen(name)-strlen(tld);
302   if (strcmp (name+offset, tld) != 0)
303   {
304     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
305                "%s is not in .%s TLD\n", name, tld);
306     return GNUNET_NO;
307   }
308
309   return GNUNET_YES;
310 }
311
312
313 /**
314  * Read HTTP request header field 'Host'
315  *
316  * @param cls buffer to write to
317  * @param kind value kind
318  * @param key field key
319  * @param value field value
320  * @return MHD_NO when Host found
321  */
322 static int
323 con_val_iter (void *cls,
324               enum MHD_ValueKind kind,
325               const char *key,
326               const char *value)
327 {
328   char* buf = (char*)cls;
329
330   if (0 == strcmp ("Host", key))
331   {
332     strcpy (buf, value);
333     return MHD_NO;
334   }
335   return MHD_YES;
336 }
337
338
339 /**
340  * Check HTTP response header for mime
341  *
342  * @param buffer curl buffer
343  * @param size curl blocksize
344  * @param nmemb curl blocknumber
345  * @param cls handle
346  * @return size of read bytes
347  */
348 static size_t
349 curl_check_hdr (void *buffer, size_t size, size_t nmemb, void *cls)
350 {
351   size_t bytes = size * nmemb;
352   struct ProxyCurlTask *ctask = cls;
353   char hdr[bytes+1];
354
355   memcpy (hdr, buffer, bytes);
356   hdr[bytes] = '\0';
357
358   if (0 == strcmp (hdr, HTML_HDR_CONTENT))
359   {
360     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
361                 "Got HTML HTTP response header\n");
362     ctask->parse_content = GNUNET_YES;
363   }
364
365   return bytes;
366 }
367
368 /**
369  * schedule mhd
370  *
371  * @param hd a http daemon list entry
372  */
373 static void
374 run_httpd (struct MhdHttpList *hd);
375
376
377 /**
378  * schedule all mhds
379  *
380  */
381 static void
382 run_httpds (void);
383
384
385 /**
386  * Task that simply runs MHD main loop
387  *
388  * @param cls NULL
389  * @param tc task context
390  */
391 static void
392 run_mhd (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
393 {
394
395   struct MhdHttpList *hd = cls;
396
397   //for (hd=mhd_httpd_head; hd != NULL; hd = hd->next)
398     MHD_run (hd->daemon);
399 }
400
401
402 /**
403  * Process cURL download bits
404  *
405  * @param ptr buffer with data
406  * @param size size of a record
407  * @param nmemb number of records downloaded
408  * @param ctx context
409  * @return number of processed bytes
410  */
411 static size_t
412 callback_download (void *ptr, size_t size, size_t nmemb, void *ctx)
413 {
414   const char *cbuf = ptr;
415   size_t total;
416   struct ProxyCurlTask *ctask = ctx;
417
418   //MHD_run (httpd);
419
420   total = size*nmemb;
421
422   if (total == 0)
423   {
424     return total;
425   }
426
427   if (total > sizeof (ctask->buffer))
428   {
429     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
430                 "CURL gave us too much data to handle (%d)!\n",
431                 total);
432     return 0;
433   }
434   
435   if (ctask->buf_status == BUF_WAIT_FOR_MHD)
436   {
437     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
438                 "CURL: Waiting for MHD (%s)\n", ctask->url);
439     return CURL_WRITEFUNC_PAUSE;
440   }
441
442
443   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
444               "CURL: Copying to MHD (%s, %d)\n", ctask->url, total);
445   memcpy (ctask->buffer, cbuf, total);
446   ctask->bytes_in_buffer = total;
447   ctask->buffer_ptr = ctask->buffer;
448
449   ctask->buf_status = BUF_WAIT_FOR_MHD;
450
451   //GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
452   //            "cURL chunk:\n%s\n", (char*)ctask->buffer);
453   //run_mhd (NULL, NULL);
454   GNUNET_SCHEDULER_add_now (&run_mhd, ctask->mhd);
455   return total;
456 }
457
458
459
460 /**
461  * Callback to free content
462  *
463  * @param cls content to free
464  */
465 static void
466 mhd_content_free (void *cls)
467 {
468   struct ProxyCurlTask *ctask = cls;
469
470   if (NULL != ctask->headers)
471     curl_slist_free_all (ctask->headers);
472
473   if (NULL != ctask->curl)
474     curl_easy_cleanup (ctask->curl);
475
476   ctask->curl = NULL;
477
478   GNUNET_free (ctask);
479
480 }
481
482
483 /**
484  * Shorten result callback
485  *
486  * @param cls the proxycurltask
487  * @param short_name the shortened name (NULL on error)
488  */
489 static void
490 process_shorten (void* cls, const char* short_name)
491 {
492   struct ProxyCurlTask *ctask = cls;
493
494   char tmp[strlen(ctask->pp_buf)]; //TODO length
495
496   if (NULL == short_name)
497   {
498     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
499                 "MHD PP: Unable to shorten %s\n",
500                 ctask->pp_buf);
501     return;
502   }
503
504   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
505               "MHD PP: Shorten %s -> %s\n",
506               ctask->pp_buf,
507               short_name);
508
509   sprintf (tmp, "<a href=\"http://%s", short_name);
510   strcpy (ctask->pp_buf, tmp);
511
512   ctask->pp_finished = GNUNET_YES;
513
514   GNUNET_SCHEDULER_add_now (&run_mhd, ctask->mhd);
515 }
516
517
518 /**
519  * Postprocessing task that uses GNS to shorten names
520  *
521  * @param cls the proxycurltask
522  * @param tc the task context
523  *
524 static void
525 postprocess_name (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
526 {
527   struct ProxyCurlTask *ctask = cls;
528   char tmp[strlen(ctask->pp_buf)];
529
530   sprintf ( tmp, "%s%s", ctask->pp_buf, ctask->authority);
531
532   GNUNET_GNS_shorten (gns_handle,
533                       tmp,
534                       &process_shorten,
535                       ctask);
536
537 }
538 */
539
540 /**
541  * Callback for MHD response
542  *
543  * @param cls closure
544  * @param pos in buffer
545  * @param buf buffer
546  * @param max space in buffer
547  */
548 static ssize_t
549 mhd_content_cb (void *cls,
550                 uint64_t pos,
551                 char* buf,
552                 size_t max)
553 {
554   struct ProxyCurlTask *ctask = cls;
555   ssize_t copied = 0;
556   size_t bytes_to_copy;
557   int nomatch;
558   char *hostptr;
559   regmatch_t m[RE_N_MATCHES];
560
561   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
562               "MHD: content cb\n");
563
564   if (ctask->download_successful &&
565       (ctask->buf_status == BUF_WAIT_FOR_CURL))
566   {
567     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
568                 "MHD: sending response for %s\n", ctask->url);
569     ctask->download_in_progress = GNUNET_NO;
570     curl_multi_remove_handle (curl_multi, ctask->curl);
571     curl_easy_cleanup (ctask->curl);
572     GNUNET_SCHEDULER_add_now (&run_mhd, ctask->mhd);
573     total_mhd_connections--;
574     return MHD_CONTENT_READER_END_OF_STREAM;
575   }
576   
577   if (ctask->download_error &&
578       (ctask->buf_status == BUF_WAIT_FOR_CURL))
579   {
580     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
581                 "MHD: sending error response\n");
582     ctask->download_in_progress = GNUNET_NO;
583     curl_multi_remove_handle (curl_multi, ctask->curl);
584     curl_easy_cleanup (ctask->curl);
585     GNUNET_SCHEDULER_add_now (&run_mhd, ctask->mhd);
586     total_mhd_connections--;
587     return MHD_CONTENT_READER_END_WITH_ERROR;
588   }
589
590   if ( ctask->buf_status == BUF_WAIT_FOR_CURL )
591     return 0;
592
593   bytes_to_copy = ctask->bytes_in_buffer;
594   
595   if (ctask->parse_content == GNUNET_YES)
596   {
597
598     GNUNET_log ( GNUNET_ERROR_TYPE_DEBUG,
599                  "MHD: We need to parse the HTML %s\n", ctask->buffer_ptr);
600
601     nomatch = regexec ( &re_dotplus, ctask->buffer_ptr, RE_N_MATCHES, m, 0);
602
603     if (nomatch)
604     {
605       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
606                   "MHD RE: No match\n");
607     }
608     else
609     {
610       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
611                   "MHD RE: Match\n");
612
613       GNUNET_assert (m[1].rm_so != -1);
614
615       hostptr = ctask->buffer_ptr+m[1].rm_so;
616
617       if (m[0].rm_so > 0)
618       {
619         bytes_to_copy = m[0].rm_so;
620         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
621                     "Copying %d bytes.\n", m[0].rm_so);
622
623
624       }
625       else
626       {
627         if (ctask->is_postprocessing == GNUNET_YES)
628         {
629           
630           /*Done?*/
631           if ( ctask->pp_finished == GNUNET_NO )
632           {
633             GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
634                         "MHD PP: Waiting for PP of %s\n", ctask->pp_buf);
635             return 0;
636           }
637           
638           ctask->is_postprocessing = GNUNET_NO;
639
640           ctask->bytes_in_buffer -= m[0].rm_eo;//(m[1].rm_eo-m[1].rm_so);
641           ctask->buffer_ptr += m[0].rm_eo;//(m[1].rm_eo-m[1].rm_so);
642           GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
643                       "Skipping next %d bytes in buffer\n", m[0].rm_eo);
644
645           GNUNET_SCHEDULER_add_now (&run_mhd, ctask->mhd);
646
647           if ( strlen (ctask->pp_buf) <= max )
648           {
649             GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
650                         "Copying postprocessed %s.\n", ctask->pp_buf);
651             memcpy ( buf, ctask->pp_buf, strlen (ctask->pp_buf) );
652             GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
653                         "Done %s.\n", buf);
654             ctask->is_postprocessing = GNUNET_NO;
655             return strlen (ctask->pp_buf);
656           }
657           
658           return 0;
659         }
660
661         memset (ctask->pp_buf, 0, sizeof(ctask->pp_buf));
662         
663         /* If .+ extend with authority */
664         if (*(ctask->buffer_ptr+m[1].rm_eo) == '+')
665         {
666           GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
667                       "Links is .+\n");
668            memcpy (ctask->pp_buf, hostptr, (m[1].rm_eo-m[1].rm_so));
669            strcpy ( ctask->pp_buf+strlen(ctask->pp_buf),
670                     ctask->authority);
671         }
672         /* If .zkey simply copy the name */
673         else
674         {
675           GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
676                       "Link is zkey\n");
677           memcpy (ctask->pp_buf, hostptr, (m[1].rm_eo-m[1].rm_so + strlen (GNUNET_GNS_TLD_ZKEY)));
678         }
679
680         ctask->is_postprocessing = GNUNET_YES;
681         ctask->pp_finished = GNUNET_NO;
682         
683         GNUNET_GNS_shorten (gns_handle,
684                            ctask->pp_buf,
685                            &process_shorten,
686                            ctask);
687
688         //postprocess_name(ctask, NULL);
689         //ctask->pp_task = GNUNET_SCHEDULER_add_now (&postprocess_name, ctask);
690
691         return 0;
692       }
693     }
694   }
695
696   if ( bytes_to_copy > max )
697   {
698     GNUNET_log ( GNUNET_ERROR_TYPE_DEBUG,
699                  "MHD: buffer in response too small! (%s)\n",
700                  ctask->url);
701     memcpy ( buf, ctask->buffer_ptr, max);
702     ctask->bytes_in_buffer -= max;
703     ctask->buffer_ptr += max;
704     copied = max;
705   }
706   else
707   {
708     GNUNET_log ( GNUNET_ERROR_TYPE_DEBUG,
709                  "MHD: copying %d bytes to mhd response at offset %d\n",
710                  bytes_to_copy, pos);
711
712     memcpy ( buf, ctask->buffer_ptr, bytes_to_copy );
713     copied = bytes_to_copy;
714     if (bytes_to_copy < ctask->bytes_in_buffer)
715     {
716       ctask->bytes_in_buffer -= bytes_to_copy;
717       ctask->buffer_ptr += bytes_to_copy;
718     }
719     else
720     {
721       ctask->bytes_in_buffer = 0;
722       ctask->buf_status = BUF_WAIT_FOR_CURL;
723       ctask->buffer_ptr = ctask->buffer;
724       curl_easy_pause (ctask->curl, CURLPAUSE_CONT);
725       GNUNET_SCHEDULER_add_now (&run_mhd, ctask->mhd);
726     }
727   }
728
729   GNUNET_SCHEDULER_add_now (&run_mhd, ctask->mhd);
730
731   return copied;
732 }
733
734
735
736 /**
737  * Task that is run when we are ready to receive more data
738  * from curl
739  *
740  * @param cls closure
741  * @param tc task context
742  */
743 static void
744 curl_task_download (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
745
746 /**
747  * Ask cURL for the select sets and schedule download
748  */
749 static void
750 curl_download_prepare ()
751 {
752   CURLMcode mret;
753   fd_set rs;
754   fd_set ws;
755   fd_set es;
756   int max;
757   struct GNUNET_NETWORK_FDSet *grs;
758   struct GNUNET_NETWORK_FDSet *gws;
759   long to;
760   struct GNUNET_TIME_Relative rtime;
761
762   max = -1;
763   FD_ZERO (&rs);
764   FD_ZERO (&ws);
765   FD_ZERO (&es);
766   mret = curl_multi_fdset (curl_multi, &rs, &ws, &es, &max);
767
768   if (mret != CURLM_OK)
769   {
770     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
771                 "%s failed at %s:%d: `%s'\n",
772                 "curl_multi_fdset", __FILE__, __LINE__,
773                 curl_multi_strerror (mret));
774     //TODO cleanup here?
775     return;
776   }
777
778   mret = curl_multi_timeout (curl_multi, &to);
779   rtime = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, to);
780
781   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
782               "cURL multi fds: max=%d timeout=%llu\n", max, to);
783
784   grs = GNUNET_NETWORK_fdset_create ();
785   gws = GNUNET_NETWORK_fdset_create ();
786   GNUNET_NETWORK_fdset_copy_native (grs, &rs, max + 1);
787   GNUNET_NETWORK_fdset_copy_native (gws, &ws, max + 1);
788   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
789               "Scheduling task cURL\n");
790
791   if (curl_download_task != GNUNET_SCHEDULER_NO_TASK)
792     GNUNET_SCHEDULER_cancel (curl_download_task);
793   
794   curl_download_task =
795     GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
796                                  rtime,
797                                  grs, gws,
798                                  &curl_task_download, curl_multi);
799   GNUNET_NETWORK_fdset_destroy (gws);
800   GNUNET_NETWORK_fdset_destroy (grs);
801
802 }
803
804
805 /**
806  * Task that is run when we are ready to receive more data
807  * from curl
808  *
809  * @param cls closure
810  * @param tc task context
811  */
812 static void
813 curl_task_download (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
814 {
815   int running;
816   int msgnum;
817   struct CURLMsg *msg;
818   CURLMcode mret;
819   struct ProxyCurlTask *ctask;
820   int num_ctasks;
821
822   curl_download_task = GNUNET_SCHEDULER_NO_TASK;
823
824   if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
825   {
826     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
827                 "Shutdown requested while trying to download\n");
828     //TODO cleanup
829     return;
830   }
831   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
832               "Ready to dl\n");
833
834   do
835   {
836     running = 0;
837     num_ctasks = 0;
838     
839     mret = curl_multi_perform (curl_multi, &running);
840
841     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
842                 "Running curl tasks: %d\n", running);
843
844     ctask = ctasks_head;
845     for (; ctask != NULL; ctask = ctask->next)
846     {
847       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
848                   "CTask: %s\n", ctask->url);
849       num_ctasks++;
850     }
851
852     if (num_ctasks != running)
853     {
854       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
855                   "%d ctasks, %d curl running\n", num_ctasks, running);
856     }
857     
858     do
859     {
860       ctask = ctasks_head;
861       msg = curl_multi_info_read (curl_multi, &msgnum);
862       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
863                   "Messages left: %d\n", msgnum);
864       
865       if (msg == NULL)
866         break;
867       switch (msg->msg)
868       {
869        case CURLMSG_DONE:
870          if ((msg->data.result != CURLE_OK) &&
871              (msg->data.result != CURLE_GOT_NOTHING))
872          {
873            GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
874                        "Download curl failed");
875             
876            for (; ctask != NULL; ctask = ctask->next)
877            {
878              if (memcmp (msg->easy_handle, ctask->curl, sizeof (CURL)) != 0)
879                continue;
880              
881              GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
882                          "Download curl failed for task %s: %s.\n",
883                          ctask->url,
884                          curl_easy_strerror (msg->data.result));
885              ctask->download_successful = GNUNET_NO;
886              ctask->download_error = GNUNET_YES;
887              //curl_multi_remove_handle (curl_multi, ctask->curl);
888              //curl_easy_cleanup (ctask->curl);
889              GNUNET_CONTAINER_DLL_remove (ctasks_head, ctasks_tail,
890                                           ctask);
891              break;
892            }
893            GNUNET_assert (ctask != NULL);
894          }
895          else
896          {
897            GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
898                        "cURL download completed.\n");
899
900            for (; ctask != NULL; ctask = ctask->next)
901            {
902              if (memcmp (msg->easy_handle, ctask->curl, sizeof (CURL)) != 0)
903                continue;
904              
905              GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
906                          "cURL task %s found.\n", ctask->url);
907              ctask->download_successful = GNUNET_YES;
908              //curl_multi_remove_handle (curl_multi, ctask->curl);
909              //curl_easy_cleanup (ctask->curl);
910              GNUNET_CONTAINER_DLL_remove (ctasks_head, ctasks_tail,
911                                           ctask);
912              break;
913            }
914            GNUNET_assert (ctask != NULL);
915          }
916          GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
917                      "curl end %s\n", curl_easy_strerror(msg->data.result));
918          break;
919        default:
920          GNUNET_assert (0);
921          break;
922       }
923     } while (msgnum > 0);
924     
925     num_ctasks=0;
926     for (ctask=ctasks_head; ctask != NULL; ctask = ctask->next)
927     {
928       num_ctasks++;
929     }
930     
931     if (num_ctasks != running)
932     {
933       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
934                   "%d ctasks, %d curl running\n", num_ctasks, running);
935     }
936
937     GNUNET_assert ( num_ctasks == running );
938
939     run_httpds ();
940
941   } while (mret == CURLM_CALL_MULTI_PERFORM);
942   
943   
944   if (mret != CURLM_OK)
945   {
946     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "%s failed at %s:%d: `%s'\n",
947                 "curl_multi_perform", __FILE__, __LINE__,
948                 curl_multi_strerror (mret));
949   }
950   curl_download_prepare();
951 }
952
953 /**
954  * Process LEHO lookup
955  *
956  * @param cls the ctask
957  * @param rd_count number of records returned
958  * @param rd record data
959  */
960 static void
961 process_leho_lookup (void *cls,
962                      uint32_t rd_count,
963                      const struct GNUNET_NAMESTORE_RecordData *rd)
964 {
965   struct ProxyCurlTask *ctask = cls;
966   char hosthdr[262]; //256 + "Host: "
967   int i;
968   CURLcode ret;
969   CURLMcode mret;
970   struct hostent *phost;
971   char *ssl_ip;
972   char resolvename[512];
973   char curlurl[512];
974
975   ctask->headers = NULL;
976
977   strcpy (ctask->leho, "");
978
979   if (rd_count == 0)
980     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
981                 "No LEHO present!\n");
982
983   for (i=0; i<rd_count; i++)
984   {
985     if (rd[i].record_type != GNUNET_GNS_RECORD_LEHO)
986       continue;
987
988     memcpy (ctask->leho, rd[i].data, rd[i].data_size);
989
990     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
991                 "Found LEHO %s for %s\n", ctask->leho, ctask->url);
992   }
993
994   if (0 != strcmp (ctask->leho, ""))
995   {
996     sprintf (hosthdr, "%s%s", "Host: ", ctask->leho);
997     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
998                 "New HTTP header value: %s\n", hosthdr);
999     ctask->headers = curl_slist_append (ctask->headers, hosthdr);
1000     GNUNET_assert (NULL != ctask->headers);
1001     ret = curl_easy_setopt (ctask->curl, CURLOPT_HTTPHEADER, ctask->headers);
1002     if (CURLE_OK != ret)
1003     {
1004       GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "%s failed at %s:%d: `%s'\n",
1005                            "curl_easy_setopt", __FILE__, __LINE__, curl_easy_strerror(ret));
1006     }
1007
1008   }
1009
1010   if (ctask->mhd->is_ssl)
1011   {
1012     phost = (struct hostent*)gethostbyname (ctask->host);
1013
1014     if (phost!=NULL)
1015     {
1016       ssl_ip = inet_ntoa(*((struct in_addr*)(phost->h_addr)));
1017       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1018                   "SSL target server: %s\n", ssl_ip);
1019       sprintf (resolvename, "%s:%d:%s", ctask->leho, HTTPS_PORT, ssl_ip);
1020       ctask->resolver = curl_slist_append ( ctask->resolver, resolvename);
1021       curl_easy_setopt (ctask->curl, CURLOPT_RESOLVE, ctask->resolver);
1022       sprintf (curlurl, "https://%s%s", ctask->leho, ctask->url);
1023       curl_easy_setopt (ctask->curl, CURLOPT_URL, curlurl);
1024     }
1025     else
1026     {
1027       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1028                   "gethostbyname failed for %s!\n", ctask->host);
1029       ctask->download_successful = GNUNET_NO;
1030       ctask->download_error = GNUNET_YES;
1031       return;
1032     }
1033   }
1034
1035   if (CURLM_OK != (mret=curl_multi_add_handle (curl_multi, ctask->curl)))
1036   {
1037     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1038                 "%s failed at %s:%d: `%s'\n",
1039                 "curl_multi_add_handle", __FILE__, __LINE__,
1040                 curl_multi_strerror (mret));
1041     ctask->download_successful = GNUNET_NO;
1042     ctask->download_error = GNUNET_YES;
1043     return;
1044   }
1045   GNUNET_CONTAINER_DLL_insert (ctasks_head, ctasks_tail, ctask);
1046
1047   curl_download_prepare ();
1048
1049 }
1050
1051 /**
1052  * Initialize download and trigger curl
1053  *
1054  * @param cls the proxycurltask
1055  * @param auth_name the name of the authority (site of origin) of ctask->host
1056  *
1057  */
1058 static void
1059 process_get_authority (void *cls,
1060                        const char* auth_name)
1061 {
1062   struct ProxyCurlTask *ctask = cls;
1063
1064   if (NULL == auth_name)
1065   {
1066     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1067                 "Get authority failed!\n");
1068     strcpy (ctask->authority, "");
1069   }
1070   else
1071   {
1072     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1073                 "Get authority yielded %s\n", auth_name);
1074     strcpy (ctask->authority, auth_name);
1075   }
1076
1077   GNUNET_GNS_lookup_zone (gns_handle,
1078                           ctask->host,
1079                           &local_gns_zone,
1080                           &local_shorten_zone,
1081                           GNUNET_GNS_RECORD_LEHO,
1082                           GNUNET_YES, //Only cached for performance
1083                           &process_leho_lookup,
1084                           ctask);
1085 }
1086
1087 /**
1088  * Main MHD callback for handling requests.
1089  *
1090  * @param cls unused
1091  * @param con MHD connection handle
1092  * @param url the url in the request
1093  * @param meth the HTTP method used ("GET", "PUT", etc.)
1094  * @param ver the HTTP version string (i.e. "HTTP/1.1")
1095  * @param upload_data the data being uploaded (excluding HEADERS,
1096  *        for a POST that fits into memory and that is encoded
1097  *        with a supported encoding, the POST data will NOT be
1098  *        given in upload_data and is instead available as
1099  *        part of MHD_get_connection_values; very large POST
1100  *        data *will* be made available incrementally in
1101  *        upload_data)
1102  * @param upload_data_size set initially to the size of the
1103  *        upload_data provided; the method must update this
1104  *        value to the number of bytes NOT processed;
1105  * @param con_cls pointer to location where we store the 'struct Request'
1106  * @return MHD_YES if the connection was handled successfully,
1107  *         MHD_NO if the socket must be closed due to a serious
1108  *         error while handling the request
1109  */
1110 static int
1111 create_response (void *cls,
1112                  struct MHD_Connection *con,
1113                  const char *url,
1114                  const char *meth,
1115                  const char *ver,
1116                  const char *upload_data,
1117                  size_t *upload_data_size,
1118                  void **con_cls)
1119 {
1120   static int dummy;
1121   struct MhdHttpList* hd = cls;
1122   const char* page = "<html><head><title>gnoxy</title>"\
1123                       "</head><body>cURL fail</body></html>";
1124   struct MHD_Response *response;
1125   char host[265];
1126   char curlurl[512];
1127   int ret = MHD_YES;
1128
1129   struct ProxyCurlTask *ctask;
1130   
1131   if (0 != strcmp (meth, "GET"))
1132     return MHD_NO;
1133   if (&dummy != *con_cls)
1134   {
1135     *con_cls = &dummy;
1136     return MHD_YES;
1137   }
1138
1139   if (0 != *upload_data_size)
1140     return MHD_NO;
1141
1142   *con_cls = NULL;
1143
1144   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1145               "url %s\n", url);
1146
1147   MHD_get_connection_values (con,
1148                              MHD_HEADER_KIND,
1149                              &con_val_iter, host);
1150
1151   
1152   /* Do cURL */
1153   ctask = GNUNET_malloc (sizeof (struct ProxyCurlTask));
1154   ctask->mhd = hd;
1155   ctask->curl = curl_easy_init();
1156
1157   if (curl_multi == NULL)
1158     curl_multi = curl_multi_init ();
1159   
1160   if ((ctask->curl == NULL) || (curl_multi == NULL))
1161   {
1162     response = MHD_create_response_from_buffer (strlen (page),
1163                                               (void*)page,
1164                                               MHD_RESPMEM_PERSISTENT);
1165     ret = MHD_queue_response (con,
1166                               MHD_HTTP_OK,
1167                               response);
1168     MHD_destroy_response (response);
1169     GNUNET_free (ctask);
1170     return ret;
1171   }
1172
1173   ctask->download_in_progress = GNUNET_YES;
1174   ctask->download_successful = GNUNET_NO;
1175   ctask->buf_status = BUF_WAIT_FOR_CURL;
1176   ctask->bytes_in_buffer = 0;
1177   ctask->parse_content = GNUNET_NO;
1178
1179   curl_easy_setopt (ctask->curl, CURLOPT_HEADERFUNCTION, &curl_check_hdr);
1180   curl_easy_setopt (ctask->curl, CURLOPT_HEADERDATA, ctask);
1181   curl_easy_setopt (ctask->curl, CURLOPT_WRITEFUNCTION, &callback_download);
1182   curl_easy_setopt (ctask->curl, CURLOPT_WRITEDATA, ctask);
1183   curl_easy_setopt (ctask->curl, CURLOPT_FOLLOWLOCATION, 1);
1184   curl_easy_setopt (ctask->curl, CURLOPT_MAXREDIRS, 4);
1185   /* no need to abort if the above failed */
1186   if (GNUNET_NO == ctask->mhd->is_ssl)
1187     sprintf (curlurl, "http://%s%s", host, url);
1188   strcpy (ctask->host, host);
1189   strcpy (ctask->url, url);
1190   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1191               "Adding new curl task for %s\n", curlurl);
1192   
1193   curl_easy_setopt (ctask->curl, CURLOPT_URL, curlurl);
1194   curl_easy_setopt (ctask->curl, CURLOPT_FAILONERROR, 1);
1195   curl_easy_setopt (ctask->curl, CURLOPT_CONNECTTIMEOUT, 600L);
1196   curl_easy_setopt (ctask->curl, CURLOPT_TIMEOUT, 600L);
1197
1198   GNUNET_GNS_get_authority (gns_handle,
1199                             ctask->host,
1200                             &process_get_authority,
1201                             ctask);
1202   //download_prepare (ctask);
1203   //curl_download_prepare ();
1204
1205   response = MHD_create_response_from_callback (-1, -1,
1206                                                 &mhd_content_cb,
1207                                                 ctask,
1208                                                 &mhd_content_free);
1209   
1210   ret = MHD_queue_response (con, MHD_HTTP_OK, response);
1211   
1212   //MHD_destroy_response (response);
1213
1214   return ret;
1215 }
1216
1217 /**
1218  * Task run whenever HTTP server operations are pending.
1219  *
1220  * @param cls unused
1221  * @param tc sched context
1222  */
1223 static void
1224 do_httpd (void *cls,
1225           const struct GNUNET_SCHEDULER_TaskContext *tc);
1226
1227
1228 /**
1229  * run all httpd
1230  */
1231 static void
1232 run_httpds ()
1233 {
1234   struct MhdHttpList *hd;
1235
1236   for (hd=mhd_httpd_head; hd != NULL; hd = hd->next)
1237     run_httpd (hd);
1238
1239 }
1240
1241 /**
1242  * schedule mhd
1243  */
1244 static void
1245 run_httpd (struct MhdHttpList *hd)
1246 {
1247   fd_set rs;
1248   fd_set ws;
1249   fd_set es;
1250   struct GNUNET_NETWORK_FDSet *wrs;
1251   struct GNUNET_NETWORK_FDSet *wws;
1252   struct GNUNET_NETWORK_FDSet *wes;
1253   int max;
1254   int haveto;
1255   unsigned MHD_LONG_LONG timeout;
1256   struct GNUNET_TIME_Relative tv;
1257
1258   FD_ZERO (&rs);
1259   FD_ZERO (&ws);
1260   FD_ZERO (&es);
1261   wrs = GNUNET_NETWORK_fdset_create ();
1262   wes = GNUNET_NETWORK_fdset_create ();
1263   wws = GNUNET_NETWORK_fdset_create ();
1264   max = -1;
1265   GNUNET_assert (MHD_YES == MHD_get_fdset (hd->daemon, &rs, &ws, &es, &max));
1266   
1267   
1268   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1269               "MHD fds: max=%d\n", max);
1270   
1271   haveto = MHD_get_timeout (hd->daemon, &timeout);
1272
1273   if (haveto == MHD_YES)
1274     tv.rel_value = (uint64_t) timeout;
1275   else
1276     tv = GNUNET_TIME_UNIT_FOREVER_REL;
1277   GNUNET_NETWORK_fdset_copy_native (wrs, &rs, max + 1);
1278   GNUNET_NETWORK_fdset_copy_native (wws, &ws, max + 1);
1279   GNUNET_NETWORK_fdset_copy_native (wes, &es, max + 1);
1280   
1281   if (hd->httpd_task != GNUNET_SCHEDULER_NO_TASK)
1282     GNUNET_SCHEDULER_cancel (hd->httpd_task);
1283   hd->httpd_task =
1284     GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_HIGH,
1285                                  tv, wrs, wws,
1286                                  &do_httpd, hd);
1287   GNUNET_NETWORK_fdset_destroy (wrs);
1288   GNUNET_NETWORK_fdset_destroy (wws);
1289   GNUNET_NETWORK_fdset_destroy (wes);
1290 }
1291
1292
1293 /**
1294  * Task run whenever HTTP server operations are pending.
1295  *
1296  * @param cls unused
1297  * @param tc sched context
1298  */
1299 static void
1300 do_httpd (void *cls,
1301           const struct GNUNET_SCHEDULER_TaskContext *tc)
1302 {
1303   struct MhdHttpList *hd = cls;
1304   
1305   hd->httpd_task = GNUNET_SCHEDULER_NO_TASK;
1306   
1307   MHD_run (hd->daemon);
1308   run_httpd (hd);
1309 }
1310
1311
1312 /**
1313  * Read data from socket
1314  *
1315  * @param cls the closure
1316  * @param tc scheduler context
1317  */
1318 static void
1319 do_read (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
1320
1321 /**
1322  * Read from remote end
1323  *
1324  * @param cls closure
1325  * @param tc scheduler context
1326  */
1327 static void
1328 do_read_remote (void* cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
1329
1330 /**
1331  * Write data to remote socket
1332  *
1333  * @param cls the closure
1334  * @param tc scheduler context
1335  */
1336 static void
1337 do_write_remote (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1338 {
1339   struct Socks5Request *s5r = cls;
1340   unsigned int len;
1341
1342   s5r->fwdwtask = GNUNET_SCHEDULER_NO_TASK;
1343
1344   if ((NULL != tc->read_ready) &&
1345       (GNUNET_NETWORK_fdset_isset (tc->write_ready, s5r->remote_sock)) &&
1346       ((len = GNUNET_NETWORK_socket_send (s5r->remote_sock, s5r->rbuf,
1347                                          s5r->rbuf_len)>0)))
1348   {
1349     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1350                 "Successfully sent %d bytes to remote socket\n",
1351                 len);
1352   }
1353   else
1354   {
1355     GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "write remote");
1356     //Really!?!?!?
1357     if (s5r->rtask != GNUNET_SCHEDULER_NO_TASK)
1358       GNUNET_SCHEDULER_cancel (s5r->rtask);
1359     if (s5r->wtask != GNUNET_SCHEDULER_NO_TASK)
1360       GNUNET_SCHEDULER_cancel (s5r->wtask);
1361     if (s5r->fwdrtask != GNUNET_SCHEDULER_NO_TASK)
1362       GNUNET_SCHEDULER_cancel (s5r->fwdrtask);
1363     GNUNET_NETWORK_socket_close (s5r->remote_sock);
1364     GNUNET_NETWORK_socket_close (s5r->sock);
1365     GNUNET_free(s5r);
1366     return;
1367   }
1368
1369   s5r->rtask =
1370     GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
1371                                    s5r->sock,
1372                                    &do_read, s5r);
1373 }
1374
1375
1376 /**
1377  * Clean up s5r handles
1378  *
1379  * @param s5r the handle to destroy
1380  */
1381 static void
1382 cleanup_s5r (struct Socks5Request *s5r)
1383 {
1384   if (s5r->rtask != GNUNET_SCHEDULER_NO_TASK)
1385     GNUNET_SCHEDULER_cancel (s5r->rtask);
1386   if (s5r->fwdwtask != GNUNET_SCHEDULER_NO_TASK)
1387     GNUNET_SCHEDULER_cancel (s5r->fwdwtask);
1388   if (s5r->fwdrtask != GNUNET_SCHEDULER_NO_TASK)
1389     GNUNET_SCHEDULER_cancel (s5r->fwdrtask);
1390   
1391   if (NULL != s5r->remote_sock)
1392     GNUNET_NETWORK_socket_close (s5r->remote_sock);
1393   if ((NULL != s5r->sock) && (s5r->cleanup_sock == GNUNET_YES))
1394     GNUNET_NETWORK_socket_close (s5r->sock);
1395   
1396   GNUNET_free(s5r);
1397 }
1398
1399 /**
1400  * Write data to socket
1401  *
1402  * @param cls the closure
1403  * @param tc scheduler context
1404  */
1405 static void
1406 do_write (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1407 {
1408   struct Socks5Request *s5r = cls;
1409   unsigned int len;
1410
1411   s5r->wtask = GNUNET_SCHEDULER_NO_TASK;
1412
1413   if ((NULL != tc->read_ready) &&
1414       (GNUNET_NETWORK_fdset_isset (tc->write_ready, s5r->sock)) &&
1415       ((len = GNUNET_NETWORK_socket_send (s5r->sock, s5r->wbuf,
1416                                          s5r->wbuf_len)>0)))
1417   {
1418     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1419                 "Successfully sent %d bytes to socket\n",
1420                 len);
1421   }
1422   else
1423   {
1424     
1425     GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "write");
1426     s5r->cleanup = GNUNET_YES;
1427     s5r->cleanup_sock = GNUNET_YES;
1428     cleanup_s5r (s5r);
1429     
1430     return;
1431   }
1432
1433   if (GNUNET_YES == s5r->cleanup)
1434   {
1435     cleanup_s5r (s5r);
1436     return;
1437   }
1438
1439   if ((s5r->state == SOCKS5_DATA_TRANSFER) &&
1440       (s5r->fwdrtask == GNUNET_SCHEDULER_NO_TASK))
1441     s5r->fwdrtask =
1442       GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
1443                                      s5r->remote_sock,
1444                                      &do_read_remote, s5r);
1445 }
1446
1447 /**
1448  * Read from remote end
1449  *
1450  * @param cls closure
1451  * @param tc scheduler context
1452  */
1453 static void
1454 do_read_remote (void* cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1455 {
1456   struct Socks5Request *s5r = cls;
1457   
1458   s5r->fwdrtask = GNUNET_SCHEDULER_NO_TASK;
1459
1460
1461   if ((NULL != tc->write_ready) &&
1462       (GNUNET_NETWORK_fdset_isset (tc->read_ready, s5r->remote_sock)) &&
1463       (s5r->wbuf_len = GNUNET_NETWORK_socket_recv (s5r->remote_sock, s5r->wbuf,
1464                                          sizeof (s5r->wbuf))))
1465   {
1466     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1467                 "Successfully read %d bytes from remote socket\n",
1468                 s5r->wbuf_len);
1469   }
1470   else
1471   {
1472     if (s5r->wbuf_len == 0)
1473       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1474                   "0 bytes received from remote... graceful shutdown!\n");
1475     if (s5r->fwdwtask != GNUNET_SCHEDULER_NO_TASK)
1476       GNUNET_SCHEDULER_cancel (s5r->fwdwtask);
1477     if (s5r->rtask != GNUNET_SCHEDULER_NO_TASK)
1478       GNUNET_SCHEDULER_cancel (s5r->rtask);
1479     
1480     GNUNET_NETWORK_socket_close (s5r->remote_sock);
1481     s5r->remote_sock = NULL;
1482     GNUNET_NETWORK_socket_close (s5r->sock);
1483     GNUNET_free(s5r);
1484
1485     return;
1486   }
1487   
1488   s5r->wtask = GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
1489                                                s5r->sock,
1490                                                &do_write, s5r);
1491   
1492 }
1493
1494
1495 /**
1496  * Adds a socket to MHD
1497  *
1498  * @param h the handle to the socket to add
1499  * @param daemon the daemon to add the fd to
1500  * @return whatever MHD_add_connection returns
1501  */
1502 static int
1503 add_handle_to_mhd (struct GNUNET_NETWORK_Handle *h, struct MHD_Daemon *daemon)
1504 {
1505   int fd;
1506   struct sockaddr *addr;
1507   socklen_t len;
1508
1509   fd = GNUNET_NETWORK_get_fd (h);
1510   addr = GNUNET_NETWORK_get_addr (h);
1511   len = GNUNET_NETWORK_get_addrlen (h);
1512
1513   return MHD_add_connection (daemon, fd, addr, len);
1514 }
1515
1516
1517 static long
1518 get_file_size (const char* filename)
1519 {
1520   FILE *fp;
1521
1522   fp = fopen (filename, "rb");
1523   if (fp)
1524   {
1525     long size;
1526
1527     if ((0 != fseek (fp, 0, SEEK_END)) || (-1 == (size = ftell (fp))))
1528       size = 0;
1529
1530     fclose (fp);
1531
1532     return size;
1533   }
1534   
1535   return 0;
1536 }
1537
1538 /**
1539  * Read file in filename
1540  *
1541  * @param filename file to read
1542  * @return data
1543  */
1544 static char*
1545 load_file (const char* filename)
1546 {
1547   FILE *fp;
1548   char *buffer;
1549   long size;
1550
1551   size = get_file_size (filename);
1552   if (size == 0)
1553     return NULL;
1554
1555   fp = fopen (filename, "rb");
1556   if (!fp)
1557     return NULL;
1558
1559   buffer = GNUNET_malloc (size);
1560   if (!buffer)
1561   {
1562     fclose (fp);
1563     return NULL;
1564   }
1565
1566   if (size != fread (buffer, 1, size, fp))
1567   {
1568     GNUNET_free (buffer);
1569     buffer = NULL;
1570   }
1571
1572   fclose (fp);
1573   return buffer;
1574 }
1575
1576 /** SSL stuff **/
1577
1578 /**
1579  * Load PEM key from file
1580  *
1581  * @param key where to store the data
1582  * @param keyfile path to the PEM file
1583  */
1584 static void
1585 load_key_from_file (gnutls_x509_privkey_t key, char* keyfile)
1586 {
1587   gnutls_datum_t key_data;
1588   int ret;
1589
1590   key_data.data = (unsigned char*)load_file (keyfile);
1591   key_data.size = strlen ((char*)key_data.data);
1592
1593   ret = gnutls_x509_privkey_import (key, &key_data,
1594                                     GNUTLS_X509_FMT_PEM);
1595   
1596   if (GNUTLS_E_SUCCESS != ret)
1597   {
1598     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1599                 "Unable to import private key %s(ret=%d)\n", key_data.data, ret);
1600     GNUNET_break (0);
1601   }
1602
1603   GNUNET_free (key_data.data);
1604 }
1605
1606 /**
1607  * Load cert from file
1608  *
1609  * @param crt struct to store data in
1610  * @param certfile path to pem file
1611  */
1612 static void
1613 load_cert_from_file (gnutls_x509_crt_t crt, char* certfile)
1614 {
1615   gnutls_datum_t cert_data;
1616   int ret;
1617
1618   cert_data.data = (unsigned char*)load_file (certfile);
1619   cert_data.size = strlen ((char*)cert_data.data);
1620
1621   ret = gnutls_x509_crt_import (crt, &cert_data,
1622                                  GNUTLS_X509_FMT_PEM);
1623   if (GNUTLS_E_SUCCESS != ret)
1624   {
1625     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1626                 "Unable to import certificate %s(ret=%d)\n", certfile, ret);
1627     GNUNET_break (0);
1628   }
1629
1630   GNUNET_free (cert_data.data);
1631
1632 }
1633
1634
1635 /**
1636  * Generate new certificate for specific name
1637  *
1638  * @param name the subject name to generate a cert for
1639  * @return a struct holding the PEM data
1640  */
1641 static struct ProxyGNSCertificate *
1642 generate_gns_certificate (const char *name)
1643 {
1644
1645   int ret;
1646   unsigned int serial;
1647   size_t key_buf_size;
1648   size_t cert_buf_size;
1649   gnutls_x509_crt_t request;
1650   time_t etime;
1651   struct tm *tm_data;
1652
1653   ret = gnutls_x509_crt_init (&request);
1654
1655   if (GNUTLS_E_SUCCESS != ret)
1656   {
1657     GNUNET_break (0);
1658   }
1659
1660   ret = gnutls_x509_crt_set_key (request, proxy_ca.key);
1661
1662   if (GNUTLS_E_SUCCESS != ret)
1663   {
1664     GNUNET_break (0);
1665   }
1666
1667   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Generating cert\n");
1668
1669   struct ProxyGNSCertificate *pgc =
1670     GNUNET_malloc (sizeof (struct ProxyGNSCertificate));
1671
1672   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Adding DNs\n");
1673   
1674   gnutls_x509_crt_set_dn_by_oid (request, GNUTLS_OID_X520_COUNTRY_NAME,
1675                                  0, "DE", 2);
1676
1677   gnutls_x509_crt_set_dn_by_oid (request, GNUTLS_OID_X520_ORGANIZATION_NAME,
1678                                  0, "GNUnet", 6);
1679
1680   gnutls_x509_crt_set_dn_by_oid (request, GNUTLS_OID_X520_COMMON_NAME,
1681                                  0, name, strlen (name));
1682
1683   ret = gnutls_x509_crt_set_version (request, 3);
1684
1685   ret = gnutls_rnd (GNUTLS_RND_NONCE, &serial, sizeof (serial));
1686
1687   etime = time (NULL);
1688   tm_data = localtime (&etime);
1689   
1690
1691   ret = gnutls_x509_crt_set_serial (request,
1692                                     &serial,
1693                                     sizeof (serial));
1694
1695   ret = gnutls_x509_crt_set_activation_time (request,
1696                                              etime);
1697   tm_data->tm_year++;
1698   etime = mktime (tm_data);
1699
1700   if (-1 == etime)
1701   {
1702     GNUNET_break (0);
1703   }
1704
1705   ret = gnutls_x509_crt_set_expiration_time (request,
1706                                              etime);
1707   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Signing...\n");
1708
1709   ret = gnutls_x509_crt_sign (request, proxy_ca.cert, proxy_ca.key);
1710
1711   key_buf_size = sizeof (pgc->key);
1712   cert_buf_size = sizeof (pgc->cert);
1713
1714   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Exporting certificate...\n");
1715   
1716   gnutls_x509_crt_export (request, GNUTLS_X509_FMT_PEM,
1717                           pgc->cert, &cert_buf_size);
1718
1719   gnutls_x509_privkey_export (proxy_ca.key, GNUTLS_X509_FMT_PEM,
1720                           pgc->key, &key_buf_size);
1721
1722
1723   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Cleaning up\n");
1724   gnutls_x509_crt_deinit (request);
1725
1726   return pgc;
1727
1728 }
1729
1730
1731 /*
1732  * Accept policy for mhdaemons
1733  *
1734  * @param cls NULL
1735  * @param addr the sockaddr
1736  * @param addrlen the sockaddr length
1737  * @return MHD_NO if sockaddr is wrong or #conns too high
1738  */
1739 static int
1740 accept_cb (void* cls, const struct sockaddr *addr, socklen_t addrlen)
1741 {
1742   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1743               "In MHD accept policy cb\n");
1744
1745   if (addr != NULL)
1746   {
1747     if (addr->sa_family == AF_UNIX)
1748       return MHD_NO;
1749   }
1750
1751   if (total_mhd_connections >= MAX_MHD_CONNECTIONS)
1752     return MHD_NO;
1753
1754   total_mhd_connections++;
1755
1756   return MHD_YES;
1757 }
1758
1759
1760 /**
1761  * Adds a socket to an SSL MHD instance
1762  * It is important the the domain name is
1763  * correct. In most cases we need to start a new daemon
1764  *
1765  * @param h the handle to add to a daemon
1766  * @praram domain the domain the ssl daemon has to serve
1767  * @return MHD_YES on success
1768  */
1769 static int
1770 add_handle_to_ssl_mhd (struct GNUNET_NETWORK_Handle *h, char* domain)
1771 {
1772   struct MhdHttpList *hd = NULL;
1773   struct ProxyGNSCertificate *pgc;
1774
1775   for (hd = mhd_httpd_head; hd != NULL; hd = hd->next)
1776   {
1777     if (0 == strcmp (hd->domain, domain))
1778       break;
1779   }
1780
1781   if (NULL == hd)
1782   {
1783     
1784     pgc = generate_gns_certificate (domain);
1785     
1786     hd = GNUNET_malloc (sizeof (struct MhdHttpList));
1787     hd->is_ssl = GNUNET_YES;
1788     strcpy (hd->domain, domain);
1789     hd->proxy_cert = pgc;
1790
1791     /* Start new MHD */
1792     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1793                 "No previous SSL instance found... starting new one for %s\n",
1794                 domain);
1795
1796     hd->daemon = MHD_start_daemon (MHD_USE_DEBUG | MHD_USE_SSL, 4444,
1797             &accept_cb, NULL,
1798             &create_response, hd,
1799             MHD_OPTION_LISTEN_SOCKET, GNUNET_NETWORK_get_fd (mhd_unix_socket),
1800             MHD_OPTION_CONNECTION_LIMIT, (unsigned int) 128,
1801             MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) 16,
1802             MHD_OPTION_NOTIFY_COMPLETED,
1803             NULL, NULL,
1804             MHD_OPTION_HTTPS_MEM_KEY, pgc->key,
1805             MHD_OPTION_HTTPS_MEM_CERT, pgc->cert,
1806             MHD_OPTION_END);
1807
1808     GNUNET_assert (hd->daemon != NULL);
1809     hd->httpd_task = GNUNET_SCHEDULER_NO_TASK;
1810     
1811     GNUNET_CONTAINER_DLL_insert (mhd_httpd_head, mhd_httpd_tail, hd);
1812   }
1813   
1814   return add_handle_to_mhd (h, hd->daemon);
1815 }
1816
1817
1818
1819 /**
1820  * Read data from incoming connection
1821  *
1822  * @param cls the closure
1823  * @param tc the scheduler context
1824  */
1825 static void
1826 do_read (void* cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1827 {
1828   struct Socks5Request *s5r = cls;
1829   struct socks5_client_hello *c_hello;
1830   struct socks5_server_hello *s_hello;
1831   struct socks5_client_request *c_req;
1832   struct socks5_server_response *s_resp;
1833
1834   int ret;
1835   char domain[256];
1836   uint8_t dom_len;
1837   uint16_t req_port;
1838   struct hostent *phost;
1839   uint32_t remote_ip;
1840   struct sockaddr_in remote_addr;
1841   struct in_addr *r_sin_addr;
1842
1843   s5r->rtask = GNUNET_SCHEDULER_NO_TASK;
1844
1845   if ((NULL != tc->write_ready) &&
1846       (GNUNET_NETWORK_fdset_isset (tc->read_ready, s5r->sock)) &&
1847       (s5r->rbuf_len = GNUNET_NETWORK_socket_recv (s5r->sock, s5r->rbuf,
1848                                          sizeof (s5r->rbuf))))
1849   {
1850     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1851                 "Successfully read %d bytes from socket\n",
1852                 s5r->rbuf_len);
1853   }
1854   else
1855   {
1856     if (s5r->rbuf_len != 0)
1857       GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "read");
1858     else
1859       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client disco!\n");
1860
1861     if (s5r->fwdrtask != GNUNET_SCHEDULER_NO_TASK)
1862       GNUNET_SCHEDULER_cancel (s5r->fwdrtask);
1863     if (s5r->wtask != GNUNET_SCHEDULER_NO_TASK)
1864       GNUNET_SCHEDULER_cancel (s5r->wtask);
1865     if (s5r->fwdwtask != GNUNET_SCHEDULER_NO_TASK)
1866       GNUNET_SCHEDULER_cancel (s5r->fwdwtask);
1867     GNUNET_NETWORK_socket_close (s5r->remote_sock);
1868     GNUNET_NETWORK_socket_close (s5r->sock);
1869     GNUNET_free(s5r);
1870     return;
1871   }
1872
1873   if (s5r->state == SOCKS5_INIT)
1874   {
1875     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1876                 "SOCKS5 init\n");
1877     c_hello = (struct socks5_client_hello*)&s5r->rbuf;
1878
1879     GNUNET_assert (c_hello->version == SOCKS_VERSION_5);
1880
1881     s_hello = (struct socks5_server_hello*)&s5r->wbuf;
1882     s5r->wbuf_len = sizeof( struct socks5_server_hello );
1883
1884     s_hello->version = c_hello->version;
1885     s_hello->auth_method = SOCKS_AUTH_NONE;
1886
1887     /* Write response to client */
1888     s5r->wtask = GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
1889                                                 s5r->sock,
1890                                                 &do_write, s5r);
1891
1892     s5r->rtask = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
1893                                                 s5r->sock,
1894                                                 &do_read, s5r);
1895
1896     s5r->state = SOCKS5_REQUEST;
1897     return;
1898   }
1899
1900   if (s5r->state == SOCKS5_REQUEST)
1901   {
1902     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1903                 "Processing SOCKS5 request\n");
1904     c_req = (struct socks5_client_request*)&s5r->rbuf;
1905     s_resp = (struct socks5_server_response*)&s5r->wbuf;
1906     //Only 10byte for ipv4 response!
1907     s5r->wbuf_len = 10;//sizeof (struct socks5_server_response);
1908
1909     GNUNET_assert (c_req->addr_type == 3);
1910
1911     dom_len = *((uint8_t*)(&(c_req->addr_type) + 1));
1912     memset(domain, 0, sizeof(domain));
1913     strncpy(domain, (char*)(&(c_req->addr_type) + 2), dom_len);
1914     req_port = *((uint16_t*)(&(c_req->addr_type) + 2 + dom_len));
1915
1916     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1917                 "Requested connection is %s:%d\n",
1918                 domain,
1919                 ntohs(req_port));
1920
1921     if (is_tld (domain, GNUNET_GNS_TLD) ||
1922         is_tld (domain, GNUNET_GNS_TLD_ZKEY))
1923     {
1924       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1925                   "Requested connection is gnunet tld\n",
1926                   domain);
1927       
1928       ret = MHD_NO;
1929       if (ntohs(req_port) == HTTPS_PORT)
1930       {
1931         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1932                     "Requested connection is HTTPS\n");
1933         ret = add_handle_to_ssl_mhd ( s5r->sock, domain );
1934       }
1935       else if (NULL != httpd)
1936       {
1937         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1938                     "Requested connection is HTTP\n");
1939         ret = add_handle_to_mhd ( s5r->sock, httpd );
1940       }
1941
1942       if (ret != MHD_YES)
1943       {
1944         GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1945                     _("Failed to start HTTP server\n"));
1946         s_resp->version = 0x05;
1947         s_resp->reply = 0x01;
1948         s5r->cleanup = GNUNET_YES;
1949         s5r->cleanup_sock = GNUNET_YES;
1950         s5r->wtask = 
1951           GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
1952                                         s5r->sock,
1953                                         &do_write, s5r);
1954         return;
1955       }
1956       
1957       /* Signal success */
1958       s_resp->version = 0x05;
1959       s_resp->reply = 0x00;
1960       s_resp->reserved = 0x00;
1961       s_resp->addr_type = 0x01;
1962       
1963       s5r->cleanup = GNUNET_YES;
1964       s5r->cleanup_sock = GNUNET_NO;
1965       s5r->wtask =
1966         GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
1967                                         s5r->sock,
1968                                         &do_write, s5r);
1969       run_httpds ();
1970       return;
1971     }
1972     else
1973     {
1974       phost = (struct hostent*)gethostbyname (domain);
1975       if (phost == NULL)
1976       {
1977         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1978                     "Resolve %s error!\n", domain );
1979         s_resp->version = 0x05;
1980         s_resp->reply = 0x01;
1981         s5r->cleanup = GNUNET_YES;
1982         s5r->cleanup_sock = GNUNET_YES;
1983         s5r->wtask = 
1984           GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
1985                                           s5r->sock,
1986                                           &do_write, s5r);
1987         return;
1988       }
1989
1990       s5r->remote_sock = GNUNET_NETWORK_socket_create (AF_INET,
1991                                                        SOCK_STREAM,
1992                                                        0);
1993       r_sin_addr = (struct in_addr*)(phost->h_addr);
1994       remote_ip = r_sin_addr->s_addr;
1995       memset(&remote_addr, 0, sizeof(remote_addr));
1996       remote_addr.sin_family = AF_INET;
1997 #if HAVE_SOCKADDR_IN_SIN_LEN
1998       remote_addr.sin_len = sizeof (remote_addr);
1999 #endif
2000       remote_addr.sin_addr.s_addr = remote_ip;
2001       remote_addr.sin_port = req_port;
2002       
2003       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2004                   "target server: %s:%u\n", inet_ntoa(remote_addr.sin_addr),
2005                   ntohs(req_port));
2006
2007       if ((GNUNET_OK !=
2008           GNUNET_NETWORK_socket_connect ( s5r->remote_sock,
2009                                           (const struct sockaddr*)&remote_addr,
2010                                           sizeof (remote_addr)))
2011           && (errno != EINPROGRESS))
2012       {
2013         GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "connect");
2014         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2015                     "socket request error...\n");
2016         s_resp->version = 0x05;
2017         s_resp->reply = 0x01;
2018         s5r->wtask =
2019           GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
2020                                           s5r->sock,
2021                                           &do_write, s5r);
2022         //TODO see above
2023         return;
2024       }
2025
2026       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2027                   "new remote connection\n");
2028
2029       s_resp->version = 0x05;
2030       s_resp->reply = 0x00;
2031       s_resp->reserved = 0x00;
2032       s_resp->addr_type = 0x01;
2033
2034       s5r->state = SOCKS5_DATA_TRANSFER;
2035
2036       s5r->wtask =
2037         GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
2038                                         s5r->sock,
2039                                         &do_write, s5r);
2040       s5r->rtask =
2041         GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
2042                                        s5r->sock,
2043                                        &do_read, s5r);
2044
2045     }
2046     return;
2047   }
2048
2049   if (s5r->state == SOCKS5_DATA_TRANSFER)
2050   {
2051     if ((s5r->remote_sock == NULL) || (s5r->rbuf_len == 0))
2052     {
2053       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2054                   "Closing connection to client\n");
2055       if (s5r->rtask != GNUNET_SCHEDULER_NO_TASK)
2056         GNUNET_SCHEDULER_cancel (s5r->rtask);
2057       if (s5r->fwdwtask != GNUNET_SCHEDULER_NO_TASK)
2058         GNUNET_SCHEDULER_cancel (s5r->fwdwtask);
2059       if (s5r->fwdrtask != GNUNET_SCHEDULER_NO_TASK)
2060         GNUNET_SCHEDULER_cancel (s5r->fwdrtask);
2061       if (s5r->fwdrtask != GNUNET_SCHEDULER_NO_TASK)
2062         GNUNET_SCHEDULER_cancel (s5r->fwdrtask);
2063       
2064       if (s5r->remote_sock != NULL)
2065         GNUNET_NETWORK_socket_close (s5r->remote_sock);
2066       GNUNET_NETWORK_socket_close (s5r->sock);
2067       GNUNET_free(s5r);
2068       return;
2069     }
2070
2071     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2072                 "forwarding %d bytes from client\n", s5r->rbuf_len);
2073
2074     s5r->fwdwtask =
2075       GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
2076                                       s5r->remote_sock,
2077                                       &do_write_remote, s5r);
2078
2079     if (s5r->fwdrtask == GNUNET_SCHEDULER_NO_TASK)
2080     {
2081       s5r->fwdrtask =
2082         GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
2083                                        s5r->remote_sock,
2084                                        &do_read_remote, s5r);
2085     }
2086
2087
2088   }
2089
2090   //GNUNET_CONTAINER_DLL_remove (s5conns.head, s5conns.tail, s5r);
2091
2092 }
2093
2094
2095 /**
2096  * Accept new incoming connections
2097  *
2098  * @param cls the closure
2099  * @param tc the scheduler context
2100  */
2101 static void
2102 do_accept (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2103 {
2104   struct GNUNET_NETWORK_Handle *s;
2105   struct Socks5Request *s5r;
2106
2107   ltask = GNUNET_SCHEDULER_NO_TASK;
2108   if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
2109     return;
2110
2111   ltask = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
2112                                          lsock,
2113                                          &do_accept, NULL);
2114
2115   s = GNUNET_NETWORK_socket_accept (lsock, NULL, NULL);
2116
2117   if (NULL == s)
2118   {
2119     GNUNET_log_strerror (GNUNET_ERROR_TYPE_INFO, "accept");
2120     return;
2121   }
2122
2123   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2124               "Got an inbound connection, waiting for data\n");
2125
2126   s5r = GNUNET_malloc (sizeof (struct Socks5Request));
2127   s5r->sock = s;
2128   s5r->state = SOCKS5_INIT;
2129   s5r->wtask = GNUNET_SCHEDULER_NO_TASK;
2130   s5r->fwdwtask = GNUNET_SCHEDULER_NO_TASK;
2131   s5r->fwdrtask = GNUNET_SCHEDULER_NO_TASK;
2132   s5r->rtask = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
2133                                               s5r->sock,
2134                                               &do_read, s5r);
2135   //GNUNET_CONTAINER_DLL_insert (s5conns.head, s5conns.tail, s5r);
2136 }
2137
2138
2139 /**
2140  * Task run on shutdown
2141  *
2142  * @param cls closure
2143  * @param tc task context
2144  */
2145 static void
2146 do_shutdown (void *cls,
2147              const struct GNUNET_SCHEDULER_TaskContext *tc)
2148 {
2149
2150   struct MhdHttpList *hd;
2151   struct MhdHttpList *tmp_hd;
2152   struct ProxyCurlTask *ctask;
2153   struct ProxyCurlTask *ctask_tmp;
2154
2155   if (GNUNET_SCHEDULER_NO_TASK != curl_download_task)
2156   {
2157     GNUNET_SCHEDULER_cancel (curl_download_task);
2158     curl_download_task = GNUNET_SCHEDULER_NO_TASK;
2159   }
2160
2161   for (hd = mhd_httpd_head; hd != NULL; hd = tmp_hd)
2162   {
2163     tmp_hd = hd->next;
2164
2165     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2166                 "Stopping daemon\n");
2167
2168     if (GNUNET_SCHEDULER_NO_TASK != hd->httpd_task)
2169     {
2170       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2171                   "Stopping select task %d\n",
2172                   hd->httpd_task);
2173       GNUNET_SCHEDULER_cancel (hd->httpd_task);
2174       hd->httpd_task = GNUNET_SCHEDULER_NO_TASK;
2175     }
2176
2177     if (NULL != hd->daemon)
2178     {
2179       MHD_stop_daemon (hd->daemon);
2180       hd->daemon = NULL;
2181     }
2182
2183     if (NULL != hd->proxy_cert)
2184     {
2185       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2186                   "Free certificate\n");
2187       GNUNET_free (hd->proxy_cert);
2188     }
2189
2190     GNUNET_free (hd);
2191   }
2192
2193   for (ctask=ctasks_head; ctask != NULL; ctask=ctask_tmp)
2194   {
2195     ctask_tmp = ctask->next;
2196
2197     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2198                 "Cleaning up cURL task\n");
2199
2200     if (ctask->curl != NULL)
2201       curl_easy_cleanup (ctask->curl);
2202     ctask->curl = NULL;
2203     if (NULL != ctask->headers)
2204       curl_slist_free_all (ctask->headers);
2205
2206     GNUNET_free (ctask);
2207   }
2208
2209   GNUNET_GNS_disconnect (gns_handle);
2210 }
2211
2212
2213 /**
2214  * Compiles a regex for us
2215  *
2216  * @param re ptr to re struct
2217  * @param rt the expression to compile
2218  * @return 0 on success
2219  */
2220 static int
2221 compile_regex (regex_t *re, const char* rt)
2222 {
2223   int status;
2224   char err[1024];
2225
2226   status = regcomp (re, rt, REG_EXTENDED|REG_NEWLINE);
2227   if (status)
2228   {
2229     regerror (status, re, err, 1024);
2230     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2231                 "Regex error compiling '%s': %s\n", rt, err);
2232     return 1;
2233   }
2234   return 0;
2235 }
2236
2237
2238 /**
2239  * Loads the users local zone key
2240  *
2241  * @return GNUNET_YES on success
2242  */
2243 static int
2244 load_local_zone_key (const struct GNUNET_CONFIGURATION_Handle *cfg)
2245 {
2246   char *keyfile;
2247   struct GNUNET_CRYPTO_RsaPrivateKey *key = NULL;
2248   struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pkey;
2249   struct GNUNET_CRYPTO_ShortHashCode *zone = NULL;
2250   struct GNUNET_CRYPTO_ShortHashAsciiEncoded zonename;
2251
2252   if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "gns",
2253                                                             "ZONEKEY", &keyfile))
2254   {
2255     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2256                 "Unable to load zone key config value!\n");
2257     return GNUNET_NO;
2258   }
2259
2260   if (GNUNET_NO == GNUNET_DISK_file_test (keyfile))
2261   {
2262     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2263                 "Unable to load zone key %s!\n", keyfile);
2264     GNUNET_free(keyfile);
2265     return GNUNET_NO;
2266   }
2267
2268   key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
2269   GNUNET_CRYPTO_rsa_key_get_public (key, &pkey);
2270   GNUNET_CRYPTO_short_hash(&pkey,
2271                            sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
2272                            &local_gns_zone);
2273   zone = &local_gns_zone;
2274   GNUNET_CRYPTO_short_hash_to_enc (zone, &zonename);
2275   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2276               "Using zone: %s!\n", &zonename);
2277   GNUNET_CRYPTO_rsa_key_free(key);
2278   GNUNET_free(keyfile);
2279
2280   return GNUNET_YES;
2281 }
2282
2283 /**
2284  * Loads the users local shorten zone key
2285  *
2286  * @return GNUNET_YES on success
2287  */
2288 static int
2289 load_local_shorten_key (const struct GNUNET_CONFIGURATION_Handle *cfg)
2290 {
2291   char *keyfile;
2292   struct GNUNET_CRYPTO_RsaPrivateKey *key = NULL;
2293   struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pkey;
2294   struct GNUNET_CRYPTO_ShortHashCode *zone = NULL;
2295   struct GNUNET_CRYPTO_ShortHashAsciiEncoded zonename;
2296
2297   if (GNUNET_NO == GNUNET_CONFIGURATION_get_value_yesno (cfg, "gns",
2298                                                 "AUTO_IMPORT_PKEY"))
2299   {
2300     return GNUNET_NO;
2301   }
2302
2303   if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "gns",
2304                                                           "AUTO_IMPORT_ZONEKEY",
2305                                                           &keyfile))
2306   {
2307     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2308                 "Unable to load shorten key config value! (not fatal)\n");
2309     return GNUNET_NO;
2310   }
2311
2312   if (GNUNET_NO == GNUNET_DISK_file_test (keyfile))
2313   {
2314     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2315                 "Unable to load shorten key %s! (not fatal)\n", keyfile);
2316     GNUNET_free(keyfile);
2317     return GNUNET_NO;
2318   }
2319
2320   key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
2321   GNUNET_CRYPTO_rsa_key_get_public (key, &pkey);
2322   GNUNET_CRYPTO_short_hash(&pkey,
2323                            sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
2324                            &local_shorten_zone);
2325   zone = &local_gns_zone;
2326   GNUNET_CRYPTO_short_hash_to_enc (zone, &zonename);
2327   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2328               "Using shorten zone: %s!\n", &zonename);
2329   GNUNET_CRYPTO_rsa_key_free(key);
2330   GNUNET_free(keyfile);
2331
2332   return GNUNET_YES;
2333 }
2334
2335 /**
2336  * Main function that will be run
2337  *
2338  * @param cls closure
2339  * @param args remaining command-line arguments
2340  * @param cfgfile name of the configuration file used (for saving, can be NULL!)
2341  * @param cfg configuration
2342  */
2343 static void
2344 run (void *cls, char *const *args, const char *cfgfile,
2345      const struct GNUNET_CONFIGURATION_Handle *cfg)
2346 {
2347   struct sockaddr_in sa;
2348   struct MhdHttpList *hd;
2349   struct sockaddr_un mhd_unix_sock_addr;
2350   size_t len;
2351   char* proxy_sockfile;
2352   char* cafile_cfg = NULL;
2353   char* cafile;
2354
2355   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2356               "Loading CA\n");
2357   
2358   cafile = cafile_opt;
2359
2360   if (NULL == cafile)
2361   {
2362     if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "gns-proxy",
2363                                                           "PROXY_CACERT",
2364                                                           &cafile_cfg))
2365     {
2366       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2367                   "Unable to load proxy CA config value!\n");
2368       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2369                   "No proxy CA provided!\n");
2370       return;
2371     }
2372     cafile = cafile_cfg;
2373   }
2374   
2375   gnutls_global_init ();
2376
2377   gnutls_x509_crt_init (&proxy_ca.cert);
2378   gnutls_x509_privkey_init (&proxy_ca.key);
2379   
2380   load_cert_from_file (proxy_ca.cert, cafile);
2381   load_key_from_file (proxy_ca.key, cafile);
2382
2383   if (cafile_cfg)
2384     GNUNET_free (cafile_cfg);
2385   
2386   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2387               "Loading Template\n");
2388
2389   compile_regex (&re_dotplus, (char*) RE_A_HREF);
2390
2391   gns_handle = GNUNET_GNS_connect (cfg);
2392
2393   if (GNUNET_NO == load_local_zone_key (cfg))
2394   {
2395     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2396                 "Unable to load zone!\n");
2397     return;
2398   }
2399
2400   use_shorten = load_local_shorten_key (cfg);
2401
2402   if (NULL == gns_handle)
2403   {
2404     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2405                 "Unable to connect to GNS!\n");
2406     return;
2407   }
2408
2409   memset (&sa, 0, sizeof (sa));
2410   sa.sin_family = AF_INET;
2411   sa.sin_port = htons (port);
2412 #if HAVE_SOCKADDR_IN_SIN_LEN
2413   sa.sin_len = sizeof (sa);
2414 #endif
2415
2416   lsock = GNUNET_NETWORK_socket_create (AF_INET,
2417                                         SOCK_STREAM,
2418                                         0);
2419
2420   if ((NULL == lsock) ||
2421       (GNUNET_OK !=
2422        GNUNET_NETWORK_socket_bind (lsock, (const struct sockaddr *) &sa,
2423                                    sizeof (sa))))
2424   {
2425     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2426                 "Failed to create listen socket bound to `%s'",
2427                 GNUNET_a2s ((const struct sockaddr *) &sa, sizeof (sa)));
2428     if (NULL != lsock)
2429       GNUNET_NETWORK_socket_close (lsock);
2430     return;
2431   }
2432
2433   if (GNUNET_OK != GNUNET_NETWORK_socket_listen (lsock, 5))
2434   {
2435     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2436                 "Failed to listen on socket bound to `%s'",
2437                 GNUNET_a2s ((const struct sockaddr *) &sa, sizeof (sa)));
2438     return;
2439   }
2440
2441   ltask = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
2442                                          lsock, &do_accept, NULL);
2443
2444   ctasks_head = NULL;
2445   ctasks_tail = NULL;
2446
2447   if (0 != curl_global_init (CURL_GLOBAL_WIN32))
2448   {
2449     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2450                 "cURL global init failed!\n");
2451     return;
2452   }
2453
2454   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2455               "Proxy listens on port %u\n",
2456               port);
2457
2458   mhd_httpd_head = NULL;
2459   mhd_httpd_tail = NULL;
2460   total_mhd_connections = 0;
2461
2462   if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "gns-proxy",
2463                                                             "PROXY_UNIXPATH",
2464                                                             &proxy_sockfile))
2465   {
2466     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2467                 "Specify PROXY_UNIXPATH in gns-proxy config section!\n");
2468     return;
2469   }
2470   
2471   mhd_unix_socket = GNUNET_NETWORK_socket_create (AF_UNIX,
2472                                                 SOCK_STREAM,
2473                                                 0);
2474
2475   if (NULL == mhd_unix_socket)
2476   {
2477     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2478                 "Unable to create unix domain socket!\n");
2479     return;
2480   }
2481
2482   mhd_unix_sock_addr.sun_family = AF_UNIX;
2483   strcpy (mhd_unix_sock_addr.sun_path, proxy_sockfile);
2484   unlink (proxy_sockfile);
2485   len = strlen (proxy_sockfile) + sizeof(AF_UNIX);
2486
2487   GNUNET_free (proxy_sockfile);
2488
2489   if (GNUNET_OK != GNUNET_NETWORK_socket_bind (mhd_unix_socket,
2490                                (struct sockaddr*)&mhd_unix_sock_addr,
2491                                len))
2492   {
2493     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2494                 "Unable to bind unix domain socket!\n");
2495     return;
2496   }
2497
2498   if (GNUNET_OK != GNUNET_NETWORK_socket_listen (mhd_unix_socket,
2499                                                  1))
2500   {
2501     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2502                 "Unable to listen on unix domain socket!\n");
2503     return;
2504   }
2505
2506   hd = GNUNET_malloc (sizeof (struct MhdHttpList));
2507   hd->is_ssl = GNUNET_NO;
2508   strcpy (hd->domain, "");
2509   httpd = MHD_start_daemon (MHD_USE_DEBUG, 4444, //Dummy port
2510             &accept_cb, NULL,
2511             &create_response, hd,
2512             MHD_OPTION_LISTEN_SOCKET, GNUNET_NETWORK_get_fd (mhd_unix_socket),
2513             MHD_OPTION_CONNECTION_LIMIT, (unsigned int) 128,
2514             MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) 16,
2515             MHD_OPTION_NOTIFY_COMPLETED,
2516             NULL, NULL,
2517             MHD_OPTION_END);
2518
2519   GNUNET_assert (httpd != NULL);
2520   hd->daemon = httpd;
2521   hd->httpd_task = GNUNET_SCHEDULER_NO_TASK;
2522
2523   GNUNET_CONTAINER_DLL_insert (mhd_httpd_head, mhd_httpd_tail, hd);
2524
2525   run_httpds ();
2526
2527   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
2528                                 &do_shutdown, NULL);
2529
2530 }
2531
2532
2533 /**
2534  * The main function for gnunet-gns-proxy.
2535  *
2536  * @param argc number of arguments from the command line
2537  * @param argv command line arguments
2538  * @return 0 ok, 1 on error
2539  */
2540 int
2541 main (int argc, char *const *argv)
2542 {
2543   static const struct GNUNET_GETOPT_CommandLineOption options[] = {
2544     {'p', "port", NULL,
2545      gettext_noop ("listen on specified port"), 1,
2546      &GNUNET_GETOPT_set_string, &port},
2547     {'a', "authority", NULL,
2548       gettext_noop ("pem file to use as CA"), 1,
2549       &GNUNET_GETOPT_set_string, &cafile_opt},
2550     GNUNET_GETOPT_OPTION_END
2551   };
2552
2553   int ret;
2554
2555   if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
2556     return 2;
2557
2558   GNUNET_log_setup ("gnunet-gns-proxy", "WARNING", NULL);
2559   ret =
2560       (GNUNET_OK ==
2561        GNUNET_PROGRAM_run (argc, argv, "gnunet-gns-proxy",
2562                            _("GNUnet GNS proxy"),
2563                            options,
2564                            &run, NULL)) ? 0 : 1;
2565   return ret;
2566 }