mirror of
https://github.com/weaveworks/scope.git
synced 2026-02-14 10:00:13 +00:00
Faster path to check an IP address against known networks
We modify the critbitgo library to skip creating a route object we don't use. The weaveworks-local modification can be removed if https://github.com/k-sone/critbitgo/pull/7 is merged.
This commit is contained in:
@@ -34,8 +34,8 @@ func (n Networks) AddCIDR(cidr string) error {
|
||||
|
||||
// Contains returns true if IP is in Networks.
|
||||
func (n Networks) Contains(ip net.IP) bool {
|
||||
network, _, _ := n.MatchIP(ip)
|
||||
return network != nil
|
||||
contained, _ := n.ContainedIP(ip)
|
||||
return contained
|
||||
}
|
||||
|
||||
// LocalAddresses returns a list of the local IP addresses.
|
||||
|
||||
21
vendor/github.com/k-sone/critbitgo/net.go
generated
vendored
21
vendor/github.com/k-sone/critbitgo/net.go
generated
vendored
@@ -97,9 +97,25 @@ func (n *Net) MatchCIDR(s string) (route *net.IPNet, value interface{}, err erro
|
||||
return
|
||||
}
|
||||
|
||||
// Return a bool indicating whether a route would be found
|
||||
func (n *Net) ContainedIP(ip net.IP) (contained bool, err error) {
|
||||
k, _, err := n.matchIP(ip)
|
||||
contained = k != nil
|
||||
return
|
||||
}
|
||||
|
||||
// Return a specific route by using the longest prefix matching.
|
||||
// If `ip` is invalid IP, or a route is not found, `route` is nil.
|
||||
func (n *Net) MatchIP(ip net.IP) (route *net.IPNet, value interface{}, err error) {
|
||||
k, v, err := n.matchIP(ip)
|
||||
if k != nil {
|
||||
route = netKeyToIPNet(k)
|
||||
value = v
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (n *Net) matchIP(ip net.IP) (k []byte, v interface{}, err error) {
|
||||
var isV4 bool
|
||||
ip, isV4, err = netValidateIP(ip)
|
||||
if err != nil {
|
||||
@@ -111,10 +127,7 @@ func (n *Net) MatchIP(ip net.IP) (route *net.IPNet, value interface{}, err error
|
||||
} else {
|
||||
mask = mask128
|
||||
}
|
||||
if k, v := n.match(netIPNetToKey(ip, mask)); k != nil {
|
||||
route = netKeyToIPNet(k)
|
||||
value = v
|
||||
}
|
||||
k, v = n.match(netIPNetToKey(ip, mask))
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
6
vendor/manifest
vendored
6
vendor/manifest
vendored
@@ -849,10 +849,10 @@
|
||||
},
|
||||
{
|
||||
"importpath": "github.com/k-sone/critbitgo",
|
||||
"repository": "https://github.com/k-sone/critbitgo",
|
||||
"repository": "https://github.com/weaveworks/critbitgo",
|
||||
"vcs": "git",
|
||||
"revision": "327359a051d71948cb756142d112c7df283fac4d",
|
||||
"branch": "master",
|
||||
"revision": "aa814e9447366a571819e37affacbdc60e3af218",
|
||||
"branch": "contained",
|
||||
"notests": true
|
||||
},
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user