checking in- needs some refinement then done
This commit is contained in:
@@ -150,8 +150,10 @@ func AddrInvert(ip netip.Addr) (inverted netip.Addr) {
|
||||
}
|
||||
|
||||
/*
|
||||
CheckReserved checks nets for any reserved prefixes, either directly or included within the prefix depending on recurse.
|
||||
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.
|
||||
|
||||
@@ -159,18 +161,68 @@ func AddrInvert(ip netip.Addr) (inverted netip.Addr) {
|
||||
|
||||
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, recurse, excludePrivate bool) (reservations map[netip.Prefix]string, err error) {
|
||||
func CheckReserved(nets []*netip.Prefix, revRecursive, recursive, excludePrivate bool) (reservations map[netip.Prefix]*IANAAddrNetResRecord, err error) {
|
||||
|
||||
// TODO
|
||||
var ok bool
|
||||
var res *IANAAddrNetResRecord
|
||||
var reserved map[netip.Prefix]*IANAAddrNetResRecord
|
||||
|
||||
if nets == nil || len(nets) == 0 {
|
||||
return
|
||||
}
|
||||
|
||||
if _, _, reserved, err = RetrieveReserved(); err != nil {
|
||||
return
|
||||
}
|
||||
for _, n := range nets {
|
||||
if n == nil {
|
||||
continue
|
||||
}
|
||||
if n.Addr().IsPrivate() && excludePrivate {
|
||||
continue
|
||||
}
|
||||
*n = n.Masked()
|
||||
if res, ok = reserved[*n]; ok {
|
||||
if reservations == nil {
|
||||
reservations = make(map[netip.Prefix]*IANAAddrNetResRecord)
|
||||
}
|
||||
reservations[*n] = res
|
||||
if !revRecursive && !recursive {
|
||||
continue
|
||||
}
|
||||
for p, r := range reserved {
|
||||
// This... *should* be safe? I don't think any reservations overlap.
|
||||
// Anyways, revRecursive works because n.Addr() returns the network address, which should be the canonical boundary.
|
||||
// recursive works for the same reason, just the other end.
|
||||
// Math!
|
||||
if revRecursive && p.Contains(n.Addr()) {
|
||||
if reservations == nil {
|
||||
reservations = make(map[netip.Prefix]*IANAAddrNetResRecord)
|
||||
}
|
||||
reservations[p] = r
|
||||
} else if recursive && n.Contains(p.Addr()) {
|
||||
if reservations == nil {
|
||||
reservations = make(map[netip.Prefix]*IANAAddrNetResRecord)
|
||||
}
|
||||
reservations[p] = r
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// Contain takes the results of a NetSplitter and returns a StructuredResults.
|
||||
// Contain takes the results of a NetSplitter and returns a StructuredResults. The reservations are only checked against nets.
|
||||
func Contain(origPfx *netip.Prefix, nets []*netip.Prefix, remaining *netipx.IPSet, splitter NetSplitter) (s *StructuredResults, err error) {
|
||||
|
||||
var idx int
|
||||
var r *IANAAddrNetResRecord
|
||||
var rem []netip.Prefix
|
||||
var reserved map[netip.Prefix]*IANAAddrNetResRecord
|
||||
var sr = StructuredResults{
|
||||
Original: origPfx,
|
||||
}
|
||||
@@ -223,6 +275,18 @@ func Contain(origPfx *netip.Prefix, nets []*netip.Prefix, remaining *netipx.IPSe
|
||||
}
|
||||
}
|
||||
|
||||
if nets != nil {
|
||||
if reserved, err = CheckReserved(nets, true, true, false); err != nil {
|
||||
return
|
||||
}
|
||||
if reserved != nil && len(reserved) > 0 {
|
||||
s.Reservations = make([]*IANAAddrNetResRecord, len(reserved))
|
||||
for idx, r = range reserved {
|
||||
s.Reservations[idx] = r
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
s = &sr
|
||||
|
||||
return
|
||||
|
||||
Reference in New Issue
Block a user