-cleanup
[oweals/gnunet.git] / src / gns / gnunet-service-gns_resolver.c
index 0844920b9c3330e705ca611ac77edf38dbd6bb44..7258f97a145db1d532cbae4ff7b4955ebdffe7c2 100644 (file)
@@ -19,8 +19,6 @@
 */
 
 /**
- *
- *
  * @file gns/gnunet-service-gns_resolver.c
  * @brief GNUnet GNS resolver logic
  * @author Martin Schanzenbach
@@ -131,6 +129,11 @@ static struct ResolverHandle *nah_head;
  */
 static struct ResolverHandle *nah_tail;
 
+/**
+ * Global configuration.
+ */
+static const struct GNUNET_CONFIGURATION_Handle *cfg;
+
 /**
  * a resolution identifier pool variable
  * FIXME overflow?
@@ -138,28 +141,36 @@ static struct ResolverHandle *nah_tail;
  */
 static unsigned long long rid = 0;
 
+/*
+ * Check if name is in srv format (_x._y.xxx)
+ *
+ * @param name
+ * @return GNUNET_YES if true
+ */
 static int
 is_srv (char* name)
 {
   char* ndup;
-  int ret = 1;
+  int ret = GNUNET_YES;
 
   if (*name != '_')
-    return 0;
+    return GNUNET_NO;
   if (NULL == strstr (name, "._"))
-    return 0;
+    return GNUNET_NO;
 
   ndup = GNUNET_strdup (name);
   strtok (ndup, ".");
 
   if (NULL == strtok (NULL, "."))
-    ret = 0;
+    ret = GNUNET_NO;
 
   if (NULL == strtok (NULL, "."))
-    ret = 0;
+    ret = GNUNET_NO;
 
   if (NULL != strtok (NULL, "."))
-    ret = 0;
+    ret = GNUNET_NO;
+
+  GNUNET_free (ndup);
 
   return ret;
 }
@@ -171,10 +182,10 @@ is_srv (char* name)
  * a           = canonical
  *
  * @param name the name to test
- * @return 1 if canonical
+ * @return GNUNET_YES if canonical
  */
 static int
-is_canonical(char* name)
+is_canonical (char* name)
 {
   char* ndup;
   char* tok;
@@ -190,10 +201,10 @@ is_canonical(char* name)
     if (*tok == '_')
       continue;
     GNUNET_free (ndup);
-    return 0;
+    return GNUNET_NO;
   }
   GNUNET_free (ndup);
-  return 1;
+  return GNUNET_YES;
 }
 
 
@@ -719,20 +730,21 @@ start_shorten (struct AuthorityChain *atail,
  * @param nh the namestore handle
  * @param dh the dht handle
  * @param lz the local zone's hash
- * @param cfg configuration handle
+ * @param c configuration handle
  * @param max_bg_queries maximum number of parallel background queries in dht
  * @param ignore_pending ignore records that still require user confirmation
  *        on lookup
  * @return GNUNET_OK on success
  */
 int
-gns_resolver_init(struct GNUNET_NAMESTORE_Handle *nh,
-                  struct GNUNET_DHT_Handle *dh,
-                  struct GNUNET_CRYPTO_ShortHashCode lz,
-                  const struct GNUNET_CONFIGURATION_Handle *cfg,
-                  unsigned long long max_bg_queries,
-                  int ignore_pending)
+gns_resolver_init (struct GNUNET_NAMESTORE_Handle *nh,
+                  struct GNUNET_DHT_Handle *dh,
+                  struct GNUNET_CRYPTO_ShortHashCode lz,
+                  const struct GNUNET_CONFIGURATION_Handle *c,
+                  unsigned long long max_bg_queries,
+                  int ignore_pending)
 {
+  cfg = c;
   namestore_handle = nh;
   dht_handle = dh;
   local_zone = lz;
@@ -756,17 +768,6 @@ gns_resolver_init(struct GNUNET_NAMESTORE_Handle *nh,
   
   GNUNET_RESOLVER_connect (cfg);
   
-  if (NULL == vpn_handle)
-  {
-    vpn_handle = GNUNET_VPN_connect (cfg);
-    if (NULL == vpn_handle)
-    {
-      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                  "GNS_PHASE_INIT: Error connecting to VPN!\n");
-
-      return GNUNET_SYSERR;
-    }
-  }
 
   if ((namestore_handle != NULL) && (dht_handle != NULL))
   {
@@ -911,6 +912,9 @@ free_resolver_handle (struct ResolverHandle* rh)
     GNUNET_NETWORK_socket_close (rh->dns_sock);
   if (NULL != rh->dns_resolver_handle)
     GNUNET_RESOLVER_request_cancel (rh->dns_resolver_handle);
+
+  if (NULL != rh->rd.data)
+    GNUNET_free ((void*)(rh->rd.data));
   GNUNET_free(rh);
 }
 
@@ -1099,7 +1103,7 @@ dht_lookup_timeout(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
                              rh->private_local_zone,
                              rlh->record_type,
                              new_name,
-                             rh->priv_key,
+                             NULL,
                              GNUNET_TIME_UNIT_FOREVER_REL,
                              GNUNET_NO,
                              &background_lookup_result_processor,
@@ -1883,7 +1887,8 @@ resolve_record_dns (struct ResolverHandle *rh,
     rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
   }
   /* Start shortening */
-  if ((rh->priv_key != NULL) && is_canonical (rh->name))
+  if ((rh->priv_key != NULL) &&
+      (is_canonical (rh->name) == GNUNET_YES))
   {
     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
              "GNS_PHASE_REC_DNS-%llu: Trying to shorten authority chain\n",
@@ -2002,7 +2007,8 @@ resolve_record_vpn (struct ResolverHandle *rh,
     rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
   }
   /* Start shortening */
-  if ((rh->priv_key != NULL) && is_canonical (rh->name))
+  if ((rh->priv_key != NULL) &&
+      (is_canonical (rh->name) == GNUNET_YES))
   {
     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
              "GNS_PHASE_REC_VPN-%llu: Trying to shorten authority chain\n",
@@ -2045,14 +2051,27 @@ resolve_record_vpn (struct ResolverHandle *rh,
     af = AF_INET6;
   
   //FIXME timeout??
+  if (NULL == vpn_handle)
+  {
+    vpn_handle = GNUNET_VPN_connect (cfg);
+    if (NULL == vpn_handle)
+    {
+      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                  "GNS_PHASE_INIT: Error connecting to VPN!\n");
+      finish_lookup (rh, rh->proc_cls, 0, NULL);
+      return;
+    }
+  }
+
+
   rh->vpn_handle = GNUNET_VPN_redirect_to_peer (vpn_handle,
-                                          af, ntohs (vpn->proto),
-                                          (struct GNUNET_PeerIdentity *)&vpn->peer,
-                                          &serv_desc,
-                                          GNUNET_NO, //nac
-                                          GNUNET_TIME_UNIT_FOREVER_ABS, //FIXME
-                                          &process_record_result_vpn,
-                                          rh);
+                                               af, ntohs (vpn->proto),
+                                               (struct GNUNET_PeerIdentity *)&vpn->peer,
+                                               &serv_desc,
+                                               GNUNET_NO, //nac
+                                               GNUNET_TIME_UNIT_FOREVER_ABS, //FIXME
+                                               &process_record_result_vpn,
+                                               rh);
 
 }
 
@@ -2075,7 +2094,8 @@ resolve_record_ns(struct ResolverHandle *rh)
     rh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
   }
   /* Start shortening */
-  if ((rh->priv_key != NULL) && is_canonical (rh->name))
+  if ((rh->priv_key != NULL) &&
+     (is_canonical (rh->name) == GNUNET_YES))
   {
     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
              "GNS_PHASE_REC-%llu: Trying to shorten authority chain\n",
@@ -2165,7 +2185,7 @@ dht_authority_lookup_timeout(void *cls,
                              rh->private_local_zone,
                              rlh->record_type,
                              new_name,
-                             rh->priv_key,
+                             NULL,
                              GNUNET_TIME_UNIT_FOREVER_REL,
                              GNUNET_NO,
                              &background_lookup_result_processor,
@@ -2252,7 +2272,7 @@ process_pkey_revocation_result_ns (void *cls,
                                  rh->private_local_zone,
                                  GNUNET_GNS_RECORD_REV,
                                  GNUNET_GNS_TLD,
-                                 rh->priv_key,
+                                 NULL,
                                  GNUNET_TIME_UNIT_FOREVER_REL,
                                  GNUNET_NO,
                                  &background_lookup_result_processor,
@@ -2267,9 +2287,10 @@ process_pkey_revocation_result_ns (void *cls,
    * else resolve again with new authority
    */
   if (strcmp (rh->name, "") == 0)
-    rh->proc (rh->proc_cls, rh, 0, NULL);
+    rh->proc (rh->proc_cls, rh, rh->rd_count, &rh->rd);
   else
     resolve_delegation_ns (rh);
+
   return;
 }
 
@@ -2439,6 +2460,14 @@ process_delegation_result_dht(void* cls,
                                      rh->authority_chain_tail,
                                      auth);
 
+        if (NULL != rh->rd.data)
+          GNUNET_free ((void*)rh->rd.data);
+        
+        memcpy (&rh->rd, &rd[i], sizeof (struct GNUNET_NAMESTORE_RecordData));
+        rh->rd.data = GNUNET_malloc (rd[i].data_size);
+        memcpy ((void*)(rh->rd.data), rd[i].data, rd[i].data_size);
+        rh->rd_count = 1;
+
         /** try to import pkey if private key available */
         //if (rh->priv_key && is_canonical (rh->name))
         //  process_discovered_authority(name, auth->zone,
@@ -2500,7 +2529,8 @@ process_delegation_result_dht(void* cls,
     if (strcmp(rh->name, "") == 0)
     {
       /* Start shortening */
-      if ((rh->priv_key != NULL) && is_canonical (rh->name))
+      if ((rh->priv_key != NULL) &&
+          (is_canonical (rh->name) == GNUNET_YES))
       {
         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
              "GNS_PHASE_DELEGATE_DHT-%llu: Trying to shorten authority chain\n",
@@ -2512,6 +2542,7 @@ process_delegation_result_dht(void* cls,
     else
       rh->proc = &handle_delegation_ns;
 
+
     /* Check for key revocation and delegate */
     rh->namestore_task = GNUNET_NAMESTORE_lookup_record (namestore_handle,
                                     &rh->authority,
@@ -2787,7 +2818,7 @@ handle_record_ns (void* cls, struct ResolverHandle *rh,
                                           &rh->private_local_zone))
       check_dht = GNUNET_NO;
     
-    if ((strcmp (rh->name, "+") != 0) && (is_srv (rh->name) != 0))
+    if ((strcmp (rh->name, "+") != 0) && (is_srv (rh->name) == GNUNET_YES))
         check_dht = GNUNET_NO;
 
 
@@ -2827,7 +2858,7 @@ pop_tld(char* name, char* dest)
 {
   uint32_t len;
 
-  if (is_canonical (name))
+  if (is_canonical (name) == GNUNET_YES)
   {
     strcpy(dest, name);
     strcpy(name, "");
@@ -2916,7 +2947,7 @@ handle_delegation_dht(void* cls, struct ResolverHandle *rh,
   /**
    * we still have some left
    **/
-  if (is_canonical(rh->name))
+  if (is_canonical (rh->name) == GNUNET_YES)
   {
     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
              "GNS_PHASE_DELEGATE_DHT-%llu: Resolving canonical record %s in ns\n",
@@ -3139,7 +3170,6 @@ handle_delegation_ns (void* cls, struct ResolverHandle *rh,
       }
       else if (rlh->record_type == GNUNET_GNS_RECORD_PKEY)
       {
-        GNUNET_assert(rd_count == 1);
         GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
                    "GNS_PHASE_DELEGATE_NS-%llu: Resolved queried PKEY in NS.\n",
                    rh->id);
@@ -3196,7 +3226,7 @@ handle_delegation_ns (void* cls, struct ResolverHandle *rh,
 
   if (check_dht == GNUNET_NO)
   {
-    if (is_canonical(rh->name))
+    if (is_canonical (rh->name) == GNUNET_YES)
     {
       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
                  "GNS_PHASE_DELEGATE_NS-%llu: Resolving canonical record %s\n",
@@ -3424,7 +3454,13 @@ process_delegation_result_ns (void* cls,
     GNUNET_CONTAINER_DLL_insert (rh->authority_chain_head,
                                  rh->authority_chain_tail,
                                  auth);
+    if (NULL != rh->rd.data)
+      GNUNET_free ((void*)(rh->rd.data));
     
+    memcpy (&rh->rd, &rd[i], sizeof (struct GNUNET_NAMESTORE_RecordData));
+    rh->rd.data = GNUNET_malloc (rd[i].data_size);
+    memcpy ((void*)rh->rd.data, rd[i].data, rd[i].data_size);
+    rh->rd_count = 1;
     /* Check for key revocation and delegate */
     rh->namestore_task = GNUNET_NAMESTORE_lookup_record (namestore_handle,
                                     &rh->authority,
@@ -3445,10 +3481,11 @@ process_delegation_result_ns (void* cls,
    * If we have found some records for the LAST label
    * we return the results. Else null.
    */
-  if (strcmp(rh->name, "") == 0)
+  if (strcmp (rh->name, "") == 0)
   {
     /* Start shortening */
-    if ((rh->priv_key != NULL) && is_canonical (rh->name))
+    if ((rh->priv_key != NULL) &&
+        (is_canonical (rh->name) == GNUNET_YES))
     {
       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
               "GNS_PHASE_DELEGATE_NS-%llu: Trying to shorten authority chain\n",
@@ -3457,15 +3494,18 @@ process_delegation_result_ns (void* cls,
                     rh->priv_key);
     }
     /* simply promote back */
-    GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
-               "GNS_PHASE_DELEGATE_NS-%llu: Promoting %s back to name\n",
-               rh->id, rh->authority_name);
-    strcpy(rh->name, rh->authority_name);
-    rh->proc(rh->proc_cls, rh, rd_count, rd);
+    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                "GNS_PHASE_DELEGATE_NS-%llu: Promoting %s back to name\n",
+                rh->id, rh->authority_name);
+    strcpy (rh->name, rh->authority_name);
+    rh->proc (rh->proc_cls, rh, rd_count, rd);
   }
   else
   {
-    rh->proc(rh->proc_cls, rh, 0, NULL);
+    GNUNET_snprintf (new_name, MAX_DNS_NAME_LENGTH,
+                     "%s.%s", rh->name, rh->authority_name);
+    strcpy (rh->name, new_name);
+    rh->proc (rh->proc_cls, rh, 0, NULL);
   }
 }
 
@@ -3528,7 +3568,8 @@ gns_resolver_lookup_record (struct GNUNET_CRYPTO_ShortHashCode zone,
               name, record_type);
 
   
-  if (is_canonical((char*)name) && (strcmp(GNUNET_GNS_TLD, name) != 0))
+  if ((is_canonical ((char*)name) == GNUNET_YES) &&
+      (strcmp(GNUNET_GNS_TLD, name) != 0))
   {
     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                 "%s is canonical and not gnunet -> cannot resolve!\n", name);
@@ -3549,6 +3590,7 @@ gns_resolver_lookup_record (struct GNUNET_CRYPTO_ShortHashCode zone,
   rh->private_local_zone = pzone;
   rh->only_cached = only_cached;
   rh->namestore_task = NULL;
+  rh->rd.data = NULL;
 
   GNUNET_CONTAINER_DLL_insert (rlh_head, rlh_tail, rh);
   
@@ -3622,7 +3664,7 @@ gns_resolver_lookup_record (struct GNUNET_CRYPTO_ShortHashCode zone,
       }
 
     }
-    else
+    else if (is_gnunet_tld (name) == GNUNET_YES)
     {
       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                   "TLD is gnunet\n");
@@ -3634,6 +3676,19 @@ gns_resolver_lookup_record (struct GNUNET_CRYPTO_ShortHashCode zone,
       memcpy (rh->name, name,
               strlen(name)-strlen(GNUNET_GNS_TLD) - 1);
     }
+    else
+    {
+      GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+                  "Cannot handle this TLD %s\n", string_hash);
+      
+      if (GNUNET_SCHEDULER_NO_TASK != rh->timeout_task)
+        GNUNET_SCHEDULER_cancel (rh->timeout_task);
+      GNUNET_CONTAINER_DLL_remove (rlh_head, rlh_tail, rh);
+      GNUNET_free (rh);
+      GNUNET_free (rlh);
+      proc (cls, 0, NULL);
+      return;
+    }
   }
   
   /**
@@ -4247,7 +4302,7 @@ gns_resolver_shorten_name (struct GNUNET_CRYPTO_ShortHashCode *zone,
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
               "Starting shorten for %s!\n", name);
   
-  if (is_canonical ((char*)name))
+  if (is_canonical ((char*)name) == GNUNET_YES)
   {
     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                 "%s is canonical. Returning verbatim\n", name);
@@ -4321,7 +4376,7 @@ gns_resolver_shorten_name (struct GNUNET_CRYPTO_ShortHashCode *zone,
     return;
 
   }
-  else
+  else if (is_gnunet_tld (name) == GNUNET_YES)
   {
     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                 "TLD is gnunet\n");
@@ -4333,6 +4388,15 @@ gns_resolver_shorten_name (struct GNUNET_CRYPTO_ShortHashCode *zone,
     memcpy (rh->name, name,
             strlen (name)-strlen (GNUNET_GNS_TLD) - 1);
   }
+  else
+  {
+    GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Unknown TLD in %s\n", name);
+    GNUNET_free (rh);
+    GNUNET_free (nsh);
+    GNUNET_CONTAINER_DLL_remove (nsh_head, nsh_tail, rh);
+    proc (proc_cls, name);
+    return;
+  }
 
   rh->authority_chain_head = GNUNET_malloc (sizeof (struct AuthorityChain));
   rh->authority_chain_tail = rh->authority_chain_head;
@@ -4345,6 +4409,12 @@ gns_resolver_shorten_name (struct GNUNET_CRYPTO_ShortHashCode *zone,
 
 /*********** END NAME SHORTEN ********************/
 
+/**
+ * Conclude get authority lookup
+ *
+ * @param rh resolver handle
+ * @param nah get authority lookup handle
+ */
 static void
 finish_get_auth (struct ResolverHandle *rh,
                  struct GetNameAuthorityHandle *nah)
@@ -4390,7 +4460,7 @@ handle_delegation_result_ns_get_auth(void* cls,
 
   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
              "Building response!\n");
-  if (is_canonical(rh->name))
+  if (is_canonical (rh->name) == GNUNET_YES)
   {
     /**
      * We successfully resolved the authority in the ns