Browse Source

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>
Jason A. Donenfeld 6 years ago
parent
commit
dbbf3f1254
1 changed files with 15 additions and 1 deletions
  1. 15 1
      windows/types_windows.go

+ 15 - 1
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 @@ type SocketAddress struct {
 	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