Patrick Welche <prlw1@cam.ac.uk>
[netbsd-mini2440.git] / external / bsd / bind / dist / lib / dns / rdata / generic / keydata_65533.c
blob859f91c46c686eaa6534df78d2f0371dd77caf49
1 /* $NetBSD$ */
3 /*
4 * Copyright (C) 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: keydata_65533.c,v 1.3 2009/12/04 21:09:33 marka Exp */
21 #ifndef GENERIC_KEYDATA_65533_C
22 #define GENERIC_KEYDATA_65533_C 1
24 #include <dst/dst.h>
26 #define RRTYPE_KEYDATA_ATTRIBUTES (DNS_RDATATYPEATTR_DNSSEC)
28 static inline isc_result_t
29 fromtext_keydata(ARGS_FROMTEXT) {
30 isc_token_t token;
31 dns_secalg_t alg;
32 dns_secproto_t proto;
33 dns_keyflags_t flags;
34 isc_uint32_t refresh, addhd, removehd;
36 REQUIRE(type == 65533);
38 UNUSED(type);
39 UNUSED(rdclass);
40 UNUSED(origin);
41 UNUSED(options);
42 UNUSED(callbacks);
44 /* refresh timer */
45 RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
46 ISC_FALSE));
47 RETTOK(dns_time32_fromtext(DNS_AS_STR(token), &refresh));
48 RETERR(uint32_tobuffer(refresh, target));
50 /* add hold-down */
51 RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
52 ISC_FALSE));
53 RETTOK(dns_time32_fromtext(DNS_AS_STR(token), &addhd));
54 RETERR(uint32_tobuffer(addhd, target));
56 /* remove hold-down */
57 RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
58 ISC_FALSE));
59 RETTOK(dns_time32_fromtext(DNS_AS_STR(token), &removehd));
60 RETERR(uint32_tobuffer(removehd, target));
62 /* flags */
63 RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
64 ISC_FALSE));
65 RETTOK(dns_keyflags_fromtext(&flags, &token.value.as_textregion));
66 RETERR(uint16_tobuffer(flags, target));
68 /* protocol */
69 RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
70 ISC_FALSE));
71 RETTOK(dns_secproto_fromtext(&proto, &token.value.as_textregion));
72 RETERR(mem_tobuffer(target, &proto, 1));
74 /* algorithm */
75 RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
76 ISC_FALSE));
77 RETTOK(dns_secalg_fromtext(&alg, &token.value.as_textregion));
78 RETERR(mem_tobuffer(target, &alg, 1));
80 /* No Key? */
81 if ((flags & 0xc000) == 0xc000)
82 return (ISC_R_SUCCESS);
84 return (isc_base64_tobuffer(lexer, target, -1));
87 static inline isc_result_t
88 totext_keydata(ARGS_TOTEXT) {
89 isc_region_t sr;
90 char buf[sizeof("64000")];
91 unsigned int flags;
92 unsigned char algorithm;
93 unsigned long when;
95 REQUIRE(rdata->type == 65533);
96 REQUIRE(rdata->length != 0);
98 dns_rdata_toregion(rdata, &sr);
100 /* refresh timer */
101 when = uint32_fromregion(&sr);
102 isc_region_consume(&sr, 4);
103 RETERR(dns_time32_totext(when, target));
104 RETERR(str_totext(" ", target));
106 /* add hold-down */
107 when = uint32_fromregion(&sr);
108 isc_region_consume(&sr, 4);
109 RETERR(dns_time32_totext(when, target));
110 RETERR(str_totext(" ", target));
112 /* remove hold-down */
113 when = uint32_fromregion(&sr);
114 isc_region_consume(&sr, 4);
115 RETERR(dns_time32_totext(when, target));
116 RETERR(str_totext(" ", target));
118 /* flags */
119 flags = uint16_fromregion(&sr);
120 isc_region_consume(&sr, 2);
121 sprintf(buf, "%u", flags);
122 RETERR(str_totext(buf, target));
123 RETERR(str_totext(" ", target));
125 /* protocol */
126 sprintf(buf, "%u", sr.base[0]);
127 isc_region_consume(&sr, 1);
128 RETERR(str_totext(buf, target));
129 RETERR(str_totext(" ", target));
131 /* algorithm */
132 algorithm = sr.base[0];
133 sprintf(buf, "%u", algorithm);
134 isc_region_consume(&sr, 1);
135 RETERR(str_totext(buf, target));
137 /* No Key? */
138 if ((flags & 0xc000) == 0xc000)
139 return (ISC_R_SUCCESS);
141 /* key */
142 if ((tctx->flags & DNS_STYLEFLAG_MULTILINE) != 0)
143 RETERR(str_totext(" (", target));
144 RETERR(str_totext(tctx->linebreak, target));
145 RETERR(isc_base64_totext(&sr, tctx->width - 2,
146 tctx->linebreak, target));
148 if ((tctx->flags & DNS_STYLEFLAG_COMMENT) != 0)
149 RETERR(str_totext(tctx->linebreak, target));
150 else if ((tctx->flags & DNS_STYLEFLAG_MULTILINE) != 0)
151 RETERR(str_totext(" ", target));
153 if ((tctx->flags & DNS_STYLEFLAG_MULTILINE) != 0)
154 RETERR(str_totext(")", target));
156 if ((tctx->flags & DNS_STYLEFLAG_COMMENT) != 0) {
157 isc_region_t tmpr;
159 RETERR(str_totext(" ; key id = ", target));
160 dns_rdata_toregion(rdata, &tmpr);
161 /* Skip over refresh, addhd, and removehd */
162 isc_region_consume(&tmpr, 12);
163 sprintf(buf, "%u", dst_region_computeid(&tmpr, algorithm));
164 RETERR(str_totext(buf, target));
166 return (ISC_R_SUCCESS);
169 static inline isc_result_t
170 fromwire_keydata(ARGS_FROMWIRE) {
171 isc_region_t sr;
173 REQUIRE(type == 65533);
175 UNUSED(type);
176 UNUSED(rdclass);
177 UNUSED(dctx);
178 UNUSED(options);
180 isc_buffer_activeregion(source, &sr);
181 if (sr.length < 4)
182 return (ISC_R_UNEXPECTEDEND);
184 isc_buffer_forward(source, sr.length);
185 return (mem_tobuffer(target, sr.base, sr.length));
188 static inline isc_result_t
189 towire_keydata(ARGS_TOWIRE) {
190 isc_region_t sr;
192 REQUIRE(rdata->type == 65533);
193 REQUIRE(rdata->length != 0);
195 UNUSED(cctx);
197 dns_rdata_toregion(rdata, &sr);
198 return (mem_tobuffer(target, sr.base, sr.length));
201 static inline int
202 compare_keydata(ARGS_COMPARE) {
203 isc_region_t r1;
204 isc_region_t r2;
206 REQUIRE(rdata1->type == rdata2->type);
207 REQUIRE(rdata1->rdclass == rdata2->rdclass);
208 REQUIRE(rdata1->type == 65533);
209 REQUIRE(rdata1->length != 0);
210 REQUIRE(rdata2->length != 0);
212 dns_rdata_toregion(rdata1, &r1);
213 dns_rdata_toregion(rdata2, &r2);
214 return (isc_region_compare(&r1, &r2));
217 static inline isc_result_t
218 fromstruct_keydata(ARGS_FROMSTRUCT) {
219 dns_rdata_keydata_t *keydata = source;
221 REQUIRE(type == 65533);
222 REQUIRE(source != NULL);
223 REQUIRE(keydata->common.rdtype == type);
224 REQUIRE(keydata->common.rdclass == rdclass);
226 UNUSED(type);
227 UNUSED(rdclass);
229 /* Refresh timer */
230 RETERR(uint32_tobuffer(keydata->refresh, target));
232 /* Add hold-down */
233 RETERR(uint32_tobuffer(keydata->addhd, target));
235 /* Remove hold-down */
236 RETERR(uint32_tobuffer(keydata->removehd, target));
238 /* Flags */
239 RETERR(uint16_tobuffer(keydata->flags, target));
241 /* Protocol */
242 RETERR(uint8_tobuffer(keydata->protocol, target));
244 /* Algorithm */
245 RETERR(uint8_tobuffer(keydata->algorithm, target));
247 /* Data */
248 return (mem_tobuffer(target, keydata->data, keydata->datalen));
251 static inline isc_result_t
252 tostruct_keydata(ARGS_TOSTRUCT) {
253 dns_rdata_keydata_t *keydata = target;
254 isc_region_t sr;
256 REQUIRE(rdata->type == 65533);
257 REQUIRE(target != NULL);
258 REQUIRE(rdata->length != 0);
260 keydata->common.rdclass = rdata->rdclass;
261 keydata->common.rdtype = rdata->type;
262 ISC_LINK_INIT(&keydata->common, link);
264 dns_rdata_toregion(rdata, &sr);
266 /* Refresh timer */
267 if (sr.length < 4)
268 return (ISC_R_UNEXPECTEDEND);
269 keydata->refresh = uint32_fromregion(&sr);
270 isc_region_consume(&sr, 4);
272 /* Add hold-down */
273 if (sr.length < 4)
274 return (ISC_R_UNEXPECTEDEND);
275 keydata->addhd = uint32_fromregion(&sr);
276 isc_region_consume(&sr, 4);
278 /* Remove hold-down */
279 if (sr.length < 4)
280 return (ISC_R_UNEXPECTEDEND);
281 keydata->removehd = uint32_fromregion(&sr);
282 isc_region_consume(&sr, 4);
284 /* Flags */
285 if (sr.length < 2)
286 return (ISC_R_UNEXPECTEDEND);
287 keydata->flags = uint16_fromregion(&sr);
288 isc_region_consume(&sr, 2);
290 /* Protocol */
291 if (sr.length < 1)
292 return (ISC_R_UNEXPECTEDEND);
293 keydata->protocol = uint8_fromregion(&sr);
294 isc_region_consume(&sr, 1);
296 /* Algorithm */
297 if (sr.length < 1)
298 return (ISC_R_UNEXPECTEDEND);
299 keydata->algorithm = uint8_fromregion(&sr);
300 isc_region_consume(&sr, 1);
302 /* Data */
303 keydata->datalen = sr.length;
304 keydata->data = mem_maybedup(mctx, sr.base, keydata->datalen);
305 if (keydata->data == NULL)
306 return (ISC_R_NOMEMORY);
308 keydata->mctx = mctx;
309 return (ISC_R_SUCCESS);
312 static inline void
313 freestruct_keydata(ARGS_FREESTRUCT) {
314 dns_rdata_keydata_t *keydata = (dns_rdata_keydata_t *) source;
316 REQUIRE(source != NULL);
317 REQUIRE(keydata->common.rdtype == 65533);
319 if (keydata->mctx == NULL)
320 return;
322 if (keydata->data != NULL)
323 isc_mem_free(keydata->mctx, keydata->data);
324 keydata->mctx = NULL;
327 static inline isc_result_t
328 additionaldata_keydata(ARGS_ADDLDATA) {
329 REQUIRE(rdata->type == 65533);
331 UNUSED(rdata);
332 UNUSED(add);
333 UNUSED(arg);
335 return (ISC_R_SUCCESS);
338 static inline isc_result_t
339 digest_keydata(ARGS_DIGEST) {
340 isc_region_t r;
342 REQUIRE(rdata->type == 65533);
344 dns_rdata_toregion(rdata, &r);
346 return ((digest)(arg, &r));
349 static inline isc_boolean_t
350 checkowner_keydata(ARGS_CHECKOWNER) {
352 REQUIRE(type == 65533);
354 UNUSED(name);
355 UNUSED(type);
356 UNUSED(rdclass);
357 UNUSED(wildcard);
359 return (ISC_TRUE);
362 static inline isc_boolean_t
363 checknames_keydata(ARGS_CHECKNAMES) {
365 REQUIRE(rdata->type == 65533);
367 UNUSED(rdata);
368 UNUSED(owner);
369 UNUSED(bad);
371 return (ISC_TRUE);
374 static inline int
375 casecompare_keydata(ARGS_COMPARE) {
376 return (compare_keydata(rdata1, rdata2));
379 #endif /* GENERIC_KEYDATA_65533_C */