Skip to content

Commit ad0bdb4

Browse files
committed
net: add IP.IsPrivate
Add IsPrivate() helper to check if an IP is private according to RFC 1918 & RFC 4193 Fixes golang#29146
1 parent e8b8278 commit ad0bdb4

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

src/net/ip.go

+20
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,26 @@ func (ip IP) IsLoopback() bool {
125125
return ip.Equal(IPv6loopback)
126126
}
127127

128+
// IsPrivate reports whether ip is a private address, according to
129+
// Following RFC 4193, Section 3. Local IPv6 Unicast Addresses which says:
130+
// The Internet Assigned Numbers Authority (IANA) has reserved the
131+
// following three blocks of the IPv4 address space for private internets:
132+
// 10.0.0.0 - 10.255.255.255 (10/8 prefix)
133+
// 172.16.0.0 - 172.31.255.255 (172.16/12 prefix)
134+
// 192.168.0.0 - 192.168.255.255 (192.168/16 prefix)
135+
// Following RFC 4193, Section 3. Private Address Space which says:
136+
// The Internet Assigned Numbers Authority (IANA) has reserved the
137+
// following block of the IPv6 address space for local internets:
138+
// FC00:: - FDFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF (FC00::/7 prefix)
139+
func (ip IP) IsPrivate() bool {
140+
if ip4 := ip.To4(); ip4 != nil {
141+
return ip4[0] == 10 ||
142+
(ip4[0] == 172 && ip4[1]&0xf0 == 16) ||
143+
(ip4[0] == 192 && ip4[1] == 168)
144+
}
145+
return len(ip) == IPv6len && ip[0]&0xfe == 0xfc
146+
}
147+
128148
// IsMulticast reports whether ip is a multicast address.
129149
func (ip IP) IsMulticast() bool {
130150
if ip4 := ip.To4(); ip4 != nil {

src/net/ip_test.go

+22
Original file line numberDiff line numberDiff line change
@@ -691,6 +691,28 @@ var ipAddrScopeTests = []struct {
691691
{IP.IsGlobalUnicast, IP{0xfe, 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, false},
692692
{IP.IsGlobalUnicast, IP{0xff, 0x05, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, false},
693693
{IP.IsGlobalUnicast, nil, false},
694+
{IP.IsPrivate, nil, false},
695+
{IP.IsPrivate, IPv4(1, 1, 1, 1), false},
696+
{IP.IsPrivate, IPv4(9, 255, 255, 255), false},
697+
{IP.IsPrivate, IPv4(10, 0, 0, 0), true},
698+
{IP.IsPrivate, IPv4(10, 255, 255, 255), true},
699+
{IP.IsPrivate, IPv4(11, 0, 0, 0), false},
700+
{IP.IsPrivate, IPv4(172, 15, 255, 255), false},
701+
{IP.IsPrivate, IPv4(172, 16, 0, 0), true},
702+
{IP.IsPrivate, IPv4(172, 16, 255, 255), true},
703+
{IP.IsPrivate, IPv4(172, 23, 18, 255), true},
704+
{IP.IsPrivate, IPv4(172, 31, 255, 255), true},
705+
{IP.IsPrivate, IPv4(172, 31, 0, 0), true},
706+
{IP.IsPrivate, IPv4(172, 32, 0, 0), false},
707+
{IP.IsPrivate, IPv4(192, 167, 255, 255), false},
708+
{IP.IsPrivate, IPv4(192, 168, 0, 0), true},
709+
{IP.IsPrivate, IPv4(192, 168, 255, 255), true},
710+
{IP.IsPrivate, IPv4(192, 169, 0, 0), false},
711+
{IP.IsPrivate, IP{0xfb, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, false},
712+
{IP.IsPrivate, IP{0xfc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, true},
713+
{IP.IsPrivate, IP{0xfc, 0xff, 0x12, 0, 0, 0, 0, 0x44, 0, 0, 0, 0, 0, 0, 0, 0}, true},
714+
{IP.IsPrivate, IP{0xfd, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, true},
715+
{IP.IsPrivate, IP{0xfe, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, false},
694716
}
695717

696718
func name(f interface{}) string {

0 commit comments

Comments
 (0)