3ebbab070c4e1558839d4f6d816d69ce6ce9605c
[asterisk/asterisk.git] / main / dns_recurring.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 Recurring Query Support
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_FILE_VERSION(__FILE__, "$Revision$")
33
34 #include "asterisk/astobj2.h"
35 #include "asterisk/linkedlists.h"
36 #include "asterisk/sched.h"
37 #include "asterisk/strings.h"
38 #include "asterisk/dns_core.h"
39 #include "asterisk/dns_recurring.h"
40 #include "asterisk/dns_internal.h"
41
42 #include <arpa/nameser.h>
43
44 /*! \brief Destructor for a DNS query */
45 static void dns_query_recurring_destroy(void *data)
46 {
47         struct ast_dns_query_recurring *recurring = data;
48
49         ao2_cleanup(recurring->user_data);
50 }
51
52 static void dns_query_recurring_resolution_callback(const struct ast_dns_query *query);
53
54 /*! \brief Scheduled recurring query callback */
55 static int dns_query_recurring_scheduled_callback(const void *data)
56 {
57         struct ast_dns_query_recurring *recurring = (struct ast_dns_query_recurring *)data;
58
59         ao2_lock(recurring);
60         recurring->timer = -1;
61         if (!recurring->cancelled) {
62                 recurring->active = ast_dns_resolve_async(recurring->name, recurring->rr_type, recurring->rr_class, dns_query_recurring_resolution_callback,
63                         recurring);
64         }
65         ao2_unlock(recurring);
66
67         ao2_ref(recurring, -1);
68
69         return 0;
70 }
71
72 /*! \brief Query resolution callback */
73 static void dns_query_recurring_resolution_callback(const struct ast_dns_query *query)
74 {
75         struct ast_dns_query_recurring *recurring = ast_dns_query_get_data(query);
76
77         /* Replace the user data so the actual callback sees what it provided */
78         ((struct ast_dns_query*)query)->user_data = ao2_bump(recurring->user_data);
79         recurring->callback(query);
80
81         ao2_lock(recurring);
82         /* So.. if something has not externally cancelled this we can reschedule based on the TTL */
83         if (!recurring->cancelled) {
84                 const struct ast_dns_result *result = ast_dns_query_get_result(query);
85                 int ttl = MIN(ast_dns_result_get_lowest_ttl(result), INT_MAX / 1000);
86
87                 if (ttl) {
88                         recurring->timer = ast_sched_add(ast_dns_get_sched(), ttl * 1000, dns_query_recurring_scheduled_callback, ao2_bump(recurring));
89                         if (recurring->timer < 0) {
90                                 /* It is impossible for this to be the last reference as this callback function holds a reference itself */
91                                 ao2_ref(recurring, -1);
92                         }
93                 }
94         }
95
96         ao2_replace(recurring->active, NULL);
97         ao2_unlock(recurring);
98
99         /* Since we stole the reference from the query we need to drop it ourselves */
100         ao2_ref(recurring, -1);
101 }
102
103 struct ast_dns_query_recurring *ast_dns_resolve_recurring(const char *name, int rr_type, int rr_class, ast_dns_resolve_callback callback, void *data)
104 {
105         struct ast_dns_query_recurring *recurring;
106
107         if (ast_strlen_zero(name) || !callback || !ast_dns_get_sched()) {
108                 return NULL;
109         }
110
111         recurring = ao2_alloc(sizeof(*recurring) + strlen(name) + 1, dns_query_recurring_destroy);
112         if (!recurring) {
113                 return NULL;
114         }
115
116         recurring->callback = callback;
117         recurring->user_data = ao2_bump(data);
118         recurring->timer = -1;
119         recurring->rr_type = rr_type;
120         recurring->rr_class = rr_class;
121         strcpy(recurring->name, name); /* SAFE */
122
123         recurring->active = ast_dns_resolve_async(name, rr_type, rr_class, dns_query_recurring_resolution_callback, recurring);
124         if (!recurring->active) {
125                 ao2_ref(recurring, -1);
126                 return NULL;
127         }
128
129         return recurring;
130 }
131
132 int ast_dns_resolve_recurring_cancel(struct ast_dns_query_recurring *recurring)
133 {
134         int res = 0;
135
136         ao2_lock(recurring);
137
138         recurring->cancelled = 1;
139         AST_SCHED_DEL_UNREF(ast_dns_get_sched(), recurring->timer, ao2_ref(recurring, -1));
140
141         if (recurring->active) {
142                 res = ast_dns_resolve_cancel(recurring->active);
143                 ao2_replace(recurring->active, NULL);
144         }
145
146         ao2_unlock(recurring);
147
148         return res;
149 }