]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/resolve/resolved-dns-answer.c
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 "resolved-dns-answer.h"
23 #include "resolved-dns-domain.h"
25 DnsAnswer
*dns_answer_new(unsigned n
) {
30 a
= malloc0(offsetof(DnsAnswer
, rrs
) + sizeof(DnsResourceRecord
*) * 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
->rrs
[i
]);
68 int dns_answer_add(DnsAnswer
*a
, DnsResourceRecord
*rr
) {
75 for (i
= 0; i
< a
->n_rrs
; i
++) {
76 r
= dns_resource_record_equal(a
->rrs
[i
], rr
);
80 /* Entry already exists, keep the entry with
81 * the higher RR, or the one with TTL 0 */
83 if (rr
->ttl
== 0 || (rr
->ttl
> a
->rrs
[i
]->ttl
&& a
->rrs
[i
]->ttl
!= 0)) {
84 dns_resource_record_ref(rr
);
85 dns_resource_record_unref(a
->rrs
[i
]);
93 if (a
->n_rrs
>= a
->n_allocated
)
96 a
->rrs
[a
->n_rrs
++] = dns_resource_record_ref(rr
);
100 int dns_answer_contains(DnsAnswer
*a
, DnsResourceKey
*key
) {
107 for (i
= 0; i
< a
->n_rrs
; i
++) {
108 r
= dns_resource_key_match_rr(key
, a
->rrs
[i
]);
118 int dns_answer_find_soa(DnsAnswer
*a
, DnsResourceKey
*key
, DnsResourceRecord
**ret
) {
125 /* For a SOA record we can never find a matching SOA record */
126 if (key
->type
== DNS_TYPE_SOA
)
129 for (i
= 0; i
< a
->n_rrs
; i
++) {
131 if (a
->rrs
[i
]->key
->class != DNS_CLASS_IN
)
134 if (a
->rrs
[i
]->key
->type
!= DNS_TYPE_SOA
)
137 if (dns_name_endswith(DNS_RESOURCE_KEY_NAME(key
), DNS_RESOURCE_KEY_NAME(a
->rrs
[i
]->key
))) {
146 DnsAnswer
*dns_answer_merge(DnsAnswer
*a
, DnsAnswer
*b
) {
147 _cleanup_(dns_answer_unrefp
) DnsAnswer
*ret
= NULL
;
152 if (a
&& (!b
|| b
->n_rrs
<= 0))
153 return dns_answer_ref(a
);
154 if ((!a
|| a
->n_rrs
<= 0) && b
)
155 return dns_answer_ref(b
);
157 ret
= dns_answer_new((a
? a
->n_rrs
: 0) + (b
? b
->n_rrs
: 0));
162 for (i
= 0; i
< a
->n_rrs
; i
++) {
163 r
= dns_answer_add(ret
, a
->rrs
[i
]);
170 for (i
= 0; i
< b
->n_rrs
; i
++) {
171 r
= dns_answer_add(ret
, b
->rrs
[i
]);
183 void dns_answer_order_by_scope(DnsAnswer
*a
, bool prefer_link_local
) {
184 DnsResourceRecord
**rrs
;
185 unsigned i
, start
, end
;
194 /* RFC 4795, Section 2.6 suggests we should order entries
195 * depending on whether the sender is a link-local address. */
197 rrs
= newa(DnsResourceRecord
*, a
->n_rrs
);
198 for (i
= 0; i
< a
->n_rrs
; i
++) {
200 if (a
->rrs
[i
]->key
->class == DNS_CLASS_IN
&&
201 ((a
->rrs
[i
]->key
->type
== DNS_TYPE_A
&& in_addr_is_link_local(AF_INET
, (union in_addr_union
*) &a
->rrs
[i
]->a
.in_addr
) != prefer_link_local
) ||
202 (a
->rrs
[i
]->key
->type
== DNS_TYPE_AAAA
&& in_addr_is_link_local(AF_INET6
, (union in_addr_union
*) &a
->rrs
[i
]->aaaa
.in6_addr
) != prefer_link_local
)))
203 /* Order address records that are are not preferred to the end of the array */
204 rrs
[end
--] = a
->rrs
[i
];
206 /* Order all other records to the beginning of the array */
207 rrs
[start
++] = a
->rrs
[i
];
210 assert(start
== end
+1);
211 memcpy(a
->rrs
, rrs
, sizeof(DnsResourceRecord
*) * a
->n_rrs
);