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
|
#include <stdlib.h>
#include <string.h>
#include "ibuf.h"
#include "misc.h"
#include "striter.h"
#include "dns.h"
static str data = {0};
extern ipv4port dns_use_port;
/** Read the \c /etc/resolv.conf file.
If \c $DNSRESOLVCONF is set, it names a file to read instead of \c /etc/resolv.conf . */
int dns_read_resolvconf(str *out)
{
const char *x;
x = getenv("DNSRESOLVCONF");
if (!x)
x = "/etc/resolv.conf";
return ibuf_openreadclose(x, out);
}
static int init(ipv4addr ip[DNS_MAX_IPS])
{
int i;
striter j;
int iplen = 0;
const char *x;
x = getenv("DNSCACHEPORT");
if (x && *x) {
ipv4port p = strtou(x, &x);
if (*x)
dns_use_port = p;
}
x = getenv("DNSCACHEIP");
if (x)
while (iplen <= 15) {
if (*x == ',')
++x;
else {
x = ipv4_scan(x,ip + iplen);
if (!x) break;
iplen++;
}
}
if (!iplen) {
i = dns_read_resolvconf(&data);
if (i == -1) return -1;
if (i) {
if (!str_catc(&data,'\n')) return -1;
striter_loop(&j, &data, '\n') {
if (memcmp("nameserver ", j.startptr, 11) == 0 || memcmp("nameserver\t", j.startptr, 11) == 0) {
for (i = j.start + 10; data.s[i] == ' ' || data.s[i] == '\t'; ++i)
;
if (iplen <= 15)
if (ipv4_scan(data.s + i,ip + iplen)) {
if (memcmp(ip + iplen,"\0\0\0\0",4) == 0)
memcpy(ip + iplen,"\177\0\0\1",4);
iplen++;
}
}
}
}
}
if (!iplen) {
memcpy(ip,"\177\0\0\1",4);
iplen = 4;
}
memset(ip + iplen,0,64 - iplen);
return 0;
}
static int ok = 0;
static unsigned int uses;
static struct timeval deadline;
static ipv4addr ip[DNS_MAX_IPS]; /* defined if ok */
/** Parse \c /etc/resolv.conf for a list of nameserver IPs. */
int dns_resolvconfip(ipv4addr s[DNS_MAX_IPS])
{
struct timeval now;
gettimeofday(&now,0);
if (TV_LESS(&deadline,&now)) ok = 0;
if (!uses) ok = 0;
if (!ok) {
if (init(ip) == -1) return -1;
deadline = now;
deadline.tv_sec += 600;
uses = 10000;
ok = 1;
}
--uses;
memcpy(s,ip,64);
return 0;
}
#ifdef SELFTEST_MAIN
MAIN
{
ipv4addr ips[DNS_MAX_IPS];
int i;
str rcfile = {0};
makefile(&rcfile, "nameserver 1.2.3.4\nnameserver\t 2.3.4.5\nnameserver \t3.4.5.6");
debugfn(setenv("DNSRESOLVCONF", rcfile.s, 1));
debugfn(dns_resolvconfip(ips));
for (i = 0; i < DNS_MAX_IPS; ++i) {
obuf_puts(&outbuf, ipv4_format(&ips[i]));
NL();
}
uses = 0;
debugfn(setenv("DNSCACHEIP", "192.168.1.2,192.168.1.3", 1));
debugfn(dns_resolvconfip(ips));
for (i = 0; i < DNS_MAX_IPS; ++i) {
obuf_puts(&outbuf, ipv4_format(&ips[i]));
NL();
}
unlink(rcfile.s);
}
#endif
#ifdef SELFTEST_EXP
result=0
result=0
1.2.3.4
2.3.4.5
3.4.5.6
0.0.0.0
0.0.0.0
0.0.0.0
0.0.0.0
0.0.0.0
0.0.0.0
0.0.0.0
0.0.0.0
0.0.0.0
0.0.0.0
0.0.0.0
0.0.0.0
0.0.0.0
result=0
result=0
192.168.1.2
192.168.1.3
0.0.0.0
0.0.0.0
0.0.0.0
0.0.0.0
0.0.0.0
0.0.0.0
0.0.0.0
0.0.0.0
0.0.0.0
0.0.0.0
0.0.0.0
0.0.0.0
0.0.0.0
0.0.0.0
#endif
|