Add BIND 9.2.4rc7.
[dragonfly.git] / contrib / bind-9.2.4rc7 / lib / bind / irs / dns_nw.c
blob106f50d1c4e8efdef84af7fc68dd99c39703aa6a
1 /*
2 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (c) 1996-1999 by Internet Software Consortium.
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
15 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 #if defined(LIBC_SCCS) && !defined(lint)
19 static const char rcsid[] = "$Id: dns_nw.c,v 1.3.2.7 2004/05/17 07:46:42 marka Exp $";
20 #endif /* LIBC_SCCS and not lint */
22 /* Imports. */
24 #include "port_before.h"
26 #include <sys/param.h>
27 #include <sys/socket.h>
29 #include <netinet/in.h>
30 #include <arpa/inet.h>
31 #include <arpa/nameser.h>
33 #include <ctype.h>
34 #include <errno.h>
35 #include <netdb.h>
36 #include <resolv.h>
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <string.h>
41 #include <isc/memcluster.h>
42 #include <irs.h>
44 #include "port_after.h"
46 #include "irs_p.h"
47 #include "dns_p.h"
49 #ifdef SPRINTF_CHAR
50 # define SPRINTF(x) strlen(sprintf/**/x)
51 #else
52 # define SPRINTF(x) sprintf x
53 #endif
55 /* Definitions. */
57 #define MAXALIASES 35
59 #define MAXPACKET (64*1024)
61 struct pvt {
62 struct nwent net;
63 char * ali[MAXALIASES];
64 char buf[BUFSIZ+1];
65 struct __res_state * res;
66 void (*free_res)(void *);
69 typedef union {
70 long al;
71 char ac;
72 } align;
74 enum by_what { by_addr, by_name };
76 /* Forwards. */
78 static void nw_close(struct irs_nw *);
79 static struct nwent * nw_byname(struct irs_nw *, const char *, int);
80 static struct nwent * nw_byaddr(struct irs_nw *, void *, int, int);
81 static struct nwent * nw_next(struct irs_nw *);
82 static void nw_rewind(struct irs_nw *);
83 static void nw_minimize(struct irs_nw *);
84 static struct __res_state * nw_res_get(struct irs_nw *this);
85 static void nw_res_set(struct irs_nw *this,
86 struct __res_state *res,
87 void (*free_res)(void *));
89 static struct nwent * get1101byaddr(struct irs_nw *, u_char *, int);
90 static struct nwent * get1101byname(struct irs_nw *, const char *);
91 static struct nwent * get1101answer(struct irs_nw *,
92 u_char *ansbuf, int anslen,
93 enum by_what by_what,
94 int af, const char *name,
95 const u_char *addr, int addrlen);
96 static struct nwent * get1101mask(struct irs_nw *this, struct nwent *);
97 static int make1101inaddr(const u_char *, int, char *, int);
98 static void normalize_name(char *name);
99 static int init(struct irs_nw *this);
101 /* Exports. */
103 struct irs_nw *
104 irs_dns_nw(struct irs_acc *this) {
105 struct irs_nw *nw;
106 struct pvt *pvt;
108 UNUSED(this);
110 if (!(pvt = memget(sizeof *pvt))) {
111 errno = ENOMEM;
112 return (NULL);
114 memset(pvt, 0, sizeof *pvt);
115 if (!(nw = memget(sizeof *nw))) {
116 memput(pvt, sizeof *pvt);
117 errno = ENOMEM;
118 return (NULL);
120 memset(nw, 0x5e, sizeof *nw);
121 nw->private = pvt;
122 nw->close = nw_close;
123 nw->byname = nw_byname;
124 nw->byaddr = nw_byaddr;
125 nw->next = nw_next;
126 nw->rewind = nw_rewind;
127 nw->minimize = nw_minimize;
128 nw->res_get = nw_res_get;
129 nw->res_set = nw_res_set;
130 return (nw);
133 /* Methods. */
135 static void
136 nw_close(struct irs_nw *this) {
137 struct pvt *pvt = (struct pvt *)this->private;
139 nw_minimize(this);
141 if (pvt->res && pvt->free_res)
142 (*pvt->free_res)(pvt->res);
144 memput(pvt, sizeof *pvt);
145 memput(this, sizeof *this);
148 static struct nwent *
149 nw_byname(struct irs_nw *this, const char *name, int af) {
150 struct pvt *pvt = (struct pvt *)this->private;
152 if (init(this) == -1)
153 return (NULL);
155 switch (af) {
156 case AF_INET:
157 return (get1101byname(this, name));
158 default:
159 (void)NULL;
161 RES_SET_H_ERRNO(pvt->res, NETDB_INTERNAL);
162 errno = EAFNOSUPPORT;
163 return (NULL);
166 static struct nwent *
167 nw_byaddr(struct irs_nw *this, void *net, int len, int af) {
168 struct pvt *pvt = (struct pvt *)this->private;
170 if (init(this) == -1)
171 return (NULL);
173 switch (af) {
174 case AF_INET:
175 return (get1101byaddr(this, net, len));
176 default:
177 (void)NULL;
179 RES_SET_H_ERRNO(pvt->res, NETDB_INTERNAL);
180 errno = EAFNOSUPPORT;
181 return (NULL);
184 static struct nwent *
185 nw_next(struct irs_nw *this) {
187 UNUSED(this);
189 return (NULL);
192 static void
193 nw_rewind(struct irs_nw *this) {
194 UNUSED(this);
195 /* NOOP */
198 static void
199 nw_minimize(struct irs_nw *this) {
200 struct pvt *pvt = (struct pvt *)this->private;
202 if (pvt->res)
203 res_nclose(pvt->res);
206 static struct __res_state *
207 nw_res_get(struct irs_nw *this) {
208 struct pvt *pvt = (struct pvt *)this->private;
210 if (!pvt->res) {
211 struct __res_state *res;
212 res = (struct __res_state *)malloc(sizeof *res);
213 if (!res) {
214 errno = ENOMEM;
215 return (NULL);
217 memset(res, 0, sizeof *res);
218 nw_res_set(this, res, free);
221 return (pvt->res);
224 static void
225 nw_res_set(struct irs_nw *this, struct __res_state *res,
226 void (*free_res)(void *)) {
227 struct pvt *pvt = (struct pvt *)this->private;
229 if (pvt->res && pvt->free_res) {
230 res_nclose(pvt->res);
231 (*pvt->free_res)(pvt->res);
234 pvt->res = res;
235 pvt->free_res = free_res;
238 /* Private. */
240 static struct nwent *
241 get1101byname(struct irs_nw *this, const char *name) {
242 struct pvt *pvt = (struct pvt *)this->private;
243 u_char *ansbuf;
244 int anslen;
245 struct nwent *result;
247 ansbuf = memget(MAXPACKET);
248 if (ansbuf == NULL) {
249 errno = ENOMEM;
250 RES_SET_H_ERRNO(pvt->res, NETDB_INTERNAL);
251 return (NULL);
253 anslen = res_nsearch(pvt->res, name, C_IN, T_PTR, ansbuf, MAXPACKET);
254 if (anslen < 0) {
255 memput(ansbuf, MAXPACKET);
256 return (NULL);
258 result = get1101mask(this, get1101answer(this, ansbuf, anslen, by_name,
259 AF_INET, name, NULL, 0));
260 memput(ansbuf, MAXPACKET);
261 return (result);
264 static struct nwent *
265 get1101byaddr(struct irs_nw *this, u_char *net, int len) {
266 struct pvt *pvt = (struct pvt *)this->private;
267 char qbuf[sizeof "255.255.255.255.in-addr.arpa"];
268 struct nwent *result;
269 u_char *ansbuf;
270 int anslen;
272 if (len < 1 || len > 32) {
273 errno = EINVAL;
274 RES_SET_H_ERRNO(pvt->res, NETDB_INTERNAL);
275 return (NULL);
277 if (make1101inaddr(net, len, qbuf, sizeof qbuf) < 0)
278 return (NULL);
279 ansbuf = memget(MAXPACKET);
280 if (ansbuf == NULL) {
281 errno = ENOMEM;
282 RES_SET_H_ERRNO(pvt->res, NETDB_INTERNAL);
283 return (NULL);
285 anslen = res_nquery(pvt->res, qbuf, C_IN, T_PTR, ansbuf, MAXPACKET);
286 if (anslen < 0) {
287 memput(ansbuf, MAXPACKET);
288 return (NULL);
290 result = get1101mask(this, get1101answer(this, ansbuf, anslen, by_addr,
291 AF_INET, NULL, net, len));
292 memput(ansbuf, MAXPACKET);
293 return (result);
296 static struct nwent *
297 get1101answer(struct irs_nw *this,
298 u_char *ansbuf, int anslen, enum by_what by_what,
299 int af, const char *name, const u_char *addr, int addrlen)
301 struct pvt *pvt = (struct pvt *)this->private;
302 int type, class, ancount, qdcount, haveanswer;
303 char *bp, *ep, **ap;
304 u_char *cp, *eom;
305 HEADER *hp;
307 /* Initialize, and parse header. */
308 eom = ansbuf + anslen;
309 if (ansbuf + HFIXEDSZ > eom) {
310 RES_SET_H_ERRNO(pvt->res, NO_RECOVERY);
311 return (NULL);
313 hp = (HEADER *)ansbuf;
314 cp = ansbuf + HFIXEDSZ;
315 qdcount = ntohs(hp->qdcount);
316 while (qdcount-- > 0) {
317 int n = dn_skipname(cp, eom);
318 cp += n + QFIXEDSZ;
319 if (n < 0 || cp > eom) {
320 RES_SET_H_ERRNO(pvt->res, NO_RECOVERY);
321 return (NULL);
324 ancount = ntohs(hp->ancount);
325 if (!ancount) {
326 if (hp->aa)
327 RES_SET_H_ERRNO(pvt->res, HOST_NOT_FOUND);
328 else
329 RES_SET_H_ERRNO(pvt->res, TRY_AGAIN);
330 return (NULL);
333 /* Prepare a return structure. */
334 bp = pvt->buf;
335 ep = pvt->buf + sizeof(pvt->buf);
336 pvt->net.n_name = NULL;
337 pvt->net.n_aliases = pvt->ali;
338 pvt->net.n_addrtype = af;
339 pvt->net.n_addr = NULL;
340 pvt->net.n_length = addrlen;
342 /* Save input key if given. */
343 switch (by_what) {
344 case by_name:
345 if (name != NULL) {
346 int n = strlen(name) + 1;
348 if (n > (ep - bp)) {
349 RES_SET_H_ERRNO(pvt->res, NO_RECOVERY);
350 return (NULL);
352 pvt->net.n_name = strcpy(bp, name); /* (checked) */
353 bp += n;
355 break;
356 case by_addr:
357 if (addr != NULL && addrlen != 0) {
358 int n = addrlen / 8 + ((addrlen % 8) != 0);
360 if (INADDRSZ > (ep - bp)) {
361 RES_SET_H_ERRNO(pvt->res, NO_RECOVERY);
362 return (NULL);
364 memset(bp, 0, INADDRSZ);
365 memcpy(bp, addr, n);
366 pvt->net.n_addr = bp;
367 bp += INADDRSZ;
369 break;
370 default:
371 abort();
374 /* Parse the answer, collect aliases. */
375 ap = pvt->ali;
376 haveanswer = 0;
377 while (--ancount >= 0 && cp < eom) {
378 int n = dn_expand(ansbuf, eom, cp, bp, ep - bp);
380 cp += n; /* Owner */
381 if (n < 0 || !maybe_dnok(pvt->res, bp) ||
382 cp + 3 * INT16SZ + INT32SZ > eom) {
383 RES_SET_H_ERRNO(pvt->res, NO_RECOVERY);
384 return (NULL);
386 GETSHORT(type, cp); /* Type */
387 GETSHORT(class, cp); /* Class */
388 cp += INT32SZ; /* TTL */
389 GETSHORT(n, cp); /* RDLENGTH */
390 if (class == C_IN && type == T_PTR) {
391 int nn;
393 nn = dn_expand(ansbuf, eom, cp, bp, ep - bp);
394 if (nn < 0 || !maybe_hnok(pvt->res, bp) || nn != n) {
395 RES_SET_H_ERRNO(pvt->res, NO_RECOVERY);
396 return (NULL);
398 normalize_name(bp);
399 switch (by_what) {
400 case by_addr: {
401 if (pvt->net.n_name == NULL)
402 pvt->net.n_name = bp;
403 else if (ns_samename(pvt->net.n_name, bp) == 1)
404 break;
405 else
406 *ap++ = bp;
407 nn = strlen(bp) + 1;
408 bp += nn;
409 haveanswer++;
410 break;
412 case by_name: {
413 u_int b1, b2, b3, b4;
415 if (pvt->net.n_addr != NULL ||
416 sscanf(bp, "%u.%u.%u.%u.in-addr.arpa",
417 &b1, &b2, &b3, &b4) != 4)
418 break;
419 if ((ep - bp) < INADDRSZ) {
420 RES_SET_H_ERRNO(pvt->res, NO_RECOVERY);
421 return (NULL);
423 pvt->net.n_addr = bp;
424 *bp++ = b4;
425 *bp++ = b3;
426 *bp++ = b2;
427 *bp++ = b1;
428 pvt->net.n_length = INADDRSZ * 8;
429 haveanswer++;
433 cp += n; /* RDATA */
435 if (!haveanswer) {
436 RES_SET_H_ERRNO(pvt->res, TRY_AGAIN);
437 return (NULL);
439 *ap = NULL;
441 return (&pvt->net);
444 static struct nwent *
445 get1101mask(struct irs_nw *this, struct nwent *nwent) {
446 struct pvt *pvt = (struct pvt *)this->private;
447 char qbuf[sizeof "255.255.255.255.in-addr.arpa"], owner[MAXDNAME];
448 int anslen, type, class, ancount, qdcount;
449 u_char *ansbuf, *cp, *eom;
450 HEADER *hp;
452 if (!nwent)
453 return (NULL);
454 if (make1101inaddr(nwent->n_addr, nwent->n_length, qbuf, sizeof qbuf)
455 < 0) {
456 /* "First, do no harm." */
457 return (nwent);
460 ansbuf = memget(MAXPACKET);
461 if (ansbuf == NULL) {
462 errno = ENOMEM;
463 RES_SET_H_ERRNO(pvt->res, NETDB_INTERNAL);
464 return (NULL);
466 /* Query for the A RR that would hold this network's mask. */
467 anslen = res_nquery(pvt->res, qbuf, C_IN, T_A, ansbuf, MAXPACKET);
468 if (anslen < HFIXEDSZ) {
469 memput(ansbuf, MAXPACKET);
470 return (nwent);
473 /* Initialize, and parse header. */
474 hp = (HEADER *)ansbuf;
475 cp = ansbuf + HFIXEDSZ;
476 eom = ansbuf + anslen;
477 qdcount = ntohs(hp->qdcount);
478 while (qdcount-- > 0) {
479 int n = dn_skipname(cp, eom);
480 cp += n + QFIXEDSZ;
481 if (n < 0 || cp > eom) {
482 memput(ansbuf, MAXPACKET);
483 return (nwent);
486 ancount = ntohs(hp->ancount);
488 /* Parse the answer, collect aliases. */
489 while (--ancount >= 0 && cp < eom) {
490 int n = dn_expand(ansbuf, eom, cp, owner, sizeof owner);
492 if (n < 0 || !maybe_dnok(pvt->res, owner))
493 break;
494 cp += n; /* Owner */
495 if (cp + 3 * INT16SZ + INT32SZ > eom)
496 break;
497 GETSHORT(type, cp); /* Type */
498 GETSHORT(class, cp); /* Class */
499 cp += INT32SZ; /* TTL */
500 GETSHORT(n, cp); /* RDLENGTH */
501 if (cp + n > eom)
502 break;
503 if (n == INADDRSZ && class == C_IN && type == T_A &&
504 ns_samename(qbuf, owner) == 1) {
505 /* This A RR indicates the actual netmask. */
506 int nn, mm;
508 nwent->n_length = 0;
509 for (nn = 0; nn < INADDRSZ; nn++)
510 for (mm = 7; mm >= 0; mm--)
511 if (cp[nn] & (1 << mm))
512 nwent->n_length++;
513 else
514 break;
516 cp += n; /* RDATA */
518 memput(ansbuf, MAXPACKET);
519 return (nwent);
522 static int
523 make1101inaddr(const u_char *net, int bits, char *name, int size) {
524 int n, m;
525 char *ep;
527 ep = name + size;
529 /* Zero fill any whole bytes left out of the prefix. */
530 for (n = (32 - bits) / 8; n > 0; n--) {
531 if (ep - name < (int)(sizeof "0."))
532 goto emsgsize;
533 m = SPRINTF((name, "0."));
534 name += m;
537 /* Format the partial byte, if any, within the prefix. */
538 if ((n = bits % 8) != 0) {
539 if (ep - name < (int)(sizeof "255."))
540 goto emsgsize;
541 m = SPRINTF((name, "%u.",
542 net[bits / 8] & ~((1 << (8 - n)) - 1)));
543 name += m;
546 /* Format the whole bytes within the prefix. */
547 for (n = bits / 8; n > 0; n--) {
548 if (ep - name < (int)(sizeof "255."))
549 goto emsgsize;
550 m = SPRINTF((name, "%u.", net[n - 1]));
551 name += m;
554 /* Add the static text. */
555 if (ep - name < (int)(sizeof "in-addr.arpa"))
556 goto emsgsize;
557 (void) SPRINTF((name, "in-addr.arpa"));
558 return (0);
560 emsgsize:
561 errno = EMSGSIZE;
562 return (-1);
565 static void
566 normalize_name(char *name) {
567 char *t;
569 /* Make lower case. */
570 for (t = name; *t; t++)
571 if (isascii((unsigned char)*t) && isupper((unsigned char)*t))
572 *t = tolower(*t);
574 /* Remove trailing dots. */
575 while (t > name && t[-1] == '.')
576 *--t = '\0';
579 static int
580 init(struct irs_nw *this) {
581 struct pvt *pvt = (struct pvt *)this->private;
583 if (!pvt->res && !nw_res_get(this))
584 return (-1);
585 if (((pvt->res->options & RES_INIT) == 0U) &&
586 res_ninit(pvt->res) == -1)
587 return (-1);
588 return (0);