1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2014 Lennart Poettering
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
22 #include "alloc-util.h"
23 #include "dns-domain.h"
24 #include "resolved-dns-answer.h"
25 #include "string-util.h"
27 DnsAnswer
*dns_answer_new(unsigned n
) {
30 a
= malloc0(offsetof(DnsAnswer
, items
) + sizeof(DnsAnswerItem
) * n
);
40 DnsAnswer
*dns_answer_ref(DnsAnswer
*a
) {
49 DnsAnswer
*dns_answer_unref(DnsAnswer
*a
) {
58 for (i
= 0; i
< a
->n_rrs
; i
++)
59 dns_resource_record_unref(a
->items
[i
].rr
);
68 int dns_answer_add(DnsAnswer
*a
, DnsResourceRecord
*rr
, int ifindex
) {
77 for (i
= 0; i
< a
->n_rrs
; i
++) {
78 if (a
->items
[i
].ifindex
!= ifindex
)
81 r
= dns_resource_record_equal(a
->items
[i
].rr
, rr
);
85 /* Entry already exists, keep the entry with
86 * the higher RR, or the one with TTL 0 */
88 if (rr
->ttl
== 0 || (rr
->ttl
> a
->items
[i
].rr
->ttl
&& a
->items
[i
].rr
->ttl
!= 0)) {
89 dns_resource_record_ref(rr
);
90 dns_resource_record_unref(a
->items
[i
].rr
);
98 if (a
->n_rrs
>= a
->n_allocated
)
101 a
->items
[a
->n_rrs
].rr
= dns_resource_record_ref(rr
);
102 a
->items
[a
->n_rrs
].ifindex
= ifindex
;
108 int dns_answer_add_soa(DnsAnswer
*a
, const char *name
, uint32_t ttl
) {
109 _cleanup_(dns_resource_record_unrefp
) DnsResourceRecord
*soa
= NULL
;
111 soa
= dns_resource_record_new_full(DNS_CLASS_IN
, DNS_TYPE_SOA
, name
);
117 soa
->soa
.mname
= strdup(name
);
121 soa
->soa
.rname
= strappend("root.", name
);
126 soa
->soa
.refresh
= 1;
129 soa
->soa
.minimum
= ttl
;
131 return dns_answer_add(a
, soa
, 0);
134 int dns_answer_contains(DnsAnswer
*a
, DnsResourceKey
*key
) {
143 for (i
= 0; i
< a
->n_rrs
; i
++) {
144 r
= dns_resource_key_match_rr(key
, a
->items
[i
].rr
);
154 int dns_answer_match_soa(DnsResourceKey
*key
, DnsResourceKey
*soa
) {
155 if (soa
->class != DNS_CLASS_IN
)
158 if (soa
->type
!= DNS_TYPE_SOA
)
161 if (!dns_name_endswith(DNS_RESOURCE_KEY_NAME(key
), DNS_RESOURCE_KEY_NAME(soa
)))
167 int dns_answer_find_soa(DnsAnswer
*a
, DnsResourceKey
*key
, DnsResourceRecord
**ret
) {
176 /* For a SOA record we can never find a matching SOA record */
177 if (key
->type
== DNS_TYPE_SOA
)
180 for (i
= 0; i
< a
->n_rrs
; i
++) {
182 if (dns_answer_match_soa(key
, a
->items
[i
].rr
->key
)) {
183 *ret
= a
->items
[i
].rr
;
191 DnsAnswer
*dns_answer_merge(DnsAnswer
*a
, DnsAnswer
*b
) {
192 _cleanup_(dns_answer_unrefp
) DnsAnswer
*ret
= NULL
;
197 if (a
&& (!b
|| b
->n_rrs
<= 0))
198 return dns_answer_ref(a
);
199 if ((!a
|| a
->n_rrs
<= 0) && b
)
200 return dns_answer_ref(b
);
202 ret
= dns_answer_new((a
? a
->n_rrs
: 0) + (b
? b
->n_rrs
: 0));
207 for (i
= 0; i
< a
->n_rrs
; i
++) {
208 r
= dns_answer_add(ret
, a
->items
[i
].rr
, a
->items
[i
].ifindex
);
215 for (i
= 0; i
< b
->n_rrs
; i
++) {
216 r
= dns_answer_add(ret
, b
->items
[i
].rr
, b
->items
[i
].ifindex
);
228 void dns_answer_order_by_scope(DnsAnswer
*a
, bool prefer_link_local
) {
229 DnsAnswerItem
*items
;
230 unsigned i
, start
, end
;
241 /* RFC 4795, Section 2.6 suggests we should order entries
242 * depending on whether the sender is a link-local address. */
244 items
= newa(DnsAnswerItem
, a
->n_rrs
);
245 for (i
= 0; i
< a
->n_rrs
; i
++) {
247 if (a
->items
[i
].rr
->key
->class == DNS_CLASS_IN
&&
248 ((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
) ||
249 (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
)))
250 /* Order address records that are are not preferred to the end of the array */
251 items
[end
--] = a
->items
[i
];
253 /* Order all other records to the beginning of the array */
254 items
[start
++] = a
->items
[i
];
257 assert(start
== end
+1);
258 memcpy(a
->items
, items
, sizeof(DnsAnswerItem
) * a
->n_rrs
);
261 int dns_answer_reserve(DnsAnswer
**a
, unsigned n_free
) {
273 ns
= (*a
)->n_rrs
+ n_free
;
275 if ((*a
)->n_allocated
>= ns
)
278 n
= realloc(*a
, offsetof(DnsAnswer
, items
) + sizeof(DnsAnswerItem
) * ns
);
284 n
= dns_answer_new(n_free
);