From 34117fc0247db14660b37c6ca5bfd54b5758f484 Mon Sep 17 00:00:00 2001 From: Anton Keks Date: Mon, 15 Feb 2021 20:43:25 +0200 Subject: [PATCH] do not fail if netmask is shorter than the IP (e.g. /32 for IPv6) --- src/net/azib/ipscan/util/InetAddressUtils.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/net/azib/ipscan/util/InetAddressUtils.java b/src/net/azib/ipscan/util/InetAddressUtils.java index b532f1c6..01daa3db 100644 --- a/src/net/azib/ipscan/util/InetAddressUtils.java +++ b/src/net/azib/ipscan/util/InetAddressUtils.java @@ -32,10 +32,10 @@ public class InetAddressUtils { public static final Pattern HOSTNAME_REGEX = Pattern.compile("\\b((([a-z]|[a-z0-9][a-z0-9\\-]*[a-z0-9])\\.)+([a-z]{2,})|\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3})\\b", CASE_INSENSITIVE); public static InetAddress startRangeByNetmask(InetAddress address, InetAddress netmask) { - byte[] netmaskBytes = netmask.getAddress(); byte[] addressBytes = address.getAddress(); + byte[] netmaskBytes = netmask.getAddress(); for (int i = 0; i < addressBytes.length; i++) { - addressBytes[i] = (byte) (addressBytes[i] & netmaskBytes[i]); + addressBytes[i] = i < netmaskBytes.length ? (byte) (addressBytes[i] & netmaskBytes[i]) : 0; } try { return InetAddress.getByAddress(addressBytes); @@ -50,7 +50,7 @@ public class InetAddressUtils { byte[] netmaskBytes = netmask.getAddress(); byte[] addressBytes = address.getAddress(); for (int i = 0; i < addressBytes.length; i++) { - addressBytes[i] = (byte) (addressBytes[i] | ~(netmaskBytes[i])); + addressBytes[i] = (byte) (i < netmaskBytes.length ? (addressBytes[i] | ~(netmaskBytes[i])) : 255); } try { return InetAddress.getByAddress(addressBytes);