1 /* 2 * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved. 3 */ 4 5 /* 6 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC") 7 * Copyright (c) 1996,1999 by Internet Software Consortium. 8 * 9 * Permission to use, copy, modify, and distribute this software for any 10 * purpose with or without fee is hereby granted, provided that the above 11 * copyright notice and this permission notice appear in all copies. 12 * 13 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES 14 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 15 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR 16 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 17 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 18 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT 19 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 20 */ 21 22 #if !defined(LINT) && !defined(CODECENTER) 23 static const char rcsid[] = "$Id: irs_data.c,v 1.12 2007/08/27 03:32:26 marka Exp $"; 24 #endif 25 26 #include "port_before.h" 27 28 #ifndef __BIND_NOSTATIC 29 30 #include <sys/types.h> 31 32 #include <netinet/in.h> 33 #include <arpa/nameser.h> 34 35 #include <resolv.h> 36 #include <stdio.h> 37 #include <string.h> 38 #include <isc/memcluster.h> 39 40 #ifdef DO_PTHREADS 41 #include <pthread.h> 42 #endif 43 44 #include <irs.h> 45 #include <stdlib.h> 46 47 #include "port_after.h" 48 49 #include "irs_data.h" 50 #undef _res 51 #if !(__GLIBC__ > 2 || __GLIBC__ == 2 && __GLIBC_MINOR__ >= 3) 52 #undef h_errno 53 extern int h_errno; 54 #endif 55 56 extern struct __res_state _res; 57 58 #ifdef DO_PTHREADS 59 static pthread_key_t key; 60 static int once = 0; 61 #else 62 static struct net_data *net_data; 63 #endif 64 65 void 66 irs_destroy(void) { 67 #ifndef DO_PTHREADS 68 if (net_data != NULL) 69 net_data_destroy(net_data); 70 net_data = NULL; 71 #endif 72 } 73 74 void 75 net_data_destroy(void *p) { 76 struct net_data *net_data = p; 77 78 res_ndestroy(net_data->res); 79 if (net_data->gr != NULL) { 80 (*net_data->gr->close)(net_data->gr); 81 net_data->gr = NULL; 82 } 83 if (net_data->pw != NULL) { 84 (*net_data->pw->close)(net_data->pw); 85 net_data->pw = NULL; 86 } 87 if (net_data->sv != NULL) { 88 (*net_data->sv->close)(net_data->sv); 89 net_data->sv = NULL; 90 } 91 if (net_data->pr != NULL) { 92 (*net_data->pr->close)(net_data->pr); 93 net_data->pr = NULL; 94 } 95 if (net_data->ho != NULL) { 96 (*net_data->ho->close)(net_data->ho); 97 net_data->ho = NULL; 98 } 99 if (net_data->nw != NULL) { 100 (*net_data->nw->close)(net_data->nw); 101 net_data->nw = NULL; 102 } 103 if (net_data->ng != NULL) { 104 (*net_data->ng->close)(net_data->ng); 105 net_data->ng = NULL; 106 } 107 if (net_data->ho_data != NULL) { 108 free(net_data->ho_data); 109 net_data->ho_data = NULL; 110 } 111 if (net_data->nw_data != NULL) { 112 free(net_data->nw_data); 113 net_data->nw_data = NULL; 114 } 115 116 (*net_data->irs->close)(net_data->irs); 117 memput(net_data, sizeof *net_data); 118 } 119 120 /*% 121 * applications that need a specific config file other than 122 * _PATH_IRS_CONF should call net_data_init directly rather than letting 123 * the various wrapper functions make the first call. - brister 124 */ 125 126 struct net_data * 127 net_data_init(const char *conf_file) { 128 #ifdef DO_PTHREADS 129 #ifndef LIBBIND_MUTEX_INITIALIZER 130 #define LIBBIND_MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER 131 #endif 132 static pthread_mutex_t keylock = LIBBIND_MUTEX_INITIALIZER; 133 struct net_data *net_data; 134 135 if (!once) { 136 if (pthread_mutex_lock(&keylock) != 0) 137 return (NULL); 138 if (!once) { 139 if (pthread_key_create(&key, net_data_destroy) != 0) { 140 (void)pthread_mutex_unlock(&keylock); 141 return (NULL); 142 } 143 once = 1; 144 } 145 if (pthread_mutex_unlock(&keylock) != 0) 146 return (NULL); 147 } 148 net_data = pthread_getspecific(key); 149 #endif 150 151 if (net_data == NULL) { 152 net_data = net_data_create(conf_file); 153 if (net_data == NULL) 154 return (NULL); 155 #ifdef DO_PTHREADS 156 if (pthread_setspecific(key, net_data) != 0) { 157 net_data_destroy(net_data); 158 return (NULL); 159 } 160 #endif 161 } 162 163 return (net_data); 164 } 165 166 struct net_data * 167 net_data_create(const char *conf_file) { 168 struct net_data *net_data; 169 170 net_data = memget(sizeof (struct net_data)); 171 if (net_data == NULL) 172 return (NULL); 173 memset(net_data, 0, sizeof (struct net_data)); 174 175 if ((net_data->irs = irs_gen_acc("", conf_file)) == NULL) { 176 memput(net_data, sizeof (struct net_data)); 177 return (NULL); 178 } 179 #ifndef DO_PTHREADS 180 (*net_data->irs->res_set)(net_data->irs, &_res, NULL); 181 #endif 182 183 net_data->res = (*net_data->irs->res_get)(net_data->irs); 184 if (net_data->res == NULL) { 185 (*net_data->irs->close)(net_data->irs); 186 memput(net_data, sizeof (struct net_data)); 187 return (NULL); 188 } 189 190 if ((net_data->res->options & RES_INIT) == 0U && 191 res_ninit(net_data->res) == -1) { 192 (*net_data->irs->close)(net_data->irs); 193 memput(net_data, sizeof (struct net_data)); 194 return (NULL); 195 } 196 197 return (net_data); 198 } 199 200 void 201 net_data_minimize(struct net_data *net_data) { 202 res_nclose(net_data->res); 203 } 204 205 #ifdef _REENTRANT 206 struct __res_state * 207 __res_state(void) { 208 /* NULL param here means use the default config file. */ 209 struct net_data *net_data = net_data_init(NULL); 210 if (net_data && net_data->res) 211 return (net_data->res); 212 213 return (&_res); 214 } 215 #else 216 #ifdef __linux 217 struct __res_state * 218 __res_state(void) { 219 return (&_res); 220 } 221 #endif 222 #endif 223 224 int * 225 __h_errno(void) { 226 /* NULL param here means use the default config file. */ 227 struct net_data *net_data = net_data_init(NULL); 228 if (net_data && net_data->res) 229 return (&net_data->res->res_h_errno); 230 #ifdef ORIGINAL_ISC_CODE 231 #if !(__GLIBC__ > 2 || __GLIBC__ == 2 && __GLIBC_MINOR__ >= 3) 232 return(&_res.res_h_errno); 233 #else 234 return (&h_errno); 235 #endif 236 #else 237 return (&h_errno); 238 #endif /* ORIGINAL_ISC_CODE */ 239 } 240 241 void 242 __h_errno_set(struct __res_state *res, int err) { 243 244 245 #if (__GLIBC__ > 2 || __GLIBC__ == 2 && __GLIBC_MINOR__ >= 3) 246 res->res_h_errno = err; 247 #else 248 h_errno = res->res_h_errno = err; 249 #endif 250 } 251 252 #endif /*__BIND_NOSTATIC*/ 253 254 /*! \file */ 255