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