Merge branch 'vendor/TCSH'
[dragonfly.git] / contrib / bind-9.3 / lib / dns / rdata / generic / mx_15.c
1 /*
2  * Copyright (C) 2004  Internet Systems Consortium, Inc. ("ISC")
3  * Copyright (C) 1998-2001, 2003  Internet Software Consortium.
4  *
5  * Permission to use, copy, modify, and distribute this software for any
6  * purpose with or without fee is hereby granted, provided that the above
7  * copyright notice and this permission notice appear in all copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
10  * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11  * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
12  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
14  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15  * PERFORMANCE OF THIS SOFTWARE.
16  */
17
18 /* $Id: mx_15.c,v 1.48.2.1.2.3 2004/03/06 08:14:08 marka Exp $ */
19
20 /* reviewed: Wed Mar 15 18:05:46 PST 2000 by brister */
21
22 #ifndef RDATA_GENERIC_MX_15_C
23 #define RDATA_GENERIC_MX_15_C
24
25 #define RRTYPE_MX_ATTRIBUTES (0)
26
27 static inline isc_result_t
28 fromtext_mx(ARGS_FROMTEXT) {
29         isc_token_t token;
30         dns_name_t name;
31         isc_buffer_t buffer;
32         isc_boolean_t ok;
33
34         REQUIRE(type == 15);
35
36         UNUSED(type);
37         UNUSED(rdclass);
38         UNUSED(callbacks);
39
40         RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_number,
41                                       ISC_FALSE));
42         if (token.value.as_ulong > 0xffffU)
43                 RETTOK(ISC_R_RANGE);
44         RETERR(uint16_tobuffer(token.value.as_ulong, target));
45
46         RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
47                                       ISC_FALSE));
48         dns_name_init(&name, NULL);
49         buffer_fromregion(&buffer, &token.value.as_region);
50         origin = (origin != NULL) ? origin : dns_rootname;
51         RETTOK(dns_name_fromtext(&name, &buffer, origin, options, target));
52         ok = ISC_TRUE;
53         if ((options & DNS_RDATA_CHECKNAMES) != 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);
59         return (ISC_R_SUCCESS);
60 }
61
62 static inline isc_result_t
63 totext_mx(ARGS_TOTEXT) {
64         isc_region_t region;
65         dns_name_t name;
66         dns_name_t prefix;
67         isc_boolean_t sub;
68         char buf[sizeof("64000")];
69         unsigned short num;
70
71         REQUIRE(rdata->type == 15);
72         REQUIRE(rdata->length != 0);
73
74         dns_name_init(&name, NULL);
75         dns_name_init(&prefix, NULL);
76
77         dns_rdata_toregion(rdata, &region);
78         num = uint16_fromregion(&region);
79         isc_region_consume(&region, 2);
80         sprintf(buf, "%u", num);
81         RETERR(str_totext(buf, target));
82
83         RETERR(str_totext(" ", target));
84
85         dns_name_fromregion(&name, &region);
86         sub = name_prefix(&name, tctx->origin, &prefix);
87         return (dns_name_totext(&prefix, sub, target));
88 }
89
90 static inline isc_result_t
91 fromwire_mx(ARGS_FROMWIRE) {
92         dns_name_t name;
93         isc_region_t sregion;
94
95         REQUIRE(type == 15);
96
97         UNUSED(type);
98         UNUSED(rdclass);
99
100         dns_decompress_setmethods(dctx, DNS_COMPRESS_GLOBAL14);
101
102         dns_name_init(&name, NULL);
103
104         isc_buffer_activeregion(source, &sregion);
105         if (sregion.length < 2)
106                 return (ISC_R_UNEXPECTEDEND);
107         RETERR(mem_tobuffer(target, sregion.base, 2));
108         isc_buffer_forward(source, 2);
109         return (dns_name_fromwire(&name, source, dctx, options, target));
110 }
111
112 static inline isc_result_t
113 towire_mx(ARGS_TOWIRE) {
114         dns_name_t name;
115         dns_offsets_t offsets;
116         isc_region_t region;
117
118         REQUIRE(rdata->type == 15);
119         REQUIRE(rdata->length != 0);
120
121         dns_compress_setmethods(cctx, DNS_COMPRESS_GLOBAL14);
122
123         dns_rdata_toregion(rdata, &region);
124         RETERR(mem_tobuffer(target, region.base, 2));
125         isc_region_consume(&region, 2);
126
127         dns_name_init(&name, offsets);
128         dns_name_fromregion(&name, &region);
129
130         return (dns_name_towire(&name, cctx, target));
131 }
132
133 static inline int
134 compare_mx(ARGS_COMPARE) {
135         dns_name_t name1;
136         dns_name_t name2;
137         isc_region_t region1;
138         isc_region_t region2;
139         int order;
140
141         REQUIRE(rdata1->type == rdata2->type);
142         REQUIRE(rdata1->rdclass == rdata2->rdclass);
143         REQUIRE(rdata1->type == 15);
144         REQUIRE(rdata1->length != 0);
145         REQUIRE(rdata2->length != 0);
146
147         order = memcmp(rdata1->data, rdata2->data, 2);
148         if (order != 0)
149                 return (order < 0 ? -1 : 1);
150
151         dns_name_init(&name1, NULL);
152         dns_name_init(&name2, NULL);
153
154         dns_rdata_toregion(rdata1, &region1);
155         dns_rdata_toregion(rdata2, &region2);
156
157         isc_region_consume(&region1, 2);
158         isc_region_consume(&region2, 2);
159
160         dns_name_fromregion(&name1, &region1);
161         dns_name_fromregion(&name2, &region2);
162
163         return (dns_name_rdatacompare(&name1, &name2));
164 }
165
166 static inline isc_result_t
167 fromstruct_mx(ARGS_FROMSTRUCT) {
168         dns_rdata_mx_t *mx = source;
169         isc_region_t region;
170
171         REQUIRE(type == 15);
172         REQUIRE(source != NULL);
173         REQUIRE(mx->common.rdtype == type);
174         REQUIRE(mx->common.rdclass == rdclass);
175
176         UNUSED(type);
177         UNUSED(rdclass);
178
179         RETERR(uint16_tobuffer(mx->pref, target));
180         dns_name_toregion(&mx->mx, &region);
181         return (isc_buffer_copyregion(target, &region));
182 }
183
184 static inline isc_result_t
185 tostruct_mx(ARGS_TOSTRUCT) {
186         isc_region_t region;
187         dns_rdata_mx_t *mx = target;
188         dns_name_t name;
189
190         REQUIRE(rdata->type == 15);
191         REQUIRE(target != NULL);
192         REQUIRE(rdata->length != 0);
193
194         mx->common.rdclass = rdata->rdclass;
195         mx->common.rdtype = rdata->type;
196         ISC_LINK_INIT(&mx->common, link);
197
198         dns_name_init(&name, NULL);
199         dns_rdata_toregion(rdata, &region);
200         mx->pref = uint16_fromregion(&region);
201         isc_region_consume(&region, 2);
202         dns_name_fromregion(&name, &region);
203         dns_name_init(&mx->mx, NULL);
204         RETERR(name_duporclone(&name, mctx, &mx->mx));
205         mx->mctx = mctx;
206         return (ISC_R_SUCCESS);
207 }
208
209 static inline void
210 freestruct_mx(ARGS_FREESTRUCT) {
211         dns_rdata_mx_t *mx = source;
212
213         REQUIRE(source != NULL);
214         REQUIRE(mx->common.rdtype == 15);
215
216         if (mx->mctx == NULL)
217                 return;
218
219         dns_name_free(&mx->mx, mx->mctx);
220         mx->mctx = NULL;
221 }
222
223 static inline isc_result_t
224 additionaldata_mx(ARGS_ADDLDATA) {
225         dns_name_t name;
226         dns_offsets_t offsets;
227         isc_region_t region;
228
229         REQUIRE(rdata->type == 15);
230
231         dns_name_init(&name, offsets);
232         dns_rdata_toregion(rdata, &region);
233         isc_region_consume(&region, 2);
234         dns_name_fromregion(&name, &region);
235
236         return ((add)(arg, &name, dns_rdatatype_a));
237 }
238
239 static inline isc_result_t
240 digest_mx(ARGS_DIGEST) {
241         isc_region_t r1, r2;
242         dns_name_t name;
243
244         REQUIRE(rdata->type == 15);
245
246         dns_rdata_toregion(rdata, &r1);
247         r2 = r1;
248         isc_region_consume(&r2, 2);
249         r1.length = 2;
250         RETERR((digest)(arg, &r1));
251         dns_name_init(&name, NULL);
252         dns_name_fromregion(&name, &r2);
253         return (dns_name_digest(&name, digest, arg));
254 }
255
256 static inline isc_boolean_t
257 checkowner_mx(ARGS_CHECKOWNER) {
258
259         REQUIRE(type == 15);
260
261         UNUSED(type);
262         UNUSED(rdclass);
263
264         return (dns_name_ishostname(name, wildcard));
265 }
266
267 static inline isc_boolean_t
268 checknames_mx(ARGS_CHECKNAMES) {
269         isc_region_t region;
270         dns_name_t name;
271
272         REQUIRE(rdata->type == 15);
273
274         UNUSED(owner);
275
276         dns_rdata_toregion(rdata, &region);
277         isc_region_consume(&region, 2);
278         dns_name_init(&name, NULL);
279         dns_name_fromregion(&name, &region);
280         if (!dns_name_ishostname(&name, ISC_FALSE)) {
281                 if (bad != NULL)
282                         dns_name_clone(&name, bad);
283                 return (ISC_FALSE);
284         }
285         return (ISC_TRUE);
286 }
287
288 #endif  /* RDATA_GENERIC_MX_15_C */