17c478bd9Sstevel@tonic-gate /*
2*4e567b44SStacey Marshall * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved.
3*4e567b44SStacey Marshall */
4*4e567b44SStacey Marshall
5*4e567b44SStacey Marshall /*
69525b14bSRao Shoaib * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
77c478bd9Sstevel@tonic-gate * Copyright (c) 1996,1999 by Internet Software Consortium.
87c478bd9Sstevel@tonic-gate *
97c478bd9Sstevel@tonic-gate * Permission to use, copy, modify, and distribute this software for any
107c478bd9Sstevel@tonic-gate * purpose with or without fee is hereby granted, provided that the above
117c478bd9Sstevel@tonic-gate * copyright notice and this permission notice appear in all copies.
127c478bd9Sstevel@tonic-gate *
139525b14bSRao Shoaib * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
149525b14bSRao Shoaib * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
159525b14bSRao Shoaib * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
169525b14bSRao Shoaib * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
179525b14bSRao Shoaib * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
189525b14bSRao Shoaib * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
199525b14bSRao Shoaib * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
207c478bd9Sstevel@tonic-gate */
217c478bd9Sstevel@tonic-gate
227c478bd9Sstevel@tonic-gate #include "port_before.h"
237c478bd9Sstevel@tonic-gate
247c478bd9Sstevel@tonic-gate #ifndef __BIND_NOSTATIC
257c478bd9Sstevel@tonic-gate
267c478bd9Sstevel@tonic-gate #include <sys/types.h>
277c478bd9Sstevel@tonic-gate
287c478bd9Sstevel@tonic-gate #include <netinet/in.h>
297c478bd9Sstevel@tonic-gate #include <arpa/nameser.h>
307c478bd9Sstevel@tonic-gate
317c478bd9Sstevel@tonic-gate #include <resolv.h>
327c478bd9Sstevel@tonic-gate #include <stdio.h>
337c478bd9Sstevel@tonic-gate #include <string.h>
347c478bd9Sstevel@tonic-gate #include <isc/memcluster.h>
357c478bd9Sstevel@tonic-gate
367c478bd9Sstevel@tonic-gate #ifdef DO_PTHREADS
377c478bd9Sstevel@tonic-gate #include <pthread.h>
387c478bd9Sstevel@tonic-gate #endif
397c478bd9Sstevel@tonic-gate
407c478bd9Sstevel@tonic-gate #include <irs.h>
419525b14bSRao Shoaib #include <stdlib.h>
427c478bd9Sstevel@tonic-gate
437c478bd9Sstevel@tonic-gate #include "port_after.h"
447c478bd9Sstevel@tonic-gate
457c478bd9Sstevel@tonic-gate #include "irs_data.h"
467c478bd9Sstevel@tonic-gate #undef _res
479525b14bSRao Shoaib #if !(__GLIBC__ > 2 || __GLIBC__ == 2 && __GLIBC_MINOR__ >= 3)
487c478bd9Sstevel@tonic-gate #undef h_errno
499525b14bSRao Shoaib extern int h_errno;
509525b14bSRao Shoaib #endif
517c478bd9Sstevel@tonic-gate
527c478bd9Sstevel@tonic-gate extern struct __res_state _res;
537c478bd9Sstevel@tonic-gate
547c478bd9Sstevel@tonic-gate #ifdef DO_PTHREADS
559525b14bSRao Shoaib static pthread_key_t key;
569525b14bSRao Shoaib static int once = 0;
577c478bd9Sstevel@tonic-gate #else
589525b14bSRao Shoaib static struct net_data *net_data;
597c478bd9Sstevel@tonic-gate #endif
607c478bd9Sstevel@tonic-gate
617c478bd9Sstevel@tonic-gate void
irs_destroy(void)627c478bd9Sstevel@tonic-gate irs_destroy(void) {
637c478bd9Sstevel@tonic-gate #ifndef DO_PTHREADS
647c478bd9Sstevel@tonic-gate if (net_data != NULL)
657c478bd9Sstevel@tonic-gate net_data_destroy(net_data);
667c478bd9Sstevel@tonic-gate net_data = NULL;
677c478bd9Sstevel@tonic-gate #endif
687c478bd9Sstevel@tonic-gate }
697c478bd9Sstevel@tonic-gate
707c478bd9Sstevel@tonic-gate void
net_data_destroy(void * p)717c478bd9Sstevel@tonic-gate net_data_destroy(void *p) {
727c478bd9Sstevel@tonic-gate struct net_data *net_data = p;
737c478bd9Sstevel@tonic-gate
747c478bd9Sstevel@tonic-gate res_ndestroy(net_data->res);
757c478bd9Sstevel@tonic-gate if (net_data->gr != NULL) {
767c478bd9Sstevel@tonic-gate (*net_data->gr->close)(net_data->gr);
777c478bd9Sstevel@tonic-gate net_data->gr = NULL;
787c478bd9Sstevel@tonic-gate }
797c478bd9Sstevel@tonic-gate if (net_data->pw != NULL) {
807c478bd9Sstevel@tonic-gate (*net_data->pw->close)(net_data->pw);
817c478bd9Sstevel@tonic-gate net_data->pw = NULL;
827c478bd9Sstevel@tonic-gate }
837c478bd9Sstevel@tonic-gate if (net_data->sv != NULL) {
847c478bd9Sstevel@tonic-gate (*net_data->sv->close)(net_data->sv);
857c478bd9Sstevel@tonic-gate net_data->sv = NULL;
867c478bd9Sstevel@tonic-gate }
877c478bd9Sstevel@tonic-gate if (net_data->pr != NULL) {
887c478bd9Sstevel@tonic-gate (*net_data->pr->close)(net_data->pr);
897c478bd9Sstevel@tonic-gate net_data->pr = NULL;
907c478bd9Sstevel@tonic-gate }
917c478bd9Sstevel@tonic-gate if (net_data->ho != NULL) {
927c478bd9Sstevel@tonic-gate (*net_data->ho->close)(net_data->ho);
937c478bd9Sstevel@tonic-gate net_data->ho = NULL;
947c478bd9Sstevel@tonic-gate }
957c478bd9Sstevel@tonic-gate if (net_data->nw != NULL) {
967c478bd9Sstevel@tonic-gate (*net_data->nw->close)(net_data->nw);
977c478bd9Sstevel@tonic-gate net_data->nw = NULL;
987c478bd9Sstevel@tonic-gate }
997c478bd9Sstevel@tonic-gate if (net_data->ng != NULL) {
1007c478bd9Sstevel@tonic-gate (*net_data->ng->close)(net_data->ng);
1017c478bd9Sstevel@tonic-gate net_data->ng = NULL;
1027c478bd9Sstevel@tonic-gate }
1037c478bd9Sstevel@tonic-gate if (net_data->ho_data != NULL) {
1047c478bd9Sstevel@tonic-gate free(net_data->ho_data);
1057c478bd9Sstevel@tonic-gate net_data->ho_data = NULL;
1067c478bd9Sstevel@tonic-gate }
1077c478bd9Sstevel@tonic-gate if (net_data->nw_data != NULL) {
1087c478bd9Sstevel@tonic-gate free(net_data->nw_data);
1097c478bd9Sstevel@tonic-gate net_data->nw_data = NULL;
1107c478bd9Sstevel@tonic-gate }
1117c478bd9Sstevel@tonic-gate
1127c478bd9Sstevel@tonic-gate (*net_data->irs->close)(net_data->irs);
1137c478bd9Sstevel@tonic-gate memput(net_data, sizeof *net_data);
1147c478bd9Sstevel@tonic-gate }
1157c478bd9Sstevel@tonic-gate
1169525b14bSRao Shoaib /*%
1179525b14bSRao Shoaib * applications that need a specific config file other than
1187c478bd9Sstevel@tonic-gate * _PATH_IRS_CONF should call net_data_init directly rather than letting
1197c478bd9Sstevel@tonic-gate * the various wrapper functions make the first call. - brister
1207c478bd9Sstevel@tonic-gate */
1217c478bd9Sstevel@tonic-gate
1227c478bd9Sstevel@tonic-gate struct net_data *
net_data_init(const char * conf_file)1237c478bd9Sstevel@tonic-gate net_data_init(const char *conf_file) {
1247c478bd9Sstevel@tonic-gate #ifdef DO_PTHREADS
1259525b14bSRao Shoaib #ifndef LIBBIND_MUTEX_INITIALIZER
1269525b14bSRao Shoaib #define LIBBIND_MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER
1279525b14bSRao Shoaib #endif
1289525b14bSRao Shoaib static pthread_mutex_t keylock = LIBBIND_MUTEX_INITIALIZER;
1297c478bd9Sstevel@tonic-gate struct net_data *net_data;
1307c478bd9Sstevel@tonic-gate
1319525b14bSRao Shoaib if (!once) {
1329525b14bSRao Shoaib if (pthread_mutex_lock(&keylock) != 0)
133168c2130Sjs198686 return (NULL);
1349525b14bSRao Shoaib if (!once) {
1359525b14bSRao Shoaib if (pthread_key_create(&key, net_data_destroy) != 0) {
1369525b14bSRao Shoaib (void)pthread_mutex_unlock(&keylock);
1379525b14bSRao Shoaib return (NULL);
1389525b14bSRao Shoaib }
1399525b14bSRao Shoaib once = 1;
1409525b14bSRao Shoaib }
1419525b14bSRao Shoaib if (pthread_mutex_unlock(&keylock) != 0)
1429525b14bSRao Shoaib return (NULL);
1439525b14bSRao Shoaib }
1447c478bd9Sstevel@tonic-gate net_data = pthread_getspecific(key);
1457c478bd9Sstevel@tonic-gate #endif
1467c478bd9Sstevel@tonic-gate
1477c478bd9Sstevel@tonic-gate if (net_data == NULL) {
1487c478bd9Sstevel@tonic-gate net_data = net_data_create(conf_file);
1497c478bd9Sstevel@tonic-gate if (net_data == NULL)
1507c478bd9Sstevel@tonic-gate return (NULL);
1517c478bd9Sstevel@tonic-gate #ifdef DO_PTHREADS
152168c2130Sjs198686 if (pthread_setspecific(key, net_data) != 0) {
153168c2130Sjs198686 net_data_destroy(net_data);
154168c2130Sjs198686 return (NULL);
155168c2130Sjs198686 }
1567c478bd9Sstevel@tonic-gate #endif
1577c478bd9Sstevel@tonic-gate }
1587c478bd9Sstevel@tonic-gate
1597c478bd9Sstevel@tonic-gate return (net_data);
1607c478bd9Sstevel@tonic-gate }
1617c478bd9Sstevel@tonic-gate
1627c478bd9Sstevel@tonic-gate struct net_data *
net_data_create(const char * conf_file)1637c478bd9Sstevel@tonic-gate net_data_create(const char *conf_file) {
1647c478bd9Sstevel@tonic-gate struct net_data *net_data;
1657c478bd9Sstevel@tonic-gate
1667c478bd9Sstevel@tonic-gate net_data = memget(sizeof (struct net_data));
1677c478bd9Sstevel@tonic-gate if (net_data == NULL)
1687c478bd9Sstevel@tonic-gate return (NULL);
1697c478bd9Sstevel@tonic-gate memset(net_data, 0, sizeof (struct net_data));
1707c478bd9Sstevel@tonic-gate
1717c478bd9Sstevel@tonic-gate if ((net_data->irs = irs_gen_acc("", conf_file)) == NULL) {
1727c478bd9Sstevel@tonic-gate memput(net_data, sizeof (struct net_data));
1737c478bd9Sstevel@tonic-gate return (NULL);
1747c478bd9Sstevel@tonic-gate }
1757c478bd9Sstevel@tonic-gate #ifndef DO_PTHREADS
1767c478bd9Sstevel@tonic-gate (*net_data->irs->res_set)(net_data->irs, &_res, NULL);
1777c478bd9Sstevel@tonic-gate #endif
1787c478bd9Sstevel@tonic-gate
1797c478bd9Sstevel@tonic-gate net_data->res = (*net_data->irs->res_get)(net_data->irs);
1807c478bd9Sstevel@tonic-gate if (net_data->res == NULL) {
1817c478bd9Sstevel@tonic-gate (*net_data->irs->close)(net_data->irs);
1827c478bd9Sstevel@tonic-gate memput(net_data, sizeof (struct net_data));
1837c478bd9Sstevel@tonic-gate return (NULL);
1847c478bd9Sstevel@tonic-gate }
1857c478bd9Sstevel@tonic-gate
1869525b14bSRao Shoaib if ((net_data->res->options & RES_INIT) == 0U &&
1877c478bd9Sstevel@tonic-gate res_ninit(net_data->res) == -1) {
1887c478bd9Sstevel@tonic-gate (*net_data->irs->close)(net_data->irs);
1897c478bd9Sstevel@tonic-gate memput(net_data, sizeof (struct net_data));
1907c478bd9Sstevel@tonic-gate return (NULL);
1917c478bd9Sstevel@tonic-gate }
1927c478bd9Sstevel@tonic-gate
1937c478bd9Sstevel@tonic-gate return (net_data);
1947c478bd9Sstevel@tonic-gate }
1957c478bd9Sstevel@tonic-gate
1967c478bd9Sstevel@tonic-gate void
net_data_minimize(struct net_data * net_data)1977c478bd9Sstevel@tonic-gate net_data_minimize(struct net_data *net_data) {
1987c478bd9Sstevel@tonic-gate res_nclose(net_data->res);
1997c478bd9Sstevel@tonic-gate }
2007c478bd9Sstevel@tonic-gate
2017c478bd9Sstevel@tonic-gate #ifdef _REENTRANT
2027c478bd9Sstevel@tonic-gate struct __res_state *
__res_state(void)2037c478bd9Sstevel@tonic-gate __res_state(void) {
2047c478bd9Sstevel@tonic-gate /* NULL param here means use the default config file. */
2057c478bd9Sstevel@tonic-gate struct net_data *net_data = net_data_init(NULL);
2067c478bd9Sstevel@tonic-gate if (net_data && net_data->res)
2077c478bd9Sstevel@tonic-gate return (net_data->res);
2087c478bd9Sstevel@tonic-gate
2097c478bd9Sstevel@tonic-gate return (&_res);
2107c478bd9Sstevel@tonic-gate }
2119525b14bSRao Shoaib #else
2129525b14bSRao Shoaib #ifdef __linux
2139525b14bSRao Shoaib struct __res_state *
__res_state(void)2149525b14bSRao Shoaib __res_state(void) {
2159525b14bSRao Shoaib return (&_res);
2169525b14bSRao Shoaib }
2179525b14bSRao Shoaib #endif
2187c478bd9Sstevel@tonic-gate #endif
2197c478bd9Sstevel@tonic-gate
2207c478bd9Sstevel@tonic-gate int *
__h_errno(void)2217c478bd9Sstevel@tonic-gate __h_errno(void) {
2227c478bd9Sstevel@tonic-gate /* NULL param here means use the default config file. */
2237c478bd9Sstevel@tonic-gate struct net_data *net_data = net_data_init(NULL);
2247c478bd9Sstevel@tonic-gate if (net_data && net_data->res)
2257c478bd9Sstevel@tonic-gate return (&net_data->res->res_h_errno);
226*4e567b44SStacey Marshall #ifdef ORIGINAL_ISC_CODE
2279525b14bSRao Shoaib #if !(__GLIBC__ > 2 || __GLIBC__ == 2 && __GLIBC_MINOR__ >= 3)
2289525b14bSRao Shoaib return(&_res.res_h_errno);
2299525b14bSRao Shoaib #else
2307c478bd9Sstevel@tonic-gate return (&h_errno);
2319525b14bSRao Shoaib #endif
232*4e567b44SStacey Marshall #else
233*4e567b44SStacey Marshall return (&h_errno);
234*4e567b44SStacey Marshall #endif /* ORIGINAL_ISC_CODE */
2357c478bd9Sstevel@tonic-gate }
2367c478bd9Sstevel@tonic-gate
2377c478bd9Sstevel@tonic-gate void
__h_errno_set(struct __res_state * res,int err)2387c478bd9Sstevel@tonic-gate __h_errno_set(struct __res_state *res, int err) {
2397c478bd9Sstevel@tonic-gate
2409525b14bSRao Shoaib
2419525b14bSRao Shoaib #if (__GLIBC__ > 2 || __GLIBC__ == 2 && __GLIBC_MINOR__ >= 3)
2429525b14bSRao Shoaib res->res_h_errno = err;
2439525b14bSRao Shoaib #else
2447c478bd9Sstevel@tonic-gate h_errno = res->res_h_errno = err;
2459525b14bSRao Shoaib #endif
2467c478bd9Sstevel@tonic-gate }
2477c478bd9Sstevel@tonic-gate
2487c478bd9Sstevel@tonic-gate #endif /*__BIND_NOSTATIC*/
2499525b14bSRao Shoaib
2509525b14bSRao Shoaib /*! \file */
251