@Test public void testMasksV4() throws UnknownHostException { InetAddress mask = InetAddress.getByName("128.0.0.0"); assertTrue(mask.equals(NetUtils.getInetNetworkMask(1, false))); mask = InetAddress.getByName("192.0.0.0"); assertTrue(mask.equals(NetUtils.getInetNetworkMask(2, false))); mask = InetAddress.getByName("224.0.0.0"); assertTrue(mask.equals(NetUtils.getInetNetworkMask(3, false))); mask = InetAddress.getByName("240.0.0.0"); assertTrue(mask.equals(NetUtils.getInetNetworkMask(4, false))); mask = InetAddress.getByName("248.0.0.0"); assertTrue(mask.equals(NetUtils.getInetNetworkMask(5, false))); mask = InetAddress.getByName("252.0.0.0"); assertTrue(mask.equals(NetUtils.getInetNetworkMask(6, false))); mask = InetAddress.getByName("254.0.0.0"); assertTrue(mask.equals(NetUtils.getInetNetworkMask(7, false))); mask = InetAddress.getByName("255.0.0.0"); assertTrue(mask.equals(NetUtils.getInetNetworkMask(8, false))); mask = InetAddress.getByName("255.128.0.0"); assertTrue(mask.equals(NetUtils.getInetNetworkMask(9, false))); mask = InetAddress.getByName("255.192.0.0"); assertTrue(mask.equals(NetUtils.getInetNetworkMask(10, false))); mask = InetAddress.getByName("255.224.0.0"); assertTrue(mask.equals(NetUtils.getInetNetworkMask(11, false))); mask = InetAddress.getByName("255.240.0.0"); assertTrue(mask.equals(NetUtils.getInetNetworkMask(12, false))); mask = InetAddress.getByName("255.248.0.0"); assertTrue(mask.equals(NetUtils.getInetNetworkMask(13, false))); mask = InetAddress.getByName("255.252.0.0"); assertTrue(mask.equals(NetUtils.getInetNetworkMask(14, false))); mask = InetAddress.getByName("255.254.0.0"); assertTrue(mask.equals(NetUtils.getInetNetworkMask(15, false))); mask = InetAddress.getByName("255.255.0.0"); assertTrue(mask.equals(NetUtils.getInetNetworkMask(16, false))); mask = InetAddress.getByName("255.255.128.0"); assertTrue(mask.equals(NetUtils.getInetNetworkMask(17, false))); mask = InetAddress.getByName("255.255.192.0"); assertTrue(mask.equals(NetUtils.getInetNetworkMask(18, false))); mask = InetAddress.getByName("255.255.224.0"); assertTrue(mask.equals(NetUtils.getInetNetworkMask(19, false))); mask = InetAddress.getByName("255.255.240.0"); assertTrue(mask.equals(NetUtils.getInetNetworkMask(20, false))); mask = InetAddress.getByName("255.255.248.0"); assertTrue(mask.equals(NetUtils.getInetNetworkMask(21, false))); mask = InetAddress.getByName("255.255.252.0"); assertTrue(mask.equals(NetUtils.getInetNetworkMask(22, false))); mask = InetAddress.getByName("255.255.254.0"); assertTrue(mask.equals(NetUtils.getInetNetworkMask(23, false))); mask = InetAddress.getByName("255.255.255.0"); assertTrue(mask.equals(NetUtils.getInetNetworkMask(24, false))); mask = InetAddress.getByName("255.255.255.128"); assertTrue(mask.equals(NetUtils.getInetNetworkMask(25, false))); mask = InetAddress.getByName("255.255.255.192"); assertTrue(mask.equals(NetUtils.getInetNetworkMask(26, false))); mask = InetAddress.getByName("255.255.255.224"); assertTrue(mask.equals(NetUtils.getInetNetworkMask(27, false))); mask = InetAddress.getByName("255.255.255.240"); assertTrue(mask.equals(NetUtils.getInetNetworkMask(28, false))); mask = InetAddress.getByName("255.255.255.248"); assertTrue(mask.equals(NetUtils.getInetNetworkMask(29, false))); mask = InetAddress.getByName("255.255.255.252"); assertTrue(mask.equals(NetUtils.getInetNetworkMask(30, false))); mask = InetAddress.getByName("255.255.255.254"); assertTrue(mask.equals(NetUtils.getInetNetworkMask(31, false))); mask = InetAddress.getByName("255.255.255.255"); assertTrue(mask.equals(NetUtils.getInetNetworkMask(32, false))); }
public static InetAddress getInetNetworkMask(int prefixMaskLength, boolean isV6) { if (prefixMaskLength < 0 || !isV6 && prefixMaskLength > 32 || isV6 && prefixMaskLength > 128) { return null; } byte[] v4Address = { 0, 0, 0, 0 }; byte[] v6Address = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; byte[] address = isV6 ? v6Address : v4Address; int numBytes = prefixMaskLength / 8; int numBits = prefixMaskLength % 8; int index = 0; for (; index < numBytes; index++) { address[index] = (byte) 0xff; } if (numBits > 0) { int rem = 0; for (int j = 0; j < numBits; j++) { rem |= 1 << 7 - j; } address[index] = (byte) rem; } try { return InetAddress.getByAddress(address); } catch (UnknownHostException e) { LOG.error("", e); } return null; }
NetUtils { public static InetAddress getInetNetworkMask(int prefixMaskLength, boolean isV6) { if (prefixMaskLength < 0 || !isV6 && prefixMaskLength > 32 || isV6 && prefixMaskLength > 128) { return null; } byte[] v4Address = { 0, 0, 0, 0 }; byte[] v6Address = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; byte[] address = isV6 ? v6Address : v4Address; int numBytes = prefixMaskLength / 8; int numBits = prefixMaskLength % 8; int index = 0; for (; index < numBytes; index++) { address[index] = (byte) 0xff; } if (numBits > 0) { int rem = 0; for (int j = 0; j < numBits; j++) { rem |= 1 << 7 - j; } address[index] = (byte) rem; } try { return InetAddress.getByAddress(address); } catch (UnknownHostException e) { LOG.error("", e); } return null; } }
NetUtils { public static InetAddress getInetNetworkMask(int prefixMaskLength, boolean isV6) { if (prefixMaskLength < 0 || !isV6 && prefixMaskLength > 32 || isV6 && prefixMaskLength > 128) { return null; } byte[] v4Address = { 0, 0, 0, 0 }; byte[] v6Address = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; byte[] address = isV6 ? v6Address : v4Address; int numBytes = prefixMaskLength / 8; int numBits = prefixMaskLength % 8; int index = 0; for (; index < numBytes; index++) { address[index] = (byte) 0xff; } if (numBits > 0) { int rem = 0; for (int j = 0; j < numBits; j++) { rem |= 1 << 7 - j; } address[index] = (byte) rem; } try { return InetAddress.getByAddress(address); } catch (UnknownHostException e) { LOG.error("", e); } return null; } private  NetUtils(); }
NetUtils { public static InetAddress getInetNetworkMask(int prefixMaskLength, boolean isV6) { if (prefixMaskLength < 0 || !isV6 && prefixMaskLength > 32 || isV6 && prefixMaskLength > 128) { return null; } byte[] v4Address = { 0, 0, 0, 0 }; byte[] v6Address = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; byte[] address = isV6 ? v6Address : v4Address; int numBytes = prefixMaskLength / 8; int numBits = prefixMaskLength % 8; int index = 0; for (; index < numBytes; index++) { address[index] = (byte) 0xff; } if (numBits > 0) { int rem = 0; for (int j = 0; j < numBits; j++) { rem |= 1 << 7 - j; } address[index] = (byte) rem; } try { return InetAddress.getByAddress(address); } catch (UnknownHostException e) { LOG.error("", e); } return null; } 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(); }
NetUtils { public static InetAddress getInetNetworkMask(int prefixMaskLength, boolean isV6) { if (prefixMaskLength < 0 || !isV6 && prefixMaskLength > 32 || isV6 && prefixMaskLength > 128) { return null; } byte[] v4Address = { 0, 0, 0, 0 }; byte[] v6Address = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; byte[] address = isV6 ? v6Address : v4Address; int numBytes = prefixMaskLength / 8; int numBits = prefixMaskLength % 8; int index = 0; for (; index < numBytes; index++) { address[index] = (byte) 0xff; } if (numBits > 0) { int rem = 0; for (int j = 0; j < numBits; j++) { rem |= 1 << 7 - j; } address[index] = (byte) rem; } try { return InetAddress.getByAddress(address); } catch (UnknownHostException e) { LOG.error("", e); } return null; } 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; }