answer: add helper for randomizing RR of answers
This commit is contained in:
@@ -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]);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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) \
|
||||
|
||||
Reference in New Issue
Block a user