2021-11-04 02:54:04 +01:00
|
|
|
package cidr
|
2019-11-19 18:00:20 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"net"
|
2021-11-04 02:54:04 +01:00
|
|
|
|
|
|
|
"github.com/slackhq/nebula/iputil"
|
2019-11-19 18:00:20 +01:00
|
|
|
)
|
|
|
|
|
2021-11-04 02:54:04 +01:00
|
|
|
type Node struct {
|
|
|
|
left *Node
|
|
|
|
right *Node
|
|
|
|
parent *Node
|
2019-11-19 18:00:20 +01:00
|
|
|
value interface{}
|
|
|
|
}
|
|
|
|
|
2021-11-04 02:54:04 +01:00
|
|
|
type Tree4 struct {
|
|
|
|
root *Node
|
2019-11-19 18:00:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
2021-11-04 02:54:04 +01:00
|
|
|
startbit = iputil.VpnIp(0x80000000)
|
2019-11-19 18:00:20 +01:00
|
|
|
)
|
|
|
|
|
2021-11-04 02:54:04 +01:00
|
|
|
func NewTree4() *Tree4 {
|
|
|
|
tree := new(Tree4)
|
|
|
|
tree.root = &Node{}
|
2019-11-19 18:00:20 +01:00
|
|
|
return tree
|
|
|
|
}
|
|
|
|
|
2021-11-04 02:54:04 +01:00
|
|
|
func (tree *Tree4) AddCIDR(cidr *net.IPNet, val interface{}) {
|
2019-11-19 18:00:20 +01:00
|
|
|
bit := startbit
|
|
|
|
node := tree.root
|
|
|
|
next := tree.root
|
|
|
|
|
2021-11-04 02:54:04 +01:00
|
|
|
ip := iputil.Ip2VpnIp(cidr.IP)
|
|
|
|
mask := iputil.Ip2VpnIp(cidr.Mask)
|
2019-11-19 18:00:20 +01:00
|
|
|
|
|
|
|
// Find our last ancestor in the tree
|
|
|
|
for bit&mask != 0 {
|
|
|
|
if ip&bit != 0 {
|
|
|
|
next = node.right
|
|
|
|
} else {
|
|
|
|
next = node.left
|
|
|
|
}
|
|
|
|
|
|
|
|
if next == nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
bit = bit >> 1
|
|
|
|
node = next
|
|
|
|
}
|
|
|
|
|
|
|
|
// We already have this range so update the value
|
|
|
|
if next != nil {
|
|
|
|
node.value = val
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Build up the rest of the tree we don't already have
|
|
|
|
for bit&mask != 0 {
|
2021-11-04 02:54:04 +01:00
|
|
|
next = &Node{}
|
2019-11-19 18:00:20 +01:00
|
|
|
next.parent = node
|
|
|
|
|
|
|
|
if ip&bit != 0 {
|
|
|
|
node.right = next
|
|
|
|
} else {
|
|
|
|
node.left = next
|
|
|
|
}
|
|
|
|
|
|
|
|
bit >>= 1
|
|
|
|
node = next
|
|
|
|
}
|
|
|
|
|
|
|
|
// Final node marks our cidr, set the value
|
|
|
|
node.value = val
|
|
|
|
}
|
|
|
|
|
2021-03-19 02:37:24 +01:00
|
|
|
// Finds the first match, which may be the least specific
|
2021-11-04 02:54:04 +01:00
|
|
|
func (tree *Tree4) Contains(ip iputil.VpnIp) (value interface{}) {
|
2019-11-19 18:00:20 +01:00
|
|
|
bit := startbit
|
|
|
|
node := tree.root
|
|
|
|
|
|
|
|
for node != nil {
|
|
|
|
if node.value != nil {
|
|
|
|
return node.value
|
|
|
|
}
|
|
|
|
|
|
|
|
if ip&bit != 0 {
|
|
|
|
node = node.right
|
|
|
|
} else {
|
|
|
|
node = node.left
|
|
|
|
}
|
|
|
|
|
|
|
|
bit >>= 1
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return value
|
|
|
|
}
|
|
|
|
|
2019-12-10 01:28:58 +01:00
|
|
|
// Finds the most specific match
|
2021-11-04 02:54:04 +01:00
|
|
|
func (tree *Tree4) MostSpecificContains(ip iputil.VpnIp) (value interface{}) {
|
2019-12-10 01:28:58 +01:00
|
|
|
bit := startbit
|
|
|
|
node := tree.root
|
|
|
|
|
|
|
|
for node != nil {
|
|
|
|
if node.value != nil {
|
|
|
|
value = node.value
|
|
|
|
}
|
|
|
|
|
|
|
|
if ip&bit != 0 {
|
|
|
|
node = node.right
|
|
|
|
} else {
|
|
|
|
node = node.left
|
|
|
|
}
|
|
|
|
|
|
|
|
bit >>= 1
|
|
|
|
}
|
|
|
|
|
|
|
|
return value
|
|
|
|
}
|
|
|
|
|
2019-11-19 18:00:20 +01:00
|
|
|
// Finds the most specific match
|
2021-11-04 02:54:04 +01:00
|
|
|
func (tree *Tree4) Match(ip iputil.VpnIp) (value interface{}) {
|
2019-11-19 18:00:20 +01:00
|
|
|
bit := startbit
|
|
|
|
node := tree.root
|
|
|
|
lastNode := node
|
|
|
|
|
|
|
|
for node != nil {
|
|
|
|
lastNode = node
|
|
|
|
if ip&bit != 0 {
|
|
|
|
node = node.right
|
|
|
|
} else {
|
|
|
|
node = node.left
|
|
|
|
}
|
|
|
|
|
|
|
|
bit >>= 1
|
|
|
|
}
|
|
|
|
|
|
|
|
if bit == 0 && lastNode != nil {
|
|
|
|
value = lastNode.value
|
|
|
|
}
|
|
|
|
return value
|
|
|
|
}
|