polachok / pnetlink

netlink library for rust
43 stars 13 forks source link

netlink is host-native ordering #5

Open rbtcollins opened 7 years ago

rbtcollins commented 7 years ago

Sadly netlink doesn't have a stable wireformat - its host representation specific. E.g. a __u32 in the linux headers is neither u32be or u32le - its dependent on the architecture of the kernel. The current definitions in pnetlink are e.g. u32le which will fail on architectures with different byte orderings (such as various ARM and MIPS machines - common these days in IoT)

polachok commented 7 years ago

This is correct. I think it can be easily solved by using cfg, e.g.:

struct Whatever {
    #[cfg(target_endian = "little")]
    a: u32le,
    #[cfg(target_endian = "big")]
    a: i32be,
}

or alternatively with type aliases:

#[cfg(target_endian = "little")]
type u32ne = u32le;
#[cfg(target_endian = "big")]
type u32ne = u32be;
rbtcollins commented 7 years ago

I think we need to make it hard to add modules in a buggy fashion - the cfg should apply throughout all of pnetlink, and use of u32le etc in pnetlink struct definitions should trigger compiler errors.