4 * Copyright (C) 2004, 2005, 2007, 2009 Internet Systems Consortium, Inc. ("ISC")
5 * Copyright (C) 1998-2002 Internet Software Consortium.
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.
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.
20 /* Id: opt_41.c,v 1.35 2009/12/04 22:06:37 tbox Exp */
22 /* Reviewed: Thu Mar 16 14:06:44 PST 2000 by gson */
26 #ifndef RDATA_GENERIC_OPT_41_C
27 #define RDATA_GENERIC_OPT_41_C
29 #define RRTYPE_OPT_ATTRIBUTES (DNS_RDATATYPEATTR_SINGLETON | \
30 DNS_RDATATYPEATTR_META | \
31 DNS_RDATATYPEATTR_NOTQUESTION)
33 static inline isc_result_t
34 fromtext_opt(ARGS_FROMTEXT
) {
36 * OPT records do not have a text format.
49 return (ISC_R_NOTIMPLEMENTED
);
52 static inline isc_result_t
53 totext_opt(ARGS_TOTEXT
) {
58 char buf
[sizeof("64000 64000")];
61 * OPT records do not have a text format.
64 REQUIRE(rdata
->type
== 41);
66 dns_rdata_toregion(rdata
, &r
);
67 while (r
.length
> 0) {
68 option
= uint16_fromregion(&r
);
69 isc_region_consume(&r
, 2);
70 length
= uint16_fromregion(&r
);
71 isc_region_consume(&r
, 2);
72 sprintf(buf
, "%u %u", option
, length
);
73 RETERR(str_totext(buf
, target
));
74 INSIST(r
.length
>= length
);
76 if ((tctx
->flags
& DNS_STYLEFLAG_MULTILINE
) != 0)
77 RETERR(str_totext(" (", target
));
78 RETERR(str_totext(tctx
->linebreak
, target
));
81 RETERR(isc_base64_totext(&or, tctx
->width
- 2,
82 tctx
->linebreak
, target
));
83 isc_region_consume(&r
, length
);
84 if ((tctx
->flags
& DNS_STYLEFLAG_MULTILINE
) != 0)
85 RETERR(str_totext(" )", target
));
88 RETERR(str_totext(" ", target
));
91 return (ISC_R_SUCCESS
);
94 static inline isc_result_t
95 fromwire_opt(ARGS_FROMWIRE
) {
108 isc_buffer_activeregion(source
, &sregion
);
110 while (sregion
.length
!= 0) {
111 if (sregion
.length
< 4)
112 return (ISC_R_UNEXPECTEDEND
);
114 * Eat the 16bit option code. There is nothing to
115 * be done with it currently.
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
);
123 isc_region_consume(&sregion
, length
);
127 isc_buffer_activeregion(source
, &sregion
);
128 isc_buffer_availableregion(target
, &tregion
);
129 if (tregion
.length
< total
)
130 return (ISC_R_NOSPACE
);
131 memcpy(tregion
.base
, sregion
.base
, total
);
132 isc_buffer_forward(source
, total
);
133 isc_buffer_add(target
, total
);
135 return (ISC_R_SUCCESS
);
138 static inline isc_result_t
139 towire_opt(ARGS_TOWIRE
) {
141 REQUIRE(rdata
->type
== 41);
145 return (mem_tobuffer(target
, rdata
->data
, rdata
->length
));
149 compare_opt(ARGS_COMPARE
) {
153 REQUIRE(rdata1
->type
== rdata2
->type
);
154 REQUIRE(rdata1
->rdclass
== rdata2
->rdclass
);
155 REQUIRE(rdata1
->type
== 41);
157 dns_rdata_toregion(rdata1
, &r1
);
158 dns_rdata_toregion(rdata2
, &r2
);
159 return (isc_region_compare(&r1
, &r2
));
162 static inline isc_result_t
163 fromstruct_opt(ARGS_FROMSTRUCT
) {
164 dns_rdata_opt_t
*opt
= source
;
169 REQUIRE(source
!= NULL
);
170 REQUIRE(opt
->common
.rdtype
== type
);
171 REQUIRE(opt
->common
.rdclass
== rdclass
);
172 REQUIRE(opt
->options
!= NULL
|| opt
->length
== 0);
177 region
.base
= opt
->options
;
178 region
.length
= opt
->length
;
179 while (region
.length
>= 4) {
180 isc_region_consume(®ion
, 2); /* opt */
181 length
= uint16_fromregion(®ion
);
182 isc_region_consume(®ion
, 2);
183 if (region
.length
< length
)
184 return (ISC_R_UNEXPECTEDEND
);
185 isc_region_consume(®ion
, length
);
187 if (region
.length
!= 0)
188 return (ISC_R_UNEXPECTEDEND
);
190 return (mem_tobuffer(target
, opt
->options
, opt
->length
));
193 static inline isc_result_t
194 tostruct_opt(ARGS_TOSTRUCT
) {
195 dns_rdata_opt_t
*opt
= target
;
198 REQUIRE(rdata
->type
== 41);
199 REQUIRE(target
!= NULL
);
201 opt
->common
.rdclass
= rdata
->rdclass
;
202 opt
->common
.rdtype
= rdata
->type
;
203 ISC_LINK_INIT(&opt
->common
, link
);
205 dns_rdata_toregion(rdata
, &r
);
206 opt
->length
= r
.length
;
207 opt
->options
= mem_maybedup(mctx
, r
.base
, r
.length
);
208 if (opt
->options
== NULL
)
209 return (ISC_R_NOMEMORY
);
213 return (ISC_R_SUCCESS
);
217 freestruct_opt(ARGS_FREESTRUCT
) {
218 dns_rdata_opt_t
*opt
= source
;
220 REQUIRE(source
!= NULL
);
221 REQUIRE(opt
->common
.rdtype
== 41);
223 if (opt
->mctx
== NULL
)
226 if (opt
->options
!= NULL
)
227 isc_mem_free(opt
->mctx
, opt
->options
);
231 static inline isc_result_t
232 additionaldata_opt(ARGS_ADDLDATA
) {
233 REQUIRE(rdata
->type
== 41);
239 return (ISC_R_SUCCESS
);
242 static inline isc_result_t
243 digest_opt(ARGS_DIGEST
) {
246 * OPT records are not digested.
249 REQUIRE(rdata
->type
== 41);
255 return (ISC_R_NOTIMPLEMENTED
);
258 static inline isc_boolean_t
259 checkowner_opt(ARGS_CHECKOWNER
) {
267 return (dns_name_equal(name
, dns_rootname
));
270 static inline isc_boolean_t
271 checknames_opt(ARGS_CHECKNAMES
) {
273 REQUIRE(rdata
->type
== 41);
283 casecompare_opt(ARGS_COMPARE
) {
284 return (compare_opt(rdata1
, rdata2
));
287 #endif /* RDATA_GENERIC_OPT_41_C */