lib/beacon: Only send to appropriately flagged interfaces (ref #5957) (#6404)

saves some traffic (potential mobile wakeups), may help with #5957.
This commit is contained in:
Alex Xu 2020-03-17 08:40:37 +00:00 committed by GitHub
parent f08d09f607
commit 1e68ab3f90
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 5 deletions

View File

@ -44,17 +44,29 @@ func writeBroadcasts(ctx context.Context, inbox <-chan []byte, port int) error {
return nil return nil
} }
addrs, err := net.InterfaceAddrs() intfs, err := net.Interfaces()
if err != nil { if err != nil {
l.Debugln(err) l.Debugln(err)
return err return err
} }
var dsts []net.IP var dsts []net.IP
for _, addr := range addrs { for _, intf := range intfs {
if iaddr, ok := addr.(*net.IPNet); ok && len(iaddr.IP) >= 4 && iaddr.IP.IsGlobalUnicast() && iaddr.IP.To4() != nil { if intf.Flags&net.FlagBroadcast == 0 {
baddr := bcast(iaddr) continue
dsts = append(dsts, baddr.IP) }
addrs, err := intf.Addrs()
if err != nil {
l.Debugln(err)
return err
}
for _, addr := range addrs {
if iaddr, ok := addr.(*net.IPNet); ok && len(iaddr.IP) >= 4 && iaddr.IP.IsGlobalUnicast() && iaddr.IP.To4() != nil {
baddr := bcast(iaddr)
dsts = append(dsts, baddr.IP)
}
} }
} }

View File

@ -67,6 +67,10 @@ func writeMulticasts(ctx context.Context, inbox <-chan []byte, addr string) erro
success := 0 success := 0
for _, intf := range intfs { for _, intf := range intfs {
if intf.Flags&net.FlagMulticast == 0 {
continue
}
wcm.IfIndex = intf.Index wcm.IfIndex = intf.Index
pconn.SetWriteDeadline(time.Now().Add(time.Second)) pconn.SetWriteDeadline(time.Now().Add(time.Second))
_, err = pconn.WriteTo(bs, wcm, gaddr) _, err = pconn.WriteTo(bs, wcm, gaddr)