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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
use crate::builder::DeviceConfig;
use crate::platform::windows::netsh;
use crate::platform::windows::tap::TapDevice;
use crate::platform::windows::tun::{check_adapter_if_orphaned_devices, TunDevice};
use crate::platform::ETHER_ADDR_LEN;
use crate::{Layer, ToIpv4Address, ToIpv4Netmask, ToIpv6Address, ToIpv6Netmask};
use getifaddrs::Interface;
use ipnet::IpNet;
use std::collections::HashSet;
use std::io;
use std::net::IpAddr;
use std::sync::Mutex;
use windows_sys::core::GUID;
pub(crate) const GUID_NETWORK_ADAPTER: GUID = GUID {
data1: 0x4d36e972,
data2: 0xe325,
data3: 0x11ce,
data4: [0xbf, 0xc1, 0x08, 0x00, 0x2b, 0xe1, 0x03, 0x18],
};
pub(crate) enum Driver {
Tun(TunDevice),
Tap(TapDevice),
}
/// A TUN device using the wintun driver.
pub struct DeviceImpl {
lock: Mutex<()>,
pub(crate) driver: Driver,
}
impl DeviceImpl {
/// Create a new `Device` for the given `Configuration`.
pub(crate) fn new(config: DeviceConfig) -> io::Result<Self> {
let layer = config.layer.unwrap_or(Layer::L3);
let mut count = 0;
let interfaces: HashSet<String> = Self::get_all_adapter_address()?
.into_iter()
.map(|v| v.description)
.collect();
let device = if layer == Layer::L3 {
let wintun_log = config.wintun_log.unwrap_or(false);
let wintun_file = config.wintun_file.as_deref().unwrap_or("wintun.dll");
let ring_capacity = config.ring_capacity.unwrap_or(0x20_0000);
let delete_driver = config.delete_driver.unwrap_or(false);
let mut attempts = 0;
let tun_device = loop {
let default_name = format!("tun{count}");
count += 1;
let name = config.dev_name.as_deref().unwrap_or(&default_name);
if interfaces.contains(name) {
if config.dev_name.is_none() {
continue;
}
// Resolves an issue where there are orphaned adapters. fixes #33
let is_orphaned_adapter = check_adapter_if_orphaned_devices(name);
if !is_orphaned_adapter {
// Try to open an existing Wintun adapter.
break TunDevice::open(
wintun_file,
name,
ring_capacity,
delete_driver,
wintun_log,
)?;
}
}
let description = config.description.as_deref().unwrap_or(name);
match TunDevice::create(
wintun_file,
name,
description,
config.device_guid,
ring_capacity,
delete_driver,
wintun_log,
) {
Ok(tun_device) => break tun_device,
Err(e) => {
if attempts > 3 {
Err(e)?
}
attempts += 1;
}
}
};
DeviceImpl {
lock: Mutex::new(()),
driver: Driver::Tun(tun_device),
}
} else if layer == Layer::L2 {
const HARDWARE_ID: &str = "tap0901";
let persist = config.persist.unwrap_or(false);
let tap = loop {
let default_name = format!("tap{count}");
let name = config.dev_name.as_deref().unwrap_or(&default_name);
if interfaces.contains(name) {
if config.dev_name.is_none() {
count += 1;
continue;
} else if !config.reuse_dev.unwrap_or(true) {
Err(io::Error::other(format!(
"The network adapter [{name}] already exists."
)))?
}
let tap =
TapDevice::open(HARDWARE_ID, name, persist, config.mac_address.as_ref())?;
break tap;
}
let tap = TapDevice::create(HARDWARE_ID, persist, config.mac_address.as_ref())?;
if let Err(e) = tap.set_name(name) {
if config.dev_name.is_some() {
Err(e)?
}
}
break tap;
};
DeviceImpl {
lock: Mutex::new(()),
driver: Driver::Tap(tap),
}
} else {
panic!("unknown layer {layer:?}");
};
Ok(device)
}
#[cfg(any(
feature = "interruptible",
feature = "async_tokio",
feature = "async_io"
))]
pub(crate) fn wait_readable_interruptible(
&self,
event: &crate::platform::windows::InterruptEvent,
timeout: Option<std::time::Duration>,
) -> io::Result<()> {
match &self.driver {
Driver::Tap(tap) => tap.wait_readable_interruptible(&event.handle, timeout),
Driver::Tun(tun) => tun.wait_readable_interruptible(&event.handle, timeout),
}
}
#[cfg(feature = "interruptible")]
pub(crate) fn read_interruptible(
&self,
buf: &mut [u8],
event: &crate::InterruptEvent,
timeout: Option<std::time::Duration>,
) -> io::Result<usize> {
loop {
self.wait_readable_interruptible(event, timeout)?;
match self.try_recv(buf) {
Ok(rs) => {
return Ok(rs);
}
Err(ref e) if e.kind() == io::ErrorKind::WouldBlock => {
continue;
}
Err(e) => return Err(e),
}
}
}
/// Recv a packet from tun device
pub(crate) fn recv(&self, buf: &mut [u8]) -> io::Result<usize> {
match &self.driver {
Driver::Tap(tap) => tap.read(buf),
Driver::Tun(tun) => tun.recv(buf),
}
}
pub(crate) fn try_recv(&self, buf: &mut [u8]) -> io::Result<usize> {
match &self.driver {
Driver::Tap(tap) => tap.try_read(buf),
Driver::Tun(tun) => tun.try_recv(buf),
}
}
/// Send a packet to tun device
pub(crate) fn send(&self, buf: &[u8]) -> io::Result<usize> {
match &self.driver {
Driver::Tap(tap) => tap.write(buf),
Driver::Tun(tun) => tun.send(buf),
}
}
#[cfg(any(
feature = "interruptible",
feature = "async_tokio",
feature = "async_io"
))]
pub(crate) fn write_interruptible(
&self,
buf: &[u8],
event: &crate::platform::windows::InterruptEvent,
) -> io::Result<usize> {
match &self.driver {
Driver::Tap(tap) => tap.write_interruptible(buf, &event.handle),
Driver::Tun(tun) => tun.send_interruptible(buf, &event.handle),
}
}
pub(crate) fn try_send(&self, buf: &[u8]) -> io::Result<usize> {
match &self.driver {
Driver::Tap(tap) => tap.try_write(buf),
Driver::Tun(tun) => tun.try_send(buf),
}
}
pub(crate) fn shutdown(&self) -> io::Result<()> {
match &self.driver {
Driver::Tun(tun) => tun.shutdown(),
Driver::Tap(tap) => tap.down(),
}
}
fn if_index_impl(&self) -> io::Result<u32> {
match &self.driver {
Driver::Tun(tun) => Ok(tun.index()),
Driver::Tap(tap) => Ok(tap.index()),
}
}
fn get_all_adapter_address() -> io::Result<Vec<Interface>> {
Ok(getifaddrs::getifaddrs()?.collect())
}
fn name_impl(&self) -> io::Result<String> {
match &self.driver {
Driver::Tun(tun) => tun.get_name(),
Driver::Tap(tap) => tap.get_name(),
}
}
}
// Public User interface
impl DeviceImpl {
/// Retrieves the name of the device.
///
/// Calls the appropriate method on the underlying driver (TUN or TAP) to obtain the device name.
pub fn name(&self) -> io::Result<String> {
let _guard = self.lock.lock().unwrap();
self.name_impl()
}
/// Sets a new name for the device.
///
/// This method first checks if the current name is different from the desired one. If it is,
/// it uses the `netsh` command to update the interface name.
pub fn set_name(&self, value: &str) -> io::Result<()> {
let _guard = self.lock.lock().unwrap();
let name = self.name_impl()?;
if value == name {
return Ok(());
}
netsh::set_interface_name(&name, value)
}
/// Retrieves the interface index (if_index) of the device.
///
/// This is used for various network configuration commands.
pub fn if_index(&self) -> io::Result<u32> {
let _guard = self.lock.lock().unwrap();
self.if_index_impl()
}
/// Enables or disables the device.
///
/// For a TUN device, disabling is not supported and will return an error.
/// For a TAP device, this calls the appropriate method to set the device status.
pub fn enabled(&self, value: bool) -> io::Result<()> {
let _guard = self.lock.lock().unwrap();
match &self.driver {
Driver::Tun(tun) => tun.enabled(value),
Driver::Tap(tap) => tap.set_status(value),
}
}
/// Retrieves all IP addresses associated with this device.
///
/// Filters the adapter addresses by matching the device's interface index.
pub fn addresses(&self) -> io::Result<Vec<IpAddr>> {
let _guard = self.lock.lock().unwrap();
let index = self.if_index_impl()?;
let r = Self::get_all_adapter_address()?
.into_iter()
.filter(|v| v.index == Some(index))
.filter_map(|v| v.address.ip_addr())
.collect();
Ok(r)
}
/// Sets the IPv4 network address, netmask, and an optional destination address.
/// Remove all previous set IPv4 addresses and set the specified address.
pub fn set_network_address<IPv4: ToIpv4Address, Netmask: ToIpv4Netmask>(
&self,
address: IPv4,
netmask: Netmask,
destination: Option<IPv4>,
) -> io::Result<()> {
let _guard = self.lock.lock().unwrap();
netsh::set_interface_ip(
self.if_index_impl()?,
address.ipv4()?.into(),
netmask.netmask()?.into(),
destination.map(|v| v.ipv4()).transpose()?.map(|v| v.into()),
)
}
/// Add IPv4 network address and netmask to the interface.
///
/// This allows configuring multiple IPv4 addresses on a single TUN/TAP device on Windows.
///
/// # Arguments
///
/// * `address` - The IPv4 address to add
/// * `netmask` - The network mask (can be specified as a prefix length or full netmask)
///
/// # Example
///
/// ```no_run
/// # #[cfg(target_os = "windows")]
/// # {
/// use tun_rs::DeviceBuilder;
///
/// let dev = DeviceBuilder::new()
/// .ipv4("10.0.0.1", 24, None)
/// .build_sync()?;
///
/// // Add additional IPv4 addresses
/// dev.add_address_v4("10.0.1.1", 24)?;
/// dev.add_address_v4("10.0.2.1", 24)?;
/// println!("Added multiple IPv4 addresses");
/// # }
/// # Ok::<(), std::io::Error>(())
/// ```
///
/// # Platform
///
/// Windows only. Requires administrator privileges.
pub fn add_address_v4<IPv4: ToIpv4Address, Netmask: ToIpv4Netmask>(
&self,
address: IPv4,
netmask: Netmask,
) -> io::Result<()> {
let _guard = self.lock.lock().unwrap();
let interface = netconfig_rs::Interface::try_from_index(self.if_index_impl()?)
.map_err(io::Error::from)?;
interface
.add_address(IpNet::new_assert(address.ipv4()?.into(), netmask.prefix()?))
.map_err(io::Error::from)
}
/// Removes the specified IP address from the device.
pub fn remove_address(&self, addr: IpAddr) -> io::Result<()> {
let _guard = self.lock.lock().unwrap();
netsh::delete_interface_ip(self.if_index_impl()?, addr)
}
/// Adds an IPv6 address and netmask to the device.
///
/// Configures the IPv6 address and netmask (converted from prefix) for the interface.
///
/// # Arguments
///
/// * `addr` - The IPv6 address to add
/// * `netmask` - The network mask (can be specified as a prefix length or full netmask)
///
/// # Example
///
/// ```no_run
/// # #[cfg(target_os = "windows")]
/// # {
/// use tun_rs::DeviceBuilder;
///
/// let dev = DeviceBuilder::new()
/// .ipv4("10.0.0.1", 24, None)
/// .build_sync()?;
///
/// // Add IPv6 addresses
/// dev.add_address_v6("fd00::1", 64)?;
/// dev.add_address_v6("fd00::2", 64)?;
/// println!("Added IPv6 addresses");
/// # }
/// # Ok::<(), std::io::Error>(())
/// ```
///
/// # Platform
///
/// Windows only. Requires administrator privileges.
pub fn add_address_v6<IPv6: ToIpv6Address, Netmask: ToIpv6Netmask>(
&self,
addr: IPv6,
netmask: Netmask,
) -> io::Result<()> {
let _guard = self.lock.lock().unwrap();
let mask = netmask.netmask()?;
netsh::set_interface_ip(
self.if_index_impl()?,
addr.ipv6()?.into(),
mask.into(),
None,
)
}
/// Retrieves the MTU for the device (IPv4).
///
/// This method uses a Windows-specific FFI function to query the MTU by interface index.
pub fn mtu(&self) -> io::Result<u16> {
let _guard = self.lock.lock().unwrap();
let index = self.if_index_impl()?;
let mtu = crate::platform::windows::ffi::get_mtu_by_index(index, true)?;
Ok(mtu as _)
}
/// Retrieves the MTU for the device (IPv6).
///
/// This method uses a Windows-specific FFI function to query the IPv6 MTU by interface index.
pub fn mtu_v6(&self) -> io::Result<u16> {
let _guard = self.lock.lock().unwrap();
let index = self.if_index_impl()?;
let mtu = crate::platform::windows::ffi::get_mtu_by_index(index, false)?;
Ok(mtu as _)
}
/// Sets the MTU for the device (IPv4) using the `netsh` command.
pub fn set_mtu(&self, mtu: u16) -> io::Result<()> {
let _guard = self.lock.lock().unwrap();
netsh::set_interface_mtu(self.if_index_impl()?, mtu as _)
}
/// Sets the MTU for the device (IPv6) using the `netsh` command.
pub fn set_mtu_v6(&self, mtu: u16) -> io::Result<()> {
let _guard = self.lock.lock().unwrap();
netsh::set_interface_mtu_v6(self.if_index_impl()?, mtu as _)
}
/// Sets the MAC address for the device.
///
/// Attempting to set a MAC address will result in an error.
///
/// #Note:
/// set a MAC address is only supported when creating a TUN/TAP device.
pub fn set_mac_address(&self, eth_addr: [u8; ETHER_ADDR_LEN as usize]) -> io::Result<()> {
let _guard = self.lock.lock().unwrap();
match &self.driver {
Driver::Tun(_tun) => Err(io::Error::from(io::ErrorKind::Unsupported)),
Driver::Tap(tap) => tap.set_mac(ð_addr),
}
}
/// Retrieves the MAC address of the device.
///
/// This operation is only supported for TAP devices.
pub fn mac_address(&self) -> io::Result<[u8; ETHER_ADDR_LEN as usize]> {
let _guard = self.lock.lock().unwrap();
match &self.driver {
Driver::Tun(_tun) => Err(io::Error::from(io::ErrorKind::Unsupported)),
Driver::Tap(tap) => tap.get_mac(),
}
}
/// Sets the interface routing metric (routing cost).
///
/// The metric value determines the priority of this interface when multiple routes exist
/// to the same destination. Lower metric values have higher priority.
///
/// # Arguments
///
/// * `metric` - The metric value to set (lower values = higher priority)
///
/// # Example
///
/// ```no_run
/// # #[cfg(target_os = "windows")]
/// # {
/// use tun_rs::DeviceBuilder;
///
/// let dev = DeviceBuilder::new()
/// .ipv4("10.0.0.1", 24, None)
/// .build_sync()?;
///
/// // Set a lower metric to prioritize this interface
/// dev.set_metric(10)?;
/// println!("Set interface metric to 10");
/// # }
/// # Ok::<(), std::io::Error>(())
/// ```
///
/// # Platform
///
/// Windows only. Uses the `netsh` command internally. Requires administrator privileges.
pub fn set_metric(&self, metric: u16) -> io::Result<()> {
let _guard = self.lock.lock().unwrap();
netsh::set_interface_metric(self.if_index_impl()?, metric)
}
/// Retrieves the version of the underlying driver.
///
/// For TUN devices, this directly queries the driver version.
/// For TAP devices, the version is composed of several components joined by dots.
pub fn version(&self) -> io::Result<String> {
let _guard = self.lock.lock().unwrap();
match &self.driver {
Driver::Tun(tun) => tun.version(),
Driver::Tap(tap) => tap.get_version().map(|v| {
v.iter()
.map(|v| v.to_string())
.collect::<Vec<String>>()
.join(".")
}),
}
}
/// Set DNS servers for the current device (supports primary and secondary DNS)
/// dns_servers: A priority-ordered list of DNS servers (must be all IPv4 or all IPv6)
pub fn set_dns_servers(&self, dns_servers: &[IpAddr]) -> io::Result<()> {
let _guard = self.lock.lock().unwrap();
let index = self.if_index_impl()?;
netsh::set_dns_servers(index, dns_servers)
}
/// Clear DNS configuration for the current device (restore to automatic acquisition)
/// is_ipv4: true to clear IPv4 DNS, false to clear IPv6 DNS
pub fn clear_dns_servers(&self, is_ipv4: bool) -> io::Result<()> {
let _guard = self.lock.lock().unwrap();
let index = self.if_index_impl()?;
netsh::clear_dns_servers(index, is_ipv4)
}
}