mirror of
https://github.com/smoltcp-rs/smoltcp.git
synced 2025-10-02 15:15:05 +00:00

Various parts of smoltcp require an arrow of time; a monotonically increasing timestamp. Most obviously this is TCP sockets, but the tracer and the pcap writer devices also benefit from having timestamps. There are a few ways this could be implemented: 1. using a static Cell, global for the entire smoltcp crate; 2. using a static method on Device; 3. using an instance method on Device; 4. passing the current timestamp into *Interface::poll. The first two options are undesirable because they create a notion of global clock, and interfere e.g. with mocking. The third option is undesirable because not all devices are inherently tied to a particular clock, e.g. a loopback device isn't. Therefore, the timestamp is injected into both sockets and devices through the *Interface::poll method.
15 lines
409 B
Rust
15 lines
409 B
Rust
extern crate smoltcp;
|
|
|
|
use std::env;
|
|
use smoltcp::phy::{Device, RawSocket};
|
|
use smoltcp::wire::{PrettyPrinter, EthernetFrame};
|
|
|
|
fn main() {
|
|
let ifname = env::args().nth(1).unwrap();
|
|
let mut socket = RawSocket::new(ifname.as_ref()).unwrap();
|
|
loop {
|
|
let buffer = socket.receive(/*timestamp=*/0).unwrap();
|
|
print!("{}", PrettyPrinter::<EthernetFrame<&[u8]>>::new("", &buffer))
|
|
}
|
|
}
|