首页
学习
活动
专区
圈层
工具
发布

IPv6验证
EN

Stack Overflow用户
提问于 2011-05-11 11:05:40
回答 6查看 21.2K关注 0票数 15

我使用IPAddressUtil.isIPv6LiteralAddress (ipAddress)方法来验证IPv6,但是对于IPV6的IPV6 6地址/前缀长度格式(格式在RFC 4291第2.3节中提到),这种方法失败了。

有谁知道任何验证“ipv6 6地址/前缀长度”格式的验证器吗?

IPV6的法律表述

  1. ABCD:EF01 01:2345:6789:ABCD:EF01 01:2345:6789
  2. 2001年:Db8:0:0:8:800:200 C:417 A
  3. FF01 01:0:0:0:0:0:0:101
  4. 0:0:0:0:0:0:
  5. 0:0:0:0:0:0:0
  6. 2001年:Db8::8:800:200 C:417 A
  7. FF01 01: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. F:129.144.52.38
  14. 2001:0DB8:0000:60 30:0000:0000:0000:0000/60
  15. 2001年:0DB8::60 30:0:0:0:0/60
  16. 2001年:0DB8:0:60 30::/60

非IPV6的法律表述

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

回答 6

Stack Overflow用户

回答已采纳

发布于 2011-05-11 12:14:11

看看这是否有效:

代码语言:javascript
复制
try {
    if (subjectString.matches(
        "(?ix)\\A(?:                                                  # Anchor address\n" +
        " (?:  # Mixed\n" +
        "  (?:[A-F0-9]{1,4}:){6}                                # Non-compressed\n" +
        " |(?=(?:[A-F0-9]{0,4}:){2,6}                           # Compressed with 2 to 6 colons\n" +
        "     (?:[0-9]{1,3}\\.){3}[0-9]{1,3}                     #    and 4 bytes\n" +
        "     \\z)                                               #    and anchored\n" +
        "  (([0-9A-F]{1,4}:){1,5}|:)((:[0-9A-F]{1,4}){1,5}:|:)  #    and at most 1 double colon\n" +
        " |::(?:[A-F0-9]{1,4}:){5}                              # Compressed with 7 colons and 5 numbers\n" +
        " )\n" +
        " (?:(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\\.){3}  # 255.255.255.\n" +
        " (?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])           # 255\n" +
        "|     # Standard\n" +
        " (?:[A-F0-9]{1,4}:){7}[A-F0-9]{1,4}                    # Standard\n" +
        "|     # Compressed\n" +
        " (?=(?:[A-F0-9]{0,4}:){0,7}[A-F0-9]{0,4}               # Compressed with at most 7 colons\n" +
        "    \\z)                                                #    and anchored\n" +
        " (([0-9A-F]{1,4}:){1,7}|:)((:[0-9A-F]{1,4}){1,7}|:)    #    and at most 1 double colon\n" +
        "|(?:[A-F0-9]{1,4}:){7}:|:(:[A-F0-9]{1,4}){7}           # Compressed with 8 colons\n" +
        ")/[A-F0-9]{0,4}\\z                                                    # Anchor address")) 
        {
        // String matched entirely
    } else {
        // Match attempt failed
    } 
} catch (PatternSyntaxException ex) {
    // Syntax error in the regular expression
}

大约一年前,我为一些复杂的正则表达式购买了一个名为RegexMagic的非常有用的程序。

这应该是Java,所以它应该编译,我假设/60可以在0000和FFFF之间,您可以修改最后一部分。

/A-F0-9{0,4}是我添加到正则表达式中以匹配示例的内容。

票数 2
EN

Stack Overflow用户

发布于 2013-01-18 00:21:05

您可以使用Guava库,特别是使用com.google.common.net.InetAddresses类,调用isInetAddress()

isInetAddress

public static boolean isInetAddress(String ipString)

如果提供的字符串是有效的IP字符串文本,则返回true,否则返回false。

参数:作为IP字符串文本计算的ipString - String

如果参数是有效的IP字符串文本,返回true

票数 7
EN

Stack Overflow用户

发布于 2019-04-07 19:29:05

IPAddress Java库支持以多态方式解析IPv4和IPv6 CIDR子网(即地址/前缀格式)。免责声明:我是项目经理。

下面的方法是验证的示例代码:

代码语言:javascript
复制
static void parse(String str) {
    IPAddressString addrString = new IPAddressString(str);
    try {
         IPAddress addr = addrString.toAddress();
         IPAddress hostAddr = addrString.toHostAddress();
         Integer prefix = addr.getNetworkPrefixLength();
         if(prefix == null) {
             System.out.println(addr + " has no prefix length"); 
         } else {
             System.out.println(addr + " has host address " + hostAddr + " and prefix length " + prefix);
         }
    } catch(AddressStringException e) {
        System.out.println(addrString + " is invalid: " + e.getMessage());
    }
}

使用问题中提供的例子,上述方法的输出如下:

代码语言:javascript
复制
abcd:ef01:2345:6789:abcd:ef01:2345:6789 has no prefix length
2001:db8::8:800:200c:417a has no prefix length
ff01::101 has no prefix length
::1 has no prefix length
:: has no prefix length
2001:db8::8:800:200c:417a has no prefix length
ff01::101 has no prefix length
::1 has no prefix length
:: has no prefix length
::d01:4403 has no prefix length
::ffff:8190:3426 has no prefix length
::d01:4403 has no prefix length
FFFF:129.144.52.38 is invalid: FFFF:129.144.52.38 IP Address error: address has too few segments
2001:db8:0:cd30::/60 has host address 2001:db8:0:cd30:: and prefix length 60
2001:db8:0:cd30::/60 has host address 2001:db8:0:cd30:: and prefix length 60
2001:db8:0:cd30::/60 has host address 2001:db8:0:cd30:: and prefix length 60
2001:0DB8:0:CD3/60 is invalid: 2001:0DB8:0:CD3/60 IP Address error: address has too few segments
2001:db8::cd30/60 has host address 2001:db8::cd30 and prefix length 60
2001:db8::cd3/60 has host address 2001:db8::cd3 and prefix length 60

如你所见,关于FFFF:129.144.52.38是有效的,大约2001年:Db8::cd3 30/60和2001:Db8:Db8:CD3/60无效,这个问题是不正确的。第一个条件是::FFFF:129.144.52.38

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5963199

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档