IPv6 validation

Tony picture Tony · May 11, 2011 · Viewed 18.2k times · Source

I used IPAddressUtil.isIPv6LiteralAddress (ipAddress) method to validate IPv6, but this method fails for ipv6-address/prefix-length format (format is mentioned in RFC 4291 section 2.3) of IPV6.

Could anyone know any validators which validate " ipv6-address/prefix-length " format?

Legal representations of IPV6

  1. ABCD:EF01:2345:6789:ABCD:EF01:2345:6789
  2. 2001:DB8:0:0:8:800:200C:417A
  3. FF01:0:0:0:0:0:0:101
  4. 0:0:0:0:0:0:0:1
  5. 0:0:0:0:0:0:0:0
  6. 2001:DB8::8:800:200C:417A
  7. FF01::101
  8. ::1
  9. ::
  10. 0:0:0:0:0:0:13.1.68.3
  11. 0:0:0:0:0:FFFF:129.144.52.38
  12. ::13.1.68.3
  13. FFFF:129.144.52.38
  14. 2001:0DB8:0000:CD30:0000:0000:0000:0000/60
  15. 2001:0DB8::CD30:0:0:0:0/60
  16. 2001:0DB8:0:CD30::/60

NOT legal representations of IPV6

  1. 2001:0DB8:0:CD3/60
  2. 2001:0DB8::CD30/60
  3. 2001:0DB8::CD3/60

Answer

broc.seib picture broc.seib · Jan 18, 2013

You can use the Guava library, specifically using the com.google.common.net.InetAddresses class, calling isInetAddress().


isInetAddress

public static boolean isInetAddress(String ipString)

Returns true if the supplied string is a valid IP string literal, false otherwise.

Parameters: ipString - String to evaluated as an IP string literal

Returns: true if the argument is a valid IP string literal