- added check against statistics
[oweals/gnunet.git] / src / gns / gnunet-service-gns.c
index 736bff4bf69e6578e93827510a259af6981bea98..c2ea2a44d3ee065eda0cfcd60790d293973fffd8 100644 (file)
 */
 
 /**
- *
- * TODO:
- *    - Write xquery and block plugin
- *    - The smaller FIXME issues all around
  *
  * @file gns/gnunet-service-gns.c
  * @brief GNUnet GNS service
@@ -146,6 +142,11 @@ struct GNUNET_CRYPTO_ShortHashCode zone_hash;
  */
 static int num_public_records =  3600;
 
+/**
+ * update interval in seconds
+ */
+static unsigned long long int dht_max_update_interval;
+
 /* dht update interval FIXME define? */
 static struct GNUNET_TIME_Relative dht_update_interval;
 
@@ -155,6 +156,19 @@ GNUNET_SCHEDULER_TaskIdentifier zone_update_taskid = GNUNET_SCHEDULER_NO_TASK;
 /* automatic pkey import for name shortening */
 static int auto_import_pkey;
 
+/* lookup timeout */
+static struct GNUNET_TIME_Relative default_lookup_timeout;
+
+/**
+ * Continue shutdown
+ */
+static void
+on_resolver_cleanup(void)
+{
+  GNUNET_NAMESTORE_disconnect(namestore_handle, 1);
+  GNUNET_DHT_disconnect(dht_handle);
+}
+
 /**
  * Task run during shutdown.
  *
@@ -174,9 +188,8 @@ shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
   GNUNET_SERVER_notification_context_destroy (nc);
   
   gns_interceptor_stop();
+  gns_resolver_cleanup(&on_resolver_cleanup);
 
-  GNUNET_NAMESTORE_disconnect(namestore_handle, 1);
-  GNUNET_DHT_disconnect(dht_handle);
 }
 
 
@@ -245,11 +258,11 @@ put_gns_record(void *cls,
   {
     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
                "Zone iteration finished. Rescheduling put in %ds\n",
-               GNUNET_GNS_DHT_MAX_UPDATE_INTERVAL);
+               dht_max_update_interval);
     zone_update_taskid = GNUNET_SCHEDULER_add_delayed (
                                         GNUNET_TIME_relative_multiply(
                                             GNUNET_TIME_UNIT_SECONDS,
-                                            GNUNET_GNS_DHT_MAX_UPDATE_INTERVAL
+                                            dht_max_update_interval
                                             ),
                                             &update_zone_dht_start,
                                             NULL);
@@ -316,6 +329,9 @@ put_gns_record(void *cls,
   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
              "putting records for %s under key: %s with size %d\n",
              name, (char*)&xor_hash_string, rd_payload_length);
+  
+  GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
+             "DHT req to %d\n", DHT_OPERATION_TIMEOUT.rel_value);
 
   GNUNET_DHT_put (dht_handle, &xor_hash,
                   DHT_GNS_REPLICATION_LEVEL,
@@ -355,20 +371,20 @@ update_zone_dht_start(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
   {
     dht_update_interval = GNUNET_TIME_relative_multiply(
                                             GNUNET_TIME_UNIT_SECONDS,
-                                            GNUNET_GNS_DHT_MAX_UPDATE_INTERVAL);
+                                            dht_max_update_interval);
     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
                "No records in db. Adjusted DHT update interval to %ds\n",
-               GNUNET_GNS_DHT_MAX_UPDATE_INTERVAL);
+               dht_max_update_interval);
   }
   else
   {
     
     dht_update_interval = GNUNET_TIME_relative_multiply(
-                                                      GNUNET_TIME_UNIT_SECONDS,
-                                                     (3600/num_public_records));
+                                  GNUNET_TIME_UNIT_SECONDS,
+                                  (dht_max_update_interval/num_public_records));
     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
                "Adjusted DHT update interval to %ds!\n",
-               (3600/num_public_records));
+               (dht_max_update_interval/num_public_records));
   }
 
   /* start counting again */
@@ -387,8 +403,8 @@ update_zone_dht_start(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
 /**
  * Send shorten response back to client
  * 
+ * @param cls the closure containing a client shorten handle
  * @param name the shortened name result or NULL if cannot be shortened
- * @param csh the handle to the shorten request
  */
 static void
 send_shorten_response(void* cls, const char* name)
@@ -440,7 +456,8 @@ static void handle_shorten(void *cls,
 
   size_t msg_size = 0;
   struct ClientShortenHandle *csh;
-  const char* name;
+  char name[MAX_DNS_NAME_LENGTH];
+  char* nameptr = name;
 
   if (ntohs (message->size) < sizeof (struct GNUNET_GNS_ClientShortenMessage))
   {
@@ -467,7 +484,7 @@ static void handle_shorten(void *cls,
   csh->client = client;
   csh->unique_id = sh_msg->id;
   
-  name = (char*)&sh_msg[1];
+  GNUNET_STRINGS_utf8_tolower((char*)&sh_msg[1], &nameptr);
 
   if (strlen (name) < strlen(GNUNET_GNS_TLD)) {
     csh->name = NULL;
@@ -475,8 +492,7 @@ static void handle_shorten(void *cls,
     return;
   }
   
-  if (strcmp(name+strlen(name)-strlen(GNUNET_GNS_TLD),
-             GNUNET_GNS_TLD) != 0)
+  if (!is_gnunet_tld(name) && !is_zkey_tld(name))
   {
     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                 "%s is not our domain. Returning\n", name);
@@ -485,23 +501,21 @@ static void handle_shorten(void *cls,
     return;
   }
   
-  csh->name = GNUNET_malloc(strlen(name)
-                            - strlen(GNUNET_GNS_TLD) + 1);
-  memset(csh->name, 0,
-         strlen(name)-strlen(GNUNET_GNS_TLD) + 1);
-  memcpy(csh->name, name,
-         strlen(name)-strlen(GNUNET_GNS_TLD));
-
   /* Start shortening */
-  gns_resolver_shorten_name(zone_hash, name, &send_shorten_response, csh);
+  if (GNUNET_YES == auto_import_pkey)
+    gns_resolver_shorten_name(zone_hash, name, zone_key,
+                              &send_shorten_response, csh);
+  else
+    gns_resolver_shorten_name(zone_hash, name, NULL,
+                              &send_shorten_response, csh);
 }
 
 
 /**
  * Send get authority response back to client
  * 
+ * @param cls the closure containing a client get auth handle
  * @param name the shortened name result or NULL if cannot be shortened
- * @param cah the handle to the get authority request
  */
 static void
 send_get_auth_response(void *cls, const char* name)
@@ -558,7 +572,9 @@ static void handle_get_authority(void *cls,
 
   size_t msg_size = 0;
   struct ClientGetAuthHandle *cah;
-  const char* name;
+  char name[MAX_DNS_NAME_LENGTH];
+  char* nameptr = name;
+
 
   if (ntohs (message->size) < sizeof (struct GNUNET_GNS_ClientGetAuthMessage))
   {
@@ -581,7 +597,8 @@ static void handle_get_authority(void *cls,
     return;
   }
   
-  name = (char*)&sh_msg[1];
+  GNUNET_STRINGS_utf8_tolower((char*)&sh_msg[1], &nameptr);
+
 
   cah = GNUNET_malloc(sizeof(struct ClientGetAuthHandle));
   cah->client = client;
@@ -631,7 +648,6 @@ static void handle_get_authority(void *cls,
  * Reply to client with the result from our lookup.
  *
  * @param cls the closure (our client lookup handle)
- * @param rh the request handle of the lookup
  * @param rd_count the number of records
  * @param rd the record data
  */
@@ -686,8 +702,9 @@ handle_lookup(void *cls,
 
   size_t msg_size = 0;
   size_t namelen;
-  char* name;
+  char name[MAX_DNS_NAME_LENGTH];
   struct ClientLookupHandle *clh;
+  char* nameptr = name;
 
   if (ntohs (message->size) < sizeof (struct GNUNET_GNS_ClientLookupMessage))
   {
@@ -710,7 +727,7 @@ handle_lookup(void *cls,
     return;
   }
   
-  name = (char*)&sh_msg[1];
+  GNUNET_STRINGS_utf8_tolower((char*)&sh_msg[1], &nameptr);
   namelen = strlen(name)+1;
   clh = GNUNET_malloc(sizeof(struct ClientLookupHandle));
   clh->client = client;
@@ -723,12 +740,14 @@ handle_lookup(void *cls,
   {
     gns_resolver_lookup_record(zone_hash, clh->type, name,
                                zone_key,
+                               default_lookup_timeout,
                                &send_lookup_response, clh);
   }
   else
   {
     gns_resolver_lookup_record(zone_hash, clh->type, name,
                                NULL,
+                               default_lookup_timeout,
                                &send_lookup_response, clh);
   }
 }
@@ -751,6 +770,9 @@ run (void *cls, struct GNUNET_SERVER_Handle *server,
   
   char* keyfile;
   struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pkey;
+  unsigned long long max_parallel_bg_queries = 0;
+  unsigned long long default_lookup_timeout_secs = 0;
+  int ignore_pending = GNUNET_NO;
 
   static const struct GNUNET_SERVER_MessageHandler handlers[] = {
     {&handle_shorten, NULL, GNUNET_MESSAGE_TYPE_GNS_SHORTEN, 0},
@@ -792,15 +814,9 @@ run (void *cls, struct GNUNET_SERVER_Handle *server,
     return;
   }
   
-  /**
-   * handle to the dht
-   */
-  dht_handle = GNUNET_DHT_connect(c, 1); //FIXME get ht_len from cfg
+  
 
-  if (NULL == dht_handle)
-  {
-    GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Could not connect to DHT!\n");
-  }
+  auto_import_pkey = GNUNET_NO;
 
   if (GNUNET_YES ==
       GNUNET_CONFIGURATION_get_value_yesno (c, "gns",
@@ -811,8 +827,65 @@ run (void *cls, struct GNUNET_SERVER_Handle *server,
     auto_import_pkey = GNUNET_YES;
 
   }
+
+  dht_max_update_interval = GNUNET_GNS_DHT_MAX_UPDATE_INTERVAL;
+
+  if (GNUNET_OK ==
+      GNUNET_CONFIGURATION_get_value_number (c, "gns",
+                                             "DHT_MAX_UPDATE_INTERVAL",
+                                             &dht_max_update_interval))
+  {
+    GNUNET_log(GNUNET_ERROR_TYPE_INFO,
+               "DHT zone update interval: %d\n",
+               dht_max_update_interval);
+  }
+
+  if (GNUNET_OK ==
+      GNUNET_CONFIGURATION_get_value_number (c, "gns",
+                                            "MAX_PARALLEL_BACKGROUND_QUERIES",
+                                            &max_parallel_bg_queries))
+  {
+    GNUNET_log(GNUNET_ERROR_TYPE_INFO,
+               "Number of allowed parallel background queries: %d\n",
+               max_parallel_bg_queries);
+  }
+
+  if (GNUNET_YES ==
+      GNUNET_CONFIGURATION_get_value_yesno (c, "gns",
+                                            "AUTO_IMPORT_CONFIRMATION_REQ"))
+  {
+    GNUNET_log(GNUNET_ERROR_TYPE_INFO,
+               "Auto import requires user confirmation\n");
+    ignore_pending = GNUNET_YES;
+  }
+
+  if (GNUNET_OK ==
+      GNUNET_CONFIGURATION_get_value_number(c, "gns",
+                                            "DEFAULT_LOOKUP_TIMEOUT",
+                                            &default_lookup_timeout_secs))
+  {
+    GNUNET_log(GNUNET_ERROR_TYPE_INFO,
+               "Default lookup timeout: %ds\n", default_lookup_timeout_secs);
+    default_lookup_timeout = GNUNET_TIME_relative_multiply(
+                                            GNUNET_TIME_UNIT_SECONDS,
+                                            default_lookup_timeout_secs);
+  }
+  
+  /**
+   * handle to the dht
+   */
+  dht_handle = GNUNET_DHT_connect(c,
+                       //max_parallel_bg_queries); //FIXME get ht_len from cfg
+                       1024);
+
+  if (NULL == dht_handle)
+  {
+    GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Could not connect to DHT!\n");
+  }
   
-  if (gns_resolver_init(namestore_handle, dht_handle)
+  if (gns_resolver_init(namestore_handle, dht_handle, zone_hash,
+                        max_parallel_bg_queries,
+                        ignore_pending)
       == GNUNET_SYSERR)
   {
     GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
@@ -822,8 +895,7 @@ run (void *cls, struct GNUNET_SERVER_Handle *server,
   }
 
   if (GNUNET_YES ==
-      GNUNET_CONFIGURATION_get_value_yesno (c, "gns",
-                                            "HIJACK_DNS"))
+      GNUNET_CONFIGURATION_get_value_yesno (c, "gns", "HIJACK_DNS"))
   {
     GNUNET_log(GNUNET_ERROR_TYPE_INFO,
                "DNS hijacking enabled... connecting to service.\n");
@@ -835,8 +907,6 @@ run (void *cls, struct GNUNET_SERVER_Handle *server,
     }
   }
   
-  //put_some_records(); //FIXME for testing
-  
   /**
    * Schedule periodic put
    * for our records