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 "<a href=\"https?://(([A-Za-z0-9]+[.])+)([+]|zkey)"
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;
69 * Structure for GNS certificates
71 struct ProxyGNSCertificate
73 /* The certificate as PEM */
76 /* The private key as PEM */
82 * A structure for socks requests
86 /* The client socket */
87 struct GNUNET_NETWORK_Handle *sock;
89 /* The server socket */
90 struct GNUNET_NETWORK_Handle *remote_sock;
95 /* Client socket read task */
96 GNUNET_SCHEDULER_TaskIdentifier rtask;
98 /* Server socket read task */
99 GNUNET_SCHEDULER_TaskIdentifier fwdrtask;
101 /* Client socket write task */
102 GNUNET_SCHEDULER_TaskIdentifier wtask;
104 /* Server socket write task */
105 GNUNET_SCHEDULER_TaskIdentifier fwdwtask;
113 /* Length of data in read buffer */
114 unsigned int rbuf_len;
116 /* Length of data in write buffer */
117 unsigned int wbuf_len;
119 /* This handle is scheduled for cleanup? */
122 /* Shall we close the client socket on cleanup? */
127 * DLL for Network Handles
129 struct NetworkHandleList
132 struct NetworkHandleList *next;
135 struct NetworkHandleList *prev;
138 struct GNUNET_NETWORK_Handle *h;
142 * A structure for all running Httpds
147 struct MhdHttpList *prev;
150 struct MhdHttpList *next;
152 /* is this an ssl daemon? */
155 /* the domain name to server (only important for SSL) */
158 /* The daemon handle */
159 struct MHD_Daemon *daemon;
161 /* Optional proxy certificate used */
162 struct ProxyGNSCertificate *proxy_cert;
165 GNUNET_SCHEDULER_TaskIdentifier httpd_task;
167 /* Handles associated with this daemon */
168 struct NetworkHandleList *socket_handles_head;
170 /* Handles associated with this daemon */
171 struct NetworkHandleList *socket_handles_tail;
175 * A structure for MHD<->cURL streams
180 struct ProxyCurlTask *prev;
183 struct ProxyCurlTask *next;
188 /* Optional header replacements for curl (LEHO) */
189 struct curl_slist *headers;
191 /* Optional resolver replacements for curl (LEHO) */
192 struct curl_slist *resolver;
194 /* curl response code */
195 long curl_response_code;
197 /* The URL to fetch */
200 /* The cURL write buffer / MHD read buffer */
201 char buffer[CURL_MAX_WRITE_SIZE];
203 /* The pointer to the data in the buffer */
206 /* The buffer status (BUF_WAIT_FOR_CURL or BUF_WAIT_FOR_MHD) */
209 /* Number of bytes in buffer */
210 unsigned int bytes_in_buffer;
212 /* Indicates wheather the download is in progress */
213 int download_in_progress;
215 /* Indicates wheather the download was successful */
216 int download_successful;
218 /* Indicates wheather the download failed */
221 /* Indicates wheather we need to parse HTML */
224 /* Indicates wheather we are postprocessing the HTML right now */
225 int is_postprocessing;
227 /* Indicates wheather postprocessing has finished */
230 /* Task ID of the postprocessing task */
231 GNUNET_SCHEDULER_TaskIdentifier pp_task;
233 /* The postprocessing buffer TODO length? */
236 /* The authority of the corresponding host (site of origin) */
239 /* The hostname (Host header field) */
242 /* The LEgacy HOstname (can be empty) */
245 /* The associated daemon list entry */
246 struct MhdHttpList *mhd;
248 /* The associated response */
249 struct MHD_Response *response;
252 struct ProxySetCookieHeader *set_cookies_head;
255 struct ProxySetCookieHeader *set_cookies_tail;
257 /* connection status */
264 struct MHD_Connection *connection;
269 * Struct for set-cookies
271 struct ProxySetCookieHeader
274 struct ProxySetCookieHeader *next;
277 struct ProxySetCookieHeader *prev;
283 /* The port the proxy is running on (default 7777) */
284 static unsigned long port = GNUNET_GNS_PROXY_PORT;
286 /* The CA file (pem) to use for the proxy CA */
287 static char* cafile_opt;
289 /* The listen socket of the proxy */
290 static struct GNUNET_NETWORK_Handle *lsock;
292 /* The listen task ID */
293 GNUNET_SCHEDULER_TaskIdentifier ltask;
295 /* The cURL download task */
296 GNUNET_SCHEDULER_TaskIdentifier curl_download_task;
298 /* The non SSL httpd daemon handle */
299 static struct MHD_Daemon *httpd;
301 /* Number of current mhd connections */
302 static unsigned int total_mhd_connections;
304 /* The cURL multi handle */
305 static CURLM *curl_multi;
307 /* Handle to the GNS service */
308 static struct GNUNET_GNS_Handle *gns_handle;
310 /* DLL for ProxyCurlTasks */
311 static struct ProxyCurlTask *ctasks_head;
313 /* DLL for ProxyCurlTasks */
314 static struct ProxyCurlTask *ctasks_tail;
316 /* DLL for http daemons */
317 static struct MhdHttpList *mhd_httpd_head;
319 /* DLL for http daemons */
320 static struct MhdHttpList *mhd_httpd_tail;
322 /* Handle to the regex for dotplus (.+) replacement in HTML */
323 static regex_t re_dotplus;
325 /* The users local GNS zone hash */
326 static struct GNUNET_CRYPTO_ShortHashCode local_gns_zone;
328 /* The users local private zone */
329 static struct GNUNET_CRYPTO_ShortHashCode local_private_zone;
331 /* The users local shorten zone */
332 static struct GNUNET_CRYPTO_ShortHashCode local_shorten_zone;
334 /* The CA for SSL certificate generation */
335 static struct ProxyCA proxy_ca;
337 /* UNIX domain socket for mhd */
338 struct GNUNET_NETWORK_Handle *mhd_unix_socket;
340 /* Shorten zone private key */
341 struct GNUNET_CRYPTO_RsaPrivateKey *shorten_zonekey;
344 * Checks if name is in tld
346 * @param name the name to check
347 * @param tld the TLD to check for
348 * @return GNUNET_YES or GNUNET_NO
351 is_tld(const char* name, const char* tld)
355 if (strlen(name) <= strlen(tld))
360 offset = strlen(name)-strlen(tld);
361 if (strcmp (name+offset, tld) != 0)
363 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
364 "%s is not in .%s TLD\n", name, tld);
373 * Read HTTP request header field 'Host'
375 * @param cls buffer to write to
376 * @param kind value kind
377 * @param key field key
378 * @param value field value
379 * @return MHD_NO when Host found
382 con_val_iter (void *cls,
383 enum MHD_ValueKind kind,
387 struct ProxyCurlTask *ctask = (struct ProxyCurlTask *) cls;
388 char* buf = ctask->host;
392 if (0 == strcmp ("Host", key))
398 if (0 == strcmp ("Accept-Encoding", key))
403 cstr = GNUNET_malloc (strlen (key) + strlen (hdr_val) + 3);
404 GNUNET_snprintf (cstr, strlen (key) + strlen (hdr_val) + 3,
405 "%s: %s", key, hdr_val);
407 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
408 "Client Header: %s\n", cstr);
410 ctask->headers = curl_slist_append (ctask->headers, cstr);
418 * Check HTTP response header for mime
420 * @param buffer curl buffer
421 * @param size curl blocksize
422 * @param nmemb curl blocknumber
424 * @return size of read bytes
427 curl_check_hdr (void *buffer, size_t size, size_t nmemb, void *cls)
429 size_t bytes = size * nmemb;
430 struct ProxyCurlTask *ctask = cls;
431 int html_mime_len = strlen (HTML_HDR_CONTENT);
432 int cookie_hdr_len = strlen (MHD_HTTP_HEADER_SET_COOKIE);
433 char hdr_mime[html_mime_len+1];
434 char hdr_generic[bytes+1];
435 char new_cookie_hdr[bytes+strlen (ctask->leho)+1];
444 if (html_mime_len <= bytes)
446 memcpy (hdr_mime, buffer, html_mime_len);
447 hdr_mime[html_mime_len] = '\0';
449 if (0 == strcmp (hdr_mime, HTML_HDR_CONTENT))
451 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
452 "Got HTML HTTP response header\n");
453 ctask->parse_content = GNUNET_YES;
457 if (cookie_hdr_len > bytes)
460 memcpy (hdr_generic, buffer, bytes);
461 hdr_generic[bytes] = '\0';
463 if (hdr_generic[bytes-1] == '\n')
464 hdr_generic[bytes-1] = '\0';
466 if (hdr_generic[bytes-2] == '\r')
467 hdr_generic[bytes-2] = '\0';
469 if (0 == memcmp (hdr_generic,
470 MHD_HTTP_HEADER_SET_COOKIE,
473 ndup = GNUNET_strdup (hdr_generic+cookie_hdr_len+1);
474 memset (new_cookie_hdr, 0, sizeof (new_cookie_hdr));
475 tok = strtok (ndup, ";");
477 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
478 "Looking for cookie in : %s\n", hdr_generic);
480 for (; tok != NULL; tok = strtok (NULL, ";"))
482 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
483 "Got Cookie token: %s\n", tok);
484 //memcpy (new_cookie_hdr+offset, tok, strlen (tok));
485 if (0 == memcmp (tok, " domain", strlen (" domain")))
487 cookie_domain = tok + strlen (" domain") + 1;
489 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
490 "Got Set-Cookie Domain: %s\n", cookie_domain);
492 if (strlen (cookie_domain) < strlen (ctask->leho))
494 delta_cdomain = strlen (ctask->leho) - strlen (cookie_domain);
495 if (0 == strcmp (cookie_domain, ctask->leho + (delta_cdomain)))
497 GNUNET_snprintf (new_cookie_hdr+offset,
498 sizeof (new_cookie_hdr),
499 " domain=%s", ctask->authority);
500 offset += strlen (" domain=") + strlen (ctask->authority);
501 new_cookie_hdr[offset] = ';';
506 else if (strlen (cookie_domain) == strlen (ctask->leho))
508 if (0 == strcmp (cookie_domain, ctask->leho))
510 GNUNET_snprintf (new_cookie_hdr+offset,
511 sizeof (new_cookie_hdr),
512 " domain=%s", ctask->host);
513 offset += strlen (" domain=") + strlen (ctask->host);
514 new_cookie_hdr[offset] = ';';
519 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
520 "Cookie domain invalid\n");
524 memcpy (new_cookie_hdr+offset, tok, strlen (tok));
525 offset += strlen (tok);
526 new_cookie_hdr[offset] = ';';
530 //memcpy (new_cookie_hdr+offset, tok, strlen (tok));
534 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
535 "Got Set-Cookie HTTP header %s\n", new_cookie_hdr);
537 //pch = GNUNET_malloc (sizeof (struct ProxySetCookieHeader));
538 //len = strlen (hdr_cookie) - cookie_hdr_len - 1;
539 //pch->cookie = GNUNET_malloc (len + 1);
540 //memset (pch->cookie, 0, len + 1);
541 //memcpy (pch->cookie, hdr_cookie+cookie_hdr_len+1, len);
542 //GNUNET_CONTAINER_DLL_insert (ctask->set_cookies_head,
543 // ctask->set_cookies_tail,
545 //pch = ctask->set_cookies_head;
546 //while (pch != NULL)
548 if (GNUNET_NO == MHD_add_response_header (ctask->response,
549 MHD_HTTP_HEADER_SET_COOKIE,
552 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
553 "MHD: Error adding set-cookie header field %s\n",
554 hdr_generic+cookie_hdr_len+1);
557 //GNUNET_free (pch->cookie);
558 //GNUNET_CONTAINER_DLL_remove (ctask->set_cookies_head,
559 // ctask->set_cookies_tail,
562 //pch = ctask->set_cookies_head;
566 ndup = GNUNET_strdup (hdr_generic);
567 hdr_type = strtok (ndup, ":");
569 if (hdr_type != NULL)
571 hdr_val = strtok (NULL, "");
574 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
575 "Trying to set %s: %s\n",
578 if (GNUNET_NO == MHD_add_response_header (ctask->response,
582 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
583 "MHD: Error adding %s header field %s\n",
597 * @param hd a http daemon list entry
600 run_httpd (struct MhdHttpList *hd);
612 * Task that simply runs MHD main loop
615 * @param tc task context
618 run_mhd (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
621 struct MhdHttpList *hd = cls;
623 //for (hd=mhd_httpd_head; hd != NULL; hd = hd->next)
624 MHD_run (hd->daemon);
629 * Process cURL download bits
631 * @param ptr buffer with data
632 * @param size size of a record
633 * @param nmemb number of records downloaded
635 * @return number of processed bytes
638 callback_download (void *ptr, size_t size, size_t nmemb, void *ctx)
640 const char *cbuf = ptr;
642 struct ProxyCurlTask *ctask = ctx;
645 ctask->ready_to_queue = GNUNET_YES;
646 /*if (ctask->con_status == MHD_NO)
648 MHD_queue_response (ctask->connection,
651 ctask->con_status = MHD_YES;
660 if (total > sizeof (ctask->buffer))
662 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
663 "CURL gave us too much data to handle (%d)!\n",
668 if (ctask->buf_status == BUF_WAIT_FOR_MHD)
670 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
671 "CURL: Waiting for MHD (%s)\n", ctask->url);
672 return CURL_WRITEFUNC_PAUSE;
676 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
677 "CURL: Copying to MHD (%s, %d)\n", ctask->url, total);
678 memcpy (ctask->buffer, cbuf, total);
679 ctask->bytes_in_buffer = total;
680 ctask->buffer_ptr = ctask->buffer;
682 ctask->buf_status = BUF_WAIT_FOR_MHD;
684 //GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
685 // "cURL chunk:\n%s\n", (char*)ctask->buffer);
686 //run_mhd (NULL, NULL);
687 GNUNET_SCHEDULER_add_now (&run_mhd, ctask->mhd);
692 * Ask cURL for the select sets and schedule download
695 curl_download_prepare ();
698 * Callback to free content
700 * @param cls content to free
701 * @param tc task context
704 mhd_content_free (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
706 struct ProxyCurlTask *ctask = cls;
708 if (NULL != ctask->headers)
709 curl_slist_free_all (ctask->headers);
711 if (NULL != ctask->headers)
712 curl_slist_free_all (ctask->resolver);
714 if (NULL != ctask->response)
715 MHD_destroy_response (ctask->response);
723 * Shorten result callback
725 * @param cls the proxycurltask
726 * @param short_name the shortened name (NULL on error)
729 process_shorten (void* cls, const char* short_name)
731 struct ProxyCurlTask *ctask = cls;
733 char tmp[strlen(ctask->pp_buf)]; //TODO length
735 if (NULL == short_name)
737 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
738 "MHD PP: Unable to shorten %s\n",
743 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
744 "MHD PP: Shorten %s -> %s\n",
748 sprintf (tmp, "<a href=\"http://%s", short_name);
749 strcpy (ctask->pp_buf, tmp);
751 ctask->pp_finished = GNUNET_YES;
753 GNUNET_SCHEDULER_add_now (&run_mhd, ctask->mhd);
758 * Callback for MHD response
761 * @param pos in buffer
763 * @param max space in buffer
764 * @return number of bytes written
767 mhd_content_cb (void *cls,
772 struct ProxyCurlTask *ctask = cls;
774 size_t bytes_to_copy;
777 regmatch_t m[RE_N_MATCHES];
779 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
780 "MHD: content cb %s\n", ctask->url);
782 if (ctask->download_successful &&
783 (ctask->buf_status == BUF_WAIT_FOR_CURL))
785 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
786 "MHD: sending response for %s\n", ctask->url);
787 ctask->download_in_progress = GNUNET_NO;
788 GNUNET_SCHEDULER_add_now (&mhd_content_free, ctask);
789 GNUNET_SCHEDULER_add_now (&run_mhd, ctask->mhd);
790 total_mhd_connections--;
791 return MHD_CONTENT_READER_END_OF_STREAM;
794 if (ctask->download_error &&
795 (ctask->buf_status == BUF_WAIT_FOR_CURL))
797 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
798 "MHD: sending error response\n");
799 ctask->download_in_progress = GNUNET_NO;
800 GNUNET_SCHEDULER_add_now (&mhd_content_free, ctask);
801 GNUNET_SCHEDULER_add_now (&run_mhd, ctask->mhd);
802 total_mhd_connections--;
803 return MHD_CONTENT_READER_END_WITH_ERROR;
806 if ( ctask->buf_status == BUF_WAIT_FOR_CURL )
809 bytes_to_copy = ctask->bytes_in_buffer;
811 if (ctask->parse_content == GNUNET_YES)
814 GNUNET_log ( GNUNET_ERROR_TYPE_DEBUG,
815 "MHD: We need to parse the HTML %s\n", ctask->buffer_ptr);
817 nomatch = regexec ( &re_dotplus, ctask->buffer_ptr, RE_N_MATCHES, m, 0);
821 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
822 "MHD RE: No match\n");
826 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
829 GNUNET_assert (m[1].rm_so != -1);
831 hostptr = ctask->buffer_ptr+m[1].rm_so;
835 bytes_to_copy = m[0].rm_so;
836 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
837 "Copying %d bytes.\n", m[0].rm_so);
843 if (ctask->is_postprocessing == GNUNET_YES)
847 if ( ctask->pp_finished == GNUNET_NO )
849 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
850 "MHD PP: Waiting for PP of %s\n", ctask->pp_buf);
854 ctask->is_postprocessing = GNUNET_NO;
856 ctask->bytes_in_buffer -= m[0].rm_eo;//(m[1].rm_eo-m[1].rm_so);
857 ctask->buffer_ptr += m[0].rm_eo;//(m[1].rm_eo-m[1].rm_so);
858 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
859 "Skipping next %d bytes in buffer\n", m[0].rm_eo);
861 GNUNET_SCHEDULER_add_now (&run_mhd, ctask->mhd);
863 if ( strlen (ctask->pp_buf) <= max )
865 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
866 "Copying postprocessed %s.\n", ctask->pp_buf);
867 memcpy ( buf, ctask->pp_buf, strlen (ctask->pp_buf) );
868 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
870 ctask->is_postprocessing = GNUNET_NO;
871 return strlen (ctask->pp_buf);
877 memset (ctask->pp_buf, 0, sizeof(ctask->pp_buf));
879 /* If .+ extend with authority */
880 if (*(ctask->buffer_ptr+m[1].rm_eo) == '+')
882 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
884 memcpy (ctask->pp_buf, hostptr, (m[1].rm_eo-m[1].rm_so));
885 strcpy ( ctask->pp_buf+strlen(ctask->pp_buf),
888 /* If .zkey simply copy the name */
891 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
893 memcpy (ctask->pp_buf, hostptr, (m[1].rm_eo-m[1].rm_so + strlen (GNUNET_GNS_TLD_ZKEY)));
896 ctask->is_postprocessing = GNUNET_YES;
897 ctask->pp_finished = GNUNET_NO;
899 GNUNET_GNS_shorten_zone (gns_handle,
912 if ( bytes_to_copy > max )
914 GNUNET_log ( GNUNET_ERROR_TYPE_DEBUG,
915 "MHD: buffer in response too small! (%s)\n",
917 memcpy ( buf, ctask->buffer_ptr, max);
918 ctask->bytes_in_buffer -= max;
919 ctask->buffer_ptr += max;
924 GNUNET_log ( GNUNET_ERROR_TYPE_DEBUG,
925 "MHD: copying %d bytes to mhd response at offset %d\n",
928 memcpy ( buf, ctask->buffer_ptr, bytes_to_copy );
929 copied = bytes_to_copy;
930 if (bytes_to_copy < ctask->bytes_in_buffer)
932 ctask->bytes_in_buffer -= bytes_to_copy;
933 ctask->buffer_ptr += bytes_to_copy;
937 ctask->bytes_in_buffer = 0;
938 ctask->buf_status = BUF_WAIT_FOR_CURL;
939 ctask->buffer_ptr = ctask->buffer;
940 if (NULL != ctask->curl)
941 curl_easy_pause (ctask->curl, CURLPAUSE_CONT);
942 GNUNET_SCHEDULER_add_now (&run_mhd, ctask->mhd);
946 GNUNET_SCHEDULER_add_now (&run_mhd, ctask->mhd);
954 * Task that is run when we are ready to receive more data
958 * @param tc task context
961 curl_task_download (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
964 * Ask cURL for the select sets and schedule download
967 curl_download_prepare ()
974 struct GNUNET_NETWORK_FDSet *grs;
975 struct GNUNET_NETWORK_FDSet *gws;
977 struct GNUNET_TIME_Relative rtime;
983 mret = curl_multi_fdset (curl_multi, &rs, &ws, &es, &max);
985 if (mret != CURLM_OK)
987 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
988 "%s failed at %s:%d: `%s'\n",
989 "curl_multi_fdset", __FILE__, __LINE__,
990 curl_multi_strerror (mret));
995 mret = curl_multi_timeout (curl_multi, &to);
996 rtime = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, to);
998 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
999 "cURL multi fds: max=%d timeout=%llu\n", max, to);
1001 grs = GNUNET_NETWORK_fdset_create ();
1002 gws = GNUNET_NETWORK_fdset_create ();
1003 GNUNET_NETWORK_fdset_copy_native (grs, &rs, max + 1);
1004 GNUNET_NETWORK_fdset_copy_native (gws, &ws, max + 1);
1005 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1006 "Scheduling task cURL\n");
1008 if (curl_download_task != GNUNET_SCHEDULER_NO_TASK)
1009 GNUNET_SCHEDULER_cancel (curl_download_task);
1011 curl_download_task =
1012 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1015 &curl_task_download, curl_multi);
1016 GNUNET_NETWORK_fdset_destroy (gws);
1017 GNUNET_NETWORK_fdset_destroy (grs);
1023 * Task that is run when we are ready to receive more data
1026 * @param cls closure
1027 * @param tc task context
1030 curl_task_download (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1034 struct CURLMsg *msg;
1036 struct ProxyCurlTask *ctask;
1040 struct ProxyCurlTask *clean_head = NULL;
1041 struct ProxyCurlTask *clean_tail = NULL;
1043 curl_download_task = GNUNET_SCHEDULER_NO_TASK;
1045 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1047 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1048 "Shutdown requested while trying to download\n");
1052 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1060 mret = curl_multi_perform (curl_multi, &running);
1062 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1063 "Running curl tasks: %d\n", running);
1065 ctask = ctasks_head;
1066 for (; ctask != NULL; ctask = ctask->next)
1068 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1069 "CTask: %s\n", ctask->url);
1073 if (num_ctasks != running)
1075 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1076 "%d ctasks, %d curl running\n", num_ctasks, running);
1081 ctask = ctasks_head;
1082 msg = curl_multi_info_read (curl_multi, &msgnum);
1083 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1084 "Messages left: %d\n", msgnum);
1091 if ((msg->data.result != CURLE_OK) &&
1092 (msg->data.result != CURLE_GOT_NOTHING))
1094 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1095 "Download curl failed");
1097 for (; ctask != NULL; ctask = ctask->next)
1099 if (NULL == ctask->curl)
1102 if (memcmp (msg->easy_handle, ctask->curl, sizeof (CURL)) != 0)
1105 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1106 "Download curl failed for task %s: %s.\n",
1108 curl_easy_strerror (msg->data.result));
1109 ctask->download_successful = GNUNET_NO;
1110 ctask->download_error = GNUNET_YES;
1111 if (CURLE_OK == curl_easy_getinfo (ctask->curl,
1112 CURLINFO_RESPONSE_CODE,
1114 ctask->curl_response_code = resp_code;
1115 ctask->ready_to_queue = MHD_YES;
1117 GNUNET_CONTAINER_DLL_remove (ctasks_head, ctasks_tail,
1119 GNUNET_CONTAINER_DLL_insert (clean_head, clean_tail, ctask);
1122 GNUNET_assert (ctask != NULL);
1126 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1127 "cURL download completed.\n");
1129 for (; ctask != NULL; ctask = ctask->next)
1131 if (NULL == ctask->curl)
1134 if (memcmp (msg->easy_handle, ctask->curl, sizeof (CURL)) != 0)
1137 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1138 "cURL task %s found.\n", ctask->url);
1139 if (CURLE_OK == curl_easy_getinfo (ctask->curl,
1140 CURLINFO_RESPONSE_CODE,
1142 ctask->curl_response_code = resp_code;
1143 ctask->ready_to_queue = MHD_YES;
1144 ctask->download_successful = GNUNET_YES;
1145 GNUNET_CONTAINER_DLL_remove (ctasks_head, ctasks_tail,
1147 GNUNET_CONTAINER_DLL_insert (clean_head, clean_tail, ctask);
1151 GNUNET_assert (ctask != NULL);
1153 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1154 "curl end %s\n", curl_easy_strerror(msg->data.result));
1160 } while (msgnum > 0);
1162 for (ctask=clean_head; ctask != NULL; ctask = ctask->next)
1164 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1165 "Removing cURL task %s.\n", ctask->url);
1166 curl_multi_remove_handle (curl_multi, ctask->curl);
1167 curl_easy_cleanup (ctask->curl);
1172 for (ctask=ctasks_head; ctask != NULL; ctask = ctask->next)
1177 if (num_ctasks != running)
1179 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1180 "%d ctasks, %d curl running\n", num_ctasks, running);
1183 GNUNET_assert ( num_ctasks == running );
1187 } while (mret == CURLM_CALL_MULTI_PERFORM);
1190 if (mret != CURLM_OK)
1192 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "%s failed at %s:%d: `%s'\n",
1193 "curl_multi_perform", __FILE__, __LINE__,
1194 curl_multi_strerror (mret));
1196 curl_download_prepare();
1200 * Process LEHO lookup
1202 * @param cls the ctask
1203 * @param rd_count number of records returned
1204 * @param rd record data
1207 process_leho_lookup (void *cls,
1209 const struct GNUNET_NAMESTORE_RecordData *rd)
1211 struct ProxyCurlTask *ctask = cls;
1212 char hosthdr[262]; //256 + "Host: "
1216 struct hostent *phost;
1218 char resolvename[512];
1221 strcpy (ctask->leho, "");
1224 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1225 "No LEHO present!\n");
1227 for (i=0; i<rd_count; i++)
1229 if (rd[i].record_type != GNUNET_GNS_RECORD_LEHO)
1232 memcpy (ctask->leho, rd[i].data, rd[i].data_size);
1234 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1235 "Found LEHO %s for %s\n", ctask->leho, ctask->url);
1238 if (0 != strcmp (ctask->leho, ""))
1240 sprintf (hosthdr, "%s%s", "Host: ", ctask->leho);
1241 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1242 "New HTTP header value: %s\n", hosthdr);
1243 ctask->headers = curl_slist_append (ctask->headers, hosthdr);
1244 GNUNET_assert (NULL != ctask->headers);
1245 ret = curl_easy_setopt (ctask->curl, CURLOPT_HTTPHEADER, ctask->headers);
1246 if (CURLE_OK != ret)
1248 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "%s failed at %s:%d: `%s'\n",
1249 "curl_easy_setopt", __FILE__, __LINE__, curl_easy_strerror(ret));
1254 if (ctask->mhd->is_ssl)
1256 phost = (struct hostent*)gethostbyname (ctask->host);
1260 ssl_ip = inet_ntoa(*((struct in_addr*)(phost->h_addr)));
1261 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1262 "SSL target server: %s\n", ssl_ip);
1263 sprintf (resolvename, "%s:%d:%s", ctask->leho, HTTPS_PORT, ssl_ip);
1264 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1265 "Curl resolve: %s\n", resolvename);
1266 ctask->resolver = curl_slist_append ( ctask->resolver, resolvename);
1267 curl_easy_setopt (ctask->curl, CURLOPT_RESOLVE, ctask->resolver);
1268 sprintf (curlurl, "https://%s%s", ctask->leho, ctask->url);
1269 curl_easy_setopt (ctask->curl, CURLOPT_URL, curlurl);
1273 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1274 "gethostbyname failed for %s!\n", ctask->host);
1275 ctask->download_successful = GNUNET_NO;
1276 ctask->download_error = GNUNET_YES;
1281 if (CURLM_OK != (mret=curl_multi_add_handle (curl_multi, ctask->curl)))
1283 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1284 "%s failed at %s:%d: `%s'\n",
1285 "curl_multi_add_handle", __FILE__, __LINE__,
1286 curl_multi_strerror (mret));
1287 ctask->download_successful = GNUNET_NO;
1288 ctask->download_error = GNUNET_YES;
1291 GNUNET_CONTAINER_DLL_insert (ctasks_head, ctasks_tail, ctask);
1293 curl_download_prepare ();
1298 * Initialize download and trigger curl
1300 * @param cls the proxycurltask
1301 * @param auth_name the name of the authority (site of origin) of ctask->host
1305 process_get_authority (void *cls,
1306 const char* auth_name)
1308 struct ProxyCurlTask *ctask = cls;
1310 if (NULL == auth_name)
1312 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1313 "Get authority failed!\n");
1314 strcpy (ctask->authority, "");
1318 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1319 "Get authority yielded %s\n", auth_name);
1320 strcpy (ctask->authority, auth_name);
1323 GNUNET_GNS_lookup_zone (gns_handle,
1326 GNUNET_GNS_RECORD_LEHO,
1327 GNUNET_YES, //Only cached for performance
1329 &process_leho_lookup,
1334 * Main MHD callback for handling requests.
1337 * @param con MHD connection handle
1338 * @param url the url in the request
1339 * @param meth the HTTP method used ("GET", "PUT", etc.)
1340 * @param ver the HTTP version string (i.e. "HTTP/1.1")
1341 * @param upload_data the data being uploaded (excluding HEADERS,
1342 * for a POST that fits into memory and that is encoded
1343 * with a supported encoding, the POST data will NOT be
1344 * given in upload_data and is instead available as
1345 * part of MHD_get_connection_values; very large POST
1346 * data *will* be made available incrementally in
1348 * @param upload_data_size set initially to the size of the
1349 * upload_data provided; the method must update this
1350 * value to the number of bytes NOT processed;
1351 * @param con_cls pointer to location where we store the 'struct Request'
1352 * @return MHD_YES if the connection was handled successfully,
1353 * MHD_NO if the socket must be closed due to a serious
1354 * error while handling the request
1357 create_response (void *cls,
1358 struct MHD_Connection *con,
1362 const char *upload_data,
1363 size_t *upload_data_size,
1366 struct MhdHttpList* hd = cls;
1367 const char* page = "<html><head><title>gnoxy</title>"\
1368 "</head><body>cURL fail</body></html>";
1373 struct ProxyCurlTask *ctask;
1376 if (0 != strcmp (meth, "GET"))
1379 if (0 != *upload_data_size)
1382 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1386 if (NULL == *con_cls)
1388 ctask = GNUNET_malloc (sizeof (struct ProxyCurlTask));
1392 if (curl_multi == NULL)
1393 curl_multi = curl_multi_init ();
1395 ctask->curl = curl_easy_init();
1397 if ((ctask->curl == NULL) || (curl_multi == NULL))
1399 ctask->response = MHD_create_response_from_buffer (strlen (page),
1401 MHD_RESPMEM_PERSISTENT);
1402 ret = MHD_queue_response (con,
1405 MHD_destroy_response (ctask->response);
1406 GNUNET_free (ctask);
1412 ctask->headers = NULL;
1413 ctask->resolver = NULL;
1414 ctask->buffer_ptr = NULL;
1415 ctask->download_in_progress = GNUNET_YES;
1416 ctask->download_successful = GNUNET_NO;
1417 ctask->buf_status = BUF_WAIT_FOR_CURL;
1418 ctask->bytes_in_buffer = 0;
1419 ctask->parse_content = GNUNET_NO;
1420 ctask->connection = con;
1421 ctask->ready_to_queue = MHD_NO;
1422 ctask->fin = GNUNET_NO;
1423 ctask->headers = NULL;
1424 ctask->curl_response_code = MHD_HTTP_OK;
1427 MHD_get_connection_values (con,
1429 &con_val_iter, ctask);
1431 curl_easy_setopt (ctask->curl, CURLOPT_HEADERFUNCTION, &curl_check_hdr);
1432 curl_easy_setopt (ctask->curl, CURLOPT_HEADERDATA, ctask);
1433 curl_easy_setopt (ctask->curl, CURLOPT_WRITEFUNCTION, &callback_download);
1434 curl_easy_setopt (ctask->curl, CURLOPT_WRITEDATA, ctask);
1435 curl_easy_setopt (ctask->curl, CURLOPT_FOLLOWLOCATION, 0);
1436 //curl_easy_setopt (ctask->curl, CURLOPT_MAXREDIRS, 4);
1437 /* no need to abort if the above failed */
1438 if (GNUNET_NO == ctask->mhd->is_ssl)
1440 sprintf (curlurl, "http://%s%s", ctask->host, url);
1441 curl_easy_setopt (ctask->curl, CURLOPT_URL, curlurl);
1442 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1443 "Adding new curl task for %s\n", curlurl);
1445 strcpy (ctask->url, url);
1447 //curl_easy_setopt (ctask->curl, CURLOPT_URL, curlurl);
1448 curl_easy_setopt (ctask->curl, CURLOPT_FAILONERROR, 1);
1449 curl_easy_setopt (ctask->curl, CURLOPT_CONNECTTIMEOUT, 600L);
1450 curl_easy_setopt (ctask->curl, CURLOPT_TIMEOUT, 600L);
1452 GNUNET_GNS_get_authority (gns_handle,
1454 &process_get_authority,
1457 ctask->response = MHD_create_response_from_callback (MHD_SIZE_UNKNOWN,
1463 ctask->ready_to_queue = GNUNET_NO;
1464 ctask->fin = GNUNET_NO;
1468 ctask = (struct ProxyCurlTask *) *con_cls;
1470 if (ctask->fin == GNUNET_YES)
1473 if (ctask->ready_to_queue == GNUNET_YES)
1475 ctask->fin = GNUNET_YES;
1476 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1477 "Queueing response with code=%ld\n",
1478 ctask->curl_response_code);
1479 return MHD_queue_response (con, ctask->curl_response_code, ctask->response);
1485 //MHD_destroy_response (response);
1492 * Task run whenever HTTP server operations are pending.
1495 * @param tc sched context
1498 do_httpd (void *cls,
1499 const struct GNUNET_SCHEDULER_TaskContext *tc);
1508 struct MhdHttpList *hd;
1510 for (hd=mhd_httpd_head; hd != NULL; hd = hd->next)
1518 * @param hd the daemon to run
1521 run_httpd (struct MhdHttpList *hd)
1526 struct GNUNET_NETWORK_FDSet *wrs;
1527 struct GNUNET_NETWORK_FDSet *wws;
1528 struct GNUNET_NETWORK_FDSet *wes;
1531 unsigned MHD_LONG_LONG timeout;
1532 struct GNUNET_TIME_Relative tv;
1537 wrs = GNUNET_NETWORK_fdset_create ();
1538 wes = GNUNET_NETWORK_fdset_create ();
1539 wws = GNUNET_NETWORK_fdset_create ();
1541 GNUNET_assert (MHD_YES == MHD_get_fdset (hd->daemon, &rs, &ws, &es, &max));
1544 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1545 "MHD fds: max=%d\n", max);
1547 haveto = MHD_get_timeout (hd->daemon, &timeout);
1549 if (haveto == MHD_YES)
1550 tv.rel_value = (uint64_t) timeout;
1552 tv = GNUNET_TIME_UNIT_FOREVER_REL;
1553 GNUNET_NETWORK_fdset_copy_native (wrs, &rs, max + 1);
1554 GNUNET_NETWORK_fdset_copy_native (wws, &ws, max + 1);
1555 GNUNET_NETWORK_fdset_copy_native (wes, &es, max + 1);
1557 if (hd->httpd_task != GNUNET_SCHEDULER_NO_TASK)
1558 GNUNET_SCHEDULER_cancel (hd->httpd_task);
1560 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_HIGH,
1563 GNUNET_NETWORK_fdset_destroy (wrs);
1564 GNUNET_NETWORK_fdset_destroy (wws);
1565 GNUNET_NETWORK_fdset_destroy (wes);
1570 * Task run whenever HTTP server operations are pending.
1573 * @param tc sched context
1576 do_httpd (void *cls,
1577 const struct GNUNET_SCHEDULER_TaskContext *tc)
1579 struct MhdHttpList *hd = cls;
1581 hd->httpd_task = GNUNET_SCHEDULER_NO_TASK;
1583 MHD_run (hd->daemon);
1589 * Read data from socket
1591 * @param cls the closure
1592 * @param tc scheduler context
1595 do_read (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
1598 * Read from remote end
1600 * @param cls closure
1601 * @param tc scheduler context
1604 do_read_remote (void* cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
1607 * Write data to remote socket
1609 * @param cls the closure
1610 * @param tc scheduler context
1613 do_write_remote (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1615 struct Socks5Request *s5r = cls;
1618 s5r->fwdwtask = GNUNET_SCHEDULER_NO_TASK;
1620 if ((NULL != tc->read_ready) &&
1621 (GNUNET_NETWORK_fdset_isset (tc->write_ready, s5r->remote_sock)) &&
1622 ((len = GNUNET_NETWORK_socket_send (s5r->remote_sock, s5r->rbuf,
1625 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1626 "Successfully sent %d bytes to remote socket\n",
1631 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "write remote");
1633 if (s5r->rtask != GNUNET_SCHEDULER_NO_TASK)
1634 GNUNET_SCHEDULER_cancel (s5r->rtask);
1635 if (s5r->wtask != GNUNET_SCHEDULER_NO_TASK)
1636 GNUNET_SCHEDULER_cancel (s5r->wtask);
1637 if (s5r->fwdrtask != GNUNET_SCHEDULER_NO_TASK)
1638 GNUNET_SCHEDULER_cancel (s5r->fwdrtask);
1639 GNUNET_NETWORK_socket_close (s5r->remote_sock);
1640 GNUNET_NETWORK_socket_close (s5r->sock);
1646 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
1653 * Clean up s5r handles
1655 * @param s5r the handle to destroy
1658 cleanup_s5r (struct Socks5Request *s5r)
1660 if (s5r->rtask != GNUNET_SCHEDULER_NO_TASK)
1661 GNUNET_SCHEDULER_cancel (s5r->rtask);
1662 if (s5r->fwdwtask != GNUNET_SCHEDULER_NO_TASK)
1663 GNUNET_SCHEDULER_cancel (s5r->fwdwtask);
1664 if (s5r->fwdrtask != GNUNET_SCHEDULER_NO_TASK)
1665 GNUNET_SCHEDULER_cancel (s5r->fwdrtask);
1667 if (NULL != s5r->remote_sock)
1668 GNUNET_NETWORK_socket_close (s5r->remote_sock);
1669 if ((NULL != s5r->sock) && (s5r->cleanup_sock == GNUNET_YES))
1670 GNUNET_NETWORK_socket_close (s5r->sock);
1676 * Write data to socket
1678 * @param cls the closure
1679 * @param tc scheduler context
1682 do_write (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1684 struct Socks5Request *s5r = cls;
1687 s5r->wtask = GNUNET_SCHEDULER_NO_TASK;
1689 if ((NULL != tc->read_ready) &&
1690 (GNUNET_NETWORK_fdset_isset (tc->write_ready, s5r->sock)) &&
1691 ((len = GNUNET_NETWORK_socket_send (s5r->sock, s5r->wbuf,
1694 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1695 "Successfully sent %d bytes to socket\n",
1701 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "write");
1702 s5r->cleanup = GNUNET_YES;
1703 s5r->cleanup_sock = GNUNET_YES;
1709 if (GNUNET_YES == s5r->cleanup)
1715 if ((s5r->state == SOCKS5_DATA_TRANSFER) &&
1716 (s5r->fwdrtask == GNUNET_SCHEDULER_NO_TASK))
1718 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
1720 &do_read_remote, s5r);
1724 * Read from remote end
1726 * @param cls closure
1727 * @param tc scheduler context
1730 do_read_remote (void* cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1732 struct Socks5Request *s5r = cls;
1734 s5r->fwdrtask = GNUNET_SCHEDULER_NO_TASK;
1737 if ((NULL != tc->write_ready) &&
1738 (GNUNET_NETWORK_fdset_isset (tc->read_ready, s5r->remote_sock)) &&
1739 (s5r->wbuf_len = GNUNET_NETWORK_socket_recv (s5r->remote_sock, s5r->wbuf,
1740 sizeof (s5r->wbuf))))
1742 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1743 "Successfully read %d bytes from remote socket\n",
1748 if (s5r->wbuf_len == 0)
1749 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1750 "0 bytes received from remote... graceful shutdown!\n");
1751 if (s5r->fwdwtask != GNUNET_SCHEDULER_NO_TASK)
1752 GNUNET_SCHEDULER_cancel (s5r->fwdwtask);
1753 if (s5r->rtask != GNUNET_SCHEDULER_NO_TASK)
1754 GNUNET_SCHEDULER_cancel (s5r->rtask);
1756 GNUNET_NETWORK_socket_close (s5r->remote_sock);
1757 s5r->remote_sock = NULL;
1758 GNUNET_NETWORK_socket_close (s5r->sock);
1764 s5r->wtask = GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
1772 * Adds a socket to MHD
1774 * @param h the handle to the socket to add
1775 * @param daemon the daemon to add the fd to
1776 * @return whatever MHD_add_connection returns
1779 add_handle_to_mhd (struct GNUNET_NETWORK_Handle *h, struct MHD_Daemon *daemon)
1782 struct sockaddr *addr;
1785 fd = dup (GNUNET_NETWORK_get_fd (h));
1786 addr = GNUNET_NETWORK_get_addr (h);
1787 len = GNUNET_NETWORK_get_addrlen (h);
1789 return MHD_add_connection (daemon, fd, addr, len);
1793 * Calculate size of file
1795 * @param filename name of file
1796 * @return filesize or 0 on error
1799 get_file_size (const char* filename)
1803 fp = fopen (filename, "rb");
1808 if ((0 != fseek (fp, 0, SEEK_END)) || (-1 == (size = ftell (fp))))
1820 * Read file in filename
1822 * @param filename file to read
1823 * @param size pointer where filesize is stored
1827 load_file (const char* filename, unsigned int* size)
1832 *size = get_file_size (filename);
1836 fp = fopen (filename, "rb");
1840 buffer = GNUNET_malloc (*size);
1847 if (*size != fread (buffer, 1, *size, fp))
1849 GNUNET_free (buffer);
1858 * Load PEM key from file
1860 * @param key where to store the data
1861 * @param keyfile path to the PEM file
1864 load_key_from_file (gnutls_x509_privkey_t key, char* keyfile)
1866 gnutls_datum_t key_data;
1867 key_data.data = NULL;
1870 key_data.data = (unsigned char*)load_file (keyfile, &key_data.size);
1872 ret = gnutls_x509_privkey_import (key, &key_data,
1873 GNUTLS_X509_FMT_PEM);
1875 if (GNUTLS_E_SUCCESS != ret)
1877 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1878 "Unable to import private key %s(ret=%d)\n", key_data.data, ret);
1882 GNUNET_free (key_data.data);
1886 * Load cert from file
1888 * @param crt struct to store data in
1889 * @param certfile path to pem file
1892 load_cert_from_file (gnutls_x509_crt_t crt, char* certfile)
1894 gnutls_datum_t cert_data;
1895 cert_data.data = NULL;
1898 cert_data.data = (unsigned char*)load_file (certfile, &cert_data.size);
1900 ret = gnutls_x509_crt_import (crt, &cert_data,
1901 GNUTLS_X509_FMT_PEM);
1902 if (GNUTLS_E_SUCCESS != ret)
1904 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1905 "Unable to import certificate %s(ret=%d)\n", certfile, ret);
1909 GNUNET_free (cert_data.data);
1915 * Generate new certificate for specific name
1917 * @param name the subject name to generate a cert for
1918 * @return a struct holding the PEM data
1920 static struct ProxyGNSCertificate *
1921 generate_gns_certificate (const char *name)
1925 unsigned int serial;
1926 size_t key_buf_size;
1927 size_t cert_buf_size;
1928 gnutls_x509_crt_t request;
1932 ret = gnutls_x509_crt_init (&request);
1934 if (GNUTLS_E_SUCCESS != ret)
1939 ret = gnutls_x509_crt_set_key (request, proxy_ca.key);
1941 if (GNUTLS_E_SUCCESS != ret)
1946 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Generating cert\n");
1948 struct ProxyGNSCertificate *pgc =
1949 GNUNET_malloc (sizeof (struct ProxyGNSCertificate));
1951 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Adding DNs\n");
1953 gnutls_x509_crt_set_dn_by_oid (request, GNUTLS_OID_X520_COUNTRY_NAME,
1956 gnutls_x509_crt_set_dn_by_oid (request, GNUTLS_OID_X520_ORGANIZATION_NAME,
1959 gnutls_x509_crt_set_dn_by_oid (request, GNUTLS_OID_X520_COMMON_NAME,
1960 0, name, strlen (name));
1962 ret = gnutls_x509_crt_set_version (request, 3);
1964 ret = gnutls_rnd (GNUTLS_RND_NONCE, &serial, sizeof (serial));
1966 etime = time (NULL);
1967 tm_data = localtime (&etime);
1970 ret = gnutls_x509_crt_set_serial (request,
1974 ret = gnutls_x509_crt_set_activation_time (request,
1977 etime = mktime (tm_data);
1984 ret = gnutls_x509_crt_set_expiration_time (request,
1986 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Signing...\n");
1988 ret = gnutls_x509_crt_sign (request, proxy_ca.cert, proxy_ca.key);
1990 key_buf_size = sizeof (pgc->key);
1991 cert_buf_size = sizeof (pgc->cert);
1993 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Exporting certificate...\n");
1995 gnutls_x509_crt_export (request, GNUTLS_X509_FMT_PEM,
1996 pgc->cert, &cert_buf_size);
1998 gnutls_x509_privkey_export (proxy_ca.key, GNUTLS_X509_FMT_PEM,
1999 pgc->key, &key_buf_size);
2002 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Cleaning up\n");
2003 gnutls_x509_crt_deinit (request);
2011 * Accept policy for mhdaemons
2014 * @param addr the sockaddr
2015 * @param addrlen the sockaddr length
2016 * @return MHD_NO if sockaddr is wrong or #conns too high
2019 accept_cb (void* cls, const struct sockaddr *addr, socklen_t addrlen)
2021 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2022 "In MHD accept policy cb\n");
2026 if (addr->sa_family == AF_UNIX)
2030 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2031 "Connection accepted\n");
2038 * Adds a socket to an SSL MHD instance
2039 * It is important the the domain name is
2040 * correct. In most cases we need to start a new daemon
2042 * @param h the handle to add to a daemon
2043 * @param domain the domain the ssl daemon has to serve
2044 * @return MHD_YES on success
2047 add_handle_to_ssl_mhd (struct GNUNET_NETWORK_Handle *h, char* domain)
2049 struct MhdHttpList *hd = NULL;
2050 struct ProxyGNSCertificate *pgc;
2051 struct NetworkHandleList *nh;
2053 for (hd = mhd_httpd_head; hd != NULL; hd = hd->next)
2055 if (0 == strcmp (hd->domain, domain))
2062 pgc = generate_gns_certificate (domain);
2064 hd = GNUNET_malloc (sizeof (struct MhdHttpList));
2065 hd->is_ssl = GNUNET_YES;
2066 strcpy (hd->domain, domain);
2067 hd->proxy_cert = pgc;
2070 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2071 "No previous SSL instance found... starting new one for %s\n",
2074 hd->daemon = MHD_start_daemon (MHD_USE_DEBUG | MHD_USE_SSL, 4444,
2076 &create_response, hd,
2077 MHD_OPTION_LISTEN_SOCKET, GNUNET_NETWORK_get_fd (mhd_unix_socket),
2078 MHD_OPTION_CONNECTION_LIMIT, MHD_MAX_CONNECTIONS,
2079 MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) 16,
2080 MHD_OPTION_NOTIFY_COMPLETED,
2082 MHD_OPTION_HTTPS_MEM_KEY, pgc->key,
2083 MHD_OPTION_HTTPS_MEM_CERT, pgc->cert,
2086 GNUNET_assert (hd->daemon != NULL);
2087 hd->httpd_task = GNUNET_SCHEDULER_NO_TASK;
2089 GNUNET_CONTAINER_DLL_insert (mhd_httpd_head, mhd_httpd_tail, hd);
2092 nh = GNUNET_malloc (sizeof (struct NetworkHandleList));
2095 GNUNET_CONTAINER_DLL_insert (hd->socket_handles_head,
2096 hd->socket_handles_tail,
2099 return add_handle_to_mhd (h, hd->daemon);
2105 * Read data from incoming connection
2107 * @param cls the closure
2108 * @param tc the scheduler context
2111 do_read (void* cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2113 struct Socks5Request *s5r = cls;
2114 struct socks5_client_hello *c_hello;
2115 struct socks5_server_hello *s_hello;
2116 struct socks5_client_request *c_req;
2117 struct socks5_server_response *s_resp;
2123 struct hostent *phost;
2125 struct sockaddr_in remote_addr;
2126 struct in_addr *r_sin_addr;
2128 struct NetworkHandleList *nh;
2130 s5r->rtask = GNUNET_SCHEDULER_NO_TASK;
2132 if ((NULL != tc->write_ready) &&
2133 (GNUNET_NETWORK_fdset_isset (tc->read_ready, s5r->sock)) &&
2134 (s5r->rbuf_len = GNUNET_NETWORK_socket_recv (s5r->sock, s5r->rbuf,
2135 sizeof (s5r->rbuf))))
2137 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2138 "Successfully read %d bytes from socket\n",
2143 if (s5r->rbuf_len != 0)
2144 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "read");
2146 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client disco!\n");
2148 if (s5r->fwdrtask != GNUNET_SCHEDULER_NO_TASK)
2149 GNUNET_SCHEDULER_cancel (s5r->fwdrtask);
2150 if (s5r->wtask != GNUNET_SCHEDULER_NO_TASK)
2151 GNUNET_SCHEDULER_cancel (s5r->wtask);
2152 if (s5r->fwdwtask != GNUNET_SCHEDULER_NO_TASK)
2153 GNUNET_SCHEDULER_cancel (s5r->fwdwtask);
2154 GNUNET_NETWORK_socket_close (s5r->remote_sock);
2155 GNUNET_NETWORK_socket_close (s5r->sock);
2160 if (s5r->state == SOCKS5_INIT)
2162 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2164 c_hello = (struct socks5_client_hello*)&s5r->rbuf;
2166 GNUNET_assert (c_hello->version == SOCKS_VERSION_5);
2168 s_hello = (struct socks5_server_hello*)&s5r->wbuf;
2169 s5r->wbuf_len = sizeof( struct socks5_server_hello );
2171 s_hello->version = c_hello->version;
2172 s_hello->auth_method = SOCKS_AUTH_NONE;
2174 /* Write response to client */
2175 s5r->wtask = GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
2179 s5r->rtask = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
2183 s5r->state = SOCKS5_REQUEST;
2187 if (s5r->state == SOCKS5_REQUEST)
2189 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2190 "Processing SOCKS5 request\n");
2191 c_req = (struct socks5_client_request*)&s5r->rbuf;
2192 s_resp = (struct socks5_server_response*)&s5r->wbuf;
2193 //Only 10byte for ipv4 response!
2194 s5r->wbuf_len = 10;//sizeof (struct socks5_server_response);
2196 GNUNET_assert (c_req->addr_type == 3);
2198 dom_len = *((uint8_t*)(&(c_req->addr_type) + 1));
2199 memset(domain, 0, sizeof(domain));
2200 strncpy(domain, (char*)(&(c_req->addr_type) + 2), dom_len);
2201 req_port = *((uint16_t*)(&(c_req->addr_type) + 2 + dom_len));
2203 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2204 "Requested connection is %s:%d\n",
2208 if (is_tld (domain, GNUNET_GNS_TLD) ||
2209 is_tld (domain, GNUNET_GNS_TLD_ZKEY))
2211 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2212 "Requested connection is gnunet tld\n",
2216 if (ntohs(req_port) == HTTPS_PORT)
2218 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2219 "Requested connection is HTTPS\n");
2220 ret = add_handle_to_ssl_mhd ( s5r->sock, domain );
2222 else if (NULL != httpd)
2224 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2225 "Requested connection is HTTP\n");
2226 nh = GNUNET_malloc (sizeof (struct NetworkHandleList));
2229 GNUNET_CONTAINER_DLL_insert (mhd_httpd_head->socket_handles_head,
2230 mhd_httpd_head->socket_handles_tail,
2233 ret = add_handle_to_mhd ( s5r->sock, httpd );
2238 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2239 _("Failed to start HTTP server\n"));
2240 s_resp->version = 0x05;
2241 s_resp->reply = 0x01;
2242 s5r->cleanup = GNUNET_YES;
2243 s5r->cleanup_sock = GNUNET_YES;
2245 GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
2251 /* Signal success */
2252 s_resp->version = 0x05;
2253 s_resp->reply = 0x00;
2254 s_resp->reserved = 0x00;
2255 s_resp->addr_type = 0x01;
2257 s5r->cleanup = GNUNET_YES;
2258 s5r->cleanup_sock = GNUNET_NO;
2260 GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
2268 phost = (struct hostent*)gethostbyname (domain);
2271 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2272 "Resolve %s error!\n", domain );
2273 s_resp->version = 0x05;
2274 s_resp->reply = 0x01;
2275 s5r->cleanup = GNUNET_YES;
2276 s5r->cleanup_sock = GNUNET_YES;
2278 GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
2284 s5r->remote_sock = GNUNET_NETWORK_socket_create (AF_INET,
2287 r_sin_addr = (struct in_addr*)(phost->h_addr);
2288 remote_ip = r_sin_addr->s_addr;
2289 memset(&remote_addr, 0, sizeof(remote_addr));
2290 remote_addr.sin_family = AF_INET;
2291 #if HAVE_SOCKADDR_IN_SIN_LEN
2292 remote_addr.sin_len = sizeof (remote_addr);
2294 remote_addr.sin_addr.s_addr = remote_ip;
2295 remote_addr.sin_port = req_port;
2297 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2298 "target server: %s:%u\n", inet_ntoa(remote_addr.sin_addr),
2302 GNUNET_NETWORK_socket_connect ( s5r->remote_sock,
2303 (const struct sockaddr*)&remote_addr,
2304 sizeof (remote_addr)))
2305 && (errno != EINPROGRESS))
2307 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "connect");
2308 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2309 "socket request error...\n");
2310 s_resp->version = 0x05;
2311 s_resp->reply = 0x01;
2313 GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
2320 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2321 "new remote connection\n");
2323 s_resp->version = 0x05;
2324 s_resp->reply = 0x00;
2325 s_resp->reserved = 0x00;
2326 s_resp->addr_type = 0x01;
2328 s5r->state = SOCKS5_DATA_TRANSFER;
2331 GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
2335 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
2343 if (s5r->state == SOCKS5_DATA_TRANSFER)
2345 if ((s5r->remote_sock == NULL) || (s5r->rbuf_len == 0))
2347 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2348 "Closing connection to client\n");
2349 if (s5r->rtask != GNUNET_SCHEDULER_NO_TASK)
2350 GNUNET_SCHEDULER_cancel (s5r->rtask);
2351 if (s5r->fwdwtask != GNUNET_SCHEDULER_NO_TASK)
2352 GNUNET_SCHEDULER_cancel (s5r->fwdwtask);
2353 if (s5r->fwdrtask != GNUNET_SCHEDULER_NO_TASK)
2354 GNUNET_SCHEDULER_cancel (s5r->fwdrtask);
2355 if (s5r->fwdrtask != GNUNET_SCHEDULER_NO_TASK)
2356 GNUNET_SCHEDULER_cancel (s5r->fwdrtask);
2358 if (s5r->remote_sock != NULL)
2359 GNUNET_NETWORK_socket_close (s5r->remote_sock);
2360 GNUNET_NETWORK_socket_close (s5r->sock);
2365 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2366 "forwarding %d bytes from client\n", s5r->rbuf_len);
2369 GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
2371 &do_write_remote, s5r);
2373 if (s5r->fwdrtask == GNUNET_SCHEDULER_NO_TASK)
2376 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
2378 &do_read_remote, s5r);
2384 //GNUNET_CONTAINER_DLL_remove (s5conns.head, s5conns.tail, s5r);
2390 * Accept new incoming connections
2392 * @param cls the closure
2393 * @param tc the scheduler context
2396 do_accept (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2398 struct GNUNET_NETWORK_Handle *s;
2399 struct Socks5Request *s5r;
2401 ltask = GNUNET_SCHEDULER_NO_TASK;
2402 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
2405 ltask = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
2409 s = GNUNET_NETWORK_socket_accept (lsock, NULL, NULL);
2413 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "accept");
2417 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2418 "Got an inbound connection, waiting for data\n");
2420 s5r = GNUNET_malloc (sizeof (struct Socks5Request));
2422 s5r->state = SOCKS5_INIT;
2423 s5r->wtask = GNUNET_SCHEDULER_NO_TASK;
2424 s5r->fwdwtask = GNUNET_SCHEDULER_NO_TASK;
2425 s5r->fwdrtask = GNUNET_SCHEDULER_NO_TASK;
2426 s5r->rtask = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
2429 //GNUNET_CONTAINER_DLL_insert (s5conns.head, s5conns.tail, s5r);
2434 * Task run on shutdown
2436 * @param cls closure
2437 * @param tc task context
2440 do_shutdown (void *cls,
2441 const struct GNUNET_SCHEDULER_TaskContext *tc)
2444 struct MhdHttpList *hd;
2445 struct MhdHttpList *tmp_hd;
2446 struct NetworkHandleList *nh;
2447 struct NetworkHandleList *tmp_nh;
2448 struct ProxyCurlTask *ctask;
2449 struct ProxyCurlTask *ctask_tmp;
2451 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2452 "Shutting down...\n");
2454 gnutls_global_deinit ();
2456 if (GNUNET_SCHEDULER_NO_TASK != curl_download_task)
2458 GNUNET_SCHEDULER_cancel (curl_download_task);
2459 curl_download_task = GNUNET_SCHEDULER_NO_TASK;
2462 for (hd = mhd_httpd_head; hd != NULL; hd = tmp_hd)
2466 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2467 "Stopping daemon\n");
2469 if (GNUNET_SCHEDULER_NO_TASK != hd->httpd_task)
2471 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2472 "Stopping select task %d\n",
2474 GNUNET_SCHEDULER_cancel (hd->httpd_task);
2475 hd->httpd_task = GNUNET_SCHEDULER_NO_TASK;
2478 if (NULL != hd->daemon)
2480 MHD_stop_daemon (hd->daemon);
2484 for (nh = hd->socket_handles_head; nh != NULL; nh = tmp_nh)
2488 GNUNET_NETWORK_socket_close (nh->h);
2493 if (NULL != hd->proxy_cert)
2495 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2496 "Free certificate\n");
2497 GNUNET_free (hd->proxy_cert);
2503 for (ctask=ctasks_head; ctask != NULL; ctask=ctask_tmp)
2505 ctask_tmp = ctask->next;
2507 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2508 "Cleaning up cURL task\n");
2510 if (ctask->curl != NULL)
2511 curl_easy_cleanup (ctask->curl);
2513 if (NULL != ctask->headers)
2514 curl_slist_free_all (ctask->headers);
2515 if (NULL != ctask->resolver)
2516 curl_slist_free_all (ctask->resolver);
2518 if (NULL != ctask->response)
2519 MHD_destroy_response (ctask->response);
2522 GNUNET_free (ctask);
2525 GNUNET_GNS_disconnect (gns_handle);
2530 * Compiles a regex for us
2532 * @param re ptr to re struct
2533 * @param rt the expression to compile
2534 * @return 0 on success
2537 compile_regex (regex_t *re, const char* rt)
2542 status = regcomp (re, rt, REG_EXTENDED|REG_NEWLINE);
2545 regerror (status, re, err, 1024);
2546 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2547 "Regex error compiling '%s': %s\n", rt, err);
2555 * Loads the users local zone key
2557 * @return GNUNET_YES on success
2560 load_local_zone_key (const struct GNUNET_CONFIGURATION_Handle *cfg)
2563 struct GNUNET_CRYPTO_RsaPrivateKey *key = NULL;
2564 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pkey;
2565 struct GNUNET_CRYPTO_ShortHashCode *zone = NULL;
2566 struct GNUNET_CRYPTO_ShortHashAsciiEncoded zonename;
2568 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "gns",
2569 "ZONEKEY", &keyfile))
2571 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2572 "Unable to load zone key config value!\n");
2576 if (GNUNET_NO == GNUNET_DISK_file_test (keyfile))
2578 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2579 "Unable to load zone key %s!\n", keyfile);
2580 GNUNET_free(keyfile);
2584 key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
2585 GNUNET_CRYPTO_rsa_key_get_public (key, &pkey);
2586 GNUNET_CRYPTO_short_hash(&pkey,
2587 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
2589 zone = &local_gns_zone;
2590 GNUNET_CRYPTO_short_hash_to_enc (zone, &zonename);
2591 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2592 "Using zone: %s!\n", &zonename);
2593 GNUNET_CRYPTO_rsa_key_free(key);
2594 GNUNET_free(keyfile);
2596 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "gns",
2597 "PRIVATE_ZONEKEY", &keyfile))
2599 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2600 "Unable to load private zone key config value!\n");
2604 if (GNUNET_NO == GNUNET_DISK_file_test (keyfile))
2606 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2607 "Unable to load private zone key %s!\n", keyfile);
2608 GNUNET_free(keyfile);
2612 key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
2613 GNUNET_CRYPTO_rsa_key_get_public (key, &pkey);
2614 GNUNET_CRYPTO_short_hash(&pkey,
2615 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
2616 &local_private_zone);
2617 GNUNET_CRYPTO_short_hash_to_enc (zone, &zonename);
2618 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2619 "Using private zone: %s!\n", &zonename);
2620 GNUNET_CRYPTO_rsa_key_free(key);
2621 GNUNET_free(keyfile);
2623 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "gns",
2624 "SHORTEN_ZONEKEY", &keyfile))
2626 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2627 "Unable to load shorten zone key config value!\n");
2631 if (GNUNET_NO == GNUNET_DISK_file_test (keyfile))
2633 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2634 "Unable to load shorten zone key %s!\n", keyfile);
2635 GNUNET_free(keyfile);
2639 key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
2640 GNUNET_CRYPTO_rsa_key_get_public (key, &pkey);
2641 GNUNET_CRYPTO_short_hash(&pkey,
2642 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
2643 &local_shorten_zone);
2644 GNUNET_CRYPTO_short_hash_to_enc (zone, &zonename);
2645 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2646 "Using shorten zone: %s!\n", &zonename);
2647 GNUNET_CRYPTO_rsa_key_free(key);
2648 GNUNET_free(keyfile);
2654 * Main function that will be run
2656 * @param cls closure
2657 * @param args remaining command-line arguments
2658 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
2659 * @param cfg configuration
2662 run (void *cls, char *const *args, const char *cfgfile,
2663 const struct GNUNET_CONFIGURATION_Handle *cfg)
2665 struct sockaddr_in sa;
2666 struct MhdHttpList *hd;
2667 struct sockaddr_un mhd_unix_sock_addr;
2669 char* proxy_sockfile;
2670 char* cafile_cfg = NULL;
2675 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2678 cafile = cafile_opt;
2682 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "gns-proxy",
2686 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2687 "Unable to load proxy CA config value!\n");
2688 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2689 "No proxy CA provided!\n");
2692 cafile = cafile_cfg;
2695 gnutls_global_init ();
2697 gnutls_x509_crt_init (&proxy_ca.cert);
2698 gnutls_x509_privkey_init (&proxy_ca.key);
2700 load_cert_from_file (proxy_ca.cert, cafile);
2701 load_key_from_file (proxy_ca.key, cafile);
2703 GNUNET_free_non_null (cafile_cfg);
2705 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2706 "Loading Template\n");
2708 compile_regex (&re_dotplus, (char*) RE_A_HREF);
2710 gns_handle = GNUNET_GNS_connect (cfg);
2712 if (GNUNET_NO == load_local_zone_key (cfg))
2714 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2715 "Unable to load zone!\n");
2719 if (NULL == gns_handle)
2721 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2722 "Unable to connect to GNS!\n");
2726 memset (&sa, 0, sizeof (sa));
2727 sa.sin_family = AF_INET;
2728 sa.sin_port = htons (port);
2729 #if HAVE_SOCKADDR_IN_SIN_LEN
2730 sa.sin_len = sizeof (sa);
2733 lsock = GNUNET_NETWORK_socket_create (AF_INET,
2737 if ((NULL == lsock) ||
2739 GNUNET_NETWORK_socket_bind (lsock, (const struct sockaddr *) &sa,
2742 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2743 "Failed to create listen socket bound to `%s'",
2744 GNUNET_a2s ((const struct sockaddr *) &sa, sizeof (sa)));
2746 GNUNET_NETWORK_socket_close (lsock);
2750 if (GNUNET_OK != GNUNET_NETWORK_socket_listen (lsock, 5))
2752 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2753 "Failed to listen on socket bound to `%s'",
2754 GNUNET_a2s ((const struct sockaddr *) &sa, sizeof (sa)));
2758 ltask = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
2759 lsock, &do_accept, NULL);
2764 if (0 != curl_global_init (CURL_GLOBAL_WIN32))
2766 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2767 "cURL global init failed!\n");
2771 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2772 "Proxy listens on port %u\n",
2775 mhd_httpd_head = NULL;
2776 mhd_httpd_tail = NULL;
2777 total_mhd_connections = 0;
2779 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "gns-proxy",
2783 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2784 "Specify PROXY_UNIXPATH in gns-proxy config section!\n");
2788 mhd_unix_socket = GNUNET_NETWORK_socket_create (AF_UNIX,
2792 if (NULL == mhd_unix_socket)
2794 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2795 "Unable to create unix domain socket!\n");
2799 mhd_unix_sock_addr.sun_family = AF_UNIX;
2800 strcpy (mhd_unix_sock_addr.sun_path, proxy_sockfile);
2803 mhd_unix_sock_addr.sun_path[0] = '\0';
2805 #if HAVE_SOCKADDR_IN_SIN_LEN
2806 mhd_unix_sock_addr.sun_len = (u_char) sizeof (struct sockaddr_un);
2809 len = strlen (proxy_sockfile) + sizeof(AF_UNIX);
2811 GNUNET_free (proxy_sockfile);
2813 if (GNUNET_OK != GNUNET_NETWORK_socket_bind (mhd_unix_socket,
2814 (struct sockaddr*)&mhd_unix_sock_addr,
2817 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2818 "Unable to bind unix domain socket!\n");
2822 if (GNUNET_OK != GNUNET_NETWORK_socket_listen (mhd_unix_socket,
2825 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2826 "Unable to listen on unix domain socket!\n");
2830 hd = GNUNET_malloc (sizeof (struct MhdHttpList));
2831 hd->is_ssl = GNUNET_NO;
2832 strcpy (hd->domain, "");
2833 httpd = MHD_start_daemon (MHD_USE_DEBUG, 4444, //Dummy port
2835 &create_response, hd,
2836 MHD_OPTION_LISTEN_SOCKET, GNUNET_NETWORK_get_fd (mhd_unix_socket),
2837 MHD_OPTION_CONNECTION_LIMIT, MHD_MAX_CONNECTIONS,
2838 MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) 16,
2839 MHD_OPTION_NOTIFY_COMPLETED,
2843 GNUNET_assert (httpd != NULL);
2845 hd->httpd_task = GNUNET_SCHEDULER_NO_TASK;
2847 GNUNET_CONTAINER_DLL_insert (mhd_httpd_head, mhd_httpd_tail, hd);
2851 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
2852 &do_shutdown, NULL);
2858 * The main function for gnunet-gns-proxy.
2860 * @param argc number of arguments from the command line
2861 * @param argv command line arguments
2862 * @return 0 ok, 1 on error
2865 main (int argc, char *const *argv)
2867 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
2869 gettext_noop ("listen on specified port"), 1,
2870 &GNUNET_GETOPT_set_string, &port},
2871 {'a', "authority", NULL,
2872 gettext_noop ("pem file to use as CA"), 1,
2873 &GNUNET_GETOPT_set_string, &cafile_opt},
2874 GNUNET_GETOPT_OPTION_END
2879 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
2883 GNUNET_log_setup ("gnunet-gns-proxy", "WARNING", NULL);
2886 GNUNET_PROGRAM_run (argc, argv, "gnunet-gns-proxy",
2887 _("GNUnet GNS proxy"),
2889 &run, NULL)) ? 0 : 1;
2890 GNUNET_free_non_null ((char*)argv);