Class IpV4Subnet

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

    public class IpV4Subnet
    extends java.lang.Object
    implements IpSet, java.lang.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 Summary

      Constructors 
      Constructor Description
      IpV4Subnet()
      Create IpV4Subnet for ALL (used for ALLOW or DENY ALL)
      IpV4Subnet​(java.lang.String netAddress)
      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");
      IpV4Subnet​(java.net.InetAddress inetAddress, int cidrNetMask)
      Create IpV4Subnet using the CIDR Notation
      IpV4Subnet​(java.net.InetAddress inetAddress, java.lang.String netMask)
      Create IpV4Subnet using the normal Notation
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      int compareTo​(IpV4Subnet o)
      Compare two IpV4Subnet
      boolean contains​(java.lang.String ipAddr)
      Compares the given IP-Address against the Subnet and returns true if the ip is in the subnet-ip-range and false if not.
      boolean contains​(java.net.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.
      boolean equals​(java.lang.Object o)  
      int hashCode()  
      private void setCidrNetMask​(int cidrNetMask)
      Sets the CIDR Netmask
      i.e.: setCidrNetMask(24);
      private void setNetAddress​(java.lang.String netAddress)
      Sets the Network Address in either CIDR or Decimal Notation.
      i.e.: setNetAddress("1.1.1.1/24"); or
      setNetAddress("1.1.1.1/255.255.255.0");
      private void setNetAddress​(java.net.InetAddress inetAddress, int cidrNetMask)
      Sets the Network Address in CIDR Notation.
      private void setNetAddress​(java.net.InetAddress inetAddress, java.lang.String netMask)
      Sets the Network Address in Decimal Notation.
      private void setNetId​(java.lang.String netId)
      Sets the BaseAdress of the Subnet.
      i.e.: setNetId("192.168.1.0");
      private void setNetId​(java.net.InetAddress inetAddress)
      Sets the BaseAdress of the Subnet.
      private void setNetMask​(java.lang.String netMask)
      Sets the Subnet's Netmask in Decimal format.
      i.e.: setNetMask("255.255.255.0");
      private static int toInt​(java.net.InetAddress inetAddress1)
      Compute integer representation of InetAddress
      java.lang.String toString()  
      • Methods inherited from class java.lang.Object

        clone, finalize, getClass, notify, notifyAll, wait, wait, wait
    • Field Detail

      • inetAddress

        private java.net.InetAddress inetAddress
      • subnet

        private int subnet
      • mask

        private int mask
      • cidrMask

        private int cidrMask
    • Constructor Detail

      • IpV4Subnet

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

        public IpV4Subnet​(java.lang.String netAddress)
                   throws java.net.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:
        java.net.UnknownHostException
      • IpV4Subnet

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

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

      • setNetAddress

        private void setNetAddress​(java.lang.String netAddress)
                            throws java.net.UnknownHostException
        Sets the Network Address in either CIDR or Decimal Notation.
        i.e.: setNetAddress("1.1.1.1/24"); or
        setNetAddress("1.1.1.1/255.255.255.0");
        Parameters:
        netAddress - a network address as string.
        Throws:
        java.net.UnknownHostException
      • setNetAddress

        private void setNetAddress​(java.net.InetAddress inetAddress,
                                   int cidrNetMask)
        Sets the Network Address in CIDR Notation.
      • setNetAddress

        private void setNetAddress​(java.net.InetAddress inetAddress,
                                   java.lang.String netMask)
        Sets the Network Address in Decimal Notation.
      • setNetId

        private void setNetId​(java.lang.String netId)
                       throws java.net.UnknownHostException
        Sets the BaseAdress of the Subnet.
        i.e.: setNetId("192.168.1.0");
        Parameters:
        netId - a network ID
        Throws:
        java.net.UnknownHostException
      • toInt

        private static int toInt​(java.net.InetAddress inetAddress1)
        Compute integer representation of InetAddress
        Returns:
        the integer representation
      • setNetId

        private void setNetId​(java.net.InetAddress inetAddress)
        Sets the BaseAdress of the Subnet.
      • setNetMask

        private void setNetMask​(java.lang.String netMask)
        Sets the Subnet's Netmask in Decimal format.
        i.e.: setNetMask("255.255.255.0");
        Parameters:
        netMask - a network mask
      • setCidrNetMask

        private void setCidrNetMask​(int cidrNetMask)
        Sets the CIDR Netmask
        i.e.: setCidrNetMask(24);
        Parameters:
        cidrNetMask - a netmask in CIDR notation
      • contains

        public boolean contains​(java.lang.String ipAddr)
                         throws java.net.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:
        java.net.UnknownHostException
      • contains

        public boolean contains​(java.net.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.
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object
      • equals

        public boolean equals​(java.lang.Object o)
        Overrides:
        equals in class java.lang.Object
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class java.lang.Object
      • compareTo

        public int compareTo​(IpV4Subnet o)
        Compare two IpV4Subnet
        Specified by:
        compareTo in interface java.lang.Comparable<IpV4Subnet>