This commit is contained in:
crusader 2017-11-22 15:07:33 +09:00
parent e20c998949
commit f1f75535be
3 changed files with 11 additions and 10 deletions

View File

@ -32,11 +32,11 @@ type CIDRRanger interface {
First() net.IP
Last() net.IP
Range() []net.IP
// (!Contains(startIP) || !Contains(endIP)) return nil
// (startIP > endIP) return nil
// (!Contains(startIP) || !Contains(endIP)) return error
// (startIP > endIP) return error
// (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
Ranges(startIP net.IP, endIP net.IP, include []net.IP, exclude []net.IP) ([]net.IP, error)
Broadcast() net.IP
Network() net.IP
Next(ip net.IP) net.IP

View File

@ -1,6 +1,7 @@
package cidr
import (
"fmt"
"net"
"git.loafle.net/commons_go/util/net/converter"
@ -47,21 +48,21 @@ func (cr *cidrRangeIPv4) Range() []net.IP {
// (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 {
func (cr *cidrRangeIPv4) Ranges(startIP net.IP, endIP net.IP, include []net.IP, exclude []net.IP) ([]net.IP, error) {
res := make(map[int32]bool)
if nil != startIP && nil != endIP {
if !cr.Contains(startIP) {
return nil
return nil, fmt.Errorf("CIDR Range: CIDR not contains start ip[%v]", startIP)
}
if !cr.Contains(endIP) {
return nil
return nil, fmt.Errorf("CIDR Range: CIDR not contains end ip[%v]", endIP)
}
sNum := converter.IPv4ToInt(startIP.To4())
eNum := converter.IPv4ToInt(endIP.To4())
if sNum > eNum {
return nil
return nil, fmt.Errorf("CIDR Range: Start IP[%v] must smaller then End IP[%v]", startIP, endIP)
}
for i := sNum; i <= eNum; i++ {
res[i] = true
@ -91,7 +92,7 @@ func (cr *cidrRangeIPv4) Ranges(startIP net.IP, endIP net.IP, include []net.IP,
r = append(r, converter.IntToIPv4(k))
}
return r
return r, nil
}
func (cr *cidrRangeIPv4) Broadcast() net.IP {

View File

@ -22,8 +22,8 @@ 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
func (cr *cidrRangeIPv6) Ranges(startIP net.IP, endIP net.IP, include []net.IP, exclude []net.IP) ([]net.IP, error) {
return nil, nil
}
func (cr *cidrRangeIPv6) Broadcast() net.IP {