move msghdr to Go

This commit is contained in:
Josh Bleecher Snyder
2021-06-01 15:53:29 -07:00
parent fcdc9086a2
commit f75d32151b
2 changed files with 6 additions and 12 deletions

View File

@@ -15,6 +15,7 @@
// TODO: use fixed buffers? https://unixism.net/loti/tutorial/fixed_buffers.html
typedef struct io_uring go_uring;
typedef struct msghdr go_msghdr;
// Wait for a completion to be available, fetch the data
static int receive_into(int sock, struct io_uring *ring, char *ip, uint16_t *port) {
@@ -50,7 +51,6 @@ again:;
free(mhdr->msg_iov);
free(mhdr->msg_name);
free(mhdr);
io_uring_cqe_seen(ring, cqe);
return n;
@@ -58,17 +58,10 @@ again:;
// submit a recvmsg request via liburing
// TODO: What recvfrom support arrives, maybe use that instead?
static int submit_recvmsg_request(int sock, struct io_uring *ring, char *buf, int buflen) {
struct msghdr *mhdr = malloc(sizeof(struct msghdr));
if (!mhdr) {
perror("malloc(msghdr)");
return 1;
}
static int submit_recvmsg_request(int sock, struct io_uring *ring, struct msghdr *mhdr, char *buf, int buflen) {
struct iovec *iov = malloc(sizeof(struct iovec));
if (!iov) {
perror("malloc(iov)");
free(iov);
return 1;
}
@@ -76,7 +69,6 @@ static int submit_recvmsg_request(int sock, struct io_uring *ring, char *buf, in
if (!sender) {
perror("malloc(sender)");
free(iov);
free(mhdr);
return 1;
}
@@ -84,7 +76,6 @@ static int submit_recvmsg_request(int sock, struct io_uring *ring, char *buf, in
iov->iov_base = buf;
iov->iov_len = buflen;
memset(mhdr, 0, sizeof(*mhdr));
mhdr->msg_iov = iov;
mhdr->msg_iovlen = 1;

View File

@@ -9,6 +9,7 @@
"fmt"
"net"
"os"
"runtime"
"sync"
"time"
"unsafe"
@@ -69,8 +70,9 @@ func (u *UDPConn) ReadFromNetaddr(buf []byte) (int, netaddr.IPPort, error) {
if u.fd == 0 {
return 0, netaddr.IPPort{}, errors.New("invalid uring.UDPConn")
}
mhdr := new(C.go_msghdr)
// TODO: eventually separate submitting the request and waiting for the response.
errno := C.submit_recvmsg_request(u.fd, u.ptr, (*C.char)(unsafe.Pointer(&buf[0])), C.int(len(buf)))
errno := C.submit_recvmsg_request(u.fd, u.ptr, mhdr, (*C.char)(unsafe.Pointer(&buf[0])), C.int(len(buf)))
if errno < 0 {
return 0, netaddr.IPPort{}, fmt.Errorf("uring.UDPConn recv failed: %v", errno) // TODO: Improve errno
}
@@ -82,6 +84,7 @@ func (u *UDPConn) ReadFromNetaddr(buf []byte) (int, netaddr.IPPort, error) {
return 0, netaddr.IPPort{}, errors.New("something wrong")
}
ipp := netaddr.IPPortFrom(netaddr.IPFrom4(*a), uint16(port))
runtime.KeepAlive(mhdr)
return int(n), ipp, nil
}