Skip to content

Commit d77477e

Browse files
committed
Fix type mismatching for different OSes.
1 parent 4e9714b commit d77477e

File tree

1 file changed

+105
-41
lines changed

1 file changed

+105
-41
lines changed

library/std/src/sys/unix/ext/net/ancillary.rs

Lines changed: 105 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use crate::marker::PhantomData;
44
use crate::mem::{size_of, zeroed};
55
use crate::os::unix::io::RawFd;
66
use crate::path::Path;
7-
use crate::ptr::{null_mut, read_unaligned};
7+
use crate::ptr::read_unaligned;
88
use crate::slice::from_raw_parts;
99
use crate::sys::unix::ext::net::addr::{sockaddr_un, SocketAddr};
1010
use crate::sys::unix::net::Socket;
@@ -20,19 +20,31 @@ pub(super) fn recv_vectored_with_ancillary_from(
2020
unsafe {
2121
let mut msg_name: libc::sockaddr_un = zeroed();
2222

23-
let mut msg = libc::msghdr {
24-
msg_name: &mut msg_name as *mut _ as *mut _,
25-
msg_namelen: size_of::<libc::sockaddr_un>() as libc::socklen_t,
26-
msg_iov: bufs.as_mut_ptr().cast(),
27-
msg_iovlen: bufs.len(),
28-
msg_control: ancillary.buffer.as_mut_ptr().cast(),
29-
msg_controllen: ancillary.buffer.len(),
30-
msg_flags: 0,
31-
};
23+
let mut msg: libc::msghdr = zeroed();
24+
msg.msg_name = &mut msg_name as *mut _ as *mut _;
25+
msg.msg_namelen = size_of::<libc::sockaddr_un>() as libc::socklen_t;
26+
msg.msg_iov = bufs.as_mut_ptr().cast();
27+
msg.msg_control = ancillary.buffer.as_mut_ptr().cast();
28+
cfg_if::cfg_if! {
29+
if #[cfg(any(target_os = "android", all(target_os = "linux", target_env = "gnu")))] {
30+
msg.msg_iovlen = bufs.len() as libc::size_t;
31+
msg.msg_controllen = ancillary.buffer.len() as libc::size_t;
32+
} else if #[cfg(any(
33+
target_os = "dragonfly",
34+
target_os = "emscripten",
35+
target_os = "freebsd",
36+
all(target_os = "linux", target_env = "musl",),
37+
target_os = "netbsd",
38+
target_os = "openbsd",
39+
))] {
40+
msg.msg_iovlen = bufs.len() as libc::c_int;
41+
msg.msg_controllen = ancillary.buffer.len() as libc::socklen_t;
42+
}
43+
}
3244

3345
let count = socket.recv_msg(&mut msg)?;
3446

35-
ancillary.length = msg.msg_controllen;
47+
ancillary.length = msg.msg_controllen as usize;
3648
ancillary.truncated = msg.msg_flags & libc::MSG_CTRUNC == libc::MSG_CTRUNC;
3749

3850
let truncated = msg.msg_flags & libc::MSG_TRUNC == libc::MSG_TRUNC;
@@ -52,15 +64,27 @@ pub(super) fn send_vectored_with_ancillary_to(
5264
let (mut msg_name, msg_namelen) =
5365
if let Some(path) = path { sockaddr_un(path)? } else { (zeroed(), 0) };
5466

55-
let mut msg = libc::msghdr {
56-
msg_name: &mut msg_name as *mut _ as *mut _,
57-
msg_namelen,
58-
msg_iov: bufs.as_mut_ptr().cast(),
59-
msg_iovlen: bufs.len(),
60-
msg_control: ancillary.buffer.as_mut_ptr().cast(),
61-
msg_controllen: ancillary.length,
62-
msg_flags: 0,
63-
};
67+
let mut msg: libc::msghdr = zeroed();
68+
msg.msg_name = &mut msg_name as *mut _ as *mut _;
69+
msg.msg_namelen = msg_namelen;
70+
msg.msg_iov = bufs.as_mut_ptr().cast();
71+
msg.msg_control = ancillary.buffer.as_mut_ptr().cast();
72+
cfg_if::cfg_if! {
73+
if #[cfg(any(target_os = "android", all(target_os = "linux", target_env = "gnu")))] {
74+
msg.msg_iovlen = bufs.len() as libc::size_t;
75+
msg.msg_controllen = ancillary.length as libc::size_t;
76+
} else if #[cfg(any(
77+
target_os = "dragonfly",
78+
target_os = "emscripten",
79+
target_os = "freebsd",
80+
all(target_os = "linux", target_env = "musl",),
81+
target_os = "netbsd",
82+
target_os = "openbsd",
83+
))] {
84+
msg.msg_iovlen = bufs.len() as libc::c_int;
85+
msg.msg_controllen = ancillary.length as libc::socklen_t;
86+
}
87+
}
6488

6589
ancillary.truncated = false;
6690

@@ -102,15 +126,22 @@ fn add_to_ancillary_data<T>(
102126

103127
*length = new_length;
104128

105-
let msg = libc::msghdr {
106-
msg_name: null_mut(),
107-
msg_namelen: 0,
108-
msg_iov: null_mut(),
109-
msg_iovlen: 0,
110-
msg_control: buffer.as_mut_ptr().cast(),
111-
msg_controllen: *length,
112-
msg_flags: 0,
113-
};
129+
let mut msg: libc::msghdr = zeroed();
130+
msg.msg_control = buffer.as_mut_ptr().cast();
131+
cfg_if::cfg_if! {
132+
if #[cfg(any(target_os = "android", all(target_os = "linux", target_env = "gnu")))] {
133+
msg.msg_controllen = *length as libc::size_t;
134+
} else if #[cfg(any(
135+
target_os = "dragonfly",
136+
target_os = "emscripten",
137+
target_os = "freebsd",
138+
all(target_os = "linux", target_env = "musl",),
139+
target_os = "netbsd",
140+
target_os = "openbsd",
141+
))] {
142+
msg.msg_controllen = *length as libc::socklen_t;
143+
}
144+
}
114145

115146
let mut cmsg = libc::CMSG_FIRSTHDR(&msg);
116147
let mut previous_cmsg = cmsg;
@@ -125,7 +156,20 @@ fn add_to_ancillary_data<T>(
125156

126157
(*previous_cmsg).cmsg_level = cmsg_level;
127158
(*previous_cmsg).cmsg_type = cmsg_type;
128-
(*previous_cmsg).cmsg_len = libc::CMSG_LEN(source_len) as usize;
159+
cfg_if::cfg_if! {
160+
if #[cfg(any(target_os = "android", all(target_os = "linux", target_env = "gnu")))] {
161+
(*previous_cmsg).cmsg_len = libc::CMSG_LEN(source_len) as libc::size_t;
162+
} else if #[cfg(any(
163+
target_os = "dragonfly",
164+
target_os = "emscripten",
165+
target_os = "freebsd",
166+
all(target_os = "linux", target_env = "musl",),
167+
target_os = "netbsd",
168+
target_os = "openbsd",
169+
))] {
170+
(*previous_cmsg).cmsg_len = libc::CMSG_LEN(source_len) as libc::socklen_t;
171+
}
172+
}
129173

130174
let data = libc::CMSG_DATA(previous_cmsg).cast();
131175

@@ -295,10 +339,23 @@ impl<'a> AncillaryData<'a> {
295339

296340
fn try_from_cmsghdr(cmsg: &'a libc::cmsghdr) -> Result<Self, AncillaryError> {
297341
unsafe {
298-
let cmsg_len_zero = libc::CMSG_LEN(0) as usize;
342+
cfg_if::cfg_if! {
343+
if #[cfg(any(target_os = "android", all(target_os = "linux", target_env = "gnu")))] {
344+
let cmsg_len_zero = libc::CMSG_LEN(0) as libc::size_t;
345+
} else if #[cfg(any(
346+
target_os = "dragonfly",
347+
target_os = "emscripten",
348+
target_os = "freebsd",
349+
all(target_os = "linux", target_env = "musl",),
350+
target_os = "netbsd",
351+
target_os = "openbsd",
352+
))] {
353+
let cmsg_len_zero = libc::CMSG_LEN(0) as libc::socklen_t;
354+
}
355+
}
299356
let data_len = (*cmsg).cmsg_len - cmsg_len_zero;
300357
let data = libc::CMSG_DATA(cmsg).cast();
301-
let data = from_raw_parts(data, data_len);
358+
let data = from_raw_parts(data, data_len as usize);
302359

303360
match (*cmsg).cmsg_level {
304361
libc::SOL_SOCKET => match (*cmsg).cmsg_type {
@@ -330,15 +387,22 @@ impl<'a> Iterator for Messages<'a> {
330387

331388
fn next(&mut self) -> Option<Self::Item> {
332389
unsafe {
333-
let msg = libc::msghdr {
334-
msg_name: null_mut(),
335-
msg_namelen: 0,
336-
msg_iov: null_mut(),
337-
msg_iovlen: 0,
338-
msg_control: self.buffer.as_ptr() as *mut _,
339-
msg_controllen: self.buffer.len(),
340-
msg_flags: 0,
341-
};
390+
let mut msg: libc::msghdr = zeroed();
391+
msg.msg_control = self.buffer.as_ptr() as *mut _;
392+
cfg_if::cfg_if! {
393+
if #[cfg(any(target_os = "android", all(target_os = "linux", target_env = "gnu")))] {
394+
msg.msg_controllen = self.buffer.len() as libc::size_t;
395+
} else if #[cfg(any(
396+
target_os = "dragonfly",
397+
target_os = "emscripten",
398+
target_os = "freebsd",
399+
all(target_os = "linux", target_env = "musl",),
400+
target_os = "netbsd",
401+
target_os = "openbsd",
402+
))] {
403+
msg.msg_controllen = self.buffer.len() as libc::socklen_t;;
404+
}
405+
}
342406

343407
let cmsg = if let Some(current) = self.current {
344408
libc::CMSG_NXTHDR(&msg, current)

0 commit comments

Comments
 (0)