Skip to content

Commit ca01e3f

Browse files
committed
fix(net): check if it is unix addr
1 parent bfb14fc commit ca01e3f

File tree

1 file changed

+60
-32
lines changed

1 file changed

+60
-32
lines changed

compio-net/src/unix.rs

Lines changed: 60 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,13 @@ impl UnixListener {
5252
/// the specified file path. The file path cannot yet exist, and will be
5353
/// cleaned up upon dropping [`UnixListener`]
5454
pub fn bind_addr(addr: &SockAddr) -> io::Result<Self> {
55+
if !addr.is_unix() {
56+
return Err(io::Error::new(
57+
io::ErrorKind::InvalidInput,
58+
"addr is not unix socket address",
59+
));
60+
}
61+
5562
let socket = Socket::bind(addr, Type::STREAM, None)?;
5663
socket.listen(1024)?;
5764
Ok(UnixListener { inner: socket })
@@ -129,27 +136,16 @@ impl UnixStream {
129136
/// [`UnixListener`] or equivalent listening on the corresponding Unix
130137
/// domain socket to successfully connect and return a `UnixStream`.
131138
pub async fn connect_addr(addr: &SockAddr) -> io::Result<Self> {
139+
if !addr.is_unix() {
140+
return Err(io::Error::new(
141+
io::ErrorKind::InvalidInput,
142+
"addr is not unix socket address",
143+
));
144+
}
145+
132146
#[cfg(windows)]
133147
let socket = {
134-
use windows_sys::Win32::Networking::WinSock::{AF_UNIX, SOCKADDR_UN};
135-
136-
let new_addr = unsafe {
137-
SockAddr::try_init(|addr, len| {
138-
let addr: *mut SOCKADDR_UN = addr.cast();
139-
std::ptr::write(
140-
addr,
141-
SOCKADDR_UN {
142-
sun_family: AF_UNIX,
143-
sun_path: [0; 108],
144-
},
145-
);
146-
std::ptr::write(len, 3);
147-
Ok(())
148-
})
149-
}
150-
// it is always Ok
151-
.unwrap()
152-
.1;
148+
let new_addr = empty_unix_socket();
153149
Socket::bind(&new_addr, Type::STREAM, None)?
154150
};
155151
#[cfg(unix)]
@@ -181,21 +177,9 @@ impl UnixStream {
181177
pub fn peer_addr(&self) -> io::Result<SockAddr> {
182178
#[allow(unused_mut)]
183179
let mut addr = self.inner.peer_addr()?;
184-
// The peer addr returned after ConnectEx is buggy. It contains bytes that
185-
// should not belong to the address. Luckily a unix path should not contain `\0`
186-
// until the end. We can determine the path ending by that.
187180
#[cfg(windows)]
188181
{
189-
use windows_sys::Win32::Networking::WinSock::SOCKADDR_UN;
190-
191-
let unix_addr: &SOCKADDR_UN = unsafe { &*addr.as_ptr().cast() };
192-
let addr_len = match std::ffi::CStr::from_bytes_until_nul(&unix_addr.sun_path) {
193-
Ok(str) => str.to_bytes_with_nul().len() + 2,
194-
Err(_) => std::mem::size_of::<SOCKADDR_UN>(),
195-
};
196-
unsafe {
197-
addr.set_length(addr_len as _);
198-
}
182+
fix_unix_socket_length(&mut addr);
199183
}
200184
Ok(addr)
201185
}
@@ -296,3 +280,47 @@ impl AsyncWrite for &UnixStream {
296280
impl_try_as_raw_fd!(UnixStream, inner);
297281

298282
impl_attachable!(UnixStream, inner);
283+
284+
#[cfg(windows)]
285+
#[inline]
286+
fn empty_unix_socket() -> SockAddr {
287+
use windows_sys::Win32::Networking::WinSock::{AF_UNIX, SOCKADDR_UN};
288+
289+
// SAFETY: the length is correct
290+
unsafe {
291+
SockAddr::try_init(|addr, len| {
292+
let addr: *mut SOCKADDR_UN = addr.cast();
293+
std::ptr::write(
294+
addr,
295+
SOCKADDR_UN {
296+
sun_family: AF_UNIX,
297+
sun_path: [0; 108],
298+
},
299+
);
300+
std::ptr::write(len, 3);
301+
Ok(())
302+
})
303+
}
304+
// it is always Ok
305+
.unwrap()
306+
.1
307+
}
308+
309+
// The peer addr returned after ConnectEx is buggy. It contains bytes that
310+
// should not belong to the address. Luckily a unix path should not contain `\0`
311+
// until the end. We can determine the path ending by that.
312+
#[cfg(windows)]
313+
#[inline]
314+
fn fix_unix_socket_length(addr: &mut SockAddr) {
315+
use windows_sys::Win32::Networking::WinSock::SOCKADDR_UN;
316+
317+
// SAFETY: cannot construct non-unix socket address in safe way.
318+
let unix_addr: &SOCKADDR_UN = unsafe { &*addr.as_ptr().cast() };
319+
let addr_len = match std::ffi::CStr::from_bytes_until_nul(&unix_addr.sun_path) {
320+
Ok(str) => str.to_bytes_with_nul().len() + 2,
321+
Err(_) => std::mem::size_of::<SOCKADDR_UN>(),
322+
};
323+
unsafe {
324+
addr.set_length(addr_len as _);
325+
}
326+
}

0 commit comments

Comments
 (0)