private static int ipToInt(Inet4Address ipAddress) { byte[] octets = ipAddress.getAddress(); assert octets.length == 4; return (octets[0] & 0xff) << 24 | (octets[1] & 0xff) << 16 | (octets[2] & 0xff) << 8 | octets[3] & 0xff; }
/** * Adapted from: https://stackoverflow.com/questions/4209760/validate-an-ip-address-with-mask */ @SuppressWarnings("PointlessBitwiseExpression") private static boolean netMatch(Pair<Inet4Address, Integer> subnet, Inet4Address addr ){ byte[] b = subnet.first.getAddress(); int ipInt = ((b[0] & 0xFF) << 24) | ((b[1] & 0xFF) << 16) | ((b[2] & 0xFF) << 8) | ((b[3] & 0xFF) << 0); byte[] b1 = addr.getAddress(); int ipInt1 = ((b1[0] & 0xFF) << 24) | ((b1[1] & 0xFF) << 16) | ((b1[2] & 0xFF) << 8) | ((b1[3] & 0xFF) << 0); int mask = ~((1 << (32 - subnet.second)) - 1); return (ipInt & mask) == (ipInt1 & mask); }
private static int ipToInt(Inet4Address ipAddress) { byte[] octets = ipAddress.getAddress(); assert octets.length == 4; return (octets[0] & 0xff) << 24 | (octets[1] & 0xff) << 16 | (octets[2] & 0xff) << 8 | octets[3] & 0xff; }
/** * Compute a byte representation of IpV6 from a IpV4 * * @return the byte representation * @throws IllegalArgumentException if the IpV6 cannot be mapped to IpV4 */ public static byte[] getIpV6FromIpV4(Inet4Address address) { byte[] baddr = address.getAddress(); return new byte[] {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, baddr[0], baddr[1], baddr[2], baddr[3]}; } }
private static Inet6Address toCompactAddress(Inet4Address a4) { byte[] b4 = a4.getAddress(); byte[] b6 = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, b4[0], b4[1], b4[2], b4[3] }; try { return (Inet6Address) InetAddress.getByAddress(b6); } catch (UnknownHostException e) { throw new Error(e); } }
String s = "10.1.1.99"; Inet4Address a = (Inet4Address) InetAddress.getByName(s); byte[] b = a.getAddress(); int i = ((b[0] & 0xFF) << 24) | ((b[1] & 0xFF) << 16) | ((b[2] & 0xFF) << 8) | ((b[3] & 0xFF) << 0);
private static Inet6Address toCompactAddress(Inet4Address a4) { byte[] b4 = a4.getAddress(); byte[] b6 = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, b4[0], b4[1], b4[2], b4[3] }; try { return (Inet6Address) InetAddress.getByAddress(b6); } catch (UnknownHostException e) { throw new Error(e); } }
public static int inetAddressToInt(Inet4Address clientIp) { return ByteBuffer.wrap(clientIp.getAddress()).getInt(); }
@Override public void writeObjectData(ByteBuffer buffer, Object object) { Inet4Address i4a = (Inet4Address) object; kryo.writeObject(buffer, i4a.getAddress()); }
Inet4AddressBlock(Inet4Address baseAddress, int maskBits) { this.baseAddress = requireNonNull(baseAddress, "baseAddress"); checkArgument(maskBits >= 0 && maskBits <= 32, "maskBits: %s (expected: 0-32)", maskBits); this.maskBits = maskBits; if (maskBits == 32) { lowerBound = upperBound = ipv4AddressToInt(baseAddress.getAddress()); } else if (maskBits == 0) { lowerBound = upperBound = 0; } else { // Calculate the lower and upper bounds of this address block. // e.g. mask is 256(0x00000100) if maskBits is 24. final int mask = 1 << 32 - maskBits; // e.g. (-mask) is 0xFFFFFF00 if mask is 256. lowerBound = ipv4AddressToInt(baseAddress.getAddress()) & -mask; // e.g. (mask-1) is 0x000000FF if mask is 256. upperBound = lowerBound + mask - 1; } }
/** * Anonymizes IPv4 addresses * <p> * The last octet is set to zero. * <p> * This is just like Google Analytics handles IP anonymization: https://support.google.com/analytics/answer/2763052 * * @param inetAddress the full IPv4 address * @return the anonymized IP address */ public static Inet4Address anonymizeIpV4Address(Inet4Address inetAddress) { try { final byte[] address = inetAddress.getAddress(); anonymizeLastBytes(address, ANONYMIZED_IPV4_OCTETS); return (Inet4Address) InetAddress.getByAddress(address); } catch (UnknownHostException e) { throw new AssertionError(e); } }
/** * Determine if this CIDR address matches the given address. * * @param address the address to test * @return {@code true} if the address matches, {@code false} otherwise */ public boolean matches(Inet4Address address) { Assert.checkNotNullParam("address", address); return networkAddress instanceof Inet4Address && bitsMatch(cachedBytes, address.getAddress(), netmaskBits); }
/** * Returns an integer representing an IPv4 address regardless of whether the supplied argument is * an IPv4 address or not. * * <p>IPv6 addresses are <b>coerced</b> to IPv4 addresses before being converted to integers. * * <p>As long as there are applications that assume that all IP addresses are IPv4 addresses and * can therefore be converted safely to integers (for whatever purpose) this function can be used * to handle IPv6 addresses as well until the application is suitably fixed. * * <p>NOTE: an IPv6 address coerced to an IPv4 address can only be used for such purposes as * rudimentary identification or indexing into a collection of real {@link InetAddress}es. They * cannot be used as real addresses for the purposes of network communication. * * @param ip {@link InetAddress} to convert * @return {@code int}, "coerced" if ip is not an IPv4 address * @since 7.0 */ public static int coerceToInteger(InetAddress ip) { return ByteStreams.newDataInput(getCoercedIPv4Address(ip).getAddress()).readInt(); }
/** * Returns an integer representing an IPv4 address regardless of whether the supplied argument is * an IPv4 address or not. * * <p>IPv6 addresses are <b>coerced</b> to IPv4 addresses before being converted to integers. * * <p>As long as there are applications that assume that all IP addresses are IPv4 addresses and * can therefore be converted safely to integers (for whatever purpose) this function can be used * to handle IPv6 addresses as well until the application is suitably fixed. * * <p>NOTE: an IPv6 address coerced to an IPv4 address can only be used for such purposes as * rudimentary identification or indexing into a collection of real {@link InetAddress}es. They * cannot be used as real addresses for the purposes of network communication. * * @param ip {@link InetAddress} to convert * @return {@code int}, "coerced" if ip is not an IPv4 address * @since 7.0 */ public static int coerceToInteger(InetAddress ip) { return ByteStreams.newDataInput(getCoercedIPv4Address(ip).getAddress()).readInt(); }
/** * Returns an integer representing an IPv4 address regardless of whether the supplied argument is * an IPv4 address or not. * * <p>IPv6 addresses are <b>coerced</b> to IPv4 addresses before being converted to integers. * * <p>As long as there are applications that assume that all IP addresses are IPv4 addresses and * can therefore be converted safely to integers (for whatever purpose) this function can be used * to handle IPv6 addresses as well until the application is suitably fixed. * * <p>NOTE: an IPv6 address coerced to an IPv4 address can only be used for such purposes as * rudimentary identification or indexing into a collection of real {@link InetAddress}es. They * cannot be used as real addresses for the purposes of network communication. * * @param ip {@link InetAddress} to convert * @return {@code int}, "coerced" if ip is not an IPv4 address * @since 7.0 */ public static int coerceToInteger(InetAddress ip) { return ByteStreams.newDataInput(getCoercedIPv4Address(ip).getAddress()).readInt(); }
/** * Returns an integer representing an IPv4 address regardless of whether the supplied argument is * an IPv4 address or not. * * <p>IPv6 addresses are <b>coerced</b> to IPv4 addresses before being converted to integers. * * <p>As long as there are applications that assume that all IP addresses are IPv4 addresses and * can therefore be converted safely to integers (for whatever purpose) this function can be used * to handle IPv6 addresses as well until the application is suitably fixed. * * <p>NOTE: an IPv6 address coerced to an IPv4 address can only be used for such purposes as * rudimentary identification or indexing into a collection of real {@link InetAddress}es. They * cannot be used as real addresses for the purposes of network communication. * * @param ip {@link InetAddress} to convert * @return {@code int}, "coerced" if ip is not an IPv4 address * @since 7.0 */ public static int coerceToInteger(InetAddress ip) { return ByteStreams.newDataInput(getCoercedIPv4Address(ip).getAddress()).readInt(); }
private static int ipToInt(Inet4Address ipAddress) { byte[] octets = ipAddress.getAddress(); assert octets.length == 4; return (octets[0] & 0xff) << 24 | (octets[1] & 0xff) << 16 | (octets[2] & 0xff) << 8 | octets[3] & 0xff; }
@Override public byte[] getRawData() { if (addressPlainText) { return address.getHostAddress().getBytes(); } else { return address.getAddress(); } }
@Override public byte[] getRawData() { byte[] data = new byte[length()]; int cursor = 0; System.arraycopy(address.getAddress(), 0, data, cursor, INET4_ADDRESS_SIZE_IN_BYTES); cursor += INET4_ADDRESS_SIZE_IN_BYTES; System.arraycopy(ByteArrays.toByteArray(protocol.value()), 0, data, cursor, BYTE_SIZE_IN_BYTES); cursor += BYTE_SIZE_IN_BYTES; System.arraycopy(bitMap, 0, data, cursor, bitMap.length); return data; }
@Override public byte[] getRawData() { byte[] rawData = new byte[length()]; rawData[0] = getType().value(); rawData[1] = length; rawData[2] = pointer; int i = 3; for (Inet4Address addr : routeData) { System.arraycopy(addr.getAddress(), 0, rawData, i, INET4_ADDRESS_SIZE_IN_BYTES); i += INET4_ADDRESS_SIZE_IN_BYTES; } return rawData; }