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 
18*9525b14bSRao Shoaib /*! \file
19*9525b14bSRao Shoaib  * \brief
207c478bd9Sstevel@tonic-gate  * Based on the Dynamic DNS reference implementation by Viraj Bais
21*9525b14bSRao Shoaib  * <viraj_bais@ccm.fm.intel.com>
227c478bd9Sstevel@tonic-gate  */
237c478bd9Sstevel@tonic-gate 
247c478bd9Sstevel@tonic-gate #include "port_before.h"
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #include <sys/types.h>
277c478bd9Sstevel@tonic-gate #include <sys/param.h>
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate #include <netinet/in.h>
307c478bd9Sstevel@tonic-gate #include <arpa/nameser.h>
317c478bd9Sstevel@tonic-gate #include <arpa/inet.h>
327c478bd9Sstevel@tonic-gate 
337c478bd9Sstevel@tonic-gate #include <errno.h>
347c478bd9Sstevel@tonic-gate #include <limits.h>
357c478bd9Sstevel@tonic-gate #include <netdb.h>
367c478bd9Sstevel@tonic-gate #include <resolv.h>
377c478bd9Sstevel@tonic-gate #include <res_update.h>
387c478bd9Sstevel@tonic-gate #include <stdio.h>
397c478bd9Sstevel@tonic-gate #include <stdlib.h>
407c478bd9Sstevel@tonic-gate #include <string.h>
417c478bd9Sstevel@tonic-gate #include <unistd.h>
427c478bd9Sstevel@tonic-gate #include <ctype.h>
437c478bd9Sstevel@tonic-gate 
447c478bd9Sstevel@tonic-gate #include "port_after.h"
457c478bd9Sstevel@tonic-gate 
467c478bd9Sstevel@tonic-gate /* Options.  Leave them on. */
477c478bd9Sstevel@tonic-gate #define DEBUG
487c478bd9Sstevel@tonic-gate #define MAXPORT 1024
497c478bd9Sstevel@tonic-gate 
507c478bd9Sstevel@tonic-gate static int getnum_str(u_char **, u_char *);
517c478bd9Sstevel@tonic-gate static int gethexnum_str(u_char **, u_char *);
527c478bd9Sstevel@tonic-gate static int getword_str(char *, int, u_char **, u_char *);
537c478bd9Sstevel@tonic-gate static int getstr_str(char *, int, u_char **, u_char *);
547c478bd9Sstevel@tonic-gate 
557c478bd9Sstevel@tonic-gate #define ShrinkBuffer(x)  if ((buflen -= x) < 0) return (-2);
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate /* Forward. */
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate int res_protocolnumber(const char *);
607c478bd9Sstevel@tonic-gate int res_servicenumber(const char *);
617c478bd9Sstevel@tonic-gate 
62*9525b14bSRao Shoaib /*%
637c478bd9Sstevel@tonic-gate  * Form update packets.
647c478bd9Sstevel@tonic-gate  * Returns the size of the resulting packet if no error
65*9525b14bSRao Shoaib  *
667c478bd9Sstevel@tonic-gate  * On error,
67*9525b14bSRao Shoaib  *	returns
68*9525b14bSRao Shoaib  *\li              -1 if error in reading a word/number in rdata
697c478bd9Sstevel@tonic-gate  *		   portion for update packets
70*9525b14bSRao Shoaib  *\li		-2 if length of buffer passed is insufficient
71*9525b14bSRao Shoaib  *\li		-3 if zone section is not the first section in
727c478bd9Sstevel@tonic-gate  *		   the linked list, or section order has a problem
73*9525b14bSRao Shoaib  *\li		-4 on a number overflow
74*9525b14bSRao Shoaib  *\li		-5 unknown operation or no records
757c478bd9Sstevel@tonic-gate  */
767c478bd9Sstevel@tonic-gate int
res_nmkupdate(res_state statp,ns_updrec * rrecp_in,u_char * buf,int buflen)777c478bd9Sstevel@tonic-gate res_nmkupdate(res_state statp, ns_updrec *rrecp_in, u_char *buf, int buflen) {
787c478bd9Sstevel@tonic-gate 	ns_updrec *rrecp_start = rrecp_in;
797c478bd9Sstevel@tonic-gate 	HEADER *hp;
80*9525b14bSRao Shoaib 	u_char *cp, *sp2, *startp, *endp;
817c478bd9Sstevel@tonic-gate 	int n, i, soanum, multiline;
827c478bd9Sstevel@tonic-gate 	ns_updrec *rrecp;
837c478bd9Sstevel@tonic-gate 	struct in_addr ina;
847c478bd9Sstevel@tonic-gate 	struct in6_addr in6a;
857c478bd9Sstevel@tonic-gate         char buf2[MAXDNAME];
867c478bd9Sstevel@tonic-gate 	u_char buf3[MAXDNAME];
877c478bd9Sstevel@tonic-gate 	int section, numrrs = 0, counts[ns_s_max];
887c478bd9Sstevel@tonic-gate 	u_int16_t rtype, rclass;
897c478bd9Sstevel@tonic-gate 	u_int32_t n1, rttl;
907c478bd9Sstevel@tonic-gate 	u_char *dnptrs[20], **dpp, **lastdnptr;
917c478bd9Sstevel@tonic-gate 	int siglen, keylen, certlen;
927c478bd9Sstevel@tonic-gate 
937c478bd9Sstevel@tonic-gate 	/*
947c478bd9Sstevel@tonic-gate 	 * Initialize header fields.
957c478bd9Sstevel@tonic-gate 	 */
967c478bd9Sstevel@tonic-gate 	if ((buf == NULL) || (buflen < HFIXEDSZ))
977c478bd9Sstevel@tonic-gate 		return (-1);
987c478bd9Sstevel@tonic-gate 	memset(buf, 0, HFIXEDSZ);
997c478bd9Sstevel@tonic-gate 	hp = (HEADER *) buf;
100*9525b14bSRao Shoaib 	statp->id = res_nrandomid(statp);
101*9525b14bSRao Shoaib 	hp->id = htons(statp->id);
1027c478bd9Sstevel@tonic-gate 	hp->opcode = ns_o_update;
1037c478bd9Sstevel@tonic-gate 	hp->rcode = NOERROR;
1047c478bd9Sstevel@tonic-gate 	cp = buf + HFIXEDSZ;
1057c478bd9Sstevel@tonic-gate 	buflen -= HFIXEDSZ;
1067c478bd9Sstevel@tonic-gate 	dpp = dnptrs;
1077c478bd9Sstevel@tonic-gate 	*dpp++ = buf;
1087c478bd9Sstevel@tonic-gate 	*dpp++ = NULL;
1097c478bd9Sstevel@tonic-gate 	lastdnptr = dnptrs + sizeof dnptrs / sizeof dnptrs[0];
1107c478bd9Sstevel@tonic-gate 
1117c478bd9Sstevel@tonic-gate 	if (rrecp_start == NULL)
1127c478bd9Sstevel@tonic-gate 		return (-5);
1137c478bd9Sstevel@tonic-gate 	else if (rrecp_start->r_section != S_ZONE)
1147c478bd9Sstevel@tonic-gate 		return (-3);
1157c478bd9Sstevel@tonic-gate 
1167c478bd9Sstevel@tonic-gate 	memset(counts, 0, sizeof counts);
1177c478bd9Sstevel@tonic-gate 	for (rrecp = rrecp_start; rrecp; rrecp = NEXT(rrecp, r_glink)) {
1187c478bd9Sstevel@tonic-gate 		numrrs++;
1197c478bd9Sstevel@tonic-gate                 section = rrecp->r_section;
1207c478bd9Sstevel@tonic-gate 		if (section < 0 || section >= ns_s_max)
1217c478bd9Sstevel@tonic-gate 			return (-1);
1227c478bd9Sstevel@tonic-gate 		counts[section]++;
1237c478bd9Sstevel@tonic-gate 		for (i = section + 1; i < ns_s_max; i++)
1247c478bd9Sstevel@tonic-gate 			if (counts[i])
1257c478bd9Sstevel@tonic-gate 				return (-3);
1267c478bd9Sstevel@tonic-gate 		rtype = rrecp->r_type;
1277c478bd9Sstevel@tonic-gate 		rclass = rrecp->r_class;
1287c478bd9Sstevel@tonic-gate 		rttl = rrecp->r_ttl;
1297c478bd9Sstevel@tonic-gate 		/* overload class and type */
1307c478bd9Sstevel@tonic-gate 		if (section == S_PREREQ) {
1317c478bd9Sstevel@tonic-gate 			rttl = 0;
1327c478bd9Sstevel@tonic-gate 			switch (rrecp->r_opcode) {
1337c478bd9Sstevel@tonic-gate 			case YXDOMAIN:
1347c478bd9Sstevel@tonic-gate 				rclass = C_ANY;
1357c478bd9Sstevel@tonic-gate 				rtype = T_ANY;
1367c478bd9Sstevel@tonic-gate 				rrecp->r_size = 0;
1377c478bd9Sstevel@tonic-gate 				break;
1387c478bd9Sstevel@tonic-gate 			case NXDOMAIN:
1397c478bd9Sstevel@tonic-gate 				rclass = C_NONE;
1407c478bd9Sstevel@tonic-gate 				rtype = T_ANY;
1417c478bd9Sstevel@tonic-gate 				rrecp->r_size = 0;
1427c478bd9Sstevel@tonic-gate 				break;
1437c478bd9Sstevel@tonic-gate 			case NXRRSET:
1447c478bd9Sstevel@tonic-gate 				rclass = C_NONE;
1457c478bd9Sstevel@tonic-gate 				rrecp->r_size = 0;
1467c478bd9Sstevel@tonic-gate 				break;
1477c478bd9Sstevel@tonic-gate 			case YXRRSET:
1487c478bd9Sstevel@tonic-gate 				if (rrecp->r_size == 0)
1497c478bd9Sstevel@tonic-gate 					rclass = C_ANY;
1507c478bd9Sstevel@tonic-gate 				break;
1517c478bd9Sstevel@tonic-gate 			default:
1527c478bd9Sstevel@tonic-gate 				fprintf(stderr,
1537c478bd9Sstevel@tonic-gate 					"res_mkupdate: incorrect opcode: %d\n",
1547c478bd9Sstevel@tonic-gate 					rrecp->r_opcode);
1557c478bd9Sstevel@tonic-gate 				fflush(stderr);
1567c478bd9Sstevel@tonic-gate 				return (-1);
1577c478bd9Sstevel@tonic-gate 			}
1587c478bd9Sstevel@tonic-gate 		} else if (section == S_UPDATE) {
1597c478bd9Sstevel@tonic-gate 			switch (rrecp->r_opcode) {
1607c478bd9Sstevel@tonic-gate 			case DELETE:
1617c478bd9Sstevel@tonic-gate 				rclass = rrecp->r_size == 0 ? C_ANY : C_NONE;
1627c478bd9Sstevel@tonic-gate 				break;
1637c478bd9Sstevel@tonic-gate 			case ADD:
1647c478bd9Sstevel@tonic-gate 				break;
1657c478bd9Sstevel@tonic-gate 			default:
1667c478bd9Sstevel@tonic-gate 				fprintf(stderr,
1677c478bd9Sstevel@tonic-gate 					"res_mkupdate: incorrect opcode: %d\n",
1687c478bd9Sstevel@tonic-gate 					rrecp->r_opcode);
1697c478bd9Sstevel@tonic-gate 				fflush(stderr);
1707c478bd9Sstevel@tonic-gate 				return (-1);
1717c478bd9Sstevel@tonic-gate 			}
1727c478bd9Sstevel@tonic-gate 		}
1737c478bd9Sstevel@tonic-gate 
1747c478bd9Sstevel@tonic-gate 		/*
1757c478bd9Sstevel@tonic-gate 		 * XXX	appending default domain to owner name is omitted,
1767c478bd9Sstevel@tonic-gate 		 *	fqdn must be provided
1777c478bd9Sstevel@tonic-gate 		 */
1787c478bd9Sstevel@tonic-gate 		if ((n = dn_comp(rrecp->r_dname, cp, buflen, dnptrs,
1797c478bd9Sstevel@tonic-gate 				 lastdnptr)) < 0)
1807c478bd9Sstevel@tonic-gate 			return (-1);
1817c478bd9Sstevel@tonic-gate 		cp += n;
1827c478bd9Sstevel@tonic-gate 		ShrinkBuffer(n + 2*INT16SZ);
1837c478bd9Sstevel@tonic-gate 		PUTSHORT(rtype, cp);
1847c478bd9Sstevel@tonic-gate 		PUTSHORT(rclass, cp);
1857c478bd9Sstevel@tonic-gate 		if (section == S_ZONE) {
1867c478bd9Sstevel@tonic-gate 			if (numrrs != 1 || rrecp->r_type != T_SOA)
1877c478bd9Sstevel@tonic-gate 				return (-3);
1887c478bd9Sstevel@tonic-gate 			continue;
1897c478bd9Sstevel@tonic-gate 		}
1907c478bd9Sstevel@tonic-gate 		ShrinkBuffer(INT32SZ + INT16SZ);
1917c478bd9Sstevel@tonic-gate 		PUTLONG(rttl, cp);
192*9525b14bSRao Shoaib 		sp2 = cp;  /*%< save pointer to length byte */
1937c478bd9Sstevel@tonic-gate 		cp += INT16SZ;
1947c478bd9Sstevel@tonic-gate 		if (rrecp->r_size == 0) {
1957c478bd9Sstevel@tonic-gate 			if (section == S_UPDATE && rclass != C_ANY)
1967c478bd9Sstevel@tonic-gate 				return (-1);
1977c478bd9Sstevel@tonic-gate 			else {
1987c478bd9Sstevel@tonic-gate 				PUTSHORT(0, sp2);
1997c478bd9Sstevel@tonic-gate 				continue;
2007c478bd9Sstevel@tonic-gate 			}
2017c478bd9Sstevel@tonic-gate 		}
2027c478bd9Sstevel@tonic-gate 		startp = rrecp->r_data;
2037c478bd9Sstevel@tonic-gate 		endp = startp + rrecp->r_size - 1;
2047c478bd9Sstevel@tonic-gate 		/* XXX this should be done centrally. */
2057c478bd9Sstevel@tonic-gate 		switch (rrecp->r_type) {
2067c478bd9Sstevel@tonic-gate 		case T_A:
2077c478bd9Sstevel@tonic-gate 			if (!getword_str(buf2, sizeof buf2, &startp, endp))
2087c478bd9Sstevel@tonic-gate 				return (-1);
2097c478bd9Sstevel@tonic-gate 			if (!inet_aton(buf2, &ina))
2107c478bd9Sstevel@tonic-gate 				return (-1);
2117c478bd9Sstevel@tonic-gate 			n1 = ntohl(ina.s_addr);
2127c478bd9Sstevel@tonic-gate 			ShrinkBuffer(INT32SZ);
2137c478bd9Sstevel@tonic-gate 			PUTLONG(n1, cp);
2147c478bd9Sstevel@tonic-gate 			break;
2157c478bd9Sstevel@tonic-gate 		case T_CNAME:
2167c478bd9Sstevel@tonic-gate 		case T_MB:
2177c478bd9Sstevel@tonic-gate 		case T_MG:
2187c478bd9Sstevel@tonic-gate 		case T_MR:
2197c478bd9Sstevel@tonic-gate 		case T_NS:
2207c478bd9Sstevel@tonic-gate 		case T_PTR:
221*9525b14bSRao Shoaib 		case ns_t_dname:
2227c478bd9Sstevel@tonic-gate 			if (!getword_str(buf2, sizeof buf2, &startp, endp))
2237c478bd9Sstevel@tonic-gate 				return (-1);
2247c478bd9Sstevel@tonic-gate 			n = dn_comp(buf2, cp, buflen, dnptrs, lastdnptr);
2257c478bd9Sstevel@tonic-gate 			if (n < 0)
2267c478bd9Sstevel@tonic-gate 				return (-1);
2277c478bd9Sstevel@tonic-gate 			cp += n;
2287c478bd9Sstevel@tonic-gate 			ShrinkBuffer(n);
2297c478bd9Sstevel@tonic-gate 			break;
2307c478bd9Sstevel@tonic-gate 		case T_MINFO:
2317c478bd9Sstevel@tonic-gate 		case T_SOA:
2327c478bd9Sstevel@tonic-gate 		case T_RP:
2337c478bd9Sstevel@tonic-gate 			for (i = 0; i < 2; i++) {
2347c478bd9Sstevel@tonic-gate 				if (!getword_str(buf2, sizeof buf2, &startp,
2357c478bd9Sstevel@tonic-gate 						 endp))
2367c478bd9Sstevel@tonic-gate 				return (-1);
2377c478bd9Sstevel@tonic-gate 				n = dn_comp(buf2, cp, buflen,
2387c478bd9Sstevel@tonic-gate 					    dnptrs, lastdnptr);
2397c478bd9Sstevel@tonic-gate 				if (n < 0)
2407c478bd9Sstevel@tonic-gate 					return (-1);
2417c478bd9Sstevel@tonic-gate 				cp += n;
2427c478bd9Sstevel@tonic-gate 				ShrinkBuffer(n);
2437c478bd9Sstevel@tonic-gate 			}
2447c478bd9Sstevel@tonic-gate 			if (rrecp->r_type == T_SOA) {
2457c478bd9Sstevel@tonic-gate 				ShrinkBuffer(5 * INT32SZ);
2467c478bd9Sstevel@tonic-gate 				while (isspace(*startp) || !*startp)
2477c478bd9Sstevel@tonic-gate 					startp++;
2487c478bd9Sstevel@tonic-gate 				if (*startp == '(') {
2497c478bd9Sstevel@tonic-gate 					multiline = 1;
2507c478bd9Sstevel@tonic-gate 					startp++;
2517c478bd9Sstevel@tonic-gate 				} else
2527c478bd9Sstevel@tonic-gate 					multiline = 0;
2537c478bd9Sstevel@tonic-gate 				/* serial, refresh, retry, expire, minimum */
2547c478bd9Sstevel@tonic-gate 				for (i = 0; i < 5; i++) {
2557c478bd9Sstevel@tonic-gate 					soanum = getnum_str(&startp, endp);
2567c478bd9Sstevel@tonic-gate 					if (soanum < 0)
2577c478bd9Sstevel@tonic-gate 						return (-1);
2587c478bd9Sstevel@tonic-gate 					PUTLONG(soanum, cp);
2597c478bd9Sstevel@tonic-gate 				}
2607c478bd9Sstevel@tonic-gate 				if (multiline) {
2617c478bd9Sstevel@tonic-gate 					while (isspace(*startp) || !*startp)
2627c478bd9Sstevel@tonic-gate 						startp++;
2637c478bd9Sstevel@tonic-gate 					if (*startp != ')')
2647c478bd9Sstevel@tonic-gate 						return (-1);
2657c478bd9Sstevel@tonic-gate 				}
2667c478bd9Sstevel@tonic-gate 			}
2677c478bd9Sstevel@tonic-gate 			break;
2687c478bd9Sstevel@tonic-gate 		case T_MX:
2697c478bd9Sstevel@tonic-gate 		case T_AFSDB:
2707c478bd9Sstevel@tonic-gate 		case T_RT:
2717c478bd9Sstevel@tonic-gate 			n = getnum_str(&startp, endp);
2727c478bd9Sstevel@tonic-gate 			if (n < 0)
2737c478bd9Sstevel@tonic-gate 				return (-1);
2747c478bd9Sstevel@tonic-gate 			ShrinkBuffer(INT16SZ);
2757c478bd9Sstevel@tonic-gate 			PUTSHORT(n, cp);
2767c478bd9Sstevel@tonic-gate 			if (!getword_str(buf2, sizeof buf2, &startp, endp))
2777c478bd9Sstevel@tonic-gate 				return (-1);
2787c478bd9Sstevel@tonic-gate 			n = dn_comp(buf2, cp, buflen, dnptrs, lastdnptr);
2797c478bd9Sstevel@tonic-gate 			if (n < 0)
2807c478bd9Sstevel@tonic-gate 				return (-1);
2817c478bd9Sstevel@tonic-gate 			cp += n;
2827c478bd9Sstevel@tonic-gate 			ShrinkBuffer(n);
2837c478bd9Sstevel@tonic-gate 			break;
2847c478bd9Sstevel@tonic-gate 		case T_SRV:
2857c478bd9Sstevel@tonic-gate 			n = getnum_str(&startp, endp);
2867c478bd9Sstevel@tonic-gate 			if (n < 0)
2877c478bd9Sstevel@tonic-gate 				return (-1);
2887c478bd9Sstevel@tonic-gate 			ShrinkBuffer(INT16SZ);
2897c478bd9Sstevel@tonic-gate 			PUTSHORT(n, cp);
2907c478bd9Sstevel@tonic-gate 
2917c478bd9Sstevel@tonic-gate 			n = getnum_str(&startp, endp);
2927c478bd9Sstevel@tonic-gate 			if (n < 0)
2937c478bd9Sstevel@tonic-gate 				return (-1);
2947c478bd9Sstevel@tonic-gate 			ShrinkBuffer(INT16SZ);
2957c478bd9Sstevel@tonic-gate 			PUTSHORT(n, cp);
2967c478bd9Sstevel@tonic-gate 
2977c478bd9Sstevel@tonic-gate 			n = getnum_str(&startp, endp);
2987c478bd9Sstevel@tonic-gate 			if (n < 0)
2997c478bd9Sstevel@tonic-gate 				return (-1);
3007c478bd9Sstevel@tonic-gate 			ShrinkBuffer(INT16SZ);
3017c478bd9Sstevel@tonic-gate 			PUTSHORT(n, cp);
3027c478bd9Sstevel@tonic-gate 
3037c478bd9Sstevel@tonic-gate 			if (!getword_str(buf2, sizeof buf2, &startp, endp))
3047c478bd9Sstevel@tonic-gate 				return (-1);
3057c478bd9Sstevel@tonic-gate 			n = dn_comp(buf2, cp, buflen, NULL, NULL);
3067c478bd9Sstevel@tonic-gate 			if (n < 0)
3077c478bd9Sstevel@tonic-gate 				return (-1);
3087c478bd9Sstevel@tonic-gate 			cp += n;
3097c478bd9Sstevel@tonic-gate 			ShrinkBuffer(n);
3107c478bd9Sstevel@tonic-gate 			break;
3117c478bd9Sstevel@tonic-gate 		case T_PX:
3127c478bd9Sstevel@tonic-gate 			n = getnum_str(&startp, endp);
3137c478bd9Sstevel@tonic-gate 			if (n < 0)
3147c478bd9Sstevel@tonic-gate 				return (-1);
3157c478bd9Sstevel@tonic-gate 			PUTSHORT(n, cp);
3167c478bd9Sstevel@tonic-gate 			ShrinkBuffer(INT16SZ);
3177c478bd9Sstevel@tonic-gate 			for (i = 0; i < 2; i++) {
3187c478bd9Sstevel@tonic-gate 				if (!getword_str(buf2, sizeof buf2, &startp,
3197c478bd9Sstevel@tonic-gate 						 endp))
3207c478bd9Sstevel@tonic-gate 					return (-1);
3217c478bd9Sstevel@tonic-gate 				n = dn_comp(buf2, cp, buflen, dnptrs,
3227c478bd9Sstevel@tonic-gate 					    lastdnptr);
3237c478bd9Sstevel@tonic-gate 				if (n < 0)
3247c478bd9Sstevel@tonic-gate 					return (-1);
3257c478bd9Sstevel@tonic-gate 				cp += n;
3267c478bd9Sstevel@tonic-gate 				ShrinkBuffer(n);
3277c478bd9Sstevel@tonic-gate 			}
3287c478bd9Sstevel@tonic-gate 			break;
3297c478bd9Sstevel@tonic-gate 		case T_WKS: {
3307c478bd9Sstevel@tonic-gate 			char bm[MAXPORT/8];
3317c478bd9Sstevel@tonic-gate 			unsigned int maxbm = 0;
3327c478bd9Sstevel@tonic-gate 
3337c478bd9Sstevel@tonic-gate 			if (!getword_str(buf2, sizeof buf2, &startp, endp))
3347c478bd9Sstevel@tonic-gate 				return (-1);
3357c478bd9Sstevel@tonic-gate 			if (!inet_aton(buf2, &ina))
3367c478bd9Sstevel@tonic-gate 				return (-1);
3377c478bd9Sstevel@tonic-gate 			n1 = ntohl(ina.s_addr);
3387c478bd9Sstevel@tonic-gate 			ShrinkBuffer(INT32SZ);
3397c478bd9Sstevel@tonic-gate 			PUTLONG(n1, cp);
3407c478bd9Sstevel@tonic-gate 
3417c478bd9Sstevel@tonic-gate 			if (!getword_str(buf2, sizeof buf2, &startp, endp))
3427c478bd9Sstevel@tonic-gate 				return (-1);
3437c478bd9Sstevel@tonic-gate 			if ((i = res_protocolnumber(buf2)) < 0)
3447c478bd9Sstevel@tonic-gate 				return (-1);
3457c478bd9Sstevel@tonic-gate 			ShrinkBuffer(1);
3467c478bd9Sstevel@tonic-gate 			*cp++ = i & 0xff;
3477c478bd9Sstevel@tonic-gate 
3487c478bd9Sstevel@tonic-gate 			for (i = 0; i < MAXPORT/8 ; i++)
3497c478bd9Sstevel@tonic-gate 				bm[i] = 0;
3507c478bd9Sstevel@tonic-gate 
3517c478bd9Sstevel@tonic-gate 			while (getword_str(buf2, sizeof buf2, &startp, endp)) {
352*9525b14bSRao Shoaib 				if ((n = res_servicenumber(buf2)) <= 0)
3537c478bd9Sstevel@tonic-gate 					return (-1);
3547c478bd9Sstevel@tonic-gate 
355*9525b14bSRao Shoaib 				if (n < MAXPORT) {
356*9525b14bSRao Shoaib 					bm[n/8] |= (0x80>>(n%8));
357*9525b14bSRao Shoaib 					if ((unsigned)n > maxbm)
358*9525b14bSRao Shoaib 						maxbm = n;
3597c478bd9Sstevel@tonic-gate 				} else
3607c478bd9Sstevel@tonic-gate 					return (-1);
3617c478bd9Sstevel@tonic-gate 			}
3627c478bd9Sstevel@tonic-gate 			maxbm = maxbm/8 + 1;
3637c478bd9Sstevel@tonic-gate 			ShrinkBuffer(maxbm);
3647c478bd9Sstevel@tonic-gate 			memcpy(cp, bm, maxbm);
3657c478bd9Sstevel@tonic-gate 			cp += maxbm;
3667c478bd9Sstevel@tonic-gate 			break;
3677c478bd9Sstevel@tonic-gate 		}
3687c478bd9Sstevel@tonic-gate 		case T_HINFO:
3697c478bd9Sstevel@tonic-gate 			for (i = 0; i < 2; i++) {
3707c478bd9Sstevel@tonic-gate 				if ((n = getstr_str(buf2, sizeof buf2,
3717c478bd9Sstevel@tonic-gate 						&startp, endp)) < 0)
3727c478bd9Sstevel@tonic-gate 					return (-1);
3737c478bd9Sstevel@tonic-gate 				if (n > 255)
3747c478bd9Sstevel@tonic-gate 					return (-1);
3757c478bd9Sstevel@tonic-gate 				ShrinkBuffer(n+1);
3767c478bd9Sstevel@tonic-gate 				*cp++ = n;
3777c478bd9Sstevel@tonic-gate 				memcpy(cp, buf2, n);
3787c478bd9Sstevel@tonic-gate 				cp += n;
3797c478bd9Sstevel@tonic-gate 			}
3807c478bd9Sstevel@tonic-gate 			break;
3817c478bd9Sstevel@tonic-gate 		case T_TXT:
382*9525b14bSRao Shoaib 			for (;;) {
3837c478bd9Sstevel@tonic-gate 				if ((n = getstr_str(buf2, sizeof buf2,
3847c478bd9Sstevel@tonic-gate 						&startp, endp)) < 0) {
3857c478bd9Sstevel@tonic-gate 					if (cp != (sp2 + INT16SZ))
3867c478bd9Sstevel@tonic-gate 						break;
3877c478bd9Sstevel@tonic-gate 					return (-1);
3887c478bd9Sstevel@tonic-gate 				}
3897c478bd9Sstevel@tonic-gate 				if (n > 255)
3907c478bd9Sstevel@tonic-gate 					return (-1);
3917c478bd9Sstevel@tonic-gate 				ShrinkBuffer(n+1);
3927c478bd9Sstevel@tonic-gate 				*cp++ = n;
3937c478bd9Sstevel@tonic-gate 				memcpy(cp, buf2, n);
3947c478bd9Sstevel@tonic-gate 				cp += n;
3957c478bd9Sstevel@tonic-gate 			}
3967c478bd9Sstevel@tonic-gate 			break;
3977c478bd9Sstevel@tonic-gate 		case T_X25:
398*9525b14bSRao Shoaib 			/* RFC1183 */
3997c478bd9Sstevel@tonic-gate 			if ((n = getstr_str(buf2, sizeof buf2, &startp,
4007c478bd9Sstevel@tonic-gate 					 endp)) < 0)
4017c478bd9Sstevel@tonic-gate 				return (-1);
4027c478bd9Sstevel@tonic-gate 			if (n > 255)
4037c478bd9Sstevel@tonic-gate 				return (-1);
4047c478bd9Sstevel@tonic-gate 			ShrinkBuffer(n+1);
4057c478bd9Sstevel@tonic-gate 			*cp++ = n;
4067c478bd9Sstevel@tonic-gate 			memcpy(cp, buf2, n);
4077c478bd9Sstevel@tonic-gate 			cp += n;
4087c478bd9Sstevel@tonic-gate 			break;
4097c478bd9Sstevel@tonic-gate 		case T_ISDN:
410*9525b14bSRao Shoaib 			/* RFC1183 */
4117c478bd9Sstevel@tonic-gate 			if ((n = getstr_str(buf2, sizeof buf2, &startp,
4127c478bd9Sstevel@tonic-gate 					 endp)) < 0)
4137c478bd9Sstevel@tonic-gate 				return (-1);
4147c478bd9Sstevel@tonic-gate 			if ((n > 255) || (n == 0))
4157c478bd9Sstevel@tonic-gate 				return (-1);
4167c478bd9Sstevel@tonic-gate 			ShrinkBuffer(n+1);
4177c478bd9Sstevel@tonic-gate 			*cp++ = n;
4187c478bd9Sstevel@tonic-gate 			memcpy(cp, buf2, n);
4197c478bd9Sstevel@tonic-gate 			cp += n;
4207c478bd9Sstevel@tonic-gate 			if ((n = getstr_str(buf2, sizeof buf2, &startp,
4217c478bd9Sstevel@tonic-gate 					 endp)) < 0)
4227c478bd9Sstevel@tonic-gate 				n = 0;
4237c478bd9Sstevel@tonic-gate 			if (n > 255)
4247c478bd9Sstevel@tonic-gate 				return (-1);
4257c478bd9Sstevel@tonic-gate 			ShrinkBuffer(n+1);
4267c478bd9Sstevel@tonic-gate 			*cp++ = n;
4277c478bd9Sstevel@tonic-gate 			memcpy(cp, buf2, n);
4287c478bd9Sstevel@tonic-gate 			cp += n;
4297c478bd9Sstevel@tonic-gate 			break;
4307c478bd9Sstevel@tonic-gate 		case T_NSAP:
4317c478bd9Sstevel@tonic-gate 			if ((n = inet_nsap_addr((char *)startp, (u_char *)buf2, sizeof(buf2))) != 0) {
4327c478bd9Sstevel@tonic-gate 				ShrinkBuffer(n);
4337c478bd9Sstevel@tonic-gate 				memcpy(cp, buf2, n);
4347c478bd9Sstevel@tonic-gate 				cp += n;
4357c478bd9Sstevel@tonic-gate 			} else {
4367c478bd9Sstevel@tonic-gate 				return (-1);
4377c478bd9Sstevel@tonic-gate 			}
4387c478bd9Sstevel@tonic-gate 			break;
4397c478bd9Sstevel@tonic-gate 		case T_LOC:
4407c478bd9Sstevel@tonic-gate 			if ((n = loc_aton((char *)startp, (u_char *)buf2)) != 0) {
4417c478bd9Sstevel@tonic-gate 				ShrinkBuffer(n);
4427c478bd9Sstevel@tonic-gate 				memcpy(cp, buf2, n);
4437c478bd9Sstevel@tonic-gate 				cp += n;
4447c478bd9Sstevel@tonic-gate 			} else
4457c478bd9Sstevel@tonic-gate 				return (-1);
4467c478bd9Sstevel@tonic-gate 			break;
4477c478bd9Sstevel@tonic-gate 		case ns_t_sig:
4487c478bd9Sstevel@tonic-gate 		    {
4497c478bd9Sstevel@tonic-gate 			int sig_type, success, dateerror;
4507c478bd9Sstevel@tonic-gate 			u_int32_t exptime, timesigned;
4517c478bd9Sstevel@tonic-gate 
4527c478bd9Sstevel@tonic-gate 			/* type */
4537c478bd9Sstevel@tonic-gate 			if ((n = getword_str(buf2, sizeof buf2,
4547c478bd9Sstevel@tonic-gate 					     &startp, endp)) < 0)
4557c478bd9Sstevel@tonic-gate 				return (-1);
4567c478bd9Sstevel@tonic-gate 			sig_type = sym_ston(__p_type_syms, buf2, &success);
4577c478bd9Sstevel@tonic-gate 			if (!success || sig_type == ns_t_any)
4587c478bd9Sstevel@tonic-gate 				return (-1);
4597c478bd9Sstevel@tonic-gate 			ShrinkBuffer(INT16SZ);
4607c478bd9Sstevel@tonic-gate 			PUTSHORT(sig_type, cp);
4617c478bd9Sstevel@tonic-gate 			/* alg */
4627c478bd9Sstevel@tonic-gate 			n = getnum_str(&startp, endp);
4637c478bd9Sstevel@tonic-gate 			if (n < 0)
4647c478bd9Sstevel@tonic-gate 				return (-1);
4657c478bd9Sstevel@tonic-gate 			ShrinkBuffer(1);
4667c478bd9Sstevel@tonic-gate 			*cp++ = n;
4677c478bd9Sstevel@tonic-gate 			/* labels */
4687c478bd9Sstevel@tonic-gate 			n = getnum_str(&startp, endp);
4697c478bd9Sstevel@tonic-gate 			if (n <= 0 || n > 255)
4707c478bd9Sstevel@tonic-gate 				return (-1);
4717c478bd9Sstevel@tonic-gate 			ShrinkBuffer(1);
4727c478bd9Sstevel@tonic-gate 			*cp++ = n;
4737c478bd9Sstevel@tonic-gate 			/* ottl  & expire */
4747c478bd9Sstevel@tonic-gate 			if (!getword_str(buf2, sizeof buf2, &startp, endp))
4757c478bd9Sstevel@tonic-gate 				return (-1);
4767c478bd9Sstevel@tonic-gate 			exptime = ns_datetosecs(buf2, &dateerror);
4777c478bd9Sstevel@tonic-gate 			if (!dateerror) {
4787c478bd9Sstevel@tonic-gate 				ShrinkBuffer(INT32SZ);
4797c478bd9Sstevel@tonic-gate 				PUTLONG(rttl, cp);
4807c478bd9Sstevel@tonic-gate 			}
4817c478bd9Sstevel@tonic-gate 			else {
4827c478bd9Sstevel@tonic-gate 				char *ulendp;
4837c478bd9Sstevel@tonic-gate 				u_int32_t ottl;
4847c478bd9Sstevel@tonic-gate 
4857c478bd9Sstevel@tonic-gate 				errno = 0;
4867c478bd9Sstevel@tonic-gate 				ottl = strtoul(buf2, &ulendp, 10);
4877c478bd9Sstevel@tonic-gate 				if (errno != 0 ||
4887c478bd9Sstevel@tonic-gate 				    (ulendp != NULL && *ulendp != '\0'))
4897c478bd9Sstevel@tonic-gate 					return (-1);
4907c478bd9Sstevel@tonic-gate 				ShrinkBuffer(INT32SZ);
4917c478bd9Sstevel@tonic-gate 				PUTLONG(ottl, cp);
4927c478bd9Sstevel@tonic-gate 				if (!getword_str(buf2, sizeof buf2, &startp,
4937c478bd9Sstevel@tonic-gate 						 endp))
4947c478bd9Sstevel@tonic-gate 					return (-1);
4957c478bd9Sstevel@tonic-gate 				exptime = ns_datetosecs(buf2, &dateerror);
4967c478bd9Sstevel@tonic-gate 				if (dateerror)
4977c478bd9Sstevel@tonic-gate 					return (-1);
4987c478bd9Sstevel@tonic-gate 			}
4997c478bd9Sstevel@tonic-gate 			/* expire */
5007c478bd9Sstevel@tonic-gate 			ShrinkBuffer(INT32SZ);
5017c478bd9Sstevel@tonic-gate 			PUTLONG(exptime, cp);
5027c478bd9Sstevel@tonic-gate 			/* timesigned */
5037c478bd9Sstevel@tonic-gate 			if (!getword_str(buf2, sizeof buf2, &startp, endp))
5047c478bd9Sstevel@tonic-gate 				return (-1);
5057c478bd9Sstevel@tonic-gate 			timesigned = ns_datetosecs(buf2, &dateerror);
5067c478bd9Sstevel@tonic-gate 			if (!dateerror) {
5077c478bd9Sstevel@tonic-gate 				ShrinkBuffer(INT32SZ);
5087c478bd9Sstevel@tonic-gate 				PUTLONG(timesigned, cp);
5097c478bd9Sstevel@tonic-gate 			}
5107c478bd9Sstevel@tonic-gate 			else
5117c478bd9Sstevel@tonic-gate 				return (-1);
5127c478bd9Sstevel@tonic-gate 			/* footprint */
5137c478bd9Sstevel@tonic-gate 			n = getnum_str(&startp, endp);
5147c478bd9Sstevel@tonic-gate 			if (n < 0)
5157c478bd9Sstevel@tonic-gate 				return (-1);
5167c478bd9Sstevel@tonic-gate 			ShrinkBuffer(INT16SZ);
5177c478bd9Sstevel@tonic-gate 			PUTSHORT(n, cp);
5187c478bd9Sstevel@tonic-gate 			/* signer name */
5197c478bd9Sstevel@tonic-gate 			if (!getword_str(buf2, sizeof buf2, &startp, endp))
5207c478bd9Sstevel@tonic-gate 				return (-1);
5217c478bd9Sstevel@tonic-gate 			n = dn_comp(buf2, cp, buflen, dnptrs, lastdnptr);
5227c478bd9Sstevel@tonic-gate 			if (n < 0)
5237c478bd9Sstevel@tonic-gate 				return (-1);
5247c478bd9Sstevel@tonic-gate 			cp += n;
5257c478bd9Sstevel@tonic-gate 			ShrinkBuffer(n);
5267c478bd9Sstevel@tonic-gate 			/* sig */
5277c478bd9Sstevel@tonic-gate 			if ((n = getword_str(buf2, sizeof buf2,
5287c478bd9Sstevel@tonic-gate 					     &startp, endp)) < 0)
5297c478bd9Sstevel@tonic-gate 				return (-1);
5307c478bd9Sstevel@tonic-gate 			siglen = b64_pton(buf2, buf3, sizeof(buf3));
5317c478bd9Sstevel@tonic-gate 			if (siglen < 0)
5327c478bd9Sstevel@tonic-gate 				return (-1);
5337c478bd9Sstevel@tonic-gate 			ShrinkBuffer(siglen);
5347c478bd9Sstevel@tonic-gate 			memcpy(cp, buf3, siglen);
5357c478bd9Sstevel@tonic-gate 			cp += siglen;
5367c478bd9Sstevel@tonic-gate 			break;
5377c478bd9Sstevel@tonic-gate 		    }
5387c478bd9Sstevel@tonic-gate 		case ns_t_key:
5397c478bd9Sstevel@tonic-gate 			/* flags */
5407c478bd9Sstevel@tonic-gate 			n = gethexnum_str(&startp, endp);
5417c478bd9Sstevel@tonic-gate 			if (n < 0)
5427c478bd9Sstevel@tonic-gate 				return (-1);
5437c478bd9Sstevel@tonic-gate 			ShrinkBuffer(INT16SZ);
5447c478bd9Sstevel@tonic-gate 			PUTSHORT(n, cp);
5457c478bd9Sstevel@tonic-gate 			/* proto */
5467c478bd9Sstevel@tonic-gate 			n = getnum_str(&startp, endp);
5477c478bd9Sstevel@tonic-gate 			if (n < 0)
5487c478bd9Sstevel@tonic-gate 				return (-1);
5497c478bd9Sstevel@tonic-gate 			ShrinkBuffer(1);
5507c478bd9Sstevel@tonic-gate 			*cp++ = n;
5517c478bd9Sstevel@tonic-gate 			/* alg */
5527c478bd9Sstevel@tonic-gate 			n = getnum_str(&startp, endp);
5537c478bd9Sstevel@tonic-gate 			if (n < 0)
5547c478bd9Sstevel@tonic-gate 				return (-1);
5557c478bd9Sstevel@tonic-gate 			ShrinkBuffer(1);
5567c478bd9Sstevel@tonic-gate 			*cp++ = n;
5577c478bd9Sstevel@tonic-gate 			/* key */
5587c478bd9Sstevel@tonic-gate 			if ((n = getword_str(buf2, sizeof buf2,
5597c478bd9Sstevel@tonic-gate 					     &startp, endp)) < 0)
5607c478bd9Sstevel@tonic-gate 				return (-1);
5617c478bd9Sstevel@tonic-gate 			keylen = b64_pton(buf2, buf3, sizeof(buf3));
5627c478bd9Sstevel@tonic-gate 			if (keylen < 0)
5637c478bd9Sstevel@tonic-gate 				return (-1);
5647c478bd9Sstevel@tonic-gate 			ShrinkBuffer(keylen);
5657c478bd9Sstevel@tonic-gate 			memcpy(cp, buf3, keylen);
5667c478bd9Sstevel@tonic-gate 			cp += keylen;
5677c478bd9Sstevel@tonic-gate 			break;
5687c478bd9Sstevel@tonic-gate 		case ns_t_nxt:
5697c478bd9Sstevel@tonic-gate 		    {
5707c478bd9Sstevel@tonic-gate 			int success, nxt_type;
5717c478bd9Sstevel@tonic-gate 			u_char data[32];
5727c478bd9Sstevel@tonic-gate 			int maxtype;
5737c478bd9Sstevel@tonic-gate 
5747c478bd9Sstevel@tonic-gate 			/* next name */
5757c478bd9Sstevel@tonic-gate 			if (!getword_str(buf2, sizeof buf2, &startp, endp))
5767c478bd9Sstevel@tonic-gate 				return (-1);
5777c478bd9Sstevel@tonic-gate 			n = dn_comp(buf2, cp, buflen, NULL, NULL);
5787c478bd9Sstevel@tonic-gate 			if (n < 0)
5797c478bd9Sstevel@tonic-gate 				return (-1);
5807c478bd9Sstevel@tonic-gate 			cp += n;
5817c478bd9Sstevel@tonic-gate 			ShrinkBuffer(n);
5827c478bd9Sstevel@tonic-gate 			maxtype = 0;
5837c478bd9Sstevel@tonic-gate 			memset(data, 0, sizeof data);
584*9525b14bSRao Shoaib 			for (;;) {
5857c478bd9Sstevel@tonic-gate 				if (!getword_str(buf2, sizeof buf2, &startp,
5867c478bd9Sstevel@tonic-gate 						 endp))
5877c478bd9Sstevel@tonic-gate 					break;
5887c478bd9Sstevel@tonic-gate 				nxt_type = sym_ston(__p_type_syms, buf2,
5897c478bd9Sstevel@tonic-gate 						    &success);
5907c478bd9Sstevel@tonic-gate 				if (!success || !ns_t_rr_p(nxt_type))
5917c478bd9Sstevel@tonic-gate 					return (-1);
5927c478bd9Sstevel@tonic-gate 				NS_NXT_BIT_SET(nxt_type, data);
5937c478bd9Sstevel@tonic-gate 				if (nxt_type > maxtype)
5947c478bd9Sstevel@tonic-gate 					maxtype = nxt_type;
5957c478bd9Sstevel@tonic-gate 			}
5967c478bd9Sstevel@tonic-gate 			n = maxtype/NS_NXT_BITS+1;
5977c478bd9Sstevel@tonic-gate 			ShrinkBuffer(n);
5987c478bd9Sstevel@tonic-gate 			memcpy(cp, data, n);
5997c478bd9Sstevel@tonic-gate 			cp += n;
6007c478bd9Sstevel@tonic-gate 			break;
6017c478bd9Sstevel@tonic-gate 		    }
6027c478bd9Sstevel@tonic-gate 		case ns_t_cert:
6037c478bd9Sstevel@tonic-gate 			/* type */
6047c478bd9Sstevel@tonic-gate 			n = getnum_str(&startp, endp);
6057c478bd9Sstevel@tonic-gate 			if (n < 0)
6067c478bd9Sstevel@tonic-gate 				return (-1);
6077c478bd9Sstevel@tonic-gate 			ShrinkBuffer(INT16SZ);
6087c478bd9Sstevel@tonic-gate 			PUTSHORT(n, cp);
6097c478bd9Sstevel@tonic-gate 			/* key tag */
6107c478bd9Sstevel@tonic-gate 			n = getnum_str(&startp, endp);
6117c478bd9Sstevel@tonic-gate 			if (n < 0)
6127c478bd9Sstevel@tonic-gate 				return (-1);
6137c478bd9Sstevel@tonic-gate 			ShrinkBuffer(INT16SZ);
6147c478bd9Sstevel@tonic-gate 			PUTSHORT(n, cp);
6157c478bd9Sstevel@tonic-gate 			/* alg */
6167c478bd9Sstevel@tonic-gate 			n = getnum_str(&startp, endp);
6177c478bd9Sstevel@tonic-gate 			if (n < 0)
6187c478bd9Sstevel@tonic-gate 				return (-1);
6197c478bd9Sstevel@tonic-gate 			ShrinkBuffer(1);
6207c478bd9Sstevel@tonic-gate 			*cp++ = n;
6217c478bd9Sstevel@tonic-gate 			/* cert */
6227c478bd9Sstevel@tonic-gate 			if ((n = getword_str(buf2, sizeof buf2,
6237c478bd9Sstevel@tonic-gate 					     &startp, endp)) < 0)
6247c478bd9Sstevel@tonic-gate 				return (-1);
6257c478bd9Sstevel@tonic-gate 			certlen = b64_pton(buf2, buf3, sizeof(buf3));
6267c478bd9Sstevel@tonic-gate 			if (certlen < 0)
6277c478bd9Sstevel@tonic-gate 				return (-1);
6287c478bd9Sstevel@tonic-gate 			ShrinkBuffer(certlen);
6297c478bd9Sstevel@tonic-gate 			memcpy(cp, buf3, certlen);
6307c478bd9Sstevel@tonic-gate 			cp += certlen;
6317c478bd9Sstevel@tonic-gate 			break;
6327c478bd9Sstevel@tonic-gate 		case ns_t_aaaa:
6337c478bd9Sstevel@tonic-gate 			if (!getword_str(buf2, sizeof buf2, &startp, endp))
6347c478bd9Sstevel@tonic-gate 				return (-1);
6357c478bd9Sstevel@tonic-gate 			if (inet_pton(AF_INET6, buf2, &in6a) <= 0)
6367c478bd9Sstevel@tonic-gate 				return (-1);
6377c478bd9Sstevel@tonic-gate 			ShrinkBuffer(NS_IN6ADDRSZ);
6387c478bd9Sstevel@tonic-gate 			memcpy(cp, &in6a, NS_IN6ADDRSZ);
6397c478bd9Sstevel@tonic-gate 			cp += NS_IN6ADDRSZ;
6407c478bd9Sstevel@tonic-gate 			break;
6417c478bd9Sstevel@tonic-gate 		case ns_t_naptr:
6427c478bd9Sstevel@tonic-gate 			/* Order Preference Flags Service Replacement Regexp */
6437c478bd9Sstevel@tonic-gate 			/* Order */
6447c478bd9Sstevel@tonic-gate 			n = getnum_str(&startp, endp);
6457c478bd9Sstevel@tonic-gate 			if (n < 0 || n > 65535)
6467c478bd9Sstevel@tonic-gate 				return (-1);
6477c478bd9Sstevel@tonic-gate 			ShrinkBuffer(INT16SZ);
6487c478bd9Sstevel@tonic-gate 			PUTSHORT(n, cp);
6497c478bd9Sstevel@tonic-gate 			/* Preference */
6507c478bd9Sstevel@tonic-gate 			n = getnum_str(&startp, endp);
6517c478bd9Sstevel@tonic-gate 			if (n < 0 || n > 65535)
6527c478bd9Sstevel@tonic-gate 				return (-1);
6537c478bd9Sstevel@tonic-gate 			ShrinkBuffer(INT16SZ);
6547c478bd9Sstevel@tonic-gate 			PUTSHORT(n, cp);
6557c478bd9Sstevel@tonic-gate 			/* Flags */
6567c478bd9Sstevel@tonic-gate 			if ((n = getstr_str(buf2, sizeof buf2,
6577c478bd9Sstevel@tonic-gate 					&startp, endp)) < 0) {
6587c478bd9Sstevel@tonic-gate 				return (-1);
6597c478bd9Sstevel@tonic-gate 			}
6607c478bd9Sstevel@tonic-gate 			if (n > 255)
6617c478bd9Sstevel@tonic-gate 				return (-1);
6627c478bd9Sstevel@tonic-gate 			ShrinkBuffer(n+1);
6637c478bd9Sstevel@tonic-gate 			*cp++ = n;
6647c478bd9Sstevel@tonic-gate 			memcpy(cp, buf2, n);
6657c478bd9Sstevel@tonic-gate 			cp += n;
6667c478bd9Sstevel@tonic-gate 			/* Service Classes */
6677c478bd9Sstevel@tonic-gate 			if ((n = getstr_str(buf2, sizeof buf2,
6687c478bd9Sstevel@tonic-gate 					&startp, endp)) < 0) {
6697c478bd9Sstevel@tonic-gate 				return (-1);
6707c478bd9Sstevel@tonic-gate 			}
6717c478bd9Sstevel@tonic-gate 			if (n > 255)
6727c478bd9Sstevel@tonic-gate 				return (-1);
6737c478bd9Sstevel@tonic-gate 			ShrinkBuffer(n+1);
6747c478bd9Sstevel@tonic-gate 			*cp++ = n;
6757c478bd9Sstevel@tonic-gate 			memcpy(cp, buf2, n);
6767c478bd9Sstevel@tonic-gate 			cp += n;
6777c478bd9Sstevel@tonic-gate 			/* Pattern */
6787c478bd9Sstevel@tonic-gate 			if ((n = getstr_str(buf2, sizeof buf2,
6797c478bd9Sstevel@tonic-gate 					&startp, endp)) < 0) {
6807c478bd9Sstevel@tonic-gate 				return (-1);
6817c478bd9Sstevel@tonic-gate 			}
6827c478bd9Sstevel@tonic-gate 			if (n > 255)
6837c478bd9Sstevel@tonic-gate 				return (-1);
6847c478bd9Sstevel@tonic-gate 			ShrinkBuffer(n+1);
6857c478bd9Sstevel@tonic-gate 			*cp++ = n;
6867c478bd9Sstevel@tonic-gate 			memcpy(cp, buf2, n);
6877c478bd9Sstevel@tonic-gate 			cp += n;
6887c478bd9Sstevel@tonic-gate 			/* Replacement */
6897c478bd9Sstevel@tonic-gate 			if (!getword_str(buf2, sizeof buf2, &startp, endp))
6907c478bd9Sstevel@tonic-gate 				return (-1);
6917c478bd9Sstevel@tonic-gate 			n = dn_comp(buf2, cp, buflen, NULL, NULL);
6927c478bd9Sstevel@tonic-gate 			if (n < 0)
6937c478bd9Sstevel@tonic-gate 				return (-1);
6947c478bd9Sstevel@tonic-gate 			cp += n;
6957c478bd9Sstevel@tonic-gate 			ShrinkBuffer(n);
6967c478bd9Sstevel@tonic-gate 			break;
6977c478bd9Sstevel@tonic-gate 		default:
6987c478bd9Sstevel@tonic-gate 			return (-1);
6997c478bd9Sstevel@tonic-gate 		} /*switch*/
7007c478bd9Sstevel@tonic-gate 		n = (u_int16_t)((cp - sp2) - INT16SZ);
7017c478bd9Sstevel@tonic-gate 		PUTSHORT(n, sp2);
7027c478bd9Sstevel@tonic-gate 	} /*for*/
7037c478bd9Sstevel@tonic-gate 
7047c478bd9Sstevel@tonic-gate 	hp->qdcount = htons(counts[0]);
7057c478bd9Sstevel@tonic-gate 	hp->ancount = htons(counts[1]);
7067c478bd9Sstevel@tonic-gate 	hp->nscount = htons(counts[2]);
7077c478bd9Sstevel@tonic-gate 	hp->arcount = htons(counts[3]);
7087c478bd9Sstevel@tonic-gate 	return (cp - buf);
7097c478bd9Sstevel@tonic-gate }
7107c478bd9Sstevel@tonic-gate 
711*9525b14bSRao Shoaib /*%
7127c478bd9Sstevel@tonic-gate  * Get a whitespace delimited word from a string (not file)
7137c478bd9Sstevel@tonic-gate  * into buf. modify the start pointer to point after the
7147c478bd9Sstevel@tonic-gate  * word in the string.
7157c478bd9Sstevel@tonic-gate  */
7167c478bd9Sstevel@tonic-gate static int
getword_str(char * buf,int size,u_char ** startpp,u_char * endp)7177c478bd9Sstevel@tonic-gate getword_str(char *buf, int size, u_char **startpp, u_char *endp) {
7187c478bd9Sstevel@tonic-gate         char *cp;
7197c478bd9Sstevel@tonic-gate         int c;
7207c478bd9Sstevel@tonic-gate 
7217c478bd9Sstevel@tonic-gate         for (cp = buf; *startpp <= endp; ) {
7227c478bd9Sstevel@tonic-gate                 c = **startpp;
7237c478bd9Sstevel@tonic-gate                 if (isspace(c) || c == '\0') {
724*9525b14bSRao Shoaib                         if (cp != buf) /*%< trailing whitespace */
7257c478bd9Sstevel@tonic-gate                                 break;
726*9525b14bSRao Shoaib                         else { /*%< leading whitespace */
7277c478bd9Sstevel@tonic-gate                                 (*startpp)++;
7287c478bd9Sstevel@tonic-gate                                 continue;
7297c478bd9Sstevel@tonic-gate                         }
7307c478bd9Sstevel@tonic-gate                 }
7317c478bd9Sstevel@tonic-gate                 (*startpp)++;
7327c478bd9Sstevel@tonic-gate                 if (cp >= buf+size-1)
7337c478bd9Sstevel@tonic-gate                         break;
7347c478bd9Sstevel@tonic-gate                 *cp++ = (u_char)c;
7357c478bd9Sstevel@tonic-gate         }
7367c478bd9Sstevel@tonic-gate         *cp = '\0';
7377c478bd9Sstevel@tonic-gate         return (cp != buf);
7387c478bd9Sstevel@tonic-gate }
7397c478bd9Sstevel@tonic-gate 
740*9525b14bSRao Shoaib /*%
7417c478bd9Sstevel@tonic-gate  * get a white spae delimited string from memory.  Process quoted strings
742*9525b14bSRao Shoaib  * and \\DDD escapes.  Return length or -1 on error.  Returned string may
7437c478bd9Sstevel@tonic-gate  * contain nulls.
7447c478bd9Sstevel@tonic-gate  */
7457c478bd9Sstevel@tonic-gate static char digits[] = "0123456789";
7467c478bd9Sstevel@tonic-gate static int
getstr_str(char * buf,int size,u_char ** startpp,u_char * endp)7477c478bd9Sstevel@tonic-gate getstr_str(char *buf, int size, u_char **startpp, u_char *endp) {
7487c478bd9Sstevel@tonic-gate         char *cp;
7497c478bd9Sstevel@tonic-gate         int c, c1 = 0;
7507c478bd9Sstevel@tonic-gate 	int inquote = 0;
7517c478bd9Sstevel@tonic-gate 	int seen_quote = 0;
7527c478bd9Sstevel@tonic-gate 	int escape = 0;
7537c478bd9Sstevel@tonic-gate 	int dig = 0;
7547c478bd9Sstevel@tonic-gate 
7557c478bd9Sstevel@tonic-gate 	for (cp = buf; *startpp <= endp; ) {
7567c478bd9Sstevel@tonic-gate                 if ((c = **startpp) == '\0')
7577c478bd9Sstevel@tonic-gate 			break;
7587c478bd9Sstevel@tonic-gate 		/* leading white space */
7597c478bd9Sstevel@tonic-gate 		if ((cp == buf) && !seen_quote && isspace(c)) {
7607c478bd9Sstevel@tonic-gate 			(*startpp)++;
7617c478bd9Sstevel@tonic-gate 			continue;
7627c478bd9Sstevel@tonic-gate 		}
7637c478bd9Sstevel@tonic-gate 
7647c478bd9Sstevel@tonic-gate 		switch (c) {
7657c478bd9Sstevel@tonic-gate 		case '\\':
7667c478bd9Sstevel@tonic-gate 			if (!escape)  {
7677c478bd9Sstevel@tonic-gate 				escape = 1;
7687c478bd9Sstevel@tonic-gate 				dig = 0;
7697c478bd9Sstevel@tonic-gate 				c1 = 0;
7707c478bd9Sstevel@tonic-gate 				(*startpp)++;
7717c478bd9Sstevel@tonic-gate 				continue;
7727c478bd9Sstevel@tonic-gate 			}
7737c478bd9Sstevel@tonic-gate 			goto do_escape;
7747c478bd9Sstevel@tonic-gate 		case '"':
7757c478bd9Sstevel@tonic-gate 			if (!escape) {
7767c478bd9Sstevel@tonic-gate 				inquote = !inquote;
7777c478bd9Sstevel@tonic-gate 				seen_quote = 1;
7787c478bd9Sstevel@tonic-gate 				(*startpp)++;
7797c478bd9Sstevel@tonic-gate 				continue;
7807c478bd9Sstevel@tonic-gate 			}
7817c478bd9Sstevel@tonic-gate 			/* fall through */
7827c478bd9Sstevel@tonic-gate 		default:
7837c478bd9Sstevel@tonic-gate 		do_escape:
7847c478bd9Sstevel@tonic-gate 			if (escape) {
7857c478bd9Sstevel@tonic-gate 				switch (c) {
7867c478bd9Sstevel@tonic-gate 				case '0':
7877c478bd9Sstevel@tonic-gate 				case '1':
7887c478bd9Sstevel@tonic-gate 				case '2':
7897c478bd9Sstevel@tonic-gate 				case '3':
7907c478bd9Sstevel@tonic-gate 				case '4':
7917c478bd9Sstevel@tonic-gate 				case '5':
7927c478bd9Sstevel@tonic-gate 				case '6':
7937c478bd9Sstevel@tonic-gate 				case '7':
7947c478bd9Sstevel@tonic-gate 				case '8':
7957c478bd9Sstevel@tonic-gate 				case '9':
7967c478bd9Sstevel@tonic-gate 					c1 = c1 * 10 +
7977c478bd9Sstevel@tonic-gate 						(strchr(digits, c) - digits);
7987c478bd9Sstevel@tonic-gate 
7997c478bd9Sstevel@tonic-gate 					if (++dig == 3) {
8007c478bd9Sstevel@tonic-gate 						c = c1 &0xff;
8017c478bd9Sstevel@tonic-gate 						break;
8027c478bd9Sstevel@tonic-gate 					}
8037c478bd9Sstevel@tonic-gate 					(*startpp)++;
8047c478bd9Sstevel@tonic-gate 					continue;
8057c478bd9Sstevel@tonic-gate 				}
8067c478bd9Sstevel@tonic-gate 				escape = 0;
8077c478bd9Sstevel@tonic-gate 			} else if (!inquote && isspace(c))
8087c478bd9Sstevel@tonic-gate 				goto done;
8097c478bd9Sstevel@tonic-gate 			if (cp >= buf+size-1)
8107c478bd9Sstevel@tonic-gate 				goto done;
8117c478bd9Sstevel@tonic-gate 			*cp++ = (u_char)c;
8127c478bd9Sstevel@tonic-gate 			(*startpp)++;
8137c478bd9Sstevel@tonic-gate 		}
8147c478bd9Sstevel@tonic-gate 	}
8157c478bd9Sstevel@tonic-gate  done:
8167c478bd9Sstevel@tonic-gate 	*cp = '\0';
8177c478bd9Sstevel@tonic-gate 	return ((cp == buf)?  (seen_quote? 0: -1): (cp - buf));
8187c478bd9Sstevel@tonic-gate }
819*9525b14bSRao Shoaib 
820*9525b14bSRao Shoaib /*%
8217c478bd9Sstevel@tonic-gate  * Get a whitespace delimited base 16 number from a string (not file) into buf
8227c478bd9Sstevel@tonic-gate  * update the start pointer to point after the number in the string.
8237c478bd9Sstevel@tonic-gate  */
8247c478bd9Sstevel@tonic-gate static int
gethexnum_str(u_char ** startpp,u_char * endp)8257c478bd9Sstevel@tonic-gate gethexnum_str(u_char **startpp, u_char *endp) {
8267c478bd9Sstevel@tonic-gate         int c, n;
8277c478bd9Sstevel@tonic-gate         int seendigit = 0;
8287c478bd9Sstevel@tonic-gate         int m = 0;
8297c478bd9Sstevel@tonic-gate 
8307c478bd9Sstevel@tonic-gate 	if (*startpp + 2 >= endp || strncasecmp((char *)*startpp, "0x", 2) != 0)
8317c478bd9Sstevel@tonic-gate 		return getnum_str(startpp, endp);
8327c478bd9Sstevel@tonic-gate 	(*startpp)+=2;
8337c478bd9Sstevel@tonic-gate         for (n = 0; *startpp <= endp; ) {
8347c478bd9Sstevel@tonic-gate                 c = **startpp;
8357c478bd9Sstevel@tonic-gate                 if (isspace(c) || c == '\0') {
836*9525b14bSRao Shoaib                         if (seendigit) /*%< trailing whitespace */
8377c478bd9Sstevel@tonic-gate                                 break;
838*9525b14bSRao Shoaib                         else { /*%< leading whitespace */
8397c478bd9Sstevel@tonic-gate                                 (*startpp)++;
8407c478bd9Sstevel@tonic-gate                                 continue;
8417c478bd9Sstevel@tonic-gate                         }
8427c478bd9Sstevel@tonic-gate                 }
8437c478bd9Sstevel@tonic-gate                 if (c == ';') {
8447c478bd9Sstevel@tonic-gate                         while ((*startpp <= endp) &&
8457c478bd9Sstevel@tonic-gate 			       ((c = **startpp) != '\n'))
8467c478bd9Sstevel@tonic-gate 					(*startpp)++;
8477c478bd9Sstevel@tonic-gate                         if (seendigit)
8487c478bd9Sstevel@tonic-gate                                 break;
8497c478bd9Sstevel@tonic-gate                         continue;
8507c478bd9Sstevel@tonic-gate                 }
8517c478bd9Sstevel@tonic-gate                 if (!isxdigit(c)) {
8527c478bd9Sstevel@tonic-gate                         if (c == ')' && seendigit) {
8537c478bd9Sstevel@tonic-gate                                 (*startpp)--;
8547c478bd9Sstevel@tonic-gate                                 break;
8557c478bd9Sstevel@tonic-gate                         }
8567c478bd9Sstevel@tonic-gate 			return (-1);
8577c478bd9Sstevel@tonic-gate                 }
8587c478bd9Sstevel@tonic-gate                 (*startpp)++;
8597c478bd9Sstevel@tonic-gate 		if (isdigit(c))
8607c478bd9Sstevel@tonic-gate 	                n = n * 16 + (c - '0');
8617c478bd9Sstevel@tonic-gate 		else
8627c478bd9Sstevel@tonic-gate 			n = n * 16 + (tolower(c) - 'a' + 10);
8637c478bd9Sstevel@tonic-gate                 seendigit = 1;
8647c478bd9Sstevel@tonic-gate         }
8657c478bd9Sstevel@tonic-gate         return (n + m);
8667c478bd9Sstevel@tonic-gate }
8677c478bd9Sstevel@tonic-gate 
868*9525b14bSRao Shoaib /*%
8697c478bd9Sstevel@tonic-gate  * Get a whitespace delimited base 10 number from a string (not file) into buf
8707c478bd9Sstevel@tonic-gate  * update the start pointer to point after the number in the string.
8717c478bd9Sstevel@tonic-gate  */
8727c478bd9Sstevel@tonic-gate static int
getnum_str(u_char ** startpp,u_char * endp)8737c478bd9Sstevel@tonic-gate getnum_str(u_char **startpp, u_char *endp) {
8747c478bd9Sstevel@tonic-gate         int c, n;
8757c478bd9Sstevel@tonic-gate         int seendigit = 0;
8767c478bd9Sstevel@tonic-gate         int m = 0;
8777c478bd9Sstevel@tonic-gate 
8787c478bd9Sstevel@tonic-gate         for (n = 0; *startpp <= endp; ) {
8797c478bd9Sstevel@tonic-gate                 c = **startpp;
8807c478bd9Sstevel@tonic-gate                 if (isspace(c) || c == '\0') {
881*9525b14bSRao Shoaib                         if (seendigit) /*%< trailing whitespace */
8827c478bd9Sstevel@tonic-gate                                 break;
883*9525b14bSRao Shoaib                         else { /*%< leading whitespace */
8847c478bd9Sstevel@tonic-gate                                 (*startpp)++;
8857c478bd9Sstevel@tonic-gate                                 continue;
8867c478bd9Sstevel@tonic-gate                         }
8877c478bd9Sstevel@tonic-gate                 }
8887c478bd9Sstevel@tonic-gate                 if (c == ';') {
8897c478bd9Sstevel@tonic-gate                         while ((*startpp <= endp) &&
8907c478bd9Sstevel@tonic-gate 			       ((c = **startpp) != '\n'))
8917c478bd9Sstevel@tonic-gate 					(*startpp)++;
8927c478bd9Sstevel@tonic-gate                         if (seendigit)
8937c478bd9Sstevel@tonic-gate                                 break;
8947c478bd9Sstevel@tonic-gate                         continue;
8957c478bd9Sstevel@tonic-gate                 }
8967c478bd9Sstevel@tonic-gate                 if (!isdigit(c)) {
8977c478bd9Sstevel@tonic-gate                         if (c == ')' && seendigit) {
8987c478bd9Sstevel@tonic-gate                                 (*startpp)--;
8997c478bd9Sstevel@tonic-gate                                 break;
9007c478bd9Sstevel@tonic-gate                         }
9017c478bd9Sstevel@tonic-gate 			return (-1);
9027c478bd9Sstevel@tonic-gate                 }
9037c478bd9Sstevel@tonic-gate                 (*startpp)++;
9047c478bd9Sstevel@tonic-gate                 n = n * 10 + (c - '0');
9057c478bd9Sstevel@tonic-gate                 seendigit = 1;
9067c478bd9Sstevel@tonic-gate         }
9077c478bd9Sstevel@tonic-gate         return (n + m);
9087c478bd9Sstevel@tonic-gate }
9097c478bd9Sstevel@tonic-gate 
910*9525b14bSRao Shoaib /*%
9117c478bd9Sstevel@tonic-gate  * Allocate a resource record buffer & save rr info.
9127c478bd9Sstevel@tonic-gate  */
9137c478bd9Sstevel@tonic-gate ns_updrec *
res_mkupdrec(int section,const char * dname,u_int class,u_int type,u_long ttl)9147c478bd9Sstevel@tonic-gate res_mkupdrec(int section, const char *dname,
9157c478bd9Sstevel@tonic-gate 	     u_int class, u_int type, u_long ttl) {
9167c478bd9Sstevel@tonic-gate 	ns_updrec *rrecp = (ns_updrec *)calloc(1, sizeof(ns_updrec));
9177c478bd9Sstevel@tonic-gate 
9187c478bd9Sstevel@tonic-gate 	if (!rrecp || !(rrecp->r_dname = strdup(dname))) {
9197c478bd9Sstevel@tonic-gate 		if (rrecp)
9207c478bd9Sstevel@tonic-gate 			free((char *)rrecp);
9217c478bd9Sstevel@tonic-gate 		return (NULL);
9227c478bd9Sstevel@tonic-gate 	}
9237c478bd9Sstevel@tonic-gate 	INIT_LINK(rrecp, r_link);
9247c478bd9Sstevel@tonic-gate 	INIT_LINK(rrecp, r_glink);
925*9525b14bSRao Shoaib  	rrecp->r_class = (ns_class)class;
926*9525b14bSRao Shoaib 	rrecp->r_type = (ns_type)type;
9277c478bd9Sstevel@tonic-gate 	rrecp->r_ttl = ttl;
928*9525b14bSRao Shoaib 	rrecp->r_section = (ns_sect)section;
9297c478bd9Sstevel@tonic-gate 	return (rrecp);
9307c478bd9Sstevel@tonic-gate }
9317c478bd9Sstevel@tonic-gate 
932*9525b14bSRao Shoaib /*%
9337c478bd9Sstevel@tonic-gate  * Free a resource record buffer created by res_mkupdrec.
9347c478bd9Sstevel@tonic-gate  */
9357c478bd9Sstevel@tonic-gate void
res_freeupdrec(ns_updrec * rrecp)9367c478bd9Sstevel@tonic-gate res_freeupdrec(ns_updrec *rrecp) {
9377c478bd9Sstevel@tonic-gate 	/* Note: freeing r_dp is the caller's responsibility. */
9387c478bd9Sstevel@tonic-gate 	if (rrecp->r_dname != NULL)
9397c478bd9Sstevel@tonic-gate 		free(rrecp->r_dname);
9407c478bd9Sstevel@tonic-gate 	free(rrecp);
9417c478bd9Sstevel@tonic-gate }
9427c478bd9Sstevel@tonic-gate 
9437c478bd9Sstevel@tonic-gate struct valuelist {
9447c478bd9Sstevel@tonic-gate 	struct valuelist *	next;
9457c478bd9Sstevel@tonic-gate 	struct valuelist *	prev;
9467c478bd9Sstevel@tonic-gate 	char *			name;
9477c478bd9Sstevel@tonic-gate 	char *			proto;
9487c478bd9Sstevel@tonic-gate 	int			port;
9497c478bd9Sstevel@tonic-gate };
9507c478bd9Sstevel@tonic-gate static struct valuelist *servicelist, *protolist;
9517c478bd9Sstevel@tonic-gate 
9527c478bd9Sstevel@tonic-gate static void
res_buildservicelist()9537c478bd9Sstevel@tonic-gate res_buildservicelist() {
9547c478bd9Sstevel@tonic-gate 	struct servent *sp;
9557c478bd9Sstevel@tonic-gate 	struct valuelist *slp;
9567c478bd9Sstevel@tonic-gate 
9577c478bd9Sstevel@tonic-gate #ifdef MAYBE_HESIOD
9587c478bd9Sstevel@tonic-gate 	setservent(0);
9597c478bd9Sstevel@tonic-gate #else
9607c478bd9Sstevel@tonic-gate 	setservent(1);
9617c478bd9Sstevel@tonic-gate #endif
9627c478bd9Sstevel@tonic-gate 	while ((sp = getservent()) != NULL) {
9637c478bd9Sstevel@tonic-gate 		slp = (struct valuelist *)malloc(sizeof(struct valuelist));
9647c478bd9Sstevel@tonic-gate 		if (!slp)
9657c478bd9Sstevel@tonic-gate 			break;
9667c478bd9Sstevel@tonic-gate 		slp->name = strdup(sp->s_name);
9677c478bd9Sstevel@tonic-gate 		slp->proto = strdup(sp->s_proto);
9687c478bd9Sstevel@tonic-gate 		if ((slp->name == NULL) || (slp->proto == NULL)) {
9697c478bd9Sstevel@tonic-gate 			if (slp->name) free(slp->name);
9707c478bd9Sstevel@tonic-gate 			if (slp->proto) free(slp->proto);
9717c478bd9Sstevel@tonic-gate 			free(slp);
9727c478bd9Sstevel@tonic-gate 			break;
9737c478bd9Sstevel@tonic-gate 		}
974*9525b14bSRao Shoaib 		slp->port = ntohs((u_int16_t)sp->s_port);  /*%< host byt order */
9757c478bd9Sstevel@tonic-gate 		slp->next = servicelist;
9767c478bd9Sstevel@tonic-gate 		slp->prev = NULL;
9777c478bd9Sstevel@tonic-gate 		if (servicelist)
9787c478bd9Sstevel@tonic-gate 			servicelist->prev = slp;
9797c478bd9Sstevel@tonic-gate 		servicelist = slp;
9807c478bd9Sstevel@tonic-gate 	}
9817c478bd9Sstevel@tonic-gate 	endservent();
9827c478bd9Sstevel@tonic-gate }
9837c478bd9Sstevel@tonic-gate 
9847c478bd9Sstevel@tonic-gate void
res_destroyservicelist()9857c478bd9Sstevel@tonic-gate res_destroyservicelist() {
9867c478bd9Sstevel@tonic-gate 	struct valuelist *slp, *slp_next;
9877c478bd9Sstevel@tonic-gate 
9887c478bd9Sstevel@tonic-gate 	for (slp = servicelist; slp != NULL; slp = slp_next) {
9897c478bd9Sstevel@tonic-gate 		slp_next = slp->next;
9907c478bd9Sstevel@tonic-gate 		free(slp->name);
9917c478bd9Sstevel@tonic-gate 		free(slp->proto);
9927c478bd9Sstevel@tonic-gate 		free(slp);
9937c478bd9Sstevel@tonic-gate 	}
9947c478bd9Sstevel@tonic-gate 	servicelist = (struct valuelist *)0;
9957c478bd9Sstevel@tonic-gate }
9967c478bd9Sstevel@tonic-gate 
9977c478bd9Sstevel@tonic-gate void
res_buildprotolist(void)9987c478bd9Sstevel@tonic-gate res_buildprotolist(void) {
9997c478bd9Sstevel@tonic-gate 	struct protoent *pp;
10007c478bd9Sstevel@tonic-gate 	struct valuelist *slp;
10017c478bd9Sstevel@tonic-gate 
10027c478bd9Sstevel@tonic-gate #ifdef MAYBE_HESIOD
10037c478bd9Sstevel@tonic-gate 	setprotoent(0);
10047c478bd9Sstevel@tonic-gate #else
10057c478bd9Sstevel@tonic-gate 	setprotoent(1);
10067c478bd9Sstevel@tonic-gate #endif
10077c478bd9Sstevel@tonic-gate 	while ((pp = getprotoent()) != NULL) {
10087c478bd9Sstevel@tonic-gate 		slp = (struct valuelist *)malloc(sizeof(struct valuelist));
10097c478bd9Sstevel@tonic-gate 		if (!slp)
10107c478bd9Sstevel@tonic-gate 			break;
10117c478bd9Sstevel@tonic-gate 		slp->name = strdup(pp->p_name);
10127c478bd9Sstevel@tonic-gate 		if (slp->name == NULL) {
10137c478bd9Sstevel@tonic-gate 			free(slp);
10147c478bd9Sstevel@tonic-gate 			break;
10157c478bd9Sstevel@tonic-gate 		}
1016*9525b14bSRao Shoaib 		slp->port = pp->p_proto;	/*%< host byte order */
10177c478bd9Sstevel@tonic-gate 		slp->next = protolist;
10187c478bd9Sstevel@tonic-gate 		slp->prev = NULL;
10197c478bd9Sstevel@tonic-gate 		if (protolist)
10207c478bd9Sstevel@tonic-gate 			protolist->prev = slp;
10217c478bd9Sstevel@tonic-gate 		protolist = slp;
10227c478bd9Sstevel@tonic-gate 	}
10237c478bd9Sstevel@tonic-gate 	endprotoent();
10247c478bd9Sstevel@tonic-gate }
10257c478bd9Sstevel@tonic-gate 
10267c478bd9Sstevel@tonic-gate void
res_destroyprotolist(void)10277c478bd9Sstevel@tonic-gate res_destroyprotolist(void) {
10287c478bd9Sstevel@tonic-gate 	struct valuelist *plp, *plp_next;
10297c478bd9Sstevel@tonic-gate 
10307c478bd9Sstevel@tonic-gate 	for (plp = protolist; plp != NULL; plp = plp_next) {
10317c478bd9Sstevel@tonic-gate 		plp_next = plp->next;
10327c478bd9Sstevel@tonic-gate 		free(plp->name);
10337c478bd9Sstevel@tonic-gate 		free(plp);
10347c478bd9Sstevel@tonic-gate 	}
10357c478bd9Sstevel@tonic-gate 	protolist = (struct valuelist *)0;
10367c478bd9Sstevel@tonic-gate }
10377c478bd9Sstevel@tonic-gate 
10387c478bd9Sstevel@tonic-gate static int
findservice(const char * s,struct valuelist ** list)10397c478bd9Sstevel@tonic-gate findservice(const char *s, struct valuelist **list) {
10407c478bd9Sstevel@tonic-gate 	struct valuelist *lp = *list;
10417c478bd9Sstevel@tonic-gate 	int n;
10427c478bd9Sstevel@tonic-gate 
10437c478bd9Sstevel@tonic-gate 	for (; lp != NULL; lp = lp->next)
10447c478bd9Sstevel@tonic-gate 		if (strcasecmp(lp->name, s) == 0) {
10457c478bd9Sstevel@tonic-gate 			if (lp != *list) {
10467c478bd9Sstevel@tonic-gate 				lp->prev->next = lp->next;
10477c478bd9Sstevel@tonic-gate 				if (lp->next)
10487c478bd9Sstevel@tonic-gate 					lp->next->prev = lp->prev;
10497c478bd9Sstevel@tonic-gate 				(*list)->prev = lp;
10507c478bd9Sstevel@tonic-gate 				lp->next = *list;
10517c478bd9Sstevel@tonic-gate 				*list = lp;
10527c478bd9Sstevel@tonic-gate 			}
1053*9525b14bSRao Shoaib 			return (lp->port);	/*%< host byte order */
10547c478bd9Sstevel@tonic-gate 		}
10557c478bd9Sstevel@tonic-gate 	if (sscanf(s, "%d", &n) != 1 || n <= 0)
10567c478bd9Sstevel@tonic-gate 		n = -1;
10577c478bd9Sstevel@tonic-gate 	return (n);
10587c478bd9Sstevel@tonic-gate }
10597c478bd9Sstevel@tonic-gate 
1060*9525b14bSRao Shoaib /*%
10617c478bd9Sstevel@tonic-gate  * Convert service name or (ascii) number to int.
10627c478bd9Sstevel@tonic-gate  */
10637c478bd9Sstevel@tonic-gate int
res_servicenumber(const char * p)10647c478bd9Sstevel@tonic-gate res_servicenumber(const char *p) {
10657c478bd9Sstevel@tonic-gate 	if (servicelist == (struct valuelist *)0)
10667c478bd9Sstevel@tonic-gate 		res_buildservicelist();
10677c478bd9Sstevel@tonic-gate 	return (findservice(p, &servicelist));
10687c478bd9Sstevel@tonic-gate }
10697c478bd9Sstevel@tonic-gate 
1070*9525b14bSRao Shoaib /*%
10717c478bd9Sstevel@tonic-gate  * Convert protocol name or (ascii) number to int.
10727c478bd9Sstevel@tonic-gate  */
10737c478bd9Sstevel@tonic-gate int
res_protocolnumber(const char * p)10747c478bd9Sstevel@tonic-gate res_protocolnumber(const char *p) {
10757c478bd9Sstevel@tonic-gate 	if (protolist == (struct valuelist *)0)
10767c478bd9Sstevel@tonic-gate 		res_buildprotolist();
10777c478bd9Sstevel@tonic-gate 	return (findservice(p, &protolist));
10787c478bd9Sstevel@tonic-gate }
10797c478bd9Sstevel@tonic-gate 
10807c478bd9Sstevel@tonic-gate static struct servent *
cgetservbyport(u_int16_t port,const char * proto)1081*9525b14bSRao Shoaib cgetservbyport(u_int16_t port, const char *proto) {	/*%< Host byte order. */
10827c478bd9Sstevel@tonic-gate 	struct valuelist **list = &servicelist;
10837c478bd9Sstevel@tonic-gate 	struct valuelist *lp = *list;
10847c478bd9Sstevel@tonic-gate 	static struct servent serv;
10857c478bd9Sstevel@tonic-gate 
10867c478bd9Sstevel@tonic-gate 	port = ntohs(port);
10877c478bd9Sstevel@tonic-gate 	for (; lp != NULL; lp = lp->next) {
1088*9525b14bSRao Shoaib 		if (port != (u_int16_t)lp->port)	/*%< Host byte order. */
10897c478bd9Sstevel@tonic-gate 			continue;
10907c478bd9Sstevel@tonic-gate 		if (strcasecmp(lp->proto, proto) == 0) {
10917c478bd9Sstevel@tonic-gate 			if (lp != *list) {
10927c478bd9Sstevel@tonic-gate 				lp->prev->next = lp->next;
10937c478bd9Sstevel@tonic-gate 				if (lp->next)
10947c478bd9Sstevel@tonic-gate 					lp->next->prev = lp->prev;
10957c478bd9Sstevel@tonic-gate 				(*list)->prev = lp;
10967c478bd9Sstevel@tonic-gate 				lp->next = *list;
10977c478bd9Sstevel@tonic-gate 				*list = lp;
10987c478bd9Sstevel@tonic-gate 			}
10997c478bd9Sstevel@tonic-gate 			serv.s_name = lp->name;
11007c478bd9Sstevel@tonic-gate 			serv.s_port = htons((u_int16_t)lp->port);
11017c478bd9Sstevel@tonic-gate 			serv.s_proto = lp->proto;
11027c478bd9Sstevel@tonic-gate 			return (&serv);
11037c478bd9Sstevel@tonic-gate 		}
11047c478bd9Sstevel@tonic-gate 	}
11057c478bd9Sstevel@tonic-gate 	return (0);
11067c478bd9Sstevel@tonic-gate }
11077c478bd9Sstevel@tonic-gate 
11087c478bd9Sstevel@tonic-gate static struct protoent *
cgetprotobynumber(int proto)1109*9525b14bSRao Shoaib cgetprotobynumber(int proto) {				/*%< Host byte order. */
11107c478bd9Sstevel@tonic-gate 	struct valuelist **list = &protolist;
11117c478bd9Sstevel@tonic-gate 	struct valuelist *lp = *list;
11127c478bd9Sstevel@tonic-gate 	static struct protoent prot;
11137c478bd9Sstevel@tonic-gate 
11147c478bd9Sstevel@tonic-gate 	for (; lp != NULL; lp = lp->next)
1115*9525b14bSRao Shoaib 		if (lp->port == proto) {		/*%< Host byte order. */
11167c478bd9Sstevel@tonic-gate 			if (lp != *list) {
11177c478bd9Sstevel@tonic-gate 				lp->prev->next = lp->next;
11187c478bd9Sstevel@tonic-gate 				if (lp->next)
11197c478bd9Sstevel@tonic-gate 					lp->next->prev = lp->prev;
11207c478bd9Sstevel@tonic-gate 				(*list)->prev = lp;
11217c478bd9Sstevel@tonic-gate 				lp->next = *list;
11227c478bd9Sstevel@tonic-gate 				*list = lp;
11237c478bd9Sstevel@tonic-gate 			}
11247c478bd9Sstevel@tonic-gate 			prot.p_name = lp->name;
1125*9525b14bSRao Shoaib 			prot.p_proto = lp->port;	/*%< Host byte order. */
11267c478bd9Sstevel@tonic-gate 			return (&prot);
11277c478bd9Sstevel@tonic-gate 		}
11287c478bd9Sstevel@tonic-gate 	return (0);
11297c478bd9Sstevel@tonic-gate }
11307c478bd9Sstevel@tonic-gate 
11317c478bd9Sstevel@tonic-gate const char *
res_protocolname(int num)11327c478bd9Sstevel@tonic-gate res_protocolname(int num) {
11337c478bd9Sstevel@tonic-gate 	static char number[8];
11347c478bd9Sstevel@tonic-gate 	struct protoent *pp;
11357c478bd9Sstevel@tonic-gate 
11367c478bd9Sstevel@tonic-gate 	if (protolist == (struct valuelist *)0)
11377c478bd9Sstevel@tonic-gate 		res_buildprotolist();
11387c478bd9Sstevel@tonic-gate 	pp = cgetprotobynumber(num);
11397c478bd9Sstevel@tonic-gate 	if (pp == 0)  {
11407c478bd9Sstevel@tonic-gate 		(void) sprintf(number, "%d", num);
11417c478bd9Sstevel@tonic-gate 		return (number);
11427c478bd9Sstevel@tonic-gate 	}
11437c478bd9Sstevel@tonic-gate 	return (pp->p_name);
11447c478bd9Sstevel@tonic-gate }
11457c478bd9Sstevel@tonic-gate 
11467c478bd9Sstevel@tonic-gate const char *
res_servicename(u_int16_t port,const char * proto)1147*9525b14bSRao Shoaib res_servicename(u_int16_t port, const char *proto) {	/*%< Host byte order. */
11487c478bd9Sstevel@tonic-gate 	static char number[8];
11497c478bd9Sstevel@tonic-gate 	struct servent *ss;
11507c478bd9Sstevel@tonic-gate 
11517c478bd9Sstevel@tonic-gate 	if (servicelist == (struct valuelist *)0)
11527c478bd9Sstevel@tonic-gate 		res_buildservicelist();
11537c478bd9Sstevel@tonic-gate 	ss = cgetservbyport(htons(port), proto);
11547c478bd9Sstevel@tonic-gate 	if (ss == 0)  {
1155