1 /*	$NetBSD: dname_39.c,v 1.7 2022/09/23 12:15:31 christos Exp $	*/
2 
3 /*
4  * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
5  *
6  * SPDX-License-Identifier: MPL-2.0
7  *
8  * This Source Code Form is subject to the terms of the Mozilla Public
9  * License, v. 2.0. If a copy of the MPL was not distributed with this
10  * file, you can obtain one at https://mozilla.org/MPL/2.0/.
11  *
12  * See the COPYRIGHT file distributed with this work for additional
13  * information regarding copyright ownership.
14  */
15 
16 /* RFC2672 */
17 
18 #ifndef RDATA_GENERIC_DNAME_39_C
19 #define RDATA_GENERIC_DNAME_39_C
20 
21 #define RRTYPE_DNAME_ATTRIBUTES (DNS_RDATATYPEATTR_SINGLETON)
22 
23 static isc_result_t
fromtext_dname(ARGS_FROMTEXT)24 fromtext_dname(ARGS_FROMTEXT) {
25 	isc_token_t token;
26 	dns_name_t name;
27 	isc_buffer_t buffer;
28 
29 	REQUIRE(type == dns_rdatatype_dname);
30 
31 	UNUSED(type);
32 	UNUSED(rdclass);
33 	UNUSED(callbacks);
34 
35 	RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
36 				      false));
37 
38 	dns_name_init(&name, NULL);
39 	buffer_fromregion(&buffer, &token.value.as_region);
40 	if (origin == NULL) {
41 		origin = dns_rootname;
42 	}
43 	RETTOK(dns_name_fromtext(&name, &buffer, origin, options, target));
44 	return (ISC_R_SUCCESS);
45 }
46 
47 static isc_result_t
totext_dname(ARGS_TOTEXT)48 totext_dname(ARGS_TOTEXT) {
49 	isc_region_t region;
50 	dns_name_t name;
51 	dns_name_t prefix;
52 	bool sub;
53 
54 	REQUIRE(rdata->type == dns_rdatatype_dname);
55 	REQUIRE(rdata->length != 0);
56 
57 	dns_name_init(&name, NULL);
58 	dns_name_init(&prefix, NULL);
59 
60 	dns_rdata_toregion(rdata, &region);
61 	dns_name_fromregion(&name, &region);
62 
63 	sub = name_prefix(&name, tctx->origin, &prefix);
64 
65 	return (dns_name_totext(&prefix, sub, target));
66 }
67 
68 static isc_result_t
fromwire_dname(ARGS_FROMWIRE)69 fromwire_dname(ARGS_FROMWIRE) {
70 	dns_name_t name;
71 
72 	REQUIRE(type == dns_rdatatype_dname);
73 
74 	UNUSED(type);
75 	UNUSED(rdclass);
76 
77 	dns_decompress_setmethods(dctx, DNS_COMPRESS_NONE);
78 
79 	dns_name_init(&name, NULL);
80 	return (dns_name_fromwire(&name, source, dctx, options, target));
81 }
82 
83 static isc_result_t
towire_dname(ARGS_TOWIRE)84 towire_dname(ARGS_TOWIRE) {
85 	dns_name_t name;
86 	dns_offsets_t offsets;
87 	isc_region_t region;
88 
89 	REQUIRE(rdata->type == dns_rdatatype_dname);
90 	REQUIRE(rdata->length != 0);
91 
92 	dns_compress_setmethods(cctx, DNS_COMPRESS_NONE);
93 	dns_name_init(&name, offsets);
94 	dns_rdata_toregion(rdata, &region);
95 	dns_name_fromregion(&name, &region);
96 
97 	return (dns_name_towire(&name, cctx, target));
98 }
99 
100 static int
compare_dname(ARGS_COMPARE)101 compare_dname(ARGS_COMPARE) {
102 	dns_name_t name1;
103 	dns_name_t name2;
104 	isc_region_t region1;
105 	isc_region_t region2;
106 
107 	REQUIRE(rdata1->type == rdata2->type);
108 	REQUIRE(rdata1->rdclass == rdata2->rdclass);
109 	REQUIRE(rdata1->type == dns_rdatatype_dname);
110 	REQUIRE(rdata1->length != 0);
111 	REQUIRE(rdata2->length != 0);
112 
113 	dns_name_init(&name1, NULL);
114 	dns_name_init(&name2, NULL);
115 
116 	dns_rdata_toregion(rdata1, &region1);
117 	dns_rdata_toregion(rdata2, &region2);
118 
119 	dns_name_fromregion(&name1, &region1);
120 	dns_name_fromregion(&name2, &region2);
121 
122 	return (dns_name_rdatacompare(&name1, &name2));
123 }
124 
125 static isc_result_t
fromstruct_dname(ARGS_FROMSTRUCT)126 fromstruct_dname(ARGS_FROMSTRUCT) {
127 	dns_rdata_dname_t *dname = source;
128 	isc_region_t region;
129 
130 	REQUIRE(type == dns_rdatatype_dname);
131 	REQUIRE(dname != NULL);
132 	REQUIRE(dname->common.rdtype == type);
133 	REQUIRE(dname->common.rdclass == rdclass);
134 
135 	UNUSED(type);
136 	UNUSED(rdclass);
137 
138 	dns_name_toregion(&dname->dname, &region);
139 	return (isc_buffer_copyregion(target, &region));
140 }
141 
142 static isc_result_t
tostruct_dname(ARGS_TOSTRUCT)143 tostruct_dname(ARGS_TOSTRUCT) {
144 	isc_region_t region;
145 	dns_rdata_dname_t *dname = target;
146 	dns_name_t name;
147 
148 	REQUIRE(rdata->type == dns_rdatatype_dname);
149 	REQUIRE(dname != NULL);
150 	REQUIRE(rdata->length != 0);
151 
152 	dname->common.rdclass = rdata->rdclass;
153 	dname->common.rdtype = rdata->type;
154 	ISC_LINK_INIT(&dname->common, link);
155 
156 	dns_name_init(&name, NULL);
157 	dns_rdata_toregion(rdata, &region);
158 	dns_name_fromregion(&name, &region);
159 	dns_name_init(&dname->dname, NULL);
160 	RETERR(name_duporclone(&name, mctx, &dname->dname));
161 	dname->mctx = mctx;
162 	return (ISC_R_SUCCESS);
163 }
164 
165 static void
freestruct_dname(ARGS_FREESTRUCT)166 freestruct_dname(ARGS_FREESTRUCT) {
167 	dns_rdata_dname_t *dname = source;
168 
169 	REQUIRE(dname != NULL);
170 	REQUIRE(dname->common.rdtype == dns_rdatatype_dname);
171 
172 	if (dname->mctx == NULL) {
173 		return;
174 	}
175 
176 	dns_name_free(&dname->dname, dname->mctx);
177 	dname->mctx = NULL;
178 }
179 
180 static isc_result_t
additionaldata_dname(ARGS_ADDLDATA)181 additionaldata_dname(ARGS_ADDLDATA) {
182 	UNUSED(rdata);
183 	UNUSED(add);
184 	UNUSED(arg);
185 
186 	REQUIRE(rdata->type == dns_rdatatype_dname);
187 
188 	return (ISC_R_SUCCESS);
189 }
190 
191 static isc_result_t
digest_dname(ARGS_DIGEST)192 digest_dname(ARGS_DIGEST) {
193 	isc_region_t r;
194 	dns_name_t name;
195 
196 	REQUIRE(rdata->type == dns_rdatatype_dname);
197 
198 	dns_rdata_toregion(rdata, &r);
199 	dns_name_init(&name, NULL);
200 	dns_name_fromregion(&name, &r);
201 
202 	return (dns_name_digest(&name, digest, arg));
203 }
204 
205 static bool
checkowner_dname(ARGS_CHECKOWNER)206 checkowner_dname(ARGS_CHECKOWNER) {
207 	REQUIRE(type == dns_rdatatype_dname);
208 
209 	UNUSED(name);
210 	UNUSED(type);
211 	UNUSED(rdclass);
212 	UNUSED(wildcard);
213 
214 	return (true);
215 }
216 
217 static bool
checknames_dname(ARGS_CHECKNAMES)218 checknames_dname(ARGS_CHECKNAMES) {
219 	REQUIRE(rdata->type == dns_rdatatype_dname);
220 
221 	UNUSED(rdata);
222 	UNUSED(owner);
223 	UNUSED(bad);
224 
225 	return (true);
226 }
227 
228 static int
casecompare_dname(ARGS_COMPARE)229 casecompare_dname(ARGS_COMPARE) {
230 	return (compare_dname(rdata1, rdata2));
231 }
232 #endif /* RDATA_GENERIC_DNAME_39_C */
233