2 * Copyright (C) 2004, 2005 Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (C) 2000, 2001 Internet Software Consortium.
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.
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.6.18.4 2005/07/12 01:22:20 marka 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_find(dns_fwdtable_t *fwdtable, dns_name_t *name,
137 dns_forwarders_t **forwardersp)
139 return (dns_fwdtable_find2(fwdtable, name, NULL, forwardersp));
143 dns_fwdtable_find2(dns_fwdtable_t *fwdtable, dns_name_t *name,
144 dns_name_t *foundname, dns_forwarders_t **forwardersp)
148 REQUIRE(VALID_FWDTABLE(fwdtable));
150 RWLOCK(&fwdtable->rwlock, isc_rwlocktype_read);
152 result = dns_rbt_findname(fwdtable->table, name, 0, foundname,
153 (void **)forwardersp);
154 if (result == DNS_R_PARTIALMATCH)
155 result = ISC_R_SUCCESS;
157 RWUNLOCK(&fwdtable->rwlock, isc_rwlocktype_read);
163 dns_fwdtable_destroy(dns_fwdtable_t **fwdtablep) {
164 dns_fwdtable_t *fwdtable;
167 REQUIRE(fwdtablep != NULL && VALID_FWDTABLE(*fwdtablep));
169 fwdtable = *fwdtablep;
171 dns_rbt_destroy(&fwdtable->table);
172 isc_rwlock_destroy(&fwdtable->rwlock);
174 mctx = fwdtable->mctx;
175 isc_mem_put(mctx, fwdtable, sizeof(dns_fwdtable_t));
176 isc_mem_detach(&mctx);
186 auto_detach(void *data, void *arg) {
187 dns_forwarders_t *forwarders = data;
188 dns_fwdtable_t *fwdtable = arg;
193 while (!ISC_LIST_EMPTY(forwarders->addrs)) {
194 sa = ISC_LIST_HEAD(forwarders->addrs);
195 ISC_LIST_UNLINK(forwarders->addrs, sa, link);
196 isc_mem_put(fwdtable->mctx, sa, sizeof(isc_sockaddr_t));
198 isc_mem_put(fwdtable->mctx, forwarders, sizeof(dns_forwarders_t));