Skip to content

Commit

Permalink
RSDK-8731 - hardcode loopback interface to listen on for mDNS (#360)
Browse files Browse the repository at this point in the history
  • Loading branch information
cheukt authored Oct 11, 2024
1 parent 77ed10a commit 9c8e880
Showing 1 changed file with 38 additions and 1 deletion.
39 changes: 38 additions & 1 deletion rpc/dial.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,9 @@ func dial(
wg.Add(1)
go func(dOpts dialOptions) {
defer wg.Done()
if dOpts.debug {
logger.Debugw("trying mDNS", "address", address)
}
conn, cached, err := dialMulticastDNS(ctxParallel, address, logger, dOpts)
if err != nil {
dialCh <- dialResult{err: err}
Expand Down Expand Up @@ -295,11 +298,39 @@ func dial(
return conn, cached, nil
}

func listMulticastInterfaces() []net.Interface {
var interfaces []net.Interface
ifaces, err := net.Interfaces()
if err != nil {
return nil
}
for _, ifi := range ifaces {
// If the interface is not up, skip and continue the loop
if (ifi.Flags & net.FlagUp) == 0 {
continue
}

// On Linux machines the loopback interface may not enable multicast by default even if it is
// capable. In cases where other network interfaces is shut off, this will cause issues
// when trying to connect to a candidate on the same host.
// Therefore, hardcode and return loopback interfaces as a multicast interface regardless of whether
// the multicast flag exists.
if (ifi.Flags&net.FlagLoopback) > 0 || (ifi.Flags&net.FlagMulticast) > 0 {
interfaces = append(interfaces, ifi)
}
}
return interfaces
}

func lookupMDNSCandidate(ctx context.Context, address string, logger utils.ZapCompatibleLogger) (*zeroconf.ServiceEntry, error) {
candidates := []string{address, strings.ReplaceAll(address, ".", "-")}
// RSDK-8205: logger.Desugar().Sugar() is necessary to massage a ZapCompatibleLogger into a
// *zap.SugaredLogger to match zeroconf function signatures.
resolver, err := zeroconf.NewResolver(logger.Desugar().Sugar(), zeroconf.SelectIPRecordType(zeroconf.IPv4))
resolver, err := zeroconf.NewResolver(
logger.Desugar().Sugar(),
zeroconf.SelectIPRecordType(zeroconf.IPv4),
zeroconf.SelectIfaces(listMulticastInterfaces()),
)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -336,6 +367,12 @@ func dialMulticastDNS(
) (ClientConn, bool, error) {
entry, err := lookupMDNSCandidate(ctx, address, logger)
if err != nil {
if dOpts.debug {
logger.Debugw(
"failed to find mDNS candidate",
"err", err.Error(),
)
}
return nil, false, err
}
var hasGRPC, hasWebRTC bool
Expand Down

0 comments on commit 9c8e880

Please sign in to comment.