Skip to content

Commit b07c41d

Browse files
zx2c4bradfitz
authored andcommitted
net/netip: add IPv4Unspecified
There is IPv6Unspecified but there is not IPv4Unspecified, making for inconsistent code. This commit adds the missing function. Updates #49298. Change-Id: Id2519b646323642f59fb1cc6ea8e335fdde16290 Reviewed-on: https://go-review.googlesource.com/c/go/+/361056 Trust: Jason A. Donenfeld <[email protected]> Trust: Brad Fitzpatrick <[email protected]> Run-TryBot: Jason A. Donenfeld <[email protected]> TryBot-Result: Go Bot <[email protected]> Reviewed-by: Brad Fitzpatrick <[email protected]>
1 parent 87a3fc5 commit b07c41d

File tree

1 file changed

+5
-2
lines changed

1 file changed

+5
-2
lines changed

src/net/netip/netip.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,9 @@ func IPv6LinkLocalAllNodes() Addr { return AddrFrom16([16]byte{0: 0xff, 1: 0x02,
7575
// IPv6Unspecified returns the IPv6 unspecified address "::".
7676
func IPv6Unspecified() Addr { return Addr{z: z6noz} }
7777

78+
// IPv4Unspecified returns the IPv4 unspecified address "0.0.0.0".
79+
func IPv4Unspecified() Addr { return AddrFrom4([4]byte{}) }
80+
7881
// AddrFrom4 returns the address of the IPv4 address given by the bytes in addr.
7982
func AddrFrom4(addr [4]byte) Addr {
8083
return Addr{
@@ -595,7 +598,7 @@ func (ip Addr) IsGlobalUnicast() bool {
595598

596599
// Match package net's IsGlobalUnicast logic. Notably private IPv4 addresses
597600
// and ULA IPv6 addresses are still considered "global unicast".
598-
if ip.Is4() && (ip == AddrFrom4([4]byte{}) || ip == AddrFrom4([4]byte{255, 255, 255, 255})) {
601+
if ip.Is4() && (ip == IPv4Unspecified() || ip == AddrFrom4([4]byte{255, 255, 255, 255})) {
599602
return false
600603
}
601604

@@ -633,7 +636,7 @@ func (ip Addr) IsPrivate() bool {
633636
//
634637
// Note that the zero Addr is not an unspecified address.
635638
func (ip Addr) IsUnspecified() bool {
636-
return ip == AddrFrom4([4]byte{}) || ip == IPv6Unspecified()
639+
return ip == IPv4Unspecified() || ip == IPv6Unspecified()
637640
}
638641

639642
// Prefix keeps only the top b bits of IP, producing a Prefix

0 commit comments

Comments
 (0)