diff --git a/core-java-modules/core-java-networking-3/pom.xml b/core-java-modules/core-java-networking-3/pom.xml index 0ad800e173..d3b2398b75 100644 --- a/core-java-modules/core-java-networking-3/pom.xml +++ b/core-java-modules/core-java-networking-3/pom.xml @@ -47,6 +47,24 @@ javax.mail 1.6.2 + + + com.github.seancfoley + ipaddress + ${seancfoley.ipaddress.version} + + + + com.github.jgonian + commons-ip-math + ${jgonian.commons-ip-math.version} + + + + com.googlecode.java-ipv6 + java-ipv6 + ${googlecode.ipv6.version} + @@ -66,6 +84,9 @@ 9.4.31.v20200723 10.0.0-M7 3.11.1 + 5.3.3 + 1.32 + 0.17 \ No newline at end of file diff --git a/core-java-modules/core-java-networking-3/src/main/java/com/baeldung/ipingivenrange/IPWithGivenRangeCheck.java b/core-java-modules/core-java-networking-3/src/main/java/com/baeldung/ipingivenrange/IPWithGivenRangeCheck.java new file mode 100644 index 0000000000..634c88ccba --- /dev/null +++ b/core-java-modules/core-java-networking-3/src/main/java/com/baeldung/ipingivenrange/IPWithGivenRangeCheck.java @@ -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); + } +} diff --git a/core-java-modules/core-java-networking-3/src/test/java/com/baeldung/ipingivenrange/IPWithGivenRangeCheckUnitTest.java b/core-java-modules/core-java-networking-3/src/test/java/com/baeldung/ipingivenrange/IPWithGivenRangeCheckUnitTest.java new file mode 100644 index 0000000000..a9b54834d5 --- /dev/null +++ b/core-java-modules/core-java-networking-3/src/test/java/com/baeldung/ipingivenrange/IPWithGivenRangeCheckUnitTest.java @@ -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")); + } +}