Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
d37aa3eb6b
|
||
|
|
0c8577f149
|
@@ -89,7 +89,10 @@ 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"`
|
||||
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
|
||||
}
|
||||
|
||||
|
||||
26
cmd/subnetter/funcs_vlsmargs.go
Normal file
26
cmd/subnetter/funcs_vlsmargs.go
Normal 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
|
||||
}
|
||||
33
cmd/subnetter/funcs_vlsmsize.go
Normal file
33
cmd/subnetter/funcs_vlsmsize.go
Normal 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
|
||||
}
|
||||
@@ -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,9 +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,
|
||||
PrefixLengths: args.VLSM.Sizes,
|
||||
Explicit: args.VLSM.Explicit,
|
||||
PrefixLengths: vlsmSizes,
|
||||
BaseSplitter: new(netsplit.BaseSplitter),
|
||||
}
|
||||
default:
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -2,7 +2,7 @@ package netsplit
|
||||
|
||||
import (
|
||||
"net/netip"
|
||||
"sort"
|
||||
`sort`
|
||||
|
||||
"go4.org/netipx"
|
||||
)
|
||||
@@ -42,6 +42,7 @@ func (v *VLSMSplitter) Split() (nets []*netip.Prefix, remaining *netipx.IPSet, e
|
||||
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.
|
||||
@@ -53,6 +54,7 @@ func (v *VLSMSplitter) Split() (nets []*netip.Prefix, remaining *netipx.IPSet, e
|
||||
return
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
pfxLen, _ = v.network.Mask.Size()
|
||||
pfxLen8 = uint8(pfxLen)
|
||||
|
||||
@@ -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"`
|
||||
|
||||
Reference in New Issue
Block a user