aboutsummaryrefslogtreecommitdiff
path: root/src/link.rs
blob: 209b8a6bb09e88cb010b531eda5154a61f966711 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
//! Simple functions to add, monitor and configure network interfaces.

use crate::{Error, Result};

use std::time::Duration;

use tokio::time::sleep;

use futures::TryStreamExt;
use netlink_packet_route::rtnl::IFF_UP;

/// Brings an interface up or down.
#[cfg(feature = "link")]
pub async fn set(link: String, state: bool) -> Result<()> {
    let (conn, handle, _) = rtnetlink::new_connection()?;
    tokio::spawn(conn);

    let link = handle
        .link()
        .get()
        .match_name(link.clone())
        .execute()
        .try_next()
        .await?
        .ok_or(Error::LinkNotFound(link))?;

    let id = link.header.index;

    match state {
        true => handle.link().set(id).up(),
        false => handle.link().set(id).down(),
    }
    .execute()
    .await?;

    Ok(())
}

/// Reports whether an interface is up.
///
/// # Errors
///
/// This function fails if the interface doesn't exist
/// or if any other `rtnetlink` error occurs.
pub async fn is_up(link: String) -> Result<bool> {
    let (conn, handle, _) = rtnetlink::new_connection()?;
    tokio::spawn(conn);

    let link = handle
        .link()
        .get()
        .match_name(link.clone())
        .execute()
        .try_next()
        .await?
        .ok_or(Error::LinkNotFound(link))?;

    let is_up = link.header.flags & IFF_UP == IFF_UP;
    Ok(is_up)
}

/// Sets the MTU of an interface.
#[cfg(feature = "link")]
pub async fn set_mtu(link: String, mtu: u32) -> Result<()> {
    let (conn, handle, _) = rtnetlink::new_connection()?;
    tokio::spawn(conn);

    let link = handle
        .link()
        .get()
        .match_name(link.clone())
        .execute()
        .try_next()
        .await?
        .ok_or(Error::LinkNotFound(link))?;

    let id = link.header.index;

    handle.link().set(id).mtu(mtu).execute().await?;
    Ok(())
}

/// Creates a VLAN interface on top of a parent interface.
///
/// # Arguments
///
/// * `link` - The name of the VLAN interface to be created.
/// * `parent` - The name of the parent interface for the actual traffic.
/// * `vlan_id` - The VLAN ID for tagging.
#[cfg(feature = "link")]
pub async fn add_vlan(link: String, parent: String, vlan_id: u16) -> Result<()> {
    let (conn, handle, _) = rtnetlink::new_connection()?;
    tokio::spawn(conn);

    let parent = handle
        .link()
        .get()
        .match_name(parent.clone())
        .execute()
        .try_next()
        .await?
        .ok_or(Error::LinkNotFound(parent))?;

    let parent_id = parent.header.index;

    handle
        .link()
        .add()
        .vlan(link, parent_id, vlan_id)
        .execute()
        .await?;

    Ok(())
}

/// Waits for an interface to come up, including waiting for its creation.
pub async fn wait_up(link: String) -> Result<()> {
    while !exists(link.clone()).await? || !is_up(link.clone()).await? {
        sleep(Duration::from_millis(200)).await;
    }

    Ok(())
}

/// Reports whether an interface exists.
pub async fn exists(link: String) -> Result<bool> {
    let (conn, handle, _) = rtnetlink::new_connection()?;
    tokio::spawn(conn);

    let exists = handle
        .link()
        .get()
        .match_name(link)
        .execute()
        .try_next()
        .await
        .is_ok();

    Ok(exists)
}

/// Waits until an interface is created.
pub async fn wait_exists(link: String) -> Result<()> {
    while !exists(link.clone()).await? {
        sleep(Duration::from_millis(200)).await;
    }

    Ok(())
}