]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/resolve/resolved-dns-answer.c
resolved: make DnsQuestion logic handle NULL arrays as empty arrays
[thirdparty/systemd.git] / src / resolve / resolved-dns-answer.c
CommitLineData
faa133f3
LP
1/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3/***
4 This file is part of systemd.
5
6 Copyright 2014 Lennart Poettering
7
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.
12
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.
17
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/>.
20***/
21
22#include "resolved-dns-answer.h"
4ad7f276 23#include "dns-domain.h"
faa133f3
LP
24
25DnsAnswer *dns_answer_new(unsigned n) {
26 DnsAnswer *a;
27
faa133f3
LP
28 a = malloc0(offsetof(DnsAnswer, rrs) + sizeof(DnsResourceRecord*) * n);
29 if (!a)
30 return NULL;
31
32 a->n_ref = 1;
33 a->n_allocated = n;
34
35 return a;
36}
37
38DnsAnswer *dns_answer_ref(DnsAnswer *a) {
39 if (!a)
40 return NULL;
41
42 assert(a->n_ref > 0);
43 a->n_ref++;
44 return a;
45}
46
47DnsAnswer *dns_answer_unref(DnsAnswer *a) {
48 if (!a)
49 return NULL;
50
51 assert(a->n_ref > 0);
52
53 if (a->n_ref == 1) {
54 unsigned i;
55
56 for (i = 0; i < a->n_rrs; i++)
57 dns_resource_record_unref(a->rrs[i]);
58
59 free(a);
60 } else
61 a->n_ref--;
62
63 return NULL;
64}
65
66int dns_answer_add(DnsAnswer *a, DnsResourceRecord *rr) {
7e8e0422
LP
67 unsigned i;
68 int r;
69
faa133f3
LP
70 assert(a);
71 assert(rr);
72
7e8e0422
LP
73 for (i = 0; i < a->n_rrs; i++) {
74 r = dns_resource_record_equal(a->rrs[i], rr);
75 if (r < 0)
76 return r;
77 if (r > 0) {
78 /* Entry already exists, keep the entry with
79 * the higher RR, or the one with TTL 0 */
80
81 if (rr->ttl == 0 || (rr->ttl > a->rrs[i]->ttl && a->rrs[i]->ttl != 0)) {
82 dns_resource_record_ref(rr);
83 dns_resource_record_unref(a->rrs[i]);
84 a->rrs[i] = rr;
85 }
86
87 return 0;
88 }
89 }
90
faa133f3
LP
91 if (a->n_rrs >= a->n_allocated)
92 return -ENOSPC;
93
94 a->rrs[a->n_rrs++] = dns_resource_record_ref(rr);
7e8e0422
LP
95 return 1;
96}
97
57f5ad31 98int dns_answer_add_soa(DnsAnswer *a, const char *name, uint32_t ttl) {
8bf52d3d
LP
99 _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *soa = NULL;
100
101 soa = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_SOA, name);
102 if (!soa)
103 return -ENOMEM;
104
57f5ad31
LP
105 soa->ttl = ttl;
106
8bf52d3d
LP
107 soa->soa.mname = strdup(name);
108 if (!soa->soa.mname)
109 return -ENOMEM;
110
111 soa->soa.rname = strappend("root.", name);
112 if (!soa->soa.rname)
113 return -ENOMEM;
114
115 soa->soa.serial = 1;
116 soa->soa.refresh = 1;
117 soa->soa.retry = 1;
118 soa->soa.expire = 1;
57f5ad31 119 soa->soa.minimum = ttl;
8bf52d3d
LP
120
121 return dns_answer_add(a, soa);
122}
123
7e8e0422
LP
124int dns_answer_contains(DnsAnswer *a, DnsResourceKey *key) {
125 unsigned i;
126 int r;
127
128 assert(a);
129 assert(key);
130
131 for (i = 0; i < a->n_rrs; i++) {
132 r = dns_resource_key_match_rr(key, a->rrs[i]);
133 if (r < 0)
134 return r;
135 if (r > 0)
136 return 1;
137 }
138
139 return 0;
140}
141
142int dns_answer_find_soa(DnsAnswer *a, DnsResourceKey *key, DnsResourceRecord **ret) {
143 unsigned i;
144
145 assert(a);
146 assert(key);
147 assert(ret);
148
0f05c387
LP
149 /* For a SOA record we can never find a matching SOA record */
150 if (key->type == DNS_TYPE_SOA)
151 return 0;
152
7e8e0422
LP
153 for (i = 0; i < a->n_rrs; i++) {
154
155 if (a->rrs[i]->key->class != DNS_CLASS_IN)
156 continue;
157
158 if (a->rrs[i]->key->type != DNS_TYPE_SOA)
159 continue;
160
161 if (dns_name_endswith(DNS_RESOURCE_KEY_NAME(key), DNS_RESOURCE_KEY_NAME(a->rrs[i]->key))) {
162 *ret = a->rrs[i];
163 return 1;
164 }
165 }
166
faa133f3
LP
167 return 0;
168}
934e9b10
LP
169
170DnsAnswer *dns_answer_merge(DnsAnswer *a, DnsAnswer *b) {
171 _cleanup_(dns_answer_unrefp) DnsAnswer *ret = NULL;
172 DnsAnswer *k;
173 unsigned i;
174 int r;
175
176 if (a && (!b || b->n_rrs <= 0))
177 return dns_answer_ref(a);
178 if ((!a || a->n_rrs <= 0) && b)
179 return dns_answer_ref(b);
180
181 ret = dns_answer_new((a ? a->n_rrs : 0) + (b ? b->n_rrs : 0));
182 if (!ret)
183 return NULL;
184
185 if (a) {
186 for (i = 0; i < a->n_rrs; i++) {
187 r = dns_answer_add(ret, a->rrs[i]);
188 if (r < 0)
189 return NULL;
190 }
191 }
192
193 if (b) {
194 for (i = 0; i < b->n_rrs; i++) {
195 r = dns_answer_add(ret, b->rrs[i]);
196 if (r < 0)
197 return NULL;
198 }
199 }
200
201 k = ret;
202 ret = NULL;
203
204 return k;
205}
af93291c
LP
206
207void dns_answer_order_by_scope(DnsAnswer *a, bool prefer_link_local) {
208 DnsResourceRecord **rrs;
209 unsigned i, start, end;
210 assert(a);
211
212 if (a->n_rrs <= 1)
213 return;
214
215 start = 0;
216 end = a->n_rrs-1;
217
218 /* RFC 4795, Section 2.6 suggests we should order entries
219 * depending on whether the sender is a link-local address. */
220
221 rrs = newa(DnsResourceRecord*, a->n_rrs);
222 for (i = 0; i < a->n_rrs; i++) {
223
224 if (a->rrs[i]->key->class == DNS_CLASS_IN &&
225 ((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) ||
226 (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)))
227 /* Order address records that are are not preferred to the end of the array */
228 rrs[end--] = a->rrs[i];
229 else
230 /* Order all other records to the beginning of the array */
231 rrs[start++] = a->rrs[i];
232 }
233
234 assert(start == end+1);
235 memcpy(a->rrs, rrs, sizeof(DnsResourceRecord*) * a->n_rrs);
236}