5#include <arpa/nameser.h>
24static stralloc glue = {0};
25static stralloc ip = {0};
27static int dns_ipplus(ipalloc *
ia,stralloc *
sa,
int pref)
37 if (ip4_scanbracket(
sa->s,
ip4)) {
39 if (!stralloc_0(&glue))
return DNS_MEM;
43 byte_copy(&ix.addr,4,
ip4);
51 if (ip6_scanbracket(
sa->s,
ip6)) {
53 if (!stralloc_0(&glue))
return DNS_MEM;
57 byte_copy(&ix.addr,16,
ip6);
65 if (dns_ip6(&ip,
sa) > 0) {
66 for (i = 0; i + 16 <= ip.len; i += 16) {
67 if (ip6_isv4mapped(ip.s + i))
continue;
70 byte_copy(&ix.addr,16,ip.s + i);
71 str_copy(ix.mxh,
sa->s);
80 if (dns_ip4(&ip,
sa) > 0) {
81 for (i = 0; i + 4 <= ip.len; i += 4) {
84 byte_copy(&ix.addr,4,ip.s + i);
85 str_copy(ix.mxh,
sa->s);
97 if (!ipalloc_readyplus(
ia,0))
return DNS_MEM;
100 return dns_ipplus(
ia,
sa,0);
106 struct mx { stralloc
sa;
unsigned short p; } *mx;
117 if (!ipalloc_readyplus(
ia,0))
return DNS_MEM;
121 if (!stralloc_0(&glue))
return DNS_MEM;
124 if (!glue.s[ip4_scan(glue.s,(
char *)&ix.
addr.
ip4)] || \
125 !glue.s[ip4_scanbracket(glue.s,(
char *)&ix.
addr.
ip4)]) {
130 if (!glue.s[ip6_scan(glue.s,(
char *)&ix.
addr.
ip6)] || \
131 !glue.s[ip6_scanbracket(glue.s,(
char *)&ix.
addr.
ip6)]) {
144 mx = (
struct mx *) alloc(i *
sizeof(
struct mx));
145 if (!mx)
return DNS_MEM;
149 j = str_chr(ip.s + len + 2,
'\0');
152 alloc_free(mx);
return DNS_MEM;
154 ip.s[len + 3] =
'\0';
155 uint16_unpack_big(ip.s + len,&pref);
159 }
while (len < ip.len);
166 unsigned long numsame;
169 for (
j = 1;
j < nummx; ++
j) {
170 if (mx[
j].
p < mx[i].
p) {
174 else if (mx[
j].
p == mx[i].
p) {
176 random = random * 69069 + 1;
177 if ((random / 2) < (2147483647 / numsame)) i =
j;
181 switch (dns_ipplus(
ia,&mx[i].
sa,mx[i].
p)) {
182 case -1:
return DNS_MEM;
183 case -2:
case -3: flagsoft = -5;
break;
186 alloc_free(mx[i].
sa.s);
197 if (!ipalloc_readyplus(
ia,0))
return DNS_MEM;
200 return dns_ipplus(
ia,
sa,0);
int stralloc_copys(stralloc *, char const *)
int dns_mxip(ipalloc *ia, stralloc *sa, unsigned long random)
int dns_ipalloc(ipalloc *ia, stralloc *sa)
int dns_ip(ipalloc *ia, stralloc *sa)
void p(char *, char *, int, int, int)