Skip to content
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
50 changes: 49 additions & 1 deletion pkg/mdns/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,14 +192,22 @@ func (b *Browser) ListenMulticastUDP() error {
for _, ipn := range nets {
conn, err := lc1.ListenPacket(ctx, "udp4", ipn.IP.String()+":5353") // same port important
if err != nil {
if strings.Contains(err.Error(), "address already in use") {
continue
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is useless

}
continue
}
b.Nets = append(b.Nets, ipn)
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)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This won't work. We need networks. They are used further in the logic.

}

// 3. Create receiver
Expand Down Expand Up @@ -372,3 +380,43 @@ func NewServiceEntries(msg *dns.Msg, ip net.IP) (entries []*ServiceEntry) {

return
}

func InterfacesIP4() ([]net.IP, error) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is useless

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

var ips []net.IP

loop:
for _, intf := range intfs {
if intf.Flags&net.FlagUp == 0 || intf.Flags&net.FlagLoopback != 0 {
continue
}

addrs, err := intf.Addrs()
if err != nil {
continue
}

for _, addr := range addrs {
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
}