Patrick Welche <prlw1@cam.ac.uk>
[netbsd-mini2440.git] / external / bsd / bind / dist / lib / dns / rdata / generic / mx_15.c
blobc680a9155ba19705b5da3af9700f0b2cedf64c1c
1 /* $NetBSD$ */
3 /*
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
27 #include <string.h>
29 #include <isc/net.h>
31 #define RRTYPE_MX_ATTRIBUTES (0)
33 static isc_boolean_t
34 check_mx(isc_token_t *token) {
35 char tmp[sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:123.123.123.123.")];
36 struct in_addr addr;
37 struct in6_addr addr6;
39 if (strlcpy(tmp, DNS_AS_STR(*token), sizeof(tmp)) >= sizeof(tmp))
40 return (ISC_TRUE);
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)
46 return (ISC_FALSE);
48 return (ISC_TRUE);
51 static inline isc_result_t
52 fromtext_mx(ARGS_FROMTEXT) {
53 isc_token_t token;
54 dns_name_t name;
55 isc_buffer_t buffer;
56 isc_boolean_t ok;
58 REQUIRE(type == 15);
60 UNUSED(type);
61 UNUSED(rdclass);
62 UNUSED(callbacks);
64 RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_number,
65 ISC_FALSE));
66 if (token.value.as_ulong > 0xffffU)
67 RETTOK(ISC_R_RANGE);
68 RETERR(uint16_tobuffer(token.value.as_ulong, target));
70 RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
71 ISC_FALSE));
73 ok = ISC_TRUE;
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));
85 ok = ISC_TRUE;
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) {
97 isc_region_t region;
98 dns_name_t name;
99 dns_name_t prefix;
100 isc_boolean_t sub;
101 char buf[sizeof("64000")];
102 unsigned short num;
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, &region);
111 num = uint16_fromregion(&region);
112 isc_region_consume(&region, 2);
113 sprintf(buf, "%u", num);
114 RETERR(str_totext(buf, target));
116 RETERR(str_totext(" ", target));
118 dns_name_fromregion(&name, &region);
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) {
125 dns_name_t name;
126 isc_region_t sregion;
128 REQUIRE(type == 15);
130 UNUSED(type);
131 UNUSED(rdclass);
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) {
147 dns_name_t name;
148 dns_offsets_t offsets;
149 isc_region_t region;
151 REQUIRE(rdata->type == 15);
152 REQUIRE(rdata->length != 0);
154 dns_compress_setmethods(cctx, DNS_COMPRESS_GLOBAL14);
156 dns_rdata_toregion(rdata, &region);
157 RETERR(mem_tobuffer(target, region.base, 2));
158 isc_region_consume(&region, 2);
160 dns_name_init(&name, offsets);
161 dns_name_fromregion(&name, &region);
163 return (dns_name_towire(&name, cctx, target));
166 static inline int
167 compare_mx(ARGS_COMPARE) {
168 dns_name_t name1;
169 dns_name_t name2;
170 isc_region_t region1;
171 isc_region_t region2;
172 int order;
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);
181 if (order != 0)
182 return (order < 0 ? -1 : 1);
184 dns_name_init(&name1, NULL);
185 dns_name_init(&name2, NULL);
187 dns_rdata_toregion(rdata1, &region1);
188 dns_rdata_toregion(rdata2, &region2);
190 isc_region_consume(&region1, 2);
191 isc_region_consume(&region2, 2);
193 dns_name_fromregion(&name1, &region1);
194 dns_name_fromregion(&name2, &region2);
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;
202 isc_region_t region;
204 REQUIRE(type == 15);
205 REQUIRE(source != NULL);
206 REQUIRE(mx->common.rdtype == type);
207 REQUIRE(mx->common.rdclass == rdclass);
209 UNUSED(type);
210 UNUSED(rdclass);
212 RETERR(uint16_tobuffer(mx->pref, target));
213 dns_name_toregion(&mx->mx, &region);
214 return (isc_buffer_copyregion(target, &region));
217 static inline isc_result_t
218 tostruct_mx(ARGS_TOSTRUCT) {
219 isc_region_t region;
220 dns_rdata_mx_t *mx = target;
221 dns_name_t name;
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, &region);
233 mx->pref = uint16_fromregion(&region);
234 isc_region_consume(&region, 2);
235 dns_name_fromregion(&name, &region);
236 dns_name_init(&mx->mx, NULL);
237 RETERR(name_duporclone(&name, mctx, &mx->mx));
238 mx->mctx = mctx;
239 return (ISC_R_SUCCESS);
242 static inline void
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)
250 return;
252 dns_name_free(&mx->mx, mx->mctx);
253 mx->mctx = NULL;
256 static inline isc_result_t
257 additionaldata_mx(ARGS_ADDLDATA) {
258 dns_name_t name;
259 dns_offsets_t offsets;
260 isc_region_t region;
262 REQUIRE(rdata->type == 15);
264 dns_name_init(&name, offsets);
265 dns_rdata_toregion(rdata, &region);
266 isc_region_consume(&region, 2);
267 dns_name_fromregion(&name, &region);
269 return ((add)(arg, &name, dns_rdatatype_a));
272 static inline isc_result_t
273 digest_mx(ARGS_DIGEST) {
274 isc_region_t r1, r2;
275 dns_name_t name;
277 REQUIRE(rdata->type == 15);
279 dns_rdata_toregion(rdata, &r1);
280 r2 = r1;
281 isc_region_consume(&r2, 2);
282 r1.length = 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) {
292 REQUIRE(type == 15);
294 UNUSED(type);
295 UNUSED(rdclass);
297 return (dns_name_ishostname(name, wildcard));
300 static inline isc_boolean_t
301 checknames_mx(ARGS_CHECKNAMES) {
302 isc_region_t region;
303 dns_name_t name;
305 REQUIRE(rdata->type == 15);
307 UNUSED(owner);
309 dns_rdata_toregion(rdata, &region);
310 isc_region_consume(&region, 2);
311 dns_name_init(&name, NULL);
312 dns_name_fromregion(&name, &region);
313 if (!dns_name_ishostname(&name, ISC_FALSE)) {
314 if (bad != NULL)
315 dns_name_clone(&name, bad);
316 return (ISC_FALSE);
318 return (ISC_TRUE);
321 static inline int
322 casecompare_mx(ARGS_COMPARE) {
323 return (compare_mx(rdata1, rdata2));
326 #endif /* RDATA_GENERIC_MX_15_C */