diff --git a/net/cidr/range.go b/net/cidr/range.go index 8871555..9d908ee 100644 --- a/net/cidr/range.go +++ b/net/cidr/range.go @@ -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 diff --git a/net/cidr/range_v4.go b/net/cidr/range_v4.go index bb4528d..45f5d42 100644 --- a/net/cidr/range_v4.go +++ b/net/cidr/range_v4.go @@ -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 { diff --git a/net/cidr/range_v6.go b/net/cidr/range_v6.go index f59f9a9..440ad0b 100644 --- a/net/cidr/range_v6.go +++ b/net/cidr/range_v6.go @@ -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 {