IPv6 验证
我使用 IPAddressUtil.isIPv6LiteralAddress (ipAddress)
方法来验证 IPv6,但此方法对于 ipv6-address/prefix-length 格式失败(格式在 RFC 4291 第 2.3 节) IPV6。
有人知道验证“ ipv6-address/prefix-length ”格式的验证器吗?
IPV6的法律表示
- ABCD:EF01:2345:6789:ABCD:EF01:2345:6789
- 2001:DB8:0:0:8:800:200C:417A
- FF01:0:0:0:0 :0:0:101
- 0:0:0:0:0:0:0:1
- 0:0:0:0:0:0:0:0
- 2001:DB8::8:800:200C:417A
- FF01::101
- ::1
- ::
- 0:0:0:0:0:0:13.1。 68.3
- 0:0:0:0:0:FFFF:129.144.52.38
- ::13.1.68.3
- FFFF:129.144.52.38
- 2001:0DB8:0000:CD30:0000:0000:0000:0000/60
- 2001:0DB8::CD30:0:0:0:0/60
- 2001:0DB8:0:CD30::/60
< strong>不是 IPV6 的法律代表
- 2001:0DB8:0:CD3/60
- 2001:0DB8::CD30/60
- 2001:0DB8::CD3/60
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
- ABCD:EF01:2345:6789:ABCD:EF01:2345:6789
- 2001:DB8:0:0:8:800:200C:417A
- FF01:0:0:0:0:0:0:101
- 0:0:0:0:0:0:0:1
- 0:0:0:0:0:0:0:0
- 2001:DB8::8:800:200C:417A
- FF01::101
- ::1
- ::
- 0:0:0:0:0:0:13.1.68.3
- 0:0:0:0:0:FFFF:129.144.52.38
- ::13.1.68.3
- FFFF:129.144.52.38
- 2001:0DB8:0000:CD30:0000:0000:0000:0000/60
- 2001:0DB8::CD30:0:0:0:0/60
- 2001:0DB8:0:CD30::/60
NOT legal representations of IPV6
- 2001:0DB8:0:CD3/60
- 2001:0DB8::CD30/60
- 2001:0DB8::CD3/60
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您可以使用 Guava 库,特别是使用
com.google.common.net.InetAddresses
类,调用isInetAddress()
。isInetAddress
public static boolean isInetAddress(String ipString)
如果提供的字符串是有效的 IP 字符串文字,则返回 true,否则返回 false。
参数:
ipString
- 要计算为 IP 字符串文字的字符串返回:
true
(如果参数是有效 IP)字符串文字You can use the Guava library, specifically using the
com.google.common.net.InetAddresses
class, callingisInetAddress()
.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 literalReturns:
true
if the argument is a valid IP string literalIPAddress Java 库 支持以 a 格式解析 IPv4 和 IPv6 CIDR 子网(即地址/前缀格式)多态方式。免责声明:我是项目经理。
以下方法是用于验证的示例代码:
使用问题中提供的示例,上述方法的输出为:
如您所见,问题关于 FFFF:129.144.52.38 有效且关于 2001:db8::cd30 是不正确的/60 和 2001:db8::cd3/60 无效。如果第一个是 ::FFFF:129.144.52.38 则有效
The IPAddress Java library supports parsing both IPv4 and IPv6 CIDR subnets (ie address/prefix format) in a polymorphic manner. Disclaimer: I am the project manager.
The following method is example code for validating:
Using the examples provided in the question, the output of the above method is:
As you can see, the question was incorrect about FFFF:129.144.52.38 being valid and about 2001:db8::cd30/60 and 2001:db8::cd3/60 being invalid. The first one would be valid if it were ::FFFF:129.144.52.38
看看这是否有效:
大约一年前,我购买了一个非常有用的程序,名为 RegexMagic,用于我计划使用的一些复杂的正则表达式。
这应该是Java,所以它应该编译,我假设/60可以在0000和FFFF的范围之间,你可以修改最后一部分。
/[A-F0-9]{0,4} 是我添加到正则表达式中以匹配您的示例的内容。
See if this works:
I purchased a very helpful program called RegexMagic nearly a year ago for some complicated regular expressions I planned on using.
This was suppose to be Java, so it should compile, I assume the /60 can be between the ranges of 0000 and FFFF you can modify that last part.
/[A-F0-9]{0,4} is what I added to the regular expression to match your example.
我的想法是把它分成两部分,前缀地址和前缀len。
My idea is to split it into two part, prefix address and prefix len.
严格来说,2.3节描述的不是地址表示,而是地址前缀的表示(即使是“全长”前缀也与地址不同)。
这意味着如果您需要验证地址,您可以安全地忽略此格式。
Strictly speaking, a section 2.3 does not describe an address representation, but a representation of address prefix (even the "full-length" prefix is not the same as an address).
That means you may safely disregard this format if you need to validate addresses.
我在java中尝试过下面的正则表达式,它适用于IPV4和IPV6
I had tried below regex in java and it worked for IPV4 and IPV6