fehQlibs 26
Qlibs
Loading...
Searching...
No Matches
dns_rcrw.c
Go to the documentation of this file.
1#include <unistd.h>
2#include "taia.h"
3#include "env.h"
4#include "byte.h"
5#include "str.h"
6#include "readclose.h"
7#include "dnsresolv.h"
8
16static stralloc data = {0};
17
18static int init(stralloc *rules)
19{
20 char host[256];
21 const char *x;
22 int i;
23 int j;
24 int k;
25
26 if (!stralloc_copys(rules,"")) return DNS_MEM;
27
28 x = env_get("DNSREWRITEFILE");
29 if (!x) x = "/etc/dnsrewrite";
30
31 i = openreadclose(x,&data,64);
32 if (i == -1) return DNS_INT;
33
34 if (i) {
35 if (!stralloc_append(&data,"\n")) return DNS_MEM;
36 i = 0;
37 for (j = 0; j < data.len; ++j)
38 if (data.s[j] == '\n') {
39 if (!stralloc_catb(rules,data.s + i,j - i)) return DNS_MEM;
40 while (rules->len) {
41 if (rules->s[rules->len - 1] != ' ')
42 if (rules->s[rules->len - 1] != '\t')
43 if (rules->s[rules->len - 1] != '\r')
44 break;
45 --rules->len;
46 }
47 if (!stralloc_0(rules)) return DNS_MEM;
48 i = j + 1;
49 }
50 return 0;
51 }
52
53 x = env_get("LOCALDOMAIN");
54 if (x) {
55 if (!stralloc_copys(&data,x)) return DNS_MEM;
56 if (!stralloc_append(&data," ")) return DNS_MEM;
57 if (!stralloc_copys(rules,"?:")) return DNS_MEM;
58 i = 0;
59 for (j = 0; j < data.len; ++j)
60 if (data.s[j] == ' ') {
61 if (!stralloc_cats(rules,"+.")) return DNS_MEM;
62 if (!stralloc_catb(rules,data.s + i,j - i)) return DNS_MEM;
63 i = j + 1;
64 }
65 if (!stralloc_0(rules)) return DNS_MEM;
66 if (!stralloc_cats(rules,"*.:")) return DNS_MEM;
67 if (!stralloc_0(rules)) return DNS_MEM;
68 return 0;
69 }
70
71 i = openreadclose("/etc/resolv.conf",&data,64);
72 if (i == -1) return DNS_INT;
73
74 if (i) {
75 if (!stralloc_append(&data,"\n")) return DNS_MEM;
76 i = 0;
77 for (j = 0; j < data.len; ++j)
78 if (data.s[j] == '\n') {
79 if (byte_equal("search ",7,data.s + i) ||
80 byte_equal("search\t",7,data.s + i) ||
81 byte_equal("domain ",7,data.s + i) ||
82 byte_equal("domain\t",7,data.s + i)) {
83 if (!stralloc_copys(rules,"?:")) return DNS_MEM;
84 i += 7;
85 while (i < j) {
86 k = byte_chr(data.s + i,j - i,' ');
87 k = byte_chr(data.s + i,k,'\t');
88 if (!k) { ++i; continue; }
89 if (!stralloc_cats(rules,"+.")) return DNS_MEM;
90 if (!stralloc_catb(rules,data.s + i,k)) return DNS_MEM;
91 i += k;
92 }
93 if (!stralloc_0(rules)) return DNS_MEM;
94 if (!stralloc_cats(rules,"*.:")) return DNS_MEM;
95 if (!stralloc_0(rules)) return DNS_MEM;
96 return 0;
97 }
98 i = j + 1;
99 }
100 }
101
102 host[0] = 0;
103 if (gethostname(host,sizeof(host)) == -1) return DNS_ERR;
104 host[(sizeof(host)) - 1] = 0;
105 i = str_chr(host,'.');
106 if (host[i]) {
107 if (!stralloc_copys(rules,"?:")) return DNS_MEM;
108 if (!stralloc_cats(rules,host + i)) return DNS_MEM;
109 if (!stralloc_0(rules)) return DNS_MEM;
110 }
111 if (!stralloc_cats(rules,"*.:")) return DNS_MEM;
112 if (!stralloc_0(rules)) return DNS_MEM;
113
114 return 0;
115}
116
117static int ok = 0;
118static unsigned int uses;
119static struct taia deadline;
120static stralloc rules = {0}; /* defined if ok */
121
123{
124 struct taia now;
125
126 taia_now(&now);
127 if (taia_less(&deadline,&now)) ok = 0;
128 if (!uses) ok = 0;
129
130 if (!ok) {
131 if (init(&rules) < 0) return DNS_INT;
132 taia_uint(&deadline,600);
133 taia_add(&deadline,&now,&deadline);
134 uses = 10000;
135 ok = 1;
136 }
137
138 --uses;
139 if (!stralloc_copy(out,&rules)) return DNS_MEM;
140 return 0;
141}
int dns_resolvconfrewrite(stralloc *out)
Definition: dns_rcrw.c:122
char * env_get(char *)
Definition: env.c:12
int taia_less(struct taia *, struct taia *)
Definition: taia.c:39
void taia_add(struct taia *, struct taia *, struct taia *)
Definition: taia.c:14
void taia_uint(struct taia *, unsigned int)
Definition: taia.c:99
int taia_now(struct taia *)
Definition: taia.c:48
int openreadclose(const char *, stralloc *, unsigned int)
Definition: readclose.c:33
unsigned int byte_chr(char *, unsigned int, int)
Definition: byte.c:9
#define byte_equal(s, n, t)
Definition: byte.h:18
#define DNS_ERR
Definition: dnsresolv.h:44
#define DNS_INT
Definition: dnsresolv.h:46
#define DNS_MEM
Definition: dnsresolv.h:43
unsigned int str_chr(const char *, int)
Definition: str.c:81
int stralloc_copy(stralloc *, stralloc *)
Definition: stralloc.c:41
int stralloc_catb(stralloc *, const char *, unsigned int)
Definition: stralloc.c:26
int stralloc_append(stralloc *sa, const char *in)
Definition: stralloc.c:112
#define stralloc_0(sa)
Definition: stralloc.h:37
int stralloc_cats(stralloc *, const char *)
Definition: stralloc.c:36
int stralloc_copys(stralloc *, const char *)
Definition: stralloc.c:79
size_t len
Definition: stralloc.h:19
char * s
Definition: stralloc.h:18
Definition: taia.h:13