xref: /onnv-gate/usr/src/lib/libresolv2/common/resolv/res_mkupdate.c (revision 11038:74b12212b8a2)
10Sstevel@tonic-gate /*
2*11038SRao.Shoaib@Sun.COM  * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
30Sstevel@tonic-gate  * Copyright (c) 1996-1999 by Internet Software Consortium.
40Sstevel@tonic-gate  *
50Sstevel@tonic-gate  * Permission to use, copy, modify, and distribute this software for any
60Sstevel@tonic-gate  * purpose with or without fee is hereby granted, provided that the above
70Sstevel@tonic-gate  * copyright notice and this permission notice appear in all copies.
80Sstevel@tonic-gate  *
9*11038SRao.Shoaib@Sun.COM  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
10*11038SRao.Shoaib@Sun.COM  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11*11038SRao.Shoaib@Sun.COM  * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
12*11038SRao.Shoaib@Sun.COM  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13*11038SRao.Shoaib@Sun.COM  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14*11038SRao.Shoaib@Sun.COM  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
15*11038SRao.Shoaib@Sun.COM  * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
160Sstevel@tonic-gate  */
170Sstevel@tonic-gate 
18*11038SRao.Shoaib@Sun.COM /*! \file
19*11038SRao.Shoaib@Sun.COM  * \brief
200Sstevel@tonic-gate  * Based on the Dynamic DNS reference implementation by Viraj Bais
21*11038SRao.Shoaib@Sun.COM  * <viraj_bais@ccm.fm.intel.com>
220Sstevel@tonic-gate  */
230Sstevel@tonic-gate 
240Sstevel@tonic-gate #if !defined(lint) && !defined(SABER)
25*11038SRao.Shoaib@Sun.COM static const char rcsid[] = "$Id: res_mkupdate.c,v 1.10 2008/12/11 09:59:00 marka Exp $";
260Sstevel@tonic-gate #endif /* not lint */
270Sstevel@tonic-gate 
280Sstevel@tonic-gate #include "port_before.h"
290Sstevel@tonic-gate 
300Sstevel@tonic-gate #include <sys/types.h>
310Sstevel@tonic-gate #include <sys/param.h>
320Sstevel@tonic-gate 
330Sstevel@tonic-gate #include <netinet/in.h>
340Sstevel@tonic-gate #include <arpa/nameser.h>
350Sstevel@tonic-gate #include <arpa/inet.h>
360Sstevel@tonic-gate 
370Sstevel@tonic-gate #include <errno.h>
380Sstevel@tonic-gate #include <limits.h>
390Sstevel@tonic-gate #include <netdb.h>
400Sstevel@tonic-gate #include <resolv.h>
410Sstevel@tonic-gate #include <res_update.h>
420Sstevel@tonic-gate #include <stdio.h>
430Sstevel@tonic-gate #include <stdlib.h>
440Sstevel@tonic-gate #include <string.h>
450Sstevel@tonic-gate #include <unistd.h>
460Sstevel@tonic-gate #include <ctype.h>
470Sstevel@tonic-gate 
480Sstevel@tonic-gate #include "port_after.h"
490Sstevel@tonic-gate 
500Sstevel@tonic-gate /* Options.  Leave them on. */
510Sstevel@tonic-gate #define DEBUG
520Sstevel@tonic-gate #define MAXPORT 1024
530Sstevel@tonic-gate 
540Sstevel@tonic-gate static int getnum_str(u_char **, u_char *);
550Sstevel@tonic-gate static int gethexnum_str(u_char **, u_char *);
560Sstevel@tonic-gate static int getword_str(char *, int, u_char **, u_char *);
570Sstevel@tonic-gate static int getstr_str(char *, int, u_char **, u_char *);
580Sstevel@tonic-gate 
590Sstevel@tonic-gate #define ShrinkBuffer(x)  if ((buflen -= x) < 0) return (-2);
600Sstevel@tonic-gate 
610Sstevel@tonic-gate /* Forward. */
620Sstevel@tonic-gate 
630Sstevel@tonic-gate int res_protocolnumber(const char *);
640Sstevel@tonic-gate int res_servicenumber(const char *);
650Sstevel@tonic-gate 
66*11038SRao.Shoaib@Sun.COM /*%
670Sstevel@tonic-gate  * Form update packets.
680Sstevel@tonic-gate  * Returns the size of the resulting packet if no error
69*11038SRao.Shoaib@Sun.COM  *
700Sstevel@tonic-gate  * On error,
71*11038SRao.Shoaib@Sun.COM  *	returns
72*11038SRao.Shoaib@Sun.COM  *\li              -1 if error in reading a word/number in rdata
730Sstevel@tonic-gate  *		   portion for update packets
74*11038SRao.Shoaib@Sun.COM  *\li		-2 if length of buffer passed is insufficient
75*11038SRao.Shoaib@Sun.COM  *\li		-3 if zone section is not the first section in
760Sstevel@tonic-gate  *		   the linked list, or section order has a problem
77*11038SRao.Shoaib@Sun.COM  *\li		-4 on a number overflow
78*11038SRao.Shoaib@Sun.COM  *\li		-5 unknown operation or no records
790Sstevel@tonic-gate  */
800Sstevel@tonic-gate int
res_nmkupdate(res_state statp,ns_updrec * rrecp_in,u_char * buf,int buflen)810Sstevel@tonic-gate res_nmkupdate(res_state statp, ns_updrec *rrecp_in, u_char *buf, int buflen) {
820Sstevel@tonic-gate 	ns_updrec *rrecp_start = rrecp_in;
830Sstevel@tonic-gate 	HEADER *hp;
84*11038SRao.Shoaib@Sun.COM 	u_char *cp, *sp2, *startp, *endp;
850Sstevel@tonic-gate 	int n, i, soanum, multiline;
860Sstevel@tonic-gate 	ns_updrec *rrecp;
870Sstevel@tonic-gate 	struct in_addr ina;
880Sstevel@tonic-gate 	struct in6_addr in6a;
890Sstevel@tonic-gate         char buf2[MAXDNAME];
900Sstevel@tonic-gate 	u_char buf3[MAXDNAME];
910Sstevel@tonic-gate 	int section, numrrs = 0, counts[ns_s_max];
920Sstevel@tonic-gate 	u_int16_t rtype, rclass;
930Sstevel@tonic-gate 	u_int32_t n1, rttl;
940Sstevel@tonic-gate 	u_char *dnptrs[20], **dpp, **lastdnptr;
950Sstevel@tonic-gate 	int siglen, keylen, certlen;
960Sstevel@tonic-gate 
970Sstevel@tonic-gate 	/*
980Sstevel@tonic-gate 	 * Initialize header fields.
990Sstevel@tonic-gate 	 */
1000Sstevel@tonic-gate 	if ((buf == NULL) || (buflen < HFIXEDSZ))
1010Sstevel@tonic-gate 		return (-1);
1020Sstevel@tonic-gate 	memset(buf, 0, HFIXEDSZ);
1030Sstevel@tonic-gate 	hp = (HEADER *) buf;
104*11038SRao.Shoaib@Sun.COM 	statp->id = res_nrandomid(statp);
105*11038SRao.Shoaib@Sun.COM 	hp->id = htons(statp->id);
1060Sstevel@tonic-gate 	hp->opcode = ns_o_update;
1070Sstevel@tonic-gate 	hp->rcode = NOERROR;
1080Sstevel@tonic-gate 	cp = buf + HFIXEDSZ;
1090Sstevel@tonic-gate 	buflen -= HFIXEDSZ;
1100Sstevel@tonic-gate 	dpp = dnptrs;
1110Sstevel@tonic-gate 	*dpp++ = buf;
1120Sstevel@tonic-gate 	*dpp++ = NULL;
1130Sstevel@tonic-gate 	lastdnptr = dnptrs + sizeof dnptrs / sizeof dnptrs[0];
1140Sstevel@tonic-gate 
1150Sstevel@tonic-gate 	if (rrecp_start == NULL)
1160Sstevel@tonic-gate 		return (-5);
1170Sstevel@tonic-gate 	else if (rrecp_start->r_section != S_ZONE)
1180Sstevel@tonic-gate 		return (-3);
1190Sstevel@tonic-gate 
1200Sstevel@tonic-gate 	memset(counts, 0, sizeof counts);
1210Sstevel@tonic-gate 	for (rrecp = rrecp_start; rrecp; rrecp = NEXT(rrecp, r_glink)) {
1220Sstevel@tonic-gate 		numrrs++;
1230Sstevel@tonic-gate                 section = rrecp->r_section;
1240Sstevel@tonic-gate 		if (section < 0 || section >= ns_s_max)
1250Sstevel@tonic-gate 			return (-1);
1260Sstevel@tonic-gate 		counts[section]++;
1270Sstevel@tonic-gate 		for (i = section + 1; i < ns_s_max; i++)
1280Sstevel@tonic-gate 			if (counts[i])
1290Sstevel@tonic-gate 				return (-3);
1300Sstevel@tonic-gate 		rtype = rrecp->r_type;
1310Sstevel@tonic-gate 		rclass = rrecp->r_class;
1320Sstevel@tonic-gate 		rttl = rrecp->r_ttl;
1330Sstevel@tonic-gate 		/* overload class and type */
1340Sstevel@tonic-gate 		if (section == S_PREREQ) {
1350Sstevel@tonic-gate 			rttl = 0;
1360Sstevel@tonic-gate 			switch (rrecp->r_opcode) {
1370Sstevel@tonic-gate 			case YXDOMAIN:
1380Sstevel@tonic-gate 				rclass = C_ANY;
1390Sstevel@tonic-gate 				rtype = T_ANY;
1400Sstevel@tonic-gate 				rrecp->r_size = 0;
1410Sstevel@tonic-gate 				break;
1420Sstevel@tonic-gate 			case NXDOMAIN:
1430Sstevel@tonic-gate 				rclass = C_NONE;
1440Sstevel@tonic-gate 				rtype = T_ANY;
1450Sstevel@tonic-gate 				rrecp->r_size = 0;
1460Sstevel@tonic-gate 				break;
1470Sstevel@tonic-gate 			case NXRRSET:
1480Sstevel@tonic-gate 				rclass = C_NONE;
1490Sstevel@tonic-gate 				rrecp->r_size = 0;
1500Sstevel@tonic-gate 				break;
1510Sstevel@tonic-gate 			case YXRRSET:
1520Sstevel@tonic-gate 				if (rrecp->r_size == 0)
1530Sstevel@tonic-gate 					rclass = C_ANY;
1540Sstevel@tonic-gate 				break;
1550Sstevel@tonic-gate 			default:
1560Sstevel@tonic-gate 				fprintf(stderr,
1570Sstevel@tonic-gate 					"res_mkupdate: incorrect opcode: %d\n",
1580Sstevel@tonic-gate 					rrecp->r_opcode);
1590Sstevel@tonic-gate 				fflush(stderr);
1600Sstevel@tonic-gate 				return (-1);
1610Sstevel@tonic-gate 			}
1620Sstevel@tonic-gate 		} else if (section == S_UPDATE) {
1630Sstevel@tonic-gate 			switch (rrecp->r_opcode) {
1640Sstevel@tonic-gate 			case DELETE:
1650Sstevel@tonic-gate 				rclass = rrecp->r_size == 0 ? C_ANY : C_NONE;
1660Sstevel@tonic-gate 				break;
1670Sstevel@tonic-gate 			case ADD:
1680Sstevel@tonic-gate 				break;
1690Sstevel@tonic-gate 			default:
1700Sstevel@tonic-gate 				fprintf(stderr,
1710Sstevel@tonic-gate 					"res_mkupdate: incorrect opcode: %d\n",
1720Sstevel@tonic-gate 					rrecp->r_opcode);
1730Sstevel@tonic-gate 				fflush(stderr);
1740Sstevel@tonic-gate 				return (-1);
1750Sstevel@tonic-gate 			}
1760Sstevel@tonic-gate 		}
1770Sstevel@tonic-gate 
1780Sstevel@tonic-gate 		/*
1790Sstevel@tonic-gate 		 * XXX	appending default domain to owner name is omitted,
1800Sstevel@tonic-gate 		 *	fqdn must be provided
1810Sstevel@tonic-gate 		 */
1820Sstevel@tonic-gate 		if ((n = dn_comp(rrecp->r_dname, cp, buflen, dnptrs,
1830Sstevel@tonic-gate 				 lastdnptr)) < 0)
1840Sstevel@tonic-gate 			return (-1);
1850Sstevel@tonic-gate 		cp += n;
1860Sstevel@tonic-gate 		ShrinkBuffer(n + 2*INT16SZ);
1870Sstevel@tonic-gate 		PUTSHORT(rtype, cp);
1880Sstevel@tonic-gate 		PUTSHORT(rclass, cp);
1890Sstevel@tonic-gate 		if (section == S_ZONE) {
1900Sstevel@tonic-gate 			if (numrrs != 1 || rrecp->r_type != T_SOA)
1910Sstevel@tonic-gate 				return (-3);
1920Sstevel@tonic-gate 			continue;
1930Sstevel@tonic-gate 		}
1940Sstevel@tonic-gate 		ShrinkBuffer(INT32SZ + INT16SZ);
1950Sstevel@tonic-gate 		PUTLONG(rttl, cp);
196*11038SRao.Shoaib@Sun.COM 		sp2 = cp;  /*%< save pointer to length byte */
1970Sstevel@tonic-gate 		cp += INT16SZ;
1980Sstevel@tonic-gate 		if (rrecp->r_size == 0) {
1990Sstevel@tonic-gate 			if (section == S_UPDATE && rclass != C_ANY)
2000Sstevel@tonic-gate 				return (-1);
2010Sstevel@tonic-gate 			else {
2020Sstevel@tonic-gate 				PUTSHORT(0, sp2);
2030Sstevel@tonic-gate 				continue;
2040Sstevel@tonic-gate 			}
2050Sstevel@tonic-gate 		}
2060Sstevel@tonic-gate 		startp = rrecp->r_data;
2070Sstevel@tonic-gate 		endp = startp + rrecp->r_size - 1;
2080Sstevel@tonic-gate 		/* XXX this should be done centrally. */
2090Sstevel@tonic-gate 		switch (rrecp->r_type) {
2100Sstevel@tonic-gate 		case T_A:
2110Sstevel@tonic-gate 			if (!getword_str(buf2, sizeof buf2, &startp, endp))
2120Sstevel@tonic-gate 				return (-1);
2130Sstevel@tonic-gate 			if (!inet_aton(buf2, &ina))
2140Sstevel@tonic-gate 				return (-1);
2150Sstevel@tonic-gate 			n1 = ntohl(ina.s_addr);
2160Sstevel@tonic-gate 			ShrinkBuffer(INT32SZ);
2170Sstevel@tonic-gate 			PUTLONG(n1, cp);
2180Sstevel@tonic-gate 			break;
2190Sstevel@tonic-gate 		case T_CNAME:
2200Sstevel@tonic-gate 		case T_MB:
2210Sstevel@tonic-gate 		case T_MG:
2220Sstevel@tonic-gate 		case T_MR:
2230Sstevel@tonic-gate 		case T_NS:
2240Sstevel@tonic-gate 		case T_PTR:
225*11038SRao.Shoaib@Sun.COM 		case ns_t_dname:
2260Sstevel@tonic-gate 			if (!getword_str(buf2, sizeof buf2, &startp, endp))
2270Sstevel@tonic-gate 				return (-1);
2280Sstevel@tonic-gate 			n = dn_comp(buf2, cp, buflen, dnptrs, lastdnptr);
2290Sstevel@tonic-gate 			if (n < 0)
2300Sstevel@tonic-gate 				return (-1);
2310Sstevel@tonic-gate 			cp += n;
2320Sstevel@tonic-gate 			ShrinkBuffer(n);
2330Sstevel@tonic-gate 			break;
2340Sstevel@tonic-gate 		case T_MINFO:
2350Sstevel@tonic-gate 		case T_SOA:
2360Sstevel@tonic-gate 		case T_RP:
2370Sstevel@tonic-gate 			for (i = 0; i < 2; i++) {
2380Sstevel@tonic-gate 				if (!getword_str(buf2, sizeof buf2, &startp,
2390Sstevel@tonic-gate 						 endp))
2400Sstevel@tonic-gate 				return (-1);
2410Sstevel@tonic-gate 				n = dn_comp(buf2, cp, buflen,
2420Sstevel@tonic-gate 					    dnptrs, lastdnptr);
2430Sstevel@tonic-gate 				if (n < 0)
2440Sstevel@tonic-gate 					return (-1);
2450Sstevel@tonic-gate 				cp += n;
2460Sstevel@tonic-gate 				ShrinkBuffer(n);
2470Sstevel@tonic-gate 			}
2480Sstevel@tonic-gate 			if (rrecp->r_type == T_SOA) {
2490Sstevel@tonic-gate 				ShrinkBuffer(5 * INT32SZ);
2500Sstevel@tonic-gate 				while (isspace(*startp) || !*startp)
2510Sstevel@tonic-gate 					startp++;
2520Sstevel@tonic-gate 				if (*startp == '(') {
2530Sstevel@tonic-gate 					multiline = 1;
2540Sstevel@tonic-gate 					startp++;
2550Sstevel@tonic-gate 				} else
2560Sstevel@tonic-gate 					multiline = 0;
2570Sstevel@tonic-gate 				/* serial, refresh, retry, expire, minimum */
2580Sstevel@tonic-gate 				for (i = 0; i < 5; i++) {
2590Sstevel@tonic-gate 					soanum = getnum_str(&startp, endp);
2600Sstevel@tonic-gate 					if (soanum < 0)
2610Sstevel@tonic-gate 						return (-1);
2620Sstevel@tonic-gate 					PUTLONG(soanum, cp);
2630Sstevel@tonic-gate 				}
2640Sstevel@tonic-gate 				if (multiline) {
2650Sstevel@tonic-gate 					while (isspace(*startp) || !*startp)
2660Sstevel@tonic-gate 						startp++;
2670Sstevel@tonic-gate 					if (*startp != ')')
2680Sstevel@tonic-gate 						return (-1);
2690Sstevel@tonic-gate 				}
2700Sstevel@tonic-gate 			}
2710Sstevel@tonic-gate 			break;
2720Sstevel@tonic-gate 		case T_MX:
2730Sstevel@tonic-gate 		case T_AFSDB:
2740Sstevel@tonic-gate 		case T_RT:
2750Sstevel@tonic-gate 			n = getnum_str(&startp, endp);
2760Sstevel@tonic-gate 			if (n < 0)
2770Sstevel@tonic-gate 				return (-1);
2780Sstevel@tonic-gate 			ShrinkBuffer(INT16SZ);
2790Sstevel@tonic-gate 			PUTSHORT(n, cp);
2800Sstevel@tonic-gate 			if (!getword_str(buf2, sizeof buf2, &startp, endp))
2810Sstevel@tonic-gate 				return (-1);
2820Sstevel@tonic-gate 			n = dn_comp(buf2, cp, buflen, dnptrs, lastdnptr);
2830Sstevel@tonic-gate 			if (n < 0)
2840Sstevel@tonic-gate 				return (-1);
2850Sstevel@tonic-gate 			cp += n;
2860Sstevel@tonic-gate 			ShrinkBuffer(n);
2870Sstevel@tonic-gate 			break;
2880Sstevel@tonic-gate 		case T_SRV:
2890Sstevel@tonic-gate 			n = getnum_str(&startp, endp);
2900Sstevel@tonic-gate 			if (n < 0)
2910Sstevel@tonic-gate 				return (-1);
2920Sstevel@tonic-gate 			ShrinkBuffer(INT16SZ);
2930Sstevel@tonic-gate 			PUTSHORT(n, cp);
2940Sstevel@tonic-gate 
2950Sstevel@tonic-gate 			n = getnum_str(&startp, endp);
2960Sstevel@tonic-gate 			if (n < 0)
2970Sstevel@tonic-gate 				return (-1);
2980Sstevel@tonic-gate 			ShrinkBuffer(INT16SZ);
2990Sstevel@tonic-gate 			PUTSHORT(n, cp);
3000Sstevel@tonic-gate 
3010Sstevel@tonic-gate 			n = getnum_str(&startp, endp);
3020Sstevel@tonic-gate 			if (n < 0)
3030Sstevel@tonic-gate 				return (-1);
3040Sstevel@tonic-gate 			ShrinkBuffer(INT16SZ);
3050Sstevel@tonic-gate 			PUTSHORT(n, cp);
3060Sstevel@tonic-gate 
3070Sstevel@tonic-gate 			if (!getword_str(buf2, sizeof buf2, &startp, endp))
3080Sstevel@tonic-gate 				return (-1);
3090Sstevel@tonic-gate 			n = dn_comp(buf2, cp, buflen, NULL, NULL);
3100Sstevel@tonic-gate 			if (n < 0)
3110Sstevel@tonic-gate 				return (-1);
3120Sstevel@tonic-gate 			cp += n;
3130Sstevel@tonic-gate 			ShrinkBuffer(n);
3140Sstevel@tonic-gate 			break;
3150Sstevel@tonic-gate 		case T_PX:
3160Sstevel@tonic-gate 			n = getnum_str(&startp, endp);
3170Sstevel@tonic-gate 			if (n < 0)
3180Sstevel@tonic-gate 				return (-1);
3190Sstevel@tonic-gate 			PUTSHORT(n, cp);
3200Sstevel@tonic-gate 			ShrinkBuffer(INT16SZ);
3210Sstevel@tonic-gate 			for (i = 0; i < 2; i++) {
3220Sstevel@tonic-gate 				if (!getword_str(buf2, sizeof buf2, &startp,
3230Sstevel@tonic-gate 						 endp))
3240Sstevel@tonic-gate 					return (-1);
3250Sstevel@tonic-gate 				n = dn_comp(buf2, cp, buflen, dnptrs,
3260Sstevel@tonic-gate 					    lastdnptr);
3270Sstevel@tonic-gate 				if (n < 0)
3280Sstevel@tonic-gate 					return (-1);
3290Sstevel@tonic-gate 				cp += n;
3300Sstevel@tonic-gate 				ShrinkBuffer(n);
3310Sstevel@tonic-gate 			}
3320Sstevel@tonic-gate 			break;
3330Sstevel@tonic-gate 		case T_WKS: {
3340Sstevel@tonic-gate 			char bm[MAXPORT/8];
3350Sstevel@tonic-gate 			unsigned int maxbm = 0;
3360Sstevel@tonic-gate 
3370Sstevel@tonic-gate 			if (!getword_str(buf2, sizeof buf2, &startp, endp))
3380Sstevel@tonic-gate 				return (-1);
3390Sstevel@tonic-gate 			if (!inet_aton(buf2, &ina))
3400Sstevel@tonic-gate 				return (-1);
3410Sstevel@tonic-gate 			n1 = ntohl(ina.s_addr);
3420Sstevel@tonic-gate 			ShrinkBuffer(INT32SZ);
3430Sstevel@tonic-gate 			PUTLONG(n1, cp);
3440Sstevel@tonic-gate 
3450Sstevel@tonic-gate 			if (!getword_str(buf2, sizeof buf2, &startp, endp))
3460Sstevel@tonic-gate 				return (-1);
3470Sstevel@tonic-gate 			if ((i = res_protocolnumber(buf2)) < 0)
3480Sstevel@tonic-gate 				return (-1);
3490Sstevel@tonic-gate 			ShrinkBuffer(1);
3500Sstevel@tonic-gate 			*cp++ = i & 0xff;
3510Sstevel@tonic-gate 
3520Sstevel@tonic-gate 			for (i = 0; i < MAXPORT/8 ; i++)
3530Sstevel@tonic-gate 				bm[i] = 0;
3540Sstevel@tonic-gate 
3550Sstevel@tonic-gate 			while (getword_str(buf2, sizeof buf2, &startp, endp)) {
356*11038SRao.Shoaib@Sun.COM 				if ((n = res_servicenumber(buf2)) <= 0)
3570Sstevel@tonic-gate 					return (-1);
3580Sstevel@tonic-gate 
359*11038SRao.Shoaib@Sun.COM 				if (n < MAXPORT) {
360*11038SRao.Shoaib@Sun.COM 					bm[n/8] |= (0x80>>(n%8));
361*11038SRao.Shoaib@Sun.COM 					if ((unsigned)n > maxbm)
362*11038SRao.Shoaib@Sun.COM 						maxbm = n;
3630Sstevel@tonic-gate 				} else
3640Sstevel@tonic-gate 					return (-1);
3650Sstevel@tonic-gate 			}
3660Sstevel@tonic-gate 			maxbm = maxbm/8 + 1;
3670Sstevel@tonic-gate 			ShrinkBuffer(maxbm);
3680Sstevel@tonic-gate 			memcpy(cp, bm, maxbm);
3690Sstevel@tonic-gate 			cp += maxbm;
3700Sstevel@tonic-gate 			break;
3710Sstevel@tonic-gate 		}
3720Sstevel@tonic-gate 		case T_HINFO:
3730Sstevel@tonic-gate 			for (i = 0; i < 2; i++) {
3740Sstevel@tonic-gate 				if ((n = getstr_str(buf2, sizeof buf2,
3750Sstevel@tonic-gate 						&startp, endp)) < 0)
3760Sstevel@tonic-gate 					return (-1);
3770Sstevel@tonic-gate 				if (n > 255)
3780Sstevel@tonic-gate 					return (-1);
3790Sstevel@tonic-gate 				ShrinkBuffer(n+1);
3800Sstevel@tonic-gate 				*cp++ = n;
3810Sstevel@tonic-gate 				memcpy(cp, buf2, n);
3820Sstevel@tonic-gate 				cp += n;
3830Sstevel@tonic-gate 			}
3840Sstevel@tonic-gate 			break;
3850Sstevel@tonic-gate 		case T_TXT:
386*11038SRao.Shoaib@Sun.COM 			for (;;) {
3870Sstevel@tonic-gate 				if ((n = getstr_str(buf2, sizeof buf2,
3880Sstevel@tonic-gate 						&startp, endp)) < 0) {
3890Sstevel@tonic-gate 					if (cp != (sp2 + INT16SZ))
3900Sstevel@tonic-gate 						break;
3910Sstevel@tonic-gate 					return (-1);
3920Sstevel@tonic-gate 				}
3930Sstevel@tonic-gate 				if (n > 255)
3940Sstevel@tonic-gate 					return (-1);
3950Sstevel@tonic-gate 				ShrinkBuffer(n+1);
3960Sstevel@tonic-gate 				*cp++ = n;
3970Sstevel@tonic-gate 				memcpy(cp, buf2, n);
3980Sstevel@tonic-gate 				cp += n;
3990Sstevel@tonic-gate 			}
4000Sstevel@tonic-gate 			break;
4010Sstevel@tonic-gate 		case T_X25:
402*11038SRao.Shoaib@Sun.COM 			/* RFC1183 */
4030Sstevel@tonic-gate 			if ((n = getstr_str(buf2, sizeof buf2, &startp,
4040Sstevel@tonic-gate 					 endp)) < 0)
4050Sstevel@tonic-gate 				return (-1);
4060Sstevel@tonic-gate 			if (n > 255)
4070Sstevel@tonic-gate 				return (-1);
4080Sstevel@tonic-gate 			ShrinkBuffer(n+1);
4090Sstevel@tonic-gate 			*cp++ = n;
4100Sstevel@tonic-gate 			memcpy(cp, buf2, n);
4110Sstevel@tonic-gate 			cp += n;
4120Sstevel@tonic-gate 			break;
4130Sstevel@tonic-gate 		case T_ISDN:
414*11038SRao.Shoaib@Sun.COM 			/* RFC1183 */
4150Sstevel@tonic-gate 			if ((n = getstr_str(buf2, sizeof buf2, &startp,
4160Sstevel@tonic-gate 					 endp)) < 0)
4170Sstevel@tonic-gate 				return (-1);
4180Sstevel@tonic-gate 			if ((n > 255) || (n == 0))
4190Sstevel@tonic-gate 				return (-1);
4200Sstevel@tonic-gate 			ShrinkBuffer(n+1);
4210Sstevel@tonic-gate 			*cp++ = n;
4220Sstevel@tonic-gate 			memcpy(cp, buf2, n);
4230Sstevel@tonic-gate 			cp += n;
4240Sstevel@tonic-gate 			if ((n = getstr_str(buf2, sizeof buf2, &startp,
4250Sstevel@tonic-gate 					 endp)) < 0)
4260Sstevel@tonic-gate 				n = 0;
4270Sstevel@tonic-gate 			if (n > 255)
4280Sstevel@tonic-gate 				return (-1);
4290Sstevel@tonic-gate 			ShrinkBuffer(n+1);
4300Sstevel@tonic-gate 			*cp++ = n;
4310Sstevel@tonic-gate 			memcpy(cp, buf2, n);
4320Sstevel@tonic-gate 			cp += n;
4330Sstevel@tonic-gate 			break;
4340Sstevel@tonic-gate 		case T_NSAP:
4350Sstevel@tonic-gate 			if ((n = inet_nsap_addr((char *)startp, (u_char *)buf2, sizeof(buf2))) != 0) {
4360Sstevel@tonic-gate 				ShrinkBuffer(n);
4370Sstevel@tonic-gate 				memcpy(cp, buf2, n);
4380Sstevel@tonic-gate 				cp += n;
4390Sstevel@tonic-gate 			} else {
4400Sstevel@tonic-gate 				return (-1);
4410Sstevel@tonic-gate 			}
4420Sstevel@tonic-gate 			break;
4430Sstevel@tonic-gate 		case T_LOC:
4440Sstevel@tonic-gate 			if ((n = loc_aton((char *)startp, (u_char *)buf2)) != 0) {
4450Sstevel@tonic-gate 				ShrinkBuffer(n);
4460Sstevel@tonic-gate 				memcpy(cp, buf2, n);
4470Sstevel@tonic-gate 				cp += n;
4480Sstevel@tonic-gate 			} else
4490Sstevel@tonic-gate 				return (-1);
4500Sstevel@tonic-gate 			break;
4510Sstevel@tonic-gate 		case ns_t_sig:
4520Sstevel@tonic-gate 		    {
4530Sstevel@tonic-gate 			int sig_type, success, dateerror;
4540Sstevel@tonic-gate 			u_int32_t exptime, timesigned;
4550Sstevel@tonic-gate 
4560Sstevel@tonic-gate 			/* type */
4570Sstevel@tonic-gate 			if ((n = getword_str(buf2, sizeof buf2,
4580Sstevel@tonic-gate 					     &startp, endp)) < 0)
4590Sstevel@tonic-gate 				return (-1);
4600Sstevel@tonic-gate 			sig_type = sym_ston(__p_type_syms, buf2, &success);
4610Sstevel@tonic-gate 			if (!success || sig_type == ns_t_any)
4620Sstevel@tonic-gate 				return (-1);
4630Sstevel@tonic-gate 			ShrinkBuffer(INT16SZ);
4640Sstevel@tonic-gate 			PUTSHORT(sig_type, cp);
4650Sstevel@tonic-gate 			/* alg */
4660Sstevel@tonic-gate 			n = getnum_str(&startp, endp);
4670Sstevel@tonic-gate 			if (n < 0)
4680Sstevel@tonic-gate 				return (-1);
4690Sstevel@tonic-gate 			ShrinkBuffer(1);
4700Sstevel@tonic-gate 			*cp++ = n;
4710Sstevel@tonic-gate 			/* labels */
4720Sstevel@tonic-gate 			n = getnum_str(&startp, endp);
4730Sstevel@tonic-gate 			if (n <= 0 || n > 255)
4740Sstevel@tonic-gate 				return (-1);
4750Sstevel@tonic-gate 			ShrinkBuffer(1);
4760Sstevel@tonic-gate 			*cp++ = n;
4770Sstevel@tonic-gate 			/* ottl  & expire */
4780Sstevel@tonic-gate 			if (!getword_str(buf2, sizeof buf2, &startp, endp))
4790Sstevel@tonic-gate 				return (-1);
4800Sstevel@tonic-gate 			exptime = ns_datetosecs(buf2, &dateerror);
4810Sstevel@tonic-gate 			if (!dateerror) {
4820Sstevel@tonic-gate 				ShrinkBuffer(INT32SZ);
4830Sstevel@tonic-gate 				PUTLONG(rttl, cp);
4840Sstevel@tonic-gate 			}
4850Sstevel@tonic-gate 			else {
4860Sstevel@tonic-gate 				char *ulendp;
4870Sstevel@tonic-gate 				u_int32_t ottl;
4880Sstevel@tonic-gate 
4890Sstevel@tonic-gate 				errno = 0;
4900Sstevel@tonic-gate 				ottl = strtoul(buf2, &ulendp, 10);
4910Sstevel@tonic-gate 				if (errno != 0 ||
4920Sstevel@tonic-gate 				    (ulendp != NULL && *ulendp != '\0'))
4930Sstevel@tonic-gate 					return (-1);
4940Sstevel@tonic-gate 				ShrinkBuffer(INT32SZ);
4950Sstevel@tonic-gate 				PUTLONG(ottl, cp);
4960Sstevel@tonic-gate 				if (!getword_str(buf2, sizeof buf2, &startp,
4970Sstevel@tonic-gate 						 endp))
4980Sstevel@tonic-gate 					return (-1);
4990Sstevel@tonic-gate 				exptime = ns_datetosecs(buf2, &dateerror);
5000Sstevel@tonic-gate 				if (dateerror)
5010Sstevel@tonic-gate 					return (-1);
5020Sstevel@tonic-gate 			}
5030Sstevel@tonic-gate 			/* expire */
5040Sstevel@tonic-gate 			ShrinkBuffer(INT32SZ);
5050Sstevel@tonic-gate 			PUTLONG(exptime, cp);
5060Sstevel@tonic-gate 			/* timesigned */
5070Sstevel@tonic-gate 			if (!getword_str(buf2, sizeof buf2, &startp, endp))
5080Sstevel@tonic-gate 				return (-1);
5090Sstevel@tonic-gate 			timesigned = ns_datetosecs(buf2, &dateerror);
5100Sstevel@tonic-gate 			if (!dateerror) {
5110Sstevel@tonic-gate 				ShrinkBuffer(INT32SZ);
5120Sstevel@tonic-gate 				PUTLONG(timesigned, cp);
5130Sstevel@tonic-gate 			}
5140Sstevel@tonic-gate 			else
5150Sstevel@tonic-gate 				return (-1);
5160Sstevel@tonic-gate 			/* footprint */
5170Sstevel@tonic-gate 			n = getnum_str(&startp, endp);
5180Sstevel@tonic-gate 			if (n < 0)
5190Sstevel@tonic-gate 				return (-1);
5200Sstevel@tonic-gate 			ShrinkBuffer(INT16SZ);
5210Sstevel@tonic-gate 			PUTSHORT(n, cp);
5220Sstevel@tonic-gate 			/* signer name */
5230Sstevel@tonic-gate 			if (!getword_str(buf2, sizeof buf2, &startp, endp))
5240Sstevel@tonic-gate 				return (-1);
5250Sstevel@tonic-gate 			n = dn_comp(buf2, cp, buflen, dnptrs, lastdnptr);
5260Sstevel@tonic-gate 			if (n < 0)
5270Sstevel@tonic-gate 				return (-1);
5280Sstevel@tonic-gate 			cp += n;
5290Sstevel@tonic-gate 			ShrinkBuffer(n);
5300Sstevel@tonic-gate 			/* sig */
5310Sstevel@tonic-gate 			if ((n = getword_str(buf2, sizeof buf2,
5320Sstevel@tonic-gate 					     &startp, endp)) < 0)
5330Sstevel@tonic-gate 				return (-1);
5340Sstevel@tonic-gate 			siglen = b64_pton(buf2, buf3, sizeof(buf3));
5350Sstevel@tonic-gate 			if (siglen < 0)
5360Sstevel@tonic-gate 				return (-1);
5370Sstevel@tonic-gate 			ShrinkBuffer(siglen);
5380Sstevel@tonic-gate 			memcpy(cp, buf3, siglen);
5390Sstevel@tonic-gate 			cp += siglen;
5400Sstevel@tonic-gate 			break;
5410Sstevel@tonic-gate 		    }
5420Sstevel@tonic-gate 		case ns_t_key:
5430Sstevel@tonic-gate 			/* flags */
5440Sstevel@tonic-gate 			n = gethexnum_str(&startp, endp);
5450Sstevel@tonic-gate 			if (n < 0)
5460Sstevel@tonic-gate 				return (-1);
5470Sstevel@tonic-gate 			ShrinkBuffer(INT16SZ);
5480Sstevel@tonic-gate 			PUTSHORT(n, cp);
5490Sstevel@tonic-gate 			/* proto */
5500Sstevel@tonic-gate 			n = getnum_str(&startp, endp);
5510Sstevel@tonic-gate 			if (n < 0)
5520Sstevel@tonic-gate 				return (-1);
5530Sstevel@tonic-gate 			ShrinkBuffer(1);
5540Sstevel@tonic-gate 			*cp++ = n;
5550Sstevel@tonic-gate 			/* alg */
5560Sstevel@tonic-gate 			n = getnum_str(&startp, endp);
5570Sstevel@tonic-gate 			if (n < 0)
5580Sstevel@tonic-gate 				return (-1);
5590Sstevel@tonic-gate 			ShrinkBuffer(1);
5600Sstevel@tonic-gate 			*cp++ = n;
5610Sstevel@tonic-gate 			/* key */
5620Sstevel@tonic-gate 			if ((n = getword_str(buf2, sizeof buf2,
5630Sstevel@tonic-gate 					     &startp, endp)) < 0)
5640Sstevel@tonic-gate 				return (-1);
5650Sstevel@tonic-gate 			keylen = b64_pton(buf2, buf3, sizeof(buf3));
5660Sstevel@tonic-gate 			if (keylen < 0)
5670Sstevel@tonic-gate 				return (-1);
5680Sstevel@tonic-gate 			ShrinkBuffer(keylen);
5690Sstevel@tonic-gate 			memcpy(cp, buf3, keylen);
5700Sstevel@tonic-gate 			cp += keylen;
5710Sstevel@tonic-gate 			break;
5720Sstevel@tonic-gate 		case ns_t_nxt:
5730Sstevel@tonic-gate 		    {
5740Sstevel@tonic-gate 			int success, nxt_type;
5750Sstevel@tonic-gate 			u_char data[32];
5760Sstevel@tonic-gate 			int maxtype;
5770Sstevel@tonic-gate 
5780Sstevel@tonic-gate 			/* next name */
5790Sstevel@tonic-gate 			if (!getword_str(buf2, sizeof buf2, &startp, endp))
5800Sstevel@tonic-gate 				return (-1);
5810Sstevel@tonic-gate 			n = dn_comp(buf2, cp, buflen, NULL, NULL);
5820Sstevel@tonic-gate 			if (n < 0)
5830Sstevel@tonic-gate 				return (-1);
5840Sstevel@tonic-gate 			cp += n;
5850Sstevel@tonic-gate 			ShrinkBuffer(n);
5860Sstevel@tonic-gate 			maxtype = 0;
5870Sstevel@tonic-gate 			memset(data, 0, sizeof data);
588*11038SRao.Shoaib@Sun.COM 			for (;;) {
5890Sstevel@tonic-gate 				if (!getword_str(buf2, sizeof buf2, &startp,
5900Sstevel@tonic-gate 						 endp))
5910Sstevel@tonic-gate 					break;
5920Sstevel@tonic-gate 				nxt_type = sym_ston(__p_type_syms, buf2,
5930Sstevel@tonic-gate 						    &success);
5940Sstevel@tonic-gate 				if (!success || !ns_t_rr_p(nxt_type))
5950Sstevel@tonic-gate 					return (-1);
5960Sstevel@tonic-gate 				NS_NXT_BIT_SET(nxt_type, data);
5970Sstevel@tonic-gate 				if (nxt_type > maxtype)
5980Sstevel@tonic-gate 					maxtype = nxt_type;
5990Sstevel@tonic-gate 			}
6000Sstevel@tonic-gate 			n = maxtype/NS_NXT_BITS+1;
6010Sstevel@tonic-gate 			ShrinkBuffer(n);
6020Sstevel@tonic-gate 			memcpy(cp, data, n);
6030Sstevel@tonic-gate 			cp += n;
6040Sstevel@tonic-gate 			break;
6050Sstevel@tonic-gate 		    }
6060Sstevel@tonic-gate 		case ns_t_cert:
6070Sstevel@tonic-gate 			/* type */
6080Sstevel@tonic-gate 			n = getnum_str(&startp, endp);
6090Sstevel@tonic-gate 			if (n < 0)
6100Sstevel@tonic-gate 				return (-1);
6110Sstevel@tonic-gate 			ShrinkBuffer(INT16SZ);
6120Sstevel@tonic-gate 			PUTSHORT(n, cp);
6130Sstevel@tonic-gate 			/* key tag */
6140Sstevel@tonic-gate 			n = getnum_str(&startp, endp);
6150Sstevel@tonic-gate 			if (n < 0)
6160Sstevel@tonic-gate 				return (-1);
6170Sstevel@tonic-gate 			ShrinkBuffer(INT16SZ);
6180Sstevel@tonic-gate 			PUTSHORT(n, cp);
6190Sstevel@tonic-gate 			/* alg */
6200Sstevel@tonic-gate 			n = getnum_str(&startp, endp);
6210Sstevel@tonic-gate 			if (n < 0)
6220Sstevel@tonic-gate 				return (-1);
6230Sstevel@tonic-gate 			ShrinkBuffer(1);
6240Sstevel@tonic-gate 			*cp++ = n;
6250Sstevel@tonic-gate 			/* cert */
6260Sstevel@tonic-gate 			if ((n = getword_str(buf2, sizeof buf2,
6270Sstevel@tonic-gate 					     &startp, endp)) < 0)
6280Sstevel@tonic-gate 				return (-1);
6290Sstevel@tonic-gate 			certlen = b64_pton(buf2, buf3, sizeof(buf3));
6300Sstevel@tonic-gate 			if (certlen < 0)
6310Sstevel@tonic-gate 				return (-1);
6320Sstevel@tonic-gate 			ShrinkBuffer(certlen);
6330Sstevel@tonic-gate 			memcpy(cp, buf3, certlen);
6340Sstevel@tonic-gate 			cp += certlen;
6350Sstevel@tonic-gate 			break;
6360Sstevel@tonic-gate 		case ns_t_aaaa:
6370Sstevel@tonic-gate 			if (!getword_str(buf2, sizeof buf2, &startp, endp))
6380Sstevel@tonic-gate 				return (-1);
6390Sstevel@tonic-gate 			if (inet_pton(AF_INET6, buf2, &in6a) <= 0)
6400Sstevel@tonic-gate 				return (-1);
6410Sstevel@tonic-gate 			ShrinkBuffer(NS_IN6ADDRSZ);
6420Sstevel@tonic-gate 			memcpy(cp, &in6a, NS_IN6ADDRSZ);
6430Sstevel@tonic-gate 			cp += NS_IN6ADDRSZ;
6440Sstevel@tonic-gate 			break;
6450Sstevel@tonic-gate 		case ns_t_naptr:
6460Sstevel@tonic-gate 			/* Order Preference Flags Service Replacement Regexp */
6470Sstevel@tonic-gate 			/* Order */
6480Sstevel@tonic-gate 			n = getnum_str(&startp, endp);
6490Sstevel@tonic-gate 			if (n < 0 || n > 65535)
6500Sstevel@tonic-gate 				return (-1);
6510Sstevel@tonic-gate 			ShrinkBuffer(INT16SZ);
6520Sstevel@tonic-gate 			PUTSHORT(n, cp);
6530Sstevel@tonic-gate 			/* Preference */
6540Sstevel@tonic-gate 			n = getnum_str(&startp, endp);
6550Sstevel@tonic-gate 			if (n < 0 || n > 65535)
6560Sstevel@tonic-gate 				return (-1);
6570Sstevel@tonic-gate 			ShrinkBuffer(INT16SZ);
6580Sstevel@tonic-gate 			PUTSHORT(n, cp);
6590Sstevel@tonic-gate 			/* Flags */
6600Sstevel@tonic-gate 			if ((n = getstr_str(buf2, sizeof buf2,
6610Sstevel@tonic-gate 					&startp, endp)) < 0) {
6620Sstevel@tonic-gate 				return (-1);
6630Sstevel@tonic-gate 			}
6640Sstevel@tonic-gate 			if (n > 255)
6650Sstevel@tonic-gate 				return (-1);
6660Sstevel@tonic-gate 			ShrinkBuffer(n+1);
6670Sstevel@tonic-gate 			*cp++ = n;
6680Sstevel@tonic-gate 			memcpy(cp, buf2, n);
6690Sstevel@tonic-gate 			cp += n;
6700Sstevel@tonic-gate 			/* Service Classes */
6710Sstevel@tonic-gate 			if ((n = getstr_str(buf2, sizeof buf2,
6720Sstevel@tonic-gate 					&startp, endp)) < 0) {
6730Sstevel@tonic-gate 				return (-1);
6740Sstevel@tonic-gate 			}
6750Sstevel@tonic-gate 			if (n > 255)
6760Sstevel@tonic-gate 				return (-1);
6770Sstevel@tonic-gate 			ShrinkBuffer(n+1);
6780Sstevel@tonic-gate 			*cp++ = n;
6790Sstevel@tonic-gate 			memcpy(cp, buf2, n);
6800Sstevel@tonic-gate 			cp += n;
6810Sstevel@tonic-gate 			/* Pattern */
6820Sstevel@tonic-gate 			if ((n = getstr_str(buf2, sizeof buf2,
6830Sstevel@tonic-gate 					&startp, endp)) < 0) {
6840Sstevel@tonic-gate 				return (-1);
6850Sstevel@tonic-gate 			}
6860Sstevel@tonic-gate 			if (n > 255)
6870Sstevel@tonic-gate 				return (-1);
6880Sstevel@tonic-gate 			ShrinkBuffer(n+1);
6890Sstevel@tonic-gate 			*cp++ = n;
6900Sstevel@tonic-gate 			memcpy(cp, buf2, n);
6910Sstevel@tonic-gate 			cp += n;
6920Sstevel@tonic-gate 			/* Replacement */
6930Sstevel@tonic-gate 			if (!getword_str(buf2, sizeof buf2, &startp, endp))
6940Sstevel@tonic-gate 				return (-1);
6950Sstevel@tonic-gate 			n = dn_comp(buf2, cp, buflen, NULL, NULL);
6960Sstevel@tonic-gate 			if (n < 0)
6970Sstevel@tonic-gate 				return (-1);
6980Sstevel@tonic-gate 			cp += n;
6990Sstevel@tonic-gate 			ShrinkBuffer(n);
7000Sstevel@tonic-gate 			break;
7010Sstevel@tonic-gate 		default:
7020Sstevel@tonic-gate 			return (-1);
7030Sstevel@tonic-gate 		} /*switch*/
7040Sstevel@tonic-gate 		n = (u_int16_t)((cp - sp2) - INT16SZ);
7050Sstevel@tonic-gate 		PUTSHORT(n, sp2);
7060Sstevel@tonic-gate 	} /*for*/
7070Sstevel@tonic-gate 
7080Sstevel@tonic-gate 	hp->qdcount = htons(counts[0]);
7090Sstevel@tonic-gate 	hp->ancount = htons(counts[1]);
7100Sstevel@tonic-gate 	hp->nscount = htons(counts[2]);
7110Sstevel@tonic-gate 	hp->arcount = htons(counts[3]);
7120Sstevel@tonic-gate 	return (cp - buf);
7130Sstevel@tonic-gate }
7140Sstevel@tonic-gate 
715*11038SRao.Shoaib@Sun.COM /*%
7160Sstevel@tonic-gate  * Get a whitespace delimited word from a string (not file)
7170Sstevel@tonic-gate  * into buf. modify the start pointer to point after the
7180Sstevel@tonic-gate  * word in the string.
7190Sstevel@tonic-gate  */
7200Sstevel@tonic-gate static int
getword_str(char * buf,int size,u_char ** startpp,u_char * endp)7210Sstevel@tonic-gate getword_str(char *buf, int size, u_char **startpp, u_char *endp) {
7220Sstevel@tonic-gate         char *cp;
7230Sstevel@tonic-gate         int c;
7240Sstevel@tonic-gate 
7250Sstevel@tonic-gate         for (cp = buf; *startpp <= endp; ) {
7260Sstevel@tonic-gate                 c = **startpp;
7270Sstevel@tonic-gate                 if (isspace(c) || c == '\0') {
728*11038SRao.Shoaib@Sun.COM                         if (cp != buf) /*%< trailing whitespace */
7290Sstevel@tonic-gate                                 break;
730*11038SRao.Shoaib@Sun.COM                         else { /*%< leading whitespace */
7310Sstevel@tonic-gate                                 (*startpp)++;
7320Sstevel@tonic-gate                                 continue;
7330Sstevel@tonic-gate                         }
7340Sstevel@tonic-gate                 }
7350Sstevel@tonic-gate                 (*startpp)++;
7360Sstevel@tonic-gate                 if (cp >= buf+size-1)
7370Sstevel@tonic-gate                         break;
7380Sstevel@tonic-gate                 *cp++ = (u_char)c;
7390Sstevel@tonic-gate         }
7400Sstevel@tonic-gate         *cp = '\0';
7410Sstevel@tonic-gate         return (cp != buf);
7420Sstevel@tonic-gate }
7430Sstevel@tonic-gate 
744*11038SRao.Shoaib@Sun.COM /*%
7450Sstevel@tonic-gate  * get a white spae delimited string from memory.  Process quoted strings
746*11038SRao.Shoaib@Sun.COM  * and \\DDD escapes.  Return length or -1 on error.  Returned string may
7470Sstevel@tonic-gate  * contain nulls.
7480Sstevel@tonic-gate  */
7490Sstevel@tonic-gate static char digits[] = "0123456789";
7500Sstevel@tonic-gate static int
getstr_str(char * buf,int size,u_char ** startpp,u_char * endp)7510Sstevel@tonic-gate getstr_str(char *buf, int size, u_char **startpp, u_char *endp) {
7520Sstevel@tonic-gate         char *cp;
7530Sstevel@tonic-gate         int c, c1 = 0;
7540Sstevel@tonic-gate 	int inquote = 0;
7550Sstevel@tonic-gate 	int seen_quote = 0;
7560Sstevel@tonic-gate 	int escape = 0;
7570Sstevel@tonic-gate 	int dig = 0;
7580Sstevel@tonic-gate 
7590Sstevel@tonic-gate 	for (cp = buf; *startpp <= endp; ) {
7600Sstevel@tonic-gate                 if ((c = **startpp) == '\0')
7610Sstevel@tonic-gate 			break;
7620Sstevel@tonic-gate 		/* leading white space */
7630Sstevel@tonic-gate 		if ((cp == buf) && !seen_quote && isspace(c)) {
7640Sstevel@tonic-gate 			(*startpp)++;
7650Sstevel@tonic-gate 			continue;
7660Sstevel@tonic-gate 		}
7670Sstevel@tonic-gate 
7680Sstevel@tonic-gate 		switch (c) {
7690Sstevel@tonic-gate 		case '\\':
7700Sstevel@tonic-gate 			if (!escape)  {
7710Sstevel@tonic-gate 				escape = 1;
7720Sstevel@tonic-gate 				dig = 0;
7730Sstevel@tonic-gate 				c1 = 0;
7740Sstevel@tonic-gate 				(*startpp)++;
7750Sstevel@tonic-gate 				continue;
7760Sstevel@tonic-gate 			}
7770Sstevel@tonic-gate 			goto do_escape;
7780Sstevel@tonic-gate 		case '"':
7790Sstevel@tonic-gate 			if (!escape) {
7800Sstevel@tonic-gate 				inquote = !inquote;
7810Sstevel@tonic-gate 				seen_quote = 1;
7820Sstevel@tonic-gate 				(*startpp)++;
7830Sstevel@tonic-gate 				continue;
7840Sstevel@tonic-gate 			}
7850Sstevel@tonic-gate 			/* fall through */
7860Sstevel@tonic-gate 		default:
7870Sstevel@tonic-gate 		do_escape:
7880Sstevel@tonic-gate 			if (escape) {
7890Sstevel@tonic-gate 				switch (c) {
7900Sstevel@tonic-gate 				case '0':
7910Sstevel@tonic-gate 				case '1':
7920Sstevel@tonic-gate 				case '2':
7930Sstevel@tonic-gate 				case '3':
7940Sstevel@tonic-gate 				case '4':
7950Sstevel@tonic-gate 				case '5':
7960Sstevel@tonic-gate 				case '6':
7970Sstevel@tonic-gate 				case '7':
7980Sstevel@tonic-gate 				case '8':
7990Sstevel@tonic-gate 				case '9':
8000Sstevel@tonic-gate 					c1 = c1 * 10 +
8010Sstevel@tonic-gate 						(strchr(digits, c) - digits);
8020Sstevel@tonic-gate 
8030Sstevel@tonic-gate 					if (++dig == 3) {
8040Sstevel@tonic-gate 						c = c1 &0xff;
8050Sstevel@tonic-gate 						break;
8060Sstevel@tonic-gate 					}
8070Sstevel@tonic-gate 					(*startpp)++;
8080Sstevel@tonic-gate 					continue;
8090Sstevel@tonic-gate 				}
8100Sstevel@tonic-gate 				escape = 0;
8110Sstevel@tonic-gate 			} else if (!inquote && isspace(c))
8120Sstevel@tonic-gate 				goto done;
8130Sstevel@tonic-gate 			if (cp >= buf+size-1)
8140Sstevel@tonic-gate 				goto done;
8150Sstevel@tonic-gate 			*cp++ = (u_char)c;
8160Sstevel@tonic-gate 			(*startpp)++;
8170Sstevel@tonic-gate 		}
8180Sstevel@tonic-gate 	}
8190Sstevel@tonic-gate  done:
8200Sstevel@tonic-gate 	*cp = '\0';
8210Sstevel@tonic-gate 	return ((cp == buf)?  (seen_quote? 0: -1): (cp - buf));
8220Sstevel@tonic-gate }
823*11038SRao.Shoaib@Sun.COM 
824*11038SRao.Shoaib@Sun.COM /*%
8250Sstevel@tonic-gate  * Get a whitespace delimited base 16 number from a string (not file) into buf
8260Sstevel@tonic-gate  * update the start pointer to point after the number in the string.
8270Sstevel@tonic-gate  */
8280Sstevel@tonic-gate static int
gethexnum_str(u_char ** startpp,u_char * endp)8290Sstevel@tonic-gate gethexnum_str(u_char **startpp, u_char *endp) {
8300Sstevel@tonic-gate         int c, n;
8310Sstevel@tonic-gate         int seendigit = 0;
8320Sstevel@tonic-gate         int m = 0;
8330Sstevel@tonic-gate 
8340Sstevel@tonic-gate 	if (*startpp + 2 >= endp || strncasecmp((char *)*startpp, "0x", 2) != 0)
8350Sstevel@tonic-gate 		return getnum_str(startpp, endp);
8360Sstevel@tonic-gate 	(*startpp)+=2;
8370Sstevel@tonic-gate         for (n = 0; *startpp <= endp; ) {
8380Sstevel@tonic-gate                 c = **startpp;
8390Sstevel@tonic-gate                 if (isspace(c) || c == '\0') {
840*11038SRao.Shoaib@Sun.COM                         if (seendigit) /*%< trailing whitespace */
8410Sstevel@tonic-gate                                 break;
842*11038SRao.Shoaib@Sun.COM                         else { /*%< leading whitespace */
8430Sstevel@tonic-gate                                 (*startpp)++;
8440Sstevel@tonic-gate                                 continue;
8450Sstevel@tonic-gate                         }
8460Sstevel@tonic-gate                 }
8470Sstevel@tonic-gate                 if (c == ';') {
8480Sstevel@tonic-gate                         while ((*startpp <= endp) &&
8490Sstevel@tonic-gate 			       ((c = **startpp) != '\n'))
8500Sstevel@tonic-gate 					(*startpp)++;
8510Sstevel@tonic-gate                         if (seendigit)
8520Sstevel@tonic-gate                                 break;
8530Sstevel@tonic-gate                         continue;
8540Sstevel@tonic-gate                 }
8550Sstevel@tonic-gate                 if (!isxdigit(c)) {
8560Sstevel@tonic-gate                         if (c == ')' && seendigit) {
8570Sstevel@tonic-gate                                 (*startpp)--;
8580Sstevel@tonic-gate                                 break;
8590Sstevel@tonic-gate                         }
8600Sstevel@tonic-gate 			return (-1);
8610Sstevel@tonic-gate                 }
8620Sstevel@tonic-gate                 (*startpp)++;
8630Sstevel@tonic-gate 		if (isdigit(c))
8640Sstevel@tonic-gate 	                n = n * 16 + (c - '0');
8650Sstevel@tonic-gate 		else
8660Sstevel@tonic-gate 			n = n * 16 + (tolower(c) - 'a' + 10);
8670Sstevel@tonic-gate                 seendigit = 1;
8680Sstevel@tonic-gate         }
8690Sstevel@tonic-gate         return (n + m);
8700Sstevel@tonic-gate }
8710Sstevel@tonic-gate 
872*11038SRao.Shoaib@Sun.COM /*%
8730Sstevel@tonic-gate  * Get a whitespace delimited base 10 number from a string (not file) into buf
8740Sstevel@tonic-gate  * update the start pointer to point after the number in the string.
8750Sstevel@tonic-gate  */
8760Sstevel@tonic-gate static int
getnum_str(u_char ** startpp,u_char * endp)8770Sstevel@tonic-gate getnum_str(u_char **startpp, u_char *endp) {
8780Sstevel@tonic-gate         int c, n;
8790Sstevel@tonic-gate         int seendigit = 0;
8800Sstevel@tonic-gate         int m = 0;
8810Sstevel@tonic-gate 
8820Sstevel@tonic-gate         for (n = 0; *startpp <= endp; ) {
8830Sstevel@tonic-gate                 c = **startpp;
8840Sstevel@tonic-gate                 if (isspace(c) || c == '\0') {
885*11038SRao.Shoaib@Sun.COM                         if (seendigit) /*%< trailing whitespace */
8860Sstevel@tonic-gate                                 break;
887*11038SRao.Shoaib@Sun.COM                         else { /*%< leading whitespace */
8880Sstevel@tonic-gate                                 (*startpp)++;
8890Sstevel@tonic-gate                                 continue;
8900Sstevel@tonic-gate                         }
8910Sstevel@tonic-gate                 }
8920Sstevel@tonic-gate                 if (c == ';') {
8930Sstevel@tonic-gate                         while ((*startpp <= endp) &&
8940Sstevel@tonic-gate 			       ((c = **startpp) != '\n'))
8950Sstevel@tonic-gate 					(*startpp)++;
8960Sstevel@tonic-gate                         if (seendigit)
8970Sstevel@tonic-gate                                 break;
8980Sstevel@tonic-gate                         continue;
8990Sstevel@tonic-gate                 }
9000Sstevel@tonic-gate                 if (!isdigit(c)) {
9010Sstevel@tonic-gate                         if (c == ')' && seendigit) {
9020Sstevel@tonic-gate                                 (*startpp)--;
9030Sstevel@tonic-gate                                 break;
9040Sstevel@tonic-gate                         }
9050Sstevel@tonic-gate 			return (-1);
9060Sstevel@tonic-gate                 }
9070Sstevel@tonic-gate                 (*startpp)++;
9080Sstevel@tonic-gate                 n = n * 10 + (c - '0');
9090Sstevel@tonic-gate                 seendigit = 1;
9100Sstevel@tonic-gate         }
9110Sstevel@tonic-gate         return (n + m);
9120Sstevel@tonic-gate }
9130Sstevel@tonic-gate 
914*11038SRao.Shoaib@Sun.COM /*%
9150Sstevel@tonic-gate  * Allocate a resource record buffer & save rr info.
9160Sstevel@tonic-gate  */
9170Sstevel@tonic-gate ns_updrec *
res_mkupdrec(int section,const char * dname,u_int class,u_int type,u_long ttl)9180Sstevel@tonic-gate res_mkupdrec(int section, const char *dname,
9190Sstevel@tonic-gate 	     u_int class, u_int type, u_long ttl) {
9200Sstevel@tonic-gate 	ns_updrec *rrecp = (ns_updrec *)calloc(1, sizeof(ns_updrec));
9210Sstevel@tonic-gate 
9220Sstevel@tonic-gate 	if (!rrecp || !(rrecp->r_dname = strdup(dname))) {
9230Sstevel@tonic-gate 		if (rrecp)
9240Sstevel@tonic-gate 			free((char *)rrecp);
9250Sstevel@tonic-gate 		return (NULL);
9260Sstevel@tonic-gate 	}
9270Sstevel@tonic-gate 	INIT_LINK(rrecp, r_link);
9280Sstevel@tonic-gate 	INIT_LINK(rrecp, r_glink);
929*11038SRao.Shoaib@Sun.COM  	rrecp->r_class = (ns_class)class;
930*11038SRao.Shoaib@Sun.COM 	rrecp->r_type = (ns_type)type;
9310Sstevel@tonic-gate 	rrecp->r_ttl = ttl;
932*11038SRao.Shoaib@Sun.COM 	rrecp->r_section = (ns_sect)section;
9330Sstevel@tonic-gate 	return (rrecp);
9340Sstevel@tonic-gate }
9350Sstevel@tonic-gate 
936*11038SRao.Shoaib@Sun.COM /*%
9370Sstevel@tonic-gate  * Free a resource record buffer created by res_mkupdrec.
9380Sstevel@tonic-gate  */
9390Sstevel@tonic-gate void
res_freeupdrec(ns_updrec * rrecp)9400Sstevel@tonic-gate res_freeupdrec(ns_updrec *rrecp) {
9410Sstevel@tonic-gate 	/* Note: freeing r_dp is the caller's responsibility. */
9420Sstevel@tonic-gate 	if (rrecp->r_dname != NULL)
9430Sstevel@tonic-gate 		free(rrecp->r_dname);
9440Sstevel@tonic-gate 	free(rrecp);
9450Sstevel@tonic-gate }
9460Sstevel@tonic-gate 
9470Sstevel@tonic-gate struct valuelist {
9480Sstevel@tonic-gate 	struct valuelist *	next;
9490Sstevel@tonic-gate 	struct valuelist *	prev;
9500Sstevel@tonic-gate 	char *			name;
9510Sstevel@tonic-gate 	char *			proto;
9520Sstevel@tonic-gate 	int			port;
9530Sstevel@tonic-gate };
9540Sstevel@tonic-gate static struct valuelist *servicelist, *protolist;
9550Sstevel@tonic-gate 
9560Sstevel@tonic-gate static void
res_buildservicelist()9570Sstevel@tonic-gate res_buildservicelist() {
9580Sstevel@tonic-gate 	struct servent *sp;
9590Sstevel@tonic-gate 	struct valuelist *slp;
9600Sstevel@tonic-gate 
9610Sstevel@tonic-gate #ifdef MAYBE_HESIOD
9620Sstevel@tonic-gate 	setservent(0);
9630Sstevel@tonic-gate #else
9640Sstevel@tonic-gate 	setservent(1);
9650Sstevel@tonic-gate #endif
9660Sstevel@tonic-gate 	while ((sp = getservent()) != NULL) {
9670Sstevel@tonic-gate 		slp = (struct valuelist *)malloc(sizeof(struct valuelist));
9680Sstevel@tonic-gate 		if (!slp)
9690Sstevel@tonic-gate 			break;
9700Sstevel@tonic-gate 		slp->name = strdup(sp->s_name);
9710Sstevel@tonic-gate 		slp->proto = strdup(sp->s_proto);
9720Sstevel@tonic-gate 		if ((slp->name == NULL) || (slp->proto == NULL)) {
9730Sstevel@tonic-gate 			if (slp->name) free(slp->name);
9740Sstevel@tonic-gate 			if (slp->proto) free(slp->proto);
9750Sstevel@tonic-gate 			free(slp);
9760Sstevel@tonic-gate 			break;
9770Sstevel@tonic-gate 		}
978*11038SRao.Shoaib@Sun.COM 		slp->port = ntohs((u_int16_t)sp->s_port);  /*%< host byt order */
9790Sstevel@tonic-gate 		slp->next = servicelist;
9800Sstevel@tonic-gate 		slp->prev = NULL;
9810Sstevel@tonic-gate 		if (servicelist)
9820Sstevel@tonic-gate 			servicelist->prev = slp;
9830Sstevel@tonic-gate 		servicelist = slp;
9840Sstevel@tonic-gate 	}
9850Sstevel@tonic-gate 	endservent();
9860Sstevel@tonic-gate }
9870Sstevel@tonic-gate 
9880Sstevel@tonic-gate void
res_destroyservicelist()9890Sstevel@tonic-gate res_destroyservicelist() {
9900Sstevel@tonic-gate 	struct valuelist *slp, *slp_next;
9910Sstevel@tonic-gate 
9920Sstevel@tonic-gate 	for (slp = servicelist; slp != NULL; slp = slp_next) {
9930Sstevel@tonic-gate 		slp_next = slp->next;
9940Sstevel@tonic-gate 		free(slp->name);
9950Sstevel@tonic-gate 		free(slp->proto);
9960Sstevel@tonic-gate 		free(slp);
9970Sstevel@tonic-gate 	}
9980Sstevel@tonic-gate 	servicelist = (struct valuelist *)0;
9990Sstevel@tonic-gate }
10000Sstevel@tonic-gate 
10010Sstevel@tonic-gate void
res_buildprotolist(void)10020Sstevel@tonic-gate res_buildprotolist(void) {
10030Sstevel@tonic-gate 	struct protoent *pp;
10040Sstevel@tonic-gate 	struct valuelist *slp;
10050Sstevel@tonic-gate 
10060Sstevel@tonic-gate #ifdef MAYBE_HESIOD
10070Sstevel@tonic-gate 	setprotoent(0);
10080Sstevel@tonic-gate #else
10090Sstevel@tonic-gate 	setprotoent(1);
10100Sstevel@tonic-gate #endif
10110Sstevel@tonic-gate 	while ((pp = getprotoent()) != NULL) {
10120Sstevel@tonic-gate 		slp = (struct valuelist *)malloc(sizeof(struct valuelist));
10130Sstevel@tonic-gate 		if (!slp)
10140Sstevel@tonic-gate 			break;
10150Sstevel@tonic-gate 		slp->name = strdup(pp->p_name);
10160Sstevel@tonic-gate 		if (slp->name == NULL) {
10170Sstevel@tonic-gate 			free(slp);
10180Sstevel@tonic-gate 			break;
10190Sstevel@tonic-gate 		}
1020*11038SRao.Shoaib@Sun.COM 		slp->port = pp->p_proto;	/*%< host byte order */
10210Sstevel@tonic-gate 		slp->next = protolist;
10220Sstevel@tonic-gate 		slp->prev = NULL;
10230Sstevel@tonic-gate 		if (protolist)
10240Sstevel@tonic-gate 			protolist->prev = slp;
10250Sstevel@tonic-gate 		protolist = slp;
10260Sstevel@tonic-gate 	}
10270Sstevel@tonic-gate 	endprotoent();
10280Sstevel@tonic-gate }
10290Sstevel@tonic-gate 
10300Sstevel@tonic-gate void
res_destroyprotolist(void)10310Sstevel@tonic-gate res_destroyprotolist(void) {
10320Sstevel@tonic-gate 	struct valuelist *plp, *plp_next;
10330Sstevel@tonic-gate 
10340Sstevel@tonic-gate 	for (plp = protolist; plp != NULL; plp = plp_next) {
10350Sstevel@tonic-gate 		plp_next = plp->next;
10360Sstevel@tonic-gate 		free(plp->name);
10370Sstevel@tonic-gate 		free(plp);
10380Sstevel@tonic-gate 	}
10390Sstevel@tonic-gate 	protolist = (struct valuelist *)0;
10400Sstevel@tonic-gate }
10410Sstevel@tonic-gate 
10420Sstevel@tonic-gate static int
findservice(const char * s,struct valuelist ** list)10430Sstevel@tonic-gate findservice(const char *s, struct valuelist **list) {
10440Sstevel@tonic-gate 	struct valuelist *lp = *list;
10450Sstevel@tonic-gate 	int n;
10460Sstevel@tonic-gate 
10470Sstevel@tonic-gate 	for (; lp != NULL; lp = lp->next)
10480Sstevel@tonic-gate 		if (strcasecmp(lp->name, s) == 0) {
10490Sstevel@tonic-gate 			if (lp != *list) {
10500Sstevel@tonic-gate 				lp->prev->next = lp->next;
10510Sstevel@tonic-gate 				if (lp->next)
10520Sstevel@tonic-gate 					lp->next->prev = lp->prev;
10530Sstevel@tonic-gate 				(*list)->prev = lp;
10540Sstevel@tonic-gate 				lp->next = *list;
10550Sstevel@tonic-gate 				*list = lp;
10560Sstevel@tonic-gate 			}
1057*11038SRao.Shoaib@Sun.COM 			return (lp->port);	/*%< host byte order */
10580Sstevel@tonic-gate 		}
10590Sstevel@tonic-gate 	if (sscanf(s, "%d", &n) != 1 || n <= 0)
10600Sstevel@tonic-gate 		n = -1;
10610Sstevel@tonic-gate 	return (n);
10620Sstevel@tonic-gate }
10630Sstevel@tonic-gate 
1064*11038SRao.Shoaib@Sun.COM /*%
10650Sstevel@tonic-gate  * Convert service name or (ascii) number to int.
10660Sstevel@tonic-gate  */
10670Sstevel@tonic-gate int
res_servicenumber(const char * p)10680Sstevel@tonic-gate res_servicenumber(const char *p) {
10690Sstevel@tonic-gate 	if (servicelist == (struct valuelist *)0)
10700Sstevel@tonic-gate 		res_buildservicelist();
10710Sstevel@tonic-gate 	return (findservice(p, &servicelist));
10720Sstevel@tonic-gate }
10730Sstevel@tonic-gate 
1074*11038SRao.Shoaib@Sun.COM /*%
10750Sstevel@tonic-gate  * Convert protocol name or (ascii) number to int.
10760Sstevel@tonic-gate  */
10770Sstevel@tonic-gate int
res_protocolnumber(const char * p)10780Sstevel@tonic-gate res_protocolnumber(const char *p) {
10790Sstevel@tonic-gate 	if (protolist == (struct valuelist *)0)
10800Sstevel@tonic-gate 		res_buildprotolist();
10810Sstevel@tonic-gate 	return (findservice(p, &protolist));
10820Sstevel@tonic-gate }
10830Sstevel@tonic-gate 
10840Sstevel@tonic-gate static struct servent *
cgetservbyport(u_int16_t port,const char * proto)1085*11038SRao.Shoaib@Sun.COM cgetservbyport(u_int16_t port, const char *proto) {	/*%< Host byte order. */
10860Sstevel@tonic-gate 	struct valuelist **list = &servicelist;
10870Sstevel@tonic-gate 	struct valuelist *lp = *list;
10880Sstevel@tonic-gate 	static struct servent serv;
10890Sstevel@tonic-gate 
10900Sstevel@tonic-gate 	port = ntohs(port);
10910Sstevel@tonic-gate 	for (; lp != NULL; lp = lp->next) {
1092*11038SRao.Shoaib@Sun.COM 		if (port != (u_int16_t)lp->port)	/*%< Host byte order. */
10930Sstevel@tonic-gate 			continue;
10940Sstevel@tonic-gate 		if (strcasecmp(lp->proto, proto) == 0) {
10950Sstevel@tonic-gate 			if (lp != *list) {
10960Sstevel@tonic-gate 				lp->prev->next = lp->next;
10970Sstevel@tonic-gate 				if (lp->next)
10980Sstevel@tonic-gate 					lp->next->prev = lp->prev;
10990Sstevel@tonic-gate 				(*list)->prev = lp;
11000Sstevel@tonic-gate 				lp->next = *list;
11010Sstevel@tonic-gate 				*list = lp;
11020Sstevel@tonic-gate 			}
11030Sstevel@tonic-gate 			serv.s_name = lp->name;
11040Sstevel@tonic-gate 			serv.s_port = htons((u_int16_t)lp->port);
11050Sstevel@tonic-gate 			serv.s_proto = lp->proto;
11060Sstevel@tonic-gate 			return (&serv);
11070Sstevel@tonic-gate 		}
11080Sstevel@tonic-gate 	}
11090Sstevel@tonic-gate 	return (0);
11100Sstevel@tonic-gate }
11110Sstevel@tonic-gate 
11120Sstevel@tonic-gate static struct protoent *
cgetprotobynumber(int proto)1113*11038SRao.Shoaib@Sun.COM cgetprotobynumber(int proto) {				/*%< Host byte order. */
11140Sstevel@tonic-gate 	struct valuelist **list = &protolist;
11150Sstevel@tonic-gate 	struct valuelist *lp = *list;
11160Sstevel@tonic-gate 	static struct protoent prot;
11170Sstevel@tonic-gate 
11180Sstevel@tonic-gate 	for (; lp != NULL; lp = lp->next)
1119*11038SRao.Shoaib@Sun.COM 		if (lp->port == proto) {		/*%< Host byte order. */
11200Sstevel@tonic-gate 			if (lp != *list) {
11210Sstevel@tonic-gate 				lp->prev->next = lp->next;
11220Sstevel@tonic-gate 				if (lp->next)
11230Sstevel@tonic-gate 					lp->next->prev = lp->prev;
11240Sstevel@tonic-gate 				(*list)->prev = lp;
11250Sstevel@tonic-gate 				lp->next = *list;
11260Sstevel@tonic-gate 				*list = lp;
11270Sstevel@tonic-gate 			}
11280Sstevel@tonic-gate 			prot.p_name = lp->name;
1129*11038SRao.Shoaib@Sun.COM 			prot.p_proto = lp->port;	/*%< Host byte order. */
11300Sstevel@tonic-gate 			return (&prot);
11310Sstevel@tonic-gate 		}
11320Sstevel@tonic-gate 	return (0);
11330Sstevel@tonic-gate }
11340Sstevel@tonic-gate 
11350Sstevel@tonic-gate const char *
res_protocolname(int num)11360Sstevel@tonic-gate res_protocolname(int num) {
11370Sstevel@tonic-gate 	static char number[8];
11380Sstevel@tonic-gate 	struct protoent *pp;
11390Sstevel@tonic-gate 
11400Sstevel@tonic-gate 	if (protolist == (struct valuelist *)0)
11410Sstevel@tonic-gate 		res_buildprotolist();
11420Sstevel@tonic-gate 	pp = cgetprotobynumber(num);
11430Sstevel@tonic-gate 	if (pp == 0)  {
11440Sstevel@tonic-gate 		(void) sprintf(number, "%d", num);
11450Sstevel@tonic-gate 		return (number);
11460Sstevel@tonic-gate 	}
11470Sstevel@tonic-gate 	return (pp->p_name);
11480Sstevel@tonic-gate }
11490Sstevel@tonic-gate 
11500Sstevel@tonic-gate const char *
res_servicename(u_int16_t port,const char * proto)1151*11038SRao.Shoaib@Sun.COM res_servicename(u_int16_t port, const char *proto) {	/*%< Host byte order. */
11520Sstevel@tonic-gate 	static char number[8];
11530Sstevel@tonic-gate 	struct servent *ss;
11540Sstevel@tonic-gate 
11550Sstevel@tonic-gate 	if (servicelist == (struct valuelist *)0)
11560Sstevel@tonic-gate 		res_buildservicelist();
11570Sstevel@tonic-gate 	ss = cgetservbyport(htons(port), proto);
11580Sstevel@tonic-gate 	if (ss == 0)  {
11590Sstevel@tonic-gate 		(void) sprintf(number, "%d", port);
11600Sstevel@tonic-gate 		return (number);
11610Sstevel@tonic-gate 	}
11620Sstevel@tonic-gate 	return (ss->s_name);
11630Sstevel@tonic-gate }
1164