Class IPTable


  • public class IPTable
    extends Object
    A Spare v4 IPTable implementation that uses nested HashMaps to optimize IP address matching over ranges of IP addresses.
    Author:
    mdiggory at atmire.com
    • Constructor Detail

      • IPTable

        public IPTable()
    • Method Detail

      • add

        public void add​(String ip)
                 throws IPTable.IPFormatException
        Can be full v4 IP, subnet or range string.
        • A full address is a complete dotted-quad: "1.2.3.4".
        • A subnet is a dotted-triplet: "1.2.3". It means an entire Class C subnet: "1.2.3.0-1.2.3.255".
        • A range is two dotted-quad addresses separated by hyphen: "1.2.3.4-1.2.3.14". Only the final octet may be different.
        Any attempt at CIDR notation is ignored.
        Parameters:
        ip - IP address(es)
        Throws:
        IPTable.IPFormatException - Exception Class to deal with IPFormat errors.
      • ipToLong

        public static long ipToLong​(InetAddress ip)
        Convert an IP address to a long integer
        Parameters:
        ip - the IP address
        Returns:
      • longToIp

        public static String longToIp​(long ip)
        Convert a long integer into an IP address string
        Parameters:
        ip - the IP address as a long integer
        Returns:
      • contains

        public boolean contains​(String ip)
                         throws IPTable.IPFormatException
        Check whether a given address is contained in this netblock.
        Parameters:
        ip - the address to be tested
        Returns:
        true if ip is within this table's limits. Returns false if ip looks like an IPv6 address.
        Throws:
        IPTable.IPFormatException - Exception Class to deal with IPFormat errors.
      • toSet

        public Set<String> toSet()
        Convert to a Set. This set contains all IPs in the range
        Returns:
        this table's content as a Set
      • isEmpty

        public boolean isEmpty()
        Return whether IPTable is empty (having no entries)
        Returns:
        true if empty, false otherwise
      • toString

        public String toString()
        Represent this IP table as a string
        Overrides:
        toString in class Object
        Returns:
        a string containing all IP ranges in this IP table