windows: add IP() accessor to SocketAddress type

This is what everybody winds up doing with this object, so we make it
somewhat nicer than copying and pasting this everywhere or using type
aliases.

Change-Id: I3e12395cadfe212a7d01ce86478de9486383729a
Reviewed-on: https://go-review.googlesource.com/c/sys/+/178577
Run-TryBot: Jason Donenfeld <Jason@zx2c4.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
diff --git a/windows/types_windows.go b/windows/types_windows.go
index ee27936..2aa7716 100644
--- a/windows/types_windows.go
+++ b/windows/types_windows.go
@@ -4,7 +4,11 @@
 
 package windows
 
-import "syscall"
+import (
+	"net"
+	"syscall"
+	"unsafe"
+)
 
 const (
 	// Invented values to support what package os expects.
@@ -1314,6 +1318,16 @@
 	SockaddrLength int32
 }
 
+// IP returns an IPv4 or IPv6 address, or nil if the underlying SocketAddress is neither.
+func (addr *SocketAddress) IP() net.IP {
+	if uintptr(addr.SockaddrLength) >= unsafe.Sizeof(RawSockaddrInet4{}) && addr.Sockaddr.Addr.Family == AF_INET {
+		return (*RawSockaddrInet4)(unsafe.Pointer(addr.Sockaddr)).Addr[:]
+	} else if uintptr(addr.SockaddrLength) >= unsafe.Sizeof(RawSockaddrInet6{}) && addr.Sockaddr.Addr.Family == AF_INET6 {
+		return (*RawSockaddrInet6)(unsafe.Pointer(addr.Sockaddr)).Addr[:]
+	}
+	return nil
+}
+
 type IpAdapterUnicastAddress struct {
 	Length             uint32
 	Flags              uint32