Skip to main content

name_to_index

Function name_to_index 

Source
pub fn name_to_index(name: IfName) -> Result<InterfaceId, Error>
Expand description

Converts a network interface name to the index.

This function just uses libc. May be a blocking call.

Examples found in repository?
examples/if_nametoindex.rs (line 18)
3fn main() {
4    let args = std::env::args().collect::<Vec<_>>();
5    if args.len() < 2 {
6        println!("Usage: {} <if_index>", &args[0]);
7        std::process::exit(1);
8    }
9
10    let name = match IfName::new(args[1].clone()) {
11        Ok(n) => n,
12        _ => {
13            eprintln!("{}: Invalid interface name", &args[0]);
14            std::process::exit(1);
15        }
16    };
17
18    match name_to_index(name) {
19        Ok(id) => {
20            println!("{}", id);
21        },
22        _ => {
23            eprintln!("{}: No such interface", args[0]);
24            std::process::exit(2);
25        }
26    }
27}