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 MHD_MAX_CONNECTIONS 300
39 #define MAX_HTTP_URI_LENGTH 2048
40 #define POSTBUFFERSIZE 4096
42 /* MHD/cURL defines */
43 #define BUF_WAIT_FOR_CURL 0
44 #define BUF_WAIT_FOR_MHD 1
45 #define BUF_WAIT_FOR_PP 2
46 #define HTML_HDR_CONTENT "Content-Type: text/html"
48 /* buffer padding for proper RE matching */
49 #define CURL_BUF_PADDING 1000
52 //#define RE_DOTPLUS "<a href=\"http://(([A-Za-z]+[.])+)([+])"
53 #define RE_A_HREF "href=\"https?://(([A-Za-z0-9]+[.])+)([+]|[a-z]+)"
54 #define RE_N_MATCHES 4
56 /* The usual suspects */
58 #define HTTPS_PORT 443
62 * A structure for CA cert/key
67 gnutls_x509_crt_t cert;
70 gnutls_x509_privkey_t key;
73 #define MAX_PEM_SIZE (10 * 1024)
76 * Structure for GNS certificates
78 struct ProxyGNSCertificate
80 /* The certificate as PEM */
81 char cert[MAX_PEM_SIZE];
83 /* The private key as PEM */
84 char key[MAX_PEM_SIZE];
89 * A structure for socks requests
93 /* The client socket */
94 struct GNUNET_NETWORK_Handle *sock;
96 /* The server socket */
97 struct GNUNET_NETWORK_Handle *remote_sock;
102 /* Client socket read task */
103 GNUNET_SCHEDULER_TaskIdentifier rtask;
105 /* Server socket read task */
106 GNUNET_SCHEDULER_TaskIdentifier fwdrtask;
108 /* Client socket write task */
109 GNUNET_SCHEDULER_TaskIdentifier wtask;
111 /* Server socket write task */
112 GNUNET_SCHEDULER_TaskIdentifier fwdwtask;
120 /* Length of data in read buffer */
121 unsigned int rbuf_len;
123 /* Length of data in write buffer */
124 unsigned int wbuf_len;
126 /* This handle is scheduled for cleanup? */
129 /* Shall we close the client socket on cleanup? */
134 * DLL for Network Handles
136 struct NetworkHandleList
139 struct NetworkHandleList *next;
142 struct NetworkHandleList *prev;
145 struct GNUNET_NETWORK_Handle *h;
149 * A structure for all running Httpds
154 struct MhdHttpList *prev;
157 struct MhdHttpList *next;
159 /* is this an ssl daemon? */
162 /* the domain name to server (only important for SSL) */
165 /* The daemon handle */
166 struct MHD_Daemon *daemon;
168 /* Optional proxy certificate used */
169 struct ProxyGNSCertificate *proxy_cert;
172 GNUNET_SCHEDULER_TaskIdentifier httpd_task;
174 /* Handles associated with this daemon */
175 struct NetworkHandleList *socket_handles_head;
177 /* Handles associated with this daemon */
178 struct NetworkHandleList *socket_handles_tail;
182 * A structure for MHD<->cURL streams
187 struct ProxyCurlTask *prev;
190 struct ProxyCurlTask *next;
195 /* Optional header replacements for curl (LEHO) */
196 struct curl_slist *headers;
198 /* Optional resolver replacements for curl (LEHO) */
199 struct curl_slist *resolver;
201 /* curl response code */
202 long curl_response_code;
204 /* The URL to fetch */
205 char url[MAX_HTTP_URI_LENGTH];
207 /* The cURL write buffer / MHD read buffer */
208 char buffer[CURL_MAX_WRITE_SIZE + CURL_BUF_PADDING];
210 /* Read pos of the data in the buffer */
211 char *buffer_read_ptr;
213 /* Write pos in the buffer */
214 char *buffer_write_ptr;
216 /* The buffer status (BUF_WAIT_FOR_CURL or BUF_WAIT_FOR_MHD) */
219 /* Number of bytes in buffer */
220 unsigned int bytes_in_buffer;
222 /* Indicates wheather the download is in progress */
223 int download_in_progress;
225 /* Indicates wheather the download was successful */
226 int download_is_finished;
228 /* Indicates wheather the download failed */
231 /* Indicates wheather we need to parse HTML */
234 /* Indicates wheather we are postprocessing the HTML right now */
235 int is_postprocessing;
237 /* Indicates wheather postprocessing has finished */
241 GNUNET_SCHEDULER_TaskIdentifier pp_task;
244 struct ProxyREMatch *pp_match_head;
247 struct ProxyREMatch *pp_match_tail;
249 /* The authority of the corresponding host (site of origin) */
252 /* The hostname (Host header field) */
258 /* The LEgacy HOstname (can be empty) */
261 /* The associated daemon list entry */
262 struct MhdHttpList *mhd;
264 /* The associated response */
265 struct MHD_Response *response;
268 struct ProxySetCookieHeader *set_cookies_head;
271 struct ProxySetCookieHeader *set_cookies_tail;
273 /* connection status */
280 struct MHD_Connection *connection;
283 size_t put_read_offset;
284 size_t put_read_size;
287 struct MHD_PostProcessor *post_handler;
290 struct ProxyPostData *post_data_head;
291 struct ProxyPostData *post_data_tail;
295 /* the type of POST encoding */
301 * Struct for RE matches in postprocessing of HTML
306 struct ProxyREMatch *next;
309 struct ProxyREMatch *prev;
318 struct GNUNET_GNS_ShortenRequest *shorten_task;
323 /* start of match in buffer */
326 /* end of match in buffer */
329 /* associated proxycurltask */
330 struct ProxyCurlTask *ctask;
334 * Struct for set-cookies
336 struct ProxySetCookieHeader
339 struct ProxySetCookieHeader *next;
342 struct ProxySetCookieHeader *prev;
349 * Post data structure
354 struct ProxyPostData *next;
357 struct ProxyPostData *prev;
370 /* The port the proxy is running on (default 7777) */
371 static unsigned long port = GNUNET_GNS_PROXY_PORT;
373 /* The CA file (pem) to use for the proxy CA */
374 static char* cafile_opt;
376 /* The listen socket of the proxy */
377 static struct GNUNET_NETWORK_Handle *lsock;
379 /* The listen task ID */
380 GNUNET_SCHEDULER_TaskIdentifier ltask;
382 /* The cURL download task */
383 GNUNET_SCHEDULER_TaskIdentifier curl_download_task;
385 /* The non SSL httpd daemon handle */
386 static struct MHD_Daemon *httpd;
388 /* Number of current mhd connections */
389 static unsigned int total_mhd_connections;
391 /* The cURL multi handle */
392 static CURLM *curl_multi;
394 /* Handle to the GNS service */
395 static struct GNUNET_GNS_Handle *gns_handle;
397 /* DLL for ProxyCurlTasks */
398 static struct ProxyCurlTask *ctasks_head;
400 /* DLL for ProxyCurlTasks */
401 static struct ProxyCurlTask *ctasks_tail;
403 /* DLL for http daemons */
404 static struct MhdHttpList *mhd_httpd_head;
406 /* DLL for http daemons */
407 static struct MhdHttpList *mhd_httpd_tail;
409 /* Handle to the regex for dotplus (.+) replacement in HTML */
410 static regex_t re_dotplus;
412 /* The users local GNS zone hash */
413 static struct GNUNET_CRYPTO_ShortHashCode *local_gns_zone;
415 /* The users local private zone */
416 static struct GNUNET_CRYPTO_ShortHashCode *local_private_zone;
418 /* The users local shorten zone */
419 static struct GNUNET_CRYPTO_ShortHashCode *local_shorten_zone;
421 /* The CA for SSL certificate generation */
422 static struct ProxyCA proxy_ca;
424 /* UNIX domain socket for mhd */
425 struct GNUNET_NETWORK_Handle *mhd_unix_socket;
427 /* Shorten zone private key */
428 struct GNUNET_CRYPTO_RsaPrivateKey *shorten_zonekey;
431 * Checks if name is in tld
433 * @param name the name to check
434 * @param tld the TLD to check for
435 * @return GNUNET_YES or GNUNET_NO
438 is_tld(const char* name, const char* tld)
442 if (strlen(name) <= strlen(tld))
445 offset = strlen(name) - strlen(tld);
446 if (0 != strcmp (name+offset, tld))
448 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
449 "%s is not in .%s TLD\n", name, tld);
457 * convert integer to string representation
460 * @return the character
462 char i_to_hexchar (char i)
464 static char hexmap[] = "0123456789abcdef";
465 GNUNET_assert (sizeof (hexmap) > (i & 15));
466 return hexmap[i & 15];
470 * Escape giben 0-terminated string
472 * @param to_esc string to escapse
473 * @return allocated new escaped string (MUST free!)
476 escape_to_urlenc (const char *to_esc)
478 char *pos = (char*)to_esc;
479 char *res = GNUNET_malloc (strlen (to_esc) * 3 + 1);
484 if (isalnum (*pos) ||
485 ('-' == *pos) || ('_' == *pos) ||
486 ('.' == *pos) || ('~' == *pos))
488 else if (' ' == *pos)
493 *rpos++ = i_to_hexchar (*pos >> 4);
494 *rpos++ = i_to_hexchar (*pos >> 15);
503 con_post_data_iter (void *cls,
504 enum MHD_ValueKind kind,
506 const char *filename,
507 const char *content_type,
508 const char *transfer_encoding,
513 struct ProxyCurlTask* ctask = cls;
514 struct ProxyPostData* pdata;
517 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
518 "Got POST data: '%s : %s' at offset %llu size %lld\n",
519 key, data, off, size);
521 if (0 != strcasecmp (MHD_HTTP_POST_ENCODING_FORM_URLENCODED,
530 pdata = GNUNET_malloc (sizeof (struct ProxyPostData));
531 enc = escape_to_urlenc (key);
532 pdata->value = GNUNET_malloc (strlen (enc) + 3);
533 if (NULL != ctask->post_data_head)
535 pdata->value[0] = '&';
536 memcpy (pdata->value+1, enc, strlen (enc));
539 memcpy (pdata->value, enc, strlen (enc));
540 pdata->value[strlen (pdata->value)] = '=';
541 pdata->bytes_left = strlen (pdata->value);
542 pdata->total_bytes = pdata->bytes_left;
545 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
546 "Escaped POST key: '%s'\n",
549 GNUNET_CONTAINER_DLL_insert_tail (ctask->post_data_head,
550 ctask->post_data_tail,
555 pdata = GNUNET_malloc (sizeof (struct ProxyPostData));
556 enc = escape_to_urlenc (data);
557 pdata->value = GNUNET_malloc (strlen (enc) + 1);
558 memcpy (pdata->value, enc, strlen (enc));
559 pdata->bytes_left = strlen (pdata->value);
560 pdata->total_bytes = pdata->bytes_left;
563 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
564 "Escaped POST value: '%s'\n",
567 GNUNET_CONTAINER_DLL_insert_tail (ctask->post_data_head,
568 ctask->post_data_tail,
575 get_uri_val_iter (void *cls,
576 enum MHD_ValueKind kind,
582 if (strlen (buf) + strlen (value) + 3 > MAX_HTTP_URI_LENGTH)
584 sprintf (buf+strlen (buf), "?%s=%s", key, value);
590 * Read HTTP request header field 'Host'
592 * @param cls buffer to write to
593 * @param kind value kind
594 * @param key field key
595 * @param value field value
596 * @return MHD_NO when Host found
599 con_val_iter (void *cls,
600 enum MHD_ValueKind kind,
604 struct ProxyCurlTask *ctask = cls;
605 char* buf = ctask->host;
611 if (0 == strcmp ("Host", key))
613 port = strstr (value, ":");
616 strncpy (buf, value, port-value);
618 if ((1 != sscanf (port, "%u", &uport)) ||
619 (uport > UINT16_MAX) ||
621 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
622 "Unable to parse port!\n");
624 ctask->port = (uint16_t) uport;
631 if (0 == strcmp ("Accept-Encoding", key))
636 if (0 == strcmp (MHD_HTTP_HEADER_CONTENT_TYPE,
639 if (0 == strcmp (value,
640 MHD_HTTP_POST_ENCODING_FORM_URLENCODED))
641 ctask->post_type = MHD_HTTP_POST_ENCODING_FORM_URLENCODED;
642 else if (0 == strcmp (value,
643 MHD_HTTP_POST_ENCODING_MULTIPART_FORMDATA))
644 ctask->post_type = MHD_HTTP_POST_ENCODING_MULTIPART_FORMDATA;
646 ctask->post_type = NULL;
650 cstr = GNUNET_malloc (strlen (key) + strlen (hdr_val) + 3);
651 GNUNET_snprintf (cstr, strlen (key) + strlen (hdr_val) + 3,
652 "%s: %s", key, hdr_val);
654 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
655 "Client Header: %s\n", cstr);
657 ctask->headers = curl_slist_append (ctask->headers, cstr);
665 * Callback for MHD response
668 * @param pos in buffer
670 * @param max space in buffer
671 * @return number of bytes written
674 mhd_content_cb (void *cls,
680 * Check HTTP response header for mime
682 * @param buffer curl buffer
683 * @param size curl blocksize
684 * @param nmemb curl blocknumber
686 * @return size of read bytes
689 curl_check_hdr (void *buffer, size_t size, size_t nmemb, void *cls)
691 size_t bytes = size * nmemb;
692 struct ProxyCurlTask *ctask = cls;
693 int html_mime_len = strlen (HTML_HDR_CONTENT);
694 int cookie_hdr_len = strlen (MHD_HTTP_HEADER_SET_COOKIE);
695 char hdr_mime[html_mime_len+1];
696 char hdr_generic[bytes+1];
697 char new_cookie_hdr[bytes+strlen (ctask->leho)+1];
698 char new_location[MAX_HTTP_URI_LENGTH+500];
709 if (NULL == ctask->response)
711 /* FIXME: get total size from curl (if available) */
712 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
713 "Creating response for %s\n", ctask->url);
714 ctask->response = MHD_create_response_from_callback (MHD_SIZE_UNKNOWN,
715 sizeof (ctask->buffer),
719 ctask->ready_to_queue = GNUNET_YES;
723 if (html_mime_len <= bytes)
725 memcpy (hdr_mime, buffer, html_mime_len);
726 hdr_mime[html_mime_len] = '\0';
728 if (0 == strcmp (hdr_mime, HTML_HDR_CONTENT))
730 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
731 "Got HTML HTTP response header\n");
732 ctask->parse_content = GNUNET_YES;
736 if (cookie_hdr_len > bytes)
739 memcpy (hdr_generic, buffer, bytes);
740 hdr_generic[bytes] = '\0';
742 if ('\n' == hdr_generic[bytes-1])
743 hdr_generic[bytes-1] = '\0';
745 if (hdr_generic[bytes-2] == '\r')
746 hdr_generic[bytes-2] = '\0';
748 if (0 == memcmp (hdr_generic,
749 MHD_HTTP_HEADER_SET_COOKIE,
752 ndup = GNUNET_strdup (hdr_generic+cookie_hdr_len+1);
753 memset (new_cookie_hdr, 0, sizeof (new_cookie_hdr));
754 tok = strtok (ndup, ";");
756 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
757 "Looking for cookie in : %s\n", hdr_generic);
759 for (; tok != NULL; tok = strtok (NULL, ";"))
761 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
762 "Got Cookie token: %s\n", tok);
763 //memcpy (new_cookie_hdr+offset, tok, strlen (tok));
764 if (0 == memcmp (tok, " domain", strlen (" domain")))
766 cookie_domain = tok + strlen (" domain") + 1;
768 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
769 "Got Set-Cookie Domain: %s\n", cookie_domain);
771 if (strlen (cookie_domain) < strlen (ctask->leho))
773 delta_cdomain = strlen (ctask->leho) - strlen (cookie_domain);
774 if (0 == strcmp (cookie_domain, ctask->leho + (delta_cdomain)))
776 GNUNET_snprintf (new_cookie_hdr+offset,
777 sizeof (new_cookie_hdr),
778 " domain=%s", ctask->authority);
779 offset += strlen (" domain=") + strlen (ctask->authority);
780 new_cookie_hdr[offset] = ';';
785 else if (strlen (cookie_domain) == strlen (ctask->leho))
787 if (0 == strcmp (cookie_domain, ctask->leho))
789 GNUNET_snprintf (new_cookie_hdr+offset,
790 sizeof (new_cookie_hdr),
791 " domain=%s", ctask->host);
792 offset += strlen (" domain=") + strlen (ctask->host);
793 new_cookie_hdr[offset] = ';';
798 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
799 "Cookie domain invalid\n");
803 memcpy (new_cookie_hdr+offset, tok, strlen (tok));
804 offset += strlen (tok);
805 new_cookie_hdr[offset] = ';';
811 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
812 "Got Set-Cookie HTTP header %s\n", new_cookie_hdr);
814 if (GNUNET_NO == MHD_add_response_header (ctask->response,
815 MHD_HTTP_HEADER_SET_COOKIE,
818 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
819 "MHD: Error adding set-cookie header field %s\n",
820 hdr_generic+cookie_hdr_len+1);
825 ndup = GNUNET_strdup (hdr_generic);
826 hdr_type = strtok (ndup, ":");
828 if (NULL == hdr_type)
834 hdr_val = strtok (NULL, "");
844 if (0 == strcasecmp (MHD_HTTP_HEADER_LOCATION, hdr_type))
846 if (ctask->mhd->is_ssl)
848 sprintf (leho_host, "https://%s", ctask->leho);
849 sprintf (real_host, "https://%s", ctask->host);
853 sprintf (leho_host, "http://%s", ctask->leho);
854 sprintf (real_host, "http://%s", ctask->host);
857 if (0 == memcmp (leho_host, hdr_val, strlen (leho_host)))
859 sprintf (new_location, "%s%s", real_host, hdr_val+strlen (leho_host));
860 hdr_val = new_location;
864 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
865 "Trying to set %s: %s\n",
868 if (GNUNET_NO == MHD_add_response_header (ctask->response,
872 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
873 "MHD: Error adding %s header field %s\n",
884 * @param hd a http daemon list entry
887 run_httpd (struct MhdHttpList *hd);
898 * Task run whenever HTTP server operations are pending.
901 * @param tc sched context
905 const struct GNUNET_SCHEDULER_TaskContext *tc);
908 run_mhd_now (struct MhdHttpList *hd)
910 if (GNUNET_SCHEDULER_NO_TASK != hd->httpd_task)
912 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
913 "MHD: killing old task\n");
914 GNUNET_SCHEDULER_cancel (hd->httpd_task);
916 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
917 "MHD: Scheduling MHD now\n");
918 hd->httpd_task = GNUNET_SCHEDULER_add_now (&do_httpd, hd);
922 * Ask cURL for the select sets and schedule download
925 curl_download_prepare ();
928 * Callback to free content
930 * @param cls content to free
931 * @param tc task context
934 mhd_content_free (void *cls,
935 const struct GNUNET_SCHEDULER_TaskContext *tc)
937 struct ProxyCurlTask *ctask = cls;
938 GNUNET_assert (NULL == ctask->pp_match_head);
940 if (NULL != ctask->headers)
941 curl_slist_free_all (ctask->headers);
943 if (NULL != ctask->headers)
944 curl_slist_free_all (ctask->resolver);
946 if (NULL != ctask->response)
947 MHD_destroy_response (ctask->response);
949 if (NULL != ctask->post_handler)
950 MHD_destroy_post_processor (ctask->post_handler);
958 * Callback for MHD response
961 * @param pos in buffer
963 * @param max space in buffer
964 * @return number of bytes written
967 mhd_content_cb (void *cls,
972 struct ProxyCurlTask *ctask = cls;
973 struct ProxyREMatch *re_match = ctask->pp_match_head;
975 long long int bytes_to_copy = ctask->buffer_write_ptr - ctask->buffer_read_ptr;
977 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
978 "MHD: content cb for %s. To copy: %lld\n",
979 ctask->url, bytes_to_copy);
980 GNUNET_assert (bytes_to_copy >= 0);
982 if ((GNUNET_YES == ctask->download_is_finished) &&
983 (GNUNET_NO == ctask->download_error) &&
984 (0 == bytes_to_copy)) /* &&
985 (BUF_WAIT_FOR_CURL == ctask->buf_status))*/
987 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
988 "MHD: sending response for %s\n", ctask->url);
989 ctask->download_in_progress = GNUNET_NO;
990 run_mhd_now (ctask->mhd);
991 GNUNET_SCHEDULER_add_now (&mhd_content_free, ctask);
992 total_mhd_connections--;
993 return MHD_CONTENT_READER_END_OF_STREAM;
996 if ((GNUNET_YES == ctask->download_error) &&
997 (GNUNET_YES == ctask->download_is_finished) &&
998 (0 == bytes_to_copy)) /* &&
999 (BUF_WAIT_FOR_CURL == ctask->buf_status))*/
1001 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1002 "MHD: sending error response\n");
1003 ctask->download_in_progress = GNUNET_NO;
1004 run_mhd_now (ctask->mhd);
1005 GNUNET_SCHEDULER_add_now (&mhd_content_free, ctask);
1006 total_mhd_connections--;
1007 return MHD_CONTENT_READER_END_WITH_ERROR;
1010 if ( ctask->buf_status == BUF_WAIT_FOR_CURL )
1014 for (; NULL != re_match; re_match = ctask->pp_match_head)
1016 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1017 "MHD: Processing PP %s\n",
1018 re_match->hostname);
1019 bytes_to_copy = re_match->start - ctask->buffer_read_ptr;
1020 GNUNET_assert (bytes_to_copy >= 0);
1022 if (bytes_to_copy+copied > max)
1024 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1025 "MHD: buffer in response too small for %d. Using available space (%d). (%s)\n",
1029 memcpy (buf+copied, ctask->buffer_read_ptr, max-copied);
1030 ctask->buffer_read_ptr += max-copied;
1032 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1033 "MHD: copied %d bytes\n", copied);
1037 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1038 "MHD: copying %d bytes to mhd response at offset %d\n",
1039 bytes_to_copy, ctask->buffer_read_ptr);
1040 memcpy (buf+copied, ctask->buffer_read_ptr, bytes_to_copy);
1041 copied += bytes_to_copy;
1043 if (GNUNET_NO == re_match->done)
1045 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1046 "MHD: Waiting for PP of %s\n", re_match->hostname);
1047 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1048 "MHD: copied %d bytes\n", copied);
1049 ctask->buffer_read_ptr += bytes_to_copy;
1053 if (strlen (re_match->result) > (max - copied))
1055 //FIXME partially copy domain here
1056 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1057 "MHD: buffer in response too small for %s! (%s)\n",
1060 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1061 "MHD: copied %d bytes\n", copied);
1062 ctask->buffer_read_ptr += bytes_to_copy;
1066 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1067 "MHD: Adding PP result %s to buffer\n",
1069 memcpy (buf+copied, re_match->result, strlen (re_match->result));
1070 copied += strlen (re_match->result);
1071 ctask->buffer_read_ptr = re_match->end;
1072 GNUNET_CONTAINER_DLL_remove (ctask->pp_match_head,
1073 ctask->pp_match_tail,
1075 GNUNET_free (re_match);
1078 bytes_to_copy = ctask->buffer_write_ptr - ctask->buffer_read_ptr;
1080 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1081 "MHD: copied: %d left: %d, space left in buf: %d\n",
1083 bytes_to_copy, max-copied);
1085 GNUNET_assert (0 <= bytes_to_copy);
1087 if (GNUNET_NO == ctask->download_is_finished)
1089 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1090 "MHD: Purging buffer\n");
1091 memmove (ctask->buffer, ctask->buffer_read_ptr, bytes_to_copy);
1092 ctask->buffer_read_ptr = ctask->buffer;
1093 ctask->buffer_write_ptr = ctask->buffer + bytes_to_copy;
1094 ctask->buffer[bytes_to_copy] = '\0';
1097 if (bytes_to_copy+copied > max)
1098 bytes_to_copy = max-copied;
1100 if (0 > bytes_to_copy)
1103 memcpy (buf+copied, ctask->buffer_read_ptr, bytes_to_copy);
1104 ctask->buffer_read_ptr += bytes_to_copy;
1105 copied += bytes_to_copy;
1106 ctask->buf_status = BUF_WAIT_FOR_CURL;
1108 if (NULL != ctask->curl)
1109 curl_easy_pause (ctask->curl, CURLPAUSE_CONT);
1111 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1112 "MHD: copied %d bytes\n", copied);
1113 run_mhd_now (ctask->mhd);
1118 * Shorten result callback
1120 * @param cls the proxycurltask
1121 * @param short_name the shortened name (NULL on error)
1124 process_shorten (void* cls, const char* short_name)
1126 struct ProxyREMatch *re_match = cls;
1127 char result[sizeof (re_match->result)];
1129 if (NULL == short_name)
1131 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1132 "PP: Unable to shorten %s\n",
1133 re_match->hostname);
1134 GNUNET_CONTAINER_DLL_remove (re_match->ctask->pp_match_head,
1135 re_match->ctask->pp_match_tail,
1137 GNUNET_free (re_match);
1141 if (0 == strcmp (short_name, re_match->ctask->leho))
1142 strcpy (result, re_match->ctask->host);
1144 strcpy (result, short_name);
1146 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1147 "PP: Shorten %s -> %s\n",
1151 if (re_match->ctask->mhd->is_ssl)
1152 sprintf (re_match->result, "href=\"https://%s", result);
1154 sprintf (re_match->result, "href=\"http://%s", result);
1156 re_match->done = GNUNET_YES;
1157 run_mhd_now (re_match->ctask->mhd);
1162 * Postprocess data in buffer. From read ptr to write ptr
1164 * @param cls the curlproxytask
1165 * @param tc task context
1168 postprocess_buffer (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1170 struct ProxyCurlTask *ctask = cls;
1171 struct ProxyREMatch *re_match;
1172 char* re_ptr = ctask->buffer_read_ptr;
1173 char re_hostname[255];
1174 regmatch_t m[RE_N_MATCHES];
1176 ctask->pp_task = GNUNET_SCHEDULER_NO_TASK;
1178 if (GNUNET_YES != ctask->parse_content)
1180 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1181 "PP: Not parsing content\n");
1182 ctask->buf_status = BUF_WAIT_FOR_MHD;
1183 run_mhd_now (ctask->mhd);
1187 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1188 "PP: We need to parse the HTML\n");
1190 /* 0 means match found */
1191 while (0 == regexec (&re_dotplus, re_ptr, RE_N_MATCHES, m, 0))
1193 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1194 "PP: regex match\n");
1196 GNUNET_assert (m[1].rm_so != -1);
1198 memset (re_hostname, 0, sizeof (re_hostname));
1199 memcpy (re_hostname, re_ptr+m[1].rm_so, (m[3].rm_eo-m[1].rm_so));
1201 re_match = GNUNET_malloc (sizeof (struct ProxyREMatch));
1202 re_match->start = re_ptr + m[0].rm_so;
1203 re_match->end = re_ptr + m[3].rm_eo;
1204 re_match->done = GNUNET_NO;
1205 re_match->ctask = ctask;
1206 strcpy (re_match->hostname, re_hostname);
1207 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1208 "PP: Got hostname %s\n", re_hostname);
1209 re_ptr += m[3].rm_eo;
1211 if (GNUNET_YES == is_tld (re_match->hostname, GNUNET_GNS_TLD_PLUS))
1213 re_match->hostname[strlen(re_match->hostname)-1] = '\0';
1214 strcpy (re_match->hostname+strlen(re_match->hostname),
1218 re_match->shorten_task = GNUNET_GNS_shorten_zone (gns_handle,
1224 re_match); //FIXME cancel appropriately
1226 GNUNET_CONTAINER_DLL_insert_tail (ctask->pp_match_head,
1227 ctask->pp_match_tail,
1231 ctask->buf_status = BUF_WAIT_FOR_MHD;
1232 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1233 "PP: No more matches\n");
1234 run_mhd_now (ctask->mhd);
1238 * Handle data from cURL
1240 * @param ptr pointer to the data
1241 * @param size number of blocks of data
1242 * @param nmemb blocksize
1243 * @param ctx the curlproxytask
1244 * @return number of bytes handled
1247 curl_download_cb (void *ptr, size_t size, size_t nmemb, void* ctx)
1249 const char *cbuf = ptr;
1250 size_t total = size * nmemb;
1251 struct ProxyCurlTask *ctask = ctx;
1252 size_t buf_space = sizeof (ctask->buffer) -
1253 (ctask->buffer_write_ptr-ctask->buffer);
1255 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1256 "CURL: Got %d. %d free in buffer\n",
1259 if (total > (buf_space - CURL_BUF_PADDING))
1261 if (ctask->buf_status == BUF_WAIT_FOR_CURL)
1263 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1264 "CURL: Buffer full starting postprocessing\n");
1265 ctask->buf_status = BUF_WAIT_FOR_PP;
1266 ctask->pp_task = GNUNET_SCHEDULER_add_now (&postprocess_buffer,
1268 return CURL_WRITEFUNC_PAUSE;
1271 /* we should not get called in that case */
1272 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1273 "CURL: called out of context and no space in buffer!\n");
1274 return CURL_WRITEFUNC_PAUSE;
1277 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1278 "CURL: Copying %d bytes to buffer (%s)\n", total, ctask->url);
1279 memcpy (ctask->buffer_write_ptr, cbuf, total);
1280 ctask->bytes_in_buffer += total;
1281 ctask->buffer_write_ptr += total;
1282 ctask->buffer_write_ptr[0] = '\0';
1288 * cURL callback for post data
1291 read_callback (void *buf, size_t size, size_t nmemb, void *cls)
1293 struct ProxyCurlTask *ctask = cls;
1294 struct ProxyPostData *pdata = ctask->post_data_head;
1295 size_t len = size * nmemb;
1299 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1300 "CURL: read callback\n");
1306 if (NULL == pdata->value)
1308 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1309 "CURL: Terminating POST data\n");
1311 GNUNET_CONTAINER_DLL_remove (ctask->post_data_head,
1312 ctask->post_data_tail,
1314 GNUNET_free (pdata);
1318 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1319 "CURL: read callback value %s\n", pdata->value);
1321 to_copy = pdata->bytes_left;
1325 pos = pdata->value + (pdata->total_bytes - pdata->bytes_left);
1326 memcpy (buf, pos, to_copy);
1327 pdata->bytes_left -= to_copy;
1328 if (pdata->bytes_left <= 0)
1330 GNUNET_free (pdata->value);
1331 GNUNET_CONTAINER_DLL_remove (ctask->post_data_head,
1332 ctask->post_data_tail,
1334 GNUNET_free (pdata);
1340 * Task that is run when we are ready to receive more data
1343 * @param cls closure
1344 * @param tc task context
1347 curl_task_download (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
1350 * Ask cURL for the select sets and schedule download
1353 curl_download_prepare ()
1360 struct GNUNET_NETWORK_FDSet *grs;
1361 struct GNUNET_NETWORK_FDSet *gws;
1363 struct GNUNET_TIME_Relative rtime;
1369 mret = curl_multi_fdset (curl_multi, &rs, &ws, &es, &max);
1371 if (mret != CURLM_OK)
1373 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1374 "%s failed at %s:%d: `%s'\n",
1375 "curl_multi_fdset", __FILE__, __LINE__,
1376 curl_multi_strerror (mret));
1377 //TODO cleanup here?
1381 mret = curl_multi_timeout (curl_multi, &to);
1382 rtime = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, to);
1384 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1385 "cURL multi fds: max=%d timeout=%llu\n", max, to);
1387 grs = GNUNET_NETWORK_fdset_create ();
1388 gws = GNUNET_NETWORK_fdset_create ();
1389 GNUNET_NETWORK_fdset_copy_native (grs, &rs, max + 1);
1390 GNUNET_NETWORK_fdset_copy_native (gws, &ws, max + 1);
1391 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1392 "Scheduling task cURL\n");
1394 if (curl_download_task != GNUNET_SCHEDULER_NO_TASK)
1395 GNUNET_SCHEDULER_cancel (curl_download_task);
1399 curl_download_task =
1400 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1403 &curl_task_download, curl_multi);
1405 else if (NULL != ctasks_head)
1407 /* as specified in curl docs */
1408 curl_download_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MILLISECONDS,
1409 &curl_task_download,
1412 GNUNET_NETWORK_fdset_destroy (gws);
1413 GNUNET_NETWORK_fdset_destroy (grs);
1419 * Task that is run when we are ready to receive more data
1422 * @param cls closure
1423 * @param tc task context
1426 curl_task_download (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1430 struct CURLMsg *msg;
1432 struct ProxyCurlTask *ctask;
1436 struct ProxyCurlTask *clean_head = NULL;
1437 struct ProxyCurlTask *clean_tail = NULL;
1439 curl_download_task = GNUNET_SCHEDULER_NO_TASK;
1441 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1443 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1444 "Shutdown requested while trying to download\n");
1448 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1456 mret = curl_multi_perform (curl_multi, &running);
1458 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1459 "Running curl tasks: %d\n", running);
1461 ctask = ctasks_head;
1462 for (; ctask != NULL; ctask = ctask->next)
1464 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1465 "CTask: %s\n", ctask->url);
1469 if (num_ctasks != running)
1471 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1472 "%d ctasks, %d curl running\n", num_ctasks, running);
1477 ctask = ctasks_head;
1478 msg = curl_multi_info_read (curl_multi, &msgnum);
1479 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1480 "Messages left: %d\n", msgnum);
1487 if ((msg->data.result != CURLE_OK) &&
1488 (msg->data.result != CURLE_GOT_NOTHING))
1490 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1491 "Download curl failed");
1493 for (; ctask != NULL; ctask = ctask->next)
1495 if (NULL == ctask->curl)
1498 if (memcmp (msg->easy_handle, ctask->curl, sizeof (CURL)) != 0)
1501 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1502 "CURL: Download failed for task %s: %s.\n",
1504 curl_easy_strerror (msg->data.result));
1505 ctask->download_is_finished = GNUNET_YES;
1506 ctask->download_error = GNUNET_YES;
1507 if (CURLE_OK == curl_easy_getinfo (ctask->curl,
1508 CURLINFO_RESPONSE_CODE,
1510 ctask->curl_response_code = resp_code;
1511 ctask->ready_to_queue = MHD_YES;
1512 ctask->buf_status = BUF_WAIT_FOR_MHD;
1513 run_mhd_now (ctask->mhd);
1515 GNUNET_CONTAINER_DLL_remove (ctasks_head, ctasks_tail,
1517 GNUNET_CONTAINER_DLL_insert (clean_head, clean_tail, ctask);
1520 GNUNET_assert (ctask != NULL);
1524 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1525 "CURL: download completed.\n");
1527 for (; ctask != NULL; ctask = ctask->next)
1529 if (NULL == ctask->curl)
1532 if (memcmp (msg->easy_handle, ctask->curl, sizeof (CURL)) != 0)
1535 if (ctask->buf_status != BUF_WAIT_FOR_CURL)
1538 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1539 "CURL: completed task %s found.\n", ctask->url);
1540 if (CURLE_OK == curl_easy_getinfo (ctask->curl,
1541 CURLINFO_RESPONSE_CODE,
1543 ctask->curl_response_code = resp_code;
1546 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1547 "CURL: Completed ctask!\n");
1548 if (GNUNET_SCHEDULER_NO_TASK == ctask->pp_task)
1550 ctask->buf_status = BUF_WAIT_FOR_PP;
1551 ctask->pp_task = GNUNET_SCHEDULER_add_now (&postprocess_buffer,
1555 ctask->ready_to_queue = MHD_YES;
1556 ctask->download_is_finished = GNUNET_YES;
1558 /* We MUST not modify the multi handle else we loose messages */
1559 GNUNET_CONTAINER_DLL_remove (ctasks_head, ctasks_tail,
1561 GNUNET_CONTAINER_DLL_insert (clean_head, clean_tail, ctask);
1565 GNUNET_assert (ctask != NULL);
1567 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1568 "CURL: %s\n", curl_easy_strerror(msg->data.result));
1574 } while (msgnum > 0);
1576 for (ctask=clean_head; ctask != NULL; ctask = ctask->next)
1578 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1579 "CURL: Removing task %s.\n", ctask->url);
1580 curl_multi_remove_handle (curl_multi, ctask->curl);
1581 curl_easy_cleanup (ctask->curl);
1586 for (ctask=ctasks_head; ctask != NULL; ctask = ctask->next)
1591 if (num_ctasks != running)
1593 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1594 "CURL: %d tasks, %d running\n", num_ctasks, running);
1597 GNUNET_assert ( num_ctasks == running );
1599 } while (mret == CURLM_CALL_MULTI_PERFORM);
1601 if (mret != CURLM_OK)
1603 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "CURL: %s failed at %s:%d: `%s'\n",
1604 "curl_multi_perform", __FILE__, __LINE__,
1605 curl_multi_strerror (mret));
1607 curl_download_prepare();
1611 * Process LEHO lookup
1613 * @param cls the ctask
1614 * @param rd_count number of records returned
1615 * @param rd record data
1618 process_leho_lookup (void *cls,
1620 const struct GNUNET_NAMESTORE_RecordData *rd)
1622 struct ProxyCurlTask *ctask = cls;
1623 char hosthdr[262]; //256 + "Host: "
1627 struct hostent *phost;
1629 char resolvename[512];
1632 strcpy (ctask->leho, "");
1635 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1636 "No LEHO present!\n");
1638 for (i=0; i<rd_count; i++)
1640 if (rd[i].record_type != GNUNET_GNS_RECORD_LEHO)
1643 memcpy (ctask->leho, rd[i].data, rd[i].data_size);
1645 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1646 "Found LEHO %s for %s\n", ctask->leho, ctask->url);
1649 if (0 != strcmp (ctask->leho, ""))
1651 sprintf (hosthdr, "%s%s:%d", "Host: ", ctask->leho, ctask->port);
1652 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1653 "New HTTP header value: %s\n", hosthdr);
1654 ctask->headers = curl_slist_append (ctask->headers, hosthdr);
1655 GNUNET_assert (NULL != ctask->headers);
1656 ret = curl_easy_setopt (ctask->curl, CURLOPT_HTTPHEADER, ctask->headers);
1657 if (CURLE_OK != ret)
1659 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "%s failed at %s:%d: `%s'\n",
1660 "curl_easy_setopt", __FILE__, __LINE__, curl_easy_strerror(ret));
1665 if (ctask->mhd->is_ssl)
1667 phost = (struct hostent*)gethostbyname (ctask->host);
1671 ssl_ip = inet_ntoa(*((struct in_addr*)(phost->h_addr)));
1672 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1673 "SSL target server: %s\n", ssl_ip);
1674 sprintf (resolvename, "%s:%d:%s", ctask->leho, HTTPS_PORT, ssl_ip);
1675 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1676 "Curl resolve: %s\n", resolvename);
1677 ctask->resolver = curl_slist_append ( ctask->resolver, resolvename);
1678 curl_easy_setopt (ctask->curl, CURLOPT_RESOLVE, ctask->resolver);
1679 sprintf (curlurl, "https://%s:%d%s", ctask->leho, ctask->port, ctask->url);
1680 curl_easy_setopt (ctask->curl, CURLOPT_URL, curlurl);
1684 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1685 "gethostbyname failed for %s!\n", ctask->host);
1686 ctask->download_is_finished = GNUNET_YES;
1687 ctask->download_error = GNUNET_YES;
1692 if (CURLM_OK != (mret=curl_multi_add_handle (curl_multi, ctask->curl)))
1694 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1695 "%s failed at %s:%d: `%s'\n",
1696 "curl_multi_add_handle", __FILE__, __LINE__,
1697 curl_multi_strerror (mret));
1698 ctask->download_is_finished = GNUNET_YES;
1699 ctask->download_error = GNUNET_YES;
1702 GNUNET_CONTAINER_DLL_insert (ctasks_head, ctasks_tail, ctask);
1704 curl_download_prepare ();
1709 * Initialize download and trigger curl
1711 * @param cls the proxycurltask
1712 * @param auth_name the name of the authority (site of origin) of ctask->host
1716 process_get_authority (void *cls,
1717 const char* auth_name)
1719 struct ProxyCurlTask *ctask = cls;
1721 if (NULL == auth_name)
1723 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1724 "Get authority failed!\n");
1725 strcpy (ctask->authority, "");
1729 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1730 "Get authority yielded %s\n", auth_name);
1731 strcpy (ctask->authority, auth_name);
1734 GNUNET_GNS_lookup_zone (gns_handle,
1737 GNUNET_GNS_RECORD_LEHO,
1738 GNUNET_YES, //Only cached for performance
1740 &process_leho_lookup,
1746 * Main MHD callback for handling requests.
1749 * @param con MHD connection handle
1750 * @param url the url in the request
1751 * @param meth the HTTP method used ("GET", "PUT", etc.)
1752 * @param ver the HTTP version string (i.e. "HTTP/1.1")
1753 * @param upload_data the data being uploaded (excluding HEADERS,
1754 * for a POST that fits into memory and that is encoded
1755 * with a supported encoding, the POST data will NOT be
1756 * given in upload_data and is instead available as
1757 * part of MHD_get_connection_values; very large POST
1758 * data *will* be made available incrementally in
1760 * @param upload_data_size set initially to the size of the
1761 * upload_data provided; the method must update this
1762 * value to the number of bytes NOT processed;
1763 * @param con_cls pointer to location where we store the 'struct Request'
1764 * @return MHD_YES if the connection was handled successfully,
1765 * MHD_NO if the socket must be closed due to a serious
1766 * error while handling the request
1769 create_response (void *cls,
1770 struct MHD_Connection *con,
1774 const char *upload_data,
1775 size_t *upload_data_size,
1778 struct MhdHttpList* hd = cls;
1779 const char* page = "<html><head><title>gnoxy</title>"\
1780 "</head><body>cURL fail</body></html>";
1782 char curlurl[MAX_HTTP_URI_LENGTH]; // buffer overflow!
1785 struct ProxyCurlTask *ctask;
1786 struct ProxyPostData *fin_post;
1789 if ((0 != strcasecmp (meth, MHD_HTTP_METHOD_GET)) &&
1790 (0 != strcasecmp (meth, MHD_HTTP_METHOD_PUT)) &&
1791 (0 != strcasecmp (meth, MHD_HTTP_METHOD_POST)))
1793 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1794 "MHD: %s NOT IMPLEMENTED!\n", meth);
1799 if (NULL == *con_cls)
1802 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1803 "Got %s request for %s\n", meth, url);
1804 ctask = GNUNET_malloc (sizeof (struct ProxyCurlTask));
1808 ctask->curl = curl_easy_init();
1809 if (NULL == ctask->curl)
1811 ctask->response = MHD_create_response_from_buffer (strlen (page),
1813 MHD_RESPMEM_PERSISTENT);
1814 ret = MHD_queue_response (con,
1817 MHD_destroy_response (ctask->response);
1818 GNUNET_free (ctask);
1822 ctask->download_in_progress = GNUNET_YES;
1823 ctask->buf_status = BUF_WAIT_FOR_CURL;
1824 ctask->connection = con;
1825 ctask->curl_response_code = MHD_HTTP_OK;
1826 ctask->buffer_read_ptr = ctask->buffer;
1827 ctask->buffer_write_ptr = ctask->buffer;
1828 ctask->pp_task = GNUNET_SCHEDULER_NO_TASK;
1829 ctask->port = HTTP_PORT;
1831 MHD_get_connection_values (con,
1833 &con_val_iter, ctask);
1835 if (0 == strcasecmp (meth, MHD_HTTP_METHOD_PUT))
1837 if (0 == *upload_data_size)
1839 curl_easy_cleanup (ctask->curl);
1840 GNUNET_free (ctask);
1843 ctask->put_read_offset = 0;
1844 ctask->put_read_size = *upload_data_size;
1845 curl_easy_setopt (ctask->curl, CURLOPT_UPLOAD, 1);
1846 curl_easy_setopt (ctask->curl, CURLOPT_READDATA, upload_data);
1847 //curl_easy_setopt (ctask->curl, CURLOPT_READFUNCTION, &curl_read_cb);
1849 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1850 "Got PUT data: %s\n", upload_data);
1851 curl_easy_cleanup (ctask->curl);
1852 GNUNET_free (ctask);
1856 if (0 == strcasecmp (meth, MHD_HTTP_METHOD_POST))
1858 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1859 "Setting up POST processor\n");
1860 ctask->post_handler = MHD_create_post_processor (con,
1862 &con_post_data_iter,
1864 curl_easy_setopt (ctask->curl, CURLOPT_POST, 1);
1865 curl_easy_setopt (ctask->curl, CURLOPT_READFUNCTION,
1867 curl_easy_setopt (ctask->curl, CURLOPT_READDATA, ctask);
1868 ctask->headers = curl_slist_append (ctask->headers,
1869 "Transfer-Encoding: chunked");
1870 /*curl_easy_setopt (ctask->curl, CURLOPT_POST, 1);
1871 curl_easy_setopt (ctask->curl, CURLOPT_POSTFIELDSIZE, *upload_data_size);
1872 curl_easy_setopt (ctask->curl, CURLOPT_COPYPOSTFIELDS, upload_data);
1874 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1875 "Got POST data: %s\n", upload_data);
1876 curl_easy_cleanup (ctask->curl);
1877 GNUNET_free (ctask);
1881 curl_easy_setopt (ctask->curl, CURLOPT_HEADERFUNCTION, &curl_check_hdr);
1882 curl_easy_setopt (ctask->curl, CURLOPT_HEADERDATA, ctask);
1883 curl_easy_setopt (ctask->curl, CURLOPT_WRITEFUNCTION, &curl_download_cb);
1884 curl_easy_setopt (ctask->curl, CURLOPT_WRITEDATA, ctask);
1885 curl_easy_setopt (ctask->curl, CURLOPT_FOLLOWLOCATION, 0);
1886 curl_easy_setopt (ctask->curl, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
1888 if (GNUNET_NO == ctask->mhd->is_ssl)
1890 sprintf (curlurl, "http://%s:%d%s", ctask->host, ctask->port, url);
1891 MHD_get_connection_values (con,
1892 MHD_GET_ARGUMENT_KIND,
1893 &get_uri_val_iter, curlurl);
1894 curl_easy_setopt (ctask->curl, CURLOPT_URL, curlurl);
1896 strcpy (ctask->url, url);
1897 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1898 "MHD: Adding new curl task for %s%s\n", ctask->host, url);
1899 MHD_get_connection_values (con,
1900 MHD_GET_ARGUMENT_KIND,
1901 &get_uri_val_iter, ctask->url);
1903 curl_easy_setopt (ctask->curl, CURLOPT_FAILONERROR, 1);
1904 curl_easy_setopt (ctask->curl, CURLOPT_CONNECTTIMEOUT, 600L);
1905 curl_easy_setopt (ctask->curl, CURLOPT_TIMEOUT, 600L);
1907 GNUNET_GNS_get_authority (gns_handle,
1909 &process_get_authority,
1911 ctask->ready_to_queue = GNUNET_NO;
1912 ctask->fin = GNUNET_NO;
1916 ctask = (struct ProxyCurlTask *) *con_cls;
1917 if (0 == strcasecmp (meth, MHD_HTTP_METHOD_POST))
1919 if (0 != *upload_data_size)
1921 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1922 "Invoking POST processor\n");
1923 MHD_post_process (ctask->post_handler,
1924 upload_data, *upload_data_size);
1925 *upload_data_size = 0;
1928 else if (GNUNET_NO == ctask->post_done)
1930 fin_post = GNUNET_malloc (sizeof (struct ProxyPostData));
1931 GNUNET_CONTAINER_DLL_insert_tail (ctask->post_data_head,
1932 ctask->post_data_tail,
1934 ctask->post_done = GNUNET_YES;
1939 if (GNUNET_YES != ctask->ready_to_queue)
1940 return MHD_YES; /* wait longer */
1942 if (GNUNET_YES == ctask->fin)
1945 ctask->fin = GNUNET_YES;
1946 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1947 "MHD: Queueing response for %s\n", ctask->url);
1948 ret = MHD_queue_response (con, ctask->curl_response_code, ctask->response);
1949 run_mhd_now (ctask->mhd);
1960 struct MhdHttpList *hd;
1962 for (hd=mhd_httpd_head; NULL != hd; hd = hd->next)
1970 * @param hd the daemon to run
1973 run_httpd (struct MhdHttpList *hd)
1978 struct GNUNET_NETWORK_FDSet *wrs;
1979 struct GNUNET_NETWORK_FDSet *wws;
1980 struct GNUNET_NETWORK_FDSet *wes;
1983 unsigned MHD_LONG_LONG timeout;
1984 struct GNUNET_TIME_Relative tv;
1989 wrs = GNUNET_NETWORK_fdset_create ();
1990 wes = GNUNET_NETWORK_fdset_create ();
1991 wws = GNUNET_NETWORK_fdset_create ();
1993 GNUNET_assert (MHD_YES == MHD_get_fdset (hd->daemon, &rs, &ws, &es, &max));
1996 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1997 "MHD fds: max=%d\n", max);
1999 haveto = MHD_get_timeout (hd->daemon, &timeout);
2001 if (MHD_YES == haveto)
2002 tv.rel_value = (uint64_t) timeout;
2004 tv = GNUNET_TIME_UNIT_FOREVER_REL;
2005 GNUNET_NETWORK_fdset_copy_native (wrs, &rs, max + 1);
2006 GNUNET_NETWORK_fdset_copy_native (wws, &ws, max + 1);
2007 GNUNET_NETWORK_fdset_copy_native (wes, &es, max + 1);
2009 if (GNUNET_SCHEDULER_NO_TASK != hd->httpd_task)
2010 GNUNET_SCHEDULER_cancel (hd->httpd_task);
2012 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_HIGH,
2015 GNUNET_NETWORK_fdset_destroy (wrs);
2016 GNUNET_NETWORK_fdset_destroy (wws);
2017 GNUNET_NETWORK_fdset_destroy (wes);
2022 * Task run whenever HTTP server operations are pending.
2025 * @param tc sched context
2028 do_httpd (void *cls,
2029 const struct GNUNET_SCHEDULER_TaskContext *tc)
2031 struct MhdHttpList *hd = cls;
2033 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2034 "MHD: Main loop\n");
2035 hd->httpd_task = GNUNET_SCHEDULER_NO_TASK;
2036 MHD_run (hd->daemon);
2043 * Read data from socket
2045 * @param cls the closure
2046 * @param tc scheduler context
2049 do_read (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
2052 * Read from remote end
2054 * @param cls closure
2055 * @param tc scheduler context
2058 do_read_remote (void* cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
2061 * Write data to remote socket
2063 * @param cls the closure
2064 * @param tc scheduler context
2067 do_write_remote (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2069 struct Socks5Request *s5r = cls;
2072 s5r->fwdwtask = GNUNET_SCHEDULER_NO_TASK;
2074 if ((NULL != tc->read_ready) &&
2075 (GNUNET_NETWORK_fdset_isset (tc->write_ready, s5r->remote_sock)) &&
2076 ((len = GNUNET_NETWORK_socket_send (s5r->remote_sock, s5r->rbuf,
2079 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2080 "Successfully sent %d bytes to remote socket\n",
2085 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "write remote");
2086 if (GNUNET_SCHEDULER_NO_TASK != s5r->rtask)
2087 GNUNET_SCHEDULER_cancel (s5r->rtask);
2088 if (GNUNET_SCHEDULER_NO_TASK != s5r->wtask)
2089 GNUNET_SCHEDULER_cancel (s5r->wtask);
2090 if (GNUNET_SCHEDULER_NO_TASK != s5r->fwdrtask)
2091 GNUNET_SCHEDULER_cancel (s5r->fwdrtask);
2092 GNUNET_NETWORK_socket_close (s5r->remote_sock);
2093 GNUNET_NETWORK_socket_close (s5r->sock);
2099 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
2106 * Clean up s5r handles
2108 * @param s5r the handle to destroy
2111 cleanup_s5r (struct Socks5Request *s5r)
2113 if (GNUNET_SCHEDULER_NO_TASK != s5r->rtask)
2114 GNUNET_SCHEDULER_cancel (s5r->rtask);
2115 if (GNUNET_SCHEDULER_NO_TASK != s5r->fwdwtask)
2116 GNUNET_SCHEDULER_cancel (s5r->fwdwtask);
2117 if (GNUNET_SCHEDULER_NO_TASK != s5r->fwdrtask)
2118 GNUNET_SCHEDULER_cancel (s5r->fwdrtask);
2120 if (NULL != s5r->remote_sock)
2121 GNUNET_NETWORK_socket_close (s5r->remote_sock);
2122 if ((NULL != s5r->sock) && (s5r->cleanup_sock == GNUNET_YES))
2123 GNUNET_NETWORK_socket_close (s5r->sock);
2129 * Write data to socket
2131 * @param cls the closure
2132 * @param tc scheduler context
2135 do_write (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2137 struct Socks5Request *s5r = cls;
2140 s5r->wtask = GNUNET_SCHEDULER_NO_TASK;
2142 if ((NULL != tc->read_ready) &&
2143 (GNUNET_NETWORK_fdset_isset (tc->write_ready, s5r->sock)) &&
2144 ((len = GNUNET_NETWORK_socket_send (s5r->sock, s5r->wbuf,
2147 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2148 "Successfully sent %d bytes to socket\n",
2154 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "write");
2155 s5r->cleanup = GNUNET_YES;
2156 s5r->cleanup_sock = GNUNET_YES;
2162 if (GNUNET_YES == s5r->cleanup)
2168 if ((s5r->state == SOCKS5_DATA_TRANSFER) &&
2169 (s5r->fwdrtask == GNUNET_SCHEDULER_NO_TASK))
2171 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
2173 &do_read_remote, s5r);
2177 * Read from remote end
2179 * @param cls closure
2180 * @param tc scheduler context
2183 do_read_remote (void* cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2185 struct Socks5Request *s5r = cls;
2187 s5r->fwdrtask = GNUNET_SCHEDULER_NO_TASK;
2190 if ((NULL != tc->write_ready) &&
2191 (GNUNET_NETWORK_fdset_isset (tc->read_ready, s5r->remote_sock)) &&
2192 (s5r->wbuf_len = GNUNET_NETWORK_socket_recv (s5r->remote_sock, s5r->wbuf,
2193 sizeof (s5r->wbuf))))
2195 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2196 "Successfully read %d bytes from remote socket\n",
2201 if (s5r->wbuf_len == 0)
2202 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2203 "0 bytes received from remote... graceful shutdown!\n");
2204 if (s5r->fwdwtask != GNUNET_SCHEDULER_NO_TASK)
2205 GNUNET_SCHEDULER_cancel (s5r->fwdwtask);
2206 if (s5r->rtask != GNUNET_SCHEDULER_NO_TASK)
2207 GNUNET_SCHEDULER_cancel (s5r->rtask);
2209 GNUNET_NETWORK_socket_close (s5r->remote_sock);
2210 s5r->remote_sock = NULL;
2211 GNUNET_NETWORK_socket_close (s5r->sock);
2217 s5r->wtask = GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
2225 * Adds a socket to MHD
2227 * @param h the handle to the socket to add
2228 * @param daemon the daemon to add the fd to
2229 * @return whatever MHD_add_connection returns
2232 add_handle_to_mhd (struct GNUNET_NETWORK_Handle *h, struct MHD_Daemon *daemon)
2235 struct sockaddr *addr;
2238 fd = dup (GNUNET_NETWORK_get_fd (h));
2239 addr = GNUNET_NETWORK_get_addr (h);
2240 len = GNUNET_NETWORK_get_addrlen (h);
2242 return MHD_add_connection (daemon, fd, addr, len);
2246 * Read file in filename
2248 * @param filename file to read
2249 * @param size pointer where filesize is stored
2253 load_file (const char* filename,
2260 GNUNET_DISK_file_size (filename, &fsize,
2261 GNUNET_YES, GNUNET_YES))
2263 if (fsize > MAX_PEM_SIZE)
2265 *size = (unsigned int) fsize;
2266 buffer = GNUNET_malloc (*size);
2267 if (fsize != GNUNET_DISK_fn_read (filename, buffer, (size_t) fsize))
2269 GNUNET_free (buffer);
2277 * Load PEM key from file
2279 * @param key where to store the data
2280 * @param keyfile path to the PEM file
2281 * @return GNUNET_OK on success
2284 load_key_from_file (gnutls_x509_privkey_t key, const char* keyfile)
2286 gnutls_datum_t key_data;
2289 key_data.data = (unsigned char*) load_file (keyfile, &key_data.size);
2290 ret = gnutls_x509_privkey_import (key, &key_data,
2291 GNUTLS_X509_FMT_PEM);
2292 if (GNUTLS_E_SUCCESS != ret)
2294 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2295 _("Unable to import private key from file `%s'\n"),
2299 GNUNET_free (key_data.data);
2300 return (GNUTLS_E_SUCCESS != ret) ? GNUNET_SYSERR : GNUNET_OK;
2305 * Load cert from file
2307 * @param crt struct to store data in
2308 * @param certfile path to pem file
2309 * @return GNUNET_OK on success
2312 load_cert_from_file (gnutls_x509_crt_t crt, char* certfile)
2314 gnutls_datum_t cert_data;
2315 cert_data.data = NULL;
2318 cert_data.data = (unsigned char*) load_file (certfile, &cert_data.size);
2319 ret = gnutls_x509_crt_import (crt, &cert_data,
2320 GNUTLS_X509_FMT_PEM);
2321 if (GNUTLS_E_SUCCESS != ret)
2323 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2324 _("Unable to import certificate %s\n"), certfile);
2327 GNUNET_free (cert_data.data);
2328 return (GNUTLS_E_SUCCESS != ret) ? GNUNET_SYSERR : GNUNET_OK;
2333 * Generate new certificate for specific name
2335 * @param name the subject name to generate a cert for
2336 * @return a struct holding the PEM data
2338 static struct ProxyGNSCertificate *
2339 generate_gns_certificate (const char *name)
2343 unsigned int serial;
2344 size_t key_buf_size;
2345 size_t cert_buf_size;
2346 gnutls_x509_crt_t request;
2350 ret = gnutls_x509_crt_init (&request);
2352 if (GNUTLS_E_SUCCESS != ret)
2357 ret = gnutls_x509_crt_set_key (request, proxy_ca.key);
2359 if (GNUTLS_E_SUCCESS != ret)
2364 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Generating cert\n");
2366 struct ProxyGNSCertificate *pgc =
2367 GNUNET_malloc (sizeof (struct ProxyGNSCertificate));
2369 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Adding DNs\n");
2371 gnutls_x509_crt_set_dn_by_oid (request, GNUTLS_OID_X520_COUNTRY_NAME,
2374 gnutls_x509_crt_set_dn_by_oid (request, GNUTLS_OID_X520_ORGANIZATION_NAME,
2377 gnutls_x509_crt_set_dn_by_oid (request, GNUTLS_OID_X520_COMMON_NAME,
2378 0, name, strlen (name));
2380 ret = gnutls_x509_crt_set_version (request, 3);
2382 ret = gnutls_rnd (GNUTLS_RND_NONCE, &serial, sizeof (serial));
2384 etime = time (NULL);
2385 tm_data = localtime (&etime);
2388 ret = gnutls_x509_crt_set_serial (request,
2392 ret = gnutls_x509_crt_set_activation_time (request,
2395 etime = mktime (tm_data);
2402 ret = gnutls_x509_crt_set_expiration_time (request,
2404 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Signing...\n");
2406 ret = gnutls_x509_crt_sign (request, proxy_ca.cert, proxy_ca.key);
2408 key_buf_size = sizeof (pgc->key);
2409 cert_buf_size = sizeof (pgc->cert);
2411 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Exporting certificate...\n");
2413 gnutls_x509_crt_export (request, GNUTLS_X509_FMT_PEM,
2414 pgc->cert, &cert_buf_size);
2416 gnutls_x509_privkey_export (proxy_ca.key, GNUTLS_X509_FMT_PEM,
2417 pgc->key, &key_buf_size);
2420 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Cleaning up\n");
2421 gnutls_x509_crt_deinit (request);
2429 * Accept policy for mhdaemons
2432 * @param addr the sockaddr
2433 * @param addrlen the sockaddr length
2434 * @return MHD_NO if sockaddr is wrong or #conns too high
2437 accept_cb (void* cls, const struct sockaddr *addr, socklen_t addrlen)
2439 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2440 "In MHD accept policy cb\n");
2444 if (addr->sa_family == AF_UNIX)
2448 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2449 "Connection accepted\n");
2456 * Adds a socket to an SSL MHD instance
2457 * It is important the the domain name is
2458 * correct. In most cases we need to start a new daemon
2460 * @param h the handle to add to a daemon
2461 * @param domain the domain the ssl daemon has to serve
2462 * @return MHD_YES on success
2465 add_handle_to_ssl_mhd (struct GNUNET_NETWORK_Handle *h, const char* domain)
2467 struct MhdHttpList *hd = NULL;
2468 struct ProxyGNSCertificate *pgc;
2469 struct NetworkHandleList *nh;
2471 for (hd = mhd_httpd_head; NULL != hd; hd = hd->next)
2472 if (0 == strcmp (hd->domain, domain))
2477 pgc = generate_gns_certificate (domain);
2479 hd = GNUNET_malloc (sizeof (struct MhdHttpList));
2480 hd->is_ssl = GNUNET_YES;
2481 strcpy (hd->domain, domain);
2482 hd->proxy_cert = pgc;
2485 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2486 "No previous SSL instance found... starting new one for %s\n",
2489 hd->daemon = MHD_start_daemon (MHD_USE_DEBUG | MHD_USE_SSL, 4444,
2491 &create_response, hd,
2492 MHD_OPTION_LISTEN_SOCKET, GNUNET_NETWORK_get_fd (mhd_unix_socket),
2493 MHD_OPTION_CONNECTION_LIMIT, MHD_MAX_CONNECTIONS,
2494 MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) 16,
2495 MHD_OPTION_NOTIFY_COMPLETED,
2497 MHD_OPTION_HTTPS_MEM_KEY, pgc->key,
2498 MHD_OPTION_HTTPS_MEM_CERT, pgc->cert,
2501 GNUNET_assert (hd->daemon != NULL);
2502 hd->httpd_task = GNUNET_SCHEDULER_NO_TASK;
2504 GNUNET_CONTAINER_DLL_insert (mhd_httpd_head, mhd_httpd_tail, hd);
2507 nh = GNUNET_malloc (sizeof (struct NetworkHandleList));
2510 GNUNET_CONTAINER_DLL_insert (hd->socket_handles_head,
2511 hd->socket_handles_tail,
2514 return add_handle_to_mhd (h, hd->daemon);
2520 * Read data from incoming connection
2522 * @param cls the closure
2523 * @param tc the scheduler context
2526 do_read (void* cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2528 struct Socks5Request *s5r = cls;
2529 struct socks5_client_hello *c_hello;
2530 struct socks5_server_hello *s_hello;
2531 struct socks5_client_request *c_req;
2532 struct socks5_server_response *s_resp;
2538 struct hostent *phost;
2540 struct sockaddr_in remote_addr;
2541 struct in_addr *r_sin_addr;
2543 struct NetworkHandleList *nh;
2545 s5r->rtask = GNUNET_SCHEDULER_NO_TASK;
2547 if ((NULL != tc->write_ready) &&
2548 (GNUNET_NETWORK_fdset_isset (tc->read_ready, s5r->sock)) &&
2549 (s5r->rbuf_len = GNUNET_NETWORK_socket_recv (s5r->sock, s5r->rbuf,
2550 sizeof (s5r->rbuf))))
2552 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2553 "Successfully read %d bytes from socket\n",
2558 if (s5r->rbuf_len != 0)
2559 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "read");
2561 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client disco!\n");
2563 if (s5r->fwdrtask != GNUNET_SCHEDULER_NO_TASK)
2564 GNUNET_SCHEDULER_cancel (s5r->fwdrtask);
2565 if (s5r->wtask != GNUNET_SCHEDULER_NO_TASK)
2566 GNUNET_SCHEDULER_cancel (s5r->wtask);
2567 if (s5r->fwdwtask != GNUNET_SCHEDULER_NO_TASK)
2568 GNUNET_SCHEDULER_cancel (s5r->fwdwtask);
2569 GNUNET_NETWORK_socket_close (s5r->remote_sock);
2570 GNUNET_NETWORK_socket_close (s5r->sock);
2575 if (s5r->state == SOCKS5_INIT)
2577 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2579 c_hello = (struct socks5_client_hello*)&s5r->rbuf;
2581 GNUNET_assert (c_hello->version == SOCKS_VERSION_5);
2583 s_hello = (struct socks5_server_hello*)&s5r->wbuf;
2584 s5r->wbuf_len = sizeof( struct socks5_server_hello );
2586 s_hello->version = c_hello->version;
2587 s_hello->auth_method = SOCKS_AUTH_NONE;
2589 /* Write response to client */
2590 s5r->wtask = GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
2594 s5r->rtask = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
2598 s5r->state = SOCKS5_REQUEST;
2602 if (s5r->state == SOCKS5_REQUEST)
2604 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2605 "Processing SOCKS5 request\n");
2606 c_req = (struct socks5_client_request*)&s5r->rbuf;
2607 s_resp = (struct socks5_server_response*)&s5r->wbuf;
2608 //Only 10byte for ipv4 response!
2609 s5r->wbuf_len = 10;//sizeof (struct socks5_server_response);
2611 GNUNET_assert (c_req->addr_type == 3);
2613 dom_len = *((uint8_t*)(&(c_req->addr_type) + 1));
2614 memset(domain, 0, sizeof(domain));
2615 strncpy(domain, (char*)(&(c_req->addr_type) + 2), dom_len);
2616 req_port = *((uint16_t*)(&(c_req->addr_type) + 2 + dom_len));
2618 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2619 "Requested connection is %s:%d\n",
2623 if (is_tld (domain, GNUNET_GNS_TLD) ||
2624 is_tld (domain, GNUNET_GNS_TLD_ZKEY))
2626 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2627 "Requested connection is gnunet tld\n",
2631 if (ntohs(req_port) == HTTPS_PORT)
2633 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2634 "Requested connection is HTTPS\n");
2635 ret = add_handle_to_ssl_mhd ( s5r->sock, domain );
2637 else if (NULL != httpd)
2639 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2640 "Requested connection is HTTP\n");
2641 nh = GNUNET_malloc (sizeof (struct NetworkHandleList));
2644 GNUNET_CONTAINER_DLL_insert (mhd_httpd_head->socket_handles_head,
2645 mhd_httpd_head->socket_handles_tail,
2648 ret = add_handle_to_mhd ( s5r->sock, httpd );
2653 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2654 _("Failed to start HTTP server\n"));
2655 s_resp->version = 0x05;
2656 s_resp->reply = 0x01;
2657 s5r->cleanup = GNUNET_YES;
2658 s5r->cleanup_sock = GNUNET_YES;
2660 GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
2666 /* Signal success */
2667 s_resp->version = 0x05;
2668 s_resp->reply = 0x00;
2669 s_resp->reserved = 0x00;
2670 s_resp->addr_type = 0x01;
2672 s5r->cleanup = GNUNET_YES;
2673 s5r->cleanup_sock = GNUNET_NO;
2675 GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
2683 phost = (struct hostent*)gethostbyname (domain);
2686 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2687 "Resolve %s error!\n", domain );
2688 s_resp->version = 0x05;
2689 s_resp->reply = 0x01;
2690 s5r->cleanup = GNUNET_YES;
2691 s5r->cleanup_sock = GNUNET_YES;
2693 GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
2699 s5r->remote_sock = GNUNET_NETWORK_socket_create (AF_INET,
2702 r_sin_addr = (struct in_addr*)(phost->h_addr);
2703 remote_ip = r_sin_addr->s_addr;
2704 memset(&remote_addr, 0, sizeof(remote_addr));
2705 remote_addr.sin_family = AF_INET;
2706 #if HAVE_SOCKADDR_IN_SIN_LEN
2707 remote_addr.sin_len = sizeof (remote_addr);
2709 remote_addr.sin_addr.s_addr = remote_ip;
2710 remote_addr.sin_port = req_port;
2712 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2713 "target server: %s:%u\n", inet_ntoa(remote_addr.sin_addr),
2717 GNUNET_NETWORK_socket_connect ( s5r->remote_sock,
2718 (const struct sockaddr*)&remote_addr,
2719 sizeof (remote_addr)))
2720 && (errno != EINPROGRESS))
2722 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "connect");
2723 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2724 "socket request error...\n");
2725 s_resp->version = 0x05;
2726 s_resp->reply = 0x01;
2728 GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
2735 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2736 "new remote connection\n");
2738 s_resp->version = 0x05;
2739 s_resp->reply = 0x00;
2740 s_resp->reserved = 0x00;
2741 s_resp->addr_type = 0x01;
2743 s5r->state = SOCKS5_DATA_TRANSFER;
2746 GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
2750 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
2758 if (s5r->state == SOCKS5_DATA_TRANSFER)
2760 if ((s5r->remote_sock == NULL) || (s5r->rbuf_len == 0))
2762 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2763 "Closing connection to client\n");
2764 if (s5r->rtask != GNUNET_SCHEDULER_NO_TASK)
2765 GNUNET_SCHEDULER_cancel (s5r->rtask);
2766 if (s5r->fwdwtask != GNUNET_SCHEDULER_NO_TASK)
2767 GNUNET_SCHEDULER_cancel (s5r->fwdwtask);
2768 if (s5r->fwdrtask != GNUNET_SCHEDULER_NO_TASK)
2769 GNUNET_SCHEDULER_cancel (s5r->fwdrtask);
2770 if (s5r->fwdrtask != GNUNET_SCHEDULER_NO_TASK)
2771 GNUNET_SCHEDULER_cancel (s5r->fwdrtask);
2773 if (s5r->remote_sock != NULL)
2774 GNUNET_NETWORK_socket_close (s5r->remote_sock);
2775 GNUNET_NETWORK_socket_close (s5r->sock);
2780 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2781 "forwarding %d bytes from client\n", s5r->rbuf_len);
2784 GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
2786 &do_write_remote, s5r);
2788 if (s5r->fwdrtask == GNUNET_SCHEDULER_NO_TASK)
2791 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
2793 &do_read_remote, s5r);
2803 * Accept new incoming connections
2805 * @param cls the closure
2806 * @param tc the scheduler context
2809 do_accept (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2811 struct GNUNET_NETWORK_Handle *s;
2812 struct Socks5Request *s5r;
2814 ltask = GNUNET_SCHEDULER_NO_TASK;
2815 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
2818 ltask = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
2822 s = GNUNET_NETWORK_socket_accept (lsock, NULL, NULL);
2826 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "accept");
2830 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2831 "Got an inbound connection, waiting for data\n");
2833 s5r = GNUNET_malloc (sizeof (struct Socks5Request));
2835 s5r->state = SOCKS5_INIT;
2836 s5r->wtask = GNUNET_SCHEDULER_NO_TASK;
2837 s5r->fwdwtask = GNUNET_SCHEDULER_NO_TASK;
2838 s5r->fwdrtask = GNUNET_SCHEDULER_NO_TASK;
2839 s5r->rtask = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
2846 * Task run on shutdown
2848 * @param cls closure
2849 * @param tc task context
2852 do_shutdown (void *cls,
2853 const struct GNUNET_SCHEDULER_TaskContext *tc)
2856 struct MhdHttpList *hd;
2857 struct MhdHttpList *tmp_hd;
2858 struct NetworkHandleList *nh;
2859 struct NetworkHandleList *tmp_nh;
2860 struct ProxyCurlTask *ctask;
2861 struct ProxyCurlTask *ctask_tmp;
2863 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2864 "Shutting down...\n");
2866 gnutls_global_deinit ();
2868 if (NULL != local_gns_zone)
2869 GNUNET_free (local_gns_zone);
2870 if (NULL != local_private_zone)
2871 GNUNET_free (local_private_zone);
2872 if (NULL != local_shorten_zone)
2873 GNUNET_free (local_shorten_zone);
2875 if (GNUNET_SCHEDULER_NO_TASK != curl_download_task)
2877 GNUNET_SCHEDULER_cancel (curl_download_task);
2878 curl_download_task = GNUNET_SCHEDULER_NO_TASK;
2881 for (hd = mhd_httpd_head; hd != NULL; hd = tmp_hd)
2885 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2886 "Stopping daemon\n");
2888 if (GNUNET_SCHEDULER_NO_TASK != hd->httpd_task)
2890 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2891 "Stopping select task %d\n",
2893 GNUNET_SCHEDULER_cancel (hd->httpd_task);
2894 hd->httpd_task = GNUNET_SCHEDULER_NO_TASK;
2897 if (NULL != hd->daemon)
2899 MHD_stop_daemon (hd->daemon);
2903 for (nh = hd->socket_handles_head; nh != NULL; nh = tmp_nh)
2907 GNUNET_NETWORK_socket_close (nh->h);
2912 if (NULL != hd->proxy_cert)
2914 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2915 "Free certificate\n");
2916 GNUNET_free (hd->proxy_cert);
2922 for (ctask=ctasks_head; ctask != NULL; ctask=ctask_tmp)
2924 ctask_tmp = ctask->next;
2926 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2927 "Cleaning up cURL task\n");
2929 if (ctask->curl != NULL)
2930 curl_easy_cleanup (ctask->curl);
2932 if (NULL != ctask->headers)
2933 curl_slist_free_all (ctask->headers);
2934 if (NULL != ctask->resolver)
2935 curl_slist_free_all (ctask->resolver);
2937 if (NULL != ctask->response)
2938 MHD_destroy_response (ctask->response);
2941 GNUNET_free (ctask);
2943 curl_multi_cleanup (curl_multi);
2945 GNUNET_GNS_disconnect (gns_handle);
2950 * Compiles a regex for us
2952 * @param re ptr to re struct
2953 * @param rt the expression to compile
2954 * @return 0 on success
2957 compile_regex (regex_t *re, const char* rt)
2962 status = regcomp (re, rt, REG_EXTENDED|REG_NEWLINE);
2965 regerror (status, re, err, 1024);
2966 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2967 "Regex error compiling '%s': %s\n", rt, err);
2975 * Loads the users local zone key
2977 * @return GNUNET_YES on success
2980 load_local_zone_key (const struct GNUNET_CONFIGURATION_Handle *cfg)
2983 struct GNUNET_CRYPTO_RsaPrivateKey *key = NULL;
2984 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pkey;
2985 struct GNUNET_CRYPTO_ShortHashCode *zone = NULL;
2986 struct GNUNET_CRYPTO_ShortHashAsciiEncoded zonename;
2988 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "gns",
2989 "ZONEKEY", &keyfile))
2991 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2992 "Unable to load zone key config value!\n");
2996 if (GNUNET_NO == GNUNET_DISK_file_test (keyfile))
2998 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2999 "Unable to load zone key %s!\n", keyfile);
3000 GNUNET_free(keyfile);
3004 key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
3005 GNUNET_CRYPTO_rsa_key_get_public (key, &pkey);
3006 local_gns_zone = GNUNET_malloc (sizeof (struct GNUNET_CRYPTO_ShortHashCode));
3007 GNUNET_CRYPTO_short_hash(&pkey,
3008 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
3010 zone = local_gns_zone;
3011 GNUNET_CRYPTO_short_hash_to_enc (zone, &zonename);
3012 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3013 "Using zone: %s!\n", &zonename);
3014 GNUNET_CRYPTO_rsa_key_free(key);
3015 GNUNET_free(keyfile);
3018 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "gns",
3019 "PRIVATE_ZONEKEY", &keyfile))
3021 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3022 "Unable to load private zone key config value!\n");
3025 if ((NULL != keyfile) && (GNUNET_NO == GNUNET_DISK_file_test (keyfile)))
3027 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3028 "Unable to load private zone key %s!\n", keyfile);
3029 GNUNET_free(keyfile);
3033 key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
3034 GNUNET_CRYPTO_rsa_key_get_public (key, &pkey);
3035 local_private_zone = GNUNET_malloc (sizeof (struct GNUNET_CRYPTO_ShortHashCode));
3036 GNUNET_CRYPTO_short_hash(&pkey,
3037 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
3038 local_private_zone);
3039 GNUNET_CRYPTO_short_hash_to_enc (zone, &zonename);
3040 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3041 "Using private zone: %s!\n", &zonename);
3042 GNUNET_CRYPTO_rsa_key_free(key);
3043 GNUNET_free(keyfile);
3047 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "gns",
3048 "SHORTEN_ZONEKEY", &keyfile))
3050 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3051 "Unable to load shorten zone key config value!\n");
3054 if ((NULL != keyfile) && (GNUNET_NO == GNUNET_DISK_file_test (keyfile)))
3056 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3057 "Unable to load shorten zone key %s!\n", keyfile);
3058 GNUNET_free(keyfile);
3062 key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
3063 GNUNET_CRYPTO_rsa_key_get_public (key, &pkey);
3064 local_shorten_zone = GNUNET_malloc (sizeof (struct GNUNET_CRYPTO_ShortHashCode));
3065 GNUNET_CRYPTO_short_hash(&pkey,
3066 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
3067 local_shorten_zone);
3068 GNUNET_CRYPTO_short_hash_to_enc (zone, &zonename);
3069 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3070 "Using shorten zone: %s!\n", &zonename);
3071 GNUNET_CRYPTO_rsa_key_free(key);
3072 GNUNET_free(keyfile);
3079 * Main function that will be run
3081 * @param cls closure
3082 * @param args remaining command-line arguments
3083 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
3084 * @param cfg configuration
3087 run (void *cls, char *const *args, const char *cfgfile,
3088 const struct GNUNET_CONFIGURATION_Handle *cfg)
3090 struct sockaddr_in sa;
3091 struct MhdHttpList *hd;
3092 struct sockaddr_un mhd_unix_sock_addr;
3094 char* proxy_sockfile;
3095 char* cafile_cfg = NULL;
3098 curl_multi = curl_multi_init ();
3100 if (NULL == curl_multi)
3102 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3103 "Failed to create cURL multo handle!\n");
3107 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3109 cafile = cafile_opt;
3112 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "gns-proxy",
3116 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3117 "Unable to load proxy CA config value!\n");
3118 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3119 "No proxy CA provided!\n");
3122 cafile = cafile_cfg;
3124 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3125 "Using %s as CA\n", cafile);
3127 gnutls_global_init ();
3128 gnutls_x509_crt_init (&proxy_ca.cert);
3129 gnutls_x509_privkey_init (&proxy_ca.key);
3131 if ( (GNUNET_OK != load_cert_from_file (proxy_ca.cert, cafile)) ||
3132 (GNUNET_OK != load_key_from_file (proxy_ca.key, cafile)) )
3134 // FIXME: release resources...
3138 GNUNET_free_non_null (cafile_cfg);
3140 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3141 "Loading Template\n");
3143 compile_regex (&re_dotplus, (char*) RE_A_HREF);
3145 gns_handle = GNUNET_GNS_connect (cfg);
3147 if (GNUNET_NO == load_local_zone_key (cfg))
3149 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3150 "Unable to load zone!\n");
3154 if (NULL == gns_handle)
3156 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3157 "Unable to connect to GNS!\n");
3161 memset (&sa, 0, sizeof (sa));
3162 sa.sin_family = AF_INET;
3163 sa.sin_port = htons (port);
3164 #if HAVE_SOCKADDR_IN_SIN_LEN
3165 sa.sin_len = sizeof (sa);
3168 lsock = GNUNET_NETWORK_socket_create (AF_INET,
3172 if ((NULL == lsock) ||
3174 GNUNET_NETWORK_socket_bind (lsock, (const struct sockaddr *) &sa,
3177 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3178 "Failed to create listen socket bound to `%s'",
3179 GNUNET_a2s ((const struct sockaddr *) &sa, sizeof (sa)));
3181 GNUNET_NETWORK_socket_close (lsock);
3185 if (GNUNET_OK != GNUNET_NETWORK_socket_listen (lsock, 5))
3187 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3188 "Failed to listen on socket bound to `%s'",
3189 GNUNET_a2s ((const struct sockaddr *) &sa, sizeof (sa)));
3193 ltask = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
3194 lsock, &do_accept, NULL);
3199 if (0 != curl_global_init (CURL_GLOBAL_WIN32))
3201 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3202 "cURL global init failed!\n");
3206 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3207 "Proxy listens on port %u\n",
3210 mhd_httpd_head = NULL;
3211 mhd_httpd_tail = NULL;
3212 total_mhd_connections = 0;
3214 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "gns-proxy",
3218 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3219 "Specify PROXY_UNIXPATH in gns-proxy config section!\n");
3223 mhd_unix_socket = GNUNET_NETWORK_socket_create (AF_UNIX,
3227 if (NULL == mhd_unix_socket)
3229 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3230 "Unable to create unix domain socket!\n");
3234 mhd_unix_sock_addr.sun_family = AF_UNIX;
3235 strcpy (mhd_unix_sock_addr.sun_path, proxy_sockfile);
3238 mhd_unix_sock_addr.sun_path[0] = '\0';
3240 #if HAVE_SOCKADDR_IN_SIN_LEN
3241 mhd_unix_sock_addr.sun_len = (u_char) sizeof (struct sockaddr_un);
3244 len = strlen (proxy_sockfile) + sizeof(AF_UNIX);
3246 GNUNET_free (proxy_sockfile);
3248 if (GNUNET_OK != GNUNET_NETWORK_socket_bind (mhd_unix_socket,
3249 (struct sockaddr*)&mhd_unix_sock_addr,
3252 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3253 "Unable to bind unix domain socket!\n");
3257 if (GNUNET_OK != GNUNET_NETWORK_socket_listen (mhd_unix_socket,
3260 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3261 "Unable to listen on unix domain socket!\n");
3265 hd = GNUNET_malloc (sizeof (struct MhdHttpList));
3266 hd->is_ssl = GNUNET_NO;
3267 strcpy (hd->domain, "");
3268 httpd = MHD_start_daemon (MHD_USE_DEBUG, 4444, //Dummy port
3270 &create_response, hd,
3271 MHD_OPTION_LISTEN_SOCKET, GNUNET_NETWORK_get_fd (mhd_unix_socket),
3272 MHD_OPTION_CONNECTION_LIMIT, MHD_MAX_CONNECTIONS,
3273 MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) 16,
3274 MHD_OPTION_NOTIFY_COMPLETED,
3278 GNUNET_assert (httpd != NULL);
3280 hd->httpd_task = GNUNET_SCHEDULER_NO_TASK;
3282 GNUNET_CONTAINER_DLL_insert (mhd_httpd_head, mhd_httpd_tail, hd);
3286 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
3287 &do_shutdown, NULL);
3293 * The main function for gnunet-gns-proxy.
3295 * @param argc number of arguments from the command line
3296 * @param argv command line arguments
3297 * @return 0 ok, 1 on error
3300 main (int argc, char *const *argv)
3302 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
3304 gettext_noop ("listen on specified port"), 1,
3305 &GNUNET_GETOPT_set_string, &port},
3306 {'a', "authority", NULL,
3307 gettext_noop ("pem file to use as CA"), 1,
3308 &GNUNET_GETOPT_set_string, &cafile_opt},
3309 GNUNET_GETOPT_OPTION_END
3314 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
3318 GNUNET_log_setup ("gnunet-gns-proxy", "WARNING", NULL);
3321 GNUNET_PROGRAM_run (argc, argv, "gnunet-gns-proxy",
3322 _("GNUnet GNS proxy"),
3324 &run, NULL)) ? 0 : 1;
3325 GNUNET_free_non_null ((char*)argv);