Skip to content

Commit 414bf0b

Browse files
committed
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.
1 parent c847a2c commit 414bf0b

File tree

4 files changed

+19
-23
lines changed

4 files changed

+19
-23
lines changed

src/net/cgo_unix.go

-9
Original file line numberDiff line numberDiff line change
@@ -337,12 +337,3 @@ func cgoSockaddr(ip IP, zone string) (*C.struct_sockaddr, C.socklen_t) {
337337
}
338338
return nil, 0
339339
}
340-
341-
func copyIP(x IP) IP {
342-
if len(x) < 16 {
343-
return x.To16()
344-
}
345-
y := make(IP, len(x))
346-
copy(y, x)
347-
return y
348-
}

src/net/ip.go

+6
Original file line numberDiff line numberDiff line change
@@ -757,3 +757,9 @@ func ParseCIDR(s string) (IP, *IPNet, error) {
757757
m := CIDRMask(n, 8*iplen)
758758
return ip, &IPNet{IP: ip.Mask(m), Mask: m}, nil
759759
}
760+
761+
func copyIP(x IP) IP {
762+
y := make(IP, len(x))
763+
copy(y, x)
764+
return y
765+
}

src/net/lookup_test.go

+11-12
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"context"
1212
"fmt"
1313
"internal/testenv"
14+
"net/netip"
1415
"reflect"
1516
"runtime"
1617
"sort"
@@ -1279,18 +1280,16 @@ func TestResolverLookupIP(t *testing.T) {
12791280
t.Fatalf("DefaultResolver.LookupIP(%q, %q): failed with unexpected error: %v", network, host, err)
12801281
}
12811282

1282-
var v4Addrs []IP
1283-
var v6Addrs []IP
1283+
var v4Addrs []netip.Addr
1284+
var v6Addrs []netip.Addr
12841285
for _, ip := range ips {
1285-
switch {
1286-
case ip.To4() != nil:
1287-
// We need to skip the test below because To16 will
1288-
// convent an IPv4 address to an IPv4-mapped IPv6
1289-
// address.
1290-
v4Addrs = append(v4Addrs, ip)
1291-
case ip.To16() != nil:
1292-
v6Addrs = append(v6Addrs, ip)
1293-
default:
1286+
if addr, ok := netip.AddrFromSlice(ip); ok {
1287+
if addr.Is4() {
1288+
v4Addrs = append(v4Addrs, addr)
1289+
} else {
1290+
v6Addrs = append(v6Addrs, addr)
1291+
}
1292+
} else {
12941293
t.Fatalf("IP=%q is neither IPv4 nor IPv6", ip)
12951294
}
12961295
}
@@ -1312,7 +1311,7 @@ func TestResolverLookupIP(t *testing.T) {
13121311
t.Errorf("DefaultResolver.LookupIP(%q, %q): unexpected IPv4 addresses: %v", network, host, v4Addrs)
13131312
}
13141313
if network == "ip4" && len(v6Addrs) > 0 {
1315-
t.Errorf("DefaultResolver.LookupIP(%q, %q): unexpected IPv6 addresses: %v", network, host, v6Addrs)
1314+
t.Errorf("DefaultResolver.LookupIP(%q, %q): unexpected IPv6 or IPv4-mapped IPv6 addresses: %v", network, host, v6Addrs)
13161315
}
13171316
})
13181317
}

src/net/lookup_windows.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -134,11 +134,11 @@ func (r *Resolver) lookupIP(ctx context.Context, network, name string) ([]IPAddr
134134
switch result.Family {
135135
case syscall.AF_INET:
136136
a := (*syscall.RawSockaddrInet4)(addr).Addr
137-
addrs = append(addrs, IPAddr{IP: IPv4(a[0], a[1], a[2], a[3])})
137+
addrs = append(addrs, IPAddr{IP: copyIP(a[:])})
138138
case syscall.AF_INET6:
139139
a := (*syscall.RawSockaddrInet6)(addr).Addr
140140
zone := zoneCache.name(int((*syscall.RawSockaddrInet6)(addr).Scope_id))
141-
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})
141+
addrs = append(addrs, IPAddr{IP: copyIP(a[:]), Zone: zone})
142142
default:
143143
return nil, &DNSError{Err: syscall.EWINDOWS.Error(), Name: name}
144144
}

0 commit comments

Comments
 (0)