order.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2004, 2005, 2007  Internet Systems Consortium, Inc. ("ISC")
00003  * Copyright (C) 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: order.c,v 1.10 2007/06/19 23:47:16 tbox Exp $ */
00019 
00020 /*! \file */
00021 
00022 #include <config.h>
00023 
00024 #include <isc/magic.h>
00025 #include <isc/mem.h>
00026 #include <isc/types.h>
00027 #include <isc/util.h>
00028 #include <isc/refcount.h>
00029 
00030 #include <dns/fixedname.h>
00031 #include <dns/name.h>
00032 #include <dns/order.h>
00033 #include <dns/rdataset.h>
00034 #include <dns/types.h>
00035 
00036 typedef struct dns_order_ent dns_order_ent_t;
00037 struct dns_order_ent {
00038         dns_fixedname_t                 name;
00039         dns_rdataclass_t                rdclass;
00040         dns_rdatatype_t                 rdtype;
00041         unsigned int                    mode;
00042         ISC_LINK(dns_order_ent_t)       link;
00043 };
00044 
00045 struct dns_order {
00046         unsigned int                    magic;
00047         isc_refcount_t                  references;
00048         ISC_LIST(dns_order_ent_t)       ents;
00049         isc_mem_t                       *mctx;
00050 };
00051         
00052 #define DNS_ORDER_MAGIC ISC_MAGIC('O','r','d','r')
00053 #define DNS_ORDER_VALID(order)  ISC_MAGIC_VALID(order, DNS_ORDER_MAGIC)
00054 
00055 isc_result_t
00056 dns_order_create(isc_mem_t *mctx, dns_order_t **orderp) {
00057         dns_order_t *order;
00058         isc_result_t result;
00059 
00060         REQUIRE(orderp != NULL && *orderp == NULL);
00061 
00062         order = isc_mem_get(mctx, sizeof(*order));
00063         if (order == NULL)
00064                 return (ISC_R_NOMEMORY);
00065         
00066         ISC_LIST_INIT(order->ents);
00067 
00068         /* Implicit attach. */
00069         result = isc_refcount_init(&order->references, 1);
00070         if (result != ISC_R_SUCCESS) {
00071                 isc_mem_put(mctx, order, sizeof(*order));
00072                 return (result);
00073         }
00074 
00075         order->mctx = NULL;
00076         isc_mem_attach(mctx, &order->mctx);
00077         order->magic = DNS_ORDER_MAGIC;
00078         *orderp = order;
00079         return (ISC_R_SUCCESS);
00080 }
00081 
00082 isc_result_t
00083 dns_order_add(dns_order_t *order, dns_name_t *name,
00084               dns_rdatatype_t rdtype, dns_rdataclass_t rdclass,
00085               unsigned int mode)
00086 {
00087         dns_order_ent_t *ent;
00088 
00089         REQUIRE(DNS_ORDER_VALID(order));
00090         REQUIRE(mode == DNS_RDATASETATTR_RANDOMIZE ||
00091                 mode == DNS_RDATASETATTR_FIXEDORDER ||
00092                 mode == 0 /* DNS_RDATASETATTR_CYCLIC */ );
00093 
00094         ent = isc_mem_get(order->mctx, sizeof(*ent));
00095         if (ent == NULL)
00096                 return (ISC_R_NOMEMORY);
00097 
00098         dns_fixedname_init(&ent->name);
00099         RUNTIME_CHECK(dns_name_copy(name, dns_fixedname_name(&ent->name), NULL)
00100                       == ISC_R_SUCCESS);
00101         ent->rdtype = rdtype;
00102         ent->rdclass = rdclass;
00103         ent->mode = mode;
00104         ISC_LINK_INIT(ent, link);
00105         ISC_LIST_INITANDAPPEND(order->ents, ent, link);
00106         return (ISC_R_SUCCESS);
00107 }
00108 
00109 static inline isc_boolean_t
00110 match(dns_name_t *name1, dns_name_t *name2) {
00111         
00112         if (dns_name_iswildcard(name2))
00113                 return(dns_name_matcheswildcard(name1, name2));
00114         return (dns_name_equal(name1, name2));
00115 }
00116 
00117 unsigned int
00118 dns_order_find(dns_order_t *order, dns_name_t *name,
00119                dns_rdatatype_t rdtype, dns_rdataclass_t rdclass)
00120 {
00121         dns_order_ent_t *ent;
00122         REQUIRE(DNS_ORDER_VALID(order));
00123 
00124         for (ent = ISC_LIST_HEAD(order->ents);
00125              ent != NULL;
00126              ent = ISC_LIST_NEXT(ent, link)) {
00127                 if (ent->rdtype != rdtype && ent->rdtype != dns_rdatatype_any)
00128                         continue;
00129                 if (ent->rdclass != rdclass &&
00130                     ent->rdclass != dns_rdataclass_any)
00131                         continue;
00132                 if (match(name, dns_fixedname_name(&ent->name)))
00133                         return (ent->mode);
00134         }
00135         return (0);
00136 }
00137 
00138 void
00139 dns_order_attach(dns_order_t *source, dns_order_t **target) {
00140         REQUIRE(DNS_ORDER_VALID(source));
00141         REQUIRE(target != NULL && *target == NULL);
00142         isc_refcount_increment(&source->references, NULL);
00143         *target = source;
00144 }
00145 
00146 void
00147 dns_order_detach(dns_order_t **orderp) {
00148         dns_order_t *order;
00149         dns_order_ent_t *ent;
00150         unsigned int references;
00151 
00152         REQUIRE(orderp != NULL);
00153         order = *orderp;
00154         REQUIRE(DNS_ORDER_VALID(order));
00155         isc_refcount_decrement(&order->references, &references);
00156         *orderp = NULL;
00157         if (references != 0)
00158                 return;
00159 
00160         order->magic = 0;
00161         while ((ent = ISC_LIST_HEAD(order->ents)) != NULL) {
00162                 ISC_LIST_UNLINK(order->ents, ent, link);
00163                 isc_mem_put(order->mctx, ent, sizeof(*ent));
00164         }
00165         isc_refcount_destroy(&order->references);
00166         isc_mem_putanddetach(&order->mctx, order, sizeof(*order));
00167 }

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