Skip to content

Commit 563aca3

Browse files
committed
Use the new version of tun-rs
1 parent 274f4df commit 563aca3

File tree

8 files changed

+37
-51
lines changed

8 files changed

+37
-51
lines changed

Cargo.lock

+11-18
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

common/src/cli.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -511,7 +511,7 @@ fn print_usage(program: &str, _opts: Options) {
511511
" --use-channel <p2p> {}",
512512
get_description("--use-channel <p2p>", &language)
513513
);
514-
#[cfg(not(feature = "vn-link-model"))]
514+
#[cfg(feature = "integrated_tun")]
515515
println!(
516516
" --nic <tun0> {}",
517517
get_description("--nic <tun0>", &language)

vnt/Cargo.toml

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ edition = "2021"
66
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
77

88
[dependencies]
9-
tun-rs = { version = "1.5.0", optional = true,features = ["experimental"] }
9+
tun-rs = { version = "2", optional = true, features = ["experimental"] }
1010
packet = { path = "./packet" }
1111
bytes = "1.5.0"
1212
log = "0.4.17"
@@ -52,13 +52,13 @@ network-interface = "2.0.0"
5252
futures-util = "0.3.30"
5353
[target.'cfg(target_os = "windows")'.dependencies]
5454
libloading = "0.8.0"
55-
windows-sys = {version = "0.59.0",features = [ "Win32_Foundation",
55+
windows-sys = { version = "0.59.0", features = ["Win32_Foundation",
5656
"Win32_NetworkManagement",
5757
"Win32_NetworkManagement_IpHelper",
5858
"Win32_Networking_WinSock",
5959
"Win32_System_IO",
6060
"Win32_System_Threading",
61-
"Win32_System_WindowsProgramming",]}
61+
"Win32_System_WindowsProgramming", ] }
6262

6363
[build-dependencies]
6464
protobuf-codegen = "=3.2.0"

vnt/src/handle/recv_data/server.rs

-2
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@ use packet::ip::ipv4;
1212
use packet::ip::ipv4::packet::IpV4Packet;
1313
use parking_lot::Mutex;
1414
use protobuf::Message;
15-
#[cfg(feature = "integrated_tun")]
16-
use tun_rs::AbstractDevice;
1715

1816
use crate::channel::context::ChannelContext;
1917
use crate::channel::{Route, RouteKey};

vnt/src/handle/tun_tap/platform.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@ use parking_lot::Mutex;
1313
use std::collections::HashMap;
1414
use std::net::Ipv4Addr;
1515
use std::sync::Arc;
16-
use tun_rs::platform::Device;
16+
use tun_rs::SyncDevice;
1717

1818
pub(crate) fn start_simple(
1919
stop_manager: StopManager,
2020
context: &ChannelContext,
21-
device: Arc<Device>,
21+
device: Arc<SyncDevice>,
2222
current_device: Arc<AtomicCell<CurrentDeviceInfo>>,
2323
ip_route: ExternalRoute,
2424
#[cfg(feature = "ip_proxy")] ip_proxy_map: Option<IpProxyMap>,
@@ -71,7 +71,7 @@ pub(crate) fn start_simple(
7171

7272
fn start_simple0(
7373
context: &ChannelContext,
74-
device: Arc<Device>,
74+
device: Arc<SyncDevice>,
7575
current_device: Arc<AtomicCell<CurrentDeviceInfo>>,
7676
ip_route: ExternalRoute,
7777
#[cfg(feature = "ip_proxy")] ip_proxy_map: Option<IpProxyMap>,

vnt/src/handle/tun_tap/tun_handler.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use std::collections::HashMap;
88
use std::net::Ipv4Addr;
99
use std::sync::Arc;
1010
use std::{io, thread};
11-
use tun_rs::platform::Device;
11+
use tun_rs::SyncDevice;
1212

1313
use crate::channel::context::ChannelContext;
1414
use crate::channel::sender::{send_to_wg, send_to_wg_broadcast};
@@ -26,7 +26,7 @@ use crate::protocol::body::ENCRYPTION_RESERVED;
2626
use crate::protocol::ip_turn_packet::BroadcastPacket;
2727
use crate::protocol::{ip_turn_packet, NetPacket, MAX_TTL};
2828
use crate::util::StopManager;
29-
fn icmp(device_writer: &Device, mut ipv4_packet: IpV4Packet<&mut [u8]>) -> anyhow::Result<()> {
29+
fn icmp(device_writer: &SyncDevice, mut ipv4_packet: IpV4Packet<&mut [u8]>) -> anyhow::Result<()> {
3030
if ipv4_packet.protocol() == Protocol::Icmp {
3131
let mut icmp = IcmpPacket::new(ipv4_packet.payload_mut())?;
3232
if icmp.kind() == Kind::EchoRequest {
@@ -45,7 +45,7 @@ fn icmp(device_writer: &Device, mut ipv4_packet: IpV4Packet<&mut [u8]>) -> anyho
4545
pub fn start(
4646
stop_manager: StopManager,
4747
context: ChannelContext,
48-
device: Arc<Device>,
48+
device: Arc<SyncDevice>,
4949
current_device: Arc<AtomicCell<CurrentDeviceInfo>>,
5050
ip_route: ExternalRoute,
5151
#[cfg(feature = "ip_proxy")] ip_proxy_map: Option<IpProxyMap>,
@@ -158,7 +158,7 @@ pub(crate) fn handle(
158158
buf: &mut [u8],
159159
data_len: usize, //数据总长度=12+ip包长度
160160
extend: &mut [u8],
161-
device_writer: &Device,
161+
device_writer: &SyncDevice,
162162
current_device: CurrentDeviceInfo,
163163
ip_route: &ExternalRoute,
164164
#[cfg(feature = "ip_proxy")] proxy_map: &Option<IpProxyMap>,

vnt/src/tun_tap_device/create_device.rs

+11-16
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
use std::io;
22
use std::net::Ipv4Addr;
33
use std::sync::Arc;
4-
use tun_rs::platform::Device;
5-
use tun_rs::AbstractDevice;
4+
use tun_rs::SyncDevice;
65

76
use crate::{DeviceConfig, ErrorInfo, ErrorType, VntCallback};
87

@@ -12,7 +11,7 @@ const DEFAULT_TUN_NAME: &str = "vnt-tun";
1211
pub fn create_device<Call: VntCallback>(
1312
config: DeviceConfig,
1413
call: &Call,
15-
) -> Result<Arc<Device>, ErrorInfo> {
14+
) -> Result<Arc<SyncDevice>, ErrorInfo> {
1615
let device = match create_device0(&config) {
1716
Ok(device) => device,
1817
Err(e) => {
@@ -53,28 +52,25 @@ pub fn create_device<Call: VntCallback>(
5352
Ok(device)
5453
}
5554

56-
fn create_device0(config: &DeviceConfig) -> io::Result<Arc<Device>> {
57-
let mut tun_config = tun_rs::Configuration::default();
58-
let netmask = u32::from_be_bytes(config.virtual_netmask.octets());
59-
tun_config
60-
.address_with_prefix(config.virtual_ip, netmask.count_ones() as u8)
61-
.up();
55+
fn create_device0(config: &DeviceConfig) -> io::Result<Arc<SyncDevice>> {
56+
let mut tun_builder = tun_rs::DeviceBuilder::default();
57+
tun_builder = tun_builder.ipv4(config.virtual_ip, config.virtual_netmask, None);
6258

6359
match &config.device_name {
6460
None => {
6561
#[cfg(any(target_os = "windows", target_os = "linux"))]
66-
tun_config.name(DEFAULT_TUN_NAME);
62+
{
63+
tun_builder = tun_builder.name(DEFAULT_TUN_NAME);
64+
}
6765
}
6866
Some(name) => {
69-
tun_config.name(name);
67+
tun_builder = tun_builder.name(name);
7068
}
7169
}
7270

7371
#[cfg(target_os = "windows")]
7472
{
75-
tun_config.platform_config(|v| {
76-
v.metric(0).ring_capacity(4 * 1024 * 1024);
77-
});
73+
tun_builder = tun_builder.metric(0).ring_capacity(4 * 1024 * 1024);
7874
}
7975

8076
#[cfg(target_os = "linux")]
@@ -88,8 +84,7 @@ fn create_device0(config: &DeviceConfig) -> io::Result<Arc<Device>> {
8884
}
8985
}
9086

91-
tun_config.mtu(config.mtu as u16);
92-
let device = tun_rs::create(&tun_config)?;
87+
let device = tun_builder.mtu(config.mtu as u16).build_sync()?;
9388
Ok(Arc::new(device))
9489
}
9590

vnt/src/tun_tap_device/tun_create_helper.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,16 @@ use crate::tun_tap_device::vnt_device::DeviceWrite;
1515
use crate::util::StopManager;
1616
use crossbeam_utils::atomic::AtomicCell;
1717
use parking_lot::Mutex;
18-
use tun_rs::platform::Device;
18+
use tun_rs::SyncDevice;
1919

2020
#[repr(transparent)]
2121
#[derive(Clone, Default)]
2222
pub struct DeviceAdapter {
23-
tun: Arc<Mutex<Option<Arc<Device>>>>,
23+
tun: Arc<Mutex<Option<Arc<SyncDevice>>>>,
2424
}
2525

2626
impl DeviceAdapter {
27-
pub fn insert(&self, device: Arc<Device>) {
27+
pub fn insert(&self, device: Arc<SyncDevice>) {
2828
let r = self.tun.lock().replace(device);
2929
assert!(r.is_none());
3030
}
@@ -116,7 +116,7 @@ impl TunDeviceHelper {
116116
}
117117
}
118118
/// 要保证先stop 再start
119-
pub fn start(&self, device: Arc<Device>, allow_wire_guard: bool) -> io::Result<()> {
119+
pub fn start(&self, device: Arc<SyncDevice>, allow_wire_guard: bool) -> io::Result<()> {
120120
self.device_adapter.insert(device.clone());
121121
let device_stop = DeviceStop::default();
122122
let s = self.device_stop.lock().replace(device_stop.clone());

0 commit comments

Comments
 (0)