pub struct L2Sender { /* private fields */ }Expand description
Sender for raw L2 Ethernet frames via AF_PACKET socket.
Implementations§
Source§impl L2Sender
impl L2Sender
Sourcepub fn new(ifname: &str) -> Result<Self, IoError>
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}Sourcepub fn send(&self, packet: &[u8]) -> Result<usize, IoError>
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§
Auto Trait Implementations§
impl Freeze for L2Sender
impl RefUnwindSafe for L2Sender
impl Send for L2Sender
impl Sync for L2Sender
impl Unpin for L2Sender
impl UnsafeUnpin for L2Sender
impl UnwindSafe for L2Sender
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more