[mio] 网卡名称使用String
This commit is contained in:
@@ -18,16 +18,16 @@ pub fn create_device(config: &Config) -> io::Result<Arc<Device>> {
|
||||
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")]
|
||||
|
||||
@@ -23,7 +23,7 @@ pub struct Device {
|
||||
}
|
||||
|
||||
impl Device {
|
||||
pub fn new(name: Option<&str>, tap: bool) -> io::Result<Self> {
|
||||
pub fn new(name: Option<String>, tap: bool) -> io::Result<Self> {
|
||||
let device = unsafe {
|
||||
let dev = match name {
|
||||
Some(name) => {
|
||||
|
||||
@@ -20,7 +20,7 @@ pub struct Device {
|
||||
}
|
||||
|
||||
impl Device {
|
||||
pub fn new(name: Option<&str>) -> io::Result<Self> {
|
||||
pub fn new(name: Option<String>) -> io::Result<Self> {
|
||||
let id = if let Some(name) = name {
|
||||
if name.len() > IFNAMSIZ {
|
||||
return Err(io::Error::new(io::ErrorKind::InvalidInput, "name too long"));
|
||||
|
||||
@@ -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(())
|
||||
}
|
||||
|
||||
@@ -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<Self> {
|
||||
pub fn new(name: String, tap: bool) -> io::Result<Self> {
|
||||
if tap {
|
||||
Ok(Device::Tap(tap::Device::new(name)?))
|
||||
} else {
|
||||
|
||||
@@ -52,12 +52,11 @@ unsafe impl Sync for Device {}
|
||||
|
||||
impl Device {
|
||||
/// 打开设备,设置为TUN模式,激活网卡
|
||||
pub fn new(name_str: &str) -> io::Result<Self> {
|
||||
let name = encode_utf16(name_str);
|
||||
let luid = ffi::alias_to_luid(&name).map_err(|e| {
|
||||
pub fn new(name: String) -> io::Result<Self> {
|
||||
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)))?;
|
||||
|
||||
@@ -49,7 +49,7 @@ unsafe impl Send for Device {}
|
||||
unsafe impl Sync for Device {}
|
||||
|
||||
impl Device {
|
||||
pub fn new(name: &str) -> io::Result<Self> {
|
||||
pub fn new(name: String) -> io::Result<Self> {
|
||||
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,
|
||||
|
||||
Reference in New Issue
Block a user