2 * Copyright (C) 2005, 2007, 2009, 2011 Internet Systems Consortium, Inc. ("ISC")
4 * Permission to use, copy, modify, and/or distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
9 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
10 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
11 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
12 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
13 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
14 * PERFORMANCE OF THIS SOFTWARE.
17 /* $Id: ipseckey_45.c,v 1.11 2011-01-13 04:59:26 tbox Exp $ */
19 #ifndef RDATA_GENERIC_IPSECKEY_45_C
20 #define RDATA_GENERIC_IPSECKEY_45_C
26 #define RRTYPE_IPSECKEY_ATTRIBUTES (0)
28 static inline isc_result_t
29 fromtext_ipseckey(ARGS_FROMTEXT) {
35 unsigned char addr6[16];
47 RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_number,
49 if (token.value.as_ulong > 0xffU)
51 RETERR(uint8_tobuffer(token.value.as_ulong, target));
56 RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_number,
58 if (token.value.as_ulong > 0x3U)
60 RETERR(uint8_tobuffer(token.value.as_ulong, target));
61 gateway = token.value.as_ulong;
66 RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_number,
68 if (token.value.as_ulong > 0xffU)
70 RETERR(uint8_tobuffer(token.value.as_ulong, target));
75 RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
80 if (strcmp(DNS_AS_STR(token), ".") != 0)
85 if (getquad(DNS_AS_STR(token), &addr, lexer, callbacks) != 1)
86 RETTOK(DNS_R_BADDOTTEDQUAD);
87 isc_buffer_availableregion(target, ®ion);
88 if (region.length < 4)
89 return (ISC_R_NOSPACE);
90 memcpy(region.base, &addr, 4);
91 isc_buffer_add(target, 4);
95 if (inet_pton(AF_INET6, DNS_AS_STR(token), addr6) != 1)
96 RETTOK(DNS_R_BADAAAA);
97 isc_buffer_availableregion(target, ®ion);
98 if (region.length < 16)
99 return (ISC_R_NOSPACE);
100 memcpy(region.base, addr6, 16);
101 isc_buffer_add(target, 16);
105 dns_name_init(&name, NULL);
106 buffer_fromregion(&buffer, &token.value.as_region);
107 origin = (origin != NULL) ? origin : dns_rootname;
108 RETTOK(dns_name_fromtext(&name, &buffer, origin,
116 return (isc_base64_tobuffer(lexer, target, -1));
119 static inline isc_result_t
120 totext_ipseckey(ARGS_TOTEXT) {
123 char buf[sizeof("255 ")];
125 unsigned short gateway;
127 REQUIRE(rdata->type == 45);
128 REQUIRE(rdata->length >= 3);
130 dns_name_init(&name, NULL);
132 if (rdata->data[1] > 3U)
133 return (ISC_R_NOTIMPLEMENTED);
135 if ((tctx->flags & DNS_STYLEFLAG_MULTILINE) != 0)
136 RETERR(str_totext("( ", target));
141 dns_rdata_toregion(rdata, ®ion);
142 num = uint8_fromregion(®ion);
143 isc_region_consume(®ion, 1);
144 sprintf(buf, "%u ", num);
145 RETERR(str_totext(buf, target));
150 gateway = uint8_fromregion(®ion);
151 isc_region_consume(®ion, 1);
152 sprintf(buf, "%u ", gateway);
153 RETERR(str_totext(buf, target));
158 num = uint8_fromregion(®ion);
159 isc_region_consume(®ion, 1);
160 sprintf(buf, "%u ", num);
161 RETERR(str_totext(buf, target));
168 RETERR(str_totext(".", target));
172 RETERR(inet_totext(AF_INET, ®ion, target));
173 isc_region_consume(®ion, 4);
177 RETERR(inet_totext(AF_INET6, ®ion, target));
178 isc_region_consume(®ion, 16);
182 dns_name_fromregion(&name, ®ion);
183 RETERR(dns_name_totext(&name, ISC_FALSE, target));
184 isc_region_consume(®ion, name_length(&name));
191 if (region.length > 0U) {
192 RETERR(str_totext(tctx->linebreak, target));
193 RETERR(isc_base64_totext(®ion, tctx->width - 2,
194 tctx->linebreak, target));
197 if ((tctx->flags & DNS_STYLEFLAG_MULTILINE) != 0)
198 RETERR(str_totext(" )", target));
199 return (ISC_R_SUCCESS);
202 static inline isc_result_t
203 fromwire_ipseckey(ARGS_FROMWIRE) {
212 dns_decompress_setmethods(dctx, DNS_COMPRESS_NONE);
214 dns_name_init(&name, NULL);
216 isc_buffer_activeregion(source, ®ion);
217 if (region.length < 3)
218 return (ISC_R_UNEXPECTEDEND);
220 switch (region.base[1]) {
222 isc_buffer_forward(source, region.length);
223 return (mem_tobuffer(target, region.base, region.length));
226 if (region.length < 7)
227 return (ISC_R_UNEXPECTEDEND);
228 isc_buffer_forward(source, region.length);
229 return (mem_tobuffer(target, region.base, region.length));
232 if (region.length < 19)
233 return (ISC_R_UNEXPECTEDEND);
234 isc_buffer_forward(source, region.length);
235 return (mem_tobuffer(target, region.base, region.length));
238 RETERR(mem_tobuffer(target, region.base, 3));
239 isc_buffer_forward(source, 3);
240 RETERR(dns_name_fromwire(&name, source, dctx, options, target));
241 isc_buffer_activeregion(source, ®ion);
242 isc_buffer_forward(source, region.length);
243 return(mem_tobuffer(target, region.base, region.length));
246 return (ISC_R_NOTIMPLEMENTED);
250 static inline isc_result_t
251 towire_ipseckey(ARGS_TOWIRE) {
254 REQUIRE(rdata->type == 45);
255 REQUIRE(rdata->length != 0);
259 dns_rdata_toregion(rdata, ®ion);
260 return (mem_tobuffer(target, region.base, region.length));
264 compare_ipseckey(ARGS_COMPARE) {
265 isc_region_t region1;
266 isc_region_t region2;
268 REQUIRE(rdata1->type == rdata2->type);
269 REQUIRE(rdata1->rdclass == rdata2->rdclass);
270 REQUIRE(rdata1->type == 45);
271 REQUIRE(rdata1->length >= 3);
272 REQUIRE(rdata2->length >= 3);
274 dns_rdata_toregion(rdata1, ®ion1);
275 dns_rdata_toregion(rdata2, ®ion2);
277 return (isc_region_compare(®ion1, ®ion2));
280 static inline isc_result_t
281 fromstruct_ipseckey(ARGS_FROMSTRUCT) {
282 dns_rdata_ipseckey_t *ipseckey = source;
287 REQUIRE(source != NULL);
288 REQUIRE(ipseckey->common.rdtype == type);
289 REQUIRE(ipseckey->common.rdclass == rdclass);
294 if (ipseckey->gateway_type > 3U)
295 return (ISC_R_NOTIMPLEMENTED);
297 RETERR(uint8_tobuffer(ipseckey->precedence, target));
298 RETERR(uint8_tobuffer(ipseckey->gateway_type, target));
299 RETERR(uint8_tobuffer(ipseckey->algorithm, target));
301 switch (ipseckey->gateway_type) {
306 n = ntohl(ipseckey->in_addr.s_addr);
307 RETERR(uint32_tobuffer(n, target));
311 RETERR(mem_tobuffer(target, ipseckey->in6_addr.s6_addr, 16));
315 dns_name_toregion(&ipseckey->gateway, ®ion);
316 RETERR(isc_buffer_copyregion(target, ®ion));
320 return (mem_tobuffer(target, ipseckey->key, ipseckey->keylength));
323 static inline isc_result_t
324 tostruct_ipseckey(ARGS_TOSTRUCT) {
326 dns_rdata_ipseckey_t *ipseckey = target;
330 REQUIRE(rdata->type == 45);
331 REQUIRE(target != NULL);
332 REQUIRE(rdata->length >= 3);
334 if (rdata->data[1] > 3U)
335 return (ISC_R_NOTIMPLEMENTED);
337 ipseckey->common.rdclass = rdata->rdclass;
338 ipseckey->common.rdtype = rdata->type;
339 ISC_LINK_INIT(&ipseckey->common, link);
341 dns_name_init(&name, NULL);
342 dns_rdata_toregion(rdata, ®ion);
344 ipseckey->precedence = uint8_fromregion(®ion);
345 isc_region_consume(®ion, 1);
347 ipseckey->gateway_type = uint8_fromregion(®ion);
348 isc_region_consume(®ion, 1);
350 ipseckey->algorithm = uint8_fromregion(®ion);
351 isc_region_consume(®ion, 1);
353 switch (ipseckey->gateway_type) {
358 n = uint32_fromregion(®ion);
359 ipseckey->in_addr.s_addr = htonl(n);
360 isc_region_consume(®ion, 4);
364 memcpy(ipseckey->in6_addr.s6_addr, region.base, 16);
365 isc_region_consume(®ion, 16);
369 dns_name_init(&ipseckey->gateway, NULL);
370 dns_name_fromregion(&name, ®ion);
371 RETERR(name_duporclone(&name, mctx, &ipseckey->gateway));
372 isc_region_consume(®ion, name_length(&name));
376 ipseckey->keylength = region.length;
377 if (ipseckey->keylength != 0U) {
378 ipseckey->key = mem_maybedup(mctx, region.base,
379 ipseckey->keylength);
380 if (ipseckey->key == NULL) {
381 if (ipseckey->gateway_type == 3)
382 dns_name_free(&ipseckey->gateway,
384 return (ISC_R_NOMEMORY);
387 ipseckey->key = NULL;
389 ipseckey->mctx = mctx;
390 return (ISC_R_SUCCESS);
394 freestruct_ipseckey(ARGS_FREESTRUCT) {
395 dns_rdata_ipseckey_t *ipseckey = source;
397 REQUIRE(source != NULL);
398 REQUIRE(ipseckey->common.rdtype == 45);
400 if (ipseckey->mctx == NULL)
403 if (ipseckey->gateway_type == 3)
404 dns_name_free(&ipseckey->gateway, ipseckey->mctx);
406 if (ipseckey->key != NULL)
407 isc_mem_free(ipseckey->mctx, ipseckey->key);
409 ipseckey->mctx = NULL;
412 static inline isc_result_t
413 additionaldata_ipseckey(ARGS_ADDLDATA) {
415 REQUIRE(rdata->type == 45);
421 return (ISC_R_SUCCESS);
424 static inline isc_result_t
425 digest_ipseckey(ARGS_DIGEST) {
428 REQUIRE(rdata->type == 45);
430 dns_rdata_toregion(rdata, ®ion);
431 return ((digest)(arg, ®ion));
434 static inline isc_boolean_t
435 checkowner_ipseckey(ARGS_CHECKOWNER) {
447 static inline isc_boolean_t
448 checknames_ipseckey(ARGS_CHECKNAMES) {
450 REQUIRE(rdata->type == 45);
460 casecompare_ipseckey(ARGS_COMPARE) {
461 isc_region_t region1;
462 isc_region_t region2;
467 REQUIRE(rdata1->type == rdata2->type);
468 REQUIRE(rdata1->rdclass == rdata2->rdclass);
469 REQUIRE(rdata1->type == 45);
470 REQUIRE(rdata1->length >= 3);
471 REQUIRE(rdata2->length >= 3);
473 dns_rdata_toregion(rdata1, ®ion1);
474 dns_rdata_toregion(rdata2, ®ion2);
476 if (memcmp(region1.base, region2.base, 3) != 0 || region1.base[1] != 3)
477 return (isc_region_compare(®ion1, ®ion2));
479 dns_name_init(&name1, NULL);
480 dns_name_init(&name2, NULL);
482 isc_region_consume(®ion1, 3);
483 isc_region_consume(®ion2, 3);
485 dns_name_fromregion(&name1, ®ion1);
486 dns_name_fromregion(&name2, ®ion2);
488 order = dns_name_rdatacompare(&name1, &name2);
492 isc_region_consume(®ion1, name_length(&name1));
493 isc_region_consume(®ion2, name_length(&name2));
495 return (isc_region_compare(®ion1, ®ion2));
498 #endif /* RDATA_GENERIC_IPSECKEY_45_C */