not sure what's not working, but initial work on the trait
This commit is contained in:
Cara Salter 2023-04-22 21:04:14 -04:00
parent 1a13b77295
commit 8fefd54d90
No known key found for this signature in database
GPG key ID: A8A3A601440EADA5
5 changed files with 112 additions and 73 deletions

View file

@ -2,19 +2,17 @@ use std::{mem::MaybeUninit, net::Ipv4Addr};
use tokio::io::Result; use tokio::io::Result;
use tun::TunInterface; use tun::TunInterface;
use tun_async::TunQueue; use tun::TunQueue;
async fn try_main() -> Result<()> { async fn try_main() -> Result<()> {
let iface = TunInterface::new()?; let iface = tun::create_interface();
iface.set_ipv4_addr(Ipv4Addr::new(10, 0, 0, 2))?; iface.set_ipv4(Ipv4Addr::new(10, 0, 0, 2))?;
println!("{:?}", iface.index()?);
println!("{:?}", iface.ipv4_addr()?);
let queue = TunQueue::from_queue(iface.into())?; let queue = TunQueue::from(iface);
loop { loop {
let mut buf = [MaybeUninit::<u8>::uninit(); 1500]; let mut buf = [MaybeUninit::<u8>::uninit(); 1500];
let len = queue.try_recv(&mut buf).await?; let len = queue.recv(&mut buf)?;
println!("Received {len} bytes"); println!("Received {len} bytes");
} }
} }

View file

@ -6,4 +6,37 @@ mod imp;
#[path = "unix/mod.rs"] #[path = "unix/mod.rs"]
pub(crate) mod imp; pub(crate) mod imp;
pub use imp::{TunInterface, TunQueue}; /**
* Standard platform-independent interface for a tunnel.
*/
pub trait TunInterface {
/**
* Sets the interface IP address. Accepts either IPv6 or IPv4
*/
fn set_ip(&self, ip: IpAddr) -> Result<(), std::io::Error>;
/**
* Sets the interface IP address to an IPv4 address.
*
* Used by [set_ip](TunInterface::set_ip)
*/
fn set_ipv4(&self, ip: Ipv4Addr) -> Result<(), std::io::Error>;
fn get_ip(&self) -> Result<IpAddr, std::io::Error>;
fn get_interface_name(&self) -> Result<String, std::io::Error>;
fn into_raw_socket(self) -> socket2::Socket;
}
#[cfg(target_os = "linux")]
pub fn create_interface() -> impl TunInterface {
PlatformTun::new().unwrap()
}
use std::{
net::{IpAddr, Ipv4Addr},
sync::Mutex,
};
pub use imp::{PlatformTun, TunQueue};

View file

@ -4,23 +4,35 @@ use socket2::{Domain, SockAddr, Socket, Type};
use std::fs::OpenOptions; use std::fs::OpenOptions;
use std::io::Error; use std::io::Error;
use std::mem; use std::mem;
use std::net::{Ipv4Addr, SocketAddrV4}; use std::net::{IpAddr, Ipv4Addr, SocketAddrV4};
use std::os::fd::RawFd; use std::os::fd::RawFd;
use std::os::unix::io::{AsRawFd, FromRawFd, IntoRawFd}; use std::os::unix::io::{AsRawFd, FromRawFd, IntoRawFd};
use std::ptr;
use std::sync::{Arc, Mutex};
use super::{ifname_to_string, string_to_ifname}; use super::{ifname_to_string, string_to_ifname};
use crate::TunInterface;
mod sys; mod sys;
#[derive(Debug)] #[derive(Debug)]
pub struct TunInterface { pub struct PlatformTun {
pub(crate) socket: socket2::Socket, pub(crate) socket: socket2::Socket,
} }
impl TunInterface { impl PlatformTun {
#[throws] #[throws]
pub fn new() -> TunInterface { pub fn index(&self) -> i32 {
let socket = Socket::new(Domain::IPV4, Type::DGRAM, None)?;
unsafe {
let mut iff: libc::ifreq = mem::zeroed();
iff.ifr_name = string_to_ifname(&self.get_interface_name()?);
sys::if_get_index(socket.as_raw_fd(), &mut iff)?;
iff.ifr_ifru.ifru_ifindex
}
}
#[throws]
pub(crate) fn new() -> PlatformTun {
let file = OpenOptions::new() let file = OpenOptions::new()
.read(true) .read(true)
.write(true) .write(true)
@ -35,53 +47,47 @@ impl TunInterface {
unsafe { sys::tun_set_iff(file.as_raw_fd(), &iff)? }; unsafe { sys::tun_set_iff(file.as_raw_fd(), &iff)? };
let socket = unsafe { socket2::Socket::from_raw_fd(file.into_raw_fd()) }; let socket = unsafe { socket2::Socket::from_raw_fd(file.into_raw_fd()) };
TunInterface { socket } PlatformTun { socket }
}
} }
impl TunInterface for PlatformTun {
#[throws] #[throws]
pub fn name(&self) -> String { fn get_interface_name(&self) -> String {
let mut iff = unsafe { mem::zeroed() }; unsafe {
unsafe { sys::tun_get_iff(self.socket.as_raw_fd(), &mut iff)? }; let mut iff = mem::zeroed();
sys::tun_get_iff(self.socket.as_raw_fd(), &mut iff)?;
ifname_to_string(iff.ifr_name) ifname_to_string(iff.ifr_name)
} }
#[throws]
fn ifreq(&self) -> sys::ifreq {
let mut iff: sys::ifreq = unsafe { mem::zeroed() };
iff.ifr_name = string_to_ifname(&self.name()?);
iff
} }
#[throws] #[throws]
pub fn index(&self) -> i32 { fn set_ip(&self, addr: IpAddr) {
let mut iff = self.ifreq()?; match addr {
self.perform(|fd| unsafe { sys::if_get_index(fd, &mut iff) })?; IpAddr::V4(addr) => self.set_ipv4(addr)?,
unsafe { iff.ifr_ifru.ifru_ifindex } _ => (),
}
} }
#[throws] #[throws]
pub fn set_ipv4_addr(&self, addr: Ipv4Addr) { fn set_ipv4(&self, addr: Ipv4Addr) {
let addr = SockAddr::from(SocketAddrV4::new(addr, 0));
let mut iff = self.ifreq()?;
iff.ifr_ifru.ifru_addr = unsafe { *addr.as_ptr() };
self.perform(|fd| unsafe { sys::if_set_addr(fd, &iff) })?;
}
#[throws]
pub fn ipv4_addr(&self) -> Ipv4Addr {
let mut iff = self.ifreq()?;
self.perform(|fd| unsafe { sys::if_get_addr(fd, &mut iff) })?;
let addr = unsafe { &*(&iff.ifr_ifru.ifru_addr as *const _ as *const sys::sockaddr_in) };
Ipv4Addr::from(addr.sin_addr.s_addr)
}
#[throws]
fn perform<R>(&self, perform: impl FnOnce(RawFd) -> Result<R, nix::Error>) -> R {
let socket = Socket::new(Domain::IPV4, Type::DGRAM, None)?; let socket = Socket::new(Domain::IPV4, Type::DGRAM, None)?;
perform(socket.as_raw_fd())? let addr = SockAddr::from(SocketAddrV4::new(addr, 0));
unsafe {
let mut iff: libc::ifreq = mem::zeroed();
iff.ifr_name = string_to_ifname(&self.get_interface_name()?);
iff.ifr_ifru.ifru_addr = *addr.as_ptr();
sys::if_set_addr(socket.as_raw_fd(), &iff)?;
}
}
#[throws]
fn get_ip(&self) -> IpAddr {
let addr = self.socket.local_addr()?;
addr.as_socket().unwrap().ip()
}
fn into_raw_socket(self) -> socket2::Socket {
self.socket
} }
} }

View file

@ -8,11 +8,11 @@ mod imp;
#[path = "linux/mod.rs"] #[path = "linux/mod.rs"]
mod imp; mod imp;
pub use crate::TunInterface;
pub use imp::PlatformTun;
pub use imp::TunInterface; use libc::IFNAMSIZ;
pub use queue::TunQueue; pub use queue::TunQueue;
use std::ffi::CString;
pub fn ifname_to_string(buf: [libc::c_char; libc::IFNAMSIZ]) -> String { pub fn ifname_to_string(buf: [libc::c_char; libc::IFNAMSIZ]) -> String {
// TODO: Switch to `CStr::from_bytes_until_nul` when stabilized // TODO: Switch to `CStr::from_bytes_until_nul` when stabilized

View file

@ -38,10 +38,10 @@ impl Write for TunQueue {
} }
} }
impl From<TunInterface> for TunQueue { impl TunQueue {
fn from(interface: TunInterface) -> TunQueue { pub fn from<T: TunInterface>(interface: T) -> TunQueue {
TunQueue { TunQueue {
socket: interface.socket, socket: interface.into_raw_socket(),
} }
} }
} }
@ -52,8 +52,10 @@ impl AsRawFd for TunQueue {
} }
} }
impl IntoRawFd for TunQueue { //TODO: error[E0507]: cannot move out of `*self.socket` which is behind a mutable reference
fn into_raw_fd(self) -> RawFd {
self.socket.into_raw_fd() //impl IntoRawFd for TunQueue {
} // fn into_raw_fd(self) -> RawFd {
} // self.socket.into_raw_fd()
// }
//}