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