xref: /titanic_52/usr/src/lib/libresolv2/common/irs/getnetent_r.c (revision 9525b14bcdeb5b5f6f95ab27c2f48f18bd2ec829)
17c478bd9Sstevel@tonic-gate /*
2*9525b14bSRao Shoaib  * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
37c478bd9Sstevel@tonic-gate  * Copyright (c) 1998-1999 by Internet Software Consortium.
47c478bd9Sstevel@tonic-gate  *
57c478bd9Sstevel@tonic-gate  * Permission to use, copy, modify, and distribute this software for any
67c478bd9Sstevel@tonic-gate  * purpose with or without fee is hereby granted, provided that the above
77c478bd9Sstevel@tonic-gate  * copyright notice and this permission notice appear in all copies.
87c478bd9Sstevel@tonic-gate  *
9*9525b14bSRao Shoaib  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
10*9525b14bSRao Shoaib  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11*9525b14bSRao Shoaib  * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
12*9525b14bSRao Shoaib  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13*9525b14bSRao Shoaib  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14*9525b14bSRao Shoaib  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
15*9525b14bSRao Shoaib  * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
167c478bd9Sstevel@tonic-gate  */
177c478bd9Sstevel@tonic-gate 
187c478bd9Sstevel@tonic-gate #if defined(LIBC_SCCS) && !defined(lint)
19*9525b14bSRao Shoaib static const char rcsid[] = "$Id: getnetent_r.c,v 1.6 2005/09/03 12:41:38 marka Exp $";
207c478bd9Sstevel@tonic-gate #endif /* LIBC_SCCS and not lint */
217c478bd9Sstevel@tonic-gate 
227c478bd9Sstevel@tonic-gate #include <port_before.h>
237c478bd9Sstevel@tonic-gate #if !defined(_REENTRANT) || !defined(DO_PTHREADS)
247c478bd9Sstevel@tonic-gate 	static int getnetent_r_not_required = 0;
257c478bd9Sstevel@tonic-gate #else
267c478bd9Sstevel@tonic-gate #include <errno.h>
277c478bd9Sstevel@tonic-gate #include <string.h>
287c478bd9Sstevel@tonic-gate #include <stdio.h>
297c478bd9Sstevel@tonic-gate #include <sys/types.h>
307c478bd9Sstevel@tonic-gate #include <netinet/in.h>
317c478bd9Sstevel@tonic-gate #include <netdb.h>
327c478bd9Sstevel@tonic-gate #include <sys/param.h>
337c478bd9Sstevel@tonic-gate #include <port_after.h>
347c478bd9Sstevel@tonic-gate 
357c478bd9Sstevel@tonic-gate #ifdef NET_R_RETURN
367c478bd9Sstevel@tonic-gate 
377c478bd9Sstevel@tonic-gate static NET_R_RETURN
387c478bd9Sstevel@tonic-gate copy_netent(struct netent *, struct netent *, NET_R_COPY_ARGS);
397c478bd9Sstevel@tonic-gate 
407c478bd9Sstevel@tonic-gate NET_R_RETURN
417c478bd9Sstevel@tonic-gate getnetbyname_r(const char *name,  struct netent *nptr, NET_R_ARGS) {
427c478bd9Sstevel@tonic-gate 	struct netent *ne = getnetbyname(name);
437c478bd9Sstevel@tonic-gate #ifdef NET_R_SETANSWER
447c478bd9Sstevel@tonic-gate 	int n = 0;
457c478bd9Sstevel@tonic-gate 
467c478bd9Sstevel@tonic-gate 	if (ne == NULL || (n = copy_netent(ne, nptr, NET_R_COPY)) != 0)
477c478bd9Sstevel@tonic-gate 		*answerp = NULL;
487c478bd9Sstevel@tonic-gate 	else
497c478bd9Sstevel@tonic-gate 		*answerp = ne;
507c478bd9Sstevel@tonic-gate 	if (ne == NULL)
517c478bd9Sstevel@tonic-gate 		*h_errnop = h_errno;
527c478bd9Sstevel@tonic-gate 	return (n);
537c478bd9Sstevel@tonic-gate #else
547c478bd9Sstevel@tonic-gate 	if (ne == NULL)
557c478bd9Sstevel@tonic-gate 		return (NET_R_BAD);
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate 	return (copy_netent(ne, nptr, NET_R_COPY));
587c478bd9Sstevel@tonic-gate #endif
597c478bd9Sstevel@tonic-gate }
607c478bd9Sstevel@tonic-gate 
617c478bd9Sstevel@tonic-gate #ifndef GETNETBYADDR_ADDR_T
627c478bd9Sstevel@tonic-gate #define GETNETBYADDR_ADDR_T long
637c478bd9Sstevel@tonic-gate #endif
647c478bd9Sstevel@tonic-gate NET_R_RETURN
657c478bd9Sstevel@tonic-gate getnetbyaddr_r(GETNETBYADDR_ADDR_T addr, int type, struct netent *nptr, NET_R_ARGS) {
667c478bd9Sstevel@tonic-gate 	struct netent *ne = getnetbyaddr(addr, type);
677c478bd9Sstevel@tonic-gate #ifdef NET_R_SETANSWER
687c478bd9Sstevel@tonic-gate 	int n = 0;
697c478bd9Sstevel@tonic-gate 
707c478bd9Sstevel@tonic-gate 	if (ne == NULL || (n = copy_netent(ne, nptr, NET_R_COPY)) != 0)
717c478bd9Sstevel@tonic-gate 		*answerp = NULL;
727c478bd9Sstevel@tonic-gate 	else
737c478bd9Sstevel@tonic-gate 		*answerp = ne;
747c478bd9Sstevel@tonic-gate 	if (ne == NULL)
757c478bd9Sstevel@tonic-gate 		*h_errnop = h_errno;
767c478bd9Sstevel@tonic-gate 	return (n);
777c478bd9Sstevel@tonic-gate #else
787c478bd9Sstevel@tonic-gate 
797c478bd9Sstevel@tonic-gate 	if (ne == NULL)
807c478bd9Sstevel@tonic-gate 		return (NET_R_BAD);
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate 	return (copy_netent(ne, nptr, NET_R_COPY));
837c478bd9Sstevel@tonic-gate #endif
847c478bd9Sstevel@tonic-gate }
857c478bd9Sstevel@tonic-gate 
86*9525b14bSRao Shoaib /*%
877c478bd9Sstevel@tonic-gate  *	These assume a single context is in operation per thread.
887c478bd9Sstevel@tonic-gate  *	If this is not the case we will need to call irs directly
897c478bd9Sstevel@tonic-gate  *	rather than through the base functions.
907c478bd9Sstevel@tonic-gate  */
917c478bd9Sstevel@tonic-gate 
927c478bd9Sstevel@tonic-gate NET_R_RETURN
937c478bd9Sstevel@tonic-gate getnetent_r(struct netent *nptr, NET_R_ARGS) {
947c478bd9Sstevel@tonic-gate 	struct netent *ne = getnetent();
957c478bd9Sstevel@tonic-gate #ifdef NET_R_SETANSWER
967c478bd9Sstevel@tonic-gate 	int n = 0;
977c478bd9Sstevel@tonic-gate 
987c478bd9Sstevel@tonic-gate 	if (ne == NULL || (n = copy_netent(ne, nptr, NET_R_COPY)) != 0)
997c478bd9Sstevel@tonic-gate 		*answerp = NULL;
1007c478bd9Sstevel@tonic-gate 	else
1017c478bd9Sstevel@tonic-gate 		*answerp = ne;
1027c478bd9Sstevel@tonic-gate 	if (ne == NULL)
1037c478bd9Sstevel@tonic-gate 		*h_errnop = h_errno;
1047c478bd9Sstevel@tonic-gate 	return (n);
1057c478bd9Sstevel@tonic-gate #else
1067c478bd9Sstevel@tonic-gate 
1077c478bd9Sstevel@tonic-gate 	if (ne == NULL)
1087c478bd9Sstevel@tonic-gate 		return (NET_R_BAD);
1097c478bd9Sstevel@tonic-gate 
1107c478bd9Sstevel@tonic-gate 	return (copy_netent(ne, nptr, NET_R_COPY));
1117c478bd9Sstevel@tonic-gate #endif
1127c478bd9Sstevel@tonic-gate }
1137c478bd9Sstevel@tonic-gate 
1147c478bd9Sstevel@tonic-gate NET_R_SET_RETURN
1157c478bd9Sstevel@tonic-gate #ifdef NET_R_ENT_ARGS
1167c478bd9Sstevel@tonic-gate setnetent_r(int stay_open, NET_R_ENT_ARGS)
1177c478bd9Sstevel@tonic-gate #else
1187c478bd9Sstevel@tonic-gate setnetent_r(int stay_open)
1197c478bd9Sstevel@tonic-gate #endif
1207c478bd9Sstevel@tonic-gate {
121*9525b14bSRao Shoaib #ifdef NET_R_ENT_ARGS
122*9525b14bSRao Shoaib 	UNUSED(ndptr);
123*9525b14bSRao Shoaib #endif
1247c478bd9Sstevel@tonic-gate 	setnetent(stay_open);
1257c478bd9Sstevel@tonic-gate #ifdef NET_R_SET_RESULT
1267c478bd9Sstevel@tonic-gate 	return (NET_R_SET_RESULT);
1277c478bd9Sstevel@tonic-gate #endif
1287c478bd9Sstevel@tonic-gate }
1297c478bd9Sstevel@tonic-gate 
1307c478bd9Sstevel@tonic-gate NET_R_END_RETURN
1317c478bd9Sstevel@tonic-gate #ifdef NET_R_ENT_ARGS
1327c478bd9Sstevel@tonic-gate endnetent_r(NET_R_ENT_ARGS)
1337c478bd9Sstevel@tonic-gate #else
1347c478bd9Sstevel@tonic-gate endnetent_r()
1357c478bd9Sstevel@tonic-gate #endif
1367c478bd9Sstevel@tonic-gate {
137*9525b14bSRao Shoaib #ifdef NET_R_ENT_ARGS
138*9525b14bSRao Shoaib 	UNUSED(ndptr);
139*9525b14bSRao Shoaib #endif
1407c478bd9Sstevel@tonic-gate 	endnetent();
1417c478bd9Sstevel@tonic-gate 	NET_R_END_RESULT(NET_R_OK);
1427c478bd9Sstevel@tonic-gate }
1437c478bd9Sstevel@tonic-gate 
1447c478bd9Sstevel@tonic-gate /* Private */
1457c478bd9Sstevel@tonic-gate 
1467c478bd9Sstevel@tonic-gate #ifndef NETENT_DATA
1477c478bd9Sstevel@tonic-gate static NET_R_RETURN
1487c478bd9Sstevel@tonic-gate copy_netent(struct netent *ne, struct netent *nptr, NET_R_COPY_ARGS) {
1497c478bd9Sstevel@tonic-gate 	char *cp;
1507c478bd9Sstevel@tonic-gate 	int i, n;
1517c478bd9Sstevel@tonic-gate 	int numptr, len;
1527c478bd9Sstevel@tonic-gate 
1537c478bd9Sstevel@tonic-gate 	/* Find out the amount of space required to store the answer. */
154*9525b14bSRao Shoaib 	numptr = 1; /*%< NULL ptr */
1557c478bd9Sstevel@tonic-gate 	len = (char *)ALIGN(buf) - buf;
1567c478bd9Sstevel@tonic-gate 	for (i = 0; ne->n_aliases[i]; i++, numptr++) {
1577c478bd9Sstevel@tonic-gate 		len += strlen(ne->n_aliases[i]) + 1;
1587c478bd9Sstevel@tonic-gate 	}
1597c478bd9Sstevel@tonic-gate 	len += strlen(ne->n_name) + 1;
1607c478bd9Sstevel@tonic-gate 	len += numptr * sizeof(char*);
1617c478bd9Sstevel@tonic-gate 
1627c478bd9Sstevel@tonic-gate 	if (len > (int)buflen) {
1637c478bd9Sstevel@tonic-gate 		errno = ERANGE;
1647c478bd9Sstevel@tonic-gate 		return (NET_R_BAD);
1657c478bd9Sstevel@tonic-gate 	}
1667c478bd9Sstevel@tonic-gate 
1677c478bd9Sstevel@tonic-gate 	/* copy net value and type */
1687c478bd9Sstevel@tonic-gate 	nptr->n_addrtype = ne->n_addrtype;
1697c478bd9Sstevel@tonic-gate 	nptr->n_net = ne->n_net;
1707c478bd9Sstevel@tonic-gate 
1717c478bd9Sstevel@tonic-gate 	cp = (char *)ALIGN(buf) + numptr * sizeof(char *);
1727c478bd9Sstevel@tonic-gate 
1737c478bd9Sstevel@tonic-gate 	/* copy official name */
1747c478bd9Sstevel@tonic-gate 	n = strlen(ne->n_name) + 1;
1757c478bd9Sstevel@tonic-gate 	strcpy(cp, ne->n_name);
1767c478bd9Sstevel@tonic-gate 	nptr->n_name = cp;
1777c478bd9Sstevel@tonic-gate 	cp += n;
1787c478bd9Sstevel@tonic-gate 
1797c478bd9Sstevel@tonic-gate 	/* copy aliases */
1807c478bd9Sstevel@tonic-gate 	nptr->n_aliases = (char **)ALIGN(buf);
1817c478bd9Sstevel@tonic-gate 	for (i = 0 ; ne->n_aliases[i]; i++) {
1827c478bd9Sstevel@tonic-gate 		n = strlen(ne->n_aliases[i]) + 1;
1837c478bd9Sstevel@tonic-gate 		strcpy(cp, ne->n_aliases[i]);
1847c478bd9Sstevel@tonic-gate 		nptr->n_aliases[i] = cp;
1857c478bd9Sstevel@tonic-gate 		cp += n;
1867c478bd9Sstevel@tonic-gate 	}
1877c478bd9Sstevel@tonic-gate 	nptr->n_aliases[i] = NULL;
1887c478bd9Sstevel@tonic-gate 
1897c478bd9Sstevel@tonic-gate 	return (NET_R_OK);
1907c478bd9Sstevel@tonic-gate }
1917c478bd9Sstevel@tonic-gate #else /* !NETENT_DATA */
1927c478bd9Sstevel@tonic-gate static int
1937c478bd9Sstevel@tonic-gate copy_netent(struct netent *ne, struct netent *nptr, NET_R_COPY_ARGS) {
1947c478bd9Sstevel@tonic-gate 	char *cp, *eob;
1957c478bd9Sstevel@tonic-gate 	int i, n;
1967c478bd9Sstevel@tonic-gate 
1977c478bd9Sstevel@tonic-gate 	/* copy net value and type */
1987c478bd9Sstevel@tonic-gate 	nptr->n_addrtype = ne->n_addrtype;
1997c478bd9Sstevel@tonic-gate 	nptr->n_net = ne->n_net;
2007c478bd9Sstevel@tonic-gate 
2017c478bd9Sstevel@tonic-gate 	/* copy official name */
2027c478bd9Sstevel@tonic-gate 	cp = ndptr->line;
2037c478bd9Sstevel@tonic-gate 	eob = ndptr->line + sizeof(ndptr->line);
2047c478bd9Sstevel@tonic-gate 	if ((n = strlen(ne->n_name) + 1) < (eob - cp)) {
2057c478bd9Sstevel@tonic-gate 		strcpy(cp, ne->n_name);
2067c478bd9Sstevel@tonic-gate 		nptr->n_name = cp;
2077c478bd9Sstevel@tonic-gate 		cp += n;
2087c478bd9Sstevel@tonic-gate 	} else {
2097c478bd9Sstevel@tonic-gate 		return (-1);
2107c478bd9Sstevel@tonic-gate 	}
2117c478bd9Sstevel@tonic-gate 
2127c478bd9Sstevel@tonic-gate 	/* copy aliases */
2137c478bd9Sstevel@tonic-gate 	i = 0;
2147c478bd9Sstevel@tonic-gate 	nptr->n_aliases = ndptr->net_aliases;
2157c478bd9Sstevel@tonic-gate 	while (ne->n_aliases[i] && i < (_MAXALIASES-1)) {
2167c478bd9Sstevel@tonic-gate 		if ((n = strlen(ne->n_aliases[i]) + 1) < (eob - cp)) {
2177c478bd9Sstevel@tonic-gate 			strcpy(cp, ne->n_aliases[i]);
2187c478bd9Sstevel@tonic-gate 			nptr->n_aliases[i] = cp;
2197c478bd9Sstevel@tonic-gate 			cp += n;
2207c478bd9Sstevel@tonic-gate 		} else {
2217c478bd9Sstevel@tonic-gate 			break;
2227c478bd9Sstevel@tonic-gate 		}
2237c478bd9Sstevel@tonic-gate 		i++;
2247c478bd9Sstevel@tonic-gate 	}
2257c478bd9Sstevel@tonic-gate 	nptr->n_aliases[i] = NULL;
2267c478bd9Sstevel@tonic-gate 
2277c478bd9Sstevel@tonic-gate 	return (NET_R_OK);
2287c478bd9Sstevel@tonic-gate }
2297c478bd9Sstevel@tonic-gate #endif /* !NETENT_DATA */
2307c478bd9Sstevel@tonic-gate #else /* NET_R_RETURN */
2317c478bd9Sstevel@tonic-gate 	static int getnetent_r_unknown_system = 0;
2327c478bd9Sstevel@tonic-gate #endif /* NET_R_RETURN */
2337c478bd9Sstevel@tonic-gate #endif /* !defined(_REENTRANT) || !defined(DO_PTHREADS) */
234*9525b14bSRao Shoaib /*! \file */
235