mirror of
https://github.com/golang/sys.git
synced 2026-02-08 03:36:03 +03:00
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>
This commit is contained in:
committed by
Brad Fitzpatrick
parent
c3d486d151
commit
dbbf3f1254
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user