File: forward.c

package info (click to toggle)
bind9 1%3A9.20.19-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 43,408 kB
  • sloc: ansic: 316,115; sh: 50,056; python: 23,954; perl: 3,062; makefile: 2,247
file content (251 lines) | stat: -rw-r--r-- 6,472 bytes parent folder | download | duplicates (7)
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
/*
 * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
 *
 * SPDX-License-Identifier: MPL-2.0
 *
 * 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.
 */

/*! \file */

#include <isc/magic.h>
#include <isc/mem.h>
#include <isc/result.h>
#include <isc/util.h>

#include <dns/fixedname.h>
#include <dns/forward.h>
#include <dns/name.h>
#include <dns/qp.h>
#include <dns/types.h>
#include <dns/view.h>

struct dns_fwdtable {
	/* Unlocked. */
	unsigned int magic;
	isc_mem_t *mctx;
	dns_qpmulti_t *table;
};

#define FWDTABLEMAGIC	   ISC_MAGIC('F', 'w', 'd', 'T')
#define VALID_FWDTABLE(ft) ISC_MAGIC_VALID(ft, FWDTABLEMAGIC)

static void
qp_attach(void *uctx, void *pval, uint32_t ival);
static void
qp_detach(void *uctx, void *pval, uint32_t ival);
static size_t
qp_makekey(dns_qpkey_t key, void *uctx, void *pval, uint32_t ival);
static void
qp_triename(void *uctx, char *buf, size_t size);

static dns_qpmethods_t qpmethods = {
	qp_attach,
	qp_detach,
	qp_makekey,
	qp_triename,
};

void
dns_fwdtable_create(isc_mem_t *mctx, dns_view_t *view,
		    dns_fwdtable_t **fwdtablep) {
	dns_fwdtable_t *fwdtable = NULL;

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

	fwdtable = isc_mem_get(mctx, sizeof(*fwdtable));
	*fwdtable = (dns_fwdtable_t){ .magic = FWDTABLEMAGIC };

	dns_qpmulti_create(mctx, &qpmethods, view, &fwdtable->table);

	isc_mem_attach(mctx, &fwdtable->mctx);
	*fwdtablep = fwdtable;
}

static dns_forwarders_t *
new_forwarders(isc_mem_t *mctx, const dns_name_t *name,
	       dns_fwdpolicy_t fwdpolicy) {
	dns_forwarders_t *forwarders = NULL;

	forwarders = isc_mem_get(mctx, sizeof(*forwarders));
	*forwarders = (dns_forwarders_t){
		.fwdpolicy = fwdpolicy,
		.name = DNS_NAME_INITEMPTY,
		.fwdrs = ISC_LIST_INITIALIZER,
	};
	isc_mem_attach(mctx, &forwarders->mctx);
	isc_refcount_init(&forwarders->references, 1);

	dns_name_dupwithoffsets(name, mctx, &forwarders->name);

	return forwarders;
}

isc_result_t
dns_fwdtable_addfwd(dns_fwdtable_t *fwdtable, const dns_name_t *name,
		    dns_forwarderlist_t *fwdrs, dns_fwdpolicy_t fwdpolicy) {
	isc_result_t result;
	dns_forwarders_t *forwarders = NULL;
	dns_forwarder_t *fwd = NULL, *nfwd = NULL;
	dns_qp_t *qp = NULL;

	REQUIRE(VALID_FWDTABLE(fwdtable));

	forwarders = new_forwarders(fwdtable->mctx, name, fwdpolicy);

	for (fwd = ISC_LIST_HEAD(*fwdrs); fwd != NULL;
	     fwd = ISC_LIST_NEXT(fwd, link))
	{
		nfwd = isc_mem_get(fwdtable->mctx, sizeof(*nfwd));
		*nfwd = *fwd;

		if (fwd->tlsname != NULL) {
			nfwd->tlsname = isc_mem_get(fwdtable->mctx,
						    sizeof(*nfwd->tlsname));
			dns_name_init(nfwd->tlsname, NULL);
			dns_name_dup(fwd->tlsname, fwdtable->mctx,
				     nfwd->tlsname);
		}

		ISC_LINK_INIT(nfwd, link);
		ISC_LIST_APPEND(forwarders->fwdrs, nfwd, link);
	}

	dns_qpmulti_write(fwdtable->table, &qp);
	result = dns_qp_insert(qp, forwarders, 0);
	dns_qp_compact(qp, DNS_QPGC_MAYBE);
	dns_qpmulti_commit(fwdtable->table, &qp);

	dns_forwarders_detach(&forwarders);

	return result;
}

isc_result_t
dns_fwdtable_add(dns_fwdtable_t *fwdtable, const dns_name_t *name,
		 isc_sockaddrlist_t *addrs, dns_fwdpolicy_t fwdpolicy) {
	isc_result_t result;
	dns_forwarders_t *forwarders = NULL;
	dns_forwarder_t *fwd = NULL;
	isc_sockaddr_t *sa = NULL;
	dns_qp_t *qp = NULL;

	REQUIRE(VALID_FWDTABLE(fwdtable));

	forwarders = new_forwarders(fwdtable->mctx, name, fwdpolicy);

	for (sa = ISC_LIST_HEAD(*addrs); sa != NULL;
	     sa = ISC_LIST_NEXT(sa, link))
	{
		fwd = isc_mem_get(fwdtable->mctx, sizeof(*fwd));
		*fwd = (dns_forwarder_t){ .addr = *sa,
					  .link = ISC_LINK_INITIALIZER };
		ISC_LIST_APPEND(forwarders->fwdrs, fwd, link);
	}

	dns_qpmulti_write(fwdtable->table, &qp);
	result = dns_qp_insert(qp, forwarders, 0);
	dns_qp_compact(qp, DNS_QPGC_MAYBE);
	dns_qpmulti_commit(fwdtable->table, &qp);

	dns_forwarders_detach(&forwarders);

	return result;
}

isc_result_t
dns_fwdtable_find(dns_fwdtable_t *fwdtable, const dns_name_t *name,
		  dns_forwarders_t **forwardersp) {
	isc_result_t result;
	dns_qpread_t qpr;
	void *pval = NULL;

	REQUIRE(VALID_FWDTABLE(fwdtable));

	dns_qpmulti_query(fwdtable->table, &qpr);
	result = dns_qp_lookup(&qpr, name, NULL, NULL, NULL, &pval, NULL);
	if (result == ISC_R_SUCCESS || result == DNS_R_PARTIALMATCH) {
		dns_forwarders_t *fwdrs = pval;
		*forwardersp = fwdrs;
		dns_forwarders_ref(fwdrs);
	}
	dns_qpread_destroy(fwdtable->table, &qpr);

	return result;
}

void
dns_fwdtable_destroy(dns_fwdtable_t **fwdtablep) {
	dns_fwdtable_t *fwdtable = NULL;

	REQUIRE(fwdtablep != NULL && VALID_FWDTABLE(*fwdtablep));

	fwdtable = *fwdtablep;
	*fwdtablep = NULL;

	dns_qpmulti_destroy(&fwdtable->table);
	fwdtable->magic = 0;

	isc_mem_putanddetach(&fwdtable->mctx, fwdtable, sizeof(*fwdtable));
}

/***
 *** Private
 ***/

static void
destroy_forwarders(dns_forwarders_t *forwarders) {
	dns_forwarder_t *fwd = NULL;

	while (!ISC_LIST_EMPTY(forwarders->fwdrs)) {
		fwd = ISC_LIST_HEAD(forwarders->fwdrs);
		ISC_LIST_UNLINK(forwarders->fwdrs, fwd, link);
		if (fwd->tlsname != NULL) {
			dns_name_free(fwd->tlsname, forwarders->mctx);
			isc_mem_put(forwarders->mctx, fwd->tlsname,
				    sizeof(*fwd->tlsname));
		}
		isc_mem_put(forwarders->mctx, fwd, sizeof(*fwd));
	}
	dns_name_free(&forwarders->name, forwarders->mctx);
	isc_mem_putanddetach(&forwarders->mctx, forwarders,
			     sizeof(*forwarders));
}

#if DNS_FORWARD_TRACE
ISC_REFCOUNT_TRACE_IMPL(dns_forwarders, destroy_forwarders);
#else
ISC_REFCOUNT_IMPL(dns_forwarders, destroy_forwarders);
#endif

static void
qp_attach(void *uctx ISC_ATTR_UNUSED, void *pval,
	  uint32_t ival ISC_ATTR_UNUSED) {
	dns_forwarders_t *forwarders = pval;
	dns_forwarders_ref(forwarders);
}

static void
qp_detach(void *uctx ISC_ATTR_UNUSED, void *pval,
	  uint32_t ival ISC_ATTR_UNUSED) {
	dns_forwarders_t *forwarders = pval;
	dns_forwarders_detach(&forwarders);
}

static size_t
qp_makekey(dns_qpkey_t key, void *uctx ISC_ATTR_UNUSED, void *pval,
	   uint32_t ival ISC_ATTR_UNUSED) {
	dns_forwarders_t *fwd = pval;
	return dns_qpkey_fromname(key, &fwd->name);
}

static void
qp_triename(void *uctx, char *buf, size_t size) {
	dns_view_t *view = uctx;
	snprintf(buf, size, "view %s forwarder table", view->name);
}