mirror of
https://github.com/linuxkit/linuxkit.git
synced 2025-07-27 04:28:20 +00:00
proxy: vsock connections support CloseRead and CloseWrite
This patch adds a wrapper around the `net.Conn` to include the `CloseRead` and `CloseWrite` implementations. This patch also exposes the `VsockAddr` type, which is similar to `TCPAddr` and `UDPAddr`. Signed-off-by: David Scott <dave.scott@docker.com>
This commit is contained in:
parent
3c68728e9f
commit
2f80e98e7f
75
alpine/packages/proxy/vendor/vsock/vsock.go
vendored
75
alpine/packages/proxy/vendor/vsock/vsock.go
vendored
@ -26,8 +26,8 @@ int accept_vm(int fd, struct sockaddr_vm *sa_vm, socklen_t *sa_vm_len) {
|
|||||||
import "C"
|
import "C"
|
||||||
|
|
||||||
const (
|
const (
|
||||||
AF_VSOCK = 40
|
AF_VSOCK = 40
|
||||||
VSOCK_CID_ANY = 4294967295 /* 2^32-1 */
|
VSOCK_CID_ANY = 4294967295 /* 2^32-1 */
|
||||||
)
|
)
|
||||||
|
|
||||||
// Listen returns a net.Listener which can accept connections on the given
|
// Listen returns a net.Listener which can accept connections on the given
|
||||||
@ -54,6 +54,13 @@ func Listen(port uint) (net.Listener, error) {
|
|||||||
return &vsockListener{accept_fd, port}, nil
|
return &vsockListener{accept_fd, port}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Conn is a vsock connection which support half-close.
|
||||||
|
type Conn interface {
|
||||||
|
net.Conn
|
||||||
|
CloseRead() error
|
||||||
|
CloseWrite() error
|
||||||
|
}
|
||||||
|
|
||||||
type vsockListener struct {
|
type vsockListener struct {
|
||||||
accept_fd int
|
accept_fd int
|
||||||
port uint
|
port uint
|
||||||
@ -68,34 +75,64 @@ func (v *vsockListener) Accept() (net.Conn, error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
vsock := os.NewFile(uintptr(fd), fmt.Sprintf("vsock:%d", fd))
|
return newVsockConn(uintptr(fd), v.port)
|
||||||
conn, err := net.FileConn(vsock)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return conn, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (v *vsockListener) Close() error {
|
func (v *vsockListener) Close() error {
|
||||||
return syscall.Close(v.accept_fd)
|
return syscall.Close(v.accept_fd)
|
||||||
}
|
}
|
||||||
|
|
||||||
type vsockAddr struct {
|
type VsockAddr struct {
|
||||||
network string
|
Port uint
|
||||||
addr string
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *vsockAddr) Network() string {
|
func (a VsockAddr) Network() string {
|
||||||
return a.network
|
return "vsock"
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *vsockAddr) String() string {
|
func (a VsockAddr) String() string {
|
||||||
return a.addr
|
return fmt.Sprintf("%08x", a.Port)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (v *vsockListener) Addr() net.Addr {
|
func (v *vsockListener) Addr() net.Addr {
|
||||||
return &vsockAddr{
|
return VsockAddr{Port: v.port}
|
||||||
network: "vsock",
|
}
|
||||||
addr: fmt.Sprintf("%08x", v.port),
|
|
||||||
}
|
// a wrapper around FileConn which supports CloseRead and CloseWrite
|
||||||
|
type vsockConn struct {
|
||||||
|
net.Conn
|
||||||
|
fd uintptr
|
||||||
|
local VsockAddr
|
||||||
|
remote VsockAddr
|
||||||
|
}
|
||||||
|
|
||||||
|
type VsockConn struct {
|
||||||
|
vsockConn
|
||||||
|
}
|
||||||
|
|
||||||
|
func newVsockConn(fd uintptr, localPort uint) (*VsockConn, error) {
|
||||||
|
vsock := os.NewFile(fd, fmt.Sprintf("vsock:%d", fd))
|
||||||
|
conn, err := net.FileConn(vsock)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
local := VsockAddr{Port: localPort}
|
||||||
|
remote := VsockAddr{Port: uint(0)} // FIXME
|
||||||
|
return &VsockConn{vsockConn{Conn: conn, fd: fd, local: local, remote: remote}}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (v *VsockConn) LocalAddr() net.Addr {
|
||||||
|
return v.local
|
||||||
|
}
|
||||||
|
|
||||||
|
func (v *VsockConn) RemoteAddr() net.Addr {
|
||||||
|
return v.remote
|
||||||
|
}
|
||||||
|
|
||||||
|
func (v *VsockConn) CloseRead() error {
|
||||||
|
return syscall.Shutdown(int(v.fd), syscall.SHUT_RD)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (v *VsockConn) CloseWrite() error {
|
||||||
|
return syscall.Shutdown(int(v.fd), syscall.SHUT_WR)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user