2 * Copyright (C) 2005 Internet Systems Consortium, Inc. ("ISC")
4 * Permission to use, copy, modify, and 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.2.2.1 2005/07/07 03:17:36 marka 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 return(mem_tobuffer(target, region.base, region.length));
249 return (ISC_R_NOTIMPLEMENTED);
253 static inline isc_result_t
254 towire_ipseckey(ARGS_TOWIRE) {
257 REQUIRE(rdata->type == 45);
258 REQUIRE(rdata->length != 0);
262 dns_rdata_toregion(rdata, ®ion);
263 return (mem_tobuffer(target, region.base, region.length));
267 compare_ipseckey(ARGS_COMPARE) {
268 isc_region_t region1;
269 isc_region_t region2;
271 REQUIRE(rdata1->type == rdata2->type);
272 REQUIRE(rdata1->rdclass == rdata2->rdclass);
273 REQUIRE(rdata1->type == 45);
274 REQUIRE(rdata1->length >= 3);
275 REQUIRE(rdata2->length >= 3);
277 dns_rdata_toregion(rdata1, ®ion1);
278 dns_rdata_toregion(rdata2, ®ion2);
280 return (isc_region_compare(®ion1, ®ion2));
283 static inline isc_result_t
284 fromstruct_ipseckey(ARGS_FROMSTRUCT) {
285 dns_rdata_ipseckey_t *ipseckey = source;
290 REQUIRE(source != NULL);
291 REQUIRE(ipseckey->common.rdtype == type);
292 REQUIRE(ipseckey->common.rdclass == rdclass);
297 if (ipseckey->gateway_type > 3U)
298 return (ISC_R_NOTIMPLEMENTED);
300 RETERR(uint8_tobuffer(ipseckey->precedence, target));
301 RETERR(uint8_tobuffer(ipseckey->gateway_type, target));
302 RETERR(uint8_tobuffer(ipseckey->algorithm, target));
304 switch (ipseckey->gateway_type) {
309 n = ntohl(ipseckey->in_addr.s_addr);
310 RETERR(uint32_tobuffer(n, target));
314 RETERR(mem_tobuffer(target, ipseckey->in6_addr.s6_addr, 16));
318 dns_name_toregion(&ipseckey->gateway, ®ion);
319 RETERR(isc_buffer_copyregion(target, ®ion));
323 return (mem_tobuffer(target, ipseckey->key, ipseckey->keylength));
326 static inline isc_result_t
327 tostruct_ipseckey(ARGS_TOSTRUCT) {
329 dns_rdata_ipseckey_t *ipseckey = target;
333 REQUIRE(rdata->type == 45);
334 REQUIRE(target != NULL);
335 REQUIRE(rdata->length >= 3);
337 if (rdata->data[1] > 3U)
338 return (ISC_R_NOTIMPLEMENTED);
340 ipseckey->common.rdclass = rdata->rdclass;
341 ipseckey->common.rdtype = rdata->type;
342 ISC_LINK_INIT(&ipseckey->common, link);
344 dns_name_init(&name, NULL);
345 dns_rdata_toregion(rdata, ®ion);
347 ipseckey->precedence = uint8_fromregion(®ion);
348 isc_region_consume(®ion, 1);
350 ipseckey->gateway_type = uint8_fromregion(®ion);
351 isc_region_consume(®ion, 1);
353 ipseckey->algorithm = uint8_fromregion(®ion);
354 isc_region_consume(®ion, 1);
356 switch (ipseckey->gateway_type) {
361 n = uint32_fromregion(®ion);
362 ipseckey->in_addr.s_addr = htonl(n);
363 isc_region_consume(®ion, 4);
367 memcpy(ipseckey->in6_addr.s6_addr, region.base, 16);
368 isc_region_consume(®ion, 16);
372 dns_name_init(&ipseckey->gateway, NULL);
373 dns_name_fromregion(&name, ®ion);
374 RETERR(name_duporclone(&name, mctx, &ipseckey->gateway));
375 isc_region_consume(®ion, name_length(&name));
379 ipseckey->keylength = region.length;
380 if (ipseckey->keylength != 0U) {
381 ipseckey->key = mem_maybedup(mctx, region.base,
382 ipseckey->keylength);
383 if (ipseckey->key == NULL) {
384 if (ipseckey->gateway_type == 3)
385 dns_name_free(&ipseckey->gateway,
387 return (ISC_R_NOMEMORY);
390 ipseckey->key = NULL;
392 ipseckey->mctx = mctx;
393 return (ISC_R_SUCCESS);
397 freestruct_ipseckey(ARGS_FREESTRUCT) {
398 dns_rdata_ipseckey_t *ipseckey = source;
400 REQUIRE(source != NULL);
401 REQUIRE(ipseckey->common.rdtype == 45);
403 if (ipseckey->mctx == NULL)
406 if (ipseckey->gateway_type == 3)
407 dns_name_free(&ipseckey->gateway, ipseckey->mctx);
409 if (ipseckey->key != NULL)
410 isc_mem_free(ipseckey->mctx, ipseckey->key);
412 ipseckey->mctx = NULL;
415 static inline isc_result_t
416 additionaldata_ipseckey(ARGS_ADDLDATA) {
418 REQUIRE(rdata->type == 45);
424 return (ISC_R_SUCCESS);
427 static inline isc_result_t
428 digest_ipseckey(ARGS_DIGEST) {
431 REQUIRE(rdata->type == 45);
433 dns_rdata_toregion(rdata, ®ion);
434 return ((digest)(arg, ®ion));
437 static inline isc_boolean_t
438 checkowner_ipseckey(ARGS_CHECKOWNER) {
450 static inline isc_boolean_t
451 checknames_ipseckey(ARGS_CHECKNAMES) {
453 REQUIRE(rdata->type == 45);
462 #endif /* RDATA_GENERIC_IPSECKEY_45_C */