2 * Copyright (C) 2004, 2005, 2007, 2009, 2011-2014 Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (C) 1998-2002 Internet Software Consortium.
5 * Permission to use, copy, modify, and/or distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
10 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
12 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
14 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15 * PERFORMANCE OF THIS SOFTWARE.
20 /* Reviewed: Thu Mar 16 14:06:44 PST 2000 by gson */
24 #ifndef RDATA_GENERIC_OPT_41_C
25 #define RDATA_GENERIC_OPT_41_C
27 #define RRTYPE_OPT_ATTRIBUTES (DNS_RDATATYPEATTR_SINGLETON | \
28 DNS_RDATATYPEATTR_META | \
29 DNS_RDATATYPEATTR_NOTQUESTION)
31 static inline isc_result_t
32 fromtext_opt(ARGS_FROMTEXT) {
34 * OPT records do not have a text format.
47 return (ISC_R_NOTIMPLEMENTED);
50 static inline isc_result_t
51 totext_opt(ARGS_TOTEXT) {
56 char buf[sizeof("64000 64000")];
59 * OPT records do not have a text format.
62 REQUIRE(rdata->type == 41);
64 dns_rdata_toregion(rdata, &r);
65 while (r.length > 0) {
66 option = uint16_fromregion(&r);
67 isc_region_consume(&r, 2);
68 length = uint16_fromregion(&r);
69 isc_region_consume(&r, 2);
70 sprintf(buf, "%u %u", option, length);
71 RETERR(str_totext(buf, target));
72 INSIST(r.length >= length);
74 if ((tctx->flags & DNS_STYLEFLAG_MULTILINE) != 0)
75 RETERR(str_totext(" (", target));
76 RETERR(str_totext(tctx->linebreak, target));
79 if (tctx->width == 0) /* No splitting */
80 RETERR(isc_base64_totext(&or, 60, "", target));
82 RETERR(isc_base64_totext(&or, tctx->width - 2,
85 isc_region_consume(&r, length);
86 if ((tctx->flags & DNS_STYLEFLAG_MULTILINE) != 0)
87 RETERR(str_totext(" )", target));
90 RETERR(str_totext(" ", target));
93 return (ISC_R_SUCCESS);
96 static inline isc_result_t
97 fromwire_opt(ARGS_FROMWIRE) {
111 isc_buffer_activeregion(source, &sregion);
113 while (sregion.length != 0) {
114 if (sregion.length < 4)
115 return (ISC_R_UNEXPECTEDEND);
116 opt = uint16_fromregion(&sregion);
117 isc_region_consume(&sregion, 2);
118 length = uint16_fromregion(&sregion);
119 isc_region_consume(&sregion, 2);
121 if (sregion.length < length)
122 return (ISC_R_UNEXPECTEDEND);
124 case DNS_OPT_CLIENT_SUBNET: {
128 isc_uint8_t addrbytes;
131 return (DNS_R_FORMERR);
132 family = uint16_fromregion(&sregion);
133 isc_region_consume(&sregion, 2);
134 addrlen = uint8_fromregion(&sregion);
135 isc_region_consume(&sregion, 1);
136 scope = uint8_fromregion(&sregion);
137 isc_region_consume(&sregion, 1);
140 if (addrlen > 32U || scope > 32U)
141 return (DNS_R_FORMERR);
144 if (addrlen > 128U || scope > 128U)
145 return (DNS_R_FORMERR);
148 addrbytes = (addrlen + 7) / 8;
149 if (addrbytes + 4 != length)
150 return (DNS_R_FORMERR);
151 isc_region_consume(&sregion, addrbytes);
155 isc_region_consume(&sregion, length);
161 isc_buffer_activeregion(source, &sregion);
162 isc_buffer_availableregion(target, &tregion);
163 if (tregion.length < total)
164 return (ISC_R_NOSPACE);
165 memmove(tregion.base, sregion.base, total);
166 isc_buffer_forward(source, total);
167 isc_buffer_add(target, total);
169 return (ISC_R_SUCCESS);
172 static inline isc_result_t
173 towire_opt(ARGS_TOWIRE) {
175 REQUIRE(rdata->type == 41);
179 return (mem_tobuffer(target, rdata->data, rdata->length));
183 compare_opt(ARGS_COMPARE) {
187 REQUIRE(rdata1->type == rdata2->type);
188 REQUIRE(rdata1->rdclass == rdata2->rdclass);
189 REQUIRE(rdata1->type == 41);
191 dns_rdata_toregion(rdata1, &r1);
192 dns_rdata_toregion(rdata2, &r2);
193 return (isc_region_compare(&r1, &r2));
196 static inline isc_result_t
197 fromstruct_opt(ARGS_FROMSTRUCT) {
198 dns_rdata_opt_t *opt = source;
203 REQUIRE(source != NULL);
204 REQUIRE(opt->common.rdtype == type);
205 REQUIRE(opt->common.rdclass == rdclass);
206 REQUIRE(opt->options != NULL || opt->length == 0);
211 region.base = opt->options;
212 region.length = opt->length;
213 while (region.length >= 4) {
214 isc_region_consume(®ion, 2); /* opt */
215 length = uint16_fromregion(®ion);
216 isc_region_consume(®ion, 2);
217 if (region.length < length)
218 return (ISC_R_UNEXPECTEDEND);
219 isc_region_consume(®ion, length);
221 if (region.length != 0)
222 return (ISC_R_UNEXPECTEDEND);
224 return (mem_tobuffer(target, opt->options, opt->length));
227 static inline isc_result_t
228 tostruct_opt(ARGS_TOSTRUCT) {
229 dns_rdata_opt_t *opt = target;
232 REQUIRE(rdata->type == 41);
233 REQUIRE(target != NULL);
235 opt->common.rdclass = rdata->rdclass;
236 opt->common.rdtype = rdata->type;
237 ISC_LINK_INIT(&opt->common, link);
239 dns_rdata_toregion(rdata, &r);
240 opt->length = r.length;
241 opt->options = mem_maybedup(mctx, r.base, r.length);
242 if (opt->options == NULL)
243 return (ISC_R_NOMEMORY);
247 return (ISC_R_SUCCESS);
251 freestruct_opt(ARGS_FREESTRUCT) {
252 dns_rdata_opt_t *opt = source;
254 REQUIRE(source != NULL);
255 REQUIRE(opt->common.rdtype == 41);
257 if (opt->mctx == NULL)
260 if (opt->options != NULL)
261 isc_mem_free(opt->mctx, opt->options);
265 static inline isc_result_t
266 additionaldata_opt(ARGS_ADDLDATA) {
267 REQUIRE(rdata->type == 41);
273 return (ISC_R_SUCCESS);
276 static inline isc_result_t
277 digest_opt(ARGS_DIGEST) {
280 * OPT records are not digested.
283 REQUIRE(rdata->type == 41);
289 return (ISC_R_NOTIMPLEMENTED);
292 static inline isc_boolean_t
293 checkowner_opt(ARGS_CHECKOWNER) {
301 return (dns_name_equal(name, dns_rootname));
304 static inline isc_boolean_t
305 checknames_opt(ARGS_CHECKNAMES) {
307 REQUIRE(rdata->type == 41);
317 casecompare_opt(ARGS_COMPARE) {
318 return (compare_opt(rdata1, rdata2));
321 #endif /* RDATA_GENERIC_OPT_41_C */