17c478bd9Sstevel@tonic-gate /*
29525b14bSRao 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  *
99525b14bSRao Shoaib  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
109525b14bSRao Shoaib  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
119525b14bSRao Shoaib  * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
129525b14bSRao Shoaib  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
139525b14bSRao Shoaib  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
149525b14bSRao Shoaib  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
159525b14bSRao Shoaib  * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
167c478bd9Sstevel@tonic-gate  */
177c478bd9Sstevel@tonic-gate 
187c478bd9Sstevel@tonic-gate #include <port_before.h>
197c478bd9Sstevel@tonic-gate #if !defined(_REENTRANT) || !defined(DO_PTHREADS)
207c478bd9Sstevel@tonic-gate 	static int getprotoent_r_not_required = 0;
217c478bd9Sstevel@tonic-gate #else
227c478bd9Sstevel@tonic-gate #include <errno.h>
237c478bd9Sstevel@tonic-gate #include <string.h>
247c478bd9Sstevel@tonic-gate #include <stdio.h>
257c478bd9Sstevel@tonic-gate #include <sys/types.h>
267c478bd9Sstevel@tonic-gate #include <netinet/in.h>
277c478bd9Sstevel@tonic-gate #include <netdb.h>
287c478bd9Sstevel@tonic-gate #include <port_after.h>
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate #ifdef PROTO_R_RETURN
317c478bd9Sstevel@tonic-gate 
32*55fea89dSDan Cross static PROTO_R_RETURN
337c478bd9Sstevel@tonic-gate copy_protoent(struct protoent *, struct protoent *, PROTO_R_COPY_ARGS);
347c478bd9Sstevel@tonic-gate 
357c478bd9Sstevel@tonic-gate PROTO_R_RETURN
getprotobyname_r(const char * name,struct protoent * pptr,PROTO_R_ARGS)367c478bd9Sstevel@tonic-gate getprotobyname_r(const char *name, struct protoent *pptr, PROTO_R_ARGS) {
377c478bd9Sstevel@tonic-gate 	struct protoent *pe = getprotobyname(name);
387c478bd9Sstevel@tonic-gate #ifdef PROTO_R_SETANSWER
397c478bd9Sstevel@tonic-gate 	int n = 0;
407c478bd9Sstevel@tonic-gate 
417c478bd9Sstevel@tonic-gate 	if (pe == NULL || (n = copy_protoent(pe, pptr, PROTO_R_COPY)) != 0)
427c478bd9Sstevel@tonic-gate 		*answerp = NULL;
437c478bd9Sstevel@tonic-gate 	else
447c478bd9Sstevel@tonic-gate 		*answerp = pptr;
45*55fea89dSDan Cross 
467c478bd9Sstevel@tonic-gate 	return (n);
477c478bd9Sstevel@tonic-gate #else
487c478bd9Sstevel@tonic-gate 	if (pe == NULL)
497c478bd9Sstevel@tonic-gate 		return (PROTO_R_BAD);
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate 	return (copy_protoent(pe, pptr, PROTO_R_COPY));
527c478bd9Sstevel@tonic-gate #endif
537c478bd9Sstevel@tonic-gate }
547c478bd9Sstevel@tonic-gate 
557c478bd9Sstevel@tonic-gate PROTO_R_RETURN
getprotobynumber_r(int proto,struct protoent * pptr,PROTO_R_ARGS)567c478bd9Sstevel@tonic-gate getprotobynumber_r(int proto, struct protoent *pptr, PROTO_R_ARGS) {
577c478bd9Sstevel@tonic-gate 	struct protoent *pe = getprotobynumber(proto);
587c478bd9Sstevel@tonic-gate #ifdef PROTO_R_SETANSWER
597c478bd9Sstevel@tonic-gate 	int n = 0;
607c478bd9Sstevel@tonic-gate 
617c478bd9Sstevel@tonic-gate 	if (pe == NULL || (n = copy_protoent(pe, pptr, PROTO_R_COPY)) != 0)
627c478bd9Sstevel@tonic-gate 		*answerp = NULL;
637c478bd9Sstevel@tonic-gate 	else
647c478bd9Sstevel@tonic-gate 		*answerp = pptr;
65*55fea89dSDan Cross 
667c478bd9Sstevel@tonic-gate 	return (n);
677c478bd9Sstevel@tonic-gate #else
687c478bd9Sstevel@tonic-gate 	if (pe == NULL)
697c478bd9Sstevel@tonic-gate 		return (PROTO_R_BAD);
707c478bd9Sstevel@tonic-gate 
717c478bd9Sstevel@tonic-gate 	return (copy_protoent(pe, pptr, PROTO_R_COPY));
727c478bd9Sstevel@tonic-gate #endif
737c478bd9Sstevel@tonic-gate }
747c478bd9Sstevel@tonic-gate 
759525b14bSRao Shoaib /*%
767c478bd9Sstevel@tonic-gate  *	These assume a single context is in operation per thread.
777c478bd9Sstevel@tonic-gate  *	If this is not the case we will need to call irs directly
787c478bd9Sstevel@tonic-gate  *	rather than through the base functions.
797c478bd9Sstevel@tonic-gate  */
807c478bd9Sstevel@tonic-gate 
817c478bd9Sstevel@tonic-gate PROTO_R_RETURN
getprotoent_r(struct protoent * pptr,PROTO_R_ARGS)827c478bd9Sstevel@tonic-gate getprotoent_r(struct protoent *pptr, PROTO_R_ARGS) {
837c478bd9Sstevel@tonic-gate 	struct protoent *pe = getprotoent();
847c478bd9Sstevel@tonic-gate #ifdef PROTO_R_SETANSWER
857c478bd9Sstevel@tonic-gate 	int n = 0;
867c478bd9Sstevel@tonic-gate 
877c478bd9Sstevel@tonic-gate 	if (pe == NULL || (n = copy_protoent(pe, pptr, PROTO_R_COPY)) != 0)
887c478bd9Sstevel@tonic-gate 		*answerp = NULL;
897c478bd9Sstevel@tonic-gate 	else
907c478bd9Sstevel@tonic-gate 		*answerp = pptr;
91*55fea89dSDan Cross 
927c478bd9Sstevel@tonic-gate 	return (n);
937c478bd9Sstevel@tonic-gate #else
947c478bd9Sstevel@tonic-gate 	if (pe == NULL)
957c478bd9Sstevel@tonic-gate 		return (PROTO_R_BAD);
967c478bd9Sstevel@tonic-gate 
977c478bd9Sstevel@tonic-gate 	return (copy_protoent(pe, pptr, PROTO_R_COPY));
987c478bd9Sstevel@tonic-gate #endif
997c478bd9Sstevel@tonic-gate }
1007c478bd9Sstevel@tonic-gate 
1017c478bd9Sstevel@tonic-gate PROTO_R_SET_RETURN
1027c478bd9Sstevel@tonic-gate #ifdef PROTO_R_ENT_ARGS
setprotoent_r(int stay_open,PROTO_R_ENT_ARGS)1037c478bd9Sstevel@tonic-gate setprotoent_r(int stay_open, PROTO_R_ENT_ARGS)
1047c478bd9Sstevel@tonic-gate #else
1057c478bd9Sstevel@tonic-gate setprotoent_r(int stay_open)
1067c478bd9Sstevel@tonic-gate #endif
1077c478bd9Sstevel@tonic-gate {
1089525b14bSRao Shoaib #ifdef PROTO_R_ENT_UNUSED
1099525b14bSRao Shoaib         PROTO_R_ENT_UNUSED;
1109525b14bSRao Shoaib #endif
1117c478bd9Sstevel@tonic-gate 	setprotoent(stay_open);
1127c478bd9Sstevel@tonic-gate #ifdef PROTO_R_SET_RESULT
1137c478bd9Sstevel@tonic-gate 	return (PROTO_R_SET_RESULT);
1147c478bd9Sstevel@tonic-gate #endif
1157c478bd9Sstevel@tonic-gate }
1167c478bd9Sstevel@tonic-gate 
1177c478bd9Sstevel@tonic-gate PROTO_R_END_RETURN
1187c478bd9Sstevel@tonic-gate #ifdef PROTO_R_ENT_ARGS
endprotoent_r(PROTO_R_ENT_ARGS)1197c478bd9Sstevel@tonic-gate endprotoent_r(PROTO_R_ENT_ARGS)
1207c478bd9Sstevel@tonic-gate #else
1217c478bd9Sstevel@tonic-gate endprotoent_r()
1227c478bd9Sstevel@tonic-gate #endif
1237c478bd9Sstevel@tonic-gate {
1249525b14bSRao Shoaib #ifdef PROTO_R_ENT_UNUSED
1259525b14bSRao Shoaib         PROTO_R_ENT_UNUSED;
1269525b14bSRao Shoaib #endif
1277c478bd9Sstevel@tonic-gate 	endprotoent();
1287c478bd9Sstevel@tonic-gate 	PROTO_R_END_RESULT(PROTO_R_OK);
1297c478bd9Sstevel@tonic-gate }
1307c478bd9Sstevel@tonic-gate 
1317c478bd9Sstevel@tonic-gate /* Private */
1327c478bd9Sstevel@tonic-gate 
1337c478bd9Sstevel@tonic-gate #ifndef PROTOENT_DATA
1347c478bd9Sstevel@tonic-gate static PROTO_R_RETURN
copy_protoent(struct protoent * pe,struct protoent * pptr,PROTO_R_COPY_ARGS)1357c478bd9Sstevel@tonic-gate copy_protoent(struct protoent *pe, struct protoent *pptr, PROTO_R_COPY_ARGS) {
1367c478bd9Sstevel@tonic-gate 	char *cp;
1377c478bd9Sstevel@tonic-gate 	int i, n;
1387c478bd9Sstevel@tonic-gate 	int numptr, len;
1397c478bd9Sstevel@tonic-gate 
1407c478bd9Sstevel@tonic-gate 	/* Find out the amount of space required to store the answer. */
1419525b14bSRao Shoaib 	numptr = 1; /*%< NULL ptr */
1427c478bd9Sstevel@tonic-gate 	len = (char *)ALIGN(buf) - buf;
1437c478bd9Sstevel@tonic-gate 	for (i = 0; pe->p_aliases[i]; i++, numptr++) {
1447c478bd9Sstevel@tonic-gate 		len += strlen(pe->p_aliases[i]) + 1;
1457c478bd9Sstevel@tonic-gate 	}
1467c478bd9Sstevel@tonic-gate 	len += strlen(pe->p_name) + 1;
1477c478bd9Sstevel@tonic-gate 	len += numptr * sizeof(char*);
148*55fea89dSDan Cross 
1497c478bd9Sstevel@tonic-gate 	if (len > (int)buflen) {
1507c478bd9Sstevel@tonic-gate 		errno = ERANGE;
1517c478bd9Sstevel@tonic-gate 		return (PROTO_R_BAD);
1527c478bd9Sstevel@tonic-gate 	}
1537c478bd9Sstevel@tonic-gate 
1547c478bd9Sstevel@tonic-gate 	/* copy protocol value*/
1557c478bd9Sstevel@tonic-gate 	pptr->p_proto = pe->p_proto;
1567c478bd9Sstevel@tonic-gate 
1577c478bd9Sstevel@tonic-gate 	cp = (char *)ALIGN(buf) + numptr * sizeof(char *);
1587c478bd9Sstevel@tonic-gate 
1597c478bd9Sstevel@tonic-gate 	/* copy official name */
1607c478bd9Sstevel@tonic-gate 	n = strlen(pe->p_name) + 1;
1617c478bd9Sstevel@tonic-gate 	strcpy(cp, pe->p_name);
1627c478bd9Sstevel@tonic-gate 	pptr->p_name = cp;
1637c478bd9Sstevel@tonic-gate 	cp += n;
1647c478bd9Sstevel@tonic-gate 
1657c478bd9Sstevel@tonic-gate 	/* copy aliases */
1667c478bd9Sstevel@tonic-gate 	pptr->p_aliases = (char **)ALIGN(buf);
1677c478bd9Sstevel@tonic-gate 	for (i = 0 ; pe->p_aliases[i]; i++) {
1687c478bd9Sstevel@tonic-gate 		n = strlen(pe->p_aliases[i]) + 1;
1697c478bd9Sstevel@tonic-gate 		strcpy(cp, pe->p_aliases[i]);
1707c478bd9Sstevel@tonic-gate 		pptr->p_aliases[i] = cp;
1717c478bd9Sstevel@tonic-gate 		cp += n;
1727c478bd9Sstevel@tonic-gate 	}
1737c478bd9Sstevel@tonic-gate 	pptr->p_aliases[i] = NULL;
1747c478bd9Sstevel@tonic-gate 
1757c478bd9Sstevel@tonic-gate 	return (PROTO_R_OK);
1767c478bd9Sstevel@tonic-gate }
1777c478bd9Sstevel@tonic-gate #else /* !PROTOENT_DATA */
1787c478bd9Sstevel@tonic-gate static int
copy_protoent(struct protoent * pe,struct protoent * pptr,PROTO_R_COPY_ARGS)1797c478bd9Sstevel@tonic-gate copy_protoent(struct protoent *pe, struct protoent *pptr, PROTO_R_COPY_ARGS) {
1807c478bd9Sstevel@tonic-gate 	char *cp, *eob;
1817c478bd9Sstevel@tonic-gate 	int i, n;
1827c478bd9Sstevel@tonic-gate 
1837c478bd9Sstevel@tonic-gate 	/* copy protocol value */
1847c478bd9Sstevel@tonic-gate 	pptr->p_proto = pe->p_proto;
1857c478bd9Sstevel@tonic-gate 
1867c478bd9Sstevel@tonic-gate 	/* copy official name */
1877c478bd9Sstevel@tonic-gate 	cp = pdptr->line;
1887c478bd9Sstevel@tonic-gate 	eob = pdptr->line + sizeof(pdptr->line);
1897c478bd9Sstevel@tonic-gate 	if ((n = strlen(pe->p_name) + 1) < (eob - cp)) {
1907c478bd9Sstevel@tonic-gate 		strcpy(cp, pe->p_name);
1917c478bd9Sstevel@tonic-gate 		pptr->p_name = cp;
1927c478bd9Sstevel@tonic-gate 		cp += n;
1937c478bd9Sstevel@tonic-gate 	} else {
1947c478bd9Sstevel@tonic-gate 		return (-1);
1957c478bd9Sstevel@tonic-gate 	}
1967c478bd9Sstevel@tonic-gate 
1977c478bd9Sstevel@tonic-gate 	/* copy aliases */
1987c478bd9Sstevel@tonic-gate 	i = 0;
1997c478bd9Sstevel@tonic-gate 	pptr->p_aliases = pdptr->proto_aliases;
2007c478bd9Sstevel@tonic-gate 	while (pe->p_aliases[i] && i < (_MAXALIASES-1)) {
2017c478bd9Sstevel@tonic-gate 		if ((n = strlen(pe->p_aliases[i]) + 1) < (eob - cp)) {
2027c478bd9Sstevel@tonic-gate 			strcpy(cp, pe->p_aliases[i]);
2037c478bd9Sstevel@tonic-gate 			pptr->p_aliases[i] = cp;
2047c478bd9Sstevel@tonic-gate 			cp += n;
2057c478bd9Sstevel@tonic-gate 		} else {
2067c478bd9Sstevel@tonic-gate 			break;
2077c478bd9Sstevel@tonic-gate 		}
2087c478bd9Sstevel@tonic-gate 		i++;
2097c478bd9Sstevel@tonic-gate 	}
2107c478bd9Sstevel@tonic-gate 	pptr->p_aliases[i] = NULL;
2117c478bd9Sstevel@tonic-gate 
2127c478bd9Sstevel@tonic-gate 	return (PROTO_R_OK);
2137c478bd9Sstevel@tonic-gate }
2147c478bd9Sstevel@tonic-gate #endif /* PROTOENT_DATA */
2157c478bd9Sstevel@tonic-gate #else /* PROTO_R_RETURN */
2167c478bd9Sstevel@tonic-gate 	static int getprotoent_r_unknown_system = 0;
2177c478bd9Sstevel@tonic-gate #endif /* PROTO_R_RETURN */
2187c478bd9Sstevel@tonic-gate #endif /* !defined(_REENTRANT) || !defined(DO_PTHREADS) */
2199525b14bSRao Shoaib /*! \file */
220