blob: ee6bae5c5e1ae0950c592664428e613d274cb5b4 [file] [log] [blame]
Cosmos Nicolaouf7a11d92014-08-29 09:56:07 -07001package netstate
David Why Use Two When One Will Do Presotto748a0d52014-07-30 17:07:04 -07002
3import (
4 "net"
5)
6
7var privateCIDRs = []net.IPNet{
8 net.IPNet{IP: net.IPv4(10, 0, 0, 0), Mask: net.IPv4Mask(0xff, 0, 0, 0)},
9 net.IPNet{IP: net.IPv4(172, 16, 0, 0), Mask: net.IPv4Mask(0xff, 0xf0, 0, 0)},
10 net.IPNet{IP: net.IPv4(192, 168, 0, 0), Mask: net.IPv4Mask(0xff, 0xff, 0, 0)},
11}
12
13// IsGloballyRoutable returns true if the argument is a globally routable IP address.
Cosmos Nicolaouf7a11d92014-08-29 09:56:07 -070014func IsGloballyRoutableIP(ip net.IP) bool {
David Why Use Two When One Will Do Presotto748a0d52014-07-30 17:07:04 -070015 if !ip.IsGlobalUnicast() {
16 return false
17 }
18 if ip4 := ip.To4(); ip4 != nil {
19 for _, cidr := range privateCIDRs {
20 if cidr.Contains(ip4) {
21 return false
22 }
23 }
24 if ip4.Equal(net.IPv4bcast) {
25 return false
26 }
27 }
28 return true
29}