]>
Commit | Line | Data |
---|---|---|
9210bea3 | 1 | diff -urN djbdns-1.05.org/MULTIPLEIP djbdns-1.05/MULTIPLEIP |
2 | --- djbdns-1.05.org/MULTIPLEIP Thu Jan 1 01:00:00 1970 | |
3 | +++ djbdns-1.05/MULTIPLEIP Sat Apr 28 22:15:42 2001 | |
4 | @@ -0,0 +1,10 @@ | |
5 | + | |
6 | +When applied, dnscache will accept a /-delimited list of IP address in $IP | |
7 | +and bind to each in turn. This allows one to have dnscache listen on, say, | |
8 | +external IP 1.2.3.4 which only client group A can reach, and, say, | |
9 | +internal IP 4.3.2.1 which only client group B can reach. Assuming 1.2.3.4 | |
10 | +and 4.3.2.1 are on the same machine, instead of running two independent | |
11 | +dnscache instances on 1.2.3.4 and 4.3.2.1, one instance can bind to both | |
12 | +IP's and serve queries from the shared cache. Simply put 1.2.3.4/4.3.2.1 in env/IP. | |
13 | + | |
14 | +http://danp.net/djbdns/patches.html | |
15 | diff -urN djbdns-1.05.org/dnscache.c djbdns-1.05/dnscache.c | |
16 | --- djbdns-1.05.org/dnscache.c Sun Feb 11 22:11:45 2001 | |
17 | +++ djbdns-1.05/dnscache.c Sat Apr 28 22:12:13 2001 | |
18 | @@ -5,6 +5,7 @@ | |
19 | #include "strerr.h" | |
20 | #include "error.h" | |
21 | #include "ip4.h" | |
22 | +#include "str.h" | |
23 | #include "uint16.h" | |
24 | #include "uint64.h" | |
25 | #include "socket.h" | |
26 | @@ -47,12 +48,20 @@ | |
27 | ||
28 | ||
29 | static char myipoutgoing[4]; | |
30 | -static char myipincoming[4]; | |
31 | static char buf[1024]; | |
32 | uint64 numqueries = 0; | |
33 | ||
34 | +struct interf { | |
35 | + char ip[4]; | |
36 | + int udp53; | |
37 | + int tcp53; | |
38 | + iopause_fd *udp53io; | |
39 | + iopause_fd *tcp53io; | |
40 | + | |
41 | + struct interf *next; | |
42 | +} ; | |
43 | ||
44 | -static int udp53; | |
45 | +struct interf *interhead = 0; | |
46 | ||
47 | #define MAXUDP 200 | |
48 | static struct udpclient { | |
49 | @@ -60,6 +69,7 @@ | |
50 | struct taia start; | |
51 | uint64 active; /* query number, if active; otherwise 0 */ | |
52 | iopause_fd *io; | |
53 | + int fd; | |
54 | char ip[4]; | |
55 | uint16 port; | |
56 | char id[2]; | |
57 | @@ -78,12 +88,12 @@ | |
58 | if (!u[j].active) return; | |
59 | response_id(u[j].id); | |
60 | if (response_len > 512) response_tc(); | |
61 | - socket_send4(udp53,response,response_len,u[j].ip,u[j].port); | |
62 | + socket_send4(u[j].fd,response,response_len,u[j].ip,u[j].port); | |
63 | log_querydone(&u[j].active,response_len); | |
64 | u[j].active = 0; --uactive; | |
65 | } | |
66 | ||
67 | -void u_new(void) | |
68 | +void u_new(int fd) | |
69 | { | |
70 | int j; | |
71 | int i; | |
72 | @@ -108,8 +118,9 @@ | |
73 | ||
74 | x = u + j; | |
75 | taia_now(&x->start); | |
76 | + x->fd = fd; | |
77 | ||
78 | - len = socket_recv4(udp53,buf,sizeof buf,x->ip,&x->port); | |
79 | + len = socket_recv4(x->fd,buf,sizeof buf,x->ip,&x->port); | |
80 | if (len == -1) return; | |
81 | if (len >= sizeof buf) return; | |
82 | if (x->port < 1024) if (x->port != 53) return; | |
83 | @@ -129,8 +140,6 @@ | |
84 | } | |
85 | ||
86 | ||
87 | -static int tcp53; | |
88 | - | |
89 | #define MAXTCP 20 | |
90 | struct tcpclient { | |
91 | struct query q; | |
92 | @@ -138,6 +147,7 @@ | |
93 | struct taia timeout; | |
94 | uint64 active; /* query number or 1, if active; otherwise 0 */ | |
95 | iopause_fd *io; | |
96 | + int fd; | |
97 | char ip[4]; /* send response to this address */ | |
98 | uint16 port; /* send response to this port */ | |
99 | char id[2]; | |
100 | @@ -266,7 +276,7 @@ | |
101 | x->state = 0; | |
102 | } | |
103 | ||
104 | -void t_new(void) | |
105 | +void t_new(int fd) | |
106 | { | |
107 | int i; | |
108 | int j; | |
109 | @@ -290,8 +300,9 @@ | |
110 | ||
111 | x = t + j; | |
112 | taia_now(&x->start); | |
113 | + x->fd = fd; | |
114 | ||
115 | - x->tcp = socket_accept4(tcp53,x->ip,&x->port); | |
116 | + x->tcp = socket_accept4(x->fd,x->ip,&x->port); | |
117 | if (x->tcp == -1) return; | |
118 | if (x->port < 1024) if (x->port != 53) { close(x->tcp); return; } | |
119 | if (!okclient(x->ip)) { close(x->tcp); return; } | |
120 | @@ -304,19 +315,24 @@ | |
121 | log_tcpopen(x->ip,x->port); | |
122 | } | |
123 | ||
124 | +#define FATAL "dnscache: fatal: " | |
125 | ||
126 | -iopause_fd io[3 + MAXUDP + MAXTCP]; | |
127 | -iopause_fd *udp53io; | |
128 | -iopause_fd *tcp53io; | |
129 | +iopause_fd *io = 0; | |
130 | +int numio; | |
131 | ||
132 | static void doit(void) | |
133 | { | |
134 | int j; | |
135 | struct taia deadline; | |
136 | struct taia stamp; | |
137 | + struct interf *inter; | |
138 | int iolen; | |
139 | int r; | |
140 | ||
141 | + io = (iopause_fd *) alloc((numio + 1 + MAXUDP + MAXTCP) * sizeof(iopause_fd)); | |
142 | + if (!io) | |
143 | + strerr_die2sys(111,FATAL,"unable to alloc io: "); | |
144 | + | |
145 | for (;;) { | |
146 | taia_now(&stamp); | |
147 | taia_uint(&deadline,120); | |
148 | @@ -324,13 +340,15 @@ | |
149 | ||
150 | iolen = 0; | |
151 | ||
152 | - udp53io = io + iolen++; | |
153 | - udp53io->fd = udp53; | |
154 | - udp53io->events = IOPAUSE_READ; | |
155 | - | |
156 | - tcp53io = io + iolen++; | |
157 | - tcp53io->fd = tcp53; | |
158 | - tcp53io->events = IOPAUSE_READ; | |
159 | + for (inter = interhead; inter != 0; inter = inter->next) { | |
160 | + inter->udp53io = io + iolen++; | |
161 | + inter->udp53io->fd = inter->udp53; | |
162 | + inter->udp53io->events = IOPAUSE_READ; | |
163 | + | |
164 | + inter->tcp53io = io + iolen++; | |
165 | + inter->tcp53io->fd = inter->tcp53; | |
166 | + inter->tcp53io->events = IOPAUSE_READ; | |
167 | + } | |
168 | ||
169 | for (j = 0;j < MAXUDP;++j) | |
170 | if (u[j].active) { | |
171 | @@ -372,46 +390,82 @@ | |
172 | t_rw(j); | |
173 | } | |
174 | ||
175 | - if (udp53io) | |
176 | - if (udp53io->revents) | |
177 | - u_new(); | |
178 | - | |
179 | - if (tcp53io) | |
180 | - if (tcp53io->revents) | |
181 | - t_new(); | |
182 | + for (inter = interhead; inter != 0; inter = inter->next) { | |
183 | + if (inter->udp53io) | |
184 | + if (inter->udp53io->revents) | |
185 | + u_new(inter->udp53); | |
186 | + | |
187 | + if (inter->tcp53io) | |
188 | + if (inter->tcp53io->revents) | |
189 | + t_new(inter->tcp53); | |
190 | + } | |
191 | } | |
192 | } | |
193 | ||
194 | -#define FATAL "dnscache: fatal: " | |
195 | - | |
196 | char seed[128]; | |
197 | ||
198 | int main() | |
199 | { | |
200 | char *x; | |
201 | + int len; | |
202 | + int pos; | |
203 | + int oldpos; | |
204 | + char iptmp[4]; | |
205 | + char iperr[IP4_FMT]; | |
206 | + struct interf *inter; | |
207 | + struct interf *itmp; | |
208 | unsigned long cachesize; | |
209 | ||
210 | x = env_get("IP"); | |
211 | if (!x) | |
212 | strerr_die2x(111,FATAL,"$IP not set"); | |
213 | - if (!ip4_scan(x,myipincoming)) | |
214 | - strerr_die3x(111,FATAL,"unable to parse IP address ",x); | |
215 | ||
216 | - udp53 = socket_udp(); | |
217 | - if (udp53 == -1) | |
218 | - strerr_die2sys(111,FATAL,"unable to create UDP socket: "); | |
219 | - if (socket_bind4_reuse(udp53,myipincoming,53) == -1) | |
220 | - strerr_die2sys(111,FATAL,"unable to bind UDP socket: "); | |
221 | - | |
222 | - tcp53 = socket_tcp(); | |
223 | - if (tcp53 == -1) | |
224 | - strerr_die2sys(111,FATAL,"unable to create TCP socket: "); | |
225 | - if (socket_bind4_reuse(tcp53,myipincoming,53) == -1) | |
226 | - strerr_die2sys(111,FATAL,"unable to bind TCP socket: "); | |
227 | + len = str_len(x); | |
228 | + numio = pos = oldpos = 0; | |
229 | + | |
230 | + while (pos < len) { | |
231 | + if (pos) oldpos = pos + 1; | |
232 | + pos = oldpos + str_chr(x + oldpos,'/'); | |
233 | + x[pos] = 0; | |
234 | + if (!str_len(x + oldpos)) continue; | |
235 | + | |
236 | + if (!ip4_scan(x + oldpos,iptmp)) | |
237 | + strerr_die3x(111,FATAL,"unable to parse IP address ",x + oldpos); | |
238 | + | |
239 | + inter = (struct interf *) alloc(sizeof(struct interf)); | |
240 | + | |
241 | + if (interhead == 0) interhead = inter; | |
242 | + else if (interhead->next == 0) interhead->next = inter; | |
243 | + else { | |
244 | + for (itmp = interhead; itmp->next != 0; itmp = itmp->next); | |
245 | + itmp->next = inter; | |
246 | + } | |
247 | + | |
248 | + inter->next = 0; | |
249 | + | |
250 | + inter->udp53 = socket_udp(); | |
251 | + if (inter->udp53 == -1) | |
252 | + strerr_die4sys(111,FATAL,"unable to create UDP socket for IP address ",x + oldpos,": "); | |
253 | + if (socket_bind4_reuse(inter->udp53,iptmp,53) == -1) | |
254 | + strerr_die4sys(111,FATAL,"unable to bind UDP socket for IP address ",x + oldpos,": "); | |
255 | + | |
256 | + inter->tcp53 = socket_tcp(); | |
257 | + if (inter->tcp53 == -1) | |
258 | + strerr_die4sys(111,FATAL,"unable to create TCP socket for IP address ",x + oldpos,": "); | |
259 | + if (socket_bind4_reuse(inter->tcp53,iptmp,53) == -1) | |
260 | + strerr_die4sys(111,FATAL,"unable to bind TCP socket for IP address ",x + oldpos,": "); | |
261 | + | |
262 | + numio++; | |
263 | + log_listen(iptmp); | |
264 | + } | |
265 | + | |
266 | + if (interhead == 0) | |
267 | + strerr_die2x(111,FATAL,"no interfaces to listen on"); | |
268 | ||
269 | droproot(FATAL); | |
270 | ||
271 | - socket_tryreservein(udp53,131072); | |
272 | + for (inter = interhead; inter != 0; inter = inter->next) | |
273 | + socket_tryreservein(inter->udp53,131072); | |
274 | ||
275 | byte_zero(seed,sizeof seed); | |
276 | read(0,seed,sizeof seed); | |
277 | @@ -439,8 +493,11 @@ | |
278 | if (!roots_init()) | |
279 | strerr_die2sys(111,FATAL,"unable to read servers: "); | |
280 | ||
281 | - if (socket_listen(tcp53,20) == -1) | |
282 | - strerr_die2sys(111,FATAL,"unable to listen on TCP socket: "); | |
283 | + for (inter = interhead; inter != 0; inter = inter->next) | |
284 | + if (socket_listen(inter->tcp53,20) == -1) { | |
285 | + iperr[ip4_fmt(iperr,inter->ip)] = 0; | |
286 | + strerr_die4sys(111,FATAL,"unable to listen on TCP socket for IP ",iperr,": "); | |
287 | + } | |
288 | ||
289 | log_startup(); | |
290 | doit(); | |
291 | diff -urN djbdns-1.05.org/log.c djbdns-1.05/log.c | |
292 | --- djbdns-1.05.org/log.c Sun Feb 11 22:11:45 2001 | |
293 | +++ djbdns-1.05/log.c Sat Apr 28 22:12:13 2001 | |
294 | @@ -94,6 +94,13 @@ | |
295 | line(); | |
296 | } | |
297 | ||
298 | +void log_listen(const char addr[4]) | |
299 | +{ | |
300 | + string("listening on "); | |
301 | + ip(addr); | |
302 | + line(); | |
303 | +} | |
304 | + | |
305 | void log_query(uint64 *qnum,const char client[4],unsigned int port,const char id[2],const char *q,const char qtype[2]) | |
306 | { | |
307 | string("query "); number(*qnum); space(); | |
308 | diff -urN djbdns-1.05.org/log.h djbdns-1.05/log.h | |
309 | --- djbdns-1.05.org/log.h Sun Feb 11 22:11:45 2001 | |
310 | +++ djbdns-1.05/log.h Sat Apr 28 22:12:13 2001 | |
311 | @@ -4,6 +4,7 @@ | |
312 | #include "uint64.h" | |
313 | ||
314 | extern void log_startup(void); | |
315 | +extern void log_listen(const char *); | |
316 | ||
317 | extern void log_query(uint64 *,const char *,unsigned int,const char *,const char *,const char *); | |
318 | extern void log_querydrop(uint64 *); |