-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
multicast.go
165 lines (141 loc) · 3.07 KB
/
multicast.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
// Copyright (C) 2014 The Syncthing Authors.
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
// You can obtain one at https://mozilla.org/MPL/2.0/.
package beacon
import (
"context"
"errors"
"net"
"time"
"golang.org/x/net/ipv6"
)
func NewMulticast(addr string) Interface {
c := newCast("multicastBeacon")
c.addReader(func(ctx context.Context) error {
return readMulticasts(ctx, c.outbox, addr)
})
c.addWriter(func(ctx context.Context) error {
return writeMulticasts(ctx, c.inbox, addr)
})
return c
}
func writeMulticasts(ctx context.Context, inbox <-chan []byte, addr string) error {
gaddr, err := net.ResolveUDPAddr("udp6", addr)
if err != nil {
l.Debugln(err)
return err
}
conn, err := net.ListenPacket("udp6", ":0")
if err != nil {
l.Debugln(err)
return err
}
doneCtx, cancel := context.WithCancel(ctx)
defer cancel()
go func() {
<-doneCtx.Done()
conn.Close()
}()
pconn := ipv6.NewPacketConn(conn)
wcm := &ipv6.ControlMessage{
HopLimit: 1,
}
for {
var bs []byte
select {
case bs = <-inbox:
case <-doneCtx.Done():
return doneCtx.Err()
}
intfs, err := net.Interfaces()
if err != nil {
l.Debugln(err)
return err
}
success := 0
for _, intf := range intfs {
if intf.Flags&net.FlagMulticast == 0 {
continue
}
wcm.IfIndex = intf.Index
pconn.SetWriteDeadline(time.Now().Add(time.Second))
_, err = pconn.WriteTo(bs, wcm, gaddr)
pconn.SetWriteDeadline(time.Time{})
if err != nil {
l.Debugln(err, "on write to", gaddr, intf.Name)
continue
}
l.Debugf("sent %d bytes to %v on %s", len(bs), gaddr, intf.Name)
success++
select {
case <-doneCtx.Done():
return doneCtx.Err()
default:
}
}
if success == 0 {
return err
}
}
}
func readMulticasts(ctx context.Context, outbox chan<- recv, addr string) error {
gaddr, err := net.ResolveUDPAddr("udp6", addr)
if err != nil {
l.Debugln(err)
return err
}
conn, err := net.ListenPacket("udp6", addr)
if err != nil {
l.Debugln(err)
return err
}
doneCtx, cancel := context.WithCancel(ctx)
defer cancel()
go func() {
<-doneCtx.Done()
conn.Close()
}()
intfs, err := net.Interfaces()
if err != nil {
l.Debugln(err)
return err
}
pconn := ipv6.NewPacketConn(conn)
joined := 0
for _, intf := range intfs {
err := pconn.JoinGroup(&intf, &net.UDPAddr{IP: gaddr.IP})
if err != nil {
l.Debugln("IPv6 join", intf.Name, "failed:", err)
} else {
l.Debugln("IPv6 join", intf.Name, "success")
}
joined++
}
if joined == 0 {
l.Debugln("no multicast interfaces available")
return errors.New("no multicast interfaces available")
}
bs := make([]byte, 65536)
for {
select {
case <-doneCtx.Done():
return doneCtx.Err()
default:
}
n, _, addr, err := pconn.ReadFrom(bs)
if err != nil {
l.Debugln(err)
return err
}
l.Debugf("recv %d bytes from %s", n, addr)
c := make([]byte, n)
copy(c, bs)
select {
case outbox <- recv{c, addr}:
default:
l.Debugln("dropping message")
}
}
}