summaryrefslogtreecommitdiffstats
path: root/lib/dns/order.c
blob: 6ffc4a0f9a79088aa5e56fce721cba0bf607888a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/*
 * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
 *
 * 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 http://mozilla.org/MPL/2.0/.
 *
 * See the COPYRIGHT file distributed with this work for additional
 * information regarding copyright ownership.
 */

/* $Id: order.c,v 1.10 2007/06/19 23:47:16 tbox Exp $ */

/*! \file */

#include <config.h>

#include <stdbool.h>

#include <isc/magic.h>
#include <isc/mem.h>
#include <isc/types.h>
#include <isc/util.h>
#include <isc/refcount.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 {
	dns_fixedname_t			name;
	dns_rdataclass_t		rdclass;
	dns_rdatatype_t			rdtype;
	unsigned int			mode;
	ISC_LINK(dns_order_ent_t)	link;
};

struct dns_order {
	unsigned int			magic;
	isc_refcount_t          	references;
	ISC_LIST(dns_order_ent_t)	ents;
	isc_mem_t			*mctx;
};

#define DNS_ORDER_MAGIC ISC_MAGIC('O','r','d','r')
#define DNS_ORDER_VALID(order)	ISC_MAGIC_VALID(order, DNS_ORDER_MAGIC)

isc_result_t
dns_order_create(isc_mem_t *mctx, dns_order_t **orderp) {
	dns_order_t *order;
	isc_result_t result;

	REQUIRE(orderp != NULL && *orderp == NULL);

	order = isc_mem_get(mctx, sizeof(*order));
	if (order == NULL)
		return (ISC_R_NOMEMORY);

	ISC_LIST_INIT(order->ents);

	/* Implicit attach. */
	result = isc_refcount_init(&order->references, 1);
	if (result != ISC_R_SUCCESS) {
		isc_mem_put(mctx, order, sizeof(*order));
		return (result);
	}

	order->mctx = NULL;
	isc_mem_attach(mctx, &order->mctx);
	order->magic = DNS_ORDER_MAGIC;
	*orderp = order;
	return (ISC_R_SUCCESS);
}

isc_result_t
dns_order_add(dns_order_t *order, dns_name_t *name,
	      dns_rdatatype_t rdtype, dns_rdataclass_t rdclass,
	      unsigned int mode)
{
	dns_order_ent_t *ent;

	REQUIRE(DNS_ORDER_VALID(order));
	REQUIRE(mode == DNS_RDATASETATTR_RANDOMIZE ||
		mode == DNS_RDATASETATTR_FIXEDORDER ||
		mode == 0 /* DNS_RDATASETATTR_CYCLIC */ );

	ent = isc_mem_get(order->mctx, sizeof(*ent));
	if (ent == NULL)
		return (ISC_R_NOMEMORY);

	dns_fixedname_init(&ent->name);
	RUNTIME_CHECK(dns_name_copy(name, dns_fixedname_name(&ent->name), NULL)
		      == ISC_R_SUCCESS);
	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
match(dns_name_t *name1, dns_name_t *name2) {

	if (dns_name_iswildcard(name2))
		return(dns_name_matcheswildcard(name1, name2));
	return (dns_name_equal(name1, name2));
}

unsigned int
dns_order_find(dns_order_t *order, dns_name_t *name,
	       dns_rdatatype_t rdtype, dns_rdataclass_t rdclass)
{
	dns_order_ent_t *ent;
	REQUIRE(DNS_ORDER_VALID(order));

	for (ent = ISC_LIST_HEAD(order->ents);
	     ent != NULL;
	     ent = ISC_LIST_NEXT(ent, link)) {
		if (ent->rdtype != rdtype && ent->rdtype != dns_rdatatype_any)
			continue;
		if (ent->rdclass != rdclass &&
		    ent->rdclass != dns_rdataclass_any)
			continue;
		if (match(name, dns_fixedname_name(&ent->name)))
			return (ent->mode);
	}
	return (DNS_RDATASETATTR_RANDOMIZE);
}

void
dns_order_attach(dns_order_t *source, dns_order_t **target) {
	REQUIRE(DNS_ORDER_VALID(source));
	REQUIRE(target != NULL && *target == NULL);
	isc_refcount_increment(&source->references, NULL);
	*target = source;
}

void
dns_order_detach(dns_order_t **orderp) {
	dns_order_t *order;
	dns_order_ent_t *ent;
	unsigned int references;

	REQUIRE(orderp != NULL);
	order = *orderp;
	REQUIRE(DNS_ORDER_VALID(order));
	isc_refcount_decrement(&order->references, &references);
	*orderp = NULL;
	if (references != 0)
		return;

	order->magic = 0;
	while ((ent = ISC_LIST_HEAD(order->ents)) != NULL) {
		ISC_LIST_UNLINK(order->ents, ent, link);
		isc_mem_put(order->mctx, ent, sizeof(*ent));
	}
	isc_refcount_destroy(&order->references);
	isc_mem_putanddetach(&order->mctx, order, sizeof(*order));
}