summaryrefslogtreecommitdiff
path: root/src/dnsstub/dns_sortip.c
blob: f319f53c9e8472f108b054f41851d653bc10d2eb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include "byte.h"
#include "dnsresolv.h"
#include "ip.h"

/**
  @file dns_sortip.c
  @authors djb, fefe, feh
  @source ucspi-tcp6
  @brief random sort of DNS servers per IP
*/

/* XXX: sort servers by configurable notion of closeness? */
/* XXX: pay attention to competence of each server? */
/* XXX: pay attention to qualification (DNSSec, DNSCurve) of each server? */
/* YYY: we use a randomly sorted list of NS; not depending on answer */

void dns_sortip4(char *s, unsigned int n)
{
  unsigned int i;
  char tmp[4];

  n >>= 2;  // 4 byte per IPv4 address
  while (n > 1) {
    i = dns_random(n);
    --n;
    byte_copy(tmp, 4, s + (i << 2));
    byte_copy(s + (i << 2), 4, s + (n << 2));
    byte_copy(s + (n << 2), 4, tmp);
  }
}

void dns_sortip6(char *s, unsigned int n)
{
  unsigned int i;
  char tmp[16];

  n >>= 4;  // 16 byte per IPv4 address
  while (n > 1) {
    i = dns_random(n);
    --n;
    byte_copy(tmp, 16, s + (i << 4));
    byte_copy(s + (i << 4), 16, s + (n << 4));
    byte_copy(s + (n << 4), 16, tmp);
  }
}