Skip to content

Add support for sendmsg(2), recvmsg(2), and cmsg(3), take 2 #197

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

Merged
merged 1 commit into from
Oct 8, 2015

Conversation

geofft
Copy link
Contributor

@geofft geofft commented Oct 6, 2015

I've confirmed that the following changes fix the reported Valgrind problem with PR #179, on both Linux and OS X 10.8.5 amd64:

@@ -122,3 +122,3 @@ struct cmsghdr {
     pub cmsg_type: c_int,
-    cmsg_data: [size_t; 0]
+    cmsg_data: [type_of_cmsg_len; 0]
 }
@@ -286,4 +286,6 @@ impl<'a> ControlMessage<'a> {
 pub fn sendmsg<'a>(fd: RawFd, iov: &[IoVec<&'a [u8]>], cmsgs: &[ControlMessage<'a>], flags: SockMessageFlags, addr: Option<&'a SockAddr>) -> Result<usize> {
+    let mut len = 0;
     let mut capacity = 0;
     for cmsg in cmsgs {
+        len += cmsg.len();
         capacity += cmsg.space();
@@ -317,3 +319,3 @@ pub fn sendmsg<'a>(fd: RawFd, iov: &[IoVec<&'a [u8]>], cmsgs: &[ControlMessage<'
         msg_control: cmsg_buffer.as_ptr() as *const c_void,
-        msg_controllen: cmsg_buffer.len() as size_t,
+        msg_controllen: len as size_t,
         msg_flags: 0,

Or in other words, fix the alignment of CMSG_DATA, and set msg_controllen to the actual length, not just the (rounded-up) length of the allocated buffer.

This is a resend of the reverted commit with those changes folded in (and, uh, fixing cmsg(4) to cmsg(3) in the commit message, idk why I thought it was in section 4).

The best specification for control message layout appears to be
[RFC 2292, section 4](https://tools.ietf.org/html/rfc2292#section-4),
despite this not being a wire protocol. These definitions have also been
checked against glibc 2.19 <bits/socket.h> and Linux 4.0
<linux/socket.h>, and tested on Debian 8.1 and FreeBSD 10.2 x86_64.

The API differs a bit from the cmsg(3) API for type-safety reasons (and
also because the cmsg(3) API is terrible). See test/sys/test_socket.rs
for an example.

Only supports SCM_RIGHTS at the moment.

Fixes nix-rust#88.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants