checking in- working on fix for numhost subnetting

This commit is contained in:
brent saner
2025-04-06 14:37:57 -04:00
parent 3c239a4d09
commit fd344f3b8e
3 changed files with 50 additions and 26 deletions

View File

@@ -4,7 +4,8 @@ import (
"encoding/json"
"encoding/xml"
"fmt"
`math`
"math"
"math/big"
"net"
"net/netip"
"strings"
@@ -151,19 +152,19 @@ func AddrInvert(ip netip.Addr) (inverted netip.Addr) {
}
/*
CheckReserved checks nets for any reserved prefixes; either directly/explicitly,
included *within* a reserved prefix (revRecursive), or *including* a reserved prefix (recursive).
excludePrivate indicates if LAN networks should be considered as "reserved" or not.
If a network is found via revRecursive/recursive, the matching prefix - not the specified one - will be in reservations.
CheckReserved checks nets for any reserved prefixes; either directly/explicitly,
included *within* a reserved prefix (revRecursive), or *including* a reserved prefix (recursive).
excludePrivate indicates if LAN networks should be considered as "reserved" or not.
If a network is found via revRecursive/recursive, the matching prefix - not the specified one - will be in reservations.
Any found will be returned in reservations.
Any found will be returned in reservations.
If no reserved networks are found, reservations will be nil.
If no reserved networks are found, reservations will be nil.
Note that prefix-specific broadcasts (e.g. x.255.255.255/8, x.x.x.255/24, ::/64, x:ffff:ffff:ffff:ffff/64, etc.)
will *not* be considered as "reserved" as they are considered normal addresses expected for functionality.
This primarily focuses on prefixes/subnets for this reason.
Additionally, all of nets will be aligned to their proper boundary range/CIDR/subnet.
Note that prefix-specific broadcasts (e.g. x.255.255.255/8, x.x.x.255/24, ::/64, x:ffff:ffff:ffff:ffff/64, etc.)
will *not* be considered as "reserved" as they are considered normal addresses expected for functionality.
This primarily focuses on prefixes/subnets for this reason.
Additionally, all of nets will be aligned to their proper boundary range/CIDR/subnet.
*/
func CheckReserved(nets []*netip.Prefix, revRecursive, recursive, excludePrivate bool) (reservations map[netip.Prefix]*IANAAddrNetResRecord, err error) {
@@ -377,12 +378,34 @@ func MaskInvert(mask net.IPMask) (inverted net.IPMask) {
}
/*
NumNets returns the number of times prefix size subnet fits into prefix size network.
NumAddrsNet returns the number of IP addresses in a net.IPNet.
It will error if network is larger than 128 or if subnet is smaller than network.
# The network address is included in the count if inclNet is true, otherwise it is excluded
This is MUCH more performant than splitting out an actual network into explicit subnets,
and does not require an actual network.
Only assignable addresses ("hosts") are considered if hostsOnly is true,
otherwise all addresses are counted (depending on inclNet).
*/
func NumAddrsNet(pfx *net.IPNet, inclNet, hostsOnly bool) (numAddrs *big.Int) {
if pfx == nil {
return
}
// TODO
return
}
// NumAddrsPfx is the exact same as NumAddrsNet but for a net/netip.Prefix instead.
// TODO
/*
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) {
@@ -397,9 +420,7 @@ func NumNets(subnet, network uint8) (numNets uint, ipv6Only bool, err error) {
err = ErrBigPrefix
return
}
if network > maxBitsv4 {
ipv6Only = true
}
ipv6Only = (network > maxBitsv4) || (subnet > maxBitsv4)
x = float64(subnet - network)