Crate iproute2[][src]

This crate provides methods to manipulate networking resources (links, addresses, arp tables, route tables) via the netlink protocol.

It can be used on its own for simple needs, but it is possible to tweak any netlink request. See this link creation snippet for example.

Example: listing links

extern crate futures;
extern crate iproute2;
extern crate tokio_core;

use futures::Future;
use iproute2::new_connection;
use tokio_core::reactor::Core;

fn main() {
    // Create a netlink connection, and a handle to send requests via this connection
    let (connection, handle) = new_connection().unwrap();

    // The connection will run in an event loop
    let mut core = Core::new().unwrap();
    core.handle().spawn(connection.map_err(|_| ()));

    /// Create a netlink request
    let request = handle.link().get().execute().and_then(|links| {
        println!("{:#?}", links);
        Ok(())
    });

    /// Run the request on the event loop
    core.run(request).unwrap();
}

Example: creating a veth pair

extern crate futures;
extern crate iproute2;
extern crate tokio_core;

use std::thread::spawn;

use futures::Future;
use tokio_core::reactor::Core;

use iproute2::new_connection;

fn main() {
    // Create a netlink connection, and a handle to send requests via this connection
    let (connection, handle) = new_connection().unwrap();

    // The connection we run in its own thread
    spawn(move || Core::new().unwrap().run(connection));

    // Create a request to create the veth pair
    handle
        .link()
        .add()
        .veth("veth-rs-1".into(), "veth-rs-2".into())
        // Execute the request, and wait for it to finish
        .execute()
        .wait()
        .unwrap();
}

Example: deleting a link by name

extern crate futures;
extern crate iproute2;
extern crate tokio_core;

use std::env;
use std::thread::spawn;

use futures::Future;
use tokio_core::reactor::Core;

use iproute2::new_connection;

fn main() {
    let args: Vec<String> = env::args().collect();
    if args.len() != 2 { panic!("expected one link name as argument"); }
    let link_name = &args[1];

    let (connection, handle) = new_connection().unwrap();
    spawn(move || Core::new().unwrap().run(connection));

    // Get the list of links
    let links = handle.link().get().execute().wait().unwrap();

    // Find the link with the name provided as argument, and delete it
    for link in links {
        if link.name().unwrap() == link_name {
            handle.link().del(link.index()).execute().wait().unwrap();
        }
    }
}

Structs

Connection

Connection to a netlink socket, running in the background.

ConnectionHandle

A handle to pass requests to a Connection.

Link
LinkAddRequest

A request to create a new link. This is equivalent to the ip link add commands.

LinkDelRequest
LinkGetRequest
LinkHandle
LinkSetRequest

Enums

NetlinkIpError

Functions

new_connection

Create a new connection and a handle to pass requests to it. The connection is a future (see Connection, so once created, it needs to be run on a task executor, for example an event loop: