2N/A/*
2N/A * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved.
2N/A */
2N/A
2N/A/*
2N/A * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
2N/A * Copyright (c) 1996,1999 by Internet Software Consortium.
2N/A *
2N/A * Permission to use, copy, modify, and distribute this software for any
2N/A * purpose with or without fee is hereby granted, provided that the above
2N/A * copyright notice and this permission notice appear in all copies.
2N/A *
2N/A * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
2N/A * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
2N/A * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
2N/A * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
2N/A * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
2N/A * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
2N/A * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
2N/A */
2N/A
2N/A#if !defined(LINT) && !defined(CODECENTER)
2N/Astatic const char rcsid[] = "$Id: irs_data.c,v 1.12 2007/08/27 03:32:26 marka Exp $";
2N/A#endif
2N/A
2N/A#include "port_before.h"
2N/A
2N/A#ifndef __BIND_NOSTATIC
2N/A
2N/A#include <sys/types.h>
2N/A
2N/A#include <netinet/in.h>
2N/A#include <arpa/nameser.h>
2N/A
2N/A#include <resolv.h>
2N/A#include <stdio.h>
2N/A#include <string.h>
2N/A#include <isc/memcluster.h>
2N/A
2N/A#ifdef DO_PTHREADS
2N/A#include <pthread.h>
2N/A#endif
2N/A
2N/A#include <irs.h>
2N/A#include <stdlib.h>
2N/A
2N/A#include "port_after.h"
2N/A
2N/A#include "irs_data.h"
2N/A#undef _res
2N/A#if !(__GLIBC__ > 2 || __GLIBC__ == 2 && __GLIBC_MINOR__ >= 3)
2N/A#undef h_errno
2N/Aextern int h_errno;
2N/A#endif
2N/A
2N/Aextern struct __res_state _res;
2N/A
2N/A#ifdef DO_PTHREADS
2N/Astatic pthread_key_t key;
2N/Astatic int once = 0;
2N/A#else
2N/Astatic struct net_data *net_data;
2N/A#endif
2N/A
2N/Avoid
2N/Airs_destroy(void) {
2N/A#ifndef DO_PTHREADS
2N/A if (net_data != NULL)
2N/A net_data_destroy(net_data);
2N/A net_data = NULL;
2N/A#endif
2N/A}
2N/A
2N/Avoid
2N/Anet_data_destroy(void *p) {
2N/A struct net_data *net_data = p;
2N/A
2N/A res_ndestroy(net_data->res);
2N/A if (net_data->gr != NULL) {
2N/A (*net_data->gr->close)(net_data->gr);
2N/A net_data->gr = NULL;
2N/A }
2N/A if (net_data->pw != NULL) {
2N/A (*net_data->pw->close)(net_data->pw);
2N/A net_data->pw = NULL;
2N/A }
2N/A if (net_data->sv != NULL) {
2N/A (*net_data->sv->close)(net_data->sv);
2N/A net_data->sv = NULL;
2N/A }
2N/A if (net_data->pr != NULL) {
2N/A (*net_data->pr->close)(net_data->pr);
2N/A net_data->pr = NULL;
2N/A }
2N/A if (net_data->ho != NULL) {
2N/A (*net_data->ho->close)(net_data->ho);
2N/A net_data->ho = NULL;
2N/A }
2N/A if (net_data->nw != NULL) {
2N/A (*net_data->nw->close)(net_data->nw);
2N/A net_data->nw = NULL;
2N/A }
2N/A if (net_data->ng != NULL) {
2N/A (*net_data->ng->close)(net_data->ng);
2N/A net_data->ng = NULL;
2N/A }
2N/A if (net_data->ho_data != NULL) {
2N/A free(net_data->ho_data);
2N/A net_data->ho_data = NULL;
2N/A }
2N/A if (net_data->nw_data != NULL) {
2N/A free(net_data->nw_data);
2N/A net_data->nw_data = NULL;
2N/A }
2N/A
2N/A (*net_data->irs->close)(net_data->irs);
2N/A memput(net_data, sizeof *net_data);
2N/A}
2N/A
2N/A/*%
2N/A * applications that need a specific config file other than
2N/A * _PATH_IRS_CONF should call net_data_init directly rather than letting
2N/A * the various wrapper functions make the first call. - brister
2N/A */
2N/A
2N/Astruct net_data *
2N/Anet_data_init(const char *conf_file) {
2N/A#ifdef DO_PTHREADS
2N/A#ifndef LIBBIND_MUTEX_INITIALIZER
2N/A#define LIBBIND_MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER
2N/A#endif
2N/A static pthread_mutex_t keylock = LIBBIND_MUTEX_INITIALIZER;
2N/A struct net_data *net_data;
2N/A
2N/A if (!once) {
2N/A if (pthread_mutex_lock(&keylock) != 0)
2N/A return (NULL);
2N/A if (!once) {
2N/A if (pthread_key_create(&key, net_data_destroy) != 0) {
2N/A (void)pthread_mutex_unlock(&keylock);
2N/A return (NULL);
2N/A }
2N/A once = 1;
2N/A }
2N/A if (pthread_mutex_unlock(&keylock) != 0)
2N/A return (NULL);
2N/A }
2N/A net_data = pthread_getspecific(key);
2N/A#endif
2N/A
2N/A if (net_data == NULL) {
2N/A net_data = net_data_create(conf_file);
2N/A if (net_data == NULL)
2N/A return (NULL);
2N/A#ifdef DO_PTHREADS
2N/A if (pthread_setspecific(key, net_data) != 0) {
2N/A net_data_destroy(net_data);
2N/A return (NULL);
2N/A }
2N/A#endif
2N/A }
2N/A
2N/A return (net_data);
2N/A}
2N/A
2N/Astruct net_data *
2N/Anet_data_create(const char *conf_file) {
2N/A struct net_data *net_data;
2N/A
2N/A net_data = memget(sizeof (struct net_data));
2N/A if (net_data == NULL)
2N/A return (NULL);
2N/A memset(net_data, 0, sizeof (struct net_data));
2N/A
2N/A if ((net_data->irs = irs_gen_acc("", conf_file)) == NULL) {
2N/A memput(net_data, sizeof (struct net_data));
2N/A return (NULL);
2N/A }
2N/A#ifndef DO_PTHREADS
2N/A (*net_data->irs->res_set)(net_data->irs, &_res, NULL);
2N/A#endif
2N/A
2N/A net_data->res = (*net_data->irs->res_get)(net_data->irs);
2N/A if (net_data->res == NULL) {
2N/A (*net_data->irs->close)(net_data->irs);
2N/A memput(net_data, sizeof (struct net_data));
2N/A return (NULL);
2N/A }
2N/A
2N/A if ((net_data->res->options & RES_INIT) == 0U &&
2N/A res_ninit(net_data->res) == -1) {
2N/A (*net_data->irs->close)(net_data->irs);
2N/A memput(net_data, sizeof (struct net_data));
2N/A return (NULL);
2N/A }
2N/A
2N/A return (net_data);
2N/A}
2N/A
2N/Avoid
2N/Anet_data_minimize(struct net_data *net_data) {
2N/A res_nclose(net_data->res);
2N/A}
2N/A
2N/A#ifdef _REENTRANT
2N/Astruct __res_state *
2N/A__res_state(void) {
2N/A /* NULL param here means use the default config file. */
2N/A struct net_data *net_data = net_data_init(NULL);
2N/A if (net_data && net_data->res)
2N/A return (net_data->res);
2N/A
2N/A return (&_res);
2N/A}
2N/A#else
2N/A#ifdef __linux
2N/Astruct __res_state *
2N/A__res_state(void) {
2N/A return (&_res);
2N/A}
2N/A#endif
2N/A#endif
2N/A
2N/Aint *
2N/A__h_errno(void) {
2N/A /* NULL param here means use the default config file. */
2N/A struct net_data *net_data = net_data_init(NULL);
2N/A if (net_data && net_data->res)
2N/A return (&net_data->res->res_h_errno);
2N/A#ifdef ORIGINAL_ISC_CODE
2N/A#if !(__GLIBC__ > 2 || __GLIBC__ == 2 && __GLIBC_MINOR__ >= 3)
2N/A return(&_res.res_h_errno);
2N/A#else
2N/A return (&h_errno);
2N/A#endif
2N/A#else
2N/A return (&h_errno);
2N/A#endif /* ORIGINAL_ISC_CODE */
2N/A}
2N/A
2N/Avoid
2N/A__h_errno_set(struct __res_state *res, int err) {
2N/A
2N/A
2N/A#if (__GLIBC__ > 2 || __GLIBC__ == 2 && __GLIBC_MINOR__ >= 3)
2N/A res->res_h_errno = err;
2N/A#else
2N/A h_errno = res->res_h_errno = err;
2N/A#endif
2N/A}
2N/A
2N/A#endif /*__BIND_NOSTATIC*/
2N/A
2N/A/*! \file */