ADDED:
* num-addrs subcommand, to get the number of hosts/addresses in a given
  prefix length
* get-net subcommand, to more easily get a single subnet from either the
  beginning or the end of a prefix. (MUCH FASTER than CIDR-splitting!)
This commit is contained in:
brent saner
2025-04-13 18:25:32 -04:00
parent c05f9c4d47
commit 860ad5842b
16 changed files with 334 additions and 78 deletions

View File

@@ -31,7 +31,16 @@ type NetSplitter interface {
// BaseSplitter is used to encapsulate the "parent" network to be split.
type BaseSplitter struct {
network *net.IPNet
network *net.IPNet `validate:"required"`
}
// PrefixGetter is a "pseudo-splitter"; it splits according to a given prefix but only returns a specfic subnet (either the first or the last).
type PrefixGetter struct {
// Pos is the position in BaseSplitter.network for the selected PrefixLength.
Pos string `json:"pos" xml:"pos,attr" yaml:"Position" validate:"required,oneof=first last"`
// PrefixLength specifies the CIDR/prefix length of the subnet.
PrefixLength uint8 `json:"prefix" xml:"prefix,attr" yaml:"network Prefix Length" validate:"required,lte=128"`
*BaseSplitter
}
/*
@@ -40,7 +49,11 @@ It attemps to split the network into as many networks of size PrefixLength as cl
*/
type CIDRSplitter struct {
// PrefixLength specifies the CIDR/prefix length of the subnets to split out.
PrefixLength uint8 `json:"prefix" xml:"prefix,attr" yaml:"network Prefix Length"`
PrefixLength uint8 `json:"prefix" xml:"prefix,attr" yaml:"network Prefix Length" validate:"required,lte=128"`
// TODO: See CIDRSplitter.Split for future optimization using this.
// LenSwitch specifies the threshold bit offset after which (inclusive) it switches from a repeated prefix removal to manual recursive binary split.
// If 0, 12 is the default.
// LenSwitch uint8 `json:"switch_at" xml:"switchAt,attr" yaml:"Switch Offset Threshold"`
*BaseSplitter `json:"net" xml:"net,omitempty" yaml:"network,omitempty"`
}
@@ -50,7 +63,7 @@ It attempts to evenly distribute addresses amoungs subnets.
*/
type HostSplitter struct {
// NumberHosts is the number of hosts to be placed in each subnet to split out.
NumberHosts uint `json:"hosts" xml:"hosts,attr" yaml:"Number of Hosts Per Subnet"`
NumberHosts uint `json:"hosts" xml:"hosts,attr" yaml:"Number of Hosts Per Subnet" validate:"required"`
// InclNetAddr, if true, specifies that NumberHosts includes the network address.
InclNetAddr bool `json:"net_addr" xml:"netAddr,attr,omitempty" yaml:"Network Address Included,omitempty"`
// InclBcastAddr, if true, specifies that NumberHosts includes the broadcast address.
@@ -66,7 +79,7 @@ as cleanly as poossible.
*/
type SubnetSplitter struct {
// NumberSubnets indicates the number of subnets to split the network into.
NumberSubnets uint `json:"nets" xml:"nets,attr" yaml:"Number of Target Subnets"`
NumberSubnets uint `json:"nets" xml:"nets,attr" yaml:"Number of Target Subnets" validate:"required"`
// Strict, if true, will return an error from Split if the network sizes cannot split into equally-sized networks.
Strict bool `json:"strict" xml:"strict,attr,omitempty" yaml:"Strictly Equal Subnet Sizes"`
*BaseSplitter `json:"net" xml:"net,omitempty" yaml:"network,omitempty"`
@@ -91,7 +104,7 @@ type VLSMSplitter struct {
*/
Explicit bool `json:"explicit,omitempty" xml:"explicit,attr,omitempty" yaml:"Explicit Ordering,omitempty"`
// PrefixLengths contains the prefix lengths of each subnet to split out from the network.
PrefixLengths []uint8 `json:"prefixes" xml:"prefixes>prefix" yaml:"Prefix Lengths"`
PrefixLengths []uint8 `json:"prefixes" xml:"prefixes>prefix" yaml:"Prefix Lengths" validate:"required,dive,lte=128"`
*BaseSplitter `json:"net" xml:"net,omitempty" yaml:"network,omitempty"`
}