Class IpV4Subnet

  • All Implemented Interfaces:
    Comparable<IpV4Subnet>, IpSet
    Direct Known Subclasses:
    IpV4SubnetFilterRule

    public class IpV4Subnet
    extends Object
    implements IpSet, Comparable<IpV4Subnet>
    This class allows to check if an IP-V4-Address is contained in a subnet.
    Supported Formats for the Subnets are: 1.1.1.1/255.255.255.255 or 1.1.1.1/32 (CIDR-Notation) and (InetAddress,Mask) where Mask is a integer for CIDR-notation or a String for Standard Mask notation.


    Example1:
    IpV4Subnet ips = new IpV4Subnet("192.168.1.0/24");
    System.out.println("Result: "+ ips.contains("192.168.1.123"));
    System.out.println("Result: "+ ips.contains(inetAddress2));

    Example1 bis:
    IpV4Subnet ips = new IpV4Subnet(inetAddress, 24);
    where inetAddress is 192.168.1.0 and inetAddress2 is 192.168.1.123


    Example2:
    IpV4Subnet ips = new IpV4Subnet("192.168.1.0/255.255.255.0");
    System.out.println("Result: "+ ips.contains("192.168.1.123"));
    System.out.println("Result: "+ ips.contains(inetAddress2));

    Example2 bis:
    IpV4Subnet ips = new IpV4Subnet(inetAddress, "255.255.255.0");
    where inetAddress is 192.168.1.0 and inetAddress2 is 192.168.1.123
    • Constructor Detail

      • IpV4Subnet

        public IpV4Subnet()
        Create IpV4Subnet for ALL (used for ALLOW or DENY ALL)
      • IpV4Subnet

        public IpV4Subnet​(String netAddress)
                   throws UnknownHostException
        Create IpV4Subnet using the CIDR or normal Notation
        i.e.: IpV4Subnet subnet = new IpV4Subnet("10.10.10.0/24"); or IpV4Subnet subnet = new IpV4Subnet("10.10.10.0/255.255.255.0");
        Parameters:
        netAddress - a network address as string.
        Throws:
        UnknownHostException
      • IpV4Subnet

        public IpV4Subnet​(InetAddress inetAddress,
                          int cidrNetMask)
        Create IpV4Subnet using the CIDR Notation
      • IpV4Subnet

        public IpV4Subnet​(InetAddress inetAddress,
                          String netMask)
        Create IpV4Subnet using the normal Notation
    • Method Detail

      • contains

        public boolean contains​(String ipAddr)
                         throws UnknownHostException
        Compares the given IP-Address against the Subnet and returns true if the ip is in the subnet-ip-range and false if not.
        Parameters:
        ipAddr - an ipaddress
        Returns:
        returns true if the given IP address is inside the currently set network.
        Throws:
        UnknownHostException
      • contains

        public boolean contains​(InetAddress inetAddress1)
        Compares the given InetAddress against the Subnet and returns true if the ip is in the subnet-ip-range and false if not.
        Specified by:
        contains in interface IpSet
        Returns:
        returns true if the given IP address is inside the currently set network.
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class Object