1/*
2 * Copyright (C) 2004, 2005, 2007, 2009, 2014  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: rt_21.c,v 1.48 2009/12/04 22:06:37 tbox Exp $ */
19
20/* reviewed: Thu Mar 16 15:02:31 PST 2000 by brister */
21
22/* RFC1183 */
23
24#ifndef RDATA_GENERIC_RT_21_C
25#define RDATA_GENERIC_RT_21_C
26
27#define RRTYPE_RT_ATTRIBUTES (0)
28
29static inline isc_result_t
30fromtext_rt(ARGS_FROMTEXT) {
31	isc_token_t token;
32	dns_name_t name;
33	isc_buffer_t buffer;
34	isc_boolean_t ok;
35
36	REQUIRE(type == 21);
37
38	UNUSED(type);
39	UNUSED(rdclass);
40	UNUSED(callbacks);
41
42	RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_number,
43				      ISC_FALSE));
44	if (token.value.as_ulong > 0xffffU)
45		RETTOK(ISC_R_RANGE);
46	RETERR(uint16_tobuffer(token.value.as_ulong, target));
47
48	RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
49				      ISC_FALSE));
50
51	dns_name_init(&name, NULL);
52	buffer_fromregion(&buffer, &token.value.as_region);
53	origin = (origin != NULL) ? origin : dns_rootname;
54	RETTOK(dns_name_fromtext(&name, &buffer, origin, options, target));
55	ok = ISC_TRUE;
56	if ((options & DNS_RDATA_CHECKNAMES) != 0)
57		ok = dns_name_ishostname(&name, ISC_FALSE);
58	if (!ok && (options & DNS_RDATA_CHECKNAMESFAIL) != 0)
59		RETTOK(DNS_R_BADNAME);
60	if (!ok && callbacks != NULL)
61		warn_badname(&name, lexer, callbacks);
62	return (ISC_R_SUCCESS);
63}
64
65static inline isc_result_t
66totext_rt(ARGS_TOTEXT) {
67	isc_region_t region;
68	dns_name_t name;
69	dns_name_t prefix;
70	isc_boolean_t sub;
71	char buf[sizeof("64000")];
72	unsigned short num;
73
74	REQUIRE(rdata->type == 21);
75	REQUIRE(rdata->length != 0);
76
77	dns_name_init(&name, NULL);
78	dns_name_init(&prefix, NULL);
79
80	dns_rdata_toregion(rdata, &region);
81	num = uint16_fromregion(&region);
82	isc_region_consume(&region, 2);
83	sprintf(buf, "%u", num);
84	RETERR(str_totext(buf, target));
85	RETERR(str_totext(" ", target));
86	dns_name_fromregion(&name, &region);
87	sub = name_prefix(&name, tctx->origin, &prefix);
88	return (dns_name_totext(&prefix, sub, target));
89}
90
91static inline isc_result_t
92fromwire_rt(ARGS_FROMWIRE) {
93	dns_name_t name;
94	isc_region_t sregion;
95	isc_region_t tregion;
96
97	REQUIRE(type == 21);
98
99	UNUSED(type);
100	UNUSED(rdclass);
101
102	dns_decompress_setmethods(dctx, DNS_COMPRESS_NONE);
103
104	dns_name_init(&name, NULL);
105
106	isc_buffer_activeregion(source, &sregion);
107	isc_buffer_availableregion(target, &tregion);
108	if (tregion.length < 2)
109		return (ISC_R_NOSPACE);
110	if (sregion.length < 2)
111		return (ISC_R_UNEXPECTEDEND);
112	memmove(tregion.base, sregion.base, 2);
113	isc_buffer_forward(source, 2);
114	isc_buffer_add(target, 2);
115	return (dns_name_fromwire(&name, source, dctx, options, target));
116}
117
118static inline isc_result_t
119towire_rt(ARGS_TOWIRE) {
120	dns_name_t name;
121	dns_offsets_t offsets;
122	isc_region_t region;
123	isc_region_t tr;
124
125	REQUIRE(rdata->type == 21);
126	REQUIRE(rdata->length != 0);
127
128	dns_compress_setmethods(cctx, DNS_COMPRESS_NONE);
129	isc_buffer_availableregion(target, &tr);
130	dns_rdata_toregion(rdata, &region);
131	if (tr.length < 2)
132		return (ISC_R_NOSPACE);
133	memmove(tr.base, region.base, 2);
134	isc_region_consume(&region, 2);
135	isc_buffer_add(target, 2);
136
137	dns_name_init(&name, offsets);
138	dns_name_fromregion(&name, &region);
139
140	return (dns_name_towire(&name, cctx, target));
141}
142
143static inline int
144compare_rt(ARGS_COMPARE) {
145	dns_name_t name1;
146	dns_name_t name2;
147	isc_region_t region1;
148	isc_region_t region2;
149	int order;
150
151	REQUIRE(rdata1->type == rdata2->type);
152	REQUIRE(rdata1->rdclass == rdata2->rdclass);
153	REQUIRE(rdata1->type == 21);
154	REQUIRE(rdata1->length != 0);
155	REQUIRE(rdata2->length != 0);
156
157	order = memcmp(rdata1->data, rdata2->data, 2);
158	if (order != 0)
159		return (order < 0 ? -1 : 1);
160
161	dns_name_init(&name1, NULL);
162	dns_name_init(&name2, NULL);
163
164	dns_rdata_toregion(rdata1, &region1);
165	dns_rdata_toregion(rdata2, &region2);
166
167	isc_region_consume(&region1, 2);
168	isc_region_consume(&region2, 2);
169
170	dns_name_fromregion(&name1, &region1);
171	dns_name_fromregion(&name2, &region2);
172
173	return (dns_name_rdatacompare(&name1, &name2));
174}
175
176static inline isc_result_t
177fromstruct_rt(ARGS_FROMSTRUCT) {
178	dns_rdata_rt_t *rt = source;
179	isc_region_t region;
180
181	REQUIRE(type == 21);
182	REQUIRE(source != NULL);
183	REQUIRE(rt->common.rdtype == type);
184	REQUIRE(rt->common.rdclass == rdclass);
185
186	UNUSED(type);
187	UNUSED(rdclass);
188
189	RETERR(uint16_tobuffer(rt->preference, target));
190	dns_name_toregion(&rt->host, &region);
191	return (isc_buffer_copyregion(target, &region));
192}
193
194static inline isc_result_t
195tostruct_rt(ARGS_TOSTRUCT) {
196	isc_region_t region;
197	dns_rdata_rt_t *rt = target;
198	dns_name_t name;
199
200	REQUIRE(rdata->type == 21);
201	REQUIRE(target != NULL);
202	REQUIRE(rdata->length != 0);
203
204	rt->common.rdclass = rdata->rdclass;
205	rt->common.rdtype = rdata->type;
206	ISC_LINK_INIT(&rt->common, link);
207
208	dns_name_init(&name, NULL);
209	dns_rdata_toregion(rdata, &region);
210	rt->preference = uint16_fromregion(&region);
211	isc_region_consume(&region, 2);
212	dns_name_fromregion(&name, &region);
213	dns_name_init(&rt->host, NULL);
214	RETERR(name_duporclone(&name, mctx, &rt->host));
215
216	rt->mctx = mctx;
217	return (ISC_R_SUCCESS);
218}
219
220static inline void
221freestruct_rt(ARGS_FREESTRUCT) {
222	dns_rdata_rt_t *rt = source;
223
224	REQUIRE(source != NULL);
225	REQUIRE(rt->common.rdtype == 21);
226
227	if (rt->mctx == NULL)
228		return;
229
230	dns_name_free(&rt->host, rt->mctx);
231	rt->mctx = NULL;
232}
233
234static inline isc_result_t
235additionaldata_rt(ARGS_ADDLDATA) {
236	dns_name_t name;
237	dns_offsets_t offsets;
238	isc_region_t region;
239	isc_result_t result;
240
241	REQUIRE(rdata->type == 21);
242
243	dns_name_init(&name, offsets);
244	dns_rdata_toregion(rdata, &region);
245	isc_region_consume(&region, 2);
246	dns_name_fromregion(&name, &region);
247
248	result = (add)(arg, &name, dns_rdatatype_x25);
249	if (result != ISC_R_SUCCESS)
250		return (result);
251	result = (add)(arg, &name, dns_rdatatype_isdn);
252	if (result != ISC_R_SUCCESS)
253		return (result);
254	return ((add)(arg, &name, dns_rdatatype_a));
255}
256
257static inline isc_result_t
258digest_rt(ARGS_DIGEST) {
259	isc_region_t r1, r2;
260	isc_result_t result;
261	dns_name_t name;
262
263	REQUIRE(rdata->type == 21);
264
265	dns_rdata_toregion(rdata, &r1);
266	r2 = r1;
267	isc_region_consume(&r2, 2);
268	r1.length = 2;
269	result = (digest)(arg, &r1);
270	if (result != ISC_R_SUCCESS)
271		return (result);
272	dns_name_init(&name, NULL);
273	dns_name_fromregion(&name, &r2);
274	return (dns_name_digest(&name, digest, arg));
275}
276
277static inline isc_boolean_t
278checkowner_rt(ARGS_CHECKOWNER) {
279
280	REQUIRE(type == 21);
281
282	UNUSED(name);
283	UNUSED(type);
284	UNUSED(rdclass);
285	UNUSED(wildcard);
286
287	return (ISC_TRUE);
288}
289
290static inline isc_boolean_t
291checknames_rt(ARGS_CHECKNAMES) {
292	isc_region_t region;
293	dns_name_t name;
294
295	REQUIRE(rdata->type == 21);
296
297	UNUSED(owner);
298
299	dns_rdata_toregion(rdata, &region);
300	isc_region_consume(&region, 2);
301	dns_name_init(&name, NULL);
302	dns_name_fromregion(&name, &region);
303	if (!dns_name_ishostname(&name, ISC_FALSE)) {
304		if (bad != NULL)
305			dns_name_clone(&name, bad);
306		return (ISC_FALSE);
307	}
308	return (ISC_TRUE);
309}
310
311static inline int
312casecompare_rt(ARGS_COMPARE) {
313	return (compare_rt(rdata1, rdata2));
314}
315
316#endif	/* RDATA_GENERIC_RT_21_C */
317