1*0Sstevel@tonic-gate /* 2*0Sstevel@tonic-gate * Copyright 1999-2002 Sun Microsystems, Inc. All rights reserved. 3*0Sstevel@tonic-gate * Use is subject to license terms. 4*0Sstevel@tonic-gate */ 5*0Sstevel@tonic-gate 6*0Sstevel@tonic-gate /* 7*0Sstevel@tonic-gate * Copyright (c) 1998-1999 by Internet Software Consortium. 8*0Sstevel@tonic-gate * 9*0Sstevel@tonic-gate * Permission to use, copy, modify, and distribute this software for any 10*0Sstevel@tonic-gate * purpose with or without fee is hereby granted, provided that the above 11*0Sstevel@tonic-gate * copyright notice and this permission notice appear in all copies. 12*0Sstevel@tonic-gate * 13*0Sstevel@tonic-gate * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS 14*0Sstevel@tonic-gate * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES 15*0Sstevel@tonic-gate * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE 16*0Sstevel@tonic-gate * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL 17*0Sstevel@tonic-gate * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR 18*0Sstevel@tonic-gate * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS 19*0Sstevel@tonic-gate * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS 20*0Sstevel@tonic-gate * SOFTWARE. 21*0Sstevel@tonic-gate */ 22*0Sstevel@tonic-gate 23*0Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 24*0Sstevel@tonic-gate 25*0Sstevel@tonic-gate #if defined(LIBC_SCCS) && !defined(lint) 26*0Sstevel@tonic-gate static const char rcsid[] = "$Id: getnetent_r.c,v 8.6 2001/11/01 08:02:11 marka Exp $"; 27*0Sstevel@tonic-gate #endif /* LIBC_SCCS and not lint */ 28*0Sstevel@tonic-gate 29*0Sstevel@tonic-gate #include <port_before.h> 30*0Sstevel@tonic-gate #if !defined(_REENTRANT) || !defined(DO_PTHREADS) 31*0Sstevel@tonic-gate static int getnetent_r_not_required = 0; 32*0Sstevel@tonic-gate #else 33*0Sstevel@tonic-gate #include <errno.h> 34*0Sstevel@tonic-gate #include <string.h> 35*0Sstevel@tonic-gate #include <stdio.h> 36*0Sstevel@tonic-gate #include <sys/types.h> 37*0Sstevel@tonic-gate #include <netinet/in.h> 38*0Sstevel@tonic-gate #include <netdb.h> 39*0Sstevel@tonic-gate #include <sys/param.h> 40*0Sstevel@tonic-gate #include <port_after.h> 41*0Sstevel@tonic-gate 42*0Sstevel@tonic-gate #ifdef NET_R_RETURN 43*0Sstevel@tonic-gate 44*0Sstevel@tonic-gate static NET_R_RETURN 45*0Sstevel@tonic-gate copy_netent(struct netent *, struct netent *, NET_R_COPY_ARGS); 46*0Sstevel@tonic-gate 47*0Sstevel@tonic-gate NET_R_RETURN 48*0Sstevel@tonic-gate getnetbyname_r(const char *name, struct netent *nptr, NET_R_ARGS) { 49*0Sstevel@tonic-gate struct netent *ne = getnetbyname(name); 50*0Sstevel@tonic-gate #ifdef NET_R_SETANSWER 51*0Sstevel@tonic-gate int n = 0; 52*0Sstevel@tonic-gate 53*0Sstevel@tonic-gate if (ne == NULL || (n = copy_netent(ne, nptr, NET_R_COPY)) != 0) 54*0Sstevel@tonic-gate *answerp = NULL; 55*0Sstevel@tonic-gate else 56*0Sstevel@tonic-gate *answerp = ne; 57*0Sstevel@tonic-gate if (ne == NULL) 58*0Sstevel@tonic-gate *h_errnop = h_errno; 59*0Sstevel@tonic-gate return (n); 60*0Sstevel@tonic-gate #else 61*0Sstevel@tonic-gate if (ne == NULL) 62*0Sstevel@tonic-gate return (NET_R_BAD); 63*0Sstevel@tonic-gate 64*0Sstevel@tonic-gate return (copy_netent(ne, nptr, NET_R_COPY)); 65*0Sstevel@tonic-gate #endif 66*0Sstevel@tonic-gate } 67*0Sstevel@tonic-gate 68*0Sstevel@tonic-gate #ifndef GETNETBYADDR_ADDR_T 69*0Sstevel@tonic-gate #define GETNETBYADDR_ADDR_T long 70*0Sstevel@tonic-gate #endif 71*0Sstevel@tonic-gate NET_R_RETURN 72*0Sstevel@tonic-gate getnetbyaddr_r(GETNETBYADDR_ADDR_T addr, int type, struct netent *nptr, NET_R_ARGS) { 73*0Sstevel@tonic-gate struct netent *ne = getnetbyaddr(addr, type); 74*0Sstevel@tonic-gate #ifdef NET_R_SETANSWER 75*0Sstevel@tonic-gate int n = 0; 76*0Sstevel@tonic-gate 77*0Sstevel@tonic-gate if (ne == NULL || (n = copy_netent(ne, nptr, NET_R_COPY)) != 0) 78*0Sstevel@tonic-gate *answerp = NULL; 79*0Sstevel@tonic-gate else 80*0Sstevel@tonic-gate *answerp = ne; 81*0Sstevel@tonic-gate if (ne == NULL) 82*0Sstevel@tonic-gate *h_errnop = h_errno; 83*0Sstevel@tonic-gate return (n); 84*0Sstevel@tonic-gate #else 85*0Sstevel@tonic-gate 86*0Sstevel@tonic-gate if (ne == NULL) 87*0Sstevel@tonic-gate return (NET_R_BAD); 88*0Sstevel@tonic-gate 89*0Sstevel@tonic-gate return (copy_netent(ne, nptr, NET_R_COPY)); 90*0Sstevel@tonic-gate #endif 91*0Sstevel@tonic-gate } 92*0Sstevel@tonic-gate 93*0Sstevel@tonic-gate /* 94*0Sstevel@tonic-gate * These assume a single context is in operation per thread. 95*0Sstevel@tonic-gate * If this is not the case we will need to call irs directly 96*0Sstevel@tonic-gate * rather than through the base functions. 97*0Sstevel@tonic-gate */ 98*0Sstevel@tonic-gate 99*0Sstevel@tonic-gate NET_R_RETURN 100*0Sstevel@tonic-gate getnetent_r(struct netent *nptr, NET_R_ARGS) { 101*0Sstevel@tonic-gate struct netent *ne = getnetent(); 102*0Sstevel@tonic-gate #ifdef NET_R_SETANSWER 103*0Sstevel@tonic-gate int n = 0; 104*0Sstevel@tonic-gate 105*0Sstevel@tonic-gate if (ne == NULL || (n = copy_netent(ne, nptr, NET_R_COPY)) != 0) 106*0Sstevel@tonic-gate *answerp = NULL; 107*0Sstevel@tonic-gate else 108*0Sstevel@tonic-gate *answerp = ne; 109*0Sstevel@tonic-gate if (ne == NULL) 110*0Sstevel@tonic-gate *h_errnop = h_errno; 111*0Sstevel@tonic-gate return (n); 112*0Sstevel@tonic-gate #else 113*0Sstevel@tonic-gate 114*0Sstevel@tonic-gate if (ne == NULL) 115*0Sstevel@tonic-gate return (NET_R_BAD); 116*0Sstevel@tonic-gate 117*0Sstevel@tonic-gate return (copy_netent(ne, nptr, NET_R_COPY)); 118*0Sstevel@tonic-gate #endif 119*0Sstevel@tonic-gate } 120*0Sstevel@tonic-gate 121*0Sstevel@tonic-gate NET_R_SET_RETURN 122*0Sstevel@tonic-gate #ifdef NET_R_ENT_ARGS 123*0Sstevel@tonic-gate setnetent_r(int stay_open, NET_R_ENT_ARGS) 124*0Sstevel@tonic-gate #else 125*0Sstevel@tonic-gate setnetent_r(int stay_open) 126*0Sstevel@tonic-gate #endif 127*0Sstevel@tonic-gate { 128*0Sstevel@tonic-gate setnetent(stay_open); 129*0Sstevel@tonic-gate #ifdef NET_R_SET_RESULT 130*0Sstevel@tonic-gate return (NET_R_SET_RESULT); 131*0Sstevel@tonic-gate #endif 132*0Sstevel@tonic-gate } 133*0Sstevel@tonic-gate 134*0Sstevel@tonic-gate NET_R_END_RETURN 135*0Sstevel@tonic-gate #ifdef NET_R_ENT_ARGS 136*0Sstevel@tonic-gate endnetent_r(NET_R_ENT_ARGS) 137*0Sstevel@tonic-gate #else 138*0Sstevel@tonic-gate endnetent_r() 139*0Sstevel@tonic-gate #endif 140*0Sstevel@tonic-gate { 141*0Sstevel@tonic-gate endnetent(); 142*0Sstevel@tonic-gate NET_R_END_RESULT(NET_R_OK); 143*0Sstevel@tonic-gate } 144*0Sstevel@tonic-gate 145*0Sstevel@tonic-gate /* Private */ 146*0Sstevel@tonic-gate 147*0Sstevel@tonic-gate #ifndef NETENT_DATA 148*0Sstevel@tonic-gate static NET_R_RETURN 149*0Sstevel@tonic-gate copy_netent(struct netent *ne, struct netent *nptr, NET_R_COPY_ARGS) { 150*0Sstevel@tonic-gate char *cp; 151*0Sstevel@tonic-gate int i, n; 152*0Sstevel@tonic-gate int numptr, len; 153*0Sstevel@tonic-gate 154*0Sstevel@tonic-gate /* Find out the amount of space required to store the answer. */ 155*0Sstevel@tonic-gate numptr = 1; /* NULL ptr */ 156*0Sstevel@tonic-gate len = (char *)ALIGN(buf) - buf; 157*0Sstevel@tonic-gate for (i = 0; ne->n_aliases[i]; i++, numptr++) { 158*0Sstevel@tonic-gate len += strlen(ne->n_aliases[i]) + 1; 159*0Sstevel@tonic-gate } 160*0Sstevel@tonic-gate len += strlen(ne->n_name) + 1; 161*0Sstevel@tonic-gate len += numptr * sizeof(char*); 162*0Sstevel@tonic-gate 163*0Sstevel@tonic-gate if (len > (int)buflen) { 164*0Sstevel@tonic-gate errno = ERANGE; 165*0Sstevel@tonic-gate return (NET_R_BAD); 166*0Sstevel@tonic-gate } 167*0Sstevel@tonic-gate 168*0Sstevel@tonic-gate /* copy net value and type */ 169*0Sstevel@tonic-gate nptr->n_addrtype = ne->n_addrtype; 170*0Sstevel@tonic-gate nptr->n_net = ne->n_net; 171*0Sstevel@tonic-gate 172*0Sstevel@tonic-gate cp = (char *)ALIGN(buf) + numptr * sizeof(char *); 173*0Sstevel@tonic-gate 174*0Sstevel@tonic-gate /* copy official name */ 175*0Sstevel@tonic-gate n = strlen(ne->n_name) + 1; 176*0Sstevel@tonic-gate strcpy(cp, ne->n_name); 177*0Sstevel@tonic-gate nptr->n_name = cp; 178*0Sstevel@tonic-gate cp += n; 179*0Sstevel@tonic-gate 180*0Sstevel@tonic-gate /* copy aliases */ 181*0Sstevel@tonic-gate nptr->n_aliases = (char **)ALIGN(buf); 182*0Sstevel@tonic-gate for (i = 0 ; ne->n_aliases[i]; i++) { 183*0Sstevel@tonic-gate n = strlen(ne->n_aliases[i]) + 1; 184*0Sstevel@tonic-gate strcpy(cp, ne->n_aliases[i]); 185*0Sstevel@tonic-gate nptr->n_aliases[i] = cp; 186*0Sstevel@tonic-gate cp += n; 187*0Sstevel@tonic-gate } 188*0Sstevel@tonic-gate nptr->n_aliases[i] = NULL; 189*0Sstevel@tonic-gate 190*0Sstevel@tonic-gate return (NET_R_OK); 191*0Sstevel@tonic-gate } 192*0Sstevel@tonic-gate #else /* !NETENT_DATA */ 193*0Sstevel@tonic-gate static int 194*0Sstevel@tonic-gate copy_netent(struct netent *ne, struct netent *nptr, NET_R_COPY_ARGS) { 195*0Sstevel@tonic-gate char *cp, *eob; 196*0Sstevel@tonic-gate int i, n; 197*0Sstevel@tonic-gate 198*0Sstevel@tonic-gate /* copy net value and type */ 199*0Sstevel@tonic-gate nptr->n_addrtype = ne->n_addrtype; 200*0Sstevel@tonic-gate nptr->n_net = ne->n_net; 201*0Sstevel@tonic-gate 202*0Sstevel@tonic-gate /* copy official name */ 203*0Sstevel@tonic-gate cp = ndptr->line; 204*0Sstevel@tonic-gate eob = ndptr->line + sizeof(ndptr->line); 205*0Sstevel@tonic-gate if ((n = strlen(ne->n_name) + 1) < (eob - cp)) { 206*0Sstevel@tonic-gate strcpy(cp, ne->n_name); 207*0Sstevel@tonic-gate nptr->n_name = cp; 208*0Sstevel@tonic-gate cp += n; 209*0Sstevel@tonic-gate } else { 210*0Sstevel@tonic-gate return (-1); 211*0Sstevel@tonic-gate } 212*0Sstevel@tonic-gate 213*0Sstevel@tonic-gate /* copy aliases */ 214*0Sstevel@tonic-gate i = 0; 215*0Sstevel@tonic-gate nptr->n_aliases = ndptr->net_aliases; 216*0Sstevel@tonic-gate while (ne->n_aliases[i] && i < (_MAXALIASES-1)) { 217*0Sstevel@tonic-gate if ((n = strlen(ne->n_aliases[i]) + 1) < (eob - cp)) { 218*0Sstevel@tonic-gate strcpy(cp, ne->n_aliases[i]); 219*0Sstevel@tonic-gate nptr->n_aliases[i] = cp; 220*0Sstevel@tonic-gate cp += n; 221*0Sstevel@tonic-gate } else { 222*0Sstevel@tonic-gate break; 223*0Sstevel@tonic-gate } 224*0Sstevel@tonic-gate i++; 225*0Sstevel@tonic-gate } 226*0Sstevel@tonic-gate nptr->n_aliases[i] = NULL; 227*0Sstevel@tonic-gate 228*0Sstevel@tonic-gate return (NET_R_OK); 229*0Sstevel@tonic-gate } 230*0Sstevel@tonic-gate #endif /* !NETENT_DATA */ 231*0Sstevel@tonic-gate #else /* NET_R_RETURN */ 232*0Sstevel@tonic-gate static int getnetent_r_unknown_system = 0; 233*0Sstevel@tonic-gate #endif /* NET_R_RETURN */ 234*0Sstevel@tonic-gate #endif /* !defined(_REENTRANT) || !defined(DO_PTHREADS) */ 235