udp: Windows: abstract the GetSocketOption away too.

Windows is weird, put this stuff in the impl. specific sub file too.
This commit is contained in:
Miek Gieben 2014-07-11 08:13:05 +01:00
parent 892588a93c
commit 907e91be57
3 changed files with 17 additions and 3 deletions

5
udp.go
View File

@ -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:

View File

@ -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
}

View File

@ -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 }