Patrick Welche <prlw1@cam.ac.uk>
[netbsd-mini2440.git] / external / bsd / bind / dist / lib / dns / forward.c
blobd77d598099381c636cb7f1479776ef4df2bef7c2
1 /* $NetBSD$ */
3 /*
4 * Copyright (C) 2004, 2005, 2007, 2009 Internet Systems Consortium, Inc. ("ISC")
5 * Copyright (C) 2000, 2001 Internet Software Consortium.
7 * Permission to use, copy, modify, and/or 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.
11 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
12 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
13 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
14 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
15 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
16 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17 * PERFORMANCE OF THIS SOFTWARE.
20 /* Id: forward.c,v 1.14 2009/09/02 23:48:02 tbox Exp */
22 /*! \file */
24 #include <config.h>
26 #include <isc/magic.h>
27 #include <isc/mem.h>
28 #include <isc/rwlock.h>
29 #include <isc/sockaddr.h>
30 #include <isc/util.h>
32 #include <dns/forward.h>
33 #include <dns/rbt.h>
34 #include <dns/result.h>
35 #include <dns/types.h>
37 struct dns_fwdtable {
38 /* Unlocked. */
39 unsigned int magic;
40 isc_mem_t *mctx;
41 isc_rwlock_t rwlock;
42 /* Locked by lock. */
43 dns_rbt_t *table;
46 #define FWDTABLEMAGIC ISC_MAGIC('F', 'w', 'd', 'T')
47 #define VALID_FWDTABLE(ft) ISC_MAGIC_VALID(ft, FWDTABLEMAGIC)
49 static void
50 auto_detach(void *, void *);
52 isc_result_t
53 dns_fwdtable_create(isc_mem_t *mctx, dns_fwdtable_t **fwdtablep) {
54 dns_fwdtable_t *fwdtable;
55 isc_result_t result;
57 REQUIRE(fwdtablep != NULL && *fwdtablep == NULL);
59 fwdtable = isc_mem_get(mctx, sizeof(dns_fwdtable_t));
60 if (fwdtable == NULL)
61 return (ISC_R_NOMEMORY);
63 fwdtable->table = NULL;
64 result = dns_rbt_create(mctx, auto_detach, fwdtable, &fwdtable->table);
65 if (result != ISC_R_SUCCESS)
66 goto cleanup_fwdtable;
68 result = isc_rwlock_init(&fwdtable->rwlock, 0, 0);
69 if (result != ISC_R_SUCCESS)
70 goto cleanup_rbt;
72 fwdtable->mctx = NULL;
73 isc_mem_attach(mctx, &fwdtable->mctx);
74 fwdtable->magic = FWDTABLEMAGIC;
75 *fwdtablep = fwdtable;
77 return (ISC_R_SUCCESS);
79 cleanup_rbt:
80 dns_rbt_destroy(&fwdtable->table);
82 cleanup_fwdtable:
83 isc_mem_put(mctx, fwdtable, sizeof(dns_fwdtable_t));
85 return (result);
88 isc_result_t
89 dns_fwdtable_add(dns_fwdtable_t *fwdtable, dns_name_t *name,
90 isc_sockaddrlist_t *addrs, dns_fwdpolicy_t fwdpolicy)
92 isc_result_t result;
93 dns_forwarders_t *forwarders;
94 isc_sockaddr_t *sa, *nsa;
96 REQUIRE(VALID_FWDTABLE(fwdtable));
98 forwarders = isc_mem_get(fwdtable->mctx, sizeof(dns_forwarders_t));
99 if (forwarders == NULL)
100 return (ISC_R_NOMEMORY);
102 ISC_LIST_INIT(forwarders->addrs);
103 for (sa = ISC_LIST_HEAD(*addrs);
104 sa != NULL;
105 sa = ISC_LIST_NEXT(sa, link))
107 nsa = isc_mem_get(fwdtable->mctx, sizeof(isc_sockaddr_t));
108 if (nsa == NULL) {
109 result = ISC_R_NOMEMORY;
110 goto cleanup;
112 *nsa = *sa;
113 ISC_LINK_INIT(nsa, link);
114 ISC_LIST_APPEND(forwarders->addrs, nsa, link);
116 forwarders->fwdpolicy = fwdpolicy;
118 RWLOCK(&fwdtable->rwlock, isc_rwlocktype_write);
119 result = dns_rbt_addname(fwdtable->table, name, forwarders);
120 RWUNLOCK(&fwdtable->rwlock, isc_rwlocktype_write);
122 if (result != ISC_R_SUCCESS)
123 goto cleanup;
125 return (ISC_R_SUCCESS);
127 cleanup:
128 while (!ISC_LIST_EMPTY(forwarders->addrs)) {
129 sa = ISC_LIST_HEAD(forwarders->addrs);
130 ISC_LIST_UNLINK(forwarders->addrs, sa, link);
131 isc_mem_put(fwdtable->mctx, sa, sizeof(isc_sockaddr_t));
133 isc_mem_put(fwdtable->mctx, forwarders, sizeof(dns_forwarders_t));
134 return (result);
137 isc_result_t
138 dns_fwdtable_delete(dns_fwdtable_t *fwdtable, dns_name_t *name) {
139 isc_result_t result;
141 REQUIRE(VALID_FWDTABLE(fwdtable));
143 RWLOCK(&fwdtable->rwlock, isc_rwlocktype_write);
144 result = dns_rbt_deletename(fwdtable->table, name, ISC_FALSE);
145 RWUNLOCK(&fwdtable->rwlock, isc_rwlocktype_write);
147 if (result == DNS_R_PARTIALMATCH)
148 result = ISC_R_NOTFOUND;
150 return (result);
153 isc_result_t
154 dns_fwdtable_find(dns_fwdtable_t *fwdtable, dns_name_t *name,
155 dns_forwarders_t **forwardersp)
157 return (dns_fwdtable_find2(fwdtable, name, NULL, forwardersp));
160 isc_result_t
161 dns_fwdtable_find2(dns_fwdtable_t *fwdtable, dns_name_t *name,
162 dns_name_t *foundname, dns_forwarders_t **forwardersp)
164 isc_result_t result;
166 REQUIRE(VALID_FWDTABLE(fwdtable));
168 RWLOCK(&fwdtable->rwlock, isc_rwlocktype_read);
170 result = dns_rbt_findname(fwdtable->table, name, 0, foundname,
171 (void **)forwardersp);
172 if (result == DNS_R_PARTIALMATCH)
173 result = ISC_R_SUCCESS;
175 RWUNLOCK(&fwdtable->rwlock, isc_rwlocktype_read);
177 return (result);
180 void
181 dns_fwdtable_destroy(dns_fwdtable_t **fwdtablep) {
182 dns_fwdtable_t *fwdtable;
183 isc_mem_t *mctx;
185 REQUIRE(fwdtablep != NULL && VALID_FWDTABLE(*fwdtablep));
187 fwdtable = *fwdtablep;
189 dns_rbt_destroy(&fwdtable->table);
190 isc_rwlock_destroy(&fwdtable->rwlock);
191 fwdtable->magic = 0;
192 mctx = fwdtable->mctx;
193 isc_mem_put(mctx, fwdtable, sizeof(dns_fwdtable_t));
194 isc_mem_detach(&mctx);
196 *fwdtablep = NULL;
199 /***
200 *** Private
201 ***/
203 static void
204 auto_detach(void *data, void *arg) {
205 dns_forwarders_t *forwarders = data;
206 dns_fwdtable_t *fwdtable = arg;
207 isc_sockaddr_t *sa;
209 UNUSED(arg);
211 while (!ISC_LIST_EMPTY(forwarders->addrs)) {
212 sa = ISC_LIST_HEAD(forwarders->addrs);
213 ISC_LIST_UNLINK(forwarders->addrs, sa, link);
214 isc_mem_put(fwdtable->mctx, sa, sizeof(isc_sockaddr_t));
216 isc_mem_put(fwdtable->mctx, forwarders, sizeof(dns_forwarders_t));