[asterisk-commits] pabelanger: trunk r335555 - in /trunk: UPGRADE.txt main/dnsmgr.c

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Tue Sep 13 09:23:03 CDT 2011


Author: pabelanger
Date: Tue Sep 13 09:22:58 2011
New Revision: 335555

URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=335555
Log:
Clean up dnsmgr.conf parsing

Review: https://reviewboard.asterisk.org/r/1432/

Modified:
    trunk/UPGRADE.txt
    trunk/main/dnsmgr.c

Modified: trunk/UPGRADE.txt
URL: http://svnview.digium.com/svn/asterisk/trunk/UPGRADE.txt?view=diff&rev=335555&r1=335554&r2=335555
==============================================================================
--- trunk/UPGRADE.txt (original)
+++ trunk/UPGRADE.txt Tue Sep 13 09:22:58 2011
@@ -33,6 +33,7 @@
    with what is expected across modules.
 
    - cdr.conf: [general] section
+   - dnsmgr.conf
 
 SIP
 ===

Modified: trunk/main/dnsmgr.c
URL: http://svnview.digium.com/svn/asterisk/trunk/main/dnsmgr.c?view=diff&rev=335555&r1=335554&r2=335555
==============================================================================
--- trunk/main/dnsmgr.c (original)
+++ trunk/main/dnsmgr.c Tue Sep 13 09:22:58 2011
@@ -109,8 +109,9 @@
 
 void ast_dnsmgr_release(struct ast_dnsmgr_entry *entry)
 {
-	if (!entry)
+	if (!entry) {
 		return;
+	}
 
 	AST_RWLIST_WRLOCK(&entry_list);
 	AST_RWLIST_REMOVE(&entry_list, entry, list);
@@ -143,12 +144,12 @@
 
 	/* 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);
-	
+
 	/* if dnsmgr is not enable don't bother adding an entry */
 	if (!enabled) {
 		return 0;
 	}
-	
+
 	ast_verb(3, "adding dns manager for '%s'\n", name);
 	*dnsmgr = ast_dnsmgr_get(name, result, service);
 	return !*dnsmgr;
@@ -198,7 +199,7 @@
 /*
  * 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;
 
@@ -208,7 +209,7 @@
 	entry->changed = 0;
 
 	ast_mutex_unlock(&entry->lock);
-	
+
 	return changed;
 }
 
@@ -230,16 +231,18 @@
 
 	/* if a refresh or reload is already in progress, exit now */
 	if (ast_mutex_trylock(&refresh_lock)) {
-		if (info->verbose)
+		if (info->verbose) {
 			ast_log(LOG_WARNING, "DNS Manager refresh already in progress.\n");
+		}
 		return -1;
 	}
 
 	ast_verb(3, "Refreshing DNS lookups.\n");
 	AST_RWLIST_RDLOCK(info->entries);
 	AST_RWLIST_TRAVERSE(info->entries, entry, list) {
-		if (info->regex_present && regexec(&info->filter, entry->name, 0, NULL, 0))
-		    continue;
+		if (info->regex_present && regexec(&info->filter, entry->name, 0, NULL, 0)) {
+			continue;
+		}
 
 		dnsmgr_refresh(entry, info->verbose);
 	}
@@ -266,15 +269,16 @@
 	switch (cmd) {
 	case CLI_INIT:
 		e->command = "dnsmgr reload";
-		e->usage = 
+		e->usage =
 			"Usage: dnsmgr reload\n"
 			"       Reloads the DNS manager configuration.\n";
 		return NULL;
 	case CLI_GENERATE:
-		return NULL;	
-	}
-	if (a->argc > 2)
+		return NULL;
+	}
+	if (a->argc > 2) {
 		return CLI_SHOWUSAGE;
+	}
 
 	do_reload(0);
 	return CLI_SUCCESS;
@@ -289,13 +293,13 @@
 	switch (cmd) {
 	case CLI_INIT:
 		e->command = "dnsmgr refresh";
-		e->usage = 
+		e->usage =
 			"Usage: dnsmgr refresh [pattern]\n"
 			"       Peforms an immediate refresh of the managed DNS entries.\n"
 			"       Optional regular expression pattern is used to filter the entries to refresh.\n";
 		return NULL;
 	case CLI_GENERATE:
-		return NULL;	
+		return NULL;
 	}
 
 	if (!enabled) {
@@ -331,16 +335,17 @@
 	switch (cmd) {
 	case CLI_INIT:
 		e->command = "dnsmgr status";
-		e->usage = 
+		e->usage =
 			"Usage: dnsmgr status\n"
 			"       Displays the DNS manager status.\n";
 		return NULL;
 	case CLI_GENERATE:
-		return NULL;	
-	}
-
-	if (a->argc > 2)
+		return NULL;
+	}
+
+	if (a->argc > 2) {
 		return CLI_SHOWUSAGE;
+	}
 
 	ast_cli(a->fd, "DNS Manager: %s\n", enabled ? "enabled" : "disabled");
 	ast_cli(a->fd, "Refresh Interval: %d seconds\n", refresh_interval);
@@ -377,16 +382,15 @@
 static int do_reload(int loading)
 {
 	struct ast_config *config;
+	struct ast_variable *v;
 	struct ast_flags config_flags = { loading ? 0 : CONFIG_FLAG_FILEUNCHANGED };
-	const char *interval_value;
-	const char *enabled_value;
 	int interval;
 	int was_enabled;
-	int res = -1;
+	int res = 0;
 
 	config = ast_config_load2("dnsmgr.conf", "dnsmgr", config_flags);
 	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 */
@@ -399,23 +403,24 @@
 
 	AST_SCHED_DEL(sched, refresh_sched);
 
-	if (config) {
-		if ((enabled_value = ast_variable_retrieve(config, "general", "enable"))) {
-			enabled = ast_true(enabled_value);
-		}
-		if ((interval_value = ast_variable_retrieve(config, "general", "refreshinterval"))) {
-			if (sscanf(interval_value, "%30d", &interval) < 1)
-				ast_log(LOG_WARNING, "Unable to convert '%s' to a numeric value.\n", interval_value);
-			else if (interval < 0)
+	for (v = ast_variable_browse(config, "general"); v; v = v->next) {
+		if (!strcasecmp(v->name, "enable")) {
+			enabled = ast_true(v->value);
+		} else if (!strcasecmp(v->name, "refreshinterval")) {
+			if (sscanf(v->value, "%30d", &interval) < 1) {
+				ast_log(LOG_WARNING, "Unable to convert '%s' to a numeric value.\n", v->value);
+			} else if (interval < 0) {
 				ast_log(LOG_WARNING, "Invalid refresh interval '%d' specified, using default\n", interval);
-			else
+			} else {
 				refresh_interval = interval;
-		}
-		ast_config_destroy(config);
-	}
-
-	if (enabled && refresh_interval)
+			}
+		}
+	}
+	ast_config_destroy(config);
+
+	if (enabled && 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 it does not exist */
@@ -427,20 +432,14 @@
 		}
 		/* make a background refresh happen right away */
 		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 */
 		pthread_cancel(refresh_thread);
 		pthread_kill(refresh_thread, SIGURG);
 		pthread_join(refresh_thread, NULL);
 		refresh_thread = AST_PTHREADT_NULL;
-		res = 0;
-	}
-	else
-		res = 0;
+	}
 
 	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");




More information about the asterisk-commits mailing list