-more rsa refactoring
[oweals/gnunet.git] / src / gns / gnunet-gns-proxy.c
index ac990eb8380b1d0f0f8cb64fe2cc06cd45a1f495..9c8fde8615f6ec7935402a59c04ef47c495f5e21 100644 (file)
      Free Software Foundation, Inc., 59 Temple Place - Suite 330,
      Boston, MA 02111-1307, USA.
 */
-
 #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 */
@@ -191,15 +191,9 @@ struct ProxyCurlTask
   /* 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;
 
@@ -221,29 +215,29 @@ struct ProxyCurlTask
   /* 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;
@@ -254,18 +248,6 @@ struct ProxyCurlTask
   /* 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;
 
@@ -278,34 +260,52 @@ struct ProxyCurlTask
   /* 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;
   
@@ -322,6 +322,15 @@ struct ProxyREMatch
   /* 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];
 
@@ -334,14 +343,9 @@ struct ProxyREMatch
   /* 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;
 };
 
 /**
@@ -399,10 +403,10 @@ static char* cafile_opt;
 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;
@@ -444,51 +448,54 @@ static struct GNUNET_CRYPTO_ShortHashCode *local_shorten_zone;
 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)
+static 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;
+  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)) );
 }
 
+
 /**
  * convert integer to string representation
  *
  * @param i integer
  * @return the character
  */
-char i_to_hexchar (char i)
+static char 
+i_to_hexchar (unsigned char i)
 {
   static char hexmap[] = "0123456789abcdef";
+
   GNUNET_assert (sizeof (hexmap) > (i & 15));
   return hexmap[i & 15];
 }
 
+
 /**
+// FIXME: use cURL API
  * Escape given 0-terminated string
  *
  * @param to_esc string to escapse
@@ -521,6 +528,7 @@ escape_to_urlenc (const char *to_esc)
   return res;
 }
 
+
 static int
 con_post_data_iter (void *cls,
                   enum MHD_ValueKind kind,
@@ -538,9 +546,11 @@ 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,
@@ -551,25 +561,12 @@ con_post_data_iter (void *cls,
     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);
@@ -578,7 +575,8 @@ con_post_data_iter (void *cls,
                                         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;
     }
     
@@ -587,10 +585,10 @@ con_post_data_iter (void *cls,
     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,
@@ -675,7 +673,7 @@ con_val_iter (void *cls,
 
   if (0 == strcmp ("Host", key))
   {
-    port = strstr (value, ":");
+    port = strchr (value, ':');
     if (NULL != port)
     {
       strncpy (buf, value, port-value);
@@ -693,7 +691,7 @@ con_val_iter (void *cls,
     return MHD_YES;
   }
 
-  if (0 == strcmp ("Accept-Encoding", key))
+  if (0 == strcmp (MHD_HTTP_HEADER_ACCEPT_ENCODING, key))
     hdr_val = "";
   else
     hdr_val = value;
@@ -1012,7 +1010,7 @@ run_mhd_now (struct MhdHttpList *hd)
  * Ask cURL for the select sets and schedule download
  */
 static void
-curl_download_prepare ();
+curl_download_prepare (void);
 
 /**
  * Callback to free content
@@ -1040,7 +1038,10 @@ mhd_content_free (void *cls,
   if (NULL != ctask->post_handler)
     MHD_destroy_post_processor (ctask->post_handler);
 
-  for (pdata = ctask->upload_data_head;; NULL != pdata; pdata = ctask->upload_data_head)
+  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,
@@ -1073,17 +1074,14 @@ mhd_content_cb (void *cls,
   struct ProxyCurlTask *ctask = cls;
   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);
@@ -1096,8 +1094,7 @@ mhd_content_cb (void *cls,
   
   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");
@@ -1118,26 +1115,24 @@ mhd_content_cb (void *cls,
                 "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;
 
@@ -1146,7 +1141,7 @@ mhd_content_cb (void *cls,
       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;
     }
@@ -1159,7 +1154,7 @@ mhd_content_cb (void *cls,
                   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;
     }
@@ -1167,7 +1162,7 @@ mhd_content_cb (void *cls,
     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,
@@ -1179,12 +1174,10 @@ mhd_content_cb (void *cls,
   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,
@@ -1195,12 +1188,8 @@ mhd_content_cb (void *cls,
     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;
@@ -1210,7 +1199,7 @@ mhd_content_cb (void *cls,
     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;
 }
@@ -1250,7 +1239,7 @@ process_shorten (void* cls, const char* short_name)
               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);
@@ -1299,12 +1288,19 @@ postprocess_buffer (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
 
     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);
@@ -1737,10 +1733,8 @@ curl_task_download (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
     }
     
     num_ctasks=0;
-    for (ctask=ctasks_head; NULL != ctask; ctask = ctask->next)
-    {
-      num_ctasks++;
-    }
+    for (ctask=ctasks_head; NULL != ctask; ctask = ctask->next)    
+      num_ctasks++; 
     
     if (num_ctasks != running)
     {
@@ -1903,10 +1897,10 @@ 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.
  *
@@ -1941,13 +1935,12 @@ create_response (void *cls,
                  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];
@@ -2109,11 +2102,11 @@ create_response (void *cls,
     {
       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;
@@ -2201,6 +2194,10 @@ run_httpds ()
 
 }
 
+
+#define UNSIGNED_MHD_LONG_LONG unsigned MHD_LONG_LONG
+
+
 /**
  * schedule mhd
  *
@@ -2217,7 +2214,7 @@ run_httpd (struct MhdHttpList *hd)
   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);
@@ -2352,8 +2349,7 @@ cleanup_s5r (struct Socks5Request *s5r)
   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))
@@ -2484,13 +2480,13 @@ add_handle_to_mhd (struct GNUNET_NETWORK_Handle *h, struct MHD_Daemon *daemon)
  *
  * @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 !=
@@ -2523,7 +2519,7 @@ load_key_from_file (gnutls_x509_privkey_t key, const char* keyfile)
   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)
@@ -2549,10 +2545,9 @@ static int
 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)
@@ -2575,7 +2570,6 @@ load_cert_from_file (gnutls_x509_crt_t crt, char* certfile)
 static struct ProxyGNSCertificate *
 generate_gns_certificate (const char *name)
 {
-
   int ret;
   unsigned int serial;
   size_t key_buf_size;
@@ -2591,12 +2585,7 @@ generate_gns_certificate (const char *name)
     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");
 
@@ -2607,14 +2596,11 @@ generate_gns_certificate (const char *name)
   
   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));
 
@@ -2722,29 +2708,36 @@ add_handle_to_ssl_mhd (struct GNUNET_NETWORK_Handle *h, const char* domain)
     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;
     
@@ -3474,7 +3467,7 @@ run (void *cls, char *const *args, const char *cfgfile,
   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))
@@ -3530,24 +3523,31 @@ run (void *cls, char *const *args, const char *cfgfile,
   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
-#endif
-            &accept_cb, NULL,
-            &create_response, hd,
-#if !HAVE_MHD_NO_LISTEN_SOCKET
-            MHD_OPTION_LISTEN_SOCKET, GNUNET_NETWORK_get_fd (mhd_unix_socket),
+  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
-            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);
   hd->daemon = httpd;
@@ -3575,8 +3575,8 @@ main (int argc, char *const *argv)
 {
   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},