resolved-dns-answer.c revision 1849cb7cb723e8ea7c13b967d056c1d3a36d9042
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering This file is part of systemd.
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering Copyright 2014 Lennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering systemd is free software; you can redistribute it and/or modify it
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering under the terms of the GNU Lesser General Public License as published by
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering the Free Software Foundation; either version 2.1 of the License, or
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering (at your option) any later version.
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering systemd is distributed in the hope that it will be useful, but
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering WITHOUT ANY WARRANTY; without even the implied warranty of
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering Lesser General Public License for more details.
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering You should have received a copy of the GNU Lesser General Public License
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering along with systemd; If not, see <http://www.gnu.org/licenses/>.
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering a = malloc0(offsetof(DnsAnswer, items) + sizeof(DnsAnswerItem) * n);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poetteringstatic void dns_answer_flush(DnsAnswer *a) {
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart PoetteringDnsAnswer *dns_answer_unref(DnsAnswer *a) {
8013e860b6344cb109e68208a3a91b0fc3cb9ed1Lennart Poetteringstatic int dns_answer_add_raw(DnsAnswer *a, DnsResourceRecord *rr, int ifindex) {
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering a->items[a->n_rrs].rr = dns_resource_record_ref(rr);
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poetteringstatic int dns_answer_add_raw_all(DnsAnswer *a, DnsAnswer *source) {
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering DNS_ANSWER_FOREACH_IFINDEX(rr, ifindex, source) {
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poetteringint dns_answer_add(DnsAnswer *a, DnsResourceRecord *rr, int ifindex) {
8013e860b6344cb109e68208a3a91b0fc3cb9ed1Lennart Poettering for (i = 0; i < a->n_rrs; i++) {
5d27351f8546530cf779847b0b04b0172c09f9d0Tom Gundersen r = dns_resource_record_equal(a->items[i].rr, rr);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering /* Entry already exists, keep the entry with
34b9656f0b2890743eee6a746ef08d817abfd5e9Lennart Poettering * the higher RR, or the one with TTL 0 */
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering if (rr->ttl == 0 || (rr->ttl > a->items[i].rr->ttl && a->items[i].rr->ttl != 0)) {
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering dns_resource_record_unref(a->items[i].rr);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering return dns_answer_add_raw(a, rr, ifindex);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poetteringstatic int dns_answer_add_all(DnsAnswer *a, DnsAnswer *b) {
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering DNS_ANSWER_FOREACH_IFINDEX(rr, ifindex, b) {
0f7091e624fdba6c0bf281f2a9a23cd3e9ca93fbLennart Poetteringint dns_answer_add_extend(DnsAnswer **a, DnsResourceRecord *rr, int ifindex) {
1086182d83d4c02a75f96f0184d5e8e5d3af6528Lennart Poetteringint dns_answer_add_soa(DnsAnswer *a, const char *name, uint32_t ttl) {
1086182d83d4c02a75f96f0184d5e8e5d3af6528Lennart Poettering _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *soa = NULL;
8013e860b6344cb109e68208a3a91b0fc3cb9ed1Lennart Poettering soa = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_SOA, name);
1086182d83d4c02a75f96f0184d5e8e5d3af6528Lennart Poettering soa->soa.rname = strappend("root.", name);
1086182d83d4c02a75f96f0184d5e8e5d3af6528Lennart Poetteringint dns_answer_match_key(DnsAnswer *a, const DnsResourceKey *key) {
1086182d83d4c02a75f96f0184d5e8e5d3af6528Lennart Poettering r = dns_resource_key_match_rr(key, i, NULL);
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poetteringint dns_answer_contains_rr(DnsAnswer *a, DnsResourceRecord *rr) {
58db254ade4fb2ef77de68f28c4f13814819f6a1Lennart Poetteringint dns_answer_find_soa(DnsAnswer *a, const DnsResourceKey *key, DnsResourceRecord **ret) {
58db254ade4fb2ef77de68f28c4f13814819f6a1Lennart Poettering /* For a SOA record we can never find a matching SOA record */
58db254ade4fb2ef77de68f28c4f13814819f6a1Lennart Poettering r = dns_resource_key_match_soa(key, rr->key);
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poetteringint dns_answer_find_cname_or_dname(DnsAnswer *a, const DnsResourceKey *key, DnsResourceRecord **ret) {
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering /* For a {C,D}NAME record we can never find a matching {C,D}NAME record */
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering if (key->type == DNS_TYPE_CNAME || key->type == DNS_TYPE_DNAME)
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering r = dns_resource_key_match_cname_or_dname(key, rr->key, NULL);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poetteringint dns_answer_merge(DnsAnswer *a, DnsAnswer *b, DnsAnswer **ret) {
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering _cleanup_(dns_answer_unrefp) DnsAnswer *k = NULL;
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poetteringint dns_answer_extend(DnsAnswer **a, DnsAnswer *b) {
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poetteringint dns_answer_remove_by_key(DnsAnswer **a, const DnsResourceKey *key) {
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering /* Remove all entries matching the specified key from *a */
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering r = dns_resource_key_equal(rr->key, key);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering *a = dns_answer_unref(*a); /* Return NULL for the empty answer */
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering _cleanup_(dns_answer_unrefp) DnsAnswer *copy = NULL;
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering DNS_ANSWER_FOREACH_IFINDEX(rr, ifindex, *a) {
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering r = dns_resource_key_equal(rr->key, key);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering r = dns_answer_add_raw(copy, rr, ifindex);
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering /* Only a single reference, edit in-place */
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering if (i >= (*a)->n_rrs)
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering r = dns_resource_key_equal((*a)->items[i].rr->key, key);
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering /* Kill this entry */
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering dns_resource_record_unref((*a)->items[i].rr);
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering memmove((*a)->items + i, (*a)->items + i + 1, sizeof(DnsAnswerItem) * ((*a)->n_rrs - i - 1));
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering /* Keep this entry */
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poetteringint dns_answer_copy_by_key(DnsAnswer **a, DnsAnswer *source, const DnsResourceKey *key) {
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering /* Copy all RRs matching the specified key from source into *a */
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering DNS_ANSWER_FOREACH_IFINDEX(rr_source, ifindex_source, source) {
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering r = dns_resource_key_equal(rr_source->key, key);
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering /* Make space for at least one entry */
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering r = dns_answer_add(*a, rr_source, ifindex_source);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poetteringvoid dns_answer_order_by_scope(DnsAnswer *a, bool prefer_link_local) {
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering /* RFC 4795, Section 2.6 suggests we should order entries
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering * depending on whether the sender is a link-local address. */
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering for (i = 0; i < a->n_rrs; i++) {
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering if (a->items[i].rr->key->class == DNS_CLASS_IN &&
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering ((a->items[i].rr->key->type == DNS_TYPE_A && in_addr_is_link_local(AF_INET, (union in_addr_union*) &a->items[i].rr->a.in_addr) != prefer_link_local) ||
(a->items[i].rr->key->type == DNS_TYPE_AAAA && in_addr_is_link_local(AF_INET6, (union in_addr_union*) &a->items[i].rr->aaaa.in6_addr) != prefer_link_local)))
DnsAnswer *n;
assert(a);
if (n_free <= 0)
unsigned ns;
return -EBUSY;
return -ENOMEM;
return -ENOMEM;
assert(a);
if (r != -EBUSY)
assert(*a);
return -ENOMEM;
r = dns_answer_add_raw_all(n, *a);
dns_answer_unref(*a);
n = NULL;