9static stralloc
data = {0};
11static int init(stralloc *rules)
19 if (!stralloc_copys(rules,
""))
return DNS_MEM;
21 x = env_get(
"DNSREWRITEFILE");
22 if (!
x)
x =
"/etc/dnsrewrite";
24 i = openreadclose(
x,&data,64);
28 if (!stralloc_append(&data,
"\n"))
return DNS_MEM;
30 for (j = 0; j <
data.len; ++j)
31 if (
data.s[j] ==
'\n') {
32 if (!stralloc_catb(rules,
data.s + i,j - i))
return DNS_MEM;
34 if (rules->s[rules->len - 1] !=
' ')
35 if (rules->s[rules->len - 1] !=
'\t')
36 if (rules->s[rules->len - 1] !=
'\r')
40 if (!stralloc_0(rules))
return DNS_MEM;
46 x = env_get(
"LOCALDOMAIN");
48 if (!stralloc_copys(&data,
x))
return DNS_MEM;
49 if (!stralloc_append(&data,
" "))
return DNS_MEM;
50 if (!stralloc_copys(rules,
"?:"))
return DNS_MEM;
52 for (j = 0; j <
data.len; ++j)
53 if (
data.s[j] ==
' ') {
54 if (!stralloc_cats(rules,
"+."))
return DNS_MEM;
55 if (!stralloc_catb(rules,
data.s + i,j - i))
return DNS_MEM;
58 if (!stralloc_0(rules))
return DNS_MEM;
59 if (!stralloc_cats(rules,
"*.:"))
return DNS_MEM;
60 if (!stralloc_0(rules))
return DNS_MEM;
64 i = openreadclose(
"/etc/resolv.conf",&data,64);
68 if (!stralloc_append(&data,
"\n"))
return DNS_MEM;
70 for (j = 0;j <
data.len;++j)
71 if (
data.s[j] ==
'\n') {
72 if (byte_equal(
"search ",7,
data.s + i) ||
73 byte_equal(
"search\t",7,
data.s + i) ||
74 byte_equal(
"domain ",7,
data.s + i) ||
75 byte_equal(
"domain\t",7,
data.s + i)) {
76 if (!stralloc_copys(rules,
"?:"))
return DNS_MEM;
79 k = byte_chr(
data.s + i,j - i,
' ');
80 k = byte_chr(
data.s + i,k,
'\t');
81 if (!k) { ++i;
continue; }
82 if (!stralloc_cats(rules,
"+."))
return DNS_MEM;
83 if (!stralloc_catb(rules,
data.s + i,k))
return DNS_MEM;
86 if (!stralloc_0(rules))
return DNS_MEM;
87 if (!stralloc_cats(rules,
"*.:"))
return DNS_MEM;
88 if (!stralloc_0(rules))
return DNS_MEM;
96 if (gethostname(host,
sizeof(host)) == -1)
return DNS_ERR;
97 host[(
sizeof(host)) - 1] = 0;
98 i = str_chr(host,
'.');
100 if (!stralloc_copys(rules,
"?:"))
return DNS_MEM;
101 if (!stralloc_cats(rules,host + i))
return DNS_MEM;
102 if (!stralloc_0(rules))
return DNS_MEM;
104 if (!stralloc_cats(rules,
"*.:"))
return DNS_MEM;
105 if (!stralloc_0(rules))
return DNS_MEM;
111static unsigned int uses;
112static struct taia deadline;
113static stralloc rules = {0};
120 if (taia_less(&deadline,&
now)) ok = 0;
124 if (init(&rules) < 0)
return DNS_INT;
125 taia_uint(&deadline,600);
126 taia_add(&deadline,&
now,&deadline);
132 if (!stralloc_copy(
out,&rules))
return DNS_MEM;
int dns_resolvconfrewrite(stralloc *out)
void out(const char *s, unsigned int len)