]> CyberLeo.Net >> Repos - FreeBSD/stable/9.git/blob - contrib/bind9/lib/dns/rdata/generic/opt_41.c
MFC r363988:
[FreeBSD/stable/9.git] / contrib / bind9 / lib / dns / rdata / generic / opt_41.c
1 /*
2  * Copyright (C) 2004, 2005, 2007, 2009, 2011-2015  Internet Systems Consortium, Inc. ("ISC")
3  * Copyright (C) 1998-2002  Internet Software Consortium.
4  *
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.
8  *
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.
16  */
17
18 /* $Id$ */
19
20 /* Reviewed: Thu Mar 16 14:06:44 PST 2000 by gson */
21
22 /* RFC2671 */
23
24 #ifndef RDATA_GENERIC_OPT_41_C
25 #define RDATA_GENERIC_OPT_41_C
26
27 #define RRTYPE_OPT_ATTRIBUTES (DNS_RDATATYPEATTR_SINGLETON | \
28                                DNS_RDATATYPEATTR_META | \
29                                DNS_RDATATYPEATTR_NOTQUESTION)
30
31 static inline isc_result_t
32 fromtext_opt(ARGS_FROMTEXT) {
33         /*
34          * OPT records do not have a text format.
35          */
36
37         REQUIRE(type == dns_rdatatype_opt);
38
39         UNUSED(type);
40         UNUSED(rdclass);
41         UNUSED(lexer);
42         UNUSED(origin);
43         UNUSED(options);
44         UNUSED(target);
45         UNUSED(callbacks);
46
47         return (ISC_R_NOTIMPLEMENTED);
48 }
49
50 static inline isc_result_t
51 totext_opt(ARGS_TOTEXT) {
52         isc_region_t r;
53         isc_region_t or;
54         isc_uint16_t option;
55         isc_uint16_t length;
56         char buf[sizeof("64000 64000")];
57
58         /*
59          * OPT records do not have a text format.
60          */
61
62         REQUIRE(rdata->type == dns_rdatatype_opt);
63
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);
73                 if (length > 0) {
74                         if ((tctx->flags & DNS_STYLEFLAG_MULTILINE) != 0)
75                                 RETERR(str_totext(" (", target));
76                         RETERR(str_totext(tctx->linebreak, target));
77                         or = r;
78                         or.length = length;
79                         if (tctx->width == 0)   /* No splitting */
80                                 RETERR(isc_base64_totext(&or, 60, "", target));
81                         else
82                                 RETERR(isc_base64_totext(&or, tctx->width - 2,
83                                                          tctx->linebreak,
84                                                          target));
85                         isc_region_consume(&r, length);
86                         if ((tctx->flags & DNS_STYLEFLAG_MULTILINE) != 0)
87                                 RETERR(str_totext(" )", target));
88                 }
89                 if (r.length > 0)
90                         RETERR(str_totext(" ", target));
91         }
92
93         return (ISC_R_SUCCESS);
94 }
95
96 static inline isc_result_t
97 fromwire_opt(ARGS_FROMWIRE) {
98         isc_region_t sregion;
99         isc_region_t tregion;
100         isc_uint16_t opt;
101         isc_uint16_t length;
102         unsigned int total;
103
104         REQUIRE(type == dns_rdatatype_opt);
105
106         UNUSED(type);
107         UNUSED(rdclass);
108         UNUSED(dctx);
109         UNUSED(options);
110
111         isc_buffer_activeregion(source, &sregion);
112         total = 0;
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);
120                 total += 4;
121                 if (sregion.length < length)
122                         return (ISC_R_UNEXPECTEDEND);
123                 switch (opt) {
124                 case DNS_OPT_CLIENT_SUBNET: {
125                         isc_uint16_t family;
126                         isc_uint8_t addrlen;
127                         isc_uint8_t scope;
128                         isc_uint8_t addrbytes;
129
130                         if (length < 4)
131                                 return (DNS_R_OPTERR);
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);
138                         switch (family) {
139                         case 1:
140                                 if (addrlen > 32U || scope > 32U)
141                                         return (DNS_R_OPTERR);
142                                 break;
143                         case 2:
144                                 if (addrlen > 128U || scope > 128U)
145                                         return (DNS_R_OPTERR);
146                                 break;
147                         default:
148                                 return (DNS_R_OPTERR);
149                         }
150                         addrbytes = (addrlen + 7) / 8;
151                         if (addrbytes + 4 != length)
152                                 return (DNS_R_OPTERR);
153
154                         if (addrbytes != 0U && (addrlen % 8) != 0) {
155                                 isc_uint8_t bits = ~0 << (8 - (addrlen % 8));
156                                 bits &= sregion.base[addrbytes - 1];
157                                 if (bits != sregion.base[addrbytes - 1])
158                                         return (DNS_R_OPTERR);
159                         }
160                         isc_region_consume(&sregion, addrbytes);
161                         break;
162                 }
163                 case DNS_OPT_EXPIRE:
164                         /*
165                          * Request has zero length.  Response is 32 bits.
166                          */
167                         if (length != 0 && length != 4)
168                                 return (DNS_R_OPTERR);
169                         isc_region_consume(&sregion, length);
170                         break;
171                 case DNS_OPT_COOKIE:
172                         if (length != 8 && (length < 16 || length > 40))
173                                 return (DNS_R_OPTERR);
174                         isc_region_consume(&sregion, length);
175                         break;
176                 default:
177                         isc_region_consume(&sregion, length);
178                         break;
179                 }
180                 total += length;
181         }
182
183         isc_buffer_activeregion(source, &sregion);
184         isc_buffer_availableregion(target, &tregion);
185         if (tregion.length < total)
186                 return (ISC_R_NOSPACE);
187         memmove(tregion.base, sregion.base, total);
188         isc_buffer_forward(source, total);
189         isc_buffer_add(target, total);
190
191         return (ISC_R_SUCCESS);
192 }
193
194 static inline isc_result_t
195 towire_opt(ARGS_TOWIRE) {
196
197         REQUIRE(rdata->type == dns_rdatatype_opt);
198
199         UNUSED(cctx);
200
201         return (mem_tobuffer(target, rdata->data, rdata->length));
202 }
203
204 static inline int
205 compare_opt(ARGS_COMPARE) {
206         isc_region_t r1;
207         isc_region_t r2;
208
209         REQUIRE(rdata1->type == rdata2->type);
210         REQUIRE(rdata1->rdclass == rdata2->rdclass);
211         REQUIRE(rdata1->type == dns_rdatatype_opt);
212
213         dns_rdata_toregion(rdata1, &r1);
214         dns_rdata_toregion(rdata2, &r2);
215         return (isc_region_compare(&r1, &r2));
216 }
217
218 static inline isc_result_t
219 fromstruct_opt(ARGS_FROMSTRUCT) {
220         dns_rdata_opt_t *opt = source;
221         isc_region_t region;
222         isc_uint16_t length;
223
224         REQUIRE(type == dns_rdatatype_opt);
225         REQUIRE(source != NULL);
226         REQUIRE(opt->common.rdtype == type);
227         REQUIRE(opt->common.rdclass == rdclass);
228         REQUIRE(opt->options != NULL || opt->length == 0);
229
230         UNUSED(type);
231         UNUSED(rdclass);
232
233         region.base = opt->options;
234         region.length = opt->length;
235         while (region.length >= 4) {
236                 isc_region_consume(&region, 2); /* opt */
237                 length = uint16_fromregion(&region);
238                 isc_region_consume(&region, 2);
239                 if (region.length < length)
240                         return (ISC_R_UNEXPECTEDEND);
241                 isc_region_consume(&region, length);
242         }
243         if (region.length != 0)
244                 return (ISC_R_UNEXPECTEDEND);
245
246         return (mem_tobuffer(target, opt->options, opt->length));
247 }
248
249 static inline isc_result_t
250 tostruct_opt(ARGS_TOSTRUCT) {
251         dns_rdata_opt_t *opt = target;
252         isc_region_t r;
253
254         REQUIRE(rdata->type == dns_rdatatype_opt);
255         REQUIRE(target != NULL);
256
257         opt->common.rdclass = rdata->rdclass;
258         opt->common.rdtype = rdata->type;
259         ISC_LINK_INIT(&opt->common, link);
260
261         dns_rdata_toregion(rdata, &r);
262         opt->length = r.length;
263         opt->options = mem_maybedup(mctx, r.base, r.length);
264         if (opt->options == NULL)
265                 return (ISC_R_NOMEMORY);
266
267         opt->offset = 0;
268         opt->mctx = mctx;
269         return (ISC_R_SUCCESS);
270 }
271
272 static inline void
273 freestruct_opt(ARGS_FREESTRUCT) {
274         dns_rdata_opt_t *opt = source;
275
276         REQUIRE(source != NULL);
277         REQUIRE(opt->common.rdtype == dns_rdatatype_opt);
278
279         if (opt->mctx == NULL)
280                 return;
281
282         if (opt->options != NULL)
283                 isc_mem_free(opt->mctx, opt->options);
284         opt->mctx = NULL;
285 }
286
287 static inline isc_result_t
288 additionaldata_opt(ARGS_ADDLDATA) {
289         REQUIRE(rdata->type == dns_rdatatype_opt);
290
291         UNUSED(rdata);
292         UNUSED(add);
293         UNUSED(arg);
294
295         return (ISC_R_SUCCESS);
296 }
297
298 static inline isc_result_t
299 digest_opt(ARGS_DIGEST) {
300
301         /*
302          * OPT records are not digested.
303          */
304
305         REQUIRE(rdata->type == dns_rdatatype_opt);
306
307         UNUSED(rdata);
308         UNUSED(digest);
309         UNUSED(arg);
310
311         return (ISC_R_NOTIMPLEMENTED);
312 }
313
314 static inline isc_boolean_t
315 checkowner_opt(ARGS_CHECKOWNER) {
316
317         REQUIRE(type == dns_rdatatype_opt);
318
319         UNUSED(type);
320         UNUSED(rdclass);
321         UNUSED(wildcard);
322
323         return (dns_name_equal(name, dns_rootname));
324 }
325
326 static inline isc_boolean_t
327 checknames_opt(ARGS_CHECKNAMES) {
328
329         REQUIRE(rdata->type == dns_rdatatype_opt);
330
331         UNUSED(rdata);
332         UNUSED(owner);
333         UNUSED(bad);
334
335         return (ISC_TRUE);
336 }
337
338 static inline int
339 casecompare_opt(ARGS_COMPARE) {
340         return (compare_opt(rdata1, rdata2));
341 }
342
343 isc_result_t
344 dns_rdata_opt_first(dns_rdata_opt_t *opt) {
345
346         REQUIRE(opt != NULL);
347         REQUIRE(opt->common.rdtype == dns_rdatatype_opt);
348         REQUIRE(opt->options != NULL || opt->length == 0);
349
350         if (opt->length == 0)
351                 return (ISC_R_NOMORE);
352
353         opt->offset = 0;
354         return (ISC_R_SUCCESS);
355 }
356
357 isc_result_t
358 dns_rdata_opt_next(dns_rdata_opt_t *opt) {
359         isc_region_t r;
360         isc_uint16_t length;
361
362         REQUIRE(opt != NULL);
363         REQUIRE(opt->common.rdtype == dns_rdatatype_opt);
364         REQUIRE(opt->options != NULL && opt->length != 0);
365         REQUIRE(opt->offset < opt->length);
366
367         INSIST(opt->offset + 4 <= opt->length);
368         r.base = opt->options + opt->offset + 2;
369         r.length = opt->length - opt->offset - 2;
370         length = uint16_fromregion(&r);
371         INSIST(opt->offset + 4 + length <= opt->length);
372         opt->offset = opt->offset + 4 + length;
373         if (opt->offset == opt->length)
374                 return (ISC_R_NOMORE);
375         return (ISC_R_SUCCESS);
376 }
377
378 isc_result_t
379 dns_rdata_opt_current(dns_rdata_opt_t *opt, dns_rdata_opt_opcode_t *opcode) {
380         isc_region_t r;
381
382         REQUIRE(opt != NULL);
383         REQUIRE(opcode != NULL);
384         REQUIRE(opt->common.rdtype == dns_rdatatype_opt);
385         REQUIRE(opt->options != NULL);
386         REQUIRE(opt->offset < opt->length);
387
388         INSIST(opt->offset + 4 <= opt->length);
389         r.base = opt->options + opt->offset;
390         r.length = opt->length - opt->offset;
391
392         opcode->opcode = uint16_fromregion(&r);
393         isc_region_consume(&r, 2);
394         opcode->length = uint16_fromregion(&r);
395         isc_region_consume(&r, 2);
396         opcode->data = r.base;
397         INSIST(opt->offset + 4 + opcode->length <= opt->length);
398
399         return (ISC_R_SUCCESS);
400 }
401
402 #endif  /* RDATA_GENERIC_OPT_41_C */