#include <math.h>
+#include "dns-domain.h"
+#include "dns-type.h"
+#include "resolved-dns-packet.h"
+#include "string-util.h"
#include "strv.h"
-
-#include "resolved-dns-domain.h"
#include "resolved-dns-rr.h"
DnsResourceKey* dns_resource_key_new(uint16_t class, uint16_t type, const char *name) {
return k;
}
+DnsResourceKey* dns_resource_key_new_cname(const DnsResourceKey *key) {
+ assert(key);
+
+ return dns_resource_key_new(key->class, DNS_TYPE_CNAME, DNS_RESOURCE_KEY_NAME(key));
+}
+
+DnsResourceKey* dns_resource_key_new_redirect(const DnsResourceKey *key, const DnsResourceRecord *cname) {
+ assert(key);
+ assert(cname);
+
+ return dns_resource_key_new(key->class, key->type, cname->cname.name);
+}
+
DnsResourceKey* dns_resource_key_new_consume(uint16_t class, uint16_t type, char *name) {
DnsResourceKey *k;
return dns_name_equal(DNS_RESOURCE_KEY_NAME(rr->key), DNS_RESOURCE_KEY_NAME(key));
}
-unsigned long dns_resource_key_hash_func(const void *i, const uint8_t hash_key[HASH_KEY_SIZE]) {
+static void dns_resource_key_hash_func(const void *i, struct siphash *state) {
const DnsResourceKey *k = i;
- unsigned long ul;
- ul = dns_name_hash_func(DNS_RESOURCE_KEY_NAME(k), hash_key);
- ul = ul * hash_key[0] + ul + k->class;
- ul = ul * hash_key[1] + ul + k->type;
+ assert(k);
- return ul;
+ dns_name_hash_func(DNS_RESOURCE_KEY_NAME(k), state);
+ siphash24_compress(&k->class, sizeof(k->class), state);
+ siphash24_compress(&k->type, sizeof(k->type), state);
}
-int dns_resource_key_compare_func(const void *a, const void *b) {
+static int dns_resource_key_compare_func(const void *a, const void *b) {
const DnsResourceKey *x = a, *y = b;
int ret;
return 0;
}
+const struct hash_ops dns_resource_key_hash_ops = {
+ .hash = dns_resource_key_hash_func,
+ .compare = dns_resource_key_compare_func
+};
+
int dns_resource_key_to_string(const DnsResourceKey *key, char **ret) {
- char cbuf[DECIMAL_STR_MAX(uint16_t)], tbuf[DECIMAL_STR_MAX(uint16_t)];
+ char cbuf[strlen("CLASS") + DECIMAL_STR_MAX(uint16_t)], tbuf[strlen("TYPE") + DECIMAL_STR_MAX(uint16_t)];
const char *c, *t;
char *s;
c = dns_class_to_string(key->class);
if (!c) {
- sprintf(cbuf, "%i", key->class);
+ sprintf(cbuf, "CLASS%u", key->class);
c = cbuf;
}
t = dns_type_to_string(key->type);
if (!t){
- sprintf(tbuf, "%i", key->type);
+ sprintf(tbuf, "TYPE%u", key->type);
t = tbuf;
}
- s = strjoin(DNS_RESOURCE_KEY_NAME(key), " ", c, " ", t, NULL);
- if (!s)
+ if (asprintf(&s, "%s %s %-5s", DNS_RESOURCE_KEY_NAME(key), c, t) < 0)
return -ENOMEM;
*ret = s;
free(rr->mx.exchange);
break;
+ case DNS_TYPE_DS:
+ free(rr->ds.digest);
+ break;
+
case DNS_TYPE_SSHFP:
- free(rr->sshfp.key);
+ free(rr->sshfp.fingerprint);
+ break;
+
+ case DNS_TYPE_DNSKEY:
+ free(rr->dnskey.key);
+ break;
+
+ case DNS_TYPE_RRSIG:
+ free(rr->rrsig.signer);
+ free(rr->rrsig.signature);
+ break;
+
+ case DNS_TYPE_NSEC:
+ free(rr->nsec.next_domain_name);
+ bitmap_free(rr->nsec.types);
+ break;
+
+ case DNS_TYPE_NSEC3:
+ free(rr->nsec3.next_hashed_name);
+ free(rr->nsec3.salt);
+ bitmap_free(rr->nsec3.types);
break;
case DNS_TYPE_LOC:
return 0;
}
+int dns_resource_record_new_address(DnsResourceRecord **ret, int family, const union in_addr_union *address, const char *name) {
+ DnsResourceRecord *rr;
+
+ assert(ret);
+ assert(address);
+ assert(family);
+
+ if (family == AF_INET) {
+
+ rr = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_A, name);
+ if (!rr)
+ return -ENOMEM;
+
+ rr->a.in_addr = address->in;
+
+ } else if (family == AF_INET6) {
+
+ rr = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_AAAA, name);
+ if (!rr)
+ return -ENOMEM;
+
+ rr->aaaa.in6_addr = address->in6;
+ } else
+ return -EAFNOSUPPORT;
+
+ *ret = rr;
+
+ return 0;
+}
+
int dns_resource_record_equal(const DnsResourceRecord *a, const DnsResourceRecord *b) {
int r;
if (r <= 0)
return r;
- switch (a->key->type) {
+ if (a->unparseable != b->unparseable)
+ return 0;
+
+ switch (a->unparseable ? _DNS_TYPE_INVALID : a->key->type) {
case DNS_TYPE_SRV:
r = dns_name_equal(a->srv.name, b->srv.name);
strcaseeq(a->hinfo.os, b->hinfo.os);
case DNS_TYPE_SPF: /* exactly the same as TXT */
- case DNS_TYPE_TXT: {
- int i;
-
- for (i = 0; a->txt.strings[i] || b->txt.strings[i]; i++)
- if (!streq_ptr(a->txt.strings[i], b->txt.strings[i]))
- return false;
- return true;
- }
+ case DNS_TYPE_TXT:
+ return strv_equal(a->txt.strings, b->txt.strings);
case DNS_TYPE_A:
return memcmp(&a->a.in_addr, &b->a.in_addr, sizeof(struct in_addr)) == 0;
a->loc.longitude == b->loc.longitude &&
a->loc.altitude == b->loc.altitude;
+ case DNS_TYPE_DS:
+ return a->ds.key_tag == b->ds.key_tag &&
+ a->ds.algorithm == b->ds.algorithm &&
+ a->ds.digest_type == b->ds.digest_type &&
+ a->ds.digest_size == b->ds.digest_size &&
+ memcmp(a->ds.digest, b->ds.digest, a->ds.digest_size) == 0;
+
case DNS_TYPE_SSHFP:
return a->sshfp.algorithm == b->sshfp.algorithm &&
a->sshfp.fptype == b->sshfp.fptype &&
- a->sshfp.key_size == b->sshfp.key_size &&
- memcmp(a->sshfp.key, b->sshfp.key, a->sshfp.key_size) == 0;
+ a->sshfp.fingerprint_size == b->sshfp.fingerprint_size &&
+ memcmp(a->sshfp.fingerprint, b->sshfp.fingerprint, a->sshfp.fingerprint_size) == 0;
+
+ case DNS_TYPE_DNSKEY:
+ return a->dnskey.zone_key_flag == b->dnskey.zone_key_flag &&
+ a->dnskey.sep_flag == b->dnskey.sep_flag &&
+ a->dnskey.algorithm == b->dnskey.algorithm &&
+ a->dnskey.key_size == b->dnskey.key_size &&
+ memcmp(a->dnskey.key, b->dnskey.key, a->dnskey.key_size) == 0;
+
+ case DNS_TYPE_RRSIG:
+ /* do the fast comparisons first */
+ if (a->rrsig.type_covered != b->rrsig.type_covered ||
+ a->rrsig.algorithm != b->rrsig.algorithm ||
+ a->rrsig.labels != b->rrsig.labels ||
+ a->rrsig.original_ttl != b->rrsig.original_ttl ||
+ a->rrsig.expiration != b->rrsig.expiration ||
+ a->rrsig.inception != b->rrsig.inception ||
+ a->rrsig.key_tag != b->rrsig.key_tag ||
+ a->rrsig.signature_size != b->rrsig.signature_size ||
+ memcmp(a->rrsig.signature, b->rrsig.signature, a->rrsig.signature_size) != 0)
+ return false;
+
+ return dns_name_equal(a->rrsig.signer, b->rrsig.signer);
+
+ case DNS_TYPE_NSEC:
+ return dns_name_equal(a->nsec.next_domain_name, b->nsec.next_domain_name) &&
+ bitmap_equal(a->nsec.types, b->nsec.types);
+
+ case DNS_TYPE_NSEC3:
+ return a->nsec3.algorithm == b->nsec3.algorithm &&
+ a->nsec3.flags == b->nsec3.flags &&
+ a->nsec3.iterations == b->nsec3.iterations &&
+ a->nsec3.salt_size == b->nsec3.salt_size &&
+ memcmp(a->nsec3.salt, b->nsec3.salt, a->nsec3.salt_size) == 0 &&
+ memcmp(a->nsec3.next_hashed_name, b->nsec3.next_hashed_name, a->nsec3.next_hashed_name_size) == 0 &&
+ bitmap_equal(a->nsec3.types, b->nsec3.types);
default:
return a->generic.size == b->generic.size &&
return s;
}
+static int format_timestamp_dns(char *buf, size_t l, time_t sec) {
+ struct tm tm;
+
+ assert(buf);
+ assert(l > strlen("YYYYMMDDHHmmSS"));
+
+ if (!gmtime_r(&sec, &tm))
+ return -EINVAL;
+
+ if (strftime(buf, l, "%Y%m%d%H%M%S", &tm) <= 0)
+ return -EINVAL;
+
+ return 0;
+}
+
+static char *format_types(Bitmap *types) {
+ _cleanup_strv_free_ char **strv = NULL;
+ _cleanup_free_ char *str = NULL;
+ Iterator i;
+ unsigned type;
+ int r;
+
+ BITMAP_FOREACH(type, types, i) {
+ if (dns_type_to_string(type)) {
+ r = strv_extend(&strv, dns_type_to_string(type));
+ if (r < 0)
+ return NULL;
+ } else {
+ char *t;
+
+ r = asprintf(&t, "TYPE%u", type);
+ if (r < 0)
+ return NULL;
+
+ r = strv_consume(&strv, t);
+ if (r < 0)
+ return NULL;
+ }
+ }
+
+ str = strv_join(strv, " ");
+ if (!str)
+ return NULL;
+
+ return strjoin("( ", str, " )", NULL);
+}
+
int dns_resource_record_to_string(const DnsResourceRecord *rr, char **ret) {
- _cleanup_free_ char *k = NULL;
+ _cleanup_free_ char *k = NULL, *t = NULL;
char *s;
int r;
break;
case DNS_TYPE_SPF: /* exactly the same as TXT */
- case DNS_TYPE_TXT: {
- _cleanup_free_ char *t;
-
+ case DNS_TYPE_TXT:
t = strv_join_quoted(rr->txt.strings);
if (!t)
return -ENOMEM;
return -ENOMEM;
break;
- }
case DNS_TYPE_A: {
_cleanup_free_ char *x = NULL;
break;
}
- case DNS_TYPE_AAAA: {
- _cleanup_free_ char *x = NULL;
-
- r = in_addr_to_string(AF_INET6, (const union in_addr_union*) &rr->aaaa.in6_addr, &x);
+ case DNS_TYPE_AAAA:
+ r = in_addr_to_string(AF_INET6, (const union in_addr_union*) &rr->aaaa.in6_addr, &t);
if (r < 0)
return r;
- s = strjoin(k, " ", x, NULL);
+ s = strjoin(k, " ", t, NULL);
if (!s)
return -ENOMEM;
break;
- }
case DNS_TYPE_SOA:
r = asprintf(&s, "%s %s %s %u %u %u %u %u",
return -ENOMEM;
break;
- case DNS_TYPE_LOC: {
- _cleanup_free_ char *loc;
+ case DNS_TYPE_LOC:
assert(rr->loc.version == 0);
- loc = format_location(rr->loc.latitude,
- rr->loc.longitude,
- rr->loc.altitude,
- rr->loc.size,
- rr->loc.horiz_pre,
- rr->loc.vert_pre);
- if (!loc)
+ t = format_location(rr->loc.latitude,
+ rr->loc.longitude,
+ rr->loc.altitude,
+ rr->loc.size,
+ rr->loc.horiz_pre,
+ rr->loc.vert_pre);
+ if (!t)
return -ENOMEM;
- s = strjoin(k, " ", loc, NULL);
+ s = strjoin(k, " ", t, NULL);
if (!s)
return -ENOMEM;
-
break;
- }
- case DNS_TYPE_SSHFP: {
- _cleanup_free_ char *x = NULL;
+ case DNS_TYPE_DS:
+ t = hexmem(rr->ds.digest, rr->ds.digest_size);
+ if (!t)
+ return -ENOMEM;
- x = hexmem(rr->sshfp.key, rr->sshfp.key_size);
- if (!x)
+ r = asprintf(&s, "%s %u %u %u %s",
+ k,
+ rr->ds.key_tag,
+ rr->ds.algorithm,
+ rr->ds.digest_type,
+ t);
+ if (r < 0)
+ return -ENOMEM;
+ break;
+
+ case DNS_TYPE_SSHFP:
+ t = hexmem(rr->sshfp.fingerprint, rr->sshfp.fingerprint_size);
+ if (!t)
return -ENOMEM;
r = asprintf(&s, "%s %u %u %s",
k,
rr->sshfp.algorithm,
rr->sshfp.fptype,
- x);
+ t);
+ if (r < 0)
+ return -ENOMEM;
+ break;
+
+ case DNS_TYPE_DNSKEY: {
+ const char *alg;
+
+ alg = dnssec_algorithm_to_string(rr->dnskey.algorithm);
+
+ t = base64mem(rr->dnskey.key, rr->dnskey.key_size);
+ if (!t)
+ return -ENOMEM;
+
+ r = asprintf(&s, "%s %u 3 %.*s%.*u %s",
+ k,
+ dnskey_to_flags(rr),
+ alg ? -1 : 0, alg,
+ alg ? 0 : 1, alg ? 0u : (unsigned) rr->dnskey.algorithm,
+ t);
if (r < 0)
return -ENOMEM;
break;
}
- default: {
- _cleanup_free_ char *x = NULL;
+ case DNS_TYPE_RRSIG: {
+ const char *type, *alg;
+ char expiration[strlen("YYYYMMDDHHmmSS") + 1], inception[strlen("YYYYMMDDHHmmSS") + 1];
+
+ type = dns_type_to_string(rr->rrsig.type_covered);
+ alg = dnssec_algorithm_to_string(rr->rrsig.algorithm);
+
+ t = base64mem(rr->rrsig.signature, rr->rrsig.signature_size);
+ if (!t)
+ return -ENOMEM;
+
+ r = format_timestamp_dns(expiration, sizeof(expiration), rr->rrsig.expiration);
+ if (r < 0)
+ return r;
+
+ r = format_timestamp_dns(inception, sizeof(inception), rr->rrsig.inception);
+ if (r < 0)
+ return r;
+
+ /* TYPE?? follows
+ * http://tools.ietf.org/html/rfc3597#section-5 */
+
+ r = asprintf(&s, "%s %s%.*u %.*s%.*u %u %u %s %s %u %s %s",
+ k,
+ type ?: "TYPE",
+ type ? 0 : 1, type ? 0u : (unsigned) rr->rrsig.type_covered,
+ alg ? -1 : 0, alg,
+ alg ? 0 : 1, alg ? 0u : (unsigned) rr->rrsig.algorithm,
+ rr->rrsig.labels,
+ rr->rrsig.original_ttl,
+ expiration,
+ inception,
+ rr->rrsig.key_tag,
+ rr->rrsig.signer,
+ t);
+ if (r < 0)
+ return -ENOMEM;
+ break;
+ }
- x = hexmem(rr->generic.data, rr->generic.size);
- if (!x)
+ case DNS_TYPE_NSEC:
+ t = format_types(rr->nsec.types);
+ if (!t)
return -ENOMEM;
- s = strjoin(k, " ", x, NULL);
- if (!s)
+ r = asprintf(&s, "%s %s %s",
+ k,
+ rr->nsec.next_domain_name,
+ t);
+ if (r < 0)
return -ENOMEM;
break;
- }}
+
+ case DNS_TYPE_NSEC3: {
+ _cleanup_free_ char *salt = NULL, *hash = NULL;
+
+ if (rr->nsec3.salt_size > 0) {
+ salt = hexmem(rr->nsec3.salt, rr->nsec3.salt_size);
+ if (!salt)
+ return -ENOMEM;
+ }
+
+ hash = base32hexmem(rr->nsec3.next_hashed_name, rr->nsec3.next_hashed_name_size, false);
+ if (!hash)
+ return -ENOMEM;
+
+ t = format_types(rr->nsec3.types);
+ if (!t)
+ return -ENOMEM;
+
+ r = asprintf(&s, "%s %"PRIu8" %"PRIu8" %"PRIu16" %s %s %s",
+ k,
+ rr->nsec3.algorithm,
+ rr->nsec3.flags,
+ rr->nsec3.iterations,
+ rr->nsec3.salt_size > 0 ? salt : "-",
+ hash,
+ t);
+ if (r < 0)
+ return -ENOMEM;
+
+ break;
+ }
+
+ default:
+ t = hexmem(rr->generic.data, rr->generic.size);
+ if (!t)
+ return -ENOMEM;
+
+ r = asprintf(&s, "%s \\# %zu %s", k, rr->generic.size, t);
+ if (r < 0)
+ return -ENOMEM;
+ break;
+ }
*ret = s;
return 0;
if (strcaseeq(s, "IN"))
*class = DNS_CLASS_IN;
else if (strcaseeq(s, "ANY"))
- *class = DNS_TYPE_ANY;
+ *class = DNS_CLASS_ANY;
else
return -EINVAL;
return 0;
}
-
-static const struct {
- uint16_t type;
- const char *name;
-} dns_types[] = {
- { DNS_TYPE_A, "A" },
- { DNS_TYPE_NS, "NS" },
- { DNS_TYPE_CNAME, "CNAME" },
- { DNS_TYPE_SOA, "SOA" },
- { DNS_TYPE_PTR, "PTR" },
- { DNS_TYPE_HINFO, "HINFO" },
- { DNS_TYPE_MX, "MX" },
- { DNS_TYPE_TXT, "TXT" },
- { DNS_TYPE_AAAA, "AAAA" },
- { DNS_TYPE_LOC, "LOC" },
- { DNS_TYPE_SRV, "SRV" },
- { DNS_TYPE_SSHFP, "SSHFP" },
- { DNS_TYPE_SPF, "SPF" },
- { DNS_TYPE_DNAME, "DNAME" },
- { DNS_TYPE_ANY, "ANY" },
- { DNS_TYPE_OPT, "OPT" },
- { DNS_TYPE_TKEY, "TKEY" },
- { DNS_TYPE_TSIG, "TSIG" },
- { DNS_TYPE_IXFR, "IXFR" },
- { DNS_TYPE_AXFR, "AXFR" },
-};
-
-const char *dns_type_to_string(uint16_t type) {
- unsigned i;
-
- for (i = 0; i < ELEMENTSOF(dns_types); i++)
- if (dns_types[i].type == type)
- return dns_types[i].name;
-
- return NULL;
-}
-
-int dns_type_from_string(const char *s, uint16_t *type) {
- unsigned i;
-
- assert(s);
- assert(type);
-
- for (i = 0; i < ELEMENTSOF(dns_types); i++)
- if (strcaseeq(dns_types[i].name, s)) {
- *type = dns_types[i].type;
- return 0;
- }
-
- return -EINVAL;
-}