From e571f699d21c9f1a2fa2baa1c50c3cdbdb024769 Mon Sep 17 00:00:00 2001 From: Miek Gieben Date: Fri, 11 Jul 2014 08:47:49 +0000 Subject: [PATCH] Move more syscall function into their own impl. Windows is stupid --- udp.go | 6 ++---- udp_linux.go | 28 +++++++++++++++++++++------- udp_other.go | 8 +++++--- 3 files changed, 28 insertions(+), 14 deletions(-) diff --git a/udp.go b/udp.go index 8e003f8f..9b426c74 100644 --- a/udp.go +++ b/udp.go @@ -19,15 +19,13 @@ func (s *sessionUDP) RemoteAddr() net.Addr { return s.raddr } // setUDPSocketOptions sets the UDP socket options. // This function is implemented on a per platform basis. See udp_*.go for more details func setUDPSocketOptions(conn *net.UDPConn) error { - file, err := conn.File() + sa, err := getUDPSocketName(conn) if err != nil { return err } - - sa, err := syscall.Getsockname(int(file.Fd())) switch sa.(type) { case *syscall.SockaddrInet6: - v6only, err := getUDPSocketOption6Only(conn) + v6only, err := getUDPSocketOptions6Only(conn) if err != nil { return err } diff --git a/udp_linux.go b/udp_linux.go index 03059085..aedc7663 100644 --- a/udp_linux.go +++ b/udp_linux.go @@ -21,8 +21,10 @@ import ( // setUDPSocketOptions4 prepares the v4 socket for sessions. func setUDPSocketOptions4(conn *net.UDPConn) error { - // We got the .File() in NewUDPConn, this this will work. - file, _ := conn.File() + file, err := conn.File() + if err != nil { + return err + } if err := syscall.SetsockoptInt(int(file.Fd()), syscall.IPPROTO_IP, syscall.IP_PKTINFO, 1); err != nil { return err } @@ -31,8 +33,10 @@ func setUDPSocketOptions4(conn *net.UDPConn) error { // setUDPSocketOptions6 prepares the v6 socket for sessions. func setUDPSocketOptions6(conn *net.UDPConn) error { - // We got the .File() in NewUDPConn, this this will work. - file, _ := conn.File() + file, err := conn.File() + if err != nil { + return err + } if err := syscall.SetsockoptInt(int(file.Fd()), syscall.IPPROTO_IPV6, syscall.IPV6_RECVPKTINFO, 1); err != nil { return err } @@ -41,9 +45,11 @@ func setUDPSocketOptions6(conn *net.UDPConn) error { // 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() +func getUDPSocketOptions6Only(conn *net.UDPConn) (bool, error) { + file, err := conn.File() + if err != nil { + return false, err + } // 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 { @@ -51,3 +57,11 @@ func getUDPSocketOption6Only(conn *net.UDPConn) (bool, error) { } return v6only == 1, nil } + +func getUDPSocketName(conn *net.UDPConn) (syscall.Sockaddr, error) { + file, err := conn.File() + if err != nil { + return nil, err + } + return syscall.Getsockname(int(file.Fd())) +} diff --git a/udp_other.go b/udp_other.go index cbf60857..c419295c 100644 --- a/udp_other.go +++ b/udp_other.go @@ -12,7 +12,9 @@ import ( // These do nothing. See udp_linux.go for an example of how to implement this. -func setUDPSocketOptions4(conn *net.UDPConn) error { return nil } -func setUDPSocketOptions6(conn *net.UDPConn) error { return nil } +// We tried to adhire to some kind of naming scheme. -func getUDPSocketOption6Only(conn *net.UDPConn) (bool, error) { return false, nil } +func setUDPSocketOptions4(conn *net.UDPConn) error { return nil } +func setUDPSocketOptions6(conn *net.UDPConn) error { return nil } +func getUDPSocketOptions6Only(conn *net.UDPConn) (bool, error) { return false, nil } +func getUDPSocketName(conn *net.UDPConn) (syscal.Sockaddr, error) { return nil, nil }