1 /* $NetBSD: rdatalist.c,v 1.6 2015/07/08 17:28:59 christos Exp $ */
4 * Copyright (C) 2004, 2005, 2007, 2008, 2010-2012, 2014 Internet Systems Consortium, Inc. ("ISC")
5 * Copyright (C) 1999-2001, 2003 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.
31 #include <dns/nsec3.h>
32 #include <dns/rdata.h>
33 #include <dns/rdatalist.h>
34 #include <dns/rdataset.h>
36 #include "rdatalist_p.h"
38 static dns_rdatasetmethods_t methods
= {
39 isc__rdatalist_disassociate
,
42 isc__rdatalist_current
,
45 isc__rdatalist_addnoqname
,
46 isc__rdatalist_getnoqname
,
47 isc__rdatalist_addclosest
,
48 isc__rdatalist_getclosest
,
58 dns_rdatalist_init(dns_rdatalist_t
*rdatalist
) {
60 REQUIRE(rdatalist
!= NULL
);
63 * Initialize rdatalist.
66 rdatalist
->rdclass
= 0;
68 rdatalist
->covers
= 0;
70 ISC_LIST_INIT(rdatalist
->rdata
);
71 ISC_LINK_INIT(rdatalist
, link
);
75 dns_rdatalist_tordataset(dns_rdatalist_t
*rdatalist
,
76 dns_rdataset_t
*rdataset
)
79 * Make 'rdataset' refer to the rdata in 'rdatalist'.
82 REQUIRE(rdatalist
!= NULL
);
83 REQUIRE(DNS_RDATASET_VALID(rdataset
));
84 REQUIRE(! dns_rdataset_isassociated(rdataset
));
86 rdataset
->methods
= &methods
;
87 rdataset
->rdclass
= rdatalist
->rdclass
;
88 rdataset
->type
= rdatalist
->type
;
89 rdataset
->covers
= rdatalist
->covers
;
90 rdataset
->ttl
= rdatalist
->ttl
;
92 rdataset
->private1
= rdatalist
;
93 rdataset
->private2
= NULL
;
94 rdataset
->private3
= NULL
;
95 rdataset
->privateuint4
= 0;
96 rdataset
->private5
= NULL
;
98 return (ISC_R_SUCCESS
);
102 dns_rdatalist_fromrdataset(dns_rdataset_t
*rdataset
,
103 dns_rdatalist_t
**rdatalist
)
105 REQUIRE(rdatalist
!= NULL
&& rdataset
!= NULL
);
106 *rdatalist
= rdataset
->private1
;
108 return (ISC_R_SUCCESS
);
112 isc__rdatalist_disassociate(dns_rdataset_t
*rdataset
) {
117 isc__rdatalist_first(dns_rdataset_t
*rdataset
) {
118 dns_rdatalist_t
*rdatalist
;
120 rdatalist
= rdataset
->private1
;
121 rdataset
->private2
= ISC_LIST_HEAD(rdatalist
->rdata
);
123 if (rdataset
->private2
== NULL
)
124 return (ISC_R_NOMORE
);
126 return (ISC_R_SUCCESS
);
130 isc__rdatalist_next(dns_rdataset_t
*rdataset
) {
133 REQUIRE(rdataset
!= NULL
);
135 rdata
= rdataset
->private2
;
137 return (ISC_R_NOMORE
);
139 rdataset
->private2
= ISC_LIST_NEXT(rdata
, link
);
141 if (rdataset
->private2
== NULL
)
142 return (ISC_R_NOMORE
);
144 return (ISC_R_SUCCESS
);
148 isc__rdatalist_current(dns_rdataset_t
*rdataset
, dns_rdata_t
*rdata
) {
149 dns_rdata_t
*list_rdata
;
151 REQUIRE(rdataset
!= NULL
);
153 list_rdata
= rdataset
->private2
;
154 INSIST(list_rdata
!= NULL
);
156 dns_rdata_clone(list_rdata
, rdata
);
160 isc__rdatalist_clone(dns_rdataset_t
*source
, dns_rdataset_t
*target
) {
162 REQUIRE(source
!= NULL
);
163 REQUIRE(target
!= NULL
);
168 * Reset iterator state.
170 target
->private2
= NULL
;
174 isc__rdatalist_count(dns_rdataset_t
*rdataset
) {
175 dns_rdatalist_t
*rdatalist
;
179 REQUIRE(rdataset
!= NULL
);
181 rdatalist
= rdataset
->private1
;
184 for (rdata
= ISC_LIST_HEAD(rdatalist
->rdata
);
186 rdata
= ISC_LIST_NEXT(rdata
, link
))
193 isc__rdatalist_addnoqname(dns_rdataset_t
*rdataset
, dns_name_t
*name
) {
194 dns_rdataset_t
*neg
= NULL
;
195 dns_rdataset_t
*negsig
= NULL
;
196 dns_rdataset_t
*rdset
;
199 REQUIRE(rdataset
!= NULL
);
201 for (rdset
= ISC_LIST_HEAD(name
->list
);
203 rdset
= ISC_LIST_NEXT(rdset
, link
))
205 if (rdset
->rdclass
!= rdataset
->rdclass
)
207 if (rdset
->type
== dns_rdatatype_nsec
||
208 rdset
->type
== dns_rdatatype_nsec3
)
212 return (ISC_R_NOTFOUND
);
214 for (rdset
= ISC_LIST_HEAD(name
->list
);
216 rdset
= ISC_LIST_NEXT(rdset
, link
))
218 if (rdset
->type
== dns_rdatatype_rrsig
&&
219 rdset
->covers
== neg
->type
)
224 return (ISC_R_NOTFOUND
);
231 if (negsig
->ttl
< ttl
)
233 rdataset
->ttl
= neg
->ttl
= negsig
->ttl
= ttl
;
234 rdataset
->attributes
|= DNS_RDATASETATTR_NOQNAME
;
235 rdataset
->private6
= name
;
236 return (ISC_R_SUCCESS
);
240 isc__rdatalist_getnoqname(dns_rdataset_t
*rdataset
, dns_name_t
*name
,
241 dns_rdataset_t
*neg
, dns_rdataset_t
*negsig
)
243 dns_rdataclass_t rdclass
= rdataset
->rdclass
;
244 dns_rdataset_t
*tneg
= NULL
;
245 dns_rdataset_t
*tnegsig
= NULL
;
246 dns_name_t
*noqname
= rdataset
->private6
;
248 REQUIRE(rdataset
!= NULL
);
249 REQUIRE((rdataset
->attributes
& DNS_RDATASETATTR_NOQNAME
) != 0);
251 (void)dns_name_dynamic(noqname
); /* Sanity Check. */
253 for (rdataset
= ISC_LIST_HEAD(noqname
->list
);
255 rdataset
= ISC_LIST_NEXT(rdataset
, link
))
257 if (rdataset
->rdclass
!= rdclass
)
259 if (rdataset
->type
== dns_rdatatype_nsec
||
260 rdataset
->type
== dns_rdatatype_nsec3
)
264 return (ISC_R_NOTFOUND
);
266 for (rdataset
= ISC_LIST_HEAD(noqname
->list
);
268 rdataset
= ISC_LIST_NEXT(rdataset
, link
))
270 if (rdataset
->type
== dns_rdatatype_rrsig
&&
271 rdataset
->covers
== tneg
->type
)
275 return (ISC_R_NOTFOUND
);
277 dns_name_clone(noqname
, name
);
278 dns_rdataset_clone(tneg
, neg
);
279 dns_rdataset_clone(tnegsig
, negsig
);
280 return (ISC_R_SUCCESS
);
284 isc__rdatalist_addclosest(dns_rdataset_t
*rdataset
, dns_name_t
*name
) {
285 dns_rdataset_t
*neg
= NULL
;
286 dns_rdataset_t
*negsig
= NULL
;
287 dns_rdataset_t
*rdset
;
290 REQUIRE(rdataset
!= NULL
);
292 for (rdset
= ISC_LIST_HEAD(name
->list
);
294 rdset
= ISC_LIST_NEXT(rdset
, link
))
296 if (rdset
->rdclass
!= rdataset
->rdclass
)
298 if (rdset
->type
== dns_rdatatype_nsec
||
299 rdset
->type
== dns_rdatatype_nsec3
)
303 return (ISC_R_NOTFOUND
);
305 for (rdset
= ISC_LIST_HEAD(name
->list
);
307 rdset
= ISC_LIST_NEXT(rdset
, link
))
309 if (rdset
->type
== dns_rdatatype_rrsig
&&
310 rdset
->covers
== neg
->type
)
315 return (ISC_R_NOTFOUND
);
322 if (negsig
->ttl
< ttl
)
324 rdataset
->ttl
= neg
->ttl
= negsig
->ttl
= ttl
;
325 rdataset
->attributes
|= DNS_RDATASETATTR_CLOSEST
;
326 rdataset
->private7
= name
;
327 return (ISC_R_SUCCESS
);
331 isc__rdatalist_getclosest(dns_rdataset_t
*rdataset
, dns_name_t
*name
,
332 dns_rdataset_t
*neg
, dns_rdataset_t
*negsig
)
334 dns_rdataclass_t rdclass
= rdataset
->rdclass
;
335 dns_rdataset_t
*tneg
= NULL
;
336 dns_rdataset_t
*tnegsig
= NULL
;
337 dns_name_t
*closest
= rdataset
->private7
;
339 REQUIRE(rdataset
!= NULL
);
340 REQUIRE((rdataset
->attributes
& DNS_RDATASETATTR_CLOSEST
) != 0);
342 (void)dns_name_dynamic(closest
); /* Sanity Check. */
344 for (rdataset
= ISC_LIST_HEAD(closest
->list
);
346 rdataset
= ISC_LIST_NEXT(rdataset
, link
))
348 if (rdataset
->rdclass
!= rdclass
)
350 if (rdataset
->type
== dns_rdatatype_nsec
||
351 rdataset
->type
== dns_rdatatype_nsec3
)
355 return (ISC_R_NOTFOUND
);
357 for (rdataset
= ISC_LIST_HEAD(closest
->list
);
359 rdataset
= ISC_LIST_NEXT(rdataset
, link
))
361 if (rdataset
->type
== dns_rdatatype_rrsig
&&
362 rdataset
->covers
== tneg
->type
)
366 return (ISC_R_NOTFOUND
);
368 dns_name_clone(closest
, name
);
369 dns_rdataset_clone(tneg
, neg
);
370 dns_rdataset_clone(tnegsig
, negsig
);
371 return (ISC_R_SUCCESS
);