-remove trailing whitespace
[oweals/gnunet.git] / src / gns / gnunet-service-gns_resolver.c
index 14e714a85855550b3fd4824a879cd90959688674..ff07a6cf07727a59e14e7ea0e1689aafe89f0907 100644 (file)
@@ -41,6 +41,7 @@
 #include "gnunet_resolver_service.h"
 #include "gnunet_revocation_service.h"
 #include "gnunet_dnsparser_lib.h"
+#include "gnunet_tun_lib.h"
 #include "gnunet_gns_service.h"
 #include "gns.h"
 #include "gnunet-service-gns_resolver.h"
@@ -1413,9 +1414,8 @@ handle_gns_resolution_result (void *cls,
              GNS_resolver_lookup_cancel (rh);
              return;
            }
-           GNUNET_CRYPTO_hash (vname,
-                               strlen (vname), // FIXME: +1?
-                               &vhash);
+           GNUNET_TUN_service_name_to_hash (vname,
+                                             &vhash);
             GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                         "Attempting VPN allocation for %s-%s (AF: %d, proto %d)\n",
                         GNUNET_i2s (&vpn->peer),
@@ -1937,14 +1937,23 @@ handle_dht_response (void *cls,
                               co);
 }
 
+
+/**
+ * Initiate a DHT query for a set of GNS records.
+ *
+ * @param rh resolution handle
+ * @param query key to use in the DHT lookup
+ */
 static void
-start_dht_request (struct GNS_ResolverHandle *rh, struct GNUNET_HashCode query)
+start_dht_request (struct GNS_ResolverHandle *rh,
+                   const struct GNUNET_HashCode *query)
 {
   struct GNS_ResolverHandle *rx;
+
   GNUNET_assert (NULL == rh->get_handle);
   rh->get_handle = GNUNET_DHT_get_start (dht_handle,
                                          GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
-                                         &query,
+                                         query,
                                          DHT_GNS_REPLICATION_LEVEL,
                                          GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
                                          NULL, 0,
@@ -1963,6 +1972,80 @@ start_dht_request (struct GNS_ResolverHandle *rh, struct GNUNET_HashCode query)
 }
 
 
+/**
+ * Process a records that were decrypted from a block that we
+ * got from the namecache.  If the desired record type is not
+ * included, we should query the DHT.  Otherwise, we should
+ * simply call #handle_gns_resolution_result().
+ *
+ * @param cls closure with the `struct GNS_ResolverHandle`
+ * @param rd_count number of entries in @a rd array
+ * @param rd array of records with data to store
+ */
+static void
+handle_gns_namecache_resolution_result (void *cls,
+                                        unsigned int rd_count,
+                                        const struct GNUNET_GNSRECORD_Data *rd)
+{
+  struct GNS_ResolverHandle *rh = cls;
+  unsigned int i;
+  int found;
+
+  found = GNUNET_NO;
+  for (i=0;i<rd_count;i++)
+  {
+    if (rd[i].record_type == rh->record_type)
+    {
+      found = GNUNET_YES;
+      break;
+    }
+    switch (rd[i].record_type)
+    {
+    case GNUNET_GNSRECORD_TYPE_VPN:
+      if ( (GNUNET_DNSPARSER_TYPE_A == rh->record_type) ||
+           (GNUNET_DNSPARSER_TYPE_AAAA == rh->record_type) )
+      {
+        found = GNUNET_YES;
+        break;
+      }
+      break;
+    case GNUNET_DNSPARSER_TYPE_CNAME:
+    case GNUNET_GNSRECORD_TYPE_PKEY:
+    case GNUNET_GNSRECORD_TYPE_GNS2DNS:
+      /* delegations always count as 'found' */
+      found = GNUNET_YES;
+      break;
+    default:
+      break;
+    }
+  }
+  if (GNUNET_YES == found)
+  {
+    handle_gns_resolution_result (rh,
+                                  rd_count,
+                                  rd);
+  }
+  else
+  {
+    /* try DHT */
+    struct AuthorityChain *ac = rh->ac_tail;
+    const char *label = ac->label;
+    const struct GNUNET_CRYPTO_EcdsaPublicKey *auth = &ac->authority_info.gns_authority;
+    struct GNUNET_HashCode query;
+
+    GNUNET_GNSRECORD_query_from_public_key (auth,
+                                            label,
+                                            &query);
+    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                "Starting DHT lookup for `%s' in zone `%s' under key `%s'\n",
+                ac->label,
+                GNUNET_GNSRECORD_z2s (&ac->authority_info.gns_authority),
+                GNUNET_h2s (&query));
+    start_dht_request (rh, &query);
+  }
+}
+
+
 /**
  * Process a record that was stored in the namecache.
  *
@@ -1986,15 +2069,16 @@ handle_namecache_block_response (void *cls,
         (0 == GNUNET_TIME_absolute_get_remaining (GNUNET_TIME_absolute_ntoh (block->expiration_time)).rel_value_us) ) )
   {
     /* namecache knows nothing; try DHT lookup */
+    GNUNET_GNSRECORD_query_from_public_key (auth,
+                                            label,
+                                            &query);
     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                 "Starting DHT lookup for `%s' in zone `%s' under key `%s'\n",
                 ac->label,
                 GNUNET_GNSRECORD_z2s (&ac->authority_info.gns_authority),
                 GNUNET_h2s (&query));
-    GNUNET_GNSRECORD_query_from_public_key (auth,
-                                            label,
-                                            &query);
-    start_dht_request(rh, query);
+    start_dht_request (rh, &query);
+    return;
   }
 
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
@@ -2017,12 +2101,20 @@ handle_namecache_block_response (void *cls,
       GNUNET_GNSRECORD_block_decrypt (block,
                                      auth,
                                      label,
-                                     &handle_gns_resolution_result,
+                                     &handle_gns_namecache_resolution_result,
                                      rh))
   {
     GNUNET_break_op (0); /* block was ill-formed */
-    rh->proc (rh->proc_cls, 0, NULL);
-    GNS_resolver_lookup_cancel (rh);
+    /* try DHT instead */
+    GNUNET_GNSRECORD_query_from_public_key (auth,
+                                            label,
+                                            &query);
+    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                "Starting DHT lookup for `%s' in zone `%s' under key `%s'\n",
+                ac->label,
+                GNUNET_GNSRECORD_z2s (&ac->authority_info.gns_authority),
+                GNUNET_h2s (&query));
+    start_dht_request (rh, &query);
     return;
   }
 }
@@ -2055,7 +2147,9 @@ recursive_gns_resolution_namecache (struct GNS_ResolverHandle *rh)
     GNUNET_assert (NULL != rh->namecache_qe);
   }
   else
-    start_dht_request (rh, query);
+  {
+    start_dht_request (rh, &query);
+  }
 }