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