1 /*	$NetBSD: ptr_12.c,v 1.4 2014/12/10 04:37:59 christos Exp $	*/
2 
3 /*
4  * Copyright (C) 2004, 2007, 2009  Internet Systems Consortium, Inc. ("ISC")
5  * Copyright (C) 1998-2001  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: ptr_12.c,v 1.45 2009/12/04 22:06:37 tbox Exp  */
21 
22 /* Reviewed: Thu Mar 16 14:05:12 PST 2000 by explorer */
23 
24 #ifndef RDATA_GENERIC_PTR_12_C
25 #define RDATA_GENERIC_PTR_12_C
26 
27 #define RRTYPE_PTR_ATTRIBUTES (0)
28 
29 static inline isc_result_t
fromtext_ptr(ARGS_FROMTEXT)30 fromtext_ptr(ARGS_FROMTEXT) {
31 	isc_token_t token;
32 	dns_name_t name;
33 	isc_buffer_t buffer;
34 
35 	REQUIRE(type == 12);
36 
37 	UNUSED(type);
38 	UNUSED(rdclass);
39 	UNUSED(callbacks);
40 
41 	RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
42 				      ISC_FALSE));
43 
44 	dns_name_init(&name, NULL);
45 	buffer_fromregion(&buffer, &token.value.as_region);
46 	origin = (origin != NULL) ? origin : dns_rootname;
47 	RETTOK(dns_name_fromtext(&name, &buffer, origin, options, target));
48 	if (rdclass == dns_rdataclass_in &&
49 	    (options & DNS_RDATA_CHECKNAMES) != 0 &&
50 	    (options & DNS_RDATA_CHECKREVERSE) != 0) {
51 		isc_boolean_t ok;
52 		ok = dns_name_ishostname(&name, ISC_FALSE);
53 		if (!ok && (options & DNS_RDATA_CHECKNAMESFAIL) != 0)
54 			RETTOK(DNS_R_BADNAME);
55 		if (!ok && callbacks != NULL)
56 			warn_badname(&name, lexer, callbacks);
57 	}
58 	return (ISC_R_SUCCESS);
59 }
60 
61 static inline isc_result_t
totext_ptr(ARGS_TOTEXT)62 totext_ptr(ARGS_TOTEXT) {
63 	isc_region_t region;
64 	dns_name_t name;
65 	dns_name_t prefix;
66 	isc_boolean_t sub;
67 
68 	REQUIRE(rdata->type == 12);
69 	REQUIRE(rdata->length != 0);
70 
71 	dns_name_init(&name, NULL);
72 	dns_name_init(&prefix, NULL);
73 
74 	dns_rdata_toregion(rdata, &region);
75 	dns_name_fromregion(&name, &region);
76 
77 	sub = name_prefix(&name, tctx->origin, &prefix);
78 
79 	return (dns_name_totext(&prefix, sub, target));
80 }
81 
82 static inline isc_result_t
fromwire_ptr(ARGS_FROMWIRE)83 fromwire_ptr(ARGS_FROMWIRE) {
84 	dns_name_t name;
85 
86 	REQUIRE(type == 12);
87 
88 	UNUSED(type);
89 	UNUSED(rdclass);
90 
91 	dns_decompress_setmethods(dctx, DNS_COMPRESS_GLOBAL14);
92 
93 	dns_name_init(&name, NULL);
94 	return (dns_name_fromwire(&name, source, dctx, options, target));
95 }
96 
97 static inline isc_result_t
towire_ptr(ARGS_TOWIRE)98 towire_ptr(ARGS_TOWIRE) {
99 	dns_name_t name;
100 	dns_offsets_t offsets;
101 	isc_region_t region;
102 
103 	REQUIRE(rdata->type == 12);
104 	REQUIRE(rdata->length != 0);
105 
106 	dns_compress_setmethods(cctx, DNS_COMPRESS_GLOBAL14);
107 
108 	dns_name_init(&name, offsets);
109 	dns_rdata_toregion(rdata, &region);
110 	dns_name_fromregion(&name, &region);
111 
112 	return (dns_name_towire(&name, cctx, target));
113 }
114 
115 static inline int
compare_ptr(ARGS_COMPARE)116 compare_ptr(ARGS_COMPARE) {
117 	dns_name_t name1;
118 	dns_name_t name2;
119 	isc_region_t region1;
120 	isc_region_t region2;
121 
122 	REQUIRE(rdata1->type == rdata2->type);
123 	REQUIRE(rdata1->rdclass == rdata2->rdclass);
124 	REQUIRE(rdata1->type == 12);
125 	REQUIRE(rdata1->length != 0);
126 	REQUIRE(rdata2->length != 0);
127 
128 	dns_name_init(&name1, NULL);
129 	dns_name_init(&name2, NULL);
130 
131 	dns_rdata_toregion(rdata1, &region1);
132 	dns_rdata_toregion(rdata2, &region2);
133 
134 	dns_name_fromregion(&name1, &region1);
135 	dns_name_fromregion(&name2, &region2);
136 
137 	return (dns_name_rdatacompare(&name1, &name2));
138 }
139 
140 static inline isc_result_t
fromstruct_ptr(ARGS_FROMSTRUCT)141 fromstruct_ptr(ARGS_FROMSTRUCT) {
142 	dns_rdata_ptr_t *ptr = source;
143 	isc_region_t region;
144 
145 	REQUIRE(type == 12);
146 	REQUIRE(source != NULL);
147 	REQUIRE(ptr->common.rdtype == type);
148 	REQUIRE(ptr->common.rdclass == rdclass);
149 
150 	UNUSED(type);
151 	UNUSED(rdclass);
152 
153 	dns_name_toregion(&ptr->ptr, &region);
154 	return (isc_buffer_copyregion(target, &region));
155 }
156 
157 static inline isc_result_t
tostruct_ptr(ARGS_TOSTRUCT)158 tostruct_ptr(ARGS_TOSTRUCT) {
159 	isc_region_t region;
160 	dns_rdata_ptr_t *ptr = target;
161 	dns_name_t name;
162 
163 	REQUIRE(rdata->type == 12);
164 	REQUIRE(target != NULL);
165 	REQUIRE(rdata->length != 0);
166 
167 	ptr->common.rdclass = rdata->rdclass;
168 	ptr->common.rdtype = rdata->type;
169 	ISC_LINK_INIT(&ptr->common, link);
170 
171 	dns_name_init(&name, NULL);
172 	dns_rdata_toregion(rdata, &region);
173 	dns_name_fromregion(&name, &region);
174 	dns_name_init(&ptr->ptr, NULL);
175 	RETERR(name_duporclone(&name, mctx, &ptr->ptr));
176 	ptr->mctx = mctx;
177 	return (ISC_R_SUCCESS);
178 }
179 
180 static inline void
freestruct_ptr(ARGS_FREESTRUCT)181 freestruct_ptr(ARGS_FREESTRUCT) {
182 	dns_rdata_ptr_t *ptr = source;
183 
184 	REQUIRE(source != NULL);
185 	REQUIRE(ptr->common.rdtype == 12);
186 
187 	if (ptr->mctx == NULL)
188 		return;
189 
190 	dns_name_free(&ptr->ptr, ptr->mctx);
191 	ptr->mctx = NULL;
192 }
193 
194 static inline isc_result_t
additionaldata_ptr(ARGS_ADDLDATA)195 additionaldata_ptr(ARGS_ADDLDATA) {
196 	REQUIRE(rdata->type == 12);
197 
198 	UNUSED(rdata);
199 	UNUSED(add);
200 	UNUSED(arg);
201 
202 	return (ISC_R_SUCCESS);
203 }
204 
205 static inline isc_result_t
digest_ptr(ARGS_DIGEST)206 digest_ptr(ARGS_DIGEST) {
207 	isc_region_t r;
208 	dns_name_t name;
209 
210 	REQUIRE(rdata->type == 12);
211 
212 	dns_rdata_toregion(rdata, &r);
213 	dns_name_init(&name, NULL);
214 	dns_name_fromregion(&name, &r);
215 
216 	return (dns_name_digest(&name, digest, arg));
217 }
218 
219 static inline isc_boolean_t
checkowner_ptr(ARGS_CHECKOWNER)220 checkowner_ptr(ARGS_CHECKOWNER) {
221 
222 	REQUIRE(type == 12);
223 
224 	UNUSED(name);
225 	UNUSED(type);
226 	UNUSED(rdclass);
227 	UNUSED(wildcard);
228 
229 	return (ISC_TRUE);
230 }
231 
232 static unsigned char ip6_arpa_data[]  = "\003IP6\004ARPA";
233 static unsigned char ip6_arpa_offsets[] = { 0, 4, 9 };
234 static const dns_name_t ip6_arpa =
235 {
236 	DNS_NAME_MAGIC,
237 	ip6_arpa_data, 10, 3,
238 	DNS_NAMEATTR_READONLY | DNS_NAMEATTR_ABSOLUTE,
239 	ip6_arpa_offsets, NULL,
240 	{(void *)-1, (void *)-1},
241 	{NULL, NULL}
242 };
243 
244 static unsigned char ip6_int_data[]  = "\003IP6\003INT";
245 static unsigned char ip6_int_offsets[] = { 0, 4, 8 };
246 static const dns_name_t ip6_int =
247 {
248 	DNS_NAME_MAGIC,
249 	ip6_int_data, 9, 3,
250 	DNS_NAMEATTR_READONLY | DNS_NAMEATTR_ABSOLUTE,
251 	ip6_int_offsets, NULL,
252 	{(void *)-1, (void *)-1},
253 	{NULL, NULL}
254 };
255 
256 static unsigned char in_addr_arpa_data[]  = "\007IN-ADDR\004ARPA";
257 static unsigned char in_addr_arpa_offsets[] = { 0, 8, 13 };
258 static const dns_name_t in_addr_arpa =
259 {
260 	DNS_NAME_MAGIC,
261 	in_addr_arpa_data, 14, 3,
262 	DNS_NAMEATTR_READONLY | DNS_NAMEATTR_ABSOLUTE,
263 	in_addr_arpa_offsets, NULL,
264 	{(void *)-1, (void *)-1},
265 	{NULL, NULL}
266 };
267 
268 static inline isc_boolean_t
checknames_ptr(ARGS_CHECKNAMES)269 checknames_ptr(ARGS_CHECKNAMES) {
270 	isc_region_t region;
271 	dns_name_t name;
272 
273 	REQUIRE(rdata->type == 12);
274 
275 	if (rdata->rdclass != dns_rdataclass_in)
276 	    return (ISC_TRUE);
277 
278 	if (dns_name_issubdomain(owner, &in_addr_arpa) ||
279 	    dns_name_issubdomain(owner, &ip6_arpa) ||
280 	    dns_name_issubdomain(owner, &ip6_int)) {
281 		dns_rdata_toregion(rdata, &region);
282 		dns_name_init(&name, NULL);
283 		dns_name_fromregion(&name, &region);
284 		if (!dns_name_ishostname(&name, ISC_FALSE)) {
285 			if (bad != NULL)
286 				dns_name_clone(&name, bad);
287 			return (ISC_FALSE);
288 		}
289 	}
290 	return (ISC_TRUE);
291 }
292 
293 static inline int
casecompare_ptr(ARGS_COMPARE)294 casecompare_ptr(ARGS_COMPARE) {
295 	return (compare_ptr(rdata1, rdata2));
296 }
297 #endif	/* RDATA_GENERIC_PTR_12_C */
298