Debian Codegraph @

main/dnsmgr.c

raw symbols SCIP
1 /*
2 * Asterisk -- An open source telephony toolkit.
3 *
4 * Copyright (C) 2005-2006, Kevin P. Fleming
5 *
6 * Kevin P. Fleming <[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 Background DNS update manager
22 *
23 * \author Kevin P. Fleming <[email protected]>
24 *
25 * \bug There is a minor race condition. In the event that an IP address
26 * of a dnsmgr managed host changes, there is the potential for the consumer
27 * of that address to access the in_addr data at the same time that the dnsmgr
28 * thread is in the middle of updating it to the new address.
29 */
30
31
32 /*! \li \ref dnsmgr.c uses the configuration file \ref dnsmgr.conf
33 * \addtogroup configuration_file Configuration Files
34 */
35
36 /*!
37 * \page dnsmgr.conf dnsmgr.conf
38 * \verbinclude dnsmgr.conf.sample
39 */
40
41
42 /*** MODULEINFO
43 <support_level>core</support_level>
44 ***/
45
46 #include "asterisk.h"
47
48 #include "asterisk/_private.h"
49 #include <regex.h>
50 #include <signal.h>
51
52 #include "asterisk/module.h"
53 #include "asterisk/dnsmgr.h"
54 #include "asterisk/linkedlists.h"
55 #include "asterisk/utils.h"
56 #include "asterisk/config.h"
57 #include "asterisk/sched.h"
58 #include "asterisk/cli.h"
59 #include "asterisk/manager.h"
60 #include "asterisk/acl.h"
61
62 static struct ast_sched_context *sched;
63 static int refresh_sched = -1;
64 static pthread_t refresh_thread = AST_PTHREADT_NULL;
65
66 struct ast_dnsmgr_entry {
67 /*! where we will store the resulting IP address and port number */
68 struct ast_sockaddr *result;
69 /*! SRV record to lookup, if provided. Composed of service, protocol, and domain name: _Service._Proto.Name */
70 char *service;
71 /*! Address family to filter DNS responses. */
72 unsigned int family;
73 /*! Set to 1 if the entry changes */
74 unsigned int changed:1;
75 /*! Data to pass back to update_func */
76 void *data;
77 /*! The callback function to execute on address update */
78 dns_update_func update_func;
79 ast_mutex_t lock;
80 AST_RWLIST_ENTRY(ast_dnsmgr_entry) list;
81 /*! just 1 here, but we use calloc to allocate the correct size */
82 char name[1];
83 };
84
85 static AST_RWLIST_HEAD_STATIC(entry_list, ast_dnsmgr_entry);
86
87 AST_MUTEX_DEFINE_STATIC(refresh_lock);
88
89 #define REFRESH_DEFAULT 300
90
91 static int enabled;
92 static int refresh_interval;
93
94 struct refresh_info {
95 struct entry_list *entries;
96 int verbose;
97 unsigned int regex_present:1;
98 regex_t filter;
99 };
100
101 static struct refresh_info master_refresh_info = {
102 .entries = &entry_list,
103 .verbose = 0,
104 };
105
106 struct ast_dnsmgr_entry *ast_dnsmgr_get_family(const char *name, struct ast_sockaddr *result, const char *service, unsigned int family)
107 {
108 struct ast_dnsmgr_entry *entry;
109 int total_size = sizeof(*entry) + strlen(name) + (service ? strlen(service) + 1 : 0);
110
111 if (!result || ast_strlen_zero(name) || !(entry = ast_calloc(1, total_size))) {
112 return NULL;
113 }
114
115 entry->result = result;
116 ast_mutex_init(&entry->lock);
117 strcpy(entry->name, name);
118 if (service) {
119 entry->service = ((char *) entry) + sizeof(*entry) + strlen(name);
120 strcpy(entry->service, service);
121 }
122 entry->family = family;
123
124 AST_RWLIST_WRLOCK(&entry_list);
125 AST_RWLIST_INSERT_HEAD(&entry_list, entry, list);
126 AST_RWLIST_UNLOCK(&entry_list);
127
128 return entry;
129 }
130
131 struct ast_dnsmgr_entry *ast_dnsmgr_get(const char *name, struct ast_sockaddr *result, const char *service)
132 {
133 return ast_dnsmgr_get_family(name, result, service, 0);
134 }
135
136 void ast_dnsmgr_release(struct ast_dnsmgr_entry *entry)
137 {
138 if (!entry) {
139 return;
140 }
141
142 AST_RWLIST_WRLOCK(&entry_list);
143 AST_RWLIST_REMOVE(&entry_list, entry, list);
144 AST_RWLIST_UNLOCK(&entry_list);
145 ast_debug(6, "removing dns manager for '%s'\n", entry->name);
146
147 ast_mutex_destroy(&entry->lock);
148 ast_free(entry);
149 }
150
151 static int internal_dnsmgr_lookup(const char *name, struct ast_sockaddr *result, struct ast_dnsmgr_entry **dnsmgr, const char *service, dns_update_func func, void *data)
152 {
153 unsigned int family;
154
155 if (ast_strlen_zero(name) || !result || !dnsmgr) {
156 return -1;
157 }
158
159 if (*dnsmgr && !strcasecmp((*dnsmgr)->name, name)) {
160 return 0;
161 }
162
163 /* Lookup address family filter. */
164 family = result->ss.ss_family;
165
166 /*
167 * If it's actually an IP address and not a name, there's no
168 * need for a managed lookup.
169 */
170 if (ast_sockaddr_parse(result, name, PARSE_PORT_FORBID)) {
171 return 0;
172 }
173
174 ast_debug(6, "doing dnsmgr_lookup for '%s'\n", name);
175
176 /* do a lookup now but add a manager so it will automagically get updated in the background */
177 ast_get_ip_or_srv(result, name, service);
178
179 /* if dnsmgr is not enable don't bother adding an entry */
180 if (!enabled) {
181 return 0;
182 }
183
184 ast_debug(6, "adding dns manager for '%s'\n", name);
185 *dnsmgr = ast_dnsmgr_get_family(name, result, service, family);
186 (*dnsmgr)->update_func = func;
187 (*dnsmgr)->data = data;
188 return !*dnsmgr;
189 }
190
191 int ast_dnsmgr_lookup(const char *name, struct ast_sockaddr *result, struct ast_dnsmgr_entry **dnsmgr, const char *service)
192 {
193 return internal_dnsmgr_lookup(name, result, dnsmgr, service, NULL, NULL);
194 }
195
196 int ast_dnsmgr_lookup_cb(const char *name, struct ast_sockaddr *result, struct ast_dnsmgr_entry **dnsmgr, const char *service, dns_update_func func, void *data)
197 {
198 return internal_dnsmgr_lookup(name, result, dnsmgr, service, func, data);
199 }
200
201 /*
202 * Refresh a dnsmgr entry
203 */
204 static int dnsmgr_refresh(struct ast_dnsmgr_entry *entry, int verbose)
205 {
206 struct ast_sockaddr tmp = { .len = 0, };
207 int changed = 0;
208
209 ast_mutex_lock(&entry->lock);
210
211 ast_debug(6, "refreshing '%s'\n", entry->name);
212
213 tmp.ss.ss_family = entry->family;
214 if (!ast_get_ip_or_srv_with_preference(&tmp, entry->name, entry->service, entry->result)) {
215 if (!ast_sockaddr_port(&tmp)) {
216 ast_sockaddr_set_port(&tmp, ast_sockaddr_port(entry->result));
217 }
218 if (ast_sockaddr_cmp(&tmp, entry->result)) {
219 const char *old_addr = ast_strdupa(ast_sockaddr_stringify(entry->result));
220 const char *new_addr = ast_strdupa(ast_sockaddr_stringify(&tmp));
221
222 if (entry->update_func) {
223 entry->update_func(entry->result, &tmp, entry->data);
224 } else {
225 ast_log(LOG_NOTICE, "dnssrv: host '%s' changed from %s to %s\n",
226 entry->name, old_addr, new_addr);
227
228 ast_sockaddr_copy(entry->result, &tmp);
229 changed = entry->changed = 1;
230 }
231 }
232 }
233
234 ast_mutex_unlock(&entry->lock);
235
236 return changed;
237 }
238
239 int ast_dnsmgr_refresh(struct ast_dnsmgr_entry *entry)
240 {
241 return dnsmgr_refresh(entry, 0);
242 }
243
244 /*
245 * Check if dnsmgr entry has changed from since last call to this function
246 */
247 int ast_dnsmgr_changed(struct ast_dnsmgr_entry *entry)
248 {
249 int changed;
250
251 ast_mutex_lock(&entry->lock);
252
253 changed = entry->changed;
254 entry->changed = 0;
255
256 ast_mutex_unlock(&entry->lock);
257
258 return changed;
259 }
260
261 static void *do_refresh(void *data)
262 {
263 for (;;) {
264 pthread_testcancel();
265 usleep((ast_sched_wait(sched)*1000));
266 pthread_testcancel();
267 ast_sched_runq(sched);
268 }
269 return NULL;
270 }
271
272 static int refresh_list(const void *data)
273 {
274 struct refresh_info *info = (struct refresh_info *)data;
275 struct ast_dnsmgr_entry *entry;
276
277 /* if a refresh or reload is already in progress, exit now */
278 if (ast_mutex_trylock(&refresh_lock)) {
279 if (info->verbose) {
280 ast_log(LOG_WARNING, "DNS Manager refresh already in progress.\n");
281 }
282 return -1;
283 }
284
285 ast_debug(6, "Refreshing DNS lookups.\n");
286 AST_RWLIST_RDLOCK(info->entries);
287 AST_RWLIST_TRAVERSE(info->entries, entry, list) {
288 if (info->regex_present && regexec(&info->filter, entry->name, 0, NULL, 0)) {
289 continue;
290 }
291
292 dnsmgr_refresh(entry, info->verbose);
293 }
294 AST_RWLIST_UNLOCK(info->entries);
295
296 ast_mutex_unlock(&refresh_lock);
297
298 /* automatically reschedule based on the interval */
299 return refresh_interval * 1000;
300 }
301
302 void dnsmgr_start_refresh(void)
303 {
304 if (refresh_sched > -1) {
305 AST_SCHED_DEL(sched, refresh_sched);
306 refresh_sched = ast_sched_add_variable(sched, 100, refresh_list, &master_refresh_info, 1);
307 }
308 }
309
310 static int do_reload(int loading);
311
312 static char *handle_cli_reload(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
313 {
314 switch (cmd) {
315 case CLI_INIT:
316 e->command = "dnsmgr reload";
317 e->usage =
318 "Usage: dnsmgr reload\n"
319 " Reloads the DNS manager configuration.\n";
320 return NULL;
321 case CLI_GENERATE:
322 return NULL;
323 }
324 if (a->argc > 2) {
325 return CLI_SHOWUSAGE;
326 }
327
328 do_reload(0);
329 return CLI_SUCCESS;
330 }
331
332 static char *handle_cli_refresh(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
333 {
334 struct refresh_info info = {
335 .entries = &entry_list,
336 .verbose = 1,
337 };
338 switch (cmd) {
339 case CLI_INIT:
340 e->command = "dnsmgr refresh";
341 e->usage =
342 "Usage: dnsmgr refresh [pattern]\n"
343 " Performs an immediate refresh of the managed DNS entries.\n"
344 " Optional regular expression pattern is used to filter the entries to refresh.\n";
345 return NULL;
346 case CLI_GENERATE:
347 return NULL;
348 }
349
350 if (!enabled) {
351 ast_cli(a->fd, "DNS Manager is disabled.\n");
352 return 0;
353 }
354
355 if (a->argc > 3) {
356 return CLI_SHOWUSAGE;
357 }
358
359 if (a->argc == 3) {
360 if (regcomp(&info.filter, a->argv[2], REG_EXTENDED | REG_NOSUB)) {
361 return CLI_SHOWUSAGE;
362 } else {
363 info.regex_present = 1;
364 }
365 }
366
367 refresh_list(&info);
368
369 if (info.regex_present) {
370 regfree(&info.filter);
371 }
372
373 return CLI_SUCCESS;
374 }
375
376 static char *handle_cli_status(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
377 {
378 int count = 0;
379 struct ast_dnsmgr_entry *entry;
380 switch (cmd) {
381 case CLI_INIT:
382 e->command = "dnsmgr status";
383 e->usage =
384 "Usage: dnsmgr status\n"
385 " Displays the DNS manager status.\n";
386 return NULL;
387 case CLI_GENERATE:
388 return NULL;
389 }
390
391 if (a->argc > 2) {
392 return CLI_SHOWUSAGE;
393 }
394
395 ast_cli(a->fd, "DNS Manager: %s\n", enabled ? "enabled" : "disabled");
396 ast_cli(a->fd, "Refresh Interval: %d seconds\n", refresh_interval);
397 AST_RWLIST_RDLOCK(&entry_list);
398 AST_RWLIST_TRAVERSE(&entry_list, entry, list)
399 count++;
400 AST_RWLIST_UNLOCK(&entry_list);
401 ast_cli(a->fd, "Number of entries: %d\n", count);
402
403 return CLI_SUCCESS;
404 }
405
406 static struct ast_cli_entry cli_reload = AST_CLI_DEFINE(handle_cli_reload, "Reloads the DNS manager configuration");
407 static struct ast_cli_entry cli_refresh = AST_CLI_DEFINE(handle_cli_refresh, "Performs an immediate refresh");
408 static struct ast_cli_entry cli_status = AST_CLI_DEFINE(handle_cli_status, "Display the DNS manager status");
409
410 static int unload_module(void)
411 {
412 ast_cli_unregister(&cli_reload);
413 ast_cli_unregister(&cli_status);
414 ast_cli_unregister(&cli_refresh);
415
416 /* Destroy refresh thread. */
417 ast_mutex_lock(&refresh_lock);
418 if (refresh_thread != AST_PTHREADT_NULL) {
419 /* wake up the thread so it will exit */
420 pthread_cancel(refresh_thread);
421 pthread_kill(refresh_thread, SIGURG);
422 pthread_join(refresh_thread, NULL);
423 refresh_thread = AST_PTHREADT_NULL;
424 }
425 ast_mutex_unlock(&refresh_lock);
426
427 ast_sched_context_destroy(sched);
428
429 return 0;
430 }
431
432 static int load_module(void)
433 {
434 if (!(sched = ast_sched_context_create())) {
435 ast_log(LOG_ERROR, "Unable to create schedule context.\n");
436 return AST_MODULE_LOAD_FAILURE;
437 }
438 ast_cli_register(&cli_reload);
439 ast_cli_register(&cli_status);
440 ast_cli_register(&cli_refresh);
441
442 return do_reload(1) ? AST_MODULE_LOAD_FAILURE : AST_MODULE_LOAD_SUCCESS;
443 }
444
445 static int reload_module(void)
446 {
447 return do_reload(0);
448 }
449
450 static int do_reload(int loading)
451 {
452 struct ast_config *config;
453 struct ast_variable *v;
454 struct ast_flags config_flags = { loading ? 0 : CONFIG_FLAG_FILEUNCHANGED };
455 int interval;
456 int was_enabled;
457
458 if ((config = ast_config_load2("dnsmgr.conf", "dnsmgr", config_flags)) == CONFIG_STATUS_FILEUNCHANGED) {
459 return 0;
460 }
461
462 /* ensure that no refresh cycles run while the reload is in progress */
463 ast_mutex_lock(&refresh_lock);
464
465 /* reset defaults in preparation for reading config file */
466 refresh_interval = REFRESH_DEFAULT;
467 was_enabled = enabled;
468 enabled = 0;
469
470 if (config == CONFIG_STATUS_FILEMISSING || config == CONFIG_STATUS_FILEINVALID) {
471 ast_mutex_unlock(&refresh_lock);
472 return 0;
473 }
474
475 AST_SCHED_DEL(sched, refresh_sched);
476
477 for (v = ast_variable_browse(config, "general"); v; v = v->next) {
478 if (!strcasecmp(v->name, "enable")) {
479 enabled = ast_true(v->value);
480 } else if (!strcasecmp(v->name, "refreshinterval")) {
481 if (sscanf(v->value, "%30d", &interval) < 1) {
482 ast_log(LOG_WARNING, "Unable to convert '%s' to a numeric value.\n", v->value);
483 } else if (interval < 0) {
484 ast_log(LOG_WARNING, "Invalid refresh interval '%d' specified, using default\n", interval);
485 } else {
486 refresh_interval = interval;
487 }
488 }
489 }
490 ast_config_destroy(config);
491
492 if (enabled && refresh_interval) {
493 ast_log(LOG_NOTICE, "Managed DNS entries will be refreshed every %d seconds.\n", refresh_interval);
494 }
495
496 /* if this reload enabled the manager, create the background thread
497 if it does not exist */
498 if (enabled) {
499 if (!was_enabled && (refresh_thread == AST_PTHREADT_NULL)) {
500 if (ast_pthread_create_background(&refresh_thread, NULL, do_refresh, NULL) < 0) {
501 ast_log(LOG_ERROR, "Unable to start refresh thread.\n");
502 }
503 }
504 /* make a background refresh happen right away */
505 refresh_sched = ast_sched_add_variable(sched, 100, refresh_list, &master_refresh_info, 1);
506 /* if this reload disabled the manager and there is a background thread, kill it */
507 } else if (!enabled && was_enabled && (refresh_thread != AST_PTHREADT_NULL)) {
508 /* wake up the thread so it will exit */
509 pthread_cancel(refresh_thread);
510 pthread_kill(refresh_thread, SIGURG);
511 pthread_join(refresh_thread, NULL);
512 refresh_thread = AST_PTHREADT_NULL;
513 }
514
515 ast_mutex_unlock(&refresh_lock);
516
517 return 0;
518 }
519
520 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_GLOBAL_SYMBOLS | AST_MODFLAG_LOAD_ORDER, "DNS Manager",
521 .support_level = AST_MODULE_SUPPORT_CORE,
522 .load = load_module,
523 .unload = unload_module,
524 .reload = reload_module,
525 .load_pri = AST_MODPRI_CORE,
526 .requires = "extconfig",
527 );