-
Notifications
You must be signed in to change notification settings - Fork 4
/
netprobe_linux.go
59 lines (54 loc) · 1.22 KB
/
netprobe_linux.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
// +build !windows
package behaviors
import (
"net"
"time"
"github.com/jedisct1/dlog"
"github.com/AZ-X/pique/repique/common"
)
const MaxTimeout = 43200
func NetProbe(address string, ifi *string, timeout int) error {
if len(address) <= 0 || timeout == 0 {
return nil
}
var err error
endpoint, err := common.ResolveEndpoint(address)
if err != nil {
return err
}
retried := false
if timeout < 0 {
timeout = MaxTimeout
} else {
timeout = common.Min(MaxTimeout, timeout)
}
var localAddr net.Addr
if ifi != nil {
for tries := timeout; tries > 0; tries-- {
if localAddr, err = common.GetInferfaceDefaultAddr(*ifi, "udp"); err != nil {
dlog.Debug(err)
time.Sleep(1 * time.Second)
} else {
break
}
}
}
d := &net.Dialer{LocalAddr:localAddr, KeepAlive:-1, FallbackDelay:-1,}
for tries := timeout; tries > 0; tries-- {
pc, err := d.Dial("udp", endpoint.String())
if err != nil {
if !retried {
retried = true
dlog.Notice("network not available yet -- waiting...")
}
dlog.Debug(err)
time.Sleep(1 * time.Second)
continue
}
pc.Close()
dlog.Notice("network connectivity detected")
return nil
}
dlog.Error("timeout while waiting for network connectivity")
return nil
}