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