- fixes, intendation
[oweals/gnunet.git] / src / gns / gnunet-service-gns_interceptor.c
index ec6664454a1194dd6c767a9d2e5cc923916e1466..e4fbde70f717e945413a610285b033a472acb759 100644 (file)
@@ -1,6 +1,6 @@
 /*
      This file is part of GNUnet.
-     (C) 2009-2013 Christian Grothoff (and other contributing authors)
+     Copyright (C) 2009-2013 Christian Grothoff (and other contributing authors)
 
      GNUnet is free software; you can redistribute it and/or modify
      it under the terms of the GNU General Public License as published
@@ -21,6 +21,7 @@
  * @file gns/gnunet-service-gns_interceptor.c
  * @brief GNUnet GNS interceptor logic
  * @author Martin Schanzenbach
+ * @author Christian Grothoff
  */
 #include "platform.h"
 #include "gnunet_util_lib.h"
@@ -49,12 +50,12 @@ struct InterceptLookupHandle
   struct InterceptLookupHandle *prev;
 
   /**
-   * the request handle to reply to 
+   * the request handle to reply to
    */
   struct GNUNET_DNS_RequestHandle *request_handle;
-  
+
   /**
-   * the dns parser packet received 
+   * the dns parser packet received
    */
   struct GNUNET_DNSPARSER_Packet *packet;
 
@@ -74,7 +75,7 @@ static struct GNUNET_DNS_Handle *dns_handle;
 /**
  * Key of the zone we start lookups in.
  */
-static struct GNUNET_CRYPTO_EccPublicKey zone;
+static struct GNUNET_CRYPTO_EcdsaPublicKey zone;
 
 /**
  * Head of the DLL.
@@ -96,7 +97,7 @@ static struct InterceptLookupHandle *ilh_tail;
  */
 static void
 reply_to_dns (void *cls, uint32_t rd_count,
-             const struct GNUNET_NAMESTORE_RecordData *rd)
+             const struct GNUNET_GNSRECORD_Data *rd)
 {
   struct InterceptLookupHandle *ilh = cls;
   struct GNUNET_DNSPARSER_Packet *packet = ilh->packet;
@@ -106,12 +107,17 @@ reply_to_dns (void *cls, uint32_t rd_count,
   int ret;
   char *buf;
   unsigned int num_answers;
-    
+  unsigned int skip_answers;
+  unsigned int skip_additional;
+  size_t off;
+
   /* Put records in the DNS packet */
   num_answers = 0;
   for (i=0; i < rd_count; i++)
     if (rd[i].record_type == query->type)
       num_answers++;
+  skip_answers = 0;
+  skip_additional = 0;
 
   {
     struct GNUNET_DNSPARSER_Record answer_records[num_answers];
@@ -119,73 +125,133 @@ reply_to_dns (void *cls, uint32_t rd_count,
 
     packet->answers = answer_records;
     packet->additional_records = additional_records;
-
+    /* FIXME: need to handle #GNUNET_GNSRECORD_RF_SHADOW_RECORD option
+       (by ignoring records where this flag is set if there is any
+       other record of that type in the result set) */
     for (i=0; i < rd_count; i++)
     {
       if (rd[i].record_type == query->type)
       {
-       answer_records[i].name = query->name;
-       answer_records[i].type = rd[i].record_type;
+       answer_records[i - skip_answers].name = query->name;
+       answer_records[i - skip_answers].type = rd[i].record_type;
        switch(rd[i].record_type)
        {
        case GNUNET_DNSPARSER_TYPE_NS:
        case GNUNET_DNSPARSER_TYPE_CNAME:
        case GNUNET_DNSPARSER_TYPE_PTR:
-         answer_records[i].data.hostname = (char*)rd[i].data;
+         answer_records[i - skip_answers].data.hostname
+           = GNUNET_DNSPARSER_parse_name (rd[i].data,
+                                          rd[i].data_size,
+                                          &off);
+         if ( (off != rd[i].data_size) ||
+              (NULL == answer_records[i].data.hostname) )
+         {
+           GNUNET_break_op (0);
+           skip_answers++;
+         }
          break;
        case GNUNET_DNSPARSER_TYPE_SOA:
-         answer_records[i].data.soa =
-           (struct GNUNET_DNSPARSER_SoaRecord *)rd[i].data;
+         answer_records[i - skip_answers].data.soa
+           = GNUNET_DNSPARSER_parse_soa (rd[i].data,
+                                         rd[i].data_size,
+                                         &off);
+         if ( (off != rd[i].data_size) ||
+              (NULL == answer_records[i].data.soa) )
+         {
+           GNUNET_break_op (0);
+           skip_answers++;
+         }
+         break;
+       case GNUNET_DNSPARSER_TYPE_SRV:
+         /* FIXME: SRV is not yet supported */
+         skip_answers++;
          break;
        case GNUNET_DNSPARSER_TYPE_MX:
-         answer_records[i].data.mx =
-           (struct GNUNET_DNSPARSER_MxRecord *)rd[i].data;
+         answer_records[i - skip_answers].data.mx
+           = GNUNET_DNSPARSER_parse_mx (rd[i].data,
+                                        rd[i].data_size,
+                                        &off);
+         if ( (off != rd[i].data_size) ||
+              (NULL == answer_records[i].data.hostname) )
+         {
+           GNUNET_break_op (0);
+           skip_answers++;
+         }
          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 - skip_answers].data.raw.data_len = rd[i].data_size;
+         answer_records[i - skip_answers].data.raw.data = (char*)rd[i].data;
          break;
        }
-       GNUNET_break (0 == (rd[i].flags & GNUNET_NAMESTORE_RF_RELATIVE_EXPIRATION));
-       answer_records[i].expiration_time.abs_value_us = rd[i].expiration_time;
-       answer_records[i].class = GNUNET_DNSPARSER_CLASS_INTERNET;
+       GNUNET_break (0 == (rd[i - skip_answers].flags & GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION));
+       answer_records[i - skip_answers].expiration_time.abs_value_us = rd[i].expiration_time;
+       answer_records[i - skip_answers].dns_traffic_class = GNUNET_TUN_DNS_CLASS_INTERNET;
       }
       else
       {
-       additional_records[i].name = query->name;
-       additional_records[i].type = rd[i].record_type;
+       additional_records[i - skip_additional].name = query->name;
+       additional_records[i - skip_additional].type = rd[i].record_type;
        switch(rd[i].record_type)
        {
        case GNUNET_DNSPARSER_TYPE_NS:
        case GNUNET_DNSPARSER_TYPE_CNAME:
        case GNUNET_DNSPARSER_TYPE_PTR:
-         additional_records[i].data.hostname = (char*)rd[i].data;
+         additional_records[i - skip_additional].data.hostname
+           = GNUNET_DNSPARSER_parse_name (rd[i].data,
+                                          rd[i].data_size,
+                                          &off);
+         if ( (off != rd[i].data_size) ||
+              (NULL == additional_records[i].data.hostname) )
+         {
+           GNUNET_break_op (0);
+           skip_additional++;
+         }
          break;
        case GNUNET_DNSPARSER_TYPE_SOA:
-         additional_records[i].data.soa =
-           (struct GNUNET_DNSPARSER_SoaRecord *)rd[i].data;
+         additional_records[i - skip_additional].data.soa
+           = GNUNET_DNSPARSER_parse_soa (rd[i].data,
+                                         rd[i].data_size,
+                                         &off);
+         if ( (off != rd[i].data_size) ||
+              (NULL == additional_records[i].data.hostname) )
+         {
+           GNUNET_break_op (0);
+           skip_additional++;
+         }
          break;
        case GNUNET_DNSPARSER_TYPE_MX:
-         additional_records[i].data.mx =
-           (struct GNUNET_DNSPARSER_MxRecord *)rd[i].data;
+         additional_records[i - skip_additional].data.mx
+           = GNUNET_DNSPARSER_parse_mx (rd[i].data,
+                                        rd[i].data_size,
+                                        &off);
+         if ( (off != rd[i].data_size) ||
+              (NULL == additional_records[i].data.hostname) )
+         {
+           GNUNET_break_op (0);
+           skip_additional++;
+         }
+         break;
+       case GNUNET_DNSPARSER_TYPE_SRV:
+         /* FIXME: SRV is not yet supported */
+         skip_answers++;
          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 - skip_additional].data.raw.data_len = rd[i].data_size;
+         additional_records[i - skip_additional].data.raw.data = (char*)rd[i].data;
          break;
        }
-       GNUNET_break (0 == (rd[i].flags & GNUNET_NAMESTORE_RF_RELATIVE_EXPIRATION));
-       additional_records[i].expiration_time.abs_value_us = rd[i].expiration_time;
-       additional_records[i].class = GNUNET_DNSPARSER_CLASS_INTERNET; 
+       GNUNET_break (0 == (rd[i - skip_additional].flags & GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION));
+       additional_records[i - skip_additional].expiration_time.abs_value_us = rd[i].expiration_time;
+       additional_records[i - skip_additional].dns_traffic_class = GNUNET_TUN_DNS_CLASS_INTERNET;
       }
     }
-    packet->num_answers = num_answers;
-    packet->num_additional_records = rd_count - num_answers;
+    packet->num_answers = num_answers - skip_answers;
+    packet->num_additional_records = rd_count - num_answers - skip_additional;
     packet->flags.authoritative_answer = 1;
     if (NULL == rd)
-      packet->flags.return_code = GNUNET_DNSPARSER_RETURN_CODE_NAME_ERROR;
+      packet->flags.return_code = GNUNET_TUN_DNS_RETURN_CODE_NAME_ERROR;
     else
-      packet->flags.return_code = GNUNET_DNSPARSER_RETURN_CODE_NO_ERROR;
+      packet->flags.return_code = GNUNET_TUN_DNS_RETURN_CODE_NO_ERROR;
     packet->flags.query_or_response = 1;
     ret = GNUNET_DNSPARSER_pack (packet,
                                 1024, /* maximum allowed size for DNS reply */
@@ -195,14 +261,14 @@ reply_to_dns (void *cls, uint32_t rd_count,
     {
       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
                  _("Error converting GNS response to DNS response!\n"));
-    }   
+    }
     else
     {
       GNUNET_DNS_request_answer (ilh->request_handle,
                                 len,
                                 buf);
       GNUNET_free (buf);
-    } 
+    }
     packet->num_answers = 0;
     packet->answers = NULL;
     packet->num_additional_records = 0;
@@ -231,7 +297,7 @@ handle_dns_request (void *cls,
   struct GNUNET_DNSPARSER_Packet *p;
   struct InterceptLookupHandle *ilh;
 
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
              "Hijacked a DNS request. Processing.\n");
   if (NULL == (p = GNUNET_DNSPARSER_parse (request, request_length)))
   {
@@ -241,7 +307,7 @@ handle_dns_request (void *cls,
     GNUNET_DNSPARSER_free_packet (p);
     return;
   }
-  
+
   /* Check TLD and decide if we or legacy dns is responsible */
   if (1 != p->num_queries)
   {
@@ -252,8 +318,8 @@ handle_dns_request (void *cls,
     return;
   }
 
-  /* Check for GNS TLDs. */ 
-  if ( (GNUNET_YES == is_gads_tld (p->queries[0].name)) ||
+  /* Check for GNS TLDs. */
+  if ( (GNUNET_YES == is_gnu_tld (p->queries[0].name)) ||
        (GNUNET_YES == is_zkey_tld (p->queries[0].name)) ||
        (0 == strcmp (p->queries[0].name, GNUNET_GNS_TLD)) )
   {
@@ -262,8 +328,8 @@ handle_dns_request (void *cls,
     GNUNET_CONTAINER_DLL_insert (ilh_head, ilh_tail, ilh);
     ilh->packet = p;
     ilh->request_handle = rh;
-    ilh->lookup = GNS_resolver_lookup (&zone, 
-                                      p->queries[0].type, 
+    ilh->lookup = GNS_resolver_lookup (&zone,
+                                      p->queries[0].type,
                                       p->queries[0].name,
                                       NULL /* FIXME: enable shorten for DNS intercepts? */,
                                       GNUNET_NO,
@@ -272,7 +338,7 @@ handle_dns_request (void *cls,
   }
   /* This request does not concern us. Forward to real DNS. */
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-             "Request for `%s' is forwarded to DNS untouched.\n", 
+             "Request for `%s' is forwarded to DNS untouched.\n",
              p->queries[0].name);
   GNUNET_DNS_request_forward (rh);
   GNUNET_DNSPARSER_free_packet (p);
@@ -284,10 +350,10 @@ handle_dns_request (void *cls,
  *
  * @param gnu_zone the zone to work in
  * @param c the configuration
- * @return GNUNET_OK on success
+ * @return #GNUNET_OK on success
  */
 int
-GNS_interceptor_init (const struct GNUNET_CRYPTO_EccPublicKey *gnu_zone,
+GNS_interceptor_init (const struct GNUNET_CRYPTO_EcdsaPublicKey *gnu_zone,
                      const struct GNUNET_CONFIGURATION_Handle *c)
 {
   GNUNET_log (GNUNET_ERROR_TYPE_INFO,