2 * Copyright (C) 2005, 2007, 2009 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.4.332.3 2009/09/18 21:55:48 jinmei 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) {
125 char buf[sizeof("255 ")];
127 unsigned short gateway;
129 REQUIRE(rdata->type == 45);
130 REQUIRE(rdata->length >= 3);
132 dns_name_init(&name, NULL);
133 dns_name_init(&prefix, NULL);
135 if (rdata->data[1] > 3U)
136 return (ISC_R_NOTIMPLEMENTED);
138 if ((tctx->flags & DNS_STYLEFLAG_MULTILINE) != 0)
139 RETERR(str_totext("( ", target));
144 dns_rdata_toregion(rdata, ®ion);
145 num = uint8_fromregion(®ion);
146 isc_region_consume(®ion, 1);
147 sprintf(buf, "%u ", num);
148 RETERR(str_totext(buf, target));
153 gateway = uint8_fromregion(®ion);
154 isc_region_consume(®ion, 1);
155 sprintf(buf, "%u ", gateway);
156 RETERR(str_totext(buf, target));
161 num = uint8_fromregion(®ion);
162 isc_region_consume(®ion, 1);
163 sprintf(buf, "%u ", num);
164 RETERR(str_totext(buf, target));
171 RETERR(str_totext(".", target));
175 RETERR(inet_totext(AF_INET, ®ion, target));
176 isc_region_consume(®ion, 4);
180 RETERR(inet_totext(AF_INET6, ®ion, target));
181 isc_region_consume(®ion, 16);
185 dns_name_fromregion(&name, ®ion);
186 sub = name_prefix(&name, tctx->origin, &prefix);
187 RETERR(dns_name_totext(&prefix, sub, target));
188 isc_region_consume(®ion, name_length(&name));
195 if (region.length > 0U) {
196 RETERR(str_totext(tctx->linebreak, target));
197 RETERR(isc_base64_totext(®ion, tctx->width - 2,
198 tctx->linebreak, target));
201 if ((tctx->flags & DNS_STYLEFLAG_MULTILINE) != 0)
202 RETERR(str_totext(" )", target));
203 return (ISC_R_SUCCESS);
206 static inline isc_result_t
207 fromwire_ipseckey(ARGS_FROMWIRE) {
216 dns_decompress_setmethods(dctx, DNS_COMPRESS_NONE);
218 dns_name_init(&name, NULL);
220 isc_buffer_activeregion(source, ®ion);
221 if (region.length < 3)
222 return (ISC_R_UNEXPECTEDEND);
224 switch (region.base[1]) {
226 isc_buffer_forward(source, region.length);
227 return (mem_tobuffer(target, region.base, region.length));
230 if (region.length < 7)
231 return (ISC_R_UNEXPECTEDEND);
232 isc_buffer_forward(source, region.length);
233 return (mem_tobuffer(target, region.base, region.length));
236 if (region.length < 19)
237 return (ISC_R_UNEXPECTEDEND);
238 isc_buffer_forward(source, region.length);
239 return (mem_tobuffer(target, region.base, region.length));
242 RETERR(mem_tobuffer(target, region.base, 3));
243 isc_buffer_forward(source, 3);
244 RETERR(dns_name_fromwire(&name, source, dctx, options, target));
245 isc_buffer_activeregion(source, ®ion);
246 isc_buffer_forward(source, region.length);
247 return(mem_tobuffer(target, region.base, region.length));
250 return (ISC_R_NOTIMPLEMENTED);
254 static inline isc_result_t
255 towire_ipseckey(ARGS_TOWIRE) {
258 REQUIRE(rdata->type == 45);
259 REQUIRE(rdata->length != 0);
263 dns_rdata_toregion(rdata, ®ion);
264 return (mem_tobuffer(target, region.base, region.length));
268 compare_ipseckey(ARGS_COMPARE) {
269 isc_region_t region1;
270 isc_region_t region2;
272 REQUIRE(rdata1->type == rdata2->type);
273 REQUIRE(rdata1->rdclass == rdata2->rdclass);
274 REQUIRE(rdata1->type == 45);
275 REQUIRE(rdata1->length >= 3);
276 REQUIRE(rdata2->length >= 3);
278 dns_rdata_toregion(rdata1, ®ion1);
279 dns_rdata_toregion(rdata2, ®ion2);
281 return (isc_region_compare(®ion1, ®ion2));
284 static inline isc_result_t
285 fromstruct_ipseckey(ARGS_FROMSTRUCT) {
286 dns_rdata_ipseckey_t *ipseckey = source;
291 REQUIRE(source != NULL);
292 REQUIRE(ipseckey->common.rdtype == type);
293 REQUIRE(ipseckey->common.rdclass == rdclass);
298 if (ipseckey->gateway_type > 3U)
299 return (ISC_R_NOTIMPLEMENTED);
301 RETERR(uint8_tobuffer(ipseckey->precedence, target));
302 RETERR(uint8_tobuffer(ipseckey->gateway_type, target));
303 RETERR(uint8_tobuffer(ipseckey->algorithm, target));
305 switch (ipseckey->gateway_type) {
310 n = ntohl(ipseckey->in_addr.s_addr);
311 RETERR(uint32_tobuffer(n, target));
315 RETERR(mem_tobuffer(target, ipseckey->in6_addr.s6_addr, 16));
319 dns_name_toregion(&ipseckey->gateway, ®ion);
320 RETERR(isc_buffer_copyregion(target, ®ion));
324 return (mem_tobuffer(target, ipseckey->key, ipseckey->keylength));
327 static inline isc_result_t
328 tostruct_ipseckey(ARGS_TOSTRUCT) {
330 dns_rdata_ipseckey_t *ipseckey = target;
334 REQUIRE(rdata->type == 45);
335 REQUIRE(target != NULL);
336 REQUIRE(rdata->length >= 3);
338 if (rdata->data[1] > 3U)
339 return (ISC_R_NOTIMPLEMENTED);
341 ipseckey->common.rdclass = rdata->rdclass;
342 ipseckey->common.rdtype = rdata->type;
343 ISC_LINK_INIT(&ipseckey->common, link);
345 dns_name_init(&name, NULL);
346 dns_rdata_toregion(rdata, ®ion);
348 ipseckey->precedence = uint8_fromregion(®ion);
349 isc_region_consume(®ion, 1);
351 ipseckey->gateway_type = uint8_fromregion(®ion);
352 isc_region_consume(®ion, 1);
354 ipseckey->algorithm = uint8_fromregion(®ion);
355 isc_region_consume(®ion, 1);
357 switch (ipseckey->gateway_type) {
362 n = uint32_fromregion(®ion);
363 ipseckey->in_addr.s_addr = htonl(n);
364 isc_region_consume(®ion, 4);
368 memcpy(ipseckey->in6_addr.s6_addr, region.base, 16);
369 isc_region_consume(®ion, 16);
373 dns_name_init(&ipseckey->gateway, NULL);
374 dns_name_fromregion(&name, ®ion);
375 RETERR(name_duporclone(&name, mctx, &ipseckey->gateway));
376 isc_region_consume(®ion, name_length(&name));
380 ipseckey->keylength = region.length;
381 if (ipseckey->keylength != 0U) {
382 ipseckey->key = mem_maybedup(mctx, region.base,
383 ipseckey->keylength);
384 if (ipseckey->key == NULL) {
385 if (ipseckey->gateway_type == 3)
386 dns_name_free(&ipseckey->gateway,
388 return (ISC_R_NOMEMORY);
391 ipseckey->key = NULL;
393 ipseckey->mctx = mctx;
394 return (ISC_R_SUCCESS);
398 freestruct_ipseckey(ARGS_FREESTRUCT) {
399 dns_rdata_ipseckey_t *ipseckey = source;
401 REQUIRE(source != NULL);
402 REQUIRE(ipseckey->common.rdtype == 45);
404 if (ipseckey->mctx == NULL)
407 if (ipseckey->gateway_type == 3)
408 dns_name_free(&ipseckey->gateway, ipseckey->mctx);
410 if (ipseckey->key != NULL)
411 isc_mem_free(ipseckey->mctx, ipseckey->key);
413 ipseckey->mctx = NULL;
416 static inline isc_result_t
417 additionaldata_ipseckey(ARGS_ADDLDATA) {
419 REQUIRE(rdata->type == 45);
425 return (ISC_R_SUCCESS);
428 static inline isc_result_t
429 digest_ipseckey(ARGS_DIGEST) {
432 REQUIRE(rdata->type == 45);
434 dns_rdata_toregion(rdata, ®ion);
435 return ((digest)(arg, ®ion));
438 static inline isc_boolean_t
439 checkowner_ipseckey(ARGS_CHECKOWNER) {
451 static inline isc_boolean_t
452 checknames_ipseckey(ARGS_CHECKNAMES) {
454 REQUIRE(rdata->type == 45);
463 #endif /* RDATA_GENERIC_IPSECKEY_45_C */