resolved-dns-rr.c revision 549c1a2564b56f2bb38f1203d59c747ea15817f3
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering This file is part of systemd.
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering Copyright 2014 Lennart Poettering
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering systemd is free software; you can redistribute it and/or modify it
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering under the terms of the GNU Lesser General Public License as published by
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering the Free Software Foundation; either version 2.1 of the License, or
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering (at your option) any later version.
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering systemd is distributed in the hope that it will be useful, but
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering WITHOUT ANY WARRANTY; without even the implied warranty of
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering Lesser General Public License for more details.
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering You should have received a copy of the GNU Lesser General Public License
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering along with systemd; If not, see <http://www.gnu.org/licenses/>.
875c2e220e2611165e09051c4747971811f1de58Lennart PoetteringDnsResourceKey* dns_resource_key_new(uint16_t class, uint16_t type, const char *name) {
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering k = malloc0(sizeof(DnsResourceKey) + l + 1);
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering strcpy((char*) k + sizeof(DnsResourceKey), name);
875c2e220e2611165e09051c4747971811f1de58Lennart PoetteringDnsResourceKey* dns_resource_key_new_consume(uint16_t class, uint16_t type, char *name) {
875c2e220e2611165e09051c4747971811f1de58Lennart PoetteringDnsResourceKey* dns_resource_key_ref(DnsResourceKey *k) {
875c2e220e2611165e09051c4747971811f1de58Lennart PoetteringDnsResourceKey* dns_resource_key_unref(DnsResourceKey *k) {
875c2e220e2611165e09051c4747971811f1de58Lennart Poetteringint dns_resource_key_equal(const DnsResourceKey *a, const DnsResourceKey *b) {
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering r = dns_name_equal(DNS_RESOURCE_KEY_NAME(a), DNS_RESOURCE_KEY_NAME(b));
875c2e220e2611165e09051c4747971811f1de58Lennart Poetteringint dns_resource_key_match_rr(const DnsResourceKey *key, const DnsResourceRecord *rr) {
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering if (rr->key->class != key->class && key->class != DNS_CLASS_ANY)
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering if (rr->key->type != key->type && key->type != DNS_TYPE_ANY)
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering return dns_name_equal(DNS_RESOURCE_KEY_NAME(rr->key), DNS_RESOURCE_KEY_NAME(key));
78fe420ff0bb4cd94de3c4d3f15a3021cc3e2878Lennart Poetteringint dns_resource_key_match_cname(const DnsResourceKey *key, const DnsResourceRecord *rr) {
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering if (rr->key->class != key->class && key->class != DNS_CLASS_ANY)
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering return dns_name_equal(DNS_RESOURCE_KEY_NAME(rr->key), DNS_RESOURCE_KEY_NAME(key));
875c2e220e2611165e09051c4747971811f1de58Lennart Poetteringstatic unsigned long dns_resource_key_hash_func(const void *i, const uint8_t hash_key[HASH_KEY_SIZE]) {
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering unsigned long ul;
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering ul = dns_name_hash_func(DNS_RESOURCE_KEY_NAME(k), hash_key);
875c2e220e2611165e09051c4747971811f1de58Lennart Poetteringstatic int dns_resource_key_compare_func(const void *a, const void *b) {
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering const DnsResourceKey *x = a, *y = b;
78fe420ff0bb4cd94de3c4d3f15a3021cc3e2878Lennart Poettering ret = dns_name_compare_func(DNS_RESOURCE_KEY_NAME(x), DNS_RESOURCE_KEY_NAME(y));
875c2e220e2611165e09051c4747971811f1de58Lennart Poetteringconst struct hash_ops dns_resource_key_hash_ops = {
875c2e220e2611165e09051c4747971811f1de58Lennart Poetteringint dns_resource_key_to_string(const DnsResourceKey *key, char **ret) {
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering char cbuf[strlen("CLASS") + DECIMAL_STR_MAX(uint16_t)], tbuf[strlen("TYPE") + DECIMAL_STR_MAX(uint16_t)];
9833a66c7eba011c3740867c80133bc6fa976aa3Lennart Poettering const char *c, *t;
9833a66c7eba011c3740867c80133bc6fa976aa3Lennart Poettering if (asprintf(&s, "%s %s %-5s", DNS_RESOURCE_KEY_NAME(key), c, t) < 0)
875c2e220e2611165e09051c4747971811f1de58Lennart PoetteringDnsResourceRecord* dns_resource_record_new(DnsResourceKey *key) {
875c2e220e2611165e09051c4747971811f1de58Lennart PoetteringDnsResourceRecord* dns_resource_record_new_full(uint16_t class, uint16_t type, const char *name) {
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering _cleanup_(dns_resource_key_unrefp) DnsResourceKey *key = NULL;
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering key = dns_resource_key_new(class, type, name);
875c2e220e2611165e09051c4747971811f1de58Lennart PoetteringDnsResourceRecord* dns_resource_record_ref(DnsResourceRecord *rr) {
875c2e220e2611165e09051c4747971811f1de58Lennart PoetteringDnsResourceRecord* dns_resource_record_unref(DnsResourceRecord *rr) {
875c2e220e2611165e09051c4747971811f1de58Lennart Poetteringint dns_resource_record_new_reverse(DnsResourceRecord **ret, int family, const union in_addr_union *address, const char *hostname) {
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering _cleanup_(dns_resource_key_unrefp) DnsResourceKey *key = NULL;
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *rr = NULL;
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering r = dns_name_reverse(family, address, &ptr);
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering key = dns_resource_key_new_consume(DNS_CLASS_IN, DNS_TYPE_PTR, ptr);
875c2e220e2611165e09051c4747971811f1de58Lennart Poetteringint dns_resource_record_equal(const DnsResourceRecord *a, const DnsResourceRecord *b) {
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering r = dns_resource_key_equal(a->key, b->key);
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering switch (a->unparseable ? _DNS_TYPE_INVALID : a->key->type) {
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering r = dns_name_equal(a->srv.name, b->srv.name);
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering return a->srv.priority == b->srv.priority &&
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering return dns_name_equal(a->ptr.name, b->ptr.name);
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering return strcaseeq(a->hinfo.cpu, b->hinfo.cpu) &&
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering case DNS_TYPE_SPF: /* exactly the same as TXT */
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering return strv_equal(a->txt.strings, b->txt.strings);
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering return memcmp(&a->a.in_addr, &b->a.in_addr, sizeof(struct in_addr)) == 0;
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering return memcmp(&a->aaaa.in6_addr, &b->aaaa.in6_addr, sizeof(struct in6_addr)) == 0;
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering r = dns_name_equal(a->soa.mname, b->soa.mname);
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering r = dns_name_equal(a->soa.rname, b->soa.rname);
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering return dns_name_equal(a->mx.exchange, b->mx.exchange);
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering assert(a->loc.version == b->loc.version);
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering a->ds.digest_type == b->ds.digest_type &&
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering a->ds.digest_size == b->ds.digest_size &&
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering memcmp(a->ds.digest, b->ds.digest, a->ds.digest_size) == 0;
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering return a->sshfp.algorithm == b->sshfp.algorithm &&
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering a->sshfp.fingerprint_size == b->sshfp.fingerprint_size &&
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering memcmp(a->sshfp.fingerprint, b->sshfp.fingerprint, a->sshfp.fingerprint_size) == 0;
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering return a->dnskey.zone_key_flag == b->dnskey.zone_key_flag &&
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering a->dnskey.sep_flag == b->dnskey.sep_flag &&
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering a->dnskey.algorithm == b->dnskey.algorithm &&
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering a->dnskey.key_size == b->dnskey.key_size &&
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering memcmp(a->dnskey.key, b->dnskey.key, a->dnskey.key_size) == 0;
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering /* do the fast comparisons first */
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering if (a->rrsig.type_covered != b->rrsig.type_covered ||
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering a->rrsig.algorithm != b->rrsig.algorithm ||
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering a->rrsig.original_ttl != b->rrsig.original_ttl ||
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering a->rrsig.expiration != b->rrsig.expiration ||
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering a->rrsig.inception != b->rrsig.inception ||
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering a->rrsig.signature_size != b->rrsig.signature_size ||
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering memcmp(a->rrsig.signature, b->rrsig.signature, a->rrsig.signature_size) != 0)
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering return dns_name_equal(a->rrsig.signer, b->rrsig.signer);
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering return dns_name_equal(a->nsec.next_domain_name, b->nsec.next_domain_name) &&
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering bitmap_equal(a->nsec.types, b->nsec.types);
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering return a->nsec3.algorithm == b->nsec3.algorithm &&
4d9ced9956755901238fede6fc5a3d7e4e816aa6Lennart Poettering a->nsec3.iterations == b->nsec3.iterations &&
4d9ced9956755901238fede6fc5a3d7e4e816aa6Lennart Poettering a->nsec3.salt_size == b->nsec3.salt_size &&
4d9ced9956755901238fede6fc5a3d7e4e816aa6Lennart Poettering memcmp(a->nsec3.salt, b->nsec3.salt, a->nsec3.salt_size) == 0 &&
4d9ced9956755901238fede6fc5a3d7e4e816aa6Lennart Poettering memcmp(a->nsec3.next_hashed_name, b->nsec3.next_hashed_name, a->nsec3.next_hashed_name_size) == 0 &&
4d9ced9956755901238fede6fc5a3d7e4e816aa6Lennart Poettering bitmap_equal(a->nsec3.types, b->nsec3.types);
4d9ced9956755901238fede6fc5a3d7e4e816aa6Lennart Poettering return a->generic.size == b->generic.size &&
4d9ced9956755901238fede6fc5a3d7e4e816aa6Lennart Poettering memcmp(a->generic.data, b->generic.data, a->generic.size) == 0;
4d9ced9956755901238fede6fc5a3d7e4e816aa6Lennart Poetteringstatic char* format_location(uint32_t latitude, uint32_t longitude, uint32_t altitude,
4d9ced9956755901238fede6fc5a3d7e4e816aa6Lennart Poettering uint8_t size, uint8_t horiz_pre, uint8_t vert_pre) {
4d9ced9956755901238fede6fc5a3d7e4e816aa6Lennart Poettering char NS = latitude >= 1U<<31 ? 'N' : 'S';
4d9ced9956755901238fede6fc5a3d7e4e816aa6Lennart Poettering char EW = longitude >= 1U<<31 ? 'E' : 'W';
4d9ced9956755901238fede6fc5a3d7e4e816aa6Lennart Poettering int lat = latitude >= 1U<<31 ? (int) (latitude - (1U<<31)) : (int) ((1U<<31) - latitude);
4d9ced9956755901238fede6fc5a3d7e4e816aa6Lennart Poettering int lon = longitude >= 1U<<31 ? (int) (longitude - (1U<<31)) : (int) ((1U<<31) - longitude);
4d9ced9956755901238fede6fc5a3d7e4e816aa6Lennart Poettering double alt = altitude >= 10000000u ? altitude - 10000000u : -(double)(10000000u - altitude);
4d9ced9956755901238fede6fc5a3d7e4e816aa6Lennart Poettering double siz = (size >> 4) * exp10((double) (size & 0xF));
4d9ced9956755901238fede6fc5a3d7e4e816aa6Lennart Poettering double hor = (horiz_pre >> 4) * exp10((double) (horiz_pre & 0xF));
4d9ced9956755901238fede6fc5a3d7e4e816aa6Lennart Poettering double ver = (vert_pre >> 4) * exp10((double) (vert_pre & 0xF));
4d9ced9956755901238fede6fc5a3d7e4e816aa6Lennart Poettering if (asprintf(&s, "%d %d %.3f %c %d %d %.3f %c %.2fm %.2fm %.2fm %.2fm",
4d9ced9956755901238fede6fc5a3d7e4e816aa6Lennart Poetteringstatic int format_timestamp_dns(char *buf, size_t l, time_t sec) {
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering if (strftime(buf, l, "%Y%m%d%H%M%S", &tm) <= 0)
875c2e220e2611165e09051c4747971811f1de58Lennart Poetteringstatic char *format_types(Bitmap *types) {
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering r = strv_extend(&strv, dns_type_to_string(type));
875c2e220e2611165e09051c4747971811f1de58Lennart Poetteringint dns_resource_record_to_string(const DnsResourceRecord *rr, char **ret) {
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering _cleanup_free_ char *k = NULL, *t = NULL;
case DNS_TYPE_SRV:
return -ENOMEM;
case DNS_TYPE_PTR:
case DNS_TYPE_NS:
case DNS_TYPE_CNAME:
case DNS_TYPE_DNAME:
return -ENOMEM;
case DNS_TYPE_HINFO:
return -ENOMEM;
case DNS_TYPE_TXT:
return -ENOMEM;
return -ENOMEM;
case DNS_TYPE_A: {
return -ENOMEM;
case DNS_TYPE_AAAA:
return -ENOMEM;
case DNS_TYPE_SOA:
return -ENOMEM;
case DNS_TYPE_MX:
return -ENOMEM;
case DNS_TYPE_LOC:
return -ENOMEM;
return -ENOMEM;
case DNS_TYPE_DS:
return -ENOMEM;
return -ENOMEM;
case DNS_TYPE_SSHFP:
return -ENOMEM;
return -ENOMEM;
case DNS_TYPE_DNSKEY: {
const char *alg;
return -ENOMEM;
return -ENOMEM;
case DNS_TYPE_RRSIG: {
return -ENOMEM;
* http://tools.ietf.org/html/rfc3597#section-5 */
return -ENOMEM;
case DNS_TYPE_NSEC:
return -ENOMEM;
return -ENOMEM;
case DNS_TYPE_NSEC3: {
if (!salt)
return -ENOMEM;
if (!hash)
return -ENOMEM;
return -ENOMEM;
hash,
return -ENOMEM;
return -ENOMEM;
return -ENOMEM;
*ret = s;
switch (class) {
case DNS_CLASS_IN:
case DNS_CLASS_ANY:
return NULL;
assert(s);
return -EINVAL;