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