/*
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
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.
*/
/**
GNUNET_break_op (0);
return GNUNET_SYSERR;
}
- memcpy (&ql, &udp_payload[*off], sizeof (ql));
+ GNUNET_memcpy (&ql, &udp_payload[*off], sizeof (ql));
*off += sizeof (ql);
q->type = ntohs (ql.type);
q->dns_traffic_class = ntohs (ql.dns_traffic_class);
*off = old_off;
return NULL;
}
- memcpy (&soa_bin,
+ GNUNET_memcpy (&soa_bin,
&udp_payload[*off],
sizeof (struct GNUNET_TUN_DnsSoaRecord));
soa->serial = ntohl (soa_bin.serial);
GNUNET_break_op (0);
return NULL;
}
- memcpy (&mxpref, &udp_payload[*off], sizeof (uint16_t));
+ GNUNET_memcpy (&mxpref, &udp_payload[*off], sizeof (uint16_t));
(*off) += sizeof (uint16_t);
mx = GNUNET_new (struct GNUNET_DNSPARSER_MxRecord);
mx->preference = ntohs (mxpref);
old_off = *off;
if (*off + sizeof (struct GNUNET_TUN_DnsSrvRecord) > udp_payload_length)
return NULL;
- memcpy (&srv_bin,
+ GNUNET_memcpy (&srv_bin,
&udp_payload[*off],
sizeof (struct GNUNET_TUN_DnsSrvRecord));
(*off) += sizeof (struct GNUNET_TUN_DnsSrvRecord);
GNUNET_break_op (0);
return NULL;
}
- memcpy (&dcert, &udp_payload[*off], sizeof (struct GNUNET_TUN_DnsCertRecord));
- (*off) += sizeof (sizeof (struct GNUNET_TUN_DnsCertRecord));
+ GNUNET_memcpy (&dcert, &udp_payload[*off], 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);
cert->algorithm = dcert.algorithm;
cert->certificate_size = udp_payload_length - (*off);
cert->certificate_data = GNUNET_malloc (cert->certificate_size);
- memcpy (cert->certificate_data,
+ GNUNET_memcpy (cert->certificate_data,
&udp_payload[*off],
cert->certificate_size);
(*off) += cert->certificate_size;
GNUNET_break_op (0);
return GNUNET_SYSERR;
}
- memcpy (&rl, &udp_payload[*off], sizeof (rl));
+ GNUNET_memcpy (&rl, &udp_payload[*off], sizeof (rl));
(*off) += sizeof (rl);
r->type = ntohs (rl.type);
r->dns_traffic_class = ntohs (rl.dns_traffic_class);
default:
r->data.raw.data = GNUNET_malloc (data_len);
r->data.raw.data_len = data_len;
- memcpy (r->data.raw.data, &udp_payload[*off], data_len);
+ GNUNET_memcpy (r->data.raw.data, &udp_payload[*off], data_len);
break;
}
(*off) += data_len;
goto fail; /* segment too long or empty */
}
dst[pos++] = (char) (uint8_t) len;
- memcpy (&dst[pos], idna_name, len);
+ GNUNET_memcpy (&dst[pos], idna_name, len);
pos += len;
idna_name += len + 1; /* also skip dot */
}
return ret;
ql.type = htons (query->type);
ql.dns_traffic_class = htons (query->dns_traffic_class);
- memcpy (&dst[*off], &ql, sizeof (ql));
+ GNUNET_memcpy (&dst[*off], &ql, sizeof (ql));
(*off) += sizeof (ql);
return GNUNET_OK;
}
if (*off + sizeof (uint16_t) > dst_len)
return GNUNET_NO;
mxpref = htons (mx->preference);
- memcpy (&dst[*off], &mxpref, sizeof (mxpref));
+ GNUNET_memcpy (&dst[*off], &mxpref, sizeof (mxpref));
(*off) += sizeof (mxpref);
return GNUNET_DNSPARSER_builder_add_name (dst, dst_len, off, mx->mxhost);
}
dcert.cert_type = htons ((uint16_t) cert->cert_type);
dcert.cert_tag = htons ((uint16_t) cert->cert_tag);
dcert.algorithm = (uint8_t) cert->algorithm;
- memcpy (&dst[*off], &dcert, sizeof (dcert));
+ GNUNET_memcpy (&dst[*off], &dcert, sizeof (dcert));
(*off) += sizeof (dcert);
- memcpy (&dst[*off], cert->certificate_data, cert->certificate_size);
+ GNUNET_memcpy (&dst[*off], cert->certificate_data, cert->certificate_size);
(*off) += cert->certificate_size;
return GNUNET_OK;
}
sd.retry = htonl (soa->retry);
sd.expire = htonl (soa->expire);
sd.minimum = htonl (soa->minimum_ttl);
- memcpy (&dst[*off], &sd, sizeof (sd));
+ GNUNET_memcpy (&dst[*off], &sd, sizeof (sd));
(*off) += sizeof (sd);
return GNUNET_OK;
}
sd.prio = htons (srv->priority);
sd.weight = htons (srv->weight);
sd.port = htons (srv->port);
- memcpy (&dst[*off], &sd, sizeof (sd));
+ GNUNET_memcpy (&dst[*off], &sd, sizeof (sd));
(*off) += sizeof (sd);
if (GNUNET_OK != (ret = GNUNET_DNSPARSER_builder_add_name (dst,
dst_len,
ret = GNUNET_NO;
break;
}
- memcpy (&dst[pos], record->data.raw.data, record->data.raw.data_len);
+ GNUNET_memcpy (&dst[pos], record->data.raw.data, record->data.raw.data_len);
pos += record->data.raw.data_len;
ret = GNUNET_OK;
break;
rl.dns_traffic_class = htons (record->dns_traffic_class);
rl.ttl = htonl (GNUNET_TIME_absolute_get_remaining (record->expiration_time).rel_value_us / 1000LL / 1000LL); /* in seconds */
rl.data_len = htons ((uint16_t) (pos - (*off + sizeof (struct GNUNET_TUN_DnsRecordLine))));
- memcpy (&dst[*off], &rl, sizeof (struct GNUNET_TUN_DnsRecordLine));
+ GNUNET_memcpy (&dst[*off], &rl, sizeof (struct GNUNET_TUN_DnsRecordLine));
*off = pos;
return GNUNET_OK;
}
trc = GNUNET_NO;
for (i=0;i<p->num_queries;i++)
{
- ret = GNUNET_DNSPARSER_builder_add_query (tmp, sizeof (tmp), &off, &p->queries[i]);
+ ret = GNUNET_DNSPARSER_builder_add_query (tmp,
+ sizeof (tmp),
+ &off,
+ &p->queries[i]);
if (GNUNET_SYSERR == ret)
return GNUNET_SYSERR;
if (GNUNET_NO == ret)
}
for (i=0;i<p->num_answers;i++)
{
- ret = add_record (tmp, sizeof (tmp), &off, &p->answers[i]);
+ ret = add_record (tmp,
+ sizeof (tmp),
+ &off,
+ &p->answers[i]);
if (GNUNET_SYSERR == ret)
return GNUNET_SYSERR;
if (GNUNET_NO == ret)
}
for (i=0;i<p->num_authority_records;i++)
{
- ret = add_record (tmp, sizeof (tmp), &off, &p->authority_records[i]);
+ ret = add_record (tmp,
+ sizeof (tmp),
+ &off,
+ &p->authority_records[i]);
if (GNUNET_SYSERR == ret)
return GNUNET_SYSERR;
if (GNUNET_NO == ret)
}
for (i=0;i<p->num_additional_records;i++)
{
- ret = add_record (tmp, sizeof (tmp), &off, &p->additional_records[i]);
+ ret = add_record (tmp,
+ sizeof (tmp),
+ &off,
+ &p->additional_records[i]);
if (GNUNET_SYSERR == ret)
return GNUNET_SYSERR;
if (GNUNET_NO == ret)
if (GNUNET_YES == trc)
dns.flags.message_truncated = 1;
- memcpy (tmp, &dns, sizeof (struct GNUNET_TUN_DnsHeader));
+ GNUNET_memcpy (tmp,
+ &dns,
+ sizeof (struct GNUNET_TUN_DnsHeader));
*buf = GNUNET_malloc (off);
*buf_length = off;
- memcpy (*buf, tmp, off);
+ GNUNET_memcpy (*buf,
+ tmp,
+ off);
if (GNUNET_YES == trc)
return GNUNET_NO;
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 ((unsigned char) hex[off * 2]);
+ in[1] = tolower ((unsigned char) hex[off * 2 + 1]);
+ if (1 != sscanf (in, "%x", &h))
+ return off;
+ idata[off] = (uint8_t) h;
+ }
+ return off;
+}
+
+
/* end of dnsparser.c */