-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
28d8fc4
commit 8a80340
Showing
5 changed files
with
102 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,6 +26,7 @@ pub enum MaybeString<T> | |
where | ||
T: Config, | ||
{ | ||
#[cfg(feature = "libpcap")] | ||
String(String), | ||
Other(T), | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
use std::io; | ||
|
||
use crate::config::RawNetConfig; | ||
use pcap::{Capture, Device}; | ||
use rd_interface::{Error, ErrorContext, Result}; | ||
use tokio_smoltcp::{ | ||
device::{AsyncDevice, DeviceCapabilities}, | ||
smoltcp::{phy::Checksum, wire::IpCidr}, | ||
}; | ||
|
||
pub(super) fn get_filter(ip_cidr: &IpCidr) -> Option<String> { | ||
match ip_cidr { | ||
IpCidr::Ipv4(v4) => Some(format!("net {}", v4.network())), | ||
_ => None, | ||
} | ||
} | ||
|
||
pub(super) fn pcap_device_by_name(name: &str) -> Result<Device> { | ||
let mut devices = Device::list().context("Failed to list device")?; | ||
|
||
if let Some(id) = devices.iter().position(|d| d.name == name) { | ||
Ok(devices.remove(id)) | ||
} else { | ||
Err(Error::Other( | ||
format!("Failed to find device: {}", name,).into(), | ||
)) | ||
} | ||
} | ||
|
||
#[cfg(unix)] | ||
pub(super) fn get_by_device( | ||
device: Device, | ||
filter: Option<String>, | ||
config: &RawNetConfig, | ||
) -> Result<impl AsyncDevice> { | ||
use tokio_smoltcp::device::AsyncCapture; | ||
|
||
let mut cap = Capture::from_device(device) | ||
.context("Failed to capture device")? | ||
.promisc(true) | ||
.immediate_mode(true) | ||
.timeout(500) | ||
.open() | ||
.context("Failed to open device")?; | ||
|
||
if let Some(filter) = filter { | ||
cap.filter(&filter, true).context("Failed to add filter")?; | ||
} | ||
|
||
fn map_err(e: pcap::Error) -> io::Error { | ||
match e { | ||
pcap::Error::IoError(e) => e.into(), | ||
pcap::Error::TimeoutExpired => io::ErrorKind::WouldBlock.into(), | ||
other => io::Error::new(io::ErrorKind::Other, other), | ||
} | ||
} | ||
let mut caps = DeviceCapabilities::default(); | ||
caps.max_transmission_unit = config.mtu; | ||
caps.checksum.ipv4 = Checksum::Tx; | ||
caps.checksum.tcp = Checksum::Tx; | ||
caps.checksum.udp = Checksum::Tx; | ||
caps.checksum.icmpv4 = Checksum::Tx; | ||
caps.checksum.icmpv6 = Checksum::Tx; | ||
|
||
AsyncCapture::new( | ||
cap.setnonblock().context("Failed to set nonblock")?, | ||
|d| d.next().map_err(map_err).map(|p| p.to_vec()), | ||
|d, pkt| d.sendpacket(pkt).map_err(map_err), | ||
caps, | ||
) | ||
.context("Failed to create async capture") | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
use tokio_smoltcp::smoltcp::wire::Ipv4Address; | ||
|
||
#[test] | ||
fn test_get_filter() { | ||
let filter = get_filter(&IpCidr::new(Ipv4Address::new(192, 168, 1, 1).into(), 24)); | ||
|
||
assert_eq!(filter, Some("net 192.168.1.0/24".to_string())); | ||
} | ||
} |