axnet/smoltcp_impl/
addr.rs

1use core::net::{IpAddr, Ipv4Addr, SocketAddr};
2use smoltcp::wire::{IpAddress, IpEndpoint, Ipv4Address};
3
4pub const fn from_core_ipaddr(ip: IpAddr) -> IpAddress {
5    match ip {
6        IpAddr::V4(ipv4) => IpAddress::Ipv4(Ipv4Address(ipv4.octets())),
7        _ => panic!("IPv6 not supported"),
8    }
9}
10
11pub const fn into_core_ipaddr(ip: IpAddress) -> IpAddr {
12    match ip {
13        IpAddress::Ipv4(ipv4) => {
14            IpAddr::V4(unsafe { core::mem::transmute::<[u8; 4], Ipv4Addr>(ipv4.0) })
15        } // _ => panic!("IPv6 not supported"),
16    }
17}
18
19pub const fn from_core_sockaddr(addr: SocketAddr) -> IpEndpoint {
20    IpEndpoint {
21        addr: from_core_ipaddr(addr.ip()),
22        port: addr.port(),
23    }
24}
25
26pub const fn into_core_sockaddr(addr: IpEndpoint) -> SocketAddr {
27    SocketAddr::new(into_core_ipaddr(addr.addr), addr.port)
28}
29
30pub fn is_unspecified(ip: IpAddress) -> bool {
31    ip.as_bytes() == [0, 0, 0, 0]
32}
33
34pub const UNSPECIFIED_IP: IpAddress = IpAddress::v4(0, 0, 0, 0);
35pub const UNSPECIFIED_ENDPOINT: IpEndpoint = IpEndpoint::new(UNSPECIFIED_IP, 0);