resolved-dns-rr.c revision 0936416a1cba187dc76903dae73246f4cbddf312
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_redirect(const DnsResourceKey *key, const DnsResourceRecord *cname) {
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering assert(IN_SET(cname->key->type, DNS_TYPE_CNAME, DNS_TYPE_DNAME));
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering return dns_resource_key_new(key->class, key->type, cname->cname.name);
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering r = dns_name_change_suffix(DNS_RESOURCE_KEY_NAME(key), DNS_RESOURCE_KEY_NAME(cname->key), cname->dname.name, &destination);
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering return dns_resource_key_ref((DnsResourceKey*) key);
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering k = dns_resource_key_new_consume(key->class, key->type, destination);
875c2e220e2611165e09051c4747971811f1de58Lennart Poetteringint dns_resource_key_new_append_suffix(DnsResourceKey **ret, DnsResourceKey *key, char *name) {
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering r = dns_name_concat(DNS_RESOURCE_KEY_NAME(key), name, &joined);
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering new_key = dns_resource_key_new_consume(key->class, key->type, joined);
875c2e220e2611165e09051c4747971811f1de58Lennart PoetteringDnsResourceKey* dns_resource_key_new_consume(uint16_t class, uint16_t type, char *name) {
78fe420ff0bb4cd94de3c4d3f15a3021cc3e2878Lennart PoetteringDnsResourceKey* dns_resource_key_ref(DnsResourceKey *k) {
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering /* Static/const keys created with DNS_RESOURCE_KEY_CONST will
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering * set this to -1, they should not be reffed/unreffed */
875c2e220e2611165e09051c4747971811f1de58Lennart PoetteringDnsResourceKey* dns_resource_key_unref(DnsResourceKey *k) {
875c2e220e2611165e09051c4747971811f1de58Lennart Poetteringbool dns_resource_key_is_address(const DnsResourceKey *key) {
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering /* Check if this is an A or AAAA resource key */
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering return key->class == DNS_CLASS_IN && IN_SET(key->type, DNS_TYPE_A, DNS_TYPE_AAAA);
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));
9833a66c7eba011c3740867c80133bc6fa976aa3Lennart Poetteringint dns_resource_key_match_rr(const DnsResourceKey *key, DnsResourceRecord *rr, const char *search_domain) {
9833a66c7eba011c3740867c80133bc6fa976aa3Lennart Poettering /* Checks if an rr matches the specified key. If a search
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering * domain is specified, it will also be checked if the key
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering * with the search domain suffixed might match the 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 r = dns_name_equal(DNS_RESOURCE_KEY_NAME(rr->key), DNS_RESOURCE_KEY_NAME(key));
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering r = dns_name_concat(DNS_RESOURCE_KEY_NAME(key), search_domain, &joined);
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering return dns_name_equal(DNS_RESOURCE_KEY_NAME(rr->key), joined);
875c2e220e2611165e09051c4747971811f1de58Lennart Poetteringint dns_resource_key_match_cname_or_dname(const DnsResourceKey *key, const DnsResourceKey *cname, const char *search_domain) {
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering if (cname->class != key->class && key->class != DNS_CLASS_ANY)
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering r = dns_name_equal(DNS_RESOURCE_KEY_NAME(key), DNS_RESOURCE_KEY_NAME(cname));
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering r = dns_name_endswith(DNS_RESOURCE_KEY_NAME(key), DNS_RESOURCE_KEY_NAME(cname));
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering r = dns_name_concat(DNS_RESOURCE_KEY_NAME(key), search_domain, &joined);
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering return dns_name_equal(joined, DNS_RESOURCE_KEY_NAME(cname));
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering return dns_name_endswith(joined, DNS_RESOURCE_KEY_NAME(cname));
875c2e220e2611165e09051c4747971811f1de58Lennart Poetteringint dns_resource_key_match_soa(const DnsResourceKey *key, const DnsResourceKey *soa) {
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering /* Checks whether 'soa' is a SOA record for the specified key. */
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering return dns_name_endswith(DNS_RESOURCE_KEY_NAME(key), DNS_RESOURCE_KEY_NAME(soa));
875c2e220e2611165e09051c4747971811f1de58Lennart Poetteringstatic void dns_resource_key_hash_func(const void *i, struct siphash *state) {
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering dns_name_hash_func(DNS_RESOURCE_KEY_NAME(k), state);
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering siphash24_compress(&k->class, sizeof(k->class), state);
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering siphash24_compress(&k->type, sizeof(k->type), state);
875c2e220e2611165e09051c4747971811f1de58Lennart Poetteringstatic int dns_resource_key_compare_func(const void *a, const void *b) {
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering const DnsResourceKey *x = a, *y = b;
875c2e220e2611165e09051c4747971811f1de58Lennart 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)];
23bbb0de4e3f85d9704a5c12a5afa2dfa0159e41Michal Schmidt const char *c, *t;
875c2e220e2611165e09051c4747971811f1de58Lennart 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;
4d9ced9956755901238fede6fc5a3d7e4e816aa6Lennart Poettering r = dns_name_reverse(family, address, &ptr);
4d9ced9956755901238fede6fc5a3d7e4e816aa6Lennart Poettering key = dns_resource_key_new_consume(DNS_CLASS_IN, DNS_TYPE_PTR, ptr);
4d9ced9956755901238fede6fc5a3d7e4e816aa6Lennart Poetteringint dns_resource_record_new_address(DnsResourceRecord **ret, int family, const union in_addr_union *address, const char *name) {
4d9ced9956755901238fede6fc5a3d7e4e816aa6Lennart Poettering rr = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_A, name);
4d9ced9956755901238fede6fc5a3d7e4e816aa6Lennart Poettering rr = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_AAAA, name);
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 &&
4d9ced9956755901238fede6fc5a3d7e4e816aa6Lennart Poettering return dns_name_equal(a->ptr.name, b->ptr.name);
875c2e220e2611165e09051c4747971811f1de58Lennart Poettering return strcaseeq(a->hinfo.cpu, b->hinfo.cpu) &&
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 &&
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];
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;
.on_stack = true,
.refuse_compression = true,
DnsTxtItem *n;
return NULL;
n = i->items_next;
free(i);
return dns_txt_item_free_all(n);