getnetbyht.c revision 146904
1/*
2 * Copyright (c) 1983, 1993
3 *	The Regents of the University of California.  All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 *    must display the following acknowledgement:
15 *	This product includes software developed by the University of
16 *	California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 *    may be used to endorse or promote products derived from this software
19 *    without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34/* Portions Copyright (c) 1993 Carlos Leandro and Rui Salgueiro
35 *	Dep. Matematica Universidade de Coimbra, Portugal, Europe
36 *
37 * Permission to use, copy, modify, and distribute this software for any
38 * purpose with or without fee is hereby granted, provided that the above
39 * copyright notice and this permission notice appear in all copies.
40 *
41 * from getnetent.c	1.1 (Coimbra) 93/06/02
42 */
43
44#if defined(LIBC_SCCS) && !defined(lint)
45static char sccsid[] = "@(#)getnetent.c	8.1 (Berkeley) 6/4/93";
46static char orig_rcsid[] = "From: Id: getnetent.c,v 8.4 1997/06/01 20:34:37 vixie Exp";
47#endif /* LIBC_SCCS and not lint */
48#include <sys/cdefs.h>
49__FBSDID("$FreeBSD: head/lib/libc/net/getnetbyht.c 146904 2005-06-03 03:32:06Z ume $");
50
51#include <sys/types.h>
52#include <sys/socket.h>
53#include <netinet/in.h>
54#include <arpa/inet.h>
55#include <arpa/nameser.h>
56#include <netdb.h>
57#include <stdio.h>
58#include <string.h>
59#include <stdarg.h>
60#include <nsswitch.h>
61#include "netdb_private.h"
62
63void
64_setnethtent(int f, struct netent_data *ned)
65{
66
67	if (ned->netf == NULL)
68		ned->netf = fopen(_PATH_NETWORKS, "r");
69	else
70		rewind(ned->netf);
71	ned->stayopen |= f;
72}
73
74void
75_endnethtent(struct netent_data *ned)
76{
77
78	if (ned->netf) {
79		fclose(ned->netf);
80		ned->netf = NULL;
81	}
82	ned->stayopen = 0;
83}
84
85int
86getnetent_r(struct netent *ne, struct netent_data *ned)
87{
88	char *p, *bp, *ep;
89	char *cp, **q;
90	int len;
91	char line[BUFSIZ + 1];
92
93	if (ned->netf == NULL &&
94	    (ned->netf = fopen(_PATH_NETWORKS, "r")) == NULL)
95		return -1;
96again:
97	p = fgets(line, sizeof line, ned->netf);
98	if (p == NULL)
99		return -1;
100	if (*p == '#')
101		goto again;
102	cp = strpbrk(p, "#\n");
103	if (cp != NULL)
104		*cp = '\0';
105	bp = ned->netbuf;
106	ep = ned->netbuf + sizeof ned->netbuf;
107	ne->n_name = bp;
108	cp = strpbrk(p, " \t");
109	if (cp == NULL)
110		goto again;
111	*cp++ = '\0';
112	len = strlen(p) + 1;
113	if (ep - bp < len) {
114		h_errno = NO_RECOVERY;
115		return -1;
116	}
117	strlcpy(bp, p, ep - bp);
118	bp += len;
119	while (*cp == ' ' || *cp == '\t')
120		cp++;
121	p = strpbrk(cp, " \t");
122	if (p != NULL)
123		*p++ = '\0';
124	ne->n_net = inet_network(cp);
125	ne->n_addrtype = AF_INET;
126	q = ne->n_aliases = ned->net_aliases;
127	if (p != NULL) {
128		cp = p;
129		while (cp && *cp) {
130			if (*cp == ' ' || *cp == '\t') {
131				cp++;
132				continue;
133			}
134			if (q >= &ned->net_aliases[_MAXALIASES - 1])
135				break;
136			p = strpbrk(cp, " \t");
137			if (p != NULL)
138				*p++ = '\0';
139			len = strlen(cp) + 1;
140			if (ep - bp < len)
141				break;
142			strlcpy(bp, cp, ep - bp);
143			*q++ = bp;
144			bp += len;
145			cp = p;
146		}
147	}
148	*q = NULL;
149	return 0;
150}
151
152struct netent *
153getnetent(void)
154{
155	struct netdata *nd;
156
157	if ((nd = __netdata_init()) == NULL)
158		return NULL;
159	if (getnetent_r(&nd->net, &nd->data) != 0)
160		return NULL;
161	return &nd->net;
162}
163
164int
165_ht_getnetbyname(void *rval, void *cb_data, va_list ap)
166{
167	const char *name;
168	struct netent *ne;
169	struct netent_data *ned;
170	char **cp;
171	int error;
172
173	name = va_arg(ap, const char *);
174	ne = va_arg(ap, struct netent *);
175	ned = va_arg(ap, struct netent_data *);
176
177	setnetent_r(ned->stayopen, ned);
178	while ((error = getnetent_r(ne, ned)) == 0) {
179		if (strcasecmp(ne->n_name, name) == 0)
180			break;
181		for (cp = ne->n_aliases; *cp != 0; cp++)
182			if (strcasecmp(*cp, name) == 0)
183				goto found;
184	}
185found:
186	if (!ned->stayopen)
187		endnetent_r(ned);
188	return (error == 0) ? NS_SUCCESS : NS_NOTFOUND;
189}
190
191int
192_ht_getnetbyaddr(void *rval, void *cb_data, va_list ap)
193{
194	uint32_t net;
195	int type;
196	struct netent *ne;
197	struct netent_data *ned;
198	int error;
199
200	net = va_arg(ap, uint32_t);
201	type = va_arg(ap, int);
202	ne = va_arg(ap, struct netent *);
203	ned = va_arg(ap, struct netent_data *);
204
205	setnetent_r(ned->stayopen, ned);
206	while ((error = getnetent_r(ne, ned)) == 0)
207		if (ne->n_addrtype == type && ne->n_net == net)
208			break;
209	if (!ned->stayopen)
210		endnetent_r(ned);
211	return (error == 0) ? NS_SUCCESS : NS_NOTFOUND;
212}
213