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"
30 #define GNUNET_GNS_PROXY_PORT 7777
32 /* MHD/cURL defines */
33 #define BUF_WAIT_FOR_CURL 0
34 #define BUF_WAIT_FOR_MHD 1
35 #define HTML_HDR_CONTENT "Content-Type: text/html\r\n"
38 #define RE_DOTPLUS "<a href=\"http://(([A-Za-z]+[.])+)([+])"
39 #define RE_N_MATCHES 4
41 /* The usual suspects */
43 #define HTTPS_PORT 443
48 * A structure for socks requests
52 /* The client socket */
53 struct GNUNET_NETWORK_Handle *sock;
55 /* The server socket */
56 struct GNUNET_NETWORK_Handle *remote_sock;
61 /* Client socket read task */
62 GNUNET_SCHEDULER_TaskIdentifier rtask;
64 /* Server socket read task */
65 GNUNET_SCHEDULER_TaskIdentifier fwdrtask;
67 /* Client socket write task */
68 GNUNET_SCHEDULER_TaskIdentifier wtask;
70 /* Server socket write task */
71 GNUNET_SCHEDULER_TaskIdentifier fwdwtask;
79 /* Length of data in read buffer */
80 unsigned int rbuf_len;
82 /* Length of data in write buffer */
83 unsigned int wbuf_len;
85 /* This handle is scheduled for cleanup? */
88 /* Shall we close the client socket on cleanup? */
94 * A structure for all running Httpds
99 struct MhdHttpList *prev;
102 struct MhdHttpList *next;
104 /* is this an ssl daemon? */
107 /* the domain name to server (only important for SSL) */
110 /* The daemon handle */
111 struct MHD_Daemon *daemon;
114 GNUNET_SCHEDULER_TaskIdentifier httpd_task;
118 * A structure for MHD<->cURL streams
123 struct ProxyCurlTask *prev;
126 struct ProxyCurlTask *next;
131 /* Optional header replacements for curl (LEHO) */
132 struct curl_slist *headers;
134 /* Optional resolver replacements for curl (LEHO) */
135 struct curl_slist *resolver;
137 /* The URL to fetch */
140 /* The cURL write buffer / MHD read buffer */
141 char buffer[CURL_MAX_WRITE_SIZE];
143 /* The pointer to the data in the buffer */
146 /* The buffer status (BUF_WAIT_FOR_CURL or BUF_WAIT_FOR_MHD) */
149 /* Number of bytes in buffer */
150 unsigned int bytes_in_buffer;
152 /* Indicates wheather the download is in progress */
153 int download_in_progress;
155 /* Indicates wheather the download was successful */
156 int download_successful;
158 /* Indicates wheather the download failed */
161 /* Indicates wheather we need to parse HTML */
164 /* Indicates wheather we are postprocessing the HTML right now */
165 int is_postprocessing;
167 /* Indicates wheather postprocessing has finished */
170 /* Task ID of the postprocessing task */
171 GNUNET_SCHEDULER_TaskIdentifier pp_task;
173 /* The postprocessing buffer TODO length? */
176 /* The authority of the corresponding host (site of origin) */
179 /* The hostname (Host header field) */
182 /* The LEgacy HOstname (can be empty) */
185 /* The associated daemon list entry */
186 struct MhdHttpList *mhd;
190 /* The port the proxy is running on (default 7777) */
191 unsigned long port = GNUNET_GNS_PROXY_PORT;
193 /* The listen socket of the proxy */
194 static struct GNUNET_NETWORK_Handle *lsock;
196 /* The listen task ID */
197 GNUNET_SCHEDULER_TaskIdentifier ltask;
199 /* The cURL download task */
200 GNUNET_SCHEDULER_TaskIdentifier curl_download_task;
202 /* The non SSL httpd daemon handle */
203 static struct MHD_Daemon *httpd;
205 /* The cURL multi handle */
206 static CURLM *curl_multi;
208 /* Handle to the GNS service */
209 static struct GNUNET_GNS_Handle *gns_handle;
211 /* DLL for ProxyCurlTasks */
212 static struct ProxyCurlTask *ctasks_head;
214 /* DLL for ProxyCurlTasks */
215 static struct ProxyCurlTask *ctasks_tail;
217 /* DLL for http daemons */
218 static struct MhdHttpList *mhd_httpd_head;
220 /* DLL for http daemons */
221 static struct MhdHttpList *mhd_httpd_tail;
223 /* Handle to the regex for dotplus (.+) replacement in HTML */
224 static regex_t re_dotplus;
226 /* The users local GNS zone hash */
227 struct GNUNET_CRYPTO_ShortHashCode local_gns_zone;
230 * Checks if name is in tld
232 * @param name the name to check
233 * @param tld the TLD to check for
234 * @return GNUNET_YES or GNUNET_NO
237 is_tld(const char* name, const char* tld)
241 if (strlen(name) <= strlen(tld))
246 offset = strlen(name)-strlen(tld);
247 if (strcmp (name+offset, tld) != 0)
249 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
250 "%s is not in .%s TLD\n", name, tld);
259 * Read HTTP request header field 'Host'
261 * @param cls buffer to write to
262 * @param kind value kind
263 * @param key field key
264 * @param value field value
265 * @return MHD_NO when Host found
268 con_val_iter (void *cls,
269 enum MHD_ValueKind kind,
273 char* buf = (char*)cls;
275 if (0 == strcmp ("Host", key))
285 * Check HTTP response header for mime
287 * @param buffer curl buffer
288 * @param size curl blocksize
289 * @param nmemb curl blocknumber
291 * @return size of read bytes
294 curl_check_hdr (void *buffer, size_t size, size_t nmemb, void *cls)
296 size_t bytes = size * nmemb;
297 struct ProxyCurlTask *ctask = cls;
300 memcpy (hdr, buffer, bytes);
303 if (0 == strcmp (hdr, HTML_HDR_CONTENT))
305 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
306 "Got HTML HTTP response header\n");
307 ctask->parse_content = GNUNET_YES;
316 * @param hd a http daemon list entry
319 run_httpd (struct MhdHttpList *hd);
331 * Task that simply runs MHD main loop
334 * @param tc task context
337 run_mhd (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
340 struct MhdHttpList *hd = cls;
342 //for (hd=mhd_httpd_head; hd != NULL; hd = hd->next)
343 MHD_run (hd->daemon);
348 * Process cURL download bits
350 * @param ptr buffer with data
351 * @param size size of a record
352 * @param nmemb number of records downloaded
354 * @return number of processed bytes
357 callback_download (void *ptr, size_t size, size_t nmemb, void *ctx)
359 const char *cbuf = ptr;
361 struct ProxyCurlTask *ctask = ctx;
372 if (total > sizeof (ctask->buffer))
374 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
375 "CURL gave us too much data to handle (%d)!\n",
380 if (ctask->buf_status == BUF_WAIT_FOR_MHD)
382 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
383 "CURL: Waiting for MHD (%s)\n", ctask->url);
384 return CURL_WRITEFUNC_PAUSE;
388 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
389 "CURL: Copying to MHD (%s, %d)\n", ctask->url, total);
390 memcpy (ctask->buffer, cbuf, total);
391 ctask->bytes_in_buffer = total;
392 ctask->buffer_ptr = ctask->buffer;
394 ctask->buf_status = BUF_WAIT_FOR_MHD;
396 //GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
397 // "cURL chunk:\n%s\n", (char*)ctask->buffer);
398 //run_mhd (NULL, NULL);
399 GNUNET_SCHEDULER_add_now (&run_mhd, ctask->mhd);
406 * Callback to free content
408 * @param cls content to free
411 mhd_content_free (void *cls)
413 struct ProxyCurlTask *ctask = cls;
415 if (NULL != ctask->headers)
416 curl_slist_free_all (ctask->headers);
418 if (NULL != ctask->curl)
419 curl_easy_cleanup (ctask->curl);
429 * Shorten result callback
431 * @param cls the proxycurltask
432 * @param short_name the shortened name (NULL on error)
435 process_shorten (void* cls, const char* short_name)
437 struct ProxyCurlTask *ctask = cls;
439 char tmp[strlen(ctask->pp_buf)]; //TODO length
441 if (NULL == short_name)
443 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
444 "MHD PP: Unable to shorten %s\n",
449 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
450 "MHD PP: Shorten %s -> %s\n",
454 sprintf (tmp, "<a href=http://%s", short_name);
455 strcpy (ctask->pp_buf, tmp);
457 ctask->pp_finished = GNUNET_YES;
459 GNUNET_SCHEDULER_add_now (&run_mhd, ctask->mhd);
464 * Postprocessing task that uses GNS to shorten names
466 * @param cls the proxycurltask
467 * @param tc the task context
470 postprocess_name (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
472 struct ProxyCurlTask *ctask = cls;
473 char tmp[strlen(ctask->pp_buf)];
475 sprintf ( tmp, "%s%s", ctask->pp_buf, ctask->authority);
477 GNUNET_GNS_shorten (gns_handle,
485 * Callback for MHD response
488 * @param pos in buffer
490 * @param max space in buffer
493 mhd_content_cb (void *cls,
498 struct ProxyCurlTask *ctask = cls;
500 size_t bytes_to_copy;
503 regmatch_t m[RE_N_MATCHES];
505 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
506 "MHD: content cb\n");
508 if (ctask->download_successful &&
509 (ctask->buf_status == BUF_WAIT_FOR_CURL))
511 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
512 "MHD: sending response for %s\n", ctask->url);
513 ctask->download_in_progress = GNUNET_NO;
514 curl_multi_remove_handle (curl_multi, ctask->curl);
515 curl_easy_cleanup (ctask->curl);
516 GNUNET_SCHEDULER_add_now (&run_mhd, ctask->mhd);
517 return MHD_CONTENT_READER_END_OF_STREAM;
520 if (ctask->download_error &&
521 (ctask->buf_status == BUF_WAIT_FOR_CURL))
523 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
524 "MHD: sending error response\n");
525 ctask->download_in_progress = GNUNET_NO;
526 curl_multi_remove_handle (curl_multi, ctask->curl);
527 curl_easy_cleanup (ctask->curl);
528 GNUNET_SCHEDULER_add_now (&run_mhd, ctask->mhd);
529 return MHD_CONTENT_READER_END_WITH_ERROR;
532 if ( ctask->buf_status == BUF_WAIT_FOR_CURL )
535 bytes_to_copy = ctask->bytes_in_buffer;
537 if (ctask->parse_content == GNUNET_YES)
540 GNUNET_log ( GNUNET_ERROR_TYPE_DEBUG,
541 "MHD: We need to parse the HTML %s\n", ctask->buffer_ptr);
543 nomatch = regexec ( &re_dotplus, ctask->buffer_ptr, RE_N_MATCHES, m, 0);
547 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
550 GNUNET_assert (m[1].rm_so != -1);
552 hostptr = ctask->buffer_ptr+m[1].rm_so;
556 bytes_to_copy = m[0].rm_so;
557 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
558 "Copying %d bytes.\n", m[0].rm_so);
564 if (ctask->is_postprocessing == GNUNET_YES)
568 if ( ctask->pp_finished == GNUNET_NO )
570 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
571 "MHD PP: Waiting for PP of %s\n", ctask->pp_buf);
575 ctask->is_postprocessing = GNUNET_NO;
577 ctask->bytes_in_buffer -= m[0].rm_eo;//(m[1].rm_eo-m[1].rm_so);
578 ctask->buffer_ptr += m[0].rm_eo;//(m[1].rm_eo-m[1].rm_so);
579 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
580 "Skipping next %d bytes in buffer\n", m[0].rm_eo);
582 GNUNET_SCHEDULER_add_now (&run_mhd, ctask->mhd);
584 if ( strlen (ctask->pp_buf) <= max )
586 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
587 "Copying postprocessed %s.\n", ctask->pp_buf);
588 memcpy ( buf, ctask->pp_buf, strlen (ctask->pp_buf) );
589 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
591 ctask->is_postprocessing = GNUNET_NO;
592 return strlen (ctask->pp_buf);
598 memset (ctask->pp_buf, 0, sizeof(ctask->pp_buf));
599 memcpy (ctask->pp_buf, hostptr, (m[1].rm_eo-m[1].rm_so));
601 ctask->is_postprocessing = GNUNET_YES;
602 ctask->pp_finished = GNUNET_NO;
604 //postprocess_name(ctask, NULL);
605 ctask->pp_task = GNUNET_SCHEDULER_add_now (&postprocess_name, ctask);
612 if ( bytes_to_copy > max )
614 GNUNET_log ( GNUNET_ERROR_TYPE_DEBUG,
615 "MHD: buffer in response too small! (%s)\n",
617 memcpy ( buf, ctask->buffer_ptr, max);
618 ctask->bytes_in_buffer -= max;
619 ctask->buffer_ptr += max;
624 GNUNET_log ( GNUNET_ERROR_TYPE_DEBUG,
625 "MHD: copying %d bytes to mhd response at offset %d\n",
628 memcpy ( buf, ctask->buffer_ptr, bytes_to_copy );
629 copied = bytes_to_copy;
630 if (bytes_to_copy < ctask->bytes_in_buffer)
632 ctask->bytes_in_buffer -= bytes_to_copy;
633 ctask->buffer_ptr += bytes_to_copy;
637 ctask->bytes_in_buffer = 0;
638 ctask->buf_status = BUF_WAIT_FOR_CURL;
639 ctask->buffer_ptr = ctask->buffer;
640 curl_easy_pause (ctask->curl, CURLPAUSE_CONT);
641 GNUNET_SCHEDULER_add_now (&run_mhd, ctask->mhd);
645 GNUNET_SCHEDULER_add_now (&run_mhd, ctask->mhd);
653 * Task that is run when we are ready to receive more data
657 * @param tc task context
660 curl_task_download (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
663 * Ask cURL for the select sets and schedule download
666 curl_download_prepare ()
673 struct GNUNET_NETWORK_FDSet *grs;
674 struct GNUNET_NETWORK_FDSet *gws;
676 struct GNUNET_TIME_Relative rtime;
682 mret = curl_multi_fdset (curl_multi, &rs, &ws, &es, &max);
684 if (mret != CURLM_OK)
686 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
687 "%s failed at %s:%d: `%s'\n",
688 "curl_multi_fdset", __FILE__, __LINE__,
689 curl_multi_strerror (mret));
694 mret = curl_multi_timeout (curl_multi, &to);
695 rtime = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, to);
697 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
698 "cURL multi fds: max=%d timeout=%llu\n", max, to);
700 grs = GNUNET_NETWORK_fdset_create ();
701 gws = GNUNET_NETWORK_fdset_create ();
702 GNUNET_NETWORK_fdset_copy_native (grs, &rs, max + 1);
703 GNUNET_NETWORK_fdset_copy_native (gws, &ws, max + 1);
704 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
705 "Scheduling task cURL\n");
707 if (curl_download_task != GNUNET_SCHEDULER_NO_TASK)
708 GNUNET_SCHEDULER_cancel (curl_download_task);
711 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
714 &curl_task_download, curl_multi);
715 GNUNET_NETWORK_fdset_destroy (gws);
716 GNUNET_NETWORK_fdset_destroy (grs);
722 * Task that is run when we are ready to receive more data
726 * @param tc task context
729 curl_task_download (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
735 struct ProxyCurlTask *ctask;
738 curl_download_task = GNUNET_SCHEDULER_NO_TASK;
740 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
742 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
743 "Shutdown requested while trying to download\n");
747 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
755 mret = curl_multi_perform (curl_multi, &running);
757 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
758 "Running curl tasks: %d\n", running);
761 for (; ctask != NULL; ctask = ctask->next)
763 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
764 "CTask: %s\n", ctask->url);
768 if (num_ctasks != running)
770 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
771 "%d ctasks, %d curl running\n", num_ctasks, running);
777 msg = curl_multi_info_read (curl_multi, &msgnum);
778 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
779 "Messages left: %d\n", msgnum);
786 if ((msg->data.result != CURLE_OK) &&
787 (msg->data.result != CURLE_GOT_NOTHING))
789 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
790 "Download curl failed");
792 for (; ctask != NULL; ctask = ctask->next)
794 if (memcmp (msg->easy_handle, ctask->curl, sizeof (CURL)) != 0)
797 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
798 "Download curl failed for task %s: %s.\n",
800 curl_easy_strerror (msg->data.result));
801 ctask->download_successful = GNUNET_NO;
802 ctask->download_error = GNUNET_YES;
803 //curl_multi_remove_handle (curl_multi, ctask->curl);
804 //curl_easy_cleanup (ctask->curl);
805 GNUNET_CONTAINER_DLL_remove (ctasks_head, ctasks_tail,
809 GNUNET_assert (ctask != NULL);
813 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
814 "cURL download completed.\n");
816 for (; ctask != NULL; ctask = ctask->next)
818 if (memcmp (msg->easy_handle, ctask->curl, sizeof (CURL)) != 0)
821 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
822 "cURL task %s found.\n", ctask->url);
823 ctask->download_successful = GNUNET_YES;
824 //curl_multi_remove_handle (curl_multi, ctask->curl);
825 //curl_easy_cleanup (ctask->curl);
826 GNUNET_CONTAINER_DLL_remove (ctasks_head, ctasks_tail,
830 GNUNET_assert (ctask != NULL);
832 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
833 "curl end %s\n", curl_easy_strerror(msg->data.result));
839 } while (msgnum > 0);
842 for (ctask=ctasks_head; ctask != NULL; ctask = ctask->next)
847 if (num_ctasks != running)
849 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
850 "%d ctasks, %d curl running\n", num_ctasks, running);
853 GNUNET_assert ( num_ctasks == running );
857 } while (mret == CURLM_CALL_MULTI_PERFORM);
860 if (mret != CURLM_OK)
862 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "%s failed at %s:%d: `%s'\n",
863 "curl_multi_perform", __FILE__, __LINE__,
864 curl_multi_strerror (mret));
866 curl_download_prepare();
870 * Process LEHO lookup
872 * @param cls the ctask
873 * @param rd_count number of records returned
874 * @param rd record data
877 process_leho_lookup (void *cls,
879 const struct GNUNET_NAMESTORE_RecordData *rd)
881 struct ProxyCurlTask *ctask = cls;
882 char hosthdr[262]; //256 + "Host: "
886 struct hostent *phost;
888 char resolvename[512];
891 ctask->headers = NULL;
893 strcpy (ctask->leho, "");
895 for (i=0; i<rd_count; i++)
897 if (rd[i].record_type != GNUNET_GNS_RECORD_LEHO)
900 memcpy (ctask->leho, rd[i].data, rd[i].data_size);
902 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
903 "Found LEHO %s for %s\n", ctask->leho, ctask->url);
906 if (0 != strcmp (ctask->leho, ""))
908 sprintf (hosthdr, "%s%s", "Host: ", ctask->leho);
909 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
910 "New HTTP header value: %s\n", hosthdr);
911 ctask->headers = curl_slist_append (ctask->headers, hosthdr);
912 GNUNET_assert (NULL != ctask->headers);
913 ret = curl_easy_setopt (ctask->curl, CURLOPT_HTTPHEADER, ctask->headers);
916 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "%s failed at %s:%d: `%s'\n",
917 "curl_easy_setopt", __FILE__, __LINE__, curl_easy_strerror(ret));
922 if (ctask->mhd->is_ssl)
924 phost = (struct hostent*)gethostbyname (ctask->host);
925 ssl_ip = inet_ntoa(*((struct in_addr*)(phost->h_addr)));
926 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
927 "SSL target server: %s\n", ssl_ip);
928 sprintf (resolvename, "%s:%d:%s", ctask->leho, HTTPS_PORT, ssl_ip);
929 ctask->resolver = curl_slist_append ( ctask->resolver, resolvename);
930 curl_easy_setopt (ctask->curl, CURLOPT_RESOLVE, ctask->resolver);
931 sprintf (curlurl, "https://%s%s", ctask->leho, ctask->url);
932 curl_easy_setopt (ctask->curl, CURLOPT_URL, curlurl);
935 if (CURLM_OK != (mret=curl_multi_add_handle (curl_multi, ctask->curl)))
937 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
938 "%s failed at %s:%d: `%s'\n",
939 "curl_multi_add_handle", __FILE__, __LINE__,
940 curl_multi_strerror (mret));
941 ctask->download_successful = GNUNET_NO;
942 ctask->download_error = GNUNET_YES;
945 GNUNET_CONTAINER_DLL_insert (ctasks_head, ctasks_tail, ctask);
947 curl_download_prepare ();
952 * Initialize download and trigger curl
954 * @param cls the proxycurltask
955 * @param auth_name the name of the authority (site of origin) of ctask->host
959 process_get_authority (void *cls,
960 const char* auth_name)
962 struct ProxyCurlTask *ctask = cls;
964 if (NULL == auth_name)
966 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
967 "Get authority failed!\n");
968 strcpy (ctask->authority, "");
971 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
972 "Get authority yielded %s\n", auth_name);
973 strcpy (ctask->authority, auth_name);
975 GNUNET_GNS_lookup_zone (gns_handle,
978 GNUNET_GNS_RECORD_LEHO,
979 &process_leho_lookup,
984 * Main MHD callback for handling requests.
987 * @param con MHD connection handle
988 * @param meth the HTTP method used ("GET", "PUT", etc.)
989 * @param ver the HTTP version string (i.e. "HTTP/1.1")
990 * @param upload_data the data being uploaded (excluding HEADERS,
991 * for a POST that fits into memory and that is encoded
992 * with a supported encoding, the POST data will NOT be
993 * given in upload_data and is instead available as
994 * part of MHD_get_connection_values; very large POST
995 * data *will* be made available incrementally in
997 * @param upload_data_size set initially to the size of the
998 * upload_data provided; the method must update this
999 * value to the number of bytes NOT processed;
1000 * @param con_cls pointer to location where we store the 'struct Request'
1001 * @return MHD_YES if the connection was handled successfully,
1002 * MHD_NO if the socket must be closed due to a serious
1003 * error while handling the request
1006 create_response (void *cls,
1007 struct MHD_Connection *con,
1011 const char *upload_data,
1012 size_t *upload_data_size,
1016 struct MhdHttpList* hd = cls;
1017 const char* page = "<html><head><title>gnoxy</title>"\
1018 "</head><body>cURL fail</body></html>";
1019 struct MHD_Response *response;
1024 struct hostent *phost;
1027 struct ProxyCurlTask *ctask;
1029 if (0 != strcmp (meth, "GET"))
1031 if (&dummy != *con_cls)
1037 if (0 != *upload_data_size)
1042 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1045 MHD_get_connection_values (con,
1047 &con_val_iter, host);
1051 ctask = GNUNET_malloc (sizeof (struct ProxyCurlTask));
1053 ctask->curl = curl_easy_init();
1055 if (curl_multi == NULL)
1056 curl_multi = curl_multi_init ();
1058 if ((ctask->curl == NULL) || (curl_multi == NULL))
1060 response = MHD_create_response_from_buffer (strlen (page),
1062 MHD_RESPMEM_PERSISTENT);
1063 ret = MHD_queue_response (con,
1066 MHD_destroy_response (response);
1067 GNUNET_free (ctask);
1071 ctask->download_in_progress = GNUNET_YES;
1072 ctask->download_successful = GNUNET_NO;
1073 ctask->buf_status = BUF_WAIT_FOR_CURL;
1074 ctask->bytes_in_buffer = 0;
1075 ctask->parse_content = GNUNET_NO;
1077 curl_easy_setopt (ctask->curl, CURLOPT_HEADERFUNCTION, &curl_check_hdr);
1078 curl_easy_setopt (ctask->curl, CURLOPT_HEADERDATA, ctask);
1079 curl_easy_setopt (ctask->curl, CURLOPT_WRITEFUNCTION, &callback_download);
1080 curl_easy_setopt (ctask->curl, CURLOPT_WRITEDATA, ctask);
1081 curl_easy_setopt (ctask->curl, CURLOPT_FOLLOWLOCATION, 1);
1082 curl_easy_setopt (ctask->curl, CURLOPT_MAXREDIRS, 4);
1083 /* no need to abort if the above failed */
1084 if (GNUNET_NO == ctask->mhd->is_ssl)
1085 sprintf (curlurl, "http://%s%s", host, url);
1086 strcpy (ctask->host, host);
1087 strcpy (ctask->url, url);
1088 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1089 "Adding new curl task for %s\n", curlurl);
1091 curl_easy_setopt (ctask->curl, CURLOPT_URL, curlurl);
1092 curl_easy_setopt (ctask->curl, CURLOPT_FAILONERROR, 1);
1093 curl_easy_setopt (ctask->curl, CURLOPT_CONNECTTIMEOUT, 600L);
1094 curl_easy_setopt (ctask->curl, CURLOPT_TIMEOUT, 600L);
1096 GNUNET_GNS_get_authority (gns_handle,
1098 &process_get_authority,
1100 //download_prepare (ctask);
1101 //curl_download_prepare ();
1103 response = MHD_create_response_from_callback (-1, -1,
1108 ret = MHD_queue_response (con, MHD_HTTP_OK, response);
1110 //MHD_destroy_response (response);
1116 * Task run whenever HTTP server operations are pending.
1119 * @param tc sched context
1122 do_httpd (void *cls,
1123 const struct GNUNET_SCHEDULER_TaskContext *tc);
1132 struct MhdHttpList *hd;
1134 for (hd=mhd_httpd_head; hd != NULL; hd = hd->next)
1143 run_httpd (struct MhdHttpList *hd)
1148 struct GNUNET_NETWORK_FDSet *wrs;
1149 struct GNUNET_NETWORK_FDSet *wws;
1150 struct GNUNET_NETWORK_FDSet *wes;
1153 unsigned MHD_LONG_LONG timeout;
1154 struct GNUNET_TIME_Relative tv;
1159 wrs = GNUNET_NETWORK_fdset_create ();
1160 wes = GNUNET_NETWORK_fdset_create ();
1161 wws = GNUNET_NETWORK_fdset_create ();
1163 GNUNET_assert (MHD_YES == MHD_get_fdset (hd->daemon, &rs, &ws, &es, &max));
1166 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1167 "MHD fds: max=%d\n", max);
1169 haveto = MHD_get_timeout (hd->daemon, &timeout);
1171 if (haveto == MHD_YES)
1172 tv.rel_value = (uint64_t) timeout;
1174 tv = GNUNET_TIME_UNIT_FOREVER_REL;
1175 GNUNET_NETWORK_fdset_copy_native (wrs, &rs, max + 1);
1176 GNUNET_NETWORK_fdset_copy_native (wws, &ws, max + 1);
1177 GNUNET_NETWORK_fdset_copy_native (wes, &es, max + 1);
1179 if (hd->httpd_task != GNUNET_SCHEDULER_NO_TASK)
1180 GNUNET_SCHEDULER_cancel (hd->httpd_task);
1182 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_HIGH,
1185 GNUNET_NETWORK_fdset_destroy (wrs);
1186 GNUNET_NETWORK_fdset_destroy (wws);
1187 GNUNET_NETWORK_fdset_destroy (wes);
1192 * Task run whenever HTTP server operations are pending.
1195 * @param tc sched context
1198 do_httpd (void *cls,
1199 const struct GNUNET_SCHEDULER_TaskContext *tc)
1201 struct MhdHttpList *hd = cls;
1203 hd->httpd_task = GNUNET_SCHEDULER_NO_TASK;
1205 MHD_run (hd->daemon);
1211 * Read data from socket
1213 * @param cls the closure
1214 * @param tc scheduler context
1217 do_read (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
1220 * Read from remote end
1222 * @param cls closure
1223 * @param tc scheduler context
1226 do_read_remote (void* cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
1229 * Write data to remote socket
1231 * @param cls the closure
1232 * @param tc scheduler context
1235 do_write_remote (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1237 struct Socks5Request *s5r = cls;
1240 s5r->fwdwtask = GNUNET_SCHEDULER_NO_TASK;
1242 if ((NULL != tc->read_ready) &&
1243 (GNUNET_NETWORK_fdset_isset (tc->write_ready, s5r->remote_sock)) &&
1244 ((len = GNUNET_NETWORK_socket_send (s5r->remote_sock, s5r->rbuf,
1247 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1248 "Successfully sent %d bytes to remote socket\n",
1253 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "write remote");
1255 if (s5r->rtask != GNUNET_SCHEDULER_NO_TASK)
1256 GNUNET_SCHEDULER_cancel (s5r->rtask);
1257 if (s5r->wtask != GNUNET_SCHEDULER_NO_TASK)
1258 GNUNET_SCHEDULER_cancel (s5r->wtask);
1259 if (s5r->fwdrtask != GNUNET_SCHEDULER_NO_TASK)
1260 GNUNET_SCHEDULER_cancel (s5r->fwdrtask);
1261 GNUNET_NETWORK_socket_close (s5r->remote_sock);
1262 GNUNET_NETWORK_socket_close (s5r->sock);
1268 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
1275 * Clean up s5r handles
1277 * @param s5r the handle to destroy
1280 cleanup_s5r (struct Socks5Request *s5r)
1282 if (s5r->rtask != GNUNET_SCHEDULER_NO_TASK)
1283 GNUNET_SCHEDULER_cancel (s5r->rtask);
1284 if (s5r->fwdwtask != GNUNET_SCHEDULER_NO_TASK)
1285 GNUNET_SCHEDULER_cancel (s5r->fwdwtask);
1286 if (s5r->fwdrtask != GNUNET_SCHEDULER_NO_TASK)
1287 GNUNET_SCHEDULER_cancel (s5r->fwdrtask);
1289 if (NULL != s5r->remote_sock)
1290 GNUNET_NETWORK_socket_close (s5r->remote_sock);
1291 if ((NULL != s5r->sock) && (s5r->cleanup_sock == GNUNET_YES))
1292 GNUNET_NETWORK_socket_close (s5r->sock);
1298 * Write data to socket
1300 * @param cls the closure
1301 * @param tc scheduler context
1304 do_write (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1306 struct Socks5Request *s5r = cls;
1309 s5r->wtask = GNUNET_SCHEDULER_NO_TASK;
1311 if ((NULL != tc->read_ready) &&
1312 (GNUNET_NETWORK_fdset_isset (tc->write_ready, s5r->sock)) &&
1313 ((len = GNUNET_NETWORK_socket_send (s5r->sock, s5r->wbuf,
1316 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1317 "Successfully sent %d bytes to socket\n",
1323 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "write");
1324 s5r->cleanup = GNUNET_YES;
1325 s5r->cleanup_sock = GNUNET_YES;
1331 if (GNUNET_YES == s5r->cleanup)
1337 if ((s5r->state == SOCKS5_DATA_TRANSFER) &&
1338 (s5r->fwdrtask == GNUNET_SCHEDULER_NO_TASK))
1340 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
1342 &do_read_remote, s5r);
1346 * Read from remote end
1348 * @param cls closure
1349 * @param tc scheduler context
1352 do_read_remote (void* cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1354 struct Socks5Request *s5r = cls;
1356 s5r->fwdrtask = GNUNET_SCHEDULER_NO_TASK;
1359 if ((NULL != tc->write_ready) &&
1360 (GNUNET_NETWORK_fdset_isset (tc->read_ready, s5r->remote_sock)) &&
1361 (s5r->wbuf_len = GNUNET_NETWORK_socket_recv (s5r->remote_sock, s5r->wbuf,
1362 sizeof (s5r->wbuf))))
1364 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1365 "Successfully read %d bytes from remote socket\n",
1370 if (s5r->wbuf_len == 0)
1371 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1372 "0 bytes received from remote... graceful shutdown!\n");
1373 if (s5r->fwdwtask != GNUNET_SCHEDULER_NO_TASK)
1374 GNUNET_SCHEDULER_cancel (s5r->fwdwtask);
1375 if (s5r->rtask != GNUNET_SCHEDULER_NO_TASK)
1376 GNUNET_SCHEDULER_cancel (s5r->rtask);
1378 GNUNET_NETWORK_socket_close (s5r->remote_sock);
1379 s5r->remote_sock = NULL;
1380 GNUNET_NETWORK_socket_close (s5r->sock);
1386 s5r->wtask = GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
1394 * Adds a socket to MHD
1396 * @param h the handle to the socket to add
1397 * @return whatever MHD_add_connection returns
1400 add_handle_to_mhd (struct GNUNET_NETWORK_Handle *h, struct MHD_Daemon *daemon)
1403 struct sockaddr *addr;
1406 fd = GNUNET_NETWORK_get_fd (h);
1407 addr = GNUNET_NETWORK_get_addr (h);
1408 len = GNUNET_NETWORK_get_addrlen (h);
1410 return MHD_add_connection (daemon, fd, addr, len);
1414 /*TODO this needs MHD API modification */
1415 static int http_port = 4444;
1419 get_file_size (const char* filename)
1423 fp = fopen (filename, "rb");
1428 if ((0 != fseek (fp, 0, SEEK_END)) || (-1 == (size = ftell (fp))))
1440 * Read file in filename
1442 * @param filename file to read
1446 load_file (const char* filename)
1452 size = get_file_size (filename);
1456 fp = fopen (filename, "rb");
1460 buffer = GNUNET_malloc (size);
1467 if (size != fread (buffer, 1, size, fp))
1469 GNUNET_free (buffer);
1478 * Adds a socket to an SSL MHD instance
1479 * It is important the the domain name is
1480 * correct. In most cases we need to start a new daemon
1483 add_handle_to_ssl_mhd (struct GNUNET_NETWORK_Handle *h, char* domain)
1485 struct MhdHttpList *hd = NULL;
1487 static char *key_pem;
1488 static char *cert_pem;
1490 key_pem = load_file ("server.key");
1491 cert_pem = load_file ("server.pem");
1493 for (hd = mhd_httpd_head; hd != NULL; hd = hd->next)
1495 if (0 == strcmp (hd->domain, domain))
1502 /* TODO: create cert, start SSL MHD */
1503 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1504 "No previous SSL instance found... starting new one\n");
1505 hd = GNUNET_malloc (sizeof (struct MhdHttpList));
1506 hd->is_ssl = GNUNET_YES;
1507 strcpy (hd->domain, domain);
1508 hd->daemon = MHD_start_daemon (MHD_USE_DEBUG | MHD_USE_SSL, http_port++,
1510 &create_response, hd,
1511 MHD_OPTION_CONNECTION_LIMIT, (unsigned int) 128,
1512 MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) 16,
1513 MHD_OPTION_NOTIFY_COMPLETED,
1515 MHD_OPTION_HTTPS_MEM_KEY, key_pem,
1516 MHD_OPTION_HTTPS_MEM_CERT, cert_pem,
1518 hd->httpd_task = GNUNET_SCHEDULER_NO_TASK;
1520 GNUNET_CONTAINER_DLL_insert (mhd_httpd_head, mhd_httpd_tail, hd);
1523 return add_handle_to_mhd (h, hd->daemon);
1529 * Read data from incoming connection
1531 * @param cls the closure
1532 * @param tc the scheduler context
1535 do_read (void* cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1537 struct Socks5Request *s5r = cls;
1538 struct socks5_client_hello *c_hello;
1539 struct socks5_server_hello *s_hello;
1540 struct socks5_client_request *c_req;
1541 struct socks5_server_response *s_resp;
1547 struct hostent *phost;
1549 struct sockaddr_in remote_addr;
1550 struct in_addr *r_sin_addr;
1552 s5r->rtask = GNUNET_SCHEDULER_NO_TASK;
1554 if ((NULL != tc->write_ready) &&
1555 (GNUNET_NETWORK_fdset_isset (tc->read_ready, s5r->sock)) &&
1556 (s5r->rbuf_len = GNUNET_NETWORK_socket_recv (s5r->sock, s5r->rbuf,
1557 sizeof (s5r->rbuf))))
1559 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1560 "Successfully read %d bytes from socket\n",
1565 if (s5r->rbuf_len != 0)
1566 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "read");
1568 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client disco!\n");
1570 if (s5r->fwdrtask != GNUNET_SCHEDULER_NO_TASK)
1571 GNUNET_SCHEDULER_cancel (s5r->fwdrtask);
1572 if (s5r->wtask != GNUNET_SCHEDULER_NO_TASK)
1573 GNUNET_SCHEDULER_cancel (s5r->wtask);
1574 if (s5r->fwdwtask != GNUNET_SCHEDULER_NO_TASK)
1575 GNUNET_SCHEDULER_cancel (s5r->fwdwtask);
1576 GNUNET_NETWORK_socket_close (s5r->remote_sock);
1577 GNUNET_NETWORK_socket_close (s5r->sock);
1582 if (s5r->state == SOCKS5_INIT)
1584 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1586 c_hello = (struct socks5_client_hello*)&s5r->rbuf;
1588 GNUNET_assert (c_hello->version == SOCKS_VERSION_5);
1590 s_hello = (struct socks5_server_hello*)&s5r->wbuf;
1591 s5r->wbuf_len = sizeof( struct socks5_server_hello );
1593 s_hello->version = c_hello->version;
1594 s_hello->auth_method = SOCKS_AUTH_NONE;
1596 /* Write response to client */
1597 s5r->wtask = GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
1601 s5r->rtask = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
1605 s5r->state = SOCKS5_REQUEST;
1609 if (s5r->state == SOCKS5_REQUEST)
1611 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1612 "Processing SOCKS5 request\n");
1613 c_req = (struct socks5_client_request*)&s5r->rbuf;
1614 s_resp = (struct socks5_server_response*)&s5r->wbuf;
1615 //Only 10byte for ipv4 response!
1616 s5r->wbuf_len = 10;//sizeof (struct socks5_server_response);
1618 GNUNET_assert (c_req->addr_type == 3);
1620 dom_len = *((uint8_t*)(&(c_req->addr_type) + 1));
1621 memset(domain, 0, sizeof(domain));
1622 strncpy(domain, (char*)(&(c_req->addr_type) + 2), dom_len);
1623 req_port = *((uint16_t*)(&(c_req->addr_type) + 2 + dom_len));
1625 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1626 "Requested connection is %s:%d\n",
1630 if (is_tld (domain, GNUNET_GNS_TLD) ||
1631 is_tld (domain, GNUNET_GNS_TLD_ZKEY))
1633 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1634 "Requested connection is gnunet tld\n",
1638 if (ntohs(req_port) == HTTPS_PORT)
1640 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1641 "Requested connection is HTTPS\n");
1642 ret = add_handle_to_ssl_mhd ( s5r->sock, domain );
1644 else if (NULL != httpd)
1646 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1647 "Requested connection is HTTP\n");
1648 ret = add_handle_to_mhd ( s5r->sock, httpd );
1653 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1654 _("Failed to start HTTP server\n"));
1655 s_resp->version = 0x05;
1656 s_resp->reply = 0x01;
1657 s5r->cleanup = GNUNET_YES;
1658 s5r->cleanup_sock = GNUNET_YES;
1660 GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
1666 /* Signal success */
1667 s_resp->version = 0x05;
1668 s_resp->reply = 0x00;
1669 s_resp->reserved = 0x00;
1670 s_resp->addr_type = 0x01;
1672 s5r->cleanup = GNUNET_YES;
1673 s5r->cleanup_sock = GNUNET_NO;
1675 GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
1683 phost = (struct hostent*)gethostbyname (domain);
1686 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1687 "Resolve %s error!\n", domain );
1688 s_resp->version = 0x05;
1689 s_resp->reply = 0x01;
1690 s5r->cleanup = GNUNET_YES;
1691 s5r->cleanup_sock = GNUNET_YES;
1693 GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
1699 s5r->remote_sock = GNUNET_NETWORK_socket_create (AF_INET,
1702 r_sin_addr = (struct in_addr*)(phost->h_addr);
1703 remote_ip = r_sin_addr->s_addr;
1704 memset(&remote_addr, 0, sizeof(remote_addr));
1705 remote_addr.sin_family = AF_INET;
1706 #if HAVE_SOCKADDR_IN_SIN_LEN
1707 remote_addr.sin_len = sizeof (remote_addr);
1709 remote_addr.sin_addr.s_addr = remote_ip;
1710 remote_addr.sin_port = req_port;
1712 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1713 "target server: %s:%u\n", inet_ntoa(remote_addr.sin_addr),
1717 GNUNET_NETWORK_socket_connect ( s5r->remote_sock,
1718 (const struct sockaddr*)&remote_addr,
1719 sizeof (remote_addr)))
1720 && (errno != EINPROGRESS))
1722 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "connect");
1723 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1724 "socket request error...\n");
1725 s_resp->version = 0x05;
1726 s_resp->reply = 0x01;
1728 GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
1735 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1736 "new remote connection\n");
1738 s_resp->version = 0x05;
1739 s_resp->reply = 0x00;
1740 s_resp->reserved = 0x00;
1741 s_resp->addr_type = 0x01;
1743 s5r->state = SOCKS5_DATA_TRANSFER;
1746 GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
1750 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
1758 if (s5r->state == SOCKS5_DATA_TRANSFER)
1760 if ((s5r->remote_sock == NULL) || (s5r->rbuf_len == 0))
1762 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1763 "Closing connection to client\n");
1764 if (s5r->rtask != GNUNET_SCHEDULER_NO_TASK)
1765 GNUNET_SCHEDULER_cancel (s5r->rtask);
1766 if (s5r->fwdwtask != GNUNET_SCHEDULER_NO_TASK)
1767 GNUNET_SCHEDULER_cancel (s5r->fwdwtask);
1768 if (s5r->fwdrtask != GNUNET_SCHEDULER_NO_TASK)
1769 GNUNET_SCHEDULER_cancel (s5r->fwdrtask);
1770 if (s5r->fwdrtask != GNUNET_SCHEDULER_NO_TASK)
1771 GNUNET_SCHEDULER_cancel (s5r->fwdrtask);
1773 if (s5r->remote_sock != NULL)
1774 GNUNET_NETWORK_socket_close (s5r->remote_sock);
1775 GNUNET_NETWORK_socket_close (s5r->sock);
1780 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1781 "forwarding %d bytes from client\n", s5r->rbuf_len);
1784 GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
1786 &do_write_remote, s5r);
1788 if (s5r->fwdrtask == GNUNET_SCHEDULER_NO_TASK)
1791 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
1793 &do_read_remote, s5r);
1799 //GNUNET_CONTAINER_DLL_remove (s5conns.head, s5conns.tail, s5r);
1805 * Accept new incoming connections
1807 * @param cls the closure
1808 * @param tc the scheduler context
1811 do_accept (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1813 struct GNUNET_NETWORK_Handle *s;
1814 struct Socks5Request *s5r;
1816 ltask = GNUNET_SCHEDULER_NO_TASK;
1817 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1820 ltask = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
1824 s = GNUNET_NETWORK_socket_accept (lsock, NULL, NULL);
1828 GNUNET_log_strerror (GNUNET_ERROR_TYPE_INFO, "accept");
1832 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1833 "Got an inbound connection, waiting for data\n");
1835 s5r = GNUNET_malloc (sizeof (struct Socks5Request));
1837 s5r->state = SOCKS5_INIT;
1838 s5r->wtask = GNUNET_SCHEDULER_NO_TASK;
1839 s5r->fwdwtask = GNUNET_SCHEDULER_NO_TASK;
1840 s5r->fwdrtask = GNUNET_SCHEDULER_NO_TASK;
1841 s5r->rtask = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
1844 //GNUNET_CONTAINER_DLL_insert (s5conns.head, s5conns.tail, s5r);
1849 * Task run on shutdown
1851 * @param cls closure
1852 * @param tc task context
1855 do_shutdown (void *cls,
1856 const struct GNUNET_SCHEDULER_TaskContext *tc)
1859 struct MhdHttpList *hd;
1860 struct MhdHttpList *tmp_hd;
1861 struct ProxyCurlTask *ctask;
1862 struct ProxyCurlTask *ctask_tmp;
1864 if (GNUNET_SCHEDULER_NO_TASK != curl_download_task)
1866 GNUNET_SCHEDULER_cancel (curl_download_task);
1867 curl_download_task = GNUNET_SCHEDULER_NO_TASK;
1870 for (hd = mhd_httpd_head; hd != NULL; hd = tmp_hd)
1874 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1875 "Stopping daemon\n");
1877 if (GNUNET_SCHEDULER_NO_TASK != hd->httpd_task)
1879 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1880 "Stopping select task %d\n",
1882 GNUNET_SCHEDULER_cancel (hd->httpd_task);
1883 hd->httpd_task = GNUNET_SCHEDULER_NO_TASK;
1886 if (NULL != hd->daemon)
1888 MHD_stop_daemon (hd->daemon);
1895 for (ctask=ctasks_head; ctask != NULL; ctask=ctask_tmp)
1897 ctask_tmp = ctask->next;
1899 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1900 "Cleaning up cURL task\n");
1902 if (ctask->curl != NULL)
1903 curl_easy_cleanup (ctask->curl);
1905 if (NULL != ctask->headers)
1906 curl_slist_free_all (ctask->headers);
1908 GNUNET_free (ctask);
1911 GNUNET_GNS_disconnect (gns_handle);
1916 * Compiles a regex for us
1918 * @param re ptr to re struct
1919 * @param rt the expression to compile
1920 * @return 0 on success
1923 compile_regex (regex_t *re, const char* rt)
1928 status = regcomp (re, rt, REG_EXTENDED|REG_NEWLINE);
1931 regerror (status, re, err, 1024);
1932 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1933 "Regex error compiling '%s': %s\n", rt, err);
1941 * Loads the users local zone key
1943 * @return GNUNET_YES on success
1946 load_local_zone_key (const struct GNUNET_CONFIGURATION_Handle *cfg)
1949 struct GNUNET_CRYPTO_RsaPrivateKey *key = NULL;
1950 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pkey;
1951 struct GNUNET_CRYPTO_ShortHashCode *zone = NULL;
1952 struct GNUNET_CRYPTO_ShortHashAsciiEncoded zonename;
1954 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "gns",
1955 "ZONEKEY", &keyfile))
1957 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1958 "Unable to load zone key config value!\n");
1962 if (GNUNET_NO == GNUNET_DISK_file_test (keyfile))
1964 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1965 "Unable to load zone key!\n");
1966 GNUNET_free(keyfile);
1970 key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
1971 GNUNET_CRYPTO_rsa_key_get_public (key, &pkey);
1972 GNUNET_CRYPTO_short_hash(&pkey,
1973 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
1975 zone = &local_gns_zone;
1976 GNUNET_CRYPTO_short_hash_to_enc (zone, &zonename);
1977 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1978 "Using zone: %s!\n", &zonename);
1979 GNUNET_CRYPTO_rsa_key_free(key);
1980 GNUNET_free(keyfile);
1986 * Main function that will be run
1988 * @param cls closure
1989 * @param args remaining command-line arguments
1990 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
1991 * @param cfg configuration
1994 run (void *cls, char *const *args, const char *cfgfile,
1995 const struct GNUNET_CONFIGURATION_Handle *cfg)
1997 struct sockaddr_in sa;
1998 struct MhdHttpList *hd;
2000 compile_regex (&re_dotplus, (char*) RE_DOTPLUS);
2002 gns_handle = GNUNET_GNS_connect (cfg);
2004 if (GNUNET_NO == load_local_zone_key (cfg))
2006 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2007 "Unable to load zone!\n");
2011 if (NULL == gns_handle)
2013 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2014 "Unable to connect to GNS!\n");
2018 memset (&sa, 0, sizeof (sa));
2019 sa.sin_family = AF_INET;
2020 sa.sin_port = htons (port);
2021 #if HAVE_SOCKADDR_IN_SIN_LEN
2022 sa.sin_len = sizeof (sa);
2025 lsock = GNUNET_NETWORK_socket_create (AF_INET,
2029 if ((NULL == lsock) ||
2031 GNUNET_NETWORK_socket_bind (lsock, (const struct sockaddr *) &sa,
2034 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2035 "Failed to create listen socket bound to `%s'",
2036 GNUNET_a2s ((const struct sockaddr *) &sa, sizeof (sa)));
2038 GNUNET_NETWORK_socket_close (lsock);
2042 if (GNUNET_OK != GNUNET_NETWORK_socket_listen (lsock, 5))
2044 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2045 "Failed to listen on socket bound to `%s'",
2046 GNUNET_a2s ((const struct sockaddr *) &sa, sizeof (sa)));
2050 ltask = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
2051 lsock, &do_accept, NULL);
2056 if (0 != curl_global_init (CURL_GLOBAL_WIN32))
2058 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2059 "cURL global init failed!\n");
2063 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2064 "Proxy listens on port %u\n",
2067 mhd_httpd_head = NULL;
2068 mhd_httpd_tail = NULL;
2070 hd = GNUNET_malloc (sizeof (struct MhdHttpList));
2071 hd->is_ssl = GNUNET_NO;
2072 strcpy (hd->domain, "");
2073 httpd = MHD_start_daemon (MHD_USE_DEBUG, http_port++,
2075 &create_response, hd,
2076 MHD_OPTION_CONNECTION_LIMIT, (unsigned int) 128,
2077 MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) 16,
2078 MHD_OPTION_NOTIFY_COMPLETED,
2082 hd->httpd_task = GNUNET_SCHEDULER_NO_TASK;
2084 GNUNET_CONTAINER_DLL_insert (mhd_httpd_head, mhd_httpd_tail, hd);
2088 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
2089 &do_shutdown, NULL);
2095 * The main function for gnunet-gns-proxy.
2097 * @param argc number of arguments from the command line
2098 * @param argv command line arguments
2099 * @return 0 ok, 1 on error
2102 main (int argc, char *const *argv)
2104 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
2106 gettext_noop ("listen on specified port"), 1,
2107 &GNUNET_GETOPT_set_string, &port},
2108 GNUNET_GETOPT_OPTION_END
2113 GNUNET_log_setup ("gnunet-gns-proxy", "WARNING", NULL);
2116 GNUNET_PROGRAM_run (argc, argv, "gnunet-gns-proxy",
2117 _("GNUnet GNS proxy"),
2119 &run, NULL)) ? 0 : 1;