~singpolyma/asterisk

ref: 130ba7aeba07eb8bb3ea34418ca458bb58b05080 asterisk/main/dns_query_set.c -rw-r--r-- 7.8 KiB
130ba7ae — Bernhard Schmidt Import asterisk_16.2.1~dfsg.orig.tar.xz 3 years ago
                                                                                
130ba7ae Bernhard Schmidt
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
/*
 * Asterisk -- An open source telephony toolkit.
 *
 * Copyright (C) 2015, Digium, Inc.
 *
 * Joshua Colp <jcolp@digium.com>
 *
 * See http://www.asterisk.org for more information about
 * the Asterisk project. Please do not directly contact
 * any of the maintainers of this project for assistance;
 * the project provides a web site, mailing lists and IRC
 * channels for your use.
 *
 * This program is free software, distributed under the terms of
 * the GNU General Public License Version 2. See the LICENSE file
 * at the top of the source tree.
 */

/*! \file
 *
 * \brief DNS Query Set API
 *
 * \author Joshua Colp <jcolp@digium.com>
 */

/*** MODULEINFO
	<support_level>core</support_level>
 ***/

#include "asterisk.h"

#include "asterisk/vector.h"
#include "asterisk/astobj2.h"
#include "asterisk/utils.h"
#include "asterisk/linkedlists.h"
#include "asterisk/dns_core.h"
#include "asterisk/dns_query_set.h"
#include "asterisk/dns_internal.h"
#include "asterisk/dns_resolver.h"

/*! \brief The default number of expected queries to be added to the query set */
#define DNS_QUERY_SET_EXPECTED_QUERY_COUNT 5

/*! \brief Destructor for DNS query set */
static void dns_query_set_destroy(void *data)
{
	struct ast_dns_query_set *query_set = data;
	int idx;

	for (idx = 0; idx < AST_VECTOR_SIZE(&query_set->queries); ++idx) {
		struct dns_query_set_query *query = AST_VECTOR_GET_ADDR(&query_set->queries, idx);

		ao2_ref(query->query, -1);
	}
	AST_VECTOR_FREE(&query_set->queries);

	ao2_cleanup(query_set->user_data);
}

struct ast_dns_query_set *ast_dns_query_set_create(void)
{
	struct ast_dns_query_set *query_set;

	query_set = ao2_alloc_options(sizeof(*query_set), dns_query_set_destroy, AO2_ALLOC_OPT_LOCK_NOLOCK);
	if (!query_set) {
		return NULL;
	}

	if (AST_VECTOR_INIT(&query_set->queries, DNS_QUERY_SET_EXPECTED_QUERY_COUNT)) {
		ao2_ref(query_set, -1);
		return NULL;
	}

	return query_set;
}

/*! \brief Callback invoked upon completion of a DNS query */
static void dns_query_set_callback(const struct ast_dns_query *query)
{
	struct ast_dns_query_set *query_set = ast_dns_query_get_data(query);

	/* The reference count of the query set is bumped here in case this query holds the last reference */
	ao2_ref(query_set, +1);

	/* Drop the query set from the query so the query set can be destroyed if this is the last one */
	ao2_cleanup(((struct ast_dns_query *)query)->user_data);
	((struct ast_dns_query *)query)->user_data = NULL;

	if (ast_atomic_fetchadd_int(&query_set->queries_completed, +1) != (AST_VECTOR_SIZE(&query_set->queries) - 1)) {
		ao2_ref(query_set, -1);
		return;
	}

	/* All queries have been completed, invoke final callback */
	if (query_set->queries_cancelled != AST_VECTOR_SIZE(&query_set->queries)) {
		query_set->callback(query_set);
	}

	ao2_cleanup(query_set->user_data);
	query_set->user_data = NULL;

	ao2_ref(query_set, -1);
}

int ast_dns_query_set_add(struct ast_dns_query_set *query_set, const char *name, int rr_type, int rr_class)
{
	struct dns_query_set_query query = {
		.started = 0,
	};

	ast_assert(!query_set->in_progress);
	if (query_set->in_progress) {
		ast_log(LOG_ERROR, "Attempted to add additional query to query set '%p' after resolution has started\n",
			query_set);
		return -1;
	}

	/*
	 * We are intentionally passing NULL for the user data even
	 * though dns_query_set_callback() is not NULL tolerant.  Doing
	 * this avoids a circular reference chain until the queries are
	 * started.  ast_dns_query_set_resolve_async() will set the
	 * query user_data for us later when we actually kick off the
	 * queries.
	 */
	query.query = dns_query_alloc(name, rr_type, rr_class, dns_query_set_callback, NULL);
	if (!query.query) {
		return -1;
	}

	if (AST_VECTOR_APPEND(&query_set->queries, query)) {
		ao2_ref(query.query, -1);
		return -1;
	}

	return 0;
}

size_t ast_dns_query_set_num_queries(const struct ast_dns_query_set *query_set)
{
	return AST_VECTOR_SIZE(&query_set->queries);
}

struct ast_dns_query *ast_dns_query_set_get(const struct ast_dns_query_set *query_set, unsigned int index)
{
	/* Only once all queries have been completed can results be retrieved */
	if (query_set->queries_completed != AST_VECTOR_SIZE(&query_set->queries)) {
		return NULL;
	}

	/* If the index exceeds the number of queries... no query for you */
	if (index >= AST_VECTOR_SIZE(&query_set->queries)) {
		return NULL;
	}

	return AST_VECTOR_GET_ADDR(&query_set->queries, index)->query;
}

void *ast_dns_query_set_get_data(const struct ast_dns_query_set *query_set)
{
	return query_set->user_data;
}

void ast_dns_query_set_resolve_async(struct ast_dns_query_set *query_set, ast_dns_query_set_callback callback, void *data)
{
	int idx;

	ast_assert(!query_set->in_progress);
	if (query_set->in_progress) {
		ast_log(LOG_ERROR, "Attempted to start asynchronous resolution of query set '%p' when it has already started\n",
			query_set);
		return;
	}

	query_set->in_progress = 1;
	query_set->callback = callback;
	query_set->user_data = ao2_bump(data);

	/*
	 * Bump the query_set ref in case all queries complete
	 * before we are done kicking them off.
	 */
	ao2_ref(query_set, +1);
	for (idx = 0; idx < AST_VECTOR_SIZE(&query_set->queries); ++idx) {
		struct dns_query_set_query *query = AST_VECTOR_GET_ADDR(&query_set->queries, idx);

		query->query->user_data = ao2_bump(query_set);

		if (!query->query->resolver->resolve(query->query)) {
			query->started = 1;
			continue;
		}

		dns_query_set_callback(query->query);
	}
	if (!idx) {
		/*
		 * There were no queries in the set;
		 * therefore all queries are "completed".
		 * Invoke the final callback.
		 */
		query_set->callback(query_set);
		ao2_cleanup(query_set->user_data);
		query_set->user_data = NULL;
	}
	ao2_ref(query_set, -1);
}

/*! \brief Structure used for signaling back for synchronous resolution completion */
struct dns_synchronous_resolve {
	/*! \brief Lock used for signaling */
	ast_mutex_t lock;
	/*! \brief Condition used for signaling */
	ast_cond_t cond;
	/*! \brief Whether the query has completed */
	unsigned int completed;
};

/*! \brief Destructor for synchronous resolution structure */
static void dns_synchronous_resolve_destroy(void *data)
{
	struct dns_synchronous_resolve *synchronous = data;

	ast_mutex_destroy(&synchronous->lock);
	ast_cond_destroy(&synchronous->cond);
}

/*! \brief Callback used to implement synchronous resolution */
static void dns_synchronous_resolve_callback(const struct ast_dns_query_set *query_set)
{
	struct dns_synchronous_resolve *synchronous = ast_dns_query_set_get_data(query_set);

	ast_mutex_lock(&synchronous->lock);
	synchronous->completed = 1;
	ast_cond_signal(&synchronous->cond);
	ast_mutex_unlock(&synchronous->lock);
}

int ast_query_set_resolve(struct ast_dns_query_set *query_set)
{
	struct dns_synchronous_resolve *synchronous;

	synchronous = ao2_alloc_options(sizeof(*synchronous), dns_synchronous_resolve_destroy, AO2_ALLOC_OPT_LOCK_NOLOCK);
	if (!synchronous) {
		return -1;
	}

	ast_mutex_init(&synchronous->lock);
	ast_cond_init(&synchronous->cond, NULL);

	ast_dns_query_set_resolve_async(query_set, dns_synchronous_resolve_callback, synchronous);

	/* Wait for resolution to complete */
	ast_mutex_lock(&synchronous->lock);
	while (!synchronous->completed) {
		ast_cond_wait(&synchronous->cond, &synchronous->lock);
	}
	ast_mutex_unlock(&synchronous->lock);

	ao2_ref(synchronous, -1);

	return 0;
}

int ast_dns_query_set_resolve_cancel(struct ast_dns_query_set *query_set)
{
	int idx;
	size_t query_count = AST_VECTOR_SIZE(&query_set->queries);

	for (idx = 0; idx < AST_VECTOR_SIZE(&query_set->queries); ++idx) {
		struct dns_query_set_query *query = AST_VECTOR_GET_ADDR(&query_set->queries, idx);

		if (query->started) {
			if (!query->query->resolver->cancel(query->query)) {
				query_set->queries_cancelled++;
				dns_query_set_callback(query->query);
			}
		} else {
			query_set->queries_cancelled++;
		}
	}

	return (query_set->queries_cancelled == query_count) ? 0 : -1;
}