forked from qmsk/clusterf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
route.go
115 lines (90 loc) · 2.63 KB
/
route.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package clusterf
import (
"github.com/qmsk/clusterf/config"
"fmt"
"github.com/qmsk/clusterf/ipvs"
"net"
)
type Routes map[string]*Route
func makeRoutes() Routes {
return Routes(make(map[string]*Route))
}
func (self Routes) get(name string) *Route {
if route, exists := self[name]; exists {
return route
} else {
route := &Route{
Name: name,
}
self[name] = route
return route
}
}
func (self Routes) del(name string) {
delete(self, name)
}
// Return most-specific matching route for given IPv4/IPv6 IP
func (self Routes) Lookup(ip net.IP) *Route {
var matchRoute *Route
var matchLength int = 0
for _, route := range self {
if match, routeLength := route.match(ip); !match {
} else if matchRoute == nil || routeLength > matchLength {
matchRoute = route
}
}
return matchRoute
}
type Route struct {
Name string
// default -> nil
Prefix4 *net.IPNet
// attributes
Gateway4 net.IP
ipvs_fwdMethod *ipvs.FwdMethod
ipvs_filter bool
}
func (self *Route) config(action config.Action, routeConfig config.Route) error {
if routeConfig.Prefix4 == "" {
self.Prefix4 = nil // default
} else if _, prefix4, err := net.ParseCIDR(routeConfig.Prefix4); err != nil {
return fmt.Errorf("Invalid Prefix4: %s", routeConfig.Prefix4)
} else {
self.Prefix4 = prefix4
}
if routeConfig.Gateway4 == "" {
self.Gateway4 = nil
} else if gateway4 := net.ParseIP(routeConfig.Gateway4).To4(); gateway4 == nil {
return fmt.Errorf("Invalid Gateway4: %s", routeConfig.Gateway4)
} else {
self.Gateway4 = gateway4
}
if routeConfig.IpvsMethod == "" {
self.ipvs_filter = false
self.ipvs_fwdMethod = nil
} else if routeConfig.IpvsMethod == "filter" {
self.ipvs_filter = true
self.ipvs_fwdMethod = nil
} else if fwdMethod, err := ipvs.ParseFwdMethod(routeConfig.IpvsMethod); err != nil {
return err
} else {
self.ipvs_filter = false
self.ipvs_fwdMethod = &fwdMethod
}
return nil
}
// Match given ip within our prefix
// Returns true if matches, with the length of the matching prefix
// Returns false otherwise
func (self *Route) match(ip net.IP) (match bool, length int) {
if ip4 := ip.To4(); ip4 == nil {
} else if self.Prefix4 == nil {
// default match
return true, 0
} else if !self.Prefix4.Contains(ip4) {
} else {
prefixLength, _:= self.Prefix4.Mask.Size()
return true, prefixLength
}
return false, 0
}