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: getprotoent_r.c,v 1.6 2006/08/01 01:14:16 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 getprotoent_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 <port_after.h> 330Sstevel@tonic-gate 340Sstevel@tonic-gate #ifdef PROTO_R_RETURN 350Sstevel@tonic-gate 360Sstevel@tonic-gate static PROTO_R_RETURN 370Sstevel@tonic-gate copy_protoent(struct protoent *, struct protoent *, PROTO_R_COPY_ARGS); 380Sstevel@tonic-gate 390Sstevel@tonic-gate PROTO_R_RETURN 400Sstevel@tonic-gate getprotobyname_r(const char *name, struct protoent *pptr, PROTO_R_ARGS) { 410Sstevel@tonic-gate struct protoent *pe = getprotobyname(name); 420Sstevel@tonic-gate #ifdef PROTO_R_SETANSWER 430Sstevel@tonic-gate int n = 0; 440Sstevel@tonic-gate 450Sstevel@tonic-gate if (pe == NULL || (n = copy_protoent(pe, pptr, PROTO_R_COPY)) != 0) 460Sstevel@tonic-gate *answerp = NULL; 470Sstevel@tonic-gate else 480Sstevel@tonic-gate *answerp = pptr; 490Sstevel@tonic-gate 500Sstevel@tonic-gate return (n); 510Sstevel@tonic-gate #else 520Sstevel@tonic-gate if (pe == NULL) 530Sstevel@tonic-gate return (PROTO_R_BAD); 540Sstevel@tonic-gate 550Sstevel@tonic-gate return (copy_protoent(pe, pptr, PROTO_R_COPY)); 560Sstevel@tonic-gate #endif 570Sstevel@tonic-gate } 580Sstevel@tonic-gate 590Sstevel@tonic-gate PROTO_R_RETURN 600Sstevel@tonic-gate getprotobynumber_r(int proto, struct protoent *pptr, PROTO_R_ARGS) { 610Sstevel@tonic-gate struct protoent *pe = getprotobynumber(proto); 620Sstevel@tonic-gate #ifdef PROTO_R_SETANSWER 630Sstevel@tonic-gate int n = 0; 640Sstevel@tonic-gate 650Sstevel@tonic-gate if (pe == NULL || (n = copy_protoent(pe, pptr, PROTO_R_COPY)) != 0) 660Sstevel@tonic-gate *answerp = NULL; 670Sstevel@tonic-gate else 680Sstevel@tonic-gate *answerp = pptr; 690Sstevel@tonic-gate 700Sstevel@tonic-gate return (n); 710Sstevel@tonic-gate #else 720Sstevel@tonic-gate if (pe == NULL) 730Sstevel@tonic-gate return (PROTO_R_BAD); 740Sstevel@tonic-gate 750Sstevel@tonic-gate return (copy_protoent(pe, pptr, PROTO_R_COPY)); 760Sstevel@tonic-gate #endif 770Sstevel@tonic-gate } 780Sstevel@tonic-gate 79*11038SRao.Shoaib@Sun.COM /*% 800Sstevel@tonic-gate * These assume a single context is in operation per thread. 810Sstevel@tonic-gate * If this is not the case we will need to call irs directly 820Sstevel@tonic-gate * rather than through the base functions. 830Sstevel@tonic-gate */ 840Sstevel@tonic-gate 850Sstevel@tonic-gate PROTO_R_RETURN 860Sstevel@tonic-gate getprotoent_r(struct protoent *pptr, PROTO_R_ARGS) { 870Sstevel@tonic-gate struct protoent *pe = getprotoent(); 880Sstevel@tonic-gate #ifdef PROTO_R_SETANSWER 890Sstevel@tonic-gate int n = 0; 900Sstevel@tonic-gate 910Sstevel@tonic-gate if (pe == NULL || (n = copy_protoent(pe, pptr, PROTO_R_COPY)) != 0) 920Sstevel@tonic-gate *answerp = NULL; 930Sstevel@tonic-gate else 940Sstevel@tonic-gate *answerp = pptr; 950Sstevel@tonic-gate 960Sstevel@tonic-gate return (n); 970Sstevel@tonic-gate #else 980Sstevel@tonic-gate if (pe == NULL) 990Sstevel@tonic-gate return (PROTO_R_BAD); 1000Sstevel@tonic-gate 1010Sstevel@tonic-gate return (copy_protoent(pe, pptr, PROTO_R_COPY)); 1020Sstevel@tonic-gate #endif 1030Sstevel@tonic-gate } 1040Sstevel@tonic-gate 1050Sstevel@tonic-gate PROTO_R_SET_RETURN 1060Sstevel@tonic-gate #ifdef PROTO_R_ENT_ARGS 1070Sstevel@tonic-gate setprotoent_r(int stay_open, PROTO_R_ENT_ARGS) 1080Sstevel@tonic-gate #else 1090Sstevel@tonic-gate setprotoent_r(int stay_open) 1100Sstevel@tonic-gate #endif 1110Sstevel@tonic-gate { 112*11038SRao.Shoaib@Sun.COM #ifdef PROTO_R_ENT_UNUSED 113*11038SRao.Shoaib@Sun.COM PROTO_R_ENT_UNUSED; 114*11038SRao.Shoaib@Sun.COM #endif 1150Sstevel@tonic-gate setprotoent(stay_open); 1160Sstevel@tonic-gate #ifdef PROTO_R_SET_RESULT 1170Sstevel@tonic-gate return (PROTO_R_SET_RESULT); 1180Sstevel@tonic-gate #endif 1190Sstevel@tonic-gate } 1200Sstevel@tonic-gate 1210Sstevel@tonic-gate PROTO_R_END_RETURN 1220Sstevel@tonic-gate #ifdef PROTO_R_ENT_ARGS 1230Sstevel@tonic-gate endprotoent_r(PROTO_R_ENT_ARGS) 1240Sstevel@tonic-gate #else 1250Sstevel@tonic-gate endprotoent_r() 1260Sstevel@tonic-gate #endif 1270Sstevel@tonic-gate { 128*11038SRao.Shoaib@Sun.COM #ifdef PROTO_R_ENT_UNUSED 129*11038SRao.Shoaib@Sun.COM PROTO_R_ENT_UNUSED; 130*11038SRao.Shoaib@Sun.COM #endif 1310Sstevel@tonic-gate endprotoent(); 1320Sstevel@tonic-gate PROTO_R_END_RESULT(PROTO_R_OK); 1330Sstevel@tonic-gate } 1340Sstevel@tonic-gate 1350Sstevel@tonic-gate /* Private */ 1360Sstevel@tonic-gate 1370Sstevel@tonic-gate #ifndef PROTOENT_DATA 1380Sstevel@tonic-gate static PROTO_R_RETURN 1390Sstevel@tonic-gate copy_protoent(struct protoent *pe, struct protoent *pptr, PROTO_R_COPY_ARGS) { 1400Sstevel@tonic-gate char *cp; 1410Sstevel@tonic-gate int i, n; 1420Sstevel@tonic-gate int numptr, len; 1430Sstevel@tonic-gate 1440Sstevel@tonic-gate /* Find out the amount of space required to store the answer. */ 145*11038SRao.Shoaib@Sun.COM numptr = 1; /*%< NULL ptr */ 1460Sstevel@tonic-gate len = (char *)ALIGN(buf) - buf; 1470Sstevel@tonic-gate for (i = 0; pe->p_aliases[i]; i++, numptr++) { 1480Sstevel@tonic-gate len += strlen(pe->p_aliases[i]) + 1; 1490Sstevel@tonic-gate } 1500Sstevel@tonic-gate len += strlen(pe->p_name) + 1; 1510Sstevel@tonic-gate len += numptr * sizeof(char*); 1520Sstevel@tonic-gate 1530Sstevel@tonic-gate if (len > (int)buflen) { 1540Sstevel@tonic-gate errno = ERANGE; 1550Sstevel@tonic-gate return (PROTO_R_BAD); 1560Sstevel@tonic-gate } 1570Sstevel@tonic-gate 1580Sstevel@tonic-gate /* copy protocol value*/ 1590Sstevel@tonic-gate pptr->p_proto = pe->p_proto; 1600Sstevel@tonic-gate 1610Sstevel@tonic-gate cp = (char *)ALIGN(buf) + numptr * sizeof(char *); 1620Sstevel@tonic-gate 1630Sstevel@tonic-gate /* copy official name */ 1640Sstevel@tonic-gate n = strlen(pe->p_name) + 1; 1650Sstevel@tonic-gate strcpy(cp, pe->p_name); 1660Sstevel@tonic-gate pptr->p_name = cp; 1670Sstevel@tonic-gate cp += n; 1680Sstevel@tonic-gate 1690Sstevel@tonic-gate /* copy aliases */ 1700Sstevel@tonic-gate pptr->p_aliases = (char **)ALIGN(buf); 1710Sstevel@tonic-gate for (i = 0 ; pe->p_aliases[i]; i++) { 1720Sstevel@tonic-gate n = strlen(pe->p_aliases[i]) + 1; 1730Sstevel@tonic-gate strcpy(cp, pe->p_aliases[i]); 1740Sstevel@tonic-gate pptr->p_aliases[i] = cp; 1750Sstevel@tonic-gate cp += n; 1760Sstevel@tonic-gate } 1770Sstevel@tonic-gate pptr->p_aliases[i] = NULL; 1780Sstevel@tonic-gate 1790Sstevel@tonic-gate return (PROTO_R_OK); 1800Sstevel@tonic-gate } 1810Sstevel@tonic-gate #else /* !PROTOENT_DATA */ 1820Sstevel@tonic-gate static int 1830Sstevel@tonic-gate copy_protoent(struct protoent *pe, struct protoent *pptr, PROTO_R_COPY_ARGS) { 1840Sstevel@tonic-gate char *cp, *eob; 1850Sstevel@tonic-gate int i, n; 1860Sstevel@tonic-gate 1870Sstevel@tonic-gate /* copy protocol value */ 1880Sstevel@tonic-gate pptr->p_proto = pe->p_proto; 1890Sstevel@tonic-gate 1900Sstevel@tonic-gate /* copy official name */ 1910Sstevel@tonic-gate cp = pdptr->line; 1920Sstevel@tonic-gate eob = pdptr->line + sizeof(pdptr->line); 1930Sstevel@tonic-gate if ((n = strlen(pe->p_name) + 1) < (eob - cp)) { 1940Sstevel@tonic-gate strcpy(cp, pe->p_name); 1950Sstevel@tonic-gate pptr->p_name = cp; 1960Sstevel@tonic-gate cp += n; 1970Sstevel@tonic-gate } else { 1980Sstevel@tonic-gate return (-1); 1990Sstevel@tonic-gate } 2000Sstevel@tonic-gate 2010Sstevel@tonic-gate /* copy aliases */ 2020Sstevel@tonic-gate i = 0; 2030Sstevel@tonic-gate pptr->p_aliases = pdptr->proto_aliases; 2040Sstevel@tonic-gate while (pe->p_aliases[i] && i < (_MAXALIASES-1)) { 2050Sstevel@tonic-gate if ((n = strlen(pe->p_aliases[i]) + 1) < (eob - cp)) { 2060Sstevel@tonic-gate strcpy(cp, pe->p_aliases[i]); 2070Sstevel@tonic-gate pptr->p_aliases[i] = cp; 2080Sstevel@tonic-gate cp += n; 2090Sstevel@tonic-gate } else { 2100Sstevel@tonic-gate break; 2110Sstevel@tonic-gate } 2120Sstevel@tonic-gate i++; 2130Sstevel@tonic-gate } 2140Sstevel@tonic-gate pptr->p_aliases[i] = NULL; 2150Sstevel@tonic-gate 2160Sstevel@tonic-gate return (PROTO_R_OK); 2170Sstevel@tonic-gate } 2180Sstevel@tonic-gate #endif /* PROTOENT_DATA */ 2190Sstevel@tonic-gate #else /* PROTO_R_RETURN */ 2200Sstevel@tonic-gate static int getprotoent_r_unknown_system = 0; 2210Sstevel@tonic-gate #endif /* PROTO_R_RETURN */ 2220Sstevel@tonic-gate #endif /* !defined(_REENTRANT) || !defined(DO_PTHREADS) */ 223*11038SRao.Shoaib@Sun.COM /*! \file */ 224