-
Notifications
You must be signed in to change notification settings - Fork 6
/
trie.go
54 lines (44 loc) · 1.15 KB
/
trie.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package main
import (
"github.com/miekg/bitradix"
"log"
"net"
"reflect"
)
func addRoute(r *bitradix.Radix32, ipnet *net.IPNet, route *Route) {
net, mask := ipNetToUint(ipnet)
r.Insert(net, mask, route)
}
func removeRoute(r *bitradix.Radix32, ipnet *net.IPNet) {
net, mask := ipNetToUint(ipnet)
r.Remove(net, mask)
}
func ipNetToUint(n *net.IPNet) (i uint32, mask int) {
i = ipToUint(&n.IP)
mask, _ = n.Mask.Size()
return
}
func ipToUint(nip *net.IP) (i uint32) {
ip := nip.To4()
fv := reflect.ValueOf(&i).Elem()
fv.SetUint(uint64(uint32(ip[0])<<24 | uint32(ip[1])<<16 | uint32(ip[2])<<8 | uint32(ip[+3])))
return
}
func uintToIP(n uint32) net.IP {
return net.IPv4(byte(n>>24), byte(n>>16), byte(n>>8), byte(n))
}
func nodeToIPNet(node *bitradix.Radix32) *net.IPNet {
ip := uintToIP(node.Key())
ipnet := net.IPNet{IP: ip, Mask: net.CIDRMask(node.Bits(), 32)}
return &ipnet
}
func (n *Neighbor) FindNode(ip *net.IP) *bitradix.Radix32 {
log.Println("Looking for ASN for", ip)
i := ipToUint(ip)
node := n.trie.Find(i, 32)
return node
}
func (n *Neighbor) FindAsn(ip *net.IP) ASN {
node := n.FindNode(ip)
return node.Value.(Route).PrimaryASN
}