[Asterisk-code-review] channels: Allow updating variable value (...asterisk[13])

Friendly Automation asteriskteam at digium.com
Fri Sep 13 07:45:42 CDT 2019


Friendly Automation has submitted this change and it was merged. ( https://gerrit.asterisk.org/c/asterisk/+/12744 )

Change subject: channels: Allow updating variable value
......................................................................

channels: Allow updating variable value

When modifying an already defined variable in some channel drivers they
add a new variable with the same name to the list, but that value is
never used, only the first one found.

Introduce ast_variable_list_replace() and use it where appropriate.

ASTERISK-23756 #close
Patches:
  setvar-multiplie.patch submitted by Michael Goryainov

Change-Id: Ie1897a96c82b8945e752733612ee963686f32839
---
M channels/chan_dahdi.c
M channels/chan_iax2.c
M channels/chan_sip.c
M include/asterisk/config.h
M main/config.c
M res/res_pjsip/pjsip_configuration.c
6 files changed, 55 insertions(+), 11 deletions(-)

Approvals:
  Richard Mudgett: Looks good to me, but someone else must approve
  Guido Falsi: Looks good to me, but someone else must approve
  George Joseph: Looks good to me, approved
  Friendly Automation: Approved for Submit



diff --git a/channels/chan_dahdi.c b/channels/chan_dahdi.c
index 1a3b158..5ff12c6 100644
--- a/channels/chan_dahdi.c
+++ b/channels/chan_dahdi.c
@@ -12873,8 +12873,10 @@
 			struct ast_variable *v, *tmpvar;
 			for (v = conf->chan.vars ; v ; v = v->next) {
 				if ((tmpvar = ast_variable_new(v->name, v->value, v->file))) {
-					tmpvar->next = tmp->vars;
-					tmp->vars = tmpvar;
+					if (ast_variable_list_replace(&tmp->vars, tmpvar)) {
+						tmpvar->next = tmp->vars;
+						tmp->vars = tmpvar;
+					}
 				}
 			}
 		}
diff --git a/channels/chan_iax2.c b/channels/chan_iax2.c
index 3bf06d0..81bdf15 100644
--- a/channels/chan_iax2.c
+++ b/channels/chan_iax2.c
@@ -7941,9 +7941,11 @@
 		/* We found our match (use the first) */
 		/* copy vars */
 		for (v = user->vars ; v ; v = v->next) {
-			if((tmpvar = ast_variable_new(v->name, v->value, v->file))) {
-				tmpvar->next = iaxs[callno]->vars;
-				iaxs[callno]->vars = tmpvar;
+			if ((tmpvar = ast_variable_new(v->name, v->value, v->file))) {
+				if (ast_variable_list_replace(&iaxs[callno]->vars, tmpvar)) {
+					tmpvar->next = iaxs[callno]->vars;
+					iaxs[callno]->vars = tmpvar;
+				}
 			}
 		}
 		/* If a max AUTHREQ restriction is in place, activate it */
@@ -13212,9 +13214,11 @@
 				if ((varval = strchr(varname, '='))) {
 					*varval = '\0';
 					varval++;
-					if((tmpvar = ast_variable_new(varname, varval, ""))) {
-						tmpvar->next = user->vars;
-						user->vars = tmpvar;
+					if ((tmpvar = ast_variable_new(varname, varval, ""))) {
+						if (ast_variable_list_replace(&user->vars, tmpvar)) {
+							tmpvar->next = user->vars;
+							user->vars = tmpvar;
+						}
 					}
 				}
 			} else if (!strcasecmp(v->name, "allow")) {
diff --git a/channels/chan_sip.c b/channels/chan_sip.c
index 223ff3c..ea78d23 100644
--- a/channels/chan_sip.c
+++ b/channels/chan_sip.c
@@ -31126,8 +31126,10 @@
 	if ((varval = strchr(varname, '='))) {
 		*varval++ = '\0';
 		if ((tmpvar = ast_variable_new(varname, varval, ""))) {
-			tmpvar->next = list;
-			list = tmpvar;
+			if (ast_variable_list_replace(&list, tmpvar)) {
+				tmpvar->next = list;
+				list = tmpvar;
+			}
 		}
 	}
 	return list;
diff --git a/include/asterisk/config.h b/include/asterisk/config.h
index 5ab1f68..04b858a 100644
--- a/include/asterisk/config.h
+++ b/include/asterisk/config.h
@@ -961,6 +961,24 @@
 #define ast_variable_list_append(head, new_var) ast_variable_list_append_hint(head, NULL, new_var)
 
 /*!
+ * \brief Replace a variable in the given list with a new value
+ * \since 13.30.0
+ *
+ * \param head A pointer to an ast_variable * of the existing variable list head. May NOT be NULL
+ * but the content may be to initialize a new list.  If so, upon return, this parameter will be updated
+ * with a pointer to the new list head.
+ * \param replacement The variable that replaces another variable in the list with the
+ * same name.
+ *
+ * \retval 0 if a variable was replaced in the list
+ * \retval -1 if no replacement occured
+ *
+ * \note The variable name comparison is performed case-sensitively
+ * \note If a variable is replaced, its memory is freed.
+ */
+int ast_variable_list_replace(struct ast_variable **head, struct ast_variable *replacement);
+
+/*!
  * \brief Update variable value within a config
  *
  * \param category Category element within the config
diff --git a/main/config.c b/main/config.c
index 0267979..8731ebb 100644
--- a/main/config.c
+++ b/main/config.c
@@ -669,6 +669,22 @@
 	return curr;
 }
 
+int ast_variable_list_replace(struct ast_variable **head, struct ast_variable *replacement)
+{
+	struct ast_variable *v, **prev = head;
+
+	for (v = *head; v; prev = &v->next, v = v->next) {
+		if (!strcmp(v->name, replacement->name)) {
+			replacement->next = v->next;
+			*prev = replacement;
+			ast_free(v);
+			return 0;
+		}
+	}
+
+	return -1;
+}
+
 const char *ast_config_option(struct ast_config *cfg, const char *cat, const char *var)
 {
 	const char *tmp;
diff --git a/res/res_pjsip/pjsip_configuration.c b/res/res_pjsip/pjsip_configuration.c
index 4f7beb6..b5525be 100644
--- a/res/res_pjsip/pjsip_configuration.c
+++ b/res/res_pjsip/pjsip_configuration.c
@@ -1036,7 +1036,9 @@
 		return -1;
 	}
 
-	ast_variable_list_append(&endpoint->channel_vars, new_var);
+	if (ast_variable_list_replace(&endpoint->channel_vars, new_var)) {
+		ast_variable_list_append(&endpoint->channel_vars, new_var);
+	}
 
 	return 0;
 }

-- 
To view, visit https://gerrit.asterisk.org/c/asterisk/+/12744
To unsubscribe, or for help writing mail filters, visit https://gerrit.asterisk.org/settings

Gerrit-Project: asterisk
Gerrit-Branch: 13
Gerrit-Change-Id: Ie1897a96c82b8945e752733612ee963686f32839
Gerrit-Change-Number: 12744
Gerrit-PatchSet: 8
Gerrit-Owner: Sean Bright <sean.bright at gmail.com>
Gerrit-Reviewer: Friendly Automation
Gerrit-Reviewer: George Joseph <gjoseph at digium.com>
Gerrit-Reviewer: Guido Falsi <madpilot at freebsd.org>
Gerrit-Reviewer: Richard Mudgett <rmudgett at digium.com>
Gerrit-Reviewer: Sean Bright <sean.bright at gmail.com>
Gerrit-CC: Joshua C. Colp <jcolp at digium.com>
Gerrit-MessageType: merged
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.digium.com/pipermail/asterisk-code-review/attachments/20190913/02c47367/attachment.html>


More information about the asterisk-code-review mailing list