Skip to content

Commit 1c9e587

Browse files
6543odeke-em
authored andcommitted
net: add IP.IsPrivate
Adds a new method IsPrivate to check if an IP is private according to RFC 1918 & RFC 4193. Fixes #29146 Change-Id: If77b9e1746d86029df66ae9f18437b1f65a18b59 GitHub-Last-Rev: 09f4ba7 GitHub-Pull-Request: #42793 Reviewed-on: https://go-review.googlesource.com/c/go/+/272668 Reviewed-by: Emmanuel Odeke <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]> Run-TryBot: Emmanuel Odeke <[email protected]> TryBot-Result: Go Bot <[email protected]>
1 parent d9691ff commit 1c9e587

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

src/net/ip.go

+21
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,27 @@ func (ip IP) IsLoopback() bool {
128128
return ip.Equal(IPv6loopback)
129129
}
130130

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