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