From 28dc580777ac9c3ce62932ae6ce184ea7c82d61c Mon Sep 17 00:00:00 2001 From: Quentin De Coninck Date: Wed, 16 Nov 2022 14:01:36 +0100 Subject: [PATCH] recvfrom: don't assume memory layout --- apps/src/recvfrom.rs | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/apps/src/recvfrom.rs b/apps/src/recvfrom.rs index 8430a55733..a0fa402cdf 100644 --- a/apps/src/recvfrom.rs +++ b/apps/src/recvfrom.rs @@ -70,6 +70,10 @@ fn recvmsg( ) -> std::io::Result<(usize, RecvInfo)> { use std::io::IoSliceMut; use std::mem::MaybeUninit; + use std::net::Ipv4Addr; + use std::net::Ipv6Addr; + use std::net::SocketAddrV4; + use std::net::SocketAddrV6; use std::os::unix::io::AsRawFd; use libc::CMSG_LEN; @@ -118,11 +122,24 @@ fn recvmsg( } let source = match libc::c_int::from(name.ss_family) { - libc::AF_INET => unsafe { - SocketAddr::V4(std::ptr::read(&name as *const _ as _)) + libc::AF_INET => { + let addr = + unsafe { &*(&name as *const _ as *const libc::sockaddr_in) }; + let ip = Ipv4Addr::from(addr.sin_addr.s_addr.to_ne_bytes()); + let port = u16::from_be(addr.sin_port); + SocketAddr::V4(SocketAddrV4::new(ip, port)) }, - libc::AF_INET6 => unsafe { - SocketAddr::V6(std::ptr::read(&name as *const _ as _)) + libc::AF_INET6 => { + let addr = + unsafe { &*(&name as *const _ as *const libc::sockaddr_in6) }; + let ip = Ipv6Addr::from(addr.sin6_addr.s6_addr); + let port = u16::from_be(addr.sin6_port); + SocketAddr::V6(SocketAddrV6::new( + ip, + port, + addr.sin6_flowinfo, + addr.sin6_scope_id, + )) }, _ => unreachable!(), };