-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathmulticast_udp.go
342 lines (289 loc) · 8.28 KB
/
multicast_udp.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
package sonic
import (
"fmt"
"io"
"net"
"sync/atomic"
"syscall"
"github.com/talostrading/sonic/internal"
"github.com/talostrading/sonic/sonicerrors"
"github.com/talostrading/sonic/sonicopts"
)
// SizeofIPMreqSource I would love to do unsafe.SizeOf but for a struct with 3 4-byte arrays, it returns 8 on my Mac.
// It should return 12 :). So we add 4 bytes instead which is enough for the source IP.
const SizeofIPMreqSource = syscall.SizeofIPMreq + 4
// IPMreqSource adds Sourceaddr to net.IPMreq
type IPMreqSource struct {
Multiaddr [4]byte /* in_addr */
Interface [4]byte /* in_addr */
Sourceaddr [4]byte /* in_addr */
}
type MulticastRequestType int
const (
JoinGroup = iota
JoinSourceGroup
LeaveGroup
LeaveSourceGroup
BlockSource
UnblockSource
)
func (r MulticastRequestType) String() string {
switch r {
case JoinGroup:
return "join_group"
case JoinSourceGroup:
return "join_source_group"
case LeaveGroup:
return "leave_group"
case LeaveSourceGroup:
return "leave_source_group"
case BlockSource:
return "block_source"
case UnblockSource:
return "unblock_source"
default:
panic("unknown multicast_request_type")
}
}
func (r MulticastRequestType) ToIPv4() int {
switch r {
case JoinGroup:
return syscall.IP_ADD_MEMBERSHIP
case JoinSourceGroup:
return syscall.IP_ADD_SOURCE_MEMBERSHIP
case LeaveGroup:
return syscall.IP_DROP_MEMBERSHIP
case LeaveSourceGroup:
return syscall.IP_DROP_SOURCE_MEMBERSHIP
case BlockSource:
return syscall.IP_BLOCK_SOURCE
case UnblockSource:
return syscall.IP_UNBLOCK_SOURCE
default:
panic("invalid request")
}
}
func (r MulticastRequestType) ToIPv6() int {
// TODO not sure how IPv6 works, some are not defined
// I think the filtering is most likely done on layer 2 (ethernet) level
switch r {
case JoinGroup:
return syscall.IPV6_JOIN_GROUP
case JoinSourceGroup:
return syscall.IPV6_JOIN_GROUP
case LeaveGroup:
return syscall.IPV6_LEAVE_GROUP
case LeaveSourceGroup:
return syscall.IPV6_LEAVE_GROUP
case BlockSource:
panic("invalid request")
case UnblockSource:
panic("invalid request")
default:
panic("invalid request")
}
}
type udpMulticastClient struct {
ioc *IO
iff *net.Interface
boundIP net.IP
opts []sonicopts.Option
fd int
pd internal.PollData
boundAddr *net.UDPAddr
closed uint32
dispatched int
// from is updated on every read
from *net.UDPAddr
}
var _ UDPMulticastClient = &udpMulticastClient{}
// NewUDPMulticastClient creates a new multicast client bound to the provided interface and IP. For most cases, the boundIP
// should be net.IPv4zero or net.IPv6zero, unless explicit multicast group filtering is needed - in that case, the
// boundIP should be the multicast group IP.
func NewUDPMulticastClient(
ioc *IO,
iff *net.Interface,
boundIP net.IP,
opts ...sonicopts.Option,
) (UDPMulticastClient, error) {
c := &udpMulticastClient{
ioc: ioc,
iff: iff,
boundIP: boundIP,
opts: opts,
fd: -1,
from: &net.UDPAddr{},
}
return c, nil
}
func (c *udpMulticastClient) make(port int) error {
var network, addr string
if c.boundIP.To4() != nil {
network = "udp4"
} else {
network = "udp6"
}
addr = fmt.Sprintf("%s:%d", c.boundIP.String(), port)
fd, boundAddr, err := internal.CreateSocketUDP(network, addr)
if err != nil {
return err
}
if boundAddr == nil {
// logic error
panic("boundAddr should not be nil")
}
var opts []sonicopts.Option
opts = append(opts, c.opts...)
opts = append(opts, sonicopts.BindSocket(boundAddr))
// Allow multiple sockets to listen to the same multicast group.
opts = append(opts, sonicopts.ReusePort(true))
if err := internal.ApplyOpts(fd, opts...); err != nil {
return err
}
c.fd = fd
c.pd = internal.PollData{Fd: fd}
c.boundAddr = boundAddr
return nil
}
func (c *udpMulticastClient) prepareJoin(multicastAddr *net.UDPAddr) error {
if !multicastAddr.IP.IsMulticast() {
return fmt.Errorf("cannot join non-multicast address %s", multicastAddr)
}
if c.boundAddr == nil {
return c.make(multicastAddr.Port)
} else if multicastAddr.Port != c.boundAddr.Port {
return fmt.Errorf("invalid address %s: client must join groups on the same port", multicastAddr)
} else {
return nil
}
}
// Join a multicast group.
//
// A MulticastClient may join multiple groups as long as they are all bound to the same port and the client is not
// bound to a specific group address (i.e. it is instead bound to 0.0.0.0).
//
// The caller must ensure they do not join an already joined group.
func (c *udpMulticastClient) Join(multicastAddr *net.UDPAddr) error {
if err := c.prepareJoin(multicastAddr); err != nil {
return err
}
return makeInterfaceRequest(JoinGroup, c.iff, c.fd, multicastAddr.IP, nil)
}
// JoinSource joins a multicast group, filtering out packets not coming from sourceAddr.
//
// The caller must ensure they do not join an already joined source.
func (c *udpMulticastClient) JoinSource(multicastAddr, sourceAddr *net.UDPAddr) error {
if err := c.prepareJoin(multicastAddr); err != nil {
return err
}
return makeInterfaceRequest(JoinSourceGroup, c.iff, c.fd, multicastAddr.IP, sourceAddr.IP)
}
// Leave a group.
//
// The caller must ensure they do not leave an already left group.
func (c *udpMulticastClient) Leave(multicastAddr *net.UDPAddr) error {
return makeInterfaceRequest(LeaveGroup, c.iff, c.fd, multicastAddr.IP, nil)
}
// LeaveSource ...
//
// The caller must ensure they do not leave an already left source from the group.
func (c *udpMulticastClient) LeaveSource(multicastAddr, sourceAddr *net.UDPAddr) error {
return makeInterfaceRequest(LeaveSourceGroup, c.iff, c.fd, multicastAddr.IP, sourceAddr.IP)
}
// BlockSource ...
//
// The caller must ensure they do not block an already blocked source.
func (c *udpMulticastClient) BlockSource(multicastAddr, sourceAddr *net.UDPAddr) error {
return makeInterfaceRequest(BlockSource, c.iff, c.fd, multicastAddr.IP, sourceAddr.IP)
}
// UnblockSource ...
//
// The caller must ensure they do not unblock an already unblocked source.
func (c *udpMulticastClient) UnblockSource(multicastAddr, sourceAddr *net.UDPAddr) error {
return makeInterfaceRequest(UnblockSource, c.iff, c.fd, multicastAddr.IP, sourceAddr.IP)
}
func (c *udpMulticastClient) ReadFrom(b []byte) (n int, from net.Addr, err error) {
var addr syscall.Sockaddr
n, addr, err = syscall.Recvfrom(c.fd, b, 0)
if err != nil {
if err == syscall.EWOULDBLOCK || err == syscall.EAGAIN {
return 0, nil, sonicerrors.ErrWouldBlock
}
return 0, nil, err
}
if n == 0 {
return 0, from, io.EOF
}
if n < 0 {
n = 0 // error contains the information
}
return n, internal.FromSockaddrUDP(addr, c.from), err
}
func (c *udpMulticastClient) AsyncReadFrom(b []byte, cb AsyncReadCallbackPacket) {
if c.dispatched < MaxCallbackDispatch {
c.asyncReadNow(b, func(err error, n int, addr net.Addr) {
c.dispatched++
cb(err, n, addr)
c.dispatched--
})
} else {
c.scheduleRead(b, cb)
}
}
func (c *udpMulticastClient) asyncReadNow(b []byte, cb AsyncReadCallbackPacket) {
n, addr, err := c.ReadFrom(b)
if err == nil {
cb(err, n, addr)
return
}
if err == sonicerrors.ErrWouldBlock {
c.scheduleRead(b, cb)
} else {
cb(err, 0, addr)
}
}
func (c *udpMulticastClient) scheduleRead(b []byte, cb AsyncReadCallbackPacket) {
if c.Closed() {
cb(io.EOF, 0, nil)
return
}
handler := c.getReadHandler(b, cb)
c.pd.Set(internal.ReadEvent, handler)
if err := c.setRead(); err != nil {
cb(err, 0, nil)
} else {
c.ioc.pendingReads[&c.pd] = struct{}{}
}
}
func (c *udpMulticastClient) getReadHandler(b []byte, cb AsyncReadCallbackPacket) internal.Handler {
return func(err error) {
delete(c.ioc.pendingReads, &c.pd)
if err != nil {
cb(err, 0, nil)
} else {
c.asyncReadNow(b, cb)
}
}
}
func (c *udpMulticastClient) setRead() error {
return c.ioc.poller.SetRead(c.fd, &c.pd)
}
func (c *udpMulticastClient) RawFd() int {
return c.fd
}
func (c *udpMulticastClient) Interface() *net.Interface {
return c.iff
}
func (c *udpMulticastClient) LocalAddr() *net.UDPAddr {
return c.boundAddr
}
func (c *udpMulticastClient) Close() error {
if atomic.CompareAndSwapUint32(&c.closed, 0, 1) {
// TODO maybe shutdown instead? on TCP it is cleaner. Also check TCP then.
return syscall.Close(c.fd)
}
return nil
}
func (c *udpMulticastClient) Closed() bool {
return atomic.LoadUint32(&c.closed) == 1
}