docu
[oweals/gnunet.git] / src / dns / dnsparser.c
index c6a60cd44bc31c645c6cd96ea058c2dab7af9099..6ddfebb11a344a1189b19117e75443b5275041b1 100644 (file)
  * @author Christian Grothoff
  */
 #include "platform.h"
+#include <idna.h>
+#if WINDOWS
+#include <idn-free.h>
+#endif
 #include "gnunet_util_lib.h"
 #include "gnunet_dnsparser_lib.h"
-
-
-GNUNET_NETWORK_STRUCT_BEGIN
-
-/* FIXME: replace this one with the one from tcpip_tun.h!? */
-/**
- * Head of a any DNS message.
- */
-struct GNUNET_TUN_DnsHeader
-{
-  /**
-   * Request/response ID. (NBO)
-   */
-  uint16_t id GNUNET_PACKED;
-
-  /**
-   * Flags for the operation.
-   */
-  struct GNUNET_DNSPARSER_Flags flags; 
-
-  /**
-   * number of questions (NBO)
-   */
-  uint16_t query_count GNUNET_PACKED;
-
-  /**
-   * number of answers (NBO)
-   */
-  uint16_t answer_rcount GNUNET_PACKED;
-
-  /**
-   * number of authority-records (NBO)
-   */
-  uint16_t authority_rcount GNUNET_PACKED;
-
-  /**
-   * number of additional records (NBO)
-   */
-  uint16_t additional_rcount GNUNET_PACKED;
-};
-
-
-/**
- * DNS query prefix.
- */
-struct query_line
-{
-  /**
-   * Desired type (GNUNET_DNSPARSER_TYPE_XXX). (NBO)
-   */
-  uint16_t type GNUNET_PACKED;
-
-  /**
-   * Desired class (usually GNUNET_DNSPARSER_CLASS_INTERNET). (NBO)
-   */
-  uint16_t class GNUNET_PACKED;
-};
+#include "dnsparser.h"
 
 
 /**
- * General DNS record prefix.
- */
-struct record_line
-{
-  /**
-   * Record type (GNUNET_DNSPARSER_TYPE_XXX). (NBO)
-   */
-  uint16_t type GNUNET_PACKED;
-
-  /**
-   * Record class (usually GNUNET_DNSPARSER_CLASS_INTERNET). (NBO)
-   */
-  uint16_t class GNUNET_PACKED;
-
-  /**
-   * Expiration for the record (in seconds). (NBO)
-   */
-  uint32_t ttl GNUNET_PACKED;
-
-  /**
-   * Number of bytes of data that follow. (NBO)
-   */
-  uint16_t data_len GNUNET_PACKED;
-};
-
-
-/**
- * Payload of DNS SOA record (header).
+ * Check if a label in UTF-8 format can be coded into valid IDNA.
+ * This can fail if the ASCII-conversion becomes longer than 63 characters.
+ *
+ * @param label label to check (UTF-8 string)
+ * @return GNUNET_OK if the label can be converted to IDNA,
+ *         GNUNET_SYSERR if the label is not valid for DNS names
  */
-struct soa_data
+int
+GNUNET_DNSPARSER_check_label (const char *label)
 {
-  /**
-   * The version number of the original copy of the zone.   (NBO)
-   */
-  uint32_t serial GNUNET_PACKED;
-  
-  /**
-   * Time interval before the zone should be refreshed. (NBO)
-   */
-  uint32_t refresh GNUNET_PACKED;
+  char *output;
+  size_t slen;
   
-  /**
-   * Time interval that should elapse before a failed refresh should
-   * be retried. (NBO)
-   */
-  uint32_t retry GNUNET_PACKED;
-  /**
-   * Time value that specifies the upper limit on the time interval
-   * that can elapse before the zone is no longer authoritative. (NBO)
-   */
-  uint32_t expire GNUNET_PACKED;
-
-  /**
-   * The bit minimum TTL field that should be exported with any RR
-   * from this zone. (NBO)
-   */
-  uint32_t minimum GNUNET_PACKED;
-};
+  if (NULL != strchr (label, '.'))
+    return GNUNET_SYSERR; /* not a label! Did you mean GNUNET_DNSPARSER_check_name? */
+  if (IDNA_SUCCESS != 
+      idna_to_ascii_8z (label, &output, IDNA_USE_STD3_ASCII_RULES))
+    return GNUNET_SYSERR;
+  slen = strlen (output);
+#if WINDOWS
+  idn_free (output);
+#else
+  free (output);
+#endif
+  return (slen > 63) ? GNUNET_SYSERR : GNUNET_OK;
+}
 
 
 /**
- * Payload of DNS SRV record (header).
+ * Check if a label in UTF-8 format can be coded into valid IDNA.
+ * This can fail if the ASCII-conversion becomes longer than 253 characters.
+ *
+ * @param name name to check (UTF-8 string)
+ * @return GNUNET_OK if the label can be converted to IDNA,
+ *         GNUNET_SYSERR if the label is not valid for DNS names
  */
-struct srv_data
+int
+GNUNET_DNSPARSER_check_name (const char *name)
 {
-
-  /**
-   * Preference for this entry (lower value is higher preference).  Clients
-   * will contact hosts from the lowest-priority group first and fall back
-   * to higher priorities if the low-priority entries are unavailable. (NBO)
-   */
-  uint16_t prio GNUNET_PACKED;
-
-  /**
-   * Relative weight for records with the same priority.  Clients will use
-   * the hosts of the same (lowest) priority with a probability proportional
-   * to the weight given. (NBO)
-   */
-  uint16_t weight GNUNET_PACKED;
-
-  /**
-   * TCP or UDP port of the service. (NBO)
-   */
-  uint16_t port GNUNET_PACKED;
-
-  /* followed by 'target' name */
-};
-
-GNUNET_NETWORK_STRUCT_END
+  char *ldup;
+  char *output;
+  size_t slen;
+  char *tok;
+  
+  ldup = GNUNET_strdup (name);
+  for (tok = strtok (ldup, "."); NULL != tok; tok = strtok (NULL, "."))
+    if (GNUNET_OK !=
+       GNUNET_DNSPARSER_check_label (tok))
+    {
+      GNUNET_free (ldup);
+      return GNUNET_SYSERR;
+    }
+  GNUNET_free (ldup);
+  if (IDNA_SUCCESS != 
+      idna_to_ascii_8z (name, &output, IDNA_USE_STD3_ASCII_RULES))
+    return GNUNET_SYSERR;
+  slen = strlen (output);
+#if WINDOWS
+  idn_free (output);
+#else
+  free (output);
+#endif
+  return (slen > 253) ? GNUNET_SYSERR : GNUNET_OK;
+}
 
 
 /**
@@ -201,6 +123,8 @@ parse_name (const char *udp_payload,
   char *xstr;
   uint8_t len;
   size_t xoff;
+  char *utf8;
+  Idna_rc rc;
   
   ret = GNUNET_strdup ("");
   while (1)
@@ -218,10 +142,36 @@ parse_name (const char *udp_payload,
       if (*off + 1 + len > udp_payload_length)
        goto error;
       GNUNET_asprintf (&tmp,
-                      "%s%.*s.",
-                      ret,
+                      "%.*s",
                       (int) len,
                       &udp_payload[*off + 1]);
+      if (IDNA_SUCCESS !=
+         (rc = idna_to_unicode_8z8z (tmp, &utf8, IDNA_USE_STD3_ASCII_RULES)))
+      {
+       GNUNET_log (GNUNET_ERROR_TYPE_INFO,
+                   _("Failed to convert DNS IDNA name `%s' to UTF-8: %s\n"),
+                   tmp,
+                   idna_strerror (rc));
+       GNUNET_free (tmp);
+       GNUNET_asprintf (&tmp,
+                        "%s%.*s.",
+                        ret,
+                        (int) len,
+                        &udp_payload[*off + 1]);
+      }
+      else
+      {
+       GNUNET_free (tmp);
+       GNUNET_asprintf (&tmp,
+                        "%s%s.",
+                        ret,
+                        utf8);
+#if WINDOWS
+       idn_free (utf8);
+#else
+       free (utf8);
+#endif
+      }
       GNUNET_free (ret);
       ret = tmp;
       *off += 1 + len;
@@ -397,9 +347,9 @@ parse_record (const char *udp_payload,
       return GNUNET_SYSERR;
     return GNUNET_OK;
   case GNUNET_DNSPARSER_TYPE_SRV:
-    if ('_' != *r->data.hostname)
+    if ('_' != *r->name)
       return GNUNET_SYSERR; /* all valid srv names must start with "_" */
-    if (NULL == strstr (r->data.hostname, "._"))
+    if (NULL == strstr (r->name, "._"))
       return GNUNET_SYSERR; /* necessary string from "._$PROTO" not present */
     old_off = *off;
     if (*off + sizeof (struct srv_data) > udp_payload_length)
@@ -412,12 +362,13 @@ parse_record (const char *udp_payload,
     r->data.srv->port = ntohs (srv.port);
     /* parse 'data.hostname' into components, which are
        "_$SERVICE._$PROTO.$DOMAIN_NAME" */
-    ndup = GNUNET_strdup (r->data.hostname);
+    ndup = GNUNET_strdup (r->name);
     tok = strtok (ndup, ".");
+    GNUNET_assert (NULL != tok);
     GNUNET_assert ('_' == *tok);
     r->data.srv->service = GNUNET_strdup (&tok[1]);
     tok = strtok (NULL, ".");
-    if ('_' != *tok)
+    if ( (NULL == tok) || ('_' != *tok) )
     {
       GNUNET_free (r->data.srv);
       GNUNET_free (ndup);
@@ -658,34 +609,60 @@ add_name (char *dst,
          const char *name)
 {
   const char *dot;
+  const char *idna_name;
+  char *idna_start;
   size_t start;
   size_t pos;
   size_t len;
+  Idna_rc rc;
 
   if (NULL == name)
     return GNUNET_SYSERR;
-  start = *off;
-  if (start + strlen (name) + 2 > dst_len)
+
+  if (IDNA_SUCCESS != 
+      (rc = idna_to_ascii_8z (name, &idna_start, IDNA_USE_STD3_ASCII_RULES)))
+  {
+    GNUNET_log (GNUNET_ERROR_TYPE_INFO,
+               _("Failed to convert UTF-8 name `%s' to DNS IDNA format: %s\n"),
+               name,
+               idna_strerror (rc));
     return GNUNET_NO;
+  }
+  idna_name = idna_start;
+  start = *off;
+  if (start + strlen (idna_name) + 2 > dst_len)
+    goto fail;
   pos = start;
   do
   {
-    dot = strchr (name, '.');
+    dot = strchr (idna_name, '.');
     if (NULL == dot)
-      len = strlen (name);
+      len = strlen (idna_name);
     else
-      len = dot - name;
+      len = dot - idna_name;
     if ( (len >= 64) || (len == 0) )
-      return GNUNET_NO; /* segment too long or empty */
+      goto fail; /* segment too long or empty */  
     dst[pos++] = (char) (uint8_t) len;
-    memcpy (&dst[pos], name, len);
+    memcpy (&dst[pos], idna_name, len);
     pos += len;
-    name += len + 1; /* also skip dot */
+    idna_name += len + 1; /* also skip dot */
   }
   while (NULL != dot);
   dst[pos++] = '\0'; /* terminator */
   *off = pos;
+#if WINDOWS
+  idn_free (idna_start);
+#else
+  free (idna_start);
+#endif
   return GNUNET_OK;
+ fail:
+#if WINDOWS
+  idn_free (idna_start);
+#else
+  free (idna_start);
+#endif
+  return GNUNET_NO; 
 }