-fixes
[oweals/gnunet.git] / src / gns / gnunet-service-gns_interceptor.c
index 1a14eb96e960f58e6af3ba8b8481bd5200ad2feb..b3ecdfdf9ef3e489c68e3b3ed037f9b551753482 100644 (file)
@@ -20,7 +20,7 @@
 
 /**
  *
- * @file gns/gns_interceptor.c
+ * @file gns/gnunet-service-gns_interceptor.c
  * @brief GNUnet GNS interceptor logic
  * @author Martin Schanzenbach
  */
@@ -54,18 +54,27 @@ struct InterceptLookupHandle
 /**
  * Our handle to the DNS handler library
  */
-struct GNUNET_DNS_Handle *dns_handle;
+static struct GNUNET_DNS_Handle *dns_handle;
 
 /**
  * The root zone for this interceptor
  */
-static GNUNET_HashCode our_zone;
+static struct GNUNET_CRYPTO_ShortHashCode our_zone;
+
+/**
+ * Our priv key
+ */
+static struct GNUNET_CRYPTO_RsaPrivateKey *our_key;
+
+/**
+ * Default timeout
+ */
+static struct GNUNET_TIME_Relative default_lookup_timeout;
 
 /**
  * Reply to dns request with the result from our lookup.
  *
  * @param cls the closure to the request (an InterceptLookupHandle)
- * @param rh the request handle of the lookup
  * @param rd_count the number of records to return
  * @param rd the record data
  */
@@ -107,11 +116,27 @@ reply_to_dns(void* cls, uint32_t rd_count,
     
     if (rd[i].record_type == ilh->query->type)
     {
-      num_answers++;
       answer_records[i].name = ilh->query->name;
       answer_records[i].type = rd[i].record_type;
-      answer_records[i].data.raw.data_len = rd[i].data_size;
-      answer_records[i].data.raw.data = (char*)rd[i].data;
+      switch(rd[i].record_type)
+      {
+       case GNUNET_GNS_RECORD_TYPE_NS:
+       case GNUNET_GNS_RECORD_TYPE_CNAME:
+       case GNUNET_GNS_RECORD_TYPE_PTR:
+         answer_records[i].data.hostname = (char*)rd[i].data;
+         break;
+       case GNUNET_GNS_RECORD_TYPE_SOA:
+         answer_records[i].data.soa =
+           (struct GNUNET_DNSPARSER_SoaRecord *)rd[i].data;
+         break;
+       case GNUNET_GNS_RECORD_MX:
+         answer_records[i].data.mx =
+           (struct GNUNET_DNSPARSER_MxRecord *)rd[i].data;
+         break;
+       default:
+        answer_records[i].data.raw.data_len = rd[i].data_size;
+        answer_records[i].data.raw.data = (char*)rd[i].data;
+      }
       answer_records[i].expiration_time = rd[i].expiration;
       answer_records[i].class = GNUNET_DNSPARSER_CLASS_INTERNET;//hmmn
     }
@@ -119,8 +144,25 @@ reply_to_dns(void* cls, uint32_t rd_count,
     {
       additional_records[i].name = ilh->query->name;
       additional_records[i].type = rd[i].record_type;
-      additional_records[i].data.raw.data_len = rd[i].data_size;
-      additional_records[i].data.raw.data = (char*)rd[i].data;
+      switch(rd[i].record_type)
+      {
+       case GNUNET_GNS_RECORD_TYPE_NS:
+       case GNUNET_GNS_RECORD_TYPE_CNAME:
+       case GNUNET_GNS_RECORD_TYPE_PTR:
+         additional_records[i].data.hostname = (char*)rd[i].data;
+         break;
+       case GNUNET_GNS_RECORD_TYPE_SOA:
+         additional_records[i].data.soa =
+           (struct GNUNET_DNSPARSER_SoaRecord *)rd[i].data;
+         break;
+       case GNUNET_GNS_RECORD_MX:
+         additional_records[i].data.mx =
+           (struct GNUNET_DNSPARSER_MxRecord *)rd[i].data;
+         break;
+       default:
+        additional_records[i].data.raw.data_len = rd[i].data_size;
+        additional_records[i].data.raw.data = (char*)rd[i].data;
+      }
       additional_records[i].expiration_time = rd[i].expiration;
       additional_records[i].class = GNUNET_DNSPARSER_CLASS_INTERNET;//hmmn
     }
@@ -172,9 +214,6 @@ reply_to_dns(void* cls, uint32_t rd_count,
   packet->num_additional_records = 0;
   packet->additional_records = NULL;
   GNUNET_DNSPARSER_free_packet(packet);
-  //FIXME free resolver handle in resp functions in resolver!
-  //GNUNET_free((struct RecordLookupHandle*)rh->proc_cls);
-  //free_resolver_handle(rh);
   GNUNET_free(ilh);
 }
 
@@ -204,7 +243,10 @@ start_resolution_for_dns(struct GNUNET_DNS_RequestHandle *request,
   ilh->request_handle = request;
   
   /* Start resolution in our zone */
-  gns_resolver_lookup_record(our_zone, q->type, q->name, &reply_to_dns, ilh);
+  gns_resolver_lookup_record(our_zone, q->type, q->name,
+                             our_key,
+                             default_lookup_timeout,
+                             &reply_to_dns, ilh);
 }
 
 
@@ -225,8 +267,6 @@ handle_dns_request(void *cls,
                    const char *request)
 {
   struct GNUNET_DNSPARSER_Packet *p;
-  int i;
-  char *tldoffset;
 
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Hijacked a DNS request...processing\n");
   p = GNUNET_DNSPARSER_parse (request, request_length);
@@ -270,18 +310,12 @@ handle_dns_request(void *cls,
 
   
   /**
-   * Check for .gnunet
+   * Check for .gnunet/.zkey
    */
-  tldoffset = p->queries[0].name + strlen(p->queries[0].name) - 1;
-  
-  for (i=0; i<strlen(p->queries[0].name); i++)
-  {
-    if (*(tldoffset-i) == '.')
-      break;
-  }
   
-  if ((i==strlen(GNUNET_GNS_TLD)-1)
-      && (0 == strcmp(tldoffset-i, GNUNET_GNS_TLD)))
+  if ((is_gnunet_tld(p->queries[0].name) == GNUNET_YES) ||
+      (is_zkey_tld(p->queries[0].name) == GNUNET_YES) ||
+      (strcmp(p->queries[0].name, GNUNET_GNS_TLD) == 0))
   {
     start_resolution_for_dns(rh, p, p->queries);
   }
@@ -299,14 +333,26 @@ handle_dns_request(void *cls,
 }
 
 
+/**
+ * Initialized the interceptor
+ *
+ * @param zone the zone to work in
+ * @param key the prov key of the zone (can be null, needed for caching)
+ * @param c the configuration
+ * @return GNUNET_OK on success
+ */
 int
-gns_interceptor_init(GNUNET_HashCode zone,
+gns_interceptor_init(struct GNUNET_CRYPTO_ShortHashCode zone,
+                     struct GNUNET_CRYPTO_RsaPrivateKey *key,
                      const struct GNUNET_CONFIGURATION_Handle *c)
 {
+  unsigned long long default_lookup_timeout_secs = 0;
+
   GNUNET_log(GNUNET_ERROR_TYPE_INFO,
              "DNS hijacking enabled... connecting to service.\n");
 
   our_zone = zone;
+  our_key = key;
   /**
    * Do gnunet dns init here
    */
@@ -314,6 +360,17 @@ gns_interceptor_init(GNUNET_HashCode zone,
                                   GNUNET_DNS_FLAG_PRE_RESOLUTION,
                                   &handle_dns_request, /* rh */
                                   NULL); /* Closure */
+
+  if (GNUNET_OK ==
+      GNUNET_CONFIGURATION_get_value_number(c, "gns",
+                                            "DEFAULT_LOOKUP_TIMEOUT",
+                                            &default_lookup_timeout_secs))
+  {
+    default_lookup_timeout = GNUNET_TIME_relative_multiply(
+                                                  GNUNET_TIME_UNIT_SECONDS,
+                                                  default_lookup_timeout_secs);
+  }
+
   if (NULL == dns_handle)
   {
     GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
@@ -324,4 +381,14 @@ gns_interceptor_init(GNUNET_HashCode zone,
   return GNUNET_YES;
 }
 
+/**
+ * Disconnect from interceptor
+ */
+void
+gns_interceptor_stop(void)
+{
+  if (dns_handle)
+    GNUNET_DNS_disconnect(dns_handle);
+}
+
 /* end of gns_interceptor.c */