2 * Copyright (C) 2004, 2005, 2007, 2009 Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (C) 2000, 2001 Internet Software Consortium.
5 * Permission to use, copy, modify, and/or 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.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
10 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
12 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
14 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15 * PERFORMANCE OF THIS SOFTWARE.
18 /* $Id: forward.c,v 1.14 2009/09/02 23:48:02 tbox Exp $ */
24 #include <isc/magic.h>
26 #include <isc/rwlock.h>
27 #include <isc/sockaddr.h>
30 #include <dns/forward.h>
32 #include <dns/result.h>
33 #include <dns/types.h>
44 #define FWDTABLEMAGIC ISC_MAGIC('F', 'w', 'd', 'T')
45 #define VALID_FWDTABLE(ft) ISC_MAGIC_VALID(ft, FWDTABLEMAGIC)
48 auto_detach(void *, void *);
51 dns_fwdtable_create(isc_mem_t *mctx, dns_fwdtable_t **fwdtablep) {
52 dns_fwdtable_t *fwdtable;
55 REQUIRE(fwdtablep != NULL && *fwdtablep == NULL);
57 fwdtable = isc_mem_get(mctx, sizeof(dns_fwdtable_t));
59 return (ISC_R_NOMEMORY);
61 fwdtable->table = NULL;
62 result = dns_rbt_create(mctx, auto_detach, fwdtable, &fwdtable->table);
63 if (result != ISC_R_SUCCESS)
64 goto cleanup_fwdtable;
66 result = isc_rwlock_init(&fwdtable->rwlock, 0, 0);
67 if (result != ISC_R_SUCCESS)
70 fwdtable->mctx = NULL;
71 isc_mem_attach(mctx, &fwdtable->mctx);
72 fwdtable->magic = FWDTABLEMAGIC;
73 *fwdtablep = fwdtable;
75 return (ISC_R_SUCCESS);
78 dns_rbt_destroy(&fwdtable->table);
81 isc_mem_put(mctx, fwdtable, sizeof(dns_fwdtable_t));
87 dns_fwdtable_add(dns_fwdtable_t *fwdtable, dns_name_t *name,
88 isc_sockaddrlist_t *addrs, dns_fwdpolicy_t fwdpolicy)
91 dns_forwarders_t *forwarders;
92 isc_sockaddr_t *sa, *nsa;
94 REQUIRE(VALID_FWDTABLE(fwdtable));
96 forwarders = isc_mem_get(fwdtable->mctx, sizeof(dns_forwarders_t));
97 if (forwarders == NULL)
98 return (ISC_R_NOMEMORY);
100 ISC_LIST_INIT(forwarders->addrs);
101 for (sa = ISC_LIST_HEAD(*addrs);
103 sa = ISC_LIST_NEXT(sa, link))
105 nsa = isc_mem_get(fwdtable->mctx, sizeof(isc_sockaddr_t));
107 result = ISC_R_NOMEMORY;
111 ISC_LINK_INIT(nsa, link);
112 ISC_LIST_APPEND(forwarders->addrs, nsa, link);
114 forwarders->fwdpolicy = fwdpolicy;
116 RWLOCK(&fwdtable->rwlock, isc_rwlocktype_write);
117 result = dns_rbt_addname(fwdtable->table, name, forwarders);
118 RWUNLOCK(&fwdtable->rwlock, isc_rwlocktype_write);
120 if (result != ISC_R_SUCCESS)
123 return (ISC_R_SUCCESS);
126 while (!ISC_LIST_EMPTY(forwarders->addrs)) {
127 sa = ISC_LIST_HEAD(forwarders->addrs);
128 ISC_LIST_UNLINK(forwarders->addrs, sa, link);
129 isc_mem_put(fwdtable->mctx, sa, sizeof(isc_sockaddr_t));
131 isc_mem_put(fwdtable->mctx, forwarders, sizeof(dns_forwarders_t));
136 dns_fwdtable_delete(dns_fwdtable_t *fwdtable, dns_name_t *name) {
139 REQUIRE(VALID_FWDTABLE(fwdtable));
141 RWLOCK(&fwdtable->rwlock, isc_rwlocktype_write);
142 result = dns_rbt_deletename(fwdtable->table, name, ISC_FALSE);
143 RWUNLOCK(&fwdtable->rwlock, isc_rwlocktype_write);
145 if (result == DNS_R_PARTIALMATCH)
146 result = ISC_R_NOTFOUND;
152 dns_fwdtable_find(dns_fwdtable_t *fwdtable, dns_name_t *name,
153 dns_forwarders_t **forwardersp)
155 return (dns_fwdtable_find2(fwdtable, name, NULL, forwardersp));
159 dns_fwdtable_find2(dns_fwdtable_t *fwdtable, dns_name_t *name,
160 dns_name_t *foundname, dns_forwarders_t **forwardersp)
164 REQUIRE(VALID_FWDTABLE(fwdtable));
166 RWLOCK(&fwdtable->rwlock, isc_rwlocktype_read);
168 result = dns_rbt_findname(fwdtable->table, name, 0, foundname,
169 (void **)forwardersp);
170 if (result == DNS_R_PARTIALMATCH)
171 result = ISC_R_SUCCESS;
173 RWUNLOCK(&fwdtable->rwlock, isc_rwlocktype_read);
179 dns_fwdtable_destroy(dns_fwdtable_t **fwdtablep) {
180 dns_fwdtable_t *fwdtable;
183 REQUIRE(fwdtablep != NULL && VALID_FWDTABLE(*fwdtablep));
185 fwdtable = *fwdtablep;
187 dns_rbt_destroy(&fwdtable->table);
188 isc_rwlock_destroy(&fwdtable->rwlock);
190 mctx = fwdtable->mctx;
191 isc_mem_put(mctx, fwdtable, sizeof(dns_fwdtable_t));
192 isc_mem_detach(&mctx);
202 auto_detach(void *data, void *arg) {
203 dns_forwarders_t *forwarders = data;
204 dns_fwdtable_t *fwdtable = arg;
209 while (!ISC_LIST_EMPTY(forwarders->addrs)) {
210 sa = ISC_LIST_HEAD(forwarders->addrs);
211 ISC_LIST_UNLINK(forwarders->addrs, sa, link);
212 isc_mem_put(fwdtable->mctx, sa, sizeof(isc_sockaddr_t));
214 isc_mem_put(fwdtable->mctx, forwarders, sizeof(dns_forwarders_t));