use std::io;
use std::iter::Iterator;
use std::option::{Option};
use packet::ethernet::{EtherType, EthernetPacket, MutableEthernetPacket};
use util::NetworkInterface;
#[cfg(windows)]
#[path = "winpcap.rs"]
mod backend;
#[cfg(all(not(feature = "netmap"),
target_os = "linux"
)
)]
#[path = "linux.rs"]
mod backend;
#[cfg(all(not(feature = "netmap"),
any(target_os = "freebsd",
target_os = "macos")
)
)]
#[path = "bpf.rs"]
mod backend;
#[cfg(feature = "netmap")]
#[path = "netmap.rs"]
mod backend;
#[derive(Clone, Copy)]
pub enum DataLinkChannelType {
Layer2,
Layer3(EtherType)
}
#[inline]
pub fn datalink_channel(network_interface: &NetworkInterface,
write_buffer_size: usize,
read_buffer_size: usize,
channel_type: DataLinkChannelType)
-> io::Result<(DataLinkSender, DataLinkReceiver)> {
match backend::datalink_channel(network_interface, write_buffer_size, read_buffer_size,
channel_type) {
Ok((tx, rx)) => Ok((DataLinkSender { dlsi: tx }, DataLinkReceiver { dlri: rx })),
Err(e) => Err(e)
}
}
pub struct DataLinkSender {
dlsi: backend::DataLinkSenderImpl
}
impl DataLinkSender {
#[inline]
pub fn build_and_send<F>(&mut self, num_packets: usize, packet_size: usize,
func: &mut F) -> Option<io::Result<()>>
where F : FnMut(MutableEthernetPacket)
{
self.dlsi.build_and_send(num_packets, packet_size, func)
}
#[inline]
pub fn send_to(&mut self, packet: &EthernetPacket, dst: Option<NetworkInterface>)
-> Option<io::Result<()>> {
self.dlsi.send_to(packet, dst)
}
}
pub struct DataLinkReceiver {
dlri: backend::DataLinkReceiverImpl
}
impl DataLinkReceiver {
#[inline]
pub fn iter<'a>(&'a mut self) -> DataLinkChannelIterator<'a> {
DataLinkChannelIterator {
imp: self.dlri.iter()
}
}
}
pub struct DataLinkChannelIterator<'a> {
imp: backend::DataLinkChannelIteratorImpl<'a>,
}
impl<'a> DataLinkChannelIterator<'a> {
#[inline]
pub fn next<'c>(&'c mut self) -> io::Result<EthernetPacket<'c>> {
self.imp.next()
}
}