#[cfg(not(windows))]
use libc::{sockaddr, sockaddr_in, sockaddr_in6, AF_INET, AF_INET6};
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
use std::ptr::NonNull;
#[cfg(windows)]
use winapi::{
shared::ws2def::{AF_INET, AF_INET6, SOCKADDR as sockaddr, SOCKADDR_IN as sockaddr_in},
shared::ws2ipdef::SOCKADDR_IN6 as sockaddr_in6,
};
pub fn to_ipaddr(sockaddr: *const sockaddr) -> Option<IpAddr> {
if sockaddr.is_null() {
return None;
}
SockAddr::new(sockaddr)?.as_ipaddr()
}
struct SockAddr {
inner: NonNull<sockaddr>,
}
impl SockAddr {
#[allow(clippy::new_ret_no_self)]
fn new(sockaddr: *const sockaddr) -> Option<Self> {
NonNull::new(sockaddr as *mut _).map(|inner| Self { inner })
}
#[cfg(not(windows))]
fn as_ipaddr(&self) -> Option<IpAddr> {
match self.sockaddr_in() {
Some(SockAddrIn::In(sa)) => Some(IpAddr::V4(Ipv4Addr::new(
((sa.sin_addr.s_addr) & 255) as u8,
((sa.sin_addr.s_addr >> 8) & 255) as u8,
((sa.sin_addr.s_addr >> 16) & 255) as u8,
((sa.sin_addr.s_addr >> 24) & 255) as u8,
))),
Some(SockAddrIn::In6(sa)) => {
if sa.sin6_addr.s6_addr[0] == 0xfe && sa.sin6_addr.s6_addr[1] == 0x80 {
return None;
}
Some(IpAddr::V6(Ipv6Addr::from(sa.sin6_addr.s6_addr)))
}
None => None,
}
}
#[cfg(windows)]
fn as_ipaddr(&self) -> Option<IpAddr> {
match self.sockaddr_in() {
Some(SockAddrIn::In(sa)) => {
let s_addr = unsafe { sa.sin_addr.S_un.S_addr() };
if s_addr & 65535 == 0xfea9 {
return None;
}
Some(IpAddr::V4(Ipv4Addr::new(
((s_addr >> 0) & 255u32) as u8,
((s_addr >> 8) & 255u32) as u8,
((s_addr >> 16) & 255u32) as u8,
((s_addr >> 24) & 255u32) as u8,
)))
}
Some(SockAddrIn::In6(sa)) => {
let s6_addr = unsafe { sa.sin6_addr.u.Byte() };
if s6_addr[0] == 0xfe && s6_addr[1] == 0x80 {
return None;
}
Some(IpAddr::V6(Ipv6Addr::from(s6_addr.clone())))
}
None => None,
}
}
fn sockaddr_in(&self) -> Option<SockAddrIn> {
const AF_INET_U32: u32 = AF_INET as u32;
const AF_INET6_U32: u32 = AF_INET6 as u32;
match self.sa_family() {
AF_INET_U32 => Some(SockAddrIn::In(self.sa_in())),
AF_INET6_U32 => Some(SockAddrIn::In6(self.sa_in6())),
_ => None,
}
}
#[allow(unsafe_code)]
fn sa_family(&self) -> u32 {
unsafe { u32::from(self.inner.as_ref().sa_family) }
}
#[allow(unsafe_code)]
#[allow(clippy::cast_ptr_alignment)]
fn sa_in(&self) -> sockaddr_in {
unsafe { *(self.inner.as_ptr() as *const sockaddr_in) }
}
#[allow(unsafe_code)]
#[allow(clippy::cast_ptr_alignment)]
fn sa_in6(&self) -> sockaddr_in6 {
unsafe { *(self.inner.as_ptr() as *const sockaddr_in6) }
}
}
enum SockAddrIn {
In(sockaddr_in),
In6(sockaddr_in6),
}