fixing compiler warnings
[oweals/gnunet.git] / src / dns / dnsparser.c
index d4306c374c62b9ad19a8ee68f3bdd95da47192e7..5041d29a8a1adc9fb77428c10fd40392b1804de0 100644 (file)
@@ -1,6 +1,6 @@
 /*
       This file is part of GNUnet
-      (C) 2010-2013 Christian Grothoff (and other contributing authors)
+      Copyright (C) 2010-2014 GNUnet e.V.
 
       GNUnet is free software; you can redistribute it and/or modify
       it under the terms of the GNU General Public License as published
@@ -14,8 +14,8 @@
 
       You should have received a copy of the GNU General Public License
       along with GNUnet; see the file COPYING.  If not, write to the
-      Free Software Foundation, Inc., 59 Temple Place - Suite 330,
-      Boston, MA 02111-1307, USA.
+      Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+      Boston, MA 02110-1301, USA.
  */
 
 /**
@@ -143,9 +143,6 @@ GNUNET_DNSPARSER_free_srv (struct GNUNET_DNSPARSER_SrvRecord *srv)
   if (NULL == srv)
     return;
   GNUNET_free_non_null (srv->target);
-  GNUNET_free_non_null (srv->domain_name);
-  GNUNET_free_non_null (srv->proto);
-  GNUNET_free_non_null (srv->service);
   GNUNET_free (srv);
 }
 
@@ -489,7 +486,6 @@ GNUNET_DNSPARSER_parse_mx (const char *udp_payload,
 /**
  * Parse a DNS SRV record.
  *
- * @param r_name name of the SRV record
  * @param udp_payload reference to UDP packet
  * @param udp_payload_length length of @a udp_payload
  * @param off pointer to the offset of the query to parse in the SRV record (to be
@@ -497,21 +493,14 @@ GNUNET_DNSPARSER_parse_mx (const char *udp_payload,
  * @return the parsed SRV record, NULL on error
  */
 struct GNUNET_DNSPARSER_SrvRecord *
-GNUNET_DNSPARSER_parse_srv (const char *r_name,
-                           const char *udp_payload,
+GNUNET_DNSPARSER_parse_srv (const char *udp_payload,
                            size_t udp_payload_length,
                            size_t *off)
 {
   struct GNUNET_DNSPARSER_SrvRecord *srv;
   struct GNUNET_TUN_DnsSrvRecord srv_bin;
   size_t old_off;
-  char *ndup;
-  char *tok;
 
-  if ('_' != *r_name)
-    return NULL; /* all valid srv names must start with "_" */
-  if (NULL == strstr (r_name, "._"))
-    return NULL; /* necessary string from "._$PROTO" not present */
   old_off = *off;
   if (*off + sizeof (struct GNUNET_TUN_DnsSrvRecord) > udp_payload_length)
     return NULL;
@@ -523,32 +512,6 @@ GNUNET_DNSPARSER_parse_srv (const char *r_name,
   srv->priority = ntohs (srv_bin.prio);
   srv->weight = ntohs (srv_bin.weight);
   srv->port = ntohs (srv_bin.port);
-  /* parse 'data.hostname' into components, which are
-     "_$SERVICE._$PROTO.$DOMAIN_NAME" */
-  ndup = GNUNET_strdup (r_name);
-  tok = strtok (ndup, ".");
-  GNUNET_assert (NULL != tok);
-  GNUNET_assert ('_' == *tok);
-  srv->service = GNUNET_strdup (&tok[1]);
-  tok = strtok (NULL, ".");
-  if ( (NULL == tok) || ('_' != *tok) )
-  {
-    GNUNET_DNSPARSER_free_srv (srv);
-    GNUNET_free (ndup);
-    *off = old_off;
-    return NULL;
-  }
-  srv->proto = GNUNET_strdup (&tok[1]);
-  tok = strtok (NULL, ".");
-  if (NULL == tok)
-  {
-    GNUNET_DNSPARSER_free_srv (srv);
-    GNUNET_free (ndup);
-    *off = old_off;
-    return NULL;
-  }
-  srv->domain_name = GNUNET_strdup (tok);
-  GNUNET_free (ndup);
   srv->target = GNUNET_DNSPARSER_parse_name (udp_payload,
                                             udp_payload_length,
                                             off);
@@ -585,7 +548,7 @@ GNUNET_DNSPARSER_parse_cert (const char *udp_payload,
     return NULL;
   }
   memcpy (&dcert, &udp_payload[*off], sizeof (struct GNUNET_TUN_DnsCertRecord));
-  (*off) += sizeof (sizeof (struct GNUNET_TUN_DnsCertRecord));
+  (*off) += sizeof (struct GNUNET_TUN_DnsCertRecord);
   cert = GNUNET_new (struct GNUNET_DNSPARSER_CertRecord);
   cert->cert_type = ntohs (dcert.cert_type);
   cert->cert_tag = ntohs (dcert.cert_tag);
@@ -683,8 +646,7 @@ GNUNET_DNSPARSER_parse_record (const char *udp_payload,
     }
     return GNUNET_OK;
   case GNUNET_DNSPARSER_TYPE_SRV:
-    r->data.srv = GNUNET_DNSPARSER_parse_srv (r->name,
-                                             udp_payload,
+    r->data.srv = GNUNET_DNSPARSER_parse_srv (udp_payload,
                                              udp_payload_length,
                                              off);
     if ( (NULL == r->data.srv) ||
@@ -1102,22 +1064,12 @@ add_record (char *dst,
   size_t start;
   size_t pos;
   struct GNUNET_TUN_DnsRecordLine rl;
-  char *name;
 
   start = *off;
-  /* for SRV records, we can create the name from the details
-     of the record if needed */
-  name = record->name;
-  if  ( (GNUNET_DNSPARSER_TYPE_SRV == record->type) &&
-       (NULL == name) )
-    GNUNET_asprintf (&name,
-                    "_%s._%s.%s",
-                    record->data.srv->service,
-                    record->data.srv->proto,
-                    record->data.srv->domain_name);
-  ret = GNUNET_DNSPARSER_builder_add_name (dst, dst_len - sizeof (struct GNUNET_TUN_DnsRecordLine), off, name);
-  if (name != record->name)
-    GNUNET_free (name);
+  ret = GNUNET_DNSPARSER_builder_add_name (dst,
+                                           dst_len - sizeof (struct GNUNET_TUN_DnsRecordLine),
+                                           off,
+                                           record->name);
   if (GNUNET_OK != ret)
     return ret;
   /* '*off' is now the position where we will need to write the record line */
@@ -1277,4 +1229,63 @@ GNUNET_DNSPARSER_pack (const struct GNUNET_DNSPARSER_Packet *p,
   return GNUNET_OK;
 }
 
+
+/**
+ * Convert a block of binary data to HEX.
+ *
+ * @param data binary data to convert
+ * @param data_size number of bytes in @a data
+ * @return HEX string (lower case)
+ */
+char *
+GNUNET_DNSPARSER_bin_to_hex (const void *data,
+                             size_t data_size)
+{
+  char *ret;
+  size_t off;
+  const uint8_t *idata;
+
+  idata = data;
+  ret = GNUNET_malloc (data_size * 2 + 1);
+  for (off = 0; off < data_size; off++)
+    sprintf (&ret[off * 2],
+             "%02x",
+             idata[off]);
+  return ret;
+}
+
+
+/**
+ * Convert a HEX string to block of binary data.
+ *
+ * @param hex HEX string to convert (may contain mixed case)
+ * @param data where to write result, must be
+ *             at least `strlen(hex)/2` bytes long
+ * @return number of bytes written to data
+ */
+size_t
+GNUNET_DNSPARSER_hex_to_bin (const char *hex,
+                             void *data)
+{
+  size_t data_size;
+  size_t off;
+  uint8_t *idata;
+  unsigned int h;
+  char in[3];
+
+  data_size = strlen (hex) / 2;
+  idata = data;
+  in[2] = '\0';
+  for (off = 0; off < data_size; off++)
+  {
+    in[0] = tolower ((int) hex[off * 2]);
+    in[1] = tolower ((int) hex[off * 2 + 1]);
+    if (1 != sscanf (in, "%x", &h))
+      return off;
+    idata[off] = (uint8_t) h;
+  }
+  return off;
+}
+
+
 /* end of dnsparser.c */