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:
parent
892588a93c
commit
907e91be57
5
udp.go
5
udp.go
|
@ -27,13 +27,12 @@ func setUDPSocketOptions(conn *net.UDPConn) error {
|
||||||
sa, err := syscall.Getsockname(int(file.Fd()))
|
sa, err := syscall.Getsockname(int(file.Fd()))
|
||||||
switch sa.(type) {
|
switch sa.(type) {
|
||||||
case *syscall.SockaddrInet6:
|
case *syscall.SockaddrInet6:
|
||||||
// dual stack. See http://stackoverflow.com/questions/1618240/how-to-support-both-ipv4-and-ipv6-connections
|
v6only, err := getUDPSocketOption6Only(conn)
|
||||||
v6only, err := syscall.GetsockoptInt(int(file.Fd()), syscall.IPPROTO_IPV6, syscall.IPV6_V6ONLY)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
setUDPSocketOptions6(conn)
|
setUDPSocketOptions6(conn)
|
||||||
if v6only == 0 {
|
if !v6only {
|
||||||
setUDPSocketOptions4(conn)
|
setUDPSocketOptions4(conn)
|
||||||
}
|
}
|
||||||
case *syscall.SockaddrInet4:
|
case *syscall.SockaddrInet4:
|
||||||
|
|
13
udp_linux.go
13
udp_linux.go
|
@ -38,3 +38,16 @@ func setUDPSocketOptions6(conn *net.UDPConn) error {
|
||||||
}
|
}
|
||||||
return nil
|
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
|
||||||
|
}
|
||||||
|
|
|
@ -14,3 +14,5 @@ import (
|
||||||
|
|
||||||
func setUDPSocketOptions4(conn *net.UDPConn) error { return nil }
|
func setUDPSocketOptions4(conn *net.UDPConn) error { return nil }
|
||||||
func setUDPSocketOptions6(conn *net.UDPConn) error { return nil }
|
func setUDPSocketOptions6(conn *net.UDPConn) error { return nil }
|
||||||
|
|
||||||
|
func getUDPSocketOption6Only(conn *net.UDPConn) (bool, error) { return false, nil }
|
||||||
|
|
Loading…
Reference in New Issue