]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/resolve/resolved-dns-answer.c
dns-domain: add code for verifying validity of DNS-SD service names and types
[thirdparty/systemd.git] / src / resolve / resolved-dns-answer.c
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 "alloc-util.h"
23 #include "dns-domain.h"
24 #include "resolved-dns-answer.h"
25 #include "string-util.h"
26
27 DnsAnswer *dns_answer_new(unsigned n) {
28 DnsAnswer *a;
29
30 a = malloc0(offsetof(DnsAnswer, items) + sizeof(DnsAnswerItem) * n);
31 if (!a)
32 return NULL;
33
34 a->n_ref = 1;
35 a->n_allocated = n;
36
37 return a;
38 }
39
40 DnsAnswer *dns_answer_ref(DnsAnswer *a) {
41 if (!a)
42 return NULL;
43
44 assert(a->n_ref > 0);
45 a->n_ref++;
46 return a;
47 }
48
49 DnsAnswer *dns_answer_unref(DnsAnswer *a) {
50 if (!a)
51 return NULL;
52
53 assert(a->n_ref > 0);
54
55 if (a->n_ref == 1) {
56 unsigned i;
57
58 for (i = 0; i < a->n_rrs; i++)
59 dns_resource_record_unref(a->items[i].rr);
60
61 free(a);
62 } else
63 a->n_ref--;
64
65 return NULL;
66 }
67
68 int dns_answer_add(DnsAnswer *a, DnsResourceRecord *rr, int ifindex) {
69 unsigned i;
70 int r;
71
72 assert(rr);
73
74 if (!a)
75 return -ENOSPC;
76
77 for (i = 0; i < a->n_rrs; i++) {
78 if (a->items[i].ifindex != ifindex)
79 continue;
80
81 r = dns_resource_record_equal(a->items[i].rr, rr);
82 if (r < 0)
83 return r;
84 if (r > 0) {
85 /* Entry already exists, keep the entry with
86 * the higher RR, or the one with TTL 0 */
87
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);
91 a->items[i].rr = rr;
92 }
93
94 return 0;
95 }
96 }
97
98 if (a->n_rrs >= a->n_allocated)
99 return -ENOSPC;
100
101 a->items[a->n_rrs].rr = dns_resource_record_ref(rr);
102 a->items[a->n_rrs].ifindex = ifindex;
103 a->n_rrs++;
104
105 return 1;
106 }
107
108 int dns_answer_add_soa(DnsAnswer *a, const char *name, uint32_t ttl) {
109 _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *soa = NULL;
110
111 soa = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_SOA, name);
112 if (!soa)
113 return -ENOMEM;
114
115 soa->ttl = ttl;
116
117 soa->soa.mname = strdup(name);
118 if (!soa->soa.mname)
119 return -ENOMEM;
120
121 soa->soa.rname = strappend("root.", name);
122 if (!soa->soa.rname)
123 return -ENOMEM;
124
125 soa->soa.serial = 1;
126 soa->soa.refresh = 1;
127 soa->soa.retry = 1;
128 soa->soa.expire = 1;
129 soa->soa.minimum = ttl;
130
131 return dns_answer_add(a, soa, 0);
132 }
133
134 int dns_answer_contains(DnsAnswer *a, DnsResourceKey *key) {
135 unsigned i;
136 int r;
137
138 assert(key);
139
140 if (!a)
141 return 0;
142
143 for (i = 0; i < a->n_rrs; i++) {
144 r = dns_resource_key_match_rr(key, a->items[i].rr);
145 if (r < 0)
146 return r;
147 if (r > 0)
148 return 1;
149 }
150
151 return 0;
152 }
153
154 int dns_answer_match_soa(DnsResourceKey *key, DnsResourceKey *soa) {
155 if (soa->class != DNS_CLASS_IN)
156 return 0;
157
158 if (soa->type != DNS_TYPE_SOA)
159 return 0;
160
161 if (!dns_name_endswith(DNS_RESOURCE_KEY_NAME(key), DNS_RESOURCE_KEY_NAME(soa)))
162 return 0;
163
164 return 1;
165 }
166
167 int dns_answer_find_soa(DnsAnswer *a, DnsResourceKey *key, DnsResourceRecord **ret) {
168 unsigned i;
169
170 assert(key);
171 assert(ret);
172
173 if (!a)
174 return 0;
175
176 /* For a SOA record we can never find a matching SOA record */
177 if (key->type == DNS_TYPE_SOA)
178 return 0;
179
180 for (i = 0; i < a->n_rrs; i++) {
181
182 if (dns_answer_match_soa(key, a->items[i].rr->key)) {
183 *ret = a->items[i].rr;
184 return 1;
185 }
186 }
187
188 return 0;
189 }
190
191 DnsAnswer *dns_answer_merge(DnsAnswer *a, DnsAnswer *b) {
192 _cleanup_(dns_answer_unrefp) DnsAnswer *ret = NULL;
193 DnsAnswer *k;
194 unsigned i;
195 int r;
196
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);
201
202 ret = dns_answer_new((a ? a->n_rrs : 0) + (b ? b->n_rrs : 0));
203 if (!ret)
204 return NULL;
205
206 if (a) {
207 for (i = 0; i < a->n_rrs; i++) {
208 r = dns_answer_add(ret, a->items[i].rr, a->items[i].ifindex);
209 if (r < 0)
210 return NULL;
211 }
212 }
213
214 if (b) {
215 for (i = 0; i < b->n_rrs; i++) {
216 r = dns_answer_add(ret, b->items[i].rr, b->items[i].ifindex);
217 if (r < 0)
218 return NULL;
219 }
220 }
221
222 k = ret;
223 ret = NULL;
224
225 return k;
226 }
227
228 void dns_answer_order_by_scope(DnsAnswer *a, bool prefer_link_local) {
229 DnsAnswerItem *items;
230 unsigned i, start, end;
231
232 if (!a)
233 return;
234
235 if (a->n_rrs <= 1)
236 return;
237
238 start = 0;
239 end = a->n_rrs-1;
240
241 /* RFC 4795, Section 2.6 suggests we should order entries
242 * depending on whether the sender is a link-local address. */
243
244 items = newa(DnsAnswerItem, a->n_rrs);
245 for (i = 0; i < a->n_rrs; i++) {
246
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];
252 else
253 /* Order all other records to the beginning of the array */
254 items[start++] = a->items[i];
255 }
256
257 assert(start == end+1);
258 memcpy(a->items, items, sizeof(DnsAnswerItem) * a->n_rrs);
259 }
260
261 int dns_answer_reserve(DnsAnswer **a, unsigned n_free) {
262 DnsAnswer *n;
263
264 if (n_free <= 0)
265 return 0;
266
267 if (*a) {
268 unsigned ns;
269
270 if ((*a)->n_ref > 1)
271 return -EBUSY;
272
273 ns = (*a)->n_rrs + n_free;
274
275 if ((*a)->n_allocated >= ns)
276 return 0;
277
278 n = realloc(*a, offsetof(DnsAnswer, items) + sizeof(DnsAnswerItem) * ns);
279 if (!n)
280 return -ENOMEM;
281
282 n->n_allocated = ns;
283 } else {
284 n = dns_answer_new(n_free);
285 if (!n)
286 return -ENOMEM;
287 }
288
289 *a = n;
290 return 0;
291 }