gluxon / wireguard-uapi-rs

MIT License
32 stars 10 forks source link

Implement add/delete device #4

Closed gluxon closed 4 years ago

gluxon commented 4 years ago

Wanted to document how adding a device was done with the rtnetlink crates package. I wrote this a while back and it'll probably be helpful for implementing it in neli.

#[cfg(feature = "route-cmds")]
pub fn add_device(&self, name: &str) -> std::io::Result<()> {
    use tokio_core::reactor::Core;
    use futures::future::Future;
    use rtnetlink::packet::{LinkNla, LinkInfo, LinkInfoKind};
    use std::thread::spawn;

    let (connection, handle) = rtnetlink::new_connection()?;

    let mut link_add_request = handle.link().add();
    let message = link_add_request.message_mut();
    message.nlas_mut().push(LinkNla::IfName(name.to_string()));
    message.nlas_mut().push(LinkNla::LinkInfo(vec![LinkInfo::Kind(LinkInfoKind::Other("wireguard".to_string()))]));

    spawn(move || Core::new().unwrap().run(connection));

    println!("{:#?}", link_add_request.execute().wait().unwrap());

    Ok(())
}