answer: add helper for randomizing RR of answers

This commit is contained in:
Lennart Poettering
2021-02-14 15:42:56 +01:00
parent 5464c96186
commit 5b2d8ffb5a
2 changed files with 23 additions and 0 deletions
+21
View File
@@ -4,6 +4,7 @@
#include "alloc-util.h"
#include "dns-domain.h"
#include "random-util.h"
#include "resolved-dns-answer.h"
#include "resolved-dns-dnssec.h"
#include "string-util.h"
@@ -898,3 +899,23 @@ int dns_answer_has_dname_for_cname(DnsAnswer *a, DnsResourceRecord *cname) {
return 0;
}
void dns_answer_randomize(DnsAnswer *a) {
size_t n;
/* Permutes the answer list randomly (Knuth shuffle) */
n = dns_answer_size(a);
if (n <= 1)
return;
for (size_t i = 0; i < n; i++) {
size_t k;
k = random_u64_range(n);
if (k == i)
continue;
SWAP_TWO(a->items[i], a->items[k]);
}
}
+2
View File
@@ -80,6 +80,8 @@ static inline bool dns_answer_isempty(DnsAnswer *a) {
void dns_answer_dump(DnsAnswer *answer, FILE *f);
void dns_answer_randomize(DnsAnswer *a);
DEFINE_TRIVIAL_CLEANUP_FUNC(DnsAnswer*, dns_answer_unref);
#define _DNS_ANSWER_FOREACH(q, kk, a) \