From 0c8577f149cdc188b8a058f042ea89b8b18221ac Mon Sep 17 00:00:00 2001 From: brent saner Date: Thu, 3 Apr 2025 18:35:35 -0400 Subject: [PATCH] v0.1.0 ADDED: * Explicit subnet ordering option for VLSM --- cmd/subnetter/args.go | 5 +++-- cmd/subnetter/main.go | 1 + netsplit/funcs_vlsmsplitter.go | 26 ++++++++++++++------------ netsplit/types.go | 7 +++++++ 4 files changed, 25 insertions(+), 14 deletions(-) diff --git a/cmd/subnetter/args.go b/cmd/subnetter/args.go index 4fa8431..cf673e6 100644 --- a/cmd/subnetter/args.go +++ b/cmd/subnetter/args.go @@ -88,8 +88,9 @@ type XNetArgs struct { } type VLSMArgs struct { - Asc bool `short:"A" long:"ascending" description:"If specified, place smaller networks (larger prefixes) at the beginning. You almost assuredly do not want to do this."` - Sizes []uint8 `short:"s" long:"size" required:"true" description:"Prefix lengths. May be specified multiple times." validate:"required"` + Asc bool `short:"A" long:"ascending" description:"If specified, place smaller networks (larger prefixes) at the beginning. You almost assuredly do not want to do this."` + Explicit bool `short:"O" long:"explicit-order" description:"If specified, ignore -A/--ascending and do no reordering of prefix sizes whatsoever, instead using the order given. This is EXTREMELY suboptimal and can lead to drastic addressing waste."` + Sizes []uint8 `short:"s" long:"size" required:"true" description:"Prefix lengths. May be specified multiple times." validate:"required"` splitArgs } diff --git a/cmd/subnetter/main.go b/cmd/subnetter/main.go index 646c182..085d1ad 100644 --- a/cmd/subnetter/main.go +++ b/cmd/subnetter/main.go @@ -172,6 +172,7 @@ func main() { cmnArgs = args.VLSM.common splitter = &netsplit.VLSMSplitter{ Ascending: args.VLSM.Asc, + Explicit: args.VLSM.Explicit, PrefixLengths: args.VLSM.Sizes, BaseSplitter: new(netsplit.BaseSplitter), } diff --git a/netsplit/funcs_vlsmsplitter.go b/netsplit/funcs_vlsmsplitter.go index 005e8a8..94704ba 100644 --- a/netsplit/funcs_vlsmsplitter.go +++ b/netsplit/funcs_vlsmsplitter.go @@ -2,7 +2,7 @@ package netsplit import ( "net/netip" - "sort" + `sort` "go4.org/netipx" ) @@ -42,17 +42,19 @@ func (v *VLSMSplitter) Split() (nets []*netip.Prefix, remaining *netipx.IPSet, e return } - sort.SliceStable( - v.PrefixLengths, - func(i, j int) (isBefore bool) { // We use a reverse sorting by default so we get larger prefixes at the beginning. - if v.Ascending { - isBefore = v.PrefixLengths[i] > v.PrefixLengths[j] - } else { - isBefore = v.PrefixLengths[i] < v.PrefixLengths[j] - } - return - }, - ) + if !v.Explicit { + sort.SliceStable( + v.PrefixLengths, + func(i, j int) (isBefore bool) { // We use a reverse sorting by default so we get larger prefixes at the beginning. + if v.Ascending { + isBefore = v.PrefixLengths[i] > v.PrefixLengths[j] + } else { + isBefore = v.PrefixLengths[i] < v.PrefixLengths[j] + } + return + }, + ) + } pfxLen, _ = v.network.Mask.Size() pfxLen8 = uint8(pfxLen) diff --git a/netsplit/types.go b/netsplit/types.go index d21bf2c..636d9b8 100644 --- a/netsplit/types.go +++ b/netsplit/types.go @@ -79,6 +79,13 @@ type VLSMSplitter struct { You almost assuredly do not want to do this. */ Ascending bool + /* + Explicit, if true, will ignore Ascending completely and split in the explicit order of PrefixLengths. + + This has the potential to be *extremely* wasteful of addressing space as the resulting blocks are + VERY unoptimized. + */ + Explicit bool // PrefixLengths contains the prefix lengths of each subnet to split out from the network. PrefixLengths []uint8 `json:"prefixes" xml:"prefixes>prefix" yaml:"Prefix Lengths"` *BaseSplitter `json:"net" xml:"net,omitempty" yaml:"network,omitempty"`