This repository has been archived by the owner on Aug 2, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
endpoint_broadcast.go
131 lines (113 loc) · 3.19 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
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 {
if intfs, err := net.Interfaces(); err == nil {
for _, intf := range intfs {
if addrs, err := intf.Addrs(); err == nil {
for _, addr := range addrs {
if ipn, ok := addr.(*net.IPNet); ok {
if ip := ipn.IP.To4(); ip != nil {
broadcastIp := net.IP(make([]byte, 4))
for i := range ip {
broadcastIp[i] = ip[i] | ^ipn.Mask[i]
}
if reflect.DeepEqual(broadcastIp, target) == true {
return ip
}
}
}
}
}
}
}
return nil
}
// EndpointUdpBroadcast sets up a endpoint that works through 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
packetConn net.PacketConn
broadcastAddr net.Addr
terminate chan struct{}
}
func (conf EndpointUdpBroadcast) init() (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")
}
}
packetConn, err := net.ListenPacket("udp4", conf.LocalAddress)
if err != nil {
return nil, err
}
iport, _ := strconv.Atoi(port)
t := &endpointUdpBroadcast{
conf: conf,
packetConn: packetConn,
broadcastAddr: &net.UDPAddr{IP: broadcastIp, Port: iport},
terminate: make(chan struct{}, 1),
}
return t, nil
}
func (t *endpointUdpBroadcast) isEndpoint() {}
func (t *endpointUdpBroadcast) Conf() interface{} {
return t.conf
}
func (t *endpointUdpBroadcast) Label() string {
return fmt.Sprintf("udp:%s", t.broadcastAddr)
}
func (t *endpointUdpBroadcast) Close() error {
t.terminate <- struct{}{}
t.packetConn.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.packetConn.ReadFrom(buf)
// wait termination, do not report errors
if err != nil {
<-t.terminate
return 0, errorTerminated
}
return n, nil
}
func (t *endpointUdpBroadcast) Write(buf []byte) (int, error) {
err := t.packetConn.SetWriteDeadline(time.Now().Add(_NET_WRITE_TIMEOUT))
if err != nil {
return 0, err
}
return t.packetConn.WriteTo(buf, t.broadcastAddr)
}