Java Code Examples for java.net.Inet6Address.getAddress()

The following are Jave code examples for showing how to use getAddress() of the java.net.Inet6Address class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
Example 1
Project: guava-mock   File: InetAddresses.java   Source Code and License Vote up 6 votes
/**
 * Returns the Teredo information embedded in a Teredo address.
 *
 * @param ip {@link Inet6Address} to be examined for embedded Teredo information
 * @return extracted {@code TeredoInfo}
 * @throws IllegalArgumentException if the argument is not a valid IPv6 Teredo address
 */
public static TeredoInfo getTeredoInfo(Inet6Address ip) {
  checkArgument(isTeredoAddress(ip), "Address '%s' is not a Teredo address.", toAddrString(ip));

  byte[] bytes = ip.getAddress();
  Inet4Address server = getInet4Address(Arrays.copyOfRange(bytes, 4, 8));

  int flags = ByteStreams.newDataInput(bytes, 8).readShort() & 0xffff;

  // Teredo obfuscates the mapped client port, per section 4 of the RFC.
  int port = ~ByteStreams.newDataInput(bytes, 10).readShort() & 0xffff;

  byte[] clientBytes = Arrays.copyOfRange(bytes, 12, 16);
  for (int i = 0; i < clientBytes.length; i++) {
    // Teredo obfuscates the mapped client IP, per section 4 of the RFC.
    clientBytes[i] = (byte) ~clientBytes[i];
  }
  Inet4Address client = getInet4Address(clientBytes);

  return new TeredoInfo(server, client, port, flags);
}
 
Example 2
Project: guava-mock   File: InetAddresses.java   Source Code and License Vote up 6 votes
/**
 * Evaluates whether the argument is an ISATAP address.
 *
 * <p>From RFC 5214: "ISATAP interface identifiers are constructed in Modified EUI-64 format [...]
 * by concatenating the 24-bit IANA OUI (00-00-5E), the 8-bit hexadecimal value 0xFE, and a 32-bit
 * IPv4 address in network byte order [...]"
 *
 * <p>For more on ISATAP addresses see section 6.1 of
 * <a target="_parent" href="http://tools.ietf.org/html/rfc5214#section-6.1">RFC 5214</a>.
 *
 * @param ip {@link Inet6Address} to be examined for ISATAP address format
 * @return {@code true} if the argument is an ISATAP address
 */
public static boolean isIsatapAddress(Inet6Address ip) {

  // If it's a Teredo address with the right port (41217, or 0xa101)
  // which would be encoded as 0x5efe then it can't be an ISATAP address.
  if (isTeredoAddress(ip)) {
    return false;
  }

  byte[] bytes = ip.getAddress();

  if ((bytes[8] | (byte) 0x03) != (byte) 0x03) {

    // Verify that high byte of the 64 bit identifier is zero, modulo
    // the U/L and G bits, with which we are not concerned.
    return false;
  }

  return (bytes[9] == (byte) 0x00) && (bytes[10] == (byte) 0x5e) && (bytes[11] == (byte) 0xfe);
}
 
Example 3
Project: jdk8u-jdk   File: Inet6AddressSerializationTest.java   Source Code and License Vote up 6 votes
static void displayExpectedInet6Address(Inet6Address expectedInet6Address) {

        String expectedHostName = expectedInet6Address.getHostName();
        byte[] expectedAddress = expectedInet6Address.getAddress();
        String expectedHostAddress = expectedInet6Address.getHostAddress();
        int expectedScopeId = expectedInet6Address.getScopeId();
        NetworkInterface expectedNetIf = expectedInet6Address
                .getScopedInterface();

        System.err.println("Excpected HostName: " + expectedHostName);
        System.err.println("Expected Address: "
                + Arrays.toString(expectedAddress));
        System.err.println("Expected HostAddress: " + expectedHostAddress);
        System.err.println("Expected Scope Id " + expectedScopeId);
        System.err.println("Expected NetworkInterface " + expectedNetIf);
        System.err.println("Expected Inet6Address " + expectedInet6Address);
    }
 
Example 4
Project: openjdk-jdk10   File: Inet6AddressSerializationTest.java   Source Code and License Vote up 6 votes
static void displayExpectedInet6Address(Inet6Address expectedInet6Address) {

        String expectedHostName = expectedInet6Address.getHostName();
        byte[] expectedAddress = expectedInet6Address.getAddress();
        String expectedHostAddress = expectedInet6Address.getHostAddress();
        int expectedScopeId = expectedInet6Address.getScopeId();
        NetworkInterface expectedNetIf = expectedInet6Address
                .getScopedInterface();

        System.err.println("Excpected HostName: " + expectedHostName);
        System.err.println("Expected Address: "
                + Arrays.toString(expectedAddress));
        System.err.println("Expected HostAddress: " + expectedHostAddress);
        System.err.println("Expected Scope Id " + expectedScopeId);
        System.err.println("Expected NetworkInterface " + expectedNetIf);
        System.err.println("Expected Inet6Address " + expectedInet6Address);
    }
 
Example 5
Project: googles-monorepo-demo   File: InetAddresses.java   Source Code and License Vote up 6 votes
/**
 * Returns the Teredo information embedded in a Teredo address.
 *
 * @param ip {@link Inet6Address} to be examined for embedded Teredo information
 * @return extracted {@code TeredoInfo}
 * @throws IllegalArgumentException if the argument is not a valid IPv6 Teredo address
 */
public static TeredoInfo getTeredoInfo(Inet6Address ip) {
  checkArgument(isTeredoAddress(ip), "Address '%s' is not a Teredo address.", toAddrString(ip));

  byte[] bytes = ip.getAddress();
  Inet4Address server = getInet4Address(Arrays.copyOfRange(bytes, 4, 8));

  int flags = ByteStreams.newDataInput(bytes, 8).readShort() & 0xffff;

  // Teredo obfuscates the mapped client port, per section 4 of the RFC.
  int port = ~ByteStreams.newDataInput(bytes, 10).readShort() & 0xffff;

  byte[] clientBytes = Arrays.copyOfRange(bytes, 12, 16);
  for (int i = 0; i < clientBytes.length; i++) {
    // Teredo obfuscates the mapped client IP, per section 4 of the RFC.
    clientBytes[i] = (byte) ~clientBytes[i];
  }
  Inet4Address client = getInet4Address(clientBytes);

  return new TeredoInfo(server, client, port, flags);
}
 
Example 6
Project: googles-monorepo-demo   File: InetAddresses.java   Source Code and License Vote up 6 votes
/**
 * Evaluates whether the argument is an ISATAP address.
 *
 * <p>From RFC 5214: "ISATAP interface identifiers are constructed in Modified EUI-64 format [...]
 * by concatenating the 24-bit IANA OUI (00-00-5E), the 8-bit hexadecimal value 0xFE, and a 32-bit
 * IPv4 address in network byte order [...]"
 *
 * <p>For more on ISATAP addresses see section 6.1 of
 * <a target="_parent" href="http://tools.ietf.org/html/rfc5214#section-6.1">RFC 5214</a>.
 *
 * @param ip {@link Inet6Address} to be examined for ISATAP address format
 * @return {@code true} if the argument is an ISATAP address
 */
public static boolean isIsatapAddress(Inet6Address ip) {

  // If it's a Teredo address with the right port (41217, or 0xa101)
  // which would be encoded as 0x5efe then it can't be an ISATAP address.
  if (isTeredoAddress(ip)) {
    return false;
  }

  byte[] bytes = ip.getAddress();

  if ((bytes[8] | (byte) 0x03) != (byte) 0x03) {

    // Verify that high byte of the 64 bit identifier is zero, modulo
    // the U/L and G bits, with which we are not concerned.
    return false;
  }

  return (bytes[9] == (byte) 0x00) && (bytes[10] == (byte) 0x5e) && (bytes[11] == (byte) 0xfe);
}
 
Example 7
Project: Cybernet-VPN   File: NetworkSpace.java   Source Code and License Vote up 5 votes
public ipAddress(Inet6Address address, int mask, boolean include) {
    networkMask = mask;
    included = include;
    int s = 128;
    netAddress = BigInteger.ZERO;
    for (byte b : address.getAddress()) {
        s -= 8;
        netAddress = netAddress.add(BigInteger.valueOf((b & 0xFF)).shiftLeft(s));
    }
}
 
Example 8
Project: mac-address-detector-java   File: MacAddressHelper.java   Source Code and License Vote up 5 votes
/**
 * The broadcast ip for a icmp v6 request is replaced all bytes except the last three bytes with _IPv6_BROADCAST_IPADDRESS_PREFIX
 *
 * @param inet6Address
 * @return
 */
private Inet6Address _getBroadcastIPAddress4IPv6(Inet6Address inet6Address) throws UnknownHostException {
    //"fe80:0:0:0:250:56ff:febc:2688" -> "FF02::1:FFbc:2688"
    byte[] ipInBytes = inet6Address.getAddress();
    byte[] broadcastIpAddress = new byte[ipInBytes.length];
    System.arraycopy(_IPv6_BROADCAST_IPADDRESS_PREFIX, 0, broadcastIpAddress, 0, _IPv6_BROADCAST_IPADDRESS_PREFIX.length);
    int reservedBytes = 3;
    System.arraycopy(ipInBytes, ipInBytes.length - reservedBytes, broadcastIpAddress, _IPv6_BROADCAST_IPADDRESS_PREFIX.length -
            reservedBytes, reservedBytes);
    return (Inet6Address) Inet6Address.getByAddress(broadcastIpAddress);
}
 
Example 9
Project: mac-address-detector-java   File: MacAddressHelper.java   Source Code and License Vote up 5 votes
/**
 * The broadcast mac address for a icmpv6 request is replaced all bytes with 33:33:ff except the last thress bytes
 * fe80::250:56ff:fe95:f8d -> 33:33:ff:95:0f:8d
 *
 * @param inet6Address
 * @return
 */
private MacAddress _getBroadcastMacAddress4IPv6(Inet6Address inet6Address) {
    byte[] ipInBytes = inet6Address.getAddress();
    byte[] broadcastMacAddress = new byte[_IPv6_BROADCAST_MACADDRESS_PREFIX.length];
    System.arraycopy(_IPv6_BROADCAST_MACADDRESS_PREFIX, 0, broadcastMacAddress, 0, _IPv6_BROADCAST_MACADDRESS_PREFIX.length);
    int reservedBytes = 3;
    System.arraycopy(ipInBytes, ipInBytes.length - reservedBytes, broadcastMacAddress, _IPv6_BROADCAST_MACADDRESS_PREFIX.length -
            reservedBytes, reservedBytes);
    return MacAddress.getByAddress(broadcastMacAddress);
}
 
Example 10
Project: guava-mock   File: InetAddresses.java   Source Code and License Vote up 4 votes
/**
 * Evaluates whether the argument is an IPv6 "compat" address.
 *
 * <p>An "IPv4 compatible", or "compat", address is one with 96 leading bits of zero, with the
 * remaining 32 bits interpreted as an IPv4 address. These are conventionally represented in
 * string literals as {@code "::192.168.0.1"}, though {@code "::c0a8:1"} is also considered an
 * IPv4 compatible address (and equivalent to {@code "::192.168.0.1"}).
 *
 * <p>For more on IPv4 compatible addresses see section 2.5.5.1 of
 * <a target="_parent" href="http://tools.ietf.org/html/rfc4291#section-2.5.5.1">RFC 4291</a>.
 *
 * <p>NOTE: This method is different from {@link Inet6Address#isIPv4CompatibleAddress} in that it
 * more correctly classifies {@code "::"} and {@code "::1"} as proper IPv6 addresses (which they
 * are), NOT IPv4 compatible addresses (which they are generally NOT considered to be).
 *
 * @param ip {@link Inet6Address} to be examined for embedded IPv4 compatible address format
 * @return {@code true} if the argument is a valid "compat" address
 */
public static boolean isCompatIPv4Address(Inet6Address ip) {
  if (!ip.isIPv4CompatibleAddress()) {
    return false;
  }

  byte[] bytes = ip.getAddress();
  if ((bytes[12] == 0)
      && (bytes[13] == 0)
      && (bytes[14] == 0)
      && ((bytes[15] == 0) || (bytes[15] == 1))) {
    return false;
  }

  return true;
}
 
Example 11
Project: googles-monorepo-demo   File: InetAddresses.java   Source Code and License Vote up 4 votes
/**
 * Evaluates whether the argument is an IPv6 "compat" address.
 *
 * <p>An "IPv4 compatible", or "compat", address is one with 96 leading bits of zero, with the
 * remaining 32 bits interpreted as an IPv4 address. These are conventionally represented in
 * string literals as {@code "::192.168.0.1"}, though {@code "::c0a8:1"} is also considered an
 * IPv4 compatible address (and equivalent to {@code "::192.168.0.1"}).
 *
 * <p>For more on IPv4 compatible addresses see section 2.5.5.1 of
 * <a target="_parent" href="http://tools.ietf.org/html/rfc4291#section-2.5.5.1">RFC 4291</a>.
 *
 * <p>NOTE: This method is different from {@link Inet6Address#isIPv4CompatibleAddress} in that it
 * more correctly classifies {@code "::"} and {@code "::1"} as proper IPv6 addresses (which they
 * are), NOT IPv4 compatible addresses (which they are generally NOT considered to be).
 *
 * @param ip {@link Inet6Address} to be examined for embedded IPv4 compatible address format
 * @return {@code true} if the argument is a valid "compat" address
 */
public static boolean isCompatIPv4Address(Inet6Address ip) {
  if (!ip.isIPv4CompatibleAddress()) {
    return false;
  }

  byte[] bytes = ip.getAddress();
  if ((bytes[12] == 0)
      && (bytes[13] == 0)
      && (bytes[14] == 0)
      && ((bytes[15] == 0) || (bytes[15] == 1))) {
    return false;
  }

  return true;
}
 
Example 12
Project: guava-mock   File: InetAddresses.java   Source Code and License Vote up 3 votes
/**
 * Evaluates whether the argument is a Teredo address.
 *
 * <p>Teredo addresses begin with the {@code "2001::/32"} prefix.
 *
 * @param ip {@link Inet6Address} to be examined for Teredo address format
 * @return {@code true} if the argument is a Teredo address
 */
public static boolean isTeredoAddress(Inet6Address ip) {
  byte[] bytes = ip.getAddress();
  return (bytes[0] == (byte) 0x20)
      && (bytes[1] == (byte) 0x01)
      && (bytes[2] == 0)
      && (bytes[3] == 0);
}
 
Example 13
Project: googles-monorepo-demo   File: InetAddresses.java   Source Code and License Vote up 3 votes
/**
 * Evaluates whether the argument is a Teredo address.
 *
 * <p>Teredo addresses begin with the {@code "2001::/32"} prefix.
 *
 * @param ip {@link Inet6Address} to be examined for Teredo address format
 * @return {@code true} if the argument is a Teredo address
 */
public static boolean isTeredoAddress(Inet6Address ip) {
  byte[] bytes = ip.getAddress();
  return (bytes[0] == (byte) 0x20)
      && (bytes[1] == (byte) 0x01)
      && (bytes[2] == 0)
      && (bytes[3] == 0);
}
 
Example 14
Project: guava-mock   File: InetAddresses.java   Source Code and License Vote up 2 votes
/**
 * Evaluates whether the argument is a 6to4 address.
 *
 * <p>6to4 addresses begin with the {@code "2002::/16"} prefix. The next 32 bits are the IPv4
 * address of the host to which IPv6-in-IPv4 tunneled packets should be routed.
 *
 * <p>For more on 6to4 addresses see section 2 of
 * <a target="_parent" href="http://tools.ietf.org/html/rfc3056#section-2">RFC 3056</a>.
 *
 * @param ip {@link Inet6Address} to be examined for 6to4 address format
 * @return {@code true} if the argument is a 6to4 address
 */
public static boolean is6to4Address(Inet6Address ip) {
  byte[] bytes = ip.getAddress();
  return (bytes[0] == (byte) 0x20) && (bytes[1] == (byte) 0x02);
}
 
Example 15
Project: monarch   File: InternalDistributedSystemJUnitTest.java   Source Code and License Vote up 2 votes
/**
 * Detect LinkLocal IPv6 address where the interface is missing, ie %[0-9].
 * 
 * @see InetAddress#isLinkLocalAddress()
 */
private static boolean isIPv6LinkLocalAddress(Inet6Address addr) {
  byte[] addrBytes = addr.getAddress();
  return ((addrBytes[0] == (byte) 0xfe) && (addrBytes[1] == (byte) 0x80));
}
 
Example 16
Project: googles-monorepo-demo   File: InetAddresses.java   Source Code and License Vote up 2 votes
/**
 * Evaluates whether the argument is a 6to4 address.
 *
 * <p>6to4 addresses begin with the {@code "2002::/16"} prefix. The next 32 bits are the IPv4
 * address of the host to which IPv6-in-IPv4 tunneled packets should be routed.
 *
 * <p>For more on 6to4 addresses see section 2 of
 * <a target="_parent" href="http://tools.ietf.org/html/rfc3056#section-2">RFC 3056</a>.
 *
 * @param ip {@link Inet6Address} to be examined for 6to4 address format
 * @return {@code true} if the argument is a 6to4 address
 */
public static boolean is6to4Address(Inet6Address ip) {
  byte[] bytes = ip.getAddress();
  return (bytes[0] == (byte) 0x20) && (bytes[1] == (byte) 0x02);
}