4 * Copyright (C) 2005, 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: ipseckey_45.c,v 1.9 2009/12/04 22:06:37 tbox Exp */
21 #ifndef RDATA_GENERIC_IPSECKEY_45_C
22 #define RDATA_GENERIC_IPSECKEY_45_C
28 #define RRTYPE_IPSECKEY_ATTRIBUTES (0)
30 static inline isc_result_t
31 fromtext_ipseckey(ARGS_FROMTEXT
) {
37 unsigned char addr6
[16];
49 RETERR(isc_lex_getmastertoken(lexer
, &token
, isc_tokentype_number
,
51 if (token
.value
.as_ulong
> 0xffU
)
53 RETERR(uint8_tobuffer(token
.value
.as_ulong
, target
));
58 RETERR(isc_lex_getmastertoken(lexer
, &token
, isc_tokentype_number
,
60 if (token
.value
.as_ulong
> 0x3U
)
62 RETERR(uint8_tobuffer(token
.value
.as_ulong
, target
));
63 gateway
= token
.value
.as_ulong
;
68 RETERR(isc_lex_getmastertoken(lexer
, &token
, isc_tokentype_number
,
70 if (token
.value
.as_ulong
> 0xffU
)
72 RETERR(uint8_tobuffer(token
.value
.as_ulong
, target
));
77 RETERR(isc_lex_getmastertoken(lexer
, &token
, isc_tokentype_string
,
82 if (strcmp(DNS_AS_STR(token
), ".") != 0)
87 if (getquad(DNS_AS_STR(token
), &addr
, lexer
, callbacks
) != 1)
88 RETTOK(DNS_R_BADDOTTEDQUAD
);
89 isc_buffer_availableregion(target
, ®ion
);
90 if (region
.length
< 4)
91 return (ISC_R_NOSPACE
);
92 memcpy(region
.base
, &addr
, 4);
93 isc_buffer_add(target
, 4);
97 if (inet_pton(AF_INET6
, DNS_AS_STR(token
), addr6
) != 1)
98 RETTOK(DNS_R_BADAAAA
);
99 isc_buffer_availableregion(target
, ®ion
);
100 if (region
.length
< 16)
101 return (ISC_R_NOSPACE
);
102 memcpy(region
.base
, addr6
, 16);
103 isc_buffer_add(target
, 16);
107 dns_name_init(&name
, NULL
);
108 buffer_fromregion(&buffer
, &token
.value
.as_region
);
109 origin
= (origin
!= NULL
) ? origin
: dns_rootname
;
110 RETTOK(dns_name_fromtext(&name
, &buffer
, origin
,
118 return (isc_base64_tobuffer(lexer
, target
, -1));
121 static inline isc_result_t
122 totext_ipseckey(ARGS_TOTEXT
) {
127 char buf
[sizeof("255 ")];
129 unsigned short gateway
;
131 REQUIRE(rdata
->type
== 45);
132 REQUIRE(rdata
->length
>= 3);
134 dns_name_init(&name
, NULL
);
135 dns_name_init(&prefix
, NULL
);
137 if (rdata
->data
[1] > 3U)
138 return (ISC_R_NOTIMPLEMENTED
);
140 if ((tctx
->flags
& DNS_STYLEFLAG_MULTILINE
) != 0)
141 RETERR(str_totext("( ", target
));
146 dns_rdata_toregion(rdata
, ®ion
);
147 num
= uint8_fromregion(®ion
);
148 isc_region_consume(®ion
, 1);
149 sprintf(buf
, "%u ", num
);
150 RETERR(str_totext(buf
, target
));
155 gateway
= uint8_fromregion(®ion
);
156 isc_region_consume(®ion
, 1);
157 sprintf(buf
, "%u ", gateway
);
158 RETERR(str_totext(buf
, target
));
163 num
= uint8_fromregion(®ion
);
164 isc_region_consume(®ion
, 1);
165 sprintf(buf
, "%u ", num
);
166 RETERR(str_totext(buf
, target
));
173 RETERR(str_totext(".", target
));
177 RETERR(inet_totext(AF_INET
, ®ion
, target
));
178 isc_region_consume(®ion
, 4);
182 RETERR(inet_totext(AF_INET6
, ®ion
, target
));
183 isc_region_consume(®ion
, 16);
187 dns_name_fromregion(&name
, ®ion
);
188 sub
= name_prefix(&name
, tctx
->origin
, &prefix
);
189 RETERR(dns_name_totext(&prefix
, sub
, target
));
190 isc_region_consume(®ion
, name_length(&name
));
197 if (region
.length
> 0U) {
198 RETERR(str_totext(tctx
->linebreak
, target
));
199 RETERR(isc_base64_totext(®ion
, tctx
->width
- 2,
200 tctx
->linebreak
, target
));
203 if ((tctx
->flags
& DNS_STYLEFLAG_MULTILINE
) != 0)
204 RETERR(str_totext(" )", target
));
205 return (ISC_R_SUCCESS
);
208 static inline isc_result_t
209 fromwire_ipseckey(ARGS_FROMWIRE
) {
218 dns_decompress_setmethods(dctx
, DNS_COMPRESS_NONE
);
220 dns_name_init(&name
, NULL
);
222 isc_buffer_activeregion(source
, ®ion
);
223 if (region
.length
< 3)
224 return (ISC_R_UNEXPECTEDEND
);
226 switch (region
.base
[1]) {
228 isc_buffer_forward(source
, region
.length
);
229 return (mem_tobuffer(target
, region
.base
, region
.length
));
232 if (region
.length
< 7)
233 return (ISC_R_UNEXPECTEDEND
);
234 isc_buffer_forward(source
, region
.length
);
235 return (mem_tobuffer(target
, region
.base
, region
.length
));
238 if (region
.length
< 19)
239 return (ISC_R_UNEXPECTEDEND
);
240 isc_buffer_forward(source
, region
.length
);
241 return (mem_tobuffer(target
, region
.base
, region
.length
));
244 RETERR(mem_tobuffer(target
, region
.base
, 3));
245 isc_buffer_forward(source
, 3);
246 RETERR(dns_name_fromwire(&name
, source
, dctx
, options
, target
));
247 isc_buffer_activeregion(source
, ®ion
);
248 isc_buffer_forward(source
, region
.length
);
249 return(mem_tobuffer(target
, region
.base
, region
.length
));
252 return (ISC_R_NOTIMPLEMENTED
);
256 static inline isc_result_t
257 towire_ipseckey(ARGS_TOWIRE
) {
260 REQUIRE(rdata
->type
== 45);
261 REQUIRE(rdata
->length
!= 0);
265 dns_rdata_toregion(rdata
, ®ion
);
266 return (mem_tobuffer(target
, region
.base
, region
.length
));
270 compare_ipseckey(ARGS_COMPARE
) {
271 isc_region_t region1
;
272 isc_region_t region2
;
274 REQUIRE(rdata1
->type
== rdata2
->type
);
275 REQUIRE(rdata1
->rdclass
== rdata2
->rdclass
);
276 REQUIRE(rdata1
->type
== 45);
277 REQUIRE(rdata1
->length
>= 3);
278 REQUIRE(rdata2
->length
>= 3);
280 dns_rdata_toregion(rdata1
, ®ion1
);
281 dns_rdata_toregion(rdata2
, ®ion2
);
283 return (isc_region_compare(®ion1
, ®ion2
));
286 static inline isc_result_t
287 fromstruct_ipseckey(ARGS_FROMSTRUCT
) {
288 dns_rdata_ipseckey_t
*ipseckey
= source
;
293 REQUIRE(source
!= NULL
);
294 REQUIRE(ipseckey
->common
.rdtype
== type
);
295 REQUIRE(ipseckey
->common
.rdclass
== rdclass
);
300 if (ipseckey
->gateway_type
> 3U)
301 return (ISC_R_NOTIMPLEMENTED
);
303 RETERR(uint8_tobuffer(ipseckey
->precedence
, target
));
304 RETERR(uint8_tobuffer(ipseckey
->gateway_type
, target
));
305 RETERR(uint8_tobuffer(ipseckey
->algorithm
, target
));
307 switch (ipseckey
->gateway_type
) {
312 n
= ntohl(ipseckey
->in_addr
.s_addr
);
313 RETERR(uint32_tobuffer(n
, target
));
317 RETERR(mem_tobuffer(target
, ipseckey
->in6_addr
.s6_addr
, 16));
321 dns_name_toregion(&ipseckey
->gateway
, ®ion
);
322 RETERR(isc_buffer_copyregion(target
, ®ion
));
326 return (mem_tobuffer(target
, ipseckey
->key
, ipseckey
->keylength
));
329 static inline isc_result_t
330 tostruct_ipseckey(ARGS_TOSTRUCT
) {
332 dns_rdata_ipseckey_t
*ipseckey
= target
;
336 REQUIRE(rdata
->type
== 45);
337 REQUIRE(target
!= NULL
);
338 REQUIRE(rdata
->length
>= 3);
340 if (rdata
->data
[1] > 3U)
341 return (ISC_R_NOTIMPLEMENTED
);
343 ipseckey
->common
.rdclass
= rdata
->rdclass
;
344 ipseckey
->common
.rdtype
= rdata
->type
;
345 ISC_LINK_INIT(&ipseckey
->common
, link
);
347 dns_name_init(&name
, NULL
);
348 dns_rdata_toregion(rdata
, ®ion
);
350 ipseckey
->precedence
= uint8_fromregion(®ion
);
351 isc_region_consume(®ion
, 1);
353 ipseckey
->gateway_type
= uint8_fromregion(®ion
);
354 isc_region_consume(®ion
, 1);
356 ipseckey
->algorithm
= uint8_fromregion(®ion
);
357 isc_region_consume(®ion
, 1);
359 switch (ipseckey
->gateway_type
) {
364 n
= uint32_fromregion(®ion
);
365 ipseckey
->in_addr
.s_addr
= htonl(n
);
366 isc_region_consume(®ion
, 4);
370 memcpy(ipseckey
->in6_addr
.s6_addr
, region
.base
, 16);
371 isc_region_consume(®ion
, 16);
375 dns_name_init(&ipseckey
->gateway
, NULL
);
376 dns_name_fromregion(&name
, ®ion
);
377 RETERR(name_duporclone(&name
, mctx
, &ipseckey
->gateway
));
378 isc_region_consume(®ion
, name_length(&name
));
382 ipseckey
->keylength
= region
.length
;
383 if (ipseckey
->keylength
!= 0U) {
384 ipseckey
->key
= mem_maybedup(mctx
, region
.base
,
385 ipseckey
->keylength
);
386 if (ipseckey
->key
== NULL
) {
387 if (ipseckey
->gateway_type
== 3)
388 dns_name_free(&ipseckey
->gateway
,
390 return (ISC_R_NOMEMORY
);
393 ipseckey
->key
= NULL
;
395 ipseckey
->mctx
= mctx
;
396 return (ISC_R_SUCCESS
);
400 freestruct_ipseckey(ARGS_FREESTRUCT
) {
401 dns_rdata_ipseckey_t
*ipseckey
= source
;
403 REQUIRE(source
!= NULL
);
404 REQUIRE(ipseckey
->common
.rdtype
== 45);
406 if (ipseckey
->mctx
== NULL
)
409 if (ipseckey
->gateway_type
== 3)
410 dns_name_free(&ipseckey
->gateway
, ipseckey
->mctx
);
412 if (ipseckey
->key
!= NULL
)
413 isc_mem_free(ipseckey
->mctx
, ipseckey
->key
);
415 ipseckey
->mctx
= NULL
;
418 static inline isc_result_t
419 additionaldata_ipseckey(ARGS_ADDLDATA
) {
421 REQUIRE(rdata
->type
== 45);
427 return (ISC_R_SUCCESS
);
430 static inline isc_result_t
431 digest_ipseckey(ARGS_DIGEST
) {
434 REQUIRE(rdata
->type
== 45);
436 dns_rdata_toregion(rdata
, ®ion
);
437 return ((digest
)(arg
, ®ion
));
440 static inline isc_boolean_t
441 checkowner_ipseckey(ARGS_CHECKOWNER
) {
453 static inline isc_boolean_t
454 checknames_ipseckey(ARGS_CHECKNAMES
) {
456 REQUIRE(rdata
->type
== 45);
466 casecompare_ipseckey(ARGS_COMPARE
) {
467 isc_region_t region1
;
468 isc_region_t region2
;
473 REQUIRE(rdata1
->type
== rdata2
->type
);
474 REQUIRE(rdata1
->rdclass
== rdata2
->rdclass
);
475 REQUIRE(rdata1
->type
== 45);
476 REQUIRE(rdata1
->length
>= 3);
477 REQUIRE(rdata2
->length
>= 3);
479 dns_rdata_toregion(rdata1
, ®ion1
);
480 dns_rdata_toregion(rdata2
, ®ion2
);
482 if (memcmp(region1
.base
, region2
.base
, 3) != 0 || region1
.base
[1] != 3)
483 return (isc_region_compare(®ion1
, ®ion2
));
485 dns_name_init(&name1
, NULL
);
486 dns_name_init(&name2
, NULL
);
488 isc_region_consume(®ion1
, 3);
489 isc_region_consume(®ion2
, 3);
491 dns_name_fromregion(&name1
, ®ion1
);
492 dns_name_fromregion(&name2
, ®ion2
);
494 order
= dns_name_rdatacompare(&name1
, &name2
);
498 isc_region_consume(®ion1
, name_length(&name1
));
499 isc_region_consume(®ion2
, name_length(&name2
));
501 return (isc_region_compare(®ion1
, ®ion2
));
504 #endif /* RDATA_GENERIC_IPSECKEY_45_C */