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