andrew-d / interfaces-rs

Library to work with network interfaces in Rust.
https://andrew-d.github.io/interfaces-rs/interfaces/index.html
53 stars 19 forks source link

.is_up() not working correctly? #24

Closed ljufa closed 3 years ago

ljufa commented 3 years ago
fn main() {
    let args = env::args().collect::<Vec<String>>();
    let iname = args[1].clone();
    loop {
        match Interface::get_by_name(&iname) {
            Ok(iopt) => match iopt {
                Some(i) => {
                    println!("Interface {} is present", iname);
                    if !i.is_up() {
                        println!("Interface is down!.");

                    } else {
                        println!("Interface is up.");
                    }
                }
                None => {
                    println!("Interface is not available!.");
                }
            },
            Err(e) => {
                println!("Could not get interface. {}", e);
            }
        };
        std::thread::sleep_ms(2000);
    }
}

After the LAN cable was unplugged it kept printing. Interface is up! Although ip a command showed a change to DOWN.

In the debugger, I could see that flags have changed.

ljufa commented 3 years ago

Actually, maybe I have wrong expectations from this function, I guess there is a difference btw physical and logical if up/down. This is the output of ip a with and without cable:

2: enp1s0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
2: enp1s0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc fq_codel state DOWN group default qlen 1000
dalance commented 3 years ago

Yes. That's right. is_up is corresponding to IFF_UP of SIOCGIFFLAGS. https://man7.org/linux/man-pages/man7/netdevice.7.html. It means the interfaces is enabled only. So it is true even if the cable is unplugged. IFF_RUNNING represents the actual status of the interface. I added is_running to access the flag.

ljufa commented 3 years ago

Great tnx, is_running will help.