|
| 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 | +} |
0 commit comments