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);
622 * Task run whenever HTTP server operations are pending.
625 * @param tc sched context
629 const struct GNUNET_SCHEDULER_TaskContext *tc);
632 * Task that simply runs MHD main loop
634 * @param cls NULL --- FIXME: interesting...
635 * @param tc task context
638 run_mhd (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
640 struct MhdHttpList *hd = cls;
642 //for (hd=mhd_httpd_head; hd != NULL; hd = hd->next)
643 MHD_run (hd->daemon);
648 run_mhd_now (struct MhdHttpList *hd)
650 GNUNET_SCHEDULER_cancel (hd->httpd_task);
651 hd->httpd_task = GNUNET_SCHEDULER_add_now (&do_httpd, hd);
655 * Ask cURL for the select sets and schedule download
658 curl_download_prepare ();
661 * Callback to free content
663 * @param cls content to free
664 * @param tc task context
667 mhd_content_free (struct ProxyCurlTask *ctask)
669 if (NULL != ctask->headers)
670 curl_slist_free_all (ctask->headers);
672 if (NULL != ctask->headers)
673 curl_slist_free_all (ctask->resolver);
675 if (NULL != ctask->response)
676 MHD_destroy_response (ctask->response);
683 * Shorten result callback
685 * @param cls the proxycurltask
686 * @param short_name the shortened name (NULL on error)
689 process_shorten (void* cls, const char* short_name)
691 struct ProxyCurlTask *ctask = cls;
693 char tmp[strlen(ctask->pp_buf)]; //TODO length
695 if (NULL == short_name)
697 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
698 "MHD PP: Unable to shorten %s\n",
703 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
704 "MHD PP: Shorten %s -> %s\n",
707 //this is evil.. what about https
708 sprintf (tmp, "href=\"http://%s", short_name);
709 strcpy (ctask->pp_buf, tmp);
711 ctask->pp_finished = GNUNET_YES;
713 GNUNET_SCHEDULER_add_now (&run_mhd, ctask->mhd);
718 * Callback for MHD response
721 * @param pos in buffer
723 * @param max space in buffer
724 * @return number of bytes written
727 mhd_content_cb (void *cls,
732 struct ProxyCurlTask *ctask = cls;
734 size_t bytes_to_copy;
737 regmatch_t m[RE_N_MATCHES];
739 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
740 "MHD: content cb %s\n", ctask->url);
742 if (ctask->download_successful &&
743 (ctask->buf_status == BUF_WAIT_FOR_CURL))
745 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
746 "MHD: sending response for %s\n", ctask->url);
747 ctask->download_in_progress = GNUNET_NO;
748 run_mhd_now (ctask->mhd);
749 GNUNET_SCHEDULER_add_now (&run_mhd, ctask->mhd);
750 mhd_content_free (ctask);
751 total_mhd_connections--;
752 return MHD_CONTENT_READER_END_OF_STREAM;
755 if (ctask->download_error &&
756 (ctask->buf_status == BUF_WAIT_FOR_CURL))
758 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
759 "MHD: sending error response\n");
760 ctask->download_in_progress = GNUNET_NO;
761 run_mhd_now (ctask->mhd);
762 mhd_content_free (ctask);
763 total_mhd_connections--;
764 return MHD_CONTENT_READER_END_WITH_ERROR;
767 if ( ctask->buf_status == BUF_WAIT_FOR_CURL )
770 bytes_to_copy = ctask->bytes_in_buffer;
772 if (ctask->parse_content == GNUNET_YES)
775 GNUNET_log ( GNUNET_ERROR_TYPE_DEBUG,
776 "MHD: We need to parse the HTML %s\n", ctask->buffer_ptr);
778 nomatch = regexec ( &re_dotplus,
779 ctask->buffer_ptr, RE_N_MATCHES, m, 0);
783 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
784 "MHD RE: No match\n");
788 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
791 GNUNET_assert (m[1].rm_so != -1);
793 hostptr = ctask->buffer_ptr+m[1].rm_so;
797 bytes_to_copy = m[0].rm_so;
798 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
799 "Copying %d bytes.\n", m[0].rm_so);
805 if (ctask->is_postprocessing == GNUNET_YES)
809 if ( ctask->pp_finished == GNUNET_NO )
811 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
812 "MHD PP: Waiting for PP of %s\n", ctask->pp_buf);
816 ctask->is_postprocessing = GNUNET_NO;
818 ctask->bytes_in_buffer -= m[0].rm_eo;//(m[1].rm_eo-m[1].rm_so);
819 ctask->buffer_ptr += m[0].rm_eo;//(m[1].rm_eo-m[1].rm_so);
820 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
821 "Skipping next %d bytes in buffer\n", m[0].rm_eo);
823 GNUNET_SCHEDULER_add_now (&run_mhd, ctask->mhd);
825 if ( strlen (ctask->pp_buf) <= max )
827 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
828 "Copying postprocessed %s.\n", ctask->pp_buf);
829 memcpy ( buf, ctask->pp_buf, strlen (ctask->pp_buf) );
830 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
832 ctask->is_postprocessing = GNUNET_NO;
833 return strlen (ctask->pp_buf);
839 memset (ctask->pp_buf, 0, sizeof(ctask->pp_buf));
841 /* If .+ extend with authority */
842 if (*(ctask->buffer_ptr+m[1].rm_eo) == '+')
844 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
846 memcpy (ctask->pp_buf, hostptr, (m[1].rm_eo-m[1].rm_so));
847 strcpy ( ctask->pp_buf+strlen(ctask->pp_buf),
850 /* If .zkey or .tld simply copy the name */
853 memcpy (ctask->pp_buf, hostptr, (m[3].rm_eo-m[1].rm_so));
854 if (is_tld (ctask->pp_buf, GNUNET_GNS_TLD_ZKEY) == GNUNET_NO)
856 if (0 == strcmp (ctask->pp_buf, ctask->leho))
858 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
860 memcpy (ctask->pp_buf,
861 ctask->buffer_ptr+m[0].rm_so,
862 m[1].rm_so-m[0].rm_so);
863 strcpy (ctask->pp_buf+(m[1].rm_so-m[0].rm_so),
865 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
866 "New tag: %s\n", ctask->pp_buf);
867 ctask->is_postprocessing = GNUNET_YES;
868 ctask->pp_finished = GNUNET_YES;
869 GNUNET_SCHEDULER_add_now (&run_mhd, ctask->mhd);
875 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
876 "Link %s is zkey\n", ctask->pp_buf);
879 ctask->is_postprocessing = GNUNET_YES;
880 ctask->pp_finished = GNUNET_NO;
882 GNUNET_GNS_shorten_zone (gns_handle,
888 ctask); // FIXME: use after free of 'ctask'?
895 if ( bytes_to_copy > max )
897 GNUNET_log ( GNUNET_ERROR_TYPE_DEBUG,
898 "MHD: buffer in response too small! (%s)\n",
900 memcpy ( buf, ctask->buffer_ptr, max);
901 ctask->bytes_in_buffer -= max;
902 ctask->buffer_ptr += max;
907 GNUNET_log ( GNUNET_ERROR_TYPE_DEBUG,
908 "MHD: copying %d bytes to mhd response at offset %d\n",
911 memcpy ( buf, ctask->buffer_ptr, bytes_to_copy );
912 copied = bytes_to_copy;
913 if (bytes_to_copy < ctask->bytes_in_buffer)
915 ctask->bytes_in_buffer -= bytes_to_copy;
916 ctask->buffer_ptr += bytes_to_copy;
920 ctask->bytes_in_buffer = 0;
921 ctask->buf_status = BUF_WAIT_FOR_CURL;
922 ctask->buffer_ptr = ctask->buffer;
923 if (NULL != ctask->curl)
924 curl_easy_pause (ctask->curl, CURLPAUSE_CONT);
925 GNUNET_SCHEDULER_add_now (&run_mhd, ctask->mhd);
929 GNUNET_SCHEDULER_add_now (&run_mhd, ctask->mhd);
935 * Process cURL download bits
937 * @param ptr buffer with data
938 * @param size size of a record
939 * @param nmemb number of records downloaded
941 * @return number of processed bytes
944 callback_download (void *ptr, size_t size, size_t nmemb, void *ctx)
946 const char *cbuf = ptr;
948 struct ProxyCurlTask *ctask = ctx;
950 if (NULL == ctask->response)
952 /* FIXME: get total size from curl (if available) */
953 ctask->response = MHD_create_response_from_callback (MHD_SIZE_UNKNOWN,
954 20 /* 20 bytes IO buffers!? */,
962 ctask->ready_to_queue = GNUNET_YES;
963 /*if (ctask->con_status == MHD_NO)
965 MHD_queue_response (ctask->connection,
968 ctask->con_status = MHD_YES;
977 if (total > sizeof (ctask->buffer))
979 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
980 "CURL gave us too much data to handle (%d)!\n",
985 if (ctask->buf_status == BUF_WAIT_FOR_MHD)
987 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
988 "CURL: Waiting for MHD (%s)\n", ctask->url);
989 return CURL_WRITEFUNC_PAUSE;
993 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
994 "CURL: Copying to MHD (%s, %d)\n", ctask->url, total);
995 memcpy (ctask->buffer, cbuf, total);
996 ctask->bytes_in_buffer = total;
997 ctask->buffer_ptr = ctask->buffer;
999 ctask->buf_status = BUF_WAIT_FOR_MHD;
1001 //GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1002 // "cURL chunk:\n%s\n", (char*)ctask->buffer);
1003 run_mhd_now (ctask->mhd);
1010 * Task that is run when we are ready to receive more data
1013 * @param cls closure
1014 * @param tc task context
1017 curl_task_download (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
1020 * Ask cURL for the select sets and schedule download
1023 curl_download_prepare ()
1030 struct GNUNET_NETWORK_FDSet *grs;
1031 struct GNUNET_NETWORK_FDSet *gws;
1033 struct GNUNET_TIME_Relative rtime;
1039 mret = curl_multi_fdset (curl_multi, &rs, &ws, &es, &max);
1041 if (mret != CURLM_OK)
1043 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1044 "%s failed at %s:%d: `%s'\n",
1045 "curl_multi_fdset", __FILE__, __LINE__,
1046 curl_multi_strerror (mret));
1047 //TODO cleanup here?
1051 mret = curl_multi_timeout (curl_multi, &to);
1052 rtime = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, to);
1054 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1055 "cURL multi fds: max=%d timeout=%llu\n", max, to);
1057 grs = GNUNET_NETWORK_fdset_create ();
1058 gws = GNUNET_NETWORK_fdset_create ();
1059 GNUNET_NETWORK_fdset_copy_native (grs, &rs, max + 1);
1060 GNUNET_NETWORK_fdset_copy_native (gws, &ws, max + 1);
1061 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1062 "Scheduling task cURL\n");
1064 if (curl_download_task != GNUNET_SCHEDULER_NO_TASK)
1065 GNUNET_SCHEDULER_cancel (curl_download_task);
1067 curl_download_task =
1068 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1071 &curl_task_download, curl_multi);
1072 GNUNET_NETWORK_fdset_destroy (gws);
1073 GNUNET_NETWORK_fdset_destroy (grs);
1079 * Task that is run when we are ready to receive more data
1082 * @param cls closure
1083 * @param tc task context
1086 curl_task_download (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1090 struct CURLMsg *msg;
1092 struct ProxyCurlTask *ctask;
1096 struct ProxyCurlTask *clean_head = NULL;
1097 struct ProxyCurlTask *clean_tail = NULL;
1099 curl_download_task = GNUNET_SCHEDULER_NO_TASK;
1101 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1103 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1104 "Shutdown requested while trying to download\n");
1108 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1116 mret = curl_multi_perform (curl_multi, &running);
1118 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1119 "Running curl tasks: %d\n", running);
1121 ctask = ctasks_head;
1122 for (; ctask != NULL; ctask = ctask->next)
1124 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1125 "CTask: %s\n", ctask->url);
1129 if (num_ctasks != running)
1131 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1132 "%d ctasks, %d curl running\n", num_ctasks, running);
1137 ctask = ctasks_head;
1138 msg = curl_multi_info_read (curl_multi, &msgnum);
1139 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1140 "Messages left: %d\n", msgnum);
1147 if ((msg->data.result != CURLE_OK) &&
1148 (msg->data.result != CURLE_GOT_NOTHING))
1150 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1151 "Download curl failed");
1153 for (; ctask != NULL; ctask = ctask->next)
1155 if (NULL == ctask->curl)
1158 if (memcmp (msg->easy_handle, ctask->curl, sizeof (CURL)) != 0)
1161 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1162 "Download curl failed for task %s: %s.\n",
1164 curl_easy_strerror (msg->data.result));
1165 ctask->download_successful = GNUNET_NO;
1166 ctask->download_error = GNUNET_YES;
1167 if (CURLE_OK == curl_easy_getinfo (ctask->curl,
1168 CURLINFO_RESPONSE_CODE,
1170 ctask->curl_response_code = resp_code;
1171 ctask->ready_to_queue = MHD_YES;
1172 GNUNET_SCHEDULER_add_now (&run_mhd, ctask->mhd);
1174 GNUNET_CONTAINER_DLL_remove (ctasks_head, ctasks_tail,
1176 GNUNET_CONTAINER_DLL_insert (clean_head, clean_tail, ctask);
1179 GNUNET_assert (ctask != NULL);
1183 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1184 "cURL download completed.\n");
1186 for (; ctask != NULL; ctask = ctask->next)
1188 if (NULL == ctask->curl)
1191 if (memcmp (msg->easy_handle, ctask->curl, sizeof (CURL)) != 0)
1194 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1195 "cURL task %s found.\n", ctask->url);
1196 if (CURLE_OK == curl_easy_getinfo (ctask->curl,
1197 CURLINFO_RESPONSE_CODE,
1199 ctask->curl_response_code = resp_code;
1200 ctask->ready_to_queue = MHD_YES;
1201 ctask->download_successful = GNUNET_YES;
1203 GNUNET_SCHEDULER_add_now (&run_mhd, ctask->mhd);
1204 GNUNET_CONTAINER_DLL_remove (ctasks_head, ctasks_tail,
1206 GNUNET_CONTAINER_DLL_insert (clean_head, clean_tail, ctask);
1210 GNUNET_assert (ctask != NULL);
1212 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1213 "curl end %s\n", curl_easy_strerror(msg->data.result));
1219 } while (msgnum > 0);
1221 for (ctask=clean_head; ctask != NULL; ctask = ctask->next)
1223 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1224 "Removing cURL task %s.\n", ctask->url);
1225 curl_multi_remove_handle (curl_multi, ctask->curl);
1226 curl_easy_cleanup (ctask->curl);
1231 for (ctask=ctasks_head; ctask != NULL; ctask = ctask->next)
1236 if (num_ctasks != running)
1238 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1239 "%d ctasks, %d curl running\n", num_ctasks, running);
1242 GNUNET_assert ( num_ctasks == running );
1246 } while (mret == CURLM_CALL_MULTI_PERFORM);
1249 if (mret != CURLM_OK)
1251 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "%s failed at %s:%d: `%s'\n",
1252 "curl_multi_perform", __FILE__, __LINE__,
1253 curl_multi_strerror (mret));
1255 curl_download_prepare();
1259 * Process LEHO lookup
1261 * @param cls the ctask
1262 * @param rd_count number of records returned
1263 * @param rd record data
1266 process_leho_lookup (void *cls,
1268 const struct GNUNET_NAMESTORE_RecordData *rd)
1270 struct ProxyCurlTask *ctask = cls;
1271 char hosthdr[262]; //256 + "Host: "
1275 struct hostent *phost;
1277 char resolvename[512];
1280 strcpy (ctask->leho, "");
1283 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1284 "No LEHO present!\n");
1286 for (i=0; i<rd_count; i++)
1288 if (rd[i].record_type != GNUNET_GNS_RECORD_LEHO)
1291 memcpy (ctask->leho, rd[i].data, rd[i].data_size);
1293 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1294 "Found LEHO %s for %s\n", ctask->leho, ctask->url);
1297 if (0 != strcmp (ctask->leho, ""))
1299 sprintf (hosthdr, "%s%s", "Host: ", ctask->leho);
1300 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1301 "New HTTP header value: %s\n", hosthdr);
1302 ctask->headers = curl_slist_append (ctask->headers, hosthdr);
1303 GNUNET_assert (NULL != ctask->headers);
1304 ret = curl_easy_setopt (ctask->curl, CURLOPT_HTTPHEADER, ctask->headers);
1305 if (CURLE_OK != ret)
1307 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "%s failed at %s:%d: `%s'\n",
1308 "curl_easy_setopt", __FILE__, __LINE__, curl_easy_strerror(ret));
1313 if (ctask->mhd->is_ssl)
1315 phost = (struct hostent*)gethostbyname (ctask->host);
1319 ssl_ip = inet_ntoa(*((struct in_addr*)(phost->h_addr)));
1320 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1321 "SSL target server: %s\n", ssl_ip);
1322 sprintf (resolvename, "%s:%d:%s", ctask->leho, HTTPS_PORT, ssl_ip);
1323 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1324 "Curl resolve: %s\n", resolvename);
1325 ctask->resolver = curl_slist_append ( ctask->resolver, resolvename);
1326 curl_easy_setopt (ctask->curl, CURLOPT_RESOLVE, ctask->resolver);
1327 sprintf (curlurl, "https://%s%s", ctask->leho, ctask->url);
1328 curl_easy_setopt (ctask->curl, CURLOPT_URL, curlurl);
1332 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1333 "gethostbyname failed for %s!\n", ctask->host);
1334 ctask->download_successful = GNUNET_NO;
1335 ctask->download_error = GNUNET_YES;
1340 if (CURLM_OK != (mret=curl_multi_add_handle (curl_multi, ctask->curl)))
1342 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1343 "%s failed at %s:%d: `%s'\n",
1344 "curl_multi_add_handle", __FILE__, __LINE__,
1345 curl_multi_strerror (mret));
1346 ctask->download_successful = GNUNET_NO;
1347 ctask->download_error = GNUNET_YES;
1350 GNUNET_CONTAINER_DLL_insert (ctasks_head, ctasks_tail, ctask);
1352 curl_download_prepare ();
1357 * Initialize download and trigger curl
1359 * @param cls the proxycurltask
1360 * @param auth_name the name of the authority (site of origin) of ctask->host
1364 process_get_authority (void *cls,
1365 const char* auth_name)
1367 struct ProxyCurlTask *ctask = cls;
1369 if (NULL == auth_name)
1371 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1372 "Get authority failed!\n");
1373 strcpy (ctask->authority, "");
1377 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1378 "Get authority yielded %s\n", auth_name);
1379 strcpy (ctask->authority, auth_name);
1382 GNUNET_GNS_lookup_zone (gns_handle,
1385 GNUNET_GNS_RECORD_LEHO,
1386 GNUNET_YES, //Only cached for performance
1388 &process_leho_lookup,
1394 * Main MHD callback for handling requests.
1397 * @param con MHD connection handle
1398 * @param url the url in the request
1399 * @param meth the HTTP method used ("GET", "PUT", etc.)
1400 * @param ver the HTTP version string (i.e. "HTTP/1.1")
1401 * @param upload_data the data being uploaded (excluding HEADERS,
1402 * for a POST that fits into memory and that is encoded
1403 * with a supported encoding, the POST data will NOT be
1404 * given in upload_data and is instead available as
1405 * part of MHD_get_connection_values; very large POST
1406 * data *will* be made available incrementally in
1408 * @param upload_data_size set initially to the size of the
1409 * upload_data provided; the method must update this
1410 * value to the number of bytes NOT processed;
1411 * @param con_cls pointer to location where we store the 'struct Request'
1412 * @return MHD_YES if the connection was handled successfully,
1413 * MHD_NO if the socket must be closed due to a serious
1414 * error while handling the request
1417 create_response (void *cls,
1418 struct MHD_Connection *con,
1422 const char *upload_data,
1423 size_t *upload_data_size,
1426 struct MhdHttpList* hd = cls;
1427 const char* page = "<html><head><title>gnoxy</title>"\
1428 "</head><body>cURL fail</body></html>";
1430 char curlurl[512]; // buffer overflow!
1433 struct ProxyCurlTask *ctask;
1436 if (0 != strcasecmp (meth, MHD_HTTP_METHOD_GET))
1438 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1439 "%s NOT IMPLEMENTED!\n", meth);
1443 if (0 != *upload_data_size)
1446 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1450 if (NULL == *con_cls)
1452 ctask = GNUNET_malloc (sizeof (struct ProxyCurlTask));
1456 // FIXME: probably not best here, also never free'ed...
1457 if (NULL == curl_multi)
1458 curl_multi = curl_multi_init ();
1460 ctask->curl = curl_easy_init();
1462 if ((NULL == ctask->curl) || (NULL == curl_multi)) // ugh, curl_multi init failure check MUCH earlier
1464 ctask->response = MHD_create_response_from_buffer (strlen (page),
1466 MHD_RESPMEM_PERSISTENT);
1467 ret = MHD_queue_response (con,
1470 MHD_destroy_response (ctask->response);
1471 GNUNET_free (ctask);
1475 ctask->download_in_progress = GNUNET_YES;
1476 ctask->buf_status = BUF_WAIT_FOR_CURL;
1477 ctask->connection = con;
1478 ctask->curl_response_code = MHD_HTTP_OK;
1480 MHD_get_connection_values (con,
1482 &con_val_iter, ctask);
1484 curl_easy_setopt (ctask->curl, CURLOPT_HEADERFUNCTION, &curl_check_hdr);
1485 curl_easy_setopt (ctask->curl, CURLOPT_HEADERDATA, ctask);
1486 curl_easy_setopt (ctask->curl, CURLOPT_WRITEFUNCTION, &callback_download);
1487 curl_easy_setopt (ctask->curl, CURLOPT_WRITEDATA, ctask);
1488 curl_easy_setopt (ctask->curl, CURLOPT_FOLLOWLOCATION, 0);
1489 //curl_easy_setopt (ctask->curl, CURLOPT_MAXREDIRS, 4);
1490 /* no need to abort if the above failed */
1491 if (GNUNET_NO == ctask->mhd->is_ssl)
1493 sprintf (curlurl, "http://%s%s", ctask->host, url);
1494 MHD_get_connection_values (con,
1495 MHD_GET_ARGUMENT_KIND,
1496 &get_uri_val_iter, curlurl);
1497 curl_easy_setopt (ctask->curl, CURLOPT_URL, curlurl);
1498 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1499 "Adding new curl task for %s\n", curlurl);
1501 strcpy (ctask->url, url);
1502 MHD_get_connection_values (con,
1503 MHD_GET_ARGUMENT_KIND,
1504 &get_uri_val_iter, ctask->url);
1506 //curl_easy_setopt (ctask->curl, CURLOPT_URL, curlurl);
1507 curl_easy_setopt (ctask->curl, CURLOPT_FAILONERROR, 1);
1508 curl_easy_setopt (ctask->curl, CURLOPT_CONNECTTIMEOUT, 600L);
1509 curl_easy_setopt (ctask->curl, CURLOPT_TIMEOUT, 600L);
1511 GNUNET_GNS_get_authority (gns_handle,
1513 &process_get_authority,
1515 ctask->ready_to_queue = GNUNET_NO;
1516 ctask->fin = GNUNET_NO;
1520 ctask = (struct ProxyCurlTask *) *con_cls;
1521 GNUNET_break (GNUNET_YES != ctask->fin);
1522 if (GNUNET_YES != ctask->ready_to_queue)
1523 return MHD_YES; /* wait longer */
1524 ctask->fin = GNUNET_YES;
1525 ret = MHD_queue_response (con, ctask->curl_response_code, ctask->response);
1526 run_mhd_now (ctask->mhd);
1537 struct MhdHttpList *hd;
1539 for (hd=mhd_httpd_head; hd != NULL; hd = hd->next)
1547 * @param hd the daemon to run
1550 run_httpd (struct MhdHttpList *hd)
1555 struct GNUNET_NETWORK_FDSet *wrs;
1556 struct GNUNET_NETWORK_FDSet *wws;
1557 struct GNUNET_NETWORK_FDSet *wes;
1560 unsigned MHD_LONG_LONG timeout;
1561 struct GNUNET_TIME_Relative tv;
1566 wrs = GNUNET_NETWORK_fdset_create ();
1567 wes = GNUNET_NETWORK_fdset_create ();
1568 wws = GNUNET_NETWORK_fdset_create ();
1570 GNUNET_assert (MHD_YES == MHD_get_fdset (hd->daemon, &rs, &ws, &es, &max));
1573 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1574 "MHD fds: max=%d\n", max);
1576 haveto = MHD_get_timeout (hd->daemon, &timeout);
1578 if (haveto == MHD_YES)
1579 tv.rel_value = (uint64_t) timeout;
1581 tv = GNUNET_TIME_UNIT_FOREVER_REL;
1582 GNUNET_NETWORK_fdset_copy_native (wrs, &rs, max + 1);
1583 GNUNET_NETWORK_fdset_copy_native (wws, &ws, max + 1);
1584 GNUNET_NETWORK_fdset_copy_native (wes, &es, max + 1);
1586 if (hd->httpd_task != GNUNET_SCHEDULER_NO_TASK)
1587 GNUNET_SCHEDULER_cancel (hd->httpd_task);
1589 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_HIGH,
1592 GNUNET_NETWORK_fdset_destroy (wrs);
1593 GNUNET_NETWORK_fdset_destroy (wws);
1594 GNUNET_NETWORK_fdset_destroy (wes);
1599 * Task run whenever HTTP server operations are pending.
1602 * @param tc sched context
1605 do_httpd (void *cls,
1606 const struct GNUNET_SCHEDULER_TaskContext *tc)
1608 struct MhdHttpList *hd = cls;
1610 hd->httpd_task = GNUNET_SCHEDULER_NO_TASK;
1611 MHD_run (hd->daemon);
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 * Read file in filename
1824 * @param filename file to read
1825 * @param size pointer where filesize is stored
1829 load_file (const char* filename,
1836 GNUNET_DISK_file_size (filename, &fsize,
1837 GNUNET_YES, GNUNET_YES))
1839 if (fsize > MAX_PEM_SIZE)
1841 *size = (unsigned int) fsize;
1842 buffer = GNUNET_malloc (*size);
1843 if (fsize != GNUNET_DISK_fn_read (filename, buffer, (size_t) fsize))
1845 GNUNET_free (buffer);
1853 * Load PEM key from file
1855 * @param key where to store the data
1856 * @param keyfile path to the PEM file
1857 * @return GNUNET_OK on success
1860 load_key_from_file (gnutls_x509_privkey_t key, const char* keyfile)
1862 gnutls_datum_t key_data;
1865 key_data.data = (unsigned char*) load_file (keyfile, &key_data.size);
1866 ret = gnutls_x509_privkey_import (key, &key_data,
1867 GNUTLS_X509_FMT_PEM);
1868 if (GNUTLS_E_SUCCESS != ret)
1870 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1871 _("Unable to import private key from file `%s'\n"),
1875 GNUNET_free (key_data.data);
1876 return (GNUTLS_E_SUCCESS != ret) ? GNUNET_SYSERR : GNUNET_OK;
1881 * Load cert from file
1883 * @param crt struct to store data in
1884 * @param certfile path to pem file
1885 * @return GNUNET_OK on success
1888 load_cert_from_file (gnutls_x509_crt_t crt, char* certfile)
1890 gnutls_datum_t cert_data;
1891 cert_data.data = NULL;
1894 cert_data.data = (unsigned char*) load_file (certfile, &cert_data.size);
1895 ret = gnutls_x509_crt_import (crt, &cert_data,
1896 GNUTLS_X509_FMT_PEM);
1897 if (GNUTLS_E_SUCCESS != ret)
1899 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1900 _("Unable to import certificate %s\n"), certfile);
1903 GNUNET_free (cert_data.data);
1904 return (GNUTLS_E_SUCCESS != ret) ? GNUNET_SYSERR : GNUNET_OK;
1909 * Generate new certificate for specific name
1911 * @param name the subject name to generate a cert for
1912 * @return a struct holding the PEM data
1914 static struct ProxyGNSCertificate *
1915 generate_gns_certificate (const char *name)
1919 unsigned int serial;
1920 size_t key_buf_size;
1921 size_t cert_buf_size;
1922 gnutls_x509_crt_t request;
1926 ret = gnutls_x509_crt_init (&request);
1928 if (GNUTLS_E_SUCCESS != ret)
1933 ret = gnutls_x509_crt_set_key (request, proxy_ca.key);
1935 if (GNUTLS_E_SUCCESS != ret)
1940 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Generating cert\n");
1942 struct ProxyGNSCertificate *pgc =
1943 GNUNET_malloc (sizeof (struct ProxyGNSCertificate));
1945 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Adding DNs\n");
1947 gnutls_x509_crt_set_dn_by_oid (request, GNUTLS_OID_X520_COUNTRY_NAME,
1950 gnutls_x509_crt_set_dn_by_oid (request, GNUTLS_OID_X520_ORGANIZATION_NAME,
1953 gnutls_x509_crt_set_dn_by_oid (request, GNUTLS_OID_X520_COMMON_NAME,
1954 0, name, strlen (name));
1956 ret = gnutls_x509_crt_set_version (request, 3);
1958 ret = gnutls_rnd (GNUTLS_RND_NONCE, &serial, sizeof (serial));
1960 etime = time (NULL);
1961 tm_data = localtime (&etime);
1964 ret = gnutls_x509_crt_set_serial (request,
1968 ret = gnutls_x509_crt_set_activation_time (request,
1971 etime = mktime (tm_data);
1978 ret = gnutls_x509_crt_set_expiration_time (request,
1980 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Signing...\n");
1982 ret = gnutls_x509_crt_sign (request, proxy_ca.cert, proxy_ca.key);
1984 key_buf_size = sizeof (pgc->key);
1985 cert_buf_size = sizeof (pgc->cert);
1987 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Exporting certificate...\n");
1989 gnutls_x509_crt_export (request, GNUTLS_X509_FMT_PEM,
1990 pgc->cert, &cert_buf_size);
1992 gnutls_x509_privkey_export (proxy_ca.key, GNUTLS_X509_FMT_PEM,
1993 pgc->key, &key_buf_size);
1996 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Cleaning up\n");
1997 gnutls_x509_crt_deinit (request);
2005 * Accept policy for mhdaemons
2008 * @param addr the sockaddr
2009 * @param addrlen the sockaddr length
2010 * @return MHD_NO if sockaddr is wrong or #conns too high
2013 accept_cb (void* cls, const struct sockaddr *addr, socklen_t addrlen)
2015 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2016 "In MHD accept policy cb\n");
2020 if (addr->sa_family == AF_UNIX)
2024 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2025 "Connection accepted\n");
2032 * Adds a socket to an SSL MHD instance
2033 * It is important the the domain name is
2034 * correct. In most cases we need to start a new daemon
2036 * @param h the handle to add to a daemon
2037 * @param domain the domain the ssl daemon has to serve
2038 * @return MHD_YES on success
2041 add_handle_to_ssl_mhd (struct GNUNET_NETWORK_Handle *h, const char* domain)
2043 struct MhdHttpList *hd = NULL;
2044 struct ProxyGNSCertificate *pgc;
2045 struct NetworkHandleList *nh;
2047 for (hd = mhd_httpd_head; NULL != hd; hd = hd->next)
2048 if (0 == strcmp (hd->domain, domain))
2053 pgc = generate_gns_certificate (domain);
2055 hd = GNUNET_malloc (sizeof (struct MhdHttpList));
2056 hd->is_ssl = GNUNET_YES;
2057 strcpy (hd->domain, domain);
2058 hd->proxy_cert = pgc;
2061 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2062 "No previous SSL instance found... starting new one for %s\n",
2065 hd->daemon = MHD_start_daemon (MHD_USE_DEBUG | MHD_USE_SSL, 0,
2067 &create_response, hd,
2068 MHD_OPTION_LISTEN_SOCKET, GNUNET_NETWORK_get_fd (mhd_unix_socket),
2069 MHD_OPTION_CONNECTION_LIMIT, MHD_MAX_CONNECTIONS,
2070 MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) 16,
2071 MHD_OPTION_NOTIFY_COMPLETED,
2073 MHD_OPTION_HTTPS_MEM_KEY, pgc->key,
2074 MHD_OPTION_HTTPS_MEM_CERT, pgc->cert,
2077 GNUNET_assert (hd->daemon != NULL);
2078 hd->httpd_task = GNUNET_SCHEDULER_NO_TASK;
2080 GNUNET_CONTAINER_DLL_insert (mhd_httpd_head, mhd_httpd_tail, hd);
2083 nh = GNUNET_malloc (sizeof (struct NetworkHandleList));
2086 GNUNET_CONTAINER_DLL_insert (hd->socket_handles_head,
2087 hd->socket_handles_tail,
2090 return add_handle_to_mhd (h, hd->daemon);
2096 * Read data from incoming connection
2098 * @param cls the closure
2099 * @param tc the scheduler context
2102 do_read (void* cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2104 struct Socks5Request *s5r = cls;
2105 struct socks5_client_hello *c_hello;
2106 struct socks5_server_hello *s_hello;
2107 struct socks5_client_request *c_req;
2108 struct socks5_server_response *s_resp;
2114 struct hostent *phost;
2116 struct sockaddr_in remote_addr;
2117 struct in_addr *r_sin_addr;
2119 struct NetworkHandleList *nh;
2121 s5r->rtask = GNUNET_SCHEDULER_NO_TASK;
2123 if ((NULL != tc->write_ready) &&
2124 (GNUNET_NETWORK_fdset_isset (tc->read_ready, s5r->sock)) &&
2125 (s5r->rbuf_len = GNUNET_NETWORK_socket_recv (s5r->sock, s5r->rbuf,
2126 sizeof (s5r->rbuf))))
2128 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2129 "Successfully read %d bytes from socket\n",
2134 if (s5r->rbuf_len != 0)
2135 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "read");
2137 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client disco!\n");
2139 if (s5r->fwdrtask != GNUNET_SCHEDULER_NO_TASK)
2140 GNUNET_SCHEDULER_cancel (s5r->fwdrtask);
2141 if (s5r->wtask != GNUNET_SCHEDULER_NO_TASK)
2142 GNUNET_SCHEDULER_cancel (s5r->wtask);
2143 if (s5r->fwdwtask != GNUNET_SCHEDULER_NO_TASK)
2144 GNUNET_SCHEDULER_cancel (s5r->fwdwtask);
2145 GNUNET_NETWORK_socket_close (s5r->remote_sock);
2146 GNUNET_NETWORK_socket_close (s5r->sock);
2151 if (s5r->state == SOCKS5_INIT)
2153 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2155 c_hello = (struct socks5_client_hello*)&s5r->rbuf;
2157 GNUNET_assert (c_hello->version == SOCKS_VERSION_5);
2159 s_hello = (struct socks5_server_hello*)&s5r->wbuf;
2160 s5r->wbuf_len = sizeof( struct socks5_server_hello );
2162 s_hello->version = c_hello->version;
2163 s_hello->auth_method = SOCKS_AUTH_NONE;
2165 /* Write response to client */
2166 s5r->wtask = GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
2170 s5r->rtask = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
2174 s5r->state = SOCKS5_REQUEST;
2178 if (s5r->state == SOCKS5_REQUEST)
2180 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2181 "Processing SOCKS5 request\n");
2182 c_req = (struct socks5_client_request*)&s5r->rbuf;
2183 s_resp = (struct socks5_server_response*)&s5r->wbuf;
2184 //Only 10byte for ipv4 response!
2185 s5r->wbuf_len = 10;//sizeof (struct socks5_server_response);
2187 GNUNET_assert (c_req->addr_type == 3);
2189 dom_len = *((uint8_t*)(&(c_req->addr_type) + 1));
2190 memset(domain, 0, sizeof(domain));
2191 strncpy(domain, (char*)(&(c_req->addr_type) + 2), dom_len);
2192 req_port = *((uint16_t*)(&(c_req->addr_type) + 2 + dom_len));
2194 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2195 "Requested connection is %s:%d\n",
2199 if (is_tld (domain, GNUNET_GNS_TLD) ||
2200 is_tld (domain, GNUNET_GNS_TLD_ZKEY))
2202 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2203 "Requested connection is gnunet tld\n",
2207 if (ntohs(req_port) == HTTPS_PORT)
2209 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2210 "Requested connection is HTTPS\n");
2211 ret = add_handle_to_ssl_mhd ( s5r->sock, domain );
2213 else if (NULL != httpd)
2215 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2216 "Requested connection is HTTP\n");
2217 nh = GNUNET_malloc (sizeof (struct NetworkHandleList));
2220 GNUNET_CONTAINER_DLL_insert (mhd_httpd_head->socket_handles_head,
2221 mhd_httpd_head->socket_handles_tail,
2224 ret = add_handle_to_mhd ( s5r->sock, httpd );
2229 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2230 _("Failed to start HTTP server\n"));
2231 s_resp->version = 0x05;
2232 s_resp->reply = 0x01;
2233 s5r->cleanup = GNUNET_YES;
2234 s5r->cleanup_sock = GNUNET_YES;
2236 GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
2242 /* Signal success */
2243 s_resp->version = 0x05;
2244 s_resp->reply = 0x00;
2245 s_resp->reserved = 0x00;
2246 s_resp->addr_type = 0x01;
2248 s5r->cleanup = GNUNET_YES;
2249 s5r->cleanup_sock = GNUNET_NO;
2251 GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
2259 phost = (struct hostent*)gethostbyname (domain);
2262 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2263 "Resolve %s error!\n", domain );
2264 s_resp->version = 0x05;
2265 s_resp->reply = 0x01;
2266 s5r->cleanup = GNUNET_YES;
2267 s5r->cleanup_sock = GNUNET_YES;
2269 GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
2275 s5r->remote_sock = GNUNET_NETWORK_socket_create (AF_INET,
2278 r_sin_addr = (struct in_addr*)(phost->h_addr);
2279 remote_ip = r_sin_addr->s_addr;
2280 memset(&remote_addr, 0, sizeof(remote_addr));
2281 remote_addr.sin_family = AF_INET;
2282 #if HAVE_SOCKADDR_IN_SIN_LEN
2283 remote_addr.sin_len = sizeof (remote_addr);
2285 remote_addr.sin_addr.s_addr = remote_ip;
2286 remote_addr.sin_port = req_port;
2288 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2289 "target server: %s:%u\n", inet_ntoa(remote_addr.sin_addr),
2293 GNUNET_NETWORK_socket_connect ( s5r->remote_sock,
2294 (const struct sockaddr*)&remote_addr,
2295 sizeof (remote_addr)))
2296 && (errno != EINPROGRESS))
2298 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "connect");
2299 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2300 "socket request error...\n");
2301 s_resp->version = 0x05;
2302 s_resp->reply = 0x01;
2304 GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
2311 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2312 "new remote connection\n");
2314 s_resp->version = 0x05;
2315 s_resp->reply = 0x00;
2316 s_resp->reserved = 0x00;
2317 s_resp->addr_type = 0x01;
2319 s5r->state = SOCKS5_DATA_TRANSFER;
2322 GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
2326 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
2334 if (s5r->state == SOCKS5_DATA_TRANSFER)
2336 if ((s5r->remote_sock == NULL) || (s5r->rbuf_len == 0))
2338 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2339 "Closing connection to client\n");
2340 if (s5r->rtask != GNUNET_SCHEDULER_NO_TASK)
2341 GNUNET_SCHEDULER_cancel (s5r->rtask);
2342 if (s5r->fwdwtask != GNUNET_SCHEDULER_NO_TASK)
2343 GNUNET_SCHEDULER_cancel (s5r->fwdwtask);
2344 if (s5r->fwdrtask != GNUNET_SCHEDULER_NO_TASK)
2345 GNUNET_SCHEDULER_cancel (s5r->fwdrtask);
2346 if (s5r->fwdrtask != GNUNET_SCHEDULER_NO_TASK)
2347 GNUNET_SCHEDULER_cancel (s5r->fwdrtask);
2349 if (s5r->remote_sock != NULL)
2350 GNUNET_NETWORK_socket_close (s5r->remote_sock);
2351 GNUNET_NETWORK_socket_close (s5r->sock);
2356 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2357 "forwarding %d bytes from client\n", s5r->rbuf_len);
2360 GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
2362 &do_write_remote, s5r);
2364 if (s5r->fwdrtask == GNUNET_SCHEDULER_NO_TASK)
2367 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
2369 &do_read_remote, s5r);
2375 //GNUNET_CONTAINER_DLL_remove (s5conns.head, s5conns.tail, s5r);
2381 * Accept new incoming connections
2383 * @param cls the closure
2384 * @param tc the scheduler context
2387 do_accept (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2389 struct GNUNET_NETWORK_Handle *s;
2390 struct Socks5Request *s5r;
2392 ltask = GNUNET_SCHEDULER_NO_TASK;
2393 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
2396 ltask = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
2400 s = GNUNET_NETWORK_socket_accept (lsock, NULL, NULL);
2404 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "accept");
2408 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2409 "Got an inbound connection, waiting for data\n");
2411 s5r = GNUNET_malloc (sizeof (struct Socks5Request));
2413 s5r->state = SOCKS5_INIT;
2414 s5r->wtask = GNUNET_SCHEDULER_NO_TASK;
2415 s5r->fwdwtask = GNUNET_SCHEDULER_NO_TASK;
2416 s5r->fwdrtask = GNUNET_SCHEDULER_NO_TASK;
2417 s5r->rtask = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
2420 //GNUNET_CONTAINER_DLL_insert (s5conns.head, s5conns.tail, s5r);
2425 * Task run on shutdown
2427 * @param cls closure
2428 * @param tc task context
2431 do_shutdown (void *cls,
2432 const struct GNUNET_SCHEDULER_TaskContext *tc)
2435 struct MhdHttpList *hd;
2436 struct MhdHttpList *tmp_hd;
2437 struct NetworkHandleList *nh;
2438 struct NetworkHandleList *tmp_nh;
2439 struct ProxyCurlTask *ctask;
2440 struct ProxyCurlTask *ctask_tmp;
2442 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2443 "Shutting down...\n");
2445 gnutls_global_deinit ();
2447 if (GNUNET_SCHEDULER_NO_TASK != curl_download_task)
2449 GNUNET_SCHEDULER_cancel (curl_download_task);
2450 curl_download_task = GNUNET_SCHEDULER_NO_TASK;
2453 for (hd = mhd_httpd_head; hd != NULL; hd = tmp_hd)
2457 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2458 "Stopping daemon\n");
2460 if (GNUNET_SCHEDULER_NO_TASK != hd->httpd_task)
2462 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2463 "Stopping select task %d\n",
2465 GNUNET_SCHEDULER_cancel (hd->httpd_task);
2466 hd->httpd_task = GNUNET_SCHEDULER_NO_TASK;
2469 if (NULL != hd->daemon)
2471 MHD_stop_daemon (hd->daemon);
2475 for (nh = hd->socket_handles_head; nh != NULL; nh = tmp_nh)
2479 GNUNET_NETWORK_socket_close (nh->h);
2484 if (NULL != hd->proxy_cert)
2486 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2487 "Free certificate\n");
2488 GNUNET_free (hd->proxy_cert);
2494 for (ctask=ctasks_head; ctask != NULL; ctask=ctask_tmp)
2496 ctask_tmp = ctask->next;
2498 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2499 "Cleaning up cURL task\n");
2501 if (ctask->curl != NULL)
2502 curl_easy_cleanup (ctask->curl);
2504 if (NULL != ctask->headers)
2505 curl_slist_free_all (ctask->headers);
2506 if (NULL != ctask->resolver)
2507 curl_slist_free_all (ctask->resolver);
2509 if (NULL != ctask->response)
2510 MHD_destroy_response (ctask->response);
2513 GNUNET_free (ctask);
2516 GNUNET_GNS_disconnect (gns_handle);
2521 * Compiles a regex for us
2523 * @param re ptr to re struct
2524 * @param rt the expression to compile
2525 * @return 0 on success
2528 compile_regex (regex_t *re, const char* rt)
2533 status = regcomp (re, rt, REG_EXTENDED|REG_NEWLINE);
2536 regerror (status, re, err, 1024);
2537 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2538 "Regex error compiling '%s': %s\n", rt, err);
2546 * Loads the users local zone key
2548 * @return GNUNET_YES on success
2551 load_local_zone_key (const struct GNUNET_CONFIGURATION_Handle *cfg)
2554 struct GNUNET_CRYPTO_RsaPrivateKey *key = NULL;
2555 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pkey;
2556 struct GNUNET_CRYPTO_ShortHashCode *zone = NULL;
2557 struct GNUNET_CRYPTO_ShortHashAsciiEncoded zonename;
2559 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "gns",
2560 "ZONEKEY", &keyfile))
2562 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2563 "Unable to load zone key config value!\n");
2567 if (GNUNET_NO == GNUNET_DISK_file_test (keyfile))
2569 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2570 "Unable to load zone key %s!\n", keyfile);
2571 GNUNET_free(keyfile);
2575 key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
2576 GNUNET_CRYPTO_rsa_key_get_public (key, &pkey);
2577 GNUNET_CRYPTO_short_hash(&pkey,
2578 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
2580 zone = &local_gns_zone;
2581 GNUNET_CRYPTO_short_hash_to_enc (zone, &zonename);
2582 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2583 "Using zone: %s!\n", &zonename);
2584 GNUNET_CRYPTO_rsa_key_free(key);
2585 GNUNET_free(keyfile);
2587 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "gns",
2588 "PRIVATE_ZONEKEY", &keyfile))
2590 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2591 "Unable to load private zone key config value!\n");
2595 if (GNUNET_NO == GNUNET_DISK_file_test (keyfile))
2597 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2598 "Unable to load private zone key %s!\n", keyfile);
2599 GNUNET_free(keyfile);
2603 key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
2604 GNUNET_CRYPTO_rsa_key_get_public (key, &pkey);
2605 GNUNET_CRYPTO_short_hash(&pkey,
2606 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
2607 &local_private_zone);
2608 GNUNET_CRYPTO_short_hash_to_enc (zone, &zonename);
2609 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2610 "Using private zone: %s!\n", &zonename);
2611 GNUNET_CRYPTO_rsa_key_free(key);
2612 GNUNET_free(keyfile);
2614 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "gns",
2615 "SHORTEN_ZONEKEY", &keyfile))
2617 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2618 "Unable to load shorten zone key config value!\n");
2622 if (GNUNET_NO == GNUNET_DISK_file_test (keyfile))
2624 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2625 "Unable to load shorten zone key %s!\n", keyfile);
2626 GNUNET_free(keyfile);
2630 key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
2631 GNUNET_CRYPTO_rsa_key_get_public (key, &pkey);
2632 GNUNET_CRYPTO_short_hash(&pkey,
2633 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
2634 &local_shorten_zone);
2635 GNUNET_CRYPTO_short_hash_to_enc (zone, &zonename);
2636 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2637 "Using shorten zone: %s!\n", &zonename);
2638 GNUNET_CRYPTO_rsa_key_free(key);
2639 GNUNET_free(keyfile);
2645 * Main function that will be run
2647 * @param cls closure
2648 * @param args remaining command-line arguments
2649 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
2650 * @param cfg configuration
2653 run (void *cls, char *const *args, const char *cfgfile,
2654 const struct GNUNET_CONFIGURATION_Handle *cfg)
2656 struct sockaddr_in sa;
2657 struct MhdHttpList *hd;
2658 struct sockaddr_un mhd_unix_sock_addr;
2660 char* proxy_sockfile;
2661 char* cafile_cfg = NULL;
2666 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2669 cafile = cafile_opt;
2673 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "gns-proxy",
2677 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2678 "Unable to load proxy CA config value!\n");
2679 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2680 "No proxy CA provided!\n");
2683 cafile = cafile_cfg;
2686 gnutls_global_init ();
2688 gnutls_x509_crt_init (&proxy_ca.cert);
2689 gnutls_x509_privkey_init (&proxy_ca.key);
2691 if ( (GNUNET_OK != load_cert_from_file (proxy_ca.cert, cafile)) ||
2692 (GNUNET_OK != load_key_from_file (proxy_ca.key, cafile)) )
2694 // FIXME: release resources...
2698 GNUNET_free_non_null (cafile_cfg);
2700 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2701 "Loading Template\n");
2703 compile_regex (&re_dotplus, (char*) RE_A_HREF);
2705 gns_handle = GNUNET_GNS_connect (cfg);
2707 if (GNUNET_NO == load_local_zone_key (cfg))
2709 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2710 "Unable to load zone!\n");
2714 if (NULL == gns_handle)
2716 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2717 "Unable to connect to GNS!\n");
2721 memset (&sa, 0, sizeof (sa));
2722 sa.sin_family = AF_INET;
2723 sa.sin_port = htons (port);
2724 #if HAVE_SOCKADDR_IN_SIN_LEN
2725 sa.sin_len = sizeof (sa);
2728 lsock = GNUNET_NETWORK_socket_create (AF_INET,
2732 if ((NULL == lsock) ||
2734 GNUNET_NETWORK_socket_bind (lsock, (const struct sockaddr *) &sa,
2737 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2738 "Failed to create listen socket bound to `%s'",
2739 GNUNET_a2s ((const struct sockaddr *) &sa, sizeof (sa)));
2741 GNUNET_NETWORK_socket_close (lsock);
2745 if (GNUNET_OK != GNUNET_NETWORK_socket_listen (lsock, 5))
2747 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2748 "Failed to listen on socket bound to `%s'",
2749 GNUNET_a2s ((const struct sockaddr *) &sa, sizeof (sa)));
2753 ltask = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
2754 lsock, &do_accept, NULL);
2759 if (0 != curl_global_init (CURL_GLOBAL_WIN32))
2761 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2762 "cURL global init failed!\n");
2766 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2767 "Proxy listens on port %u\n",
2770 mhd_httpd_head = NULL;
2771 mhd_httpd_tail = NULL;
2772 total_mhd_connections = 0;
2774 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "gns-proxy",
2778 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2779 "Specify PROXY_UNIXPATH in gns-proxy config section!\n");
2783 mhd_unix_socket = GNUNET_NETWORK_socket_create (AF_UNIX,
2787 if (NULL == mhd_unix_socket)
2789 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2790 "Unable to create unix domain socket!\n");
2794 mhd_unix_sock_addr.sun_family = AF_UNIX;
2795 strcpy (mhd_unix_sock_addr.sun_path, proxy_sockfile);
2798 mhd_unix_sock_addr.sun_path[0] = '\0';
2800 #if HAVE_SOCKADDR_IN_SIN_LEN
2801 mhd_unix_sock_addr.sun_len = (u_char) sizeof (struct sockaddr_un);
2804 len = strlen (proxy_sockfile) + sizeof(AF_UNIX);
2806 GNUNET_free (proxy_sockfile);
2808 if (GNUNET_OK != GNUNET_NETWORK_socket_bind (mhd_unix_socket,
2809 (struct sockaddr*)&mhd_unix_sock_addr,
2812 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2813 "Unable to bind unix domain socket!\n");
2817 if (GNUNET_OK != GNUNET_NETWORK_socket_listen (mhd_unix_socket,
2820 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2821 "Unable to listen on unix domain socket!\n");
2825 hd = GNUNET_malloc (sizeof (struct MhdHttpList));
2826 hd->is_ssl = GNUNET_NO;
2827 strcpy (hd->domain, "");
2828 httpd = MHD_start_daemon (MHD_USE_DEBUG, 4444, //Dummy port
2830 &create_response, hd,
2831 MHD_OPTION_LISTEN_SOCKET, GNUNET_NETWORK_get_fd (mhd_unix_socket),
2832 MHD_OPTION_CONNECTION_LIMIT, MHD_MAX_CONNECTIONS,
2833 MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) 16,
2834 MHD_OPTION_NOTIFY_COMPLETED,
2838 GNUNET_assert (httpd != NULL);
2840 hd->httpd_task = GNUNET_SCHEDULER_NO_TASK;
2842 GNUNET_CONTAINER_DLL_insert (mhd_httpd_head, mhd_httpd_tail, hd);
2846 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
2847 &do_shutdown, NULL);
2853 * The main function for gnunet-gns-proxy.
2855 * @param argc number of arguments from the command line
2856 * @param argv command line arguments
2857 * @return 0 ok, 1 on error
2860 main (int argc, char *const *argv)
2862 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
2864 gettext_noop ("listen on specified port"), 1,
2865 &GNUNET_GETOPT_set_string, &port},
2866 {'a', "authority", NULL,
2867 gettext_noop ("pem file to use as CA"), 1,
2868 &GNUNET_GETOPT_set_string, &cafile_opt},
2869 GNUNET_GETOPT_OPTION_END
2874 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
2878 GNUNET_log_setup ("gnunet-gns-proxy", "WARNING", NULL);
2881 GNUNET_PROGRAM_run (argc, argv, "gnunet-gns-proxy",
2882 _("GNUnet GNS proxy"),
2884 &run, NULL)) ? 0 : 1;
2885 GNUNET_free_non_null ((char*)argv);