meh / rust-tun

TUN device creation and handling.
340 stars 133 forks source link

Socket operation on non-socket #43

Closed spacemeowx2 closed 2 years ago

spacemeowx2 commented 2 years ago

OS: Ubuntu 20.04.3 LTS (Linux ubuntu 5.11.0-34-generic #36~20.04.1-Ubuntu SMP Fri Aug 27 08:06:32 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux)

It seems that on Linux, you can't use sendmsg and recvmsg on a tun fd.

https://github.com/meh/rust-tun/blob/aeca1a64e9ded75ca256d920e45ffdb868a24c6f/src/platform/posix/fd.rs#L99

https://www.reddit.com/r/linuxquestions/comments/44gs03/operation_on_nonsocket_when_using_a_tun_interface/

spacemeowx2 commented 2 years ago

Minimal reproduction code:

use std::io::{IoSliceMut, Read};

fn main() {
    let mut config = tun::Configuration::default();
    config
        .address((10, 0, 0, 1))
        .netmask((255, 255, 255, 0))
        .up();

    #[cfg(target_os = "linux")]
    config.platform(|config| {
        config.packet_information(true);
    });

    let mut dev = tun::create(&config).unwrap();
    let mut buf = [0; 4096];

    loop {
        let bufs = IoSliceMut::new(&mut buf);
        let amount = dev.read_vectored(&mut [bufs]).unwrap();
        println!("{:?}", &buf[0..amount]);
    }
}