Newer
Older
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package routingtable
import (
"sync"
"github.com/bio-routing/bio-rd/net"
"github.com/bio-routing/bio-rd/route"
)
// RoutingTable is a binary trie that stores prefixes and their paths
type RoutingTable struct {
root *node
mu sync.RWMutex
}
// NewRoutingTable creates a new routing table
func NewRoutingTable() *RoutingTable {
return &RoutingTable{}
}
// AddPath adds a path to the routing table
func (rt *RoutingTable) AddPath(pfx net.Prefix, p *route.Path) error {
if rt.root == nil {
rt.root = newNode(pfx, p, pfx.Pfxlen(), false)
return nil
}
rt.root = rt.root.addPath(pfx, p)
return nil
}
// RemovePath removes a path from the trie
func (rt *RoutingTable) RemovePath(pfx net.Prefix, p *route.Path) error {
rt.root.removePath(pfx, p)
return nil
}
// LPM performs a longest prefix match for pfx on lpm
func (rt *RoutingTable) LPM(pfx net.Prefix) (res []*route.Route) {
if rt.root == nil {
return nil
}
rt.root.lpm(pfx, &res)
return res
}
// Get get's the route for pfx from the LPM
func (rt *RoutingTable) Get(pfx net.Prefix) *route.Route {
if rt.root == nil {
return nil
}
res := rt.root.get(pfx)
if res == nil {
return nil
}
return res.route
}
// GetLonger get's prefix pfx and all it's more specifics from the LPM
func (rt *RoutingTable) GetLonger(pfx net.Prefix) (res []*route.Route) {
if rt.root == nil {
return []*route.Route{}
}
return rt.root.get(pfx).dumpPfxs(res)
}
// Dump dumps all routes in table rt into a slice
func (rt *RoutingTable) Dump() []*route.Route {
res := make([]*route.Route, 0)
return rt.root.dump(res)
}