mirror of
https://github.com/asterisk/asterisk.git
synced 2025-10-03 19:16:46 +00:00
Clean up dnsmgr.conf parsing
Review: https://reviewboard.asterisk.org/r/1432/ git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@335555 65c4cc65-6c06-0410-ace0-fbb531ad65f3
This commit is contained in:
@@ -33,6 +33,7 @@ Configuration Files:
|
|||||||
with what is expected across modules.
|
with what is expected across modules.
|
||||||
|
|
||||||
- cdr.conf: [general] section
|
- cdr.conf: [general] section
|
||||||
|
- dnsmgr.conf
|
||||||
|
|
||||||
SIP
|
SIP
|
||||||
===
|
===
|
||||||
|
@@ -109,8 +109,9 @@ struct ast_dnsmgr_entry *ast_dnsmgr_get(const char *name, struct ast_sockaddr *r
|
|||||||
|
|
||||||
void ast_dnsmgr_release(struct ast_dnsmgr_entry *entry)
|
void ast_dnsmgr_release(struct ast_dnsmgr_entry *entry)
|
||||||
{
|
{
|
||||||
if (!entry)
|
if (!entry) {
|
||||||
return;
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
AST_RWLIST_WRLOCK(&entry_list);
|
AST_RWLIST_WRLOCK(&entry_list);
|
||||||
AST_RWLIST_REMOVE(&entry_list, entry, list);
|
AST_RWLIST_REMOVE(&entry_list, entry, list);
|
||||||
@@ -143,12 +144,12 @@ int ast_dnsmgr_lookup(const char *name, struct ast_sockaddr *result, struct ast_
|
|||||||
|
|
||||||
/* do a lookup now but add a manager so it will automagically get updated in the background */
|
/* do a lookup now but add a manager so it will automagically get updated in the background */
|
||||||
ast_get_ip_or_srv(result, name, service);
|
ast_get_ip_or_srv(result, name, service);
|
||||||
|
|
||||||
/* if dnsmgr is not enable don't bother adding an entry */
|
/* if dnsmgr is not enable don't bother adding an entry */
|
||||||
if (!enabled) {
|
if (!enabled) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
ast_verb(3, "adding dns manager for '%s'\n", name);
|
ast_verb(3, "adding dns manager for '%s'\n", name);
|
||||||
*dnsmgr = ast_dnsmgr_get(name, result, service);
|
*dnsmgr = ast_dnsmgr_get(name, result, service);
|
||||||
return !*dnsmgr;
|
return !*dnsmgr;
|
||||||
@@ -198,7 +199,7 @@ int ast_dnsmgr_refresh(struct ast_dnsmgr_entry *entry)
|
|||||||
/*
|
/*
|
||||||
* Check if dnsmgr entry has changed from since last call to this function
|
* Check if dnsmgr entry has changed from since last call to this function
|
||||||
*/
|
*/
|
||||||
int ast_dnsmgr_changed(struct ast_dnsmgr_entry *entry)
|
int ast_dnsmgr_changed(struct ast_dnsmgr_entry *entry)
|
||||||
{
|
{
|
||||||
int changed;
|
int changed;
|
||||||
|
|
||||||
@@ -208,7 +209,7 @@ int ast_dnsmgr_changed(struct ast_dnsmgr_entry *entry)
|
|||||||
entry->changed = 0;
|
entry->changed = 0;
|
||||||
|
|
||||||
ast_mutex_unlock(&entry->lock);
|
ast_mutex_unlock(&entry->lock);
|
||||||
|
|
||||||
return changed;
|
return changed;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -230,16 +231,18 @@ static int refresh_list(const void *data)
|
|||||||
|
|
||||||
/* if a refresh or reload is already in progress, exit now */
|
/* if a refresh or reload is already in progress, exit now */
|
||||||
if (ast_mutex_trylock(&refresh_lock)) {
|
if (ast_mutex_trylock(&refresh_lock)) {
|
||||||
if (info->verbose)
|
if (info->verbose) {
|
||||||
ast_log(LOG_WARNING, "DNS Manager refresh already in progress.\n");
|
ast_log(LOG_WARNING, "DNS Manager refresh already in progress.\n");
|
||||||
|
}
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
ast_verb(3, "Refreshing DNS lookups.\n");
|
ast_verb(3, "Refreshing DNS lookups.\n");
|
||||||
AST_RWLIST_RDLOCK(info->entries);
|
AST_RWLIST_RDLOCK(info->entries);
|
||||||
AST_RWLIST_TRAVERSE(info->entries, entry, list) {
|
AST_RWLIST_TRAVERSE(info->entries, entry, list) {
|
||||||
if (info->regex_present && regexec(&info->filter, entry->name, 0, NULL, 0))
|
if (info->regex_present && regexec(&info->filter, entry->name, 0, NULL, 0)) {
|
||||||
continue;
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
dnsmgr_refresh(entry, info->verbose);
|
dnsmgr_refresh(entry, info->verbose);
|
||||||
}
|
}
|
||||||
@@ -266,15 +269,16 @@ static char *handle_cli_reload(struct ast_cli_entry *e, int cmd, struct ast_cli_
|
|||||||
switch (cmd) {
|
switch (cmd) {
|
||||||
case CLI_INIT:
|
case CLI_INIT:
|
||||||
e->command = "dnsmgr reload";
|
e->command = "dnsmgr reload";
|
||||||
e->usage =
|
e->usage =
|
||||||
"Usage: dnsmgr reload\n"
|
"Usage: dnsmgr reload\n"
|
||||||
" Reloads the DNS manager configuration.\n";
|
" Reloads the DNS manager configuration.\n";
|
||||||
return NULL;
|
return NULL;
|
||||||
case CLI_GENERATE:
|
case CLI_GENERATE:
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
if (a->argc > 2)
|
if (a->argc > 2) {
|
||||||
return CLI_SHOWUSAGE;
|
return CLI_SHOWUSAGE;
|
||||||
|
}
|
||||||
|
|
||||||
do_reload(0);
|
do_reload(0);
|
||||||
return CLI_SUCCESS;
|
return CLI_SUCCESS;
|
||||||
@@ -289,13 +293,13 @@ static char *handle_cli_refresh(struct ast_cli_entry *e, int cmd, struct ast_cli
|
|||||||
switch (cmd) {
|
switch (cmd) {
|
||||||
case CLI_INIT:
|
case CLI_INIT:
|
||||||
e->command = "dnsmgr refresh";
|
e->command = "dnsmgr refresh";
|
||||||
e->usage =
|
e->usage =
|
||||||
"Usage: dnsmgr refresh [pattern]\n"
|
"Usage: dnsmgr refresh [pattern]\n"
|
||||||
" Peforms an immediate refresh of the managed DNS entries.\n"
|
" Peforms an immediate refresh of the managed DNS entries.\n"
|
||||||
" Optional regular expression pattern is used to filter the entries to refresh.\n";
|
" Optional regular expression pattern is used to filter the entries to refresh.\n";
|
||||||
return NULL;
|
return NULL;
|
||||||
case CLI_GENERATE:
|
case CLI_GENERATE:
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!enabled) {
|
if (!enabled) {
|
||||||
@@ -331,16 +335,17 @@ static char *handle_cli_status(struct ast_cli_entry *e, int cmd, struct ast_cli_
|
|||||||
switch (cmd) {
|
switch (cmd) {
|
||||||
case CLI_INIT:
|
case CLI_INIT:
|
||||||
e->command = "dnsmgr status";
|
e->command = "dnsmgr status";
|
||||||
e->usage =
|
e->usage =
|
||||||
"Usage: dnsmgr status\n"
|
"Usage: dnsmgr status\n"
|
||||||
" Displays the DNS manager status.\n";
|
" Displays the DNS manager status.\n";
|
||||||
return NULL;
|
return NULL;
|
||||||
case CLI_GENERATE:
|
case CLI_GENERATE:
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (a->argc > 2)
|
if (a->argc > 2) {
|
||||||
return CLI_SHOWUSAGE;
|
return CLI_SHOWUSAGE;
|
||||||
|
}
|
||||||
|
|
||||||
ast_cli(a->fd, "DNS Manager: %s\n", enabled ? "enabled" : "disabled");
|
ast_cli(a->fd, "DNS Manager: %s\n", enabled ? "enabled" : "disabled");
|
||||||
ast_cli(a->fd, "Refresh Interval: %d seconds\n", refresh_interval);
|
ast_cli(a->fd, "Refresh Interval: %d seconds\n", refresh_interval);
|
||||||
@@ -377,16 +382,15 @@ int dnsmgr_reload(void)
|
|||||||
static int do_reload(int loading)
|
static int do_reload(int loading)
|
||||||
{
|
{
|
||||||
struct ast_config *config;
|
struct ast_config *config;
|
||||||
|
struct ast_variable *v;
|
||||||
struct ast_flags config_flags = { loading ? 0 : CONFIG_FLAG_FILEUNCHANGED };
|
struct ast_flags config_flags = { loading ? 0 : CONFIG_FLAG_FILEUNCHANGED };
|
||||||
const char *interval_value;
|
|
||||||
const char *enabled_value;
|
|
||||||
int interval;
|
int interval;
|
||||||
int was_enabled;
|
int was_enabled;
|
||||||
int res = -1;
|
int res = 0;
|
||||||
|
|
||||||
config = ast_config_load2("dnsmgr.conf", "dnsmgr", config_flags);
|
config = ast_config_load2("dnsmgr.conf", "dnsmgr", config_flags);
|
||||||
if (config == CONFIG_STATUS_FILEMISSING || config == CONFIG_STATUS_FILEUNCHANGED || config == CONFIG_STATUS_FILEINVALID) {
|
if (config == CONFIG_STATUS_FILEMISSING || config == CONFIG_STATUS_FILEUNCHANGED || config == CONFIG_STATUS_FILEINVALID) {
|
||||||
return 0;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* ensure that no refresh cycles run while the reload is in progress */
|
/* ensure that no refresh cycles run while the reload is in progress */
|
||||||
@@ -399,23 +403,24 @@ static int do_reload(int loading)
|
|||||||
|
|
||||||
AST_SCHED_DEL(sched, refresh_sched);
|
AST_SCHED_DEL(sched, refresh_sched);
|
||||||
|
|
||||||
if (config) {
|
for (v = ast_variable_browse(config, "general"); v; v = v->next) {
|
||||||
if ((enabled_value = ast_variable_retrieve(config, "general", "enable"))) {
|
if (!strcasecmp(v->name, "enable")) {
|
||||||
enabled = ast_true(enabled_value);
|
enabled = ast_true(v->value);
|
||||||
}
|
} else if (!strcasecmp(v->name, "refreshinterval")) {
|
||||||
if ((interval_value = ast_variable_retrieve(config, "general", "refreshinterval"))) {
|
if (sscanf(v->value, "%30d", &interval) < 1) {
|
||||||
if (sscanf(interval_value, "%30d", &interval) < 1)
|
ast_log(LOG_WARNING, "Unable to convert '%s' to a numeric value.\n", v->value);
|
||||||
ast_log(LOG_WARNING, "Unable to convert '%s' to a numeric value.\n", interval_value);
|
} else if (interval < 0) {
|
||||||
else if (interval < 0)
|
|
||||||
ast_log(LOG_WARNING, "Invalid refresh interval '%d' specified, using default\n", interval);
|
ast_log(LOG_WARNING, "Invalid refresh interval '%d' specified, using default\n", interval);
|
||||||
else
|
} else {
|
||||||
refresh_interval = interval;
|
refresh_interval = interval;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
ast_config_destroy(config);
|
|
||||||
}
|
}
|
||||||
|
ast_config_destroy(config);
|
||||||
|
|
||||||
if (enabled && refresh_interval)
|
if (enabled && refresh_interval) {
|
||||||
ast_log(LOG_NOTICE, "Managed DNS entries will be refreshed every %d seconds.\n", refresh_interval);
|
ast_log(LOG_NOTICE, "Managed DNS entries will be refreshed every %d seconds.\n", refresh_interval);
|
||||||
|
}
|
||||||
|
|
||||||
/* if this reload enabled the manager, create the background thread
|
/* if this reload enabled the manager, create the background thread
|
||||||
if it does not exist */
|
if it does not exist */
|
||||||
@@ -427,20 +432,14 @@ static int do_reload(int loading)
|
|||||||
}
|
}
|
||||||
/* make a background refresh happen right away */
|
/* make a background refresh happen right away */
|
||||||
refresh_sched = ast_sched_add_variable(sched, 100, refresh_list, &master_refresh_info, 1);
|
refresh_sched = ast_sched_add_variable(sched, 100, refresh_list, &master_refresh_info, 1);
|
||||||
res = 0;
|
/* if this reload disabled the manager and there is a background thread, kill it */
|
||||||
}
|
} else if (!enabled && was_enabled && (refresh_thread != AST_PTHREADT_NULL)) {
|
||||||
/* if this reload disabled the manager and there is a background thread,
|
|
||||||
kill it */
|
|
||||||
else if (!enabled && was_enabled && (refresh_thread != AST_PTHREADT_NULL)) {
|
|
||||||
/* wake up the thread so it will exit */
|
/* wake up the thread so it will exit */
|
||||||
pthread_cancel(refresh_thread);
|
pthread_cancel(refresh_thread);
|
||||||
pthread_kill(refresh_thread, SIGURG);
|
pthread_kill(refresh_thread, SIGURG);
|
||||||
pthread_join(refresh_thread, NULL);
|
pthread_join(refresh_thread, NULL);
|
||||||
refresh_thread = AST_PTHREADT_NULL;
|
refresh_thread = AST_PTHREADT_NULL;
|
||||||
res = 0;
|
|
||||||
}
|
}
|
||||||
else
|
|
||||||
res = 0;
|
|
||||||
|
|
||||||
ast_mutex_unlock(&refresh_lock);
|
ast_mutex_unlock(&refresh_lock);
|
||||||
manager_event(EVENT_FLAG_SYSTEM, "Reload", "Module: DNSmgr\r\nStatus: %s\r/nMessage: DNSmgr reload Requested\r\n", enabled ? "Enabled" : "Disabled");
|
manager_event(EVENT_FLAG_SYSTEM, "Reload", "Module: DNSmgr\r\nStatus: %s\r/nMessage: DNSmgr reload Requested\r\n", enabled ? "Enabled" : "Disabled");
|
||||||
|
Reference in New Issue
Block a user