Skip to content

x/sys/unix: add RecvmsgBuffers and SendmsgBuffers #52885

@ianlancetaylor

Description

@ianlancetaylor

The golang.org/x/sys/unix package was created with Recvmsg and Sendmsg functions copied from the ones in the syscall package. Those date back to https://go.dev/cl/2331044, committed in 2010 for #1101. Unfortunately, those Recvmsg and Sendmsg functions do not correspond to the recvmsg and sendmsg system calls. The system calls take a pointer to a Msghdr struct. The Recvmsg and Sendmsg functions build a Msghdr instance, but they don't support the full functionality of recvmsg and sendmsg: they don't permit setting up the scatter/gather array.

Working directly with a Msghdr is awkward for the x/sys/unix routines. It seems better to provide some translation for the socket address. Still, we should provide access to the scatter/gather array. I propose the following:

// RecvmsgBufs receives a message from a socket using the recvmsg system call.
// The flags are passed to recvmsg. Any non-control data read is scattered into bufs. The results are:
//  - n is the number of non-control data read into bufs
//  - oobn is the number of control data read into oob; this may be interpreted using [ParseSocketControlMessage]
//  - recvflags is flags returned by recvmsg
//  - from is the address of the sender
func RecvmsgBufs(fd int, bufs [][]byte, oob []byte, flags int) (n, oobn int, recvflags int, from Sockaddr, err error)

// SendmsgBufs sends a message on a socket to an address using the sendmsg system call.
// The flags are passed to sendmsg. Any non-control data writen is gathered from bufs.
// The function returns the number of bytes written to the socket.
func SendmsgBufs(fd int, bufs [][]byte, oob []byte, to Sockaddr, flags int) (int, error)

Part of the goal of these functions is to simply the golang.org/x/net/internal/socket package, which currently reaches into the syscall package for syscall.recvmsg and syscall.sendmsg.

CC @tklauser @mdlayher

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions