Switch logging to use tracing instead of log
Tracing has support for intervals and a great os_log integration.
This commit is contained in:
parent
60cfd95789
commit
e643d9dd41
14 changed files with 297 additions and 8 deletions
|
|
@ -1,12 +1,13 @@
|
|||
use byteorder::{ByteOrder, NetworkEndian};
|
||||
use fehler::throws;
|
||||
use libc::{c_char, iovec, writev, AF_INET, AF_INET6};
|
||||
use log::info;
|
||||
use tracing::info;
|
||||
use socket2::{Domain, SockAddr, Socket, Type};
|
||||
use std::io::IoSlice;
|
||||
use std::net::{Ipv4Addr, SocketAddrV4};
|
||||
use std::os::fd::{AsRawFd, RawFd};
|
||||
use std::{io::Error, mem};
|
||||
use tracing::instrument;
|
||||
|
||||
mod kern_control;
|
||||
mod sys;
|
||||
|
|
@ -23,16 +24,19 @@ pub struct TunInterface {
|
|||
|
||||
impl TunInterface {
|
||||
#[throws]
|
||||
#[instrument]
|
||||
pub fn new() -> TunInterface {
|
||||
Self::new_with_options(TunOptions::new())?
|
||||
}
|
||||
|
||||
#[throws]
|
||||
#[instrument]
|
||||
pub fn new_with_options(_: TunOptions) -> TunInterface {
|
||||
TunInterface::connect(0)?
|
||||
}
|
||||
|
||||
#[throws]
|
||||
#[instrument]
|
||||
fn connect(index: u32) -> TunInterface {
|
||||
use socket2::{Domain, Protocol, Socket, Type};
|
||||
|
||||
|
|
@ -48,6 +52,7 @@ impl TunInterface {
|
|||
}
|
||||
|
||||
#[throws]
|
||||
#[instrument]
|
||||
pub fn name(&self) -> String {
|
||||
let mut buf = [0 as c_char; sys::IFNAMSIZ];
|
||||
let mut len = buf.len() as sys::socklen_t;
|
||||
|
|
@ -62,6 +67,7 @@ impl TunInterface {
|
|||
}
|
||||
|
||||
#[throws]
|
||||
#[instrument]
|
||||
fn ifreq(&self) -> sys::ifreq {
|
||||
let mut iff: sys::ifreq = unsafe { mem::zeroed() };
|
||||
iff.ifr_name = string_to_ifname(&self.name()?);
|
||||
|
|
@ -69,6 +75,7 @@ impl TunInterface {
|
|||
}
|
||||
|
||||
#[throws]
|
||||
#[instrument]
|
||||
pub fn set_ipv4_addr(&self, addr: Ipv4Addr) {
|
||||
let addr = SockAddr::from(SocketAddrV4::new(addr, 0));
|
||||
let mut iff = self.ifreq()?;
|
||||
|
|
@ -78,6 +85,7 @@ impl TunInterface {
|
|||
}
|
||||
|
||||
#[throws]
|
||||
#[instrument]
|
||||
pub fn ipv4_addr(&self) -> Ipv4Addr {
|
||||
let mut iff = self.ifreq()?;
|
||||
self.perform(|fd| unsafe { sys::if_get_addr(fd, &mut iff) })?;
|
||||
|
|
@ -87,11 +95,15 @@ impl TunInterface {
|
|||
|
||||
#[throws]
|
||||
fn perform<R>(&self, perform: impl FnOnce(RawFd) -> Result<R, nix::Error>) -> R {
|
||||
let span = tracing::info_span!("perform", fd = self.as_raw_fd());
|
||||
let _enter = span.enter();
|
||||
|
||||
let socket = Socket::new(Domain::IPV4, Type::DGRAM, None)?;
|
||||
perform(socket.as_raw_fd())?
|
||||
}
|
||||
|
||||
#[throws]
|
||||
#[instrument]
|
||||
pub fn mtu(&self) -> i32 {
|
||||
let mut iff = self.ifreq()?;
|
||||
self.perform(|fd| unsafe { sys::if_get_mtu(fd, &mut iff) })?;
|
||||
|
|
@ -101,6 +113,7 @@ impl TunInterface {
|
|||
}
|
||||
|
||||
#[throws]
|
||||
#[instrument]
|
||||
pub fn set_mtu(&self, mtu: i32) {
|
||||
let mut iff = self.ifreq()?;
|
||||
iff.ifr_ifru.ifru_mtu = mtu;
|
||||
|
|
@ -109,6 +122,7 @@ impl TunInterface {
|
|||
}
|
||||
|
||||
#[throws]
|
||||
#[instrument]
|
||||
pub fn netmask(&self) -> Ipv4Addr {
|
||||
let mut iff = self.ifreq()?;
|
||||
self.perform(|fd| unsafe { sys::if_get_netmask(fd, &mut iff) })?;
|
||||
|
|
@ -120,6 +134,7 @@ impl TunInterface {
|
|||
}
|
||||
|
||||
#[throws]
|
||||
#[instrument]
|
||||
pub fn set_netmask(&self, addr: Ipv4Addr) {
|
||||
let addr = SockAddr::from(SocketAddrV4::new(addr, 0));
|
||||
let mut iff = self.ifreq()?;
|
||||
|
|
@ -133,6 +148,7 @@ impl TunInterface {
|
|||
}
|
||||
|
||||
#[throws]
|
||||
#[instrument]
|
||||
pub fn send(&self, buf: &[u8]) -> usize {
|
||||
use std::io::ErrorKind;
|
||||
let proto = match buf[0] >> 4 {
|
||||
|
|
|
|||
|
|
@ -8,7 +8,7 @@ use std::net::{Ipv4Addr, Ipv6Addr, SocketAddrV4};
|
|||
use std::os::fd::RawFd;
|
||||
use std::os::unix::io::{AsRawFd, FromRawFd, IntoRawFd};
|
||||
|
||||
use log::info;
|
||||
use tracing::{info, instrument};
|
||||
|
||||
use libc::in6_ifreq;
|
||||
|
||||
|
|
@ -23,11 +23,13 @@ pub struct TunInterface {
|
|||
|
||||
impl TunInterface {
|
||||
#[throws]
|
||||
#[instrument]
|
||||
pub fn new() -> TunInterface {
|
||||
Self::new_with_options(TunOptions::new())?
|
||||
}
|
||||
|
||||
#[throws]
|
||||
#[instrument]
|
||||
pub(crate) fn new_with_options(options: TunOptions) -> TunInterface {
|
||||
let file = OpenOptions::new()
|
||||
.read(true)
|
||||
|
|
@ -59,6 +61,7 @@ impl TunInterface {
|
|||
}
|
||||
|
||||
#[throws]
|
||||
#[instrument]
|
||||
pub fn name(&self) -> String {
|
||||
let mut iff = unsafe { mem::zeroed() };
|
||||
unsafe { sys::tun_get_iff(self.socket.as_raw_fd(), &mut iff)? };
|
||||
|
|
@ -66,6 +69,7 @@ impl TunInterface {
|
|||
}
|
||||
|
||||
#[throws]
|
||||
#[instrument]
|
||||
fn ifreq(&self) -> sys::ifreq {
|
||||
let mut iff: sys::ifreq = unsafe { mem::zeroed() };
|
||||
iff.ifr_name = string_to_ifname(&self.name()?);
|
||||
|
|
@ -73,6 +77,7 @@ impl TunInterface {
|
|||
}
|
||||
|
||||
#[throws]
|
||||
#[instrument]
|
||||
fn in6_ifreq(&self) -> in6_ifreq {
|
||||
let mut iff: in6_ifreq = unsafe { mem::zeroed() };
|
||||
iff.ifr6_ifindex = self.index()?;
|
||||
|
|
@ -80,6 +85,7 @@ impl TunInterface {
|
|||
}
|
||||
|
||||
#[throws]
|
||||
#[instrument]
|
||||
pub fn index(&self) -> i32 {
|
||||
let mut iff = self.ifreq()?;
|
||||
self.perform(|fd| unsafe { sys::if_get_index(fd, &mut iff) })?;
|
||||
|
|
@ -87,6 +93,7 @@ impl TunInterface {
|
|||
}
|
||||
|
||||
#[throws]
|
||||
#[instrument]
|
||||
pub fn set_ipv4_addr(&self, addr: Ipv4Addr) {
|
||||
let addr = SockAddr::from(SocketAddrV4::new(addr, 0));
|
||||
let mut iff = self.ifreq()?;
|
||||
|
|
@ -96,6 +103,7 @@ impl TunInterface {
|
|||
}
|
||||
|
||||
#[throws]
|
||||
#[instrument]
|
||||
pub fn ipv4_addr(&self) -> Ipv4Addr {
|
||||
let mut iff = self.ifreq()?;
|
||||
self.perform(|fd| unsafe { sys::if_get_addr(fd, &mut iff) })?;
|
||||
|
|
@ -104,6 +112,7 @@ impl TunInterface {
|
|||
}
|
||||
|
||||
#[throws]
|
||||
#[instrument]
|
||||
pub fn set_broadcast_addr(&self, addr: Ipv4Addr) {
|
||||
let addr = SockAddr::from(SocketAddrV4::new(addr, 0));
|
||||
let mut iff = self.ifreq()?;
|
||||
|
|
@ -117,6 +126,7 @@ impl TunInterface {
|
|||
}
|
||||
|
||||
#[throws]
|
||||
#[instrument]
|
||||
pub fn broadcast_addr(&self) -> Ipv4Addr {
|
||||
let mut iff = self.ifreq()?;
|
||||
self.perform(|fd| unsafe { sys::if_get_brdaddr(fd, &mut iff) })?;
|
||||
|
|
@ -126,6 +136,7 @@ impl TunInterface {
|
|||
}
|
||||
|
||||
#[throws]
|
||||
#[instrument]
|
||||
pub fn set_ipv6_addr(&self, addr: Ipv6Addr) {
|
||||
let mut iff = self.in6_ifreq()?;
|
||||
iff.ifr6_addr.s6_addr = addr.octets();
|
||||
|
|
@ -134,6 +145,7 @@ impl TunInterface {
|
|||
}
|
||||
|
||||
#[throws]
|
||||
#[instrument]
|
||||
pub fn set_mtu(&self, mtu: i32) {
|
||||
let mut iff = self.ifreq()?;
|
||||
iff.ifr_ifru.ifru_mtu = mtu;
|
||||
|
|
@ -142,6 +154,7 @@ impl TunInterface {
|
|||
}
|
||||
|
||||
#[throws]
|
||||
#[instrument]
|
||||
pub fn mtu(&self) -> i32 {
|
||||
let mut iff = self.ifreq()?;
|
||||
self.perform(|fd| unsafe { sys::if_get_mtu(fd, &mut iff) })?;
|
||||
|
|
@ -151,6 +164,7 @@ impl TunInterface {
|
|||
}
|
||||
|
||||
#[throws]
|
||||
#[instrument]
|
||||
pub fn set_netmask(&self, addr: Ipv4Addr) {
|
||||
let addr = SockAddr::from(SocketAddrV4::new(addr, 0));
|
||||
|
||||
|
|
@ -167,6 +181,7 @@ impl TunInterface {
|
|||
}
|
||||
|
||||
#[throws]
|
||||
#[instrument]
|
||||
pub fn netmask(&self) -> Ipv4Addr {
|
||||
let mut iff = self.ifreq()?;
|
||||
self.perform(|fd| unsafe { sys::if_get_netmask(fd, &mut iff) })?;
|
||||
|
|
@ -179,17 +194,24 @@ impl TunInterface {
|
|||
|
||||
#[throws]
|
||||
fn perform<R>(&self, perform: impl FnOnce(RawFd) -> Result<R, nix::Error>) -> R {
|
||||
let span = tracing::info_span!("perform");
|
||||
let _enter = span.enter();
|
||||
|
||||
let socket = Socket::new(Domain::IPV4, Type::DGRAM, None)?;
|
||||
perform(socket.as_raw_fd())?
|
||||
}
|
||||
|
||||
#[throws]
|
||||
fn perform6<R>(&self, perform: impl FnOnce(RawFd) -> Result<R, nix::Error>) -> R {
|
||||
let span = tracing::info_span!("perform");
|
||||
let _enter = span.enter();
|
||||
|
||||
let socket = Socket::new(Domain::IPV6, Type::DGRAM, None)?;
|
||||
perform(socket.as_raw_fd())?
|
||||
}
|
||||
|
||||
#[throws]
|
||||
#[instrument]
|
||||
pub fn send(&self, buf: &[u8]) -> usize {
|
||||
self.socket.send(buf)?
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@ use std::{
|
|||
io::{Error, Read},
|
||||
os::fd::{AsRawFd, FromRawFd, IntoRawFd, RawFd},
|
||||
};
|
||||
use tracing::instrument;
|
||||
|
||||
use super::TunOptions;
|
||||
|
||||
|
|
@ -41,11 +42,13 @@ impl IntoRawFd for TunInterface {
|
|||
|
||||
impl TunInterface {
|
||||
#[throws]
|
||||
#[instrument]
|
||||
pub fn recv(&mut self, buf: &mut [u8]) -> usize {
|
||||
self.socket.read(buf)?
|
||||
}
|
||||
}
|
||||
|
||||
#[instrument]
|
||||
pub fn ifname_to_string(buf: [libc::c_char; libc::IFNAMSIZ]) -> String {
|
||||
// TODO: Switch to `CStr::from_bytes_until_nul` when stabilized
|
||||
unsafe {
|
||||
|
|
@ -56,6 +59,7 @@ pub fn ifname_to_string(buf: [libc::c_char; libc::IFNAMSIZ]) -> String {
|
|||
}
|
||||
}
|
||||
|
||||
#[instrument]
|
||||
pub fn string_to_ifname(name: &str) -> [libc::c_char; libc::IFNAMSIZ] {
|
||||
let mut buf = [0 as libc::c_char; libc::IFNAMSIZ];
|
||||
let len = name.len().min(buf.len());
|
||||
|
|
|
|||
|
|
@ -5,15 +5,18 @@ use std::{
|
|||
mem::MaybeUninit,
|
||||
os::unix::io::{AsRawFd, IntoRawFd, RawFd},
|
||||
};
|
||||
use tracing::instrument;
|
||||
|
||||
use crate::TunInterface;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct TunQueue {
|
||||
socket: socket2::Socket,
|
||||
}
|
||||
|
||||
impl TunQueue {
|
||||
#[throws]
|
||||
#[instrument]
|
||||
pub fn recv(&self, buf: &mut [MaybeUninit<u8>]) -> usize {
|
||||
self.socket.recv(buf)?
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue