seancfoley / ipaddress-go

Go library for handling IP addresses and subnets, both IPv4 and IPv6
https://seancfoley.github.io/IPAddress/
Apache License 2.0
87 stars 9 forks source link

How to list free subnet can use in ipv6 #6

Closed dungtranhoang closed 1 year ago

dungtranhoang commented 1 year ago

Hi, Thanks for your great work. I have read the issue #1 and #2 But none of these match my needed So like I have an root network is

2001:db8::/32

Then I assigned two addresses 2001:db8:0:4::/63 2001:db8:0:2::/63 Then I wanna do kind of this


func main() {

    listSubnets("2001:db8::/32", 63)
}

func listSubnets(original string, newPrefix int) {
    subnet := ipaddr.NewIPAddressString(original).GetAddress()
    iterator := subnet.SetPrefixLen(newPrefix).PrefixIterator()
    for iterator.HasNext() {
        fmt.Println(iterator.Next(), " ")
    }
}

But now the listSubnets will input an array of IPAddresses that Taken and then will ignore it from the list (with newPrefix) when list Some kind like i Choose the newPrefix is 64 it will not show the 2001:db8:0:4::/64 and 2001:db8:0:2::/64 Example exptected output image

seancfoley commented 1 year ago

The following code does what you are describing:

package main

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

func main() {
    listSubnetsExclude("2001:db8::/32", 64, []string{"2001:db8:0:4::/64", "2001:db8:0:2::/64"})
}

func listSubnetsExclude(original string, newPrefix int, excludeAddrs []string) {
    subnet := ipaddr.NewIPAddressString(original).GetAddress()
    newSubnets := []*ipaddr.IPAddress{subnet.SetPrefixLen(newPrefix)}
    for _, addr := range excludeAddrs {
        var result []*ipaddr.IPAddress
        removeAddr := ipaddr.NewIPAddressString(addr).GetAddress()
        for _, s := range newSubnets {
            result = append(result, s.Subtract(removeAddr)...)
        }
        newSubnets = result
    }
    fmt.Println(newSubnets)
    //iterateSubnets(newSubnets) // see below
}

Output:

[2001:db8:1-ffff:*::/64 2001:db8:0:0-1::/64 2001:db8:0:3::/64 2001:db8:0:5-ffff::/64]

You could iterate on those subnets as in the listSubnets function above, but that's a very large number of subnets, about 2 to the power of 32, more than a billion. So you would not want to iterate through the whole thing.

func iterateSubnets(newSubnets []*ipaddr.IPAddress) {
    i := 0
top:
    for _, s := range newSubnets {
        iterator := s.PrefixIterator()
        for iterator.HasNext() {
            net := iterator.Next()
            fmt.Print(net, " ")
            i++
            if i > 10 {
                break top
            }
        }
    }
    fmt.Println("...")
}

Output:

2001:db8:1::/64 2001:db8:1:1::/64 2001:db8:1:2::/64 2001:db8:1:3::/64 2001:db8:1:4::/64 2001:db8:1:5::/64 2001:db8:1:6::/64 2001:db8:1:7::/64 2001:db8:1:8::/64 2001:db8:1:9::/64 2001:db8:1:a::/64 ...

On a more general level, you might wish to look at a couple examples of subnetting:

https://github.com/seancfoley/ipaddress-go/wiki/Code-Examples-3:-Subnetting-and-Other-Subnet-Operations#variable-length-subnetting and https://github.com/seancfoley/ipaddress-go/wiki/Code-Examples-3:-Subnetting-and-Other-Subnet-Operations#automatic-subnetting

dungtranhoang commented 1 year ago

Thank you