From e20c9989492f82a4fac2f4b855569e45aa4701a4 Mon Sep 17 00:00:00 2001 From: crusader Date: Wed, 22 Nov 2017 14:52:17 +0900 Subject: [PATCH] ing --- net/cidr/range.go | 7 ++++- net/cidr/range_v4.go | 60 ++++++++++++++++++++++++++++++++++-------- net/cidr/range_v6.go | 6 ++++- net/converter/ip_v4.go | 1 - 4 files changed, 60 insertions(+), 14 deletions(-) diff --git a/net/cidr/range.go b/net/cidr/range.go index 3dbff65..8871555 100644 --- a/net/cidr/range.go +++ b/net/cidr/range.go @@ -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 diff --git a/net/cidr/range_v4.go b/net/cidr/range_v4.go index 1beb18f..bb4528d 100644 --- a/net/cidr/range_v4.go +++ b/net/cidr/range_v4.go @@ -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 } diff --git a/net/cidr/range_v6.go b/net/cidr/range_v6.go index fe5ecfa..f59f9a9 100644 --- a/net/cidr/range_v6.go +++ b/net/cidr/range_v6.go @@ -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 } diff --git a/net/converter/ip_v4.go b/net/converter/ip_v4.go index 88d4efc..694e778 100644 --- a/net/converter/ip_v4.go +++ b/net/converter/ip_v4.go @@ -13,5 +13,4 @@ func IntToIPv4(n int32) net.IP { b := make([]byte, 4) binary.BigEndian.PutUint32(b, uint32(n)) return net.IP(b) - }