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
40 /* MHD/cURL defines */
41 #define BUF_WAIT_FOR_CURL 0
42 #define BUF_WAIT_FOR_MHD 1
43 #define HTML_HDR_CONTENT "Content-Type: text/html"
46 //#define RE_DOTPLUS "<a href=\"http://(([A-Za-z]+[.])+)([+])"
47 #define RE_A_HREF "<a href=\"https?://(([A-Za-z0-9]+[.])+)([+]|zkey)"
48 #define RE_N_MATCHES 4
50 /* The usual suspects */
52 #define HTTPS_PORT 443
56 * A structure for CA cert/key
61 gnutls_x509_crt_t cert;
64 gnutls_x509_privkey_t key;
69 * Structure for GNS certificates
71 struct ProxyGNSCertificate
73 /* The certificate as PEM */
76 /* The private key as PEM */
82 * A structure for socks requests
86 /* The client socket */
87 struct GNUNET_NETWORK_Handle *sock;
89 /* The server socket */
90 struct GNUNET_NETWORK_Handle *remote_sock;
95 /* Client socket read task */
96 GNUNET_SCHEDULER_TaskIdentifier rtask;
98 /* Server socket read task */
99 GNUNET_SCHEDULER_TaskIdentifier fwdrtask;
101 /* Client socket write task */
102 GNUNET_SCHEDULER_TaskIdentifier wtask;
104 /* Server socket write task */
105 GNUNET_SCHEDULER_TaskIdentifier fwdwtask;
113 /* Length of data in read buffer */
114 unsigned int rbuf_len;
116 /* Length of data in write buffer */
117 unsigned int wbuf_len;
119 /* This handle is scheduled for cleanup? */
122 /* Shall we close the client socket on cleanup? */
127 * DLL for Network Handles
129 struct NetworkHandleList
132 struct NetworkHandleList *next;
135 struct NetworkHandleList *prev;
138 struct GNUNET_NETWORK_Handle *h;
142 * A structure for all running Httpds
147 struct MhdHttpList *prev;
150 struct MhdHttpList *next;
152 /* is this an ssl daemon? */
155 /* the domain name to server (only important for SSL) */
158 /* The daemon handle */
159 struct MHD_Daemon *daemon;
161 /* Optional proxy certificate used */
162 struct ProxyGNSCertificate *proxy_cert;
165 GNUNET_SCHEDULER_TaskIdentifier httpd_task;
167 /* Handles associated with this daemon */
168 struct NetworkHandleList *socket_handles_head;
170 /* Handles associated with this daemon */
171 struct NetworkHandleList *socket_handles_tail;
175 * A structure for MHD<->cURL streams
180 struct ProxyCurlTask *prev;
183 struct ProxyCurlTask *next;
188 /* Optional header replacements for curl (LEHO) */
189 struct curl_slist *headers;
191 /* Optional resolver replacements for curl (LEHO) */
192 struct curl_slist *resolver;
194 /* The URL to fetch */
197 /* The cURL write buffer / MHD read buffer */
198 char buffer[CURL_MAX_WRITE_SIZE];
200 /* The pointer to the data in the buffer */
203 /* The buffer status (BUF_WAIT_FOR_CURL or BUF_WAIT_FOR_MHD) */
206 /* Number of bytes in buffer */
207 unsigned int bytes_in_buffer;
209 /* Indicates wheather the download is in progress */
210 int download_in_progress;
212 /* Indicates wheather the download was successful */
213 int download_successful;
215 /* Indicates wheather the download failed */
218 /* Indicates wheather we need to parse HTML */
221 /* Indicates wheather we are postprocessing the HTML right now */
222 int is_postprocessing;
224 /* Indicates wheather postprocessing has finished */
227 /* Task ID of the postprocessing task */
228 GNUNET_SCHEDULER_TaskIdentifier pp_task;
230 /* The postprocessing buffer TODO length? */
233 /* The authority of the corresponding host (site of origin) */
236 /* The hostname (Host header field) */
239 /* The LEgacy HOstname (can be empty) */
242 /* The associated daemon list entry */
243 struct MhdHttpList *mhd;
245 /* The associated response */
246 struct MHD_Response *response;
249 struct ProxySetCookieHeader *set_cookies_head;
252 struct ProxySetCookieHeader *set_cookies_tail;
254 /* connection status */
261 struct MHD_Connection *connection;
266 * Struct for set-cookies
268 struct ProxySetCookieHeader
271 struct ProxySetCookieHeader *next;
274 struct ProxySetCookieHeader *prev;
280 /* The port the proxy is running on (default 7777) */
281 static unsigned long port = GNUNET_GNS_PROXY_PORT;
283 /* The CA file (pem) to use for the proxy CA */
284 static char* cafile_opt;
286 /* The listen socket of the proxy */
287 static struct GNUNET_NETWORK_Handle *lsock;
289 /* The listen task ID */
290 GNUNET_SCHEDULER_TaskIdentifier ltask;
292 /* The cURL download task */
293 GNUNET_SCHEDULER_TaskIdentifier curl_download_task;
295 /* The non SSL httpd daemon handle */
296 static struct MHD_Daemon *httpd;
298 /* Number of current mhd connections */
299 static unsigned int total_mhd_connections;
301 /* The cURL multi handle */
302 static CURLM *curl_multi;
304 /* Handle to the GNS service */
305 static struct GNUNET_GNS_Handle *gns_handle;
307 /* DLL for ProxyCurlTasks */
308 static struct ProxyCurlTask *ctasks_head;
310 /* DLL for ProxyCurlTasks */
311 static struct ProxyCurlTask *ctasks_tail;
313 /* DLL for http daemons */
314 static struct MhdHttpList *mhd_httpd_head;
316 /* DLL for http daemons */
317 static struct MhdHttpList *mhd_httpd_tail;
319 /* Handle to the regex for dotplus (.+) replacement in HTML */
320 static regex_t re_dotplus;
322 /* The users local GNS zone hash */
323 static struct GNUNET_CRYPTO_ShortHashCode local_gns_zone;
325 /* The users local private zone */
326 static struct GNUNET_CRYPTO_ShortHashCode local_private_zone;
328 /* The users local shorten zone */
329 static struct GNUNET_CRYPTO_ShortHashCode local_shorten_zone;
331 /* The CA for SSL certificate generation */
332 static struct ProxyCA proxy_ca;
334 /* UNIX domain socket for mhd */
335 struct GNUNET_NETWORK_Handle *mhd_unix_socket;
337 /* Shorten zone private key */
338 struct GNUNET_CRYPTO_RsaPrivateKey *shorten_zonekey;
341 * Checks if name is in tld
343 * @param name the name to check
344 * @param tld the TLD to check for
345 * @return GNUNET_YES or GNUNET_NO
348 is_tld(const char* name, const char* tld)
352 if (strlen(name) <= strlen(tld))
357 offset = strlen(name)-strlen(tld);
358 if (strcmp (name+offset, tld) != 0)
360 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
361 "%s is not in .%s TLD\n", name, tld);
370 * Read HTTP request header field 'Host'
372 * @param cls buffer to write to
373 * @param kind value kind
374 * @param key field key
375 * @param value field value
376 * @return MHD_NO when Host found
379 con_val_iter (void *cls,
380 enum MHD_ValueKind kind,
384 char* buf = (char*)cls;
386 if (0 == strcmp ("Host", key))
396 * Check HTTP response header for mime
398 * @param buffer curl buffer
399 * @param size curl blocksize
400 * @param nmemb curl blocknumber
402 * @return size of read bytes
405 curl_check_hdr (void *buffer, size_t size, size_t nmemb, void *cls)
407 size_t bytes = size * nmemb;
408 struct ProxyCurlTask *ctask = cls;
409 int html_mime_len = strlen (HTML_HDR_CONTENT);
410 int cookie_hdr_len = strlen (MHD_HTTP_HEADER_SET_COOKIE);
411 char hdr_mime[html_mime_len+1];
412 char hdr_cookie[bytes+1];
413 char new_cookie_hdr[bytes+strlen (ctask->leho)+1];
420 if (html_mime_len <= bytes)
422 memcpy (hdr_mime, buffer, html_mime_len);
423 hdr_mime[html_mime_len] = '\0';
425 if (0 == strcmp (hdr_mime, HTML_HDR_CONTENT))
427 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
428 "Got HTML HTTP response header\n");
429 ctask->parse_content = GNUNET_YES;
433 if (cookie_hdr_len > bytes)
436 memcpy (hdr_cookie, buffer, bytes);
437 hdr_cookie[bytes] = '\0';
439 if (hdr_cookie[bytes-1] == '\n')
440 hdr_cookie[bytes-1] = '\0';
442 if (hdr_cookie[bytes-2] == '\r')
443 hdr_cookie[bytes-2] = '\0';
445 if (0 == memcmp (hdr_cookie,
446 MHD_HTTP_HEADER_SET_COOKIE,
449 ndup = GNUNET_strdup (hdr_cookie+cookie_hdr_len+1);
450 memset (new_cookie_hdr, 0, sizeof (new_cookie_hdr));
451 tok = strtok (ndup, ";");
453 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
454 "Looking for cookie in : %s\n", hdr_cookie);
456 for (; tok != NULL; tok = strtok (NULL, ";"))
458 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
459 "Got Cookie token: %s\n", tok);
460 //memcpy (new_cookie_hdr+offset, tok, strlen (tok));
461 if (0 == memcmp (tok, " domain", strlen (" domain")))
463 cookie_domain = tok + strlen (" domain") + 1;
465 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
466 "Got Set-Cookie Domain: %s\n", cookie_domain);
468 if (strlen (cookie_domain) < strlen (ctask->leho))
470 delta_cdomain = strlen (ctask->leho) - strlen (cookie_domain);
471 if (0 == strcmp (cookie_domain, ctask->leho + (delta_cdomain)))
473 GNUNET_snprintf (new_cookie_hdr+offset,
474 sizeof (new_cookie_hdr),
475 " domain=%s", ctask->authority);
476 offset += strlen (" domain=") + strlen (ctask->authority);
477 new_cookie_hdr[offset] = ';';
482 else if (strlen (cookie_domain) == strlen (ctask->leho))
484 if (0 == strcmp (cookie_domain, ctask->leho))
486 GNUNET_snprintf (new_cookie_hdr+offset,
487 sizeof (new_cookie_hdr),
488 " domain=%s", ctask->host);
489 offset += strlen (" domain=") + strlen (ctask->host);
490 new_cookie_hdr[offset] = ';';
495 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
496 "Cookie domain invalid\n");
500 memcpy (new_cookie_hdr+offset, tok, strlen (tok));
501 offset += strlen (tok);
502 new_cookie_hdr[offset] = ';';
506 //memcpy (new_cookie_hdr+offset, tok, strlen (tok));
510 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
511 "Got Set-Cookie HTTP header %s\n", new_cookie_hdr);
513 //pch = GNUNET_malloc (sizeof (struct ProxySetCookieHeader));
514 //len = strlen (hdr_cookie) - cookie_hdr_len - 1;
515 //pch->cookie = GNUNET_malloc (len + 1);
516 //memset (pch->cookie, 0, len + 1);
517 //memcpy (pch->cookie, hdr_cookie+cookie_hdr_len+1, len);
518 //GNUNET_CONTAINER_DLL_insert (ctask->set_cookies_head,
519 // ctask->set_cookies_tail,
521 //pch = ctask->set_cookies_head;
522 //while (pch != NULL)
524 if (GNUNET_NO == MHD_add_response_header (ctask->response,
525 MHD_HTTP_HEADER_SET_COOKIE,
528 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
529 "MHD: Error adding set-cookie header field %s\n",
530 hdr_cookie+cookie_hdr_len+1);
532 //GNUNET_free (pch->cookie);
533 //GNUNET_CONTAINER_DLL_remove (ctask->set_cookies_head,
534 // ctask->set_cookies_tail,
537 //pch = ctask->set_cookies_head;
547 * @param hd a http daemon list entry
550 run_httpd (struct MhdHttpList *hd);
562 * Task that simply runs MHD main loop
565 * @param tc task context
568 run_mhd (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
571 struct MhdHttpList *hd = cls;
573 //for (hd=mhd_httpd_head; hd != NULL; hd = hd->next)
574 MHD_run (hd->daemon);
579 * Process cURL download bits
581 * @param ptr buffer with data
582 * @param size size of a record
583 * @param nmemb number of records downloaded
585 * @return number of processed bytes
588 callback_download (void *ptr, size_t size, size_t nmemb, void *ctx)
590 const char *cbuf = ptr;
592 struct ProxyCurlTask *ctask = ctx;
595 ctask->ready_to_queue = GNUNET_YES;
596 /*if (ctask->con_status == MHD_NO)
598 MHD_queue_response (ctask->connection,
601 ctask->con_status = MHD_YES;
610 if (total > sizeof (ctask->buffer))
612 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
613 "CURL gave us too much data to handle (%d)!\n",
618 if (ctask->buf_status == BUF_WAIT_FOR_MHD)
620 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
621 "CURL: Waiting for MHD (%s)\n", ctask->url);
622 return CURL_WRITEFUNC_PAUSE;
626 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
627 "CURL: Copying to MHD (%s, %d)\n", ctask->url, total);
628 memcpy (ctask->buffer, cbuf, total);
629 ctask->bytes_in_buffer = total;
630 ctask->buffer_ptr = ctask->buffer;
632 ctask->buf_status = BUF_WAIT_FOR_MHD;
634 //GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
635 // "cURL chunk:\n%s\n", (char*)ctask->buffer);
636 //run_mhd (NULL, NULL);
637 GNUNET_SCHEDULER_add_now (&run_mhd, ctask->mhd);
642 * Ask cURL for the select sets and schedule download
645 curl_download_prepare ();
648 * Callback to free content
650 * @param cls content to free
651 * @param tc task context
654 mhd_content_free (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
656 struct ProxyCurlTask *ctask = cls;
658 if (NULL != ctask->headers)
659 curl_slist_free_all (ctask->headers);
661 if (NULL != ctask->headers)
662 curl_slist_free_all (ctask->resolver);
664 if (NULL != ctask->response)
665 MHD_destroy_response (ctask->response);
673 * Shorten result callback
675 * @param cls the proxycurltask
676 * @param short_name the shortened name (NULL on error)
679 process_shorten (void* cls, const char* short_name)
681 struct ProxyCurlTask *ctask = cls;
683 char tmp[strlen(ctask->pp_buf)]; //TODO length
685 if (NULL == short_name)
687 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
688 "MHD PP: Unable to shorten %s\n",
693 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
694 "MHD PP: Shorten %s -> %s\n",
698 sprintf (tmp, "<a href=\"http://%s", short_name);
699 strcpy (ctask->pp_buf, tmp);
701 ctask->pp_finished = GNUNET_YES;
703 GNUNET_SCHEDULER_add_now (&run_mhd, ctask->mhd);
708 * Callback for MHD response
711 * @param pos in buffer
713 * @param max space in buffer
714 * @return number of bytes written
717 mhd_content_cb (void *cls,
722 struct ProxyCurlTask *ctask = cls;
724 size_t bytes_to_copy;
727 regmatch_t m[RE_N_MATCHES];
729 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
730 "MHD: content cb %s\n", ctask->url);
732 if (ctask->download_successful &&
733 (ctask->buf_status == BUF_WAIT_FOR_CURL))
735 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
736 "MHD: sending response for %s\n", ctask->url);
737 ctask->download_in_progress = GNUNET_NO;
738 GNUNET_SCHEDULER_add_now (&mhd_content_free, ctask);
739 GNUNET_SCHEDULER_add_now (&run_mhd, ctask->mhd);
740 total_mhd_connections--;
741 return MHD_CONTENT_READER_END_OF_STREAM;
744 if (ctask->download_error &&
745 (ctask->buf_status == BUF_WAIT_FOR_CURL))
747 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
748 "MHD: sending error response\n");
749 ctask->download_in_progress = GNUNET_NO;
750 GNUNET_SCHEDULER_add_now (&mhd_content_free, ctask);
751 GNUNET_SCHEDULER_add_now (&run_mhd, ctask->mhd);
752 total_mhd_connections--;
753 return MHD_CONTENT_READER_END_WITH_ERROR;
756 if ( ctask->buf_status == BUF_WAIT_FOR_CURL )
759 bytes_to_copy = ctask->bytes_in_buffer;
761 if (ctask->parse_content == GNUNET_YES)
764 GNUNET_log ( GNUNET_ERROR_TYPE_DEBUG,
765 "MHD: We need to parse the HTML %s\n", ctask->buffer_ptr);
767 nomatch = regexec ( &re_dotplus, ctask->buffer_ptr, RE_N_MATCHES, m, 0);
771 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
772 "MHD RE: No match\n");
776 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
779 GNUNET_assert (m[1].rm_so != -1);
781 hostptr = ctask->buffer_ptr+m[1].rm_so;
785 bytes_to_copy = m[0].rm_so;
786 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
787 "Copying %d bytes.\n", m[0].rm_so);
793 if (ctask->is_postprocessing == GNUNET_YES)
797 if ( ctask->pp_finished == GNUNET_NO )
799 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
800 "MHD PP: Waiting for PP of %s\n", ctask->pp_buf);
804 ctask->is_postprocessing = GNUNET_NO;
806 ctask->bytes_in_buffer -= m[0].rm_eo;//(m[1].rm_eo-m[1].rm_so);
807 ctask->buffer_ptr += m[0].rm_eo;//(m[1].rm_eo-m[1].rm_so);
808 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
809 "Skipping next %d bytes in buffer\n", m[0].rm_eo);
811 GNUNET_SCHEDULER_add_now (&run_mhd, ctask->mhd);
813 if ( strlen (ctask->pp_buf) <= max )
815 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
816 "Copying postprocessed %s.\n", ctask->pp_buf);
817 memcpy ( buf, ctask->pp_buf, strlen (ctask->pp_buf) );
818 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
820 ctask->is_postprocessing = GNUNET_NO;
821 return strlen (ctask->pp_buf);
827 memset (ctask->pp_buf, 0, sizeof(ctask->pp_buf));
829 /* If .+ extend with authority */
830 if (*(ctask->buffer_ptr+m[1].rm_eo) == '+')
832 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
834 memcpy (ctask->pp_buf, hostptr, (m[1].rm_eo-m[1].rm_so));
835 strcpy ( ctask->pp_buf+strlen(ctask->pp_buf),
838 /* If .zkey simply copy the name */
841 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
843 memcpy (ctask->pp_buf, hostptr, (m[1].rm_eo-m[1].rm_so + strlen (GNUNET_GNS_TLD_ZKEY)));
846 ctask->is_postprocessing = GNUNET_YES;
847 ctask->pp_finished = GNUNET_NO;
849 GNUNET_GNS_shorten_zone (gns_handle,
862 if ( bytes_to_copy > max )
864 GNUNET_log ( GNUNET_ERROR_TYPE_DEBUG,
865 "MHD: buffer in response too small! (%s)\n",
867 memcpy ( buf, ctask->buffer_ptr, max);
868 ctask->bytes_in_buffer -= max;
869 ctask->buffer_ptr += max;
874 GNUNET_log ( GNUNET_ERROR_TYPE_DEBUG,
875 "MHD: copying %d bytes to mhd response at offset %d\n",
878 memcpy ( buf, ctask->buffer_ptr, bytes_to_copy );
879 copied = bytes_to_copy;
880 if (bytes_to_copy < ctask->bytes_in_buffer)
882 ctask->bytes_in_buffer -= bytes_to_copy;
883 ctask->buffer_ptr += bytes_to_copy;
887 ctask->bytes_in_buffer = 0;
888 ctask->buf_status = BUF_WAIT_FOR_CURL;
889 ctask->buffer_ptr = ctask->buffer;
890 if (NULL != ctask->curl)
891 curl_easy_pause (ctask->curl, CURLPAUSE_CONT);
892 GNUNET_SCHEDULER_add_now (&run_mhd, ctask->mhd);
896 GNUNET_SCHEDULER_add_now (&run_mhd, ctask->mhd);
904 * Task that is run when we are ready to receive more data
908 * @param tc task context
911 curl_task_download (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
914 * Ask cURL for the select sets and schedule download
917 curl_download_prepare ()
924 struct GNUNET_NETWORK_FDSet *grs;
925 struct GNUNET_NETWORK_FDSet *gws;
927 struct GNUNET_TIME_Relative rtime;
933 mret = curl_multi_fdset (curl_multi, &rs, &ws, &es, &max);
935 if (mret != CURLM_OK)
937 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
938 "%s failed at %s:%d: `%s'\n",
939 "curl_multi_fdset", __FILE__, __LINE__,
940 curl_multi_strerror (mret));
945 mret = curl_multi_timeout (curl_multi, &to);
946 rtime = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, to);
948 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
949 "cURL multi fds: max=%d timeout=%llu\n", max, to);
951 grs = GNUNET_NETWORK_fdset_create ();
952 gws = GNUNET_NETWORK_fdset_create ();
953 GNUNET_NETWORK_fdset_copy_native (grs, &rs, max + 1);
954 GNUNET_NETWORK_fdset_copy_native (gws, &ws, max + 1);
955 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
956 "Scheduling task cURL\n");
958 if (curl_download_task != GNUNET_SCHEDULER_NO_TASK)
959 GNUNET_SCHEDULER_cancel (curl_download_task);
962 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
965 &curl_task_download, curl_multi);
966 GNUNET_NETWORK_fdset_destroy (gws);
967 GNUNET_NETWORK_fdset_destroy (grs);
973 * Task that is run when we are ready to receive more data
977 * @param tc task context
980 curl_task_download (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
986 struct ProxyCurlTask *ctask;
989 struct ProxyCurlTask *clean_head = NULL;
990 struct ProxyCurlTask *clean_tail = NULL;
992 curl_download_task = GNUNET_SCHEDULER_NO_TASK;
994 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
996 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
997 "Shutdown requested while trying to download\n");
1001 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1009 mret = curl_multi_perform (curl_multi, &running);
1011 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1012 "Running curl tasks: %d\n", running);
1014 ctask = ctasks_head;
1015 for (; ctask != NULL; ctask = ctask->next)
1017 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1018 "CTask: %s\n", ctask->url);
1022 if (num_ctasks != running)
1024 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1025 "%d ctasks, %d curl running\n", num_ctasks, running);
1030 ctask = ctasks_head;
1031 msg = curl_multi_info_read (curl_multi, &msgnum);
1032 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1033 "Messages left: %d\n", msgnum);
1040 if ((msg->data.result != CURLE_OK) &&
1041 (msg->data.result != CURLE_GOT_NOTHING))
1043 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1044 "Download curl failed");
1046 for (; ctask != NULL; ctask = ctask->next)
1048 if (NULL == ctask->curl)
1051 if (memcmp (msg->easy_handle, ctask->curl, sizeof (CURL)) != 0)
1054 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1055 "Download curl failed for task %s: %s.\n",
1057 curl_easy_strerror (msg->data.result));
1058 ctask->download_successful = GNUNET_NO;
1059 ctask->download_error = GNUNET_YES;
1060 //curl_multi_remove_handle (curl_multi, ctask->curl);
1061 //curl_easy_cleanup (ctask->curl);
1062 //ctask->curl = NULL;
1063 GNUNET_CONTAINER_DLL_remove (ctasks_head, ctasks_tail,
1065 GNUNET_CONTAINER_DLL_insert (clean_head, clean_tail, ctask);
1068 GNUNET_assert (ctask != NULL);
1072 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1073 "cURL download completed.\n");
1075 for (; ctask != NULL; ctask = ctask->next)
1077 if (NULL == ctask->curl)
1080 if (memcmp (msg->easy_handle, ctask->curl, sizeof (CURL)) != 0)
1083 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1084 "cURL task %s found.\n", ctask->url);
1085 ctask->download_successful = GNUNET_YES;
1086 //curl_multi_remove_handle (curl_multi, ctask->curl);
1087 //curl_easy_cleanup (ctask->curl);
1088 //ctask->curl = NULL;
1089 GNUNET_CONTAINER_DLL_remove (ctasks_head, ctasks_tail,
1091 GNUNET_CONTAINER_DLL_insert (clean_head, clean_tail, ctask);
1095 GNUNET_assert (ctask != NULL);
1097 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1098 "curl end %s\n", curl_easy_strerror(msg->data.result));
1104 } while (msgnum > 0);
1106 for (ctask=clean_head; ctask != NULL; ctask = ctask->next)
1108 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1109 "Removing cURL task %s.\n", ctask->url);
1110 curl_multi_remove_handle (curl_multi, ctask->curl);
1111 curl_easy_cleanup (ctask->curl);
1116 for (ctask=ctasks_head; ctask != NULL; ctask = ctask->next)
1121 if (num_ctasks != running)
1123 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1124 "%d ctasks, %d curl running\n", num_ctasks, running);
1127 GNUNET_assert ( num_ctasks == running );
1131 } while (mret == CURLM_CALL_MULTI_PERFORM);
1134 if (mret != CURLM_OK)
1136 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "%s failed at %s:%d: `%s'\n",
1137 "curl_multi_perform", __FILE__, __LINE__,
1138 curl_multi_strerror (mret));
1140 curl_download_prepare();
1144 * Process LEHO lookup
1146 * @param cls the ctask
1147 * @param rd_count number of records returned
1148 * @param rd record data
1151 process_leho_lookup (void *cls,
1153 const struct GNUNET_NAMESTORE_RecordData *rd)
1155 struct ProxyCurlTask *ctask = cls;
1156 char hosthdr[262]; //256 + "Host: "
1160 struct hostent *phost;
1162 char resolvename[512];
1165 ctask->headers = NULL;
1167 strcpy (ctask->leho, "");
1170 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1171 "No LEHO present!\n");
1173 for (i=0; i<rd_count; i++)
1175 if (rd[i].record_type != GNUNET_GNS_RECORD_LEHO)
1178 memcpy (ctask->leho, rd[i].data, rd[i].data_size);
1180 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1181 "Found LEHO %s for %s\n", ctask->leho, ctask->url);
1184 if (0 != strcmp (ctask->leho, ""))
1186 sprintf (hosthdr, "%s%s", "Host: ", ctask->leho);
1187 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1188 "New HTTP header value: %s\n", hosthdr);
1189 ctask->headers = curl_slist_append (ctask->headers, hosthdr);
1190 GNUNET_assert (NULL != ctask->headers);
1191 ret = curl_easy_setopt (ctask->curl, CURLOPT_HTTPHEADER, ctask->headers);
1192 if (CURLE_OK != ret)
1194 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "%s failed at %s:%d: `%s'\n",
1195 "curl_easy_setopt", __FILE__, __LINE__, curl_easy_strerror(ret));
1200 if (ctask->mhd->is_ssl)
1202 phost = (struct hostent*)gethostbyname (ctask->host);
1206 ssl_ip = inet_ntoa(*((struct in_addr*)(phost->h_addr)));
1207 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1208 "SSL target server: %s\n", ssl_ip);
1209 sprintf (resolvename, "%s:%d:%s", ctask->leho, HTTPS_PORT, ssl_ip);
1210 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1211 "Curl resolve: %s\n", resolvename);
1212 ctask->resolver = curl_slist_append ( ctask->resolver, resolvename);
1213 curl_easy_setopt (ctask->curl, CURLOPT_RESOLVE, ctask->resolver);
1214 sprintf (curlurl, "https://%s%s", ctask->leho, ctask->url);
1215 curl_easy_setopt (ctask->curl, CURLOPT_URL, curlurl);
1219 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1220 "gethostbyname failed for %s!\n", ctask->host);
1221 ctask->download_successful = GNUNET_NO;
1222 ctask->download_error = GNUNET_YES;
1227 if (CURLM_OK != (mret=curl_multi_add_handle (curl_multi, ctask->curl)))
1229 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1230 "%s failed at %s:%d: `%s'\n",
1231 "curl_multi_add_handle", __FILE__, __LINE__,
1232 curl_multi_strerror (mret));
1233 ctask->download_successful = GNUNET_NO;
1234 ctask->download_error = GNUNET_YES;
1237 GNUNET_CONTAINER_DLL_insert (ctasks_head, ctasks_tail, ctask);
1239 curl_download_prepare ();
1244 * Initialize download and trigger curl
1246 * @param cls the proxycurltask
1247 * @param auth_name the name of the authority (site of origin) of ctask->host
1251 process_get_authority (void *cls,
1252 const char* auth_name)
1254 struct ProxyCurlTask *ctask = cls;
1256 if (NULL == auth_name)
1258 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1259 "Get authority failed!\n");
1260 strcpy (ctask->authority, "");
1264 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1265 "Get authority yielded %s\n", auth_name);
1266 strcpy (ctask->authority, auth_name);
1269 GNUNET_GNS_lookup_zone (gns_handle,
1272 GNUNET_GNS_RECORD_LEHO,
1273 GNUNET_YES, //Only cached for performance
1275 &process_leho_lookup,
1280 * Main MHD callback for handling requests.
1283 * @param con MHD connection handle
1284 * @param url the url in the request
1285 * @param meth the HTTP method used ("GET", "PUT", etc.)
1286 * @param ver the HTTP version string (i.e. "HTTP/1.1")
1287 * @param upload_data the data being uploaded (excluding HEADERS,
1288 * for a POST that fits into memory and that is encoded
1289 * with a supported encoding, the POST data will NOT be
1290 * given in upload_data and is instead available as
1291 * part of MHD_get_connection_values; very large POST
1292 * data *will* be made available incrementally in
1294 * @param upload_data_size set initially to the size of the
1295 * upload_data provided; the method must update this
1296 * value to the number of bytes NOT processed;
1297 * @param con_cls pointer to location where we store the 'struct Request'
1298 * @return MHD_YES if the connection was handled successfully,
1299 * MHD_NO if the socket must be closed due to a serious
1300 * error while handling the request
1303 create_response (void *cls,
1304 struct MHD_Connection *con,
1308 const char *upload_data,
1309 size_t *upload_data_size,
1312 struct MhdHttpList* hd = cls;
1313 const char* page = "<html><head><title>gnoxy</title>"\
1314 "</head><body>cURL fail</body></html>";
1320 struct ProxyCurlTask *ctask;
1323 if (0 != strcmp (meth, "GET"))
1326 if (0 != *upload_data_size)
1329 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1332 MHD_get_connection_values (con,
1334 &con_val_iter, host);
1336 if (NULL == *con_cls)
1338 ctask = GNUNET_malloc (sizeof (struct ProxyCurlTask));
1342 if (curl_multi == NULL)
1343 curl_multi = curl_multi_init ();
1345 ctask->curl = curl_easy_init();
1347 if ((ctask->curl == NULL) || (curl_multi == NULL))
1349 ctask->response = MHD_create_response_from_buffer (strlen (page),
1351 MHD_RESPMEM_PERSISTENT);
1352 ret = MHD_queue_response (con,
1355 MHD_destroy_response (ctask->response);
1356 GNUNET_free (ctask);
1362 ctask->headers = NULL;
1363 ctask->resolver = NULL;
1364 ctask->buffer_ptr = NULL;
1365 ctask->download_in_progress = GNUNET_YES;
1366 ctask->download_successful = GNUNET_NO;
1367 ctask->buf_status = BUF_WAIT_FOR_CURL;
1368 ctask->bytes_in_buffer = 0;
1369 ctask->parse_content = GNUNET_NO;
1370 ctask->connection = con;
1371 ctask->ready_to_queue = MHD_NO;
1372 ctask->fin = GNUNET_NO;
1374 curl_easy_setopt (ctask->curl, CURLOPT_HEADERFUNCTION, &curl_check_hdr);
1375 curl_easy_setopt (ctask->curl, CURLOPT_HEADERDATA, ctask);
1376 curl_easy_setopt (ctask->curl, CURLOPT_WRITEFUNCTION, &callback_download);
1377 curl_easy_setopt (ctask->curl, CURLOPT_WRITEDATA, ctask);
1378 curl_easy_setopt (ctask->curl, CURLOPT_FOLLOWLOCATION, 1);
1379 curl_easy_setopt (ctask->curl, CURLOPT_MAXREDIRS, 4);
1380 /* no need to abort if the above failed */
1381 if (GNUNET_NO == ctask->mhd->is_ssl)
1383 sprintf (curlurl, "http://%s%s", host, url);
1384 curl_easy_setopt (ctask->curl, CURLOPT_URL, curlurl);
1385 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1386 "Adding new curl task for %s\n", curlurl);
1388 strcpy (ctask->host, host);
1389 strcpy (ctask->url, url);
1391 //curl_easy_setopt (ctask->curl, CURLOPT_URL, curlurl);
1392 curl_easy_setopt (ctask->curl, CURLOPT_FAILONERROR, 1);
1393 curl_easy_setopt (ctask->curl, CURLOPT_CONNECTTIMEOUT, 600L);
1394 curl_easy_setopt (ctask->curl, CURLOPT_TIMEOUT, 600L);
1396 GNUNET_GNS_get_authority (gns_handle,
1398 &process_get_authority,
1401 ctask->response = MHD_create_response_from_callback (MHD_SIZE_UNKNOWN,
1407 ctask->ready_to_queue = GNUNET_NO;
1408 ctask->fin = GNUNET_NO;
1412 ctask = (struct ProxyCurlTask *) *con_cls;
1414 if (ctask->fin == GNUNET_YES)
1417 if (ctask->ready_to_queue == GNUNET_YES)
1419 ctask->fin = GNUNET_YES;
1420 return MHD_queue_response (con, MHD_HTTP_OK, ctask->response);
1426 //MHD_destroy_response (response);
1433 * Task run whenever HTTP server operations are pending.
1436 * @param tc sched context
1439 do_httpd (void *cls,
1440 const struct GNUNET_SCHEDULER_TaskContext *tc);
1449 struct MhdHttpList *hd;
1451 for (hd=mhd_httpd_head; hd != NULL; hd = hd->next)
1459 * @param hd the daemon to run
1462 run_httpd (struct MhdHttpList *hd)
1467 struct GNUNET_NETWORK_FDSet *wrs;
1468 struct GNUNET_NETWORK_FDSet *wws;
1469 struct GNUNET_NETWORK_FDSet *wes;
1472 unsigned MHD_LONG_LONG timeout;
1473 struct GNUNET_TIME_Relative tv;
1478 wrs = GNUNET_NETWORK_fdset_create ();
1479 wes = GNUNET_NETWORK_fdset_create ();
1480 wws = GNUNET_NETWORK_fdset_create ();
1482 GNUNET_assert (MHD_YES == MHD_get_fdset (hd->daemon, &rs, &ws, &es, &max));
1485 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1486 "MHD fds: max=%d\n", max);
1488 haveto = MHD_get_timeout (hd->daemon, &timeout);
1490 if (haveto == MHD_YES)
1491 tv.rel_value = (uint64_t) timeout;
1493 tv = GNUNET_TIME_UNIT_FOREVER_REL;
1494 GNUNET_NETWORK_fdset_copy_native (wrs, &rs, max + 1);
1495 GNUNET_NETWORK_fdset_copy_native (wws, &ws, max + 1);
1496 GNUNET_NETWORK_fdset_copy_native (wes, &es, max + 1);
1498 if (hd->httpd_task != GNUNET_SCHEDULER_NO_TASK)
1499 GNUNET_SCHEDULER_cancel (hd->httpd_task);
1501 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_HIGH,
1504 GNUNET_NETWORK_fdset_destroy (wrs);
1505 GNUNET_NETWORK_fdset_destroy (wws);
1506 GNUNET_NETWORK_fdset_destroy (wes);
1511 * Task run whenever HTTP server operations are pending.
1514 * @param tc sched context
1517 do_httpd (void *cls,
1518 const struct GNUNET_SCHEDULER_TaskContext *tc)
1520 struct MhdHttpList *hd = cls;
1522 hd->httpd_task = GNUNET_SCHEDULER_NO_TASK;
1524 MHD_run (hd->daemon);
1530 * Read data from socket
1532 * @param cls the closure
1533 * @param tc scheduler context
1536 do_read (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
1539 * Read from remote end
1541 * @param cls closure
1542 * @param tc scheduler context
1545 do_read_remote (void* cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
1548 * Write data to remote socket
1550 * @param cls the closure
1551 * @param tc scheduler context
1554 do_write_remote (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1556 struct Socks5Request *s5r = cls;
1559 s5r->fwdwtask = GNUNET_SCHEDULER_NO_TASK;
1561 if ((NULL != tc->read_ready) &&
1562 (GNUNET_NETWORK_fdset_isset (tc->write_ready, s5r->remote_sock)) &&
1563 ((len = GNUNET_NETWORK_socket_send (s5r->remote_sock, s5r->rbuf,
1566 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1567 "Successfully sent %d bytes to remote socket\n",
1572 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "write remote");
1574 if (s5r->rtask != GNUNET_SCHEDULER_NO_TASK)
1575 GNUNET_SCHEDULER_cancel (s5r->rtask);
1576 if (s5r->wtask != GNUNET_SCHEDULER_NO_TASK)
1577 GNUNET_SCHEDULER_cancel (s5r->wtask);
1578 if (s5r->fwdrtask != GNUNET_SCHEDULER_NO_TASK)
1579 GNUNET_SCHEDULER_cancel (s5r->fwdrtask);
1580 GNUNET_NETWORK_socket_close (s5r->remote_sock);
1581 GNUNET_NETWORK_socket_close (s5r->sock);
1587 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
1594 * Clean up s5r handles
1596 * @param s5r the handle to destroy
1599 cleanup_s5r (struct Socks5Request *s5r)
1601 if (s5r->rtask != GNUNET_SCHEDULER_NO_TASK)
1602 GNUNET_SCHEDULER_cancel (s5r->rtask);
1603 if (s5r->fwdwtask != GNUNET_SCHEDULER_NO_TASK)
1604 GNUNET_SCHEDULER_cancel (s5r->fwdwtask);
1605 if (s5r->fwdrtask != GNUNET_SCHEDULER_NO_TASK)
1606 GNUNET_SCHEDULER_cancel (s5r->fwdrtask);
1608 if (NULL != s5r->remote_sock)
1609 GNUNET_NETWORK_socket_close (s5r->remote_sock);
1610 if ((NULL != s5r->sock) && (s5r->cleanup_sock == GNUNET_YES))
1611 GNUNET_NETWORK_socket_close (s5r->sock);
1617 * Write data to socket
1619 * @param cls the closure
1620 * @param tc scheduler context
1623 do_write (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1625 struct Socks5Request *s5r = cls;
1628 s5r->wtask = GNUNET_SCHEDULER_NO_TASK;
1630 if ((NULL != tc->read_ready) &&
1631 (GNUNET_NETWORK_fdset_isset (tc->write_ready, s5r->sock)) &&
1632 ((len = GNUNET_NETWORK_socket_send (s5r->sock, s5r->wbuf,
1635 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1636 "Successfully sent %d bytes to socket\n",
1642 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "write");
1643 s5r->cleanup = GNUNET_YES;
1644 s5r->cleanup_sock = GNUNET_YES;
1650 if (GNUNET_YES == s5r->cleanup)
1656 if ((s5r->state == SOCKS5_DATA_TRANSFER) &&
1657 (s5r->fwdrtask == GNUNET_SCHEDULER_NO_TASK))
1659 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
1661 &do_read_remote, s5r);
1665 * Read from remote end
1667 * @param cls closure
1668 * @param tc scheduler context
1671 do_read_remote (void* cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1673 struct Socks5Request *s5r = cls;
1675 s5r->fwdrtask = GNUNET_SCHEDULER_NO_TASK;
1678 if ((NULL != tc->write_ready) &&
1679 (GNUNET_NETWORK_fdset_isset (tc->read_ready, s5r->remote_sock)) &&
1680 (s5r->wbuf_len = GNUNET_NETWORK_socket_recv (s5r->remote_sock, s5r->wbuf,
1681 sizeof (s5r->wbuf))))
1683 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1684 "Successfully read %d bytes from remote socket\n",
1689 if (s5r->wbuf_len == 0)
1690 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1691 "0 bytes received from remote... graceful shutdown!\n");
1692 if (s5r->fwdwtask != GNUNET_SCHEDULER_NO_TASK)
1693 GNUNET_SCHEDULER_cancel (s5r->fwdwtask);
1694 if (s5r->rtask != GNUNET_SCHEDULER_NO_TASK)
1695 GNUNET_SCHEDULER_cancel (s5r->rtask);
1697 GNUNET_NETWORK_socket_close (s5r->remote_sock);
1698 s5r->remote_sock = NULL;
1699 GNUNET_NETWORK_socket_close (s5r->sock);
1705 s5r->wtask = GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
1713 * Adds a socket to MHD
1715 * @param h the handle to the socket to add
1716 * @param daemon the daemon to add the fd to
1717 * @return whatever MHD_add_connection returns
1720 add_handle_to_mhd (struct GNUNET_NETWORK_Handle *h, struct MHD_Daemon *daemon)
1723 struct sockaddr *addr;
1726 fd = dup (GNUNET_NETWORK_get_fd (h));
1727 addr = GNUNET_NETWORK_get_addr (h);
1728 len = GNUNET_NETWORK_get_addrlen (h);
1730 return MHD_add_connection (daemon, fd, addr, len);
1734 * Calculate size of file
1736 * @param filename name of file
1737 * @return filesize or 0 on error
1740 get_file_size (const char* filename)
1744 fp = fopen (filename, "rb");
1749 if ((0 != fseek (fp, 0, SEEK_END)) || (-1 == (size = ftell (fp))))
1761 * Read file in filename
1763 * @param filename file to read
1764 * @param size pointer where filesize is stored
1768 load_file (const char* filename, unsigned int* size)
1773 *size = get_file_size (filename);
1777 fp = fopen (filename, "rb");
1781 buffer = GNUNET_malloc (*size);
1788 if (*size != fread (buffer, 1, *size, fp))
1790 GNUNET_free (buffer);
1799 * Load PEM key from file
1801 * @param key where to store the data
1802 * @param keyfile path to the PEM file
1805 load_key_from_file (gnutls_x509_privkey_t key, char* keyfile)
1807 gnutls_datum_t key_data;
1808 key_data.data = NULL;
1811 key_data.data = (unsigned char*)load_file (keyfile, &key_data.size);
1813 ret = gnutls_x509_privkey_import (key, &key_data,
1814 GNUTLS_X509_FMT_PEM);
1816 if (GNUTLS_E_SUCCESS != ret)
1818 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1819 "Unable to import private key %s(ret=%d)\n", key_data.data, ret);
1823 GNUNET_free (key_data.data);
1827 * Load cert from file
1829 * @param crt struct to store data in
1830 * @param certfile path to pem file
1833 load_cert_from_file (gnutls_x509_crt_t crt, char* certfile)
1835 gnutls_datum_t cert_data;
1836 cert_data.data = NULL;
1839 cert_data.data = (unsigned char*)load_file (certfile, &cert_data.size);
1841 ret = gnutls_x509_crt_import (crt, &cert_data,
1842 GNUTLS_X509_FMT_PEM);
1843 if (GNUTLS_E_SUCCESS != ret)
1845 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1846 "Unable to import certificate %s(ret=%d)\n", certfile, ret);
1850 GNUNET_free (cert_data.data);
1856 * Generate new certificate for specific name
1858 * @param name the subject name to generate a cert for
1859 * @return a struct holding the PEM data
1861 static struct ProxyGNSCertificate *
1862 generate_gns_certificate (const char *name)
1866 unsigned int serial;
1867 size_t key_buf_size;
1868 size_t cert_buf_size;
1869 gnutls_x509_crt_t request;
1873 ret = gnutls_x509_crt_init (&request);
1875 if (GNUTLS_E_SUCCESS != ret)
1880 ret = gnutls_x509_crt_set_key (request, proxy_ca.key);
1882 if (GNUTLS_E_SUCCESS != ret)
1887 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Generating cert\n");
1889 struct ProxyGNSCertificate *pgc =
1890 GNUNET_malloc (sizeof (struct ProxyGNSCertificate));
1892 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Adding DNs\n");
1894 gnutls_x509_crt_set_dn_by_oid (request, GNUTLS_OID_X520_COUNTRY_NAME,
1897 gnutls_x509_crt_set_dn_by_oid (request, GNUTLS_OID_X520_ORGANIZATION_NAME,
1900 gnutls_x509_crt_set_dn_by_oid (request, GNUTLS_OID_X520_COMMON_NAME,
1901 0, name, strlen (name));
1903 ret = gnutls_x509_crt_set_version (request, 3);
1905 ret = gnutls_rnd (GNUTLS_RND_NONCE, &serial, sizeof (serial));
1907 etime = time (NULL);
1908 tm_data = localtime (&etime);
1911 ret = gnutls_x509_crt_set_serial (request,
1915 ret = gnutls_x509_crt_set_activation_time (request,
1918 etime = mktime (tm_data);
1925 ret = gnutls_x509_crt_set_expiration_time (request,
1927 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Signing...\n");
1929 ret = gnutls_x509_crt_sign (request, proxy_ca.cert, proxy_ca.key);
1931 key_buf_size = sizeof (pgc->key);
1932 cert_buf_size = sizeof (pgc->cert);
1934 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Exporting certificate...\n");
1936 gnutls_x509_crt_export (request, GNUTLS_X509_FMT_PEM,
1937 pgc->cert, &cert_buf_size);
1939 gnutls_x509_privkey_export (proxy_ca.key, GNUTLS_X509_FMT_PEM,
1940 pgc->key, &key_buf_size);
1943 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Cleaning up\n");
1944 gnutls_x509_crt_deinit (request);
1952 * Accept policy for mhdaemons
1955 * @param addr the sockaddr
1956 * @param addrlen the sockaddr length
1957 * @return MHD_NO if sockaddr is wrong or #conns too high
1960 accept_cb (void* cls, const struct sockaddr *addr, socklen_t addrlen)
1962 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1963 "In MHD accept policy cb\n");
1967 if (addr->sa_family == AF_UNIX)
1971 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1972 "Connection accepted\n");
1979 * Adds a socket to an SSL MHD instance
1980 * It is important the the domain name is
1981 * correct. In most cases we need to start a new daemon
1983 * @param h the handle to add to a daemon
1984 * @param domain the domain the ssl daemon has to serve
1985 * @return MHD_YES on success
1988 add_handle_to_ssl_mhd (struct GNUNET_NETWORK_Handle *h, char* domain)
1990 struct MhdHttpList *hd = NULL;
1991 struct ProxyGNSCertificate *pgc;
1992 struct NetworkHandleList *nh;
1994 for (hd = mhd_httpd_head; hd != NULL; hd = hd->next)
1996 if (0 == strcmp (hd->domain, domain))
2003 pgc = generate_gns_certificate (domain);
2005 hd = GNUNET_malloc (sizeof (struct MhdHttpList));
2006 hd->is_ssl = GNUNET_YES;
2007 strcpy (hd->domain, domain);
2008 hd->proxy_cert = pgc;
2011 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2012 "No previous SSL instance found... starting new one for %s\n",
2015 hd->daemon = MHD_start_daemon (MHD_USE_DEBUG | MHD_USE_SSL, 4444,
2017 &create_response, hd,
2018 MHD_OPTION_LISTEN_SOCKET, GNUNET_NETWORK_get_fd (mhd_unix_socket),
2019 MHD_OPTION_CONNECTION_LIMIT, MHD_MAX_CONNECTIONS,
2020 MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) 16,
2021 MHD_OPTION_NOTIFY_COMPLETED,
2023 MHD_OPTION_HTTPS_MEM_KEY, pgc->key,
2024 MHD_OPTION_HTTPS_MEM_CERT, pgc->cert,
2027 GNUNET_assert (hd->daemon != NULL);
2028 hd->httpd_task = GNUNET_SCHEDULER_NO_TASK;
2030 GNUNET_CONTAINER_DLL_insert (mhd_httpd_head, mhd_httpd_tail, hd);
2033 nh = GNUNET_malloc (sizeof (struct NetworkHandleList));
2036 GNUNET_CONTAINER_DLL_insert (hd->socket_handles_head,
2037 hd->socket_handles_tail,
2040 return add_handle_to_mhd (h, hd->daemon);
2046 * Read data from incoming connection
2048 * @param cls the closure
2049 * @param tc the scheduler context
2052 do_read (void* cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2054 struct Socks5Request *s5r = cls;
2055 struct socks5_client_hello *c_hello;
2056 struct socks5_server_hello *s_hello;
2057 struct socks5_client_request *c_req;
2058 struct socks5_server_response *s_resp;
2064 struct hostent *phost;
2066 struct sockaddr_in remote_addr;
2067 struct in_addr *r_sin_addr;
2069 struct NetworkHandleList *nh;
2071 s5r->rtask = GNUNET_SCHEDULER_NO_TASK;
2073 if ((NULL != tc->write_ready) &&
2074 (GNUNET_NETWORK_fdset_isset (tc->read_ready, s5r->sock)) &&
2075 (s5r->rbuf_len = GNUNET_NETWORK_socket_recv (s5r->sock, s5r->rbuf,
2076 sizeof (s5r->rbuf))))
2078 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2079 "Successfully read %d bytes from socket\n",
2084 if (s5r->rbuf_len != 0)
2085 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "read");
2087 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client disco!\n");
2089 if (s5r->fwdrtask != GNUNET_SCHEDULER_NO_TASK)
2090 GNUNET_SCHEDULER_cancel (s5r->fwdrtask);
2091 if (s5r->wtask != GNUNET_SCHEDULER_NO_TASK)
2092 GNUNET_SCHEDULER_cancel (s5r->wtask);
2093 if (s5r->fwdwtask != GNUNET_SCHEDULER_NO_TASK)
2094 GNUNET_SCHEDULER_cancel (s5r->fwdwtask);
2095 GNUNET_NETWORK_socket_close (s5r->remote_sock);
2096 GNUNET_NETWORK_socket_close (s5r->sock);
2101 if (s5r->state == SOCKS5_INIT)
2103 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2105 c_hello = (struct socks5_client_hello*)&s5r->rbuf;
2107 GNUNET_assert (c_hello->version == SOCKS_VERSION_5);
2109 s_hello = (struct socks5_server_hello*)&s5r->wbuf;
2110 s5r->wbuf_len = sizeof( struct socks5_server_hello );
2112 s_hello->version = c_hello->version;
2113 s_hello->auth_method = SOCKS_AUTH_NONE;
2115 /* Write response to client */
2116 s5r->wtask = GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
2120 s5r->rtask = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
2124 s5r->state = SOCKS5_REQUEST;
2128 if (s5r->state == SOCKS5_REQUEST)
2130 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2131 "Processing SOCKS5 request\n");
2132 c_req = (struct socks5_client_request*)&s5r->rbuf;
2133 s_resp = (struct socks5_server_response*)&s5r->wbuf;
2134 //Only 10byte for ipv4 response!
2135 s5r->wbuf_len = 10;//sizeof (struct socks5_server_response);
2137 GNUNET_assert (c_req->addr_type == 3);
2139 dom_len = *((uint8_t*)(&(c_req->addr_type) + 1));
2140 memset(domain, 0, sizeof(domain));
2141 strncpy(domain, (char*)(&(c_req->addr_type) + 2), dom_len);
2142 req_port = *((uint16_t*)(&(c_req->addr_type) + 2 + dom_len));
2144 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2145 "Requested connection is %s:%d\n",
2149 if (is_tld (domain, GNUNET_GNS_TLD) ||
2150 is_tld (domain, GNUNET_GNS_TLD_ZKEY))
2152 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2153 "Requested connection is gnunet tld\n",
2157 if (ntohs(req_port) == HTTPS_PORT)
2159 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2160 "Requested connection is HTTPS\n");
2161 ret = add_handle_to_ssl_mhd ( s5r->sock, domain );
2163 else if (NULL != httpd)
2165 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2166 "Requested connection is HTTP\n");
2167 nh = GNUNET_malloc (sizeof (struct NetworkHandleList));
2170 GNUNET_CONTAINER_DLL_insert (mhd_httpd_head->socket_handles_head,
2171 mhd_httpd_head->socket_handles_tail,
2174 ret = add_handle_to_mhd ( s5r->sock, httpd );
2179 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2180 _("Failed to start HTTP server\n"));
2181 s_resp->version = 0x05;
2182 s_resp->reply = 0x01;
2183 s5r->cleanup = GNUNET_YES;
2184 s5r->cleanup_sock = GNUNET_YES;
2186 GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
2192 /* Signal success */
2193 s_resp->version = 0x05;
2194 s_resp->reply = 0x00;
2195 s_resp->reserved = 0x00;
2196 s_resp->addr_type = 0x01;
2198 s5r->cleanup = GNUNET_YES;
2199 s5r->cleanup_sock = GNUNET_NO;
2201 GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
2209 phost = (struct hostent*)gethostbyname (domain);
2212 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2213 "Resolve %s error!\n", domain );
2214 s_resp->version = 0x05;
2215 s_resp->reply = 0x01;
2216 s5r->cleanup = GNUNET_YES;
2217 s5r->cleanup_sock = GNUNET_YES;
2219 GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
2225 s5r->remote_sock = GNUNET_NETWORK_socket_create (AF_INET,
2228 r_sin_addr = (struct in_addr*)(phost->h_addr);
2229 remote_ip = r_sin_addr->s_addr;
2230 memset(&remote_addr, 0, sizeof(remote_addr));
2231 remote_addr.sin_family = AF_INET;
2232 #if HAVE_SOCKADDR_IN_SIN_LEN
2233 remote_addr.sin_len = sizeof (remote_addr);
2235 remote_addr.sin_addr.s_addr = remote_ip;
2236 remote_addr.sin_port = req_port;
2238 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2239 "target server: %s:%u\n", inet_ntoa(remote_addr.sin_addr),
2243 GNUNET_NETWORK_socket_connect ( s5r->remote_sock,
2244 (const struct sockaddr*)&remote_addr,
2245 sizeof (remote_addr)))
2246 && (errno != EINPROGRESS))
2248 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "connect");
2249 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2250 "socket request error...\n");
2251 s_resp->version = 0x05;
2252 s_resp->reply = 0x01;
2254 GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
2261 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2262 "new remote connection\n");
2264 s_resp->version = 0x05;
2265 s_resp->reply = 0x00;
2266 s_resp->reserved = 0x00;
2267 s_resp->addr_type = 0x01;
2269 s5r->state = SOCKS5_DATA_TRANSFER;
2272 GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
2276 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
2284 if (s5r->state == SOCKS5_DATA_TRANSFER)
2286 if ((s5r->remote_sock == NULL) || (s5r->rbuf_len == 0))
2288 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2289 "Closing connection to client\n");
2290 if (s5r->rtask != GNUNET_SCHEDULER_NO_TASK)
2291 GNUNET_SCHEDULER_cancel (s5r->rtask);
2292 if (s5r->fwdwtask != GNUNET_SCHEDULER_NO_TASK)
2293 GNUNET_SCHEDULER_cancel (s5r->fwdwtask);
2294 if (s5r->fwdrtask != GNUNET_SCHEDULER_NO_TASK)
2295 GNUNET_SCHEDULER_cancel (s5r->fwdrtask);
2296 if (s5r->fwdrtask != GNUNET_SCHEDULER_NO_TASK)
2297 GNUNET_SCHEDULER_cancel (s5r->fwdrtask);
2299 if (s5r->remote_sock != NULL)
2300 GNUNET_NETWORK_socket_close (s5r->remote_sock);
2301 GNUNET_NETWORK_socket_close (s5r->sock);
2306 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2307 "forwarding %d bytes from client\n", s5r->rbuf_len);
2310 GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
2312 &do_write_remote, s5r);
2314 if (s5r->fwdrtask == GNUNET_SCHEDULER_NO_TASK)
2317 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
2319 &do_read_remote, s5r);
2325 //GNUNET_CONTAINER_DLL_remove (s5conns.head, s5conns.tail, s5r);
2331 * Accept new incoming connections
2333 * @param cls the closure
2334 * @param tc the scheduler context
2337 do_accept (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2339 struct GNUNET_NETWORK_Handle *s;
2340 struct Socks5Request *s5r;
2342 ltask = GNUNET_SCHEDULER_NO_TASK;
2343 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
2346 ltask = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
2350 s = GNUNET_NETWORK_socket_accept (lsock, NULL, NULL);
2354 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "accept");
2358 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2359 "Got an inbound connection, waiting for data\n");
2361 s5r = GNUNET_malloc (sizeof (struct Socks5Request));
2363 s5r->state = SOCKS5_INIT;
2364 s5r->wtask = GNUNET_SCHEDULER_NO_TASK;
2365 s5r->fwdwtask = GNUNET_SCHEDULER_NO_TASK;
2366 s5r->fwdrtask = GNUNET_SCHEDULER_NO_TASK;
2367 s5r->rtask = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
2370 //GNUNET_CONTAINER_DLL_insert (s5conns.head, s5conns.tail, s5r);
2375 * Task run on shutdown
2377 * @param cls closure
2378 * @param tc task context
2381 do_shutdown (void *cls,
2382 const struct GNUNET_SCHEDULER_TaskContext *tc)
2385 struct MhdHttpList *hd;
2386 struct MhdHttpList *tmp_hd;
2387 struct NetworkHandleList *nh;
2388 struct NetworkHandleList *tmp_nh;
2389 struct ProxyCurlTask *ctask;
2390 struct ProxyCurlTask *ctask_tmp;
2392 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2393 "Shutting down...\n");
2395 gnutls_global_deinit ();
2397 if (GNUNET_SCHEDULER_NO_TASK != curl_download_task)
2399 GNUNET_SCHEDULER_cancel (curl_download_task);
2400 curl_download_task = GNUNET_SCHEDULER_NO_TASK;
2403 for (hd = mhd_httpd_head; hd != NULL; hd = tmp_hd)
2407 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2408 "Stopping daemon\n");
2410 if (GNUNET_SCHEDULER_NO_TASK != hd->httpd_task)
2412 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2413 "Stopping select task %d\n",
2415 GNUNET_SCHEDULER_cancel (hd->httpd_task);
2416 hd->httpd_task = GNUNET_SCHEDULER_NO_TASK;
2419 if (NULL != hd->daemon)
2421 MHD_stop_daemon (hd->daemon);
2425 for (nh = hd->socket_handles_head; nh != NULL; nh = tmp_nh)
2429 GNUNET_NETWORK_socket_close (nh->h);
2434 if (NULL != hd->proxy_cert)
2436 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2437 "Free certificate\n");
2438 GNUNET_free (hd->proxy_cert);
2444 for (ctask=ctasks_head; ctask != NULL; ctask=ctask_tmp)
2446 ctask_tmp = ctask->next;
2448 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2449 "Cleaning up cURL task\n");
2451 if (ctask->curl != NULL)
2452 curl_easy_cleanup (ctask->curl);
2454 if (NULL != ctask->headers)
2455 curl_slist_free_all (ctask->headers);
2456 if (NULL != ctask->resolver)
2457 curl_slist_free_all (ctask->resolver);
2459 if (NULL != ctask->response)
2460 MHD_destroy_response (ctask->response);
2463 GNUNET_free (ctask);
2466 GNUNET_GNS_disconnect (gns_handle);
2471 * Compiles a regex for us
2473 * @param re ptr to re struct
2474 * @param rt the expression to compile
2475 * @return 0 on success
2478 compile_regex (regex_t *re, const char* rt)
2483 status = regcomp (re, rt, REG_EXTENDED|REG_NEWLINE);
2486 regerror (status, re, err, 1024);
2487 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2488 "Regex error compiling '%s': %s\n", rt, err);
2496 * Loads the users local zone key
2498 * @return GNUNET_YES on success
2501 load_local_zone_key (const struct GNUNET_CONFIGURATION_Handle *cfg)
2504 struct GNUNET_CRYPTO_RsaPrivateKey *key = NULL;
2505 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pkey;
2506 struct GNUNET_CRYPTO_ShortHashCode *zone = NULL;
2507 struct GNUNET_CRYPTO_ShortHashAsciiEncoded zonename;
2509 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "gns",
2510 "ZONEKEY", &keyfile))
2512 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2513 "Unable to load zone key config value!\n");
2517 if (GNUNET_NO == GNUNET_DISK_file_test (keyfile))
2519 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2520 "Unable to load zone key %s!\n", keyfile);
2521 GNUNET_free(keyfile);
2525 key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
2526 GNUNET_CRYPTO_rsa_key_get_public (key, &pkey);
2527 GNUNET_CRYPTO_short_hash(&pkey,
2528 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
2530 zone = &local_gns_zone;
2531 GNUNET_CRYPTO_short_hash_to_enc (zone, &zonename);
2532 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2533 "Using zone: %s!\n", &zonename);
2534 GNUNET_CRYPTO_rsa_key_free(key);
2535 GNUNET_free(keyfile);
2537 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "gns",
2538 "PRIVATE_ZONEKEY", &keyfile))
2540 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2541 "Unable to load private zone key config value!\n");
2545 if (GNUNET_NO == GNUNET_DISK_file_test (keyfile))
2547 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2548 "Unable to load private zone key %s!\n", keyfile);
2549 GNUNET_free(keyfile);
2553 key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
2554 GNUNET_CRYPTO_rsa_key_get_public (key, &pkey);
2555 GNUNET_CRYPTO_short_hash(&pkey,
2556 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
2557 &local_private_zone);
2558 GNUNET_CRYPTO_short_hash_to_enc (zone, &zonename);
2559 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2560 "Using private zone: %s!\n", &zonename);
2561 GNUNET_CRYPTO_rsa_key_free(key);
2562 GNUNET_free(keyfile);
2564 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "gns",
2565 "SHORTEN_ZONEKEY", &keyfile))
2567 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2568 "Unable to load shorten zone key config value!\n");
2572 if (GNUNET_NO == GNUNET_DISK_file_test (keyfile))
2574 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2575 "Unable to load shorten zone key %s!\n", keyfile);
2576 GNUNET_free(keyfile);
2580 key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
2581 GNUNET_CRYPTO_rsa_key_get_public (key, &pkey);
2582 GNUNET_CRYPTO_short_hash(&pkey,
2583 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
2584 &local_shorten_zone);
2585 GNUNET_CRYPTO_short_hash_to_enc (zone, &zonename);
2586 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2587 "Using shorten zone: %s!\n", &zonename);
2588 GNUNET_CRYPTO_rsa_key_free(key);
2589 GNUNET_free(keyfile);
2595 * Main function that will be run
2597 * @param cls closure
2598 * @param args remaining command-line arguments
2599 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
2600 * @param cfg configuration
2603 run (void *cls, char *const *args, const char *cfgfile,
2604 const struct GNUNET_CONFIGURATION_Handle *cfg)
2606 struct sockaddr_in sa;
2607 struct MhdHttpList *hd;
2608 struct sockaddr_un mhd_unix_sock_addr;
2610 char* proxy_sockfile;
2611 char* cafile_cfg = NULL;
2616 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2619 cafile = cafile_opt;
2623 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "gns-proxy",
2627 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2628 "Unable to load proxy CA config value!\n");
2629 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2630 "No proxy CA provided!\n");
2633 cafile = cafile_cfg;
2636 gnutls_global_init ();
2638 gnutls_x509_crt_init (&proxy_ca.cert);
2639 gnutls_x509_privkey_init (&proxy_ca.key);
2641 load_cert_from_file (proxy_ca.cert, cafile);
2642 load_key_from_file (proxy_ca.key, cafile);
2644 GNUNET_free_non_null (cafile_cfg);
2646 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2647 "Loading Template\n");
2649 compile_regex (&re_dotplus, (char*) RE_A_HREF);
2651 gns_handle = GNUNET_GNS_connect (cfg);
2653 if (GNUNET_NO == load_local_zone_key (cfg))
2655 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2656 "Unable to load zone!\n");
2660 if (NULL == gns_handle)
2662 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2663 "Unable to connect to GNS!\n");
2667 memset (&sa, 0, sizeof (sa));
2668 sa.sin_family = AF_INET;
2669 sa.sin_port = htons (port);
2670 #if HAVE_SOCKADDR_IN_SIN_LEN
2671 sa.sin_len = sizeof (sa);
2674 lsock = GNUNET_NETWORK_socket_create (AF_INET,
2678 if ((NULL == lsock) ||
2680 GNUNET_NETWORK_socket_bind (lsock, (const struct sockaddr *) &sa,
2683 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2684 "Failed to create listen socket bound to `%s'",
2685 GNUNET_a2s ((const struct sockaddr *) &sa, sizeof (sa)));
2687 GNUNET_NETWORK_socket_close (lsock);
2691 if (GNUNET_OK != GNUNET_NETWORK_socket_listen (lsock, 5))
2693 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2694 "Failed to listen on socket bound to `%s'",
2695 GNUNET_a2s ((const struct sockaddr *) &sa, sizeof (sa)));
2699 ltask = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
2700 lsock, &do_accept, NULL);
2705 if (0 != curl_global_init (CURL_GLOBAL_WIN32))
2707 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2708 "cURL global init failed!\n");
2712 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2713 "Proxy listens on port %u\n",
2716 mhd_httpd_head = NULL;
2717 mhd_httpd_tail = NULL;
2718 total_mhd_connections = 0;
2720 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "gns-proxy",
2724 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2725 "Specify PROXY_UNIXPATH in gns-proxy config section!\n");
2729 mhd_unix_socket = GNUNET_NETWORK_socket_create (AF_UNIX,
2733 if (NULL == mhd_unix_socket)
2735 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2736 "Unable to create unix domain socket!\n");
2740 mhd_unix_sock_addr.sun_family = AF_UNIX;
2741 strcpy (mhd_unix_sock_addr.sun_path, proxy_sockfile);
2744 mhd_unix_sock_addr.sun_path[0] = '\0';
2746 #if HAVE_SOCKADDR_IN_SIN_LEN
2747 mhd_unix_sock_addr.sun_len = (u_char) sizeof (struct sockaddr_un);
2750 len = strlen (proxy_sockfile) + sizeof(AF_UNIX);
2752 GNUNET_free (proxy_sockfile);
2754 if (GNUNET_OK != GNUNET_NETWORK_socket_bind (mhd_unix_socket,
2755 (struct sockaddr*)&mhd_unix_sock_addr,
2758 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2759 "Unable to bind unix domain socket!\n");
2763 if (GNUNET_OK != GNUNET_NETWORK_socket_listen (mhd_unix_socket,
2766 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2767 "Unable to listen on unix domain socket!\n");
2771 hd = GNUNET_malloc (sizeof (struct MhdHttpList));
2772 hd->is_ssl = GNUNET_NO;
2773 strcpy (hd->domain, "");
2774 httpd = MHD_start_daemon (MHD_USE_DEBUG, 4444, //Dummy port
2776 &create_response, hd,
2777 MHD_OPTION_LISTEN_SOCKET, GNUNET_NETWORK_get_fd (mhd_unix_socket),
2778 MHD_OPTION_CONNECTION_LIMIT, MHD_MAX_CONNECTIONS,
2779 MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) 16,
2780 MHD_OPTION_NOTIFY_COMPLETED,
2784 GNUNET_assert (httpd != NULL);
2786 hd->httpd_task = GNUNET_SCHEDULER_NO_TASK;
2788 GNUNET_CONTAINER_DLL_insert (mhd_httpd_head, mhd_httpd_tail, hd);
2792 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
2793 &do_shutdown, NULL);
2799 * The main function for gnunet-gns-proxy.
2801 * @param argc number of arguments from the command line
2802 * @param argv command line arguments
2803 * @return 0 ok, 1 on error
2806 main (int argc, char *const *argv)
2808 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
2810 gettext_noop ("listen on specified port"), 1,
2811 &GNUNET_GETOPT_set_string, &port},
2812 {'a', "authority", NULL,
2813 gettext_noop ("pem file to use as CA"), 1,
2814 &GNUNET_GETOPT_set_string, &cafile_opt},
2815 GNUNET_GETOPT_OPTION_END
2820 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
2824 GNUNET_log_setup ("gnunet-gns-proxy", "WARNING", NULL);
2827 GNUNET_PROGRAM_run (argc, argv, "gnunet-gns-proxy",
2828 _("GNUnet GNS proxy"),
2830 &run, NULL)) ? 0 : 1;
2831 GNUNET_free_non_null ((char*)argv);