[OpenSIPS-Users] routing function variable scope

Kingsley Tart kingsley at dns99.co.uk
Fri May 7 17:07:27 EST 2021


On Wed, 2021-04-28 at 15:27 +0300, Liviu Chircu wrote:
> On 28.04.2021 15:19, Kingsley Tart wrote:
> > Is there a better way, apart from trying to make sure that
> > variables in
> > each function don't share names with variables in other functions?
> 
> No, you have to manage those iterator names and make sure the logic 
> still holds during nested calls.

Hi,

I have now ran into the opposite problem! Given that values seem to be
globally shared, I am now trying to use a sub-function to set the value
of a $var() variable from a parent function, but the changed value gets
lost when the sub function quits.

Here is the code:

route[bhah] {
	xlog("ALLOW BEFORE=\"$var(N9_hdrval)\"\n");
	route(csv_filter, $var(N9_hdrval), "(?i)^ *(OPTIONS|INVITE|ACK|BYE|CANCEL|UPDATE|PRACK|MESSAGE) *$", "N9_hdrval");
	xlog("ALLOW AFTER=\"$var(N9_hdrval)\"\n");
}


route[csv_filter] {
	$var(N11_srcCSV) = $param(1);
	$var(N11_filter_regex) = $param(2);
	$var(N11_resultvar_name) = $param(3);

	$var(N11_result) = "";
	$var(N11_pos) = $(var(N11_srcCSV){csv.count}) - 1;
	while ($var(N11_pos) >= 0) {
		$var(N11_element) = $(var(N11_srcCSV){csv.value,$var(N11_pos)});
		if (pcre_match($var(N11_element), $var(N11_filter_regex))) {
			$var(N11_result) = $var(N11_element) + "," + $var(N11_result);
		}
		$var(N11_pos) = $var(N11_pos) - 1;
	}
	$var(N11_resultLen) = $(var(N11_result){s.len}) - 1;
	$var($var(N11_resultvar_name)) = $(var(N11_result){s.substr,0,$var(N11_resultLen)});
	xlog("FILTERED var($var(N11_resultvar_name))=\"$var($var(N11_resultvar_name))\"\n");
}


But this is what gets logged:

opensips[21955]: ALLOW BEFORE="OPTIONS, SUBSCRIBE, NOTIFY, PUBLISH, INVITE, ACK, BYE, CANCEL, UPDATE, PRACK, REGISTER, REFER, MESSAGE"
opensips[21955]: FILTERED var(N9_hdrval)="OPTIONS, INVITE, ACK, BYE, CANCEL, UPDATE, PRACK, MESSAGE"
opensips[21955]: ALLOW AFTER="OPTIONS, SUBSCRIBE, NOTIFY, PUBLISH, INVITE, ACK, BYE, CANCEL, UPDATE, PRACK, REGISTER, REFER, MESSAGE"


This is what I was hoping to get:

opensips[21955]: ALLOW BEFORE="OPTIONS, SUBSCRIBE, NOTIFY, PUBLISH, INVITE, ACK, BYE, CANCEL, UPDATE, PRACK, REGISTER, REFER, MESSAGE"
opensips[21955]: FILTERED var(N9_hdrval)="OPTIONS, INVITE, ACK, BYE, CANCEL, UPDATE, PRACK, MESSAGE"
opensips[21955]: ALLOW AFTER="OPTIONS, INVITE, ACK, BYE, CANCEL, UPDATE, PRACK, MESSAGE"


Have I done something silly here?

Cheers,
Kingsley.




More information about the Users mailing list