Class IpSubnet

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

    public class IpSubnet
    extends java.lang.Object
    implements IpSet, java.lang.Comparable<IpSubnet>
    This class allows to check if an IP V4 or V6 Address is contained in a subnet.

    Supported IP V4 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

    Supported IP V6 Formats for the Subnets are: a:b:c:d:e:f:g:h/NN (CIDR-Notation) or any IPV6 notations (like a:b:c:d::/NN, a:b:c:d:e:f:w.x.y.z/NN) and (InetAddress,Mask) where Mask is a integer for CIDR-notation and (InetAddress,subnet).


    Example1:
    IpSubnet ips = new IpSubnet("1fff:0:0a88:85a3:0:0:0:0/24");
    IpSubnet ips = new IpSubnet("1fff:0:0a88:85a3::/24");
    System.out.println("Result: "+ ips.contains("1fff:0:0a88:85a3:0:0:ac1f:8001"));
    System.out.println("Result: "+ ips.contains(inetAddress2));

    Example1 bis:
    IpSubnet ips = new IpSubnet(inetAddress, 24);
    where inetAddress2 is 1fff:0:0a88:85a3:0:0:ac1f:8001

    • Constructor Summary

      Constructors 
      Constructor Description
      IpSubnet()
      Create IpSubnet for ALL (used for ALLOW or DENY ALL)
      IpSubnet​(java.lang.String netAddress)
      Create IpSubnet using the CIDR or normal Notation
      i.e.:
      IpSubnet subnet = new IpSubnet("10.10.10.0/24"); or
      IpSubnet subnet = new IpSubnet("10.10.10.0/255.255.255.0"); or
      IpSubnet subnet = new IpSubnet("1fff:0:0a88:85a3:0:0:0:0/24");
      IpSubnet​(java.net.InetAddress inetAddress, int cidrNetMask)
      Create IpSubnet using the CIDR Notation
      IpSubnet​(java.net.InetAddress inetAddress, java.lang.String netMask)
      Create IpSubnet using the normal Notation
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      int compareTo​(IpSubnet o)
      Compare two IpSubnet
      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 inetAddress)
      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()  
      java.lang.String toString()  
      • Methods inherited from class java.lang.Object

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

      • cidr

        private final CIDR cidr
        Internal representation
    • Constructor Detail

      • IpSubnet

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

        public IpSubnet​(java.lang.String netAddress)
                 throws java.net.UnknownHostException
        Create IpSubnet using the CIDR or normal Notation
        i.e.:
        IpSubnet subnet = new IpSubnet("10.10.10.0/24"); or
        IpSubnet subnet = new IpSubnet("10.10.10.0/255.255.255.0"); or
        IpSubnet subnet = new IpSubnet("1fff:0:0a88:85a3:0:0:0:0/24");
        Parameters:
        netAddress - a network address as string.
        Throws:
        java.net.UnknownHostException
      • IpSubnet

        public IpSubnet​(java.net.InetAddress inetAddress,
                        int cidrNetMask)
                 throws java.net.UnknownHostException
        Create IpSubnet using the CIDR Notation
        Throws:
        java.net.UnknownHostException
      • IpSubnet

        public IpSubnet​(java.net.InetAddress inetAddress,
                        java.lang.String netMask)
                 throws java.net.UnknownHostException
        Create IpSubnet using the normal Notation
        Throws:
        java.net.UnknownHostException
    • Method Detail

      • 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 inetAddress)
        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​(IpSubnet o)
        Compare two IpSubnet
        Specified by:
        compareTo in interface java.lang.Comparable<IpSubnet>