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 /* is curl running? */
203 /* Optional header replacements for curl (LEHO) */
204 struct curl_slist *headers;
206 /* Optional resolver replacements for curl (LEHO) */
207 struct curl_slist *resolver;
209 /* curl response code */
210 long curl_response_code;
212 /* The URL to fetch */
213 char url[MAX_HTTP_URI_LENGTH];
215 /* The cURL write buffer / MHD read buffer */
216 char buffer[CURL_MAX_WRITE_SIZE + CURL_BUF_PADDING];
218 /* Read pos of the data in the buffer */
219 char *buffer_read_ptr;
221 /* Write pos in the buffer */
222 char *buffer_write_ptr;
224 /* The buffer status (BUF_WAIT_FOR_CURL or BUF_WAIT_FOR_MHD) */
227 /* Number of bytes in buffer */
228 unsigned int bytes_in_buffer;
230 /* Indicates wheather the download is in progress */
231 int download_in_progress;
233 /* Indicates wheather the download was successful */
234 int download_is_finished;
236 /* Indicates wheather the download failed */
239 /* Indicates wheather we need to parse HTML */
242 /* Indicates wheather we are postprocessing the HTML right now */
243 int is_postprocessing;
245 /* Indicates wheather postprocessing has finished */
249 GNUNET_SCHEDULER_TaskIdentifier pp_task;
252 struct ProxyREMatch *pp_match_head;
255 struct ProxyREMatch *pp_match_tail;
257 /* The authority of the corresponding host (site of origin) */
260 /* The hostname (Host header field) */
266 /* The LEgacy HOstname (can be empty) */
269 /* The associated daemon list entry */
270 struct MhdHttpList *mhd;
272 /* The associated response */
273 struct MHD_Response *response;
276 struct ProxySetCookieHeader *set_cookies_head;
279 struct ProxySetCookieHeader *set_cookies_tail;
281 /* connection status */
288 struct MHD_Connection *connection;
291 size_t put_read_offset;
292 size_t put_read_size;
295 struct MHD_PostProcessor *post_handler;
298 struct ProxyUploadData *upload_data_head;
299 struct ProxyUploadData *upload_data_tail;
303 /* the type of POST encoding */
306 struct curl_httppost *httppost;
308 struct curl_httppost *httppost_last;
315 * Struct for RE matches in postprocessing of HTML
320 struct ProxyREMatch *next;
323 struct ProxyREMatch *prev;
332 struct GNUNET_GNS_ShortenRequest *shorten_task;
337 /* start of match in buffer */
340 /* end of match in buffer */
343 /* associated proxycurltask */
344 struct ProxyCurlTask *ctask;
348 * Struct for set-cookies
350 struct ProxySetCookieHeader
353 struct ProxySetCookieHeader *next;
356 struct ProxySetCookieHeader *prev;
363 * Post data structure
365 struct ProxyUploadData
368 struct ProxyUploadData *next;
371 struct ProxyUploadData *prev;
379 size_t content_length;
392 /* The port the proxy is running on (default 7777) */
393 static unsigned long port = GNUNET_GNS_PROXY_PORT;
395 /* The CA file (pem) to use for the proxy CA */
396 static char* cafile_opt;
398 /* The listen socket of the proxy */
399 static struct GNUNET_NETWORK_Handle *lsock;
401 /* The listen task ID */
402 GNUNET_SCHEDULER_TaskIdentifier ltask;
404 /* The cURL download task */
405 GNUNET_SCHEDULER_TaskIdentifier curl_download_task;
407 /* The non SSL httpd daemon handle */
408 static struct MHD_Daemon *httpd;
410 /* Number of current mhd connections */
411 static unsigned int total_mhd_connections;
413 /* The cURL multi handle */
414 static CURLM *curl_multi;
416 /* Handle to the GNS service */
417 static struct GNUNET_GNS_Handle *gns_handle;
419 /* DLL for ProxyCurlTasks */
420 static struct ProxyCurlTask *ctasks_head;
422 /* DLL for ProxyCurlTasks */
423 static struct ProxyCurlTask *ctasks_tail;
425 /* DLL for http daemons */
426 static struct MhdHttpList *mhd_httpd_head;
428 /* DLL for http daemons */
429 static struct MhdHttpList *mhd_httpd_tail;
431 /* Handle to the regex for dotplus (.+) replacement in HTML */
432 static regex_t re_dotplus;
434 /* The users local GNS zone hash */
435 static struct GNUNET_CRYPTO_ShortHashCode *local_gns_zone;
437 /* The users local private zone */
438 static struct GNUNET_CRYPTO_ShortHashCode *local_private_zone;
440 /* The users local shorten zone */
441 static struct GNUNET_CRYPTO_ShortHashCode *local_shorten_zone;
443 /* The CA for SSL certificate generation */
444 static struct ProxyCA proxy_ca;
446 /* UNIX domain socket for mhd */
447 struct GNUNET_NETWORK_Handle *mhd_unix_socket;
449 /* Shorten zone private key */
450 struct GNUNET_CRYPTO_RsaPrivateKey *shorten_zonekey;
453 * Checks if name is in tld
455 * @param name the name to check
456 * @param tld the TLD to check for
457 * @return GNUNET_YES or GNUNET_NO
460 is_tld(const char* name, const char* tld)
464 if (strlen(name) <= strlen(tld))
467 offset = strlen(name) - strlen(tld);
468 if (0 != strcmp (name+offset, tld))
470 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
471 "%s is not in .%s TLD\n", name, tld);
479 * convert integer to string representation
482 * @return the character
484 char i_to_hexchar (char i)
486 static char hexmap[] = "0123456789abcdef";
487 GNUNET_assert (sizeof (hexmap) > (i & 15));
488 return hexmap[i & 15];
492 * Escape given 0-terminated string
494 * @param to_esc string to escapse
495 * @return allocated new escaped string (MUST free!)
498 escape_to_urlenc (const char *to_esc)
500 char *pos = (char*)to_esc;
501 char *res = GNUNET_malloc (strlen (to_esc) * 3 + 1);
506 if (isalnum (*pos) ||
507 ('-' == *pos) || ('_' == *pos) ||
508 ('.' == *pos) || ('~' == *pos))
510 else if (' ' == *pos)
515 *rpos++ = i_to_hexchar (*pos >> 4);
516 *rpos++ = i_to_hexchar (*pos >> 15);
525 con_post_data_iter (void *cls,
526 enum MHD_ValueKind kind,
528 const char *filename,
529 const char *content_type,
530 const char *transfer_encoding,
535 struct ProxyCurlTask* ctask = cls;
536 struct ProxyUploadData* pdata;
540 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
541 "Got POST data: '%s : %s' at offset %llu size %lld\n",
542 key, data, off, size);
544 GNUNET_assert (NULL != ctask->post_type);
546 if (0 == strcasecmp (MHD_HTTP_POST_ENCODING_MULTIPART_FORMDATA,
549 ctask->is_httppost = GNUNET_YES;
553 pdata = GNUNET_malloc (sizeof (struct ProxyUploadData));
554 pdata->key = strdup (key);
556 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
559 if (NULL != filename)
561 pdata->filename = strdup (filename);
562 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
563 "Filename %s\n", filename);
566 if (NULL != content_type)
568 pdata->content_type = strdup (content_type);
569 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
570 "Content-Type %s\n", content_type);
573 pdata->value = GNUNET_malloc (size);
574 pdata->total_bytes = size;
575 memcpy (pdata->value, data, size);
576 GNUNET_CONTAINER_DLL_insert_tail (ctask->upload_data_head,
577 ctask->upload_data_tail,
580 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
581 "Copied %lld bytes of POST Data\n", size);
585 pdata = ctask->upload_data_tail;
586 new_value = GNUNET_malloc (size + pdata->total_bytes);
587 memcpy (new_value, pdata->value, pdata->total_bytes);
588 memcpy (new_value+off, data, size);
589 GNUNET_free (pdata->value);
590 pdata->total_bytes += size;
596 if (0 != strcasecmp (MHD_HTTP_POST_ENCODING_FORM_URLENCODED,
602 ctask->is_httppost = GNUNET_NO;
604 if (NULL != ctask->curl)
605 curl_easy_pause (ctask->curl, CURLPAUSE_CONT);
610 pdata = GNUNET_malloc (sizeof (struct ProxyUploadData));
611 enc = escape_to_urlenc (key);
612 pdata->value = GNUNET_malloc (strlen (enc) + 3);
613 if (NULL != ctask->upload_data_head)
615 pdata->value[0] = '&';
616 memcpy (pdata->value+1, enc, strlen (enc));
619 memcpy (pdata->value, enc, strlen (enc));
620 pdata->value[strlen (pdata->value)] = '=';
621 pdata->bytes_left = strlen (pdata->value);
622 pdata->total_bytes = pdata->bytes_left;
625 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
626 "Escaped POST key: '%s'\n",
629 GNUNET_CONTAINER_DLL_insert_tail (ctask->upload_data_head,
630 ctask->upload_data_tail,
635 pdata = GNUNET_malloc (sizeof (struct ProxyUploadData));
636 enc = escape_to_urlenc (data);
637 pdata->value = GNUNET_malloc (strlen (enc) + 1);
638 memcpy (pdata->value, enc, strlen (enc));
639 pdata->bytes_left = strlen (pdata->value);
640 pdata->total_bytes = pdata->bytes_left;
643 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
644 "Escaped POST value: '%s'\n",
647 GNUNET_CONTAINER_DLL_insert_tail (ctask->upload_data_head,
648 ctask->upload_data_tail,
655 * Read HTTP request header field 'Host'
657 * @param cls buffer to write to
658 * @param kind value kind
659 * @param key field key
660 * @param value field value
661 * @return MHD_NO when Host found
664 con_val_iter (void *cls,
665 enum MHD_ValueKind kind,
669 struct ProxyCurlTask *ctask = cls;
670 char* buf = ctask->host;
676 if (0 == strcmp ("Host", key))
678 port = strstr (value, ":");
681 strncpy (buf, value, port-value);
683 if ((1 != sscanf (port, "%u", &uport)) ||
684 (uport > UINT16_MAX) ||
686 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
687 "Unable to parse port!\n");
689 ctask->port = (uint16_t) uport;
696 if (0 == strcmp ("Accept-Encoding", key))
701 if (0 == strcasecmp (MHD_HTTP_HEADER_CONTENT_TYPE,
704 if (0 == strncasecmp (value,
705 MHD_HTTP_POST_ENCODING_FORM_URLENCODED,
706 strlen (MHD_HTTP_POST_ENCODING_FORM_URLENCODED)))
707 ctask->post_type = MHD_HTTP_POST_ENCODING_FORM_URLENCODED;
708 else if (0 == strncasecmp (value,
709 MHD_HTTP_POST_ENCODING_MULTIPART_FORMDATA,
710 strlen (MHD_HTTP_POST_ENCODING_MULTIPART_FORMDATA)))
711 ctask->post_type = MHD_HTTP_POST_ENCODING_MULTIPART_FORMDATA;
713 ctask->post_type = NULL;
717 cstr = GNUNET_malloc (strlen (key) + strlen (hdr_val) + 3);
718 GNUNET_snprintf (cstr, strlen (key) + strlen (hdr_val) + 3,
719 "%s: %s", key, hdr_val);
721 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
722 "Client Header: %s\n", cstr);
724 ctask->headers = curl_slist_append (ctask->headers, cstr);
732 * Callback for MHD response
735 * @param pos in buffer
737 * @param max space in buffer
738 * @return number of bytes written
741 mhd_content_cb (void *cls,
747 * Check HTTP response header for mime
749 * @param buffer curl buffer
750 * @param size curl blocksize
751 * @param nmemb curl blocknumber
753 * @return size of read bytes
756 curl_check_hdr (void *buffer, size_t size, size_t nmemb, void *cls)
758 size_t bytes = size * nmemb;
759 struct ProxyCurlTask *ctask = cls;
760 int html_mime_len = strlen (HTML_HDR_CONTENT);
761 int cookie_hdr_len = strlen (MHD_HTTP_HEADER_SET_COOKIE);
762 char hdr_mime[html_mime_len+1];
763 char hdr_generic[bytes+1];
764 char new_cookie_hdr[bytes+strlen (ctask->leho)+1];
765 char new_location[MAX_HTTP_URI_LENGTH+500];
776 if (NULL == ctask->response)
778 /* FIXME: get total size from curl (if available) */
779 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
780 "Creating response for %s\n", ctask->url);
781 ctask->response = MHD_create_response_from_callback (MHD_SIZE_UNKNOWN,
782 sizeof (ctask->buffer),
786 ctask->ready_to_queue = GNUNET_YES;
790 if (html_mime_len <= bytes)
792 memcpy (hdr_mime, buffer, html_mime_len);
793 hdr_mime[html_mime_len] = '\0';
795 if (0 == strcmp (hdr_mime, HTML_HDR_CONTENT))
797 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
798 "Got HTML HTTP response header\n");
799 ctask->parse_content = GNUNET_YES;
803 if (cookie_hdr_len > bytes)
806 memcpy (hdr_generic, buffer, bytes);
807 hdr_generic[bytes] = '\0';
809 if ('\n' == hdr_generic[bytes-1])
810 hdr_generic[bytes-1] = '\0';
812 if (hdr_generic[bytes-2] == '\r')
813 hdr_generic[bytes-2] = '\0';
815 if (0 == memcmp (hdr_generic,
816 MHD_HTTP_HEADER_SET_COOKIE,
819 ndup = GNUNET_strdup (hdr_generic+cookie_hdr_len+1);
820 memset (new_cookie_hdr, 0, sizeof (new_cookie_hdr));
821 tok = strtok (ndup, ";");
823 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
824 "Looking for cookie in : %s\n", hdr_generic);
826 for (; tok != NULL; tok = strtok (NULL, ";"))
828 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
829 "Got Cookie token: %s\n", tok);
830 //memcpy (new_cookie_hdr+offset, tok, strlen (tok));
831 if (0 == memcmp (tok, " domain", strlen (" domain")))
833 cookie_domain = tok + strlen (" domain") + 1;
835 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
836 "Got Set-Cookie Domain: %s\n", cookie_domain);
838 if (strlen (cookie_domain) < strlen (ctask->leho))
840 delta_cdomain = strlen (ctask->leho) - strlen (cookie_domain);
841 if (0 == strcmp (cookie_domain, ctask->leho + (delta_cdomain)))
843 GNUNET_snprintf (new_cookie_hdr+offset,
844 sizeof (new_cookie_hdr),
845 " domain=%s", ctask->authority);
846 offset += strlen (" domain=") + strlen (ctask->authority);
847 new_cookie_hdr[offset] = ';';
852 else if (strlen (cookie_domain) == strlen (ctask->leho))
854 if (0 == strcmp (cookie_domain, ctask->leho))
856 GNUNET_snprintf (new_cookie_hdr+offset,
857 sizeof (new_cookie_hdr),
858 " domain=%s", ctask->host);
859 offset += strlen (" domain=") + strlen (ctask->host);
860 new_cookie_hdr[offset] = ';';
865 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
866 "Cookie domain invalid\n");
870 memcpy (new_cookie_hdr+offset, tok, strlen (tok));
871 offset += strlen (tok);
872 new_cookie_hdr[offset] = ';';
878 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
879 "Got Set-Cookie HTTP header %s\n", new_cookie_hdr);
881 if (GNUNET_NO == MHD_add_response_header (ctask->response,
882 MHD_HTTP_HEADER_SET_COOKIE,
885 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
886 "MHD: Error adding set-cookie header field %s\n",
887 hdr_generic+cookie_hdr_len+1);
892 ndup = GNUNET_strdup (hdr_generic);
893 hdr_type = strtok (ndup, ":");
895 if (NULL == hdr_type)
901 hdr_val = strtok (NULL, "");
911 if (0 == strcasecmp (MHD_HTTP_HEADER_LOCATION, hdr_type))
913 if (ctask->mhd->is_ssl)
915 sprintf (leho_host, "https://%s", ctask->leho);
916 sprintf (real_host, "https://%s", ctask->host);
920 sprintf (leho_host, "http://%s", ctask->leho);
921 sprintf (real_host, "http://%s", ctask->host);
924 if (0 == memcmp (leho_host, hdr_val, strlen (leho_host)))
926 sprintf (new_location, "%s%s", real_host, hdr_val+strlen (leho_host));
927 hdr_val = new_location;
931 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
932 "Trying to set %s: %s\n",
935 if (GNUNET_NO == MHD_add_response_header (ctask->response,
939 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
940 "MHD: Error adding %s header field %s\n",
951 * @param hd a http daemon list entry
954 run_httpd (struct MhdHttpList *hd);
965 * Task run whenever HTTP server operations are pending.
968 * @param tc sched context
972 const struct GNUNET_SCHEDULER_TaskContext *tc);
975 run_mhd_now (struct MhdHttpList *hd)
977 if (GNUNET_SCHEDULER_NO_TASK != hd->httpd_task)
979 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
980 "MHD: killing old task\n");
981 GNUNET_SCHEDULER_cancel (hd->httpd_task);
983 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
984 "MHD: Scheduling MHD now\n");
985 hd->httpd_task = GNUNET_SCHEDULER_add_now (&do_httpd, hd);
989 * Ask cURL for the select sets and schedule download
992 curl_download_prepare ();
995 * Callback to free content
997 * @param cls content to free
998 * @param tc task context
1001 mhd_content_free (void *cls,
1002 const struct GNUNET_SCHEDULER_TaskContext *tc)
1004 struct ProxyCurlTask *ctask = cls;
1005 struct ProxyUploadData *pdata;
1007 GNUNET_assert (NULL == ctask->pp_match_head);
1008 if (NULL != ctask->headers)
1009 curl_slist_free_all (ctask->headers);
1011 if (NULL != ctask->headers)
1012 curl_slist_free_all (ctask->resolver);
1014 if (NULL != ctask->response)
1015 MHD_destroy_response (ctask->response);
1017 if (NULL != ctask->post_handler)
1018 MHD_destroy_post_processor (ctask->post_handler);
1020 for (pdata = ctask->upload_data_head;; NULL != pdata; pdata = ctask->upload_data_head)
1022 GNUNET_CONTAINER_DLL_remove (ctask->upload_data_head,
1023 ctask->upload_data_tail,
1025 GNUNET_free_non_null (pdata->filename);
1026 GNUNET_free_non_null (pdata->content_type);
1027 GNUNET_free_non_null (pdata->key);
1028 GNUNET_free_non_null (pdata->value);
1029 GNUNET_free (pdata);
1031 GNUNET_free (ctask);
1036 * Callback for MHD response
1038 * @param cls closure
1039 * @param pos in buffer
1041 * @param max space in buffer
1042 * @return number of bytes written
1045 mhd_content_cb (void *cls,
1050 struct ProxyCurlTask *ctask = cls;
1051 struct ProxyREMatch *re_match;
1053 long long int bytes_to_copy = ctask->buffer_write_ptr - ctask->buffer_read_ptr;
1055 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1056 "MHD: content cb for %s. To copy: %lld\n",
1057 ctask->url, bytes_to_copy);
1058 GNUNET_assert (bytes_to_copy >= 0);
1060 if ((GNUNET_YES == ctask->download_is_finished) &&
1061 (GNUNET_NO == ctask->download_error) &&
1062 (0 == bytes_to_copy)) /* &&
1063 (BUF_WAIT_FOR_CURL == ctask->buf_status))*/
1065 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1066 "MHD: sending response for %s\n", ctask->url);
1067 ctask->download_in_progress = GNUNET_NO;
1068 run_mhd_now (ctask->mhd);
1069 GNUNET_SCHEDULER_add_now (&mhd_content_free, ctask);
1070 total_mhd_connections--;
1071 return MHD_CONTENT_READER_END_OF_STREAM;
1074 if ((GNUNET_YES == ctask->download_error) &&
1075 (GNUNET_YES == ctask->download_is_finished) &&
1076 (0 == bytes_to_copy)) /* &&
1077 (BUF_WAIT_FOR_CURL == ctask->buf_status))*/
1079 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1080 "MHD: sending error response\n");
1081 ctask->download_in_progress = GNUNET_NO;
1082 run_mhd_now (ctask->mhd);
1083 GNUNET_SCHEDULER_add_now (&mhd_content_free, ctask);
1084 total_mhd_connections--;
1085 return MHD_CONTENT_READER_END_WITH_ERROR;
1088 if ( ctask->buf_status == BUF_WAIT_FOR_CURL )
1092 for (re_match = ctask->pp_match_head; NULL != re_match; re_match = ctask->pp_match_head)
1094 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1095 "MHD: Processing PP %s\n",
1096 re_match->hostname);
1097 bytes_to_copy = re_match->start - ctask->buffer_read_ptr;
1098 GNUNET_assert (bytes_to_copy >= 0);
1100 if (bytes_to_copy+copied > max)
1102 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1103 "MHD: buffer in response too small for %d. Using available space (%d). (%s)\n",
1107 memcpy (buf+copied, ctask->buffer_read_ptr, max-copied);
1108 ctask->buffer_read_ptr += max-copied;
1110 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1111 "MHD: copied %d bytes\n", copied);
1115 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1116 "MHD: copying %d bytes to mhd response at offset %d\n",
1117 bytes_to_copy, ctask->buffer_read_ptr);
1118 memcpy (buf+copied, ctask->buffer_read_ptr, bytes_to_copy);
1119 copied += bytes_to_copy;
1121 if (GNUNET_NO == re_match->done)
1123 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1124 "MHD: Waiting for PP of %s\n", re_match->hostname);
1125 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1126 "MHD: copied %d bytes\n", copied);
1127 ctask->buffer_read_ptr += bytes_to_copy;
1131 if (strlen (re_match->result) > (max - copied))
1133 //FIXME partially copy domain here
1134 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1135 "MHD: buffer in response too small for %s! (%s)\n",
1138 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1139 "MHD: copied %d bytes\n", copied);
1140 ctask->buffer_read_ptr += bytes_to_copy;
1144 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1145 "MHD: Adding PP result %s to buffer\n",
1147 memcpy (buf+copied, re_match->result, strlen (re_match->result));
1148 copied += strlen (re_match->result);
1149 ctask->buffer_read_ptr = re_match->end;
1150 GNUNET_CONTAINER_DLL_remove (ctask->pp_match_head,
1151 ctask->pp_match_tail,
1153 GNUNET_free (re_match);
1156 bytes_to_copy = ctask->buffer_write_ptr - ctask->buffer_read_ptr;
1158 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1159 "MHD: copied: %d left: %d, space left in buf: %d\n",
1161 bytes_to_copy, max-copied);
1163 GNUNET_assert (0 <= bytes_to_copy);
1165 if (GNUNET_NO == ctask->download_is_finished)
1167 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1168 "MHD: Purging buffer\n");
1169 memmove (ctask->buffer, ctask->buffer_read_ptr, bytes_to_copy);
1170 ctask->buffer_read_ptr = ctask->buffer;
1171 ctask->buffer_write_ptr = ctask->buffer + bytes_to_copy;
1172 ctask->buffer[bytes_to_copy] = '\0';
1175 if (bytes_to_copy+copied > max)
1176 bytes_to_copy = max-copied;
1178 if (0 > bytes_to_copy)
1181 memcpy (buf+copied, ctask->buffer_read_ptr, bytes_to_copy);
1182 ctask->buffer_read_ptr += bytes_to_copy;
1183 copied += bytes_to_copy;
1184 ctask->buf_status = BUF_WAIT_FOR_CURL;
1186 if (NULL != ctask->curl)
1187 curl_easy_pause (ctask->curl, CURLPAUSE_CONT);
1189 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1190 "MHD: copied %d bytes\n", copied);
1191 run_mhd_now (ctask->mhd);
1197 * Shorten result callback
1199 * @param cls the proxycurltask
1200 * @param short_name the shortened name (NULL on error)
1203 process_shorten (void* cls, const char* short_name)
1205 struct ProxyREMatch *re_match = cls;
1206 char result[sizeof (re_match->result)];
1208 if (NULL == short_name)
1210 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1211 "PP: Unable to shorten %s\n",
1212 re_match->hostname);
1213 GNUNET_CONTAINER_DLL_remove (re_match->ctask->pp_match_head,
1214 re_match->ctask->pp_match_tail,
1216 GNUNET_free (re_match);
1220 if (0 == strcmp (short_name, re_match->ctask->leho))
1221 strcpy (result, re_match->ctask->host);
1223 strcpy (result, short_name);
1225 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1226 "PP: Shorten %s -> %s\n",
1230 if (re_match->ctask->mhd->is_ssl)
1231 sprintf (re_match->result, "href=\"https://%s", result);
1233 sprintf (re_match->result, "href=\"http://%s", result);
1235 re_match->done = GNUNET_YES;
1236 run_mhd_now (re_match->ctask->mhd);
1241 * Postprocess data in buffer. From read ptr to write ptr
1243 * @param cls the curlproxytask
1244 * @param tc task context
1247 postprocess_buffer (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1249 struct ProxyCurlTask *ctask = cls;
1250 struct ProxyREMatch *re_match;
1251 char* re_ptr = ctask->buffer_read_ptr;
1252 char re_hostname[255];
1253 regmatch_t m[RE_N_MATCHES];
1255 ctask->pp_task = GNUNET_SCHEDULER_NO_TASK;
1257 if (GNUNET_YES != ctask->parse_content)
1259 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1260 "PP: Not parsing content\n");
1261 ctask->buf_status = BUF_WAIT_FOR_MHD;
1262 run_mhd_now (ctask->mhd);
1266 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1267 "PP: We need to parse the HTML\n");
1269 /* 0 means match found */
1270 while (0 == regexec (&re_dotplus, re_ptr, RE_N_MATCHES, m, 0))
1272 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1273 "PP: regex match\n");
1275 GNUNET_assert (m[1].rm_so != -1);
1277 memset (re_hostname, 0, sizeof (re_hostname));
1278 memcpy (re_hostname, re_ptr+m[1].rm_so, (m[3].rm_eo-m[1].rm_so));
1280 re_match = GNUNET_malloc (sizeof (struct ProxyREMatch));
1281 re_match->start = re_ptr + m[0].rm_so;
1282 re_match->end = re_ptr + m[3].rm_eo;
1283 re_match->done = GNUNET_NO;
1284 re_match->ctask = ctask;
1285 strcpy (re_match->hostname, re_hostname);
1286 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1287 "PP: Got hostname %s\n", re_hostname);
1288 re_ptr += m[3].rm_eo;
1290 if (GNUNET_YES == is_tld (re_match->hostname, GNUNET_GNS_TLD_PLUS))
1292 re_match->hostname[strlen(re_match->hostname)-1] = '\0';
1293 strcpy (re_match->hostname+strlen(re_match->hostname),
1297 re_match->shorten_task = GNUNET_GNS_shorten_zone (gns_handle,
1303 re_match); //FIXME cancel appropriately
1305 GNUNET_CONTAINER_DLL_insert_tail (ctask->pp_match_head,
1306 ctask->pp_match_tail,
1310 ctask->buf_status = BUF_WAIT_FOR_MHD;
1311 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1312 "PP: No more matches\n");
1313 run_mhd_now (ctask->mhd);
1317 * Handle data from cURL
1319 * @param ptr pointer to the data
1320 * @param size number of blocks of data
1321 * @param nmemb blocksize
1322 * @param ctx the curlproxytask
1323 * @return number of bytes handled
1326 curl_download_cb (void *ptr, size_t size, size_t nmemb, void* ctx)
1328 const char *cbuf = ptr;
1329 size_t total = size * nmemb;
1330 struct ProxyCurlTask *ctask = ctx;
1331 size_t buf_space = sizeof (ctask->buffer) -
1332 (ctask->buffer_write_ptr-ctask->buffer);
1334 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1335 "CURL: Got %d. %d free in buffer\n",
1338 if (BUF_WAIT_FOR_CURL != ctask->buf_status)
1339 return CURL_WRITEFUNC_PAUSE;
1341 if (total > (buf_space - CURL_BUF_PADDING))
1343 if (ctask->buf_status == BUF_WAIT_FOR_CURL)
1345 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1346 "CURL: Buffer full starting postprocessing\n");
1347 ctask->buf_status = BUF_WAIT_FOR_PP;
1348 ctask->pp_task = GNUNET_SCHEDULER_add_now (&postprocess_buffer,
1350 return CURL_WRITEFUNC_PAUSE;
1353 /* we should not get called in that case */
1354 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1355 "CURL: called out of context and no space in buffer!\n");
1356 return CURL_WRITEFUNC_PAUSE;
1359 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1360 "CURL: Copying %d bytes to buffer (%s)\n", total, ctask->url);
1361 memcpy (ctask->buffer_write_ptr, cbuf, total);
1362 ctask->bytes_in_buffer += total;
1363 ctask->buffer_write_ptr += total;
1364 ctask->buffer_write_ptr[0] = '\0';
1371 * cURL callback for put data
1374 put_read_callback (void *buf, size_t size, size_t nmemb, void *cls)
1376 struct ProxyCurlTask *ctask = cls;
1377 struct ProxyUploadData *pdata = ctask->upload_data_head;
1378 size_t len = size * nmemb;
1382 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1383 "CURL: put read callback\n");
1386 return CURL_READFUNC_PAUSE;
1389 if (NULL == pdata->value)
1391 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1392 "CURL: Terminating PUT\n");
1394 GNUNET_CONTAINER_DLL_remove (ctask->upload_data_head,
1395 ctask->upload_data_tail,
1397 GNUNET_free (pdata);
1401 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1402 "CURL: read callback value %s\n", pdata->value);
1404 to_copy = pdata->bytes_left;
1408 pos = pdata->value + (pdata->total_bytes - pdata->bytes_left);
1409 memcpy (buf, pos, to_copy);
1410 pdata->bytes_left -= to_copy;
1411 if (pdata->bytes_left <= 0)
1413 GNUNET_free (pdata->value);
1414 GNUNET_CONTAINER_DLL_remove (ctask->upload_data_head,
1415 ctask->upload_data_tail,
1417 GNUNET_free (pdata);
1424 * cURL callback for post data
1427 post_read_callback (void *buf, size_t size, size_t nmemb, void *cls)
1429 struct ProxyCurlTask *ctask = cls;
1430 struct ProxyUploadData *pdata = ctask->upload_data_head;
1431 size_t len = size * nmemb;
1435 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1436 "CURL: read callback\n");
1439 return CURL_READFUNC_PAUSE;
1442 if (NULL == pdata->value)
1444 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1445 "CURL: Terminating POST data\n");
1447 GNUNET_CONTAINER_DLL_remove (ctask->upload_data_head,
1448 ctask->upload_data_tail,
1450 GNUNET_free (pdata);
1454 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1455 "CURL: read callback value %s\n", pdata->value);
1457 to_copy = pdata->bytes_left;
1461 pos = pdata->value + (pdata->total_bytes - pdata->bytes_left);
1462 memcpy (buf, pos, to_copy);
1463 pdata->bytes_left -= to_copy;
1464 if (pdata->bytes_left <= 0)
1466 GNUNET_free (pdata->value);
1467 GNUNET_CONTAINER_DLL_remove (ctask->upload_data_head,
1468 ctask->upload_data_tail,
1470 GNUNET_free (pdata);
1476 * Task that is run when we are ready to receive more data
1479 * @param cls closure
1480 * @param tc task context
1483 curl_task_download (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
1486 * Ask cURL for the select sets and schedule download
1489 curl_download_prepare ()
1496 struct GNUNET_NETWORK_FDSet *grs;
1497 struct GNUNET_NETWORK_FDSet *gws;
1499 struct GNUNET_TIME_Relative rtime;
1505 mret = curl_multi_fdset (curl_multi, &rs, &ws, &es, &max);
1507 if (mret != CURLM_OK)
1509 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1510 "%s failed at %s:%d: `%s'\n",
1511 "curl_multi_fdset", __FILE__, __LINE__,
1512 curl_multi_strerror (mret));
1513 //TODO cleanup here?
1517 mret = curl_multi_timeout (curl_multi, &to);
1518 rtime = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, to);
1520 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1521 "cURL multi fds: max=%d timeout=%llu\n", max, to);
1523 grs = GNUNET_NETWORK_fdset_create ();
1524 gws = GNUNET_NETWORK_fdset_create ();
1525 GNUNET_NETWORK_fdset_copy_native (grs, &rs, max + 1);
1526 GNUNET_NETWORK_fdset_copy_native (gws, &ws, max + 1);
1527 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1528 "Scheduling task cURL\n");
1530 if (curl_download_task != GNUNET_SCHEDULER_NO_TASK)
1531 GNUNET_SCHEDULER_cancel (curl_download_task);
1535 curl_download_task =
1536 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1539 &curl_task_download, curl_multi);
1541 else if (NULL != ctasks_head)
1543 /* as specified in curl docs */
1544 curl_download_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MILLISECONDS,
1545 &curl_task_download,
1548 GNUNET_NETWORK_fdset_destroy (gws);
1549 GNUNET_NETWORK_fdset_destroy (grs);
1554 * Task that is run when we are ready to receive more data
1557 * @param cls closure
1558 * @param tc task context
1561 curl_task_download (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1565 struct CURLMsg *msg;
1567 struct ProxyCurlTask *ctask;
1571 struct ProxyCurlTask *clean_head = NULL;
1572 struct ProxyCurlTask *clean_tail = NULL;
1574 curl_download_task = GNUNET_SCHEDULER_NO_TASK;
1576 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1578 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1579 "Shutdown requested while trying to download\n");
1583 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1591 mret = curl_multi_perform (curl_multi, &running);
1593 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1594 "Running curl tasks: %d\n", running);
1596 for (ctask = ctasks_head; NULL != ctask; ctask = ctask->next)
1598 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1599 "CTask: %s\n", ctask->url);
1603 if (num_ctasks != running)
1605 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1606 "%d ctasks, %d curl running\n", num_ctasks, running);
1612 msg = curl_multi_info_read (curl_multi, &msgnum);
1613 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1614 "Messages left: %d\n", msgnum);
1621 if ((msg->data.result != CURLE_OK) &&
1622 (msg->data.result != CURLE_GOT_NOTHING))
1624 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1625 "Download curl failed");
1627 for (ctask = ctasks_head; NULL != ctask; ctask = ctask->next)
1629 if (NULL == ctask->curl)
1632 if (memcmp (msg->easy_handle, ctask->curl, sizeof (CURL)) != 0)
1635 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1636 "CURL: Download failed for task %s: %s.\n",
1638 curl_easy_strerror (msg->data.result));
1639 ctask->download_is_finished = GNUNET_YES;
1640 ctask->download_error = GNUNET_YES;
1641 if (CURLE_OK == curl_easy_getinfo (ctask->curl,
1642 CURLINFO_RESPONSE_CODE,
1644 ctask->curl_response_code = resp_code;
1645 ctask->ready_to_queue = MHD_YES;
1646 ctask->buf_status = BUF_WAIT_FOR_MHD;
1647 run_mhd_now (ctask->mhd);
1649 GNUNET_CONTAINER_DLL_remove (ctasks_head, ctasks_tail,
1651 GNUNET_CONTAINER_DLL_insert (clean_head, clean_tail, ctask);
1654 GNUNET_assert (ctask != NULL);
1658 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1659 "CURL: download completed.\n");
1661 for (ctask = ctasks_head; NULL != ctask; ctask = ctask->next)
1663 if (NULL == ctask->curl)
1666 if (0 != memcmp (msg->easy_handle, ctask->curl, sizeof (CURL)))
1669 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1670 "CURL: completed task %s found.\n", ctask->url);
1671 if (CURLE_OK == curl_easy_getinfo (ctask->curl,
1672 CURLINFO_RESPONSE_CODE,
1674 ctask->curl_response_code = resp_code;
1677 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1678 "CURL: Completed ctask!\n");
1679 if (GNUNET_SCHEDULER_NO_TASK == ctask->pp_task)
1681 ctask->buf_status = BUF_WAIT_FOR_PP;
1682 ctask->pp_task = GNUNET_SCHEDULER_add_now (&postprocess_buffer,
1686 ctask->ready_to_queue = MHD_YES;
1687 ctask->download_is_finished = GNUNET_YES;
1689 /* We MUST not modify the multi handle else we loose messages */
1690 GNUNET_CONTAINER_DLL_remove (ctasks_head, ctasks_tail,
1692 GNUNET_CONTAINER_DLL_insert (clean_head, clean_tail, ctask);
1696 GNUNET_assert (ctask != NULL);
1698 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1699 "CURL: %s\n", curl_easy_strerror(msg->data.result));
1705 } while (msgnum > 0);
1707 for (ctask=clean_head; NULL != ctask; ctask = ctask->next)
1709 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1710 "CURL: Removing task %s.\n", ctask->url);
1711 curl_multi_remove_handle (curl_multi, ctask->curl);
1712 curl_easy_cleanup (ctask->curl);
1717 for (ctask=ctasks_head; NULL != ctask; ctask = ctask->next)
1722 if (num_ctasks != running)
1724 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1725 "CURL: %d tasks, %d running\n", num_ctasks, running);
1728 GNUNET_assert ( num_ctasks == running );
1730 } while (mret == CURLM_CALL_MULTI_PERFORM);
1732 if (mret != CURLM_OK)
1734 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "CURL: %s failed at %s:%d: `%s'\n",
1735 "curl_multi_perform", __FILE__, __LINE__,
1736 curl_multi_strerror (mret));
1738 curl_download_prepare();
1743 * Process LEHO lookup
1745 * @param cls the ctask
1746 * @param rd_count number of records returned
1747 * @param rd record data
1750 process_leho_lookup (void *cls,
1752 const struct GNUNET_NAMESTORE_RecordData *rd)
1754 struct ProxyCurlTask *ctask = cls;
1755 char hosthdr[262]; //256 + "Host: "
1759 struct hostent *phost;
1761 char resolvename[512];
1764 strcpy (ctask->leho, "");
1767 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1768 "No LEHO present!\n");
1770 for (i=0; i<rd_count; i++)
1772 if (rd[i].record_type != GNUNET_GNS_RECORD_LEHO)
1775 memcpy (ctask->leho, rd[i].data, rd[i].data_size);
1777 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1778 "Found LEHO %s for %s\n", ctask->leho, ctask->url);
1781 if (0 != strcmp (ctask->leho, ""))
1783 sprintf (hosthdr, "%s%s:%d", "Host: ", ctask->leho, ctask->port);
1784 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1785 "New HTTP header value: %s\n", hosthdr);
1786 ctask->headers = curl_slist_append (ctask->headers, hosthdr);
1787 GNUNET_assert (NULL != ctask->headers);
1788 ret = curl_easy_setopt (ctask->curl, CURLOPT_HTTPHEADER, ctask->headers);
1789 if (CURLE_OK != ret)
1791 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "%s failed at %s:%d: `%s'\n",
1792 "curl_easy_setopt", __FILE__, __LINE__, curl_easy_strerror(ret));
1797 if (ctask->mhd->is_ssl)
1799 phost = (struct hostent*)gethostbyname (ctask->host);
1803 ssl_ip = inet_ntoa(*((struct in_addr*)(phost->h_addr)));
1804 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1805 "SSL target server: %s\n", ssl_ip);
1806 sprintf (resolvename, "%s:%d:%s", ctask->leho, HTTPS_PORT, ssl_ip);
1807 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1808 "Curl resolve: %s\n", resolvename);
1809 ctask->resolver = curl_slist_append ( ctask->resolver, resolvename);
1810 curl_easy_setopt (ctask->curl, CURLOPT_RESOLVE, ctask->resolver);
1811 sprintf (curlurl, "https://%s:%d%s", ctask->leho, ctask->port, ctask->url);
1812 curl_easy_setopt (ctask->curl, CURLOPT_URL, curlurl);
1816 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1817 "gethostbyname failed for %s!\n", ctask->host);
1818 ctask->download_is_finished = GNUNET_YES;
1819 ctask->download_error = GNUNET_YES;
1824 if (CURLM_OK != (mret=curl_multi_add_handle (curl_multi, ctask->curl)))
1826 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1827 "%s failed at %s:%d: `%s'\n",
1828 "curl_multi_add_handle", __FILE__, __LINE__,
1829 curl_multi_strerror (mret));
1830 ctask->download_is_finished = GNUNET_YES;
1831 ctask->download_error = GNUNET_YES;
1834 GNUNET_CONTAINER_DLL_insert (ctasks_head, ctasks_tail, ctask);
1836 curl_download_prepare ();
1841 * Initialize download and trigger curl
1843 * @param cls the proxycurltask
1844 * @param auth_name the name of the authority (site of origin) of ctask->host
1848 process_get_authority (void *cls,
1849 const char* auth_name)
1851 struct ProxyCurlTask *ctask = cls;
1853 if (NULL == auth_name)
1855 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1856 "Get authority failed!\n");
1857 strcpy (ctask->authority, "");
1861 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1862 "Get authority yielded %s\n", auth_name);
1863 strcpy (ctask->authority, auth_name);
1866 GNUNET_GNS_lookup_zone (gns_handle,
1869 GNUNET_GNS_RECORD_LEHO,
1870 GNUNET_YES, //Only cached for performance
1872 &process_leho_lookup,
1877 mhd_log_callback (void* cls, const char* url)
1879 struct ProxyCurlTask *ctask;
1881 ctask = GNUNET_malloc (sizeof (struct ProxyCurlTask));
1882 strcpy (ctask->url, url);
1883 ctask->accepted = GNUNET_NO;
1888 * Main MHD callback for handling requests.
1891 * @param con MHD connection handle
1892 * @param url the url in the request
1893 * @param meth the HTTP method used ("GET", "PUT", etc.)
1894 * @param ver the HTTP version string (i.e. "HTTP/1.1")
1895 * @param upload_data the data being uploaded (excluding HEADERS,
1896 * for a POST that fits into memory and that is encoded
1897 * with a supported encoding, the POST data will NOT be
1898 * given in upload_data and is instead available as
1899 * part of MHD_get_connection_values; very large POST
1900 * data *will* be made available incrementally in
1902 * @param upload_data_size set initially to the size of the
1903 * upload_data provided; the method must update this
1904 * value to the number of bytes NOT processed;
1905 * @param con_cls pointer to location where we store the 'struct Request'
1906 * @return MHD_YES if the connection was handled successfully,
1907 * MHD_NO if the socket must be closed due to a serious
1908 * error while handling the request
1911 create_response (void *cls,
1912 struct MHD_Connection *con,
1916 const char *upload_data,
1917 size_t *upload_data_size,
1920 struct MhdHttpList* hd = cls;
1921 const char* page = "<html><head><title>gnoxy</title>"\
1922 "</head><body>cURL fail</body></html>";
1924 char curlurl[MAX_HTTP_URI_LENGTH]; // buffer overflow!
1928 struct ProxyCurlTask *ctask = *con_cls;
1929 struct ProxyUploadData *fin_post;
1930 struct curl_forms forms[5];
1931 struct ProxyUploadData *upload_data_iter;
1934 if ((0 != strcasecmp (meth, MHD_HTTP_METHOD_GET)) &&
1935 (0 != strcasecmp (meth, MHD_HTTP_METHOD_PUT)) &&
1936 (0 != strcasecmp (meth, MHD_HTTP_METHOD_POST)) &&
1937 (0 != strcasecmp (meth, MHD_HTTP_METHOD_HEAD)))
1939 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1940 "MHD: %s NOT IMPLEMENTED!\n", meth);
1945 if (GNUNET_NO == ctask->accepted)
1948 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1949 "Got %s request for %s\n", meth, url);
1951 ctask->curl = curl_easy_init();
1952 ctask->curl_running = GNUNET_NO;
1953 if (NULL == ctask->curl)
1955 ctask->response = MHD_create_response_from_buffer (strlen (page),
1957 MHD_RESPMEM_PERSISTENT);
1958 ret = MHD_queue_response (con,
1961 MHD_destroy_response (ctask->response);
1962 GNUNET_free (ctask);
1966 if (ctask->mhd->is_ssl)
1967 ctask->port = HTTPS_PORT;
1969 ctask->port = HTTP_PORT;
1971 MHD_get_connection_values (con,
1973 &con_val_iter, ctask);
1975 curl_easy_setopt (ctask->curl, CURLOPT_HEADERFUNCTION, &curl_check_hdr);
1976 curl_easy_setopt (ctask->curl, CURLOPT_HEADERDATA, ctask);
1977 curl_easy_setopt (ctask->curl, CURLOPT_WRITEFUNCTION, &curl_download_cb);
1978 curl_easy_setopt (ctask->curl, CURLOPT_WRITEDATA, ctask);
1979 curl_easy_setopt (ctask->curl, CURLOPT_FOLLOWLOCATION, 0);
1980 curl_easy_setopt (ctask->curl, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
1982 if (GNUNET_NO == ctask->mhd->is_ssl)
1984 sprintf (curlurl, "http://%s:%d%s", ctask->host, ctask->port, ctask->url);
1985 curl_easy_setopt (ctask->curl, CURLOPT_URL, curlurl);
1989 curl_easy_setopt (ctask->curl, CURLOPT_FAILONERROR, 1);
1990 curl_easy_setopt (ctask->curl, CURLOPT_CONNECTTIMEOUT, 600L);
1991 curl_easy_setopt (ctask->curl, CURLOPT_TIMEOUT, 600L);
1993 /* Add GNS header */
1994 //ctask->headers = curl_slist_append (ctask->headers,
1996 ctask->accepted = GNUNET_YES;
1997 ctask->download_in_progress = GNUNET_YES;
1998 ctask->buf_status = BUF_WAIT_FOR_CURL;
1999 ctask->connection = con;
2000 ctask->curl_response_code = MHD_HTTP_OK;
2001 ctask->buffer_read_ptr = ctask->buffer;
2002 ctask->buffer_write_ptr = ctask->buffer;
2003 ctask->pp_task = GNUNET_SCHEDULER_NO_TASK;
2006 if (0 == strcasecmp (meth, MHD_HTTP_METHOD_PUT))
2008 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2009 "Setting up PUT\n");
2011 curl_easy_setopt (ctask->curl, CURLOPT_UPLOAD, 1);
2012 curl_easy_setopt (ctask->curl, CURLOPT_READDATA, ctask);
2013 curl_easy_setopt (ctask->curl, CURLOPT_READFUNCTION, &put_read_callback);
2014 ctask->headers = curl_slist_append (ctask->headers,
2015 "Transfer-Encoding: chunked");
2018 if (0 == strcasecmp (meth, MHD_HTTP_METHOD_POST))
2020 //FIXME handle multipart
2021 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2022 "Setting up POST processor\n");
2023 ctask->post_handler = MHD_create_post_processor (con,
2025 &con_post_data_iter,
2027 ctask->headers = curl_slist_append (ctask->headers,
2028 "Transfer-Encoding: chunked");
2032 if (0 == strcasecmp (meth, MHD_HTTP_METHOD_HEAD))
2034 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2035 "Setting NOBODY\n");
2036 curl_easy_setopt (ctask->curl, CURLOPT_NOBODY, 1);
2040 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2041 "MHD: Adding new curl task for %s\n", ctask->host);
2043 GNUNET_GNS_get_authority (gns_handle,
2045 &process_get_authority,
2047 ctask->ready_to_queue = GNUNET_NO;
2048 ctask->fin = GNUNET_NO;
2049 ctask->curl_running = GNUNET_YES;
2053 ctask = (struct ProxyCurlTask *) *con_cls;
2054 if (0 == strcasecmp (meth, MHD_HTTP_METHOD_POST))
2056 if (0 != *upload_data_size)
2059 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2060 "Invoking POST processor\n");
2061 MHD_post_process (ctask->post_handler,
2062 upload_data, *upload_data_size);
2063 *upload_data_size = 0;
2064 if ((GNUNET_NO == ctask->is_httppost) &&
2065 (GNUNET_NO == ctask->curl_running))
2067 curl_easy_setopt (ctask->curl, CURLOPT_POST, 1);
2068 curl_easy_setopt (ctask->curl, CURLOPT_READFUNCTION,
2069 &post_read_callback);
2070 curl_easy_setopt (ctask->curl, CURLOPT_READDATA, ctask);
2072 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2073 "MHD: Adding new curl task for %s\n", ctask->host);
2075 GNUNET_GNS_get_authority (gns_handle,
2077 &process_get_authority,
2079 ctask->ready_to_queue = GNUNET_NO;
2080 ctask->fin = GNUNET_NO;
2081 ctask->curl_running = GNUNET_YES;
2085 else if (GNUNET_NO == ctask->post_done)
2087 if (GNUNET_YES == ctask->is_httppost)
2090 for (upload_data_iter = ctask->upload_data_head;
2091 NULL != upload_data_iter;
2092 upload_data_iter = upload_data_iter->next)
2094 if (NULL != upload_data_iter->filename)
2096 forms[i].option = CURLFORM_FILENAME;
2097 forms[i].value = upload_data_iter->filename;
2098 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2099 "Adding filename %s\n",
2103 if (NULL != upload_data_iter->content_type)
2105 forms[i].option = CURLFORM_CONTENTTYPE;
2106 forms[i].value = upload_data_iter->content_type;
2107 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2108 "Adding content type %s\n",
2112 forms[i].option = CURLFORM_PTRCONTENTS;
2113 forms[i].value = upload_data_iter->value;
2114 forms[i+1].option = CURLFORM_END;
2116 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2117 "Adding formdata for %s (len=%lld)\n",
2118 upload_data_iter->key,
2119 upload_data_iter->total_bytes);
2121 curl_formadd(&ctask->httppost, &ctask->httppost_last,
2122 CURLFORM_COPYNAME, upload_data_iter->key,
2123 CURLFORM_CONTENTSLENGTH, upload_data_iter->total_bytes,
2124 CURLFORM_ARRAY, forms,
2127 curl_easy_setopt (ctask->curl, CURLOPT_HTTPPOST,
2130 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2131 "MHD: Adding new curl task for %s\n", ctask->host);
2133 GNUNET_GNS_get_authority (gns_handle,
2135 &process_get_authority,
2137 ctask->ready_to_queue = GNUNET_YES;
2138 ctask->fin = GNUNET_NO;
2139 ctask->curl_running = GNUNET_YES;
2140 ctask->post_done = GNUNET_YES;
2144 fin_post = GNUNET_malloc (sizeof (struct ProxyUploadData));
2145 GNUNET_CONTAINER_DLL_insert_tail (ctask->upload_data_head,
2146 ctask->upload_data_tail,
2148 ctask->post_done = GNUNET_YES;
2153 if (GNUNET_YES != ctask->ready_to_queue)
2154 return MHD_YES; /* wait longer */
2156 if (GNUNET_YES == ctask->fin)
2159 ctask->fin = GNUNET_YES;
2160 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2161 "MHD: Queueing response for %s\n", ctask->url);
2162 ret = MHD_queue_response (con, ctask->curl_response_code, ctask->response);
2163 run_mhd_now (ctask->mhd);
2174 struct MhdHttpList *hd;
2176 for (hd=mhd_httpd_head; NULL != hd; hd = hd->next)
2184 * @param hd the daemon to run
2187 run_httpd (struct MhdHttpList *hd)
2192 struct GNUNET_NETWORK_FDSet *wrs;
2193 struct GNUNET_NETWORK_FDSet *wws;
2194 struct GNUNET_NETWORK_FDSet *wes;
2197 unsigned MHD_LONG_LONG timeout;
2198 struct GNUNET_TIME_Relative tv;
2203 wrs = GNUNET_NETWORK_fdset_create ();
2204 wes = GNUNET_NETWORK_fdset_create ();
2205 wws = GNUNET_NETWORK_fdset_create ();
2207 GNUNET_assert (MHD_YES == MHD_get_fdset (hd->daemon, &rs, &ws, &es, &max));
2210 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2211 "MHD fds: max=%d\n", max);
2213 haveto = MHD_get_timeout (hd->daemon, &timeout);
2215 if (MHD_YES == haveto)
2216 tv.rel_value = (uint64_t) timeout;
2218 tv = GNUNET_TIME_UNIT_FOREVER_REL;
2219 GNUNET_NETWORK_fdset_copy_native (wrs, &rs, max + 1);
2220 GNUNET_NETWORK_fdset_copy_native (wws, &ws, max + 1);
2221 GNUNET_NETWORK_fdset_copy_native (wes, &es, max + 1);
2223 if (GNUNET_SCHEDULER_NO_TASK != hd->httpd_task)
2224 GNUNET_SCHEDULER_cancel (hd->httpd_task);
2226 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_HIGH,
2229 GNUNET_NETWORK_fdset_destroy (wrs);
2230 GNUNET_NETWORK_fdset_destroy (wws);
2231 GNUNET_NETWORK_fdset_destroy (wes);
2236 * Task run whenever HTTP server operations are pending.
2239 * @param tc sched context
2242 do_httpd (void *cls,
2243 const struct GNUNET_SCHEDULER_TaskContext *tc)
2245 struct MhdHttpList *hd = cls;
2247 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2248 "MHD: Main loop\n");
2249 hd->httpd_task = GNUNET_SCHEDULER_NO_TASK;
2250 MHD_run (hd->daemon);
2257 * Read data from socket
2259 * @param cls the closure
2260 * @param tc scheduler context
2263 do_read (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
2266 * Read from remote end
2268 * @param cls closure
2269 * @param tc scheduler context
2272 do_read_remote (void* cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
2275 * Write data to remote socket
2277 * @param cls the closure
2278 * @param tc scheduler context
2281 do_write_remote (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2283 struct Socks5Request *s5r = cls;
2286 s5r->fwdwtask = GNUNET_SCHEDULER_NO_TASK;
2288 if ((NULL != tc->read_ready) &&
2289 (GNUNET_NETWORK_fdset_isset (tc->write_ready, s5r->remote_sock)) &&
2290 ((len = GNUNET_NETWORK_socket_send (s5r->remote_sock, s5r->rbuf,
2293 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2294 "Successfully sent %d bytes to remote socket\n",
2299 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "write remote");
2300 if (GNUNET_SCHEDULER_NO_TASK != s5r->rtask)
2301 GNUNET_SCHEDULER_cancel (s5r->rtask);
2302 if (GNUNET_SCHEDULER_NO_TASK != s5r->wtask)
2303 GNUNET_SCHEDULER_cancel (s5r->wtask);
2304 if (GNUNET_SCHEDULER_NO_TASK != s5r->fwdrtask)
2305 GNUNET_SCHEDULER_cancel (s5r->fwdrtask);
2306 GNUNET_NETWORK_socket_close (s5r->remote_sock);
2307 GNUNET_NETWORK_socket_close (s5r->sock);
2313 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
2320 * Clean up s5r handles
2322 * @param s5r the handle to destroy
2325 cleanup_s5r (struct Socks5Request *s5r)
2327 if (GNUNET_SCHEDULER_NO_TASK != s5r->rtask)
2328 GNUNET_SCHEDULER_cancel (s5r->rtask);
2329 if (GNUNET_SCHEDULER_NO_TASK != s5r->fwdwtask)
2330 GNUNET_SCHEDULER_cancel (s5r->fwdwtask);
2331 if (GNUNET_SCHEDULER_NO_TASK != s5r->fwdrtask)
2332 GNUNET_SCHEDULER_cancel (s5r->fwdrtask);
2334 if (NULL != s5r->remote_sock)
2335 GNUNET_NETWORK_socket_close (s5r->remote_sock);
2336 if ((NULL != s5r->sock) && (s5r->cleanup_sock == GNUNET_YES))
2337 GNUNET_NETWORK_socket_close (s5r->sock);
2343 * Write data to socket
2345 * @param cls the closure
2346 * @param tc scheduler context
2349 do_write (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2351 struct Socks5Request *s5r = cls;
2354 s5r->wtask = GNUNET_SCHEDULER_NO_TASK;
2356 if ((NULL != tc->read_ready) &&
2357 (GNUNET_NETWORK_fdset_isset (tc->write_ready, s5r->sock)) &&
2358 ((len = GNUNET_NETWORK_socket_send (s5r->sock, s5r->wbuf,
2361 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2362 "Successfully sent %d bytes to socket\n",
2368 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "write");
2369 s5r->cleanup = GNUNET_YES;
2370 s5r->cleanup_sock = GNUNET_YES;
2376 if (GNUNET_YES == s5r->cleanup)
2382 if ((s5r->state == SOCKS5_DATA_TRANSFER) &&
2383 (s5r->fwdrtask == GNUNET_SCHEDULER_NO_TASK))
2385 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
2387 &do_read_remote, s5r);
2391 * Read from remote end
2393 * @param cls closure
2394 * @param tc scheduler context
2397 do_read_remote (void* cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2399 struct Socks5Request *s5r = cls;
2401 s5r->fwdrtask = GNUNET_SCHEDULER_NO_TASK;
2404 if ((NULL != tc->write_ready) &&
2405 (GNUNET_NETWORK_fdset_isset (tc->read_ready, s5r->remote_sock)) &&
2406 (s5r->wbuf_len = GNUNET_NETWORK_socket_recv (s5r->remote_sock, s5r->wbuf,
2407 sizeof (s5r->wbuf))))
2409 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2410 "Successfully read %d bytes from remote socket\n",
2415 if (s5r->wbuf_len == 0)
2416 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2417 "0 bytes received from remote... graceful shutdown!\n");
2418 if (s5r->fwdwtask != GNUNET_SCHEDULER_NO_TASK)
2419 GNUNET_SCHEDULER_cancel (s5r->fwdwtask);
2420 if (s5r->rtask != GNUNET_SCHEDULER_NO_TASK)
2421 GNUNET_SCHEDULER_cancel (s5r->rtask);
2423 GNUNET_NETWORK_socket_close (s5r->remote_sock);
2424 s5r->remote_sock = NULL;
2425 GNUNET_NETWORK_socket_close (s5r->sock);
2431 s5r->wtask = GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
2439 * Adds a socket to MHD
2441 * @param h the handle to the socket to add
2442 * @param daemon the daemon to add the fd to
2443 * @return whatever MHD_add_connection returns
2446 add_handle_to_mhd (struct GNUNET_NETWORK_Handle *h, struct MHD_Daemon *daemon)
2449 struct sockaddr *addr;
2452 fd = dup (GNUNET_NETWORK_get_fd (h));
2453 addr = GNUNET_NETWORK_get_addr (h);
2454 len = GNUNET_NETWORK_get_addrlen (h);
2456 return MHD_add_connection (daemon, fd, addr, len);
2460 * Read file in filename
2462 * @param filename file to read
2463 * @param size pointer where filesize is stored
2467 load_file (const char* filename,
2474 GNUNET_DISK_file_size (filename, &fsize,
2475 GNUNET_YES, GNUNET_YES))
2477 if (fsize > MAX_PEM_SIZE)
2479 *size = (unsigned int) fsize;
2480 buffer = GNUNET_malloc (*size);
2481 if (fsize != GNUNET_DISK_fn_read (filename, buffer, (size_t) fsize))
2483 GNUNET_free (buffer);
2491 * Load PEM key from file
2493 * @param key where to store the data
2494 * @param keyfile path to the PEM file
2495 * @return GNUNET_OK on success
2498 load_key_from_file (gnutls_x509_privkey_t key, const char* keyfile)
2500 gnutls_datum_t key_data;
2503 key_data.data = (unsigned char*) load_file (keyfile, &key_data.size);
2504 ret = gnutls_x509_privkey_import (key, &key_data,
2505 GNUTLS_X509_FMT_PEM);
2506 if (GNUTLS_E_SUCCESS != ret)
2508 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2509 _("Unable to import private key from file `%s'\n"),
2513 GNUNET_free (key_data.data);
2514 return (GNUTLS_E_SUCCESS != ret) ? GNUNET_SYSERR : GNUNET_OK;
2519 * Load cert from file
2521 * @param crt struct to store data in
2522 * @param certfile path to pem file
2523 * @return GNUNET_OK on success
2526 load_cert_from_file (gnutls_x509_crt_t crt, char* certfile)
2528 gnutls_datum_t cert_data;
2529 cert_data.data = NULL;
2532 cert_data.data = (unsigned char*) load_file (certfile, &cert_data.size);
2533 ret = gnutls_x509_crt_import (crt, &cert_data,
2534 GNUTLS_X509_FMT_PEM);
2535 if (GNUTLS_E_SUCCESS != ret)
2537 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2538 _("Unable to import certificate %s\n"), certfile);
2541 GNUNET_free (cert_data.data);
2542 return (GNUTLS_E_SUCCESS != ret) ? GNUNET_SYSERR : GNUNET_OK;
2547 * Generate new certificate for specific name
2549 * @param name the subject name to generate a cert for
2550 * @return a struct holding the PEM data
2552 static struct ProxyGNSCertificate *
2553 generate_gns_certificate (const char *name)
2557 unsigned int serial;
2558 size_t key_buf_size;
2559 size_t cert_buf_size;
2560 gnutls_x509_crt_t request;
2564 ret = gnutls_x509_crt_init (&request);
2566 if (GNUTLS_E_SUCCESS != ret)
2571 ret = gnutls_x509_crt_set_key (request, proxy_ca.key);
2573 if (GNUTLS_E_SUCCESS != ret)
2578 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Generating cert\n");
2580 struct ProxyGNSCertificate *pgc =
2581 GNUNET_malloc (sizeof (struct ProxyGNSCertificate));
2583 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Adding DNs\n");
2585 gnutls_x509_crt_set_dn_by_oid (request, GNUTLS_OID_X520_COUNTRY_NAME,
2588 gnutls_x509_crt_set_dn_by_oid (request, GNUTLS_OID_X520_ORGANIZATION_NAME,
2591 gnutls_x509_crt_set_dn_by_oid (request, GNUTLS_OID_X520_COMMON_NAME,
2592 0, name, strlen (name));
2594 ret = gnutls_x509_crt_set_version (request, 3);
2596 ret = gnutls_rnd (GNUTLS_RND_NONCE, &serial, sizeof (serial));
2598 etime = time (NULL);
2599 tm_data = localtime (&etime);
2602 ret = gnutls_x509_crt_set_serial (request,
2606 ret = gnutls_x509_crt_set_activation_time (request,
2609 etime = mktime (tm_data);
2616 ret = gnutls_x509_crt_set_expiration_time (request,
2618 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Signing...\n");
2620 ret = gnutls_x509_crt_sign (request, proxy_ca.cert, proxy_ca.key);
2622 key_buf_size = sizeof (pgc->key);
2623 cert_buf_size = sizeof (pgc->cert);
2625 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Exporting certificate...\n");
2627 gnutls_x509_crt_export (request, GNUTLS_X509_FMT_PEM,
2628 pgc->cert, &cert_buf_size);
2630 gnutls_x509_privkey_export (proxy_ca.key, GNUTLS_X509_FMT_PEM,
2631 pgc->key, &key_buf_size);
2634 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Cleaning up\n");
2635 gnutls_x509_crt_deinit (request);
2643 * Accept policy for mhdaemons
2646 * @param addr the sockaddr
2647 * @param addrlen the sockaddr length
2648 * @return MHD_NO if sockaddr is wrong or #conns too high
2651 accept_cb (void* cls, const struct sockaddr *addr, socklen_t addrlen)
2653 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2654 "In MHD accept policy cb\n");
2658 if (addr->sa_family == AF_UNIX)
2662 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2663 "Connection accepted\n");
2670 * Adds a socket to an SSL MHD instance
2671 * It is important the the domain name is
2672 * correct. In most cases we need to start a new daemon
2674 * @param h the handle to add to a daemon
2675 * @param domain the domain the ssl daemon has to serve
2676 * @return MHD_YES on success
2679 add_handle_to_ssl_mhd (struct GNUNET_NETWORK_Handle *h, const char* domain)
2681 struct MhdHttpList *hd = NULL;
2682 struct ProxyGNSCertificate *pgc;
2683 struct NetworkHandleList *nh;
2685 for (hd = mhd_httpd_head; NULL != hd; hd = hd->next)
2686 if (0 == strcmp (hd->domain, domain))
2691 pgc = generate_gns_certificate (domain);
2693 hd = GNUNET_malloc (sizeof (struct MhdHttpList));
2694 hd->is_ssl = GNUNET_YES;
2695 strcpy (hd->domain, domain);
2696 hd->proxy_cert = pgc;
2699 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2700 "No previous SSL instance found... starting new one for %s\n",
2702 hd->daemon = MHD_start_daemon (MHD_USE_DEBUG
2704 #if HAVE_MHD_NO_LISTEN_SOCKET
2705 | MHD_USE_NO_LISTEN_SOCKET,
2711 &create_response, hd,
2712 #if !HAVE_MHD_NO_LISTEN_SOCKET
2713 MHD_OPTION_LISTEN_SOCKET, GNUNET_NETWORK_get_fd (mhd_unix_socket),
2715 MHD_OPTION_CONNECTION_LIMIT,
2716 MHD_MAX_CONNECTIONS,
2717 MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) 16,
2718 MHD_OPTION_NOTIFY_COMPLETED, NULL, NULL,
2719 MHD_OPTION_HTTPS_MEM_KEY, pgc->key,
2720 MHD_OPTION_HTTPS_MEM_CERT, pgc->cert,
2721 MHD_OPTION_URI_LOG_CALLBACK, &mhd_log_callback,
2725 GNUNET_assert (hd->daemon != NULL);
2726 hd->httpd_task = GNUNET_SCHEDULER_NO_TASK;
2728 GNUNET_CONTAINER_DLL_insert (mhd_httpd_head, mhd_httpd_tail, hd);
2731 nh = GNUNET_malloc (sizeof (struct NetworkHandleList));
2734 GNUNET_CONTAINER_DLL_insert (hd->socket_handles_head,
2735 hd->socket_handles_tail,
2738 return add_handle_to_mhd (h, hd->daemon);
2744 * Read data from incoming connection
2746 * @param cls the closure
2747 * @param tc the scheduler context
2750 do_read (void* cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2752 struct Socks5Request *s5r = cls;
2753 struct socks5_client_hello *c_hello;
2754 struct socks5_server_hello *s_hello;
2755 struct socks5_client_request *c_req;
2756 struct socks5_server_response *s_resp;
2762 struct hostent *phost;
2764 struct sockaddr_in remote_addr;
2765 struct in_addr *r_sin_addr;
2767 struct NetworkHandleList *nh;
2769 s5r->rtask = GNUNET_SCHEDULER_NO_TASK;
2771 if ((NULL != tc->write_ready) &&
2772 (GNUNET_NETWORK_fdset_isset (tc->read_ready, s5r->sock)) &&
2773 (s5r->rbuf_len = GNUNET_NETWORK_socket_recv (s5r->sock, s5r->rbuf,
2774 sizeof (s5r->rbuf))))
2776 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2777 "Successfully read %d bytes from socket\n",
2782 if (s5r->rbuf_len != 0)
2783 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "read");
2785 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client disco!\n");
2787 if (s5r->fwdrtask != GNUNET_SCHEDULER_NO_TASK)
2788 GNUNET_SCHEDULER_cancel (s5r->fwdrtask);
2789 if (s5r->wtask != GNUNET_SCHEDULER_NO_TASK)
2790 GNUNET_SCHEDULER_cancel (s5r->wtask);
2791 if (s5r->fwdwtask != GNUNET_SCHEDULER_NO_TASK)
2792 GNUNET_SCHEDULER_cancel (s5r->fwdwtask);
2793 GNUNET_NETWORK_socket_close (s5r->remote_sock);
2794 GNUNET_NETWORK_socket_close (s5r->sock);
2799 if (s5r->state == SOCKS5_INIT)
2801 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2803 c_hello = (struct socks5_client_hello*)&s5r->rbuf;
2805 GNUNET_assert (c_hello->version == SOCKS_VERSION_5);
2807 s_hello = (struct socks5_server_hello*)&s5r->wbuf;
2808 s5r->wbuf_len = sizeof( struct socks5_server_hello );
2810 s_hello->version = c_hello->version;
2811 s_hello->auth_method = SOCKS_AUTH_NONE;
2813 /* Write response to client */
2814 s5r->wtask = GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
2818 s5r->rtask = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
2822 s5r->state = SOCKS5_REQUEST;
2826 if (s5r->state == SOCKS5_REQUEST)
2828 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2829 "Processing SOCKS5 request\n");
2830 c_req = (struct socks5_client_request*)&s5r->rbuf;
2831 s_resp = (struct socks5_server_response*)&s5r->wbuf;
2832 //Only 10byte for ipv4 response!
2833 s5r->wbuf_len = 10;//sizeof (struct socks5_server_response);
2835 GNUNET_assert (c_req->addr_type == 3);
2837 dom_len = *((uint8_t*)(&(c_req->addr_type) + 1));
2838 memset(domain, 0, sizeof(domain));
2839 strncpy(domain, (char*)(&(c_req->addr_type) + 2), dom_len);
2840 req_port = *((uint16_t*)(&(c_req->addr_type) + 2 + dom_len));
2842 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2843 "Requested connection is %s:%d\n",
2847 if (is_tld (domain, GNUNET_GNS_TLD) ||
2848 is_tld (domain, GNUNET_GNS_TLD_ZKEY))
2850 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2851 "Requested connection is gnunet tld\n",
2855 if (ntohs(req_port) == HTTPS_PORT)
2857 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2858 "Requested connection is HTTPS\n");
2859 ret = add_handle_to_ssl_mhd ( s5r->sock, domain );
2861 else if (NULL != httpd)
2863 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2864 "Requested connection is HTTP\n");
2865 nh = GNUNET_malloc (sizeof (struct NetworkHandleList));
2868 GNUNET_CONTAINER_DLL_insert (mhd_httpd_head->socket_handles_head,
2869 mhd_httpd_head->socket_handles_tail,
2872 ret = add_handle_to_mhd ( s5r->sock, httpd );
2877 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2878 _("Failed to start HTTP server\n"));
2879 s_resp->version = 0x05;
2880 s_resp->reply = 0x01;
2881 s5r->cleanup = GNUNET_YES;
2882 s5r->cleanup_sock = GNUNET_YES;
2884 GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
2890 /* Signal success */
2891 s_resp->version = 0x05;
2892 s_resp->reply = 0x00;
2893 s_resp->reserved = 0x00;
2894 s_resp->addr_type = 0x01;
2896 s5r->cleanup = GNUNET_YES;
2897 s5r->cleanup_sock = GNUNET_NO;
2899 GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
2907 phost = (struct hostent*)gethostbyname (domain);
2910 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2911 "Resolve %s error!\n", domain );
2912 s_resp->version = 0x05;
2913 s_resp->reply = 0x01;
2914 s5r->cleanup = GNUNET_YES;
2915 s5r->cleanup_sock = GNUNET_YES;
2917 GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
2923 s5r->remote_sock = GNUNET_NETWORK_socket_create (AF_INET,
2926 r_sin_addr = (struct in_addr*)(phost->h_addr);
2927 remote_ip = r_sin_addr->s_addr;
2928 memset(&remote_addr, 0, sizeof(remote_addr));
2929 remote_addr.sin_family = AF_INET;
2930 #if HAVE_SOCKADDR_IN_SIN_LEN
2931 remote_addr.sin_len = sizeof (remote_addr);
2933 remote_addr.sin_addr.s_addr = remote_ip;
2934 remote_addr.sin_port = req_port;
2936 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2937 "target server: %s:%u\n", inet_ntoa(remote_addr.sin_addr),
2941 GNUNET_NETWORK_socket_connect ( s5r->remote_sock,
2942 (const struct sockaddr*)&remote_addr,
2943 sizeof (remote_addr)))
2944 && (errno != EINPROGRESS))
2946 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "connect");
2947 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2948 "socket request error...\n");
2949 s_resp->version = 0x05;
2950 s_resp->reply = 0x01;
2952 GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
2959 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2960 "new remote connection\n");
2962 s_resp->version = 0x05;
2963 s_resp->reply = 0x00;
2964 s_resp->reserved = 0x00;
2965 s_resp->addr_type = 0x01;
2967 s5r->state = SOCKS5_DATA_TRANSFER;
2970 GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
2974 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
2982 if (s5r->state == SOCKS5_DATA_TRANSFER)
2984 if ((s5r->remote_sock == NULL) || (s5r->rbuf_len == 0))
2986 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2987 "Closing connection to client\n");
2988 if (s5r->rtask != GNUNET_SCHEDULER_NO_TASK)
2989 GNUNET_SCHEDULER_cancel (s5r->rtask);
2990 if (s5r->fwdwtask != GNUNET_SCHEDULER_NO_TASK)
2991 GNUNET_SCHEDULER_cancel (s5r->fwdwtask);
2992 if (s5r->fwdrtask != GNUNET_SCHEDULER_NO_TASK)
2993 GNUNET_SCHEDULER_cancel (s5r->fwdrtask);
2994 if (s5r->fwdrtask != GNUNET_SCHEDULER_NO_TASK)
2995 GNUNET_SCHEDULER_cancel (s5r->fwdrtask);
2997 if (s5r->remote_sock != NULL)
2998 GNUNET_NETWORK_socket_close (s5r->remote_sock);
2999 GNUNET_NETWORK_socket_close (s5r->sock);
3004 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3005 "forwarding %d bytes from client\n", s5r->rbuf_len);
3008 GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
3010 &do_write_remote, s5r);
3012 if (s5r->fwdrtask == GNUNET_SCHEDULER_NO_TASK)
3015 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
3017 &do_read_remote, s5r);
3027 * Accept new incoming connections
3029 * @param cls the closure
3030 * @param tc the scheduler context
3033 do_accept (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3035 struct GNUNET_NETWORK_Handle *s;
3036 struct Socks5Request *s5r;
3038 ltask = GNUNET_SCHEDULER_NO_TASK;
3039 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
3042 ltask = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
3046 s = GNUNET_NETWORK_socket_accept (lsock, NULL, NULL);
3050 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "accept");
3054 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3055 "Got an inbound connection, waiting for data\n");
3057 s5r = GNUNET_malloc (sizeof (struct Socks5Request));
3059 s5r->state = SOCKS5_INIT;
3060 s5r->wtask = GNUNET_SCHEDULER_NO_TASK;
3061 s5r->fwdwtask = GNUNET_SCHEDULER_NO_TASK;
3062 s5r->fwdrtask = GNUNET_SCHEDULER_NO_TASK;
3063 s5r->rtask = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
3070 * Task run on shutdown
3072 * @param cls closure
3073 * @param tc task context
3076 do_shutdown (void *cls,
3077 const struct GNUNET_SCHEDULER_TaskContext *tc)
3080 struct MhdHttpList *hd;
3081 struct MhdHttpList *tmp_hd;
3082 struct NetworkHandleList *nh;
3083 struct NetworkHandleList *tmp_nh;
3084 struct ProxyCurlTask *ctask;
3085 struct ProxyCurlTask *ctask_tmp;
3086 struct ProxyUploadData *pdata;
3088 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3089 "Shutting down...\n");
3091 gnutls_global_deinit ();
3093 if (NULL != local_gns_zone)
3094 GNUNET_free (local_gns_zone);
3095 if (NULL != local_private_zone)
3096 GNUNET_free (local_private_zone);
3097 if (NULL != local_shorten_zone)
3098 GNUNET_free (local_shorten_zone);
3100 if (GNUNET_SCHEDULER_NO_TASK != curl_download_task)
3102 GNUNET_SCHEDULER_cancel (curl_download_task);
3103 curl_download_task = GNUNET_SCHEDULER_NO_TASK;
3106 for (hd = mhd_httpd_head; hd != NULL; hd = tmp_hd)
3110 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3111 "Stopping daemon\n");
3113 if (GNUNET_SCHEDULER_NO_TASK != hd->httpd_task)
3115 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3116 "Stopping select task %d\n",
3118 GNUNET_SCHEDULER_cancel (hd->httpd_task);
3119 hd->httpd_task = GNUNET_SCHEDULER_NO_TASK;
3122 if (NULL != hd->daemon)
3124 MHD_stop_daemon (hd->daemon);
3128 for (nh = hd->socket_handles_head; nh != NULL; nh = tmp_nh)
3132 GNUNET_NETWORK_socket_close (nh->h);
3137 if (NULL != hd->proxy_cert)
3139 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3140 "Free certificate\n");
3141 GNUNET_free (hd->proxy_cert);
3147 for (ctask=ctasks_head; ctask != NULL; ctask=ctask_tmp)
3149 ctask_tmp = ctask->next;
3151 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3152 "Cleaning up cURL task\n");
3154 if (ctask->curl != NULL)
3155 curl_easy_cleanup (ctask->curl);
3157 if (NULL != ctask->headers)
3158 curl_slist_free_all (ctask->headers);
3159 if (NULL != ctask->resolver)
3160 curl_slist_free_all (ctask->resolver);
3162 if (NULL != ctask->response)
3163 MHD_destroy_response (ctask->response);
3165 pdata = ctask->upload_data_head;
3167 //FIXME free pdata here
3168 for (; pdata != NULL; pdata = ctask->upload_data_head)
3170 GNUNET_CONTAINER_DLL_remove (ctask->upload_data_head,
3171 ctask->upload_data_tail,
3173 GNUNET_free_non_null (pdata->filename);
3174 GNUNET_free_non_null (pdata->content_type);
3175 GNUNET_free_non_null (pdata->key);
3176 GNUNET_free_non_null (pdata->value);
3177 GNUNET_free (pdata);
3180 GNUNET_free (ctask);
3182 curl_multi_cleanup (curl_multi);
3184 GNUNET_GNS_disconnect (gns_handle);
3189 * Compiles a regex for us
3191 * @param re ptr to re struct
3192 * @param rt the expression to compile
3193 * @return 0 on success
3196 compile_regex (regex_t *re, const char* rt)
3201 status = regcomp (re, rt, REG_EXTENDED|REG_NEWLINE);
3204 regerror (status, re, err, 1024);
3205 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3206 "Regex error compiling '%s': %s\n", rt, err);
3214 * Loads the users local zone key
3216 * @return GNUNET_YES on success
3219 load_local_zone_key (const struct GNUNET_CONFIGURATION_Handle *cfg)
3222 struct GNUNET_CRYPTO_RsaPrivateKey *key = NULL;
3223 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pkey;
3224 struct GNUNET_CRYPTO_ShortHashCode *zone = NULL;
3225 struct GNUNET_CRYPTO_ShortHashAsciiEncoded zonename;
3227 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "gns",
3228 "ZONEKEY", &keyfile))
3230 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3231 "Unable to load zone key config value!\n");
3235 if (GNUNET_NO == GNUNET_DISK_file_test (keyfile))
3237 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3238 "Unable to load zone key %s!\n", keyfile);
3239 GNUNET_free(keyfile);
3243 key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
3244 GNUNET_CRYPTO_rsa_key_get_public (key, &pkey);
3245 local_gns_zone = GNUNET_malloc (sizeof (struct GNUNET_CRYPTO_ShortHashCode));
3246 GNUNET_CRYPTO_short_hash(&pkey,
3247 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
3249 zone = local_gns_zone;
3250 GNUNET_CRYPTO_short_hash_to_enc (zone, &zonename);
3251 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3252 "Using zone: %s!\n", &zonename);
3253 GNUNET_CRYPTO_rsa_key_free(key);
3254 GNUNET_free(keyfile);
3257 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "gns",
3258 "PRIVATE_ZONEKEY", &keyfile))
3260 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3261 "Unable to load private zone key config value!\n");
3264 if ((NULL != keyfile) && (GNUNET_NO == GNUNET_DISK_file_test (keyfile)))
3266 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3267 "Unable to load private zone key %s!\n", keyfile);
3268 GNUNET_free(keyfile);
3272 key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
3273 GNUNET_CRYPTO_rsa_key_get_public (key, &pkey);
3274 local_private_zone = GNUNET_malloc (sizeof (struct GNUNET_CRYPTO_ShortHashCode));
3275 GNUNET_CRYPTO_short_hash(&pkey,
3276 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
3277 local_private_zone);
3278 GNUNET_CRYPTO_short_hash_to_enc (zone, &zonename);
3279 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3280 "Using private zone: %s!\n", &zonename);
3281 GNUNET_CRYPTO_rsa_key_free(key);
3282 GNUNET_free(keyfile);
3286 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "gns",
3287 "SHORTEN_ZONEKEY", &keyfile))
3289 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3290 "Unable to load shorten zone key config value!\n");
3293 if ((NULL != keyfile) && (GNUNET_NO == GNUNET_DISK_file_test (keyfile)))
3295 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3296 "Unable to load shorten zone key %s!\n", keyfile);
3297 GNUNET_free(keyfile);
3301 key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
3302 GNUNET_CRYPTO_rsa_key_get_public (key, &pkey);
3303 local_shorten_zone = GNUNET_malloc (sizeof (struct GNUNET_CRYPTO_ShortHashCode));
3304 GNUNET_CRYPTO_short_hash(&pkey,
3305 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
3306 local_shorten_zone);
3307 GNUNET_CRYPTO_short_hash_to_enc (zone, &zonename);
3308 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3309 "Using shorten zone: %s!\n", &zonename);
3310 GNUNET_CRYPTO_rsa_key_free(key);
3311 GNUNET_free(keyfile);
3318 * Main function that will be run
3320 * @param cls closure
3321 * @param args remaining command-line arguments
3322 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
3323 * @param cfg configuration
3326 run (void *cls, char *const *args, const char *cfgfile,
3327 const struct GNUNET_CONFIGURATION_Handle *cfg)
3329 struct sockaddr_in sa;
3330 struct MhdHttpList *hd;
3331 char* cafile_cfg = NULL;
3333 #if !HAVE_MHD_NO_LISTEN_SOCKET
3335 char* proxy_sockfile;
3336 struct sockaddr_un mhd_unix_sock_addr;
3339 curl_multi = curl_multi_init ();
3341 if (NULL == curl_multi)
3343 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3344 "Failed to create cURL multo handle!\n");
3348 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3350 cafile = cafile_opt;
3353 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "gns-proxy",
3357 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3358 "Unable to load proxy CA config value!\n");
3359 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3360 "No proxy CA provided!\n");
3363 cafile = cafile_cfg;
3365 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3366 "Using %s as CA\n", cafile);
3368 gnutls_global_init ();
3369 gnutls_x509_crt_init (&proxy_ca.cert);
3370 gnutls_x509_privkey_init (&proxy_ca.key);
3372 if ( (GNUNET_OK != load_cert_from_file (proxy_ca.cert, cafile)) ||
3373 (GNUNET_OK != load_key_from_file (proxy_ca.key, cafile)) )
3375 // FIXME: release resources...
3379 GNUNET_free_non_null (cafile_cfg);
3381 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3382 "Loading Template\n");
3384 compile_regex (&re_dotplus, (char*) RE_A_HREF);
3386 gns_handle = GNUNET_GNS_connect (cfg);
3388 if (GNUNET_NO == load_local_zone_key (cfg))
3390 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3391 "Unable to load zone!\n");
3395 if (NULL == gns_handle)
3397 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3398 "Unable to connect to GNS!\n");
3402 memset (&sa, 0, sizeof (sa));
3403 sa.sin_family = AF_INET;
3404 sa.sin_port = htons (port);
3405 #if HAVE_SOCKADDR_IN_SIN_LEN
3406 sa.sin_len = sizeof (sa);
3409 lsock = GNUNET_NETWORK_socket_create (AF_INET,
3413 if ((NULL == lsock) ||
3415 GNUNET_NETWORK_socket_bind (lsock, (const struct sockaddr *) &sa,
3418 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3419 "Failed to create listen socket bound to `%s'",
3420 GNUNET_a2s ((const struct sockaddr *) &sa, sizeof (sa)));
3422 GNUNET_NETWORK_socket_close (lsock);
3426 if (GNUNET_OK != GNUNET_NETWORK_socket_listen (lsock, 5))
3428 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3429 "Failed to listen on socket bound to `%s'",
3430 GNUNET_a2s ((const struct sockaddr *) &sa, sizeof (sa)));
3434 ltask = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
3435 lsock, &do_accept, NULL);
3440 if (0 != curl_global_init (CURL_GLOBAL_WIN32))
3442 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3443 "cURL global init failed!\n");
3447 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3448 "Proxy listens on port %u\n",
3451 mhd_httpd_head = NULL;
3452 mhd_httpd_tail = NULL;
3453 total_mhd_connections = 0;
3454 #ifndef HAVE_MHD_NO_LISTEN_SOCKET
3455 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "gns-proxy",
3459 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3460 "Specify PROXY_UNIXPATH in gns-proxy config section!\n");
3464 mhd_unix_socket = GNUNET_NETWORK_socket_create (AF_UNIX,
3468 if (NULL == mhd_unix_socket)
3470 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3471 "Unable to create unix domain socket!\n");
3475 mhd_unix_sock_addr.sun_family = AF_UNIX;
3476 strcpy (mhd_unix_sock_addr.sun_path, proxy_sockfile);
3479 mhd_unix_sock_addr.sun_path[0] = '\0';
3481 #if HAVE_SOCKADDR_IN_SIN_LEN
3482 mhd_unix_sock_addr.sun_len = (u_char) sizeof (struct sockaddr_un);
3485 len = strlen (proxy_sockfile) + sizeof(AF_UNIX);
3487 GNUNET_free (proxy_sockfile);
3489 if (GNUNET_OK != GNUNET_NETWORK_socket_bind (mhd_unix_socket,
3490 (struct sockaddr*)&mhd_unix_sock_addr,
3493 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3494 "Unable to bind unix domain socket!\n");
3498 if (GNUNET_OK != GNUNET_NETWORK_socket_listen (mhd_unix_socket,
3501 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3502 "Unable to listen on unix domain socket!\n");
3507 hd = GNUNET_malloc (sizeof (struct MhdHttpList));
3508 hd->is_ssl = GNUNET_NO;
3509 strcpy (hd->domain, "");
3510 httpd = MHD_start_daemon (MHD_USE_DEBUG
3511 #if HAVE_MHD_NO_LISTEN_SOCKET
3512 | MHD_USE_NO_LISTEN_SOCKET,
3515 , 4444, //Dummy port
3518 &create_response, hd,
3519 #if !HAVE_MHD_NO_LISTEN_SOCKET
3520 MHD_OPTION_LISTEN_SOCKET, GNUNET_NETWORK_get_fd (mhd_unix_socket),
3522 MHD_OPTION_CONNECTION_LIMIT, MHD_MAX_CONNECTIONS,
3523 MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) 16,
3524 MHD_OPTION_NOTIFY_COMPLETED,
3526 MHD_OPTION_URI_LOG_CALLBACK, &mhd_log_callback, NULL,
3529 GNUNET_assert (httpd != NULL);
3531 hd->httpd_task = GNUNET_SCHEDULER_NO_TASK;
3533 GNUNET_CONTAINER_DLL_insert (mhd_httpd_head, mhd_httpd_tail, hd);
3537 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
3538 &do_shutdown, NULL);
3544 * The main function for gnunet-gns-proxy.
3546 * @param argc number of arguments from the command line
3547 * @param argv command line arguments
3548 * @return 0 ok, 1 on error
3551 main (int argc, char *const *argv)
3553 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
3555 gettext_noop ("listen on specified port"), 1,
3556 &GNUNET_GETOPT_set_string, &port},
3557 {'a', "authority", NULL,
3558 gettext_noop ("pem file to use as CA"), 1,
3559 &GNUNET_GETOPT_set_string, &cafile_opt},
3560 GNUNET_GETOPT_OPTION_END
3565 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
3569 GNUNET_log_setup ("gnunet-gns-proxy", "WARNING", NULL);
3572 GNUNET_PROGRAM_run (argc, argv, "gnunet-gns-proxy",
3573 _("GNUnet GNS proxy"),
3575 &run, NULL)) ? 0 : 1;
3576 GNUNET_free_non_null ((char*)argv);