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