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 */
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
) {
50 RETERR(uint8_tobuffer(0, target
));
55 RETERR(isc_lex_getmastertoken(lexer
, &token
, isc_tokentype_number
,
57 if (token
.value
.as_ulong
> 0xffU
)
59 RETERR(uint8_tobuffer(token
.value
.as_ulong
, target
));
65 RETERR(uint16_tobuffer(0, target
));
70 start
= isc_buffer_used(target
);
71 RETERR(isc_lex_getmastertoken(lexer
, &token
, isc_tokentype_string
,
73 RETTOK(isc_hex_decodestring(DNS_AS_STR(token
), target
));
78 len
= (unsigned char *)isc_buffer_used(target
) - start
;
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
,
89 RETTOK(isc_base64_decodestring(DNS_AS_STR(token
), target
));
94 len
= (unsigned char *)isc_buffer_used(target
) - start
;
97 RETERR(uint16_tobuffer(len
, &key_len
));
100 * Rendezvous Servers.
102 dns_name_init(&name
, NULL
);
104 RETERR(isc_lex_getmastertoken(lexer
, &token
,
105 isc_tokentype_string
,
107 if (token
.type
!= isc_tokentype_string
)
109 buffer_fromregion(&buffer
, &token
.value
.as_region
);
110 origin
= (origin
!= NULL
) ? origin
: dns_rootname
;
111 RETTOK(dns_name_fromtext(&name
, &buffer
, origin
, options
,
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
) {
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
, ®ion
);
138 hit_len
= uint8_fromregion(®ion
);
139 isc_region_consume(®ion
, 1);
141 algorithm
= uint8_fromregion(®ion
);
142 isc_region_consume(®ion
, 1);
144 key_len
= uint16_fromregion(®ion
);
145 isc_region_consume(®ion
, 2);
147 if ((tctx
->flags
& DNS_STYLEFLAG_MULTILINE
) != 0)
148 RETERR(str_totext("( ", target
));
153 sprintf(buf
, "%u ", algorithm
);
154 RETERR(str_totext(buf
, target
));
159 INSIST(hit_len
< region
.length
);
160 length
= region
.length
;
161 region
.length
= hit_len
;
162 RETERR(isc_hex_totext(®ion
, 1, "", target
));
163 region
.length
= length
- hit_len
;
164 RETERR(str_totext(tctx
->linebreak
, target
));
169 INSIST(key_len
<= region
.length
);
170 length
= region
.length
;
171 region
.length
= key_len
;
172 RETERR(isc_base64_totext(®ion
, 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
, ®ion
);
184 sub
= name_prefix(&name
, tctx
->origin
, &prefix
);
185 RETERR(dns_name_totext(&prefix
, sub
, target
));
186 isc_region_consume(®ion
, 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
;
200 isc_uint16_t key_len
;
207 isc_buffer_activeregion(source
, ®ion
);
208 if (region
.length
< 4U)
209 RETERR(DNS_R_FORMERR
);
212 hit_len
= uint8_fromregion(®ion
);
214 RETERR(DNS_R_FORMERR
);
215 isc_region_consume(®ion
, 2); /* hit length + algorithm */
216 key_len
= uint16_fromregion(®ion
);
218 RETERR(DNS_R_FORMERR
);
219 isc_region_consume(®ion
, 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
) {
238 REQUIRE(rdata
->type
== 55);
239 REQUIRE(rdata
->length
!= 0);
243 dns_rdata_toregion(rdata
, ®ion
);
244 return (mem_tobuffer(target
, region
.base
, region
.length
));
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
, ®ion1
);
259 dns_rdata_toregion(rdata2
, ®ion2
);
260 return (isc_region_compare(®ion1
, ®ion2
));
263 static inline isc_result_t
264 fromstruct_hip(ARGS_FROMSTRUCT
) {
265 dns_rdata_hip_t
*hip
= source
;
266 dns_rdata_hip_t myhip
;
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));
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
));
288 for (result
= dns_rdata_hip_first(&myhip
);
289 result
== ISC_R_SUCCESS
;
290 result
= dns_rdata_hip_next(&myhip
))
293 return(mem_tobuffer(target
, hip
->servers
, hip
->servers_len
));
296 static inline isc_result_t
297 tostruct_hip(ARGS_TOSTRUCT
) {
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
, ®ion
);
311 hip
->hit_len
= uint8_fromregion(®ion
);
312 isc_region_consume(®ion
, 1);
314 hip
->algorithm
= uint8_fromregion(®ion
);
315 isc_region_consume(®ion
, 1);
317 hip
->key_len
= uint16_fromregion(®ion
);
318 isc_region_consume(®ion
, 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
)
325 isc_region_consume(®ion
, hip
->hit_len
);
327 hip
->key
= mem_maybedup(mctx
, region
.base
, hip
->key_len
);
328 if (hip
->key
== NULL
)
330 isc_region_consume(®ion
, 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
)
339 hip
->offset
= hip
->servers_len
;
341 return (ISC_R_SUCCESS
);
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
);
355 freestruct_hip(ARGS_FREESTRUCT
) {
356 dns_rdata_hip_t
*hip
= source
;
358 REQUIRE(source
!= NULL
);
360 if (hip
->mctx
== NULL
)
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
);
370 static inline isc_result_t
371 additionaldata_hip(ARGS_ADDLDATA
) {
376 REQUIRE(rdata
->type
== 55);
378 return (ISC_R_SUCCESS
);
381 static inline isc_result_t
382 digest_hip(ARGS_DIGEST
) {
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
) {
404 static inline isc_boolean_t
405 checknames_hip(ARGS_CHECKNAMES
) {
407 REQUIRE(rdata
->type
== 55);
417 dns_rdata_hip_first(dns_rdata_hip_t
*hip
) {
418 if (hip
->servers_len
== 0)
419 return (ISC_R_NOMORE
);
421 return (ISC_R_SUCCESS
);
425 dns_rdata_hip_next(dns_rdata_hip_t
*hip
) {
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
, ®ion
);
436 hip
->offset
+= name
.length
;
437 INSIST(hip
->offset
<= hip
->servers_len
);
438 return (ISC_R_SUCCESS
);
442 dns_rdata_hip_current(dns_rdata_hip_t
*hip
, dns_name_t
*name
) {
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
, ®ion
);
451 INSIST(name
->length
+ hip
->offset
<= hip
->servers_len
);
455 casecompare_hip(ARGS_COMPARE
) {
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);
477 order
= isc_region_compare(&r1
, &r2
);
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
);
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
);
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 */