Skip to main content

L2Sender

Struct L2Sender 

Source
pub struct L2Sender { /* private fields */ }
Expand description

Sender for raw L2 Ethernet frames via AF_PACKET socket.

Implementations§

Source§

impl L2Sender

Source

pub fn new(ifname: &str) -> Result<Self, IoError>

Open an AF_PACKET socket for sending on the given interface.

Examples found in repository?
examples/cross_platform_send.rs (line 49)
20fn main() -> Result<(), Box<dyn std::error::Error>> {
21    let ifname = std::env::args().nth(1).unwrap_or_else(|| "eth0".into());
22    println!("Sending on {} ...", ifname);
23
24    // Build ICMP Echo Request
25    let icmp = IcmpPacketBuilder::echo_request()
26        .identifier(0x0001)
27        .sequence_number(1)
28        .payload(b"wireforge!")
29        .build();
30
31    // Build IPv4 packet
32    let ipv4 = Ipv4PacketBuilder::new()
33        .source(Ipv4Addr::new(192, 168, 1, 100))
34        .destination(Ipv4Addr::new(192, 168, 1, 1))
35        .protocol(IpProtocol::Icmp)
36        .ttl(64)
37        .payload(&icmp)
38        .unwrap()
39        .build();
40
41    // Build Ethernet frame (broadcast)
42    let frame = EthernetPacketBuilder::new()
43        .dst_mac([0xFF; 6])
44        .src_mac([0x00, 0x11, 0x22, 0x33, 0x44, 0x55])
45        .ethertype(EtherType::Ipv4)
46        .payload(&ipv4)
47        .build();
48
49    let tx = DefaultL2Writer::new(&ifname)?;
50    let n = tx.send(&frame)?;
51    println!("Sent {} bytes", n);
52    Ok(())
53}
Source

pub fn send(&self, packet: &[u8]) -> Result<usize, IoError>

Send raw L2 bytes on the interface.

Examples found in repository?
examples/cross_platform_send.rs (line 50)
20fn main() -> Result<(), Box<dyn std::error::Error>> {
21    let ifname = std::env::args().nth(1).unwrap_or_else(|| "eth0".into());
22    println!("Sending on {} ...", ifname);
23
24    // Build ICMP Echo Request
25    let icmp = IcmpPacketBuilder::echo_request()
26        .identifier(0x0001)
27        .sequence_number(1)
28        .payload(b"wireforge!")
29        .build();
30
31    // Build IPv4 packet
32    let ipv4 = Ipv4PacketBuilder::new()
33        .source(Ipv4Addr::new(192, 168, 1, 100))
34        .destination(Ipv4Addr::new(192, 168, 1, 1))
35        .protocol(IpProtocol::Icmp)
36        .ttl(64)
37        .payload(&icmp)
38        .unwrap()
39        .build();
40
41    // Build Ethernet frame (broadcast)
42    let frame = EthernetPacketBuilder::new()
43        .dst_mac([0xFF; 6])
44        .src_mac([0x00, 0x11, 0x22, 0x33, 0x44, 0x55])
45        .ethertype(EtherType::Ipv4)
46        .payload(&ipv4)
47        .build();
48
49    let tx = DefaultL2Writer::new(&ifname)?;
50    let n = tx.send(&frame)?;
51    println!("Sent {} bytes", n);
52    Ok(())
53}

Trait Implementations§

Source§

impl L2Writer for L2Sender

Source§

fn send(&self, packet: &[u8]) -> Result<usize, IoError>

Send raw bytes as an Ethernet frame. Returns the number of bytes sent.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.