This repository has been archived by the owner on May 27, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathgather.go
68 lines (63 loc) · 1.45 KB
/
gather.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
package ice
import (
"net"
ct "gortc.io/ice/candidate"
"gortc.io/ice/gather"
)
// Gather via DefaultGatherer.
func Gather() ([]gather.Addr, error) {
return gather.DefaultGatherer.Gather()
}
type systemCandidateGatherer struct {
addr gather.Gatherer
}
func (g systemCandidateGatherer) gatherUDP(opt gathererOptions) ([]*localUDPCandidate, error) {
addrs, err := g.addr.Gather()
if err != nil {
// Failed to gather host addresses.
return nil, err
}
hostAddr, err := HostAddresses(addrs)
if err != nil {
return nil, err
}
var candidates []*localUDPCandidate
for component := 1; component <= opt.Components; component++ {
for _, addr := range hostAddr {
if opt.IPv4Only && addr.IP.To4() == nil {
continue
}
zeroPort := net.UDPAddr{
IP: addr.IP,
Port: 0,
}
l, err := net.ListenPacket("udp", zeroPort.String())
if err != nil {
return nil, err
}
a := l.LocalAddr().(*net.UDPAddr)
c := Candidate{
Base: Addr{
IP: addr.IP,
Port: a.Port,
Proto: ct.UDP,
},
Type: ct.Host,
Addr: Addr{
IP: addr.IP,
Port: a.Port,
Proto: ct.UDP,
},
ComponentID: component,
LocalPreference: addr.LocalPreference,
}
c.Foundation = Foundation(&c, Addr{})
c.Priority = Priority(TypePreference(c.Type), addr.LocalPreference, c.ComponentID)
candidates = append(candidates, &localUDPCandidate{
candidate: c,
conn: l,
})
}
}
return candidates, nil
}