- added check against statistics
[oweals/gnunet.git] / src / gns / gnunet-service-gns.c
index 2f64a637db8de6e98592cf66693c2b542ae13917..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
@@ -108,7 +104,7 @@ struct ClientLookupHandle
 /**
  * Our handle to the DHT
  */
-struct GNUNET_DHT_Handle *dht_handle;
+static struct GNUNET_DHT_Handle *dht_handle;
 
 /**
  * Our zone's private key
@@ -139,19 +135,40 @@ static struct GNUNET_SERVER_NotificationContext *nc;
 /**
  * Our zone hash
  */
-GNUNET_HashCode zone_hash;
+struct GNUNET_CRYPTO_ShortHashCode zone_hash;
 
 /**
  * Useful for zone update for DHT put
  */
 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;
 
 /* zone update task */
 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.
  *
@@ -171,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);
 }
 
 
@@ -228,20 +244,42 @@ put_gns_record(void *cls,
 {
   
   struct GNSNameRecordBlock *nrb;
-  GNUNET_HashCode name_hash;
+  struct GNUNET_CRYPTO_ShortHashCode name_hash;
   GNUNET_HashCode xor_hash;
+  GNUNET_HashCode name_hash_double;
+  GNUNET_HashCode zone_hash_double;
   struct GNUNET_CRYPTO_HashAsciiEncoded xor_hash_string;
   uint32_t rd_payload_length;
   char* nrb_data = NULL;
+  size_t namelen;
 
   /* we're done */
   if (NULL == name)
   {
-    GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Zone iteration finished\n");
-    GNUNET_NAMESTORE_zone_iteration_stop (namestore_iter);
-    zone_update_taskid = GNUNET_SCHEDULER_add_now (&update_zone_dht_start,
+    GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
+               "Zone iteration finished. Rescheduling put in %ds\n",
+               dht_max_update_interval);
+    zone_update_taskid = GNUNET_SCHEDULER_add_delayed (
+                                        GNUNET_TIME_relative_multiply(
+                                            GNUNET_TIME_UNIT_SECONDS,
+                                            dht_max_update_interval
+                                            ),
+                                            &update_zone_dht_start,
+                                            NULL);
+    return;
+  }
+  
+  namelen = strlen(name) + 1;
+  
+  if (signature == NULL)
+  {
+    GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
+               "No signature for %s record data provided! Skipping...\n",
+               name);
+    zone_update_taskid = GNUNET_SCHEDULER_add_now (&update_zone_dht_next,
                                                    NULL);
     return;
+
   }
   
   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
@@ -249,46 +287,51 @@ put_gns_record(void *cls,
   
   rd_payload_length = GNUNET_NAMESTORE_records_get_size (rd_count, rd);
   
-  nrb = GNUNET_malloc(rd_payload_length + strlen(name) + 1 
+  nrb = GNUNET_malloc(rd_payload_length + namelen
                       + sizeof(struct GNSNameRecordBlock));
   
-  if (signature != NULL)
-    nrb->signature = *signature;
+  nrb->signature = *signature;
   
   nrb->public_key = *key;
 
   nrb->rd_count = htonl(rd_count);
   
-  memset(&nrb[1], 0, strlen(name) + 1);
-  memcpy(&nrb[1], name, strlen(name));
+  memcpy(&nrb[1], name, namelen);
 
   nrb_data = (char*)&nrb[1];
-  nrb_data += strlen(name) + 1;
+  nrb_data += namelen;
 
-  rd_payload_length += sizeof(struct GNSNameRecordBlock) +
-    strlen(name) + 1;
+  rd_payload_length += sizeof(struct GNSNameRecordBlock) + namelen;
 
   if (-1 == GNUNET_NAMESTORE_records_serialize (rd_count,
                                                 rd,
                                                 rd_payload_length,
                                                 nrb_data))
   {
-    GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Record serialization failed!\n");
+    GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
+               "Record serialization failed! Skipping...\n");
     GNUNET_free(nrb);
+    zone_update_taskid = GNUNET_SCHEDULER_add_now (&update_zone_dht_next,
+                                                   NULL);
     return;
-    //FIXME what to do
   }
 
 
   /*
    * calculate DHT key: H(name) xor H(pubkey)
    */
-  GNUNET_CRYPTO_hash(name, strlen(name), &name_hash);
-  GNUNET_CRYPTO_hash_xor(&zone_hash, &name_hash, &xor_hash);
+  GNUNET_CRYPTO_short_hash(name, strlen(name), &name_hash);
+  GNUNET_CRYPTO_short_hash_double (&name_hash, &name_hash_double);
+  GNUNET_CRYPTO_short_hash_double (&zone_hash, &zone_hash_double);
+  GNUNET_CRYPTO_hash_xor(&zone_hash_double, &name_hash_double, &xor_hash);
   GNUNET_CRYPTO_hash_to_enc (&xor_hash, &xor_hash_string);
+
   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,
@@ -323,26 +366,35 @@ put_gns_record(void *cls,
 static void
 update_zone_dht_start(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
 {
-  GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Starting DHT zone update!\n");
+  GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Scheduling DHT zone update!\n");
   if (0 == num_public_records)
   {
     dht_update_interval = GNUNET_TIME_relative_multiply(
-                                                      GNUNET_TIME_UNIT_SECONDS,
-                                                      1);
+                                            GNUNET_TIME_UNIT_SECONDS,
+                                            dht_max_update_interval);
+    GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
+               "No records in db. Adjusted DHT update interval to %ds\n",
+               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",
+               (dht_max_update_interval/num_public_records));
   }
-  num_public_records = 0; //start counting again
+
+  /* start counting again */
+  num_public_records = 0;
   namestore_iter = GNUNET_NAMESTORE_zone_iteration_start (namestore_handle,
-                                                          &zone_hash,
-                                                          GNUNET_NAMESTORE_RF_AUTHORITY,
-                                                          GNUNET_NAMESTORE_RF_PRIVATE,
-                                                          &put_gns_record,
-                                                          NULL);
+                                                 &zone_hash,
+                                                 GNUNET_NAMESTORE_RF_AUTHORITY,
+                                                 GNUNET_NAMESTORE_RF_PRIVATE,
+                                                 &put_gns_record,
+                                                 NULL);
 }
 
 
@@ -351,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)
@@ -384,7 +436,7 @@ send_shorten_response(void* cls, const char* name)
   GNUNET_SERVER_receive_done (csh->client, GNUNET_OK);
   
   GNUNET_free(rmsg);
-  GNUNET_free(csh->name);
+  GNUNET_free_non_null(csh->name);
   GNUNET_free(csh);
 
 }
@@ -404,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))
   {
@@ -431,24 +484,38 @@ static void handle_shorten(void *cls,
   csh->client = client;
   csh->unique_id = sh_msg->id;
   
-  name = (char*)&sh_msg[1];
-  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));
+  GNUNET_STRINGS_utf8_tolower((char*)&sh_msg[1], &nameptr);
 
+  if (strlen (name) < strlen(GNUNET_GNS_TLD)) {
+    csh->name = NULL;
+    send_shorten_response(csh, name);
+    return;
+  }
+  
+  if (!is_gnunet_tld(name) && !is_zkey_tld(name))
+  {
+    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                "%s is not our domain. Returning\n", name);
+    csh->name = NULL;
+    send_shorten_response(csh, name);
+    return;
+  }
+  
   /* 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)
@@ -482,7 +549,7 @@ send_get_auth_response(void *cls, const char* name)
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Cleaning up handles...\n");
 
   GNUNET_free(rmsg);
-  GNUNET_free(cah->name);
+  GNUNET_free_non_null(cah->name);
   GNUNET_free(cah);
 
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "done.\n");
@@ -505,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))
   {
@@ -527,12 +596,42 @@ static void handle_get_authority(void *cls,
     GNUNET_SERVER_receive_done (client, GNUNET_OK);
     return;
   }
+  
+  GNUNET_STRINGS_utf8_tolower((char*)&sh_msg[1], &nameptr);
+
 
   cah = GNUNET_malloc(sizeof(struct ClientGetAuthHandle));
   cah->client = client;
   cah->unique_id = sh_msg->id;
+
+  if (strlen(name) < strlen(GNUNET_GNS_TLD))
+  {
+    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                "%s is too short. Returning\n", name);
+    cah->name = NULL;
+    send_get_auth_response(cah, name);
+    return;
+  }
+
+  if (strcmp(name+strlen(name)-strlen(GNUNET_GNS_TLD),
+             GNUNET_GNS_TLD) != 0)
+  {
+    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                "%s is not our domain. Returning\n", name);
+    cah->name = NULL;
+    send_get_auth_response(cah, name);
+    return;
+  }
+
+  if (strcmp(name, GNUNET_GNS_TLD) == 0)
+  {
+    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                "%s is us. Returning\n", name);
+    cah->name = NULL;
+    send_get_auth_response(cah, name);
+    return;
+  }
   
-  name = (char*)&sh_msg[1];
   cah->name = GNUNET_malloc(strlen(name)
                             - strlen(GNUNET_GNS_TLD) + 1);
   memset(cah->name, 0,
@@ -549,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
  */
@@ -603,7 +701,10 @@ handle_lookup(void *cls,
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "LOOKUP");
 
   size_t msg_size = 0;
+  size_t namelen;
+  char name[MAX_DNS_NAME_LENGTH];
   struct ClientLookupHandle *clh;
+  char* nameptr = name;
 
   if (ntohs (message->size) < sizeof (struct GNUNET_GNS_ClientLookupMessage))
   {
@@ -625,16 +726,30 @@ handle_lookup(void *cls,
     GNUNET_SERVER_receive_done (client, GNUNET_OK);
     return;
   }
-
+  
+  GNUNET_STRINGS_utf8_tolower((char*)&sh_msg[1], &nameptr);
+  namelen = strlen(name)+1;
   clh = GNUNET_malloc(sizeof(struct ClientLookupHandle));
   clh->client = client;
-  clh->name = GNUNET_malloc(strlen((char*)&sh_msg[1]) + 1);
-  strcpy(clh->name, (char*)&sh_msg[1]);
+  clh->name = GNUNET_malloc(namelen);
+  strcpy(clh->name, name);
   clh->unique_id = sh_msg->id;
   clh->type = ntohl(sh_msg->type);
   
-  gns_resolver_lookup_record(zone_hash, clh->type, (char*)&sh_msg[1],
-                             &send_lookup_response, clh);
+  if (GNUNET_YES == auto_import_pkey)
+  {
+    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);
+  }
 }
 
 
@@ -655,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},
@@ -677,27 +795,11 @@ run (void *cls, struct GNUNET_SERVER_Handle *server,
   zone_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
   GNUNET_CRYPTO_rsa_key_get_public (zone_key, &pkey);
 
-  GNUNET_CRYPTO_hash(&pkey, sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
+  GNUNET_CRYPTO_short_hash(&pkey,
+                     sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
                      &zone_hash);
   GNUNET_free(keyfile);
   
-
-  if (GNUNET_YES ==
-      GNUNET_CONFIGURATION_get_value_yesno (c, "gns",
-                                            "HIJACK_DNS"))
-  {
-    GNUNET_log(GNUNET_ERROR_TYPE_INFO,
-               "DNS hijacking enabled... connecting to service.\n");
-
-    if (gns_interceptor_init(zone_hash, c) == GNUNET_SYSERR)
-    {
-      GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
-               "Failed to enable the dns interceptor!\n");
-    }
-  }
-
-  
-
   /**
    * handle to our local namestore
    */
@@ -712,17 +814,98 @@ run (void *cls, struct GNUNET_SERVER_Handle *server,
     return;
   }
   
+  
+
+  auto_import_pkey = GNUNET_NO;
+
+  if (GNUNET_YES ==
+      GNUNET_CONFIGURATION_get_value_yesno (c, "gns",
+                                            "AUTO_IMPORT_PKEY"))
+  {
+    GNUNET_log(GNUNET_ERROR_TYPE_INFO,
+               "Automatic PKEY import is enabled.\n");
+    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, 1); //FIXME get ht_len from cfg
+  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, zone_hash,
+                        max_parallel_bg_queries,
+                        ignore_pending)
+      == GNUNET_SYSERR)
+  {
+    GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
+               "Unable to initialize resolver!\n");
+    GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
+    return;
+  }
 
-  //put_some_records(); //FIXME for testing
+  if (GNUNET_YES ==
+      GNUNET_CONFIGURATION_get_value_yesno (c, "gns", "HIJACK_DNS"))
+  {
+    GNUNET_log(GNUNET_ERROR_TYPE_INFO,
+               "DNS hijacking enabled... connecting to service.\n");
+
+    if (gns_interceptor_init(zone_hash, zone_key, c) == GNUNET_SYSERR)
+    {
+      GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
+               "Failed to enable the dns interceptor!\n");
+    }
+  }
   
   /**
    * Schedule periodic put
@@ -731,7 +914,7 @@ run (void *cls, struct GNUNET_SERVER_Handle *server,
    */
   dht_update_interval = GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS,
                                                       1);
-  //zone_update_taskid = GNUNET_SCHEDULER_add_now (&update_zone_dht_start, NULL);
+  zone_update_taskid = GNUNET_SCHEDULER_add_now (&update_zone_dht_start, NULL);
 
   GNUNET_SERVER_add_handlers (server, handlers);