xref: /minix3/external/bsd/bind/dist/lib/dns/rdata/in_1/px_26.c (revision 00b67f09dd46474d133c95011a48590a8e8f94c7)
1 /*	$NetBSD: px_26.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: px_26.c,v 1.45 2009/12/04 22:06:37 tbox Exp  */
21 
22 /* Reviewed: Mon Mar 20 10:44:27 PST 2000 */
23 
24 /* RFC2163 */
25 
26 #ifndef RDATA_IN_1_PX_26_C
27 #define RDATA_IN_1_PX_26_C
28 
29 #define RRTYPE_PX_ATTRIBUTES (0)
30 
31 static inline isc_result_t
fromtext_in_px(ARGS_FROMTEXT)32 fromtext_in_px(ARGS_FROMTEXT) {
33 	isc_token_t token;
34 	dns_name_t name;
35 	isc_buffer_t buffer;
36 
37 	REQUIRE(type == 26);
38 	REQUIRE(rdclass == 1);
39 
40 	UNUSED(type);
41 	UNUSED(rdclass);
42 	UNUSED(callbacks);
43 
44 	/*
45 	 * Preference.
46 	 */
47 	RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_number,
48 				      ISC_FALSE));
49 	if (token.value.as_ulong > 0xffffU)
50 		RETTOK(ISC_R_RANGE);
51 	RETERR(uint16_tobuffer(token.value.as_ulong, target));
52 
53 	/*
54 	 * MAP822.
55 	 */
56 	RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
57 				      ISC_FALSE));
58 	dns_name_init(&name, NULL);
59 	buffer_fromregion(&buffer, &token.value.as_region);
60 	origin = (origin != NULL) ? origin : dns_rootname;
61 	RETTOK(dns_name_fromtext(&name, &buffer, origin, options, target));
62 
63 	/*
64 	 * MAPX400.
65 	 */
66 	RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
67 				      ISC_FALSE));
68 	dns_name_init(&name, NULL);
69 	buffer_fromregion(&buffer, &token.value.as_region);
70 	origin = (origin != NULL) ? origin : dns_rootname;
71 	RETTOK(dns_name_fromtext(&name, &buffer, origin, options, target));
72 	return (ISC_R_SUCCESS);
73 }
74 
75 static inline isc_result_t
totext_in_px(ARGS_TOTEXT)76 totext_in_px(ARGS_TOTEXT) {
77 	isc_region_t region;
78 	dns_name_t name;
79 	dns_name_t prefix;
80 	isc_boolean_t sub;
81 	char buf[sizeof("64000")];
82 	unsigned short num;
83 
84 	REQUIRE(rdata->type == 26);
85 	REQUIRE(rdata->rdclass == 1);
86 	REQUIRE(rdata->length != 0);
87 
88 	dns_name_init(&name, NULL);
89 	dns_name_init(&prefix, NULL);
90 
91 	/*
92 	 * Preference.
93 	 */
94 	dns_rdata_toregion(rdata, &region);
95 	num = uint16_fromregion(&region);
96 	isc_region_consume(&region, 2);
97 	sprintf(buf, "%u", num);
98 	RETERR(str_totext(buf, target));
99 	RETERR(str_totext(" ", target));
100 
101 	/*
102 	 * MAP822.
103 	 */
104 	dns_name_fromregion(&name, &region);
105 	sub = name_prefix(&name, tctx->origin, &prefix);
106 	isc_region_consume(&region, name_length(&name));
107 	RETERR(dns_name_totext(&prefix, sub, target));
108 	RETERR(str_totext(" ", target));
109 
110 	/*
111 	 * MAPX400.
112 	 */
113 	dns_name_fromregion(&name, &region);
114 	sub = name_prefix(&name, tctx->origin, &prefix);
115 	return(dns_name_totext(&prefix, sub, target));
116 }
117 
118 static inline isc_result_t
fromwire_in_px(ARGS_FROMWIRE)119 fromwire_in_px(ARGS_FROMWIRE) {
120 	dns_name_t name;
121 	isc_region_t sregion;
122 
123 	REQUIRE(type == 26);
124 	REQUIRE(rdclass == 1);
125 
126 	UNUSED(type);
127 	UNUSED(rdclass);
128 
129 	dns_decompress_setmethods(dctx, DNS_COMPRESS_NONE);
130 
131 	dns_name_init(&name, NULL);
132 
133 	/*
134 	 * Preference.
135 	 */
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 
142 	/*
143 	 * MAP822.
144 	 */
145 	RETERR(dns_name_fromwire(&name, source, dctx, options, target));
146 
147 	/*
148 	 * MAPX400.
149 	 */
150 	return (dns_name_fromwire(&name, source, dctx, options, target));
151 }
152 
153 static inline isc_result_t
towire_in_px(ARGS_TOWIRE)154 towire_in_px(ARGS_TOWIRE) {
155 	dns_name_t name;
156 	dns_offsets_t offsets;
157 	isc_region_t region;
158 
159 	REQUIRE(rdata->type == 26);
160 	REQUIRE(rdata->rdclass == 1);
161 	REQUIRE(rdata->length != 0);
162 
163 	dns_compress_setmethods(cctx, DNS_COMPRESS_NONE);
164 	/*
165 	 * Preference.
166 	 */
167 	dns_rdata_toregion(rdata, &region);
168 	RETERR(mem_tobuffer(target, region.base, 2));
169 	isc_region_consume(&region, 2);
170 
171 	/*
172 	 * MAP822.
173 	 */
174 	dns_name_init(&name, offsets);
175 	dns_name_fromregion(&name, &region);
176 	RETERR(dns_name_towire(&name, cctx, target));
177 	isc_region_consume(&region, name_length(&name));
178 
179 	/*
180 	 * MAPX400.
181 	 */
182 	dns_name_init(&name, offsets);
183 	dns_name_fromregion(&name, &region);
184 	return (dns_name_towire(&name, cctx, target));
185 }
186 
187 static inline int
compare_in_px(ARGS_COMPARE)188 compare_in_px(ARGS_COMPARE) {
189 	dns_name_t name1;
190 	dns_name_t name2;
191 	isc_region_t region1;
192 	isc_region_t region2;
193 	int order;
194 
195 	REQUIRE(rdata1->type == rdata2->type);
196 	REQUIRE(rdata1->rdclass == rdata2->rdclass);
197 	REQUIRE(rdata1->type == 26);
198 	REQUIRE(rdata1->rdclass == 1);
199 	REQUIRE(rdata1->length != 0);
200 	REQUIRE(rdata2->length != 0);
201 
202 	order = memcmp(rdata1->data, rdata2->data, 2);
203 	if (order != 0)
204 		return (order < 0 ? -1 : 1);
205 
206 	dns_name_init(&name1, NULL);
207 	dns_name_init(&name2, NULL);
208 
209 	dns_rdata_toregion(rdata1, &region1);
210 	dns_rdata_toregion(rdata2, &region2);
211 
212 	isc_region_consume(&region1, 2);
213 	isc_region_consume(&region2, 2);
214 
215 	dns_name_fromregion(&name1, &region1);
216 	dns_name_fromregion(&name2, &region2);
217 
218 	order = dns_name_rdatacompare(&name1, &name2);
219 	if (order != 0)
220 		return (order);
221 
222 	isc_region_consume(&region1, name_length(&name1));
223 	isc_region_consume(&region2, name_length(&name2));
224 
225 	dns_name_fromregion(&name1, &region1);
226 	dns_name_fromregion(&name2, &region2);
227 
228 	return (dns_name_rdatacompare(&name1, &name2));
229 }
230 
231 static inline isc_result_t
fromstruct_in_px(ARGS_FROMSTRUCT)232 fromstruct_in_px(ARGS_FROMSTRUCT) {
233 	dns_rdata_in_px_t *px = source;
234 	isc_region_t region;
235 
236 	REQUIRE(type == 26);
237 	REQUIRE(rdclass == 1);
238 	REQUIRE(source != NULL);
239 	REQUIRE(px->common.rdtype == type);
240 	REQUIRE(px->common.rdclass == rdclass);
241 
242 	UNUSED(type);
243 	UNUSED(rdclass);
244 
245 	RETERR(uint16_tobuffer(px->preference, target));
246 	dns_name_toregion(&px->map822, &region);
247 	RETERR(isc_buffer_copyregion(target, &region));
248 	dns_name_toregion(&px->mapx400, &region);
249 	return (isc_buffer_copyregion(target, &region));
250 }
251 
252 static inline isc_result_t
tostruct_in_px(ARGS_TOSTRUCT)253 tostruct_in_px(ARGS_TOSTRUCT) {
254 	dns_rdata_in_px_t *px = target;
255 	dns_name_t name;
256 	isc_region_t region;
257 	isc_result_t result;
258 
259 	REQUIRE(rdata->type == 26);
260 	REQUIRE(rdata->rdclass == 1);
261 	REQUIRE(target != NULL);
262 	REQUIRE(rdata->length != 0);
263 
264 	px->common.rdclass = rdata->rdclass;
265 	px->common.rdtype = rdata->type;
266 	ISC_LINK_INIT(&px->common, link);
267 
268 	dns_name_init(&name, NULL);
269 	dns_rdata_toregion(rdata, &region);
270 
271 	px->preference = uint16_fromregion(&region);
272 	isc_region_consume(&region, 2);
273 
274 	dns_name_fromregion(&name, &region);
275 
276 	dns_name_init(&px->map822, NULL);
277 	RETERR(name_duporclone(&name, mctx, &px->map822));
278 	isc_region_consume(&region, name_length(&px->map822));
279 
280 	dns_name_init(&px->mapx400, NULL);
281 	result = name_duporclone(&name, mctx, &px->mapx400);
282 	if (result != ISC_R_SUCCESS)
283 		goto cleanup;
284 
285 	px->mctx = mctx;
286 	return (result);
287 
288  cleanup:
289 	dns_name_free(&px->map822, mctx);
290 	return (ISC_R_NOMEMORY);
291 }
292 
293 static inline void
freestruct_in_px(ARGS_FREESTRUCT)294 freestruct_in_px(ARGS_FREESTRUCT) {
295 	dns_rdata_in_px_t *px = source;
296 
297 	REQUIRE(source != NULL);
298 	REQUIRE(px->common.rdclass == 1);
299 	REQUIRE(px->common.rdtype == 26);
300 
301 	if (px->mctx == NULL)
302 		return;
303 
304 	dns_name_free(&px->map822, px->mctx);
305 	dns_name_free(&px->mapx400, px->mctx);
306 	px->mctx = NULL;
307 }
308 
309 static inline isc_result_t
additionaldata_in_px(ARGS_ADDLDATA)310 additionaldata_in_px(ARGS_ADDLDATA) {
311 	REQUIRE(rdata->type == 26);
312 	REQUIRE(rdata->rdclass == 1);
313 
314 	UNUSED(rdata);
315 	UNUSED(add);
316 	UNUSED(arg);
317 
318 	return (ISC_R_SUCCESS);
319 }
320 
321 static inline isc_result_t
digest_in_px(ARGS_DIGEST)322 digest_in_px(ARGS_DIGEST) {
323 	isc_region_t r1, r2;
324 	dns_name_t name;
325 	isc_result_t result;
326 
327 	REQUIRE(rdata->type == 26);
328 	REQUIRE(rdata->rdclass == 1);
329 
330 	dns_rdata_toregion(rdata, &r1);
331 	r2 = r1;
332 	isc_region_consume(&r2, 2);
333 	r1.length = 2;
334 	result = (digest)(arg, &r1);
335 	if (result != ISC_R_SUCCESS)
336 		return (result);
337 	dns_name_init(&name, NULL);
338 	dns_name_fromregion(&name, &r2);
339 	result = dns_name_digest(&name, digest, arg);
340 	if (result != ISC_R_SUCCESS)
341 		return (result);
342 	isc_region_consume(&r2, name_length(&name));
343 	dns_name_init(&name, NULL);
344 	dns_name_fromregion(&name, &r2);
345 
346 	return (dns_name_digest(&name, digest, arg));
347 }
348 
349 static inline isc_boolean_t
checkowner_in_px(ARGS_CHECKOWNER)350 checkowner_in_px(ARGS_CHECKOWNER) {
351 
352 	REQUIRE(type == 26);
353 	REQUIRE(rdclass == 1);
354 
355 	UNUSED(name);
356 	UNUSED(type);
357 	UNUSED(rdclass);
358 	UNUSED(wildcard);
359 
360 	return (ISC_TRUE);
361 }
362 
363 static inline isc_boolean_t
checknames_in_px(ARGS_CHECKNAMES)364 checknames_in_px(ARGS_CHECKNAMES) {
365 
366 	REQUIRE(rdata->type == 26);
367 	REQUIRE(rdata->rdclass == 1);
368 
369 	UNUSED(rdata);
370 	UNUSED(owner);
371 	UNUSED(bad);
372 
373 	return (ISC_TRUE);
374 }
375 
376 static inline int
casecompare_in_px(ARGS_COMPARE)377 casecompare_in_px(ARGS_COMPARE) {
378 	return (compare_in_px(rdata1, rdata2));
379 }
380 
381 #endif	/* RDATA_IN_1_PX_26_C */
382