147c737f6f31b43cd0c3cbb09c88c4539d0e50b4
[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         AST_VECTOR_APPEND(&query_set->queries, query);
134
135         return 0;
136 }
137
138 size_t ast_dns_query_set_num_queries(const struct ast_dns_query_set *query_set)
139 {
140         return AST_VECTOR_SIZE(&query_set->queries);
141 }
142
143 struct ast_dns_query *ast_dns_query_set_get(const struct ast_dns_query_set *query_set, unsigned int index)
144 {
145         /* Only once all queries have been completed can results be retrieved */
146         if (query_set->queries_completed != AST_VECTOR_SIZE(&query_set->queries)) {
147                 return NULL;
148         }
149
150         /* If the index exceeds the number of queries... no query for you */
151         if (index >= AST_VECTOR_SIZE(&query_set->queries)) {
152                 return NULL;
153         }
154
155         return AST_VECTOR_GET_ADDR(&query_set->queries, index)->query;
156 }
157
158 void *ast_dns_query_set_get_data(const struct ast_dns_query_set *query_set)
159 {
160         return query_set->user_data;
161 }
162
163 void ast_dns_query_set_resolve_async(struct ast_dns_query_set *query_set, ast_dns_query_set_callback callback, void *data)
164 {
165         int idx;
166
167         ast_assert(!query_set->in_progress);
168         if (query_set->in_progress) {
169                 ast_log(LOG_ERROR, "Attempted to start asynchronous resolution of query set '%p' when it has already started\n",
170                         query_set);
171                 return;
172         }
173
174         query_set->in_progress = 1;
175         query_set->callback = callback;
176         query_set->user_data = ao2_bump(data);
177
178         for (idx = 0; idx < AST_VECTOR_SIZE(&query_set->queries); ++idx) {
179                 struct dns_query_set_query *query = AST_VECTOR_GET_ADDR(&query_set->queries, idx);
180
181                 query->query->user_data = ao2_bump(query_set);
182
183                 if (!query->query->resolver->resolve(query->query)) {
184                         query->started = 1;
185                         continue;
186                 }
187
188                 dns_query_set_callback(query->query);
189         }
190 }
191
192 /*! \brief Structure used for signaling back for synchronous resolution completion */
193 struct dns_synchronous_resolve {
194         /*! \brief Lock used for signaling */
195         ast_mutex_t lock;
196         /*! \brief Condition used for signaling */
197         ast_cond_t cond;
198         /*! \brief Whether the query has completed */
199         unsigned int completed;
200 };
201
202 /*! \brief Destructor for synchronous resolution structure */
203 static void dns_synchronous_resolve_destroy(void *data)
204 {
205         struct dns_synchronous_resolve *synchronous = data;
206
207         ast_mutex_destroy(&synchronous->lock);
208         ast_cond_destroy(&synchronous->cond);
209 }
210
211 /*! \brief Callback used to implement synchronous resolution */
212 static void dns_synchronous_resolve_callback(const struct ast_dns_query_set *query_set)
213 {
214         struct dns_synchronous_resolve *synchronous = ast_dns_query_set_get_data(query_set);
215
216         ast_mutex_lock(&synchronous->lock);
217         synchronous->completed = 1;
218         ast_cond_signal(&synchronous->cond);
219         ast_mutex_unlock(&synchronous->lock);
220 }
221
222 int ast_query_set_resolve(struct ast_dns_query_set *query_set)
223 {
224         struct dns_synchronous_resolve *synchronous;
225
226         synchronous = ao2_alloc_options(sizeof(*synchronous), dns_synchronous_resolve_destroy, AO2_ALLOC_OPT_LOCK_NOLOCK);
227         if (!synchronous) {
228                 return -1;
229         }
230
231         ast_mutex_init(&synchronous->lock);
232         ast_cond_init(&synchronous->cond, NULL);
233
234         ast_dns_query_set_resolve_async(query_set, dns_synchronous_resolve_callback, synchronous);
235
236         /* Wait for resolution to complete */
237         ast_mutex_lock(&synchronous->lock);
238         while (!synchronous->completed) {
239                 ast_cond_wait(&synchronous->cond, &synchronous->lock);
240         }
241         ast_mutex_unlock(&synchronous->lock);
242
243         ao2_ref(synchronous, -1);
244
245         return 0;
246 }
247
248 int ast_dns_query_set_resolve_cancel(struct ast_dns_query_set *query_set)
249 {
250         int idx;
251         size_t query_count = AST_VECTOR_SIZE(&query_set->queries);
252
253         for (idx = 0; idx < AST_VECTOR_SIZE(&query_set->queries); ++idx) {
254                 struct dns_query_set_query *query = AST_VECTOR_GET_ADDR(&query_set->queries, idx);
255
256                 if (query->started) {
257                         if (!query->query->resolver->cancel(query->query)) {
258                                 query_set->queries_cancelled++;
259                                 dns_query_set_callback(query->query);
260                         }
261                 } else {
262                         query_set->queries_cancelled++;
263                 }
264         }
265
266         return (query_set->queries_cancelled == query_count) ? 0 : -1;
267 }