tcpdump.rs 731 B

123456789101112131415161718192021222324
  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()).unwrap();
  10. loop {
  11. phy_wait(socket.as_raw_fd(), None).unwrap();
  12. let (rx_token, _) = socket.receive().unwrap();
  13. rx_token
  14. .consume(Instant::now(), |buffer| {
  15. println!(
  16. "{}",
  17. PrettyPrinter::<EthernetFrame<&[u8]>>::new("", &buffer)
  18. );
  19. Ok(())
  20. })
  21. .unwrap();
  22. }
  23. }