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
// SPDX-License-Identifier: MIT

use crate::{
    packet::{
        nlas::link::{Nla, Prop},
        LinkMessage,
        NetlinkMessage,
        NetlinkPayload,
        RtnlMessage,
        NLM_F_ACK,
        NLM_F_EXCL,
        NLM_F_REQUEST,
    },
    Error,
    Handle,
};
use futures::stream::StreamExt;

pub struct LinkDelPropRequest {
    handle: Handle,
    message: LinkMessage,
}

impl LinkDelPropRequest {
    pub(crate) fn new(handle: Handle, index: u32) -> Self {
        let mut message = LinkMessage::default();
        message.header.index = index;
        LinkDelPropRequest { handle, message }
    }

    /// Execute the request
    pub async fn execute(self) -> Result<(), Error> {
        let LinkDelPropRequest {
            mut handle,
            message,
        } = self;
        let mut req = NetlinkMessage::from(RtnlMessage::DelLinkProp(message));
        req.header.flags = NLM_F_REQUEST | NLM_F_ACK | NLM_F_EXCL;

        let mut response = handle.request(req)?;
        while let Some(message) = response.next().await {
            if let NetlinkPayload::Error(err) = message.payload {
                return Err(Error::NetlinkError(err));
            }
        }
        Ok(())
    }

    /// Return a mutable reference to the request
    pub fn message_mut(&mut self) -> &mut LinkMessage {
        &mut self.message
    }

    /// Remove alternative name to the link. This is equivalent to `ip link property del altname
    /// ALT_IFNAME dev LINK`.
    pub fn alt_ifname(mut self, alt_ifnames: &[&str]) -> Self {
        let mut props = Vec::new();
        for alt_ifname in alt_ifnames {
            props.push(Prop::AltIfName(alt_ifname.to_string()));
        }

        self.message.nlas.push(Nla::PropList(props));
        self
    }
}