"Fossies" - the Fresh Open Source Software Archive

Member "bind-9.11.23/lib/dns/rdata/generic/amtrelay_260.c" (7 Sep 2020, 10649 Bytes) of package /linux/misc/dns/bind9/9.11.23/bind-9.11.23.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "amtrelay_260.c" see the Fossies "Dox" file reference documentation and the last Fossies "Diffs" side-by-side code changes report: 9.17.2_vs_9.17.3.

    1 /*
    2  * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
    3  *
    4  * This Source Code Form is subject to the terms of the Mozilla Public
    5  * License, v. 2.0. If a copy of the MPL was not distributed with this
    6  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
    7  *
    8  * See the COPYRIGHT file distributed with this work for additional
    9  * information regarding copyright ownership.
   10  */
   11 
   12 
   13 #ifndef RDATA_GENERIC_AMTRELAY_260_C
   14 #define RDATA_GENERIC_AMTRELAY_260_C
   15 
   16 #include <string.h>
   17 
   18 #include <isc/net.h>
   19 
   20 #define RRTYPE_AMTRELAY_ATTRIBUTES (0)
   21 
   22 static inline isc_result_t
   23 fromtext_amtrelay(ARGS_FROMTEXT) {
   24     isc_token_t token;
   25     dns_name_t name;
   26     isc_buffer_t buffer;
   27     unsigned int discovery;
   28     unsigned int gateway;
   29     struct in_addr addr;
   30     unsigned char addr6[16];
   31     isc_region_t region;
   32 
   33     REQUIRE(type == dns_rdatatype_amtrelay);
   34 
   35     UNUSED(type);
   36     UNUSED(rdclass);
   37     UNUSED(callbacks);
   38 
   39     /*
   40      * Precedence.
   41      */
   42     RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_number,
   43                       false));
   44     if (token.value.as_ulong > 0xffU) {
   45         RETTOK(ISC_R_RANGE);
   46     }
   47     RETERR(uint8_tobuffer(token.value.as_ulong, target));
   48 
   49     /*
   50      * Discovery.
   51      */
   52     RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_number,
   53                       false));
   54     if (token.value.as_ulong > 1U) {
   55         RETTOK(ISC_R_RANGE);
   56     }
   57     discovery = token.value.as_ulong;
   58 
   59     /*
   60      * Gateway type.
   61      */
   62     RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_number,
   63                       false));
   64     if (token.value.as_ulong > 0x7fU)
   65         RETTOK(ISC_R_RANGE);
   66     RETERR(uint8_tobuffer(token.value.as_ulong | (discovery << 7), target));
   67     gateway = token.value.as_ulong;
   68 
   69     if (gateway == 0) {
   70         return (ISC_R_SUCCESS);
   71     }
   72 
   73     if (gateway > 3) {
   74         return (ISC_R_NOTIMPLEMENTED);
   75     }
   76 
   77     /*
   78      * Gateway.
   79      */
   80     RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
   81                       false));
   82 
   83     switch (gateway) {
   84     case 1:
   85         if (inet_pton(AF_INET, DNS_AS_STR(token), &addr) != 1) {
   86             RETTOK(DNS_R_BADDOTTEDQUAD);
   87         }
   88         isc_buffer_availableregion(target, &region);
   89         if (region.length < 4) {
   90             return (ISC_R_NOSPACE);
   91         }
   92         memmove(region.base, &addr, 4);
   93         isc_buffer_add(target, 4);
   94         return (ISC_R_SUCCESS);
   95 
   96     case 2:
   97         if (inet_pton(AF_INET6, DNS_AS_STR(token), addr6) != 1) {
   98             RETTOK(DNS_R_BADAAAA);
   99         }
  100         isc_buffer_availableregion(target, &region);
  101         if (region.length < 16) {
  102             return (ISC_R_NOSPACE);
  103         }
  104         memmove(region.base, addr6, 16);
  105         isc_buffer_add(target, 16);
  106         return (ISC_R_SUCCESS);
  107 
  108     case 3:
  109         dns_name_init(&name, NULL);
  110         buffer_fromregion(&buffer, &token.value.as_region);
  111         if (origin == NULL) {
  112             origin = dns_rootname;
  113         }
  114         return (dns_name_fromtext(&name, &buffer, origin, options,
  115                       target));
  116     default:
  117         INSIST(0);
  118         ISC_UNREACHABLE();
  119     }
  120 }
  121 
  122 static inline isc_result_t
  123 totext_amtrelay(ARGS_TOTEXT) {
  124     isc_region_t region;
  125     dns_name_t name;
  126     char buf[sizeof("0 255 ")];
  127     unsigned char precedence;
  128     unsigned char discovery;
  129     unsigned char gateway;
  130     const char *space;
  131 
  132     UNUSED(tctx);
  133 
  134     REQUIRE(rdata->type == dns_rdatatype_amtrelay);
  135     REQUIRE(rdata->length >= 2);
  136 
  137     if ((rdata->data[1] & 0x7f) > 3U)
  138         return (ISC_R_NOTIMPLEMENTED);
  139 
  140     /*
  141      * Precedence.
  142      */
  143     dns_rdata_toregion(rdata, &region);
  144     precedence = uint8_fromregion(&region);
  145     isc_region_consume(&region, 1);
  146     snprintf(buf, sizeof(buf), "%u ", precedence);
  147     RETERR(str_totext(buf, target));
  148 
  149     /*
  150      * Discovery and Gateway type.
  151      */
  152     gateway = uint8_fromregion(&region);
  153     discovery = gateway >> 7;
  154     gateway &= 0x7f;
  155     space = (gateway != 0U) ? " " : "";
  156     isc_region_consume(&region, 1);
  157     snprintf(buf, sizeof(buf), "%u %u%s", discovery, gateway, space);
  158     RETERR(str_totext(buf, target));
  159 
  160     /*
  161      * Gateway.
  162      */
  163     switch (gateway) {
  164     case 0:
  165         break;
  166     case 1:
  167         return (inet_totext(AF_INET, tctx->flags, &region, target));
  168 
  169     case 2:
  170         return (inet_totext(AF_INET6, tctx->flags, &region, target));
  171 
  172     case 3:
  173         dns_name_init(&name, NULL);
  174         dns_name_fromregion(&name, &region);
  175         return (dns_name_totext(&name, false, target));
  176 
  177     default:
  178         INSIST(0);
  179         ISC_UNREACHABLE();
  180     }
  181     return (ISC_R_SUCCESS);
  182 }
  183 
  184 static inline isc_result_t
  185 fromwire_amtrelay(ARGS_FROMWIRE) {
  186     dns_name_t name;
  187     isc_region_t region;
  188 
  189     REQUIRE(type == dns_rdatatype_amtrelay);
  190 
  191     UNUSED(type);
  192     UNUSED(rdclass);
  193 
  194     dns_decompress_setmethods(dctx, DNS_COMPRESS_NONE);
  195 
  196     isc_buffer_activeregion(source, &region);
  197     if (region.length < 2)
  198         return (ISC_R_UNEXPECTEDEND);
  199 
  200     switch (region.base[1] & 0x7f) {
  201     case 0:
  202         if (region.length != 2) {
  203             return (DNS_R_FORMERR);
  204         }
  205         isc_buffer_forward(source, region.length);
  206         return (mem_tobuffer(target, region.base, region.length));
  207 
  208     case 1:
  209         if (region.length != 6) {
  210             return (DNS_R_FORMERR);
  211         }
  212         isc_buffer_forward(source, region.length);
  213         return (mem_tobuffer(target, region.base, region.length));
  214 
  215     case 2:
  216         if (region.length != 18) {
  217             return (DNS_R_FORMERR);
  218         }
  219         isc_buffer_forward(source, region.length);
  220         return (mem_tobuffer(target, region.base, region.length));
  221 
  222     case 3:
  223         RETERR(mem_tobuffer(target, region.base, 2));
  224         isc_buffer_forward(source, 2);
  225         dns_name_init(&name, NULL);
  226         return (dns_name_fromwire(&name, source, dctx, options,
  227                       target));
  228 
  229     default:
  230         isc_buffer_forward(source, region.length);
  231         return (mem_tobuffer(target, region.base, region.length));
  232     }
  233 }
  234 
  235 static inline isc_result_t
  236 towire_amtrelay(ARGS_TOWIRE) {
  237     isc_region_t region;
  238 
  239     REQUIRE(rdata->type == dns_rdatatype_amtrelay);
  240     REQUIRE(rdata->length != 0);
  241 
  242     UNUSED(cctx);
  243 
  244     dns_rdata_toregion(rdata, &region);
  245     return (mem_tobuffer(target, region.base, region.length));
  246 }
  247 
  248 static inline int
  249 compare_amtrelay(ARGS_COMPARE) {
  250     isc_region_t region1;
  251     isc_region_t region2;
  252 
  253     REQUIRE(rdata1->type == rdata2->type);
  254     REQUIRE(rdata1->rdclass == rdata2->rdclass);
  255     REQUIRE(rdata1->type == dns_rdatatype_amtrelay);
  256     REQUIRE(rdata1->length >= 2);
  257     REQUIRE(rdata2->length >= 2);
  258 
  259     dns_rdata_toregion(rdata1, &region1);
  260     dns_rdata_toregion(rdata2, &region2);
  261 
  262     return (isc_region_compare(&region1, &region2));
  263 }
  264 
  265 static inline isc_result_t
  266 fromstruct_amtrelay(ARGS_FROMSTRUCT) {
  267     dns_rdata_amtrelay_t *amtrelay;
  268     isc_region_t region;
  269     uint32_t n;
  270 
  271     REQUIRE(type == dns_rdatatype_amtrelay);
  272     REQUIRE(((dns_rdata_amtrelay_t *)source) != NULL);
  273     REQUIRE(((dns_rdata_amtrelay_t *)source)->common.rdtype == type);
  274     REQUIRE(((dns_rdata_amtrelay_t *)source)->common.rdclass == rdclass);
  275 
  276     amtrelay = source;
  277 
  278     UNUSED(type);
  279     UNUSED(rdclass);
  280 
  281     RETERR(uint8_tobuffer(amtrelay->precedence, target));
  282     n = (amtrelay->discovery ? 0x80 : 0) | amtrelay->gateway_type;
  283     RETERR(uint8_tobuffer(n, target));
  284 
  285     switch  (amtrelay->gateway_type) {
  286     case 0:
  287         return (ISC_R_SUCCESS);
  288 
  289     case 1:
  290         n = ntohl(amtrelay->in_addr.s_addr);
  291         return (uint32_tobuffer(n, target));
  292 
  293     case 2:
  294         return (mem_tobuffer(target, amtrelay->in6_addr.s6_addr, 16));
  295         break;
  296 
  297     case 3:
  298         dns_name_toregion(&amtrelay->gateway, &region);
  299         return (isc_buffer_copyregion(target, &region));
  300         break;
  301 
  302     default:
  303         return (mem_tobuffer(target, amtrelay->data, amtrelay->length));
  304     }
  305 }
  306 
  307 static inline isc_result_t
  308 tostruct_amtrelay(ARGS_TOSTRUCT) {
  309     isc_region_t region;
  310     dns_rdata_amtrelay_t *amtrelay;
  311     dns_name_t name;
  312     uint32_t n;
  313 
  314     REQUIRE(((dns_rdata_amtrelay_t *)target) != NULL);
  315     REQUIRE(rdata->type == dns_rdatatype_amtrelay);
  316     REQUIRE(rdata->length >= 2);
  317 
  318     amtrelay = target;
  319 
  320     amtrelay->common.rdclass = rdata->rdclass;
  321     amtrelay->common.rdtype = rdata->type;
  322     ISC_LINK_INIT(&amtrelay->common, link);
  323 
  324     dns_name_init(&amtrelay->gateway, NULL);
  325     amtrelay->data = NULL;
  326 
  327     dns_name_init(&name, NULL);
  328     dns_rdata_toregion(rdata, &region);
  329 
  330     amtrelay->precedence = uint8_fromregion(&region);
  331     isc_region_consume(&region, 1);
  332 
  333     amtrelay->gateway_type = uint8_fromregion(&region);
  334     amtrelay->discovery = (amtrelay->gateway_type & 0x80) != 0;
  335     amtrelay->gateway_type &= 0x7f;
  336     isc_region_consume(&region, 1);
  337 
  338     switch (amtrelay->gateway_type) {
  339     case 0:
  340         break;
  341 
  342     case 1:
  343         n = uint32_fromregion(&region);
  344         amtrelay->in_addr.s_addr = htonl(n);
  345         isc_region_consume(&region, 4);
  346         break;
  347 
  348     case 2:
  349         memmove(amtrelay->in6_addr.s6_addr, region.base, 16);
  350         isc_region_consume(&region, 16);
  351         break;
  352 
  353     case 3:
  354         dns_name_fromregion(&name, &region);
  355         RETERR(name_duporclone(&name, mctx, &amtrelay->gateway));
  356         isc_region_consume(&region, name_length(&name));
  357         break;
  358 
  359     default:
  360         if (region.length != 0) {
  361             amtrelay->data = mem_maybedup(mctx, region.base,
  362                               region.length);
  363             if (amtrelay->data == NULL) {
  364                 return (ISC_R_NOMEMORY);
  365             }
  366         }
  367         amtrelay->length = region.length;
  368     }
  369     amtrelay->mctx = mctx;
  370     return (ISC_R_SUCCESS);
  371 }
  372 
  373 static inline void
  374 freestruct_amtrelay(ARGS_FREESTRUCT) {
  375     dns_rdata_amtrelay_t *amtrelay;
  376 
  377     REQUIRE(((dns_rdata_amtrelay_t *)source) != NULL);
  378     REQUIRE(((dns_rdata_amtrelay_t *)source)->common.rdtype ==
  379         dns_rdatatype_amtrelay);
  380 
  381     amtrelay = source;
  382 
  383     if (amtrelay->mctx == NULL)
  384         return;
  385 
  386     if (amtrelay->gateway_type == 3)
  387         dns_name_free(&amtrelay->gateway, amtrelay->mctx);
  388 
  389     if (amtrelay->data != NULL)
  390         isc_mem_free(amtrelay->mctx, amtrelay->data);
  391 
  392     amtrelay->mctx = NULL;
  393 }
  394 
  395 static inline isc_result_t
  396 additionaldata_amtrelay(ARGS_ADDLDATA) {
  397 
  398     REQUIRE(rdata->type == dns_rdatatype_amtrelay);
  399 
  400     UNUSED(rdata);
  401     UNUSED(add);
  402     UNUSED(arg);
  403 
  404     return (ISC_R_SUCCESS);
  405 }
  406 
  407 static inline isc_result_t
  408 digest_amtrelay(ARGS_DIGEST) {
  409     isc_region_t region;
  410 
  411     REQUIRE(rdata->type == dns_rdatatype_amtrelay);
  412 
  413     dns_rdata_toregion(rdata, &region);
  414     return ((digest)(arg, &region));
  415 }
  416 
  417 static inline bool
  418 checkowner_amtrelay(ARGS_CHECKOWNER) {
  419 
  420     REQUIRE(type == dns_rdatatype_amtrelay);
  421 
  422     UNUSED(name);
  423     UNUSED(type);
  424     UNUSED(rdclass);
  425     UNUSED(wildcard);
  426 
  427     return (true);
  428 }
  429 
  430 static inline bool
  431 checknames_amtrelay(ARGS_CHECKNAMES) {
  432 
  433     REQUIRE(rdata->type == dns_rdatatype_amtrelay);
  434 
  435     UNUSED(rdata);
  436     UNUSED(owner);
  437     UNUSED(bad);
  438 
  439     return (true);
  440 }
  441 
  442 static inline int
  443 casecompare_amtrelay(ARGS_COMPARE) {
  444     isc_region_t region1;
  445     isc_region_t region2;
  446     dns_name_t name1;
  447     dns_name_t name2;
  448 
  449     REQUIRE(rdata1->type == rdata2->type);
  450     REQUIRE(rdata1->rdclass == rdata2->rdclass);
  451     REQUIRE(rdata1->type == dns_rdatatype_amtrelay);
  452     REQUIRE(rdata1->length >= 2);
  453     REQUIRE(rdata2->length >= 2);
  454 
  455     dns_rdata_toregion(rdata1, &region1);
  456     dns_rdata_toregion(rdata2, &region2);
  457 
  458     if (memcmp(region1.base, region2.base, 2) != 0 ||
  459         (region1.base[1] & 0x7f) != 3)
  460         return (isc_region_compare(&region1, &region2));
  461 
  462     dns_name_init(&name1, NULL);
  463     dns_name_init(&name2, NULL);
  464 
  465     isc_region_consume(&region1, 2);
  466     isc_region_consume(&region2, 2);
  467 
  468     dns_name_fromregion(&name1, &region1);
  469     dns_name_fromregion(&name2, &region2);
  470 
  471     return (dns_name_rdatacompare(&name1, &name2));
  472 }
  473 
  474 #endif  /* RDATA_GENERIC_AMTRELAY_260_C */