chore: run cargo fmt
This commit is contained in:
@@ -1,22 +1,24 @@
|
||||
// Copyright (C) 2023 Andrew Rioux
|
||||
//
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Affero General Public License as
|
||||
// published by the Free Software Foundation, either version 3 of the
|
||||
// License, or (at your option) any later version.
|
||||
//
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU Affero General Public License for more details.
|
||||
//
|
||||
//
|
||||
// You should have received a copy of the GNU Affero General Public License
|
||||
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
use errno::Errno;
|
||||
use std::{
|
||||
convert::From,
|
||||
ffi::{self, CStr, CString}, fmt::Display, error,
|
||||
error,
|
||||
ffi::{self, CStr, CString},
|
||||
fmt::Display,
|
||||
};
|
||||
|
||||
#[derive(Debug)]
|
||||
@@ -42,17 +44,20 @@ impl Display for Error {
|
||||
}
|
||||
|
||||
write!(f, "unknown pcap error")
|
||||
},
|
||||
}
|
||||
Error::PcapErrorIf(ifname, err) => {
|
||||
if let Ok(err_str) = std::str::from_utf8(err.as_bytes()) {
|
||||
return write!(f, "pcap error on interface {ifname}: {err_str}");
|
||||
}
|
||||
|
||||
write!(f, "unknown pcap error with interface {ifname}")
|
||||
},
|
||||
}
|
||||
Error::StringParse => write!(f, "unable to parse a string from pcap"),
|
||||
Error::UnknownPacketType(ptype) => write!(f, "unknown packet type ({ptype})"),
|
||||
Error::PacketLengthInvalid => write!(f, "received a packet with a length that mismatched the header"),
|
||||
Error::PacketLengthInvalid => write!(
|
||||
f,
|
||||
"received a packet with a length that mismatched the header"
|
||||
),
|
||||
Error::InvalidPcapFd => write!(f, "internal pcap file descriptor error"),
|
||||
Error::Io(io) => write!(f, "std::io error ({io})"),
|
||||
Error::Libc(err) => write!(f, "libc error ({err})"),
|
||||
@@ -64,7 +69,7 @@ impl Error {
|
||||
pub fn add_ifname(self, ifname: &str) -> Self {
|
||||
match self {
|
||||
Error::PcapError(err) => Error::PcapErrorIf(ifname.to_string(), err),
|
||||
other => other
|
||||
other => other,
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -74,7 +79,7 @@ impl error::Error for Error {
|
||||
match self {
|
||||
Error::Io(err) => Some(err),
|
||||
Error::Libc(err) => Some(err),
|
||||
_ => None
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,15 +1,15 @@
|
||||
// Copyright (C) 2023 Andrew Rioux
|
||||
//
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Affero General Public License as
|
||||
// published by the Free Software Foundation, either version 3 of the
|
||||
// License, or (at your option) any later version.
|
||||
//
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU Affero General Public License for more details.
|
||||
//
|
||||
//
|
||||
// You should have received a copy of the GNU Affero General Public License
|
||||
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
|
||||
@@ -1,24 +1,25 @@
|
||||
// Copyright (C) 2023 Andrew Rioux
|
||||
//
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Affero General Public License as
|
||||
// published by the Free Software Foundation, either version 3 of the
|
||||
// License, or (at your option) any later version.
|
||||
//
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU Affero General Public License for more details.
|
||||
//
|
||||
//
|
||||
// You should have received a copy of the GNU Affero General Public License
|
||||
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
use std::{
|
||||
collections::HashMap,
|
||||
ffi::{CStr, CString},
|
||||
os::fd::{AsRawFd, RawFd},
|
||||
pin::Pin,
|
||||
ptr, slice,
|
||||
task::{self, Poll}, collections::HashMap,
|
||||
task::{self, Poll},
|
||||
};
|
||||
|
||||
pub mod error;
|
||||
@@ -470,9 +471,12 @@ impl<T: Activated> futures::Stream for InterfaceStream<T> {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn new_aggregate_interface_filtered<F>(crash: bool, mut f: F) -> error::Result<AggregateInterface<DevDisabled>>
|
||||
pub fn new_aggregate_interface_filtered<F>(
|
||||
crash: bool,
|
||||
mut f: F,
|
||||
) -> error::Result<AggregateInterface<DevDisabled>>
|
||||
where
|
||||
F: FnMut(&str) -> bool
|
||||
F: FnMut(&str) -> bool,
|
||||
{
|
||||
let interfaces = if crash {
|
||||
PcapDevIterator::new()?
|
||||
@@ -500,10 +504,7 @@ where
|
||||
.collect::<HashMap<_, _>>()
|
||||
};
|
||||
|
||||
Ok(AggregateInterface {
|
||||
interfaces,
|
||||
crash
|
||||
})
|
||||
Ok(AggregateInterface { interfaces, crash })
|
||||
}
|
||||
|
||||
pub fn new_aggregate_interface(crash: bool) -> error::Result<AggregateInterface<DevDisabled>> {
|
||||
@@ -512,14 +513,13 @@ pub fn new_aggregate_interface(crash: bool) -> error::Result<AggregateInterface<
|
||||
|
||||
pub struct AggregateInterface<T: State> {
|
||||
interfaces: HashMap<String, Interface<T>>,
|
||||
crash: bool
|
||||
crash: bool,
|
||||
}
|
||||
|
||||
impl<T: State> AggregateInterface<T> {
|
||||
pub fn set_non_blocking(&mut self, nonblocking: bool) -> error::Result<()> {
|
||||
for (n, i) in self.interfaces.iter_mut() {
|
||||
i
|
||||
.set_non_blocking(nonblocking)
|
||||
i.set_non_blocking(nonblocking)
|
||||
.map_err(|e| e.add_ifname(n))?;
|
||||
}
|
||||
|
||||
@@ -571,33 +571,33 @@ impl<T: Disabled> AggregateInterface<T> {
|
||||
pub fn activate(self) -> error::Result<AggregateInterface<DevActivated>> {
|
||||
Ok(AggregateInterface {
|
||||
interfaces: if self.crash {
|
||||
self.interfaces
|
||||
.into_iter()
|
||||
.map(|(name, interface)| {
|
||||
let new_name = name.clone();
|
||||
interface
|
||||
.activate()
|
||||
.map(|interface| (name, interface))
|
||||
.map_err(|e| e.add_ifname(&new_name))
|
||||
})
|
||||
.collect::<error::Result<_>>()?
|
||||
} else {
|
||||
self.interfaces
|
||||
.into_iter()
|
||||
.filter_map(|(name, interface)| {
|
||||
let name_clone = name.clone();
|
||||
interface
|
||||
.activate()
|
||||
.map(|interface| (name, interface))
|
||||
.ok()
|
||||
.or_else(|| {
|
||||
println!("{} failed to activate", name_clone);
|
||||
None
|
||||
})
|
||||
})
|
||||
.collect::<_>()
|
||||
},
|
||||
crash: self.crash
|
||||
self.interfaces
|
||||
.into_iter()
|
||||
.map(|(name, interface)| {
|
||||
let new_name = name.clone();
|
||||
interface
|
||||
.activate()
|
||||
.map(|interface| (name, interface))
|
||||
.map_err(|e| e.add_ifname(&new_name))
|
||||
})
|
||||
.collect::<error::Result<_>>()?
|
||||
} else {
|
||||
self.interfaces
|
||||
.into_iter()
|
||||
.filter_map(|(name, interface)| {
|
||||
let name_clone = name.clone();
|
||||
interface
|
||||
.activate()
|
||||
.map(|interface| (name, interface))
|
||||
.ok()
|
||||
.or_else(|| {
|
||||
println!("{} failed to activate", name_clone);
|
||||
None
|
||||
})
|
||||
})
|
||||
.collect::<_>()
|
||||
},
|
||||
crash: self.crash,
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -606,25 +606,18 @@ impl<T: Activated> AggregateInterface<T> {
|
||||
pub fn datalinks(&self) -> HashMap<&str, i32> {
|
||||
self.interfaces
|
||||
.iter()
|
||||
.map(|(name, interface)| {
|
||||
(&**name, interface.datalink())
|
||||
})
|
||||
.map(|(name, interface)| (&**name, interface.datalink()))
|
||||
.collect::<_>()
|
||||
}
|
||||
|
||||
pub fn prune<F>(&mut self, mut f: F)
|
||||
where
|
||||
F: FnMut(&str, &mut Interface<T>) -> bool
|
||||
F: FnMut(&str, &mut Interface<T>) -> bool,
|
||||
{
|
||||
let to_prune = self.interfaces
|
||||
let to_prune = self
|
||||
.interfaces
|
||||
.iter_mut()
|
||||
.filter_map(|(k,v)| {
|
||||
if (f)(k, v) {
|
||||
Some(k.clone())
|
||||
} else {
|
||||
None
|
||||
}
|
||||
})
|
||||
.filter_map(|(k, v)| if (f)(k, v) { Some(k.clone()) } else { None })
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
for name in to_prune {
|
||||
@@ -636,23 +629,26 @@ impl<T: Activated> AggregateInterface<T> {
|
||||
&mut self,
|
||||
filter: &str,
|
||||
optimize: bool,
|
||||
mask: Option<u32>
|
||||
mask: Option<u32>,
|
||||
) -> error::Result<HashMap<&str, Box<ffi::BpfProgram>>> {
|
||||
if self.crash {
|
||||
self.interfaces
|
||||
.iter_mut()
|
||||
.map(|(name, interface)| {
|
||||
interface.set_filter(filter, optimize, mask)
|
||||
interface
|
||||
.set_filter(filter, optimize, mask)
|
||||
.map(|bpf| (&**name, bpf))
|
||||
.map_err(|e| e.add_ifname(&name))
|
||||
})
|
||||
.collect::<error::Result<_>>()
|
||||
} else {
|
||||
Ok(self.interfaces
|
||||
Ok(self
|
||||
.interfaces
|
||||
.iter_mut()
|
||||
.filter_map(|(name, interface)| {
|
||||
let name_clone = name.clone();
|
||||
interface.set_filter(filter, optimize, mask)
|
||||
interface
|
||||
.set_filter(filter, optimize, mask)
|
||||
.map(|bpf| (&**name, bpf))
|
||||
.ok()
|
||||
.or_else(|| {
|
||||
@@ -666,7 +662,9 @@ impl<T: Activated> AggregateInterface<T> {
|
||||
|
||||
pub fn sendpacket(&self, ifname: &str, packet: packets::EthernetPkt) -> error::Result<()> {
|
||||
if let Some(interface) = self.interfaces.get(ifname) {
|
||||
interface.sendpacket(packet).map_err(|e| e.add_ifname(ifname))?;
|
||||
interface
|
||||
.sendpacket(packet)
|
||||
.map_err(|e| e.add_ifname(ifname))?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
@@ -676,7 +674,8 @@ impl<T: Activated> AggregateInterface<T> {
|
||||
impl<T: NotListening> AggregateInterface<T> {
|
||||
pub fn stream(self) -> error::Result<AggregateInterfaceStream<DevActivated>> {
|
||||
Ok(AggregateInterfaceStream {
|
||||
streams: self.interfaces
|
||||
streams: self
|
||||
.interfaces
|
||||
.into_iter()
|
||||
.map(|(ifname, interface)| {
|
||||
let new_name = ifname.clone();
|
||||
@@ -685,13 +684,13 @@ impl<T: NotListening> AggregateInterface<T> {
|
||||
.map(|stream| (ifname, stream))
|
||||
.map_err(|e| e.add_ifname(&new_name))
|
||||
})
|
||||
.collect::<error::Result<_>>()?
|
||||
.collect::<error::Result<_>>()?,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
pub struct AggregateInterfaceStream<T: Activated> {
|
||||
streams: StreamMap<String, InterfaceStream<T>>
|
||||
streams: StreamMap<String, InterfaceStream<T>>,
|
||||
}
|
||||
|
||||
impl<T: Activated> AggregateInterfaceStream<T> {
|
||||
@@ -700,15 +699,9 @@ impl<T: Activated> AggregateInterfaceStream<T> {
|
||||
}
|
||||
|
||||
pub fn sendpacket(&mut self, ifname: &str, packet: packets::EthernetPkt) -> error::Result<()> {
|
||||
if let Some(interface) = self.streams
|
||||
.values_mut()
|
||||
.find(|interface| {
|
||||
interface.inner
|
||||
.get_ref()
|
||||
.interface
|
||||
.dev_name
|
||||
.as_bytes() == ifname.as_bytes()
|
||||
}) {
|
||||
if let Some(interface) = self.streams.values_mut().find(|interface| {
|
||||
interface.inner.get_ref().interface.dev_name.as_bytes() == ifname.as_bytes()
|
||||
}) {
|
||||
interface.sendpacket(packet)?;
|
||||
}
|
||||
|
||||
@@ -722,4 +715,4 @@ impl<T: Activated> futures::Stream for AggregateInterfaceStream<T> {
|
||||
fn poll_next(mut self: Pin<&mut Self>, cx: &mut task::Context<'_>) -> Poll<Option<Self::Item>> {
|
||||
self.streams.poll_next_unpin(cx)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,15 +1,15 @@
|
||||
// Copyright (C) 2023 Andrew Rioux
|
||||
//
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Affero General Public License as
|
||||
// published by the Free Software Foundation, either version 3 of the
|
||||
// License, or (at your option) any later version.
|
||||
//
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU Affero General Public License for more details.
|
||||
//
|
||||
//
|
||||
// You should have received a copy of the GNU Affero General Public License
|
||||
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
|
||||
Reference in New Issue
Block a user