1 /* $NetBSD: mx_15.c,v 1.5 2014/12/10 04:37:59 christos Exp $ */
4 * Copyright (C) 2004, 2005, 2007, 2009, 2012 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
) {
63 RETERR(isc_lex_getmastertoken(lexer
, &token
, isc_tokentype_number
,
65 if (token
.value
.as_ulong
> 0xffffU
)
67 RETERR(uint16_tobuffer(token
.value
.as_ulong
, target
));
69 RETERR(isc_lex_getmastertoken(lexer
, &token
, isc_tokentype_string
,
73 if ((options
& DNS_RDATA_CHECKMX
) != 0)
74 ok
= check_mx(&token
);
75 if (!ok
&& (options
& DNS_RDATA_CHECKMXFAIL
) != 0)
76 RETTOK(DNS_R_MXISADDRESS
);
77 if (!ok
&& callbacks
!= NULL
)
78 warn_badmx(&token
, lexer
, callbacks
);
80 dns_name_init(&name
, NULL
);
81 buffer_fromregion(&buffer
, &token
.value
.as_region
);
82 origin
= (origin
!= NULL
) ? origin
: dns_rootname
;
83 RETTOK(dns_name_fromtext(&name
, &buffer
, origin
, options
, target
));
85 if ((options
& DNS_RDATA_CHECKNAMES
) != 0)
86 ok
= dns_name_ishostname(&name
, ISC_FALSE
);
87 if (!ok
&& (options
& DNS_RDATA_CHECKNAMESFAIL
) != 0)
88 RETTOK(DNS_R_BADNAME
);
89 if (!ok
&& callbacks
!= NULL
)
90 warn_badname(&name
, lexer
, callbacks
);
91 return (ISC_R_SUCCESS
);
94 static inline isc_result_t
95 totext_mx(ARGS_TOTEXT
) {
100 char buf
[sizeof("64000")];
103 REQUIRE(rdata
->type
== 15);
104 REQUIRE(rdata
->length
!= 0);
106 dns_name_init(&name
, NULL
);
107 dns_name_init(&prefix
, NULL
);
109 dns_rdata_toregion(rdata
, ®ion
);
110 num
= uint16_fromregion(®ion
);
111 isc_region_consume(®ion
, 2);
112 sprintf(buf
, "%u", num
);
113 RETERR(str_totext(buf
, target
));
115 RETERR(str_totext(" ", target
));
117 dns_name_fromregion(&name
, ®ion
);
118 sub
= name_prefix(&name
, tctx
->origin
, &prefix
);
119 return (dns_name_totext(&prefix
, sub
, target
));
122 static inline isc_result_t
123 fromwire_mx(ARGS_FROMWIRE
) {
125 isc_region_t sregion
;
132 dns_decompress_setmethods(dctx
, DNS_COMPRESS_GLOBAL14
);
134 dns_name_init(&name
, NULL
);
136 isc_buffer_activeregion(source
, &sregion
);
137 if (sregion
.length
< 2)
138 return (ISC_R_UNEXPECTEDEND
);
139 RETERR(mem_tobuffer(target
, sregion
.base
, 2));
140 isc_buffer_forward(source
, 2);
141 return (dns_name_fromwire(&name
, source
, dctx
, options
, target
));
144 static inline isc_result_t
145 towire_mx(ARGS_TOWIRE
) {
147 dns_offsets_t offsets
;
150 REQUIRE(rdata
->type
== 15);
151 REQUIRE(rdata
->length
!= 0);
153 dns_compress_setmethods(cctx
, DNS_COMPRESS_GLOBAL14
);
155 dns_rdata_toregion(rdata
, ®ion
);
156 RETERR(mem_tobuffer(target
, region
.base
, 2));
157 isc_region_consume(®ion
, 2);
159 dns_name_init(&name
, offsets
);
160 dns_name_fromregion(&name
, ®ion
);
162 return (dns_name_towire(&name
, cctx
, target
));
166 compare_mx(ARGS_COMPARE
) {
169 isc_region_t region1
;
170 isc_region_t region2
;
173 REQUIRE(rdata1
->type
== rdata2
->type
);
174 REQUIRE(rdata1
->rdclass
== rdata2
->rdclass
);
175 REQUIRE(rdata1
->type
== 15);
176 REQUIRE(rdata1
->length
!= 0);
177 REQUIRE(rdata2
->length
!= 0);
179 order
= memcmp(rdata1
->data
, rdata2
->data
, 2);
181 return (order
< 0 ? -1 : 1);
183 dns_name_init(&name1
, NULL
);
184 dns_name_init(&name2
, NULL
);
186 dns_rdata_toregion(rdata1
, ®ion1
);
187 dns_rdata_toregion(rdata2
, ®ion2
);
189 isc_region_consume(®ion1
, 2);
190 isc_region_consume(®ion2
, 2);
192 dns_name_fromregion(&name1
, ®ion1
);
193 dns_name_fromregion(&name2
, ®ion2
);
195 return (dns_name_rdatacompare(&name1
, &name2
));
198 static inline isc_result_t
199 fromstruct_mx(ARGS_FROMSTRUCT
) {
200 dns_rdata_mx_t
*mx
= source
;
204 REQUIRE(source
!= NULL
);
205 REQUIRE(mx
->common
.rdtype
== type
);
206 REQUIRE(mx
->common
.rdclass
== rdclass
);
211 RETERR(uint16_tobuffer(mx
->pref
, target
));
212 dns_name_toregion(&mx
->mx
, ®ion
);
213 return (isc_buffer_copyregion(target
, ®ion
));
216 static inline isc_result_t
217 tostruct_mx(ARGS_TOSTRUCT
) {
219 dns_rdata_mx_t
*mx
= target
;
222 REQUIRE(rdata
->type
== 15);
223 REQUIRE(target
!= NULL
);
224 REQUIRE(rdata
->length
!= 0);
226 mx
->common
.rdclass
= rdata
->rdclass
;
227 mx
->common
.rdtype
= rdata
->type
;
228 ISC_LINK_INIT(&mx
->common
, link
);
230 dns_name_init(&name
, NULL
);
231 dns_rdata_toregion(rdata
, ®ion
);
232 mx
->pref
= uint16_fromregion(®ion
);
233 isc_region_consume(®ion
, 2);
234 dns_name_fromregion(&name
, ®ion
);
235 dns_name_init(&mx
->mx
, NULL
);
236 RETERR(name_duporclone(&name
, mctx
, &mx
->mx
));
238 return (ISC_R_SUCCESS
);
242 freestruct_mx(ARGS_FREESTRUCT
) {
243 dns_rdata_mx_t
*mx
= source
;
245 REQUIRE(source
!= NULL
);
246 REQUIRE(mx
->common
.rdtype
== 15);
248 if (mx
->mctx
== NULL
)
251 dns_name_free(&mx
->mx
, mx
->mctx
);
255 static inline isc_result_t
256 additionaldata_mx(ARGS_ADDLDATA
) {
258 dns_offsets_t offsets
;
261 REQUIRE(rdata
->type
== 15);
263 dns_name_init(&name
, offsets
);
264 dns_rdata_toregion(rdata
, ®ion
);
265 isc_region_consume(®ion
, 2);
266 dns_name_fromregion(&name
, ®ion
);
268 return ((add
)(arg
, &name
, dns_rdatatype_a
));
271 static inline isc_result_t
272 digest_mx(ARGS_DIGEST
) {
276 REQUIRE(rdata
->type
== 15);
278 dns_rdata_toregion(rdata
, &r1
);
280 isc_region_consume(&r2
, 2);
282 RETERR((digest
)(arg
, &r1
));
283 dns_name_init(&name
, NULL
);
284 dns_name_fromregion(&name
, &r2
);
285 return (dns_name_digest(&name
, digest
, arg
));
288 static inline isc_boolean_t
289 checkowner_mx(ARGS_CHECKOWNER
) {
296 return (dns_name_ishostname(name
, wildcard
));
299 static inline isc_boolean_t
300 checknames_mx(ARGS_CHECKNAMES
) {
304 REQUIRE(rdata
->type
== 15);
308 dns_rdata_toregion(rdata
, ®ion
);
309 isc_region_consume(®ion
, 2);
310 dns_name_init(&name
, NULL
);
311 dns_name_fromregion(&name
, ®ion
);
312 if (!dns_name_ishostname(&name
, ISC_FALSE
)) {
314 dns_name_clone(&name
, bad
);
321 casecompare_mx(ARGS_COMPARE
) {
322 return (compare_mx(rdata1
, rdata2
));
325 #endif /* RDATA_GENERIC_MX_15_C */