-use GPLv3+ consistently
[oweals/gnunet.git] / src / gns / gnunet-gns-proxy.c
index 5cef441545a8c9fffda3e5d49d5e0fa58ea42631..3a3678df1f5e039e25f18d37c56a0b99fc431989 100644 (file)
@@ -1,6 +1,6 @@
 /*
      This file is part of GNUnet.
-     (C) 2012 Christian Grothoff (and other contributing authors)
+     (C) 2012-2013 Christian Grothoff (and other contributing authors)
 
      GNUnet is free software; you can redistribute it and/or modify
      it under the terms of the GNU General Public License as published
@@ -20,7 +20,7 @@
 /**
  * @author Martin Schanzenbach
  * @file src/gns/gnunet-gns-proxy.c
- * @brief HTTP(S) proxy that rewrites URIs and fakes certificats to make GADS work
+ * @brief HTTP(S) proxy that rewrites URIs and fakes certificats to make GNS work
  *        with legacy browsers
  */
 #include "platform.h"
@@ -459,7 +459,7 @@ static struct GNUNET_NETWORK_Handle *mhd_unix_socket;
 #endif
 
 /* Shorten zone private key */
-static struct GNUNET_CRYPTO_RsaPrivateKey *shorten_zonekey;
+static struct GNUNET_CRYPTO_EccPrivateKey *shorten_zonekey;
 
 
 /**
@@ -484,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,
@@ -610,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)
     {
@@ -624,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",
@@ -636,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",
@@ -1341,6 +1300,7 @@ postprocess_buffer (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
   run_mhd_now (ctask->mhd);
 }
 
+
 /**
  * Handle data from cURL
  *
@@ -1592,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;
 
@@ -1602,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");
@@ -1792,7 +1751,7 @@ process_leho_lookup (void *cls,
 
   for (i=0; i<rd_count; i++)
   {
-    if (rd[i].record_type != GNUNET_GNS_RECORD_LEHO)
+    if (rd[i].record_type != GNUNET_NAMESTORE_TYPE_LEHO)
       continue;
 
     memcpy (ctask->leho, rd[i].data, rd[i].data_size);
@@ -1890,7 +1849,7 @@ process_get_authority (void *cls,
   GNUNET_GNS_lookup_zone (gns_handle,
                           ctask->host,
                           local_gns_zone,
-                          GNUNET_GNS_RECORD_LEHO,
+                          GNUNET_NAMESTORE_TYPE_LEHO,
                           GNUNET_YES, //Only cached for performance
                           shorten_zonekey,
                           &process_leho_lookup,
@@ -2241,7 +2200,7 @@ run_httpd (struct MhdHttpList *hd)
   haveto = MHD_get_timeout (hd->daemon, &timeout);
 
   if (MHD_YES == haveto)
-    tv.rel_value = (uint64_t) timeout;
+    tv.rel_value_us = (uint64_t) timeout * 1000LL;
   else
     tv = GNUNET_TIME_UNIT_FOREVER_REL;
   GNUNET_NETWORK_fdset_copy_native (wrs, &rs, max + 1);
@@ -2392,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;
   }
 
@@ -2429,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,
@@ -2442,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)
@@ -2460,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);  
 }
 
 
@@ -2666,7 +2620,7 @@ generate_gns_certificate (const char *name)
  * @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)
@@ -2699,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;
 
@@ -2781,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;
@@ -2790,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;
@@ -3100,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;
@@ -3111,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)
@@ -3142,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;
@@ -3200,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 ();
 }
 
 
@@ -3243,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_RsaPublicKeyBinaryEncoded pkey;
-  struct GNUNET_CRYPTO_ShortHashCode *zone = NULL;
+  struct GNUNET_CRYPTO_EccPrivateKey *key;
+  struct GNUNET_CRYPTO_EccPublicKey pkey;
+  struct GNUNET_CRYPTO_ShortHashCode *zone;
   struct GNUNET_CRYPTO_ShortHashAsciiEncoded zonename;
 
   if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "gns",
@@ -3264,17 +3209,17 @@ load_local_zone_key (const struct GNUNET_CONFIGURATION_Handle *cfg)
     return GNUNET_NO;
   }
 
-  key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
-  GNUNET_CRYPTO_rsa_key_get_public (key, &pkey);
+  key = GNUNET_CRYPTO_ecc_key_create_from_file (keyfile);
+  GNUNET_CRYPTO_ecc_key_get_public (key, &pkey);
   local_gns_zone = GNUNET_malloc (sizeof (struct GNUNET_CRYPTO_ShortHashCode));
-  GNUNET_CRYPTO_short_hash(&pkey,
-                           sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
-                           local_gns_zone);
+  GNUNET_CRYPTO_short_hash (&pkey,
+                           sizeof (struct GNUNET_CRYPTO_EccPublicKey),
+                           local_gns_zone);
   zone = local_gns_zone;
   GNUNET_CRYPTO_short_hash_to_enc (zone, &zonename);
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
               "Using zone: %s!\n", &zonename);
-  GNUNET_CRYPTO_rsa_key_free(key);
+  GNUNET_free(key);
   GNUNET_free(keyfile);
   keyfile = NULL;
 
@@ -3293,16 +3238,16 @@ load_local_zone_key (const struct GNUNET_CONFIGURATION_Handle *cfg)
   }
   else
   {
-    key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
-    GNUNET_CRYPTO_rsa_key_get_public (key, &pkey);
+    key = GNUNET_CRYPTO_ecc_key_create_from_file (keyfile);
+    GNUNET_CRYPTO_ecc_key_get_public (key, &pkey);
     local_private_zone = GNUNET_malloc (sizeof (struct GNUNET_CRYPTO_ShortHashCode));
-    GNUNET_CRYPTO_short_hash(&pkey,
-                             sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
-                             local_private_zone);
+    GNUNET_CRYPTO_short_hash (&pkey,
+                             sizeof (struct GNUNET_CRYPTO_EccPublicKey),
+                             local_private_zone);
     GNUNET_CRYPTO_short_hash_to_enc (zone, &zonename);
     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                 "Using private zone: %s!\n", &zonename);
-    GNUNET_CRYPTO_rsa_key_free(key);
+    GNUNET_free(key);
     GNUNET_free(keyfile);
   }
   keyfile = NULL;
@@ -3322,16 +3267,16 @@ load_local_zone_key (const struct GNUNET_CONFIGURATION_Handle *cfg)
   }
   else
   {
-    key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
-    GNUNET_CRYPTO_rsa_key_get_public (key, &pkey);
+    key = GNUNET_CRYPTO_ecc_key_create_from_file (keyfile);
+    GNUNET_CRYPTO_ecc_key_get_public (key, &pkey);
     local_shorten_zone = GNUNET_malloc (sizeof (struct GNUNET_CRYPTO_ShortHashCode));
-    GNUNET_CRYPTO_short_hash(&pkey,
-                             sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
-                             local_shorten_zone);
+    GNUNET_CRYPTO_short_hash (&pkey,
+                             sizeof(struct GNUNET_CRYPTO_EccPublicKey),
+                             local_shorten_zone);
     GNUNET_CRYPTO_short_hash_to_enc (zone, &zonename);
     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                 "Using shorten zone: %s!\n", &zonename);
-    GNUNET_CRYPTO_rsa_key_free(key);
+    GNUNET_free(key);
     GNUNET_free(keyfile);
   }
 
@@ -3361,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");
@@ -3376,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");
@@ -3408,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;
   }
 
@@ -3438,7 +3378,7 @@ run (void *cls, char *const *args, const char *cfgfile,
   if ((NULL == lsock) ||
       (GNUNET_OK !=
        GNUNET_NETWORK_socket_bind (lsock, (const struct sockaddr *) &sa,
-                                   sizeof (sa))))
+                                   sizeof (sa), 0)))
   {
     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                 "Failed to create listen socket bound to `%s'",
@@ -3477,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");
@@ -3500,12 +3437,11 @@ 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,
                                (struct sockaddr*)&mhd_unix_sock_addr,
-                               len))
+                               len, 0))
   {
     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                 "Unable to bind unix domain socket!\n");