Sync usage with man page.
[netbsd-mini2440.git] / external / bsd / bind / dist / lib / dns / rdata / generic / ipseckey_45.c
blob511bebe8d63966bf8558c6ff65b97b36a8ca043f
1 /* $NetBSD$ */
3 /*
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
24 #include <string.h>
26 #include <isc/net.h>
28 #define RRTYPE_IPSECKEY_ATTRIBUTES (0)
30 static inline isc_result_t
31 fromtext_ipseckey(ARGS_FROMTEXT) {
32 isc_token_t token;
33 dns_name_t name;
34 isc_buffer_t buffer;
35 unsigned int gateway;
36 struct in_addr addr;
37 unsigned char addr6[16];
38 isc_region_t region;
40 REQUIRE(type == 45);
42 UNUSED(type);
43 UNUSED(rdclass);
44 UNUSED(callbacks);
47 * Precedence.
49 RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_number,
50 ISC_FALSE));
51 if (token.value.as_ulong > 0xffU)
52 RETTOK(ISC_R_RANGE);
53 RETERR(uint8_tobuffer(token.value.as_ulong, target));
56 * Gateway type.
58 RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_number,
59 ISC_FALSE));
60 if (token.value.as_ulong > 0x3U)
61 RETTOK(ISC_R_RANGE);
62 RETERR(uint8_tobuffer(token.value.as_ulong, target));
63 gateway = token.value.as_ulong;
66 * Algorithm.
68 RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_number,
69 ISC_FALSE));
70 if (token.value.as_ulong > 0xffU)
71 RETTOK(ISC_R_RANGE);
72 RETERR(uint8_tobuffer(token.value.as_ulong, target));
75 * Gateway.
77 RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
78 ISC_FALSE));
80 switch (gateway) {
81 case 0:
82 if (strcmp(DNS_AS_STR(token), ".") != 0)
83 RETTOK(DNS_R_SYNTAX);
84 break;
86 case 1:
87 if (getquad(DNS_AS_STR(token), &addr, lexer, callbacks) != 1)
88 RETTOK(DNS_R_BADDOTTEDQUAD);
89 isc_buffer_availableregion(target, &region);
90 if (region.length < 4)
91 return (ISC_R_NOSPACE);
92 memcpy(region.base, &addr, 4);
93 isc_buffer_add(target, 4);
94 break;
96 case 2:
97 if (inet_pton(AF_INET6, DNS_AS_STR(token), addr6) != 1)
98 RETTOK(DNS_R_BADAAAA);
99 isc_buffer_availableregion(target, &region);
100 if (region.length < 16)
101 return (ISC_R_NOSPACE);
102 memcpy(region.base, addr6, 16);
103 isc_buffer_add(target, 16);
104 break;
106 case 3:
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,
111 options, target));
112 break;
116 * Public key.
118 return (isc_base64_tobuffer(lexer, target, -1));
121 static inline isc_result_t
122 totext_ipseckey(ARGS_TOTEXT) {
123 isc_region_t region;
124 dns_name_t name;
125 dns_name_t prefix;
126 isc_boolean_t sub;
127 char buf[sizeof("255 ")];
128 unsigned short num;
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));
144 * Precedence.
146 dns_rdata_toregion(rdata, &region);
147 num = uint8_fromregion(&region);
148 isc_region_consume(&region, 1);
149 sprintf(buf, "%u ", num);
150 RETERR(str_totext(buf, target));
153 * Gateway type.
155 gateway = uint8_fromregion(&region);
156 isc_region_consume(&region, 1);
157 sprintf(buf, "%u ", gateway);
158 RETERR(str_totext(buf, target));
161 * Algorithm.
163 num = uint8_fromregion(&region);
164 isc_region_consume(&region, 1);
165 sprintf(buf, "%u ", num);
166 RETERR(str_totext(buf, target));
169 * Gateway.
171 switch (gateway) {
172 case 0:
173 RETERR(str_totext(".", target));
174 break;
176 case 1:
177 RETERR(inet_totext(AF_INET, &region, target));
178 isc_region_consume(&region, 4);
179 break;
181 case 2:
182 RETERR(inet_totext(AF_INET6, &region, target));
183 isc_region_consume(&region, 16);
184 break;
186 case 3:
187 dns_name_fromregion(&name, &region);
188 sub = name_prefix(&name, tctx->origin, &prefix);
189 RETERR(dns_name_totext(&prefix, sub, target));
190 isc_region_consume(&region, name_length(&name));
191 break;
195 * Key.
197 if (region.length > 0U) {
198 RETERR(str_totext(tctx->linebreak, target));
199 RETERR(isc_base64_totext(&region, 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) {
210 dns_name_t name;
211 isc_region_t region;
213 REQUIRE(type == 45);
215 UNUSED(type);
216 UNUSED(rdclass);
218 dns_decompress_setmethods(dctx, DNS_COMPRESS_NONE);
220 dns_name_init(&name, NULL);
222 isc_buffer_activeregion(source, &region);
223 if (region.length < 3)
224 return (ISC_R_UNEXPECTEDEND);
226 switch (region.base[1]) {
227 case 0:
228 isc_buffer_forward(source, region.length);
229 return (mem_tobuffer(target, region.base, region.length));
231 case 1:
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));
237 case 2:
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));
243 case 3:
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, &region);
248 isc_buffer_forward(source, region.length);
249 return(mem_tobuffer(target, region.base, region.length));
251 default:
252 return (ISC_R_NOTIMPLEMENTED);
256 static inline isc_result_t
257 towire_ipseckey(ARGS_TOWIRE) {
258 isc_region_t region;
260 REQUIRE(rdata->type == 45);
261 REQUIRE(rdata->length != 0);
263 UNUSED(cctx);
265 dns_rdata_toregion(rdata, &region);
266 return (mem_tobuffer(target, region.base, region.length));
269 static inline int
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, &region1);
281 dns_rdata_toregion(rdata2, &region2);
283 return (isc_region_compare(&region1, &region2));
286 static inline isc_result_t
287 fromstruct_ipseckey(ARGS_FROMSTRUCT) {
288 dns_rdata_ipseckey_t *ipseckey = source;
289 isc_region_t region;
290 isc_uint32_t n;
292 REQUIRE(type == 45);
293 REQUIRE(source != NULL);
294 REQUIRE(ipseckey->common.rdtype == type);
295 REQUIRE(ipseckey->common.rdclass == rdclass);
297 UNUSED(type);
298 UNUSED(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) {
308 case 0:
309 break;
311 case 1:
312 n = ntohl(ipseckey->in_addr.s_addr);
313 RETERR(uint32_tobuffer(n, target));
314 break;
316 case 2:
317 RETERR(mem_tobuffer(target, ipseckey->in6_addr.s6_addr, 16));
318 break;
320 case 3:
321 dns_name_toregion(&ipseckey->gateway, &region);
322 RETERR(isc_buffer_copyregion(target, &region));
323 break;
326 return (mem_tobuffer(target, ipseckey->key, ipseckey->keylength));
329 static inline isc_result_t
330 tostruct_ipseckey(ARGS_TOSTRUCT) {
331 isc_region_t region;
332 dns_rdata_ipseckey_t *ipseckey = target;
333 dns_name_t name;
334 isc_uint32_t n;
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, &region);
350 ipseckey->precedence = uint8_fromregion(&region);
351 isc_region_consume(&region, 1);
353 ipseckey->gateway_type = uint8_fromregion(&region);
354 isc_region_consume(&region, 1);
356 ipseckey->algorithm = uint8_fromregion(&region);
357 isc_region_consume(&region, 1);
359 switch (ipseckey->gateway_type) {
360 case 0:
361 break;
363 case 1:
364 n = uint32_fromregion(&region);
365 ipseckey->in_addr.s_addr = htonl(n);
366 isc_region_consume(&region, 4);
367 break;
369 case 2:
370 memcpy(ipseckey->in6_addr.s6_addr, region.base, 16);
371 isc_region_consume(&region, 16);
372 break;
374 case 3:
375 dns_name_init(&ipseckey->gateway, NULL);
376 dns_name_fromregion(&name, &region);
377 RETERR(name_duporclone(&name, mctx, &ipseckey->gateway));
378 isc_region_consume(&region, name_length(&name));
379 break;
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,
389 ipseckey->mctx);
390 return (ISC_R_NOMEMORY);
392 } else
393 ipseckey->key = NULL;
395 ipseckey->mctx = mctx;
396 return (ISC_R_SUCCESS);
399 static inline void
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)
407 return;
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);
423 UNUSED(rdata);
424 UNUSED(add);
425 UNUSED(arg);
427 return (ISC_R_SUCCESS);
430 static inline isc_result_t
431 digest_ipseckey(ARGS_DIGEST) {
432 isc_region_t region;
434 REQUIRE(rdata->type == 45);
436 dns_rdata_toregion(rdata, &region);
437 return ((digest)(arg, &region));
440 static inline isc_boolean_t
441 checkowner_ipseckey(ARGS_CHECKOWNER) {
443 REQUIRE(type == 45);
445 UNUSED(name);
446 UNUSED(type);
447 UNUSED(rdclass);
448 UNUSED(wildcard);
450 return (ISC_TRUE);
453 static inline isc_boolean_t
454 checknames_ipseckey(ARGS_CHECKNAMES) {
456 REQUIRE(rdata->type == 45);
458 UNUSED(rdata);
459 UNUSED(owner);
460 UNUSED(bad);
462 return (ISC_TRUE);
465 static inline int
466 casecompare_ipseckey(ARGS_COMPARE) {
467 isc_region_t region1;
468 isc_region_t region2;
469 dns_name_t name1;
470 dns_name_t name2;
471 int order;
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, &region1);
480 dns_rdata_toregion(rdata2, &region2);
482 if (memcmp(region1.base, region2.base, 3) != 0 || region1.base[1] != 3)
483 return (isc_region_compare(&region1, &region2));
485 dns_name_init(&name1, NULL);
486 dns_name_init(&name2, NULL);
488 isc_region_consume(&region1, 3);
489 isc_region_consume(&region2, 3);
491 dns_name_fromregion(&name1, &region1);
492 dns_name_fromregion(&name2, &region2);
494 order = dns_name_rdatacompare(&name1, &name2);
495 if (order != 0)
496 return (order);
498 isc_region_consume(&region1, name_length(&name1));
499 isc_region_consume(&region2, name_length(&name2));
501 return (isc_region_compare(&region1, &region2));
504 #endif /* RDATA_GENERIC_IPSECKEY_45_C */