v0.2.4
FIXED: * IPv6 split-nets splitter didn't work. It does not. https://github.com/projectdiscovery/mapcidr/issues/628 Noticed. As such, this library/program has *completely removed* ALL use of the mapcidr library as it cannot be expected to be accurate.
This commit is contained in:
parent
4ab83c9069
commit
c05f9c4d47
@ -77,7 +77,7 @@ type SplitHostArgs struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type SplitSubnetArgs struct {
|
type SplitSubnetArgs struct {
|
||||||
Strict bool `short:"t" long:"strict" description:"If specified, an error will occur if the number of possible equally-sized subnets is not exactly -n/--num-nets."`
|
Strict bool `short:"t" long:"strict" description:"If specified, an error will occur if the number of subnets is not exactly -n/--num-nets."`
|
||||||
NumNets uint `short:"n" long:"num-nets" required:"true" description:"Number of networks." validate:"required"`
|
NumNets uint `short:"n" long:"num-nets" required:"true" description:"Number of networks." validate:"required"`
|
||||||
splitArgs
|
splitArgs
|
||||||
}
|
}
|
||||||
|
@ -5,6 +5,7 @@ import "errors"
|
|||||||
var (
|
var (
|
||||||
ErrBadBoundary error = errors.New("subnet does not align on bit boundary")
|
ErrBadBoundary error = errors.New("subnet does not align on bit boundary")
|
||||||
ErrBadNumHosts error = errors.New("bad number of hosts; cannot split into prefix exactly")
|
ErrBadNumHosts error = errors.New("bad number of hosts; cannot split into prefix exactly")
|
||||||
|
ErrBadNumNets error = errors.New("bad number of nets; cannot split into prefix exactly")
|
||||||
ErrBadPrefix error = errors.New("prefix is invalid")
|
ErrBadPrefix error = errors.New("prefix is invalid")
|
||||||
ErrBadPrefixLen error = errors.New("prefix length exceeds maximum possible for prefix's inet family")
|
ErrBadPrefixLen error = errors.New("prefix length exceeds maximum possible for prefix's inet family")
|
||||||
ErrBadSplitter error = errors.New("invalid or unknown splitter when containing")
|
ErrBadSplitter error = errors.New("invalid or unknown splitter when containing")
|
||||||
|
@ -1,10 +1,9 @@
|
|||||||
package netsplit
|
package netsplit
|
||||||
|
|
||||||
import (
|
import (
|
||||||
`net`
|
`math`
|
||||||
"net/netip"
|
"net/netip"
|
||||||
|
|
||||||
`github.com/projectdiscovery/mapcidr`
|
|
||||||
"go4.org/netipx"
|
"go4.org/netipx"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -22,10 +21,7 @@ func (s *SubnetSplitter) Split() (nets []*netip.Prefix, remaining *netipx.IPSet,
|
|||||||
var ok bool
|
var ok bool
|
||||||
var pfxLen int
|
var pfxLen int
|
||||||
var base netip.Prefix
|
var base netip.Prefix
|
||||||
var sub netip.Prefix
|
var vlsm *VLSMSplitter
|
||||||
var subPtr *netip.Prefix
|
|
||||||
var split []*net.IPNet
|
|
||||||
var ipsb *netipx.IPSetBuilder = new(netipx.IPSetBuilder)
|
|
||||||
|
|
||||||
if s == nil || s.BaseSplitter == nil || s.network == nil || s.NumberSubnets == 0 {
|
if s == nil || s.BaseSplitter == nil || s.network == nil || s.NumberSubnets == 0 {
|
||||||
return
|
return
|
||||||
@ -40,65 +36,48 @@ func (s *SubnetSplitter) Split() (nets []*netip.Prefix, remaining *netipx.IPSet,
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if split, err = mapcidr.SplitIPNetIntoN(s.network, int(s.NumberSubnets)); err != nil {
|
// Previously, this used (github.com/projectdiscovery/mapcidr).SplitIPNetIntoN.
|
||||||
return
|
// It no longer does: https://github.com/projectdiscovery/mapcidr/issues/628
|
||||||
}
|
// I am Noticing.
|
||||||
|
|
||||||
for _, n := range split {
|
// First the number of bits needed is calculated.
|
||||||
if sub, ok = netipx.FromStdIPNet(n); !ok {
|
pfxLen = int(math.Ceil(math.Log2(float64(s.NumberSubnets))))
|
||||||
// We bail early on this error.
|
// And this is then added to the original prefix length to get the new prefix size.
|
||||||
err = &SplitErr{
|
pfxLen = pfxLen + base.Bits()
|
||||||
Wrapped: ErrBadBoundary,
|
// I don't know how this would happen, but it'd be bad if it did.
|
||||||
Nets: nets,
|
if pfxLen < base.Bits() {
|
||||||
Remaining: remaining,
|
err = ErrBigPrefix
|
||||||
LastSubnet: subPtr,
|
|
||||||
RequestedPrefixLen: 0,
|
|
||||||
}
|
|
||||||
err = ErrBadBoundary
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if sub.String() == base.String() {
|
// Likewise.
|
||||||
continue
|
if base.Addr().Is6() {
|
||||||
}
|
ok = pfxLen <= int(maxBitsv6)
|
||||||
if pfxLen == 0 {
|
|
||||||
pfxLen = sub.Bits()
|
|
||||||
if nets == nil {
|
|
||||||
nets = make([]*netip.Prefix, 0)
|
|
||||||
}
|
|
||||||
subPtr = new(netip.Prefix)
|
|
||||||
*subPtr = sub
|
|
||||||
nets = append(nets, subPtr)
|
|
||||||
} else {
|
} else {
|
||||||
if sub.Bits() != pfxLen {
|
ok = pfxLen <= int(maxBitsv4)
|
||||||
if err == nil {
|
|
||||||
// Return this err but don't return early; wait for the populate.
|
|
||||||
err = &SplitErr{
|
|
||||||
Wrapped: ErrNoNetSpace,
|
|
||||||
Nets: nets,
|
|
||||||
Remaining: remaining,
|
|
||||||
LastSubnet: subPtr,
|
|
||||||
RequestedPrefixLen: uint8(pfxLen),
|
|
||||||
}
|
}
|
||||||
}
|
if !ok {
|
||||||
ipsb.AddPrefix(sub)
|
err = ErrBadPrefix
|
||||||
} else {
|
|
||||||
subPtr = new(netip.Prefix)
|
|
||||||
*subPtr = sub
|
|
||||||
nets = append(nets, subPtr)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if remaining, err = ipsb.IPSet(); err != nil {
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(nets) < int(s.NumberSubnets) {
|
// We can now VLSM.
|
||||||
err = &SplitErr{
|
vlsm = &VLSMSplitter{
|
||||||
Wrapped: ErrNoNetSpace,
|
// Ascenting and Explicit are pointless to set as all defined sizes are the same.
|
||||||
Nets: nets,
|
Ascending: false,
|
||||||
Remaining: remaining,
|
Explicit: false,
|
||||||
|
PrefixLengths: make([]uint8, s.NumberSubnets),
|
||||||
|
BaseSplitter: s.BaseSplitter,
|
||||||
}
|
}
|
||||||
|
for i := 0; i < int(s.NumberSubnets); i++ {
|
||||||
|
vlsm.PrefixLengths[i] = uint8(pfxLen)
|
||||||
|
}
|
||||||
|
|
||||||
|
if nets, remaining, err = vlsm.Split(); err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if s.Strict && remaining != nil && remaining.Prefixes() != nil && len(remaining.Prefixes()) > 0 {
|
||||||
|
err = ErrBadNumNets
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user