[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 {
|
if &device_name == DEFAULT_NAME {
|
||||||
delete_device();
|
delete_device();
|
||||||
}
|
}
|
||||||
Arc::new(Device::new(Some(&device_name), config.tap)?)
|
Arc::new(Device::new(Some(device_name), config.tap)?)
|
||||||
};
|
};
|
||||||
#[cfg(target_os = "macos")]
|
#[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")]
|
#[cfg(target_os = "windows")]
|
||||||
let device = Arc::new(Device::new(
|
let device = Arc::new(Device::new(
|
||||||
&config
|
config
|
||||||
.device_name
|
.device_name
|
||||||
.as_ref()
|
.clone()
|
||||||
.unwrap_or(&DEFAULT_NAME.to_string()),
|
.unwrap_or(DEFAULT_NAME.to_string()),
|
||||||
config.tap,
|
config.tap,
|
||||||
)?);
|
)?);
|
||||||
#[cfg(target_os = "android")]
|
#[cfg(target_os = "android")]
|
||||||
|
|||||||
@@ -23,7 +23,7 @@ pub struct Device {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl 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 device = unsafe {
|
||||||
let dev = match name {
|
let dev = match name {
|
||||||
Some(name) => {
|
Some(name) => {
|
||||||
|
|||||||
@@ -20,7 +20,7 @@ pub struct Device {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl 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 {
|
let id = if let Some(name) = name {
|
||||||
if name.len() > IFNAMSIZ {
|
if name.len() > IFNAMSIZ {
|
||||||
return Err(io::Error::new(io::ErrorKind::InvalidInput, "name too long"));
|
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 {}",
|
"route -n add {} -netmask {} -interface {}",
|
||||||
address, netmask, name
|
address, netmask, name
|
||||||
);
|
);
|
||||||
exe_cmd(&cmd)
|
exe_cmd(&cmd)?;
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
pub fn del_route(name: &str, address: Ipv4Addr, netmask: Ipv4Addr) -> io::Result<()> {
|
pub fn del_route(name: &str, address: Ipv4Addr, netmask: Ipv4Addr) -> io::Result<()> {
|
||||||
let cmd = format!(
|
let cmd = format!(
|
||||||
"route -n delete {} -netmask {} -interface {}",
|
"route -n delete {} -netmask {} -interface {}",
|
||||||
address, netmask, name
|
address, netmask, name
|
||||||
);
|
);
|
||||||
exe_cmd(&cmd)
|
exe_cmd(&cmd)?;
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,7 +2,6 @@ use crate::device::IFace;
|
|||||||
use crate::windows::{tap, tun};
|
use crate::windows::{tap, tun};
|
||||||
use std::io;
|
use std::io;
|
||||||
use std::net::Ipv4Addr;
|
use std::net::Ipv4Addr;
|
||||||
use std::ops::Deref;
|
|
||||||
|
|
||||||
pub enum Device {
|
pub enum Device {
|
||||||
Tap(tap::Device),
|
Tap(tap::Device),
|
||||||
@@ -10,7 +9,7 @@ pub enum Device {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl 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 {
|
if tap {
|
||||||
Ok(Device::Tap(tap::Device::new(name)?))
|
Ok(Device::Tap(tap::Device::new(name)?))
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@@ -52,12 +52,11 @@ unsafe impl Sync for Device {}
|
|||||||
|
|
||||||
impl Device {
|
impl Device {
|
||||||
/// 打开设备,设置为TUN模式,激活网卡
|
/// 打开设备,设置为TUN模式,激活网卡
|
||||||
pub fn new(name_str: &str) -> io::Result<Self> {
|
pub fn new(name: String) -> io::Result<Self> {
|
||||||
let name = encode_utf16(name_str);
|
let luid = ffi::alias_to_luid(&encode_utf16(&name)).map_err(|e| {
|
||||||
let luid = ffi::alias_to_luid(&name).map_err(|e| {
|
|
||||||
io::Error::new(
|
io::Error::new(
|
||||||
e.kind(),
|
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)
|
let guid = ffi::luid_to_guid(&luid)
|
||||||
@@ -65,7 +64,7 @@ impl Device {
|
|||||||
.map_err(|e| {
|
.map_err(|e| {
|
||||||
io::Error::new(
|
io::Error::new(
|
||||||
e.kind(),
|
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));
|
let path = format!(r"\\.\Global\{}.tap", decode_utf16(&guid));
|
||||||
@@ -76,7 +75,7 @@ impl Device {
|
|||||||
OPEN_EXISTING,
|
OPEN_EXISTING,
|
||||||
FILE_ATTRIBUTE_SYSTEM | FILE_FLAG_OVERLAPPED,
|
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地址和掩码
|
// ep保存tun网卡的IP地址和掩码
|
||||||
// let mut ep = [0;3];
|
// let mut ep = [0;3];
|
||||||
@@ -97,7 +96,7 @@ impl Device {
|
|||||||
.map_err(|e| {
|
.map_err(|e| {
|
||||||
io::Error::new(
|
io::Error::new(
|
||||||
e.kind(),
|
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)))?;
|
.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 {}
|
unsafe impl Sync for Device {}
|
||||||
|
|
||||||
impl Device {
|
impl Device {
|
||||||
pub fn new(name: &str) -> io::Result<Self> {
|
pub fn new(name: String) -> io::Result<Self> {
|
||||||
unsafe {
|
unsafe {
|
||||||
let library = match Library::new("wintun.dll") {
|
let library = match Library::new("wintun.dll") {
|
||||||
Ok(library) => library,
|
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 {
|
if name_utf16.len() > MAX_POOL {
|
||||||
return Err(io::Error::new(
|
return Err(io::Error::new(
|
||||||
io::ErrorKind::Other,
|
io::ErrorKind::Other,
|
||||||
|
|||||||
Reference in New Issue
Block a user