removing remenants of abstract unix domain socket handling, this finishes addressing...
[oweals/gnunet.git] / src / gns / gnunet-service-gns.c
index 9f05269f19e9a93955159470d161b44006dd7c57..8fcd75e7c37ddf637b99aa10aaec4a5fa6703e6c 100644 (file)
 */
 /**
  * @file gns/gnunet-service-gns.c
- * @brief GNUnet GNS service
+ * @brief GNU Name System (main service)
  * @author Martin Schanzenbach
  * @author Christian Grothoff
- *
- * TODO:
- * - conversion of private to public records does NOT check if the
- *   records are actually public 
- * - need to watch for client disconnects and abort operations on
- *   disconnect
  */
 #include "platform.h"
 #include "gnunet_util_lib.h"
-#include "gnunet_transport_service.h"
 #include "gnunet_dns_service.h"
 #include "gnunet_dnsparser_lib.h"
 #include "gnunet_dht_service.h"
@@ -39,8 +32,8 @@
 #include "gnunet_gns_service.h"
 #include "gnunet_statistics_service.h"
 #include "gns.h"
-#include "gns_common.h"
 #include "gnunet-service-gns_resolver.h"
+#include "gnunet-service-gns_shorten.h"
 #include "gnunet-service-gns_interceptor.h"
 #include "gnunet_protocols.h"
 
@@ -105,7 +98,7 @@ struct ClientLookupHandle
   struct GNS_ResolverHandle *lookup;
 
   /**
-   * request id 
+   * request id
    */
   uint32_t request_id;
 
@@ -177,11 +170,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
  */
@@ -211,9 +199,10 @@ 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);
     GNS_resolver_lookup_cancel (clh->lookup);
     GNUNET_CONTAINER_DLL_remove (clh_head, clh_tail, clh);
     GNUNET_free (clh);
@@ -221,6 +210,7 @@ shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
 
   GNS_interceptor_done ();
   GNS_resolver_done ();
+  GNS_shorten_done ();
   if (NULL != statistics)
   {
     GNUNET_STATISTICS_destroy (statistics, GNUNET_NO);
@@ -276,7 +266,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);
 
 
@@ -290,9 +280,10 @@ static void
 dht_put_continuation (void *cls,
                      int success)
 {
-  struct GNUNET_TIME_Relative next_put_interval; 
+  struct GNUNET_TIME_Relative next_put_interval;
 
-  num_public_records++;  
+  active_put = NULL;
+  num_public_records++;
   if ( (num_public_records > last_num_public_records) &&
        (GNUNET_NO == first_zone_iteration) )
   {
@@ -309,7 +300,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);
@@ -331,11 +322,15 @@ put_gns_record (void *cls,
                 const char *name,
                 unsigned int rd_count,
                 const struct GNUNET_NAMESTORE_RecordData *rd)
-{  
+{
   struct GNUNET_NAMESTORE_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];
+  unsigned int rd_public_count;
+  unsigned int i;
 
   if (NULL == name)
   {
@@ -370,8 +365,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,30 +377,45 @@ 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;
   }
 
-  /* FIXME: filter out records that are not public! */
+  /* filter out records that are not public, and convert to
+     absolute expiration time. */
+  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)))
+    {
+      rd_public[rd_public_count] = rd[i];
+      if (0 != (rd[i].flags & GNUNET_NAMESTORE_RF_RELATIVE_EXPIRATION))
+      {
+       rd_public[rd_public_count].flags &= ~GNUNET_NAMESTORE_RF_RELATIVE_EXPIRATION;
+       rd_public[rd_public_count].expiration_time += now.abs_value_us;
+      }
+      rd_public_count++;
+    }
 
   /* We got a set of records to publish */
-  if (0 == rd_count)
+  if (0 == rd_public_count)
   {
     zone_publish_task = GNUNET_SCHEDULER_add_now (&publish_zone_dht_next,
                                                    NULL);
     return;
   }
-  expire = GNUNET_NAMESTORE_record_get_expiration_time (rd_count,
-                                                       rd);
+  expire = GNUNET_NAMESTORE_record_get_expiration_time (rd_public_count,
+                                                       rd_public);
   block = GNUNET_NAMESTORE_block_create (key,
                                         expire,
                                         name,
-                                        rd,
-                                        rd_count);
-  block_size = ntohl (block->purpose.size) 
-    + sizeof (struct GNUNET_CRYPTO_EccSignature) 
-    + sizeof (struct GNUNET_CRYPTO_EccPublicKey);
+                                        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,
                                           name,
                                           &query);
@@ -419,7 +429,7 @@ put_gns_record (void *cls,
                               expire,
                               DHT_OPERATION_TIMEOUT,
                               &dht_put_continuation,
-                              NULL); 
+                              NULL);
   if (NULL == active_put)
   {
     GNUNET_break (0);
@@ -436,13 +446,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,
@@ -459,7 +469,7 @@ publish_zone_dht_start (void *cls,
  * Reply to client with the result from our lookup.
  *
  * @param cls the closure (our client lookup handle)
- * @param rd_count the number of records
+ * @param rd_count the number of records in @a rd
  * @param rd the record data
  */
 static void
@@ -470,35 +480,34 @@ send_lookup_response (void* cls,
   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)); 
+  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_NAMESTORE_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);
 }
 
@@ -522,9 +531,9 @@ handle_lookup (void *cls,
   const struct GNUNET_CRYPTO_EccPrivateKey *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))
@@ -535,7 +544,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;
@@ -548,8 +557,10 @@ 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);
   clh->client = client;
   clh->request_id = sh_msg->id;
@@ -560,7 +571,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) )
   {
@@ -569,18 +580,41 @@ 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);
 }
 
 
+/**
+ * One of our clients disconnected, clean up after it.
+ *
+ * @param cls NULL
+ * @param client the client that disconnected
+ */
+static void
+notify_client_disconnect (void *cls,
+                         struct GNUNET_SERVER_Client *client)
+{
+  struct ClientLookupHandle *clh;
+
+  if (NULL == client)
+    return;
+  clh = GNUNET_SERVER_client_get_user_context (client, struct ClientLookupHandle);
+  if (NULL == clh)
+    return;
+  GNS_resolver_lookup_cancel (clh->lookup);
+  GNUNET_CONTAINER_DLL_remove (clh_head, clh_tail, clh);
+  GNUNET_free (clh);
+}
+
+
 /**
  * Process GNS requests.
  *
@@ -596,9 +630,8 @@ run (void *cls, struct GNUNET_SERVER_Handle *server,
     { &handle_lookup, NULL, GNUNET_MESSAGE_TYPE_GNS_LOOKUP, 0},
     {NULL, NULL, 0, 0}
   };
-  struct GNUNET_CRYPTO_EccPublicKey dns_root;
+  struct GNUNET_CRYPTO_EccPublicSignKey dns_root;
   unsigned long long max_parallel_bg_queries = 0;
-  int ignore_pending = GNUNET_NO;
   char *dns_root_name;
 
   v6_enabled = GNUNET_NETWORK_test_pf (PF_INET6);
@@ -612,7 +645,7 @@ run (void *cls, struct GNUNET_SERVER_Handle *server,
     GNUNET_SCHEDULER_shutdown ();
     return;
   }
-  
+
   put_interval = INITIAL_PUT_INTERVAL;
   zone_publish_time_window = DEFAULT_ZONE_PUBLISH_TIME_WINDOW;
 
@@ -635,26 +668,6 @@ run (void *cls, struct GNUNET_SERVER_Handle *server,
                max_parallel_bg_queries);
   }
 
-  if (GNUNET_YES ==
-      GNUNET_CONFIGURATION_get_value_yesno (c, "gns",
-                                            "AUTO_IMPORT_CONFIRMATION_REQ"))
-  {
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-               "Auto import requires user confirmation\n");
-    ignore_pending = GNUNET_YES;
-  }
-
-  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)
@@ -664,30 +677,18 @@ run (void *cls, struct GNUNET_SERVER_Handle *server,
     GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
     return;
   }
-  
-  if (GNUNET_SYSERR ==
-      GNS_resolver_init (namestore_handle, dht_handle, 
-                        c,
-                        max_parallel_bg_queries,
-                        ignore_pending))
-  {
-    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
-               _("Unable to initialize resolver!\n"));
-    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_key_from_string (dns_root_name,
+       GNUNET_CRYPTO_ecc_public_sign_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);
@@ -696,7 +697,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))
     {
@@ -704,17 +705,21 @@ run (void *cls, struct GNUNET_SERVER_Handle *server,
       return;
     }
   }
-  /* FIXME: install client disconnect handle to clean up pending
-     lookups on client disconnect! */
-  
-  /* Schedule periodic put for our records. */  
+  GNS_resolver_init (namestore_handle, dht_handle,
+                    c,
+                    max_parallel_bg_queries);
+  GNS_shorten_init (namestore_handle, dht_handle);
+  GNUNET_SERVER_disconnect_notify (server,
+                                  &notify_client_disconnect,
+                                  NULL);
+  /* 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);
 }