10Sstevel@tonic-gate /* 2*11038SRao.Shoaib@Sun.COM * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC") 30Sstevel@tonic-gate * Copyright (c) 1996-1999 by Internet Software Consortium. 40Sstevel@tonic-gate * 50Sstevel@tonic-gate * Permission to use, copy, modify, and distribute this software for any 60Sstevel@tonic-gate * purpose with or without fee is hereby granted, provided that the above 70Sstevel@tonic-gate * copyright notice and this permission notice appear in all copies. 80Sstevel@tonic-gate * 9*11038SRao.Shoaib@Sun.COM * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES 10*11038SRao.Shoaib@Sun.COM * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 11*11038SRao.Shoaib@Sun.COM * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR 12*11038SRao.Shoaib@Sun.COM * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 13*11038SRao.Shoaib@Sun.COM * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 14*11038SRao.Shoaib@Sun.COM * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT 15*11038SRao.Shoaib@Sun.COM * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 160Sstevel@tonic-gate */ 170Sstevel@tonic-gate 180Sstevel@tonic-gate #if defined(LIBC_SCCS) && !defined(lint) 19*11038SRao.Shoaib@Sun.COM static const char rcsid[] = "$Id: dns.c,v 1.5 2006/03/09 23:57:56 marka Exp $"; 200Sstevel@tonic-gate #endif 210Sstevel@tonic-gate 22*11038SRao.Shoaib@Sun.COM /*! \file 23*11038SRao.Shoaib@Sun.COM * \brief 240Sstevel@tonic-gate * dns.c --- this is the top-level accessor function for the dns 250Sstevel@tonic-gate */ 260Sstevel@tonic-gate 270Sstevel@tonic-gate #include "port_before.h" 280Sstevel@tonic-gate 290Sstevel@tonic-gate #include <stdlib.h> 300Sstevel@tonic-gate #include <string.h> 310Sstevel@tonic-gate #include <errno.h> 320Sstevel@tonic-gate 330Sstevel@tonic-gate #include <sys/types.h> 340Sstevel@tonic-gate #include <netinet/in.h> 350Sstevel@tonic-gate #include <arpa/nameser.h> 360Sstevel@tonic-gate #include <resolv.h> 370Sstevel@tonic-gate 380Sstevel@tonic-gate #include <resolv.h> 390Sstevel@tonic-gate 400Sstevel@tonic-gate #include <isc/memcluster.h> 410Sstevel@tonic-gate #include <irs.h> 420Sstevel@tonic-gate 430Sstevel@tonic-gate #include "port_after.h" 440Sstevel@tonic-gate 450Sstevel@tonic-gate #include "irs_p.h" 460Sstevel@tonic-gate #include "hesiod.h" 470Sstevel@tonic-gate #include "dns_p.h" 480Sstevel@tonic-gate 490Sstevel@tonic-gate /* forward */ 500Sstevel@tonic-gate 510Sstevel@tonic-gate static void dns_close(struct irs_acc *); 520Sstevel@tonic-gate static struct __res_state * dns_res_get(struct irs_acc *); 530Sstevel@tonic-gate static void dns_res_set(struct irs_acc *, struct __res_state *, 540Sstevel@tonic-gate void (*)(void *)); 550Sstevel@tonic-gate 560Sstevel@tonic-gate /* public */ 570Sstevel@tonic-gate 580Sstevel@tonic-gate struct irs_acc * 590Sstevel@tonic-gate irs_dns_acc(const char *options) { 600Sstevel@tonic-gate struct irs_acc *acc; 610Sstevel@tonic-gate struct dns_p *dns; 620Sstevel@tonic-gate 630Sstevel@tonic-gate UNUSED(options); 640Sstevel@tonic-gate 650Sstevel@tonic-gate if (!(acc = memget(sizeof *acc))) { 660Sstevel@tonic-gate errno = ENOMEM; 670Sstevel@tonic-gate return (NULL); 680Sstevel@tonic-gate } 690Sstevel@tonic-gate memset(acc, 0x5e, sizeof *acc); 700Sstevel@tonic-gate if (!(dns = memget(sizeof *dns))) { 710Sstevel@tonic-gate errno = ENOMEM; 720Sstevel@tonic-gate memput(acc, sizeof *acc); 730Sstevel@tonic-gate return (NULL); 740Sstevel@tonic-gate } 750Sstevel@tonic-gate memset(dns, 0x5e, sizeof *dns); 760Sstevel@tonic-gate dns->res = NULL; 770Sstevel@tonic-gate dns->free_res = NULL; 780Sstevel@tonic-gate if (hesiod_init(&dns->hes_ctx) < 0) { 790Sstevel@tonic-gate /* 800Sstevel@tonic-gate * We allow the dns accessor class to initialize 810Sstevel@tonic-gate * despite hesiod failing to initialize correctly, 820Sstevel@tonic-gate * since dns host queries don't depend on hesiod. 830Sstevel@tonic-gate */ 840Sstevel@tonic-gate dns->hes_ctx = NULL; 850Sstevel@tonic-gate } 860Sstevel@tonic-gate acc->private = dns; 870Sstevel@tonic-gate #ifdef WANT_IRS_GR 880Sstevel@tonic-gate acc->gr_map = irs_dns_gr; 890Sstevel@tonic-gate #else 900Sstevel@tonic-gate acc->gr_map = NULL; 910Sstevel@tonic-gate #endif 920Sstevel@tonic-gate #ifdef WANT_IRS_PW 930Sstevel@tonic-gate acc->pw_map = irs_dns_pw; 940Sstevel@tonic-gate #else 950Sstevel@tonic-gate acc->pw_map = NULL; 960Sstevel@tonic-gate #endif 970Sstevel@tonic-gate acc->sv_map = irs_dns_sv; 980Sstevel@tonic-gate acc->pr_map = irs_dns_pr; 990Sstevel@tonic-gate acc->ho_map = irs_dns_ho; 1000Sstevel@tonic-gate acc->nw_map = irs_dns_nw; 1010Sstevel@tonic-gate acc->ng_map = irs_nul_ng; 1020Sstevel@tonic-gate acc->res_get = dns_res_get; 1030Sstevel@tonic-gate acc->res_set = dns_res_set; 1040Sstevel@tonic-gate acc->close = dns_close; 1050Sstevel@tonic-gate return (acc); 1060Sstevel@tonic-gate } 1070Sstevel@tonic-gate 1080Sstevel@tonic-gate /* methods */ 1090Sstevel@tonic-gate static struct __res_state * 1100Sstevel@tonic-gate dns_res_get(struct irs_acc *this) { 1110Sstevel@tonic-gate struct dns_p *dns = (struct dns_p *)this->private; 1120Sstevel@tonic-gate 1130Sstevel@tonic-gate if (dns->res == NULL) { 1140Sstevel@tonic-gate struct __res_state *res; 1150Sstevel@tonic-gate res = (struct __res_state *)malloc(sizeof *res); 1160Sstevel@tonic-gate if (res == NULL) 1170Sstevel@tonic-gate return (NULL); 118*11038SRao.Shoaib@Sun.COM memset(res, 0, sizeof *res); 1190Sstevel@tonic-gate dns_res_set(this, res, free); 1200Sstevel@tonic-gate } 1210Sstevel@tonic-gate 122*11038SRao.Shoaib@Sun.COM if ((dns->res->options & RES_INIT) == 0U && 1230Sstevel@tonic-gate res_ninit(dns->res) < 0) 1240Sstevel@tonic-gate return (NULL); 1250Sstevel@tonic-gate 1260Sstevel@tonic-gate return (dns->res); 1270Sstevel@tonic-gate } 1280Sstevel@tonic-gate 1290Sstevel@tonic-gate static void 1300Sstevel@tonic-gate dns_res_set(struct irs_acc *this, struct __res_state *res, 1310Sstevel@tonic-gate void (*free_res)(void *)) { 1320Sstevel@tonic-gate struct dns_p *dns = (struct dns_p *)this->private; 1330Sstevel@tonic-gate 1340Sstevel@tonic-gate if (dns->res && dns->free_res) { 1350Sstevel@tonic-gate res_nclose(dns->res); 1360Sstevel@tonic-gate (*dns->free_res)(dns->res); 1370Sstevel@tonic-gate } 1380Sstevel@tonic-gate dns->res = res; 1390Sstevel@tonic-gate dns->free_res = free_res; 1400Sstevel@tonic-gate } 1410Sstevel@tonic-gate 1420Sstevel@tonic-gate static void 1430Sstevel@tonic-gate dns_close(struct irs_acc *this) { 1440Sstevel@tonic-gate struct dns_p *dns; 1450Sstevel@tonic-gate 1460Sstevel@tonic-gate dns = (struct dns_p *)this->private; 1470Sstevel@tonic-gate if (dns->res && dns->free_res) 1480Sstevel@tonic-gate (*dns->free_res)(dns->res); 1490Sstevel@tonic-gate if (dns->hes_ctx) 1500Sstevel@tonic-gate hesiod_end(dns->hes_ctx); 1510Sstevel@tonic-gate memput(dns, sizeof *dns); 1520Sstevel@tonic-gate memput(this, sizeof *this); 1530Sstevel@tonic-gate } 1540Sstevel@tonic-gate 155