extern crate libc;
extern crate ipnetwork;
extern crate pnet_base;
extern crate pnet_sys;
use std::io;
use std::option::Option;
use std::time::Duration;
use ipnetwork::IpNetwork;
pub use pnet_base::{MacAddr, ParseMacAddrErr};
mod bindings;
#[cfg(windows)]
#[path = "winpcap.rs"]
mod backend;
#[cfg(windows)]
pub mod winpcap;
#[cfg(all(not(feature = "netmap"),
target_os = "linux"
)
)]
#[path = "linux.rs"]
mod backend;
#[cfg(target_os = "linux")]
pub mod linux;
#[cfg(all(not(feature = "netmap"),
any(target_os = "freebsd",
target_os = "macos")
)
)]
#[path = "bpf.rs"]
mod backend;
#[cfg(any(target_os = "freebsd", target_os = "macos"))]
pub mod bpf;
#[cfg(feature = "netmap")]
#[path = "netmap.rs"]
mod backend;
#[cfg(feature = "netmap")]
pub mod netmap;
#[cfg(feature = "pcap")]
pub mod pcap;
pub mod dummy;
pub type EtherType = u16;
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub enum ChannelType {
Layer2,
Layer3(EtherType),
}
pub enum Channel {
Ethernet(Box<DataLinkSender>, Box<DataLinkReceiver>),
PleaseIncludeACatchAllVariantWhenMatchingOnThisEnum,
}
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub struct Config {
pub write_buffer_size: usize,
pub read_buffer_size: usize,
pub read_timeout: Option<Duration>,
pub write_timeout: Option<Duration>,
pub channel_type: ChannelType,
pub bpf_fd_attempts: usize,
}
impl Default for Config {
fn default() -> Config {
Config {
write_buffer_size: 4096,
read_buffer_size: 4096,
channel_type: ChannelType::Layer2,
bpf_fd_attempts: 1000,
read_timeout: None,
write_timeout: None,
}
}
}
#[inline]
pub fn channel(network_interface: &NetworkInterface, configuration: Config) -> io::Result<Channel> {
backend::channel(network_interface, (&configuration).into())
}
pub trait DataLinkSender: Send {
#[inline]
fn build_and_send(&mut self,
num_packets: usize,
packet_size: usize,
func: &mut FnMut(&mut [u8]))
-> Option<io::Result<()>>;
#[inline]
fn send_to(&mut self,
packet: &[u8],
dst: Option<NetworkInterface>)
-> Option<io::Result<()>>;
}
pub trait DataLinkReceiver: Send {
#[inline]
fn next(&mut self) -> io::Result<&[u8]>;
}
#[derive(Clone, PartialEq, Eq, Debug, Hash)]
pub struct NetworkInterface {
pub name: String,
pub index: u32,
pub mac: Option<MacAddr>,
pub ips: Vec<IpNetwork>,
pub flags: u32,
}
impl NetworkInterface {
pub fn mac_address(&self) -> MacAddr {
self.mac.unwrap()
}
pub fn is_loopback(&self) -> bool {
self.flags & (pnet_sys::IFF_LOOPBACK as u32) != 0
}
}
pub fn interfaces() -> Vec<NetworkInterface> {
backend::interfaces()
}