From 907e91be5750a858976be3effc87c908bb5a3885 Mon Sep 17 00:00:00 2001 From: Miek Gieben Date: Fri, 11 Jul 2014 08:13:05 +0100 Subject: [PATCH] udp: Windows: abstract the GetSocketOption away too. Windows is weird, put this stuff in the impl. specific sub file too. --- udp.go | 5 ++--- udp_linux.go | 13 +++++++++++++ udp_other.go | 2 ++ 3 files changed, 17 insertions(+), 3 deletions(-) diff --git a/udp.go b/udp.go index 6ec72f6e..8e003f8f 100644 --- a/udp.go +++ b/udp.go @@ -27,13 +27,12 @@ func setUDPSocketOptions(conn *net.UDPConn) error { sa, err := syscall.Getsockname(int(file.Fd())) switch sa.(type) { case *syscall.SockaddrInet6: - // dual stack. See http://stackoverflow.com/questions/1618240/how-to-support-both-ipv4-and-ipv6-connections - v6only, err := syscall.GetsockoptInt(int(file.Fd()), syscall.IPPROTO_IPV6, syscall.IPV6_V6ONLY) + v6only, err := getUDPSocketOption6Only(conn) if err != nil { return err } setUDPSocketOptions6(conn) - if v6only == 0 { + if !v6only { setUDPSocketOptions4(conn) } case *syscall.SockaddrInet4: diff --git a/udp_linux.go b/udp_linux.go index d1929382..03059085 100644 --- a/udp_linux.go +++ b/udp_linux.go @@ -38,3 +38,16 @@ func setUDPSocketOptions6(conn *net.UDPConn) error { } return nil } + +// getUDPSocketOption6Only return true if the socket is v6 only and false when it is v4/v6 combined +// (dualstack). +func getUDPSocketOption6Only(conn *net.UDPConn) (bool, error) { + // We got the .File() in NewUDPConn, this this will work. + file, _ := conn.File() + // dual stack. See http://stackoverflow.com/questions/1618240/how-to-support-both-ipv4-and-ipv6-connections + v6only, err := syscall.GetsockoptInt(int(file.Fd()), syscall.IPPROTO_IPV6, syscall.IPV6_V6ONLY) + if err != nil { + return false, err + } + return v6only == 1, nil +} diff --git a/udp_other.go b/udp_other.go index d93199db..cbf60857 100644 --- a/udp_other.go +++ b/udp_other.go @@ -14,3 +14,5 @@ import ( func setUDPSocketOptions4(conn *net.UDPConn) error { return nil } func setUDPSocketOptions6(conn *net.UDPConn) error { return nil } + +func getUDPSocketOption6Only(conn *net.UDPConn) (bool, error) { return false, nil }