1 /*	$NetBSD: ns_2.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: ns_2.c,v 1.48 2009/12/04 22:06:37 tbox Exp  */
21 
22 /* Reviewed: Wed Mar 15 18:15:00 PST 2000 by bwelling */
23 
24 #ifndef RDATA_GENERIC_NS_2_C
25 #define RDATA_GENERIC_NS_2_C
26 
27 #define RRTYPE_NS_ATTRIBUTES (DNS_RDATATYPEATTR_ZONECUTAUTH)
28 
29 static inline isc_result_t
30 fromtext_ns(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 == 2);
37 
38 	UNUSED(type);
39 	UNUSED(rdclass);
40 	UNUSED(callbacks);
41 
42 	RETERR(isc_lex_getmastertoken(lexer, &token,isc_tokentype_string,
43 				      ISC_FALSE));
44 
45 	dns_name_init(&name, NULL);
46 	buffer_fromregion(&buffer, &token.value.as_region);
47 	origin = (origin != NULL) ? origin : dns_rootname;
48 	RETTOK(dns_name_fromtext(&name, &buffer, origin, options, target));
49 	ok = ISC_TRUE;
50 	if ((options & DNS_RDATA_CHECKNAMES) != 0)
51 		ok = dns_name_ishostname(&name, ISC_FALSE);
52 	if (!ok && (options & DNS_RDATA_CHECKNAMESFAIL) != 0)
53 		RETTOK(DNS_R_BADNAME);
54 	if (!ok && callbacks != NULL)
55 		warn_badname(&name, lexer, callbacks);
56 	return (ISC_R_SUCCESS);
57 }
58 
59 static inline isc_result_t
60 totext_ns(ARGS_TOTEXT) {
61 	isc_region_t region;
62 	dns_name_t name;
63 	dns_name_t prefix;
64 	isc_boolean_t sub;
65 
66 	REQUIRE(rdata->type == 2);
67 	REQUIRE(rdata->length != 0);
68 
69 	dns_name_init(&name, NULL);
70 	dns_name_init(&prefix, NULL);
71 
72 	dns_rdata_toregion(rdata, &region);
73 	dns_name_fromregion(&name, &region);
74 
75 	sub = name_prefix(&name, tctx->origin, &prefix);
76 
77 	return (dns_name_totext(&prefix, sub, target));
78 }
79 
80 static inline isc_result_t
81 fromwire_ns(ARGS_FROMWIRE) {
82 	dns_name_t name;
83 
84 	REQUIRE(type == 2);
85 
86 	UNUSED(type);
87 	UNUSED(rdclass);
88 
89 	dns_decompress_setmethods(dctx, DNS_COMPRESS_GLOBAL14);
90 
91 	dns_name_init(&name, NULL);
92 	return (dns_name_fromwire(&name, source, dctx, options, target));
93 }
94 
95 static inline isc_result_t
96 towire_ns(ARGS_TOWIRE) {
97 	dns_name_t name;
98 	dns_offsets_t offsets;
99 	isc_region_t region;
100 
101 	REQUIRE(rdata->type == 2);
102 	REQUIRE(rdata->length != 0);
103 
104 	dns_compress_setmethods(cctx, DNS_COMPRESS_GLOBAL14);
105 
106 	dns_name_init(&name, offsets);
107 	dns_rdata_toregion(rdata, &region);
108 	dns_name_fromregion(&name, &region);
109 
110 	return (dns_name_towire(&name, cctx, target));
111 }
112 
113 static inline int
114 compare_ns(ARGS_COMPARE) {
115 	dns_name_t name1;
116 	dns_name_t name2;
117 	isc_region_t region1;
118 	isc_region_t region2;
119 
120 	REQUIRE(rdata1->type == rdata2->type);
121 	REQUIRE(rdata1->rdclass == rdata2->rdclass);
122 	REQUIRE(rdata1->type == 2);
123 	REQUIRE(rdata1->length != 0);
124 	REQUIRE(rdata2->length != 0);
125 
126 	dns_name_init(&name1, NULL);
127 	dns_name_init(&name2, NULL);
128 
129 	dns_rdata_toregion(rdata1, &region1);
130 	dns_rdata_toregion(rdata2, &region2);
131 
132 	dns_name_fromregion(&name1, &region1);
133 	dns_name_fromregion(&name2, &region2);
134 
135 	return (dns_name_rdatacompare(&name1, &name2));
136 }
137 
138 static inline isc_result_t
139 fromstruct_ns(ARGS_FROMSTRUCT) {
140 	dns_rdata_ns_t *ns = source;
141 	isc_region_t region;
142 
143 	REQUIRE(type == 2);
144 	REQUIRE(source != NULL);
145 	REQUIRE(ns->common.rdtype == type);
146 	REQUIRE(ns->common.rdclass == rdclass);
147 
148 	UNUSED(type);
149 	UNUSED(rdclass);
150 
151 	dns_name_toregion(&ns->name, &region);
152 	return (isc_buffer_copyregion(target, &region));
153 }
154 
155 static inline isc_result_t
156 tostruct_ns(ARGS_TOSTRUCT) {
157 	isc_region_t region;
158 	dns_rdata_ns_t *ns = target;
159 	dns_name_t name;
160 
161 	REQUIRE(rdata->type == 2);
162 	REQUIRE(target != NULL);
163 	REQUIRE(rdata->length != 0);
164 
165 	ns->common.rdclass = rdata->rdclass;
166 	ns->common.rdtype = rdata->type;
167 	ISC_LINK_INIT(&ns->common, link);
168 
169 	dns_name_init(&name, NULL);
170 	dns_rdata_toregion(rdata, &region);
171 	dns_name_fromregion(&name, &region);
172 	dns_name_init(&ns->name, NULL);
173 	RETERR(name_duporclone(&name, mctx, &ns->name));
174 	ns->mctx = mctx;
175 	return (ISC_R_SUCCESS);
176 }
177 
178 static inline void
179 freestruct_ns(ARGS_FREESTRUCT) {
180 	dns_rdata_ns_t *ns = source;
181 
182 	REQUIRE(source != NULL);
183 
184 	if (ns->mctx == NULL)
185 		return;
186 
187 	dns_name_free(&ns->name, ns->mctx);
188 	ns->mctx = NULL;
189 }
190 
191 static inline isc_result_t
192 additionaldata_ns(ARGS_ADDLDATA) {
193 	dns_name_t name;
194 	dns_offsets_t offsets;
195 	isc_region_t region;
196 
197 	REQUIRE(rdata->type == 2);
198 
199 	dns_name_init(&name, offsets);
200 	dns_rdata_toregion(rdata, &region);
201 	dns_name_fromregion(&name, &region);
202 
203 	return ((add)(arg, &name, dns_rdatatype_a));
204 }
205 
206 static inline isc_result_t
207 digest_ns(ARGS_DIGEST) {
208 	isc_region_t r;
209 	dns_name_t name;
210 
211 	REQUIRE(rdata->type == 2);
212 
213 	dns_rdata_toregion(rdata, &r);
214 	dns_name_init(&name, NULL);
215 	dns_name_fromregion(&name, &r);
216 
217 	return (dns_name_digest(&name, digest, arg));
218 }
219 
220 static inline isc_boolean_t
221 checkowner_ns(ARGS_CHECKOWNER) {
222 
223 	REQUIRE(type == 2);
224 
225 	UNUSED(name);
226 	UNUSED(type);
227 	UNUSED(rdclass);
228 	UNUSED(wildcard);
229 
230 	return (ISC_TRUE);
231 }
232 
233 static inline isc_boolean_t
234 checknames_ns(ARGS_CHECKNAMES) {
235 	isc_region_t region;
236 	dns_name_t name;
237 
238 	REQUIRE(rdata->type == 2);
239 
240 	UNUSED(owner);
241 
242 	dns_rdata_toregion(rdata, &region);
243 	dns_name_init(&name, NULL);
244 	dns_name_fromregion(&name, &region);
245 	if (!dns_name_ishostname(&name, ISC_FALSE)) {
246 		if (bad != NULL)
247 			dns_name_clone(&name, bad);
248 		return (ISC_FALSE);
249 	}
250 	return (ISC_TRUE);
251 }
252 
253 static inline int
254 casecompare_ns(ARGS_COMPARE) {
255 	return (compare_ns(rdata1, rdata2));
256 }
257 
258 #endif	/* RDATA_GENERIC_NS_2_C */
259