17c478bd9Sstevel@tonic-gate /*
2*9525b14bSRao Shoaib * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
37c478bd9Sstevel@tonic-gate * Copyright (c) 1996-1999 by Internet Software Consortium.
47c478bd9Sstevel@tonic-gate *
57c478bd9Sstevel@tonic-gate * Permission to use, copy, modify, and distribute this software for any
67c478bd9Sstevel@tonic-gate * purpose with or without fee is hereby granted, provided that the above
77c478bd9Sstevel@tonic-gate * copyright notice and this permission notice appear in all copies.
87c478bd9Sstevel@tonic-gate *
9*9525b14bSRao Shoaib * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
10*9525b14bSRao Shoaib * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11*9525b14bSRao Shoaib * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
12*9525b14bSRao Shoaib * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13*9525b14bSRao Shoaib * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14*9525b14bSRao Shoaib * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
15*9525b14bSRao Shoaib * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
167c478bd9Sstevel@tonic-gate */
177c478bd9Sstevel@tonic-gate
187c478bd9Sstevel@tonic-gate #if defined(LIBC_SCCS) && !defined(lint)
19*9525b14bSRao Shoaib static const char rcsid[] = "$Id: dns_nw.c,v 1.12 2005/04/27 04:56:22 sra Exp $";
207c478bd9Sstevel@tonic-gate #endif /* LIBC_SCCS and not lint */
217c478bd9Sstevel@tonic-gate
227c478bd9Sstevel@tonic-gate /* Imports. */
237c478bd9Sstevel@tonic-gate
247c478bd9Sstevel@tonic-gate #include "port_before.h"
257c478bd9Sstevel@tonic-gate
267c478bd9Sstevel@tonic-gate #include <sys/param.h>
277c478bd9Sstevel@tonic-gate #include <sys/socket.h>
287c478bd9Sstevel@tonic-gate
297c478bd9Sstevel@tonic-gate #include <netinet/in.h>
307c478bd9Sstevel@tonic-gate #include <arpa/inet.h>
317c478bd9Sstevel@tonic-gate #include <arpa/nameser.h>
327c478bd9Sstevel@tonic-gate
337c478bd9Sstevel@tonic-gate #include <ctype.h>
347c478bd9Sstevel@tonic-gate #include <errno.h>
357c478bd9Sstevel@tonic-gate #include <netdb.h>
367c478bd9Sstevel@tonic-gate #include <resolv.h>
377c478bd9Sstevel@tonic-gate #include <stdio.h>
387c478bd9Sstevel@tonic-gate #include <stdlib.h>
397c478bd9Sstevel@tonic-gate #include <string.h>
407c478bd9Sstevel@tonic-gate
417c478bd9Sstevel@tonic-gate #include <isc/memcluster.h>
427c478bd9Sstevel@tonic-gate #include <irs.h>
437c478bd9Sstevel@tonic-gate
447c478bd9Sstevel@tonic-gate #include "port_after.h"
457c478bd9Sstevel@tonic-gate
467c478bd9Sstevel@tonic-gate #include "irs_p.h"
477c478bd9Sstevel@tonic-gate #include "dns_p.h"
487c478bd9Sstevel@tonic-gate
497c478bd9Sstevel@tonic-gate #ifdef SPRINTF_CHAR
507c478bd9Sstevel@tonic-gate # define SPRINTF(x) strlen(sprintf/**/x)
517c478bd9Sstevel@tonic-gate #else
527c478bd9Sstevel@tonic-gate # define SPRINTF(x) sprintf x
537c478bd9Sstevel@tonic-gate #endif
547c478bd9Sstevel@tonic-gate
557c478bd9Sstevel@tonic-gate /* Definitions. */
567c478bd9Sstevel@tonic-gate
577c478bd9Sstevel@tonic-gate #define MAXALIASES 35
587c478bd9Sstevel@tonic-gate
597c478bd9Sstevel@tonic-gate #define MAXPACKET (64*1024)
607c478bd9Sstevel@tonic-gate
617c478bd9Sstevel@tonic-gate struct pvt {
627c478bd9Sstevel@tonic-gate struct nwent net;
637c478bd9Sstevel@tonic-gate char * ali[MAXALIASES];
647c478bd9Sstevel@tonic-gate char buf[BUFSIZ+1];
657c478bd9Sstevel@tonic-gate struct __res_state * res;
667c478bd9Sstevel@tonic-gate void (*free_res)(void *);
677c478bd9Sstevel@tonic-gate };
687c478bd9Sstevel@tonic-gate
697c478bd9Sstevel@tonic-gate typedef union {
707c478bd9Sstevel@tonic-gate long al;
717c478bd9Sstevel@tonic-gate char ac;
727c478bd9Sstevel@tonic-gate } align;
737c478bd9Sstevel@tonic-gate
747c478bd9Sstevel@tonic-gate enum by_what { by_addr, by_name };
757c478bd9Sstevel@tonic-gate
767c478bd9Sstevel@tonic-gate /* Forwards. */
777c478bd9Sstevel@tonic-gate
787c478bd9Sstevel@tonic-gate static void nw_close(struct irs_nw *);
797c478bd9Sstevel@tonic-gate static struct nwent * nw_byname(struct irs_nw *, const char *, int);
807c478bd9Sstevel@tonic-gate static struct nwent * nw_byaddr(struct irs_nw *, void *, int, int);
817c478bd9Sstevel@tonic-gate static struct nwent * nw_next(struct irs_nw *);
827c478bd9Sstevel@tonic-gate static void nw_rewind(struct irs_nw *);
837c478bd9Sstevel@tonic-gate static void nw_minimize(struct irs_nw *);
847c478bd9Sstevel@tonic-gate static struct __res_state * nw_res_get(struct irs_nw *this);
857c478bd9Sstevel@tonic-gate static void nw_res_set(struct irs_nw *this,
867c478bd9Sstevel@tonic-gate struct __res_state *res,
877c478bd9Sstevel@tonic-gate void (*free_res)(void *));
887c478bd9Sstevel@tonic-gate
897c478bd9Sstevel@tonic-gate static struct nwent * get1101byaddr(struct irs_nw *, u_char *, int);
907c478bd9Sstevel@tonic-gate static struct nwent * get1101byname(struct irs_nw *, const char *);
917c478bd9Sstevel@tonic-gate static struct nwent * get1101answer(struct irs_nw *,
927c478bd9Sstevel@tonic-gate u_char *ansbuf, int anslen,
937c478bd9Sstevel@tonic-gate enum by_what by_what,
947c478bd9Sstevel@tonic-gate int af, const char *name,
957c478bd9Sstevel@tonic-gate const u_char *addr, int addrlen);
967c478bd9Sstevel@tonic-gate static struct nwent * get1101mask(struct irs_nw *this, struct nwent *);
977c478bd9Sstevel@tonic-gate static int make1101inaddr(const u_char *, int, char *, int);
987c478bd9Sstevel@tonic-gate static void normalize_name(char *name);
997c478bd9Sstevel@tonic-gate static int init(struct irs_nw *this);
1007c478bd9Sstevel@tonic-gate
1017c478bd9Sstevel@tonic-gate /* Exports. */
1027c478bd9Sstevel@tonic-gate
1037c478bd9Sstevel@tonic-gate struct irs_nw *
irs_dns_nw(struct irs_acc * this)1047c478bd9Sstevel@tonic-gate irs_dns_nw(struct irs_acc *this) {
1057c478bd9Sstevel@tonic-gate struct irs_nw *nw;
1067c478bd9Sstevel@tonic-gate struct pvt *pvt;
1077c478bd9Sstevel@tonic-gate
1087c478bd9Sstevel@tonic-gate UNUSED(this);
1097c478bd9Sstevel@tonic-gate
1107c478bd9Sstevel@tonic-gate if (!(pvt = memget(sizeof *pvt))) {
1117c478bd9Sstevel@tonic-gate errno = ENOMEM;
1127c478bd9Sstevel@tonic-gate return (NULL);
1137c478bd9Sstevel@tonic-gate }
1147c478bd9Sstevel@tonic-gate memset(pvt, 0, sizeof *pvt);
1157c478bd9Sstevel@tonic-gate if (!(nw = memget(sizeof *nw))) {
1167c478bd9Sstevel@tonic-gate memput(pvt, sizeof *pvt);
1177c478bd9Sstevel@tonic-gate errno = ENOMEM;
1187c478bd9Sstevel@tonic-gate return (NULL);
1197c478bd9Sstevel@tonic-gate }
1207c478bd9Sstevel@tonic-gate memset(nw, 0x5e, sizeof *nw);
1217c478bd9Sstevel@tonic-gate nw->private = pvt;
1227c478bd9Sstevel@tonic-gate nw->close = nw_close;
1237c478bd9Sstevel@tonic-gate nw->byname = nw_byname;
1247c478bd9Sstevel@tonic-gate nw->byaddr = nw_byaddr;
1257c478bd9Sstevel@tonic-gate nw->next = nw_next;
1267c478bd9Sstevel@tonic-gate nw->rewind = nw_rewind;
1277c478bd9Sstevel@tonic-gate nw->minimize = nw_minimize;
1287c478bd9Sstevel@tonic-gate nw->res_get = nw_res_get;
1297c478bd9Sstevel@tonic-gate nw->res_set = nw_res_set;
1307c478bd9Sstevel@tonic-gate return (nw);
1317c478bd9Sstevel@tonic-gate }
1327c478bd9Sstevel@tonic-gate
1337c478bd9Sstevel@tonic-gate /* Methods. */
1347c478bd9Sstevel@tonic-gate
1357c478bd9Sstevel@tonic-gate static void
nw_close(struct irs_nw * this)1367c478bd9Sstevel@tonic-gate nw_close(struct irs_nw *this) {
1377c478bd9Sstevel@tonic-gate struct pvt *pvt = (struct pvt *)this->private;
1387c478bd9Sstevel@tonic-gate
1397c478bd9Sstevel@tonic-gate nw_minimize(this);
1407c478bd9Sstevel@tonic-gate
1417c478bd9Sstevel@tonic-gate if (pvt->res && pvt->free_res)
1427c478bd9Sstevel@tonic-gate (*pvt->free_res)(pvt->res);
1437c478bd9Sstevel@tonic-gate
1447c478bd9Sstevel@tonic-gate memput(pvt, sizeof *pvt);
1457c478bd9Sstevel@tonic-gate memput(this, sizeof *this);
1467c478bd9Sstevel@tonic-gate }
1477c478bd9Sstevel@tonic-gate
1487c478bd9Sstevel@tonic-gate static struct nwent *
nw_byname(struct irs_nw * this,const char * name,int af)1497c478bd9Sstevel@tonic-gate nw_byname(struct irs_nw *this, const char *name, int af) {
1507c478bd9Sstevel@tonic-gate struct pvt *pvt = (struct pvt *)this->private;
1517c478bd9Sstevel@tonic-gate
1527c478bd9Sstevel@tonic-gate if (init(this) == -1)
1537c478bd9Sstevel@tonic-gate return (NULL);
1547c478bd9Sstevel@tonic-gate
1557c478bd9Sstevel@tonic-gate switch (af) {
1567c478bd9Sstevel@tonic-gate case AF_INET:
1577c478bd9Sstevel@tonic-gate return (get1101byname(this, name));
1587c478bd9Sstevel@tonic-gate default:
1597c478bd9Sstevel@tonic-gate (void)NULL;
1607c478bd9Sstevel@tonic-gate }
1617c478bd9Sstevel@tonic-gate RES_SET_H_ERRNO(pvt->res, NETDB_INTERNAL);
1627c478bd9Sstevel@tonic-gate errno = EAFNOSUPPORT;
1637c478bd9Sstevel@tonic-gate return (NULL);
1647c478bd9Sstevel@tonic-gate }
1657c478bd9Sstevel@tonic-gate
1667c478bd9Sstevel@tonic-gate static struct nwent *
nw_byaddr(struct irs_nw * this,void * net,int len,int af)1677c478bd9Sstevel@tonic-gate nw_byaddr(struct irs_nw *this, void *net, int len, int af) {
1687c478bd9Sstevel@tonic-gate struct pvt *pvt = (struct pvt *)this->private;
1697c478bd9Sstevel@tonic-gate
1707c478bd9Sstevel@tonic-gate if (init(this) == -1)
1717c478bd9Sstevel@tonic-gate return (NULL);
1727c478bd9Sstevel@tonic-gate
1737c478bd9Sstevel@tonic-gate switch (af) {
1747c478bd9Sstevel@tonic-gate case AF_INET:
1757c478bd9Sstevel@tonic-gate return (get1101byaddr(this, net, len));
1767c478bd9Sstevel@tonic-gate default:
1777c478bd9Sstevel@tonic-gate (void)NULL;
1787c478bd9Sstevel@tonic-gate }
1797c478bd9Sstevel@tonic-gate RES_SET_H_ERRNO(pvt->res, NETDB_INTERNAL);
1807c478bd9Sstevel@tonic-gate errno = EAFNOSUPPORT;
1817c478bd9Sstevel@tonic-gate return (NULL);
1827c478bd9Sstevel@tonic-gate }
1837c478bd9Sstevel@tonic-gate
1847c478bd9Sstevel@tonic-gate static struct nwent *
nw_next(struct irs_nw * this)1857c478bd9Sstevel@tonic-gate nw_next(struct irs_nw *this) {
1867c478bd9Sstevel@tonic-gate
1877c478bd9Sstevel@tonic-gate UNUSED(this);
1887c478bd9Sstevel@tonic-gate
1897c478bd9Sstevel@tonic-gate return (NULL);
1907c478bd9Sstevel@tonic-gate }
1917c478bd9Sstevel@tonic-gate
1927c478bd9Sstevel@tonic-gate static void
nw_rewind(struct irs_nw * this)1937c478bd9Sstevel@tonic-gate nw_rewind(struct irs_nw *this) {
1947c478bd9Sstevel@tonic-gate UNUSED(this);
1957c478bd9Sstevel@tonic-gate /* NOOP */
1967c478bd9Sstevel@tonic-gate }
1977c478bd9Sstevel@tonic-gate
1987c478bd9Sstevel@tonic-gate static void
nw_minimize(struct irs_nw * this)1997c478bd9Sstevel@tonic-gate nw_minimize(struct irs_nw *this) {
2007c478bd9Sstevel@tonic-gate struct pvt *pvt = (struct pvt *)this->private;
2017c478bd9Sstevel@tonic-gate
2027c478bd9Sstevel@tonic-gate if (pvt->res)
2037c478bd9Sstevel@tonic-gate res_nclose(pvt->res);
2047c478bd9Sstevel@tonic-gate }
2057c478bd9Sstevel@tonic-gate
2067c478bd9Sstevel@tonic-gate static struct __res_state *
nw_res_get(struct irs_nw * this)2077c478bd9Sstevel@tonic-gate nw_res_get(struct irs_nw *this) {
2087c478bd9Sstevel@tonic-gate struct pvt *pvt = (struct pvt *)this->private;
2097c478bd9Sstevel@tonic-gate
2107c478bd9Sstevel@tonic-gate if (!pvt->res) {
2117c478bd9Sstevel@tonic-gate struct __res_state *res;
2127c478bd9Sstevel@tonic-gate res = (struct __res_state *)malloc(sizeof *res);
2137c478bd9Sstevel@tonic-gate if (!res) {
2147c478bd9Sstevel@tonic-gate errno = ENOMEM;
2157c478bd9Sstevel@tonic-gate return (NULL);
2167c478bd9Sstevel@tonic-gate }
2177c478bd9Sstevel@tonic-gate memset(res, 0, sizeof *res);
2187c478bd9Sstevel@tonic-gate nw_res_set(this, res, free);
2197c478bd9Sstevel@tonic-gate }
2207c478bd9Sstevel@tonic-gate
2217c478bd9Sstevel@tonic-gate return (pvt->res);
2227c478bd9Sstevel@tonic-gate }
2237c478bd9Sstevel@tonic-gate
2247c478bd9Sstevel@tonic-gate static void
nw_res_set(struct irs_nw * this,struct __res_state * res,void (* free_res)(void *))2257c478bd9Sstevel@tonic-gate nw_res_set(struct irs_nw *this, struct __res_state *res,
2267c478bd9Sstevel@tonic-gate void (*free_res)(void *)) {
2277c478bd9Sstevel@tonic-gate struct pvt *pvt = (struct pvt *)this->private;
2287c478bd9Sstevel@tonic-gate
2297c478bd9Sstevel@tonic-gate if (pvt->res && pvt->free_res) {
2307c478bd9Sstevel@tonic-gate res_nclose(pvt->res);
2317c478bd9Sstevel@tonic-gate (*pvt->free_res)(pvt->res);
2327c478bd9Sstevel@tonic-gate }
2337c478bd9Sstevel@tonic-gate
2347c478bd9Sstevel@tonic-gate pvt->res = res;
2357c478bd9Sstevel@tonic-gate pvt->free_res = free_res;
2367c478bd9Sstevel@tonic-gate }
2377c478bd9Sstevel@tonic-gate
2387c478bd9Sstevel@tonic-gate /* Private. */
2397c478bd9Sstevel@tonic-gate
2407c478bd9Sstevel@tonic-gate static struct nwent *
get1101byname(struct irs_nw * this,const char * name)2417c478bd9Sstevel@tonic-gate get1101byname(struct irs_nw *this, const char *name) {
2427c478bd9Sstevel@tonic-gate struct pvt *pvt = (struct pvt *)this->private;
2437c478bd9Sstevel@tonic-gate u_char *ansbuf;
2447c478bd9Sstevel@tonic-gate int anslen;
2457c478bd9Sstevel@tonic-gate struct nwent *result;
2467c478bd9Sstevel@tonic-gate
2477c478bd9Sstevel@tonic-gate ansbuf = memget(MAXPACKET);
2487c478bd9Sstevel@tonic-gate if (ansbuf == NULL) {
2497c478bd9Sstevel@tonic-gate errno = ENOMEM;
2507c478bd9Sstevel@tonic-gate RES_SET_H_ERRNO(pvt->res, NETDB_INTERNAL);
2517c478bd9Sstevel@tonic-gate return (NULL);
2527c478bd9Sstevel@tonic-gate }
2537c478bd9Sstevel@tonic-gate anslen = res_nsearch(pvt->res, name, C_IN, T_PTR, ansbuf, MAXPACKET);
2547c478bd9Sstevel@tonic-gate if (anslen < 0) {
2557c478bd9Sstevel@tonic-gate memput(ansbuf, MAXPACKET);
2567c478bd9Sstevel@tonic-gate return (NULL);
2577c478bd9Sstevel@tonic-gate }
2587c478bd9Sstevel@tonic-gate result = get1101mask(this, get1101answer(this, ansbuf, anslen, by_name,
2597c478bd9Sstevel@tonic-gate AF_INET, name, NULL, 0));
2607c478bd9Sstevel@tonic-gate memput(ansbuf, MAXPACKET);
2617c478bd9Sstevel@tonic-gate return (result);
2627c478bd9Sstevel@tonic-gate }
2637c478bd9Sstevel@tonic-gate
2647c478bd9Sstevel@tonic-gate static struct nwent *
get1101byaddr(struct irs_nw * this,u_char * net,int len)2657c478bd9Sstevel@tonic-gate get1101byaddr(struct irs_nw *this, u_char *net, int len) {
2667c478bd9Sstevel@tonic-gate struct pvt *pvt = (struct pvt *)this->private;
2677c478bd9Sstevel@tonic-gate char qbuf[sizeof "255.255.255.255.in-addr.arpa"];
2687c478bd9Sstevel@tonic-gate struct nwent *result;
2697c478bd9Sstevel@tonic-gate u_char *ansbuf;
2707c478bd9Sstevel@tonic-gate int anslen;
2717c478bd9Sstevel@tonic-gate
2727c478bd9Sstevel@tonic-gate if (len < 1 || len > 32) {
2737c478bd9Sstevel@tonic-gate errno = EINVAL;
2747c478bd9Sstevel@tonic-gate RES_SET_H_ERRNO(pvt->res, NETDB_INTERNAL);
2757c478bd9Sstevel@tonic-gate return (NULL);
2767c478bd9Sstevel@tonic-gate }
2777c478bd9Sstevel@tonic-gate if (make1101inaddr(net, len, qbuf, sizeof qbuf) < 0)
2787c478bd9Sstevel@tonic-gate return (NULL);
2797c478bd9Sstevel@tonic-gate ansbuf = memget(MAXPACKET);
2807c478bd9Sstevel@tonic-gate if (ansbuf == NULL) {
2817c478bd9Sstevel@tonic-gate errno = ENOMEM;
2827c478bd9Sstevel@tonic-gate RES_SET_H_ERRNO(pvt->res, NETDB_INTERNAL);
2837c478bd9Sstevel@tonic-gate return (NULL);
2847c478bd9Sstevel@tonic-gate }
2857c478bd9Sstevel@tonic-gate anslen = res_nquery(pvt->res, qbuf, C_IN, T_PTR, ansbuf, MAXPACKET);
2867c478bd9Sstevel@tonic-gate if (anslen < 0) {
2877c478bd9Sstevel@tonic-gate memput(ansbuf, MAXPACKET);
2887c478bd9Sstevel@tonic-gate return (NULL);
2897c478bd9Sstevel@tonic-gate }
2907c478bd9Sstevel@tonic-gate result = get1101mask(this, get1101answer(this, ansbuf, anslen, by_addr,
2917c478bd9Sstevel@tonic-gate AF_INET, NULL, net, len));
2927c478bd9Sstevel@tonic-gate memput(ansbuf, MAXPACKET);
2937c478bd9Sstevel@tonic-gate return (result);
2947c478bd9Sstevel@tonic-gate }
2957c478bd9Sstevel@tonic-gate
2967c478bd9Sstevel@tonic-gate static struct nwent *
get1101answer(struct irs_nw * this,u_char * ansbuf,int anslen,enum by_what by_what,int af,const char * name,const u_char * addr,int addrlen)2977c478bd9Sstevel@tonic-gate get1101answer(struct irs_nw *this,
2987c478bd9Sstevel@tonic-gate u_char *ansbuf, int anslen, enum by_what by_what,
2997c478bd9Sstevel@tonic-gate int af, const char *name, const u_char *addr, int addrlen)
3007c478bd9Sstevel@tonic-gate {
3017c478bd9Sstevel@tonic-gate struct pvt *pvt = (struct pvt *)this->private;
3027c478bd9Sstevel@tonic-gate int type, class, ancount, qdcount, haveanswer;
3037c478bd9Sstevel@tonic-gate char *bp, *ep, **ap;
3047c478bd9Sstevel@tonic-gate u_char *cp, *eom;
3057c478bd9Sstevel@tonic-gate HEADER *hp;
3067c478bd9Sstevel@tonic-gate
3077c478bd9Sstevel@tonic-gate /* Initialize, and parse header. */
3087c478bd9Sstevel@tonic-gate eom = ansbuf + anslen;
3097c478bd9Sstevel@tonic-gate if (ansbuf + HFIXEDSZ > eom) {
3107c478bd9Sstevel@tonic-gate RES_SET_H_ERRNO(pvt->res, NO_RECOVERY);
3117c478bd9Sstevel@tonic-gate return (NULL);
3127c478bd9Sstevel@tonic-gate }
3137c478bd9Sstevel@tonic-gate hp = (HEADER *)ansbuf;
3147c478bd9Sstevel@tonic-gate cp = ansbuf + HFIXEDSZ;
3157c478bd9Sstevel@tonic-gate qdcount = ntohs(hp->qdcount);
3167c478bd9Sstevel@tonic-gate while (qdcount-- > 0) {
3177c478bd9Sstevel@tonic-gate int n = dn_skipname(cp, eom);
3187c478bd9Sstevel@tonic-gate cp += n + QFIXEDSZ;
3197c478bd9Sstevel@tonic-gate if (n < 0 || cp > eom) {
3207c478bd9Sstevel@tonic-gate RES_SET_H_ERRNO(pvt->res, NO_RECOVERY);
3217c478bd9Sstevel@tonic-gate return (NULL);
3227c478bd9Sstevel@tonic-gate }
3237c478bd9Sstevel@tonic-gate }
3247c478bd9Sstevel@tonic-gate ancount = ntohs(hp->ancount);
3257c478bd9Sstevel@tonic-gate if (!ancount) {
3267c478bd9Sstevel@tonic-gate if (hp->aa)
3277c478bd9Sstevel@tonic-gate RES_SET_H_ERRNO(pvt->res, HOST_NOT_FOUND);
3287c478bd9Sstevel@tonic-gate else
3297c478bd9Sstevel@tonic-gate RES_SET_H_ERRNO(pvt->res, TRY_AGAIN);
3307c478bd9Sstevel@tonic-gate return (NULL);
3317c478bd9Sstevel@tonic-gate }
3327c478bd9Sstevel@tonic-gate
3337c478bd9Sstevel@tonic-gate /* Prepare a return structure. */
3347c478bd9Sstevel@tonic-gate bp = pvt->buf;
3357c478bd9Sstevel@tonic-gate ep = pvt->buf + sizeof(pvt->buf);
3367c478bd9Sstevel@tonic-gate pvt->net.n_name = NULL;
3377c478bd9Sstevel@tonic-gate pvt->net.n_aliases = pvt->ali;
3387c478bd9Sstevel@tonic-gate pvt->net.n_addrtype = af;
3397c478bd9Sstevel@tonic-gate pvt->net.n_addr = NULL;
3407c478bd9Sstevel@tonic-gate pvt->net.n_length = addrlen;
3417c478bd9Sstevel@tonic-gate
3427c478bd9Sstevel@tonic-gate /* Save input key if given. */
3437c478bd9Sstevel@tonic-gate switch (by_what) {
3447c478bd9Sstevel@tonic-gate case by_name:
3457c478bd9Sstevel@tonic-gate if (name != NULL) {
3467c478bd9Sstevel@tonic-gate int n = strlen(name) + 1;
3477c478bd9Sstevel@tonic-gate
3487c478bd9Sstevel@tonic-gate if (n > (ep - bp)) {
3497c478bd9Sstevel@tonic-gate RES_SET_H_ERRNO(pvt->res, NO_RECOVERY);
3507c478bd9Sstevel@tonic-gate return (NULL);
3517c478bd9Sstevel@tonic-gate }
352*9525b14bSRao Shoaib pvt->net.n_name = strcpy(bp, name); /* (checked) */
3537c478bd9Sstevel@tonic-gate bp += n;
3547c478bd9Sstevel@tonic-gate }
3557c478bd9Sstevel@tonic-gate break;
3567c478bd9Sstevel@tonic-gate case by_addr:
3577c478bd9Sstevel@tonic-gate if (addr != NULL && addrlen != 0) {
3587c478bd9Sstevel@tonic-gate int n = addrlen / 8 + ((addrlen % 8) != 0);
3597c478bd9Sstevel@tonic-gate
3607c478bd9Sstevel@tonic-gate if (INADDRSZ > (ep - bp)) {
3617c478bd9Sstevel@tonic-gate RES_SET_H_ERRNO(pvt->res, NO_RECOVERY);
3627c478bd9Sstevel@tonic-gate return (NULL);
3637c478bd9Sstevel@tonic-gate }
3647c478bd9Sstevel@tonic-gate memset(bp, 0, INADDRSZ);
3657c478bd9Sstevel@tonic-gate memcpy(bp, addr, n);
3667c478bd9Sstevel@tonic-gate pvt->net.n_addr = bp;
3677c478bd9Sstevel@tonic-gate bp += INADDRSZ;
3687c478bd9Sstevel@tonic-gate }
3697c478bd9Sstevel@tonic-gate break;
3707c478bd9Sstevel@tonic-gate default:
3717c478bd9Sstevel@tonic-gate abort();
3727c478bd9Sstevel@tonic-gate }
3737c478bd9Sstevel@tonic-gate
3747c478bd9Sstevel@tonic-gate /* Parse the answer, collect aliases. */
3757c478bd9Sstevel@tonic-gate ap = pvt->ali;
3767c478bd9Sstevel@tonic-gate haveanswer = 0;
3777c478bd9Sstevel@tonic-gate while (--ancount >= 0 && cp < eom) {
3787c478bd9Sstevel@tonic-gate int n = dn_expand(ansbuf, eom, cp, bp, ep - bp);
3797c478bd9Sstevel@tonic-gate
380*9525b14bSRao Shoaib cp += n; /*%< Owner */
3817c478bd9Sstevel@tonic-gate if (n < 0 || !maybe_dnok(pvt->res, bp) ||
3827c478bd9Sstevel@tonic-gate cp + 3 * INT16SZ + INT32SZ > eom) {
3837c478bd9Sstevel@tonic-gate RES_SET_H_ERRNO(pvt->res, NO_RECOVERY);
3847c478bd9Sstevel@tonic-gate return (NULL);
3857c478bd9Sstevel@tonic-gate }
386*9525b14bSRao Shoaib GETSHORT(type, cp); /*%< Type */
387*9525b14bSRao Shoaib GETSHORT(class, cp); /*%< Class */
388*9525b14bSRao Shoaib cp += INT32SZ; /*%< TTL */
389*9525b14bSRao Shoaib GETSHORT(n, cp); /*%< RDLENGTH */
3907c478bd9Sstevel@tonic-gate if (class == C_IN && type == T_PTR) {
3917c478bd9Sstevel@tonic-gate int nn;
3927c478bd9Sstevel@tonic-gate
3937c478bd9Sstevel@tonic-gate nn = dn_expand(ansbuf, eom, cp, bp, ep - bp);
3947c478bd9Sstevel@tonic-gate if (nn < 0 || !maybe_hnok(pvt->res, bp) || nn != n) {
3957c478bd9Sstevel@tonic-gate RES_SET_H_ERRNO(pvt->res, NO_RECOVERY);
3967c478bd9Sstevel@tonic-gate return (NULL);
3977c478bd9Sstevel@tonic-gate }
3987c478bd9Sstevel@tonic-gate normalize_name(bp);
3997c478bd9Sstevel@tonic-gate switch (by_what) {
4007c478bd9Sstevel@tonic-gate case by_addr: {
4017c478bd9Sstevel@tonic-gate if (pvt->net.n_name == NULL)
4027c478bd9Sstevel@tonic-gate pvt->net.n_name = bp;
4037c478bd9Sstevel@tonic-gate else if (ns_samename(pvt->net.n_name, bp) == 1)
4047c478bd9Sstevel@tonic-gate break;
4057c478bd9Sstevel@tonic-gate else
4067c478bd9Sstevel@tonic-gate *ap++ = bp;
4077c478bd9Sstevel@tonic-gate nn = strlen(bp) + 1;
4087c478bd9Sstevel@tonic-gate bp += nn;
4097c478bd9Sstevel@tonic-gate haveanswer++;
4107c478bd9Sstevel@tonic-gate break;
4117c478bd9Sstevel@tonic-gate }
4127c478bd9Sstevel@tonic-gate case by_name: {
4137c478bd9Sstevel@tonic-gate u_int b1, b2, b3, b4;
4147c478bd9Sstevel@tonic-gate
4157c478bd9Sstevel@tonic-gate if (pvt->net.n_addr != NULL ||
4167c478bd9Sstevel@tonic-gate sscanf(bp, "%u.%u.%u.%u.in-addr.arpa",
4177c478bd9Sstevel@tonic-gate &b1, &b2, &b3, &b4) != 4)
4187c478bd9Sstevel@tonic-gate break;
4197c478bd9Sstevel@tonic-gate if ((ep - bp) < INADDRSZ) {
4207c478bd9Sstevel@tonic-gate RES_SET_H_ERRNO(pvt->res, NO_RECOVERY);
4217c478bd9Sstevel@tonic-gate return (NULL);
4227c478bd9Sstevel@tonic-gate }
4237c478bd9Sstevel@tonic-gate pvt->net.n_addr = bp;
4247c478bd9Sstevel@tonic-gate *bp++ = b4;
4257c478bd9Sstevel@tonic-gate *bp++ = b3;
4267c478bd9Sstevel@tonic-gate *bp++ = b2;
4277c478bd9Sstevel@tonic-gate *bp++ = b1;
4287c478bd9Sstevel@tonic-gate pvt->net.n_length = INADDRSZ * 8;
4297c478bd9Sstevel@tonic-gate haveanswer++;
4307c478bd9Sstevel@tonic-gate }
4317c478bd9Sstevel@tonic-gate }
4327c478bd9Sstevel@tonic-gate }
433*9525b14bSRao Shoaib cp += n; /*%< RDATA */
4347c478bd9Sstevel@tonic-gate }
4357c478bd9Sstevel@tonic-gate if (!haveanswer) {
4367c478bd9Sstevel@tonic-gate RES_SET_H_ERRNO(pvt->res, TRY_AGAIN);
4377c478bd9Sstevel@tonic-gate return (NULL);
4387c478bd9Sstevel@tonic-gate }
4397c478bd9Sstevel@tonic-gate *ap = NULL;
4407c478bd9Sstevel@tonic-gate
4417c478bd9Sstevel@tonic-gate return (&pvt->net);
4427c478bd9Sstevel@tonic-gate }
4437c478bd9Sstevel@tonic-gate
4447c478bd9Sstevel@tonic-gate static struct nwent *
get1101mask(struct irs_nw * this,struct nwent * nwent)4457c478bd9Sstevel@tonic-gate get1101mask(struct irs_nw *this, struct nwent *nwent) {
4467c478bd9Sstevel@tonic-gate struct pvt *pvt = (struct pvt *)this->private;
4477c478bd9Sstevel@tonic-gate char qbuf[sizeof "255.255.255.255.in-addr.arpa"], owner[MAXDNAME];
4487c478bd9Sstevel@tonic-gate int anslen, type, class, ancount, qdcount;
4497c478bd9Sstevel@tonic-gate u_char *ansbuf, *cp, *eom;
4507c478bd9Sstevel@tonic-gate HEADER *hp;
4517c478bd9Sstevel@tonic-gate
4527c478bd9Sstevel@tonic-gate if (!nwent)
4537c478bd9Sstevel@tonic-gate return (NULL);
4547c478bd9Sstevel@tonic-gate if (make1101inaddr(nwent->n_addr, nwent->n_length, qbuf, sizeof qbuf)
4557c478bd9Sstevel@tonic-gate < 0) {
4567c478bd9Sstevel@tonic-gate /* "First, do no harm." */
4577c478bd9Sstevel@tonic-gate return (nwent);
4587c478bd9Sstevel@tonic-gate }
4597c478bd9Sstevel@tonic-gate
4607c478bd9Sstevel@tonic-gate ansbuf = memget(MAXPACKET);
4617c478bd9Sstevel@tonic-gate if (ansbuf == NULL) {
4627c478bd9Sstevel@tonic-gate errno = ENOMEM;
4637c478bd9Sstevel@tonic-gate RES_SET_H_ERRNO(pvt->res, NETDB_INTERNAL);
4647c478bd9Sstevel@tonic-gate return (NULL);
4657c478bd9Sstevel@tonic-gate }
4667c478bd9Sstevel@tonic-gate /* Query for the A RR that would hold this network's mask. */
4677c478bd9Sstevel@tonic-gate anslen = res_nquery(pvt->res, qbuf, C_IN, T_A, ansbuf, MAXPACKET);
4687c478bd9Sstevel@tonic-gate if (anslen < HFIXEDSZ) {
4697c478bd9Sstevel@tonic-gate memput(ansbuf, MAXPACKET);
4707c478bd9Sstevel@tonic-gate return (nwent);
4717c478bd9Sstevel@tonic-gate }
4727c478bd9Sstevel@tonic-gate
4737c478bd9Sstevel@tonic-gate /* Initialize, and parse header. */
4747c478bd9Sstevel@tonic-gate hp = (HEADER *)ansbuf;
4757c478bd9Sstevel@tonic-gate cp = ansbuf + HFIXEDSZ;
4767c478bd9Sstevel@tonic-gate eom = ansbuf + anslen;
4777c478bd9Sstevel@tonic-gate qdcount = ntohs(hp->qdcount);
4787c478bd9Sstevel@tonic-gate while (qdcount-- > 0) {
4797c478bd9Sstevel@tonic-gate int n = dn_skipname(cp, eom);
4807c478bd9Sstevel@tonic-gate cp += n + QFIXEDSZ;
4817c478bd9Sstevel@tonic-gate if (n < 0 || cp > eom) {
4827c478bd9Sstevel@tonic-gate memput(ansbuf, MAXPACKET);
4837c478bd9Sstevel@tonic-gate return (nwent);
4847c478bd9Sstevel@tonic-gate }
4857c478bd9Sstevel@tonic-gate }
4867c478bd9Sstevel@tonic-gate ancount = ntohs(hp->ancount);
4877c478bd9Sstevel@tonic-gate
4887c478bd9Sstevel@tonic-gate /* Parse the answer, collect aliases. */
4897c478bd9Sstevel@tonic-gate while (--ancount >= 0 && cp < eom) {
4907c478bd9Sstevel@tonic-gate int n = dn_expand(ansbuf, eom, cp, owner, sizeof owner);
4917c478bd9Sstevel@tonic-gate
4927c478bd9Sstevel@tonic-gate if (n < 0 || !maybe_dnok(pvt->res, owner))
4937c478bd9Sstevel@tonic-gate break;
494*9525b14bSRao Shoaib cp += n; /*%< Owner */
4957c478bd9Sstevel@tonic-gate if (cp + 3 * INT16SZ + INT32SZ > eom)
4967c478bd9Sstevel@tonic-gate break;
497*9525b14bSRao Shoaib GETSHORT(type, cp); /*%< Type */
498*9525b14bSRao Shoaib GETSHORT(class, cp); /*%< Class */
499*9525b14bSRao Shoaib cp += INT32SZ; /*%< TTL */
500*9525b14bSRao Shoaib GETSHORT(n, cp); /*%< RDLENGTH */
5017c478bd9Sstevel@tonic-gate if (cp + n > eom)
5027c478bd9Sstevel@tonic-gate break;
5037c478bd9Sstevel@tonic-gate if (n == INADDRSZ && class == C_IN && type == T_A &&
5047c478bd9Sstevel@tonic-gate ns_samename(qbuf, owner) == 1) {
5057c478bd9Sstevel@tonic-gate /* This A RR indicates the actual netmask. */
5067c478bd9Sstevel@tonic-gate int nn, mm;
5077c478bd9Sstevel@tonic-gate
5087c478bd9Sstevel@tonic-gate nwent->n_length = 0;
5097c478bd9Sstevel@tonic-gate for (nn = 0; nn < INADDRSZ; nn++)
5107c478bd9Sstevel@tonic-gate for (mm = 7; mm >= 0; mm--)
5117c478bd9Sstevel@tonic-gate if (cp[nn] & (1 << mm))
5127c478bd9Sstevel@tonic-gate nwent->n_length++;
5137c478bd9Sstevel@tonic-gate else
5147c478bd9Sstevel@tonic-gate break;
5157c478bd9Sstevel@tonic-gate }
516*9525b14bSRao Shoaib cp += n; /*%< RDATA */
5177c478bd9Sstevel@tonic-gate }
5187c478bd9Sstevel@tonic-gate memput(ansbuf, MAXPACKET);
5197c478bd9Sstevel@tonic-gate return (nwent);
5207c478bd9Sstevel@tonic-gate }
5217c478bd9Sstevel@tonic-gate
5227c478bd9Sstevel@tonic-gate static int
make1101inaddr(const u_char * net,int bits,char * name,int size)5237c478bd9Sstevel@tonic-gate make1101inaddr(const u_char *net, int bits, char *name, int size) {
5247c478bd9Sstevel@tonic-gate int n, m;
5257c478bd9Sstevel@tonic-gate char *ep;
5267c478bd9Sstevel@tonic-gate
5277c478bd9Sstevel@tonic-gate ep = name + size;
5287c478bd9Sstevel@tonic-gate
5297c478bd9Sstevel@tonic-gate /* Zero fill any whole bytes left out of the prefix. */
5307c478bd9Sstevel@tonic-gate for (n = (32 - bits) / 8; n > 0; n--) {
5317c478bd9Sstevel@tonic-gate if (ep - name < (int)(sizeof "0."))
5327c478bd9Sstevel@tonic-gate goto emsgsize;
5337c478bd9Sstevel@tonic-gate m = SPRINTF((name, "0."));
5347c478bd9Sstevel@tonic-gate name += m;
5357c478bd9Sstevel@tonic-gate }
5367c478bd9Sstevel@tonic-gate
5377c478bd9Sstevel@tonic-gate /* Format the partial byte, if any, within the prefix. */
5387c478bd9Sstevel@tonic-gate if ((n = bits % 8) != 0) {
5397c478bd9Sstevel@tonic-gate if (ep - name < (int)(sizeof "255."))
5407c478bd9Sstevel@tonic-gate goto emsgsize;
5417c478bd9Sstevel@tonic-gate m = SPRINTF((name, "%u.",
5427c478bd9Sstevel@tonic-gate net[bits / 8] & ~((1 << (8 - n)) - 1)));
5437c478bd9Sstevel@tonic-gate name += m;
5447c478bd9Sstevel@tonic-gate }
5457c478bd9Sstevel@tonic-gate
5467c478bd9Sstevel@tonic-gate /* Format the whole bytes within the prefix. */
5477c478bd9Sstevel@tonic-gate for (n = bits / 8; n > 0; n--) {
5487c478bd9Sstevel@tonic-gate if (ep - name < (int)(sizeof "255."))
5497c478bd9Sstevel@tonic-gate goto emsgsize;
5507c478bd9Sstevel@tonic-gate m = SPRINTF((name, "%u.", net[n - 1]));
5517c478bd9Sstevel@tonic-gate name += m;
5527c478bd9Sstevel@tonic-gate }
5537c478bd9Sstevel@tonic-gate
5547c478bd9Sstevel@tonic-gate /* Add the static text. */
5557c478bd9Sstevel@tonic-gate if (ep - name < (int)(sizeof "in-addr.arpa"))
5567c478bd9Sstevel@tonic-gate goto emsgsize;
5577c478bd9Sstevel@tonic-gate (void) SPRINTF((name, "in-addr.arpa"));
5587c478bd9Sstevel@tonic-gate return (0);
5597c478bd9Sstevel@tonic-gate
5607c478bd9Sstevel@tonic-gate emsgsize:
5617c478bd9Sstevel@tonic-gate errno = EMSGSIZE;
5627c478bd9Sstevel@tonic-gate return (-1);
5637c478bd9Sstevel@tonic-gate }
5647c478bd9Sstevel@tonic-gate
5657c478bd9Sstevel@tonic-gate static void
normalize_name(char * name)5667c478bd9Sstevel@tonic-gate normalize_name(char *name) {
5677c478bd9Sstevel@tonic-gate char *t;
5687c478bd9Sstevel@tonic-gate
5697c478bd9Sstevel@tonic-gate /* Make lower case. */
5707c478bd9Sstevel@tonic-gate for (t = name; *t; t++)
5717c478bd9Sstevel@tonic-gate if (isascii((unsigned char)*t) && isupper((unsigned char)*t))
572*9525b14bSRao Shoaib *t = tolower((*t)&0xff);
5737c478bd9Sstevel@tonic-gate
5747c478bd9Sstevel@tonic-gate /* Remove trailing dots. */
5757c478bd9Sstevel@tonic-gate while (t > name && t[-1] == '.')
5767c478bd9Sstevel@tonic-gate *--t = '\0';
5777c478bd9Sstevel@tonic-gate }
5787c478bd9Sstevel@tonic-gate
5797c478bd9Sstevel@tonic-gate static int
init(struct irs_nw * this)5807c478bd9Sstevel@tonic-gate init(struct irs_nw *this) {
5817c478bd9Sstevel@tonic-gate struct pvt *pvt = (struct pvt *)this->private;
5827c478bd9Sstevel@tonic-gate
5837c478bd9Sstevel@tonic-gate if (!pvt->res && !nw_res_get(this))
5847c478bd9Sstevel@tonic-gate return (-1);
585*9525b14bSRao Shoaib if (((pvt->res->options & RES_INIT) == 0U) &&
5867c478bd9Sstevel@tonic-gate res_ninit(pvt->res) == -1)
5877c478bd9Sstevel@tonic-gate return (-1);
5887c478bd9Sstevel@tonic-gate return (0);
5897c478bd9Sstevel@tonic-gate }
590*9525b14bSRao Shoaib
591*9525b14bSRao Shoaib /*! \file */
592