v0.2.0
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:
@@ -8,6 +8,12 @@ import (
|
||||
`github.com/go-resty/resty/v2`
|
||||
)
|
||||
|
||||
const (
|
||||
maxBitsv4 uint8 = 32
|
||||
maxBitsv6 uint8 = 128
|
||||
maxBits uint8 = maxBitsv6
|
||||
)
|
||||
|
||||
const (
|
||||
cachedirEnvName string = "SBNTR_RSVCACHE_DIR"
|
||||
// https://www.iana.org/assignments/iana-ipv4-special-registry/iana-ipv4-special-registry.xhtml
|
||||
|
||||
@@ -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) {
|
||||
|
||||
|
||||
Reference in New Issue
Block a user