BIND: update vendor tree to 9.5.2-P2
[dragonfly.git] / contrib / bind / lib / dns / rdatalist.c
CommitLineData
bbbf71a3
JL
1/*
2 * Copyright (C) 2004, 2005, 2007, 2008 Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (C) 1999-2001, 2003 Internet Software Consortium.
4 *
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.
8 *
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.
16 */
17
18/* $Id: rdatalist.c,v 1.33.128.2 2008/04/03 06:08:27 tbox Exp $ */
19
20/*! \file */
21
22#include <config.h>
23
24#include <stddef.h>
25
26#include <isc/util.h>
27
28#include <dns/name.h>
29#include <dns/rdata.h>
30#include <dns/rdatalist.h>
31#include <dns/rdataset.h>
32
33#include "rdatalist_p.h"
34
35static dns_rdatasetmethods_t methods = {
36 isc__rdatalist_disassociate,
37 isc__rdatalist_first,
38 isc__rdatalist_next,
39 isc__rdatalist_current,
40 isc__rdatalist_clone,
41 isc__rdatalist_count,
42 isc__rdatalist_addnoqname,
43 isc__rdatalist_getnoqname,
44 NULL,
45 NULL,
46 NULL
47};
48
49void
50dns_rdatalist_init(dns_rdatalist_t *rdatalist) {
51
52 /*
53 * Initialize rdatalist.
54 */
55
56 rdatalist->rdclass = 0;
57 rdatalist->type = 0;
58 rdatalist->covers = 0;
59 rdatalist->ttl = 0;
60 ISC_LIST_INIT(rdatalist->rdata);
61 ISC_LINK_INIT(rdatalist, link);
62}
63
64isc_result_t
65dns_rdatalist_tordataset(dns_rdatalist_t *rdatalist,
66 dns_rdataset_t *rdataset) {
67
68 /*
69 * Make 'rdataset' refer to the rdata in 'rdatalist'.
70 */
71
72 REQUIRE(rdatalist != NULL);
73 REQUIRE(DNS_RDATASET_VALID(rdataset));
74 REQUIRE(! dns_rdataset_isassociated(rdataset));
75
76 rdataset->methods = &methods;
77 rdataset->rdclass = rdatalist->rdclass;
78 rdataset->type = rdatalist->type;
79 rdataset->covers = rdatalist->covers;
80 rdataset->ttl = rdatalist->ttl;
81 rdataset->trust = 0;
82 rdataset->private1 = rdatalist;
83 rdataset->private2 = NULL;
84 rdataset->private3 = NULL;
85 rdataset->privateuint4 = 0;
86 rdataset->private5 = NULL;
87
88 return (ISC_R_SUCCESS);
89}
90
91isc_result_t
92dns_rdatalist_fromrdataset(dns_rdataset_t *rdataset,
93 dns_rdatalist_t **rdatalist)
94{
95 REQUIRE(rdatalist != NULL && rdataset != NULL);
96 *rdatalist = rdataset->private1;
97
98 return (ISC_R_SUCCESS);
99}
100
101void
102isc__rdatalist_disassociate(dns_rdataset_t *rdataset) {
103 UNUSED(rdataset);
104}
105
106isc_result_t
107isc__rdatalist_first(dns_rdataset_t *rdataset) {
108 dns_rdatalist_t *rdatalist;
109
110 rdatalist = rdataset->private1;
111 rdataset->private2 = ISC_LIST_HEAD(rdatalist->rdata);
112
113 if (rdataset->private2 == NULL)
114 return (ISC_R_NOMORE);
115
116 return (ISC_R_SUCCESS);
117}
118
119isc_result_t
120isc__rdatalist_next(dns_rdataset_t *rdataset) {
121 dns_rdata_t *rdata;
122
123 rdata = rdataset->private2;
124 if (rdata == NULL)
125 return (ISC_R_NOMORE);
126
127 rdataset->private2 = ISC_LIST_NEXT(rdata, link);
128
129 if (rdataset->private2 == NULL)
130 return (ISC_R_NOMORE);
131
132 return (ISC_R_SUCCESS);
133}
134
135void
136isc__rdatalist_current(dns_rdataset_t *rdataset, dns_rdata_t *rdata) {
137 dns_rdata_t *list_rdata;
138
139 list_rdata = rdataset->private2;
140 INSIST(list_rdata != NULL);
141
142 dns_rdata_clone(list_rdata, rdata);
143}
144
145void
146isc__rdatalist_clone(dns_rdataset_t *source, dns_rdataset_t *target) {
147 *target = *source;
148
149 /*
150 * Reset iterator state.
151 */
152 target->private2 = NULL;
153}
154
155unsigned int
156isc__rdatalist_count(dns_rdataset_t *rdataset) {
157 dns_rdatalist_t *rdatalist;
158 dns_rdata_t *rdata;
159 unsigned int count;
160
161 rdatalist = rdataset->private1;
162
163 count = 0;
164 for (rdata = ISC_LIST_HEAD(rdatalist->rdata);
165 rdata != NULL;
166 rdata = ISC_LIST_NEXT(rdata, link))
167 count++;
168
169 return (count);
170}
171
172isc_result_t
173isc__rdatalist_addnoqname(dns_rdataset_t *rdataset, dns_name_t *name) {
174 dns_rdataset_t *nsec = NULL;
175 dns_rdataset_t *nsecsig = NULL;
176 dns_rdataset_t *rdset;
177 dns_ttl_t ttl;
178
179 for (rdset = ISC_LIST_HEAD(name->list);
180 rdset != NULL;
181 rdset = ISC_LIST_NEXT(rdset, link))
182 {
183 if (rdset->rdclass != rdataset->rdclass)
184 continue;
185 if (rdset->type == dns_rdatatype_nsec)
186 nsec = rdset;
187 if (rdset->type == dns_rdatatype_rrsig &&
188 rdset->covers == dns_rdatatype_nsec)
189 nsecsig = rdset;
190 }
191
192 if (nsec == NULL || nsecsig == NULL)
193 return (ISC_R_NOTFOUND);
194 /*
195 * Minimise ttl.
196 */
197 ttl = rdataset->ttl;
198 if (nsec->ttl < ttl)
199 ttl = nsec->ttl;
200 if (nsecsig->ttl < ttl)
201 ttl = nsecsig->ttl;
202 rdataset->ttl = nsec->ttl = nsecsig->ttl = ttl;
203 rdataset->attributes |= DNS_RDATASETATTR_NOQNAME;
204 rdataset->private6 = name;
205 return (ISC_R_SUCCESS);
206}
207
208isc_result_t
209isc__rdatalist_getnoqname(dns_rdataset_t *rdataset, dns_name_t *name,
210 dns_rdataset_t *nsec, dns_rdataset_t *nsecsig)
211{
212 dns_rdataclass_t rdclass = rdataset->rdclass;
213 dns_rdataset_t *tnsec = NULL;
214 dns_rdataset_t *tnsecsig = NULL;
215 dns_name_t *noqname = rdataset->private6;
216
217 REQUIRE((rdataset->attributes & DNS_RDATASETATTR_NOQNAME) != 0);
218 (void)dns_name_dynamic(noqname); /* Sanity Check. */
219
220 for (rdataset = ISC_LIST_HEAD(noqname->list);
221 rdataset != NULL;
222 rdataset = ISC_LIST_NEXT(rdataset, link))
223 {
224 if (rdataset->rdclass != rdclass)
225 continue;
226 if (rdataset->type == dns_rdatatype_nsec)
227 tnsec = rdataset;
228 if (rdataset->type == dns_rdatatype_rrsig &&
229 rdataset->covers == dns_rdatatype_nsec)
230 tnsecsig = rdataset;
231 }
232 if (tnsec == NULL || tnsecsig == NULL)
233 return (ISC_R_NOTFOUND);
234
235 dns_name_clone(noqname, name);
236 dns_rdataset_clone(tnsec, nsec);
237 dns_rdataset_clone(tnsecsig, nsecsig);
238 return (ISC_R_SUCCESS);
239}