"Fossies" - the Fresh Open Source Software Archive

Member "bind-9.17.5/lib/dns/order.c" (4 Sep 2020, 3709 Bytes) of package /linux/misc/dns/bind9/9.17.5/bind-9.17.5.tar.xz:


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 "order.c" see the Fossies "Dox" file reference documentation.

    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 /*! \file */
   13 
   14 #include <stdbool.h>
   15 
   16 #include <isc/magic.h>
   17 #include <isc/mem.h>
   18 #include <isc/refcount.h>
   19 #include <isc/types.h>
   20 #include <isc/util.h>
   21 
   22 #include <dns/fixedname.h>
   23 #include <dns/name.h>
   24 #include <dns/order.h>
   25 #include <dns/rdataset.h>
   26 #include <dns/types.h>
   27 
   28 typedef struct dns_order_ent dns_order_ent_t;
   29 struct dns_order_ent {
   30     dns_fixedname_t name;
   31     dns_rdataclass_t rdclass;
   32     dns_rdatatype_t rdtype;
   33     unsigned int mode;
   34     ISC_LINK(dns_order_ent_t) link;
   35 };
   36 
   37 struct dns_order {
   38     unsigned int magic;
   39     isc_refcount_t references;
   40     ISC_LIST(dns_order_ent_t) ents;
   41     isc_mem_t *mctx;
   42 };
   43 
   44 #define DNS_ORDER_MAGIC        ISC_MAGIC('O', 'r', 'd', 'r')
   45 #define DNS_ORDER_VALID(order) ISC_MAGIC_VALID(order, DNS_ORDER_MAGIC)
   46 
   47 isc_result_t
   48 dns_order_create(isc_mem_t *mctx, dns_order_t **orderp) {
   49     dns_order_t *order;
   50 
   51     REQUIRE(orderp != NULL && *orderp == NULL);
   52 
   53     order = isc_mem_get(mctx, sizeof(*order));
   54 
   55     ISC_LIST_INIT(order->ents);
   56 
   57     /* Implicit attach. */
   58     isc_refcount_init(&order->references, 1);
   59 
   60     order->mctx = NULL;
   61     isc_mem_attach(mctx, &order->mctx);
   62     order->magic = DNS_ORDER_MAGIC;
   63     *orderp = order;
   64     return (ISC_R_SUCCESS);
   65 }
   66 
   67 isc_result_t
   68 dns_order_add(dns_order_t *order, const dns_name_t *name,
   69           dns_rdatatype_t rdtype, dns_rdataclass_t rdclass,
   70           unsigned int mode) {
   71     dns_order_ent_t *ent;
   72 
   73     REQUIRE(DNS_ORDER_VALID(order));
   74     REQUIRE(mode == DNS_RDATASETATTR_RANDOMIZE ||
   75         mode == DNS_RDATASETATTR_FIXEDORDER ||
   76         mode == DNS_RDATASETATTR_CYCLIC);
   77 
   78     ent = isc_mem_get(order->mctx, sizeof(*ent));
   79 
   80     dns_fixedname_init(&ent->name);
   81     dns_name_copynf(name, dns_fixedname_name(&ent->name));
   82     ent->rdtype = rdtype;
   83     ent->rdclass = rdclass;
   84     ent->mode = mode;
   85     ISC_LINK_INIT(ent, link);
   86     ISC_LIST_INITANDAPPEND(order->ents, ent, link);
   87     return (ISC_R_SUCCESS);
   88 }
   89 
   90 static inline bool
   91 match(const dns_name_t *name1, const dns_name_t *name2) {
   92     if (dns_name_iswildcard(name2)) {
   93         return (dns_name_matcheswildcard(name1, name2));
   94     }
   95     return (dns_name_equal(name1, name2));
   96 }
   97 
   98 unsigned int
   99 dns_order_find(dns_order_t *order, const dns_name_t *name,
  100            dns_rdatatype_t rdtype, dns_rdataclass_t rdclass) {
  101     dns_order_ent_t *ent;
  102     REQUIRE(DNS_ORDER_VALID(order));
  103 
  104     for (ent = ISC_LIST_HEAD(order->ents); ent != NULL;
  105          ent = ISC_LIST_NEXT(ent, link))
  106     {
  107         if (ent->rdtype != rdtype && ent->rdtype != dns_rdatatype_any) {
  108             continue;
  109         }
  110         if (ent->rdclass != rdclass &&
  111             ent->rdclass != dns_rdataclass_any) {
  112             continue;
  113         }
  114         if (match(name, dns_fixedname_name(&ent->name))) {
  115             return (ent->mode);
  116         }
  117     }
  118     return (DNS_RDATASETATTR_NONE);
  119 }
  120 
  121 void
  122 dns_order_attach(dns_order_t *source, dns_order_t **target) {
  123     REQUIRE(DNS_ORDER_VALID(source));
  124     REQUIRE(target != NULL && *target == NULL);
  125     isc_refcount_increment(&source->references);
  126     *target = source;
  127 }
  128 
  129 void
  130 dns_order_detach(dns_order_t **orderp) {
  131     REQUIRE(orderp != NULL && DNS_ORDER_VALID(*orderp));
  132     dns_order_t *order;
  133     order = *orderp;
  134     *orderp = NULL;
  135 
  136     if (isc_refcount_decrement(&order->references) == 1) {
  137         isc_refcount_destroy(&order->references);
  138         order->magic = 0;
  139         dns_order_ent_t *ent;
  140         while ((ent = ISC_LIST_HEAD(order->ents)) != NULL) {
  141             ISC_LIST_UNLINK(order->ents, ent, link);
  142             isc_mem_put(order->mctx, ent, sizeof(*ent));
  143         }
  144         isc_mem_putanddetach(&order->mctx, order, sizeof(*order));
  145     }
  146 }