Debian Codegraph @

main/dns_recurring.c

raw symbols SCIP
1 /*
2 * Asterisk -- An open source telephony toolkit.
3 *
4 * Copyright (C) 2015, Digium, Inc.
5 *
6 * Joshua Colp <[email protected]>
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 <[email protected]>
24 */
25
26 /*** MODULEINFO
27 <support_level>core</support_level>
28 ***/
29
30 #include "asterisk.h"
31
32 #include "asterisk/astobj2.h"
33 #include "asterisk/linkedlists.h"
34 #include "asterisk/sched.h"
35 #include "asterisk/strings.h"
36 #include "asterisk/dns_core.h"
37 #include "asterisk/dns_recurring.h"
38 #include "asterisk/dns_internal.h"
39
40 #include <arpa/nameser.h>
41
42 /*! \brief Delay between TTL expiration and the next DNS query, to make sure the
43 resolver cache really expired. */
44 #define EXTRA_TTL 2
45 #define MAX_TTL ((INT_MAX - EXTRA_TTL) / 1000)
46
47 /*! \brief Destructor for a DNS query */
48 static void dns_query_recurring_destroy(void *data)
49 {
50 struct ast_dns_query_recurring *recurring = data;
51
52 ao2_cleanup(recurring->user_data);
53 }
54
55 static void dns_query_recurring_resolution_callback(const struct ast_dns_query *query);
56
57 /*! \brief Scheduled recurring query callback */
58 static int dns_query_recurring_scheduled_callback(const void *data)
59 {
60 struct ast_dns_query_recurring *recurring = (struct ast_dns_query_recurring *)data;
61
62 ao2_lock(recurring);
63 recurring->timer = -1;
64 if (!recurring->cancelled) {
65 recurring->active = ast_dns_resolve_async(recurring->name, recurring->rr_type, recurring->rr_class, dns_query_recurring_resolution_callback,
66 recurring);
67 }
68 ao2_unlock(recurring);
69
70 ao2_ref(recurring, -1);
71
72 return 0;
73 }
74
75 /*! \brief Query resolution callback */
76 static void dns_query_recurring_resolution_callback(const struct ast_dns_query *query)
77 {
78 struct ast_dns_query_recurring *recurring = ast_dns_query_get_data(query);
79 struct ast_dns_query *callback_query;
80
81 /* Create a separate query to invoke the user specific callback on as the
82 * recurring query user data may get used externally (by the unit test)
83 * and thus changing it is problematic
84 */
85 callback_query = dns_query_alloc(query->name, query->rr_type, query->rr_class,
86 recurring->callback, recurring->user_data);
87 if (callback_query) {
88 /* The result is immutable at this point and can be safely provided */
89 callback_query->result = query->result;
90 callback_query->callback(callback_query);
91 callback_query->result = NULL;
92 ao2_ref(callback_query, -1);
93 }
94
95 ao2_lock(recurring);
96 /* So.. if something has not externally cancelled this we can reschedule based on the TTL */
97 if (!recurring->cancelled) {
98 const struct ast_dns_result *result = ast_dns_query_get_result(query);
99 int ttl = MIN(ast_dns_result_get_lowest_ttl(result), MAX_TTL);
100
101 if (ttl) {
102 recurring->timer = ast_sched_add(ast_dns_get_sched(), (ttl + EXTRA_TTL) * 1000, dns_query_recurring_scheduled_callback, ao2_bump(recurring));
103 if (recurring->timer < 0) {
104 /* It is impossible for this to be the last reference as the query has a reference to it */
105 ao2_ref(recurring, -1);
106 }
107 ast_debug(2, "Rescheduled resolution for name: %s class: %d type: %d in %d seconds",
108 ast_dns_query_get_name(query), ast_dns_query_get_rr_class(query),
109 ast_dns_query_get_rr_type(query), ttl + EXTRA_TTL);
110 } else {
111 ast_debug(2, "TTL = 0 so not rescheduling resolution for name: %s class: %d type: %d",
112 ast_dns_query_get_name(query), ast_dns_query_get_rr_class(query), ast_dns_query_get_rr_type(query));
113 }
114 } else {
115 ast_debug(2, "Recurring resolution cancelled for name: %s class: %d type: %d",
116 ast_dns_query_get_name(query), ast_dns_query_get_rr_class(query), ast_dns_query_get_rr_type(query));
117 }
118
119 ao2_replace(recurring->active, NULL);
120 ao2_unlock(recurring);
121 }
122
123 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)
124 {
125 struct ast_dns_query_recurring *recurring;
126
127 if (ast_strlen_zero(name) || !callback || !ast_dns_get_sched()) {
128 return NULL;
129 }
130
131 recurring = ao2_alloc(sizeof(*recurring) + strlen(name) + 1, dns_query_recurring_destroy);
132 if (!recurring) {
133 return NULL;
134 }
135
136 recurring->callback = callback;
137 recurring->user_data = ao2_bump(data);
138 recurring->timer = -1;
139 recurring->rr_type = rr_type;
140 recurring->rr_class = rr_class;
141 strcpy(recurring->name, name); /* SAFE */
142
143 recurring->active = ast_dns_resolve_async(name, rr_type, rr_class, dns_query_recurring_resolution_callback, recurring);
144 if (!recurring->active) {
145 ao2_ref(recurring, -1);
146 return NULL;
147 }
148
149 return recurring;
150 }
151
152 int ast_dns_resolve_recurring_cancel(struct ast_dns_query_recurring *recurring)
153 {
154 int res = 0;
155
156 ao2_lock(recurring);
157
158 ast_debug(2, "Cancelling recurring resolution for name: %s class: %d type: %d",
159 recurring->name, recurring->rr_class, recurring->rr_type);
160
161 recurring->cancelled = 1;
162 AST_SCHED_DEL_UNREF(ast_dns_get_sched(), recurring->timer, ao2_ref(recurring, -1));
163
164 if (recurring->active) {
165 res = ast_dns_resolve_cancel(recurring->active);
166 ao2_replace(recurring->active, NULL);
167 }
168
169 ao2_unlock(recurring);
170
171 return res;
172 }