ADDED:
* The -s/--size argument to the VLSM splitter may now be a
  comma-delimited list of sizes instead of needing to specify -s/--size
  for each size.
  This change is backwards-compatible.
This commit is contained in:
brent saner 2025-04-04 11:53:55 -04:00
parent 0c8577f149
commit d37aa3eb6b
Signed by: bts
GPG Key ID: 8C004C2F93481F6B
5 changed files with 72 additions and 4 deletions

View File

@ -88,9 +88,11 @@ 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."`
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"`
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."`
// Custom type for now; see https://github.com/jessevdk/go-flags/issues/245
// Sizes []uint8 `short:"s" long:"size" required:"true" description:"Prefix lengths. May be specified multiple times." validate:"required"`
Sizes []vlsmSize `short:"s" long:"size" required:"true" description:"Prefix lengths. May be specified multiple times or as a comma-delimited list." validate:"required"`
splitArgs
}


View File

@ -0,0 +1,26 @@
package main

/*
AllSizes returns a properly parsed and consolidated slice
of all specified sizes, as it takes two valid syntaxes (`-s 32 -s 32`, `-s 32,32`)
that can be mixed together.
*/
func (v *VLSMArgs) AllSizes() (sizes []uint8, err error) {

var sizeSlice []uint8

if v == nil {
return
}
if v.Sizes == nil || len(v.Sizes) == 0 {
return
}
for _, s := range v.Sizes {
if sizeSlice, err = s.Sizes(); err != nil {
return
}
sizes = append(sizes, sizeSlice...)
}

return
}

View File

@ -0,0 +1,33 @@
package main

import (
`strconv`
`strings`
)

// Sizes returns a parsed/split slice of uint8s from a vlsmSize.
func (v *vlsmSize) Sizes() (sizes []uint8, err error) {

var s []string
var u uint64

if v == nil {
return
}
s = strings.Split(string(*v), ",")
for idx, i := range s {
s[idx] = strings.TrimSpace(i)
}

sizes = make([]uint8, len(s))

// No validation is performed since we don't have access to the addr inet family; that's up to the parsers.
for idx, i := range s {
if u, err = strconv.ParseUint(i, 10, 8); err != nil {
return
}
sizes[idx] = uint8(u)
}

return
}

View File

@ -26,6 +26,7 @@ func main() {
var pfx *net.IPNet
var resPfx *netip.Prefix
var origPfx netip.Prefix
var vlsmSizes []uint8
var splitter netsplit.NetSplitter
var cmnArgs common
var nets []*netip.Prefix
@ -170,10 +171,13 @@ func main() {
log.Panicln(err)
}
cmnArgs = args.VLSM.common
if vlsmSizes, err = args.VLSM.AllSizes(); err != nil {
log.Panicln(err)
}
splitter = &netsplit.VLSMSplitter{
Ascending: args.VLSM.Asc,
Explicit: args.VLSM.Explicit,
PrefixLengths: args.VLSM.Sizes,
PrefixLengths: vlsmSizes,
BaseSplitter: new(netsplit.BaseSplitter),
}
default:

View File

@ -96,6 +96,9 @@ type tableFormatter struct {
NoBoldTitle bool
}

// vlsmSize is a custom type to let us specify multiple sizes as a repeated or consolidated argument.
type vlsmSize string

type ReservedResults struct {
Opts CheckArgs
Reserved map[netip.Prefix]*netsplit.IANAAddrNetResRecord