Skip to content

Commit

Permalink
fix using multicast when a single interface doesn't support it (#1874)
Browse files Browse the repository at this point in the history
  • Loading branch information
aler9 authored May 28, 2023
1 parent af324c9 commit ce1d2ab
Showing 1 changed file with 25 additions and 8 deletions.
33 changes: 25 additions & 8 deletions internal/core/udp_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,30 @@ func opusGetPacketDuration(pkt []byte) time.Duration {
return (time.Duration(frameDuration) * time.Duration(frameCount) * time.Millisecond) / 48
}

func joinMulticastGroupOnAtLeastOneInterface(p *ipv4.PacketConn, listenIP net.IP) error {
intfs, err := net.Interfaces()
if err != nil {
return err
}

success := false

for _, intf := range intfs {
if (intf.Flags & net.FlagMulticast) != 0 {
err := p.JoinGroup(&intf, &net.UDPAddr{IP: listenIP})
if err == nil {
success = true
}
}
}

if !success {
return fmt.Errorf("unable to activate multicast on any network interface")
}

return nil
}

type packetConnReader struct {
pc net.PacketConn
midbuf []byte
Expand Down Expand Up @@ -144,17 +168,10 @@ func (s *udpSource) run(ctx context.Context, cnf *conf.PathConf, _ chan *conf.Pa
return err
}

intfs, err := net.Interfaces()
err = joinMulticastGroupOnAtLeastOneInterface(p, ip)
if err != nil {
return err
}

for _, intf := range intfs {
err := p.JoinGroup(&intf, &net.UDPAddr{IP: ip})
if err != nil {
return err
}
}
}

dem := astits.NewDemuxer(
Expand Down

0 comments on commit ce1d2ab

Please sign in to comment.