resolved-dns-rr.c revision 0f84a72e3c0f58d71cff2121e6df1611eaf9c9ea
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering This file is part of systemd.
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering Copyright 2014 Lennart Poettering
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering systemd is free software; you can redistribute it and/or modify it
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering under the terms of the GNU Lesser General Public License as published by
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering the Free Software Foundation; either version 2.1 of the License, or
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering (at your option) any later version.
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering systemd is distributed in the hope that it will be useful, but
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering WITHOUT ANY WARRANTY; without even the implied warranty of
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering Lesser General Public License for more details.
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering You should have received a copy of the GNU Lesser General Public License
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering along with systemd; If not, see <http://www.gnu.org/licenses/>.
4b357e15876b730343db08719c877fdb45b6ad42Michael MarineauDnsResourceKey* dns_resource_key_new(uint16_t class, uint16_t type, const char *name) {
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering k = malloc0(sizeof(DnsResourceKey) + l + 1);
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering strcpy((char*) k + sizeof(DnsResourceKey), name);
7384146530ac083efbef62b9ef5bb82c56565cd4Zbigniew Jędrzejewski-SzmekDnsResourceKey* dns_resource_key_new_consume(uint16_t class, uint16_t type, char *name) {
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart PoetteringDnsResourceKey* dns_resource_key_ref(DnsResourceKey *k) {
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart PoetteringDnsResourceKey* dns_resource_key_unref(DnsResourceKey *k) {
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poetteringint dns_resource_key_equal(const DnsResourceKey *a, const DnsResourceKey *b) {
7384146530ac083efbef62b9ef5bb82c56565cd4Zbigniew Jędrzejewski-Szmek r = dns_name_equal(DNS_RESOURCE_KEY_NAME(a), DNS_RESOURCE_KEY_NAME(b));
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poetteringint dns_resource_key_match_rr(const DnsResourceKey *key, const DnsResourceRecord *rr) {
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering if (rr->key->class != key->class && key->class != DNS_CLASS_ANY)
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering if (rr->key->type != key->type && key->type != DNS_TYPE_ANY)
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering return dns_name_equal(DNS_RESOURCE_KEY_NAME(rr->key), DNS_RESOURCE_KEY_NAME(key));
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poetteringint dns_resource_key_match_cname(const DnsResourceKey *key, const DnsResourceRecord *rr) {
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering if (rr->key->class != key->class && key->class != DNS_CLASS_ANY)
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering return dns_name_equal(DNS_RESOURCE_KEY_NAME(rr->key), DNS_RESOURCE_KEY_NAME(key));
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poetteringstatic unsigned long dns_resource_key_hash_func(const void *i, const uint8_t hash_key[HASH_KEY_SIZE]) {
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering unsigned long ul;
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering ul = dns_name_hash_func(DNS_RESOURCE_KEY_NAME(k), hash_key);
1ebab691c7749779072741f71865bd0e055b7ecfLennart Poetteringstatic int dns_resource_key_compare_func(const void *a, const void *b) {
c3834f9b881f2b1a68dc7d797c134f0b66b47b57Lennart Poettering const DnsResourceKey *x = a, *y = b;
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering ret = dns_name_compare_func(DNS_RESOURCE_KEY_NAME(x), DNS_RESOURCE_KEY_NAME(y));
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poetteringconst struct hash_ops dns_resource_key_hash_ops = {
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poetteringint dns_resource_key_to_string(const DnsResourceKey *key, char **ret) {
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering char cbuf[DECIMAL_STR_MAX(uint16_t)], tbuf[DECIMAL_STR_MAX(uint16_t)];
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering const char *c, *t;
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering if (asprintf(&s, "%s %s %-5s", DNS_RESOURCE_KEY_NAME(key), c, t) < 0)
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart PoetteringDnsResourceRecord* dns_resource_record_new(DnsResourceKey *key) {
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart PoetteringDnsResourceRecord* dns_resource_record_new_full(uint16_t class, uint16_t type, const char *name) {
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering _cleanup_(dns_resource_key_unrefp) DnsResourceKey *key = NULL;
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering key = dns_resource_key_new(class, type, name);
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart PoetteringDnsResourceRecord* dns_resource_record_ref(DnsResourceRecord *rr) {
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart PoetteringDnsResourceRecord* dns_resource_record_unref(DnsResourceRecord *rr) {
e48fdd84432bbf9c2ecc339183258c7c33116032Lennart Poetteringint dns_resource_record_new_reverse(DnsResourceRecord **ret, int family, const union in_addr_union *address, const char *hostname) {
4b1b14a6a6acb1640596d5e9542829d32989d385Lennart Poettering _cleanup_(dns_resource_key_unrefp) DnsResourceKey *key = NULL;
e48fdd84432bbf9c2ecc339183258c7c33116032Lennart Poettering _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *rr = NULL;
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering r = dns_name_reverse(family, address, &ptr);
e48fdd84432bbf9c2ecc339183258c7c33116032Lennart Poettering key = dns_resource_key_new_consume(DNS_CLASS_IN, DNS_TYPE_PTR, ptr);
e48fdd84432bbf9c2ecc339183258c7c33116032Lennart Poetteringint dns_resource_record_equal(const DnsResourceRecord *a, const DnsResourceRecord *b) {
e48fdd84432bbf9c2ecc339183258c7c33116032Lennart Poettering r = dns_resource_key_equal(a->key, b->key);
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering switch (a->unparseable ? _DNS_TYPE_INVALID : a->key->type) {
1af7211984a8dba3c5ba40fae794c4c55f5e6bd3Lennart Poettering r = dns_name_equal(a->srv.name, b->srv.name);
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering return a->srv.priority == b->srv.priority &&
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering return dns_name_equal(a->ptr.name, b->ptr.name);
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering return strcaseeq(a->hinfo.cpu, b->hinfo.cpu) &&
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering case DNS_TYPE_SPF: /* exactly the same as TXT */
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering return strv_equal(a->txt.strings, b->txt.strings);
1ca208fb4f93e5869704af1812cbff7130a2fc03Zbigniew Jędrzejewski-Szmek return memcmp(&a->a.in_addr, &b->a.in_addr, sizeof(struct in_addr)) == 0;
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering return memcmp(&a->aaaa.in6_addr, &b->aaaa.in6_addr, sizeof(struct in6_addr)) == 0;
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering r = dns_name_equal(a->soa.mname, b->soa.mname);
b47d419c25ecc735615a1088060c1ec8bef1e41fZbigniew Jędrzejewski-Szmek r = dns_name_equal(a->soa.rname, b->soa.rname);
1ca208fb4f93e5869704af1812cbff7130a2fc03Zbigniew Jędrzejewski-Szmek return a->soa.serial == b->soa.serial &&
1ca208fb4f93e5869704af1812cbff7130a2fc03Zbigniew Jędrzejewski-Szmek a->soa.expire == b->soa.expire &&
1ca208fb4f93e5869704af1812cbff7130a2fc03Zbigniew Jędrzejewski-Szmek a->soa.minimum == b->soa.minimum;
1ca208fb4f93e5869704af1812cbff7130a2fc03Zbigniew Jędrzejewski-Szmek if (a->mx.priority != b->mx.priority)
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering return dns_name_equal(a->mx.exchange, b->mx.exchange);
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering assert(a->loc.version == b->loc.version);
1ca208fb4f93e5869704af1812cbff7130a2fc03Zbigniew Jędrzejewski-Szmek a->loc.vert_pre == b->loc.vert_pre &&
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering return a->sshfp.algorithm == b->sshfp.algorithm &&
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering a->sshfp.key_size == b->sshfp.key_size &&
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering memcmp(a->sshfp.key, b->sshfp.key, a->sshfp.key_size) == 0;
1ca208fb4f93e5869704af1812cbff7130a2fc03Zbigniew Jędrzejewski-Szmek return a->dnskey.zone_key_flag == b->dnskey.zone_key_flag &&
1ca208fb4f93e5869704af1812cbff7130a2fc03Zbigniew Jędrzejewski-Szmek a->dnskey.sep_flag == b->dnskey.sep_flag &&
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering a->dnskey.algorithm == b->dnskey.algorithm &&
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering a->dnskey.key_size == b->dnskey.key_size &&
1ca208fb4f93e5869704af1812cbff7130a2fc03Zbigniew Jędrzejewski-Szmek memcmp(a->dnskey.key, b->dnskey.key, a->dnskey.key_size) == 0;
1ca208fb4f93e5869704af1812cbff7130a2fc03Zbigniew Jędrzejewski-Szmek /* do the fast comparisons first */
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering if (a->rrsig.type_covered != b->rrsig.type_covered ||
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering a->rrsig.algorithm != b->rrsig.algorithm ||
1ca208fb4f93e5869704af1812cbff7130a2fc03Zbigniew Jędrzejewski-Szmek a->rrsig.labels != b->rrsig.labels ||
1ca208fb4f93e5869704af1812cbff7130a2fc03Zbigniew Jędrzejewski-Szmek a->rrsig.original_ttl != b->rrsig.original_ttl ||
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering a->rrsig.expiration != b->rrsig.expiration ||
3db604b907323b8df0fc810216f6112056d26a02Lennart Poettering a->rrsig.inception != b->rrsig.inception ||
843f737ade9c73609a2280dd3dd16e18222a5dcbŁukasz Stelmach a->rrsig.signature_size != b->rrsig.signature_size ||
b94801803417c23d099cb7e508754181ecd27f9cZbigniew Jędrzejewski-Szmek memcmp(a->rrsig.signature, b->rrsig.signature, a->rrsig.signature_size) != 0)
843f737ade9c73609a2280dd3dd16e18222a5dcbŁukasz Stelmach return false;
1ca208fb4f93e5869704af1812cbff7130a2fc03Zbigniew Jędrzejewski-Szmek return dns_name_equal(a->rrsig.signer, b->rrsig.signer);
1ca208fb4f93e5869704af1812cbff7130a2fc03Zbigniew Jędrzejewski-Szmek return a->generic.size == b->generic.size &&
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering memcmp(a->generic.data, b->generic.data, a->generic.size) == 0;
b47d419c25ecc735615a1088060c1ec8bef1e41fZbigniew Jędrzejewski-Szmekstatic char* format_location(uint32_t latitude, uint32_t longitude, uint32_t altitude,
e48fdd84432bbf9c2ecc339183258c7c33116032Lennart Poettering uint8_t size, uint8_t horiz_pre, uint8_t vert_pre) {
e48fdd84432bbf9c2ecc339183258c7c33116032Lennart Poettering char NS = latitude >= 1U<<31 ? 'N' : 'S';
e48fdd84432bbf9c2ecc339183258c7c33116032Lennart Poettering char EW = longitude >= 1U<<31 ? 'E' : 'W';
e48fdd84432bbf9c2ecc339183258c7c33116032Lennart Poettering int lat = latitude >= 1U<<31 ? (int) (latitude - (1U<<31)) : (int) ((1U<<31) - latitude);
e48fdd84432bbf9c2ecc339183258c7c33116032Lennart Poettering int lon = longitude >= 1U<<31 ? (int) (longitude - (1U<<31)) : (int) ((1U<<31) - longitude);
e48fdd84432bbf9c2ecc339183258c7c33116032Lennart Poettering double alt = altitude >= 10000000u ? altitude - 10000000u : -(double)(10000000u - altitude);
e48fdd84432bbf9c2ecc339183258c7c33116032Lennart Poettering double siz = (size >> 4) * exp10((double) (size & 0xF));
e48fdd84432bbf9c2ecc339183258c7c33116032Lennart Poettering double hor = (horiz_pre >> 4) * exp10((double) (horiz_pre & 0xF));
e48fdd84432bbf9c2ecc339183258c7c33116032Lennart Poettering double ver = (vert_pre >> 4) * exp10((double) (vert_pre & 0xF));
e48fdd84432bbf9c2ecc339183258c7c33116032Lennart Poettering if (asprintf(&s, "%d %d %.3f %c %d %d %.3f %c %.2fm %.2fm %.2fm %.2fm",
e48fdd84432bbf9c2ecc339183258c7c33116032Lennart Poetteringint dns_resource_record_to_string(const DnsResourceRecord *rr, char **ret) {
e48fdd84432bbf9c2ecc339183258c7c33116032Lennart Poettering _cleanup_free_ char *k = NULL, *t = NULL;
e48fdd84432bbf9c2ecc339183258c7c33116032Lennart Poettering r = dns_resource_key_to_string(rr->key, &k);
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering switch (rr->unparseable ? _DNS_TYPE_INVALID : rr->key->type) {
b47d419c25ecc735615a1088060c1ec8bef1e41fZbigniew Jędrzejewski-Szmek s = strjoin(k, " ", rr->hinfo.cpu, " ", rr->hinfo.os, NULL);
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering case DNS_TYPE_SPF: /* exactly the same as TXT */
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering r = in_addr_to_string(AF_INET, (const union in_addr_union*) &rr->a.in_addr, &x);
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering r = in_addr_to_string(AF_INET6, (const union in_addr_union*) &rr->aaaa.in6_addr, &t);
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering r = asprintf(&s, "%s %s %s %u %u %u %u %u",
1ca208fb4f93e5869704af1812cbff7130a2fc03Zbigniew Jędrzejewski-Szmek t = format_location(rr->loc.latitude,
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering t = hexmem(rr->sshfp.key, rr->sshfp.key_size);
9a5cb1371b6d8b0a04bd08665bcf9b06cb40c64cZbigniew Jędrzejewski-Szmek alg = dnssec_algorithm_to_string(rr->dnskey.algorithm);
e48fdd84432bbf9c2ecc339183258c7c33116032Lennart Poettering t = hexmem(rr->dnskey.key, rr->dnskey.key_size);
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering alg ? 0 : 1, alg ? 0u : (unsigned) rr->dnskey.algorithm,
3db604b907323b8df0fc810216f6112056d26a02Lennart Poettering type = dns_type_to_string(rr->rrsig.type_covered);
3db604b907323b8df0fc810216f6112056d26a02Lennart Poettering alg = dnssec_algorithm_to_string(rr->rrsig.algorithm);
e48fdd84432bbf9c2ecc339183258c7c33116032Lennart Poettering t = hexmem(rr->rrsig.signature, rr->rrsig.signature_size);
1a14a53cfded6e78c6e8dfb73fdff0039971d642Lennart Poettering /* TYPE?? follows
3db604b907323b8df0fc810216f6112056d26a02Lennart Poettering * http://tools.ietf.org/html/rfc3597#section-5 */
3db604b907323b8df0fc810216f6112056d26a02Lennart Poettering r = asprintf(&s, "%s %s%.*u %.*s%.*u %u %u %u %u %u %s %s",
e48fdd84432bbf9c2ecc339183258c7c33116032Lennart Poettering type ? 0 : 1, type ? 0u : (unsigned) rr->rrsig.type_covered,
e48fdd84432bbf9c2ecc339183258c7c33116032Lennart Poettering alg ? 0 : 1, alg ? 0u : (unsigned) rr->rrsig.algorithm,
return -ENOMEM;
return -ENOMEM;
return -ENOMEM;
*ret = s;
switch (class) {
case DNS_CLASS_IN:
case DNS_CLASS_ANY:
return NULL;
assert(s);
return -EINVAL;