Skip to content

Commit 9d4a777

Browse files
committed
Add Socket::send_msg_with_flags and Socket::recv_msg_with_flags
Motivation: Existing methods send_msg and recv_msg do not expose the full capabilities of the sendmsg() and recvmsg() syscalls. Changes: Add these to methods that expose the flag argument. Rewrite send_msg and recv_msg to use the _with_flags_version
1 parent e23e390 commit 9d4a777

File tree

1 file changed

+14
-4
lines changed
  • library/std/src/sys/net/connection/socket

1 file changed

+14
-4
lines changed

library/std/src/sys/net/connection/socket/unix.rs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -362,11 +362,16 @@ impl Socket {
362362
}
363363

364364
#[cfg(any(target_os = "android", target_os = "linux", target_os = "cygwin"))]
365-
pub fn recv_msg(&self, msg: &mut libc::msghdr) -> io::Result<usize> {
366-
let n = cvt(unsafe { libc::recvmsg(self.as_raw_fd(), msg, libc::MSG_CMSG_CLOEXEC) })?;
365+
pub fn recv_msg_with_flags(&self, msg: &mut libc::msghdr, flags: c_int) -> io::Result<usize> {
366+
let n = cvt(unsafe { libc::recvmsg(self.as_raw_fd(), msg, flags) })?;
367367
Ok(n as usize)
368368
}
369369

370+
#[cfg(any(target_os = "android", target_os = "linux", target_os = "cygwin"))]
371+
pub fn recv_msg(&self, msg: &mut libc::msghdr) -> io::Result<usize> {
372+
self.recv_msg_with_flags(msg, libc::MSG_CMSG_CLOEXEC)
373+
}
374+
370375
pub fn peek_from(&self, buf: &mut [u8]) -> io::Result<(usize, SocketAddr)> {
371376
self.recv_from_with_flags(buf, MSG_PEEK)
372377
}
@@ -385,11 +390,16 @@ impl Socket {
385390
}
386391

387392
#[cfg(any(target_os = "android", target_os = "linux", target_os = "cygwin"))]
388-
pub fn send_msg(&self, msg: &libc::msghdr) -> io::Result<usize> {
389-
let n = cvt(unsafe { libc::sendmsg(self.as_raw_fd(), msg, 0) })?;
393+
pub fn send_msg_with_flags(&self, msg: &libc::msghdr, flags: c_int) -> io::Result<usize> {
394+
let n = cvt(unsafe { libc::sendmsg(self.as_raw_fd(), msg, flags) })?;
390395
Ok(n as usize)
391396
}
392397

398+
#[cfg(any(target_os = "android", target_os = "linux", target_os = "cygwin"))]
399+
pub fn send_msg(&self, msg: &libc::msghdr) -> io::Result<usize> {
400+
self.send_msg_with_flags(msg, 0)
401+
}
402+
393403
pub fn set_timeout(&self, dur: Option<Duration>, kind: libc::c_int) -> io::Result<()> {
394404
let timeout = match dur {
395405
Some(dur) => {

0 commit comments

Comments
 (0)