Sync usage with man page.
[netbsd-mini2440.git] / external / bsd / bind / dist / lib / dns / rdata / generic / dlv_32769.c
blob347907111032fa4d46609227454a673bb7b85cef
1 /* $NetBSD$ */
3 /*
4 * Copyright (C) 2004, 2006, 2007, 2009 Internet Systems Consortium, Inc. ("ISC")
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
11 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
12 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
13 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
14 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
15 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
16 * PERFORMANCE OF THIS SOFTWARE.
19 /* Id: dlv_32769.c,v 1.8 2009/12/04 22:06:37 tbox Exp */
21 /* draft-ietf-dnsext-delegation-signer-05.txt */
23 #ifndef RDATA_GENERIC_DLV_32769_C
24 #define RDATA_GENERIC_DLV_32769_C
26 #define RRTYPE_DLV_ATTRIBUTES 0
28 #include <isc/sha1.h>
29 #include <isc/sha2.h>
31 #include <dns/ds.h>
34 static inline isc_result_t
35 fromtext_dlv(ARGS_FROMTEXT) {
36 isc_token_t token;
37 unsigned char c;
38 int length;
40 REQUIRE(type == 32769);
42 UNUSED(type);
43 UNUSED(rdclass);
44 UNUSED(origin);
45 UNUSED(options);
46 UNUSED(callbacks);
49 * Key tag.
51 RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_number,
52 ISC_FALSE));
53 if (token.value.as_ulong > 0xffffU)
54 RETTOK(ISC_R_RANGE);
55 RETERR(uint16_tobuffer(token.value.as_ulong, target));
58 * Algorithm.
60 RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_number,
61 ISC_FALSE));
62 if (token.value.as_ulong > 0xffU)
63 RETTOK(ISC_R_RANGE);
64 RETERR(uint8_tobuffer(token.value.as_ulong, target));
67 * Digest type.
69 RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_number,
70 ISC_FALSE));
71 if (token.value.as_ulong > 0xffU)
72 RETTOK(ISC_R_RANGE);
73 RETERR(uint8_tobuffer(token.value.as_ulong, target));
74 c = (unsigned char) token.value.as_ulong;
77 * Digest.
79 if (c == DNS_DSDIGEST_SHA1)
80 length = ISC_SHA1_DIGESTLENGTH;
81 else if (c == DNS_DSDIGEST_SHA256)
82 length = ISC_SHA256_DIGESTLENGTH;
83 else
84 length = -1;
85 return (isc_hex_tobuffer(lexer, target, -1));
88 static inline isc_result_t
89 totext_dlv(ARGS_TOTEXT) {
90 isc_region_t sr;
91 char buf[sizeof("64000 ")];
92 unsigned int n;
94 REQUIRE(rdata->type == 32769);
95 REQUIRE(rdata->length != 0);
97 UNUSED(tctx);
99 dns_rdata_toregion(rdata, &sr);
102 * Key tag.
104 n = uint16_fromregion(&sr);
105 isc_region_consume(&sr, 2);
106 sprintf(buf, "%u ", n);
107 RETERR(str_totext(buf, target));
110 * Algorithm.
112 n = uint8_fromregion(&sr);
113 isc_region_consume(&sr, 1);
114 sprintf(buf, "%u ", n);
115 RETERR(str_totext(buf, target));
118 * Digest type.
120 n = uint8_fromregion(&sr);
121 isc_region_consume(&sr, 1);
122 sprintf(buf, "%u", n);
123 RETERR(str_totext(buf, target));
126 * Digest.
128 if ((tctx->flags & DNS_STYLEFLAG_MULTILINE) != 0)
129 RETERR(str_totext(" (", target));
130 RETERR(str_totext(tctx->linebreak, target));
131 RETERR(isc_hex_totext(&sr, tctx->width - 2, tctx->linebreak, target));
132 if ((tctx->flags & DNS_STYLEFLAG_MULTILINE) != 0)
133 RETERR(str_totext(" )", target));
134 return (ISC_R_SUCCESS);
137 static inline isc_result_t
138 fromwire_dlv(ARGS_FROMWIRE) {
139 isc_region_t sr;
141 REQUIRE(type == 32769);
143 UNUSED(type);
144 UNUSED(rdclass);
145 UNUSED(dctx);
146 UNUSED(options);
148 isc_buffer_activeregion(source, &sr);
151 * Check digest lengths if we know them.
153 if (sr.length < 4 ||
154 (sr.base[3] == DNS_DSDIGEST_SHA1 &&
155 sr.length < 4 + ISC_SHA1_DIGESTLENGTH) ||
156 (sr.base[3] == DNS_DSDIGEST_SHA256 &&
157 sr.length < 4 + ISC_SHA256_DIGESTLENGTH))
158 return (ISC_R_UNEXPECTEDEND);
161 * Only copy digest lengths if we know them.
162 * If there is extra data dns_rdata_fromwire() will
163 * detect that.
165 if (sr.base[3] == DNS_DSDIGEST_SHA1)
166 sr.length = 4 + ISC_SHA1_DIGESTLENGTH;
167 else if (sr.base[3] == DNS_DSDIGEST_SHA256)
168 sr.length = 4 + ISC_SHA256_DIGESTLENGTH;
170 isc_buffer_forward(source, sr.length);
171 return (mem_tobuffer(target, sr.base, sr.length));
174 static inline isc_result_t
175 towire_dlv(ARGS_TOWIRE) {
176 isc_region_t sr;
178 REQUIRE(rdata->type == 32769);
179 REQUIRE(rdata->length != 0);
181 UNUSED(cctx);
183 dns_rdata_toregion(rdata, &sr);
184 return (mem_tobuffer(target, sr.base, sr.length));
187 static inline int
188 compare_dlv(ARGS_COMPARE) {
189 isc_region_t r1;
190 isc_region_t r2;
192 REQUIRE(rdata1->type == rdata2->type);
193 REQUIRE(rdata1->rdclass == rdata2->rdclass);
194 REQUIRE(rdata1->type == 32769);
195 REQUIRE(rdata1->length != 0);
196 REQUIRE(rdata2->length != 0);
198 dns_rdata_toregion(rdata1, &r1);
199 dns_rdata_toregion(rdata2, &r2);
200 return (isc_region_compare(&r1, &r2));
203 static inline isc_result_t
204 fromstruct_dlv(ARGS_FROMSTRUCT) {
205 dns_rdata_dlv_t *dlv = source;
207 REQUIRE(type == 32769);
208 REQUIRE(source != NULL);
209 REQUIRE(dlv->common.rdtype == type);
210 REQUIRE(dlv->common.rdclass == rdclass);
211 switch (dlv->digest_type) {
212 case DNS_DSDIGEST_SHA1:
213 REQUIRE(dlv->length == ISC_SHA1_DIGESTLENGTH);
214 break;
215 case DNS_DSDIGEST_SHA256:
216 REQUIRE(dlv->length == ISC_SHA256_DIGESTLENGTH);
217 break;
220 UNUSED(type);
221 UNUSED(rdclass);
223 RETERR(uint16_tobuffer(dlv->key_tag, target));
224 RETERR(uint8_tobuffer(dlv->algorithm, target));
225 RETERR(uint8_tobuffer(dlv->digest_type, target));
227 return (mem_tobuffer(target, dlv->digest, dlv->length));
230 static inline isc_result_t
231 tostruct_dlv(ARGS_TOSTRUCT) {
232 dns_rdata_dlv_t *dlv = target;
233 isc_region_t region;
235 REQUIRE(rdata->type == 32769);
236 REQUIRE(target != NULL);
237 REQUIRE(rdata->length != 0);
239 dlv->common.rdclass = rdata->rdclass;
240 dlv->common.rdtype = rdata->type;
241 ISC_LINK_INIT(&dlv->common, link);
243 dns_rdata_toregion(rdata, &region);
245 dlv->key_tag = uint16_fromregion(&region);
246 isc_region_consume(&region, 2);
247 dlv->algorithm = uint8_fromregion(&region);
248 isc_region_consume(&region, 1);
249 dlv->digest_type = uint8_fromregion(&region);
250 isc_region_consume(&region, 1);
251 dlv->length = region.length;
253 dlv->digest = mem_maybedup(mctx, region.base, region.length);
254 if (dlv->digest == NULL)
255 return (ISC_R_NOMEMORY);
257 dlv->mctx = mctx;
258 return (ISC_R_SUCCESS);
261 static inline void
262 freestruct_dlv(ARGS_FREESTRUCT) {
263 dns_rdata_dlv_t *dlv = source;
265 REQUIRE(dlv != NULL);
266 REQUIRE(dlv->common.rdtype == 32769);
268 if (dlv->mctx == NULL)
269 return;
271 if (dlv->digest != NULL)
272 isc_mem_free(dlv->mctx, dlv->digest);
273 dlv->mctx = NULL;
276 static inline isc_result_t
277 additionaldata_dlv(ARGS_ADDLDATA) {
278 REQUIRE(rdata->type == 32769);
280 UNUSED(rdata);
281 UNUSED(add);
282 UNUSED(arg);
284 return (ISC_R_SUCCESS);
287 static inline isc_result_t
288 digest_dlv(ARGS_DIGEST) {
289 isc_region_t r;
291 REQUIRE(rdata->type == 32769);
293 dns_rdata_toregion(rdata, &r);
295 return ((digest)(arg, &r));
298 static inline isc_boolean_t
299 checkowner_dlv(ARGS_CHECKOWNER) {
301 REQUIRE(type == 32769);
303 UNUSED(name);
304 UNUSED(type);
305 UNUSED(rdclass);
306 UNUSED(wildcard);
308 return (ISC_TRUE);
311 static inline isc_boolean_t
312 checknames_dlv(ARGS_CHECKNAMES) {
314 REQUIRE(rdata->type == 32769);
316 UNUSED(rdata);
317 UNUSED(owner);
318 UNUSED(bad);
320 return (ISC_TRUE);
323 static inline int
324 casecompare_dlv(ARGS_COMPARE) {
325 return (compare_dlv(rdata1, rdata2));
328 #endif /* RDATA_GENERIC_DLV_32769_C */