[OpenSIPS-Devel] 2.4 new IP and Subnet comparison.

Bogdan-Andrei Iancu bogdan at opensips.org
Thu Aug 9 05:07:52 EDT 2018


Hi Adam,

The errors you get at runtime (the script is successfully parsed and 
compiled) are from these lines:

   $var(my_real_subnet) = ( $(var(input_subnet){ip.pton}) & 
$(var(input_mask){ip.pton}) );
   $var(my_src_net) = ( $(var(input_ip){ip.pton}) & 
$(var(input_mask){ip.pton}) );

The correct way to test against a subnet is the option 3:


   $var(input_ip) = "10.10.10.10";
   if ($var(input_ip) == 10.10.10.0/24)
     route(rlog, LV_INFO, "Test3");

I just tested as:

startup_route {
   xlog("---starting----\n");
   $var(input_ip) = "10.10.10.10";
   if ($var(input_ip) == 10.10.10.0/24)
     xlog("matched\n");
   xlog("---starting done----\n");
}

and works as expected

Aug  9 12:04:29 [25365] ---starting----
Aug  9 12:04:29 [25365] DBG:core:comp_scriptvar: ip 20 : 10.10.10.10
Aug  9 12:04:29 [25365] matched
Aug  9 12:04:29 [25365] ---starting done----

Regards,

Bogdan-Andrei Iancu

OpenSIPS Founder and Developer
   http://www.opensips-solutions.com
OpenSIPS Bootcamp 2018
   http://opensips.org/training/OpenSIPS_Bootcamp_2018/

On 08/08/2018 09:22 PM, Adam Overbeeke wrote:
> Hi Bogdan,
>
> I am providing a piece of my script file (below) and the corresponding output. The route `ip_in_cidr` is called directly from the startup route, for testing purposes.
>
> I can't seem to get the right syntax for my script. Basically I would like to determine if an IP address is in a given subnet. As you can see I am trying to use 2.4 style comparisons for the last 3 IF statements. The first statement passes, for the wrong reasons... Seems that pton is have an issue transforming the IP address.
>
> Thanks,
>
> Adam
>
>
>
>
> ------- OUTPUT -------
>
> Aug  8 18:03:38 [336] { "t": "2018-08-08T18:03:38.509832Z", "logger_name": "test", "thread_name": 336, "level": "INFO", "route": "startup" }
> Aug  8 18:03:38 [336] { "t": "2018-08-08T18:03:38.509913Z", "logger_name": " test ", "thread_name": 336, "level": "INFO", "route": "ip_in_cidr" }
> Aug  8 18:03:38 [336] { "t": "2018-08-08T18:03:38.510018Z", "logger_name": " test ", "thread_name": 336, "level": "INFO", "route": "ip_in_cidr", "message": { "mask": -256, "mask_str": "255.255.255.0" } }
> Aug  8 18:03:38 [336] ERROR:core:tr_eval_ip: pton transformation applied to invalid IP
> Aug  8 18:03:38 [336] ERROR:core:eval_elem: Invalid operator : 10
> Aug  8 18:03:38 [336] WARNING:core:do_assign: no value in right expression at /opensips/control.cfg:1613
> Aug  8 18:03:38 [336] ERROR:core:tr_eval_ip: pton transformation applied to invalid IP
> Aug  8 18:03:38 [336] ERROR:core:eval_elem: Invalid operator : 10
> Aug  8 18:03:38 [336] WARNING:core:do_assign: no value in right expression at /opensips/control.cfg:1614
> Aug  8 18:03:38 [336] { "t": "2018-08-08T18:03:38.510179Z", "logger_name": " test ", "thread_name": 336, "level": "INFO", "route": "10.10.10.0\/24" }
>
> --- SNIPPET FROM SCRIPT ---
>
> route[ip_in_cidr]
> {
>    route(rlog, LV_INFO, "ip_in_cidr");
>
>    $var(input_mask) = route(cidr_to_mask, 24); # Confirmed returns "255.255.255.0"
>    $var(input_subnet) = "10.10.10.0";
>    $var(input_ip) = "10.10.10.10";
>
>    $var(my_real_subnet) = ( $(var(input_subnet){ip.pton}) & $(var(input_mask){ip.pton}) );
>    $var(my_src_net) = ( $(var(input_ip){ip.pton}) & $(var(input_mask){ip.pton}) );
>
>    if ( $var(my_src_net) == $var(my_real_subnet) ) # This passes however... look at the errors from the output above.
>      route(rlog, LV_INFO, "10.10.10.0/24");
>
>    $var(input_ip) = "10.10.10.10";
>    if ($var(input_ip) == "10.10.10.0/24")
>      route(rlog, LV_INFO, "Test1");
>
>    $var(input_ip) = "10.10.10.10";
>    if ($var(input_ip) == '10.10.10.0/24')
>      route(rlog, LV_INFO, "Test2");
>
>    $var(input_ip) = "10.10.10.10";
>    if ($var(input_ip) == 10.10.10.0/24) # Fails to compile here
>      route(rlog, LV_INFO, "Test3");
> }
>
> route[cidr_to_mask]
> {
>    $var(cidr_to_mask_iter) = 0;
>    $var(mask_str) = "";
>    $var(num_ff_octets) = $param(1) / 8;
>    $var(partial_octet) = $param(1) % 8;
>    
>    while ($var(cidr_to_mask_iter) < 4)
>    {
>      if ($var(cidr_to_mask_iter) != 0) {
>        $var(mask_str) = $var(mask_str) + ".";
>      }
>
>      if ($var(cidr_to_mask_iter) < $var(num_ff_octets)) {
>        $var(mask_str) = $var(mask_str) + "255";
>      } else {
>        $var(last_octet) = ((255 << (8 - $var(partial_octet))) & 255);
>        $var(mask_str) = $var(mask_str) + $var(last_octet);
>      }
>
>      $var(cidr_to_mask_iter) = $var(cidr_to_mask_iter) + 1;
>    }
>
>    return($var(mask_str));
> }
>




More information about the Devel mailing list