xref: /illumos-gate/usr/src/lib/libresolv2/common/irs/lcl_sv.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * Copyright 1997-2002 Sun Microsystems, Inc.  All rights reserved.
3*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
4*7c478bd9Sstevel@tonic-gate  */
5*7c478bd9Sstevel@tonic-gate 
6*7c478bd9Sstevel@tonic-gate /*
7*7c478bd9Sstevel@tonic-gate  * Copyright (c) 1989, 1993, 1995
8*7c478bd9Sstevel@tonic-gate  *	The Regents of the University of California.  All rights reserved.
9*7c478bd9Sstevel@tonic-gate  *
10*7c478bd9Sstevel@tonic-gate  * Redistribution and use in source and binary forms, with or without
11*7c478bd9Sstevel@tonic-gate  * modification, are permitted provided that the following conditions
12*7c478bd9Sstevel@tonic-gate  * are met:
13*7c478bd9Sstevel@tonic-gate  * 1. Redistributions of source code must retain the above copyright
14*7c478bd9Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer.
15*7c478bd9Sstevel@tonic-gate  * 2. Redistributions in binary form must reproduce the above copyright
16*7c478bd9Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer in the
17*7c478bd9Sstevel@tonic-gate  *    documentation and/or other materials provided with the distribution.
18*7c478bd9Sstevel@tonic-gate  * 3. All advertising materials mentioning features or use of this software
19*7c478bd9Sstevel@tonic-gate  *    must display the following acknowledgement:
20*7c478bd9Sstevel@tonic-gate  *	This product includes software developed by the University of
21*7c478bd9Sstevel@tonic-gate  *	California, Berkeley and its contributors.
22*7c478bd9Sstevel@tonic-gate  * 4. Neither the name of the University nor the names of its contributors
23*7c478bd9Sstevel@tonic-gate  *    may be used to endorse or promote products derived from this software
24*7c478bd9Sstevel@tonic-gate  *    without specific prior written permission.
25*7c478bd9Sstevel@tonic-gate  *
26*7c478bd9Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27*7c478bd9Sstevel@tonic-gate  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28*7c478bd9Sstevel@tonic-gate  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29*7c478bd9Sstevel@tonic-gate  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30*7c478bd9Sstevel@tonic-gate  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31*7c478bd9Sstevel@tonic-gate  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32*7c478bd9Sstevel@tonic-gate  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33*7c478bd9Sstevel@tonic-gate  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34*7c478bd9Sstevel@tonic-gate  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35*7c478bd9Sstevel@tonic-gate  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36*7c478bd9Sstevel@tonic-gate  * SUCH DAMAGE.
37*7c478bd9Sstevel@tonic-gate  */
38*7c478bd9Sstevel@tonic-gate 
39*7c478bd9Sstevel@tonic-gate /*
40*7c478bd9Sstevel@tonic-gate  * Portions Copyright (c) 1996-1999 by Internet Software Consortium.
41*7c478bd9Sstevel@tonic-gate  *
42*7c478bd9Sstevel@tonic-gate  * Permission to use, copy, modify, and distribute this software for any
43*7c478bd9Sstevel@tonic-gate  * purpose with or without fee is hereby granted, provided that the above
44*7c478bd9Sstevel@tonic-gate  * copyright notice and this permission notice appear in all copies.
45*7c478bd9Sstevel@tonic-gate  *
46*7c478bd9Sstevel@tonic-gate  * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
47*7c478bd9Sstevel@tonic-gate  * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
48*7c478bd9Sstevel@tonic-gate  * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
49*7c478bd9Sstevel@tonic-gate  * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
50*7c478bd9Sstevel@tonic-gate  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
51*7c478bd9Sstevel@tonic-gate  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
52*7c478bd9Sstevel@tonic-gate  * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
53*7c478bd9Sstevel@tonic-gate  * SOFTWARE.
54*7c478bd9Sstevel@tonic-gate  */
55*7c478bd9Sstevel@tonic-gate 
56*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
57*7c478bd9Sstevel@tonic-gate 
58*7c478bd9Sstevel@tonic-gate #if defined(LIBC_SCCS) && !defined(lint)
59*7c478bd9Sstevel@tonic-gate static const char rcsid[] = "$Id: lcl_sv.c,v 1.22 2001/06/18 14:43:59 marka Exp $";
60*7c478bd9Sstevel@tonic-gate #endif /* LIBC_SCCS and not lint */
61*7c478bd9Sstevel@tonic-gate 
62*7c478bd9Sstevel@tonic-gate /* extern */
63*7c478bd9Sstevel@tonic-gate 
64*7c478bd9Sstevel@tonic-gate #include "port_before.h"
65*7c478bd9Sstevel@tonic-gate 
66*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
67*7c478bd9Sstevel@tonic-gate #include <sys/socket.h>
68*7c478bd9Sstevel@tonic-gate #include <netinet/in.h>
69*7c478bd9Sstevel@tonic-gate #include <arpa/nameser.h>
70*7c478bd9Sstevel@tonic-gate #include <resolv.h>
71*7c478bd9Sstevel@tonic-gate 
72*7c478bd9Sstevel@tonic-gate #ifdef IRS_LCL_SV_DB
73*7c478bd9Sstevel@tonic-gate #include <db.h>
74*7c478bd9Sstevel@tonic-gate #endif
75*7c478bd9Sstevel@tonic-gate #include <errno.h>
76*7c478bd9Sstevel@tonic-gate #include <fcntl.h>
77*7c478bd9Sstevel@tonic-gate #include <limits.h>
78*7c478bd9Sstevel@tonic-gate #include <stdio.h>
79*7c478bd9Sstevel@tonic-gate #include <string.h>
80*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
81*7c478bd9Sstevel@tonic-gate 
82*7c478bd9Sstevel@tonic-gate #include <irs.h>
83*7c478bd9Sstevel@tonic-gate #include <isc/memcluster.h>
84*7c478bd9Sstevel@tonic-gate 
85*7c478bd9Sstevel@tonic-gate #include "port_after.h"
86*7c478bd9Sstevel@tonic-gate 
87*7c478bd9Sstevel@tonic-gate #include "irs_p.h"
88*7c478bd9Sstevel@tonic-gate #include "lcl_p.h"
89*7c478bd9Sstevel@tonic-gate 
90*7c478bd9Sstevel@tonic-gate #ifdef SPRINTF_CHAR
91*7c478bd9Sstevel@tonic-gate # define SPRINTF(x) strlen(sprintf/**/x)
92*7c478bd9Sstevel@tonic-gate #else
93*7c478bd9Sstevel@tonic-gate # define SPRINTF(x) ((size_t)sprintf x)
94*7c478bd9Sstevel@tonic-gate #endif
95*7c478bd9Sstevel@tonic-gate 
96*7c478bd9Sstevel@tonic-gate /* Types */
97*7c478bd9Sstevel@tonic-gate 
98*7c478bd9Sstevel@tonic-gate struct pvt {
99*7c478bd9Sstevel@tonic-gate #ifdef IRS_LCL_SV_DB
100*7c478bd9Sstevel@tonic-gate 	DB *		dbh;
101*7c478bd9Sstevel@tonic-gate 	int		dbf;
102*7c478bd9Sstevel@tonic-gate #endif
103*7c478bd9Sstevel@tonic-gate 	struct lcl_sv	sv;
104*7c478bd9Sstevel@tonic-gate };
105*7c478bd9Sstevel@tonic-gate 
106*7c478bd9Sstevel@tonic-gate /* Forward */
107*7c478bd9Sstevel@tonic-gate 
108*7c478bd9Sstevel@tonic-gate static void			sv_close(struct irs_sv*);
109*7c478bd9Sstevel@tonic-gate static struct servent *		sv_next(struct irs_sv *);
110*7c478bd9Sstevel@tonic-gate static struct servent *		sv_byname(struct irs_sv *, const char *,
111*7c478bd9Sstevel@tonic-gate 					  const char *);
112*7c478bd9Sstevel@tonic-gate static struct servent *		sv_byport(struct irs_sv *, int, const char *);
113*7c478bd9Sstevel@tonic-gate static void			sv_rewind(struct irs_sv *);
114*7c478bd9Sstevel@tonic-gate static void			sv_minimize(struct irs_sv *);
115*7c478bd9Sstevel@tonic-gate /*global*/ struct servent *	irs_lclsv_fnxt(struct lcl_sv *);
116*7c478bd9Sstevel@tonic-gate #ifdef IRS_LCL_SV_DB
117*7c478bd9Sstevel@tonic-gate static struct servent *		sv_db_rec(struct lcl_sv *, DBT *, DBT *);
118*7c478bd9Sstevel@tonic-gate #endif
119*7c478bd9Sstevel@tonic-gate 
120*7c478bd9Sstevel@tonic-gate /* Portability */
121*7c478bd9Sstevel@tonic-gate 
122*7c478bd9Sstevel@tonic-gate #ifndef SEEK_SET
123*7c478bd9Sstevel@tonic-gate # define SEEK_SET 0
124*7c478bd9Sstevel@tonic-gate #endif
125*7c478bd9Sstevel@tonic-gate 
126*7c478bd9Sstevel@tonic-gate /* Public */
127*7c478bd9Sstevel@tonic-gate 
128*7c478bd9Sstevel@tonic-gate struct irs_sv *
129*7c478bd9Sstevel@tonic-gate irs_lcl_sv(struct irs_acc *this) {
130*7c478bd9Sstevel@tonic-gate 	struct irs_sv *sv;
131*7c478bd9Sstevel@tonic-gate 	struct pvt *pvt;
132*7c478bd9Sstevel@tonic-gate 
133*7c478bd9Sstevel@tonic-gate 	UNUSED(this);
134*7c478bd9Sstevel@tonic-gate 
135*7c478bd9Sstevel@tonic-gate 	if ((sv = memget(sizeof *sv)) == NULL) {
136*7c478bd9Sstevel@tonic-gate 		errno = ENOMEM;
137*7c478bd9Sstevel@tonic-gate 		return (NULL);
138*7c478bd9Sstevel@tonic-gate 	}
139*7c478bd9Sstevel@tonic-gate 	memset(sv, 0x5e, sizeof *sv);
140*7c478bd9Sstevel@tonic-gate 	if ((pvt = memget(sizeof *pvt)) == NULL) {
141*7c478bd9Sstevel@tonic-gate 		memput(sv, sizeof *sv);
142*7c478bd9Sstevel@tonic-gate 		errno = ENOMEM;
143*7c478bd9Sstevel@tonic-gate 		return (NULL);
144*7c478bd9Sstevel@tonic-gate 	}
145*7c478bd9Sstevel@tonic-gate 	memset(pvt, 0, sizeof *pvt);
146*7c478bd9Sstevel@tonic-gate 	sv->private = pvt;
147*7c478bd9Sstevel@tonic-gate 	sv->close = sv_close;
148*7c478bd9Sstevel@tonic-gate 	sv->next = sv_next;
149*7c478bd9Sstevel@tonic-gate 	sv->byname = sv_byname;
150*7c478bd9Sstevel@tonic-gate 	sv->byport = sv_byport;
151*7c478bd9Sstevel@tonic-gate 	sv->rewind = sv_rewind;
152*7c478bd9Sstevel@tonic-gate 	sv->minimize = sv_minimize;
153*7c478bd9Sstevel@tonic-gate 	sv->res_get = NULL;
154*7c478bd9Sstevel@tonic-gate 	sv->res_set = NULL;
155*7c478bd9Sstevel@tonic-gate #ifdef IRS_LCL_SV_DB
156*7c478bd9Sstevel@tonic-gate 	pvt->dbf = R_FIRST;
157*7c478bd9Sstevel@tonic-gate #endif
158*7c478bd9Sstevel@tonic-gate 	return (sv);
159*7c478bd9Sstevel@tonic-gate }
160*7c478bd9Sstevel@tonic-gate 
161*7c478bd9Sstevel@tonic-gate /* Methods */
162*7c478bd9Sstevel@tonic-gate 
163*7c478bd9Sstevel@tonic-gate static void
164*7c478bd9Sstevel@tonic-gate sv_close(struct irs_sv *this) {
165*7c478bd9Sstevel@tonic-gate 	struct pvt *pvt = (struct pvt *)this->private;
166*7c478bd9Sstevel@tonic-gate 
167*7c478bd9Sstevel@tonic-gate #ifdef IRS_LCL_SV_DB
168*7c478bd9Sstevel@tonic-gate 	if (pvt->dbh != NULL)
169*7c478bd9Sstevel@tonic-gate 		(*pvt->dbh->close)(pvt->dbh);
170*7c478bd9Sstevel@tonic-gate #endif
171*7c478bd9Sstevel@tonic-gate 	if (pvt->sv.fp)
172*7c478bd9Sstevel@tonic-gate 		fclose(pvt->sv.fp);
173*7c478bd9Sstevel@tonic-gate 	memput(pvt, sizeof *pvt);
174*7c478bd9Sstevel@tonic-gate 	memput(this, sizeof *this);
175*7c478bd9Sstevel@tonic-gate }
176*7c478bd9Sstevel@tonic-gate 
177*7c478bd9Sstevel@tonic-gate static struct servent *
178*7c478bd9Sstevel@tonic-gate sv_byname(struct irs_sv *this, const char *name, const char *proto) {
179*7c478bd9Sstevel@tonic-gate #ifdef IRS_LCL_SV_DB
180*7c478bd9Sstevel@tonic-gate 	struct pvt *pvt = (struct pvt *)this->private;
181*7c478bd9Sstevel@tonic-gate #endif
182*7c478bd9Sstevel@tonic-gate 	struct servent *p;
183*7c478bd9Sstevel@tonic-gate 	char **cp;
184*7c478bd9Sstevel@tonic-gate 
185*7c478bd9Sstevel@tonic-gate 	sv_rewind(this);
186*7c478bd9Sstevel@tonic-gate #ifdef IRS_LCL_SV_DB
187*7c478bd9Sstevel@tonic-gate 	if (pvt->dbh != NULL) {
188*7c478bd9Sstevel@tonic-gate 		DBT key, data;
189*7c478bd9Sstevel@tonic-gate 
190*7c478bd9Sstevel@tonic-gate 		/* Note that (sizeof "/") == 2. */
191*7c478bd9Sstevel@tonic-gate 		if ((strlen(name) + sizeof "/" + proto ? strlen(proto) : 0)
192*7c478bd9Sstevel@tonic-gate 		    > sizeof pvt->sv.line)
193*7c478bd9Sstevel@tonic-gate 			goto try_local;
194*7c478bd9Sstevel@tonic-gate 		key.data = pvt->sv.line;
195*7c478bd9Sstevel@tonic-gate 		key.size = SPRINTF((pvt->sv.line, "%s/%s", name,
196*7c478bd9Sstevel@tonic-gate 				    proto ? proto : "")) + 1;
197*7c478bd9Sstevel@tonic-gate 		if (proto != NULL) {
198*7c478bd9Sstevel@tonic-gate 			if ((*pvt->dbh->get)(pvt->dbh, &key, &data, 0) != 0)
199*7c478bd9Sstevel@tonic-gate 				return (NULL);
200*7c478bd9Sstevel@tonic-gate 		} else if ((*pvt->dbh->seq)(pvt->dbh, &key, &data, R_CURSOR)
201*7c478bd9Sstevel@tonic-gate 			   != 0)
202*7c478bd9Sstevel@tonic-gate 			return (NULL);
203*7c478bd9Sstevel@tonic-gate 		return (sv_db_rec(&pvt->sv, &key, &data));
204*7c478bd9Sstevel@tonic-gate 	}
205*7c478bd9Sstevel@tonic-gate  try_local:
206*7c478bd9Sstevel@tonic-gate #endif
207*7c478bd9Sstevel@tonic-gate 
208*7c478bd9Sstevel@tonic-gate 	while ((p = sv_next(this))) {
209*7c478bd9Sstevel@tonic-gate 		if (strcmp(name, p->s_name) == 0)
210*7c478bd9Sstevel@tonic-gate 			goto gotname;
211*7c478bd9Sstevel@tonic-gate 		for (cp = p->s_aliases; *cp; cp++)
212*7c478bd9Sstevel@tonic-gate 			if (strcmp(name, *cp) == 0)
213*7c478bd9Sstevel@tonic-gate 				goto gotname;
214*7c478bd9Sstevel@tonic-gate 		continue;
215*7c478bd9Sstevel@tonic-gate  gotname:
216*7c478bd9Sstevel@tonic-gate 		if (proto == NULL || strcmp(p->s_proto, proto) == 0)
217*7c478bd9Sstevel@tonic-gate 			break;
218*7c478bd9Sstevel@tonic-gate 	}
219*7c478bd9Sstevel@tonic-gate 	return (p);
220*7c478bd9Sstevel@tonic-gate }
221*7c478bd9Sstevel@tonic-gate 
222*7c478bd9Sstevel@tonic-gate static struct servent *
223*7c478bd9Sstevel@tonic-gate sv_byport(struct irs_sv *this, int port, const char *proto) {
224*7c478bd9Sstevel@tonic-gate #ifdef IRS_LCL_SV_DB
225*7c478bd9Sstevel@tonic-gate 	struct pvt *pvt = (struct pvt *)this->private;
226*7c478bd9Sstevel@tonic-gate #endif
227*7c478bd9Sstevel@tonic-gate 	struct servent *p;
228*7c478bd9Sstevel@tonic-gate 
229*7c478bd9Sstevel@tonic-gate 	sv_rewind(this);
230*7c478bd9Sstevel@tonic-gate #ifdef IRS_LCL_SV_DB
231*7c478bd9Sstevel@tonic-gate 	if (pvt->dbh != NULL) {
232*7c478bd9Sstevel@tonic-gate 		DBT key, data;
233*7c478bd9Sstevel@tonic-gate 		u_short *ports;
234*7c478bd9Sstevel@tonic-gate 
235*7c478bd9Sstevel@tonic-gate 		ports = (u_short *)pvt->sv.line;
236*7c478bd9Sstevel@tonic-gate 		ports[0] = 0;
237*7c478bd9Sstevel@tonic-gate 		ports[1] = port;
238*7c478bd9Sstevel@tonic-gate 		key.data = ports;
239*7c478bd9Sstevel@tonic-gate 		key.size = sizeof(u_short) * 2;
240*7c478bd9Sstevel@tonic-gate 		if (proto && *proto) {
241*7c478bd9Sstevel@tonic-gate 			strncpy((char *)ports + key.size, proto,
242*7c478bd9Sstevel@tonic-gate 				BUFSIZ - key.size);
243*7c478bd9Sstevel@tonic-gate 			key.size += strlen((char *)ports + key.size) + 1;
244*7c478bd9Sstevel@tonic-gate 			if ((*pvt->dbh->get)(pvt->dbh, &key, &data, 0) != 0)
245*7c478bd9Sstevel@tonic-gate 				return (NULL);
246*7c478bd9Sstevel@tonic-gate 		} else {
247*7c478bd9Sstevel@tonic-gate 			if ((*pvt->dbh->seq)(pvt->dbh, &key, &data, R_CURSOR)
248*7c478bd9Sstevel@tonic-gate 			    != 0)
249*7c478bd9Sstevel@tonic-gate 				return (NULL);
250*7c478bd9Sstevel@tonic-gate 		}
251*7c478bd9Sstevel@tonic-gate 		return (sv_db_rec(&pvt->sv, &key, &data));
252*7c478bd9Sstevel@tonic-gate 	}
253*7c478bd9Sstevel@tonic-gate #endif
254*7c478bd9Sstevel@tonic-gate 	while ((p = sv_next(this))) {
255*7c478bd9Sstevel@tonic-gate 		if (p->s_port != port)
256*7c478bd9Sstevel@tonic-gate 			continue;
257*7c478bd9Sstevel@tonic-gate 		if (proto == NULL || strcmp(p->s_proto, proto) == 0)
258*7c478bd9Sstevel@tonic-gate 			break;
259*7c478bd9Sstevel@tonic-gate 	}
260*7c478bd9Sstevel@tonic-gate 	return (p);
261*7c478bd9Sstevel@tonic-gate }
262*7c478bd9Sstevel@tonic-gate 
263*7c478bd9Sstevel@tonic-gate static void
264*7c478bd9Sstevel@tonic-gate sv_rewind(struct irs_sv *this) {
265*7c478bd9Sstevel@tonic-gate 	struct pvt *pvt = (struct pvt *)this->private;
266*7c478bd9Sstevel@tonic-gate 
267*7c478bd9Sstevel@tonic-gate 	if (pvt->sv.fp) {
268*7c478bd9Sstevel@tonic-gate 		if (fseek(pvt->sv.fp, 0L, SEEK_SET) == 0)
269*7c478bd9Sstevel@tonic-gate 			return;
270*7c478bd9Sstevel@tonic-gate 		(void)fclose(pvt->sv.fp);
271*7c478bd9Sstevel@tonic-gate 		pvt->sv.fp = NULL;
272*7c478bd9Sstevel@tonic-gate 	}
273*7c478bd9Sstevel@tonic-gate #ifdef IRS_LCL_SV_DB
274*7c478bd9Sstevel@tonic-gate 	pvt->dbf = R_FIRST;
275*7c478bd9Sstevel@tonic-gate 	if (pvt->dbh != NULL)
276*7c478bd9Sstevel@tonic-gate 		return;
277*7c478bd9Sstevel@tonic-gate 	pvt->dbh = dbopen(_PATH_SERVICES_DB, O_RDONLY,O_RDONLY,DB_BTREE, NULL);
278*7c478bd9Sstevel@tonic-gate 	if (pvt->dbh != NULL) {
279*7c478bd9Sstevel@tonic-gate 		if (fcntl((*pvt->dbh->fd)(pvt->dbh), F_SETFD, 1) < 0) {
280*7c478bd9Sstevel@tonic-gate 			(*pvt->dbh->close)(pvt->dbh);
281*7c478bd9Sstevel@tonic-gate 			pvt->dbh = NULL;
282*7c478bd9Sstevel@tonic-gate 		}
283*7c478bd9Sstevel@tonic-gate 		return;
284*7c478bd9Sstevel@tonic-gate 	}
285*7c478bd9Sstevel@tonic-gate #endif
286*7c478bd9Sstevel@tonic-gate 	if ((pvt->sv.fp = fopen(_PATH_SERVICES, "r")) == NULL)
287*7c478bd9Sstevel@tonic-gate 		return;
288*7c478bd9Sstevel@tonic-gate 	if (fcntl(fileno(pvt->sv.fp), F_SETFD, 1) < 0) {
289*7c478bd9Sstevel@tonic-gate 		(void)fclose(pvt->sv.fp);
290*7c478bd9Sstevel@tonic-gate 		pvt->sv.fp = NULL;
291*7c478bd9Sstevel@tonic-gate 	}
292*7c478bd9Sstevel@tonic-gate }
293*7c478bd9Sstevel@tonic-gate 
294*7c478bd9Sstevel@tonic-gate static struct servent *
295*7c478bd9Sstevel@tonic-gate sv_next(struct irs_sv *this) {
296*7c478bd9Sstevel@tonic-gate 	struct pvt *pvt = (struct pvt *)this->private;
297*7c478bd9Sstevel@tonic-gate 
298*7c478bd9Sstevel@tonic-gate #ifdef IRS_LCL_SV_DB
299*7c478bd9Sstevel@tonic-gate 	if (pvt->dbh == NULL && pvt->sv.fp == NULL)
300*7c478bd9Sstevel@tonic-gate #else
301*7c478bd9Sstevel@tonic-gate 	if (pvt->sv.fp == NULL)
302*7c478bd9Sstevel@tonic-gate #endif
303*7c478bd9Sstevel@tonic-gate 		sv_rewind(this);
304*7c478bd9Sstevel@tonic-gate 
305*7c478bd9Sstevel@tonic-gate #ifdef IRS_LCL_SV_DB
306*7c478bd9Sstevel@tonic-gate 	if (pvt->dbh != NULL) {
307*7c478bd9Sstevel@tonic-gate 		DBT key, data;
308*7c478bd9Sstevel@tonic-gate 
309*7c478bd9Sstevel@tonic-gate 		while ((*pvt->dbh->seq)(pvt->dbh, &key, &data, pvt->dbf) == 0){
310*7c478bd9Sstevel@tonic-gate 			pvt->dbf = R_NEXT;
311*7c478bd9Sstevel@tonic-gate 			if (((char *)key.data)[0])
312*7c478bd9Sstevel@tonic-gate 				continue;
313*7c478bd9Sstevel@tonic-gate 			return (sv_db_rec(&pvt->sv, &key, &data));
314*7c478bd9Sstevel@tonic-gate 		}
315*7c478bd9Sstevel@tonic-gate 	}
316*7c478bd9Sstevel@tonic-gate #endif
317*7c478bd9Sstevel@tonic-gate 
318*7c478bd9Sstevel@tonic-gate 	if (pvt->sv.fp == NULL)
319*7c478bd9Sstevel@tonic-gate 		return (NULL);
320*7c478bd9Sstevel@tonic-gate 	return (irs_lclsv_fnxt(&pvt->sv));
321*7c478bd9Sstevel@tonic-gate }
322*7c478bd9Sstevel@tonic-gate 
323*7c478bd9Sstevel@tonic-gate static void
324*7c478bd9Sstevel@tonic-gate sv_minimize(struct irs_sv *this) {
325*7c478bd9Sstevel@tonic-gate 	struct pvt *pvt = (struct pvt *)this->private;
326*7c478bd9Sstevel@tonic-gate 
327*7c478bd9Sstevel@tonic-gate #ifdef IRS_LCL_SV_DB
328*7c478bd9Sstevel@tonic-gate 	if (pvt->dbh != NULL) {
329*7c478bd9Sstevel@tonic-gate 		(*pvt->dbh->close)(pvt->dbh);
330*7c478bd9Sstevel@tonic-gate 		pvt->dbh = NULL;
331*7c478bd9Sstevel@tonic-gate 	}
332*7c478bd9Sstevel@tonic-gate #endif
333*7c478bd9Sstevel@tonic-gate 	if (pvt->sv.fp != NULL) {
334*7c478bd9Sstevel@tonic-gate 		(void)fclose(pvt->sv.fp);
335*7c478bd9Sstevel@tonic-gate 		pvt->sv.fp = NULL;
336*7c478bd9Sstevel@tonic-gate 	}
337*7c478bd9Sstevel@tonic-gate }
338*7c478bd9Sstevel@tonic-gate 
339*7c478bd9Sstevel@tonic-gate /* Quasipublic. */
340*7c478bd9Sstevel@tonic-gate 
341*7c478bd9Sstevel@tonic-gate struct servent *
342*7c478bd9Sstevel@tonic-gate irs_lclsv_fnxt(struct lcl_sv *sv) {
343*7c478bd9Sstevel@tonic-gate 	char *p, *cp, **q;
344*7c478bd9Sstevel@tonic-gate 
345*7c478bd9Sstevel@tonic-gate  again:
346*7c478bd9Sstevel@tonic-gate 	if ((p = fgets(sv->line, BUFSIZ, sv->fp)) == NULL)
347*7c478bd9Sstevel@tonic-gate 		return (NULL);
348*7c478bd9Sstevel@tonic-gate 	if (*p == '#')
349*7c478bd9Sstevel@tonic-gate 		goto again;
350*7c478bd9Sstevel@tonic-gate 	sv->serv.s_name = p;
351*7c478bd9Sstevel@tonic-gate 	while (*p && *p != '\n' && *p != ' ' && *p != '\t' && *p != '#')
352*7c478bd9Sstevel@tonic-gate 		++p;
353*7c478bd9Sstevel@tonic-gate 	if (*p == '\0' || *p == '#' || *p == '\n')
354*7c478bd9Sstevel@tonic-gate 		goto again;
355*7c478bd9Sstevel@tonic-gate 	*p++ = '\0';
356*7c478bd9Sstevel@tonic-gate 	while (*p == ' ' || *p == '\t')
357*7c478bd9Sstevel@tonic-gate 		p++;
358*7c478bd9Sstevel@tonic-gate 	if (*p == '\0' || *p == '#' || *p == '\n')
359*7c478bd9Sstevel@tonic-gate 		goto again;
360*7c478bd9Sstevel@tonic-gate 	sv->serv.s_port = htons((u_short)strtol(p, &cp, 10));
361*7c478bd9Sstevel@tonic-gate 	if (cp == p || (*cp != '/' && *cp != ','))
362*7c478bd9Sstevel@tonic-gate 		goto again;
363*7c478bd9Sstevel@tonic-gate 	p = cp + 1;
364*7c478bd9Sstevel@tonic-gate 	sv->serv.s_proto = p;
365*7c478bd9Sstevel@tonic-gate 
366*7c478bd9Sstevel@tonic-gate 	q = sv->serv.s_aliases = sv->serv_aliases;
367*7c478bd9Sstevel@tonic-gate 
368*7c478bd9Sstevel@tonic-gate 	while (*p && *p != '\n' && *p != ' ' && *p != '\t' && *p != '#')
369*7c478bd9Sstevel@tonic-gate 		++p;
370*7c478bd9Sstevel@tonic-gate 
371*7c478bd9Sstevel@tonic-gate 	while (*p == ' ' || *p == '\t') {
372*7c478bd9Sstevel@tonic-gate 		*p++ = '\0';
373*7c478bd9Sstevel@tonic-gate 		while (*p == ' ' || *p == '\t')
374*7c478bd9Sstevel@tonic-gate 			++p;
375*7c478bd9Sstevel@tonic-gate 		if (*p == '\0' || *p == '#' || *p == '\n')
376*7c478bd9Sstevel@tonic-gate 			break;
377*7c478bd9Sstevel@tonic-gate 		if (q < &sv->serv_aliases[IRS_SV_MAXALIASES - 1])
378*7c478bd9Sstevel@tonic-gate 			*q++ = p;
379*7c478bd9Sstevel@tonic-gate 		while (*p && *p != '\n' && *p != ' ' && *p != '\t' && *p != '#')
380*7c478bd9Sstevel@tonic-gate 			++p;
381*7c478bd9Sstevel@tonic-gate 	}
382*7c478bd9Sstevel@tonic-gate 
383*7c478bd9Sstevel@tonic-gate 	*p = '\0';
384*7c478bd9Sstevel@tonic-gate 	*q = NULL;
385*7c478bd9Sstevel@tonic-gate 	return (&sv->serv);
386*7c478bd9Sstevel@tonic-gate }
387*7c478bd9Sstevel@tonic-gate 
388*7c478bd9Sstevel@tonic-gate /* Private. */
389*7c478bd9Sstevel@tonic-gate 
390*7c478bd9Sstevel@tonic-gate #ifdef IRS_LCL_SV_DB
391*7c478bd9Sstevel@tonic-gate static struct servent *
392*7c478bd9Sstevel@tonic-gate sv_db_rec(struct lcl_sv *sv, DBT *key, DBT *data) {
393*7c478bd9Sstevel@tonic-gate 	char *p, **q;
394*7c478bd9Sstevel@tonic-gate 	int n;
395*7c478bd9Sstevel@tonic-gate 
396*7c478bd9Sstevel@tonic-gate 	p = data->data;
397*7c478bd9Sstevel@tonic-gate 	p[data->size - 1] = '\0';	/* should be, but we depend on it */
398*7c478bd9Sstevel@tonic-gate 
399*7c478bd9Sstevel@tonic-gate 	if (((char *)key->data)[0] == '\0') {
400*7c478bd9Sstevel@tonic-gate 		if (key->size < sizeof(u_short)*2 || data->size < 2)
401*7c478bd9Sstevel@tonic-gate 			return (NULL);
402*7c478bd9Sstevel@tonic-gate 		sv->serv.s_port = ((u_short *)key->data)[1];
403*7c478bd9Sstevel@tonic-gate 		n = strlen(p) + 1;
404*7c478bd9Sstevel@tonic-gate 		if ((size_t)n > sizeof(sv->line)) {
405*7c478bd9Sstevel@tonic-gate 			n = sizeof(sv->line);
406*7c478bd9Sstevel@tonic-gate 		}
407*7c478bd9Sstevel@tonic-gate 		memcpy(sv->line, p, n);
408*7c478bd9Sstevel@tonic-gate 		sv->serv.s_name = sv->line;
409*7c478bd9Sstevel@tonic-gate 		if ((sv->serv.s_proto = strchr(sv->line, '/')) != NULL)
410*7c478bd9Sstevel@tonic-gate 			*(sv->serv.s_proto)++ = '\0';
411*7c478bd9Sstevel@tonic-gate 		p += n;
412*7c478bd9Sstevel@tonic-gate 		data->size -= n;
413*7c478bd9Sstevel@tonic-gate 	} else {
414*7c478bd9Sstevel@tonic-gate 		if (data->size < sizeof(u_short) + 1)
415*7c478bd9Sstevel@tonic-gate 			return (NULL);
416*7c478bd9Sstevel@tonic-gate 		if (key->size > sizeof(sv->line))
417*7c478bd9Sstevel@tonic-gate 			key->size = sizeof(sv->line);
418*7c478bd9Sstevel@tonic-gate 		((char *)key->data)[key->size - 1] = '\0';
419*7c478bd9Sstevel@tonic-gate 		memcpy(sv->line, key->data, key->size);
420*7c478bd9Sstevel@tonic-gate 		sv->serv.s_name = sv->line;
421*7c478bd9Sstevel@tonic-gate 		if ((sv->serv.s_proto = strchr(sv->line, '/')) != NULL)
422*7c478bd9Sstevel@tonic-gate 			*(sv->serv.s_proto)++ = '\0';
423*7c478bd9Sstevel@tonic-gate 		sv->serv.s_port = *(u_short *)data->data;
424*7c478bd9Sstevel@tonic-gate 		p += sizeof(u_short);
425*7c478bd9Sstevel@tonic-gate 		data->size -= sizeof(u_short);
426*7c478bd9Sstevel@tonic-gate 	}
427*7c478bd9Sstevel@tonic-gate 	q = sv->serv.s_aliases = sv->serv_aliases;
428*7c478bd9Sstevel@tonic-gate 	while (data->size > 0 && q < &sv->serv_aliases[IRS_SV_MAXALIASES - 1]) {
429*7c478bd9Sstevel@tonic-gate 
430*7c478bd9Sstevel@tonic-gate 		*q++ = p;
431*7c478bd9Sstevel@tonic-gate 		n = strlen(p) + 1;
432*7c478bd9Sstevel@tonic-gate 		data->size -= n;
433*7c478bd9Sstevel@tonic-gate 		p += n;
434*7c478bd9Sstevel@tonic-gate 	}
435*7c478bd9Sstevel@tonic-gate 	*q = NULL;
436*7c478bd9Sstevel@tonic-gate 	return (&sv->serv);
437*7c478bd9Sstevel@tonic-gate }
438*7c478bd9Sstevel@tonic-gate #endif
439