diff --git a/vnt/src/tun_tap_device/mod.rs b/vnt/src/tun_tap_device/mod.rs index 246822e..34993ba 100644 --- a/vnt/src/tun_tap_device/mod.rs +++ b/vnt/src/tun_tap_device/mod.rs @@ -18,16 +18,16 @@ pub fn create_device(config: &Config) -> io::Result> { if &device_name == DEFAULT_NAME { delete_device(); } - Arc::new(Device::new(Some(&device_name), config.tap)?) + Arc::new(Device::new(Some(device_name), config.tap)?) }; #[cfg(target_os = "macos")] - let device = Arc::new(Device::new(Some(&config.device_name.clone()))?); + let device = Arc::new(Device::new(config.device_name.clone())?); #[cfg(target_os = "windows")] let device = Arc::new(Device::new( - &config + config .device_name - .as_ref() - .unwrap_or(&DEFAULT_NAME.to_string()), + .clone() + .unwrap_or(DEFAULT_NAME.to_string()), config.tap, )?); #[cfg(target_os = "android")] diff --git a/vnt/tun/src/linux/device.rs b/vnt/tun/src/linux/device.rs index 25568d8..7b2af7d 100644 --- a/vnt/tun/src/linux/device.rs +++ b/vnt/tun/src/linux/device.rs @@ -23,7 +23,7 @@ pub struct Device { } impl Device { - pub fn new(name: Option<&str>, tap: bool) -> io::Result { + pub fn new(name: Option, tap: bool) -> io::Result { let device = unsafe { let dev = match name { Some(name) => { diff --git a/vnt/tun/src/macos/device.rs b/vnt/tun/src/macos/device.rs index eb7ef9f..66bcbbc 100644 --- a/vnt/tun/src/macos/device.rs +++ b/vnt/tun/src/macos/device.rs @@ -20,7 +20,7 @@ pub struct Device { } impl Device { - pub fn new(name: Option<&str>) -> io::Result { + pub fn new(name: Option) -> io::Result { let id = if let Some(name) = name { if name.len() > IFNAMSIZ { return Err(io::Error::new(io::ErrorKind::InvalidInput, "name too long")); diff --git a/vnt/tun/src/macos/route.rs b/vnt/tun/src/macos/route.rs index baab9a7..d4644b1 100644 --- a/vnt/tun/src/macos/route.rs +++ b/vnt/tun/src/macos/route.rs @@ -7,12 +7,14 @@ pub fn add_route(name: &str, address: Ipv4Addr, netmask: Ipv4Addr) -> io::Result "route -n add {} -netmask {} -interface {}", address, netmask, name ); - exe_cmd(&cmd) + exe_cmd(&cmd)?; + Ok(()) } pub fn del_route(name: &str, address: Ipv4Addr, netmask: Ipv4Addr) -> io::Result<()> { let cmd = format!( "route -n delete {} -netmask {} -interface {}", address, netmask, name ); - exe_cmd(&cmd) + exe_cmd(&cmd)?; + Ok(()) } diff --git a/vnt/tun/src/windows/device.rs b/vnt/tun/src/windows/device.rs index 208e833..a557125 100644 --- a/vnt/tun/src/windows/device.rs +++ b/vnt/tun/src/windows/device.rs @@ -2,7 +2,6 @@ use crate::device::IFace; use crate::windows::{tap, tun}; use std::io; use std::net::Ipv4Addr; -use std::ops::Deref; pub enum Device { Tap(tap::Device), @@ -10,7 +9,7 @@ pub enum Device { } impl Device { - pub fn new(name: &str, tap: bool) -> io::Result { + pub fn new(name: String, tap: bool) -> io::Result { if tap { Ok(Device::Tap(tap::Device::new(name)?)) } else { diff --git a/vnt/tun/src/windows/tap/mod.rs b/vnt/tun/src/windows/tap/mod.rs index c157522..7f4d76b 100644 --- a/vnt/tun/src/windows/tap/mod.rs +++ b/vnt/tun/src/windows/tap/mod.rs @@ -52,12 +52,11 @@ unsafe impl Sync for Device {} impl Device { /// 打开设备,设置为TUN模式,激活网卡 - pub fn new(name_str: &str) -> io::Result { - let name = encode_utf16(name_str); - let luid = ffi::alias_to_luid(&name).map_err(|e| { + pub fn new(name: String) -> io::Result { + let luid = ffi::alias_to_luid(&encode_utf16(&name)).map_err(|e| { io::Error::new( e.kind(), - format!("alias_to_luid name={},err={:?}", name_str, e), + format!("alias_to_luid name={},err={:?}", name, e), ) })?; let guid = ffi::luid_to_guid(&luid) @@ -65,7 +64,7 @@ impl Device { .map_err(|e| { io::Error::new( e.kind(), - format!("luid_to_guid name={},err={:?}", name_str, e), + format!("luid_to_guid name={},err={:?}", name, e), ) })?; let path = format!(r"\\.\Global\{}.tap", decode_utf16(&guid)); @@ -76,7 +75,7 @@ impl Device { OPEN_EXISTING, FILE_ATTRIBUTE_SYSTEM | FILE_FLAG_OVERLAPPED, ) - .map_err(|e| io::Error::new(e.kind(), format!("tap name={},err={:?}", name_str, e)))?; + .map_err(|e| io::Error::new(e.kind(), format!("tap name={},err={:?}", name, e)))?; // ep保存tun网卡的IP地址和掩码 // let mut ep = [0;3]; @@ -97,7 +96,7 @@ impl Device { .map_err(|e| { io::Error::new( e.kind(), - format!("TAP_WIN_IOCTL_CONFIG_TUN name={},err={:?}", name_str, e), + format!("TAP_WIN_IOCTL_CONFIG_TUN name={},err={:?}", name, e), ) }) .map_err(|e| io::Error::new(e.kind(), format!("TAP_WIN_IOCTL_GET_MAC,err={:?}", e)))?; diff --git a/vnt/tun/src/windows/tun/mod.rs b/vnt/tun/src/windows/tun/mod.rs index 3a01dc5..c95b208 100644 --- a/vnt/tun/src/windows/tun/mod.rs +++ b/vnt/tun/src/windows/tun/mod.rs @@ -49,7 +49,7 @@ unsafe impl Send for Device {} unsafe impl Sync for Device {} impl Device { - pub fn new(name: &str) -> io::Result { + pub fn new(name: String) -> io::Result { unsafe { let library = match Library::new("wintun.dll") { Ok(library) => library, @@ -69,7 +69,7 @@ impl Device { )); } }; - let name_utf16 = encode_utf16(name); + let name_utf16 = encode_utf16(&name); if name_utf16.len() > MAX_POOL { return Err(io::Error::new( io::ErrorKind::Other,