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;
610 if (0 == strcmp ("Host", key))
612 port = strstr (value, ":");
615 strncpy (buf, value, port-value);
616 ctask->port = atoi (++port);
623 if (0 == strcmp ("Accept-Encoding", key))
628 if (0 == strcmp (MHD_HTTP_HEADER_CONTENT_TYPE,
631 if (0 == strcmp (value,
632 MHD_HTTP_POST_ENCODING_FORM_URLENCODED))
633 ctask->post_type = MHD_HTTP_POST_ENCODING_FORM_URLENCODED;
634 else if (0 == strcmp (value,
635 MHD_HTTP_POST_ENCODING_MULTIPART_FORMDATA))
636 ctask->post_type = MHD_HTTP_POST_ENCODING_MULTIPART_FORMDATA;
638 ctask->post_type = NULL;
642 cstr = GNUNET_malloc (strlen (key) + strlen (hdr_val) + 3);
643 GNUNET_snprintf (cstr, strlen (key) + strlen (hdr_val) + 3,
644 "%s: %s", key, hdr_val);
646 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
647 "Client Header: %s\n", cstr);
649 ctask->headers = curl_slist_append (ctask->headers, cstr);
657 * Callback for MHD response
660 * @param pos in buffer
662 * @param max space in buffer
663 * @return number of bytes written
666 mhd_content_cb (void *cls,
672 * Check HTTP response header for mime
674 * @param buffer curl buffer
675 * @param size curl blocksize
676 * @param nmemb curl blocknumber
678 * @return size of read bytes
681 curl_check_hdr (void *buffer, size_t size, size_t nmemb, void *cls)
683 size_t bytes = size * nmemb;
684 struct ProxyCurlTask *ctask = cls;
685 int html_mime_len = strlen (HTML_HDR_CONTENT);
686 int cookie_hdr_len = strlen (MHD_HTTP_HEADER_SET_COOKIE);
687 char hdr_mime[html_mime_len+1];
688 char hdr_generic[bytes+1];
689 char new_cookie_hdr[bytes+strlen (ctask->leho)+1];
690 char new_location[MAX_HTTP_URI_LENGTH+500];
701 if (NULL == ctask->response)
703 /* FIXME: get total size from curl (if available) */
704 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
705 "Creating response for %s\n", ctask->url);
706 ctask->response = MHD_create_response_from_callback (MHD_SIZE_UNKNOWN,
707 sizeof (ctask->buffer),
711 ctask->ready_to_queue = GNUNET_YES;
715 if (html_mime_len <= bytes)
717 memcpy (hdr_mime, buffer, html_mime_len);
718 hdr_mime[html_mime_len] = '\0';
720 if (0 == strcmp (hdr_mime, HTML_HDR_CONTENT))
722 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
723 "Got HTML HTTP response header\n");
724 ctask->parse_content = GNUNET_YES;
728 if (cookie_hdr_len > bytes)
731 memcpy (hdr_generic, buffer, bytes);
732 hdr_generic[bytes] = '\0';
734 if ('\n' == hdr_generic[bytes-1])
735 hdr_generic[bytes-1] = '\0';
737 if (hdr_generic[bytes-2] == '\r')
738 hdr_generic[bytes-2] = '\0';
740 if (0 == memcmp (hdr_generic,
741 MHD_HTTP_HEADER_SET_COOKIE,
744 ndup = GNUNET_strdup (hdr_generic+cookie_hdr_len+1);
745 memset (new_cookie_hdr, 0, sizeof (new_cookie_hdr));
746 tok = strtok (ndup, ";");
748 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
749 "Looking for cookie in : %s\n", hdr_generic);
751 for (; tok != NULL; tok = strtok (NULL, ";"))
753 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
754 "Got Cookie token: %s\n", tok);
755 //memcpy (new_cookie_hdr+offset, tok, strlen (tok));
756 if (0 == memcmp (tok, " domain", strlen (" domain")))
758 cookie_domain = tok + strlen (" domain") + 1;
760 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
761 "Got Set-Cookie Domain: %s\n", cookie_domain);
763 if (strlen (cookie_domain) < strlen (ctask->leho))
765 delta_cdomain = strlen (ctask->leho) - strlen (cookie_domain);
766 if (0 == strcmp (cookie_domain, ctask->leho + (delta_cdomain)))
768 GNUNET_snprintf (new_cookie_hdr+offset,
769 sizeof (new_cookie_hdr),
770 " domain=%s", ctask->authority);
771 offset += strlen (" domain=") + strlen (ctask->authority);
772 new_cookie_hdr[offset] = ';';
777 else if (strlen (cookie_domain) == strlen (ctask->leho))
779 if (0 == strcmp (cookie_domain, ctask->leho))
781 GNUNET_snprintf (new_cookie_hdr+offset,
782 sizeof (new_cookie_hdr),
783 " domain=%s", ctask->host);
784 offset += strlen (" domain=") + strlen (ctask->host);
785 new_cookie_hdr[offset] = ';';
790 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
791 "Cookie domain invalid\n");
795 memcpy (new_cookie_hdr+offset, tok, strlen (tok));
796 offset += strlen (tok);
797 new_cookie_hdr[offset] = ';';
803 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
804 "Got Set-Cookie HTTP header %s\n", new_cookie_hdr);
806 if (GNUNET_NO == MHD_add_response_header (ctask->response,
807 MHD_HTTP_HEADER_SET_COOKIE,
810 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
811 "MHD: Error adding set-cookie header field %s\n",
812 hdr_generic+cookie_hdr_len+1);
817 ndup = GNUNET_strdup (hdr_generic);
818 hdr_type = strtok (ndup, ":");
820 if (NULL == hdr_type)
826 hdr_val = strtok (NULL, "");
836 if (0 == strcasecmp (MHD_HTTP_HEADER_LOCATION, hdr_type))
838 if (ctask->mhd->is_ssl)
840 sprintf (leho_host, "https://%s", ctask->leho);
841 sprintf (real_host, "https://%s", ctask->host);
845 sprintf (leho_host, "http://%s", ctask->leho);
846 sprintf (real_host, "http://%s", ctask->host);
849 if (0 == memcmp (leho_host, hdr_val, strlen (leho_host)))
851 sprintf (new_location, "%s%s", real_host, hdr_val+strlen (leho_host));
852 hdr_val = new_location;
856 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
857 "Trying to set %s: %s\n",
860 if (GNUNET_NO == MHD_add_response_header (ctask->response,
864 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
865 "MHD: Error adding %s header field %s\n",
876 * @param hd a http daemon list entry
879 run_httpd (struct MhdHttpList *hd);
890 * Task run whenever HTTP server operations are pending.
893 * @param tc sched context
897 const struct GNUNET_SCHEDULER_TaskContext *tc);
900 run_mhd_now (struct MhdHttpList *hd)
902 if (GNUNET_SCHEDULER_NO_TASK != hd->httpd_task)
904 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
905 "MHD: killing old task\n");
906 GNUNET_SCHEDULER_cancel (hd->httpd_task);
908 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
909 "MHD: Scheduling MHD now\n");
910 hd->httpd_task = GNUNET_SCHEDULER_add_now (&do_httpd, hd);
914 * Ask cURL for the select sets and schedule download
917 curl_download_prepare ();
920 * Callback to free content
922 * @param cls content to free
923 * @param tc task context
926 mhd_content_free (void *cls,
927 const struct GNUNET_SCHEDULER_TaskContext *tc)
929 struct ProxyCurlTask *ctask = cls;
930 GNUNET_assert (NULL == ctask->pp_match_head);
932 if (NULL != ctask->headers)
933 curl_slist_free_all (ctask->headers);
935 if (NULL != ctask->headers)
936 curl_slist_free_all (ctask->resolver);
938 if (NULL != ctask->response)
939 MHD_destroy_response (ctask->response);
941 if (NULL != ctask->post_handler)
942 MHD_destroy_post_processor (ctask->post_handler);
950 * Callback for MHD response
953 * @param pos in buffer
955 * @param max space in buffer
956 * @return number of bytes written
959 mhd_content_cb (void *cls,
964 struct ProxyCurlTask *ctask = cls;
965 struct ProxyREMatch *re_match = ctask->pp_match_head;
967 long long int bytes_to_copy = ctask->buffer_write_ptr - ctask->buffer_read_ptr;
969 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
970 "MHD: content cb for %s. To copy: %lld\n",
971 ctask->url, bytes_to_copy);
972 GNUNET_assert (bytes_to_copy >= 0);
974 if ((GNUNET_YES == ctask->download_is_finished) &&
975 (GNUNET_NO == ctask->download_error) &&
976 (0 == bytes_to_copy)) /* &&
977 (BUF_WAIT_FOR_CURL == ctask->buf_status))*/
979 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
980 "MHD: sending response for %s\n", ctask->url);
981 ctask->download_in_progress = GNUNET_NO;
982 run_mhd_now (ctask->mhd);
983 GNUNET_SCHEDULER_add_now (&mhd_content_free, ctask);
984 total_mhd_connections--;
985 return MHD_CONTENT_READER_END_OF_STREAM;
988 if ((GNUNET_YES == ctask->download_error) &&
989 (GNUNET_YES == ctask->download_is_finished) &&
990 (0 == bytes_to_copy)) /* &&
991 (BUF_WAIT_FOR_CURL == ctask->buf_status))*/
993 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
994 "MHD: sending error response\n");
995 ctask->download_in_progress = GNUNET_NO;
996 run_mhd_now (ctask->mhd);
997 GNUNET_SCHEDULER_add_now (&mhd_content_free, ctask);
998 total_mhd_connections--;
999 return MHD_CONTENT_READER_END_WITH_ERROR;
1002 if ( ctask->buf_status == BUF_WAIT_FOR_CURL )
1006 for (; NULL != re_match; re_match = ctask->pp_match_head)
1008 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1009 "MHD: Processing PP %s\n",
1010 re_match->hostname);
1011 bytes_to_copy = re_match->start - ctask->buffer_read_ptr;
1012 GNUNET_assert (bytes_to_copy >= 0);
1014 if (bytes_to_copy+copied > max)
1016 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1017 "MHD: buffer in response too small for %d. Using available space (%d). (%s)\n",
1021 memcpy (buf+copied, ctask->buffer_read_ptr, max-copied);
1022 ctask->buffer_read_ptr += max-copied;
1024 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1025 "MHD: copied %d bytes\n", copied);
1029 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1030 "MHD: copying %d bytes to mhd response at offset %d\n",
1031 bytes_to_copy, ctask->buffer_read_ptr);
1032 memcpy (buf+copied, ctask->buffer_read_ptr, bytes_to_copy);
1033 copied += bytes_to_copy;
1035 if (GNUNET_NO == re_match->done)
1037 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1038 "MHD: Waiting for PP of %s\n", re_match->hostname);
1039 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1040 "MHD: copied %d bytes\n", copied);
1041 ctask->buffer_read_ptr += bytes_to_copy;
1045 if (strlen (re_match->result) > (max - copied))
1047 //FIXME partially copy domain here
1048 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1049 "MHD: buffer in response too small for %s! (%s)\n",
1052 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1053 "MHD: copied %d bytes\n", copied);
1054 ctask->buffer_read_ptr += bytes_to_copy;
1058 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1059 "MHD: Adding PP result %s to buffer\n",
1061 memcpy (buf+copied, re_match->result, strlen (re_match->result));
1062 copied += strlen (re_match->result);
1063 ctask->buffer_read_ptr = re_match->end;
1064 GNUNET_CONTAINER_DLL_remove (ctask->pp_match_head,
1065 ctask->pp_match_tail,
1067 GNUNET_free (re_match);
1070 bytes_to_copy = ctask->buffer_write_ptr - ctask->buffer_read_ptr;
1072 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1073 "MHD: copied: %d left: %d, space left in buf: %d\n",
1075 bytes_to_copy, max-copied);
1077 GNUNET_assert (0 <= bytes_to_copy);
1079 if (GNUNET_NO == ctask->download_is_finished)
1081 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1082 "MHD: Purging buffer\n");
1083 memmove (ctask->buffer, ctask->buffer_read_ptr, bytes_to_copy);
1084 ctask->buffer_read_ptr = ctask->buffer;
1085 ctask->buffer_write_ptr = ctask->buffer + bytes_to_copy;
1086 ctask->buffer[bytes_to_copy] = '\0';
1089 if (bytes_to_copy+copied > max)
1090 bytes_to_copy = max-copied;
1092 if (0 > bytes_to_copy)
1095 memcpy (buf+copied, ctask->buffer_read_ptr, bytes_to_copy);
1096 ctask->buffer_read_ptr += bytes_to_copy;
1097 copied += bytes_to_copy;
1098 ctask->buf_status = BUF_WAIT_FOR_CURL;
1100 if (NULL != ctask->curl)
1101 curl_easy_pause (ctask->curl, CURLPAUSE_CONT);
1103 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1104 "MHD: copied %d bytes\n", copied);
1105 run_mhd_now (ctask->mhd);
1110 * Shorten result callback
1112 * @param cls the proxycurltask
1113 * @param short_name the shortened name (NULL on error)
1116 process_shorten (void* cls, const char* short_name)
1118 struct ProxyREMatch *re_match = cls;
1119 char result[sizeof (re_match->result)];
1121 if (NULL == short_name)
1123 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1124 "PP: Unable to shorten %s\n",
1125 re_match->hostname);
1126 GNUNET_CONTAINER_DLL_remove (re_match->ctask->pp_match_head,
1127 re_match->ctask->pp_match_tail,
1129 GNUNET_free (re_match);
1133 if (0 == strcmp (short_name, re_match->ctask->leho))
1134 strcpy (result, re_match->ctask->host);
1136 strcpy (result, short_name);
1138 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1139 "PP: Shorten %s -> %s\n",
1143 if (re_match->ctask->mhd->is_ssl)
1144 sprintf (re_match->result, "href=\"https://%s", result);
1146 sprintf (re_match->result, "href=\"http://%s", result);
1148 re_match->done = GNUNET_YES;
1149 run_mhd_now (re_match->ctask->mhd);
1154 * Postprocess data in buffer. From read ptr to write ptr
1156 * @param cls the curlproxytask
1157 * @param tc task context
1160 postprocess_buffer (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1162 struct ProxyCurlTask *ctask = cls;
1163 struct ProxyREMatch *re_match;
1164 char* re_ptr = ctask->buffer_read_ptr;
1165 char re_hostname[255];
1166 regmatch_t m[RE_N_MATCHES];
1168 ctask->pp_task = GNUNET_SCHEDULER_NO_TASK;
1170 if (GNUNET_YES != ctask->parse_content)
1172 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1173 "PP: Not parsing content\n");
1174 ctask->buf_status = BUF_WAIT_FOR_MHD;
1175 run_mhd_now (ctask->mhd);
1179 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1180 "PP: We need to parse the HTML\n");
1182 /* 0 means match found */
1183 while (0 == regexec (&re_dotplus, re_ptr, RE_N_MATCHES, m, 0))
1185 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1186 "PP: regex match\n");
1188 GNUNET_assert (m[1].rm_so != -1);
1190 memset (re_hostname, 0, sizeof (re_hostname));
1191 memcpy (re_hostname, re_ptr+m[1].rm_so, (m[3].rm_eo-m[1].rm_so));
1193 re_match = GNUNET_malloc (sizeof (struct ProxyREMatch));
1194 re_match->start = re_ptr + m[0].rm_so;
1195 re_match->end = re_ptr + m[3].rm_eo;
1196 re_match->done = GNUNET_NO;
1197 re_match->ctask = ctask;
1198 strcpy (re_match->hostname, re_hostname);
1199 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1200 "PP: Got hostname %s\n", re_hostname);
1201 re_ptr += m[3].rm_eo;
1203 if (GNUNET_YES == is_tld (re_match->hostname, GNUNET_GNS_TLD_PLUS))
1205 re_match->hostname[strlen(re_match->hostname)-1] = '\0';
1206 strcpy (re_match->hostname+strlen(re_match->hostname),
1210 re_match->shorten_task = GNUNET_GNS_shorten_zone (gns_handle,
1216 re_match); //FIXME cancel appropriately
1218 GNUNET_CONTAINER_DLL_insert_tail (ctask->pp_match_head,
1219 ctask->pp_match_tail,
1223 ctask->buf_status = BUF_WAIT_FOR_MHD;
1224 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1225 "PP: No more matches\n");
1226 run_mhd_now (ctask->mhd);
1230 * Handle data from cURL
1232 * @param ptr pointer to the data
1233 * @param size number of blocks of data
1234 * @param nmemb blocksize
1235 * @param ctx the curlproxytask
1236 * @return number of bytes handled
1239 curl_download_cb (void *ptr, size_t size, size_t nmemb, void* ctx)
1241 const char *cbuf = ptr;
1242 size_t total = size * nmemb;
1243 struct ProxyCurlTask *ctask = ctx;
1244 size_t buf_space = sizeof (ctask->buffer) -
1245 (ctask->buffer_write_ptr-ctask->buffer);
1247 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1248 "CURL: Got %d. %d free in buffer\n",
1251 if (total > (buf_space - CURL_BUF_PADDING))
1253 if (ctask->buf_status == BUF_WAIT_FOR_CURL)
1255 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1256 "CURL: Buffer full starting postprocessing\n");
1257 ctask->buf_status = BUF_WAIT_FOR_PP;
1258 ctask->pp_task = GNUNET_SCHEDULER_add_now (&postprocess_buffer,
1260 return CURL_WRITEFUNC_PAUSE;
1263 /* we should not get called in that case */
1264 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1265 "CURL: called out of context and no space in buffer!\n");
1266 return CURL_WRITEFUNC_PAUSE;
1269 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1270 "CURL: Copying %d bytes to buffer (%s)\n", total, ctask->url);
1271 memcpy (ctask->buffer_write_ptr, cbuf, total);
1272 ctask->bytes_in_buffer += total;
1273 ctask->buffer_write_ptr += total;
1274 ctask->buffer_write_ptr[0] = '\0';
1280 * cURL callback for post data
1283 read_callback (void *buf, size_t size, size_t nmemb, void *cls)
1285 struct ProxyCurlTask *ctask = cls;
1286 struct ProxyPostData *pdata = ctask->post_data_head;
1287 size_t len = size * nmemb;
1291 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1292 "CURL: read callback\n");
1298 if (NULL == pdata->value)
1300 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1301 "CURL: Terminating POST data\n");
1303 GNUNET_CONTAINER_DLL_remove (ctask->post_data_head,
1304 ctask->post_data_tail,
1306 GNUNET_free (pdata);
1310 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1311 "CURL: read callback value %s\n", pdata->value);
1313 to_copy = pdata->bytes_left;
1317 pos = pdata->value + (pdata->total_bytes - pdata->bytes_left);
1318 memcpy (buf, pos, to_copy);
1319 pdata->bytes_left -= to_copy;
1320 if (pdata->bytes_left <= 0)
1322 GNUNET_free (pdata->value);
1323 GNUNET_CONTAINER_DLL_remove (ctask->post_data_head,
1324 ctask->post_data_tail,
1326 GNUNET_free (pdata);
1332 * Task that is run when we are ready to receive more data
1335 * @param cls closure
1336 * @param tc task context
1339 curl_task_download (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
1342 * Ask cURL for the select sets and schedule download
1345 curl_download_prepare ()
1352 struct GNUNET_NETWORK_FDSet *grs;
1353 struct GNUNET_NETWORK_FDSet *gws;
1355 struct GNUNET_TIME_Relative rtime;
1361 mret = curl_multi_fdset (curl_multi, &rs, &ws, &es, &max);
1363 if (mret != CURLM_OK)
1365 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1366 "%s failed at %s:%d: `%s'\n",
1367 "curl_multi_fdset", __FILE__, __LINE__,
1368 curl_multi_strerror (mret));
1369 //TODO cleanup here?
1373 mret = curl_multi_timeout (curl_multi, &to);
1374 rtime = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, to);
1376 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1377 "cURL multi fds: max=%d timeout=%llu\n", max, to);
1379 grs = GNUNET_NETWORK_fdset_create ();
1380 gws = GNUNET_NETWORK_fdset_create ();
1381 GNUNET_NETWORK_fdset_copy_native (grs, &rs, max + 1);
1382 GNUNET_NETWORK_fdset_copy_native (gws, &ws, max + 1);
1383 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1384 "Scheduling task cURL\n");
1386 if (curl_download_task != GNUNET_SCHEDULER_NO_TASK)
1387 GNUNET_SCHEDULER_cancel (curl_download_task);
1391 curl_download_task =
1392 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1395 &curl_task_download, curl_multi);
1397 else if (NULL != ctasks_head)
1399 /* as specified in curl docs */
1400 curl_download_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
1401 &curl_task_download,
1404 GNUNET_NETWORK_fdset_destroy (gws);
1405 GNUNET_NETWORK_fdset_destroy (grs);
1411 * Task that is run when we are ready to receive more data
1414 * @param cls closure
1415 * @param tc task context
1418 curl_task_download (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1422 struct CURLMsg *msg;
1424 struct ProxyCurlTask *ctask;
1428 struct ProxyCurlTask *clean_head = NULL;
1429 struct ProxyCurlTask *clean_tail = NULL;
1431 curl_download_task = GNUNET_SCHEDULER_NO_TASK;
1433 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1435 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1436 "Shutdown requested while trying to download\n");
1440 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1448 mret = curl_multi_perform (curl_multi, &running);
1450 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1451 "Running curl tasks: %d\n", running);
1453 ctask = ctasks_head;
1454 for (; ctask != NULL; ctask = ctask->next)
1456 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1457 "CTask: %s\n", ctask->url);
1461 if (num_ctasks != running)
1463 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1464 "%d ctasks, %d curl running\n", num_ctasks, running);
1469 ctask = ctasks_head;
1470 msg = curl_multi_info_read (curl_multi, &msgnum);
1471 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1472 "Messages left: %d\n", msgnum);
1479 if ((msg->data.result != CURLE_OK) &&
1480 (msg->data.result != CURLE_GOT_NOTHING))
1482 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1483 "Download curl failed");
1485 for (; ctask != NULL; ctask = ctask->next)
1487 if (NULL == ctask->curl)
1490 if (memcmp (msg->easy_handle, ctask->curl, sizeof (CURL)) != 0)
1493 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1494 "CURL: Download failed for task %s: %s.\n",
1496 curl_easy_strerror (msg->data.result));
1497 ctask->download_is_finished = GNUNET_YES;
1498 ctask->download_error = GNUNET_YES;
1499 if (CURLE_OK == curl_easy_getinfo (ctask->curl,
1500 CURLINFO_RESPONSE_CODE,
1502 ctask->curl_response_code = resp_code;
1503 ctask->ready_to_queue = MHD_YES;
1504 ctask->buf_status = BUF_WAIT_FOR_MHD;
1505 run_mhd_now (ctask->mhd);
1507 GNUNET_CONTAINER_DLL_remove (ctasks_head, ctasks_tail,
1509 GNUNET_CONTAINER_DLL_insert (clean_head, clean_tail, ctask);
1512 GNUNET_assert (ctask != NULL);
1516 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1517 "CURL: download completed.\n");
1519 for (; ctask != NULL; ctask = ctask->next)
1521 if (NULL == ctask->curl)
1524 if (memcmp (msg->easy_handle, ctask->curl, sizeof (CURL)) != 0)
1527 if (ctask->buf_status != BUF_WAIT_FOR_CURL)
1530 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1531 "CURL: completed task %s found.\n", ctask->url);
1532 if (CURLE_OK == curl_easy_getinfo (ctask->curl,
1533 CURLINFO_RESPONSE_CODE,
1535 ctask->curl_response_code = resp_code;
1538 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1539 "CURL: Completed ctask!\n");
1540 if (GNUNET_SCHEDULER_NO_TASK == ctask->pp_task)
1542 ctask->buf_status = BUF_WAIT_FOR_PP;
1543 ctask->pp_task = GNUNET_SCHEDULER_add_now (&postprocess_buffer,
1547 ctask->ready_to_queue = MHD_YES;
1548 ctask->download_is_finished = GNUNET_YES;
1550 /* We MUST not modify the multi handle else we loose messages */
1551 GNUNET_CONTAINER_DLL_remove (ctasks_head, ctasks_tail,
1553 GNUNET_CONTAINER_DLL_insert (clean_head, clean_tail, ctask);
1557 GNUNET_assert (ctask != NULL);
1559 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1560 "CURL: %s\n", curl_easy_strerror(msg->data.result));
1566 } while (msgnum > 0);
1568 for (ctask=clean_head; ctask != NULL; ctask = ctask->next)
1570 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1571 "CURL: Removing task %s.\n", ctask->url);
1572 curl_multi_remove_handle (curl_multi, ctask->curl);
1573 curl_easy_cleanup (ctask->curl);
1578 for (ctask=ctasks_head; ctask != NULL; ctask = ctask->next)
1583 if (num_ctasks != running)
1585 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1586 "CURL: %d tasks, %d running\n", num_ctasks, running);
1589 GNUNET_assert ( num_ctasks == running );
1591 } while (mret == CURLM_CALL_MULTI_PERFORM);
1593 if (mret != CURLM_OK)
1595 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "CURL: %s failed at %s:%d: `%s'\n",
1596 "curl_multi_perform", __FILE__, __LINE__,
1597 curl_multi_strerror (mret));
1599 curl_download_prepare();
1603 * Process LEHO lookup
1605 * @param cls the ctask
1606 * @param rd_count number of records returned
1607 * @param rd record data
1610 process_leho_lookup (void *cls,
1612 const struct GNUNET_NAMESTORE_RecordData *rd)
1614 struct ProxyCurlTask *ctask = cls;
1615 char hosthdr[262]; //256 + "Host: "
1619 struct hostent *phost;
1621 char resolvename[512];
1624 strcpy (ctask->leho, "");
1627 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1628 "No LEHO present!\n");
1630 for (i=0; i<rd_count; i++)
1632 if (rd[i].record_type != GNUNET_GNS_RECORD_LEHO)
1635 memcpy (ctask->leho, rd[i].data, rd[i].data_size);
1637 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1638 "Found LEHO %s for %s\n", ctask->leho, ctask->url);
1641 if (0 != strcmp (ctask->leho, ""))
1643 sprintf (hosthdr, "%s%s", "Host: ", ctask->leho);
1644 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1645 "New HTTP header value: %s\n", hosthdr);
1646 ctask->headers = curl_slist_append (ctask->headers, hosthdr);
1647 GNUNET_assert (NULL != ctask->headers);
1648 ret = curl_easy_setopt (ctask->curl, CURLOPT_HTTPHEADER, ctask->headers);
1649 if (CURLE_OK != ret)
1651 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "%s failed at %s:%d: `%s'\n",
1652 "curl_easy_setopt", __FILE__, __LINE__, curl_easy_strerror(ret));
1657 if (ctask->mhd->is_ssl)
1659 phost = (struct hostent*)gethostbyname (ctask->host);
1663 ssl_ip = inet_ntoa(*((struct in_addr*)(phost->h_addr)));
1664 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1665 "SSL target server: %s\n", ssl_ip);
1666 sprintf (resolvename, "%s:%d:%s", ctask->leho, HTTPS_PORT, ssl_ip);
1667 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1668 "Curl resolve: %s\n", resolvename);
1669 ctask->resolver = curl_slist_append ( ctask->resolver, resolvename);
1670 curl_easy_setopt (ctask->curl, CURLOPT_RESOLVE, ctask->resolver);
1671 sprintf (curlurl, "https://%s:%d%s", ctask->leho, ctask->port, ctask->url);
1672 curl_easy_setopt (ctask->curl, CURLOPT_URL, curlurl);
1676 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1677 "gethostbyname failed for %s!\n", ctask->host);
1678 ctask->download_is_finished = GNUNET_YES;
1679 ctask->download_error = GNUNET_YES;
1684 if (CURLM_OK != (mret=curl_multi_add_handle (curl_multi, ctask->curl)))
1686 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1687 "%s failed at %s:%d: `%s'\n",
1688 "curl_multi_add_handle", __FILE__, __LINE__,
1689 curl_multi_strerror (mret));
1690 ctask->download_is_finished = GNUNET_YES;
1691 ctask->download_error = GNUNET_YES;
1694 GNUNET_CONTAINER_DLL_insert (ctasks_head, ctasks_tail, ctask);
1696 curl_download_prepare ();
1701 * Initialize download and trigger curl
1703 * @param cls the proxycurltask
1704 * @param auth_name the name of the authority (site of origin) of ctask->host
1708 process_get_authority (void *cls,
1709 const char* auth_name)
1711 struct ProxyCurlTask *ctask = cls;
1713 if (NULL == auth_name)
1715 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1716 "Get authority failed!\n");
1717 strcpy (ctask->authority, "");
1721 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1722 "Get authority yielded %s\n", auth_name);
1723 strcpy (ctask->authority, auth_name);
1726 GNUNET_GNS_lookup_zone (gns_handle,
1729 GNUNET_GNS_RECORD_LEHO,
1730 GNUNET_YES, //Only cached for performance
1732 &process_leho_lookup,
1738 * Main MHD callback for handling requests.
1741 * @param con MHD connection handle
1742 * @param url the url in the request
1743 * @param meth the HTTP method used ("GET", "PUT", etc.)
1744 * @param ver the HTTP version string (i.e. "HTTP/1.1")
1745 * @param upload_data the data being uploaded (excluding HEADERS,
1746 * for a POST that fits into memory and that is encoded
1747 * with a supported encoding, the POST data will NOT be
1748 * given in upload_data and is instead available as
1749 * part of MHD_get_connection_values; very large POST
1750 * data *will* be made available incrementally in
1752 * @param upload_data_size set initially to the size of the
1753 * upload_data provided; the method must update this
1754 * value to the number of bytes NOT processed;
1755 * @param con_cls pointer to location where we store the 'struct Request'
1756 * @return MHD_YES if the connection was handled successfully,
1757 * MHD_NO if the socket must be closed due to a serious
1758 * error while handling the request
1761 create_response (void *cls,
1762 struct MHD_Connection *con,
1766 const char *upload_data,
1767 size_t *upload_data_size,
1770 struct MhdHttpList* hd = cls;
1771 const char* page = "<html><head><title>gnoxy</title>"\
1772 "</head><body>cURL fail</body></html>";
1774 char curlurl[MAX_HTTP_URI_LENGTH]; // buffer overflow!
1777 struct ProxyCurlTask *ctask;
1778 struct ProxyPostData *fin_post;
1781 if ((0 != strcasecmp (meth, MHD_HTTP_METHOD_GET)) &&
1782 (0 != strcasecmp (meth, MHD_HTTP_METHOD_PUT)) &&
1783 (0 != strcasecmp (meth, MHD_HTTP_METHOD_POST)))
1785 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1786 "MHD: %s NOT IMPLEMENTED!\n", meth);
1791 if (NULL == *con_cls)
1794 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1795 "Got %s request for %s\n", meth, url);
1796 ctask = GNUNET_malloc (sizeof (struct ProxyCurlTask));
1800 ctask->curl = curl_easy_init();
1801 if (NULL == ctask->curl)
1803 ctask->response = MHD_create_response_from_buffer (strlen (page),
1805 MHD_RESPMEM_PERSISTENT);
1806 ret = MHD_queue_response (con,
1809 MHD_destroy_response (ctask->response);
1810 GNUNET_free (ctask);
1814 ctask->download_in_progress = GNUNET_YES;
1815 ctask->buf_status = BUF_WAIT_FOR_CURL;
1816 ctask->connection = con;
1817 ctask->curl_response_code = MHD_HTTP_OK;
1818 ctask->buffer_read_ptr = ctask->buffer;
1819 ctask->buffer_write_ptr = ctask->buffer;
1820 ctask->pp_task = GNUNET_SCHEDULER_NO_TASK;
1821 ctask->port = HTTP_PORT;
1823 MHD_get_connection_values (con,
1825 &con_val_iter, ctask);
1827 if (0 == strcasecmp (meth, MHD_HTTP_METHOD_PUT))
1829 if (0 == *upload_data_size)
1831 curl_easy_cleanup (ctask->curl);
1832 GNUNET_free (ctask);
1835 ctask->put_read_offset = 0;
1836 ctask->put_read_size = *upload_data_size;
1837 curl_easy_setopt (ctask->curl, CURLOPT_UPLOAD, 1);
1838 curl_easy_setopt (ctask->curl, CURLOPT_READDATA, upload_data);
1839 //curl_easy_setopt (ctask->curl, CURLOPT_READFUNCTION, &curl_read_cb);
1841 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1842 "Got PUT data: %s\n", upload_data);
1843 curl_easy_cleanup (ctask->curl);
1844 GNUNET_free (ctask);
1848 if (0 == strcasecmp (meth, MHD_HTTP_METHOD_POST))
1850 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1851 "Setting up POST processor\n");
1852 ctask->post_handler = MHD_create_post_processor (con,
1854 &con_post_data_iter,
1856 curl_easy_setopt (ctask->curl, CURLOPT_POST, 1);
1857 curl_easy_setopt (ctask->curl, CURLOPT_READFUNCTION,
1859 curl_easy_setopt (ctask->curl, CURLOPT_READDATA, ctask);
1860 ctask->headers = curl_slist_append (ctask->headers,
1861 "Transfer-Encoding: chunked");
1862 /*curl_easy_setopt (ctask->curl, CURLOPT_POST, 1);
1863 curl_easy_setopt (ctask->curl, CURLOPT_POSTFIELDSIZE, *upload_data_size);
1864 curl_easy_setopt (ctask->curl, CURLOPT_COPYPOSTFIELDS, upload_data);
1866 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1867 "Got POST data: %s\n", upload_data);
1868 curl_easy_cleanup (ctask->curl);
1869 GNUNET_free (ctask);
1873 curl_easy_setopt (ctask->curl, CURLOPT_HEADERFUNCTION, &curl_check_hdr);
1874 curl_easy_setopt (ctask->curl, CURLOPT_HEADERDATA, ctask);
1875 curl_easy_setopt (ctask->curl, CURLOPT_WRITEFUNCTION, &curl_download_cb);
1876 curl_easy_setopt (ctask->curl, CURLOPT_WRITEDATA, ctask);
1877 curl_easy_setopt (ctask->curl, CURLOPT_FOLLOWLOCATION, 0);
1878 curl_easy_setopt (ctask->curl, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
1880 if (GNUNET_NO == ctask->mhd->is_ssl)
1882 sprintf (curlurl, "http://%s:%d%s", ctask->host, ctask->port, url);
1883 MHD_get_connection_values (con,
1884 MHD_GET_ARGUMENT_KIND,
1885 &get_uri_val_iter, curlurl);
1886 curl_easy_setopt (ctask->curl, CURLOPT_URL, curlurl);
1888 strcpy (ctask->url, url);
1889 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1890 "MHD: Adding new curl task for %s%s\n", ctask->host, url);
1891 MHD_get_connection_values (con,
1892 MHD_GET_ARGUMENT_KIND,
1893 &get_uri_val_iter, ctask->url);
1895 curl_easy_setopt (ctask->curl, CURLOPT_FAILONERROR, 1);
1896 curl_easy_setopt (ctask->curl, CURLOPT_CONNECTTIMEOUT, 600L);
1897 curl_easy_setopt (ctask->curl, CURLOPT_TIMEOUT, 600L);
1899 GNUNET_GNS_get_authority (gns_handle,
1901 &process_get_authority,
1903 ctask->ready_to_queue = GNUNET_NO;
1904 ctask->fin = GNUNET_NO;
1908 ctask = (struct ProxyCurlTask *) *con_cls;
1909 if (0 == strcasecmp (meth, MHD_HTTP_METHOD_POST))
1911 if (0 != *upload_data_size)
1913 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1914 "Invoking POST processor\n");
1915 MHD_post_process (ctask->post_handler,
1916 upload_data, *upload_data_size);
1917 *upload_data_size = 0;
1920 else if (GNUNET_NO == ctask->post_done)
1922 fin_post = GNUNET_malloc (sizeof (struct ProxyPostData));
1923 GNUNET_CONTAINER_DLL_insert_tail (ctask->post_data_head,
1924 ctask->post_data_tail,
1926 ctask->post_done = GNUNET_YES;
1931 if (GNUNET_YES != ctask->ready_to_queue)
1932 return MHD_YES; /* wait longer */
1934 if (GNUNET_YES == ctask->fin)
1937 ctask->fin = GNUNET_YES;
1938 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1939 "MHD: Queueing response for %s\n", ctask->url);
1940 ret = MHD_queue_response (con, ctask->curl_response_code, ctask->response);
1941 run_mhd_now (ctask->mhd);
1952 struct MhdHttpList *hd;
1954 for (hd=mhd_httpd_head; NULL != hd; hd = hd->next)
1962 * @param hd the daemon to run
1965 run_httpd (struct MhdHttpList *hd)
1970 struct GNUNET_NETWORK_FDSet *wrs;
1971 struct GNUNET_NETWORK_FDSet *wws;
1972 struct GNUNET_NETWORK_FDSet *wes;
1975 unsigned MHD_LONG_LONG timeout;
1976 struct GNUNET_TIME_Relative tv;
1981 wrs = GNUNET_NETWORK_fdset_create ();
1982 wes = GNUNET_NETWORK_fdset_create ();
1983 wws = GNUNET_NETWORK_fdset_create ();
1985 GNUNET_assert (MHD_YES == MHD_get_fdset (hd->daemon, &rs, &ws, &es, &max));
1988 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1989 "MHD fds: max=%d\n", max);
1991 haveto = MHD_get_timeout (hd->daemon, &timeout);
1993 if (MHD_YES == haveto)
1994 tv.rel_value = (uint64_t) timeout;
1996 tv = GNUNET_TIME_UNIT_FOREVER_REL;
1997 GNUNET_NETWORK_fdset_copy_native (wrs, &rs, max + 1);
1998 GNUNET_NETWORK_fdset_copy_native (wws, &ws, max + 1);
1999 GNUNET_NETWORK_fdset_copy_native (wes, &es, max + 1);
2001 if (GNUNET_SCHEDULER_NO_TASK != hd->httpd_task)
2002 GNUNET_SCHEDULER_cancel (hd->httpd_task);
2004 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_HIGH,
2007 GNUNET_NETWORK_fdset_destroy (wrs);
2008 GNUNET_NETWORK_fdset_destroy (wws);
2009 GNUNET_NETWORK_fdset_destroy (wes);
2014 * Task run whenever HTTP server operations are pending.
2017 * @param tc sched context
2020 do_httpd (void *cls,
2021 const struct GNUNET_SCHEDULER_TaskContext *tc)
2023 struct MhdHttpList *hd = cls;
2025 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2026 "MHD: Main loop\n");
2027 hd->httpd_task = GNUNET_SCHEDULER_NO_TASK;
2028 MHD_run (hd->daemon);
2035 * Read data from socket
2037 * @param cls the closure
2038 * @param tc scheduler context
2041 do_read (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
2044 * Read from remote end
2046 * @param cls closure
2047 * @param tc scheduler context
2050 do_read_remote (void* cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
2053 * Write data to remote socket
2055 * @param cls the closure
2056 * @param tc scheduler context
2059 do_write_remote (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2061 struct Socks5Request *s5r = cls;
2064 s5r->fwdwtask = GNUNET_SCHEDULER_NO_TASK;
2066 if ((NULL != tc->read_ready) &&
2067 (GNUNET_NETWORK_fdset_isset (tc->write_ready, s5r->remote_sock)) &&
2068 ((len = GNUNET_NETWORK_socket_send (s5r->remote_sock, s5r->rbuf,
2071 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2072 "Successfully sent %d bytes to remote socket\n",
2077 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "write remote");
2078 if (GNUNET_SCHEDULER_NO_TASK != s5r->rtask)
2079 GNUNET_SCHEDULER_cancel (s5r->rtask);
2080 if (GNUNET_SCHEDULER_NO_TASK != s5r->wtask)
2081 GNUNET_SCHEDULER_cancel (s5r->wtask);
2082 if (GNUNET_SCHEDULER_NO_TASK != s5r->fwdrtask)
2083 GNUNET_SCHEDULER_cancel (s5r->fwdrtask);
2084 GNUNET_NETWORK_socket_close (s5r->remote_sock);
2085 GNUNET_NETWORK_socket_close (s5r->sock);
2091 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
2098 * Clean up s5r handles
2100 * @param s5r the handle to destroy
2103 cleanup_s5r (struct Socks5Request *s5r)
2105 if (GNUNET_SCHEDULER_NO_TASK != s5r->rtask)
2106 GNUNET_SCHEDULER_cancel (s5r->rtask);
2107 if (GNUNET_SCHEDULER_NO_TASK != s5r->fwdwtask)
2108 GNUNET_SCHEDULER_cancel (s5r->fwdwtask);
2109 if (GNUNET_SCHEDULER_NO_TASK != s5r->fwdrtask)
2110 GNUNET_SCHEDULER_cancel (s5r->fwdrtask);
2112 if (NULL != s5r->remote_sock)
2113 GNUNET_NETWORK_socket_close (s5r->remote_sock);
2114 if ((NULL != s5r->sock) && (s5r->cleanup_sock == GNUNET_YES))
2115 GNUNET_NETWORK_socket_close (s5r->sock);
2121 * Write data to socket
2123 * @param cls the closure
2124 * @param tc scheduler context
2127 do_write (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2129 struct Socks5Request *s5r = cls;
2132 s5r->wtask = GNUNET_SCHEDULER_NO_TASK;
2134 if ((NULL != tc->read_ready) &&
2135 (GNUNET_NETWORK_fdset_isset (tc->write_ready, s5r->sock)) &&
2136 ((len = GNUNET_NETWORK_socket_send (s5r->sock, s5r->wbuf,
2139 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2140 "Successfully sent %d bytes to socket\n",
2146 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "write");
2147 s5r->cleanup = GNUNET_YES;
2148 s5r->cleanup_sock = GNUNET_YES;
2154 if (GNUNET_YES == s5r->cleanup)
2160 if ((s5r->state == SOCKS5_DATA_TRANSFER) &&
2161 (s5r->fwdrtask == GNUNET_SCHEDULER_NO_TASK))
2163 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
2165 &do_read_remote, s5r);
2169 * Read from remote end
2171 * @param cls closure
2172 * @param tc scheduler context
2175 do_read_remote (void* cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2177 struct Socks5Request *s5r = cls;
2179 s5r->fwdrtask = GNUNET_SCHEDULER_NO_TASK;
2182 if ((NULL != tc->write_ready) &&
2183 (GNUNET_NETWORK_fdset_isset (tc->read_ready, s5r->remote_sock)) &&
2184 (s5r->wbuf_len = GNUNET_NETWORK_socket_recv (s5r->remote_sock, s5r->wbuf,
2185 sizeof (s5r->wbuf))))
2187 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2188 "Successfully read %d bytes from remote socket\n",
2193 if (s5r->wbuf_len == 0)
2194 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2195 "0 bytes received from remote... graceful shutdown!\n");
2196 if (s5r->fwdwtask != GNUNET_SCHEDULER_NO_TASK)
2197 GNUNET_SCHEDULER_cancel (s5r->fwdwtask);
2198 if (s5r->rtask != GNUNET_SCHEDULER_NO_TASK)
2199 GNUNET_SCHEDULER_cancel (s5r->rtask);
2201 GNUNET_NETWORK_socket_close (s5r->remote_sock);
2202 s5r->remote_sock = NULL;
2203 GNUNET_NETWORK_socket_close (s5r->sock);
2209 s5r->wtask = GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
2217 * Adds a socket to MHD
2219 * @param h the handle to the socket to add
2220 * @param daemon the daemon to add the fd to
2221 * @return whatever MHD_add_connection returns
2224 add_handle_to_mhd (struct GNUNET_NETWORK_Handle *h, struct MHD_Daemon *daemon)
2227 struct sockaddr *addr;
2230 fd = dup (GNUNET_NETWORK_get_fd (h));
2231 addr = GNUNET_NETWORK_get_addr (h);
2232 len = GNUNET_NETWORK_get_addrlen (h);
2234 return MHD_add_connection (daemon, fd, addr, len);
2238 * Read file in filename
2240 * @param filename file to read
2241 * @param size pointer where filesize is stored
2245 load_file (const char* filename,
2252 GNUNET_DISK_file_size (filename, &fsize,
2253 GNUNET_YES, GNUNET_YES))
2255 if (fsize > MAX_PEM_SIZE)
2257 *size = (unsigned int) fsize;
2258 buffer = GNUNET_malloc (*size);
2259 if (fsize != GNUNET_DISK_fn_read (filename, buffer, (size_t) fsize))
2261 GNUNET_free (buffer);
2269 * Load PEM key from file
2271 * @param key where to store the data
2272 * @param keyfile path to the PEM file
2273 * @return GNUNET_OK on success
2276 load_key_from_file (gnutls_x509_privkey_t key, const char* keyfile)
2278 gnutls_datum_t key_data;
2281 key_data.data = (unsigned char*) load_file (keyfile, &key_data.size);
2282 ret = gnutls_x509_privkey_import (key, &key_data,
2283 GNUTLS_X509_FMT_PEM);
2284 if (GNUTLS_E_SUCCESS != ret)
2286 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2287 _("Unable to import private key from file `%s'\n"),
2291 GNUNET_free (key_data.data);
2292 return (GNUTLS_E_SUCCESS != ret) ? GNUNET_SYSERR : GNUNET_OK;
2297 * Load cert from file
2299 * @param crt struct to store data in
2300 * @param certfile path to pem file
2301 * @return GNUNET_OK on success
2304 load_cert_from_file (gnutls_x509_crt_t crt, char* certfile)
2306 gnutls_datum_t cert_data;
2307 cert_data.data = NULL;
2310 cert_data.data = (unsigned char*) load_file (certfile, &cert_data.size);
2311 ret = gnutls_x509_crt_import (crt, &cert_data,
2312 GNUTLS_X509_FMT_PEM);
2313 if (GNUTLS_E_SUCCESS != ret)
2315 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2316 _("Unable to import certificate %s\n"), certfile);
2319 GNUNET_free (cert_data.data);
2320 return (GNUTLS_E_SUCCESS != ret) ? GNUNET_SYSERR : GNUNET_OK;
2325 * Generate new certificate for specific name
2327 * @param name the subject name to generate a cert for
2328 * @return a struct holding the PEM data
2330 static struct ProxyGNSCertificate *
2331 generate_gns_certificate (const char *name)
2335 unsigned int serial;
2336 size_t key_buf_size;
2337 size_t cert_buf_size;
2338 gnutls_x509_crt_t request;
2342 ret = gnutls_x509_crt_init (&request);
2344 if (GNUTLS_E_SUCCESS != ret)
2349 ret = gnutls_x509_crt_set_key (request, proxy_ca.key);
2351 if (GNUTLS_E_SUCCESS != ret)
2356 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Generating cert\n");
2358 struct ProxyGNSCertificate *pgc =
2359 GNUNET_malloc (sizeof (struct ProxyGNSCertificate));
2361 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Adding DNs\n");
2363 gnutls_x509_crt_set_dn_by_oid (request, GNUTLS_OID_X520_COUNTRY_NAME,
2366 gnutls_x509_crt_set_dn_by_oid (request, GNUTLS_OID_X520_ORGANIZATION_NAME,
2369 gnutls_x509_crt_set_dn_by_oid (request, GNUTLS_OID_X520_COMMON_NAME,
2370 0, name, strlen (name));
2372 ret = gnutls_x509_crt_set_version (request, 3);
2374 ret = gnutls_rnd (GNUTLS_RND_NONCE, &serial, sizeof (serial));
2376 etime = time (NULL);
2377 tm_data = localtime (&etime);
2380 ret = gnutls_x509_crt_set_serial (request,
2384 ret = gnutls_x509_crt_set_activation_time (request,
2387 etime = mktime (tm_data);
2394 ret = gnutls_x509_crt_set_expiration_time (request,
2396 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Signing...\n");
2398 ret = gnutls_x509_crt_sign (request, proxy_ca.cert, proxy_ca.key);
2400 key_buf_size = sizeof (pgc->key);
2401 cert_buf_size = sizeof (pgc->cert);
2403 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Exporting certificate...\n");
2405 gnutls_x509_crt_export (request, GNUTLS_X509_FMT_PEM,
2406 pgc->cert, &cert_buf_size);
2408 gnutls_x509_privkey_export (proxy_ca.key, GNUTLS_X509_FMT_PEM,
2409 pgc->key, &key_buf_size);
2412 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Cleaning up\n");
2413 gnutls_x509_crt_deinit (request);
2421 * Accept policy for mhdaemons
2424 * @param addr the sockaddr
2425 * @param addrlen the sockaddr length
2426 * @return MHD_NO if sockaddr is wrong or #conns too high
2429 accept_cb (void* cls, const struct sockaddr *addr, socklen_t addrlen)
2431 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2432 "In MHD accept policy cb\n");
2436 if (addr->sa_family == AF_UNIX)
2440 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2441 "Connection accepted\n");
2448 * Adds a socket to an SSL MHD instance
2449 * It is important the the domain name is
2450 * correct. In most cases we need to start a new daemon
2452 * @param h the handle to add to a daemon
2453 * @param domain the domain the ssl daemon has to serve
2454 * @return MHD_YES on success
2457 add_handle_to_ssl_mhd (struct GNUNET_NETWORK_Handle *h, const char* domain)
2459 struct MhdHttpList *hd = NULL;
2460 struct ProxyGNSCertificate *pgc;
2461 struct NetworkHandleList *nh;
2463 for (hd = mhd_httpd_head; NULL != hd; hd = hd->next)
2464 if (0 == strcmp (hd->domain, domain))
2469 pgc = generate_gns_certificate (domain);
2471 hd = GNUNET_malloc (sizeof (struct MhdHttpList));
2472 hd->is_ssl = GNUNET_YES;
2473 strcpy (hd->domain, domain);
2474 hd->proxy_cert = pgc;
2477 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2478 "No previous SSL instance found... starting new one for %s\n",
2481 hd->daemon = MHD_start_daemon (MHD_USE_DEBUG | MHD_USE_SSL, 4444,
2483 &create_response, hd,
2484 MHD_OPTION_LISTEN_SOCKET, GNUNET_NETWORK_get_fd (mhd_unix_socket),
2485 MHD_OPTION_CONNECTION_LIMIT, MHD_MAX_CONNECTIONS,
2486 MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) 16,
2487 MHD_OPTION_NOTIFY_COMPLETED,
2489 MHD_OPTION_HTTPS_MEM_KEY, pgc->key,
2490 MHD_OPTION_HTTPS_MEM_CERT, pgc->cert,
2493 GNUNET_assert (hd->daemon != NULL);
2494 hd->httpd_task = GNUNET_SCHEDULER_NO_TASK;
2496 GNUNET_CONTAINER_DLL_insert (mhd_httpd_head, mhd_httpd_tail, hd);
2499 nh = GNUNET_malloc (sizeof (struct NetworkHandleList));
2502 GNUNET_CONTAINER_DLL_insert (hd->socket_handles_head,
2503 hd->socket_handles_tail,
2506 return add_handle_to_mhd (h, hd->daemon);
2512 * Read data from incoming connection
2514 * @param cls the closure
2515 * @param tc the scheduler context
2518 do_read (void* cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2520 struct Socks5Request *s5r = cls;
2521 struct socks5_client_hello *c_hello;
2522 struct socks5_server_hello *s_hello;
2523 struct socks5_client_request *c_req;
2524 struct socks5_server_response *s_resp;
2530 struct hostent *phost;
2532 struct sockaddr_in remote_addr;
2533 struct in_addr *r_sin_addr;
2535 struct NetworkHandleList *nh;
2537 s5r->rtask = GNUNET_SCHEDULER_NO_TASK;
2539 if ((NULL != tc->write_ready) &&
2540 (GNUNET_NETWORK_fdset_isset (tc->read_ready, s5r->sock)) &&
2541 (s5r->rbuf_len = GNUNET_NETWORK_socket_recv (s5r->sock, s5r->rbuf,
2542 sizeof (s5r->rbuf))))
2544 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2545 "Successfully read %d bytes from socket\n",
2550 if (s5r->rbuf_len != 0)
2551 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "read");
2553 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client disco!\n");
2555 if (s5r->fwdrtask != GNUNET_SCHEDULER_NO_TASK)
2556 GNUNET_SCHEDULER_cancel (s5r->fwdrtask);
2557 if (s5r->wtask != GNUNET_SCHEDULER_NO_TASK)
2558 GNUNET_SCHEDULER_cancel (s5r->wtask);
2559 if (s5r->fwdwtask != GNUNET_SCHEDULER_NO_TASK)
2560 GNUNET_SCHEDULER_cancel (s5r->fwdwtask);
2561 GNUNET_NETWORK_socket_close (s5r->remote_sock);
2562 GNUNET_NETWORK_socket_close (s5r->sock);
2567 if (s5r->state == SOCKS5_INIT)
2569 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2571 c_hello = (struct socks5_client_hello*)&s5r->rbuf;
2573 GNUNET_assert (c_hello->version == SOCKS_VERSION_5);
2575 s_hello = (struct socks5_server_hello*)&s5r->wbuf;
2576 s5r->wbuf_len = sizeof( struct socks5_server_hello );
2578 s_hello->version = c_hello->version;
2579 s_hello->auth_method = SOCKS_AUTH_NONE;
2581 /* Write response to client */
2582 s5r->wtask = GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
2586 s5r->rtask = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
2590 s5r->state = SOCKS5_REQUEST;
2594 if (s5r->state == SOCKS5_REQUEST)
2596 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2597 "Processing SOCKS5 request\n");
2598 c_req = (struct socks5_client_request*)&s5r->rbuf;
2599 s_resp = (struct socks5_server_response*)&s5r->wbuf;
2600 //Only 10byte for ipv4 response!
2601 s5r->wbuf_len = 10;//sizeof (struct socks5_server_response);
2603 GNUNET_assert (c_req->addr_type == 3);
2605 dom_len = *((uint8_t*)(&(c_req->addr_type) + 1));
2606 memset(domain, 0, sizeof(domain));
2607 strncpy(domain, (char*)(&(c_req->addr_type) + 2), dom_len);
2608 req_port = *((uint16_t*)(&(c_req->addr_type) + 2 + dom_len));
2610 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2611 "Requested connection is %s:%d\n",
2615 if (is_tld (domain, GNUNET_GNS_TLD) ||
2616 is_tld (domain, GNUNET_GNS_TLD_ZKEY))
2618 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2619 "Requested connection is gnunet tld\n",
2623 if (ntohs(req_port) == HTTPS_PORT)
2625 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2626 "Requested connection is HTTPS\n");
2627 ret = add_handle_to_ssl_mhd ( s5r->sock, domain );
2629 else if (NULL != httpd)
2631 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2632 "Requested connection is HTTP\n");
2633 nh = GNUNET_malloc (sizeof (struct NetworkHandleList));
2636 GNUNET_CONTAINER_DLL_insert (mhd_httpd_head->socket_handles_head,
2637 mhd_httpd_head->socket_handles_tail,
2640 ret = add_handle_to_mhd ( s5r->sock, httpd );
2645 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2646 _("Failed to start HTTP server\n"));
2647 s_resp->version = 0x05;
2648 s_resp->reply = 0x01;
2649 s5r->cleanup = GNUNET_YES;
2650 s5r->cleanup_sock = GNUNET_YES;
2652 GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
2658 /* Signal success */
2659 s_resp->version = 0x05;
2660 s_resp->reply = 0x00;
2661 s_resp->reserved = 0x00;
2662 s_resp->addr_type = 0x01;
2664 s5r->cleanup = GNUNET_YES;
2665 s5r->cleanup_sock = GNUNET_NO;
2667 GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
2675 phost = (struct hostent*)gethostbyname (domain);
2678 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2679 "Resolve %s error!\n", domain );
2680 s_resp->version = 0x05;
2681 s_resp->reply = 0x01;
2682 s5r->cleanup = GNUNET_YES;
2683 s5r->cleanup_sock = GNUNET_YES;
2685 GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
2691 s5r->remote_sock = GNUNET_NETWORK_socket_create (AF_INET,
2694 r_sin_addr = (struct in_addr*)(phost->h_addr);
2695 remote_ip = r_sin_addr->s_addr;
2696 memset(&remote_addr, 0, sizeof(remote_addr));
2697 remote_addr.sin_family = AF_INET;
2698 #if HAVE_SOCKADDR_IN_SIN_LEN
2699 remote_addr.sin_len = sizeof (remote_addr);
2701 remote_addr.sin_addr.s_addr = remote_ip;
2702 remote_addr.sin_port = req_port;
2704 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2705 "target server: %s:%u\n", inet_ntoa(remote_addr.sin_addr),
2709 GNUNET_NETWORK_socket_connect ( s5r->remote_sock,
2710 (const struct sockaddr*)&remote_addr,
2711 sizeof (remote_addr)))
2712 && (errno != EINPROGRESS))
2714 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "connect");
2715 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2716 "socket request error...\n");
2717 s_resp->version = 0x05;
2718 s_resp->reply = 0x01;
2720 GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
2727 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2728 "new remote connection\n");
2730 s_resp->version = 0x05;
2731 s_resp->reply = 0x00;
2732 s_resp->reserved = 0x00;
2733 s_resp->addr_type = 0x01;
2735 s5r->state = SOCKS5_DATA_TRANSFER;
2738 GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
2742 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
2750 if (s5r->state == SOCKS5_DATA_TRANSFER)
2752 if ((s5r->remote_sock == NULL) || (s5r->rbuf_len == 0))
2754 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2755 "Closing connection to client\n");
2756 if (s5r->rtask != GNUNET_SCHEDULER_NO_TASK)
2757 GNUNET_SCHEDULER_cancel (s5r->rtask);
2758 if (s5r->fwdwtask != GNUNET_SCHEDULER_NO_TASK)
2759 GNUNET_SCHEDULER_cancel (s5r->fwdwtask);
2760 if (s5r->fwdrtask != GNUNET_SCHEDULER_NO_TASK)
2761 GNUNET_SCHEDULER_cancel (s5r->fwdrtask);
2762 if (s5r->fwdrtask != GNUNET_SCHEDULER_NO_TASK)
2763 GNUNET_SCHEDULER_cancel (s5r->fwdrtask);
2765 if (s5r->remote_sock != NULL)
2766 GNUNET_NETWORK_socket_close (s5r->remote_sock);
2767 GNUNET_NETWORK_socket_close (s5r->sock);
2772 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2773 "forwarding %d bytes from client\n", s5r->rbuf_len);
2776 GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
2778 &do_write_remote, s5r);
2780 if (s5r->fwdrtask == GNUNET_SCHEDULER_NO_TASK)
2783 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
2785 &do_read_remote, s5r);
2795 * Accept new incoming connections
2797 * @param cls the closure
2798 * @param tc the scheduler context
2801 do_accept (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2803 struct GNUNET_NETWORK_Handle *s;
2804 struct Socks5Request *s5r;
2806 ltask = GNUNET_SCHEDULER_NO_TASK;
2807 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
2810 ltask = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
2814 s = GNUNET_NETWORK_socket_accept (lsock, NULL, NULL);
2818 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "accept");
2822 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2823 "Got an inbound connection, waiting for data\n");
2825 s5r = GNUNET_malloc (sizeof (struct Socks5Request));
2827 s5r->state = SOCKS5_INIT;
2828 s5r->wtask = GNUNET_SCHEDULER_NO_TASK;
2829 s5r->fwdwtask = GNUNET_SCHEDULER_NO_TASK;
2830 s5r->fwdrtask = GNUNET_SCHEDULER_NO_TASK;
2831 s5r->rtask = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
2838 * Task run on shutdown
2840 * @param cls closure
2841 * @param tc task context
2844 do_shutdown (void *cls,
2845 const struct GNUNET_SCHEDULER_TaskContext *tc)
2848 struct MhdHttpList *hd;
2849 struct MhdHttpList *tmp_hd;
2850 struct NetworkHandleList *nh;
2851 struct NetworkHandleList *tmp_nh;
2852 struct ProxyCurlTask *ctask;
2853 struct ProxyCurlTask *ctask_tmp;
2855 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2856 "Shutting down...\n");
2858 gnutls_global_deinit ();
2860 if (NULL != local_gns_zone)
2861 GNUNET_free (local_gns_zone);
2862 if (NULL != local_private_zone)
2863 GNUNET_free (local_private_zone);
2864 if (NULL != local_shorten_zone)
2865 GNUNET_free (local_shorten_zone);
2867 if (GNUNET_SCHEDULER_NO_TASK != curl_download_task)
2869 GNUNET_SCHEDULER_cancel (curl_download_task);
2870 curl_download_task = GNUNET_SCHEDULER_NO_TASK;
2873 for (hd = mhd_httpd_head; hd != NULL; hd = tmp_hd)
2877 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2878 "Stopping daemon\n");
2880 if (GNUNET_SCHEDULER_NO_TASK != hd->httpd_task)
2882 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2883 "Stopping select task %d\n",
2885 GNUNET_SCHEDULER_cancel (hd->httpd_task);
2886 hd->httpd_task = GNUNET_SCHEDULER_NO_TASK;
2889 if (NULL != hd->daemon)
2891 MHD_stop_daemon (hd->daemon);
2895 for (nh = hd->socket_handles_head; nh != NULL; nh = tmp_nh)
2899 GNUNET_NETWORK_socket_close (nh->h);
2904 if (NULL != hd->proxy_cert)
2906 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2907 "Free certificate\n");
2908 GNUNET_free (hd->proxy_cert);
2914 for (ctask=ctasks_head; ctask != NULL; ctask=ctask_tmp)
2916 ctask_tmp = ctask->next;
2918 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2919 "Cleaning up cURL task\n");
2921 if (ctask->curl != NULL)
2922 curl_easy_cleanup (ctask->curl);
2924 if (NULL != ctask->headers)
2925 curl_slist_free_all (ctask->headers);
2926 if (NULL != ctask->resolver)
2927 curl_slist_free_all (ctask->resolver);
2929 if (NULL != ctask->response)
2930 MHD_destroy_response (ctask->response);
2933 GNUNET_free (ctask);
2935 curl_multi_cleanup (curl_multi);
2937 GNUNET_GNS_disconnect (gns_handle);
2942 * Compiles a regex for us
2944 * @param re ptr to re struct
2945 * @param rt the expression to compile
2946 * @return 0 on success
2949 compile_regex (regex_t *re, const char* rt)
2954 status = regcomp (re, rt, REG_EXTENDED|REG_NEWLINE);
2957 regerror (status, re, err, 1024);
2958 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2959 "Regex error compiling '%s': %s\n", rt, err);
2967 * Loads the users local zone key
2969 * @return GNUNET_YES on success
2972 load_local_zone_key (const struct GNUNET_CONFIGURATION_Handle *cfg)
2975 struct GNUNET_CRYPTO_RsaPrivateKey *key = NULL;
2976 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pkey;
2977 struct GNUNET_CRYPTO_ShortHashCode *zone = NULL;
2978 struct GNUNET_CRYPTO_ShortHashAsciiEncoded zonename;
2980 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "gns",
2981 "ZONEKEY", &keyfile))
2983 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2984 "Unable to load zone key config value!\n");
2988 if (GNUNET_NO == GNUNET_DISK_file_test (keyfile))
2990 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2991 "Unable to load zone key %s!\n", keyfile);
2992 GNUNET_free(keyfile);
2996 key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
2997 GNUNET_CRYPTO_rsa_key_get_public (key, &pkey);
2998 local_gns_zone = GNUNET_malloc (sizeof (struct GNUNET_CRYPTO_ShortHashCode));
2999 GNUNET_CRYPTO_short_hash(&pkey,
3000 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
3002 zone = local_gns_zone;
3003 GNUNET_CRYPTO_short_hash_to_enc (zone, &zonename);
3004 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3005 "Using zone: %s!\n", &zonename);
3006 GNUNET_CRYPTO_rsa_key_free(key);
3007 GNUNET_free(keyfile);
3010 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "gns",
3011 "PRIVATE_ZONEKEY", &keyfile))
3013 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3014 "Unable to load private zone key config value!\n");
3017 if ((NULL != keyfile) && (GNUNET_NO == GNUNET_DISK_file_test (keyfile)))
3019 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3020 "Unable to load private zone key %s!\n", keyfile);
3021 GNUNET_free(keyfile);
3025 key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
3026 GNUNET_CRYPTO_rsa_key_get_public (key, &pkey);
3027 local_private_zone = GNUNET_malloc (sizeof (struct GNUNET_CRYPTO_ShortHashCode));
3028 GNUNET_CRYPTO_short_hash(&pkey,
3029 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
3030 local_private_zone);
3031 GNUNET_CRYPTO_short_hash_to_enc (zone, &zonename);
3032 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3033 "Using private zone: %s!\n", &zonename);
3034 GNUNET_CRYPTO_rsa_key_free(key);
3035 GNUNET_free(keyfile);
3039 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "gns",
3040 "SHORTEN_ZONEKEY", &keyfile))
3042 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3043 "Unable to load shorten zone key config value!\n");
3046 if ((NULL != keyfile) && (GNUNET_NO == GNUNET_DISK_file_test (keyfile)))
3048 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3049 "Unable to load shorten zone key %s!\n", keyfile);
3050 GNUNET_free(keyfile);
3054 key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
3055 GNUNET_CRYPTO_rsa_key_get_public (key, &pkey);
3056 local_shorten_zone = GNUNET_malloc (sizeof (struct GNUNET_CRYPTO_ShortHashCode));
3057 GNUNET_CRYPTO_short_hash(&pkey,
3058 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
3059 local_shorten_zone);
3060 GNUNET_CRYPTO_short_hash_to_enc (zone, &zonename);
3061 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3062 "Using shorten zone: %s!\n", &zonename);
3063 GNUNET_CRYPTO_rsa_key_free(key);
3064 GNUNET_free(keyfile);
3071 * Main function that will be run
3073 * @param cls closure
3074 * @param args remaining command-line arguments
3075 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
3076 * @param cfg configuration
3079 run (void *cls, char *const *args, const char *cfgfile,
3080 const struct GNUNET_CONFIGURATION_Handle *cfg)
3082 struct sockaddr_in sa;
3083 struct MhdHttpList *hd;
3084 struct sockaddr_un mhd_unix_sock_addr;
3086 char* proxy_sockfile;
3087 char* cafile_cfg = NULL;
3090 curl_multi = curl_multi_init ();
3092 if (NULL == curl_multi)
3094 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3095 "Failed to create cURL multo handle!\n");
3099 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3101 cafile = cafile_opt;
3104 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "gns-proxy",
3108 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3109 "Unable to load proxy CA config value!\n");
3110 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3111 "No proxy CA provided!\n");
3114 cafile = cafile_cfg;
3117 gnutls_global_init ();
3118 gnutls_x509_crt_init (&proxy_ca.cert);
3119 gnutls_x509_privkey_init (&proxy_ca.key);
3121 if ( (GNUNET_OK != load_cert_from_file (proxy_ca.cert, cafile)) ||
3122 (GNUNET_OK != load_key_from_file (proxy_ca.key, cafile)) )
3124 // FIXME: release resources...
3128 GNUNET_free_non_null (cafile_cfg);
3130 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3131 "Loading Template\n");
3133 compile_regex (&re_dotplus, (char*) RE_A_HREF);
3135 gns_handle = GNUNET_GNS_connect (cfg);
3137 if (GNUNET_NO == load_local_zone_key (cfg))
3139 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3140 "Unable to load zone!\n");
3144 if (NULL == gns_handle)
3146 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3147 "Unable to connect to GNS!\n");
3151 memset (&sa, 0, sizeof (sa));
3152 sa.sin_family = AF_INET;
3153 sa.sin_port = htons (port);
3154 #if HAVE_SOCKADDR_IN_SIN_LEN
3155 sa.sin_len = sizeof (sa);
3158 lsock = GNUNET_NETWORK_socket_create (AF_INET,
3162 if ((NULL == lsock) ||
3164 GNUNET_NETWORK_socket_bind (lsock, (const struct sockaddr *) &sa,
3167 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3168 "Failed to create listen socket bound to `%s'",
3169 GNUNET_a2s ((const struct sockaddr *) &sa, sizeof (sa)));
3171 GNUNET_NETWORK_socket_close (lsock);
3175 if (GNUNET_OK != GNUNET_NETWORK_socket_listen (lsock, 5))
3177 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3178 "Failed to listen on socket bound to `%s'",
3179 GNUNET_a2s ((const struct sockaddr *) &sa, sizeof (sa)));
3183 ltask = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
3184 lsock, &do_accept, NULL);
3189 if (0 != curl_global_init (CURL_GLOBAL_WIN32))
3191 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3192 "cURL global init failed!\n");
3196 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3197 "Proxy listens on port %u\n",
3200 mhd_httpd_head = NULL;
3201 mhd_httpd_tail = NULL;
3202 total_mhd_connections = 0;
3204 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "gns-proxy",
3208 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3209 "Specify PROXY_UNIXPATH in gns-proxy config section!\n");
3213 mhd_unix_socket = GNUNET_NETWORK_socket_create (AF_UNIX,
3217 if (NULL == mhd_unix_socket)
3219 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3220 "Unable to create unix domain socket!\n");
3224 mhd_unix_sock_addr.sun_family = AF_UNIX;
3225 strcpy (mhd_unix_sock_addr.sun_path, proxy_sockfile);
3228 mhd_unix_sock_addr.sun_path[0] = '\0';
3230 #if HAVE_SOCKADDR_IN_SIN_LEN
3231 mhd_unix_sock_addr.sun_len = (u_char) sizeof (struct sockaddr_un);
3234 len = strlen (proxy_sockfile) + sizeof(AF_UNIX);
3236 GNUNET_free (proxy_sockfile);
3238 if (GNUNET_OK != GNUNET_NETWORK_socket_bind (mhd_unix_socket,
3239 (struct sockaddr*)&mhd_unix_sock_addr,
3242 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3243 "Unable to bind unix domain socket!\n");
3247 if (GNUNET_OK != GNUNET_NETWORK_socket_listen (mhd_unix_socket,
3250 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3251 "Unable to listen on unix domain socket!\n");
3255 hd = GNUNET_malloc (sizeof (struct MhdHttpList));
3256 hd->is_ssl = GNUNET_NO;
3257 strcpy (hd->domain, "");
3258 httpd = MHD_start_daemon (MHD_USE_DEBUG, 4444, //Dummy port
3260 &create_response, hd,
3261 MHD_OPTION_LISTEN_SOCKET, GNUNET_NETWORK_get_fd (mhd_unix_socket),
3262 MHD_OPTION_CONNECTION_LIMIT, MHD_MAX_CONNECTIONS,
3263 MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) 16,
3264 MHD_OPTION_NOTIFY_COMPLETED,
3268 GNUNET_assert (httpd != NULL);
3270 hd->httpd_task = GNUNET_SCHEDULER_NO_TASK;
3272 GNUNET_CONTAINER_DLL_insert (mhd_httpd_head, mhd_httpd_tail, hd);
3276 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
3277 &do_shutdown, NULL);
3283 * The main function for gnunet-gns-proxy.
3285 * @param argc number of arguments from the command line
3286 * @param argv command line arguments
3287 * @return 0 ok, 1 on error
3290 main (int argc, char *const *argv)
3292 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
3294 gettext_noop ("listen on specified port"), 1,
3295 &GNUNET_GETOPT_set_string, &port},
3296 {'a', "authority", NULL,
3297 gettext_noop ("pem file to use as CA"), 1,
3298 &GNUNET_GETOPT_set_string, &cafile_opt},
3299 GNUNET_GETOPT_OPTION_END
3304 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
3308 GNUNET_log_setup ("gnunet-gns-proxy", "WARNING", NULL);
3311 GNUNET_PROGRAM_run (argc, argv, "gnunet-gns-proxy",
3312 _("GNUnet GNS proxy"),
3314 &run, NULL)) ? 0 : 1;
3315 GNUNET_free_non_null ((char*)argv);