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
// SPDX-License-Identifier: MIT
use std::net::IpAddr;
use crate::{
packet_route::link::{AmtMode, InfoAmt, InfoData, InfoKind},
LinkMessageBuilder,
};
/// Represent AMT (Automatic Multicast Tunneling) interface.
/// Example code on creating an AMT interface
/// ```no_run
/// use std::net::{IpAddr, Ipv4Addr};
/// use rtnetlink::{new_connection, LinkAmt};
/// use rtnetlink::packet_route::link::AmtMode;
/// #[tokio::main]
/// async fn main() -> Result<(), String> {
/// let (connection, handle, _) = new_connection().unwrap();
/// tokio::spawn(connection);
///
/// handle
/// .link()
/// .add(
/// LinkAmt::new("amt0")
/// .mode(AmtMode::Gateway)
/// .dev(2)
/// .local_ip(IpAddr::V4(Ipv4Addr::new(10, 0, 0, 1)))
/// .build(),
/// )
/// .execute()
/// .await
/// .map_err(|e| format!("{e}"))
/// }
/// ```
///
/// Please check LinkMessageBuilder::<LinkAmt> for more detail.
#[derive(Default, Debug)]
pub struct LinkAmt;
impl LinkAmt {
/// Equal to `LinkMessageBuilder::<LinkAmt>::new()`
pub fn new(name: &str) -> LinkMessageBuilder<Self> {
LinkMessageBuilder::<LinkAmt>::new(name)
}
}
impl LinkMessageBuilder<LinkAmt> {
/// Create [LinkMessageBuilder] for AMT interface type
pub fn new(name: &str) -> Self {
LinkMessageBuilder::<LinkAmt>::new_with_info_kind(InfoKind::Amt)
.name(name.to_string())
}
fn append_info_data(self, info: InfoAmt) -> Self {
let mut ret = self;
if let InfoData::Amt(infos) = ret
.info_data
.get_or_insert_with(|| InfoData::Amt(Vec::new()))
{
infos.push(info);
}
ret
}
pub fn mode(self, mode: AmtMode) -> Self {
self.append_info_data(InfoAmt::Mode(mode))
}
pub fn relay_port(self, port: u16) -> Self {
self.append_info_data(InfoAmt::RelayPort(port))
}
pub fn gateway_port(self, port: u16) -> Self {
self.append_info_data(InfoAmt::GatewayPort(port))
}
/// Set the AMT link device (IFLA_AMT_LINK inside IFLA_INFO_DATA).
/// This is the `dev` parameter in `ip link add ... type amt dev DEV`.
pub fn dev(self, ifindex: u32) -> Self {
self.append_info_data(InfoAmt::Link(ifindex))
}
pub fn local_ip(self, ip: IpAddr) -> Self {
self.append_info_data(InfoAmt::LocalIp(ip))
}
pub fn remote_ip(self, ip: IpAddr) -> Self {
self.append_info_data(InfoAmt::RemoteIp(ip))
}
pub fn discovery_ip(self, ip: IpAddr) -> Self {
self.append_info_data(InfoAmt::DiscoveryIp(ip))
}
pub fn max_tunnels(self, count: u32) -> Self {
self.append_info_data(InfoAmt::MaxTunnels(count))
}
}