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-z0-9]+[.])+)([+]|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,
525 * Callback for MHD response
528 * @param pos in buffer
530 * @param max space in buffer
533 mhd_content_cb (void *cls,
538 struct ProxyCurlTask *ctask = cls;
540 size_t bytes_to_copy;
543 regmatch_t m[RE_N_MATCHES];
545 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
546 "MHD: content cb\n");
548 if (ctask->download_successful &&
549 (ctask->buf_status == BUF_WAIT_FOR_CURL))
551 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
552 "MHD: sending response for %s\n", ctask->url);
553 ctask->download_in_progress = GNUNET_NO;
554 curl_multi_remove_handle (curl_multi, ctask->curl);
555 curl_easy_cleanup (ctask->curl);
556 GNUNET_SCHEDULER_add_now (&run_mhd, ctask->mhd);
557 return MHD_CONTENT_READER_END_OF_STREAM;
560 if (ctask->download_error &&
561 (ctask->buf_status == BUF_WAIT_FOR_CURL))
563 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
564 "MHD: sending error response\n");
565 ctask->download_in_progress = GNUNET_NO;
566 curl_multi_remove_handle (curl_multi, ctask->curl);
567 curl_easy_cleanup (ctask->curl);
568 GNUNET_SCHEDULER_add_now (&run_mhd, ctask->mhd);
569 return MHD_CONTENT_READER_END_WITH_ERROR;
572 if ( ctask->buf_status == BUF_WAIT_FOR_CURL )
575 bytes_to_copy = ctask->bytes_in_buffer;
577 if (ctask->parse_content == GNUNET_YES)
580 GNUNET_log ( GNUNET_ERROR_TYPE_DEBUG,
581 "MHD: We need to parse the HTML %s\n", ctask->buffer_ptr);
583 nomatch = regexec ( &re_dotplus, ctask->buffer_ptr, RE_N_MATCHES, m, 0);
587 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
588 "MHD RE: No match\n");
592 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
595 GNUNET_assert (m[1].rm_so != -1);
597 hostptr = ctask->buffer_ptr+m[1].rm_so;
601 bytes_to_copy = m[0].rm_so;
602 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
603 "Copying %d bytes.\n", m[0].rm_so);
609 if (ctask->is_postprocessing == GNUNET_YES)
613 if ( ctask->pp_finished == GNUNET_NO )
615 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
616 "MHD PP: Waiting for PP of %s\n", ctask->pp_buf);
620 ctask->is_postprocessing = GNUNET_NO;
622 ctask->bytes_in_buffer -= m[0].rm_eo;//(m[1].rm_eo-m[1].rm_so);
623 ctask->buffer_ptr += m[0].rm_eo;//(m[1].rm_eo-m[1].rm_so);
624 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
625 "Skipping next %d bytes in buffer\n", m[0].rm_eo);
627 GNUNET_SCHEDULER_add_now (&run_mhd, ctask->mhd);
629 if ( strlen (ctask->pp_buf) <= max )
631 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
632 "Copying postprocessed %s.\n", ctask->pp_buf);
633 memcpy ( buf, ctask->pp_buf, strlen (ctask->pp_buf) );
634 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
636 ctask->is_postprocessing = GNUNET_NO;
637 return strlen (ctask->pp_buf);
643 memset (ctask->pp_buf, 0, sizeof(ctask->pp_buf));
645 /* If .+ extend with authority */
646 if (*(ctask->buffer_ptr+m[1].rm_eo) == '+')
648 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
650 memcpy (ctask->pp_buf, hostptr, (m[1].rm_eo-m[1].rm_so));
651 strcpy ( ctask->pp_buf+strlen(ctask->pp_buf),
654 /* If .zkey simply copy the name */
657 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
659 memcpy (ctask->pp_buf, hostptr, (m[1].rm_eo-m[1].rm_so + strlen (GNUNET_GNS_TLD_ZKEY)));
662 ctask->is_postprocessing = GNUNET_YES;
663 ctask->pp_finished = GNUNET_NO;
665 GNUNET_GNS_shorten (gns_handle,
670 //postprocess_name(ctask, NULL);
671 //ctask->pp_task = GNUNET_SCHEDULER_add_now (&postprocess_name, ctask);
678 if ( bytes_to_copy > max )
680 GNUNET_log ( GNUNET_ERROR_TYPE_DEBUG,
681 "MHD: buffer in response too small! (%s)\n",
683 memcpy ( buf, ctask->buffer_ptr, max);
684 ctask->bytes_in_buffer -= max;
685 ctask->buffer_ptr += max;
690 GNUNET_log ( GNUNET_ERROR_TYPE_DEBUG,
691 "MHD: copying %d bytes to mhd response at offset %d\n",
694 memcpy ( buf, ctask->buffer_ptr, bytes_to_copy );
695 copied = bytes_to_copy;
696 if (bytes_to_copy < ctask->bytes_in_buffer)
698 ctask->bytes_in_buffer -= bytes_to_copy;
699 ctask->buffer_ptr += bytes_to_copy;
703 ctask->bytes_in_buffer = 0;
704 ctask->buf_status = BUF_WAIT_FOR_CURL;
705 ctask->buffer_ptr = ctask->buffer;
706 curl_easy_pause (ctask->curl, CURLPAUSE_CONT);
707 GNUNET_SCHEDULER_add_now (&run_mhd, ctask->mhd);
711 GNUNET_SCHEDULER_add_now (&run_mhd, ctask->mhd);
719 * Task that is run when we are ready to receive more data
723 * @param tc task context
726 curl_task_download (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
729 * Ask cURL for the select sets and schedule download
732 curl_download_prepare ()
739 struct GNUNET_NETWORK_FDSet *grs;
740 struct GNUNET_NETWORK_FDSet *gws;
742 struct GNUNET_TIME_Relative rtime;
748 mret = curl_multi_fdset (curl_multi, &rs, &ws, &es, &max);
750 if (mret != CURLM_OK)
752 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
753 "%s failed at %s:%d: `%s'\n",
754 "curl_multi_fdset", __FILE__, __LINE__,
755 curl_multi_strerror (mret));
760 mret = curl_multi_timeout (curl_multi, &to);
761 rtime = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, to);
763 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
764 "cURL multi fds: max=%d timeout=%llu\n", max, to);
766 grs = GNUNET_NETWORK_fdset_create ();
767 gws = GNUNET_NETWORK_fdset_create ();
768 GNUNET_NETWORK_fdset_copy_native (grs, &rs, max + 1);
769 GNUNET_NETWORK_fdset_copy_native (gws, &ws, max + 1);
770 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
771 "Scheduling task cURL\n");
773 if (curl_download_task != GNUNET_SCHEDULER_NO_TASK)
774 GNUNET_SCHEDULER_cancel (curl_download_task);
777 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
780 &curl_task_download, curl_multi);
781 GNUNET_NETWORK_fdset_destroy (gws);
782 GNUNET_NETWORK_fdset_destroy (grs);
788 * Task that is run when we are ready to receive more data
792 * @param tc task context
795 curl_task_download (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
801 struct ProxyCurlTask *ctask;
804 curl_download_task = GNUNET_SCHEDULER_NO_TASK;
806 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
808 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
809 "Shutdown requested while trying to download\n");
813 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
821 mret = curl_multi_perform (curl_multi, &running);
823 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
824 "Running curl tasks: %d\n", running);
827 for (; ctask != NULL; ctask = ctask->next)
829 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
830 "CTask: %s\n", ctask->url);
834 if (num_ctasks != running)
836 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
837 "%d ctasks, %d curl running\n", num_ctasks, running);
843 msg = curl_multi_info_read (curl_multi, &msgnum);
844 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
845 "Messages left: %d\n", msgnum);
852 if ((msg->data.result != CURLE_OK) &&
853 (msg->data.result != CURLE_GOT_NOTHING))
855 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
856 "Download curl failed");
858 for (; ctask != NULL; ctask = ctask->next)
860 if (memcmp (msg->easy_handle, ctask->curl, sizeof (CURL)) != 0)
863 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
864 "Download curl failed for task %s: %s.\n",
866 curl_easy_strerror (msg->data.result));
867 ctask->download_successful = GNUNET_NO;
868 ctask->download_error = GNUNET_YES;
869 //curl_multi_remove_handle (curl_multi, ctask->curl);
870 //curl_easy_cleanup (ctask->curl);
871 GNUNET_CONTAINER_DLL_remove (ctasks_head, ctasks_tail,
875 GNUNET_assert (ctask != NULL);
879 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
880 "cURL download completed.\n");
882 for (; ctask != NULL; ctask = ctask->next)
884 if (memcmp (msg->easy_handle, ctask->curl, sizeof (CURL)) != 0)
887 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
888 "cURL task %s found.\n", ctask->url);
889 ctask->download_successful = GNUNET_YES;
890 //curl_multi_remove_handle (curl_multi, ctask->curl);
891 //curl_easy_cleanup (ctask->curl);
892 GNUNET_CONTAINER_DLL_remove (ctasks_head, ctasks_tail,
896 GNUNET_assert (ctask != NULL);
898 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
899 "curl end %s\n", curl_easy_strerror(msg->data.result));
905 } while (msgnum > 0);
908 for (ctask=ctasks_head; ctask != NULL; ctask = ctask->next)
913 if (num_ctasks != running)
915 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
916 "%d ctasks, %d curl running\n", num_ctasks, running);
919 GNUNET_assert ( num_ctasks == running );
923 } while (mret == CURLM_CALL_MULTI_PERFORM);
926 if (mret != CURLM_OK)
928 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "%s failed at %s:%d: `%s'\n",
929 "curl_multi_perform", __FILE__, __LINE__,
930 curl_multi_strerror (mret));
932 curl_download_prepare();
936 * Process LEHO lookup
938 * @param cls the ctask
939 * @param rd_count number of records returned
940 * @param rd record data
943 process_leho_lookup (void *cls,
945 const struct GNUNET_NAMESTORE_RecordData *rd)
947 struct ProxyCurlTask *ctask = cls;
948 char hosthdr[262]; //256 + "Host: "
952 struct hostent *phost;
954 char resolvename[512];
957 ctask->headers = NULL;
959 strcpy (ctask->leho, "");
962 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
963 "No LEHO present!\n");
965 for (i=0; i<rd_count; i++)
967 if (rd[i].record_type != GNUNET_GNS_RECORD_LEHO)
970 memcpy (ctask->leho, rd[i].data, rd[i].data_size);
972 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
973 "Found LEHO %s for %s\n", ctask->leho, ctask->url);
976 if (0 != strcmp (ctask->leho, ""))
978 sprintf (hosthdr, "%s%s", "Host: ", ctask->leho);
979 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
980 "New HTTP header value: %s\n", hosthdr);
981 ctask->headers = curl_slist_append (ctask->headers, hosthdr);
982 GNUNET_assert (NULL != ctask->headers);
983 ret = curl_easy_setopt (ctask->curl, CURLOPT_HTTPHEADER, ctask->headers);
986 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "%s failed at %s:%d: `%s'\n",
987 "curl_easy_setopt", __FILE__, __LINE__, curl_easy_strerror(ret));
992 if (ctask->mhd->is_ssl)
994 phost = (struct hostent*)gethostbyname (ctask->host);
995 ssl_ip = inet_ntoa(*((struct in_addr*)(phost->h_addr)));
996 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
997 "SSL target server: %s\n", ssl_ip);
998 sprintf (resolvename, "%s:%d:%s", ctask->leho, HTTPS_PORT, ssl_ip);
999 ctask->resolver = curl_slist_append ( ctask->resolver, resolvename);
1000 curl_easy_setopt (ctask->curl, CURLOPT_RESOLVE, ctask->resolver);
1001 sprintf (curlurl, "https://%s%s", ctask->leho, ctask->url);
1002 curl_easy_setopt (ctask->curl, CURLOPT_URL, curlurl);
1005 if (CURLM_OK != (mret=curl_multi_add_handle (curl_multi, ctask->curl)))
1007 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1008 "%s failed at %s:%d: `%s'\n",
1009 "curl_multi_add_handle", __FILE__, __LINE__,
1010 curl_multi_strerror (mret));
1011 ctask->download_successful = GNUNET_NO;
1012 ctask->download_error = GNUNET_YES;
1015 GNUNET_CONTAINER_DLL_insert (ctasks_head, ctasks_tail, ctask);
1017 curl_download_prepare ();
1022 * Initialize download and trigger curl
1024 * @param cls the proxycurltask
1025 * @param auth_name the name of the authority (site of origin) of ctask->host
1029 process_get_authority (void *cls,
1030 const char* auth_name)
1032 struct ProxyCurlTask *ctask = cls;
1034 if (NULL == auth_name)
1036 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1037 "Get authority failed!\n");
1038 strcpy (ctask->authority, "");
1041 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1042 "Get authority yielded %s\n", auth_name);
1043 strcpy (ctask->authority, auth_name);
1045 GNUNET_GNS_lookup_zone (gns_handle,
1048 GNUNET_GNS_RECORD_LEHO,
1049 &process_leho_lookup,
1054 * Main MHD callback for handling requests.
1057 * @param con MHD connection handle
1058 * @param meth the HTTP method used ("GET", "PUT", etc.)
1059 * @param ver the HTTP version string (i.e. "HTTP/1.1")
1060 * @param upload_data the data being uploaded (excluding HEADERS,
1061 * for a POST that fits into memory and that is encoded
1062 * with a supported encoding, the POST data will NOT be
1063 * given in upload_data and is instead available as
1064 * part of MHD_get_connection_values; very large POST
1065 * data *will* be made available incrementally in
1067 * @param upload_data_size set initially to the size of the
1068 * upload_data provided; the method must update this
1069 * value to the number of bytes NOT processed;
1070 * @param con_cls pointer to location where we store the 'struct Request'
1071 * @return MHD_YES if the connection was handled successfully,
1072 * MHD_NO if the socket must be closed due to a serious
1073 * error while handling the request
1076 create_response (void *cls,
1077 struct MHD_Connection *con,
1081 const char *upload_data,
1082 size_t *upload_data_size,
1086 struct MhdHttpList* hd = cls;
1087 const char* page = "<html><head><title>gnoxy</title>"\
1088 "</head><body>cURL fail</body></html>";
1089 struct MHD_Response *response;
1094 struct ProxyCurlTask *ctask;
1096 if (0 != strcmp (meth, "GET"))
1098 if (&dummy != *con_cls)
1104 if (0 != *upload_data_size)
1109 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1112 MHD_get_connection_values (con,
1114 &con_val_iter, host);
1118 ctask = GNUNET_malloc (sizeof (struct ProxyCurlTask));
1120 ctask->curl = curl_easy_init();
1122 if (curl_multi == NULL)
1123 curl_multi = curl_multi_init ();
1125 if ((ctask->curl == NULL) || (curl_multi == NULL))
1127 response = MHD_create_response_from_buffer (strlen (page),
1129 MHD_RESPMEM_PERSISTENT);
1130 ret = MHD_queue_response (con,
1133 MHD_destroy_response (response);
1134 GNUNET_free (ctask);
1138 ctask->download_in_progress = GNUNET_YES;
1139 ctask->download_successful = GNUNET_NO;
1140 ctask->buf_status = BUF_WAIT_FOR_CURL;
1141 ctask->bytes_in_buffer = 0;
1142 ctask->parse_content = GNUNET_NO;
1144 curl_easy_setopt (ctask->curl, CURLOPT_HEADERFUNCTION, &curl_check_hdr);
1145 curl_easy_setopt (ctask->curl, CURLOPT_HEADERDATA, ctask);
1146 curl_easy_setopt (ctask->curl, CURLOPT_WRITEFUNCTION, &callback_download);
1147 curl_easy_setopt (ctask->curl, CURLOPT_WRITEDATA, ctask);
1148 curl_easy_setopt (ctask->curl, CURLOPT_FOLLOWLOCATION, 1);
1149 curl_easy_setopt (ctask->curl, CURLOPT_MAXREDIRS, 4);
1150 /* no need to abort if the above failed */
1151 if (GNUNET_NO == ctask->mhd->is_ssl)
1152 sprintf (curlurl, "http://%s%s", host, url);
1153 strcpy (ctask->host, host);
1154 strcpy (ctask->url, url);
1155 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1156 "Adding new curl task for %s\n", curlurl);
1158 curl_easy_setopt (ctask->curl, CURLOPT_URL, curlurl);
1159 curl_easy_setopt (ctask->curl, CURLOPT_FAILONERROR, 1);
1160 curl_easy_setopt (ctask->curl, CURLOPT_CONNECTTIMEOUT, 600L);
1161 curl_easy_setopt (ctask->curl, CURLOPT_TIMEOUT, 600L);
1163 GNUNET_GNS_get_authority (gns_handle,
1165 &process_get_authority,
1167 //download_prepare (ctask);
1168 //curl_download_prepare ();
1170 response = MHD_create_response_from_callback (-1, -1,
1175 ret = MHD_queue_response (con, MHD_HTTP_OK, response);
1177 //MHD_destroy_response (response);
1183 * Task run whenever HTTP server operations are pending.
1186 * @param tc sched context
1189 do_httpd (void *cls,
1190 const struct GNUNET_SCHEDULER_TaskContext *tc);
1199 struct MhdHttpList *hd;
1201 for (hd=mhd_httpd_head; hd != NULL; hd = hd->next)
1210 run_httpd (struct MhdHttpList *hd)
1215 struct GNUNET_NETWORK_FDSet *wrs;
1216 struct GNUNET_NETWORK_FDSet *wws;
1217 struct GNUNET_NETWORK_FDSet *wes;
1220 unsigned MHD_LONG_LONG timeout;
1221 struct GNUNET_TIME_Relative tv;
1226 wrs = GNUNET_NETWORK_fdset_create ();
1227 wes = GNUNET_NETWORK_fdset_create ();
1228 wws = GNUNET_NETWORK_fdset_create ();
1230 GNUNET_assert (MHD_YES == MHD_get_fdset (hd->daemon, &rs, &ws, &es, &max));
1233 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1234 "MHD fds: max=%d\n", max);
1236 haveto = MHD_get_timeout (hd->daemon, &timeout);
1238 if (haveto == MHD_YES)
1239 tv.rel_value = (uint64_t) timeout;
1241 tv = GNUNET_TIME_UNIT_FOREVER_REL;
1242 GNUNET_NETWORK_fdset_copy_native (wrs, &rs, max + 1);
1243 GNUNET_NETWORK_fdset_copy_native (wws, &ws, max + 1);
1244 GNUNET_NETWORK_fdset_copy_native (wes, &es, max + 1);
1246 if (hd->httpd_task != GNUNET_SCHEDULER_NO_TASK)
1247 GNUNET_SCHEDULER_cancel (hd->httpd_task);
1249 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_HIGH,
1252 GNUNET_NETWORK_fdset_destroy (wrs);
1253 GNUNET_NETWORK_fdset_destroy (wws);
1254 GNUNET_NETWORK_fdset_destroy (wes);
1259 * Task run whenever HTTP server operations are pending.
1262 * @param tc sched context
1265 do_httpd (void *cls,
1266 const struct GNUNET_SCHEDULER_TaskContext *tc)
1268 struct MhdHttpList *hd = cls;
1270 hd->httpd_task = GNUNET_SCHEDULER_NO_TASK;
1272 MHD_run (hd->daemon);
1278 * Read data from socket
1280 * @param cls the closure
1281 * @param tc scheduler context
1284 do_read (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
1287 * Read from remote end
1289 * @param cls closure
1290 * @param tc scheduler context
1293 do_read_remote (void* cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
1296 * Write data to remote socket
1298 * @param cls the closure
1299 * @param tc scheduler context
1302 do_write_remote (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1304 struct Socks5Request *s5r = cls;
1307 s5r->fwdwtask = GNUNET_SCHEDULER_NO_TASK;
1309 if ((NULL != tc->read_ready) &&
1310 (GNUNET_NETWORK_fdset_isset (tc->write_ready, s5r->remote_sock)) &&
1311 ((len = GNUNET_NETWORK_socket_send (s5r->remote_sock, s5r->rbuf,
1314 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1315 "Successfully sent %d bytes to remote socket\n",
1320 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "write remote");
1322 if (s5r->rtask != GNUNET_SCHEDULER_NO_TASK)
1323 GNUNET_SCHEDULER_cancel (s5r->rtask);
1324 if (s5r->wtask != GNUNET_SCHEDULER_NO_TASK)
1325 GNUNET_SCHEDULER_cancel (s5r->wtask);
1326 if (s5r->fwdrtask != GNUNET_SCHEDULER_NO_TASK)
1327 GNUNET_SCHEDULER_cancel (s5r->fwdrtask);
1328 GNUNET_NETWORK_socket_close (s5r->remote_sock);
1329 GNUNET_NETWORK_socket_close (s5r->sock);
1335 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
1342 * Clean up s5r handles
1344 * @param s5r the handle to destroy
1347 cleanup_s5r (struct Socks5Request *s5r)
1349 if (s5r->rtask != GNUNET_SCHEDULER_NO_TASK)
1350 GNUNET_SCHEDULER_cancel (s5r->rtask);
1351 if (s5r->fwdwtask != GNUNET_SCHEDULER_NO_TASK)
1352 GNUNET_SCHEDULER_cancel (s5r->fwdwtask);
1353 if (s5r->fwdrtask != GNUNET_SCHEDULER_NO_TASK)
1354 GNUNET_SCHEDULER_cancel (s5r->fwdrtask);
1356 if (NULL != s5r->remote_sock)
1357 GNUNET_NETWORK_socket_close (s5r->remote_sock);
1358 if ((NULL != s5r->sock) && (s5r->cleanup_sock == GNUNET_YES))
1359 GNUNET_NETWORK_socket_close (s5r->sock);
1365 * Write data to socket
1367 * @param cls the closure
1368 * @param tc scheduler context
1371 do_write (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1373 struct Socks5Request *s5r = cls;
1376 s5r->wtask = GNUNET_SCHEDULER_NO_TASK;
1378 if ((NULL != tc->read_ready) &&
1379 (GNUNET_NETWORK_fdset_isset (tc->write_ready, s5r->sock)) &&
1380 ((len = GNUNET_NETWORK_socket_send (s5r->sock, s5r->wbuf,
1383 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1384 "Successfully sent %d bytes to socket\n",
1390 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "write");
1391 s5r->cleanup = GNUNET_YES;
1392 s5r->cleanup_sock = GNUNET_YES;
1398 if (GNUNET_YES == s5r->cleanup)
1404 if ((s5r->state == SOCKS5_DATA_TRANSFER) &&
1405 (s5r->fwdrtask == GNUNET_SCHEDULER_NO_TASK))
1407 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
1409 &do_read_remote, s5r);
1413 * Read from remote end
1415 * @param cls closure
1416 * @param tc scheduler context
1419 do_read_remote (void* cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1421 struct Socks5Request *s5r = cls;
1423 s5r->fwdrtask = GNUNET_SCHEDULER_NO_TASK;
1426 if ((NULL != tc->write_ready) &&
1427 (GNUNET_NETWORK_fdset_isset (tc->read_ready, s5r->remote_sock)) &&
1428 (s5r->wbuf_len = GNUNET_NETWORK_socket_recv (s5r->remote_sock, s5r->wbuf,
1429 sizeof (s5r->wbuf))))
1431 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1432 "Successfully read %d bytes from remote socket\n",
1437 if (s5r->wbuf_len == 0)
1438 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1439 "0 bytes received from remote... graceful shutdown!\n");
1440 if (s5r->fwdwtask != GNUNET_SCHEDULER_NO_TASK)
1441 GNUNET_SCHEDULER_cancel (s5r->fwdwtask);
1442 if (s5r->rtask != GNUNET_SCHEDULER_NO_TASK)
1443 GNUNET_SCHEDULER_cancel (s5r->rtask);
1445 GNUNET_NETWORK_socket_close (s5r->remote_sock);
1446 s5r->remote_sock = NULL;
1447 GNUNET_NETWORK_socket_close (s5r->sock);
1453 s5r->wtask = GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
1461 * Adds a socket to MHD
1463 * @param h the handle to the socket to add
1464 * @return whatever MHD_add_connection returns
1467 add_handle_to_mhd (struct GNUNET_NETWORK_Handle *h, struct MHD_Daemon *daemon)
1470 struct sockaddr *addr;
1473 fd = GNUNET_NETWORK_get_fd (h);
1474 addr = GNUNET_NETWORK_get_addr (h);
1475 len = GNUNET_NETWORK_get_addrlen (h);
1477 return MHD_add_connection (daemon, fd, addr, len);
1481 /*TODO this needs MHD API modification */
1482 static int http_port = 4444;
1486 get_file_size (const char* filename)
1490 fp = fopen (filename, "rb");
1495 if ((0 != fseek (fp, 0, SEEK_END)) || (-1 == (size = ftell (fp))))
1507 * Read file in filename
1509 * @param filename file to read
1513 load_file (const char* filename)
1519 size = get_file_size (filename);
1523 fp = fopen (filename, "rb");
1527 buffer = GNUNET_malloc (size);
1534 if (size != fread (buffer, 1, size, fp))
1536 GNUNET_free (buffer);
1547 * Load PEM key from file
1549 * @param key where to store the data
1550 * @param keyfile path to the PEM file
1553 load_key_from_file (gnutls_x509_privkey_t key, char* keyfile)
1555 gnutls_datum_t key_data;
1558 key_data.data = (unsigned char*)load_file (keyfile);
1559 key_data.size = strlen ((char*)key_data.data);
1561 ret = gnutls_x509_privkey_import (key, &key_data,
1562 GNUTLS_X509_FMT_PEM);
1564 if (GNUTLS_E_SUCCESS != ret)
1566 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1567 "Unable to import private key %s(ret=%d)\n", key_data.data, ret);
1573 * Load cert from file
1575 * @param crt struct to store data in
1576 * @param certfile path to pem file
1579 load_cert_from_file (gnutls_x509_crt_t crt, char* certfile)
1581 gnutls_datum_t cert_data;
1584 cert_data.data = (unsigned char*)load_file (certfile);
1585 cert_data.size = strlen ((char*)cert_data.data);
1587 ret = gnutls_x509_crt_import (crt, &cert_data,
1588 GNUTLS_X509_FMT_PEM);
1589 if (GNUTLS_E_SUCCESS != ret)
1591 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1592 "Unable to import certificate %s(ret=%d)\n", certfile, ret);
1600 * Generate new certificate for specific name
1602 * @param name the subject name to generate a cert for
1603 * @return a struct holding the PEM data
1605 static struct ProxyGNSCertificate *
1606 generate_gns_certificate (const char *name)
1610 unsigned int serial;
1612 size_t key_buf_size;
1613 size_t cert_buf_size;
1614 gnutls_x509_crt_t request;
1615 gnutls_x509_privkey_t rsa;
1619 ret = gnutls_x509_crt_init (&request);
1621 if (GNUTLS_E_SUCCESS != ret)
1626 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Generating key\n");
1627 gnutls_x509_privkey_init (&rsa);
1628 bits = gnutls_sec_param_to_pk_bits (GNUTLS_PK_RSA, GNUTLS_SEC_PARAM_NORMAL);
1629 ret = gnutls_x509_privkey_generate (rsa, GNUTLS_PK_RSA, bits, 0);
1631 if (GNUTLS_E_SUCCESS != ret)
1636 ret = gnutls_x509_crt_set_key (request, rsa);
1638 if (GNUTLS_E_SUCCESS != ret)
1643 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Generating cert\n");
1645 struct ProxyGNSCertificate *pgc =
1646 GNUNET_malloc (sizeof (struct ProxyGNSCertificate));
1648 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Adding DNs\n");
1650 gnutls_x509_crt_set_dn_by_oid (request, GNUTLS_OID_X520_COUNTRY_NAME,
1653 gnutls_x509_crt_set_dn_by_oid (request, GNUTLS_OID_X520_ORGANIZATION_NAME,
1656 gnutls_x509_crt_set_dn_by_oid (request, GNUTLS_OID_X520_COMMON_NAME,
1657 0, name, strlen (name));
1659 ret = gnutls_x509_crt_set_version (request, 3);
1661 ret = gnutls_rnd (GNUTLS_RND_NONCE, &serial, sizeof (serial));
1663 etime = time (NULL);
1664 tm_data = localtime (&etime);
1667 ret = gnutls_x509_crt_set_serial (request,
1671 ret = gnutls_x509_crt_set_activation_time (request,
1674 etime = mktime (tm_data);
1681 ret = gnutls_x509_crt_set_expiration_time (request,
1683 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Signing...\n");
1685 ret = gnutls_x509_crt_sign (request, proxy_ca.cert, proxy_ca.key);
1687 key_buf_size = sizeof (pgc->key);
1688 cert_buf_size = sizeof (pgc->cert);
1690 gnutls_x509_crt_export (request, GNUTLS_X509_FMT_PEM,
1691 pgc->cert, &cert_buf_size);
1693 gnutls_x509_privkey_export (rsa, GNUTLS_X509_FMT_PEM,
1694 pgc->key, &key_buf_size);
1697 gnutls_x509_crt_deinit (request);
1698 gnutls_x509_privkey_deinit (rsa);
1706 * Adds a socket to an SSL MHD instance
1707 * It is important the the domain name is
1708 * correct. In most cases we need to start a new daemon
1711 add_handle_to_ssl_mhd (struct GNUNET_NETWORK_Handle *h, char* domain)
1713 struct MhdHttpList *hd = NULL;
1714 struct ProxyGNSCertificate *pgc;
1716 for (hd = mhd_httpd_head; hd != NULL; hd = hd->next)
1718 if (0 == strcmp (hd->domain, domain))
1725 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1726 "No previous SSL instance found... starting new one for %s\n",
1729 pgc = generate_gns_certificate (domain);
1731 hd = GNUNET_malloc (sizeof (struct MhdHttpList));
1732 hd->is_ssl = GNUNET_YES;
1733 strcpy (hd->domain, domain);
1734 hd->daemon = MHD_start_daemon (MHD_USE_DEBUG | MHD_USE_SSL, http_port++,
1736 &create_response, hd,
1737 MHD_OPTION_CONNECTION_LIMIT, (unsigned int) 128,
1738 MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) 16,
1739 MHD_OPTION_NOTIFY_COMPLETED,
1741 MHD_OPTION_HTTPS_MEM_KEY, pgc->key,
1742 MHD_OPTION_HTTPS_MEM_CERT, pgc->cert,
1744 hd->httpd_task = GNUNET_SCHEDULER_NO_TASK;
1746 GNUNET_CONTAINER_DLL_insert (mhd_httpd_head, mhd_httpd_tail, hd);
1749 return add_handle_to_mhd (h, hd->daemon);
1755 * Read data from incoming connection
1757 * @param cls the closure
1758 * @param tc the scheduler context
1761 do_read (void* cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1763 struct Socks5Request *s5r = cls;
1764 struct socks5_client_hello *c_hello;
1765 struct socks5_server_hello *s_hello;
1766 struct socks5_client_request *c_req;
1767 struct socks5_server_response *s_resp;
1773 struct hostent *phost;
1775 struct sockaddr_in remote_addr;
1776 struct in_addr *r_sin_addr;
1778 s5r->rtask = GNUNET_SCHEDULER_NO_TASK;
1780 if ((NULL != tc->write_ready) &&
1781 (GNUNET_NETWORK_fdset_isset (tc->read_ready, s5r->sock)) &&
1782 (s5r->rbuf_len = GNUNET_NETWORK_socket_recv (s5r->sock, s5r->rbuf,
1783 sizeof (s5r->rbuf))))
1785 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1786 "Successfully read %d bytes from socket\n",
1791 if (s5r->rbuf_len != 0)
1792 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "read");
1794 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client disco!\n");
1796 if (s5r->fwdrtask != GNUNET_SCHEDULER_NO_TASK)
1797 GNUNET_SCHEDULER_cancel (s5r->fwdrtask);
1798 if (s5r->wtask != GNUNET_SCHEDULER_NO_TASK)
1799 GNUNET_SCHEDULER_cancel (s5r->wtask);
1800 if (s5r->fwdwtask != GNUNET_SCHEDULER_NO_TASK)
1801 GNUNET_SCHEDULER_cancel (s5r->fwdwtask);
1802 GNUNET_NETWORK_socket_close (s5r->remote_sock);
1803 GNUNET_NETWORK_socket_close (s5r->sock);
1808 if (s5r->state == SOCKS5_INIT)
1810 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1812 c_hello = (struct socks5_client_hello*)&s5r->rbuf;
1814 GNUNET_assert (c_hello->version == SOCKS_VERSION_5);
1816 s_hello = (struct socks5_server_hello*)&s5r->wbuf;
1817 s5r->wbuf_len = sizeof( struct socks5_server_hello );
1819 s_hello->version = c_hello->version;
1820 s_hello->auth_method = SOCKS_AUTH_NONE;
1822 /* Write response to client */
1823 s5r->wtask = GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
1827 s5r->rtask = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
1831 s5r->state = SOCKS5_REQUEST;
1835 if (s5r->state == SOCKS5_REQUEST)
1837 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1838 "Processing SOCKS5 request\n");
1839 c_req = (struct socks5_client_request*)&s5r->rbuf;
1840 s_resp = (struct socks5_server_response*)&s5r->wbuf;
1841 //Only 10byte for ipv4 response!
1842 s5r->wbuf_len = 10;//sizeof (struct socks5_server_response);
1844 GNUNET_assert (c_req->addr_type == 3);
1846 dom_len = *((uint8_t*)(&(c_req->addr_type) + 1));
1847 memset(domain, 0, sizeof(domain));
1848 strncpy(domain, (char*)(&(c_req->addr_type) + 2), dom_len);
1849 req_port = *((uint16_t*)(&(c_req->addr_type) + 2 + dom_len));
1851 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1852 "Requested connection is %s:%d\n",
1856 if (is_tld (domain, GNUNET_GNS_TLD) ||
1857 is_tld (domain, GNUNET_GNS_TLD_ZKEY))
1859 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1860 "Requested connection is gnunet tld\n",
1864 if (ntohs(req_port) == HTTPS_PORT)
1866 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1867 "Requested connection is HTTPS\n");
1868 ret = add_handle_to_ssl_mhd ( s5r->sock, domain );
1870 else if (NULL != httpd)
1872 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1873 "Requested connection is HTTP\n");
1874 ret = add_handle_to_mhd ( s5r->sock, httpd );
1879 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1880 _("Failed to start HTTP server\n"));
1881 s_resp->version = 0x05;
1882 s_resp->reply = 0x01;
1883 s5r->cleanup = GNUNET_YES;
1884 s5r->cleanup_sock = GNUNET_YES;
1886 GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
1892 /* Signal success */
1893 s_resp->version = 0x05;
1894 s_resp->reply = 0x00;
1895 s_resp->reserved = 0x00;
1896 s_resp->addr_type = 0x01;
1898 s5r->cleanup = GNUNET_YES;
1899 s5r->cleanup_sock = GNUNET_NO;
1901 GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
1909 phost = (struct hostent*)gethostbyname (domain);
1912 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1913 "Resolve %s error!\n", domain );
1914 s_resp->version = 0x05;
1915 s_resp->reply = 0x01;
1916 s5r->cleanup = GNUNET_YES;
1917 s5r->cleanup_sock = GNUNET_YES;
1919 GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
1925 s5r->remote_sock = GNUNET_NETWORK_socket_create (AF_INET,
1928 r_sin_addr = (struct in_addr*)(phost->h_addr);
1929 remote_ip = r_sin_addr->s_addr;
1930 memset(&remote_addr, 0, sizeof(remote_addr));
1931 remote_addr.sin_family = AF_INET;
1932 #if HAVE_SOCKADDR_IN_SIN_LEN
1933 remote_addr.sin_len = sizeof (remote_addr);
1935 remote_addr.sin_addr.s_addr = remote_ip;
1936 remote_addr.sin_port = req_port;
1938 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1939 "target server: %s:%u\n", inet_ntoa(remote_addr.sin_addr),
1943 GNUNET_NETWORK_socket_connect ( s5r->remote_sock,
1944 (const struct sockaddr*)&remote_addr,
1945 sizeof (remote_addr)))
1946 && (errno != EINPROGRESS))
1948 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "connect");
1949 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1950 "socket request error...\n");
1951 s_resp->version = 0x05;
1952 s_resp->reply = 0x01;
1954 GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
1961 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1962 "new remote connection\n");
1964 s_resp->version = 0x05;
1965 s_resp->reply = 0x00;
1966 s_resp->reserved = 0x00;
1967 s_resp->addr_type = 0x01;
1969 s5r->state = SOCKS5_DATA_TRANSFER;
1972 GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
1976 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
1984 if (s5r->state == SOCKS5_DATA_TRANSFER)
1986 if ((s5r->remote_sock == NULL) || (s5r->rbuf_len == 0))
1988 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1989 "Closing connection to client\n");
1990 if (s5r->rtask != GNUNET_SCHEDULER_NO_TASK)
1991 GNUNET_SCHEDULER_cancel (s5r->rtask);
1992 if (s5r->fwdwtask != GNUNET_SCHEDULER_NO_TASK)
1993 GNUNET_SCHEDULER_cancel (s5r->fwdwtask);
1994 if (s5r->fwdrtask != GNUNET_SCHEDULER_NO_TASK)
1995 GNUNET_SCHEDULER_cancel (s5r->fwdrtask);
1996 if (s5r->fwdrtask != GNUNET_SCHEDULER_NO_TASK)
1997 GNUNET_SCHEDULER_cancel (s5r->fwdrtask);
1999 if (s5r->remote_sock != NULL)
2000 GNUNET_NETWORK_socket_close (s5r->remote_sock);
2001 GNUNET_NETWORK_socket_close (s5r->sock);
2006 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2007 "forwarding %d bytes from client\n", s5r->rbuf_len);
2010 GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
2012 &do_write_remote, s5r);
2014 if (s5r->fwdrtask == GNUNET_SCHEDULER_NO_TASK)
2017 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
2019 &do_read_remote, s5r);
2025 //GNUNET_CONTAINER_DLL_remove (s5conns.head, s5conns.tail, s5r);
2031 * Accept new incoming connections
2033 * @param cls the closure
2034 * @param tc the scheduler context
2037 do_accept (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2039 struct GNUNET_NETWORK_Handle *s;
2040 struct Socks5Request *s5r;
2042 ltask = GNUNET_SCHEDULER_NO_TASK;
2043 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
2046 ltask = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
2050 s = GNUNET_NETWORK_socket_accept (lsock, NULL, NULL);
2054 GNUNET_log_strerror (GNUNET_ERROR_TYPE_INFO, "accept");
2058 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2059 "Got an inbound connection, waiting for data\n");
2061 s5r = GNUNET_malloc (sizeof (struct Socks5Request));
2063 s5r->state = SOCKS5_INIT;
2064 s5r->wtask = GNUNET_SCHEDULER_NO_TASK;
2065 s5r->fwdwtask = GNUNET_SCHEDULER_NO_TASK;
2066 s5r->fwdrtask = GNUNET_SCHEDULER_NO_TASK;
2067 s5r->rtask = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
2070 //GNUNET_CONTAINER_DLL_insert (s5conns.head, s5conns.tail, s5r);
2075 * Task run on shutdown
2077 * @param cls closure
2078 * @param tc task context
2081 do_shutdown (void *cls,
2082 const struct GNUNET_SCHEDULER_TaskContext *tc)
2085 struct MhdHttpList *hd;
2086 struct MhdHttpList *tmp_hd;
2087 struct ProxyCurlTask *ctask;
2088 struct ProxyCurlTask *ctask_tmp;
2090 if (GNUNET_SCHEDULER_NO_TASK != curl_download_task)
2092 GNUNET_SCHEDULER_cancel (curl_download_task);
2093 curl_download_task = GNUNET_SCHEDULER_NO_TASK;
2096 for (hd = mhd_httpd_head; hd != NULL; hd = tmp_hd)
2100 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2101 "Stopping daemon\n");
2103 if (GNUNET_SCHEDULER_NO_TASK != hd->httpd_task)
2105 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2106 "Stopping select task %d\n",
2108 GNUNET_SCHEDULER_cancel (hd->httpd_task);
2109 hd->httpd_task = GNUNET_SCHEDULER_NO_TASK;
2112 if (NULL != hd->daemon)
2114 MHD_stop_daemon (hd->daemon);
2121 for (ctask=ctasks_head; ctask != NULL; ctask=ctask_tmp)
2123 ctask_tmp = ctask->next;
2125 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2126 "Cleaning up cURL task\n");
2128 if (ctask->curl != NULL)
2129 curl_easy_cleanup (ctask->curl);
2131 if (NULL != ctask->headers)
2132 curl_slist_free_all (ctask->headers);
2134 GNUNET_free (ctask);
2137 GNUNET_GNS_disconnect (gns_handle);
2142 * Compiles a regex for us
2144 * @param re ptr to re struct
2145 * @param rt the expression to compile
2146 * @return 0 on success
2149 compile_regex (regex_t *re, const char* rt)
2154 status = regcomp (re, rt, REG_EXTENDED|REG_NEWLINE);
2157 regerror (status, re, err, 1024);
2158 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2159 "Regex error compiling '%s': %s\n", rt, err);
2167 * Loads the users local zone key
2169 * @return GNUNET_YES on success
2172 load_local_zone_key (const struct GNUNET_CONFIGURATION_Handle *cfg)
2175 struct GNUNET_CRYPTO_RsaPrivateKey *key = NULL;
2176 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pkey;
2177 struct GNUNET_CRYPTO_ShortHashCode *zone = NULL;
2178 struct GNUNET_CRYPTO_ShortHashAsciiEncoded zonename;
2180 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "gns",
2181 "ZONEKEY", &keyfile))
2183 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2184 "Unable to load zone key config value!\n");
2188 if (GNUNET_NO == GNUNET_DISK_file_test (keyfile))
2190 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2191 "Unable to load zone key!\n");
2192 GNUNET_free(keyfile);
2196 key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
2197 GNUNET_CRYPTO_rsa_key_get_public (key, &pkey);
2198 GNUNET_CRYPTO_short_hash(&pkey,
2199 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
2201 zone = &local_gns_zone;
2202 GNUNET_CRYPTO_short_hash_to_enc (zone, &zonename);
2203 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2204 "Using zone: %s!\n", &zonename);
2205 GNUNET_CRYPTO_rsa_key_free(key);
2206 GNUNET_free(keyfile);
2214 * Main function that will be run
2216 * @param cls closure
2217 * @param args remaining command-line arguments
2218 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
2219 * @param cfg configuration
2222 run (void *cls, char *const *args, const char *cfgfile,
2223 const struct GNUNET_CONFIGURATION_Handle *cfg)
2225 struct sockaddr_in sa;
2226 struct MhdHttpList *hd;
2228 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2231 gnutls_global_init ();
2233 gnutls_x509_crt_init (&proxy_ca.cert);
2234 gnutls_x509_privkey_init (&proxy_ca.key);
2236 load_cert_from_file (proxy_ca.cert, cafile);
2237 load_key_from_file (proxy_ca.key, cafile);
2239 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2240 "Loading Template\n");
2242 compile_regex (&re_dotplus, (char*) RE_A_HREF);
2244 gns_handle = GNUNET_GNS_connect (cfg);
2246 if (GNUNET_NO == load_local_zone_key (cfg))
2248 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2249 "Unable to load zone!\n");
2253 if (NULL == gns_handle)
2255 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2256 "Unable to connect to GNS!\n");
2260 memset (&sa, 0, sizeof (sa));
2261 sa.sin_family = AF_INET;
2262 sa.sin_port = htons (port);
2263 #if HAVE_SOCKADDR_IN_SIN_LEN
2264 sa.sin_len = sizeof (sa);
2267 lsock = GNUNET_NETWORK_socket_create (AF_INET,
2271 if ((NULL == lsock) ||
2273 GNUNET_NETWORK_socket_bind (lsock, (const struct sockaddr *) &sa,
2276 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2277 "Failed to create listen socket bound to `%s'",
2278 GNUNET_a2s ((const struct sockaddr *) &sa, sizeof (sa)));
2280 GNUNET_NETWORK_socket_close (lsock);
2284 if (GNUNET_OK != GNUNET_NETWORK_socket_listen (lsock, 5))
2286 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2287 "Failed to listen on socket bound to `%s'",
2288 GNUNET_a2s ((const struct sockaddr *) &sa, sizeof (sa)));
2292 ltask = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
2293 lsock, &do_accept, NULL);
2298 if (0 != curl_global_init (CURL_GLOBAL_WIN32))
2300 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2301 "cURL global init failed!\n");
2305 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2306 "Proxy listens on port %u\n",
2309 mhd_httpd_head = NULL;
2310 mhd_httpd_tail = NULL;
2312 hd = GNUNET_malloc (sizeof (struct MhdHttpList));
2313 hd->is_ssl = GNUNET_NO;
2314 strcpy (hd->domain, "");
2315 httpd = MHD_start_daemon (MHD_USE_DEBUG, http_port++,
2317 &create_response, hd,
2318 MHD_OPTION_CONNECTION_LIMIT, (unsigned int) 128,
2319 MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) 16,
2320 MHD_OPTION_NOTIFY_COMPLETED,
2324 hd->httpd_task = GNUNET_SCHEDULER_NO_TASK;
2326 GNUNET_CONTAINER_DLL_insert (mhd_httpd_head, mhd_httpd_tail, hd);
2330 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
2331 &do_shutdown, NULL);
2337 * The main function for gnunet-gns-proxy.
2339 * @param argc number of arguments from the command line
2340 * @param argv command line arguments
2341 * @return 0 ok, 1 on error
2344 main (int argc, char *const *argv)
2346 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
2348 gettext_noop ("listen on specified port"), 1,
2349 &GNUNET_GETOPT_set_string, &port},
2350 {'a', "authority", NULL,
2351 gettext_noop ("pem file to use as CA"), 1,
2352 &GNUNET_GETOPT_set_string, &cafile},
2353 GNUNET_GETOPT_OPTION_END
2358 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
2361 GNUNET_log_setup ("gnunet-gns-proxy", "WARNING", NULL);
2364 GNUNET_PROGRAM_run (argc, argv, "gnunet-gns-proxy",
2365 _("GNUnet GNS proxy"),
2367 &run, NULL)) ? 0 : 1;