Debian Codegraph @

src/shared/dns-answer.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <stdio.h>
4
5 #include "sd-json.h"
6
7 #include "alloc-util.h"
8 #include "dns-answer.h"
9 #include "dns-domain.h"
10 #include "dns-rr.h"
11 #include "log.h"
12 #include "random-util.h"
13 #include "siphash24.h"
14 #include "string-util.h"
15
16 static DnsAnswerItem *dns_answer_item_free(DnsAnswerItem *item) {
17 if (!item)
18 return NULL;
19
20 dns_resource_record_unref(item->rr);
21 dns_resource_record_unref(item->rrsig);
22
23 return mfree(item);
24 }
25
26 DEFINE_PRIVATE_TRIVIAL_REF_UNREF_FUNC(DnsAnswerItem, dns_answer_item, dns_answer_item_free);
27 DEFINE_TRIVIAL_CLEANUP_FUNC(DnsAnswerItem*, dns_answer_item_unref);
28
29 static void dns_answer_item_hash_func(const DnsAnswerItem *a, struct siphash *state) {
30 assert(a);
31 assert(state);
32
33 siphash24_compress_typesafe(a->ifindex, state);
34
35 dns_resource_record_hash_func(a->rr, state);
36 }
37
38 static int dns_answer_item_compare_func(const DnsAnswerItem *a, const DnsAnswerItem *b) {
39 int r;
40
41 assert(a);
42 assert(b);
43
44 r = CMP(a->ifindex, b->ifindex);
45 if (r != 0)
46 return r;
47
48 return dns_resource_record_compare_func(a->rr, b->rr);
49 }
50
51 DEFINE_PRIVATE_HASH_OPS_WITH_KEY_DESTRUCTOR(
52 dns_answer_item_hash_ops,
53 DnsAnswerItem,
54 dns_answer_item_hash_func,
55 dns_answer_item_compare_func,
56 dns_answer_item_unref);
57
58 static int dns_answer_reserve_internal(DnsAnswer *a, size_t n) {
59 size_t m;
60
61 assert(a);
62 assert(a->items);
63
64 m = ordered_set_size(a->items);
65 assert(m <= UINT16_MAX); /* We can only place 64K RRs in an answer at max */
66
67 n = saturate_add(m, n, UINT16_MAX);
68
69 /* Higher multipliers give slightly higher efficiency through hash collisions, but the gains
70 * quickly drop off after 2. */
71 return ordered_set_reserve(a->items, n * 2);
72 }
73
74 DnsAnswer *dns_answer_new(size_t n) {
75 _cleanup_ordered_set_free_ OrderedSet *s = NULL;
76 _cleanup_(dns_answer_unrefp) DnsAnswer *a = NULL;
77
78 if (n > UINT16_MAX)
79 n = UINT16_MAX;
80
81 s = ordered_set_new(&dns_answer_item_hash_ops);
82 if (!s)
83 return NULL;
84
85 a = new(DnsAnswer, 1);
86 if (!a)
87 return NULL;
88
89 *a = (DnsAnswer) {
90 .n_ref = 1,
91 .items = TAKE_PTR(s),
92 };
93
94 if (dns_answer_reserve_internal(a, n) < 0)
95 return NULL;
96
97 return TAKE_PTR(a);
98 }
99
100 static DnsAnswer *dns_answer_free(DnsAnswer *a) {
101 assert(a);
102
103 ordered_set_free(a->items);
104 return mfree(a);
105 }
106
107 DEFINE_TRIVIAL_REF_UNREF_FUNC(DnsAnswer, dns_answer, dns_answer_free);
108
109 static int dns_answer_add_raw(
110 DnsAnswer *a,
111 DnsResourceRecord *rr,
112 int ifindex,
113 DnsAnswerFlags flags,
114 DnsResourceRecord *rrsig,
115 usec_t until) {
116
117 _cleanup_(dns_answer_item_unrefp) DnsAnswerItem *item = NULL;
118 int r;
119
120 assert(rr);
121
122 if (!a)
123 return -ENOSPC;
124
125 if (dns_answer_size(a) >= UINT16_MAX)
126 return -ENOSPC;
127
128 item = new(DnsAnswerItem, 1);
129 if (!item)
130 return -ENOMEM;
131
132 *item = (DnsAnswerItem) {
133 .n_ref = 1,
134 .rr = dns_resource_record_ref(rr),
135 .ifindex = ifindex,
136 .flags = flags,
137 .until = until,
138 .rrsig = dns_resource_record_ref(rrsig),
139 };
140
141 r = ordered_set_put(a->items, item);
142 if (r < 0)
143 return r;
144
145 TAKE_PTR(item);
146 return 1;
147 }
148
149 static int dns_answer_add_raw_all(DnsAnswer *a, DnsAnswer *source) {
150 DnsAnswerItem *item;
151 int r;
152
153 DNS_ANSWER_FOREACH_ITEM(item, source) {
154 r = dns_answer_add_raw(
155 a,
156 item->rr,
157 item->ifindex,
158 item->flags,
159 item->rrsig,
160 /* until= */ USEC_INFINITY);
161 if (r < 0)
162 return r;
163 }
164
165 return 0;
166 }
167
168 int dns_answer_add_full(
169 DnsAnswer *a,
170 DnsResourceRecord *rr,
171 int ifindex,
172 DnsAnswerFlags flags,
173 DnsResourceRecord *rrsig,
174 usec_t until) {
175
176 DnsAnswerItem tmp, *exist;
177
178 assert(rr);
179
180 if (!a)
181 return -ENOSPC;
182 if (a->n_ref > 1)
183 return -EBUSY;
184
185 tmp = (DnsAnswerItem) {
186 .rr = rr,
187 .ifindex = ifindex,
188 };
189
190 exist = ordered_set_get(a->items, &tmp);
191 if (exist) {
192 /* There's already an RR of the same RRset in place! Let's see if the TTLs more or
193 * less match. RFC 2181, Section 5.2 suggests clients should reject RRsets
194 * containing RRs with differing TTLs. We are more tolerant of this situation except
195 * if one RR has a zero TTL and the other a nonzero TTL. In mDNS, zero TTLs are
196 * special, so we must error in that case. */
197 if ((rr->ttl == 0) != (exist->rr->ttl == 0)) {
198 if ((exist->flags | flags) & DNS_ANSWER_REFUSE_TTL_NO_MATCH)
199 return log_debug_errno(
200 SYNTHETIC_ERRNO(EINVAL),
201 "Refusing to merge RRs with zero TTL and non-zero TTL: %s vs. %s",
202 dns_resource_record_to_string(rr),
203 dns_resource_record_to_string(exist->rr));
204
205 log_debug("Merging RRs with zero TTL and non-zero TTL (not RFC 2181/5.2 compliant): %s vs. %s",
206 dns_resource_record_to_string(rr),
207 dns_resource_record_to_string(exist->rr));
208 }
209
210 /* Entry already exists, keep the entry with the higher TTL. */
211 if (rr->ttl > exist->rr->ttl) {
212 DNS_RR_REPLACE(exist->rr, dns_resource_record_ref(rr));
213
214 /* Update RRSIG and RR at the same time */
215 if (rrsig)
216 DNS_RR_REPLACE(exist->rrsig, dns_resource_record_ref(rrsig));
217 }
218
219 exist->flags |= flags;
220
221 if (rr->key->type == DNS_TYPE_RRSIG) {
222 /* If the rr is RRSIG, then move the rr to the end. */
223 assert_se(ordered_set_remove(a->items, exist) == exist);
224 assert_se(ordered_set_put(a->items, exist) == 1);
225 }
226 return 0;
227 }
228
229 return dns_answer_add_raw(a, rr, ifindex, flags, rrsig, until);
230 }
231
232 static int dns_answer_add_all(DnsAnswer *a, DnsAnswer *b) {
233 DnsAnswerItem *item;
234 int r;
235
236 DNS_ANSWER_FOREACH_ITEM(item, b) {
237 r = dns_answer_add(a, item->rr, item->ifindex, item->flags, item->rrsig);
238 if (r < 0)
239 return r;
240 }
241
242 return 0;
243 }
244
245 int dns_answer_add_extend_full(
246 DnsAnswer **a,
247 DnsResourceRecord *rr,
248 int ifindex,
249 DnsAnswerFlags flags,
250 DnsResourceRecord *rrsig,
251 usec_t until) {
252
253 int r;
254
255 assert(a);
256 assert(rr);
257
258 r = dns_answer_reserve_or_clone(a, 1);
259 if (r < 0)
260 return r;
261
262 return dns_answer_add_full(*a, rr, ifindex, flags, rrsig, until);
263 }
264
265 int dns_answer_add_soa(DnsAnswer *a, const char *name, uint32_t ttl, int ifindex) {
266 _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *soa = NULL;
267
268 soa = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_SOA, name);
269 if (!soa)
270 return -ENOMEM;
271
272 soa->ttl = ttl;
273
274 soa->soa.mname = strdup(name);
275 if (!soa->soa.mname)
276 return -ENOMEM;
277
278 soa->soa.rname = strjoin("root.", name);
279 if (!soa->soa.rname)
280 return -ENOMEM;
281
282 soa->soa.serial = 1;
283 soa->soa.refresh = 1;
284 soa->soa.retry = 1;
285 soa->soa.expire = 1;
286 soa->soa.minimum = ttl;
287
288 return dns_answer_add(a, soa, ifindex, DNS_ANSWER_AUTHENTICATED, NULL);
289 }
290
291 int dns_answer_match_key(DnsAnswer *a, const DnsResourceKey *key, DnsAnswerFlags *ret_flags) {
292 DnsAnswerFlags flags = 0, i_flags;
293 DnsResourceRecord *i;
294 bool found = false;
295 int r;
296
297 assert(key);
298
299 DNS_ANSWER_FOREACH_FLAGS(i, i_flags, a) {
300 r = dns_resource_key_match_rr(key, i, NULL);
301 if (r < 0)
302 return r;
303 if (r == 0)
304 continue;
305
306 if (!ret_flags)
307 return 1;
308
309 if (found)
310 flags &= i_flags;
311 else {
312 flags = i_flags;
313 found = true;
314 }
315 }
316
317 if (ret_flags)
318 *ret_flags = flags;
319
320 return found;
321 }
322
323 bool dns_answer_contains_nsec_or_nsec3(DnsAnswer *a) {
324 DnsResourceRecord *i;
325
326 DNS_ANSWER_FOREACH(i, a)
327 if (IN_SET(i->key->type, DNS_TYPE_NSEC, DNS_TYPE_NSEC3))
328 return true;
329
330 return false;
331 }
332
333 int dns_answer_contains_zone_nsec3(DnsAnswer *answer, const char *zone) {
334 DnsResourceRecord *rr;
335 int r;
336
337 /* Checks whether the specified answer contains at least one NSEC3 RR in the specified zone */
338
339 DNS_ANSWER_FOREACH(rr, answer) {
340 const char *p;
341
342 if (rr->key->type != DNS_TYPE_NSEC3)
343 continue;
344
345 p = dns_resource_key_name(rr->key);
346 r = dns_name_parent(&p);
347 if (r < 0)
348 return r;
349 if (r == 0)
350 continue;
351
352 r = dns_name_equal(p, zone);
353 if (r != 0)
354 return r;
355 }
356
357 return false;
358 }
359
360 bool dns_answer_contains(DnsAnswer *answer, DnsResourceRecord *rr) {
361 DnsResourceRecord *i;
362
363 DNS_ANSWER_FOREACH(i, answer)
364 if (dns_resource_record_equal(i, rr))
365 return true;
366
367 return false;
368 }
369
370 int dns_answer_find_soa(
371 DnsAnswer *a,
372 const DnsResourceKey *key,
373 DnsResourceRecord **ret,
374 DnsAnswerFlags *ret_flags) {
375
376 DnsResourceRecord *rr, *soa = NULL;
377 DnsAnswerFlags rr_flags, soa_flags = 0;
378 int r;
379
380 assert(key);
381
382 /* For a SOA record we can never find a matching SOA record */
383 if (key->type == DNS_TYPE_SOA)
384 goto not_found;
385
386 DNS_ANSWER_FOREACH_FLAGS(rr, rr_flags, a) {
387 r = dns_resource_key_match_soa(key, rr->key);
388 if (r < 0)
389 return r;
390 if (r > 0) {
391
392 if (soa) {
393 r = dns_name_endswith(dns_resource_key_name(rr->key), dns_resource_key_name(soa->key));
394 if (r < 0)
395 return r;
396 if (r > 0)
397 continue;
398 }
399
400 soa = rr;
401 soa_flags = rr_flags;
402 }
403 }
404
405 if (!soa)
406 goto not_found;
407
408 if (ret)
409 *ret = soa;
410 if (ret_flags)
411 *ret_flags = soa_flags;
412
413 return 1;
414
415 not_found:
416 if (ret)
417 *ret = NULL;
418 if (ret_flags)
419 *ret_flags = 0;
420
421 return 0;
422 }
423
424 int dns_answer_find_cname_or_dname(
425 DnsAnswer *a,
426 const DnsResourceKey *key,
427 DnsResourceRecord **ret,
428 DnsAnswerFlags *ret_flags) {
429
430 DnsResourceRecord *rr;
431 DnsAnswerFlags rr_flags;
432 int r;
433
434 assert(key);
435
436 /* For a {C,D}NAME record we can never find a matching {C,D}NAME record */
437 if (!dns_type_may_redirect(key->type))
438 return 0;
439
440 DNS_ANSWER_FOREACH_FLAGS(rr, rr_flags, a) {
441 r = dns_resource_key_match_cname_or_dname(key, rr->key, NULL);
442 if (r < 0)
443 return r;
444 if (r > 0) {
445 if (ret)
446 *ret = rr;
447 if (ret_flags)
448 *ret_flags = rr_flags;
449 return 1;
450 }
451 }
452
453 if (ret)
454 *ret = NULL;
455 if (ret_flags)
456 *ret_flags = 0;
457
458 return 0;
459 }
460
461 int dns_answer_merge(DnsAnswer *a, DnsAnswer *b, DnsAnswer **ret) {
462 _cleanup_(dns_answer_unrefp) DnsAnswer *k = NULL;
463 int r;
464
465 assert(ret);
466
467 if (a == b) {
468 *ret = dns_answer_ref(a);
469 return 0;
470 }
471
472 if (dns_answer_size(a) <= 0) {
473 *ret = dns_answer_ref(b);
474 return 0;
475 }
476
477 if (dns_answer_size(b) <= 0) {
478 *ret = dns_answer_ref(a);
479 return 0;
480 }
481
482 k = dns_answer_new(dns_answer_size(a) + dns_answer_size(b));
483 if (!k)
484 return -ENOMEM;
485
486 r = dns_answer_add_raw_all(k, a);
487 if (r < 0)
488 return r;
489
490 r = dns_answer_add_all(k, b);
491 if (r < 0)
492 return r;
493
494 *ret = TAKE_PTR(k);
495
496 return 0;
497 }
498
499 int dns_answer_extend(DnsAnswer **a, DnsAnswer *b) {
500 DnsAnswer *merged;
501 int r;
502
503 assert(a);
504
505 r = dns_answer_merge(*a, b, &merged);
506 if (r < 0)
507 return r;
508
509 DNS_ANSWER_REPLACE(*a, merged);
510 return 0;
511 }
512
513 int dns_answer_remove_by_key(DnsAnswer **a, const DnsResourceKey *key) {
514 DnsAnswerItem *item;
515 bool found = false;
516 int r;
517
518 assert(a);
519 assert(key);
520
521 /* Remove all entries matching the specified key from *a */
522
523 DNS_ANSWER_FOREACH_ITEM(item, *a) {
524 r = dns_resource_key_equal(item->rr->key, key);
525 if (r < 0)
526 return r;
527 if (r > 0) {
528 dns_answer_item_unref(ordered_set_remove((*a)->items, item));
529 found = true;
530 }
531 }
532
533 if (!found)
534 return 0;
535
536 if (dns_answer_isempty(*a))
537 *a = dns_answer_unref(*a); /* Return NULL for the empty answer */
538
539 return 1;
540 }
541
542 int dns_answer_remove_by_rr(DnsAnswer **a, DnsResourceRecord *rr) {
543 _unused_ _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *rr_ref = dns_resource_record_ref(rr);
544 DnsAnswerItem *item;
545 bool found = false;
546 int r;
547
548 assert(a);
549 assert(rr);
550
551 /* Remove all entries matching the specified RR from *a */
552
553 DNS_ANSWER_FOREACH_ITEM(item, *a) {
554 r = dns_resource_record_equal(item->rr, rr);
555 if (r < 0)
556 return r;
557 if (r > 0) {
558 dns_answer_item_unref(ordered_set_remove((*a)->items, item));
559 found = true;
560 }
561 }
562
563 if (!found)
564 return 0;
565
566 if (dns_answer_isempty(*a))
567 *a = dns_answer_unref(*a); /* Return NULL for the empty answer */
568
569 return 1;
570 }
571
572 int dns_answer_remove_by_answer_keys(DnsAnswer **a, DnsAnswer *b) {
573 _cleanup_(dns_resource_key_unrefp) DnsResourceKey *prev = NULL;
574 DnsAnswerItem *item;
575 int r;
576
577 assert(a);
578
579 /* Removes all items from '*a' that have a matching key in 'b' */
580
581 DNS_ANSWER_FOREACH_ITEM(item, b) {
582
583 if (prev && dns_resource_key_equal(item->rr->key, prev)) /* Skip this one, we already looked at it */
584 continue;
585
586 r = dns_answer_remove_by_key(a, item->rr->key);
587 if (r < 0)
588 return r;
589 if (!*a)
590 return 0; /* a is already empty. */
591
592 /* Let's remember this entry's RR key, to optimize the loop a bit: if we have an RRset with
593 * more than one item then we don't need to remove the key multiple times */
594 DNS_RESOURCE_KEY_REPLACE(prev, dns_resource_key_ref(item->rr->key));
595 }
596
597 return 0;
598 }
599
600 int dns_answer_copy_by_key(
601 DnsAnswer **a,
602 DnsAnswer *source,
603 const DnsResourceKey *key,
604 DnsAnswerFlags or_flags,
605 DnsResourceRecord *rrsig) {
606
607 DnsAnswerItem *item;
608 int r;
609
610 assert(a);
611 assert(key);
612
613 /* Copy all RRs matching the specified key from source into *a */
614
615 DNS_ANSWER_FOREACH_ITEM(item, source) {
616
617 r = dns_resource_key_equal(item->rr->key, key);
618 if (r < 0)
619 return r;
620 if (r == 0)
621 continue;
622
623 r = dns_answer_add_extend(a, item->rr, item->ifindex, item->flags|or_flags, rrsig ?: item->rrsig);
624 if (r < 0)
625 return r;
626 }
627
628 return 0;
629 }
630
631 int dns_answer_move_by_key(
632 DnsAnswer **to,
633 DnsAnswer **from,
634 const DnsResourceKey *key,
635 DnsAnswerFlags or_flags,
636 DnsResourceRecord *rrsig) {
637
638 int r;
639
640 assert(to);
641 assert(from);
642 assert(key);
643
644 r = dns_answer_copy_by_key(to, *from, key, or_flags, rrsig);
645 if (r < 0)
646 return r;
647
648 return dns_answer_remove_by_key(from, key);
649 }
650
651 void dns_answer_order_by_scope(DnsAnswer *a, bool prefer_link_local) {
652 _cleanup_free_ DnsAnswerItem **items = NULL;
653 DnsAnswerItem **p, *item;
654 size_t n;
655
656 n = dns_answer_size(a);
657 if (n <= 1)
658 return;
659
660 /* RFC 4795, Section 2.6 suggests we should order entries
661 * depending on whether the sender is a link-local address. */
662
663 p = items = new(DnsAnswerItem*, n);
664 if (!items)
665 return (void) log_oom();
666
667 /* Order preferred address records and other records to the beginning of the array */
668 DNS_ANSWER_FOREACH_ITEM(item, a)
669 if (dns_resource_record_is_link_local_address(item->rr) == prefer_link_local)
670 *p++ = dns_answer_item_ref(item);
671
672 /* Order address records that are not preferred to the end of the array */
673 DNS_ANSWER_FOREACH_ITEM(item, a)
674 if (dns_resource_record_is_link_local_address(item->rr) != prefer_link_local)
675 *p++ = dns_answer_item_ref(item);
676
677 assert((size_t) (p - items) == n);
678
679 ordered_set_clear(a->items);
680 for (size_t i = 0; i < n; i++)
681 assert_se(ordered_set_put(a->items, items[i]) >= 0);
682 }
683
684 int dns_answer_reserve(DnsAnswer **a, size_t n_free) {
685 assert(a);
686
687 if (n_free <= 0)
688 return 0;
689
690 if (!*a) {
691 DnsAnswer *n;
692
693 n = dns_answer_new(n_free);
694 if (!n)
695 return -ENOMEM;
696
697 *a = n;
698 return 0;
699 }
700
701 if ((*a)->n_ref > 1)
702 return -EBUSY;
703
704 return dns_answer_reserve_internal(*a, n_free);
705 }
706
707 int dns_answer_reserve_or_clone(DnsAnswer **a, size_t n_free) {
708 _cleanup_(dns_answer_unrefp) DnsAnswer *n = NULL;
709 size_t ns;
710 int r;
711
712 assert(a);
713
714 r = dns_answer_reserve(a, n_free);
715 if (r != -EBUSY)
716 return r;
717
718 ns = dns_answer_size(*a);
719 assert(ns <= UINT16_MAX); /* Maximum number of RRs we can stick into a DNS packet section */
720
721 ns = saturate_add(ns, n_free, UINT16_MAX);
722
723 n = dns_answer_new(ns);
724 if (!n)
725 return -ENOMEM;
726
727 r = dns_answer_add_raw_all(n, *a);
728 if (r < 0)
729 return r;
730
731 DNS_ANSWER_REPLACE(*a, TAKE_PTR(n));
732 return 0;
733 }
734
735 /*
736 * This function is not used in the code base, but is useful when debugging. Do not delete.
737 */
738 void dns_answer_dump(DnsAnswer *answer, FILE *f) {
739 DnsAnswerItem *item;
740
741 if (!f)
742 f = stdout;
743
744 DNS_ANSWER_FOREACH_ITEM(item, answer) {
745 const char *t;
746
747 fputc('\t', f);
748
749 t = dns_resource_record_to_string(item->rr);
750 if (!t) {
751 log_oom();
752 continue;
753 }
754
755 fputs(t, f);
756 fputs("\t;", f);
757 fprintf(f, " ttl=%" PRIu32, item->rr->ttl);
758
759 if (item->ifindex != 0)
760 fprintf(f, " ifindex=%i", item->ifindex);
761 if (item->rrsig)
762 fputs(" rrsig", f);
763 if (item->flags & DNS_ANSWER_AUTHENTICATED)
764 fputs(" authenticated", f);
765 if (item->flags & DNS_ANSWER_CACHEABLE)
766 fputs(" cacheable", f);
767 if (item->flags & DNS_ANSWER_SHARED_OWNER)
768 fputs(" shared-owner", f);
769 if (item->flags & DNS_ANSWER_CACHE_FLUSH)
770 fputs(" cache-flush", f);
771 if (item->flags & DNS_ANSWER_GOODBYE)
772 fputs(" goodbye", f);
773 if (item->flags & DNS_ANSWER_SECTION_ANSWER)
774 fputs(" section-answer", f);
775 if (item->flags & DNS_ANSWER_SECTION_AUTHORITY)
776 fputs(" section-authority", f);
777 if (item->flags & DNS_ANSWER_SECTION_ADDITIONAL)
778 fputs(" section-additional", f);
779
780 fputc('\n', f);
781 }
782 }
783
784 int dns_answer_has_dname_for_cname(DnsAnswer *a, DnsResourceRecord *cname) {
785 DnsResourceRecord *rr;
786 int r;
787
788 assert(cname);
789
790 /* Checks whether the answer contains a DNAME record that indicates that the specified CNAME record is
791 * synthesized from it */
792
793 if (cname->key->type != DNS_TYPE_CNAME)
794 return 0;
795
796 DNS_ANSWER_FOREACH(rr, a) {
797 _cleanup_free_ char *n = NULL;
798
799 if (rr->key->type != DNS_TYPE_DNAME)
800 continue;
801 if (rr->key->class != cname->key->class)
802 continue;
803
804 r = dns_name_change_suffix(cname->cname.name, rr->dname.name, dns_resource_key_name(rr->key), &n);
805 if (r < 0)
806 return r;
807 if (r == 0)
808 continue;
809
810 r = dns_name_equal(n, dns_resource_key_name(cname->key));
811 if (r < 0)
812 return r;
813 if (r > 0)
814 return 1;
815 }
816
817 return 0;
818 }
819
820 void dns_answer_randomize(DnsAnswer *a) {
821 _cleanup_free_ DnsAnswerItem **items = NULL;
822 DnsAnswerItem **p, *item;
823 size_t n;
824
825 /* Permutes the answer list randomly (Knuth shuffle) */
826
827 n = dns_answer_size(a);
828 if (n <= 1)
829 return;
830
831 p = items = new(DnsAnswerItem*, n);
832 if (!items)
833 return (void) log_oom();
834
835 DNS_ANSWER_FOREACH_ITEM(item, a)
836 *p++ = dns_answer_item_ref(item);
837
838 assert((size_t) (p - items) == n);
839
840 for (size_t i = 0; i < n; i++) {
841 size_t k;
842
843 k = random_u64_range(n);
844 if (k == i)
845 continue;
846
847 SWAP_TWO(items[i], items[k]);
848 }
849
850 ordered_set_clear(a->items);
851 for (size_t i = 0; i < n; i++)
852 assert_se(ordered_set_put(a->items, items[i]) >= 0);
853 }
854
855 uint32_t dns_answer_min_ttl(DnsAnswer *a) {
856 uint32_t ttl = UINT32_MAX;
857 DnsResourceRecord *rr;
858
859 /* Return the smallest TTL of all RRs in this answer */
860
861 DNS_ANSWER_FOREACH(rr, a) {
862 /* Don't consider OPT (where the TTL field is used for other purposes than an actual TTL) */
863
864 if (dns_type_is_pseudo(rr->key->type) ||
865 dns_class_is_pseudo(rr->key->class))
866 continue;
867
868 ttl = MIN(ttl, rr->ttl);
869 }
870
871 return ttl;
872 }
873
874 int dns_answer_to_json(DnsAnswer *answer, sd_json_variant **ret) {
875 int r;
876
877 assert(ret);
878
879 _cleanup_(sd_json_variant_unrefp) sd_json_variant *ja = NULL;
880 DnsResourceRecord *rr;
881 DNS_ANSWER_FOREACH(rr, answer) {
882 _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL;
883
884 r = dns_resource_record_to_json(rr, &v);
885 if (r < 0)
886 return r;
887
888 r = dns_resource_record_to_wire_format(rr, /* canonical= */ false);
889 if (r < 0)
890 return r;
891
892 r = sd_json_variant_append_arraybo(
893 &ja,
894 SD_JSON_BUILD_PAIR_VARIANT("rr", v),
895 SD_JSON_BUILD_PAIR_BASE64("raw", rr->wire_format, rr->wire_format_size));
896 if (r < 0)
897 return r;
898 }
899
900 if (!ja)
901 return sd_json_variant_new_array(ret, /* array= */ NULL, /* n= */ 0);
902
903 *ret = TAKE_PTR(ja);
904 return 0;
905 }