2021-11-03 19:54:04 -06:00
|
|
|
package cidr
|
2019-11-19 10:00:20 -07:00
|
|
|
|
|
|
|
import (
|
|
|
|
"net"
|
2021-11-03 19:54:04 -06:00
|
|
|
|
|
|
|
"github.com/slackhq/nebula/iputil"
|
2019-11-19 10:00:20 -07:00
|
|
|
)
|
|
|
|
|
2023-11-02 16:05:08 -06:00
|
|
|
type Node[T any] struct {
|
|
|
|
left *Node[T]
|
|
|
|
right *Node[T]
|
|
|
|
parent *Node[T]
|
|
|
|
hasValue bool
|
|
|
|
value T
|
2019-11-19 10:00:20 -07:00
|
|
|
}
|
|
|
|
|
2023-11-02 16:05:08 -06:00
|
|
|
type entry[T any] struct {
|
2023-05-09 09:36:55 -06:00
|
|
|
CIDR *net.IPNet
|
2023-11-02 16:05:08 -06:00
|
|
|
Value T
|
2023-05-09 09:36:55 -06:00
|
|
|
}
|
|
|
|
|
2023-11-02 16:05:08 -06:00
|
|
|
type Tree4[T any] struct {
|
|
|
|
root *Node[T]
|
|
|
|
list []entry[T]
|
2019-11-19 10:00:20 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
2021-11-03 19:54:04 -06:00
|
|
|
startbit = iputil.VpnIp(0x80000000)
|
2019-11-19 10:00:20 -07:00
|
|
|
)
|
|
|
|
|
2023-11-02 16:05:08 -06:00
|
|
|
func NewTree4[T any]() *Tree4[T] {
|
|
|
|
tree := new(Tree4[T])
|
|
|
|
tree.root = &Node[T]{}
|
|
|
|
tree.list = []entry[T]{}
|
2019-11-19 10:00:20 -07:00
|
|
|
return tree
|
|
|
|
}
|
|
|
|
|
2023-11-02 16:05:08 -06:00
|
|
|
func (tree *Tree4[T]) AddCIDR(cidr *net.IPNet, val T) {
|
2019-11-19 10:00:20 -07:00
|
|
|
bit := startbit
|
|
|
|
node := tree.root
|
|
|
|
next := tree.root
|
|
|
|
|
2021-11-03 19:54:04 -06:00
|
|
|
ip := iputil.Ip2VpnIp(cidr.IP)
|
|
|
|
mask := iputil.Ip2VpnIp(cidr.Mask)
|
2019-11-19 10:00:20 -07: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 {
|
2023-05-09 09:36:55 -06:00
|
|
|
addCIDR := cidr.String()
|
|
|
|
for i, v := range tree.list {
|
|
|
|
if addCIDR == v.CIDR.String() {
|
|
|
|
tree.list = append(tree.list[:i], tree.list[i+1:]...)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-02 16:05:08 -06:00
|
|
|
tree.list = append(tree.list, entry[T]{CIDR: cidr, Value: val})
|
2019-11-19 10:00:20 -07:00
|
|
|
node.value = val
|
2023-11-02 16:05:08 -06:00
|
|
|
node.hasValue = true
|
2019-11-19 10:00:20 -07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Build up the rest of the tree we don't already have
|
|
|
|
for bit&mask != 0 {
|
2023-11-02 16:05:08 -06:00
|
|
|
next = &Node[T]{}
|
2019-11-19 10:00:20 -07: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
|
2023-11-02 16:05:08 -06:00
|
|
|
node.hasValue = true
|
|
|
|
tree.list = append(tree.list, entry[T]{CIDR: cidr, Value: val})
|
2019-11-19 10:00:20 -07:00
|
|
|
}
|
|
|
|
|
2023-05-09 09:36:55 -06:00
|
|
|
// Contains finds the first match, which may be the least specific
|
2023-11-02 16:05:08 -06:00
|
|
|
func (tree *Tree4[T]) Contains(ip iputil.VpnIp) (ok bool, value T) {
|
2019-11-19 10:00:20 -07:00
|
|
|
bit := startbit
|
|
|
|
node := tree.root
|
|
|
|
|
|
|
|
for node != nil {
|
2023-11-02 16:05:08 -06:00
|
|
|
if node.hasValue {
|
|
|
|
return true, node.value
|
2019-11-19 10:00:20 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if ip&bit != 0 {
|
|
|
|
node = node.right
|
|
|
|
} else {
|
|
|
|
node = node.left
|
|
|
|
}
|
|
|
|
|
|
|
|
bit >>= 1
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-11-02 16:05:08 -06:00
|
|
|
return false, value
|
2019-11-19 10:00:20 -07:00
|
|
|
}
|
|
|
|
|
2023-05-09 09:36:55 -06:00
|
|
|
// MostSpecificContains finds the most specific match
|
2023-11-02 16:05:08 -06:00
|
|
|
func (tree *Tree4[T]) MostSpecificContains(ip iputil.VpnIp) (ok bool, value T) {
|
2019-12-09 17:28:58 -07:00
|
|
|
bit := startbit
|
|
|
|
node := tree.root
|
|
|
|
|
|
|
|
for node != nil {
|
2023-11-02 16:05:08 -06:00
|
|
|
if node.hasValue {
|
2019-12-09 17:28:58 -07:00
|
|
|
value = node.value
|
2023-11-02 16:05:08 -06:00
|
|
|
ok = true
|
2019-12-09 17:28:58 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if ip&bit != 0 {
|
|
|
|
node = node.right
|
|
|
|
} else {
|
|
|
|
node = node.left
|
|
|
|
}
|
|
|
|
|
|
|
|
bit >>= 1
|
|
|
|
}
|
|
|
|
|
2023-11-02 16:05:08 -06:00
|
|
|
return ok, value
|
2019-12-09 17:28:58 -07:00
|
|
|
}
|
|
|
|
|
2024-01-23 15:02:10 -07:00
|
|
|
type eachFunc[T any] func(T) bool
|
|
|
|
|
2024-01-29 14:30:52 -07:00
|
|
|
// EachContains will call a function, passing the value, for each entry until the function returns true or the search is complete
|
2024-01-23 15:02:10 -07:00
|
|
|
// The final return value will be true if the provided function returned true
|
|
|
|
func (tree *Tree4[T]) EachContains(ip iputil.VpnIp, each eachFunc[T]) bool {
|
2019-11-19 10:00:20 -07:00
|
|
|
bit := startbit
|
|
|
|
node := tree.root
|
|
|
|
|
|
|
|
for node != nil {
|
2024-01-23 15:02:10 -07:00
|
|
|
if node.hasValue {
|
|
|
|
// If the each func returns true then we can exit the loop
|
|
|
|
if each(node.value) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-19 10:00:20 -07:00
|
|
|
if ip&bit != 0 {
|
|
|
|
node = node.right
|
|
|
|
} else {
|
|
|
|
node = node.left
|
|
|
|
}
|
|
|
|
|
|
|
|
bit >>= 1
|
|
|
|
}
|
|
|
|
|
2024-01-23 15:02:10 -07:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetCIDR returns the entry added by the most recent matching AddCIDR call
|
|
|
|
func (tree *Tree4[T]) GetCIDR(cidr *net.IPNet) (ok bool, value T) {
|
|
|
|
bit := startbit
|
|
|
|
node := tree.root
|
|
|
|
|
|
|
|
ip := iputil.Ip2VpnIp(cidr.IP)
|
|
|
|
mask := iputil.Ip2VpnIp(cidr.Mask)
|
|
|
|
|
|
|
|
// Find our last ancestor in the tree
|
|
|
|
for node != nil && bit&mask != 0 {
|
|
|
|
if ip&bit != 0 {
|
|
|
|
node = node.right
|
|
|
|
} else {
|
|
|
|
node = node.left
|
|
|
|
}
|
|
|
|
|
|
|
|
bit = bit >> 1
|
|
|
|
}
|
|
|
|
|
|
|
|
if bit&mask == 0 && node != nil {
|
|
|
|
value = node.value
|
|
|
|
ok = node.hasValue
|
2019-11-19 10:00:20 -07:00
|
|
|
}
|
2024-01-23 15:02:10 -07:00
|
|
|
|
2023-11-02 16:05:08 -06:00
|
|
|
return ok, value
|
2019-11-19 10:00:20 -07:00
|
|
|
}
|
2023-05-09 09:36:55 -06:00
|
|
|
|
|
|
|
// List will return all CIDRs and their current values. Do not modify the contents!
|
2023-11-02 16:05:08 -06:00
|
|
|
func (tree *Tree4[T]) List() []entry[T] {
|
2023-05-09 09:36:55 -06:00
|
|
|
return tree.list
|
|
|
|
}
|