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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
|
#include <netdb.h>
#include <string.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/nameser.h>
#include <sys/socket.h>
#include "ip.h"
#include "ipalloc.h"
#include "fmt.h"
#include "alloc.h"
#include "str.h"
#include "stralloc.h"
#include "dnsresolv.h"
#include "case.h"
#include "dns.h"
#include "buffer.h"
#include "exit.h"
/**
@file dns.c
@brief DNS helpers: dns_ipplus, dns_ipalloc, dns_ip (IPv4+IPv6), dns_mxip
*/
static stralloc glue = {0};
static stralloc ip = {0};
static int dns_ipplus(ipalloc *ia,stralloc *sa,int pref)
{
struct ip_mx ix;
int error = 0;
char ip4[4];
char ip6[16];
int i;
/* Case 1: sa is just IPv4 */
if (ip4_scanbracket(sa->s,ip4)) {
if (!stralloc_copys(&glue,sa->s)) return DNS_MEM;
if (!stralloc_0(&glue)) return DNS_MEM;
if (glue.s[0]) {
ix.pref = 0;
ix.af = AF_INET;
byte_copy(&ix.addr,4,ip4); // = ip; //cp
if (!ipalloc_append(ia,&ix)) return DNS_MEM;
return 0;
}
}
/* Case 2: sa is just IPv6 */
if (ip6_scanbracket(sa->s,ip6)) {
if (!stralloc_copys(&glue,sa->s)) return DNS_MEM;
if (!stralloc_0(&glue)) return DNS_MEM;
if (glue.s[0]) {
ix.pref = 0;
ix.af = AF_INET6;
byte_copy(&ix.addr,16,ip6); // = ip; //cp
if (!ipalloc_append(ia,&ix)) return DNS_MEM;
return 0;
}
}
/* Case 3: sa is fqdn and looking for IPv6 */
if (dns_ip6(&ip,sa) > 0) {
for (i = 0; i + 16 <= ip.len; i += 16) {
if (ip6_isv4mapped(ip.s + i)) continue;
ix.af = AF_INET6;
ix.pref = pref;
byte_copy(&ix.addr,16,ip.s + i); // = ip; //cp
str_copy(ix.mxh,sa->s); // mx hostname
if (!ipalloc_append(ia,&ix)) { error = DNS_MEM; break; }
error = 0;
}
} else
error = 1;
/* Case 4: sa is fqdn and looking for IPv4 */
if (dns_ip4(&ip,sa) > 0) {
for (i = 0; i + 4 <= ip.len; i += 4) {
ix.af = AF_INET;
ix.pref = pref;
byte_copy(&ix.addr,4,ip.s + i); // = ip; //cp
str_copy(ix.mxh,sa->s); // mx hostname
if (!ipalloc_append(ia,&ix)) { error = DNS_MEM; break; }
error = 0;
}
} else
error += 2;
return error;
}
int dns_ipalloc(ipalloc *ia,stralloc *sa)
{
if (!ipalloc_readyplus(ia,0)) return DNS_MEM;
ia->len = 0;
return dns_ipplus(ia,sa,0);
}
/* dns_mxip */
int dns_mxip(ipalloc *ia,stralloc *sa,unsigned long random) {
struct mx { stralloc sa; unsigned short p; } *mx;
struct ip_mx ix;
int nummx;
int i;
int j = 0;
int len;
int flagsoft;
uint16 pref;
/* Case 1: sa is just IPv4 or IPv6 */
if (!ipalloc_readyplus(ia,0)) return DNS_MEM;
ia->len = 0;
if (!stralloc_copys(&glue,sa->s)) return DNS_MEM;
if (!stralloc_0(&glue)) return DNS_MEM;
if (glue.s[0]) {
ix.pref = 0;
if (!glue.s[ip4_scan(glue.s,(char *)&ix.addr.ip4)] || \
!glue.s[ip4_scanbracket(glue.s,(char *)&ix.addr.ip4)]) {
ix.af = AF_INET;
if (!ipalloc_append(ia,&ix)) return DNS_MEM;
return 0;
}
if (!glue.s[ip6_scan(glue.s,(char *)&ix.addr.ip6)] || \
!glue.s[ip6_scanbracket(glue.s,(char *)&ix.addr.ip6)]) {
ix.af = AF_INET6;
if (!ipalloc_append(ia,&ix)) return DNS_MEM;
return 0;
}
}
/* Case 2: sa is FQDN and do a mx lookup */
DNS_INIT
nummx = 0;
len = 0;
i = dns_mx(&ip,sa);
mx = (struct mx *) alloc(i * sizeof(struct mx));
if (!mx) return DNS_MEM;
if (i) {
do {
j = str_chr(ip.s + len + 2,'\0'); /* several answers */
mx[nummx].sa.s = 0;
if (!stralloc_copys(&mx[nummx].sa,ip.s + len + 2)) { /* mxhost name */
alloc_free(mx); return DNS_MEM;
}
ip.s[len + 3] = '\0';
uint16_unpack_big(ip.s + len,&pref);
mx[nummx].p = pref;
len += j + 3;
++nummx;
} while (len < ip.len);
}
if (!nummx) return dns_ipalloc(ia,sa); /* e.g., CNAME -> A */
flagsoft = 0;
while (nummx > 0) {
unsigned long numsame;
i = 0;
numsame = 1;
for (j = 1; j < nummx; ++j) {
if (mx[j].p < mx[i].p) {
i = j;
numsame = 1;
}
else if (mx[j].p == mx[i].p) {
++numsame;
random = random * 69069 + 1;
if ((random / 2) < (2147483647 / numsame)) i = j;
}
}
switch (dns_ipplus(ia,&mx[i].sa,mx[i].p)) {
case -1: return DNS_MEM;
case -2: case -3: flagsoft = -5; break;
}
alloc_free(mx[i].sa.s);
mx[i] = mx[--nummx];
}
alloc_free(mx);
return flagsoft;
}
int dns_ip(ipalloc *ia,stralloc *sa)
{
if (!ipalloc_readyplus(ia,0)) return DNS_MEM;
ia->len = 0;
return dns_ipplus(ia,sa,0);
}
|