Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
1048: Restored the bytes field on RecvMsg r=asomers a=vdagonneau

This pull request restores the `bytes` field on the `RecvMsg` structure in order to be able to know the amount of bytes read during a call to `recvmsg`.

Co-authored-by: Vincent Dagonneau <[email protected]>
  • Loading branch information
bors[bot] and vdagonneau committed May 5, 2019
2 parents 71b35a0 + cffb4b3 commit 63aec50
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/sys/socket/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,7 @@ impl CmsgBuffer for Vec<u8> {

#[allow(missing_debug_implementations)] // msghdr isn't Debug
pub struct RecvMsg<'a> {
pub bytes: usize,
cmsghdr: Option<&'a cmsghdr>,
pub address: Option<SockAddr>,
pub flags: MsgFlags,
Expand Down Expand Up @@ -970,7 +971,7 @@ pub fn recvmsg<'a>(fd: RawFd, iov: &[IoVec<&mut [u8]>],

let ret = unsafe { libc::recvmsg(fd, &mut mhdr, flags.bits()) };

Errno::result(ret).map(|_| {
Errno::result(ret).map(|r| {
let cmsghdr = unsafe {
if mhdr.msg_controllen > 0 {
// got control message(s)
Expand All @@ -986,6 +987,7 @@ pub fn recvmsg<'a>(fd: RawFd, iov: &[IoVec<&mut [u8]>],
sockaddr_storage_to_addr(&address, mhdr.msg_namelen as usize).ok()
};
RecvMsg {
bytes: r as usize,
cmsghdr,
address,
flags: MsgFlags::from_bits_truncate(mhdr.msg_flags),
Expand Down
8 changes: 8 additions & 0 deletions test/sys/test_socket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ pub fn test_scm_rights() {
panic!("unexpected cmsg");
}
}
assert_eq!(msg.bytes, 5);
assert!(!msg.flags.intersects(MsgFlags::MSG_TRUNC | MsgFlags::MSG_CTRUNC));
close(fd2).unwrap();
}
Expand Down Expand Up @@ -373,6 +374,7 @@ fn test_scm_rights_single_cmsg_multiple_fds() {
}
assert!(cmsgs.next().is_none(), "unexpected control msg");

assert_eq!(msg.bytes, 8);
assert_eq!(iovec[0].as_slice(), [1u8, 2, 3, 4, 5, 6, 7, 8]);
});

Expand Down Expand Up @@ -414,6 +416,7 @@ pub fn test_sendmsg_empty_cmsgs() {
panic!("unexpected cmsg");
}
assert!(!msg.flags.intersects(MsgFlags::MSG_TRUNC | MsgFlags::MSG_CTRUNC));
assert_eq!(msg.bytes, 5);
close(fd2).unwrap();
}
}
Expand Down Expand Up @@ -464,6 +467,7 @@ fn test_scm_credentials() {
}
}
received_cred.expect("no creds received");
assert_eq!(msg.bytes, 5);
assert!(!msg.flags.intersects(MsgFlags::MSG_TRUNC | MsgFlags::MSG_CTRUNC));
close(recv).unwrap();
}
Expand Down Expand Up @@ -555,6 +559,7 @@ fn test_impl_scm_credentials_and_rights(mut space: Vec<u8>) {
}
}
received_cred.expect("no creds received");
assert_eq!(msg.bytes, 5);
assert!(!msg.flags.intersects(MsgFlags::MSG_TRUNC | MsgFlags::MSG_CTRUNC));
close(recv).unwrap();
}
Expand Down Expand Up @@ -754,6 +759,7 @@ pub fn test_recv_ipv4pktinfo() {
_ => (),
}
assert!(cmsgs.next().is_none(), "unexpected additional control msg");
assert_eq!(msg.bytes, 8);
assert_eq!(
iovec[0].as_slice(),
[1u8, 2, 3, 4, 5, 6, 7, 8]
Expand Down Expand Up @@ -862,6 +868,7 @@ pub fn test_recvif() {
}
assert_eq!(rx_recvif, true);
assert_eq!(rx_recvdstaddr, true);
assert_eq!(msg.bytes, 8);
assert_eq!(
iovec[0].as_slice(),
[1u8, 2, 3, 4, 5, 6, 7, 8]
Expand Down Expand Up @@ -953,6 +960,7 @@ pub fn test_recv_ipv6pktinfo() {
_ => (),
}
assert!(cmsgs.next().is_none(), "unexpected additional control msg");
assert_eq!(msg.bytes, 8);
assert_eq!(
iovec[0].as_slice(),
[1u8, 2, 3, 4, 5, 6, 7, 8]
Expand Down

0 comments on commit 63aec50

Please sign in to comment.