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 get_uri_val_iter (void *cls,
581 enum MHD_ValueKind kind,
587 if (strlen (buf) + strlen (value) + 3 > MAX_HTTP_URI_LENGTH)
589 sprintf (buf+strlen (buf), "?%s=%s", key, value);
595 * Read HTTP request header field 'Host'
597 * @param cls buffer to write to
598 * @param kind value kind
599 * @param key field key
600 * @param value field value
601 * @return MHD_NO when Host found
604 con_val_iter (void *cls,
605 enum MHD_ValueKind kind,
609 struct ProxyCurlTask *ctask = cls;
610 char* buf = ctask->host;
616 if (0 == strcmp ("Host", key))
618 port = strstr (value, ":");
621 strncpy (buf, value, port-value);
623 if ((1 != sscanf (port, "%u", &uport)) ||
624 (uport > UINT16_MAX) ||
626 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
627 "Unable to parse port!\n");
629 ctask->port = (uint16_t) uport;
636 if (0 == strcmp ("Accept-Encoding", key))
641 if (0 == strcmp (MHD_HTTP_HEADER_CONTENT_TYPE,
644 if (0 == strcmp (value,
645 MHD_HTTP_POST_ENCODING_FORM_URLENCODED))
646 ctask->post_type = MHD_HTTP_POST_ENCODING_FORM_URLENCODED;
647 else if (0 == strcmp (value,
648 MHD_HTTP_POST_ENCODING_MULTIPART_FORMDATA))
649 ctask->post_type = MHD_HTTP_POST_ENCODING_MULTIPART_FORMDATA;
651 ctask->post_type = NULL;
655 cstr = GNUNET_malloc (strlen (key) + strlen (hdr_val) + 3);
656 GNUNET_snprintf (cstr, strlen (key) + strlen (hdr_val) + 3,
657 "%s: %s", key, hdr_val);
659 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
660 "Client Header: %s\n", cstr);
662 ctask->headers = curl_slist_append (ctask->headers, cstr);
670 * Callback for MHD response
673 * @param pos in buffer
675 * @param max space in buffer
676 * @return number of bytes written
679 mhd_content_cb (void *cls,
685 * Check HTTP response header for mime
687 * @param buffer curl buffer
688 * @param size curl blocksize
689 * @param nmemb curl blocknumber
691 * @return size of read bytes
694 curl_check_hdr (void *buffer, size_t size, size_t nmemb, void *cls)
696 size_t bytes = size * nmemb;
697 struct ProxyCurlTask *ctask = cls;
698 int html_mime_len = strlen (HTML_HDR_CONTENT);
699 int cookie_hdr_len = strlen (MHD_HTTP_HEADER_SET_COOKIE);
700 char hdr_mime[html_mime_len+1];
701 char hdr_generic[bytes+1];
702 char new_cookie_hdr[bytes+strlen (ctask->leho)+1];
703 char new_location[MAX_HTTP_URI_LENGTH+500];
714 if (NULL == ctask->response)
716 /* FIXME: get total size from curl (if available) */
717 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
718 "Creating response for %s\n", ctask->url);
719 ctask->response = MHD_create_response_from_callback (MHD_SIZE_UNKNOWN,
720 sizeof (ctask->buffer),
724 ctask->ready_to_queue = GNUNET_YES;
728 if (html_mime_len <= bytes)
730 memcpy (hdr_mime, buffer, html_mime_len);
731 hdr_mime[html_mime_len] = '\0';
733 if (0 == strcmp (hdr_mime, HTML_HDR_CONTENT))
735 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
736 "Got HTML HTTP response header\n");
737 ctask->parse_content = GNUNET_YES;
741 if (cookie_hdr_len > bytes)
744 memcpy (hdr_generic, buffer, bytes);
745 hdr_generic[bytes] = '\0';
747 if ('\n' == hdr_generic[bytes-1])
748 hdr_generic[bytes-1] = '\0';
750 if (hdr_generic[bytes-2] == '\r')
751 hdr_generic[bytes-2] = '\0';
753 if (0 == memcmp (hdr_generic,
754 MHD_HTTP_HEADER_SET_COOKIE,
757 ndup = GNUNET_strdup (hdr_generic+cookie_hdr_len+1);
758 memset (new_cookie_hdr, 0, sizeof (new_cookie_hdr));
759 tok = strtok (ndup, ";");
761 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
762 "Looking for cookie in : %s\n", hdr_generic);
764 for (; tok != NULL; tok = strtok (NULL, ";"))
766 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
767 "Got Cookie token: %s\n", tok);
768 //memcpy (new_cookie_hdr+offset, tok, strlen (tok));
769 if (0 == memcmp (tok, " domain", strlen (" domain")))
771 cookie_domain = tok + strlen (" domain") + 1;
773 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
774 "Got Set-Cookie Domain: %s\n", cookie_domain);
776 if (strlen (cookie_domain) < strlen (ctask->leho))
778 delta_cdomain = strlen (ctask->leho) - strlen (cookie_domain);
779 if (0 == strcmp (cookie_domain, ctask->leho + (delta_cdomain)))
781 GNUNET_snprintf (new_cookie_hdr+offset,
782 sizeof (new_cookie_hdr),
783 " domain=%s", ctask->authority);
784 offset += strlen (" domain=") + strlen (ctask->authority);
785 new_cookie_hdr[offset] = ';';
790 else if (strlen (cookie_domain) == strlen (ctask->leho))
792 if (0 == strcmp (cookie_domain, ctask->leho))
794 GNUNET_snprintf (new_cookie_hdr+offset,
795 sizeof (new_cookie_hdr),
796 " domain=%s", ctask->host);
797 offset += strlen (" domain=") + strlen (ctask->host);
798 new_cookie_hdr[offset] = ';';
803 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
804 "Cookie domain invalid\n");
808 memcpy (new_cookie_hdr+offset, tok, strlen (tok));
809 offset += strlen (tok);
810 new_cookie_hdr[offset] = ';';
816 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
817 "Got Set-Cookie HTTP header %s\n", new_cookie_hdr);
819 if (GNUNET_NO == MHD_add_response_header (ctask->response,
820 MHD_HTTP_HEADER_SET_COOKIE,
823 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
824 "MHD: Error adding set-cookie header field %s\n",
825 hdr_generic+cookie_hdr_len+1);
830 ndup = GNUNET_strdup (hdr_generic);
831 hdr_type = strtok (ndup, ":");
833 if (NULL == hdr_type)
839 hdr_val = strtok (NULL, "");
849 if (0 == strcasecmp (MHD_HTTP_HEADER_LOCATION, hdr_type))
851 if (ctask->mhd->is_ssl)
853 sprintf (leho_host, "https://%s", ctask->leho);
854 sprintf (real_host, "https://%s", ctask->host);
858 sprintf (leho_host, "http://%s", ctask->leho);
859 sprintf (real_host, "http://%s", ctask->host);
862 if (0 == memcmp (leho_host, hdr_val, strlen (leho_host)))
864 sprintf (new_location, "%s%s", real_host, hdr_val+strlen (leho_host));
865 hdr_val = new_location;
869 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
870 "Trying to set %s: %s\n",
873 if (GNUNET_NO == MHD_add_response_header (ctask->response,
877 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
878 "MHD: Error adding %s header field %s\n",
889 * @param hd a http daemon list entry
892 run_httpd (struct MhdHttpList *hd);
903 * Task run whenever HTTP server operations are pending.
906 * @param tc sched context
910 const struct GNUNET_SCHEDULER_TaskContext *tc);
913 run_mhd_now (struct MhdHttpList *hd)
915 if (GNUNET_SCHEDULER_NO_TASK != hd->httpd_task)
917 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
918 "MHD: killing old task\n");
919 GNUNET_SCHEDULER_cancel (hd->httpd_task);
921 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
922 "MHD: Scheduling MHD now\n");
923 hd->httpd_task = GNUNET_SCHEDULER_add_now (&do_httpd, hd);
927 * Ask cURL for the select sets and schedule download
930 curl_download_prepare ();
933 * Callback to free content
935 * @param cls content to free
936 * @param tc task context
939 mhd_content_free (void *cls,
940 const struct GNUNET_SCHEDULER_TaskContext *tc)
942 struct ProxyCurlTask *ctask = cls;
943 GNUNET_assert (NULL == ctask->pp_match_head);
945 if (NULL != ctask->headers)
946 curl_slist_free_all (ctask->headers);
948 if (NULL != ctask->headers)
949 curl_slist_free_all (ctask->resolver);
951 if (NULL != ctask->response)
952 MHD_destroy_response (ctask->response);
954 if (NULL != ctask->post_handler)
955 MHD_destroy_post_processor (ctask->post_handler);
963 * Callback for MHD response
966 * @param pos in buffer
968 * @param max space in buffer
969 * @return number of bytes written
972 mhd_content_cb (void *cls,
977 struct ProxyCurlTask *ctask = cls;
978 struct ProxyREMatch *re_match = ctask->pp_match_head;
980 long long int bytes_to_copy = ctask->buffer_write_ptr - ctask->buffer_read_ptr;
982 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
983 "MHD: content cb for %s. To copy: %lld\n",
984 ctask->url, bytes_to_copy);
985 GNUNET_assert (bytes_to_copy >= 0);
987 if ((GNUNET_YES == ctask->download_is_finished) &&
988 (GNUNET_NO == ctask->download_error) &&
989 (0 == bytes_to_copy)) /* &&
990 (BUF_WAIT_FOR_CURL == ctask->buf_status))*/
992 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
993 "MHD: sending response for %s\n", ctask->url);
994 ctask->download_in_progress = GNUNET_NO;
995 run_mhd_now (ctask->mhd);
996 GNUNET_SCHEDULER_add_now (&mhd_content_free, ctask);
997 total_mhd_connections--;
998 return MHD_CONTENT_READER_END_OF_STREAM;
1001 if ((GNUNET_YES == ctask->download_error) &&
1002 (GNUNET_YES == ctask->download_is_finished) &&
1003 (0 == bytes_to_copy)) /* &&
1004 (BUF_WAIT_FOR_CURL == ctask->buf_status))*/
1006 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1007 "MHD: sending error response\n");
1008 ctask->download_in_progress = GNUNET_NO;
1009 run_mhd_now (ctask->mhd);
1010 GNUNET_SCHEDULER_add_now (&mhd_content_free, ctask);
1011 total_mhd_connections--;
1012 return MHD_CONTENT_READER_END_WITH_ERROR;
1015 if ( ctask->buf_status == BUF_WAIT_FOR_CURL )
1019 for (; NULL != re_match; re_match = ctask->pp_match_head)
1021 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1022 "MHD: Processing PP %s\n",
1023 re_match->hostname);
1024 bytes_to_copy = re_match->start - ctask->buffer_read_ptr;
1025 GNUNET_assert (bytes_to_copy >= 0);
1027 if (bytes_to_copy+copied > max)
1029 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1030 "MHD: buffer in response too small for %d. Using available space (%d). (%s)\n",
1034 memcpy (buf+copied, ctask->buffer_read_ptr, max-copied);
1035 ctask->buffer_read_ptr += max-copied;
1037 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1038 "MHD: copied %d bytes\n", copied);
1042 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1043 "MHD: copying %d bytes to mhd response at offset %d\n",
1044 bytes_to_copy, ctask->buffer_read_ptr);
1045 memcpy (buf+copied, ctask->buffer_read_ptr, bytes_to_copy);
1046 copied += bytes_to_copy;
1048 if (GNUNET_NO == re_match->done)
1050 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1051 "MHD: Waiting for PP of %s\n", re_match->hostname);
1052 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1053 "MHD: copied %d bytes\n", copied);
1054 ctask->buffer_read_ptr += bytes_to_copy;
1058 if (strlen (re_match->result) > (max - copied))
1060 //FIXME partially copy domain here
1061 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1062 "MHD: buffer in response too small for %s! (%s)\n",
1065 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1066 "MHD: copied %d bytes\n", copied);
1067 ctask->buffer_read_ptr += bytes_to_copy;
1071 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1072 "MHD: Adding PP result %s to buffer\n",
1074 memcpy (buf+copied, re_match->result, strlen (re_match->result));
1075 copied += strlen (re_match->result);
1076 ctask->buffer_read_ptr = re_match->end;
1077 GNUNET_CONTAINER_DLL_remove (ctask->pp_match_head,
1078 ctask->pp_match_tail,
1080 GNUNET_free (re_match);
1083 bytes_to_copy = ctask->buffer_write_ptr - ctask->buffer_read_ptr;
1085 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1086 "MHD: copied: %d left: %d, space left in buf: %d\n",
1088 bytes_to_copy, max-copied);
1090 GNUNET_assert (0 <= bytes_to_copy);
1092 if (GNUNET_NO == ctask->download_is_finished)
1094 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1095 "MHD: Purging buffer\n");
1096 memmove (ctask->buffer, ctask->buffer_read_ptr, bytes_to_copy);
1097 ctask->buffer_read_ptr = ctask->buffer;
1098 ctask->buffer_write_ptr = ctask->buffer + bytes_to_copy;
1099 ctask->buffer[bytes_to_copy] = '\0';
1102 if (bytes_to_copy+copied > max)
1103 bytes_to_copy = max-copied;
1105 if (0 > bytes_to_copy)
1108 memcpy (buf+copied, ctask->buffer_read_ptr, bytes_to_copy);
1109 ctask->buffer_read_ptr += bytes_to_copy;
1110 copied += bytes_to_copy;
1111 ctask->buf_status = BUF_WAIT_FOR_CURL;
1113 if (NULL != ctask->curl)
1114 curl_easy_pause (ctask->curl, CURLPAUSE_CONT);
1116 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1117 "MHD: copied %d bytes\n", copied);
1118 run_mhd_now (ctask->mhd);
1123 * Shorten result callback
1125 * @param cls the proxycurltask
1126 * @param short_name the shortened name (NULL on error)
1129 process_shorten (void* cls, const char* short_name)
1131 struct ProxyREMatch *re_match = cls;
1132 char result[sizeof (re_match->result)];
1134 if (NULL == short_name)
1136 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1137 "PP: Unable to shorten %s\n",
1138 re_match->hostname);
1139 GNUNET_CONTAINER_DLL_remove (re_match->ctask->pp_match_head,
1140 re_match->ctask->pp_match_tail,
1142 GNUNET_free (re_match);
1146 if (0 == strcmp (short_name, re_match->ctask->leho))
1147 strcpy (result, re_match->ctask->host);
1149 strcpy (result, short_name);
1151 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1152 "PP: Shorten %s -> %s\n",
1156 if (re_match->ctask->mhd->is_ssl)
1157 sprintf (re_match->result, "href=\"https://%s", result);
1159 sprintf (re_match->result, "href=\"http://%s", result);
1161 re_match->done = GNUNET_YES;
1162 run_mhd_now (re_match->ctask->mhd);
1167 * Postprocess data in buffer. From read ptr to write ptr
1169 * @param cls the curlproxytask
1170 * @param tc task context
1173 postprocess_buffer (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1175 struct ProxyCurlTask *ctask = cls;
1176 struct ProxyREMatch *re_match;
1177 char* re_ptr = ctask->buffer_read_ptr;
1178 char re_hostname[255];
1179 regmatch_t m[RE_N_MATCHES];
1181 ctask->pp_task = GNUNET_SCHEDULER_NO_TASK;
1183 if (GNUNET_YES != ctask->parse_content)
1185 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1186 "PP: Not parsing content\n");
1187 ctask->buf_status = BUF_WAIT_FOR_MHD;
1188 run_mhd_now (ctask->mhd);
1192 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1193 "PP: We need to parse the HTML\n");
1195 /* 0 means match found */
1196 while (0 == regexec (&re_dotplus, re_ptr, RE_N_MATCHES, m, 0))
1198 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1199 "PP: regex match\n");
1201 GNUNET_assert (m[1].rm_so != -1);
1203 memset (re_hostname, 0, sizeof (re_hostname));
1204 memcpy (re_hostname, re_ptr+m[1].rm_so, (m[3].rm_eo-m[1].rm_so));
1206 re_match = GNUNET_malloc (sizeof (struct ProxyREMatch));
1207 re_match->start = re_ptr + m[0].rm_so;
1208 re_match->end = re_ptr + m[3].rm_eo;
1209 re_match->done = GNUNET_NO;
1210 re_match->ctask = ctask;
1211 strcpy (re_match->hostname, re_hostname);
1212 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1213 "PP: Got hostname %s\n", re_hostname);
1214 re_ptr += m[3].rm_eo;
1216 if (GNUNET_YES == is_tld (re_match->hostname, GNUNET_GNS_TLD_PLUS))
1218 re_match->hostname[strlen(re_match->hostname)-1] = '\0';
1219 strcpy (re_match->hostname+strlen(re_match->hostname),
1223 re_match->shorten_task = GNUNET_GNS_shorten_zone (gns_handle,
1229 re_match); //FIXME cancel appropriately
1231 GNUNET_CONTAINER_DLL_insert_tail (ctask->pp_match_head,
1232 ctask->pp_match_tail,
1236 ctask->buf_status = BUF_WAIT_FOR_MHD;
1237 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1238 "PP: No more matches\n");
1239 run_mhd_now (ctask->mhd);
1243 * Handle data from cURL
1245 * @param ptr pointer to the data
1246 * @param size number of blocks of data
1247 * @param nmemb blocksize
1248 * @param ctx the curlproxytask
1249 * @return number of bytes handled
1252 curl_download_cb (void *ptr, size_t size, size_t nmemb, void* ctx)
1254 const char *cbuf = ptr;
1255 size_t total = size * nmemb;
1256 struct ProxyCurlTask *ctask = ctx;
1257 size_t buf_space = sizeof (ctask->buffer) -
1258 (ctask->buffer_write_ptr-ctask->buffer);
1260 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1261 "CURL: Got %d. %d free in buffer\n",
1264 if (BUF_WAIT_FOR_CURL != ctask->buf_status)
1265 return CURL_WRITEFUNC_PAUSE;
1267 if (total > (buf_space - CURL_BUF_PADDING))
1269 if (ctask->buf_status == BUF_WAIT_FOR_CURL)
1271 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1272 "CURL: Buffer full starting postprocessing\n");
1273 ctask->buf_status = BUF_WAIT_FOR_PP;
1274 ctask->pp_task = GNUNET_SCHEDULER_add_now (&postprocess_buffer,
1276 return CURL_WRITEFUNC_PAUSE;
1279 /* we should not get called in that case */
1280 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1281 "CURL: called out of context and no space in buffer!\n");
1282 return CURL_WRITEFUNC_PAUSE;
1285 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1286 "CURL: Copying %d bytes to buffer (%s)\n", total, ctask->url);
1287 memcpy (ctask->buffer_write_ptr, cbuf, total);
1288 ctask->bytes_in_buffer += total;
1289 ctask->buffer_write_ptr += total;
1290 ctask->buffer_write_ptr[0] = '\0';
1296 * cURL callback for post data
1299 read_callback (void *buf, size_t size, size_t nmemb, void *cls)
1301 struct ProxyCurlTask *ctask = cls;
1302 struct ProxyPostData *pdata = ctask->post_data_head;
1303 size_t len = size * nmemb;
1307 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1308 "CURL: read callback\n");
1314 if (NULL == pdata->value)
1316 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1317 "CURL: Terminating POST data\n");
1319 GNUNET_CONTAINER_DLL_remove (ctask->post_data_head,
1320 ctask->post_data_tail,
1322 GNUNET_free (pdata);
1326 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1327 "CURL: read callback value %s\n", pdata->value);
1329 to_copy = pdata->bytes_left;
1333 pos = pdata->value + (pdata->total_bytes - pdata->bytes_left);
1334 memcpy (buf, pos, to_copy);
1335 pdata->bytes_left -= to_copy;
1336 if (pdata->bytes_left <= 0)
1338 GNUNET_free (pdata->value);
1339 GNUNET_CONTAINER_DLL_remove (ctask->post_data_head,
1340 ctask->post_data_tail,
1342 GNUNET_free (pdata);
1348 * Task that is run when we are ready to receive more data
1351 * @param cls closure
1352 * @param tc task context
1355 curl_task_download (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
1358 * Ask cURL for the select sets and schedule download
1361 curl_download_prepare ()
1368 struct GNUNET_NETWORK_FDSet *grs;
1369 struct GNUNET_NETWORK_FDSet *gws;
1371 struct GNUNET_TIME_Relative rtime;
1377 mret = curl_multi_fdset (curl_multi, &rs, &ws, &es, &max);
1379 if (mret != CURLM_OK)
1381 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1382 "%s failed at %s:%d: `%s'\n",
1383 "curl_multi_fdset", __FILE__, __LINE__,
1384 curl_multi_strerror (mret));
1385 //TODO cleanup here?
1389 mret = curl_multi_timeout (curl_multi, &to);
1390 rtime = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, to);
1392 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1393 "cURL multi fds: max=%d timeout=%llu\n", max, to);
1395 grs = GNUNET_NETWORK_fdset_create ();
1396 gws = GNUNET_NETWORK_fdset_create ();
1397 GNUNET_NETWORK_fdset_copy_native (grs, &rs, max + 1);
1398 GNUNET_NETWORK_fdset_copy_native (gws, &ws, max + 1);
1399 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1400 "Scheduling task cURL\n");
1402 if (curl_download_task != GNUNET_SCHEDULER_NO_TASK)
1403 GNUNET_SCHEDULER_cancel (curl_download_task);
1407 curl_download_task =
1408 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1411 &curl_task_download, curl_multi);
1413 else if (NULL != ctasks_head)
1415 /* as specified in curl docs */
1416 curl_download_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MILLISECONDS,
1417 &curl_task_download,
1420 GNUNET_NETWORK_fdset_destroy (gws);
1421 GNUNET_NETWORK_fdset_destroy (grs);
1426 * Task that is run when we are ready to receive more data
1429 * @param cls closure
1430 * @param tc task context
1433 curl_task_download (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1437 struct CURLMsg *msg;
1439 struct ProxyCurlTask *ctask;
1443 struct ProxyCurlTask *clean_head = NULL;
1444 struct ProxyCurlTask *clean_tail = NULL;
1446 curl_download_task = GNUNET_SCHEDULER_NO_TASK;
1448 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1450 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1451 "Shutdown requested while trying to download\n");
1455 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1463 mret = curl_multi_perform (curl_multi, &running);
1465 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1466 "Running curl tasks: %d\n", running);
1468 ctask = ctasks_head;
1469 for (; ctask != NULL; ctask = ctask->next)
1471 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1472 "CTask: %s\n", ctask->url);
1476 if (num_ctasks != running)
1478 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1479 "%d ctasks, %d curl running\n", num_ctasks, running);
1484 ctask = ctasks_head;
1485 msg = curl_multi_info_read (curl_multi, &msgnum);
1486 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1487 "Messages left: %d\n", msgnum);
1494 if ((msg->data.result != CURLE_OK) &&
1495 (msg->data.result != CURLE_GOT_NOTHING))
1497 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1498 "Download curl failed");
1500 for (; ctask != NULL; ctask = ctask->next)
1502 if (NULL == ctask->curl)
1505 if (memcmp (msg->easy_handle, ctask->curl, sizeof (CURL)) != 0)
1508 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1509 "CURL: Download failed for task %s: %s.\n",
1511 curl_easy_strerror (msg->data.result));
1512 ctask->download_is_finished = GNUNET_YES;
1513 ctask->download_error = GNUNET_YES;
1514 if (CURLE_OK == curl_easy_getinfo (ctask->curl,
1515 CURLINFO_RESPONSE_CODE,
1517 ctask->curl_response_code = resp_code;
1518 ctask->ready_to_queue = MHD_YES;
1519 ctask->buf_status = BUF_WAIT_FOR_MHD;
1520 run_mhd_now (ctask->mhd);
1522 GNUNET_CONTAINER_DLL_remove (ctasks_head, ctasks_tail,
1524 GNUNET_CONTAINER_DLL_insert (clean_head, clean_tail, ctask);
1527 GNUNET_assert (ctask != NULL);
1531 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1532 "CURL: download completed.\n");
1534 for (; ctask != NULL; ctask = ctask->next)
1536 if (NULL == ctask->curl)
1539 if (0 != memcmp (msg->easy_handle, ctask->curl, sizeof (CURL)))
1542 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1543 "CURL: completed task %s found.\n", ctask->url);
1544 if (CURLE_OK == curl_easy_getinfo (ctask->curl,
1545 CURLINFO_RESPONSE_CODE,
1547 ctask->curl_response_code = resp_code;
1550 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1551 "CURL: Completed ctask!\n");
1552 if (GNUNET_SCHEDULER_NO_TASK == ctask->pp_task)
1554 ctask->buf_status = BUF_WAIT_FOR_PP;
1555 ctask->pp_task = GNUNET_SCHEDULER_add_now (&postprocess_buffer,
1559 ctask->ready_to_queue = MHD_YES;
1560 ctask->download_is_finished = GNUNET_YES;
1562 /* We MUST not modify the multi handle else we loose messages */
1563 GNUNET_CONTAINER_DLL_remove (ctasks_head, ctasks_tail,
1565 GNUNET_CONTAINER_DLL_insert (clean_head, clean_tail, ctask);
1569 GNUNET_assert (ctask != NULL);
1571 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1572 "CURL: %s\n", curl_easy_strerror(msg->data.result));
1578 } while (msgnum > 0);
1580 for (ctask=clean_head; ctask != NULL; ctask = ctask->next)
1582 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1583 "CURL: Removing task %s.\n", ctask->url);
1584 curl_multi_remove_handle (curl_multi, ctask->curl);
1585 curl_easy_cleanup (ctask->curl);
1590 for (ctask=ctasks_head; ctask != NULL; ctask = ctask->next)
1595 if (num_ctasks != running)
1597 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1598 "CURL: %d tasks, %d running\n", num_ctasks, running);
1601 GNUNET_assert ( num_ctasks == running );
1603 } while (mret == CURLM_CALL_MULTI_PERFORM);
1605 if (mret != CURLM_OK)
1607 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "CURL: %s failed at %s:%d: `%s'\n",
1608 "curl_multi_perform", __FILE__, __LINE__,
1609 curl_multi_strerror (mret));
1611 curl_download_prepare();
1615 * Process LEHO lookup
1617 * @param cls the ctask
1618 * @param rd_count number of records returned
1619 * @param rd record data
1622 process_leho_lookup (void *cls,
1624 const struct GNUNET_NAMESTORE_RecordData *rd)
1626 struct ProxyCurlTask *ctask = cls;
1627 char hosthdr[262]; //256 + "Host: "
1631 struct hostent *phost;
1633 char resolvename[512];
1636 strcpy (ctask->leho, "");
1639 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1640 "No LEHO present!\n");
1642 for (i=0; i<rd_count; i++)
1644 if (rd[i].record_type != GNUNET_GNS_RECORD_LEHO)
1647 memcpy (ctask->leho, rd[i].data, rd[i].data_size);
1649 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1650 "Found LEHO %s for %s\n", ctask->leho, ctask->url);
1653 if (0 != strcmp (ctask->leho, ""))
1655 sprintf (hosthdr, "%s%s:%d", "Host: ", ctask->leho, ctask->port);
1656 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1657 "New HTTP header value: %s\n", hosthdr);
1658 ctask->headers = curl_slist_append (ctask->headers, hosthdr);
1659 GNUNET_assert (NULL != ctask->headers);
1660 ret = curl_easy_setopt (ctask->curl, CURLOPT_HTTPHEADER, ctask->headers);
1661 if (CURLE_OK != ret)
1663 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "%s failed at %s:%d: `%s'\n",
1664 "curl_easy_setopt", __FILE__, __LINE__, curl_easy_strerror(ret));
1669 if (ctask->mhd->is_ssl)
1671 phost = (struct hostent*)gethostbyname (ctask->host);
1675 ssl_ip = inet_ntoa(*((struct in_addr*)(phost->h_addr)));
1676 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1677 "SSL target server: %s\n", ssl_ip);
1678 sprintf (resolvename, "%s:%d:%s", ctask->leho, HTTPS_PORT, ssl_ip);
1679 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1680 "Curl resolve: %s\n", resolvename);
1681 ctask->resolver = curl_slist_append ( ctask->resolver, resolvename);
1682 curl_easy_setopt (ctask->curl, CURLOPT_RESOLVE, ctask->resolver);
1683 sprintf (curlurl, "https://%s:%d%s", ctask->leho, ctask->port, ctask->url);
1684 curl_easy_setopt (ctask->curl, CURLOPT_URL, curlurl);
1688 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1689 "gethostbyname failed for %s!\n", ctask->host);
1690 ctask->download_is_finished = GNUNET_YES;
1691 ctask->download_error = GNUNET_YES;
1696 if (CURLM_OK != (mret=curl_multi_add_handle (curl_multi, ctask->curl)))
1698 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1699 "%s failed at %s:%d: `%s'\n",
1700 "curl_multi_add_handle", __FILE__, __LINE__,
1701 curl_multi_strerror (mret));
1702 ctask->download_is_finished = GNUNET_YES;
1703 ctask->download_error = GNUNET_YES;
1706 GNUNET_CONTAINER_DLL_insert (ctasks_head, ctasks_tail, ctask);
1708 curl_download_prepare ();
1713 * Initialize download and trigger curl
1715 * @param cls the proxycurltask
1716 * @param auth_name the name of the authority (site of origin) of ctask->host
1720 process_get_authority (void *cls,
1721 const char* auth_name)
1723 struct ProxyCurlTask *ctask = cls;
1725 if (NULL == auth_name)
1727 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1728 "Get authority failed!\n");
1729 strcpy (ctask->authority, "");
1733 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1734 "Get authority yielded %s\n", auth_name);
1735 strcpy (ctask->authority, auth_name);
1738 GNUNET_GNS_lookup_zone (gns_handle,
1741 GNUNET_GNS_RECORD_LEHO,
1742 GNUNET_YES, //Only cached for performance
1744 &process_leho_lookup,
1749 mhd_log_callback (void* cls, const char* url)
1751 struct ProxyCurlTask *ctask;
1753 ctask = GNUNET_malloc (sizeof (struct ProxyCurlTask));
1754 strcpy (ctask->url, url);
1755 ctask->accepted = GNUNET_NO;
1760 * Main MHD callback for handling requests.
1763 * @param con MHD connection handle
1764 * @param url the url in the request
1765 * @param meth the HTTP method used ("GET", "PUT", etc.)
1766 * @param ver the HTTP version string (i.e. "HTTP/1.1")
1767 * @param upload_data the data being uploaded (excluding HEADERS,
1768 * for a POST that fits into memory and that is encoded
1769 * with a supported encoding, the POST data will NOT be
1770 * given in upload_data and is instead available as
1771 * part of MHD_get_connection_values; very large POST
1772 * data *will* be made available incrementally in
1774 * @param upload_data_size set initially to the size of the
1775 * upload_data provided; the method must update this
1776 * value to the number of bytes NOT processed;
1777 * @param con_cls pointer to location where we store the 'struct Request'
1778 * @return MHD_YES if the connection was handled successfully,
1779 * MHD_NO if the socket must be closed due to a serious
1780 * error while handling the request
1783 create_response (void *cls,
1784 struct MHD_Connection *con,
1788 const char *upload_data,
1789 size_t *upload_data_size,
1792 struct MhdHttpList* hd = cls;
1793 const char* page = "<html><head><title>gnoxy</title>"\
1794 "</head><body>cURL fail</body></html>";
1796 char curlurl[MAX_HTTP_URI_LENGTH]; // buffer overflow!
1799 struct ProxyCurlTask *ctask = *con_cls;
1800 struct ProxyPostData *fin_post;
1803 if ((0 != strcasecmp (meth, MHD_HTTP_METHOD_GET)) &&
1804 (0 != strcasecmp (meth, MHD_HTTP_METHOD_PUT)) &&
1805 (0 != strcasecmp (meth, MHD_HTTP_METHOD_POST)) &&
1806 (0 != strcasecmp (meth, MHD_HTTP_METHOD_HEAD)))
1808 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1809 "MHD: %s NOT IMPLEMENTED!\n", meth);
1814 if (GNUNET_NO == ctask->accepted)
1817 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1818 "Got %s request for %s\n", meth, url);
1819 //ctask = GNUNET_malloc (sizeof (struct ProxyCurlTask));
1822 ctask->curl = curl_easy_init();
1823 if (NULL == ctask->curl)
1825 ctask->response = MHD_create_response_from_buffer (strlen (page),
1827 MHD_RESPMEM_PERSISTENT);
1828 ret = MHD_queue_response (con,
1831 MHD_destroy_response (ctask->response);
1832 GNUNET_free (ctask);
1836 ctask->accepted = GNUNET_YES;
1837 ctask->download_in_progress = GNUNET_YES;
1838 ctask->buf_status = BUF_WAIT_FOR_CURL;
1839 ctask->connection = con;
1840 ctask->curl_response_code = MHD_HTTP_OK;
1841 ctask->buffer_read_ptr = ctask->buffer;
1842 ctask->buffer_write_ptr = ctask->buffer;
1843 ctask->pp_task = GNUNET_SCHEDULER_NO_TASK;
1844 if (ctask->mhd->is_ssl)
1845 ctask->port = HTTPS_PORT;
1847 ctask->port = HTTP_PORT;
1849 MHD_get_connection_values (con,
1851 &con_val_iter, ctask);
1853 if (0 == strcasecmp (meth, MHD_HTTP_METHOD_PUT))
1855 //FIXME: this prolly works like POST?
1856 if (0 == *upload_data_size)
1858 curl_easy_cleanup (ctask->curl);
1859 GNUNET_free (ctask);
1862 ctask->put_read_offset = 0;
1863 ctask->put_read_size = *upload_data_size;
1864 curl_easy_setopt (ctask->curl, CURLOPT_UPLOAD, 1);
1865 curl_easy_setopt (ctask->curl, CURLOPT_READDATA, upload_data);
1866 //curl_easy_setopt (ctask->curl, CURLOPT_READFUNCTION, &curl_read_cb);
1868 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1869 "Got PUT data: %s\n", upload_data);
1870 curl_easy_cleanup (ctask->curl);
1871 GNUNET_free (ctask);
1875 if (0 == strcasecmp (meth, MHD_HTTP_METHOD_POST))
1877 //FIXME handle multipart
1878 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1879 "Setting up POST processor\n");
1880 ctask->post_handler = MHD_create_post_processor (con,
1882 &con_post_data_iter,
1884 curl_easy_setopt (ctask->curl, CURLOPT_POST, 1);
1885 curl_easy_setopt (ctask->curl, CURLOPT_READFUNCTION,
1887 curl_easy_setopt (ctask->curl, CURLOPT_READDATA, ctask);
1888 ctask->headers = curl_slist_append (ctask->headers,
1889 "Transfer-Encoding: chunked");
1892 if (0 == strcasecmp (meth, MHD_HTTP_METHOD_HEAD))
1894 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1895 "Setting NOBODY\n");
1896 curl_easy_setopt (ctask->curl, CURLOPT_NOBODY, 1);
1899 curl_easy_setopt (ctask->curl, CURLOPT_HEADERFUNCTION, &curl_check_hdr);
1900 curl_easy_setopt (ctask->curl, CURLOPT_HEADERDATA, ctask);
1901 curl_easy_setopt (ctask->curl, CURLOPT_WRITEFUNCTION, &curl_download_cb);
1902 curl_easy_setopt (ctask->curl, CURLOPT_WRITEDATA, ctask);
1903 curl_easy_setopt (ctask->curl, CURLOPT_FOLLOWLOCATION, 0);
1904 curl_easy_setopt (ctask->curl, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
1906 if (GNUNET_NO == ctask->mhd->is_ssl)
1908 sprintf (curlurl, "http://%s:%d%s", ctask->host, ctask->port, ctask->url);
1909 //MHD_get_connection_values (con,
1910 // MHD_GET_ARGUMENT_KIND,
1911 // &get_uri_val_iter, curlurl);
1912 curl_easy_setopt (ctask->curl, CURLOPT_URL, curlurl);
1915 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1916 "MHD: Adding new curl task for %s\n", ctask->host);
1917 //MHD_get_connection_values (con,
1918 // MHD_GET_ARGUMENT_KIND,
1919 // &get_uri_val_iter, ctask->url);
1921 curl_easy_setopt (ctask->curl, CURLOPT_FAILONERROR, 1);
1922 curl_easy_setopt (ctask->curl, CURLOPT_CONNECTTIMEOUT, 600L);
1923 curl_easy_setopt (ctask->curl, CURLOPT_TIMEOUT, 600L);
1925 GNUNET_GNS_get_authority (gns_handle,
1927 &process_get_authority,
1929 ctask->ready_to_queue = GNUNET_NO;
1930 ctask->fin = GNUNET_NO;
1934 ctask = (struct ProxyCurlTask *) *con_cls;
1935 if (0 == strcasecmp (meth, MHD_HTTP_METHOD_POST))
1937 if (0 != *upload_data_size)
1939 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1940 "Invoking POST processor\n");
1941 MHD_post_process (ctask->post_handler,
1942 upload_data, *upload_data_size);
1943 *upload_data_size = 0;
1946 else if (GNUNET_NO == ctask->post_done)
1948 fin_post = GNUNET_malloc (sizeof (struct ProxyPostData));
1949 GNUNET_CONTAINER_DLL_insert_tail (ctask->post_data_head,
1950 ctask->post_data_tail,
1952 ctask->post_done = GNUNET_YES;
1957 if (GNUNET_YES != ctask->ready_to_queue)
1958 return MHD_YES; /* wait longer */
1960 if (GNUNET_YES == ctask->fin)
1963 ctask->fin = GNUNET_YES;
1964 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1965 "MHD: Queueing response for %s\n", ctask->url);
1966 ret = MHD_queue_response (con, ctask->curl_response_code, ctask->response);
1967 run_mhd_now (ctask->mhd);
1978 struct MhdHttpList *hd;
1980 for (hd=mhd_httpd_head; NULL != hd; hd = hd->next)
1988 * @param hd the daemon to run
1991 run_httpd (struct MhdHttpList *hd)
1996 struct GNUNET_NETWORK_FDSet *wrs;
1997 struct GNUNET_NETWORK_FDSet *wws;
1998 struct GNUNET_NETWORK_FDSet *wes;
2001 unsigned MHD_LONG_LONG timeout;
2002 struct GNUNET_TIME_Relative tv;
2007 wrs = GNUNET_NETWORK_fdset_create ();
2008 wes = GNUNET_NETWORK_fdset_create ();
2009 wws = GNUNET_NETWORK_fdset_create ();
2011 GNUNET_assert (MHD_YES == MHD_get_fdset (hd->daemon, &rs, &ws, &es, &max));
2014 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2015 "MHD fds: max=%d\n", max);
2017 haveto = MHD_get_timeout (hd->daemon, &timeout);
2019 if (MHD_YES == haveto)
2020 tv.rel_value = (uint64_t) timeout;
2022 tv = GNUNET_TIME_UNIT_FOREVER_REL;
2023 GNUNET_NETWORK_fdset_copy_native (wrs, &rs, max + 1);
2024 GNUNET_NETWORK_fdset_copy_native (wws, &ws, max + 1);
2025 GNUNET_NETWORK_fdset_copy_native (wes, &es, max + 1);
2027 if (GNUNET_SCHEDULER_NO_TASK != hd->httpd_task)
2028 GNUNET_SCHEDULER_cancel (hd->httpd_task);
2030 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_HIGH,
2033 GNUNET_NETWORK_fdset_destroy (wrs);
2034 GNUNET_NETWORK_fdset_destroy (wws);
2035 GNUNET_NETWORK_fdset_destroy (wes);
2040 * Task run whenever HTTP server operations are pending.
2043 * @param tc sched context
2046 do_httpd (void *cls,
2047 const struct GNUNET_SCHEDULER_TaskContext *tc)
2049 struct MhdHttpList *hd = cls;
2051 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2052 "MHD: Main loop\n");
2053 hd->httpd_task = GNUNET_SCHEDULER_NO_TASK;
2054 MHD_run (hd->daemon);
2061 * Read data from socket
2063 * @param cls the closure
2064 * @param tc scheduler context
2067 do_read (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
2070 * Read from remote end
2072 * @param cls closure
2073 * @param tc scheduler context
2076 do_read_remote (void* cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
2079 * Write data to remote socket
2081 * @param cls the closure
2082 * @param tc scheduler context
2085 do_write_remote (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2087 struct Socks5Request *s5r = cls;
2090 s5r->fwdwtask = GNUNET_SCHEDULER_NO_TASK;
2092 if ((NULL != tc->read_ready) &&
2093 (GNUNET_NETWORK_fdset_isset (tc->write_ready, s5r->remote_sock)) &&
2094 ((len = GNUNET_NETWORK_socket_send (s5r->remote_sock, s5r->rbuf,
2097 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2098 "Successfully sent %d bytes to remote socket\n",
2103 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "write remote");
2104 if (GNUNET_SCHEDULER_NO_TASK != s5r->rtask)
2105 GNUNET_SCHEDULER_cancel (s5r->rtask);
2106 if (GNUNET_SCHEDULER_NO_TASK != s5r->wtask)
2107 GNUNET_SCHEDULER_cancel (s5r->wtask);
2108 if (GNUNET_SCHEDULER_NO_TASK != s5r->fwdrtask)
2109 GNUNET_SCHEDULER_cancel (s5r->fwdrtask);
2110 GNUNET_NETWORK_socket_close (s5r->remote_sock);
2111 GNUNET_NETWORK_socket_close (s5r->sock);
2117 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
2124 * Clean up s5r handles
2126 * @param s5r the handle to destroy
2129 cleanup_s5r (struct Socks5Request *s5r)
2131 if (GNUNET_SCHEDULER_NO_TASK != s5r->rtask)
2132 GNUNET_SCHEDULER_cancel (s5r->rtask);
2133 if (GNUNET_SCHEDULER_NO_TASK != s5r->fwdwtask)
2134 GNUNET_SCHEDULER_cancel (s5r->fwdwtask);
2135 if (GNUNET_SCHEDULER_NO_TASK != s5r->fwdrtask)
2136 GNUNET_SCHEDULER_cancel (s5r->fwdrtask);
2138 if (NULL != s5r->remote_sock)
2139 GNUNET_NETWORK_socket_close (s5r->remote_sock);
2140 if ((NULL != s5r->sock) && (s5r->cleanup_sock == GNUNET_YES))
2141 GNUNET_NETWORK_socket_close (s5r->sock);
2147 * Write data to socket
2149 * @param cls the closure
2150 * @param tc scheduler context
2153 do_write (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2155 struct Socks5Request *s5r = cls;
2158 s5r->wtask = GNUNET_SCHEDULER_NO_TASK;
2160 if ((NULL != tc->read_ready) &&
2161 (GNUNET_NETWORK_fdset_isset (tc->write_ready, s5r->sock)) &&
2162 ((len = GNUNET_NETWORK_socket_send (s5r->sock, s5r->wbuf,
2165 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2166 "Successfully sent %d bytes to socket\n",
2172 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "write");
2173 s5r->cleanup = GNUNET_YES;
2174 s5r->cleanup_sock = GNUNET_YES;
2180 if (GNUNET_YES == s5r->cleanup)
2186 if ((s5r->state == SOCKS5_DATA_TRANSFER) &&
2187 (s5r->fwdrtask == GNUNET_SCHEDULER_NO_TASK))
2189 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
2191 &do_read_remote, s5r);
2195 * Read from remote end
2197 * @param cls closure
2198 * @param tc scheduler context
2201 do_read_remote (void* cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2203 struct Socks5Request *s5r = cls;
2205 s5r->fwdrtask = GNUNET_SCHEDULER_NO_TASK;
2208 if ((NULL != tc->write_ready) &&
2209 (GNUNET_NETWORK_fdset_isset (tc->read_ready, s5r->remote_sock)) &&
2210 (s5r->wbuf_len = GNUNET_NETWORK_socket_recv (s5r->remote_sock, s5r->wbuf,
2211 sizeof (s5r->wbuf))))
2213 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2214 "Successfully read %d bytes from remote socket\n",
2219 if (s5r->wbuf_len == 0)
2220 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2221 "0 bytes received from remote... graceful shutdown!\n");
2222 if (s5r->fwdwtask != GNUNET_SCHEDULER_NO_TASK)
2223 GNUNET_SCHEDULER_cancel (s5r->fwdwtask);
2224 if (s5r->rtask != GNUNET_SCHEDULER_NO_TASK)
2225 GNUNET_SCHEDULER_cancel (s5r->rtask);
2227 GNUNET_NETWORK_socket_close (s5r->remote_sock);
2228 s5r->remote_sock = NULL;
2229 GNUNET_NETWORK_socket_close (s5r->sock);
2235 s5r->wtask = GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
2243 * Adds a socket to MHD
2245 * @param h the handle to the socket to add
2246 * @param daemon the daemon to add the fd to
2247 * @return whatever MHD_add_connection returns
2250 add_handle_to_mhd (struct GNUNET_NETWORK_Handle *h, struct MHD_Daemon *daemon)
2253 struct sockaddr *addr;
2256 fd = dup (GNUNET_NETWORK_get_fd (h));
2257 addr = GNUNET_NETWORK_get_addr (h);
2258 len = GNUNET_NETWORK_get_addrlen (h);
2260 return MHD_add_connection (daemon, fd, addr, len);
2264 * Read file in filename
2266 * @param filename file to read
2267 * @param size pointer where filesize is stored
2271 load_file (const char* filename,
2278 GNUNET_DISK_file_size (filename, &fsize,
2279 GNUNET_YES, GNUNET_YES))
2281 if (fsize > MAX_PEM_SIZE)
2283 *size = (unsigned int) fsize;
2284 buffer = GNUNET_malloc (*size);
2285 if (fsize != GNUNET_DISK_fn_read (filename, buffer, (size_t) fsize))
2287 GNUNET_free (buffer);
2295 * Load PEM key from file
2297 * @param key where to store the data
2298 * @param keyfile path to the PEM file
2299 * @return GNUNET_OK on success
2302 load_key_from_file (gnutls_x509_privkey_t key, const char* keyfile)
2304 gnutls_datum_t key_data;
2307 key_data.data = (unsigned char*) load_file (keyfile, &key_data.size);
2308 ret = gnutls_x509_privkey_import (key, &key_data,
2309 GNUTLS_X509_FMT_PEM);
2310 if (GNUTLS_E_SUCCESS != ret)
2312 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2313 _("Unable to import private key from file `%s'\n"),
2317 GNUNET_free (key_data.data);
2318 return (GNUTLS_E_SUCCESS != ret) ? GNUNET_SYSERR : GNUNET_OK;
2323 * Load cert from file
2325 * @param crt struct to store data in
2326 * @param certfile path to pem file
2327 * @return GNUNET_OK on success
2330 load_cert_from_file (gnutls_x509_crt_t crt, char* certfile)
2332 gnutls_datum_t cert_data;
2333 cert_data.data = NULL;
2336 cert_data.data = (unsigned char*) load_file (certfile, &cert_data.size);
2337 ret = gnutls_x509_crt_import (crt, &cert_data,
2338 GNUTLS_X509_FMT_PEM);
2339 if (GNUTLS_E_SUCCESS != ret)
2341 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2342 _("Unable to import certificate %s\n"), certfile);
2345 GNUNET_free (cert_data.data);
2346 return (GNUTLS_E_SUCCESS != ret) ? GNUNET_SYSERR : GNUNET_OK;
2351 * Generate new certificate for specific name
2353 * @param name the subject name to generate a cert for
2354 * @return a struct holding the PEM data
2356 static struct ProxyGNSCertificate *
2357 generate_gns_certificate (const char *name)
2361 unsigned int serial;
2362 size_t key_buf_size;
2363 size_t cert_buf_size;
2364 gnutls_x509_crt_t request;
2368 ret = gnutls_x509_crt_init (&request);
2370 if (GNUTLS_E_SUCCESS != ret)
2375 ret = gnutls_x509_crt_set_key (request, proxy_ca.key);
2377 if (GNUTLS_E_SUCCESS != ret)
2382 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Generating cert\n");
2384 struct ProxyGNSCertificate *pgc =
2385 GNUNET_malloc (sizeof (struct ProxyGNSCertificate));
2387 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Adding DNs\n");
2389 gnutls_x509_crt_set_dn_by_oid (request, GNUTLS_OID_X520_COUNTRY_NAME,
2392 gnutls_x509_crt_set_dn_by_oid (request, GNUTLS_OID_X520_ORGANIZATION_NAME,
2395 gnutls_x509_crt_set_dn_by_oid (request, GNUTLS_OID_X520_COMMON_NAME,
2396 0, name, strlen (name));
2398 ret = gnutls_x509_crt_set_version (request, 3);
2400 ret = gnutls_rnd (GNUTLS_RND_NONCE, &serial, sizeof (serial));
2402 etime = time (NULL);
2403 tm_data = localtime (&etime);
2406 ret = gnutls_x509_crt_set_serial (request,
2410 ret = gnutls_x509_crt_set_activation_time (request,
2413 etime = mktime (tm_data);
2420 ret = gnutls_x509_crt_set_expiration_time (request,
2422 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Signing...\n");
2424 ret = gnutls_x509_crt_sign (request, proxy_ca.cert, proxy_ca.key);
2426 key_buf_size = sizeof (pgc->key);
2427 cert_buf_size = sizeof (pgc->cert);
2429 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Exporting certificate...\n");
2431 gnutls_x509_crt_export (request, GNUTLS_X509_FMT_PEM,
2432 pgc->cert, &cert_buf_size);
2434 gnutls_x509_privkey_export (proxy_ca.key, GNUTLS_X509_FMT_PEM,
2435 pgc->key, &key_buf_size);
2438 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Cleaning up\n");
2439 gnutls_x509_crt_deinit (request);
2447 * Accept policy for mhdaemons
2450 * @param addr the sockaddr
2451 * @param addrlen the sockaddr length
2452 * @return MHD_NO if sockaddr is wrong or #conns too high
2455 accept_cb (void* cls, const struct sockaddr *addr, socklen_t addrlen)
2457 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2458 "In MHD accept policy cb\n");
2462 if (addr->sa_family == AF_UNIX)
2466 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2467 "Connection accepted\n");
2474 * Adds a socket to an SSL MHD instance
2475 * It is important the the domain name is
2476 * correct. In most cases we need to start a new daemon
2478 * @param h the handle to add to a daemon
2479 * @param domain the domain the ssl daemon has to serve
2480 * @return MHD_YES on success
2483 add_handle_to_ssl_mhd (struct GNUNET_NETWORK_Handle *h, const char* domain)
2485 struct MhdHttpList *hd = NULL;
2486 struct ProxyGNSCertificate *pgc;
2487 struct NetworkHandleList *nh;
2489 for (hd = mhd_httpd_head; NULL != hd; hd = hd->next)
2490 if (0 == strcmp (hd->domain, domain))
2495 pgc = generate_gns_certificate (domain);
2497 hd = GNUNET_malloc (sizeof (struct MhdHttpList));
2498 hd->is_ssl = GNUNET_YES;
2499 strcpy (hd->domain, domain);
2500 hd->proxy_cert = pgc;
2503 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2504 "No previous SSL instance found... starting new one for %s\n",
2506 hd->daemon = MHD_start_daemon (MHD_USE_DEBUG
2508 #if HAVE_MHD_NO_LISTEN_SOCKET
2509 | MHD_USE_NO_LISTEN_SOCKET,
2515 &create_response, hd,
2516 #if !HAVE_MHD_NO_LISTEN_SOCKET
2517 MHD_OPTION_LISTEN_SOCKET, GNUNET_NETWORK_get_fd (mhd_unix_socket),
2519 MHD_OPTION_CONNECTION_LIMIT,
2520 MHD_MAX_CONNECTIONS,
2521 MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) 16,
2522 MHD_OPTION_NOTIFY_COMPLETED, NULL, NULL,
2523 MHD_OPTION_HTTPS_MEM_KEY, pgc->key,
2524 MHD_OPTION_HTTPS_MEM_CERT, pgc->cert,
2525 MHD_OPTION_URI_LOG_CALLBACK, &mhd_log_callback,
2529 GNUNET_assert (hd->daemon != NULL);
2530 hd->httpd_task = GNUNET_SCHEDULER_NO_TASK;
2532 GNUNET_CONTAINER_DLL_insert (mhd_httpd_head, mhd_httpd_tail, hd);
2535 nh = GNUNET_malloc (sizeof (struct NetworkHandleList));
2538 GNUNET_CONTAINER_DLL_insert (hd->socket_handles_head,
2539 hd->socket_handles_tail,
2542 return add_handle_to_mhd (h, hd->daemon);
2548 * Read data from incoming connection
2550 * @param cls the closure
2551 * @param tc the scheduler context
2554 do_read (void* cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2556 struct Socks5Request *s5r = cls;
2557 struct socks5_client_hello *c_hello;
2558 struct socks5_server_hello *s_hello;
2559 struct socks5_client_request *c_req;
2560 struct socks5_server_response *s_resp;
2566 struct hostent *phost;
2568 struct sockaddr_in remote_addr;
2569 struct in_addr *r_sin_addr;
2571 struct NetworkHandleList *nh;
2573 s5r->rtask = GNUNET_SCHEDULER_NO_TASK;
2575 if ((NULL != tc->write_ready) &&
2576 (GNUNET_NETWORK_fdset_isset (tc->read_ready, s5r->sock)) &&
2577 (s5r->rbuf_len = GNUNET_NETWORK_socket_recv (s5r->sock, s5r->rbuf,
2578 sizeof (s5r->rbuf))))
2580 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2581 "Successfully read %d bytes from socket\n",
2586 if (s5r->rbuf_len != 0)
2587 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "read");
2589 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client disco!\n");
2591 if (s5r->fwdrtask != GNUNET_SCHEDULER_NO_TASK)
2592 GNUNET_SCHEDULER_cancel (s5r->fwdrtask);
2593 if (s5r->wtask != GNUNET_SCHEDULER_NO_TASK)
2594 GNUNET_SCHEDULER_cancel (s5r->wtask);
2595 if (s5r->fwdwtask != GNUNET_SCHEDULER_NO_TASK)
2596 GNUNET_SCHEDULER_cancel (s5r->fwdwtask);
2597 GNUNET_NETWORK_socket_close (s5r->remote_sock);
2598 GNUNET_NETWORK_socket_close (s5r->sock);
2603 if (s5r->state == SOCKS5_INIT)
2605 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2607 c_hello = (struct socks5_client_hello*)&s5r->rbuf;
2609 GNUNET_assert (c_hello->version == SOCKS_VERSION_5);
2611 s_hello = (struct socks5_server_hello*)&s5r->wbuf;
2612 s5r->wbuf_len = sizeof( struct socks5_server_hello );
2614 s_hello->version = c_hello->version;
2615 s_hello->auth_method = SOCKS_AUTH_NONE;
2617 /* Write response to client */
2618 s5r->wtask = GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
2622 s5r->rtask = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
2626 s5r->state = SOCKS5_REQUEST;
2630 if (s5r->state == SOCKS5_REQUEST)
2632 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2633 "Processing SOCKS5 request\n");
2634 c_req = (struct socks5_client_request*)&s5r->rbuf;
2635 s_resp = (struct socks5_server_response*)&s5r->wbuf;
2636 //Only 10byte for ipv4 response!
2637 s5r->wbuf_len = 10;//sizeof (struct socks5_server_response);
2639 GNUNET_assert (c_req->addr_type == 3);
2641 dom_len = *((uint8_t*)(&(c_req->addr_type) + 1));
2642 memset(domain, 0, sizeof(domain));
2643 strncpy(domain, (char*)(&(c_req->addr_type) + 2), dom_len);
2644 req_port = *((uint16_t*)(&(c_req->addr_type) + 2 + dom_len));
2646 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2647 "Requested connection is %s:%d\n",
2651 if (is_tld (domain, GNUNET_GNS_TLD) ||
2652 is_tld (domain, GNUNET_GNS_TLD_ZKEY))
2654 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2655 "Requested connection is gnunet tld\n",
2659 if (ntohs(req_port) == HTTPS_PORT)
2661 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2662 "Requested connection is HTTPS\n");
2663 ret = add_handle_to_ssl_mhd ( s5r->sock, domain );
2665 else if (NULL != httpd)
2667 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2668 "Requested connection is HTTP\n");
2669 nh = GNUNET_malloc (sizeof (struct NetworkHandleList));
2672 GNUNET_CONTAINER_DLL_insert (mhd_httpd_head->socket_handles_head,
2673 mhd_httpd_head->socket_handles_tail,
2676 ret = add_handle_to_mhd ( s5r->sock, httpd );
2681 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2682 _("Failed to start HTTP server\n"));
2683 s_resp->version = 0x05;
2684 s_resp->reply = 0x01;
2685 s5r->cleanup = GNUNET_YES;
2686 s5r->cleanup_sock = GNUNET_YES;
2688 GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
2694 /* Signal success */
2695 s_resp->version = 0x05;
2696 s_resp->reply = 0x00;
2697 s_resp->reserved = 0x00;
2698 s_resp->addr_type = 0x01;
2700 s5r->cleanup = GNUNET_YES;
2701 s5r->cleanup_sock = GNUNET_NO;
2703 GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
2711 phost = (struct hostent*)gethostbyname (domain);
2714 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2715 "Resolve %s error!\n", domain );
2716 s_resp->version = 0x05;
2717 s_resp->reply = 0x01;
2718 s5r->cleanup = GNUNET_YES;
2719 s5r->cleanup_sock = GNUNET_YES;
2721 GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
2727 s5r->remote_sock = GNUNET_NETWORK_socket_create (AF_INET,
2730 r_sin_addr = (struct in_addr*)(phost->h_addr);
2731 remote_ip = r_sin_addr->s_addr;
2732 memset(&remote_addr, 0, sizeof(remote_addr));
2733 remote_addr.sin_family = AF_INET;
2734 #if HAVE_SOCKADDR_IN_SIN_LEN
2735 remote_addr.sin_len = sizeof (remote_addr);
2737 remote_addr.sin_addr.s_addr = remote_ip;
2738 remote_addr.sin_port = req_port;
2740 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2741 "target server: %s:%u\n", inet_ntoa(remote_addr.sin_addr),
2745 GNUNET_NETWORK_socket_connect ( s5r->remote_sock,
2746 (const struct sockaddr*)&remote_addr,
2747 sizeof (remote_addr)))
2748 && (errno != EINPROGRESS))
2750 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "connect");
2751 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2752 "socket request error...\n");
2753 s_resp->version = 0x05;
2754 s_resp->reply = 0x01;
2756 GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
2763 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2764 "new remote connection\n");
2766 s_resp->version = 0x05;
2767 s_resp->reply = 0x00;
2768 s_resp->reserved = 0x00;
2769 s_resp->addr_type = 0x01;
2771 s5r->state = SOCKS5_DATA_TRANSFER;
2774 GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
2778 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
2786 if (s5r->state == SOCKS5_DATA_TRANSFER)
2788 if ((s5r->remote_sock == NULL) || (s5r->rbuf_len == 0))
2790 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2791 "Closing connection to client\n");
2792 if (s5r->rtask != GNUNET_SCHEDULER_NO_TASK)
2793 GNUNET_SCHEDULER_cancel (s5r->rtask);
2794 if (s5r->fwdwtask != GNUNET_SCHEDULER_NO_TASK)
2795 GNUNET_SCHEDULER_cancel (s5r->fwdwtask);
2796 if (s5r->fwdrtask != GNUNET_SCHEDULER_NO_TASK)
2797 GNUNET_SCHEDULER_cancel (s5r->fwdrtask);
2798 if (s5r->fwdrtask != GNUNET_SCHEDULER_NO_TASK)
2799 GNUNET_SCHEDULER_cancel (s5r->fwdrtask);
2801 if (s5r->remote_sock != NULL)
2802 GNUNET_NETWORK_socket_close (s5r->remote_sock);
2803 GNUNET_NETWORK_socket_close (s5r->sock);
2808 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2809 "forwarding %d bytes from client\n", s5r->rbuf_len);
2812 GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
2814 &do_write_remote, s5r);
2816 if (s5r->fwdrtask == GNUNET_SCHEDULER_NO_TASK)
2819 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
2821 &do_read_remote, s5r);
2831 * Accept new incoming connections
2833 * @param cls the closure
2834 * @param tc the scheduler context
2837 do_accept (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2839 struct GNUNET_NETWORK_Handle *s;
2840 struct Socks5Request *s5r;
2842 ltask = GNUNET_SCHEDULER_NO_TASK;
2843 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
2846 ltask = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
2850 s = GNUNET_NETWORK_socket_accept (lsock, NULL, NULL);
2854 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "accept");
2858 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2859 "Got an inbound connection, waiting for data\n");
2861 s5r = GNUNET_malloc (sizeof (struct Socks5Request));
2863 s5r->state = SOCKS5_INIT;
2864 s5r->wtask = GNUNET_SCHEDULER_NO_TASK;
2865 s5r->fwdwtask = GNUNET_SCHEDULER_NO_TASK;
2866 s5r->fwdrtask = GNUNET_SCHEDULER_NO_TASK;
2867 s5r->rtask = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
2874 * Task run on shutdown
2876 * @param cls closure
2877 * @param tc task context
2880 do_shutdown (void *cls,
2881 const struct GNUNET_SCHEDULER_TaskContext *tc)
2884 struct MhdHttpList *hd;
2885 struct MhdHttpList *tmp_hd;
2886 struct NetworkHandleList *nh;
2887 struct NetworkHandleList *tmp_nh;
2888 struct ProxyCurlTask *ctask;
2889 struct ProxyCurlTask *ctask_tmp;
2891 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2892 "Shutting down...\n");
2894 gnutls_global_deinit ();
2896 if (NULL != local_gns_zone)
2897 GNUNET_free (local_gns_zone);
2898 if (NULL != local_private_zone)
2899 GNUNET_free (local_private_zone);
2900 if (NULL != local_shorten_zone)
2901 GNUNET_free (local_shorten_zone);
2903 if (GNUNET_SCHEDULER_NO_TASK != curl_download_task)
2905 GNUNET_SCHEDULER_cancel (curl_download_task);
2906 curl_download_task = GNUNET_SCHEDULER_NO_TASK;
2909 for (hd = mhd_httpd_head; hd != NULL; hd = tmp_hd)
2913 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2914 "Stopping daemon\n");
2916 if (GNUNET_SCHEDULER_NO_TASK != hd->httpd_task)
2918 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2919 "Stopping select task %d\n",
2921 GNUNET_SCHEDULER_cancel (hd->httpd_task);
2922 hd->httpd_task = GNUNET_SCHEDULER_NO_TASK;
2925 if (NULL != hd->daemon)
2927 MHD_stop_daemon (hd->daemon);
2931 for (nh = hd->socket_handles_head; nh != NULL; nh = tmp_nh)
2935 GNUNET_NETWORK_socket_close (nh->h);
2940 if (NULL != hd->proxy_cert)
2942 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2943 "Free certificate\n");
2944 GNUNET_free (hd->proxy_cert);
2950 for (ctask=ctasks_head; ctask != NULL; ctask=ctask_tmp)
2952 ctask_tmp = ctask->next;
2954 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2955 "Cleaning up cURL task\n");
2957 if (ctask->curl != NULL)
2958 curl_easy_cleanup (ctask->curl);
2960 if (NULL != ctask->headers)
2961 curl_slist_free_all (ctask->headers);
2962 if (NULL != ctask->resolver)
2963 curl_slist_free_all (ctask->resolver);
2965 if (NULL != ctask->response)
2966 MHD_destroy_response (ctask->response);
2969 GNUNET_free (ctask);
2971 curl_multi_cleanup (curl_multi);
2973 GNUNET_GNS_disconnect (gns_handle);
2978 * Compiles a regex for us
2980 * @param re ptr to re struct
2981 * @param rt the expression to compile
2982 * @return 0 on success
2985 compile_regex (regex_t *re, const char* rt)
2990 status = regcomp (re, rt, REG_EXTENDED|REG_NEWLINE);
2993 regerror (status, re, err, 1024);
2994 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2995 "Regex error compiling '%s': %s\n", rt, err);
3003 * Loads the users local zone key
3005 * @return GNUNET_YES on success
3008 load_local_zone_key (const struct GNUNET_CONFIGURATION_Handle *cfg)
3011 struct GNUNET_CRYPTO_RsaPrivateKey *key = NULL;
3012 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pkey;
3013 struct GNUNET_CRYPTO_ShortHashCode *zone = NULL;
3014 struct GNUNET_CRYPTO_ShortHashAsciiEncoded zonename;
3016 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "gns",
3017 "ZONEKEY", &keyfile))
3019 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3020 "Unable to load zone key config value!\n");
3024 if (GNUNET_NO == GNUNET_DISK_file_test (keyfile))
3026 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3027 "Unable to load zone key %s!\n", keyfile);
3028 GNUNET_free(keyfile);
3032 key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
3033 GNUNET_CRYPTO_rsa_key_get_public (key, &pkey);
3034 local_gns_zone = GNUNET_malloc (sizeof (struct GNUNET_CRYPTO_ShortHashCode));
3035 GNUNET_CRYPTO_short_hash(&pkey,
3036 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
3038 zone = local_gns_zone;
3039 GNUNET_CRYPTO_short_hash_to_enc (zone, &zonename);
3040 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3041 "Using zone: %s!\n", &zonename);
3042 GNUNET_CRYPTO_rsa_key_free(key);
3043 GNUNET_free(keyfile);
3046 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "gns",
3047 "PRIVATE_ZONEKEY", &keyfile))
3049 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3050 "Unable to load private zone key config value!\n");
3053 if ((NULL != keyfile) && (GNUNET_NO == GNUNET_DISK_file_test (keyfile)))
3055 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3056 "Unable to load private zone key %s!\n", keyfile);
3057 GNUNET_free(keyfile);
3061 key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
3062 GNUNET_CRYPTO_rsa_key_get_public (key, &pkey);
3063 local_private_zone = GNUNET_malloc (sizeof (struct GNUNET_CRYPTO_ShortHashCode));
3064 GNUNET_CRYPTO_short_hash(&pkey,
3065 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
3066 local_private_zone);
3067 GNUNET_CRYPTO_short_hash_to_enc (zone, &zonename);
3068 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3069 "Using private zone: %s!\n", &zonename);
3070 GNUNET_CRYPTO_rsa_key_free(key);
3071 GNUNET_free(keyfile);
3075 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "gns",
3076 "SHORTEN_ZONEKEY", &keyfile))
3078 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3079 "Unable to load shorten zone key config value!\n");
3082 if ((NULL != keyfile) && (GNUNET_NO == GNUNET_DISK_file_test (keyfile)))
3084 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3085 "Unable to load shorten zone key %s!\n", keyfile);
3086 GNUNET_free(keyfile);
3090 key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
3091 GNUNET_CRYPTO_rsa_key_get_public (key, &pkey);
3092 local_shorten_zone = GNUNET_malloc (sizeof (struct GNUNET_CRYPTO_ShortHashCode));
3093 GNUNET_CRYPTO_short_hash(&pkey,
3094 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
3095 local_shorten_zone);
3096 GNUNET_CRYPTO_short_hash_to_enc (zone, &zonename);
3097 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3098 "Using shorten zone: %s!\n", &zonename);
3099 GNUNET_CRYPTO_rsa_key_free(key);
3100 GNUNET_free(keyfile);
3107 * Main function that will be run
3109 * @param cls closure
3110 * @param args remaining command-line arguments
3111 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
3112 * @param cfg configuration
3115 run (void *cls, char *const *args, const char *cfgfile,
3116 const struct GNUNET_CONFIGURATION_Handle *cfg)
3118 struct sockaddr_in sa;
3119 struct MhdHttpList *hd;
3120 char* cafile_cfg = NULL;
3122 #if !HAVE_MHD_NO_LISTEN_SOCKET
3124 char* proxy_sockfile;
3125 struct sockaddr_un mhd_unix_sock_addr;
3128 curl_multi = curl_multi_init ();
3130 if (NULL == curl_multi)
3132 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3133 "Failed to create cURL multo handle!\n");
3137 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3139 cafile = cafile_opt;
3142 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "gns-proxy",
3146 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3147 "Unable to load proxy CA config value!\n");
3148 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3149 "No proxy CA provided!\n");
3152 cafile = cafile_cfg;
3154 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3155 "Using %s as CA\n", cafile);
3157 gnutls_global_init ();
3158 gnutls_x509_crt_init (&proxy_ca.cert);
3159 gnutls_x509_privkey_init (&proxy_ca.key);
3161 if ( (GNUNET_OK != load_cert_from_file (proxy_ca.cert, cafile)) ||
3162 (GNUNET_OK != load_key_from_file (proxy_ca.key, cafile)) )
3164 // FIXME: release resources...
3168 GNUNET_free_non_null (cafile_cfg);
3170 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3171 "Loading Template\n");
3173 compile_regex (&re_dotplus, (char*) RE_A_HREF);
3175 gns_handle = GNUNET_GNS_connect (cfg);
3177 if (GNUNET_NO == load_local_zone_key (cfg))
3179 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3180 "Unable to load zone!\n");
3184 if (NULL == gns_handle)
3186 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3187 "Unable to connect to GNS!\n");
3191 memset (&sa, 0, sizeof (sa));
3192 sa.sin_family = AF_INET;
3193 sa.sin_port = htons (port);
3194 #if HAVE_SOCKADDR_IN_SIN_LEN
3195 sa.sin_len = sizeof (sa);
3198 lsock = GNUNET_NETWORK_socket_create (AF_INET,
3202 if ((NULL == lsock) ||
3204 GNUNET_NETWORK_socket_bind (lsock, (const struct sockaddr *) &sa,
3207 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3208 "Failed to create listen socket bound to `%s'",
3209 GNUNET_a2s ((const struct sockaddr *) &sa, sizeof (sa)));
3211 GNUNET_NETWORK_socket_close (lsock);
3215 if (GNUNET_OK != GNUNET_NETWORK_socket_listen (lsock, 5))
3217 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3218 "Failed to listen on socket bound to `%s'",
3219 GNUNET_a2s ((const struct sockaddr *) &sa, sizeof (sa)));
3223 ltask = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
3224 lsock, &do_accept, NULL);
3229 if (0 != curl_global_init (CURL_GLOBAL_WIN32))
3231 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3232 "cURL global init failed!\n");
3236 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3237 "Proxy listens on port %u\n",
3240 mhd_httpd_head = NULL;
3241 mhd_httpd_tail = NULL;
3242 total_mhd_connections = 0;
3243 #ifndef HAVE_MHD_NO_LISTEN_SOCKET
3244 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "gns-proxy",
3248 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3249 "Specify PROXY_UNIXPATH in gns-proxy config section!\n");
3253 mhd_unix_socket = GNUNET_NETWORK_socket_create (AF_UNIX,
3257 if (NULL == mhd_unix_socket)
3259 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3260 "Unable to create unix domain socket!\n");
3264 mhd_unix_sock_addr.sun_family = AF_UNIX;
3265 strcpy (mhd_unix_sock_addr.sun_path, proxy_sockfile);
3268 mhd_unix_sock_addr.sun_path[0] = '\0';
3270 #if HAVE_SOCKADDR_IN_SIN_LEN
3271 mhd_unix_sock_addr.sun_len = (u_char) sizeof (struct sockaddr_un);
3274 len = strlen (proxy_sockfile) + sizeof(AF_UNIX);
3276 GNUNET_free (proxy_sockfile);
3278 if (GNUNET_OK != GNUNET_NETWORK_socket_bind (mhd_unix_socket,
3279 (struct sockaddr*)&mhd_unix_sock_addr,
3282 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3283 "Unable to bind unix domain socket!\n");
3287 if (GNUNET_OK != GNUNET_NETWORK_socket_listen (mhd_unix_socket,
3290 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3291 "Unable to listen on unix domain socket!\n");
3296 hd = GNUNET_malloc (sizeof (struct MhdHttpList));
3297 hd->is_ssl = GNUNET_NO;
3298 strcpy (hd->domain, "");
3299 httpd = MHD_start_daemon (MHD_USE_DEBUG
3300 #if HAVE_MHD_NO_LISTEN_SOCKET
3301 | MHD_USE_NO_LISTEN_SOCKET,
3304 , 4444, //Dummy port
3307 &create_response, hd,
3308 #if !HAVE_MHD_NO_LISTEN_SOCKET
3309 MHD_OPTION_LISTEN_SOCKET, GNUNET_NETWORK_get_fd (mhd_unix_socket),
3311 MHD_OPTION_CONNECTION_LIMIT, MHD_MAX_CONNECTIONS,
3312 MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) 16,
3313 MHD_OPTION_NOTIFY_COMPLETED,
3315 MHD_OPTION_URI_LOG_CALLBACK, &mhd_log_callback, NULL,
3318 GNUNET_assert (httpd != NULL);
3320 hd->httpd_task = GNUNET_SCHEDULER_NO_TASK;
3322 GNUNET_CONTAINER_DLL_insert (mhd_httpd_head, mhd_httpd_tail, hd);
3326 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
3327 &do_shutdown, NULL);
3333 * The main function for gnunet-gns-proxy.
3335 * @param argc number of arguments from the command line
3336 * @param argv command line arguments
3337 * @return 0 ok, 1 on error
3340 main (int argc, char *const *argv)
3342 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
3344 gettext_noop ("listen on specified port"), 1,
3345 &GNUNET_GETOPT_set_string, &port},
3346 {'a', "authority", NULL,
3347 gettext_noop ("pem file to use as CA"), 1,
3348 &GNUNET_GETOPT_set_string, &cafile_opt},
3349 GNUNET_GETOPT_OPTION_END
3354 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
3358 GNUNET_log_setup ("gnunet-gns-proxy", "WARNING", NULL);
3361 GNUNET_PROGRAM_run (argc, argv, "gnunet-gns-proxy",
3362 _("GNUnet GNS proxy"),
3364 &run, NULL)) ? 0 : 1;
3365 GNUNET_free_non_null ((char*)argv);