Skip to content

Commit 4c4f3c3

Browse files
committed
Update cmsg types to match structs on non-Linux OSes
1 parent 20ce7a1 commit 4c4f3c3

File tree

2 files changed

+17
-8
lines changed

2 files changed

+17
-8
lines changed

src/sys/socket/ffi.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ pub use libc::{socket, listen, bind, accept, connect, setsockopt, sendto, recvfr
55

66
use libc::{c_int, c_void, socklen_t, size_t, ssize_t};
77

8-
#[cfg(target_os = "macos")]
8+
#[cfg(not(target_os = "linux"))]
99
use libc::c_uint;
1010

1111
use sys::uio::IoVec;
@@ -23,6 +23,14 @@ pub type type_of_cmsg_data = c_uint;
2323
#[cfg(not(target_os = "macos"))]
2424
pub type type_of_cmsg_data = size_t;
2525

26+
#[cfg(target_os = "linux")]
27+
pub type type_of_msg_iovlen = size_t;
28+
29+
#[cfg(not(target_os = "linux"))]
30+
pub type type_of_msg_iovlen = c_uint;
31+
32+
33+
2634
// Private because we don't expose any external functions that operate
2735
// directly on this type; we just use it internally at FFI boundaries.
2836
// Note that in some cases we store pointers in *const fields that the
@@ -33,9 +41,9 @@ pub struct msghdr<'a> {
3341
pub msg_name: *const c_void,
3442
pub msg_namelen: socklen_t,
3543
pub msg_iov: *const IoVec<&'a [u8]>,
36-
pub msg_iovlen: size_t,
44+
pub msg_iovlen: type_of_msg_iovlen,
3745
pub msg_control: *const c_void,
38-
pub msg_controllen: size_t,
46+
pub msg_controllen: type_of_cmsg_len,
3947
pub msg_flags: c_int,
4048
}
4149

src/sys/socket/mod.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ unsafe fn copy_bytes<'a, 'b, T: ?Sized>(src: &T, dst: &'a mut &'b mut [u8]) {
8585
}
8686

8787

88-
use self::ffi::{cmsghdr, msghdr, type_of_cmsg_len, type_of_cmsg_data};
88+
use self::ffi::{cmsghdr, msghdr, type_of_cmsg_len, type_of_cmsg_data, type_of_msg_iovlen};
8989

9090
/// A structure used to make room in a cmsghdr passed to recvmsg. The
9191
/// size and alignment match that of a cmsghdr followed by a T, but the
@@ -152,6 +152,7 @@ impl<'a> Iterator for CmsgIterator<'a> {
152152

153153
// Advance our internal pointer.
154154
if cmsg_align(cmsg_len) > buf.len() {
155+
println!("cmsg_align(cmsg_len) > buf.len(): {} > {}", cmsg_align(cmsg_len), buf.len());
155156
return None;
156157
}
157158
self.0 = &buf[cmsg_align(cmsg_len)..];
@@ -287,9 +288,9 @@ pub fn sendmsg<'a>(fd: RawFd, iov: &[IoVec<&'a [u8]>], cmsgs: &[ControlMessage<'
287288
msg_name: name as *const c_void,
288289
msg_namelen: namelen,
289290
msg_iov: iov.as_ptr(),
290-
msg_iovlen: iov.len() as size_t,
291+
msg_iovlen: iov.len() as type_of_msg_iovlen,
291292
msg_control: cmsg_ptr,
292-
msg_controllen: capacity as size_t,
293+
msg_controllen: capacity as type_of_cmsg_len,
293294
msg_flags: 0,
294295
};
295296
let ret = unsafe { ffi::sendmsg(fd, &mhdr, flags.bits()) };
@@ -310,9 +311,9 @@ pub fn recvmsg<'a, T>(fd: RawFd, iov: &[IoVec<&mut [u8]>], cmsg_buffer: Option<&
310311
msg_name: &mut address as *const _ as *const c_void,
311312
msg_namelen: mem::size_of::<sockaddr_storage>() as socklen_t,
312313
msg_iov: iov.as_ptr() as *const IoVec<&[u8]>, // safe cast to add const-ness
313-
msg_iovlen: iov.len() as size_t,
314+
msg_iovlen: iov.len() as type_of_msg_iovlen,
314315
msg_control: msg_control as *const c_void,
315-
msg_controllen: msg_controllen as size_t,
316+
msg_controllen: msg_controllen as type_of_cmsg_len,
316317
msg_flags: 0,
317318
};
318319
let ret = unsafe { ffi::recvmsg(fd, &mut mhdr, flags.bits()) };

0 commit comments

Comments
 (0)