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