2
0
mirror of https://gitlab.isc.org/isc-projects/bind9 synced 2025-08-23 10:39:16 +00:00
bind/lib/dns/order.c

148 lines
3.7 KiB
C
Raw Normal View History

2002-03-07 06:29:37 +00:00
/*
* Copyright (C) Internet Systems Consortium, Inc. ("ISC")
2002-03-07 06:29:37 +00:00
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, you can obtain one at https://mozilla.org/MPL/2.0/.
*
* See the COPYRIGHT file distributed with this work for additional
* information regarding copyright ownership.
2002-03-07 06:29:37 +00:00
*/
/*! \file */
2002-05-02 06:55:23 +00:00
#include <stdbool.h>
2002-03-07 06:29:37 +00:00
#include <isc/magic.h>
#include <isc/mem.h>
#include <isc/refcount.h>
2002-03-07 06:29:37 +00:00
#include <isc/types.h>
#include <isc/util.h>
#include <dns/fixedname.h>
#include <dns/name.h>
#include <dns/order.h>
#include <dns/rdataset.h>
#include <dns/types.h>
typedef struct dns_order_ent dns_order_ent_t;
struct dns_order_ent {
2020-02-13 14:44:37 -08:00
dns_fixedname_t name;
dns_rdataclass_t rdclass;
2020-02-13 14:44:37 -08:00
dns_rdatatype_t rdtype;
unsigned int mode;
ISC_LINK(dns_order_ent_t) link;
2002-03-07 06:29:37 +00:00
};
struct dns_order {
2020-02-13 14:44:37 -08:00
unsigned int magic;
isc_refcount_t references;
ISC_LIST(dns_order_ent_t) ents;
isc_mem_t *mctx;
2002-03-07 06:29:37 +00:00
};
2015-08-25 23:45:27 +00:00
2020-02-13 14:44:37 -08:00
#define DNS_ORDER_MAGIC ISC_MAGIC('O', 'r', 'd', 'r')
#define DNS_ORDER_VALID(order) ISC_MAGIC_VALID(order, DNS_ORDER_MAGIC)
2002-03-07 06:29:37 +00:00
isc_result_t
2020-02-13 14:44:37 -08:00
dns_order_create(isc_mem_t *mctx, dns_order_t **orderp) {
2002-03-07 06:29:37 +00:00
dns_order_t *order;
2002-03-07 06:29:37 +00:00
REQUIRE(orderp != NULL && *orderp == NULL);
order = isc_mem_get(mctx, sizeof(*order));
2015-08-25 23:45:27 +00:00
2002-03-07 06:29:37 +00:00
ISC_LIST_INIT(order->ents);
/* Implicit attach. */
isc_refcount_init(&order->references, 1);
2002-03-07 06:29:37 +00:00
order->mctx = NULL;
2002-03-07 06:29:37 +00:00
isc_mem_attach(mctx, &order->mctx);
order->magic = DNS_ORDER_MAGIC;
*orderp = order;
2002-03-07 06:29:37 +00:00
return (ISC_R_SUCCESS);
}
isc_result_t
dns_order_add(dns_order_t *order, const dns_name_t *name,
2002-03-07 06:29:37 +00:00
dns_rdatatype_t rdtype, dns_rdataclass_t rdclass,
2020-02-13 14:44:37 -08:00
unsigned int mode) {
2002-03-07 06:29:37 +00:00
dns_order_ent_t *ent;
REQUIRE(DNS_ORDER_VALID(order));
REQUIRE(mode == DNS_RDATASETATTR_RANDOMIZE ||
2015-08-25 23:45:27 +00:00
mode == DNS_RDATASETATTR_FIXEDORDER ||
mode == DNS_RDATASETATTR_CYCLIC ||
mode == DNS_RDATASETATTR_NONE);
2002-03-07 06:29:37 +00:00
ent = isc_mem_get(order->mctx, sizeof(*ent));
dns_fixedname_init(&ent->name);
dns_name_copynf(name, dns_fixedname_name(&ent->name));
2002-03-07 06:29:37 +00:00
ent->rdtype = rdtype;
ent->rdclass = rdclass;
ent->mode = mode;
ISC_LINK_INIT(ent, link);
ISC_LIST_INITANDAPPEND(order->ents, ent, link);
return (ISC_R_SUCCESS);
}
static inline bool
2020-02-13 14:44:37 -08:00
match(const dns_name_t *name1, const dns_name_t *name2) {
if (dns_name_iswildcard(name2)) {
return (dns_name_matcheswildcard(name1, name2));
}
2002-03-07 06:29:37 +00:00
return (dns_name_equal(name1, name2));
}
unsigned int
dns_order_find(dns_order_t *order, const dns_name_t *name,
2020-02-13 14:44:37 -08:00
dns_rdatatype_t rdtype, dns_rdataclass_t rdclass) {
2002-03-07 06:29:37 +00:00
dns_order_ent_t *ent;
REQUIRE(DNS_ORDER_VALID(order));
for (ent = ISC_LIST_HEAD(order->ents); ent != NULL;
2020-02-13 14:44:37 -08:00
ent = ISC_LIST_NEXT(ent, link))
{
if (ent->rdtype != rdtype && ent->rdtype != dns_rdatatype_any) {
2002-03-07 06:29:37 +00:00
continue;
}
2002-03-07 06:29:37 +00:00
if (ent->rdclass != rdclass &&
ent->rdclass != dns_rdataclass_any) {
2002-03-07 06:29:37 +00:00
continue;
}
if (match(name, dns_fixedname_name(&ent->name))) {
2002-03-07 06:29:37 +00:00
return (ent->mode);
}
2002-03-07 06:29:37 +00:00
}
return (DNS_RDATASETATTR_NONE);
2002-03-07 06:29:37 +00:00
}
void
2020-02-13 14:44:37 -08:00
dns_order_attach(dns_order_t *source, dns_order_t **target) {
2002-03-07 06:29:37 +00:00
REQUIRE(DNS_ORDER_VALID(source));
REQUIRE(target != NULL && *target == NULL);
isc_refcount_increment(&source->references);
2002-03-07 06:29:37 +00:00
*target = source;
}
void
2020-02-13 14:44:37 -08:00
dns_order_detach(dns_order_t **orderp) {
REQUIRE(orderp != NULL && DNS_ORDER_VALID(*orderp));
2002-03-07 06:29:37 +00:00
dns_order_t *order;
order = *orderp;
*orderp = NULL;
2002-03-07 06:29:37 +00:00
if (isc_refcount_decrement(&order->references) == 1) {
isc_refcount_destroy(&order->references);
order->magic = 0;
dns_order_ent_t *ent;
while ((ent = ISC_LIST_HEAD(order->ents)) != NULL) {
ISC_LIST_UNLINK(order->ents, ent, link);
isc_mem_put(order->mctx, ent, sizeof(*ent));
}
isc_mem_putanddetach(&order->mctx, order, sizeof(*order));
2002-03-07 06:29:37 +00:00
}
}