1 /*	$NetBSD: uri_256.c,v 1.1.1.5 2014/12/10 03:34:42 christos Exp $	*/
2 
3 /*
4  * Copyright (C) 2011, 2012, 2014  Internet Systems Consortium, Inc. ("ISC")
5  *
6  * Permission to use, copy, modify, and/or distribute this software for any
7  * purpose with or without fee is hereby granted, provided that the above
8  * copyright notice and this permission notice appear in all copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
11  * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
12  * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
13  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
14  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
15  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
16  * PERFORMANCE OF THIS SOFTWARE.
17  */
18 
19 /* Id */
20 
21 #ifndef GENERIC_URI_256_C
22 #define GENERIC_URI_256_C 1
23 
24 #define RRTYPE_URI_ATTRIBUTES (0)
25 
26 static inline isc_result_t
27 fromtext_uri(ARGS_FROMTEXT) {
28 	isc_token_t token;
29 
30 	REQUIRE(type == 256);
31 
32 	UNUSED(type);
33 	UNUSED(rdclass);
34 	UNUSED(origin);
35 	UNUSED(options);
36 	UNUSED(callbacks);
37 
38 	/*
39 	 * Priority
40 	 */
41 	RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_number,
42 				      ISC_FALSE));
43 	if (token.value.as_ulong > 0xffffU)
44 		RETTOK(ISC_R_RANGE);
45 	RETERR(uint16_tobuffer(token.value.as_ulong, target));
46 
47 	/*
48 	 * Weight
49 	 */
50 	RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_number,
51 				      ISC_FALSE));
52 	if (token.value.as_ulong > 0xffffU)
53 		RETTOK(ISC_R_RANGE);
54 	RETERR(uint16_tobuffer(token.value.as_ulong, target));
55 
56 	/*
57 	 * Target URI
58 	 */
59 	RETERR(isc_lex_getmastertoken(lexer, &token,
60 				      isc_tokentype_qstring, ISC_FALSE));
61 	if (token.type != isc_tokentype_qstring)
62 		RETTOK(DNS_R_SYNTAX);
63 	RETTOK(multitxt_fromtext(&token.value.as_textregion, target));
64 	return (ISC_R_SUCCESS);
65 }
66 
67 static inline isc_result_t
68 totext_uri(ARGS_TOTEXT) {
69 	isc_region_t region;
70 	unsigned short priority, weight;
71 	char buf[sizeof("65000 ")];
72 
73 	UNUSED(tctx);
74 
75 	REQUIRE(rdata->type == 256);
76 	REQUIRE(rdata->length != 0);
77 
78 	dns_rdata_toregion(rdata, &region);
79 
80 	/*
81 	 * Priority
82 	 */
83 	priority = uint16_fromregion(&region);
84 	isc_region_consume(&region, 2);
85 	sprintf(buf, "%u ", priority);
86 	RETERR(str_totext(buf, target));
87 
88 	/*
89 	 * Weight
90 	 */
91 	weight = uint16_fromregion(&region);
92 	isc_region_consume(&region, 2);
93 	sprintf(buf, "%u ", weight);
94 	RETERR(str_totext(buf, target));
95 
96 	/*
97 	 * Target URI
98 	 */
99 	RETERR(multitxt_totext(&region, target));
100 	return (ISC_R_SUCCESS);
101 }
102 
103 static inline isc_result_t
104 fromwire_uri(ARGS_FROMWIRE) {
105 	isc_region_t region;
106 
107 	REQUIRE(type == 256);
108 
109 	UNUSED(type);
110 	UNUSED(rdclass);
111 	UNUSED(dctx);
112 	UNUSED(options);
113 
114 	/*
115 	 * Priority, weight
116 	 */
117 	isc_buffer_activeregion(source, &region);
118 	if (region.length < 4)
119 		return (ISC_R_UNEXPECTEDEND);
120 
121 	/*
122 	 * Priority, weight and target URI
123 	 */
124 	isc_buffer_forward(source, region.length);
125 	return (mem_tobuffer(target, region.base, region.length));
126 }
127 
128 static inline isc_result_t
129 towire_uri(ARGS_TOWIRE) {
130 	isc_region_t region;
131 
132 	REQUIRE(rdata->type == 256);
133 	REQUIRE(rdata->length != 0);
134 
135 	UNUSED(cctx);
136 
137 	dns_rdata_toregion(rdata, &region);
138 	return (mem_tobuffer(target, region.base, region.length));
139 }
140 
141 static inline int
142 compare_uri(ARGS_COMPARE) {
143 	isc_region_t r1;
144 	isc_region_t r2;
145 	int order;
146 
147 	REQUIRE(rdata1->type == rdata2->type);
148 	REQUIRE(rdata1->rdclass == rdata2->rdclass);
149 	REQUIRE(rdata1->type == 256);
150 	REQUIRE(rdata1->length != 0);
151 	REQUIRE(rdata2->length != 0);
152 
153 	dns_rdata_toregion(rdata1, &r1);
154 	dns_rdata_toregion(rdata2, &r2);
155 
156 	/*
157 	 * Priority
158 	 */
159 	order = memcmp(r1.base, r2.base, 2);
160 	if (order != 0)
161 		return (order < 0 ? -1 : 1);
162 	isc_region_consume(&r1, 2);
163 	isc_region_consume(&r2, 2);
164 
165 	/*
166 	 * Weight
167 	 */
168 	order = memcmp(r1.base, r2.base, 2);
169 	if (order != 0)
170 		return (order < 0 ? -1 : 1);
171 	isc_region_consume(&r1, 2);
172 	isc_region_consume(&r2, 2);
173 
174 	return (isc_region_compare(&r1, &r2));
175 }
176 
177 static inline isc_result_t
178 fromstruct_uri(ARGS_FROMSTRUCT) {
179 	dns_rdata_uri_t *uri = source;
180 
181 	REQUIRE(type == 256);
182 	REQUIRE(source != NULL);
183 	REQUIRE(uri->common.rdtype == type);
184 	REQUIRE(uri->common.rdclass == rdclass);
185 	REQUIRE(uri->target != NULL && uri->tgt_len != 0);
186 
187 	UNUSED(type);
188 	UNUSED(rdclass);
189 
190 	/*
191 	 * Priority
192 	 */
193 	RETERR(uint16_tobuffer(uri->priority, target));
194 
195 	/*
196 	 * Weight
197 	 */
198 	RETERR(uint16_tobuffer(uri->weight, target));
199 
200 	/*
201 	 * Target URI
202 	 */
203 	return (mem_tobuffer(target, uri->target, uri->tgt_len));
204 }
205 
206 static inline isc_result_t
207 tostruct_uri(ARGS_TOSTRUCT) {
208 	dns_rdata_uri_t *uri = target;
209 	isc_region_t sr;
210 
211 	REQUIRE(rdata->type == 256);
212 	REQUIRE(target != NULL);
213 	REQUIRE(rdata->length != 0);
214 
215 	uri->common.rdclass = rdata->rdclass;
216 	uri->common.rdtype = rdata->type;
217 	ISC_LINK_INIT(&uri->common, link);
218 
219 	dns_rdata_toregion(rdata, &sr);
220 
221 	/*
222 	 * Priority
223 	 */
224 	if (sr.length < 2)
225 		return (ISC_R_UNEXPECTEDEND);
226 	uri->priority = uint16_fromregion(&sr);
227 	isc_region_consume(&sr, 2);
228 
229 	/*
230 	 * Weight
231 	 */
232 	if (sr.length < 2)
233 		return (ISC_R_UNEXPECTEDEND);
234 	uri->weight = uint16_fromregion(&sr);
235 	isc_region_consume(&sr, 2);
236 
237 	/*
238 	 * Target URI
239 	 */
240 	uri->tgt_len = sr.length;
241 	uri->target = mem_maybedup(mctx, sr.base, sr.length);
242 	if (uri->target == NULL)
243 		return (ISC_R_NOMEMORY);
244 
245 	uri->mctx = mctx;
246 	return (ISC_R_SUCCESS);
247 }
248 
249 static inline void
250 freestruct_uri(ARGS_FREESTRUCT) {
251 	dns_rdata_uri_t *uri = (dns_rdata_uri_t *) source;
252 
253 	REQUIRE(source != NULL);
254 	REQUIRE(uri->common.rdtype == 256);
255 
256 	if (uri->mctx == NULL)
257 		return;
258 
259 	if (uri->target != NULL)
260 		isc_mem_free(uri->mctx, uri->target);
261 	uri->mctx = NULL;
262 }
263 
264 static inline isc_result_t
265 additionaldata_uri(ARGS_ADDLDATA) {
266 	REQUIRE(rdata->type == 256);
267 
268 	UNUSED(rdata);
269 	UNUSED(add);
270 	UNUSED(arg);
271 
272 	return (ISC_R_SUCCESS);
273 }
274 
275 static inline isc_result_t
276 digest_uri(ARGS_DIGEST) {
277 	isc_region_t r;
278 
279 	REQUIRE(rdata->type == 256);
280 
281 	dns_rdata_toregion(rdata, &r);
282 
283 	return ((digest)(arg, &r));
284 }
285 
286 static inline isc_boolean_t
287 checkowner_uri(ARGS_CHECKOWNER) {
288 
289 	REQUIRE(type == 256);
290 
291 	UNUSED(name);
292 	UNUSED(type);
293 	UNUSED(rdclass);
294 	UNUSED(wildcard);
295 
296 	return (ISC_TRUE);
297 }
298 
299 static inline isc_boolean_t
300 checknames_uri(ARGS_CHECKNAMES) {
301 
302 	REQUIRE(rdata->type == 256);
303 
304 	UNUSED(rdata);
305 	UNUSED(owner);
306 	UNUSED(bad);
307 
308 	return (ISC_TRUE);
309 }
310 
311 static inline int
312 casecompare_uri(ARGS_COMPARE) {
313 	return (compare_uri(rdata1, rdata2));
314 }
315 
316 #endif /* GENERIC_URI_256_C */
317