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;
250 /* The port the proxy is running on (default 7777) */
251 static unsigned long port = GNUNET_GNS_PROXY_PORT;
253 /* The CA file (pem) to use for the proxy CA */
254 static char* cafile_opt;
256 /* The listen socket of the proxy */
257 static struct GNUNET_NETWORK_Handle *lsock;
259 /* The listen task ID */
260 GNUNET_SCHEDULER_TaskIdentifier ltask;
262 /* The cURL download task */
263 GNUNET_SCHEDULER_TaskIdentifier curl_download_task;
265 /* The non SSL httpd daemon handle */
266 static struct MHD_Daemon *httpd;
268 /* Number of current mhd connections */
269 static unsigned int total_mhd_connections;
271 /* The cURL multi handle */
272 static CURLM *curl_multi;
274 /* Handle to the GNS service */
275 static struct GNUNET_GNS_Handle *gns_handle;
277 /* DLL for ProxyCurlTasks */
278 static struct ProxyCurlTask *ctasks_head;
280 /* DLL for ProxyCurlTasks */
281 static struct ProxyCurlTask *ctasks_tail;
283 /* DLL for http daemons */
284 static struct MhdHttpList *mhd_httpd_head;
286 /* DLL for http daemons */
287 static struct MhdHttpList *mhd_httpd_tail;
289 /* Handle to the regex for dotplus (.+) replacement in HTML */
290 static regex_t re_dotplus;
292 /* The users local GNS zone hash */
293 static struct GNUNET_CRYPTO_ShortHashCode local_gns_zone;
295 /* The users local private zone */
296 static struct GNUNET_CRYPTO_ShortHashCode local_private_zone;
298 /* The users local shorten zone */
299 static struct GNUNET_CRYPTO_ShortHashCode local_shorten_zone;
301 /* The CA for SSL certificate generation */
302 static struct ProxyCA proxy_ca;
304 /* UNIX domain socket for mhd */
305 struct GNUNET_NETWORK_Handle *mhd_unix_socket;
307 /* Shorten zone private key */
308 struct GNUNET_CRYPTO_RsaPrivateKey *shorten_zonekey;
311 * Checks if name is in tld
313 * @param name the name to check
314 * @param tld the TLD to check for
315 * @return GNUNET_YES or GNUNET_NO
318 is_tld(const char* name, const char* tld)
322 if (strlen(name) <= strlen(tld))
327 offset = strlen(name)-strlen(tld);
328 if (strcmp (name+offset, tld) != 0)
330 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
331 "%s is not in .%s TLD\n", name, tld);
340 * Read HTTP request header field 'Host'
342 * @param cls buffer to write to
343 * @param kind value kind
344 * @param key field key
345 * @param value field value
346 * @return MHD_NO when Host found
349 con_val_iter (void *cls,
350 enum MHD_ValueKind kind,
354 char* buf = (char*)cls;
356 if (0 == strcmp ("Host", key))
366 * Check HTTP response header for mime
368 * @param buffer curl buffer
369 * @param size curl blocksize
370 * @param nmemb curl blocknumber
372 * @return size of read bytes
375 curl_check_hdr (void *buffer, size_t size, size_t nmemb, void *cls)
377 size_t bytes = size * nmemb;
378 struct ProxyCurlTask *ctask = cls;
379 int len = strlen (HTML_HDR_CONTENT);
382 if ( (len+1) > bytes)
385 memcpy (hdr, buffer, len);
388 if (0 == strcmp (hdr, HTML_HDR_CONTENT))
390 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
391 "Got HTML HTTP response header\n");
392 ctask->parse_content = GNUNET_YES;
401 * @param hd a http daemon list entry
404 run_httpd (struct MhdHttpList *hd);
416 * Task that simply runs MHD main loop
419 * @param tc task context
422 run_mhd (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
425 struct MhdHttpList *hd = cls;
427 //for (hd=mhd_httpd_head; hd != NULL; hd = hd->next)
428 MHD_run (hd->daemon);
433 * Process cURL download bits
435 * @param ptr buffer with data
436 * @param size size of a record
437 * @param nmemb number of records downloaded
439 * @return number of processed bytes
442 callback_download (void *ptr, size_t size, size_t nmemb, void *ctx)
444 const char *cbuf = ptr;
446 struct ProxyCurlTask *ctask = ctx;
457 if (total > sizeof (ctask->buffer))
459 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
460 "CURL gave us too much data to handle (%d)!\n",
465 if (ctask->buf_status == BUF_WAIT_FOR_MHD)
467 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
468 "CURL: Waiting for MHD (%s)\n", ctask->url);
469 return CURL_WRITEFUNC_PAUSE;
473 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
474 "CURL: Copying to MHD (%s, %d)\n", ctask->url, total);
475 memcpy (ctask->buffer, cbuf, total);
476 ctask->bytes_in_buffer = total;
477 ctask->buffer_ptr = ctask->buffer;
479 ctask->buf_status = BUF_WAIT_FOR_MHD;
481 //GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
482 // "cURL chunk:\n%s\n", (char*)ctask->buffer);
483 //run_mhd (NULL, NULL);
484 GNUNET_SCHEDULER_add_now (&run_mhd, ctask->mhd);
489 * Ask cURL for the select sets and schedule download
492 curl_download_prepare ();
495 * Callback to free content
497 * @param cls content to free
498 * @param tc task context
501 mhd_content_free (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
503 struct ProxyCurlTask *ctask = cls;
505 if (NULL != ctask->headers)
506 curl_slist_free_all (ctask->headers);
508 if (NULL != ctask->headers)
509 curl_slist_free_all (ctask->resolver);
511 if (NULL != ctask->response)
512 MHD_destroy_response (ctask->response);
520 * Shorten result callback
522 * @param cls the proxycurltask
523 * @param short_name the shortened name (NULL on error)
526 process_shorten (void* cls, const char* short_name)
528 struct ProxyCurlTask *ctask = cls;
530 char tmp[strlen(ctask->pp_buf)]; //TODO length
532 if (NULL == short_name)
534 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
535 "MHD PP: Unable to shorten %s\n",
540 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
541 "MHD PP: Shorten %s -> %s\n",
545 sprintf (tmp, "<a href=\"http://%s", short_name);
546 strcpy (ctask->pp_buf, tmp);
548 ctask->pp_finished = GNUNET_YES;
550 GNUNET_SCHEDULER_add_now (&run_mhd, ctask->mhd);
555 * Callback for MHD response
558 * @param pos in buffer
560 * @param max space in buffer
561 * @return number of bytes written
564 mhd_content_cb (void *cls,
569 struct ProxyCurlTask *ctask = cls;
571 size_t bytes_to_copy;
574 regmatch_t m[RE_N_MATCHES];
576 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
577 "MHD: content cb %s\n", ctask->url);
579 if (ctask->download_successful &&
580 (ctask->buf_status == BUF_WAIT_FOR_CURL))
582 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
583 "MHD: sending response for %s\n", ctask->url);
584 ctask->download_in_progress = GNUNET_NO;
585 GNUNET_SCHEDULER_add_now (&mhd_content_free, ctask);
586 GNUNET_SCHEDULER_add_now (&run_mhd, ctask->mhd);
587 total_mhd_connections--;
588 return MHD_CONTENT_READER_END_OF_STREAM;
591 if (ctask->download_error &&
592 (ctask->buf_status == BUF_WAIT_FOR_CURL))
594 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
595 "MHD: sending error response\n");
596 ctask->download_in_progress = GNUNET_NO;
597 GNUNET_SCHEDULER_add_now (&mhd_content_free, ctask);
598 GNUNET_SCHEDULER_add_now (&run_mhd, ctask->mhd);
599 total_mhd_connections--;
600 return MHD_CONTENT_READER_END_WITH_ERROR;
603 if ( ctask->buf_status == BUF_WAIT_FOR_CURL )
606 bytes_to_copy = ctask->bytes_in_buffer;
608 if (ctask->parse_content == GNUNET_YES)
611 GNUNET_log ( GNUNET_ERROR_TYPE_DEBUG,
612 "MHD: We need to parse the HTML %s\n", ctask->buffer_ptr);
614 nomatch = regexec ( &re_dotplus, ctask->buffer_ptr, RE_N_MATCHES, m, 0);
618 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
619 "MHD RE: No match\n");
623 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
626 GNUNET_assert (m[1].rm_so != -1);
628 hostptr = ctask->buffer_ptr+m[1].rm_so;
632 bytes_to_copy = m[0].rm_so;
633 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
634 "Copying %d bytes.\n", m[0].rm_so);
640 if (ctask->is_postprocessing == GNUNET_YES)
644 if ( ctask->pp_finished == GNUNET_NO )
646 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
647 "MHD PP: Waiting for PP of %s\n", ctask->pp_buf);
651 ctask->is_postprocessing = GNUNET_NO;
653 ctask->bytes_in_buffer -= m[0].rm_eo;//(m[1].rm_eo-m[1].rm_so);
654 ctask->buffer_ptr += m[0].rm_eo;//(m[1].rm_eo-m[1].rm_so);
655 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
656 "Skipping next %d bytes in buffer\n", m[0].rm_eo);
658 GNUNET_SCHEDULER_add_now (&run_mhd, ctask->mhd);
660 if ( strlen (ctask->pp_buf) <= max )
662 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
663 "Copying postprocessed %s.\n", ctask->pp_buf);
664 memcpy ( buf, ctask->pp_buf, strlen (ctask->pp_buf) );
665 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
667 ctask->is_postprocessing = GNUNET_NO;
668 return strlen (ctask->pp_buf);
674 memset (ctask->pp_buf, 0, sizeof(ctask->pp_buf));
676 /* If .+ extend with authority */
677 if (*(ctask->buffer_ptr+m[1].rm_eo) == '+')
679 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
681 memcpy (ctask->pp_buf, hostptr, (m[1].rm_eo-m[1].rm_so));
682 strcpy ( ctask->pp_buf+strlen(ctask->pp_buf),
685 /* If .zkey simply copy the name */
688 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
690 memcpy (ctask->pp_buf, hostptr, (m[1].rm_eo-m[1].rm_so + strlen (GNUNET_GNS_TLD_ZKEY)));
693 ctask->is_postprocessing = GNUNET_YES;
694 ctask->pp_finished = GNUNET_NO;
696 GNUNET_GNS_shorten_zone (gns_handle,
709 if ( bytes_to_copy > max )
711 GNUNET_log ( GNUNET_ERROR_TYPE_DEBUG,
712 "MHD: buffer in response too small! (%s)\n",
714 memcpy ( buf, ctask->buffer_ptr, max);
715 ctask->bytes_in_buffer -= max;
716 ctask->buffer_ptr += max;
721 GNUNET_log ( GNUNET_ERROR_TYPE_DEBUG,
722 "MHD: copying %d bytes to mhd response at offset %d\n",
725 memcpy ( buf, ctask->buffer_ptr, bytes_to_copy );
726 copied = bytes_to_copy;
727 if (bytes_to_copy < ctask->bytes_in_buffer)
729 ctask->bytes_in_buffer -= bytes_to_copy;
730 ctask->buffer_ptr += bytes_to_copy;
734 ctask->bytes_in_buffer = 0;
735 ctask->buf_status = BUF_WAIT_FOR_CURL;
736 ctask->buffer_ptr = ctask->buffer;
737 if (NULL != ctask->curl)
738 curl_easy_pause (ctask->curl, CURLPAUSE_CONT);
739 GNUNET_SCHEDULER_add_now (&run_mhd, ctask->mhd);
743 GNUNET_SCHEDULER_add_now (&run_mhd, ctask->mhd);
751 * Task that is run when we are ready to receive more data
755 * @param tc task context
758 curl_task_download (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
761 * Ask cURL for the select sets and schedule download
764 curl_download_prepare ()
771 struct GNUNET_NETWORK_FDSet *grs;
772 struct GNUNET_NETWORK_FDSet *gws;
774 struct GNUNET_TIME_Relative rtime;
780 mret = curl_multi_fdset (curl_multi, &rs, &ws, &es, &max);
782 if (mret != CURLM_OK)
784 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
785 "%s failed at %s:%d: `%s'\n",
786 "curl_multi_fdset", __FILE__, __LINE__,
787 curl_multi_strerror (mret));
792 mret = curl_multi_timeout (curl_multi, &to);
793 rtime = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, to);
795 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
796 "cURL multi fds: max=%d timeout=%llu\n", max, to);
798 grs = GNUNET_NETWORK_fdset_create ();
799 gws = GNUNET_NETWORK_fdset_create ();
800 GNUNET_NETWORK_fdset_copy_native (grs, &rs, max + 1);
801 GNUNET_NETWORK_fdset_copy_native (gws, &ws, max + 1);
802 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
803 "Scheduling task cURL\n");
805 if (curl_download_task != GNUNET_SCHEDULER_NO_TASK)
806 GNUNET_SCHEDULER_cancel (curl_download_task);
809 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
812 &curl_task_download, curl_multi);
813 GNUNET_NETWORK_fdset_destroy (gws);
814 GNUNET_NETWORK_fdset_destroy (grs);
820 * Task that is run when we are ready to receive more data
824 * @param tc task context
827 curl_task_download (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
833 struct ProxyCurlTask *ctask;
836 struct ProxyCurlTask *clean_head = NULL;
837 struct ProxyCurlTask *clean_tail = NULL;
839 curl_download_task = GNUNET_SCHEDULER_NO_TASK;
841 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
843 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
844 "Shutdown requested while trying to download\n");
848 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
856 mret = curl_multi_perform (curl_multi, &running);
858 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
859 "Running curl tasks: %d\n", running);
862 for (; ctask != NULL; ctask = ctask->next)
864 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
865 "CTask: %s\n", ctask->url);
869 if (num_ctasks != running)
871 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
872 "%d ctasks, %d curl running\n", num_ctasks, running);
878 msg = curl_multi_info_read (curl_multi, &msgnum);
879 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
880 "Messages left: %d\n", msgnum);
887 if ((msg->data.result != CURLE_OK) &&
888 (msg->data.result != CURLE_GOT_NOTHING))
890 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
891 "Download curl failed");
893 for (; ctask != NULL; ctask = ctask->next)
895 if (NULL == ctask->curl)
898 if (memcmp (msg->easy_handle, ctask->curl, sizeof (CURL)) != 0)
901 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
902 "Download curl failed for task %s: %s.\n",
904 curl_easy_strerror (msg->data.result));
905 ctask->download_successful = GNUNET_NO;
906 ctask->download_error = GNUNET_YES;
907 //curl_multi_remove_handle (curl_multi, ctask->curl);
908 //curl_easy_cleanup (ctask->curl);
909 //ctask->curl = NULL;
910 GNUNET_CONTAINER_DLL_remove (ctasks_head, ctasks_tail,
912 GNUNET_CONTAINER_DLL_insert (clean_head, clean_tail, ctask);
915 GNUNET_assert (ctask != NULL);
919 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
920 "cURL download completed.\n");
922 for (; ctask != NULL; ctask = ctask->next)
924 if (NULL == ctask->curl)
927 if (memcmp (msg->easy_handle, ctask->curl, sizeof (CURL)) != 0)
930 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
931 "cURL task %s found.\n", ctask->url);
932 ctask->download_successful = GNUNET_YES;
933 //curl_multi_remove_handle (curl_multi, ctask->curl);
934 //curl_easy_cleanup (ctask->curl);
935 //ctask->curl = NULL;
936 GNUNET_CONTAINER_DLL_remove (ctasks_head, ctasks_tail,
938 GNUNET_CONTAINER_DLL_insert (clean_head, clean_tail, ctask);
942 GNUNET_assert (ctask != NULL);
944 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
945 "curl end %s\n", curl_easy_strerror(msg->data.result));
951 } while (msgnum > 0);
953 for (ctask=clean_head; ctask != NULL; ctask = ctask->next)
955 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
956 "Removing cURL task %s.\n", ctask->url);
957 curl_multi_remove_handle (curl_multi, ctask->curl);
958 curl_easy_cleanup (ctask->curl);
963 for (ctask=ctasks_head; ctask != NULL; ctask = ctask->next)
968 if (num_ctasks != running)
970 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
971 "%d ctasks, %d curl running\n", num_ctasks, running);
974 GNUNET_assert ( num_ctasks == running );
978 } while (mret == CURLM_CALL_MULTI_PERFORM);
981 if (mret != CURLM_OK)
983 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "%s failed at %s:%d: `%s'\n",
984 "curl_multi_perform", __FILE__, __LINE__,
985 curl_multi_strerror (mret));
987 curl_download_prepare();
991 * Process LEHO lookup
993 * @param cls the ctask
994 * @param rd_count number of records returned
995 * @param rd record data
998 process_leho_lookup (void *cls,
1000 const struct GNUNET_NAMESTORE_RecordData *rd)
1002 struct ProxyCurlTask *ctask = cls;
1003 char hosthdr[262]; //256 + "Host: "
1007 struct hostent *phost;
1009 char resolvename[512];
1012 ctask->headers = NULL;
1014 strcpy (ctask->leho, "");
1017 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1018 "No LEHO present!\n");
1020 for (i=0; i<rd_count; i++)
1022 if (rd[i].record_type != GNUNET_GNS_RECORD_LEHO)
1025 memcpy (ctask->leho, rd[i].data, rd[i].data_size);
1027 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1028 "Found LEHO %s for %s\n", ctask->leho, ctask->url);
1031 if (0 != strcmp (ctask->leho, ""))
1033 sprintf (hosthdr, "%s%s", "Host: ", ctask->leho);
1034 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1035 "New HTTP header value: %s\n", hosthdr);
1036 ctask->headers = curl_slist_append (ctask->headers, hosthdr);
1037 GNUNET_assert (NULL != ctask->headers);
1038 ret = curl_easy_setopt (ctask->curl, CURLOPT_HTTPHEADER, ctask->headers);
1039 if (CURLE_OK != ret)
1041 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "%s failed at %s:%d: `%s'\n",
1042 "curl_easy_setopt", __FILE__, __LINE__, curl_easy_strerror(ret));
1047 if (ctask->mhd->is_ssl)
1049 phost = (struct hostent*)gethostbyname (ctask->host);
1053 ssl_ip = inet_ntoa(*((struct in_addr*)(phost->h_addr)));
1054 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1055 "SSL target server: %s\n", ssl_ip);
1056 sprintf (resolvename, "%s:%d:%s", ctask->leho, HTTPS_PORT, ssl_ip);
1057 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1058 "Curl resolve: %s\n", resolvename);
1059 ctask->resolver = curl_slist_append ( ctask->resolver, resolvename);
1060 curl_easy_setopt (ctask->curl, CURLOPT_RESOLVE, ctask->resolver);
1061 sprintf (curlurl, "https://%s%s", ctask->leho, ctask->url);
1062 curl_easy_setopt (ctask->curl, CURLOPT_URL, curlurl);
1066 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1067 "gethostbyname failed for %s!\n", ctask->host);
1068 ctask->download_successful = GNUNET_NO;
1069 ctask->download_error = GNUNET_YES;
1074 if (CURLM_OK != (mret=curl_multi_add_handle (curl_multi, ctask->curl)))
1076 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1077 "%s failed at %s:%d: `%s'\n",
1078 "curl_multi_add_handle", __FILE__, __LINE__,
1079 curl_multi_strerror (mret));
1080 ctask->download_successful = GNUNET_NO;
1081 ctask->download_error = GNUNET_YES;
1084 GNUNET_CONTAINER_DLL_insert (ctasks_head, ctasks_tail, ctask);
1086 curl_download_prepare ();
1091 * Initialize download and trigger curl
1093 * @param cls the proxycurltask
1094 * @param auth_name the name of the authority (site of origin) of ctask->host
1098 process_get_authority (void *cls,
1099 const char* auth_name)
1101 struct ProxyCurlTask *ctask = cls;
1103 if (NULL == auth_name)
1105 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1106 "Get authority failed!\n");
1107 strcpy (ctask->authority, "");
1111 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1112 "Get authority yielded %s\n", auth_name);
1113 strcpy (ctask->authority, auth_name);
1116 GNUNET_GNS_lookup_zone (gns_handle,
1119 GNUNET_GNS_RECORD_LEHO,
1120 GNUNET_YES, //Only cached for performance
1122 &process_leho_lookup,
1127 * Main MHD callback for handling requests.
1130 * @param con MHD connection handle
1131 * @param url the url in the request
1132 * @param meth the HTTP method used ("GET", "PUT", etc.)
1133 * @param ver the HTTP version string (i.e. "HTTP/1.1")
1134 * @param upload_data the data being uploaded (excluding HEADERS,
1135 * for a POST that fits into memory and that is encoded
1136 * with a supported encoding, the POST data will NOT be
1137 * given in upload_data and is instead available as
1138 * part of MHD_get_connection_values; very large POST
1139 * data *will* be made available incrementally in
1141 * @param upload_data_size set initially to the size of the
1142 * upload_data provided; the method must update this
1143 * value to the number of bytes NOT processed;
1144 * @param con_cls pointer to location where we store the 'struct Request'
1145 * @return MHD_YES if the connection was handled successfully,
1146 * MHD_NO if the socket must be closed due to a serious
1147 * error while handling the request
1150 create_response (void *cls,
1151 struct MHD_Connection *con,
1155 const char *upload_data,
1156 size_t *upload_data_size,
1160 struct MhdHttpList* hd = cls;
1161 const char* page = "<html><head><title>gnoxy</title>"\
1162 "</head><body>cURL fail</body></html>";
1168 struct ProxyCurlTask *ctask;
1170 if (0 != strcmp (meth, "GET"))
1172 if (&dummy != *con_cls)
1178 if (0 != *upload_data_size)
1183 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1186 MHD_get_connection_values (con,
1188 &con_val_iter, host);
1192 ctask = GNUNET_malloc (sizeof (struct ProxyCurlTask));
1195 if (curl_multi == NULL)
1196 curl_multi = curl_multi_init ();
1198 ctask->curl = curl_easy_init();
1200 if ((ctask->curl == NULL) || (curl_multi == NULL))
1202 ctask->response = MHD_create_response_from_buffer (strlen (page),
1204 MHD_RESPMEM_PERSISTENT);
1205 ret = MHD_queue_response (con,
1208 MHD_destroy_response (ctask->response);
1209 GNUNET_free (ctask);
1215 ctask->headers = NULL;
1216 ctask->resolver = NULL;
1217 ctask->buffer_ptr = NULL;
1218 ctask->download_in_progress = GNUNET_YES;
1219 ctask->download_successful = GNUNET_NO;
1220 ctask->buf_status = BUF_WAIT_FOR_CURL;
1221 ctask->bytes_in_buffer = 0;
1222 ctask->parse_content = GNUNET_NO;
1224 curl_easy_setopt (ctask->curl, CURLOPT_HEADERFUNCTION, &curl_check_hdr);
1225 curl_easy_setopt (ctask->curl, CURLOPT_HEADERDATA, ctask);
1226 curl_easy_setopt (ctask->curl, CURLOPT_WRITEFUNCTION, &callback_download);
1227 curl_easy_setopt (ctask->curl, CURLOPT_WRITEDATA, ctask);
1228 curl_easy_setopt (ctask->curl, CURLOPT_FOLLOWLOCATION, 1);
1229 curl_easy_setopt (ctask->curl, CURLOPT_MAXREDIRS, 4);
1230 /* no need to abort if the above failed */
1231 if (GNUNET_NO == ctask->mhd->is_ssl)
1233 sprintf (curlurl, "http://%s%s", host, url);
1234 curl_easy_setopt (ctask->curl, CURLOPT_URL, curlurl);
1235 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1236 "Adding new curl task for %s\n", curlurl);
1238 strcpy (ctask->host, host);
1239 strcpy (ctask->url, url);
1241 //curl_easy_setopt (ctask->curl, CURLOPT_URL, curlurl);
1242 curl_easy_setopt (ctask->curl, CURLOPT_FAILONERROR, 1);
1243 curl_easy_setopt (ctask->curl, CURLOPT_CONNECTTIMEOUT, 600L);
1244 curl_easy_setopt (ctask->curl, CURLOPT_TIMEOUT, 600L);
1246 GNUNET_GNS_get_authority (gns_handle,
1248 &process_get_authority,
1250 //download_prepare (ctask);
1251 //curl_download_prepare ();
1253 ctask->response = MHD_create_response_from_callback (MHD_SIZE_UNKNOWN,
1259 ret = MHD_queue_response (con, MHD_HTTP_OK, ctask->response);
1261 //MHD_destroy_response (response);
1267 * Task run whenever HTTP server operations are pending.
1270 * @param tc sched context
1273 do_httpd (void *cls,
1274 const struct GNUNET_SCHEDULER_TaskContext *tc);
1283 struct MhdHttpList *hd;
1285 for (hd=mhd_httpd_head; hd != NULL; hd = hd->next)
1293 * @param hd the daemon to run
1296 run_httpd (struct MhdHttpList *hd)
1301 struct GNUNET_NETWORK_FDSet *wrs;
1302 struct GNUNET_NETWORK_FDSet *wws;
1303 struct GNUNET_NETWORK_FDSet *wes;
1306 unsigned MHD_LONG_LONG timeout;
1307 struct GNUNET_TIME_Relative tv;
1312 wrs = GNUNET_NETWORK_fdset_create ();
1313 wes = GNUNET_NETWORK_fdset_create ();
1314 wws = GNUNET_NETWORK_fdset_create ();
1316 GNUNET_assert (MHD_YES == MHD_get_fdset (hd->daemon, &rs, &ws, &es, &max));
1319 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1320 "MHD fds: max=%d\n", max);
1322 haveto = MHD_get_timeout (hd->daemon, &timeout);
1324 if (haveto == MHD_YES)
1325 tv.rel_value = (uint64_t) timeout;
1327 tv = GNUNET_TIME_UNIT_FOREVER_REL;
1328 GNUNET_NETWORK_fdset_copy_native (wrs, &rs, max + 1);
1329 GNUNET_NETWORK_fdset_copy_native (wws, &ws, max + 1);
1330 GNUNET_NETWORK_fdset_copy_native (wes, &es, max + 1);
1332 if (hd->httpd_task != GNUNET_SCHEDULER_NO_TASK)
1333 GNUNET_SCHEDULER_cancel (hd->httpd_task);
1335 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_HIGH,
1338 GNUNET_NETWORK_fdset_destroy (wrs);
1339 GNUNET_NETWORK_fdset_destroy (wws);
1340 GNUNET_NETWORK_fdset_destroy (wes);
1345 * Task run whenever HTTP server operations are pending.
1348 * @param tc sched context
1351 do_httpd (void *cls,
1352 const struct GNUNET_SCHEDULER_TaskContext *tc)
1354 struct MhdHttpList *hd = cls;
1356 hd->httpd_task = GNUNET_SCHEDULER_NO_TASK;
1358 MHD_run (hd->daemon);
1364 * Read data from socket
1366 * @param cls the closure
1367 * @param tc scheduler context
1370 do_read (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
1373 * Read from remote end
1375 * @param cls closure
1376 * @param tc scheduler context
1379 do_read_remote (void* cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
1382 * Write data to remote socket
1384 * @param cls the closure
1385 * @param tc scheduler context
1388 do_write_remote (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1390 struct Socks5Request *s5r = cls;
1393 s5r->fwdwtask = GNUNET_SCHEDULER_NO_TASK;
1395 if ((NULL != tc->read_ready) &&
1396 (GNUNET_NETWORK_fdset_isset (tc->write_ready, s5r->remote_sock)) &&
1397 ((len = GNUNET_NETWORK_socket_send (s5r->remote_sock, s5r->rbuf,
1400 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1401 "Successfully sent %d bytes to remote socket\n",
1406 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "write remote");
1408 if (s5r->rtask != GNUNET_SCHEDULER_NO_TASK)
1409 GNUNET_SCHEDULER_cancel (s5r->rtask);
1410 if (s5r->wtask != GNUNET_SCHEDULER_NO_TASK)
1411 GNUNET_SCHEDULER_cancel (s5r->wtask);
1412 if (s5r->fwdrtask != GNUNET_SCHEDULER_NO_TASK)
1413 GNUNET_SCHEDULER_cancel (s5r->fwdrtask);
1414 GNUNET_NETWORK_socket_close (s5r->remote_sock);
1415 GNUNET_NETWORK_socket_close (s5r->sock);
1421 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
1428 * Clean up s5r handles
1430 * @param s5r the handle to destroy
1433 cleanup_s5r (struct Socks5Request *s5r)
1435 if (s5r->rtask != GNUNET_SCHEDULER_NO_TASK)
1436 GNUNET_SCHEDULER_cancel (s5r->rtask);
1437 if (s5r->fwdwtask != GNUNET_SCHEDULER_NO_TASK)
1438 GNUNET_SCHEDULER_cancel (s5r->fwdwtask);
1439 if (s5r->fwdrtask != GNUNET_SCHEDULER_NO_TASK)
1440 GNUNET_SCHEDULER_cancel (s5r->fwdrtask);
1442 if (NULL != s5r->remote_sock)
1443 GNUNET_NETWORK_socket_close (s5r->remote_sock);
1444 if ((NULL != s5r->sock) && (s5r->cleanup_sock == GNUNET_YES))
1445 GNUNET_NETWORK_socket_close (s5r->sock);
1451 * Write data to socket
1453 * @param cls the closure
1454 * @param tc scheduler context
1457 do_write (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1459 struct Socks5Request *s5r = cls;
1462 s5r->wtask = GNUNET_SCHEDULER_NO_TASK;
1464 if ((NULL != tc->read_ready) &&
1465 (GNUNET_NETWORK_fdset_isset (tc->write_ready, s5r->sock)) &&
1466 ((len = GNUNET_NETWORK_socket_send (s5r->sock, s5r->wbuf,
1469 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1470 "Successfully sent %d bytes to socket\n",
1476 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "write");
1477 s5r->cleanup = GNUNET_YES;
1478 s5r->cleanup_sock = GNUNET_YES;
1484 if (GNUNET_YES == s5r->cleanup)
1490 if ((s5r->state == SOCKS5_DATA_TRANSFER) &&
1491 (s5r->fwdrtask == GNUNET_SCHEDULER_NO_TASK))
1493 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
1495 &do_read_remote, s5r);
1499 * Read from remote end
1501 * @param cls closure
1502 * @param tc scheduler context
1505 do_read_remote (void* cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1507 struct Socks5Request *s5r = cls;
1509 s5r->fwdrtask = GNUNET_SCHEDULER_NO_TASK;
1512 if ((NULL != tc->write_ready) &&
1513 (GNUNET_NETWORK_fdset_isset (tc->read_ready, s5r->remote_sock)) &&
1514 (s5r->wbuf_len = GNUNET_NETWORK_socket_recv (s5r->remote_sock, s5r->wbuf,
1515 sizeof (s5r->wbuf))))
1517 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1518 "Successfully read %d bytes from remote socket\n",
1523 if (s5r->wbuf_len == 0)
1524 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1525 "0 bytes received from remote... graceful shutdown!\n");
1526 if (s5r->fwdwtask != GNUNET_SCHEDULER_NO_TASK)
1527 GNUNET_SCHEDULER_cancel (s5r->fwdwtask);
1528 if (s5r->rtask != GNUNET_SCHEDULER_NO_TASK)
1529 GNUNET_SCHEDULER_cancel (s5r->rtask);
1531 GNUNET_NETWORK_socket_close (s5r->remote_sock);
1532 s5r->remote_sock = NULL;
1533 GNUNET_NETWORK_socket_close (s5r->sock);
1539 s5r->wtask = GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
1547 * Adds a socket to MHD
1549 * @param h the handle to the socket to add
1550 * @param daemon the daemon to add the fd to
1551 * @return whatever MHD_add_connection returns
1554 add_handle_to_mhd (struct GNUNET_NETWORK_Handle *h, struct MHD_Daemon *daemon)
1557 struct sockaddr *addr;
1560 fd = dup (GNUNET_NETWORK_get_fd (h));
1561 addr = GNUNET_NETWORK_get_addr (h);
1562 len = GNUNET_NETWORK_get_addrlen (h);
1564 return MHD_add_connection (daemon, fd, addr, len);
1568 * Calculate size of file
1570 * @param filename name of file
1571 * @return filesize or 0 on error
1574 get_file_size (const char* filename)
1578 fp = fopen (filename, "rb");
1583 if ((0 != fseek (fp, 0, SEEK_END)) || (-1 == (size = ftell (fp))))
1595 * Read file in filename
1597 * @param filename file to read
1598 * @param size pointer where filesize is stored
1602 load_file (const char* filename, unsigned int* size)
1607 *size = get_file_size (filename);
1611 fp = fopen (filename, "rb");
1615 buffer = GNUNET_malloc (*size);
1622 if (*size != fread (buffer, 1, *size, fp))
1624 GNUNET_free (buffer);
1633 * Load PEM key from file
1635 * @param key where to store the data
1636 * @param keyfile path to the PEM file
1639 load_key_from_file (gnutls_x509_privkey_t key, char* keyfile)
1641 gnutls_datum_t key_data;
1642 key_data.data = NULL;
1645 key_data.data = (unsigned char*)load_file (keyfile, &key_data.size);
1647 ret = gnutls_x509_privkey_import (key, &key_data,
1648 GNUTLS_X509_FMT_PEM);
1650 if (GNUTLS_E_SUCCESS != ret)
1652 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1653 "Unable to import private key %s(ret=%d)\n", key_data.data, ret);
1657 GNUNET_free (key_data.data);
1661 * Load cert from file
1663 * @param crt struct to store data in
1664 * @param certfile path to pem file
1667 load_cert_from_file (gnutls_x509_crt_t crt, char* certfile)
1669 gnutls_datum_t cert_data;
1670 cert_data.data = NULL;
1673 cert_data.data = (unsigned char*)load_file (certfile, &cert_data.size);
1675 ret = gnutls_x509_crt_import (crt, &cert_data,
1676 GNUTLS_X509_FMT_PEM);
1677 if (GNUTLS_E_SUCCESS != ret)
1679 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1680 "Unable to import certificate %s(ret=%d)\n", certfile, ret);
1684 GNUNET_free (cert_data.data);
1690 * Generate new certificate for specific name
1692 * @param name the subject name to generate a cert for
1693 * @return a struct holding the PEM data
1695 static struct ProxyGNSCertificate *
1696 generate_gns_certificate (const char *name)
1700 unsigned int serial;
1701 size_t key_buf_size;
1702 size_t cert_buf_size;
1703 gnutls_x509_crt_t request;
1707 ret = gnutls_x509_crt_init (&request);
1709 if (GNUTLS_E_SUCCESS != ret)
1714 ret = gnutls_x509_crt_set_key (request, proxy_ca.key);
1716 if (GNUTLS_E_SUCCESS != ret)
1721 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Generating cert\n");
1723 struct ProxyGNSCertificate *pgc =
1724 GNUNET_malloc (sizeof (struct ProxyGNSCertificate));
1726 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Adding DNs\n");
1728 gnutls_x509_crt_set_dn_by_oid (request, GNUTLS_OID_X520_COUNTRY_NAME,
1731 gnutls_x509_crt_set_dn_by_oid (request, GNUTLS_OID_X520_ORGANIZATION_NAME,
1734 gnutls_x509_crt_set_dn_by_oid (request, GNUTLS_OID_X520_COMMON_NAME,
1735 0, name, strlen (name));
1737 ret = gnutls_x509_crt_set_version (request, 3);
1739 ret = gnutls_rnd (GNUTLS_RND_NONCE, &serial, sizeof (serial));
1741 etime = time (NULL);
1742 tm_data = localtime (&etime);
1745 ret = gnutls_x509_crt_set_serial (request,
1749 ret = gnutls_x509_crt_set_activation_time (request,
1752 etime = mktime (tm_data);
1759 ret = gnutls_x509_crt_set_expiration_time (request,
1761 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Signing...\n");
1763 ret = gnutls_x509_crt_sign (request, proxy_ca.cert, proxy_ca.key);
1765 key_buf_size = sizeof (pgc->key);
1766 cert_buf_size = sizeof (pgc->cert);
1768 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Exporting certificate...\n");
1770 gnutls_x509_crt_export (request, GNUTLS_X509_FMT_PEM,
1771 pgc->cert, &cert_buf_size);
1773 gnutls_x509_privkey_export (proxy_ca.key, GNUTLS_X509_FMT_PEM,
1774 pgc->key, &key_buf_size);
1777 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Cleaning up\n");
1778 gnutls_x509_crt_deinit (request);
1786 * Accept policy for mhdaemons
1789 * @param addr the sockaddr
1790 * @param addrlen the sockaddr length
1791 * @return MHD_NO if sockaddr is wrong or #conns too high
1794 accept_cb (void* cls, const struct sockaddr *addr, socklen_t addrlen)
1796 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1797 "In MHD accept policy cb\n");
1801 if (addr->sa_family == AF_UNIX)
1805 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1806 "Connection accepted\n");
1813 * Adds a socket to an SSL MHD instance
1814 * It is important the the domain name is
1815 * correct. In most cases we need to start a new daemon
1817 * @param h the handle to add to a daemon
1818 * @param domain the domain the ssl daemon has to serve
1819 * @return MHD_YES on success
1822 add_handle_to_ssl_mhd (struct GNUNET_NETWORK_Handle *h, char* domain)
1824 struct MhdHttpList *hd = NULL;
1825 struct ProxyGNSCertificate *pgc;
1826 struct NetworkHandleList *nh;
1828 for (hd = mhd_httpd_head; hd != NULL; hd = hd->next)
1830 if (0 == strcmp (hd->domain, domain))
1837 pgc = generate_gns_certificate (domain);
1839 hd = GNUNET_malloc (sizeof (struct MhdHttpList));
1840 hd->is_ssl = GNUNET_YES;
1841 strcpy (hd->domain, domain);
1842 hd->proxy_cert = pgc;
1845 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1846 "No previous SSL instance found... starting new one for %s\n",
1849 hd->daemon = MHD_start_daemon (MHD_USE_DEBUG | MHD_USE_SSL, 4444,
1851 &create_response, hd,
1852 MHD_OPTION_LISTEN_SOCKET, GNUNET_NETWORK_get_fd (mhd_unix_socket),
1853 MHD_OPTION_CONNECTION_LIMIT, MHD_MAX_CONNECTIONS,
1854 MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) 16,
1855 MHD_OPTION_NOTIFY_COMPLETED,
1857 MHD_OPTION_HTTPS_MEM_KEY, pgc->key,
1858 MHD_OPTION_HTTPS_MEM_CERT, pgc->cert,
1861 GNUNET_assert (hd->daemon != NULL);
1862 hd->httpd_task = GNUNET_SCHEDULER_NO_TASK;
1864 GNUNET_CONTAINER_DLL_insert (mhd_httpd_head, mhd_httpd_tail, hd);
1867 nh = GNUNET_malloc (sizeof (struct NetworkHandleList));
1870 GNUNET_CONTAINER_DLL_insert (hd->socket_handles_head,
1871 hd->socket_handles_tail,
1874 return add_handle_to_mhd (h, hd->daemon);
1880 * Read data from incoming connection
1882 * @param cls the closure
1883 * @param tc the scheduler context
1886 do_read (void* cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1888 struct Socks5Request *s5r = cls;
1889 struct socks5_client_hello *c_hello;
1890 struct socks5_server_hello *s_hello;
1891 struct socks5_client_request *c_req;
1892 struct socks5_server_response *s_resp;
1898 struct hostent *phost;
1900 struct sockaddr_in remote_addr;
1901 struct in_addr *r_sin_addr;
1903 struct NetworkHandleList *nh;
1905 s5r->rtask = GNUNET_SCHEDULER_NO_TASK;
1907 if ((NULL != tc->write_ready) &&
1908 (GNUNET_NETWORK_fdset_isset (tc->read_ready, s5r->sock)) &&
1909 (s5r->rbuf_len = GNUNET_NETWORK_socket_recv (s5r->sock, s5r->rbuf,
1910 sizeof (s5r->rbuf))))
1912 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1913 "Successfully read %d bytes from socket\n",
1918 if (s5r->rbuf_len != 0)
1919 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "read");
1921 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client disco!\n");
1923 if (s5r->fwdrtask != GNUNET_SCHEDULER_NO_TASK)
1924 GNUNET_SCHEDULER_cancel (s5r->fwdrtask);
1925 if (s5r->wtask != GNUNET_SCHEDULER_NO_TASK)
1926 GNUNET_SCHEDULER_cancel (s5r->wtask);
1927 if (s5r->fwdwtask != GNUNET_SCHEDULER_NO_TASK)
1928 GNUNET_SCHEDULER_cancel (s5r->fwdwtask);
1929 GNUNET_NETWORK_socket_close (s5r->remote_sock);
1930 GNUNET_NETWORK_socket_close (s5r->sock);
1935 if (s5r->state == SOCKS5_INIT)
1937 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1939 c_hello = (struct socks5_client_hello*)&s5r->rbuf;
1941 GNUNET_assert (c_hello->version == SOCKS_VERSION_5);
1943 s_hello = (struct socks5_server_hello*)&s5r->wbuf;
1944 s5r->wbuf_len = sizeof( struct socks5_server_hello );
1946 s_hello->version = c_hello->version;
1947 s_hello->auth_method = SOCKS_AUTH_NONE;
1949 /* Write response to client */
1950 s5r->wtask = GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
1954 s5r->rtask = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
1958 s5r->state = SOCKS5_REQUEST;
1962 if (s5r->state == SOCKS5_REQUEST)
1964 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1965 "Processing SOCKS5 request\n");
1966 c_req = (struct socks5_client_request*)&s5r->rbuf;
1967 s_resp = (struct socks5_server_response*)&s5r->wbuf;
1968 //Only 10byte for ipv4 response!
1969 s5r->wbuf_len = 10;//sizeof (struct socks5_server_response);
1971 GNUNET_assert (c_req->addr_type == 3);
1973 dom_len = *((uint8_t*)(&(c_req->addr_type) + 1));
1974 memset(domain, 0, sizeof(domain));
1975 strncpy(domain, (char*)(&(c_req->addr_type) + 2), dom_len);
1976 req_port = *((uint16_t*)(&(c_req->addr_type) + 2 + dom_len));
1978 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1979 "Requested connection is %s:%d\n",
1983 if (is_tld (domain, GNUNET_GNS_TLD) ||
1984 is_tld (domain, GNUNET_GNS_TLD_ZKEY))
1986 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1987 "Requested connection is gnunet tld\n",
1991 if (ntohs(req_port) == HTTPS_PORT)
1993 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1994 "Requested connection is HTTPS\n");
1995 ret = add_handle_to_ssl_mhd ( s5r->sock, domain );
1997 else if (NULL != httpd)
1999 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2000 "Requested connection is HTTP\n");
2001 nh = GNUNET_malloc (sizeof (struct NetworkHandleList));
2004 GNUNET_CONTAINER_DLL_insert (mhd_httpd_head->socket_handles_head,
2005 mhd_httpd_head->socket_handles_tail,
2008 ret = add_handle_to_mhd ( s5r->sock, httpd );
2013 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2014 _("Failed to start HTTP server\n"));
2015 s_resp->version = 0x05;
2016 s_resp->reply = 0x01;
2017 s5r->cleanup = GNUNET_YES;
2018 s5r->cleanup_sock = GNUNET_YES;
2020 GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
2026 /* Signal success */
2027 s_resp->version = 0x05;
2028 s_resp->reply = 0x00;
2029 s_resp->reserved = 0x00;
2030 s_resp->addr_type = 0x01;
2032 s5r->cleanup = GNUNET_YES;
2033 s5r->cleanup_sock = GNUNET_NO;
2035 GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
2043 phost = (struct hostent*)gethostbyname (domain);
2046 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2047 "Resolve %s error!\n", domain );
2048 s_resp->version = 0x05;
2049 s_resp->reply = 0x01;
2050 s5r->cleanup = GNUNET_YES;
2051 s5r->cleanup_sock = GNUNET_YES;
2053 GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
2059 s5r->remote_sock = GNUNET_NETWORK_socket_create (AF_INET,
2062 r_sin_addr = (struct in_addr*)(phost->h_addr);
2063 remote_ip = r_sin_addr->s_addr;
2064 memset(&remote_addr, 0, sizeof(remote_addr));
2065 remote_addr.sin_family = AF_INET;
2066 #if HAVE_SOCKADDR_IN_SIN_LEN
2067 remote_addr.sin_len = sizeof (remote_addr);
2069 remote_addr.sin_addr.s_addr = remote_ip;
2070 remote_addr.sin_port = req_port;
2072 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2073 "target server: %s:%u\n", inet_ntoa(remote_addr.sin_addr),
2077 GNUNET_NETWORK_socket_connect ( s5r->remote_sock,
2078 (const struct sockaddr*)&remote_addr,
2079 sizeof (remote_addr)))
2080 && (errno != EINPROGRESS))
2082 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "connect");
2083 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2084 "socket request error...\n");
2085 s_resp->version = 0x05;
2086 s_resp->reply = 0x01;
2088 GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
2095 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2096 "new remote connection\n");
2098 s_resp->version = 0x05;
2099 s_resp->reply = 0x00;
2100 s_resp->reserved = 0x00;
2101 s_resp->addr_type = 0x01;
2103 s5r->state = SOCKS5_DATA_TRANSFER;
2106 GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
2110 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
2118 if (s5r->state == SOCKS5_DATA_TRANSFER)
2120 if ((s5r->remote_sock == NULL) || (s5r->rbuf_len == 0))
2122 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2123 "Closing connection to client\n");
2124 if (s5r->rtask != GNUNET_SCHEDULER_NO_TASK)
2125 GNUNET_SCHEDULER_cancel (s5r->rtask);
2126 if (s5r->fwdwtask != GNUNET_SCHEDULER_NO_TASK)
2127 GNUNET_SCHEDULER_cancel (s5r->fwdwtask);
2128 if (s5r->fwdrtask != GNUNET_SCHEDULER_NO_TASK)
2129 GNUNET_SCHEDULER_cancel (s5r->fwdrtask);
2130 if (s5r->fwdrtask != GNUNET_SCHEDULER_NO_TASK)
2131 GNUNET_SCHEDULER_cancel (s5r->fwdrtask);
2133 if (s5r->remote_sock != NULL)
2134 GNUNET_NETWORK_socket_close (s5r->remote_sock);
2135 GNUNET_NETWORK_socket_close (s5r->sock);
2140 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2141 "forwarding %d bytes from client\n", s5r->rbuf_len);
2144 GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
2146 &do_write_remote, s5r);
2148 if (s5r->fwdrtask == GNUNET_SCHEDULER_NO_TASK)
2151 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
2153 &do_read_remote, s5r);
2159 //GNUNET_CONTAINER_DLL_remove (s5conns.head, s5conns.tail, s5r);
2165 * Accept new incoming connections
2167 * @param cls the closure
2168 * @param tc the scheduler context
2171 do_accept (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2173 struct GNUNET_NETWORK_Handle *s;
2174 struct Socks5Request *s5r;
2176 ltask = GNUNET_SCHEDULER_NO_TASK;
2177 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
2180 ltask = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
2184 s = GNUNET_NETWORK_socket_accept (lsock, NULL, NULL);
2188 GNUNET_log_strerror (GNUNET_ERROR_TYPE_INFO, "accept");
2192 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2193 "Got an inbound connection, waiting for data\n");
2195 s5r = GNUNET_malloc (sizeof (struct Socks5Request));
2197 s5r->state = SOCKS5_INIT;
2198 s5r->wtask = GNUNET_SCHEDULER_NO_TASK;
2199 s5r->fwdwtask = GNUNET_SCHEDULER_NO_TASK;
2200 s5r->fwdrtask = GNUNET_SCHEDULER_NO_TASK;
2201 s5r->rtask = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
2204 //GNUNET_CONTAINER_DLL_insert (s5conns.head, s5conns.tail, s5r);
2209 * Task run on shutdown
2211 * @param cls closure
2212 * @param tc task context
2215 do_shutdown (void *cls,
2216 const struct GNUNET_SCHEDULER_TaskContext *tc)
2219 struct MhdHttpList *hd;
2220 struct MhdHttpList *tmp_hd;
2221 struct NetworkHandleList *nh;
2222 struct NetworkHandleList *tmp_nh;
2223 struct ProxyCurlTask *ctask;
2224 struct ProxyCurlTask *ctask_tmp;
2226 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2227 "Shutting down...\n");
2229 gnutls_global_deinit ();
2231 if (GNUNET_SCHEDULER_NO_TASK != curl_download_task)
2233 GNUNET_SCHEDULER_cancel (curl_download_task);
2234 curl_download_task = GNUNET_SCHEDULER_NO_TASK;
2237 for (hd = mhd_httpd_head; hd != NULL; hd = tmp_hd)
2241 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2242 "Stopping daemon\n");
2244 if (GNUNET_SCHEDULER_NO_TASK != hd->httpd_task)
2246 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2247 "Stopping select task %d\n",
2249 GNUNET_SCHEDULER_cancel (hd->httpd_task);
2250 hd->httpd_task = GNUNET_SCHEDULER_NO_TASK;
2253 if (NULL != hd->daemon)
2255 MHD_stop_daemon (hd->daemon);
2259 for (nh = hd->socket_handles_head; nh != NULL; nh = tmp_nh)
2263 GNUNET_NETWORK_socket_close (nh->h);
2268 if (NULL != hd->proxy_cert)
2270 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2271 "Free certificate\n");
2272 GNUNET_free (hd->proxy_cert);
2278 for (ctask=ctasks_head; ctask != NULL; ctask=ctask_tmp)
2280 ctask_tmp = ctask->next;
2282 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2283 "Cleaning up cURL task\n");
2285 if (ctask->curl != NULL)
2286 curl_easy_cleanup (ctask->curl);
2288 if (NULL != ctask->headers)
2289 curl_slist_free_all (ctask->headers);
2290 if (NULL != ctask->resolver)
2291 curl_slist_free_all (ctask->resolver);
2293 if (NULL != ctask->response)
2294 MHD_destroy_response (ctask->response);
2297 GNUNET_free (ctask);
2300 GNUNET_GNS_disconnect (gns_handle);
2305 * Compiles a regex for us
2307 * @param re ptr to re struct
2308 * @param rt the expression to compile
2309 * @return 0 on success
2312 compile_regex (regex_t *re, const char* rt)
2317 status = regcomp (re, rt, REG_EXTENDED|REG_NEWLINE);
2320 regerror (status, re, err, 1024);
2321 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2322 "Regex error compiling '%s': %s\n", rt, err);
2330 * Loads the users local zone key
2332 * @return GNUNET_YES on success
2335 load_local_zone_key (const struct GNUNET_CONFIGURATION_Handle *cfg)
2338 struct GNUNET_CRYPTO_RsaPrivateKey *key = NULL;
2339 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pkey;
2340 struct GNUNET_CRYPTO_ShortHashCode *zone = NULL;
2341 struct GNUNET_CRYPTO_ShortHashAsciiEncoded zonename;
2343 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "gns",
2344 "ZONEKEY", &keyfile))
2346 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2347 "Unable to load zone key config value!\n");
2351 if (GNUNET_NO == GNUNET_DISK_file_test (keyfile))
2353 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2354 "Unable to load zone key %s!\n", keyfile);
2355 GNUNET_free(keyfile);
2359 key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
2360 GNUNET_CRYPTO_rsa_key_get_public (key, &pkey);
2361 GNUNET_CRYPTO_short_hash(&pkey,
2362 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
2364 zone = &local_gns_zone;
2365 GNUNET_CRYPTO_short_hash_to_enc (zone, &zonename);
2366 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2367 "Using zone: %s!\n", &zonename);
2368 GNUNET_CRYPTO_rsa_key_free(key);
2369 GNUNET_free(keyfile);
2371 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "gns",
2372 "PRIVATE_ZONEKEY", &keyfile))
2374 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2375 "Unable to load private zone key config value!\n");
2379 if (GNUNET_NO == GNUNET_DISK_file_test (keyfile))
2381 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2382 "Unable to load private zone key %s!\n", keyfile);
2383 GNUNET_free(keyfile);
2387 key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
2388 GNUNET_CRYPTO_rsa_key_get_public (key, &pkey);
2389 GNUNET_CRYPTO_short_hash(&pkey,
2390 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
2391 &local_private_zone);
2392 GNUNET_CRYPTO_short_hash_to_enc (zone, &zonename);
2393 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2394 "Using private zone: %s!\n", &zonename);
2395 GNUNET_CRYPTO_rsa_key_free(key);
2396 GNUNET_free(keyfile);
2398 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "gns",
2399 "SHORTEN_ZONEKEY", &keyfile))
2401 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2402 "Unable to load shorten zone key config value!\n");
2406 if (GNUNET_NO == GNUNET_DISK_file_test (keyfile))
2408 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2409 "Unable to load shorten zone key %s!\n", keyfile);
2410 GNUNET_free(keyfile);
2414 key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
2415 GNUNET_CRYPTO_rsa_key_get_public (key, &pkey);
2416 GNUNET_CRYPTO_short_hash(&pkey,
2417 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
2418 &local_shorten_zone);
2419 GNUNET_CRYPTO_short_hash_to_enc (zone, &zonename);
2420 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2421 "Using shorten zone: %s!\n", &zonename);
2422 GNUNET_CRYPTO_rsa_key_free(key);
2423 GNUNET_free(keyfile);
2429 * Main function that will be run
2431 * @param cls closure
2432 * @param args remaining command-line arguments
2433 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
2434 * @param cfg configuration
2437 run (void *cls, char *const *args, const char *cfgfile,
2438 const struct GNUNET_CONFIGURATION_Handle *cfg)
2440 struct sockaddr_in sa;
2441 struct MhdHttpList *hd;
2442 struct sockaddr_un mhd_unix_sock_addr;
2444 char* proxy_sockfile;
2445 char* cafile_cfg = NULL;
2447 char* shorten_keyfile;
2451 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2454 cafile = cafile_opt;
2458 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "gns-proxy",
2462 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2463 "Unable to load proxy CA config value!\n");
2464 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2465 "No proxy CA provided!\n");
2468 cafile = cafile_cfg;
2471 gnutls_global_init ();
2473 gnutls_x509_crt_init (&proxy_ca.cert);
2474 gnutls_x509_privkey_init (&proxy_ca.key);
2476 load_cert_from_file (proxy_ca.cert, cafile);
2477 load_key_from_file (proxy_ca.key, cafile);
2479 GNUNET_free_non_null (cafile_cfg);
2481 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2482 "Loading Template\n");
2484 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "gns-proxy",
2488 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2489 "Unable to load shorten zonekey config value!\n");
2490 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2491 "No shorten key provided!\n");
2496 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2497 "Loading shorten zonekey %s!\n",
2499 shorten_zonekey = GNUNET_CRYPTO_rsa_key_create_from_file (shorten_keyfile);
2500 GNUNET_free (shorten_keyfile);
2503 compile_regex (&re_dotplus, (char*) RE_A_HREF);
2505 gns_handle = GNUNET_GNS_connect (cfg);
2507 if (GNUNET_NO == load_local_zone_key (cfg))
2509 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2510 "Unable to load zone!\n");
2514 if (NULL == gns_handle)
2516 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2517 "Unable to connect to GNS!\n");
2521 memset (&sa, 0, sizeof (sa));
2522 sa.sin_family = AF_INET;
2523 sa.sin_port = htons (port);
2524 #if HAVE_SOCKADDR_IN_SIN_LEN
2525 sa.sin_len = sizeof (sa);
2528 lsock = GNUNET_NETWORK_socket_create (AF_INET,
2532 if ((NULL == lsock) ||
2534 GNUNET_NETWORK_socket_bind (lsock, (const struct sockaddr *) &sa,
2537 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2538 "Failed to create listen socket bound to `%s'",
2539 GNUNET_a2s ((const struct sockaddr *) &sa, sizeof (sa)));
2541 GNUNET_NETWORK_socket_close (lsock);
2545 if (GNUNET_OK != GNUNET_NETWORK_socket_listen (lsock, 5))
2547 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2548 "Failed to listen on socket bound to `%s'",
2549 GNUNET_a2s ((const struct sockaddr *) &sa, sizeof (sa)));
2553 ltask = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
2554 lsock, &do_accept, NULL);
2559 if (0 != curl_global_init (CURL_GLOBAL_WIN32))
2561 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2562 "cURL global init failed!\n");
2566 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2567 "Proxy listens on port %u\n",
2570 mhd_httpd_head = NULL;
2571 mhd_httpd_tail = NULL;
2572 total_mhd_connections = 0;
2574 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "gns-proxy",
2578 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2579 "Specify PROXY_UNIXPATH in gns-proxy config section!\n");
2583 mhd_unix_socket = GNUNET_NETWORK_socket_create (AF_UNIX,
2587 if (NULL == mhd_unix_socket)
2589 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2590 "Unable to create unix domain socket!\n");
2594 mhd_unix_sock_addr.sun_family = AF_UNIX;
2595 strcpy (mhd_unix_sock_addr.sun_path, proxy_sockfile);
2596 unlink (proxy_sockfile);
2597 len = strlen (proxy_sockfile) + sizeof(AF_UNIX);
2599 GNUNET_free (proxy_sockfile);
2601 if (GNUNET_OK != GNUNET_NETWORK_socket_bind (mhd_unix_socket,
2602 (struct sockaddr*)&mhd_unix_sock_addr,
2605 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2606 "Unable to bind unix domain socket!\n");
2610 if (GNUNET_OK != GNUNET_NETWORK_socket_listen (mhd_unix_socket,
2613 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2614 "Unable to listen on unix domain socket!\n");
2618 hd = GNUNET_malloc (sizeof (struct MhdHttpList));
2619 hd->is_ssl = GNUNET_NO;
2620 strcpy (hd->domain, "");
2621 httpd = MHD_start_daemon (MHD_USE_DEBUG, 4444, //Dummy port
2623 &create_response, hd,
2624 MHD_OPTION_LISTEN_SOCKET, GNUNET_NETWORK_get_fd (mhd_unix_socket),
2625 MHD_OPTION_CONNECTION_LIMIT, MHD_MAX_CONNECTIONS,
2626 MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) 16,
2627 MHD_OPTION_NOTIFY_COMPLETED,
2631 GNUNET_assert (httpd != NULL);
2633 hd->httpd_task = GNUNET_SCHEDULER_NO_TASK;
2635 GNUNET_CONTAINER_DLL_insert (mhd_httpd_head, mhd_httpd_tail, hd);
2639 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
2640 &do_shutdown, NULL);
2646 * The main function for gnunet-gns-proxy.
2648 * @param argc number of arguments from the command line
2649 * @param argv command line arguments
2650 * @return 0 ok, 1 on error
2653 main (int argc, char *const *argv)
2655 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
2657 gettext_noop ("listen on specified port"), 1,
2658 &GNUNET_GETOPT_set_string, &port},
2659 {'a', "authority", NULL,
2660 gettext_noop ("pem file to use as CA"), 1,
2661 &GNUNET_GETOPT_set_string, &cafile_opt},
2662 GNUNET_GETOPT_OPTION_END
2667 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
2671 GNUNET_log_setup ("gnunet-gns-proxy", "WARNING", NULL);
2674 GNUNET_PROGRAM_run (argc, argv, "gnunet-gns-proxy",
2675 _("GNUnet GNS proxy"),
2677 &run, NULL)) ? 0 : 1;
2678 GNUNET_free_non_null ((char*)argv);