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 19, 2024
1 parent 8b8e395 commit 90cebe8
Showing 1 changed file with 46 additions and 0 deletions.
46 changes: 46 additions & 0 deletions microcloud/mdns/mdns.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ package mdns
import (
"fmt"
"net"
"os"
"path/filepath"
"strings"

"github.com/hashicorp/mdns"
)
Expand Down Expand Up @@ -48,3 +51,46 @@ 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
}

allIPv6InterfacesDisabledBytes, err := os.ReadFile("/proc/sys/net/ipv6/conf/all/disable_ipv6")
if err != nil {
return false, false, err
}

allIPv6InterfacesEnabled := strings.TrimSpace(string(allIPv6InterfacesDisabledBytes)) == "0"

newIPv6IfacesDisabledBytes, err := os.ReadFile("/proc/sys/net/ipv6/conf/default/disable_ipv6")
if err != nil {
return false, false, err
}

newIPv6IfacesEnabled := strings.TrimSpace(string(newIPv6IfacesDisabledBytes)) == "0"

interfaceIPv6Enabled := false
if iface != "" {
ifaceIPv6DisabledBytes, err := os.ReadFile(filepath.Join("proc/sys/net/ipv6/conf", iface, "disable_ipv6"))
if err != nil {
return false, false, err
}

interfaceIPv6Enabled = strings.TrimSpace(string(ifaceIPv6DisabledBytes)) == "0"
}

ipv6OK = allIPv6InterfacesEnabled && newIPv6IfacesEnabled && interfaceIPv6Enabled
return true, ipv6OK, nil
}

0 comments on commit 90cebe8

Please sign in to comment.