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 "dns-domain.h"
23 #include "resolved-dns-answer.h"
24 #include "string-util.h"
26 DnsAnswer
*dns_answer_new(unsigned n
) {
29 a
= malloc0(offsetof(DnsAnswer
, items
) + sizeof(DnsAnswerItem
) * n
);
39 DnsAnswer
*dns_answer_ref(DnsAnswer
*a
) {
48 DnsAnswer
*dns_answer_unref(DnsAnswer
*a
) {
57 for (i
= 0; i
< a
->n_rrs
; i
++)
58 dns_resource_record_unref(a
->items
[i
].rr
);
67 int dns_answer_add(DnsAnswer
*a
, DnsResourceRecord
*rr
, int ifindex
) {
76 for (i
= 0; i
< a
->n_rrs
; i
++) {
77 if (a
->items
[i
].ifindex
!= ifindex
)
80 r
= dns_resource_record_equal(a
->items
[i
].rr
, rr
);
84 /* Entry already exists, keep the entry with
85 * the higher RR, or the one with TTL 0 */
87 if (rr
->ttl
== 0 || (rr
->ttl
> a
->items
[i
].rr
->ttl
&& a
->items
[i
].rr
->ttl
!= 0)) {
88 dns_resource_record_ref(rr
);
89 dns_resource_record_unref(a
->items
[i
].rr
);
97 if (a
->n_rrs
>= a
->n_allocated
)
100 a
->items
[a
->n_rrs
].rr
= dns_resource_record_ref(rr
);
101 a
->items
[a
->n_rrs
].ifindex
= ifindex
;
107 int dns_answer_add_soa(DnsAnswer
*a
, const char *name
, uint32_t ttl
) {
108 _cleanup_(dns_resource_record_unrefp
) DnsResourceRecord
*soa
= NULL
;
110 soa
= dns_resource_record_new_full(DNS_CLASS_IN
, DNS_TYPE_SOA
, name
);
116 soa
->soa
.mname
= strdup(name
);
120 soa
->soa
.rname
= strappend("root.", name
);
125 soa
->soa
.refresh
= 1;
128 soa
->soa
.minimum
= ttl
;
130 return dns_answer_add(a
, soa
, 0);
133 int dns_answer_contains(DnsAnswer
*a
, DnsResourceKey
*key
) {
142 for (i
= 0; i
< a
->n_rrs
; i
++) {
143 r
= dns_resource_key_match_rr(key
, a
->items
[i
].rr
);
153 int dns_answer_match_soa(DnsResourceKey
*key
, DnsResourceKey
*soa
) {
154 if (soa
->class != DNS_CLASS_IN
)
157 if (soa
->type
!= DNS_TYPE_SOA
)
160 if (!dns_name_endswith(DNS_RESOURCE_KEY_NAME(key
), DNS_RESOURCE_KEY_NAME(soa
)))
166 int dns_answer_find_soa(DnsAnswer
*a
, DnsResourceKey
*key
, DnsResourceRecord
**ret
) {
175 /* For a SOA record we can never find a matching SOA record */
176 if (key
->type
== DNS_TYPE_SOA
)
179 for (i
= 0; i
< a
->n_rrs
; i
++) {
181 if (dns_answer_match_soa(key
, a
->items
[i
].rr
->key
)) {
182 *ret
= a
->items
[i
].rr
;
190 DnsAnswer
*dns_answer_merge(DnsAnswer
*a
, DnsAnswer
*b
) {
191 _cleanup_(dns_answer_unrefp
) DnsAnswer
*ret
= NULL
;
196 if (a
&& (!b
|| b
->n_rrs
<= 0))
197 return dns_answer_ref(a
);
198 if ((!a
|| a
->n_rrs
<= 0) && b
)
199 return dns_answer_ref(b
);
201 ret
= dns_answer_new((a
? a
->n_rrs
: 0) + (b
? b
->n_rrs
: 0));
206 for (i
= 0; i
< a
->n_rrs
; i
++) {
207 r
= dns_answer_add(ret
, a
->items
[i
].rr
, a
->items
[i
].ifindex
);
214 for (i
= 0; i
< b
->n_rrs
; i
++) {
215 r
= dns_answer_add(ret
, b
->items
[i
].rr
, b
->items
[i
].ifindex
);
227 void dns_answer_order_by_scope(DnsAnswer
*a
, bool prefer_link_local
) {
228 DnsAnswerItem
*items
;
229 unsigned i
, start
, end
;
240 /* RFC 4795, Section 2.6 suggests we should order entries
241 * depending on whether the sender is a link-local address. */
243 items
= newa(DnsAnswerItem
, a
->n_rrs
);
244 for (i
= 0; i
< a
->n_rrs
; i
++) {
246 if (a
->items
[i
].rr
->key
->class == DNS_CLASS_IN
&&
247 ((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
) ||
248 (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
)))
249 /* Order address records that are are not preferred to the end of the array */
250 items
[end
--] = a
->items
[i
];
252 /* Order all other records to the beginning of the array */
253 items
[start
++] = a
->items
[i
];
256 assert(start
== end
+1);
257 memcpy(a
->items
, items
, sizeof(DnsAnswerItem
) * a
->n_rrs
);
260 int dns_answer_reserve(DnsAnswer
**a
, unsigned n_free
) {
272 ns
= (*a
)->n_rrs
+ n_free
;
274 if ((*a
)->n_allocated
>= ns
)
277 n
= realloc(*a
, offsetof(DnsAnswer
, items
) + sizeof(DnsAnswerItem
) * ns
);
283 n
= dns_answer_new(n_free
);