unix: add Ifreq.Inet4Addr methods for manipulating IPv4 addresses

ioctls such as SIOCGIFADDR deal with AF_INET sockaddr addresses, but none of
the fields aside from the embedded IPv4 address are used. To keep the interface
more simple, we directly expose Inet4Addr get and set methods which enable use
of these ioctls with the Ifreq wrapper.

Change-Id: Ia8b6ab9730f852cb99f4152e334a59d395476d2f
Reviewed-on: https://go-review.googlesource.com/c/sys/+/343250
Trust: Matt Layher <mdlayher@gmail.com>
Run-TryBot: Matt Layher <mdlayher@gmail.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Reviewed-by: Tobias Klauser <tobias.klauser@gmail.com>
This commit is contained in:
Matt Layher
2021-08-20 12:10:16 +00:00
parent f52c844e1c
commit 41cdb8703e
3 changed files with 135 additions and 1 deletions
+50
View File
@@ -158,9 +158,59 @@ func TestIoctlIfreq(t *testing.T) {
t.Fatalf("unexpected interface name for index %d: got: %q, want: %q",
ifi.Index, got, want)
}
wantIP, ok := firstIPv4(t, &ifi)
if err := unix.IoctlIfreq(s, unix.SIOCGIFADDR, ifr); err != nil {
// Interface may have no assigned IPv4 address.
if err != unix.EADDRNOTAVAIL {
t.Fatalf("failed to get IPv4 address for %q: %v", ifi.Name, err)
}
// But if we found an address via rtnetlink, we should expect the
// ioctl to return one.
if ok {
t.Fatalf("found IPv4 address %q for %q but ioctl returned none", wantIP, ifi.Name)
}
continue
}
// Found an address, compare it directly.
addr, err := ifr.Inet4Addr()
if err != nil {
t.Fatalf("failed to get ifreq IPv4 address: %v", err)
}
if want, got := wantIP, addr; !want.Equal(got) {
t.Fatalf("unexpected first IPv4 address for %q: got: %q, want: %q",
ifi.Name, got, want)
}
}
}
// firstIPv4 reports whether the interface has an IPv4 address assigned,
// returning the first discovered address.
func firstIPv4(t *testing.T, ifi *net.Interface) (net.IP, bool) {
t.Helper()
addrs, err := ifi.Addrs()
if err != nil {
t.Fatalf("failed to get interface %q addresses: %v", ifi.Name, err)
}
for _, a := range addrs {
// Only want valid IPv4 addresses.
ipn, ok := a.(*net.IPNet)
if !ok || ipn.IP.To4() == nil {
continue
}
return ipn.IP, true
}
return nil, false
}
func TestPpoll(t *testing.T) {
if runtime.GOOS == "android" {
t.Skip("mkfifo syscall is not available on android, skipping test")