4 * Copyright (C) 2004, 2005, 2007, 2009 Internet Systems Consortium, Inc. ("ISC")
5 * Copyright (C) 1998-2001, 2003 Internet Software Consortium.
7 * Permission to use, copy, modify, and/or distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
11 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
12 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
13 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
14 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
15 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
16 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17 * PERFORMANCE OF THIS SOFTWARE.
20 /* Id: mx_15.c,v 1.58 2009/12/04 22:06:37 tbox Exp */
22 /* reviewed: Wed Mar 15 18:05:46 PST 2000 by brister */
24 #ifndef RDATA_GENERIC_MX_15_C
25 #define RDATA_GENERIC_MX_15_C
31 #define RRTYPE_MX_ATTRIBUTES (0)
34 check_mx(isc_token_t
*token
) {
35 char tmp
[sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:123.123.123.123.")];
37 struct in6_addr addr6
;
39 if (strlcpy(tmp
, DNS_AS_STR(*token
), sizeof(tmp
)) >= sizeof(tmp
))
42 if (tmp
[strlen(tmp
) - 1] == '.')
43 tmp
[strlen(tmp
) - 1] = '\0';
44 if (inet_aton(tmp
, &addr
) == 1 ||
45 inet_pton(AF_INET6
, tmp
, &addr6
) == 1)
51 static inline isc_result_t
52 fromtext_mx(ARGS_FROMTEXT
) {
64 RETERR(isc_lex_getmastertoken(lexer
, &token
, isc_tokentype_number
,
66 if (token
.value
.as_ulong
> 0xffffU
)
68 RETERR(uint16_tobuffer(token
.value
.as_ulong
, target
));
70 RETERR(isc_lex_getmastertoken(lexer
, &token
, isc_tokentype_string
,
74 if ((options
& DNS_RDATA_CHECKMX
) != 0)
75 ok
= check_mx(&token
);
76 if (!ok
&& (options
& DNS_RDATA_CHECKMXFAIL
) != 0)
77 RETTOK(DNS_R_MXISADDRESS
);
78 if (!ok
&& callbacks
!= NULL
)
79 warn_badmx(&token
, lexer
, callbacks
);
81 dns_name_init(&name
, NULL
);
82 buffer_fromregion(&buffer
, &token
.value
.as_region
);
83 origin
= (origin
!= NULL
) ? origin
: dns_rootname
;
84 RETTOK(dns_name_fromtext(&name
, &buffer
, origin
, options
, target
));
86 if ((options
& DNS_RDATA_CHECKNAMES
) != 0)
87 ok
= dns_name_ishostname(&name
, ISC_FALSE
);
88 if (!ok
&& (options
& DNS_RDATA_CHECKNAMESFAIL
) != 0)
89 RETTOK(DNS_R_BADNAME
);
90 if (!ok
&& callbacks
!= NULL
)
91 warn_badname(&name
, lexer
, callbacks
);
92 return (ISC_R_SUCCESS
);
95 static inline isc_result_t
96 totext_mx(ARGS_TOTEXT
) {
101 char buf
[sizeof("64000")];
104 REQUIRE(rdata
->type
== 15);
105 REQUIRE(rdata
->length
!= 0);
107 dns_name_init(&name
, NULL
);
108 dns_name_init(&prefix
, NULL
);
110 dns_rdata_toregion(rdata
, ®ion
);
111 num
= uint16_fromregion(®ion
);
112 isc_region_consume(®ion
, 2);
113 sprintf(buf
, "%u", num
);
114 RETERR(str_totext(buf
, target
));
116 RETERR(str_totext(" ", target
));
118 dns_name_fromregion(&name
, ®ion
);
119 sub
= name_prefix(&name
, tctx
->origin
, &prefix
);
120 return (dns_name_totext(&prefix
, sub
, target
));
123 static inline isc_result_t
124 fromwire_mx(ARGS_FROMWIRE
) {
126 isc_region_t sregion
;
133 dns_decompress_setmethods(dctx
, DNS_COMPRESS_GLOBAL14
);
135 dns_name_init(&name
, NULL
);
137 isc_buffer_activeregion(source
, &sregion
);
138 if (sregion
.length
< 2)
139 return (ISC_R_UNEXPECTEDEND
);
140 RETERR(mem_tobuffer(target
, sregion
.base
, 2));
141 isc_buffer_forward(source
, 2);
142 return (dns_name_fromwire(&name
, source
, dctx
, options
, target
));
145 static inline isc_result_t
146 towire_mx(ARGS_TOWIRE
) {
148 dns_offsets_t offsets
;
151 REQUIRE(rdata
->type
== 15);
152 REQUIRE(rdata
->length
!= 0);
154 dns_compress_setmethods(cctx
, DNS_COMPRESS_GLOBAL14
);
156 dns_rdata_toregion(rdata
, ®ion
);
157 RETERR(mem_tobuffer(target
, region
.base
, 2));
158 isc_region_consume(®ion
, 2);
160 dns_name_init(&name
, offsets
);
161 dns_name_fromregion(&name
, ®ion
);
163 return (dns_name_towire(&name
, cctx
, target
));
167 compare_mx(ARGS_COMPARE
) {
170 isc_region_t region1
;
171 isc_region_t region2
;
174 REQUIRE(rdata1
->type
== rdata2
->type
);
175 REQUIRE(rdata1
->rdclass
== rdata2
->rdclass
);
176 REQUIRE(rdata1
->type
== 15);
177 REQUIRE(rdata1
->length
!= 0);
178 REQUIRE(rdata2
->length
!= 0);
180 order
= memcmp(rdata1
->data
, rdata2
->data
, 2);
182 return (order
< 0 ? -1 : 1);
184 dns_name_init(&name1
, NULL
);
185 dns_name_init(&name2
, NULL
);
187 dns_rdata_toregion(rdata1
, ®ion1
);
188 dns_rdata_toregion(rdata2
, ®ion2
);
190 isc_region_consume(®ion1
, 2);
191 isc_region_consume(®ion2
, 2);
193 dns_name_fromregion(&name1
, ®ion1
);
194 dns_name_fromregion(&name2
, ®ion2
);
196 return (dns_name_rdatacompare(&name1
, &name2
));
199 static inline isc_result_t
200 fromstruct_mx(ARGS_FROMSTRUCT
) {
201 dns_rdata_mx_t
*mx
= source
;
205 REQUIRE(source
!= NULL
);
206 REQUIRE(mx
->common
.rdtype
== type
);
207 REQUIRE(mx
->common
.rdclass
== rdclass
);
212 RETERR(uint16_tobuffer(mx
->pref
, target
));
213 dns_name_toregion(&mx
->mx
, ®ion
);
214 return (isc_buffer_copyregion(target
, ®ion
));
217 static inline isc_result_t
218 tostruct_mx(ARGS_TOSTRUCT
) {
220 dns_rdata_mx_t
*mx
= target
;
223 REQUIRE(rdata
->type
== 15);
224 REQUIRE(target
!= NULL
);
225 REQUIRE(rdata
->length
!= 0);
227 mx
->common
.rdclass
= rdata
->rdclass
;
228 mx
->common
.rdtype
= rdata
->type
;
229 ISC_LINK_INIT(&mx
->common
, link
);
231 dns_name_init(&name
, NULL
);
232 dns_rdata_toregion(rdata
, ®ion
);
233 mx
->pref
= uint16_fromregion(®ion
);
234 isc_region_consume(®ion
, 2);
235 dns_name_fromregion(&name
, ®ion
);
236 dns_name_init(&mx
->mx
, NULL
);
237 RETERR(name_duporclone(&name
, mctx
, &mx
->mx
));
239 return (ISC_R_SUCCESS
);
243 freestruct_mx(ARGS_FREESTRUCT
) {
244 dns_rdata_mx_t
*mx
= source
;
246 REQUIRE(source
!= NULL
);
247 REQUIRE(mx
->common
.rdtype
== 15);
249 if (mx
->mctx
== NULL
)
252 dns_name_free(&mx
->mx
, mx
->mctx
);
256 static inline isc_result_t
257 additionaldata_mx(ARGS_ADDLDATA
) {
259 dns_offsets_t offsets
;
262 REQUIRE(rdata
->type
== 15);
264 dns_name_init(&name
, offsets
);
265 dns_rdata_toregion(rdata
, ®ion
);
266 isc_region_consume(®ion
, 2);
267 dns_name_fromregion(&name
, ®ion
);
269 return ((add
)(arg
, &name
, dns_rdatatype_a
));
272 static inline isc_result_t
273 digest_mx(ARGS_DIGEST
) {
277 REQUIRE(rdata
->type
== 15);
279 dns_rdata_toregion(rdata
, &r1
);
281 isc_region_consume(&r2
, 2);
283 RETERR((digest
)(arg
, &r1
));
284 dns_name_init(&name
, NULL
);
285 dns_name_fromregion(&name
, &r2
);
286 return (dns_name_digest(&name
, digest
, arg
));
289 static inline isc_boolean_t
290 checkowner_mx(ARGS_CHECKOWNER
) {
297 return (dns_name_ishostname(name
, wildcard
));
300 static inline isc_boolean_t
301 checknames_mx(ARGS_CHECKNAMES
) {
305 REQUIRE(rdata
->type
== 15);
309 dns_rdata_toregion(rdata
, ®ion
);
310 isc_region_consume(®ion
, 2);
311 dns_name_init(&name
, NULL
);
312 dns_name_fromregion(&name
, ®ion
);
313 if (!dns_name_ishostname(&name
, ISC_FALSE
)) {
315 dns_name_clone(&name
, bad
);
322 casecompare_mx(ARGS_COMPARE
) {
323 return (compare_mx(rdata1
, rdata2
));
326 #endif /* RDATA_GENERIC_MX_15_C */