demikernel
demikernel copied to clipboard
[pdpix] Improve Byte Ordering Conversion for `sockaddr` and `Ipv4Endpoint`
Description
There is some unclear byte-ordering conversions happening in sockaddr_to_ipv4endpoint
.
fn sockaddr_to_ipv4endpoint(saddr: *const sockaddr) -> Result<Ipv4Endpoint, Fail> {
// TODO: Review why we need byte ordering conversion here.
let sin: libc::sockaddr_in =
unsafe { *mem::transmute::<*const sockaddr, *const libc::sockaddr_in>(saddr) };
let addr: Ipv4Addr = { Ipv4Addr::from(u32::from_be_bytes(sin.sin_addr.s_addr.to_le_bytes())) };
let port: Port16 = Port16::try_from(u16::from_be(sin.sin_port))?;
Ok(Ipv4Endpoint::new(addr, port))
}
Quoting @BrianZill
Ideally, we wouldn't have an assumption that host order is little endian. Windows (well NT) does, unfortunately, and I don't know about Linux, but BSD didn't, and had things like htons() which stood for host-to-network-short, which would convert a u16 from whatever the host order was to network order (which is always big endian). I haven't looked at whether the standard Rust libraries have such conversion functions that swap (or not) depending on the architecture they're running on.