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>
diff --git a/unix/ifreq_linux_test.go b/unix/ifreq_linux_test.go
index 52640ac..7a6d56c 100644
--- a/unix/ifreq_linux_test.go
+++ b/unix/ifreq_linux_test.go
@@ -8,6 +8,8 @@
 package unix
 
 import (
+	"bytes"
+	"net"
 	"testing"
 	"unsafe"
 )
@@ -74,6 +76,48 @@
 	}
 }
 
+func TestIfreqInet4Addr(t *testing.T) {
+	ifr := testIfreq(t)
+	in := net.IPv4(192, 0, 2, 1).To4()
+	if err := ifr.SetInet4Addr(in); err != nil {
+		t.Fatalf("failed to set ifreq IPv4 address: %v", err)
+	}
+
+	// Store fixed offset data (AF_INET, IPv4 address) within underlying
+	// sockaddr bytes. Everything else should be zeroed.
+	want := ifreqUnion{4: 192, 5: 0, 6: 2, 7: 1}
+	if isBigEndian {
+		want[0] = 0x00
+		want[1] = 0x02
+	} else {
+		want[0] = 0x02
+		want[1] = 0x00
+	}
+
+	if got := ifr.raw.Ifru; want != got {
+		t.Fatalf("unexpected ifreq sockaddr bytes:\n got: % #x\nwant: % #x", got, want)
+	}
+
+	got, err := ifr.Inet4Addr()
+	if err != nil {
+		t.Fatalf("failed to get ifreq IPv4 address: %v", err)
+	}
+	if !bytes.Equal(in, got) {
+		t.Fatalf("unexpected ifreq IPv4 address:\n got: % #x\nwant: % #x", got, in)
+	}
+
+	// Invalid input, wrong length.
+	if err := ifr.SetInet4Addr([]byte{0xff}); err == nil {
+		t.Fatal("expected an error setting invalid IPv4 address, but none occurred")
+	}
+
+	// Invalid output, AF_INET is only set by SetInet4Addr input.
+	ifr.SetUint32(0xffffffff)
+	if _, err := ifr.Inet4Addr(); err == nil {
+		t.Fatal("expected an error getting invalid IPv4 address, but none occurred")
+	}
+}
+
 func TestIfreqUint16(t *testing.T) {
 	ifr := testIfreq(t)
 	const in = 0x0102