Skip to content

Commit d070c89

Browse files
committed
fix(mdns): macos: handle address reuse and fallback for no interfaces
1 parent d559ec0 commit d070c89

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

pkg/mdns/client.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,13 +185,21 @@ func (b *Browser) ListenMulticastUDP() error {
185185
for _, ip4 := range ip4s {
186186
conn, err := lc1.ListenPacket(ctx, "udp4", ip4.String()+":5353") // same port important
187187
if err != nil {
188+
if strings.Contains(err.Error(), "address already in use") {
189+
continue
190+
}
188191
continue
189192
}
190193
b.Sends = append(b.Sends, conn)
191194
}
192195

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

197205
// 3. Create receiver
@@ -388,12 +396,19 @@ loop:
388396
switch v := addr.(type) {
389397
case *net.IPNet:
390398
if ip := v.IP.To4(); ip != nil {
399+
if ip.IsLinkLocalUnicast() {
400+
continue
401+
}
391402
ips = append(ips, ip)
392403
continue loop
393404
}
394405
}
395406
}
396407
}
397408

409+
if len(ips) == 0 {
410+
return nil, errors.New("no IPv4 addresses found")
411+
}
412+
398413
return ips, nil
399414
}

0 commit comments

Comments
 (0)