res_pjsip: Add external PJSIP resolver implementation using core DNS API.
[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 Release all queries held in a query set */
47 static void dns_query_set_release(struct ast_dns_query_set *query_set)
48 {
49         int idx;
50
51         for (idx = 0; idx < AST_VECTOR_SIZE(&query_set->queries); ++idx) {
52                 struct dns_query_set_query *query = AST_VECTOR_GET_ADDR(&query_set->queries, idx);
53
54                 ao2_ref(query->query, -1);
55         }
56
57         AST_VECTOR_FREE(&query_set->queries);
58 }
59
60 /*! \brief Destructor for DNS query set */
61 static void dns_query_set_destroy(void *data)
62 {
63         struct ast_dns_query_set *query_set = data;
64
65         dns_query_set_release(query_set);
66         ao2_cleanup(query_set->user_data);
67 }
68
69 struct ast_dns_query_set *ast_dns_query_set_create(void)
70 {
71         struct ast_dns_query_set *query_set;
72
73         query_set = ao2_alloc_options(sizeof(*query_set), dns_query_set_destroy, AO2_ALLOC_OPT_LOCK_NOLOCK);
74         if (!query_set) {
75                 return NULL;
76         }
77
78         if (AST_VECTOR_INIT(&query_set->queries, DNS_QUERY_SET_EXPECTED_QUERY_COUNT)) {
79                 ao2_ref(query_set, -1);
80                 return NULL;
81         }
82
83         return query_set;
84 }
85
86 /*! \brief Callback invoked upon completion of a DNS query */
87 static void dns_query_set_callback(const struct ast_dns_query *query)
88 {
89         struct ast_dns_query_set *query_set = ast_dns_query_get_data(query);
90
91         if (ast_atomic_fetchadd_int(&query_set->queries_completed, +1) != (AST_VECTOR_SIZE(&query_set->queries) - 1)) {
92                 return;
93         }
94
95         /* All queries have been completed, invoke final callback */
96         if (query_set->queries_cancelled != AST_VECTOR_SIZE(&query_set->queries)) {
97                 query_set->callback(query_set);
98         }
99
100         ao2_cleanup(query_set->user_data);
101         query_set->user_data = NULL;
102
103         dns_query_set_release(query_set);
104 }
105
106 int ast_dns_query_set_add(struct ast_dns_query_set *query_set, const char *name, int rr_type, int rr_class)
107 {
108         struct dns_query_set_query query = {
109                 .started = 0,
110         };
111
112         ast_assert(!query_set->in_progress);
113         if (query_set->in_progress) {
114                 ast_log(LOG_ERROR, "Attempted to add additional query to query set '%p' after resolution has started\n",
115                         query_set);
116                 return -1;
117         }
118
119         query.query = dns_query_alloc(name, rr_type, rr_class, dns_query_set_callback, query_set);
120         if (!query.query) {
121                 return -1;
122         }
123
124         AST_VECTOR_APPEND(&query_set->queries, query);
125
126         return 0;
127 }
128
129 size_t ast_dns_query_set_num_queries(const struct ast_dns_query_set *query_set)
130 {
131         return AST_VECTOR_SIZE(&query_set->queries);
132 }
133
134 struct ast_dns_query *ast_dns_query_set_get(const struct ast_dns_query_set *query_set, unsigned int index)
135 {
136         /* Only once all queries have been completed can results be retrieved */
137         if (query_set->queries_completed != AST_VECTOR_SIZE(&query_set->queries)) {
138                 return NULL;
139         }
140
141         /* If the index exceeds the number of queries... no query for you */
142         if (index >= AST_VECTOR_SIZE(&query_set->queries)) {
143                 return NULL;
144         }
145
146         return AST_VECTOR_GET_ADDR(&query_set->queries, index)->query;
147 }
148
149 void *ast_dns_query_set_get_data(const struct ast_dns_query_set *query_set)
150 {
151         return query_set->user_data;
152 }
153
154 void ast_dns_query_set_resolve_async(struct ast_dns_query_set *query_set, ast_dns_query_set_callback callback, void *data)
155 {
156         int idx;
157
158         ast_assert(!query_set->in_progress);
159         if (query_set->in_progress) {
160                 ast_log(LOG_ERROR, "Attempted to start asynchronous resolution of query set '%p' when it has already started\n",
161                         query_set);
162                 return;
163         }
164
165         query_set->in_progress = 1;
166         query_set->callback = callback;
167         query_set->user_data = ao2_bump(data);
168
169         for (idx = 0; idx < AST_VECTOR_SIZE(&query_set->queries); ++idx) {
170                 struct dns_query_set_query *query = AST_VECTOR_GET_ADDR(&query_set->queries, idx);
171
172                 if (!query->query->resolver->resolve(query->query)) {
173                         query->started = 1;
174                         continue;
175                 }
176
177                 dns_query_set_callback(query->query);
178         }
179 }
180
181 /*! \brief Structure used for signaling back for synchronous resolution completion */
182 struct dns_synchronous_resolve {
183         /*! \brief Lock used for signaling */
184         ast_mutex_t lock;
185         /*! \brief Condition used for signaling */
186         ast_cond_t cond;
187         /*! \brief Whether the query has completed */
188         unsigned int completed;
189 };
190
191 /*! \brief Destructor for synchronous resolution structure */
192 static void dns_synchronous_resolve_destroy(void *data)
193 {
194         struct dns_synchronous_resolve *synchronous = data;
195
196         ast_mutex_destroy(&synchronous->lock);
197         ast_cond_destroy(&synchronous->cond);
198 }
199
200 /*! \brief Callback used to implement synchronous resolution */
201 static void dns_synchronous_resolve_callback(const struct ast_dns_query_set *query_set)
202 {
203         struct dns_synchronous_resolve *synchronous = ast_dns_query_set_get_data(query_set);
204
205         ast_mutex_lock(&synchronous->lock);
206         synchronous->completed = 1;
207         ast_cond_signal(&synchronous->cond);
208         ast_mutex_unlock(&synchronous->lock);
209 }
210
211 int ast_query_set_resolve(struct ast_dns_query_set *query_set)
212 {
213         struct dns_synchronous_resolve *synchronous;
214
215         synchronous = ao2_alloc_options(sizeof(*synchronous), dns_synchronous_resolve_destroy, AO2_ALLOC_OPT_LOCK_NOLOCK);
216         if (!synchronous) {
217                 return -1;
218         }
219
220         ast_mutex_init(&synchronous->lock);
221         ast_cond_init(&synchronous->cond, NULL);
222
223         ast_dns_query_set_resolve_async(query_set, dns_synchronous_resolve_callback, synchronous);
224
225         /* Wait for resolution to complete */
226         ast_mutex_lock(&synchronous->lock);
227         while (!synchronous->completed) {
228                 ast_cond_wait(&synchronous->cond, &synchronous->lock);
229         }
230         ast_mutex_unlock(&synchronous->lock);
231
232         ao2_ref(synchronous, -1);
233
234         return 0;
235 }
236
237 int ast_dns_query_set_resolve_cancel(struct ast_dns_query_set *query_set)
238 {
239         int idx;
240         size_t query_count = AST_VECTOR_SIZE(&query_set->queries);
241
242         for (idx = 0; idx < AST_VECTOR_SIZE(&query_set->queries); ++idx) {
243                 struct dns_query_set_query *query = AST_VECTOR_GET_ADDR(&query_set->queries, idx);
244
245                 if (query->started) {
246                         if (!query->query->resolver->cancel(query->query)) {
247                                 query_set->queries_cancelled++;
248                                 dns_query_set_callback(query->query);
249                         }
250                 } else {
251                         query_set->queries_cancelled++;
252                 }
253         }
254
255         return (query_set->queries_cancelled == query_count) ? 0 : -1;
256 }