Patrick Welche <prlw1@cam.ac.uk>
[netbsd-mini2440.git] / external / bsd / bind / dist / lib / dns / rdata / generic / hip_55.c
blob46c0f3a6b2eced19892bec99757c410adca850d8
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: hip_55.c,v 1.6 2009/12/04 22:06:37 tbox Exp */
21 /* reviewed: TBC */
23 /* RFC 5205 */
25 #ifndef RDATA_GENERIC_HIP_5_C
26 #define RDATA_GENERIC_HIP_5_C
28 #define RRTYPE_HIP_ATTRIBUTES (0)
30 static inline isc_result_t
31 fromtext_hip(ARGS_FROMTEXT) {
32 isc_token_t token;
33 dns_name_t name;
34 isc_buffer_t buffer;
35 isc_buffer_t hit_len;
36 isc_buffer_t key_len;
37 unsigned char *start;
38 size_t len;
40 REQUIRE(type == 55);
42 UNUSED(type);
43 UNUSED(rdclass);
44 UNUSED(callbacks);
47 * Dummy HIT len.
49 hit_len = *target;
50 RETERR(uint8_tobuffer(0, target));
53 * Algorithm.
55 RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_number,
56 ISC_FALSE));
57 if (token.value.as_ulong > 0xffU)
58 RETTOK(ISC_R_RANGE);
59 RETERR(uint8_tobuffer(token.value.as_ulong, target));
62 * Dummy KEY len.
64 key_len = *target;
65 RETERR(uint16_tobuffer(0, target));
68 * HIT (base16).
70 start = isc_buffer_used(target);
71 RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
72 ISC_FALSE));
73 RETTOK(isc_hex_decodestring(DNS_AS_STR(token), target));
76 * Fill in HIT len.
78 len = (unsigned char *)isc_buffer_used(target) - start;
79 if (len > 0xffU)
80 RETTOK(ISC_R_RANGE);
81 RETERR(uint8_tobuffer(len, &hit_len));
84 * Public key (base64).
86 start = isc_buffer_used(target);
87 RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
88 ISC_FALSE));
89 RETTOK(isc_base64_decodestring(DNS_AS_STR(token), target));
92 * Fill in KEY len.
94 len = (unsigned char *)isc_buffer_used(target) - start;
95 if (len > 0xffffU)
96 RETTOK(ISC_R_RANGE);
97 RETERR(uint16_tobuffer(len, &key_len));
100 * Rendezvous Servers.
102 dns_name_init(&name, NULL);
103 do {
104 RETERR(isc_lex_getmastertoken(lexer, &token,
105 isc_tokentype_string,
106 ISC_TRUE));
107 if (token.type != isc_tokentype_string)
108 break;
109 buffer_fromregion(&buffer, &token.value.as_region);
110 origin = (origin != NULL) ? origin : dns_rootname;
111 RETTOK(dns_name_fromtext(&name, &buffer, origin, options,
112 target));
113 } while (1);
116 * Let upper layer handle eol/eof.
118 isc_lex_ungettoken(lexer, &token);
120 return (ISC_R_SUCCESS);
123 static inline isc_result_t
124 totext_hip(ARGS_TOTEXT) {
125 isc_region_t region;
126 dns_name_t name;
127 dns_name_t prefix;
128 isc_boolean_t sub;
129 size_t length, key_len, hit_len;
130 unsigned char algorithm;
131 char buf[sizeof("225 ")];
133 REQUIRE(rdata->type == 55);
134 REQUIRE(rdata->length != 0);
136 dns_rdata_toregion(rdata, &region);
138 hit_len = uint8_fromregion(&region);
139 isc_region_consume(&region, 1);
141 algorithm = uint8_fromregion(&region);
142 isc_region_consume(&region, 1);
144 key_len = uint16_fromregion(&region);
145 isc_region_consume(&region, 2);
147 if ((tctx->flags & DNS_STYLEFLAG_MULTILINE) != 0)
148 RETERR(str_totext("( ", target));
151 * Algorithm
153 sprintf(buf, "%u ", algorithm);
154 RETERR(str_totext(buf, target));
157 * HIT.
159 INSIST(hit_len < region.length);
160 length = region.length;
161 region.length = hit_len;
162 RETERR(isc_hex_totext(&region, 1, "", target));
163 region.length = length - hit_len;
164 RETERR(str_totext(tctx->linebreak, target));
167 * Public KEY.
169 INSIST(key_len <= region.length);
170 length = region.length;
171 region.length = key_len;
172 RETERR(isc_base64_totext(&region, 1, "", target));
173 region.length = length - key_len;
174 RETERR(str_totext(tctx->linebreak, target));
177 * Rendezvous Servers.
179 dns_name_init(&name, NULL);
180 dns_name_init(&prefix, NULL);
181 while (region.length > 0) {
182 dns_name_fromregion(&name, &region);
184 sub = name_prefix(&name, tctx->origin, &prefix);
185 RETERR(dns_name_totext(&prefix, sub, target));
186 isc_region_consume(&region, name.length);
187 if (region.length > 0)
188 RETERR(str_totext(tctx->linebreak, target));
190 if ((tctx->flags & DNS_STYLEFLAG_MULTILINE) != 0)
191 RETERR(str_totext(" )", target));
192 return (ISC_R_SUCCESS);
195 static inline isc_result_t
196 fromwire_hip(ARGS_FROMWIRE) {
197 isc_region_t region, rr;
198 dns_name_t name;
199 isc_uint8_t hit_len;
200 isc_uint16_t key_len;
202 REQUIRE(type == 55);
204 UNUSED(type);
205 UNUSED(rdclass);
207 isc_buffer_activeregion(source, &region);
208 if (region.length < 4U)
209 RETERR(DNS_R_FORMERR);
211 rr = region;
212 hit_len = uint8_fromregion(&region);
213 if (hit_len == 0)
214 RETERR(DNS_R_FORMERR);
215 isc_region_consume(&region, 2); /* hit length + algorithm */
216 key_len = uint16_fromregion(&region);
217 if (key_len == 0)
218 RETERR(DNS_R_FORMERR);
219 isc_region_consume(&region, 2);
220 if (region.length < (unsigned) (hit_len + key_len))
221 RETERR(DNS_R_FORMERR);
223 RETERR(mem_tobuffer(target, rr.base, 4 + hit_len + key_len));
224 isc_buffer_forward(source, 4 + hit_len + key_len);
226 dns_decompress_setmethods(dctx, DNS_COMPRESS_NONE);
227 while (isc_buffer_activelength(source) > 0) {
228 dns_name_init(&name, NULL);
229 RETERR(dns_name_fromwire(&name, source, dctx, options, target));
231 return (ISC_R_SUCCESS);
234 static inline isc_result_t
235 towire_hip(ARGS_TOWIRE) {
236 isc_region_t region;
238 REQUIRE(rdata->type == 55);
239 REQUIRE(rdata->length != 0);
241 UNUSED(cctx);
243 dns_rdata_toregion(rdata, &region);
244 return (mem_tobuffer(target, region.base, region.length));
247 static inline int
248 compare_hip(ARGS_COMPARE) {
249 isc_region_t region1;
250 isc_region_t region2;
252 REQUIRE(rdata1->type == rdata2->type);
253 REQUIRE(rdata1->rdclass == rdata2->rdclass);
254 REQUIRE(rdata1->type == 55);
255 REQUIRE(rdata1->length != 0);
256 REQUIRE(rdata2->length != 0);
258 dns_rdata_toregion(rdata1, &region1);
259 dns_rdata_toregion(rdata2, &region2);
260 return (isc_region_compare(&region1, &region2));
263 static inline isc_result_t
264 fromstruct_hip(ARGS_FROMSTRUCT) {
265 dns_rdata_hip_t *hip = source;
266 dns_rdata_hip_t myhip;
267 isc_result_t result;
269 REQUIRE(type == 55);
270 REQUIRE(source != NULL);
271 REQUIRE(hip->common.rdtype == type);
272 REQUIRE(hip->common.rdclass == rdclass);
273 REQUIRE(hip->hit_len > 0 && hip->hit != NULL);
274 REQUIRE(hip->key_len > 0 && hip->key != NULL);
275 REQUIRE((hip->servers == NULL && hip->servers_len == 0) ||
276 (hip->servers != NULL && hip->servers_len != 0));
278 UNUSED(type);
279 UNUSED(rdclass);
281 RETERR(uint8_tobuffer(hip->hit_len, target));
282 RETERR(uint8_tobuffer(hip->algorithm, target));
283 RETERR(uint16_tobuffer(hip->key_len, target));
284 RETERR(mem_tobuffer(target, hip->hit, hip->hit_len));
285 RETERR(mem_tobuffer(target, hip->key, hip->key_len));
287 myhip = *hip;
288 for (result = dns_rdata_hip_first(&myhip);
289 result == ISC_R_SUCCESS;
290 result = dns_rdata_hip_next(&myhip))
291 /* empty */;
293 return(mem_tobuffer(target, hip->servers, hip->servers_len));
296 static inline isc_result_t
297 tostruct_hip(ARGS_TOSTRUCT) {
298 isc_region_t region;
299 dns_rdata_hip_t *hip = target;
301 REQUIRE(rdata->type == 55);
302 REQUIRE(target != NULL);
303 REQUIRE(rdata->length != 0);
305 hip->common.rdclass = rdata->rdclass;
306 hip->common.rdtype = rdata->type;
307 ISC_LINK_INIT(&hip->common, link);
309 dns_rdata_toregion(rdata, &region);
311 hip->hit_len = uint8_fromregion(&region);
312 isc_region_consume(&region, 1);
314 hip->algorithm = uint8_fromregion(&region);
315 isc_region_consume(&region, 1);
317 hip->key_len = uint16_fromregion(&region);
318 isc_region_consume(&region, 2);
320 hip->hit = hip->key = hip->servers = NULL;
322 hip->hit = mem_maybedup(mctx, region.base, hip->hit_len);
323 if (hip->hit == NULL)
324 goto cleanup;
325 isc_region_consume(&region, hip->hit_len);
327 hip->key = mem_maybedup(mctx, region.base, hip->key_len);
328 if (hip->key == NULL)
329 goto cleanup;
330 isc_region_consume(&region, hip->key_len);
332 hip->servers_len = region.length;
333 if (hip->servers_len != 0) {
334 hip->servers = mem_maybedup(mctx, region.base, region.length);
335 if (hip->servers == NULL)
336 goto cleanup;
339 hip->offset = hip->servers_len;
340 hip->mctx = mctx;
341 return (ISC_R_SUCCESS);
343 cleanup:
344 if (hip->hit != NULL)
345 isc_mem_free(mctx, hip->hit);
346 if (hip->key != NULL)
347 isc_mem_free(mctx, hip->key);
348 if (hip->servers != NULL)
349 isc_mem_free(mctx, hip->servers);
350 return (ISC_R_NOMEMORY);
354 static inline void
355 freestruct_hip(ARGS_FREESTRUCT) {
356 dns_rdata_hip_t *hip = source;
358 REQUIRE(source != NULL);
360 if (hip->mctx == NULL)
361 return;
363 isc_mem_free(hip->mctx, hip->hit);
364 isc_mem_free(hip->mctx, hip->key);
365 if (hip->servers != NULL)
366 isc_mem_free(hip->mctx, hip->servers);
367 hip->mctx = NULL;
370 static inline isc_result_t
371 additionaldata_hip(ARGS_ADDLDATA) {
372 UNUSED(rdata);
373 UNUSED(add);
374 UNUSED(arg);
376 REQUIRE(rdata->type == 55);
378 return (ISC_R_SUCCESS);
381 static inline isc_result_t
382 digest_hip(ARGS_DIGEST) {
383 isc_region_t r;
385 REQUIRE(rdata->type == 55);
387 dns_rdata_toregion(rdata, &r);
388 return ((digest)(arg, &r));
391 static inline isc_boolean_t
392 checkowner_hip(ARGS_CHECKOWNER) {
394 REQUIRE(type == 55);
396 UNUSED(name);
397 UNUSED(type);
398 UNUSED(rdclass);
399 UNUSED(wildcard);
401 return (ISC_TRUE);
404 static inline isc_boolean_t
405 checknames_hip(ARGS_CHECKNAMES) {
407 REQUIRE(rdata->type == 55);
409 UNUSED(rdata);
410 UNUSED(owner);
411 UNUSED(bad);
413 return (ISC_TRUE);
416 isc_result_t
417 dns_rdata_hip_first(dns_rdata_hip_t *hip) {
418 if (hip->servers_len == 0)
419 return (ISC_R_NOMORE);
420 hip->offset = 0;
421 return (ISC_R_SUCCESS);
424 isc_result_t
425 dns_rdata_hip_next(dns_rdata_hip_t *hip) {
426 isc_region_t region;
427 dns_name_t name;
429 if (hip->offset >= hip->servers_len)
430 return (ISC_R_NOMORE);
432 region.base = hip->servers + hip->offset;
433 region.length = hip->servers_len - hip->offset;
434 dns_name_init(&name, NULL);
435 dns_name_fromregion(&name, &region);
436 hip->offset += name.length;
437 INSIST(hip->offset <= hip->servers_len);
438 return (ISC_R_SUCCESS);
441 void
442 dns_rdata_hip_current(dns_rdata_hip_t *hip, dns_name_t *name) {
443 isc_region_t region;
445 REQUIRE(hip->offset < hip->servers_len);
447 region.base = hip->servers + hip->offset;
448 region.length = hip->servers_len - hip->offset;
449 dns_name_fromregion(name, &region);
451 INSIST(name->length + hip->offset <= hip->servers_len);
454 static inline int
455 casecompare_hip(ARGS_COMPARE) {
456 isc_region_t r1;
457 isc_region_t r2;
458 dns_name_t name1;
459 dns_name_t name2;
460 int order;
461 isc_uint8_t hit_len;
462 isc_uint16_t key_len;
464 REQUIRE(rdata1->type == rdata2->type);
465 REQUIRE(rdata1->rdclass == rdata2->rdclass);
466 REQUIRE(rdata1->type == 55);
467 REQUIRE(rdata1->length != 0);
468 REQUIRE(rdata2->length != 0);
470 dns_rdata_toregion(rdata1, &r1);
471 dns_rdata_toregion(rdata2, &r2);
473 INSIST(r1.length > 4);
474 INSIST(r2.length > 4);
475 r1.length = 4;
476 r2.length = 4;
477 order = isc_region_compare(&r1, &r2);
478 if (order != 0)
479 return (order);
481 hit_len = uint8_fromregion(&r1);
482 isc_region_consume(&r1, 2); /* hit length + algorithm */
483 key_len = uint16_fromregion(&r1);
485 dns_rdata_toregion(rdata1, &r1);
486 dns_rdata_toregion(rdata2, &r2);
487 isc_region_consume(&r1, 4);
488 isc_region_consume(&r2, 4);
489 INSIST(r1.length >= (unsigned) (hit_len + key_len));
490 INSIST(r2.length >= (unsigned) (hit_len + key_len));
491 order = isc_region_compare(&r1, &r2);
492 if (order != 0)
493 return (order);
494 isc_region_consume(&r1, hit_len + key_len);
495 isc_region_consume(&r2, hit_len + key_len);
497 dns_name_init(&name1, NULL);
498 dns_name_init(&name2, NULL);
499 while (r1.length != 0 && r2.length != 0) {
500 dns_name_fromregion(&name1, &r1);
501 dns_name_fromregion(&name2, &r2);
502 order = dns_name_rdatacompare(&name1, &name2);
503 if (order != 0)
504 return (order);
506 isc_region_consume(&r1, name_length(&name1));
507 isc_region_consume(&r2, name_length(&name2));
509 return (isc_region_compare(&r1, &r2));
512 #endif /* RDATA_GENERIC_HIP_5_C */