Skip to content

Commit

Permalink
mdns: add utility to check if host supports IPv6
Browse files Browse the repository at this point in the history
Signed-off-by: Gabriel Mougard <[email protected]>
  • Loading branch information
gabrielmougard committed Apr 30, 2024
1 parent 30fd95b commit d6067a4
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions microcloud/mdns/mdns.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,39 @@ func dnsTXTSlice(list []byte) []string {

return parts
}

// checkIPStatus checks if the interface is up, has multicast, and supports IPv4/IPv6.
func checkIPStatus(iface string) (ipv4OK bool, ipv6OK bool, err error) {
netInterface, err := net.InterfaceByName(iface)
if err != nil {
return false, false, err
}

if netInterface.Flags&net.FlagUp == net.FlagUp {
return false, false, nil
}

if netInterface.Flags&net.FlagMulticast != net.FlagMulticast {
return false, false, nil
}

addrs, err := netInterface.Addrs()
if err != nil {
return false, false, err
}

for _, addr := range addrs {
ipNet, ok := addr.(*net.IPNet)
if !ok {
continue
}

if ipNet.IP.To4() != nil {
ipv4OK = true
} else if (ipNet.IP.IsLinkLocalUnicast() || ipNet.IP.IsLinkLocalMulticast()) && ipNet.IP.To16() != nil {
ipv6OK = true
}
}

return ipv4OK, ipv6OK, nil
}

0 comments on commit d6067a4

Please sign in to comment.