xref: /onnv-gate/usr/src/lib/libresolv2/common/irs/getnetent_r.c (revision 11038:74b12212b8a2)
10Sstevel@tonic-gate /*
2*11038SRao.Shoaib@Sun.COM  * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
30Sstevel@tonic-gate  * Copyright (c) 1998-1999 by Internet Software Consortium.
40Sstevel@tonic-gate  *
50Sstevel@tonic-gate  * Permission to use, copy, modify, and distribute this software for any
60Sstevel@tonic-gate  * purpose with or without fee is hereby granted, provided that the above
70Sstevel@tonic-gate  * copyright notice and this permission notice appear in all copies.
80Sstevel@tonic-gate  *
9*11038SRao.Shoaib@Sun.COM  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
10*11038SRao.Shoaib@Sun.COM  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11*11038SRao.Shoaib@Sun.COM  * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
12*11038SRao.Shoaib@Sun.COM  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13*11038SRao.Shoaib@Sun.COM  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14*11038SRao.Shoaib@Sun.COM  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
15*11038SRao.Shoaib@Sun.COM  * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
160Sstevel@tonic-gate  */
170Sstevel@tonic-gate 
180Sstevel@tonic-gate #if defined(LIBC_SCCS) && !defined(lint)
19*11038SRao.Shoaib@Sun.COM static const char rcsid[] = "$Id: getnetent_r.c,v 1.6 2005/09/03 12:41:38 marka Exp $";
200Sstevel@tonic-gate #endif /* LIBC_SCCS and not lint */
210Sstevel@tonic-gate 
220Sstevel@tonic-gate #include <port_before.h>
230Sstevel@tonic-gate #if !defined(_REENTRANT) || !defined(DO_PTHREADS)
240Sstevel@tonic-gate 	static int getnetent_r_not_required = 0;
250Sstevel@tonic-gate #else
260Sstevel@tonic-gate #include <errno.h>
270Sstevel@tonic-gate #include <string.h>
280Sstevel@tonic-gate #include <stdio.h>
290Sstevel@tonic-gate #include <sys/types.h>
300Sstevel@tonic-gate #include <netinet/in.h>
310Sstevel@tonic-gate #include <netdb.h>
320Sstevel@tonic-gate #include <sys/param.h>
330Sstevel@tonic-gate #include <port_after.h>
340Sstevel@tonic-gate 
350Sstevel@tonic-gate #ifdef NET_R_RETURN
360Sstevel@tonic-gate 
370Sstevel@tonic-gate static NET_R_RETURN
380Sstevel@tonic-gate copy_netent(struct netent *, struct netent *, NET_R_COPY_ARGS);
390Sstevel@tonic-gate 
400Sstevel@tonic-gate NET_R_RETURN
getnetbyname_r(const char * name,struct netent * nptr,NET_R_ARGS)410Sstevel@tonic-gate getnetbyname_r(const char *name,  struct netent *nptr, NET_R_ARGS) {
420Sstevel@tonic-gate 	struct netent *ne = getnetbyname(name);
430Sstevel@tonic-gate #ifdef NET_R_SETANSWER
440Sstevel@tonic-gate 	int n = 0;
450Sstevel@tonic-gate 
460Sstevel@tonic-gate 	if (ne == NULL || (n = copy_netent(ne, nptr, NET_R_COPY)) != 0)
470Sstevel@tonic-gate 		*answerp = NULL;
480Sstevel@tonic-gate 	else
490Sstevel@tonic-gate 		*answerp = ne;
500Sstevel@tonic-gate 	if (ne == NULL)
510Sstevel@tonic-gate 		*h_errnop = h_errno;
520Sstevel@tonic-gate 	return (n);
530Sstevel@tonic-gate #else
540Sstevel@tonic-gate 	if (ne == NULL)
550Sstevel@tonic-gate 		return (NET_R_BAD);
560Sstevel@tonic-gate 
570Sstevel@tonic-gate 	return (copy_netent(ne, nptr, NET_R_COPY));
580Sstevel@tonic-gate #endif
590Sstevel@tonic-gate }
600Sstevel@tonic-gate 
610Sstevel@tonic-gate #ifndef GETNETBYADDR_ADDR_T
620Sstevel@tonic-gate #define GETNETBYADDR_ADDR_T long
630Sstevel@tonic-gate #endif
640Sstevel@tonic-gate NET_R_RETURN
getnetbyaddr_r(GETNETBYADDR_ADDR_T addr,int type,struct netent * nptr,NET_R_ARGS)650Sstevel@tonic-gate getnetbyaddr_r(GETNETBYADDR_ADDR_T addr, int type, struct netent *nptr, NET_R_ARGS) {
660Sstevel@tonic-gate 	struct netent *ne = getnetbyaddr(addr, type);
670Sstevel@tonic-gate #ifdef NET_R_SETANSWER
680Sstevel@tonic-gate 	int n = 0;
690Sstevel@tonic-gate 
700Sstevel@tonic-gate 	if (ne == NULL || (n = copy_netent(ne, nptr, NET_R_COPY)) != 0)
710Sstevel@tonic-gate 		*answerp = NULL;
720Sstevel@tonic-gate 	else
730Sstevel@tonic-gate 		*answerp = ne;
740Sstevel@tonic-gate 	if (ne == NULL)
750Sstevel@tonic-gate 		*h_errnop = h_errno;
760Sstevel@tonic-gate 	return (n);
770Sstevel@tonic-gate #else
780Sstevel@tonic-gate 
790Sstevel@tonic-gate 	if (ne == NULL)
800Sstevel@tonic-gate 		return (NET_R_BAD);
810Sstevel@tonic-gate 
820Sstevel@tonic-gate 	return (copy_netent(ne, nptr, NET_R_COPY));
830Sstevel@tonic-gate #endif
840Sstevel@tonic-gate }
850Sstevel@tonic-gate 
86*11038SRao.Shoaib@Sun.COM /*%
870Sstevel@tonic-gate  *	These assume a single context is in operation per thread.
880Sstevel@tonic-gate  *	If this is not the case we will need to call irs directly
890Sstevel@tonic-gate  *	rather than through the base functions.
900Sstevel@tonic-gate  */
910Sstevel@tonic-gate 
920Sstevel@tonic-gate NET_R_RETURN
getnetent_r(struct netent * nptr,NET_R_ARGS)930Sstevel@tonic-gate getnetent_r(struct netent *nptr, NET_R_ARGS) {
940Sstevel@tonic-gate 	struct netent *ne = getnetent();
950Sstevel@tonic-gate #ifdef NET_R_SETANSWER
960Sstevel@tonic-gate 	int n = 0;
970Sstevel@tonic-gate 
980Sstevel@tonic-gate 	if (ne == NULL || (n = copy_netent(ne, nptr, NET_R_COPY)) != 0)
990Sstevel@tonic-gate 		*answerp = NULL;
1000Sstevel@tonic-gate 	else
1010Sstevel@tonic-gate 		*answerp = ne;
1020Sstevel@tonic-gate 	if (ne == NULL)
1030Sstevel@tonic-gate 		*h_errnop = h_errno;
1040Sstevel@tonic-gate 	return (n);
1050Sstevel@tonic-gate #else
1060Sstevel@tonic-gate 
1070Sstevel@tonic-gate 	if (ne == NULL)
1080Sstevel@tonic-gate 		return (NET_R_BAD);
1090Sstevel@tonic-gate 
1100Sstevel@tonic-gate 	return (copy_netent(ne, nptr, NET_R_COPY));
1110Sstevel@tonic-gate #endif
1120Sstevel@tonic-gate }
1130Sstevel@tonic-gate 
1140Sstevel@tonic-gate NET_R_SET_RETURN
1150Sstevel@tonic-gate #ifdef NET_R_ENT_ARGS
setnetent_r(int stay_open,NET_R_ENT_ARGS)1160Sstevel@tonic-gate setnetent_r(int stay_open, NET_R_ENT_ARGS)
1170Sstevel@tonic-gate #else
1180Sstevel@tonic-gate setnetent_r(int stay_open)
1190Sstevel@tonic-gate #endif
1200Sstevel@tonic-gate {
121*11038SRao.Shoaib@Sun.COM #ifdef NET_R_ENT_ARGS
122*11038SRao.Shoaib@Sun.COM 	UNUSED(ndptr);
123*11038SRao.Shoaib@Sun.COM #endif
1240Sstevel@tonic-gate 	setnetent(stay_open);
1250Sstevel@tonic-gate #ifdef NET_R_SET_RESULT
1260Sstevel@tonic-gate 	return (NET_R_SET_RESULT);
1270Sstevel@tonic-gate #endif
1280Sstevel@tonic-gate }
1290Sstevel@tonic-gate 
1300Sstevel@tonic-gate NET_R_END_RETURN
1310Sstevel@tonic-gate #ifdef NET_R_ENT_ARGS
endnetent_r(NET_R_ENT_ARGS)1320Sstevel@tonic-gate endnetent_r(NET_R_ENT_ARGS)
1330Sstevel@tonic-gate #else
1340Sstevel@tonic-gate endnetent_r()
1350Sstevel@tonic-gate #endif
1360Sstevel@tonic-gate {
137*11038SRao.Shoaib@Sun.COM #ifdef NET_R_ENT_ARGS
138*11038SRao.Shoaib@Sun.COM 	UNUSED(ndptr);
139*11038SRao.Shoaib@Sun.COM #endif
1400Sstevel@tonic-gate 	endnetent();
1410Sstevel@tonic-gate 	NET_R_END_RESULT(NET_R_OK);
1420Sstevel@tonic-gate }
1430Sstevel@tonic-gate 
1440Sstevel@tonic-gate /* Private */
1450Sstevel@tonic-gate 
1460Sstevel@tonic-gate #ifndef NETENT_DATA
1470Sstevel@tonic-gate static NET_R_RETURN
copy_netent(struct netent * ne,struct netent * nptr,NET_R_COPY_ARGS)1480Sstevel@tonic-gate copy_netent(struct netent *ne, struct netent *nptr, NET_R_COPY_ARGS) {
1490Sstevel@tonic-gate 	char *cp;
1500Sstevel@tonic-gate 	int i, n;
1510Sstevel@tonic-gate 	int numptr, len;
1520Sstevel@tonic-gate 
1530Sstevel@tonic-gate 	/* Find out the amount of space required to store the answer. */
154*11038SRao.Shoaib@Sun.COM 	numptr = 1; /*%< NULL ptr */
1550Sstevel@tonic-gate 	len = (char *)ALIGN(buf) - buf;
1560Sstevel@tonic-gate 	for (i = 0; ne->n_aliases[i]; i++, numptr++) {
1570Sstevel@tonic-gate 		len += strlen(ne->n_aliases[i]) + 1;
1580Sstevel@tonic-gate 	}
1590Sstevel@tonic-gate 	len += strlen(ne->n_name) + 1;
1600Sstevel@tonic-gate 	len += numptr * sizeof(char*);
1610Sstevel@tonic-gate 
1620Sstevel@tonic-gate 	if (len > (int)buflen) {
1630Sstevel@tonic-gate 		errno = ERANGE;
1640Sstevel@tonic-gate 		return (NET_R_BAD);
1650Sstevel@tonic-gate 	}
1660Sstevel@tonic-gate 
1670Sstevel@tonic-gate 	/* copy net value and type */
1680Sstevel@tonic-gate 	nptr->n_addrtype = ne->n_addrtype;
1690Sstevel@tonic-gate 	nptr->n_net = ne->n_net;
1700Sstevel@tonic-gate 
1710Sstevel@tonic-gate 	cp = (char *)ALIGN(buf) + numptr * sizeof(char *);
1720Sstevel@tonic-gate 
1730Sstevel@tonic-gate 	/* copy official name */
1740Sstevel@tonic-gate 	n = strlen(ne->n_name) + 1;
1750Sstevel@tonic-gate 	strcpy(cp, ne->n_name);
1760Sstevel@tonic-gate 	nptr->n_name = cp;
1770Sstevel@tonic-gate 	cp += n;
1780Sstevel@tonic-gate 
1790Sstevel@tonic-gate 	/* copy aliases */
1800Sstevel@tonic-gate 	nptr->n_aliases = (char **)ALIGN(buf);
1810Sstevel@tonic-gate 	for (i = 0 ; ne->n_aliases[i]; i++) {
1820Sstevel@tonic-gate 		n = strlen(ne->n_aliases[i]) + 1;
1830Sstevel@tonic-gate 		strcpy(cp, ne->n_aliases[i]);
1840Sstevel@tonic-gate 		nptr->n_aliases[i] = cp;
1850Sstevel@tonic-gate 		cp += n;
1860Sstevel@tonic-gate 	}
1870Sstevel@tonic-gate 	nptr->n_aliases[i] = NULL;
1880Sstevel@tonic-gate 
1890Sstevel@tonic-gate 	return (NET_R_OK);
1900Sstevel@tonic-gate }
1910Sstevel@tonic-gate #else /* !NETENT_DATA */
1920Sstevel@tonic-gate static int
copy_netent(struct netent * ne,struct netent * nptr,NET_R_COPY_ARGS)1930Sstevel@tonic-gate copy_netent(struct netent *ne, struct netent *nptr, NET_R_COPY_ARGS) {
1940Sstevel@tonic-gate 	char *cp, *eob;
1950Sstevel@tonic-gate 	int i, n;
1960Sstevel@tonic-gate 
1970Sstevel@tonic-gate 	/* copy net value and type */
1980Sstevel@tonic-gate 	nptr->n_addrtype = ne->n_addrtype;
1990Sstevel@tonic-gate 	nptr->n_net = ne->n_net;
2000Sstevel@tonic-gate 
2010Sstevel@tonic-gate 	/* copy official name */
2020Sstevel@tonic-gate 	cp = ndptr->line;
2030Sstevel@tonic-gate 	eob = ndptr->line + sizeof(ndptr->line);
2040Sstevel@tonic-gate 	if ((n = strlen(ne->n_name) + 1) < (eob - cp)) {
2050Sstevel@tonic-gate 		strcpy(cp, ne->n_name);
2060Sstevel@tonic-gate 		nptr->n_name = cp;
2070Sstevel@tonic-gate 		cp += n;
2080Sstevel@tonic-gate 	} else {
2090Sstevel@tonic-gate 		return (-1);
2100Sstevel@tonic-gate 	}
2110Sstevel@tonic-gate 
2120Sstevel@tonic-gate 	/* copy aliases */
2130Sstevel@tonic-gate 	i = 0;
2140Sstevel@tonic-gate 	nptr->n_aliases = ndptr->net_aliases;
2150Sstevel@tonic-gate 	while (ne->n_aliases[i] && i < (_MAXALIASES-1)) {
2160Sstevel@tonic-gate 		if ((n = strlen(ne->n_aliases[i]) + 1) < (eob - cp)) {
2170Sstevel@tonic-gate 			strcpy(cp, ne->n_aliases[i]);
2180Sstevel@tonic-gate 			nptr->n_aliases[i] = cp;
2190Sstevel@tonic-gate 			cp += n;
2200Sstevel@tonic-gate 		} else {
2210Sstevel@tonic-gate 			break;
2220Sstevel@tonic-gate 		}
2230Sstevel@tonic-gate 		i++;
2240Sstevel@tonic-gate 	}
2250Sstevel@tonic-gate 	nptr->n_aliases[i] = NULL;
2260Sstevel@tonic-gate 
2270Sstevel@tonic-gate 	return (NET_R_OK);
2280Sstevel@tonic-gate }
2290Sstevel@tonic-gate #endif /* !NETENT_DATA */
2300Sstevel@tonic-gate #else /* NET_R_RETURN */
2310Sstevel@tonic-gate 	static int getnetent_r_unknown_system = 0;
2320Sstevel@tonic-gate #endif /* NET_R_RETURN */
2330Sstevel@tonic-gate #endif /* !defined(_REENTRANT) || !defined(DO_PTHREADS) */
234*11038SRao.Shoaib@Sun.COM /*! \file */
235