use core::net::{IpAddr, SocketAddr};
use smoltcp::wire::{IpAddress, IpEndpoint, Ipv4Address};
pub const fn from_core_ipaddr(ip: IpAddr) -> IpAddress {
match ip {
IpAddr::V4(ipv4) => IpAddress::Ipv4(Ipv4Address(ipv4.octets())),
_ => panic!("IPv6 not supported"),
}
}
pub const fn into_core_ipaddr(ip: IpAddress) -> IpAddr {
match ip {
IpAddress::Ipv4(ipv4) => IpAddr::V4(unsafe { core::mem::transmute(ipv4.0) }),
}
}
pub const fn from_core_sockaddr(addr: SocketAddr) -> IpEndpoint {
IpEndpoint {
addr: from_core_ipaddr(addr.ip()),
port: addr.port(),
}
}
pub const fn into_core_sockaddr(addr: IpEndpoint) -> SocketAddr {
SocketAddr::new(into_core_ipaddr(addr.addr), addr.port)
}
pub fn is_unspecified(ip: IpAddress) -> bool {
ip.as_bytes() == [0, 0, 0, 0]
}
pub const UNSPECIFIED_IP: IpAddress = IpAddress::v4(0, 0, 0, 0);
pub const UNSPECIFIED_ENDPOINT: IpEndpoint = IpEndpoint::new(UNSPECIFIED_IP, 0);