-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathparsecidr.go
47 lines (45 loc) · 1.09 KB
/
parsecidr.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
package main
import (
"log"
"net"
"strconv"
"strings"
)
// parseCIDR parses s as a CIDR notation IP address and mask,
// like "192.168.100.1/24" or "2001:DB8::/48", as defined in
// RFC 4632 and RFC 4291.
//
// It returns the network implied by the IP and mask.
// For example, ParseCIDR("192.168.100.1/16") returns
// the IP address 192.168.100.0 and the mask 255.255.255.0.
func parseCIDR(s string, verbose bool) *net.IPNet {
//log.Printf("s: %#v\n", s)
i := strings.Index(s, "/")
if i < 0 {
ip := net.ParseIP(s)
if ip == nil {
return nil
}
iplen := net.IPv4len
if len(ip.To4()) == 0 {
iplen = net.IPv6len
}
m := net.CIDRMask(8*iplen, 8*iplen)
return &net.IPNet{IP: ip, Mask: m}
}
addr, mask := s[:i], s[i+1:]
iplen := net.IPv4len
ip := net.ParseIP(addr)
if ip.To4() == nil {
iplen = net.IPv6len
}
n, err := strconv.Atoi(mask)
if ip == nil || err != nil || n < 0 || n > 8*iplen {
return nil
}
m := net.CIDRMask(n, 8*iplen)
if verbose && !ip.Mask(m).Equal(ip) {
log.Printf("WARNING: prefix/ip %s has hostbits set\n", s)
}
return &net.IPNet{IP: ip.Mask(m), Mask: m}
}