1 /* $NetBSD: a_1.c,v 1.5 2014/12/10 04:37:58 christos Exp $ */
4 * Copyright (C) 2005, 2007, 2009, 2014 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: a_1.c,v 1.8 2009/12/04 22:06:37 tbox Exp */
21 /* by Bjorn.Victor@it.uu.se, 2005-05-07 */
22 /* Based on generic/soa_6.c and generic/mx_15.c */
24 #ifndef RDATA_CH_3_A_1_C
25 #define RDATA_CH_3_A_1_C
29 #define RRTYPE_A_ATTRIBUTES (0)
31 static inline isc_result_t
32 fromtext_ch_a(ARGS_FROMTEXT
) {
38 REQUIRE(rdclass
== dns_rdataclass_ch
); /* 3 */
43 RETERR(isc_lex_getmastertoken(lexer
, &token
, isc_tokentype_string
,
47 dns_name_init(&name
, NULL
);
48 buffer_fromregion(&buffer
, &token
.value
.as_region
);
49 origin
= (origin
!= NULL
) ? origin
: dns_rootname
;
50 RETTOK(dns_name_fromtext(&name
, &buffer
, origin
, options
, target
));
51 if ((options
& DNS_RDATA_CHECKNAMES
) != 0 &&
52 (options
& DNS_RDATA_CHECKREVERSE
) != 0) {
54 ok
= dns_name_ishostname(&name
, ISC_FALSE
);
55 if (!ok
&& (options
& DNS_RDATA_CHECKNAMESFAIL
) != 0)
56 RETTOK(DNS_R_BADNAME
);
57 if (!ok
&& callbacks
!= NULL
)
58 warn_badname(&name
, lexer
, callbacks
);
61 /* 16-bit octal address */
62 RETERR(isc_lex_getoctaltoken(lexer
, &token
, ISC_FALSE
));
63 if (token
.value
.as_ulong
> 0xffffU
)
65 return (uint16_tobuffer(token
.value
.as_ulong
, target
));
68 static inline isc_result_t
69 totext_ch_a(ARGS_TOTEXT
) {
74 char buf
[sizeof("0177777")];
77 REQUIRE(rdata
->type
== 1);
78 REQUIRE(rdata
->rdclass
== dns_rdataclass_ch
); /* 3 */
79 REQUIRE(rdata
->length
!= 0);
81 dns_name_init(&name
, NULL
);
82 dns_name_init(&prefix
, NULL
);
84 dns_rdata_toregion(rdata
, ®ion
);
85 dns_name_fromregion(&name
, ®ion
);
86 isc_region_consume(®ion
, name_length(&name
));
87 addr
= uint16_fromregion(®ion
);
89 sub
= name_prefix(&name
, tctx
->origin
, &prefix
);
90 RETERR(dns_name_totext(&prefix
, sub
, target
));
92 sprintf(buf
, "%o", addr
); /* note octal */
93 RETERR(str_totext(" ", target
));
94 return (str_totext(buf
, target
));
97 static inline isc_result_t
98 fromwire_ch_a(ARGS_FROMWIRE
) {
100 isc_region_t tregion
;
104 REQUIRE(rdclass
== dns_rdataclass_ch
);
109 dns_decompress_setmethods(dctx
, DNS_COMPRESS_GLOBAL14
);
111 dns_name_init(&name
, NULL
);
113 RETERR(dns_name_fromwire(&name
, source
, dctx
, options
, target
));
115 isc_buffer_activeregion(source
, &sregion
);
116 isc_buffer_availableregion(target
, &tregion
);
117 if (sregion
.length
< 2)
118 return (ISC_R_UNEXPECTEDEND
);
119 if (tregion
.length
< 2)
120 return (ISC_R_NOSPACE
);
122 memmove(tregion
.base
, sregion
.base
, 2);
123 isc_buffer_forward(source
, 2);
124 isc_buffer_add(target
, 2);
126 return (ISC_R_SUCCESS
);
129 static inline isc_result_t
130 towire_ch_a(ARGS_TOWIRE
) {
132 dns_offsets_t offsets
;
133 isc_region_t sregion
;
134 isc_region_t tregion
;
136 REQUIRE(rdata
->type
== 1);
137 REQUIRE(rdata
->rdclass
== dns_rdataclass_ch
);
138 REQUIRE(rdata
->length
!= 0);
140 dns_compress_setmethods(cctx
, DNS_COMPRESS_GLOBAL14
);
142 dns_name_init(&name
, offsets
);
144 dns_rdata_toregion(rdata
, &sregion
);
146 dns_name_fromregion(&name
, &sregion
);
147 isc_region_consume(&sregion
, name_length(&name
));
148 RETERR(dns_name_towire(&name
, cctx
, target
));
150 isc_buffer_availableregion(target
, &tregion
);
151 if (tregion
.length
< 2)
152 return (ISC_R_NOSPACE
);
154 memmove(tregion
.base
, sregion
.base
, 2);
155 isc_buffer_add(target
, 2);
156 return (ISC_R_SUCCESS
);
160 compare_ch_a(ARGS_COMPARE
) {
163 isc_region_t region1
;
164 isc_region_t region2
;
167 REQUIRE(rdata1
->type
== rdata2
->type
);
168 REQUIRE(rdata1
->rdclass
== rdata2
->rdclass
);
169 REQUIRE(rdata1
->type
== 1);
170 REQUIRE(rdata1
->rdclass
== dns_rdataclass_ch
);
171 REQUIRE(rdata1
->length
!= 0);
172 REQUIRE(rdata2
->length
!= 0);
174 dns_name_init(&name1
, NULL
);
175 dns_name_init(&name2
, NULL
);
177 dns_rdata_toregion(rdata1
, ®ion1
);
178 dns_rdata_toregion(rdata2
, ®ion2
);
180 dns_name_fromregion(&name1
, ®ion1
);
181 dns_name_fromregion(&name2
, ®ion2
);
182 isc_region_consume(®ion1
, name_length(&name1
));
183 isc_region_consume(®ion2
, name_length(&name2
));
185 order
= dns_name_rdatacompare(&name1
, &name2
);
189 order
= memcmp(rdata1
->data
, rdata2
->data
, 2);
191 order
= (order
< 0) ? -1 : 1;
195 static inline isc_result_t
196 fromstruct_ch_a(ARGS_FROMSTRUCT
) {
197 dns_rdata_ch_a_t
*a
= source
;
201 REQUIRE(source
!= NULL
);
202 REQUIRE(a
->common
.rdtype
== type
);
203 REQUIRE(a
->common
.rdclass
== rdclass
);
208 dns_name_toregion(&a
->ch_addr_dom
, ®ion
);
209 RETERR(isc_buffer_copyregion(target
, ®ion
));
211 return (uint16_tobuffer(ntohs(a
->ch_addr
), target
));
214 static inline isc_result_t
215 tostruct_ch_a(ARGS_TOSTRUCT
) {
216 dns_rdata_ch_a_t
*a
= target
;
220 REQUIRE(rdata
->type
== 1);
221 REQUIRE(rdata
->rdclass
== dns_rdataclass_ch
);
222 REQUIRE(rdata
->length
!= 0);
224 a
->common
.rdclass
= rdata
->rdclass
;
225 a
->common
.rdtype
= rdata
->type
;
226 ISC_LINK_INIT(&a
->common
, link
);
228 dns_rdata_toregion(rdata
, ®ion
);
230 dns_name_init(&name
, NULL
);
231 dns_name_fromregion(&name
, ®ion
);
232 isc_region_consume(®ion
, name_length(&name
));
234 dns_name_init(&a
->ch_addr_dom
, NULL
);
235 RETERR(name_duporclone(&name
, mctx
, &a
->ch_addr_dom
));
236 a
->ch_addr
= htons(uint16_fromregion(®ion
));
238 return (ISC_R_SUCCESS
);
242 freestruct_ch_a(ARGS_FREESTRUCT
) {
243 dns_rdata_ch_a_t
*a
= source
;
245 REQUIRE(source
!= NULL
);
246 REQUIRE(a
->common
.rdtype
== 1);
251 dns_name_free(&a
->ch_addr_dom
, a
->mctx
);
255 static inline isc_result_t
256 additionaldata_ch_a(ARGS_ADDLDATA
) {
258 REQUIRE(rdata
->type
== 1);
259 REQUIRE(rdata
->rdclass
== dns_rdataclass_ch
);
265 return (ISC_R_SUCCESS
);
268 static inline isc_result_t
269 digest_ch_a(ARGS_DIGEST
) {
274 REQUIRE(rdata
->type
== 1);
275 REQUIRE(rdata
->rdclass
== dns_rdataclass_ch
);
277 dns_rdata_toregion(rdata
, &r
);
278 dns_name_init(&name
, NULL
);
279 dns_name_fromregion(&name
, &r
);
280 isc_region_consume(&r
, name_length(&name
));
281 RETERR(dns_name_digest(&name
, digest
, arg
));
282 return ((digest
)(arg
, &r
));
285 static inline isc_boolean_t
286 checkowner_ch_a(ARGS_CHECKOWNER
) {
289 REQUIRE(rdclass
== dns_rdataclass_ch
);
293 return (dns_name_ishostname(name
, wildcard
));
296 static inline isc_boolean_t
297 checknames_ch_a(ARGS_CHECKNAMES
) {
301 REQUIRE(rdata
->type
== 1);
302 REQUIRE(rdata
->rdclass
== dns_rdataclass_ch
);
306 dns_rdata_toregion(rdata
, ®ion
);
307 dns_name_init(&name
, NULL
);
308 dns_name_fromregion(&name
, ®ion
);
309 if (!dns_name_ishostname(&name
, ISC_FALSE
)) {
311 dns_name_clone(&name
, bad
);
319 casecompare_ch_a(ARGS_COMPARE
) {
320 return (compare_ch_a(rdata1
, rdata2
));
322 #endif /* RDATA_CH_3_A_1_C */