net: make {TCP,Unix}Listener implement syscall.Conn

This change adds the syscall.Conn interface to Listener types, with the caveat that only RawConn.Control is supported. Custom socket options can now be set safely.

Updates #19435
Fixes #22065

Change-Id: I7e74780d00318dc54a923d1c628a18a36009acab
Reviewed-on: https://go-review.googlesource.com/71651
Run-TryBot: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
This commit is contained in:
Lorenz Bauer 2017-10-18 11:15:04 +01:00 committed by Ian Lance Taylor
parent ff4ee88162
commit eed308de31
5 changed files with 143 additions and 0 deletions

View file

@ -92,3 +92,53 @@ func TestRawConn(t *testing.T) {
t.Fatal("should fail")
}
}
func TestRawConnListener(t *testing.T) {
ln, err := newLocalListener("tcp")
if err != nil {
t.Fatal(err)
}
defer ln.Close()
cc, err := ln.(*TCPListener).SyscallConn()
if err != nil {
t.Fatal(err)
}
called := false
op := func(uintptr) bool {
called = true
return true
}
err = cc.Write(op)
if err == nil {
t.Error("Write should return an error")
}
if called {
t.Error("Write shouldn't call op")
}
called = false
err = cc.Read(op)
if err == nil {
t.Error("Read should return an error")
}
if called {
t.Error("Read shouldn't call op")
}
var operr error
fn := func(s uintptr) {
_, operr = syscall.GetsockoptInt(int(s), syscall.SOL_SOCKET, syscall.SO_REUSEADDR)
}
err = cc.Control(fn)
if err != nil || operr != nil {
t.Fatal(err, operr)
}
ln.Close()
err = cc.Control(fn)
if err == nil {
t.Fatal("Control after Close should fail")
}
}