]> Cypherpunks repositories - gostls13.git/commitdiff
net/netip: add IPv4Unspecified
authorJason A. Donenfeld <Jason@zx2c4.com>
Wed, 3 Nov 2021 15:46:44 +0000 (16:46 +0100)
committerBrad Fitzpatrick <bradfitz@golang.org>
Fri, 5 Nov 2021 22:30:17 +0000 (22:30 +0000)
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 <Jason@zx2c4.com>
Trust: Brad Fitzpatrick <bradfitz@golang.org>
Run-TryBot: Jason A. Donenfeld <Jason@zx2c4.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
src/net/netip/netip.go

index e6e90f11dbbe3265b20e93054d6ede4614f08feb..1596acbb8ef7ad17bd937a90d8290632d60e1c12 100644 (file)
@@ -75,6 +75,9 @@ func IPv6LinkLocalAllNodes() Addr { return AddrFrom16([16]byte{0: 0xff, 1: 0x02,
 // IPv6Unspecified returns the IPv6 unspecified address "::".
 func IPv6Unspecified() Addr { return Addr{z: z6noz} }
 
+// IPv4Unspecified returns the IPv4 unspecified address "0.0.0.0".
+func IPv4Unspecified() Addr { return AddrFrom4([4]byte{}) }
+
 // AddrFrom4 returns the address of the IPv4 address given by the bytes in addr.
 func AddrFrom4(addr [4]byte) Addr {
        return Addr{
@@ -595,7 +598,7 @@ func (ip Addr) IsGlobalUnicast() bool {
 
        // Match package net's IsGlobalUnicast logic. Notably private IPv4 addresses
        // and ULA IPv6 addresses are still considered "global unicast".
-       if ip.Is4() && (ip == AddrFrom4([4]byte{}) || ip == AddrFrom4([4]byte{255, 255, 255, 255})) {
+       if ip.Is4() && (ip == IPv4Unspecified() || ip == AddrFrom4([4]byte{255, 255, 255, 255})) {
                return false
        }
 
@@ -633,7 +636,7 @@ func (ip Addr) IsPrivate() bool {
 //
 // Note that the zero Addr is not an unspecified address.
 func (ip Addr) IsUnspecified() bool {
-       return ip == AddrFrom4([4]byte{}) || ip == IPv6Unspecified()
+       return ip == IPv4Unspecified() || ip == IPv6Unspecified()
 }
 
 // Prefix keeps only the top b bits of IP, producing a Prefix