2 * Copyright (C) 2004, 2007-2009, 2011 Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (C) 2003 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: nsec_47.c,v 1.15 2011/01/13 04:59:26 tbox Exp $ */
20 /* reviewed: Wed Mar 15 18:21:15 PST 2000 by brister */
24 #ifndef RDATA_GENERIC_NSEC_47_C
25 #define RDATA_GENERIC_NSEC_47_C
28 * The attributes do not include DNS_RDATATYPEATTR_SINGLETON
29 * because we must be able to handle a parent/child NSEC pair.
31 #define RRTYPE_NSEC_ATTRIBUTES (DNS_RDATATYPEATTR_DNSSEC)
33 static inline isc_result_t
34 fromtext_nsec(ARGS_FROMTEXT) {
38 unsigned char bm[8*1024]; /* 64k bits */
39 dns_rdatatype_t covered;
52 RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
54 dns_name_init(&name, NULL);
55 buffer_fromregion(&buffer, &token.value.as_region);
56 origin = (origin != NULL) ? origin : dns_rootname;
57 RETTOK(dns_name_fromtext(&name, &buffer, origin, options, target));
59 memset(bm, 0, sizeof(bm));
61 RETERR(isc_lex_getmastertoken(lexer, &token,
62 isc_tokentype_string, ISC_TRUE));
63 if (token.type != isc_tokentype_string)
65 RETTOK(dns_rdatatype_fromtext(&covered,
66 &token.value.as_textregion));
67 bm[covered/8] |= (0x80>>(covered%8));
69 isc_lex_ungettoken(lexer, &token);
70 for (window = 0; window < 256 ; window++) {
72 * Find if we have a type in this window.
74 for (octet = 31; octet >= 0; octet--)
75 if (bm[window * 32 + octet] != 0)
79 RETERR(uint8_tobuffer(window, target));
80 RETERR(uint8_tobuffer(octet + 1, target));
81 RETERR(mem_tobuffer(target, &bm[window * 32], octet + 1));
83 return (ISC_R_SUCCESS);
86 static inline isc_result_t
87 totext_nsec(ARGS_TOTEXT) {
91 unsigned int window, len;
93 REQUIRE(rdata->type == 47);
94 REQUIRE(rdata->length != 0);
98 dns_name_init(&name, NULL);
99 dns_rdata_toregion(rdata, &sr);
100 dns_name_fromregion(&name, &sr);
101 isc_region_consume(&sr, name_length(&name));
102 RETERR(dns_name_totext(&name, ISC_FALSE, target));
105 for (i = 0; i < sr.length; i += len) {
106 INSIST(i + 2 <= sr.length);
108 len = sr.base[i + 1];
109 INSIST(len > 0 && len <= 32);
111 INSIST(i + len <= sr.length);
112 for (j = 0; j < len; j++) {
114 if (sr.base[i + j] == 0)
116 for (k = 0; k < 8; k++) {
117 if ((sr.base[i + j] & (0x80 >> k)) == 0)
119 t = window * 256 + j * 8 + k;
120 RETERR(str_totext(" ", target));
121 if (dns_rdatatype_isknown(t)) {
122 RETERR(dns_rdatatype_totext(t, target));
124 char buf[sizeof("TYPE65535")];
125 sprintf(buf, "TYPE%u", t);
126 RETERR(str_totext(buf, target));
131 return (ISC_R_SUCCESS);
134 static /* inline */ isc_result_t
135 fromwire_nsec(ARGS_FROMWIRE) {
138 unsigned int window, lastwindow = 0;
140 isc_boolean_t first = ISC_TRUE;
148 dns_decompress_setmethods(dctx, DNS_COMPRESS_NONE);
150 dns_name_init(&name, NULL);
151 RETERR(dns_name_fromwire(&name, source, dctx, options, target));
153 isc_buffer_activeregion(source, &sr);
154 for (i = 0; i < sr.length; i += len) {
156 * Check for overflow.
158 if (i + 2 > sr.length)
159 RETERR(DNS_R_FORMERR);
161 len = sr.base[i + 1];
164 * Check that bitmap windows are in the correct order.
166 if (!first && window <= lastwindow)
167 RETERR(DNS_R_FORMERR);
169 * Check for legal lengths.
171 if (len < 1 || len > 32)
172 RETERR(DNS_R_FORMERR);
174 * Check for overflow.
176 if (i + len > sr.length)
177 RETERR(DNS_R_FORMERR);
179 * The last octet of the bitmap must be non zero.
181 if (sr.base[i + len - 1] == 0)
182 RETERR(DNS_R_FORMERR);
187 return (DNS_R_EXTRADATA);
189 RETERR(DNS_R_FORMERR);
190 RETERR(mem_tobuffer(target, sr.base, sr.length));
191 isc_buffer_forward(source, sr.length);
192 return (ISC_R_SUCCESS);
195 static inline isc_result_t
196 towire_nsec(ARGS_TOWIRE) {
199 dns_offsets_t offsets;
201 REQUIRE(rdata->type == 47);
202 REQUIRE(rdata->length != 0);
204 dns_compress_setmethods(cctx, DNS_COMPRESS_NONE);
205 dns_name_init(&name, offsets);
206 dns_rdata_toregion(rdata, &sr);
207 dns_name_fromregion(&name, &sr);
208 isc_region_consume(&sr, name_length(&name));
209 RETERR(dns_name_towire(&name, cctx, target));
211 return (mem_tobuffer(target, sr.base, sr.length));
215 compare_nsec(ARGS_COMPARE) {
219 REQUIRE(rdata1->type == rdata2->type);
220 REQUIRE(rdata1->rdclass == rdata2->rdclass);
221 REQUIRE(rdata1->type == 47);
222 REQUIRE(rdata1->length != 0);
223 REQUIRE(rdata2->length != 0);
225 dns_rdata_toregion(rdata1, &r1);
226 dns_rdata_toregion(rdata2, &r2);
227 return (isc_region_compare(&r1, &r2));
230 static inline isc_result_t
231 fromstruct_nsec(ARGS_FROMSTRUCT) {
232 dns_rdata_nsec_t *nsec = source;
234 unsigned int i, len, window, lastwindow = 0;
235 isc_boolean_t first = ISC_TRUE;
238 REQUIRE(source != NULL);
239 REQUIRE(nsec->common.rdtype == type);
240 REQUIRE(nsec->common.rdclass == rdclass);
241 REQUIRE(nsec->typebits != NULL || nsec->len == 0);
246 dns_name_toregion(&nsec->next, ®ion);
247 RETERR(isc_buffer_copyregion(target, ®ion));
249 * Perform sanity check.
251 for (i = 0; i < nsec->len ; i += len) {
252 INSIST(i + 2 <= nsec->len);
253 window = nsec->typebits[i];
254 len = nsec->typebits[i+1];
256 INSIST(first || window > lastwindow);
257 INSIST(len > 0 && len <= 32);
258 INSIST(i + len <= nsec->len);
259 INSIST(nsec->typebits[i + len - 1] != 0);
264 return (mem_tobuffer(target, nsec->typebits, nsec->len));
267 static inline isc_result_t
268 tostruct_nsec(ARGS_TOSTRUCT) {
270 dns_rdata_nsec_t *nsec = target;
273 REQUIRE(rdata->type == 47);
274 REQUIRE(target != NULL);
275 REQUIRE(rdata->length != 0);
277 nsec->common.rdclass = rdata->rdclass;
278 nsec->common.rdtype = rdata->type;
279 ISC_LINK_INIT(&nsec->common, link);
281 dns_name_init(&name, NULL);
282 dns_rdata_toregion(rdata, ®ion);
283 dns_name_fromregion(&name, ®ion);
284 isc_region_consume(®ion, name_length(&name));
285 dns_name_init(&nsec->next, NULL);
286 RETERR(name_duporclone(&name, mctx, &nsec->next));
288 nsec->len = region.length;
289 nsec->typebits = mem_maybedup(mctx, region.base, region.length);
290 if (nsec->typebits == NULL)
294 return (ISC_R_SUCCESS);
298 dns_name_free(&nsec->next, mctx);
299 return (ISC_R_NOMEMORY);
303 freestruct_nsec(ARGS_FREESTRUCT) {
304 dns_rdata_nsec_t *nsec = source;
306 REQUIRE(source != NULL);
307 REQUIRE(nsec->common.rdtype == 47);
309 if (nsec->mctx == NULL)
312 dns_name_free(&nsec->next, nsec->mctx);
313 if (nsec->typebits != NULL)
314 isc_mem_free(nsec->mctx, nsec->typebits);
318 static inline isc_result_t
319 additionaldata_nsec(ARGS_ADDLDATA) {
320 REQUIRE(rdata->type == 47);
326 return (ISC_R_SUCCESS);
329 static inline isc_result_t
330 digest_nsec(ARGS_DIGEST) {
333 REQUIRE(rdata->type == 47);
335 dns_rdata_toregion(rdata, &r);
336 return ((digest)(arg, &r));
339 static inline isc_boolean_t
340 checkowner_nsec(ARGS_CHECKOWNER) {
352 static inline isc_boolean_t
353 checknames_nsec(ARGS_CHECKNAMES) {
355 REQUIRE(rdata->type == 47);
365 casecompare_nsec(ARGS_COMPARE) {
366 isc_region_t region1;
367 isc_region_t region2;
372 REQUIRE(rdata1->type == rdata2->type);
373 REQUIRE(rdata1->rdclass == rdata2->rdclass);
374 REQUIRE(rdata1->type == 47);
375 REQUIRE(rdata1->length != 0);
376 REQUIRE(rdata2->length != 0);
378 dns_name_init(&name1, NULL);
379 dns_name_init(&name2, NULL);
381 dns_rdata_toregion(rdata1, ®ion1);
382 dns_rdata_toregion(rdata2, ®ion2);
384 dns_name_fromregion(&name1, ®ion1);
385 dns_name_fromregion(&name2, ®ion2);
387 order = dns_name_rdatacompare(&name1, &name2);
391 isc_region_consume(®ion1, name_length(&name1));
392 isc_region_consume(®ion2, name_length(&name2));
394 return (isc_region_compare(®ion1, ®ion2));
396 #endif /* RDATA_GENERIC_NSEC_47_C */