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
use super::result::Result;
use super::tun::Tun;
#[cfg(target_os = "linux")]
use crate::linux::params::Params;
use core::convert::From;
use libc::{IFF_NO_PI, IFF_TAP, IFF_TUN};
use std::net::Ipv4Addr;
pub struct TunBuilder<'a> {
name: &'a str,
is_tap: bool,
packet_info: bool,
persist: bool,
up: bool,
mtu: Option<i32>,
owner: Option<i32>,
group: Option<i32>,
address: Option<Ipv4Addr>,
destination: Option<Ipv4Addr>,
broadcast: Option<Ipv4Addr>,
netmask: Option<Ipv4Addr>,
}
impl<'a> Default for TunBuilder<'a> {
fn default() -> Self {
Self {
name: "",
owner: None,
group: None,
is_tap: false,
persist: false,
up: false,
mtu: None,
packet_info: true,
address: None,
destination: None,
broadcast: None,
netmask: None,
}
}
}
impl<'a> TunBuilder<'a> {
pub fn new() -> Self {
Default::default()
}
pub fn name(mut self, name: &'a str) -> Self {
self.name = name;
self
}
pub fn tap(mut self, is_tap: bool) -> Self {
self.is_tap = is_tap;
self
}
pub fn packet_info(mut self, packet_info: bool) -> Self {
self.packet_info = packet_info;
self
}
pub fn mtu(mut self, mtu: i32) -> Self {
self.mtu = Some(mtu);
self
}
pub fn owner(mut self, owner: i32) -> Self {
self.owner = Some(owner);
self
}
pub fn group(mut self, group: i32) -> Self {
self.group = Some(group);
self
}
pub fn address(mut self, address: Ipv4Addr) -> Self {
self.address = Some(address);
self
}
pub fn destination(mut self, dst: Ipv4Addr) -> Self {
self.destination = Some(dst);
self
}
pub fn broadcast(mut self, broadcast: Ipv4Addr) -> Self {
self.broadcast = Some(broadcast);
self
}
pub fn netmask(mut self, netmask: Ipv4Addr) -> Self {
self.netmask = Some(netmask);
self
}
pub fn persist(mut self) -> Self {
self.persist = true;
self
}
pub fn up(mut self) -> Self {
self.up = true;
self
}
pub async fn try_build(self) -> Result<Tun> {
Tun::new(self.into()).await
}
#[cfg(target_os = "linux")]
pub async fn try_build_mq(self, queues: usize) -> Result<Vec<Tun>> {
Tun::new_mq(self.into(), queues).await
}
}
impl<'a> From<TunBuilder<'a>> for Params {
#[cfg(target_os = "linux")]
fn from(builder: TunBuilder) -> Self {
Params {
name: if builder.name.is_empty() {
None
} else {
Some(builder.name.into())
},
flags: {
let mut flags = if builder.is_tap { IFF_TAP } else { IFF_TUN } as _;
if !builder.packet_info {
flags |= IFF_NO_PI as i16;
}
flags
},
persist: builder.persist,
up: builder.up,
mtu: builder.mtu,
owner: builder.owner,
group: builder.group,
address: builder.address,
destination: builder.destination,
broadcast: builder.broadcast,
netmask: builder.netmask,
}
}
#[cfg(not(any(target_os = "linux")))]
fn from(builder: TunBuilder) -> Self {
unimplemented!()
}
}