ADDED:
* num-nets subcommand' this is MUCH much faster than actually splitting
  if you're trying to figure out how many times a given subnet fits into
  a network.
This commit is contained in:
brent saner
2025-04-06 01:31:51 -04:00
parent 701b598b1c
commit 3c239a4d09
5 changed files with 91 additions and 8 deletions

View File

@@ -4,6 +4,7 @@ import (
"encoding/json"
"encoding/xml"
"fmt"
`math`
"net"
"net/netip"
"strings"
@@ -375,6 +376,38 @@ func MaskInvert(mask net.IPMask) (inverted net.IPMask) {
return
}
/*
NumNets returns the number of times prefix size subnet fits into prefix size network.
It will error if network is larger than 128 or if subnet is smaller than network.
This is MUCH more performant than splitting out an actual network into explicit subnets,
and does not require an actual network.
*/
func NumNets(subnet, network uint8) (numNets uint, ipv6Only bool, err error) {
var x float64
// network cannot be higher than 128, as that's the maximum for IPv6.
if network > maxBits {
err = ErrBadPrefixLen
return
}
if subnet < network {
err = ErrBigPrefix
return
}
if network > maxBitsv4 {
ipv6Only = true
}
x = float64(subnet - network)
numNets = uint(math.Pow(2, x))
return
}
// Parse parses b for JSON/XML/YAML and tries to return a StructuredResults from it.
func Parse(b []byte) (s *StructuredResults, err error) {