去 - 计算下方的CIDR一位的CIDR

发布于 2025-02-13 19:29:44 字数 346 浏览 1 评论 0 原文

ask

您如何计算下面提供的CIDR以下的CIDR?

给定:

网络cidr:(192.168.0.0/16)

想要结果

192.168.0.0.0.0/17,192.168.128.0/17

使用默认 net等软件包软件包和 github.com/brotherpowers/ipsubnet github.com/seancfoley/ipaddress-go/ipaddr 没有得到所需的结果。

Ask

How do you calculate the cidr below one bit of the cidr provided?

Given:

a network CIDR : (192.168.0.0/16)

wanted result

192.168.0.0/17, 192.168.128.0/17

Using packages such as the default net package and github.com/brotherpowers/ipsubnet, github.com/seancfoley/ipaddress-go/ipaddr did not get the desired results.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

糖果控 2025-02-20 19:29:44

要将网络分为二,将前缀的长度增加一个。那给你下半部。要计算下半部分,请通过一个(省略为简洁的错误处理)来增加网络部分):

package main

import (
    "fmt"
    "math/big"
    "net/netip"
)

func main() {
    p := netip.MustParsePrefix("192.168.0.0/16")
    lo, hi := split(p)
    fmt.Println(lo, hi) // 192.168.0.0/17 192.168.128.0/17
}

func split(p netip.Prefix) (lo, hi netip.Prefix) {
    p = p.Masked()
    lo, _ = p.Addr().Prefix(p.Bits() + 1)

    delta := big.NewInt(1)
    delta.Lsh(delta, uint(lo.Addr().BitLen()-lo.Bits()))

    n := new(big.Int).SetBytes(lo.Addr().AsSlice())
    n.Add(n, delta)

    hiIP, _ := netip.AddrFromSlice(n.Bytes())
    hi = netip.PrefixFrom(hiIP, lo.Bits())

    return lo, hi
}

To split a network in two, increment the length of the prefix by one. That gives you the lower half. To compute the second half, increment the network part by one (error handling omitted for brevity):

package main

import (
    "fmt"
    "math/big"
    "net/netip"
)

func main() {
    p := netip.MustParsePrefix("192.168.0.0/16")
    lo, hi := split(p)
    fmt.Println(lo, hi) // 192.168.0.0/17 192.168.128.0/17
}

func split(p netip.Prefix) (lo, hi netip.Prefix) {
    p = p.Masked()
    lo, _ = p.Addr().Prefix(p.Bits() + 1)

    delta := big.NewInt(1)
    delta.Lsh(delta, uint(lo.Addr().BitLen()-lo.Bits()))

    n := new(big.Int).SetBytes(lo.Addr().AsSlice())
    n.Add(n, delta)

    hiIP, _ := netip.AddrFromSlice(n.Bytes())
    hi = netip.PrefixFrom(hiIP, lo.Bits())

    return lo, hi
}

https://go.dev/play/p/0HLqUK0RmVC

北音执念 2025-02-20 19:29:44

“使用默认网络软件包和github.com/brotherpowers/ipsubnet,github.com/seancfoley/ipaddress-go/ipaddr没有得到理想的结果

。用 github.com/seancfoley/ipaddress-go/ipaddr (注意此代码也可与IPv6一起使用,并且前缀长度的任何更改):

package main

import (
    "fmt"
    "github.com/seancfoley/ipaddress-go/ipaddr"
)

func main() {
    cidr := ipaddr.NewIPAddressString("192.168.0.0/16").GetAddress()
    for i := cidr.AdjustPrefixLen(1).PrefixBlockIterator(); i.HasNext(); {
        fmt.Println(i.Next())
    }
}

输出:输出:

192.168.0.0/17
192.168.128.0/17

"Using packages such as the default net package and github.com/brotherpowers/ipsubnet, github.com/seancfoley/ipaddress-go/ipaddr did not get the desired results."

Here is how to do it with github.com/seancfoley/ipaddress-go/ipaddr (note this code also works with IPv6 and any change in prefix length):

package main

import (
    "fmt"
    "github.com/seancfoley/ipaddress-go/ipaddr"
)

func main() {
    cidr := ipaddr.NewIPAddressString("192.168.0.0/16").GetAddress()
    for i := cidr.AdjustPrefixLen(1).PrefixBlockIterator(); i.HasNext(); {
        fmt.Println(i.Next())
    }
}

Output:

192.168.0.0/17
192.168.128.0/17
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文