2 This file is part of GNUnet.
3 (C) 2012 Christian Grothoff (and other contributing authors)
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.
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.
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.
22 #include <gnunet_util_lib.h>
23 #include <gnunet_gns_service.h>
24 #include <microhttpd.h>
25 #include <curl/curl.h>
27 #include "gns_proxy_proto.h"
31 #include <gnutls/gnutls.h>
32 #include <gnutls/x509.h>
33 #include <gnutls/abstract.h>
34 #include <gnutls/crypto.h>
37 #define HAVE_MHD_NO_LISTEN_SOCKET MHD_VERSION >= 0x00091401
39 #define GNUNET_GNS_PROXY_PORT 7777
40 #define MHD_MAX_CONNECTIONS 300
41 #define MAX_HTTP_URI_LENGTH 2048
42 #define POSTBUFFERSIZE 4096
44 /* MHD/cURL defines */
45 #define BUF_WAIT_FOR_CURL 0
46 #define BUF_WAIT_FOR_MHD 1
47 #define BUF_WAIT_FOR_PP 2
48 #define HTML_HDR_CONTENT "Content-Type: text/html"
50 /* buffer padding for proper RE matching */
51 #define CURL_BUF_PADDING 1000
54 //#define RE_DOTPLUS "<a href=\"http://(([A-Za-z]+[.])+)([+])"
55 #define RE_A_HREF "href=\"https?://(([A-Za-z0-9]+[.])+)([+]|[a-z]+)"
56 #define RE_N_MATCHES 4
58 /* The usual suspects */
60 #define HTTPS_PORT 443
64 * A structure for CA cert/key
69 gnutls_x509_crt_t cert;
72 gnutls_x509_privkey_t key;
75 #define MAX_PEM_SIZE (10 * 1024)
78 * Structure for GNS certificates
80 struct ProxyGNSCertificate
82 /* The certificate as PEM */
83 char cert[MAX_PEM_SIZE];
85 /* The private key as PEM */
86 char key[MAX_PEM_SIZE];
91 * A structure for socks requests
95 /* The client socket */
96 struct GNUNET_NETWORK_Handle *sock;
98 /* The server socket */
99 struct GNUNET_NETWORK_Handle *remote_sock;
101 /* The socks state */
104 /* Client socket read task */
105 GNUNET_SCHEDULER_TaskIdentifier rtask;
107 /* Server socket read task */
108 GNUNET_SCHEDULER_TaskIdentifier fwdrtask;
110 /* Client socket write task */
111 GNUNET_SCHEDULER_TaskIdentifier wtask;
113 /* Server socket write task */
114 GNUNET_SCHEDULER_TaskIdentifier fwdwtask;
122 /* Length of data in read buffer */
123 unsigned int rbuf_len;
125 /* Length of data in write buffer */
126 unsigned int wbuf_len;
128 /* This handle is scheduled for cleanup? */
131 /* Shall we close the client socket on cleanup? */
136 * DLL for Network Handles
138 struct NetworkHandleList
141 struct NetworkHandleList *next;
144 struct NetworkHandleList *prev;
147 struct GNUNET_NETWORK_Handle *h;
151 * A structure for all running Httpds
156 struct MhdHttpList *prev;
159 struct MhdHttpList *next;
161 /* is this an ssl daemon? */
164 /* the domain name to server (only important for SSL) */
167 /* The daemon handle */
168 struct MHD_Daemon *daemon;
170 /* Optional proxy certificate used */
171 struct ProxyGNSCertificate *proxy_cert;
174 GNUNET_SCHEDULER_TaskIdentifier httpd_task;
176 /* Handles associated with this daemon */
177 struct NetworkHandleList *socket_handles_head;
179 /* Handles associated with this daemon */
180 struct NetworkHandleList *socket_handles_tail;
184 * A structure for MHD<->cURL streams
189 struct ProxyCurlTask *prev;
192 struct ProxyCurlTask *next;
194 /* Already accepted */
200 /* Optional header replacements for curl (LEHO) */
201 struct curl_slist *headers;
203 /* Optional resolver replacements for curl (LEHO) */
204 struct curl_slist *resolver;
206 /* curl response code */
207 long curl_response_code;
209 /* The URL to fetch */
210 char url[MAX_HTTP_URI_LENGTH];
212 /* The cURL write buffer / MHD read buffer */
213 char buffer[CURL_MAX_WRITE_SIZE + CURL_BUF_PADDING];
215 /* Read pos of the data in the buffer */
216 char *buffer_read_ptr;
218 /* Write pos in the buffer */
219 char *buffer_write_ptr;
221 /* The buffer status (BUF_WAIT_FOR_CURL or BUF_WAIT_FOR_MHD) */
224 /* Number of bytes in buffer */
225 unsigned int bytes_in_buffer;
227 /* Indicates wheather the download is in progress */
228 int download_in_progress;
230 /* Indicates wheather the download was successful */
231 int download_is_finished;
233 /* Indicates wheather the download failed */
236 /* Indicates wheather we need to parse HTML */
239 /* Indicates wheather we are postprocessing the HTML right now */
240 int is_postprocessing;
242 /* Indicates wheather postprocessing has finished */
246 GNUNET_SCHEDULER_TaskIdentifier pp_task;
249 struct ProxyREMatch *pp_match_head;
252 struct ProxyREMatch *pp_match_tail;
254 /* The authority of the corresponding host (site of origin) */
257 /* The hostname (Host header field) */
263 /* The LEgacy HOstname (can be empty) */
266 /* The associated daemon list entry */
267 struct MhdHttpList *mhd;
269 /* The associated response */
270 struct MHD_Response *response;
273 struct ProxySetCookieHeader *set_cookies_head;
276 struct ProxySetCookieHeader *set_cookies_tail;
278 /* connection status */
285 struct MHD_Connection *connection;
288 size_t put_read_offset;
289 size_t put_read_size;
292 struct MHD_PostProcessor *post_handler;
295 struct ProxyPostData *post_data_head;
296 struct ProxyPostData *post_data_tail;
300 /* the type of POST encoding */
306 * Struct for RE matches in postprocessing of HTML
311 struct ProxyREMatch *next;
314 struct ProxyREMatch *prev;
323 struct GNUNET_GNS_ShortenRequest *shorten_task;
328 /* start of match in buffer */
331 /* end of match in buffer */
334 /* associated proxycurltask */
335 struct ProxyCurlTask *ctask;
339 * Struct for set-cookies
341 struct ProxySetCookieHeader
344 struct ProxySetCookieHeader *next;
347 struct ProxySetCookieHeader *prev;
354 * Post data structure
359 struct ProxyPostData *next;
362 struct ProxyPostData *prev;
375 /* The port the proxy is running on (default 7777) */
376 static unsigned long port = GNUNET_GNS_PROXY_PORT;
378 /* The CA file (pem) to use for the proxy CA */
379 static char* cafile_opt;
381 /* The listen socket of the proxy */
382 static struct GNUNET_NETWORK_Handle *lsock;
384 /* The listen task ID */
385 GNUNET_SCHEDULER_TaskIdentifier ltask;
387 /* The cURL download task */
388 GNUNET_SCHEDULER_TaskIdentifier curl_download_task;
390 /* The non SSL httpd daemon handle */
391 static struct MHD_Daemon *httpd;
393 /* Number of current mhd connections */
394 static unsigned int total_mhd_connections;
396 /* The cURL multi handle */
397 static CURLM *curl_multi;
399 /* Handle to the GNS service */
400 static struct GNUNET_GNS_Handle *gns_handle;
402 /* DLL for ProxyCurlTasks */
403 static struct ProxyCurlTask *ctasks_head;
405 /* DLL for ProxyCurlTasks */
406 static struct ProxyCurlTask *ctasks_tail;
408 /* DLL for http daemons */
409 static struct MhdHttpList *mhd_httpd_head;
411 /* DLL for http daemons */
412 static struct MhdHttpList *mhd_httpd_tail;
414 /* Handle to the regex for dotplus (.+) replacement in HTML */
415 static regex_t re_dotplus;
417 /* The users local GNS zone hash */
418 static struct GNUNET_CRYPTO_ShortHashCode *local_gns_zone;
420 /* The users local private zone */
421 static struct GNUNET_CRYPTO_ShortHashCode *local_private_zone;
423 /* The users local shorten zone */
424 static struct GNUNET_CRYPTO_ShortHashCode *local_shorten_zone;
426 /* The CA for SSL certificate generation */
427 static struct ProxyCA proxy_ca;
429 /* UNIX domain socket for mhd */
430 struct GNUNET_NETWORK_Handle *mhd_unix_socket;
432 /* Shorten zone private key */
433 struct GNUNET_CRYPTO_RsaPrivateKey *shorten_zonekey;
436 * Checks if name is in tld
438 * @param name the name to check
439 * @param tld the TLD to check for
440 * @return GNUNET_YES or GNUNET_NO
443 is_tld(const char* name, const char* tld)
447 if (strlen(name) <= strlen(tld))
450 offset = strlen(name) - strlen(tld);
451 if (0 != strcmp (name+offset, tld))
453 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
454 "%s is not in .%s TLD\n", name, tld);
462 * convert integer to string representation
465 * @return the character
467 char i_to_hexchar (char i)
469 static char hexmap[] = "0123456789abcdef";
470 GNUNET_assert (sizeof (hexmap) > (i & 15));
471 return hexmap[i & 15];
475 * Escape given 0-terminated string
477 * @param to_esc string to escapse
478 * @return allocated new escaped string (MUST free!)
481 escape_to_urlenc (const char *to_esc)
483 char *pos = (char*)to_esc;
484 char *res = GNUNET_malloc (strlen (to_esc) * 3 + 1);
489 if (isalnum (*pos) ||
490 ('-' == *pos) || ('_' == *pos) ||
491 ('.' == *pos) || ('~' == *pos))
493 else if (' ' == *pos)
498 *rpos++ = i_to_hexchar (*pos >> 4);
499 *rpos++ = i_to_hexchar (*pos >> 15);
508 con_post_data_iter (void *cls,
509 enum MHD_ValueKind kind,
511 const char *filename,
512 const char *content_type,
513 const char *transfer_encoding,
518 struct ProxyCurlTask* ctask = cls;
519 struct ProxyPostData* pdata;
522 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
523 "Got POST data: '%s : %s' at offset %llu size %lld\n",
524 key, data, off, size);
526 if (0 != strcasecmp (MHD_HTTP_POST_ENCODING_FORM_URLENCODED,
535 pdata = GNUNET_malloc (sizeof (struct ProxyPostData));
536 enc = escape_to_urlenc (key);
537 pdata->value = GNUNET_malloc (strlen (enc) + 3);
538 if (NULL != ctask->post_data_head)
540 pdata->value[0] = '&';
541 memcpy (pdata->value+1, enc, strlen (enc));
544 memcpy (pdata->value, enc, strlen (enc));
545 pdata->value[strlen (pdata->value)] = '=';
546 pdata->bytes_left = strlen (pdata->value);
547 pdata->total_bytes = pdata->bytes_left;
550 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
551 "Escaped POST key: '%s'\n",
554 GNUNET_CONTAINER_DLL_insert_tail (ctask->post_data_head,
555 ctask->post_data_tail,
560 pdata = GNUNET_malloc (sizeof (struct ProxyPostData));
561 enc = escape_to_urlenc (data);
562 pdata->value = GNUNET_malloc (strlen (enc) + 1);
563 memcpy (pdata->value, enc, strlen (enc));
564 pdata->bytes_left = strlen (pdata->value);
565 pdata->total_bytes = pdata->bytes_left;
568 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
569 "Escaped POST value: '%s'\n",
572 GNUNET_CONTAINER_DLL_insert_tail (ctask->post_data_head,
573 ctask->post_data_tail,
580 * Read HTTP request header field 'Host'
582 * @param cls buffer to write to
583 * @param kind value kind
584 * @param key field key
585 * @param value field value
586 * @return MHD_NO when Host found
589 con_val_iter (void *cls,
590 enum MHD_ValueKind kind,
594 struct ProxyCurlTask *ctask = cls;
595 char* buf = ctask->host;
601 if (0 == strcmp ("Host", key))
603 port = strstr (value, ":");
606 strncpy (buf, value, port-value);
608 if ((1 != sscanf (port, "%u", &uport)) ||
609 (uport > UINT16_MAX) ||
611 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
612 "Unable to parse port!\n");
614 ctask->port = (uint16_t) uport;
621 if (0 == strcmp ("Accept-Encoding", key))
626 if (0 == strcmp (MHD_HTTP_HEADER_CONTENT_TYPE,
629 if (0 == strcmp (value,
630 MHD_HTTP_POST_ENCODING_FORM_URLENCODED))
631 ctask->post_type = MHD_HTTP_POST_ENCODING_FORM_URLENCODED;
632 else if (0 == strcmp (value,
633 MHD_HTTP_POST_ENCODING_MULTIPART_FORMDATA))
634 ctask->post_type = MHD_HTTP_POST_ENCODING_MULTIPART_FORMDATA;
636 ctask->post_type = NULL;
640 cstr = GNUNET_malloc (strlen (key) + strlen (hdr_val) + 3);
641 GNUNET_snprintf (cstr, strlen (key) + strlen (hdr_val) + 3,
642 "%s: %s", key, hdr_val);
644 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
645 "Client Header: %s\n", cstr);
647 ctask->headers = curl_slist_append (ctask->headers, cstr);
655 * Callback for MHD response
658 * @param pos in buffer
660 * @param max space in buffer
661 * @return number of bytes written
664 mhd_content_cb (void *cls,
670 * Check HTTP response header for mime
672 * @param buffer curl buffer
673 * @param size curl blocksize
674 * @param nmemb curl blocknumber
676 * @return size of read bytes
679 curl_check_hdr (void *buffer, size_t size, size_t nmemb, void *cls)
681 size_t bytes = size * nmemb;
682 struct ProxyCurlTask *ctask = cls;
683 int html_mime_len = strlen (HTML_HDR_CONTENT);
684 int cookie_hdr_len = strlen (MHD_HTTP_HEADER_SET_COOKIE);
685 char hdr_mime[html_mime_len+1];
686 char hdr_generic[bytes+1];
687 char new_cookie_hdr[bytes+strlen (ctask->leho)+1];
688 char new_location[MAX_HTTP_URI_LENGTH+500];
699 if (NULL == ctask->response)
701 /* FIXME: get total size from curl (if available) */
702 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
703 "Creating response for %s\n", ctask->url);
704 ctask->response = MHD_create_response_from_callback (MHD_SIZE_UNKNOWN,
705 sizeof (ctask->buffer),
709 ctask->ready_to_queue = GNUNET_YES;
713 if (html_mime_len <= bytes)
715 memcpy (hdr_mime, buffer, html_mime_len);
716 hdr_mime[html_mime_len] = '\0';
718 if (0 == strcmp (hdr_mime, HTML_HDR_CONTENT))
720 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
721 "Got HTML HTTP response header\n");
722 ctask->parse_content = GNUNET_YES;
726 if (cookie_hdr_len > bytes)
729 memcpy (hdr_generic, buffer, bytes);
730 hdr_generic[bytes] = '\0';
732 if ('\n' == hdr_generic[bytes-1])
733 hdr_generic[bytes-1] = '\0';
735 if (hdr_generic[bytes-2] == '\r')
736 hdr_generic[bytes-2] = '\0';
738 if (0 == memcmp (hdr_generic,
739 MHD_HTTP_HEADER_SET_COOKIE,
742 ndup = GNUNET_strdup (hdr_generic+cookie_hdr_len+1);
743 memset (new_cookie_hdr, 0, sizeof (new_cookie_hdr));
744 tok = strtok (ndup, ";");
746 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
747 "Looking for cookie in : %s\n", hdr_generic);
749 for (; tok != NULL; tok = strtok (NULL, ";"))
751 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
752 "Got Cookie token: %s\n", tok);
753 //memcpy (new_cookie_hdr+offset, tok, strlen (tok));
754 if (0 == memcmp (tok, " domain", strlen (" domain")))
756 cookie_domain = tok + strlen (" domain") + 1;
758 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
759 "Got Set-Cookie Domain: %s\n", cookie_domain);
761 if (strlen (cookie_domain) < strlen (ctask->leho))
763 delta_cdomain = strlen (ctask->leho) - strlen (cookie_domain);
764 if (0 == strcmp (cookie_domain, ctask->leho + (delta_cdomain)))
766 GNUNET_snprintf (new_cookie_hdr+offset,
767 sizeof (new_cookie_hdr),
768 " domain=%s", ctask->authority);
769 offset += strlen (" domain=") + strlen (ctask->authority);
770 new_cookie_hdr[offset] = ';';
775 else if (strlen (cookie_domain) == strlen (ctask->leho))
777 if (0 == strcmp (cookie_domain, ctask->leho))
779 GNUNET_snprintf (new_cookie_hdr+offset,
780 sizeof (new_cookie_hdr),
781 " domain=%s", ctask->host);
782 offset += strlen (" domain=") + strlen (ctask->host);
783 new_cookie_hdr[offset] = ';';
788 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
789 "Cookie domain invalid\n");
793 memcpy (new_cookie_hdr+offset, tok, strlen (tok));
794 offset += strlen (tok);
795 new_cookie_hdr[offset] = ';';
801 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
802 "Got Set-Cookie HTTP header %s\n", new_cookie_hdr);
804 if (GNUNET_NO == MHD_add_response_header (ctask->response,
805 MHD_HTTP_HEADER_SET_COOKIE,
808 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
809 "MHD: Error adding set-cookie header field %s\n",
810 hdr_generic+cookie_hdr_len+1);
815 ndup = GNUNET_strdup (hdr_generic);
816 hdr_type = strtok (ndup, ":");
818 if (NULL == hdr_type)
824 hdr_val = strtok (NULL, "");
834 if (0 == strcasecmp (MHD_HTTP_HEADER_LOCATION, hdr_type))
836 if (ctask->mhd->is_ssl)
838 sprintf (leho_host, "https://%s", ctask->leho);
839 sprintf (real_host, "https://%s", ctask->host);
843 sprintf (leho_host, "http://%s", ctask->leho);
844 sprintf (real_host, "http://%s", ctask->host);
847 if (0 == memcmp (leho_host, hdr_val, strlen (leho_host)))
849 sprintf (new_location, "%s%s", real_host, hdr_val+strlen (leho_host));
850 hdr_val = new_location;
854 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
855 "Trying to set %s: %s\n",
858 if (GNUNET_NO == MHD_add_response_header (ctask->response,
862 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
863 "MHD: Error adding %s header field %s\n",
874 * @param hd a http daemon list entry
877 run_httpd (struct MhdHttpList *hd);
888 * Task run whenever HTTP server operations are pending.
891 * @param tc sched context
895 const struct GNUNET_SCHEDULER_TaskContext *tc);
898 run_mhd_now (struct MhdHttpList *hd)
900 if (GNUNET_SCHEDULER_NO_TASK != hd->httpd_task)
902 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
903 "MHD: killing old task\n");
904 GNUNET_SCHEDULER_cancel (hd->httpd_task);
906 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
907 "MHD: Scheduling MHD now\n");
908 hd->httpd_task = GNUNET_SCHEDULER_add_now (&do_httpd, hd);
912 * Ask cURL for the select sets and schedule download
915 curl_download_prepare ();
918 * Callback to free content
920 * @param cls content to free
921 * @param tc task context
924 mhd_content_free (void *cls,
925 const struct GNUNET_SCHEDULER_TaskContext *tc)
927 struct ProxyCurlTask *ctask = cls;
928 GNUNET_assert (NULL == ctask->pp_match_head);
930 if (NULL != ctask->headers)
931 curl_slist_free_all (ctask->headers);
933 if (NULL != ctask->headers)
934 curl_slist_free_all (ctask->resolver);
936 if (NULL != ctask->response)
937 MHD_destroy_response (ctask->response);
939 if (NULL != ctask->post_handler)
940 MHD_destroy_post_processor (ctask->post_handler);
948 * Callback for MHD response
951 * @param pos in buffer
953 * @param max space in buffer
954 * @return number of bytes written
957 mhd_content_cb (void *cls,
962 struct ProxyCurlTask *ctask = cls;
963 struct ProxyREMatch *re_match = ctask->pp_match_head;
965 long long int bytes_to_copy = ctask->buffer_write_ptr - ctask->buffer_read_ptr;
967 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
968 "MHD: content cb for %s. To copy: %lld\n",
969 ctask->url, bytes_to_copy);
970 GNUNET_assert (bytes_to_copy >= 0);
972 if ((GNUNET_YES == ctask->download_is_finished) &&
973 (GNUNET_NO == ctask->download_error) &&
974 (0 == bytes_to_copy)) /* &&
975 (BUF_WAIT_FOR_CURL == ctask->buf_status))*/
977 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
978 "MHD: sending response for %s\n", ctask->url);
979 ctask->download_in_progress = GNUNET_NO;
980 run_mhd_now (ctask->mhd);
981 GNUNET_SCHEDULER_add_now (&mhd_content_free, ctask);
982 total_mhd_connections--;
983 return MHD_CONTENT_READER_END_OF_STREAM;
986 if ((GNUNET_YES == ctask->download_error) &&
987 (GNUNET_YES == ctask->download_is_finished) &&
988 (0 == bytes_to_copy)) /* &&
989 (BUF_WAIT_FOR_CURL == ctask->buf_status))*/
991 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
992 "MHD: sending error response\n");
993 ctask->download_in_progress = GNUNET_NO;
994 run_mhd_now (ctask->mhd);
995 GNUNET_SCHEDULER_add_now (&mhd_content_free, ctask);
996 total_mhd_connections--;
997 return MHD_CONTENT_READER_END_WITH_ERROR;
1000 if ( ctask->buf_status == BUF_WAIT_FOR_CURL )
1004 for (; NULL != re_match; re_match = ctask->pp_match_head)
1006 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1007 "MHD: Processing PP %s\n",
1008 re_match->hostname);
1009 bytes_to_copy = re_match->start - ctask->buffer_read_ptr;
1010 GNUNET_assert (bytes_to_copy >= 0);
1012 if (bytes_to_copy+copied > max)
1014 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1015 "MHD: buffer in response too small for %d. Using available space (%d). (%s)\n",
1019 memcpy (buf+copied, ctask->buffer_read_ptr, max-copied);
1020 ctask->buffer_read_ptr += max-copied;
1022 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1023 "MHD: copied %d bytes\n", copied);
1027 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1028 "MHD: copying %d bytes to mhd response at offset %d\n",
1029 bytes_to_copy, ctask->buffer_read_ptr);
1030 memcpy (buf+copied, ctask->buffer_read_ptr, bytes_to_copy);
1031 copied += bytes_to_copy;
1033 if (GNUNET_NO == re_match->done)
1035 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1036 "MHD: Waiting for PP of %s\n", re_match->hostname);
1037 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1038 "MHD: copied %d bytes\n", copied);
1039 ctask->buffer_read_ptr += bytes_to_copy;
1043 if (strlen (re_match->result) > (max - copied))
1045 //FIXME partially copy domain here
1046 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1047 "MHD: buffer in response too small for %s! (%s)\n",
1050 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1051 "MHD: copied %d bytes\n", copied);
1052 ctask->buffer_read_ptr += bytes_to_copy;
1056 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1057 "MHD: Adding PP result %s to buffer\n",
1059 memcpy (buf+copied, re_match->result, strlen (re_match->result));
1060 copied += strlen (re_match->result);
1061 ctask->buffer_read_ptr = re_match->end;
1062 GNUNET_CONTAINER_DLL_remove (ctask->pp_match_head,
1063 ctask->pp_match_tail,
1065 GNUNET_free (re_match);
1068 bytes_to_copy = ctask->buffer_write_ptr - ctask->buffer_read_ptr;
1070 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1071 "MHD: copied: %d left: %d, space left in buf: %d\n",
1073 bytes_to_copy, max-copied);
1075 GNUNET_assert (0 <= bytes_to_copy);
1077 if (GNUNET_NO == ctask->download_is_finished)
1079 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1080 "MHD: Purging buffer\n");
1081 memmove (ctask->buffer, ctask->buffer_read_ptr, bytes_to_copy);
1082 ctask->buffer_read_ptr = ctask->buffer;
1083 ctask->buffer_write_ptr = ctask->buffer + bytes_to_copy;
1084 ctask->buffer[bytes_to_copy] = '\0';
1087 if (bytes_to_copy+copied > max)
1088 bytes_to_copy = max-copied;
1090 if (0 > bytes_to_copy)
1093 memcpy (buf+copied, ctask->buffer_read_ptr, bytes_to_copy);
1094 ctask->buffer_read_ptr += bytes_to_copy;
1095 copied += bytes_to_copy;
1096 ctask->buf_status = BUF_WAIT_FOR_CURL;
1098 if (NULL != ctask->curl)
1099 curl_easy_pause (ctask->curl, CURLPAUSE_CONT);
1101 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1102 "MHD: copied %d bytes\n", copied);
1103 run_mhd_now (ctask->mhd);
1108 * Shorten result callback
1110 * @param cls the proxycurltask
1111 * @param short_name the shortened name (NULL on error)
1114 process_shorten (void* cls, const char* short_name)
1116 struct ProxyREMatch *re_match = cls;
1117 char result[sizeof (re_match->result)];
1119 if (NULL == short_name)
1121 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1122 "PP: Unable to shorten %s\n",
1123 re_match->hostname);
1124 GNUNET_CONTAINER_DLL_remove (re_match->ctask->pp_match_head,
1125 re_match->ctask->pp_match_tail,
1127 GNUNET_free (re_match);
1131 if (0 == strcmp (short_name, re_match->ctask->leho))
1132 strcpy (result, re_match->ctask->host);
1134 strcpy (result, short_name);
1136 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1137 "PP: Shorten %s -> %s\n",
1141 if (re_match->ctask->mhd->is_ssl)
1142 sprintf (re_match->result, "href=\"https://%s", result);
1144 sprintf (re_match->result, "href=\"http://%s", result);
1146 re_match->done = GNUNET_YES;
1147 run_mhd_now (re_match->ctask->mhd);
1152 * Postprocess data in buffer. From read ptr to write ptr
1154 * @param cls the curlproxytask
1155 * @param tc task context
1158 postprocess_buffer (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1160 struct ProxyCurlTask *ctask = cls;
1161 struct ProxyREMatch *re_match;
1162 char* re_ptr = ctask->buffer_read_ptr;
1163 char re_hostname[255];
1164 regmatch_t m[RE_N_MATCHES];
1166 ctask->pp_task = GNUNET_SCHEDULER_NO_TASK;
1168 if (GNUNET_YES != ctask->parse_content)
1170 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1171 "PP: Not parsing content\n");
1172 ctask->buf_status = BUF_WAIT_FOR_MHD;
1173 run_mhd_now (ctask->mhd);
1177 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1178 "PP: We need to parse the HTML\n");
1180 /* 0 means match found */
1181 while (0 == regexec (&re_dotplus, re_ptr, RE_N_MATCHES, m, 0))
1183 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1184 "PP: regex match\n");
1186 GNUNET_assert (m[1].rm_so != -1);
1188 memset (re_hostname, 0, sizeof (re_hostname));
1189 memcpy (re_hostname, re_ptr+m[1].rm_so, (m[3].rm_eo-m[1].rm_so));
1191 re_match = GNUNET_malloc (sizeof (struct ProxyREMatch));
1192 re_match->start = re_ptr + m[0].rm_so;
1193 re_match->end = re_ptr + m[3].rm_eo;
1194 re_match->done = GNUNET_NO;
1195 re_match->ctask = ctask;
1196 strcpy (re_match->hostname, re_hostname);
1197 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1198 "PP: Got hostname %s\n", re_hostname);
1199 re_ptr += m[3].rm_eo;
1201 if (GNUNET_YES == is_tld (re_match->hostname, GNUNET_GNS_TLD_PLUS))
1203 re_match->hostname[strlen(re_match->hostname)-1] = '\0';
1204 strcpy (re_match->hostname+strlen(re_match->hostname),
1208 re_match->shorten_task = GNUNET_GNS_shorten_zone (gns_handle,
1214 re_match); //FIXME cancel appropriately
1216 GNUNET_CONTAINER_DLL_insert_tail (ctask->pp_match_head,
1217 ctask->pp_match_tail,
1221 ctask->buf_status = BUF_WAIT_FOR_MHD;
1222 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1223 "PP: No more matches\n");
1224 run_mhd_now (ctask->mhd);
1228 * Handle data from cURL
1230 * @param ptr pointer to the data
1231 * @param size number of blocks of data
1232 * @param nmemb blocksize
1233 * @param ctx the curlproxytask
1234 * @return number of bytes handled
1237 curl_download_cb (void *ptr, size_t size, size_t nmemb, void* ctx)
1239 const char *cbuf = ptr;
1240 size_t total = size * nmemb;
1241 struct ProxyCurlTask *ctask = ctx;
1242 size_t buf_space = sizeof (ctask->buffer) -
1243 (ctask->buffer_write_ptr-ctask->buffer);
1245 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1246 "CURL: Got %d. %d free in buffer\n",
1249 if (BUF_WAIT_FOR_CURL != ctask->buf_status)
1250 return CURL_WRITEFUNC_PAUSE;
1252 if (total > (buf_space - CURL_BUF_PADDING))
1254 if (ctask->buf_status == BUF_WAIT_FOR_CURL)
1256 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1257 "CURL: Buffer full starting postprocessing\n");
1258 ctask->buf_status = BUF_WAIT_FOR_PP;
1259 ctask->pp_task = GNUNET_SCHEDULER_add_now (&postprocess_buffer,
1261 return CURL_WRITEFUNC_PAUSE;
1264 /* we should not get called in that case */
1265 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1266 "CURL: called out of context and no space in buffer!\n");
1267 return CURL_WRITEFUNC_PAUSE;
1270 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1271 "CURL: Copying %d bytes to buffer (%s)\n", total, ctask->url);
1272 memcpy (ctask->buffer_write_ptr, cbuf, total);
1273 ctask->bytes_in_buffer += total;
1274 ctask->buffer_write_ptr += total;
1275 ctask->buffer_write_ptr[0] = '\0';
1281 * cURL callback for post data
1284 read_callback (void *buf, size_t size, size_t nmemb, void *cls)
1286 struct ProxyCurlTask *ctask = cls;
1287 struct ProxyPostData *pdata = ctask->post_data_head;
1288 size_t len = size * nmemb;
1292 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1293 "CURL: read callback\n");
1299 if (NULL == pdata->value)
1301 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1302 "CURL: Terminating POST data\n");
1304 GNUNET_CONTAINER_DLL_remove (ctask->post_data_head,
1305 ctask->post_data_tail,
1307 GNUNET_free (pdata);
1311 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1312 "CURL: read callback value %s\n", pdata->value);
1314 to_copy = pdata->bytes_left;
1318 pos = pdata->value + (pdata->total_bytes - pdata->bytes_left);
1319 memcpy (buf, pos, to_copy);
1320 pdata->bytes_left -= to_copy;
1321 if (pdata->bytes_left <= 0)
1323 GNUNET_free (pdata->value);
1324 GNUNET_CONTAINER_DLL_remove (ctask->post_data_head,
1325 ctask->post_data_tail,
1327 GNUNET_free (pdata);
1333 * Task that is run when we are ready to receive more data
1336 * @param cls closure
1337 * @param tc task context
1340 curl_task_download (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
1343 * Ask cURL for the select sets and schedule download
1346 curl_download_prepare ()
1353 struct GNUNET_NETWORK_FDSet *grs;
1354 struct GNUNET_NETWORK_FDSet *gws;
1356 struct GNUNET_TIME_Relative rtime;
1362 mret = curl_multi_fdset (curl_multi, &rs, &ws, &es, &max);
1364 if (mret != CURLM_OK)
1366 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1367 "%s failed at %s:%d: `%s'\n",
1368 "curl_multi_fdset", __FILE__, __LINE__,
1369 curl_multi_strerror (mret));
1370 //TODO cleanup here?
1374 mret = curl_multi_timeout (curl_multi, &to);
1375 rtime = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, to);
1377 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1378 "cURL multi fds: max=%d timeout=%llu\n", max, to);
1380 grs = GNUNET_NETWORK_fdset_create ();
1381 gws = GNUNET_NETWORK_fdset_create ();
1382 GNUNET_NETWORK_fdset_copy_native (grs, &rs, max + 1);
1383 GNUNET_NETWORK_fdset_copy_native (gws, &ws, max + 1);
1384 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1385 "Scheduling task cURL\n");
1387 if (curl_download_task != GNUNET_SCHEDULER_NO_TASK)
1388 GNUNET_SCHEDULER_cancel (curl_download_task);
1392 curl_download_task =
1393 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1396 &curl_task_download, curl_multi);
1398 else if (NULL != ctasks_head)
1400 /* as specified in curl docs */
1401 curl_download_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MILLISECONDS,
1402 &curl_task_download,
1405 GNUNET_NETWORK_fdset_destroy (gws);
1406 GNUNET_NETWORK_fdset_destroy (grs);
1411 * Task that is run when we are ready to receive more data
1414 * @param cls closure
1415 * @param tc task context
1418 curl_task_download (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1422 struct CURLMsg *msg;
1424 struct ProxyCurlTask *ctask;
1428 struct ProxyCurlTask *clean_head = NULL;
1429 struct ProxyCurlTask *clean_tail = NULL;
1431 curl_download_task = GNUNET_SCHEDULER_NO_TASK;
1433 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1435 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1436 "Shutdown requested while trying to download\n");
1440 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1448 mret = curl_multi_perform (curl_multi, &running);
1450 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1451 "Running curl tasks: %d\n", running);
1453 ctask = ctasks_head;
1454 for (; ctask != NULL; ctask = ctask->next)
1456 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1457 "CTask: %s\n", ctask->url);
1461 if (num_ctasks != running)
1463 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1464 "%d ctasks, %d curl running\n", num_ctasks, running);
1469 ctask = ctasks_head;
1470 msg = curl_multi_info_read (curl_multi, &msgnum);
1471 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1472 "Messages left: %d\n", msgnum);
1479 if ((msg->data.result != CURLE_OK) &&
1480 (msg->data.result != CURLE_GOT_NOTHING))
1482 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1483 "Download curl failed");
1485 for (; ctask != NULL; ctask = ctask->next)
1487 if (NULL == ctask->curl)
1490 if (memcmp (msg->easy_handle, ctask->curl, sizeof (CURL)) != 0)
1493 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1494 "CURL: Download failed for task %s: %s.\n",
1496 curl_easy_strerror (msg->data.result));
1497 ctask->download_is_finished = GNUNET_YES;
1498 ctask->download_error = GNUNET_YES;
1499 if (CURLE_OK == curl_easy_getinfo (ctask->curl,
1500 CURLINFO_RESPONSE_CODE,
1502 ctask->curl_response_code = resp_code;
1503 ctask->ready_to_queue = MHD_YES;
1504 ctask->buf_status = BUF_WAIT_FOR_MHD;
1505 run_mhd_now (ctask->mhd);
1507 GNUNET_CONTAINER_DLL_remove (ctasks_head, ctasks_tail,
1509 GNUNET_CONTAINER_DLL_insert (clean_head, clean_tail, ctask);
1512 GNUNET_assert (ctask != NULL);
1516 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1517 "CURL: download completed.\n");
1519 for (; ctask != NULL; ctask = ctask->next)
1521 if (NULL == ctask->curl)
1524 if (0 != memcmp (msg->easy_handle, ctask->curl, sizeof (CURL)))
1527 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1528 "CURL: completed task %s found.\n", ctask->url);
1529 if (CURLE_OK == curl_easy_getinfo (ctask->curl,
1530 CURLINFO_RESPONSE_CODE,
1532 ctask->curl_response_code = resp_code;
1535 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1536 "CURL: Completed ctask!\n");
1537 if (GNUNET_SCHEDULER_NO_TASK == ctask->pp_task)
1539 ctask->buf_status = BUF_WAIT_FOR_PP;
1540 ctask->pp_task = GNUNET_SCHEDULER_add_now (&postprocess_buffer,
1544 ctask->ready_to_queue = MHD_YES;
1545 ctask->download_is_finished = GNUNET_YES;
1547 /* We MUST not modify the multi handle else we loose messages */
1548 GNUNET_CONTAINER_DLL_remove (ctasks_head, ctasks_tail,
1550 GNUNET_CONTAINER_DLL_insert (clean_head, clean_tail, ctask);
1554 GNUNET_assert (ctask != NULL);
1556 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1557 "CURL: %s\n", curl_easy_strerror(msg->data.result));
1563 } while (msgnum > 0);
1565 for (ctask=clean_head; ctask != NULL; ctask = ctask->next)
1567 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1568 "CURL: Removing task %s.\n", ctask->url);
1569 curl_multi_remove_handle (curl_multi, ctask->curl);
1570 curl_easy_cleanup (ctask->curl);
1575 for (ctask=ctasks_head; ctask != NULL; ctask = ctask->next)
1580 if (num_ctasks != running)
1582 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1583 "CURL: %d tasks, %d running\n", num_ctasks, running);
1586 GNUNET_assert ( num_ctasks == running );
1588 } while (mret == CURLM_CALL_MULTI_PERFORM);
1590 if (mret != CURLM_OK)
1592 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "CURL: %s failed at %s:%d: `%s'\n",
1593 "curl_multi_perform", __FILE__, __LINE__,
1594 curl_multi_strerror (mret));
1596 curl_download_prepare();
1600 * Process LEHO lookup
1602 * @param cls the ctask
1603 * @param rd_count number of records returned
1604 * @param rd record data
1607 process_leho_lookup (void *cls,
1609 const struct GNUNET_NAMESTORE_RecordData *rd)
1611 struct ProxyCurlTask *ctask = cls;
1612 char hosthdr[262]; //256 + "Host: "
1616 struct hostent *phost;
1618 char resolvename[512];
1621 strcpy (ctask->leho, "");
1624 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1625 "No LEHO present!\n");
1627 for (i=0; i<rd_count; i++)
1629 if (rd[i].record_type != GNUNET_GNS_RECORD_LEHO)
1632 memcpy (ctask->leho, rd[i].data, rd[i].data_size);
1634 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1635 "Found LEHO %s for %s\n", ctask->leho, ctask->url);
1638 if (0 != strcmp (ctask->leho, ""))
1640 sprintf (hosthdr, "%s%s:%d", "Host: ", ctask->leho, ctask->port);
1641 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1642 "New HTTP header value: %s\n", hosthdr);
1643 ctask->headers = curl_slist_append (ctask->headers, hosthdr);
1644 GNUNET_assert (NULL != ctask->headers);
1645 ret = curl_easy_setopt (ctask->curl, CURLOPT_HTTPHEADER, ctask->headers);
1646 if (CURLE_OK != ret)
1648 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "%s failed at %s:%d: `%s'\n",
1649 "curl_easy_setopt", __FILE__, __LINE__, curl_easy_strerror(ret));
1654 if (ctask->mhd->is_ssl)
1656 phost = (struct hostent*)gethostbyname (ctask->host);
1660 ssl_ip = inet_ntoa(*((struct in_addr*)(phost->h_addr)));
1661 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1662 "SSL target server: %s\n", ssl_ip);
1663 sprintf (resolvename, "%s:%d:%s", ctask->leho, HTTPS_PORT, ssl_ip);
1664 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1665 "Curl resolve: %s\n", resolvename);
1666 ctask->resolver = curl_slist_append ( ctask->resolver, resolvename);
1667 curl_easy_setopt (ctask->curl, CURLOPT_RESOLVE, ctask->resolver);
1668 sprintf (curlurl, "https://%s:%d%s", ctask->leho, ctask->port, ctask->url);
1669 curl_easy_setopt (ctask->curl, CURLOPT_URL, curlurl);
1673 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1674 "gethostbyname failed for %s!\n", ctask->host);
1675 ctask->download_is_finished = GNUNET_YES;
1676 ctask->download_error = GNUNET_YES;
1681 if (CURLM_OK != (mret=curl_multi_add_handle (curl_multi, ctask->curl)))
1683 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1684 "%s failed at %s:%d: `%s'\n",
1685 "curl_multi_add_handle", __FILE__, __LINE__,
1686 curl_multi_strerror (mret));
1687 ctask->download_is_finished = GNUNET_YES;
1688 ctask->download_error = GNUNET_YES;
1691 GNUNET_CONTAINER_DLL_insert (ctasks_head, ctasks_tail, ctask);
1693 curl_download_prepare ();
1698 * Initialize download and trigger curl
1700 * @param cls the proxycurltask
1701 * @param auth_name the name of the authority (site of origin) of ctask->host
1705 process_get_authority (void *cls,
1706 const char* auth_name)
1708 struct ProxyCurlTask *ctask = cls;
1710 if (NULL == auth_name)
1712 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1713 "Get authority failed!\n");
1714 strcpy (ctask->authority, "");
1718 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1719 "Get authority yielded %s\n", auth_name);
1720 strcpy (ctask->authority, auth_name);
1723 GNUNET_GNS_lookup_zone (gns_handle,
1726 GNUNET_GNS_RECORD_LEHO,
1727 GNUNET_YES, //Only cached for performance
1729 &process_leho_lookup,
1734 mhd_log_callback (void* cls, const char* url)
1736 struct ProxyCurlTask *ctask;
1738 ctask = GNUNET_malloc (sizeof (struct ProxyCurlTask));
1739 strcpy (ctask->url, url);
1740 ctask->accepted = GNUNET_NO;
1745 * Main MHD callback for handling requests.
1748 * @param con MHD connection handle
1749 * @param url the url in the request
1750 * @param meth the HTTP method used ("GET", "PUT", etc.)
1751 * @param ver the HTTP version string (i.e. "HTTP/1.1")
1752 * @param upload_data the data being uploaded (excluding HEADERS,
1753 * for a POST that fits into memory and that is encoded
1754 * with a supported encoding, the POST data will NOT be
1755 * given in upload_data and is instead available as
1756 * part of MHD_get_connection_values; very large POST
1757 * data *will* be made available incrementally in
1759 * @param upload_data_size set initially to the size of the
1760 * upload_data provided; the method must update this
1761 * value to the number of bytes NOT processed;
1762 * @param con_cls pointer to location where we store the 'struct Request'
1763 * @return MHD_YES if the connection was handled successfully,
1764 * MHD_NO if the socket must be closed due to a serious
1765 * error while handling the request
1768 create_response (void *cls,
1769 struct MHD_Connection *con,
1773 const char *upload_data,
1774 size_t *upload_data_size,
1777 struct MhdHttpList* hd = cls;
1778 const char* page = "<html><head><title>gnoxy</title>"\
1779 "</head><body>cURL fail</body></html>";
1781 char curlurl[MAX_HTTP_URI_LENGTH]; // buffer overflow!
1784 struct ProxyCurlTask *ctask = *con_cls;
1785 struct ProxyPostData *fin_post;
1788 if ((0 != strcasecmp (meth, MHD_HTTP_METHOD_GET)) &&
1789 (0 != strcasecmp (meth, MHD_HTTP_METHOD_PUT)) &&
1790 (0 != strcasecmp (meth, MHD_HTTP_METHOD_POST)) &&
1791 (0 != strcasecmp (meth, MHD_HTTP_METHOD_HEAD)))
1793 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1794 "MHD: %s NOT IMPLEMENTED!\n", meth);
1799 if (GNUNET_NO == ctask->accepted)
1802 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1803 "Got %s request for %s\n", meth, url);
1805 ctask->curl = curl_easy_init();
1806 if (NULL == ctask->curl)
1808 ctask->response = MHD_create_response_from_buffer (strlen (page),
1810 MHD_RESPMEM_PERSISTENT);
1811 ret = MHD_queue_response (con,
1814 MHD_destroy_response (ctask->response);
1815 GNUNET_free (ctask);
1819 /* Add GNS header */
1820 ctask->headers = curl_slist_append (ctask->headers,
1822 ctask->accepted = GNUNET_YES;
1823 ctask->download_in_progress = GNUNET_YES;
1824 ctask->buf_status = BUF_WAIT_FOR_CURL;
1825 ctask->connection = con;
1826 ctask->curl_response_code = MHD_HTTP_OK;
1827 ctask->buffer_read_ptr = ctask->buffer;
1828 ctask->buffer_write_ptr = ctask->buffer;
1829 ctask->pp_task = GNUNET_SCHEDULER_NO_TASK;
1830 if (ctask->mhd->is_ssl)
1831 ctask->port = HTTPS_PORT;
1833 ctask->port = HTTP_PORT;
1835 MHD_get_connection_values (con,
1837 &con_val_iter, ctask);
1839 if (0 == strcasecmp (meth, MHD_HTTP_METHOD_PUT))
1841 //FIXME: this prolly works like POST?
1842 if (0 == *upload_data_size)
1844 curl_easy_cleanup (ctask->curl);
1845 GNUNET_free (ctask);
1848 ctask->put_read_offset = 0;
1849 ctask->put_read_size = *upload_data_size;
1850 curl_easy_setopt (ctask->curl, CURLOPT_UPLOAD, 1);
1851 curl_easy_setopt (ctask->curl, CURLOPT_READDATA, upload_data);
1852 //curl_easy_setopt (ctask->curl, CURLOPT_READFUNCTION, &curl_read_cb);
1854 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1855 "Got PUT data: %s\n", upload_data);
1856 curl_easy_cleanup (ctask->curl);
1857 GNUNET_free (ctask);
1861 if (0 == strcasecmp (meth, MHD_HTTP_METHOD_POST))
1863 //FIXME handle multipart
1864 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1865 "Setting up POST processor\n");
1866 ctask->post_handler = MHD_create_post_processor (con,
1868 &con_post_data_iter,
1870 curl_easy_setopt (ctask->curl, CURLOPT_POST, 1);
1871 curl_easy_setopt (ctask->curl, CURLOPT_READFUNCTION,
1873 curl_easy_setopt (ctask->curl, CURLOPT_READDATA, ctask);
1874 ctask->headers = curl_slist_append (ctask->headers,
1875 "Transfer-Encoding: chunked");
1878 if (0 == strcasecmp (meth, MHD_HTTP_METHOD_HEAD))
1880 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1881 "Setting NOBODY\n");
1882 curl_easy_setopt (ctask->curl, CURLOPT_NOBODY, 1);
1885 curl_easy_setopt (ctask->curl, CURLOPT_HEADERFUNCTION, &curl_check_hdr);
1886 curl_easy_setopt (ctask->curl, CURLOPT_HEADERDATA, ctask);
1887 curl_easy_setopt (ctask->curl, CURLOPT_WRITEFUNCTION, &curl_download_cb);
1888 curl_easy_setopt (ctask->curl, CURLOPT_WRITEDATA, ctask);
1889 curl_easy_setopt (ctask->curl, CURLOPT_FOLLOWLOCATION, 0);
1890 curl_easy_setopt (ctask->curl, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
1892 if (GNUNET_NO == ctask->mhd->is_ssl)
1894 sprintf (curlurl, "http://%s:%d%s", ctask->host, ctask->port, ctask->url);
1895 //MHD_get_connection_values (con,
1896 // MHD_GET_ARGUMENT_KIND,
1897 // &get_uri_val_iter, curlurl);
1898 curl_easy_setopt (ctask->curl, CURLOPT_URL, curlurl);
1901 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1902 "MHD: Adding new curl task for %s\n", ctask->host);
1903 //MHD_get_connection_values (con,
1904 // MHD_GET_ARGUMENT_KIND,
1905 // &get_uri_val_iter, ctask->url);
1907 curl_easy_setopt (ctask->curl, CURLOPT_FAILONERROR, 1);
1908 curl_easy_setopt (ctask->curl, CURLOPT_CONNECTTIMEOUT, 600L);
1909 curl_easy_setopt (ctask->curl, CURLOPT_TIMEOUT, 600L);
1911 GNUNET_GNS_get_authority (gns_handle,
1913 &process_get_authority,
1915 ctask->ready_to_queue = GNUNET_NO;
1916 ctask->fin = GNUNET_NO;
1920 ctask = (struct ProxyCurlTask *) *con_cls;
1921 if (0 == strcasecmp (meth, MHD_HTTP_METHOD_POST))
1923 if (0 != *upload_data_size)
1925 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1926 "Invoking POST processor\n");
1927 MHD_post_process (ctask->post_handler,
1928 upload_data, *upload_data_size);
1929 *upload_data_size = 0;
1932 else if (GNUNET_NO == ctask->post_done)
1934 fin_post = GNUNET_malloc (sizeof (struct ProxyPostData));
1935 GNUNET_CONTAINER_DLL_insert_tail (ctask->post_data_head,
1936 ctask->post_data_tail,
1938 ctask->post_done = GNUNET_YES;
1943 if (GNUNET_YES != ctask->ready_to_queue)
1944 return MHD_YES; /* wait longer */
1946 if (GNUNET_YES == ctask->fin)
1949 ctask->fin = GNUNET_YES;
1950 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1951 "MHD: Queueing response for %s\n", ctask->url);
1952 ret = MHD_queue_response (con, ctask->curl_response_code, ctask->response);
1953 run_mhd_now (ctask->mhd);
1964 struct MhdHttpList *hd;
1966 for (hd=mhd_httpd_head; NULL != hd; hd = hd->next)
1974 * @param hd the daemon to run
1977 run_httpd (struct MhdHttpList *hd)
1982 struct GNUNET_NETWORK_FDSet *wrs;
1983 struct GNUNET_NETWORK_FDSet *wws;
1984 struct GNUNET_NETWORK_FDSet *wes;
1987 unsigned MHD_LONG_LONG timeout;
1988 struct GNUNET_TIME_Relative tv;
1993 wrs = GNUNET_NETWORK_fdset_create ();
1994 wes = GNUNET_NETWORK_fdset_create ();
1995 wws = GNUNET_NETWORK_fdset_create ();
1997 GNUNET_assert (MHD_YES == MHD_get_fdset (hd->daemon, &rs, &ws, &es, &max));
2000 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2001 "MHD fds: max=%d\n", max);
2003 haveto = MHD_get_timeout (hd->daemon, &timeout);
2005 if (MHD_YES == haveto)
2006 tv.rel_value = (uint64_t) timeout;
2008 tv = GNUNET_TIME_UNIT_FOREVER_REL;
2009 GNUNET_NETWORK_fdset_copy_native (wrs, &rs, max + 1);
2010 GNUNET_NETWORK_fdset_copy_native (wws, &ws, max + 1);
2011 GNUNET_NETWORK_fdset_copy_native (wes, &es, max + 1);
2013 if (GNUNET_SCHEDULER_NO_TASK != hd->httpd_task)
2014 GNUNET_SCHEDULER_cancel (hd->httpd_task);
2016 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_HIGH,
2019 GNUNET_NETWORK_fdset_destroy (wrs);
2020 GNUNET_NETWORK_fdset_destroy (wws);
2021 GNUNET_NETWORK_fdset_destroy (wes);
2026 * Task run whenever HTTP server operations are pending.
2029 * @param tc sched context
2032 do_httpd (void *cls,
2033 const struct GNUNET_SCHEDULER_TaskContext *tc)
2035 struct MhdHttpList *hd = cls;
2037 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2038 "MHD: Main loop\n");
2039 hd->httpd_task = GNUNET_SCHEDULER_NO_TASK;
2040 MHD_run (hd->daemon);
2047 * Read data from socket
2049 * @param cls the closure
2050 * @param tc scheduler context
2053 do_read (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
2056 * Read from remote end
2058 * @param cls closure
2059 * @param tc scheduler context
2062 do_read_remote (void* cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
2065 * Write data to remote socket
2067 * @param cls the closure
2068 * @param tc scheduler context
2071 do_write_remote (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2073 struct Socks5Request *s5r = cls;
2076 s5r->fwdwtask = GNUNET_SCHEDULER_NO_TASK;
2078 if ((NULL != tc->read_ready) &&
2079 (GNUNET_NETWORK_fdset_isset (tc->write_ready, s5r->remote_sock)) &&
2080 ((len = GNUNET_NETWORK_socket_send (s5r->remote_sock, s5r->rbuf,
2083 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2084 "Successfully sent %d bytes to remote socket\n",
2089 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "write remote");
2090 if (GNUNET_SCHEDULER_NO_TASK != s5r->rtask)
2091 GNUNET_SCHEDULER_cancel (s5r->rtask);
2092 if (GNUNET_SCHEDULER_NO_TASK != s5r->wtask)
2093 GNUNET_SCHEDULER_cancel (s5r->wtask);
2094 if (GNUNET_SCHEDULER_NO_TASK != s5r->fwdrtask)
2095 GNUNET_SCHEDULER_cancel (s5r->fwdrtask);
2096 GNUNET_NETWORK_socket_close (s5r->remote_sock);
2097 GNUNET_NETWORK_socket_close (s5r->sock);
2103 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
2110 * Clean up s5r handles
2112 * @param s5r the handle to destroy
2115 cleanup_s5r (struct Socks5Request *s5r)
2117 if (GNUNET_SCHEDULER_NO_TASK != s5r->rtask)
2118 GNUNET_SCHEDULER_cancel (s5r->rtask);
2119 if (GNUNET_SCHEDULER_NO_TASK != s5r->fwdwtask)
2120 GNUNET_SCHEDULER_cancel (s5r->fwdwtask);
2121 if (GNUNET_SCHEDULER_NO_TASK != s5r->fwdrtask)
2122 GNUNET_SCHEDULER_cancel (s5r->fwdrtask);
2124 if (NULL != s5r->remote_sock)
2125 GNUNET_NETWORK_socket_close (s5r->remote_sock);
2126 if ((NULL != s5r->sock) && (s5r->cleanup_sock == GNUNET_YES))
2127 GNUNET_NETWORK_socket_close (s5r->sock);
2133 * Write data to socket
2135 * @param cls the closure
2136 * @param tc scheduler context
2139 do_write (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2141 struct Socks5Request *s5r = cls;
2144 s5r->wtask = GNUNET_SCHEDULER_NO_TASK;
2146 if ((NULL != tc->read_ready) &&
2147 (GNUNET_NETWORK_fdset_isset (tc->write_ready, s5r->sock)) &&
2148 ((len = GNUNET_NETWORK_socket_send (s5r->sock, s5r->wbuf,
2151 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2152 "Successfully sent %d bytes to socket\n",
2158 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "write");
2159 s5r->cleanup = GNUNET_YES;
2160 s5r->cleanup_sock = GNUNET_YES;
2166 if (GNUNET_YES == s5r->cleanup)
2172 if ((s5r->state == SOCKS5_DATA_TRANSFER) &&
2173 (s5r->fwdrtask == GNUNET_SCHEDULER_NO_TASK))
2175 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
2177 &do_read_remote, s5r);
2181 * Read from remote end
2183 * @param cls closure
2184 * @param tc scheduler context
2187 do_read_remote (void* cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2189 struct Socks5Request *s5r = cls;
2191 s5r->fwdrtask = GNUNET_SCHEDULER_NO_TASK;
2194 if ((NULL != tc->write_ready) &&
2195 (GNUNET_NETWORK_fdset_isset (tc->read_ready, s5r->remote_sock)) &&
2196 (s5r->wbuf_len = GNUNET_NETWORK_socket_recv (s5r->remote_sock, s5r->wbuf,
2197 sizeof (s5r->wbuf))))
2199 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2200 "Successfully read %d bytes from remote socket\n",
2205 if (s5r->wbuf_len == 0)
2206 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2207 "0 bytes received from remote... graceful shutdown!\n");
2208 if (s5r->fwdwtask != GNUNET_SCHEDULER_NO_TASK)
2209 GNUNET_SCHEDULER_cancel (s5r->fwdwtask);
2210 if (s5r->rtask != GNUNET_SCHEDULER_NO_TASK)
2211 GNUNET_SCHEDULER_cancel (s5r->rtask);
2213 GNUNET_NETWORK_socket_close (s5r->remote_sock);
2214 s5r->remote_sock = NULL;
2215 GNUNET_NETWORK_socket_close (s5r->sock);
2221 s5r->wtask = GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
2229 * Adds a socket to MHD
2231 * @param h the handle to the socket to add
2232 * @param daemon the daemon to add the fd to
2233 * @return whatever MHD_add_connection returns
2236 add_handle_to_mhd (struct GNUNET_NETWORK_Handle *h, struct MHD_Daemon *daemon)
2239 struct sockaddr *addr;
2242 fd = dup (GNUNET_NETWORK_get_fd (h));
2243 addr = GNUNET_NETWORK_get_addr (h);
2244 len = GNUNET_NETWORK_get_addrlen (h);
2246 return MHD_add_connection (daemon, fd, addr, len);
2250 * Read file in filename
2252 * @param filename file to read
2253 * @param size pointer where filesize is stored
2257 load_file (const char* filename,
2264 GNUNET_DISK_file_size (filename, &fsize,
2265 GNUNET_YES, GNUNET_YES))
2267 if (fsize > MAX_PEM_SIZE)
2269 *size = (unsigned int) fsize;
2270 buffer = GNUNET_malloc (*size);
2271 if (fsize != GNUNET_DISK_fn_read (filename, buffer, (size_t) fsize))
2273 GNUNET_free (buffer);
2281 * Load PEM key from file
2283 * @param key where to store the data
2284 * @param keyfile path to the PEM file
2285 * @return GNUNET_OK on success
2288 load_key_from_file (gnutls_x509_privkey_t key, const char* keyfile)
2290 gnutls_datum_t key_data;
2293 key_data.data = (unsigned char*) load_file (keyfile, &key_data.size);
2294 ret = gnutls_x509_privkey_import (key, &key_data,
2295 GNUTLS_X509_FMT_PEM);
2296 if (GNUTLS_E_SUCCESS != ret)
2298 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2299 _("Unable to import private key from file `%s'\n"),
2303 GNUNET_free (key_data.data);
2304 return (GNUTLS_E_SUCCESS != ret) ? GNUNET_SYSERR : GNUNET_OK;
2309 * Load cert from file
2311 * @param crt struct to store data in
2312 * @param certfile path to pem file
2313 * @return GNUNET_OK on success
2316 load_cert_from_file (gnutls_x509_crt_t crt, char* certfile)
2318 gnutls_datum_t cert_data;
2319 cert_data.data = NULL;
2322 cert_data.data = (unsigned char*) load_file (certfile, &cert_data.size);
2323 ret = gnutls_x509_crt_import (crt, &cert_data,
2324 GNUTLS_X509_FMT_PEM);
2325 if (GNUTLS_E_SUCCESS != ret)
2327 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2328 _("Unable to import certificate %s\n"), certfile);
2331 GNUNET_free (cert_data.data);
2332 return (GNUTLS_E_SUCCESS != ret) ? GNUNET_SYSERR : GNUNET_OK;
2337 * Generate new certificate for specific name
2339 * @param name the subject name to generate a cert for
2340 * @return a struct holding the PEM data
2342 static struct ProxyGNSCertificate *
2343 generate_gns_certificate (const char *name)
2347 unsigned int serial;
2348 size_t key_buf_size;
2349 size_t cert_buf_size;
2350 gnutls_x509_crt_t request;
2354 ret = gnutls_x509_crt_init (&request);
2356 if (GNUTLS_E_SUCCESS != ret)
2361 ret = gnutls_x509_crt_set_key (request, proxy_ca.key);
2363 if (GNUTLS_E_SUCCESS != ret)
2368 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Generating cert\n");
2370 struct ProxyGNSCertificate *pgc =
2371 GNUNET_malloc (sizeof (struct ProxyGNSCertificate));
2373 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Adding DNs\n");
2375 gnutls_x509_crt_set_dn_by_oid (request, GNUTLS_OID_X520_COUNTRY_NAME,
2378 gnutls_x509_crt_set_dn_by_oid (request, GNUTLS_OID_X520_ORGANIZATION_NAME,
2381 gnutls_x509_crt_set_dn_by_oid (request, GNUTLS_OID_X520_COMMON_NAME,
2382 0, name, strlen (name));
2384 ret = gnutls_x509_crt_set_version (request, 3);
2386 ret = gnutls_rnd (GNUTLS_RND_NONCE, &serial, sizeof (serial));
2388 etime = time (NULL);
2389 tm_data = localtime (&etime);
2392 ret = gnutls_x509_crt_set_serial (request,
2396 ret = gnutls_x509_crt_set_activation_time (request,
2399 etime = mktime (tm_data);
2406 ret = gnutls_x509_crt_set_expiration_time (request,
2408 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Signing...\n");
2410 ret = gnutls_x509_crt_sign (request, proxy_ca.cert, proxy_ca.key);
2412 key_buf_size = sizeof (pgc->key);
2413 cert_buf_size = sizeof (pgc->cert);
2415 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Exporting certificate...\n");
2417 gnutls_x509_crt_export (request, GNUTLS_X509_FMT_PEM,
2418 pgc->cert, &cert_buf_size);
2420 gnutls_x509_privkey_export (proxy_ca.key, GNUTLS_X509_FMT_PEM,
2421 pgc->key, &key_buf_size);
2424 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Cleaning up\n");
2425 gnutls_x509_crt_deinit (request);
2433 * Accept policy for mhdaemons
2436 * @param addr the sockaddr
2437 * @param addrlen the sockaddr length
2438 * @return MHD_NO if sockaddr is wrong or #conns too high
2441 accept_cb (void* cls, const struct sockaddr *addr, socklen_t addrlen)
2443 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2444 "In MHD accept policy cb\n");
2448 if (addr->sa_family == AF_UNIX)
2452 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2453 "Connection accepted\n");
2460 * Adds a socket to an SSL MHD instance
2461 * It is important the the domain name is
2462 * correct. In most cases we need to start a new daemon
2464 * @param h the handle to add to a daemon
2465 * @param domain the domain the ssl daemon has to serve
2466 * @return MHD_YES on success
2469 add_handle_to_ssl_mhd (struct GNUNET_NETWORK_Handle *h, const char* domain)
2471 struct MhdHttpList *hd = NULL;
2472 struct ProxyGNSCertificate *pgc;
2473 struct NetworkHandleList *nh;
2475 for (hd = mhd_httpd_head; NULL != hd; hd = hd->next)
2476 if (0 == strcmp (hd->domain, domain))
2481 pgc = generate_gns_certificate (domain);
2483 hd = GNUNET_malloc (sizeof (struct MhdHttpList));
2484 hd->is_ssl = GNUNET_YES;
2485 strcpy (hd->domain, domain);
2486 hd->proxy_cert = pgc;
2489 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2490 "No previous SSL instance found... starting new one for %s\n",
2492 hd->daemon = MHD_start_daemon (MHD_USE_DEBUG
2494 #if HAVE_MHD_NO_LISTEN_SOCKET
2495 | MHD_USE_NO_LISTEN_SOCKET,
2501 &create_response, hd,
2502 #if !HAVE_MHD_NO_LISTEN_SOCKET
2503 MHD_OPTION_LISTEN_SOCKET, GNUNET_NETWORK_get_fd (mhd_unix_socket),
2505 MHD_OPTION_CONNECTION_LIMIT,
2506 MHD_MAX_CONNECTIONS,
2507 MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) 16,
2508 MHD_OPTION_NOTIFY_COMPLETED, NULL, NULL,
2509 MHD_OPTION_HTTPS_MEM_KEY, pgc->key,
2510 MHD_OPTION_HTTPS_MEM_CERT, pgc->cert,
2511 MHD_OPTION_URI_LOG_CALLBACK, &mhd_log_callback,
2515 GNUNET_assert (hd->daemon != NULL);
2516 hd->httpd_task = GNUNET_SCHEDULER_NO_TASK;
2518 GNUNET_CONTAINER_DLL_insert (mhd_httpd_head, mhd_httpd_tail, hd);
2521 nh = GNUNET_malloc (sizeof (struct NetworkHandleList));
2524 GNUNET_CONTAINER_DLL_insert (hd->socket_handles_head,
2525 hd->socket_handles_tail,
2528 return add_handle_to_mhd (h, hd->daemon);
2534 * Read data from incoming connection
2536 * @param cls the closure
2537 * @param tc the scheduler context
2540 do_read (void* cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2542 struct Socks5Request *s5r = cls;
2543 struct socks5_client_hello *c_hello;
2544 struct socks5_server_hello *s_hello;
2545 struct socks5_client_request *c_req;
2546 struct socks5_server_response *s_resp;
2552 struct hostent *phost;
2554 struct sockaddr_in remote_addr;
2555 struct in_addr *r_sin_addr;
2557 struct NetworkHandleList *nh;
2559 s5r->rtask = GNUNET_SCHEDULER_NO_TASK;
2561 if ((NULL != tc->write_ready) &&
2562 (GNUNET_NETWORK_fdset_isset (tc->read_ready, s5r->sock)) &&
2563 (s5r->rbuf_len = GNUNET_NETWORK_socket_recv (s5r->sock, s5r->rbuf,
2564 sizeof (s5r->rbuf))))
2566 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2567 "Successfully read %d bytes from socket\n",
2572 if (s5r->rbuf_len != 0)
2573 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "read");
2575 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client disco!\n");
2577 if (s5r->fwdrtask != GNUNET_SCHEDULER_NO_TASK)
2578 GNUNET_SCHEDULER_cancel (s5r->fwdrtask);
2579 if (s5r->wtask != GNUNET_SCHEDULER_NO_TASK)
2580 GNUNET_SCHEDULER_cancel (s5r->wtask);
2581 if (s5r->fwdwtask != GNUNET_SCHEDULER_NO_TASK)
2582 GNUNET_SCHEDULER_cancel (s5r->fwdwtask);
2583 GNUNET_NETWORK_socket_close (s5r->remote_sock);
2584 GNUNET_NETWORK_socket_close (s5r->sock);
2589 if (s5r->state == SOCKS5_INIT)
2591 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2593 c_hello = (struct socks5_client_hello*)&s5r->rbuf;
2595 GNUNET_assert (c_hello->version == SOCKS_VERSION_5);
2597 s_hello = (struct socks5_server_hello*)&s5r->wbuf;
2598 s5r->wbuf_len = sizeof( struct socks5_server_hello );
2600 s_hello->version = c_hello->version;
2601 s_hello->auth_method = SOCKS_AUTH_NONE;
2603 /* Write response to client */
2604 s5r->wtask = GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
2608 s5r->rtask = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
2612 s5r->state = SOCKS5_REQUEST;
2616 if (s5r->state == SOCKS5_REQUEST)
2618 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2619 "Processing SOCKS5 request\n");
2620 c_req = (struct socks5_client_request*)&s5r->rbuf;
2621 s_resp = (struct socks5_server_response*)&s5r->wbuf;
2622 //Only 10byte for ipv4 response!
2623 s5r->wbuf_len = 10;//sizeof (struct socks5_server_response);
2625 GNUNET_assert (c_req->addr_type == 3);
2627 dom_len = *((uint8_t*)(&(c_req->addr_type) + 1));
2628 memset(domain, 0, sizeof(domain));
2629 strncpy(domain, (char*)(&(c_req->addr_type) + 2), dom_len);
2630 req_port = *((uint16_t*)(&(c_req->addr_type) + 2 + dom_len));
2632 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2633 "Requested connection is %s:%d\n",
2637 if (is_tld (domain, GNUNET_GNS_TLD) ||
2638 is_tld (domain, GNUNET_GNS_TLD_ZKEY))
2640 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2641 "Requested connection is gnunet tld\n",
2645 if (ntohs(req_port) == HTTPS_PORT)
2647 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2648 "Requested connection is HTTPS\n");
2649 ret = add_handle_to_ssl_mhd ( s5r->sock, domain );
2651 else if (NULL != httpd)
2653 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2654 "Requested connection is HTTP\n");
2655 nh = GNUNET_malloc (sizeof (struct NetworkHandleList));
2658 GNUNET_CONTAINER_DLL_insert (mhd_httpd_head->socket_handles_head,
2659 mhd_httpd_head->socket_handles_tail,
2662 ret = add_handle_to_mhd ( s5r->sock, httpd );
2667 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2668 _("Failed to start HTTP server\n"));
2669 s_resp->version = 0x05;
2670 s_resp->reply = 0x01;
2671 s5r->cleanup = GNUNET_YES;
2672 s5r->cleanup_sock = GNUNET_YES;
2674 GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
2680 /* Signal success */
2681 s_resp->version = 0x05;
2682 s_resp->reply = 0x00;
2683 s_resp->reserved = 0x00;
2684 s_resp->addr_type = 0x01;
2686 s5r->cleanup = GNUNET_YES;
2687 s5r->cleanup_sock = GNUNET_NO;
2689 GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
2697 phost = (struct hostent*)gethostbyname (domain);
2700 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2701 "Resolve %s error!\n", domain );
2702 s_resp->version = 0x05;
2703 s_resp->reply = 0x01;
2704 s5r->cleanup = GNUNET_YES;
2705 s5r->cleanup_sock = GNUNET_YES;
2707 GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
2713 s5r->remote_sock = GNUNET_NETWORK_socket_create (AF_INET,
2716 r_sin_addr = (struct in_addr*)(phost->h_addr);
2717 remote_ip = r_sin_addr->s_addr;
2718 memset(&remote_addr, 0, sizeof(remote_addr));
2719 remote_addr.sin_family = AF_INET;
2720 #if HAVE_SOCKADDR_IN_SIN_LEN
2721 remote_addr.sin_len = sizeof (remote_addr);
2723 remote_addr.sin_addr.s_addr = remote_ip;
2724 remote_addr.sin_port = req_port;
2726 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2727 "target server: %s:%u\n", inet_ntoa(remote_addr.sin_addr),
2731 GNUNET_NETWORK_socket_connect ( s5r->remote_sock,
2732 (const struct sockaddr*)&remote_addr,
2733 sizeof (remote_addr)))
2734 && (errno != EINPROGRESS))
2736 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "connect");
2737 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2738 "socket request error...\n");
2739 s_resp->version = 0x05;
2740 s_resp->reply = 0x01;
2742 GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
2749 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2750 "new remote connection\n");
2752 s_resp->version = 0x05;
2753 s_resp->reply = 0x00;
2754 s_resp->reserved = 0x00;
2755 s_resp->addr_type = 0x01;
2757 s5r->state = SOCKS5_DATA_TRANSFER;
2760 GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
2764 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
2772 if (s5r->state == SOCKS5_DATA_TRANSFER)
2774 if ((s5r->remote_sock == NULL) || (s5r->rbuf_len == 0))
2776 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2777 "Closing connection to client\n");
2778 if (s5r->rtask != GNUNET_SCHEDULER_NO_TASK)
2779 GNUNET_SCHEDULER_cancel (s5r->rtask);
2780 if (s5r->fwdwtask != GNUNET_SCHEDULER_NO_TASK)
2781 GNUNET_SCHEDULER_cancel (s5r->fwdwtask);
2782 if (s5r->fwdrtask != GNUNET_SCHEDULER_NO_TASK)
2783 GNUNET_SCHEDULER_cancel (s5r->fwdrtask);
2784 if (s5r->fwdrtask != GNUNET_SCHEDULER_NO_TASK)
2785 GNUNET_SCHEDULER_cancel (s5r->fwdrtask);
2787 if (s5r->remote_sock != NULL)
2788 GNUNET_NETWORK_socket_close (s5r->remote_sock);
2789 GNUNET_NETWORK_socket_close (s5r->sock);
2794 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2795 "forwarding %d bytes from client\n", s5r->rbuf_len);
2798 GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
2800 &do_write_remote, s5r);
2802 if (s5r->fwdrtask == GNUNET_SCHEDULER_NO_TASK)
2805 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
2807 &do_read_remote, s5r);
2817 * Accept new incoming connections
2819 * @param cls the closure
2820 * @param tc the scheduler context
2823 do_accept (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2825 struct GNUNET_NETWORK_Handle *s;
2826 struct Socks5Request *s5r;
2828 ltask = GNUNET_SCHEDULER_NO_TASK;
2829 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
2832 ltask = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
2836 s = GNUNET_NETWORK_socket_accept (lsock, NULL, NULL);
2840 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "accept");
2844 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2845 "Got an inbound connection, waiting for data\n");
2847 s5r = GNUNET_malloc (sizeof (struct Socks5Request));
2849 s5r->state = SOCKS5_INIT;
2850 s5r->wtask = GNUNET_SCHEDULER_NO_TASK;
2851 s5r->fwdwtask = GNUNET_SCHEDULER_NO_TASK;
2852 s5r->fwdrtask = GNUNET_SCHEDULER_NO_TASK;
2853 s5r->rtask = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
2860 * Task run on shutdown
2862 * @param cls closure
2863 * @param tc task context
2866 do_shutdown (void *cls,
2867 const struct GNUNET_SCHEDULER_TaskContext *tc)
2870 struct MhdHttpList *hd;
2871 struct MhdHttpList *tmp_hd;
2872 struct NetworkHandleList *nh;
2873 struct NetworkHandleList *tmp_nh;
2874 struct ProxyCurlTask *ctask;
2875 struct ProxyCurlTask *ctask_tmp;
2877 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2878 "Shutting down...\n");
2880 gnutls_global_deinit ();
2882 if (NULL != local_gns_zone)
2883 GNUNET_free (local_gns_zone);
2884 if (NULL != local_private_zone)
2885 GNUNET_free (local_private_zone);
2886 if (NULL != local_shorten_zone)
2887 GNUNET_free (local_shorten_zone);
2889 if (GNUNET_SCHEDULER_NO_TASK != curl_download_task)
2891 GNUNET_SCHEDULER_cancel (curl_download_task);
2892 curl_download_task = GNUNET_SCHEDULER_NO_TASK;
2895 for (hd = mhd_httpd_head; hd != NULL; hd = tmp_hd)
2899 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2900 "Stopping daemon\n");
2902 if (GNUNET_SCHEDULER_NO_TASK != hd->httpd_task)
2904 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2905 "Stopping select task %d\n",
2907 GNUNET_SCHEDULER_cancel (hd->httpd_task);
2908 hd->httpd_task = GNUNET_SCHEDULER_NO_TASK;
2911 if (NULL != hd->daemon)
2913 MHD_stop_daemon (hd->daemon);
2917 for (nh = hd->socket_handles_head; nh != NULL; nh = tmp_nh)
2921 GNUNET_NETWORK_socket_close (nh->h);
2926 if (NULL != hd->proxy_cert)
2928 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2929 "Free certificate\n");
2930 GNUNET_free (hd->proxy_cert);
2936 for (ctask=ctasks_head; ctask != NULL; ctask=ctask_tmp)
2938 ctask_tmp = ctask->next;
2940 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2941 "Cleaning up cURL task\n");
2943 if (ctask->curl != NULL)
2944 curl_easy_cleanup (ctask->curl);
2946 if (NULL != ctask->headers)
2947 curl_slist_free_all (ctask->headers);
2948 if (NULL != ctask->resolver)
2949 curl_slist_free_all (ctask->resolver);
2951 if (NULL != ctask->response)
2952 MHD_destroy_response (ctask->response);
2955 GNUNET_free (ctask);
2957 curl_multi_cleanup (curl_multi);
2959 GNUNET_GNS_disconnect (gns_handle);
2964 * Compiles a regex for us
2966 * @param re ptr to re struct
2967 * @param rt the expression to compile
2968 * @return 0 on success
2971 compile_regex (regex_t *re, const char* rt)
2976 status = regcomp (re, rt, REG_EXTENDED|REG_NEWLINE);
2979 regerror (status, re, err, 1024);
2980 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2981 "Regex error compiling '%s': %s\n", rt, err);
2989 * Loads the users local zone key
2991 * @return GNUNET_YES on success
2994 load_local_zone_key (const struct GNUNET_CONFIGURATION_Handle *cfg)
2997 struct GNUNET_CRYPTO_RsaPrivateKey *key = NULL;
2998 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pkey;
2999 struct GNUNET_CRYPTO_ShortHashCode *zone = NULL;
3000 struct GNUNET_CRYPTO_ShortHashAsciiEncoded zonename;
3002 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "gns",
3003 "ZONEKEY", &keyfile))
3005 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3006 "Unable to load zone key config value!\n");
3010 if (GNUNET_NO == GNUNET_DISK_file_test (keyfile))
3012 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3013 "Unable to load zone key %s!\n", keyfile);
3014 GNUNET_free(keyfile);
3018 key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
3019 GNUNET_CRYPTO_rsa_key_get_public (key, &pkey);
3020 local_gns_zone = GNUNET_malloc (sizeof (struct GNUNET_CRYPTO_ShortHashCode));
3021 GNUNET_CRYPTO_short_hash(&pkey,
3022 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
3024 zone = local_gns_zone;
3025 GNUNET_CRYPTO_short_hash_to_enc (zone, &zonename);
3026 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3027 "Using zone: %s!\n", &zonename);
3028 GNUNET_CRYPTO_rsa_key_free(key);
3029 GNUNET_free(keyfile);
3032 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "gns",
3033 "PRIVATE_ZONEKEY", &keyfile))
3035 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3036 "Unable to load private zone key config value!\n");
3039 if ((NULL != keyfile) && (GNUNET_NO == GNUNET_DISK_file_test (keyfile)))
3041 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3042 "Unable to load private zone key %s!\n", keyfile);
3043 GNUNET_free(keyfile);
3047 key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
3048 GNUNET_CRYPTO_rsa_key_get_public (key, &pkey);
3049 local_private_zone = GNUNET_malloc (sizeof (struct GNUNET_CRYPTO_ShortHashCode));
3050 GNUNET_CRYPTO_short_hash(&pkey,
3051 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
3052 local_private_zone);
3053 GNUNET_CRYPTO_short_hash_to_enc (zone, &zonename);
3054 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3055 "Using private zone: %s!\n", &zonename);
3056 GNUNET_CRYPTO_rsa_key_free(key);
3057 GNUNET_free(keyfile);
3061 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "gns",
3062 "SHORTEN_ZONEKEY", &keyfile))
3064 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3065 "Unable to load shorten zone key config value!\n");
3068 if ((NULL != keyfile) && (GNUNET_NO == GNUNET_DISK_file_test (keyfile)))
3070 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3071 "Unable to load shorten zone key %s!\n", keyfile);
3072 GNUNET_free(keyfile);
3076 key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
3077 GNUNET_CRYPTO_rsa_key_get_public (key, &pkey);
3078 local_shorten_zone = GNUNET_malloc (sizeof (struct GNUNET_CRYPTO_ShortHashCode));
3079 GNUNET_CRYPTO_short_hash(&pkey,
3080 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
3081 local_shorten_zone);
3082 GNUNET_CRYPTO_short_hash_to_enc (zone, &zonename);
3083 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3084 "Using shorten zone: %s!\n", &zonename);
3085 GNUNET_CRYPTO_rsa_key_free(key);
3086 GNUNET_free(keyfile);
3093 * Main function that will be run
3095 * @param cls closure
3096 * @param args remaining command-line arguments
3097 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
3098 * @param cfg configuration
3101 run (void *cls, char *const *args, const char *cfgfile,
3102 const struct GNUNET_CONFIGURATION_Handle *cfg)
3104 struct sockaddr_in sa;
3105 struct MhdHttpList *hd;
3106 char* cafile_cfg = NULL;
3108 #if !HAVE_MHD_NO_LISTEN_SOCKET
3110 char* proxy_sockfile;
3111 struct sockaddr_un mhd_unix_sock_addr;
3114 curl_multi = curl_multi_init ();
3116 if (NULL == curl_multi)
3118 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3119 "Failed to create cURL multo handle!\n");
3123 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3125 cafile = cafile_opt;
3128 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "gns-proxy",
3132 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3133 "Unable to load proxy CA config value!\n");
3134 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3135 "No proxy CA provided!\n");
3138 cafile = cafile_cfg;
3140 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3141 "Using %s as CA\n", cafile);
3143 gnutls_global_init ();
3144 gnutls_x509_crt_init (&proxy_ca.cert);
3145 gnutls_x509_privkey_init (&proxy_ca.key);
3147 if ( (GNUNET_OK != load_cert_from_file (proxy_ca.cert, cafile)) ||
3148 (GNUNET_OK != load_key_from_file (proxy_ca.key, cafile)) )
3150 // FIXME: release resources...
3154 GNUNET_free_non_null (cafile_cfg);
3156 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3157 "Loading Template\n");
3159 compile_regex (&re_dotplus, (char*) RE_A_HREF);
3161 gns_handle = GNUNET_GNS_connect (cfg);
3163 if (GNUNET_NO == load_local_zone_key (cfg))
3165 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3166 "Unable to load zone!\n");
3170 if (NULL == gns_handle)
3172 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3173 "Unable to connect to GNS!\n");
3177 memset (&sa, 0, sizeof (sa));
3178 sa.sin_family = AF_INET;
3179 sa.sin_port = htons (port);
3180 #if HAVE_SOCKADDR_IN_SIN_LEN
3181 sa.sin_len = sizeof (sa);
3184 lsock = GNUNET_NETWORK_socket_create (AF_INET,
3188 if ((NULL == lsock) ||
3190 GNUNET_NETWORK_socket_bind (lsock, (const struct sockaddr *) &sa,
3193 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3194 "Failed to create listen socket bound to `%s'",
3195 GNUNET_a2s ((const struct sockaddr *) &sa, sizeof (sa)));
3197 GNUNET_NETWORK_socket_close (lsock);
3201 if (GNUNET_OK != GNUNET_NETWORK_socket_listen (lsock, 5))
3203 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3204 "Failed to listen on socket bound to `%s'",
3205 GNUNET_a2s ((const struct sockaddr *) &sa, sizeof (sa)));
3209 ltask = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
3210 lsock, &do_accept, NULL);
3215 if (0 != curl_global_init (CURL_GLOBAL_WIN32))
3217 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3218 "cURL global init failed!\n");
3222 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3223 "Proxy listens on port %u\n",
3226 mhd_httpd_head = NULL;
3227 mhd_httpd_tail = NULL;
3228 total_mhd_connections = 0;
3229 #ifndef HAVE_MHD_NO_LISTEN_SOCKET
3230 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "gns-proxy",
3234 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3235 "Specify PROXY_UNIXPATH in gns-proxy config section!\n");
3239 mhd_unix_socket = GNUNET_NETWORK_socket_create (AF_UNIX,
3243 if (NULL == mhd_unix_socket)
3245 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3246 "Unable to create unix domain socket!\n");
3250 mhd_unix_sock_addr.sun_family = AF_UNIX;
3251 strcpy (mhd_unix_sock_addr.sun_path, proxy_sockfile);
3254 mhd_unix_sock_addr.sun_path[0] = '\0';
3256 #if HAVE_SOCKADDR_IN_SIN_LEN
3257 mhd_unix_sock_addr.sun_len = (u_char) sizeof (struct sockaddr_un);
3260 len = strlen (proxy_sockfile) + sizeof(AF_UNIX);
3262 GNUNET_free (proxy_sockfile);
3264 if (GNUNET_OK != GNUNET_NETWORK_socket_bind (mhd_unix_socket,
3265 (struct sockaddr*)&mhd_unix_sock_addr,
3268 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3269 "Unable to bind unix domain socket!\n");
3273 if (GNUNET_OK != GNUNET_NETWORK_socket_listen (mhd_unix_socket,
3276 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3277 "Unable to listen on unix domain socket!\n");
3282 hd = GNUNET_malloc (sizeof (struct MhdHttpList));
3283 hd->is_ssl = GNUNET_NO;
3284 strcpy (hd->domain, "");
3285 httpd = MHD_start_daemon (MHD_USE_DEBUG
3286 #if HAVE_MHD_NO_LISTEN_SOCKET
3287 | MHD_USE_NO_LISTEN_SOCKET,
3290 , 4444, //Dummy port
3293 &create_response, hd,
3294 #if !HAVE_MHD_NO_LISTEN_SOCKET
3295 MHD_OPTION_LISTEN_SOCKET, GNUNET_NETWORK_get_fd (mhd_unix_socket),
3297 MHD_OPTION_CONNECTION_LIMIT, MHD_MAX_CONNECTIONS,
3298 MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) 16,
3299 MHD_OPTION_NOTIFY_COMPLETED,
3301 MHD_OPTION_URI_LOG_CALLBACK, &mhd_log_callback, NULL,
3304 GNUNET_assert (httpd != NULL);
3306 hd->httpd_task = GNUNET_SCHEDULER_NO_TASK;
3308 GNUNET_CONTAINER_DLL_insert (mhd_httpd_head, mhd_httpd_tail, hd);
3312 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
3313 &do_shutdown, NULL);
3319 * The main function for gnunet-gns-proxy.
3321 * @param argc number of arguments from the command line
3322 * @param argv command line arguments
3323 * @return 0 ok, 1 on error
3326 main (int argc, char *const *argv)
3328 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
3330 gettext_noop ("listen on specified port"), 1,
3331 &GNUNET_GETOPT_set_string, &port},
3332 {'a', "authority", NULL,
3333 gettext_noop ("pem file to use as CA"), 1,
3334 &GNUNET_GETOPT_set_string, &cafile_opt},
3335 GNUNET_GETOPT_OPTION_END
3340 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
3344 GNUNET_log_setup ("gnunet-gns-proxy", "WARNING", NULL);
3347 GNUNET_PROGRAM_run (argc, argv, "gnunet-gns-proxy",
3348 _("GNUnet GNS proxy"),
3350 &run, NULL)) ? 0 : 1;
3351 GNUNET_free_non_null ((char*)argv);