1*0Sstevel@tonic-gate /* 2*0Sstevel@tonic-gate * Copyright 2003 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) 1996,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(LINT) && !defined(CODECENTER) 26*0Sstevel@tonic-gate static const char rcsid[] = "$Id: irs_data.c,v 1.22 2003/06/20 07:09:33 marka Exp $"; 27*0Sstevel@tonic-gate #endif 28*0Sstevel@tonic-gate 29*0Sstevel@tonic-gate #include "port_before.h" 30*0Sstevel@tonic-gate 31*0Sstevel@tonic-gate #ifndef __BIND_NOSTATIC 32*0Sstevel@tonic-gate 33*0Sstevel@tonic-gate #include <sys/types.h> 34*0Sstevel@tonic-gate 35*0Sstevel@tonic-gate #include <netinet/in.h> 36*0Sstevel@tonic-gate #include <arpa/nameser.h> 37*0Sstevel@tonic-gate 38*0Sstevel@tonic-gate #include <resolv.h> 39*0Sstevel@tonic-gate #include <stdio.h> 40*0Sstevel@tonic-gate #include <string.h> 41*0Sstevel@tonic-gate #include <isc/memcluster.h> 42*0Sstevel@tonic-gate #include <stdlib.h> 43*0Sstevel@tonic-gate 44*0Sstevel@tonic-gate #ifdef DO_PTHREADS 45*0Sstevel@tonic-gate #include <pthread.h> 46*0Sstevel@tonic-gate #endif 47*0Sstevel@tonic-gate 48*0Sstevel@tonic-gate #include <irs.h> 49*0Sstevel@tonic-gate 50*0Sstevel@tonic-gate #include "port_after.h" 51*0Sstevel@tonic-gate 52*0Sstevel@tonic-gate #include "irs_data.h" 53*0Sstevel@tonic-gate #undef _res 54*0Sstevel@tonic-gate #undef h_errno 55*0Sstevel@tonic-gate 56*0Sstevel@tonic-gate extern struct __res_state _res; 57*0Sstevel@tonic-gate extern int h_errno; 58*0Sstevel@tonic-gate 59*0Sstevel@tonic-gate #ifdef DO_PTHREADS 60*0Sstevel@tonic-gate static pthread_key_t key; 61*0Sstevel@tonic-gate static int once = 0; 62*0Sstevel@tonic-gate #else 63*0Sstevel@tonic-gate static struct net_data *net_data; 64*0Sstevel@tonic-gate #endif 65*0Sstevel@tonic-gate 66*0Sstevel@tonic-gate void 67*0Sstevel@tonic-gate irs_destroy(void) { 68*0Sstevel@tonic-gate #ifndef DO_PTHREADS 69*0Sstevel@tonic-gate if (net_data != NULL) 70*0Sstevel@tonic-gate net_data_destroy(net_data); 71*0Sstevel@tonic-gate net_data = NULL; 72*0Sstevel@tonic-gate #endif 73*0Sstevel@tonic-gate } 74*0Sstevel@tonic-gate 75*0Sstevel@tonic-gate void 76*0Sstevel@tonic-gate net_data_destroy(void *p) { 77*0Sstevel@tonic-gate struct net_data *net_data = p; 78*0Sstevel@tonic-gate 79*0Sstevel@tonic-gate res_ndestroy(net_data->res); 80*0Sstevel@tonic-gate if (net_data->gr != NULL) { 81*0Sstevel@tonic-gate (*net_data->gr->close)(net_data->gr); 82*0Sstevel@tonic-gate net_data->gr = NULL; 83*0Sstevel@tonic-gate } 84*0Sstevel@tonic-gate if (net_data->pw != NULL) { 85*0Sstevel@tonic-gate (*net_data->pw->close)(net_data->pw); 86*0Sstevel@tonic-gate net_data->pw = NULL; 87*0Sstevel@tonic-gate } 88*0Sstevel@tonic-gate if (net_data->sv != NULL) { 89*0Sstevel@tonic-gate (*net_data->sv->close)(net_data->sv); 90*0Sstevel@tonic-gate net_data->sv = NULL; 91*0Sstevel@tonic-gate } 92*0Sstevel@tonic-gate if (net_data->pr != NULL) { 93*0Sstevel@tonic-gate (*net_data->pr->close)(net_data->pr); 94*0Sstevel@tonic-gate net_data->pr = NULL; 95*0Sstevel@tonic-gate } 96*0Sstevel@tonic-gate if (net_data->ho != NULL) { 97*0Sstevel@tonic-gate (*net_data->ho->close)(net_data->ho); 98*0Sstevel@tonic-gate net_data->ho = NULL; 99*0Sstevel@tonic-gate } 100*0Sstevel@tonic-gate if (net_data->nw != NULL) { 101*0Sstevel@tonic-gate (*net_data->nw->close)(net_data->nw); 102*0Sstevel@tonic-gate net_data->nw = NULL; 103*0Sstevel@tonic-gate } 104*0Sstevel@tonic-gate if (net_data->ng != NULL) { 105*0Sstevel@tonic-gate (*net_data->ng->close)(net_data->ng); 106*0Sstevel@tonic-gate net_data->ng = NULL; 107*0Sstevel@tonic-gate } 108*0Sstevel@tonic-gate if (net_data->ho_data != NULL) { 109*0Sstevel@tonic-gate free(net_data->ho_data); 110*0Sstevel@tonic-gate net_data->ho_data = NULL; 111*0Sstevel@tonic-gate } 112*0Sstevel@tonic-gate if (net_data->nw_data != NULL) { 113*0Sstevel@tonic-gate free(net_data->nw_data); 114*0Sstevel@tonic-gate net_data->nw_data = NULL; 115*0Sstevel@tonic-gate } 116*0Sstevel@tonic-gate 117*0Sstevel@tonic-gate (*net_data->irs->close)(net_data->irs); 118*0Sstevel@tonic-gate memput(net_data, sizeof *net_data); 119*0Sstevel@tonic-gate } 120*0Sstevel@tonic-gate 121*0Sstevel@tonic-gate /* applications that need a specific config file other than 122*0Sstevel@tonic-gate * _PATH_IRS_CONF should call net_data_init directly rather than letting 123*0Sstevel@tonic-gate * the various wrapper functions make the first call. - brister 124*0Sstevel@tonic-gate */ 125*0Sstevel@tonic-gate 126*0Sstevel@tonic-gate struct net_data * 127*0Sstevel@tonic-gate net_data_init(const char *conf_file) { 128*0Sstevel@tonic-gate #ifdef DO_PTHREADS 129*0Sstevel@tonic-gate static pthread_mutex_t keylock = PTHREAD_MUTEX_INITIALIZER; 130*0Sstevel@tonic-gate struct net_data *net_data; 131*0Sstevel@tonic-gate 132*0Sstevel@tonic-gate if (!once) { 133*0Sstevel@tonic-gate pthread_mutex_lock(&keylock); 134*0Sstevel@tonic-gate if (!once++) 135*0Sstevel@tonic-gate pthread_key_create(&key, net_data_destroy); 136*0Sstevel@tonic-gate pthread_mutex_unlock(&keylock); 137*0Sstevel@tonic-gate } 138*0Sstevel@tonic-gate net_data = pthread_getspecific(key); 139*0Sstevel@tonic-gate #endif 140*0Sstevel@tonic-gate 141*0Sstevel@tonic-gate if (net_data == NULL) { 142*0Sstevel@tonic-gate net_data = net_data_create(conf_file); 143*0Sstevel@tonic-gate if (net_data == NULL) 144*0Sstevel@tonic-gate return (NULL); 145*0Sstevel@tonic-gate #ifdef DO_PTHREADS 146*0Sstevel@tonic-gate pthread_setspecific(key, net_data); 147*0Sstevel@tonic-gate #endif 148*0Sstevel@tonic-gate } 149*0Sstevel@tonic-gate 150*0Sstevel@tonic-gate return (net_data); 151*0Sstevel@tonic-gate } 152*0Sstevel@tonic-gate 153*0Sstevel@tonic-gate struct net_data * 154*0Sstevel@tonic-gate net_data_create(const char *conf_file) { 155*0Sstevel@tonic-gate struct net_data *net_data; 156*0Sstevel@tonic-gate 157*0Sstevel@tonic-gate net_data = memget(sizeof (struct net_data)); 158*0Sstevel@tonic-gate if (net_data == NULL) 159*0Sstevel@tonic-gate return (NULL); 160*0Sstevel@tonic-gate memset(net_data, 0, sizeof (struct net_data)); 161*0Sstevel@tonic-gate 162*0Sstevel@tonic-gate if ((net_data->irs = irs_gen_acc("", conf_file)) == NULL) { 163*0Sstevel@tonic-gate memput(net_data, sizeof (struct net_data)); 164*0Sstevel@tonic-gate return (NULL); 165*0Sstevel@tonic-gate } 166*0Sstevel@tonic-gate #ifndef DO_PTHREADS 167*0Sstevel@tonic-gate (*net_data->irs->res_set)(net_data->irs, &_res, NULL); 168*0Sstevel@tonic-gate #endif 169*0Sstevel@tonic-gate 170*0Sstevel@tonic-gate net_data->res = (*net_data->irs->res_get)(net_data->irs); 171*0Sstevel@tonic-gate if (net_data->res == NULL) { 172*0Sstevel@tonic-gate (*net_data->irs->close)(net_data->irs); 173*0Sstevel@tonic-gate memput(net_data, sizeof (struct net_data)); 174*0Sstevel@tonic-gate return (NULL); 175*0Sstevel@tonic-gate } 176*0Sstevel@tonic-gate 177*0Sstevel@tonic-gate if ((net_data->res->options & RES_INIT) == 0 && 178*0Sstevel@tonic-gate res_ninit(net_data->res) == -1) { 179*0Sstevel@tonic-gate (*net_data->irs->close)(net_data->irs); 180*0Sstevel@tonic-gate memput(net_data, sizeof (struct net_data)); 181*0Sstevel@tonic-gate return (NULL); 182*0Sstevel@tonic-gate } 183*0Sstevel@tonic-gate 184*0Sstevel@tonic-gate return (net_data); 185*0Sstevel@tonic-gate } 186*0Sstevel@tonic-gate 187*0Sstevel@tonic-gate 188*0Sstevel@tonic-gate 189*0Sstevel@tonic-gate void 190*0Sstevel@tonic-gate net_data_minimize(struct net_data *net_data) { 191*0Sstevel@tonic-gate res_nclose(net_data->res); 192*0Sstevel@tonic-gate } 193*0Sstevel@tonic-gate 194*0Sstevel@tonic-gate #ifdef _REENTRANT 195*0Sstevel@tonic-gate struct __res_state * 196*0Sstevel@tonic-gate __res_state(void) { 197*0Sstevel@tonic-gate /* NULL param here means use the default config file. */ 198*0Sstevel@tonic-gate struct net_data *net_data = net_data_init(NULL); 199*0Sstevel@tonic-gate if (net_data && net_data->res) 200*0Sstevel@tonic-gate return (net_data->res); 201*0Sstevel@tonic-gate 202*0Sstevel@tonic-gate return (&_res); 203*0Sstevel@tonic-gate } 204*0Sstevel@tonic-gate #endif 205*0Sstevel@tonic-gate 206*0Sstevel@tonic-gate int * 207*0Sstevel@tonic-gate __h_errno(void) { 208*0Sstevel@tonic-gate /* NULL param here means use the default config file. */ 209*0Sstevel@tonic-gate struct net_data *net_data = net_data_init(NULL); 210*0Sstevel@tonic-gate if (net_data && net_data->res) 211*0Sstevel@tonic-gate return (&net_data->res->res_h_errno); 212*0Sstevel@tonic-gate return (&h_errno); 213*0Sstevel@tonic-gate } 214*0Sstevel@tonic-gate 215*0Sstevel@tonic-gate void 216*0Sstevel@tonic-gate __h_errno_set(struct __res_state *res, int err) { 217*0Sstevel@tonic-gate 218*0Sstevel@tonic-gate h_errno = res->res_h_errno = err; 219*0Sstevel@tonic-gate } 220*0Sstevel@tonic-gate 221*0Sstevel@tonic-gate #endif /*__BIND_NOSTATIC*/ 222