resolved-dns-rr.c revision fc8eec10f6a95a7ebb0f88954b49f2ed731c3a15
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering This file is part of systemd.
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering Copyright 2014 Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering systemd is free software; you can redistribute it and/or modify it
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering under the terms of the GNU Lesser General Public License as published by
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering the Free Software Foundation; either version 2.1 of the License, or
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering (at your option) any later version.
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering systemd is distributed in the hope that it will be useful, but
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering WITHOUT ANY WARRANTY; without even the implied warranty of
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering Lesser General Public License for more details.
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering You should have received a copy of the GNU Lesser General Public License
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering along with systemd; If not, see <http://www.gnu.org/licenses/>.
71d35b6b5563817dfbe757ab9e3b9f018b2db491Thomas Hindoe Paaboel Andersen#include "resolved-dns-packet.h"
71d35b6b5563817dfbe757ab9e3b9f018b2db491Thomas Hindoe Paaboel AndersenDnsResourceKey* dns_resource_key_new(uint16_t class, uint16_t type, const char *name) {
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering k = malloc0(sizeof(DnsResourceKey) + l + 1);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering strcpy((char*) k + sizeof(DnsResourceKey), name);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart PoetteringDnsResourceKey* dns_resource_key_new_redirect(const DnsResourceKey *key, const DnsResourceRecord *cname) {
e1c959948c0e31d6997bcdfbabfbd077784b2baeLennart Poettering assert(IN_SET(cname->key->type, DNS_TYPE_CNAME, DNS_TYPE_DNAME));
e1c959948c0e31d6997bcdfbabfbd077784b2baeLennart Poettering return dns_resource_key_new(key->class, key->type, cname->cname.name);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering r = dns_name_change_suffix(DNS_RESOURCE_KEY_NAME(key), DNS_RESOURCE_KEY_NAME(cname->key), cname->dname.name, &destination);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering return dns_resource_key_ref((DnsResourceKey*) key);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering k = dns_resource_key_new_consume(key->class, key->type, destination);
0dd25fb9f005d8ab7ac4bc10a609d00569f8c56aLennart Poetteringint dns_resource_key_new_append_suffix(DnsResourceKey **ret, DnsResourceKey *key, char *name) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering r = dns_name_concat(DNS_RESOURCE_KEY_NAME(key), name, &joined);
3cb10d3a0b1b6a7c44f307f2abb5215104e16941Lennart Poettering new_key = dns_resource_key_new_consume(key->class, key->type, joined);
8b757a38611006a751c90933d1810cccaa47e1afDaniel MackDnsResourceKey* dns_resource_key_new_consume(uint16_t class, uint16_t type, char *name) {
f0258e473667f44f4656dde49597b2badb9f598aLennart PoetteringDnsResourceKey* dns_resource_key_ref(DnsResourceKey *k) {
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering /* Static/const keys created with DNS_RESOURCE_KEY_CONST will
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering * set this to -1, they should not be reffed/unreffed */
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart PoetteringDnsResourceKey* dns_resource_key_unref(DnsResourceKey *k) {
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poetteringbool dns_resource_key_is_address(const DnsResourceKey *key) {
9c5e12a4314e7192e834e1b855e5e80111e636a6Tom Gundersen /* Check if this is an A or AAAA resource key */
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering return key->class == DNS_CLASS_IN && IN_SET(key->type, DNS_TYPE_A, DNS_TYPE_AAAA);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poetteringint dns_resource_key_equal(const DnsResourceKey *a, const DnsResourceKey *b) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering r = dns_name_equal(DNS_RESOURCE_KEY_NAME(a), DNS_RESOURCE_KEY_NAME(b));
d830ebbdf67d8cb32d33d8fdd47cf467fd6d3815Lennart Poetteringint dns_resource_key_match_rr(const DnsResourceKey *key, DnsResourceRecord *rr, const char *search_domain) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering /* Checks if an rr matches the specified key. If a search
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering * domain is specified, it will also be checked if the key
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering * with the search domain suffixed might match the RR. */
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (rr->key->class != key->class && key->class != DNS_CLASS_ANY)
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (rr->key->type != key->type && key->type != DNS_TYPE_ANY)
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering r = dns_name_equal(DNS_RESOURCE_KEY_NAME(rr->key), DNS_RESOURCE_KEY_NAME(key));
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering r = dns_name_concat(DNS_RESOURCE_KEY_NAME(key), search_domain, &joined);
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering return dns_name_equal(DNS_RESOURCE_KEY_NAME(rr->key), joined);
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmekint dns_resource_key_match_cname_or_dname(const DnsResourceKey *key, const DnsResourceKey *cname, const char *search_domain) {
ff3d6560bead6879a2fed1bf99bfe8273b3723f1Zbigniew Jędrzejewski-Szmek if (cname->class != key->class && key->class != DNS_CLASS_ANY)
ff3d6560bead6879a2fed1bf99bfe8273b3723f1Zbigniew Jędrzejewski-Szmek if (cname->type == DNS_TYPE_CNAME)
ff3d6560bead6879a2fed1bf99bfe8273b3723f1Zbigniew Jędrzejewski-Szmek r = dns_name_equal(DNS_RESOURCE_KEY_NAME(key), DNS_RESOURCE_KEY_NAME(cname));
ff3d6560bead6879a2fed1bf99bfe8273b3723f1Zbigniew Jędrzejewski-Szmek else if (cname->type == DNS_TYPE_DNAME)
ff3d6560bead6879a2fed1bf99bfe8273b3723f1Zbigniew Jędrzejewski-Szmek r = dns_name_endswith(DNS_RESOURCE_KEY_NAME(key), DNS_RESOURCE_KEY_NAME(cname));
ff3d6560bead6879a2fed1bf99bfe8273b3723f1Zbigniew Jędrzejewski-Szmek _cleanup_free_ char *joined = NULL;
ff3d6560bead6879a2fed1bf99bfe8273b3723f1Zbigniew Jędrzejewski-Szmek r = dns_name_concat(DNS_RESOURCE_KEY_NAME(key), search_domain, &joined);
51323288fc628a5cac50914df915545d685b793eLennart Poettering return dns_name_equal(joined, DNS_RESOURCE_KEY_NAME(cname));
106784ebb7b303ae471851100a773ad2aebf5b80Daniel Mack return dns_name_endswith(joined, DNS_RESOURCE_KEY_NAME(cname));
106784ebb7b303ae471851100a773ad2aebf5b80Daniel Mackint dns_resource_key_match_soa(const DnsResourceKey *key, const DnsResourceKey *soa) {
51323288fc628a5cac50914df915545d685b793eLennart Poettering /* Checks whether 'soa' is a SOA record for the specified key. */
const DnsResourceKey *k = i;
assert(k);
static int dns_resource_key_compare_func(const void *a, const void *b) {
const DnsResourceKey *x = a, *y = b;
int ret;
if (ret != 0)
return ret;
char cbuf[strlen("CLASS") + DECIMAL_STR_MAX(uint16_t)], tbuf[strlen("TYPE") + DECIMAL_STR_MAX(uint16_t)];
c = cbuf;
t = tbuf;
return -ENOMEM;
*ret = s;
assert(a);
assert(b);
/* Try to replace one RR key by another if they are identical, thus saving a bit of memory. Note that we do
* this only for RR keys, not for RRs themselves, as they carry a lot of additional metadata (where they come
* from, validity data, and suchlike), and cannot be replaced so easily by other RRs that have the same
if (dns_resource_key_equal(*a, *b) <= 0)
*b = dns_resource_key_ref(*a);
*a = dns_resource_key_ref(*b);
if (!rr)
return NULL;
return rr;
if (!key)
return NULL;
if (!rr)
return NULL;
return rr;
if (!rr)
return NULL;
return NULL;
case DNS_TYPE_SRV:
case DNS_TYPE_PTR:
case DNS_TYPE_NS:
case DNS_TYPE_CNAME:
case DNS_TYPE_DNAME:
case DNS_TYPE_HINFO:
case DNS_TYPE_TXT:
case DNS_TYPE_SPF:
case DNS_TYPE_SOA:
case DNS_TYPE_MX:
case DNS_TYPE_DS:
case DNS_TYPE_SSHFP:
case DNS_TYPE_DNSKEY:
case DNS_TYPE_RRSIG:
case DNS_TYPE_NSEC:
case DNS_TYPE_NSEC3:
case DNS_TYPE_LOC:
case DNS_TYPE_A:
case DNS_TYPE_AAAA:
case DNS_TYPE_TLSA:
case DNS_TYPE_OPENPGPKEY:
return NULL;
int dns_resource_record_new_reverse(DnsResourceRecord **ret, int family, const union in_addr_union *address, const char *hostname) {
if (!key)
return -ENOMEM;
if (!rr)
return -ENOMEM;
return -ENOMEM;
int dns_resource_record_new_address(DnsResourceRecord **ret, int family, const union in_addr_union *address, const char *name) {
if (!rr)
return -ENOMEM;
if (!rr)
return -ENOMEM;
return -EAFNOSUPPORT;
assert(a);
assert(b);
case DNS_TYPE_SRV:
case DNS_TYPE_PTR:
case DNS_TYPE_NS:
case DNS_TYPE_CNAME:
case DNS_TYPE_DNAME:
case DNS_TYPE_HINFO:
case DNS_TYPE_TXT:
case DNS_TYPE_A:
case DNS_TYPE_AAAA:
case DNS_TYPE_SOA:
case DNS_TYPE_MX:
case DNS_TYPE_LOC:
case DNS_TYPE_DS:
case DNS_TYPE_SSHFP:
case DNS_TYPE_DNSKEY:
case DNS_TYPE_RRSIG:
case DNS_TYPE_NSEC:
case DNS_TYPE_NSEC3:
memcmp(a->nsec3.next_hashed_name, b->nsec3.next_hashed_name, a->nsec3.next_hashed_name_size) == 0 &&
case DNS_TYPE_TLSA:
NS,
EW,
return NULL;
return -EINVAL;
return -EINVAL;
Iterator i;
unsigned type;
return NULL;
return NULL;
return NULL;
if (!str)
return NULL;
DnsTxtItem *i;
p = s = new(char, c);
return NULL;
size_t j;
if (i != first)
for (j = 0; j < i->length; j++) {
*(p++) = i->data[j];
return NULL;
case DNS_TYPE_SRV:
return NULL;
case DNS_TYPE_PTR:
case DNS_TYPE_NS:
case DNS_TYPE_CNAME:
case DNS_TYPE_DNAME:
return NULL;
case DNS_TYPE_HINFO:
return NULL;
case DNS_TYPE_TXT:
return NULL;
return NULL;
case DNS_TYPE_A: {
return NULL;
return NULL;
case DNS_TYPE_AAAA:
return NULL;
return NULL;
case DNS_TYPE_SOA:
return NULL;
case DNS_TYPE_MX:
return NULL;
case DNS_TYPE_LOC:
return NULL;
return NULL;
case DNS_TYPE_DS:
return NULL;
return NULL;
case DNS_TYPE_SSHFP:
return NULL;
return NULL;
case DNS_TYPE_DNSKEY: {
char *ss;
int n, n1;
return NULL;
&n1,
alg,
return NULL;
r = base64_append(&s, n,
return NULL;
key_tag);
return NULL;
free(s);
s = ss;
case DNS_TYPE_RRSIG: {
const char *type;
return NULL;
return NULL;
return NULL;
* http://tools.ietf.org/html/rfc3597#section-5 */
alg,
return NULL;
r = base64_append(&s, n,
return NULL;
case DNS_TYPE_NSEC:
return NULL;
return NULL;
case DNS_TYPE_NSEC3: {
if (!salt)
return NULL;
if (!hash)
return NULL;
return NULL;
hash,
return NULL;
case DNS_TYPE_TLSA: {
char *ss;
return NULL;
r = base64_append(&s, n,
return NULL;
return NULL;
free(s);
s = ss;
case DNS_TYPE_OPENPGPKEY: {
return NULL;
r = base64_append(&s, n,
return NULL;
return NULL;
return NULL;
.on_stack = true,
.refuse_compression = true,
return -ENODATA;
return -EINVAL;
*ret = n;
return -ENODATA;
return -EINVAL;
*ret = n;
const char *signer;
return -ENODATA;
case DNS_TYPE_SRV:
case DNS_TYPE_PTR:
case DNS_TYPE_NS:
case DNS_TYPE_CNAME:
case DNS_TYPE_DNAME:
case DNS_TYPE_HINFO:
case DNS_TYPE_TXT:
case DNS_TYPE_SPF: {
DnsTxtItem *j;
case DNS_TYPE_A:
case DNS_TYPE_AAAA:
case DNS_TYPE_SOA:
case DNS_TYPE_MX:
case DNS_TYPE_LOC:
case DNS_TYPE_SSHFP:
case DNS_TYPE_DNSKEY:
case DNS_TYPE_RRSIG:
case DNS_TYPE_NSEC:
case DNS_TYPE_DS:
case DNS_TYPE_NSEC3:
case DNS_TYPE_TLSA:
case DNS_TYPE_OPENPGPKEY:
static int dns_resource_record_compare_func(const void *a, const void *b) {
const DnsResourceRecord *x = a, *y = b;
int ret;
if (ret != 0)
return ret;
if (dns_resource_record_equal(x, y))
DnsTxtItem *n;
return NULL;
n = i->items_next;
free(i);
return dns_txt_item_free_all(n);
/* Mnemonics as listed on https://www.iana.org/assignments/dns-sec-alg-numbers/dns-sec-alg-numbers.xhtml */
/* Names as listed on https://www.iana.org/assignments/ds-rr-types/ds-rr-types.xhtml */