xref: /onnv-gate/usr/src/lib/libresolv2/common/irs/lcl_sv.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_sv.c,v 1.4 2005/04/27 04:56:31 sra 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 <sys/socket.h>
610Sstevel@tonic-gate #include <netinet/in.h>
620Sstevel@tonic-gate #include <arpa/nameser.h>
630Sstevel@tonic-gate #include <resolv.h>
640Sstevel@tonic-gate 
650Sstevel@tonic-gate #ifdef IRS_LCL_SV_DB
660Sstevel@tonic-gate #include <db.h>
670Sstevel@tonic-gate #endif
680Sstevel@tonic-gate #include <errno.h>
690Sstevel@tonic-gate #include <fcntl.h>
700Sstevel@tonic-gate #include <limits.h>
710Sstevel@tonic-gate #include <stdio.h>
720Sstevel@tonic-gate #include <string.h>
730Sstevel@tonic-gate #include <stdlib.h>
740Sstevel@tonic-gate 
750Sstevel@tonic-gate #include <irs.h>
760Sstevel@tonic-gate #include <isc/memcluster.h>
770Sstevel@tonic-gate 
780Sstevel@tonic-gate #include "port_after.h"
790Sstevel@tonic-gate 
800Sstevel@tonic-gate #include "irs_p.h"
810Sstevel@tonic-gate #include "lcl_p.h"
820Sstevel@tonic-gate 
830Sstevel@tonic-gate #ifdef SPRINTF_CHAR
840Sstevel@tonic-gate # define SPRINTF(x) strlen(sprintf/**/x)
850Sstevel@tonic-gate #else
860Sstevel@tonic-gate # define SPRINTF(x) ((size_t)sprintf x)
870Sstevel@tonic-gate #endif
880Sstevel@tonic-gate 
890Sstevel@tonic-gate /* Types */
900Sstevel@tonic-gate 
910Sstevel@tonic-gate struct pvt {
920Sstevel@tonic-gate #ifdef IRS_LCL_SV_DB
930Sstevel@tonic-gate 	DB *		dbh;
940Sstevel@tonic-gate 	int		dbf;
950Sstevel@tonic-gate #endif
960Sstevel@tonic-gate 	struct lcl_sv	sv;
970Sstevel@tonic-gate };
980Sstevel@tonic-gate 
990Sstevel@tonic-gate /* Forward */
1000Sstevel@tonic-gate 
1010Sstevel@tonic-gate static void			sv_close(struct irs_sv*);
1020Sstevel@tonic-gate static struct servent *		sv_next(struct irs_sv *);
1030Sstevel@tonic-gate static struct servent *		sv_byname(struct irs_sv *, const char *,
1040Sstevel@tonic-gate 					  const char *);
1050Sstevel@tonic-gate static struct servent *		sv_byport(struct irs_sv *, int, const char *);
1060Sstevel@tonic-gate static void			sv_rewind(struct irs_sv *);
1070Sstevel@tonic-gate static void			sv_minimize(struct irs_sv *);
1080Sstevel@tonic-gate /*global*/ struct servent *	irs_lclsv_fnxt(struct lcl_sv *);
1090Sstevel@tonic-gate #ifdef IRS_LCL_SV_DB
1100Sstevel@tonic-gate static struct servent *		sv_db_rec(struct lcl_sv *, DBT *, DBT *);
1110Sstevel@tonic-gate #endif
1120Sstevel@tonic-gate 
1130Sstevel@tonic-gate /* Portability */
1140Sstevel@tonic-gate 
1150Sstevel@tonic-gate #ifndef SEEK_SET
1160Sstevel@tonic-gate # define SEEK_SET 0
1170Sstevel@tonic-gate #endif
1180Sstevel@tonic-gate 
1190Sstevel@tonic-gate /* Public */
1200Sstevel@tonic-gate 
1210Sstevel@tonic-gate struct irs_sv *
irs_lcl_sv(struct irs_acc * this)1220Sstevel@tonic-gate irs_lcl_sv(struct irs_acc *this) {
1230Sstevel@tonic-gate 	struct irs_sv *sv;
1240Sstevel@tonic-gate 	struct pvt *pvt;
1250Sstevel@tonic-gate 
1260Sstevel@tonic-gate 	UNUSED(this);
1270Sstevel@tonic-gate 
1280Sstevel@tonic-gate 	if ((sv = memget(sizeof *sv)) == NULL) {
1290Sstevel@tonic-gate 		errno = ENOMEM;
1300Sstevel@tonic-gate 		return (NULL);
1310Sstevel@tonic-gate 	}
1320Sstevel@tonic-gate 	memset(sv, 0x5e, sizeof *sv);
1330Sstevel@tonic-gate 	if ((pvt = memget(sizeof *pvt)) == NULL) {
1340Sstevel@tonic-gate 		memput(sv, sizeof *sv);
1350Sstevel@tonic-gate 		errno = ENOMEM;
1360Sstevel@tonic-gate 		return (NULL);
1370Sstevel@tonic-gate 	}
1380Sstevel@tonic-gate 	memset(pvt, 0, sizeof *pvt);
1390Sstevel@tonic-gate 	sv->private = pvt;
1400Sstevel@tonic-gate 	sv->close = sv_close;
1410Sstevel@tonic-gate 	sv->next = sv_next;
1420Sstevel@tonic-gate 	sv->byname = sv_byname;
1430Sstevel@tonic-gate 	sv->byport = sv_byport;
1440Sstevel@tonic-gate 	sv->rewind = sv_rewind;
1450Sstevel@tonic-gate 	sv->minimize = sv_minimize;
1460Sstevel@tonic-gate 	sv->res_get = NULL;
1470Sstevel@tonic-gate 	sv->res_set = NULL;
1480Sstevel@tonic-gate #ifdef IRS_LCL_SV_DB
1490Sstevel@tonic-gate 	pvt->dbf = R_FIRST;
1500Sstevel@tonic-gate #endif
1510Sstevel@tonic-gate 	return (sv);
1520Sstevel@tonic-gate }
1530Sstevel@tonic-gate 
1540Sstevel@tonic-gate /* Methods */
1550Sstevel@tonic-gate 
1560Sstevel@tonic-gate static void
sv_close(struct irs_sv * this)1570Sstevel@tonic-gate sv_close(struct irs_sv *this) {
1580Sstevel@tonic-gate 	struct pvt *pvt = (struct pvt *)this->private;
1590Sstevel@tonic-gate 
1600Sstevel@tonic-gate #ifdef IRS_LCL_SV_DB
1610Sstevel@tonic-gate 	if (pvt->dbh != NULL)
1620Sstevel@tonic-gate 		(*pvt->dbh->close)(pvt->dbh);
1630Sstevel@tonic-gate #endif
1640Sstevel@tonic-gate 	if (pvt->sv.fp)
1650Sstevel@tonic-gate 		fclose(pvt->sv.fp);
1660Sstevel@tonic-gate 	memput(pvt, sizeof *pvt);
1670Sstevel@tonic-gate 	memput(this, sizeof *this);
1680Sstevel@tonic-gate }
1690Sstevel@tonic-gate 
1700Sstevel@tonic-gate static struct servent *
sv_byname(struct irs_sv * this,const char * name,const char * proto)1710Sstevel@tonic-gate sv_byname(struct irs_sv *this, const char *name, const char *proto) {
1720Sstevel@tonic-gate #ifdef IRS_LCL_SV_DB
1730Sstevel@tonic-gate 	struct pvt *pvt = (struct pvt *)this->private;
1740Sstevel@tonic-gate #endif
1750Sstevel@tonic-gate 	struct servent *p;
1760Sstevel@tonic-gate 	char **cp;
1770Sstevel@tonic-gate 
1780Sstevel@tonic-gate 	sv_rewind(this);
1790Sstevel@tonic-gate #ifdef IRS_LCL_SV_DB
1800Sstevel@tonic-gate 	if (pvt->dbh != NULL) {
1810Sstevel@tonic-gate 		DBT key, data;
1820Sstevel@tonic-gate 
1830Sstevel@tonic-gate 		/* Note that (sizeof "/") == 2. */
1840Sstevel@tonic-gate 		if ((strlen(name) + sizeof "/" + proto ? strlen(proto) : 0)
1850Sstevel@tonic-gate 		    > sizeof pvt->sv.line)
1860Sstevel@tonic-gate 			goto try_local;
1870Sstevel@tonic-gate 		key.data = pvt->sv.line;
1880Sstevel@tonic-gate 		key.size = SPRINTF((pvt->sv.line, "%s/%s", name,
1890Sstevel@tonic-gate 				    proto ? proto : "")) + 1;
1900Sstevel@tonic-gate 		if (proto != NULL) {
1910Sstevel@tonic-gate 			if ((*pvt->dbh->get)(pvt->dbh, &key, &data, 0) != 0)
1920Sstevel@tonic-gate 				return (NULL);
1930Sstevel@tonic-gate 		} else if ((*pvt->dbh->seq)(pvt->dbh, &key, &data, R_CURSOR)
1940Sstevel@tonic-gate 			   != 0)
1950Sstevel@tonic-gate 			return (NULL);
1960Sstevel@tonic-gate 		return (sv_db_rec(&pvt->sv, &key, &data));
1970Sstevel@tonic-gate 	}
1980Sstevel@tonic-gate  try_local:
1990Sstevel@tonic-gate #endif
2000Sstevel@tonic-gate 
2010Sstevel@tonic-gate 	while ((p = sv_next(this))) {
2020Sstevel@tonic-gate 		if (strcmp(name, p->s_name) == 0)
2030Sstevel@tonic-gate 			goto gotname;
2040Sstevel@tonic-gate 		for (cp = p->s_aliases; *cp; cp++)
2050Sstevel@tonic-gate 			if (strcmp(name, *cp) == 0)
2060Sstevel@tonic-gate 				goto gotname;
2070Sstevel@tonic-gate 		continue;
2080Sstevel@tonic-gate  gotname:
2090Sstevel@tonic-gate 		if (proto == NULL || strcmp(p->s_proto, proto) == 0)
2100Sstevel@tonic-gate 			break;
2110Sstevel@tonic-gate 	}
2120Sstevel@tonic-gate 	return (p);
2130Sstevel@tonic-gate }
2140Sstevel@tonic-gate 
2150Sstevel@tonic-gate static struct servent *
sv_byport(struct irs_sv * this,int port,const char * proto)2160Sstevel@tonic-gate sv_byport(struct irs_sv *this, int port, const char *proto) {
2170Sstevel@tonic-gate #ifdef IRS_LCL_SV_DB
2180Sstevel@tonic-gate 	struct pvt *pvt = (struct pvt *)this->private;
2190Sstevel@tonic-gate #endif
2200Sstevel@tonic-gate 	struct servent *p;
2210Sstevel@tonic-gate 
2220Sstevel@tonic-gate 	sv_rewind(this);
2230Sstevel@tonic-gate #ifdef IRS_LCL_SV_DB
2240Sstevel@tonic-gate 	if (pvt->dbh != NULL) {
2250Sstevel@tonic-gate 		DBT key, data;
2260Sstevel@tonic-gate 		u_short *ports;
2270Sstevel@tonic-gate 
2280Sstevel@tonic-gate 		ports = (u_short *)pvt->sv.line;
2290Sstevel@tonic-gate 		ports[0] = 0;
2300Sstevel@tonic-gate 		ports[1] = port;
2310Sstevel@tonic-gate 		key.data = ports;
2320Sstevel@tonic-gate 		key.size = sizeof(u_short) * 2;
2330Sstevel@tonic-gate 		if (proto && *proto) {
2340Sstevel@tonic-gate 			strncpy((char *)ports + key.size, proto,
2350Sstevel@tonic-gate 				BUFSIZ - key.size);
2360Sstevel@tonic-gate 			key.size += strlen((char *)ports + key.size) + 1;
2370Sstevel@tonic-gate 			if ((*pvt->dbh->get)(pvt->dbh, &key, &data, 0) != 0)
2380Sstevel@tonic-gate 				return (NULL);
2390Sstevel@tonic-gate 		} else {
2400Sstevel@tonic-gate 			if ((*pvt->dbh->seq)(pvt->dbh, &key, &data, R_CURSOR)
2410Sstevel@tonic-gate 			    != 0)
2420Sstevel@tonic-gate 				return (NULL);
2430Sstevel@tonic-gate 		}
2440Sstevel@tonic-gate 		return (sv_db_rec(&pvt->sv, &key, &data));
2450Sstevel@tonic-gate 	}
2460Sstevel@tonic-gate #endif
2470Sstevel@tonic-gate 	while ((p = sv_next(this))) {
2480Sstevel@tonic-gate 		if (p->s_port != port)
2490Sstevel@tonic-gate 			continue;
2500Sstevel@tonic-gate 		if (proto == NULL || strcmp(p->s_proto, proto) == 0)
2510Sstevel@tonic-gate 			break;
2520Sstevel@tonic-gate 	}
2530Sstevel@tonic-gate 	return (p);
2540Sstevel@tonic-gate }
2550Sstevel@tonic-gate 
2560Sstevel@tonic-gate static void
sv_rewind(struct irs_sv * this)2570Sstevel@tonic-gate sv_rewind(struct irs_sv *this) {
2580Sstevel@tonic-gate 	struct pvt *pvt = (struct pvt *)this->private;
2590Sstevel@tonic-gate 
2600Sstevel@tonic-gate 	if (pvt->sv.fp) {
2610Sstevel@tonic-gate 		if (fseek(pvt->sv.fp, 0L, SEEK_SET) == 0)
2620Sstevel@tonic-gate 			return;
2630Sstevel@tonic-gate 		(void)fclose(pvt->sv.fp);
2640Sstevel@tonic-gate 		pvt->sv.fp = NULL;
2650Sstevel@tonic-gate 	}
2660Sstevel@tonic-gate #ifdef IRS_LCL_SV_DB
2670Sstevel@tonic-gate 	pvt->dbf = R_FIRST;
2680Sstevel@tonic-gate 	if (pvt->dbh != NULL)
2690Sstevel@tonic-gate 		return;
2700Sstevel@tonic-gate 	pvt->dbh = dbopen(_PATH_SERVICES_DB, O_RDONLY,O_RDONLY,DB_BTREE, NULL);
2710Sstevel@tonic-gate 	if (pvt->dbh != NULL) {
2720Sstevel@tonic-gate 		if (fcntl((*pvt->dbh->fd)(pvt->dbh), F_SETFD, 1) < 0) {
2730Sstevel@tonic-gate 			(*pvt->dbh->close)(pvt->dbh);
2740Sstevel@tonic-gate 			pvt->dbh = NULL;
2750Sstevel@tonic-gate 		}
2760Sstevel@tonic-gate 		return;
2770Sstevel@tonic-gate 	}
2780Sstevel@tonic-gate #endif
2790Sstevel@tonic-gate 	if ((pvt->sv.fp = fopen(_PATH_SERVICES, "r")) == NULL)
2800Sstevel@tonic-gate 		return;
2810Sstevel@tonic-gate 	if (fcntl(fileno(pvt->sv.fp), F_SETFD, 1) < 0) {
2820Sstevel@tonic-gate 		(void)fclose(pvt->sv.fp);
2830Sstevel@tonic-gate 		pvt->sv.fp = NULL;
2840Sstevel@tonic-gate 	}
2850Sstevel@tonic-gate }
2860Sstevel@tonic-gate 
2870Sstevel@tonic-gate static struct servent *
sv_next(struct irs_sv * this)2880Sstevel@tonic-gate sv_next(struct irs_sv *this) {
2890Sstevel@tonic-gate 	struct pvt *pvt = (struct pvt *)this->private;
2900Sstevel@tonic-gate 
2910Sstevel@tonic-gate #ifdef IRS_LCL_SV_DB
2920Sstevel@tonic-gate 	if (pvt->dbh == NULL && pvt->sv.fp == NULL)
2930Sstevel@tonic-gate #else
2940Sstevel@tonic-gate 	if (pvt->sv.fp == NULL)
2950Sstevel@tonic-gate #endif
2960Sstevel@tonic-gate 		sv_rewind(this);
2970Sstevel@tonic-gate 
2980Sstevel@tonic-gate #ifdef IRS_LCL_SV_DB
2990Sstevel@tonic-gate 	if (pvt->dbh != NULL) {
3000Sstevel@tonic-gate 		DBT key, data;
3010Sstevel@tonic-gate 
3020Sstevel@tonic-gate 		while ((*pvt->dbh->seq)(pvt->dbh, &key, &data, pvt->dbf) == 0){
3030Sstevel@tonic-gate 			pvt->dbf = R_NEXT;
3040Sstevel@tonic-gate 			if (((char *)key.data)[0])
3050Sstevel@tonic-gate 				continue;
3060Sstevel@tonic-gate 			return (sv_db_rec(&pvt->sv, &key, &data));
3070Sstevel@tonic-gate 		}
3080Sstevel@tonic-gate 	}
3090Sstevel@tonic-gate #endif
3100Sstevel@tonic-gate 
3110Sstevel@tonic-gate 	if (pvt->sv.fp == NULL)
3120Sstevel@tonic-gate 		return (NULL);
3130Sstevel@tonic-gate 	return (irs_lclsv_fnxt(&pvt->sv));
3140Sstevel@tonic-gate }
3150Sstevel@tonic-gate 
3160Sstevel@tonic-gate static void
sv_minimize(struct irs_sv * this)3170Sstevel@tonic-gate sv_minimize(struct irs_sv *this) {
3180Sstevel@tonic-gate 	struct pvt *pvt = (struct pvt *)this->private;
3190Sstevel@tonic-gate 
3200Sstevel@tonic-gate #ifdef IRS_LCL_SV_DB
3210Sstevel@tonic-gate 	if (pvt->dbh != NULL) {
3220Sstevel@tonic-gate 		(*pvt->dbh->close)(pvt->dbh);
3230Sstevel@tonic-gate 		pvt->dbh = NULL;
3240Sstevel@tonic-gate 	}
3250Sstevel@tonic-gate #endif
3260Sstevel@tonic-gate 	if (pvt->sv.fp != NULL) {
3270Sstevel@tonic-gate 		(void)fclose(pvt->sv.fp);
3280Sstevel@tonic-gate 		pvt->sv.fp = NULL;
3290Sstevel@tonic-gate 	}
3300Sstevel@tonic-gate }
3310Sstevel@tonic-gate 
3320Sstevel@tonic-gate /* Quasipublic. */
3330Sstevel@tonic-gate 
3340Sstevel@tonic-gate struct servent *
irs_lclsv_fnxt(struct lcl_sv * sv)3350Sstevel@tonic-gate irs_lclsv_fnxt(struct lcl_sv *sv) {
3360Sstevel@tonic-gate 	char *p, *cp, **q;
3370Sstevel@tonic-gate 
3380Sstevel@tonic-gate  again:
3390Sstevel@tonic-gate 	if ((p = fgets(sv->line, BUFSIZ, sv->fp)) == NULL)
3400Sstevel@tonic-gate 		return (NULL);
3410Sstevel@tonic-gate 	if (*p == '#')
3420Sstevel@tonic-gate 		goto again;
3430Sstevel@tonic-gate 	sv->serv.s_name = p;
3440Sstevel@tonic-gate 	while (*p && *p != '\n' && *p != ' ' && *p != '\t' && *p != '#')
3450Sstevel@tonic-gate 		++p;
3460Sstevel@tonic-gate 	if (*p == '\0' || *p == '#' || *p == '\n')
3470Sstevel@tonic-gate 		goto again;
3480Sstevel@tonic-gate 	*p++ = '\0';
3490Sstevel@tonic-gate 	while (*p == ' ' || *p == '\t')
3500Sstevel@tonic-gate 		p++;
3510Sstevel@tonic-gate 	if (*p == '\0' || *p == '#' || *p == '\n')
3520Sstevel@tonic-gate 		goto again;
3530Sstevel@tonic-gate 	sv->serv.s_port = htons((u_short)strtol(p, &cp, 10));
3540Sstevel@tonic-gate 	if (cp == p || (*cp != '/' && *cp != ','))
3550Sstevel@tonic-gate 		goto again;
3560Sstevel@tonic-gate 	p = cp + 1;
3570Sstevel@tonic-gate 	sv->serv.s_proto = p;
3580Sstevel@tonic-gate 
3590Sstevel@tonic-gate 	q = sv->serv.s_aliases = sv->serv_aliases;
3600Sstevel@tonic-gate 
3610Sstevel@tonic-gate 	while (*p && *p != '\n' && *p != ' ' && *p != '\t' && *p != '#')
3620Sstevel@tonic-gate 		++p;
3630Sstevel@tonic-gate 
3640Sstevel@tonic-gate 	while (*p == ' ' || *p == '\t') {
3650Sstevel@tonic-gate 		*p++ = '\0';
3660Sstevel@tonic-gate 		while (*p == ' ' || *p == '\t')
3670Sstevel@tonic-gate 			++p;
3680Sstevel@tonic-gate 		if (*p == '\0' || *p == '#' || *p == '\n')
3690Sstevel@tonic-gate 			break;
3700Sstevel@tonic-gate 		if (q < &sv->serv_aliases[IRS_SV_MAXALIASES - 1])
3710Sstevel@tonic-gate 			*q++ = p;
3720Sstevel@tonic-gate 		while (*p && *p != '\n' && *p != ' ' && *p != '\t' && *p != '#')
3730Sstevel@tonic-gate 			++p;
3740Sstevel@tonic-gate 	}
3750Sstevel@tonic-gate 
3760Sstevel@tonic-gate 	*p = '\0';
3770Sstevel@tonic-gate 	*q = NULL;
3780Sstevel@tonic-gate 	return (&sv->serv);
3790Sstevel@tonic-gate }
3800Sstevel@tonic-gate 
3810Sstevel@tonic-gate /* Private. */
3820Sstevel@tonic-gate 
3830Sstevel@tonic-gate #ifdef IRS_LCL_SV_DB
3840Sstevel@tonic-gate static struct servent *
sv_db_rec(struct lcl_sv * sv,DBT * key,DBT * data)3850Sstevel@tonic-gate sv_db_rec(struct lcl_sv *sv, DBT *key, DBT *data) {
3860Sstevel@tonic-gate 	char *p, **q;
3870Sstevel@tonic-gate 	int n;
3880Sstevel@tonic-gate 
3890Sstevel@tonic-gate 	p = data->data;
390*11038SRao.Shoaib@Sun.COM 	p[data->size - 1] = '\0';	/*%< should be, but we depend on it */
3910Sstevel@tonic-gate 	if (((char *)key->data)[0] == '\0') {
3920Sstevel@tonic-gate 		if (key->size < sizeof(u_short)*2 || data->size < 2)
3930Sstevel@tonic-gate 			return (NULL);
3940Sstevel@tonic-gate 		sv->serv.s_port = ((u_short *)key->data)[1];
3950Sstevel@tonic-gate 		n = strlen(p) + 1;
3960Sstevel@tonic-gate 		if ((size_t)n > sizeof(sv->line)) {
3970Sstevel@tonic-gate 			n = sizeof(sv->line);
3980Sstevel@tonic-gate 		}
3990Sstevel@tonic-gate 		memcpy(sv->line, p, n);
4000Sstevel@tonic-gate 		sv->serv.s_name = sv->line;
4010Sstevel@tonic-gate 		if ((sv->serv.s_proto = strchr(sv->line, '/')) != NULL)
4020Sstevel@tonic-gate 			*(sv->serv.s_proto)++ = '\0';
4030Sstevel@tonic-gate 		p += n;
4040Sstevel@tonic-gate 		data->size -= n;
4050Sstevel@tonic-gate 	} else {
4060Sstevel@tonic-gate 		if (data->size < sizeof(u_short) + 1)
4070Sstevel@tonic-gate 			return (NULL);
4080Sstevel@tonic-gate 		if (key->size > sizeof(sv->line))
4090Sstevel@tonic-gate 			key->size = sizeof(sv->line);
4100Sstevel@tonic-gate 		((char *)key->data)[key->size - 1] = '\0';
4110Sstevel@tonic-gate 		memcpy(sv->line, key->data, key->size);
4120Sstevel@tonic-gate 		sv->serv.s_name = sv->line;
4130Sstevel@tonic-gate 		if ((sv->serv.s_proto = strchr(sv->line, '/')) != NULL)
4140Sstevel@tonic-gate 			*(sv->serv.s_proto)++ = '\0';
4150Sstevel@tonic-gate 		sv->serv.s_port = *(u_short *)data->data;
4160Sstevel@tonic-gate 		p += sizeof(u_short);
4170Sstevel@tonic-gate 		data->size -= sizeof(u_short);
4180Sstevel@tonic-gate 	}
4190Sstevel@tonic-gate 	q = sv->serv.s_aliases = sv->serv_aliases;
4200Sstevel@tonic-gate 	while (data->size > 0 && q < &sv->serv_aliases[IRS_SV_MAXALIASES - 1]) {
4210Sstevel@tonic-gate 
4220Sstevel@tonic-gate 		*q++ = p;
4230Sstevel@tonic-gate 		n = strlen(p) + 1;
4240Sstevel@tonic-gate 		data->size -= n;
4250Sstevel@tonic-gate 		p += n;
4260Sstevel@tonic-gate 	}
4270Sstevel@tonic-gate 	*q = NULL;
4280Sstevel@tonic-gate 	return (&sv->serv);
4290Sstevel@tonic-gate }
4300Sstevel@tonic-gate #endif
431*11038SRao.Shoaib@Sun.COM 
432*11038SRao.Shoaib@Sun.COM /*! \file */
433