Skip to content

Commit

Permalink
fix IP cast, vers 0.3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
leshow committed Aug 1, 2022
1 parent ac00e3b commit 79b4a66
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 16 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "unix-udp-sock"
version = "0.3.0-alpha"
version = "0.3.0"
edition = "2021"
license = "MIT OR Apache-2.0"
repository = "https://github.com/leshow/udp-udp-sock"
Expand Down
49 changes: 34 additions & 15 deletions src/unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::{
io,
io::IoSliceMut,
mem::{self, MaybeUninit},
net::{IpAddr, SocketAddr},
net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr, SocketAddrV4, SocketAddrV6},
os::unix::io::AsRawFd,
ptr,
sync::atomic::AtomicUsize,
Expand Down Expand Up @@ -844,19 +844,21 @@ fn decode_recv(
ecn_bits = cmsg::decode::<libc::c_int>(cmsg) as u8;
}
},
(libc::IPPROTO_IP, libc::IP_PKTINFO) => unsafe {
let pktinfo = cmsg::decode::<libc::in_pktinfo>(cmsg);
dst_ip = Some(IpAddr::V4(ptr::read(&pktinfo.ipi_addr as *const _ as _)));
dst_local_ip = Some(IpAddr::V4(ptr::read(
&pktinfo.ipi_spec_dst as *const _ as _,
(libc::IPPROTO_IP, libc::IP_PKTINFO) => {
let pktinfo = unsafe { cmsg::decode::<libc::in_pktinfo>(cmsg) };
dst_ip = Some(IpAddr::V4(Ipv4Addr::from(
pktinfo.ipi_addr.s_addr.to_ne_bytes(),
)));
ifindex = ptr::read(&pktinfo.ipi_ifindex as *const _ as _);
},
(libc::IPPROTO_IPV6, libc::IPV6_PKTINFO) => unsafe {
let pktinfo = cmsg::decode::<libc::in6_pktinfo>(cmsg);
dst_ip = Some(IpAddr::V6(ptr::read(&pktinfo.ipi6_addr as *const _ as _)));
ifindex = ptr::read(&pktinfo.ipi6_ifindex as *const _ as _);
},
dst_local_ip = Some(IpAddr::V4(Ipv4Addr::from(
pktinfo.ipi_spec_dst.s_addr.to_ne_bytes(),
)));
ifindex = pktinfo.ipi_ifindex as _;
}
(libc::IPPROTO_IPV6, libc::IPV6_PKTINFO) => {
let pktinfo = unsafe { cmsg::decode::<libc::in6_pktinfo>(cmsg) };
dst_ip = Some(IpAddr::V6(Ipv6Addr::from(pktinfo.ipi6_addr.s6_addr)));
ifindex = pktinfo.ipi6_ifindex;
}
#[cfg(target_os = "linux")]
(libc::SOL_UDP, libc::UDP_GRO) => unsafe {
stride = cmsg::decode::<libc::c_int>(cmsg) as usize;
Expand All @@ -866,8 +868,25 @@ fn decode_recv(
}

let addr = match libc::c_int::from(name.ss_family) {
libc::AF_INET => unsafe { SocketAddr::V4(ptr::read(&name as *const _ as _)) },
libc::AF_INET6 => unsafe { SocketAddr::V6(ptr::read(&name as *const _ as _)) },
libc::AF_INET => {
// Safety: if the ss_family field is AF_INET then storage must be a sockaddr_in.
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 => {
// Safety: if the ss_family field is AF_INET6 then storage must be a sockaddr_in6.
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!(),
};

Expand Down

0 comments on commit 79b4a66

Please sign in to comment.