diff options
author | Vincent Bernat <vincent@bernat.im> | 2016-09-08 07:57:31 +0200 |
---|---|---|
committer | FUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp> | 2016-09-12 23:48:02 -0700 |
commit | 8c11fdfd0ff67fcab0c60b4c1ba23280fa1589f2 (patch) | |
tree | 384e6364cf2522c8785c5778bc32ebeffbe0f190 | |
parent | 83b6f07be76a5f5f8ae5b53b664bb8b902dcbf05 (diff) |
server: portability fix for setsockopt() on Linux
On some architecture, setsockopt() is a multiplexed syscall and is not
available directly (for example, on i386). Instead of invoking the
syscall directly, use syscall.SetsockoptString() which is safe as strings
are not null-terminated with Go.
On BSD, use syscall.SetsockoptInt(), but I don't know if there are
architectures with the same limitations as for Linux.
Signed-off-by: Vincent Bernat <vincent@bernat.im>
-rw-r--r-- | server/sockopt_bsd.go | 9 | ||||
-rw-r--r-- | server/sockopt_linux.go | 19 |
2 files changed, 13 insertions, 15 deletions
diff --git a/server/sockopt_bsd.go b/server/sockopt_bsd.go index 6c86e068..8fac5165 100644 --- a/server/sockopt_bsd.go +++ b/server/sockopt_bsd.go @@ -43,12 +43,9 @@ func SetTcpMD5SigSockopts(l *net.TCPListener, address string, key string) error // always enable and assumes that the configuration is done by // setkey() - t := int32(1) - _, _, e := syscall.Syscall6(syscall.SYS_SETSOCKOPT, fi.Fd(), - uintptr(syscall.IPPROTO_TCP), uintptr(TCP_MD5SIG), - uintptr(unsafe.Pointer(&t)), unsafe.Sizeof(t), 0) - if e > 0 { - return e + if err := syscall.SetsockoptInt(int(fi.Fd()), + syscall.IPPROTO_TCP, TCP_MD5SIG, 1); err != nil { + return err } return nil } diff --git a/server/sockopt_linux.go b/server/sockopt_linux.go index cbe461d4..1db559b5 100644 --- a/server/sockopt_linux.go +++ b/server/sockopt_linux.go @@ -65,11 +65,11 @@ func SetTcpMD5SigSockopts(l *net.TCPListener, address string, key string) error if l, err := net.FileListener(fi); err == nil { defer l.Close() } - _, _, e := syscall.Syscall6(syscall.SYS_SETSOCKOPT, fi.Fd(), - uintptr(syscall.IPPROTO_TCP), uintptr(TCP_MD5SIG), - uintptr(unsafe.Pointer(&t)), unsafe.Sizeof(t), 0) - if e > 0 { - return e + b := *(*[unsafe.Sizeof(t)]byte)(unsafe.Pointer(&t)) + if err := syscall.SetsockoptString(int(fi.Fd()), + syscall.IPPROTO_TCP, TCP_MD5SIG, + string(b[:])); err != nil { + return err } return nil } @@ -160,10 +160,11 @@ func DialTCPTimeoutWithMD5Sig(host string, port int, localAddr, key string, msec if err != nil { return nil, err } - if _, _, e := syscall.Syscall6(syscall.SYS_SETSOCKOPT, uintptr(fd), - uintptr(syscall.IPPROTO_TCP), uintptr(TCP_MD5SIG), - uintptr(unsafe.Pointer(&t)), unsafe.Sizeof(t), 0); e > 0 { - return nil, os.NewSyscallError("setsockopt", e) + b := *(*[unsafe.Sizeof(t)]byte)(unsafe.Pointer(&t)) + if err := syscall.SetsockoptString(int(fi.Fd()), + syscall.IPPROTO_TCP, TCP_MD5SIG, + string(b[:])); err != nil { + return nil, os.NewSyscallError("setsockopt", err) } if err = syscall.SetsockoptInt(fd, syscall.SOL_SOCKET, syscall.SO_BROADCAST, 1); err != nil { |