Skip to content

Commit bd7bb2f

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 612bb34 commit bd7bb2f

File tree

4 files changed

+19
-23
lines changed

4 files changed

+19
-23
lines changed

src/net/cgo_unix.go

Lines changed: 0 additions & 9 deletions
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

Lines changed: 6 additions & 0 deletions
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

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"context"
1111
"fmt"
1212
"internal/testenv"
13+
"net/netip"
1314
"reflect"
1415
"runtime"
1516
"sort"
@@ -1289,18 +1290,16 @@ func TestResolverLookupIP(t *testing.T) {
12891290
t.Fatalf("DefaultResolver.LookupIP(%q, %q): failed with unexpected error: %v", network, host, err)
12901291
}
12911292

1292-
var v4Addrs []IP
1293-
var v6Addrs []IP
1293+
var v4Addrs []netip.Addr
1294+
var v6Addrs []netip.Addr
12941295
for _, ip := range ips {
1295-
switch {
1296-
case ip.To4() != nil:
1297-
// We need to skip the test below because To16 will
1298-
// convent an IPv4 address to an IPv4-mapped IPv6
1299-
// address.
1300-
v4Addrs = append(v4Addrs, ip)
1301-
case ip.To16() != nil:
1302-
v6Addrs = append(v6Addrs, ip)
1303-
default:
1296+
if addr, ok := netip.AddrFromSlice(ip); ok {
1297+
if addr.Is4() {
1298+
v4Addrs = append(v4Addrs, addr)
1299+
} else {
1300+
v6Addrs = append(v6Addrs, addr)
1301+
}
1302+
} else {
13041303
t.Fatalf("IP=%q is neither IPv4 nor IPv6", ip)
13051304
}
13061305
}
@@ -1322,7 +1321,7 @@ func TestResolverLookupIP(t *testing.T) {
13221321
t.Errorf("DefaultResolver.LookupIP(%q, %q): unexpected IPv4 addresses: %v", network, host, v4Addrs)
13231322
}
13241323
if network == "ip4" && len(v6Addrs) > 0 {
1325-
t.Errorf("DefaultResolver.LookupIP(%q, %q): unexpected IPv6 addresses: %v", network, host, v6Addrs)
1324+
t.Errorf("DefaultResolver.LookupIP(%q, %q): unexpected IPv6 or IPv4-mapped IPv6 addresses: %v", network, host, v6Addrs)
13261325
}
13271326
})
13281327
}

src/net/lookup_windows.go

Lines changed: 2 additions & 2 deletions
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)