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 */
26 #include <isc/magic.h>
28 #include <isc/rwlock.h>
29 #include <isc/sockaddr.h>
32 #include <dns/forward.h>
34 #include <dns/result.h>
35 #include <dns/types.h>
46 #define FWDTABLEMAGIC ISC_MAGIC('F', 'w', 'd', 'T')
47 #define VALID_FWDTABLE(ft) ISC_MAGIC_VALID(ft, FWDTABLEMAGIC)
50 auto_detach(void *, void *);
53 dns_fwdtable_create(isc_mem_t
*mctx
, dns_fwdtable_t
**fwdtablep
) {
54 dns_fwdtable_t
*fwdtable
;
57 REQUIRE(fwdtablep
!= NULL
&& *fwdtablep
== NULL
);
59 fwdtable
= isc_mem_get(mctx
, sizeof(dns_fwdtable_t
));
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
)
72 fwdtable
->mctx
= NULL
;
73 isc_mem_attach(mctx
, &fwdtable
->mctx
);
74 fwdtable
->magic
= FWDTABLEMAGIC
;
75 *fwdtablep
= fwdtable
;
77 return (ISC_R_SUCCESS
);
80 dns_rbt_destroy(&fwdtable
->table
);
83 isc_mem_put(mctx
, fwdtable
, sizeof(dns_fwdtable_t
));
89 dns_fwdtable_add(dns_fwdtable_t
*fwdtable
, dns_name_t
*name
,
90 isc_sockaddrlist_t
*addrs
, dns_fwdpolicy_t fwdpolicy
)
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
);
105 sa
= ISC_LIST_NEXT(sa
, link
))
107 nsa
= isc_mem_get(fwdtable
->mctx
, sizeof(isc_sockaddr_t
));
109 result
= ISC_R_NOMEMORY
;
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
)
125 return (ISC_R_SUCCESS
);
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
));
138 dns_fwdtable_delete(dns_fwdtable_t
*fwdtable
, dns_name_t
*name
) {
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
;
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
));
161 dns_fwdtable_find2(dns_fwdtable_t
*fwdtable
, dns_name_t
*name
,
162 dns_name_t
*foundname
, dns_forwarders_t
**forwardersp
)
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
);
181 dns_fwdtable_destroy(dns_fwdtable_t
**fwdtablep
) {
182 dns_fwdtable_t
*fwdtable
;
185 REQUIRE(fwdtablep
!= NULL
&& VALID_FWDTABLE(*fwdtablep
));
187 fwdtable
= *fwdtablep
;
189 dns_rbt_destroy(&fwdtable
->table
);
190 isc_rwlock_destroy(&fwdtable
->rwlock
);
192 mctx
= fwdtable
->mctx
;
193 isc_mem_put(mctx
, fwdtable
, sizeof(dns_fwdtable_t
));
194 isc_mem_detach(&mctx
);
204 auto_detach(void *data
, void *arg
) {
205 dns_forwarders_t
*forwarders
= data
;
206 dns_fwdtable_t
*fwdtable
= 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
));