xref: /minix/external/bsd/bind/dist/lib/dns/rdata/in_1/kx_36.c (revision 00b67f09)
1 /*	$NetBSD: kx_36.c,v 1.4 2014/12/10 04:37:59 christos Exp $	*/
2 
3 /*
4  * Copyright (C) 2004, 2005, 2007, 2009  Internet Systems Consortium, Inc. ("ISC")
5  * Copyright (C) 1999-2001, 2003  Internet Software Consortium.
6  *
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.
10  *
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.
18  */
19 
20 /* Id: kx_36.c,v 1.47 2009/12/04 22:06:37 tbox Exp  */
21 
22 /* Reviewed: Thu Mar 16 17:24:54 PST 2000 by explorer */
23 
24 /* RFC2230 */
25 
26 #ifndef RDATA_IN_1_KX_36_C
27 #define RDATA_IN_1_KX_36_C
28 
29 #define RRTYPE_KX_ATTRIBUTES (0)
30 
31 static inline isc_result_t
fromtext_in_kx(ARGS_FROMTEXT)32 fromtext_in_kx(ARGS_FROMTEXT) {
33 	isc_token_t token;
34 	dns_name_t name;
35 	isc_buffer_t buffer;
36 
37 	REQUIRE(type == 36);
38 	REQUIRE(rdclass == 1);
39 
40 	UNUSED(type);
41 	UNUSED(rdclass);
42 	UNUSED(callbacks);
43 
44 	RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_number,
45 				      ISC_FALSE));
46 	if (token.value.as_ulong > 0xffffU)
47 		RETTOK(ISC_R_RANGE);
48 	RETERR(uint16_tobuffer(token.value.as_ulong, target));
49 
50 	RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
51 				      ISC_FALSE));
52 	dns_name_init(&name, NULL);
53 	buffer_fromregion(&buffer, &token.value.as_region);
54 	origin = (origin != NULL) ? origin : dns_rootname;
55 	RETTOK(dns_name_fromtext(&name, &buffer, origin, options, target));
56 	return (ISC_R_SUCCESS);
57 }
58 
59 static inline isc_result_t
totext_in_kx(ARGS_TOTEXT)60 totext_in_kx(ARGS_TOTEXT) {
61 	isc_region_t region;
62 	dns_name_t name;
63 	dns_name_t prefix;
64 	isc_boolean_t sub;
65 	char buf[sizeof("64000")];
66 	unsigned short num;
67 
68 	REQUIRE(rdata->type == 36);
69 	REQUIRE(rdata->rdclass == 1);
70 	REQUIRE(rdata->length != 0);
71 
72 	dns_name_init(&name, NULL);
73 	dns_name_init(&prefix, NULL);
74 
75 	dns_rdata_toregion(rdata, &region);
76 	num = uint16_fromregion(&region);
77 	isc_region_consume(&region, 2);
78 	sprintf(buf, "%u", num);
79 	RETERR(str_totext(buf, target));
80 
81 	RETERR(str_totext(" ", target));
82 
83 	dns_name_fromregion(&name, &region);
84 	sub = name_prefix(&name, tctx->origin, &prefix);
85 	return (dns_name_totext(&prefix, sub, target));
86 }
87 
88 static inline isc_result_t
fromwire_in_kx(ARGS_FROMWIRE)89 fromwire_in_kx(ARGS_FROMWIRE) {
90 	dns_name_t name;
91 	isc_region_t sregion;
92 
93 	REQUIRE(type == 36);
94 	REQUIRE(rdclass == 1);
95 
96 	UNUSED(type);
97 	UNUSED(rdclass);
98 
99 	dns_decompress_setmethods(dctx, DNS_COMPRESS_NONE);
100 
101 	dns_name_init(&name, NULL);
102 
103 	isc_buffer_activeregion(source, &sregion);
104 	if (sregion.length < 2)
105 		return (ISC_R_UNEXPECTEDEND);
106 	RETERR(mem_tobuffer(target, sregion.base, 2));
107 	isc_buffer_forward(source, 2);
108 	return (dns_name_fromwire(&name, source, dctx, options, target));
109 }
110 
111 static inline isc_result_t
towire_in_kx(ARGS_TOWIRE)112 towire_in_kx(ARGS_TOWIRE) {
113 	dns_name_t name;
114 	dns_offsets_t offsets;
115 	isc_region_t region;
116 
117 	REQUIRE(rdata->type == 36);
118 	REQUIRE(rdata->rdclass == 1);
119 	REQUIRE(rdata->length != 0);
120 
121 	dns_compress_setmethods(cctx, DNS_COMPRESS_NONE);
122 	dns_rdata_toregion(rdata, &region);
123 	RETERR(mem_tobuffer(target, region.base, 2));
124 	isc_region_consume(&region, 2);
125 
126 	dns_name_init(&name, offsets);
127 	dns_name_fromregion(&name, &region);
128 
129 	return (dns_name_towire(&name, cctx, target));
130 }
131 
132 static inline int
compare_in_kx(ARGS_COMPARE)133 compare_in_kx(ARGS_COMPARE) {
134 	dns_name_t name1;
135 	dns_name_t name2;
136 	isc_region_t region1;
137 	isc_region_t region2;
138 	int order;
139 
140 	REQUIRE(rdata1->type == rdata2->type);
141 	REQUIRE(rdata1->rdclass == rdata2->rdclass);
142 	REQUIRE(rdata1->type == 36);
143 	REQUIRE(rdata1->rdclass == 1);
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
fromstruct_in_kx(ARGS_FROMSTRUCT)167 fromstruct_in_kx(ARGS_FROMSTRUCT) {
168 	dns_rdata_in_kx_t *kx = source;
169 	isc_region_t region;
170 
171 	REQUIRE(type == 36);
172 	REQUIRE(rdclass == 1);
173 	REQUIRE(source != NULL);
174 	REQUIRE(kx->common.rdtype == type);
175 	REQUIRE(kx->common.rdclass == rdclass);
176 
177 	UNUSED(type);
178 	UNUSED(rdclass);
179 
180 	RETERR(uint16_tobuffer(kx->preference, target));
181 	dns_name_toregion(&kx->exchange, &region);
182 	return (isc_buffer_copyregion(target, &region));
183 }
184 
185 static inline isc_result_t
tostruct_in_kx(ARGS_TOSTRUCT)186 tostruct_in_kx(ARGS_TOSTRUCT) {
187 	isc_region_t region;
188 	dns_rdata_in_kx_t *kx = target;
189 	dns_name_t name;
190 
191 	REQUIRE(rdata->type == 36);
192 	REQUIRE(rdata->rdclass == 1);
193 	REQUIRE(target != NULL);
194 	REQUIRE(rdata->length != 0);
195 
196 	kx->common.rdclass = rdata->rdclass;
197 	kx->common.rdtype = rdata->type;
198 	ISC_LINK_INIT(&kx->common, link);
199 
200 	dns_name_init(&name, NULL);
201 	dns_rdata_toregion(rdata, &region);
202 
203 	kx->preference = uint16_fromregion(&region);
204 	isc_region_consume(&region, 2);
205 
206 	dns_name_fromregion(&name, &region);
207 	dns_name_init(&kx->exchange, NULL);
208 	RETERR(name_duporclone(&name, mctx, &kx->exchange));
209 	kx->mctx = mctx;
210 	return (ISC_R_SUCCESS);
211 }
212 
213 static inline void
freestruct_in_kx(ARGS_FREESTRUCT)214 freestruct_in_kx(ARGS_FREESTRUCT) {
215 	dns_rdata_in_kx_t *kx = source;
216 
217 	REQUIRE(source != NULL);
218 	REQUIRE(kx->common.rdclass == 1);
219 	REQUIRE(kx->common.rdtype == 36);
220 
221 	if (kx->mctx == NULL)
222 		return;
223 
224 	dns_name_free(&kx->exchange, kx->mctx);
225 	kx->mctx = NULL;
226 }
227 
228 static inline isc_result_t
additionaldata_in_kx(ARGS_ADDLDATA)229 additionaldata_in_kx(ARGS_ADDLDATA) {
230 	dns_name_t name;
231 	dns_offsets_t offsets;
232 	isc_region_t region;
233 
234 	REQUIRE(rdata->type == 36);
235 	REQUIRE(rdata->rdclass == 1);
236 
237 	dns_name_init(&name, offsets);
238 	dns_rdata_toregion(rdata, &region);
239 	isc_region_consume(&region, 2);
240 	dns_name_fromregion(&name, &region);
241 
242 	return ((add)(arg, &name, dns_rdatatype_a));
243 }
244 
245 static inline isc_result_t
digest_in_kx(ARGS_DIGEST)246 digest_in_kx(ARGS_DIGEST) {
247 	isc_region_t r1, r2;
248 	dns_name_t name;
249 
250 	REQUIRE(rdata->type == 36);
251 	REQUIRE(rdata->rdclass == 1);
252 
253 	dns_rdata_toregion(rdata, &r1);
254 	r2 = r1;
255 	isc_region_consume(&r2, 2);
256 	r1.length = 2;
257 	RETERR((digest)(arg, &r1));
258 	dns_name_init(&name, NULL);
259 	dns_name_fromregion(&name, &r2);
260 	return (dns_name_digest(&name, digest, arg));
261 }
262 
263 static inline isc_boolean_t
checkowner_in_kx(ARGS_CHECKOWNER)264 checkowner_in_kx(ARGS_CHECKOWNER) {
265 
266 	REQUIRE(type == 36);
267 	REQUIRE(rdclass == 1);
268 
269 	UNUSED(name);
270 	UNUSED(type);
271 	UNUSED(rdclass);
272 	UNUSED(wildcard);
273 
274 	return (ISC_TRUE);
275 }
276 
277 static inline isc_boolean_t
checknames_in_kx(ARGS_CHECKNAMES)278 checknames_in_kx(ARGS_CHECKNAMES) {
279 
280 	REQUIRE(rdata->type == 36);
281 	REQUIRE(rdata->rdclass == 1);
282 
283 	UNUSED(rdata);
284 	UNUSED(owner);
285 	UNUSED(bad);
286 
287 	return (ISC_TRUE);
288 }
289 
290 static inline int
casecompare_in_kx(ARGS_COMPARE)291 casecompare_in_kx(ARGS_COMPARE) {
292 	return (compare_in_kx(rdata1, rdata2));
293 }
294 
295 #endif	/* RDATA_IN_1_KX_36_C */
296