Menu

#50 IllegalArgumentException("invalid IPv6 address") thown while creating AAAArecord object with IPv6 Addresses With Embedded IPv4 Addresses

None
closed-fixed
None
5
2015-02-15
2015-02-12
keshav
No

While creating AAAARecord object with IPv6 address like - "0:0:0:0:0:FFFF:C633:6402", dns API is throwing IllegalArgumentException("invalid IPv6 address").

This address seems to be “Special IPv6 address” according to Java docs of Inet6Address class http://docs.oracle.com/javase/7/docs/api/java/net/Inet6Address.html

In AAAARecord.java -

/
Creates an AAAA Record from the given data
@param address The address suffix
*/
public
AAAARecord(Name name, int dclass, long ttl, InetAddress address) {
super(name, Type.AAAA, dclass, ttl);
if (Address.familyOf(address) != Address.IPv6)
throw new IllegalArgumentException("invalid IPv6 address");
this.address = address;
}

and in Address.java -

/
Returns the family of an InetAddress.
@param address The supplied address.
@return The family, either IPv4 or IPv6.
/
public static int
familyOf(InetAddress address) {
if (address instanceof Inet4Address)
return IPv4;
if (address instanceof Inet6Address)
return IPv6;
throw new IllegalArgumentException("unknown address family");
}

Discussion

  • Brian Wellington

    I believe dnsjava is doing the right thing. If you call the AAAARecord constructor with what appears to be an IPv4 address, it's perfectly reasonable to reject it. If there were some way to detect that it was a "special" address, it would make sense to accept it, but there doesn't appear to be one that works.

    There is Inet6Address.isIPv4CompatibleAddress(), but it doesn't work.

    With this code:

    String str = "0:0:0:0:0:FFFF:C633:6402";
    byte [] array = Address.toByteArray(str, Address.IPv6);
    InetAddress addr1 = InetAddress.getByName(str);
    InetAddress addr2 = InetAddress.getByAddress("foo", array);
    InetAddress addr3 = Inet6Address.getByAddress("foo", array, 0);

    addr1 and addr2 are not Inet6Address objects. addr3 is, but addr3.isIPv4CompatibleAddress() returns false.

    Accepting an IPv4 address in the AAAARecord constructor and treating it as a "special" address be what you want, but it's far more likely to be an application bug.

    There is one bug that I do see, though. It is possible to create a AAAARecord passing a "special" address as a String (using the Record.fromString factory method), but when printing it out, it's printed as an IPv4 address, not an encoded IPv6 address.

     
  • Brian Wellington

    • status: open --> closed-fixed
    • Group: -->
     
  • Brian Wellington

    The bug mentioned above has now been fixed, but there's nothing that can be done about the correct rejection of invalid addresses.