Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA.
*/
-
+/**
+ * @author Martin Schanzenbach
+ * @file src/gns/gnunet-gns-proxy.c
+ * @brief HTTP(S) proxy that rewrites URIs and fakes certificats to make GADS work
+ * with legacy browsers
+ */
#include "platform.h"
-#include <gnunet_util_lib.h>
-#include <gnunet_gns_service.h>
#include <microhttpd.h>
#include <curl/curl.h>
-#include <regex.h>
-#include "gns_proxy_proto.h"
-#include "gns.h"
-
-/** SSL **/
#include <gnutls/gnutls.h>
#include <gnutls/x509.h>
#include <gnutls/abstract.h>
#include <gnutls/crypto.h>
-#include <time.h>
+#include <regex.h>
+#include "gnunet_util_lib.h"
+#include "gnunet_gns_service.h"
+#include "gns_proxy_proto.h"
+#include "gns.h"
-#define HAVE_MHD_NO_LISTEN_SOCKET MHD_VERSION >= 0x00091401
+#define HAVE_MHD_NO_LISTEN_SOCKET (MHD_VERSION >= 0x00091401)
#define GNUNET_GNS_PROXY_PORT 7777
#define MHD_MAX_CONNECTIONS 300
#define POSTBUFFERSIZE 4096
/* MHD/cURL defines */
-#define BUF_WAIT_FOR_CURL 0
-#define BUF_WAIT_FOR_MHD 1
-#define BUF_WAIT_FOR_PP 2
+enum BufferStatus
+ {
+ BUF_WAIT_FOR_CURL,
+ BUF_WAIT_FOR_MHD,
+ BUF_WAIT_FOR_PP
+ };
+
#define HTML_HDR_CONTENT "Content-Type: text/html"
/* buffer padding for proper RE matching */
/* DLL for tasks */
struct ProxyCurlTask *next;
- /* Already accepted */
- int accepted;
-
/* Handle to cURL */
CURL *curl;
- /* is curl running? */
- int curl_running;
-
/* Optional header replacements for curl (LEHO) */
struct curl_slist *headers;
/* Write pos in the buffer */
char *buffer_write_ptr;
- /* The buffer status (BUF_WAIT_FOR_CURL or BUF_WAIT_FOR_MHD) */
- int buf_status;
+ /* connection */
+ struct MHD_Connection *connection;
- /* Number of bytes in buffer */
- unsigned int bytes_in_buffer;
+ /*put*/
+ size_t put_read_offset;
+ size_t put_read_size;
- /* Indicates wheather the download is in progress */
- int download_in_progress;
+ /*post*/
+ struct MHD_PostProcessor *post_handler;
- /* Indicates wheather the download was successful */
- int download_is_finished;
+ /* post data */
+ struct ProxyUploadData *upload_data_head;
+ struct ProxyUploadData *upload_data_tail;
- /* Indicates wheather the download failed */
- int download_error;
+ /* the type of POST encoding */
+ char* post_type;
- /* Indicates wheather we need to parse HTML */
- int parse_content;
+ struct curl_httppost *httppost;
- /* Indicates wheather we are postprocessing the HTML right now */
- int is_postprocessing;
+ struct curl_httppost *httppost_last;
- /* Indicates wheather postprocessing has finished */
- int pp_finished;
+ /* Number of bytes in buffer */
+ unsigned int bytes_in_buffer;
/* PP task */
GNUNET_SCHEDULER_TaskIdentifier pp_task;
/* PP match list */
struct ProxyREMatch *pp_match_tail;
- /* The authority of the corresponding host (site of origin) */
- char authority[256];
-
- /* The hostname (Host header field) */
- char host[256];
-
- /* The port */
- uint16_t port;
-
- /* The LEgacy HOstname (can be empty) */
- char leho[256];
-
/* The associated daemon list entry */
struct MhdHttpList *mhd;
/* Cookies to set */
struct ProxySetCookieHeader *set_cookies_tail;
+ /* The authority of the corresponding host (site of origin) */
+ char authority[256];
+
+ /* The hostname (Host header field) */
+ char host[256];
+
+ /* The LEgacy HOstname (can be empty) */
+ char leho[256];
+
+ /* The port */
+ uint16_t port;
+
+ /* The buffer status (BUF_WAIT_FOR_CURL or BUF_WAIT_FOR_MHD) */
+ enum BufferStatus buf_status;
+
/* connection status */
int ready_to_queue;
+
+ /* is curl running? */
+ int curl_running;
/* are we done */
int fin;
- /* connection */
- struct MHD_Connection *connection;
+ /* Already accepted */
+ int accepted;
- /*put*/
- size_t put_read_offset;
- size_t put_read_size;
+ /* Indicates wheather the download is in progress */
+ int download_in_progress;
- /*post*/
- struct MHD_PostProcessor *post_handler;
+ /* Indicates wheather the download was successful */
+ int download_is_finished;
- /* post data */
- struct ProxyUploadData *upload_data_head;
- struct ProxyUploadData *upload_data_tail;
+ /* Indicates wheather the download failed */
+ int download_error;
- int post_done;
+ /* Indicates wheather we need to parse HTML */
+ int parse_content;
- /* the type of POST encoding */
- char* post_type;
+ /* Indicates wheather we are postprocessing the HTML right now */
+ int is_postprocessing;
- struct curl_httppost *httppost;
+ /* Indicates wheather postprocessing has finished */
+ int pp_finished;
- struct curl_httppost *httppost_last;
+ int post_done;
int is_httppost;
/* DLL */
struct ProxyREMatch *prev;
+ /* start of match in buffer */
+ char* start;
+
+ /* end of match in buffer */
+ char* end;
+
+ /* associated proxycurltask */
+ struct ProxyCurlTask *ctask;
+
/* hostname found */
char hostname[255];
/* are we done */
int done;
- /* start of match in buffer */
- char* start;
-
- /* end of match in buffer */
- char* end;
+ /* is SSL */
+ int is_ssl;
- /* associated proxycurltask */
- struct ProxyCurlTask *ctask;
};
/**
static struct GNUNET_NETWORK_Handle *lsock;
/* The listen task ID */
-GNUNET_SCHEDULER_TaskIdentifier ltask;
+static GNUNET_SCHEDULER_TaskIdentifier ltask;
/* The cURL download task */
-GNUNET_SCHEDULER_TaskIdentifier curl_download_task;
+static GNUNET_SCHEDULER_TaskIdentifier curl_download_task;
/* The non SSL httpd daemon handle */
static struct MHD_Daemon *httpd;
static struct ProxyCA proxy_ca;
/* UNIX domain socket for mhd */
-struct GNUNET_NETWORK_Handle *mhd_unix_socket;
+#if !HAVE_MHD_NO_LISTEN_SOCKET
+static struct GNUNET_NETWORK_Handle *mhd_unix_socket;
+#endif
/* Shorten zone private key */
-struct GNUNET_CRYPTO_RsaPrivateKey *shorten_zonekey;
+static struct GNUNET_CRYPTO_RsaPrivateKey *shorten_zonekey;
+
/**
* Checks if name is in tld
*
- * @param name the name to check
- * @param tld the TLD to check for
+ * @param name the name to check
+ * @param tld the TLD to check for (must NOT begin with ".")
* @return GNUNET_YES or GNUNET_NO
*/
-int
-is_tld(const char* name, const char* tld)
-{
- size_t offset;
-
- if (strlen(name) <= strlen(tld))
- return GNUNET_NO;
-
- offset = strlen(name) - strlen(tld);
- if (0 != strcmp (name+offset, tld))
- {
- GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
- "%s is not in .%s TLD\n", name, tld);
- return GNUNET_NO;
- }
-
- return GNUNET_YES;
-}
-
-/**
- * convert integer to string representation
- *
- * @param i integer
- * @return the character
- */
-char i_to_hexchar (char i)
+static int
+is_tld (const char* name, const char* tld)
{
- static char hexmap[] = "0123456789abcdef";
- GNUNET_assert (sizeof (hexmap) > (i & 15));
- return hexmap[i & 15];
+ size_t name_len = strlen (name);
+ size_t tld_len = strlen (tld);
+
+ GNUNET_break ('.' != tld[0]);
+ return ( (tld_len < name_len) &&
+ ( ('.' == name[name_len - tld_len - 1]) || (name_len == tld_len) ) &&
+ (0 == memcmp (tld,
+ name + (name_len - tld_len),
+ tld_len)) );
}
-/**
- * Escape given 0-terminated string
- *
- * @param to_esc string to escapse
- * @return allocated new escaped string (MUST free!)
- */
-static char*
-escape_to_urlenc (const char *to_esc)
-{
- char *pos = (char*)to_esc;
- char *res = GNUNET_malloc (strlen (to_esc) * 3 + 1);
- char *rpos = res;
-
- while ('\0' != *pos)
- {
- if (isalnum (*pos) ||
- ('-' == *pos) || ('_' == *pos) ||
- ('.' == *pos) || ('~' == *pos))
- *rpos++ = *pos;
- else if (' ' == *pos)
- *rpos++ = '+';
- else
- {
- *rpos++ = '%';
- *rpos++ = i_to_hexchar (*pos >> 4);
- *rpos++ = i_to_hexchar (*pos >> 15);
- }
- pos++;
- }
- *rpos = '\0';
- return res;
-}
static int
con_post_data_iter (void *cls,
char* new_value;
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
- "Got POST data: '%s : %s' at offset %llu size %lld\n",
- key, data, off, size);
-
+ "Got POST data (file: %s, content type: %s): '%s=%.*s' at offset %llu size %llu\n",
+ filename, content_type,
+ key, (int) size, data,
+ (unsigned long long) off,
+ (unsigned long long) size);
GNUNET_assert (NULL != ctask->post_type);
if (0 == strcasecmp (MHD_HTTP_POST_ENCODING_MULTIPART_FORMDATA,
if (0 == off)
{
pdata = GNUNET_malloc (sizeof (struct ProxyUploadData));
- pdata->key = strdup (key);
-
- GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
- "Copied %lld\n");
+ pdata->key = GNUNET_strdup (key);
if (NULL != filename)
- {
- pdata->filename = strdup (filename);
- GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
- "Filename %s\n", filename);
- }
-
+ pdata->filename = GNUNET_strdup (filename);
if (NULL != content_type)
- {
- pdata->content_type = strdup (content_type);
- GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
- "Content-Type %s\n", content_type);
- }
-
+ pdata->content_type = GNUNET_strdup (content_type);
pdata->value = GNUNET_malloc (size);
pdata->total_bytes = size;
memcpy (pdata->value, data, size);
pdata);
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
- "Copied %lld bytes of POST Data\n", size);
+ "Copied %llu bytes of POST Data\n",
+ (unsigned long long) size);
return MHD_YES;
}
memcpy (new_value, pdata->value, pdata->total_bytes);
memcpy (new_value+off, data, size);
GNUNET_free (pdata->value);
+ pdata->value = new_value;
pdata->total_bytes += size;
return MHD_YES;
-
}
if (0 != strcasecmp (MHD_HTTP_POST_ENCODING_FORM_URLENCODED,
if (0 == off)
{
+ enc = curl_easy_escape (ctask->curl, key, 0);
+ if (NULL == enc)
+ {
+ GNUNET_break (0);
+ return MHD_NO;
+ }
/* a key */
pdata = GNUNET_malloc (sizeof (struct ProxyUploadData));
- enc = escape_to_urlenc (key);
pdata->value = GNUNET_malloc (strlen (enc) + 3);
if (NULL != ctask->upload_data_head)
{
pdata->value[strlen (pdata->value)] = '=';
pdata->bytes_left = strlen (pdata->value);
pdata->total_bytes = pdata->bytes_left;
- GNUNET_free (enc);
+ curl_free (enc);
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
"Escaped POST key: '%s'\n",
}
/* a value */
+ enc = curl_easy_escape (ctask->curl, data, 0);
+ if (NULL == enc)
+ {
+ GNUNET_break (0);
+ return MHD_NO;
+ }
pdata = GNUNET_malloc (sizeof (struct ProxyUploadData));
- enc = escape_to_urlenc (data);
pdata->value = GNUNET_malloc (strlen (enc) + 1);
memcpy (pdata->value, enc, strlen (enc));
pdata->bytes_left = strlen (pdata->value);
pdata->total_bytes = pdata->bytes_left;
- GNUNET_free (enc);
+ curl_free (enc);
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
"Escaped POST value: '%s'\n",
if (0 == strcmp ("Host", key))
{
- port = strstr (value, ":");
+ port = strchr (value, ':');
if (NULL != port)
{
strncpy (buf, value, port-value);
return MHD_YES;
}
- if (0 == strcmp ("Accept-Encoding", key))
+ if (0 == strcmp (MHD_HTTP_HEADER_ACCEPT_ENCODING, key))
hdr_val = "";
else
hdr_val = value;
char* buf,
size_t max);
+
/**
* Check HTTP response header for mime
*
char* hdr_val;
int delta_cdomain;
size_t offset = 0;
+ char cors_hdr[strlen (ctask->leho) + strlen ("https://")];
if (NULL == ctask->response)
{
&mhd_content_cb,
ctask,
NULL);
+
+ /* if we have a leho add a CORS header */
+ if (0 != strcmp ("", ctask->leho))
+ {
+ /* We could also allow ssl and http here */
+ if (ctask->mhd->is_ssl)
+ sprintf (cors_hdr, "https://%s", ctask->leho);
+ else
+ sprintf (cors_hdr, "http://%s", ctask->leho);
+
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "MHD: Adding CORS header field %s\n",
+ cors_hdr);
+
+ if (GNUNET_NO == MHD_add_response_header (ctask->response,
+ "Access-Control-Allow-Origin",
+ cors_hdr))
+ {
+ GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+ "MHD: Error adding CORS header field %s\n",
+ cors_hdr);
+ }
+ }
ctask->ready_to_queue = GNUNET_YES;
-
}
if (html_mime_len <= bytes)
MHD_HTTP_HEADER_SET_COOKIE,
cookie_hdr_len))
{
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "Looking for cookie in: `%s'\n", hdr_generic);
ndup = GNUNET_strdup (hdr_generic+cookie_hdr_len+1);
memset (new_cookie_hdr, 0, sizeof (new_cookie_hdr));
- tok = strtok (ndup, ";");
-
- GNUNET_log (GNUNET_ERROR_TYPE_INFO,
- "Looking for cookie in : %s\n", hdr_generic);
-
- for (; tok != NULL; tok = strtok (NULL, ";"))
+ for (tok = strtok (ndup, ";"); tok != NULL; tok = strtok (NULL, ";"))
{
GNUNET_log (GNUNET_ERROR_TYPE_INFO,
"Got Cookie token: %s\n", tok);
return bytes;
}
+
/**
* schedule mhd
*
static void
run_httpds (void);
+
/**
* Task run whenever HTTP server operations are pending.
*
do_httpd (void *cls,
const struct GNUNET_SCHEDULER_TaskContext *tc);
+
static void
run_mhd_now (struct MhdHttpList *hd)
{
hd->httpd_task = GNUNET_SCHEDULER_add_now (&do_httpd, hd);
}
+
/**
* Ask cURL for the select sets and schedule download
*/
static void
-curl_download_prepare ();
+curl_download_prepare (void);
+
/**
* Callback to free content
const struct GNUNET_SCHEDULER_TaskContext *tc)
{
struct ProxyCurlTask *ctask = cls;
- GNUNET_assert (NULL == ctask->pp_match_head);
+ struct ProxyUploadData *pdata;
+ GNUNET_assert (NULL == ctask->pp_match_head);
if (NULL != ctask->headers)
curl_slist_free_all (ctask->headers);
if (NULL != ctask->post_handler)
MHD_destroy_post_processor (ctask->post_handler);
+ if (GNUNET_SCHEDULER_NO_TASK != ctask->pp_task)
+ GNUNET_SCHEDULER_cancel (ctask->pp_task);
+ for (pdata = ctask->upload_data_head; NULL != pdata; pdata = ctask->upload_data_head)
+ {
+ GNUNET_CONTAINER_DLL_remove (ctask->upload_data_head,
+ ctask->upload_data_tail,
+ pdata);
+ GNUNET_free_non_null (pdata->filename);
+ GNUNET_free_non_null (pdata->content_type);
+ GNUNET_free_non_null (pdata->key);
+ GNUNET_free_non_null (pdata->value);
+ GNUNET_free (pdata);
+ }
GNUNET_free (ctask);
}
size_t max)
{
struct ProxyCurlTask *ctask = cls;
- struct ProxyREMatch *re_match = ctask->pp_match_head;
+ struct ProxyREMatch *re_match;
ssize_t copied = 0;
- long long int bytes_to_copy = ctask->buffer_write_ptr - ctask->buffer_read_ptr;
+ size_t bytes_to_copy = ctask->buffer_write_ptr - ctask->buffer_read_ptr;
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
- "MHD: content cb for %s. To copy: %lld\n",
- ctask->url, bytes_to_copy);
- GNUNET_assert (bytes_to_copy >= 0);
-
+ "MHD: content cb for %s. To copy: %u\n",
+ ctask->url, (unsigned int) bytes_to_copy);
if ((GNUNET_YES == ctask->download_is_finished) &&
(GNUNET_NO == ctask->download_error) &&
- (0 == bytes_to_copy)) /* &&
- (BUF_WAIT_FOR_CURL == ctask->buf_status))*/
+ (0 == bytes_to_copy))
{
GNUNET_log (GNUNET_ERROR_TYPE_INFO,
"MHD: sending response for %s\n", ctask->url);
if ((GNUNET_YES == ctask->download_error) &&
(GNUNET_YES == ctask->download_is_finished) &&
- (0 == bytes_to_copy)) /* &&
- (BUF_WAIT_FOR_CURL == ctask->buf_status))*/
+ (0 == bytes_to_copy))
{
GNUNET_log (GNUNET_ERROR_TYPE_INFO,
"MHD: sending error response\n");
return 0;
copied = 0;
- for (; NULL != re_match; re_match = ctask->pp_match_head)
+ for (re_match = ctask->pp_match_head; NULL != re_match; re_match = ctask->pp_match_head)
{
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
"MHD: Processing PP %s\n",
re_match->hostname);
bytes_to_copy = re_match->start - ctask->buffer_read_ptr;
- GNUNET_assert (bytes_to_copy >= 0);
-
if (bytes_to_copy+copied > max)
{
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
- "MHD: buffer in response too small for %d. Using available space (%d). (%s)\n",
- bytes_to_copy,
- max,
- ctask->url);
+ "MHD: buffer in response too small for %u. Using available space (%d). (%s)\n",
+ (unsigned int) bytes_to_copy,
+ max,
+ ctask->url);
memcpy (buf+copied, ctask->buffer_read_ptr, max-copied);
ctask->buffer_read_ptr += max-copied;
copied = max;
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
- "MHD: copied %d bytes\n", copied);
+ "MHD: copied %d bytes\n", (int) copied);
return copied;
}
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
- "MHD: copying %d bytes to mhd response at offset %d\n",
- bytes_to_copy, ctask->buffer_read_ptr);
+ "MHD: copying %u bytes to mhd response at offset %d\n",
+ (unsigned int) bytes_to_copy, ctask->buffer_read_ptr);
memcpy (buf+copied, ctask->buffer_read_ptr, bytes_to_copy);
copied += bytes_to_copy;
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
"MHD: Waiting for PP of %s\n", re_match->hostname);
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
- "MHD: copied %d bytes\n", copied);
+ "MHD: copied %d bytes\n", (int) copied);
ctask->buffer_read_ptr += bytes_to_copy;
return copied;
}
re_match->result,
ctask->url);
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
- "MHD: copied %d bytes\n", copied);
+ "MHD: copied %d bytes\n", (int) copied);
ctask->buffer_read_ptr += bytes_to_copy;
return copied;
}
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
"MHD: Adding PP result %s to buffer\n",
re_match->result);
- memcpy (buf+copied, re_match->result, strlen (re_match->result));
+ memcpy (buf + copied, re_match->result, strlen (re_match->result));
copied += strlen (re_match->result);
ctask->buffer_read_ptr = re_match->end;
GNUNET_CONTAINER_DLL_remove (ctask->pp_match_head,
bytes_to_copy = ctask->buffer_write_ptr - ctask->buffer_read_ptr;
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
- "MHD: copied: %d left: %d, space left in buf: %d\n",
+ "MHD: copied: %d left: %u, space left in buf: %d\n",
copied,
- bytes_to_copy, max-copied);
+ (unsigned int) bytes_to_copy, (int) (max - copied));
- GNUNET_assert (0 <= bytes_to_copy);
-
if (GNUNET_NO == ctask->download_is_finished)
{
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
ctask->buffer[bytes_to_copy] = '\0';
}
- if (bytes_to_copy+copied > max)
- bytes_to_copy = max-copied;
-
- if (0 > bytes_to_copy)
- bytes_to_copy = 0;
-
+ if (bytes_to_copy + copied > max)
+ bytes_to_copy = max - copied;
memcpy (buf+copied, ctask->buffer_read_ptr, bytes_to_copy);
ctask->buffer_read_ptr += bytes_to_copy;
copied += bytes_to_copy;
curl_easy_pause (ctask->curl, CURLPAUSE_CONT);
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
- "MHD: copied %d bytes\n", copied);
+ "MHD: copied %d bytes\n", (int) copied);
run_mhd_now (ctask->mhd);
return copied;
}
+
/**
* Shorten result callback
*
re_match->hostname,
result);
- if (re_match->ctask->mhd->is_ssl)
+ if (re_match->is_ssl)
sprintf (re_match->result, "href=\"https://%s", result);
else
sprintf (re_match->result, "href=\"http://%s", result);
memset (re_hostname, 0, sizeof (re_hostname));
memcpy (re_hostname, re_ptr+m[1].rm_so, (m[3].rm_eo-m[1].rm_so));
+
re_match = GNUNET_malloc (sizeof (struct ProxyREMatch));
re_match->start = re_ptr + m[0].rm_so;
re_match->end = re_ptr + m[3].rm_eo;
re_match->done = GNUNET_NO;
re_match->ctask = ctask;
+
+ if ('s' == *(re_ptr+m[1].rm_so-strlen("://")-1)) //FIXME strcmp
+ re_match->is_ssl = GNUNET_YES;
+ else
+ re_match->is_ssl = GNUNET_NO;
+
strcpy (re_match->hostname, re_hostname);
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
"PP: Got hostname %s\n", re_hostname);
run_mhd_now (ctask->mhd);
}
+
/**
* Handle data from cURL
*
return to_copy;
}
+
/**
* Task that is run when we are ready to receive more data
* from curl
static void
curl_task_download (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
+
/**
* Ask cURL for the select sets and schedule download
*/
FD_ZERO (&rs);
FD_ZERO (&ws);
FD_ZERO (&es);
- mret = curl_multi_fdset (curl_multi, &rs, &ws, &es, &max);
-
- if (mret != CURLM_OK)
+ if (CURLM_OK != (mret = curl_multi_fdset (curl_multi, &rs, &ws, &es, &max)))
{
GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
"%s failed at %s:%d: `%s'\n",
//TODO cleanup here?
return;
}
-
- mret = curl_multi_timeout (curl_multi, &to);
- rtime = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, to);
-
+ to = -1;
+ GNUNET_break (CURLM_OK == curl_multi_timeout (curl_multi, &to));
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
- "cURL multi fds: max=%d timeout=%llu\n", max, to);
-
+ "cURL multi fds: max=%d timeout=%lld\n", max, (long long) to);
+ if (-1 == to)
+ rtime = GNUNET_TIME_UNIT_FOREVER_REL;
+ else
+ rtime = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, to);
grs = GNUNET_NETWORK_fdset_create ();
gws = GNUNET_NETWORK_fdset_create ();
GNUNET_NETWORK_fdset_copy_native (grs, &rs, max + 1);
GNUNET_NETWORK_fdset_copy_native (gws, &ws, max + 1);
- GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
- "Scheduling task cURL\n");
-
if (curl_download_task != GNUNET_SCHEDULER_NO_TASK)
- GNUNET_SCHEDULER_cancel (curl_download_task);
-
+ GNUNET_SCHEDULER_cancel (curl_download_task);
if (-1 != max)
{
curl_download_task =
struct ProxyCurlTask *ctask;
int num_ctasks;
long resp_code;
-
struct ProxyCurlTask *clean_head = NULL;
struct ProxyCurlTask *clean_tail = NULL;
{
GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
"Shutdown requested while trying to download\n");
- //TODO cleanup
- return;
+ //TODO cleanup
+ return;
}
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
"Ready to dl\n");
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
"Running curl tasks: %d\n", running);
- ctask = ctasks_head;
- for (; ctask != NULL; ctask = ctask->next)
+ for (ctask = ctasks_head; NULL != ctask; ctask = ctask->next)
{
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
"CTask: %s\n", ctask->url);
do
{
- ctask = ctasks_head;
+
msg = curl_multi_info_read (curl_multi, &msgnum);
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
"Messages left: %d\n", msgnum);
GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
"Download curl failed");
- for (; ctask != NULL; ctask = ctask->next)
+ for (ctask = ctasks_head; NULL != ctask; ctask = ctask->next)
{
if (NULL == ctask->curl)
continue;
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
"CURL: download completed.\n");
- for (; ctask != NULL; ctask = ctask->next)
+ for (ctask = ctasks_head; NULL != ctask; ctask = ctask->next)
{
if (NULL == ctask->curl)
continue;
}
} while (msgnum > 0);
- for (ctask=clean_head; ctask != NULL; ctask = ctask->next)
+ for (ctask=clean_head; NULL != ctask; ctask = ctask->next)
{
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
"CURL: Removing task %s.\n", ctask->url);
}
num_ctasks=0;
- for (ctask=ctasks_head; ctask != NULL; ctask = ctask->next)
- {
- num_ctasks++;
- }
+ for (ctask=ctasks_head; NULL != ctask; ctask = ctask->next)
+ num_ctasks++;
if (num_ctasks != running)
{
curl_download_prepare();
}
+
/**
* Process LEHO lookup
*
}
+
/**
* Initialize download and trigger curl
*
ctask);
}
+
static void*
mhd_log_callback (void* cls, const char* url)
{
ctask = GNUNET_malloc (sizeof (struct ProxyCurlTask));
strcpy (ctask->url, url);
- ctask->accepted = GNUNET_NO;
return ctask;
}
+
/**
* Main MHD callback for handling requests.
*
void **con_cls)
{
struct MhdHttpList* hd = cls;
- const char* page = "<html><head><title>gnoxy</title>"\
+ const char* page = "<html><head><title>gnunet-gns-proxy</title>"
"</head><body>cURL fail</body></html>";
char curlurl[MAX_HTTP_URI_LENGTH]; // buffer overflow!
int ret = MHD_YES;
int i;
-
struct ProxyCurlTask *ctask = *con_cls;
struct ProxyUploadData *fin_post;
struct curl_forms forms[5];
curl_easy_setopt (ctask->curl, CURLOPT_TIMEOUT, 600L);
/* Add GNS header */
- //ctask->headers = curl_slist_append (ctask->headers,
- // "GNS: YES");
+ ctask->headers = curl_slist_append (ctask->headers,
+ "GNS: YES");
ctask->accepted = GNUNET_YES;
ctask->download_in_progress = GNUNET_YES;
ctask->buf_status = BUF_WAIT_FOR_CURL;
{
if (GNUNET_YES == ctask->is_httppost)
{
- i = 0;
for (upload_data_iter = ctask->upload_data_head;
NULL != upload_data_iter;
upload_data_iter = upload_data_iter->next)
{
+ i = 0;
if (NULL != upload_data_iter->filename)
{
forms[i].option = CURLFORM_FILENAME;
}
+
+#define UNSIGNED_MHD_LONG_LONG unsigned MHD_LONG_LONG
+
+
/**
* schedule mhd
*
struct GNUNET_NETWORK_FDSet *wes;
int max;
int haveto;
- unsigned MHD_LONG_LONG timeout;
+ UNSIGNED_MHD_LONG_LONG timeout;
struct GNUNET_TIME_Relative tv;
FD_ZERO (&rs);
}
-
/**
* Read data from socket
*
static void
do_read (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
+
/**
* Read from remote end
*
static void
do_read_remote (void* cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
+
/**
* Write data to remote socket
*
if (GNUNET_SCHEDULER_NO_TASK != s5r->fwdwtask)
GNUNET_SCHEDULER_cancel (s5r->fwdwtask);
if (GNUNET_SCHEDULER_NO_TASK != s5r->fwdrtask)
- GNUNET_SCHEDULER_cancel (s5r->fwdrtask);
-
+ GNUNET_SCHEDULER_cancel (s5r->fwdrtask);
if (NULL != s5r->remote_sock)
GNUNET_NETWORK_socket_close (s5r->remote_sock);
if ((NULL != s5r->sock) && (s5r->cleanup_sock == GNUNET_YES))
GNUNET_free(s5r);
}
+
/**
* Write data to socket
*
len);
}
else
- {
-
+ {
GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "write");
s5r->cleanup = GNUNET_YES;
s5r->cleanup_sock = GNUNET_YES;
- cleanup_s5r (s5r);
-
+ cleanup_s5r (s5r);
return;
}
&do_read_remote, s5r);
}
+
/**
* Read from remote end
*
struct Socks5Request *s5r = cls;
s5r->fwdrtask = GNUNET_SCHEDULER_NO_TASK;
-
-
if ((NULL != tc->write_ready) &&
(GNUNET_NETWORK_fdset_isset (tc->read_ready, s5r->remote_sock)) &&
(s5r->wbuf_len = GNUNET_NETWORK_socket_recv (s5r->remote_sock, s5r->wbuf,
}
else
{
- if (s5r->wbuf_len == 0)
+ if (0 == s5r->wbuf_len)
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
"0 bytes received from remote... graceful shutdown!\n");
if (s5r->fwdwtask != GNUNET_SCHEDULER_NO_TASK)
s5r->wtask = GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
s5r->sock,
- &do_write, s5r);
-
+ &do_write, s5r);
}
return MHD_add_connection (daemon, fd, addr, len);
}
+
/**
* Read file in filename
*
* @param filename file to read
* @param size pointer where filesize is stored
- * @return data
+ * @return NULL on error
*/
-static char*
+static void*
load_file (const char* filename,
unsigned int* size)
{
- char *buffer;
+ void *buffer;
uint64_t fsize;
if (GNUNET_OK !=
gnutls_datum_t key_data;
int ret;
- key_data.data = (unsigned char*) load_file (keyfile, &key_data.size);
+ key_data.data = load_file (keyfile, &key_data.size);
ret = gnutls_x509_privkey_import (key, &key_data,
GNUTLS_X509_FMT_PEM);
if (GNUTLS_E_SUCCESS != ret)
load_cert_from_file (gnutls_x509_crt_t crt, char* certfile)
{
gnutls_datum_t cert_data;
- cert_data.data = NULL;
int ret;
- cert_data.data = (unsigned char*) load_file (certfile, &cert_data.size);
+ cert_data.data = load_file (certfile, &cert_data.size);
ret = gnutls_x509_crt_import (crt, &cert_data,
GNUTLS_X509_FMT_PEM);
if (GNUTLS_E_SUCCESS != ret)
static struct ProxyGNSCertificate *
generate_gns_certificate (const char *name)
{
-
int ret;
unsigned int serial;
size_t key_buf_size;
GNUNET_break (0);
}
- ret = gnutls_x509_crt_set_key (request, proxy_ca.key);
-
- if (GNUTLS_E_SUCCESS != ret)
- {
- GNUNET_break (0);
- }
+ GNUNET_break (GNUTLS_E_SUCCESS == gnutls_x509_crt_set_key (request, proxy_ca.key));
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Generating cert\n");
gnutls_x509_crt_set_dn_by_oid (request, GNUTLS_OID_X520_COUNTRY_NAME,
0, "DE", 2);
-
gnutls_x509_crt_set_dn_by_oid (request, GNUTLS_OID_X520_ORGANIZATION_NAME,
- 0, "GNUnet", 6);
-
+ 0, "GADS", 4);
gnutls_x509_crt_set_dn_by_oid (request, GNUTLS_OID_X520_COMMON_NAME,
0, name, strlen (name));
-
- ret = gnutls_x509_crt_set_version (request, 3);
+ GNUNET_break (GNUTLS_E_SUCCESS == gnutls_x509_crt_set_version (request, 3));
ret = gnutls_rnd (GNUTLS_RND_NONCE, &serial, sizeof (serial));
}
-/*
+/**
* Accept policy for mhdaemons
*
* @param cls NULL
* @param addr the sockaddr
* @param addrlen the sockaddr length
- * @return MHD_NO if sockaddr is wrong or #conns too high
+ * @return MHD_NO if sockaddr is wrong or number of connections is too high
*/
static int
accept_cb (void* cls, const struct sockaddr *addr, socklen_t addrlen)
static int
add_handle_to_ssl_mhd (struct GNUNET_NETWORK_Handle *h, const char* domain)
{
- struct MhdHttpList *hd = NULL;
+ struct MhdHttpList *hd;
struct ProxyGNSCertificate *pgc;
struct NetworkHandleList *nh;
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
"No previous SSL instance found... starting new one for %s\n",
domain);
- hd->daemon = MHD_start_daemon (MHD_USE_DEBUG
- | MHD_USE_SSL
#if HAVE_MHD_NO_LISTEN_SOCKET
- | MHD_USE_NO_LISTEN_SOCKET,
+ hd->daemon = MHD_start_daemon (MHD_USE_DEBUG | MHD_USE_SSL | MHD_USE_NO_LISTEN_SOCKET,
0,
+ &accept_cb, NULL,
+ &create_response, hd,
+ MHD_OPTION_CONNECTION_LIMIT,
+ MHD_MAX_CONNECTIONS,
+ MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) 16,
+ MHD_OPTION_NOTIFY_COMPLETED, NULL, NULL,
+ MHD_OPTION_HTTPS_MEM_KEY, pgc->key,
+ MHD_OPTION_HTTPS_MEM_CERT, pgc->cert,
+ MHD_OPTION_URI_LOG_CALLBACK, &mhd_log_callback,
+ NULL,
+ MHD_OPTION_END);
#else
- , 4444, //Dummy
-#endif
+ hd->daemon = MHD_start_daemon (MHD_USE_DEBUG | MHD_USE_SSL,
+ 4444 /* dummy port */,
&accept_cb, NULL,
&create_response, hd,
-#if !HAVE_MHD_NO_LISTEN_SOCKET
- MHD_OPTION_LISTEN_SOCKET, GNUNET_NETWORK_get_fd (mhd_unix_socket),
-#endif
+ MHD_OPTION_LISTEN_SOCKET, GNUNET_NETWORK_get_fd (mhd_unix_socket),
MHD_OPTION_CONNECTION_LIMIT,
MHD_MAX_CONNECTIONS,
- MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) 16,
- MHD_OPTION_NOTIFY_COMPLETED, NULL, NULL,
- MHD_OPTION_HTTPS_MEM_KEY, pgc->key,
- MHD_OPTION_HTTPS_MEM_CERT, pgc->cert,
- MHD_OPTION_URI_LOG_CALLBACK, &mhd_log_callback,
- NULL,
- MHD_OPTION_END);
-
+ MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) 16,
+ MHD_OPTION_NOTIFY_COMPLETED, NULL, NULL,
+ MHD_OPTION_HTTPS_MEM_KEY, pgc->key,
+ MHD_OPTION_HTTPS_MEM_CERT, pgc->cert,
+ MHD_OPTION_URI_LOG_CALLBACK, &mhd_log_callback,
+ NULL,
+ MHD_OPTION_END);
+#endif
GNUNET_assert (hd->daemon != NULL);
hd->httpd_task = GNUNET_SCHEDULER_NO_TASK;
}
-
/**
* Read data from incoming connection
*
struct socks5_server_hello *s_hello;
struct socks5_client_request *c_req;
struct socks5_server_response *s_resp;
-
int ret;
char domain[256];
uint8_t dom_len;
uint32_t remote_ip;
struct sockaddr_in remote_addr;
struct in_addr *r_sin_addr;
-
struct NetworkHandleList *nh;
s5r->rtask = GNUNET_SCHEDULER_NO_TASK;
s5r->remote_sock,
&do_read_remote, s5r);
}
-
-
}
-
}
do_shutdown (void *cls,
const struct GNUNET_SCHEDULER_TaskContext *tc)
{
-
struct MhdHttpList *hd;
struct MhdHttpList *tmp_hd;
struct NetworkHandleList *nh;
struct NetworkHandleList *tmp_nh;
struct ProxyCurlTask *ctask;
struct ProxyCurlTask *ctask_tmp;
+ struct ProxyUploadData *pdata;
GNUNET_log (GNUNET_ERROR_TYPE_INFO,
"Shutting down...\n");
-
- gnutls_global_deinit ();
-
if (NULL != local_gns_zone)
GNUNET_free (local_gns_zone);
if (NULL != local_private_zone)
GNUNET_SCHEDULER_cancel (hd->httpd_task);
hd->httpd_task = GNUNET_SCHEDULER_NO_TASK;
}
-
if (NULL != hd->daemon)
{
MHD_stop_daemon (hd->daemon);
hd->daemon = NULL;
}
-
for (nh = hd->socket_handles_head; nh != NULL; nh = tmp_nh)
{
tmp_nh = nh->next;
if (NULL != ctask->response)
MHD_destroy_response (ctask->response);
- //FIXME free pdata here
-
+ pdata = ctask->upload_data_head;
+ //FIXME free pdata here
+ for (; pdata != NULL; pdata = ctask->upload_data_head)
+ {
+ GNUNET_CONTAINER_DLL_remove (ctask->upload_data_head,
+ ctask->upload_data_tail,
+ pdata);
+ GNUNET_free_non_null (pdata->filename);
+ GNUNET_free_non_null (pdata->content_type);
+ GNUNET_free_non_null (pdata->key);
+ GNUNET_free_non_null (pdata->value);
+ GNUNET_free (pdata);
+ }
GNUNET_free (ctask);
}
curl_multi_cleanup (curl_multi);
-
GNUNET_GNS_disconnect (gns_handle);
+ gnutls_global_deinit ();
}
load_local_zone_key (const struct GNUNET_CONFIGURATION_Handle *cfg)
{
char *keyfile;
- struct GNUNET_CRYPTO_RsaPrivateKey *key = NULL;
+ struct GNUNET_CRYPTO_RsaPrivateKey *key;
struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pkey;
- struct GNUNET_CRYPTO_ShortHashCode *zone = NULL;
+ struct GNUNET_CRYPTO_ShortHashCode *zone;
struct GNUNET_CRYPTO_ShortHashAsciiEncoded zonename;
if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "gns",
return GNUNET_YES;
}
+
/**
* Main function that will be run
*
struct sockaddr_un mhd_unix_sock_addr;
#endif
- curl_multi = curl_multi_init ();
-
- if (NULL == curl_multi)
+ if (NULL == (curl_multi = curl_multi_init ()))
{
GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
"Failed to create cURL multo handle!\n");
if (NULL == cafile)
{
if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "gns-proxy",
- "PROXY_CACERT",
- &cafile_cfg))
+ "PROXY_CACERT",
+ &cafile_cfg))
{
GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
"Unable to load proxy CA config value!\n");
compile_regex (&re_dotplus, (char*) RE_A_HREF);
- gns_handle = GNUNET_GNS_connect (cfg);
-
- if (GNUNET_NO == load_local_zone_key (cfg))
+ if (NULL == (gns_handle = GNUNET_GNS_connect (cfg)))
{
GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
- "Unable to load zone!\n");
+ "Unable to connect to GNS!\n");
return;
}
-
- if (NULL == gns_handle)
+ if (GNUNET_NO == load_local_zone_key (cfg))
{
GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
- "Unable to connect to GNS!\n");
+ "Unable to load zone!\n");
return;
}
GNUNET_a2s ((const struct sockaddr *) &sa, sizeof (sa)));
return;
}
-
ltask = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
lsock, &do_accept, NULL);
- ctasks_head = NULL;
- ctasks_tail = NULL;
-
if (0 != curl_global_init (CURL_GLOBAL_WIN32))
{
GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
"Proxy listens on port %u\n",
port);
-
- mhd_httpd_head = NULL;
- mhd_httpd_tail = NULL;
- total_mhd_connections = 0;
-#ifndef HAVE_MHD_NO_LISTEN_SOCKET
+#if ! HAVE_MHD_NO_LISTEN_SOCKET
if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "gns-proxy",
"PROXY_UNIXPATH",
&proxy_sockfile))
"Specify PROXY_UNIXPATH in gns-proxy config section!\n");
return;
}
-
- mhd_unix_socket = GNUNET_NETWORK_socket_create (AF_UNIX,
- SOCK_STREAM,
- 0);
-
- if (NULL == mhd_unix_socket)
+ if (NULL == (mhd_unix_socket = GNUNET_NETWORK_socket_create (AF_UNIX,
+ SOCK_STREAM,
+ 0)))
{
GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
"Unable to create unix domain socket!\n");
#endif
len = strlen (proxy_sockfile) + sizeof(AF_UNIX);
-
GNUNET_free (proxy_sockfile);
if (GNUNET_OK != GNUNET_NETWORK_socket_bind (mhd_unix_socket,
hd = GNUNET_malloc (sizeof (struct MhdHttpList));
hd->is_ssl = GNUNET_NO;
strcpy (hd->domain, "");
- httpd = MHD_start_daemon (MHD_USE_DEBUG
+
#if HAVE_MHD_NO_LISTEN_SOCKET
- | MHD_USE_NO_LISTEN_SOCKET,
+ httpd = MHD_start_daemon (MHD_USE_DEBUG | MHD_USE_NO_LISTEN_SOCKET,
0,
+ &accept_cb, NULL,
+ &create_response, hd,
+ MHD_OPTION_CONNECTION_LIMIT, MHD_MAX_CONNECTIONS,
+ MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) 16,
+ MHD_OPTION_NOTIFY_COMPLETED,
+ NULL, NULL,
+ MHD_OPTION_URI_LOG_CALLBACK, &mhd_log_callback, NULL,
+ MHD_OPTION_END);
#else
- , 4444, //Dummy port
+ httpd = MHD_start_daemon (MHD_USE_DEBUG,
+ 4444 /* Dummy port */,
+ &accept_cb, NULL,
+ &create_response, hd,
+ MHD_OPTION_LISTEN_SOCKET, GNUNET_NETWORK_get_fd (mhd_unix_socket),
+ MHD_OPTION_CONNECTION_LIMIT, MHD_MAX_CONNECTIONS,
+ MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) 16,
+ MHD_OPTION_NOTIFY_COMPLETED,
+ NULL, NULL,
+ MHD_OPTION_URI_LOG_CALLBACK, &mhd_log_callback, NULL,
+ MHD_OPTION_END);
#endif
- &accept_cb, NULL,
- &create_response, hd,
-#if !HAVE_MHD_NO_LISTEN_SOCKET
- MHD_OPTION_LISTEN_SOCKET, GNUNET_NETWORK_get_fd (mhd_unix_socket),
-#endif
- MHD_OPTION_CONNECTION_LIMIT, MHD_MAX_CONNECTIONS,
- MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) 16,
- MHD_OPTION_NOTIFY_COMPLETED,
- NULL, NULL,
- MHD_OPTION_URI_LOG_CALLBACK, &mhd_log_callback, NULL,
- MHD_OPTION_END);
-
- GNUNET_assert (httpd != NULL);
+ GNUNET_break (httpd != NULL);
hd->daemon = httpd;
hd->httpd_task = GNUNET_SCHEDULER_NO_TASK;
-
GNUNET_CONTAINER_DLL_insert (mhd_httpd_head, mhd_httpd_tail, hd);
-
run_httpds ();
-
GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
&do_shutdown, NULL);
-
}
{
static const struct GNUNET_GETOPT_CommandLineOption options[] = {
{'p', "port", NULL,
- gettext_noop ("listen on specified port"), 1,
- &GNUNET_GETOPT_set_string, &port},
+ gettext_noop ("listen on specified port (default: 7777)"), 1,
+ &GNUNET_GETOPT_set_ulong, &port},
{'a', "authority", NULL,
gettext_noop ("pem file to use as CA"), 1,
&GNUNET_GETOPT_set_string, &cafile_opt},
GNUNET_GETOPT_OPTION_END
};
-
int ret;
if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
return 2;
-
-
GNUNET_log_setup ("gnunet-gns-proxy", "WARNING", NULL);
ret =
(GNUNET_OK ==
_("GNUnet GNS proxy"),
options,
&run, NULL)) ? 0 : 1;
- GNUNET_free_non_null ((char*)argv);
-
+ GNUNET_free_non_null ((char *) argv);
return ret;
}
-
-
-
+/* end of gnunet-gns-proxy.c */