Discard large metadata items first
[oweals/gnunet.git] / src / gns / gnunet-gns-proxy.c
index bef3c75e0f1e8834d71799ef63997d082899f079..7b783787b7586967d616dace262be000140f9a13 100644 (file)
      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 <microhttpd.h>
 #include <curl/curl.h>
@@ -30,7 +36,7 @@
 #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
@@ -478,57 +484,6 @@ is_tld (const char* name, const char* tld)
 }
 
 
-/**
- * convert integer to string representation
- *
- * @param i integer
- * @return the character
- */
-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
- * @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,
                   enum MHD_ValueKind kind,
@@ -604,9 +559,14 @@ con_post_data_iter (void *cls,
 
   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)
     {
@@ -618,7 +578,7 @@ con_post_data_iter (void *cls,
     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",
@@ -630,13 +590,18 @@ con_post_data_iter (void *cls,
   }
 
   /* 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",
@@ -741,6 +706,7 @@ mhd_content_cb (void *cls,
                 char* buf,
                 size_t max);
 
+
 /**
  * Check HTTP response header for mime
  *
@@ -837,14 +803,11 @@ curl_check_hdr (void *buffer, size_t size, size_t nmemb, void *cls)
                    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);
@@ -966,6 +929,7 @@ curl_check_hdr (void *buffer, size_t size, size_t nmemb, void *cls)
   return bytes;
 }
 
+
 /**
  * schedule mhd
  *
@@ -982,6 +946,7 @@ run_httpd (struct MhdHttpList *hd);
 static void
 run_httpds (void);
 
+
 /**
  * Task run whenever HTTP server operations are pending.
  *
@@ -992,6 +957,7 @@ static void
 do_httpd (void *cls,
           const struct GNUNET_SCHEDULER_TaskContext *tc);
 
+
 static void
 run_mhd_now (struct MhdHttpList *hd)
 {
@@ -1006,12 +972,14 @@ 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 (void);
 
+
 /**
  * Callback to free content
  *
@@ -1189,11 +1157,7 @@ mhd_content_cb (void *cls,
   }
   
   if (bytes_to_copy + copied > max)
-    bytes_to_copy = max-copied;
-
-  if (0 > bytes_to_copy)
-    bytes_to_copy = 0;
-  
+    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;
@@ -1336,6 +1300,7 @@ postprocess_buffer (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
   run_mhd_now (ctask->mhd);
 }
 
+
 /**
  * Handle data from cURL
  *
@@ -1495,6 +1460,7 @@ post_read_callback (void *buf, size_t size, size_t nmemb, void *cls)
   return to_copy;
 }
 
+
 /**
  * Task that is run when we are ready to receive more data
  * from curl
@@ -1505,6 +1471,7 @@ post_read_callback (void *buf, size_t size, size_t nmemb, void *cls)
 static void
 curl_task_download (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
 
+
 /**
  * Ask cURL for the select sets and schedule download
  */
@@ -1525,9 +1492,7 @@ curl_download_prepare ()
   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",
@@ -1536,23 +1501,20 @@ curl_download_prepare ()
     //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 =
@@ -1590,7 +1552,6 @@ curl_task_download (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
   struct ProxyCurlTask *ctask;
   int num_ctasks;
   long resp_code;
-
   struct ProxyCurlTask *clean_head = NULL;
   struct ProxyCurlTask *clean_tail = NULL;
 
@@ -1600,8 +1561,8 @@ curl_task_download (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
   {
     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");
@@ -1858,6 +1819,7 @@ process_leho_lookup (void *cls,
 
 }
 
+
 /**
  * Initialize download and trigger curl
  *
@@ -1894,6 +1856,7 @@ process_get_authority (void *cls,
                           ctask);
 }
 
+
 static void*
 mhd_log_callback (void* cls, const char* url)
 {
@@ -1939,7 +1902,7 @@ create_response (void *cls,
                  void **con_cls)
 {
   struct MhdHttpList* hd = cls;
-  const char* page = "<html><head><title>gnunet-gns-proxy</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!
@@ -2198,6 +2161,10 @@ run_httpds ()
 
 }
 
+
+#define UNSIGNED_MHD_LONG_LONG unsigned MHD_LONG_LONG
+
+
 /**
  * schedule mhd
  *
@@ -2214,7 +2181,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);
@@ -2272,7 +2239,6 @@ do_httpd (void *cls,
 }
 
 
-
 /**
  * Read data from socket
  *
@@ -2282,6 +2248,7 @@ do_httpd (void *cls,
 static void
 do_read (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
 
+
 /**
  * Read from remote end
  *
@@ -2291,6 +2258,7 @@ do_read (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
 static void
 do_read_remote (void* cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
 
+
 /**
  * Write data to remote socket
  *
@@ -2358,6 +2326,7 @@ cleanup_s5r (struct Socks5Request *s5r)
   GNUNET_free(s5r);
 }
 
+
 /**
  * Write data to socket
  *
@@ -2382,13 +2351,11 @@ do_write (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
                 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;
   }
 
@@ -2406,6 +2373,7 @@ do_write (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
                                      &do_read_remote, s5r);
 }
 
+
 /**
  * Read from remote end
  *
@@ -2418,8 +2386,6 @@ do_read_remote (void* cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
   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,
@@ -2431,7 +2397,7 @@ do_read_remote (void* cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
   }
   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)
@@ -2449,8 +2415,7 @@ do_read_remote (void* cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
   
   s5r->wtask = GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
                                                s5r->sock,
-                                               &do_write, s5r);
-  
+                                               &do_write, s5r);  
 }
 
 
@@ -2475,6 +2440,7 @@ add_handle_to_mhd (struct GNUNET_NETWORK_Handle *h, struct MHD_Daemon *daemon)
   return MHD_add_connection (daemon, fd, addr, len);
 }
 
+
 /**
  * Read file in filename
  *
@@ -2648,13 +2614,13 @@ generate_gns_certificate (const char *name)
 }
 
 
-/*
+/**
  * 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)
@@ -2687,7 +2653,7 @@ 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;
 
@@ -2708,29 +2674,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;
     
@@ -2748,7 +2721,6 @@ add_handle_to_ssl_mhd (struct GNUNET_NETWORK_Handle *h, const char* domain)
 }
 
 
-
 /**
  * Read data from incoming connection
  *
@@ -2763,7 +2735,6 @@ do_read (void* cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
   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;
@@ -2772,7 +2743,6 @@ do_read (void* cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
   uint32_t remote_ip;
   struct sockaddr_in remote_addr;
   struct in_addr *r_sin_addr;
-
   struct NetworkHandleList *nh;
 
   s5r->rtask = GNUNET_SCHEDULER_NO_TASK;
@@ -3025,10 +2995,7 @@ do_read (void* cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
                                        s5r->remote_sock,
                                        &do_read_remote, s5r);
     }
-
-
   }
-
 }
 
 
@@ -3085,7 +3052,6 @@ static void
 do_shutdown (void *cls,
              const struct GNUNET_SCHEDULER_TaskContext *tc)
 {
-
   struct MhdHttpList *hd;
   struct MhdHttpList *tmp_hd;
   struct NetworkHandleList *nh;
@@ -3096,9 +3062,6 @@ do_shutdown (void *cls,
   
   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)
@@ -3127,13 +3090,11 @@ do_shutdown (void *cls,
       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;
@@ -3185,12 +3146,11 @@ do_shutdown (void *cls,
       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 ();
 }
 
 
@@ -3228,9 +3188,9 @@ static int
 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",
@@ -3323,6 +3283,7 @@ load_local_zone_key (const struct GNUNET_CONFIGURATION_Handle *cfg)
   return GNUNET_YES;
 }
 
+
 /**
  * Main function that will be run
  *
@@ -3345,9 +3306,7 @@ run (void *cls, char *const *args, const char *cfgfile,
   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");
@@ -3360,8 +3319,8 @@ run (void *cls, char *const *args, const char *cfgfile,
   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");
@@ -3392,19 +3351,16 @@ run (void *cls, char *const *args, const char *cfgfile,
   
   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;
   }
 
@@ -3439,13 +3395,9 @@ run (void *cls, char *const *args, const char *cfgfile,
                 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,
@@ -3456,10 +3408,6 @@ run (void *cls, char *const *args, const char *cfgfile,
   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;
 #if ! HAVE_MHD_NO_LISTEN_SOCKET
   if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "gns-proxy",
                                                             "PROXY_UNIXPATH",
@@ -3469,12 +3417,9 @@ run (void *cls, char *const *args, const char *cfgfile,
                 "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");
@@ -3492,7 +3437,6 @@ run (void *cls, char *const *args, const char *cfgfile,
 #endif
 
   len = strlen (proxy_sockfile) + sizeof(AF_UNIX);
-
   GNUNET_free (proxy_sockfile);
 
   if (GNUNET_OK != GNUNET_NETWORK_socket_bind (mhd_unix_socket,
@@ -3516,36 +3460,38 @@ 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);
+  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);
-
 }
 
 
@@ -3568,13 +3514,10 @@ main (int argc, char *const *argv)
       &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 ==
@@ -3582,11 +3525,8 @@ main (int argc, char *const *argv)
                            _("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 */