jtomschroeder / ether

Library for parsing and manipulating network data, packet captures.
MIT License
6 stars 1 forks source link

/dev/bpf* doesn't exist, Error success #5

Open kpcyrd opened 7 years ago

kpcyrd commented 7 years ago

I ran into another issue:

let mut tap = tap::Tap::new("eth0").unwrap();

is failing with this error:

thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: Error { repr: Os { code: 0, message: "Success" } }', src/libcore/result.rs:860

This is the code of open():

pub fn new(interface: &str) -> io::Result<Self> {
    fn open() -> io::Result<File> {
        // On macOS: bpf exposed as /dev/bpf###
        for entry in glob("/dev/bpf*").expect("Failed to read glob pattern") {
            if let Some(file) = entry
                   .ok()
                   .and_then(|path| {
                                 OpenOptions::new()
                                     .read(true)
                                     .write(true)
                                     .open(&path)
                                     .ok()
                             }) {
                return Ok(file);
            }
        }

        Err(io::Error::last_os_error())
    }

    let file = try!(open()); // this returns Err(io::Error::last_os_error())
    let config = Config::default();
    // [...]

It appears linux doesn't support berkley packet filter so it doesn't have /dev/bpf*, falls through the loop and returns last_os_error() which was unrelated and successful. :)

You might want to look into the pcap crate, I managed to dump traffic successfully using it, but I didn't feel like decoding ethernet, ip and tcp manually.

jtomschroeder commented 7 years ago

Yeah, on Linux, we would use an AF_PACKET socket. Also, the error handling and OS-specific gating could definitely be better. A wrapper around libpcap is viable option, but ether is meant to be a rustic replacement.

Tasks: