forked from pixelbender/go-stun
-
Notifications
You must be signed in to change notification settings - Fork 0
/
candidate.go
59 lines (55 loc) · 1.02 KB
/
candidate.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
package ice
import (
"github.com/pexip/go-stun/stun"
"net"
)
const (
TypeHost = "host"
TypeServerReflexive = "srflx"
TypePeerReflexive = "prflx"
TypeRelay = "relay"
)
type Candidate struct {
Type string
Transport string
Address *stun.Addr
BaseAddr *stun.Addr
Priority uint32
Foundation int
}
func NewLocalCandidates(transports ...string) ([]*Candidate, error) {
ifaces, err := net.Interfaces()
if err != nil {
return nil, err
}
var last error
r := make([]*Candidate, 0, 10)
for _, i := range ifaces {
addrs, err := i.Addrs()
if err != nil {
last = err
continue
}
for _, it := range addrs {
addr, ok := it.(*net.IPNet)
if !ok {
continue
}
ip := addr.IP
if ip == nil || ip.IsLoopback() {
continue
}
for _, t := range transports {
r = append(r, &Candidate{
Type: TypeHost,
Transport: t,
Address: &stun.Addr{IP: ip, Port: 9},
})
}
}
}
if len(r) == 0 && last != nil {
return nil, last
}
return r, nil
}