GithubHelp home page GithubHelp logo

Comments (2)

seancfoley avatar seancfoley commented on May 23, 2024

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

from ipaddress-go.

dungtranhoang avatar dungtranhoang commented on May 23, 2024

Thank you

from ipaddress-go.

Related Issues (9)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.