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 /* The URL to fetch */
197 /* The cURL write buffer / MHD read buffer */
198 char buffer[CURL_MAX_WRITE_SIZE];
200 /* The pointer to the data in the buffer */
203 /* The buffer status (BUF_WAIT_FOR_CURL or BUF_WAIT_FOR_MHD) */
206 /* Number of bytes in buffer */
207 unsigned int bytes_in_buffer;
209 /* Indicates wheather the download is in progress */
210 int download_in_progress;
212 /* Indicates wheather the download was successful */
213 int download_successful;
215 /* Indicates wheather the download failed */
218 /* Indicates wheather we need to parse HTML */
221 /* Indicates wheather we are postprocessing the HTML right now */
222 int is_postprocessing;
224 /* Indicates wheather postprocessing has finished */
227 /* Task ID of the postprocessing task */
228 GNUNET_SCHEDULER_TaskIdentifier pp_task;
230 /* The postprocessing buffer TODO length? */
233 /* The authority of the corresponding host (site of origin) */
236 /* The hostname (Host header field) */
239 /* The LEgacy HOstname (can be empty) */
242 /* The associated daemon list entry */
243 struct MhdHttpList *mhd;
245 /* The associated response */
246 struct MHD_Response *response;
249 struct ProxySetCookieHeader *set_cookies_head;
252 struct ProxySetCookieHeader *set_cookies_tail;
254 /* connection status */
258 struct MHD_Connection *connection;
263 * Struct for set-cookies
265 struct ProxySetCookieHeader
268 struct ProxySetCookieHeader *next;
271 struct ProxySetCookieHeader *prev;
277 /* The port the proxy is running on (default 7777) */
278 static unsigned long port = GNUNET_GNS_PROXY_PORT;
280 /* The CA file (pem) to use for the proxy CA */
281 static char* cafile_opt;
283 /* The listen socket of the proxy */
284 static struct GNUNET_NETWORK_Handle *lsock;
286 /* The listen task ID */
287 GNUNET_SCHEDULER_TaskIdentifier ltask;
289 /* The cURL download task */
290 GNUNET_SCHEDULER_TaskIdentifier curl_download_task;
292 /* The non SSL httpd daemon handle */
293 static struct MHD_Daemon *httpd;
295 /* Number of current mhd connections */
296 static unsigned int total_mhd_connections;
298 /* The cURL multi handle */
299 static CURLM *curl_multi;
301 /* Handle to the GNS service */
302 static struct GNUNET_GNS_Handle *gns_handle;
304 /* DLL for ProxyCurlTasks */
305 static struct ProxyCurlTask *ctasks_head;
307 /* DLL for ProxyCurlTasks */
308 static struct ProxyCurlTask *ctasks_tail;
310 /* DLL for http daemons */
311 static struct MhdHttpList *mhd_httpd_head;
313 /* DLL for http daemons */
314 static struct MhdHttpList *mhd_httpd_tail;
316 /* Handle to the regex for dotplus (.+) replacement in HTML */
317 static regex_t re_dotplus;
319 /* The users local GNS zone hash */
320 static struct GNUNET_CRYPTO_ShortHashCode local_gns_zone;
322 /* The users local private zone */
323 static struct GNUNET_CRYPTO_ShortHashCode local_private_zone;
325 /* The users local shorten zone */
326 static struct GNUNET_CRYPTO_ShortHashCode local_shorten_zone;
328 /* The CA for SSL certificate generation */
329 static struct ProxyCA proxy_ca;
331 /* UNIX domain socket for mhd */
332 struct GNUNET_NETWORK_Handle *mhd_unix_socket;
334 /* Shorten zone private key */
335 struct GNUNET_CRYPTO_RsaPrivateKey *shorten_zonekey;
338 * Checks if name is in tld
340 * @param name the name to check
341 * @param tld the TLD to check for
342 * @return GNUNET_YES or GNUNET_NO
345 is_tld(const char* name, const char* tld)
349 if (strlen(name) <= strlen(tld))
354 offset = strlen(name)-strlen(tld);
355 if (strcmp (name+offset, tld) != 0)
357 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
358 "%s is not in .%s TLD\n", name, tld);
367 * Read HTTP request header field 'Host'
369 * @param cls buffer to write to
370 * @param kind value kind
371 * @param key field key
372 * @param value field value
373 * @return MHD_NO when Host found
376 con_val_iter (void *cls,
377 enum MHD_ValueKind kind,
381 char* buf = (char*)cls;
383 if (0 == strcmp ("Host", key))
393 * Check HTTP response header for mime
395 * @param buffer curl buffer
396 * @param size curl blocksize
397 * @param nmemb curl blocknumber
399 * @return size of read bytes
402 curl_check_hdr (void *buffer, size_t size, size_t nmemb, void *cls)
404 size_t bytes = size * nmemb;
405 struct ProxyCurlTask *ctask = cls;
406 int html_mime_len = strlen (HTML_HDR_CONTENT);
407 int cookie_hdr_len = strlen (MHD_HTTP_HEADER_SET_COOKIE);
408 char hdr_mime[html_mime_len+1];
409 char hdr_cookie[bytes+1];
410 char new_cookie_hdr[bytes+strlen (ctask->leho)+1];
417 if (html_mime_len <= bytes)
419 memcpy (hdr_mime, buffer, html_mime_len);
420 hdr_mime[html_mime_len] = '\0';
422 if (0 == strcmp (hdr_mime, HTML_HDR_CONTENT))
424 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
425 "Got HTML HTTP response header\n");
426 ctask->parse_content = GNUNET_YES;
430 if (cookie_hdr_len > bytes)
433 memcpy (hdr_cookie, buffer, bytes);
434 hdr_cookie[bytes] = '\0';
436 if (hdr_cookie[bytes-1] == '\n')
437 hdr_cookie[bytes-1] = '\0';
439 if (hdr_cookie[bytes-2] == '\r')
440 hdr_cookie[bytes-2] = '\0';
442 if (0 == memcmp (hdr_cookie,
443 MHD_HTTP_HEADER_SET_COOKIE,
446 ndup = GNUNET_strdup (hdr_cookie+cookie_hdr_len+1);
447 memset (new_cookie_hdr, 0, sizeof (new_cookie_hdr));
448 tok = strtok (ndup, ";");
450 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
451 "Looking for cookie in : %s\n", hdr_cookie);
453 for (; tok != NULL; tok = strtok (NULL, ";"))
455 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
456 "Got Cookie token: %s\n", tok);
457 //memcpy (new_cookie_hdr+offset, tok, strlen (tok));
458 if (0 == memcmp (tok, " domain", strlen (" domain")))
460 cookie_domain = tok + strlen (" domain") + 1;
462 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
463 "Got Set-Cookie Domain: %s\n", cookie_domain);
465 if (strlen (cookie_domain) < strlen (ctask->leho))
467 delta_cdomain = strlen (ctask->leho) - strlen (cookie_domain);
468 if (0 == strcmp (cookie_domain, ctask->leho + (delta_cdomain)))
470 GNUNET_snprintf (new_cookie_hdr+offset,
471 sizeof (new_cookie_hdr),
472 " domain=%s", ctask->authority);
473 offset += strlen (" domain=") + strlen (ctask->authority);
474 new_cookie_hdr[offset] = ';';
479 else if (strlen (cookie_domain) == strlen (ctask->leho))
481 if (0 == strcmp (cookie_domain, ctask->leho))
483 GNUNET_snprintf (new_cookie_hdr+offset,
484 sizeof (new_cookie_hdr),
485 " domain=%s", ctask->host);
486 offset += strlen (" domain=") + strlen (ctask->host);
487 new_cookie_hdr[offset] = ';';
492 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
493 "Cookie domain invalid\n");
497 memcpy (new_cookie_hdr+offset, tok, strlen (tok));
498 offset += strlen (tok);
499 new_cookie_hdr[offset] = ';';
503 //memcpy (new_cookie_hdr+offset, tok, strlen (tok));
507 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
508 "Got Set-Cookie HTTP header %s\n", new_cookie_hdr);
510 //pch = GNUNET_malloc (sizeof (struct ProxySetCookieHeader));
511 //len = strlen (hdr_cookie) - cookie_hdr_len - 1;
512 //pch->cookie = GNUNET_malloc (len + 1);
513 //memset (pch->cookie, 0, len + 1);
514 //memcpy (pch->cookie, hdr_cookie+cookie_hdr_len+1, len);
515 //GNUNET_CONTAINER_DLL_insert (ctask->set_cookies_head,
516 // ctask->set_cookies_tail,
518 //pch = ctask->set_cookies_head;
519 //while (pch != NULL)
521 if (GNUNET_NO == MHD_add_response_header (ctask->response,
522 MHD_HTTP_HEADER_SET_COOKIE,
525 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
526 "MHD: Error adding set-cookie header field %s\n",
527 hdr_cookie+cookie_hdr_len+1);
529 //GNUNET_free (pch->cookie);
530 //GNUNET_CONTAINER_DLL_remove (ctask->set_cookies_head,
531 // ctask->set_cookies_tail,
534 //pch = ctask->set_cookies_head;
544 * @param hd a http daemon list entry
547 run_httpd (struct MhdHttpList *hd);
559 * Task that simply runs MHD main loop
562 * @param tc task context
565 run_mhd (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
568 struct MhdHttpList *hd = cls;
570 //for (hd=mhd_httpd_head; hd != NULL; hd = hd->next)
571 MHD_run (hd->daemon);
576 * Process cURL download bits
578 * @param ptr buffer with data
579 * @param size size of a record
580 * @param nmemb number of records downloaded
582 * @return number of processed bytes
585 callback_download (void *ptr, size_t size, size_t nmemb, void *ctx)
587 const char *cbuf = ptr;
589 struct ProxyCurlTask *ctask = ctx;
592 if (ctask->con_status == MHD_NO)
594 MHD_queue_response (ctask->connection,
597 ctask->con_status = MHD_YES;
606 if (total > sizeof (ctask->buffer))
608 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
609 "CURL gave us too much data to handle (%d)!\n",
614 if (ctask->buf_status == BUF_WAIT_FOR_MHD)
616 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
617 "CURL: Waiting for MHD (%s)\n", ctask->url);
618 return CURL_WRITEFUNC_PAUSE;
622 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
623 "CURL: Copying to MHD (%s, %d)\n", ctask->url, total);
624 memcpy (ctask->buffer, cbuf, total);
625 ctask->bytes_in_buffer = total;
626 ctask->buffer_ptr = ctask->buffer;
628 ctask->buf_status = BUF_WAIT_FOR_MHD;
630 //GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
631 // "cURL chunk:\n%s\n", (char*)ctask->buffer);
632 //run_mhd (NULL, NULL);
633 GNUNET_SCHEDULER_add_now (&run_mhd, ctask->mhd);
638 * Ask cURL for the select sets and schedule download
641 curl_download_prepare ();
644 * Callback to free content
646 * @param cls content to free
647 * @param tc task context
650 mhd_content_free (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
652 struct ProxyCurlTask *ctask = cls;
654 if (NULL != ctask->headers)
655 curl_slist_free_all (ctask->headers);
657 if (NULL != ctask->headers)
658 curl_slist_free_all (ctask->resolver);
660 if (NULL != ctask->response)
661 MHD_destroy_response (ctask->response);
669 * Shorten result callback
671 * @param cls the proxycurltask
672 * @param short_name the shortened name (NULL on error)
675 process_shorten (void* cls, const char* short_name)
677 struct ProxyCurlTask *ctask = cls;
679 char tmp[strlen(ctask->pp_buf)]; //TODO length
681 if (NULL == short_name)
683 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
684 "MHD PP: Unable to shorten %s\n",
689 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
690 "MHD PP: Shorten %s -> %s\n",
694 sprintf (tmp, "<a href=\"http://%s", short_name);
695 strcpy (ctask->pp_buf, tmp);
697 ctask->pp_finished = GNUNET_YES;
699 GNUNET_SCHEDULER_add_now (&run_mhd, ctask->mhd);
704 * Callback for MHD response
707 * @param pos in buffer
709 * @param max space in buffer
710 * @return number of bytes written
713 mhd_content_cb (void *cls,
718 struct ProxyCurlTask *ctask = cls;
720 size_t bytes_to_copy;
723 regmatch_t m[RE_N_MATCHES];
725 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
726 "MHD: content cb %s\n", ctask->url);
728 if (ctask->download_successful &&
729 (ctask->buf_status == BUF_WAIT_FOR_CURL))
731 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
732 "MHD: sending response for %s\n", ctask->url);
733 ctask->download_in_progress = GNUNET_NO;
734 GNUNET_SCHEDULER_add_now (&mhd_content_free, ctask);
735 GNUNET_SCHEDULER_add_now (&run_mhd, ctask->mhd);
736 total_mhd_connections--;
737 return MHD_CONTENT_READER_END_OF_STREAM;
740 if (ctask->download_error &&
741 (ctask->buf_status == BUF_WAIT_FOR_CURL))
743 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
744 "MHD: sending error response\n");
745 ctask->download_in_progress = GNUNET_NO;
746 GNUNET_SCHEDULER_add_now (&mhd_content_free, ctask);
747 GNUNET_SCHEDULER_add_now (&run_mhd, ctask->mhd);
748 total_mhd_connections--;
749 return MHD_CONTENT_READER_END_WITH_ERROR;
752 if ( ctask->buf_status == BUF_WAIT_FOR_CURL )
755 bytes_to_copy = ctask->bytes_in_buffer;
757 if (ctask->parse_content == GNUNET_YES)
760 GNUNET_log ( GNUNET_ERROR_TYPE_DEBUG,
761 "MHD: We need to parse the HTML %s\n", ctask->buffer_ptr);
763 nomatch = regexec ( &re_dotplus, ctask->buffer_ptr, RE_N_MATCHES, m, 0);
767 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
768 "MHD RE: No match\n");
772 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
775 GNUNET_assert (m[1].rm_so != -1);
777 hostptr = ctask->buffer_ptr+m[1].rm_so;
781 bytes_to_copy = m[0].rm_so;
782 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
783 "Copying %d bytes.\n", m[0].rm_so);
789 if (ctask->is_postprocessing == GNUNET_YES)
793 if ( ctask->pp_finished == GNUNET_NO )
795 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
796 "MHD PP: Waiting for PP of %s\n", ctask->pp_buf);
800 ctask->is_postprocessing = GNUNET_NO;
802 ctask->bytes_in_buffer -= m[0].rm_eo;//(m[1].rm_eo-m[1].rm_so);
803 ctask->buffer_ptr += m[0].rm_eo;//(m[1].rm_eo-m[1].rm_so);
804 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
805 "Skipping next %d bytes in buffer\n", m[0].rm_eo);
807 GNUNET_SCHEDULER_add_now (&run_mhd, ctask->mhd);
809 if ( strlen (ctask->pp_buf) <= max )
811 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
812 "Copying postprocessed %s.\n", ctask->pp_buf);
813 memcpy ( buf, ctask->pp_buf, strlen (ctask->pp_buf) );
814 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
816 ctask->is_postprocessing = GNUNET_NO;
817 return strlen (ctask->pp_buf);
823 memset (ctask->pp_buf, 0, sizeof(ctask->pp_buf));
825 /* If .+ extend with authority */
826 if (*(ctask->buffer_ptr+m[1].rm_eo) == '+')
828 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
830 memcpy (ctask->pp_buf, hostptr, (m[1].rm_eo-m[1].rm_so));
831 strcpy ( ctask->pp_buf+strlen(ctask->pp_buf),
834 /* If .zkey simply copy the name */
837 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
839 memcpy (ctask->pp_buf, hostptr, (m[1].rm_eo-m[1].rm_so + strlen (GNUNET_GNS_TLD_ZKEY)));
842 ctask->is_postprocessing = GNUNET_YES;
843 ctask->pp_finished = GNUNET_NO;
845 GNUNET_GNS_shorten_zone (gns_handle,
858 if ( bytes_to_copy > max )
860 GNUNET_log ( GNUNET_ERROR_TYPE_DEBUG,
861 "MHD: buffer in response too small! (%s)\n",
863 memcpy ( buf, ctask->buffer_ptr, max);
864 ctask->bytes_in_buffer -= max;
865 ctask->buffer_ptr += max;
870 GNUNET_log ( GNUNET_ERROR_TYPE_DEBUG,
871 "MHD: copying %d bytes to mhd response at offset %d\n",
874 memcpy ( buf, ctask->buffer_ptr, bytes_to_copy );
875 copied = bytes_to_copy;
876 if (bytes_to_copy < ctask->bytes_in_buffer)
878 ctask->bytes_in_buffer -= bytes_to_copy;
879 ctask->buffer_ptr += bytes_to_copy;
883 ctask->bytes_in_buffer = 0;
884 ctask->buf_status = BUF_WAIT_FOR_CURL;
885 ctask->buffer_ptr = ctask->buffer;
886 if (NULL != ctask->curl)
887 curl_easy_pause (ctask->curl, CURLPAUSE_CONT);
888 GNUNET_SCHEDULER_add_now (&run_mhd, ctask->mhd);
892 GNUNET_SCHEDULER_add_now (&run_mhd, ctask->mhd);
900 * Task that is run when we are ready to receive more data
904 * @param tc task context
907 curl_task_download (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
910 * Ask cURL for the select sets and schedule download
913 curl_download_prepare ()
920 struct GNUNET_NETWORK_FDSet *grs;
921 struct GNUNET_NETWORK_FDSet *gws;
923 struct GNUNET_TIME_Relative rtime;
929 mret = curl_multi_fdset (curl_multi, &rs, &ws, &es, &max);
931 if (mret != CURLM_OK)
933 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
934 "%s failed at %s:%d: `%s'\n",
935 "curl_multi_fdset", __FILE__, __LINE__,
936 curl_multi_strerror (mret));
941 mret = curl_multi_timeout (curl_multi, &to);
942 rtime = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, to);
944 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
945 "cURL multi fds: max=%d timeout=%llu\n", max, to);
947 grs = GNUNET_NETWORK_fdset_create ();
948 gws = GNUNET_NETWORK_fdset_create ();
949 GNUNET_NETWORK_fdset_copy_native (grs, &rs, max + 1);
950 GNUNET_NETWORK_fdset_copy_native (gws, &ws, max + 1);
951 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
952 "Scheduling task cURL\n");
954 if (curl_download_task != GNUNET_SCHEDULER_NO_TASK)
955 GNUNET_SCHEDULER_cancel (curl_download_task);
958 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
961 &curl_task_download, curl_multi);
962 GNUNET_NETWORK_fdset_destroy (gws);
963 GNUNET_NETWORK_fdset_destroy (grs);
969 * Task that is run when we are ready to receive more data
973 * @param tc task context
976 curl_task_download (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
982 struct ProxyCurlTask *ctask;
985 struct ProxyCurlTask *clean_head = NULL;
986 struct ProxyCurlTask *clean_tail = NULL;
988 curl_download_task = GNUNET_SCHEDULER_NO_TASK;
990 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
992 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
993 "Shutdown requested while trying to download\n");
997 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1005 mret = curl_multi_perform (curl_multi, &running);
1007 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1008 "Running curl tasks: %d\n", running);
1010 ctask = ctasks_head;
1011 for (; ctask != NULL; ctask = ctask->next)
1013 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1014 "CTask: %s\n", ctask->url);
1018 if (num_ctasks != running)
1020 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1021 "%d ctasks, %d curl running\n", num_ctasks, running);
1026 ctask = ctasks_head;
1027 msg = curl_multi_info_read (curl_multi, &msgnum);
1028 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1029 "Messages left: %d\n", msgnum);
1036 if ((msg->data.result != CURLE_OK) &&
1037 (msg->data.result != CURLE_GOT_NOTHING))
1039 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1040 "Download curl failed");
1042 for (; ctask != NULL; ctask = ctask->next)
1044 if (NULL == ctask->curl)
1047 if (memcmp (msg->easy_handle, ctask->curl, sizeof (CURL)) != 0)
1050 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1051 "Download curl failed for task %s: %s.\n",
1053 curl_easy_strerror (msg->data.result));
1054 ctask->download_successful = GNUNET_NO;
1055 ctask->download_error = GNUNET_YES;
1056 //curl_multi_remove_handle (curl_multi, ctask->curl);
1057 //curl_easy_cleanup (ctask->curl);
1058 //ctask->curl = NULL;
1059 GNUNET_CONTAINER_DLL_remove (ctasks_head, ctasks_tail,
1061 GNUNET_CONTAINER_DLL_insert (clean_head, clean_tail, ctask);
1064 GNUNET_assert (ctask != NULL);
1068 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1069 "cURL download completed.\n");
1071 for (; ctask != NULL; ctask = ctask->next)
1073 if (NULL == ctask->curl)
1076 if (memcmp (msg->easy_handle, ctask->curl, sizeof (CURL)) != 0)
1079 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1080 "cURL task %s found.\n", ctask->url);
1081 ctask->download_successful = GNUNET_YES;
1082 //curl_multi_remove_handle (curl_multi, ctask->curl);
1083 //curl_easy_cleanup (ctask->curl);
1084 //ctask->curl = NULL;
1085 GNUNET_CONTAINER_DLL_remove (ctasks_head, ctasks_tail,
1087 GNUNET_CONTAINER_DLL_insert (clean_head, clean_tail, ctask);
1091 GNUNET_assert (ctask != NULL);
1093 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1094 "curl end %s\n", curl_easy_strerror(msg->data.result));
1100 } while (msgnum > 0);
1102 for (ctask=clean_head; ctask != NULL; ctask = ctask->next)
1104 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1105 "Removing cURL task %s.\n", ctask->url);
1106 curl_multi_remove_handle (curl_multi, ctask->curl);
1107 curl_easy_cleanup (ctask->curl);
1112 for (ctask=ctasks_head; ctask != NULL; ctask = ctask->next)
1117 if (num_ctasks != running)
1119 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1120 "%d ctasks, %d curl running\n", num_ctasks, running);
1123 GNUNET_assert ( num_ctasks == running );
1127 } while (mret == CURLM_CALL_MULTI_PERFORM);
1130 if (mret != CURLM_OK)
1132 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "%s failed at %s:%d: `%s'\n",
1133 "curl_multi_perform", __FILE__, __LINE__,
1134 curl_multi_strerror (mret));
1136 curl_download_prepare();
1140 * Process LEHO lookup
1142 * @param cls the ctask
1143 * @param rd_count number of records returned
1144 * @param rd record data
1147 process_leho_lookup (void *cls,
1149 const struct GNUNET_NAMESTORE_RecordData *rd)
1151 struct ProxyCurlTask *ctask = cls;
1152 char hosthdr[262]; //256 + "Host: "
1156 struct hostent *phost;
1158 char resolvename[512];
1161 ctask->headers = NULL;
1163 strcpy (ctask->leho, "");
1166 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1167 "No LEHO present!\n");
1169 for (i=0; i<rd_count; i++)
1171 if (rd[i].record_type != GNUNET_GNS_RECORD_LEHO)
1174 memcpy (ctask->leho, rd[i].data, rd[i].data_size);
1176 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1177 "Found LEHO %s for %s\n", ctask->leho, ctask->url);
1180 if (0 != strcmp (ctask->leho, ""))
1182 sprintf (hosthdr, "%s%s", "Host: ", ctask->leho);
1183 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1184 "New HTTP header value: %s\n", hosthdr);
1185 ctask->headers = curl_slist_append (ctask->headers, hosthdr);
1186 GNUNET_assert (NULL != ctask->headers);
1187 ret = curl_easy_setopt (ctask->curl, CURLOPT_HTTPHEADER, ctask->headers);
1188 if (CURLE_OK != ret)
1190 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "%s failed at %s:%d: `%s'\n",
1191 "curl_easy_setopt", __FILE__, __LINE__, curl_easy_strerror(ret));
1196 if (ctask->mhd->is_ssl)
1198 phost = (struct hostent*)gethostbyname (ctask->host);
1202 ssl_ip = inet_ntoa(*((struct in_addr*)(phost->h_addr)));
1203 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1204 "SSL target server: %s\n", ssl_ip);
1205 sprintf (resolvename, "%s:%d:%s", ctask->leho, HTTPS_PORT, ssl_ip);
1206 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1207 "Curl resolve: %s\n", resolvename);
1208 ctask->resolver = curl_slist_append ( ctask->resolver, resolvename);
1209 curl_easy_setopt (ctask->curl, CURLOPT_RESOLVE, ctask->resolver);
1210 sprintf (curlurl, "https://%s%s", ctask->leho, ctask->url);
1211 curl_easy_setopt (ctask->curl, CURLOPT_URL, curlurl);
1215 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1216 "gethostbyname failed for %s!\n", ctask->host);
1217 ctask->download_successful = GNUNET_NO;
1218 ctask->download_error = GNUNET_YES;
1223 if (CURLM_OK != (mret=curl_multi_add_handle (curl_multi, ctask->curl)))
1225 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1226 "%s failed at %s:%d: `%s'\n",
1227 "curl_multi_add_handle", __FILE__, __LINE__,
1228 curl_multi_strerror (mret));
1229 ctask->download_successful = GNUNET_NO;
1230 ctask->download_error = GNUNET_YES;
1233 GNUNET_CONTAINER_DLL_insert (ctasks_head, ctasks_tail, ctask);
1235 curl_download_prepare ();
1240 * Initialize download and trigger curl
1242 * @param cls the proxycurltask
1243 * @param auth_name the name of the authority (site of origin) of ctask->host
1247 process_get_authority (void *cls,
1248 const char* auth_name)
1250 struct ProxyCurlTask *ctask = cls;
1252 if (NULL == auth_name)
1254 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1255 "Get authority failed!\n");
1256 strcpy (ctask->authority, "");
1260 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1261 "Get authority yielded %s\n", auth_name);
1262 strcpy (ctask->authority, auth_name);
1265 GNUNET_GNS_lookup_zone (gns_handle,
1268 GNUNET_GNS_RECORD_LEHO,
1269 GNUNET_YES, //Only cached for performance
1271 &process_leho_lookup,
1276 * Main MHD callback for handling requests.
1279 * @param con MHD connection handle
1280 * @param url the url in the request
1281 * @param meth the HTTP method used ("GET", "PUT", etc.)
1282 * @param ver the HTTP version string (i.e. "HTTP/1.1")
1283 * @param upload_data the data being uploaded (excluding HEADERS,
1284 * for a POST that fits into memory and that is encoded
1285 * with a supported encoding, the POST data will NOT be
1286 * given in upload_data and is instead available as
1287 * part of MHD_get_connection_values; very large POST
1288 * data *will* be made available incrementally in
1290 * @param upload_data_size set initially to the size of the
1291 * upload_data provided; the method must update this
1292 * value to the number of bytes NOT processed;
1293 * @param con_cls pointer to location where we store the 'struct Request'
1294 * @return MHD_YES if the connection was handled successfully,
1295 * MHD_NO if the socket must be closed due to a serious
1296 * error while handling the request
1299 create_response (void *cls,
1300 struct MHD_Connection *con,
1304 const char *upload_data,
1305 size_t *upload_data_size,
1309 struct MhdHttpList* hd = cls;
1310 const char* page = "<html><head><title>gnoxy</title>"\
1311 "</head><body>cURL fail</body></html>";
1317 struct ProxyCurlTask *ctask;
1319 if (0 != strcmp (meth, "GET"))
1321 if (&dummy != *con_cls)
1327 if (0 != *upload_data_size)
1332 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1335 MHD_get_connection_values (con,
1337 &con_val_iter, host);
1341 ctask = GNUNET_malloc (sizeof (struct ProxyCurlTask));
1344 if (curl_multi == NULL)
1345 curl_multi = curl_multi_init ();
1347 ctask->curl = curl_easy_init();
1349 if ((ctask->curl == NULL) || (curl_multi == NULL))
1351 ctask->response = MHD_create_response_from_buffer (strlen (page),
1353 MHD_RESPMEM_PERSISTENT);
1354 ret = MHD_queue_response (con,
1357 MHD_destroy_response (ctask->response);
1358 GNUNET_free (ctask);
1364 ctask->headers = NULL;
1365 ctask->resolver = NULL;
1366 ctask->buffer_ptr = NULL;
1367 ctask->download_in_progress = GNUNET_YES;
1368 ctask->download_successful = GNUNET_NO;
1369 ctask->buf_status = BUF_WAIT_FOR_CURL;
1370 ctask->bytes_in_buffer = 0;
1371 ctask->parse_content = GNUNET_NO;
1372 ctask->connection = con;
1373 ctask->con_status = MHD_NO;
1375 curl_easy_setopt (ctask->curl, CURLOPT_HEADERFUNCTION, &curl_check_hdr);
1376 curl_easy_setopt (ctask->curl, CURLOPT_HEADERDATA, ctask);
1377 curl_easy_setopt (ctask->curl, CURLOPT_WRITEFUNCTION, &callback_download);
1378 curl_easy_setopt (ctask->curl, CURLOPT_WRITEDATA, ctask);
1379 curl_easy_setopt (ctask->curl, CURLOPT_FOLLOWLOCATION, 1);
1380 curl_easy_setopt (ctask->curl, CURLOPT_MAXREDIRS, 4);
1381 /* no need to abort if the above failed */
1382 if (GNUNET_NO == ctask->mhd->is_ssl)
1384 sprintf (curlurl, "http://%s%s", host, url);
1385 curl_easy_setopt (ctask->curl, CURLOPT_URL, curlurl);
1386 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1387 "Adding new curl task for %s\n", curlurl);
1389 strcpy (ctask->host, host);
1390 strcpy (ctask->url, url);
1392 //curl_easy_setopt (ctask->curl, CURLOPT_URL, curlurl);
1393 curl_easy_setopt (ctask->curl, CURLOPT_FAILONERROR, 1);
1394 curl_easy_setopt (ctask->curl, CURLOPT_CONNECTTIMEOUT, 600L);
1395 curl_easy_setopt (ctask->curl, CURLOPT_TIMEOUT, 600L);
1397 GNUNET_GNS_get_authority (gns_handle,
1399 &process_get_authority,
1401 //download_prepare (ctask);
1402 //curl_download_prepare ();
1404 ctask->response = MHD_create_response_from_callback (MHD_SIZE_UNKNOWN,
1409 //ret = MHD_queue_response (con, MHD_HTTP_OK, ctask->response);
1411 //MHD_destroy_response (response);
1418 * Task run whenever HTTP server operations are pending.
1421 * @param tc sched context
1424 do_httpd (void *cls,
1425 const struct GNUNET_SCHEDULER_TaskContext *tc);
1434 struct MhdHttpList *hd;
1436 for (hd=mhd_httpd_head; hd != NULL; hd = hd->next)
1444 * @param hd the daemon to run
1447 run_httpd (struct MhdHttpList *hd)
1452 struct GNUNET_NETWORK_FDSet *wrs;
1453 struct GNUNET_NETWORK_FDSet *wws;
1454 struct GNUNET_NETWORK_FDSet *wes;
1457 unsigned MHD_LONG_LONG timeout;
1458 struct GNUNET_TIME_Relative tv;
1463 wrs = GNUNET_NETWORK_fdset_create ();
1464 wes = GNUNET_NETWORK_fdset_create ();
1465 wws = GNUNET_NETWORK_fdset_create ();
1467 GNUNET_assert (MHD_YES == MHD_get_fdset (hd->daemon, &rs, &ws, &es, &max));
1470 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1471 "MHD fds: max=%d\n", max);
1473 haveto = MHD_get_timeout (hd->daemon, &timeout);
1475 if (haveto == MHD_YES)
1476 tv.rel_value = (uint64_t) timeout;
1478 tv = GNUNET_TIME_UNIT_FOREVER_REL;
1479 GNUNET_NETWORK_fdset_copy_native (wrs, &rs, max + 1);
1480 GNUNET_NETWORK_fdset_copy_native (wws, &ws, max + 1);
1481 GNUNET_NETWORK_fdset_copy_native (wes, &es, max + 1);
1483 if (hd->httpd_task != GNUNET_SCHEDULER_NO_TASK)
1484 GNUNET_SCHEDULER_cancel (hd->httpd_task);
1486 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_HIGH,
1489 GNUNET_NETWORK_fdset_destroy (wrs);
1490 GNUNET_NETWORK_fdset_destroy (wws);
1491 GNUNET_NETWORK_fdset_destroy (wes);
1496 * Task run whenever HTTP server operations are pending.
1499 * @param tc sched context
1502 do_httpd (void *cls,
1503 const struct GNUNET_SCHEDULER_TaskContext *tc)
1505 struct MhdHttpList *hd = cls;
1507 hd->httpd_task = GNUNET_SCHEDULER_NO_TASK;
1509 MHD_run (hd->daemon);
1515 * Read data from socket
1517 * @param cls the closure
1518 * @param tc scheduler context
1521 do_read (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
1524 * Read from remote end
1526 * @param cls closure
1527 * @param tc scheduler context
1530 do_read_remote (void* cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
1533 * Write data to remote socket
1535 * @param cls the closure
1536 * @param tc scheduler context
1539 do_write_remote (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1541 struct Socks5Request *s5r = cls;
1544 s5r->fwdwtask = GNUNET_SCHEDULER_NO_TASK;
1546 if ((NULL != tc->read_ready) &&
1547 (GNUNET_NETWORK_fdset_isset (tc->write_ready, s5r->remote_sock)) &&
1548 ((len = GNUNET_NETWORK_socket_send (s5r->remote_sock, s5r->rbuf,
1551 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1552 "Successfully sent %d bytes to remote socket\n",
1557 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "write remote");
1559 if (s5r->rtask != GNUNET_SCHEDULER_NO_TASK)
1560 GNUNET_SCHEDULER_cancel (s5r->rtask);
1561 if (s5r->wtask != GNUNET_SCHEDULER_NO_TASK)
1562 GNUNET_SCHEDULER_cancel (s5r->wtask);
1563 if (s5r->fwdrtask != GNUNET_SCHEDULER_NO_TASK)
1564 GNUNET_SCHEDULER_cancel (s5r->fwdrtask);
1565 GNUNET_NETWORK_socket_close (s5r->remote_sock);
1566 GNUNET_NETWORK_socket_close (s5r->sock);
1572 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
1579 * Clean up s5r handles
1581 * @param s5r the handle to destroy
1584 cleanup_s5r (struct Socks5Request *s5r)
1586 if (s5r->rtask != GNUNET_SCHEDULER_NO_TASK)
1587 GNUNET_SCHEDULER_cancel (s5r->rtask);
1588 if (s5r->fwdwtask != GNUNET_SCHEDULER_NO_TASK)
1589 GNUNET_SCHEDULER_cancel (s5r->fwdwtask);
1590 if (s5r->fwdrtask != GNUNET_SCHEDULER_NO_TASK)
1591 GNUNET_SCHEDULER_cancel (s5r->fwdrtask);
1593 if (NULL != s5r->remote_sock)
1594 GNUNET_NETWORK_socket_close (s5r->remote_sock);
1595 if ((NULL != s5r->sock) && (s5r->cleanup_sock == GNUNET_YES))
1596 GNUNET_NETWORK_socket_close (s5r->sock);
1602 * Write data to socket
1604 * @param cls the closure
1605 * @param tc scheduler context
1608 do_write (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1610 struct Socks5Request *s5r = cls;
1613 s5r->wtask = GNUNET_SCHEDULER_NO_TASK;
1615 if ((NULL != tc->read_ready) &&
1616 (GNUNET_NETWORK_fdset_isset (tc->write_ready, s5r->sock)) &&
1617 ((len = GNUNET_NETWORK_socket_send (s5r->sock, s5r->wbuf,
1620 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1621 "Successfully sent %d bytes to socket\n",
1627 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "write");
1628 s5r->cleanup = GNUNET_YES;
1629 s5r->cleanup_sock = GNUNET_YES;
1635 if (GNUNET_YES == s5r->cleanup)
1641 if ((s5r->state == SOCKS5_DATA_TRANSFER) &&
1642 (s5r->fwdrtask == GNUNET_SCHEDULER_NO_TASK))
1644 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
1646 &do_read_remote, s5r);
1650 * Read from remote end
1652 * @param cls closure
1653 * @param tc scheduler context
1656 do_read_remote (void* cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1658 struct Socks5Request *s5r = cls;
1660 s5r->fwdrtask = GNUNET_SCHEDULER_NO_TASK;
1663 if ((NULL != tc->write_ready) &&
1664 (GNUNET_NETWORK_fdset_isset (tc->read_ready, s5r->remote_sock)) &&
1665 (s5r->wbuf_len = GNUNET_NETWORK_socket_recv (s5r->remote_sock, s5r->wbuf,
1666 sizeof (s5r->wbuf))))
1668 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1669 "Successfully read %d bytes from remote socket\n",
1674 if (s5r->wbuf_len == 0)
1675 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1676 "0 bytes received from remote... graceful shutdown!\n");
1677 if (s5r->fwdwtask != GNUNET_SCHEDULER_NO_TASK)
1678 GNUNET_SCHEDULER_cancel (s5r->fwdwtask);
1679 if (s5r->rtask != GNUNET_SCHEDULER_NO_TASK)
1680 GNUNET_SCHEDULER_cancel (s5r->rtask);
1682 GNUNET_NETWORK_socket_close (s5r->remote_sock);
1683 s5r->remote_sock = NULL;
1684 GNUNET_NETWORK_socket_close (s5r->sock);
1690 s5r->wtask = GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
1698 * Adds a socket to MHD
1700 * @param h the handle to the socket to add
1701 * @param daemon the daemon to add the fd to
1702 * @return whatever MHD_add_connection returns
1705 add_handle_to_mhd (struct GNUNET_NETWORK_Handle *h, struct MHD_Daemon *daemon)
1708 struct sockaddr *addr;
1711 fd = dup (GNUNET_NETWORK_get_fd (h));
1712 addr = GNUNET_NETWORK_get_addr (h);
1713 len = GNUNET_NETWORK_get_addrlen (h);
1715 return MHD_add_connection (daemon, fd, addr, len);
1719 * Calculate size of file
1721 * @param filename name of file
1722 * @return filesize or 0 on error
1725 get_file_size (const char* filename)
1729 fp = fopen (filename, "rb");
1734 if ((0 != fseek (fp, 0, SEEK_END)) || (-1 == (size = ftell (fp))))
1746 * Read file in filename
1748 * @param filename file to read
1749 * @param size pointer where filesize is stored
1753 load_file (const char* filename, unsigned int* size)
1758 *size = get_file_size (filename);
1762 fp = fopen (filename, "rb");
1766 buffer = GNUNET_malloc (*size);
1773 if (*size != fread (buffer, 1, *size, fp))
1775 GNUNET_free (buffer);
1784 * Load PEM key from file
1786 * @param key where to store the data
1787 * @param keyfile path to the PEM file
1790 load_key_from_file (gnutls_x509_privkey_t key, char* keyfile)
1792 gnutls_datum_t key_data;
1793 key_data.data = NULL;
1796 key_data.data = (unsigned char*)load_file (keyfile, &key_data.size);
1798 ret = gnutls_x509_privkey_import (key, &key_data,
1799 GNUTLS_X509_FMT_PEM);
1801 if (GNUTLS_E_SUCCESS != ret)
1803 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1804 "Unable to import private key %s(ret=%d)\n", key_data.data, ret);
1808 GNUNET_free (key_data.data);
1812 * Load cert from file
1814 * @param crt struct to store data in
1815 * @param certfile path to pem file
1818 load_cert_from_file (gnutls_x509_crt_t crt, char* certfile)
1820 gnutls_datum_t cert_data;
1821 cert_data.data = NULL;
1824 cert_data.data = (unsigned char*)load_file (certfile, &cert_data.size);
1826 ret = gnutls_x509_crt_import (crt, &cert_data,
1827 GNUTLS_X509_FMT_PEM);
1828 if (GNUTLS_E_SUCCESS != ret)
1830 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1831 "Unable to import certificate %s(ret=%d)\n", certfile, ret);
1835 GNUNET_free (cert_data.data);
1841 * Generate new certificate for specific name
1843 * @param name the subject name to generate a cert for
1844 * @return a struct holding the PEM data
1846 static struct ProxyGNSCertificate *
1847 generate_gns_certificate (const char *name)
1851 unsigned int serial;
1852 size_t key_buf_size;
1853 size_t cert_buf_size;
1854 gnutls_x509_crt_t request;
1858 ret = gnutls_x509_crt_init (&request);
1860 if (GNUTLS_E_SUCCESS != ret)
1865 ret = gnutls_x509_crt_set_key (request, proxy_ca.key);
1867 if (GNUTLS_E_SUCCESS != ret)
1872 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Generating cert\n");
1874 struct ProxyGNSCertificate *pgc =
1875 GNUNET_malloc (sizeof (struct ProxyGNSCertificate));
1877 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Adding DNs\n");
1879 gnutls_x509_crt_set_dn_by_oid (request, GNUTLS_OID_X520_COUNTRY_NAME,
1882 gnutls_x509_crt_set_dn_by_oid (request, GNUTLS_OID_X520_ORGANIZATION_NAME,
1885 gnutls_x509_crt_set_dn_by_oid (request, GNUTLS_OID_X520_COMMON_NAME,
1886 0, name, strlen (name));
1888 ret = gnutls_x509_crt_set_version (request, 3);
1890 ret = gnutls_rnd (GNUTLS_RND_NONCE, &serial, sizeof (serial));
1892 etime = time (NULL);
1893 tm_data = localtime (&etime);
1896 ret = gnutls_x509_crt_set_serial (request,
1900 ret = gnutls_x509_crt_set_activation_time (request,
1903 etime = mktime (tm_data);
1910 ret = gnutls_x509_crt_set_expiration_time (request,
1912 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Signing...\n");
1914 ret = gnutls_x509_crt_sign (request, proxy_ca.cert, proxy_ca.key);
1916 key_buf_size = sizeof (pgc->key);
1917 cert_buf_size = sizeof (pgc->cert);
1919 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Exporting certificate...\n");
1921 gnutls_x509_crt_export (request, GNUTLS_X509_FMT_PEM,
1922 pgc->cert, &cert_buf_size);
1924 gnutls_x509_privkey_export (proxy_ca.key, GNUTLS_X509_FMT_PEM,
1925 pgc->key, &key_buf_size);
1928 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Cleaning up\n");
1929 gnutls_x509_crt_deinit (request);
1937 * Accept policy for mhdaemons
1940 * @param addr the sockaddr
1941 * @param addrlen the sockaddr length
1942 * @return MHD_NO if sockaddr is wrong or #conns too high
1945 accept_cb (void* cls, const struct sockaddr *addr, socklen_t addrlen)
1947 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1948 "In MHD accept policy cb\n");
1952 if (addr->sa_family == AF_UNIX)
1956 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1957 "Connection accepted\n");
1964 * Adds a socket to an SSL MHD instance
1965 * It is important the the domain name is
1966 * correct. In most cases we need to start a new daemon
1968 * @param h the handle to add to a daemon
1969 * @param domain the domain the ssl daemon has to serve
1970 * @return MHD_YES on success
1973 add_handle_to_ssl_mhd (struct GNUNET_NETWORK_Handle *h, char* domain)
1975 struct MhdHttpList *hd = NULL;
1976 struct ProxyGNSCertificate *pgc;
1977 struct NetworkHandleList *nh;
1979 for (hd = mhd_httpd_head; hd != NULL; hd = hd->next)
1981 if (0 == strcmp (hd->domain, domain))
1988 pgc = generate_gns_certificate (domain);
1990 hd = GNUNET_malloc (sizeof (struct MhdHttpList));
1991 hd->is_ssl = GNUNET_YES;
1992 strcpy (hd->domain, domain);
1993 hd->proxy_cert = pgc;
1996 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1997 "No previous SSL instance found... starting new one for %s\n",
2000 hd->daemon = MHD_start_daemon (MHD_USE_DEBUG | MHD_USE_SSL, 4444,
2002 &create_response, hd,
2003 MHD_OPTION_LISTEN_SOCKET, GNUNET_NETWORK_get_fd (mhd_unix_socket),
2004 MHD_OPTION_CONNECTION_LIMIT, MHD_MAX_CONNECTIONS,
2005 MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) 16,
2006 MHD_OPTION_NOTIFY_COMPLETED,
2008 MHD_OPTION_HTTPS_MEM_KEY, pgc->key,
2009 MHD_OPTION_HTTPS_MEM_CERT, pgc->cert,
2012 GNUNET_assert (hd->daemon != NULL);
2013 hd->httpd_task = GNUNET_SCHEDULER_NO_TASK;
2015 GNUNET_CONTAINER_DLL_insert (mhd_httpd_head, mhd_httpd_tail, hd);
2018 nh = GNUNET_malloc (sizeof (struct NetworkHandleList));
2021 GNUNET_CONTAINER_DLL_insert (hd->socket_handles_head,
2022 hd->socket_handles_tail,
2025 return add_handle_to_mhd (h, hd->daemon);
2031 * Read data from incoming connection
2033 * @param cls the closure
2034 * @param tc the scheduler context
2037 do_read (void* cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2039 struct Socks5Request *s5r = cls;
2040 struct socks5_client_hello *c_hello;
2041 struct socks5_server_hello *s_hello;
2042 struct socks5_client_request *c_req;
2043 struct socks5_server_response *s_resp;
2049 struct hostent *phost;
2051 struct sockaddr_in remote_addr;
2052 struct in_addr *r_sin_addr;
2054 struct NetworkHandleList *nh;
2056 s5r->rtask = GNUNET_SCHEDULER_NO_TASK;
2058 if ((NULL != tc->write_ready) &&
2059 (GNUNET_NETWORK_fdset_isset (tc->read_ready, s5r->sock)) &&
2060 (s5r->rbuf_len = GNUNET_NETWORK_socket_recv (s5r->sock, s5r->rbuf,
2061 sizeof (s5r->rbuf))))
2063 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2064 "Successfully read %d bytes from socket\n",
2069 if (s5r->rbuf_len != 0)
2070 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "read");
2072 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client disco!\n");
2074 if (s5r->fwdrtask != GNUNET_SCHEDULER_NO_TASK)
2075 GNUNET_SCHEDULER_cancel (s5r->fwdrtask);
2076 if (s5r->wtask != GNUNET_SCHEDULER_NO_TASK)
2077 GNUNET_SCHEDULER_cancel (s5r->wtask);
2078 if (s5r->fwdwtask != GNUNET_SCHEDULER_NO_TASK)
2079 GNUNET_SCHEDULER_cancel (s5r->fwdwtask);
2080 GNUNET_NETWORK_socket_close (s5r->remote_sock);
2081 GNUNET_NETWORK_socket_close (s5r->sock);
2086 if (s5r->state == SOCKS5_INIT)
2088 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2090 c_hello = (struct socks5_client_hello*)&s5r->rbuf;
2092 GNUNET_assert (c_hello->version == SOCKS_VERSION_5);
2094 s_hello = (struct socks5_server_hello*)&s5r->wbuf;
2095 s5r->wbuf_len = sizeof( struct socks5_server_hello );
2097 s_hello->version = c_hello->version;
2098 s_hello->auth_method = SOCKS_AUTH_NONE;
2100 /* Write response to client */
2101 s5r->wtask = GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
2105 s5r->rtask = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
2109 s5r->state = SOCKS5_REQUEST;
2113 if (s5r->state == SOCKS5_REQUEST)
2115 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2116 "Processing SOCKS5 request\n");
2117 c_req = (struct socks5_client_request*)&s5r->rbuf;
2118 s_resp = (struct socks5_server_response*)&s5r->wbuf;
2119 //Only 10byte for ipv4 response!
2120 s5r->wbuf_len = 10;//sizeof (struct socks5_server_response);
2122 GNUNET_assert (c_req->addr_type == 3);
2124 dom_len = *((uint8_t*)(&(c_req->addr_type) + 1));
2125 memset(domain, 0, sizeof(domain));
2126 strncpy(domain, (char*)(&(c_req->addr_type) + 2), dom_len);
2127 req_port = *((uint16_t*)(&(c_req->addr_type) + 2 + dom_len));
2129 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2130 "Requested connection is %s:%d\n",
2134 if (is_tld (domain, GNUNET_GNS_TLD) ||
2135 is_tld (domain, GNUNET_GNS_TLD_ZKEY))
2137 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2138 "Requested connection is gnunet tld\n",
2142 if (ntohs(req_port) == HTTPS_PORT)
2144 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2145 "Requested connection is HTTPS\n");
2146 ret = add_handle_to_ssl_mhd ( s5r->sock, domain );
2148 else if (NULL != httpd)
2150 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2151 "Requested connection is HTTP\n");
2152 nh = GNUNET_malloc (sizeof (struct NetworkHandleList));
2155 GNUNET_CONTAINER_DLL_insert (mhd_httpd_head->socket_handles_head,
2156 mhd_httpd_head->socket_handles_tail,
2159 ret = add_handle_to_mhd ( s5r->sock, httpd );
2164 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2165 _("Failed to start HTTP server\n"));
2166 s_resp->version = 0x05;
2167 s_resp->reply = 0x01;
2168 s5r->cleanup = GNUNET_YES;
2169 s5r->cleanup_sock = GNUNET_YES;
2171 GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
2177 /* Signal success */
2178 s_resp->version = 0x05;
2179 s_resp->reply = 0x00;
2180 s_resp->reserved = 0x00;
2181 s_resp->addr_type = 0x01;
2183 s5r->cleanup = GNUNET_YES;
2184 s5r->cleanup_sock = GNUNET_NO;
2186 GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
2194 phost = (struct hostent*)gethostbyname (domain);
2197 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2198 "Resolve %s error!\n", domain );
2199 s_resp->version = 0x05;
2200 s_resp->reply = 0x01;
2201 s5r->cleanup = GNUNET_YES;
2202 s5r->cleanup_sock = GNUNET_YES;
2204 GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
2210 s5r->remote_sock = GNUNET_NETWORK_socket_create (AF_INET,
2213 r_sin_addr = (struct in_addr*)(phost->h_addr);
2214 remote_ip = r_sin_addr->s_addr;
2215 memset(&remote_addr, 0, sizeof(remote_addr));
2216 remote_addr.sin_family = AF_INET;
2217 #if HAVE_SOCKADDR_IN_SIN_LEN
2218 remote_addr.sin_len = sizeof (remote_addr);
2220 remote_addr.sin_addr.s_addr = remote_ip;
2221 remote_addr.sin_port = req_port;
2223 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2224 "target server: %s:%u\n", inet_ntoa(remote_addr.sin_addr),
2228 GNUNET_NETWORK_socket_connect ( s5r->remote_sock,
2229 (const struct sockaddr*)&remote_addr,
2230 sizeof (remote_addr)))
2231 && (errno != EINPROGRESS))
2233 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "connect");
2234 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2235 "socket request error...\n");
2236 s_resp->version = 0x05;
2237 s_resp->reply = 0x01;
2239 GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
2246 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2247 "new remote connection\n");
2249 s_resp->version = 0x05;
2250 s_resp->reply = 0x00;
2251 s_resp->reserved = 0x00;
2252 s_resp->addr_type = 0x01;
2254 s5r->state = SOCKS5_DATA_TRANSFER;
2257 GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
2261 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
2269 if (s5r->state == SOCKS5_DATA_TRANSFER)
2271 if ((s5r->remote_sock == NULL) || (s5r->rbuf_len == 0))
2273 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2274 "Closing connection to client\n");
2275 if (s5r->rtask != GNUNET_SCHEDULER_NO_TASK)
2276 GNUNET_SCHEDULER_cancel (s5r->rtask);
2277 if (s5r->fwdwtask != GNUNET_SCHEDULER_NO_TASK)
2278 GNUNET_SCHEDULER_cancel (s5r->fwdwtask);
2279 if (s5r->fwdrtask != GNUNET_SCHEDULER_NO_TASK)
2280 GNUNET_SCHEDULER_cancel (s5r->fwdrtask);
2281 if (s5r->fwdrtask != GNUNET_SCHEDULER_NO_TASK)
2282 GNUNET_SCHEDULER_cancel (s5r->fwdrtask);
2284 if (s5r->remote_sock != NULL)
2285 GNUNET_NETWORK_socket_close (s5r->remote_sock);
2286 GNUNET_NETWORK_socket_close (s5r->sock);
2291 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2292 "forwarding %d bytes from client\n", s5r->rbuf_len);
2295 GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
2297 &do_write_remote, s5r);
2299 if (s5r->fwdrtask == GNUNET_SCHEDULER_NO_TASK)
2302 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
2304 &do_read_remote, s5r);
2310 //GNUNET_CONTAINER_DLL_remove (s5conns.head, s5conns.tail, s5r);
2316 * Accept new incoming connections
2318 * @param cls the closure
2319 * @param tc the scheduler context
2322 do_accept (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2324 struct GNUNET_NETWORK_Handle *s;
2325 struct Socks5Request *s5r;
2327 ltask = GNUNET_SCHEDULER_NO_TASK;
2328 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
2331 ltask = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
2335 s = GNUNET_NETWORK_socket_accept (lsock, NULL, NULL);
2339 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "accept");
2343 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2344 "Got an inbound connection, waiting for data\n");
2346 s5r = GNUNET_malloc (sizeof (struct Socks5Request));
2348 s5r->state = SOCKS5_INIT;
2349 s5r->wtask = GNUNET_SCHEDULER_NO_TASK;
2350 s5r->fwdwtask = GNUNET_SCHEDULER_NO_TASK;
2351 s5r->fwdrtask = GNUNET_SCHEDULER_NO_TASK;
2352 s5r->rtask = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
2355 //GNUNET_CONTAINER_DLL_insert (s5conns.head, s5conns.tail, s5r);
2360 * Task run on shutdown
2362 * @param cls closure
2363 * @param tc task context
2366 do_shutdown (void *cls,
2367 const struct GNUNET_SCHEDULER_TaskContext *tc)
2370 struct MhdHttpList *hd;
2371 struct MhdHttpList *tmp_hd;
2372 struct NetworkHandleList *nh;
2373 struct NetworkHandleList *tmp_nh;
2374 struct ProxyCurlTask *ctask;
2375 struct ProxyCurlTask *ctask_tmp;
2377 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2378 "Shutting down...\n");
2380 gnutls_global_deinit ();
2382 if (GNUNET_SCHEDULER_NO_TASK != curl_download_task)
2384 GNUNET_SCHEDULER_cancel (curl_download_task);
2385 curl_download_task = GNUNET_SCHEDULER_NO_TASK;
2388 for (hd = mhd_httpd_head; hd != NULL; hd = tmp_hd)
2392 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2393 "Stopping daemon\n");
2395 if (GNUNET_SCHEDULER_NO_TASK != hd->httpd_task)
2397 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2398 "Stopping select task %d\n",
2400 GNUNET_SCHEDULER_cancel (hd->httpd_task);
2401 hd->httpd_task = GNUNET_SCHEDULER_NO_TASK;
2404 if (NULL != hd->daemon)
2406 MHD_stop_daemon (hd->daemon);
2410 for (nh = hd->socket_handles_head; nh != NULL; nh = tmp_nh)
2414 GNUNET_NETWORK_socket_close (nh->h);
2419 if (NULL != hd->proxy_cert)
2421 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2422 "Free certificate\n");
2423 GNUNET_free (hd->proxy_cert);
2429 for (ctask=ctasks_head; ctask != NULL; ctask=ctask_tmp)
2431 ctask_tmp = ctask->next;
2433 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2434 "Cleaning up cURL task\n");
2436 if (ctask->curl != NULL)
2437 curl_easy_cleanup (ctask->curl);
2439 if (NULL != ctask->headers)
2440 curl_slist_free_all (ctask->headers);
2441 if (NULL != ctask->resolver)
2442 curl_slist_free_all (ctask->resolver);
2444 if (NULL != ctask->response)
2445 MHD_destroy_response (ctask->response);
2448 GNUNET_free (ctask);
2451 GNUNET_GNS_disconnect (gns_handle);
2456 * Compiles a regex for us
2458 * @param re ptr to re struct
2459 * @param rt the expression to compile
2460 * @return 0 on success
2463 compile_regex (regex_t *re, const char* rt)
2468 status = regcomp (re, rt, REG_EXTENDED|REG_NEWLINE);
2471 regerror (status, re, err, 1024);
2472 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2473 "Regex error compiling '%s': %s\n", rt, err);
2481 * Loads the users local zone key
2483 * @return GNUNET_YES on success
2486 load_local_zone_key (const struct GNUNET_CONFIGURATION_Handle *cfg)
2489 struct GNUNET_CRYPTO_RsaPrivateKey *key = NULL;
2490 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pkey;
2491 struct GNUNET_CRYPTO_ShortHashCode *zone = NULL;
2492 struct GNUNET_CRYPTO_ShortHashAsciiEncoded zonename;
2494 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "gns",
2495 "ZONEKEY", &keyfile))
2497 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2498 "Unable to load zone key config value!\n");
2502 if (GNUNET_NO == GNUNET_DISK_file_test (keyfile))
2504 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2505 "Unable to load zone key %s!\n", keyfile);
2506 GNUNET_free(keyfile);
2510 key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
2511 GNUNET_CRYPTO_rsa_key_get_public (key, &pkey);
2512 GNUNET_CRYPTO_short_hash(&pkey,
2513 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
2515 zone = &local_gns_zone;
2516 GNUNET_CRYPTO_short_hash_to_enc (zone, &zonename);
2517 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2518 "Using zone: %s!\n", &zonename);
2519 GNUNET_CRYPTO_rsa_key_free(key);
2520 GNUNET_free(keyfile);
2522 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "gns",
2523 "PRIVATE_ZONEKEY", &keyfile))
2525 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2526 "Unable to load private zone key config value!\n");
2530 if (GNUNET_NO == GNUNET_DISK_file_test (keyfile))
2532 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2533 "Unable to load private zone key %s!\n", keyfile);
2534 GNUNET_free(keyfile);
2538 key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
2539 GNUNET_CRYPTO_rsa_key_get_public (key, &pkey);
2540 GNUNET_CRYPTO_short_hash(&pkey,
2541 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
2542 &local_private_zone);
2543 GNUNET_CRYPTO_short_hash_to_enc (zone, &zonename);
2544 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2545 "Using private zone: %s!\n", &zonename);
2546 GNUNET_CRYPTO_rsa_key_free(key);
2547 GNUNET_free(keyfile);
2549 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "gns",
2550 "SHORTEN_ZONEKEY", &keyfile))
2552 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2553 "Unable to load shorten zone key config value!\n");
2557 if (GNUNET_NO == GNUNET_DISK_file_test (keyfile))
2559 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2560 "Unable to load shorten zone key %s!\n", keyfile);
2561 GNUNET_free(keyfile);
2565 key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
2566 GNUNET_CRYPTO_rsa_key_get_public (key, &pkey);
2567 GNUNET_CRYPTO_short_hash(&pkey,
2568 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
2569 &local_shorten_zone);
2570 GNUNET_CRYPTO_short_hash_to_enc (zone, &zonename);
2571 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2572 "Using shorten zone: %s!\n", &zonename);
2573 GNUNET_CRYPTO_rsa_key_free(key);
2574 GNUNET_free(keyfile);
2580 * Main function that will be run
2582 * @param cls closure
2583 * @param args remaining command-line arguments
2584 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
2585 * @param cfg configuration
2588 run (void *cls, char *const *args, const char *cfgfile,
2589 const struct GNUNET_CONFIGURATION_Handle *cfg)
2591 struct sockaddr_in sa;
2592 struct MhdHttpList *hd;
2593 struct sockaddr_un mhd_unix_sock_addr;
2595 char* proxy_sockfile;
2596 char* cafile_cfg = NULL;
2601 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2604 cafile = cafile_opt;
2608 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "gns-proxy",
2612 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2613 "Unable to load proxy CA config value!\n");
2614 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2615 "No proxy CA provided!\n");
2618 cafile = cafile_cfg;
2621 gnutls_global_init ();
2623 gnutls_x509_crt_init (&proxy_ca.cert);
2624 gnutls_x509_privkey_init (&proxy_ca.key);
2626 load_cert_from_file (proxy_ca.cert, cafile);
2627 load_key_from_file (proxy_ca.key, cafile);
2629 GNUNET_free_non_null (cafile_cfg);
2631 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2632 "Loading Template\n");
2634 compile_regex (&re_dotplus, (char*) RE_A_HREF);
2636 gns_handle = GNUNET_GNS_connect (cfg);
2638 if (GNUNET_NO == load_local_zone_key (cfg))
2640 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2641 "Unable to load zone!\n");
2645 if (NULL == gns_handle)
2647 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2648 "Unable to connect to GNS!\n");
2652 memset (&sa, 0, sizeof (sa));
2653 sa.sin_family = AF_INET;
2654 sa.sin_port = htons (port);
2655 #if HAVE_SOCKADDR_IN_SIN_LEN
2656 sa.sin_len = sizeof (sa);
2659 lsock = GNUNET_NETWORK_socket_create (AF_INET,
2663 if ((NULL == lsock) ||
2665 GNUNET_NETWORK_socket_bind (lsock, (const struct sockaddr *) &sa,
2668 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2669 "Failed to create listen socket bound to `%s'",
2670 GNUNET_a2s ((const struct sockaddr *) &sa, sizeof (sa)));
2672 GNUNET_NETWORK_socket_close (lsock);
2676 if (GNUNET_OK != GNUNET_NETWORK_socket_listen (lsock, 5))
2678 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2679 "Failed to listen on socket bound to `%s'",
2680 GNUNET_a2s ((const struct sockaddr *) &sa, sizeof (sa)));
2684 ltask = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
2685 lsock, &do_accept, NULL);
2690 if (0 != curl_global_init (CURL_GLOBAL_WIN32))
2692 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2693 "cURL global init failed!\n");
2697 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2698 "Proxy listens on port %u\n",
2701 mhd_httpd_head = NULL;
2702 mhd_httpd_tail = NULL;
2703 total_mhd_connections = 0;
2705 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "gns-proxy",
2709 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2710 "Specify PROXY_UNIXPATH in gns-proxy config section!\n");
2714 mhd_unix_socket = GNUNET_NETWORK_socket_create (AF_UNIX,
2718 if (NULL == mhd_unix_socket)
2720 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2721 "Unable to create unix domain socket!\n");
2725 mhd_unix_sock_addr.sun_family = AF_UNIX;
2726 strcpy (mhd_unix_sock_addr.sun_path, proxy_sockfile);
2729 mhd_unix_sock_addr.sun_path[0] = '\0';
2731 #if HAVE_SOCKADDR_IN_SIN_LEN
2732 mhd_unix_sock_addr.sun_len = (u_char) sizeof (struct sockaddr_un);
2735 len = strlen (proxy_sockfile) + sizeof(AF_UNIX);
2737 GNUNET_free (proxy_sockfile);
2739 if (GNUNET_OK != GNUNET_NETWORK_socket_bind (mhd_unix_socket,
2740 (struct sockaddr*)&mhd_unix_sock_addr,
2743 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2744 "Unable to bind unix domain socket!\n");
2748 if (GNUNET_OK != GNUNET_NETWORK_socket_listen (mhd_unix_socket,
2751 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2752 "Unable to listen on unix domain socket!\n");
2756 hd = GNUNET_malloc (sizeof (struct MhdHttpList));
2757 hd->is_ssl = GNUNET_NO;
2758 strcpy (hd->domain, "");
2759 httpd = MHD_start_daemon (MHD_USE_DEBUG, 4444, //Dummy port
2761 &create_response, hd,
2762 MHD_OPTION_LISTEN_SOCKET, GNUNET_NETWORK_get_fd (mhd_unix_socket),
2763 MHD_OPTION_CONNECTION_LIMIT, MHD_MAX_CONNECTIONS,
2764 MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) 16,
2765 MHD_OPTION_NOTIFY_COMPLETED,
2769 GNUNET_assert (httpd != NULL);
2771 hd->httpd_task = GNUNET_SCHEDULER_NO_TASK;
2773 GNUNET_CONTAINER_DLL_insert (mhd_httpd_head, mhd_httpd_tail, hd);
2777 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
2778 &do_shutdown, NULL);
2784 * The main function for gnunet-gns-proxy.
2786 * @param argc number of arguments from the command line
2787 * @param argv command line arguments
2788 * @return 0 ok, 1 on error
2791 main (int argc, char *const *argv)
2793 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
2795 gettext_noop ("listen on specified port"), 1,
2796 &GNUNET_GETOPT_set_string, &port},
2797 {'a', "authority", NULL,
2798 gettext_noop ("pem file to use as CA"), 1,
2799 &GNUNET_GETOPT_set_string, &cafile_opt},
2800 GNUNET_GETOPT_OPTION_END
2805 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
2809 GNUNET_log_setup ("gnunet-gns-proxy", "WARNING", NULL);
2812 GNUNET_PROGRAM_run (argc, argv, "gnunet-gns-proxy",
2813 _("GNUnet GNS proxy"),
2815 &run, NULL)) ? 0 : 1;
2816 GNUNET_free_non_null ((char*)argv);