Merge pull request #11034 from KarthickSridhar/article_java_check_ip_is_in_given_range

[BAEL-4478] IP address is in given range or not using java
This commit is contained in:
doodoroma 2021-07-25 07:44:32 +02:00 committed by GitHub
commit eef71ba53c
3 changed files with 160 additions and 0 deletions

View File

@ -47,6 +47,24 @@
<artifactId>javax.mail</artifactId>
<version>1.6.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.github.seancfoley/ipaddress -->
<dependency>
<groupId>com.github.seancfoley</groupId>
<artifactId>ipaddress</artifactId>
<version>${seancfoley.ipaddress.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.github.jgonian/commons-ip-math -->
<dependency>
<groupId>com.github.jgonian</groupId>
<artifactId>commons-ip-math</artifactId>
<version>${jgonian.commons-ip-math.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.googlecode.java-ipv6/java-ipv6 -->
<dependency>
<groupId>com.googlecode.java-ipv6</groupId>
<artifactId>java-ipv6</artifactId>
<version>${googlecode.ipv6.version}</version>
</dependency>
</dependencies>
<build>
@ -66,6 +84,9 @@
<jetty.embeded.version>9.4.31.v20200723</jetty.embeded.version>
<tomcat.embeded.version>10.0.0-M7</tomcat.embeded.version>
<assertj.version>3.11.1</assertj.version>
<seancfoley.ipaddress.version>5.3.3</seancfoley.ipaddress.version>
<jgonian.commons-ip-math.version>1.32</jgonian.commons-ip-math.version>
<googlecode.ipv6.version>0.17</googlecode.ipv6.version>
</properties>
</project>

View File

@ -0,0 +1,81 @@
package com.baeldung.ipingivenrange;
import java.net.InetAddress;
import java.net.UnknownHostException;
import inet.ipaddr.IPAddress;
import inet.ipaddr.IPAddressSeqRange;
import inet.ipaddr.IPAddressString;
import inet.ipaddr.AddressStringException;
import com.github.jgonian.ipmath.Ipv4;
import com.github.jgonian.ipmath.Ipv4Range;
import com.github.jgonian.ipmath.Ipv6;
import com.github.jgonian.ipmath.Ipv6Range;
import com.googlecode.ipv6.IPv6Address;
import com.googlecode.ipv6.IPv6AddressRange;
public class IPWithGivenRangeCheck {
// using IPAddress library
public static boolean checkIPIsInGivenRange(String inputIP, String rangeStartIP, String rangeEndIP) throws AddressStringException {
IPAddress startIPAddress = new IPAddressString(rangeStartIP).getAddress();
IPAddress endIPAddress = new IPAddressString(rangeEndIP).getAddress();
IPAddressSeqRange ipRange = startIPAddress.toSequentialRange(endIPAddress);
IPAddress inputIPAddress = new IPAddressString(inputIP).toAddress();
return ipRange.contains(inputIPAddress);
}
// using Commons IP Math library for IPv4
public static boolean checkIPv4IsInRange(String inputIP, String rangeStartIP, String rangeEndIP) {
Ipv4 startIPAddress = Ipv4.of(rangeStartIP);
Ipv4 endIPAddress = Ipv4.of(rangeEndIP);
Ipv4Range ipRange = Ipv4Range.from(startIPAddress)
.to(endIPAddress);
Ipv4 inputIPAddress = Ipv4.of(inputIP);
return ipRange.contains(inputIPAddress);
}
// using Commons IP Math library for IPv6
public static boolean checkIPv6IsInRange(String inputIP, String rangeStartIP, String rangeEndIP) {
Ipv6 startIPAddress = Ipv6.of(rangeStartIP);
Ipv6 endIPAddress = Ipv6.of(rangeEndIP);
Ipv6Range ipRange = Ipv6Range.from(startIPAddress)
.to(endIPAddress);
Ipv6 inputIPAddress = Ipv6.of(inputIP);
return ipRange.contains(inputIPAddress);
}
// checking IP is in range by converting it to an integer
public static boolean checkIPv4IsInRangeByConvertingToInt(String inputIP, String rangeStartIP, String rangeEndIP) throws UnknownHostException {
long startIPAddress = ipToLongInt(InetAddress.getByName(rangeStartIP));
long endIPAddress = ipToLongInt(InetAddress.getByName(rangeEndIP));
long inputIPAddress = ipToLongInt(InetAddress.getByName(inputIP));
return (inputIPAddress >= startIPAddress && inputIPAddress <= endIPAddress);
}
private static long ipToLongInt(InetAddress ipAddress) {
long resultIP = 0;
byte[] ipAddressOctets = ipAddress.getAddress();
for (byte octet : ipAddressOctets) {
resultIP <<= 8;
resultIP |= octet & 0xFF;
}
return resultIP;
}
// using Java IPv6 library (which internally uses two long integers to store ip address)
public static boolean checkIPv6IsInRangeByIPv6library(String inputIP, String rangeStartIP, String rangeEndIP) {
IPv6Address startIPAddress = IPv6Address.fromString(rangeStartIP);
IPv6Address endIPAddress = IPv6Address.fromString(rangeEndIP);
IPv6AddressRange ipRange = IPv6AddressRange.fromFirstAndLast(startIPAddress, endIPAddress);
IPv6Address inputIPAddress = IPv6Address.fromString(inputIP);
return ipRange.contains(inputIPAddress);
}
}

View File

@ -0,0 +1,58 @@
package com.baeldung.ipingivenrange;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Test;
import com.baeldung.ipingivenrange.IPWithGivenRangeCheck;
class IPWithGivenRangeCheckUnitTest {
@Test
void givenIPv4Addresses_whenIsInRange_thenReturnsTrue() throws Exception {
// test for IPAddress library
assertTrue(IPWithGivenRangeCheck.checkIPIsInGivenRange("192.220.3.0", "192.210.0.0", "192.255.0.0"));
// test for Common IP Math library
assertTrue(IPWithGivenRangeCheck.checkIPv4IsInRange("192.220.3.0", "192.210.0.0", "192.255.0.0"));
// test for IPv4 by converting it to an integer and checking if it falls under the specified range.
assertTrue(IPWithGivenRangeCheck.checkIPv4IsInRangeByConvertingToInt("192.220.3.0", "192.210.0.0", "192.255.0.0"));
}
@Test
void givenIPv4Addresses_whenIsNotInRange_thenReturnsFalse() throws Exception {
// test for IPAddress library
assertFalse(IPWithGivenRangeCheck.checkIPIsInGivenRange("192.200.0.0", "192.210.0.0", "192.255.0.0"));
// test for Common IP Math library
assertFalse(IPWithGivenRangeCheck.checkIPv4IsInRange("192.200.0.0", "192.210.0.0", "192.255.0.0"));
// test for IPv4 by converting it to an integer and checking if it falls under the specified range.
assertFalse(IPWithGivenRangeCheck.checkIPv4IsInRangeByConvertingToInt("192.200.0.0", "192.210.0.0", "192.255.0.0"));
}
@Test
void givenIPv6Addresses_whenIsInRange_thenReturnsTrue() throws Exception {
// test for IPAddress library
assertTrue(IPWithGivenRangeCheck.checkIPIsInGivenRange("2001:db8:85a3::8a03:a:b", "2001:db8:85a3::8a00:ff:ffff", "2001:db8:85a3::8a2e:370:7334"));
// test for Common IP Math library
assertTrue(IPWithGivenRangeCheck.checkIPv6IsInRange("2001:db8:85a3::8a03:a:b", "2001:db8:85a3::8a00:ff:ffff", "2001:db8:85a3::8a2e:370:7334"));
// test for Java IPv6 library
assertTrue(IPWithGivenRangeCheck.checkIPv6IsInRangeByIPv6library("fe80::226:2dff:fefa:dcba", "fe80::226:2dff:fefa:cd1f", "fe80::226:2dff:fefa:ffff"));
}
@Test
void givenIPv6Addresses_whenIsNotInRange_thenReturnsFalse() throws Exception {
// test for IPAddress library
assertFalse(IPWithGivenRangeCheck.checkIPIsInGivenRange("2002:db8:85a3::8a03:a:b", "2001:db8:85a3::8a00:ff:ffff", "2001:db8:85a3::8a2e:370:7334"));
// test for Common IP Math library
assertFalse(IPWithGivenRangeCheck.checkIPv6IsInRange("2002:db8:85a3::8a03:a:b", "2001:db8:85a3::8a00:ff:ffff", "2001:db8:85a3::8a2e:370:7334"));
// test for Java IPv6 library
assertFalse(IPWithGivenRangeCheck.checkIPv6IsInRangeByIPv6library("2002:db8:85a3::8a03:a:b", "2001:db8:85a3::8a00:ff:ffff", "2001:db8:85a3::8a2e:370:7334"));
}
}