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