1、子网掩码地址转换为网络位长度
// 如 255.255.255.0 对应的网络位长度为 24
func SubNetMaskToLen(netmask string) (int, error) {
ipSplitArr := strings.Split(netmask, ".")
if len(ipSplitArr) != 4 {
return 0, fmt.Errorf("netmask:%v is not valid, pattern should like: 255.255.255.0", netmask)
}
ipv4MaskArr := make([]byte, 4)
for i, value := range ipSplitArr {
intValue, err := strconv.Atoi(value)
if err != nil {
return 0, fmt.Errorf("ipMaskToInt call strconv.Atoi error:[%v] string value is: [%s]", err, value)
}
if intValue > 255 {
return 0, fmt.Errorf("netmask cannot greater than 255, current value is: [%s]", value)
}
ipv4MaskArr[i] = byte(intValue)
}
ones, _ := net.IPv4Mask(ipv4MaskArr[0], ipv4MaskArr[1], ipv4MaskArr[2], ipv4MaskArr[3]).Size()
return ones, nil
}
2、网络位长度转换为子网掩码地址
// 如 24 对应的子网掩码地址为 255.255.255.0
func LenToSubNetMask(subnet int) string {
var buff bytes.Buffer
for i := 0; i < subnet; i++ {
buff.WriteString("1")
}
for i := subnet; i < 32; i++ {
buff.WriteString("0")
}
masker := buff.String()
a, _ := strconv.ParseUint(masker[:8], 2, 64)
b, _ := strconv.ParseUint(masker[8:16], 2, 64)
c, _ := strconv.ParseUint(masker[16:24], 2, 64)
d, _ := strconv.ParseUint(masker[24:32], 2, 64)
resultMask := fmt.Sprintf("%v.%v.%v.%v", a, b, c, d)
return resultMask
}