-add cached flag for performance
authorMartin Schanzenbach <mschanzenbach@posteo.de>
Mon, 11 Jun 2012 15:50:40 +0000 (15:50 +0000)
committerMartin Schanzenbach <mschanzenbach@posteo.de>
Mon, 11 Jun 2012 15:50:40 +0000 (15:50 +0000)
src/gns/gns.h
src/gns/gns_api.c
src/gns/gnunet-gns-proxy.c
src/gns/gnunet-gns.c
src/gns/gnunet-service-gns.c
src/gns/gnunet-service-gns_interceptor.c
src/gns/gnunet-service-gns_resolver.c
src/gns/gnunet-service-gns_resolver.h
src/include/gnunet_gns_service.h

index 80ebb128877be554fc4f3f0e3d5f12d68ee62188..686729dd78132231334796a7a29e83b2f51a320a 100644 (file)
@@ -52,6 +52,11 @@ struct GNUNET_GNS_ClientLookupMessage
    */
   uint32_t id GNUNET_PACKED;
 
+  /**
+   * Only check cached results
+   */
+  uint32_t only_cached GNUNET_PACKED;
+
   /**
    * Should we look up in the default zone?
    */
index 2cdb58a319fe545d7d8723842e44fee62f07970f..2e1e9712cc066417e1c49a766a7ea60779914f1f 100644 (file)
@@ -642,6 +642,7 @@ get_request_id (struct GNUNET_GNS_Handle *h)
  * @param zone the zone to start the resolution in
  * @param shorten_zone the zone where to shorten names into
  * @param type the record type to look up
+ * @param only_cached GNUNET_NO to only check locally not DHT for performance
  * @param proc processor to call on result
  * @param proc_cls closure for processor
  * @return handle to the get
@@ -652,6 +653,7 @@ GNUNET_GNS_lookup_zone (struct GNUNET_GNS_Handle *handle,
                    struct GNUNET_CRYPTO_ShortHashCode *zone,
                    struct GNUNET_CRYPTO_ShortHashCode *shorten_zone,
                    enum GNUNET_GNS_RecordType type,
+                   int only_cached,
                    GNUNET_GNS_LookupResultProcessor proc,
                    void *proc_cls)
 {
@@ -686,6 +688,7 @@ GNUNET_GNS_lookup_zone (struct GNUNET_GNS_Handle *handle,
   lookup_msg->header.type = htons (GNUNET_MESSAGE_TYPE_GNS_LOOKUP);
   lookup_msg->header.size = htons (msize);
   lookup_msg->id = htonl(qe->r_id);
+  lookup_msg->only_cached = htonl(only_cached);
 
   if (NULL != zone)
   {
@@ -728,6 +731,7 @@ GNUNET_GNS_lookup_zone (struct GNUNET_GNS_Handle *handle,
  * @param handle handle to the GNS service
  * @param name the name to look up
  * @param type the record type to look up
+ * @param only_cached GNUNET_NO to only check locally not DHT for performance
  * @param proc processor to call on result
  * @param proc_cls closure for processor
  * @return handle to the get
@@ -736,12 +740,13 @@ struct GNUNET_GNS_QueueEntry *
 GNUNET_GNS_lookup (struct GNUNET_GNS_Handle *handle,
                    const char * name,
                    enum GNUNET_GNS_RecordType type,
+                   int only_cached,
                    GNUNET_GNS_LookupResultProcessor proc,
                    void *proc_cls)
 {
   return GNUNET_GNS_lookup_zone (handle, name,
                                  NULL, NULL,
-                                 type, proc, proc_cls);
+                                 type, only_cached, proc, proc_cls);
 }
 
 /**
index eff7fbfbdc17eda0f504695e936b55b950a8a879..ea1219b29897e9d7cf26bb6d0e4eb304da7face9 100644 (file)
@@ -1010,14 +1010,26 @@ process_leho_lookup (void *cls,
   if (ctask->mhd->is_ssl)
   {
     phost = (struct hostent*)gethostbyname (ctask->host);
-    ssl_ip = inet_ntoa(*((struct in_addr*)(phost->h_addr)));
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                "SSL target server: %s\n", ssl_ip);
-    sprintf (resolvename, "%s:%d:%s", ctask->leho, HTTPS_PORT, ssl_ip);
-    ctask->resolver = curl_slist_append ( ctask->resolver, resolvename);
-    curl_easy_setopt (ctask->curl, CURLOPT_RESOLVE, ctask->resolver);
-    sprintf (curlurl, "https://%s%s", ctask->leho, ctask->url);
-    curl_easy_setopt (ctask->curl, CURLOPT_URL, curlurl);
+
+    if (phost!=NULL)
+    {
+      ssl_ip = inet_ntoa(*((struct in_addr*)(phost->h_addr)));
+      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                  "SSL target server: %s\n", ssl_ip);
+      sprintf (resolvename, "%s:%d:%s", ctask->leho, HTTPS_PORT, ssl_ip);
+      ctask->resolver = curl_slist_append ( ctask->resolver, resolvename);
+      curl_easy_setopt (ctask->curl, CURLOPT_RESOLVE, ctask->resolver);
+      sprintf (curlurl, "https://%s%s", ctask->leho, ctask->url);
+      curl_easy_setopt (ctask->curl, CURLOPT_URL, curlurl);
+    }
+    else
+    {
+      GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+                  "gethostbyname failed for %s!\n", ctask->host);
+      ctask->download_successful = GNUNET_NO;
+      ctask->download_error = GNUNET_YES;
+      return;
+    }
   }
 
   if (CURLM_OK != (mret=curl_multi_add_handle (curl_multi, ctask->curl)))
@@ -1067,6 +1079,7 @@ process_get_authority (void *cls,
                           &local_gns_zone,
                           &local_shorten_zone,
                           GNUNET_GNS_RECORD_LEHO,
+                          GNUNET_YES, //Only cached for performance
                           &process_leho_lookup,
                           ctask);
 }
index 78fe513ec4b15b67c7df4983caba9a37d1f75edf..6b0ae5dcadf280bdfccdb7de6c8d3b88c4d04128 100644 (file)
@@ -151,6 +151,7 @@ run (void *cls, char *const *args, const char *cfgfile,
   struct GNUNET_CRYPTO_ShortHashCode *zone = NULL;
   struct GNUNET_CRYPTO_ShortHashCode *shorten_zone = NULL;
   struct GNUNET_CRYPTO_ShortHashCode user_zone;
+  struct GNUNET_CRYPTO_ShortHashCode user_shorten_zone;
   struct GNUNET_CRYPTO_ShortHashAsciiEncoded zonename;
 
   if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "gns",
@@ -197,12 +198,12 @@ run (void *cls, char *const *args, const char *cfgfile,
       GNUNET_CRYPTO_rsa_key_get_public (key, &pkey);
       GNUNET_CRYPTO_short_hash(&pkey,
                          sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
-                         &user_zone);
-      shorten_zone = &user_zone;
+                         &user_shorten_zone);
+      shorten_zone = &user_shorten_zone;
       GNUNET_CRYPTO_short_hash_to_enc (shorten_zone, &zonename);
       if (!raw)
         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                    "Using zone: %s!\n", &zonename);
+                    "Using shorten zone: %s!\n", &zonename);
       GNUNET_CRYPTO_rsa_key_free(key);
     }
     GNUNET_free(keyfile);
@@ -237,6 +238,7 @@ run (void *cls, char *const *args, const char *cfgfile,
     GNUNET_GNS_lookup_zone (gns, lookup_name,
                             zone, shorten_zone,
                             rtype,
+                            GNUNET_YES, //Use DHT
                             &process_lookup_result, lookup_name);
   }
 
index 8f8fbe41b648144c561f6d20d85afa40f5b3bbaa..543ccf5181e23dba9bed84bc07cb93a8523c4ed0 100644 (file)
@@ -825,6 +825,7 @@ handle_lookup(void *cls,
   char* nameptr = name;
   struct GNUNET_CRYPTO_RsaPrivateKey *key = NULL;
   struct GNUNET_CRYPTO_ShortHashCode zone;
+  int only_cached;
 
   if (ntohs (message->size) < sizeof (struct GNUNET_GNS_ClientLookupMessage))
   {
@@ -856,6 +857,8 @@ handle_lookup(void *cls,
   clh->unique_id = sh_msg->id;
   clh->type = ntohl(sh_msg->type);
   clh->shorten_key = NULL;
+
+  only_cached = ntohl(sh_msg->only_cached);
   
   if (strlen (name) > MAX_DNS_NAME_LENGTH) {
     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
@@ -883,6 +886,7 @@ handle_lookup(void *cls,
     gns_resolver_lookup_record(zone, zone, clh->type, name,
                                key,
                                default_lookup_timeout,
+                               only_cached,
                                &send_lookup_response, clh);
   }
   else
@@ -890,6 +894,7 @@ handle_lookup(void *cls,
     gns_resolver_lookup_record(zone, zone, clh->type, name,
                                NULL,
                                default_lookup_timeout,
+                               only_cached,
                                &send_lookup_response, clh);
   }
 }
index adb09ca7f2463340430b08723aeb98ce15fb9f86..0e44755edfc0033edb413c39df153efa124f9c20 100644 (file)
@@ -246,6 +246,7 @@ start_resolution_for_dns(struct GNUNET_DNS_RequestHandle *request,
   gns_resolver_lookup_record(our_zone, our_zone, q->type, q->name,
                              our_key,
                              default_lookup_timeout,
+                             GNUNET_NO,
                              &reply_to_dns, ilh);
 }
 
index e2e99b56e250f65b811b0e2dec8191e1e65e4b52..9883f7c451dfb16b5e182f83f0e5594c8cd30981 100644 (file)
@@ -638,6 +638,7 @@ dht_lookup_timeout(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
                              new_name,
                              rh->priv_key,
                              GNUNET_TIME_UNIT_FOREVER_REL,
+                             GNUNET_NO,
                              &background_lookup_result_processor,
                              NULL);
   rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
@@ -1085,6 +1086,7 @@ dht_authority_lookup_timeout(void *cls,
                              new_name,
                              rh->priv_key,
                              GNUNET_TIME_UNIT_FOREVER_REL,
+                             GNUNET_NO,
                              &background_lookup_result_processor,
                              NULL);
 
@@ -1496,7 +1498,7 @@ handle_record_ns(void* cls, struct ResolverHandle *rh,
                rh->status);
     
     /**
-     * There are 4 conditions that have to met for us to consult the DHT:
+     * There are 5 conditions that have to met for us to consult the DHT:
      * 1. The entry in the DHT is RSL_RECORD_EXPIRED AND
      * 2. No entry in the NS existed AND
      * 3. The zone queried is not the local resolver's zone AND
@@ -1504,11 +1506,13 @@ handle_record_ns(void* cls, struct ResolverHandle *rh,
      *    because if it was any other canonical name we either already queried
      *    the DHT for the authority in the authority lookup phase (and thus
      *    would already have an entry in the NS for the record)
+     * 5. We are not in cache only mode
      */
     if (rh->status & (RSL_RECORD_EXPIRED | !RSL_RECORD_EXISTS) &&
         GNUNET_CRYPTO_short_hash_cmp(&rh->authority_chain_head->zone,
                                      &rh->private_local_zone) &&
-        (strcmp(rh->name, "+") == 0))
+        (strcmp(rh->name, "+") == 0) &&
+        (rh->only_cached == GNUNET_NO))
     {
       rh->proc = &handle_record_dht;
       resolve_record_dht(rh);
@@ -1822,6 +1826,15 @@ handle_delegation_ns(void* cls, struct ResolverHandle *rh,
     }
     return;
   }
+
+  if (rh->only_cached == GNUNET_YES)
+  {
+    GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
+               "GNS_PHASE_DELEGATE_NS-%llu: Only cache resolution, no result\n",
+               rh->id, rh->name);
+    finish_lookup(rh, rlh, rd_count, rd);
+    return;
+  }
   
   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
       "GNS_PHASE_DELEGATE_NS-%llu: Trying to resolve delegation for %s via DHT\n",
@@ -2060,6 +2073,7 @@ resolve_delegation_ns(struct ResolverHandle *rh)
  * @param name the name to look up
  * @param key a private key for use with PSEU import (can be NULL)
  * @param timeout timeout for resolution
+ * @param only_cached GNUNET_NO to only check locally not DHT for performance
  * @param proc the processor to call on result
  * @param cls the closure to pass to proc
  */
@@ -2070,6 +2084,7 @@ gns_resolver_lookup_record(struct GNUNET_CRYPTO_ShortHashCode zone,
                            const char* name,
                            struct GNUNET_CRYPTO_RsaPrivateKey *key,
                            struct GNUNET_TIME_Relative timeout,
+                           int only_cached,
                            RecordLookupProcessor proc,
                            void* cls)
 {
@@ -2102,6 +2117,7 @@ gns_resolver_lookup_record(struct GNUNET_CRYPTO_ShortHashCode zone,
   rh->timeout = timeout;
   rh->get_handle = NULL;
   rh->private_local_zone = pzone;
+  rh->only_cached = only_cached;
 
   if (timeout.rel_value != GNUNET_TIME_UNIT_FOREVER_REL.rel_value)
   {
index 8222397c4fd791228add0944ddf51c791660c503..f1f25a5c157cd3044e48c9508fee4732d24d8d20 100644 (file)
@@ -114,6 +114,9 @@ struct ResolverHandle
   /* has this query been answered? how many matches */
   int answered;
 
+  /* Use only cache */
+  int only_cached;
+
   /* the authoritative zone to query */
   struct GNUNET_CRYPTO_ShortHashCode authority;
 
@@ -284,6 +287,7 @@ gns_resolver_cleanup(ResolverCleanupContinuation cont);
  * @param name the name to look up
  * @param key optional private key for authority caching
  * @param timeout timeout for the resolution
+ * @param only_cached GNUNET_NO to only check locally not DHT for performance
  * @param proc the processor to call
  * @param cls the closure to pass to proc
  */
@@ -294,6 +298,7 @@ gns_resolver_lookup_record(struct GNUNET_CRYPTO_ShortHashCode zone,
                            const char* name,
                            struct GNUNET_CRYPTO_RsaPrivateKey *key,
                            struct GNUNET_TIME_Relative timeout,
+                           int only_cached,
                            RecordLookupProcessor proc,
                            void* cls);
 
index 1906aba4547147050eebb1a7e0764951361f67c2..4d04addf9cff84845a0cfbed801585cf1ef1c70b 100644 (file)
@@ -127,6 +127,7 @@ typedef void (*GNUNET_GNS_LookupResultProcessor) (void *cls,
  * @param handle handle to the GNS service
  * @param name the name to look up
  * @param type the GNUNET_GNS_RecordType to look for
+ * @param only_cached GNUNET_NO to only check locally not DHT for performance
  * @param proc function to call on result
  * @param proc_cls closure for processor
  *
@@ -136,6 +137,7 @@ struct GNUNET_GNS_QueueEntry *
 GNUNET_GNS_lookup (struct GNUNET_GNS_Handle *handle,
                          const char * name,
                          enum GNUNET_GNS_RecordType type,
+                         int only_cached,
                          GNUNET_GNS_LookupResultProcessor proc,
                          void *proc_cls);
 
@@ -148,6 +150,7 @@ GNUNET_GNS_lookup (struct GNUNET_GNS_Handle *handle,
  * @param zone the zone to start the resolution in
  * @param shorten_zone the zone where to shorten names into
  * @param type the GNUNET_GNS_RecordType to look for
+ * @param only_cached GNUNET_NO to only check locally not DHT for performance
  * @param proc function to call on result
  * @param proc_cls closure for processor
  *
@@ -159,6 +162,7 @@ GNUNET_GNS_lookup_zone (struct GNUNET_GNS_Handle *handle,
                          struct GNUNET_CRYPTO_ShortHashCode *zone,
                          struct GNUNET_CRYPTO_ShortHashCode *shorten_zone,
                          enum GNUNET_GNS_RecordType type,
+                         int only_cached,
                          GNUNET_GNS_LookupResultProcessor proc,
                          void *proc_cls);