]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
faa133f3 | 2 | |
ca78ad1d ZJS |
3 | #include <stdio.h> |
4 | ||
b5efdb8a | 5 | #include "alloc-util.h" |
4ad7f276 | 6 | #include "dns-domain.h" |
07630cea | 7 | #include "resolved-dns-answer.h" |
72667f08 | 8 | #include "resolved-dns-dnssec.h" |
07630cea | 9 | #include "string-util.h" |
faa133f3 | 10 | |
da6053d0 | 11 | DnsAnswer *dns_answer_new(size_t n) { |
faa133f3 LP |
12 | DnsAnswer *a; |
13 | ||
78c6a153 | 14 | a = malloc0(offsetof(DnsAnswer, items) + sizeof(DnsAnswerItem) * n); |
faa133f3 LP |
15 | if (!a) |
16 | return NULL; | |
17 | ||
18 | a->n_ref = 1; | |
19 | a->n_allocated = n; | |
20 | ||
21 | return a; | |
22 | } | |
23 | ||
d42800f1 LP |
24 | static void dns_answer_flush(DnsAnswer *a) { |
25 | DnsResourceRecord *rr; | |
26 | ||
27 | if (!a) | |
28 | return; | |
29 | ||
30 | DNS_ANSWER_FOREACH(rr, a) | |
31 | dns_resource_record_unref(rr); | |
32 | ||
33 | a->n_rrs = 0; | |
34 | } | |
35 | ||
8301aa0b YW |
36 | static DnsAnswer *dns_answer_free(DnsAnswer *a) { |
37 | assert(a); | |
faa133f3 | 38 | |
8301aa0b YW |
39 | dns_answer_flush(a); |
40 | return mfree(a); | |
faa133f3 LP |
41 | } |
42 | ||
8301aa0b YW |
43 | DEFINE_TRIVIAL_REF_UNREF_FUNC(DnsAnswer, dns_answer, dns_answer_free); |
44 | ||
105e1512 | 45 | static int dns_answer_add_raw(DnsAnswer *a, DnsResourceRecord *rr, int ifindex, DnsAnswerFlags flags) { |
547973de LP |
46 | assert(rr); |
47 | ||
48 | if (!a) | |
49 | return -ENOSPC; | |
50 | ||
51 | if (a->n_rrs >= a->n_allocated) | |
52 | return -ENOSPC; | |
53 | ||
105e1512 LP |
54 | a->items[a->n_rrs++] = (DnsAnswerItem) { |
55 | .rr = dns_resource_record_ref(rr), | |
56 | .ifindex = ifindex, | |
57 | .flags = flags, | |
58 | }; | |
547973de LP |
59 | |
60 | return 1; | |
61 | } | |
62 | ||
63 | static int dns_answer_add_raw_all(DnsAnswer *a, DnsAnswer *source) { | |
64 | DnsResourceRecord *rr; | |
105e1512 | 65 | DnsAnswerFlags flags; |
547973de LP |
66 | int ifindex, r; |
67 | ||
105e1512 LP |
68 | DNS_ANSWER_FOREACH_FULL(rr, ifindex, flags, source) { |
69 | r = dns_answer_add_raw(a, rr, ifindex, flags); | |
547973de LP |
70 | if (r < 0) |
71 | return r; | |
72 | } | |
73 | ||
74 | return 0; | |
75 | } | |
76 | ||
105e1512 | 77 | int dns_answer_add(DnsAnswer *a, DnsResourceRecord *rr, int ifindex, DnsAnswerFlags flags) { |
da6053d0 | 78 | size_t i; |
7e8e0422 LP |
79 | int r; |
80 | ||
faa133f3 LP |
81 | assert(rr); |
82 | ||
78c6a153 LP |
83 | if (!a) |
84 | return -ENOSPC; | |
c296dd2e LP |
85 | if (a->n_ref > 1) |
86 | return -EBUSY; | |
78c6a153 | 87 | |
7e8e0422 | 88 | for (i = 0; i < a->n_rrs; i++) { |
78c6a153 LP |
89 | if (a->items[i].ifindex != ifindex) |
90 | continue; | |
91 | ||
dffb8277 | 92 | r = dns_resource_key_equal(a->items[i].rr->key, rr->key); |
7e8e0422 LP |
93 | if (r < 0) |
94 | return r; | |
dffb8277 ZJS |
95 | if (r == 0) |
96 | continue; | |
7feea00b | 97 | |
dffb8277 ZJS |
98 | /* There's already an RR of the same RRset in place! Let's see if the TTLs more or less |
99 | * match. We don't really care if they match precisely, but we do care whether one is 0 and | |
100 | * the other is not. See RFC 2181, Section 5.2. */ | |
101 | if ((rr->ttl == 0) != (a->items[i].rr->ttl == 0)) | |
102 | return -EINVAL; | |
103 | ||
104 | r = dns_resource_record_payload_equal(a->items[i].rr, rr); | |
7feea00b LP |
105 | if (r < 0) |
106 | return r; | |
dffb8277 ZJS |
107 | if (r == 0) |
108 | continue; | |
109 | ||
110 | /* Entry already exists, keep the entry with the higher RR. */ | |
111 | if (rr->ttl > a->items[i].rr->ttl) { | |
112 | dns_resource_record_ref(rr); | |
113 | dns_resource_record_unref(a->items[i].rr); | |
114 | a->items[i].rr = rr; | |
7feea00b | 115 | } |
dffb8277 ZJS |
116 | |
117 | a->items[i].flags |= flags; | |
118 | return 0; | |
7e8e0422 LP |
119 | } |
120 | ||
105e1512 | 121 | return dns_answer_add_raw(a, rr, ifindex, flags); |
547973de | 122 | } |
faa133f3 | 123 | |
547973de LP |
124 | static int dns_answer_add_all(DnsAnswer *a, DnsAnswer *b) { |
125 | DnsResourceRecord *rr; | |
105e1512 | 126 | DnsAnswerFlags flags; |
547973de | 127 | int ifindex, r; |
78c6a153 | 128 | |
105e1512 LP |
129 | DNS_ANSWER_FOREACH_FULL(rr, ifindex, flags, b) { |
130 | r = dns_answer_add(a, rr, ifindex, flags); | |
547973de LP |
131 | if (r < 0) |
132 | return r; | |
133 | } | |
134 | ||
135 | return 0; | |
136 | } | |
137 | ||
105e1512 | 138 | int dns_answer_add_extend(DnsAnswer **a, DnsResourceRecord *rr, int ifindex, DnsAnswerFlags flags) { |
547973de LP |
139 | int r; |
140 | ||
141 | assert(a); | |
142 | assert(rr); | |
143 | ||
144 | r = dns_answer_reserve_or_clone(a, 1); | |
145 | if (r < 0) | |
146 | return r; | |
147 | ||
105e1512 | 148 | return dns_answer_add(*a, rr, ifindex, flags); |
7e8e0422 LP |
149 | } |
150 | ||
97ebebbc | 151 | int dns_answer_add_soa(DnsAnswer *a, const char *name, uint32_t ttl, int ifindex) { |
8bf52d3d LP |
152 | _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *soa = NULL; |
153 | ||
154 | soa = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_SOA, name); | |
155 | if (!soa) | |
156 | return -ENOMEM; | |
157 | ||
57f5ad31 LP |
158 | soa->ttl = ttl; |
159 | ||
8bf52d3d LP |
160 | soa->soa.mname = strdup(name); |
161 | if (!soa->soa.mname) | |
162 | return -ENOMEM; | |
163 | ||
164 | soa->soa.rname = strappend("root.", name); | |
165 | if (!soa->soa.rname) | |
166 | return -ENOMEM; | |
167 | ||
168 | soa->soa.serial = 1; | |
169 | soa->soa.refresh = 1; | |
170 | soa->soa.retry = 1; | |
171 | soa->soa.expire = 1; | |
57f5ad31 | 172 | soa->soa.minimum = ttl; |
8bf52d3d | 173 | |
97ebebbc | 174 | return dns_answer_add(a, soa, ifindex, DNS_ANSWER_AUTHENTICATED); |
8bf52d3d LP |
175 | } |
176 | ||
105e1512 LP |
177 | int dns_answer_match_key(DnsAnswer *a, const DnsResourceKey *key, DnsAnswerFlags *ret_flags) { |
178 | DnsAnswerFlags flags = 0, i_flags; | |
547973de | 179 | DnsResourceRecord *i; |
105e1512 | 180 | bool found = false; |
7e8e0422 LP |
181 | int r; |
182 | ||
7e8e0422 LP |
183 | assert(key); |
184 | ||
105e1512 | 185 | DNS_ANSWER_FOREACH_FLAGS(i, i_flags, a) { |
547973de | 186 | r = dns_resource_key_match_rr(key, i, NULL); |
7e8e0422 LP |
187 | if (r < 0) |
188 | return r; | |
105e1512 LP |
189 | if (r == 0) |
190 | continue; | |
191 | ||
192 | if (!ret_flags) | |
7e8e0422 | 193 | return 1; |
105e1512 LP |
194 | |
195 | if (found) | |
196 | flags &= i_flags; | |
197 | else { | |
198 | flags = i_flags; | |
199 | found = true; | |
200 | } | |
7e8e0422 LP |
201 | } |
202 | ||
105e1512 LP |
203 | if (ret_flags) |
204 | *ret_flags = flags; | |
205 | ||
206 | return found; | |
7e8e0422 LP |
207 | } |
208 | ||
105e1512 LP |
209 | int dns_answer_contains_nsec_or_nsec3(DnsAnswer *a) { |
210 | DnsResourceRecord *i; | |
211 | ||
212 | DNS_ANSWER_FOREACH(i, a) { | |
213 | if (IN_SET(i->key->type, DNS_TYPE_NSEC, DNS_TYPE_NSEC3)) | |
214 | return true; | |
215 | } | |
216 | ||
217 | return false; | |
5eefe544 TG |
218 | } |
219 | ||
e926785a LP |
220 | int dns_answer_contains_zone_nsec3(DnsAnswer *answer, const char *zone) { |
221 | DnsResourceRecord *rr; | |
222 | int r; | |
223 | ||
224 | /* Checks whether the specified answer contains at least one NSEC3 RR in the specified zone */ | |
225 | ||
226 | DNS_ANSWER_FOREACH(rr, answer) { | |
227 | const char *p; | |
228 | ||
229 | if (rr->key->type != DNS_TYPE_NSEC3) | |
230 | continue; | |
231 | ||
1c02e7ba | 232 | p = dns_resource_key_name(rr->key); |
e926785a LP |
233 | r = dns_name_parent(&p); |
234 | if (r < 0) | |
235 | return r; | |
236 | if (r == 0) | |
237 | continue; | |
238 | ||
239 | r = dns_name_equal(p, zone); | |
240 | if (r != 0) | |
241 | return r; | |
242 | } | |
243 | ||
244 | return false; | |
245 | } | |
246 | ||
fd009cd8 | 247 | int dns_answer_find_soa(DnsAnswer *a, const DnsResourceKey *key, DnsResourceRecord **ret, DnsAnswerFlags *flags) { |
81f7fc5e LP |
248 | DnsResourceRecord *rr, *soa = NULL; |
249 | DnsAnswerFlags rr_flags, soa_flags = 0; | |
29c1519e | 250 | int r; |
7e8e0422 | 251 | |
7e8e0422 | 252 | assert(key); |
7e8e0422 | 253 | |
0f05c387 LP |
254 | /* For a SOA record we can never find a matching SOA record */ |
255 | if (key->type == DNS_TYPE_SOA) | |
256 | return 0; | |
257 | ||
fd009cd8 | 258 | DNS_ANSWER_FOREACH_FLAGS(rr, rr_flags, a) { |
29c1519e LP |
259 | r = dns_resource_key_match_soa(key, rr->key); |
260 | if (r < 0) | |
261 | return r; | |
262 | if (r > 0) { | |
81f7fc5e LP |
263 | |
264 | if (soa) { | |
1c02e7ba | 265 | r = dns_name_endswith(dns_resource_key_name(rr->key), dns_resource_key_name(soa->key)); |
81f7fc5e LP |
266 | if (r < 0) |
267 | return r; | |
268 | if (r > 0) | |
269 | continue; | |
270 | } | |
271 | ||
272 | soa = rr; | |
273 | soa_flags = rr_flags; | |
7e8e0422 LP |
274 | } |
275 | } | |
276 | ||
81f7fc5e LP |
277 | if (!soa) |
278 | return 0; | |
279 | ||
280 | if (ret) | |
281 | *ret = soa; | |
282 | if (flags) | |
283 | *flags = soa_flags; | |
284 | ||
285 | return 1; | |
faa133f3 | 286 | } |
934e9b10 | 287 | |
105e1512 | 288 | int dns_answer_find_cname_or_dname(DnsAnswer *a, const DnsResourceKey *key, DnsResourceRecord **ret, DnsAnswerFlags *flags) { |
5d27351f | 289 | DnsResourceRecord *rr; |
105e1512 | 290 | DnsAnswerFlags rr_flags; |
29c1519e | 291 | int r; |
5d27351f TG |
292 | |
293 | assert(key); | |
294 | ||
5d27351f | 295 | /* For a {C,D}NAME record we can never find a matching {C,D}NAME record */ |
6b2f7093 | 296 | if (!dns_type_may_redirect(key->type)) |
5d27351f TG |
297 | return 0; |
298 | ||
105e1512 | 299 | DNS_ANSWER_FOREACH_FLAGS(rr, rr_flags, a) { |
29c1519e LP |
300 | r = dns_resource_key_match_cname_or_dname(key, rr->key, NULL); |
301 | if (r < 0) | |
302 | return r; | |
303 | if (r > 0) { | |
5d27351f TG |
304 | if (ret) |
305 | *ret = rr; | |
105e1512 LP |
306 | if (flags) |
307 | *flags = rr_flags; | |
5d27351f TG |
308 | return 1; |
309 | } | |
310 | } | |
311 | ||
312 | return 0; | |
313 | } | |
314 | ||
547973de LP |
315 | int dns_answer_merge(DnsAnswer *a, DnsAnswer *b, DnsAnswer **ret) { |
316 | _cleanup_(dns_answer_unrefp) DnsAnswer *k = NULL; | |
317 | int r; | |
318 | ||
319 | assert(ret); | |
320 | ||
321 | if (dns_answer_size(a) <= 0) { | |
322 | *ret = dns_answer_ref(b); | |
323 | return 0; | |
324 | } | |
325 | ||
326 | if (dns_answer_size(b) <= 0) { | |
327 | *ret = dns_answer_ref(a); | |
328 | return 0; | |
329 | } | |
330 | ||
331 | k = dns_answer_new(a->n_rrs + b->n_rrs); | |
332 | if (!k) | |
333 | return -ENOMEM; | |
334 | ||
335 | r = dns_answer_add_raw_all(k, a); | |
336 | if (r < 0) | |
337 | return r; | |
338 | ||
339 | r = dns_answer_add_all(k, b); | |
340 | if (r < 0) | |
341 | return r; | |
342 | ||
1cc6c93a | 343 | *ret = TAKE_PTR(k); |
547973de LP |
344 | |
345 | return 0; | |
346 | } | |
347 | ||
348 | int dns_answer_extend(DnsAnswer **a, DnsAnswer *b) { | |
349 | DnsAnswer *merged; | |
350 | int r; | |
351 | ||
352 | assert(a); | |
353 | ||
354 | r = dns_answer_merge(*a, b, &merged); | |
355 | if (r < 0) | |
356 | return r; | |
357 | ||
358 | dns_answer_unref(*a); | |
359 | *a = merged; | |
360 | ||
361 | return 0; | |
362 | } | |
363 | ||
364 | int dns_answer_remove_by_key(DnsAnswer **a, const DnsResourceKey *key) { | |
365 | bool found = false, other = false; | |
366 | DnsResourceRecord *rr; | |
da6053d0 | 367 | size_t i; |
934e9b10 LP |
368 | int r; |
369 | ||
547973de LP |
370 | assert(a); |
371 | assert(key); | |
934e9b10 | 372 | |
547973de | 373 | /* Remove all entries matching the specified key from *a */ |
934e9b10 | 374 | |
547973de LP |
375 | DNS_ANSWER_FOREACH(rr, *a) { |
376 | r = dns_resource_key_equal(rr->key, key); | |
377 | if (r < 0) | |
378 | return r; | |
379 | if (r > 0) | |
380 | found = true; | |
381 | else | |
382 | other = true; | |
383 | ||
384 | if (found && other) | |
385 | break; | |
386 | } | |
387 | ||
388 | if (!found) | |
389 | return 0; | |
390 | ||
391 | if (!other) { | |
392 | *a = dns_answer_unref(*a); /* Return NULL for the empty answer */ | |
393 | return 1; | |
934e9b10 LP |
394 | } |
395 | ||
547973de LP |
396 | if ((*a)->n_ref > 1) { |
397 | _cleanup_(dns_answer_unrefp) DnsAnswer *copy = NULL; | |
105e1512 | 398 | DnsAnswerFlags flags; |
547973de LP |
399 | int ifindex; |
400 | ||
401 | copy = dns_answer_new((*a)->n_rrs); | |
402 | if (!copy) | |
403 | return -ENOMEM; | |
404 | ||
105e1512 | 405 | DNS_ANSWER_FOREACH_FULL(rr, ifindex, flags, *a) { |
547973de | 406 | r = dns_resource_key_equal(rr->key, key); |
934e9b10 | 407 | if (r < 0) |
547973de LP |
408 | return r; |
409 | if (r > 0) | |
410 | continue; | |
411 | ||
105e1512 | 412 | r = dns_answer_add_raw(copy, rr, ifindex, flags); |
547973de LP |
413 | if (r < 0) |
414 | return r; | |
934e9b10 | 415 | } |
547973de LP |
416 | |
417 | dns_answer_unref(*a); | |
1cc6c93a | 418 | *a = TAKE_PTR(copy); |
547973de LP |
419 | |
420 | return 1; | |
421 | } | |
422 | ||
423 | /* Only a single reference, edit in-place */ | |
424 | ||
425 | i = 0; | |
426 | for (;;) { | |
427 | if (i >= (*a)->n_rrs) | |
428 | break; | |
429 | ||
430 | r = dns_resource_key_equal((*a)->items[i].rr->key, key); | |
431 | if (r < 0) | |
432 | return r; | |
433 | if (r > 0) { | |
434 | /* Kill this entry */ | |
435 | ||
436 | dns_resource_record_unref((*a)->items[i].rr); | |
437 | memmove((*a)->items + i, (*a)->items + i + 1, sizeof(DnsAnswerItem) * ((*a)->n_rrs - i - 1)); | |
313cefa1 | 438 | (*a)->n_rrs--; |
547973de LP |
439 | continue; |
440 | ||
441 | } else | |
442 | /* Keep this entry */ | |
443 | i++; | |
934e9b10 LP |
444 | } |
445 | ||
547973de LP |
446 | return 1; |
447 | } | |
448 | ||
0c857028 LP |
449 | int dns_answer_remove_by_rr(DnsAnswer **a, DnsResourceRecord *rm) { |
450 | bool found = false, other = false; | |
451 | DnsResourceRecord *rr; | |
da6053d0 | 452 | size_t i; |
0c857028 LP |
453 | int r; |
454 | ||
455 | assert(a); | |
456 | assert(rm); | |
457 | ||
458 | /* Remove all entries matching the specified RR from *a */ | |
459 | ||
460 | DNS_ANSWER_FOREACH(rr, *a) { | |
461 | r = dns_resource_record_equal(rr, rm); | |
462 | if (r < 0) | |
463 | return r; | |
464 | if (r > 0) | |
465 | found = true; | |
466 | else | |
467 | other = true; | |
468 | ||
469 | if (found && other) | |
470 | break; | |
471 | } | |
472 | ||
473 | if (!found) | |
474 | return 0; | |
475 | ||
476 | if (!other) { | |
477 | *a = dns_answer_unref(*a); /* Return NULL for the empty answer */ | |
478 | return 1; | |
479 | } | |
480 | ||
481 | if ((*a)->n_ref > 1) { | |
482 | _cleanup_(dns_answer_unrefp) DnsAnswer *copy = NULL; | |
483 | DnsAnswerFlags flags; | |
484 | int ifindex; | |
485 | ||
486 | copy = dns_answer_new((*a)->n_rrs); | |
487 | if (!copy) | |
488 | return -ENOMEM; | |
489 | ||
490 | DNS_ANSWER_FOREACH_FULL(rr, ifindex, flags, *a) { | |
491 | r = dns_resource_record_equal(rr, rm); | |
492 | if (r < 0) | |
493 | return r; | |
494 | if (r > 0) | |
495 | continue; | |
496 | ||
497 | r = dns_answer_add_raw(copy, rr, ifindex, flags); | |
498 | if (r < 0) | |
499 | return r; | |
500 | } | |
501 | ||
502 | dns_answer_unref(*a); | |
1cc6c93a | 503 | *a = TAKE_PTR(copy); |
0c857028 LP |
504 | |
505 | return 1; | |
506 | } | |
507 | ||
508 | /* Only a single reference, edit in-place */ | |
509 | ||
510 | i = 0; | |
511 | for (;;) { | |
512 | if (i >= (*a)->n_rrs) | |
513 | break; | |
514 | ||
515 | r = dns_resource_record_equal((*a)->items[i].rr, rm); | |
516 | if (r < 0) | |
517 | return r; | |
518 | if (r > 0) { | |
519 | /* Kill this entry */ | |
520 | ||
521 | dns_resource_record_unref((*a)->items[i].rr); | |
522 | memmove((*a)->items + i, (*a)->items + i + 1, sizeof(DnsAnswerItem) * ((*a)->n_rrs - i - 1)); | |
313cefa1 | 523 | (*a)->n_rrs--; |
0c857028 LP |
524 | continue; |
525 | ||
526 | } else | |
527 | /* Keep this entry */ | |
528 | i++; | |
529 | } | |
530 | ||
531 | return 1; | |
532 | } | |
533 | ||
105e1512 | 534 | int dns_answer_copy_by_key(DnsAnswer **a, DnsAnswer *source, const DnsResourceKey *key, DnsAnswerFlags or_flags) { |
547973de LP |
535 | DnsResourceRecord *rr_source; |
536 | int ifindex_source, r; | |
105e1512 | 537 | DnsAnswerFlags flags_source; |
547973de LP |
538 | |
539 | assert(a); | |
540 | assert(key); | |
541 | ||
542 | /* Copy all RRs matching the specified key from source into *a */ | |
543 | ||
105e1512 | 544 | DNS_ANSWER_FOREACH_FULL(rr_source, ifindex_source, flags_source, source) { |
547973de LP |
545 | |
546 | r = dns_resource_key_equal(rr_source->key, key); | |
547 | if (r < 0) | |
548 | return r; | |
549 | if (r == 0) | |
550 | continue; | |
551 | ||
552 | /* Make space for at least one entry */ | |
553 | r = dns_answer_reserve_or_clone(a, 1); | |
554 | if (r < 0) | |
555 | return r; | |
934e9b10 | 556 | |
105e1512 | 557 | r = dns_answer_add(*a, rr_source, ifindex_source, flags_source|or_flags); |
547973de LP |
558 | if (r < 0) |
559 | return r; | |
560 | } | |
561 | ||
562 | return 0; | |
934e9b10 | 563 | } |
af93291c | 564 | |
105e1512 LP |
565 | int dns_answer_move_by_key(DnsAnswer **to, DnsAnswer **from, const DnsResourceKey *key, DnsAnswerFlags or_flags) { |
566 | int r; | |
567 | ||
568 | assert(to); | |
569 | assert(from); | |
570 | assert(key); | |
571 | ||
572 | r = dns_answer_copy_by_key(to, *from, key, or_flags); | |
573 | if (r < 0) | |
574 | return r; | |
575 | ||
576 | return dns_answer_remove_by_key(from, key); | |
577 | } | |
578 | ||
af93291c | 579 | void dns_answer_order_by_scope(DnsAnswer *a, bool prefer_link_local) { |
78c6a153 | 580 | DnsAnswerItem *items; |
da6053d0 | 581 | size_t i, start, end; |
78c6a153 LP |
582 | |
583 | if (!a) | |
584 | return; | |
af93291c LP |
585 | |
586 | if (a->n_rrs <= 1) | |
587 | return; | |
588 | ||
589 | start = 0; | |
590 | end = a->n_rrs-1; | |
591 | ||
592 | /* RFC 4795, Section 2.6 suggests we should order entries | |
593 | * depending on whether the sender is a link-local address. */ | |
594 | ||
78c6a153 | 595 | items = newa(DnsAnswerItem, a->n_rrs); |
af93291c LP |
596 | for (i = 0; i < a->n_rrs; i++) { |
597 | ||
78c6a153 LP |
598 | if (a->items[i].rr->key->class == DNS_CLASS_IN && |
599 | ((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) || | |
600 | (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))) | |
61233823 | 601 | /* Order address records that are not preferred to the end of the array */ |
78c6a153 | 602 | items[end--] = a->items[i]; |
af93291c LP |
603 | else |
604 | /* Order all other records to the beginning of the array */ | |
78c6a153 | 605 | items[start++] = a->items[i]; |
af93291c LP |
606 | } |
607 | ||
608 | assert(start == end+1); | |
78c6a153 LP |
609 | memcpy(a->items, items, sizeof(DnsAnswerItem) * a->n_rrs); |
610 | } | |
611 | ||
da6053d0 | 612 | int dns_answer_reserve(DnsAnswer **a, size_t n_free) { |
78c6a153 LP |
613 | DnsAnswer *n; |
614 | ||
2f763887 LP |
615 | assert(a); |
616 | ||
78c6a153 LP |
617 | if (n_free <= 0) |
618 | return 0; | |
619 | ||
620 | if (*a) { | |
da6053d0 | 621 | size_t ns; |
78c6a153 LP |
622 | |
623 | if ((*a)->n_ref > 1) | |
624 | return -EBUSY; | |
625 | ||
626 | ns = (*a)->n_rrs + n_free; | |
627 | ||
628 | if ((*a)->n_allocated >= ns) | |
629 | return 0; | |
630 | ||
2f763887 LP |
631 | /* Allocate more than we need */ |
632 | ns *= 2; | |
633 | ||
78c6a153 LP |
634 | n = realloc(*a, offsetof(DnsAnswer, items) + sizeof(DnsAnswerItem) * ns); |
635 | if (!n) | |
636 | return -ENOMEM; | |
637 | ||
638 | n->n_allocated = ns; | |
639 | } else { | |
640 | n = dns_answer_new(n_free); | |
641 | if (!n) | |
642 | return -ENOMEM; | |
643 | } | |
644 | ||
645 | *a = n; | |
646 | return 0; | |
af93291c | 647 | } |
547973de | 648 | |
da6053d0 | 649 | int dns_answer_reserve_or_clone(DnsAnswer **a, size_t n_free) { |
547973de LP |
650 | _cleanup_(dns_answer_unrefp) DnsAnswer *n = NULL; |
651 | int r; | |
652 | ||
653 | assert(a); | |
654 | ||
655 | /* Tries to extend the DnsAnswer object. And if that's not | |
96d49011 | 656 | * possible, since we are not the sole owner, then allocate a |
547973de LP |
657 | * new, appropriately sized one. Either way, after this call |
658 | * the object will only have a single reference, and has room | |
659 | * for at least the specified number of RRs. */ | |
660 | ||
661 | r = dns_answer_reserve(a, n_free); | |
662 | if (r != -EBUSY) | |
663 | return r; | |
664 | ||
665 | assert(*a); | |
666 | ||
667 | n = dns_answer_new(((*a)->n_rrs + n_free) * 2); | |
668 | if (!n) | |
669 | return -ENOMEM; | |
670 | ||
671 | r = dns_answer_add_raw_all(n, *a); | |
672 | if (r < 0) | |
673 | return r; | |
674 | ||
675 | dns_answer_unref(*a); | |
1cc6c93a | 676 | *a = TAKE_PTR(n); |
547973de LP |
677 | |
678 | return 0; | |
679 | } | |
26156910 LP |
680 | |
681 | void dns_answer_dump(DnsAnswer *answer, FILE *f) { | |
682 | DnsResourceRecord *rr; | |
683 | DnsAnswerFlags flags; | |
7b50eb2e | 684 | int ifindex; |
26156910 LP |
685 | |
686 | if (!f) | |
687 | f = stdout; | |
688 | ||
689 | DNS_ANSWER_FOREACH_FULL(rr, ifindex, flags, answer) { | |
7b50eb2e | 690 | const char *t; |
26156910 LP |
691 | |
692 | fputc('\t', f); | |
693 | ||
7b50eb2e LP |
694 | t = dns_resource_record_to_string(rr); |
695 | if (!t) { | |
26156910 LP |
696 | log_oom(); |
697 | continue; | |
698 | } | |
699 | ||
700 | fputs(t, f); | |
701 | ||
702 | if (ifindex != 0 || flags & (DNS_ANSWER_AUTHENTICATED|DNS_ANSWER_CACHEABLE|DNS_ANSWER_SHARED_OWNER)) | |
703 | fputs("\t;", f); | |
704 | ||
705 | if (ifindex != 0) | |
706 | printf(" ifindex=%i", ifindex); | |
707 | if (flags & DNS_ANSWER_AUTHENTICATED) | |
708 | fputs(" authenticated", f); | |
709 | if (flags & DNS_ANSWER_CACHEABLE) | |
710 | fputs(" cachable", f); | |
711 | if (flags & DNS_ANSWER_SHARED_OWNER) | |
712 | fputs(" shared-owner", f); | |
713 | ||
714 | fputc('\n', f); | |
715 | } | |
716 | } | |
43e6779a | 717 | |
a5042ec4 | 718 | int dns_answer_has_dname_for_cname(DnsAnswer *a, DnsResourceRecord *cname) { |
43e6779a LP |
719 | DnsResourceRecord *rr; |
720 | int r; | |
721 | ||
722 | assert(cname); | |
723 | ||
724 | /* Checks whether the answer contains a DNAME record that indicates that the specified CNAME record is | |
725 | * synthesized from it */ | |
726 | ||
727 | if (cname->key->type != DNS_TYPE_CNAME) | |
728 | return 0; | |
729 | ||
730 | DNS_ANSWER_FOREACH(rr, a) { | |
731 | _cleanup_free_ char *n = NULL; | |
732 | ||
733 | if (rr->key->type != DNS_TYPE_DNAME) | |
734 | continue; | |
735 | if (rr->key->class != cname->key->class) | |
736 | continue; | |
737 | ||
1c02e7ba | 738 | r = dns_name_change_suffix(cname->cname.name, rr->dname.name, dns_resource_key_name(rr->key), &n); |
43e6779a LP |
739 | if (r < 0) |
740 | return r; | |
741 | if (r == 0) | |
742 | continue; | |
743 | ||
1c02e7ba | 744 | r = dns_name_equal(n, dns_resource_key_name(cname->key)); |
43e6779a LP |
745 | if (r < 0) |
746 | return r; | |
747 | if (r > 0) | |
748 | return 1; | |
43e6779a LP |
749 | } |
750 | ||
751 | return 0; | |
752 | } |