Gerrit Bot has uploaded this change for review.
net: store IPv4 returned from cgo resolver as 4-byte slice net.IP
net.IP states that a 16-byte slice can still be an IPv4 address.
But after netip.Addr is introduced, it requires extra care to keep
it as an IPv4 address when converting it to a netip.Addr using
netip.AddrFromSlice.
To address this issue, let's change the cgo resolver to return
4-byte net.IP for IPv4. The change will save us 12 bytes too.
Please note that the go resolver already return IPv4 as 4-byte
slice.
The test TestResolverLookupIP has been modified to cover this
behavior. So no new test is added.
Fixes #53554.
Change-Id: I0dc2a59ad785c0c67a7bc22433105529f055997f
GitHub-Last-Rev: 414bf0b5ea921b0258ce392995c6c20f01ae2a8c
GitHub-Pull-Request: golang/go#53638
---
M src/net/cgo_unix.go
M src/net/ip.go
M src/net/lookup_test.go
M src/net/lookup_windows.go
4 files changed, 46 insertions(+), 23 deletions(-)
diff --git a/src/net/cgo_unix.go b/src/net/cgo_unix.go
index 71d9056..a907063 100644
--- a/src/net/cgo_unix.go
+++ b/src/net/cgo_unix.go
@@ -337,12 +337,3 @@
}
return nil, 0
}
-
-func copyIP(x IP) IP {
- if len(x) < 16 {
- return x.To16()
- }
- y := make(IP, len(x))
- copy(y, x)
- return y
-}
diff --git a/src/net/ip.go b/src/net/ip.go
index 54c5288..d9f3da7 100644
--- a/src/net/ip.go
+++ b/src/net/ip.go
@@ -757,3 +757,9 @@
m := CIDRMask(n, 8*iplen)
return ip, &IPNet{IP: ip.Mask(m), Mask: m}, nil
}
+
+func copyIP(x IP) IP {
+ y := make(IP, len(x))
+ copy(y, x)
+ return y
+}
diff --git a/src/net/lookup_test.go b/src/net/lookup_test.go
index 3a31f56..c0ab5de 100644
--- a/src/net/lookup_test.go
+++ b/src/net/lookup_test.go
@@ -11,6 +11,7 @@
"context"
"fmt"
"internal/testenv"
+ "net/netip"
"reflect"
"runtime"
"sort"
@@ -1279,18 +1280,16 @@
t.Fatalf("DefaultResolver.LookupIP(%q, %q): failed with unexpected error: %v", network, host, err)
}
- var v4Addrs []IP
- var v6Addrs []IP
+ var v4Addrs []netip.Addr
+ var v6Addrs []netip.Addr
for _, ip := range ips {
- switch {
- case ip.To4() != nil:
- // We need to skip the test below because To16 will
- // convent an IPv4 address to an IPv4-mapped IPv6
- // address.
- v4Addrs = append(v4Addrs, ip)
- case ip.To16() != nil:
- v6Addrs = append(v6Addrs, ip)
- default:
+ if addr, ok := netip.AddrFromSlice(ip); ok {
+ if addr.Is4() {
+ v4Addrs = append(v4Addrs, addr)
+ } else {
+ v6Addrs = append(v6Addrs, addr)
+ }
+ } else {
t.Fatalf("IP=%q is neither IPv4 nor IPv6", ip)
}
}
@@ -1312,7 +1311,7 @@
t.Errorf("DefaultResolver.LookupIP(%q, %q): unexpected IPv4 addresses: %v", network, host, v4Addrs)
}
if network == "ip4" && len(v6Addrs) > 0 {
- t.Errorf("DefaultResolver.LookupIP(%q, %q): unexpected IPv6 addresses: %v", network, host, v6Addrs)
+ t.Errorf("DefaultResolver.LookupIP(%q, %q): unexpected IPv6 or IPv4-mapped IPv6 addresses: %v", network, host, v6Addrs)
}
})
}
diff --git a/src/net/lookup_windows.go b/src/net/lookup_windows.go
index 9ff39c7..d73c606 100644
--- a/src/net/lookup_windows.go
+++ b/src/net/lookup_windows.go
@@ -134,11 +134,11 @@
switch result.Family {
case syscall.AF_INET:
a := (*syscall.RawSockaddrInet4)(addr).Addr
- addrs = append(addrs, IPAddr{IP: IPv4(a[0], a[1], a[2], a[3])})
+ addrs = append(addrs, IPAddr{IP: copyIP(a[:])})
case syscall.AF_INET6:
a := (*syscall.RawSockaddrInet6)(addr).Addr
zone := zoneCache.name(int((*syscall.RawSockaddrInet6)(addr).Scope_id))
- addrs = append(addrs, IPAddr{IP: IP{a[0], a[1], a[2], a[3], a[4], a[5], a[6], a[7], a[8], a[9], a[10], a[11], a[12], a[13], a[14], a[15]}, Zone: zone})
+ addrs = append(addrs, IPAddr{IP: copyIP(a[:]), Zone: zone})
default:
return nil, &DNSError{Err: syscall.EWINDOWS.Error(), Name: name}
}
To view, visit change 415580. To unsubscribe, or for help writing mail filters, visit settings.
Attention is currently required from: Ian Lance Taylor.
Patch set 1:Run-TryBot +1
Attention is currently required from: Damien Neil, Ian Lance Taylor.
Gerrit Bot uploaded patch set #2 to this change.
The following approvals got outdated and were removed: Run-TryBot+1 by Damien Neil, TryBot-Result-1 by Gopher Robot
net: store IPv4 returned from cgo resolver as 4-byte slice net.IP
net.IP states that a 16-byte slice can still be an IPv4 address.
But after netip.Addr is introduced, it requires extra care to keep
it as an IPv4 address when converting it to a netip.Addr using
netip.AddrFromSlice.
To address this issue, let's change the cgo resolver to return
4-byte net.IP for IPv4. The change will save us 12 bytes too.
Please note that the go resolver already return IPv4 as 4-byte
slice.
The test TestResolverLookupIP has been modified to cover this
behavior. So no new test is added.
Fixes #53554.
Change-Id: I0dc2a59ad785c0c67a7bc22433105529f055997f
GitHub-Last-Rev: bd7bb2f17bd8e07ea5b39e4a24512ed35d316bb8
GitHub-Pull-Request: golang/go#53638
---
M src/net/cgo_unix.go
M src/net/ip.go
M src/net/lookup_test.go
M src/net/lookup_windows.go
4 files changed, 46 insertions(+), 23 deletions(-)
To view, visit change 415580. To unsubscribe, or for help writing mail filters, visit settings.
Attention is currently required from: Damien Neil, Ian Lance Taylor.
1 comment:
Patchset:
1 of 30 TryBots failed. […]
The failure is https://go.dev/issue/53419, which was fixed after this CL. I have just rebased this CL to the latest tip. Please run the trybot again, thank you!
To view, visit change 415580. To unsubscribe, or for help writing mail filters, visit settings.
Attention is currently required from: Damien Neil.
Patch set 2:Run-TryBot +1
Patch set 2:Auto-Submit +1Code-Review +2
1 comment:
Patchset:
Thanks.
To view, visit change 415580. To unsubscribe, or for help writing mail filters, visit settings.
Patch set 2:Code-Review +1
Gopher Robot submitted this change.
net: store IPv4 returned from cgo resolver as 4-byte slice net.IP
net.IP states that a 16-byte slice can still be an IPv4 address.
But after netip.Addr is introduced, it requires extra care to keep
it as an IPv4 address when converting it to a netip.Addr using
netip.AddrFromSlice.
To address this issue, let's change the cgo resolver to return
4-byte net.IP for IPv4. The change will save us 12 bytes too.
Please note that the go resolver already return IPv4 as 4-byte
slice.
The test TestResolverLookupIP has been modified to cover this
behavior. So no new test is added.
Fixes #53554.
Change-Id: I0dc2a59ad785c0c67a7bc22433105529f055997f
GitHub-Last-Rev: bd7bb2f17bd8e07ea5b39e4a24512ed35d316bb8
GitHub-Pull-Request: golang/go#53638
Reviewed-on: https://go-review.googlesource.com/c/go/+/415580
Auto-Submit: Damien Neil <dn...@google.com>
TryBot-Result: Gopher Robot <go...@golang.org>
Reviewed-by: Bryan Mills <bcm...@google.com>
Reviewed-by: Damien Neil <dn...@google.com>
Run-TryBot: Ian Lance Taylor <ia...@golang.org>
---
M src/net/cgo_unix.go
M src/net/ip.go
M src/net/lookup_test.go
M src/net/lookup_windows.go
4 files changed, 52 insertions(+), 23 deletions(-)
diff --git a/src/net/cgo_unix.go b/src/net/cgo_unix.go
index 81f492f..a944727 100644
--- a/src/net/cgo_unix.go
+++ b/src/net/cgo_unix.go
@@ -327,12 +327,3 @@
}
return nil, 0
}
-
-func copyIP(x IP) IP {
- if len(x) < 16 {
- return x.To16()
- }
- y := make(IP, len(x))
- copy(y, x)
- return y
-}
diff --git a/src/net/ip.go b/src/net/ip.go
index 54c5288..d9f3da7 100644
--- a/src/net/ip.go
+++ b/src/net/ip.go
@@ -757,3 +757,9 @@
m := CIDRMask(n, 8*iplen)
return ip, &IPNet{IP: ip.Mask(m), Mask: m}, nil
}
+
+func copyIP(x IP) IP {
+ y := make(IP, len(x))
+ copy(y, x)
+ return y
+}
diff --git a/src/net/lookup_test.go b/src/net/lookup_test.go
index ed9f93f..38618c7 100644
--- a/src/net/lookup_test.go
+++ b/src/net/lookup_test.go
@@ -10,6 +10,7 @@
"context"
"fmt"
"internal/testenv"
+ "net/netip"
"reflect"
"runtime"
"sort"
@@ -1289,18 +1290,16 @@
t.Fatalf("DefaultResolver.LookupIP(%q, %q): failed with unexpected error: %v", network, host, err)
}
- var v4Addrs []IP
- var v6Addrs []IP
+ var v4Addrs []netip.Addr
+ var v6Addrs []netip.Addr
for _, ip := range ips {
- switch {
- case ip.To4() != nil:
- // We need to skip the test below because To16 will
- // convent an IPv4 address to an IPv4-mapped IPv6
- // address.
- v4Addrs = append(v4Addrs, ip)
- case ip.To16() != nil:
- v6Addrs = append(v6Addrs, ip)
- default:
+ if addr, ok := netip.AddrFromSlice(ip); ok {
+ if addr.Is4() {
+ v4Addrs = append(v4Addrs, addr)
+ } else {
+ v6Addrs = append(v6Addrs, addr)
+ }
+ } else {
t.Fatalf("IP=%q is neither IPv4 nor IPv6", ip)
}
}
@@ -1322,7 +1321,7 @@
t.Errorf("DefaultResolver.LookupIP(%q, %q): unexpected IPv4 addresses: %v", network, host, v4Addrs)
}
if network == "ip4" && len(v6Addrs) > 0 {
- t.Errorf("DefaultResolver.LookupIP(%q, %q): unexpected IPv6 addresses: %v", network, host, v6Addrs)
+ t.Errorf("DefaultResolver.LookupIP(%q, %q): unexpected IPv6 or IPv4-mapped IPv6 addresses: %v", network, host, v6Addrs)
}
})
}
diff --git a/src/net/lookup_windows.go b/src/net/lookup_windows.go
index 9ff39c7..d73c606 100644
--- a/src/net/lookup_windows.go
+++ b/src/net/lookup_windows.go
@@ -134,11 +134,11 @@
switch result.Family {
case syscall.AF_INET:
a := (*syscall.RawSockaddrInet4)(addr).Addr
- addrs = append(addrs, IPAddr{IP: IPv4(a[0], a[1], a[2], a[3])})
+ addrs = append(addrs, IPAddr{IP: copyIP(a[:])})
case syscall.AF_INET6:
a := (*syscall.RawSockaddrInet6)(addr).Addr
zone := zoneCache.name(int((*syscall.RawSockaddrInet6)(addr).Scope_id))
- addrs = append(addrs, IPAddr{IP: IP{a[0], a[1], a[2], a[3], a[4], a[5], a[6], a[7], a[8], a[9], a[10], a[11], a[12], a[13], a[14], a[15]}, Zone: zone})
+ addrs = append(addrs, IPAddr{IP: copyIP(a[:]), Zone: zone})
default:
return nil, &DNSError{Err: syscall.EWINDOWS.Error(), Name: name}
}
To view, visit change 415580. To unsubscribe, or for help writing mail filters, visit settings.