[Asterisk-code-review] res pjsip config wizard/config: Fix template processing (asterisk[13])

George Joseph asteriskteam at digium.com
Thu May 14 18:20:11 CDT 2015


George Joseph has uploaded a new change for review.

  https://gerrit.asterisk.org/473

Change subject: res_pjsip_config_wizard/config: Fix template processing
......................................................................

res_pjsip_config_wizard/config: Fix template processing

The config wizard was always pulling the first occurrence of
a variable from an ast_variable list but this gets the template
value from the list instead of any overridden value.  This patch
creates ast_variable_find_last_in_list() in config.c and updates
res_pjsip_config_wizard to use it instead of
ast_variable_find_in_list.  Now the overridden values, where they
exist, are used instead of template variables.

Updated test_config to test the new API.

ASTERISK-25089 #close

Change-Id: Ifa7ddefc956a463923ee6839dd1ebe021c299de4
---
M include/asterisk/config.h
M main/config.c
M res/res_pjsip_config_wizard.c
M tests/test_config.c
4 files changed, 63 insertions(+), 17 deletions(-)


  git pull ssh://gerrit.asterisk.org:29418/asterisk refs/changes/73/473/1

diff --git a/include/asterisk/config.h b/include/asterisk/config.h
index 5552695..c4d6727 100644
--- a/include/asterisk/config.h
+++ b/include/asterisk/config.h
@@ -335,6 +335,22 @@
 const char *ast_variable_find_in_list(const struct ast_variable *list, const char *variable);
 
 /*!
+ * \brief Gets the LAST occurrence of a variable from a variable list
+ *
+ * \param list variable list to search
+ * \param variable which variable you wish to get the data for
+ *
+ * \details
+ * Goes through a given variable list and searches for the given variable.
+ * It returns the last one found.  This is useful if the list has duplicates
+ * that may have been created by a template.
+ *
+ * \retval The variable value on success
+ * \retval NULL if unable to find it.
+ */
+const char *ast_variable_find_last_in_list(const struct ast_variable *list, const char *variable);
+
+/*!
  * \brief Retrieve a category if it exists
  *
  * \param config which config to use
diff --git a/main/config.c b/main/config.c
index daccae5..f59e121 100644
--- a/main/config.c
+++ b/main/config.c
@@ -735,6 +735,19 @@
 	return NULL;
 }
 
+const char *ast_variable_find_last_in_list(const struct ast_variable *list, const char *variable)
+{
+	const struct ast_variable *v;
+	const char *found = NULL;
+
+	for (v = list; v; v = v->next) {
+		if (!strcasecmp(variable, v->name)) {
+			found = v->value;
+		}
+	}
+	return found;
+}
+
 static struct ast_variable *variable_clone(const struct ast_variable *old)
 {
 	struct ast_variable *new = ast_variable_new(old->name, old->value, old->file);
diff --git a/res/res_pjsip_config_wizard.c b/res/res_pjsip_config_wizard.c
index d6d5d5b..0c638cb 100644
--- a/res/res_pjsip_config_wizard.c
+++ b/res/res_pjsip_config_wizard.c
@@ -337,7 +337,7 @@
 /*! \brief Finds a variable in a list and tests it */
 static int is_variable_true(struct ast_variable *vars, const char *name)
 {
-	return ast_true(ast_variable_find_in_list(vars, name));
+	return ast_true(ast_variable_find_last_in_list(vars, name));
 }
 
 /*! \brief Appends a variable to the end of an existing list */
@@ -539,7 +539,7 @@
 	}
 
 	if (is_variable_true(wizvars, test_variable)) {
-		if (!ast_variable_find_in_list(vars, "username")) {
+		if (!ast_variable_find_last_in_list(vars, "username")) {
 			ast_log(LOG_ERROR,
 				"Wizard '%s' must have '%s_auth/username' if it %s.\n", id, direction, test_variable);
 			return -1;
@@ -557,7 +557,7 @@
 	variable_list_append_return(&vars, "@pjsip_wizard", id);
 
 	/* If the user set auth_type, don't override it. */
-	if (!ast_variable_find_in_list(vars, "auth_type")) {
+	if (!ast_variable_find_last_in_list(vars, "auth_type")) {
 		variable_list_append_return(&vars, "auth_type", "userpass");
 	}
 
@@ -599,8 +599,8 @@
 	variable_list_append(&vars, "@pjsip_wizard", id);
 
 	/* If the user explicitly specified an aor/contact, don't use remote hosts. */
-	if (!ast_variable_find_in_list(vars, "contact")) {
-		if (!(contact_pattern = ast_variable_find_in_list(wizvars, "contact_pattern"))) {
+	if (!ast_variable_find_last_in_list(vars, "contact")) {
+		if (!(contact_pattern = ast_variable_find_last_in_list(wizvars, "contact_pattern"))) {
 			contact_pattern = "sip:${REMOTE_HOST}";
 		}
 
@@ -645,10 +645,10 @@
 	struct ast_variable *wizvars = ast_category_first(wiz);
 	struct ast_sorcery_object *obj = NULL;
 	const char *id = ast_category_get_name(wiz);
-	const char *transport = ast_variable_find_in_list(wizvars, "transport");
-	const char *hint_context = hint_context = ast_variable_find_in_list(wizvars, "hint_context");
-	const char *hint_exten = ast_variable_find_in_list(wizvars, "hint_exten");
-	const char *hint_application= ast_variable_find_in_list(wizvars, "hint_application");
+	const char *transport = ast_variable_find_last_in_list(wizvars, "transport");
+	const char *hint_context = hint_context = ast_variable_find_last_in_list(wizvars, "hint_context");
+	const char *hint_exten = ast_variable_find_last_in_list(wizvars, "hint_exten");
+	const char *hint_application= ast_variable_find_last_in_list(wizvars, "hint_application");
 	char new_id[strlen(id) + MAX_ID_SUFFIX];
 	RAII_VAR(struct ast_variable *, vars, get_object_variables(wizvars, "endpoint/"), ast_variables_destroy);
 
@@ -656,7 +656,7 @@
 	variable_list_append_return(&vars, "aors", id);
 
 	if (ast_strlen_zero(hint_context)) {
-		hint_context = ast_variable_find_in_list(vars, "context");
+		hint_context = ast_variable_find_last_in_list(vars, "context");
 	}
 
 	if (ast_strlen_zero(hint_context)) {
@@ -737,7 +737,7 @@
 	variable_list_append_return(&vars, "endpoint", id);
 	variable_list_append_return(&vars, "@pjsip_wizard", id);
 
-	if (!ast_variable_find_in_list(vars, "match")) {
+	if (!ast_variable_find_last_in_list(vars, "match")) {
 		for (host_counter = 0; host_counter < host_count; host_counter++) {
 			char *rhost = AST_VECTOR_GET(remote_hosts_vector, host_counter);
 			char host[strlen(rhost) + 1];
@@ -787,7 +787,7 @@
 		return 0;
 	}
 
-	if (!ast_variable_find_in_list(wizvars, "phoneprov/MAC")) {
+	if (!ast_variable_find_last_in_list(wizvars, "phoneprov/MAC")) {
 		ast_log(LOG_ERROR,
 			"Wizard '%s' must have 'phoneprov/MAC' if it has_phoneprov.\n", id);
 		return -1;
@@ -834,7 +834,7 @@
 	const char *id = ast_category_get_name(wiz);
 	const char *server_uri_pattern;
 	const char *client_uri_pattern;
-	const char *transport = ast_variable_find_in_list(wizvars, "transport");
+	const char *transport = ast_variable_find_last_in_list(wizvars, "transport");
 	const char *username;
 	char new_id[strlen(id) + MAX_ID_SUFFIX];
 	int host_count = AST_VECTOR_SIZE(remote_hosts_vector);
@@ -871,16 +871,16 @@
 
 	variable_list_append_return(&vars, "@pjsip_wizard", id);
 
-	if (!(server_uri_pattern = ast_variable_find_in_list(wizvars, "server_uri_pattern"))) {
+	if (!(server_uri_pattern = ast_variable_find_last_in_list(wizvars, "server_uri_pattern"))) {
 		server_uri_pattern = "sip:${REMOTE_HOST}";
 	}
 
-	if (!(client_uri_pattern = ast_variable_find_in_list(wizvars, "client_uri_pattern"))) {
+	if (!(client_uri_pattern = ast_variable_find_last_in_list(wizvars, "client_uri_pattern"))) {
 		client_uri_pattern = "sip:${USERNAME}@${REMOTE_HOST}";
 	}
 
 	if(is_variable_true(wizvars, "sends_auth")) {
-		username = ast_variable_find_in_list(wizvars, "outbound_auth/username");
+		username = ast_variable_find_last_in_list(wizvars, "outbound_auth/username");
 	} else {
 		username = id;
 	}
@@ -958,7 +958,7 @@
 	int rc = -1;
 
 	AST_VECTOR_INIT(&remote_hosts_vector, 16);
-	remote_hosts = ast_variable_find_in_list(wizvars, "remote_hosts");
+	remote_hosts = ast_variable_find_last_in_list(wizvars, "remote_hosts");
 
 	if (!ast_strlen_zero(remote_hosts)) {
 		char *host;
diff --git a/tests/test_config.c b/tests/test_config.c
index df618f9..b9935dc 100644
--- a/tests/test_config.c
+++ b/tests/test_config.c
@@ -234,6 +234,7 @@
 	struct ast_config *cfg = NULL;
 	struct ast_category *cat = NULL;
 	struct ast_variable *var;
+	struct ast_variable *varlist;
 	char temp[32];
 	const char *cat_name;
 	const char *var_value;
@@ -537,6 +538,22 @@
 		goto out;
 	}
 
+	varlist = ast_variable_new("name1", "value1", "");
+	ast_variable_list_append_hint(&varlist, NULL, ast_variable_new("name1", "value2", ""));
+	ast_variable_list_append_hint(&varlist, NULL, ast_variable_new("name1", "value3", ""));
+
+	var_value = ast_variable_find_in_list(varlist, "name1");
+	if (strcmp(var_value, "value1") != 0) {
+		ast_test_status_update(test, "Wrong variable retrieved %s.\n", var_value);
+		goto out;
+	}
+
+	var_value = ast_variable_find_last_in_list(varlist, "name1");
+	if (strcmp(var_value, "value3") != 0) {
+		ast_test_status_update(test, "Wrong variable retrieved %s.\n", var_value);
+		goto out;
+	}
+
 	res = AST_TEST_PASS;
 
 out:

-- 
To view, visit https://gerrit.asterisk.org/473
To unsubscribe, visit https://gerrit.asterisk.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ifa7ddefc956a463923ee6839dd1ebe021c299de4
Gerrit-PatchSet: 1
Gerrit-Project: asterisk
Gerrit-Branch: 13
Gerrit-Owner: George Joseph <george.joseph at fairview5.com>



More information about the asterisk-code-review mailing list