xref: /onnv-gate/usr/src/lib/libresolv2/common/irs/lcl_pr.c (revision 11038:74b12212b8a2)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * Copyright (c) 1989, 1993, 1995
30Sstevel@tonic-gate  *	The Regents of the University of California.  All rights reserved.
40Sstevel@tonic-gate  *
50Sstevel@tonic-gate  * Redistribution and use in source and binary forms, with or without
60Sstevel@tonic-gate  * modification, are permitted provided that the following conditions
70Sstevel@tonic-gate  * are met:
80Sstevel@tonic-gate  * 1. Redistributions of source code must retain the above copyright
90Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer.
100Sstevel@tonic-gate  * 2. Redistributions in binary form must reproduce the above copyright
110Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer in the
120Sstevel@tonic-gate  *    documentation and/or other materials provided with the distribution.
130Sstevel@tonic-gate  * 3. All advertising materials mentioning features or use of this software
140Sstevel@tonic-gate  *    must display the following acknowledgement:
150Sstevel@tonic-gate  *	This product includes software developed by the University of
160Sstevel@tonic-gate  *	California, Berkeley and its contributors.
170Sstevel@tonic-gate  * 4. Neither the name of the University nor the names of its contributors
180Sstevel@tonic-gate  *    may be used to endorse or promote products derived from this software
190Sstevel@tonic-gate  *    without specific prior written permission.
200Sstevel@tonic-gate  *
210Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
220Sstevel@tonic-gate  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
230Sstevel@tonic-gate  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
240Sstevel@tonic-gate  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
250Sstevel@tonic-gate  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
260Sstevel@tonic-gate  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
270Sstevel@tonic-gate  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
280Sstevel@tonic-gate  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
290Sstevel@tonic-gate  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
300Sstevel@tonic-gate  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
310Sstevel@tonic-gate  * SUCH DAMAGE.
320Sstevel@tonic-gate  */
330Sstevel@tonic-gate 
340Sstevel@tonic-gate /*
35*11038SRao.Shoaib@Sun.COM  * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
360Sstevel@tonic-gate  * Portions Copyright (c) 1996,1999 by Internet Software Consortium.
370Sstevel@tonic-gate  *
380Sstevel@tonic-gate  * Permission to use, copy, modify, and distribute this software for any
390Sstevel@tonic-gate  * purpose with or without fee is hereby granted, provided that the above
400Sstevel@tonic-gate  * copyright notice and this permission notice appear in all copies.
410Sstevel@tonic-gate  *
42*11038SRao.Shoaib@Sun.COM  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
43*11038SRao.Shoaib@Sun.COM  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
44*11038SRao.Shoaib@Sun.COM  * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
45*11038SRao.Shoaib@Sun.COM  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
46*11038SRao.Shoaib@Sun.COM  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
47*11038SRao.Shoaib@Sun.COM  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
48*11038SRao.Shoaib@Sun.COM  * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
490Sstevel@tonic-gate  */
500Sstevel@tonic-gate 
510Sstevel@tonic-gate #if defined(LIBC_SCCS) && !defined(lint)
52*11038SRao.Shoaib@Sun.COM static const char rcsid[] = "$Id: lcl_pr.c,v 1.4 2006/03/09 23:57:56 marka Exp $";
530Sstevel@tonic-gate #endif /* LIBC_SCCS and not lint */
540Sstevel@tonic-gate 
550Sstevel@tonic-gate /* extern */
560Sstevel@tonic-gate 
570Sstevel@tonic-gate #include "port_before.h"
580Sstevel@tonic-gate 
590Sstevel@tonic-gate #include <sys/types.h>
600Sstevel@tonic-gate #include <netinet/in.h>
610Sstevel@tonic-gate #include <arpa/nameser.h>
620Sstevel@tonic-gate #include <resolv.h>
630Sstevel@tonic-gate 
640Sstevel@tonic-gate #include <errno.h>
650Sstevel@tonic-gate #include <fcntl.h>
660Sstevel@tonic-gate #include <string.h>
670Sstevel@tonic-gate #include <stdio.h>
680Sstevel@tonic-gate #include <stdlib.h>
690Sstevel@tonic-gate 
700Sstevel@tonic-gate #include <irs.h>
710Sstevel@tonic-gate #include <isc/memcluster.h>
720Sstevel@tonic-gate 
730Sstevel@tonic-gate #include "port_after.h"
740Sstevel@tonic-gate 
750Sstevel@tonic-gate #include "irs_p.h"
760Sstevel@tonic-gate #include "lcl_p.h"
770Sstevel@tonic-gate 
780Sstevel@tonic-gate #ifndef _PATH_PROTOCOLS
790Sstevel@tonic-gate #define _PATH_PROTOCOLS "/etc/protocols"
800Sstevel@tonic-gate #endif
810Sstevel@tonic-gate #define MAXALIASES      35
820Sstevel@tonic-gate 
830Sstevel@tonic-gate /* Types */
840Sstevel@tonic-gate 
850Sstevel@tonic-gate struct pvt {
860Sstevel@tonic-gate 	FILE *		fp;
870Sstevel@tonic-gate 	char		line[BUFSIZ+1];
88*11038SRao.Shoaib@Sun.COM 	char *		dbuf;
890Sstevel@tonic-gate 	struct protoent	proto;
900Sstevel@tonic-gate 	char *		proto_aliases[MAXALIASES];
910Sstevel@tonic-gate };
920Sstevel@tonic-gate 
930Sstevel@tonic-gate /* Forward */
940Sstevel@tonic-gate 
950Sstevel@tonic-gate static void			pr_close(struct irs_pr *);
960Sstevel@tonic-gate static struct protoent *	pr_next(struct irs_pr *);
970Sstevel@tonic-gate static struct protoent *	pr_byname(struct irs_pr *, const char *);
980Sstevel@tonic-gate static struct protoent *	pr_bynumber(struct irs_pr *, int);
990Sstevel@tonic-gate static void			pr_rewind(struct irs_pr *);
1000Sstevel@tonic-gate static void			pr_minimize(struct irs_pr *);
1010Sstevel@tonic-gate 
1020Sstevel@tonic-gate /* Portability. */
1030Sstevel@tonic-gate 
1040Sstevel@tonic-gate #ifndef SEEK_SET
1050Sstevel@tonic-gate # define SEEK_SET 0
1060Sstevel@tonic-gate #endif
1070Sstevel@tonic-gate 
1080Sstevel@tonic-gate /* Public */
1090Sstevel@tonic-gate 
1100Sstevel@tonic-gate struct irs_pr *
irs_lcl_pr(struct irs_acc * this)1110Sstevel@tonic-gate irs_lcl_pr(struct irs_acc *this) {
1120Sstevel@tonic-gate 	struct irs_pr *pr;
1130Sstevel@tonic-gate 	struct pvt *pvt;
1140Sstevel@tonic-gate 
1150Sstevel@tonic-gate 	if (!(pr = memget(sizeof *pr))) {
1160Sstevel@tonic-gate 		errno = ENOMEM;
1170Sstevel@tonic-gate 		return (NULL);
1180Sstevel@tonic-gate 	}
1190Sstevel@tonic-gate 	if (!(pvt = memget(sizeof *pvt))) {
1200Sstevel@tonic-gate 		memput(pr, sizeof *this);
1210Sstevel@tonic-gate 		errno = ENOMEM;
1220Sstevel@tonic-gate 		return (NULL);
1230Sstevel@tonic-gate 	}
1240Sstevel@tonic-gate 	memset(pvt, 0, sizeof *pvt);
1250Sstevel@tonic-gate 	pr->private = pvt;
1260Sstevel@tonic-gate 	pr->close = pr_close;
1270Sstevel@tonic-gate 	pr->byname = pr_byname;
1280Sstevel@tonic-gate 	pr->bynumber = pr_bynumber;
1290Sstevel@tonic-gate 	pr->next = pr_next;
1300Sstevel@tonic-gate 	pr->rewind = pr_rewind;
1310Sstevel@tonic-gate 	pr->minimize = pr_minimize;
1320Sstevel@tonic-gate 	pr->res_get = NULL;
1330Sstevel@tonic-gate 	pr->res_set = NULL;
1340Sstevel@tonic-gate 	return (pr);
1350Sstevel@tonic-gate }
1360Sstevel@tonic-gate 
1370Sstevel@tonic-gate /* Methods */
1380Sstevel@tonic-gate 
1390Sstevel@tonic-gate static void
pr_close(struct irs_pr * this)1400Sstevel@tonic-gate pr_close(struct irs_pr *this) {
1410Sstevel@tonic-gate 	struct pvt *pvt = (struct pvt *)this->private;
1420Sstevel@tonic-gate 
1430Sstevel@tonic-gate 	if (pvt->fp)
1440Sstevel@tonic-gate 		(void) fclose(pvt->fp);
145*11038SRao.Shoaib@Sun.COM 	if (pvt->dbuf)
146*11038SRao.Shoaib@Sun.COM 		free(pvt->dbuf);
1470Sstevel@tonic-gate 	memput(pvt, sizeof *pvt);
1480Sstevel@tonic-gate 	memput(this, sizeof *this);
1490Sstevel@tonic-gate }
1500Sstevel@tonic-gate 
1510Sstevel@tonic-gate static struct protoent *
pr_byname(struct irs_pr * this,const char * name)1520Sstevel@tonic-gate pr_byname(struct irs_pr *this, const char *name) {
1530Sstevel@tonic-gate 
1540Sstevel@tonic-gate 	struct protoent *p;
1550Sstevel@tonic-gate 	char **cp;
1560Sstevel@tonic-gate 
1570Sstevel@tonic-gate 	pr_rewind(this);
1580Sstevel@tonic-gate 	while ((p = pr_next(this))) {
1590Sstevel@tonic-gate 		if (!strcmp(p->p_name, name))
1600Sstevel@tonic-gate 			goto found;
1610Sstevel@tonic-gate 		for (cp = p->p_aliases; *cp; cp++)
1620Sstevel@tonic-gate 			if (!strcmp(*cp, name))
1630Sstevel@tonic-gate 				goto found;
1640Sstevel@tonic-gate 	}
1650Sstevel@tonic-gate  found:
1660Sstevel@tonic-gate 	return (p);
1670Sstevel@tonic-gate }
1680Sstevel@tonic-gate 
1690Sstevel@tonic-gate static struct protoent *
pr_bynumber(struct irs_pr * this,int proto)1700Sstevel@tonic-gate pr_bynumber(struct irs_pr *this, int proto) {
1710Sstevel@tonic-gate 	struct protoent *p;
1720Sstevel@tonic-gate 
1730Sstevel@tonic-gate 	pr_rewind(this);
1740Sstevel@tonic-gate 	while ((p = pr_next(this)))
1750Sstevel@tonic-gate 		if (p->p_proto == proto)
1760Sstevel@tonic-gate 			break;
1770Sstevel@tonic-gate 	return (p);
1780Sstevel@tonic-gate }
1790Sstevel@tonic-gate 
1800Sstevel@tonic-gate static void
pr_rewind(struct irs_pr * this)1810Sstevel@tonic-gate pr_rewind(struct irs_pr *this) {
1820Sstevel@tonic-gate 	struct pvt *pvt = (struct pvt *)this->private;
1830Sstevel@tonic-gate 
1840Sstevel@tonic-gate 	if (pvt->fp) {
1850Sstevel@tonic-gate 		if (fseek(pvt->fp, 0L, SEEK_SET) == 0)
1860Sstevel@tonic-gate 			return;
1870Sstevel@tonic-gate 		(void)fclose(pvt->fp);
1880Sstevel@tonic-gate 	}
1890Sstevel@tonic-gate 	if (!(pvt->fp = fopen(_PATH_PROTOCOLS, "r" )))
1900Sstevel@tonic-gate 		return;
1910Sstevel@tonic-gate 	if (fcntl(fileno(pvt->fp), F_SETFD, 1) < 0) {
1920Sstevel@tonic-gate 		(void)fclose(pvt->fp);
1930Sstevel@tonic-gate 		pvt->fp = NULL;
1940Sstevel@tonic-gate 	}
1950Sstevel@tonic-gate }
1960Sstevel@tonic-gate 
1970Sstevel@tonic-gate static struct protoent *
pr_next(struct irs_pr * this)1980Sstevel@tonic-gate pr_next(struct irs_pr *this) {
1990Sstevel@tonic-gate 	struct pvt *pvt = (struct pvt *)this->private;
2000Sstevel@tonic-gate 	char *p, *cp, **q;
2010Sstevel@tonic-gate 	char *bufp, *ndbuf, *dbuf = NULL;
2020Sstevel@tonic-gate 	int c, bufsiz, offset;
2030Sstevel@tonic-gate 
2040Sstevel@tonic-gate 	if (!pvt->fp)
2050Sstevel@tonic-gate 		pr_rewind(this);
2060Sstevel@tonic-gate 	if (!pvt->fp)
2070Sstevel@tonic-gate 		return (NULL);
208*11038SRao.Shoaib@Sun.COM 	if (pvt->dbuf) {
209*11038SRao.Shoaib@Sun.COM 		free(pvt->dbuf);
210*11038SRao.Shoaib@Sun.COM 		pvt->dbuf = NULL;
211*11038SRao.Shoaib@Sun.COM 	}
2120Sstevel@tonic-gate 	bufp = pvt->line;
2130Sstevel@tonic-gate 	bufsiz = BUFSIZ;
2140Sstevel@tonic-gate 	offset = 0;
2150Sstevel@tonic-gate  again:
2160Sstevel@tonic-gate 	if ((p = fgets(bufp + offset, bufsiz - offset, pvt->fp)) == NULL) {
2170Sstevel@tonic-gate 		if (dbuf)
2180Sstevel@tonic-gate 			free(dbuf);
2190Sstevel@tonic-gate 		return (NULL);
2200Sstevel@tonic-gate 	}
2210Sstevel@tonic-gate 	if (!strchr(p, '\n') && !feof(pvt->fp)) {
2220Sstevel@tonic-gate #define GROWBUF 1024
2230Sstevel@tonic-gate 		/* allocate space for longer line */
2240Sstevel@tonic-gate 		if (dbuf == NULL) {
2250Sstevel@tonic-gate 			if ((ndbuf = malloc(bufsiz + GROWBUF)) != NULL)
2260Sstevel@tonic-gate 				strcpy(ndbuf, bufp);
2270Sstevel@tonic-gate 		} else
2280Sstevel@tonic-gate 			ndbuf = realloc(dbuf, bufsiz + GROWBUF);
2290Sstevel@tonic-gate 		if (ndbuf) {
2300Sstevel@tonic-gate 			dbuf = ndbuf;
2310Sstevel@tonic-gate 			bufp = dbuf;
2320Sstevel@tonic-gate 			bufsiz += GROWBUF;
2330Sstevel@tonic-gate 			offset = strlen(dbuf);
2340Sstevel@tonic-gate 		} else {
2350Sstevel@tonic-gate 			/* allocation failed; skip this long line */
2360Sstevel@tonic-gate 			while ((c = getc(pvt->fp)) != EOF)
2370Sstevel@tonic-gate 				if (c == '\n')
2380Sstevel@tonic-gate 					break;
2390Sstevel@tonic-gate 			if (c != EOF)
2400Sstevel@tonic-gate 				ungetc(c, pvt->fp);
2410Sstevel@tonic-gate 		}
2420Sstevel@tonic-gate 		goto again;
2430Sstevel@tonic-gate 	}
2440Sstevel@tonic-gate 
2450Sstevel@tonic-gate 	p -= offset;
2460Sstevel@tonic-gate 	offset = 0;
2470Sstevel@tonic-gate 
2480Sstevel@tonic-gate 	if (*p == '#')
2490Sstevel@tonic-gate 		goto again;
2500Sstevel@tonic-gate 	cp = strpbrk(p, "#\n");
2510Sstevel@tonic-gate 	if (cp != NULL)
2520Sstevel@tonic-gate 		*cp = '\0';
2530Sstevel@tonic-gate 	pvt->proto.p_name = p;
2540Sstevel@tonic-gate 	cp = strpbrk(p, " \t");
2550Sstevel@tonic-gate 	if (cp == NULL)
2560Sstevel@tonic-gate 		goto again;
2570Sstevel@tonic-gate 	*cp++ = '\0';
2580Sstevel@tonic-gate 	while (*cp == ' ' || *cp == '\t')
2590Sstevel@tonic-gate 		cp++;
2600Sstevel@tonic-gate 	p = strpbrk(cp, " \t");
2610Sstevel@tonic-gate 	if (p != NULL)
2620Sstevel@tonic-gate 		*p++ = '\0';
2630Sstevel@tonic-gate 	pvt->proto.p_proto = atoi(cp);
2640Sstevel@tonic-gate 	q = pvt->proto.p_aliases = pvt->proto_aliases;
2650Sstevel@tonic-gate 	if (p != NULL) {
2660Sstevel@tonic-gate 		cp = p;
2670Sstevel@tonic-gate 		while (cp && *cp) {
2680Sstevel@tonic-gate 			if (*cp == ' ' || *cp == '\t') {
2690Sstevel@tonic-gate 				cp++;
2700Sstevel@tonic-gate 				continue;
2710Sstevel@tonic-gate 			}
2720Sstevel@tonic-gate 			if (q < &pvt->proto_aliases[MAXALIASES - 1])
2730Sstevel@tonic-gate 				*q++ = cp;
2740Sstevel@tonic-gate 			cp = strpbrk(cp, " \t");
2750Sstevel@tonic-gate 			if (cp != NULL)
2760Sstevel@tonic-gate 				*cp++ = '\0';
2770Sstevel@tonic-gate 		}
2780Sstevel@tonic-gate 	}
2790Sstevel@tonic-gate 	*q = NULL;
280*11038SRao.Shoaib@Sun.COM 	pvt->dbuf = dbuf;
2810Sstevel@tonic-gate 	return (&pvt->proto);
2820Sstevel@tonic-gate }
2830Sstevel@tonic-gate 
2840Sstevel@tonic-gate static void
pr_minimize(struct irs_pr * this)2850Sstevel@tonic-gate pr_minimize(struct irs_pr *this) {
2860Sstevel@tonic-gate 	struct pvt *pvt = (struct pvt *)this->private;
2870Sstevel@tonic-gate 
2880Sstevel@tonic-gate 	if (pvt->fp != NULL) {
2890Sstevel@tonic-gate 		(void)fclose(pvt->fp);
2900Sstevel@tonic-gate 		pvt->fp = NULL;
2910Sstevel@tonic-gate 	}
2920Sstevel@tonic-gate }
293*11038SRao.Shoaib@Sun.COM 
294*11038SRao.Shoaib@Sun.COM /*! \file */
295