renet/src/phy/sys/tuntap_interface.rs

120 lines
3.3 KiB
Rust
Raw Normal View History

2016-12-11 07:15:26 +08:00
use super::*;
2021-03-25 01:04:42 +08:00
use crate::{phy::Medium, wire::EthernetFrame};
2021-06-27 15:31:59 +08:00
use std::io;
use std::os::unix::io::{AsRawFd, RawFd};
2016-12-11 07:15:26 +08:00
#[derive(Debug)]
2021-03-25 01:04:42 +08:00
pub struct TunTapInterfaceDesc {
2016-12-11 07:15:26 +08:00
lower: libc::c_int,
2021-03-25 01:04:42 +08:00
ifreq: ifreq,
medium: Medium,
2016-12-11 07:15:26 +08:00
}
2021-03-25 01:04:42 +08:00
impl AsRawFd for TunTapInterfaceDesc {
fn as_raw_fd(&self) -> RawFd {
self.lower
}
}
2021-03-25 01:04:42 +08:00
impl TunTapInterfaceDesc {
pub fn new(name: &str, medium: Medium) -> io::Result<TunTapInterfaceDesc> {
2016-12-11 07:15:26 +08:00
let lower = unsafe {
2021-06-27 15:31:59 +08:00
let lower = libc::open(
"/dev/net/tun\0".as_ptr() as *const libc::c_char,
libc::O_RDWR | libc::O_NONBLOCK,
);
if lower == -1 {
return Err(io::Error::last_os_error());
}
2016-12-11 07:15:26 +08:00
lower
};
2021-03-25 01:04:42 +08:00
Ok(TunTapInterfaceDesc {
lower,
ifreq: ifreq_for(name),
medium,
2016-12-11 07:15:26 +08:00
})
}
pub fn attach_interface(&mut self) -> io::Result<()> {
2021-03-25 01:04:42 +08:00
let mode = match self.medium {
#[cfg(feature = "medium-ip")]
Medium::Ip => imp::IFF_TUN,
#[cfg(feature = "medium-ethernet")]
Medium::Ethernet => imp::IFF_TAP,
2021-04-29 18:04:46 +08:00
#[cfg(feature = "medium-ieee802154")]
Medium::Ieee802154 => todo!(),
2021-03-25 01:04:42 +08:00
};
self.ifreq.ifr_data = mode | imp::IFF_NO_PI;
2016-12-11 07:15:26 +08:00
ifreq_ioctl(self.lower, &mut self.ifreq, imp::TUNSETIFF).map(|_| ())
}
pub fn interface_mtu(&mut self) -> io::Result<usize> {
let lower = unsafe {
let lower = libc::socket(libc::AF_INET, libc::SOCK_DGRAM, libc::IPPROTO_IP);
2021-06-27 15:31:59 +08:00
if lower == -1 {
return Err(io::Error::last_os_error());
}
lower
};
let ip_mtu = ifreq_ioctl(lower, &mut self.ifreq, imp::SIOCGIFMTU).map(|mtu| mtu as usize);
2021-06-27 15:31:59 +08:00
unsafe {
libc::close(lower);
}
2021-03-25 01:04:42 +08:00
// Propagate error after close, to ensure we always close.
let ip_mtu = ip_mtu?;
// SIOCGIFMTU returns the IP MTU (typically 1500 bytes.)
// smoltcp counts the entire Ethernet packet in the MTU, so add the Ethernet header size to it.
2021-03-25 01:04:42 +08:00
let mtu = match self.medium {
#[cfg(feature = "medium-ip")]
Medium::Ip => ip_mtu,
#[cfg(feature = "medium-ethernet")]
Medium::Ethernet => ip_mtu + EthernetFrame::<&[u8]>::header_len(),
2021-04-29 18:04:46 +08:00
#[cfg(feature = "medium-ieee802154")]
Medium::Ieee802154 => todo!(),
2021-03-25 01:04:42 +08:00
};
Ok(mtu)
}
2016-12-11 07:15:26 +08:00
pub fn recv(&mut self, buffer: &mut [u8]) -> io::Result<usize> {
unsafe {
2021-06-27 15:31:59 +08:00
let len = libc::read(
self.lower,
buffer.as_mut_ptr() as *mut libc::c_void,
buffer.len(),
);
if len == -1 {
return Err(io::Error::last_os_error());
}
2016-12-11 07:15:26 +08:00
Ok(len as usize)
}
}
pub fn send(&mut self, buffer: &[u8]) -> io::Result<usize> {
unsafe {
2021-06-27 15:31:59 +08:00
let len = libc::write(
self.lower,
buffer.as_ptr() as *const libc::c_void,
buffer.len(),
);
if len == -1 {
Err(io::Error::last_os_error()).unwrap()
}
2016-12-11 07:15:26 +08:00
Ok(len as usize)
}
}
}
2021-03-25 01:04:42 +08:00
impl Drop for TunTapInterfaceDesc {
2016-12-11 07:15:26 +08:00
fn drop(&mut self) {
2021-06-27 15:31:59 +08:00
unsafe {
libc::close(self.lower);
}
2016-12-11 07:15:26 +08:00
}
}