-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
51 lines (43 loc) · 788 Bytes
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package main
import (
"fmt"
"net"
"os"
)
func main() {
if len(os.Args) != 2 {
fmt.Println("USAGE: cidr <CIDR address>")
os.Exit(1)
}
cidr := os.Args[1]
start, end, err := getIPRange(cidr)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
fmt.Printf("%v - %v\n", start, end)
}
func getIPRange(cidr string) (start net.IP, end net.IP, err error) {
ip, ipnet, err := net.ParseCIDR(cidr)
if err != nil {
return nil, nil, err
}
start = ip.Mask(ipnet.Mask)
ones, total := ipnet.Mask.Size()
zeros := total - ones
count := (1 << uint32(zeros)) // 2 ^ zeros
end = make(net.IP, len(start))
copy(end, start)
i := len(end) - 1
for i >= 0 {
if count%256 == 0 {
end[i] = 255
count /= 256
} else {
end[i] += byte(count - 1)
break
}
i--
}
return
}