diff options
author | FUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp> | 2018-08-31 11:23:13 +0900 |
---|---|---|
committer | FUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp> | 2018-08-31 11:23:13 +0900 |
commit | 9a0323419e9481186340a6455553fbc515969258 (patch) | |
tree | 9438edaa0de065ef627fa998c433e25232aab23d /pkg/server/sockopt_linux.go | |
parent | 475818dd3ccaa613fdd972097cf689ff9a4a6012 (diff) |
fix closing listening socket blocked
with go v1.11, closing a listening socket is blocked and unittests
fail due to timeout.
Signed-off-by: FUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp>
Diffstat (limited to 'pkg/server/sockopt_linux.go')
-rw-r--r-- | pkg/server/sockopt_linux.go | 73 |
1 files changed, 40 insertions, 33 deletions
diff --git a/pkg/server/sockopt_linux.go b/pkg/server/sockopt_linux.go index 9fe02ba5..152e97de 100644 --- a/pkg/server/sockopt_linux.go +++ b/pkg/server/sockopt_linux.go @@ -57,73 +57,80 @@ func buildTcpMD5Sig(address string, key string) (tcpmd5sig, error) { return t, nil } -func setsockoptTcpMD5Sig(fd int, address string, key string) error { +func SetTcpMD5SigSockopt(l *net.TCPListener, address string, key string) error { t, err := buildTcpMD5Sig(address, key) if err != nil { return err } b := *(*[unsafe.Sizeof(t)]byte)(unsafe.Pointer(&t)) - return os.NewSyscallError("setsockopt", syscall.SetsockoptString(fd, syscall.IPPROTO_TCP, TCP_MD5SIG, string(b[:]))) -} -func SetTcpMD5SigSockopt(l *net.TCPListener, address string, key string) error { - fi, _, err := extractFileAndFamilyFromTCPListener(l) + sc, err := l.SyscallConn() if err != nil { return err } - defer fi.Close() - - return setsockoptTcpMD5Sig(int(fi.Fd()), address, key) -} - -func setsockoptIpTtl(fd int, family int, value int) error { - level := syscall.IPPROTO_IP - name := syscall.IP_TTL - if family == syscall.AF_INET6 { - level = syscall.IPPROTO_IPV6 - name = syscall.IPV6_UNICAST_HOPS - } - return os.NewSyscallError("setsockopt", syscall.SetsockoptInt(fd, level, name, value)) + return setsockOptString(sc, syscall.IPPROTO_TCP, TCP_MD5SIG, string(b[:])) } func SetListenTcpTTLSockopt(l *net.TCPListener, ttl int) error { - fi, family, err := extractFileAndFamilyFromTCPListener(l) + family := extractFamilyFromTCPListener(l) + sc, err := l.SyscallConn() if err != nil { return err } - defer fi.Close() - - return setsockoptIpTtl(int(fi.Fd()), family, ttl) + return setsockoptIpTtl(sc, family, ttl) } func SetTcpTTLSockopt(conn *net.TCPConn, ttl int) error { - fi, family, err := extractFileAndFamilyFromTCPConn(conn) + family := extractFamilyFromTCPConn(conn) + sc, err := conn.SyscallConn() if err != nil { return err } - defer fi.Close() - - return setsockoptIpTtl(int(fi.Fd()), family, ttl) + return setsockoptIpTtl(sc, family, ttl) } -func setsockoptIpMinTtl(fd int, family int, value int) error { +func SetTcpMinTTLSockopt(conn *net.TCPConn, ttl int) error { + family := extractFamilyFromTCPConn(conn) + sc, err := conn.SyscallConn() + if err != nil { + return err + } level := syscall.IPPROTO_IP name := syscall.IP_MINTTL if family == syscall.AF_INET6 { level = syscall.IPPROTO_IPV6 name = IPV6_MINHOPCOUNT } - return os.NewSyscallError("setsockopt", syscall.SetsockoptInt(fd, level, name, value)) + return setsockOptInt(sc, level, name, ttl) } -func SetTcpMinTTLSockopt(conn *net.TCPConn, ttl int) error { - fi, family, err := extractFileAndFamilyFromTCPConn(conn) +func setsockoptTcpMD5Sig(fd int, address string, key string) error { + t, err := buildTcpMD5Sig(address, key) if err != nil { return err } - defer fi.Close() + b := *(*[unsafe.Sizeof(t)]byte)(unsafe.Pointer(&t)) + return os.NewSyscallError("setsockopt", syscall.SetsockoptString(fd, syscall.IPPROTO_TCP, TCP_MD5SIG, string(b[:]))) +} - return setsockoptIpMinTtl(int(fi.Fd()), family, ttl) +func setsockoptIpTtl2(fd int, family int, value int) error { + level := syscall.IPPROTO_IP + name := syscall.IP_TTL + if family == syscall.AF_INET6 { + level = syscall.IPPROTO_IPV6 + name = syscall.IPV6_UNICAST_HOPS + } + return os.NewSyscallError("setsockopt", syscall.SetsockoptInt(fd, level, name, value)) +} + +func setsockoptIpMinTtl(fd int, family int, value int) error { + level := syscall.IPPROTO_IP + name := syscall.IP_MINTTL + if family == syscall.AF_INET6 { + level = syscall.IPPROTO_IPV6 + name = IPV6_MINHOPCOUNT + } + return os.NewSyscallError("setsockopt", syscall.SetsockoptInt(fd, level, name, value)) } type TCPDialer struct { @@ -208,7 +215,7 @@ func (d *TCPDialer) DialTCP(addr string, port int) (*net.TCPConn, error) { } if d.Ttl != 0 { - if err = setsockoptIpTtl(fd, family, int(d.Ttl)); err != nil { + if err = setsockoptIpTtl2(fd, family, int(d.Ttl)); err != nil { return nil, err } } |