Skip to content

Commit

Permalink
[BROKEN] Add Socket::recvmsg
Browse files Browse the repository at this point in the history
This doesn't work on Windows as the WSARecvMsg definition is missing,
see microsoft/windows-rs#2530.
  • Loading branch information
Thomasdezeeuw committed Jun 3, 2023
1 parent 5ed3b0f commit 0b9ee46
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 2 deletions.
13 changes: 12 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, MsgHdr, RecvFlags};
use crate::{MaybeUninitSlice, MsgHdr, MsgHdrMut, RecvFlags};

/// Owned wrapper around a system socket.
///
Expand Down Expand Up @@ -627,6 +627,17 @@ impl Socket {
sys::peek_sender(self.as_raw())
}

/// Receive a message from a socket using a message structure.
///
/// The `flags` are ignored on Windows (as the `WSARecvMsg` system call
/// doesn't accept flags).
#[doc = man_links!(recvmsg(2))]
#[cfg(not(target_os = "redox"))]
#[cfg_attr(docsrs, doc(cfg(not(target_os = "redox"))))]
pub fn recvmsg(&self, msg: &mut MsgHdrMut<'_, '_, '_>, flags: sys::c_int) -> io::Result<usize> {
sys::recvmsg(self.as_raw(), msg, flags)
}

/// Sends data on the socket to a connected peer.
///
/// This is typically used on TCP sockets or datagram sockets which have
Expand Down
6 changes: 5 additions & 1 deletion src/sys/unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1010,7 +1010,11 @@ pub(crate) fn recv_from_vectored(
}

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

Expand Down
14 changes: 14 additions & 0 deletions src/sys/windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -567,6 +567,20 @@ pub(crate) fn recv_from_vectored(
.map(|((n, recv_flags), addr)| (n, recv_flags, addr))
}

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

pub(crate) fn send(socket: Socket, buf: &[u8], flags: c_int) -> io::Result<usize> {
syscall!(
send(
Expand Down

0 comments on commit 0b9ee46

Please sign in to comment.