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