Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Socket::{send,recv}msg and MsgHdr(Mut) #447

Merged
merged 10 commits into from
Jun 8, 2023
Prev Previous commit
Next Next commit
Add Socket::sendmsg
Wrapper around sendmsg(2).
  • Loading branch information
Thomasdezeeuw committed Jun 2, 2023
commit eb6e83341832af988f4579d4bf94a6eb416d99e4
10 changes: 9 additions & 1 deletion src/socket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use std::time::Duration;
use crate::sys::{self, c_int, getsockopt, setsockopt, Bool};
use crate::{Domain, Protocol, SockAddr, TcpKeepalive, Type};
#[cfg(not(target_os = "redox"))]
use crate::{MaybeUninitSlice, RecvFlags};
use crate::{MaybeUninitSlice, MsgHdr, RecvFlags};

/// Owned wrapper around a system socket.
///
Expand Down Expand Up @@ -725,6 +725,14 @@ impl Socket {
) -> io::Result<usize> {
sys::send_to_vectored(self.as_raw(), bufs, addr, flags)
}

/// Send a message on a socket using a message structure.
#[doc = man_links!(sendmsg(2))]
#[cfg(not(target_os = "redox"))]
#[cfg_attr(docsrs, doc(cfg(not(target_os = "redox"))))]
pub fn sendmsg(&self, msg: &MsgHdr<'_, '_, '_>, flags: sys::c_int) -> io::Result<usize> {
sys::sendmsg(self.as_raw(), msg, flags)
}
}

/// Set `SOCK_CLOEXEC` and `NO_HANDLE_INHERIT` on the `ty`pe on platforms that
Expand Down
2 changes: 1 addition & 1 deletion src/sys/unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1064,7 +1064,7 @@ pub(crate) fn send_to_vectored(
}

#[cfg(not(target_os = "redox"))]
fn sendmsg(fd: Socket, msg: &MsgHdr<'_, '_, '_>, flags: c_int) -> io::Result<usize> {
pub(crate) fn sendmsg(fd: Socket, msg: &MsgHdr<'_, '_, '_>, flags: c_int) -> io::Result<usize> {
syscall!(sendmsg(fd, &msg.inner, flags)).map(|n| n as usize)
}

Expand Down
19 changes: 18 additions & 1 deletion src/sys/windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ use windows_sys::Win32::Networking::WinSock::{
};
use windows_sys::Win32::System::Threading::INFINITE;

use crate::{RecvFlags, SockAddr, TcpKeepalive, Type};
use crate::{MsgHdr, RecvFlags, SockAddr, TcpKeepalive, Type};

#[allow(non_camel_case_types)]
pub(crate) type c_int = std::os::raw::c_int;
Expand Down Expand Up @@ -659,6 +659,23 @@ pub(crate) fn send_to_vectored(
.map(|_| nsent as usize)
}

pub(crate) fn sendmsg(socket: Socket, msg: &MsgHdr<'_, '_, '_>, flags: c_int) -> io::Result<usize> {
let mut nsent = 0;
syscall!(
WSASendMsg(
socket,
&msg.inner,
flags as u32,
&mut nsent,
ptr::null_mut(),
None,
),
PartialEq::eq,
SOCKET_ERROR
)
.map(|_| nsent as usize)
}

/// Wrapper around `getsockopt` to deal with platform specific timeouts.
pub(crate) fn timeout_opt(fd: Socket, lvl: c_int, name: i32) -> io::Result<Option<Duration>> {
unsafe { getsockopt(fd, lvl, name).map(from_ms) }
Expand Down
18 changes: 18 additions & 0 deletions tests/socket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -703,6 +703,24 @@ fn send_from_recv_to_vectored() {
assert_eq!(unsafe { assume_init(&swear) }, b"swear");
}

#[test]
#[cfg(not(target_os = "redox"))]
fn sendmsg() {
let (socket_a, socket_b) = udp_pair_unconnected();

const DATA: &[u8] = b"Hello, World!";

let bufs = &[IoSlice::new(DATA)];
let addr_b = socket_b.local_addr().unwrap();
let msg = socket2::MsgHdr::new().with_addr(&addr_b).with_buffers(bufs);
let sent = socket_a.sendmsg(&msg, 0).unwrap();
assert_eq!(sent, DATA.len());

let mut buf = Vec::with_capacity(DATA.len() + 1);
let received = socket_b.recv(buf.spare_capacity_mut()).unwrap();
assert_eq!(received, DATA.len());
}

#[test]
#[cfg(not(target_os = "redox"))]
fn recv_vectored_truncated() {
Expand Down