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 HTML_HDR_CONTENT "Content-Type: text/html"
46 //#define RE_DOTPLUS "<a href=\"http://(([A-Za-z]+[.])+)([+])"
47 #define RE_A_HREF "href=\"https?://(([A-Za-z0-9]+[.])+)([+]|[a-z]+)"
48 #define RE_N_MATCHES 4
50 /* The usual suspects */
52 #define HTTPS_PORT 443
56 * A structure for CA cert/key
61 gnutls_x509_crt_t cert;
64 gnutls_x509_privkey_t key;
67 #define MAX_PEM_SIZE (10 * 1024)
70 * Structure for GNS certificates
72 struct ProxyGNSCertificate
74 /* The certificate as PEM */
75 char cert[MAX_PEM_SIZE];
77 /* The private key as PEM */
78 char key[MAX_PEM_SIZE];
83 * A structure for socks requests
87 /* The client socket */
88 struct GNUNET_NETWORK_Handle *sock;
90 /* The server socket */
91 struct GNUNET_NETWORK_Handle *remote_sock;
96 /* Client socket read task */
97 GNUNET_SCHEDULER_TaskIdentifier rtask;
99 /* Server socket read task */
100 GNUNET_SCHEDULER_TaskIdentifier fwdrtask;
102 /* Client socket write task */
103 GNUNET_SCHEDULER_TaskIdentifier wtask;
105 /* Server socket write task */
106 GNUNET_SCHEDULER_TaskIdentifier fwdwtask;
114 /* Length of data in read buffer */
115 unsigned int rbuf_len;
117 /* Length of data in write buffer */
118 unsigned int wbuf_len;
120 /* This handle is scheduled for cleanup? */
123 /* Shall we close the client socket on cleanup? */
128 * DLL for Network Handles
130 struct NetworkHandleList
133 struct NetworkHandleList *next;
136 struct NetworkHandleList *prev;
139 struct GNUNET_NETWORK_Handle *h;
143 * A structure for all running Httpds
148 struct MhdHttpList *prev;
151 struct MhdHttpList *next;
153 /* is this an ssl daemon? */
156 /* the domain name to server (only important for SSL) */
159 /* The daemon handle */
160 struct MHD_Daemon *daemon;
162 /* Optional proxy certificate used */
163 struct ProxyGNSCertificate *proxy_cert;
166 GNUNET_SCHEDULER_TaskIdentifier httpd_task;
168 /* Handles associated with this daemon */
169 struct NetworkHandleList *socket_handles_head;
171 /* Handles associated with this daemon */
172 struct NetworkHandleList *socket_handles_tail;
176 * A structure for MHD<->cURL streams
181 struct ProxyCurlTask *prev;
184 struct ProxyCurlTask *next;
189 /* Optional header replacements for curl (LEHO) */
190 struct curl_slist *headers;
192 /* Optional resolver replacements for curl (LEHO) */
193 struct curl_slist *resolver;
195 /* curl response code */
196 long curl_response_code;
198 /* The URL to fetch */
201 /* The cURL write buffer / MHD read buffer */
202 char buffer[CURL_MAX_WRITE_SIZE];
204 /* The pointer to the data in the buffer */
207 /* The buffer status (BUF_WAIT_FOR_CURL or BUF_WAIT_FOR_MHD) */
210 /* Number of bytes in buffer */
211 unsigned int bytes_in_buffer;
213 /* Indicates wheather the download is in progress */
214 int download_in_progress;
216 /* Indicates wheather the download was successful */
217 int download_successful;
219 /* Indicates wheather the download failed */
222 /* Indicates wheather we need to parse HTML */
225 /* Indicates wheather we are postprocessing the HTML right now */
226 int is_postprocessing;
228 /* Indicates wheather postprocessing has finished */
231 /* Task ID of the postprocessing task */
232 GNUNET_SCHEDULER_TaskIdentifier pp_task;
234 /* The postprocessing buffer TODO length? */
237 /* The authority of the corresponding host (site of origin) */
240 /* The hostname (Host header field) */
243 /* The LEgacy HOstname (can be empty) */
246 /* The associated daemon list entry */
247 struct MhdHttpList *mhd;
249 /* The associated response */
250 struct MHD_Response *response;
253 struct ProxySetCookieHeader *set_cookies_head;
256 struct ProxySetCookieHeader *set_cookies_tail;
258 /* connection status */
265 struct MHD_Connection *connection;
270 * Struct for set-cookies
272 struct ProxySetCookieHeader
275 struct ProxySetCookieHeader *next;
278 struct ProxySetCookieHeader *prev;
284 /* The port the proxy is running on (default 7777) */
285 static unsigned long port = GNUNET_GNS_PROXY_PORT;
287 /* The CA file (pem) to use for the proxy CA */
288 static char* cafile_opt;
290 /* The listen socket of the proxy */
291 static struct GNUNET_NETWORK_Handle *lsock;
293 /* The listen task ID */
294 GNUNET_SCHEDULER_TaskIdentifier ltask;
296 /* The cURL download task */
297 GNUNET_SCHEDULER_TaskIdentifier curl_download_task;
299 /* The non SSL httpd daemon handle */
300 static struct MHD_Daemon *httpd;
302 /* Number of current mhd connections */
303 static unsigned int total_mhd_connections;
305 /* The cURL multi handle */
306 static CURLM *curl_multi;
308 /* Handle to the GNS service */
309 static struct GNUNET_GNS_Handle *gns_handle;
311 /* DLL for ProxyCurlTasks */
312 static struct ProxyCurlTask *ctasks_head;
314 /* DLL for ProxyCurlTasks */
315 static struct ProxyCurlTask *ctasks_tail;
317 /* DLL for http daemons */
318 static struct MhdHttpList *mhd_httpd_head;
320 /* DLL for http daemons */
321 static struct MhdHttpList *mhd_httpd_tail;
323 /* Handle to the regex for dotplus (.+) replacement in HTML */
324 static regex_t re_dotplus;
326 /* The users local GNS zone hash */
327 static struct GNUNET_CRYPTO_ShortHashCode local_gns_zone;
329 /* The users local private zone */
330 static struct GNUNET_CRYPTO_ShortHashCode local_private_zone;
332 /* The users local shorten zone */
333 static struct GNUNET_CRYPTO_ShortHashCode local_shorten_zone;
335 /* The CA for SSL certificate generation */
336 static struct ProxyCA proxy_ca;
338 /* UNIX domain socket for mhd */
339 struct GNUNET_NETWORK_Handle *mhd_unix_socket;
341 /* Shorten zone private key */
342 struct GNUNET_CRYPTO_RsaPrivateKey *shorten_zonekey;
345 * Checks if name is in tld
347 * @param name the name to check
348 * @param tld the TLD to check for
349 * @return GNUNET_YES or GNUNET_NO
352 is_tld(const char* name, const char* tld)
356 if (strlen(name) <= strlen(tld))
359 offset = strlen(name) - strlen(tld);
360 if (0 != strcmp (name+offset, tld))
362 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
363 "%s is not in .%s TLD\n", name, tld);
371 get_uri_val_iter (void *cls,
372 enum MHD_ValueKind kind,
378 sprintf (buf+strlen (buf), "?%s=%s", key, value);
384 * Read HTTP request header field 'Host'
386 * @param cls buffer to write to
387 * @param kind value kind
388 * @param key field key
389 * @param value field value
390 * @return MHD_NO when Host found
393 con_val_iter (void *cls,
394 enum MHD_ValueKind kind,
398 struct ProxyCurlTask *ctask = cls;
399 char* buf = ctask->host;
403 if (0 == strcmp ("Host", key))
409 if (0 == strcmp ("Accept-Encoding", key))
414 cstr = GNUNET_malloc (strlen (key) + strlen (hdr_val) + 3);
415 GNUNET_snprintf (cstr, strlen (key) + strlen (hdr_val) + 3,
416 "%s: %s", key, hdr_val);
418 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
419 "Client Header: %s\n", cstr);
421 ctask->headers = curl_slist_append (ctask->headers, cstr);
429 * Check HTTP response header for mime
431 * @param buffer curl buffer
432 * @param size curl blocksize
433 * @param nmemb curl blocknumber
435 * @return size of read bytes
438 curl_check_hdr (void *buffer, size_t size, size_t nmemb, void *cls)
440 size_t bytes = size * nmemb;
441 struct ProxyCurlTask *ctask = cls;
442 int html_mime_len = strlen (HTML_HDR_CONTENT);
443 int cookie_hdr_len = strlen (MHD_HTTP_HEADER_SET_COOKIE);
444 char hdr_mime[html_mime_len+1];
445 char hdr_generic[bytes+1];
446 char new_cookie_hdr[bytes+strlen (ctask->leho)+1];
455 if (html_mime_len <= bytes)
457 memcpy (hdr_mime, buffer, html_mime_len);
458 hdr_mime[html_mime_len] = '\0';
460 if (0 == strcmp (hdr_mime, HTML_HDR_CONTENT))
462 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
463 "Got HTML HTTP response header\n");
464 ctask->parse_content = GNUNET_YES;
468 if (cookie_hdr_len > bytes)
471 memcpy (hdr_generic, buffer, bytes);
472 hdr_generic[bytes] = '\0';
474 if ('\n' == hdr_generic[bytes-1])
475 hdr_generic[bytes-1] = '\0';
477 if (hdr_generic[bytes-2] == '\r')
478 hdr_generic[bytes-2] = '\0';
480 if (0 == memcmp (hdr_generic,
481 MHD_HTTP_HEADER_SET_COOKIE,
484 ndup = GNUNET_strdup (hdr_generic+cookie_hdr_len+1);
485 memset (new_cookie_hdr, 0, sizeof (new_cookie_hdr));
486 tok = strtok (ndup, ";");
488 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
489 "Looking for cookie in : %s\n", hdr_generic);
491 for (; tok != NULL; tok = strtok (NULL, ";"))
493 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
494 "Got Cookie token: %s\n", tok);
495 //memcpy (new_cookie_hdr+offset, tok, strlen (tok));
496 if (0 == memcmp (tok, " domain", strlen (" domain")))
498 cookie_domain = tok + strlen (" domain") + 1;
500 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
501 "Got Set-Cookie Domain: %s\n", cookie_domain);
503 if (strlen (cookie_domain) < strlen (ctask->leho))
505 delta_cdomain = strlen (ctask->leho) - strlen (cookie_domain);
506 if (0 == strcmp (cookie_domain, ctask->leho + (delta_cdomain)))
508 GNUNET_snprintf (new_cookie_hdr+offset,
509 sizeof (new_cookie_hdr),
510 " domain=%s", ctask->authority);
511 offset += strlen (" domain=") + strlen (ctask->authority);
512 new_cookie_hdr[offset] = ';';
517 else if (strlen (cookie_domain) == strlen (ctask->leho))
519 if (0 == strcmp (cookie_domain, ctask->leho))
521 GNUNET_snprintf (new_cookie_hdr+offset,
522 sizeof (new_cookie_hdr),
523 " domain=%s", ctask->host);
524 offset += strlen (" domain=") + strlen (ctask->host);
525 new_cookie_hdr[offset] = ';';
530 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
531 "Cookie domain invalid\n");
535 memcpy (new_cookie_hdr+offset, tok, strlen (tok));
536 offset += strlen (tok);
537 new_cookie_hdr[offset] = ';';
541 //memcpy (new_cookie_hdr+offset, tok, strlen (tok));
545 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
546 "Got Set-Cookie HTTP header %s\n", new_cookie_hdr);
548 //pch = GNUNET_malloc (sizeof (struct ProxySetCookieHeader));
549 //len = strlen (hdr_cookie) - cookie_hdr_len - 1;
550 //pch->cookie = GNUNET_malloc (len + 1);
551 //memset (pch->cookie, 0, len + 1);
552 //memcpy (pch->cookie, hdr_cookie+cookie_hdr_len+1, len);
553 //GNUNET_CONTAINER_DLL_insert (ctask->set_cookies_head,
554 // ctask->set_cookies_tail,
556 //pch = ctask->set_cookies_head;
557 //while (pch != NULL)
559 if (GNUNET_NO == MHD_add_response_header (ctask->response,
560 MHD_HTTP_HEADER_SET_COOKIE,
563 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
564 "MHD: Error adding set-cookie header field %s\n",
565 hdr_generic+cookie_hdr_len+1);
568 //GNUNET_free (pch->cookie);
569 //GNUNET_CONTAINER_DLL_remove (ctask->set_cookies_head,
570 // ctask->set_cookies_tail,
573 //pch = ctask->set_cookies_head;
577 ndup = GNUNET_strdup (hdr_generic);
578 hdr_type = strtok (ndup, ":");
580 if (hdr_type != NULL)
582 hdr_val = strtok (NULL, "");
585 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
586 "Trying to set %s: %s\n",
589 if (GNUNET_NO == MHD_add_response_header (ctask->response,
593 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
594 "MHD: Error adding %s header field %s\n",
608 * @param hd a http daemon list entry
611 run_httpd (struct MhdHttpList *hd);
623 * Task that simply runs MHD main loop
625 * @param cls NULL --- FIXME: interesting...
626 * @param tc task context
629 run_mhd (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
631 struct MhdHttpList *hd = cls;
633 //for (hd=mhd_httpd_head; hd != NULL; hd = hd->next)
634 MHD_run (hd->daemon);
639 * Process cURL download bits
641 * @param ptr buffer with data
642 * @param size size of a record
643 * @param nmemb number of records downloaded
645 * @return number of processed bytes
648 callback_download (void *ptr, size_t size, size_t nmemb, void *ctx)
650 const char *cbuf = ptr;
652 struct ProxyCurlTask *ctask = ctx;
654 if (NULL == ctask->response)
656 /* FIXME: get total size from curl (if available) */
657 ctask->response = MHD_create_response_from_callback (MHD_SIZE_UNKNOWN,
658 20 /* 20 bytes IO buffers!? */,
666 ctask->ready_to_queue = GNUNET_YES;
667 /*if (ctask->con_status == MHD_NO)
669 MHD_queue_response (ctask->connection,
672 ctask->con_status = MHD_YES;
681 if (total > sizeof (ctask->buffer))
683 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
684 "CURL gave us too much data to handle (%d)!\n",
689 if (ctask->buf_status == BUF_WAIT_FOR_MHD)
691 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
692 "CURL: Waiting for MHD (%s)\n", ctask->url);
693 return CURL_WRITEFUNC_PAUSE;
697 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
698 "CURL: Copying to MHD (%s, %d)\n", ctask->url, total);
699 memcpy (ctask->buffer, cbuf, total);
700 ctask->bytes_in_buffer = total;
701 ctask->buffer_ptr = ctask->buffer;
703 ctask->buf_status = BUF_WAIT_FOR_MHD;
705 //GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
706 // "cURL chunk:\n%s\n", (char*)ctask->buffer);
707 run_mhd_now (ctask->mhd);
712 * Ask cURL for the select sets and schedule download
715 curl_download_prepare ();
718 * Callback to free content
720 * @param cls content to free
721 * @param tc task context
724 mhd_content_free (struct ProxyCurlTask *ctask)
726 if (NULL != ctask->headers)
727 curl_slist_free_all (ctask->headers);
729 if (NULL != ctask->headers)
730 curl_slist_free_all (ctask->resolver);
732 if (NULL != ctask->response)
733 MHD_destroy_response (ctask->response);
740 * Shorten result callback
742 * @param cls the proxycurltask
743 * @param short_name the shortened name (NULL on error)
746 process_shorten (void* cls, const char* short_name)
748 struct ProxyCurlTask *ctask = cls;
750 char tmp[strlen(ctask->pp_buf)]; //TODO length
752 if (NULL == short_name)
754 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
755 "MHD PP: Unable to shorten %s\n",
760 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
761 "MHD PP: Shorten %s -> %s\n",
764 //this is evil.. what about https
765 sprintf (tmp, "href=\"http://%s", short_name);
766 strcpy (ctask->pp_buf, tmp);
768 ctask->pp_finished = GNUNET_YES;
770 GNUNET_SCHEDULER_add_now (&run_mhd, ctask->mhd);
775 * Callback for MHD response
778 * @param pos in buffer
780 * @param max space in buffer
781 * @return number of bytes written
784 mhd_content_cb (void *cls,
789 struct ProxyCurlTask *ctask = cls;
791 size_t bytes_to_copy;
794 regmatch_t m[RE_N_MATCHES];
796 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
797 "MHD: content cb %s\n", ctask->url);
799 if (ctask->download_successful &&
800 (ctask->buf_status == BUF_WAIT_FOR_CURL))
802 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
803 "MHD: sending response for %s\n", ctask->url);
804 ctask->download_in_progress = GNUNET_NO;
805 run_mhd_now (ctask->mhd);
806 GNUNET_SCHEDULER_add_now (&run_mhd, ctask->mhd);
807 mhd_content_free (ctask);
808 total_mhd_connections--;
809 return MHD_CONTENT_READER_END_OF_STREAM;
812 if (ctask->download_error &&
813 (ctask->buf_status == BUF_WAIT_FOR_CURL))
815 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
816 "MHD: sending error response\n");
817 ctask->download_in_progress = GNUNET_NO;
818 run_mhd_now (ctask->mhd);
819 mhd_content_free (ctask);
820 total_mhd_connections--;
821 return MHD_CONTENT_READER_END_WITH_ERROR;
824 if ( ctask->buf_status == BUF_WAIT_FOR_CURL )
827 bytes_to_copy = ctask->bytes_in_buffer;
829 if (ctask->parse_content == GNUNET_YES)
832 GNUNET_log ( GNUNET_ERROR_TYPE_DEBUG,
833 "MHD: We need to parse the HTML %s\n", ctask->buffer_ptr);
835 nomatch = regexec ( &re_dotplus,
836 ctask->buffer_ptr, RE_N_MATCHES, m, 0);
840 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
841 "MHD RE: No match\n");
845 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
848 GNUNET_assert (m[1].rm_so != -1);
850 hostptr = ctask->buffer_ptr+m[1].rm_so;
854 bytes_to_copy = m[0].rm_so;
855 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
856 "Copying %d bytes.\n", m[0].rm_so);
862 if (ctask->is_postprocessing == GNUNET_YES)
866 if ( ctask->pp_finished == GNUNET_NO )
868 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
869 "MHD PP: Waiting for PP of %s\n", ctask->pp_buf);
873 ctask->is_postprocessing = GNUNET_NO;
875 ctask->bytes_in_buffer -= m[0].rm_eo;//(m[1].rm_eo-m[1].rm_so);
876 ctask->buffer_ptr += m[0].rm_eo;//(m[1].rm_eo-m[1].rm_so);
877 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
878 "Skipping next %d bytes in buffer\n", m[0].rm_eo);
880 GNUNET_SCHEDULER_add_now (&run_mhd, ctask->mhd);
882 if ( strlen (ctask->pp_buf) <= max )
884 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
885 "Copying postprocessed %s.\n", ctask->pp_buf);
886 memcpy ( buf, ctask->pp_buf, strlen (ctask->pp_buf) );
887 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
889 ctask->is_postprocessing = GNUNET_NO;
890 return strlen (ctask->pp_buf);
896 memset (ctask->pp_buf, 0, sizeof(ctask->pp_buf));
898 /* If .+ extend with authority */
899 if (*(ctask->buffer_ptr+m[1].rm_eo) == '+')
901 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
903 memcpy (ctask->pp_buf, hostptr, (m[1].rm_eo-m[1].rm_so));
904 strcpy ( ctask->pp_buf+strlen(ctask->pp_buf),
907 /* If .zkey or .tld simply copy the name */
910 memcpy (ctask->pp_buf, hostptr, (m[3].rm_eo-m[1].rm_so));
911 if (is_tld (ctask->pp_buf, GNUNET_GNS_TLD_ZKEY) == GNUNET_NO)
913 if (0 == strcmp (ctask->pp_buf, ctask->leho))
915 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
917 memcpy (ctask->pp_buf,
918 ctask->buffer_ptr+m[0].rm_so,
919 m[1].rm_so-m[0].rm_so);
920 strcpy (ctask->pp_buf+(m[1].rm_so-m[0].rm_so),
922 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
923 "New tag: %s\n", ctask->pp_buf);
924 ctask->is_postprocessing = GNUNET_YES;
925 ctask->pp_finished = GNUNET_YES;
926 GNUNET_SCHEDULER_add_now (&run_mhd, ctask->mhd);
932 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
933 "Link %s is zkey\n", ctask->pp_buf);
936 ctask->is_postprocessing = GNUNET_YES;
937 ctask->pp_finished = GNUNET_NO;
939 GNUNET_GNS_shorten_zone (gns_handle,
945 ctask); // FIXME: use after free of 'ctask'?
952 if ( bytes_to_copy > max )
954 GNUNET_log ( GNUNET_ERROR_TYPE_DEBUG,
955 "MHD: buffer in response too small! (%s)\n",
957 memcpy ( buf, ctask->buffer_ptr, max);
958 ctask->bytes_in_buffer -= max;
959 ctask->buffer_ptr += max;
964 GNUNET_log ( GNUNET_ERROR_TYPE_DEBUG,
965 "MHD: copying %d bytes to mhd response at offset %d\n",
968 memcpy ( buf, ctask->buffer_ptr, bytes_to_copy );
969 copied = bytes_to_copy;
970 if (bytes_to_copy < ctask->bytes_in_buffer)
972 ctask->bytes_in_buffer -= bytes_to_copy;
973 ctask->buffer_ptr += bytes_to_copy;
977 ctask->bytes_in_buffer = 0;
978 ctask->buf_status = BUF_WAIT_FOR_CURL;
979 ctask->buffer_ptr = ctask->buffer;
980 if (NULL != ctask->curl)
981 curl_easy_pause (ctask->curl, CURLPAUSE_CONT);
982 GNUNET_SCHEDULER_add_now (&run_mhd, ctask->mhd);
986 GNUNET_SCHEDULER_add_now (&run_mhd, ctask->mhd);
994 * Task that is run when we are ready to receive more data
998 * @param tc task context
1001 curl_task_download (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
1004 * Ask cURL for the select sets and schedule download
1007 curl_download_prepare ()
1014 struct GNUNET_NETWORK_FDSet *grs;
1015 struct GNUNET_NETWORK_FDSet *gws;
1017 struct GNUNET_TIME_Relative rtime;
1023 mret = curl_multi_fdset (curl_multi, &rs, &ws, &es, &max);
1025 if (mret != CURLM_OK)
1027 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1028 "%s failed at %s:%d: `%s'\n",
1029 "curl_multi_fdset", __FILE__, __LINE__,
1030 curl_multi_strerror (mret));
1031 //TODO cleanup here?
1035 mret = curl_multi_timeout (curl_multi, &to);
1036 rtime = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, to);
1038 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1039 "cURL multi fds: max=%d timeout=%llu\n", max, to);
1041 grs = GNUNET_NETWORK_fdset_create ();
1042 gws = GNUNET_NETWORK_fdset_create ();
1043 GNUNET_NETWORK_fdset_copy_native (grs, &rs, max + 1);
1044 GNUNET_NETWORK_fdset_copy_native (gws, &ws, max + 1);
1045 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1046 "Scheduling task cURL\n");
1048 if (curl_download_task != GNUNET_SCHEDULER_NO_TASK)
1049 GNUNET_SCHEDULER_cancel (curl_download_task);
1051 curl_download_task =
1052 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1055 &curl_task_download, curl_multi);
1056 GNUNET_NETWORK_fdset_destroy (gws);
1057 GNUNET_NETWORK_fdset_destroy (grs);
1063 * Task that is run when we are ready to receive more data
1066 * @param cls closure
1067 * @param tc task context
1070 curl_task_download (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1074 struct CURLMsg *msg;
1076 struct ProxyCurlTask *ctask;
1080 struct ProxyCurlTask *clean_head = NULL;
1081 struct ProxyCurlTask *clean_tail = NULL;
1083 curl_download_task = GNUNET_SCHEDULER_NO_TASK;
1085 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1087 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1088 "Shutdown requested while trying to download\n");
1092 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1100 mret = curl_multi_perform (curl_multi, &running);
1102 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1103 "Running curl tasks: %d\n", running);
1105 ctask = ctasks_head;
1106 for (; ctask != NULL; ctask = ctask->next)
1108 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1109 "CTask: %s\n", ctask->url);
1113 if (num_ctasks != running)
1115 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1116 "%d ctasks, %d curl running\n", num_ctasks, running);
1121 ctask = ctasks_head;
1122 msg = curl_multi_info_read (curl_multi, &msgnum);
1123 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1124 "Messages left: %d\n", msgnum);
1131 if ((msg->data.result != CURLE_OK) &&
1132 (msg->data.result != CURLE_GOT_NOTHING))
1134 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1135 "Download curl failed");
1137 for (; ctask != NULL; ctask = ctask->next)
1139 if (NULL == ctask->curl)
1142 if (memcmp (msg->easy_handle, ctask->curl, sizeof (CURL)) != 0)
1145 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1146 "Download curl failed for task %s: %s.\n",
1148 curl_easy_strerror (msg->data.result));
1149 ctask->download_successful = GNUNET_NO;
1150 ctask->download_error = GNUNET_YES;
1151 if (CURLE_OK == curl_easy_getinfo (ctask->curl,
1152 CURLINFO_RESPONSE_CODE,
1154 ctask->curl_response_code = resp_code;
1155 ctask->ready_to_queue = MHD_YES;
1156 GNUNET_SCHEDULER_add_now (&run_mhd, ctask->mhd);
1158 GNUNET_CONTAINER_DLL_remove (ctasks_head, ctasks_tail,
1160 GNUNET_CONTAINER_DLL_insert (clean_head, clean_tail, ctask);
1163 GNUNET_assert (ctask != NULL);
1167 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1168 "cURL download completed.\n");
1170 for (; ctask != NULL; ctask = ctask->next)
1172 if (NULL == ctask->curl)
1175 if (memcmp (msg->easy_handle, ctask->curl, sizeof (CURL)) != 0)
1178 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1179 "cURL task %s found.\n", ctask->url);
1180 if (CURLE_OK == curl_easy_getinfo (ctask->curl,
1181 CURLINFO_RESPONSE_CODE,
1183 ctask->curl_response_code = resp_code;
1184 ctask->ready_to_queue = MHD_YES;
1185 ctask->download_successful = GNUNET_YES;
1187 GNUNET_SCHEDULER_add_now (&run_mhd, ctask->mhd);
1188 GNUNET_CONTAINER_DLL_remove (ctasks_head, ctasks_tail,
1190 GNUNET_CONTAINER_DLL_insert (clean_head, clean_tail, ctask);
1194 GNUNET_assert (ctask != NULL);
1196 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1197 "curl end %s\n", curl_easy_strerror(msg->data.result));
1203 } while (msgnum > 0);
1205 for (ctask=clean_head; ctask != NULL; ctask = ctask->next)
1207 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1208 "Removing cURL task %s.\n", ctask->url);
1209 curl_multi_remove_handle (curl_multi, ctask->curl);
1210 curl_easy_cleanup (ctask->curl);
1215 for (ctask=ctasks_head; ctask != NULL; ctask = ctask->next)
1220 if (num_ctasks != running)
1222 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1223 "%d ctasks, %d curl running\n", num_ctasks, running);
1226 GNUNET_assert ( num_ctasks == running );
1230 } while (mret == CURLM_CALL_MULTI_PERFORM);
1233 if (mret != CURLM_OK)
1235 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "%s failed at %s:%d: `%s'\n",
1236 "curl_multi_perform", __FILE__, __LINE__,
1237 curl_multi_strerror (mret));
1239 curl_download_prepare();
1243 * Process LEHO lookup
1245 * @param cls the ctask
1246 * @param rd_count number of records returned
1247 * @param rd record data
1250 process_leho_lookup (void *cls,
1252 const struct GNUNET_NAMESTORE_RecordData *rd)
1254 struct ProxyCurlTask *ctask = cls;
1255 char hosthdr[262]; //256 + "Host: "
1259 struct hostent *phost;
1261 char resolvename[512];
1264 strcpy (ctask->leho, "");
1267 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1268 "No LEHO present!\n");
1270 for (i=0; i<rd_count; i++)
1272 if (rd[i].record_type != GNUNET_GNS_RECORD_LEHO)
1275 memcpy (ctask->leho, rd[i].data, rd[i].data_size);
1277 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1278 "Found LEHO %s for %s\n", ctask->leho, ctask->url);
1281 if (0 != strcmp (ctask->leho, ""))
1283 sprintf (hosthdr, "%s%s", "Host: ", ctask->leho);
1284 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1285 "New HTTP header value: %s\n", hosthdr);
1286 ctask->headers = curl_slist_append (ctask->headers, hosthdr);
1287 GNUNET_assert (NULL != ctask->headers);
1288 ret = curl_easy_setopt (ctask->curl, CURLOPT_HTTPHEADER, ctask->headers);
1289 if (CURLE_OK != ret)
1291 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "%s failed at %s:%d: `%s'\n",
1292 "curl_easy_setopt", __FILE__, __LINE__, curl_easy_strerror(ret));
1297 if (ctask->mhd->is_ssl)
1299 phost = (struct hostent*)gethostbyname (ctask->host);
1303 ssl_ip = inet_ntoa(*((struct in_addr*)(phost->h_addr)));
1304 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1305 "SSL target server: %s\n", ssl_ip);
1306 sprintf (resolvename, "%s:%d:%s", ctask->leho, HTTPS_PORT, ssl_ip);
1307 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1308 "Curl resolve: %s\n", resolvename);
1309 ctask->resolver = curl_slist_append ( ctask->resolver, resolvename);
1310 curl_easy_setopt (ctask->curl, CURLOPT_RESOLVE, ctask->resolver);
1311 sprintf (curlurl, "https://%s%s", ctask->leho, ctask->url);
1312 curl_easy_setopt (ctask->curl, CURLOPT_URL, curlurl);
1316 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1317 "gethostbyname failed for %s!\n", ctask->host);
1318 ctask->download_successful = GNUNET_NO;
1319 ctask->download_error = GNUNET_YES;
1324 if (CURLM_OK != (mret=curl_multi_add_handle (curl_multi, ctask->curl)))
1326 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1327 "%s failed at %s:%d: `%s'\n",
1328 "curl_multi_add_handle", __FILE__, __LINE__,
1329 curl_multi_strerror (mret));
1330 ctask->download_successful = GNUNET_NO;
1331 ctask->download_error = GNUNET_YES;
1334 GNUNET_CONTAINER_DLL_insert (ctasks_head, ctasks_tail, ctask);
1336 curl_download_prepare ();
1341 * Initialize download and trigger curl
1343 * @param cls the proxycurltask
1344 * @param auth_name the name of the authority (site of origin) of ctask->host
1348 process_get_authority (void *cls,
1349 const char* auth_name)
1351 struct ProxyCurlTask *ctask = cls;
1353 if (NULL == auth_name)
1355 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1356 "Get authority failed!\n");
1357 strcpy (ctask->authority, "");
1361 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1362 "Get authority yielded %s\n", auth_name);
1363 strcpy (ctask->authority, auth_name);
1366 GNUNET_GNS_lookup_zone (gns_handle,
1369 GNUNET_GNS_RECORD_LEHO,
1370 GNUNET_YES, //Only cached for performance
1372 &process_leho_lookup,
1377 * Task run whenever HTTP server operations are pending.
1380 * @param tc sched context
1383 do_httpd (void *cls,
1384 const struct GNUNET_SCHEDULER_TaskContext *tc);
1387 * Main MHD callback for handling requests.
1390 * @param con MHD connection handle
1391 * @param url the url in the request
1392 * @param meth the HTTP method used ("GET", "PUT", etc.)
1393 * @param ver the HTTP version string (i.e. "HTTP/1.1")
1394 * @param upload_data the data being uploaded (excluding HEADERS,
1395 * for a POST that fits into memory and that is encoded
1396 * with a supported encoding, the POST data will NOT be
1397 * given in upload_data and is instead available as
1398 * part of MHD_get_connection_values; very large POST
1399 * data *will* be made available incrementally in
1401 * @param upload_data_size set initially to the size of the
1402 * upload_data provided; the method must update this
1403 * value to the number of bytes NOT processed;
1404 * @param con_cls pointer to location where we store the 'struct Request'
1405 * @return MHD_YES if the connection was handled successfully,
1406 * MHD_NO if the socket must be closed due to a serious
1407 * error while handling the request
1410 create_response (void *cls,
1411 struct MHD_Connection *con,
1415 const char *upload_data,
1416 size_t *upload_data_size,
1419 struct MhdHttpList* hd = cls;
1420 const char* page = "<html><head><title>gnoxy</title>"\
1421 "</head><body>cURL fail</body></html>";
1423 char curlurl[512]; // buffer overflow!
1426 struct ProxyCurlTask *ctask;
1429 if (0 != strcasecmp (meth, MHD_HTTP_METHOD_GET))
1431 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1432 "%s NOT IMPLEMENTED!\n", meth);
1436 if (0 != *upload_data_size)
1439 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1443 if (NULL == *con_cls)
1445 ctask = GNUNET_malloc (sizeof (struct ProxyCurlTask));
1449 // FIXME: probably not best here, also never free'ed...
1450 if (NULL == curl_multi)
1451 curl_multi = curl_multi_init ();
1453 ctask->curl = curl_easy_init();
1455 if ((NULL == ctask->curl) || (NULL == curl_multi)) // ugh, curl_multi init failure check MUCH earlier
1457 ctask->response = MHD_create_response_from_buffer (strlen (page),
1459 MHD_RESPMEM_PERSISTENT);
1460 ret = MHD_queue_response (con,
1463 MHD_destroy_response (ctask->response);
1464 GNUNET_free (ctask);
1468 ctask->download_in_progress = GNUNET_YES;
1469 ctask->buf_status = BUF_WAIT_FOR_CURL;
1470 ctask->connection = con;
1471 ctask->curl_response_code = MHD_HTTP_OK;
1473 MHD_get_connection_values (con,
1475 &con_val_iter, ctask);
1477 curl_easy_setopt (ctask->curl, CURLOPT_HEADERFUNCTION, &curl_check_hdr);
1478 curl_easy_setopt (ctask->curl, CURLOPT_HEADERDATA, ctask);
1479 curl_easy_setopt (ctask->curl, CURLOPT_WRITEFUNCTION, &callback_download);
1480 curl_easy_setopt (ctask->curl, CURLOPT_WRITEDATA, ctask);
1481 curl_easy_setopt (ctask->curl, CURLOPT_FOLLOWLOCATION, 0);
1482 //curl_easy_setopt (ctask->curl, CURLOPT_MAXREDIRS, 4);
1483 /* no need to abort if the above failed */
1484 if (GNUNET_NO == ctask->mhd->is_ssl)
1486 sprintf (curlurl, "http://%s%s", ctask->host, url);
1487 MHD_get_connection_values (con,
1488 MHD_GET_ARGUMENT_KIND,
1489 &get_uri_val_iter, curlurl);
1490 curl_easy_setopt (ctask->curl, CURLOPT_URL, curlurl);
1491 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1492 "Adding new curl task for %s\n", curlurl);
1494 strcpy (ctask->url, url);
1495 MHD_get_connection_values (con,
1496 MHD_GET_ARGUMENT_KIND,
1497 &get_uri_val_iter, ctask->url);
1499 //curl_easy_setopt (ctask->curl, CURLOPT_URL, curlurl);
1500 curl_easy_setopt (ctask->curl, CURLOPT_FAILONERROR, 1);
1501 curl_easy_setopt (ctask->curl, CURLOPT_CONNECTTIMEOUT, 600L);
1502 curl_easy_setopt (ctask->curl, CURLOPT_TIMEOUT, 600L);
1504 GNUNET_GNS_get_authority (gns_handle,
1506 &process_get_authority,
1508 ctask->ready_to_queue = GNUNET_NO;
1509 ctask->fin = GNUNET_NO;
1513 ctask = (struct ProxyCurlTask *) *con_cls;
1514 GNUNET_break (GNUNET_YES != ctask->fin);
1515 if (GNUNET_YES != ctask->ready_to_queue)
1516 return MHD_YES; /* wait longer */
1517 ctask->fin = GNUNET_YES;
1518 ret = MHD_queue_response (con, ctask->curl_response_code, ctask->response);
1519 run_mhd_now (ctask->mhd);
1530 struct MhdHttpList *hd;
1532 for (hd=mhd_httpd_head; hd != NULL; hd = hd->next)
1540 * @param hd the daemon to run
1543 run_httpd (struct MhdHttpList *hd)
1548 struct GNUNET_NETWORK_FDSet *wrs;
1549 struct GNUNET_NETWORK_FDSet *wws;
1550 struct GNUNET_NETWORK_FDSet *wes;
1553 unsigned MHD_LONG_LONG timeout;
1554 struct GNUNET_TIME_Relative tv;
1559 wrs = GNUNET_NETWORK_fdset_create ();
1560 wes = GNUNET_NETWORK_fdset_create ();
1561 wws = GNUNET_NETWORK_fdset_create ();
1563 GNUNET_assert (MHD_YES == MHD_get_fdset (hd->daemon, &rs, &ws, &es, &max));
1566 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1567 "MHD fds: max=%d\n", max);
1569 haveto = MHD_get_timeout (hd->daemon, &timeout);
1571 if (haveto == MHD_YES)
1572 tv.rel_value = (uint64_t) timeout;
1574 tv = GNUNET_TIME_UNIT_FOREVER_REL;
1575 GNUNET_NETWORK_fdset_copy_native (wrs, &rs, max + 1);
1576 GNUNET_NETWORK_fdset_copy_native (wws, &ws, max + 1);
1577 GNUNET_NETWORK_fdset_copy_native (wes, &es, max + 1);
1579 if (hd->httpd_task != GNUNET_SCHEDULER_NO_TASK)
1580 GNUNET_SCHEDULER_cancel (hd->httpd_task);
1582 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_HIGH,
1585 GNUNET_NETWORK_fdset_destroy (wrs);
1586 GNUNET_NETWORK_fdset_destroy (wws);
1587 GNUNET_NETWORK_fdset_destroy (wes);
1592 * Task run whenever HTTP server operations are pending.
1595 * @param tc sched context
1598 do_httpd (void *cls,
1599 const struct GNUNET_SCHEDULER_TaskContext *tc)
1601 struct MhdHttpList *hd = cls;
1603 hd->httpd_task = GNUNET_SCHEDULER_NO_TASK;
1604 MHD_run (hd->daemon);
1610 run_mhd_now (struct MhdHttpList *hd)
1612 GNUNET_SCHEDULER_cancel (hd->httpd_task);
1613 hd->httpd_task = GNUNET_SCHEDULER_add_now (&do_httpd, hd);
1618 * Read data from socket
1620 * @param cls the closure
1621 * @param tc scheduler context
1624 do_read (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
1627 * Read from remote end
1629 * @param cls closure
1630 * @param tc scheduler context
1633 do_read_remote (void* cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
1636 * Write data to remote socket
1638 * @param cls the closure
1639 * @param tc scheduler context
1642 do_write_remote (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1644 struct Socks5Request *s5r = cls;
1647 s5r->fwdwtask = GNUNET_SCHEDULER_NO_TASK;
1649 if ((NULL != tc->read_ready) &&
1650 (GNUNET_NETWORK_fdset_isset (tc->write_ready, s5r->remote_sock)) &&
1651 ((len = GNUNET_NETWORK_socket_send (s5r->remote_sock, s5r->rbuf,
1654 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1655 "Successfully sent %d bytes to remote socket\n",
1660 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "write remote");
1662 if (s5r->rtask != GNUNET_SCHEDULER_NO_TASK)
1663 GNUNET_SCHEDULER_cancel (s5r->rtask);
1664 if (s5r->wtask != GNUNET_SCHEDULER_NO_TASK)
1665 GNUNET_SCHEDULER_cancel (s5r->wtask);
1666 if (s5r->fwdrtask != GNUNET_SCHEDULER_NO_TASK)
1667 GNUNET_SCHEDULER_cancel (s5r->fwdrtask);
1668 GNUNET_NETWORK_socket_close (s5r->remote_sock);
1669 GNUNET_NETWORK_socket_close (s5r->sock);
1675 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
1682 * Clean up s5r handles
1684 * @param s5r the handle to destroy
1687 cleanup_s5r (struct Socks5Request *s5r)
1689 if (s5r->rtask != GNUNET_SCHEDULER_NO_TASK)
1690 GNUNET_SCHEDULER_cancel (s5r->rtask);
1691 if (s5r->fwdwtask != GNUNET_SCHEDULER_NO_TASK)
1692 GNUNET_SCHEDULER_cancel (s5r->fwdwtask);
1693 if (s5r->fwdrtask != GNUNET_SCHEDULER_NO_TASK)
1694 GNUNET_SCHEDULER_cancel (s5r->fwdrtask);
1696 if (NULL != s5r->remote_sock)
1697 GNUNET_NETWORK_socket_close (s5r->remote_sock);
1698 if ((NULL != s5r->sock) && (s5r->cleanup_sock == GNUNET_YES))
1699 GNUNET_NETWORK_socket_close (s5r->sock);
1705 * Write data to socket
1707 * @param cls the closure
1708 * @param tc scheduler context
1711 do_write (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1713 struct Socks5Request *s5r = cls;
1716 s5r->wtask = GNUNET_SCHEDULER_NO_TASK;
1718 if ((NULL != tc->read_ready) &&
1719 (GNUNET_NETWORK_fdset_isset (tc->write_ready, s5r->sock)) &&
1720 ((len = GNUNET_NETWORK_socket_send (s5r->sock, s5r->wbuf,
1723 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1724 "Successfully sent %d bytes to socket\n",
1730 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "write");
1731 s5r->cleanup = GNUNET_YES;
1732 s5r->cleanup_sock = GNUNET_YES;
1738 if (GNUNET_YES == s5r->cleanup)
1744 if ((s5r->state == SOCKS5_DATA_TRANSFER) &&
1745 (s5r->fwdrtask == GNUNET_SCHEDULER_NO_TASK))
1747 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
1749 &do_read_remote, s5r);
1753 * Read from remote end
1755 * @param cls closure
1756 * @param tc scheduler context
1759 do_read_remote (void* cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1761 struct Socks5Request *s5r = cls;
1763 s5r->fwdrtask = GNUNET_SCHEDULER_NO_TASK;
1766 if ((NULL != tc->write_ready) &&
1767 (GNUNET_NETWORK_fdset_isset (tc->read_ready, s5r->remote_sock)) &&
1768 (s5r->wbuf_len = GNUNET_NETWORK_socket_recv (s5r->remote_sock, s5r->wbuf,
1769 sizeof (s5r->wbuf))))
1771 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1772 "Successfully read %d bytes from remote socket\n",
1777 if (s5r->wbuf_len == 0)
1778 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1779 "0 bytes received from remote... graceful shutdown!\n");
1780 if (s5r->fwdwtask != GNUNET_SCHEDULER_NO_TASK)
1781 GNUNET_SCHEDULER_cancel (s5r->fwdwtask);
1782 if (s5r->rtask != GNUNET_SCHEDULER_NO_TASK)
1783 GNUNET_SCHEDULER_cancel (s5r->rtask);
1785 GNUNET_NETWORK_socket_close (s5r->remote_sock);
1786 s5r->remote_sock = NULL;
1787 GNUNET_NETWORK_socket_close (s5r->sock);
1793 s5r->wtask = GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
1801 * Adds a socket to MHD
1803 * @param h the handle to the socket to add
1804 * @param daemon the daemon to add the fd to
1805 * @return whatever MHD_add_connection returns
1808 add_handle_to_mhd (struct GNUNET_NETWORK_Handle *h, struct MHD_Daemon *daemon)
1811 struct sockaddr *addr;
1814 fd = dup (GNUNET_NETWORK_get_fd (h));
1815 addr = GNUNET_NETWORK_get_addr (h);
1816 len = GNUNET_NETWORK_get_addrlen (h);
1818 return MHD_add_connection (daemon, fd, addr, len);
1822 * Calculate size of file
1824 * @param filename name of file
1825 * @return filesize or 0 on error
1828 get_file_size (const char* filename)
1833 if (NULL == (fp = fopen (filename, "rb")))
1835 if ((0 != fseek (fp, 0, SEEK_END)) || (-1 == (size = ftell (fp))))
1843 * Read file in filename
1845 * @param filename file to read
1846 * @param size pointer where filesize is stored
1850 load_file (const char* filename,
1857 GNUNET_DISK_file_size (filename, &fsize,
1858 GNUNET_YES, GNUNET_YES))
1860 if (fsize > MAX_PEM_SIZE)
1862 *size = (unsigned int) fsize;
1863 buffer = GNUNET_malloc (*size);
1864 if (fsize != GNUNET_DISK_fn_read (filename, buffer, (size_t) fsize))
1866 GNUNET_free (buffer);
1874 * Load PEM key from file
1876 * @param key where to store the data
1877 * @param keyfile path to the PEM file
1878 * @return GNUNET_OK on success
1881 load_key_from_file (gnutls_x509_privkey_t key, const char* keyfile)
1883 gnutls_datum_t key_data;
1886 key_data.data = (unsigned char*) load_file (keyfile, &key_data.size);
1887 ret = gnutls_x509_privkey_import (key, &key_data,
1888 GNUTLS_X509_FMT_PEM);
1889 if (GNUTLS_E_SUCCESS != ret)
1891 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1892 _("Unable to import private key from file `%s'\n"),
1896 GNUNET_free (key_data.data);
1897 return (GNUTLS_E_SUCCESS != ret) ? GNUNET_SYSERR : GNUNET_OK;
1902 * Load cert from file
1904 * @param crt struct to store data in
1905 * @param certfile path to pem file
1908 load_cert_from_file (gnutls_x509_crt_t crt, char* certfile)
1910 gnutls_datum_t cert_data;
1911 cert_data.data = NULL;
1914 cert_data.data = (unsigned char*)load_file (certfile, &cert_data.size);
1916 ret = gnutls_x509_crt_import (crt, &cert_data,
1917 GNUTLS_X509_FMT_PEM);
1918 if (GNUTLS_E_SUCCESS != ret)
1920 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1921 "Unable to import certificate %s(ret=%d)\n", certfile, ret);
1925 GNUNET_free (cert_data.data);
1931 * Generate new certificate for specific name
1933 * @param name the subject name to generate a cert for
1934 * @return a struct holding the PEM data
1936 static struct ProxyGNSCertificate *
1937 generate_gns_certificate (const char *name)
1941 unsigned int serial;
1942 size_t key_buf_size;
1943 size_t cert_buf_size;
1944 gnutls_x509_crt_t request;
1948 ret = gnutls_x509_crt_init (&request);
1950 if (GNUTLS_E_SUCCESS != ret)
1955 ret = gnutls_x509_crt_set_key (request, proxy_ca.key);
1957 if (GNUTLS_E_SUCCESS != ret)
1962 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Generating cert\n");
1964 struct ProxyGNSCertificate *pgc =
1965 GNUNET_malloc (sizeof (struct ProxyGNSCertificate));
1967 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Adding DNs\n");
1969 gnutls_x509_crt_set_dn_by_oid (request, GNUTLS_OID_X520_COUNTRY_NAME,
1972 gnutls_x509_crt_set_dn_by_oid (request, GNUTLS_OID_X520_ORGANIZATION_NAME,
1975 gnutls_x509_crt_set_dn_by_oid (request, GNUTLS_OID_X520_COMMON_NAME,
1976 0, name, strlen (name));
1978 ret = gnutls_x509_crt_set_version (request, 3);
1980 ret = gnutls_rnd (GNUTLS_RND_NONCE, &serial, sizeof (serial));
1982 etime = time (NULL);
1983 tm_data = localtime (&etime);
1986 ret = gnutls_x509_crt_set_serial (request,
1990 ret = gnutls_x509_crt_set_activation_time (request,
1993 etime = mktime (tm_data);
2000 ret = gnutls_x509_crt_set_expiration_time (request,
2002 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Signing...\n");
2004 ret = gnutls_x509_crt_sign (request, proxy_ca.cert, proxy_ca.key);
2006 key_buf_size = sizeof (pgc->key);
2007 cert_buf_size = sizeof (pgc->cert);
2009 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Exporting certificate...\n");
2011 gnutls_x509_crt_export (request, GNUTLS_X509_FMT_PEM,
2012 pgc->cert, &cert_buf_size);
2014 gnutls_x509_privkey_export (proxy_ca.key, GNUTLS_X509_FMT_PEM,
2015 pgc->key, &key_buf_size);
2018 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Cleaning up\n");
2019 gnutls_x509_crt_deinit (request);
2027 * Accept policy for mhdaemons
2030 * @param addr the sockaddr
2031 * @param addrlen the sockaddr length
2032 * @return MHD_NO if sockaddr is wrong or #conns too high
2035 accept_cb (void* cls, const struct sockaddr *addr, socklen_t addrlen)
2037 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2038 "In MHD accept policy cb\n");
2042 if (addr->sa_family == AF_UNIX)
2046 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2047 "Connection accepted\n");
2054 * Adds a socket to an SSL MHD instance
2055 * It is important the the domain name is
2056 * correct. In most cases we need to start a new daemon
2058 * @param h the handle to add to a daemon
2059 * @param domain the domain the ssl daemon has to serve
2060 * @return MHD_YES on success
2063 add_handle_to_ssl_mhd (struct GNUNET_NETWORK_Handle *h, const char* domain)
2065 struct MhdHttpList *hd = NULL;
2066 struct ProxyGNSCertificate *pgc;
2067 struct NetworkHandleList *nh;
2069 for (hd = mhd_httpd_head; NULL != hd; hd = hd->next)
2070 if (0 == strcmp (hd->domain, domain))
2075 pgc = generate_gns_certificate (domain);
2077 hd = GNUNET_malloc (sizeof (struct MhdHttpList));
2078 hd->is_ssl = GNUNET_YES;
2079 strcpy (hd->domain, domain);
2080 hd->proxy_cert = pgc;
2083 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2084 "No previous SSL instance found... starting new one for %s\n",
2087 hd->daemon = MHD_start_daemon (MHD_USE_DEBUG | MHD_USE_SSL, 0,
2089 &create_response, hd,
2090 MHD_OPTION_LISTEN_SOCKET, GNUNET_NETWORK_get_fd (mhd_unix_socket),
2091 MHD_OPTION_CONNECTION_LIMIT, MHD_MAX_CONNECTIONS,
2092 MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) 16,
2093 MHD_OPTION_NOTIFY_COMPLETED,
2095 MHD_OPTION_HTTPS_MEM_KEY, pgc->key,
2096 MHD_OPTION_HTTPS_MEM_CERT, pgc->cert,
2099 GNUNET_assert (hd->daemon != NULL);
2100 hd->httpd_task = GNUNET_SCHEDULER_NO_TASK;
2102 GNUNET_CONTAINER_DLL_insert (mhd_httpd_head, mhd_httpd_tail, hd);
2105 nh = GNUNET_malloc (sizeof (struct NetworkHandleList));
2108 GNUNET_CONTAINER_DLL_insert (hd->socket_handles_head,
2109 hd->socket_handles_tail,
2112 return add_handle_to_mhd (h, hd->daemon);
2118 * Read data from incoming connection
2120 * @param cls the closure
2121 * @param tc the scheduler context
2124 do_read (void* cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2126 struct Socks5Request *s5r = cls;
2127 struct socks5_client_hello *c_hello;
2128 struct socks5_server_hello *s_hello;
2129 struct socks5_client_request *c_req;
2130 struct socks5_server_response *s_resp;
2136 struct hostent *phost;
2138 struct sockaddr_in remote_addr;
2139 struct in_addr *r_sin_addr;
2141 struct NetworkHandleList *nh;
2143 s5r->rtask = GNUNET_SCHEDULER_NO_TASK;
2145 if ((NULL != tc->write_ready) &&
2146 (GNUNET_NETWORK_fdset_isset (tc->read_ready, s5r->sock)) &&
2147 (s5r->rbuf_len = GNUNET_NETWORK_socket_recv (s5r->sock, s5r->rbuf,
2148 sizeof (s5r->rbuf))))
2150 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2151 "Successfully read %d bytes from socket\n",
2156 if (s5r->rbuf_len != 0)
2157 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "read");
2159 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client disco!\n");
2161 if (s5r->fwdrtask != GNUNET_SCHEDULER_NO_TASK)
2162 GNUNET_SCHEDULER_cancel (s5r->fwdrtask);
2163 if (s5r->wtask != GNUNET_SCHEDULER_NO_TASK)
2164 GNUNET_SCHEDULER_cancel (s5r->wtask);
2165 if (s5r->fwdwtask != GNUNET_SCHEDULER_NO_TASK)
2166 GNUNET_SCHEDULER_cancel (s5r->fwdwtask);
2167 GNUNET_NETWORK_socket_close (s5r->remote_sock);
2168 GNUNET_NETWORK_socket_close (s5r->sock);
2173 if (s5r->state == SOCKS5_INIT)
2175 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2177 c_hello = (struct socks5_client_hello*)&s5r->rbuf;
2179 GNUNET_assert (c_hello->version == SOCKS_VERSION_5);
2181 s_hello = (struct socks5_server_hello*)&s5r->wbuf;
2182 s5r->wbuf_len = sizeof( struct socks5_server_hello );
2184 s_hello->version = c_hello->version;
2185 s_hello->auth_method = SOCKS_AUTH_NONE;
2187 /* Write response to client */
2188 s5r->wtask = GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
2192 s5r->rtask = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
2196 s5r->state = SOCKS5_REQUEST;
2200 if (s5r->state == SOCKS5_REQUEST)
2202 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2203 "Processing SOCKS5 request\n");
2204 c_req = (struct socks5_client_request*)&s5r->rbuf;
2205 s_resp = (struct socks5_server_response*)&s5r->wbuf;
2206 //Only 10byte for ipv4 response!
2207 s5r->wbuf_len = 10;//sizeof (struct socks5_server_response);
2209 GNUNET_assert (c_req->addr_type == 3);
2211 dom_len = *((uint8_t*)(&(c_req->addr_type) + 1));
2212 memset(domain, 0, sizeof(domain));
2213 strncpy(domain, (char*)(&(c_req->addr_type) + 2), dom_len);
2214 req_port = *((uint16_t*)(&(c_req->addr_type) + 2 + dom_len));
2216 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2217 "Requested connection is %s:%d\n",
2221 if (is_tld (domain, GNUNET_GNS_TLD) ||
2222 is_tld (domain, GNUNET_GNS_TLD_ZKEY))
2224 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2225 "Requested connection is gnunet tld\n",
2229 if (ntohs(req_port) == HTTPS_PORT)
2231 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2232 "Requested connection is HTTPS\n");
2233 ret = add_handle_to_ssl_mhd ( s5r->sock, domain );
2235 else if (NULL != httpd)
2237 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2238 "Requested connection is HTTP\n");
2239 nh = GNUNET_malloc (sizeof (struct NetworkHandleList));
2242 GNUNET_CONTAINER_DLL_insert (mhd_httpd_head->socket_handles_head,
2243 mhd_httpd_head->socket_handles_tail,
2246 ret = add_handle_to_mhd ( s5r->sock, httpd );
2251 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2252 _("Failed to start HTTP server\n"));
2253 s_resp->version = 0x05;
2254 s_resp->reply = 0x01;
2255 s5r->cleanup = GNUNET_YES;
2256 s5r->cleanup_sock = GNUNET_YES;
2258 GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
2264 /* Signal success */
2265 s_resp->version = 0x05;
2266 s_resp->reply = 0x00;
2267 s_resp->reserved = 0x00;
2268 s_resp->addr_type = 0x01;
2270 s5r->cleanup = GNUNET_YES;
2271 s5r->cleanup_sock = GNUNET_NO;
2273 GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
2281 phost = (struct hostent*)gethostbyname (domain);
2284 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2285 "Resolve %s error!\n", domain );
2286 s_resp->version = 0x05;
2287 s_resp->reply = 0x01;
2288 s5r->cleanup = GNUNET_YES;
2289 s5r->cleanup_sock = GNUNET_YES;
2291 GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
2297 s5r->remote_sock = GNUNET_NETWORK_socket_create (AF_INET,
2300 r_sin_addr = (struct in_addr*)(phost->h_addr);
2301 remote_ip = r_sin_addr->s_addr;
2302 memset(&remote_addr, 0, sizeof(remote_addr));
2303 remote_addr.sin_family = AF_INET;
2304 #if HAVE_SOCKADDR_IN_SIN_LEN
2305 remote_addr.sin_len = sizeof (remote_addr);
2307 remote_addr.sin_addr.s_addr = remote_ip;
2308 remote_addr.sin_port = req_port;
2310 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2311 "target server: %s:%u\n", inet_ntoa(remote_addr.sin_addr),
2315 GNUNET_NETWORK_socket_connect ( s5r->remote_sock,
2316 (const struct sockaddr*)&remote_addr,
2317 sizeof (remote_addr)))
2318 && (errno != EINPROGRESS))
2320 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "connect");
2321 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2322 "socket request error...\n");
2323 s_resp->version = 0x05;
2324 s_resp->reply = 0x01;
2326 GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
2333 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2334 "new remote connection\n");
2336 s_resp->version = 0x05;
2337 s_resp->reply = 0x00;
2338 s_resp->reserved = 0x00;
2339 s_resp->addr_type = 0x01;
2341 s5r->state = SOCKS5_DATA_TRANSFER;
2344 GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
2348 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
2356 if (s5r->state == SOCKS5_DATA_TRANSFER)
2358 if ((s5r->remote_sock == NULL) || (s5r->rbuf_len == 0))
2360 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2361 "Closing connection to client\n");
2362 if (s5r->rtask != GNUNET_SCHEDULER_NO_TASK)
2363 GNUNET_SCHEDULER_cancel (s5r->rtask);
2364 if (s5r->fwdwtask != GNUNET_SCHEDULER_NO_TASK)
2365 GNUNET_SCHEDULER_cancel (s5r->fwdwtask);
2366 if (s5r->fwdrtask != GNUNET_SCHEDULER_NO_TASK)
2367 GNUNET_SCHEDULER_cancel (s5r->fwdrtask);
2368 if (s5r->fwdrtask != GNUNET_SCHEDULER_NO_TASK)
2369 GNUNET_SCHEDULER_cancel (s5r->fwdrtask);
2371 if (s5r->remote_sock != NULL)
2372 GNUNET_NETWORK_socket_close (s5r->remote_sock);
2373 GNUNET_NETWORK_socket_close (s5r->sock);
2378 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2379 "forwarding %d bytes from client\n", s5r->rbuf_len);
2382 GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
2384 &do_write_remote, s5r);
2386 if (s5r->fwdrtask == GNUNET_SCHEDULER_NO_TASK)
2389 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
2391 &do_read_remote, s5r);
2397 //GNUNET_CONTAINER_DLL_remove (s5conns.head, s5conns.tail, s5r);
2403 * Accept new incoming connections
2405 * @param cls the closure
2406 * @param tc the scheduler context
2409 do_accept (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2411 struct GNUNET_NETWORK_Handle *s;
2412 struct Socks5Request *s5r;
2414 ltask = GNUNET_SCHEDULER_NO_TASK;
2415 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
2418 ltask = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
2422 s = GNUNET_NETWORK_socket_accept (lsock, NULL, NULL);
2426 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "accept");
2430 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2431 "Got an inbound connection, waiting for data\n");
2433 s5r = GNUNET_malloc (sizeof (struct Socks5Request));
2435 s5r->state = SOCKS5_INIT;
2436 s5r->wtask = GNUNET_SCHEDULER_NO_TASK;
2437 s5r->fwdwtask = GNUNET_SCHEDULER_NO_TASK;
2438 s5r->fwdrtask = GNUNET_SCHEDULER_NO_TASK;
2439 s5r->rtask = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
2442 //GNUNET_CONTAINER_DLL_insert (s5conns.head, s5conns.tail, s5r);
2447 * Task run on shutdown
2449 * @param cls closure
2450 * @param tc task context
2453 do_shutdown (void *cls,
2454 const struct GNUNET_SCHEDULER_TaskContext *tc)
2457 struct MhdHttpList *hd;
2458 struct MhdHttpList *tmp_hd;
2459 struct NetworkHandleList *nh;
2460 struct NetworkHandleList *tmp_nh;
2461 struct ProxyCurlTask *ctask;
2462 struct ProxyCurlTask *ctask_tmp;
2464 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2465 "Shutting down...\n");
2467 gnutls_global_deinit ();
2469 if (GNUNET_SCHEDULER_NO_TASK != curl_download_task)
2471 GNUNET_SCHEDULER_cancel (curl_download_task);
2472 curl_download_task = GNUNET_SCHEDULER_NO_TASK;
2475 for (hd = mhd_httpd_head; hd != NULL; hd = tmp_hd)
2479 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2480 "Stopping daemon\n");
2482 if (GNUNET_SCHEDULER_NO_TASK != hd->httpd_task)
2484 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2485 "Stopping select task %d\n",
2487 GNUNET_SCHEDULER_cancel (hd->httpd_task);
2488 hd->httpd_task = GNUNET_SCHEDULER_NO_TASK;
2491 if (NULL != hd->daemon)
2493 MHD_stop_daemon (hd->daemon);
2497 for (nh = hd->socket_handles_head; nh != NULL; nh = tmp_nh)
2501 GNUNET_NETWORK_socket_close (nh->h);
2506 if (NULL != hd->proxy_cert)
2508 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2509 "Free certificate\n");
2510 GNUNET_free (hd->proxy_cert);
2516 for (ctask=ctasks_head; ctask != NULL; ctask=ctask_tmp)
2518 ctask_tmp = ctask->next;
2520 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2521 "Cleaning up cURL task\n");
2523 if (ctask->curl != NULL)
2524 curl_easy_cleanup (ctask->curl);
2526 if (NULL != ctask->headers)
2527 curl_slist_free_all (ctask->headers);
2528 if (NULL != ctask->resolver)
2529 curl_slist_free_all (ctask->resolver);
2531 if (NULL != ctask->response)
2532 MHD_destroy_response (ctask->response);
2535 GNUNET_free (ctask);
2538 GNUNET_GNS_disconnect (gns_handle);
2543 * Compiles a regex for us
2545 * @param re ptr to re struct
2546 * @param rt the expression to compile
2547 * @return 0 on success
2550 compile_regex (regex_t *re, const char* rt)
2555 status = regcomp (re, rt, REG_EXTENDED|REG_NEWLINE);
2558 regerror (status, re, err, 1024);
2559 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2560 "Regex error compiling '%s': %s\n", rt, err);
2568 * Loads the users local zone key
2570 * @return GNUNET_YES on success
2573 load_local_zone_key (const struct GNUNET_CONFIGURATION_Handle *cfg)
2576 struct GNUNET_CRYPTO_RsaPrivateKey *key = NULL;
2577 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pkey;
2578 struct GNUNET_CRYPTO_ShortHashCode *zone = NULL;
2579 struct GNUNET_CRYPTO_ShortHashAsciiEncoded zonename;
2581 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "gns",
2582 "ZONEKEY", &keyfile))
2584 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2585 "Unable to load zone key config value!\n");
2589 if (GNUNET_NO == GNUNET_DISK_file_test (keyfile))
2591 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2592 "Unable to load zone key %s!\n", keyfile);
2593 GNUNET_free(keyfile);
2597 key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
2598 GNUNET_CRYPTO_rsa_key_get_public (key, &pkey);
2599 GNUNET_CRYPTO_short_hash(&pkey,
2600 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
2602 zone = &local_gns_zone;
2603 GNUNET_CRYPTO_short_hash_to_enc (zone, &zonename);
2604 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2605 "Using zone: %s!\n", &zonename);
2606 GNUNET_CRYPTO_rsa_key_free(key);
2607 GNUNET_free(keyfile);
2609 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "gns",
2610 "PRIVATE_ZONEKEY", &keyfile))
2612 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2613 "Unable to load private zone key config value!\n");
2617 if (GNUNET_NO == GNUNET_DISK_file_test (keyfile))
2619 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2620 "Unable to load private zone key %s!\n", keyfile);
2621 GNUNET_free(keyfile);
2625 key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
2626 GNUNET_CRYPTO_rsa_key_get_public (key, &pkey);
2627 GNUNET_CRYPTO_short_hash(&pkey,
2628 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
2629 &local_private_zone);
2630 GNUNET_CRYPTO_short_hash_to_enc (zone, &zonename);
2631 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2632 "Using private zone: %s!\n", &zonename);
2633 GNUNET_CRYPTO_rsa_key_free(key);
2634 GNUNET_free(keyfile);
2636 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "gns",
2637 "SHORTEN_ZONEKEY", &keyfile))
2639 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2640 "Unable to load shorten zone key config value!\n");
2644 if (GNUNET_NO == GNUNET_DISK_file_test (keyfile))
2646 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2647 "Unable to load shorten zone key %s!\n", keyfile);
2648 GNUNET_free(keyfile);
2652 key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
2653 GNUNET_CRYPTO_rsa_key_get_public (key, &pkey);
2654 GNUNET_CRYPTO_short_hash(&pkey,
2655 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
2656 &local_shorten_zone);
2657 GNUNET_CRYPTO_short_hash_to_enc (zone, &zonename);
2658 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2659 "Using shorten zone: %s!\n", &zonename);
2660 GNUNET_CRYPTO_rsa_key_free(key);
2661 GNUNET_free(keyfile);
2667 * Main function that will be run
2669 * @param cls closure
2670 * @param args remaining command-line arguments
2671 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
2672 * @param cfg configuration
2675 run (void *cls, char *const *args, const char *cfgfile,
2676 const struct GNUNET_CONFIGURATION_Handle *cfg)
2678 struct sockaddr_in sa;
2679 struct MhdHttpList *hd;
2680 struct sockaddr_un mhd_unix_sock_addr;
2682 char* proxy_sockfile;
2683 char* cafile_cfg = NULL;
2688 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2691 cafile = cafile_opt;
2695 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "gns-proxy",
2699 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2700 "Unable to load proxy CA config value!\n");
2701 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2702 "No proxy CA provided!\n");
2705 cafile = cafile_cfg;
2708 gnutls_global_init ();
2710 gnutls_x509_crt_init (&proxy_ca.cert);
2711 gnutls_x509_privkey_init (&proxy_ca.key);
2713 if ( (GNUNET_OK != load_cert_from_file (proxy_ca.cert, cafile)) ||
2714 (GNUNET_OK != load_key_from_file (proxy_ca.key, cafile)) )
2716 // FIXME: release resources...
2720 GNUNET_free_non_null (cafile_cfg);
2722 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2723 "Loading Template\n");
2725 compile_regex (&re_dotplus, (char*) RE_A_HREF);
2727 gns_handle = GNUNET_GNS_connect (cfg);
2729 if (GNUNET_NO == load_local_zone_key (cfg))
2731 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2732 "Unable to load zone!\n");
2736 if (NULL == gns_handle)
2738 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2739 "Unable to connect to GNS!\n");
2743 memset (&sa, 0, sizeof (sa));
2744 sa.sin_family = AF_INET;
2745 sa.sin_port = htons (port);
2746 #if HAVE_SOCKADDR_IN_SIN_LEN
2747 sa.sin_len = sizeof (sa);
2750 lsock = GNUNET_NETWORK_socket_create (AF_INET,
2754 if ((NULL == lsock) ||
2756 GNUNET_NETWORK_socket_bind (lsock, (const struct sockaddr *) &sa,
2759 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2760 "Failed to create listen socket bound to `%s'",
2761 GNUNET_a2s ((const struct sockaddr *) &sa, sizeof (sa)));
2763 GNUNET_NETWORK_socket_close (lsock);
2767 if (GNUNET_OK != GNUNET_NETWORK_socket_listen (lsock, 5))
2769 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2770 "Failed to listen on socket bound to `%s'",
2771 GNUNET_a2s ((const struct sockaddr *) &sa, sizeof (sa)));
2775 ltask = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
2776 lsock, &do_accept, NULL);
2781 if (0 != curl_global_init (CURL_GLOBAL_WIN32))
2783 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2784 "cURL global init failed!\n");
2788 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2789 "Proxy listens on port %u\n",
2792 mhd_httpd_head = NULL;
2793 mhd_httpd_tail = NULL;
2794 total_mhd_connections = 0;
2796 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "gns-proxy",
2800 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2801 "Specify PROXY_UNIXPATH in gns-proxy config section!\n");
2805 mhd_unix_socket = GNUNET_NETWORK_socket_create (AF_UNIX,
2809 if (NULL == mhd_unix_socket)
2811 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2812 "Unable to create unix domain socket!\n");
2816 mhd_unix_sock_addr.sun_family = AF_UNIX;
2817 strcpy (mhd_unix_sock_addr.sun_path, proxy_sockfile);
2820 mhd_unix_sock_addr.sun_path[0] = '\0';
2822 #if HAVE_SOCKADDR_IN_SIN_LEN
2823 mhd_unix_sock_addr.sun_len = (u_char) sizeof (struct sockaddr_un);
2826 len = strlen (proxy_sockfile) + sizeof(AF_UNIX);
2828 GNUNET_free (proxy_sockfile);
2830 if (GNUNET_OK != GNUNET_NETWORK_socket_bind (mhd_unix_socket,
2831 (struct sockaddr*)&mhd_unix_sock_addr,
2834 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2835 "Unable to bind unix domain socket!\n");
2839 if (GNUNET_OK != GNUNET_NETWORK_socket_listen (mhd_unix_socket,
2842 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2843 "Unable to listen on unix domain socket!\n");
2847 hd = GNUNET_malloc (sizeof (struct MhdHttpList));
2848 hd->is_ssl = GNUNET_NO;
2849 strcpy (hd->domain, "");
2850 httpd = MHD_start_daemon (MHD_USE_DEBUG, 4444, //Dummy port
2852 &create_response, hd,
2853 MHD_OPTION_LISTEN_SOCKET, GNUNET_NETWORK_get_fd (mhd_unix_socket),
2854 MHD_OPTION_CONNECTION_LIMIT, MHD_MAX_CONNECTIONS,
2855 MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) 16,
2856 MHD_OPTION_NOTIFY_COMPLETED,
2860 GNUNET_assert (httpd != NULL);
2862 hd->httpd_task = GNUNET_SCHEDULER_NO_TASK;
2864 GNUNET_CONTAINER_DLL_insert (mhd_httpd_head, mhd_httpd_tail, hd);
2868 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
2869 &do_shutdown, NULL);
2875 * The main function for gnunet-gns-proxy.
2877 * @param argc number of arguments from the command line
2878 * @param argv command line arguments
2879 * @return 0 ok, 1 on error
2882 main (int argc, char *const *argv)
2884 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
2886 gettext_noop ("listen on specified port"), 1,
2887 &GNUNET_GETOPT_set_string, &port},
2888 {'a', "authority", NULL,
2889 gettext_noop ("pem file to use as CA"), 1,
2890 &GNUNET_GETOPT_set_string, &cafile_opt},
2891 GNUNET_GETOPT_OPTION_END
2896 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
2900 GNUNET_log_setup ("gnunet-gns-proxy", "WARNING", NULL);
2903 GNUNET_PROGRAM_run (argc, argv, "gnunet-gns-proxy",
2904 _("GNUnet GNS proxy"),
2906 &run, NULL)) ? 0 : 1;
2907 GNUNET_free_non_null ((char*)argv);