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::recvmsg
  • Loading branch information
Thomasdezeeuw committed Jun 7, 2023
commit 37a4266d63072db4b3d471008ee551c5669b15da
15 changes: 15 additions & 0 deletions src/socket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ use std::os::windows::io::{FromRawSocket, IntoRawSocket};
use std::time::Duration;

use crate::sys::{self, c_int, getsockopt, setsockopt, Bool};
#[cfg(all(unix, not(target_os = "redox")))]
use crate::MsgHdrMut;
use crate::{Domain, Protocol, SockAddr, TcpKeepalive, Type};
#[cfg(not(target_os = "redox"))]
use crate::{MaybeUninitSlice, MsgHdr, RecvFlags};
Expand Down Expand Up @@ -627,6 +629,19 @@ impl Socket {
sys::peek_sender(self.as_raw())
}

/// Receive a message from a socket using a message structure.
///
/// This is not supported on Windows as calling `WSARecvMsg` (the `recvmsg`
/// equivalent) is not straight forward on Windows. See
/// <https://github.com/microsoft/Windows-classic-samples/blob/7cbd99ac1d2b4a0beffbaba29ea63d024ceff700/Samples/Win7Samples/netds/winsock/recvmsg/rmmc.cpp>
/// for an example (in C++).
#[doc = man_links!(recvmsg(2))]
#[cfg(all(unix, not(target_os = "redox")))]
#[cfg_attr(docsrs, doc(cfg(all(unix, 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
2 changes: 1 addition & 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::{MsgHdr, MsgHdrMut, 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