-
Notifications
You must be signed in to change notification settings - Fork 1
/
address_helper.go
177 lines (162 loc) · 4.28 KB
/
address_helper.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
package minq
import (
"github.com/boisjacques/golang-utils"
"log"
"net"
"strings"
"sync"
"time"
)
type AddressHelper struct {
ipAddresses map[string]*net.UDPAddr
ipAddressesBool map[string]bool
sockets map[string]*net.UDPConn
listeners []chan string
lockAddresses sync.RWMutex
lockAddressesBool sync.RWMutex
lockSockets sync.RWMutex
}
func NewAddressHelper() *AddressHelper {
ah := AddressHelper{
make(map[string]*net.UDPAddr),
make(map[string]bool),
make(map[string]*net.UDPConn),
make([]chan string, 0),
sync.RWMutex{},
sync.RWMutex{},
sync.RWMutex{},
}
ah.GatherAddresses()
return &ah
}
func (a *AddressHelper) Subscribe(c chan string) {
a.listeners = append(a.listeners, c)
}
func (a *AddressHelper) Publish(msg string) {
if len(a.listeners) > 0 {
for _, c := range a.listeners {
c <- msg
}
}
}
func (a *AddressHelper) GatherAddresses() {
a.falsifyAddresses()
interfaces, _ := net.Interfaces()
for _, iface := range interfaces {
flags := iface.Flags.String()
if !strings.Contains(flags, "loopback") {
addrs, _ := iface.Addrs()
for _, addr := range addrs {
//TODO: Fix link local addresses, add 169.254.0.0/16
if !isLinkLocal(addr.String()) {
if strings.Contains(addr.String(), ":") {
}
arr := strings.Split(addr.String(), "/")
if strings.Contains(arr[0], ":") {
arr[0] = "[" + arr[0] + "]"
}
udpAddr, err := net.ResolveUDPAddr("udp", arr[0]+":4433")
if err != nil {
log.Println(err)
} else {
if a.containsAddress(udpAddr) {
a.write(udpAddr, true)
}
if !a.containsAddress(udpAddr) {
a.write(udpAddr, true)
a.Publish(udpAddr.String())
}
}
}
}
}
}
a.cleanUp()
}
func (a *AddressHelper) openSocket(local *net.UDPAddr) (*net.UDPConn, error) {
a.lockSockets.Lock()
log.Println("locked", util.Tracer())
defer a.lockSockets.Unlock()
defer log.Println("unlocked", util.Tracer())
var err error = nil
usock, contains := a.sockets[local.String()]
if !contains {
usock, err = net.ListenUDP("udp", local)
a.sockets[local.String()] = usock
}
return usock, err
}
func (a *AddressHelper) cleanUp() {
a.lockAddresses.Lock()
a.lockAddressesBool.Lock()
log.Println("locked: ", util.Tracer())
defer a.lockAddresses.Unlock()
defer a.lockAddressesBool.Unlock()
defer log.Println("unlocked: ", util.Tracer())
for key, value := range a.ipAddressesBool {
if value == false {
a.Publish(key)
time.Sleep(100 * time.Millisecond) //Wait 100 ms for handling in scheduler
delete(a.ipAddresses, key)
a.sockets[key].Close()
}
}
}
func (a *AddressHelper) GetAddresses() *map[string]*net.UDPAddr {
a.lockAddresses.RLock()
log.Println("locked: ", util.Tracer())
defer a.lockAddresses.RUnlock()
defer log.Println("unlocked: ", util.Tracer())
return &a.ipAddresses
}
func (a *AddressHelper) write(addr *net.UDPAddr, bool bool) {
a.lockAddresses.Lock()
a.lockAddressesBool.Lock()
log.Println("locked: ", util.Tracer())
defer a.lockAddressesBool.Unlock()
defer a.lockAddresses.Unlock()
defer log.Println("unlocked: ", util.Tracer())
a.ipAddresses[addr.String()] = addr
a.ipAddressesBool[addr.String()] = bool
}
func (a *AddressHelper) containsAddress(addr *net.UDPAddr) bool {
a.lockAddresses.RLock()
log.Println("locked: ", util.Tracer())
defer a.lockAddresses.RUnlock()
defer log.Println("unlocked: ", util.Tracer())
_, contains := a.ipAddresses[addr.String()]
return contains
}
func (a *AddressHelper) containsSocket(addr *net.UDPAddr) bool {
a.lockSockets.RLock()
log.Println("locked: ", util.Tracer())
defer a.lockSockets.RUnlock()
defer log.Println("unlocked: ", util.Tracer())
_, contains := a.sockets[addr.String()]
return contains
}
func (a *AddressHelper) falsifyAddresses() {
a.lockAddresses.Lock()
a.lockAddressesBool.Lock()
log.Println("locked: ", util.Tracer())
defer a.lockAddresses.Unlock()
defer a.lockAddressesBool.Unlock()
defer log.Println("unlocked: ", util.Tracer())
for address := range a.ipAddresses {
a.ipAddressesBool[address] = false
}
}
func isLinkLocal(addr string) bool {
three := addr[0:3]
four :=addr[0:4]
seven := addr[0:7]
if three == "127" {
return true
} else if four == "fe80" {
return true
} else if seven == "169.254" {
return true
} else {
return false
}
}