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_INFO,
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_INFO,
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 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1021 "Curl resolve: %s\n", resolvename);
1022 ctask->resolver = curl_slist_append ( ctask->resolver, resolvename);
1023 curl_easy_setopt (ctask->curl, CURLOPT_RESOLVE, ctask->resolver);
1024 sprintf (curlurl, "https://%s%s", ctask->leho, ctask->url);
1025 curl_easy_setopt (ctask->curl, CURLOPT_URL, curlurl);
1029 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1030 "gethostbyname failed for %s!\n", ctask->host);
1031 ctask->download_successful = GNUNET_NO;
1032 ctask->download_error = GNUNET_YES;
1037 if (CURLM_OK != (mret=curl_multi_add_handle (curl_multi, ctask->curl)))
1039 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1040 "%s failed at %s:%d: `%s'\n",
1041 "curl_multi_add_handle", __FILE__, __LINE__,
1042 curl_multi_strerror (mret));
1043 ctask->download_successful = GNUNET_NO;
1044 ctask->download_error = GNUNET_YES;
1047 GNUNET_CONTAINER_DLL_insert (ctasks_head, ctasks_tail, ctask);
1049 curl_download_prepare ();
1054 * Initialize download and trigger curl
1056 * @param cls the proxycurltask
1057 * @param auth_name the name of the authority (site of origin) of ctask->host
1061 process_get_authority (void *cls,
1062 const char* auth_name)
1064 struct ProxyCurlTask *ctask = cls;
1066 if (NULL == auth_name)
1068 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1069 "Get authority failed!\n");
1070 strcpy (ctask->authority, "");
1074 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1075 "Get authority yielded %s\n", auth_name);
1076 strcpy (ctask->authority, auth_name);
1079 GNUNET_GNS_lookup_zone (gns_handle,
1082 &local_shorten_zone,
1083 GNUNET_GNS_RECORD_LEHO,
1084 GNUNET_YES, //Only cached for performance
1085 &process_leho_lookup,
1090 * Main MHD callback for handling requests.
1093 * @param con MHD connection handle
1094 * @param url the url in the request
1095 * @param meth the HTTP method used ("GET", "PUT", etc.)
1096 * @param ver the HTTP version string (i.e. "HTTP/1.1")
1097 * @param upload_data the data being uploaded (excluding HEADERS,
1098 * for a POST that fits into memory and that is encoded
1099 * with a supported encoding, the POST data will NOT be
1100 * given in upload_data and is instead available as
1101 * part of MHD_get_connection_values; very large POST
1102 * data *will* be made available incrementally in
1104 * @param upload_data_size set initially to the size of the
1105 * upload_data provided; the method must update this
1106 * value to the number of bytes NOT processed;
1107 * @param con_cls pointer to location where we store the 'struct Request'
1108 * @return MHD_YES if the connection was handled successfully,
1109 * MHD_NO if the socket must be closed due to a serious
1110 * error while handling the request
1113 create_response (void *cls,
1114 struct MHD_Connection *con,
1118 const char *upload_data,
1119 size_t *upload_data_size,
1123 struct MhdHttpList* hd = cls;
1124 const char* page = "<html><head><title>gnoxy</title>"\
1125 "</head><body>cURL fail</body></html>";
1126 struct MHD_Response *response = NULL;
1131 struct ProxyCurlTask *ctask;
1133 if (0 != strcmp (meth, "GET"))
1135 if (&dummy != *con_cls)
1141 if (0 != *upload_data_size)
1146 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1149 MHD_get_connection_values (con,
1151 &con_val_iter, host);
1155 ctask = GNUNET_malloc (sizeof (struct ProxyCurlTask));
1158 if (curl_multi == NULL)
1159 curl_multi = curl_multi_init ();
1161 ctask->curl = curl_easy_init();
1163 if ((ctask->curl == NULL) || (curl_multi == NULL))
1165 response = MHD_create_response_from_buffer (strlen (page),
1167 MHD_RESPMEM_PERSISTENT);
1168 ret = MHD_queue_response (con,
1171 MHD_destroy_response (response);
1172 GNUNET_free (ctask);
1178 ctask->headers = NULL;
1179 ctask->resolver = NULL;
1180 ctask->buffer_ptr = NULL;
1181 ctask->download_in_progress = GNUNET_YES;
1182 ctask->download_successful = GNUNET_NO;
1183 ctask->buf_status = BUF_WAIT_FOR_CURL;
1184 ctask->bytes_in_buffer = 0;
1185 ctask->parse_content = GNUNET_NO;
1187 curl_easy_setopt (ctask->curl, CURLOPT_HEADERFUNCTION, &curl_check_hdr);
1188 curl_easy_setopt (ctask->curl, CURLOPT_HEADERDATA, ctask);
1189 curl_easy_setopt (ctask->curl, CURLOPT_WRITEFUNCTION, &callback_download);
1190 curl_easy_setopt (ctask->curl, CURLOPT_WRITEDATA, ctask);
1191 curl_easy_setopt (ctask->curl, CURLOPT_FOLLOWLOCATION, 1);
1192 curl_easy_setopt (ctask->curl, CURLOPT_MAXREDIRS, 4);
1193 /* no need to abort if the above failed */
1194 if (GNUNET_NO == ctask->mhd->is_ssl)
1196 sprintf (curlurl, "http://%s%s", host, url);
1197 curl_easy_setopt (ctask->curl, CURLOPT_URL, curlurl);
1198 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1199 "Adding new curl task for %s\n", curlurl);
1201 strcpy (ctask->host, host);
1202 strcpy (ctask->url, url);
1204 //curl_easy_setopt (ctask->curl, CURLOPT_URL, curlurl);
1205 curl_easy_setopt (ctask->curl, CURLOPT_FAILONERROR, 1);
1206 curl_easy_setopt (ctask->curl, CURLOPT_CONNECTTIMEOUT, 600L);
1207 curl_easy_setopt (ctask->curl, CURLOPT_TIMEOUT, 600L);
1209 GNUNET_GNS_get_authority (gns_handle,
1211 &process_get_authority,
1213 //download_prepare (ctask);
1214 //curl_download_prepare ();
1216 response = MHD_create_response_from_callback (MHD_SIZE_UNKNOWN,
1222 ret = MHD_queue_response (con, MHD_HTTP_OK, response);
1224 //MHD_destroy_response (response);
1230 * Task run whenever HTTP server operations are pending.
1233 * @param tc sched context
1236 do_httpd (void *cls,
1237 const struct GNUNET_SCHEDULER_TaskContext *tc);
1246 struct MhdHttpList *hd;
1248 for (hd=mhd_httpd_head; hd != NULL; hd = hd->next)
1257 run_httpd (struct MhdHttpList *hd)
1262 struct GNUNET_NETWORK_FDSet *wrs;
1263 struct GNUNET_NETWORK_FDSet *wws;
1264 struct GNUNET_NETWORK_FDSet *wes;
1267 unsigned MHD_LONG_LONG timeout;
1268 struct GNUNET_TIME_Relative tv;
1273 wrs = GNUNET_NETWORK_fdset_create ();
1274 wes = GNUNET_NETWORK_fdset_create ();
1275 wws = GNUNET_NETWORK_fdset_create ();
1277 GNUNET_assert (MHD_YES == MHD_get_fdset (hd->daemon, &rs, &ws, &es, &max));
1280 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1281 "MHD fds: max=%d\n", max);
1283 haveto = MHD_get_timeout (hd->daemon, &timeout);
1285 if (haveto == MHD_YES)
1286 tv.rel_value = (uint64_t) timeout;
1288 tv = GNUNET_TIME_UNIT_FOREVER_REL;
1289 GNUNET_NETWORK_fdset_copy_native (wrs, &rs, max + 1);
1290 GNUNET_NETWORK_fdset_copy_native (wws, &ws, max + 1);
1291 GNUNET_NETWORK_fdset_copy_native (wes, &es, max + 1);
1293 if (hd->httpd_task != GNUNET_SCHEDULER_NO_TASK)
1294 GNUNET_SCHEDULER_cancel (hd->httpd_task);
1296 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_HIGH,
1299 GNUNET_NETWORK_fdset_destroy (wrs);
1300 GNUNET_NETWORK_fdset_destroy (wws);
1301 GNUNET_NETWORK_fdset_destroy (wes);
1306 * Task run whenever HTTP server operations are pending.
1309 * @param tc sched context
1312 do_httpd (void *cls,
1313 const struct GNUNET_SCHEDULER_TaskContext *tc)
1315 struct MhdHttpList *hd = cls;
1317 hd->httpd_task = GNUNET_SCHEDULER_NO_TASK;
1319 MHD_run (hd->daemon);
1325 * Read data from socket
1327 * @param cls the closure
1328 * @param tc scheduler context
1331 do_read (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
1334 * Read from remote end
1336 * @param cls closure
1337 * @param tc scheduler context
1340 do_read_remote (void* cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
1343 * Write data to remote socket
1345 * @param cls the closure
1346 * @param tc scheduler context
1349 do_write_remote (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1351 struct Socks5Request *s5r = cls;
1354 s5r->fwdwtask = GNUNET_SCHEDULER_NO_TASK;
1356 if ((NULL != tc->read_ready) &&
1357 (GNUNET_NETWORK_fdset_isset (tc->write_ready, s5r->remote_sock)) &&
1358 ((len = GNUNET_NETWORK_socket_send (s5r->remote_sock, s5r->rbuf,
1361 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1362 "Successfully sent %d bytes to remote socket\n",
1367 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "write remote");
1369 if (s5r->rtask != GNUNET_SCHEDULER_NO_TASK)
1370 GNUNET_SCHEDULER_cancel (s5r->rtask);
1371 if (s5r->wtask != GNUNET_SCHEDULER_NO_TASK)
1372 GNUNET_SCHEDULER_cancel (s5r->wtask);
1373 if (s5r->fwdrtask != GNUNET_SCHEDULER_NO_TASK)
1374 GNUNET_SCHEDULER_cancel (s5r->fwdrtask);
1375 GNUNET_NETWORK_socket_close (s5r->remote_sock);
1376 GNUNET_NETWORK_socket_close (s5r->sock);
1382 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
1389 * Clean up s5r handles
1391 * @param s5r the handle to destroy
1394 cleanup_s5r (struct Socks5Request *s5r)
1396 if (s5r->rtask != GNUNET_SCHEDULER_NO_TASK)
1397 GNUNET_SCHEDULER_cancel (s5r->rtask);
1398 if (s5r->fwdwtask != GNUNET_SCHEDULER_NO_TASK)
1399 GNUNET_SCHEDULER_cancel (s5r->fwdwtask);
1400 if (s5r->fwdrtask != GNUNET_SCHEDULER_NO_TASK)
1401 GNUNET_SCHEDULER_cancel (s5r->fwdrtask);
1403 if (NULL != s5r->remote_sock)
1404 GNUNET_NETWORK_socket_close (s5r->remote_sock);
1405 if ((NULL != s5r->sock) && (s5r->cleanup_sock == GNUNET_YES))
1406 GNUNET_NETWORK_socket_close (s5r->sock);
1412 * Write data to socket
1414 * @param cls the closure
1415 * @param tc scheduler context
1418 do_write (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1420 struct Socks5Request *s5r = cls;
1423 s5r->wtask = GNUNET_SCHEDULER_NO_TASK;
1425 if ((NULL != tc->read_ready) &&
1426 (GNUNET_NETWORK_fdset_isset (tc->write_ready, s5r->sock)) &&
1427 ((len = GNUNET_NETWORK_socket_send (s5r->sock, s5r->wbuf,
1430 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1431 "Successfully sent %d bytes to socket\n",
1437 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "write");
1438 s5r->cleanup = GNUNET_YES;
1439 s5r->cleanup_sock = GNUNET_YES;
1445 if (GNUNET_YES == s5r->cleanup)
1451 if ((s5r->state == SOCKS5_DATA_TRANSFER) &&
1452 (s5r->fwdrtask == GNUNET_SCHEDULER_NO_TASK))
1454 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
1456 &do_read_remote, s5r);
1460 * Read from remote end
1462 * @param cls closure
1463 * @param tc scheduler context
1466 do_read_remote (void* cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1468 struct Socks5Request *s5r = cls;
1470 s5r->fwdrtask = GNUNET_SCHEDULER_NO_TASK;
1473 if ((NULL != tc->write_ready) &&
1474 (GNUNET_NETWORK_fdset_isset (tc->read_ready, s5r->remote_sock)) &&
1475 (s5r->wbuf_len = GNUNET_NETWORK_socket_recv (s5r->remote_sock, s5r->wbuf,
1476 sizeof (s5r->wbuf))))
1478 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1479 "Successfully read %d bytes from remote socket\n",
1484 if (s5r->wbuf_len == 0)
1485 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1486 "0 bytes received from remote... graceful shutdown!\n");
1487 if (s5r->fwdwtask != GNUNET_SCHEDULER_NO_TASK)
1488 GNUNET_SCHEDULER_cancel (s5r->fwdwtask);
1489 if (s5r->rtask != GNUNET_SCHEDULER_NO_TASK)
1490 GNUNET_SCHEDULER_cancel (s5r->rtask);
1492 GNUNET_NETWORK_socket_close (s5r->remote_sock);
1493 s5r->remote_sock = NULL;
1494 GNUNET_NETWORK_socket_close (s5r->sock);
1500 s5r->wtask = GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
1508 * Adds a socket to MHD
1510 * @param h the handle to the socket to add
1511 * @param daemon the daemon to add the fd to
1512 * @return whatever MHD_add_connection returns
1515 add_handle_to_mhd (struct GNUNET_NETWORK_Handle *h, struct MHD_Daemon *daemon)
1518 struct sockaddr *addr;
1521 fd = GNUNET_NETWORK_get_fd (h);
1522 addr = GNUNET_NETWORK_get_addr (h);
1523 len = GNUNET_NETWORK_get_addrlen (h);
1525 return MHD_add_connection (daemon, fd, addr, len);
1530 get_file_size (const char* filename)
1534 fp = fopen (filename, "rb");
1539 if ((0 != fseek (fp, 0, SEEK_END)) || (-1 == (size = ftell (fp))))
1551 * Read file in filename
1553 * @param filename file to read
1557 load_file (const char* filename, unsigned int* size)
1562 *size = get_file_size (filename);
1566 fp = fopen (filename, "rb");
1570 buffer = GNUNET_malloc (*size);
1577 if (*size != fread (buffer, 1, *size, fp))
1579 GNUNET_free (buffer);
1590 * Load PEM key from file
1592 * @param key where to store the data
1593 * @param keyfile path to the PEM file
1596 load_key_from_file (gnutls_x509_privkey_t key, char* keyfile)
1598 gnutls_datum_t key_data;
1599 key_data.data = NULL;
1602 key_data.data = (unsigned char*)load_file (keyfile, &key_data.size);
1604 ret = gnutls_x509_privkey_import (key, &key_data,
1605 GNUTLS_X509_FMT_PEM);
1607 if (GNUTLS_E_SUCCESS != ret)
1609 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1610 "Unable to import private key %s(ret=%d)\n", key_data.data, ret);
1614 GNUNET_free (key_data.data);
1618 * Load cert from file
1620 * @param crt struct to store data in
1621 * @param certfile path to pem file
1624 load_cert_from_file (gnutls_x509_crt_t crt, char* certfile)
1626 gnutls_datum_t cert_data;
1627 cert_data.data = NULL;
1630 cert_data.data = (unsigned char*)load_file (certfile, &cert_data.size);
1632 ret = gnutls_x509_crt_import (crt, &cert_data,
1633 GNUTLS_X509_FMT_PEM);
1634 if (GNUTLS_E_SUCCESS != ret)
1636 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1637 "Unable to import certificate %s(ret=%d)\n", certfile, ret);
1641 GNUNET_free (cert_data.data);
1647 * Generate new certificate for specific name
1649 * @param name the subject name to generate a cert for
1650 * @return a struct holding the PEM data
1652 static struct ProxyGNSCertificate *
1653 generate_gns_certificate (const char *name)
1657 unsigned int serial;
1658 size_t key_buf_size;
1659 size_t cert_buf_size;
1660 gnutls_x509_crt_t request;
1664 ret = gnutls_x509_crt_init (&request);
1666 if (GNUTLS_E_SUCCESS != ret)
1671 ret = gnutls_x509_crt_set_key (request, proxy_ca.key);
1673 if (GNUTLS_E_SUCCESS != ret)
1678 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Generating cert\n");
1680 struct ProxyGNSCertificate *pgc =
1681 GNUNET_malloc (sizeof (struct ProxyGNSCertificate));
1683 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Adding DNs\n");
1685 gnutls_x509_crt_set_dn_by_oid (request, GNUTLS_OID_X520_COUNTRY_NAME,
1688 gnutls_x509_crt_set_dn_by_oid (request, GNUTLS_OID_X520_ORGANIZATION_NAME,
1691 gnutls_x509_crt_set_dn_by_oid (request, GNUTLS_OID_X520_COMMON_NAME,
1692 0, name, strlen (name));
1694 ret = gnutls_x509_crt_set_version (request, 3);
1696 ret = gnutls_rnd (GNUTLS_RND_NONCE, &serial, sizeof (serial));
1698 etime = time (NULL);
1699 tm_data = localtime (&etime);
1702 ret = gnutls_x509_crt_set_serial (request,
1706 ret = gnutls_x509_crt_set_activation_time (request,
1709 etime = mktime (tm_data);
1716 ret = gnutls_x509_crt_set_expiration_time (request,
1718 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Signing...\n");
1720 ret = gnutls_x509_crt_sign (request, proxy_ca.cert, proxy_ca.key);
1722 key_buf_size = sizeof (pgc->key);
1723 cert_buf_size = sizeof (pgc->cert);
1725 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Exporting certificate...\n");
1727 gnutls_x509_crt_export (request, GNUTLS_X509_FMT_PEM,
1728 pgc->cert, &cert_buf_size);
1730 gnutls_x509_privkey_export (proxy_ca.key, GNUTLS_X509_FMT_PEM,
1731 pgc->key, &key_buf_size);
1734 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Cleaning up\n");
1735 gnutls_x509_crt_deinit (request);
1743 * Accept policy for mhdaemons
1746 * @param addr the sockaddr
1747 * @param addrlen the sockaddr length
1748 * @return MHD_NO if sockaddr is wrong or #conns too high
1751 accept_cb (void* cls, const struct sockaddr *addr, socklen_t addrlen)
1753 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1754 "In MHD accept policy cb\n");
1758 if (addr->sa_family == AF_UNIX)
1762 if (total_mhd_connections >= MAX_MHD_CONNECTIONS)
1765 total_mhd_connections++;
1772 * Adds a socket to an SSL MHD instance
1773 * It is important the the domain name is
1774 * correct. In most cases we need to start a new daemon
1776 * @param h the handle to add to a daemon
1777 * @praram domain the domain the ssl daemon has to serve
1778 * @return MHD_YES on success
1781 add_handle_to_ssl_mhd (struct GNUNET_NETWORK_Handle *h, char* domain)
1783 struct MhdHttpList *hd = NULL;
1784 struct ProxyGNSCertificate *pgc;
1786 for (hd = mhd_httpd_head; hd != NULL; hd = hd->next)
1788 if (0 == strcmp (hd->domain, domain))
1795 pgc = generate_gns_certificate (domain);
1797 hd = GNUNET_malloc (sizeof (struct MhdHttpList));
1798 hd->is_ssl = GNUNET_YES;
1799 strcpy (hd->domain, domain);
1800 hd->proxy_cert = pgc;
1803 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1804 "No previous SSL instance found... starting new one for %s\n",
1807 hd->daemon = MHD_start_daemon (MHD_USE_DEBUG | MHD_USE_SSL, 4444,
1809 &create_response, hd,
1810 MHD_OPTION_LISTEN_SOCKET, GNUNET_NETWORK_get_fd (mhd_unix_socket),
1811 MHD_OPTION_CONNECTION_LIMIT, (unsigned int) 128,
1812 MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) 16,
1813 MHD_OPTION_NOTIFY_COMPLETED,
1815 MHD_OPTION_HTTPS_MEM_KEY, pgc->key,
1816 MHD_OPTION_HTTPS_MEM_CERT, pgc->cert,
1819 GNUNET_assert (hd->daemon != NULL);
1820 hd->httpd_task = GNUNET_SCHEDULER_NO_TASK;
1822 GNUNET_CONTAINER_DLL_insert (mhd_httpd_head, mhd_httpd_tail, hd);
1825 return add_handle_to_mhd (h, hd->daemon);
1831 * Read data from incoming connection
1833 * @param cls the closure
1834 * @param tc the scheduler context
1837 do_read (void* cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1839 struct Socks5Request *s5r = cls;
1840 struct socks5_client_hello *c_hello;
1841 struct socks5_server_hello *s_hello;
1842 struct socks5_client_request *c_req;
1843 struct socks5_server_response *s_resp;
1849 struct hostent *phost;
1851 struct sockaddr_in remote_addr;
1852 struct in_addr *r_sin_addr;
1854 s5r->rtask = GNUNET_SCHEDULER_NO_TASK;
1856 if ((NULL != tc->write_ready) &&
1857 (GNUNET_NETWORK_fdset_isset (tc->read_ready, s5r->sock)) &&
1858 (s5r->rbuf_len = GNUNET_NETWORK_socket_recv (s5r->sock, s5r->rbuf,
1859 sizeof (s5r->rbuf))))
1861 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1862 "Successfully read %d bytes from socket\n",
1867 if (s5r->rbuf_len != 0)
1868 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "read");
1870 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client disco!\n");
1872 if (s5r->fwdrtask != GNUNET_SCHEDULER_NO_TASK)
1873 GNUNET_SCHEDULER_cancel (s5r->fwdrtask);
1874 if (s5r->wtask != GNUNET_SCHEDULER_NO_TASK)
1875 GNUNET_SCHEDULER_cancel (s5r->wtask);
1876 if (s5r->fwdwtask != GNUNET_SCHEDULER_NO_TASK)
1877 GNUNET_SCHEDULER_cancel (s5r->fwdwtask);
1878 GNUNET_NETWORK_socket_close (s5r->remote_sock);
1879 GNUNET_NETWORK_socket_close (s5r->sock);
1884 if (s5r->state == SOCKS5_INIT)
1886 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1888 c_hello = (struct socks5_client_hello*)&s5r->rbuf;
1890 GNUNET_assert (c_hello->version == SOCKS_VERSION_5);
1892 s_hello = (struct socks5_server_hello*)&s5r->wbuf;
1893 s5r->wbuf_len = sizeof( struct socks5_server_hello );
1895 s_hello->version = c_hello->version;
1896 s_hello->auth_method = SOCKS_AUTH_NONE;
1898 /* Write response to client */
1899 s5r->wtask = GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
1903 s5r->rtask = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
1907 s5r->state = SOCKS5_REQUEST;
1911 if (s5r->state == SOCKS5_REQUEST)
1913 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1914 "Processing SOCKS5 request\n");
1915 c_req = (struct socks5_client_request*)&s5r->rbuf;
1916 s_resp = (struct socks5_server_response*)&s5r->wbuf;
1917 //Only 10byte for ipv4 response!
1918 s5r->wbuf_len = 10;//sizeof (struct socks5_server_response);
1920 GNUNET_assert (c_req->addr_type == 3);
1922 dom_len = *((uint8_t*)(&(c_req->addr_type) + 1));
1923 memset(domain, 0, sizeof(domain));
1924 strncpy(domain, (char*)(&(c_req->addr_type) + 2), dom_len);
1925 req_port = *((uint16_t*)(&(c_req->addr_type) + 2 + dom_len));
1927 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1928 "Requested connection is %s:%d\n",
1932 if (is_tld (domain, GNUNET_GNS_TLD) ||
1933 is_tld (domain, GNUNET_GNS_TLD_ZKEY))
1935 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1936 "Requested connection is gnunet tld\n",
1940 if (ntohs(req_port) == HTTPS_PORT)
1942 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1943 "Requested connection is HTTPS\n");
1944 ret = add_handle_to_ssl_mhd ( s5r->sock, domain );
1946 else if (NULL != httpd)
1948 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1949 "Requested connection is HTTP\n");
1950 ret = add_handle_to_mhd ( s5r->sock, httpd );
1955 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1956 _("Failed to start HTTP server\n"));
1957 s_resp->version = 0x05;
1958 s_resp->reply = 0x01;
1959 s5r->cleanup = GNUNET_YES;
1960 s5r->cleanup_sock = GNUNET_YES;
1962 GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
1968 /* Signal success */
1969 s_resp->version = 0x05;
1970 s_resp->reply = 0x00;
1971 s_resp->reserved = 0x00;
1972 s_resp->addr_type = 0x01;
1974 s5r->cleanup = GNUNET_YES;
1975 s5r->cleanup_sock = GNUNET_NO;
1977 GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
1985 phost = (struct hostent*)gethostbyname (domain);
1988 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1989 "Resolve %s error!\n", domain );
1990 s_resp->version = 0x05;
1991 s_resp->reply = 0x01;
1992 s5r->cleanup = GNUNET_YES;
1993 s5r->cleanup_sock = GNUNET_YES;
1995 GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
2001 s5r->remote_sock = GNUNET_NETWORK_socket_create (AF_INET,
2004 r_sin_addr = (struct in_addr*)(phost->h_addr);
2005 remote_ip = r_sin_addr->s_addr;
2006 memset(&remote_addr, 0, sizeof(remote_addr));
2007 remote_addr.sin_family = AF_INET;
2008 #if HAVE_SOCKADDR_IN_SIN_LEN
2009 remote_addr.sin_len = sizeof (remote_addr);
2011 remote_addr.sin_addr.s_addr = remote_ip;
2012 remote_addr.sin_port = req_port;
2014 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2015 "target server: %s:%u\n", inet_ntoa(remote_addr.sin_addr),
2019 GNUNET_NETWORK_socket_connect ( s5r->remote_sock,
2020 (const struct sockaddr*)&remote_addr,
2021 sizeof (remote_addr)))
2022 && (errno != EINPROGRESS))
2024 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "connect");
2025 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2026 "socket request error...\n");
2027 s_resp->version = 0x05;
2028 s_resp->reply = 0x01;
2030 GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
2037 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2038 "new remote connection\n");
2040 s_resp->version = 0x05;
2041 s_resp->reply = 0x00;
2042 s_resp->reserved = 0x00;
2043 s_resp->addr_type = 0x01;
2045 s5r->state = SOCKS5_DATA_TRANSFER;
2048 GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
2052 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
2060 if (s5r->state == SOCKS5_DATA_TRANSFER)
2062 if ((s5r->remote_sock == NULL) || (s5r->rbuf_len == 0))
2064 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2065 "Closing connection to client\n");
2066 if (s5r->rtask != GNUNET_SCHEDULER_NO_TASK)
2067 GNUNET_SCHEDULER_cancel (s5r->rtask);
2068 if (s5r->fwdwtask != GNUNET_SCHEDULER_NO_TASK)
2069 GNUNET_SCHEDULER_cancel (s5r->fwdwtask);
2070 if (s5r->fwdrtask != GNUNET_SCHEDULER_NO_TASK)
2071 GNUNET_SCHEDULER_cancel (s5r->fwdrtask);
2072 if (s5r->fwdrtask != GNUNET_SCHEDULER_NO_TASK)
2073 GNUNET_SCHEDULER_cancel (s5r->fwdrtask);
2075 if (s5r->remote_sock != NULL)
2076 GNUNET_NETWORK_socket_close (s5r->remote_sock);
2077 GNUNET_NETWORK_socket_close (s5r->sock);
2082 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2083 "forwarding %d bytes from client\n", s5r->rbuf_len);
2086 GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
2088 &do_write_remote, s5r);
2090 if (s5r->fwdrtask == GNUNET_SCHEDULER_NO_TASK)
2093 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
2095 &do_read_remote, s5r);
2101 //GNUNET_CONTAINER_DLL_remove (s5conns.head, s5conns.tail, s5r);
2107 * Accept new incoming connections
2109 * @param cls the closure
2110 * @param tc the scheduler context
2113 do_accept (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2115 struct GNUNET_NETWORK_Handle *s;
2116 struct Socks5Request *s5r;
2118 ltask = GNUNET_SCHEDULER_NO_TASK;
2119 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
2122 ltask = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
2126 s = GNUNET_NETWORK_socket_accept (lsock, NULL, NULL);
2130 GNUNET_log_strerror (GNUNET_ERROR_TYPE_INFO, "accept");
2134 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2135 "Got an inbound connection, waiting for data\n");
2137 s5r = GNUNET_malloc (sizeof (struct Socks5Request));
2139 s5r->state = SOCKS5_INIT;
2140 s5r->wtask = GNUNET_SCHEDULER_NO_TASK;
2141 s5r->fwdwtask = GNUNET_SCHEDULER_NO_TASK;
2142 s5r->fwdrtask = GNUNET_SCHEDULER_NO_TASK;
2143 s5r->rtask = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
2146 //GNUNET_CONTAINER_DLL_insert (s5conns.head, s5conns.tail, s5r);
2151 * Task run on shutdown
2153 * @param cls closure
2154 * @param tc task context
2157 do_shutdown (void *cls,
2158 const struct GNUNET_SCHEDULER_TaskContext *tc)
2161 struct MhdHttpList *hd;
2162 struct MhdHttpList *tmp_hd;
2163 struct ProxyCurlTask *ctask;
2164 struct ProxyCurlTask *ctask_tmp;
2166 gnutls_global_deinit ();
2168 if (GNUNET_SCHEDULER_NO_TASK != curl_download_task)
2170 GNUNET_SCHEDULER_cancel (curl_download_task);
2171 curl_download_task = GNUNET_SCHEDULER_NO_TASK;
2174 for (hd = mhd_httpd_head; hd != NULL; hd = tmp_hd)
2178 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2179 "Stopping daemon\n");
2181 if (GNUNET_SCHEDULER_NO_TASK != hd->httpd_task)
2183 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2184 "Stopping select task %d\n",
2186 GNUNET_SCHEDULER_cancel (hd->httpd_task);
2187 hd->httpd_task = GNUNET_SCHEDULER_NO_TASK;
2190 if (NULL != hd->daemon)
2192 MHD_stop_daemon (hd->daemon);
2196 if (NULL != hd->proxy_cert)
2198 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2199 "Free certificate\n");
2200 GNUNET_free (hd->proxy_cert);
2206 for (ctask=ctasks_head; ctask != NULL; ctask=ctask_tmp)
2208 ctask_tmp = ctask->next;
2210 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2211 "Cleaning up cURL task\n");
2213 if (ctask->curl != NULL)
2214 curl_easy_cleanup (ctask->curl);
2216 if (NULL != ctask->headers)
2217 curl_slist_free_all (ctask->headers);
2219 GNUNET_free (ctask);
2222 GNUNET_GNS_disconnect (gns_handle);
2227 * Compiles a regex for us
2229 * @param re ptr to re struct
2230 * @param rt the expression to compile
2231 * @return 0 on success
2234 compile_regex (regex_t *re, const char* rt)
2239 status = regcomp (re, rt, REG_EXTENDED|REG_NEWLINE);
2242 regerror (status, re, err, 1024);
2243 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2244 "Regex error compiling '%s': %s\n", rt, err);
2252 * Loads the users local zone key
2254 * @return GNUNET_YES on success
2257 load_local_zone_key (const struct GNUNET_CONFIGURATION_Handle *cfg)
2260 struct GNUNET_CRYPTO_RsaPrivateKey *key = NULL;
2261 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pkey;
2262 struct GNUNET_CRYPTO_ShortHashCode *zone = NULL;
2263 struct GNUNET_CRYPTO_ShortHashAsciiEncoded zonename;
2265 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "gns",
2266 "ZONEKEY", &keyfile))
2268 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2269 "Unable to load zone key config value!\n");
2273 if (GNUNET_NO == GNUNET_DISK_file_test (keyfile))
2275 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2276 "Unable to load zone key %s!\n", keyfile);
2277 GNUNET_free(keyfile);
2281 key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
2282 GNUNET_CRYPTO_rsa_key_get_public (key, &pkey);
2283 GNUNET_CRYPTO_short_hash(&pkey,
2284 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
2286 zone = &local_gns_zone;
2287 GNUNET_CRYPTO_short_hash_to_enc (zone, &zonename);
2288 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2289 "Using zone: %s!\n", &zonename);
2290 GNUNET_CRYPTO_rsa_key_free(key);
2291 GNUNET_free(keyfile);
2297 * Loads the users local shorten zone key
2299 * @return GNUNET_YES on success
2302 load_local_shorten_key (const struct GNUNET_CONFIGURATION_Handle *cfg)
2305 struct GNUNET_CRYPTO_RsaPrivateKey *key = NULL;
2306 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pkey;
2307 struct GNUNET_CRYPTO_ShortHashCode *zone = NULL;
2308 struct GNUNET_CRYPTO_ShortHashAsciiEncoded zonename;
2310 if (GNUNET_NO == GNUNET_CONFIGURATION_get_value_yesno (cfg, "gns",
2311 "AUTO_IMPORT_PKEY"))
2316 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "gns",
2317 "AUTO_IMPORT_ZONEKEY",
2320 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2321 "Unable to load shorten key config value! (not fatal)\n");
2325 if (GNUNET_NO == GNUNET_DISK_file_test (keyfile))
2327 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2328 "Unable to load shorten key %s! (not fatal)\n", keyfile);
2329 GNUNET_free(keyfile);
2333 key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
2334 GNUNET_CRYPTO_rsa_key_get_public (key, &pkey);
2335 GNUNET_CRYPTO_short_hash(&pkey,
2336 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
2337 &local_shorten_zone);
2338 zone = &local_gns_zone;
2339 GNUNET_CRYPTO_short_hash_to_enc (zone, &zonename);
2340 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2341 "Using shorten zone: %s!\n", &zonename);
2342 GNUNET_CRYPTO_rsa_key_free(key);
2343 GNUNET_free(keyfile);
2349 * Main function that will be run
2351 * @param cls closure
2352 * @param args remaining command-line arguments
2353 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
2354 * @param cfg configuration
2357 run (void *cls, char *const *args, const char *cfgfile,
2358 const struct GNUNET_CONFIGURATION_Handle *cfg)
2360 struct sockaddr_in sa;
2361 struct MhdHttpList *hd;
2362 struct sockaddr_un mhd_unix_sock_addr;
2364 char* proxy_sockfile;
2365 char* cafile_cfg = NULL;
2370 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2373 cafile = cafile_opt;
2377 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "gns-proxy",
2381 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2382 "Unable to load proxy CA config value!\n");
2383 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2384 "No proxy CA provided!\n");
2387 cafile = cafile_cfg;
2390 gnutls_global_init ();
2392 gnutls_x509_crt_init (&proxy_ca.cert);
2393 gnutls_x509_privkey_init (&proxy_ca.key);
2395 load_cert_from_file (proxy_ca.cert, cafile);
2396 load_key_from_file (proxy_ca.key, cafile);
2398 GNUNET_free_non_null (cafile_cfg);
2400 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2401 "Loading Template\n");
2403 compile_regex (&re_dotplus, (char*) RE_A_HREF);
2405 gns_handle = GNUNET_GNS_connect (cfg);
2407 if (GNUNET_NO == load_local_zone_key (cfg))
2409 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2410 "Unable to load zone!\n");
2414 use_shorten = load_local_shorten_key (cfg);
2416 if (NULL == gns_handle)
2418 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2419 "Unable to connect to GNS!\n");
2423 memset (&sa, 0, sizeof (sa));
2424 sa.sin_family = AF_INET;
2425 sa.sin_port = htons (port);
2426 #if HAVE_SOCKADDR_IN_SIN_LEN
2427 sa.sin_len = sizeof (sa);
2430 lsock = GNUNET_NETWORK_socket_create (AF_INET,
2434 if ((NULL == lsock) ||
2436 GNUNET_NETWORK_socket_bind (lsock, (const struct sockaddr *) &sa,
2439 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2440 "Failed to create listen socket bound to `%s'",
2441 GNUNET_a2s ((const struct sockaddr *) &sa, sizeof (sa)));
2443 GNUNET_NETWORK_socket_close (lsock);
2447 if (GNUNET_OK != GNUNET_NETWORK_socket_listen (lsock, 5))
2449 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2450 "Failed to listen on socket bound to `%s'",
2451 GNUNET_a2s ((const struct sockaddr *) &sa, sizeof (sa)));
2455 ltask = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
2456 lsock, &do_accept, NULL);
2461 if (0 != curl_global_init (CURL_GLOBAL_WIN32))
2463 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2464 "cURL global init failed!\n");
2468 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2469 "Proxy listens on port %u\n",
2472 mhd_httpd_head = NULL;
2473 mhd_httpd_tail = NULL;
2474 total_mhd_connections = 0;
2476 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "gns-proxy",
2480 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2481 "Specify PROXY_UNIXPATH in gns-proxy config section!\n");
2485 mhd_unix_socket = GNUNET_NETWORK_socket_create (AF_UNIX,
2489 if (NULL == mhd_unix_socket)
2491 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2492 "Unable to create unix domain socket!\n");
2496 mhd_unix_sock_addr.sun_family = AF_UNIX;
2497 strcpy (mhd_unix_sock_addr.sun_path, proxy_sockfile);
2498 unlink (proxy_sockfile);
2499 len = strlen (proxy_sockfile) + sizeof(AF_UNIX);
2501 GNUNET_free (proxy_sockfile);
2503 if (GNUNET_OK != GNUNET_NETWORK_socket_bind (mhd_unix_socket,
2504 (struct sockaddr*)&mhd_unix_sock_addr,
2507 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2508 "Unable to bind unix domain socket!\n");
2512 if (GNUNET_OK != GNUNET_NETWORK_socket_listen (mhd_unix_socket,
2515 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2516 "Unable to listen on unix domain socket!\n");
2520 hd = GNUNET_malloc (sizeof (struct MhdHttpList));
2521 hd->is_ssl = GNUNET_NO;
2522 strcpy (hd->domain, "");
2523 httpd = MHD_start_daemon (MHD_USE_DEBUG, 4444, //Dummy port
2525 &create_response, hd,
2526 MHD_OPTION_LISTEN_SOCKET, GNUNET_NETWORK_get_fd (mhd_unix_socket),
2527 MHD_OPTION_CONNECTION_LIMIT, (unsigned int) 128,
2528 MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) 16,
2529 MHD_OPTION_NOTIFY_COMPLETED,
2533 GNUNET_assert (httpd != NULL);
2535 hd->httpd_task = GNUNET_SCHEDULER_NO_TASK;
2537 GNUNET_CONTAINER_DLL_insert (mhd_httpd_head, mhd_httpd_tail, hd);
2541 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
2542 &do_shutdown, NULL);
2548 * The main function for gnunet-gns-proxy.
2550 * @param argc number of arguments from the command line
2551 * @param argv command line arguments
2552 * @return 0 ok, 1 on error
2555 main (int argc, char *const *argv)
2557 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
2559 gettext_noop ("listen on specified port"), 1,
2560 &GNUNET_GETOPT_set_string, &port},
2561 {'a', "authority", NULL,
2562 gettext_noop ("pem file to use as CA"), 1,
2563 &GNUNET_GETOPT_set_string, &cafile_opt},
2564 GNUNET_GETOPT_OPTION_END
2569 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
2572 GNUNET_log_setup ("gnunet-gns-proxy", "WARNING", NULL);
2575 GNUNET_PROGRAM_run (argc, argv, "gnunet-gns-proxy",
2576 _("GNUnet GNS proxy"),
2578 &run, NULL)) ? 0 : 1;