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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
use std::{
io,
net::{IpAddr, Ipv4Addr, Ipv6Addr},
};
mod platform_impl;
use platform_impl::PlatformHandle;
pub struct Handle(PlatformHandle);
impl Handle {
pub fn new() -> io::Result<Self> {
Ok(Self(PlatformHandle::new()?))
}
pub async fn add(&self, route: &Route) -> io::Result<()> {
self.0.add(route).await
}
pub fn route_listen_stream(&self) -> impl futures::Stream<Item = RouteChange> {
self.0.route_listen_stream()
}
pub async fn list(&self) -> io::Result<Vec<Route>> {
self.0.list().await
}
pub async fn default_route(&self) -> io::Result<Option<Route>> {
self.0.default_route().await
}
pub async fn delete(&self, route: &Route) -> io::Result<()> {
self.0.delete(route).await
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Route {
pub destination: IpAddr,
pub prefix: u8,
pub gateway: Option<IpAddr>,
pub ifindex: Option<u32>,
#[cfg(target_os = "linux")]
pub table: u8,
#[cfg(target_os = "windows")]
pub metric: Option<u32>,
#[cfg(target_os = "windows")]
pub luid: Option<u64>,
}
impl Route {
pub fn new(destination: IpAddr, prefix: u8) -> Self {
Self {
destination,
prefix,
gateway: None,
ifindex: None,
#[cfg(target_os = "linux")]
table: 254,
#[cfg(target_os = "windows")]
metric: None,
#[cfg(target_os = "windows")]
luid: None,
}
}
pub fn with_gateway(mut self, gateway: IpAddr) -> Self {
self.gateway = Some(gateway);
self
}
pub fn with_ifindex(mut self, ifindex: u32) -> Self {
self.ifindex = Some(ifindex);
self
}
#[cfg(target_os = "linux")]
pub fn with_table(mut self, table: u8) -> Self {
self.table = table;
self
}
#[cfg(target_os = "windows")]
pub fn with_metric(mut self, metric: u32) -> Self {
self.metric = Some(metric);
self
}
#[cfg(target_os = "windows")]
pub fn with_luid(mut self, luid: u64) -> Self {
self.luid = Some(luid);
self
}
pub fn mask(&self) -> IpAddr {
match self.destination {
IpAddr::V4(_) => IpAddr::V4(Ipv4Addr::from(u32::MAX.checked_shl(32 - self.prefix as u32).unwrap_or(0))),
IpAddr::V6(_) => IpAddr::V6(Ipv6Addr::from(u128::MAX.checked_shl(128 - self.prefix as u32).unwrap_or(0))),
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum RouteChange {
Add(Route),
Delete(Route),
Change(Route),
}
#[cfg(test)]
mod tests {
use std::net::{IpAddr, Ipv6Addr};
use crate::Route;
#[test]
fn it_calculates_v4_netmask() {
let mut route = Route::new("10.10.0.0".parse().unwrap(), 32);
assert_eq!(route.mask(), "255.255.255.255".parse::<IpAddr>().unwrap());
route.prefix = 29;
assert_eq!(route.mask(), "255.255.255.248".parse::<IpAddr>().unwrap());
route.prefix = 25;
assert_eq!(route.mask(), "255.255.255.128".parse::<IpAddr>().unwrap());
route.prefix = 2;
assert_eq!(route.mask(), "192.0.0.0".parse::<IpAddr>().unwrap());
}
#[test]
fn it_calculates_v6_netmask() {
let route = Route::new(
"77ca:838b:9ec0:fc97:eedc:236a:9d41:31e5".parse().unwrap(),
32,
);
assert_eq!(
route.mask(),
Ipv6Addr::new(0xffff, 0xffff, 0, 0, 0, 0, 0, 0)
);
}
}