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

The following examples show how to use java.net.Inet6Address#getAddress() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Utils.java    From meshenger-android with GNU General Public License v3.0 6 votes vote down vote up
private static Inet6Address createEUI64Address(Inet6Address addr6, byte[] mac) {
    // addr6 is expected to be a EUI64 address
    try {
        byte[] bytes = addr6.getAddress();

        bytes[8] = (byte) (mac[0] ^ 2);
        bytes[9] = mac[1];
        bytes[10] = mac[2];

        // already set, but doesn't harm
        bytes[11] = (byte) 0xFF;
        bytes[12] = (byte) 0xFE;

        bytes[13] = mac[3];
        bytes[14] = mac[4];
        bytes[15] = mac[5];

        return Inet6Address.getByAddress(null, bytes, addr6.getScopeId());
    } catch (UnknownHostException e) {
        return null;
    }
}
 
Example 2
Source File: Inet6AddressSerializationTest.java    From jdk8u_jdk with GNU General Public License v2.0 6 votes vote down vote up
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 3
Source File: InetAddresses.java    From codebuff with BSD 2-Clause "Simplified" License 6 votes vote down vote up
/**
 * 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) {
  Preconditions.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 4
Source File: InetAddresses.java    From codebuff with BSD 2-Clause "Simplified" License 6 votes vote down vote up
/**
 * 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 5
Source File: Inet6AddressSerializationTest.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
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 6
Source File: Inet6AddressSerializationTest.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
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 7
Source File: Inet6AddressSerializationTest.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
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 8
Source File: Inet6AddressSerializationTest.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
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 9
Source File: Inet6AddressSerializationTest.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
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 10
Source File: Inet6AddressSerializationTest.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
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 11
Source File: InetAddresses.java    From codebuff with BSD 2-Clause "Simplified" License 6 votes vote down vote up
/**
 * 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 12
Source File: DnsQueryContextManager.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
private static Inet4Address toIPv4Address(Inet6Address a6) {
    byte[] b6 = a6.getAddress();
    byte[] b4 = { b6[12], b6[13], b6[14], b6[15] };
    try {
        return (Inet4Address) InetAddress.getByAddress(b4);
    } catch (UnknownHostException e) {
        throw new Error(e);
    }
}
 
Example 13
Source File: IPv6Address.java    From galimatias with MIT License 5 votes vote down vote up
/**
 * Convert from @{java.net.Inet6Address}.
 *
 * @param inet6Address The IPv6 address as a @{java.net.Inet6Address}.
 * @return The IPv6 address as a @{IPv6Address}.
 */
public static IPv6Address fromInet6Address(final Inet6Address inet6Address) {
    final byte[] bytes = inet6Address.getAddress();
    final short[] pieces = new short[8];
    for (int i = 0; i < pieces.length; i++) {
        pieces[i] = (short) (((bytes[i*2] & 0xFF) << 8) | (bytes[i*2+1] & 0x00FF));
    }
    return new IPv6Address(pieces);
}
 
Example 14
Source File: Utils.java    From meshenger-android with GNU General Public License v3.0 5 votes vote down vote up
public static byte[] getEUI64MAC(Inet6Address addr6) {
    byte[] bytes = addr6.getAddress();
    if (bytes[11] != ((byte) 0xFF) || bytes[12] != ((byte) 0xFE)) {
        return null;
    }

    byte[] mac = new byte[6];
    mac[0] = (byte) (bytes[8] ^ 2);
    mac[1] = bytes[9];
    mac[2] = bytes[10];
    mac[3] = bytes[13];
    mac[4] = bytes[14];
    mac[5] = bytes[15];
    return mac;
}
 
Example 15
Source File: NetworkSpace.java    From EasyVPN-Free with GNU General Public License v3.0 5 votes vote down vote up
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 16
Source File: TestInetAddressServlet.java    From appengine-java-vm-runtime with Apache License 2.0 4 votes vote down vote up
/**
 * Test the properties of an instance of IPV6 InetAddress. Much of the code in this method
 * is the same as the code in the method testNameAndAddress4() but it will test different
 * code paths through our byte-code-rewriting since this method uses an instance of
 * Inet6Address.
 * @param hostName The expected host name
 * @param addressString The expected address string
 * @param addressBytes The expected address bytes
 * @param scopeID the expected scopeID
 * @param inet6Addr The instance of Inet6Address being tested
 * @param response HttpServletResponse used to return failure message
 * @throws IOException If method being tested throws this.
 * @throws AssertionFailedException If an assertion fails
 */
private static void testNameAndAddress6(
    String hostName,
    String addressString,
    byte[] addressBytes,
    int scopeID,
    Inet6Address inet6Addr,
    HttpServletResponse response)
    throws IOException, AssertionFailedException {

  assertNotNull("inet6Addr", inet6Addr, response);

  //getAddress
  byte[] bytes = inet6Addr.getAddress();
  assertNotNull("inet6Addr bytes", bytes, response);
  assertEquals("inet6Addr bytes.length", 16, bytes.length, response);
  for (int i = 0; i < 16; i++) {
    assertEquals("inet6Addr address byte " + i, addressBytes[i], bytes[i], response);
  }

  //getCanonicalHostName should return addressString because user code
  //doesn't have permission to get an actual host name
  String canonicalName = inet6Addr.getCanonicalHostName();
  assertEquals("getCanonicalHostName", addressString, canonicalName.toUpperCase(), response);

  //getHostAddress
  String address = inet6Addr.getHostAddress();
  assertEquals("getHostAddress", addressString, address.toUpperCase(), response);

  //getHostName.
  String name2 = inet6Addr.getHostName().toUpperCase();
  String expectedName = (hostName == null ? addressString : hostName.toUpperCase());
  assertEquals("getHostName", expectedName, name2, response);

  //Misc Properties
  assertFalse("isAnyLocalAddress", inet6Addr.isAnyLocalAddress(), response);
  assertFalse("isLinkLocalAddress", inet6Addr.isLinkLocalAddress(), response);
  assertFalse("isLoopbackAddress", inet6Addr.isLoopbackAddress(), response);
  assertFalse("isMCGlobal", inet6Addr.isMCGlobal(), response);
  assertFalse("isMCLinkLoca", inet6Addr.isMCLinkLocal(), response);
  assertFalse("isMCNodeLocal", inet6Addr.isMCNodeLocal(), response);
  assertFalse("isMCOrgLocal", inet6Addr.isMCOrgLocal(), response);
  assertFalse("isMCSiteLoca", inet6Addr.isMCSiteLocal(), response);
  assertFalse("isMulticastAddress", inet6Addr.isMulticastAddress(), response);
  assertFalse("isSiteLocalAddress", inet6Addr.isSiteLocalAddress(), response);

  //toString
  String s = inet6Addr.toString();
  String prefix = (hostName == null ? addressString : hostName);
  assertEquals(
      "toString", (prefix + "/" + addressString).toUpperCase(), s.toUpperCase(), response);

  //isReachable
  assertFalse("isReachable", inet6Addr.isReachable(1000), response);

  //getScopedInterface
  assertNull("getScopedInterface()", inet6Addr.getScopedInterface(), response);

  //getScopedID
  assertEquals("getScopedID()", scopeID, inet6Addr.getScopeId(), response);

  //getHashCode
  assertFalse("hashCode is 0", 0 == inet6Addr.hashCode(), response);

  //isIPv4Compatible
  assertFalse("is Ipv4Compatible()", inet6Addr.isIPv4CompatibleAddress(), response);
}
 
Example 17
Source File: InetAddresses.java    From codebuff with BSD 2-Clause "Simplified" License 3 votes vote down vote up
/**
   * 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 18
Source File: InetAddresses.java    From codebuff with BSD 2-Clause "Simplified" License 3 votes vote down vote up
/**
 * 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 19
Source File: IPv6Address.java    From IPAddress with Apache License 2.0 2 votes vote down vote up
/**
 * Constructs an IPv6 address.
 *
 * @param inet6Address the java.net address object
 */
public IPv6Address(Inet6Address inet6Address) {
	this(inet6Address.getAddress(), getZone(inet6Address));
}
 
Example 20
Source File: InetAddresses.java    From codebuff with BSD 2-Clause "Simplified" License 1 votes vote down vote up
/**
 * 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);
}