IPAddress: Facing problem with IPAddressString contains(IPAddressString other) method

Hello @seancfoley, I am using contains method to check whether one IPAddressString contains other,

Examples, 192.168.1.0/24 contains 192.168.1.0/12.(true) 192.168.1.0/24 contains 192.168.2.0/24.(false) 192.168.1.1-10 contains 192.168.1.5-7.(true) 192.168.1.5-7 contains 192.168.1.1-10.(false) 192.168.. contains 192.168.1..(true) 192.168.1. contains 192.168...(false) 192.168.1.0/24 contains 192.168.1.1-10.(true) 192.168.1.0/24 contains 192.168.2.1-10.(false) 192.168.1.* contains 192.168.1.0/24.(true)

All the above cases are working fine,but some of the below cases are failing,

  • Case-1
IIPAddressString ipAddressStringSubnetError1 = new IPAddressString( "10.162.155.1-51" );
        IPAddressString ipAddressStringSubnetError2 = new IPAddressString( "10.162.155.1-255" );
        System.out.println( String.format( "%s contains %s %s", ipAddressStringSubnetError2,
                ipAddressStringSubnetError1, ipAddressStringSubnetError2.contains( ipAddressStringSubnetError1 ) ) );

Output 10.162.155.1-255 contains 10.162.155.1-51 false

It should return true but it’s returning false.But if ipAddressStringSubnetError1 range is 10.162.155.2-51 then it returns true.The problem only occurs when both ranges start with same address(10.162.155.1).

NOTE: Same problem is there for IPV6 also.

  • Case-2
IPAddressString ipAddressStringSubnetError3 =
                new IPAddressString( "2001:0db8:85a3:0000:0000:8a2e:0370:7334/120" );
        IPAddressString ipAddressStringSubnetError4 =
                new IPAddressString( "2001:0db8:85a3:0000:0000:8a2e:0370:7334/128" );
        System.out.println( String.format( "%s contains %s %s", ipAddressStringSubnetError3,
                ipAddressStringSubnetError4, ipAddressStringSubnetError3.contains( ipAddressStringSubnetError4 ) ) );

        System.out.println( String.format( "%s contains %s %s", ipAddressStringSubnetError4,
                ipAddressStringSubnetError3, ipAddressStringSubnetError4.contains( ipAddressStringSubnetError3 ) ) );

Output

2001:0db8:85a3:0000:0000:8a2e:0370:7334/120 contains 2001:0db8:85a3:0000:0000:8a2e:0370:7334/128 true

2001:0db8:85a3:0000:0000:8a2e:0370:7334/128 contains 2001:0db8:85a3:0000:0000:8a2e:0370:7334/120 true

Subnet Calculator

IP Address:	2001:0db8:85a3:0000:0000:8a2e:0370:7334/120
Full IP Address:	2001:0db8:85a3:0000:0000:8a2e:0370:7334
Total IP Addresses:	256
Network:	2001:0db8:85a3:0000:0000:8a2e:0370:7300
IP Range:	2001:0db8:85a3:0000:0000:8a2e:0370:7300 - 2001:0db8:85a3:0000:0000:8a2e:0370:73ff


IP Address:	2001:0db8:85a3:0000:0000:8a2e:0370:7334/128
Full IP Address:	2001:0db8:85a3:0000:0000:8a2e:0370:7334
Total IP Addresses:	1
Network:	2001:0db8:85a3:0000:0000:8a2e:0370:7334
IP Range:	2001:0db8:85a3:0000:0000:8a2e:0370:7334 - 2001:0db8:85a3:0000:0000:8a2e:0370:7334

In the case2 both are returning true, but 2001:0db8:85a3:0000:0000:8a2e:0370:7334/128 contains 2001:0db8:85a3:0000:0000:8a2e:0370:7334/120 should return false.

Note: Same problem is there with IPV4 address also when address not end with zero.

-Case-3

IPAddressString ipAddressStringSubnetError5 = new IPAddressString( "192.13.1.0/25" );
        IPAddressString ipAddressStringSubnetError6 = new IPAddressString( "192.13.1.1-255" );
        System.out.println( String.format( "%s contains %s %s", ipAddressStringSubnetError5,
                ipAddressStringSubnetError6, ipAddressStringSubnetError5.contains( ipAddressStringSubnetError6 ) ) );
        System.out.println( String.format( "%s contains %s %s", ipAddressStringSubnetError6,
                ipAddressStringSubnetError5, ipAddressStringSubnetError6.contains( ipAddressStringSubnetError5 ) ) );

Output

192.13.1.0/25 contains 192.13.1.1-255 false
192.13.1.1-255 contains 192.13.1.0/25 false

In case3 i think 192.13.1.1-255 contains 192.13.1.0/25 should return true.

Thanks, Vamsi.

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Comments: 19 (11 by maintainers)

Most upvoted comments

I’ve fixed the issues identified in this bug. Those fixes are included in version 5.0.1 just released. If you find any others not working as expected, please let me know.