-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathNetwork Detection.go
275 lines (225 loc) · 7.49 KB
/
Network Detection.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
/*
File Username: Network Detection.go
Copyright: 2021 Peernet s.r.o.
Author: Peter Kleissner
*/
package core
import (
"net"
"strings"
"time"
)
// FindInterfaceByIP finds an interface based on the IP. The IP must be available at the interface.
func FindInterfaceByIP(ip net.IP) (iface *net.Interface, ipnet *net.IPNet) {
interfaceList, err := net.Interfaces()
if err != nil {
return nil, nil
}
// iterate through all interfaces
for _, ifaceSingle := range interfaceList {
addresses, err := ifaceSingle.Addrs()
if err != nil {
continue
}
// iterate through all IPs of the interfaces
for _, address := range addresses {
addressIP := address.(*net.IPNet).IP
if addressIP.Equal(ip) {
return &ifaceSingle, address.(*net.IPNet)
}
}
}
return nil, nil
}
// NetworkListIPs returns a list of all IPs
func NetworkListIPs() (IPs []net.IP, err error) {
interfaceList, err := net.Interfaces()
if err != nil {
return nil, err
}
// iterate through all interfaces
for _, ifaceSingle := range interfaceList {
addresses, err := ifaceSingle.Addrs()
if err != nil {
continue
}
// iterate through all IPs of the interfaces
for _, address := range addresses {
addressIP := address.(*net.IPNet).IP
IPs = append(IPs, addressIP)
}
}
return IPs, nil
}
// IsIPv4 checks if an IP address is IPv4
func IsIPv4(IP net.IP) bool {
return IP.To4() != nil
}
// IsIPv6 checks if an IP address is IPv6
func IsIPv6(IP net.IP) bool {
return IP.To4() == nil && IP.To16() != nil
}
// IsNetworkErrorFatal checks if a network error indicates a broken connection.
// Not every network error indicates a broken connection. This function prevents from over-dropping connections.
func IsNetworkErrorFatal(err error) bool {
if err == nil {
return false
}
// Windows: A common error when the network adapter is disabled is "wsasendto: The requested address is not valid in its context".
if strings.Contains(err.Error(), "requested address is not valid in its context") {
return true
}
return false
}
// changeMonitorFrequency is the frequency in seconds to check for a network change
const changeMonitorFrequency = 10
// networkChangeMonitor() monitors for network changes to act accordingly
func (nets *Networks) networkChangeMonitor() {
// If manual IPs are entered, no need for monitoring for any network changes.
if len(nets.backend.Config.Listen) > 0 {
return
}
for {
time.Sleep(time.Second * changeMonitorFrequency)
interfaceList, err := net.Interfaces()
if err != nil {
nets.backend.LogError("networkChangeMonitor", "enumerating network adapters failed: %s\n", err.Error())
continue
}
ifacesNew := make(map[string][]net.Addr)
for _, iface := range interfaceList {
addressesNew, err := iface.Addrs()
if err != nil {
nets.backend.LogError("networkChangeMonitor", "enumerating IPs for network adapter '%s': %s\n", iface.Name, err.Error())
continue
}
ifacesNew[iface.Name] = addressesNew
// was the interface added?
addressesExist, ok := nets.ipListen.ifacesExist[iface.Name]
if !ok {
nets.networkChangeInterfaceNew(iface, addressesNew)
} else {
// new IPs added for this interface?
for _, addr := range addressesNew {
exists := false
for _, exist := range addressesExist {
if exist.String() == addr.String() {
exists = true
break
}
}
if !exists {
nets.networkChangeIPNew(iface, addr)
}
}
// were IPs removed from this interface
for _, exist := range addressesExist {
removed := true
for _, addr := range addressesNew {
if exist.String() == addr.String() {
removed = false
break
}
}
if removed {
nets.networkChangeIPRemove(iface, exist)
}
}
}
}
// was an existing interface removed?
for ifaceExist, addressesExist := range nets.ipListen.ifacesExist {
if _, ok := ifacesNew[ifaceExist]; !ok {
nets.networkChangeInterfaceRemove(ifaceExist, addressesExist)
}
}
nets.ipListen.ifacesExist = ifacesNew
}
}
// networkChangeInterfaceNew is called when a new interface is detected
func (nets *Networks) networkChangeInterfaceNew(iface net.Interface, addresses []net.Addr) {
nets.backend.LogError("networkChangeInterfaceNew", "new interface '%s' (%d IPs)\n", iface.Name, len(addresses))
networksNew := nets.InterfaceStart(iface, addresses)
for _, network := range networksNew {
go network.upnpAuto()
}
go nets.backend.nodesDHT.RefreshBuckets(0)
}
// networkChangeInterfaceRemove is called when an existing interface is removed
func (nets *Networks) networkChangeInterfaceRemove(iface string, addresses []net.Addr) {
nets.RLock()
defer nets.RUnlock()
nets.backend.LogError("networkChangeInterfaceRemove", "removing interface '%s' (%d IPs)\n", iface, len(addresses))
for n, network := range nets.networks6 {
if network.iface != nil && network.iface.Name == iface {
network.Terminate()
// remove from list
networksNew := nets.networks6[:n]
if n < len(nets.networks6)-1 {
networksNew = append(networksNew, nets.networks6[n+1:]...)
}
nets.networks6 = networksNew
}
}
for n, network := range nets.networks4 {
if network.iface != nil && network.iface.Name == iface {
network.Terminate()
// remove from list
networksNew := nets.networks4[:n]
if n < len(nets.networks4)-1 {
networksNew = append(networksNew, nets.networks4[n+1:]...)
}
nets.networks4 = networksNew
}
}
}
// networkChangeIPNew is called when an existing interface lists a new IP
func (nets *Networks) networkChangeIPNew(iface net.Interface, address net.Addr) {
nets.backend.LogError("networkChangeIPNew", "new interface '%s' IP %s\n", iface.Name, address.String())
networksNew := nets.InterfaceStart(iface, []net.Addr{address})
for _, network := range networksNew {
go network.upnpAuto()
}
go nets.backend.nodesDHT.RefreshBuckets(0)
}
// networkChangeIPRemove is called when an existing interface removes an IP
func (nets *Networks) networkChangeIPRemove(iface net.Interface, address net.Addr) {
nets.RLock()
defer nets.RUnlock()
nets.backend.LogError("networkChangeIPRemove", "remove interface '%s' IP %s\n", iface.Name, address.String())
for n, network := range nets.networks6 {
if network.address.IP.Equal(address.(*net.IPNet).IP) {
network.Terminate()
// remove from list
networksNew := nets.networks6[:n]
if n < len(nets.networks6)-1 {
networksNew = append(networksNew, nets.networks6[n+1:]...)
}
nets.networks6 = networksNew
}
}
for n, network := range nets.networks4 {
if network.address.IP.Equal(address.(*net.IPNet).IP) {
network.Terminate()
// remove from list
networksNew := nets.networks4[:n]
if n < len(nets.networks4)-1 {
networksNew = append(networksNew, nets.networks4[n+1:]...)
}
nets.networks4 = networksNew
}
}
}
// IsIPLocal reports whether ip is a private (local) address.
// The identification of private, or local, unicast addresses uses address type
// indentification as defined in RFC 1918 for ip4 and RFC 4193 (fc00::/7) for ip6 with the exception of ip4 directed broadcast addresses.
// Unassigned, reserved, multicast and limited-broadcast addresses are not handled and will return false.
// IPv6 link-local addresses (fe80::/10) are included in this check.
func IsIPLocal(ip net.IP) bool {
if ip4 := ip.To4(); ip4 != nil {
return ip4[0] == 10 || (ip4[0] == 172 && ip4[1]&0xf0 == 16) || (ip4[0] == 192 && ip4[1] == 168)
}
return len(ip) == net.IPv6len &&
(ip[0]&0xfe == 0xfc || // fc00::/7
(ip[0] == 0xfe && ip[1]&0xC0 == 0x80)) // fe80::/10
}