2 This file is part of GNUnet.
3 (C) 2012-2013 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.
21 * @author Martin Schanzenbach
22 * @author Christian Grothoff
23 * @file src/gns/gnunet-gns-proxy.c
24 * @brief HTTP(S) proxy that rewrites URIs and fakes certificats to make GNS work
25 * with legacy browsers
28 * - double-check queueing logic
29 * - improve IPv6 support (#3037)
30 * - actually check SSL certificates (#3038)
33 #include <microhttpd.h>
34 #include <curl/curl.h>
35 #include <gnutls/gnutls.h>
36 #include <gnutls/x509.h>
37 #include <gnutls/abstract.h>
38 #include <gnutls/crypto.h>
40 #include "gnunet_util_lib.h"
41 #include "gnunet_gns_service.h"
42 #include "gnunet_identity_service.h"
47 * Default Socks5 listen port.
49 #define GNUNET_GNS_PROXY_PORT 7777
52 * Maximum supported length for a URI.
53 * Should die. @deprecated
55 #define MAX_HTTP_URI_LENGTH 2048
58 * Size of the buffer for the data upload / download. Must be
59 * enough for curl, thus CURL_MAX_WRITE_SIZE is needed here (16k).
61 #define IO_BUFFERSIZE CURL_MAX_WRITE_SIZE
64 * Size of the read/write buffers for Socks. Uses
65 * 256 bytes for the hostname (at most), plus a few
66 * bytes overhead for the messages.
68 #define SOCKS_BUFFERSIZE (256 + 32)
71 * Port for plaintext HTTP.
78 #define HTTPS_PORT 443
81 * Largest allowed size for a PEM certificate.
83 #define MAX_PEM_SIZE (10 * 1024)
86 * After how long do we clean up unused MHD SSL/TLS instances?
88 #define MHD_CACHE_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 5)
91 * After how long do we clean up Socks5 handles that failed to show any activity
92 * with their respective MHD instance?
94 #define HTTP_HANDSHAKE_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 15)
100 * @param level log level
101 * @param fun name of curl_easy-function that gave the error
102 * @param rc return code from curl
104 #define LOG_CURL_EASY(level,fun,rc) GNUNET_log(level, _("%s failed at %s:%d: `%s'\n"), fun, __FILE__, __LINE__, curl_easy_strerror (rc))
107 /* *************** Socks protocol definitions (move to TUN?) ****************** */
110 * Which SOCKS version do we speak?
112 #define SOCKS_VERSION_5 0x05
115 * Flag to set for 'no authentication'.
117 #define SOCKS_AUTH_NONE 0
121 * Commands in Socks5.
126 * Establish TCP/IP stream.
128 SOCKS5_CMD_TCP_STREAM = 1,
131 * Establish TCP port binding.
133 SOCKS5_CMD_TCP_PORT = 2,
136 * Establish UDP port binding.
138 SOCKS5_CMD_UDP_PORT = 3
143 * Address types in Socks5.
145 enum Socks5AddressType
155 SOCKS5_AT_DOMAINNAME = 3,
166 * Status codes in Socks5 response.
168 enum Socks5StatusCode
170 SOCKS5_STATUS_REQUEST_GRANTED = 0,
171 SOCKS5_STATUS_GENERAL_FAILURE = 1,
172 SOCKS5_STATUS_CONNECTION_NOT_ALLOWED_BY_RULE = 2,
173 SOCKS5_STATUS_NETWORK_UNREACHABLE = 3,
174 SOCKS5_STATUS_HOST_UNREACHABLE = 4,
175 SOCKS5_STATUS_CONNECTION_REFUSED_BY_HOST = 5,
176 SOCKS5_STATUS_TTL_EXPIRED = 6,
177 SOCKS5_STATUS_COMMAND_NOT_SUPPORTED = 7,
178 SOCKS5_STATUS_ADDRESS_TYPE_NOT_SUPPORTED = 8
183 * Client hello in Socks5 protocol.
185 struct Socks5ClientHelloMessage
188 * Should be #SOCKS_VERSION_5.
193 * How many authentication methods does the client support.
195 uint8_t num_auth_methods;
197 /* followed by supported authentication methods, 1 byte per method */
203 * Server hello in Socks5 protocol.
205 struct Socks5ServerHelloMessage
208 * Should be #SOCKS_VERSION_5.
213 * Chosen authentication method, for us always #SOCKS_AUTH_NONE,
214 * which skips the authentication step.
221 * Client socks request in Socks5 protocol.
223 struct Socks5ClientRequestMessage
226 * Should be #SOCKS_VERSION_5.
231 * Command code, we only uspport #SOCKS5_CMD_TCP_STREAM.
236 * Reserved, always zero.
241 * Address type, an `enum Socks5AddressType`.
246 * Followed by either an ip4/ipv6 address or a domain name with a
247 * length field (uint8_t) in front (depending on @e addr_type).
248 * followed by port number in network byte order (uint16_t).
254 * Server response to client requests in Socks5 protocol.
256 struct Socks5ServerResponseMessage
259 * Should be #SOCKS_VERSION_5.
264 * Status code, an `enum Socks5StatusCode`
274 * Address type, an `enum Socks5AddressType`.
279 * Followed by either an ip4/ipv6 address or a domain name with a
280 * length field (uint8_t) in front (depending on @e addr_type).
281 * followed by port number in network byte order (uint16_t).
288 /* *********************** Datastructures for HTTP handling ****************** */
291 * A structure for CA cert/key
298 gnutls_x509_crt_t cert;
303 gnutls_x509_privkey_t key;
308 * Structure for GNS certificates
310 struct ProxyGNSCertificate
313 * The certificate as PEM
315 char cert[MAX_PEM_SIZE];
318 * The private key as PEM
320 char key[MAX_PEM_SIZE];
326 * A structure for all running Httpds
333 struct MhdHttpList *prev;
338 struct MhdHttpList *next;
341 * the domain name to server (only important for SSL)
348 struct MHD_Daemon *daemon;
351 * Optional proxy certificate used
353 struct ProxyGNSCertificate *proxy_cert;
358 GNUNET_SCHEDULER_TaskIdentifier httpd_task;
361 * is this an ssl daemon?
368 /* ***************** Datastructures for Socks handling **************** */
377 * We're waiting to get the client hello.
382 * We're waiting to get the initial request.
387 * We are currently resolving the destination.
392 * We're in transfer mode.
394 SOCKS5_DATA_TRANSFER,
397 * Finish writing the write buffer, then clean up.
399 SOCKS5_WRITE_THEN_CLEANUP,
402 * Socket has been passed to MHD, do not close it anymore.
404 SOCKS5_SOCKET_WITH_MHD,
407 * We've finished receiving upload data from MHD.
409 SOCKS5_SOCKET_UPLOAD_STARTED,
412 * We've finished receiving upload data from MHD.
414 SOCKS5_SOCKET_UPLOAD_DONE,
417 * We've finished uploading data via CURL and can now download.
419 SOCKS5_SOCKET_DOWNLOAD_STARTED,
422 * We've finished receiving download data from cURL.
424 SOCKS5_SOCKET_DOWNLOAD_DONE
430 * A structure for socks requests
438 struct Socks5Request *next;
443 struct Socks5Request *prev;
448 struct GNUNET_NETWORK_Handle *sock;
451 * Handle to GNS lookup, during #SOCKS5_RESOLVING phase.
453 struct GNUNET_GNS_LookupRequest *gns_lookup;
456 * Client socket read task
458 GNUNET_SCHEDULER_TaskIdentifier rtask;
461 * Client socket write task
463 GNUNET_SCHEDULER_TaskIdentifier wtask;
468 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
473 char rbuf[SOCKS_BUFFERSIZE];
478 char wbuf[SOCKS_BUFFERSIZE];
481 * Buffer we use for moving data between MHD and curl (in both directions).
483 char io_buf[IO_BUFFERSIZE];
486 * MHD HTTP instance handling this request, NULL for none.
488 struct MhdHttpList *hd;
491 * MHD response object for this request.
493 struct MHD_Response *response;
496 * the domain name to server (only important for SSL)
501 * DNS Legacy Host Name as given by GNS, NULL if not given.
516 * HTTP request headers for the curl request.
518 struct curl_slist *headers;
521 * HTTP response code to give to MHD for the response.
523 unsigned int response_code;
526 * Number of bytes already in read buffer
531 * Number of bytes already in write buffer
536 * Number of bytes already in the IO buffer.
541 * Once known, what's the target address for the connection?
543 struct sockaddr_storage destination_address;
548 enum SocksPhase state;
551 * Desired destination port.
559 /* *********************** Globals **************************** */
563 * The port the proxy is running on (default 7777)
565 static unsigned long port = GNUNET_GNS_PROXY_PORT;
568 * The CA file (pem) to use for the proxy CA
570 static char *cafile_opt;
573 * The listen socket of the proxy
575 static struct GNUNET_NETWORK_Handle *lsock;
580 static GNUNET_SCHEDULER_TaskIdentifier ltask;
583 * The cURL download task (curl multi API).
585 static GNUNET_SCHEDULER_TaskIdentifier curl_download_task;
588 * The cURL multi handle
590 static CURLM *curl_multi;
593 * Handle to the GNS service
595 static struct GNUNET_GNS_Handle *gns_handle;
598 * DLL for http/https daemons
600 static struct MhdHttpList *mhd_httpd_head;
603 * DLL for http/https daemons
605 static struct MhdHttpList *mhd_httpd_tail;
608 * Daemon for HTTP (we have one per SSL certificate, and then one for
609 * all HTTP connections; this is the one for HTTP, not HTTPS).
611 static struct MhdHttpList *httpd;
614 * DLL of active socks requests.
616 static struct Socks5Request *s5r_head;
619 * DLL of active socks requests.
621 static struct Socks5Request *s5r_tail;
624 * The users local GNS master zone
626 static struct GNUNET_CRYPTO_EccPublicSignKey local_gns_zone;
629 * The users local shorten zone
631 static struct GNUNET_CRYPTO_EccPrivateKey local_shorten_zone;
634 * Is shortening enabled?
636 static int do_shorten;
639 * The CA for SSL certificate generation
641 static struct ProxyCA proxy_ca;
644 * Response we return on cURL failures.
646 static struct MHD_Response *curl_failure_response;
649 * Connection to identity service.
651 static struct GNUNET_IDENTITY_Handle *identity;
654 * Request for our ego.
656 static struct GNUNET_IDENTITY_Operation *id_op;
661 static const struct GNUNET_CONFIGURATION_Handle *cfg;
664 /* ************************* Global helpers ********************* */
668 * Run MHD now, we have extra data ready for the callback.
670 * @param hd the daemon to run now.
673 run_mhd_now (struct MhdHttpList *hd);
677 * Clean up s5r handles.
679 * @param s5r the handle to destroy
682 cleanup_s5r (struct Socks5Request *s5r)
684 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
685 "Cleaning up socks request\n");
686 if (NULL != s5r->curl)
688 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
689 "Cleaning up cURL handle\n");
690 curl_multi_remove_handle (curl_multi, s5r->curl);
691 curl_easy_cleanup (s5r->curl);
694 curl_slist_free_all (s5r->headers);
695 if (NULL != s5r->response)
696 MHD_destroy_response (s5r->response);
697 if (GNUNET_SCHEDULER_NO_TASK != s5r->rtask)
698 GNUNET_SCHEDULER_cancel (s5r->rtask);
699 if (GNUNET_SCHEDULER_NO_TASK != s5r->timeout_task)
700 GNUNET_SCHEDULER_cancel (s5r->timeout_task);
701 if (GNUNET_SCHEDULER_NO_TASK != s5r->wtask)
702 GNUNET_SCHEDULER_cancel (s5r->wtask);
703 if (NULL != s5r->gns_lookup)
704 GNUNET_GNS_lookup_cancel (s5r->gns_lookup);
705 if (NULL != s5r->sock)
707 if (SOCKS5_SOCKET_WITH_MHD == s5r->state)
708 GNUNET_NETWORK_socket_free_memory_only_ (s5r->sock);
710 GNUNET_NETWORK_socket_close (s5r->sock);
712 GNUNET_CONTAINER_DLL_remove (s5r_head,
715 GNUNET_free_non_null (s5r->domain);
716 GNUNET_free_non_null (s5r->leho);
717 GNUNET_free_non_null (s5r->url);
722 /* ************************* HTTP handling with cURL *********************** */
726 * Callback for MHD response generation. This function is called from
727 * MHD whenever MHD expects to get data back. Copies data from the
728 * io_buf, if available.
730 * @param cls closure with our `struct Socks5Request`
731 * @param pos in buffer
732 * @param buf where to copy data
733 * @param max available space in @a buf
734 * @return number of bytes written to @a buf
737 mhd_content_cb (void *cls,
742 struct Socks5Request *s5r = cls;
743 size_t bytes_to_copy;
745 if ( (SOCKS5_SOCKET_UPLOAD_STARTED == s5r->state) ||
746 (SOCKS5_SOCKET_UPLOAD_DONE == s5r->state) )
748 /* we're still not done with the upload, do not yet
749 start the download, the IO buffer is still full
751 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
752 "Pausing MHD download, not yet ready for download\n");
753 return 0; /* not yet ready for data download */
755 bytes_to_copy = GNUNET_MIN (max,
757 if ( (0 == bytes_to_copy) &&
758 (SOCKS5_SOCKET_DOWNLOAD_DONE != s5r->state) )
760 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
761 "Pausing MHD download, no data available\n");
762 return 0; /* more data later */
764 if ( (0 == bytes_to_copy) &&
765 (SOCKS5_SOCKET_DOWNLOAD_DONE == s5r->state) )
767 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
768 "Completed MHD download\n");
769 return MHD_CONTENT_READER_END_OF_STREAM;
771 memcpy (buf, s5r->io_buf, bytes_to_copy);
772 memmove (s5r->io_buf,
773 &s5r->io_buf[bytes_to_copy],
774 s5r->io_len - bytes_to_copy);
775 s5r->io_len -= bytes_to_copy;
776 if (NULL != s5r->curl)
777 curl_easy_pause (s5r->curl, CURLPAUSE_CONT);
778 return bytes_to_copy;
783 * Check that the website has presented us with a valid SSL certificate.
784 * The certificate must either match the domain name or the LEHO name
785 * (or, if available, the TLSA record).
787 * @param s5r request to check for.
788 * @return #GNUNET_OK if the certificate is valid
791 check_ssl_certificate (struct Socks5Request *s5r)
794 struct curl_slist *to_info;
795 struct curl_certinfo *to_certinfo;
798 struct curl_slist *slist;
802 curl_easy_getinfo (s5r->curl,
805 return GNUNET_SYSERR;
806 /* FIXME: for now, we just output the certs to stderr, we should
807 check them against LEHO / TLSA record information here! (#3038) */
808 if(NULL != ptr.to_info)
810 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
812 ptr.to_certinfo->num_of_certs);
813 for (i = 0; i < ptr.to_certinfo->num_of_certs; i++)
815 for (slist = ptr.to_certinfo->certinfo[i]; NULL != slist; slist = slist->next)
816 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
817 "Certificate #%d: %s\n",
827 * We're getting an HTTP response header from cURL. Convert it to the
828 * MHD response headers. Mostly copies the headers, but makes special
829 * adjustments to "Set-Cookie" and "Location" headers as those may need
830 * to be changed from the LEHO to the domain the browser expects.
832 * @param buffer curl buffer with a single line of header data; not 0-terminated!
833 * @param size curl blocksize
834 * @param nmemb curl blocknumber
835 * @param cls our `struct Socks5Request *`
836 * @return size of processed bytes
839 curl_check_hdr (void *buffer, size_t size, size_t nmemb, void *cls)
841 struct Socks5Request *s5r = cls;
842 size_t bytes = size * nmemb;
844 const char *hdr_type;
845 const char *cookie_domain;
848 char *new_cookie_hdr;
851 size_t delta_cdomain;
855 if (NULL == s5r->response)
857 /* first, check SSL certificate */
858 if ( (HTTPS_PORT == s5r->port) &&
859 (GNUNET_OK != check_ssl_certificate (s5r)) )
862 GNUNET_break (CURLE_OK ==
863 curl_easy_getinfo (s5r->curl,
864 CURLINFO_RESPONSE_CODE,
866 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
867 "Creating MHD response with code %d\n",
869 s5r->response_code = resp_code;
870 s5r->response = MHD_create_response_from_callback (MHD_SIZE_UNKNOWN,
875 if (NULL != s5r->leho)
879 GNUNET_asprintf (&cors_hdr,
880 (HTTPS_PORT == s5r->port)
885 GNUNET_break (MHD_YES ==
886 MHD_add_response_header (s5r->response,
887 MHD_HTTP_HEADER_ACCESS_CONTROL_ALLOW_ORIGIN,
889 GNUNET_free (cors_hdr);
891 /* force connection to be closed after each request, as we
892 do not support HTTP pipelining */
893 GNUNET_break (MHD_YES ==
894 MHD_add_response_header (s5r->response,
895 MHD_HTTP_HEADER_CONNECTION,
899 ndup = GNUNET_strndup (buffer, bytes);
900 hdr_type = strtok (ndup, ":");
901 if (NULL == hdr_type)
906 hdr_val = strtok (NULL, "");
915 /* custom logic for certain header types */
916 new_cookie_hdr = NULL;
917 if (0 == strcasecmp (hdr_type,
918 MHD_HTTP_HEADER_SET_COOKIE))
920 new_cookie_hdr = GNUNET_malloc (strlen (hdr_val) +
921 strlen (s5r->domain) + 1);
923 domain_matched = GNUNET_NO; /* make sure we match domain at most once */
924 for (tok = strtok (hdr_val, ";"); NULL != tok; tok = strtok (NULL, ";"))
926 if ( (0 == strncasecmp (tok, " domain", strlen (" domain"))) &&
927 (GNUNET_NO == domain_matched) )
929 domain_matched = GNUNET_YES;
930 cookie_domain = tok + strlen (" domain") + 1;
931 if (strlen (cookie_domain) < strlen (s5r->leho))
933 delta_cdomain = strlen (s5r->leho) - strlen (cookie_domain);
934 if (0 == strcasecmp (cookie_domain, s5r->leho + delta_cdomain))
936 offset += sprintf (new_cookie_hdr + offset,
942 else if (0 == strcmp (cookie_domain, s5r->leho))
944 offset += sprintf (new_cookie_hdr + offset,
949 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
950 _("Cookie domain `%s' supplied by server is invalid\n"),
953 memcpy (new_cookie_hdr + offset, tok, strlen (tok));
954 offset += strlen (tok);
955 new_cookie_hdr[offset++] = ';';
957 hdr_val = new_cookie_hdr;
961 if (0 == strcasecmp (MHD_HTTP_HEADER_LOCATION, hdr_type))
965 GNUNET_asprintf (&leho_host,
966 (HTTPS_PORT != s5r->port)
970 if (0 == strncmp (leho_host,
974 GNUNET_asprintf (&new_location,
976 (HTTPS_PORT != s5r->port)
980 hdr_val + strlen (leho_host));
981 hdr_val = new_location;
983 GNUNET_free (leho_host);
985 /* MHD does not allow certain characters in values, remove those */
986 if (NULL != (tok = strchr (hdr_val, '\n')))
988 if (NULL != (tok = strchr (hdr_val, '\r')))
990 if (NULL != (tok = strchr (hdr_val, '\t')))
992 if (0 != strlen (hdr_val))
994 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
995 "Adding header %s: %s to MHD response\n",
998 GNUNET_break (MHD_YES ==
999 MHD_add_response_header (s5r->response,
1004 GNUNET_free_non_null (new_cookie_hdr);
1005 GNUNET_free_non_null (new_location);
1011 * Handle response payload data from cURL. Copies it into our `io_buf` to make
1012 * it available to MHD.
1014 * @param ptr pointer to the data
1015 * @param size number of blocks of data
1016 * @param nmemb blocksize
1017 * @param ctx our `struct Socks5Request *`
1018 * @return number of bytes handled
1021 curl_download_cb (void *ptr, size_t size, size_t nmemb, void* ctx)
1023 struct Socks5Request *s5r = ctx;
1024 size_t total = size * nmemb;
1026 if ( (SOCKS5_SOCKET_UPLOAD_STARTED == s5r->state) ||
1027 (SOCKS5_SOCKET_UPLOAD_DONE == s5r->state) )
1029 /* we're still not done with the upload, do not yet
1030 start the download, the IO buffer is still full
1031 with upload data. */
1032 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1033 "Pausing CURL download, waiting for UPLOAD to finish\n");
1034 return CURL_WRITEFUNC_PAUSE; /* not yet ready for data download */
1036 if (sizeof (s5r->io_buf) - s5r->io_len < total)
1038 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1039 "Pausing CURL download, not enough space\n");
1040 return CURL_WRITEFUNC_PAUSE; /* not enough space */
1042 memcpy (&s5r->io_buf[s5r->io_len],
1045 s5r->io_len += total;
1046 if (s5r->io_len == total)
1047 run_mhd_now (s5r->hd);
1053 * cURL callback for uploaded (PUT/POST) data. Copies it into our `io_buf`
1054 * to make it available to MHD.
1056 * @param buf where to write the data
1057 * @param size number of bytes per member
1058 * @param nmemb number of members available in @a buf
1059 * @param cls our `struct Socks5Request` that generated the data
1060 * @return number of bytes copied to @a buf
1063 curl_upload_cb (void *buf, size_t size, size_t nmemb, void *cls)
1065 struct Socks5Request *s5r = cls;
1066 size_t len = size * nmemb;
1069 if ( (0 == s5r->io_len) &&
1070 (SOCKS5_SOCKET_UPLOAD_DONE != s5r->state) )
1072 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1073 "Pausing CURL UPLOAD, need more data\n");
1074 return CURL_READFUNC_PAUSE;
1076 if ( (0 == s5r->io_len) &&
1077 (SOCKS5_SOCKET_UPLOAD_DONE == s5r->state) )
1079 s5r->state = SOCKS5_SOCKET_DOWNLOAD_STARTED;
1080 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1081 "Completed CURL UPLOAD\n");
1082 return 0; /* upload finished, can now download */
1084 if ( (SOCKS5_SOCKET_UPLOAD_STARTED != s5r->state) ||
1085 (SOCKS5_SOCKET_UPLOAD_DONE != s5r->state) )
1088 return CURL_READFUNC_ABORT;
1090 to_copy = GNUNET_MIN (s5r->io_len,
1092 memcpy (buf, s5r->io_buf, to_copy);
1093 memmove (s5r->io_buf,
1094 &s5r->io_buf[to_copy],
1095 s5r->io_len - to_copy);
1096 s5r->io_len -= to_copy;
1097 if (s5r->io_len + to_copy == sizeof (s5r->io_buf))
1098 run_mhd_now (s5r->hd); /* got more space for upload now */
1103 /* ************************** main loop of cURL interaction ****************** */
1107 * Task that is run when we are ready to receive more data
1110 * @param cls closure
1111 * @param tc task context
1114 curl_task_download (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
1118 * Ask cURL for the select() sets and schedule cURL operations.
1121 curl_download_prepare ()
1128 struct GNUNET_NETWORK_FDSet *grs;
1129 struct GNUNET_NETWORK_FDSet *gws;
1131 struct GNUNET_TIME_Relative rtime;
1133 if (GNUNET_SCHEDULER_NO_TASK != curl_download_task)
1135 GNUNET_SCHEDULER_cancel (curl_download_task);
1136 curl_download_task = GNUNET_SCHEDULER_NO_TASK;
1142 if (CURLM_OK != (mret = curl_multi_fdset (curl_multi, &rs, &ws, &es, &max)))
1144 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1145 "%s failed at %s:%d: `%s'\n",
1146 "curl_multi_fdset", __FILE__, __LINE__,
1147 curl_multi_strerror (mret));
1151 GNUNET_break (CURLM_OK == curl_multi_timeout (curl_multi, &to));
1153 rtime = GNUNET_TIME_UNIT_FOREVER_REL;
1155 rtime = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, to);
1158 grs = GNUNET_NETWORK_fdset_create ();
1159 gws = GNUNET_NETWORK_fdset_create ();
1160 GNUNET_NETWORK_fdset_copy_native (grs, &rs, max + 1);
1161 GNUNET_NETWORK_fdset_copy_native (gws, &ws, max + 1);
1162 curl_download_task = GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1165 &curl_task_download, curl_multi);
1166 GNUNET_NETWORK_fdset_destroy (gws);
1167 GNUNET_NETWORK_fdset_destroy (grs);
1171 curl_download_task = GNUNET_SCHEDULER_add_delayed (rtime,
1172 &curl_task_download,
1179 * Task that is run when we are ready to receive more data from curl.
1181 * @param cls closure, NULL
1182 * @param tc task context
1185 curl_task_download (void *cls,
1186 const struct GNUNET_SCHEDULER_TaskContext *tc)
1190 struct CURLMsg *msg;
1192 struct Socks5Request *s5r;
1194 curl_download_task = GNUNET_SCHEDULER_NO_TASK;
1198 mret = curl_multi_perform (curl_multi, &running);
1199 while (NULL != (msg = curl_multi_info_read (curl_multi, &msgnum)))
1201 GNUNET_break (CURLE_OK ==
1202 curl_easy_getinfo (msg->easy_handle,
1213 /* documentation says this is not used */
1217 switch (msg->data.result)
1220 case CURLE_GOT_NOTHING:
1221 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1222 "CURL download completed.\n");
1223 s5r->state = SOCKS5_SOCKET_DOWNLOAD_DONE;
1224 run_mhd_now (s5r->hd);
1227 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1228 "Download curl failed: %s\n",
1229 curl_easy_strerror (msg->data.result));
1230 /* FIXME: indicate error somehow? close MHD connection badly as well? */
1231 s5r->state = SOCKS5_SOCKET_DOWNLOAD_DONE;
1232 run_mhd_now (s5r->hd);
1235 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1236 "Cleaning up cURL handle\n");
1237 curl_multi_remove_handle (curl_multi, s5r->curl);
1238 curl_easy_cleanup (s5r->curl);
1240 if (NULL == s5r->response)
1241 cleanup_s5r (s5r); /* curl failed to yield response, close Socks socket as well */
1244 /* documentation says this is not used */
1248 /* unexpected status code */
1253 } while (mret == CURLM_CALL_MULTI_PERFORM);
1254 if (CURLM_OK != mret)
1255 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1256 "%s failed at %s:%d: `%s'\n",
1257 "curl_multi_perform", __FILE__, __LINE__,
1258 curl_multi_strerror (mret));
1261 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1262 "Suspending cURL multi loop, no more events pending\n");
1263 return; /* nothing more in progress */
1265 curl_download_prepare();
1269 /* ********************************* MHD response generation ******************* */
1273 * Read HTTP request header field from the request. Copies the fields
1274 * over to the 'headers' that will be given to curl. However, 'Host'
1275 * is substituted with the LEHO if present. We also change the
1276 * 'Connection' header value to "close" as the proxy does not support
1279 * @param cls our `struct Socks5Request`
1280 * @param kind value kind
1281 * @param key field key
1282 * @param value field value
1283 * @return #MHD_YES to continue to iterate
1286 con_val_iter (void *cls,
1287 enum MHD_ValueKind kind,
1291 struct Socks5Request *s5r = cls;
1294 if ( (0 == strcasecmp (MHD_HTTP_HEADER_HOST, key)) &&
1295 (NULL != s5r->leho) )
1297 if (0 == strcasecmp (MHD_HTTP_HEADER_CONNECTION, key))
1299 GNUNET_asprintf (&hdr,
1303 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1304 "Adding HEADER `%s' to HTTP request\n",
1306 s5r->headers = curl_slist_append (s5r->headers,
1314 * Main MHD callback for handling requests.
1317 * @param con MHD connection handle
1318 * @param url the url in the request
1319 * @param meth the HTTP method used ("GET", "PUT", etc.)
1320 * @param ver the HTTP version string (i.e. "HTTP/1.1")
1321 * @param upload_data the data being uploaded (excluding HEADERS,
1322 * for a POST that fits into memory and that is encoded
1323 * with a supported encoding, the POST data will NOT be
1324 * given in upload_data and is instead available as
1325 * part of MHD_get_connection_values; very large POST
1326 * data *will* be made available incrementally in
1328 * @param upload_data_size set initially to the size of the
1329 * @a upload_data provided; the method must update this
1330 * value to the number of bytes NOT processed;
1331 * @param con_cls pointer to location where we store the 'struct Request'
1332 * @return #MHD_YES if the connection was handled successfully,
1333 * #MHD_NO if the socket must be closed due to a serious
1334 * error while handling the request
1337 create_response (void *cls,
1338 struct MHD_Connection *con,
1342 const char *upload_data,
1343 size_t *upload_data_size,
1346 /* struct MhdHttpList* hd = cls; */
1347 struct Socks5Request *s5r = *con_cls;
1349 char ipstring[INET6_ADDRSTRLEN];
1350 char ipaddr[INET6_ADDRSTRLEN + 2];
1351 const struct sockaddr *sa;
1352 const struct sockaddr_in *s4;
1353 const struct sockaddr_in6 *s6;
1362 if ( (NULL == s5r->curl) &&
1363 (SOCKS5_SOCKET_WITH_MHD == s5r->state) )
1365 /* first time here, initialize curl handle */
1366 sa = (const struct sockaddr *) &s5r->destination_address;
1367 switch (sa->sa_family)
1370 s4 = (const struct sockaddr_in *) &s5r->destination_address;
1371 if (NULL == inet_ntop (AF_INET,
1379 GNUNET_snprintf (ipaddr,
1383 port = ntohs (s4->sin_port);
1386 s6 = (const struct sockaddr_in6 *) &s5r->destination_address;
1387 if (NULL == inet_ntop (AF_INET6,
1395 GNUNET_snprintf (ipaddr,
1399 port = ntohs (s6->sin6_port);
1405 s5r->curl = curl_easy_init ();
1406 if (NULL == s5r->curl)
1407 return MHD_queue_response (con,
1408 MHD_HTTP_INTERNAL_SERVER_ERROR,
1409 curl_failure_response);
1410 curl_easy_setopt (s5r->curl, CURLOPT_HEADERFUNCTION, &curl_check_hdr);
1411 curl_easy_setopt (s5r->curl, CURLOPT_HEADERDATA, s5r);
1412 curl_easy_setopt (s5r->curl, CURLOPT_FOLLOWLOCATION, 0);
1413 curl_easy_setopt (s5r->curl, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
1414 curl_easy_setopt (s5r->curl, CURLOPT_FAILONERROR, 1); /* not sure we want this */
1415 curl_easy_setopt (s5r->curl, CURLOPT_CONNECTTIMEOUT, 600L);
1416 curl_easy_setopt (s5r->curl, CURLOPT_TIMEOUT, 600L);
1417 curl_easy_setopt (s5r->curl, CURLOPT_NOSIGNAL, 1L);
1418 curl_easy_setopt (s5r->curl, CURLOPT_HTTP_CONTENT_DECODING, 0);
1419 curl_easy_setopt (s5r->curl, CURLOPT_HTTP_TRANSFER_DECODING, 0);
1420 curl_easy_setopt (s5r->curl, CURLOPT_NOSIGNAL, 1L);
1421 curl_easy_setopt (s5r->curl, CURLOPT_PRIVATE, s5r);
1422 curl_easy_setopt (s5r->curl, CURLOPT_VERBOSE, 0); // FIXME: remove later
1423 GNUNET_asprintf (&curlurl,
1424 (HTTPS_PORT != s5r->port)
1426 : "https://%s:%d%s",
1430 curl_easy_setopt (s5r->curl,
1433 GNUNET_free (curlurl);
1435 if (0 == strcasecmp (meth, MHD_HTTP_METHOD_PUT))
1437 s5r->state = SOCKS5_SOCKET_UPLOAD_STARTED;
1438 curl_easy_setopt (s5r->curl, CURLOPT_UPLOAD, 1);
1439 curl_easy_setopt (s5r->curl, CURLOPT_WRITEFUNCTION, &curl_download_cb);
1440 curl_easy_setopt (s5r->curl, CURLOPT_WRITEDATA, s5r);
1441 curl_easy_setopt (s5r->curl, CURLOPT_READFUNCTION, &curl_upload_cb);
1442 curl_easy_setopt (s5r->curl, CURLOPT_READDATA, s5r);
1444 else if (0 == strcasecmp (meth, MHD_HTTP_METHOD_POST))
1446 s5r->state = SOCKS5_SOCKET_UPLOAD_STARTED;
1447 curl_easy_setopt (s5r->curl, CURLOPT_POST, 1);
1448 curl_easy_setopt (s5r->curl, CURLOPT_WRITEFUNCTION, &curl_download_cb);
1449 curl_easy_setopt (s5r->curl, CURLOPT_WRITEDATA, s5r);
1450 curl_easy_setopt (s5r->curl, CURLOPT_READFUNCTION, &curl_upload_cb);
1451 curl_easy_setopt (s5r->curl, CURLOPT_READDATA, s5r);
1453 else if (0 == strcasecmp (meth, MHD_HTTP_METHOD_HEAD))
1455 s5r->state = SOCKS5_SOCKET_DOWNLOAD_STARTED;
1456 curl_easy_setopt (s5r->curl, CURLOPT_NOBODY, 1);
1458 else if (0 == strcasecmp (meth, MHD_HTTP_METHOD_GET))
1460 s5r->state = SOCKS5_SOCKET_DOWNLOAD_STARTED;
1461 curl_easy_setopt (s5r->curl, CURLOPT_HTTPGET, 1);
1462 curl_easy_setopt (s5r->curl, CURLOPT_WRITEFUNCTION, &curl_download_cb);
1463 curl_easy_setopt (s5r->curl, CURLOPT_WRITEDATA, s5r);
1467 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1468 _("Unsupported HTTP method `%s'\n"),
1470 curl_easy_cleanup (s5r->curl);
1475 if (0 == strcasecmp (ver, MHD_HTTP_VERSION_1_0))
1477 curl_easy_setopt (s5r->curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
1479 else if (0 == strcasecmp (ver, MHD_HTTP_VERSION_1_1))
1481 curl_easy_setopt (s5r->curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
1485 curl_easy_setopt (s5r->curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_NONE);
1488 if (HTTPS_PORT == s5r->port)
1490 curl_easy_setopt (s5r->curl, CURLOPT_USE_SSL, CURLUSESSL_ALL);
1491 curl_easy_setopt (s5r->curl, CURLOPT_SSL_VERIFYPEER, 1L);
1492 /* Disable cURL checking the hostname, as we will check ourselves
1493 as only we have the domain name or the LEHO or the DANE record */
1494 curl_easy_setopt (s5r->curl, CURLOPT_SSL_VERIFYHOST, 0L);
1498 curl_easy_setopt (s5r->curl, CURLOPT_USE_SSL, CURLUSESSL_NONE);
1501 if (CURLM_OK != curl_multi_add_handle (curl_multi, s5r->curl))
1504 curl_easy_cleanup (s5r->curl);
1508 MHD_get_connection_values (con,
1510 &con_val_iter, s5r);
1511 curl_easy_setopt (s5r->curl, CURLOPT_HTTPHEADER, s5r->headers);
1512 curl_download_prepare ();
1516 /* continuing to process request */
1517 if (0 != *upload_data_size)
1519 left = GNUNET_MIN (*upload_data_size,
1520 sizeof (s5r->io_buf) - s5r->io_len);
1521 memcpy (&s5r->io_buf[s5r->io_len],
1524 s5r->io_len += left;
1525 *upload_data_size -= left;
1526 GNUNET_assert (NULL != s5r->curl);
1527 curl_easy_pause (s5r->curl, CURLPAUSE_CONT);
1528 curl_download_prepare ();
1531 if (SOCKS5_SOCKET_UPLOAD_STARTED == s5r->state)
1533 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1534 "Finished processing UPLOAD\n");
1535 s5r->state = SOCKS5_SOCKET_UPLOAD_DONE;
1537 if (NULL == s5r->response)
1538 return MHD_YES; /* too early to queue response, did not yet get headers from cURL */
1539 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1540 "Queueing response with MHD\n");
1541 return MHD_queue_response (con,
1547 /* ******************** MHD HTTP setup and event loop ******************** */
1551 * Function called when MHD decides that we are done with a connection.
1554 * @param connection connection handle
1555 * @param con_cls value as set by the last call to
1556 * the #MHD_AccessHandlerCallback, should be our `struct Socks5Request`
1557 * @param toe reason for request termination (ignored)
1560 mhd_completed_cb (void *cls,
1561 struct MHD_Connection *connection,
1563 enum MHD_RequestTerminationCode toe)
1565 struct Socks5Request *s5r = *con_cls;
1569 if (MHD_REQUEST_TERMINATED_COMPLETED_OK != toe)
1570 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1571 "MHD encountered error handling request: %d\n",
1579 * Function called when MHD first processes an incoming connection.
1580 * Gives us the respective URI information.
1582 * We use this to associate the `struct MHD_Connection` with our
1583 * internal `struct Socks5Request` data structure (by checking
1584 * for matching sockets).
1586 * @param cls the HTTP server handle (a `struct MhdHttpList`)
1587 * @param url the URL that is being requested
1588 * @param connection MHD connection object for the request
1589 * @return the `struct Socks5Request` that this @a connection is for
1592 mhd_log_callback (void *cls,
1594 struct MHD_Connection *connection)
1596 struct Socks5Request *s5r;
1597 const union MHD_ConnectionInfo *ci;
1600 ci = MHD_get_connection_info (connection,
1601 MHD_CONNECTION_INFO_CONNECTION_FD);
1607 sock = ci->connect_fd;
1608 for (s5r = s5r_head; NULL != s5r; s5r = s5r->next)
1610 if (GNUNET_NETWORK_get_fd (s5r->sock) == sock)
1612 if (NULL != s5r->url)
1617 s5r->url = GNUNET_strdup (url);
1626 * Kill the given MHD daemon.
1628 * @param hd daemon to stop
1631 kill_httpd (struct MhdHttpList *hd)
1633 GNUNET_CONTAINER_DLL_remove (mhd_httpd_head,
1636 GNUNET_free_non_null (hd->domain);
1637 MHD_stop_daemon (hd->daemon);
1638 if (GNUNET_SCHEDULER_NO_TASK != hd->httpd_task)
1639 GNUNET_SCHEDULER_cancel (hd->httpd_task);
1640 GNUNET_free_non_null (hd->proxy_cert);
1648 * Task run whenever HTTP server is idle for too long. Kill it.
1650 * @param cls the `struct MhdHttpList *`
1651 * @param tc sched context
1654 kill_httpd_task (void *cls,
1655 const struct GNUNET_SCHEDULER_TaskContext *tc)
1657 struct MhdHttpList *hd = cls;
1664 * Task run whenever HTTP server operations are pending.
1666 * @param cls the `struct MhdHttpList *` of the daemon that is being run
1667 * @param tc sched context
1670 do_httpd (void *cls,
1671 const struct GNUNET_SCHEDULER_TaskContext *tc);
1675 * Schedule MHD. This function should be called initially when an
1676 * MHD is first getting its client socket, and will then automatically
1677 * always be called later whenever there is work to be done.
1679 * @param hd the daemon to schedule
1682 schedule_httpd (struct MhdHttpList *hd)
1687 struct GNUNET_NETWORK_FDSet *wrs;
1688 struct GNUNET_NETWORK_FDSet *wws;
1691 MHD_UNSIGNED_LONG_LONG timeout;
1692 struct GNUNET_TIME_Relative tv;
1698 if (MHD_YES != MHD_get_fdset (hd->daemon, &rs, &ws, &es, &max))
1703 haveto = MHD_get_timeout (hd->daemon, &timeout);
1704 if (MHD_YES == haveto)
1705 tv.rel_value_us = (uint64_t) timeout * 1000LL;
1707 tv = GNUNET_TIME_UNIT_FOREVER_REL;
1710 wrs = GNUNET_NETWORK_fdset_create ();
1711 wws = GNUNET_NETWORK_fdset_create ();
1712 GNUNET_NETWORK_fdset_copy_native (wrs, &rs, max + 1);
1713 GNUNET_NETWORK_fdset_copy_native (wws, &ws, max + 1);
1720 if (GNUNET_SCHEDULER_NO_TASK != hd->httpd_task)
1721 GNUNET_SCHEDULER_cancel (hd->httpd_task);
1722 if ( (MHD_YES != haveto) &&
1726 /* daemon is idle, kill after timeout */
1727 hd->httpd_task = GNUNET_SCHEDULER_add_delayed (MHD_CACHE_TIMEOUT,
1734 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1739 GNUNET_NETWORK_fdset_destroy (wrs);
1741 GNUNET_NETWORK_fdset_destroy (wws);
1746 * Task run whenever HTTP server operations are pending.
1748 * @param cls the `struct MhdHttpList` of the daemon that is being run
1749 * @param tc scheduler context
1752 do_httpd (void *cls,
1753 const struct GNUNET_SCHEDULER_TaskContext *tc)
1755 struct MhdHttpList *hd = cls;
1757 hd->httpd_task = GNUNET_SCHEDULER_NO_TASK;
1758 MHD_run (hd->daemon);
1759 schedule_httpd (hd);
1764 * Run MHD now, we have extra data ready for the callback.
1766 * @param hd the daemon to run now.
1769 run_mhd_now (struct MhdHttpList *hd)
1771 if (GNUNET_SCHEDULER_NO_TASK !=
1773 GNUNET_SCHEDULER_cancel (hd->httpd_task);
1774 hd->httpd_task = GNUNET_SCHEDULER_add_now (&do_httpd,
1780 * Read file in filename
1782 * @param filename file to read
1783 * @param size pointer where filesize is stored
1784 * @return NULL on error
1787 load_file (const char* filename,
1794 GNUNET_DISK_file_size (filename, &fsize,
1795 GNUNET_YES, GNUNET_YES))
1797 if (fsize > MAX_PEM_SIZE)
1799 *size = (unsigned int) fsize;
1800 buffer = GNUNET_malloc (*size);
1801 if (fsize != GNUNET_DISK_fn_read (filename, buffer, (size_t) fsize))
1803 GNUNET_free (buffer);
1811 * Load PEM key from file
1813 * @param key where to store the data
1814 * @param keyfile path to the PEM file
1815 * @return #GNUNET_OK on success
1818 load_key_from_file (gnutls_x509_privkey_t key,
1819 const char* keyfile)
1821 gnutls_datum_t key_data;
1824 key_data.data = load_file (keyfile, &key_data.size);
1825 ret = gnutls_x509_privkey_import (key, &key_data,
1826 GNUTLS_X509_FMT_PEM);
1827 if (GNUTLS_E_SUCCESS != ret)
1829 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1830 _("Unable to import private key from file `%s'\n"),
1833 GNUNET_free_non_null (key_data.data);
1834 return (GNUTLS_E_SUCCESS != ret) ? GNUNET_SYSERR : GNUNET_OK;
1839 * Load cert from file
1841 * @param crt struct to store data in
1842 * @param certfile path to pem file
1843 * @return #GNUNET_OK on success
1846 load_cert_from_file (gnutls_x509_crt_t crt,
1847 const char* certfile)
1849 gnutls_datum_t cert_data;
1852 cert_data.data = load_file (certfile, &cert_data.size);
1853 ret = gnutls_x509_crt_import (crt, &cert_data,
1854 GNUTLS_X509_FMT_PEM);
1855 if (GNUTLS_E_SUCCESS != ret)
1857 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1858 _("Unable to import certificate %s\n"), certfile);
1860 GNUNET_free_non_null (cert_data.data);
1861 return (GNUTLS_E_SUCCESS != ret) ? GNUNET_SYSERR : GNUNET_OK;
1866 * Generate new certificate for specific name
1868 * @param name the subject name to generate a cert for
1869 * @return a struct holding the PEM data, NULL on error
1871 static struct ProxyGNSCertificate *
1872 generate_gns_certificate (const char *name)
1874 unsigned int serial;
1875 size_t key_buf_size;
1876 size_t cert_buf_size;
1877 gnutls_x509_crt_t request;
1880 struct ProxyGNSCertificate *pgc;
1882 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1883 "Generating TLS/SSL certificate for `%s'\n",
1885 GNUNET_break (GNUTLS_E_SUCCESS == gnutls_x509_crt_init (&request));
1886 GNUNET_break (GNUTLS_E_SUCCESS == gnutls_x509_crt_set_key (request, proxy_ca.key));
1887 pgc = GNUNET_new (struct ProxyGNSCertificate);
1888 gnutls_x509_crt_set_dn_by_oid (request, GNUTLS_OID_X520_COUNTRY_NAME,
1890 gnutls_x509_crt_set_dn_by_oid (request, GNUTLS_OID_X520_ORGANIZATION_NAME,
1891 0, "GNU Name System", 4);
1892 gnutls_x509_crt_set_dn_by_oid (request, GNUTLS_OID_X520_COMMON_NAME,
1893 0, name, strlen (name));
1894 GNUNET_break (GNUTLS_E_SUCCESS == gnutls_x509_crt_set_version (request, 3));
1895 gnutls_rnd (GNUTLS_RND_NONCE, &serial, sizeof (serial));
1896 gnutls_x509_crt_set_serial (request,
1899 etime = time (NULL);
1900 tm_data = localtime (&etime);
1901 gnutls_x509_crt_set_activation_time (request,
1904 etime = mktime (tm_data);
1905 gnutls_x509_crt_set_expiration_time (request,
1907 gnutls_x509_crt_sign (request,
1910 key_buf_size = sizeof (pgc->key);
1911 cert_buf_size = sizeof (pgc->cert);
1912 gnutls_x509_crt_export (request, GNUTLS_X509_FMT_PEM,
1913 pgc->cert, &cert_buf_size);
1914 gnutls_x509_privkey_export (proxy_ca.key, GNUTLS_X509_FMT_PEM,
1915 pgc->key, &key_buf_size);
1916 gnutls_x509_crt_deinit (request);
1922 * Lookup (or create) an SSL MHD instance for a particular domain.
1924 * @param domain the domain the SSL daemon has to serve
1925 * @return NULL on errro
1927 static struct MhdHttpList *
1928 lookup_ssl_httpd (const char* domain)
1930 struct MhdHttpList *hd;
1931 struct ProxyGNSCertificate *pgc;
1933 for (hd = mhd_httpd_head; NULL != hd; hd = hd->next)
1934 if ( (NULL != hd->domain) &&
1935 (0 == strcmp (hd->domain, domain)) )
1937 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1938 "Starting fresh MHD HTTPS instance for domain `%s'\n",
1940 pgc = generate_gns_certificate (domain);
1941 hd = GNUNET_new (struct MhdHttpList);
1942 hd->is_ssl = GNUNET_YES;
1943 hd->domain = GNUNET_strdup (domain);
1944 hd->proxy_cert = pgc;
1945 hd->daemon = MHD_start_daemon (MHD_USE_DEBUG | MHD_USE_SSL | MHD_USE_NO_LISTEN_SOCKET,
1948 &create_response, hd,
1949 MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) 16,
1950 MHD_OPTION_NOTIFY_COMPLETED, &mhd_completed_cb, NULL,
1951 MHD_OPTION_URI_LOG_CALLBACK, &mhd_log_callback, NULL,
1952 MHD_OPTION_HTTPS_MEM_KEY, pgc->key,
1953 MHD_OPTION_HTTPS_MEM_CERT, pgc->cert,
1955 if (NULL == hd->daemon)
1961 GNUNET_CONTAINER_DLL_insert (mhd_httpd_head,
1969 * Task run when a Socks5Request somehow fails to be associated with
1970 * an MHD connection (i.e. because the client never speaks HTTP after
1971 * the SOCKS5 handshake). Clean up.
1973 * @param cls the `struct Socks5Request *`
1974 * @param tc sched context
1977 timeout_s5r_handshake (void *cls,
1978 const struct GNUNET_SCHEDULER_TaskContext *tc)
1980 struct Socks5Request *s5r = cls;
1982 s5r->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1988 * We're done with the Socks5 protocol, now we need to pass the
1989 * connection data through to the final destination, either
1990 * direct (if the protocol might not be HTTP), or via MHD
1991 * (if the port looks like it should be HTTP).
1993 * @param s5r socks request that has reached the final stage
1996 setup_data_transfer (struct Socks5Request *s5r)
1998 struct MhdHttpList *hd;
2000 const struct sockaddr *addr;
2006 hd = lookup_ssl_httpd (s5r->domain);
2009 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2010 _("Failed to start HTTPS server for `%s'\n"),
2018 GNUNET_assert (NULL != httpd);
2022 fd = GNUNET_NETWORK_get_fd (s5r->sock);
2023 addr = GNUNET_NETWORK_get_addr (s5r->sock);
2024 len = GNUNET_NETWORK_get_addrlen (s5r->sock);
2025 s5r->state = SOCKS5_SOCKET_WITH_MHD;
2026 if (MHD_YES != MHD_add_connection (hd->daemon, fd, addr, len))
2028 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2029 _("Failed to pass client to MHD\n"));
2034 schedule_httpd (hd);
2035 s5r->timeout_task = GNUNET_SCHEDULER_add_delayed (HTTP_HANDSHAKE_TIMEOUT,
2036 &timeout_s5r_handshake,
2041 /* ********************* SOCKS handling ************************* */
2045 * Write data from buffer to socks5 client, then continue with state machine.
2047 * @param cls the closure with the `struct Socks5Request`
2048 * @param tc scheduler context
2051 do_write (void *cls,
2052 const struct GNUNET_SCHEDULER_TaskContext *tc)
2054 struct Socks5Request *s5r = cls;
2057 s5r->wtask = GNUNET_SCHEDULER_NO_TASK;
2058 len = GNUNET_NETWORK_socket_send (s5r->sock,
2063 /* write error: connection closed, shutdown, etc.; just clean up */
2069 s5r->wbuf_len - len);
2070 s5r->wbuf_len -= len;
2071 if (s5r->wbuf_len > 0)
2073 /* not done writing */
2075 GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
2081 /* we're done writing, continue with state machine! */
2088 case SOCKS5_REQUEST:
2089 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK != s5r->rtask);
2091 case SOCKS5_DATA_TRANSFER:
2092 setup_data_transfer (s5r);
2094 case SOCKS5_WRITE_THEN_CLEANUP:
2105 * Return a server response message indicating a failure to the client.
2107 * @param s5r request to return failure code for
2108 * @param sc status code to return
2111 signal_socks_failure (struct Socks5Request *s5r,
2112 enum Socks5StatusCode sc)
2114 struct Socks5ServerResponseMessage *s_resp;
2116 s_resp = (struct Socks5ServerResponseMessage *) &s5r->wbuf[s5r->wbuf_len];
2117 memset (s_resp, 0, sizeof (struct Socks5ServerResponseMessage));
2118 s_resp->version = SOCKS_VERSION_5;
2120 s5r->state = SOCKS5_WRITE_THEN_CLEANUP;
2121 if (GNUNET_SCHEDULER_NO_TASK != s5r->wtask)
2123 GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
2130 * Return a server response message indicating success.
2132 * @param s5r request to return success status message for
2135 signal_socks_success (struct Socks5Request *s5r)
2137 struct Socks5ServerResponseMessage *s_resp;
2139 s_resp = (struct Socks5ServerResponseMessage *) &s5r->wbuf[s5r->wbuf_len];
2140 s_resp->version = SOCKS_VERSION_5;
2141 s_resp->reply = SOCKS5_STATUS_REQUEST_GRANTED;
2142 s_resp->reserved = 0;
2143 s_resp->addr_type = SOCKS5_AT_IPV4;
2144 /* zero out IPv4 address and port */
2147 sizeof (struct in_addr) + sizeof (uint16_t));
2148 s5r->wbuf_len += sizeof (struct Socks5ServerResponseMessage) +
2149 sizeof (struct in_addr) + sizeof (uint16_t);
2150 if (GNUNET_SCHEDULER_NO_TASK == s5r->wtask)
2152 GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
2159 * Process GNS results for target domain.
2161 * @param cls the `struct Socks5Request`
2162 * @param rd_count number of records returned
2163 * @param rd record data
2166 handle_gns_result (void *cls,
2168 const struct GNUNET_NAMESTORE_RecordData *rd)
2170 struct Socks5Request *s5r = cls;
2172 const struct GNUNET_NAMESTORE_RecordData *r;
2175 s5r->gns_lookup = NULL;
2177 for (i=0;i<rd_count;i++)
2180 switch (r->record_type)
2182 case GNUNET_DNSPARSER_TYPE_A:
2184 struct sockaddr_in *in;
2186 if (sizeof (struct in_addr) != r->data_size)
2188 GNUNET_break_op (0);
2191 if (GNUNET_YES == got_ip)
2194 GNUNET_NETWORK_test_pf (PF_INET))
2196 got_ip = GNUNET_YES;
2197 in = (struct sockaddr_in *) &s5r->destination_address;
2198 in->sin_family = AF_INET;
2199 memcpy (&in->sin_addr,
2202 in->sin_port = htons (s5r->port);
2203 #if HAVE_SOCKADDR_IN_SIN_LEN
2204 in->sin_len = sizeof (*in);
2208 case GNUNET_DNSPARSER_TYPE_AAAA:
2210 struct sockaddr_in6 *in;
2212 if (sizeof (struct in6_addr) != r->data_size)
2214 GNUNET_break_op (0);
2217 if (GNUNET_YES == got_ip)
2220 GNUNET_NETWORK_test_pf (PF_INET))
2222 /* FIXME: allow user to disable IPv6 per configuration option... */
2223 got_ip = GNUNET_YES;
2224 in = (struct sockaddr_in6 *) &s5r->destination_address;
2225 in->sin6_family = AF_INET6;
2226 memcpy (&in->sin6_addr,
2229 in->sin6_port = htons (s5r->port);
2230 #if HAVE_SOCKADDR_IN_SIN_LEN
2231 in->sin6_len = sizeof (*in);
2235 case GNUNET_NAMESTORE_TYPE_VPN:
2236 GNUNET_break (0); /* should have been translated within GNS */
2238 case GNUNET_NAMESTORE_TYPE_LEHO:
2239 GNUNET_free_non_null (s5r->leho);
2240 s5r->leho = GNUNET_strndup (r->data,
2248 if (GNUNET_YES != got_ip)
2250 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2251 "Name resolution failed to yield useful IP address.\n");
2252 signal_socks_failure (s5r,
2253 SOCKS5_STATUS_GENERAL_FAILURE);
2256 s5r->state = SOCKS5_DATA_TRANSFER;
2257 signal_socks_success (s5r);
2262 * Remove the first @a len bytes from the beginning of the read buffer.
2264 * @param s5r the handle clear the read buffer for
2265 * @param len number of bytes in read buffer to advance
2268 clear_from_s5r_rbuf (struct Socks5Request *s5r,
2271 GNUNET_assert (len <= s5r->rbuf_len);
2274 s5r->rbuf_len - len);
2275 s5r->rbuf_len -= len;
2280 * Read data from incoming Socks5 connection
2282 * @param cls the closure with the `struct Socks5Request`
2283 * @param tc the scheduler context
2286 do_s5r_read (void *cls,
2287 const struct GNUNET_SCHEDULER_TaskContext *tc)
2289 struct Socks5Request *s5r = cls;
2290 const struct Socks5ClientHelloMessage *c_hello;
2291 struct Socks5ServerHelloMessage *s_hello;
2292 const struct Socks5ClientRequestMessage *c_req;
2296 s5r->rtask = GNUNET_SCHEDULER_NO_TASK;
2297 if ( (NULL != tc->read_ready) &&
2298 (GNUNET_NETWORK_fdset_isset (tc->read_ready, s5r->sock)) )
2300 rlen = GNUNET_NETWORK_socket_recv (s5r->sock,
2301 &s5r->rbuf[s5r->rbuf_len],
2302 sizeof (s5r->rbuf) - s5r->rbuf_len);
2305 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2306 "socks5 client disconnected.\n");
2310 s5r->rbuf_len += rlen;
2312 s5r->rtask = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
2315 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2316 "Processing %u bytes of socks data in state %d\n",
2322 c_hello = (const struct Socks5ClientHelloMessage*) &s5r->rbuf;
2323 if ( (s5r->rbuf_len < sizeof (struct Socks5ClientHelloMessage)) ||
2324 (s5r->rbuf_len < sizeof (struct Socks5ClientHelloMessage) + c_hello->num_auth_methods) )
2325 return; /* need more data */
2326 if (SOCKS_VERSION_5 != c_hello->version)
2328 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2329 _("Unsupported socks version %d\n"),
2330 (int) c_hello->version);
2334 clear_from_s5r_rbuf (s5r,
2335 sizeof (struct Socks5ClientHelloMessage) + c_hello->num_auth_methods);
2336 GNUNET_assert (0 == s5r->wbuf_len);
2337 s_hello = (struct Socks5ServerHelloMessage *) &s5r->wbuf;
2338 s5r->wbuf_len = sizeof (struct Socks5ServerHelloMessage);
2339 s_hello->version = SOCKS_VERSION_5;
2340 s_hello->auth_method = SOCKS_AUTH_NONE;
2341 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == s5r->wtask);
2342 s5r->wtask = GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
2345 s5r->state = SOCKS5_REQUEST;
2347 case SOCKS5_REQUEST:
2348 c_req = (const struct Socks5ClientRequestMessage *) &s5r->rbuf;
2349 if (s5r->rbuf_len < sizeof (struct Socks5ClientRequestMessage))
2351 switch (c_req->command)
2353 case SOCKS5_CMD_TCP_STREAM:
2357 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2358 _("Unsupported socks command %d\n"),
2359 (int) c_req->command);
2360 signal_socks_failure (s5r,
2361 SOCKS5_STATUS_COMMAND_NOT_SUPPORTED);
2364 switch (c_req->addr_type)
2366 case SOCKS5_AT_IPV4:
2368 const struct in_addr *v4 = (const struct in_addr *) &c_req[1];
2369 const uint16_t *port = (const uint16_t *) &v4[1];
2370 struct sockaddr_in *in;
2372 s5r->port = ntohs (*port);
2373 alen = sizeof (struct in_addr);
2374 if (s5r->rbuf_len < sizeof (struct Socks5ClientRequestMessage) +
2375 alen + sizeof (uint16_t))
2376 return; /* need more data */
2377 in = (struct sockaddr_in *) &s5r->destination_address;
2378 in->sin_family = AF_INET;
2380 in->sin_port = *port;
2381 #if HAVE_SOCKADDR_IN_SIN_LEN
2382 in->sin_len = sizeof (*in);
2384 s5r->state = SOCKS5_DATA_TRANSFER;
2387 case SOCKS5_AT_IPV6:
2389 const struct in6_addr *v6 = (const struct in6_addr *) &c_req[1];
2390 const uint16_t *port = (const uint16_t *) &v6[1];
2391 struct sockaddr_in6 *in;
2393 s5r->port = ntohs (*port);
2394 alen = sizeof (struct in6_addr);
2395 if (s5r->rbuf_len < sizeof (struct Socks5ClientRequestMessage) +
2396 alen + sizeof (uint16_t))
2397 return; /* need more data */
2398 in = (struct sockaddr_in6 *) &s5r->destination_address;
2399 in->sin6_family = AF_INET6;
2400 in->sin6_addr = *v6;
2401 in->sin6_port = *port;
2402 #if HAVE_SOCKADDR_IN_SIN_LEN
2403 in->sin6_len = sizeof (*in);
2405 s5r->state = SOCKS5_DATA_TRANSFER;
2408 case SOCKS5_AT_DOMAINNAME:
2410 const uint8_t *dom_len;
2411 const char *dom_name;
2412 const uint16_t *port;
2414 dom_len = (const uint8_t *) &c_req[1];
2415 alen = *dom_len + 1;
2416 if (s5r->rbuf_len < sizeof (struct Socks5ClientRequestMessage) +
2417 alen + sizeof (uint16_t))
2418 return; /* need more data */
2419 dom_name = (const char *) &dom_len[1];
2420 port = (const uint16_t*) &dom_name[*dom_len];
2421 s5r->domain = GNUNET_strndup (dom_name, *dom_len);
2422 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2423 "Requested connection is to %s:%d\n",
2426 s5r->state = SOCKS5_RESOLVING;
2427 s5r->port = ntohs (*port);
2428 s5r->gns_lookup = GNUNET_GNS_lookup (gns_handle,
2431 GNUNET_DNSPARSER_TYPE_A,
2432 GNUNET_NO /* only cached */,
2433 (GNUNET_YES == do_shorten) ? &local_shorten_zone : NULL,
2439 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2440 _("Unsupported socks address type %d\n"),
2441 (int) c_req->addr_type);
2442 signal_socks_failure (s5r,
2443 SOCKS5_STATUS_ADDRESS_TYPE_NOT_SUPPORTED);
2446 clear_from_s5r_rbuf (s5r,
2447 sizeof (struct Socks5ClientRequestMessage) +
2448 alen + sizeof (uint16_t));
2449 if (0 != s5r->rbuf_len)
2451 /* read more bytes than healthy, why did the client send more!? */
2452 GNUNET_break_op (0);
2453 signal_socks_failure (s5r,
2454 SOCKS5_STATUS_GENERAL_FAILURE);
2457 if (SOCKS5_DATA_TRANSFER == s5r->state)
2459 /* if we are not waiting for GNS resolution, signal success */
2460 signal_socks_success (s5r);
2462 /* We are done reading right now */
2463 GNUNET_SCHEDULER_cancel (s5r->rtask);
2464 s5r->rtask = GNUNET_SCHEDULER_NO_TASK;
2466 case SOCKS5_RESOLVING:
2469 case SOCKS5_DATA_TRANSFER:
2480 * Accept new incoming connections
2482 * @param cls the closure
2483 * @param tc the scheduler context
2486 do_accept (void *cls,
2487 const struct GNUNET_SCHEDULER_TaskContext *tc)
2489 struct GNUNET_NETWORK_Handle *s;
2490 struct Socks5Request *s5r;
2492 ltask = GNUNET_SCHEDULER_NO_TASK;
2493 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
2495 ltask = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
2498 s = GNUNET_NETWORK_socket_accept (lsock, NULL, NULL);
2501 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "accept");
2504 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2505 "Got an inbound connection, waiting for data\n");
2506 s5r = GNUNET_new (struct Socks5Request);
2507 GNUNET_CONTAINER_DLL_insert (s5r_head,
2511 s5r->state = SOCKS5_INIT;
2512 s5r->rtask = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
2518 /* ******************* General / main code ********************* */
2522 * Task run on shutdown
2524 * @param cls closure
2525 * @param tc task context
2528 do_shutdown (void *cls,
2529 const struct GNUNET_SCHEDULER_TaskContext *tc)
2531 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2532 "Shutting down...\n");
2533 while (NULL != mhd_httpd_head)
2534 kill_httpd (mhd_httpd_head);
2535 while (NULL != s5r_head)
2536 cleanup_s5r (s5r_head);
2539 GNUNET_NETWORK_socket_close (lsock);
2544 GNUNET_IDENTITY_cancel (id_op);
2547 if (NULL != identity)
2549 GNUNET_IDENTITY_disconnect (identity);
2552 if (NULL != curl_multi)
2554 curl_multi_cleanup (curl_multi);
2557 if (NULL != gns_handle)
2559 GNUNET_GNS_disconnect (gns_handle);
2562 if (GNUNET_SCHEDULER_NO_TASK != curl_download_task)
2564 GNUNET_SCHEDULER_cancel (curl_download_task);
2565 curl_download_task = GNUNET_SCHEDULER_NO_TASK;
2567 if (GNUNET_SCHEDULER_NO_TASK != ltask)
2569 GNUNET_SCHEDULER_cancel (ltask);
2570 ltask = GNUNET_SCHEDULER_NO_TASK;
2572 gnutls_x509_crt_deinit (proxy_ca.cert);
2573 gnutls_x509_privkey_deinit (proxy_ca.key);
2574 gnutls_global_deinit ();
2579 * Continue initialization after we have our zone information.
2584 struct MhdHttpList *hd;
2585 struct sockaddr_in sa;
2587 /* Open listen socket for socks proxy */
2588 /* FIXME: support IPv6! */
2589 memset (&sa, 0, sizeof (sa));
2590 sa.sin_family = AF_INET;
2591 sa.sin_port = htons (port);
2592 #if HAVE_SOCKADDR_IN_SIN_LEN
2593 sa.sin_len = sizeof (sa);
2595 lsock = GNUNET_NETWORK_socket_create (AF_INET,
2600 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "socket");
2601 GNUNET_SCHEDULER_shutdown ();
2605 GNUNET_NETWORK_socket_bind (lsock, (const struct sockaddr *) &sa,
2608 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "bind");
2609 GNUNET_SCHEDULER_shutdown ();
2612 if (GNUNET_OK != GNUNET_NETWORK_socket_listen (lsock, 5))
2614 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "listen");
2617 ltask = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
2618 lsock, &do_accept, NULL);
2620 if (0 != curl_global_init (CURL_GLOBAL_WIN32))
2622 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2623 "cURL global init failed!\n");
2624 GNUNET_SCHEDULER_shutdown ();
2627 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2628 "Proxy listens on port %u\n",
2631 /* start MHD daemon for HTTP */
2632 hd = GNUNET_new (struct MhdHttpList);
2633 hd->daemon = MHD_start_daemon (MHD_USE_DEBUG | MHD_USE_NO_LISTEN_SOCKET,
2636 &create_response, hd,
2637 MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) 16,
2638 MHD_OPTION_NOTIFY_COMPLETED, &mhd_completed_cb, NULL,
2639 MHD_OPTION_URI_LOG_CALLBACK, &mhd_log_callback, NULL,
2641 if (NULL == hd->daemon)
2644 GNUNET_SCHEDULER_shutdown ();
2648 GNUNET_CONTAINER_DLL_insert (mhd_httpd_head, mhd_httpd_tail, hd);
2653 * Method called to inform about the egos of the shorten zone of this peer.
2655 * When used with #GNUNET_IDENTITY_create or #GNUNET_IDENTITY_get,
2656 * this function is only called ONCE, and 'NULL' being passed in
2657 * @a ego does indicate an error (i.e. name is taken or no default
2658 * value is known). If @a ego is non-NULL and if '*ctx'
2659 * is set in those callbacks, the value WILL be passed to a subsequent
2660 * call to the identity callback of #GNUNET_IDENTITY_connect (if
2661 * that one was not NULL).
2663 * @param cls closure, NULL
2664 * @param ego ego handle
2665 * @param ctx context for application to store data for this ego
2666 * (during the lifetime of this process, initially NULL)
2667 * @param name name assigned by the user for this ego,
2668 * NULL if the user just deleted the ego and it
2669 * must thus no longer be used
2672 identity_shorten_cb (void *cls,
2673 struct GNUNET_IDENTITY_Ego *ego,
2680 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2681 _("No ego configured for `shorten-zone`\n"));
2685 local_shorten_zone = *GNUNET_IDENTITY_ego_get_private_key (ego);
2686 do_shorten = GNUNET_YES;
2693 * Method called to inform about the egos of the master zone of this peer.
2695 * When used with #GNUNET_IDENTITY_create or #GNUNET_IDENTITY_get,
2696 * this function is only called ONCE, and 'NULL' being passed in
2697 * @a ego does indicate an error (i.e. name is taken or no default
2698 * value is known). If @a ego is non-NULL and if '*ctx'
2699 * is set in those callbacks, the value WILL be passed to a subsequent
2700 * call to the identity callback of #GNUNET_IDENTITY_connect (if
2701 * that one was not NULL).
2703 * @param cls closure, NULL
2704 * @param ego ego handle
2705 * @param ctx context for application to store data for this ego
2706 * (during the lifetime of this process, initially NULL)
2707 * @param name name assigned by the user for this ego,
2708 * NULL if the user just deleted the ego and it
2709 * must thus no longer be used
2712 identity_master_cb (void *cls,
2713 struct GNUNET_IDENTITY_Ego *ego,
2720 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2721 _("No ego configured for `%s`\n"),
2723 GNUNET_SCHEDULER_shutdown ();
2726 GNUNET_IDENTITY_ego_get_public_key (ego,
2728 id_op = GNUNET_IDENTITY_get (identity,
2730 &identity_shorten_cb,
2736 * Main function that will be run
2738 * @param cls closure
2739 * @param args remaining command-line arguments
2740 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
2741 * @param c configuration
2744 run (void *cls, char *const *args, const char *cfgfile,
2745 const struct GNUNET_CONFIGURATION_Handle *c)
2747 char* cafile_cfg = NULL;
2751 if (NULL == (curl_multi = curl_multi_init ()))
2753 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2754 "Failed to create cURL multi handle!\n");
2757 cafile = cafile_opt;
2760 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "gns-proxy",
2764 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
2769 cafile = cafile_cfg;
2771 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2772 "Using %s as CA\n", cafile);
2774 gnutls_global_init ();
2775 gnutls_x509_crt_init (&proxy_ca.cert);
2776 gnutls_x509_privkey_init (&proxy_ca.key);
2778 if ( (GNUNET_OK != load_cert_from_file (proxy_ca.cert, cafile)) ||
2779 (GNUNET_OK != load_key_from_file (proxy_ca.key, cafile)) )
2781 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2782 _("Failed to load SSL/TLS key and certificate from `%s'\n"),
2784 gnutls_x509_crt_deinit (proxy_ca.cert);
2785 gnutls_x509_privkey_deinit (proxy_ca.key);
2786 gnutls_global_deinit ();
2787 GNUNET_free_non_null (cafile_cfg);
2790 GNUNET_free_non_null (cafile_cfg);
2791 if (NULL == (gns_handle = GNUNET_GNS_connect (cfg)))
2793 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2794 "Unable to connect to GNS!\n");
2795 gnutls_x509_crt_deinit (proxy_ca.cert);
2796 gnutls_x509_privkey_deinit (proxy_ca.key);
2797 gnutls_global_deinit ();
2800 identity = GNUNET_IDENTITY_connect (cfg,
2802 id_op = GNUNET_IDENTITY_get (identity,
2804 &identity_master_cb,
2806 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
2807 &do_shutdown, NULL);
2812 * The main function for gnunet-gns-proxy.
2814 * @param argc number of arguments from the command line
2815 * @param argv command line arguments
2816 * @return 0 ok, 1 on error
2819 main (int argc, char *const *argv)
2821 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
2823 gettext_noop ("listen on specified port (default: 7777)"), 1,
2824 &GNUNET_GETOPT_set_ulong, &port},
2825 {'a', "authority", NULL,
2826 gettext_noop ("pem file to use as CA"), 1,
2827 &GNUNET_GETOPT_set_string, &cafile_opt},
2828 GNUNET_GETOPT_OPTION_END
2830 static const char* page =
2831 "<html><head><title>gnunet-gns-proxy</title>"
2832 "</head><body>cURL fail</body></html>";
2835 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
2837 GNUNET_log_setup ("gnunet-gns-proxy", "WARNING", NULL);
2838 curl_failure_response = MHD_create_response_from_buffer (strlen (page),
2840 MHD_RESPMEM_PERSISTENT);
2844 GNUNET_PROGRAM_run (argc, argv, "gnunet-gns-proxy",
2845 _("GNUnet GNS proxy"),
2847 &run, NULL)) ? 0 : 1;
2848 MHD_destroy_response (curl_failure_response);
2849 GNUNET_free_non_null ((char *) argv);
2853 /* end of gnunet-gns-proxy.c */