-
Notifications
You must be signed in to change notification settings - Fork 0
temp2
Jianwei Mao edited this page Jul 12, 2023
·
1 revision
// true: up, not loopback func judgeNic(intf net.Interface) bool { return (intf.Flags&net.FlagUp != 0) && // Mao: not reliable. it is administrative state, not running state. 2022.06.21 // a chance for golang contribution (intf.Flags&net.FlagLoopback == 0) }
func judgeIPv6(ip *net.IP) bool { return ip.To4() == nil }
// Get GUA, ULA // not loopback, link-local func getIPv6AddressGuaUla() (addresses []string) { result := []string{}
intfs, err := net.Interfaces()
if err != nil {
log.Printf("Fail to get Interfaces, %s\n", err.Error())
return result
}
for _, intf := range intfs {
log.Printf("NIC name: %s, flags: %s\n", intf.Name, intf.Flags)
if judgeNic(intf) {
addrs, err := intf.Addrs()
if err != nil {
log.Printf("Fail to get Addrs, %s\n", err.Error())
continue
}
for _, addr := range addrs {
if ip, ok := addr.(*net.IPNet); ok {
if judgeIPv6(&ip.IP) && (ip.IP.IsGlobalUnicast() || ip.IP.IsPrivate()) { // allow GUA, ULA
result = append(result, ip.IP.String())
} else {
//log.Println("Filtered: ", ip.IP.String())
}
}
}
}
}
log.Println("Available IPv6 addresses: ", result)
return result
}