From 815019f6da170e1a360841518917c2a6b2415a4c Mon Sep 17 00:00:00 2001 From: Darien Raymond Date: Tue, 29 Aug 2017 13:51:09 +0200 Subject: [PATCH] rename IPNet to IPNetTable --- app/router/condition.go | 4 ++-- app/router/config.go | 2 +- common/net/ipnet.go | 16 ++++++++-------- common/net/ipnet_test.go | 18 +++++++++--------- common/net/system.go | 6 ++++++ 5 files changed, 26 insertions(+), 20 deletions(-) diff --git a/app/router/condition.go b/app/router/condition.go index 939c3c9f..0f702a7f 100644 --- a/app/router/condition.go +++ b/app/router/condition.go @@ -180,11 +180,11 @@ func (v *CIDRMatcher) Apply(ctx context.Context) bool { } type IPv4Matcher struct { - ipv4net *v2net.IPNet + ipv4net *v2net.IPNetTable onSource bool } -func NewIPv4Matcher(ipnet *v2net.IPNet, onSource bool) *IPv4Matcher { +func NewIPv4Matcher(ipnet *v2net.IPNetTable, onSource bool) *IPv4Matcher { return &IPv4Matcher{ ipv4net: ipnet, onSource: onSource, diff --git a/app/router/config.go b/app/router/config.go index 77456acb..532f6b25 100644 --- a/app/router/config.go +++ b/app/router/config.go @@ -17,7 +17,7 @@ func (r *Rule) Apply(ctx context.Context) bool { } func cidrToCondition(cidr []*CIDR, source bool) (Condition, error) { - ipv4Net := v2net.NewIPNet() + ipv4Net := v2net.NewIPNetTable() ipv6Cond := NewAnyCondition() hasIpv6 := false diff --git a/common/net/ipnet.go b/common/net/ipnet.go index b0a527a5..530d3b1b 100644 --- a/common/net/ipnet.go +++ b/common/net/ipnet.go @@ -5,17 +5,17 @@ import ( "net" ) -type IPNet struct { +type IPNetTable struct { cache map[uint32]byte } -func NewIPNet() *IPNet { - return &IPNet{ +func NewIPNetTable() *IPNetTable { + return &IPNetTable{ cache: make(map[uint32]byte, 1024), } } -func ipToUint32(ip net.IP) uint32 { +func ipToUint32(ip IP) uint32 { value := uint32(0) for _, b := range []byte(ip) { value <<= 8 @@ -32,7 +32,7 @@ func ipMaskToByte(mask net.IPMask) byte { return value } -func (n *IPNet) Add(ipNet *net.IPNet) { +func (n *IPNetTable) Add(ipNet *net.IPNet) { ipv4 := ipNet.IP.To4() if ipv4 == nil { // For now, we don't support IPv6 @@ -42,7 +42,7 @@ func (n *IPNet) Add(ipNet *net.IPNet) { n.AddIP(ipv4, mask) } -func (n *IPNet) AddIP(ip []byte, mask byte) { +func (n *IPNetTable) AddIP(ip []byte, mask byte) { k := ipToUint32(ip) existing, found := n.cache[k] if !found || existing > mask { @@ -50,7 +50,7 @@ func (n *IPNet) AddIP(ip []byte, mask byte) { } } -func (n *IPNet) Contains(ip net.IP) bool { +func (n *IPNetTable) Contains(ip net.IP) bool { ipv4 := ip.To4() if ipv4 == nil { return false @@ -77,6 +77,6 @@ func (n *IPNet) Contains(ip net.IP) bool { return false } -func (n *IPNet) IsEmpty() bool { +func (n *IPNetTable) IsEmpty() bool { return len(n.cache) == 0 } diff --git a/common/net/ipnet_test.go b/common/net/ipnet_test.go index 9b11cd55..e9261603 100644 --- a/common/net/ipnet_test.go +++ b/common/net/ipnet_test.go @@ -18,7 +18,7 @@ func parseCIDR(str string) *net.IPNet { func TestIPNet(t *testing.T) { assert := assert.On(t) - ipNet := NewIPNet() + ipNet := NewIPNetTable() ipNet.Add(parseCIDR(("0.0.0.0/8"))) ipNet.Add(parseCIDR(("10.0.0.0/8"))) ipNet.Add(parseCIDR(("100.64.0.0/10"))) @@ -32,12 +32,12 @@ func TestIPNet(t *testing.T) { ipNet.Add(parseCIDR(("198.51.100.0/24"))) ipNet.Add(parseCIDR(("203.0.113.0/24"))) ipNet.Add(parseCIDR(("8.8.8.8/32"))) - assert.Bool(ipNet.Contains(net.ParseIP("192.168.1.1"))).IsTrue() - assert.Bool(ipNet.Contains(net.ParseIP("192.0.0.0"))).IsTrue() - assert.Bool(ipNet.Contains(net.ParseIP("192.0.1.0"))).IsFalse() - assert.Bool(ipNet.Contains(net.ParseIP("0.1.0.0"))).IsTrue() - assert.Bool(ipNet.Contains(net.ParseIP("1.0.0.1"))).IsFalse() - assert.Bool(ipNet.Contains(net.ParseIP("8.8.8.7"))).IsFalse() - assert.Bool(ipNet.Contains(net.ParseIP("8.8.8.8"))).IsTrue() - assert.Bool(ipNet.Contains(net.ParseIP("2001:cdba::3257:9652"))).IsFalse() + assert.Bool(ipNet.Contains(ParseIP("192.168.1.1"))).IsTrue() + assert.Bool(ipNet.Contains(ParseIP("192.0.0.0"))).IsTrue() + assert.Bool(ipNet.Contains(ParseIP("192.0.1.0"))).IsFalse() + assert.Bool(ipNet.Contains(ParseIP("0.1.0.0"))).IsTrue() + assert.Bool(ipNet.Contains(ParseIP("1.0.0.1"))).IsFalse() + assert.Bool(ipNet.Contains(ParseIP("8.8.8.7"))).IsFalse() + assert.Bool(ipNet.Contains(ParseIP("8.8.8.8"))).IsTrue() + assert.Bool(ipNet.Contains(ParseIP("2001:cdba::3257:9652"))).IsFalse() } diff --git a/common/net/system.go b/common/net/system.go index 8f0c3432..dd921681 100644 --- a/common/net/system.go +++ b/common/net/system.go @@ -10,6 +10,7 @@ var ListenTCP = net.ListenTCP var ListenUDP = net.ListenUDP var LookupIP = net.LookupIP +var ParseIP = net.ParseIP var SplitHostPort = net.SplitHostPort @@ -25,6 +26,11 @@ type UDPConn = net.UDPConn type UnixConn = net.UnixConn type IP = net.IP +type IPMask = net.IPMask +type IPNet = net.IPNet + +const IPv4len = net.IPv4len +const IPv6len = net.IPv6len type Error = net.Error type AddrError = net.AddrError