Merge "asterisk.c: When astcanary dies on linux, reset priority on all threads."
[asterisk/asterisk.git] / main / dns_query_set.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2015, Digium, Inc.
5  *
6  * Joshua Colp <jcolp@digium.com>
7  *
8  * See http://www.asterisk.org for more information about
9  * the Asterisk project. Please do not directly contact
10  * any of the maintainers of this project for assistance;
11  * the project provides a web site, mailing lists and IRC
12  * channels for your use.
13  *
14  * This program is free software, distributed under the terms of
15  * the GNU General Public License Version 2. See the LICENSE file
16  * at the top of the source tree.
17  */
18
19 /*! \file
20  *
21  * \brief DNS Query Set API
22  *
23  * \author Joshua Colp <jcolp@digium.com>
24  */
25
26 /*** MODULEINFO
27         <support_level>core</support_level>
28  ***/
29
30 #include "asterisk.h"
31
32 ASTERISK_REGISTER_FILE()
33
34 #include "asterisk/vector.h"
35 #include "asterisk/astobj2.h"
36 #include "asterisk/utils.h"
37 #include "asterisk/linkedlists.h"
38 #include "asterisk/dns_core.h"
39 #include "asterisk/dns_query_set.h"
40 #include "asterisk/dns_internal.h"
41 #include "asterisk/dns_resolver.h"
42
43 /*! \brief The default number of expected queries to be added to the query set */
44 #define DNS_QUERY_SET_EXPECTED_QUERY_COUNT 5
45
46 /*! \brief Destructor for DNS query set */
47 static void dns_query_set_destroy(void *data)
48 {
49         struct ast_dns_query_set *query_set = data;
50         int idx;
51
52         for (idx = 0; idx < AST_VECTOR_SIZE(&query_set->queries); ++idx) {
53                 struct dns_query_set_query *query = AST_VECTOR_GET_ADDR(&query_set->queries, idx);
54
55                 ao2_ref(query->query, -1);
56         }
57         AST_VECTOR_FREE(&query_set->queries);
58
59         ao2_cleanup(query_set->user_data);
60 }
61
62 struct ast_dns_query_set *ast_dns_query_set_create(void)
63 {
64         struct ast_dns_query_set *query_set;
65
66         query_set = ao2_alloc_options(sizeof(*query_set), dns_query_set_destroy, AO2_ALLOC_OPT_LOCK_NOLOCK);
67         if (!query_set) {
68                 return NULL;
69         }
70
71         if (AST_VECTOR_INIT(&query_set->queries, DNS_QUERY_SET_EXPECTED_QUERY_COUNT)) {
72                 ao2_ref(query_set, -1);
73                 return NULL;
74         }
75
76         return query_set;
77 }
78
79 /*! \brief Callback invoked upon completion of a DNS query */
80 static void dns_query_set_callback(const struct ast_dns_query *query)
81 {
82         struct ast_dns_query_set *query_set = ast_dns_query_get_data(query);
83
84         /* The reference count of the query set is bumped here in case this query holds the last reference */
85         ao2_ref(query_set, +1);
86
87         /* Drop the query set from the query so the query set can be destroyed if this is the last one */
88         ao2_cleanup(((struct ast_dns_query *)query)->user_data);
89         ((struct ast_dns_query *)query)->user_data = NULL;
90
91         if (ast_atomic_fetchadd_int(&query_set->queries_completed, +1) != (AST_VECTOR_SIZE(&query_set->queries) - 1)) {
92                 ao2_ref(query_set, -1);
93                 return;
94         }
95
96         /* All queries have been completed, invoke final callback */
97         if (query_set->queries_cancelled != AST_VECTOR_SIZE(&query_set->queries)) {
98                 query_set->callback(query_set);
99         }
100
101         ao2_cleanup(query_set->user_data);
102         query_set->user_data = NULL;
103
104         ao2_ref(query_set, -1);
105 }
106
107 int ast_dns_query_set_add(struct ast_dns_query_set *query_set, const char *name, int rr_type, int rr_class)
108 {
109         struct dns_query_set_query query = {
110                 .started = 0,
111         };
112
113         ast_assert(!query_set->in_progress);
114         if (query_set->in_progress) {
115                 ast_log(LOG_ERROR, "Attempted to add additional query to query set '%p' after resolution has started\n",
116                         query_set);
117                 return -1;
118         }
119
120         /*
121          * We are intentionally passing NULL for the user data even
122          * though dns_query_set_callback() is not NULL tolerant.  Doing
123          * this avoids a circular reference chain until the queries are
124          * started.  ast_dns_query_set_resolve_async() will set the
125          * query user_data for us later when we actually kick off the
126          * queries.
127          */
128         query.query = dns_query_alloc(name, rr_type, rr_class, dns_query_set_callback, NULL);
129         if (!query.query) {
130                 return -1;
131         }
132
133         if (AST_VECTOR_APPEND(&query_set->queries, query)) {
134                 ao2_ref(query.query, -1);
135                 return -1;
136         }
137
138         return 0;
139 }
140
141 size_t ast_dns_query_set_num_queries(const struct ast_dns_query_set *query_set)
142 {
143         return AST_VECTOR_SIZE(&query_set->queries);
144 }
145
146 struct ast_dns_query *ast_dns_query_set_get(const struct ast_dns_query_set *query_set, unsigned int index)
147 {
148         /* Only once all queries have been completed can results be retrieved */
149         if (query_set->queries_completed != AST_VECTOR_SIZE(&query_set->queries)) {
150                 return NULL;
151         }
152
153         /* If the index exceeds the number of queries... no query for you */
154         if (index >= AST_VECTOR_SIZE(&query_set->queries)) {
155                 return NULL;
156         }
157
158         return AST_VECTOR_GET_ADDR(&query_set->queries, index)->query;
159 }
160
161 void *ast_dns_query_set_get_data(const struct ast_dns_query_set *query_set)
162 {
163         return query_set->user_data;
164 }
165
166 void ast_dns_query_set_resolve_async(struct ast_dns_query_set *query_set, ast_dns_query_set_callback callback, void *data)
167 {
168         int idx;
169
170         ast_assert(!query_set->in_progress);
171         if (query_set->in_progress) {
172                 ast_log(LOG_ERROR, "Attempted to start asynchronous resolution of query set '%p' when it has already started\n",
173                         query_set);
174                 return;
175         }
176
177         query_set->in_progress = 1;
178         query_set->callback = callback;
179         query_set->user_data = ao2_bump(data);
180
181         /*
182          * Bump the query_set ref in case all queries complete
183          * before we are done kicking them off.
184          */
185         ao2_ref(query_set, +1);
186         for (idx = 0; idx < AST_VECTOR_SIZE(&query_set->queries); ++idx) {
187                 struct dns_query_set_query *query = AST_VECTOR_GET_ADDR(&query_set->queries, idx);
188
189                 query->query->user_data = ao2_bump(query_set);
190
191                 if (!query->query->resolver->resolve(query->query)) {
192                         query->started = 1;
193                         continue;
194                 }
195
196                 dns_query_set_callback(query->query);
197         }
198         if (!idx) {
199                 /*
200                  * There were no queries in the set;
201                  * therefore all queries are "completed".
202                  * Invoke the final callback.
203                  */
204                 query_set->callback(query_set);
205                 ao2_cleanup(query_set->user_data);
206                 query_set->user_data = NULL;
207         }
208         ao2_ref(query_set, -1);
209 }
210
211 /*! \brief Structure used for signaling back for synchronous resolution completion */
212 struct dns_synchronous_resolve {
213         /*! \brief Lock used for signaling */
214         ast_mutex_t lock;
215         /*! \brief Condition used for signaling */
216         ast_cond_t cond;
217         /*! \brief Whether the query has completed */
218         unsigned int completed;
219 };
220
221 /*! \brief Destructor for synchronous resolution structure */
222 static void dns_synchronous_resolve_destroy(void *data)
223 {
224         struct dns_synchronous_resolve *synchronous = data;
225
226         ast_mutex_destroy(&synchronous->lock);
227         ast_cond_destroy(&synchronous->cond);
228 }
229
230 /*! \brief Callback used to implement synchronous resolution */
231 static void dns_synchronous_resolve_callback(const struct ast_dns_query_set *query_set)
232 {
233         struct dns_synchronous_resolve *synchronous = ast_dns_query_set_get_data(query_set);
234
235         ast_mutex_lock(&synchronous->lock);
236         synchronous->completed = 1;
237         ast_cond_signal(&synchronous->cond);
238         ast_mutex_unlock(&synchronous->lock);
239 }
240
241 int ast_query_set_resolve(struct ast_dns_query_set *query_set)
242 {
243         struct dns_synchronous_resolve *synchronous;
244
245         synchronous = ao2_alloc_options(sizeof(*synchronous), dns_synchronous_resolve_destroy, AO2_ALLOC_OPT_LOCK_NOLOCK);
246         if (!synchronous) {
247                 return -1;
248         }
249
250         ast_mutex_init(&synchronous->lock);
251         ast_cond_init(&synchronous->cond, NULL);
252
253         ast_dns_query_set_resolve_async(query_set, dns_synchronous_resolve_callback, synchronous);
254
255         /* Wait for resolution to complete */
256         ast_mutex_lock(&synchronous->lock);
257         while (!synchronous->completed) {
258                 ast_cond_wait(&synchronous->cond, &synchronous->lock);
259         }
260         ast_mutex_unlock(&synchronous->lock);
261
262         ao2_ref(synchronous, -1);
263
264         return 0;
265 }
266
267 int ast_dns_query_set_resolve_cancel(struct ast_dns_query_set *query_set)
268 {
269         int idx;
270         size_t query_count = AST_VECTOR_SIZE(&query_set->queries);
271
272         for (idx = 0; idx < AST_VECTOR_SIZE(&query_set->queries); ++idx) {
273                 struct dns_query_set_query *query = AST_VECTOR_GET_ADDR(&query_set->queries, idx);
274
275                 if (query->started) {
276                         if (!query->query->resolver->cancel(query->query)) {
277                                 query_set->queries_cancelled++;
278                                 dns_query_set_callback(query->query);
279                         }
280                 } else {
281                         query_set->queries_cancelled++;
282                 }
283         }
284
285         return (query_set->queries_cancelled == query_count) ? 0 : -1;
286 }