[OpenSIPS-Users] check if ip address belongs to ip and subnet subscriber

Nick Altmann nick.altmann at gmail.com
Tue Feb 4 13:29:52 CET 2014


My old script for check subnets for SER:

# Check IP belongs to subnet
# ----------------------------------------------------------------------
# Parameters:
# * $var(ipr_check_net) - network address (ex. "192.168.1.0/24");
# * $var(ipr_check_ip)  - ip-address (ex. "192.168.1.1");
# ----------------------------------------------------------------------
# Result:
# * 1 - IP belongs to subnet
# * 0 - IP doesn't belong to subnet
# ----------------------------------------------------------------------
route[IPRANGE] {
    # Convert network address into decimal format
    $var(net_addr1) = $(var(ipr_check_net){s.select,0,.}{s.int}); # ip
net, part 1
    $var(net_addr2) = $(var(ipr_check_net){s.select,1,.}{s.int}); # ip
net, part 2
    $var(net_addr3) = $(var(ipr_check_net){s.select,2,.}{s.int}); # ip
net, part 3
    $var(net_addr4) = $(var(ipr_check_net){s.select,3,.}{s.int}); # ip
net, part 4
    $var(net_addr) = ($var(net_addr1)<<24) + ($var(net_addr2)<<16) +
($var(net_addr3)<<8) + ($var(net_addr4));
    # Get decimal network mask (because of integer is 32 bit)
    $var(net_mask) = -(1<<(32-($(var(ipr_check_net){s.select,1,/}{s.int}))));

    # Convert IP address into decimal format
    $var(ip_addr1) = $(var(ipr_check_ip){s.select,0,.}{s.int}); # ip
addr, part 1
    $var(ip_addr2) = $(var(ipr_check_ip){s.select,1,.}{s.int}); # ip
addr, part 2
    $var(ip_addr3) = $(var(ipr_check_ip){s.select,2,.}{s.int}); # ip
addr, part 3
    $var(ip_addr4) = $(var(ipr_check_ip){s.select,3,.}{s.int}); # ip
addr, part 4
    $var(ip_addr) = ($var(ip_addr1)<<24) + ($var(ip_addr2)<<16) +
($var(ip_addr3)<<8) + ($var(ip_addr4));

    # Apply mask to IP address
    return ($var(net_addr) & $var(net_mask)) == ($var(ip_addr) &
$var(net_mask));
}

--
Nick


2014-02-04 Vlad Paiu <vladpaiu at opensips.org>:
> Hello,
>
> Internally, when using ip.pton, OpenSIPS stores the binary representation of
> the IP as a character array ( due to the different size of IPv4 vs IPv6 ),
> so using the & will not work properly - that's why you're code snippet is
> not working as expected.  Will try to look and see how to fix this - if
> possible at all.
>
> In the mean time, I'd strongly suggest using the permissions module - there
> is no performance penalty when reloading the address table ( while the new
> table info is loading, OpenSIPS will hold the old table info in memory , and
> once the loading is done there new and old IP lists will just be swapped ).
>
> I'd suggest having a address groupid integer stored in the subscriber table
> which needs be added in the load_credentials param ( to be loaded at auth
> time ), and then run
>     check_source_address("$avp(subscriber_grp)")
>
>
> Best Regards,
>
> Vlad Paiu
> OpenSIPS Developer
> http://www.opensips-solutions.com
>
> On 04.02.2014 09:24, Edwin wrote:
>>
>> Vlad,
>>
>> The $avp(sourceip_net) in the test was 255.255.255.0. I want to use a
>> netmask so clients can use any ip from the ip block we have assigned them.
>>
>> I'm also testing the permission module (as Stefano suggested) which is of
>> course perfect in this case. The only thing I 'worry about' is that
>> everytime a ip is changed in the address table and we hit the 'address
>> reload' is this has an impact on a live system with many registrations per
>> second... (so will there be a little timeout or does the process seamless
>> continue). This because clients can change there own ip / subnet in a web
>> based management system.
>>
>>
>>
>> --
>> View this message in context:
>> http://opensips-open-sip-server.1449251.n2.nabble.com/check-if-ip-address-belongs-to-ip-and-subnet-subscriber-tp7589375p7589419.html
>> Sent from the OpenSIPS - Users mailing list archive at Nabble.com.
>>
>> _______________________________________________
>> Users mailing list
>> Users at lists.opensips.org
>> http://lists.opensips.org/cgi-bin/mailman/listinfo/users
>
>
>
> _______________________________________________
> Users mailing list
> Users at lists.opensips.org
> http://lists.opensips.org/cgi-bin/mailman/listinfo/users



More information about the Users mailing list