Merge branch 'identity_oidc' of git-int.aisec.fraunhofer.de:sas/gnunet-mirror into...
[oweals/gnunet.git] / src / gns / gnunet-gns-proxy.c
index af276cb3ab9e33b4fb1ba104c54848c28b39030d..3af571eba90a2e7ceb7bf18ca3a5be1d580c8b25 100644 (file)
@@ -430,6 +430,31 @@ enum SocksPhase
 };
 
 
+/**
+ * A header list
+ */
+struct HttpResponseHeader
+{
+  /**
+   * DLL
+   */
+  struct HttpResponseHeader *next;
+
+  /**
+   * DLL
+   */
+  struct HttpResponseHeader *prev;
+
+  /**
+   * Header type
+   */
+  char *type;
+
+  /**
+   * Header value
+   */
+  char *value;
+};
 
 /**
  * A structure for socks requests
@@ -572,6 +597,20 @@ struct Socks5Request
    */
   uint16_t port;
 
+  /**
+   * Headers from response
+   */
+  struct HttpResponseHeader *header_head;
+
+  /**
+   * Headers from response
+   */
+  struct HttpResponseHeader *header_tail;
+
+  /**
+   * SSL Certificate status
+   */
+  int ssl_checked;
 };
 
 
@@ -582,7 +621,7 @@ struct Socks5Request
 /**
  * The port the proxy is running on (default 7777)
  */
-static unsigned long port = GNUNET_GNS_PROXY_PORT;
+static unsigned long long port = GNUNET_GNS_PROXY_PORT;
 
 /**
  * The CA file (pem) to use for the proxy CA
@@ -655,16 +694,6 @@ static struct Socks5Request *s5r_tail;
  */
 static struct GNUNET_CRYPTO_EcdsaPublicKey local_gns_zone;
 
-/**
- * The users local shorten zone
- */
-static struct GNUNET_CRYPTO_EcdsaPrivateKey local_shorten_zone;
-
-/**
- * Is shortening enabled?
- */
-static int do_shorten;
-
 /**
  * The CA for SSL certificate generation
  */
@@ -757,6 +786,8 @@ cleanup_s5r (struct Socks5Request *s5r)
 
 /* ************************* HTTP handling with cURL *********************** */
 
+static void
+curl_download_prepare ();
 
 /**
  * Callback for MHD response generation.  This function is called from
@@ -795,6 +826,11 @@ mhd_content_cb (void *cls,
   {
     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                "Pausing MHD download, no data available\n");
+    if (NULL != s5r->curl)
+    {
+      curl_easy_pause (s5r->curl, CURLPAUSE_CONT);
+      curl_download_prepare ();
+    }
     return 0; /* more data later */
   }
   if ( (0 == bytes_to_copy) &&
@@ -804,6 +840,8 @@ mhd_content_cb (void *cls,
                "Completed MHD download\n");
     return MHD_CONTENT_READER_END_OF_STREAM;
   }
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+              "Writing %lu/%lu bytes\n", bytes_to_copy, s5r->io_len);
   GNUNET_memcpy (buf, s5r->io_buf, bytes_to_copy);
   memmove (s5r->io_buf,
           &s5r->io_buf[bytes_to_copy],
@@ -835,6 +873,9 @@ check_ssl_certificate (struct Socks5Request *s5r)
   int rc;
   const char *name;
 
+  s5r->ssl_checked = GNUNET_YES;
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+              "Checking SSL certificate\n");
   if (CURLE_OK !=
       curl_easy_getinfo (s5r->curl,
                         CURLINFO_TLS_SESSION,
@@ -986,12 +1027,12 @@ static size_t
 curl_check_hdr (void *buffer, size_t size, size_t nmemb, void *cls)
 {
   struct Socks5Request *s5r = cls;
+  struct HttpResponseHeader *header;
   size_t bytes = size * nmemb;
   char *ndup;
   const char *hdr_type;
   const char *cookie_domain;
   char *hdr_val;
-  long resp_code;
   char *new_cookie_hdr;
   char *new_location;
   size_t offset;
@@ -999,48 +1040,12 @@ curl_check_hdr (void *buffer, size_t size, size_t nmemb, void *cls)
   int domain_matched;
   char *tok;
 
-  if (NULL == s5r->response)
+  /* first, check SSL certificate */
+  if ((GNUNET_YES != s5r->ssl_checked) &&
+      (HTTPS_PORT == s5r->port))
   {
-    /* first, check SSL certificate */
-    if ( (HTTPS_PORT == s5r->port) &&
-        (GNUNET_OK != check_ssl_certificate (s5r)) )
-      return 0;
-
-    GNUNET_break (CURLE_OK ==
-                 curl_easy_getinfo (s5r->curl,
-                                    CURLINFO_RESPONSE_CODE,
-                                    &resp_code));
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-               "Creating MHD response with code %d\n",
-               (int) resp_code);
-    s5r->response_code = resp_code;
-    s5r->response = MHD_create_response_from_callback (MHD_SIZE_UNKNOWN,
-                                                      IO_BUFFERSIZE,
-                                                      &mhd_content_cb,
-                                                      s5r,
-                                                      NULL);
-    if (NULL != s5r->leho)
-    {
-      char *cors_hdr;
-
-      GNUNET_asprintf (&cors_hdr,
-                      (HTTPS_PORT == s5r->port)
-                      ? "https://%s"
-                      : "http://%s",
-                      s5r->leho);
-
-      GNUNET_break (MHD_YES ==
-                   MHD_add_response_header (s5r->response,
-                                            MHD_HTTP_HEADER_ACCESS_CONTROL_ALLOW_ORIGIN,
-                                            cors_hdr));
-      GNUNET_free (cors_hdr);
-    }
-    /* force connection to be closed after each request, as we
-       do not support HTTP pipelining (yet, FIXME!) */
-    /*GNUNET_break (MHD_YES ==
-                 MHD_add_response_header (s5r->response,
-                                          MHD_HTTP_HEADER_CONNECTION,
-                                          "close"));*/
+      if (GNUNET_OK != check_ssl_certificate (s5r))
+        return 0;
   }
 
   ndup = GNUNET_strndup (buffer, bytes);
@@ -1063,41 +1068,41 @@ curl_check_hdr (void *buffer, size_t size, size_t nmemb, void *cls)
   new_cookie_hdr = NULL;
   if ( (NULL != s5r->leho) &&
        (0 == strcasecmp (hdr_type,
-                        MHD_HTTP_HEADER_SET_COOKIE)) )
+                         MHD_HTTP_HEADER_SET_COOKIE)) )
 
   {
     new_cookie_hdr = GNUNET_malloc (strlen (hdr_val) +
-                                   strlen (s5r->domain) + 1);
+                                    strlen (s5r->domain) + 1);
     offset = 0;
     domain_matched = GNUNET_NO; /* make sure we match domain at most once */
     for (tok = strtok (hdr_val, ";"); NULL != tok; tok = strtok (NULL, ";"))
     {
       if ( (0 == strncasecmp (tok, " domain", strlen (" domain"))) &&
-          (GNUNET_NO == domain_matched) )
+           (GNUNET_NO == domain_matched) )
       {
-       domain_matched = GNUNET_YES;
+        domain_matched = GNUNET_YES;
         cookie_domain = tok + strlen (" domain") + 1;
         if (strlen (cookie_domain) < strlen (s5r->leho))
         {
           delta_cdomain = strlen (s5r->leho) - strlen (cookie_domain);
           if (0 == strcasecmp (cookie_domain, s5r->leho + delta_cdomain))
-         {
+          {
             offset += sprintf (new_cookie_hdr + offset,
-                              " domain=%s;",
-                              s5r->domain);
+                               " domain=%s;",
+                               s5r->domain);
             continue;
           }
         }
         else if (0 == strcmp (cookie_domain, s5r->leho))
         {
-         offset += sprintf (new_cookie_hdr + offset,
-                            " domain=%s;",
-                            s5r->domain);
-         continue;
+          offset += sprintf (new_cookie_hdr + offset,
+                             " domain=%s;",
+                             s5r->domain);
+          continue;
         }
         GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
                     _("Cookie domain `%s' supplied by server is invalid\n"),
-                   tok);
+                    tok);
       }
       GNUNET_memcpy (new_cookie_hdr + offset, tok, strlen (tok));
       offset += strlen (tok);
@@ -1112,21 +1117,21 @@ curl_check_hdr (void *buffer, size_t size, size_t nmemb, void *cls)
     char *leho_host;
 
     GNUNET_asprintf (&leho_host,
-                    (HTTPS_PORT != s5r->port)
-                    ? "http://%s"
-                    : "https://%s",
-                    s5r->leho);
+                     (HTTPS_PORT != s5r->port)
+                     ? "http://%s"
+                     : "https://%s",
+                     s5r->leho);
     if (0 == strncmp (leho_host,
-                     hdr_val,
-                     strlen (leho_host)))
+                      hdr_val,
+                      strlen (leho_host)))
     {
       GNUNET_asprintf (&new_location,
-                      "%s%s%s",
-                      (HTTPS_PORT != s5r->port)
-                      ? "http://"
-                      : "https://",
-                      s5r->domain,
-                      hdr_val + strlen (leho_host));
+                       "%s%s%s",
+                       (HTTPS_PORT != s5r->port)
+                       ? "http://"
+                       : "https://",
+                       s5r->domain,
+                       hdr_val + strlen (leho_host));
       hdr_val = new_location;
     }
     GNUNET_free (leho_host);
@@ -1138,17 +1143,18 @@ curl_check_hdr (void *buffer, size_t size, size_t nmemb, void *cls)
     *tok = '\0';
   if (NULL != (tok = strchr (hdr_val, '\t')))
     *tok = '\0';
-  if ((0 != strlen (hdr_val) ) &&
-      (0 != strcasecmp (MHD_HTTP_HEADER_CONTENT_LENGTH, hdr_type)))
+  if (0 != strlen (hdr_val)) /* Rely in MHD to set those */
   {
     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-               "Adding header %s: %s to MHD response\n",
-               hdr_type,
-               hdr_val);
-    GNUNET_break (MHD_YES ==
-                 MHD_add_response_header (s5r->response,
-                                          hdr_type,
-                                          hdr_val));
+                "Adding header %s: %s to MHD response\n",
+                hdr_type,
+                hdr_val);
+    header = GNUNET_new (struct HttpResponseHeader);
+    header->type = GNUNET_strndup (hdr_type, strlen (hdr_type));
+    header->value = GNUNET_strndup (hdr_val, strlen (hdr_val));
+    GNUNET_CONTAINER_DLL_insert (s5r->header_head,
+                                 s5r->header_tail,
+                                 header);
   }
   GNUNET_free (ndup);
   GNUNET_free_non_null (new_cookie_hdr);
@@ -1156,6 +1162,69 @@ curl_check_hdr (void *buffer, size_t size, size_t nmemb, void *cls)
   return bytes;
 }
 
+static int
+create_mhd_response_from_s5r (struct Socks5Request *s5r)
+{
+  long resp_code;
+  double content_length;
+  struct HttpResponseHeader *header;
+
+  if (NULL != s5r->response)
+  {
+    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+                "Response already set!\n");
+    return GNUNET_SYSERR;
+  }
+
+  GNUNET_break (CURLE_OK ==
+                curl_easy_getinfo (s5r->curl,
+                                   CURLINFO_RESPONSE_CODE,
+                                   &resp_code));
+  GNUNET_break (CURLE_OK ==
+                curl_easy_getinfo (s5r->curl,
+                                   CURLINFO_CONTENT_LENGTH_DOWNLOAD,
+                                   &content_length));
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+              "Creating MHD response with code %d and size %d\n",
+              (int) resp_code, (int) content_length);
+  s5r->response_code = resp_code;
+  s5r->response = MHD_create_response_from_callback ((-1 == content_length) ? MHD_SIZE_UNKNOWN : content_length,
+                                                     IO_BUFFERSIZE,
+                                                     &mhd_content_cb,
+                                                     s5r,
+                                                     NULL);
+  for (header = s5r->header_head; NULL != header; header = header->next)
+  {
+    GNUNET_break (MHD_YES ==
+                  MHD_add_response_header (s5r->response,
+                                           header->type,
+                                           header->value));
+
+  }
+  if (NULL != s5r->leho)
+  {
+    char *cors_hdr;
+
+    GNUNET_asprintf (&cors_hdr,
+                     (HTTPS_PORT == s5r->port)
+                     ? "https://%s"
+                     : "http://%s",
+                     s5r->leho);
+
+    GNUNET_break (MHD_YES ==
+                  MHD_add_response_header (s5r->response,
+                                           MHD_HTTP_HEADER_ACCESS_CONTROL_ALLOW_ORIGIN,
+                                           cors_hdr));
+    GNUNET_free (cors_hdr);
+  }
+  /* force connection to be closed after each request, as we
+     do not support HTTP pipelining (yet, FIXME!) */
+  /*GNUNET_break (MHD_YES ==
+    MHD_add_response_header (s5r->response,
+    MHD_HTTP_HEADER_CONNECTION,
+    "close"));*/
+  return GNUNET_OK;
+}
 
 /**
  * Handle response payload data from cURL.  Copies it into our `io_buf` to make
@@ -1173,6 +1242,9 @@ curl_download_cb (void *ptr, size_t size, size_t nmemb, void* ctx)
   struct Socks5Request *s5r = ctx;
   size_t total = size * nmemb;
 
+  if (NULL == s5r->response)
+    GNUNET_assert (GNUNET_OK == create_mhd_response_from_s5r (s5r));
+
   if ( (SOCKS5_SOCKET_UPLOAD_STARTED == s5r->state) ||
        (SOCKS5_SOCKET_UPLOAD_DONE == s5r->state) )
   {
@@ -1180,18 +1252,19 @@ curl_download_cb (void *ptr, size_t size, size_t nmemb, void* ctx)
        start the download, the IO buffer is still full
        with upload data. */
     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-               "Pausing CURL download, waiting for UPLOAD to finish\n");
+                "Pausing CURL download, waiting for UPLOAD to finish\n");
     return CURL_WRITEFUNC_PAUSE; /* not yet ready for data download */
   }
   if (sizeof (s5r->io_buf) - s5r->io_len < total)
   {
     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-               "Pausing CURL download, not enough space\n");
+                "Pausing CURL download, not enough space %lu %lu %lu\n", sizeof (s5r->io_buf),
+                s5r->io_len, total);
     return CURL_WRITEFUNC_PAUSE; /* not enough space */
   }
   GNUNET_memcpy (&s5r->io_buf[s5r->io_len],
-         ptr,
-         total);
+                 ptr,
+                 total);
   s5r->io_len += total;
   if (s5r->io_len == total)
     run_mhd_now (s5r->hd);
@@ -1220,7 +1293,7 @@ curl_upload_cb (void *buf, size_t size, size_t nmemb, void *cls)
        (SOCKS5_SOCKET_UPLOAD_DONE != s5r->state) )
   {
     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-               "Pausing CURL UPLOAD, need more data\n");
+                "Pausing CURL UPLOAD, need more data\n");
     return CURL_READFUNC_PAUSE;
   }
   if ( (0 == s5r->io_len) &&
@@ -1228,7 +1301,7 @@ curl_upload_cb (void *buf, size_t size, size_t nmemb, void *cls)
   {
     s5r->state = SOCKS5_SOCKET_DOWNLOAD_STARTED;
     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-               "Completed CURL UPLOAD\n");
+                "Completed CURL UPLOAD\n");
     return 0; /* upload finished, can now download */
   }
   if ( (SOCKS5_SOCKET_UPLOAD_STARTED != s5r->state) &&
@@ -1238,11 +1311,11 @@ curl_upload_cb (void *buf, size_t size, size_t nmemb, void *cls)
     return CURL_READFUNC_ABORT;
   }
   to_copy = GNUNET_MIN (s5r->io_len,
-                       len);
+                        len);
   GNUNET_memcpy (buf, s5r->io_buf, to_copy);
   memmove (s5r->io_buf,
-          &s5r->io_buf[to_copy],
-          s5r->io_len - to_copy);
+           &s5r->io_buf[to_copy],
+           s5r->io_len - to_copy);
   s5r->io_len -= to_copy;
   if (s5r->io_len + to_copy == sizeof (s5r->io_buf))
     run_mhd_now (s5r->hd); /* got more space for upload now */
@@ -1309,9 +1382,9 @@ curl_download_prepare ()
     GNUNET_NETWORK_fdset_copy_native (grs, &rs, max + 1);
     GNUNET_NETWORK_fdset_copy_native (gws, &ws, max + 1);
     curl_download_task = GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
-                                                     rtime,
-                                                     grs, gws,
-                                                     &curl_task_download, curl_multi);
+                                                      rtime,
+                                                      grs, gws,
+                                                      &curl_task_download, curl_multi);
     GNUNET_NETWORK_fdset_destroy (gws);
     GNUNET_NETWORK_fdset_destroy (grs);
   }
@@ -1367,6 +1440,8 @@ curl_task_download (void *cls)
             case CURLE_GOT_NOTHING:
               GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                           "CURL download completed.\n");
+              if (NULL == s5r->response)
+                GNUNET_assert (GNUNET_OK == create_mhd_response_from_s5r (s5r));
               s5r->state = SOCKS5_SOCKET_DOWNLOAD_DONE;
               run_mhd_now (s5r->hd);
               break;
@@ -1538,7 +1613,7 @@ create_response (void *cls,
         }
         GNUNET_snprintf (ipaddr,
                          sizeof (ipaddr),
-                         "[%s]",
+                         "%s",
                          ipstring);
         port = ntohs (s6->sin6_port);
         break;
@@ -1605,8 +1680,8 @@ create_response (void *cls,
     else if (0 == strcasecmp (meth, MHD_HTTP_METHOD_POST))
     {
       s5r->state = SOCKS5_SOCKET_UPLOAD_STARTED;
-      curl_easy_setopt (s5r->curl, CURLOPT_POST, 1);
-      curl_easy_setopt (s5r->curl, CURLOPT_WRITEFUNCTION, &curl_download_cb);
+      curl_easy_setopt (s5r->curl, CURLOPT_POST, 1L);
+            curl_easy_setopt (s5r->curl, CURLOPT_WRITEFUNCTION, &curl_download_cb);
       curl_easy_setopt (s5r->curl, CURLOPT_WRITEDATA, s5r);
       curl_easy_setopt (s5r->curl, CURLOPT_READFUNCTION, &curl_upload_cb);
       curl_easy_setopt (s5r->curl, CURLOPT_READDATA, s5r);
@@ -1654,7 +1729,10 @@ create_response (void *cls,
     if (HTTPS_PORT == s5r->port)
     {
       curl_easy_setopt (s5r->curl, CURLOPT_USE_SSL, CURLUSESSL_ALL);
-      curl_easy_setopt (s5r->curl, CURLOPT_SSL_VERIFYPEER, 1L);
+      if (NULL != s5r->dane_data)
+        curl_easy_setopt (s5r->curl, CURLOPT_SSL_VERIFYPEER, 0L);
+      else
+        curl_easy_setopt (s5r->curl, CURLOPT_SSL_VERIFYPEER, 1L);
       /* Disable cURL checking the hostname, as we will check ourselves
          as only we have the domain name or the LEHO or the DANE record */
       curl_easy_setopt (s5r->curl, CURLOPT_SSL_VERIFYHOST, 0L);
@@ -1674,10 +1752,6 @@ create_response (void *cls,
     MHD_get_connection_values (con,
                                MHD_HEADER_KIND,
                                &con_val_iter, s5r);
-    //TODO is this sane? Basically we disable cURLs built-in expect:
-    //100-continue
-    s5r->headers = curl_slist_append (s5r->headers,
-                                      "Expect:");
     curl_easy_setopt (s5r->curl, CURLOPT_HTTPHEADER, s5r->headers);
     curl_download_prepare ();
     return MHD_YES;
@@ -1686,8 +1760,16 @@ create_response (void *cls,
   /* continuing to process request */
   if (0 != *upload_data_size)
   {
+
     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                "Processing %lu bytes UPLOAD\n", *upload_data_size);
+                "Processing %u bytes UPLOAD\n",
+               (unsigned int) *upload_data_size);
+
+    /* FIXME: This must be set or a header with Transfer-Encoding: chunked. Else
+     * upload callback is not called!
+     */
+    curl_easy_setopt (s5r->curl, CURLOPT_POSTFIELDSIZE, *upload_data_size);
+
     left = GNUNET_MIN (*upload_data_size,
                        sizeof (s5r->io_buf) - s5r->io_len);
     GNUNET_memcpy (&s5r->io_buf[s5r->io_len],
@@ -1697,7 +1779,6 @@ create_response (void *cls,
     *upload_data_size -= left;
     GNUNET_assert (NULL != s5r->curl);
     curl_easy_pause (s5r->curl, CURLPAUSE_CONT);
-    curl_download_prepare ();
     return MHD_YES;
   }
   if (SOCKS5_SOCKET_UPLOAD_STARTED == s5r->state)
@@ -1707,7 +1788,7 @@ create_response (void *cls,
     s5r->state = SOCKS5_SOCKET_UPLOAD_DONE;
   }
   if (NULL == s5r->response)
-    return MHD_YES; /* too early to queue response, did not yet get headers from cURL */
+    return MHD_YES;
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
               "Queueing response with MHD\n");
   run_mhd_now (s5r->hd);
@@ -1736,6 +1817,7 @@ mhd_completed_cb (void *cls,
                   enum MHD_RequestTerminationCode toe)
 {
   struct Socks5Request *s5r = *con_cls;
+  struct HttpResponseHeader *header;
 
   if (NULL == s5r)
     return;
@@ -1758,6 +1840,15 @@ mhd_completed_cb (void *cls,
   if ( (NULL != s5r->response) &&
        (curl_failure_response != s5r->response) )
     MHD_destroy_response (s5r->response);
+  for (header = s5r->header_head; header != NULL; header = s5r->header_head)
+  {
+    GNUNET_CONTAINER_DLL_remove (s5r->header_head,
+                                 s5r->header_tail,
+                                 header);
+    GNUNET_free (header->type);
+    GNUNET_free (header->value);
+    GNUNET_free (header);
+  }
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Finished request for %s\n", s5r->url);
   GNUNET_free (s5r->url);
   s5r->state = SOCKS5_SOCKET_WITH_MHD;
@@ -1786,34 +1877,47 @@ mhd_connection_cb (void *cls,
   const union MHD_ConnectionInfo *ci;
   int sock;
 
-  if (MHD_CONNECTION_NOTIFY_STARTED == cnc)
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connection started...\n");
-
-  if (MHD_CONNECTION_NOTIFY_CLOSED != cnc)
-    return; //Ignore
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connection closed... cleaning up\n");
-
-  ci = MHD_get_connection_info (connection,
-                                MHD_CONNECTION_INFO_CONNECTION_FD);
-  if (NULL == ci)
+  switch (cnc)
   {
-    GNUNET_break (0);
-    return;
-  }
-
-  sock = ci->connect_fd;
-  for (s5r = s5r_head; NULL != s5r; s5r = s5r->next)
-    if (GNUNET_NETWORK_get_fd (s5r->sock) == sock)
+    case MHD_CONNECTION_NOTIFY_STARTED:
+      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connection started...\n");
+      ci = MHD_get_connection_info (connection,
+                                    MHD_CONNECTION_INFO_CONNECTION_FD);
+      if (NULL == ci)
+      {
+        GNUNET_break (0);
+        return;
+      }
+      sock = ci->connect_fd;
+      for (s5r = s5r_head; NULL != s5r; s5r = s5r->next)
+      {
+        if (GNUNET_NETWORK_get_fd (s5r->sock) == sock)
+        {
+          GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                      "Context set...\n");
+          s5r->ssl_checked = GNUNET_NO;
+          *con_cls = s5r;
+          break;
+        }
+      }
       break;
-
-  if (NULL == s5r)
-  {
-    GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Connection stale!\n");
-    return;
+    case MHD_CONNECTION_NOTIFY_CLOSED:
+      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                  "Connection closed... cleaning up\n");
+      s5r = *con_cls;
+      if (NULL == s5r)
+      {
+        GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+                    "Connection stale!\n");
+        return;
+      }
+      cleanup_s5r (s5r);
+      curl_download_prepare ();
+      *con_cls = NULL;
+      break;
+    default:
+      GNUNET_break (0);
   }
-  cleanup_s5r (s5r);
-  curl_download_prepare ();
-  *con_cls = NULL;
 }
 
 /**
@@ -1836,36 +1940,27 @@ mhd_log_callback (void *cls,
 {
   struct Socks5Request *s5r;
   const union MHD_ConnectionInfo *ci;
-  int sock;
 
   ci = MHD_get_connection_info (connection,
-                                MHD_CONNECTION_INFO_CONNECTION_FD);
+                                MHD_CONNECTION_INFO_SOCKET_CONTEXT);
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Processing %s\n", url);
   if (NULL == ci)
   {
     GNUNET_break (0);
     return NULL;
   }
-  sock = ci->connect_fd;
-  for (s5r = s5r_head; NULL != s5r; s5r = s5r->next)
+  s5r = ci->socket_context;
+  if (NULL != s5r->url)
   {
-    if (GNUNET_NETWORK_get_fd (s5r->sock) == sock)
-    {
-      if (NULL != s5r->url)
-      {
-        GNUNET_break (0);
-        return NULL;
-      }
-      s5r->url = GNUNET_strdup (url);
-      if (NULL != s5r->timeout_task)
-        GNUNET_SCHEDULER_cancel (s5r->timeout_task);
-      s5r->timeout_task = NULL;
-      GNUNET_assert (s5r->state == SOCKS5_SOCKET_WITH_MHD);
-      return s5r;
-    }
+    GNUNET_break (0);
+    return NULL;
   }
-  GNUNET_break (0);
-  return NULL;
+  s5r->url = GNUNET_strdup (url);
+  if (NULL != s5r->timeout_task)
+    GNUNET_SCHEDULER_cancel (s5r->timeout_task);
+  s5r->timeout_task = NULL;
+  GNUNET_assert (s5r->state == SOCKS5_SOCKET_WITH_MHD);
+  return s5r;
 }
 
 
@@ -2329,6 +2424,8 @@ do_write (void *cls)
   if (len <= 0)
   {
     /* write error: connection closed, shutdown, etc.; just clean up */
+    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+                "Write Error\n");
     cleanup_s5r (s5r);
     return;
   }
@@ -2736,7 +2833,6 @@ do_s5r_read (void *cls)
                                                  &local_gns_zone,
                                                  GNUNET_DNSPARSER_TYPE_A,
                                                  GNUNET_NO /* only cached */,
-                                                 (GNUNET_YES == do_shorten) ? &local_shorten_zone : NULL,
                                                  &handle_gns_result,
                                                  s5r);
             break;
@@ -3024,7 +3120,7 @@ run_cont ()
     return;
   }
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-              "Proxy listens on port %lu\n",
+              "Proxy listens on port %llu\n",
               port);
 
   /* start MHD daemon for HTTP */
@@ -3049,46 +3145,6 @@ run_cont ()
 }
 
 
-/**
- * Method called to inform about the egos of the shorten zone of this peer.
- *
- * When used with #GNUNET_IDENTITY_create or #GNUNET_IDENTITY_get,
- * this function is only called ONCE, and 'NULL' being passed in
- * @a ego does indicate an error (i.e. name is taken or no default
- * value is known).  If @a ego is non-NULL and if '*ctx'
- * is set in those callbacks, the value WILL be passed to a subsequent
- * call to the identity callback of #GNUNET_IDENTITY_connect (if
- * that one was not NULL).
- *
- * @param cls closure, NULL
- * @param ego ego handle
- * @param ctx context for application to store data for this ego
- *                 (during the lifetime of this process, initially NULL)
- * @param name name assigned by the user for this ego,
- *                   NULL if the user just deleted the ego and it
- *                   must thus no longer be used
- */
-static void
-identity_shorten_cb (void *cls,
-                     struct GNUNET_IDENTITY_Ego *ego,
-                     void **ctx,
-                     const char *name)
-{
-  id_op = NULL;
-  if (NULL == ego)
-  {
-    GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
-                _("No ego configured for `shorten-zone`\n"));
-  }
-  else
-  {
-    local_shorten_zone = *GNUNET_IDENTITY_ego_get_private_key (ego);
-    do_shorten = GNUNET_YES;
-  }
-  run_cont ();
-}
-
-
 /**
  * Method called to inform about the egos of the master zone of this peer.
  *
@@ -3125,10 +3181,7 @@ identity_master_cb (void *cls,
   }
   GNUNET_IDENTITY_ego_get_public_key (ego,
                                       &local_gns_zone);
-  id_op = GNUNET_IDENTITY_get (identity,
-                               "gns-short",
-                               &identity_shorten_cb,
-                               NULL);
+  run_cont ();
 }
 
 
@@ -3141,7 +3194,9 @@ identity_master_cb (void *cls,
  * @param c configuration
  */
 static void
-run (void *cls, char *const *args, const char *cfgfile,
+run (void *cls,
+     char *const *args,
+     const char *cfgfile,
      const struct GNUNET_CONFIGURATION_Handle *c)
 {
   char* cafile_cfg = NULL;
@@ -3218,13 +3273,20 @@ run (void *cls, char *const *args, const char *cfgfile,
 int
 main (int argc, char *const *argv)
 {
-  static const struct GNUNET_GETOPT_CommandLineOption options[] = {
-    {'p', "port", NULL,
-      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},
+  struct GNUNET_GETOPT_CommandLineOption options[] = {
+
+    GNUNET_GETOPT_option_ulong ('p',
+                                    "port",
+                                    NULL,
+                                    gettext_noop ("listen on specified port (default: 7777)"),
+                                    &port),
+
+    GNUNET_GETOPT_option_string ('a',
+                                 "authority",
+                                 NULL,
+                                 gettext_noop ("pem file to use as CA"),
+                                 &cafile_opt),
+
     GNUNET_GETOPT_OPTION_END
   };
   static const char* page =
@@ -3232,22 +3294,26 @@ main (int argc, char *const *argv)
     "</head><body>cURL fail</body></html>";
   int ret;
 
-  if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
+  if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv,
+                                                 &argc, &argv))
     return 2;
-  GNUNET_log_setup ("gnunet-gns-proxy", "WARNING", NULL);
-  curl_failure_response = MHD_create_response_from_buffer (strlen (page),
-                                                           (void*)page,
-                                                           MHD_RESPMEM_PERSISTENT);
+  GNUNET_log_setup ("gnunet-gns-proxy",
+                    "WARNING",
+                    NULL);
+  curl_failure_response
+    = MHD_create_response_from_buffer (strlen (page),
+                                       (void *) page,
+                                       MHD_RESPMEM_PERSISTENT);
 
   ret =
     (GNUNET_OK ==
-     GNUNET_PROGRAM_run (argc, argv, "gnunet-gns-proxy",
+     GNUNET_PROGRAM_run (argc, argv,
+                         "gnunet-gns-proxy",
                          _("GNUnet GNS proxy"),
                          options,
                          &run, NULL)) ? 0 : 1;
   MHD_destroy_response (curl_failure_response);
   GNUNET_free_non_null ((char *) argv);
-  GNUNET_CRYPTO_ecdsa_key_clear (&local_shorten_zone);
   return ret;
 }