-
Notifications
You must be signed in to change notification settings - Fork 1
/
routingtable.go
209 lines (181 loc) · 4.87 KB
/
routingtable.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
package main
import (
"encoding/binary"
"log"
"math/rand"
"net"
"sync"
"time"
)
type RoutingRule struct {
Distance uint8
NextHop *net.UDPAddr
Time time.Time
Sequence uint16
Public bool
}
type RoutingTable struct {
routes map[string][]*RoutingRule
virtualIP net.IP
ifaceName string
mutex *sync.RWMutex
sequence uint16
broadcaster *Broadcaster
}
func NewRoutingTable(ip net.IP, ifaceName string, broadcaster *Broadcaster) *RoutingTable {
rt := &RoutingTable{make(map[string][]*RoutingRule), ip, ifaceName, &sync.RWMutex{}, 0, broadcaster}
go func() {
for {
rt.AnnounceAll()
time.Sleep(time.Second / 15)
time.Sleep(time.Millisecond * time.Duration(rand.Intn(100))) // avoid jitter
}
}()
go rt.deleteOldRoutesJob()
if *debug_showRoutes {
go func() {
for {
rt.printRoutes()
}
}()
}
return rt
}
func (rt *RoutingTable) printRoutes() {
rt.mutex.RLock()
log.Printf("+----------+-----+-------+---+----------------------------+--------+")
log.Printf("| dest | hop | seq | p | next hop | iface |")
log.Printf("+----------+-----+-------+---+----------------------------+--------+")
for ip, rules := range rt.routes {
if len(rules) > 0 {
for i, rule := range rules {
pub := ' '
if rule.Public {
pub = 'X'
}
if i == 0 {
log.Printf("| %8s | %3d | %5d | %c | %26s |%7s |", net.IP(ip).String(), rule.Distance, rule.Sequence, pub, rule.NextHop.IP.String(), rule.NextHop.Zone)
} else {
log.Printf("| | %3d | %5d | %c | %26s |%7s |", rule.Distance, rule.Sequence, pub, rule.NextHop.IP.String(), rule.NextHop.Zone)
}
}
log.Printf("+----------+-----+-------+---+----------------------------+--------+")
}
}
rt.mutex.RUnlock()
log.Printf("\n")
time.Sleep(time.Second / 2)
}
func (rt *RoutingTable) NextHop(ip net.IP) *net.UDPAddr {
rt.mutex.RLock()
var nextHop *net.UDPAddr
nextHop = nil
routingRules := rt.routes[string(ip)]
if routingRules != nil && len(routingRules) > 0 {
nextHop = routingRules[0].NextHop
bestPath := byte(255)
for _, rule := range routingRules {
if rule.Distance < bestPath {
bestPath = rule.Distance
nextHop = rule.NextHop
}
}
}
if nextHop == nil {
rt.mutex.RUnlock()
return nil
} else {
result := &net.UDPAddr{}
*result = *nextHop
rt.mutex.RUnlock()
return result
}
}
func (rt *RoutingTable) SetRule(distance uint8, nextHop *net.UDPAddr, vIP net.IP, sequence uint16) {
rt.mutex.Lock()
if rt.routes[string(vIP)] == nil {
rules := make([]*RoutingRule, 1)
rules[0] = &RoutingRule{distance, nextHop, time.Now(), sequence, true}
rt.routes[string(vIP)] = rules
msg := make([]byte, 50)
msg[1] = distance
copy(msg[2:18], vIP)
copy(msg[18:34], rt.virtualIP)
binary.LittleEndian.PutUint16(msg[34:50], sequence)
rt.broadcaster.Broadcast(msg)
parseCommand("ip route add " + vIP.String() + " dev " + rt.ifaceName)
} else {
add := true
for _, rule := range rt.routes[string(vIP)] {
if rule.NextHop.IP.Equal(nextHop.IP) {
add = false
rule.Public = (rule.Sequence <= uint16(65515) && sequence > rule.Sequence) || ((rule.Sequence > uint16(65515) && sequence < uint16(20)) || sequence > rule.Sequence) || (sequence == rule.Sequence && distance < rule.Distance)
rule.Distance = distance
rule.Time = time.Now()
rule.Sequence = sequence
break
}
}
if add {
rt.routes[string(vIP)] = append(rt.routes[string(vIP)], &RoutingRule{distance, nextHop, time.Now(), sequence, true})
}
}
rt.mutex.Unlock()
}
func (rt *RoutingTable) AnnounceAll() {
// announce myself
msg := make([]byte, 50)
copy(msg[2:18], rt.virtualIP)
copy(msg[18:34], rt.virtualIP)
binary.LittleEndian.PutUint16(msg[34:50], rt.sequence)
rt.sequence++
rt.broadcaster.Broadcast(msg)
// announce my other routes
msg2 := make([]byte, 50)
copy(msg2[18:34], rt.virtualIP)
rt.mutex.RLock()
for ip, rules := range rt.routes {
copy(msg2[2:18], net.IP(ip))
bestPath := byte(255)
var bestIndex int
for i, rule := range rules {
if rule.Distance < bestPath {
bestPath = rule.Distance
bestIndex = i
}
}
if bestPath < 255 && rules[bestIndex].Public {
msg2[1] = bestPath
binary.LittleEndian.PutUint16(msg2[34:50], rules[bestIndex].Sequence)
rt.broadcaster.Broadcast(msg2)
}
}
rt.mutex.RUnlock()
}
func (rt *RoutingTable) deleteOldRoutesJob() {
pause := true
for {
if pause {
rt.mutex.Lock()
}
pause = true
for ip, rules := range rt.routes {
for i, rule := range rules {
if time.Now().After(rule.Time.Add(3 * time.Second / 15)) {
if len(rt.routes[ip]) == 1 {
parseCommand("ip route del " + net.IP(ip).String() + " dev " + rt.ifaceName)
delete(rt.routes, ip)
} else {
rt.routes[ip] = append(rt.routes[ip][:i], rt.routes[ip][i+1:]...)
}
pause = false
break
}
}
}
if pause {
rt.mutex.Unlock()
time.Sleep(time.Second / 5)
}
}
}