1 1.1 christos /* $NetBSD: lcl.c,v 1.1.1.2 2012/09/09 16:07:56 christos 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(LINT) && !defined(CODECENTER) 21 1.1.1.2 christos static const char rcsid[] = "Id: lcl.c,v 1.4 2005/04/27 04:56:30 sra Exp "; 22 1.1 christos #endif 23 1.1 christos 24 1.1 christos /* Imports */ 25 1.1 christos 26 1.1 christos #include "port_before.h" 27 1.1 christos 28 1.1 christos #include <stdlib.h> 29 1.1 christos #include <errno.h> 30 1.1 christos #include <string.h> 31 1.1 christos 32 1.1 christos #include <sys/types.h> 33 1.1 christos #include <netinet/in.h> 34 1.1 christos #include <arpa/nameser.h> 35 1.1 christos #include <resolv.h> 36 1.1 christos 37 1.1 christos #include <isc/memcluster.h> 38 1.1 christos 39 1.1 christos #include <irs.h> 40 1.1 christos 41 1.1 christos #include "port_after.h" 42 1.1 christos 43 1.1 christos #include "irs_p.h" 44 1.1 christos #include "lcl_p.h" 45 1.1 christos 46 1.1 christos /* Forward. */ 47 1.1 christos 48 1.1 christos static void lcl_close(struct irs_acc *); 49 1.1 christos static struct __res_state * lcl_res_get(struct irs_acc *); 50 1.1 christos static void lcl_res_set(struct irs_acc *, struct __res_state *, 51 1.1 christos void (*)(void *)); 52 1.1 christos 53 1.1 christos /* Public */ 54 1.1 christos 55 1.1 christos struct irs_acc * 56 1.1 christos irs_lcl_acc(const char *options) { 57 1.1 christos struct irs_acc *acc; 58 1.1 christos struct lcl_p *lcl; 59 1.1 christos 60 1.1 christos UNUSED(options); 61 1.1 christos 62 1.1 christos if (!(acc = memget(sizeof *acc))) { 63 1.1 christos errno = ENOMEM; 64 1.1 christos return (NULL); 65 1.1 christos } 66 1.1 christos memset(acc, 0x5e, sizeof *acc); 67 1.1 christos if (!(lcl = memget(sizeof *lcl))) { 68 1.1 christos errno = ENOMEM; 69 1.1 christos free(acc); 70 1.1 christos return (NULL); 71 1.1 christos } 72 1.1 christos memset(lcl, 0x5e, sizeof *lcl); 73 1.1 christos lcl->res = NULL; 74 1.1 christos lcl->free_res = NULL; 75 1.1 christos acc->private = lcl; 76 1.1 christos #ifdef WANT_IRS_GR 77 1.1 christos acc->gr_map = irs_lcl_gr; 78 1.1 christos #else 79 1.1 christos acc->gr_map = NULL; 80 1.1 christos #endif 81 1.1 christos #ifdef WANT_IRS_PW 82 1.1 christos acc->pw_map = irs_lcl_pw; 83 1.1 christos #else 84 1.1 christos acc->pw_map = NULL; 85 1.1 christos #endif 86 1.1 christos acc->sv_map = irs_lcl_sv; 87 1.1 christos acc->pr_map = irs_lcl_pr; 88 1.1 christos acc->ho_map = irs_lcl_ho; 89 1.1 christos acc->nw_map = irs_lcl_nw; 90 1.1 christos acc->ng_map = irs_lcl_ng; 91 1.1 christos acc->res_get = lcl_res_get; 92 1.1 christos acc->res_set = lcl_res_set; 93 1.1 christos acc->close = lcl_close; 94 1.1 christos return (acc); 95 1.1 christos } 96 1.1 christos 97 1.1 christos /* Methods */ 98 1.1 christos static struct __res_state * 99 1.1 christos lcl_res_get(struct irs_acc *this) { 100 1.1 christos struct lcl_p *lcl = (struct lcl_p *)this->private; 101 1.1 christos 102 1.1 christos if (lcl->res == NULL) { 103 1.1 christos struct __res_state *res; 104 1.1 christos res = (struct __res_state *)malloc(sizeof *res); 105 1.1 christos if (res == NULL) 106 1.1 christos return (NULL); 107 1.1 christos memset(res, 0, sizeof *res); 108 1.1 christos lcl_res_set(this, res, free); 109 1.1 christos } 110 1.1 christos 111 1.1 christos if ((lcl->res->options & RES_INIT) == 0U && 112 1.1 christos res_ninit(lcl->res) < 0) 113 1.1 christos return (NULL); 114 1.1 christos 115 1.1 christos return (lcl->res); 116 1.1 christos } 117 1.1 christos 118 1.1 christos static void 119 1.1 christos lcl_res_set(struct irs_acc *this, struct __res_state *res, 120 1.1 christos void (*free_res)(void *)) { 121 1.1 christos struct lcl_p *lcl = (struct lcl_p *)this->private; 122 1.1 christos 123 1.1 christos if (lcl->res && lcl->free_res) { 124 1.1 christos res_nclose(lcl->res); 125 1.1 christos (*lcl->free_res)(lcl->res); 126 1.1 christos } 127 1.1 christos 128 1.1 christos lcl->res = res; 129 1.1 christos lcl->free_res = free_res; 130 1.1 christos } 131 1.1 christos 132 1.1 christos static void 133 1.1 christos lcl_close(struct irs_acc *this) { 134 1.1 christos struct lcl_p *lcl = (struct lcl_p *)this->private; 135 1.1 christos 136 1.1 christos if (lcl) { 137 1.1 christos if (lcl->free_res) 138 1.1 christos (*lcl->free_res)(lcl->res); 139 1.1 christos memput(lcl, sizeof *lcl); 140 1.1 christos } 141 1.1 christos memput(this, sizeof *this); 142 1.1 christos } 143 1.1 christos 144 1.1 christos /*! \file */ 145