简体   繁体   English

从net.CIDRMask获取ipv6子网掩码

[英]Getting ipv6 subnet mask from net.CIDRMask

I am working on code, and trying to add ipv6 support. 我正在编写代码,并尝试添加ipv6支持。 The following code is in the current code base for ipv4 support. 以下代码是ipv4支持的当前代码库。 The code takes a ipv4 ip address and gets the subnet mask for the address on a /32. 该代码获取一个ipv4 ip地址,并在/ 32上获取该地址的子网掩码。

// string of ip address
networkInterface["ip_address"] = v.IpAddress[0]
m := net.CIDRMask(v.IpConfig.IpAddress[0].PrefixLength, 32)
subnetMask := net.IPv4(m[0], m[1], m[2], m[3])
networkInterface["subnet_mask"] = subnetMask.String()

I know that net.CIDRMask works with ipv6, I am uncertain how to use it with an ipv6 address. 我知道net.CIDRMask可与ipv6一起使用,我不确定如何将其与ipv6地址一起使用。

I am now testing the ip address to determine if the address is ipv4 or ipv6: 我现在正在测试IP地址,以确定该地址是ipv4还是ipv6:

testInput := net.ParseIP(v.IpAddress[0])
if testInput.To4() != nil {
// find ipv4 subnet mask
}
if testInput.To16() != nil {
// do ipv6 subnet mask
}

The unit tests for net.CIDRMask have examples working with ipv6 located here: https://golang.org/src/net/ip_test.go net.CIDRMask的单元测试中net.CIDRMask使用ipv6的示例,位于以下位置: https : net.CIDRMask

But it is beyond both my golang experience and ipv6 knowledge. 但这超出了我的golang经验和ipv6知识。

While RTFM'ing the docs https://golang.org/pkg/net/#CIDRMask mention: 在RTFM为文档https://golang.org/pkg/net/#CIDRMask提及时:

func CIDRMask(ones, bits int) IPMask

CIDRMask returns an IPMask consisting of `ones' 1 bits followed by 0s up to a total length of `bits' bits. CIDRMask返回一个IPMask,该IPMask由“ ones” 1位和后跟0s组成,总长度为“ bits”位。 For a mask of this form, CIDRMask is the inverse of IPMask.Size. 对于这种形式的蒙版,CIDRMask是IPMask.Size的逆。

So what values do I use for ones and bits ? 那么什么样的价值观做我用onesbits

This is what is comming back from the api: 这是从api返回的内容:

$ govc vm.info -json vcsa | jq .VirtualMachines[0].Guest.Net[0].IpConfig.IpAddress [   {
    "IpAddress": "10.20.128.218",
    "PrefixLength": 22,
    "Origin": "",
    "State": "preferred",
    "Lifetime": null   } ]

Thanks in advance! 提前致谢!

I'm not sure what PrefixLength is, it may be some field defined in one of your structs, but it doesn't appear to be a field on anything in the net package, or in fact anywhere in the standard library: https://golang.org/search?q=PrefixLength . 我不确定PrefixLength是什么,它可能是在您的一个结构中定义的某个字段,但它似乎不是net包中任何内容的字段,或者实际上不是标准库中任何地方的字段: https:/ /golang.org/search?q=PrefixLength

So I'm not sure what PrefixLength is expected to give, but, I can tell you: 所以我不确定PrefixLength应该给什么,但是,我可以告诉你:

  • IPv4 addresses consist of 32 bits of data (256 x 256 x 256 x 256 total IPs), so when dealing with IPv4, the value for the bits argument to net.CIDRMask should be 32. IPv4地址包含32位数据(总共256 x 256 x 256 x 256个IP),因此在处理IPv4时, net.CIDRMaskbits参数的值应为32。
  • IPv4 addresses have 128 bits of data, so the bits argument is 128. IPv4地址具有128位数据,因此bits参数为128。
  • The subnet mask for a CIDR range corresponding to a single IP will have the maximum number of ones, so the ones value is 32 or 128, depending on whether you're talking IPv4 or IPv6. 对应于一个单一的IP一个CIDR范围的子网掩码将有那些的最大数量,所以ones值是32或者128,这取决于你在说IPv4或IPv6。

Therefore, for IPv4, you should call net.CIDRMask(32, 32) , and for IPv6, net.CIDRMask(128, 128) . 因此,对于IPv4,应调用net.CIDRMask(32, 32) ,对于IPv6,应调用net.CIDRMask(32, 32) net.CIDRMask(128, 128) Since these will be the exact same calculations every time, you have the option to simply set the values up front as constants in your code. 由于每次都将进行完全相同的计算,因此您可以选择将代码中的值预先设置为常量。 The correct values are: 正确的值是:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM