NetUtils { public static boolean inetAddressConflict(InetAddress testAddress, InetAddress filterAddress, InetAddress testMask, InetAddress filterMask) { if (testAddress == null || filterAddress == null) { return false; } if (isAny(testAddress) || isAny(filterAddress)) { return false; } int testMaskLen = testMask == null ? testAddress instanceof Inet4Address ? 32 : 128 : NetUtils.getSubnetMaskLength(testMask); int filterMaskLen = filterMask == null ? testAddress instanceof Inet4Address ? 32 : 128 : NetUtils.getSubnetMaskLength(filterMask); if (testMaskLen < filterMaskLen) { return true; } InetAddress prefix1 = getSubnetPrefix(testAddress, filterMaskLen); InetAddress prefix2 = getSubnetPrefix(filterAddress, filterMaskLen); return !prefix1.equals(prefix2); } private  NetUtils(); static int byteArray4ToInt(byte[] ba); static long byteArray6ToLong(byte[] ba); static byte[] longToByteArray6(long addr); static byte[] intToByteArray4(int value); static InetAddress getInetAddress(int address); static InetAddress getInetNetworkMask(int prefixMaskLength, boolean isV6); static int getSubnetMaskLength(byte[] subnetMask); static int getSubnetMaskLength(InetAddress subnetMask); static InetAddress getSubnetPrefix(InetAddress ip, int maskLen); static boolean inetAddressConflict(InetAddress testAddress, InetAddress filterAddress, InetAddress testMask,
            InetAddress filterMask); static boolean isZeroMAC(byte[] mac); static boolean isBroadcastMACAddr(byte[] macAddress); static boolean isUnicastMACAddr(byte[] macAddress); static boolean isMulticastMACAddr(byte[] macAddress); static boolean isAny(InetAddress ip); static boolean fieldsConflict(int field1, int field2); static InetAddress parseInetAddress(String addressString); static boolean isIPv4AddressValid(String cidr); static boolean isIPv6AddressValid(String cidr); static boolean isIPAddressValid(String cidr); static int getUnsignedByte(byte value); static int getUnsignedShort(short value); static InetAddress gethighestIP(boolean v6); static byte[] getBroadcastMACAddr(); static final int NUM_BITS_IN_A_BYTE; static final int MAC_ADDR_LENGTH_IN_BYTES; static final int MAC_ADDR_LENGTH_IN_WORDS; }
@Test public void testInetAddressConflict() throws UnknownHostException { assertFalse(NetUtils.inetAddressConflict(InetAddress.getByName("9.9.1.1"), InetAddress.getByName("9.9.1.0"), null, InetAddress.getByName("255.255.255.0"))); assertTrue(NetUtils.inetAddressConflict(InetAddress.getByName("9.9.2.1"), InetAddress.getByName("9.9.1.0"), null, InetAddress.getByName("255.255.255.0"))); assertTrue( NetUtils.inetAddressConflict(InetAddress.getByName("9.9.1.1"), InetAddress.getByName("9.9.1.0"), InetAddress.getByName("255.255.0.0"), InetAddress.getByName("255.255.255.0"))); assertFalse( NetUtils.inetAddressConflict(InetAddress.getByName("9.9.1.0"), InetAddress.getByName("9.9.0.0"), InetAddress.getByName("255.255.255.0"), InetAddress.getByName("255.255.0.0"))); assertTrue( NetUtils.inetAddressConflict(InetAddress.getByName("9.8.1.0"), InetAddress.getByName("9.9.0.0"), InetAddress.getByName("255.255.255.0"), InetAddress.getByName("255.255.0.0"))); }