tcpdump.rs 682 B

123456789101112131415161718192021
  1. use smoltcp::phy::wait as phy_wait;
  2. use smoltcp::phy::{Device, RawSocket, RxToken};
  3. use smoltcp::time::Instant;
  4. use smoltcp::wire::{EthernetFrame, PrettyPrinter};
  5. use std::env;
  6. use std::os::unix::io::AsRawFd;
  7. fn main() {
  8. let ifname = env::args().nth(1).unwrap();
  9. let mut socket = RawSocket::new(ifname.as_ref(), smoltcp::phy::Medium::Ethernet).unwrap();
  10. loop {
  11. phy_wait(socket.as_raw_fd(), None).unwrap();
  12. let (rx_token, _) = socket.receive(Instant::now()).unwrap();
  13. rx_token.consume(|buffer| {
  14. println!(
  15. "{}",
  16. PrettyPrinter::<EthernetFrame<&[u8]>>::new("", &buffer)
  17. );
  18. })
  19. }
  20. }