Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calculate whether an IP address is in a specified range in Java

I want to be able to return true/false depending on an IP being in range of two other IPs.

For instance:

ip 192.200.3.0

range from 192.200.0.0

range to 192.255.0.0

should result to true.

Other examples:

assert 192.200.1.0 == true assert 192.199.1.1 == false assert 197.200.1.0 == false 
like image 622
Shervin Asgari Avatar asked Nov 23 '10 13:11

Shervin Asgari


People also ask

How do you check if an IP address is in a range with Java?

Using Java's InetAddress Class for IPv4 We can check if it falls under the given range. Java's InetAddress class represents an IP address and provides methods to get the IP for any given hostnames. An instance of InetAddress represents the IP address with its corresponding hostname.

How many IP addresses are in a range?

Addresses in IPv4 are 32-bits long. This allows for a maximum of 4,294,967,296 (232) unique addresses. Addresses in IPv6 are 128-bits, which allows for 3.4 x 1038 (2128) unique addresses. The total usable address pool of both versions is reduced by various reserved addresses and other considerations.

How do I find the IP range of a domain?

Typically you can find the IP address for a domain by pinging the domain. This can be done from your local PC. However, the steps may vary depending on the OS that you're using. To simplify this process, we can use an online Ping tool, such as https://ping.eu/ping/.


2 Answers

The easiest way to check the range is probably to convert the IP addresses to 32-bit integers and then just compare the integers.

public class Example {     public static long ipToLong(InetAddress ip) {         byte[] octets = ip.getAddress();         long result = 0;         for (byte octet : octets) {             result <<= 8;             result |= octet & 0xff;         }         return result;     }      public static void main(String[] args) throws UnknownHostException {         long ipLo = ipToLong(InetAddress.getByName("192.200.0.0"));         long ipHi = ipToLong(InetAddress.getByName("192.255.0.0"));         long ipToTest = ipToLong(InetAddress.getByName("192.200.3.0"));          System.out.println(ipToTest >= ipLo && ipToTest <= ipHi);     } } 

Rather than InetAddress.getByName(), you may want to look at the Guava library which has an InetAddresses helper class that avoids the possibility of DNS lookups.

like image 57
hallidave Avatar answered Sep 20 '22 17:09

hallidave


The following code, using the IPAddress Java library (Disclaimer: I am the project manager) handles this with both IPv4 and IPv6 addresses, and also avoids DNS lookup on invalid strings.

Here is some sample code with your given addresses as well as some IPv6 addresses:

static void range(String lowerStr, String upperStr, String str)         throws AddressStringException  {     IPAddress lower = new IPAddressString(lowerStr).toAddress();     IPAddress upper = new IPAddressString(upperStr).toAddress();     IPAddress addr = new IPAddressString(str).toAddress();     IPAddressSeqRange range = lower.toSequentialRange(upper);     System.out.println(range + " contains " + addr + " " + range.contains(addr)); }  range("192.200.0.0", "192.255.0.0", "192.200.3.0"); range("2001:0db8:85a3::8a2e:0370:7334", "2001:0db8:85a3::8a00:ff:ffff",      "2001:0db8:85a3::8a03:a:b"); range("192.200.0.0", "192.255.0.0", "191.200.3.0"); range("2001:0db8:85a3::8a2e:0370:7334", "2001:0db8:85a3::8a00:ff:ffff",      "2002:0db8:85a3::8a03:a:b"); 

Output:

192.200.0.0 -> 192.255.0.0 contains 192.200.3.0 true 2001:db8:85a3::8a00:ff:ffff -> 2001:db8:85a3::8a2e:370:7334 contains 2001:db8:85a3::8a03:a:b true 192.200.0.0 -> 192.255.0.0 contains 191.200.3.0 false 2001:db8:85a3::8a00:ff:ffff -> 2001:db8:85a3::8a2e:370:7334 contains 2002:db8:85a3::8a03:a:b false 
like image 44
Sean F Avatar answered Sep 21 '22 17:09

Sean F