Add read and write functions for TunInterface

This adds read and write functionality for TunInterface.
This commit is contained in:
JettChenT 2023-05-29 10:36:10 +08:00 committed by Conrad Kramer
parent 9aa1951575
commit 82c4d218d7
5 changed files with 105 additions and 4 deletions

1
Cargo.lock generated
View file

@ -1298,6 +1298,7 @@ version = "0.1.0"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"bindgen", "bindgen",
"byteorder",
"fehler", "fehler",
"lazy_static", "lazy_static",
"libc", "libc",

View file

@ -9,6 +9,7 @@ fehler = "1.0"
nix = { version = "0.26", features = ["ioctl"] } nix = { version = "0.26", features = ["ioctl"] }
socket2 = "0.4" socket2 = "0.4"
tokio = { version = "1.28", features = [] } tokio = { version = "1.28", features = [] }
byteorder = "1.4"
[target.'cfg(windows)'.dependencies] [target.'cfg(windows)'.dependencies]
lazy_static = "1.4" lazy_static = "1.4"

View file

@ -1,9 +1,14 @@
use byteorder::{ByteOrder, NetworkEndian};
use fehler::throws; use fehler::throws;
use libc::c_char; use libc::{c_char, iovec, writev, AF_INET, AF_INET6};
use socket2::{Domain, SockAddr, Socket, Type}; use socket2::{Domain, SockAddr, Socket, Type};
use std::io::{IoSlice, Write};
use std::net::{Ipv4Addr, SocketAddrV4}; use std::net::{Ipv4Addr, SocketAddrV4};
use std::os::fd::{AsRawFd, RawFd}; use std::os::fd::{AsRawFd, RawFd};
use std::{io::Error, mem}; use std::{
io::{Error, Read},
mem,
};
mod kern_control; mod kern_control;
mod sys; mod sys;
@ -122,6 +127,36 @@ impl TunInterface {
} }
} }
impl Write for TunInterface {
#[throws]
fn write(&mut self, buf: &[u8]) -> usize {
use std::io::ErrorKind;
let proto = match buf[0] >> 4 {
6 => Ok(AF_INET6),
4 => Ok(AF_INET),
_ => Err(Error::new(ErrorKind::InvalidInput, "Invalid IP version")),
}?;
let mut pbuf = [0; 4];
NetworkEndian::write_i32(&mut pbuf, proto);
let bufs = [IoSlice::new(&pbuf), IoSlice::new(buf)];
let bytes_written: isize = unsafe {
writev(
self.as_raw_fd(),
bufs.as_ptr() as *const iovec,
bufs.len() as i32,
)
};
bytes_written
.try_into()
.map_err(|_| Error::new(ErrorKind::Other, "Conversion error"))?
}
fn flush(&mut self) -> std::io::Result<()> {
Ok(())
}
}
#[cfg(test)] #[cfg(test)]
mod test { mod test {
use super::*; use super::*;

View file

@ -2,7 +2,7 @@ use fehler::throws;
use socket2::{Domain, SockAddr, Socket, Type}; use socket2::{Domain, SockAddr, Socket, Type};
use std::fs::OpenOptions; use std::fs::OpenOptions;
use std::io::Error; use std::io::{Error, Write};
use std::mem; use std::mem;
use std::net::{Ipv4Addr, Ipv6Addr, SocketAddrV4, SocketAddrV6}; use std::net::{Ipv4Addr, Ipv6Addr, SocketAddrV4, SocketAddrV6};
use std::os::fd::RawFd; use std::os::fd::RawFd;
@ -140,6 +140,17 @@ impl TunInterface {
} }
} }
impl Write for TunInterface {
#[throws]
fn write(&mut self, buf: &[u8]) -> usize {
self.socket.send(buf)?
}
fn flush(&mut self) -> std::io::Result<()> {
Ok(())
}
}
#[cfg(test)] #[cfg(test)]
mod test { mod test {
use super::TunInterface; use super::TunInterface;

View file

@ -1,4 +1,7 @@
use std::os::fd::{AsRawFd, FromRawFd, IntoRawFd, RawFd}; use std::{
io::{Error, Read},
os::fd::{AsRawFd, FromRawFd, IntoRawFd, RawFd},
};
mod queue; mod queue;
@ -10,6 +13,7 @@ mod imp;
#[path = "linux/mod.rs"] #[path = "linux/mod.rs"]
mod imp; mod imp;
use fehler::throws;
pub use imp::TunInterface; pub use imp::TunInterface;
pub use queue::TunQueue; pub use queue::TunQueue;
@ -32,6 +36,19 @@ impl IntoRawFd for TunInterface {
self.socket.into_raw_fd() self.socket.into_raw_fd()
} }
} }
impl TunInterface {
// #[throws]
// pub fn write(&self, buf: &[u8]) -> usize {
// self.socket.send(buf)?
// }
#[throws]
pub fn read(&mut self, buf: &mut [u8]) -> usize {
self.socket.read(buf)?
}
}
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
unsafe { unsafe {
@ -48,3 +65,39 @@ pub fn string_to_ifname(name: &str) -> [libc::c_char; libc::IFNAMSIZ] {
buf[..len].copy_from_slice(unsafe { &*(name.as_bytes() as *const _ as *const [libc::c_char]) }); buf[..len].copy_from_slice(unsafe { &*(name.as_bytes() as *const _ as *const [libc::c_char]) });
buf buf
} }
#[cfg(test)]
mod test {
use super::*;
use std::io::Write;
use std::io::{self, BufRead};
use std::net::Ipv4Addr;
#[throws]
#[test]
fn tst_read() {
// This test is interactive, you need to send a packet to any server through 192.168.1.10
// EG. `sudo route add 8.8.8.8 192.168.1.10`,
//`dig @8.8.8.8 hackclub.com`
let mut tun = TunInterface::new()?;
println!("tun name: {:?}", tun.name()?);
tun.set_ipv4_addr(Ipv4Addr::from([192, 168, 1, 10]))?;
println!("tun ip: {:?}", tun.ipv4_addr()?);
println!("Waiting for a packet...");
let buf = &mut [0u8; 1500];
let res = tun.read(buf);
println!("Received!");
assert!(res.is_ok());
}
#[test]
#[throws]
fn write_packets() {
let mut tun = TunInterface::new()?;
let mut buf = [0u8; 1500];
buf[0] = 6 << 4;
let bytes_written = tun.write(&buf)?;
assert_eq!(bytes_written, 1504);
}
}