-indentation, code cleanup
[oweals/gnunet.git] / src / gns / gnunet-service-gns.c
index 092bba7055f10230370360969b357593064ad4bd..53d6c11a4239d556d4e9c9cde638bb998464e360 100644 (file)
@@ -28,6 +28,7 @@
 #include "gnunet_dns_service.h"
 #include "gnunet_dnsparser_lib.h"
 #include "gnunet_dht_service.h"
+#include "gnunet_namecache_service.h"
 #include "gnunet_namestore_service.h"
 #include "gnunet_gns_service.h"
 #include "gnunet_statistics_service.h"
@@ -98,7 +99,7 @@ struct ClientLookupHandle
   struct GNS_ResolverHandle *lookup;
 
   /**
-   * request id 
+   * request id
    */
   uint32_t request_id;
 
@@ -120,6 +121,11 @@ static struct GNUNET_DHT_PutHandle *active_put;
  */
 static struct GNUNET_NAMESTORE_Handle *namestore_handle;
 
+/**
+ * Our handle to the namecache service
+ */
+static struct GNUNET_NAMECACHE_Handle *namecache_handle;
+
 /**
  * Handle to iterate over our authoritative zone in namestore
  */
@@ -170,11 +176,6 @@ static GNUNET_SCHEDULER_TaskIdentifier zone_publish_task;
  */
 static int first_zone_iteration;
 
-/**
- * The lookup timeout
- */
-static struct GNUNET_TIME_Relative default_lookup_timeout;
-
 /**
  * #GNUNET_YES if ipv6 is supported
  */
@@ -204,7 +205,7 @@ shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
 
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
              "Shutting down!\n");
-  GNUNET_SERVER_notification_context_destroy (nc);  
+  GNUNET_SERVER_notification_context_destroy (nc);
   while (NULL != (clh = clh_head))
   {
     GNUNET_SERVER_client_set_user_context (clh->client, NULL);
@@ -236,6 +237,11 @@ shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
     GNUNET_NAMESTORE_disconnect (namestore_handle);
     namestore_handle = NULL;
   }
+  if (NULL != namecache_handle)
+  {
+    GNUNET_NAMECACHE_disconnect (namecache_handle);
+    namecache_handle = NULL;
+  }
   if (NULL != active_put)
   {
     GNUNET_DHT_put_cancel (active_put);
@@ -271,7 +277,7 @@ publish_zone_dht_next (void *cls,
  * @param tc task context
  */
 static void
-publish_zone_dht_start (void *cls, 
+publish_zone_dht_start (void *cls,
                        const struct GNUNET_SCHEDULER_TaskContext *tc);
 
 
@@ -285,10 +291,10 @@ static void
 dht_put_continuation (void *cls,
                      int success)
 {
-  struct GNUNET_TIME_Relative next_put_interval; 
+  struct GNUNET_TIME_Relative next_put_interval;
 
   active_put = NULL;
-  num_public_records++;  
+  num_public_records++;
   if ( (num_public_records > last_num_public_records) &&
        (GNUNET_NO == first_zone_iteration) )
   {
@@ -305,7 +311,7 @@ dht_put_continuation (void *cls,
   GNUNET_STATISTICS_set (statistics,
                         "Current zone iteration interval (ms)",
                         next_put_interval.rel_value_us / 1000LL,
-                        GNUNET_NO); 
+                        GNUNET_NO);
   zone_publish_task = GNUNET_SCHEDULER_add_delayed (next_put_interval,
                                                    &publish_zone_dht_next,
                                                    NULL);
@@ -323,17 +329,17 @@ dht_put_continuation (void *cls,
  */
 static void
 put_gns_record (void *cls,
-                const struct GNUNET_CRYPTO_EccPrivateKey *key,
+                const struct GNUNET_CRYPTO_EcdsaPrivateKey *key,
                 const char *name,
                 unsigned int rd_count,
-                const struct GNUNET_NAMESTORE_RecordData *rd)
-{  
-  struct GNUNET_NAMESTORE_Block *block;
+                const struct GNUNET_GNSRECORD_Data *rd)
+{
+  struct GNUNET_GNSRECORD_Block *block;
   struct GNUNET_HashCode query;
-  struct GNUNET_TIME_Absolute expire; 
+  struct GNUNET_TIME_Absolute expire;
   struct GNUNET_TIME_Absolute now;
   size_t block_size;
-  struct GNUNET_NAMESTORE_RecordData rd_public[rd_count];
+  struct GNUNET_GNSRECORD_Data rd_public[rd_count];
   unsigned int rd_public_count;
   unsigned int i;
 
@@ -370,8 +376,8 @@ put_gns_record (void *cls,
                            put_interval.rel_value_us / 1000LL,
                            GNUNET_NO);
     GNUNET_STATISTICS_update (statistics,
-                              "Number of zone iterations", 
-                             1, 
+                              "Number of zone iterations",
+                             1,
                              GNUNET_NO);
     GNUNET_STATISTICS_set (statistics,
                            "Number of public records in DHT",
@@ -382,7 +388,7 @@ put_gns_record (void *cls,
                                                        &publish_zone_dht_start,
                                                        NULL);
     else
-      zone_publish_task = GNUNET_SCHEDULER_add_now (&publish_zone_dht_start, 
+      zone_publish_task = GNUNET_SCHEDULER_add_now (&publish_zone_dht_start,
                                                    NULL);
     return;
   }
@@ -392,13 +398,13 @@ put_gns_record (void *cls,
   rd_public_count = 0;
   now = GNUNET_TIME_absolute_get ();
   for (i=0;i<rd_count;i++)
-    if (0 == (rd[i].flags & (GNUNET_NAMESTORE_RF_PRIVATE |
-                            GNUNET_NAMESTORE_RF_PENDING)))
+    if (0 == (rd[i].flags & (GNUNET_GNSRECORD_RF_PRIVATE |
+                            GNUNET_GNSRECORD_RF_PENDING)))
     {
       rd_public[rd_public_count] = rd[i];
-      if (0 != (rd[i].flags & GNUNET_NAMESTORE_RF_RELATIVE_EXPIRATION))
+      if (0 != (rd[i].flags & GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION))
       {
-       rd_public[rd_public_count].flags &= ~GNUNET_NAMESTORE_RF_RELATIVE_EXPIRATION;
+       rd_public[rd_public_count].flags &= ~GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION;
        rd_public[rd_public_count].expiration_time += now.abs_value_us;
       }
       rd_public_count++;
@@ -411,17 +417,17 @@ put_gns_record (void *cls,
                                                    NULL);
     return;
   }
-  expire = GNUNET_NAMESTORE_record_get_expiration_time (rd_public_count,
+  expire = GNUNET_GNSRECORD_record_get_expiration_time (rd_public_count,
                                                        rd_public);
-  block = GNUNET_NAMESTORE_block_create (key,
+  block = GNUNET_GNSRECORD_block_create (key,
                                         expire,
                                         name,
                                         rd_public,
                                         rd_public_count);
-  block_size = ntohl (block->purpose.size) 
-    + sizeof (struct GNUNET_CRYPTO_EccSignature) 
-    + sizeof (struct GNUNET_CRYPTO_EccPublicSignKey);
-  GNUNET_NAMESTORE_query_from_private_key (key,
+  block_size = ntohl (block->purpose.size)
+    + sizeof (struct GNUNET_CRYPTO_EcdsaSignature)
+    + sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey);
+  GNUNET_GNSRECORD_query_from_private_key (key,
                                           name,
                                           &query);
 
@@ -434,7 +440,7 @@ put_gns_record (void *cls,
                               expire,
                               DHT_OPERATION_TIMEOUT,
                               &dht_put_continuation,
-                              NULL); 
+                              NULL);
   if (NULL == active_put)
   {
     GNUNET_break (0);
@@ -451,13 +457,13 @@ put_gns_record (void *cls,
  * @param tc task context
  */
 static void
-publish_zone_dht_start (void *cls, 
+publish_zone_dht_start (void *cls,
                        const struct GNUNET_SCHEDULER_TaskContext *tc)
 {
   zone_publish_task = GNUNET_SCHEDULER_NO_TASK;
 
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 
-             "Scheduling DHT zone update!\n");  
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+             "Scheduling DHT zone update!\n");
   /* start counting again */
   num_public_records = 0;
   namestore_iter = GNUNET_NAMESTORE_zone_iteration_start (namestore_handle,
@@ -480,41 +486,39 @@ publish_zone_dht_start (void *cls,
 static void
 send_lookup_response (void* cls,
                      uint32_t rd_count,
-                     const struct GNUNET_NAMESTORE_RecordData *rd)
+                     const struct GNUNET_GNSRECORD_Data *rd)
 {
   struct ClientLookupHandle *clh = cls;
   struct GNUNET_GNS_ClientLookupResultMessage *rmsg;
   size_t len;
-  
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 
+
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
              "Sending `%s' message with %d results\n",
-              "LOOKUP_RESULT", 
+              "LOOKUP_RESULT",
              rd_count);
-  
-  len = GNUNET_NAMESTORE_records_get_size (rd_count, rd);
-  rmsg = GNUNET_malloc (len + sizeof (struct GNUNET_GNS_ClientLookupResultMessage)); 
+
+  len = GNUNET_GNSRECORD_records_get_size (rd_count, rd);
+  rmsg = GNUNET_malloc (len + sizeof (struct GNUNET_GNS_ClientLookupResultMessage));
   rmsg->header.type = htons (GNUNET_MESSAGE_TYPE_GNS_LOOKUP_RESULT);
   rmsg->header.size = htons (len + sizeof(struct GNUNET_GNS_ClientLookupResultMessage));
   rmsg->id = clh->request_id;
-  rmsg->rd_count = htonl (rd_count); 
-  GNUNET_NAMESTORE_records_serialize (rd_count, rd, len, 
+  rmsg->rd_count = htonl (rd_count);
+  GNUNET_GNSRECORD_records_serialize (rd_count, rd, len,
                                      (char*) &rmsg[1]);
-  GNUNET_SERVER_notification_context_unicast (nc, 
+  GNUNET_SERVER_notification_context_unicast (nc,
                                              clh->client,
                                              &rmsg->header,
                                              GNUNET_NO);
   GNUNET_free (rmsg);
-  GNUNET_SERVER_receive_done (clh->client, 
-                             GNUNET_OK); 
   GNUNET_CONTAINER_DLL_remove (clh_head, clh_tail, clh);
   GNUNET_SERVER_client_set_user_context (clh->client, NULL);
   GNUNET_free (clh);
   GNUNET_STATISTICS_update (statistics,
-                            "Completed lookups", 1, 
+                            "Completed lookups", 1,
                            GNUNET_NO);
   GNUNET_STATISTICS_update (statistics,
-                           "Records resolved", 
-                           rd_count, 
+                           "Records resolved",
+                           rd_count,
                            GNUNET_NO);
 }
 
@@ -535,12 +539,12 @@ handle_lookup (void *cls,
   struct ClientLookupHandle *clh;
   char *nameptr = name;
   const char *utf_in;
-  const struct GNUNET_CRYPTO_EccPrivateKey *key;
+  const struct GNUNET_CRYPTO_EcdsaPrivateKey *key;
   uint16_t msg_size;
   const struct GNUNET_GNS_ClientLookupMessage *sh_msg;
-  
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 
-             "Received `%s' message\n", 
+
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+             "Received `%s' message\n",
              "LOOKUP");
   msg_size = ntohs (message->size);
   if (msg_size < sizeof (struct GNUNET_GNS_ClientLookupMessage))
@@ -551,7 +555,7 @@ handle_lookup (void *cls,
   }
   sh_msg = (const struct GNUNET_GNS_ClientLookupMessage *) message;
   GNUNET_SERVER_notification_context_add (nc, client);
-  if (GNUNET_YES == ntohl (sh_msg->have_key))
+  if (GNUNET_YES == ntohs (sh_msg->have_key))
     key = &sh_msg->shorten_key;
   else
     key = NULL;
@@ -564,7 +568,8 @@ handle_lookup (void *cls,
     return;
   }
   GNUNET_STRINGS_utf8_tolower (utf_in, &nameptr);
-  
+  GNUNET_SERVER_receive_done (client, GNUNET_OK);
+
   clh = GNUNET_new (struct ClientLookupHandle);
   GNUNET_SERVER_client_set_user_context (client, clh);
   GNUNET_CONTAINER_DLL_insert (clh_head, clh_tail, clh);
@@ -577,7 +582,7 @@ handle_lookup (void *cls,
                "LOOKUP: Query for A record but AF_INET not supported!");
     send_lookup_response (clh, 0, NULL);
     return;
-  }  
+  }
   if ( (GNUNET_DNSPARSER_TYPE_AAAA == ntohl (sh_msg->type)) &&
        (GNUNET_OK != v6_enabled) )
   {
@@ -586,14 +591,14 @@ handle_lookup (void *cls,
     send_lookup_response (clh, 0, NULL);
     return;
   }
-  clh->lookup = GNS_resolver_lookup (&sh_msg->zone, 
+  clh->lookup = GNS_resolver_lookup (&sh_msg->zone,
                                     ntohl (sh_msg->type),
                                     name,
                                     key,
                                     ntohl (sh_msg->only_cached),
                                     &send_lookup_response, clh);
   GNUNET_STATISTICS_update (statistics,
-                            "Lookup attempts", 
+                            "Lookup attempts",
                            1, GNUNET_NO);
 }
 
@@ -636,7 +641,7 @@ run (void *cls, struct GNUNET_SERVER_Handle *server,
     { &handle_lookup, NULL, GNUNET_MESSAGE_TYPE_GNS_LOOKUP, 0},
     {NULL, NULL, 0, 0}
   };
-  struct GNUNET_CRYPTO_EccPublicSignKey dns_root;
+  struct GNUNET_CRYPTO_EcdsaPublicKey dns_root;
   unsigned long long max_parallel_bg_queries = 0;
   char *dns_root_name;
 
@@ -651,7 +656,15 @@ run (void *cls, struct GNUNET_SERVER_Handle *server,
     GNUNET_SCHEDULER_shutdown ();
     return;
   }
-  
+  namecache_handle = GNUNET_NAMECACHE_connect (c);
+  if (NULL == namecache_handle)
+  {
+    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+               _("Failed to connect to the namecache!\n"));
+    GNUNET_SCHEDULER_shutdown ();
+    return;
+  }
+
   put_interval = INITIAL_PUT_INTERVAL;
   zone_publish_time_window = DEFAULT_ZONE_PUBLISH_TIME_WINDOW;
 
@@ -674,17 +687,6 @@ run (void *cls, struct GNUNET_SERVER_Handle *server,
                max_parallel_bg_queries);
   }
 
-  if (GNUNET_OK ==
-      GNUNET_CONFIGURATION_get_value_time (c, "gns",
-                                          "DEFAULT_LOOKUP_TIMEOUT",
-                                          &default_lookup_timeout))
-  {
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-               "Default lookup timeout: %s\n",
-               GNUNET_STRINGS_relative_time_to_string (default_lookup_timeout,
-                                                       GNUNET_YES));
-  }
-  
   dht_handle = GNUNET_DHT_connect (c,
                                   (unsigned int) max_parallel_bg_queries);
   if (NULL == dht_handle)
@@ -694,18 +696,18 @@ run (void *cls, struct GNUNET_SERVER_Handle *server,
     GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
     return;
   }
-  
+
   if (GNUNET_OK ==
       GNUNET_CONFIGURATION_get_value_string (c, "gns", "DNS_ROOT",
                                             &dns_root_name))
   {
     if (GNUNET_OK !=
-       GNUNET_CRYPTO_ecc_public_sign_key_from_string (dns_root_name,
+       GNUNET_CRYPTO_ecdsa_public_key_from_string (dns_root_name,
                                                  strlen (dns_root_name),
                                                  &dns_root))
     {
       GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
-                                "gns", "DNS_ROOT", 
+                                "gns", "DNS_ROOT",
                                 _("valid public key required"));
       GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
       GNUNET_free (dns_root_name);
@@ -714,7 +716,7 @@ run (void *cls, struct GNUNET_SERVER_Handle *server,
     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
                "DNS hijacking with root `%s' enabled. Connecting to DNS service.\n",
                dns_root_name);
-    GNUNET_free (dns_root_name);    
+    GNUNET_free (dns_root_name);
     if (GNUNET_SYSERR ==
        GNS_interceptor_init (&dns_root, c))
     {
@@ -722,21 +724,25 @@ run (void *cls, struct GNUNET_SERVER_Handle *server,
       return;
     }
   }
-  GNS_resolver_init (namestore_handle, dht_handle, 
+  GNS_resolver_init (namestore_handle,
+                     namecache_handle,
+                     dht_handle,
                     c,
                     max_parallel_bg_queries);
-  GNS_shorten_init (namestore_handle, dht_handle);
+  GNS_shorten_init (namestore_handle,
+                    namecache_handle,
+                    dht_handle);
   GNUNET_SERVER_disconnect_notify (server,
                                   &notify_client_disconnect,
                                   NULL);
-  /* Schedule periodic put for our records. */    
+  /* Schedule periodic put for our records. */
   first_zone_iteration = GNUNET_YES;
   GNUNET_SERVER_add_handlers (server, handlers);
   statistics = GNUNET_STATISTICS_create ("gns", c);
   nc = GNUNET_SERVER_notification_context_create (server, 1);
-  zone_publish_task = GNUNET_SCHEDULER_add_now (&publish_zone_dht_start, 
+  zone_publish_task = GNUNET_SCHEDULER_add_now (&publish_zone_dht_start,
                                                NULL);
-  GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, 
+  GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
                                &shutdown_task, NULL);
 }