Hi all, I am trying to follow this TUN/TAP tutorial. While I was able to get the C code to run just fine. Something weird happens when I translate the code to Rust.
You are right. I need to use ioctl!(write_int tun_set_iff with TUN_MAGIC, TUN_SET_IFF); to get the proper ioctl for TUNSETIFF.
However, the kernel defines TUNSETIFF as taking an int when in reality it takes the address of the struct. How can I solve that? Casting the pointer to an int doesn't work since it's not the same size.
Looking at the nix documentation, you may want to use this functionality:
As mentioned earlier, there are many old ioctls that do not use the newer method of generating ioctl numbers and instead use hardcoded values. These can be used with the bad * variants of the ioctl! macro. This naming comes from the Linux kernel which refers to these ioctls as "bad". These are a different variant as they bypass calling the macro that generates the ioctl number and instead use the defined value directly.
β¦In this case, the C definition doesn't actually use "hardcoded values", but rather "the newer method" with the wrong type. Still, by allowing you to specify the ioctl number directly, the 'bad' variant should serve the purpose. See also the source code defining the macro - it's quite straightforward.
You should be able to specify the number as iow!(TUN_MAGIC, TUN_SET_IFF, size_of::<c_int>()).