1 /* 2 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC") 3 * Copyright (c) 1996-1999 by Internet Software Consortium. 4 * 5 * Permission to use, copy, modify, and distribute this software for any 6 * purpose with or without fee is hereby granted, provided that the above 7 * copyright notice and this permission notice appear in all copies. 8 * 9 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES 10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR 12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT 15 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 16 */ 17 18 #if defined(LIBC_SCCS) && !defined(lint) 19 static const char rcsid[] = "$Id: dns.c,v 1.5 2006/03/09 23:57:56 marka Exp $"; 20 #endif 21 22 /*! \file 23 * \brief 24 * dns.c --- this is the top-level accessor function for the dns 25 */ 26 27 #include "port_before.h" 28 29 #include <stdlib.h> 30 #include <string.h> 31 #include <errno.h> 32 33 #include <sys/types.h> 34 #include <netinet/in.h> 35 #include <arpa/nameser.h> 36 #include <resolv.h> 37 38 #include <resolv.h> 39 40 #include <isc/memcluster.h> 41 #include <irs.h> 42 43 #include "port_after.h" 44 45 #include "irs_p.h" 46 #include "hesiod.h" 47 #include "dns_p.h" 48 49 /* forward */ 50 51 static void dns_close(struct irs_acc *); 52 static struct __res_state * dns_res_get(struct irs_acc *); 53 static void dns_res_set(struct irs_acc *, struct __res_state *, 54 void (*)(void *)); 55 56 /* public */ 57 58 struct irs_acc * 59 irs_dns_acc(const char *options) { 60 struct irs_acc *acc; 61 struct dns_p *dns; 62 63 UNUSED(options); 64 65 if (!(acc = memget(sizeof *acc))) { 66 errno = ENOMEM; 67 return (NULL); 68 } 69 memset(acc, 0x5e, sizeof *acc); 70 if (!(dns = memget(sizeof *dns))) { 71 errno = ENOMEM; 72 memput(acc, sizeof *acc); 73 return (NULL); 74 } 75 memset(dns, 0x5e, sizeof *dns); 76 dns->res = NULL; 77 dns->free_res = NULL; 78 if (hesiod_init(&dns->hes_ctx) < 0) { 79 /* 80 * We allow the dns accessor class to initialize 81 * despite hesiod failing to initialize correctly, 82 * since dns host queries don't depend on hesiod. 83 */ 84 dns->hes_ctx = NULL; 85 } 86 acc->private = dns; 87 #ifdef WANT_IRS_GR 88 acc->gr_map = irs_dns_gr; 89 #else 90 acc->gr_map = NULL; 91 #endif 92 #ifdef WANT_IRS_PW 93 acc->pw_map = irs_dns_pw; 94 #else 95 acc->pw_map = NULL; 96 #endif 97 acc->sv_map = irs_dns_sv; 98 acc->pr_map = irs_dns_pr; 99 acc->ho_map = irs_dns_ho; 100 acc->nw_map = irs_dns_nw; 101 acc->ng_map = irs_nul_ng; 102 acc->res_get = dns_res_get; 103 acc->res_set = dns_res_set; 104 acc->close = dns_close; 105 return (acc); 106 } 107 108 /* methods */ 109 static struct __res_state * 110 dns_res_get(struct irs_acc *this) { 111 struct dns_p *dns = (struct dns_p *)this->private; 112 113 if (dns->res == NULL) { 114 struct __res_state *res; 115 res = (struct __res_state *)malloc(sizeof *res); 116 if (res == NULL) 117 return (NULL); 118 memset(res, 0, sizeof *res); 119 dns_res_set(this, res, free); 120 } 121 122 if ((dns->res->options & RES_INIT) == 0U && 123 res_ninit(dns->res) < 0) 124 return (NULL); 125 126 return (dns->res); 127 } 128 129 static void 130 dns_res_set(struct irs_acc *this, struct __res_state *res, 131 void (*free_res)(void *)) { 132 struct dns_p *dns = (struct dns_p *)this->private; 133 134 if (dns->res && dns->free_res) { 135 res_nclose(dns->res); 136 (*dns->free_res)(dns->res); 137 } 138 dns->res = res; 139 dns->free_res = free_res; 140 } 141 142 static void 143 dns_close(struct irs_acc *this) { 144 struct dns_p *dns; 145 146 dns = (struct dns_p *)this->private; 147 if (dns->res && dns->free_res) 148 (*dns->free_res)(dns->res); 149 if (dns->hes_ctx) 150 hesiod_end(dns->hes_ctx); 151 memput(dns, sizeof *dns); 152 memput(this, sizeof *this); 153 } 154 155