mirror of
https://github.com/charlienet/go-mixed.git
synced 2025-07-18 08:32:40 +08:00
添加掩码格式
This commit is contained in:
34
ip_range/mask_bits.go
Normal file
34
ip_range/mask_bits.go
Normal file
@ -0,0 +1,34 @@
|
||||
package iprange
|
||||
|
||||
import "strings"
|
||||
|
||||
var maskBits = map[string]int{
|
||||
"255": 8,
|
||||
"254": 7,
|
||||
"252": 6,
|
||||
"248": 5,
|
||||
"240": 4,
|
||||
"224": 3,
|
||||
"192": 2,
|
||||
"128": 1,
|
||||
"0": 0,
|
||||
}
|
||||
|
||||
func MaskToBits(mask string) int {
|
||||
bits := 0
|
||||
|
||||
secs := strings.Split(mask, ".")
|
||||
if len(secs) != 4 {
|
||||
panic("the mask is incorrect")
|
||||
}
|
||||
|
||||
for _, s := range secs {
|
||||
if v, ok := maskBits[s]; ok {
|
||||
bits += v
|
||||
} else {
|
||||
panic("the mask is incorrect")
|
||||
}
|
||||
}
|
||||
|
||||
return bits
|
||||
}
|
Reference in New Issue
Block a user