2018-10-11 21:17:43 +02:00
|
|
|
package socketpair
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net"
|
|
|
|
"os"
|
|
|
|
|
2018-12-30 20:22:54 +01:00
|
|
|
"golang.org/x/sys/unix"
|
|
|
|
)
|
2018-10-11 21:17:43 +02:00
|
|
|
|
2018-12-30 20:22:54 +01:00
|
|
|
func SocketPair() (a, b *net.UnixConn, err error) {
|
2018-10-11 21:17:43 +02:00
|
|
|
// don't use net.Pipe, as it doesn't implement things like lingering, which our code relies on
|
|
|
|
sockpair, err := unix.Socketpair(unix.AF_UNIX, unix.SOCK_STREAM, 0)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
2018-12-30 20:22:54 +01:00
|
|
|
toConn := func(fd int) (*net.UnixConn, error) {
|
2018-10-11 21:17:43 +02:00
|
|
|
f := os.NewFile(uintptr(fd), "fileconn")
|
|
|
|
if f == nil {
|
|
|
|
panic(fd)
|
|
|
|
}
|
|
|
|
c, err := net.FileConn(f)
|
2018-12-30 20:22:54 +01:00
|
|
|
f.Close() // net.FileConn uses dup under the hood
|
2018-10-11 21:17:43 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-12-30 20:22:54 +01:00
|
|
|
// strictly, the following type assertion is an implementation detail
|
|
|
|
// however, will be caught by test TestSocketPairWorks
|
|
|
|
fileConnIsUnixConn := c.(*net.UnixConn)
|
|
|
|
return fileConnIsUnixConn, nil
|
2018-10-11 21:17:43 +02:00
|
|
|
}
|
|
|
|
if a, err = toConn(sockpair[0]); err != nil { // shadowing
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
if b, err = toConn(sockpair[1]); err != nil { // shadowing
|
|
|
|
a.Close()
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
return a, b, nil
|
|
|
|
}
|