将Golang的CIDR前缀表示法转换为IP地址/子网掩码(点分十进制)。

huangapple go评论92阅读模式
英文:

golang CIDR prefix notation to IP address/subnet mask (dot-decimal)

问题

在Golang中是否有类似于'npm netmask'的东西?我需要将CIDR格式的网络掩码(例如10.0.0.0/8)转换为点分十进制格式(例如10.0.0.0/255.0.0.0)。

我在/golang.org/src/net/ip.go中找不到它。

英文:

Is there something similar to 'npm netmask' in golang ? I need to convert i.e. 10.0.0.0/8 to 10.0.0.0/255.0.0.0 so basically netmask in CIDR format to dot-decimal.

var Netmask = require('netmask').Netmask
var block = new Netmask('10.0.0.0/8');
block.mask; // 255.0.0.0

I can't find it in /golang.org/src/net/ip.go

答案1

得分: 4

标准的Go库中没有提供创建该表示的函数。尽管如此,自己实现并不难:

func main() {
    _, ipv4Net, err := net.ParseCIDR("10.0.0.0/8")
    if err != nil {
        log.Fatal(err)
    }
    fmt.Println(ipv4MaskString(ipv4Net.Mask))
}

func ipv4MaskString(m []byte) string {
    if len(m) != 4 {
        panic("ipv4Mask: len must be 4 bytes")
    }

    return fmt.Sprintf("%d.%d.%d.%d", m[0], m[1], m[2], m[3])
}

请注意,此格式仅适用于IPv4掩码。如果传递的是IPv6掩码,将会引发错误。

英文:

The go standard library does not have a function to create that representation. That being said, it is not difficult to do yourself:

https://play.golang.org/p/XT_UXoy6ra

func main() {
    _, ipv4Net, err := net.ParseCIDR("10.0.0.0/8")
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(ipv4MaskString(ipv4Net.Mask))
}

func ipv4MaskString(m []byte) string {
	if len(m) != 4 {
		panic("ipv4Mask: len must be 4 bytes")
	}

	return fmt.Sprintf("%d.%d.%d.%d", m[0], m[1], m[2], m[3])
}

Keep in mind this format only works for ipv4 masks. If you were to pass an ipv6 mask, this would panic.

答案2

得分: 1

你可以使用golang标准库来获取点分表示,方法如下:

ipaddr, ipv4Net, err := net.ParseCIDR("10.0.0.0/8")
// 处理错误
fmt.Println(net.IP(ipv4Net.Mask).String())

这段代码之所以有效,是因为掩码和IP都是由[]byte切片支持的(请参考stdlib源代码),所以你可以通过直接进行类型转换来在它们之间进行转换。

英文:

You can use the golang standard library to get the dot representation like so:

ipaddr, ipv4Net, err := net.ParseCIDR("10.0.0.0/8")
// handle error
fmt.Println(net.IP(ipv4Net.Mask).String())

This works because both the mask and the IP is backed by a []byte slice (see the stdlib source), so you can convert between them by casting directly.

答案3

得分: 0

以下代码可以同时处理IPv4和IPv6,使用IPAddress Go库。免责声明:我是项目经理。

func main() {
	convert("10.0.0.0/8")
	convert("10::/64")
}

func convert(cidrStr string) {
	cidr := ipaddr.NewIPAddressString(cidrStr).GetAddress()
	fmt.Println(cidr.GetNetworkMask())
}

输出结果:

255.0.0.0
ffff:ffff:ffff:ffff::
英文:

The following code works with both IPv4 and IPv6 using the IPAddress Go library. Disclaimer: I am the project manager.

func main() {
	convert("10.0.0.0/8")
	convert("10::/64")
}

func convert(cidrStr string) {
	cidr := ipaddr.NewIPAddressString(cidrStr).GetAddress()
	fmt.Println(cidr.GetNetworkMask())
}

Output:

255.0.0.0
ffff:ffff:ffff:ffff::

huangapple
  • 本文由 发表于 2017年9月6日 04:55:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/46063228.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定