This commit is contained in:
crusader 2017-11-22 14:52:17 +09:00
parent b88826a1cd
commit e20c998949
4 changed files with 60 additions and 14 deletions

View File

@ -31,7 +31,12 @@ type CIDRRanger interface {
Contains(ip net.IP) bool
First() net.IP
Last() net.IP
Range(exclude []net.IP) []net.IP
Range() []net.IP
// (!Contains(startIP) || !Contains(endIP)) return nil
// (startIP > endIP) return nil
// (nil != startIP && nil != endIP) return (startIP ~ endIP) + include - exclude
// (nil == startIP || nil == endIP) return include - exclude
Ranges(startIP net.IP, endIP net.IP, include []net.IP, exclude []net.IP) []net.IP
Broadcast() net.IP
Network() net.IP
Next(ip net.IP) net.IP

View File

@ -24,7 +24,7 @@ func (cr *cidrRangeIPv4) Last() net.IP {
return cr.Previous(bIP)
}
func (cr *cidrRangeIPv4) Range(exclude []net.IP) []net.IP {
func (cr *cidrRangeIPv4) Range() []net.IP {
fIP := cr.First()
if nil == fIP {
return nil
@ -36,23 +36,61 @@ func (cr *cidrRangeIPv4) Range(exclude []net.IP) []net.IP {
fNum := converter.IPv4ToInt(fIP.To4())
lNum := converter.IPv4ToInt(lIP.To4())
exs := make([]int32, 0)
r := make([]net.IP, 0)
for i := fNum; i <= lNum; i++ {
r = append(r, converter.IntToIPv4(i))
}
return r
}
// (!Contains(startIP) || !Contains(endIP)) return nil
// (startIP > endIP) return nil
// (nil != startIP && nil != endIP) return (startIP ~ endIP) + include - exclude
// (nil == startIP || nil == endIP) return include - exclude
func (cr *cidrRangeIPv4) Ranges(startIP net.IP, endIP net.IP, include []net.IP, exclude []net.IP) []net.IP {
res := make(map[int32]bool)
if nil != startIP && nil != endIP {
if !cr.Contains(startIP) {
return nil
}
if !cr.Contains(endIP) {
return nil
}
sNum := converter.IPv4ToInt(startIP.To4())
eNum := converter.IPv4ToInt(endIP.To4())
if sNum > eNum {
return nil
}
for i := sNum; i <= eNum; i++ {
res[i] = true
}
}
if nil != include {
for _, in := range include {
iNum := converter.IPv4ToInt(in.To4())
if _, ok := res[iNum]; !ok {
res[iNum] = true
}
}
}
if nil != exclude {
for _, ex := range exclude {
exs = append(exs, converter.IPv4ToInt(ex.To4()))
iNum := converter.IPv4ToInt(ex.To4())
if _, ok := res[iNum]; ok {
delete(res, iNum)
}
}
}
r := make([]net.IP, 0)
Loop:
for i := fNum; i <= lNum; i++ {
for _, exN := range exs {
if exN == i {
continue Loop
}
}
r = append(r, converter.IntToIPv4(i))
for k, _ := range res {
r = append(r, converter.IntToIPv4(k))
}
return r
}

View File

@ -18,7 +18,11 @@ func (cr *cidrRangeIPv6) Last() net.IP {
return nil
}
func (cr *cidrRangeIPv6) Range(exclude []net.IP) []net.IP {
func (cr *cidrRangeIPv6) Range() []net.IP {
return nil
}
func (cr *cidrRangeIPv6) Ranges(startIP net.IP, endIP net.IP, include []net.IP, exclude []net.IP) []net.IP {
return nil
}

View File

@ -13,5 +13,4 @@ func IntToIPv4(n int32) net.IP {
b := make([]byte, 4)
binary.BigEndian.PutUint32(b, uint32(n))
return net.IP(b)
}