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
39 /* MHD/cURL defines */
40 #define BUF_WAIT_FOR_CURL 0
41 #define BUF_WAIT_FOR_MHD 1
42 #define HTML_HDR_CONTENT "Content-Type: text/html\r\n"
45 #define RE_DOTPLUS "<a href=\"http://(([A-Za-z]+[.])+)([+])"
46 #define RE_A_HREF "<a href=\"https?://(([A-Za-z]+[.])+)([+]|zkey)"
47 #define RE_N_MATCHES 4
49 /* The usual suspects */
51 #define HTTPS_PORT 443
55 * A structure for CA cert/key
60 gnutls_x509_crt_t cert;
63 gnutls_x509_privkey_t key;
68 * Structure for GNS certificates
70 struct ProxyGNSCertificate
72 /* The certificate as PEM */
75 /* The private key as PEM */
81 * A structure for socks requests
85 /* The client socket */
86 struct GNUNET_NETWORK_Handle *sock;
88 /* The server socket */
89 struct GNUNET_NETWORK_Handle *remote_sock;
94 /* Client socket read task */
95 GNUNET_SCHEDULER_TaskIdentifier rtask;
97 /* Server socket read task */
98 GNUNET_SCHEDULER_TaskIdentifier fwdrtask;
100 /* Client socket write task */
101 GNUNET_SCHEDULER_TaskIdentifier wtask;
103 /* Server socket write task */
104 GNUNET_SCHEDULER_TaskIdentifier fwdwtask;
112 /* Length of data in read buffer */
113 unsigned int rbuf_len;
115 /* Length of data in write buffer */
116 unsigned int wbuf_len;
118 /* This handle is scheduled for cleanup? */
121 /* Shall we close the client socket on cleanup? */
127 * A structure for all running Httpds
132 struct MhdHttpList *prev;
135 struct MhdHttpList *next;
137 /* is this an ssl daemon? */
140 /* the domain name to server (only important for SSL) */
143 /* The daemon handle */
144 struct MHD_Daemon *daemon;
147 GNUNET_SCHEDULER_TaskIdentifier httpd_task;
151 * A structure for MHD<->cURL streams
156 struct ProxyCurlTask *prev;
159 struct ProxyCurlTask *next;
164 /* Optional header replacements for curl (LEHO) */
165 struct curl_slist *headers;
167 /* Optional resolver replacements for curl (LEHO) */
168 struct curl_slist *resolver;
170 /* The URL to fetch */
173 /* The cURL write buffer / MHD read buffer */
174 char buffer[CURL_MAX_WRITE_SIZE];
176 /* The pointer to the data in the buffer */
179 /* The buffer status (BUF_WAIT_FOR_CURL or BUF_WAIT_FOR_MHD) */
182 /* Number of bytes in buffer */
183 unsigned int bytes_in_buffer;
185 /* Indicates wheather the download is in progress */
186 int download_in_progress;
188 /* Indicates wheather the download was successful */
189 int download_successful;
191 /* Indicates wheather the download failed */
194 /* Indicates wheather we need to parse HTML */
197 /* Indicates wheather we are postprocessing the HTML right now */
198 int is_postprocessing;
200 /* Indicates wheather postprocessing has finished */
203 /* Task ID of the postprocessing task */
204 GNUNET_SCHEDULER_TaskIdentifier pp_task;
206 /* The postprocessing buffer TODO length? */
209 /* The authority of the corresponding host (site of origin) */
212 /* The hostname (Host header field) */
215 /* The LEgacy HOstname (can be empty) */
218 /* The associated daemon list entry */
219 struct MhdHttpList *mhd;
223 /* The port the proxy is running on (default 7777) */
224 static unsigned long port = GNUNET_GNS_PROXY_PORT;
226 /* The CA file (pem) to use for the proxy CA */
229 /* The listen socket of the proxy */
230 static struct GNUNET_NETWORK_Handle *lsock;
232 /* The listen task ID */
233 GNUNET_SCHEDULER_TaskIdentifier ltask;
235 /* The cURL download task */
236 GNUNET_SCHEDULER_TaskIdentifier curl_download_task;
238 /* The non SSL httpd daemon handle */
239 static struct MHD_Daemon *httpd;
241 /* The cURL multi handle */
242 static CURLM *curl_multi;
244 /* Handle to the GNS service */
245 static struct GNUNET_GNS_Handle *gns_handle;
247 /* DLL for ProxyCurlTasks */
248 static struct ProxyCurlTask *ctasks_head;
250 /* DLL for ProxyCurlTasks */
251 static struct ProxyCurlTask *ctasks_tail;
253 /* DLL for http daemons */
254 static struct MhdHttpList *mhd_httpd_head;
256 /* DLL for http daemons */
257 static struct MhdHttpList *mhd_httpd_tail;
259 /* Handle to the regex for dotplus (.+) replacement in HTML */
260 static regex_t re_dotplus;
262 /* The users local GNS zone hash */
263 static struct GNUNET_CRYPTO_ShortHashCode local_gns_zone;
265 /* The CA for SSL certificate generation */
266 static struct ProxyCA proxy_ca;
269 * Checks if name is in tld
271 * @param name the name to check
272 * @param tld the TLD to check for
273 * @return GNUNET_YES or GNUNET_NO
276 is_tld(const char* name, const char* tld)
280 if (strlen(name) <= strlen(tld))
285 offset = strlen(name)-strlen(tld);
286 if (strcmp (name+offset, tld) != 0)
288 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
289 "%s is not in .%s TLD\n", name, tld);
298 * Read HTTP request header field 'Host'
300 * @param cls buffer to write to
301 * @param kind value kind
302 * @param key field key
303 * @param value field value
304 * @return MHD_NO when Host found
307 con_val_iter (void *cls,
308 enum MHD_ValueKind kind,
312 char* buf = (char*)cls;
314 if (0 == strcmp ("Host", key))
324 * Check HTTP response header for mime
326 * @param buffer curl buffer
327 * @param size curl blocksize
328 * @param nmemb curl blocknumber
330 * @return size of read bytes
333 curl_check_hdr (void *buffer, size_t size, size_t nmemb, void *cls)
335 size_t bytes = size * nmemb;
336 struct ProxyCurlTask *ctask = cls;
339 memcpy (hdr, buffer, bytes);
342 if (0 == strcmp (hdr, HTML_HDR_CONTENT))
344 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
345 "Got HTML HTTP response header\n");
346 ctask->parse_content = GNUNET_YES;
355 * @param hd a http daemon list entry
358 run_httpd (struct MhdHttpList *hd);
370 * Task that simply runs MHD main loop
373 * @param tc task context
376 run_mhd (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
379 struct MhdHttpList *hd = cls;
381 //for (hd=mhd_httpd_head; hd != NULL; hd = hd->next)
382 MHD_run (hd->daemon);
387 * Process cURL download bits
389 * @param ptr buffer with data
390 * @param size size of a record
391 * @param nmemb number of records downloaded
393 * @return number of processed bytes
396 callback_download (void *ptr, size_t size, size_t nmemb, void *ctx)
398 const char *cbuf = ptr;
400 struct ProxyCurlTask *ctask = ctx;
411 if (total > sizeof (ctask->buffer))
413 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
414 "CURL gave us too much data to handle (%d)!\n",
419 if (ctask->buf_status == BUF_WAIT_FOR_MHD)
421 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
422 "CURL: Waiting for MHD (%s)\n", ctask->url);
423 return CURL_WRITEFUNC_PAUSE;
427 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
428 "CURL: Copying to MHD (%s, %d)\n", ctask->url, total);
429 memcpy (ctask->buffer, cbuf, total);
430 ctask->bytes_in_buffer = total;
431 ctask->buffer_ptr = ctask->buffer;
433 ctask->buf_status = BUF_WAIT_FOR_MHD;
435 //GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
436 // "cURL chunk:\n%s\n", (char*)ctask->buffer);
437 //run_mhd (NULL, NULL);
438 GNUNET_SCHEDULER_add_now (&run_mhd, ctask->mhd);
445 * Callback to free content
447 * @param cls content to free
450 mhd_content_free (void *cls)
452 struct ProxyCurlTask *ctask = cls;
454 if (NULL != ctask->headers)
455 curl_slist_free_all (ctask->headers);
457 if (NULL != ctask->curl)
458 curl_easy_cleanup (ctask->curl);
468 * Shorten result callback
470 * @param cls the proxycurltask
471 * @param short_name the shortened name (NULL on error)
474 process_shorten (void* cls, const char* short_name)
476 struct ProxyCurlTask *ctask = cls;
478 char tmp[strlen(ctask->pp_buf)]; //TODO length
480 if (NULL == short_name)
482 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
483 "MHD PP: Unable to shorten %s\n",
488 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
489 "MHD PP: Shorten %s -> %s\n",
493 sprintf (tmp, "<a href=http://%s", short_name);
494 strcpy (ctask->pp_buf, tmp);
496 ctask->pp_finished = GNUNET_YES;
498 GNUNET_SCHEDULER_add_now (&run_mhd, ctask->mhd);
503 * Postprocessing task that uses GNS to shorten names
505 * @param cls the proxycurltask
506 * @param tc the task context
509 postprocess_name (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
511 struct ProxyCurlTask *ctask = cls;
512 char tmp[strlen(ctask->pp_buf)];
514 sprintf ( tmp, "%s%s", ctask->pp_buf, ctask->authority);
516 GNUNET_GNS_shorten (gns_handle,
524 * Callback for MHD response
527 * @param pos in buffer
529 * @param max space in buffer
532 mhd_content_cb (void *cls,
537 struct ProxyCurlTask *ctask = cls;
539 size_t bytes_to_copy;
542 regmatch_t m[RE_N_MATCHES];
544 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
545 "MHD: content cb\n");
547 if (ctask->download_successful &&
548 (ctask->buf_status == BUF_WAIT_FOR_CURL))
550 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
551 "MHD: sending response for %s\n", ctask->url);
552 ctask->download_in_progress = GNUNET_NO;
553 curl_multi_remove_handle (curl_multi, ctask->curl);
554 curl_easy_cleanup (ctask->curl);
555 GNUNET_SCHEDULER_add_now (&run_mhd, ctask->mhd);
556 return MHD_CONTENT_READER_END_OF_STREAM;
559 if (ctask->download_error &&
560 (ctask->buf_status == BUF_WAIT_FOR_CURL))
562 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
563 "MHD: sending error response\n");
564 ctask->download_in_progress = GNUNET_NO;
565 curl_multi_remove_handle (curl_multi, ctask->curl);
566 curl_easy_cleanup (ctask->curl);
567 GNUNET_SCHEDULER_add_now (&run_mhd, ctask->mhd);
568 return MHD_CONTENT_READER_END_WITH_ERROR;
571 if ( ctask->buf_status == BUF_WAIT_FOR_CURL )
574 bytes_to_copy = ctask->bytes_in_buffer;
576 if (ctask->parse_content == GNUNET_YES)
579 GNUNET_log ( GNUNET_ERROR_TYPE_DEBUG,
580 "MHD: We need to parse the HTML %s\n", ctask->buffer_ptr);
582 nomatch = regexec ( &re_dotplus, ctask->buffer_ptr, RE_N_MATCHES, m, 0);
586 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
589 GNUNET_assert (m[1].rm_so != -1);
591 hostptr = ctask->buffer_ptr+m[1].rm_so;
595 bytes_to_copy = m[0].rm_so;
596 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
597 "Copying %d bytes.\n", m[0].rm_so);
603 if (ctask->is_postprocessing == GNUNET_YES)
607 if ( ctask->pp_finished == GNUNET_NO )
609 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
610 "MHD PP: Waiting for PP of %s\n", ctask->pp_buf);
614 ctask->is_postprocessing = GNUNET_NO;
616 ctask->bytes_in_buffer -= m[0].rm_eo;//(m[1].rm_eo-m[1].rm_so);
617 ctask->buffer_ptr += m[0].rm_eo;//(m[1].rm_eo-m[1].rm_so);
618 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
619 "Skipping next %d bytes in buffer\n", m[0].rm_eo);
621 GNUNET_SCHEDULER_add_now (&run_mhd, ctask->mhd);
623 if ( strlen (ctask->pp_buf) <= max )
625 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
626 "Copying postprocessed %s.\n", ctask->pp_buf);
627 memcpy ( buf, ctask->pp_buf, strlen (ctask->pp_buf) );
628 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
630 ctask->is_postprocessing = GNUNET_NO;
631 return strlen (ctask->pp_buf);
637 memset (ctask->pp_buf, 0, sizeof(ctask->pp_buf));
639 /* If .+ extend with authority */
640 if (0 == strcmp (ctask->buffer_ptr+m[1].rm_eo, "+"))
642 memcpy (ctask->pp_buf, hostptr, (m[1].rm_eo-m[1].rm_so));
643 strcpy ( ctask->pp_buf+strlen(ctask->pp_buf),
646 /* If .zkey simply copy the name */
647 else if (0 == strcmp (ctask->buffer_ptr+m[1].rm_eo, "zkey", 4))
649 memcpy (ctask->pp_buf, hostptr, (m[1].rm_eo-m[1].rm_so+4));
652 ctask->is_postprocessing = GNUNET_YES;
653 ctask->pp_finished = GNUNET_NO;
655 GNUNET_GNS_shorten (gns_handle,
660 //postprocess_name(ctask, NULL);
661 //ctask->pp_task = GNUNET_SCHEDULER_add_now (&postprocess_name, ctask);
668 if ( bytes_to_copy > max )
670 GNUNET_log ( GNUNET_ERROR_TYPE_DEBUG,
671 "MHD: buffer in response too small! (%s)\n",
673 memcpy ( buf, ctask->buffer_ptr, max);
674 ctask->bytes_in_buffer -= max;
675 ctask->buffer_ptr += max;
680 GNUNET_log ( GNUNET_ERROR_TYPE_DEBUG,
681 "MHD: copying %d bytes to mhd response at offset %d\n",
684 memcpy ( buf, ctask->buffer_ptr, bytes_to_copy );
685 copied = bytes_to_copy;
686 if (bytes_to_copy < ctask->bytes_in_buffer)
688 ctask->bytes_in_buffer -= bytes_to_copy;
689 ctask->buffer_ptr += bytes_to_copy;
693 ctask->bytes_in_buffer = 0;
694 ctask->buf_status = BUF_WAIT_FOR_CURL;
695 ctask->buffer_ptr = ctask->buffer;
696 curl_easy_pause (ctask->curl, CURLPAUSE_CONT);
697 GNUNET_SCHEDULER_add_now (&run_mhd, ctask->mhd);
701 GNUNET_SCHEDULER_add_now (&run_mhd, ctask->mhd);
709 * Task that is run when we are ready to receive more data
713 * @param tc task context
716 curl_task_download (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
719 * Ask cURL for the select sets and schedule download
722 curl_download_prepare ()
729 struct GNUNET_NETWORK_FDSet *grs;
730 struct GNUNET_NETWORK_FDSet *gws;
732 struct GNUNET_TIME_Relative rtime;
738 mret = curl_multi_fdset (curl_multi, &rs, &ws, &es, &max);
740 if (mret != CURLM_OK)
742 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
743 "%s failed at %s:%d: `%s'\n",
744 "curl_multi_fdset", __FILE__, __LINE__,
745 curl_multi_strerror (mret));
750 mret = curl_multi_timeout (curl_multi, &to);
751 rtime = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, to);
753 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
754 "cURL multi fds: max=%d timeout=%llu\n", max, to);
756 grs = GNUNET_NETWORK_fdset_create ();
757 gws = GNUNET_NETWORK_fdset_create ();
758 GNUNET_NETWORK_fdset_copy_native (grs, &rs, max + 1);
759 GNUNET_NETWORK_fdset_copy_native (gws, &ws, max + 1);
760 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
761 "Scheduling task cURL\n");
763 if (curl_download_task != GNUNET_SCHEDULER_NO_TASK)
764 GNUNET_SCHEDULER_cancel (curl_download_task);
767 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
770 &curl_task_download, curl_multi);
771 GNUNET_NETWORK_fdset_destroy (gws);
772 GNUNET_NETWORK_fdset_destroy (grs);
778 * Task that is run when we are ready to receive more data
782 * @param tc task context
785 curl_task_download (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
791 struct ProxyCurlTask *ctask;
794 curl_download_task = GNUNET_SCHEDULER_NO_TASK;
796 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
798 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
799 "Shutdown requested while trying to download\n");
803 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
811 mret = curl_multi_perform (curl_multi, &running);
813 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
814 "Running curl tasks: %d\n", running);
817 for (; ctask != NULL; ctask = ctask->next)
819 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
820 "CTask: %s\n", ctask->url);
824 if (num_ctasks != running)
826 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
827 "%d ctasks, %d curl running\n", num_ctasks, running);
833 msg = curl_multi_info_read (curl_multi, &msgnum);
834 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
835 "Messages left: %d\n", msgnum);
842 if ((msg->data.result != CURLE_OK) &&
843 (msg->data.result != CURLE_GOT_NOTHING))
845 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
846 "Download curl failed");
848 for (; ctask != NULL; ctask = ctask->next)
850 if (memcmp (msg->easy_handle, ctask->curl, sizeof (CURL)) != 0)
853 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
854 "Download curl failed for task %s: %s.\n",
856 curl_easy_strerror (msg->data.result));
857 ctask->download_successful = GNUNET_NO;
858 ctask->download_error = GNUNET_YES;
859 //curl_multi_remove_handle (curl_multi, ctask->curl);
860 //curl_easy_cleanup (ctask->curl);
861 GNUNET_CONTAINER_DLL_remove (ctasks_head, ctasks_tail,
865 GNUNET_assert (ctask != NULL);
869 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
870 "cURL download completed.\n");
872 for (; ctask != NULL; ctask = ctask->next)
874 if (memcmp (msg->easy_handle, ctask->curl, sizeof (CURL)) != 0)
877 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
878 "cURL task %s found.\n", ctask->url);
879 ctask->download_successful = GNUNET_YES;
880 //curl_multi_remove_handle (curl_multi, ctask->curl);
881 //curl_easy_cleanup (ctask->curl);
882 GNUNET_CONTAINER_DLL_remove (ctasks_head, ctasks_tail,
886 GNUNET_assert (ctask != NULL);
888 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
889 "curl end %s\n", curl_easy_strerror(msg->data.result));
895 } while (msgnum > 0);
898 for (ctask=ctasks_head; ctask != NULL; ctask = ctask->next)
903 if (num_ctasks != running)
905 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
906 "%d ctasks, %d curl running\n", num_ctasks, running);
909 GNUNET_assert ( num_ctasks == running );
913 } while (mret == CURLM_CALL_MULTI_PERFORM);
916 if (mret != CURLM_OK)
918 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "%s failed at %s:%d: `%s'\n",
919 "curl_multi_perform", __FILE__, __LINE__,
920 curl_multi_strerror (mret));
922 curl_download_prepare();
926 * Process LEHO lookup
928 * @param cls the ctask
929 * @param rd_count number of records returned
930 * @param rd record data
933 process_leho_lookup (void *cls,
935 const struct GNUNET_NAMESTORE_RecordData *rd)
937 struct ProxyCurlTask *ctask = cls;
938 char hosthdr[262]; //256 + "Host: "
942 struct hostent *phost;
944 char resolvename[512];
947 ctask->headers = NULL;
949 strcpy (ctask->leho, "");
951 for (i=0; i<rd_count; i++)
953 if (rd[i].record_type != GNUNET_GNS_RECORD_LEHO)
956 memcpy (ctask->leho, rd[i].data, rd[i].data_size);
958 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
959 "Found LEHO %s for %s\n", ctask->leho, ctask->url);
962 if (0 != strcmp (ctask->leho, ""))
964 sprintf (hosthdr, "%s%s", "Host: ", ctask->leho);
965 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
966 "New HTTP header value: %s\n", hosthdr);
967 ctask->headers = curl_slist_append (ctask->headers, hosthdr);
968 GNUNET_assert (NULL != ctask->headers);
969 ret = curl_easy_setopt (ctask->curl, CURLOPT_HTTPHEADER, ctask->headers);
972 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "%s failed at %s:%d: `%s'\n",
973 "curl_easy_setopt", __FILE__, __LINE__, curl_easy_strerror(ret));
978 if (ctask->mhd->is_ssl)
980 phost = (struct hostent*)gethostbyname (ctask->host);
981 ssl_ip = inet_ntoa(*((struct in_addr*)(phost->h_addr)));
982 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
983 "SSL target server: %s\n", ssl_ip);
984 sprintf (resolvename, "%s:%d:%s", ctask->leho, HTTPS_PORT, ssl_ip);
985 ctask->resolver = curl_slist_append ( ctask->resolver, resolvename);
986 curl_easy_setopt (ctask->curl, CURLOPT_RESOLVE, ctask->resolver);
987 sprintf (curlurl, "https://%s%s", ctask->leho, ctask->url);
988 curl_easy_setopt (ctask->curl, CURLOPT_URL, curlurl);
991 if (CURLM_OK != (mret=curl_multi_add_handle (curl_multi, ctask->curl)))
993 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
994 "%s failed at %s:%d: `%s'\n",
995 "curl_multi_add_handle", __FILE__, __LINE__,
996 curl_multi_strerror (mret));
997 ctask->download_successful = GNUNET_NO;
998 ctask->download_error = GNUNET_YES;
1001 GNUNET_CONTAINER_DLL_insert (ctasks_head, ctasks_tail, ctask);
1003 curl_download_prepare ();
1008 * Initialize download and trigger curl
1010 * @param cls the proxycurltask
1011 * @param auth_name the name of the authority (site of origin) of ctask->host
1015 process_get_authority (void *cls,
1016 const char* auth_name)
1018 struct ProxyCurlTask *ctask = cls;
1020 if (NULL == auth_name)
1022 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1023 "Get authority failed!\n");
1024 strcpy (ctask->authority, "");
1027 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1028 "Get authority yielded %s\n", auth_name);
1029 strcpy (ctask->authority, auth_name);
1031 GNUNET_GNS_lookup_zone (gns_handle,
1034 GNUNET_GNS_RECORD_LEHO,
1035 &process_leho_lookup,
1040 * Main MHD callback for handling requests.
1043 * @param con MHD connection handle
1044 * @param meth the HTTP method used ("GET", "PUT", etc.)
1045 * @param ver the HTTP version string (i.e. "HTTP/1.1")
1046 * @param upload_data the data being uploaded (excluding HEADERS,
1047 * for a POST that fits into memory and that is encoded
1048 * with a supported encoding, the POST data will NOT be
1049 * given in upload_data and is instead available as
1050 * part of MHD_get_connection_values; very large POST
1051 * data *will* be made available incrementally in
1053 * @param upload_data_size set initially to the size of the
1054 * upload_data provided; the method must update this
1055 * value to the number of bytes NOT processed;
1056 * @param con_cls pointer to location where we store the 'struct Request'
1057 * @return MHD_YES if the connection was handled successfully,
1058 * MHD_NO if the socket must be closed due to a serious
1059 * error while handling the request
1062 create_response (void *cls,
1063 struct MHD_Connection *con,
1067 const char *upload_data,
1068 size_t *upload_data_size,
1072 struct MhdHttpList* hd = cls;
1073 const char* page = "<html><head><title>gnoxy</title>"\
1074 "</head><body>cURL fail</body></html>";
1075 struct MHD_Response *response;
1080 struct ProxyCurlTask *ctask;
1082 if (0 != strcmp (meth, "GET"))
1084 if (&dummy != *con_cls)
1090 if (0 != *upload_data_size)
1095 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1098 MHD_get_connection_values (con,
1100 &con_val_iter, host);
1104 ctask = GNUNET_malloc (sizeof (struct ProxyCurlTask));
1106 ctask->curl = curl_easy_init();
1108 if (curl_multi == NULL)
1109 curl_multi = curl_multi_init ();
1111 if ((ctask->curl == NULL) || (curl_multi == NULL))
1113 response = MHD_create_response_from_buffer (strlen (page),
1115 MHD_RESPMEM_PERSISTENT);
1116 ret = MHD_queue_response (con,
1119 MHD_destroy_response (response);
1120 GNUNET_free (ctask);
1124 ctask->download_in_progress = GNUNET_YES;
1125 ctask->download_successful = GNUNET_NO;
1126 ctask->buf_status = BUF_WAIT_FOR_CURL;
1127 ctask->bytes_in_buffer = 0;
1128 ctask->parse_content = GNUNET_NO;
1130 curl_easy_setopt (ctask->curl, CURLOPT_HEADERFUNCTION, &curl_check_hdr);
1131 curl_easy_setopt (ctask->curl, CURLOPT_HEADERDATA, ctask);
1132 curl_easy_setopt (ctask->curl, CURLOPT_WRITEFUNCTION, &callback_download);
1133 curl_easy_setopt (ctask->curl, CURLOPT_WRITEDATA, ctask);
1134 curl_easy_setopt (ctask->curl, CURLOPT_FOLLOWLOCATION, 1);
1135 curl_easy_setopt (ctask->curl, CURLOPT_MAXREDIRS, 4);
1136 /* no need to abort if the above failed */
1137 if (GNUNET_NO == ctask->mhd->is_ssl)
1138 sprintf (curlurl, "http://%s%s", host, url);
1139 strcpy (ctask->host, host);
1140 strcpy (ctask->url, url);
1141 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1142 "Adding new curl task for %s\n", curlurl);
1144 curl_easy_setopt (ctask->curl, CURLOPT_URL, curlurl);
1145 curl_easy_setopt (ctask->curl, CURLOPT_FAILONERROR, 1);
1146 curl_easy_setopt (ctask->curl, CURLOPT_CONNECTTIMEOUT, 600L);
1147 curl_easy_setopt (ctask->curl, CURLOPT_TIMEOUT, 600L);
1149 GNUNET_GNS_get_authority (gns_handle,
1151 &process_get_authority,
1153 //download_prepare (ctask);
1154 //curl_download_prepare ();
1156 response = MHD_create_response_from_callback (-1, -1,
1161 ret = MHD_queue_response (con, MHD_HTTP_OK, response);
1163 //MHD_destroy_response (response);
1169 * Task run whenever HTTP server operations are pending.
1172 * @param tc sched context
1175 do_httpd (void *cls,
1176 const struct GNUNET_SCHEDULER_TaskContext *tc);
1185 struct MhdHttpList *hd;
1187 for (hd=mhd_httpd_head; hd != NULL; hd = hd->next)
1196 run_httpd (struct MhdHttpList *hd)
1201 struct GNUNET_NETWORK_FDSet *wrs;
1202 struct GNUNET_NETWORK_FDSet *wws;
1203 struct GNUNET_NETWORK_FDSet *wes;
1206 unsigned MHD_LONG_LONG timeout;
1207 struct GNUNET_TIME_Relative tv;
1212 wrs = GNUNET_NETWORK_fdset_create ();
1213 wes = GNUNET_NETWORK_fdset_create ();
1214 wws = GNUNET_NETWORK_fdset_create ();
1216 GNUNET_assert (MHD_YES == MHD_get_fdset (hd->daemon, &rs, &ws, &es, &max));
1219 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1220 "MHD fds: max=%d\n", max);
1222 haveto = MHD_get_timeout (hd->daemon, &timeout);
1224 if (haveto == MHD_YES)
1225 tv.rel_value = (uint64_t) timeout;
1227 tv = GNUNET_TIME_UNIT_FOREVER_REL;
1228 GNUNET_NETWORK_fdset_copy_native (wrs, &rs, max + 1);
1229 GNUNET_NETWORK_fdset_copy_native (wws, &ws, max + 1);
1230 GNUNET_NETWORK_fdset_copy_native (wes, &es, max + 1);
1232 if (hd->httpd_task != GNUNET_SCHEDULER_NO_TASK)
1233 GNUNET_SCHEDULER_cancel (hd->httpd_task);
1235 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_HIGH,
1238 GNUNET_NETWORK_fdset_destroy (wrs);
1239 GNUNET_NETWORK_fdset_destroy (wws);
1240 GNUNET_NETWORK_fdset_destroy (wes);
1245 * Task run whenever HTTP server operations are pending.
1248 * @param tc sched context
1251 do_httpd (void *cls,
1252 const struct GNUNET_SCHEDULER_TaskContext *tc)
1254 struct MhdHttpList *hd = cls;
1256 hd->httpd_task = GNUNET_SCHEDULER_NO_TASK;
1258 MHD_run (hd->daemon);
1264 * Read data from socket
1266 * @param cls the closure
1267 * @param tc scheduler context
1270 do_read (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
1273 * Read from remote end
1275 * @param cls closure
1276 * @param tc scheduler context
1279 do_read_remote (void* cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
1282 * Write data to remote socket
1284 * @param cls the closure
1285 * @param tc scheduler context
1288 do_write_remote (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1290 struct Socks5Request *s5r = cls;
1293 s5r->fwdwtask = GNUNET_SCHEDULER_NO_TASK;
1295 if ((NULL != tc->read_ready) &&
1296 (GNUNET_NETWORK_fdset_isset (tc->write_ready, s5r->remote_sock)) &&
1297 ((len = GNUNET_NETWORK_socket_send (s5r->remote_sock, s5r->rbuf,
1300 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1301 "Successfully sent %d bytes to remote socket\n",
1306 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "write remote");
1308 if (s5r->rtask != GNUNET_SCHEDULER_NO_TASK)
1309 GNUNET_SCHEDULER_cancel (s5r->rtask);
1310 if (s5r->wtask != GNUNET_SCHEDULER_NO_TASK)
1311 GNUNET_SCHEDULER_cancel (s5r->wtask);
1312 if (s5r->fwdrtask != GNUNET_SCHEDULER_NO_TASK)
1313 GNUNET_SCHEDULER_cancel (s5r->fwdrtask);
1314 GNUNET_NETWORK_socket_close (s5r->remote_sock);
1315 GNUNET_NETWORK_socket_close (s5r->sock);
1321 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
1328 * Clean up s5r handles
1330 * @param s5r the handle to destroy
1333 cleanup_s5r (struct Socks5Request *s5r)
1335 if (s5r->rtask != GNUNET_SCHEDULER_NO_TASK)
1336 GNUNET_SCHEDULER_cancel (s5r->rtask);
1337 if (s5r->fwdwtask != GNUNET_SCHEDULER_NO_TASK)
1338 GNUNET_SCHEDULER_cancel (s5r->fwdwtask);
1339 if (s5r->fwdrtask != GNUNET_SCHEDULER_NO_TASK)
1340 GNUNET_SCHEDULER_cancel (s5r->fwdrtask);
1342 if (NULL != s5r->remote_sock)
1343 GNUNET_NETWORK_socket_close (s5r->remote_sock);
1344 if ((NULL != s5r->sock) && (s5r->cleanup_sock == GNUNET_YES))
1345 GNUNET_NETWORK_socket_close (s5r->sock);
1351 * Write data to socket
1353 * @param cls the closure
1354 * @param tc scheduler context
1357 do_write (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1359 struct Socks5Request *s5r = cls;
1362 s5r->wtask = GNUNET_SCHEDULER_NO_TASK;
1364 if ((NULL != tc->read_ready) &&
1365 (GNUNET_NETWORK_fdset_isset (tc->write_ready, s5r->sock)) &&
1366 ((len = GNUNET_NETWORK_socket_send (s5r->sock, s5r->wbuf,
1369 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1370 "Successfully sent %d bytes to socket\n",
1376 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "write");
1377 s5r->cleanup = GNUNET_YES;
1378 s5r->cleanup_sock = GNUNET_YES;
1384 if (GNUNET_YES == s5r->cleanup)
1390 if ((s5r->state == SOCKS5_DATA_TRANSFER) &&
1391 (s5r->fwdrtask == GNUNET_SCHEDULER_NO_TASK))
1393 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
1395 &do_read_remote, s5r);
1399 * Read from remote end
1401 * @param cls closure
1402 * @param tc scheduler context
1405 do_read_remote (void* cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1407 struct Socks5Request *s5r = cls;
1409 s5r->fwdrtask = GNUNET_SCHEDULER_NO_TASK;
1412 if ((NULL != tc->write_ready) &&
1413 (GNUNET_NETWORK_fdset_isset (tc->read_ready, s5r->remote_sock)) &&
1414 (s5r->wbuf_len = GNUNET_NETWORK_socket_recv (s5r->remote_sock, s5r->wbuf,
1415 sizeof (s5r->wbuf))))
1417 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1418 "Successfully read %d bytes from remote socket\n",
1423 if (s5r->wbuf_len == 0)
1424 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1425 "0 bytes received from remote... graceful shutdown!\n");
1426 if (s5r->fwdwtask != GNUNET_SCHEDULER_NO_TASK)
1427 GNUNET_SCHEDULER_cancel (s5r->fwdwtask);
1428 if (s5r->rtask != GNUNET_SCHEDULER_NO_TASK)
1429 GNUNET_SCHEDULER_cancel (s5r->rtask);
1431 GNUNET_NETWORK_socket_close (s5r->remote_sock);
1432 s5r->remote_sock = NULL;
1433 GNUNET_NETWORK_socket_close (s5r->sock);
1439 s5r->wtask = GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
1447 * Adds a socket to MHD
1449 * @param h the handle to the socket to add
1450 * @return whatever MHD_add_connection returns
1453 add_handle_to_mhd (struct GNUNET_NETWORK_Handle *h, struct MHD_Daemon *daemon)
1456 struct sockaddr *addr;
1459 fd = GNUNET_NETWORK_get_fd (h);
1460 addr = GNUNET_NETWORK_get_addr (h);
1461 len = GNUNET_NETWORK_get_addrlen (h);
1463 return MHD_add_connection (daemon, fd, addr, len);
1467 /*TODO this needs MHD API modification */
1468 static int http_port = 4444;
1472 get_file_size (const char* filename)
1476 fp = fopen (filename, "rb");
1481 if ((0 != fseek (fp, 0, SEEK_END)) || (-1 == (size = ftell (fp))))
1493 * Read file in filename
1495 * @param filename file to read
1499 load_file (const char* filename)
1505 size = get_file_size (filename);
1509 fp = fopen (filename, "rb");
1513 buffer = GNUNET_malloc (size);
1520 if (size != fread (buffer, 1, size, fp))
1522 GNUNET_free (buffer);
1533 * Load PEM key from file
1535 * @param key where to store the data
1536 * @param keyfile path to the PEM file
1539 load_key_from_file (gnutls_x509_privkey_t key, char* keyfile)
1541 gnutls_datum_t key_data;
1544 key_data.data = (unsigned char*)load_file (keyfile);
1545 key_data.size = strlen ((char*)key_data.data);
1547 ret = gnutls_x509_privkey_import (key, &key_data,
1548 GNUTLS_X509_FMT_PEM);
1550 if (GNUTLS_E_SUCCESS != ret)
1552 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1553 "Unable to import private key %s\n", keyfile);
1559 * Load cert from file
1561 * @param crt struct to store data in
1562 * @param certfile path to pem file
1565 load_cert_from_file (gnutls_x509_crt_t crt, char* certfile)
1567 gnutls_datum_t cert_data;
1570 cert_data.data = (unsigned char*)load_file (certfile);
1571 cert_data.size = strlen ((char*)cert_data.data);
1573 ret = gnutls_x509_crt_import (crt, &cert_data,
1574 GNUTLS_X509_FMT_PEM);
1575 if (GNUTLS_E_SUCCESS != ret)
1577 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1578 "Unable to import certificate %s\n", certfile);
1586 * Generate new certificate for specific name
1588 * @param name the subject name to generate a cert for
1589 * @return a struct holding the PEM data
1591 static struct ProxyGNSCertificate *
1592 generate_gns_certificate (const char *name)
1596 unsigned int serial;
1598 size_t key_buf_size;
1599 size_t cert_buf_size;
1600 gnutls_x509_crt_t request;
1601 gnutls_x509_privkey_t rsa;
1605 ret = gnutls_x509_crt_init (&request);
1607 if (GNUTLS_E_SUCCESS != ret)
1612 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Generating key\n");
1613 gnutls_x509_privkey_init (&rsa);
1614 bits = gnutls_sec_param_to_pk_bits (GNUTLS_PK_RSA, GNUTLS_SEC_PARAM_NORMAL);
1615 ret = gnutls_x509_privkey_generate (rsa, GNUTLS_PK_RSA, bits, 0);
1617 if (GNUTLS_E_SUCCESS != ret)
1622 ret = gnutls_x509_crt_set_key (request, rsa);
1624 if (GNUTLS_E_SUCCESS != ret)
1629 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Generating cert\n");
1631 struct ProxyGNSCertificate *pgc =
1632 GNUNET_malloc (sizeof (struct ProxyGNSCertificate));
1634 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Adding DNs\n");
1636 gnutls_x509_crt_set_dn_by_oid (request, GNUTLS_OID_X520_COUNTRY_NAME,
1639 gnutls_x509_crt_set_dn_by_oid (request, GNUTLS_OID_X520_ORGANIZATION_NAME,
1642 gnutls_x509_crt_set_dn_by_oid (request, GNUTLS_OID_X520_COMMON_NAME,
1643 0, name, strlen (name));
1645 ret = gnutls_x509_crt_set_version (request, 3);
1647 ret = gnutls_rnd (GNUTLS_RND_NONCE, &serial, sizeof (serial));
1649 etime = time (NULL);
1650 tm_data = localtime (&etime);
1653 ret = gnutls_x509_crt_set_serial (request,
1657 ret = gnutls_x509_crt_set_activation_time (request,
1660 etime = mktime (tm_data);
1667 ret = gnutls_x509_crt_set_expiration_time (request,
1669 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Signing...\n");
1671 ret = gnutls_x509_crt_sign (request, proxy_ca.cert, proxy_ca.key);
1673 key_buf_size = sizeof (pgc->key);
1674 cert_buf_size = sizeof (pgc->cert);
1676 gnutls_x509_crt_export (request, GNUTLS_X509_FMT_PEM,
1677 pgc->cert, &cert_buf_size);
1679 gnutls_x509_privkey_export (rsa, GNUTLS_X509_FMT_PEM,
1680 pgc->key, &key_buf_size);
1683 gnutls_x509_crt_deinit (request);
1684 gnutls_x509_privkey_deinit (rsa);
1692 * Adds a socket to an SSL MHD instance
1693 * It is important the the domain name is
1694 * correct. In most cases we need to start a new daemon
1697 add_handle_to_ssl_mhd (struct GNUNET_NETWORK_Handle *h, char* domain)
1699 struct MhdHttpList *hd = NULL;
1700 struct ProxyGNSCertificate *pgc;
1702 for (hd = mhd_httpd_head; hd != NULL; hd = hd->next)
1704 if (0 == strcmp (hd->domain, domain))
1711 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1712 "No previous SSL instance found... starting new one for %s\n",
1715 pgc = generate_gns_certificate (domain);
1717 hd = GNUNET_malloc (sizeof (struct MhdHttpList));
1718 hd->is_ssl = GNUNET_YES;
1719 strcpy (hd->domain, domain);
1720 hd->daemon = MHD_start_daemon (MHD_USE_DEBUG | MHD_USE_SSL, http_port++,
1722 &create_response, hd,
1723 MHD_OPTION_CONNECTION_LIMIT, (unsigned int) 128,
1724 MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) 16,
1725 MHD_OPTION_NOTIFY_COMPLETED,
1727 MHD_OPTION_HTTPS_MEM_KEY, pgc->key,
1728 MHD_OPTION_HTTPS_MEM_CERT, pgc->cert,
1730 hd->httpd_task = GNUNET_SCHEDULER_NO_TASK;
1732 GNUNET_CONTAINER_DLL_insert (mhd_httpd_head, mhd_httpd_tail, hd);
1735 return add_handle_to_mhd (h, hd->daemon);
1741 * Read data from incoming connection
1743 * @param cls the closure
1744 * @param tc the scheduler context
1747 do_read (void* cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1749 struct Socks5Request *s5r = cls;
1750 struct socks5_client_hello *c_hello;
1751 struct socks5_server_hello *s_hello;
1752 struct socks5_client_request *c_req;
1753 struct socks5_server_response *s_resp;
1759 struct hostent *phost;
1761 struct sockaddr_in remote_addr;
1762 struct in_addr *r_sin_addr;
1764 s5r->rtask = GNUNET_SCHEDULER_NO_TASK;
1766 if ((NULL != tc->write_ready) &&
1767 (GNUNET_NETWORK_fdset_isset (tc->read_ready, s5r->sock)) &&
1768 (s5r->rbuf_len = GNUNET_NETWORK_socket_recv (s5r->sock, s5r->rbuf,
1769 sizeof (s5r->rbuf))))
1771 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1772 "Successfully read %d bytes from socket\n",
1777 if (s5r->rbuf_len != 0)
1778 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "read");
1780 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client disco!\n");
1782 if (s5r->fwdrtask != GNUNET_SCHEDULER_NO_TASK)
1783 GNUNET_SCHEDULER_cancel (s5r->fwdrtask);
1784 if (s5r->wtask != GNUNET_SCHEDULER_NO_TASK)
1785 GNUNET_SCHEDULER_cancel (s5r->wtask);
1786 if (s5r->fwdwtask != GNUNET_SCHEDULER_NO_TASK)
1787 GNUNET_SCHEDULER_cancel (s5r->fwdwtask);
1788 GNUNET_NETWORK_socket_close (s5r->remote_sock);
1789 GNUNET_NETWORK_socket_close (s5r->sock);
1794 if (s5r->state == SOCKS5_INIT)
1796 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1798 c_hello = (struct socks5_client_hello*)&s5r->rbuf;
1800 GNUNET_assert (c_hello->version == SOCKS_VERSION_5);
1802 s_hello = (struct socks5_server_hello*)&s5r->wbuf;
1803 s5r->wbuf_len = sizeof( struct socks5_server_hello );
1805 s_hello->version = c_hello->version;
1806 s_hello->auth_method = SOCKS_AUTH_NONE;
1808 /* Write response to client */
1809 s5r->wtask = GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
1813 s5r->rtask = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
1817 s5r->state = SOCKS5_REQUEST;
1821 if (s5r->state == SOCKS5_REQUEST)
1823 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1824 "Processing SOCKS5 request\n");
1825 c_req = (struct socks5_client_request*)&s5r->rbuf;
1826 s_resp = (struct socks5_server_response*)&s5r->wbuf;
1827 //Only 10byte for ipv4 response!
1828 s5r->wbuf_len = 10;//sizeof (struct socks5_server_response);
1830 GNUNET_assert (c_req->addr_type == 3);
1832 dom_len = *((uint8_t*)(&(c_req->addr_type) + 1));
1833 memset(domain, 0, sizeof(domain));
1834 strncpy(domain, (char*)(&(c_req->addr_type) + 2), dom_len);
1835 req_port = *((uint16_t*)(&(c_req->addr_type) + 2 + dom_len));
1837 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1838 "Requested connection is %s:%d\n",
1842 if (is_tld (domain, GNUNET_GNS_TLD) ||
1843 is_tld (domain, GNUNET_GNS_TLD_ZKEY))
1845 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1846 "Requested connection is gnunet tld\n",
1850 if (ntohs(req_port) == HTTPS_PORT)
1852 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1853 "Requested connection is HTTPS\n");
1854 ret = add_handle_to_ssl_mhd ( s5r->sock, domain );
1856 else if (NULL != httpd)
1858 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1859 "Requested connection is HTTP\n");
1860 ret = add_handle_to_mhd ( s5r->sock, httpd );
1865 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1866 _("Failed to start HTTP server\n"));
1867 s_resp->version = 0x05;
1868 s_resp->reply = 0x01;
1869 s5r->cleanup = GNUNET_YES;
1870 s5r->cleanup_sock = GNUNET_YES;
1872 GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
1878 /* Signal success */
1879 s_resp->version = 0x05;
1880 s_resp->reply = 0x00;
1881 s_resp->reserved = 0x00;
1882 s_resp->addr_type = 0x01;
1884 s5r->cleanup = GNUNET_YES;
1885 s5r->cleanup_sock = GNUNET_NO;
1887 GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
1895 phost = (struct hostent*)gethostbyname (domain);
1898 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1899 "Resolve %s error!\n", domain );
1900 s_resp->version = 0x05;
1901 s_resp->reply = 0x01;
1902 s5r->cleanup = GNUNET_YES;
1903 s5r->cleanup_sock = GNUNET_YES;
1905 GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
1911 s5r->remote_sock = GNUNET_NETWORK_socket_create (AF_INET,
1914 r_sin_addr = (struct in_addr*)(phost->h_addr);
1915 remote_ip = r_sin_addr->s_addr;
1916 memset(&remote_addr, 0, sizeof(remote_addr));
1917 remote_addr.sin_family = AF_INET;
1918 #if HAVE_SOCKADDR_IN_SIN_LEN
1919 remote_addr.sin_len = sizeof (remote_addr);
1921 remote_addr.sin_addr.s_addr = remote_ip;
1922 remote_addr.sin_port = req_port;
1924 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1925 "target server: %s:%u\n", inet_ntoa(remote_addr.sin_addr),
1929 GNUNET_NETWORK_socket_connect ( s5r->remote_sock,
1930 (const struct sockaddr*)&remote_addr,
1931 sizeof (remote_addr)))
1932 && (errno != EINPROGRESS))
1934 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "connect");
1935 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1936 "socket request error...\n");
1937 s_resp->version = 0x05;
1938 s_resp->reply = 0x01;
1940 GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
1947 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1948 "new remote connection\n");
1950 s_resp->version = 0x05;
1951 s_resp->reply = 0x00;
1952 s_resp->reserved = 0x00;
1953 s_resp->addr_type = 0x01;
1955 s5r->state = SOCKS5_DATA_TRANSFER;
1958 GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
1962 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
1970 if (s5r->state == SOCKS5_DATA_TRANSFER)
1972 if ((s5r->remote_sock == NULL) || (s5r->rbuf_len == 0))
1974 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1975 "Closing connection to client\n");
1976 if (s5r->rtask != GNUNET_SCHEDULER_NO_TASK)
1977 GNUNET_SCHEDULER_cancel (s5r->rtask);
1978 if (s5r->fwdwtask != GNUNET_SCHEDULER_NO_TASK)
1979 GNUNET_SCHEDULER_cancel (s5r->fwdwtask);
1980 if (s5r->fwdrtask != GNUNET_SCHEDULER_NO_TASK)
1981 GNUNET_SCHEDULER_cancel (s5r->fwdrtask);
1982 if (s5r->fwdrtask != GNUNET_SCHEDULER_NO_TASK)
1983 GNUNET_SCHEDULER_cancel (s5r->fwdrtask);
1985 if (s5r->remote_sock != NULL)
1986 GNUNET_NETWORK_socket_close (s5r->remote_sock);
1987 GNUNET_NETWORK_socket_close (s5r->sock);
1992 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1993 "forwarding %d bytes from client\n", s5r->rbuf_len);
1996 GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
1998 &do_write_remote, s5r);
2000 if (s5r->fwdrtask == GNUNET_SCHEDULER_NO_TASK)
2003 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
2005 &do_read_remote, s5r);
2011 //GNUNET_CONTAINER_DLL_remove (s5conns.head, s5conns.tail, s5r);
2017 * Accept new incoming connections
2019 * @param cls the closure
2020 * @param tc the scheduler context
2023 do_accept (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2025 struct GNUNET_NETWORK_Handle *s;
2026 struct Socks5Request *s5r;
2028 ltask = GNUNET_SCHEDULER_NO_TASK;
2029 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
2032 ltask = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
2036 s = GNUNET_NETWORK_socket_accept (lsock, NULL, NULL);
2040 GNUNET_log_strerror (GNUNET_ERROR_TYPE_INFO, "accept");
2044 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2045 "Got an inbound connection, waiting for data\n");
2047 s5r = GNUNET_malloc (sizeof (struct Socks5Request));
2049 s5r->state = SOCKS5_INIT;
2050 s5r->wtask = GNUNET_SCHEDULER_NO_TASK;
2051 s5r->fwdwtask = GNUNET_SCHEDULER_NO_TASK;
2052 s5r->fwdrtask = GNUNET_SCHEDULER_NO_TASK;
2053 s5r->rtask = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
2056 //GNUNET_CONTAINER_DLL_insert (s5conns.head, s5conns.tail, s5r);
2061 * Task run on shutdown
2063 * @param cls closure
2064 * @param tc task context
2067 do_shutdown (void *cls,
2068 const struct GNUNET_SCHEDULER_TaskContext *tc)
2071 struct MhdHttpList *hd;
2072 struct MhdHttpList *tmp_hd;
2073 struct ProxyCurlTask *ctask;
2074 struct ProxyCurlTask *ctask_tmp;
2076 if (GNUNET_SCHEDULER_NO_TASK != curl_download_task)
2078 GNUNET_SCHEDULER_cancel (curl_download_task);
2079 curl_download_task = GNUNET_SCHEDULER_NO_TASK;
2082 for (hd = mhd_httpd_head; hd != NULL; hd = tmp_hd)
2086 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2087 "Stopping daemon\n");
2089 if (GNUNET_SCHEDULER_NO_TASK != hd->httpd_task)
2091 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2092 "Stopping select task %d\n",
2094 GNUNET_SCHEDULER_cancel (hd->httpd_task);
2095 hd->httpd_task = GNUNET_SCHEDULER_NO_TASK;
2098 if (NULL != hd->daemon)
2100 MHD_stop_daemon (hd->daemon);
2107 for (ctask=ctasks_head; ctask != NULL; ctask=ctask_tmp)
2109 ctask_tmp = ctask->next;
2111 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2112 "Cleaning up cURL task\n");
2114 if (ctask->curl != NULL)
2115 curl_easy_cleanup (ctask->curl);
2117 if (NULL != ctask->headers)
2118 curl_slist_free_all (ctask->headers);
2120 GNUNET_free (ctask);
2123 GNUNET_GNS_disconnect (gns_handle);
2128 * Compiles a regex for us
2130 * @param re ptr to re struct
2131 * @param rt the expression to compile
2132 * @return 0 on success
2135 compile_regex (regex_t *re, const char* rt)
2140 status = regcomp (re, rt, REG_EXTENDED|REG_NEWLINE);
2143 regerror (status, re, err, 1024);
2144 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2145 "Regex error compiling '%s': %s\n", rt, err);
2153 * Loads the users local zone key
2155 * @return GNUNET_YES on success
2158 load_local_zone_key (const struct GNUNET_CONFIGURATION_Handle *cfg)
2161 struct GNUNET_CRYPTO_RsaPrivateKey *key = NULL;
2162 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pkey;
2163 struct GNUNET_CRYPTO_ShortHashCode *zone = NULL;
2164 struct GNUNET_CRYPTO_ShortHashAsciiEncoded zonename;
2166 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "gns",
2167 "ZONEKEY", &keyfile))
2169 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2170 "Unable to load zone key config value!\n");
2174 if (GNUNET_NO == GNUNET_DISK_file_test (keyfile))
2176 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2177 "Unable to load zone key!\n");
2178 GNUNET_free(keyfile);
2182 key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
2183 GNUNET_CRYPTO_rsa_key_get_public (key, &pkey);
2184 GNUNET_CRYPTO_short_hash(&pkey,
2185 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
2187 zone = &local_gns_zone;
2188 GNUNET_CRYPTO_short_hash_to_enc (zone, &zonename);
2189 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2190 "Using zone: %s!\n", &zonename);
2191 GNUNET_CRYPTO_rsa_key_free(key);
2192 GNUNET_free(keyfile);
2200 * Main function that will be run
2202 * @param cls closure
2203 * @param args remaining command-line arguments
2204 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
2205 * @param cfg configuration
2208 run (void *cls, char *const *args, const char *cfgfile,
2209 const struct GNUNET_CONFIGURATION_Handle *cfg)
2211 struct sockaddr_in sa;
2212 struct MhdHttpList *hd;
2214 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2217 gnutls_global_init ();
2219 load_cert_from_file (proxy_ca.cert, cafile);
2220 load_key_from_file (proxy_ca.key, cafile);
2222 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2223 "Loading Template\n");
2225 compile_regex (&re_dotplus, (char*) RE_DOTPLUS);
2227 gns_handle = GNUNET_GNS_connect (cfg);
2229 if (GNUNET_NO == load_local_zone_key (cfg))
2231 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2232 "Unable to load zone!\n");
2236 if (NULL == gns_handle)
2238 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2239 "Unable to connect to GNS!\n");
2243 memset (&sa, 0, sizeof (sa));
2244 sa.sin_family = AF_INET;
2245 sa.sin_port = htons (port);
2246 #if HAVE_SOCKADDR_IN_SIN_LEN
2247 sa.sin_len = sizeof (sa);
2250 lsock = GNUNET_NETWORK_socket_create (AF_INET,
2254 if ((NULL == lsock) ||
2256 GNUNET_NETWORK_socket_bind (lsock, (const struct sockaddr *) &sa,
2259 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2260 "Failed to create listen socket bound to `%s'",
2261 GNUNET_a2s ((const struct sockaddr *) &sa, sizeof (sa)));
2263 GNUNET_NETWORK_socket_close (lsock);
2267 if (GNUNET_OK != GNUNET_NETWORK_socket_listen (lsock, 5))
2269 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2270 "Failed to listen on socket bound to `%s'",
2271 GNUNET_a2s ((const struct sockaddr *) &sa, sizeof (sa)));
2275 ltask = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
2276 lsock, &do_accept, NULL);
2281 if (0 != curl_global_init (CURL_GLOBAL_WIN32))
2283 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2284 "cURL global init failed!\n");
2288 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2289 "Proxy listens on port %u\n",
2292 mhd_httpd_head = NULL;
2293 mhd_httpd_tail = NULL;
2295 hd = GNUNET_malloc (sizeof (struct MhdHttpList));
2296 hd->is_ssl = GNUNET_NO;
2297 strcpy (hd->domain, "");
2298 httpd = MHD_start_daemon (MHD_USE_DEBUG, http_port++,
2300 &create_response, hd,
2301 MHD_OPTION_CONNECTION_LIMIT, (unsigned int) 128,
2302 MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) 16,
2303 MHD_OPTION_NOTIFY_COMPLETED,
2307 hd->httpd_task = GNUNET_SCHEDULER_NO_TASK;
2309 GNUNET_CONTAINER_DLL_insert (mhd_httpd_head, mhd_httpd_tail, hd);
2313 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
2314 &do_shutdown, NULL);
2320 * The main function for gnunet-gns-proxy.
2322 * @param argc number of arguments from the command line
2323 * @param argv command line arguments
2324 * @return 0 ok, 1 on error
2327 main (int argc, char *const *argv)
2329 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
2331 gettext_noop ("listen on specified port"), 1,
2332 &GNUNET_GETOPT_set_string, &port},
2333 {'a', "authority", NULL,
2334 gettext_noop ("pem file to use as CA"), 1,
2335 &GNUNET_GETOPT_set_string, &cafile},
2336 GNUNET_GETOPT_OPTION_END
2341 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
2344 GNUNET_log_setup ("gnunet-gns-proxy", "WARNING", NULL);
2347 GNUNET_PROGRAM_run (argc, argv, "gnunet-gns-proxy",
2348 _("GNUnet GNS proxy"),
2350 &run, NULL)) ? 0 : 1;