xref: /netbsd-src/external/mpl/bind/dist/lib/dns/rdata/in_1/px_26.c (revision bcda20f65a8566e103791ec395f7f499ef322704)
1 /*	$NetBSD: px_26.c,v 1.9 2025/01/26 16:25:35 christos Exp $	*/
2 
3 /*
4  * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
5  *
6  * SPDX-License-Identifier: MPL-2.0
7  *
8  * This Source Code Form is subject to the terms of the Mozilla Public
9  * License, v. 2.0. If a copy of the MPL was not distributed with this
10  * file, you can obtain one at https://mozilla.org/MPL/2.0/.
11  *
12  * See the COPYRIGHT file distributed with this work for additional
13  * information regarding copyright ownership.
14  */
15 
16 /* RFC2163 */
17 
18 #ifndef RDATA_IN_1_PX_26_C
19 #define RDATA_IN_1_PX_26_C
20 
21 #define RRTYPE_PX_ATTRIBUTES (0)
22 
23 static isc_result_t
24 fromtext_in_px(ARGS_FROMTEXT) {
25 	isc_token_t token;
26 	dns_name_t name;
27 	isc_buffer_t buffer;
28 
29 	REQUIRE(type == dns_rdatatype_px);
30 	REQUIRE(rdclass == dns_rdataclass_in);
31 
32 	UNUSED(type);
33 	UNUSED(rdclass);
34 	UNUSED(callbacks);
35 
36 	if (origin == NULL) {
37 		origin = dns_rootname;
38 	}
39 
40 	/*
41 	 * Preference.
42 	 */
43 	RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_number,
44 				      false));
45 	if (token.value.as_ulong > 0xffffU) {
46 		RETTOK(ISC_R_RANGE);
47 	}
48 	RETERR(uint16_tobuffer(token.value.as_ulong, target));
49 
50 	/*
51 	 * MAP822.
52 	 */
53 	RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
54 				      false));
55 	dns_name_init(&name, NULL);
56 	buffer_fromregion(&buffer, &token.value.as_region);
57 	RETTOK(dns_name_fromtext(&name, &buffer, origin, options, target));
58 
59 	/*
60 	 * MAPX400.
61 	 */
62 	RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
63 				      false));
64 	dns_name_init(&name, NULL);
65 	buffer_fromregion(&buffer, &token.value.as_region);
66 	RETTOK(dns_name_fromtext(&name, &buffer, origin, options, target));
67 	return ISC_R_SUCCESS;
68 }
69 
70 static isc_result_t
71 totext_in_px(ARGS_TOTEXT) {
72 	isc_region_t region;
73 	dns_name_t name;
74 	dns_name_t prefix;
75 	unsigned int opts;
76 	char buf[sizeof("64000")];
77 	unsigned short num;
78 
79 	REQUIRE(rdata->type == dns_rdatatype_px);
80 	REQUIRE(rdata->rdclass == dns_rdataclass_in);
81 	REQUIRE(rdata->length != 0);
82 
83 	dns_name_init(&name, NULL);
84 	dns_name_init(&prefix, NULL);
85 
86 	/*
87 	 * Preference.
88 	 */
89 	dns_rdata_toregion(rdata, &region);
90 	num = uint16_fromregion(&region);
91 	isc_region_consume(&region, 2);
92 	snprintf(buf, sizeof(buf), "%u", num);
93 	RETERR(str_totext(buf, target));
94 	RETERR(str_totext(" ", target));
95 
96 	/*
97 	 * MAP822.
98 	 */
99 	dns_name_fromregion(&name, &region);
100 	opts = name_prefix(&name, tctx->origin, &prefix) ? DNS_NAME_OMITFINALDOT
101 							 : 0;
102 	isc_region_consume(&region, name_length(&name));
103 	RETERR(dns_name_totext(&prefix, opts, target));
104 	RETERR(str_totext(" ", target));
105 
106 	/*
107 	 * MAPX400.
108 	 */
109 	dns_name_fromregion(&name, &region);
110 	opts = name_prefix(&name, tctx->origin, &prefix) ? DNS_NAME_OMITFINALDOT
111 							 : 0;
112 	return dns_name_totext(&prefix, opts, target);
113 }
114 
115 static isc_result_t
116 fromwire_in_px(ARGS_FROMWIRE) {
117 	dns_name_t name;
118 	isc_region_t sregion;
119 
120 	REQUIRE(type == dns_rdatatype_px);
121 	REQUIRE(rdclass == dns_rdataclass_in);
122 
123 	UNUSED(type);
124 	UNUSED(rdclass);
125 
126 	dctx = dns_decompress_setpermitted(dctx, false);
127 
128 	dns_name_init(&name, NULL);
129 
130 	/*
131 	 * Preference.
132 	 */
133 	isc_buffer_activeregion(source, &sregion);
134 	if (sregion.length < 2) {
135 		return ISC_R_UNEXPECTEDEND;
136 	}
137 	RETERR(mem_tobuffer(target, sregion.base, 2));
138 	isc_buffer_forward(source, 2);
139 
140 	/*
141 	 * MAP822.
142 	 */
143 	RETERR(dns_name_fromwire(&name, source, dctx, target));
144 
145 	/*
146 	 * MAPX400.
147 	 */
148 	return dns_name_fromwire(&name, source, dctx, target);
149 }
150 
151 static isc_result_t
152 towire_in_px(ARGS_TOWIRE) {
153 	dns_name_t name;
154 	dns_offsets_t offsets;
155 	isc_region_t region;
156 
157 	REQUIRE(rdata->type == dns_rdatatype_px);
158 	REQUIRE(rdata->rdclass == dns_rdataclass_in);
159 	REQUIRE(rdata->length != 0);
160 
161 	dns_compress_setpermitted(cctx, false);
162 	/*
163 	 * Preference.
164 	 */
165 	dns_rdata_toregion(rdata, &region);
166 	RETERR(mem_tobuffer(target, region.base, 2));
167 	isc_region_consume(&region, 2);
168 
169 	/*
170 	 * MAP822.
171 	 */
172 	dns_name_init(&name, offsets);
173 	dns_name_fromregion(&name, &region);
174 	RETERR(dns_name_towire(&name, cctx, target, NULL));
175 	isc_region_consume(&region, name_length(&name));
176 
177 	/*
178 	 * MAPX400.
179 	 */
180 	dns_name_init(&name, offsets);
181 	dns_name_fromregion(&name, &region);
182 	return dns_name_towire(&name, cctx, target, NULL);
183 }
184 
185 static int
186 compare_in_px(ARGS_COMPARE) {
187 	dns_name_t name1;
188 	dns_name_t name2;
189 	isc_region_t region1;
190 	isc_region_t region2;
191 	int order;
192 
193 	REQUIRE(rdata1->type == rdata2->type);
194 	REQUIRE(rdata1->rdclass == rdata2->rdclass);
195 	REQUIRE(rdata1->type == dns_rdatatype_px);
196 	REQUIRE(rdata1->rdclass == dns_rdataclass_in);
197 	REQUIRE(rdata1->length != 0);
198 	REQUIRE(rdata2->length != 0);
199 
200 	order = memcmp(rdata1->data, rdata2->data, 2);
201 	if (order != 0) {
202 		return order < 0 ? -1 : 1;
203 	}
204 
205 	dns_name_init(&name1, NULL);
206 	dns_name_init(&name2, NULL);
207 
208 	dns_rdata_toregion(rdata1, &region1);
209 	dns_rdata_toregion(rdata2, &region2);
210 
211 	isc_region_consume(&region1, 2);
212 	isc_region_consume(&region2, 2);
213 
214 	dns_name_fromregion(&name1, &region1);
215 	dns_name_fromregion(&name2, &region2);
216 
217 	order = dns_name_rdatacompare(&name1, &name2);
218 	if (order != 0) {
219 		return order;
220 	}
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 isc_result_t
232 fromstruct_in_px(ARGS_FROMSTRUCT) {
233 	dns_rdata_in_px_t *px = source;
234 	isc_region_t region;
235 
236 	REQUIRE(type == dns_rdatatype_px);
237 	REQUIRE(rdclass == dns_rdataclass_in);
238 	REQUIRE(px != 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 isc_result_t
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 
258 	REQUIRE(rdata->type == dns_rdatatype_px);
259 	REQUIRE(rdata->rdclass == dns_rdataclass_in);
260 	REQUIRE(px != NULL);
261 	REQUIRE(rdata->length != 0);
262 
263 	px->common.rdclass = rdata->rdclass;
264 	px->common.rdtype = rdata->type;
265 	ISC_LINK_INIT(&px->common, link);
266 
267 	dns_name_init(&name, NULL);
268 	dns_rdata_toregion(rdata, &region);
269 
270 	px->preference = uint16_fromregion(&region);
271 	isc_region_consume(&region, 2);
272 
273 	dns_name_fromregion(&name, &region);
274 
275 	dns_name_init(&px->map822, NULL);
276 	name_duporclone(&name, mctx, &px->map822);
277 	isc_region_consume(&region, name_length(&px->map822));
278 
279 	dns_name_init(&px->mapx400, NULL);
280 	name_duporclone(&name, mctx, &px->mapx400);
281 
282 	px->mctx = mctx;
283 	return ISC_R_SUCCESS;
284 }
285 
286 static void
287 freestruct_in_px(ARGS_FREESTRUCT) {
288 	dns_rdata_in_px_t *px = source;
289 
290 	REQUIRE(px != NULL);
291 	REQUIRE(px->common.rdclass == dns_rdataclass_in);
292 	REQUIRE(px->common.rdtype == dns_rdatatype_px);
293 
294 	if (px->mctx == NULL) {
295 		return;
296 	}
297 
298 	dns_name_free(&px->map822, px->mctx);
299 	dns_name_free(&px->mapx400, px->mctx);
300 	px->mctx = NULL;
301 }
302 
303 static isc_result_t
304 additionaldata_in_px(ARGS_ADDLDATA) {
305 	REQUIRE(rdata->type == dns_rdatatype_px);
306 	REQUIRE(rdata->rdclass == dns_rdataclass_in);
307 
308 	UNUSED(rdata);
309 	UNUSED(owner);
310 	UNUSED(add);
311 	UNUSED(arg);
312 
313 	return ISC_R_SUCCESS;
314 }
315 
316 static isc_result_t
317 digest_in_px(ARGS_DIGEST) {
318 	isc_region_t r1, r2;
319 	dns_name_t name;
320 	isc_result_t result;
321 
322 	REQUIRE(rdata->type == dns_rdatatype_px);
323 	REQUIRE(rdata->rdclass == dns_rdataclass_in);
324 
325 	dns_rdata_toregion(rdata, &r1);
326 	r2 = r1;
327 	isc_region_consume(&r2, 2);
328 	r1.length = 2;
329 	result = (digest)(arg, &r1);
330 	if (result != ISC_R_SUCCESS) {
331 		return result;
332 	}
333 	dns_name_init(&name, NULL);
334 	dns_name_fromregion(&name, &r2);
335 	result = dns_name_digest(&name, digest, arg);
336 	if (result != ISC_R_SUCCESS) {
337 		return result;
338 	}
339 	isc_region_consume(&r2, name_length(&name));
340 	dns_name_init(&name, NULL);
341 	dns_name_fromregion(&name, &r2);
342 
343 	return dns_name_digest(&name, digest, arg);
344 }
345 
346 static bool
347 checkowner_in_px(ARGS_CHECKOWNER) {
348 	REQUIRE(type == dns_rdatatype_px);
349 	REQUIRE(rdclass == dns_rdataclass_in);
350 
351 	UNUSED(name);
352 	UNUSED(type);
353 	UNUSED(rdclass);
354 	UNUSED(wildcard);
355 
356 	return true;
357 }
358 
359 static bool
360 checknames_in_px(ARGS_CHECKNAMES) {
361 	REQUIRE(rdata->type == dns_rdatatype_px);
362 	REQUIRE(rdata->rdclass == dns_rdataclass_in);
363 
364 	UNUSED(rdata);
365 	UNUSED(owner);
366 	UNUSED(bad);
367 
368 	return true;
369 }
370 
371 static int
372 casecompare_in_px(ARGS_COMPARE) {
373 	return compare_in_px(rdata1, rdata2);
374 }
375 
376 #endif /* RDATA_IN_1_PX_26_C */
377