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