Skip to content

Commit cba4441

Browse files
[BAEL-4478] IP address is in given range or not using java
1 parent 6caa0cb commit cba4441

3 files changed

Lines changed: 160 additions & 0 deletions

File tree

core-java-modules/core-java-networking-3/pom.xml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,24 @@
4747
<artifactId>javax.mail</artifactId>
4848
<version>1.6.2</version>
4949
</dependency>
50+
<!-- https://mvnrepository.com/artifact/com.github.seancfoley/ipaddress -->
51+
<dependency>
52+
<groupId>com.github.seancfoley</groupId>
53+
<artifactId>ipaddress</artifactId>
54+
<version>${seancfoley.ipaddress.version}</version>
55+
</dependency>
56+
<!-- https://mvnrepository.com/artifact/com.github.jgonian/commons-ip-math -->
57+
<dependency>
58+
<groupId>com.github.jgonian</groupId>
59+
<artifactId>commons-ip-math</artifactId>
60+
<version>${jgonian.commons-ip-math.version}</version>
61+
</dependency>
62+
<!-- https://mvnrepository.com/artifact/com.googlecode.java-ipv6/java-ipv6 -->
63+
<dependency>
64+
<groupId>com.googlecode.java-ipv6</groupId>
65+
<artifactId>java-ipv6</artifactId>
66+
<version>${googlecode.ipv6.version}</version>
67+
</dependency>
5068
</dependencies>
5169

5270
<build>
@@ -66,6 +84,9 @@
6684
<jetty.embeded.version>9.4.31.v20200723</jetty.embeded.version>
6785
<tomcat.embeded.version>10.0.0-M7</tomcat.embeded.version>
6886
<assertj.version>3.11.1</assertj.version>
87+
<seancfoley.ipaddress.version>5.3.3</seancfoley.ipaddress.version>
88+
<jgonian.commons-ip-math.version>1.32</jgonian.commons-ip-math.version>
89+
<googlecode.ipv6.version>0.17</googlecode.ipv6.version>
6990
</properties>
7091

7192
</project>
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package com.baeldung.ipingivenrange;
2+
3+
import java.net.InetAddress;
4+
import java.net.UnknownHostException;
5+
6+
import inet.ipaddr.IPAddress;
7+
import inet.ipaddr.IPAddressSeqRange;
8+
import inet.ipaddr.IPAddressString;
9+
import inet.ipaddr.AddressStringException;
10+
11+
import com.github.jgonian.ipmath.Ipv4;
12+
import com.github.jgonian.ipmath.Ipv4Range;
13+
import com.github.jgonian.ipmath.Ipv6;
14+
import com.github.jgonian.ipmath.Ipv6Range;
15+
import com.googlecode.ipv6.IPv6Address;
16+
import com.googlecode.ipv6.IPv6AddressRange;
17+
18+
public class IPWithGivenRangeCheck {
19+
20+
// using IPAddress library
21+
public static boolean checkIPIsInGivenRange(String inputIP, String rangeStartIP, String rangeEndIP) throws AddressStringException {
22+
IPAddress startIPAddress = new IPAddressString(rangeStartIP).getAddress();
23+
IPAddress endIPAddress = new IPAddressString(rangeEndIP).getAddress();
24+
IPAddressSeqRange ipRange = startIPAddress.toSequentialRange(endIPAddress);
25+
IPAddress inputIPAddress = new IPAddressString(inputIP).toAddress();
26+
27+
return ipRange.contains(inputIPAddress);
28+
}
29+
30+
// using Commons IP Math library for IPv4
31+
public static boolean checkIPv4IsInRange(String inputIP, String rangeStartIP, String rangeEndIP) {
32+
Ipv4 startIPAddress = Ipv4.of(rangeStartIP);
33+
Ipv4 endIPAddress = Ipv4.of(rangeEndIP);
34+
Ipv4Range ipRange = Ipv4Range.from(startIPAddress)
35+
.to(endIPAddress);
36+
Ipv4 inputIPAddress = Ipv4.of(inputIP);
37+
38+
return ipRange.contains(inputIPAddress);
39+
}
40+
41+
// using Commons IP Math library for IPv6
42+
public static boolean checkIPv6IsInRange(String inputIP, String rangeStartIP, String rangeEndIP) {
43+
Ipv6 startIPAddress = Ipv6.of(rangeStartIP);
44+
Ipv6 endIPAddress = Ipv6.of(rangeEndIP);
45+
Ipv6Range ipRange = Ipv6Range.from(startIPAddress)
46+
.to(endIPAddress);
47+
Ipv6 inputIPAddress = Ipv6.of(inputIP);
48+
49+
return ipRange.contains(inputIPAddress);
50+
}
51+
52+
// checking IP is in range by converting it to an integer
53+
public static boolean checkIPv4IsInRangeByConvertingToInt(String inputIP, String rangeStartIP, String rangeEndIP) throws UnknownHostException {
54+
long startIPAddress = ipToLongInt(InetAddress.getByName(rangeStartIP));
55+
long endIPAddress = ipToLongInt(InetAddress.getByName(rangeEndIP));
56+
long inputIPAddress = ipToLongInt(InetAddress.getByName(inputIP));
57+
58+
return (inputIPAddress >= startIPAddress && inputIPAddress <= endIPAddress);
59+
}
60+
61+
private static long ipToLongInt(InetAddress ipAddress) {
62+
long resultIP = 0;
63+
byte[] ipAddressOctets = ipAddress.getAddress();
64+
65+
for (byte octet : ipAddressOctets) {
66+
resultIP <<= 8;
67+
resultIP |= octet & 0xFF;
68+
}
69+
return resultIP;
70+
}
71+
72+
// using Java IPv6 library (which internally uses two long integers to store ip address)
73+
public static boolean checkIPv6IsInRangeByIPv6library(String inputIP, String rangeStartIP, String rangeEndIP) {
74+
IPv6Address startIPAddress = IPv6Address.fromString(rangeStartIP);
75+
IPv6Address endIPAddress = IPv6Address.fromString(rangeEndIP);
76+
IPv6AddressRange ipRange = IPv6AddressRange.fromFirstAndLast(startIPAddress, endIPAddress);
77+
IPv6Address inputIPAddress = IPv6Address.fromString(inputIP);
78+
79+
return ipRange.contains(inputIPAddress);
80+
}
81+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package com.baeldung.ipingivenrange;
2+
3+
import static org.junit.jupiter.api.Assertions.*;
4+
5+
import org.junit.jupiter.api.Test;
6+
7+
import com.baeldung.ipingivenrange.IPWithGivenRangeCheck;
8+
9+
class IPWithGivenRangeCheckUnitTest {
10+
11+
@Test
12+
void givenIPv4Addresses_whenIsInRange_thenReturnsTrue() throws Exception {
13+
// test for IPAddress library
14+
assertTrue(IPWithGivenRangeCheck.checkIPIsInGivenRange("192.220.3.0", "192.210.0.0", "192.255.0.0"));
15+
16+
// test for Common IP Math library
17+
assertTrue(IPWithGivenRangeCheck.checkIPv4IsInRange("192.220.3.0", "192.210.0.0", "192.255.0.0"));
18+
19+
// test for IPv4 by converting it to an integer and checking if it falls under the specified range.
20+
assertTrue(IPWithGivenRangeCheck.checkIPv4IsInRangeByConvertingToInt("192.220.3.0", "192.210.0.0", "192.255.0.0"));
21+
}
22+
23+
@Test
24+
void givenIPv4Addresses_whenIsNotInRange_thenReturnsFalse() throws Exception {
25+
// test for IPAddress library
26+
assertFalse(IPWithGivenRangeCheck.checkIPIsInGivenRange("192.200.0.0", "192.210.0.0", "192.255.0.0"));
27+
28+
// test for Common IP Math library
29+
assertFalse(IPWithGivenRangeCheck.checkIPv4IsInRange("192.200.0.0", "192.210.0.0", "192.255.0.0"));
30+
31+
// test for IPv4 by converting it to an integer and checking if it falls under the specified range.
32+
assertFalse(IPWithGivenRangeCheck.checkIPv4IsInRangeByConvertingToInt("192.200.0.0", "192.210.0.0", "192.255.0.0"));
33+
}
34+
35+
@Test
36+
void givenIPv6Addresses_whenIsInRange_thenReturnsTrue() throws Exception {
37+
// test for IPAddress library
38+
assertTrue(IPWithGivenRangeCheck.checkIPIsInGivenRange("2001:db8:85a3::8a03:a:b", "2001:db8:85a3::8a00:ff:ffff", "2001:db8:85a3::8a2e:370:7334"));
39+
40+
// test for Common IP Math library
41+
assertTrue(IPWithGivenRangeCheck.checkIPv6IsInRange("2001:db8:85a3::8a03:a:b", "2001:db8:85a3::8a00:ff:ffff", "2001:db8:85a3::8a2e:370:7334"));
42+
43+
// test for Java IPv6 library
44+
assertTrue(IPWithGivenRangeCheck.checkIPv6IsInRangeByIPv6library("fe80::226:2dff:fefa:dcba", "fe80::226:2dff:fefa:cd1f", "fe80::226:2dff:fefa:ffff"));
45+
}
46+
47+
@Test
48+
void givenIPv6Addresses_whenIsNotInRange_thenReturnsFalse() throws Exception {
49+
// test for IPAddress library
50+
assertFalse(IPWithGivenRangeCheck.checkIPIsInGivenRange("2002:db8:85a3::8a03:a:b", "2001:db8:85a3::8a00:ff:ffff", "2001:db8:85a3::8a2e:370:7334"));
51+
52+
// test for Common IP Math library
53+
assertFalse(IPWithGivenRangeCheck.checkIPv6IsInRange("2002:db8:85a3::8a03:a:b", "2001:db8:85a3::8a00:ff:ffff", "2001:db8:85a3::8a2e:370:7334"));
54+
55+
// test for Java IPv6 library
56+
assertFalse(IPWithGivenRangeCheck.checkIPv6IsInRangeByIPv6library("2002:db8:85a3::8a03:a:b", "2001:db8:85a3::8a00:ff:ffff", "2001:db8:85a3::8a2e:370:7334"));
57+
}
58+
}

0 commit comments

Comments
 (0)