tokio-rs / io-uring

The `io_uring` library for Rust
Apache License 2.0
1.19k stars 131 forks source link

Need help understanding `RecvMsg` #269

Open espoal opened 6 months ago

espoal commented 6 months ago

I'm trying to build an example io_uring echo udp server here. Basically since recv_from is not available, I need to use recvmsg and somehow retrieve the source address to respond.

My first question is why this work:

let mut msg_hdr: libc::msghdr = unsafe { std::mem::zeroed() };
// I copied this from tokio/io-uring test code, I don't know why it works
// https://github.com/tokio-rs/io-uring/blob/master/io-uring-test/src/tests/net.rs
msg_hdr.msg_namelen = 16;

If I set msg_namelen to anything but 0 and 16 then I get parsing errors. If I set to 0 the msghdr is always empty (excpet the payload size). If I set to 16 then I get some data like:

msg_out: RecvMsgOut { header: io_uring_recvmsg_out { namelen: 16, controllen: 0, payloadlen: 5, flags: 0 }, msghdr_name_len: 16, name_data: [2, 0, 129, 54, 127, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0], control_data: [], payload_data: [107, 107, 107, 107, 10] }
payload: "kkkk\n"
buffer: [16, 0, 0, 0, 0, 0, 0, 0, 5, 0, 0, 0, 0, 0, 0, 0, 2, 0, 129, 54, 127, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 107, 107, 107, 107, 10]
name_data: [2, 0, 129, 54, 127, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0]

But I don't know how to interpret this. How do I use this data to create a file descriptor for SendMsg? Should I maybe set somehow the IP_PKTINFO flag?