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.
21 * @author Martin Schanzenbach
22 * @file src/gns/gnunet-gns-proxy.c
23 * @brief HTTP(S) proxy that rewrites URIs and fakes certificats to make GADS work
24 * with legacy browsers
27 #include <microhttpd.h>
28 #include <curl/curl.h>
29 #include <gnutls/gnutls.h>
30 #include <gnutls/x509.h>
31 #include <gnutls/abstract.h>
32 #include <gnutls/crypto.h>
34 #include "gnunet_util_lib.h"
35 #include "gnunet_gns_service.h"
36 #include "gns_proxy_proto.h"
39 #define HAVE_MHD_NO_LISTEN_SOCKET (MHD_VERSION >= 0x00091401)
41 #define GNUNET_GNS_PROXY_PORT 7777
42 #define MHD_MAX_CONNECTIONS 300
43 #define MAX_HTTP_URI_LENGTH 2048
44 #define POSTBUFFERSIZE 4096
46 /* MHD/cURL defines */
54 #define HTML_HDR_CONTENT "Content-Type: text/html"
56 /* buffer padding for proper RE matching */
57 #define CURL_BUF_PADDING 1000
60 //#define RE_DOTPLUS "<a href=\"http://(([A-Za-z]+[.])+)([+])"
61 #define RE_A_HREF "href=\"https?://(([A-Za-z0-9]+[.])+)([+]|[a-z]+)"
62 #define RE_N_MATCHES 4
64 /* The usual suspects */
66 #define HTTPS_PORT 443
70 * A structure for CA cert/key
75 gnutls_x509_crt_t cert;
78 gnutls_x509_privkey_t key;
81 #define MAX_PEM_SIZE (10 * 1024)
84 * Structure for GNS certificates
86 struct ProxyGNSCertificate
88 /* The certificate as PEM */
89 char cert[MAX_PEM_SIZE];
91 /* The private key as PEM */
92 char key[MAX_PEM_SIZE];
97 * A structure for socks requests
101 /* The client socket */
102 struct GNUNET_NETWORK_Handle *sock;
104 /* The server socket */
105 struct GNUNET_NETWORK_Handle *remote_sock;
107 /* The socks state */
110 /* Client socket read task */
111 GNUNET_SCHEDULER_TaskIdentifier rtask;
113 /* Server socket read task */
114 GNUNET_SCHEDULER_TaskIdentifier fwdrtask;
116 /* Client socket write task */
117 GNUNET_SCHEDULER_TaskIdentifier wtask;
119 /* Server socket write task */
120 GNUNET_SCHEDULER_TaskIdentifier fwdwtask;
128 /* Length of data in read buffer */
129 unsigned int rbuf_len;
131 /* Length of data in write buffer */
132 unsigned int wbuf_len;
134 /* This handle is scheduled for cleanup? */
137 /* Shall we close the client socket on cleanup? */
142 * DLL for Network Handles
144 struct NetworkHandleList
147 struct NetworkHandleList *next;
150 struct NetworkHandleList *prev;
153 struct GNUNET_NETWORK_Handle *h;
157 * A structure for all running Httpds
162 struct MhdHttpList *prev;
165 struct MhdHttpList *next;
167 /* is this an ssl daemon? */
170 /* the domain name to server (only important for SSL) */
173 /* The daemon handle */
174 struct MHD_Daemon *daemon;
176 /* Optional proxy certificate used */
177 struct ProxyGNSCertificate *proxy_cert;
180 GNUNET_SCHEDULER_TaskIdentifier httpd_task;
182 /* Handles associated with this daemon */
183 struct NetworkHandleList *socket_handles_head;
185 /* Handles associated with this daemon */
186 struct NetworkHandleList *socket_handles_tail;
190 * A structure for MHD<->cURL streams
195 struct ProxyCurlTask *prev;
198 struct ProxyCurlTask *next;
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;
225 struct MHD_Connection *connection;
228 size_t put_read_offset;
229 size_t put_read_size;
232 struct MHD_PostProcessor *post_handler;
235 struct ProxyUploadData *upload_data_head;
236 struct ProxyUploadData *upload_data_tail;
238 /* the type of POST encoding */
241 struct curl_httppost *httppost;
243 struct curl_httppost *httppost_last;
245 /* Number of bytes in buffer */
246 unsigned int bytes_in_buffer;
249 GNUNET_SCHEDULER_TaskIdentifier pp_task;
252 struct ProxyREMatch *pp_match_head;
255 struct ProxyREMatch *pp_match_tail;
257 /* The associated daemon list entry */
258 struct MhdHttpList *mhd;
260 /* The associated response */
261 struct MHD_Response *response;
264 struct ProxySetCookieHeader *set_cookies_head;
267 struct ProxySetCookieHeader *set_cookies_tail;
269 /* The authority of the corresponding host (site of origin) */
272 /* The hostname (Host header field) */
275 /* The LEgacy HOstname (can be empty) */
281 /* The buffer status (BUF_WAIT_FOR_CURL or BUF_WAIT_FOR_MHD) */
282 enum BufferStatus buf_status;
284 /* connection status */
287 /* is curl running? */
293 /* Already accepted */
296 /* Indicates wheather the download is in progress */
297 int download_in_progress;
299 /* Indicates wheather the download was successful */
300 int download_is_finished;
302 /* Indicates wheather the download failed */
305 /* Indicates wheather we need to parse HTML */
308 /* Indicates wheather we are postprocessing the HTML right now */
309 int is_postprocessing;
311 /* Indicates wheather postprocessing has finished */
321 * Struct for RE matches in postprocessing of HTML
326 struct ProxyREMatch *next;
329 struct ProxyREMatch *prev;
331 /* start of match in buffer */
334 /* end of match in buffer */
337 /* associated proxycurltask */
338 struct ProxyCurlTask *ctask;
347 struct GNUNET_GNS_ShortenRequest *shorten_task;
358 * Struct for set-cookies
360 struct ProxySetCookieHeader
363 struct ProxySetCookieHeader *next;
366 struct ProxySetCookieHeader *prev;
373 * Post data structure
375 struct ProxyUploadData
378 struct ProxyUploadData *next;
381 struct ProxyUploadData *prev;
389 size_t content_length;
402 /* The port the proxy is running on (default 7777) */
403 static unsigned long port = GNUNET_GNS_PROXY_PORT;
405 /* The CA file (pem) to use for the proxy CA */
406 static char* cafile_opt;
408 /* The listen socket of the proxy */
409 static struct GNUNET_NETWORK_Handle *lsock;
411 /* The listen task ID */
412 static GNUNET_SCHEDULER_TaskIdentifier ltask;
414 /* The cURL download task */
415 static GNUNET_SCHEDULER_TaskIdentifier curl_download_task;
417 /* The non SSL httpd daemon handle */
418 static struct MHD_Daemon *httpd;
420 /* Number of current mhd connections */
421 static unsigned int total_mhd_connections;
423 /* The cURL multi handle */
424 static CURLM *curl_multi;
426 /* Handle to the GNS service */
427 static struct GNUNET_GNS_Handle *gns_handle;
429 /* DLL for ProxyCurlTasks */
430 static struct ProxyCurlTask *ctasks_head;
432 /* DLL for ProxyCurlTasks */
433 static struct ProxyCurlTask *ctasks_tail;
435 /* DLL for http daemons */
436 static struct MhdHttpList *mhd_httpd_head;
438 /* DLL for http daemons */
439 static struct MhdHttpList *mhd_httpd_tail;
441 /* Handle to the regex for dotplus (.+) replacement in HTML */
442 static regex_t re_dotplus;
444 /* The users local GNS zone hash */
445 static struct GNUNET_CRYPTO_ShortHashCode *local_gns_zone;
447 /* The users local private zone */
448 static struct GNUNET_CRYPTO_ShortHashCode *local_private_zone;
450 /* The users local shorten zone */
451 static struct GNUNET_CRYPTO_ShortHashCode *local_shorten_zone;
453 /* The CA for SSL certificate generation */
454 static struct ProxyCA proxy_ca;
456 /* UNIX domain socket for mhd */
457 #if !HAVE_MHD_NO_LISTEN_SOCKET
458 static struct GNUNET_NETWORK_Handle *mhd_unix_socket;
461 /* Shorten zone private key */
462 static struct GNUNET_CRYPTO_RsaPrivateKey *shorten_zonekey;
466 * Checks if name is in tld
468 * @param name the name to check
469 * @param tld the TLD to check for (must NOT begin with ".")
470 * @return GNUNET_YES or GNUNET_NO
473 is_tld (const char* name, const char* tld)
475 size_t name_len = strlen (name);
476 size_t tld_len = strlen (tld);
478 GNUNET_break ('.' != tld[0]);
479 return ( (tld_len < name_len) &&
480 ( ('.' == name[name_len - tld_len - 1]) || (name_len == tld_len) ) &&
482 name + (name_len - tld_len),
488 * convert integer to string representation
491 * @return the character
494 i_to_hexchar (unsigned char i)
496 static char hexmap[] = "0123456789abcdef";
498 GNUNET_assert (sizeof (hexmap) > (i & 15));
499 return hexmap[i & 15];
504 // FIXME: use cURL API
505 * Escape given 0-terminated string
507 * @param to_esc string to escapse
508 * @return allocated new escaped string (MUST free!)
511 escape_to_urlenc (const char *to_esc)
513 char *pos = (char*)to_esc;
514 char *res = GNUNET_malloc (strlen (to_esc) * 3 + 1);
519 if (isalnum (*pos) ||
520 ('-' == *pos) || ('_' == *pos) ||
521 ('.' == *pos) || ('~' == *pos))
523 else if (' ' == *pos)
528 *rpos++ = i_to_hexchar (*pos >> 4);
529 *rpos++ = i_to_hexchar (*pos >> 15);
539 con_post_data_iter (void *cls,
540 enum MHD_ValueKind kind,
542 const char *filename,
543 const char *content_type,
544 const char *transfer_encoding,
549 struct ProxyCurlTask* ctask = cls;
550 struct ProxyUploadData* pdata;
554 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
555 "Got POST data (file: %s, content type: %s): '%s=%.*s' at offset %llu size %llu\n",
556 filename, content_type,
557 key, (int) size, data,
558 (unsigned long long) off,
559 (unsigned long long) size);
560 GNUNET_assert (NULL != ctask->post_type);
562 if (0 == strcasecmp (MHD_HTTP_POST_ENCODING_MULTIPART_FORMDATA,
565 ctask->is_httppost = GNUNET_YES;
569 pdata = GNUNET_malloc (sizeof (struct ProxyUploadData));
570 pdata->key = GNUNET_strdup (key);
572 if (NULL != filename)
573 pdata->filename = GNUNET_strdup (filename);
574 if (NULL != content_type)
575 pdata->content_type = GNUNET_strdup (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 %llu bytes of POST Data\n",
585 (unsigned long long) size);
589 pdata = ctask->upload_data_tail;
590 new_value = GNUNET_malloc (size + pdata->total_bytes);
591 memcpy (new_value, pdata->value, pdata->total_bytes);
592 memcpy (new_value+off, data, size);
593 GNUNET_free (pdata->value);
594 pdata->value = new_value;
595 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 = strchr (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 (MHD_HTTP_HEADER_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,
752 * Check HTTP response header for mime
754 * @param buffer curl buffer
755 * @param size curl blocksize
756 * @param nmemb curl blocknumber
758 * @return size of read bytes
761 curl_check_hdr (void *buffer, size_t size, size_t nmemb, void *cls)
763 size_t bytes = size * nmemb;
764 struct ProxyCurlTask *ctask = cls;
765 int html_mime_len = strlen (HTML_HDR_CONTENT);
766 int cookie_hdr_len = strlen (MHD_HTTP_HEADER_SET_COOKIE);
767 char hdr_mime[html_mime_len+1];
768 char hdr_generic[bytes+1];
769 char new_cookie_hdr[bytes+strlen (ctask->leho)+1];
770 char new_location[MAX_HTTP_URI_LENGTH+500];
780 char cors_hdr[strlen (ctask->leho) + strlen ("https://")];
782 if (NULL == ctask->response)
784 /* FIXME: get total size from curl (if available) */
785 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
786 "Creating response for %s\n", ctask->url);
787 ctask->response = MHD_create_response_from_callback (MHD_SIZE_UNKNOWN,
788 sizeof (ctask->buffer),
793 /* if we have a leho add a CORS header */
794 if (0 != strcmp ("", ctask->leho))
796 /* We could also allow ssl and http here */
797 if (ctask->mhd->is_ssl)
798 sprintf (cors_hdr, "https://%s", ctask->leho);
800 sprintf (cors_hdr, "http://%s", ctask->leho);
802 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
803 "MHD: Adding CORS header field %s\n",
806 if (GNUNET_NO == MHD_add_response_header (ctask->response,
807 "Access-Control-Allow-Origin",
810 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
811 "MHD: Error adding CORS header field %s\n",
815 ctask->ready_to_queue = GNUNET_YES;
818 if (html_mime_len <= bytes)
820 memcpy (hdr_mime, buffer, html_mime_len);
821 hdr_mime[html_mime_len] = '\0';
823 if (0 == strcmp (hdr_mime, HTML_HDR_CONTENT))
825 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
826 "Got HTML HTTP response header\n");
827 ctask->parse_content = GNUNET_YES;
831 if (cookie_hdr_len > bytes)
834 memcpy (hdr_generic, buffer, bytes);
835 hdr_generic[bytes] = '\0';
837 if ('\n' == hdr_generic[bytes-1])
838 hdr_generic[bytes-1] = '\0';
840 if (hdr_generic[bytes-2] == '\r')
841 hdr_generic[bytes-2] = '\0';
843 if (0 == memcmp (hdr_generic,
844 MHD_HTTP_HEADER_SET_COOKIE,
847 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
848 "Looking for cookie in: `%s'\n", hdr_generic);
849 ndup = GNUNET_strdup (hdr_generic+cookie_hdr_len+1);
850 memset (new_cookie_hdr, 0, sizeof (new_cookie_hdr));
851 for (tok = strtok (ndup, ";"); tok != NULL; tok = strtok (NULL, ";"))
853 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
854 "Got Cookie token: %s\n", tok);
855 //memcpy (new_cookie_hdr+offset, tok, strlen (tok));
856 if (0 == memcmp (tok, " domain", strlen (" domain")))
858 cookie_domain = tok + strlen (" domain") + 1;
860 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
861 "Got Set-Cookie Domain: %s\n", cookie_domain);
863 if (strlen (cookie_domain) < strlen (ctask->leho))
865 delta_cdomain = strlen (ctask->leho) - strlen (cookie_domain);
866 if (0 == strcmp (cookie_domain, ctask->leho + (delta_cdomain)))
868 GNUNET_snprintf (new_cookie_hdr+offset,
869 sizeof (new_cookie_hdr),
870 " domain=%s", ctask->authority);
871 offset += strlen (" domain=") + strlen (ctask->authority);
872 new_cookie_hdr[offset] = ';';
877 else if (strlen (cookie_domain) == strlen (ctask->leho))
879 if (0 == strcmp (cookie_domain, ctask->leho))
881 GNUNET_snprintf (new_cookie_hdr+offset,
882 sizeof (new_cookie_hdr),
883 " domain=%s", ctask->host);
884 offset += strlen (" domain=") + strlen (ctask->host);
885 new_cookie_hdr[offset] = ';';
890 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
891 "Cookie domain invalid\n");
895 memcpy (new_cookie_hdr+offset, tok, strlen (tok));
896 offset += strlen (tok);
897 new_cookie_hdr[offset] = ';';
903 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
904 "Got Set-Cookie HTTP header %s\n", new_cookie_hdr);
906 if (GNUNET_NO == MHD_add_response_header (ctask->response,
907 MHD_HTTP_HEADER_SET_COOKIE,
910 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
911 "MHD: Error adding set-cookie header field %s\n",
912 hdr_generic+cookie_hdr_len+1);
917 ndup = GNUNET_strdup (hdr_generic);
918 hdr_type = strtok (ndup, ":");
920 if (NULL == hdr_type)
926 hdr_val = strtok (NULL, "");
936 if (0 == strcasecmp (MHD_HTTP_HEADER_LOCATION, hdr_type))
938 if (ctask->mhd->is_ssl)
940 sprintf (leho_host, "https://%s", ctask->leho);
941 sprintf (real_host, "https://%s", ctask->host);
945 sprintf (leho_host, "http://%s", ctask->leho);
946 sprintf (real_host, "http://%s", ctask->host);
949 if (0 == memcmp (leho_host, hdr_val, strlen (leho_host)))
951 sprintf (new_location, "%s%s", real_host, hdr_val+strlen (leho_host));
952 hdr_val = new_location;
956 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
957 "Trying to set %s: %s\n",
960 if (GNUNET_NO == MHD_add_response_header (ctask->response,
964 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
965 "MHD: Error adding %s header field %s\n",
977 * @param hd a http daemon list entry
980 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);
1003 run_mhd_now (struct MhdHttpList *hd)
1005 if (GNUNET_SCHEDULER_NO_TASK != hd->httpd_task)
1007 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1008 "MHD: killing old task\n");
1009 GNUNET_SCHEDULER_cancel (hd->httpd_task);
1011 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1012 "MHD: Scheduling MHD now\n");
1013 hd->httpd_task = GNUNET_SCHEDULER_add_now (&do_httpd, hd);
1018 * Ask cURL for the select sets and schedule download
1021 curl_download_prepare (void);
1025 * Callback to free content
1027 * @param cls content to free
1028 * @param tc task context
1031 mhd_content_free (void *cls,
1032 const struct GNUNET_SCHEDULER_TaskContext *tc)
1034 struct ProxyCurlTask *ctask = cls;
1035 struct ProxyUploadData *pdata;
1037 GNUNET_assert (NULL == ctask->pp_match_head);
1038 if (NULL != ctask->headers)
1039 curl_slist_free_all (ctask->headers);
1041 if (NULL != ctask->headers)
1042 curl_slist_free_all (ctask->resolver);
1044 if (NULL != ctask->response)
1045 MHD_destroy_response (ctask->response);
1047 if (NULL != ctask->post_handler)
1048 MHD_destroy_post_processor (ctask->post_handler);
1050 if (GNUNET_SCHEDULER_NO_TASK != ctask->pp_task)
1051 GNUNET_SCHEDULER_cancel (ctask->pp_task);
1053 for (pdata = ctask->upload_data_head; NULL != pdata; pdata = ctask->upload_data_head)
1055 GNUNET_CONTAINER_DLL_remove (ctask->upload_data_head,
1056 ctask->upload_data_tail,
1058 GNUNET_free_non_null (pdata->filename);
1059 GNUNET_free_non_null (pdata->content_type);
1060 GNUNET_free_non_null (pdata->key);
1061 GNUNET_free_non_null (pdata->value);
1062 GNUNET_free (pdata);
1064 GNUNET_free (ctask);
1069 * Callback for MHD response
1071 * @param cls closure
1072 * @param pos in buffer
1074 * @param max space in buffer
1075 * @return number of bytes written
1078 mhd_content_cb (void *cls,
1083 struct ProxyCurlTask *ctask = cls;
1084 struct ProxyREMatch *re_match;
1086 size_t bytes_to_copy = ctask->buffer_write_ptr - ctask->buffer_read_ptr;
1088 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1089 "MHD: content cb for %s. To copy: %u\n",
1090 ctask->url, (unsigned int) bytes_to_copy);
1091 if ((GNUNET_YES == ctask->download_is_finished) &&
1092 (GNUNET_NO == ctask->download_error) &&
1093 (0 == bytes_to_copy))
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))
1108 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1109 "MHD: sending error response\n");
1110 ctask->download_in_progress = GNUNET_NO;
1111 run_mhd_now (ctask->mhd);
1112 GNUNET_SCHEDULER_add_now (&mhd_content_free, ctask);
1113 total_mhd_connections--;
1114 return MHD_CONTENT_READER_END_WITH_ERROR;
1117 if ( ctask->buf_status == BUF_WAIT_FOR_CURL )
1121 for (re_match = ctask->pp_match_head; NULL != re_match; re_match = ctask->pp_match_head)
1123 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1124 "MHD: Processing PP %s\n",
1125 re_match->hostname);
1126 bytes_to_copy = re_match->start - ctask->buffer_read_ptr;
1127 if (bytes_to_copy+copied > max)
1129 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1130 "MHD: buffer in response too small for %u. Using available space (%d). (%s)\n",
1131 (unsigned int) bytes_to_copy,
1134 memcpy (buf+copied, ctask->buffer_read_ptr, max-copied);
1135 ctask->buffer_read_ptr += max-copied;
1137 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1138 "MHD: copied %d bytes\n", (int) copied);
1142 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1143 "MHD: copying %u bytes to mhd response at offset %d\n",
1144 (unsigned int) bytes_to_copy, ctask->buffer_read_ptr);
1145 memcpy (buf+copied, ctask->buffer_read_ptr, bytes_to_copy);
1146 copied += bytes_to_copy;
1148 if (GNUNET_NO == re_match->done)
1150 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1151 "MHD: Waiting for PP of %s\n", re_match->hostname);
1152 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1153 "MHD: copied %d bytes\n", (int) copied);
1154 ctask->buffer_read_ptr += bytes_to_copy;
1158 if (strlen (re_match->result) > (max - copied))
1160 //FIXME partially copy domain here
1161 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1162 "MHD: buffer in response too small for %s! (%s)\n",
1165 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1166 "MHD: copied %d bytes\n", (int) copied);
1167 ctask->buffer_read_ptr += bytes_to_copy;
1171 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1172 "MHD: Adding PP result %s to buffer\n",
1174 memcpy (buf + copied, re_match->result, strlen (re_match->result));
1175 copied += strlen (re_match->result);
1176 ctask->buffer_read_ptr = re_match->end;
1177 GNUNET_CONTAINER_DLL_remove (ctask->pp_match_head,
1178 ctask->pp_match_tail,
1180 GNUNET_free (re_match);
1183 bytes_to_copy = ctask->buffer_write_ptr - ctask->buffer_read_ptr;
1185 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1186 "MHD: copied: %d left: %u, space left in buf: %d\n",
1188 (unsigned int) bytes_to_copy, (int) (max - copied));
1190 if (GNUNET_NO == ctask->download_is_finished)
1192 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1193 "MHD: Purging buffer\n");
1194 memmove (ctask->buffer, ctask->buffer_read_ptr, bytes_to_copy);
1195 ctask->buffer_read_ptr = ctask->buffer;
1196 ctask->buffer_write_ptr = ctask->buffer + bytes_to_copy;
1197 ctask->buffer[bytes_to_copy] = '\0';
1200 if (bytes_to_copy + copied > max)
1201 bytes_to_copy = max - copied;
1202 memcpy (buf+copied, ctask->buffer_read_ptr, bytes_to_copy);
1203 ctask->buffer_read_ptr += bytes_to_copy;
1204 copied += bytes_to_copy;
1205 ctask->buf_status = BUF_WAIT_FOR_CURL;
1207 if (NULL != ctask->curl)
1208 curl_easy_pause (ctask->curl, CURLPAUSE_CONT);
1210 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1211 "MHD: copied %d bytes\n", (int) copied);
1212 run_mhd_now (ctask->mhd);
1218 * Shorten result callback
1220 * @param cls the proxycurltask
1221 * @param short_name the shortened name (NULL on error)
1224 process_shorten (void* cls, const char* short_name)
1226 struct ProxyREMatch *re_match = cls;
1227 char result[sizeof (re_match->result)];
1229 if (NULL == short_name)
1231 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1232 "PP: Unable to shorten %s\n",
1233 re_match->hostname);
1234 GNUNET_CONTAINER_DLL_remove (re_match->ctask->pp_match_head,
1235 re_match->ctask->pp_match_tail,
1237 GNUNET_free (re_match);
1241 if (0 == strcmp (short_name, re_match->ctask->leho))
1242 strcpy (result, re_match->ctask->host);
1244 strcpy (result, short_name);
1246 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1247 "PP: Shorten %s -> %s\n",
1251 if (re_match->is_ssl)
1252 sprintf (re_match->result, "href=\"https://%s", result);
1254 sprintf (re_match->result, "href=\"http://%s", result);
1256 re_match->done = GNUNET_YES;
1257 run_mhd_now (re_match->ctask->mhd);
1262 * Postprocess data in buffer. From read ptr to write ptr
1264 * @param cls the curlproxytask
1265 * @param tc task context
1268 postprocess_buffer (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1270 struct ProxyCurlTask *ctask = cls;
1271 struct ProxyREMatch *re_match;
1272 char* re_ptr = ctask->buffer_read_ptr;
1273 char re_hostname[255];
1274 regmatch_t m[RE_N_MATCHES];
1276 ctask->pp_task = GNUNET_SCHEDULER_NO_TASK;
1278 if (GNUNET_YES != ctask->parse_content)
1280 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1281 "PP: Not parsing content\n");
1282 ctask->buf_status = BUF_WAIT_FOR_MHD;
1283 run_mhd_now (ctask->mhd);
1287 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1288 "PP: We need to parse the HTML\n");
1290 /* 0 means match found */
1291 while (0 == regexec (&re_dotplus, re_ptr, RE_N_MATCHES, m, 0))
1293 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1294 "PP: regex match\n");
1296 GNUNET_assert (m[1].rm_so != -1);
1298 memset (re_hostname, 0, sizeof (re_hostname));
1299 memcpy (re_hostname, re_ptr+m[1].rm_so, (m[3].rm_eo-m[1].rm_so));
1302 re_match = GNUNET_malloc (sizeof (struct ProxyREMatch));
1303 re_match->start = re_ptr + m[0].rm_so;
1304 re_match->end = re_ptr + m[3].rm_eo;
1305 re_match->done = GNUNET_NO;
1306 re_match->ctask = ctask;
1308 if ('s' == *(re_ptr+m[1].rm_so-strlen("://")-1)) //FIXME strcmp
1309 re_match->is_ssl = GNUNET_YES;
1311 re_match->is_ssl = GNUNET_NO;
1313 strcpy (re_match->hostname, re_hostname);
1314 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1315 "PP: Got hostname %s\n", re_hostname);
1316 re_ptr += m[3].rm_eo;
1318 if (GNUNET_YES == is_tld (re_match->hostname, GNUNET_GNS_TLD_PLUS))
1320 re_match->hostname[strlen(re_match->hostname)-1] = '\0';
1321 strcpy (re_match->hostname+strlen(re_match->hostname),
1325 re_match->shorten_task = GNUNET_GNS_shorten_zone (gns_handle,
1331 re_match); //FIXME cancel appropriately
1333 GNUNET_CONTAINER_DLL_insert_tail (ctask->pp_match_head,
1334 ctask->pp_match_tail,
1338 ctask->buf_status = BUF_WAIT_FOR_MHD;
1339 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1340 "PP: No more matches\n");
1341 run_mhd_now (ctask->mhd);
1345 * Handle data from cURL
1347 * @param ptr pointer to the data
1348 * @param size number of blocks of data
1349 * @param nmemb blocksize
1350 * @param ctx the curlproxytask
1351 * @return number of bytes handled
1354 curl_download_cb (void *ptr, size_t size, size_t nmemb, void* ctx)
1356 const char *cbuf = ptr;
1357 size_t total = size * nmemb;
1358 struct ProxyCurlTask *ctask = ctx;
1359 size_t buf_space = sizeof (ctask->buffer) -
1360 (ctask->buffer_write_ptr-ctask->buffer);
1362 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1363 "CURL: Got %d. %d free in buffer\n",
1366 if (BUF_WAIT_FOR_CURL != ctask->buf_status)
1367 return CURL_WRITEFUNC_PAUSE;
1369 if (total > (buf_space - CURL_BUF_PADDING))
1371 if (ctask->buf_status == BUF_WAIT_FOR_CURL)
1373 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1374 "CURL: Buffer full starting postprocessing\n");
1375 ctask->buf_status = BUF_WAIT_FOR_PP;
1376 ctask->pp_task = GNUNET_SCHEDULER_add_now (&postprocess_buffer,
1378 return CURL_WRITEFUNC_PAUSE;
1381 /* we should not get called in that case */
1382 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1383 "CURL: called out of context and no space in buffer!\n");
1384 return CURL_WRITEFUNC_PAUSE;
1387 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1388 "CURL: Copying %d bytes to buffer (%s)\n", total, ctask->url);
1389 memcpy (ctask->buffer_write_ptr, cbuf, total);
1390 ctask->bytes_in_buffer += total;
1391 ctask->buffer_write_ptr += total;
1392 ctask->buffer_write_ptr[0] = '\0';
1399 * cURL callback for put data
1402 put_read_callback (void *buf, size_t size, size_t nmemb, void *cls)
1404 struct ProxyCurlTask *ctask = cls;
1405 struct ProxyUploadData *pdata = ctask->upload_data_head;
1406 size_t len = size * nmemb;
1410 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1411 "CURL: put read callback\n");
1414 return CURL_READFUNC_PAUSE;
1417 if (NULL == pdata->value)
1419 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1420 "CURL: Terminating PUT\n");
1422 GNUNET_CONTAINER_DLL_remove (ctask->upload_data_head,
1423 ctask->upload_data_tail,
1425 GNUNET_free (pdata);
1429 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1430 "CURL: read callback value %s\n", pdata->value);
1432 to_copy = pdata->bytes_left;
1436 pos = pdata->value + (pdata->total_bytes - pdata->bytes_left);
1437 memcpy (buf, pos, to_copy);
1438 pdata->bytes_left -= to_copy;
1439 if (pdata->bytes_left <= 0)
1441 GNUNET_free (pdata->value);
1442 GNUNET_CONTAINER_DLL_remove (ctask->upload_data_head,
1443 ctask->upload_data_tail,
1445 GNUNET_free (pdata);
1452 * cURL callback for post data
1455 post_read_callback (void *buf, size_t size, size_t nmemb, void *cls)
1457 struct ProxyCurlTask *ctask = cls;
1458 struct ProxyUploadData *pdata = ctask->upload_data_head;
1459 size_t len = size * nmemb;
1463 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1464 "CURL: read callback\n");
1467 return CURL_READFUNC_PAUSE;
1470 if (NULL == pdata->value)
1472 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1473 "CURL: Terminating POST data\n");
1475 GNUNET_CONTAINER_DLL_remove (ctask->upload_data_head,
1476 ctask->upload_data_tail,
1478 GNUNET_free (pdata);
1482 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1483 "CURL: read callback value %s\n", pdata->value);
1485 to_copy = pdata->bytes_left;
1489 pos = pdata->value + (pdata->total_bytes - pdata->bytes_left);
1490 memcpy (buf, pos, to_copy);
1491 pdata->bytes_left -= to_copy;
1492 if (pdata->bytes_left <= 0)
1494 GNUNET_free (pdata->value);
1495 GNUNET_CONTAINER_DLL_remove (ctask->upload_data_head,
1496 ctask->upload_data_tail,
1498 GNUNET_free (pdata);
1505 * Task that is run when we are ready to receive more data
1508 * @param cls closure
1509 * @param tc task context
1512 curl_task_download (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
1516 * Ask cURL for the select sets and schedule download
1519 curl_download_prepare ()
1526 struct GNUNET_NETWORK_FDSet *grs;
1527 struct GNUNET_NETWORK_FDSet *gws;
1529 struct GNUNET_TIME_Relative rtime;
1535 if (CURLM_OK != (mret = curl_multi_fdset (curl_multi, &rs, &ws, &es, &max)))
1537 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1538 "%s failed at %s:%d: `%s'\n",
1539 "curl_multi_fdset", __FILE__, __LINE__,
1540 curl_multi_strerror (mret));
1541 //TODO cleanup here?
1545 GNUNET_break (CURLM_OK == curl_multi_timeout (curl_multi, &to));
1546 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1547 "cURL multi fds: max=%d timeout=%lld\n", max, (long long) to);
1549 rtime = GNUNET_TIME_UNIT_FOREVER_REL;
1551 rtime = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, to);
1552 grs = GNUNET_NETWORK_fdset_create ();
1553 gws = GNUNET_NETWORK_fdset_create ();
1554 GNUNET_NETWORK_fdset_copy_native (grs, &rs, max + 1);
1555 GNUNET_NETWORK_fdset_copy_native (gws, &ws, max + 1);
1556 if (curl_download_task != GNUNET_SCHEDULER_NO_TASK)
1557 GNUNET_SCHEDULER_cancel (curl_download_task);
1560 curl_download_task =
1561 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1564 &curl_task_download, curl_multi);
1566 else if (NULL != ctasks_head)
1568 /* as specified in curl docs */
1569 curl_download_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MILLISECONDS,
1570 &curl_task_download,
1573 GNUNET_NETWORK_fdset_destroy (gws);
1574 GNUNET_NETWORK_fdset_destroy (grs);
1579 * Task that is run when we are ready to receive more data
1582 * @param cls closure
1583 * @param tc task context
1586 curl_task_download (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1590 struct CURLMsg *msg;
1592 struct ProxyCurlTask *ctask;
1596 struct ProxyCurlTask *clean_head = NULL;
1597 struct ProxyCurlTask *clean_tail = NULL;
1599 curl_download_task = GNUNET_SCHEDULER_NO_TASK;
1601 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1603 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1604 "Shutdown requested while trying to download\n");
1608 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1616 mret = curl_multi_perform (curl_multi, &running);
1618 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1619 "Running curl tasks: %d\n", running);
1621 for (ctask = ctasks_head; NULL != ctask; ctask = ctask->next)
1623 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1624 "CTask: %s\n", ctask->url);
1628 if (num_ctasks != running)
1630 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1631 "%d ctasks, %d curl running\n", num_ctasks, running);
1637 msg = curl_multi_info_read (curl_multi, &msgnum);
1638 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1639 "Messages left: %d\n", msgnum);
1646 if ((msg->data.result != CURLE_OK) &&
1647 (msg->data.result != CURLE_GOT_NOTHING))
1649 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1650 "Download curl failed");
1652 for (ctask = ctasks_head; NULL != ctask; ctask = ctask->next)
1654 if (NULL == ctask->curl)
1657 if (memcmp (msg->easy_handle, ctask->curl, sizeof (CURL)) != 0)
1660 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1661 "CURL: Download failed for task %s: %s.\n",
1663 curl_easy_strerror (msg->data.result));
1664 ctask->download_is_finished = GNUNET_YES;
1665 ctask->download_error = GNUNET_YES;
1666 if (CURLE_OK == curl_easy_getinfo (ctask->curl,
1667 CURLINFO_RESPONSE_CODE,
1669 ctask->curl_response_code = resp_code;
1670 ctask->ready_to_queue = MHD_YES;
1671 ctask->buf_status = BUF_WAIT_FOR_MHD;
1672 run_mhd_now (ctask->mhd);
1674 GNUNET_CONTAINER_DLL_remove (ctasks_head, ctasks_tail,
1676 GNUNET_CONTAINER_DLL_insert (clean_head, clean_tail, ctask);
1679 GNUNET_assert (ctask != NULL);
1683 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1684 "CURL: download completed.\n");
1686 for (ctask = ctasks_head; NULL != ctask; ctask = ctask->next)
1688 if (NULL == ctask->curl)
1691 if (0 != memcmp (msg->easy_handle, ctask->curl, sizeof (CURL)))
1694 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1695 "CURL: completed task %s found.\n", ctask->url);
1696 if (CURLE_OK == curl_easy_getinfo (ctask->curl,
1697 CURLINFO_RESPONSE_CODE,
1699 ctask->curl_response_code = resp_code;
1702 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1703 "CURL: Completed ctask!\n");
1704 if (GNUNET_SCHEDULER_NO_TASK == ctask->pp_task)
1706 ctask->buf_status = BUF_WAIT_FOR_PP;
1707 ctask->pp_task = GNUNET_SCHEDULER_add_now (&postprocess_buffer,
1711 ctask->ready_to_queue = MHD_YES;
1712 ctask->download_is_finished = GNUNET_YES;
1714 /* We MUST not modify the multi handle else we loose messages */
1715 GNUNET_CONTAINER_DLL_remove (ctasks_head, ctasks_tail,
1717 GNUNET_CONTAINER_DLL_insert (clean_head, clean_tail, ctask);
1721 GNUNET_assert (ctask != NULL);
1723 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1724 "CURL: %s\n", curl_easy_strerror(msg->data.result));
1730 } while (msgnum > 0);
1732 for (ctask=clean_head; NULL != ctask; ctask = ctask->next)
1734 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1735 "CURL: Removing task %s.\n", ctask->url);
1736 curl_multi_remove_handle (curl_multi, ctask->curl);
1737 curl_easy_cleanup (ctask->curl);
1742 for (ctask=ctasks_head; NULL != ctask; ctask = ctask->next)
1745 if (num_ctasks != running)
1747 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1748 "CURL: %d tasks, %d running\n", num_ctasks, running);
1751 GNUNET_assert ( num_ctasks == running );
1753 } while (mret == CURLM_CALL_MULTI_PERFORM);
1755 if (mret != CURLM_OK)
1757 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "CURL: %s failed at %s:%d: `%s'\n",
1758 "curl_multi_perform", __FILE__, __LINE__,
1759 curl_multi_strerror (mret));
1761 curl_download_prepare();
1766 * Process LEHO lookup
1768 * @param cls the ctask
1769 * @param rd_count number of records returned
1770 * @param rd record data
1773 process_leho_lookup (void *cls,
1775 const struct GNUNET_NAMESTORE_RecordData *rd)
1777 struct ProxyCurlTask *ctask = cls;
1778 char hosthdr[262]; //256 + "Host: "
1782 struct hostent *phost;
1784 char resolvename[512];
1787 strcpy (ctask->leho, "");
1790 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1791 "No LEHO present!\n");
1793 for (i=0; i<rd_count; i++)
1795 if (rd[i].record_type != GNUNET_GNS_RECORD_LEHO)
1798 memcpy (ctask->leho, rd[i].data, rd[i].data_size);
1800 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1801 "Found LEHO %s for %s\n", ctask->leho, ctask->url);
1804 if (0 != strcmp (ctask->leho, ""))
1806 sprintf (hosthdr, "%s%s:%d", "Host: ", ctask->leho, ctask->port);
1807 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1808 "New HTTP header value: %s\n", hosthdr);
1809 ctask->headers = curl_slist_append (ctask->headers, hosthdr);
1810 GNUNET_assert (NULL != ctask->headers);
1811 ret = curl_easy_setopt (ctask->curl, CURLOPT_HTTPHEADER, ctask->headers);
1812 if (CURLE_OK != ret)
1814 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "%s failed at %s:%d: `%s'\n",
1815 "curl_easy_setopt", __FILE__, __LINE__, curl_easy_strerror(ret));
1820 if (ctask->mhd->is_ssl)
1822 phost = (struct hostent*)gethostbyname (ctask->host);
1826 ssl_ip = inet_ntoa(*((struct in_addr*)(phost->h_addr)));
1827 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1828 "SSL target server: %s\n", ssl_ip);
1829 sprintf (resolvename, "%s:%d:%s", ctask->leho, HTTPS_PORT, ssl_ip);
1830 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1831 "Curl resolve: %s\n", resolvename);
1832 ctask->resolver = curl_slist_append ( ctask->resolver, resolvename);
1833 curl_easy_setopt (ctask->curl, CURLOPT_RESOLVE, ctask->resolver);
1834 sprintf (curlurl, "https://%s:%d%s", ctask->leho, ctask->port, ctask->url);
1835 curl_easy_setopt (ctask->curl, CURLOPT_URL, curlurl);
1839 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1840 "gethostbyname failed for %s!\n", ctask->host);
1841 ctask->download_is_finished = GNUNET_YES;
1842 ctask->download_error = GNUNET_YES;
1847 if (CURLM_OK != (mret=curl_multi_add_handle (curl_multi, ctask->curl)))
1849 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1850 "%s failed at %s:%d: `%s'\n",
1851 "curl_multi_add_handle", __FILE__, __LINE__,
1852 curl_multi_strerror (mret));
1853 ctask->download_is_finished = GNUNET_YES;
1854 ctask->download_error = GNUNET_YES;
1857 GNUNET_CONTAINER_DLL_insert (ctasks_head, ctasks_tail, ctask);
1859 curl_download_prepare ();
1865 * Initialize download and trigger curl
1867 * @param cls the proxycurltask
1868 * @param auth_name the name of the authority (site of origin) of ctask->host
1872 process_get_authority (void *cls,
1873 const char* auth_name)
1875 struct ProxyCurlTask *ctask = cls;
1877 if (NULL == auth_name)
1879 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1880 "Get authority failed!\n");
1881 strcpy (ctask->authority, "");
1885 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1886 "Get authority yielded %s\n", auth_name);
1887 strcpy (ctask->authority, auth_name);
1890 GNUNET_GNS_lookup_zone (gns_handle,
1893 GNUNET_GNS_RECORD_LEHO,
1894 GNUNET_YES, //Only cached for performance
1896 &process_leho_lookup,
1902 mhd_log_callback (void* cls, const char* url)
1904 struct ProxyCurlTask *ctask;
1906 ctask = GNUNET_malloc (sizeof (struct ProxyCurlTask));
1907 strcpy (ctask->url, url);
1913 * Main MHD callback for handling requests.
1916 * @param con MHD connection handle
1917 * @param url the url in the request
1918 * @param meth the HTTP method used ("GET", "PUT", etc.)
1919 * @param ver the HTTP version string (i.e. "HTTP/1.1")
1920 * @param upload_data the data being uploaded (excluding HEADERS,
1921 * for a POST that fits into memory and that is encoded
1922 * with a supported encoding, the POST data will NOT be
1923 * given in upload_data and is instead available as
1924 * part of MHD_get_connection_values; very large POST
1925 * data *will* be made available incrementally in
1927 * @param upload_data_size set initially to the size of the
1928 * upload_data provided; the method must update this
1929 * value to the number of bytes NOT processed;
1930 * @param con_cls pointer to location where we store the 'struct Request'
1931 * @return MHD_YES if the connection was handled successfully,
1932 * MHD_NO if the socket must be closed due to a serious
1933 * error while handling the request
1936 create_response (void *cls,
1937 struct MHD_Connection *con,
1941 const char *upload_data,
1942 size_t *upload_data_size,
1945 struct MhdHttpList* hd = cls;
1946 const char* page = "<html><head><title>gnunet-gns-proxy</title>"
1947 "</head><body>cURL fail</body></html>";
1949 char curlurl[MAX_HTTP_URI_LENGTH]; // buffer overflow!
1952 struct ProxyCurlTask *ctask = *con_cls;
1953 struct ProxyUploadData *fin_post;
1954 struct curl_forms forms[5];
1955 struct ProxyUploadData *upload_data_iter;
1958 if ((0 != strcasecmp (meth, MHD_HTTP_METHOD_GET)) &&
1959 (0 != strcasecmp (meth, MHD_HTTP_METHOD_PUT)) &&
1960 (0 != strcasecmp (meth, MHD_HTTP_METHOD_POST)) &&
1961 (0 != strcasecmp (meth, MHD_HTTP_METHOD_HEAD)))
1963 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1964 "MHD: %s NOT IMPLEMENTED!\n", meth);
1969 if (GNUNET_NO == ctask->accepted)
1972 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1973 "Got %s request for %s\n", meth, url);
1975 ctask->curl = curl_easy_init();
1976 ctask->curl_running = GNUNET_NO;
1977 if (NULL == ctask->curl)
1979 ctask->response = MHD_create_response_from_buffer (strlen (page),
1981 MHD_RESPMEM_PERSISTENT);
1982 ret = MHD_queue_response (con,
1985 MHD_destroy_response (ctask->response);
1986 GNUNET_free (ctask);
1990 if (ctask->mhd->is_ssl)
1991 ctask->port = HTTPS_PORT;
1993 ctask->port = HTTP_PORT;
1995 MHD_get_connection_values (con,
1997 &con_val_iter, ctask);
1999 curl_easy_setopt (ctask->curl, CURLOPT_HEADERFUNCTION, &curl_check_hdr);
2000 curl_easy_setopt (ctask->curl, CURLOPT_HEADERDATA, ctask);
2001 curl_easy_setopt (ctask->curl, CURLOPT_WRITEFUNCTION, &curl_download_cb);
2002 curl_easy_setopt (ctask->curl, CURLOPT_WRITEDATA, ctask);
2003 curl_easy_setopt (ctask->curl, CURLOPT_FOLLOWLOCATION, 0);
2004 curl_easy_setopt (ctask->curl, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
2006 if (GNUNET_NO == ctask->mhd->is_ssl)
2008 sprintf (curlurl, "http://%s:%d%s", ctask->host, ctask->port, ctask->url);
2009 curl_easy_setopt (ctask->curl, CURLOPT_URL, curlurl);
2013 curl_easy_setopt (ctask->curl, CURLOPT_FAILONERROR, 1);
2014 curl_easy_setopt (ctask->curl, CURLOPT_CONNECTTIMEOUT, 600L);
2015 curl_easy_setopt (ctask->curl, CURLOPT_TIMEOUT, 600L);
2017 /* Add GNS header */
2018 ctask->headers = curl_slist_append (ctask->headers,
2020 ctask->accepted = GNUNET_YES;
2021 ctask->download_in_progress = GNUNET_YES;
2022 ctask->buf_status = BUF_WAIT_FOR_CURL;
2023 ctask->connection = con;
2024 ctask->curl_response_code = MHD_HTTP_OK;
2025 ctask->buffer_read_ptr = ctask->buffer;
2026 ctask->buffer_write_ptr = ctask->buffer;
2027 ctask->pp_task = GNUNET_SCHEDULER_NO_TASK;
2030 if (0 == strcasecmp (meth, MHD_HTTP_METHOD_PUT))
2032 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2033 "Setting up PUT\n");
2035 curl_easy_setopt (ctask->curl, CURLOPT_UPLOAD, 1);
2036 curl_easy_setopt (ctask->curl, CURLOPT_READDATA, ctask);
2037 curl_easy_setopt (ctask->curl, CURLOPT_READFUNCTION, &put_read_callback);
2038 ctask->headers = curl_slist_append (ctask->headers,
2039 "Transfer-Encoding: chunked");
2042 if (0 == strcasecmp (meth, MHD_HTTP_METHOD_POST))
2044 //FIXME handle multipart
2045 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2046 "Setting up POST processor\n");
2047 ctask->post_handler = MHD_create_post_processor (con,
2049 &con_post_data_iter,
2051 ctask->headers = curl_slist_append (ctask->headers,
2052 "Transfer-Encoding: chunked");
2056 if (0 == strcasecmp (meth, MHD_HTTP_METHOD_HEAD))
2058 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2059 "Setting NOBODY\n");
2060 curl_easy_setopt (ctask->curl, CURLOPT_NOBODY, 1);
2064 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2065 "MHD: Adding new curl task for %s\n", ctask->host);
2067 GNUNET_GNS_get_authority (gns_handle,
2069 &process_get_authority,
2071 ctask->ready_to_queue = GNUNET_NO;
2072 ctask->fin = GNUNET_NO;
2073 ctask->curl_running = GNUNET_YES;
2077 ctask = (struct ProxyCurlTask *) *con_cls;
2078 if (0 == strcasecmp (meth, MHD_HTTP_METHOD_POST))
2080 if (0 != *upload_data_size)
2083 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2084 "Invoking POST processor\n");
2085 MHD_post_process (ctask->post_handler,
2086 upload_data, *upload_data_size);
2087 *upload_data_size = 0;
2088 if ((GNUNET_NO == ctask->is_httppost) &&
2089 (GNUNET_NO == ctask->curl_running))
2091 curl_easy_setopt (ctask->curl, CURLOPT_POST, 1);
2092 curl_easy_setopt (ctask->curl, CURLOPT_READFUNCTION,
2093 &post_read_callback);
2094 curl_easy_setopt (ctask->curl, CURLOPT_READDATA, ctask);
2096 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2097 "MHD: Adding new curl task for %s\n", ctask->host);
2099 GNUNET_GNS_get_authority (gns_handle,
2101 &process_get_authority,
2103 ctask->ready_to_queue = GNUNET_NO;
2104 ctask->fin = GNUNET_NO;
2105 ctask->curl_running = GNUNET_YES;
2109 else if (GNUNET_NO == ctask->post_done)
2111 if (GNUNET_YES == ctask->is_httppost)
2113 for (upload_data_iter = ctask->upload_data_head;
2114 NULL != upload_data_iter;
2115 upload_data_iter = upload_data_iter->next)
2118 if (NULL != upload_data_iter->filename)
2120 forms[i].option = CURLFORM_FILENAME;
2121 forms[i].value = upload_data_iter->filename;
2122 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2123 "Adding filename %s\n",
2127 if (NULL != upload_data_iter->content_type)
2129 forms[i].option = CURLFORM_CONTENTTYPE;
2130 forms[i].value = upload_data_iter->content_type;
2131 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2132 "Adding content type %s\n",
2136 forms[i].option = CURLFORM_PTRCONTENTS;
2137 forms[i].value = upload_data_iter->value;
2138 forms[i+1].option = CURLFORM_END;
2140 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2141 "Adding formdata for %s (len=%lld)\n",
2142 upload_data_iter->key,
2143 upload_data_iter->total_bytes);
2145 curl_formadd(&ctask->httppost, &ctask->httppost_last,
2146 CURLFORM_COPYNAME, upload_data_iter->key,
2147 CURLFORM_CONTENTSLENGTH, upload_data_iter->total_bytes,
2148 CURLFORM_ARRAY, forms,
2151 curl_easy_setopt (ctask->curl, CURLOPT_HTTPPOST,
2154 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2155 "MHD: Adding new curl task for %s\n", ctask->host);
2157 GNUNET_GNS_get_authority (gns_handle,
2159 &process_get_authority,
2161 ctask->ready_to_queue = GNUNET_YES;
2162 ctask->fin = GNUNET_NO;
2163 ctask->curl_running = GNUNET_YES;
2164 ctask->post_done = GNUNET_YES;
2168 fin_post = GNUNET_malloc (sizeof (struct ProxyUploadData));
2169 GNUNET_CONTAINER_DLL_insert_tail (ctask->upload_data_head,
2170 ctask->upload_data_tail,
2172 ctask->post_done = GNUNET_YES;
2177 if (GNUNET_YES != ctask->ready_to_queue)
2178 return MHD_YES; /* wait longer */
2180 if (GNUNET_YES == ctask->fin)
2183 ctask->fin = GNUNET_YES;
2184 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2185 "MHD: Queueing response for %s\n", ctask->url);
2186 ret = MHD_queue_response (con, ctask->curl_response_code, ctask->response);
2187 run_mhd_now (ctask->mhd);
2198 struct MhdHttpList *hd;
2200 for (hd=mhd_httpd_head; NULL != hd; hd = hd->next)
2206 #define UNSIGNED_MHD_LONG_LONG unsigned MHD_LONG_LONG
2212 * @param hd the daemon to run
2215 run_httpd (struct MhdHttpList *hd)
2220 struct GNUNET_NETWORK_FDSet *wrs;
2221 struct GNUNET_NETWORK_FDSet *wws;
2222 struct GNUNET_NETWORK_FDSet *wes;
2225 UNSIGNED_MHD_LONG_LONG timeout;
2226 struct GNUNET_TIME_Relative tv;
2231 wrs = GNUNET_NETWORK_fdset_create ();
2232 wes = GNUNET_NETWORK_fdset_create ();
2233 wws = GNUNET_NETWORK_fdset_create ();
2235 GNUNET_assert (MHD_YES == MHD_get_fdset (hd->daemon, &rs, &ws, &es, &max));
2238 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2239 "MHD fds: max=%d\n", max);
2241 haveto = MHD_get_timeout (hd->daemon, &timeout);
2243 if (MHD_YES == haveto)
2244 tv.rel_value = (uint64_t) timeout;
2246 tv = GNUNET_TIME_UNIT_FOREVER_REL;
2247 GNUNET_NETWORK_fdset_copy_native (wrs, &rs, max + 1);
2248 GNUNET_NETWORK_fdset_copy_native (wws, &ws, max + 1);
2249 GNUNET_NETWORK_fdset_copy_native (wes, &es, max + 1);
2251 if (GNUNET_SCHEDULER_NO_TASK != hd->httpd_task)
2252 GNUNET_SCHEDULER_cancel (hd->httpd_task);
2254 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_HIGH,
2257 GNUNET_NETWORK_fdset_destroy (wrs);
2258 GNUNET_NETWORK_fdset_destroy (wws);
2259 GNUNET_NETWORK_fdset_destroy (wes);
2264 * Task run whenever HTTP server operations are pending.
2267 * @param tc sched context
2270 do_httpd (void *cls,
2271 const struct GNUNET_SCHEDULER_TaskContext *tc)
2273 struct MhdHttpList *hd = cls;
2275 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2276 "MHD: Main loop\n");
2277 hd->httpd_task = GNUNET_SCHEDULER_NO_TASK;
2278 MHD_run (hd->daemon);
2284 * Read data from socket
2286 * @param cls the closure
2287 * @param tc scheduler context
2290 do_read (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
2294 * Read from remote end
2296 * @param cls closure
2297 * @param tc scheduler context
2300 do_read_remote (void* cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
2304 * Write data to remote socket
2306 * @param cls the closure
2307 * @param tc scheduler context
2310 do_write_remote (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2312 struct Socks5Request *s5r = cls;
2315 s5r->fwdwtask = GNUNET_SCHEDULER_NO_TASK;
2317 if ((NULL != tc->read_ready) &&
2318 (GNUNET_NETWORK_fdset_isset (tc->write_ready, s5r->remote_sock)) &&
2319 ((len = GNUNET_NETWORK_socket_send (s5r->remote_sock, s5r->rbuf,
2322 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2323 "Successfully sent %d bytes to remote socket\n",
2328 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "write remote");
2329 if (GNUNET_SCHEDULER_NO_TASK != s5r->rtask)
2330 GNUNET_SCHEDULER_cancel (s5r->rtask);
2331 if (GNUNET_SCHEDULER_NO_TASK != s5r->wtask)
2332 GNUNET_SCHEDULER_cancel (s5r->wtask);
2333 if (GNUNET_SCHEDULER_NO_TASK != s5r->fwdrtask)
2334 GNUNET_SCHEDULER_cancel (s5r->fwdrtask);
2335 GNUNET_NETWORK_socket_close (s5r->remote_sock);
2336 GNUNET_NETWORK_socket_close (s5r->sock);
2342 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
2349 * Clean up s5r handles
2351 * @param s5r the handle to destroy
2354 cleanup_s5r (struct Socks5Request *s5r)
2356 if (GNUNET_SCHEDULER_NO_TASK != s5r->rtask)
2357 GNUNET_SCHEDULER_cancel (s5r->rtask);
2358 if (GNUNET_SCHEDULER_NO_TASK != s5r->fwdwtask)
2359 GNUNET_SCHEDULER_cancel (s5r->fwdwtask);
2360 if (GNUNET_SCHEDULER_NO_TASK != s5r->fwdrtask)
2361 GNUNET_SCHEDULER_cancel (s5r->fwdrtask);
2362 if (NULL != s5r->remote_sock)
2363 GNUNET_NETWORK_socket_close (s5r->remote_sock);
2364 if ((NULL != s5r->sock) && (s5r->cleanup_sock == GNUNET_YES))
2365 GNUNET_NETWORK_socket_close (s5r->sock);
2372 * Write data to socket
2374 * @param cls the closure
2375 * @param tc scheduler context
2378 do_write (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2380 struct Socks5Request *s5r = cls;
2383 s5r->wtask = GNUNET_SCHEDULER_NO_TASK;
2385 if ((NULL != tc->read_ready) &&
2386 (GNUNET_NETWORK_fdset_isset (tc->write_ready, s5r->sock)) &&
2387 ((len = GNUNET_NETWORK_socket_send (s5r->sock, s5r->wbuf,
2390 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2391 "Successfully sent %d bytes to socket\n",
2397 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "write");
2398 s5r->cleanup = GNUNET_YES;
2399 s5r->cleanup_sock = GNUNET_YES;
2405 if (GNUNET_YES == s5r->cleanup)
2411 if ((s5r->state == SOCKS5_DATA_TRANSFER) &&
2412 (s5r->fwdrtask == GNUNET_SCHEDULER_NO_TASK))
2414 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
2416 &do_read_remote, s5r);
2421 * Read from remote end
2423 * @param cls closure
2424 * @param tc scheduler context
2427 do_read_remote (void* cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2429 struct Socks5Request *s5r = cls;
2431 s5r->fwdrtask = GNUNET_SCHEDULER_NO_TASK;
2434 if ((NULL != tc->write_ready) &&
2435 (GNUNET_NETWORK_fdset_isset (tc->read_ready, s5r->remote_sock)) &&
2436 (s5r->wbuf_len = GNUNET_NETWORK_socket_recv (s5r->remote_sock, s5r->wbuf,
2437 sizeof (s5r->wbuf))))
2439 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2440 "Successfully read %d bytes from remote socket\n",
2445 if (s5r->wbuf_len == 0)
2446 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2447 "0 bytes received from remote... graceful shutdown!\n");
2448 if (s5r->fwdwtask != GNUNET_SCHEDULER_NO_TASK)
2449 GNUNET_SCHEDULER_cancel (s5r->fwdwtask);
2450 if (s5r->rtask != GNUNET_SCHEDULER_NO_TASK)
2451 GNUNET_SCHEDULER_cancel (s5r->rtask);
2453 GNUNET_NETWORK_socket_close (s5r->remote_sock);
2454 s5r->remote_sock = NULL;
2455 GNUNET_NETWORK_socket_close (s5r->sock);
2461 s5r->wtask = GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
2469 * Adds a socket to MHD
2471 * @param h the handle to the socket to add
2472 * @param daemon the daemon to add the fd to
2473 * @return whatever MHD_add_connection returns
2476 add_handle_to_mhd (struct GNUNET_NETWORK_Handle *h, struct MHD_Daemon *daemon)
2479 struct sockaddr *addr;
2482 fd = dup (GNUNET_NETWORK_get_fd (h));
2483 addr = GNUNET_NETWORK_get_addr (h);
2484 len = GNUNET_NETWORK_get_addrlen (h);
2486 return MHD_add_connection (daemon, fd, addr, len);
2491 * Read file in filename
2493 * @param filename file to read
2494 * @param size pointer where filesize is stored
2495 * @return NULL on error
2498 load_file (const char* filename,
2505 GNUNET_DISK_file_size (filename, &fsize,
2506 GNUNET_YES, GNUNET_YES))
2508 if (fsize > MAX_PEM_SIZE)
2510 *size = (unsigned int) fsize;
2511 buffer = GNUNET_malloc (*size);
2512 if (fsize != GNUNET_DISK_fn_read (filename, buffer, (size_t) fsize))
2514 GNUNET_free (buffer);
2522 * Load PEM key from file
2524 * @param key where to store the data
2525 * @param keyfile path to the PEM file
2526 * @return GNUNET_OK on success
2529 load_key_from_file (gnutls_x509_privkey_t key, const char* keyfile)
2531 gnutls_datum_t key_data;
2534 key_data.data = load_file (keyfile, &key_data.size);
2535 ret = gnutls_x509_privkey_import (key, &key_data,
2536 GNUTLS_X509_FMT_PEM);
2537 if (GNUTLS_E_SUCCESS != ret)
2539 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2540 _("Unable to import private key from file `%s'\n"),
2544 GNUNET_free (key_data.data);
2545 return (GNUTLS_E_SUCCESS != ret) ? GNUNET_SYSERR : GNUNET_OK;
2550 * Load cert from file
2552 * @param crt struct to store data in
2553 * @param certfile path to pem file
2554 * @return GNUNET_OK on success
2557 load_cert_from_file (gnutls_x509_crt_t crt, char* certfile)
2559 gnutls_datum_t cert_data;
2562 cert_data.data = load_file (certfile, &cert_data.size);
2563 ret = gnutls_x509_crt_import (crt, &cert_data,
2564 GNUTLS_X509_FMT_PEM);
2565 if (GNUTLS_E_SUCCESS != ret)
2567 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2568 _("Unable to import certificate %s\n"), certfile);
2571 GNUNET_free (cert_data.data);
2572 return (GNUTLS_E_SUCCESS != ret) ? GNUNET_SYSERR : GNUNET_OK;
2577 * Generate new certificate for specific name
2579 * @param name the subject name to generate a cert for
2580 * @return a struct holding the PEM data
2582 static struct ProxyGNSCertificate *
2583 generate_gns_certificate (const char *name)
2586 unsigned int serial;
2587 size_t key_buf_size;
2588 size_t cert_buf_size;
2589 gnutls_x509_crt_t request;
2593 ret = gnutls_x509_crt_init (&request);
2595 if (GNUTLS_E_SUCCESS != ret)
2600 GNUNET_break (GNUTLS_E_SUCCESS == gnutls_x509_crt_set_key (request, proxy_ca.key));
2602 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Generating cert\n");
2604 struct ProxyGNSCertificate *pgc =
2605 GNUNET_malloc (sizeof (struct ProxyGNSCertificate));
2607 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Adding DNs\n");
2609 gnutls_x509_crt_set_dn_by_oid (request, GNUTLS_OID_X520_COUNTRY_NAME,
2611 gnutls_x509_crt_set_dn_by_oid (request, GNUTLS_OID_X520_ORGANIZATION_NAME,
2613 gnutls_x509_crt_set_dn_by_oid (request, GNUTLS_OID_X520_COMMON_NAME,
2614 0, name, strlen (name));
2615 GNUNET_break (GNUTLS_E_SUCCESS == gnutls_x509_crt_set_version (request, 3));
2617 ret = gnutls_rnd (GNUTLS_RND_NONCE, &serial, sizeof (serial));
2619 etime = time (NULL);
2620 tm_data = localtime (&etime);
2623 ret = gnutls_x509_crt_set_serial (request,
2627 ret = gnutls_x509_crt_set_activation_time (request,
2630 etime = mktime (tm_data);
2637 ret = gnutls_x509_crt_set_expiration_time (request,
2639 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Signing...\n");
2641 ret = gnutls_x509_crt_sign (request, proxy_ca.cert, proxy_ca.key);
2643 key_buf_size = sizeof (pgc->key);
2644 cert_buf_size = sizeof (pgc->cert);
2646 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Exporting certificate...\n");
2648 gnutls_x509_crt_export (request, GNUTLS_X509_FMT_PEM,
2649 pgc->cert, &cert_buf_size);
2651 gnutls_x509_privkey_export (proxy_ca.key, GNUTLS_X509_FMT_PEM,
2652 pgc->key, &key_buf_size);
2655 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Cleaning up\n");
2656 gnutls_x509_crt_deinit (request);
2664 * Accept policy for mhdaemons
2667 * @param addr the sockaddr
2668 * @param addrlen the sockaddr length
2669 * @return MHD_NO if sockaddr is wrong or number of connections is too high
2672 accept_cb (void* cls, const struct sockaddr *addr, socklen_t addrlen)
2674 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2675 "In MHD accept policy cb\n");
2679 if (addr->sa_family == AF_UNIX)
2683 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2684 "Connection accepted\n");
2691 * Adds a socket to an SSL MHD instance
2692 * It is important the the domain name is
2693 * correct. In most cases we need to start a new daemon
2695 * @param h the handle to add to a daemon
2696 * @param domain the domain the ssl daemon has to serve
2697 * @return MHD_YES on success
2700 add_handle_to_ssl_mhd (struct GNUNET_NETWORK_Handle *h, const char* domain)
2702 struct MhdHttpList *hd = NULL;
2703 struct ProxyGNSCertificate *pgc;
2704 struct NetworkHandleList *nh;
2706 for (hd = mhd_httpd_head; NULL != hd; hd = hd->next)
2707 if (0 == strcmp (hd->domain, domain))
2712 pgc = generate_gns_certificate (domain);
2714 hd = GNUNET_malloc (sizeof (struct MhdHttpList));
2715 hd->is_ssl = GNUNET_YES;
2716 strcpy (hd->domain, domain);
2717 hd->proxy_cert = pgc;
2720 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2721 "No previous SSL instance found... starting new one for %s\n",
2723 #if HAVE_MHD_NO_LISTEN_SOCKET
2724 hd->daemon = MHD_start_daemon (MHD_USE_DEBUG | MHD_USE_SSL | MHD_USE_NO_LISTEN_SOCKET,
2727 &create_response, hd,
2728 MHD_OPTION_CONNECTION_LIMIT,
2729 MHD_MAX_CONNECTIONS,
2730 MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) 16,
2731 MHD_OPTION_NOTIFY_COMPLETED, NULL, NULL,
2732 MHD_OPTION_HTTPS_MEM_KEY, pgc->key,
2733 MHD_OPTION_HTTPS_MEM_CERT, pgc->cert,
2734 MHD_OPTION_URI_LOG_CALLBACK, &mhd_log_callback,
2738 hd->daemon = MHD_start_daemon (MHD_USE_DEBUG | MHD_USE_SSL,
2739 4444 /* dummy port */,
2741 &create_response, hd,
2742 MHD_OPTION_LISTEN_SOCKET, GNUNET_NETWORK_get_fd (mhd_unix_socket),
2743 MHD_OPTION_CONNECTION_LIMIT,
2744 MHD_MAX_CONNECTIONS,
2745 MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) 16,
2746 MHD_OPTION_NOTIFY_COMPLETED, NULL, NULL,
2747 MHD_OPTION_HTTPS_MEM_KEY, pgc->key,
2748 MHD_OPTION_HTTPS_MEM_CERT, pgc->cert,
2749 MHD_OPTION_URI_LOG_CALLBACK, &mhd_log_callback,
2753 GNUNET_assert (hd->daemon != NULL);
2754 hd->httpd_task = GNUNET_SCHEDULER_NO_TASK;
2756 GNUNET_CONTAINER_DLL_insert (mhd_httpd_head, mhd_httpd_tail, hd);
2759 nh = GNUNET_malloc (sizeof (struct NetworkHandleList));
2762 GNUNET_CONTAINER_DLL_insert (hd->socket_handles_head,
2763 hd->socket_handles_tail,
2766 return add_handle_to_mhd (h, hd->daemon);
2771 * Read data from incoming connection
2773 * @param cls the closure
2774 * @param tc the scheduler context
2777 do_read (void* cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2779 struct Socks5Request *s5r = cls;
2780 struct socks5_client_hello *c_hello;
2781 struct socks5_server_hello *s_hello;
2782 struct socks5_client_request *c_req;
2783 struct socks5_server_response *s_resp;
2789 struct hostent *phost;
2791 struct sockaddr_in remote_addr;
2792 struct in_addr *r_sin_addr;
2794 struct NetworkHandleList *nh;
2796 s5r->rtask = GNUNET_SCHEDULER_NO_TASK;
2798 if ((NULL != tc->write_ready) &&
2799 (GNUNET_NETWORK_fdset_isset (tc->read_ready, s5r->sock)) &&
2800 (s5r->rbuf_len = GNUNET_NETWORK_socket_recv (s5r->sock, s5r->rbuf,
2801 sizeof (s5r->rbuf))))
2803 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2804 "Successfully read %d bytes from socket\n",
2809 if (s5r->rbuf_len != 0)
2810 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "read");
2812 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client disco!\n");
2814 if (s5r->fwdrtask != GNUNET_SCHEDULER_NO_TASK)
2815 GNUNET_SCHEDULER_cancel (s5r->fwdrtask);
2816 if (s5r->wtask != GNUNET_SCHEDULER_NO_TASK)
2817 GNUNET_SCHEDULER_cancel (s5r->wtask);
2818 if (s5r->fwdwtask != GNUNET_SCHEDULER_NO_TASK)
2819 GNUNET_SCHEDULER_cancel (s5r->fwdwtask);
2820 GNUNET_NETWORK_socket_close (s5r->remote_sock);
2821 GNUNET_NETWORK_socket_close (s5r->sock);
2826 if (s5r->state == SOCKS5_INIT)
2828 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2830 c_hello = (struct socks5_client_hello*)&s5r->rbuf;
2832 GNUNET_assert (c_hello->version == SOCKS_VERSION_5);
2834 s_hello = (struct socks5_server_hello*)&s5r->wbuf;
2835 s5r->wbuf_len = sizeof( struct socks5_server_hello );
2837 s_hello->version = c_hello->version;
2838 s_hello->auth_method = SOCKS_AUTH_NONE;
2840 /* Write response to client */
2841 s5r->wtask = GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
2845 s5r->rtask = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
2849 s5r->state = SOCKS5_REQUEST;
2853 if (s5r->state == SOCKS5_REQUEST)
2855 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2856 "Processing SOCKS5 request\n");
2857 c_req = (struct socks5_client_request*)&s5r->rbuf;
2858 s_resp = (struct socks5_server_response*)&s5r->wbuf;
2859 //Only 10byte for ipv4 response!
2860 s5r->wbuf_len = 10;//sizeof (struct socks5_server_response);
2862 GNUNET_assert (c_req->addr_type == 3);
2864 dom_len = *((uint8_t*)(&(c_req->addr_type) + 1));
2865 memset(domain, 0, sizeof(domain));
2866 strncpy(domain, (char*)(&(c_req->addr_type) + 2), dom_len);
2867 req_port = *((uint16_t*)(&(c_req->addr_type) + 2 + dom_len));
2869 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2870 "Requested connection is %s:%d\n",
2874 if (is_tld (domain, GNUNET_GNS_TLD) ||
2875 is_tld (domain, GNUNET_GNS_TLD_ZKEY))
2877 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2878 "Requested connection is gnunet tld\n",
2882 if (ntohs(req_port) == HTTPS_PORT)
2884 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2885 "Requested connection is HTTPS\n");
2886 ret = add_handle_to_ssl_mhd ( s5r->sock, domain );
2888 else if (NULL != httpd)
2890 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2891 "Requested connection is HTTP\n");
2892 nh = GNUNET_malloc (sizeof (struct NetworkHandleList));
2895 GNUNET_CONTAINER_DLL_insert (mhd_httpd_head->socket_handles_head,
2896 mhd_httpd_head->socket_handles_tail,
2899 ret = add_handle_to_mhd ( s5r->sock, httpd );
2904 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2905 _("Failed to start HTTP server\n"));
2906 s_resp->version = 0x05;
2907 s_resp->reply = 0x01;
2908 s5r->cleanup = GNUNET_YES;
2909 s5r->cleanup_sock = GNUNET_YES;
2911 GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
2917 /* Signal success */
2918 s_resp->version = 0x05;
2919 s_resp->reply = 0x00;
2920 s_resp->reserved = 0x00;
2921 s_resp->addr_type = 0x01;
2923 s5r->cleanup = GNUNET_YES;
2924 s5r->cleanup_sock = GNUNET_NO;
2926 GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
2934 phost = (struct hostent*)gethostbyname (domain);
2937 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2938 "Resolve %s error!\n", domain );
2939 s_resp->version = 0x05;
2940 s_resp->reply = 0x01;
2941 s5r->cleanup = GNUNET_YES;
2942 s5r->cleanup_sock = GNUNET_YES;
2944 GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
2950 s5r->remote_sock = GNUNET_NETWORK_socket_create (AF_INET,
2953 r_sin_addr = (struct in_addr*)(phost->h_addr);
2954 remote_ip = r_sin_addr->s_addr;
2955 memset(&remote_addr, 0, sizeof(remote_addr));
2956 remote_addr.sin_family = AF_INET;
2957 #if HAVE_SOCKADDR_IN_SIN_LEN
2958 remote_addr.sin_len = sizeof (remote_addr);
2960 remote_addr.sin_addr.s_addr = remote_ip;
2961 remote_addr.sin_port = req_port;
2963 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2964 "target server: %s:%u\n", inet_ntoa(remote_addr.sin_addr),
2968 GNUNET_NETWORK_socket_connect ( s5r->remote_sock,
2969 (const struct sockaddr*)&remote_addr,
2970 sizeof (remote_addr)))
2971 && (errno != EINPROGRESS))
2973 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "connect");
2974 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2975 "socket request error...\n");
2976 s_resp->version = 0x05;
2977 s_resp->reply = 0x01;
2979 GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
2986 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2987 "new remote connection\n");
2989 s_resp->version = 0x05;
2990 s_resp->reply = 0x00;
2991 s_resp->reserved = 0x00;
2992 s_resp->addr_type = 0x01;
2994 s5r->state = SOCKS5_DATA_TRANSFER;
2997 GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
3001 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
3009 if (s5r->state == SOCKS5_DATA_TRANSFER)
3011 if ((s5r->remote_sock == NULL) || (s5r->rbuf_len == 0))
3013 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3014 "Closing connection to client\n");
3015 if (s5r->rtask != GNUNET_SCHEDULER_NO_TASK)
3016 GNUNET_SCHEDULER_cancel (s5r->rtask);
3017 if (s5r->fwdwtask != GNUNET_SCHEDULER_NO_TASK)
3018 GNUNET_SCHEDULER_cancel (s5r->fwdwtask);
3019 if (s5r->fwdrtask != GNUNET_SCHEDULER_NO_TASK)
3020 GNUNET_SCHEDULER_cancel (s5r->fwdrtask);
3021 if (s5r->fwdrtask != GNUNET_SCHEDULER_NO_TASK)
3022 GNUNET_SCHEDULER_cancel (s5r->fwdrtask);
3024 if (s5r->remote_sock != NULL)
3025 GNUNET_NETWORK_socket_close (s5r->remote_sock);
3026 GNUNET_NETWORK_socket_close (s5r->sock);
3031 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3032 "forwarding %d bytes from client\n", s5r->rbuf_len);
3035 GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
3037 &do_write_remote, s5r);
3039 if (s5r->fwdrtask == GNUNET_SCHEDULER_NO_TASK)
3042 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
3044 &do_read_remote, s5r);
3051 * Accept new incoming connections
3053 * @param cls the closure
3054 * @param tc the scheduler context
3057 do_accept (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3059 struct GNUNET_NETWORK_Handle *s;
3060 struct Socks5Request *s5r;
3062 ltask = GNUNET_SCHEDULER_NO_TASK;
3063 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
3066 ltask = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
3070 s = GNUNET_NETWORK_socket_accept (lsock, NULL, NULL);
3074 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "accept");
3078 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3079 "Got an inbound connection, waiting for data\n");
3081 s5r = GNUNET_malloc (sizeof (struct Socks5Request));
3083 s5r->state = SOCKS5_INIT;
3084 s5r->wtask = GNUNET_SCHEDULER_NO_TASK;
3085 s5r->fwdwtask = GNUNET_SCHEDULER_NO_TASK;
3086 s5r->fwdrtask = GNUNET_SCHEDULER_NO_TASK;
3087 s5r->rtask = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
3094 * Task run on shutdown
3096 * @param cls closure
3097 * @param tc task context
3100 do_shutdown (void *cls,
3101 const struct GNUNET_SCHEDULER_TaskContext *tc)
3104 struct MhdHttpList *hd;
3105 struct MhdHttpList *tmp_hd;
3106 struct NetworkHandleList *nh;
3107 struct NetworkHandleList *tmp_nh;
3108 struct ProxyCurlTask *ctask;
3109 struct ProxyCurlTask *ctask_tmp;
3110 struct ProxyUploadData *pdata;
3112 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3113 "Shutting down...\n");
3115 gnutls_global_deinit ();
3117 if (NULL != local_gns_zone)
3118 GNUNET_free (local_gns_zone);
3119 if (NULL != local_private_zone)
3120 GNUNET_free (local_private_zone);
3121 if (NULL != local_shorten_zone)
3122 GNUNET_free (local_shorten_zone);
3124 if (GNUNET_SCHEDULER_NO_TASK != curl_download_task)
3126 GNUNET_SCHEDULER_cancel (curl_download_task);
3127 curl_download_task = GNUNET_SCHEDULER_NO_TASK;
3130 for (hd = mhd_httpd_head; hd != NULL; hd = tmp_hd)
3134 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3135 "Stopping daemon\n");
3137 if (GNUNET_SCHEDULER_NO_TASK != hd->httpd_task)
3139 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3140 "Stopping select task %d\n",
3142 GNUNET_SCHEDULER_cancel (hd->httpd_task);
3143 hd->httpd_task = GNUNET_SCHEDULER_NO_TASK;
3146 if (NULL != hd->daemon)
3148 MHD_stop_daemon (hd->daemon);
3152 for (nh = hd->socket_handles_head; nh != NULL; nh = tmp_nh)
3156 GNUNET_NETWORK_socket_close (nh->h);
3161 if (NULL != hd->proxy_cert)
3163 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3164 "Free certificate\n");
3165 GNUNET_free (hd->proxy_cert);
3171 for (ctask=ctasks_head; ctask != NULL; ctask=ctask_tmp)
3173 ctask_tmp = ctask->next;
3175 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3176 "Cleaning up cURL task\n");
3178 if (ctask->curl != NULL)
3179 curl_easy_cleanup (ctask->curl);
3181 if (NULL != ctask->headers)
3182 curl_slist_free_all (ctask->headers);
3183 if (NULL != ctask->resolver)
3184 curl_slist_free_all (ctask->resolver);
3186 if (NULL != ctask->response)
3187 MHD_destroy_response (ctask->response);
3189 pdata = ctask->upload_data_head;
3191 //FIXME free pdata here
3192 for (; pdata != NULL; pdata = ctask->upload_data_head)
3194 GNUNET_CONTAINER_DLL_remove (ctask->upload_data_head,
3195 ctask->upload_data_tail,
3197 GNUNET_free_non_null (pdata->filename);
3198 GNUNET_free_non_null (pdata->content_type);
3199 GNUNET_free_non_null (pdata->key);
3200 GNUNET_free_non_null (pdata->value);
3201 GNUNET_free (pdata);
3204 GNUNET_free (ctask);
3206 curl_multi_cleanup (curl_multi);
3208 GNUNET_GNS_disconnect (gns_handle);
3213 * Compiles a regex for us
3215 * @param re ptr to re struct
3216 * @param rt the expression to compile
3217 * @return 0 on success
3220 compile_regex (regex_t *re, const char* rt)
3225 status = regcomp (re, rt, REG_EXTENDED|REG_NEWLINE);
3228 regerror (status, re, err, 1024);
3229 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3230 "Regex error compiling '%s': %s\n", rt, err);
3238 * Loads the users local zone key
3240 * @return GNUNET_YES on success
3243 load_local_zone_key (const struct GNUNET_CONFIGURATION_Handle *cfg)
3246 struct GNUNET_CRYPTO_RsaPrivateKey *key = NULL;
3247 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pkey;
3248 struct GNUNET_CRYPTO_ShortHashCode *zone = NULL;
3249 struct GNUNET_CRYPTO_ShortHashAsciiEncoded zonename;
3251 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "gns",
3252 "ZONEKEY", &keyfile))
3254 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3255 "Unable to load zone key config value!\n");
3259 if (GNUNET_NO == GNUNET_DISK_file_test (keyfile))
3261 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3262 "Unable to load zone key %s!\n", keyfile);
3263 GNUNET_free(keyfile);
3267 key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
3268 GNUNET_CRYPTO_rsa_key_get_public (key, &pkey);
3269 local_gns_zone = GNUNET_malloc (sizeof (struct GNUNET_CRYPTO_ShortHashCode));
3270 GNUNET_CRYPTO_short_hash(&pkey,
3271 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
3273 zone = local_gns_zone;
3274 GNUNET_CRYPTO_short_hash_to_enc (zone, &zonename);
3275 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3276 "Using zone: %s!\n", &zonename);
3277 GNUNET_CRYPTO_rsa_key_free(key);
3278 GNUNET_free(keyfile);
3281 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "gns",
3282 "PRIVATE_ZONEKEY", &keyfile))
3284 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3285 "Unable to load private zone key config value!\n");
3288 if ((NULL != keyfile) && (GNUNET_NO == GNUNET_DISK_file_test (keyfile)))
3290 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3291 "Unable to load private zone key %s!\n", keyfile);
3292 GNUNET_free(keyfile);
3296 key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
3297 GNUNET_CRYPTO_rsa_key_get_public (key, &pkey);
3298 local_private_zone = GNUNET_malloc (sizeof (struct GNUNET_CRYPTO_ShortHashCode));
3299 GNUNET_CRYPTO_short_hash(&pkey,
3300 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
3301 local_private_zone);
3302 GNUNET_CRYPTO_short_hash_to_enc (zone, &zonename);
3303 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3304 "Using private zone: %s!\n", &zonename);
3305 GNUNET_CRYPTO_rsa_key_free(key);
3306 GNUNET_free(keyfile);
3310 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "gns",
3311 "SHORTEN_ZONEKEY", &keyfile))
3313 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3314 "Unable to load shorten zone key config value!\n");
3317 if ((NULL != keyfile) && (GNUNET_NO == GNUNET_DISK_file_test (keyfile)))
3319 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3320 "Unable to load shorten zone key %s!\n", keyfile);
3321 GNUNET_free(keyfile);
3325 key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
3326 GNUNET_CRYPTO_rsa_key_get_public (key, &pkey);
3327 local_shorten_zone = GNUNET_malloc (sizeof (struct GNUNET_CRYPTO_ShortHashCode));
3328 GNUNET_CRYPTO_short_hash(&pkey,
3329 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
3330 local_shorten_zone);
3331 GNUNET_CRYPTO_short_hash_to_enc (zone, &zonename);
3332 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3333 "Using shorten zone: %s!\n", &zonename);
3334 GNUNET_CRYPTO_rsa_key_free(key);
3335 GNUNET_free(keyfile);
3343 * Main function that will be run
3345 * @param cls closure
3346 * @param args remaining command-line arguments
3347 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
3348 * @param cfg configuration
3351 run (void *cls, char *const *args, const char *cfgfile,
3352 const struct GNUNET_CONFIGURATION_Handle *cfg)
3354 struct sockaddr_in sa;
3355 struct MhdHttpList *hd;
3356 char* cafile_cfg = NULL;
3358 #if !HAVE_MHD_NO_LISTEN_SOCKET
3360 char* proxy_sockfile;
3361 struct sockaddr_un mhd_unix_sock_addr;
3364 curl_multi = curl_multi_init ();
3366 if (NULL == curl_multi)
3368 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3369 "Failed to create cURL multo handle!\n");
3373 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3375 cafile = cafile_opt;
3378 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "gns-proxy",
3382 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3383 "Unable to load proxy CA config value!\n");
3384 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3385 "No proxy CA provided!\n");
3388 cafile = cafile_cfg;
3390 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3391 "Using %s as CA\n", cafile);
3393 gnutls_global_init ();
3394 gnutls_x509_crt_init (&proxy_ca.cert);
3395 gnutls_x509_privkey_init (&proxy_ca.key);
3397 if ( (GNUNET_OK != load_cert_from_file (proxy_ca.cert, cafile)) ||
3398 (GNUNET_OK != load_key_from_file (proxy_ca.key, cafile)) )
3400 // FIXME: release resources...
3404 GNUNET_free_non_null (cafile_cfg);
3406 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3407 "Loading Template\n");
3409 compile_regex (&re_dotplus, (char*) RE_A_HREF);
3411 gns_handle = GNUNET_GNS_connect (cfg);
3413 if (GNUNET_NO == load_local_zone_key (cfg))
3415 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3416 "Unable to load zone!\n");
3420 if (NULL == gns_handle)
3422 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3423 "Unable to connect to GNS!\n");
3427 memset (&sa, 0, sizeof (sa));
3428 sa.sin_family = AF_INET;
3429 sa.sin_port = htons (port);
3430 #if HAVE_SOCKADDR_IN_SIN_LEN
3431 sa.sin_len = sizeof (sa);
3434 lsock = GNUNET_NETWORK_socket_create (AF_INET,
3438 if ((NULL == lsock) ||
3440 GNUNET_NETWORK_socket_bind (lsock, (const struct sockaddr *) &sa,
3443 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3444 "Failed to create listen socket bound to `%s'",
3445 GNUNET_a2s ((const struct sockaddr *) &sa, sizeof (sa)));
3447 GNUNET_NETWORK_socket_close (lsock);
3451 if (GNUNET_OK != GNUNET_NETWORK_socket_listen (lsock, 5))
3453 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3454 "Failed to listen on socket bound to `%s'",
3455 GNUNET_a2s ((const struct sockaddr *) &sa, sizeof (sa)));
3458 ltask = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
3459 lsock, &do_accept, NULL);
3461 if (0 != curl_global_init (CURL_GLOBAL_WIN32))
3463 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3464 "cURL global init failed!\n");
3468 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3469 "Proxy listens on port %u\n",
3471 #if ! HAVE_MHD_NO_LISTEN_SOCKET
3472 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "gns-proxy",
3476 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3477 "Specify PROXY_UNIXPATH in gns-proxy config section!\n");
3481 mhd_unix_socket = GNUNET_NETWORK_socket_create (AF_UNIX,
3485 if (NULL == mhd_unix_socket)
3487 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3488 "Unable to create unix domain socket!\n");
3492 mhd_unix_sock_addr.sun_family = AF_UNIX;
3493 strcpy (mhd_unix_sock_addr.sun_path, proxy_sockfile);
3496 mhd_unix_sock_addr.sun_path[0] = '\0';
3498 #if HAVE_SOCKADDR_IN_SIN_LEN
3499 mhd_unix_sock_addr.sun_len = (u_char) sizeof (struct sockaddr_un);
3502 len = strlen (proxy_sockfile) + sizeof(AF_UNIX);
3504 GNUNET_free (proxy_sockfile);
3506 if (GNUNET_OK != GNUNET_NETWORK_socket_bind (mhd_unix_socket,
3507 (struct sockaddr*)&mhd_unix_sock_addr,
3510 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3511 "Unable to bind unix domain socket!\n");
3515 if (GNUNET_OK != GNUNET_NETWORK_socket_listen (mhd_unix_socket,
3518 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3519 "Unable to listen on unix domain socket!\n");
3524 hd = GNUNET_malloc (sizeof (struct MhdHttpList));
3525 hd->is_ssl = GNUNET_NO;
3526 strcpy (hd->domain, "");
3528 #if HAVE_MHD_NO_LISTEN_SOCKET
3529 httpd = MHD_start_daemon (MHD_USE_DEBUG | MHD_USE_NO_LISTEN_SOCKET,
3532 &create_response, hd,
3533 MHD_OPTION_CONNECTION_LIMIT, MHD_MAX_CONNECTIONS,
3534 MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) 16,
3535 MHD_OPTION_NOTIFY_COMPLETED,
3537 MHD_OPTION_URI_LOG_CALLBACK, &mhd_log_callback, NULL,
3540 httpd = MHD_start_daemon (MHD_USE_DEBUG,
3541 4444 /* Dummy port */,
3543 &create_response, hd,
3544 MHD_OPTION_LISTEN_SOCKET, GNUNET_NETWORK_get_fd (mhd_unix_socket),
3545 MHD_OPTION_CONNECTION_LIMIT, MHD_MAX_CONNECTIONS,
3546 MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) 16,
3547 MHD_OPTION_NOTIFY_COMPLETED,
3549 MHD_OPTION_URI_LOG_CALLBACK, &mhd_log_callback, NULL,
3552 GNUNET_break (httpd != NULL);
3554 hd->httpd_task = GNUNET_SCHEDULER_NO_TASK;
3555 GNUNET_CONTAINER_DLL_insert (mhd_httpd_head, mhd_httpd_tail, hd);
3557 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
3558 &do_shutdown, NULL);
3563 * The main function for gnunet-gns-proxy.
3565 * @param argc number of arguments from the command line
3566 * @param argv command line arguments
3567 * @return 0 ok, 1 on error
3570 main (int argc, char *const *argv)
3572 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
3574 gettext_noop ("listen on specified port (default: 7777)"), 1,
3575 &GNUNET_GETOPT_set_ulong, &port},
3576 {'a', "authority", NULL,
3577 gettext_noop ("pem file to use as CA"), 1,
3578 &GNUNET_GETOPT_set_string, &cafile_opt},
3579 GNUNET_GETOPT_OPTION_END
3583 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
3585 GNUNET_log_setup ("gnunet-gns-proxy", "WARNING", NULL);
3588 GNUNET_PROGRAM_run (argc, argv, "gnunet-gns-proxy",
3589 _("GNUnet GNS proxy"),
3591 &run, NULL)) ? 0 : 1;
3592 GNUNET_free_non_null ((char *) argv);
3596 /* end of gnunet-gns-proxy.c */