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