2 * Copyright (C) 2011-2013 Internet Systems Consortium, Inc. ("ISC")
4 * Permission to use, copy, modify, and/or distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
9 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
10 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
11 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
12 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
13 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
14 * PERFORMANCE OF THIS SOFTWARE.
17 /* $Id: uri_256.c,v 1.2 2011/03/03 14:10:27 fdupont Exp $ */
19 #ifndef GENERIC_URI_256_C
20 #define GENERIC_URI_256_C 1
22 #define RRTYPE_URI_ATTRIBUTES (0)
24 static inline isc_result_t
25 fromtext_uri(ARGS_FROMTEXT) {
39 RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_number,
41 if (token.value.as_ulong > 0xffffU)
43 RETERR(uint16_tobuffer(token.value.as_ulong, target));
48 RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_number,
50 if (token.value.as_ulong > 0xffffU)
52 RETERR(uint16_tobuffer(token.value.as_ulong, target));
57 RETERR(isc_lex_getmastertoken(lexer, &token,
58 isc_tokentype_qstring, ISC_FALSE));
59 if (token.type != isc_tokentype_qstring)
61 RETTOK(multitxt_fromtext(&token.value.as_textregion, target));
62 return (ISC_R_SUCCESS);
65 static inline isc_result_t
66 totext_uri(ARGS_TOTEXT) {
68 unsigned short priority, weight;
69 char buf[sizeof("65000 ")];
73 REQUIRE(rdata->type == 256);
74 REQUIRE(rdata->length != 0);
76 dns_rdata_toregion(rdata, ®ion);
81 priority = uint16_fromregion(®ion);
82 isc_region_consume(®ion, 2);
83 sprintf(buf, "%u ", priority);
84 RETERR(str_totext(buf, target));
89 weight = uint16_fromregion(®ion);
90 isc_region_consume(®ion, 2);
91 sprintf(buf, "%u ", weight);
92 RETERR(str_totext(buf, target));
97 RETERR(multitxt_totext(®ion, target));
98 return (ISC_R_SUCCESS);
101 static inline isc_result_t
102 fromwire_uri(ARGS_FROMWIRE) {
105 REQUIRE(type == 256);
115 isc_buffer_activeregion(source, ®ion);
116 if (region.length < 4)
117 return (ISC_R_UNEXPECTEDEND);
118 RETERR(mem_tobuffer(target, region.base, 4));
119 isc_buffer_forward(source, 4);
124 RETERR(multitxt_fromwire(source, target));
126 return (ISC_R_SUCCESS);
129 static inline isc_result_t
130 towire_uri(ARGS_TOWIRE) {
133 REQUIRE(rdata->type == 256);
134 REQUIRE(rdata->length != 0);
138 dns_rdata_toregion(rdata, ®ion);
139 return (mem_tobuffer(target, region.base, region.length));
143 compare_uri(ARGS_COMPARE) {
148 REQUIRE(rdata1->type == rdata2->type);
149 REQUIRE(rdata1->rdclass == rdata2->rdclass);
150 REQUIRE(rdata1->type == 256);
151 REQUIRE(rdata1->length != 0);
152 REQUIRE(rdata2->length != 0);
154 dns_rdata_toregion(rdata1, &r1);
155 dns_rdata_toregion(rdata2, &r2);
160 order = memcmp(r1.base, r2.base, 2);
162 return (order < 0 ? -1 : 1);
163 isc_region_consume(&r1, 2);
164 isc_region_consume(&r2, 2);
169 order = memcmp(r1.base, r2.base, 2);
171 return (order < 0 ? -1 : 1);
172 isc_region_consume(&r1, 2);
173 isc_region_consume(&r2, 2);
175 return (isc_region_compare(&r1, &r2));
178 static inline isc_result_t
179 fromstruct_uri(ARGS_FROMSTRUCT) {
180 dns_rdata_uri_t *uri = source;
184 REQUIRE(type == 256);
185 REQUIRE(source != NULL);
186 REQUIRE(uri->common.rdtype == type);
187 REQUIRE(uri->common.rdclass == rdclass);
188 REQUIRE(uri->target != NULL && uri->tgt_len != 0);
196 RETERR(uint16_tobuffer(uri->priority, target));
201 RETERR(uint16_tobuffer(uri->weight, target));
207 region.base = uri->target;
208 region.length = uri->tgt_len;
209 while (region.length > 0) {
210 REQUIRE(len == 255U);
211 len = uint8_fromregion(®ion);
212 isc_region_consume(®ion, 1);
213 if (region.length < len)
214 return (ISC_R_UNEXPECTEDEND);
215 isc_region_consume(®ion, len);
218 return (mem_tobuffer(target, uri->target, uri->tgt_len));
221 static inline isc_result_t
222 tostruct_uri(ARGS_TOSTRUCT) {
223 dns_rdata_uri_t *uri = target;
226 REQUIRE(rdata->type == 256);
227 REQUIRE(target != NULL);
228 REQUIRE(rdata->length != 0);
230 uri->common.rdclass = rdata->rdclass;
231 uri->common.rdtype = rdata->type;
232 ISC_LINK_INIT(&uri->common, link);
234 dns_rdata_toregion(rdata, &sr);
240 return (ISC_R_UNEXPECTEDEND);
241 uri->priority = uint16_fromregion(&sr);
242 isc_region_consume(&sr, 2);
248 return (ISC_R_UNEXPECTEDEND);
249 uri->weight = uint16_fromregion(&sr);
250 isc_region_consume(&sr, 2);
255 uri->tgt_len = sr.length;
256 uri->target = mem_maybedup(mctx, sr.base, sr.length);
257 if (uri->target == NULL)
258 return (ISC_R_NOMEMORY);
261 return (ISC_R_SUCCESS);
265 freestruct_uri(ARGS_FREESTRUCT) {
266 dns_rdata_uri_t *uri = (dns_rdata_uri_t *) source;
268 REQUIRE(source != NULL);
269 REQUIRE(uri->common.rdtype == 256);
271 if (uri->mctx == NULL)
274 if (uri->target != NULL)
275 isc_mem_free(uri->mctx, uri->target);
279 static inline isc_result_t
280 additionaldata_uri(ARGS_ADDLDATA) {
281 REQUIRE(rdata->type == 256);
287 return (ISC_R_SUCCESS);
290 static inline isc_result_t
291 digest_uri(ARGS_DIGEST) {
294 REQUIRE(rdata->type == 256);
296 dns_rdata_toregion(rdata, &r);
298 return ((digest)(arg, &r));
301 static inline isc_boolean_t
302 checkowner_uri(ARGS_CHECKOWNER) {
304 REQUIRE(type == 256);
314 static inline isc_boolean_t
315 checknames_uri(ARGS_CHECKNAMES) {
317 REQUIRE(rdata->type == 256);
327 casecompare_uri(ARGS_COMPARE) {
328 return (compare_uri(rdata1, rdata2));
331 #endif /* GENERIC_URI_256_C */