xref: /titanic_52/usr/src/lib/libresolv2/common/irs/lcl_sv.c (revision 9525b14bcdeb5b5f6f95ab27c2f48f18bd2ec829)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * Copyright (c) 1989, 1993, 1995
37c478bd9Sstevel@tonic-gate  *	The Regents of the University of California.  All rights reserved.
47c478bd9Sstevel@tonic-gate  *
57c478bd9Sstevel@tonic-gate  * Redistribution and use in source and binary forms, with or without
67c478bd9Sstevel@tonic-gate  * modification, are permitted provided that the following conditions
77c478bd9Sstevel@tonic-gate  * are met:
87c478bd9Sstevel@tonic-gate  * 1. Redistributions of source code must retain the above copyright
97c478bd9Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer.
107c478bd9Sstevel@tonic-gate  * 2. Redistributions in binary form must reproduce the above copyright
117c478bd9Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer in the
127c478bd9Sstevel@tonic-gate  *    documentation and/or other materials provided with the distribution.
137c478bd9Sstevel@tonic-gate  * 3. All advertising materials mentioning features or use of this software
147c478bd9Sstevel@tonic-gate  *    must display the following acknowledgement:
157c478bd9Sstevel@tonic-gate  *	This product includes software developed by the University of
167c478bd9Sstevel@tonic-gate  *	California, Berkeley and its contributors.
177c478bd9Sstevel@tonic-gate  * 4. Neither the name of the University nor the names of its contributors
187c478bd9Sstevel@tonic-gate  *    may be used to endorse or promote products derived from this software
197c478bd9Sstevel@tonic-gate  *    without specific prior written permission.
207c478bd9Sstevel@tonic-gate  *
217c478bd9Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
227c478bd9Sstevel@tonic-gate  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
237c478bd9Sstevel@tonic-gate  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
247c478bd9Sstevel@tonic-gate  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
257c478bd9Sstevel@tonic-gate  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
267c478bd9Sstevel@tonic-gate  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
277c478bd9Sstevel@tonic-gate  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
287c478bd9Sstevel@tonic-gate  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
297c478bd9Sstevel@tonic-gate  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
307c478bd9Sstevel@tonic-gate  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
317c478bd9Sstevel@tonic-gate  * SUCH DAMAGE.
327c478bd9Sstevel@tonic-gate  */
337c478bd9Sstevel@tonic-gate 
347c478bd9Sstevel@tonic-gate /*
35*9525b14bSRao Shoaib  * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
367c478bd9Sstevel@tonic-gate  * Portions Copyright (c) 1996-1999 by Internet Software Consortium.
377c478bd9Sstevel@tonic-gate  *
387c478bd9Sstevel@tonic-gate  * Permission to use, copy, modify, and distribute this software for any
397c478bd9Sstevel@tonic-gate  * purpose with or without fee is hereby granted, provided that the above
407c478bd9Sstevel@tonic-gate  * copyright notice and this permission notice appear in all copies.
417c478bd9Sstevel@tonic-gate  *
42*9525b14bSRao Shoaib  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
43*9525b14bSRao Shoaib  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
44*9525b14bSRao Shoaib  * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
45*9525b14bSRao Shoaib  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
46*9525b14bSRao Shoaib  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
47*9525b14bSRao Shoaib  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
48*9525b14bSRao Shoaib  * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
497c478bd9Sstevel@tonic-gate  */
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate #if defined(LIBC_SCCS) && !defined(lint)
52*9525b14bSRao Shoaib static const char rcsid[] = "$Id: lcl_sv.c,v 1.4 2005/04/27 04:56:31 sra Exp $";
537c478bd9Sstevel@tonic-gate #endif /* LIBC_SCCS and not lint */
547c478bd9Sstevel@tonic-gate 
557c478bd9Sstevel@tonic-gate /* extern */
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate #include "port_before.h"
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate #include <sys/types.h>
607c478bd9Sstevel@tonic-gate #include <sys/socket.h>
617c478bd9Sstevel@tonic-gate #include <netinet/in.h>
627c478bd9Sstevel@tonic-gate #include <arpa/nameser.h>
637c478bd9Sstevel@tonic-gate #include <resolv.h>
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate #ifdef IRS_LCL_SV_DB
667c478bd9Sstevel@tonic-gate #include <db.h>
677c478bd9Sstevel@tonic-gate #endif
687c478bd9Sstevel@tonic-gate #include <errno.h>
697c478bd9Sstevel@tonic-gate #include <fcntl.h>
707c478bd9Sstevel@tonic-gate #include <limits.h>
717c478bd9Sstevel@tonic-gate #include <stdio.h>
727c478bd9Sstevel@tonic-gate #include <string.h>
737c478bd9Sstevel@tonic-gate #include <stdlib.h>
747c478bd9Sstevel@tonic-gate 
757c478bd9Sstevel@tonic-gate #include <irs.h>
767c478bd9Sstevel@tonic-gate #include <isc/memcluster.h>
777c478bd9Sstevel@tonic-gate 
787c478bd9Sstevel@tonic-gate #include "port_after.h"
797c478bd9Sstevel@tonic-gate 
807c478bd9Sstevel@tonic-gate #include "irs_p.h"
817c478bd9Sstevel@tonic-gate #include "lcl_p.h"
827c478bd9Sstevel@tonic-gate 
837c478bd9Sstevel@tonic-gate #ifdef SPRINTF_CHAR
847c478bd9Sstevel@tonic-gate # define SPRINTF(x) strlen(sprintf/**/x)
857c478bd9Sstevel@tonic-gate #else
867c478bd9Sstevel@tonic-gate # define SPRINTF(x) ((size_t)sprintf x)
877c478bd9Sstevel@tonic-gate #endif
887c478bd9Sstevel@tonic-gate 
897c478bd9Sstevel@tonic-gate /* Types */
907c478bd9Sstevel@tonic-gate 
917c478bd9Sstevel@tonic-gate struct pvt {
927c478bd9Sstevel@tonic-gate #ifdef IRS_LCL_SV_DB
937c478bd9Sstevel@tonic-gate 	DB *		dbh;
947c478bd9Sstevel@tonic-gate 	int		dbf;
957c478bd9Sstevel@tonic-gate #endif
967c478bd9Sstevel@tonic-gate 	struct lcl_sv	sv;
977c478bd9Sstevel@tonic-gate };
987c478bd9Sstevel@tonic-gate 
997c478bd9Sstevel@tonic-gate /* Forward */
1007c478bd9Sstevel@tonic-gate 
1017c478bd9Sstevel@tonic-gate static void			sv_close(struct irs_sv*);
1027c478bd9Sstevel@tonic-gate static struct servent *		sv_next(struct irs_sv *);
1037c478bd9Sstevel@tonic-gate static struct servent *		sv_byname(struct irs_sv *, const char *,
1047c478bd9Sstevel@tonic-gate 					  const char *);
1057c478bd9Sstevel@tonic-gate static struct servent *		sv_byport(struct irs_sv *, int, const char *);
1067c478bd9Sstevel@tonic-gate static void			sv_rewind(struct irs_sv *);
1077c478bd9Sstevel@tonic-gate static void			sv_minimize(struct irs_sv *);
1087c478bd9Sstevel@tonic-gate /*global*/ struct servent *	irs_lclsv_fnxt(struct lcl_sv *);
1097c478bd9Sstevel@tonic-gate #ifdef IRS_LCL_SV_DB
1107c478bd9Sstevel@tonic-gate static struct servent *		sv_db_rec(struct lcl_sv *, DBT *, DBT *);
1117c478bd9Sstevel@tonic-gate #endif
1127c478bd9Sstevel@tonic-gate 
1137c478bd9Sstevel@tonic-gate /* Portability */
1147c478bd9Sstevel@tonic-gate 
1157c478bd9Sstevel@tonic-gate #ifndef SEEK_SET
1167c478bd9Sstevel@tonic-gate # define SEEK_SET 0
1177c478bd9Sstevel@tonic-gate #endif
1187c478bd9Sstevel@tonic-gate 
1197c478bd9Sstevel@tonic-gate /* Public */
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate struct irs_sv *
1227c478bd9Sstevel@tonic-gate irs_lcl_sv(struct irs_acc *this) {
1237c478bd9Sstevel@tonic-gate 	struct irs_sv *sv;
1247c478bd9Sstevel@tonic-gate 	struct pvt *pvt;
1257c478bd9Sstevel@tonic-gate 
1267c478bd9Sstevel@tonic-gate 	UNUSED(this);
1277c478bd9Sstevel@tonic-gate 
1287c478bd9Sstevel@tonic-gate 	if ((sv = memget(sizeof *sv)) == NULL) {
1297c478bd9Sstevel@tonic-gate 		errno = ENOMEM;
1307c478bd9Sstevel@tonic-gate 		return (NULL);
1317c478bd9Sstevel@tonic-gate 	}
1327c478bd9Sstevel@tonic-gate 	memset(sv, 0x5e, sizeof *sv);
1337c478bd9Sstevel@tonic-gate 	if ((pvt = memget(sizeof *pvt)) == NULL) {
1347c478bd9Sstevel@tonic-gate 		memput(sv, sizeof *sv);
1357c478bd9Sstevel@tonic-gate 		errno = ENOMEM;
1367c478bd9Sstevel@tonic-gate 		return (NULL);
1377c478bd9Sstevel@tonic-gate 	}
1387c478bd9Sstevel@tonic-gate 	memset(pvt, 0, sizeof *pvt);
1397c478bd9Sstevel@tonic-gate 	sv->private = pvt;
1407c478bd9Sstevel@tonic-gate 	sv->close = sv_close;
1417c478bd9Sstevel@tonic-gate 	sv->next = sv_next;
1427c478bd9Sstevel@tonic-gate 	sv->byname = sv_byname;
1437c478bd9Sstevel@tonic-gate 	sv->byport = sv_byport;
1447c478bd9Sstevel@tonic-gate 	sv->rewind = sv_rewind;
1457c478bd9Sstevel@tonic-gate 	sv->minimize = sv_minimize;
1467c478bd9Sstevel@tonic-gate 	sv->res_get = NULL;
1477c478bd9Sstevel@tonic-gate 	sv->res_set = NULL;
1487c478bd9Sstevel@tonic-gate #ifdef IRS_LCL_SV_DB
1497c478bd9Sstevel@tonic-gate 	pvt->dbf = R_FIRST;
1507c478bd9Sstevel@tonic-gate #endif
1517c478bd9Sstevel@tonic-gate 	return (sv);
1527c478bd9Sstevel@tonic-gate }
1537c478bd9Sstevel@tonic-gate 
1547c478bd9Sstevel@tonic-gate /* Methods */
1557c478bd9Sstevel@tonic-gate 
1567c478bd9Sstevel@tonic-gate static void
1577c478bd9Sstevel@tonic-gate sv_close(struct irs_sv *this) {
1587c478bd9Sstevel@tonic-gate 	struct pvt *pvt = (struct pvt *)this->private;
1597c478bd9Sstevel@tonic-gate 
1607c478bd9Sstevel@tonic-gate #ifdef IRS_LCL_SV_DB
1617c478bd9Sstevel@tonic-gate 	if (pvt->dbh != NULL)
1627c478bd9Sstevel@tonic-gate 		(*pvt->dbh->close)(pvt->dbh);
1637c478bd9Sstevel@tonic-gate #endif
1647c478bd9Sstevel@tonic-gate 	if (pvt->sv.fp)
1657c478bd9Sstevel@tonic-gate 		fclose(pvt->sv.fp);
1667c478bd9Sstevel@tonic-gate 	memput(pvt, sizeof *pvt);
1677c478bd9Sstevel@tonic-gate 	memput(this, sizeof *this);
1687c478bd9Sstevel@tonic-gate }
1697c478bd9Sstevel@tonic-gate 
1707c478bd9Sstevel@tonic-gate static struct servent *
1717c478bd9Sstevel@tonic-gate sv_byname(struct irs_sv *this, const char *name, const char *proto) {
1727c478bd9Sstevel@tonic-gate #ifdef IRS_LCL_SV_DB
1737c478bd9Sstevel@tonic-gate 	struct pvt *pvt = (struct pvt *)this->private;
1747c478bd9Sstevel@tonic-gate #endif
1757c478bd9Sstevel@tonic-gate 	struct servent *p;
1767c478bd9Sstevel@tonic-gate 	char **cp;
1777c478bd9Sstevel@tonic-gate 
1787c478bd9Sstevel@tonic-gate 	sv_rewind(this);
1797c478bd9Sstevel@tonic-gate #ifdef IRS_LCL_SV_DB
1807c478bd9Sstevel@tonic-gate 	if (pvt->dbh != NULL) {
1817c478bd9Sstevel@tonic-gate 		DBT key, data;
1827c478bd9Sstevel@tonic-gate 
1837c478bd9Sstevel@tonic-gate 		/* Note that (sizeof "/") == 2. */
1847c478bd9Sstevel@tonic-gate 		if ((strlen(name) + sizeof "/" + proto ? strlen(proto) : 0)
1857c478bd9Sstevel@tonic-gate 		    > sizeof pvt->sv.line)
1867c478bd9Sstevel@tonic-gate 			goto try_local;
1877c478bd9Sstevel@tonic-gate 		key.data = pvt->sv.line;
1887c478bd9Sstevel@tonic-gate 		key.size = SPRINTF((pvt->sv.line, "%s/%s", name,
1897c478bd9Sstevel@tonic-gate 				    proto ? proto : "")) + 1;
1907c478bd9Sstevel@tonic-gate 		if (proto != NULL) {
1917c478bd9Sstevel@tonic-gate 			if ((*pvt->dbh->get)(pvt->dbh, &key, &data, 0) != 0)
1927c478bd9Sstevel@tonic-gate 				return (NULL);
1937c478bd9Sstevel@tonic-gate 		} else if ((*pvt->dbh->seq)(pvt->dbh, &key, &data, R_CURSOR)
1947c478bd9Sstevel@tonic-gate 			   != 0)
1957c478bd9Sstevel@tonic-gate 			return (NULL);
1967c478bd9Sstevel@tonic-gate 		return (sv_db_rec(&pvt->sv, &key, &data));
1977c478bd9Sstevel@tonic-gate 	}
1987c478bd9Sstevel@tonic-gate  try_local:
1997c478bd9Sstevel@tonic-gate #endif
2007c478bd9Sstevel@tonic-gate 
2017c478bd9Sstevel@tonic-gate 	while ((p = sv_next(this))) {
2027c478bd9Sstevel@tonic-gate 		if (strcmp(name, p->s_name) == 0)
2037c478bd9Sstevel@tonic-gate 			goto gotname;
2047c478bd9Sstevel@tonic-gate 		for (cp = p->s_aliases; *cp; cp++)
2057c478bd9Sstevel@tonic-gate 			if (strcmp(name, *cp) == 0)
2067c478bd9Sstevel@tonic-gate 				goto gotname;
2077c478bd9Sstevel@tonic-gate 		continue;
2087c478bd9Sstevel@tonic-gate  gotname:
2097c478bd9Sstevel@tonic-gate 		if (proto == NULL || strcmp(p->s_proto, proto) == 0)
2107c478bd9Sstevel@tonic-gate 			break;
2117c478bd9Sstevel@tonic-gate 	}
2127c478bd9Sstevel@tonic-gate 	return (p);
2137c478bd9Sstevel@tonic-gate }
2147c478bd9Sstevel@tonic-gate 
2157c478bd9Sstevel@tonic-gate static struct servent *
2167c478bd9Sstevel@tonic-gate sv_byport(struct irs_sv *this, int port, const char *proto) {
2177c478bd9Sstevel@tonic-gate #ifdef IRS_LCL_SV_DB
2187c478bd9Sstevel@tonic-gate 	struct pvt *pvt = (struct pvt *)this->private;
2197c478bd9Sstevel@tonic-gate #endif
2207c478bd9Sstevel@tonic-gate 	struct servent *p;
2217c478bd9Sstevel@tonic-gate 
2227c478bd9Sstevel@tonic-gate 	sv_rewind(this);
2237c478bd9Sstevel@tonic-gate #ifdef IRS_LCL_SV_DB
2247c478bd9Sstevel@tonic-gate 	if (pvt->dbh != NULL) {
2257c478bd9Sstevel@tonic-gate 		DBT key, data;
2267c478bd9Sstevel@tonic-gate 		u_short *ports;
2277c478bd9Sstevel@tonic-gate 
2287c478bd9Sstevel@tonic-gate 		ports = (u_short *)pvt->sv.line;
2297c478bd9Sstevel@tonic-gate 		ports[0] = 0;
2307c478bd9Sstevel@tonic-gate 		ports[1] = port;
2317c478bd9Sstevel@tonic-gate 		key.data = ports;
2327c478bd9Sstevel@tonic-gate 		key.size = sizeof(u_short) * 2;
2337c478bd9Sstevel@tonic-gate 		if (proto && *proto) {
2347c478bd9Sstevel@tonic-gate 			strncpy((char *)ports + key.size, proto,
2357c478bd9Sstevel@tonic-gate 				BUFSIZ - key.size);
2367c478bd9Sstevel@tonic-gate 			key.size += strlen((char *)ports + key.size) + 1;
2377c478bd9Sstevel@tonic-gate 			if ((*pvt->dbh->get)(pvt->dbh, &key, &data, 0) != 0)
2387c478bd9Sstevel@tonic-gate 				return (NULL);
2397c478bd9Sstevel@tonic-gate 		} else {
2407c478bd9Sstevel@tonic-gate 			if ((*pvt->dbh->seq)(pvt->dbh, &key, &data, R_CURSOR)
2417c478bd9Sstevel@tonic-gate 			    != 0)
2427c478bd9Sstevel@tonic-gate 				return (NULL);
2437c478bd9Sstevel@tonic-gate 		}
2447c478bd9Sstevel@tonic-gate 		return (sv_db_rec(&pvt->sv, &key, &data));
2457c478bd9Sstevel@tonic-gate 	}
2467c478bd9Sstevel@tonic-gate #endif
2477c478bd9Sstevel@tonic-gate 	while ((p = sv_next(this))) {
2487c478bd9Sstevel@tonic-gate 		if (p->s_port != port)
2497c478bd9Sstevel@tonic-gate 			continue;
2507c478bd9Sstevel@tonic-gate 		if (proto == NULL || strcmp(p->s_proto, proto) == 0)
2517c478bd9Sstevel@tonic-gate 			break;
2527c478bd9Sstevel@tonic-gate 	}
2537c478bd9Sstevel@tonic-gate 	return (p);
2547c478bd9Sstevel@tonic-gate }
2557c478bd9Sstevel@tonic-gate 
2567c478bd9Sstevel@tonic-gate static void
2577c478bd9Sstevel@tonic-gate sv_rewind(struct irs_sv *this) {
2587c478bd9Sstevel@tonic-gate 	struct pvt *pvt = (struct pvt *)this->private;
2597c478bd9Sstevel@tonic-gate 
2607c478bd9Sstevel@tonic-gate 	if (pvt->sv.fp) {
2617c478bd9Sstevel@tonic-gate 		if (fseek(pvt->sv.fp, 0L, SEEK_SET) == 0)
2627c478bd9Sstevel@tonic-gate 			return;
2637c478bd9Sstevel@tonic-gate 		(void)fclose(pvt->sv.fp);
2647c478bd9Sstevel@tonic-gate 		pvt->sv.fp = NULL;
2657c478bd9Sstevel@tonic-gate 	}
2667c478bd9Sstevel@tonic-gate #ifdef IRS_LCL_SV_DB
2677c478bd9Sstevel@tonic-gate 	pvt->dbf = R_FIRST;
2687c478bd9Sstevel@tonic-gate 	if (pvt->dbh != NULL)
2697c478bd9Sstevel@tonic-gate 		return;
2707c478bd9Sstevel@tonic-gate 	pvt->dbh = dbopen(_PATH_SERVICES_DB, O_RDONLY,O_RDONLY,DB_BTREE, NULL);
2717c478bd9Sstevel@tonic-gate 	if (pvt->dbh != NULL) {
2727c478bd9Sstevel@tonic-gate 		if (fcntl((*pvt->dbh->fd)(pvt->dbh), F_SETFD, 1) < 0) {
2737c478bd9Sstevel@tonic-gate 			(*pvt->dbh->close)(pvt->dbh);
2747c478bd9Sstevel@tonic-gate 			pvt->dbh = NULL;
2757c478bd9Sstevel@tonic-gate 		}
2767c478bd9Sstevel@tonic-gate 		return;
2777c478bd9Sstevel@tonic-gate 	}
2787c478bd9Sstevel@tonic-gate #endif
2797c478bd9Sstevel@tonic-gate 	if ((pvt->sv.fp = fopen(_PATH_SERVICES, "r")) == NULL)
2807c478bd9Sstevel@tonic-gate 		return;
2817c478bd9Sstevel@tonic-gate 	if (fcntl(fileno(pvt->sv.fp), F_SETFD, 1) < 0) {
2827c478bd9Sstevel@tonic-gate 		(void)fclose(pvt->sv.fp);
2837c478bd9Sstevel@tonic-gate 		pvt->sv.fp = NULL;
2847c478bd9Sstevel@tonic-gate 	}
2857c478bd9Sstevel@tonic-gate }
2867c478bd9Sstevel@tonic-gate 
2877c478bd9Sstevel@tonic-gate static struct servent *
2887c478bd9Sstevel@tonic-gate sv_next(struct irs_sv *this) {
2897c478bd9Sstevel@tonic-gate 	struct pvt *pvt = (struct pvt *)this->private;
2907c478bd9Sstevel@tonic-gate 
2917c478bd9Sstevel@tonic-gate #ifdef IRS_LCL_SV_DB
2927c478bd9Sstevel@tonic-gate 	if (pvt->dbh == NULL && pvt->sv.fp == NULL)
2937c478bd9Sstevel@tonic-gate #else
2947c478bd9Sstevel@tonic-gate 	if (pvt->sv.fp == NULL)
2957c478bd9Sstevel@tonic-gate #endif
2967c478bd9Sstevel@tonic-gate 		sv_rewind(this);
2977c478bd9Sstevel@tonic-gate 
2987c478bd9Sstevel@tonic-gate #ifdef IRS_LCL_SV_DB
2997c478bd9Sstevel@tonic-gate 	if (pvt->dbh != NULL) {
3007c478bd9Sstevel@tonic-gate 		DBT key, data;
3017c478bd9Sstevel@tonic-gate 
3027c478bd9Sstevel@tonic-gate 		while ((*pvt->dbh->seq)(pvt->dbh, &key, &data, pvt->dbf) == 0){
3037c478bd9Sstevel@tonic-gate 			pvt->dbf = R_NEXT;
3047c478bd9Sstevel@tonic-gate 			if (((char *)key.data)[0])
3057c478bd9Sstevel@tonic-gate 				continue;
3067c478bd9Sstevel@tonic-gate 			return (sv_db_rec(&pvt->sv, &key, &data));
3077c478bd9Sstevel@tonic-gate 		}
3087c478bd9Sstevel@tonic-gate 	}
3097c478bd9Sstevel@tonic-gate #endif
3107c478bd9Sstevel@tonic-gate 
3117c478bd9Sstevel@tonic-gate 	if (pvt->sv.fp == NULL)
3127c478bd9Sstevel@tonic-gate 		return (NULL);
3137c478bd9Sstevel@tonic-gate 	return (irs_lclsv_fnxt(&pvt->sv));
3147c478bd9Sstevel@tonic-gate }
3157c478bd9Sstevel@tonic-gate 
3167c478bd9Sstevel@tonic-gate static void
3177c478bd9Sstevel@tonic-gate sv_minimize(struct irs_sv *this) {
3187c478bd9Sstevel@tonic-gate 	struct pvt *pvt = (struct pvt *)this->private;
3197c478bd9Sstevel@tonic-gate 
3207c478bd9Sstevel@tonic-gate #ifdef IRS_LCL_SV_DB
3217c478bd9Sstevel@tonic-gate 	if (pvt->dbh != NULL) {
3227c478bd9Sstevel@tonic-gate 		(*pvt->dbh->close)(pvt->dbh);
3237c478bd9Sstevel@tonic-gate 		pvt->dbh = NULL;
3247c478bd9Sstevel@tonic-gate 	}
3257c478bd9Sstevel@tonic-gate #endif
3267c478bd9Sstevel@tonic-gate 	if (pvt->sv.fp != NULL) {
3277c478bd9Sstevel@tonic-gate 		(void)fclose(pvt->sv.fp);
3287c478bd9Sstevel@tonic-gate 		pvt->sv.fp = NULL;
3297c478bd9Sstevel@tonic-gate 	}
3307c478bd9Sstevel@tonic-gate }
3317c478bd9Sstevel@tonic-gate 
3327c478bd9Sstevel@tonic-gate /* Quasipublic. */
3337c478bd9Sstevel@tonic-gate 
3347c478bd9Sstevel@tonic-gate struct servent *
3357c478bd9Sstevel@tonic-gate irs_lclsv_fnxt(struct lcl_sv *sv) {
3367c478bd9Sstevel@tonic-gate 	char *p, *cp, **q;
3377c478bd9Sstevel@tonic-gate 
3387c478bd9Sstevel@tonic-gate  again:
3397c478bd9Sstevel@tonic-gate 	if ((p = fgets(sv->line, BUFSIZ, sv->fp)) == NULL)
3407c478bd9Sstevel@tonic-gate 		return (NULL);
3417c478bd9Sstevel@tonic-gate 	if (*p == '#')
3427c478bd9Sstevel@tonic-gate 		goto again;
3437c478bd9Sstevel@tonic-gate 	sv->serv.s_name = p;
3447c478bd9Sstevel@tonic-gate 	while (*p && *p != '\n' && *p != ' ' && *p != '\t' && *p != '#')
3457c478bd9Sstevel@tonic-gate 		++p;
3467c478bd9Sstevel@tonic-gate 	if (*p == '\0' || *p == '#' || *p == '\n')
3477c478bd9Sstevel@tonic-gate 		goto again;
3487c478bd9Sstevel@tonic-gate 	*p++ = '\0';
3497c478bd9Sstevel@tonic-gate 	while (*p == ' ' || *p == '\t')
3507c478bd9Sstevel@tonic-gate 		p++;
3517c478bd9Sstevel@tonic-gate 	if (*p == '\0' || *p == '#' || *p == '\n')
3527c478bd9Sstevel@tonic-gate 		goto again;
3537c478bd9Sstevel@tonic-gate 	sv->serv.s_port = htons((u_short)strtol(p, &cp, 10));
3547c478bd9Sstevel@tonic-gate 	if (cp == p || (*cp != '/' && *cp != ','))
3557c478bd9Sstevel@tonic-gate 		goto again;
3567c478bd9Sstevel@tonic-gate 	p = cp + 1;
3577c478bd9Sstevel@tonic-gate 	sv->serv.s_proto = p;
3587c478bd9Sstevel@tonic-gate 
3597c478bd9Sstevel@tonic-gate 	q = sv->serv.s_aliases = sv->serv_aliases;
3607c478bd9Sstevel@tonic-gate 
3617c478bd9Sstevel@tonic-gate 	while (*p && *p != '\n' && *p != ' ' && *p != '\t' && *p != '#')
3627c478bd9Sstevel@tonic-gate 		++p;
3637c478bd9Sstevel@tonic-gate 
3647c478bd9Sstevel@tonic-gate 	while (*p == ' ' || *p == '\t') {
3657c478bd9Sstevel@tonic-gate 		*p++ = '\0';
3667c478bd9Sstevel@tonic-gate 		while (*p == ' ' || *p == '\t')
3677c478bd9Sstevel@tonic-gate 			++p;
3687c478bd9Sstevel@tonic-gate 		if (*p == '\0' || *p == '#' || *p == '\n')
3697c478bd9Sstevel@tonic-gate 			break;
3707c478bd9Sstevel@tonic-gate 		if (q < &sv->serv_aliases[IRS_SV_MAXALIASES - 1])
3717c478bd9Sstevel@tonic-gate 			*q++ = p;
3727c478bd9Sstevel@tonic-gate 		while (*p && *p != '\n' && *p != ' ' && *p != '\t' && *p != '#')
3737c478bd9Sstevel@tonic-gate 			++p;
3747c478bd9Sstevel@tonic-gate 	}
3757c478bd9Sstevel@tonic-gate 
3767c478bd9Sstevel@tonic-gate 	*p = '\0';
3777c478bd9Sstevel@tonic-gate 	*q = NULL;
3787c478bd9Sstevel@tonic-gate 	return (&sv->serv);
3797c478bd9Sstevel@tonic-gate }
3807c478bd9Sstevel@tonic-gate 
3817c478bd9Sstevel@tonic-gate /* Private. */
3827c478bd9Sstevel@tonic-gate 
3837c478bd9Sstevel@tonic-gate #ifdef IRS_LCL_SV_DB
3847c478bd9Sstevel@tonic-gate static struct servent *
3857c478bd9Sstevel@tonic-gate sv_db_rec(struct lcl_sv *sv, DBT *key, DBT *data) {
3867c478bd9Sstevel@tonic-gate 	char *p, **q;
3877c478bd9Sstevel@tonic-gate 	int n;
3887c478bd9Sstevel@tonic-gate 
3897c478bd9Sstevel@tonic-gate 	p = data->data;
390*9525b14bSRao Shoaib 	p[data->size - 1] = '\0';	/*%< should be, but we depend on it */
3917c478bd9Sstevel@tonic-gate 	if (((char *)key->data)[0] == '\0') {
3927c478bd9Sstevel@tonic-gate 		if (key->size < sizeof(u_short)*2 || data->size < 2)
3937c478bd9Sstevel@tonic-gate 			return (NULL);
3947c478bd9Sstevel@tonic-gate 		sv->serv.s_port = ((u_short *)key->data)[1];
3957c478bd9Sstevel@tonic-gate 		n = strlen(p) + 1;
3967c478bd9Sstevel@tonic-gate 		if ((size_t)n > sizeof(sv->line)) {
3977c478bd9Sstevel@tonic-gate 			n = sizeof(sv->line);
3987c478bd9Sstevel@tonic-gate 		}
3997c478bd9Sstevel@tonic-gate 		memcpy(sv->line, p, n);
4007c478bd9Sstevel@tonic-gate 		sv->serv.s_name = sv->line;
4017c478bd9Sstevel@tonic-gate 		if ((sv->serv.s_proto = strchr(sv->line, '/')) != NULL)
4027c478bd9Sstevel@tonic-gate 			*(sv->serv.s_proto)++ = '\0';
4037c478bd9Sstevel@tonic-gate 		p += n;
4047c478bd9Sstevel@tonic-gate 		data->size -= n;
4057c478bd9Sstevel@tonic-gate 	} else {
4067c478bd9Sstevel@tonic-gate 		if (data->size < sizeof(u_short) + 1)
4077c478bd9Sstevel@tonic-gate 			return (NULL);
4087c478bd9Sstevel@tonic-gate 		if (key->size > sizeof(sv->line))
4097c478bd9Sstevel@tonic-gate 			key->size = sizeof(sv->line);
4107c478bd9Sstevel@tonic-gate 		((char *)key->data)[key->size - 1] = '\0';
4117c478bd9Sstevel@tonic-gate 		memcpy(sv->line, key->data, key->size);
4127c478bd9Sstevel@tonic-gate 		sv->serv.s_name = sv->line;
4137c478bd9Sstevel@tonic-gate 		if ((sv->serv.s_proto = strchr(sv->line, '/')) != NULL)
4147c478bd9Sstevel@tonic-gate 			*(sv->serv.s_proto)++ = '\0';
4157c478bd9Sstevel@tonic-gate 		sv->serv.s_port = *(u_short *)data->data;
4167c478bd9Sstevel@tonic-gate 		p += sizeof(u_short);
4177c478bd9Sstevel@tonic-gate 		data->size -= sizeof(u_short);
4187c478bd9Sstevel@tonic-gate 	}
4197c478bd9Sstevel@tonic-gate 	q = sv->serv.s_aliases = sv->serv_aliases;
4207c478bd9Sstevel@tonic-gate 	while (data->size > 0 && q < &sv->serv_aliases[IRS_SV_MAXALIASES - 1]) {
4217c478bd9Sstevel@tonic-gate 
4227c478bd9Sstevel@tonic-gate 		*q++ = p;
4237c478bd9Sstevel@tonic-gate 		n = strlen(p) + 1;
4247c478bd9Sstevel@tonic-gate 		data->size -= n;
4257c478bd9Sstevel@tonic-gate 		p += n;
4267c478bd9Sstevel@tonic-gate 	}
4277c478bd9Sstevel@tonic-gate 	*q = NULL;
4287c478bd9Sstevel@tonic-gate 	return (&sv->serv);
4297c478bd9Sstevel@tonic-gate }
4307c478bd9Sstevel@tonic-gate #endif
431*9525b14bSRao Shoaib 
432*9525b14bSRao Shoaib /*! \file */
433