2 This file is part of GNUnet.
3 (C) 2012 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 #include <gnunet_util_lib.h>
23 #include <gnunet_gns_service.h>
24 #include <microhttpd.h>
25 #include <curl/curl.h>
27 #include "gns_proxy_proto.h"
31 #include <gnutls/gnutls.h>
32 #include <gnutls/x509.h>
33 #include <gnutls/abstract.h>
34 #include <gnutls/crypto.h>
37 #define GNUNET_GNS_PROXY_PORT 7777
38 #define MAX_MHD_CONNECTIONS 300
40 /* MHD/cURL defines */
41 #define BUF_WAIT_FOR_CURL 0
42 #define BUF_WAIT_FOR_MHD 1
43 #define HTML_HDR_CONTENT "Content-Type: text/html\r\n"
46 //#define RE_DOTPLUS "<a href=\"http://(([A-Za-z]+[.])+)([+])"
47 #define RE_A_HREF "<a href=\"https?://(([A-Za-z0-9]+[.])+)([+]|zkey)"
48 #define RE_N_MATCHES 4
50 /* The usual suspects */
52 #define HTTPS_PORT 443
56 * A structure for CA cert/key
61 gnutls_x509_crt_t cert;
64 gnutls_x509_privkey_t key;
69 * Structure for GNS certificates
71 struct ProxyGNSCertificate
73 /* The certificate as PEM */
76 /* The private key as PEM */
82 * A structure for socks requests
86 /* The client socket */
87 struct GNUNET_NETWORK_Handle *sock;
89 /* The server socket */
90 struct GNUNET_NETWORK_Handle *remote_sock;
95 /* Client socket read task */
96 GNUNET_SCHEDULER_TaskIdentifier rtask;
98 /* Server socket read task */
99 GNUNET_SCHEDULER_TaskIdentifier fwdrtask;
101 /* Client socket write task */
102 GNUNET_SCHEDULER_TaskIdentifier wtask;
104 /* Server socket write task */
105 GNUNET_SCHEDULER_TaskIdentifier fwdwtask;
113 /* Length of data in read buffer */
114 unsigned int rbuf_len;
116 /* Length of data in write buffer */
117 unsigned int wbuf_len;
119 /* This handle is scheduled for cleanup? */
122 /* Shall we close the client socket on cleanup? */
128 * A structure for all running Httpds
133 struct MhdHttpList *prev;
136 struct MhdHttpList *next;
138 /* is this an ssl daemon? */
141 /* the domain name to server (only important for SSL) */
144 /* The daemon handle */
145 struct MHD_Daemon *daemon;
147 /* Optional proxy certificate used */
148 struct ProxyGNSCertificate *proxy_cert;
151 GNUNET_SCHEDULER_TaskIdentifier httpd_task;
155 * A structure for MHD<->cURL streams
160 struct ProxyCurlTask *prev;
163 struct ProxyCurlTask *next;
168 /* Optional header replacements for curl (LEHO) */
169 struct curl_slist *headers;
171 /* Optional resolver replacements for curl (LEHO) */
172 struct curl_slist *resolver;
174 /* The URL to fetch */
177 /* The cURL write buffer / MHD read buffer */
178 char buffer[CURL_MAX_WRITE_SIZE];
180 /* The pointer to the data in the buffer */
183 /* The buffer status (BUF_WAIT_FOR_CURL or BUF_WAIT_FOR_MHD) */
186 /* Number of bytes in buffer */
187 unsigned int bytes_in_buffer;
189 /* Indicates wheather the download is in progress */
190 int download_in_progress;
192 /* Indicates wheather the download was successful */
193 int download_successful;
195 /* Indicates wheather the download failed */
198 /* Indicates wheather we need to parse HTML */
201 /* Indicates wheather we are postprocessing the HTML right now */
202 int is_postprocessing;
204 /* Indicates wheather postprocessing has finished */
207 /* Task ID of the postprocessing task */
208 GNUNET_SCHEDULER_TaskIdentifier pp_task;
210 /* The postprocessing buffer TODO length? */
213 /* The authority of the corresponding host (site of origin) */
216 /* The hostname (Host header field) */
219 /* The LEgacy HOstname (can be empty) */
222 /* The associated daemon list entry */
223 struct MhdHttpList *mhd;
227 /* The port the proxy is running on (default 7777) */
228 static unsigned long port = GNUNET_GNS_PROXY_PORT;
230 /* The CA file (pem) to use for the proxy CA */
231 static char* cafile_opt;
233 /* The listen socket of the proxy */
234 static struct GNUNET_NETWORK_Handle *lsock;
236 /* The listen task ID */
237 GNUNET_SCHEDULER_TaskIdentifier ltask;
239 /* The cURL download task */
240 GNUNET_SCHEDULER_TaskIdentifier curl_download_task;
242 /* The non SSL httpd daemon handle */
243 static struct MHD_Daemon *httpd;
245 /* Number of current mhd connections */
246 static unsigned int total_mhd_connections;
248 /* The cURL multi handle */
249 static CURLM *curl_multi;
251 /* Handle to the GNS service */
252 static struct GNUNET_GNS_Handle *gns_handle;
254 /* DLL for ProxyCurlTasks */
255 static struct ProxyCurlTask *ctasks_head;
257 /* DLL for ProxyCurlTasks */
258 static struct ProxyCurlTask *ctasks_tail;
260 /* DLL for http daemons */
261 static struct MhdHttpList *mhd_httpd_head;
263 /* DLL for http daemons */
264 static struct MhdHttpList *mhd_httpd_tail;
266 /* Handle to the regex for dotplus (.+) replacement in HTML */
267 static regex_t re_dotplus;
269 /* The users local GNS zone hash */
270 static struct GNUNET_CRYPTO_ShortHashCode local_gns_zone;
272 /* The users local shorten zone hash */
273 static struct GNUNET_CRYPTO_ShortHashCode local_shorten_zone;
275 /* The CA for SSL certificate generation */
276 static struct ProxyCA proxy_ca;
278 /* UNIX domain socket for mhd */
279 struct GNUNET_NETWORK_Handle *mhd_unix_socket;
285 * Checks if name is in tld
287 * @param name the name to check
288 * @param tld the TLD to check for
289 * @return GNUNET_YES or GNUNET_NO
292 is_tld(const char* name, const char* tld)
296 if (strlen(name) <= strlen(tld))
301 offset = strlen(name)-strlen(tld);
302 if (strcmp (name+offset, tld) != 0)
304 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
305 "%s is not in .%s TLD\n", name, tld);
314 * Read HTTP request header field 'Host'
316 * @param cls buffer to write to
317 * @param kind value kind
318 * @param key field key
319 * @param value field value
320 * @return MHD_NO when Host found
323 con_val_iter (void *cls,
324 enum MHD_ValueKind kind,
328 char* buf = (char*)cls;
330 if (0 == strcmp ("Host", key))
340 * Check HTTP response header for mime
342 * @param buffer curl buffer
343 * @param size curl blocksize
344 * @param nmemb curl blocknumber
346 * @return size of read bytes
349 curl_check_hdr (void *buffer, size_t size, size_t nmemb, void *cls)
351 size_t bytes = size * nmemb;
352 struct ProxyCurlTask *ctask = cls;
355 memcpy (hdr, buffer, bytes);
358 if (0 == strcmp (hdr, HTML_HDR_CONTENT))
360 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
361 "Got HTML HTTP response header\n");
362 ctask->parse_content = GNUNET_YES;
371 * @param hd a http daemon list entry
374 run_httpd (struct MhdHttpList *hd);
386 * Task that simply runs MHD main loop
389 * @param tc task context
392 run_mhd (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
395 struct MhdHttpList *hd = cls;
397 //for (hd=mhd_httpd_head; hd != NULL; hd = hd->next)
398 MHD_run (hd->daemon);
403 * Process cURL download bits
405 * @param ptr buffer with data
406 * @param size size of a record
407 * @param nmemb number of records downloaded
409 * @return number of processed bytes
412 callback_download (void *ptr, size_t size, size_t nmemb, void *ctx)
414 const char *cbuf = ptr;
416 struct ProxyCurlTask *ctask = ctx;
427 if (total > sizeof (ctask->buffer))
429 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
430 "CURL gave us too much data to handle (%d)!\n",
435 if (ctask->buf_status == BUF_WAIT_FOR_MHD)
437 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
438 "CURL: Waiting for MHD (%s)\n", ctask->url);
439 return CURL_WRITEFUNC_PAUSE;
443 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
444 "CURL: Copying to MHD (%s, %d)\n", ctask->url, total);
445 memcpy (ctask->buffer, cbuf, total);
446 ctask->bytes_in_buffer = total;
447 ctask->buffer_ptr = ctask->buffer;
449 ctask->buf_status = BUF_WAIT_FOR_MHD;
451 //GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
452 // "cURL chunk:\n%s\n", (char*)ctask->buffer);
453 //run_mhd (NULL, NULL);
454 GNUNET_SCHEDULER_add_now (&run_mhd, ctask->mhd);
461 * Callback to free content
463 * @param cls content to free
466 mhd_content_free (void *cls)
468 struct ProxyCurlTask *ctask = cls;
470 if (NULL != ctask->headers)
471 curl_slist_free_all (ctask->headers);
473 if (NULL != ctask->curl)
474 curl_easy_cleanup (ctask->curl);
484 * Shorten result callback
486 * @param cls the proxycurltask
487 * @param short_name the shortened name (NULL on error)
490 process_shorten (void* cls, const char* short_name)
492 struct ProxyCurlTask *ctask = cls;
494 char tmp[strlen(ctask->pp_buf)]; //TODO length
496 if (NULL == short_name)
498 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
499 "MHD PP: Unable to shorten %s\n",
504 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
505 "MHD PP: Shorten %s -> %s\n",
509 sprintf (tmp, "<a href=\"http://%s", short_name);
510 strcpy (ctask->pp_buf, tmp);
512 ctask->pp_finished = GNUNET_YES;
514 GNUNET_SCHEDULER_add_now (&run_mhd, ctask->mhd);
519 * Postprocessing task that uses GNS to shorten names
521 * @param cls the proxycurltask
522 * @param tc the task context
525 postprocess_name (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
527 struct ProxyCurlTask *ctask = cls;
528 char tmp[strlen(ctask->pp_buf)];
530 sprintf ( tmp, "%s%s", ctask->pp_buf, ctask->authority);
532 GNUNET_GNS_shorten (gns_handle,
541 * Callback for MHD response
544 * @param pos in buffer
546 * @param max space in buffer
549 mhd_content_cb (void *cls,
554 struct ProxyCurlTask *ctask = cls;
556 size_t bytes_to_copy;
559 regmatch_t m[RE_N_MATCHES];
561 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
562 "MHD: content cb\n");
564 if (ctask->download_successful &&
565 (ctask->buf_status == BUF_WAIT_FOR_CURL))
567 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
568 "MHD: sending response for %s\n", ctask->url);
569 ctask->download_in_progress = GNUNET_NO;
570 curl_multi_remove_handle (curl_multi, ctask->curl);
571 curl_easy_cleanup (ctask->curl);
572 GNUNET_SCHEDULER_add_now (&run_mhd, ctask->mhd);
573 total_mhd_connections--;
574 return MHD_CONTENT_READER_END_OF_STREAM;
577 if (ctask->download_error &&
578 (ctask->buf_status == BUF_WAIT_FOR_CURL))
580 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
581 "MHD: sending error response\n");
582 ctask->download_in_progress = GNUNET_NO;
583 curl_multi_remove_handle (curl_multi, ctask->curl);
584 curl_easy_cleanup (ctask->curl);
585 GNUNET_SCHEDULER_add_now (&run_mhd, ctask->mhd);
586 total_mhd_connections--;
587 return MHD_CONTENT_READER_END_WITH_ERROR;
590 if ( ctask->buf_status == BUF_WAIT_FOR_CURL )
593 bytes_to_copy = ctask->bytes_in_buffer;
595 if (ctask->parse_content == GNUNET_YES)
598 GNUNET_log ( GNUNET_ERROR_TYPE_DEBUG,
599 "MHD: We need to parse the HTML %s\n", ctask->buffer_ptr);
601 nomatch = regexec ( &re_dotplus, ctask->buffer_ptr, RE_N_MATCHES, m, 0);
605 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
606 "MHD RE: No match\n");
610 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
613 GNUNET_assert (m[1].rm_so != -1);
615 hostptr = ctask->buffer_ptr+m[1].rm_so;
619 bytes_to_copy = m[0].rm_so;
620 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
621 "Copying %d bytes.\n", m[0].rm_so);
627 if (ctask->is_postprocessing == GNUNET_YES)
631 if ( ctask->pp_finished == GNUNET_NO )
633 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
634 "MHD PP: Waiting for PP of %s\n", ctask->pp_buf);
638 ctask->is_postprocessing = GNUNET_NO;
640 ctask->bytes_in_buffer -= m[0].rm_eo;//(m[1].rm_eo-m[1].rm_so);
641 ctask->buffer_ptr += m[0].rm_eo;//(m[1].rm_eo-m[1].rm_so);
642 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
643 "Skipping next %d bytes in buffer\n", m[0].rm_eo);
645 GNUNET_SCHEDULER_add_now (&run_mhd, ctask->mhd);
647 if ( strlen (ctask->pp_buf) <= max )
649 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
650 "Copying postprocessed %s.\n", ctask->pp_buf);
651 memcpy ( buf, ctask->pp_buf, strlen (ctask->pp_buf) );
652 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
654 ctask->is_postprocessing = GNUNET_NO;
655 return strlen (ctask->pp_buf);
661 memset (ctask->pp_buf, 0, sizeof(ctask->pp_buf));
663 /* If .+ extend with authority */
664 if (*(ctask->buffer_ptr+m[1].rm_eo) == '+')
666 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
668 memcpy (ctask->pp_buf, hostptr, (m[1].rm_eo-m[1].rm_so));
669 strcpy ( ctask->pp_buf+strlen(ctask->pp_buf),
672 /* If .zkey simply copy the name */
675 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
677 memcpy (ctask->pp_buf, hostptr, (m[1].rm_eo-m[1].rm_so + strlen (GNUNET_GNS_TLD_ZKEY)));
680 ctask->is_postprocessing = GNUNET_YES;
681 ctask->pp_finished = GNUNET_NO;
683 GNUNET_GNS_shorten (gns_handle,
688 //postprocess_name(ctask, NULL);
689 //ctask->pp_task = GNUNET_SCHEDULER_add_now (&postprocess_name, ctask);
696 if ( bytes_to_copy > max )
698 GNUNET_log ( GNUNET_ERROR_TYPE_DEBUG,
699 "MHD: buffer in response too small! (%s)\n",
701 memcpy ( buf, ctask->buffer_ptr, max);
702 ctask->bytes_in_buffer -= max;
703 ctask->buffer_ptr += max;
708 GNUNET_log ( GNUNET_ERROR_TYPE_DEBUG,
709 "MHD: copying %d bytes to mhd response at offset %d\n",
712 memcpy ( buf, ctask->buffer_ptr, bytes_to_copy );
713 copied = bytes_to_copy;
714 if (bytes_to_copy < ctask->bytes_in_buffer)
716 ctask->bytes_in_buffer -= bytes_to_copy;
717 ctask->buffer_ptr += bytes_to_copy;
721 ctask->bytes_in_buffer = 0;
722 ctask->buf_status = BUF_WAIT_FOR_CURL;
723 ctask->buffer_ptr = ctask->buffer;
724 curl_easy_pause (ctask->curl, CURLPAUSE_CONT);
725 GNUNET_SCHEDULER_add_now (&run_mhd, ctask->mhd);
729 GNUNET_SCHEDULER_add_now (&run_mhd, ctask->mhd);
737 * Task that is run when we are ready to receive more data
741 * @param tc task context
744 curl_task_download (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
747 * Ask cURL for the select sets and schedule download
750 curl_download_prepare ()
757 struct GNUNET_NETWORK_FDSet *grs;
758 struct GNUNET_NETWORK_FDSet *gws;
760 struct GNUNET_TIME_Relative rtime;
766 mret = curl_multi_fdset (curl_multi, &rs, &ws, &es, &max);
768 if (mret != CURLM_OK)
770 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
771 "%s failed at %s:%d: `%s'\n",
772 "curl_multi_fdset", __FILE__, __LINE__,
773 curl_multi_strerror (mret));
778 mret = curl_multi_timeout (curl_multi, &to);
779 rtime = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, to);
781 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
782 "cURL multi fds: max=%d timeout=%llu\n", max, to);
784 grs = GNUNET_NETWORK_fdset_create ();
785 gws = GNUNET_NETWORK_fdset_create ();
786 GNUNET_NETWORK_fdset_copy_native (grs, &rs, max + 1);
787 GNUNET_NETWORK_fdset_copy_native (gws, &ws, max + 1);
788 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
789 "Scheduling task cURL\n");
791 if (curl_download_task != GNUNET_SCHEDULER_NO_TASK)
792 GNUNET_SCHEDULER_cancel (curl_download_task);
795 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
798 &curl_task_download, curl_multi);
799 GNUNET_NETWORK_fdset_destroy (gws);
800 GNUNET_NETWORK_fdset_destroy (grs);
806 * Task that is run when we are ready to receive more data
810 * @param tc task context
813 curl_task_download (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
819 struct ProxyCurlTask *ctask;
822 curl_download_task = GNUNET_SCHEDULER_NO_TASK;
824 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
826 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
827 "Shutdown requested while trying to download\n");
831 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
839 mret = curl_multi_perform (curl_multi, &running);
841 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
842 "Running curl tasks: %d\n", running);
845 for (; ctask != NULL; ctask = ctask->next)
847 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
848 "CTask: %s\n", ctask->url);
852 if (num_ctasks != running)
854 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
855 "%d ctasks, %d curl running\n", num_ctasks, running);
861 msg = curl_multi_info_read (curl_multi, &msgnum);
862 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
863 "Messages left: %d\n", msgnum);
870 if ((msg->data.result != CURLE_OK) &&
871 (msg->data.result != CURLE_GOT_NOTHING))
873 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
874 "Download curl failed");
876 for (; ctask != NULL; ctask = ctask->next)
878 if (memcmp (msg->easy_handle, ctask->curl, sizeof (CURL)) != 0)
881 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
882 "Download curl failed for task %s: %s.\n",
884 curl_easy_strerror (msg->data.result));
885 ctask->download_successful = GNUNET_NO;
886 ctask->download_error = GNUNET_YES;
887 //curl_multi_remove_handle (curl_multi, ctask->curl);
888 //curl_easy_cleanup (ctask->curl);
889 GNUNET_CONTAINER_DLL_remove (ctasks_head, ctasks_tail,
893 GNUNET_assert (ctask != NULL);
897 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
898 "cURL download completed.\n");
900 for (; ctask != NULL; ctask = ctask->next)
902 if (memcmp (msg->easy_handle, ctask->curl, sizeof (CURL)) != 0)
905 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
906 "cURL task %s found.\n", ctask->url);
907 ctask->download_successful = GNUNET_YES;
908 //curl_multi_remove_handle (curl_multi, ctask->curl);
909 //curl_easy_cleanup (ctask->curl);
910 GNUNET_CONTAINER_DLL_remove (ctasks_head, ctasks_tail,
914 GNUNET_assert (ctask != NULL);
916 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
917 "curl end %s\n", curl_easy_strerror(msg->data.result));
923 } while (msgnum > 0);
926 for (ctask=ctasks_head; ctask != NULL; ctask = ctask->next)
931 if (num_ctasks != running)
933 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
934 "%d ctasks, %d curl running\n", num_ctasks, running);
937 GNUNET_assert ( num_ctasks == running );
941 } while (mret == CURLM_CALL_MULTI_PERFORM);
944 if (mret != CURLM_OK)
946 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "%s failed at %s:%d: `%s'\n",
947 "curl_multi_perform", __FILE__, __LINE__,
948 curl_multi_strerror (mret));
950 curl_download_prepare();
954 * Process LEHO lookup
956 * @param cls the ctask
957 * @param rd_count number of records returned
958 * @param rd record data
961 process_leho_lookup (void *cls,
963 const struct GNUNET_NAMESTORE_RecordData *rd)
965 struct ProxyCurlTask *ctask = cls;
966 char hosthdr[262]; //256 + "Host: "
970 struct hostent *phost;
972 char resolvename[512];
975 ctask->headers = NULL;
977 strcpy (ctask->leho, "");
980 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
981 "No LEHO present!\n");
983 for (i=0; i<rd_count; i++)
985 if (rd[i].record_type != GNUNET_GNS_RECORD_LEHO)
988 memcpy (ctask->leho, rd[i].data, rd[i].data_size);
990 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
991 "Found LEHO %s for %s\n", ctask->leho, ctask->url);
994 if (0 != strcmp (ctask->leho, ""))
996 sprintf (hosthdr, "%s%s", "Host: ", ctask->leho);
997 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
998 "New HTTP header value: %s\n", hosthdr);
999 ctask->headers = curl_slist_append (ctask->headers, hosthdr);
1000 GNUNET_assert (NULL != ctask->headers);
1001 ret = curl_easy_setopt (ctask->curl, CURLOPT_HTTPHEADER, ctask->headers);
1002 if (CURLE_OK != ret)
1004 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "%s failed at %s:%d: `%s'\n",
1005 "curl_easy_setopt", __FILE__, __LINE__, curl_easy_strerror(ret));
1010 if (ctask->mhd->is_ssl)
1012 phost = (struct hostent*)gethostbyname (ctask->host);
1016 ssl_ip = inet_ntoa(*((struct in_addr*)(phost->h_addr)));
1017 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1018 "SSL target server: %s\n", ssl_ip);
1019 sprintf (resolvename, "%s:%d:%s", ctask->leho, HTTPS_PORT, ssl_ip);
1020 ctask->resolver = curl_slist_append ( ctask->resolver, resolvename);
1021 curl_easy_setopt (ctask->curl, CURLOPT_RESOLVE, ctask->resolver);
1022 sprintf (curlurl, "https://%s%s", ctask->leho, ctask->url);
1023 curl_easy_setopt (ctask->curl, CURLOPT_URL, curlurl);
1027 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1028 "gethostbyname failed for %s!\n", ctask->host);
1029 ctask->download_successful = GNUNET_NO;
1030 ctask->download_error = GNUNET_YES;
1035 if (CURLM_OK != (mret=curl_multi_add_handle (curl_multi, ctask->curl)))
1037 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1038 "%s failed at %s:%d: `%s'\n",
1039 "curl_multi_add_handle", __FILE__, __LINE__,
1040 curl_multi_strerror (mret));
1041 ctask->download_successful = GNUNET_NO;
1042 ctask->download_error = GNUNET_YES;
1045 GNUNET_CONTAINER_DLL_insert (ctasks_head, ctasks_tail, ctask);
1047 curl_download_prepare ();
1052 * Initialize download and trigger curl
1054 * @param cls the proxycurltask
1055 * @param auth_name the name of the authority (site of origin) of ctask->host
1059 process_get_authority (void *cls,
1060 const char* auth_name)
1062 struct ProxyCurlTask *ctask = cls;
1064 if (NULL == auth_name)
1066 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1067 "Get authority failed!\n");
1068 strcpy (ctask->authority, "");
1072 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1073 "Get authority yielded %s\n", auth_name);
1074 strcpy (ctask->authority, auth_name);
1077 GNUNET_GNS_lookup_zone (gns_handle,
1080 &local_shorten_zone,
1081 GNUNET_GNS_RECORD_LEHO,
1082 GNUNET_YES, //Only cached for performance
1083 &process_leho_lookup,
1088 * Main MHD callback for handling requests.
1091 * @param con MHD connection handle
1092 * @param url the url in the request
1093 * @param meth the HTTP method used ("GET", "PUT", etc.)
1094 * @param ver the HTTP version string (i.e. "HTTP/1.1")
1095 * @param upload_data the data being uploaded (excluding HEADERS,
1096 * for a POST that fits into memory and that is encoded
1097 * with a supported encoding, the POST data will NOT be
1098 * given in upload_data and is instead available as
1099 * part of MHD_get_connection_values; very large POST
1100 * data *will* be made available incrementally in
1102 * @param upload_data_size set initially to the size of the
1103 * upload_data provided; the method must update this
1104 * value to the number of bytes NOT processed;
1105 * @param con_cls pointer to location where we store the 'struct Request'
1106 * @return MHD_YES if the connection was handled successfully,
1107 * MHD_NO if the socket must be closed due to a serious
1108 * error while handling the request
1111 create_response (void *cls,
1112 struct MHD_Connection *con,
1116 const char *upload_data,
1117 size_t *upload_data_size,
1121 struct MhdHttpList* hd = cls;
1122 const char* page = "<html><head><title>gnoxy</title>"\
1123 "</head><body>cURL fail</body></html>";
1124 struct MHD_Response *response;
1129 struct ProxyCurlTask *ctask;
1131 if (0 != strcmp (meth, "GET"))
1133 if (&dummy != *con_cls)
1139 if (0 != *upload_data_size)
1144 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1147 MHD_get_connection_values (con,
1149 &con_val_iter, host);
1153 ctask = GNUNET_malloc (sizeof (struct ProxyCurlTask));
1155 ctask->curl = curl_easy_init();
1157 if (curl_multi == NULL)
1158 curl_multi = curl_multi_init ();
1160 if ((ctask->curl == NULL) || (curl_multi == NULL))
1162 response = MHD_create_response_from_buffer (strlen (page),
1164 MHD_RESPMEM_PERSISTENT);
1165 ret = MHD_queue_response (con,
1168 MHD_destroy_response (response);
1169 GNUNET_free (ctask);
1173 ctask->download_in_progress = GNUNET_YES;
1174 ctask->download_successful = GNUNET_NO;
1175 ctask->buf_status = BUF_WAIT_FOR_CURL;
1176 ctask->bytes_in_buffer = 0;
1177 ctask->parse_content = GNUNET_NO;
1179 curl_easy_setopt (ctask->curl, CURLOPT_HEADERFUNCTION, &curl_check_hdr);
1180 curl_easy_setopt (ctask->curl, CURLOPT_HEADERDATA, ctask);
1181 curl_easy_setopt (ctask->curl, CURLOPT_WRITEFUNCTION, &callback_download);
1182 curl_easy_setopt (ctask->curl, CURLOPT_WRITEDATA, ctask);
1183 curl_easy_setopt (ctask->curl, CURLOPT_FOLLOWLOCATION, 1);
1184 curl_easy_setopt (ctask->curl, CURLOPT_MAXREDIRS, 4);
1185 /* no need to abort if the above failed */
1186 if (GNUNET_NO == ctask->mhd->is_ssl)
1187 sprintf (curlurl, "http://%s%s", host, url);
1188 strcpy (ctask->host, host);
1189 strcpy (ctask->url, url);
1190 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1191 "Adding new curl task for %s\n", curlurl);
1193 curl_easy_setopt (ctask->curl, CURLOPT_URL, curlurl);
1194 curl_easy_setopt (ctask->curl, CURLOPT_FAILONERROR, 1);
1195 curl_easy_setopt (ctask->curl, CURLOPT_CONNECTTIMEOUT, 600L);
1196 curl_easy_setopt (ctask->curl, CURLOPT_TIMEOUT, 600L);
1198 GNUNET_GNS_get_authority (gns_handle,
1200 &process_get_authority,
1202 //download_prepare (ctask);
1203 //curl_download_prepare ();
1205 response = MHD_create_response_from_callback (-1, -1,
1210 ret = MHD_queue_response (con, MHD_HTTP_OK, response);
1212 //MHD_destroy_response (response);
1218 * Task run whenever HTTP server operations are pending.
1221 * @param tc sched context
1224 do_httpd (void *cls,
1225 const struct GNUNET_SCHEDULER_TaskContext *tc);
1234 struct MhdHttpList *hd;
1236 for (hd=mhd_httpd_head; hd != NULL; hd = hd->next)
1245 run_httpd (struct MhdHttpList *hd)
1250 struct GNUNET_NETWORK_FDSet *wrs;
1251 struct GNUNET_NETWORK_FDSet *wws;
1252 struct GNUNET_NETWORK_FDSet *wes;
1255 unsigned MHD_LONG_LONG timeout;
1256 struct GNUNET_TIME_Relative tv;
1261 wrs = GNUNET_NETWORK_fdset_create ();
1262 wes = GNUNET_NETWORK_fdset_create ();
1263 wws = GNUNET_NETWORK_fdset_create ();
1265 GNUNET_assert (MHD_YES == MHD_get_fdset (hd->daemon, &rs, &ws, &es, &max));
1268 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1269 "MHD fds: max=%d\n", max);
1271 haveto = MHD_get_timeout (hd->daemon, &timeout);
1273 if (haveto == MHD_YES)
1274 tv.rel_value = (uint64_t) timeout;
1276 tv = GNUNET_TIME_UNIT_FOREVER_REL;
1277 GNUNET_NETWORK_fdset_copy_native (wrs, &rs, max + 1);
1278 GNUNET_NETWORK_fdset_copy_native (wws, &ws, max + 1);
1279 GNUNET_NETWORK_fdset_copy_native (wes, &es, max + 1);
1281 if (hd->httpd_task != GNUNET_SCHEDULER_NO_TASK)
1282 GNUNET_SCHEDULER_cancel (hd->httpd_task);
1284 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_HIGH,
1287 GNUNET_NETWORK_fdset_destroy (wrs);
1288 GNUNET_NETWORK_fdset_destroy (wws);
1289 GNUNET_NETWORK_fdset_destroy (wes);
1294 * Task run whenever HTTP server operations are pending.
1297 * @param tc sched context
1300 do_httpd (void *cls,
1301 const struct GNUNET_SCHEDULER_TaskContext *tc)
1303 struct MhdHttpList *hd = cls;
1305 hd->httpd_task = GNUNET_SCHEDULER_NO_TASK;
1307 MHD_run (hd->daemon);
1313 * Read data from socket
1315 * @param cls the closure
1316 * @param tc scheduler context
1319 do_read (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
1322 * Read from remote end
1324 * @param cls closure
1325 * @param tc scheduler context
1328 do_read_remote (void* cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
1331 * Write data to remote socket
1333 * @param cls the closure
1334 * @param tc scheduler context
1337 do_write_remote (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1339 struct Socks5Request *s5r = cls;
1342 s5r->fwdwtask = GNUNET_SCHEDULER_NO_TASK;
1344 if ((NULL != tc->read_ready) &&
1345 (GNUNET_NETWORK_fdset_isset (tc->write_ready, s5r->remote_sock)) &&
1346 ((len = GNUNET_NETWORK_socket_send (s5r->remote_sock, s5r->rbuf,
1349 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1350 "Successfully sent %d bytes to remote socket\n",
1355 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "write remote");
1357 if (s5r->rtask != GNUNET_SCHEDULER_NO_TASK)
1358 GNUNET_SCHEDULER_cancel (s5r->rtask);
1359 if (s5r->wtask != GNUNET_SCHEDULER_NO_TASK)
1360 GNUNET_SCHEDULER_cancel (s5r->wtask);
1361 if (s5r->fwdrtask != GNUNET_SCHEDULER_NO_TASK)
1362 GNUNET_SCHEDULER_cancel (s5r->fwdrtask);
1363 GNUNET_NETWORK_socket_close (s5r->remote_sock);
1364 GNUNET_NETWORK_socket_close (s5r->sock);
1370 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
1377 * Clean up s5r handles
1379 * @param s5r the handle to destroy
1382 cleanup_s5r (struct Socks5Request *s5r)
1384 if (s5r->rtask != GNUNET_SCHEDULER_NO_TASK)
1385 GNUNET_SCHEDULER_cancel (s5r->rtask);
1386 if (s5r->fwdwtask != GNUNET_SCHEDULER_NO_TASK)
1387 GNUNET_SCHEDULER_cancel (s5r->fwdwtask);
1388 if (s5r->fwdrtask != GNUNET_SCHEDULER_NO_TASK)
1389 GNUNET_SCHEDULER_cancel (s5r->fwdrtask);
1391 if (NULL != s5r->remote_sock)
1392 GNUNET_NETWORK_socket_close (s5r->remote_sock);
1393 if ((NULL != s5r->sock) && (s5r->cleanup_sock == GNUNET_YES))
1394 GNUNET_NETWORK_socket_close (s5r->sock);
1400 * Write data to socket
1402 * @param cls the closure
1403 * @param tc scheduler context
1406 do_write (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1408 struct Socks5Request *s5r = cls;
1411 s5r->wtask = GNUNET_SCHEDULER_NO_TASK;
1413 if ((NULL != tc->read_ready) &&
1414 (GNUNET_NETWORK_fdset_isset (tc->write_ready, s5r->sock)) &&
1415 ((len = GNUNET_NETWORK_socket_send (s5r->sock, s5r->wbuf,
1418 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1419 "Successfully sent %d bytes to socket\n",
1425 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "write");
1426 s5r->cleanup = GNUNET_YES;
1427 s5r->cleanup_sock = GNUNET_YES;
1433 if (GNUNET_YES == s5r->cleanup)
1439 if ((s5r->state == SOCKS5_DATA_TRANSFER) &&
1440 (s5r->fwdrtask == GNUNET_SCHEDULER_NO_TASK))
1442 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
1444 &do_read_remote, s5r);
1448 * Read from remote end
1450 * @param cls closure
1451 * @param tc scheduler context
1454 do_read_remote (void* cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1456 struct Socks5Request *s5r = cls;
1458 s5r->fwdrtask = GNUNET_SCHEDULER_NO_TASK;
1461 if ((NULL != tc->write_ready) &&
1462 (GNUNET_NETWORK_fdset_isset (tc->read_ready, s5r->remote_sock)) &&
1463 (s5r->wbuf_len = GNUNET_NETWORK_socket_recv (s5r->remote_sock, s5r->wbuf,
1464 sizeof (s5r->wbuf))))
1466 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1467 "Successfully read %d bytes from remote socket\n",
1472 if (s5r->wbuf_len == 0)
1473 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1474 "0 bytes received from remote... graceful shutdown!\n");
1475 if (s5r->fwdwtask != GNUNET_SCHEDULER_NO_TASK)
1476 GNUNET_SCHEDULER_cancel (s5r->fwdwtask);
1477 if (s5r->rtask != GNUNET_SCHEDULER_NO_TASK)
1478 GNUNET_SCHEDULER_cancel (s5r->rtask);
1480 GNUNET_NETWORK_socket_close (s5r->remote_sock);
1481 s5r->remote_sock = NULL;
1482 GNUNET_NETWORK_socket_close (s5r->sock);
1488 s5r->wtask = GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
1496 * Adds a socket to MHD
1498 * @param h the handle to the socket to add
1499 * @param daemon the daemon to add the fd to
1500 * @return whatever MHD_add_connection returns
1503 add_handle_to_mhd (struct GNUNET_NETWORK_Handle *h, struct MHD_Daemon *daemon)
1506 struct sockaddr *addr;
1509 fd = GNUNET_NETWORK_get_fd (h);
1510 addr = GNUNET_NETWORK_get_addr (h);
1511 len = GNUNET_NETWORK_get_addrlen (h);
1513 return MHD_add_connection (daemon, fd, addr, len);
1518 get_file_size (const char* filename)
1522 fp = fopen (filename, "rb");
1527 if ((0 != fseek (fp, 0, SEEK_END)) || (-1 == (size = ftell (fp))))
1539 * Read file in filename
1541 * @param filename file to read
1545 load_file (const char* filename)
1551 size = get_file_size (filename);
1555 fp = fopen (filename, "rb");
1559 buffer = GNUNET_malloc (size);
1566 if (size != fread (buffer, 1, size, fp))
1568 GNUNET_free (buffer);
1579 * Load PEM key from file
1581 * @param key where to store the data
1582 * @param keyfile path to the PEM file
1585 load_key_from_file (gnutls_x509_privkey_t key, char* keyfile)
1587 gnutls_datum_t key_data;
1590 key_data.data = (unsigned char*)load_file (keyfile);
1591 key_data.size = strlen ((char*)key_data.data);
1593 ret = gnutls_x509_privkey_import (key, &key_data,
1594 GNUTLS_X509_FMT_PEM);
1596 if (GNUTLS_E_SUCCESS != ret)
1598 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1599 "Unable to import private key %s(ret=%d)\n", key_data.data, ret);
1603 GNUNET_free (key_data.data);
1607 * Load cert from file
1609 * @param crt struct to store data in
1610 * @param certfile path to pem file
1613 load_cert_from_file (gnutls_x509_crt_t crt, char* certfile)
1615 gnutls_datum_t cert_data;
1618 cert_data.data = (unsigned char*)load_file (certfile);
1619 cert_data.size = strlen ((char*)cert_data.data);
1621 ret = gnutls_x509_crt_import (crt, &cert_data,
1622 GNUTLS_X509_FMT_PEM);
1623 if (GNUTLS_E_SUCCESS != ret)
1625 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1626 "Unable to import certificate %s(ret=%d)\n", certfile, ret);
1630 GNUNET_free (cert_data.data);
1636 * Generate new certificate for specific name
1638 * @param name the subject name to generate a cert for
1639 * @return a struct holding the PEM data
1641 static struct ProxyGNSCertificate *
1642 generate_gns_certificate (const char *name)
1646 unsigned int serial;
1647 size_t key_buf_size;
1648 size_t cert_buf_size;
1649 gnutls_x509_crt_t request;
1653 ret = gnutls_x509_crt_init (&request);
1655 if (GNUTLS_E_SUCCESS != ret)
1660 ret = gnutls_x509_crt_set_key (request, proxy_ca.key);
1662 if (GNUTLS_E_SUCCESS != ret)
1667 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Generating cert\n");
1669 struct ProxyGNSCertificate *pgc =
1670 GNUNET_malloc (sizeof (struct ProxyGNSCertificate));
1672 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Adding DNs\n");
1674 gnutls_x509_crt_set_dn_by_oid (request, GNUTLS_OID_X520_COUNTRY_NAME,
1677 gnutls_x509_crt_set_dn_by_oid (request, GNUTLS_OID_X520_ORGANIZATION_NAME,
1680 gnutls_x509_crt_set_dn_by_oid (request, GNUTLS_OID_X520_COMMON_NAME,
1681 0, name, strlen (name));
1683 ret = gnutls_x509_crt_set_version (request, 3);
1685 ret = gnutls_rnd (GNUTLS_RND_NONCE, &serial, sizeof (serial));
1687 etime = time (NULL);
1688 tm_data = localtime (&etime);
1691 ret = gnutls_x509_crt_set_serial (request,
1695 ret = gnutls_x509_crt_set_activation_time (request,
1698 etime = mktime (tm_data);
1705 ret = gnutls_x509_crt_set_expiration_time (request,
1707 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Signing...\n");
1709 ret = gnutls_x509_crt_sign (request, proxy_ca.cert, proxy_ca.key);
1711 key_buf_size = sizeof (pgc->key);
1712 cert_buf_size = sizeof (pgc->cert);
1714 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Exporting certificate...\n");
1716 gnutls_x509_crt_export (request, GNUTLS_X509_FMT_PEM,
1717 pgc->cert, &cert_buf_size);
1719 gnutls_x509_privkey_export (proxy_ca.key, GNUTLS_X509_FMT_PEM,
1720 pgc->key, &key_buf_size);
1723 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Cleaning up\n");
1724 gnutls_x509_crt_deinit (request);
1732 * Accept policy for mhdaemons
1735 * @param addr the sockaddr
1736 * @param addrlen the sockaddr length
1737 * @return MHD_NO if sockaddr is wrong or #conns too high
1740 accept_cb (void* cls, const struct sockaddr *addr, socklen_t addrlen)
1742 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1743 "In MHD accept policy cb\n");
1747 if (addr->sa_family == AF_UNIX)
1751 if (total_mhd_connections >= MAX_MHD_CONNECTIONS)
1754 total_mhd_connections++;
1761 * Adds a socket to an SSL MHD instance
1762 * It is important the the domain name is
1763 * correct. In most cases we need to start a new daemon
1765 * @param h the handle to add to a daemon
1766 * @praram domain the domain the ssl daemon has to serve
1767 * @return MHD_YES on success
1770 add_handle_to_ssl_mhd (struct GNUNET_NETWORK_Handle *h, char* domain)
1772 struct MhdHttpList *hd = NULL;
1773 struct ProxyGNSCertificate *pgc;
1775 for (hd = mhd_httpd_head; hd != NULL; hd = hd->next)
1777 if (0 == strcmp (hd->domain, domain))
1784 pgc = generate_gns_certificate (domain);
1786 hd = GNUNET_malloc (sizeof (struct MhdHttpList));
1787 hd->is_ssl = GNUNET_YES;
1788 strcpy (hd->domain, domain);
1789 hd->proxy_cert = pgc;
1792 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1793 "No previous SSL instance found... starting new one for %s\n",
1796 hd->daemon = MHD_start_daemon (MHD_USE_DEBUG | MHD_USE_SSL, 4444,
1798 &create_response, hd,
1799 MHD_OPTION_LISTEN_SOCKET, GNUNET_NETWORK_get_fd (mhd_unix_socket),
1800 MHD_OPTION_CONNECTION_LIMIT, (unsigned int) 128,
1801 MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) 16,
1802 MHD_OPTION_NOTIFY_COMPLETED,
1804 MHD_OPTION_HTTPS_MEM_KEY, pgc->key,
1805 MHD_OPTION_HTTPS_MEM_CERT, pgc->cert,
1808 GNUNET_assert (hd->daemon != NULL);
1809 hd->httpd_task = GNUNET_SCHEDULER_NO_TASK;
1811 GNUNET_CONTAINER_DLL_insert (mhd_httpd_head, mhd_httpd_tail, hd);
1814 return add_handle_to_mhd (h, hd->daemon);
1820 * Read data from incoming connection
1822 * @param cls the closure
1823 * @param tc the scheduler context
1826 do_read (void* cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1828 struct Socks5Request *s5r = cls;
1829 struct socks5_client_hello *c_hello;
1830 struct socks5_server_hello *s_hello;
1831 struct socks5_client_request *c_req;
1832 struct socks5_server_response *s_resp;
1838 struct hostent *phost;
1840 struct sockaddr_in remote_addr;
1841 struct in_addr *r_sin_addr;
1843 s5r->rtask = GNUNET_SCHEDULER_NO_TASK;
1845 if ((NULL != tc->write_ready) &&
1846 (GNUNET_NETWORK_fdset_isset (tc->read_ready, s5r->sock)) &&
1847 (s5r->rbuf_len = GNUNET_NETWORK_socket_recv (s5r->sock, s5r->rbuf,
1848 sizeof (s5r->rbuf))))
1850 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1851 "Successfully read %d bytes from socket\n",
1856 if (s5r->rbuf_len != 0)
1857 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "read");
1859 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client disco!\n");
1861 if (s5r->fwdrtask != GNUNET_SCHEDULER_NO_TASK)
1862 GNUNET_SCHEDULER_cancel (s5r->fwdrtask);
1863 if (s5r->wtask != GNUNET_SCHEDULER_NO_TASK)
1864 GNUNET_SCHEDULER_cancel (s5r->wtask);
1865 if (s5r->fwdwtask != GNUNET_SCHEDULER_NO_TASK)
1866 GNUNET_SCHEDULER_cancel (s5r->fwdwtask);
1867 GNUNET_NETWORK_socket_close (s5r->remote_sock);
1868 GNUNET_NETWORK_socket_close (s5r->sock);
1873 if (s5r->state == SOCKS5_INIT)
1875 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1877 c_hello = (struct socks5_client_hello*)&s5r->rbuf;
1879 GNUNET_assert (c_hello->version == SOCKS_VERSION_5);
1881 s_hello = (struct socks5_server_hello*)&s5r->wbuf;
1882 s5r->wbuf_len = sizeof( struct socks5_server_hello );
1884 s_hello->version = c_hello->version;
1885 s_hello->auth_method = SOCKS_AUTH_NONE;
1887 /* Write response to client */
1888 s5r->wtask = GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
1892 s5r->rtask = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
1896 s5r->state = SOCKS5_REQUEST;
1900 if (s5r->state == SOCKS5_REQUEST)
1902 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1903 "Processing SOCKS5 request\n");
1904 c_req = (struct socks5_client_request*)&s5r->rbuf;
1905 s_resp = (struct socks5_server_response*)&s5r->wbuf;
1906 //Only 10byte for ipv4 response!
1907 s5r->wbuf_len = 10;//sizeof (struct socks5_server_response);
1909 GNUNET_assert (c_req->addr_type == 3);
1911 dom_len = *((uint8_t*)(&(c_req->addr_type) + 1));
1912 memset(domain, 0, sizeof(domain));
1913 strncpy(domain, (char*)(&(c_req->addr_type) + 2), dom_len);
1914 req_port = *((uint16_t*)(&(c_req->addr_type) + 2 + dom_len));
1916 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1917 "Requested connection is %s:%d\n",
1921 if (is_tld (domain, GNUNET_GNS_TLD) ||
1922 is_tld (domain, GNUNET_GNS_TLD_ZKEY))
1924 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1925 "Requested connection is gnunet tld\n",
1929 if (ntohs(req_port) == HTTPS_PORT)
1931 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1932 "Requested connection is HTTPS\n");
1933 ret = add_handle_to_ssl_mhd ( s5r->sock, domain );
1935 else if (NULL != httpd)
1937 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1938 "Requested connection is HTTP\n");
1939 ret = add_handle_to_mhd ( s5r->sock, httpd );
1944 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1945 _("Failed to start HTTP server\n"));
1946 s_resp->version = 0x05;
1947 s_resp->reply = 0x01;
1948 s5r->cleanup = GNUNET_YES;
1949 s5r->cleanup_sock = GNUNET_YES;
1951 GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
1957 /* Signal success */
1958 s_resp->version = 0x05;
1959 s_resp->reply = 0x00;
1960 s_resp->reserved = 0x00;
1961 s_resp->addr_type = 0x01;
1963 s5r->cleanup = GNUNET_YES;
1964 s5r->cleanup_sock = GNUNET_NO;
1966 GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
1974 phost = (struct hostent*)gethostbyname (domain);
1977 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1978 "Resolve %s error!\n", domain );
1979 s_resp->version = 0x05;
1980 s_resp->reply = 0x01;
1981 s5r->cleanup = GNUNET_YES;
1982 s5r->cleanup_sock = GNUNET_YES;
1984 GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
1990 s5r->remote_sock = GNUNET_NETWORK_socket_create (AF_INET,
1993 r_sin_addr = (struct in_addr*)(phost->h_addr);
1994 remote_ip = r_sin_addr->s_addr;
1995 memset(&remote_addr, 0, sizeof(remote_addr));
1996 remote_addr.sin_family = AF_INET;
1997 #if HAVE_SOCKADDR_IN_SIN_LEN
1998 remote_addr.sin_len = sizeof (remote_addr);
2000 remote_addr.sin_addr.s_addr = remote_ip;
2001 remote_addr.sin_port = req_port;
2003 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2004 "target server: %s:%u\n", inet_ntoa(remote_addr.sin_addr),
2008 GNUNET_NETWORK_socket_connect ( s5r->remote_sock,
2009 (const struct sockaddr*)&remote_addr,
2010 sizeof (remote_addr)))
2011 && (errno != EINPROGRESS))
2013 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "connect");
2014 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2015 "socket request error...\n");
2016 s_resp->version = 0x05;
2017 s_resp->reply = 0x01;
2019 GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
2026 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2027 "new remote connection\n");
2029 s_resp->version = 0x05;
2030 s_resp->reply = 0x00;
2031 s_resp->reserved = 0x00;
2032 s_resp->addr_type = 0x01;
2034 s5r->state = SOCKS5_DATA_TRANSFER;
2037 GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
2041 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
2049 if (s5r->state == SOCKS5_DATA_TRANSFER)
2051 if ((s5r->remote_sock == NULL) || (s5r->rbuf_len == 0))
2053 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2054 "Closing connection to client\n");
2055 if (s5r->rtask != GNUNET_SCHEDULER_NO_TASK)
2056 GNUNET_SCHEDULER_cancel (s5r->rtask);
2057 if (s5r->fwdwtask != GNUNET_SCHEDULER_NO_TASK)
2058 GNUNET_SCHEDULER_cancel (s5r->fwdwtask);
2059 if (s5r->fwdrtask != GNUNET_SCHEDULER_NO_TASK)
2060 GNUNET_SCHEDULER_cancel (s5r->fwdrtask);
2061 if (s5r->fwdrtask != GNUNET_SCHEDULER_NO_TASK)
2062 GNUNET_SCHEDULER_cancel (s5r->fwdrtask);
2064 if (s5r->remote_sock != NULL)
2065 GNUNET_NETWORK_socket_close (s5r->remote_sock);
2066 GNUNET_NETWORK_socket_close (s5r->sock);
2071 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2072 "forwarding %d bytes from client\n", s5r->rbuf_len);
2075 GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
2077 &do_write_remote, s5r);
2079 if (s5r->fwdrtask == GNUNET_SCHEDULER_NO_TASK)
2082 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
2084 &do_read_remote, s5r);
2090 //GNUNET_CONTAINER_DLL_remove (s5conns.head, s5conns.tail, s5r);
2096 * Accept new incoming connections
2098 * @param cls the closure
2099 * @param tc the scheduler context
2102 do_accept (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2104 struct GNUNET_NETWORK_Handle *s;
2105 struct Socks5Request *s5r;
2107 ltask = GNUNET_SCHEDULER_NO_TASK;
2108 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
2111 ltask = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
2115 s = GNUNET_NETWORK_socket_accept (lsock, NULL, NULL);
2119 GNUNET_log_strerror (GNUNET_ERROR_TYPE_INFO, "accept");
2123 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2124 "Got an inbound connection, waiting for data\n");
2126 s5r = GNUNET_malloc (sizeof (struct Socks5Request));
2128 s5r->state = SOCKS5_INIT;
2129 s5r->wtask = GNUNET_SCHEDULER_NO_TASK;
2130 s5r->fwdwtask = GNUNET_SCHEDULER_NO_TASK;
2131 s5r->fwdrtask = GNUNET_SCHEDULER_NO_TASK;
2132 s5r->rtask = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
2135 //GNUNET_CONTAINER_DLL_insert (s5conns.head, s5conns.tail, s5r);
2140 * Task run on shutdown
2142 * @param cls closure
2143 * @param tc task context
2146 do_shutdown (void *cls,
2147 const struct GNUNET_SCHEDULER_TaskContext *tc)
2150 struct MhdHttpList *hd;
2151 struct MhdHttpList *tmp_hd;
2152 struct ProxyCurlTask *ctask;
2153 struct ProxyCurlTask *ctask_tmp;
2155 if (GNUNET_SCHEDULER_NO_TASK != curl_download_task)
2157 GNUNET_SCHEDULER_cancel (curl_download_task);
2158 curl_download_task = GNUNET_SCHEDULER_NO_TASK;
2161 for (hd = mhd_httpd_head; hd != NULL; hd = tmp_hd)
2165 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2166 "Stopping daemon\n");
2168 if (GNUNET_SCHEDULER_NO_TASK != hd->httpd_task)
2170 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2171 "Stopping select task %d\n",
2173 GNUNET_SCHEDULER_cancel (hd->httpd_task);
2174 hd->httpd_task = GNUNET_SCHEDULER_NO_TASK;
2177 if (NULL != hd->daemon)
2179 MHD_stop_daemon (hd->daemon);
2183 if (NULL != hd->proxy_cert)
2185 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2186 "Free certificate\n");
2187 GNUNET_free (hd->proxy_cert);
2193 for (ctask=ctasks_head; ctask != NULL; ctask=ctask_tmp)
2195 ctask_tmp = ctask->next;
2197 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2198 "Cleaning up cURL task\n");
2200 if (ctask->curl != NULL)
2201 curl_easy_cleanup (ctask->curl);
2203 if (NULL != ctask->headers)
2204 curl_slist_free_all (ctask->headers);
2206 GNUNET_free (ctask);
2209 GNUNET_GNS_disconnect (gns_handle);
2214 * Compiles a regex for us
2216 * @param re ptr to re struct
2217 * @param rt the expression to compile
2218 * @return 0 on success
2221 compile_regex (regex_t *re, const char* rt)
2226 status = regcomp (re, rt, REG_EXTENDED|REG_NEWLINE);
2229 regerror (status, re, err, 1024);
2230 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2231 "Regex error compiling '%s': %s\n", rt, err);
2239 * Loads the users local zone key
2241 * @return GNUNET_YES on success
2244 load_local_zone_key (const struct GNUNET_CONFIGURATION_Handle *cfg)
2247 struct GNUNET_CRYPTO_RsaPrivateKey *key = NULL;
2248 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pkey;
2249 struct GNUNET_CRYPTO_ShortHashCode *zone = NULL;
2250 struct GNUNET_CRYPTO_ShortHashAsciiEncoded zonename;
2252 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "gns",
2253 "ZONEKEY", &keyfile))
2255 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2256 "Unable to load zone key config value!\n");
2260 if (GNUNET_NO == GNUNET_DISK_file_test (keyfile))
2262 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2263 "Unable to load zone key %s!\n", keyfile);
2264 GNUNET_free(keyfile);
2268 key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
2269 GNUNET_CRYPTO_rsa_key_get_public (key, &pkey);
2270 GNUNET_CRYPTO_short_hash(&pkey,
2271 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
2273 zone = &local_gns_zone;
2274 GNUNET_CRYPTO_short_hash_to_enc (zone, &zonename);
2275 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2276 "Using zone: %s!\n", &zonename);
2277 GNUNET_CRYPTO_rsa_key_free(key);
2278 GNUNET_free(keyfile);
2284 * Loads the users local shorten zone key
2286 * @return GNUNET_YES on success
2289 load_local_shorten_key (const struct GNUNET_CONFIGURATION_Handle *cfg)
2292 struct GNUNET_CRYPTO_RsaPrivateKey *key = NULL;
2293 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pkey;
2294 struct GNUNET_CRYPTO_ShortHashCode *zone = NULL;
2295 struct GNUNET_CRYPTO_ShortHashAsciiEncoded zonename;
2297 if (GNUNET_NO == GNUNET_CONFIGURATION_get_value_yesno (cfg, "gns",
2298 "AUTO_IMPORT_PKEY"))
2303 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "gns",
2304 "AUTO_IMPORT_ZONEKEY",
2307 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2308 "Unable to load shorten key config value! (not fatal)\n");
2312 if (GNUNET_NO == GNUNET_DISK_file_test (keyfile))
2314 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2315 "Unable to load shorten key %s! (not fatal)\n", keyfile);
2316 GNUNET_free(keyfile);
2320 key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
2321 GNUNET_CRYPTO_rsa_key_get_public (key, &pkey);
2322 GNUNET_CRYPTO_short_hash(&pkey,
2323 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
2324 &local_shorten_zone);
2325 zone = &local_gns_zone;
2326 GNUNET_CRYPTO_short_hash_to_enc (zone, &zonename);
2327 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2328 "Using shorten zone: %s!\n", &zonename);
2329 GNUNET_CRYPTO_rsa_key_free(key);
2330 GNUNET_free(keyfile);
2336 * Main function that will be run
2338 * @param cls closure
2339 * @param args remaining command-line arguments
2340 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
2341 * @param cfg configuration
2344 run (void *cls, char *const *args, const char *cfgfile,
2345 const struct GNUNET_CONFIGURATION_Handle *cfg)
2347 struct sockaddr_in sa;
2348 struct MhdHttpList *hd;
2349 struct sockaddr_un mhd_unix_sock_addr;
2351 char* proxy_sockfile;
2352 char* cafile_cfg = NULL;
2355 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2358 cafile = cafile_opt;
2362 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "gns-proxy",
2366 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2367 "Unable to load proxy CA config value!\n");
2368 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2369 "No proxy CA provided!\n");
2372 cafile = cafile_cfg;
2375 gnutls_global_init ();
2377 gnutls_x509_crt_init (&proxy_ca.cert);
2378 gnutls_x509_privkey_init (&proxy_ca.key);
2380 load_cert_from_file (proxy_ca.cert, cafile);
2381 load_key_from_file (proxy_ca.key, cafile);
2384 GNUNET_free (cafile_cfg);
2386 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2387 "Loading Template\n");
2389 compile_regex (&re_dotplus, (char*) RE_A_HREF);
2391 gns_handle = GNUNET_GNS_connect (cfg);
2393 if (GNUNET_NO == load_local_zone_key (cfg))
2395 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2396 "Unable to load zone!\n");
2400 use_shorten = load_local_shorten_key (cfg);
2402 if (NULL == gns_handle)
2404 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2405 "Unable to connect to GNS!\n");
2409 memset (&sa, 0, sizeof (sa));
2410 sa.sin_family = AF_INET;
2411 sa.sin_port = htons (port);
2412 #if HAVE_SOCKADDR_IN_SIN_LEN
2413 sa.sin_len = sizeof (sa);
2416 lsock = GNUNET_NETWORK_socket_create (AF_INET,
2420 if ((NULL == lsock) ||
2422 GNUNET_NETWORK_socket_bind (lsock, (const struct sockaddr *) &sa,
2425 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2426 "Failed to create listen socket bound to `%s'",
2427 GNUNET_a2s ((const struct sockaddr *) &sa, sizeof (sa)));
2429 GNUNET_NETWORK_socket_close (lsock);
2433 if (GNUNET_OK != GNUNET_NETWORK_socket_listen (lsock, 5))
2435 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2436 "Failed to listen on socket bound to `%s'",
2437 GNUNET_a2s ((const struct sockaddr *) &sa, sizeof (sa)));
2441 ltask = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
2442 lsock, &do_accept, NULL);
2447 if (0 != curl_global_init (CURL_GLOBAL_WIN32))
2449 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2450 "cURL global init failed!\n");
2454 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2455 "Proxy listens on port %u\n",
2458 mhd_httpd_head = NULL;
2459 mhd_httpd_tail = NULL;
2460 total_mhd_connections = 0;
2462 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "gns-proxy",
2466 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2467 "Specify PROXY_UNIXPATH in gns-proxy config section!\n");
2471 mhd_unix_socket = GNUNET_NETWORK_socket_create (AF_UNIX,
2475 if (NULL == mhd_unix_socket)
2477 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2478 "Unable to create unix domain socket!\n");
2482 mhd_unix_sock_addr.sun_family = AF_UNIX;
2483 strcpy (mhd_unix_sock_addr.sun_path, proxy_sockfile);
2484 unlink (proxy_sockfile);
2485 len = strlen (proxy_sockfile) + sizeof(AF_UNIX);
2487 GNUNET_free (proxy_sockfile);
2489 if (GNUNET_OK != GNUNET_NETWORK_socket_bind (mhd_unix_socket,
2490 (struct sockaddr*)&mhd_unix_sock_addr,
2493 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2494 "Unable to bind unix domain socket!\n");
2498 if (GNUNET_OK != GNUNET_NETWORK_socket_listen (mhd_unix_socket,
2501 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2502 "Unable to listen on unix domain socket!\n");
2506 hd = GNUNET_malloc (sizeof (struct MhdHttpList));
2507 hd->is_ssl = GNUNET_NO;
2508 strcpy (hd->domain, "");
2509 httpd = MHD_start_daemon (MHD_USE_DEBUG, 4444, //Dummy port
2511 &create_response, hd,
2512 MHD_OPTION_LISTEN_SOCKET, GNUNET_NETWORK_get_fd (mhd_unix_socket),
2513 MHD_OPTION_CONNECTION_LIMIT, (unsigned int) 128,
2514 MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) 16,
2515 MHD_OPTION_NOTIFY_COMPLETED,
2519 GNUNET_assert (httpd != NULL);
2521 hd->httpd_task = GNUNET_SCHEDULER_NO_TASK;
2523 GNUNET_CONTAINER_DLL_insert (mhd_httpd_head, mhd_httpd_tail, hd);
2527 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
2528 &do_shutdown, NULL);
2534 * The main function for gnunet-gns-proxy.
2536 * @param argc number of arguments from the command line
2537 * @param argv command line arguments
2538 * @return 0 ok, 1 on error
2541 main (int argc, char *const *argv)
2543 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
2545 gettext_noop ("listen on specified port"), 1,
2546 &GNUNET_GETOPT_set_string, &port},
2547 {'a', "authority", NULL,
2548 gettext_noop ("pem file to use as CA"), 1,
2549 &GNUNET_GETOPT_set_string, &cafile_opt},
2550 GNUNET_GETOPT_OPTION_END
2555 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
2558 GNUNET_log_setup ("gnunet-gns-proxy", "WARNING", NULL);
2561 GNUNET_PROGRAM_run (argc, argv, "gnunet-gns-proxy",
2562 _("GNUnet GNS proxy"),
2564 &run, NULL)) ? 0 : 1;