1/*	$NetBSD: lcl.c,v 1.1.1.2 2012/09/09 16:07:56 christos Exp $	*/
2
3/*
4 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
5 * Copyright (c) 1996-1999 by Internet Software Consortium.
6 *
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
10 *
11 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
17 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 */
19
20#if !defined(LINT) && !defined(CODECENTER)
21static const char rcsid[] = "Id: lcl.c,v 1.4 2005/04/27 04:56:30 sra Exp ";
22#endif
23
24/* Imports */
25
26#include "port_before.h"
27
28#include <stdlib.h>
29#include <errno.h>
30#include <string.h>
31
32#include <sys/types.h>
33#include <netinet/in.h>
34#include <arpa/nameser.h>
35#include <resolv.h>
36
37#include <isc/memcluster.h>
38
39#include <irs.h>
40
41#include "port_after.h"
42
43#include "irs_p.h"
44#include "lcl_p.h"
45
46/* Forward. */
47
48static void		lcl_close(struct irs_acc *);
49static struct __res_state *	lcl_res_get(struct irs_acc *);
50static void		lcl_res_set(struct irs_acc *, struct __res_state *,
51				void (*)(void *));
52
53/* Public */
54
55struct irs_acc *
56irs_lcl_acc(const char *options) {
57	struct irs_acc *acc;
58	struct lcl_p *lcl;
59
60	UNUSED(options);
61
62	if (!(acc = memget(sizeof *acc))) {
63		errno = ENOMEM;
64		return (NULL);
65	}
66	memset(acc, 0x5e, sizeof *acc);
67	if (!(lcl = memget(sizeof *lcl))) {
68		errno = ENOMEM;
69		free(acc);
70		return (NULL);
71	}
72	memset(lcl, 0x5e, sizeof *lcl);
73	lcl->res = NULL;
74	lcl->free_res = NULL;
75	acc->private = lcl;
76#ifdef WANT_IRS_GR
77	acc->gr_map = irs_lcl_gr;
78#else
79	acc->gr_map = NULL;
80#endif
81#ifdef WANT_IRS_PW
82	acc->pw_map = irs_lcl_pw;
83#else
84	acc->pw_map = NULL;
85#endif
86	acc->sv_map = irs_lcl_sv;
87	acc->pr_map = irs_lcl_pr;
88	acc->ho_map = irs_lcl_ho;
89	acc->nw_map = irs_lcl_nw;
90	acc->ng_map = irs_lcl_ng;
91	acc->res_get = lcl_res_get;
92	acc->res_set = lcl_res_set;
93	acc->close = lcl_close;
94	return (acc);
95}
96
97/* Methods */
98static struct __res_state *
99lcl_res_get(struct irs_acc *this) {
100	struct lcl_p *lcl = (struct lcl_p *)this->private;
101
102	if (lcl->res == NULL) {
103		struct __res_state *res;
104		res = (struct __res_state *)malloc(sizeof *res);
105		if (res == NULL)
106			return (NULL);
107		memset(res, 0, sizeof *res);
108		lcl_res_set(this, res, free);
109	}
110
111	if ((lcl->res->options & RES_INIT) == 0U &&
112	    res_ninit(lcl->res) < 0)
113		return (NULL);
114
115	return (lcl->res);
116}
117
118static void
119lcl_res_set(struct irs_acc *this, struct __res_state *res,
120		void (*free_res)(void *)) {
121	struct lcl_p *lcl = (struct lcl_p *)this->private;
122
123	if (lcl->res && lcl->free_res) {
124		res_nclose(lcl->res);
125		(*lcl->free_res)(lcl->res);
126	}
127
128	lcl->res = res;
129	lcl->free_res = free_res;
130}
131
132static void
133lcl_close(struct irs_acc *this) {
134	struct lcl_p *lcl = (struct lcl_p *)this->private;
135
136	if (lcl) {
137		if (lcl->free_res)
138			(*lcl->free_res)(lcl->res);
139		memput(lcl, sizeof *lcl);
140	}
141	memput(this, sizeof *this);
142}
143
144/*! \file */
145