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;
335 struct GNUNET_GNS_ShortenRequest *shorten_task;
340 /* start of match in buffer */
343 /* end of match in buffer */
346 /* associated proxycurltask */
347 struct ProxyCurlTask *ctask;
351 * Struct for set-cookies
353 struct ProxySetCookieHeader
356 struct ProxySetCookieHeader *next;
359 struct ProxySetCookieHeader *prev;
366 * Post data structure
368 struct ProxyUploadData
371 struct ProxyUploadData *next;
374 struct ProxyUploadData *prev;
382 size_t content_length;
395 /* The port the proxy is running on (default 7777) */
396 static unsigned long port = GNUNET_GNS_PROXY_PORT;
398 /* The CA file (pem) to use for the proxy CA */
399 static char* cafile_opt;
401 /* The listen socket of the proxy */
402 static struct GNUNET_NETWORK_Handle *lsock;
404 /* The listen task ID */
405 GNUNET_SCHEDULER_TaskIdentifier ltask;
407 /* The cURL download task */
408 GNUNET_SCHEDULER_TaskIdentifier curl_download_task;
410 /* The non SSL httpd daemon handle */
411 static struct MHD_Daemon *httpd;
413 /* Number of current mhd connections */
414 static unsigned int total_mhd_connections;
416 /* The cURL multi handle */
417 static CURLM *curl_multi;
419 /* Handle to the GNS service */
420 static struct GNUNET_GNS_Handle *gns_handle;
422 /* DLL for ProxyCurlTasks */
423 static struct ProxyCurlTask *ctasks_head;
425 /* DLL for ProxyCurlTasks */
426 static struct ProxyCurlTask *ctasks_tail;
428 /* DLL for http daemons */
429 static struct MhdHttpList *mhd_httpd_head;
431 /* DLL for http daemons */
432 static struct MhdHttpList *mhd_httpd_tail;
434 /* Handle to the regex for dotplus (.+) replacement in HTML */
435 static regex_t re_dotplus;
437 /* The users local GNS zone hash */
438 static struct GNUNET_CRYPTO_ShortHashCode *local_gns_zone;
440 /* The users local private zone */
441 static struct GNUNET_CRYPTO_ShortHashCode *local_private_zone;
443 /* The users local shorten zone */
444 static struct GNUNET_CRYPTO_ShortHashCode *local_shorten_zone;
446 /* The CA for SSL certificate generation */
447 static struct ProxyCA proxy_ca;
449 /* UNIX domain socket for mhd */
450 struct GNUNET_NETWORK_Handle *mhd_unix_socket;
452 /* Shorten zone private key */
453 struct GNUNET_CRYPTO_RsaPrivateKey *shorten_zonekey;
456 * Checks if name is in tld
458 * @param name the name to check
459 * @param tld the TLD to check for
460 * @return GNUNET_YES or GNUNET_NO
463 is_tld(const char* name, const char* tld)
467 if (strlen(name) <= strlen(tld))
470 offset = strlen(name) - strlen(tld);
471 if (0 != strcmp (name+offset, tld))
473 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
474 "%s is not in .%s TLD\n", name, tld);
482 * convert integer to string representation
485 * @return the character
487 char i_to_hexchar (char i)
489 static char hexmap[] = "0123456789abcdef";
490 GNUNET_assert (sizeof (hexmap) > (i & 15));
491 return hexmap[i & 15];
495 * Escape given 0-terminated string
497 * @param to_esc string to escapse
498 * @return allocated new escaped string (MUST free!)
501 escape_to_urlenc (const char *to_esc)
503 char *pos = (char*)to_esc;
504 char *res = GNUNET_malloc (strlen (to_esc) * 3 + 1);
509 if (isalnum (*pos) ||
510 ('-' == *pos) || ('_' == *pos) ||
511 ('.' == *pos) || ('~' == *pos))
513 else if (' ' == *pos)
518 *rpos++ = i_to_hexchar (*pos >> 4);
519 *rpos++ = i_to_hexchar (*pos >> 15);
528 con_post_data_iter (void *cls,
529 enum MHD_ValueKind kind,
531 const char *filename,
532 const char *content_type,
533 const char *transfer_encoding,
538 struct ProxyCurlTask* ctask = cls;
539 struct ProxyUploadData* pdata;
543 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
544 "Got POST data: '%s : %s' at offset %llu size %lld\n",
545 key, data, off, size);
547 GNUNET_assert (NULL != ctask->post_type);
549 if (0 == strcasecmp (MHD_HTTP_POST_ENCODING_MULTIPART_FORMDATA,
552 ctask->is_httppost = GNUNET_YES;
556 pdata = GNUNET_malloc (sizeof (struct ProxyUploadData));
557 pdata->key = strdup (key);
559 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
562 if (NULL != filename)
564 pdata->filename = strdup (filename);
565 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
566 "Filename %s\n", filename);
569 if (NULL != content_type)
571 pdata->content_type = strdup (content_type);
572 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
573 "Content-Type %s\n", content_type);
576 pdata->value = GNUNET_malloc (size);
577 pdata->total_bytes = size;
578 memcpy (pdata->value, data, size);
579 GNUNET_CONTAINER_DLL_insert_tail (ctask->upload_data_head,
580 ctask->upload_data_tail,
583 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
584 "Copied %lld bytes of POST Data\n", size);
588 pdata = ctask->upload_data_tail;
589 new_value = GNUNET_malloc (size + pdata->total_bytes);
590 memcpy (new_value, pdata->value, pdata->total_bytes);
591 memcpy (new_value+off, data, size);
592 GNUNET_free (pdata->value);
593 pdata->value = new_value;
594 pdata->total_bytes += size;
600 if (0 != strcasecmp (MHD_HTTP_POST_ENCODING_FORM_URLENCODED,
606 ctask->is_httppost = GNUNET_NO;
608 if (NULL != ctask->curl)
609 curl_easy_pause (ctask->curl, CURLPAUSE_CONT);
614 pdata = GNUNET_malloc (sizeof (struct ProxyUploadData));
615 enc = escape_to_urlenc (key);
616 pdata->value = GNUNET_malloc (strlen (enc) + 3);
617 if (NULL != ctask->upload_data_head)
619 pdata->value[0] = '&';
620 memcpy (pdata->value+1, enc, strlen (enc));
623 memcpy (pdata->value, enc, strlen (enc));
624 pdata->value[strlen (pdata->value)] = '=';
625 pdata->bytes_left = strlen (pdata->value);
626 pdata->total_bytes = pdata->bytes_left;
629 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
630 "Escaped POST key: '%s'\n",
633 GNUNET_CONTAINER_DLL_insert_tail (ctask->upload_data_head,
634 ctask->upload_data_tail,
639 pdata = GNUNET_malloc (sizeof (struct ProxyUploadData));
640 enc = escape_to_urlenc (data);
641 pdata->value = GNUNET_malloc (strlen (enc) + 1);
642 memcpy (pdata->value, enc, strlen (enc));
643 pdata->bytes_left = strlen (pdata->value);
644 pdata->total_bytes = pdata->bytes_left;
647 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
648 "Escaped POST value: '%s'\n",
651 GNUNET_CONTAINER_DLL_insert_tail (ctask->upload_data_head,
652 ctask->upload_data_tail,
659 * Read HTTP request header field 'Host'
661 * @param cls buffer to write to
662 * @param kind value kind
663 * @param key field key
664 * @param value field value
665 * @return MHD_NO when Host found
668 con_val_iter (void *cls,
669 enum MHD_ValueKind kind,
673 struct ProxyCurlTask *ctask = cls;
674 char* buf = ctask->host;
680 if (0 == strcmp ("Host", key))
682 port = strstr (value, ":");
685 strncpy (buf, value, port-value);
687 if ((1 != sscanf (port, "%u", &uport)) ||
688 (uport > UINT16_MAX) ||
690 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
691 "Unable to parse port!\n");
693 ctask->port = (uint16_t) uport;
700 if (0 == strcmp ("Accept-Encoding", key))
705 if (0 == strcasecmp (MHD_HTTP_HEADER_CONTENT_TYPE,
708 if (0 == strncasecmp (value,
709 MHD_HTTP_POST_ENCODING_FORM_URLENCODED,
710 strlen (MHD_HTTP_POST_ENCODING_FORM_URLENCODED)))
711 ctask->post_type = MHD_HTTP_POST_ENCODING_FORM_URLENCODED;
712 else if (0 == strncasecmp (value,
713 MHD_HTTP_POST_ENCODING_MULTIPART_FORMDATA,
714 strlen (MHD_HTTP_POST_ENCODING_MULTIPART_FORMDATA)))
715 ctask->post_type = MHD_HTTP_POST_ENCODING_MULTIPART_FORMDATA;
717 ctask->post_type = NULL;
721 cstr = GNUNET_malloc (strlen (key) + strlen (hdr_val) + 3);
722 GNUNET_snprintf (cstr, strlen (key) + strlen (hdr_val) + 3,
723 "%s: %s", key, hdr_val);
725 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
726 "Client Header: %s\n", cstr);
728 ctask->headers = curl_slist_append (ctask->headers, cstr);
736 * Callback for MHD response
739 * @param pos in buffer
741 * @param max space in buffer
742 * @return number of bytes written
745 mhd_content_cb (void *cls,
751 * Check HTTP response header for mime
753 * @param buffer curl buffer
754 * @param size curl blocksize
755 * @param nmemb curl blocknumber
757 * @return size of read bytes
760 curl_check_hdr (void *buffer, size_t size, size_t nmemb, void *cls)
762 size_t bytes = size * nmemb;
763 struct ProxyCurlTask *ctask = cls;
764 int html_mime_len = strlen (HTML_HDR_CONTENT);
765 int cookie_hdr_len = strlen (MHD_HTTP_HEADER_SET_COOKIE);
766 char hdr_mime[html_mime_len+1];
767 char hdr_generic[bytes+1];
768 char new_cookie_hdr[bytes+strlen (ctask->leho)+1];
769 char new_location[MAX_HTTP_URI_LENGTH+500];
779 char cors_hdr[strlen (ctask->leho) + strlen ("https://")];
781 if (NULL == ctask->response)
783 /* FIXME: get total size from curl (if available) */
784 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
785 "Creating response for %s\n", ctask->url);
786 ctask->response = MHD_create_response_from_callback (MHD_SIZE_UNKNOWN,
787 sizeof (ctask->buffer),
792 /* if we have a leho add a CORS header */
793 if (0 != strcmp ("", ctask->leho))
795 /* We could also allow ssl and http here */
796 if (ctask->mhd->is_ssl)
797 sprintf (cors_hdr, "https://%s", ctask->leho);
799 sprintf (cors_hdr, "http://%s", ctask->leho);
801 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
802 "MHD: Adding CORS header field %s\n",
805 if (GNUNET_NO == MHD_add_response_header (ctask->response,
806 "Access-Control-Allow-Origin",
809 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
810 "MHD: Error adding CORS header field %s\n",
814 ctask->ready_to_queue = GNUNET_YES;
817 if (html_mime_len <= bytes)
819 memcpy (hdr_mime, buffer, html_mime_len);
820 hdr_mime[html_mime_len] = '\0';
822 if (0 == strcmp (hdr_mime, HTML_HDR_CONTENT))
824 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
825 "Got HTML HTTP response header\n");
826 ctask->parse_content = GNUNET_YES;
830 if (cookie_hdr_len > bytes)
833 memcpy (hdr_generic, buffer, bytes);
834 hdr_generic[bytes] = '\0';
836 if ('\n' == hdr_generic[bytes-1])
837 hdr_generic[bytes-1] = '\0';
839 if (hdr_generic[bytes-2] == '\r')
840 hdr_generic[bytes-2] = '\0';
842 if (0 == memcmp (hdr_generic,
843 MHD_HTTP_HEADER_SET_COOKIE,
846 ndup = GNUNET_strdup (hdr_generic+cookie_hdr_len+1);
847 memset (new_cookie_hdr, 0, sizeof (new_cookie_hdr));
848 tok = strtok (ndup, ";");
850 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
851 "Looking for cookie in : %s\n", hdr_generic);
853 for (; tok != NULL; tok = strtok (NULL, ";"))
855 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
856 "Got Cookie token: %s\n", tok);
857 //memcpy (new_cookie_hdr+offset, tok, strlen (tok));
858 if (0 == memcmp (tok, " domain", strlen (" domain")))
860 cookie_domain = tok + strlen (" domain") + 1;
862 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
863 "Got Set-Cookie Domain: %s\n", cookie_domain);
865 if (strlen (cookie_domain) < strlen (ctask->leho))
867 delta_cdomain = strlen (ctask->leho) - strlen (cookie_domain);
868 if (0 == strcmp (cookie_domain, ctask->leho + (delta_cdomain)))
870 GNUNET_snprintf (new_cookie_hdr+offset,
871 sizeof (new_cookie_hdr),
872 " domain=%s", ctask->authority);
873 offset += strlen (" domain=") + strlen (ctask->authority);
874 new_cookie_hdr[offset] = ';';
879 else if (strlen (cookie_domain) == strlen (ctask->leho))
881 if (0 == strcmp (cookie_domain, ctask->leho))
883 GNUNET_snprintf (new_cookie_hdr+offset,
884 sizeof (new_cookie_hdr),
885 " domain=%s", ctask->host);
886 offset += strlen (" domain=") + strlen (ctask->host);
887 new_cookie_hdr[offset] = ';';
892 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
893 "Cookie domain invalid\n");
897 memcpy (new_cookie_hdr+offset, tok, strlen (tok));
898 offset += strlen (tok);
899 new_cookie_hdr[offset] = ';';
905 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
906 "Got Set-Cookie HTTP header %s\n", new_cookie_hdr);
908 if (GNUNET_NO == MHD_add_response_header (ctask->response,
909 MHD_HTTP_HEADER_SET_COOKIE,
912 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
913 "MHD: Error adding set-cookie header field %s\n",
914 hdr_generic+cookie_hdr_len+1);
919 ndup = GNUNET_strdup (hdr_generic);
920 hdr_type = strtok (ndup, ":");
922 if (NULL == hdr_type)
928 hdr_val = strtok (NULL, "");
938 if (0 == strcasecmp (MHD_HTTP_HEADER_LOCATION, hdr_type))
940 if (ctask->mhd->is_ssl)
942 sprintf (leho_host, "https://%s", ctask->leho);
943 sprintf (real_host, "https://%s", ctask->host);
947 sprintf (leho_host, "http://%s", ctask->leho);
948 sprintf (real_host, "http://%s", ctask->host);
951 if (0 == memcmp (leho_host, hdr_val, strlen (leho_host)))
953 sprintf (new_location, "%s%s", real_host, hdr_val+strlen (leho_host));
954 hdr_val = new_location;
958 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
959 "Trying to set %s: %s\n",
962 if (GNUNET_NO == MHD_add_response_header (ctask->response,
966 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
967 "MHD: Error adding %s header field %s\n",
978 * @param hd a http daemon list entry
981 run_httpd (struct MhdHttpList *hd);
992 * Task run whenever HTTP server operations are pending.
995 * @param tc sched context
999 const struct GNUNET_SCHEDULER_TaskContext *tc);
1002 run_mhd_now (struct MhdHttpList *hd)
1004 if (GNUNET_SCHEDULER_NO_TASK != hd->httpd_task)
1006 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1007 "MHD: killing old task\n");
1008 GNUNET_SCHEDULER_cancel (hd->httpd_task);
1010 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1011 "MHD: Scheduling MHD now\n");
1012 hd->httpd_task = GNUNET_SCHEDULER_add_now (&do_httpd, hd);
1016 * Ask cURL for the select sets and schedule download
1019 curl_download_prepare ();
1022 * Callback to free content
1024 * @param cls content to free
1025 * @param tc task context
1028 mhd_content_free (void *cls,
1029 const struct GNUNET_SCHEDULER_TaskContext *tc)
1031 struct ProxyCurlTask *ctask = cls;
1032 struct ProxyUploadData *pdata;
1034 GNUNET_assert (NULL == ctask->pp_match_head);
1035 if (NULL != ctask->headers)
1036 curl_slist_free_all (ctask->headers);
1038 if (NULL != ctask->headers)
1039 curl_slist_free_all (ctask->resolver);
1041 if (NULL != ctask->response)
1042 MHD_destroy_response (ctask->response);
1044 if (NULL != ctask->post_handler)
1045 MHD_destroy_post_processor (ctask->post_handler);
1047 if (GNUNET_SCHEDULER_NO_TASK != ctask->pp_task)
1048 GNUNET_SCHEDULER_cancel (ctask->pp_task);
1050 for (pdata = ctask->upload_data_head; NULL != pdata; pdata = ctask->upload_data_head)
1052 GNUNET_CONTAINER_DLL_remove (ctask->upload_data_head,
1053 ctask->upload_data_tail,
1055 GNUNET_free_non_null (pdata->filename);
1056 GNUNET_free_non_null (pdata->content_type);
1057 GNUNET_free_non_null (pdata->key);
1058 GNUNET_free_non_null (pdata->value);
1059 GNUNET_free (pdata);
1061 GNUNET_free (ctask);
1066 * Callback for MHD response
1068 * @param cls closure
1069 * @param pos in buffer
1071 * @param max space in buffer
1072 * @return number of bytes written
1075 mhd_content_cb (void *cls,
1080 struct ProxyCurlTask *ctask = cls;
1081 struct ProxyREMatch *re_match;
1083 long long int bytes_to_copy = ctask->buffer_write_ptr - ctask->buffer_read_ptr;
1085 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1086 "MHD: content cb for %s. To copy: %lld\n",
1087 ctask->url, bytes_to_copy);
1088 GNUNET_assert (bytes_to_copy >= 0);
1090 if ((GNUNET_YES == ctask->download_is_finished) &&
1091 (GNUNET_NO == ctask->download_error) &&
1092 (0 == bytes_to_copy)) /* &&
1093 (BUF_WAIT_FOR_CURL == ctask->buf_status))*/
1095 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1096 "MHD: sending response for %s\n", ctask->url);
1097 ctask->download_in_progress = GNUNET_NO;
1098 run_mhd_now (ctask->mhd);
1099 GNUNET_SCHEDULER_add_now (&mhd_content_free, ctask);
1100 total_mhd_connections--;
1101 return MHD_CONTENT_READER_END_OF_STREAM;
1104 if ((GNUNET_YES == ctask->download_error) &&
1105 (GNUNET_YES == ctask->download_is_finished) &&
1106 (0 == bytes_to_copy)) /* &&
1107 (BUF_WAIT_FOR_CURL == ctask->buf_status))*/
1109 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1110 "MHD: sending error response\n");
1111 ctask->download_in_progress = GNUNET_NO;
1112 run_mhd_now (ctask->mhd);
1113 GNUNET_SCHEDULER_add_now (&mhd_content_free, ctask);
1114 total_mhd_connections--;
1115 return MHD_CONTENT_READER_END_WITH_ERROR;
1118 if ( ctask->buf_status == BUF_WAIT_FOR_CURL )
1122 for (re_match = ctask->pp_match_head; NULL != re_match; re_match = ctask->pp_match_head)
1124 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1125 "MHD: Processing PP %s\n",
1126 re_match->hostname);
1127 bytes_to_copy = re_match->start - ctask->buffer_read_ptr;
1128 GNUNET_assert (bytes_to_copy >= 0);
1130 if (bytes_to_copy+copied > max)
1132 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1133 "MHD: buffer in response too small for %d. Using available space (%d). (%s)\n",
1137 memcpy (buf+copied, ctask->buffer_read_ptr, max-copied);
1138 ctask->buffer_read_ptr += max-copied;
1140 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1141 "MHD: copied %d bytes\n", copied);
1145 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1146 "MHD: copying %d bytes to mhd response at offset %d\n",
1147 bytes_to_copy, ctask->buffer_read_ptr);
1148 memcpy (buf+copied, ctask->buffer_read_ptr, bytes_to_copy);
1149 copied += bytes_to_copy;
1151 if (GNUNET_NO == re_match->done)
1153 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1154 "MHD: Waiting for PP of %s\n", re_match->hostname);
1155 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1156 "MHD: copied %d bytes\n", copied);
1157 ctask->buffer_read_ptr += bytes_to_copy;
1161 if (strlen (re_match->result) > (max - copied))
1163 //FIXME partially copy domain here
1164 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1165 "MHD: buffer in response too small for %s! (%s)\n",
1168 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1169 "MHD: copied %d bytes\n", copied);
1170 ctask->buffer_read_ptr += bytes_to_copy;
1174 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1175 "MHD: Adding PP result %s to buffer\n",
1177 memcpy (buf+copied, re_match->result, strlen (re_match->result));
1178 copied += strlen (re_match->result);
1179 ctask->buffer_read_ptr = re_match->end;
1180 GNUNET_CONTAINER_DLL_remove (ctask->pp_match_head,
1181 ctask->pp_match_tail,
1183 GNUNET_free (re_match);
1186 bytes_to_copy = ctask->buffer_write_ptr - ctask->buffer_read_ptr;
1188 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1189 "MHD: copied: %d left: %d, space left in buf: %d\n",
1191 bytes_to_copy, max-copied);
1193 GNUNET_assert (0 <= bytes_to_copy);
1195 if (GNUNET_NO == ctask->download_is_finished)
1197 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1198 "MHD: Purging buffer\n");
1199 memmove (ctask->buffer, ctask->buffer_read_ptr, bytes_to_copy);
1200 ctask->buffer_read_ptr = ctask->buffer;
1201 ctask->buffer_write_ptr = ctask->buffer + bytes_to_copy;
1202 ctask->buffer[bytes_to_copy] = '\0';
1205 if (bytes_to_copy+copied > max)
1206 bytes_to_copy = max-copied;
1208 if (0 > bytes_to_copy)
1211 memcpy (buf+copied, ctask->buffer_read_ptr, bytes_to_copy);
1212 ctask->buffer_read_ptr += bytes_to_copy;
1213 copied += bytes_to_copy;
1214 ctask->buf_status = BUF_WAIT_FOR_CURL;
1216 if (NULL != ctask->curl)
1217 curl_easy_pause (ctask->curl, CURLPAUSE_CONT);
1219 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1220 "MHD: copied %d bytes\n", copied);
1221 run_mhd_now (ctask->mhd);
1227 * Shorten result callback
1229 * @param cls the proxycurltask
1230 * @param short_name the shortened name (NULL on error)
1233 process_shorten (void* cls, const char* short_name)
1235 struct ProxyREMatch *re_match = cls;
1236 char result[sizeof (re_match->result)];
1238 if (NULL == short_name)
1240 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1241 "PP: Unable to shorten %s\n",
1242 re_match->hostname);
1243 GNUNET_CONTAINER_DLL_remove (re_match->ctask->pp_match_head,
1244 re_match->ctask->pp_match_tail,
1246 GNUNET_free (re_match);
1250 if (0 == strcmp (short_name, re_match->ctask->leho))
1251 strcpy (result, re_match->ctask->host);
1253 strcpy (result, short_name);
1255 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1256 "PP: Shorten %s -> %s\n",
1260 if (re_match->is_ssl)
1261 sprintf (re_match->result, "href=\"https://%s", result);
1263 sprintf (re_match->result, "href=\"http://%s", result);
1265 re_match->done = GNUNET_YES;
1266 run_mhd_now (re_match->ctask->mhd);
1271 * Postprocess data in buffer. From read ptr to write ptr
1273 * @param cls the curlproxytask
1274 * @param tc task context
1277 postprocess_buffer (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1279 struct ProxyCurlTask *ctask = cls;
1280 struct ProxyREMatch *re_match;
1281 char* re_ptr = ctask->buffer_read_ptr;
1282 char re_hostname[255];
1283 regmatch_t m[RE_N_MATCHES];
1285 ctask->pp_task = GNUNET_SCHEDULER_NO_TASK;
1287 if (GNUNET_YES != ctask->parse_content)
1289 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1290 "PP: Not parsing content\n");
1291 ctask->buf_status = BUF_WAIT_FOR_MHD;
1292 run_mhd_now (ctask->mhd);
1296 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1297 "PP: We need to parse the HTML\n");
1299 /* 0 means match found */
1300 while (0 == regexec (&re_dotplus, re_ptr, RE_N_MATCHES, m, 0))
1302 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1303 "PP: regex match\n");
1305 GNUNET_assert (m[1].rm_so != -1);
1307 memset (re_hostname, 0, sizeof (re_hostname));
1308 memcpy (re_hostname, re_ptr+m[1].rm_so, (m[3].rm_eo-m[1].rm_so));
1311 re_match = GNUNET_malloc (sizeof (struct ProxyREMatch));
1312 re_match->start = re_ptr + m[0].rm_so;
1313 re_match->end = re_ptr + m[3].rm_eo;
1314 re_match->done = GNUNET_NO;
1315 re_match->ctask = ctask;
1317 if ('s' == *(re_ptr+m[1].rm_so-strlen("://")-1)) //FIXME strcmp
1318 re_match->is_ssl = GNUNET_YES;
1320 re_match->is_ssl = GNUNET_NO;
1322 strcpy (re_match->hostname, re_hostname);
1323 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1324 "PP: Got hostname %s\n", re_hostname);
1325 re_ptr += m[3].rm_eo;
1327 if (GNUNET_YES == is_tld (re_match->hostname, GNUNET_GNS_TLD_PLUS))
1329 re_match->hostname[strlen(re_match->hostname)-1] = '\0';
1330 strcpy (re_match->hostname+strlen(re_match->hostname),
1334 re_match->shorten_task = GNUNET_GNS_shorten_zone (gns_handle,
1340 re_match); //FIXME cancel appropriately
1342 GNUNET_CONTAINER_DLL_insert_tail (ctask->pp_match_head,
1343 ctask->pp_match_tail,
1347 ctask->buf_status = BUF_WAIT_FOR_MHD;
1348 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1349 "PP: No more matches\n");
1350 run_mhd_now (ctask->mhd);
1354 * Handle data from cURL
1356 * @param ptr pointer to the data
1357 * @param size number of blocks of data
1358 * @param nmemb blocksize
1359 * @param ctx the curlproxytask
1360 * @return number of bytes handled
1363 curl_download_cb (void *ptr, size_t size, size_t nmemb, void* ctx)
1365 const char *cbuf = ptr;
1366 size_t total = size * nmemb;
1367 struct ProxyCurlTask *ctask = ctx;
1368 size_t buf_space = sizeof (ctask->buffer) -
1369 (ctask->buffer_write_ptr-ctask->buffer);
1371 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1372 "CURL: Got %d. %d free in buffer\n",
1375 if (BUF_WAIT_FOR_CURL != ctask->buf_status)
1376 return CURL_WRITEFUNC_PAUSE;
1378 if (total > (buf_space - CURL_BUF_PADDING))
1380 if (ctask->buf_status == BUF_WAIT_FOR_CURL)
1382 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1383 "CURL: Buffer full starting postprocessing\n");
1384 ctask->buf_status = BUF_WAIT_FOR_PP;
1385 ctask->pp_task = GNUNET_SCHEDULER_add_now (&postprocess_buffer,
1387 return CURL_WRITEFUNC_PAUSE;
1390 /* we should not get called in that case */
1391 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1392 "CURL: called out of context and no space in buffer!\n");
1393 return CURL_WRITEFUNC_PAUSE;
1396 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1397 "CURL: Copying %d bytes to buffer (%s)\n", total, ctask->url);
1398 memcpy (ctask->buffer_write_ptr, cbuf, total);
1399 ctask->bytes_in_buffer += total;
1400 ctask->buffer_write_ptr += total;
1401 ctask->buffer_write_ptr[0] = '\0';
1408 * cURL callback for put data
1411 put_read_callback (void *buf, size_t size, size_t nmemb, void *cls)
1413 struct ProxyCurlTask *ctask = cls;
1414 struct ProxyUploadData *pdata = ctask->upload_data_head;
1415 size_t len = size * nmemb;
1419 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1420 "CURL: put read callback\n");
1423 return CURL_READFUNC_PAUSE;
1426 if (NULL == pdata->value)
1428 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1429 "CURL: Terminating PUT\n");
1431 GNUNET_CONTAINER_DLL_remove (ctask->upload_data_head,
1432 ctask->upload_data_tail,
1434 GNUNET_free (pdata);
1438 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1439 "CURL: read callback value %s\n", pdata->value);
1441 to_copy = pdata->bytes_left;
1445 pos = pdata->value + (pdata->total_bytes - pdata->bytes_left);
1446 memcpy (buf, pos, to_copy);
1447 pdata->bytes_left -= to_copy;
1448 if (pdata->bytes_left <= 0)
1450 GNUNET_free (pdata->value);
1451 GNUNET_CONTAINER_DLL_remove (ctask->upload_data_head,
1452 ctask->upload_data_tail,
1454 GNUNET_free (pdata);
1461 * cURL callback for post data
1464 post_read_callback (void *buf, size_t size, size_t nmemb, void *cls)
1466 struct ProxyCurlTask *ctask = cls;
1467 struct ProxyUploadData *pdata = ctask->upload_data_head;
1468 size_t len = size * nmemb;
1472 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1473 "CURL: read callback\n");
1476 return CURL_READFUNC_PAUSE;
1479 if (NULL == pdata->value)
1481 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1482 "CURL: Terminating POST data\n");
1484 GNUNET_CONTAINER_DLL_remove (ctask->upload_data_head,
1485 ctask->upload_data_tail,
1487 GNUNET_free (pdata);
1491 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1492 "CURL: read callback value %s\n", pdata->value);
1494 to_copy = pdata->bytes_left;
1498 pos = pdata->value + (pdata->total_bytes - pdata->bytes_left);
1499 memcpy (buf, pos, to_copy);
1500 pdata->bytes_left -= to_copy;
1501 if (pdata->bytes_left <= 0)
1503 GNUNET_free (pdata->value);
1504 GNUNET_CONTAINER_DLL_remove (ctask->upload_data_head,
1505 ctask->upload_data_tail,
1507 GNUNET_free (pdata);
1513 * Task that is run when we are ready to receive more data
1516 * @param cls closure
1517 * @param tc task context
1520 curl_task_download (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
1523 * Ask cURL for the select sets and schedule download
1526 curl_download_prepare ()
1533 struct GNUNET_NETWORK_FDSet *grs;
1534 struct GNUNET_NETWORK_FDSet *gws;
1536 struct GNUNET_TIME_Relative rtime;
1542 mret = curl_multi_fdset (curl_multi, &rs, &ws, &es, &max);
1544 if (mret != CURLM_OK)
1546 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1547 "%s failed at %s:%d: `%s'\n",
1548 "curl_multi_fdset", __FILE__, __LINE__,
1549 curl_multi_strerror (mret));
1550 //TODO cleanup here?
1554 mret = curl_multi_timeout (curl_multi, &to);
1555 rtime = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, to);
1557 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1558 "cURL multi fds: max=%d timeout=%llu\n", max, to);
1560 grs = GNUNET_NETWORK_fdset_create ();
1561 gws = GNUNET_NETWORK_fdset_create ();
1562 GNUNET_NETWORK_fdset_copy_native (grs, &rs, max + 1);
1563 GNUNET_NETWORK_fdset_copy_native (gws, &ws, max + 1);
1564 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1565 "Scheduling task cURL\n");
1567 if (curl_download_task != GNUNET_SCHEDULER_NO_TASK)
1568 GNUNET_SCHEDULER_cancel (curl_download_task);
1572 curl_download_task =
1573 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1576 &curl_task_download, curl_multi);
1578 else if (NULL != ctasks_head)
1580 /* as specified in curl docs */
1581 curl_download_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MILLISECONDS,
1582 &curl_task_download,
1585 GNUNET_NETWORK_fdset_destroy (gws);
1586 GNUNET_NETWORK_fdset_destroy (grs);
1591 * Task that is run when we are ready to receive more data
1594 * @param cls closure
1595 * @param tc task context
1598 curl_task_download (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1602 struct CURLMsg *msg;
1604 struct ProxyCurlTask *ctask;
1608 struct ProxyCurlTask *clean_head = NULL;
1609 struct ProxyCurlTask *clean_tail = NULL;
1611 curl_download_task = GNUNET_SCHEDULER_NO_TASK;
1613 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1615 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1616 "Shutdown requested while trying to download\n");
1620 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1628 mret = curl_multi_perform (curl_multi, &running);
1630 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1631 "Running curl tasks: %d\n", running);
1633 for (ctask = ctasks_head; NULL != ctask; ctask = ctask->next)
1635 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1636 "CTask: %s\n", ctask->url);
1640 if (num_ctasks != running)
1642 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1643 "%d ctasks, %d curl running\n", num_ctasks, running);
1649 msg = curl_multi_info_read (curl_multi, &msgnum);
1650 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1651 "Messages left: %d\n", msgnum);
1658 if ((msg->data.result != CURLE_OK) &&
1659 (msg->data.result != CURLE_GOT_NOTHING))
1661 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1662 "Download curl failed");
1664 for (ctask = ctasks_head; NULL != ctask; ctask = ctask->next)
1666 if (NULL == ctask->curl)
1669 if (memcmp (msg->easy_handle, ctask->curl, sizeof (CURL)) != 0)
1672 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1673 "CURL: Download failed for task %s: %s.\n",
1675 curl_easy_strerror (msg->data.result));
1676 ctask->download_is_finished = GNUNET_YES;
1677 ctask->download_error = GNUNET_YES;
1678 if (CURLE_OK == curl_easy_getinfo (ctask->curl,
1679 CURLINFO_RESPONSE_CODE,
1681 ctask->curl_response_code = resp_code;
1682 ctask->ready_to_queue = MHD_YES;
1683 ctask->buf_status = BUF_WAIT_FOR_MHD;
1684 run_mhd_now (ctask->mhd);
1686 GNUNET_CONTAINER_DLL_remove (ctasks_head, ctasks_tail,
1688 GNUNET_CONTAINER_DLL_insert (clean_head, clean_tail, ctask);
1691 GNUNET_assert (ctask != NULL);
1695 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1696 "CURL: download completed.\n");
1698 for (ctask = ctasks_head; NULL != ctask; ctask = ctask->next)
1700 if (NULL == ctask->curl)
1703 if (0 != memcmp (msg->easy_handle, ctask->curl, sizeof (CURL)))
1706 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1707 "CURL: completed task %s found.\n", ctask->url);
1708 if (CURLE_OK == curl_easy_getinfo (ctask->curl,
1709 CURLINFO_RESPONSE_CODE,
1711 ctask->curl_response_code = resp_code;
1714 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1715 "CURL: Completed ctask!\n");
1716 if (GNUNET_SCHEDULER_NO_TASK == ctask->pp_task)
1718 ctask->buf_status = BUF_WAIT_FOR_PP;
1719 ctask->pp_task = GNUNET_SCHEDULER_add_now (&postprocess_buffer,
1723 ctask->ready_to_queue = MHD_YES;
1724 ctask->download_is_finished = GNUNET_YES;
1726 /* We MUST not modify the multi handle else we loose messages */
1727 GNUNET_CONTAINER_DLL_remove (ctasks_head, ctasks_tail,
1729 GNUNET_CONTAINER_DLL_insert (clean_head, clean_tail, ctask);
1733 GNUNET_assert (ctask != NULL);
1735 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1736 "CURL: %s\n", curl_easy_strerror(msg->data.result));
1742 } while (msgnum > 0);
1744 for (ctask=clean_head; NULL != ctask; ctask = ctask->next)
1746 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1747 "CURL: Removing task %s.\n", ctask->url);
1748 curl_multi_remove_handle (curl_multi, ctask->curl);
1749 curl_easy_cleanup (ctask->curl);
1754 for (ctask=ctasks_head; NULL != ctask; ctask = ctask->next)
1759 if (num_ctasks != running)
1761 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1762 "CURL: %d tasks, %d running\n", num_ctasks, running);
1765 GNUNET_assert ( num_ctasks == running );
1767 } while (mret == CURLM_CALL_MULTI_PERFORM);
1769 if (mret != CURLM_OK)
1771 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "CURL: %s failed at %s:%d: `%s'\n",
1772 "curl_multi_perform", __FILE__, __LINE__,
1773 curl_multi_strerror (mret));
1775 curl_download_prepare();
1780 * Process LEHO lookup
1782 * @param cls the ctask
1783 * @param rd_count number of records returned
1784 * @param rd record data
1787 process_leho_lookup (void *cls,
1789 const struct GNUNET_NAMESTORE_RecordData *rd)
1791 struct ProxyCurlTask *ctask = cls;
1792 char hosthdr[262]; //256 + "Host: "
1796 struct hostent *phost;
1798 char resolvename[512];
1801 strcpy (ctask->leho, "");
1804 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1805 "No LEHO present!\n");
1807 for (i=0; i<rd_count; i++)
1809 if (rd[i].record_type != GNUNET_GNS_RECORD_LEHO)
1812 memcpy (ctask->leho, rd[i].data, rd[i].data_size);
1814 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1815 "Found LEHO %s for %s\n", ctask->leho, ctask->url);
1818 if (0 != strcmp (ctask->leho, ""))
1820 sprintf (hosthdr, "%s%s:%d", "Host: ", ctask->leho, ctask->port);
1821 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1822 "New HTTP header value: %s\n", hosthdr);
1823 ctask->headers = curl_slist_append (ctask->headers, hosthdr);
1824 GNUNET_assert (NULL != ctask->headers);
1825 ret = curl_easy_setopt (ctask->curl, CURLOPT_HTTPHEADER, ctask->headers);
1826 if (CURLE_OK != ret)
1828 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "%s failed at %s:%d: `%s'\n",
1829 "curl_easy_setopt", __FILE__, __LINE__, curl_easy_strerror(ret));
1834 if (ctask->mhd->is_ssl)
1836 phost = (struct hostent*)gethostbyname (ctask->host);
1840 ssl_ip = inet_ntoa(*((struct in_addr*)(phost->h_addr)));
1841 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1842 "SSL target server: %s\n", ssl_ip);
1843 sprintf (resolvename, "%s:%d:%s", ctask->leho, HTTPS_PORT, ssl_ip);
1844 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1845 "Curl resolve: %s\n", resolvename);
1846 ctask->resolver = curl_slist_append ( ctask->resolver, resolvename);
1847 curl_easy_setopt (ctask->curl, CURLOPT_RESOLVE, ctask->resolver);
1848 sprintf (curlurl, "https://%s:%d%s", ctask->leho, ctask->port, ctask->url);
1849 curl_easy_setopt (ctask->curl, CURLOPT_URL, curlurl);
1853 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1854 "gethostbyname failed for %s!\n", ctask->host);
1855 ctask->download_is_finished = GNUNET_YES;
1856 ctask->download_error = GNUNET_YES;
1861 if (CURLM_OK != (mret=curl_multi_add_handle (curl_multi, ctask->curl)))
1863 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1864 "%s failed at %s:%d: `%s'\n",
1865 "curl_multi_add_handle", __FILE__, __LINE__,
1866 curl_multi_strerror (mret));
1867 ctask->download_is_finished = GNUNET_YES;
1868 ctask->download_error = GNUNET_YES;
1871 GNUNET_CONTAINER_DLL_insert (ctasks_head, ctasks_tail, ctask);
1873 curl_download_prepare ();
1878 * Initialize download and trigger curl
1880 * @param cls the proxycurltask
1881 * @param auth_name the name of the authority (site of origin) of ctask->host
1885 process_get_authority (void *cls,
1886 const char* auth_name)
1888 struct ProxyCurlTask *ctask = cls;
1890 if (NULL == auth_name)
1892 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1893 "Get authority failed!\n");
1894 strcpy (ctask->authority, "");
1898 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1899 "Get authority yielded %s\n", auth_name);
1900 strcpy (ctask->authority, auth_name);
1903 GNUNET_GNS_lookup_zone (gns_handle,
1906 GNUNET_GNS_RECORD_LEHO,
1907 GNUNET_YES, //Only cached for performance
1909 &process_leho_lookup,
1914 mhd_log_callback (void* cls, const char* url)
1916 struct ProxyCurlTask *ctask;
1918 ctask = GNUNET_malloc (sizeof (struct ProxyCurlTask));
1919 strcpy (ctask->url, url);
1920 ctask->accepted = GNUNET_NO;
1925 * Main MHD callback for handling requests.
1928 * @param con MHD connection handle
1929 * @param url the url in the request
1930 * @param meth the HTTP method used ("GET", "PUT", etc.)
1931 * @param ver the HTTP version string (i.e. "HTTP/1.1")
1932 * @param upload_data the data being uploaded (excluding HEADERS,
1933 * for a POST that fits into memory and that is encoded
1934 * with a supported encoding, the POST data will NOT be
1935 * given in upload_data and is instead available as
1936 * part of MHD_get_connection_values; very large POST
1937 * data *will* be made available incrementally in
1939 * @param upload_data_size set initially to the size of the
1940 * upload_data provided; the method must update this
1941 * value to the number of bytes NOT processed;
1942 * @param con_cls pointer to location where we store the 'struct Request'
1943 * @return MHD_YES if the connection was handled successfully,
1944 * MHD_NO if the socket must be closed due to a serious
1945 * error while handling the request
1948 create_response (void *cls,
1949 struct MHD_Connection *con,
1953 const char *upload_data,
1954 size_t *upload_data_size,
1957 struct MhdHttpList* hd = cls;
1958 const char* page = "<html><head><title>gnoxy</title>"\
1959 "</head><body>cURL fail</body></html>";
1961 char curlurl[MAX_HTTP_URI_LENGTH]; // buffer overflow!
1965 struct ProxyCurlTask *ctask = *con_cls;
1966 struct ProxyUploadData *fin_post;
1967 struct curl_forms forms[5];
1968 struct ProxyUploadData *upload_data_iter;
1971 if ((0 != strcasecmp (meth, MHD_HTTP_METHOD_GET)) &&
1972 (0 != strcasecmp (meth, MHD_HTTP_METHOD_PUT)) &&
1973 (0 != strcasecmp (meth, MHD_HTTP_METHOD_POST)) &&
1974 (0 != strcasecmp (meth, MHD_HTTP_METHOD_HEAD)))
1976 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1977 "MHD: %s NOT IMPLEMENTED!\n", meth);
1982 if (GNUNET_NO == ctask->accepted)
1985 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1986 "Got %s request for %s\n", meth, url);
1988 ctask->curl = curl_easy_init();
1989 ctask->curl_running = GNUNET_NO;
1990 if (NULL == ctask->curl)
1992 ctask->response = MHD_create_response_from_buffer (strlen (page),
1994 MHD_RESPMEM_PERSISTENT);
1995 ret = MHD_queue_response (con,
1998 MHD_destroy_response (ctask->response);
1999 GNUNET_free (ctask);
2003 if (ctask->mhd->is_ssl)
2004 ctask->port = HTTPS_PORT;
2006 ctask->port = HTTP_PORT;
2008 MHD_get_connection_values (con,
2010 &con_val_iter, ctask);
2012 curl_easy_setopt (ctask->curl, CURLOPT_HEADERFUNCTION, &curl_check_hdr);
2013 curl_easy_setopt (ctask->curl, CURLOPT_HEADERDATA, ctask);
2014 curl_easy_setopt (ctask->curl, CURLOPT_WRITEFUNCTION, &curl_download_cb);
2015 curl_easy_setopt (ctask->curl, CURLOPT_WRITEDATA, ctask);
2016 curl_easy_setopt (ctask->curl, CURLOPT_FOLLOWLOCATION, 0);
2017 curl_easy_setopt (ctask->curl, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
2019 if (GNUNET_NO == ctask->mhd->is_ssl)
2021 sprintf (curlurl, "http://%s:%d%s", ctask->host, ctask->port, ctask->url);
2022 curl_easy_setopt (ctask->curl, CURLOPT_URL, curlurl);
2026 curl_easy_setopt (ctask->curl, CURLOPT_FAILONERROR, 1);
2027 curl_easy_setopt (ctask->curl, CURLOPT_CONNECTTIMEOUT, 600L);
2028 curl_easy_setopt (ctask->curl, CURLOPT_TIMEOUT, 600L);
2030 /* Add GNS header */
2031 ctask->headers = curl_slist_append (ctask->headers,
2033 ctask->accepted = GNUNET_YES;
2034 ctask->download_in_progress = GNUNET_YES;
2035 ctask->buf_status = BUF_WAIT_FOR_CURL;
2036 ctask->connection = con;
2037 ctask->curl_response_code = MHD_HTTP_OK;
2038 ctask->buffer_read_ptr = ctask->buffer;
2039 ctask->buffer_write_ptr = ctask->buffer;
2040 ctask->pp_task = GNUNET_SCHEDULER_NO_TASK;
2043 if (0 == strcasecmp (meth, MHD_HTTP_METHOD_PUT))
2045 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2046 "Setting up PUT\n");
2048 curl_easy_setopt (ctask->curl, CURLOPT_UPLOAD, 1);
2049 curl_easy_setopt (ctask->curl, CURLOPT_READDATA, ctask);
2050 curl_easy_setopt (ctask->curl, CURLOPT_READFUNCTION, &put_read_callback);
2051 ctask->headers = curl_slist_append (ctask->headers,
2052 "Transfer-Encoding: chunked");
2055 if (0 == strcasecmp (meth, MHD_HTTP_METHOD_POST))
2057 //FIXME handle multipart
2058 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2059 "Setting up POST processor\n");
2060 ctask->post_handler = MHD_create_post_processor (con,
2062 &con_post_data_iter,
2064 ctask->headers = curl_slist_append (ctask->headers,
2065 "Transfer-Encoding: chunked");
2069 if (0 == strcasecmp (meth, MHD_HTTP_METHOD_HEAD))
2071 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2072 "Setting NOBODY\n");
2073 curl_easy_setopt (ctask->curl, CURLOPT_NOBODY, 1);
2077 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2078 "MHD: Adding new curl task for %s\n", ctask->host);
2080 GNUNET_GNS_get_authority (gns_handle,
2082 &process_get_authority,
2084 ctask->ready_to_queue = GNUNET_NO;
2085 ctask->fin = GNUNET_NO;
2086 ctask->curl_running = GNUNET_YES;
2090 ctask = (struct ProxyCurlTask *) *con_cls;
2091 if (0 == strcasecmp (meth, MHD_HTTP_METHOD_POST))
2093 if (0 != *upload_data_size)
2096 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2097 "Invoking POST processor\n");
2098 MHD_post_process (ctask->post_handler,
2099 upload_data, *upload_data_size);
2100 *upload_data_size = 0;
2101 if ((GNUNET_NO == ctask->is_httppost) &&
2102 (GNUNET_NO == ctask->curl_running))
2104 curl_easy_setopt (ctask->curl, CURLOPT_POST, 1);
2105 curl_easy_setopt (ctask->curl, CURLOPT_READFUNCTION,
2106 &post_read_callback);
2107 curl_easy_setopt (ctask->curl, CURLOPT_READDATA, ctask);
2109 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2110 "MHD: Adding new curl task for %s\n", ctask->host);
2112 GNUNET_GNS_get_authority (gns_handle,
2114 &process_get_authority,
2116 ctask->ready_to_queue = GNUNET_NO;
2117 ctask->fin = GNUNET_NO;
2118 ctask->curl_running = GNUNET_YES;
2122 else if (GNUNET_NO == ctask->post_done)
2124 if (GNUNET_YES == ctask->is_httppost)
2126 for (upload_data_iter = ctask->upload_data_head;
2127 NULL != upload_data_iter;
2128 upload_data_iter = upload_data_iter->next)
2131 if (NULL != upload_data_iter->filename)
2133 forms[i].option = CURLFORM_FILENAME;
2134 forms[i].value = upload_data_iter->filename;
2135 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2136 "Adding filename %s\n",
2140 if (NULL != upload_data_iter->content_type)
2142 forms[i].option = CURLFORM_CONTENTTYPE;
2143 forms[i].value = upload_data_iter->content_type;
2144 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2145 "Adding content type %s\n",
2149 forms[i].option = CURLFORM_PTRCONTENTS;
2150 forms[i].value = upload_data_iter->value;
2151 forms[i+1].option = CURLFORM_END;
2153 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2154 "Adding formdata for %s (len=%lld)\n",
2155 upload_data_iter->key,
2156 upload_data_iter->total_bytes);
2158 curl_formadd(&ctask->httppost, &ctask->httppost_last,
2159 CURLFORM_COPYNAME, upload_data_iter->key,
2160 CURLFORM_CONTENTSLENGTH, upload_data_iter->total_bytes,
2161 CURLFORM_ARRAY, forms,
2164 curl_easy_setopt (ctask->curl, CURLOPT_HTTPPOST,
2167 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2168 "MHD: Adding new curl task for %s\n", ctask->host);
2170 GNUNET_GNS_get_authority (gns_handle,
2172 &process_get_authority,
2174 ctask->ready_to_queue = GNUNET_YES;
2175 ctask->fin = GNUNET_NO;
2176 ctask->curl_running = GNUNET_YES;
2177 ctask->post_done = GNUNET_YES;
2181 fin_post = GNUNET_malloc (sizeof (struct ProxyUploadData));
2182 GNUNET_CONTAINER_DLL_insert_tail (ctask->upload_data_head,
2183 ctask->upload_data_tail,
2185 ctask->post_done = GNUNET_YES;
2190 if (GNUNET_YES != ctask->ready_to_queue)
2191 return MHD_YES; /* wait longer */
2193 if (GNUNET_YES == ctask->fin)
2196 ctask->fin = GNUNET_YES;
2197 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2198 "MHD: Queueing response for %s\n", ctask->url);
2199 ret = MHD_queue_response (con, ctask->curl_response_code, ctask->response);
2200 run_mhd_now (ctask->mhd);
2211 struct MhdHttpList *hd;
2213 for (hd=mhd_httpd_head; NULL != hd; hd = hd->next)
2221 * @param hd the daemon to run
2224 run_httpd (struct MhdHttpList *hd)
2229 struct GNUNET_NETWORK_FDSet *wrs;
2230 struct GNUNET_NETWORK_FDSet *wws;
2231 struct GNUNET_NETWORK_FDSet *wes;
2234 unsigned MHD_LONG_LONG timeout;
2235 struct GNUNET_TIME_Relative tv;
2240 wrs = GNUNET_NETWORK_fdset_create ();
2241 wes = GNUNET_NETWORK_fdset_create ();
2242 wws = GNUNET_NETWORK_fdset_create ();
2244 GNUNET_assert (MHD_YES == MHD_get_fdset (hd->daemon, &rs, &ws, &es, &max));
2247 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2248 "MHD fds: max=%d\n", max);
2250 haveto = MHD_get_timeout (hd->daemon, &timeout);
2252 if (MHD_YES == haveto)
2253 tv.rel_value = (uint64_t) timeout;
2255 tv = GNUNET_TIME_UNIT_FOREVER_REL;
2256 GNUNET_NETWORK_fdset_copy_native (wrs, &rs, max + 1);
2257 GNUNET_NETWORK_fdset_copy_native (wws, &ws, max + 1);
2258 GNUNET_NETWORK_fdset_copy_native (wes, &es, max + 1);
2260 if (GNUNET_SCHEDULER_NO_TASK != hd->httpd_task)
2261 GNUNET_SCHEDULER_cancel (hd->httpd_task);
2263 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_HIGH,
2266 GNUNET_NETWORK_fdset_destroy (wrs);
2267 GNUNET_NETWORK_fdset_destroy (wws);
2268 GNUNET_NETWORK_fdset_destroy (wes);
2273 * Task run whenever HTTP server operations are pending.
2276 * @param tc sched context
2279 do_httpd (void *cls,
2280 const struct GNUNET_SCHEDULER_TaskContext *tc)
2282 struct MhdHttpList *hd = cls;
2284 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2285 "MHD: Main loop\n");
2286 hd->httpd_task = GNUNET_SCHEDULER_NO_TASK;
2287 MHD_run (hd->daemon);
2294 * Read data from socket
2296 * @param cls the closure
2297 * @param tc scheduler context
2300 do_read (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
2303 * Read from remote end
2305 * @param cls closure
2306 * @param tc scheduler context
2309 do_read_remote (void* cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
2312 * Write data to remote socket
2314 * @param cls the closure
2315 * @param tc scheduler context
2318 do_write_remote (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2320 struct Socks5Request *s5r = cls;
2323 s5r->fwdwtask = GNUNET_SCHEDULER_NO_TASK;
2325 if ((NULL != tc->read_ready) &&
2326 (GNUNET_NETWORK_fdset_isset (tc->write_ready, s5r->remote_sock)) &&
2327 ((len = GNUNET_NETWORK_socket_send (s5r->remote_sock, s5r->rbuf,
2330 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2331 "Successfully sent %d bytes to remote socket\n",
2336 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "write remote");
2337 if (GNUNET_SCHEDULER_NO_TASK != s5r->rtask)
2338 GNUNET_SCHEDULER_cancel (s5r->rtask);
2339 if (GNUNET_SCHEDULER_NO_TASK != s5r->wtask)
2340 GNUNET_SCHEDULER_cancel (s5r->wtask);
2341 if (GNUNET_SCHEDULER_NO_TASK != s5r->fwdrtask)
2342 GNUNET_SCHEDULER_cancel (s5r->fwdrtask);
2343 GNUNET_NETWORK_socket_close (s5r->remote_sock);
2344 GNUNET_NETWORK_socket_close (s5r->sock);
2350 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
2357 * Clean up s5r handles
2359 * @param s5r the handle to destroy
2362 cleanup_s5r (struct Socks5Request *s5r)
2364 if (GNUNET_SCHEDULER_NO_TASK != s5r->rtask)
2365 GNUNET_SCHEDULER_cancel (s5r->rtask);
2366 if (GNUNET_SCHEDULER_NO_TASK != s5r->fwdwtask)
2367 GNUNET_SCHEDULER_cancel (s5r->fwdwtask);
2368 if (GNUNET_SCHEDULER_NO_TASK != s5r->fwdrtask)
2369 GNUNET_SCHEDULER_cancel (s5r->fwdrtask);
2371 if (NULL != s5r->remote_sock)
2372 GNUNET_NETWORK_socket_close (s5r->remote_sock);
2373 if ((NULL != s5r->sock) && (s5r->cleanup_sock == GNUNET_YES))
2374 GNUNET_NETWORK_socket_close (s5r->sock);
2380 * Write data to socket
2382 * @param cls the closure
2383 * @param tc scheduler context
2386 do_write (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2388 struct Socks5Request *s5r = cls;
2391 s5r->wtask = GNUNET_SCHEDULER_NO_TASK;
2393 if ((NULL != tc->read_ready) &&
2394 (GNUNET_NETWORK_fdset_isset (tc->write_ready, s5r->sock)) &&
2395 ((len = GNUNET_NETWORK_socket_send (s5r->sock, s5r->wbuf,
2398 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2399 "Successfully sent %d bytes to socket\n",
2405 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "write");
2406 s5r->cleanup = GNUNET_YES;
2407 s5r->cleanup_sock = GNUNET_YES;
2413 if (GNUNET_YES == s5r->cleanup)
2419 if ((s5r->state == SOCKS5_DATA_TRANSFER) &&
2420 (s5r->fwdrtask == GNUNET_SCHEDULER_NO_TASK))
2422 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
2424 &do_read_remote, s5r);
2428 * Read from remote end
2430 * @param cls closure
2431 * @param tc scheduler context
2434 do_read_remote (void* cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2436 struct Socks5Request *s5r = cls;
2438 s5r->fwdrtask = GNUNET_SCHEDULER_NO_TASK;
2441 if ((NULL != tc->write_ready) &&
2442 (GNUNET_NETWORK_fdset_isset (tc->read_ready, s5r->remote_sock)) &&
2443 (s5r->wbuf_len = GNUNET_NETWORK_socket_recv (s5r->remote_sock, s5r->wbuf,
2444 sizeof (s5r->wbuf))))
2446 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2447 "Successfully read %d bytes from remote socket\n",
2452 if (s5r->wbuf_len == 0)
2453 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2454 "0 bytes received from remote... graceful shutdown!\n");
2455 if (s5r->fwdwtask != GNUNET_SCHEDULER_NO_TASK)
2456 GNUNET_SCHEDULER_cancel (s5r->fwdwtask);
2457 if (s5r->rtask != GNUNET_SCHEDULER_NO_TASK)
2458 GNUNET_SCHEDULER_cancel (s5r->rtask);
2460 GNUNET_NETWORK_socket_close (s5r->remote_sock);
2461 s5r->remote_sock = NULL;
2462 GNUNET_NETWORK_socket_close (s5r->sock);
2468 s5r->wtask = GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
2476 * Adds a socket to MHD
2478 * @param h the handle to the socket to add
2479 * @param daemon the daemon to add the fd to
2480 * @return whatever MHD_add_connection returns
2483 add_handle_to_mhd (struct GNUNET_NETWORK_Handle *h, struct MHD_Daemon *daemon)
2486 struct sockaddr *addr;
2489 fd = dup (GNUNET_NETWORK_get_fd (h));
2490 addr = GNUNET_NETWORK_get_addr (h);
2491 len = GNUNET_NETWORK_get_addrlen (h);
2493 return MHD_add_connection (daemon, fd, addr, len);
2497 * Read file in filename
2499 * @param filename file to read
2500 * @param size pointer where filesize is stored
2504 load_file (const char* filename,
2511 GNUNET_DISK_file_size (filename, &fsize,
2512 GNUNET_YES, GNUNET_YES))
2514 if (fsize > MAX_PEM_SIZE)
2516 *size = (unsigned int) fsize;
2517 buffer = GNUNET_malloc (*size);
2518 if (fsize != GNUNET_DISK_fn_read (filename, buffer, (size_t) fsize))
2520 GNUNET_free (buffer);
2528 * Load PEM key from file
2530 * @param key where to store the data
2531 * @param keyfile path to the PEM file
2532 * @return GNUNET_OK on success
2535 load_key_from_file (gnutls_x509_privkey_t key, const char* keyfile)
2537 gnutls_datum_t key_data;
2540 key_data.data = (unsigned char*) load_file (keyfile, &key_data.size);
2541 ret = gnutls_x509_privkey_import (key, &key_data,
2542 GNUTLS_X509_FMT_PEM);
2543 if (GNUTLS_E_SUCCESS != ret)
2545 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2546 _("Unable to import private key from file `%s'\n"),
2550 GNUNET_free (key_data.data);
2551 return (GNUTLS_E_SUCCESS != ret) ? GNUNET_SYSERR : GNUNET_OK;
2556 * Load cert from file
2558 * @param crt struct to store data in
2559 * @param certfile path to pem file
2560 * @return GNUNET_OK on success
2563 load_cert_from_file (gnutls_x509_crt_t crt, char* certfile)
2565 gnutls_datum_t cert_data;
2566 cert_data.data = NULL;
2569 cert_data.data = (unsigned char*) load_file (certfile, &cert_data.size);
2570 ret = gnutls_x509_crt_import (crt, &cert_data,
2571 GNUTLS_X509_FMT_PEM);
2572 if (GNUTLS_E_SUCCESS != ret)
2574 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2575 _("Unable to import certificate %s\n"), certfile);
2578 GNUNET_free (cert_data.data);
2579 return (GNUTLS_E_SUCCESS != ret) ? GNUNET_SYSERR : GNUNET_OK;
2584 * Generate new certificate for specific name
2586 * @param name the subject name to generate a cert for
2587 * @return a struct holding the PEM data
2589 static struct ProxyGNSCertificate *
2590 generate_gns_certificate (const char *name)
2594 unsigned int serial;
2595 size_t key_buf_size;
2596 size_t cert_buf_size;
2597 gnutls_x509_crt_t request;
2601 ret = gnutls_x509_crt_init (&request);
2603 if (GNUTLS_E_SUCCESS != ret)
2608 ret = gnutls_x509_crt_set_key (request, proxy_ca.key);
2610 if (GNUTLS_E_SUCCESS != ret)
2615 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Generating cert\n");
2617 struct ProxyGNSCertificate *pgc =
2618 GNUNET_malloc (sizeof (struct ProxyGNSCertificate));
2620 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Adding DNs\n");
2622 gnutls_x509_crt_set_dn_by_oid (request, GNUTLS_OID_X520_COUNTRY_NAME,
2625 gnutls_x509_crt_set_dn_by_oid (request, GNUTLS_OID_X520_ORGANIZATION_NAME,
2628 gnutls_x509_crt_set_dn_by_oid (request, GNUTLS_OID_X520_COMMON_NAME,
2629 0, name, strlen (name));
2631 ret = gnutls_x509_crt_set_version (request, 3);
2633 ret = gnutls_rnd (GNUTLS_RND_NONCE, &serial, sizeof (serial));
2635 etime = time (NULL);
2636 tm_data = localtime (&etime);
2639 ret = gnutls_x509_crt_set_serial (request,
2643 ret = gnutls_x509_crt_set_activation_time (request,
2646 etime = mktime (tm_data);
2653 ret = gnutls_x509_crt_set_expiration_time (request,
2655 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Signing...\n");
2657 ret = gnutls_x509_crt_sign (request, proxy_ca.cert, proxy_ca.key);
2659 key_buf_size = sizeof (pgc->key);
2660 cert_buf_size = sizeof (pgc->cert);
2662 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Exporting certificate...\n");
2664 gnutls_x509_crt_export (request, GNUTLS_X509_FMT_PEM,
2665 pgc->cert, &cert_buf_size);
2667 gnutls_x509_privkey_export (proxy_ca.key, GNUTLS_X509_FMT_PEM,
2668 pgc->key, &key_buf_size);
2671 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Cleaning up\n");
2672 gnutls_x509_crt_deinit (request);
2680 * Accept policy for mhdaemons
2683 * @param addr the sockaddr
2684 * @param addrlen the sockaddr length
2685 * @return MHD_NO if sockaddr is wrong or #conns too high
2688 accept_cb (void* cls, const struct sockaddr *addr, socklen_t addrlen)
2690 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2691 "In MHD accept policy cb\n");
2695 if (addr->sa_family == AF_UNIX)
2699 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2700 "Connection accepted\n");
2707 * Adds a socket to an SSL MHD instance
2708 * It is important the the domain name is
2709 * correct. In most cases we need to start a new daemon
2711 * @param h the handle to add to a daemon
2712 * @param domain the domain the ssl daemon has to serve
2713 * @return MHD_YES on success
2716 add_handle_to_ssl_mhd (struct GNUNET_NETWORK_Handle *h, const char* domain)
2718 struct MhdHttpList *hd = NULL;
2719 struct ProxyGNSCertificate *pgc;
2720 struct NetworkHandleList *nh;
2722 for (hd = mhd_httpd_head; NULL != hd; hd = hd->next)
2723 if (0 == strcmp (hd->domain, domain))
2728 pgc = generate_gns_certificate (domain);
2730 hd = GNUNET_malloc (sizeof (struct MhdHttpList));
2731 hd->is_ssl = GNUNET_YES;
2732 strcpy (hd->domain, domain);
2733 hd->proxy_cert = pgc;
2736 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2737 "No previous SSL instance found... starting new one for %s\n",
2739 hd->daemon = MHD_start_daemon (MHD_USE_DEBUG
2741 #if HAVE_MHD_NO_LISTEN_SOCKET
2742 | MHD_USE_NO_LISTEN_SOCKET,
2748 &create_response, hd,
2749 #if !HAVE_MHD_NO_LISTEN_SOCKET
2750 MHD_OPTION_LISTEN_SOCKET, GNUNET_NETWORK_get_fd (mhd_unix_socket),
2752 MHD_OPTION_CONNECTION_LIMIT,
2753 MHD_MAX_CONNECTIONS,
2754 MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) 16,
2755 MHD_OPTION_NOTIFY_COMPLETED, NULL, NULL,
2756 MHD_OPTION_HTTPS_MEM_KEY, pgc->key,
2757 MHD_OPTION_HTTPS_MEM_CERT, pgc->cert,
2758 MHD_OPTION_URI_LOG_CALLBACK, &mhd_log_callback,
2762 GNUNET_assert (hd->daemon != NULL);
2763 hd->httpd_task = GNUNET_SCHEDULER_NO_TASK;
2765 GNUNET_CONTAINER_DLL_insert (mhd_httpd_head, mhd_httpd_tail, hd);
2768 nh = GNUNET_malloc (sizeof (struct NetworkHandleList));
2771 GNUNET_CONTAINER_DLL_insert (hd->socket_handles_head,
2772 hd->socket_handles_tail,
2775 return add_handle_to_mhd (h, hd->daemon);
2781 * Read data from incoming connection
2783 * @param cls the closure
2784 * @param tc the scheduler context
2787 do_read (void* cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2789 struct Socks5Request *s5r = cls;
2790 struct socks5_client_hello *c_hello;
2791 struct socks5_server_hello *s_hello;
2792 struct socks5_client_request *c_req;
2793 struct socks5_server_response *s_resp;
2799 struct hostent *phost;
2801 struct sockaddr_in remote_addr;
2802 struct in_addr *r_sin_addr;
2804 struct NetworkHandleList *nh;
2806 s5r->rtask = GNUNET_SCHEDULER_NO_TASK;
2808 if ((NULL != tc->write_ready) &&
2809 (GNUNET_NETWORK_fdset_isset (tc->read_ready, s5r->sock)) &&
2810 (s5r->rbuf_len = GNUNET_NETWORK_socket_recv (s5r->sock, s5r->rbuf,
2811 sizeof (s5r->rbuf))))
2813 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2814 "Successfully read %d bytes from socket\n",
2819 if (s5r->rbuf_len != 0)
2820 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "read");
2822 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client disco!\n");
2824 if (s5r->fwdrtask != GNUNET_SCHEDULER_NO_TASK)
2825 GNUNET_SCHEDULER_cancel (s5r->fwdrtask);
2826 if (s5r->wtask != GNUNET_SCHEDULER_NO_TASK)
2827 GNUNET_SCHEDULER_cancel (s5r->wtask);
2828 if (s5r->fwdwtask != GNUNET_SCHEDULER_NO_TASK)
2829 GNUNET_SCHEDULER_cancel (s5r->fwdwtask);
2830 GNUNET_NETWORK_socket_close (s5r->remote_sock);
2831 GNUNET_NETWORK_socket_close (s5r->sock);
2836 if (s5r->state == SOCKS5_INIT)
2838 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2840 c_hello = (struct socks5_client_hello*)&s5r->rbuf;
2842 GNUNET_assert (c_hello->version == SOCKS_VERSION_5);
2844 s_hello = (struct socks5_server_hello*)&s5r->wbuf;
2845 s5r->wbuf_len = sizeof( struct socks5_server_hello );
2847 s_hello->version = c_hello->version;
2848 s_hello->auth_method = SOCKS_AUTH_NONE;
2850 /* Write response to client */
2851 s5r->wtask = GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
2855 s5r->rtask = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
2859 s5r->state = SOCKS5_REQUEST;
2863 if (s5r->state == SOCKS5_REQUEST)
2865 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2866 "Processing SOCKS5 request\n");
2867 c_req = (struct socks5_client_request*)&s5r->rbuf;
2868 s_resp = (struct socks5_server_response*)&s5r->wbuf;
2869 //Only 10byte for ipv4 response!
2870 s5r->wbuf_len = 10;//sizeof (struct socks5_server_response);
2872 GNUNET_assert (c_req->addr_type == 3);
2874 dom_len = *((uint8_t*)(&(c_req->addr_type) + 1));
2875 memset(domain, 0, sizeof(domain));
2876 strncpy(domain, (char*)(&(c_req->addr_type) + 2), dom_len);
2877 req_port = *((uint16_t*)(&(c_req->addr_type) + 2 + dom_len));
2879 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2880 "Requested connection is %s:%d\n",
2884 if (is_tld (domain, GNUNET_GNS_TLD) ||
2885 is_tld (domain, GNUNET_GNS_TLD_ZKEY))
2887 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2888 "Requested connection is gnunet tld\n",
2892 if (ntohs(req_port) == HTTPS_PORT)
2894 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2895 "Requested connection is HTTPS\n");
2896 ret = add_handle_to_ssl_mhd ( s5r->sock, domain );
2898 else if (NULL != httpd)
2900 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2901 "Requested connection is HTTP\n");
2902 nh = GNUNET_malloc (sizeof (struct NetworkHandleList));
2905 GNUNET_CONTAINER_DLL_insert (mhd_httpd_head->socket_handles_head,
2906 mhd_httpd_head->socket_handles_tail,
2909 ret = add_handle_to_mhd ( s5r->sock, httpd );
2914 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2915 _("Failed to start HTTP server\n"));
2916 s_resp->version = 0x05;
2917 s_resp->reply = 0x01;
2918 s5r->cleanup = GNUNET_YES;
2919 s5r->cleanup_sock = GNUNET_YES;
2921 GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
2927 /* Signal success */
2928 s_resp->version = 0x05;
2929 s_resp->reply = 0x00;
2930 s_resp->reserved = 0x00;
2931 s_resp->addr_type = 0x01;
2933 s5r->cleanup = GNUNET_YES;
2934 s5r->cleanup_sock = GNUNET_NO;
2936 GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
2944 phost = (struct hostent*)gethostbyname (domain);
2947 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2948 "Resolve %s error!\n", domain );
2949 s_resp->version = 0x05;
2950 s_resp->reply = 0x01;
2951 s5r->cleanup = GNUNET_YES;
2952 s5r->cleanup_sock = GNUNET_YES;
2954 GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
2960 s5r->remote_sock = GNUNET_NETWORK_socket_create (AF_INET,
2963 r_sin_addr = (struct in_addr*)(phost->h_addr);
2964 remote_ip = r_sin_addr->s_addr;
2965 memset(&remote_addr, 0, sizeof(remote_addr));
2966 remote_addr.sin_family = AF_INET;
2967 #if HAVE_SOCKADDR_IN_SIN_LEN
2968 remote_addr.sin_len = sizeof (remote_addr);
2970 remote_addr.sin_addr.s_addr = remote_ip;
2971 remote_addr.sin_port = req_port;
2973 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2974 "target server: %s:%u\n", inet_ntoa(remote_addr.sin_addr),
2978 GNUNET_NETWORK_socket_connect ( s5r->remote_sock,
2979 (const struct sockaddr*)&remote_addr,
2980 sizeof (remote_addr)))
2981 && (errno != EINPROGRESS))
2983 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "connect");
2984 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2985 "socket request error...\n");
2986 s_resp->version = 0x05;
2987 s_resp->reply = 0x01;
2989 GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
2996 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2997 "new remote connection\n");
2999 s_resp->version = 0x05;
3000 s_resp->reply = 0x00;
3001 s_resp->reserved = 0x00;
3002 s_resp->addr_type = 0x01;
3004 s5r->state = SOCKS5_DATA_TRANSFER;
3007 GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
3011 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
3019 if (s5r->state == SOCKS5_DATA_TRANSFER)
3021 if ((s5r->remote_sock == NULL) || (s5r->rbuf_len == 0))
3023 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3024 "Closing connection to client\n");
3025 if (s5r->rtask != GNUNET_SCHEDULER_NO_TASK)
3026 GNUNET_SCHEDULER_cancel (s5r->rtask);
3027 if (s5r->fwdwtask != GNUNET_SCHEDULER_NO_TASK)
3028 GNUNET_SCHEDULER_cancel (s5r->fwdwtask);
3029 if (s5r->fwdrtask != GNUNET_SCHEDULER_NO_TASK)
3030 GNUNET_SCHEDULER_cancel (s5r->fwdrtask);
3031 if (s5r->fwdrtask != GNUNET_SCHEDULER_NO_TASK)
3032 GNUNET_SCHEDULER_cancel (s5r->fwdrtask);
3034 if (s5r->remote_sock != NULL)
3035 GNUNET_NETWORK_socket_close (s5r->remote_sock);
3036 GNUNET_NETWORK_socket_close (s5r->sock);
3041 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3042 "forwarding %d bytes from client\n", s5r->rbuf_len);
3045 GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
3047 &do_write_remote, s5r);
3049 if (s5r->fwdrtask == GNUNET_SCHEDULER_NO_TASK)
3052 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
3054 &do_read_remote, s5r);
3064 * Accept new incoming connections
3066 * @param cls the closure
3067 * @param tc the scheduler context
3070 do_accept (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3072 struct GNUNET_NETWORK_Handle *s;
3073 struct Socks5Request *s5r;
3075 ltask = GNUNET_SCHEDULER_NO_TASK;
3076 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
3079 ltask = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
3083 s = GNUNET_NETWORK_socket_accept (lsock, NULL, NULL);
3087 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "accept");
3091 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3092 "Got an inbound connection, waiting for data\n");
3094 s5r = GNUNET_malloc (sizeof (struct Socks5Request));
3096 s5r->state = SOCKS5_INIT;
3097 s5r->wtask = GNUNET_SCHEDULER_NO_TASK;
3098 s5r->fwdwtask = GNUNET_SCHEDULER_NO_TASK;
3099 s5r->fwdrtask = GNUNET_SCHEDULER_NO_TASK;
3100 s5r->rtask = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
3107 * Task run on shutdown
3109 * @param cls closure
3110 * @param tc task context
3113 do_shutdown (void *cls,
3114 const struct GNUNET_SCHEDULER_TaskContext *tc)
3117 struct MhdHttpList *hd;
3118 struct MhdHttpList *tmp_hd;
3119 struct NetworkHandleList *nh;
3120 struct NetworkHandleList *tmp_nh;
3121 struct ProxyCurlTask *ctask;
3122 struct ProxyCurlTask *ctask_tmp;
3123 struct ProxyUploadData *pdata;
3125 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3126 "Shutting down...\n");
3128 gnutls_global_deinit ();
3130 if (NULL != local_gns_zone)
3131 GNUNET_free (local_gns_zone);
3132 if (NULL != local_private_zone)
3133 GNUNET_free (local_private_zone);
3134 if (NULL != local_shorten_zone)
3135 GNUNET_free (local_shorten_zone);
3137 if (GNUNET_SCHEDULER_NO_TASK != curl_download_task)
3139 GNUNET_SCHEDULER_cancel (curl_download_task);
3140 curl_download_task = GNUNET_SCHEDULER_NO_TASK;
3143 for (hd = mhd_httpd_head; hd != NULL; hd = tmp_hd)
3147 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3148 "Stopping daemon\n");
3150 if (GNUNET_SCHEDULER_NO_TASK != hd->httpd_task)
3152 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3153 "Stopping select task %d\n",
3155 GNUNET_SCHEDULER_cancel (hd->httpd_task);
3156 hd->httpd_task = GNUNET_SCHEDULER_NO_TASK;
3159 if (NULL != hd->daemon)
3161 MHD_stop_daemon (hd->daemon);
3165 for (nh = hd->socket_handles_head; nh != NULL; nh = tmp_nh)
3169 GNUNET_NETWORK_socket_close (nh->h);
3174 if (NULL != hd->proxy_cert)
3176 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3177 "Free certificate\n");
3178 GNUNET_free (hd->proxy_cert);
3184 for (ctask=ctasks_head; ctask != NULL; ctask=ctask_tmp)
3186 ctask_tmp = ctask->next;
3188 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3189 "Cleaning up cURL task\n");
3191 if (ctask->curl != NULL)
3192 curl_easy_cleanup (ctask->curl);
3194 if (NULL != ctask->headers)
3195 curl_slist_free_all (ctask->headers);
3196 if (NULL != ctask->resolver)
3197 curl_slist_free_all (ctask->resolver);
3199 if (NULL != ctask->response)
3200 MHD_destroy_response (ctask->response);
3202 pdata = ctask->upload_data_head;
3204 //FIXME free pdata here
3205 for (; pdata != NULL; pdata = ctask->upload_data_head)
3207 GNUNET_CONTAINER_DLL_remove (ctask->upload_data_head,
3208 ctask->upload_data_tail,
3210 GNUNET_free_non_null (pdata->filename);
3211 GNUNET_free_non_null (pdata->content_type);
3212 GNUNET_free_non_null (pdata->key);
3213 GNUNET_free_non_null (pdata->value);
3214 GNUNET_free (pdata);
3217 GNUNET_free (ctask);
3219 curl_multi_cleanup (curl_multi);
3221 GNUNET_GNS_disconnect (gns_handle);
3226 * Compiles a regex for us
3228 * @param re ptr to re struct
3229 * @param rt the expression to compile
3230 * @return 0 on success
3233 compile_regex (regex_t *re, const char* rt)
3238 status = regcomp (re, rt, REG_EXTENDED|REG_NEWLINE);
3241 regerror (status, re, err, 1024);
3242 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3243 "Regex error compiling '%s': %s\n", rt, err);
3251 * Loads the users local zone key
3253 * @return GNUNET_YES on success
3256 load_local_zone_key (const struct GNUNET_CONFIGURATION_Handle *cfg)
3259 struct GNUNET_CRYPTO_RsaPrivateKey *key = NULL;
3260 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pkey;
3261 struct GNUNET_CRYPTO_ShortHashCode *zone = NULL;
3262 struct GNUNET_CRYPTO_ShortHashAsciiEncoded zonename;
3264 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "gns",
3265 "ZONEKEY", &keyfile))
3267 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3268 "Unable to load zone key config value!\n");
3272 if (GNUNET_NO == GNUNET_DISK_file_test (keyfile))
3274 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3275 "Unable to load zone key %s!\n", keyfile);
3276 GNUNET_free(keyfile);
3280 key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
3281 GNUNET_CRYPTO_rsa_key_get_public (key, &pkey);
3282 local_gns_zone = GNUNET_malloc (sizeof (struct GNUNET_CRYPTO_ShortHashCode));
3283 GNUNET_CRYPTO_short_hash(&pkey,
3284 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
3286 zone = local_gns_zone;
3287 GNUNET_CRYPTO_short_hash_to_enc (zone, &zonename);
3288 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3289 "Using zone: %s!\n", &zonename);
3290 GNUNET_CRYPTO_rsa_key_free(key);
3291 GNUNET_free(keyfile);
3294 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "gns",
3295 "PRIVATE_ZONEKEY", &keyfile))
3297 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3298 "Unable to load private zone key config value!\n");
3301 if ((NULL != keyfile) && (GNUNET_NO == GNUNET_DISK_file_test (keyfile)))
3303 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3304 "Unable to load private zone key %s!\n", keyfile);
3305 GNUNET_free(keyfile);
3309 key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
3310 GNUNET_CRYPTO_rsa_key_get_public (key, &pkey);
3311 local_private_zone = GNUNET_malloc (sizeof (struct GNUNET_CRYPTO_ShortHashCode));
3312 GNUNET_CRYPTO_short_hash(&pkey,
3313 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
3314 local_private_zone);
3315 GNUNET_CRYPTO_short_hash_to_enc (zone, &zonename);
3316 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3317 "Using private zone: %s!\n", &zonename);
3318 GNUNET_CRYPTO_rsa_key_free(key);
3319 GNUNET_free(keyfile);
3323 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "gns",
3324 "SHORTEN_ZONEKEY", &keyfile))
3326 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3327 "Unable to load shorten zone key config value!\n");
3330 if ((NULL != keyfile) && (GNUNET_NO == GNUNET_DISK_file_test (keyfile)))
3332 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3333 "Unable to load shorten zone key %s!\n", keyfile);
3334 GNUNET_free(keyfile);
3338 key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
3339 GNUNET_CRYPTO_rsa_key_get_public (key, &pkey);
3340 local_shorten_zone = GNUNET_malloc (sizeof (struct GNUNET_CRYPTO_ShortHashCode));
3341 GNUNET_CRYPTO_short_hash(&pkey,
3342 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
3343 local_shorten_zone);
3344 GNUNET_CRYPTO_short_hash_to_enc (zone, &zonename);
3345 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3346 "Using shorten zone: %s!\n", &zonename);
3347 GNUNET_CRYPTO_rsa_key_free(key);
3348 GNUNET_free(keyfile);
3355 * Main function that will be run
3357 * @param cls closure
3358 * @param args remaining command-line arguments
3359 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
3360 * @param cfg configuration
3363 run (void *cls, char *const *args, const char *cfgfile,
3364 const struct GNUNET_CONFIGURATION_Handle *cfg)
3366 struct sockaddr_in sa;
3367 struct MhdHttpList *hd;
3368 char* cafile_cfg = NULL;
3370 #if !HAVE_MHD_NO_LISTEN_SOCKET
3372 char* proxy_sockfile;
3373 struct sockaddr_un mhd_unix_sock_addr;
3376 curl_multi = curl_multi_init ();
3378 if (NULL == curl_multi)
3380 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3381 "Failed to create cURL multo handle!\n");
3385 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3387 cafile = cafile_opt;
3390 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "gns-proxy",
3394 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3395 "Unable to load proxy CA config value!\n");
3396 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3397 "No proxy CA provided!\n");
3400 cafile = cafile_cfg;
3402 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3403 "Using %s as CA\n", cafile);
3405 gnutls_global_init ();
3406 gnutls_x509_crt_init (&proxy_ca.cert);
3407 gnutls_x509_privkey_init (&proxy_ca.key);
3409 if ( (GNUNET_OK != load_cert_from_file (proxy_ca.cert, cafile)) ||
3410 (GNUNET_OK != load_key_from_file (proxy_ca.key, cafile)) )
3412 // FIXME: release resources...
3416 GNUNET_free_non_null (cafile_cfg);
3418 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3419 "Loading Template\n");
3421 compile_regex (&re_dotplus, (char*) RE_A_HREF);
3423 gns_handle = GNUNET_GNS_connect (cfg);
3425 if (GNUNET_NO == load_local_zone_key (cfg))
3427 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3428 "Unable to load zone!\n");
3432 if (NULL == gns_handle)
3434 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3435 "Unable to connect to GNS!\n");
3439 memset (&sa, 0, sizeof (sa));
3440 sa.sin_family = AF_INET;
3441 sa.sin_port = htons (port);
3442 #if HAVE_SOCKADDR_IN_SIN_LEN
3443 sa.sin_len = sizeof (sa);
3446 lsock = GNUNET_NETWORK_socket_create (AF_INET,
3450 if ((NULL == lsock) ||
3452 GNUNET_NETWORK_socket_bind (lsock, (const struct sockaddr *) &sa,
3455 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3456 "Failed to create listen socket bound to `%s'",
3457 GNUNET_a2s ((const struct sockaddr *) &sa, sizeof (sa)));
3459 GNUNET_NETWORK_socket_close (lsock);
3463 if (GNUNET_OK != GNUNET_NETWORK_socket_listen (lsock, 5))
3465 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3466 "Failed to listen on socket bound to `%s'",
3467 GNUNET_a2s ((const struct sockaddr *) &sa, sizeof (sa)));
3471 ltask = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
3472 lsock, &do_accept, NULL);
3477 if (0 != curl_global_init (CURL_GLOBAL_WIN32))
3479 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3480 "cURL global init failed!\n");
3484 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3485 "Proxy listens on port %u\n",
3488 mhd_httpd_head = NULL;
3489 mhd_httpd_tail = NULL;
3490 total_mhd_connections = 0;
3491 #ifndef HAVE_MHD_NO_LISTEN_SOCKET
3492 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "gns-proxy",
3496 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3497 "Specify PROXY_UNIXPATH in gns-proxy config section!\n");
3501 mhd_unix_socket = GNUNET_NETWORK_socket_create (AF_UNIX,
3505 if (NULL == mhd_unix_socket)
3507 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3508 "Unable to create unix domain socket!\n");
3512 mhd_unix_sock_addr.sun_family = AF_UNIX;
3513 strcpy (mhd_unix_sock_addr.sun_path, proxy_sockfile);
3516 mhd_unix_sock_addr.sun_path[0] = '\0';
3518 #if HAVE_SOCKADDR_IN_SIN_LEN
3519 mhd_unix_sock_addr.sun_len = (u_char) sizeof (struct sockaddr_un);
3522 len = strlen (proxy_sockfile) + sizeof(AF_UNIX);
3524 GNUNET_free (proxy_sockfile);
3526 if (GNUNET_OK != GNUNET_NETWORK_socket_bind (mhd_unix_socket,
3527 (struct sockaddr*)&mhd_unix_sock_addr,
3530 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3531 "Unable to bind unix domain socket!\n");
3535 if (GNUNET_OK != GNUNET_NETWORK_socket_listen (mhd_unix_socket,
3538 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3539 "Unable to listen on unix domain socket!\n");
3544 hd = GNUNET_malloc (sizeof (struct MhdHttpList));
3545 hd->is_ssl = GNUNET_NO;
3546 strcpy (hd->domain, "");
3547 httpd = MHD_start_daemon (MHD_USE_DEBUG
3548 #if HAVE_MHD_NO_LISTEN_SOCKET
3549 | MHD_USE_NO_LISTEN_SOCKET,
3552 , 4444, //Dummy port
3555 &create_response, hd,
3556 #if !HAVE_MHD_NO_LISTEN_SOCKET
3557 MHD_OPTION_LISTEN_SOCKET, GNUNET_NETWORK_get_fd (mhd_unix_socket),
3559 MHD_OPTION_CONNECTION_LIMIT, MHD_MAX_CONNECTIONS,
3560 MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) 16,
3561 MHD_OPTION_NOTIFY_COMPLETED,
3563 MHD_OPTION_URI_LOG_CALLBACK, &mhd_log_callback, NULL,
3566 GNUNET_assert (httpd != NULL);
3568 hd->httpd_task = GNUNET_SCHEDULER_NO_TASK;
3570 GNUNET_CONTAINER_DLL_insert (mhd_httpd_head, mhd_httpd_tail, hd);
3574 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
3575 &do_shutdown, NULL);
3581 * The main function for gnunet-gns-proxy.
3583 * @param argc number of arguments from the command line
3584 * @param argv command line arguments
3585 * @return 0 ok, 1 on error
3588 main (int argc, char *const *argv)
3590 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
3592 gettext_noop ("listen on specified port"), 1,
3593 &GNUNET_GETOPT_set_string, &port},
3594 {'a', "authority", NULL,
3595 gettext_noop ("pem file to use as CA"), 1,
3596 &GNUNET_GETOPT_set_string, &cafile_opt},
3597 GNUNET_GETOPT_OPTION_END
3602 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
3606 GNUNET_log_setup ("gnunet-gns-proxy", "WARNING", NULL);
3609 GNUNET_PROGRAM_run (argc, argv, "gnunet-gns-proxy",
3610 _("GNUnet GNS proxy"),
3612 &run, NULL)) ? 0 : 1;
3613 GNUNET_free_non_null ((char*)argv);