Skip to content

Commit 66db029

Browse files
author
Artem Kryvokrysenko
committed
tracer: update public API for smoltcp::phy::Tracer to allow custom inspection and printing of packet
Currently `smoltcp::phy::tracer::Packet` is not exported as public type, which leaves very limited options for using `Tracer` device: traced packets only can be directly printed "as-is" and only in limited context (inside the closure). I'm updating API of `Tracer` device to provide access to internal fields of traced packet; this will enable implementation of custom printing or packet inspection functions I renamed `smoltcp::phy::tracer::Packet` to `smoltcp::phy::tracer::TracerPacket`, following prefixing convention used by types declared in `smoltcp::phy::pcap_writer`
1 parent a54589c commit 66db029

File tree

2 files changed

+54
-14
lines changed

2 files changed

+54
-14
lines changed

src/phy/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ pub use self::loopback::Loopback;
125125
pub use self::pcap_writer::{PcapLinkType, PcapMode, PcapSink, PcapWriter};
126126
#[cfg(all(feature = "phy-raw_socket", unix))]
127127
pub use self::raw_socket::RawSocket;
128-
pub use self::tracer::Tracer;
128+
pub use self::tracer::{Tracer, TracerDirection, TracerPacket};
129129
#[cfg(all(
130130
feature = "phy-tuntap_interface",
131131
any(target_os = "linux", target_os = "android")

src/phy/tracer.rs

Lines changed: 53 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,11 @@ use crate::wire::pretty_print::{PrettyIndent, PrettyPrint};
1111
/// device.
1212
pub struct Tracer<D: Device> {
1313
inner: D,
14-
writer: fn(Instant, Packet),
14+
writer: fn(Instant, TracerPacket),
1515
}
1616

1717
impl<D: Device> Tracer<D> {
18-
/// Create a tracer device.
19-
pub fn new(inner: D, writer: fn(timestamp: Instant, packet: Packet)) -> Tracer<D> {
18+
pub fn new(inner: D, writer: fn(timestamp: Instant, packet: TracerPacket)) -> Tracer<D> {
2019
Tracer { inner, writer }
2120
}
2221

@@ -88,7 +87,7 @@ impl<D: Device> Device for Tracer<D> {
8887
#[doc(hidden)]
8988
pub struct RxToken<Rx: phy::RxToken> {
9089
token: Rx,
91-
writer: fn(Instant, Packet),
90+
writer: fn(Instant, TracerPacket),
9291
medium: Medium,
9392
timestamp: Instant,
9493
}
@@ -101,10 +100,10 @@ impl<Rx: phy::RxToken> phy::RxToken for RxToken<Rx> {
101100
self.token.consume(|buffer| {
102101
(self.writer)(
103102
self.timestamp,
104-
Packet {
103+
TracerPacket {
105104
buffer,
106105
medium: self.medium,
107-
prefix: "<- ",
106+
direction: TracerDirection::RX,
108107
},
109108
);
110109
f(buffer)
@@ -119,7 +118,7 @@ impl<Rx: phy::RxToken> phy::RxToken for RxToken<Rx> {
119118
#[doc(hidden)]
120119
pub struct TxToken<Tx: phy::TxToken> {
121120
token: Tx,
122-
writer: fn(Instant, Packet),
121+
writer: fn(Instant, TracerPacket),
123122
medium: Medium,
124123
timestamp: Instant,
125124
}
@@ -133,10 +132,10 @@ impl<Tx: phy::TxToken> phy::TxToken for TxToken<Tx> {
133132
let result = f(buffer);
134133
(self.writer)(
135134
self.timestamp,
136-
Packet {
135+
TracerPacket {
137136
buffer,
138137
medium: self.medium,
139-
prefix: "-> ",
138+
direction: TracerDirection::TX,
140139
},
141140
);
142141
result
@@ -148,15 +147,56 @@ impl<Tx: phy::TxToken> phy::TxToken for TxToken<Tx> {
148147
}
149148
}
150149

151-
pub struct Packet<'a> {
150+
/// Packet which is being traced by [Tracer](struct.Tracer.html) device.
151+
pub struct TracerPacket<'a> {
152152
buffer: &'a [u8],
153153
medium: Medium,
154-
prefix: &'static str,
154+
direction: TracerDirection,
155155
}
156156

157-
impl<'a> fmt::Display for Packet<'a> {
157+
impl<'a> TracerPacket<'a> {
158+
/// Creates new [TracerPacket](struct.TracerPacket.html)
159+
pub fn new(buffer: &'a [u8], medium: Medium, direction: TracerDirection) -> Self {
160+
Self {
161+
buffer,
162+
medium,
163+
direction,
164+
}
165+
}
166+
167+
/// Returns packet buffer
168+
pub fn buffer(&'a self) -> &'a [u8] {
169+
self.buffer
170+
}
171+
172+
/// Returns packet medium
173+
pub fn medium(&self) -> Medium {
174+
self.medium
175+
}
176+
177+
/// Returns direction in which packet being traced
178+
pub fn direction(&self) -> TracerDirection {
179+
self.direction
180+
}
181+
}
182+
183+
/// Direction on which packet is being traced
184+
#[derive(Debug, Clone, Copy)]
185+
pub enum TracerDirection {
186+
/// Packet is received by Smoltcp interface
187+
RX,
188+
/// Packet is transmitted by Smoltcp interface
189+
TX,
190+
}
191+
192+
impl<'a> fmt::Display for TracerPacket<'a> {
158193
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
159-
let mut indent = PrettyIndent::new(self.prefix);
194+
let prefix = match self.direction {
195+
TracerDirection::RX => "<- ",
196+
TracerDirection::TX => "-> ",
197+
};
198+
199+
let mut indent = PrettyIndent::new(prefix);
160200
match self.medium {
161201
#[cfg(feature = "medium-ethernet")]
162202
Medium::Ethernet => crate::wire::EthernetFrame::<&'static [u8]>::pretty_print(

0 commit comments

Comments
 (0)