xref: /freebsd/lib/libc/net/getnetbyht.c (revision 8fb3f3f68288ae2b1b53dd65e3dd673d83c80f4c)
11363f04cSPaul Traina /*
21363f04cSPaul Traina  * Copyright (c) 1983, 1993
31363f04cSPaul Traina  *	The Regents of the University of California.  All rights reserved.
41363f04cSPaul Traina  *
51363f04cSPaul Traina  * Redistribution and use in source and binary forms, with or without
61363f04cSPaul Traina  * modification, are permitted provided that the following conditions
71363f04cSPaul Traina  * are met:
81363f04cSPaul Traina  * 1. Redistributions of source code must retain the above copyright
91363f04cSPaul Traina  *    notice, this list of conditions and the following disclaimer.
101363f04cSPaul Traina  * 2. Redistributions in binary form must reproduce the above copyright
111363f04cSPaul Traina  *    notice, this list of conditions and the following disclaimer in the
121363f04cSPaul Traina  *    documentation and/or other materials provided with the distribution.
131363f04cSPaul Traina  * 3. All advertising materials mentioning features or use of this software
141363f04cSPaul Traina  *    must display the following acknowledgement:
151363f04cSPaul Traina  *	This product includes software developed by the University of
161363f04cSPaul Traina  *	California, Berkeley and its contributors.
171363f04cSPaul Traina  * 4. Neither the name of the University nor the names of its contributors
181363f04cSPaul Traina  *    may be used to endorse or promote products derived from this software
191363f04cSPaul Traina  *    without specific prior written permission.
201363f04cSPaul Traina  *
211363f04cSPaul Traina  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
221363f04cSPaul Traina  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
231363f04cSPaul Traina  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
241363f04cSPaul Traina  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
251363f04cSPaul Traina  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
261363f04cSPaul Traina  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
271363f04cSPaul Traina  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
281363f04cSPaul Traina  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
291363f04cSPaul Traina  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
301363f04cSPaul Traina  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
311363f04cSPaul Traina  * SUCH DAMAGE.
321363f04cSPaul Traina  */
331363f04cSPaul Traina 
344faad310SPeter Wemm /* Portions Copyright (c) 1993 Carlos Leandro and Rui Salgueiro
354faad310SPeter Wemm  *	Dep. Matematica Universidade de Coimbra, Portugal, Europe
364faad310SPeter Wemm  *
374faad310SPeter Wemm  * Permission to use, copy, modify, and distribute this software for any
384faad310SPeter Wemm  * purpose with or without fee is hereby granted, provided that the above
394faad310SPeter Wemm  * copyright notice and this permission notice appear in all copies.
404faad310SPeter Wemm  *
414faad310SPeter Wemm  * from getnetent.c	1.1 (Coimbra) 93/06/02
424faad310SPeter Wemm  */
434faad310SPeter Wemm 
441363f04cSPaul Traina #if defined(LIBC_SCCS) && !defined(lint)
451363f04cSPaul Traina static char sccsid[] = "@(#)getnetent.c	8.1 (Berkeley) 6/4/93";
466c5aff80SPeter Wemm static char orig_rcsid[] = "From: Id: getnetent.c,v 8.4 1997/06/01 20:34:37 vixie Exp";
477f3dea24SPeter Wemm static chat rcsid[] = "$FreeBSD$";
481363f04cSPaul Traina #endif /* LIBC_SCCS and not lint */
491363f04cSPaul Traina 
501363f04cSPaul Traina #include <sys/types.h>
511363f04cSPaul Traina #include <sys/socket.h>
521363f04cSPaul Traina #include <netinet/in.h>
531363f04cSPaul Traina #include <arpa/inet.h>
544faad310SPeter Wemm #include <arpa/nameser.h>
551363f04cSPaul Traina #include <netdb.h>
561363f04cSPaul Traina #include <stdio.h>
571363f04cSPaul Traina #include <string.h>
58248aee62SJacques Vidrine #include <stdarg.h>
59248aee62SJacques Vidrine #include <nsswitch.h>
601363f04cSPaul Traina 
611363f04cSPaul Traina #define	MAXALIASES	35
621363f04cSPaul Traina 
631363f04cSPaul Traina static FILE *netf;
641363f04cSPaul Traina static char line[BUFSIZ+1];
651363f04cSPaul Traina static struct netent net;
661363f04cSPaul Traina static char *net_aliases[MAXALIASES];
671363f04cSPaul Traina static int _net_stayopen;
681363f04cSPaul Traina 
691363f04cSPaul Traina void
701363f04cSPaul Traina _setnethtent(f)
711363f04cSPaul Traina 	int f;
721363f04cSPaul Traina {
734faad310SPeter Wemm 
741363f04cSPaul Traina 	if (netf == NULL)
751363f04cSPaul Traina 		netf = fopen(_PATH_NETWORKS, "r" );
761363f04cSPaul Traina 	else
771363f04cSPaul Traina 		rewind(netf);
781363f04cSPaul Traina 	_net_stayopen |= f;
791363f04cSPaul Traina }
801363f04cSPaul Traina 
811363f04cSPaul Traina void
821363f04cSPaul Traina _endnethtent()
831363f04cSPaul Traina {
844faad310SPeter Wemm 
851363f04cSPaul Traina 	if (netf) {
861363f04cSPaul Traina 		fclose(netf);
871363f04cSPaul Traina 		netf = NULL;
881363f04cSPaul Traina 	}
891363f04cSPaul Traina 	_net_stayopen = 0;
901363f04cSPaul Traina }
911363f04cSPaul Traina 
921363f04cSPaul Traina struct netent *
931363f04cSPaul Traina getnetent()
941363f04cSPaul Traina {
951363f04cSPaul Traina 	char *p;
968fb3f3f6SDavid E. O'Brien 	char *cp, **q;
971363f04cSPaul Traina 
981363f04cSPaul Traina 	if (netf == NULL && (netf = fopen(_PATH_NETWORKS, "r" )) == NULL)
991363f04cSPaul Traina 		return (NULL);
1001363f04cSPaul Traina again:
1016c5aff80SPeter Wemm 	p = fgets(line, sizeof line, netf);
1021363f04cSPaul Traina 	if (p == NULL)
1031363f04cSPaul Traina 		return (NULL);
1041363f04cSPaul Traina 	if (*p == '#')
1051363f04cSPaul Traina 		goto again;
1061363f04cSPaul Traina 	cp = strpbrk(p, "#\n");
1071363f04cSPaul Traina 	if (cp == NULL)
1081363f04cSPaul Traina 		goto again;
1091363f04cSPaul Traina 	*cp = '\0';
1101363f04cSPaul Traina 	net.n_name = p;
1111363f04cSPaul Traina 	cp = strpbrk(p, " \t");
1121363f04cSPaul Traina 	if (cp == NULL)
1131363f04cSPaul Traina 		goto again;
1141363f04cSPaul Traina 	*cp++ = '\0';
1151363f04cSPaul Traina 	while (*cp == ' ' || *cp == '\t')
1161363f04cSPaul Traina 		cp++;
1171363f04cSPaul Traina 	p = strpbrk(cp, " \t");
1181363f04cSPaul Traina 	if (p != NULL)
1191363f04cSPaul Traina 		*p++ = '\0';
1201363f04cSPaul Traina 	net.n_net = inet_network(cp);
1211363f04cSPaul Traina 	net.n_addrtype = AF_INET;
1221363f04cSPaul Traina 	q = net.n_aliases = net_aliases;
1231363f04cSPaul Traina 	if (p != NULL)
1241363f04cSPaul Traina 		cp = p;
1251363f04cSPaul Traina 	while (cp && *cp) {
1261363f04cSPaul Traina 		if (*cp == ' ' || *cp == '\t') {
1271363f04cSPaul Traina 			cp++;
1281363f04cSPaul Traina 			continue;
1291363f04cSPaul Traina 		}
1301363f04cSPaul Traina 		if (q < &net_aliases[MAXALIASES - 1])
1311363f04cSPaul Traina 			*q++ = cp;
1321363f04cSPaul Traina 		cp = strpbrk(cp, " \t");
1331363f04cSPaul Traina 		if (cp != NULL)
1341363f04cSPaul Traina 			*cp++ = '\0';
1351363f04cSPaul Traina 	}
1361363f04cSPaul Traina 	*q = NULL;
1371363f04cSPaul Traina 	return (&net);
1381363f04cSPaul Traina }
1391363f04cSPaul Traina 
140248aee62SJacques Vidrine int
141248aee62SJacques Vidrine _ht_getnetbyname(void *rval, void *cb_data, va_list ap)
1421363f04cSPaul Traina {
143248aee62SJacques Vidrine 	const char *name;
1448fb3f3f6SDavid E. O'Brien 	struct netent *p;
1458fb3f3f6SDavid E. O'Brien 	char **cp;
1461363f04cSPaul Traina 
147248aee62SJacques Vidrine 	name = va_arg(ap, const char *);
148248aee62SJacques Vidrine 
1491363f04cSPaul Traina 	setnetent(_net_stayopen);
15051295a4dSJordan K. Hubbard 	while ( (p = getnetent()) ) {
1511363f04cSPaul Traina 		if (strcasecmp(p->n_name, name) == 0)
1521363f04cSPaul Traina 			break;
1531363f04cSPaul Traina 		for (cp = p->n_aliases; *cp != 0; cp++)
1541363f04cSPaul Traina 			if (strcasecmp(*cp, name) == 0)
1551363f04cSPaul Traina 				goto found;
1561363f04cSPaul Traina 	}
1571363f04cSPaul Traina found:
1581363f04cSPaul Traina 	if (!_net_stayopen)
1591363f04cSPaul Traina 		endnetent();
160248aee62SJacques Vidrine 	*(struct netent **)rval = p;
161248aee62SJacques Vidrine 	return (p != NULL) ? NS_SUCCESS : NS_NOTFOUND;
1621363f04cSPaul Traina }
1631363f04cSPaul Traina 
164248aee62SJacques Vidrine int
165248aee62SJacques Vidrine _ht_getnetbyaddr(void *rval, void *cb_data, va_list ap)
1661363f04cSPaul Traina {
167248aee62SJacques Vidrine 	unsigned long net;
168248aee62SJacques Vidrine 	int type;
1698fb3f3f6SDavid E. O'Brien 	struct netent *p;
1701363f04cSPaul Traina 
171248aee62SJacques Vidrine 	net = va_arg(ap, unsigned long);
172248aee62SJacques Vidrine 	type = va_arg(ap, int);
173248aee62SJacques Vidrine 
1741363f04cSPaul Traina 	setnetent(_net_stayopen);
17551295a4dSJordan K. Hubbard 	while ( (p = getnetent()) )
1761363f04cSPaul Traina 		if (p->n_addrtype == type && p->n_net == net)
1771363f04cSPaul Traina 			break;
1781363f04cSPaul Traina 	if (!_net_stayopen)
1791363f04cSPaul Traina 		endnetent();
180248aee62SJacques Vidrine 	*(struct netent **)rval = p;
181248aee62SJacques Vidrine 	return (p != NULL) ? NS_SUCCESS : NS_NOTFOUND;
1821363f04cSPaul Traina }
183