opt_41.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2004, 2005, 2007, 2009, 2011-2014  Internet Systems Consortium, Inc. ("ISC")
00003  * Copyright (C) 1998-2002  Internet Software Consortium.
00004  *
00005  * Permission to use, copy, modify, and/or distribute this software for any
00006  * purpose with or without fee is hereby granted, provided that the above
00007  * copyright notice and this permission notice appear in all copies.
00008  *
00009  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
00010  * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
00011  * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
00012  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
00013  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
00014  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
00015  * PERFORMANCE OF THIS SOFTWARE.
00016  */
00017 
00018 /* $Id$ */
00019 
00020 /* Reviewed: Thu Mar 16 14:06:44 PST 2000 by gson */
00021 
00022 /* RFC2671 */
00023 
00024 #ifndef RDATA_GENERIC_OPT_41_C
00025 #define RDATA_GENERIC_OPT_41_C
00026 
00027 #define RRTYPE_OPT_ATTRIBUTES (DNS_RDATATYPEATTR_SINGLETON | \
00028                                DNS_RDATATYPEATTR_META | \
00029                                DNS_RDATATYPEATTR_NOTQUESTION)
00030 
00031 static inline isc_result_t
00032 fromtext_opt(ARGS_FROMTEXT) {
00033         /*
00034          * OPT records do not have a text format.
00035          */
00036 
00037         REQUIRE(type == 41);
00038 
00039         UNUSED(type);
00040         UNUSED(rdclass);
00041         UNUSED(lexer);
00042         UNUSED(origin);
00043         UNUSED(options);
00044         UNUSED(target);
00045         UNUSED(callbacks);
00046 
00047         return (ISC_R_NOTIMPLEMENTED);
00048 }
00049 
00050 static inline isc_result_t
00051 totext_opt(ARGS_TOTEXT) {
00052         isc_region_t r;
00053         isc_region_t or;
00054         isc_uint16_t option;
00055         isc_uint16_t length;
00056         char buf[sizeof("64000 64000")];
00057 
00058         /*
00059          * OPT records do not have a text format.
00060          */
00061 
00062         REQUIRE(rdata->type == 41);
00063 
00064         dns_rdata_toregion(rdata, &r);
00065         while (r.length > 0) {
00066                 option = uint16_fromregion(&r);
00067                 isc_region_consume(&r, 2);
00068                 length = uint16_fromregion(&r);
00069                 isc_region_consume(&r, 2);
00070                 sprintf(buf, "%u %u", option, length);
00071                 RETERR(str_totext(buf, target));
00072                 INSIST(r.length >= length);
00073                 if (length > 0) {
00074                         if ((tctx->flags & DNS_STYLEFLAG_MULTILINE) != 0)
00075                                 RETERR(str_totext(" (", target));
00076                         RETERR(str_totext(tctx->linebreak, target));
00077                         or = r;
00078                         or.length = length;
00079                         if (tctx->width == 0)   /* No splitting */
00080                                 RETERR(isc_base64_totext(&or, 60, "", target));
00081                         else
00082                                 RETERR(isc_base64_totext(&or, tctx->width - 2,
00083                                                          tctx->linebreak,
00084                                                          target));
00085                         isc_region_consume(&r, length);
00086                         if ((tctx->flags & DNS_STYLEFLAG_MULTILINE) != 0)
00087                                 RETERR(str_totext(" )", target));
00088                 }
00089                 if (r.length > 0)
00090                         RETERR(str_totext(" ", target));
00091         }
00092 
00093         return (ISC_R_SUCCESS);
00094 }
00095 
00096 static inline isc_result_t
00097 fromwire_opt(ARGS_FROMWIRE) {
00098         isc_region_t sregion;
00099         isc_region_t tregion;
00100         isc_uint16_t opt;
00101         isc_uint16_t length;
00102         unsigned int total;
00103 
00104         REQUIRE(type == 41);
00105 
00106         UNUSED(type);
00107         UNUSED(rdclass);
00108         UNUSED(dctx);
00109         UNUSED(options);
00110 
00111         isc_buffer_activeregion(source, &sregion);
00112         total = 0;
00113         while (sregion.length != 0) {
00114                 if (sregion.length < 4)
00115                         return (ISC_R_UNEXPECTEDEND);
00116                 opt = uint16_fromregion(&sregion);
00117                 isc_region_consume(&sregion, 2);
00118                 length = uint16_fromregion(&sregion);
00119                 isc_region_consume(&sregion, 2);
00120                 total += 4;
00121                 if (sregion.length < length)
00122                         return (ISC_R_UNEXPECTEDEND);
00123                 switch (opt) {
00124                 case DNS_OPT_CLIENT_SUBNET: {
00125                         isc_uint16_t family;
00126                         isc_uint8_t addrlen;
00127                         isc_uint8_t scope;
00128                         isc_uint8_t addrbytes;
00129 
00130                         if (length < 4)
00131                                 return (DNS_R_FORMERR);
00132                         family = uint16_fromregion(&sregion);
00133                         isc_region_consume(&sregion, 2);
00134                         addrlen = uint8_fromregion(&sregion);
00135                         isc_region_consume(&sregion, 1);
00136                         scope = uint8_fromregion(&sregion);
00137                         isc_region_consume(&sregion, 1);
00138                         switch (family) {
00139                         case 1:
00140                                 if (addrlen > 32U || scope > 32U)
00141                                         return (DNS_R_FORMERR);
00142                                 break;
00143                         case 2:
00144                                 if (addrlen > 128U || scope > 128U)
00145                                         return (DNS_R_FORMERR);
00146                                 break;
00147                         }
00148                         addrbytes = (addrlen + 7) / 8;
00149                         if (addrbytes + 4 != length)
00150                                 return (DNS_R_FORMERR);
00151                         isc_region_consume(&sregion, addrbytes);
00152                         break;
00153                 }
00154                 case DNS_OPT_EXPIRE:
00155                         /*
00156                          * Request has zero length.  Response is 32 bits.
00157                          */
00158                         if (length != 0 && length != 4)
00159                                 return (DNS_R_FORMERR);
00160                         isc_region_consume(&sregion, length);
00161                         break;
00162                 default:
00163                         isc_region_consume(&sregion, length);
00164                         break;
00165                 }
00166                 total += length;
00167         }
00168 
00169         isc_buffer_activeregion(source, &sregion);
00170         isc_buffer_availableregion(target, &tregion);
00171         if (tregion.length < total)
00172                 return (ISC_R_NOSPACE);
00173         memmove(tregion.base, sregion.base, total);
00174         isc_buffer_forward(source, total);
00175         isc_buffer_add(target, total);
00176 
00177         return (ISC_R_SUCCESS);
00178 }
00179 
00180 static inline isc_result_t
00181 towire_opt(ARGS_TOWIRE) {
00182 
00183         REQUIRE(rdata->type == 41);
00184 
00185         UNUSED(cctx);
00186 
00187         return (mem_tobuffer(target, rdata->data, rdata->length));
00188 }
00189 
00190 static inline int
00191 compare_opt(ARGS_COMPARE) {
00192         isc_region_t r1;
00193         isc_region_t r2;
00194 
00195         REQUIRE(rdata1->type == rdata2->type);
00196         REQUIRE(rdata1->rdclass == rdata2->rdclass);
00197         REQUIRE(rdata1->type == 41);
00198 
00199         dns_rdata_toregion(rdata1, &r1);
00200         dns_rdata_toregion(rdata2, &r2);
00201         return (isc_region_compare(&r1, &r2));
00202 }
00203 
00204 static inline isc_result_t
00205 fromstruct_opt(ARGS_FROMSTRUCT) {
00206         dns_rdata_opt_t *opt = source;
00207         isc_region_t region;
00208         isc_uint16_t length;
00209 
00210         REQUIRE(type == 41);
00211         REQUIRE(source != NULL);
00212         REQUIRE(opt->common.rdtype == type);
00213         REQUIRE(opt->common.rdclass == rdclass);
00214         REQUIRE(opt->options != NULL || opt->length == 0);
00215 
00216         UNUSED(type);
00217         UNUSED(rdclass);
00218 
00219         region.base = opt->options;
00220         region.length = opt->length;
00221         while (region.length >= 4) {
00222                 isc_region_consume(&region, 2); /* opt */
00223                 length = uint16_fromregion(&region);
00224                 isc_region_consume(&region, 2);
00225                 if (region.length < length)
00226                         return (ISC_R_UNEXPECTEDEND);
00227                 isc_region_consume(&region, length);
00228         }
00229         if (region.length != 0)
00230                 return (ISC_R_UNEXPECTEDEND);
00231 
00232         return (mem_tobuffer(target, opt->options, opt->length));
00233 }
00234 
00235 static inline isc_result_t
00236 tostruct_opt(ARGS_TOSTRUCT) {
00237         dns_rdata_opt_t *opt = target;
00238         isc_region_t r;
00239 
00240         REQUIRE(rdata->type == 41);
00241         REQUIRE(target != NULL);
00242 
00243         opt->common.rdclass = rdata->rdclass;
00244         opt->common.rdtype = rdata->type;
00245         ISC_LINK_INIT(&opt->common, link);
00246 
00247         dns_rdata_toregion(rdata, &r);
00248         opt->length = r.length;
00249         opt->options = mem_maybedup(mctx, r.base, r.length);
00250         if (opt->options == NULL)
00251                 return (ISC_R_NOMEMORY);
00252 
00253         opt->offset = 0;
00254         opt->mctx = mctx;
00255         return (ISC_R_SUCCESS);
00256 }
00257 
00258 static inline void
00259 freestruct_opt(ARGS_FREESTRUCT) {
00260         dns_rdata_opt_t *opt = source;
00261 
00262         REQUIRE(source != NULL);
00263         REQUIRE(opt->common.rdtype == 41);
00264 
00265         if (opt->mctx == NULL)
00266                 return;
00267 
00268         if (opt->options != NULL)
00269                 isc_mem_free(opt->mctx, opt->options);
00270         opt->mctx = NULL;
00271 }
00272 
00273 static inline isc_result_t
00274 additionaldata_opt(ARGS_ADDLDATA) {
00275         REQUIRE(rdata->type == 41);
00276 
00277         UNUSED(rdata);
00278         UNUSED(add);
00279         UNUSED(arg);
00280 
00281         return (ISC_R_SUCCESS);
00282 }
00283 
00284 static inline isc_result_t
00285 digest_opt(ARGS_DIGEST) {
00286 
00287         /*
00288          * OPT records are not digested.
00289          */
00290 
00291         REQUIRE(rdata->type == 41);
00292 
00293         UNUSED(rdata);
00294         UNUSED(digest);
00295         UNUSED(arg);
00296 
00297         return (ISC_R_NOTIMPLEMENTED);
00298 }
00299 
00300 static inline isc_boolean_t
00301 checkowner_opt(ARGS_CHECKOWNER) {
00302 
00303         REQUIRE(type == 41);
00304 
00305         UNUSED(type);
00306         UNUSED(rdclass);
00307         UNUSED(wildcard);
00308 
00309         return (dns_name_equal(name, dns_rootname));
00310 }
00311 
00312 static inline isc_boolean_t
00313 checknames_opt(ARGS_CHECKNAMES) {
00314 
00315         REQUIRE(rdata->type == 41);
00316 
00317         UNUSED(rdata);
00318         UNUSED(owner);
00319         UNUSED(bad);
00320 
00321         return (ISC_TRUE);
00322 }
00323 
00324 static inline int
00325 casecompare_opt(ARGS_COMPARE) {
00326         return (compare_opt(rdata1, rdata2));
00327 }
00328 
00329 isc_result_t
00330 dns_rdata_opt_first(dns_rdata_opt_t *opt) {
00331 
00332         REQUIRE(opt != NULL);
00333         REQUIRE(opt->common.rdtype == 41);
00334         REQUIRE(opt->options != NULL || opt->length == 0);
00335 
00336         if (opt->length == 0)
00337                 return (ISC_R_NOMORE);
00338 
00339         opt->offset = 0;
00340         return (ISC_R_SUCCESS);
00341 }
00342 
00343 isc_result_t
00344 dns_rdata_opt_next(dns_rdata_opt_t *opt) {
00345         isc_region_t r;
00346         isc_uint16_t length;
00347 
00348         REQUIRE(opt != NULL);
00349         REQUIRE(opt->common.rdtype == 41);
00350         REQUIRE(opt->options != NULL && opt->length != 0);
00351         REQUIRE(opt->offset < opt->length);
00352 
00353         INSIST(opt->offset + 4 <= opt->length);
00354         r.base = opt->options + opt->offset + 2;
00355         r.length = opt->length - opt->offset - 2;
00356         length = uint16_fromregion(&r);
00357         INSIST(opt->offset + 4 + length <= opt->length);
00358         opt->offset = opt->offset + 4 + length;
00359         if (opt->offset == opt->length)
00360                 return (ISC_R_NOMORE);
00361         return (ISC_R_SUCCESS);
00362 }
00363 
00364 isc_result_t
00365 dns_rdata_opt_current(dns_rdata_opt_t *opt, dns_rdata_opt_opcode_t *opcode) {
00366         isc_region_t r;
00367 
00368         REQUIRE(opt != NULL);
00369         REQUIRE(opcode != NULL);
00370         REQUIRE(opt->common.rdtype == 41);
00371         REQUIRE(opt->options != NULL);
00372         REQUIRE(opt->offset < opt->length);
00373 
00374         INSIST(opt->offset + 4 <= opt->length);
00375         r.base = opt->options + opt->offset;
00376         r.length = opt->length - opt->offset;
00377 
00378         opcode->opcode = uint16_fromregion(&r);
00379         isc_region_consume(&r, 2);
00380         opcode->length = uint16_fromregion(&r);
00381         isc_region_consume(&r, 2);
00382         opcode->data = r.base;
00383         INSIST(opt->offset + 4 + opcode->length <= opt->length);
00384 
00385         return (ISC_R_SUCCESS);
00386 }
00387 
00388 #endif  /* RDATA_GENERIC_OPT_41_C */

Generated on Tue Apr 28 17:41:00 2015 by Doxygen 1.5.4 for BIND9 Internals 9.11.0pre-alpha