1
1
//! Fake `tun` for those platforms that doesn't support `tun`
2
2
3
+ #![ allow( dead_code) ]
4
+
3
5
use std:: {
4
6
io:: { self , Read , Write } ,
5
7
net:: IpAddr ,
@@ -9,7 +11,142 @@ use std::{
9
11
} ;
10
12
11
13
use tokio:: io:: { AsyncRead , AsyncWrite , ReadBuf } ;
12
- use tun:: { AbstractDevice , Configuration , Error as TunError } ;
14
+
15
+ /// TUN interface OSI layer of operation.
16
+ #[ derive( Clone , Copy , Default , Debug , Eq , PartialEq ) ]
17
+ pub enum Layer {
18
+ L2 ,
19
+ #[ default]
20
+ L3 ,
21
+ }
22
+
23
+ /// Configuration builder for a TUN interface.
24
+ #[ derive( Clone , Default , Debug ) ]
25
+ pub struct Configuration ;
26
+
27
+ impl Configuration {
28
+ /// Set the tun name.
29
+ ///
30
+ /// [Note: on macOS, the tun name must be the form `utunx` where `x` is a number, such as `utun3`. -- end note]
31
+ pub fn tun_name < S : AsRef < str > > ( & mut self , _tun_name : S ) -> & mut Self {
32
+ self
33
+ }
34
+
35
+ /// Set the address.
36
+ pub fn address ( & mut self , _value : IpAddr ) -> & mut Self {
37
+ self
38
+ }
39
+
40
+ /// Set the destination address.
41
+ pub fn destination ( & mut self , _value : IpAddr ) -> & mut Self {
42
+ self
43
+ }
44
+
45
+ /// Set the broadcast address.
46
+ pub fn broadcast ( & mut self , _value : IpAddr ) -> & mut Self {
47
+ self
48
+ }
49
+
50
+ /// Set the netmask.
51
+ pub fn netmask ( & mut self , _value : IpAddr ) -> & mut Self {
52
+ self
53
+ }
54
+
55
+ /// Set the MTU.
56
+ pub fn mtu ( & mut self , _value : u16 ) -> & mut Self {
57
+ self
58
+ }
59
+
60
+ /// Set the interface to be enabled once created.
61
+ pub fn up ( & mut self ) -> & mut Self {
62
+ self
63
+ }
64
+
65
+ /// Set the interface to be disabled once created.
66
+ pub fn down ( & mut self ) -> & mut Self {
67
+ self
68
+ }
69
+
70
+ /// Set the OSI layer of operation.
71
+ pub fn layer ( & mut self , _value : Layer ) -> & mut Self {
72
+ self
73
+ }
74
+
75
+ /// Set the raw fd.
76
+ #[ cfg( unix) ]
77
+ pub fn raw_fd ( & mut self , _fd : :: std:: os:: fd:: RawFd ) -> & mut Self {
78
+ self
79
+ }
80
+ }
81
+
82
+ /// tun Error type
83
+ #[ derive( thiserror:: Error , Debug ) ]
84
+ pub enum Error {
85
+ #[ error( "not implementated" ) ]
86
+ NotImplemented ,
87
+
88
+ #[ error( transparent) ]
89
+ Io ( #[ from] std:: io:: Error ) ,
90
+ }
91
+
92
+ pub type Result < T , E = Error > = :: std:: result:: Result < T , E > ;
93
+
94
+ /// A TUN abstract device interface.
95
+ pub trait AbstractDevice : Read + Write {
96
+ /// Reconfigure the device.
97
+ fn configure ( & mut self , _config : & Configuration ) -> Result < ( ) > {
98
+ Ok ( ( ) )
99
+ }
100
+
101
+ /// Get the device index.
102
+ fn tun_index ( & self ) -> Result < i32 > ;
103
+
104
+ /// Get the device tun name.
105
+ fn tun_name ( & self ) -> Result < String > ;
106
+
107
+ /// Set the device tun name.
108
+ fn set_tun_name ( & mut self , tun_name : & str ) -> Result < ( ) > ;
109
+
110
+ /// Turn on or off the interface.
111
+ fn enabled ( & mut self , value : bool ) -> Result < ( ) > ;
112
+
113
+ /// Get the address.
114
+ fn address ( & self ) -> Result < IpAddr > ;
115
+
116
+ /// Set the address.
117
+ fn set_address ( & mut self , value : IpAddr ) -> Result < ( ) > ;
118
+
119
+ /// Get the destination address.
120
+ fn destination ( & self ) -> Result < IpAddr > ;
121
+
122
+ /// Set the destination address.
123
+ fn set_destination ( & mut self , value : IpAddr ) -> Result < ( ) > ;
124
+
125
+ /// Get the broadcast address.
126
+ fn broadcast ( & self ) -> Result < IpAddr > ;
127
+
128
+ /// Set the broadcast address.
129
+ fn set_broadcast ( & mut self , value : IpAddr ) -> Result < ( ) > ;
130
+
131
+ /// Get the netmask.
132
+ fn netmask ( & self ) -> Result < IpAddr > ;
133
+
134
+ /// Set the netmask.
135
+ fn set_netmask ( & mut self , value : IpAddr ) -> Result < ( ) > ;
136
+
137
+ /// Get the MTU.
138
+ fn mtu ( & self ) -> Result < u16 > ;
139
+
140
+ /// Set the MTU.
141
+ ///
142
+ /// [Note: This setting has no effect on the Windows platform due to the mtu of wintun is always 65535. --end note]
143
+ fn set_mtu ( & mut self , value : u16 ) -> Result < ( ) > ;
144
+
145
+ /// Return whether the underlying tun device on the platform has packet information
146
+ ///
147
+ /// [Note: This value is not used to specify whether the packets delivered from/to tun have packet information. -- end note]
148
+ fn packet_information ( & self ) -> bool ;
149
+ }
13
150
14
151
pub struct FakeQueue ;
15
152
@@ -32,60 +169,60 @@ impl Write for FakeQueue {
32
169
pub struct FakeDevice ;
33
170
34
171
impl AbstractDevice for FakeDevice {
35
- fn tun_name ( & self ) -> tun :: Result < String > {
36
- Err ( TunError :: NotImplemented )
172
+ fn tun_name ( & self ) -> Result < String > {
173
+ Err ( Error :: NotImplemented )
37
174
}
38
175
39
- fn tun_index ( & self ) -> tun :: Result < i32 > {
40
- Err ( TunError :: NotImplemented )
176
+ fn tun_index ( & self ) -> Result < i32 > {
177
+ Err ( Error :: NotImplemented )
41
178
}
42
179
43
- fn set_tun_name ( & mut self , _: & str ) -> tun :: Result < ( ) > {
44
- Err ( TunError :: NotImplemented )
180
+ fn set_tun_name ( & mut self , _: & str ) -> Result < ( ) > {
181
+ Err ( Error :: NotImplemented )
45
182
}
46
183
47
- fn enabled ( & mut self , _: bool ) -> tun :: Result < ( ) > {
48
- Err ( TunError :: NotImplemented )
184
+ fn enabled ( & mut self , _: bool ) -> Result < ( ) > {
185
+ Err ( Error :: NotImplemented )
49
186
}
50
187
51
- fn address ( & self ) -> tun :: Result < IpAddr > {
52
- Err ( TunError :: NotImplemented )
188
+ fn address ( & self ) -> Result < IpAddr > {
189
+ Err ( Error :: NotImplemented )
53
190
}
54
191
55
- fn set_address ( & mut self , _: IpAddr ) -> tun :: Result < ( ) > {
56
- Err ( TunError :: NotImplemented )
192
+ fn set_address ( & mut self , _: IpAddr ) -> Result < ( ) > {
193
+ Err ( Error :: NotImplemented )
57
194
}
58
195
59
- fn destination ( & self ) -> tun :: Result < IpAddr > {
60
- Err ( TunError :: NotImplemented )
196
+ fn destination ( & self ) -> Result < IpAddr > {
197
+ Err ( Error :: NotImplemented )
61
198
}
62
199
63
- fn set_destination ( & mut self , _: IpAddr ) -> tun :: Result < ( ) > {
64
- Err ( TunError :: NotImplemented )
200
+ fn set_destination ( & mut self , _: IpAddr ) -> Result < ( ) > {
201
+ Err ( Error :: NotImplemented )
65
202
}
66
203
67
- fn broadcast ( & self ) -> tun :: Result < IpAddr > {
68
- Err ( TunError :: NotImplemented )
204
+ fn broadcast ( & self ) -> Result < IpAddr > {
205
+ Err ( Error :: NotImplemented )
69
206
}
70
207
71
- fn set_broadcast ( & mut self , _: IpAddr ) -> tun :: Result < ( ) > {
72
- Err ( TunError :: NotImplemented )
208
+ fn set_broadcast ( & mut self , _: IpAddr ) -> Result < ( ) > {
209
+ Err ( Error :: NotImplemented )
73
210
}
74
211
75
- fn netmask ( & self ) -> tun :: Result < IpAddr > {
76
- Err ( TunError :: NotImplemented )
212
+ fn netmask ( & self ) -> Result < IpAddr > {
213
+ Err ( Error :: NotImplemented )
77
214
}
78
215
79
- fn set_netmask ( & mut self , _: IpAddr ) -> tun :: Result < ( ) > {
80
- Err ( TunError :: NotImplemented )
216
+ fn set_netmask ( & mut self , _: IpAddr ) -> Result < ( ) > {
217
+ Err ( Error :: NotImplemented )
81
218
}
82
219
83
- fn mtu ( & self ) -> tun :: Result < u16 > {
84
- Err ( TunError :: NotImplemented )
220
+ fn mtu ( & self ) -> Result < u16 > {
221
+ Err ( Error :: NotImplemented )
85
222
}
86
223
87
- fn set_mtu ( & mut self , _: u16 ) -> tun :: Result < ( ) > {
88
- Err ( TunError :: NotImplemented )
224
+ fn set_mtu ( & mut self , _: u16 ) -> Result < ( ) > {
225
+ Err ( Error :: NotImplemented )
89
226
}
90
227
91
228
fn packet_information ( & self ) -> bool {
@@ -158,6 +295,6 @@ impl AsyncWrite for AsyncDevice {
158
295
}
159
296
160
297
/// Create a TUN device with the given name.
161
- pub fn create_as_async ( _: & Configuration ) -> Result < AsyncDevice , TunError > {
162
- Err ( TunError :: NotImplemented )
298
+ pub fn create_as_async ( _: & Configuration ) -> Result < AsyncDevice , Error > {
299
+ Err ( Error :: NotImplemented )
163
300
}
0 commit comments