-
Notifications
You must be signed in to change notification settings - Fork 1
/
route_map.go
87 lines (71 loc) · 1.61 KB
/
route_map.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
package main
import (
"encoding/binary"
"fmt"
"io/ioutil"
"net"
"strings"
"sync/atomic"
"unsafe"
"github.com/go-yaml/yaml"
)
type routeMap struct {
d *net.UDPAddr // deafult route
r map[uint32]*net.UDPAddr // routes map
}
func newRouteMap() *routeMap {
return &routeMap{
r: make(map[uint32]*net.UDPAddr),
}
}
func (m *routeMap) Set(agentIP net.IP, collAddr *net.UDPAddr) {
ipUint := binary.BigEndian.Uint32(agentIP)
m.r[ipUint] = collAddr
}
func (m *routeMap) Get(agentIP net.IP) *net.UDPAddr {
ipUint := binary.BigEndian.Uint32(agentIP)
if val, ok := m.r[ipUint]; ok {
return val
}
return m.d
}
var routeMapPointer unsafe.Pointer
func routeMapLookup(agentIP net.IP) *net.UDPAddr {
return (*routeMap)(atomic.LoadPointer(&routeMapPointer)).Get(agentIP)
}
func routeMapReload() error {
data, err := ioutil.ReadFile(flagRouteMapPath)
if err != nil {
return err
}
fileMap := make(map[string]string)
if err := yaml.Unmarshal(data, fileMap); err != nil {
return err
}
l := newRouteMap()
for k, v := range fileMap {
if strings.ToLower(k) == "default" {
collAddr, err := net.ResolveUDPAddr("udp4", v)
if err != nil {
return err
}
l.d = collAddr
continue
}
agentIP := net.ParseIP(k)
if agentIP == nil {
return fmt.Errorf("Cannot parse agent address %s", k)
}
agentIP = agentIP.To4()
if agentIP == nil {
return fmt.Errorf("Only IPv4 agent addresses are supported")
}
collAddr, err := net.ResolveUDPAddr("udp4", v)
if err != nil {
return err
}
l.Set(agentIP, collAddr)
}
atomic.StorePointer(&routeMapPointer, unsafe.Pointer(l))
return nil
}