Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(mdns): macos: handle address reuse and fallback for no interfaces #1283

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion pkg/mdns/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,13 +185,21 @@ func (b *Browser) ListenMulticastUDP() error {
for _, ip4 := range ip4s {
conn, err := lc1.ListenPacket(ctx, "udp4", ip4.String()+":5353") // same port important
if err != nil {
if strings.Contains(err.Error(), "address already in use") {
continue
}
continue
}
b.Sends = append(b.Sends, conn)
}

if b.Sends == nil {
return errors.New("no interfaces for listen")
// Fall back to the system's default interface if no specific interfaces are available
conn, err := net.ListenMulticastUDP("udp4", nil, MulticastAddr)
if err != nil {
return errors.New("no interfaces for listen and fallback failed: " + err.Error())
}
b.Sends = append(b.Sends, conn)
}

// 3. Create receiver
Expand Down Expand Up @@ -388,12 +396,19 @@ loop:
switch v := addr.(type) {
case *net.IPNet:
if ip := v.IP.To4(); ip != nil {
if ip.IsLinkLocalUnicast() {
continue
}
ips = append(ips, ip)
continue loop
}
}
}
}

if len(ips) == 0 {
return nil, errors.New("no IPv4 addresses found")
}

return ips, nil
}