[OpenSIPS-Users] routing function variable scope

Ben Newlin Ben.Newlin at genesys.com
Fri May 7 17:32:00 EST 2021


Kingsley,

I think the issue here is the nested variable name references. I think within route csv_filter, you are actually setting a variable called “$var(N11_resultvar_name)”, not a variable with the name of the contents of that variable.

I believe you can easily confirm this with two things:

  *   Remove (or ignore) the parameter providing the variable name and hardcode it to “N9_hdrval” in csv_filter for a test. You should see the variable is now set when printed after.

and/or

  *   Add another log after csv_filter that prints the contents of $var($var(N11_resultvar_name)). I think you will find it also contains the correct value.


Ben Newlin

From: Users <users-bounces at lists.opensips.org> on behalf of Kingsley Tart <kingsley at dns99.co.uk>
Date: Friday, May 7, 2021 at 1:08 PM
To: Liviu Chircu <liviu at opensips.org>, OpenSIPS users mailling list <users at lists.opensips.org>
Subject: Re: [OpenSIPS-Users] routing function variable scope
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.


_______________________________________________
Users mailing list
Users at lists.opensips.org
http://lists.opensips.org/cgi-bin/mailman/listinfo/users
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.opensips.org/pipermail/users/attachments/20210507/ec60f99c/attachment.html>


More information about the Users mailing list