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 GNUNET_GNS_PROXY_PORT 7777
38 #define MHD_MAX_CONNECTIONS 300
40 /* MHD/cURL defines */
41 #define BUF_WAIT_FOR_CURL 0
42 #define BUF_WAIT_FOR_MHD 1
43 #define BUF_WAIT_FOR_PP 2
44 #define HTML_HDR_CONTENT "Content-Type: text/html"
46 /* buffer padding for proper RE matching */
47 #define CURL_BUF_PADDING 1000
50 //#define RE_DOTPLUS "<a href=\"http://(([A-Za-z]+[.])+)([+])"
51 #define RE_A_HREF "href=\"https?://(([A-Za-z0-9]+[.])+)([+]|[a-z]+)"
52 #define RE_N_MATCHES 4
54 /* The usual suspects */
56 #define HTTPS_PORT 443
60 * A structure for CA cert/key
65 gnutls_x509_crt_t cert;
68 gnutls_x509_privkey_t key;
71 #define MAX_PEM_SIZE (10 * 1024)
74 * Structure for GNS certificates
76 struct ProxyGNSCertificate
78 /* The certificate as PEM */
79 char cert[MAX_PEM_SIZE];
81 /* The private key as PEM */
82 char key[MAX_PEM_SIZE];
87 * A structure for socks requests
91 /* The client socket */
92 struct GNUNET_NETWORK_Handle *sock;
94 /* The server socket */
95 struct GNUNET_NETWORK_Handle *remote_sock;
100 /* Client socket read task */
101 GNUNET_SCHEDULER_TaskIdentifier rtask;
103 /* Server socket read task */
104 GNUNET_SCHEDULER_TaskIdentifier fwdrtask;
106 /* Client socket write task */
107 GNUNET_SCHEDULER_TaskIdentifier wtask;
109 /* Server socket write task */
110 GNUNET_SCHEDULER_TaskIdentifier fwdwtask;
118 /* Length of data in read buffer */
119 unsigned int rbuf_len;
121 /* Length of data in write buffer */
122 unsigned int wbuf_len;
124 /* This handle is scheduled for cleanup? */
127 /* Shall we close the client socket on cleanup? */
132 * DLL for Network Handles
134 struct NetworkHandleList
137 struct NetworkHandleList *next;
140 struct NetworkHandleList *prev;
143 struct GNUNET_NETWORK_Handle *h;
147 * A structure for all running Httpds
152 struct MhdHttpList *prev;
155 struct MhdHttpList *next;
157 /* is this an ssl daemon? */
160 /* the domain name to server (only important for SSL) */
163 /* The daemon handle */
164 struct MHD_Daemon *daemon;
166 /* Optional proxy certificate used */
167 struct ProxyGNSCertificate *proxy_cert;
170 GNUNET_SCHEDULER_TaskIdentifier httpd_task;
172 /* Handles associated with this daemon */
173 struct NetworkHandleList *socket_handles_head;
175 /* Handles associated with this daemon */
176 struct NetworkHandleList *socket_handles_tail;
180 * A structure for MHD<->cURL streams
185 struct ProxyCurlTask *prev;
188 struct ProxyCurlTask *next;
193 /* Optional header replacements for curl (LEHO) */
194 struct curl_slist *headers;
196 /* Optional resolver replacements for curl (LEHO) */
197 struct curl_slist *resolver;
199 /* curl response code */
200 long curl_response_code;
202 /* The URL to fetch */
205 /* The cURL write buffer / MHD read buffer */
206 char buffer[CURL_MAX_WRITE_SIZE + CURL_BUF_PADDING];
208 /* Read pos of the data in the buffer */
209 char *buffer_read_ptr;
211 /* Write pos in the buffer */
212 char *buffer_write_ptr;
214 /* The buffer status (BUF_WAIT_FOR_CURL or BUF_WAIT_FOR_MHD) */
217 /* Number of bytes in buffer */
218 unsigned int bytes_in_buffer;
220 /* Indicates wheather the download is in progress */
221 int download_in_progress;
223 /* Indicates wheather the download was successful */
224 int download_is_finished;
226 /* Indicates wheather the download failed */
229 /* Indicates wheather we need to parse HTML */
232 /* Indicates wheather we are postprocessing the HTML right now */
233 int is_postprocessing;
235 /* Indicates wheather postprocessing has finished */
239 GNUNET_SCHEDULER_TaskIdentifier pp_task;
242 struct ProxyREMatch *pp_match_head;
245 struct ProxyREMatch *pp_match_tail;
247 /* The authority of the corresponding host (site of origin) */
250 /* The hostname (Host header field) */
253 /* The LEgacy HOstname (can be empty) */
256 /* The associated daemon list entry */
257 struct MhdHttpList *mhd;
259 /* The associated response */
260 struct MHD_Response *response;
263 struct ProxySetCookieHeader *set_cookies_head;
266 struct ProxySetCookieHeader *set_cookies_tail;
268 /* connection status */
275 struct MHD_Connection *connection;
280 * Struct for RE matches in postprocessing of HTML
285 struct ProxyREMatch *next;
288 struct ProxyREMatch *prev;
297 struct GNUNET_GNS_ShortenRequest *shorten_task;
302 /* start of match in buffer */
305 /* end of match in buffer */
308 /* associated proxycurltask */
309 struct ProxyCurlTask *ctask;
313 * Struct for set-cookies
315 struct ProxySetCookieHeader
318 struct ProxySetCookieHeader *next;
321 struct ProxySetCookieHeader *prev;
327 /* The port the proxy is running on (default 7777) */
328 static unsigned long port = GNUNET_GNS_PROXY_PORT;
330 /* The CA file (pem) to use for the proxy CA */
331 static char* cafile_opt;
333 /* The listen socket of the proxy */
334 static struct GNUNET_NETWORK_Handle *lsock;
336 /* The listen task ID */
337 GNUNET_SCHEDULER_TaskIdentifier ltask;
339 /* The cURL download task */
340 GNUNET_SCHEDULER_TaskIdentifier curl_download_task;
342 /* The non SSL httpd daemon handle */
343 static struct MHD_Daemon *httpd;
345 /* Number of current mhd connections */
346 static unsigned int total_mhd_connections;
348 /* The cURL multi handle */
349 static CURLM *curl_multi;
351 /* Handle to the GNS service */
352 static struct GNUNET_GNS_Handle *gns_handle;
354 /* DLL for ProxyCurlTasks */
355 static struct ProxyCurlTask *ctasks_head;
357 /* DLL for ProxyCurlTasks */
358 static struct ProxyCurlTask *ctasks_tail;
360 /* DLL for http daemons */
361 static struct MhdHttpList *mhd_httpd_head;
363 /* DLL for http daemons */
364 static struct MhdHttpList *mhd_httpd_tail;
366 /* Handle to the regex for dotplus (.+) replacement in HTML */
367 static regex_t re_dotplus;
369 /* The users local GNS zone hash */
370 static struct GNUNET_CRYPTO_ShortHashCode local_gns_zone;
372 /* The users local private zone */
373 static struct GNUNET_CRYPTO_ShortHashCode local_private_zone;
375 /* The users local shorten zone */
376 static struct GNUNET_CRYPTO_ShortHashCode local_shorten_zone;
378 /* The CA for SSL certificate generation */
379 static struct ProxyCA proxy_ca;
381 /* UNIX domain socket for mhd */
382 struct GNUNET_NETWORK_Handle *mhd_unix_socket;
384 /* Shorten zone private key */
385 struct GNUNET_CRYPTO_RsaPrivateKey *shorten_zonekey;
388 * Checks if name is in tld
390 * @param name the name to check
391 * @param tld the TLD to check for
392 * @return GNUNET_YES or GNUNET_NO
395 is_tld(const char* name, const char* tld)
399 if (strlen(name) <= strlen(tld))
402 offset = strlen(name) - strlen(tld);
403 if (0 != strcmp (name+offset, tld))
405 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
406 "%s is not in .%s TLD\n", name, tld);
414 get_uri_val_iter (void *cls,
415 enum MHD_ValueKind kind,
421 sprintf (buf+strlen (buf), "?%s=%s", key, value);
427 * Read HTTP request header field 'Host'
429 * @param cls buffer to write to
430 * @param kind value kind
431 * @param key field key
432 * @param value field value
433 * @return MHD_NO when Host found
436 con_val_iter (void *cls,
437 enum MHD_ValueKind kind,
441 struct ProxyCurlTask *ctask = cls;
442 char* buf = ctask->host;
446 if (0 == strcmp ("Host", key))
452 if (0 == strcmp ("Accept-Encoding", key))
457 cstr = GNUNET_malloc (strlen (key) + strlen (hdr_val) + 3);
458 GNUNET_snprintf (cstr, strlen (key) + strlen (hdr_val) + 3,
459 "%s: %s", key, hdr_val);
461 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
462 "Client Header: %s\n", cstr);
464 ctask->headers = curl_slist_append (ctask->headers, cstr);
472 * Callback for MHD response
475 * @param pos in buffer
477 * @param max space in buffer
478 * @return number of bytes written
481 mhd_content_cb (void *cls,
487 * Check HTTP response header for mime
489 * @param buffer curl buffer
490 * @param size curl blocksize
491 * @param nmemb curl blocknumber
493 * @return size of read bytes
496 curl_check_hdr (void *buffer, size_t size, size_t nmemb, void *cls)
498 size_t bytes = size * nmemb;
499 struct ProxyCurlTask *ctask = cls;
500 int html_mime_len = strlen (HTML_HDR_CONTENT);
501 int cookie_hdr_len = strlen (MHD_HTTP_HEADER_SET_COOKIE);
502 char hdr_mime[html_mime_len+1];
503 char hdr_generic[bytes+1];
504 char new_cookie_hdr[bytes+strlen (ctask->leho)+1];
513 if (NULL == ctask->response)
515 /* FIXME: get total size from curl (if available) */
516 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
517 "Creating response for %s\n", ctask->url);
518 ctask->response = MHD_create_response_from_callback (MHD_SIZE_UNKNOWN,
519 sizeof (ctask->buffer),
523 ctask->ready_to_queue = GNUNET_YES;
527 if (html_mime_len <= bytes)
529 memcpy (hdr_mime, buffer, html_mime_len);
530 hdr_mime[html_mime_len] = '\0';
532 if (0 == strcmp (hdr_mime, HTML_HDR_CONTENT))
534 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
535 "Got HTML HTTP response header\n");
536 ctask->parse_content = GNUNET_YES;
540 if (cookie_hdr_len > bytes)
543 memcpy (hdr_generic, buffer, bytes);
544 hdr_generic[bytes] = '\0';
546 if ('\n' == hdr_generic[bytes-1])
547 hdr_generic[bytes-1] = '\0';
549 if (hdr_generic[bytes-2] == '\r')
550 hdr_generic[bytes-2] = '\0';
552 if (0 == memcmp (hdr_generic,
553 MHD_HTTP_HEADER_SET_COOKIE,
556 ndup = GNUNET_strdup (hdr_generic+cookie_hdr_len+1);
557 memset (new_cookie_hdr, 0, sizeof (new_cookie_hdr));
558 tok = strtok (ndup, ";");
560 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
561 "Looking for cookie in : %s\n", hdr_generic);
563 for (; tok != NULL; tok = strtok (NULL, ";"))
565 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
566 "Got Cookie token: %s\n", tok);
567 //memcpy (new_cookie_hdr+offset, tok, strlen (tok));
568 if (0 == memcmp (tok, " domain", strlen (" domain")))
570 cookie_domain = tok + strlen (" domain") + 1;
572 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
573 "Got Set-Cookie Domain: %s\n", cookie_domain);
575 if (strlen (cookie_domain) < strlen (ctask->leho))
577 delta_cdomain = strlen (ctask->leho) - strlen (cookie_domain);
578 if (0 == strcmp (cookie_domain, ctask->leho + (delta_cdomain)))
580 GNUNET_snprintf (new_cookie_hdr+offset,
581 sizeof (new_cookie_hdr),
582 " domain=%s", ctask->authority);
583 offset += strlen (" domain=") + strlen (ctask->authority);
584 new_cookie_hdr[offset] = ';';
589 else if (strlen (cookie_domain) == strlen (ctask->leho))
591 if (0 == strcmp (cookie_domain, ctask->leho))
593 GNUNET_snprintf (new_cookie_hdr+offset,
594 sizeof (new_cookie_hdr),
595 " domain=%s", ctask->host);
596 offset += strlen (" domain=") + strlen (ctask->host);
597 new_cookie_hdr[offset] = ';';
602 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
603 "Cookie domain invalid\n");
607 memcpy (new_cookie_hdr+offset, tok, strlen (tok));
608 offset += strlen (tok);
609 new_cookie_hdr[offset] = ';';
613 //memcpy (new_cookie_hdr+offset, tok, strlen (tok));
617 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
618 "Got Set-Cookie HTTP header %s\n", new_cookie_hdr);
620 //pch = GNUNET_malloc (sizeof (struct ProxySetCookieHeader));
621 //len = strlen (hdr_cookie) - cookie_hdr_len - 1;
622 //pch->cookie = GNUNET_malloc (len + 1);
623 //memset (pch->cookie, 0, len + 1);
624 //memcpy (pch->cookie, hdr_cookie+cookie_hdr_len+1, len);
625 //GNUNET_CONTAINER_DLL_insert (ctask->set_cookies_head,
626 // ctask->set_cookies_tail,
628 //pch = ctask->set_cookies_head;
629 //while (pch != NULL)
631 if (GNUNET_NO == MHD_add_response_header (ctask->response,
632 MHD_HTTP_HEADER_SET_COOKIE,
635 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
636 "MHD: Error adding set-cookie header field %s\n",
637 hdr_generic+cookie_hdr_len+1);
640 //GNUNET_free (pch->cookie);
641 //GNUNET_CONTAINER_DLL_remove (ctask->set_cookies_head,
642 // ctask->set_cookies_tail,
645 //pch = ctask->set_cookies_head;
649 ndup = GNUNET_strdup (hdr_generic);
650 hdr_type = strtok (ndup, ":");
652 if (hdr_type != NULL)
654 hdr_val = strtok (NULL, "");
657 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
658 "Trying to set %s: %s\n",
661 if (GNUNET_NO == MHD_add_response_header (ctask->response,
665 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
666 "MHD: Error adding %s header field %s\n",
680 * @param hd a http daemon list entry
683 run_httpd (struct MhdHttpList *hd);
694 * Task run whenever HTTP server operations are pending.
697 * @param tc sched context
701 const struct GNUNET_SCHEDULER_TaskContext *tc);
704 run_mhd_now (struct MhdHttpList *hd)
706 if (GNUNET_SCHEDULER_NO_TASK != hd->httpd_task)
708 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
709 "MHD: killing old task\n");
710 GNUNET_SCHEDULER_cancel (hd->httpd_task);
712 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
713 "MHD: Scheduling MHD now\n");
714 hd->httpd_task = GNUNET_SCHEDULER_add_now (&do_httpd, hd);
718 * Ask cURL for the select sets and schedule download
721 curl_download_prepare ();
724 * Callback to free content
726 * @param cls content to free
727 * @param tc task context
730 mhd_content_free (void *cls,
731 const struct GNUNET_SCHEDULER_TaskContext *tc)
733 struct ProxyCurlTask *ctask = cls;
734 GNUNET_assert (NULL == ctask->pp_match_head);
736 if (NULL != ctask->headers)
737 curl_slist_free_all (ctask->headers);
739 if (NULL != ctask->headers)
740 curl_slist_free_all (ctask->resolver);
742 if (NULL != ctask->response)
743 MHD_destroy_response (ctask->response);
751 * Callback for MHD response
754 * @param pos in buffer
756 * @param max space in buffer
757 * @return number of bytes written
760 mhd_content_cb (void *cls,
765 struct ProxyCurlTask *ctask = cls;
766 struct ProxyREMatch *re_match = ctask->pp_match_head;
768 long long int bytes_to_copy = ctask->buffer_write_ptr - ctask->buffer_read_ptr;
770 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
771 "MHD: content cb for %s. To copy: %lld\n",
772 ctask->url, bytes_to_copy);
773 GNUNET_assert (bytes_to_copy >= 0);
775 if ((GNUNET_YES == ctask->download_is_finished) &&
776 (GNUNET_NO == ctask->download_error) &&
777 (0 == bytes_to_copy) &&
778 (BUF_WAIT_FOR_CURL == ctask->buf_status))
780 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
781 "MHD: sending response for %s\n", ctask->url);
782 ctask->download_in_progress = GNUNET_NO;
783 run_mhd_now (ctask->mhd);
784 GNUNET_SCHEDULER_add_now (&mhd_content_free, ctask);
785 total_mhd_connections--;
786 return MHD_CONTENT_READER_END_OF_STREAM;
789 if ((GNUNET_YES == ctask->download_error) &&
790 (GNUNET_YES == ctask->download_is_finished) &&
791 (0 == bytes_to_copy) &&
792 (BUF_WAIT_FOR_CURL == ctask->buf_status))
794 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
795 "MHD: sending error response\n");
796 ctask->download_in_progress = GNUNET_NO;
797 run_mhd_now (ctask->mhd);
798 GNUNET_SCHEDULER_add_now (&mhd_content_free, ctask);
799 total_mhd_connections--;
800 return MHD_CONTENT_READER_END_WITH_ERROR;
803 if ( ctask->buf_status == BUF_WAIT_FOR_CURL )
807 for (; NULL != re_match; re_match = ctask->pp_match_head)
809 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
810 "MHD: Processing PP %s\n",
812 bytes_to_copy = re_match->start - ctask->buffer_read_ptr;
813 GNUNET_assert (bytes_to_copy >= 0);
815 if (bytes_to_copy+copied > max)
817 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
818 "MHD: buffer in response too small for %d. Using available space (%d). (%s)\n",
822 memcpy (buf+copied, ctask->buffer_read_ptr, max-copied);
823 ctask->buffer_read_ptr += max-copied;
825 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
826 "MHD: copied %d bytes\n", copied);
830 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
831 "MHD: copying %d bytes to mhd response at offset %d\n",
832 bytes_to_copy, ctask->buffer_read_ptr);
833 memcpy (buf+copied, ctask->buffer_read_ptr, bytes_to_copy);
834 copied += bytes_to_copy;
836 if (GNUNET_NO == re_match->done)
838 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
839 "MHD: Waiting for PP of %s\n", re_match->hostname);
840 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
841 "MHD: copied %d bytes\n", copied);
842 ctask->buffer_read_ptr += bytes_to_copy;
846 if (strlen (re_match->result) > (max - copied))
848 //FIXME partially copy domain here
849 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
850 "MHD: buffer in response too small for %s! (%s)\n",
853 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
854 "MHD: copied %d bytes\n", copied);
855 ctask->buffer_read_ptr += bytes_to_copy;
859 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
860 "MHD: Adding PP result %s to buffer\n",
862 memcpy (buf+copied, re_match->result, strlen (re_match->result));
863 copied += strlen (re_match->result);
864 ctask->buffer_read_ptr = re_match->end;
865 GNUNET_CONTAINER_DLL_remove (ctask->pp_match_head,
866 ctask->pp_match_tail,
868 GNUNET_free (re_match);
871 bytes_to_copy = ctask->buffer_write_ptr - ctask->buffer_read_ptr;
873 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
874 "MHD: copied: %d left: %d, space left in buf: %d\n",
876 bytes_to_copy, max-copied);
878 GNUNET_assert (0 <= bytes_to_copy);
880 if (GNUNET_NO == ctask->download_is_finished)
882 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
883 "MHD: Purging buffer\n");
884 memmove (ctask->buffer, ctask->buffer_read_ptr, bytes_to_copy);
885 ctask->buffer_read_ptr = ctask->buffer;
886 ctask->buffer_write_ptr = ctask->buffer + bytes_to_copy;
889 if (bytes_to_copy+copied > max)
890 bytes_to_copy = max-copied;
892 if (0 > bytes_to_copy)
895 memcpy (buf+copied, ctask->buffer_read_ptr, bytes_to_copy);
896 ctask->buffer_read_ptr += bytes_to_copy;
897 copied += bytes_to_copy;
898 ctask->buf_status = BUF_WAIT_FOR_CURL;
900 if ((NULL != ctask->curl) &&
901 (GNUNET_NO == ctask->download_is_finished) &&
902 ((ctask->buffer_write_ptr - ctask->buffer_read_ptr) <= 0))
903 curl_easy_pause (ctask->curl, CURLPAUSE_CONT);
905 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
906 "MHD: copied %d bytes\n", copied);
907 run_mhd_now (ctask->mhd);
912 * Shorten result callback
914 * @param cls the proxycurltask
915 * @param short_name the shortened name (NULL on error)
918 process_shorten (void* cls, const char* short_name)
920 struct ProxyREMatch *re_match = cls;
921 char result[sizeof (re_match->result)];
923 if (NULL == short_name)
925 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
926 "PP: Unable to shorten %s\n",
928 GNUNET_CONTAINER_DLL_remove (re_match->ctask->pp_match_head,
929 re_match->ctask->pp_match_tail,
931 GNUNET_free (re_match);
935 if (0 == strcmp (short_name, re_match->ctask->leho))
936 strcpy (result, re_match->ctask->host);
938 strcpy (result, short_name);
940 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
941 "PP: Shorten %s -> %s\n",
944 //this is evil.. what about https
945 if (re_match->ctask->mhd->is_ssl)
946 sprintf (re_match->result, "href=\"https://%s", result);
948 sprintf (re_match->result, "href=\"http://%s", result);
950 re_match->done = GNUNET_YES;
951 run_mhd_now (re_match->ctask->mhd);
956 * Tabula Rasa postprocess buffer
960 postprocess_buffer (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
962 struct ProxyCurlTask *ctask = cls;
963 struct ProxyREMatch *re_match;
964 char* re_ptr = ctask->buffer_read_ptr;
965 char re_hostname[255];
966 regmatch_t m[RE_N_MATCHES];
968 ctask->pp_task = GNUNET_SCHEDULER_NO_TASK;
970 if (GNUNET_YES != ctask->parse_content)
972 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
973 "PP: Not parsing content\n");
974 ctask->buf_status = BUF_WAIT_FOR_MHD;
975 run_mhd_now (ctask->mhd);
979 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
980 "PP: We need to parse the HTML\n");
982 /* 0 means match found */
983 while (0 == regexec (&re_dotplus, re_ptr, RE_N_MATCHES, m, 0))
985 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
986 "PP: regex match\n");
988 GNUNET_assert (m[1].rm_so != -1);
990 memset (re_hostname, 0, sizeof (re_hostname));
991 memcpy (re_hostname, re_ptr+m[1].rm_so, (m[3].rm_eo-m[1].rm_so));
993 re_match = GNUNET_malloc (sizeof (struct ProxyREMatch));
994 re_match->start = re_ptr + m[0].rm_so;
995 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
996 "XXXXX: %d\n", re_match->start);
997 re_match->end = re_ptr + m[3].rm_eo;
998 re_match->done = GNUNET_NO;
999 re_match->ctask = ctask;
1000 strcpy (re_match->hostname, re_hostname);
1001 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1002 "Got hostname %s\n", re_hostname);
1003 re_ptr += m[3].rm_eo;
1005 if (GNUNET_YES == is_tld (re_match->hostname, GNUNET_GNS_TLD_PLUS))
1007 re_match->hostname[strlen(re_match->hostname)-1] = '\0';
1008 strcpy (re_match->hostname+strlen(re_match->hostname),
1012 re_match->shorten_task = GNUNET_GNS_shorten_zone (gns_handle,
1014 &local_private_zone,
1015 &local_shorten_zone,
1018 re_match); //FIXME cancel appropriately
1020 GNUNET_CONTAINER_DLL_insert_tail (ctask->pp_match_head,
1021 ctask->pp_match_tail,
1025 ctask->buf_status = BUF_WAIT_FOR_MHD;
1026 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1027 "PP: No more matches\n");
1028 run_mhd_now (ctask->mhd);
1032 * Tabula Rasa cURL download
1036 curl_download_cb (void *ptr, size_t size, size_t nmemb, void* ctx)
1038 const char *cbuf = ptr;
1039 size_t total = size * nmemb;
1040 struct ProxyCurlTask *ctask = ctx;
1041 size_t buf_space = sizeof (ctask->buffer) -
1042 (ctask->buffer_write_ptr-ctask->buffer);
1044 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1045 "CURL: Got %d. %d free in buffer\n",
1048 if (total > (buf_space - CURL_BUF_PADDING))
1050 if (ctask->buf_status == BUF_WAIT_FOR_CURL)
1052 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1053 "CURL: Buffer full starting postprocessing\n");
1054 ctask->buf_status = BUF_WAIT_FOR_PP;
1055 ctask->pp_task = GNUNET_SCHEDULER_add_now (&postprocess_buffer,
1057 return CURL_WRITEFUNC_PAUSE;
1060 /* we should not get called in that case */
1061 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1062 "CURL: called out of context and no space in buffer!\n");
1063 return CURL_WRITEFUNC_PAUSE;
1066 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1067 "CURL: Copying %d bytes to buffer (%s)\n", total, ctask->url);
1068 memcpy (ctask->buffer_write_ptr, cbuf, total);
1069 ctask->bytes_in_buffer += total;
1070 ctask->buffer_write_ptr += total;
1072 //run_mhd_now (ctask->mhd);
1077 * Task that is run when we are ready to receive more data
1080 * @param cls closure
1081 * @param tc task context
1084 curl_task_download (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
1087 * Ask cURL for the select sets and schedule download
1090 curl_download_prepare ()
1097 struct GNUNET_NETWORK_FDSet *grs;
1098 struct GNUNET_NETWORK_FDSet *gws;
1100 struct GNUNET_TIME_Relative rtime;
1106 mret = curl_multi_fdset (curl_multi, &rs, &ws, &es, &max);
1108 if (mret != CURLM_OK)
1110 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1111 "%s failed at %s:%d: `%s'\n",
1112 "curl_multi_fdset", __FILE__, __LINE__,
1113 curl_multi_strerror (mret));
1114 //TODO cleanup here?
1118 mret = curl_multi_timeout (curl_multi, &to);
1119 rtime = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, to);
1121 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1122 "cURL multi fds: max=%d timeout=%llu\n", max, to);
1124 grs = GNUNET_NETWORK_fdset_create ();
1125 gws = GNUNET_NETWORK_fdset_create ();
1126 GNUNET_NETWORK_fdset_copy_native (grs, &rs, max + 1);
1127 GNUNET_NETWORK_fdset_copy_native (gws, &ws, max + 1);
1128 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1129 "Scheduling task cURL\n");
1131 if (curl_download_task != GNUNET_SCHEDULER_NO_TASK)
1132 GNUNET_SCHEDULER_cancel (curl_download_task);
1134 curl_download_task =
1135 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1138 &curl_task_download, curl_multi);
1139 GNUNET_NETWORK_fdset_destroy (gws);
1140 GNUNET_NETWORK_fdset_destroy (grs);
1146 * Task that is run when we are ready to receive more data
1149 * @param cls closure
1150 * @param tc task context
1153 curl_task_download (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1157 struct CURLMsg *msg;
1159 struct ProxyCurlTask *ctask;
1163 struct ProxyCurlTask *clean_head = NULL;
1164 struct ProxyCurlTask *clean_tail = NULL;
1166 curl_download_task = GNUNET_SCHEDULER_NO_TASK;
1168 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1170 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1171 "Shutdown requested while trying to download\n");
1175 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1183 mret = curl_multi_perform (curl_multi, &running);
1185 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1186 "Running curl tasks: %d\n", running);
1188 ctask = ctasks_head;
1189 for (; ctask != NULL; ctask = ctask->next)
1191 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1192 "CTask: %s\n", ctask->url);
1196 if (num_ctasks != running)
1198 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1199 "%d ctasks, %d curl running\n", num_ctasks, running);
1204 ctask = ctasks_head;
1205 msg = curl_multi_info_read (curl_multi, &msgnum);
1206 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1207 "Messages left: %d\n", msgnum);
1214 if ((msg->data.result != CURLE_OK) &&
1215 (msg->data.result != CURLE_GOT_NOTHING))
1217 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1218 "Download curl failed");
1220 for (; ctask != NULL; ctask = ctask->next)
1222 if (NULL == ctask->curl)
1225 if (memcmp (msg->easy_handle, ctask->curl, sizeof (CURL)) != 0)
1228 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1229 "CURL: Download failed for task %s: %s.\n",
1231 curl_easy_strerror (msg->data.result));
1232 ctask->download_is_finished = GNUNET_YES;
1233 ctask->download_error = GNUNET_YES;
1234 if (CURLE_OK == curl_easy_getinfo (ctask->curl,
1235 CURLINFO_RESPONSE_CODE,
1237 ctask->curl_response_code = resp_code;
1238 ctask->ready_to_queue = MHD_YES;
1239 ctask->buf_status = BUF_WAIT_FOR_MHD;
1240 run_mhd_now (ctask->mhd);
1242 GNUNET_CONTAINER_DLL_remove (ctasks_head, ctasks_tail,
1244 GNUNET_CONTAINER_DLL_insert (clean_head, clean_tail, ctask);
1247 GNUNET_assert (ctask != NULL);
1251 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1252 "CURL: download completed.\n");
1254 for (; ctask != NULL; ctask = ctask->next)
1256 if (NULL == ctask->curl)
1259 if (memcmp (msg->easy_handle, ctask->curl, sizeof (CURL)) != 0)
1262 if (ctask->buf_status != BUF_WAIT_FOR_CURL)
1265 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1266 "CURL: task %s found.\n", ctask->url);
1267 if (CURLE_OK == curl_easy_getinfo (ctask->curl,
1268 CURLINFO_RESPONSE_CODE,
1270 ctask->curl_response_code = resp_code;
1273 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1274 "CURL: Completed ctask!\n");
1275 if (GNUNET_SCHEDULER_NO_TASK == ctask->pp_task)
1277 ctask->buf_status = BUF_WAIT_FOR_PP;
1278 ctask->pp_task = GNUNET_SCHEDULER_add_now (&postprocess_buffer,
1282 //ctask->ready_to_queue = MHD_YES;
1283 ctask->download_is_finished = GNUNET_YES;
1285 //GNUNET_SCHEDULER_add_now (&run_mhd, ctask->mhd);
1287 /* We MUST not modify the multi handle else we loose messages */
1288 GNUNET_CONTAINER_DLL_remove (ctasks_head, ctasks_tail,
1290 GNUNET_CONTAINER_DLL_insert (clean_head, clean_tail, ctask);
1294 GNUNET_assert (ctask != NULL);
1296 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1297 "CURL: %s\n", curl_easy_strerror(msg->data.result));
1303 } while (msgnum > 0);
1305 for (ctask=clean_head; ctask != NULL; ctask = ctask->next)
1307 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1308 "CURL: Removing task %s.\n", ctask->url);
1309 curl_multi_remove_handle (curl_multi, ctask->curl);
1310 curl_easy_cleanup (ctask->curl);
1315 for (ctask=ctasks_head; ctask != NULL; ctask = ctask->next)
1320 if (num_ctasks != running)
1322 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1323 "CURL: %d tasks, %d running\n", num_ctasks, running);
1326 GNUNET_assert ( num_ctasks == running );
1329 } while (mret == CURLM_CALL_MULTI_PERFORM);
1331 if (mret != CURLM_OK)
1333 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "CURL: %s failed at %s:%d: `%s'\n",
1334 "curl_multi_perform", __FILE__, __LINE__,
1335 curl_multi_strerror (mret));
1337 curl_download_prepare();
1341 * Process LEHO lookup
1343 * @param cls the ctask
1344 * @param rd_count number of records returned
1345 * @param rd record data
1348 process_leho_lookup (void *cls,
1350 const struct GNUNET_NAMESTORE_RecordData *rd)
1352 struct ProxyCurlTask *ctask = cls;
1353 char hosthdr[262]; //256 + "Host: "
1357 struct hostent *phost;
1359 char resolvename[512];
1362 strcpy (ctask->leho, "");
1365 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1366 "No LEHO present!\n");
1368 for (i=0; i<rd_count; i++)
1370 if (rd[i].record_type != GNUNET_GNS_RECORD_LEHO)
1373 memcpy (ctask->leho, rd[i].data, rd[i].data_size);
1375 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1376 "Found LEHO %s for %s\n", ctask->leho, ctask->url);
1379 if (0 != strcmp (ctask->leho, ""))
1381 sprintf (hosthdr, "%s%s", "Host: ", ctask->leho);
1382 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1383 "New HTTP header value: %s\n", hosthdr);
1384 ctask->headers = curl_slist_append (ctask->headers, hosthdr);
1385 GNUNET_assert (NULL != ctask->headers);
1386 ret = curl_easy_setopt (ctask->curl, CURLOPT_HTTPHEADER, ctask->headers);
1387 if (CURLE_OK != ret)
1389 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "%s failed at %s:%d: `%s'\n",
1390 "curl_easy_setopt", __FILE__, __LINE__, curl_easy_strerror(ret));
1395 if (ctask->mhd->is_ssl)
1397 phost = (struct hostent*)gethostbyname (ctask->host);
1401 ssl_ip = inet_ntoa(*((struct in_addr*)(phost->h_addr)));
1402 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1403 "SSL target server: %s\n", ssl_ip);
1404 sprintf (resolvename, "%s:%d:%s", ctask->leho, HTTPS_PORT, ssl_ip);
1405 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1406 "Curl resolve: %s\n", resolvename);
1407 ctask->resolver = curl_slist_append ( ctask->resolver, resolvename);
1408 curl_easy_setopt (ctask->curl, CURLOPT_RESOLVE, ctask->resolver);
1409 sprintf (curlurl, "https://%s%s", ctask->leho, ctask->url);
1410 curl_easy_setopt (ctask->curl, CURLOPT_URL, curlurl);
1414 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1415 "gethostbyname failed for %s!\n", ctask->host);
1416 ctask->download_is_finished = GNUNET_YES;
1417 ctask->download_error = GNUNET_YES;
1422 if (CURLM_OK != (mret=curl_multi_add_handle (curl_multi, ctask->curl)))
1424 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1425 "%s failed at %s:%d: `%s'\n",
1426 "curl_multi_add_handle", __FILE__, __LINE__,
1427 curl_multi_strerror (mret));
1428 ctask->download_is_finished = GNUNET_YES;
1429 ctask->download_error = GNUNET_YES;
1432 GNUNET_CONTAINER_DLL_insert (ctasks_head, ctasks_tail, ctask);
1434 curl_download_prepare ();
1439 * Initialize download and trigger curl
1441 * @param cls the proxycurltask
1442 * @param auth_name the name of the authority (site of origin) of ctask->host
1446 process_get_authority (void *cls,
1447 const char* auth_name)
1449 struct ProxyCurlTask *ctask = cls;
1451 if (NULL == auth_name)
1453 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1454 "Get authority failed!\n");
1455 strcpy (ctask->authority, "");
1459 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1460 "Get authority yielded %s\n", auth_name);
1461 strcpy (ctask->authority, auth_name);
1464 GNUNET_GNS_lookup_zone (gns_handle,
1467 GNUNET_GNS_RECORD_LEHO,
1468 GNUNET_YES, //Only cached for performance
1470 &process_leho_lookup,
1476 * Main MHD callback for handling requests.
1479 * @param con MHD connection handle
1480 * @param url the url in the request
1481 * @param meth the HTTP method used ("GET", "PUT", etc.)
1482 * @param ver the HTTP version string (i.e. "HTTP/1.1")
1483 * @param upload_data the data being uploaded (excluding HEADERS,
1484 * for a POST that fits into memory and that is encoded
1485 * with a supported encoding, the POST data will NOT be
1486 * given in upload_data and is instead available as
1487 * part of MHD_get_connection_values; very large POST
1488 * data *will* be made available incrementally in
1490 * @param upload_data_size set initially to the size of the
1491 * upload_data provided; the method must update this
1492 * value to the number of bytes NOT processed;
1493 * @param con_cls pointer to location where we store the 'struct Request'
1494 * @return MHD_YES if the connection was handled successfully,
1495 * MHD_NO if the socket must be closed due to a serious
1496 * error while handling the request
1499 create_response (void *cls,
1500 struct MHD_Connection *con,
1504 const char *upload_data,
1505 size_t *upload_data_size,
1508 struct MhdHttpList* hd = cls;
1509 const char* page = "<html><head><title>gnoxy</title>"\
1510 "</head><body>cURL fail</body></html>";
1512 char curlurl[512]; // buffer overflow!
1515 struct ProxyCurlTask *ctask;
1518 if (0 != strcasecmp (meth, MHD_HTTP_METHOD_GET))
1520 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1521 "MHD: %s NOT IMPLEMENTED!\n", meth);
1525 if (0 != *upload_data_size)
1528 if (NULL == *con_cls)
1530 ctask = GNUNET_malloc (sizeof (struct ProxyCurlTask));
1534 // FIXME: probably not best here, also never free'ed...
1535 if (NULL == curl_multi)
1536 curl_multi = curl_multi_init ();
1538 ctask->curl = curl_easy_init();
1540 if ((NULL == ctask->curl) || (NULL == curl_multi)) // ugh, curl_multi init failure check MUCH earlier
1542 ctask->response = MHD_create_response_from_buffer (strlen (page),
1544 MHD_RESPMEM_PERSISTENT);
1545 ret = MHD_queue_response (con,
1548 MHD_destroy_response (ctask->response);
1549 GNUNET_free (ctask);
1553 ctask->download_in_progress = GNUNET_YES;
1554 ctask->buf_status = BUF_WAIT_FOR_CURL;
1555 ctask->connection = con;
1556 ctask->curl_response_code = MHD_HTTP_OK;
1557 ctask->buffer_read_ptr = ctask->buffer;
1558 ctask->buffer_write_ptr = ctask->buffer;
1559 ctask->pp_task = GNUNET_SCHEDULER_NO_TASK;
1561 MHD_get_connection_values (con,
1563 &con_val_iter, ctask);
1565 curl_easy_setopt (ctask->curl, CURLOPT_HEADERFUNCTION, &curl_check_hdr);
1566 curl_easy_setopt (ctask->curl, CURLOPT_HEADERDATA, ctask);
1567 curl_easy_setopt (ctask->curl, CURLOPT_WRITEFUNCTION, &curl_download_cb);
1568 curl_easy_setopt (ctask->curl, CURLOPT_WRITEDATA, ctask);
1569 curl_easy_setopt (ctask->curl, CURLOPT_FOLLOWLOCATION, 0);
1570 //curl_easy_setopt (ctask->curl, CURLOPT_MAXREDIRS, 4);
1571 /* no need to abort if the above failed */
1572 if (GNUNET_NO == ctask->mhd->is_ssl)
1574 sprintf (curlurl, "http://%s%s", ctask->host, url);
1575 MHD_get_connection_values (con,
1576 MHD_GET_ARGUMENT_KIND,
1577 &get_uri_val_iter, curlurl);
1578 curl_easy_setopt (ctask->curl, CURLOPT_URL, curlurl);
1580 strcpy (ctask->url, url);
1581 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1582 "MHD: Adding new curl task for %s%s\n", ctask->host, url);
1583 MHD_get_connection_values (con,
1584 MHD_GET_ARGUMENT_KIND,
1585 &get_uri_val_iter, ctask->url);
1587 //curl_easy_setopt (ctask->curl, CURLOPT_URL, curlurl);
1588 curl_easy_setopt (ctask->curl, CURLOPT_FAILONERROR, 1);
1589 curl_easy_setopt (ctask->curl, CURLOPT_CONNECTTIMEOUT, 600L);
1590 curl_easy_setopt (ctask->curl, CURLOPT_TIMEOUT, 600L);
1592 GNUNET_GNS_get_authority (gns_handle,
1594 &process_get_authority,
1596 ctask->ready_to_queue = GNUNET_NO;
1597 ctask->fin = GNUNET_NO;
1601 ctask = (struct ProxyCurlTask *) *con_cls;
1603 if (GNUNET_YES != ctask->ready_to_queue)
1604 return MHD_YES; /* wait longer */
1606 if (GNUNET_YES == ctask->fin)
1609 ctask->fin = GNUNET_YES;
1610 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1611 "MHD: Queueing response for %s\n", ctask->url);
1612 ret = MHD_queue_response (con, ctask->curl_response_code, ctask->response);
1613 run_mhd_now (ctask->mhd);
1624 struct MhdHttpList *hd;
1626 for (hd=mhd_httpd_head; hd != NULL; hd = hd->next)
1634 * @param hd the daemon to run
1637 run_httpd (struct MhdHttpList *hd)
1642 struct GNUNET_NETWORK_FDSet *wrs;
1643 struct GNUNET_NETWORK_FDSet *wws;
1644 struct GNUNET_NETWORK_FDSet *wes;
1647 unsigned MHD_LONG_LONG timeout;
1648 struct GNUNET_TIME_Relative tv;
1653 wrs = GNUNET_NETWORK_fdset_create ();
1654 wes = GNUNET_NETWORK_fdset_create ();
1655 wws = GNUNET_NETWORK_fdset_create ();
1657 GNUNET_assert (MHD_YES == MHD_get_fdset (hd->daemon, &rs, &ws, &es, &max));
1660 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1661 "MHD fds: max=%d\n", max);
1663 haveto = MHD_get_timeout (hd->daemon, &timeout);
1665 if (haveto == MHD_YES)
1666 tv.rel_value = (uint64_t) timeout;
1668 tv = GNUNET_TIME_UNIT_FOREVER_REL;
1669 GNUNET_NETWORK_fdset_copy_native (wrs, &rs, max + 1);
1670 GNUNET_NETWORK_fdset_copy_native (wws, &ws, max + 1);
1671 GNUNET_NETWORK_fdset_copy_native (wes, &es, max + 1);
1673 if (hd->httpd_task != GNUNET_SCHEDULER_NO_TASK)
1674 GNUNET_SCHEDULER_cancel (hd->httpd_task);
1676 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_HIGH,
1679 GNUNET_NETWORK_fdset_destroy (wrs);
1680 GNUNET_NETWORK_fdset_destroy (wws);
1681 GNUNET_NETWORK_fdset_destroy (wes);
1686 * Task run whenever HTTP server operations are pending.
1689 * @param tc sched context
1692 do_httpd (void *cls,
1693 const struct GNUNET_SCHEDULER_TaskContext *tc)
1695 struct MhdHttpList *hd = cls;
1697 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1698 "MHD: Main loop\n");
1699 hd->httpd_task = GNUNET_SCHEDULER_NO_TASK;
1700 MHD_run (hd->daemon);
1707 * Read data from socket
1709 * @param cls the closure
1710 * @param tc scheduler context
1713 do_read (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
1716 * Read from remote end
1718 * @param cls closure
1719 * @param tc scheduler context
1722 do_read_remote (void* cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
1725 * Write data to remote socket
1727 * @param cls the closure
1728 * @param tc scheduler context
1731 do_write_remote (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1733 struct Socks5Request *s5r = cls;
1736 s5r->fwdwtask = GNUNET_SCHEDULER_NO_TASK;
1738 if ((NULL != tc->read_ready) &&
1739 (GNUNET_NETWORK_fdset_isset (tc->write_ready, s5r->remote_sock)) &&
1740 ((len = GNUNET_NETWORK_socket_send (s5r->remote_sock, s5r->rbuf,
1743 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1744 "Successfully sent %d bytes to remote socket\n",
1749 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "write remote");
1751 if (s5r->rtask != GNUNET_SCHEDULER_NO_TASK)
1752 GNUNET_SCHEDULER_cancel (s5r->rtask);
1753 if (s5r->wtask != GNUNET_SCHEDULER_NO_TASK)
1754 GNUNET_SCHEDULER_cancel (s5r->wtask);
1755 if (s5r->fwdrtask != GNUNET_SCHEDULER_NO_TASK)
1756 GNUNET_SCHEDULER_cancel (s5r->fwdrtask);
1757 GNUNET_NETWORK_socket_close (s5r->remote_sock);
1758 GNUNET_NETWORK_socket_close (s5r->sock);
1764 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
1771 * Clean up s5r handles
1773 * @param s5r the handle to destroy
1776 cleanup_s5r (struct Socks5Request *s5r)
1778 if (s5r->rtask != GNUNET_SCHEDULER_NO_TASK)
1779 GNUNET_SCHEDULER_cancel (s5r->rtask);
1780 if (s5r->fwdwtask != GNUNET_SCHEDULER_NO_TASK)
1781 GNUNET_SCHEDULER_cancel (s5r->fwdwtask);
1782 if (s5r->fwdrtask != GNUNET_SCHEDULER_NO_TASK)
1783 GNUNET_SCHEDULER_cancel (s5r->fwdrtask);
1785 if (NULL != s5r->remote_sock)
1786 GNUNET_NETWORK_socket_close (s5r->remote_sock);
1787 if ((NULL != s5r->sock) && (s5r->cleanup_sock == GNUNET_YES))
1788 GNUNET_NETWORK_socket_close (s5r->sock);
1794 * Write data to socket
1796 * @param cls the closure
1797 * @param tc scheduler context
1800 do_write (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1802 struct Socks5Request *s5r = cls;
1805 s5r->wtask = GNUNET_SCHEDULER_NO_TASK;
1807 if ((NULL != tc->read_ready) &&
1808 (GNUNET_NETWORK_fdset_isset (tc->write_ready, s5r->sock)) &&
1809 ((len = GNUNET_NETWORK_socket_send (s5r->sock, s5r->wbuf,
1812 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1813 "Successfully sent %d bytes to socket\n",
1819 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "write");
1820 s5r->cleanup = GNUNET_YES;
1821 s5r->cleanup_sock = GNUNET_YES;
1827 if (GNUNET_YES == s5r->cleanup)
1833 if ((s5r->state == SOCKS5_DATA_TRANSFER) &&
1834 (s5r->fwdrtask == GNUNET_SCHEDULER_NO_TASK))
1836 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
1838 &do_read_remote, s5r);
1842 * Read from remote end
1844 * @param cls closure
1845 * @param tc scheduler context
1848 do_read_remote (void* cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1850 struct Socks5Request *s5r = cls;
1852 s5r->fwdrtask = GNUNET_SCHEDULER_NO_TASK;
1855 if ((NULL != tc->write_ready) &&
1856 (GNUNET_NETWORK_fdset_isset (tc->read_ready, s5r->remote_sock)) &&
1857 (s5r->wbuf_len = GNUNET_NETWORK_socket_recv (s5r->remote_sock, s5r->wbuf,
1858 sizeof (s5r->wbuf))))
1860 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1861 "Successfully read %d bytes from remote socket\n",
1866 if (s5r->wbuf_len == 0)
1867 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1868 "0 bytes received from remote... graceful shutdown!\n");
1869 if (s5r->fwdwtask != GNUNET_SCHEDULER_NO_TASK)
1870 GNUNET_SCHEDULER_cancel (s5r->fwdwtask);
1871 if (s5r->rtask != GNUNET_SCHEDULER_NO_TASK)
1872 GNUNET_SCHEDULER_cancel (s5r->rtask);
1874 GNUNET_NETWORK_socket_close (s5r->remote_sock);
1875 s5r->remote_sock = NULL;
1876 GNUNET_NETWORK_socket_close (s5r->sock);
1882 s5r->wtask = GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
1890 * Adds a socket to MHD
1892 * @param h the handle to the socket to add
1893 * @param daemon the daemon to add the fd to
1894 * @return whatever MHD_add_connection returns
1897 add_handle_to_mhd (struct GNUNET_NETWORK_Handle *h, struct MHD_Daemon *daemon)
1900 struct sockaddr *addr;
1903 fd = dup (GNUNET_NETWORK_get_fd (h));
1904 addr = GNUNET_NETWORK_get_addr (h);
1905 len = GNUNET_NETWORK_get_addrlen (h);
1907 return MHD_add_connection (daemon, fd, addr, len);
1911 * Read file in filename
1913 * @param filename file to read
1914 * @param size pointer where filesize is stored
1918 load_file (const char* filename,
1925 GNUNET_DISK_file_size (filename, &fsize,
1926 GNUNET_YES, GNUNET_YES))
1928 if (fsize > MAX_PEM_SIZE)
1930 *size = (unsigned int) fsize;
1931 buffer = GNUNET_malloc (*size);
1932 if (fsize != GNUNET_DISK_fn_read (filename, buffer, (size_t) fsize))
1934 GNUNET_free (buffer);
1942 * Load PEM key from file
1944 * @param key where to store the data
1945 * @param keyfile path to the PEM file
1946 * @return GNUNET_OK on success
1949 load_key_from_file (gnutls_x509_privkey_t key, const char* keyfile)
1951 gnutls_datum_t key_data;
1954 key_data.data = (unsigned char*) load_file (keyfile, &key_data.size);
1955 ret = gnutls_x509_privkey_import (key, &key_data,
1956 GNUTLS_X509_FMT_PEM);
1957 if (GNUTLS_E_SUCCESS != ret)
1959 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1960 _("Unable to import private key from file `%s'\n"),
1964 GNUNET_free (key_data.data);
1965 return (GNUTLS_E_SUCCESS != ret) ? GNUNET_SYSERR : GNUNET_OK;
1970 * Load cert from file
1972 * @param crt struct to store data in
1973 * @param certfile path to pem file
1974 * @return GNUNET_OK on success
1977 load_cert_from_file (gnutls_x509_crt_t crt, char* certfile)
1979 gnutls_datum_t cert_data;
1980 cert_data.data = NULL;
1983 cert_data.data = (unsigned char*) load_file (certfile, &cert_data.size);
1984 ret = gnutls_x509_crt_import (crt, &cert_data,
1985 GNUTLS_X509_FMT_PEM);
1986 if (GNUTLS_E_SUCCESS != ret)
1988 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1989 _("Unable to import certificate %s\n"), certfile);
1992 GNUNET_free (cert_data.data);
1993 return (GNUTLS_E_SUCCESS != ret) ? GNUNET_SYSERR : GNUNET_OK;
1998 * Generate new certificate for specific name
2000 * @param name the subject name to generate a cert for
2001 * @return a struct holding the PEM data
2003 static struct ProxyGNSCertificate *
2004 generate_gns_certificate (const char *name)
2008 unsigned int serial;
2009 size_t key_buf_size;
2010 size_t cert_buf_size;
2011 gnutls_x509_crt_t request;
2015 ret = gnutls_x509_crt_init (&request);
2017 if (GNUTLS_E_SUCCESS != ret)
2022 ret = gnutls_x509_crt_set_key (request, proxy_ca.key);
2024 if (GNUTLS_E_SUCCESS != ret)
2029 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Generating cert\n");
2031 struct ProxyGNSCertificate *pgc =
2032 GNUNET_malloc (sizeof (struct ProxyGNSCertificate));
2034 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Adding DNs\n");
2036 gnutls_x509_crt_set_dn_by_oid (request, GNUTLS_OID_X520_COUNTRY_NAME,
2039 gnutls_x509_crt_set_dn_by_oid (request, GNUTLS_OID_X520_ORGANIZATION_NAME,
2042 gnutls_x509_crt_set_dn_by_oid (request, GNUTLS_OID_X520_COMMON_NAME,
2043 0, name, strlen (name));
2045 ret = gnutls_x509_crt_set_version (request, 3);
2047 ret = gnutls_rnd (GNUTLS_RND_NONCE, &serial, sizeof (serial));
2049 etime = time (NULL);
2050 tm_data = localtime (&etime);
2053 ret = gnutls_x509_crt_set_serial (request,
2057 ret = gnutls_x509_crt_set_activation_time (request,
2060 etime = mktime (tm_data);
2067 ret = gnutls_x509_crt_set_expiration_time (request,
2069 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Signing...\n");
2071 ret = gnutls_x509_crt_sign (request, proxy_ca.cert, proxy_ca.key);
2073 key_buf_size = sizeof (pgc->key);
2074 cert_buf_size = sizeof (pgc->cert);
2076 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Exporting certificate...\n");
2078 gnutls_x509_crt_export (request, GNUTLS_X509_FMT_PEM,
2079 pgc->cert, &cert_buf_size);
2081 gnutls_x509_privkey_export (proxy_ca.key, GNUTLS_X509_FMT_PEM,
2082 pgc->key, &key_buf_size);
2085 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Cleaning up\n");
2086 gnutls_x509_crt_deinit (request);
2094 * Accept policy for mhdaemons
2097 * @param addr the sockaddr
2098 * @param addrlen the sockaddr length
2099 * @return MHD_NO if sockaddr is wrong or #conns too high
2102 accept_cb (void* cls, const struct sockaddr *addr, socklen_t addrlen)
2104 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2105 "In MHD accept policy cb\n");
2109 if (addr->sa_family == AF_UNIX)
2113 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2114 "Connection accepted\n");
2121 * Adds a socket to an SSL MHD instance
2122 * It is important the the domain name is
2123 * correct. In most cases we need to start a new daemon
2125 * @param h the handle to add to a daemon
2126 * @param domain the domain the ssl daemon has to serve
2127 * @return MHD_YES on success
2130 add_handle_to_ssl_mhd (struct GNUNET_NETWORK_Handle *h, const char* domain)
2132 struct MhdHttpList *hd = NULL;
2133 struct ProxyGNSCertificate *pgc;
2134 struct NetworkHandleList *nh;
2136 for (hd = mhd_httpd_head; NULL != hd; hd = hd->next)
2137 if (0 == strcmp (hd->domain, domain))
2142 pgc = generate_gns_certificate (domain);
2144 hd = GNUNET_malloc (sizeof (struct MhdHttpList));
2145 hd->is_ssl = GNUNET_YES;
2146 strcpy (hd->domain, domain);
2147 hd->proxy_cert = pgc;
2150 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2151 "No previous SSL instance found... starting new one for %s\n",
2154 hd->daemon = MHD_start_daemon (MHD_USE_DEBUG | MHD_USE_SSL, 4444,
2156 &create_response, hd,
2157 MHD_OPTION_LISTEN_SOCKET, GNUNET_NETWORK_get_fd (mhd_unix_socket),
2158 MHD_OPTION_CONNECTION_LIMIT, MHD_MAX_CONNECTIONS,
2159 MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) 16,
2160 MHD_OPTION_NOTIFY_COMPLETED,
2162 MHD_OPTION_HTTPS_MEM_KEY, pgc->key,
2163 MHD_OPTION_HTTPS_MEM_CERT, pgc->cert,
2166 GNUNET_assert (hd->daemon != NULL);
2167 hd->httpd_task = GNUNET_SCHEDULER_NO_TASK;
2169 GNUNET_CONTAINER_DLL_insert (mhd_httpd_head, mhd_httpd_tail, hd);
2172 nh = GNUNET_malloc (sizeof (struct NetworkHandleList));
2175 GNUNET_CONTAINER_DLL_insert (hd->socket_handles_head,
2176 hd->socket_handles_tail,
2179 return add_handle_to_mhd (h, hd->daemon);
2185 * Read data from incoming connection
2187 * @param cls the closure
2188 * @param tc the scheduler context
2191 do_read (void* cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2193 struct Socks5Request *s5r = cls;
2194 struct socks5_client_hello *c_hello;
2195 struct socks5_server_hello *s_hello;
2196 struct socks5_client_request *c_req;
2197 struct socks5_server_response *s_resp;
2203 struct hostent *phost;
2205 struct sockaddr_in remote_addr;
2206 struct in_addr *r_sin_addr;
2208 struct NetworkHandleList *nh;
2210 s5r->rtask = GNUNET_SCHEDULER_NO_TASK;
2212 if ((NULL != tc->write_ready) &&
2213 (GNUNET_NETWORK_fdset_isset (tc->read_ready, s5r->sock)) &&
2214 (s5r->rbuf_len = GNUNET_NETWORK_socket_recv (s5r->sock, s5r->rbuf,
2215 sizeof (s5r->rbuf))))
2217 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2218 "Successfully read %d bytes from socket\n",
2223 if (s5r->rbuf_len != 0)
2224 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "read");
2226 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client disco!\n");
2228 if (s5r->fwdrtask != GNUNET_SCHEDULER_NO_TASK)
2229 GNUNET_SCHEDULER_cancel (s5r->fwdrtask);
2230 if (s5r->wtask != GNUNET_SCHEDULER_NO_TASK)
2231 GNUNET_SCHEDULER_cancel (s5r->wtask);
2232 if (s5r->fwdwtask != GNUNET_SCHEDULER_NO_TASK)
2233 GNUNET_SCHEDULER_cancel (s5r->fwdwtask);
2234 GNUNET_NETWORK_socket_close (s5r->remote_sock);
2235 GNUNET_NETWORK_socket_close (s5r->sock);
2240 if (s5r->state == SOCKS5_INIT)
2242 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2244 c_hello = (struct socks5_client_hello*)&s5r->rbuf;
2246 GNUNET_assert (c_hello->version == SOCKS_VERSION_5);
2248 s_hello = (struct socks5_server_hello*)&s5r->wbuf;
2249 s5r->wbuf_len = sizeof( struct socks5_server_hello );
2251 s_hello->version = c_hello->version;
2252 s_hello->auth_method = SOCKS_AUTH_NONE;
2254 /* Write response to client */
2255 s5r->wtask = GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
2259 s5r->rtask = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
2263 s5r->state = SOCKS5_REQUEST;
2267 if (s5r->state == SOCKS5_REQUEST)
2269 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2270 "Processing SOCKS5 request\n");
2271 c_req = (struct socks5_client_request*)&s5r->rbuf;
2272 s_resp = (struct socks5_server_response*)&s5r->wbuf;
2273 //Only 10byte for ipv4 response!
2274 s5r->wbuf_len = 10;//sizeof (struct socks5_server_response);
2276 GNUNET_assert (c_req->addr_type == 3);
2278 dom_len = *((uint8_t*)(&(c_req->addr_type) + 1));
2279 memset(domain, 0, sizeof(domain));
2280 strncpy(domain, (char*)(&(c_req->addr_type) + 2), dom_len);
2281 req_port = *((uint16_t*)(&(c_req->addr_type) + 2 + dom_len));
2283 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2284 "Requested connection is %s:%d\n",
2288 if (is_tld (domain, GNUNET_GNS_TLD) ||
2289 is_tld (domain, GNUNET_GNS_TLD_ZKEY))
2291 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2292 "Requested connection is gnunet tld\n",
2296 if (ntohs(req_port) == HTTPS_PORT)
2298 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2299 "Requested connection is HTTPS\n");
2300 ret = add_handle_to_ssl_mhd ( s5r->sock, domain );
2302 else if (NULL != httpd)
2304 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2305 "Requested connection is HTTP\n");
2306 nh = GNUNET_malloc (sizeof (struct NetworkHandleList));
2309 GNUNET_CONTAINER_DLL_insert (mhd_httpd_head->socket_handles_head,
2310 mhd_httpd_head->socket_handles_tail,
2313 ret = add_handle_to_mhd ( s5r->sock, httpd );
2318 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2319 _("Failed to start HTTP server\n"));
2320 s_resp->version = 0x05;
2321 s_resp->reply = 0x01;
2322 s5r->cleanup = GNUNET_YES;
2323 s5r->cleanup_sock = GNUNET_YES;
2325 GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
2331 /* Signal success */
2332 s_resp->version = 0x05;
2333 s_resp->reply = 0x00;
2334 s_resp->reserved = 0x00;
2335 s_resp->addr_type = 0x01;
2337 s5r->cleanup = GNUNET_YES;
2338 s5r->cleanup_sock = GNUNET_NO;
2340 GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
2348 phost = (struct hostent*)gethostbyname (domain);
2351 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2352 "Resolve %s error!\n", domain );
2353 s_resp->version = 0x05;
2354 s_resp->reply = 0x01;
2355 s5r->cleanup = GNUNET_YES;
2356 s5r->cleanup_sock = GNUNET_YES;
2358 GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
2364 s5r->remote_sock = GNUNET_NETWORK_socket_create (AF_INET,
2367 r_sin_addr = (struct in_addr*)(phost->h_addr);
2368 remote_ip = r_sin_addr->s_addr;
2369 memset(&remote_addr, 0, sizeof(remote_addr));
2370 remote_addr.sin_family = AF_INET;
2371 #if HAVE_SOCKADDR_IN_SIN_LEN
2372 remote_addr.sin_len = sizeof (remote_addr);
2374 remote_addr.sin_addr.s_addr = remote_ip;
2375 remote_addr.sin_port = req_port;
2377 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2378 "target server: %s:%u\n", inet_ntoa(remote_addr.sin_addr),
2382 GNUNET_NETWORK_socket_connect ( s5r->remote_sock,
2383 (const struct sockaddr*)&remote_addr,
2384 sizeof (remote_addr)))
2385 && (errno != EINPROGRESS))
2387 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "connect");
2388 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2389 "socket request error...\n");
2390 s_resp->version = 0x05;
2391 s_resp->reply = 0x01;
2393 GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
2400 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2401 "new remote connection\n");
2403 s_resp->version = 0x05;
2404 s_resp->reply = 0x00;
2405 s_resp->reserved = 0x00;
2406 s_resp->addr_type = 0x01;
2408 s5r->state = SOCKS5_DATA_TRANSFER;
2411 GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
2415 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
2423 if (s5r->state == SOCKS5_DATA_TRANSFER)
2425 if ((s5r->remote_sock == NULL) || (s5r->rbuf_len == 0))
2427 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2428 "Closing connection to client\n");
2429 if (s5r->rtask != GNUNET_SCHEDULER_NO_TASK)
2430 GNUNET_SCHEDULER_cancel (s5r->rtask);
2431 if (s5r->fwdwtask != GNUNET_SCHEDULER_NO_TASK)
2432 GNUNET_SCHEDULER_cancel (s5r->fwdwtask);
2433 if (s5r->fwdrtask != GNUNET_SCHEDULER_NO_TASK)
2434 GNUNET_SCHEDULER_cancel (s5r->fwdrtask);
2435 if (s5r->fwdrtask != GNUNET_SCHEDULER_NO_TASK)
2436 GNUNET_SCHEDULER_cancel (s5r->fwdrtask);
2438 if (s5r->remote_sock != NULL)
2439 GNUNET_NETWORK_socket_close (s5r->remote_sock);
2440 GNUNET_NETWORK_socket_close (s5r->sock);
2445 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2446 "forwarding %d bytes from client\n", s5r->rbuf_len);
2449 GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
2451 &do_write_remote, s5r);
2453 if (s5r->fwdrtask == GNUNET_SCHEDULER_NO_TASK)
2456 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
2458 &do_read_remote, s5r);
2464 //GNUNET_CONTAINER_DLL_remove (s5conns.head, s5conns.tail, s5r);
2470 * Accept new incoming connections
2472 * @param cls the closure
2473 * @param tc the scheduler context
2476 do_accept (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2478 struct GNUNET_NETWORK_Handle *s;
2479 struct Socks5Request *s5r;
2481 ltask = GNUNET_SCHEDULER_NO_TASK;
2482 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
2485 ltask = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
2489 s = GNUNET_NETWORK_socket_accept (lsock, NULL, NULL);
2493 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "accept");
2497 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2498 "Got an inbound connection, waiting for data\n");
2500 s5r = GNUNET_malloc (sizeof (struct Socks5Request));
2502 s5r->state = SOCKS5_INIT;
2503 s5r->wtask = GNUNET_SCHEDULER_NO_TASK;
2504 s5r->fwdwtask = GNUNET_SCHEDULER_NO_TASK;
2505 s5r->fwdrtask = GNUNET_SCHEDULER_NO_TASK;
2506 s5r->rtask = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
2509 //GNUNET_CONTAINER_DLL_insert (s5conns.head, s5conns.tail, s5r);
2514 * Task run on shutdown
2516 * @param cls closure
2517 * @param tc task context
2520 do_shutdown (void *cls,
2521 const struct GNUNET_SCHEDULER_TaskContext *tc)
2524 struct MhdHttpList *hd;
2525 struct MhdHttpList *tmp_hd;
2526 struct NetworkHandleList *nh;
2527 struct NetworkHandleList *tmp_nh;
2528 struct ProxyCurlTask *ctask;
2529 struct ProxyCurlTask *ctask_tmp;
2531 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2532 "Shutting down...\n");
2534 gnutls_global_deinit ();
2536 if (GNUNET_SCHEDULER_NO_TASK != curl_download_task)
2538 GNUNET_SCHEDULER_cancel (curl_download_task);
2539 curl_download_task = GNUNET_SCHEDULER_NO_TASK;
2542 for (hd = mhd_httpd_head; hd != NULL; hd = tmp_hd)
2546 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2547 "Stopping daemon\n");
2549 if (GNUNET_SCHEDULER_NO_TASK != hd->httpd_task)
2551 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2552 "Stopping select task %d\n",
2554 GNUNET_SCHEDULER_cancel (hd->httpd_task);
2555 hd->httpd_task = GNUNET_SCHEDULER_NO_TASK;
2558 if (NULL != hd->daemon)
2560 MHD_stop_daemon (hd->daemon);
2564 for (nh = hd->socket_handles_head; nh != NULL; nh = tmp_nh)
2568 GNUNET_NETWORK_socket_close (nh->h);
2573 if (NULL != hd->proxy_cert)
2575 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2576 "Free certificate\n");
2577 GNUNET_free (hd->proxy_cert);
2583 for (ctask=ctasks_head; ctask != NULL; ctask=ctask_tmp)
2585 ctask_tmp = ctask->next;
2587 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2588 "Cleaning up cURL task\n");
2590 if (ctask->curl != NULL)
2591 curl_easy_cleanup (ctask->curl);
2593 if (NULL != ctask->headers)
2594 curl_slist_free_all (ctask->headers);
2595 if (NULL != ctask->resolver)
2596 curl_slist_free_all (ctask->resolver);
2598 if (NULL != ctask->response)
2599 MHD_destroy_response (ctask->response);
2602 GNUNET_free (ctask);
2605 GNUNET_GNS_disconnect (gns_handle);
2610 * Compiles a regex for us
2612 * @param re ptr to re struct
2613 * @param rt the expression to compile
2614 * @return 0 on success
2617 compile_regex (regex_t *re, const char* rt)
2622 status = regcomp (re, rt, REG_EXTENDED|REG_NEWLINE);
2625 regerror (status, re, err, 1024);
2626 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2627 "Regex error compiling '%s': %s\n", rt, err);
2635 * Loads the users local zone key
2637 * @return GNUNET_YES on success
2640 load_local_zone_key (const struct GNUNET_CONFIGURATION_Handle *cfg)
2643 struct GNUNET_CRYPTO_RsaPrivateKey *key = NULL;
2644 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pkey;
2645 struct GNUNET_CRYPTO_ShortHashCode *zone = NULL;
2646 struct GNUNET_CRYPTO_ShortHashAsciiEncoded zonename;
2648 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "gns",
2649 "ZONEKEY", &keyfile))
2651 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2652 "Unable to load zone key config value!\n");
2656 if (GNUNET_NO == GNUNET_DISK_file_test (keyfile))
2658 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2659 "Unable to load zone key %s!\n", keyfile);
2660 GNUNET_free(keyfile);
2664 key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
2665 GNUNET_CRYPTO_rsa_key_get_public (key, &pkey);
2666 GNUNET_CRYPTO_short_hash(&pkey,
2667 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
2669 zone = &local_gns_zone;
2670 GNUNET_CRYPTO_short_hash_to_enc (zone, &zonename);
2671 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2672 "Using zone: %s!\n", &zonename);
2673 GNUNET_CRYPTO_rsa_key_free(key);
2674 GNUNET_free(keyfile);
2676 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "gns",
2677 "PRIVATE_ZONEKEY", &keyfile))
2679 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2680 "Unable to load private zone key config value!\n");
2684 if (GNUNET_NO == GNUNET_DISK_file_test (keyfile))
2686 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2687 "Unable to load private zone key %s!\n", keyfile);
2688 GNUNET_free(keyfile);
2692 key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
2693 GNUNET_CRYPTO_rsa_key_get_public (key, &pkey);
2694 GNUNET_CRYPTO_short_hash(&pkey,
2695 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
2696 &local_private_zone);
2697 GNUNET_CRYPTO_short_hash_to_enc (zone, &zonename);
2698 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2699 "Using private zone: %s!\n", &zonename);
2700 GNUNET_CRYPTO_rsa_key_free(key);
2701 GNUNET_free(keyfile);
2703 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "gns",
2704 "SHORTEN_ZONEKEY", &keyfile))
2706 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2707 "Unable to load shorten zone key config value!\n");
2711 if (GNUNET_NO == GNUNET_DISK_file_test (keyfile))
2713 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2714 "Unable to load shorten zone key %s!\n", keyfile);
2715 GNUNET_free(keyfile);
2719 key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
2720 GNUNET_CRYPTO_rsa_key_get_public (key, &pkey);
2721 GNUNET_CRYPTO_short_hash(&pkey,
2722 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
2723 &local_shorten_zone);
2724 GNUNET_CRYPTO_short_hash_to_enc (zone, &zonename);
2725 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2726 "Using shorten zone: %s!\n", &zonename);
2727 GNUNET_CRYPTO_rsa_key_free(key);
2728 GNUNET_free(keyfile);
2734 * Main function that will be run
2736 * @param cls closure
2737 * @param args remaining command-line arguments
2738 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
2739 * @param cfg configuration
2742 run (void *cls, char *const *args, const char *cfgfile,
2743 const struct GNUNET_CONFIGURATION_Handle *cfg)
2745 struct sockaddr_in sa;
2746 struct MhdHttpList *hd;
2747 struct sockaddr_un mhd_unix_sock_addr;
2749 char* proxy_sockfile;
2750 char* cafile_cfg = NULL;
2755 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2758 cafile = cafile_opt;
2762 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "gns-proxy",
2766 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2767 "Unable to load proxy CA config value!\n");
2768 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2769 "No proxy CA provided!\n");
2772 cafile = cafile_cfg;
2775 gnutls_global_init ();
2777 gnutls_x509_crt_init (&proxy_ca.cert);
2778 gnutls_x509_privkey_init (&proxy_ca.key);
2780 if ( (GNUNET_OK != load_cert_from_file (proxy_ca.cert, cafile)) ||
2781 (GNUNET_OK != load_key_from_file (proxy_ca.key, cafile)) )
2783 // FIXME: release resources...
2787 GNUNET_free_non_null (cafile_cfg);
2789 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2790 "Loading Template\n");
2792 compile_regex (&re_dotplus, (char*) RE_A_HREF);
2794 gns_handle = GNUNET_GNS_connect (cfg);
2796 if (GNUNET_NO == load_local_zone_key (cfg))
2798 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2799 "Unable to load zone!\n");
2803 if (NULL == gns_handle)
2805 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2806 "Unable to connect to GNS!\n");
2810 memset (&sa, 0, sizeof (sa));
2811 sa.sin_family = AF_INET;
2812 sa.sin_port = htons (port);
2813 #if HAVE_SOCKADDR_IN_SIN_LEN
2814 sa.sin_len = sizeof (sa);
2817 lsock = GNUNET_NETWORK_socket_create (AF_INET,
2821 if ((NULL == lsock) ||
2823 GNUNET_NETWORK_socket_bind (lsock, (const struct sockaddr *) &sa,
2826 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2827 "Failed to create listen socket bound to `%s'",
2828 GNUNET_a2s ((const struct sockaddr *) &sa, sizeof (sa)));
2830 GNUNET_NETWORK_socket_close (lsock);
2834 if (GNUNET_OK != GNUNET_NETWORK_socket_listen (lsock, 5))
2836 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2837 "Failed to listen on socket bound to `%s'",
2838 GNUNET_a2s ((const struct sockaddr *) &sa, sizeof (sa)));
2842 ltask = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
2843 lsock, &do_accept, NULL);
2848 if (0 != curl_global_init (CURL_GLOBAL_WIN32))
2850 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2851 "cURL global init failed!\n");
2855 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2856 "Proxy listens on port %u\n",
2859 mhd_httpd_head = NULL;
2860 mhd_httpd_tail = NULL;
2861 total_mhd_connections = 0;
2863 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "gns-proxy",
2867 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2868 "Specify PROXY_UNIXPATH in gns-proxy config section!\n");
2872 mhd_unix_socket = GNUNET_NETWORK_socket_create (AF_UNIX,
2876 if (NULL == mhd_unix_socket)
2878 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2879 "Unable to create unix domain socket!\n");
2883 mhd_unix_sock_addr.sun_family = AF_UNIX;
2884 strcpy (mhd_unix_sock_addr.sun_path, proxy_sockfile);
2887 mhd_unix_sock_addr.sun_path[0] = '\0';
2889 #if HAVE_SOCKADDR_IN_SIN_LEN
2890 mhd_unix_sock_addr.sun_len = (u_char) sizeof (struct sockaddr_un);
2893 len = strlen (proxy_sockfile) + sizeof(AF_UNIX);
2895 GNUNET_free (proxy_sockfile);
2897 if (GNUNET_OK != GNUNET_NETWORK_socket_bind (mhd_unix_socket,
2898 (struct sockaddr*)&mhd_unix_sock_addr,
2901 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2902 "Unable to bind unix domain socket!\n");
2906 if (GNUNET_OK != GNUNET_NETWORK_socket_listen (mhd_unix_socket,
2909 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2910 "Unable to listen on unix domain socket!\n");
2914 hd = GNUNET_malloc (sizeof (struct MhdHttpList));
2915 hd->is_ssl = GNUNET_NO;
2916 strcpy (hd->domain, "");
2917 httpd = MHD_start_daemon (MHD_USE_DEBUG, 4444, //Dummy port
2919 &create_response, hd,
2920 MHD_OPTION_LISTEN_SOCKET, GNUNET_NETWORK_get_fd (mhd_unix_socket),
2921 MHD_OPTION_CONNECTION_LIMIT, MHD_MAX_CONNECTIONS,
2922 MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) 16,
2923 MHD_OPTION_NOTIFY_COMPLETED,
2927 GNUNET_assert (httpd != NULL);
2929 hd->httpd_task = GNUNET_SCHEDULER_NO_TASK;
2931 GNUNET_CONTAINER_DLL_insert (mhd_httpd_head, mhd_httpd_tail, hd);
2935 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
2936 &do_shutdown, NULL);
2942 * The main function for gnunet-gns-proxy.
2944 * @param argc number of arguments from the command line
2945 * @param argv command line arguments
2946 * @return 0 ok, 1 on error
2949 main (int argc, char *const *argv)
2951 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
2953 gettext_noop ("listen on specified port"), 1,
2954 &GNUNET_GETOPT_set_string, &port},
2955 {'a', "authority", NULL,
2956 gettext_noop ("pem file to use as CA"), 1,
2957 &GNUNET_GETOPT_set_string, &cafile_opt},
2958 GNUNET_GETOPT_OPTION_END
2963 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
2967 GNUNET_log_setup ("gnunet-gns-proxy", "WARNING", NULL);
2970 GNUNET_PROGRAM_run (argc, argv, "gnunet-gns-proxy",
2971 _("GNUnet GNS proxy"),
2973 &run, NULL)) ? 0 : 1;
2974 GNUNET_free_non_null ((char*)argv);