-
Notifications
You must be signed in to change notification settings - Fork 34
/
endpoint_broadcast.go
145 lines (123 loc) · 3.2 KB
/
endpoint_broadcast.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
package gomavlib
import (
"fmt"
"net"
"reflect"
"strconv"
"time"
)
// ipByBroadcastIP returns the ip of an interface associated with given broadcast ip
func ipByBroadcastIP(target net.IP) net.IP {
intfs, err := net.Interfaces()
if err != nil {
return nil
}
for _, intf := range intfs {
addrs, err := intf.Addrs()
if err != nil {
continue
}
for _, addr := range addrs {
ipn, ok := addr.(*net.IPNet)
if !ok {
continue
}
ip := ipn.IP.To4()
if ip == nil {
continue
}
broadcastIP := net.IP(make([]byte, 4))
for i := range ip {
broadcastIP[i] = ip[i] | ^ipn.Mask[i]
}
if reflect.DeepEqual(broadcastIP, target) {
return ip
}
}
}
return nil
}
// EndpointUDPBroadcast sets up a endpoint that works with UDP broadcast packets.
type EndpointUDPBroadcast struct {
// the broadcast address to which sending outgoing frames, example: 192.168.5.255:5600
BroadcastAddress string
// (optional) the listening address. if empty, it will be computed
// from the broadcast address.
LocalAddress string
}
type endpointUDPBroadcast struct {
conf EndpointUDPBroadcast
pc net.PacketConn
writeTimeout time.Duration
broadcastAddr net.Addr
terminate chan struct{}
}
func (conf EndpointUDPBroadcast) init(node *Node) (Endpoint, error) {
ipString, port, err := net.SplitHostPort(conf.BroadcastAddress)
if err != nil {
return nil, fmt.Errorf("invalid broadcast address")
}
broadcastIP := net.ParseIP(ipString)
if broadcastIP == nil {
return nil, fmt.Errorf("invalid IP")
}
broadcastIP = broadcastIP.To4()
if broadcastIP == nil {
return nil, fmt.Errorf("invalid IP")
}
if conf.LocalAddress == "" {
localIP := ipByBroadcastIP(broadcastIP)
if localIP == nil {
return nil, fmt.Errorf("cannot find local address associated with given broadcast address")
}
conf.LocalAddress = fmt.Sprintf("%s:%s", localIP, port)
} else {
_, _, err = net.SplitHostPort(conf.LocalAddress)
if err != nil {
return nil, fmt.Errorf("invalid local address")
}
}
pc, err := net.ListenPacket("udp4", conf.LocalAddress)
if err != nil {
return nil, err
}
iport, _ := strconv.Atoi(port)
t := &endpointUDPBroadcast{
conf: conf,
pc: pc,
writeTimeout: node.conf.WriteTimeout,
broadcastAddr: &net.UDPAddr{IP: broadcastIP, Port: iport},
terminate: make(chan struct{}),
}
return t, nil
}
func (t *endpointUDPBroadcast) isEndpoint() {}
func (t *endpointUDPBroadcast) Conf() EndpointConf {
return t.conf
}
func (t *endpointUDPBroadcast) label() string {
return fmt.Sprintf("udp:%s", t.broadcastAddr)
}
func (t *endpointUDPBroadcast) Close() error {
close(t.terminate)
t.pc.Close()
return nil
}
func (t *endpointUDPBroadcast) Read(buf []byte) (int, error) {
// read WITHOUT deadline. Long periods without packets are normal since
// we're not directly connected to someone.
n, _, err := t.pc.ReadFrom(buf)
// wait termination, do not report errors
if err != nil {
<-t.terminate
return 0, errTerminated
}
return n, nil
}
func (t *endpointUDPBroadcast) Write(buf []byte) (int, error) {
err := t.pc.SetWriteDeadline(time.Now().Add(t.writeTimeout))
if err != nil {
return 0, err
}
return t.pc.WriteTo(buf, t.broadcastAddr)
}