xref: /netbsd-src/external/mpl/bind/dist/lib/dns/rdata/generic/rp_17.c (revision e670fd5c413e99c2f6a37901bb21c537fcd322d2)
1 /*	$NetBSD: rp_17.c,v 1.6 2021/02/19 16:42:17 christos Exp $	*/
2 
3 /*
4  * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
5  *
6  * This Source Code Form is subject to the terms of the Mozilla Public
7  * License, v. 2.0. If a copy of the MPL was not distributed with this
8  * file, you can obtain one at https://mozilla.org/MPL/2.0/.
9  *
10  * See the COPYRIGHT file distributed with this work for additional
11  * information regarding copyright ownership.
12  */
13 
14 /* RFC1183 */
15 
16 #ifndef RDATA_GENERIC_RP_17_C
17 #define RDATA_GENERIC_RP_17_C
18 
19 #define RRTYPE_RP_ATTRIBUTES (0)
20 
21 static inline isc_result_t
22 fromtext_rp(ARGS_FROMTEXT) {
23 	isc_token_t token;
24 	dns_name_t name;
25 	isc_buffer_t buffer;
26 	int i;
27 	bool ok;
28 
29 	REQUIRE(type == dns_rdatatype_rp);
30 
31 	UNUSED(type);
32 	UNUSED(rdclass);
33 	UNUSED(callbacks);
34 
35 	if (origin == NULL) {
36 		origin = dns_rootname;
37 	}
38 
39 	for (i = 0; i < 2; i++) {
40 		RETERR(isc_lex_getmastertoken(lexer, &token,
41 					      isc_tokentype_string, false));
42 		dns_name_init(&name, NULL);
43 		buffer_fromregion(&buffer, &token.value.as_region);
44 		RETTOK(dns_name_fromtext(&name, &buffer, origin, options,
45 					 target));
46 		ok = true;
47 		if ((options & DNS_RDATA_CHECKNAMES) != 0 && i == 0) {
48 			ok = dns_name_ismailbox(&name);
49 		}
50 		if (!ok && (options & DNS_RDATA_CHECKNAMESFAIL) != 0) {
51 			RETTOK(DNS_R_BADNAME);
52 		}
53 		if (!ok && callbacks != NULL) {
54 			warn_badname(&name, lexer, callbacks);
55 		}
56 	}
57 	return (ISC_R_SUCCESS);
58 }
59 
60 static inline isc_result_t
61 totext_rp(ARGS_TOTEXT) {
62 	isc_region_t region;
63 	dns_name_t rmail;
64 	dns_name_t email;
65 	dns_name_t prefix;
66 	bool sub;
67 
68 	REQUIRE(rdata->type == dns_rdatatype_rp);
69 	REQUIRE(rdata->length != 0);
70 
71 	dns_name_init(&rmail, NULL);
72 	dns_name_init(&email, NULL);
73 	dns_name_init(&prefix, NULL);
74 
75 	dns_rdata_toregion(rdata, &region);
76 
77 	dns_name_fromregion(&rmail, &region);
78 	isc_region_consume(&region, rmail.length);
79 
80 	dns_name_fromregion(&email, &region);
81 	isc_region_consume(&region, email.length);
82 
83 	sub = name_prefix(&rmail, tctx->origin, &prefix);
84 	RETERR(dns_name_totext(&prefix, sub, target));
85 
86 	RETERR(str_totext(" ", target));
87 
88 	sub = name_prefix(&email, tctx->origin, &prefix);
89 	return (dns_name_totext(&prefix, sub, target));
90 }
91 
92 static inline isc_result_t
93 fromwire_rp(ARGS_FROMWIRE) {
94 	dns_name_t rmail;
95 	dns_name_t email;
96 
97 	REQUIRE(type == dns_rdatatype_rp);
98 
99 	UNUSED(type);
100 	UNUSED(rdclass);
101 
102 	dns_decompress_setmethods(dctx, DNS_COMPRESS_NONE);
103 
104 	dns_name_init(&rmail, NULL);
105 	dns_name_init(&email, NULL);
106 
107 	RETERR(dns_name_fromwire(&rmail, source, dctx, options, target));
108 	return (dns_name_fromwire(&email, source, dctx, options, target));
109 }
110 
111 static inline isc_result_t
112 towire_rp(ARGS_TOWIRE) {
113 	isc_region_t region;
114 	dns_name_t rmail;
115 	dns_name_t email;
116 	dns_offsets_t roffsets;
117 	dns_offsets_t eoffsets;
118 
119 	REQUIRE(rdata->type == dns_rdatatype_rp);
120 	REQUIRE(rdata->length != 0);
121 
122 	dns_compress_setmethods(cctx, DNS_COMPRESS_NONE);
123 	dns_name_init(&rmail, roffsets);
124 	dns_name_init(&email, eoffsets);
125 
126 	dns_rdata_toregion(rdata, &region);
127 
128 	dns_name_fromregion(&rmail, &region);
129 	isc_region_consume(&region, rmail.length);
130 
131 	RETERR(dns_name_towire(&rmail, cctx, target));
132 
133 	dns_name_fromregion(&rmail, &region);
134 	isc_region_consume(&region, rmail.length);
135 
136 	return (dns_name_towire(&rmail, cctx, target));
137 }
138 
139 static inline int
140 compare_rp(ARGS_COMPARE) {
141 	isc_region_t region1;
142 	isc_region_t region2;
143 	dns_name_t name1;
144 	dns_name_t name2;
145 	int order;
146 
147 	REQUIRE(rdata1->type == rdata2->type);
148 	REQUIRE(rdata1->rdclass == rdata2->rdclass);
149 	REQUIRE(rdata1->type == dns_rdatatype_rp);
150 	REQUIRE(rdata1->length != 0);
151 	REQUIRE(rdata2->length != 0);
152 
153 	dns_name_init(&name1, NULL);
154 	dns_name_init(&name2, NULL);
155 
156 	dns_rdata_toregion(rdata1, &region1);
157 	dns_rdata_toregion(rdata2, &region2);
158 
159 	dns_name_fromregion(&name1, &region1);
160 	dns_name_fromregion(&name2, &region2);
161 
162 	order = dns_name_rdatacompare(&name1, &name2);
163 	if (order != 0) {
164 		return (order);
165 	}
166 
167 	isc_region_consume(&region1, name_length(&name1));
168 	isc_region_consume(&region2, name_length(&name2));
169 
170 	dns_name_init(&name1, NULL);
171 	dns_name_init(&name2, NULL);
172 
173 	dns_name_fromregion(&name1, &region1);
174 	dns_name_fromregion(&name2, &region2);
175 
176 	return (dns_name_rdatacompare(&name1, &name2));
177 }
178 
179 static inline isc_result_t
180 fromstruct_rp(ARGS_FROMSTRUCT) {
181 	dns_rdata_rp_t *rp = source;
182 	isc_region_t region;
183 
184 	REQUIRE(type == dns_rdatatype_rp);
185 	REQUIRE(rp != NULL);
186 	REQUIRE(rp->common.rdtype == type);
187 	REQUIRE(rp->common.rdclass == rdclass);
188 
189 	UNUSED(type);
190 	UNUSED(rdclass);
191 
192 	dns_name_toregion(&rp->mail, &region);
193 	RETERR(isc_buffer_copyregion(target, &region));
194 	dns_name_toregion(&rp->text, &region);
195 	return (isc_buffer_copyregion(target, &region));
196 }
197 
198 static inline isc_result_t
199 tostruct_rp(ARGS_TOSTRUCT) {
200 	isc_result_t result;
201 	isc_region_t region;
202 	dns_rdata_rp_t *rp = target;
203 	dns_name_t name;
204 
205 	REQUIRE(rdata->type == dns_rdatatype_rp);
206 	REQUIRE(rp != NULL);
207 	REQUIRE(rdata->length != 0);
208 
209 	rp->common.rdclass = rdata->rdclass;
210 	rp->common.rdtype = rdata->type;
211 	ISC_LINK_INIT(&rp->common, link);
212 
213 	dns_name_init(&name, NULL);
214 	dns_rdata_toregion(rdata, &region);
215 	dns_name_fromregion(&name, &region);
216 	dns_name_init(&rp->mail, NULL);
217 	RETERR(name_duporclone(&name, mctx, &rp->mail));
218 	isc_region_consume(&region, name_length(&name));
219 	dns_name_fromregion(&name, &region);
220 	dns_name_init(&rp->text, NULL);
221 	result = name_duporclone(&name, mctx, &rp->text);
222 	if (result != ISC_R_SUCCESS) {
223 		goto cleanup;
224 	}
225 
226 	rp->mctx = mctx;
227 	return (ISC_R_SUCCESS);
228 
229 cleanup:
230 	if (mctx != NULL) {
231 		dns_name_free(&rp->mail, mctx);
232 	}
233 	return (ISC_R_NOMEMORY);
234 }
235 
236 static inline void
237 freestruct_rp(ARGS_FREESTRUCT) {
238 	dns_rdata_rp_t *rp = source;
239 
240 	REQUIRE(rp != NULL);
241 	REQUIRE(rp->common.rdtype == dns_rdatatype_rp);
242 
243 	if (rp->mctx == NULL) {
244 		return;
245 	}
246 
247 	dns_name_free(&rp->mail, rp->mctx);
248 	dns_name_free(&rp->text, rp->mctx);
249 	rp->mctx = NULL;
250 }
251 
252 static inline isc_result_t
253 additionaldata_rp(ARGS_ADDLDATA) {
254 	REQUIRE(rdata->type == dns_rdatatype_rp);
255 
256 	UNUSED(rdata);
257 	UNUSED(add);
258 	UNUSED(arg);
259 
260 	return (ISC_R_SUCCESS);
261 }
262 
263 static inline isc_result_t
264 digest_rp(ARGS_DIGEST) {
265 	isc_region_t r;
266 	dns_name_t name;
267 
268 	REQUIRE(rdata->type == dns_rdatatype_rp);
269 
270 	dns_rdata_toregion(rdata, &r);
271 	dns_name_init(&name, NULL);
272 
273 	dns_name_fromregion(&name, &r);
274 	RETERR(dns_name_digest(&name, digest, arg));
275 	isc_region_consume(&r, name_length(&name));
276 
277 	dns_name_init(&name, NULL);
278 	dns_name_fromregion(&name, &r);
279 
280 	return (dns_name_digest(&name, digest, arg));
281 }
282 
283 static inline bool
284 checkowner_rp(ARGS_CHECKOWNER) {
285 	REQUIRE(type == dns_rdatatype_rp);
286 
287 	UNUSED(name);
288 	UNUSED(type);
289 	UNUSED(rdclass);
290 	UNUSED(wildcard);
291 
292 	return (true);
293 }
294 
295 static inline bool
296 checknames_rp(ARGS_CHECKNAMES) {
297 	isc_region_t region;
298 	dns_name_t name;
299 
300 	REQUIRE(rdata->type == dns_rdatatype_rp);
301 
302 	UNUSED(owner);
303 
304 	dns_rdata_toregion(rdata, &region);
305 	dns_name_init(&name, NULL);
306 	dns_name_fromregion(&name, &region);
307 	if (!dns_name_ismailbox(&name)) {
308 		if (bad != NULL) {
309 			dns_name_clone(&name, bad);
310 		}
311 		return (false);
312 	}
313 	return (true);
314 }
315 
316 static inline int
317 casecompare_rp(ARGS_COMPARE) {
318 	return (compare_rp(rdata1, rdata2));
319 }
320 #endif /* RDATA_GENERIC_RP_17_C */
321